]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/pt.c
Implement P0732R2, class types in non-type template parameters.
[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 auto_diagnostic_group d;
804 if (permerror (input_location,
805 "specialization of %qD in different namespace", tmpl))
806 inform (DECL_SOURCE_LOCATION (tmpl),
807 " from definition of %q#D", tmpl);
808 return false;
809 }
810 }
811
812 /* SPEC is an explicit instantiation. Check that it is valid to
813 perform this explicit instantiation in the current namespace. */
814
815 static void
816 check_explicit_instantiation_namespace (tree spec)
817 {
818 tree ns;
819
820 /* DR 275: An explicit instantiation shall appear in an enclosing
821 namespace of its template. */
822 ns = decl_namespace_context (spec);
823 if (!is_nested_namespace (current_namespace, ns))
824 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
825 "(which does not enclose namespace %qD)",
826 spec, current_namespace, ns);
827 }
828
829 // Returns the type of a template specialization only if that
830 // specialization needs to be defined. Otherwise (e.g., if the type has
831 // already been defined), the function returns NULL_TREE.
832 static tree
833 maybe_new_partial_specialization (tree type)
834 {
835 // An implicit instantiation of an incomplete type implies
836 // the definition of a new class template.
837 //
838 // template<typename T>
839 // struct S;
840 //
841 // template<typename T>
842 // struct S<T*>;
843 //
844 // Here, S<T*> is an implicit instantiation of S whose type
845 // is incomplete.
846 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
847 return type;
848
849 // It can also be the case that TYPE is a completed specialization.
850 // Continuing the previous example, suppose we also declare:
851 //
852 // template<typename T>
853 // requires Integral<T>
854 // struct S<T*>;
855 //
856 // Here, S<T*> refers to the specialization S<T*> defined
857 // above. However, we need to differentiate definitions because
858 // we intend to define a new partial specialization. In this case,
859 // we rely on the fact that the constraints are different for
860 // this declaration than that above.
861 //
862 // Note that we also get here for injected class names and
863 // late-parsed template definitions. We must ensure that we
864 // do not create new type declarations for those cases.
865 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
866 {
867 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
868 tree args = CLASSTYPE_TI_ARGS (type);
869
870 // If there are no template parameters, this cannot be a new
871 // partial template specializtion?
872 if (!current_template_parms)
873 return NULL_TREE;
874
875 // The injected-class-name is not a new partial specialization.
876 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
877 return NULL_TREE;
878
879 // If the constraints are not the same as those of the primary
880 // then, we can probably create a new specialization.
881 tree type_constr = current_template_constraints ();
882
883 if (type == TREE_TYPE (tmpl))
884 {
885 tree main_constr = get_constraints (tmpl);
886 if (equivalent_constraints (type_constr, main_constr))
887 return NULL_TREE;
888 }
889
890 // Also, if there's a pre-existing specialization with matching
891 // constraints, then this also isn't new.
892 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
893 while (specs)
894 {
895 tree spec_tmpl = TREE_VALUE (specs);
896 tree spec_args = TREE_PURPOSE (specs);
897 tree spec_constr = get_constraints (spec_tmpl);
898 if (comp_template_args (args, spec_args)
899 && equivalent_constraints (type_constr, spec_constr))
900 return NULL_TREE;
901 specs = TREE_CHAIN (specs);
902 }
903
904 // Create a new type node (and corresponding type decl)
905 // for the newly declared specialization.
906 tree t = make_class_type (TREE_CODE (type));
907 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
908 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
909
910 /* We only need a separate type node for storing the definition of this
911 partial specialization; uses of S<T*> are unconstrained, so all are
912 equivalent. So keep TYPE_CANONICAL the same. */
913 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
914
915 // Build the corresponding type decl.
916 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
917 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
918 DECL_SOURCE_LOCATION (d) = input_location;
919
920 return t;
921 }
922
923 return NULL_TREE;
924 }
925
926 /* The TYPE is being declared. If it is a template type, that means it
927 is a partial specialization. Do appropriate error-checking. */
928
929 tree
930 maybe_process_partial_specialization (tree type)
931 {
932 tree context;
933
934 if (type == error_mark_node)
935 return error_mark_node;
936
937 /* A lambda that appears in specialization context is not itself a
938 specialization. */
939 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
940 return type;
941
942 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
943 {
944 error ("name of class shadows template template parameter %qD",
945 TYPE_NAME (type));
946 return error_mark_node;
947 }
948
949 context = TYPE_CONTEXT (type);
950
951 if (TYPE_ALIAS_P (type))
952 {
953 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
954
955 if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
956 error ("specialization of alias template %qD",
957 TI_TEMPLATE (tinfo));
958 else
959 error ("explicit specialization of non-template %qT", type);
960 return error_mark_node;
961 }
962 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
963 {
964 /* This is for ordinary explicit specialization and partial
965 specialization of a template class such as:
966
967 template <> class C<int>;
968
969 or:
970
971 template <class T> class C<T*>;
972
973 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
974
975 if (tree t = maybe_new_partial_specialization (type))
976 {
977 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
978 && !at_namespace_scope_p ())
979 return error_mark_node;
980 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
981 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
982 if (processing_template_decl)
983 {
984 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
985 if (decl == error_mark_node)
986 return error_mark_node;
987 return TREE_TYPE (decl);
988 }
989 }
990 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
991 error ("specialization of %qT after instantiation", type);
992 else if (errorcount && !processing_specialization
993 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
994 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
995 /* Trying to define a specialization either without a template<> header
996 or in an inappropriate place. We've already given an error, so just
997 bail now so we don't actually define the specialization. */
998 return error_mark_node;
999 }
1000 else if (CLASS_TYPE_P (type)
1001 && !CLASSTYPE_USE_TEMPLATE (type)
1002 && CLASSTYPE_TEMPLATE_INFO (type)
1003 && context && CLASS_TYPE_P (context)
1004 && CLASSTYPE_TEMPLATE_INFO (context))
1005 {
1006 /* This is for an explicit specialization of member class
1007 template according to [temp.expl.spec/18]:
1008
1009 template <> template <class U> class C<int>::D;
1010
1011 The context `C<int>' must be an implicit instantiation.
1012 Otherwise this is just a member class template declared
1013 earlier like:
1014
1015 template <> class C<int> { template <class U> class D; };
1016 template <> template <class U> class C<int>::D;
1017
1018 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1019 while in the second case, `C<int>::D' is a primary template
1020 and `C<T>::D' may not exist. */
1021
1022 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1023 && !COMPLETE_TYPE_P (type))
1024 {
1025 tree t;
1026 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1027
1028 if (current_namespace
1029 != decl_namespace_context (tmpl))
1030 {
1031 permerror (input_location,
1032 "specializing %q#T in different namespace", type);
1033 permerror (DECL_SOURCE_LOCATION (tmpl),
1034 " from definition of %q#D", tmpl);
1035 }
1036
1037 /* Check for invalid specialization after instantiation:
1038
1039 template <> template <> class C<int>::D<int>;
1040 template <> template <class U> class C<int>::D; */
1041
1042 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1043 t; t = TREE_CHAIN (t))
1044 {
1045 tree inst = TREE_VALUE (t);
1046 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1047 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1048 {
1049 /* We already have a full specialization of this partial
1050 instantiation, or a full specialization has been
1051 looked up but not instantiated. Reassign it to the
1052 new member specialization template. */
1053 spec_entry elt;
1054 spec_entry *entry;
1055
1056 elt.tmpl = most_general_template (tmpl);
1057 elt.args = CLASSTYPE_TI_ARGS (inst);
1058 elt.spec = inst;
1059
1060 type_specializations->remove_elt (&elt);
1061
1062 elt.tmpl = tmpl;
1063 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1064
1065 spec_entry **slot
1066 = type_specializations->find_slot (&elt, INSERT);
1067 entry = ggc_alloc<spec_entry> ();
1068 *entry = elt;
1069 *slot = entry;
1070 }
1071 else
1072 /* But if we've had an implicit instantiation, that's a
1073 problem ([temp.expl.spec]/6). */
1074 error ("specialization %qT after instantiation %qT",
1075 type, inst);
1076 }
1077
1078 /* Mark TYPE as a specialization. And as a result, we only
1079 have one level of template argument for the innermost
1080 class template. */
1081 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1082 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1083 CLASSTYPE_TI_ARGS (type)
1084 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1085 }
1086 }
1087 else if (processing_specialization)
1088 {
1089 /* Someday C++0x may allow for enum template specialization. */
1090 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1091 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1092 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1093 "of %qD not allowed by ISO C++", type);
1094 else
1095 {
1096 error ("explicit specialization of non-template %qT", type);
1097 return error_mark_node;
1098 }
1099 }
1100
1101 return type;
1102 }
1103
1104 /* Returns nonzero if we can optimize the retrieval of specializations
1105 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1106 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1107
1108 static inline bool
1109 optimize_specialization_lookup_p (tree tmpl)
1110 {
1111 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1112 && DECL_CLASS_SCOPE_P (tmpl)
1113 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1114 parameter. */
1115 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1116 /* The optimized lookup depends on the fact that the
1117 template arguments for the member function template apply
1118 purely to the containing class, which is not true if the
1119 containing class is an explicit or partial
1120 specialization. */
1121 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1122 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1123 && !DECL_CONV_FN_P (tmpl)
1124 /* It is possible to have a template that is not a member
1125 template and is not a member of a template class:
1126
1127 template <typename T>
1128 struct S { friend A::f(); };
1129
1130 Here, the friend function is a template, but the context does
1131 not have template information. The optimized lookup relies
1132 on having ARGS be the template arguments for both the class
1133 and the function template. */
1134 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1135 }
1136
1137 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1138 gone through coerce_template_parms by now. */
1139
1140 static void
1141 verify_unstripped_args_1 (tree inner)
1142 {
1143 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1144 {
1145 tree arg = TREE_VEC_ELT (inner, i);
1146 if (TREE_CODE (arg) == TEMPLATE_DECL)
1147 /* OK */;
1148 else if (TYPE_P (arg))
1149 gcc_assert (strip_typedefs (arg, NULL) == arg);
1150 else if (ARGUMENT_PACK_P (arg))
1151 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg));
1152 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1153 /* Allow typedefs on the type of a non-type argument, since a
1154 parameter can have them. */;
1155 else
1156 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1157 }
1158 }
1159
1160 static void
1161 verify_unstripped_args (tree args)
1162 {
1163 ++processing_template_decl;
1164 if (!any_dependent_template_arguments_p (args))
1165 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args));
1166 --processing_template_decl;
1167 }
1168
1169 /* Retrieve the specialization (in the sense of [temp.spec] - a
1170 specialization is either an instantiation or an explicit
1171 specialization) of TMPL for the given template ARGS. If there is
1172 no such specialization, return NULL_TREE. The ARGS are a vector of
1173 arguments, or a vector of vectors of arguments, in the case of
1174 templates with more than one level of parameters.
1175
1176 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1177 then we search for a partial specialization matching ARGS. This
1178 parameter is ignored if TMPL is not a class template.
1179
1180 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1181 result is a NONTYPE_ARGUMENT_PACK. */
1182
1183 static tree
1184 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1185 {
1186 if (tmpl == NULL_TREE)
1187 return NULL_TREE;
1188
1189 if (args == error_mark_node)
1190 return NULL_TREE;
1191
1192 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1193 || TREE_CODE (tmpl) == FIELD_DECL);
1194
1195 /* There should be as many levels of arguments as there are
1196 levels of parameters. */
1197 gcc_assert (TMPL_ARGS_DEPTH (args)
1198 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1199 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1200 : template_class_depth (DECL_CONTEXT (tmpl))));
1201
1202 if (flag_checking)
1203 verify_unstripped_args (args);
1204
1205 /* Lambda functions in templates aren't instantiated normally, but through
1206 tsubst_lambda_expr. */
1207 if (lambda_fn_in_template_p (tmpl))
1208 return NULL_TREE;
1209
1210 if (optimize_specialization_lookup_p (tmpl))
1211 {
1212 /* The template arguments actually apply to the containing
1213 class. Find the class specialization with those
1214 arguments. */
1215 tree class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1216 tree class_specialization
1217 = retrieve_specialization (class_template, args, 0);
1218 if (!class_specialization)
1219 return NULL_TREE;
1220
1221 /* Find the instance of TMPL. */
1222 tree fns = get_class_binding (class_specialization, DECL_NAME (tmpl));
1223 for (ovl_iterator iter (fns); iter; ++iter)
1224 {
1225 tree fn = *iter;
1226 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1227 /* using-declarations can add base methods to the method vec,
1228 and we don't want those here. */
1229 && DECL_CONTEXT (fn) == class_specialization)
1230 return fn;
1231 }
1232 return NULL_TREE;
1233 }
1234 else
1235 {
1236 spec_entry *found;
1237 spec_entry elt;
1238 hash_table<spec_hasher> *specializations;
1239
1240 elt.tmpl = tmpl;
1241 elt.args = args;
1242 elt.spec = NULL_TREE;
1243
1244 if (DECL_CLASS_TEMPLATE_P (tmpl))
1245 specializations = type_specializations;
1246 else
1247 specializations = decl_specializations;
1248
1249 if (hash == 0)
1250 hash = spec_hasher::hash (&elt);
1251 found = specializations->find_with_hash (&elt, hash);
1252 if (found)
1253 return found->spec;
1254 }
1255
1256 return NULL_TREE;
1257 }
1258
1259 /* Like retrieve_specialization, but for local declarations. */
1260
1261 tree
1262 retrieve_local_specialization (tree tmpl)
1263 {
1264 if (local_specializations == NULL)
1265 return NULL_TREE;
1266
1267 tree *slot = local_specializations->get (tmpl);
1268 return slot ? *slot : NULL_TREE;
1269 }
1270
1271 /* Returns nonzero iff DECL is a specialization of TMPL. */
1272
1273 int
1274 is_specialization_of (tree decl, tree tmpl)
1275 {
1276 tree t;
1277
1278 if (TREE_CODE (decl) == FUNCTION_DECL)
1279 {
1280 for (t = decl;
1281 t != NULL_TREE;
1282 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1283 if (t == tmpl)
1284 return 1;
1285 }
1286 else
1287 {
1288 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1289
1290 for (t = TREE_TYPE (decl);
1291 t != NULL_TREE;
1292 t = CLASSTYPE_USE_TEMPLATE (t)
1293 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1294 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1295 return 1;
1296 }
1297
1298 return 0;
1299 }
1300
1301 /* Returns nonzero iff DECL is a specialization of friend declaration
1302 FRIEND_DECL according to [temp.friend]. */
1303
1304 bool
1305 is_specialization_of_friend (tree decl, tree friend_decl)
1306 {
1307 bool need_template = true;
1308 int template_depth;
1309
1310 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1311 || TREE_CODE (decl) == TYPE_DECL);
1312
1313 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1314 of a template class, we want to check if DECL is a specialization
1315 if this. */
1316 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1317 && DECL_TEMPLATE_INFO (friend_decl)
1318 && !DECL_USE_TEMPLATE (friend_decl))
1319 {
1320 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1321 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1322 need_template = false;
1323 }
1324 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1325 && !PRIMARY_TEMPLATE_P (friend_decl))
1326 need_template = false;
1327
1328 /* There is nothing to do if this is not a template friend. */
1329 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1330 return false;
1331
1332 if (is_specialization_of (decl, friend_decl))
1333 return true;
1334
1335 /* [temp.friend/6]
1336 A member of a class template may be declared to be a friend of a
1337 non-template class. In this case, the corresponding member of
1338 every specialization of the class template is a friend of the
1339 class granting friendship.
1340
1341 For example, given a template friend declaration
1342
1343 template <class T> friend void A<T>::f();
1344
1345 the member function below is considered a friend
1346
1347 template <> struct A<int> {
1348 void f();
1349 };
1350
1351 For this type of template friend, TEMPLATE_DEPTH below will be
1352 nonzero. To determine if DECL is a friend of FRIEND, we first
1353 check if the enclosing class is a specialization of another. */
1354
1355 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1356 if (template_depth
1357 && DECL_CLASS_SCOPE_P (decl)
1358 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1359 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1360 {
1361 /* Next, we check the members themselves. In order to handle
1362 a few tricky cases, such as when FRIEND_DECL's are
1363
1364 template <class T> friend void A<T>::g(T t);
1365 template <class T> template <T t> friend void A<T>::h();
1366
1367 and DECL's are
1368
1369 void A<int>::g(int);
1370 template <int> void A<int>::h();
1371
1372 we need to figure out ARGS, the template arguments from
1373 the context of DECL. This is required for template substitution
1374 of `T' in the function parameter of `g' and template parameter
1375 of `h' in the above examples. Here ARGS corresponds to `int'. */
1376
1377 tree context = DECL_CONTEXT (decl);
1378 tree args = NULL_TREE;
1379 int current_depth = 0;
1380
1381 while (current_depth < template_depth)
1382 {
1383 if (CLASSTYPE_TEMPLATE_INFO (context))
1384 {
1385 if (current_depth == 0)
1386 args = TYPE_TI_ARGS (context);
1387 else
1388 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1389 current_depth++;
1390 }
1391 context = TYPE_CONTEXT (context);
1392 }
1393
1394 if (TREE_CODE (decl) == FUNCTION_DECL)
1395 {
1396 bool is_template;
1397 tree friend_type;
1398 tree decl_type;
1399 tree friend_args_type;
1400 tree decl_args_type;
1401
1402 /* Make sure that both DECL and FRIEND_DECL are templates or
1403 non-templates. */
1404 is_template = DECL_TEMPLATE_INFO (decl)
1405 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1406 if (need_template ^ is_template)
1407 return false;
1408 else if (is_template)
1409 {
1410 /* If both are templates, check template parameter list. */
1411 tree friend_parms
1412 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1413 args, tf_none);
1414 if (!comp_template_parms
1415 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1416 friend_parms))
1417 return false;
1418
1419 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1420 }
1421 else
1422 decl_type = TREE_TYPE (decl);
1423
1424 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1425 tf_none, NULL_TREE);
1426 if (friend_type == error_mark_node)
1427 return false;
1428
1429 /* Check if return types match. */
1430 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1431 return false;
1432
1433 /* Check if function parameter types match, ignoring the
1434 `this' parameter. */
1435 friend_args_type = TYPE_ARG_TYPES (friend_type);
1436 decl_args_type = TYPE_ARG_TYPES (decl_type);
1437 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1438 friend_args_type = TREE_CHAIN (friend_args_type);
1439 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1440 decl_args_type = TREE_CHAIN (decl_args_type);
1441
1442 return compparms (decl_args_type, friend_args_type);
1443 }
1444 else
1445 {
1446 /* DECL is a TYPE_DECL */
1447 bool is_template;
1448 tree decl_type = TREE_TYPE (decl);
1449
1450 /* Make sure that both DECL and FRIEND_DECL are templates or
1451 non-templates. */
1452 is_template
1453 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1454 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1455
1456 if (need_template ^ is_template)
1457 return false;
1458 else if (is_template)
1459 {
1460 tree friend_parms;
1461 /* If both are templates, check the name of the two
1462 TEMPLATE_DECL's first because is_friend didn't. */
1463 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1464 != DECL_NAME (friend_decl))
1465 return false;
1466
1467 /* Now check template parameter list. */
1468 friend_parms
1469 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1470 args, tf_none);
1471 return comp_template_parms
1472 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1473 friend_parms);
1474 }
1475 else
1476 return (DECL_NAME (decl)
1477 == DECL_NAME (friend_decl));
1478 }
1479 }
1480 return false;
1481 }
1482
1483 /* Register the specialization SPEC as a specialization of TMPL with
1484 the indicated ARGS. IS_FRIEND indicates whether the specialization
1485 is actually just a friend declaration. ATTRLIST is the list of
1486 attributes that the specialization is declared with or NULL when
1487 it isn't. Returns SPEC, or an equivalent prior declaration, if
1488 available.
1489
1490 We also store instantiations of field packs in the hash table, even
1491 though they are not themselves templates, to make lookup easier. */
1492
1493 static tree
1494 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1495 hashval_t hash)
1496 {
1497 tree fn;
1498 spec_entry **slot = NULL;
1499 spec_entry elt;
1500
1501 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1502 || (TREE_CODE (tmpl) == FIELD_DECL
1503 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1504
1505 if (TREE_CODE (spec) == FUNCTION_DECL
1506 && uses_template_parms (DECL_TI_ARGS (spec)))
1507 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1508 register it; we want the corresponding TEMPLATE_DECL instead.
1509 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1510 the more obvious `uses_template_parms (spec)' to avoid problems
1511 with default function arguments. In particular, given
1512 something like this:
1513
1514 template <class T> void f(T t1, T t = T())
1515
1516 the default argument expression is not substituted for in an
1517 instantiation unless and until it is actually needed. */
1518 return spec;
1519
1520 if (optimize_specialization_lookup_p (tmpl))
1521 /* We don't put these specializations in the hash table, but we might
1522 want to give an error about a mismatch. */
1523 fn = retrieve_specialization (tmpl, args, 0);
1524 else
1525 {
1526 elt.tmpl = tmpl;
1527 elt.args = args;
1528 elt.spec = spec;
1529
1530 if (hash == 0)
1531 hash = spec_hasher::hash (&elt);
1532
1533 slot =
1534 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1535 if (*slot)
1536 fn = ((spec_entry *) *slot)->spec;
1537 else
1538 fn = NULL_TREE;
1539 }
1540
1541 /* We can sometimes try to re-register a specialization that we've
1542 already got. In particular, regenerate_decl_from_template calls
1543 duplicate_decls which will update the specialization list. But,
1544 we'll still get called again here anyhow. It's more convenient
1545 to simply allow this than to try to prevent it. */
1546 if (fn == spec)
1547 return spec;
1548 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1549 {
1550 if (DECL_TEMPLATE_INSTANTIATION (fn))
1551 {
1552 if (DECL_ODR_USED (fn)
1553 || DECL_EXPLICIT_INSTANTIATION (fn))
1554 {
1555 error ("specialization of %qD after instantiation",
1556 fn);
1557 return error_mark_node;
1558 }
1559 else
1560 {
1561 tree clone;
1562 /* This situation should occur only if the first
1563 specialization is an implicit instantiation, the
1564 second is an explicit specialization, and the
1565 implicit instantiation has not yet been used. That
1566 situation can occur if we have implicitly
1567 instantiated a member function and then specialized
1568 it later.
1569
1570 We can also wind up here if a friend declaration that
1571 looked like an instantiation turns out to be a
1572 specialization:
1573
1574 template <class T> void foo(T);
1575 class S { friend void foo<>(int) };
1576 template <> void foo(int);
1577
1578 We transform the existing DECL in place so that any
1579 pointers to it become pointers to the updated
1580 declaration.
1581
1582 If there was a definition for the template, but not
1583 for the specialization, we want this to look as if
1584 there were no definition, and vice versa. */
1585 DECL_INITIAL (fn) = NULL_TREE;
1586 duplicate_decls (spec, fn, is_friend);
1587 /* The call to duplicate_decls will have applied
1588 [temp.expl.spec]:
1589
1590 An explicit specialization of a function template
1591 is inline only if it is explicitly declared to be,
1592 and independently of whether its function template
1593 is.
1594
1595 to the primary function; now copy the inline bits to
1596 the various clones. */
1597 FOR_EACH_CLONE (clone, fn)
1598 {
1599 DECL_DECLARED_INLINE_P (clone)
1600 = DECL_DECLARED_INLINE_P (fn);
1601 DECL_SOURCE_LOCATION (clone)
1602 = DECL_SOURCE_LOCATION (fn);
1603 DECL_DELETED_FN (clone)
1604 = DECL_DELETED_FN (fn);
1605 }
1606 check_specialization_namespace (tmpl);
1607
1608 return fn;
1609 }
1610 }
1611 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1612 {
1613 tree dd = duplicate_decls (spec, fn, is_friend);
1614 if (dd == error_mark_node)
1615 /* We've already complained in duplicate_decls. */
1616 return error_mark_node;
1617
1618 if (dd == NULL_TREE && DECL_INITIAL (spec))
1619 /* Dup decl failed, but this is a new definition. Set the
1620 line number so any errors match this new
1621 definition. */
1622 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1623
1624 return fn;
1625 }
1626 }
1627 else if (fn)
1628 return duplicate_decls (spec, fn, is_friend);
1629
1630 /* A specialization must be declared in the same namespace as the
1631 template it is specializing. */
1632 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1633 && !check_specialization_namespace (tmpl))
1634 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1635
1636 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1637 {
1638 spec_entry *entry = ggc_alloc<spec_entry> ();
1639 gcc_assert (tmpl && args && spec);
1640 *entry = elt;
1641 *slot = entry;
1642 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1643 && PRIMARY_TEMPLATE_P (tmpl)
1644 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1645 || variable_template_p (tmpl))
1646 /* If TMPL is a forward declaration of a template function, keep a list
1647 of all specializations in case we need to reassign them to a friend
1648 template later in tsubst_friend_function.
1649
1650 Also keep a list of all variable template instantiations so that
1651 process_partial_specialization can check whether a later partial
1652 specialization would have used it. */
1653 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1654 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1655 }
1656
1657 return spec;
1658 }
1659
1660 /* Returns true iff two spec_entry nodes are equivalent. */
1661
1662 int comparing_specializations;
1663
1664 bool
1665 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1666 {
1667 int equal;
1668
1669 ++comparing_specializations;
1670 equal = (e1->tmpl == e2->tmpl
1671 && comp_template_args (e1->args, e2->args));
1672 if (equal && flag_concepts
1673 /* tmpl could be a FIELD_DECL for a capture pack. */
1674 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1675 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1676 && uses_template_parms (e1->args))
1677 {
1678 /* Partial specializations of a variable template can be distinguished by
1679 constraints. */
1680 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1681 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1682 equal = equivalent_constraints (c1, c2);
1683 }
1684 --comparing_specializations;
1685
1686 return equal;
1687 }
1688
1689 /* Returns a hash for a template TMPL and template arguments ARGS. */
1690
1691 static hashval_t
1692 hash_tmpl_and_args (tree tmpl, tree args)
1693 {
1694 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1695 return iterative_hash_template_arg (args, val);
1696 }
1697
1698 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1699 ignoring SPEC. */
1700
1701 hashval_t
1702 spec_hasher::hash (spec_entry *e)
1703 {
1704 return hash_tmpl_and_args (e->tmpl, e->args);
1705 }
1706
1707 /* Recursively calculate a hash value for a template argument ARG, for use
1708 in the hash tables of template specializations. */
1709
1710 hashval_t
1711 iterative_hash_template_arg (tree arg, hashval_t val)
1712 {
1713 unsigned HOST_WIDE_INT i;
1714 enum tree_code code;
1715 char tclass;
1716
1717 if (arg == NULL_TREE)
1718 return iterative_hash_object (arg, val);
1719
1720 if (!TYPE_P (arg))
1721 STRIP_NOPS (arg);
1722
1723 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1724 gcc_unreachable ();
1725
1726 code = TREE_CODE (arg);
1727 tclass = TREE_CODE_CLASS (code);
1728
1729 val = iterative_hash_object (code, val);
1730
1731 switch (code)
1732 {
1733 case ERROR_MARK:
1734 return val;
1735
1736 case IDENTIFIER_NODE:
1737 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1738
1739 case TREE_VEC:
1740 {
1741 int i, len = TREE_VEC_LENGTH (arg);
1742 for (i = 0; i < len; ++i)
1743 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1744 return val;
1745 }
1746
1747 case TYPE_PACK_EXPANSION:
1748 case EXPR_PACK_EXPANSION:
1749 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1750 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1751
1752 case TYPE_ARGUMENT_PACK:
1753 case NONTYPE_ARGUMENT_PACK:
1754 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1755
1756 case TREE_LIST:
1757 for (; arg; arg = TREE_CHAIN (arg))
1758 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1759 return val;
1760
1761 case OVERLOAD:
1762 for (lkp_iterator iter (arg); iter; ++iter)
1763 val = iterative_hash_template_arg (*iter, val);
1764 return val;
1765
1766 case CONSTRUCTOR:
1767 {
1768 tree field, value;
1769 iterative_hash_template_arg (TREE_TYPE (arg), val);
1770 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1771 {
1772 val = iterative_hash_template_arg (field, val);
1773 val = iterative_hash_template_arg (value, val);
1774 }
1775 return val;
1776 }
1777
1778 case PARM_DECL:
1779 if (!DECL_ARTIFICIAL (arg))
1780 {
1781 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1782 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1783 }
1784 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1785
1786 case TARGET_EXPR:
1787 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1788
1789 case PTRMEM_CST:
1790 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1791 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1792
1793 case TEMPLATE_PARM_INDEX:
1794 val = iterative_hash_template_arg
1795 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1796 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1797 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1798
1799 case TRAIT_EXPR:
1800 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1801 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1802 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1803
1804 case BASELINK:
1805 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1806 val);
1807 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1808 val);
1809
1810 case MODOP_EXPR:
1811 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1812 code = TREE_CODE (TREE_OPERAND (arg, 1));
1813 val = iterative_hash_object (code, val);
1814 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1815
1816 case LAMBDA_EXPR:
1817 /* A lambda can't appear in a template arg, but don't crash on
1818 erroneous input. */
1819 gcc_assert (seen_error ());
1820 return val;
1821
1822 case CAST_EXPR:
1823 case IMPLICIT_CONV_EXPR:
1824 case STATIC_CAST_EXPR:
1825 case REINTERPRET_CAST_EXPR:
1826 case CONST_CAST_EXPR:
1827 case DYNAMIC_CAST_EXPR:
1828 case NEW_EXPR:
1829 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1830 /* Now hash operands as usual. */
1831 break;
1832
1833 default:
1834 break;
1835 }
1836
1837 switch (tclass)
1838 {
1839 case tcc_type:
1840 if (alias_template_specialization_p (arg))
1841 {
1842 // We want an alias specialization that survived strip_typedefs
1843 // to hash differently from its TYPE_CANONICAL, to avoid hash
1844 // collisions that compare as different in template_args_equal.
1845 // These could be dependent specializations that strip_typedefs
1846 // left alone, or untouched specializations because
1847 // coerce_template_parms returns the unconverted template
1848 // arguments if it sees incomplete argument packs.
1849 tree ti = TYPE_ALIAS_TEMPLATE_INFO (arg);
1850 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1851 }
1852 if (TYPE_CANONICAL (arg))
1853 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1854 val);
1855 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1856 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1857 /* Otherwise just compare the types during lookup. */
1858 return val;
1859
1860 case tcc_declaration:
1861 case tcc_constant:
1862 return iterative_hash_expr (arg, val);
1863
1864 default:
1865 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1866 {
1867 unsigned n = cp_tree_operand_length (arg);
1868 for (i = 0; i < n; ++i)
1869 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1870 return val;
1871 }
1872 }
1873 gcc_unreachable ();
1874 return 0;
1875 }
1876
1877 /* Unregister the specialization SPEC as a specialization of TMPL.
1878 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1879 if the SPEC was listed as a specialization of TMPL.
1880
1881 Note that SPEC has been ggc_freed, so we can't look inside it. */
1882
1883 bool
1884 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1885 {
1886 spec_entry *entry;
1887 spec_entry elt;
1888
1889 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1890 elt.args = TI_ARGS (tinfo);
1891 elt.spec = NULL_TREE;
1892
1893 entry = decl_specializations->find (&elt);
1894 if (entry != NULL)
1895 {
1896 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1897 gcc_assert (new_spec != NULL_TREE);
1898 entry->spec = new_spec;
1899 return 1;
1900 }
1901
1902 return 0;
1903 }
1904
1905 /* Like register_specialization, but for local declarations. We are
1906 registering SPEC, an instantiation of TMPL. */
1907
1908 void
1909 register_local_specialization (tree spec, tree tmpl)
1910 {
1911 gcc_assert (tmpl != spec);
1912 local_specializations->put (tmpl, spec);
1913 }
1914
1915 /* TYPE is a class type. Returns true if TYPE is an explicitly
1916 specialized class. */
1917
1918 bool
1919 explicit_class_specialization_p (tree type)
1920 {
1921 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1922 return false;
1923 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1924 }
1925
1926 /* Print the list of functions at FNS, going through all the overloads
1927 for each element of the list. Alternatively, FNS can not be a
1928 TREE_LIST, in which case it will be printed together with all the
1929 overloads.
1930
1931 MORE and *STR should respectively be FALSE and NULL when the function
1932 is called from the outside. They are used internally on recursive
1933 calls. print_candidates manages the two parameters and leaves NULL
1934 in *STR when it ends. */
1935
1936 static void
1937 print_candidates_1 (tree fns, char **str, bool more = false)
1938 {
1939 if (TREE_CODE (fns) == TREE_LIST)
1940 for (; fns; fns = TREE_CHAIN (fns))
1941 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
1942 else
1943 for (lkp_iterator iter (fns); iter;)
1944 {
1945 tree cand = *iter;
1946 ++iter;
1947
1948 const char *pfx = *str;
1949 if (!pfx)
1950 {
1951 if (more || iter)
1952 pfx = _("candidates are:");
1953 else
1954 pfx = _("candidate is:");
1955 *str = get_spaces (pfx);
1956 }
1957 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
1958 }
1959 }
1960
1961 /* Print the list of candidate FNS in an error message. FNS can also
1962 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1963
1964 void
1965 print_candidates (tree fns)
1966 {
1967 char *str = NULL;
1968 print_candidates_1 (fns, &str);
1969 free (str);
1970 }
1971
1972 /* Get a (possibly) constrained template declaration for the
1973 purpose of ordering candidates. */
1974 static tree
1975 get_template_for_ordering (tree list)
1976 {
1977 gcc_assert (TREE_CODE (list) == TREE_LIST);
1978 tree f = TREE_VALUE (list);
1979 if (tree ti = DECL_TEMPLATE_INFO (f))
1980 return TI_TEMPLATE (ti);
1981 return f;
1982 }
1983
1984 /* Among candidates having the same signature, return the
1985 most constrained or NULL_TREE if there is no best candidate.
1986 If the signatures of candidates vary (e.g., template
1987 specialization vs. member function), then there can be no
1988 most constrained.
1989
1990 Note that we don't compare constraints on the functions
1991 themselves, but rather those of their templates. */
1992 static tree
1993 most_constrained_function (tree candidates)
1994 {
1995 // Try to find the best candidate in a first pass.
1996 tree champ = candidates;
1997 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
1998 {
1999 int winner = more_constrained (get_template_for_ordering (champ),
2000 get_template_for_ordering (c));
2001 if (winner == -1)
2002 champ = c; // The candidate is more constrained
2003 else if (winner == 0)
2004 return NULL_TREE; // Neither is more constrained
2005 }
2006
2007 // Verify that the champ is better than previous candidates.
2008 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2009 if (!more_constrained (get_template_for_ordering (champ),
2010 get_template_for_ordering (c)))
2011 return NULL_TREE;
2012 }
2013
2014 return champ;
2015 }
2016
2017
2018 /* Returns the template (one of the functions given by TEMPLATE_ID)
2019 which can be specialized to match the indicated DECL with the
2020 explicit template args given in TEMPLATE_ID. The DECL may be
2021 NULL_TREE if none is available. In that case, the functions in
2022 TEMPLATE_ID are non-members.
2023
2024 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2025 specialization of a member template.
2026
2027 The TEMPLATE_COUNT is the number of references to qualifying
2028 template classes that appeared in the name of the function. See
2029 check_explicit_specialization for a more accurate description.
2030
2031 TSK indicates what kind of template declaration (if any) is being
2032 declared. TSK_TEMPLATE indicates that the declaration given by
2033 DECL, though a FUNCTION_DECL, has template parameters, and is
2034 therefore a template function.
2035
2036 The template args (those explicitly specified and those deduced)
2037 are output in a newly created vector *TARGS_OUT.
2038
2039 If it is impossible to determine the result, an error message is
2040 issued. The error_mark_node is returned to indicate failure. */
2041
2042 static tree
2043 determine_specialization (tree template_id,
2044 tree decl,
2045 tree* targs_out,
2046 int need_member_template,
2047 int template_count,
2048 tmpl_spec_kind tsk)
2049 {
2050 tree fns;
2051 tree targs;
2052 tree explicit_targs;
2053 tree candidates = NULL_TREE;
2054
2055 /* A TREE_LIST of templates of which DECL may be a specialization.
2056 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2057 corresponding TREE_PURPOSE is the set of template arguments that,
2058 when used to instantiate the template, would produce a function
2059 with the signature of DECL. */
2060 tree templates = NULL_TREE;
2061 int header_count;
2062 cp_binding_level *b;
2063
2064 *targs_out = NULL_TREE;
2065
2066 if (template_id == error_mark_node || decl == error_mark_node)
2067 return error_mark_node;
2068
2069 /* We shouldn't be specializing a member template of an
2070 unspecialized class template; we already gave an error in
2071 check_specialization_scope, now avoid crashing. */
2072 if (!VAR_P (decl)
2073 && template_count && DECL_CLASS_SCOPE_P (decl)
2074 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2075 {
2076 gcc_assert (errorcount);
2077 return error_mark_node;
2078 }
2079
2080 fns = TREE_OPERAND (template_id, 0);
2081 explicit_targs = TREE_OPERAND (template_id, 1);
2082
2083 if (fns == error_mark_node)
2084 return error_mark_node;
2085
2086 /* Check for baselinks. */
2087 if (BASELINK_P (fns))
2088 fns = BASELINK_FUNCTIONS (fns);
2089
2090 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2091 {
2092 error ("%qD is not a function template", fns);
2093 return error_mark_node;
2094 }
2095 else if (VAR_P (decl) && !variable_template_p (fns))
2096 {
2097 error ("%qD is not a variable template", fns);
2098 return error_mark_node;
2099 }
2100
2101 /* Count the number of template headers specified for this
2102 specialization. */
2103 header_count = 0;
2104 for (b = current_binding_level;
2105 b->kind == sk_template_parms;
2106 b = b->level_chain)
2107 ++header_count;
2108
2109 tree orig_fns = fns;
2110
2111 if (variable_template_p (fns))
2112 {
2113 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2114 targs = coerce_template_parms (parms, explicit_targs, fns,
2115 tf_warning_or_error,
2116 /*req_all*/true, /*use_defarg*/true);
2117 if (targs != error_mark_node)
2118 templates = tree_cons (targs, fns, templates);
2119 }
2120 else for (lkp_iterator iter (fns); iter; ++iter)
2121 {
2122 tree fn = *iter;
2123
2124 if (TREE_CODE (fn) == TEMPLATE_DECL)
2125 {
2126 tree decl_arg_types;
2127 tree fn_arg_types;
2128 tree insttype;
2129
2130 /* In case of explicit specialization, we need to check if
2131 the number of template headers appearing in the specialization
2132 is correct. This is usually done in check_explicit_specialization,
2133 but the check done there cannot be exhaustive when specializing
2134 member functions. Consider the following code:
2135
2136 template <> void A<int>::f(int);
2137 template <> template <> void A<int>::f(int);
2138
2139 Assuming that A<int> is not itself an explicit specialization
2140 already, the first line specializes "f" which is a non-template
2141 member function, whilst the second line specializes "f" which
2142 is a template member function. So both lines are syntactically
2143 correct, and check_explicit_specialization does not reject
2144 them.
2145
2146 Here, we can do better, as we are matching the specialization
2147 against the declarations. We count the number of template
2148 headers, and we check if they match TEMPLATE_COUNT + 1
2149 (TEMPLATE_COUNT is the number of qualifying template classes,
2150 plus there must be another header for the member template
2151 itself).
2152
2153 Notice that if header_count is zero, this is not a
2154 specialization but rather a template instantiation, so there
2155 is no check we can perform here. */
2156 if (header_count && header_count != template_count + 1)
2157 continue;
2158
2159 /* Check that the number of template arguments at the
2160 innermost level for DECL is the same as for FN. */
2161 if (current_binding_level->kind == sk_template_parms
2162 && !current_binding_level->explicit_spec_p
2163 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2164 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2165 (current_template_parms))))
2166 continue;
2167
2168 /* DECL might be a specialization of FN. */
2169 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2170 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2171
2172 /* For a non-static member function, we need to make sure
2173 that the const qualification is the same. Since
2174 get_bindings does not try to merge the "this" parameter,
2175 we must do the comparison explicitly. */
2176 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2177 {
2178 if (!same_type_p (TREE_VALUE (fn_arg_types),
2179 TREE_VALUE (decl_arg_types)))
2180 continue;
2181
2182 /* And the ref-qualification. */
2183 if (type_memfn_rqual (TREE_TYPE (decl))
2184 != type_memfn_rqual (TREE_TYPE (fn)))
2185 continue;
2186 }
2187
2188 /* Skip the "this" parameter and, for constructors of
2189 classes with virtual bases, the VTT parameter. A
2190 full specialization of a constructor will have a VTT
2191 parameter, but a template never will. */
2192 decl_arg_types
2193 = skip_artificial_parms_for (decl, decl_arg_types);
2194 fn_arg_types
2195 = skip_artificial_parms_for (fn, fn_arg_types);
2196
2197 /* Function templates cannot be specializations; there are
2198 no partial specializations of functions. Therefore, if
2199 the type of DECL does not match FN, there is no
2200 match.
2201
2202 Note that it should never be the case that we have both
2203 candidates added here, and for regular member functions
2204 below. */
2205 if (tsk == tsk_template)
2206 {
2207 if (compparms (fn_arg_types, decl_arg_types))
2208 candidates = tree_cons (NULL_TREE, fn, candidates);
2209 continue;
2210 }
2211
2212 /* See whether this function might be a specialization of this
2213 template. Suppress access control because we might be trying
2214 to make this specialization a friend, and we have already done
2215 access control for the declaration of the specialization. */
2216 push_deferring_access_checks (dk_no_check);
2217 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2218 pop_deferring_access_checks ();
2219
2220 if (!targs)
2221 /* We cannot deduce template arguments that when used to
2222 specialize TMPL will produce DECL. */
2223 continue;
2224
2225 if (uses_template_parms (targs))
2226 /* We deduced something involving 'auto', which isn't a valid
2227 template argument. */
2228 continue;
2229
2230 /* Remove, from the set of candidates, all those functions
2231 whose constraints are not satisfied. */
2232 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2233 continue;
2234
2235 // Then, try to form the new function type.
2236 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2237 if (insttype == error_mark_node)
2238 continue;
2239 fn_arg_types
2240 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2241 if (!compparms (fn_arg_types, decl_arg_types))
2242 continue;
2243
2244 /* Save this template, and the arguments deduced. */
2245 templates = tree_cons (targs, fn, templates);
2246 }
2247 else if (need_member_template)
2248 /* FN is an ordinary member function, and we need a
2249 specialization of a member template. */
2250 ;
2251 else if (TREE_CODE (fn) != FUNCTION_DECL)
2252 /* We can get IDENTIFIER_NODEs here in certain erroneous
2253 cases. */
2254 ;
2255 else if (!DECL_FUNCTION_MEMBER_P (fn))
2256 /* This is just an ordinary non-member function. Nothing can
2257 be a specialization of that. */
2258 ;
2259 else if (DECL_ARTIFICIAL (fn))
2260 /* Cannot specialize functions that are created implicitly. */
2261 ;
2262 else
2263 {
2264 tree decl_arg_types;
2265
2266 /* This is an ordinary member function. However, since
2267 we're here, we can assume its enclosing class is a
2268 template class. For example,
2269
2270 template <typename T> struct S { void f(); };
2271 template <> void S<int>::f() {}
2272
2273 Here, S<int>::f is a non-template, but S<int> is a
2274 template class. If FN has the same type as DECL, we
2275 might be in business. */
2276
2277 if (!DECL_TEMPLATE_INFO (fn))
2278 /* Its enclosing class is an explicit specialization
2279 of a template class. This is not a candidate. */
2280 continue;
2281
2282 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2283 TREE_TYPE (TREE_TYPE (fn))))
2284 /* The return types differ. */
2285 continue;
2286
2287 /* Adjust the type of DECL in case FN is a static member. */
2288 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2289 if (DECL_STATIC_FUNCTION_P (fn)
2290 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2291 decl_arg_types = TREE_CHAIN (decl_arg_types);
2292
2293 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2294 decl_arg_types))
2295 continue;
2296
2297 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2298 && (type_memfn_rqual (TREE_TYPE (decl))
2299 != type_memfn_rqual (TREE_TYPE (fn))))
2300 continue;
2301
2302 // If the deduced arguments do not satisfy the constraints,
2303 // this is not a candidate.
2304 if (flag_concepts && !constraints_satisfied_p (fn))
2305 continue;
2306
2307 // Add the candidate.
2308 candidates = tree_cons (NULL_TREE, fn, candidates);
2309 }
2310 }
2311
2312 if (templates && TREE_CHAIN (templates))
2313 {
2314 /* We have:
2315
2316 [temp.expl.spec]
2317
2318 It is possible for a specialization with a given function
2319 signature to be instantiated from more than one function
2320 template. In such cases, explicit specification of the
2321 template arguments must be used to uniquely identify the
2322 function template specialization being specialized.
2323
2324 Note that here, there's no suggestion that we're supposed to
2325 determine which of the candidate templates is most
2326 specialized. However, we, also have:
2327
2328 [temp.func.order]
2329
2330 Partial ordering of overloaded function template
2331 declarations is used in the following contexts to select
2332 the function template to which a function template
2333 specialization refers:
2334
2335 -- when an explicit specialization refers to a function
2336 template.
2337
2338 So, we do use the partial ordering rules, at least for now.
2339 This extension can only serve to make invalid programs valid,
2340 so it's safe. And, there is strong anecdotal evidence that
2341 the committee intended the partial ordering rules to apply;
2342 the EDG front end has that behavior, and John Spicer claims
2343 that the committee simply forgot to delete the wording in
2344 [temp.expl.spec]. */
2345 tree tmpl = most_specialized_instantiation (templates);
2346 if (tmpl != error_mark_node)
2347 {
2348 templates = tmpl;
2349 TREE_CHAIN (templates) = NULL_TREE;
2350 }
2351 }
2352
2353 // Concepts allows multiple declarations of member functions
2354 // with the same signature. Like above, we need to rely on
2355 // on the partial ordering of those candidates to determine which
2356 // is the best.
2357 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2358 {
2359 if (tree cand = most_constrained_function (candidates))
2360 {
2361 candidates = cand;
2362 TREE_CHAIN (cand) = NULL_TREE;
2363 }
2364 }
2365
2366 if (templates == NULL_TREE && candidates == NULL_TREE)
2367 {
2368 error ("template-id %qD for %q+D does not match any template "
2369 "declaration", template_id, decl);
2370 if (header_count && header_count != template_count + 1)
2371 inform (input_location, "saw %d %<template<>%>, need %d for "
2372 "specializing a member function template",
2373 header_count, template_count + 1);
2374 else
2375 print_candidates (orig_fns);
2376 return error_mark_node;
2377 }
2378 else if ((templates && TREE_CHAIN (templates))
2379 || (candidates && TREE_CHAIN (candidates))
2380 || (templates && candidates))
2381 {
2382 error ("ambiguous template specialization %qD for %q+D",
2383 template_id, decl);
2384 candidates = chainon (candidates, templates);
2385 print_candidates (candidates);
2386 return error_mark_node;
2387 }
2388
2389 /* We have one, and exactly one, match. */
2390 if (candidates)
2391 {
2392 tree fn = TREE_VALUE (candidates);
2393 *targs_out = copy_node (DECL_TI_ARGS (fn));
2394
2395 // Propagate the candidate's constraints to the declaration.
2396 set_constraints (decl, get_constraints (fn));
2397
2398 /* DECL is a re-declaration or partial instantiation of a template
2399 function. */
2400 if (TREE_CODE (fn) == TEMPLATE_DECL)
2401 return fn;
2402 /* It was a specialization of an ordinary member function in a
2403 template class. */
2404 return DECL_TI_TEMPLATE (fn);
2405 }
2406
2407 /* It was a specialization of a template. */
2408 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2409 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2410 {
2411 *targs_out = copy_node (targs);
2412 SET_TMPL_ARGS_LEVEL (*targs_out,
2413 TMPL_ARGS_DEPTH (*targs_out),
2414 TREE_PURPOSE (templates));
2415 }
2416 else
2417 *targs_out = TREE_PURPOSE (templates);
2418 return TREE_VALUE (templates);
2419 }
2420
2421 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2422 but with the default argument values filled in from those in the
2423 TMPL_TYPES. */
2424
2425 static tree
2426 copy_default_args_to_explicit_spec_1 (tree spec_types,
2427 tree tmpl_types)
2428 {
2429 tree new_spec_types;
2430
2431 if (!spec_types)
2432 return NULL_TREE;
2433
2434 if (spec_types == void_list_node)
2435 return void_list_node;
2436
2437 /* Substitute into the rest of the list. */
2438 new_spec_types =
2439 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2440 TREE_CHAIN (tmpl_types));
2441
2442 /* Add the default argument for this parameter. */
2443 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2444 TREE_VALUE (spec_types),
2445 new_spec_types);
2446 }
2447
2448 /* DECL is an explicit specialization. Replicate default arguments
2449 from the template it specializes. (That way, code like:
2450
2451 template <class T> void f(T = 3);
2452 template <> void f(double);
2453 void g () { f (); }
2454
2455 works, as required.) An alternative approach would be to look up
2456 the correct default arguments at the call-site, but this approach
2457 is consistent with how implicit instantiations are handled. */
2458
2459 static void
2460 copy_default_args_to_explicit_spec (tree decl)
2461 {
2462 tree tmpl;
2463 tree spec_types;
2464 tree tmpl_types;
2465 tree new_spec_types;
2466 tree old_type;
2467 tree new_type;
2468 tree t;
2469 tree object_type = NULL_TREE;
2470 tree in_charge = NULL_TREE;
2471 tree vtt = NULL_TREE;
2472
2473 /* See if there's anything we need to do. */
2474 tmpl = DECL_TI_TEMPLATE (decl);
2475 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2476 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2477 if (TREE_PURPOSE (t))
2478 break;
2479 if (!t)
2480 return;
2481
2482 old_type = TREE_TYPE (decl);
2483 spec_types = TYPE_ARG_TYPES (old_type);
2484
2485 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2486 {
2487 /* Remove the this pointer, but remember the object's type for
2488 CV quals. */
2489 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2490 spec_types = TREE_CHAIN (spec_types);
2491 tmpl_types = TREE_CHAIN (tmpl_types);
2492
2493 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2494 {
2495 /* DECL may contain more parameters than TMPL due to the extra
2496 in-charge parameter in constructors and destructors. */
2497 in_charge = spec_types;
2498 spec_types = TREE_CHAIN (spec_types);
2499 }
2500 if (DECL_HAS_VTT_PARM_P (decl))
2501 {
2502 vtt = spec_types;
2503 spec_types = TREE_CHAIN (spec_types);
2504 }
2505 }
2506
2507 /* Compute the merged default arguments. */
2508 new_spec_types =
2509 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2510
2511 /* Compute the new FUNCTION_TYPE. */
2512 if (object_type)
2513 {
2514 if (vtt)
2515 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2516 TREE_VALUE (vtt),
2517 new_spec_types);
2518
2519 if (in_charge)
2520 /* Put the in-charge parameter back. */
2521 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2522 TREE_VALUE (in_charge),
2523 new_spec_types);
2524
2525 new_type = build_method_type_directly (object_type,
2526 TREE_TYPE (old_type),
2527 new_spec_types);
2528 }
2529 else
2530 new_type = build_function_type (TREE_TYPE (old_type),
2531 new_spec_types);
2532 new_type = cp_build_type_attribute_variant (new_type,
2533 TYPE_ATTRIBUTES (old_type));
2534 new_type = cxx_copy_lang_qualifiers (new_type, old_type);
2535
2536 TREE_TYPE (decl) = new_type;
2537 }
2538
2539 /* Return the number of template headers we expect to see for a definition
2540 or specialization of CTYPE or one of its non-template members. */
2541
2542 int
2543 num_template_headers_for_class (tree ctype)
2544 {
2545 int num_templates = 0;
2546
2547 while (ctype && CLASS_TYPE_P (ctype))
2548 {
2549 /* You're supposed to have one `template <...>' for every
2550 template class, but you don't need one for a full
2551 specialization. For example:
2552
2553 template <class T> struct S{};
2554 template <> struct S<int> { void f(); };
2555 void S<int>::f () {}
2556
2557 is correct; there shouldn't be a `template <>' for the
2558 definition of `S<int>::f'. */
2559 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2560 /* If CTYPE does not have template information of any
2561 kind, then it is not a template, nor is it nested
2562 within a template. */
2563 break;
2564 if (explicit_class_specialization_p (ctype))
2565 break;
2566 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2567 ++num_templates;
2568
2569 ctype = TYPE_CONTEXT (ctype);
2570 }
2571
2572 return num_templates;
2573 }
2574
2575 /* Do a simple sanity check on the template headers that precede the
2576 variable declaration DECL. */
2577
2578 void
2579 check_template_variable (tree decl)
2580 {
2581 tree ctx = CP_DECL_CONTEXT (decl);
2582 int wanted = num_template_headers_for_class (ctx);
2583 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2584 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2585 {
2586 if (cxx_dialect < cxx14)
2587 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2588 "variable templates only available with "
2589 "-std=c++14 or -std=gnu++14");
2590
2591 // Namespace-scope variable templates should have a template header.
2592 ++wanted;
2593 }
2594 if (template_header_count > wanted)
2595 {
2596 auto_diagnostic_group d;
2597 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2598 "too many template headers for %qD "
2599 "(should be %d)",
2600 decl, wanted);
2601 if (warned && CLASS_TYPE_P (ctx)
2602 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2603 inform (DECL_SOURCE_LOCATION (decl),
2604 "members of an explicitly specialized class are defined "
2605 "without a template header");
2606 }
2607 }
2608
2609 /* An explicit specialization whose declarator-id or class-head-name is not
2610 qualified shall be declared in the nearest enclosing namespace of the
2611 template, or, if the namespace is inline (7.3.1), any namespace from its
2612 enclosing namespace set.
2613
2614 If the name declared in the explicit instantiation is an unqualified name,
2615 the explicit instantiation shall appear in the namespace where its template
2616 is declared or, if that namespace is inline (7.3.1), any namespace from its
2617 enclosing namespace set. */
2618
2619 void
2620 check_unqualified_spec_or_inst (tree t, location_t loc)
2621 {
2622 tree tmpl = most_general_template (t);
2623 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2624 && !is_nested_namespace (current_namespace,
2625 CP_DECL_CONTEXT (tmpl), true))
2626 {
2627 if (processing_specialization)
2628 permerror (loc, "explicit specialization of %qD outside its "
2629 "namespace must use a nested-name-specifier", tmpl);
2630 else if (processing_explicit_instantiation
2631 && cxx_dialect >= cxx11)
2632 /* This was allowed in C++98, so only pedwarn. */
2633 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2634 "outside its namespace must use a nested-name-"
2635 "specifier", tmpl);
2636 }
2637 }
2638
2639 /* Warn for a template specialization SPEC that is missing some of a set
2640 of function or type attributes that the template TEMPL is declared with.
2641 ATTRLIST is a list of additional attributes that SPEC should be taken
2642 to ultimately be declared with. */
2643
2644 static void
2645 warn_spec_missing_attributes (tree tmpl, tree spec, tree attrlist)
2646 {
2647 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
2648 tmpl = DECL_TEMPLATE_RESULT (tmpl);
2649
2650 if (TREE_CODE (tmpl) != FUNCTION_DECL)
2651 return;
2652
2653 /* Avoid warning if either declaration or its type is deprecated. */
2654 if (TREE_DEPRECATED (tmpl)
2655 || TREE_DEPRECATED (spec))
2656 return;
2657
2658 tree tmpl_type = TREE_TYPE (tmpl);
2659 tree spec_type = TREE_TYPE (spec);
2660
2661 if (TREE_DEPRECATED (tmpl_type)
2662 || TREE_DEPRECATED (spec_type)
2663 || TREE_DEPRECATED (TREE_TYPE (tmpl_type))
2664 || TREE_DEPRECATED (TREE_TYPE (spec_type)))
2665 return;
2666
2667 tree tmpl_attrs[] = { DECL_ATTRIBUTES (tmpl), TYPE_ATTRIBUTES (tmpl_type) };
2668 tree spec_attrs[] = { DECL_ATTRIBUTES (spec), TYPE_ATTRIBUTES (spec_type) };
2669
2670 if (!spec_attrs[0])
2671 spec_attrs[0] = attrlist;
2672 else if (!spec_attrs[1])
2673 spec_attrs[1] = attrlist;
2674
2675 /* Avoid warning if the primary has no attributes. */
2676 if (!tmpl_attrs[0] && !tmpl_attrs[1])
2677 return;
2678
2679 /* Avoid warning if either declaration contains an attribute on
2680 the white list below. */
2681 const char* const whitelist[] = {
2682 "error", "warning"
2683 };
2684
2685 for (unsigned i = 0; i != 2; ++i)
2686 for (unsigned j = 0; j != sizeof whitelist / sizeof *whitelist; ++j)
2687 if (lookup_attribute (whitelist[j], tmpl_attrs[i])
2688 || lookup_attribute (whitelist[j], spec_attrs[i]))
2689 return;
2690
2691 /* Avoid warning if the difference between the primary and
2692 the specialization is not in one of the attributes below. */
2693 const char* const blacklist[] = {
2694 "alloc_align", "alloc_size", "assume_aligned", "format",
2695 "format_arg", "malloc", "nonnull"
2696 };
2697
2698 /* Put together a list of the black listed attributes that the primary
2699 template is declared with that the specialization is not, in case
2700 it's not apparent from the most recent declaration of the primary. */
2701 unsigned nattrs = 0;
2702 pretty_printer str;
2703
2704 for (unsigned i = 0; i != sizeof blacklist / sizeof *blacklist; ++i)
2705 {
2706 for (unsigned j = 0; j != 2; ++j)
2707 {
2708 if (!lookup_attribute (blacklist[i], tmpl_attrs[j]))
2709 continue;
2710
2711 for (unsigned k = 0; k != 1 + !!spec_attrs[1]; ++k)
2712 {
2713 if (lookup_attribute (blacklist[i], spec_attrs[k]))
2714 break;
2715
2716 if (nattrs)
2717 pp_string (&str, ", ");
2718 pp_begin_quote (&str, pp_show_color (global_dc->printer));
2719 pp_string (&str, blacklist[i]);
2720 pp_end_quote (&str, pp_show_color (global_dc->printer));
2721 ++nattrs;
2722 }
2723 }
2724 }
2725
2726 if (!nattrs)
2727 return;
2728
2729 auto_diagnostic_group d;
2730 if (warning_at (DECL_SOURCE_LOCATION (spec), OPT_Wmissing_attributes,
2731 "explicit specialization %q#D may be missing attributes",
2732 spec))
2733 inform (DECL_SOURCE_LOCATION (tmpl),
2734 nattrs > 1
2735 ? G_("missing primary template attributes %s")
2736 : G_("missing primary template attribute %s"),
2737 pp_formatted_text (&str));
2738 }
2739
2740 /* Check to see if the function just declared, as indicated in
2741 DECLARATOR, and in DECL, is a specialization of a function
2742 template. We may also discover that the declaration is an explicit
2743 instantiation at this point.
2744
2745 Returns DECL, or an equivalent declaration that should be used
2746 instead if all goes well. Issues an error message if something is
2747 amiss. Returns error_mark_node if the error is not easily
2748 recoverable.
2749
2750 FLAGS is a bitmask consisting of the following flags:
2751
2752 2: The function has a definition.
2753 4: The function is a friend.
2754
2755 The TEMPLATE_COUNT is the number of references to qualifying
2756 template classes that appeared in the name of the function. For
2757 example, in
2758
2759 template <class T> struct S { void f(); };
2760 void S<int>::f();
2761
2762 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2763 classes are not counted in the TEMPLATE_COUNT, so that in
2764
2765 template <class T> struct S {};
2766 template <> struct S<int> { void f(); }
2767 template <> void S<int>::f();
2768
2769 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2770 invalid; there should be no template <>.)
2771
2772 If the function is a specialization, it is marked as such via
2773 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2774 is set up correctly, and it is added to the list of specializations
2775 for that template. */
2776
2777 tree
2778 check_explicit_specialization (tree declarator,
2779 tree decl,
2780 int template_count,
2781 int flags,
2782 tree attrlist)
2783 {
2784 int have_def = flags & 2;
2785 int is_friend = flags & 4;
2786 bool is_concept = flags & 8;
2787 int specialization = 0;
2788 int explicit_instantiation = 0;
2789 int member_specialization = 0;
2790 tree ctype = DECL_CLASS_CONTEXT (decl);
2791 tree dname = DECL_NAME (decl);
2792 tmpl_spec_kind tsk;
2793
2794 if (is_friend)
2795 {
2796 if (!processing_specialization)
2797 tsk = tsk_none;
2798 else
2799 tsk = tsk_excessive_parms;
2800 }
2801 else
2802 tsk = current_tmpl_spec_kind (template_count);
2803
2804 switch (tsk)
2805 {
2806 case tsk_none:
2807 if (processing_specialization && !VAR_P (decl))
2808 {
2809 specialization = 1;
2810 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2811 }
2812 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2813 {
2814 if (is_friend)
2815 /* This could be something like:
2816
2817 template <class T> void f(T);
2818 class S { friend void f<>(int); } */
2819 specialization = 1;
2820 else
2821 {
2822 /* This case handles bogus declarations like template <>
2823 template <class T> void f<int>(); */
2824
2825 error ("template-id %qD in declaration of primary template",
2826 declarator);
2827 return decl;
2828 }
2829 }
2830 break;
2831
2832 case tsk_invalid_member_spec:
2833 /* The error has already been reported in
2834 check_specialization_scope. */
2835 return error_mark_node;
2836
2837 case tsk_invalid_expl_inst:
2838 error ("template parameter list used in explicit instantiation");
2839
2840 /* Fall through. */
2841
2842 case tsk_expl_inst:
2843 if (have_def)
2844 error ("definition provided for explicit instantiation");
2845
2846 explicit_instantiation = 1;
2847 break;
2848
2849 case tsk_excessive_parms:
2850 case tsk_insufficient_parms:
2851 if (tsk == tsk_excessive_parms)
2852 error ("too many template parameter lists in declaration of %qD",
2853 decl);
2854 else if (template_header_count)
2855 error("too few template parameter lists in declaration of %qD", decl);
2856 else
2857 error("explicit specialization of %qD must be introduced by "
2858 "%<template <>%>", decl);
2859
2860 /* Fall through. */
2861 case tsk_expl_spec:
2862 if (is_concept)
2863 error ("explicit specialization declared %<concept%>");
2864
2865 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2866 /* In cases like template<> constexpr bool v = true;
2867 We'll give an error in check_template_variable. */
2868 break;
2869
2870 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2871 if (ctype)
2872 member_specialization = 1;
2873 else
2874 specialization = 1;
2875 break;
2876
2877 case tsk_template:
2878 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2879 {
2880 /* This case handles bogus declarations like template <>
2881 template <class T> void f<int>(); */
2882
2883 if (!uses_template_parms (declarator))
2884 error ("template-id %qD in declaration of primary template",
2885 declarator);
2886 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2887 {
2888 /* Partial specialization of variable template. */
2889 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2890 specialization = 1;
2891 goto ok;
2892 }
2893 else if (cxx_dialect < cxx14)
2894 error ("non-type partial specialization %qD "
2895 "is not allowed", declarator);
2896 else
2897 error ("non-class, non-variable partial specialization %qD "
2898 "is not allowed", declarator);
2899 return decl;
2900 ok:;
2901 }
2902
2903 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2904 /* This is a specialization of a member template, without
2905 specialization the containing class. Something like:
2906
2907 template <class T> struct S {
2908 template <class U> void f (U);
2909 };
2910 template <> template <class U> void S<int>::f(U) {}
2911
2912 That's a specialization -- but of the entire template. */
2913 specialization = 1;
2914 break;
2915
2916 default:
2917 gcc_unreachable ();
2918 }
2919
2920 if ((specialization || member_specialization)
2921 /* This doesn't apply to variable templates. */
2922 && (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE
2923 || TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE))
2924 {
2925 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2926 for (; t; t = TREE_CHAIN (t))
2927 if (TREE_PURPOSE (t))
2928 {
2929 permerror (input_location,
2930 "default argument specified in explicit specialization");
2931 break;
2932 }
2933 }
2934
2935 if (specialization || member_specialization || explicit_instantiation)
2936 {
2937 tree tmpl = NULL_TREE;
2938 tree targs = NULL_TREE;
2939 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2940
2941 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2942 if (!was_template_id)
2943 {
2944 tree fns;
2945
2946 gcc_assert (identifier_p (declarator));
2947 if (ctype)
2948 fns = dname;
2949 else
2950 {
2951 /* If there is no class context, the explicit instantiation
2952 must be at namespace scope. */
2953 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2954
2955 /* Find the namespace binding, using the declaration
2956 context. */
2957 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2958 false, true);
2959 if (fns == error_mark_node)
2960 /* If lookup fails, look for a friend declaration so we can
2961 give a better diagnostic. */
2962 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2963 /*type*/false, /*complain*/true,
2964 /*hidden*/true);
2965
2966 if (fns == error_mark_node || !is_overloaded_fn (fns))
2967 {
2968 error ("%qD is not a template function", dname);
2969 fns = error_mark_node;
2970 }
2971 }
2972
2973 declarator = lookup_template_function (fns, NULL_TREE);
2974 }
2975
2976 if (declarator == error_mark_node)
2977 return error_mark_node;
2978
2979 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2980 {
2981 if (!explicit_instantiation)
2982 /* A specialization in class scope. This is invalid,
2983 but the error will already have been flagged by
2984 check_specialization_scope. */
2985 return error_mark_node;
2986 else
2987 {
2988 /* It's not valid to write an explicit instantiation in
2989 class scope, e.g.:
2990
2991 class C { template void f(); }
2992
2993 This case is caught by the parser. However, on
2994 something like:
2995
2996 template class C { void f(); };
2997
2998 (which is invalid) we can get here. The error will be
2999 issued later. */
3000 ;
3001 }
3002
3003 return decl;
3004 }
3005 else if (ctype != NULL_TREE
3006 && (identifier_p (TREE_OPERAND (declarator, 0))))
3007 {
3008 // We'll match variable templates in start_decl.
3009 if (VAR_P (decl))
3010 return decl;
3011
3012 /* Find the list of functions in ctype that have the same
3013 name as the declared function. */
3014 tree name = TREE_OPERAND (declarator, 0);
3015
3016 if (constructor_name_p (name, ctype))
3017 {
3018 if (DECL_CONSTRUCTOR_P (decl)
3019 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
3020 : !CLASSTYPE_DESTRUCTOR (ctype))
3021 {
3022 /* From [temp.expl.spec]:
3023
3024 If such an explicit specialization for the member
3025 of a class template names an implicitly-declared
3026 special member function (clause _special_), the
3027 program is ill-formed.
3028
3029 Similar language is found in [temp.explicit]. */
3030 error ("specialization of implicitly-declared special member function");
3031 return error_mark_node;
3032 }
3033
3034 name = DECL_NAME (decl);
3035 }
3036
3037 /* For a type-conversion operator, We might be looking for
3038 `operator int' which will be a specialization of
3039 `operator T'. Grab all the conversion operators, and
3040 then select from them. */
3041 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
3042 ? conv_op_identifier : name);
3043
3044 if (fns == NULL_TREE)
3045 {
3046 error ("no member function %qD declared in %qT", name, ctype);
3047 return error_mark_node;
3048 }
3049 else
3050 TREE_OPERAND (declarator, 0) = fns;
3051 }
3052
3053 /* Figure out what exactly is being specialized at this point.
3054 Note that for an explicit instantiation, even one for a
3055 member function, we cannot tell a priori whether the
3056 instantiation is for a member template, or just a member
3057 function of a template class. Even if a member template is
3058 being instantiated, the member template arguments may be
3059 elided if they can be deduced from the rest of the
3060 declaration. */
3061 tmpl = determine_specialization (declarator, decl,
3062 &targs,
3063 member_specialization,
3064 template_count,
3065 tsk);
3066
3067 if (!tmpl || tmpl == error_mark_node)
3068 /* We couldn't figure out what this declaration was
3069 specializing. */
3070 return error_mark_node;
3071 else
3072 {
3073 if (TREE_CODE (decl) == FUNCTION_DECL
3074 && DECL_HIDDEN_FRIEND_P (tmpl))
3075 {
3076 auto_diagnostic_group d;
3077 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3078 "friend declaration %qD is not visible to "
3079 "explicit specialization", tmpl))
3080 inform (DECL_SOURCE_LOCATION (tmpl),
3081 "friend declaration here");
3082 }
3083 else if (!ctype && !is_friend
3084 && CP_DECL_CONTEXT (decl) == current_namespace)
3085 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
3086
3087 tree gen_tmpl = most_general_template (tmpl);
3088
3089 if (explicit_instantiation)
3090 {
3091 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3092 is done by do_decl_instantiation later. */
3093
3094 int arg_depth = TMPL_ARGS_DEPTH (targs);
3095 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3096
3097 if (arg_depth > parm_depth)
3098 {
3099 /* If TMPL is not the most general template (for
3100 example, if TMPL is a friend template that is
3101 injected into namespace scope), then there will
3102 be too many levels of TARGS. Remove some of them
3103 here. */
3104 int i;
3105 tree new_targs;
3106
3107 new_targs = make_tree_vec (parm_depth);
3108 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3109 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3110 = TREE_VEC_ELT (targs, i);
3111 targs = new_targs;
3112 }
3113
3114 return instantiate_template (tmpl, targs, tf_error);
3115 }
3116
3117 /* If we thought that the DECL was a member function, but it
3118 turns out to be specializing a static member function,
3119 make DECL a static member function as well. */
3120 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3121 && DECL_STATIC_FUNCTION_P (tmpl)
3122 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3123 revert_static_member_fn (decl);
3124
3125 /* If this is a specialization of a member template of a
3126 template class, we want to return the TEMPLATE_DECL, not
3127 the specialization of it. */
3128 if (tsk == tsk_template && !was_template_id)
3129 {
3130 tree result = DECL_TEMPLATE_RESULT (tmpl);
3131 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3132 DECL_INITIAL (result) = NULL_TREE;
3133 if (have_def)
3134 {
3135 tree parm;
3136 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3137 DECL_SOURCE_LOCATION (result)
3138 = DECL_SOURCE_LOCATION (decl);
3139 /* We want to use the argument list specified in the
3140 definition, not in the original declaration. */
3141 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3142 for (parm = DECL_ARGUMENTS (result); parm;
3143 parm = DECL_CHAIN (parm))
3144 DECL_CONTEXT (parm) = result;
3145 }
3146 return register_specialization (tmpl, gen_tmpl, targs,
3147 is_friend, 0);
3148 }
3149
3150 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3151 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3152
3153 if (was_template_id)
3154 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3155
3156 /* Inherit default function arguments from the template
3157 DECL is specializing. */
3158 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3159 copy_default_args_to_explicit_spec (decl);
3160
3161 /* This specialization has the same protection as the
3162 template it specializes. */
3163 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3164 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3165
3166 /* 7.1.1-1 [dcl.stc]
3167
3168 A storage-class-specifier shall not be specified in an
3169 explicit specialization...
3170
3171 The parser rejects these, so unless action is taken here,
3172 explicit function specializations will always appear with
3173 global linkage.
3174
3175 The action recommended by the C++ CWG in response to C++
3176 defect report 605 is to make the storage class and linkage
3177 of the explicit specialization match the templated function:
3178
3179 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3180 */
3181 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3182 {
3183 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3184 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3185
3186 /* A concept cannot be specialized. */
3187 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3188 {
3189 error ("explicit specialization of function concept %qD",
3190 gen_tmpl);
3191 return error_mark_node;
3192 }
3193
3194 /* This specialization has the same linkage and visibility as
3195 the function template it specializes. */
3196 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3197 if (! TREE_PUBLIC (decl))
3198 {
3199 DECL_INTERFACE_KNOWN (decl) = 1;
3200 DECL_NOT_REALLY_EXTERN (decl) = 1;
3201 }
3202 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3203 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3204 {
3205 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3206 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3207 }
3208 }
3209
3210 /* If DECL is a friend declaration, declared using an
3211 unqualified name, the namespace associated with DECL may
3212 have been set incorrectly. For example, in:
3213
3214 template <typename T> void f(T);
3215 namespace N {
3216 struct S { friend void f<int>(int); }
3217 }
3218
3219 we will have set the DECL_CONTEXT for the friend
3220 declaration to N, rather than to the global namespace. */
3221 if (DECL_NAMESPACE_SCOPE_P (decl))
3222 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3223
3224 if (is_friend && !have_def)
3225 /* This is not really a declaration of a specialization.
3226 It's just the name of an instantiation. But, it's not
3227 a request for an instantiation, either. */
3228 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3229 else if (TREE_CODE (decl) == FUNCTION_DECL)
3230 /* A specialization is not necessarily COMDAT. */
3231 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3232 && DECL_DECLARED_INLINE_P (decl));
3233 else if (VAR_P (decl))
3234 DECL_COMDAT (decl) = false;
3235
3236 /* If this is a full specialization, register it so that we can find
3237 it again. Partial specializations will be registered in
3238 process_partial_specialization. */
3239 if (!processing_template_decl)
3240 {
3241 warn_spec_missing_attributes (gen_tmpl, decl, attrlist);
3242
3243 decl = register_specialization (decl, gen_tmpl, targs,
3244 is_friend, 0);
3245 }
3246
3247
3248 /* A 'structor should already have clones. */
3249 gcc_assert (decl == error_mark_node
3250 || variable_template_p (tmpl)
3251 || !(DECL_CONSTRUCTOR_P (decl)
3252 || DECL_DESTRUCTOR_P (decl))
3253 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3254 }
3255 }
3256
3257 return decl;
3258 }
3259
3260 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3261 parameters. These are represented in the same format used for
3262 DECL_TEMPLATE_PARMS. */
3263
3264 int
3265 comp_template_parms (const_tree parms1, const_tree parms2)
3266 {
3267 const_tree p1;
3268 const_tree p2;
3269
3270 if (parms1 == parms2)
3271 return 1;
3272
3273 for (p1 = parms1, p2 = parms2;
3274 p1 != NULL_TREE && p2 != NULL_TREE;
3275 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3276 {
3277 tree t1 = TREE_VALUE (p1);
3278 tree t2 = TREE_VALUE (p2);
3279 int i;
3280
3281 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3282 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3283
3284 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3285 return 0;
3286
3287 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3288 {
3289 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3290 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3291
3292 /* If either of the template parameters are invalid, assume
3293 they match for the sake of error recovery. */
3294 if (error_operand_p (parm1) || error_operand_p (parm2))
3295 return 1;
3296
3297 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3298 return 0;
3299
3300 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3301 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3302 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3303 continue;
3304 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3305 return 0;
3306 }
3307 }
3308
3309 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3310 /* One set of parameters has more parameters lists than the
3311 other. */
3312 return 0;
3313
3314 return 1;
3315 }
3316
3317 /* Determine whether PARM is a parameter pack. */
3318
3319 bool
3320 template_parameter_pack_p (const_tree parm)
3321 {
3322 /* Determine if we have a non-type template parameter pack. */
3323 if (TREE_CODE (parm) == PARM_DECL)
3324 return (DECL_TEMPLATE_PARM_P (parm)
3325 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3326 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3327 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3328
3329 /* If this is a list of template parameters, we could get a
3330 TYPE_DECL or a TEMPLATE_DECL. */
3331 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3332 parm = TREE_TYPE (parm);
3333
3334 /* Otherwise it must be a type template parameter. */
3335 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3336 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3337 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3338 }
3339
3340 /* Determine if T is a function parameter pack. */
3341
3342 bool
3343 function_parameter_pack_p (const_tree t)
3344 {
3345 if (t && TREE_CODE (t) == PARM_DECL)
3346 return DECL_PACK_P (t);
3347 return false;
3348 }
3349
3350 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3351 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3352
3353 tree
3354 get_function_template_decl (const_tree primary_func_tmpl_inst)
3355 {
3356 if (! primary_func_tmpl_inst
3357 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3358 || ! primary_template_specialization_p (primary_func_tmpl_inst))
3359 return NULL;
3360
3361 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3362 }
3363
3364 /* Return true iff the function parameter PARAM_DECL was expanded
3365 from the function parameter pack PACK. */
3366
3367 bool
3368 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3369 {
3370 if (DECL_ARTIFICIAL (param_decl)
3371 || !function_parameter_pack_p (pack))
3372 return false;
3373
3374 /* The parameter pack and its pack arguments have the same
3375 DECL_PARM_INDEX. */
3376 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3377 }
3378
3379 /* Determine whether ARGS describes a variadic template args list,
3380 i.e., one that is terminated by a template argument pack. */
3381
3382 static bool
3383 template_args_variadic_p (tree args)
3384 {
3385 int nargs;
3386 tree last_parm;
3387
3388 if (args == NULL_TREE)
3389 return false;
3390
3391 args = INNERMOST_TEMPLATE_ARGS (args);
3392 nargs = TREE_VEC_LENGTH (args);
3393
3394 if (nargs == 0)
3395 return false;
3396
3397 last_parm = TREE_VEC_ELT (args, nargs - 1);
3398
3399 return ARGUMENT_PACK_P (last_parm);
3400 }
3401
3402 /* Generate a new name for the parameter pack name NAME (an
3403 IDENTIFIER_NODE) that incorporates its */
3404
3405 static tree
3406 make_ith_pack_parameter_name (tree name, int i)
3407 {
3408 /* Munge the name to include the parameter index. */
3409 #define NUMBUF_LEN 128
3410 char numbuf[NUMBUF_LEN];
3411 char* newname;
3412 int newname_len;
3413
3414 if (name == NULL_TREE)
3415 return name;
3416 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3417 newname_len = IDENTIFIER_LENGTH (name)
3418 + strlen (numbuf) + 2;
3419 newname = (char*)alloca (newname_len);
3420 snprintf (newname, newname_len,
3421 "%s#%i", IDENTIFIER_POINTER (name), i);
3422 return get_identifier (newname);
3423 }
3424
3425 /* Return true if T is a primary function, class or alias template
3426 specialization, not including the template pattern. */
3427
3428 bool
3429 primary_template_specialization_p (const_tree t)
3430 {
3431 if (!t)
3432 return false;
3433
3434 if (TREE_CODE (t) == FUNCTION_DECL || VAR_P (t))
3435 return (DECL_LANG_SPECIFIC (t)
3436 && DECL_USE_TEMPLATE (t)
3437 && DECL_TEMPLATE_INFO (t)
3438 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
3439 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3440 return (CLASSTYPE_TEMPLATE_INFO (t)
3441 && CLASSTYPE_USE_TEMPLATE (t)
3442 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
3443 else if (alias_template_specialization_p (t))
3444 return true;
3445 return false;
3446 }
3447
3448 /* Return true if PARM is a template template parameter. */
3449
3450 bool
3451 template_template_parameter_p (const_tree parm)
3452 {
3453 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3454 }
3455
3456 /* Return true iff PARM is a DECL representing a type template
3457 parameter. */
3458
3459 bool
3460 template_type_parameter_p (const_tree parm)
3461 {
3462 return (parm
3463 && (TREE_CODE (parm) == TYPE_DECL
3464 || TREE_CODE (parm) == TEMPLATE_DECL)
3465 && DECL_TEMPLATE_PARM_P (parm));
3466 }
3467
3468 /* Return the template parameters of T if T is a
3469 primary template instantiation, NULL otherwise. */
3470
3471 tree
3472 get_primary_template_innermost_parameters (const_tree t)
3473 {
3474 tree parms = NULL, template_info = NULL;
3475
3476 if ((template_info = get_template_info (t))
3477 && primary_template_specialization_p (t))
3478 parms = INNERMOST_TEMPLATE_PARMS
3479 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3480
3481 return parms;
3482 }
3483
3484 /* Return the template parameters of the LEVELth level from the full list
3485 of template parameters PARMS. */
3486
3487 tree
3488 get_template_parms_at_level (tree parms, int level)
3489 {
3490 tree p;
3491 if (!parms
3492 || TREE_CODE (parms) != TREE_LIST
3493 || level > TMPL_PARMS_DEPTH (parms))
3494 return NULL_TREE;
3495
3496 for (p = parms; p; p = TREE_CHAIN (p))
3497 if (TMPL_PARMS_DEPTH (p) == level)
3498 return p;
3499
3500 return NULL_TREE;
3501 }
3502
3503 /* Returns the template arguments of T if T is a template instantiation,
3504 NULL otherwise. */
3505
3506 tree
3507 get_template_innermost_arguments (const_tree t)
3508 {
3509 tree args = NULL, template_info = NULL;
3510
3511 if ((template_info = get_template_info (t))
3512 && TI_ARGS (template_info))
3513 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3514
3515 return args;
3516 }
3517
3518 /* Return the argument pack elements of T if T is a template argument pack,
3519 NULL otherwise. */
3520
3521 tree
3522 get_template_argument_pack_elems (const_tree t)
3523 {
3524 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3525 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3526 return NULL;
3527
3528 return ARGUMENT_PACK_ARGS (t);
3529 }
3530
3531 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3532 ARGUMENT_PACK_SELECT represents. */
3533
3534 static tree
3535 argument_pack_select_arg (tree t)
3536 {
3537 tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
3538 tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
3539
3540 /* If the selected argument is an expansion E, that most likely means we were
3541 called from gen_elem_of_pack_expansion_instantiation during the
3542 substituting of an argument pack (of which the Ith element is a pack
3543 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3544 In this case, the Ith element resulting from this substituting is going to
3545 be a pack expansion, which pattern is the pattern of E. Let's return the
3546 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3547 resulting pack expansion from it. */
3548 if (PACK_EXPANSION_P (arg))
3549 {
3550 /* Make sure we aren't throwing away arg info. */
3551 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
3552 arg = PACK_EXPANSION_PATTERN (arg);
3553 }
3554
3555 return arg;
3556 }
3557
3558
3559 /* True iff FN is a function representing a built-in variadic parameter
3560 pack. */
3561
3562 bool
3563 builtin_pack_fn_p (tree fn)
3564 {
3565 if (!fn
3566 || TREE_CODE (fn) != FUNCTION_DECL
3567 || !DECL_IS_BUILTIN (fn))
3568 return false;
3569
3570 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3571 return true;
3572
3573 return false;
3574 }
3575
3576 /* True iff CALL is a call to a function representing a built-in variadic
3577 parameter pack. */
3578
3579 static bool
3580 builtin_pack_call_p (tree call)
3581 {
3582 if (TREE_CODE (call) != CALL_EXPR)
3583 return false;
3584 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3585 }
3586
3587 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3588
3589 static tree
3590 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3591 tree in_decl)
3592 {
3593 tree ohi = CALL_EXPR_ARG (call, 0);
3594 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3595 false/*fn*/, true/*int_cst*/);
3596
3597 if (value_dependent_expression_p (hi))
3598 {
3599 if (hi != ohi)
3600 {
3601 call = copy_node (call);
3602 CALL_EXPR_ARG (call, 0) = hi;
3603 }
3604 tree ex = make_pack_expansion (call, complain);
3605 tree vec = make_tree_vec (1);
3606 TREE_VEC_ELT (vec, 0) = ex;
3607 return vec;
3608 }
3609 else
3610 {
3611 hi = cxx_constant_value (hi);
3612 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3613
3614 /* Calculate the largest value of len that won't make the size of the vec
3615 overflow an int. The compiler will exceed resource limits long before
3616 this, but it seems a decent place to diagnose. */
3617 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3618
3619 if (len < 0 || len > max)
3620 {
3621 if ((complain & tf_error)
3622 && hi != error_mark_node)
3623 error ("argument to __integer_pack must be between 0 and %d", max);
3624 return error_mark_node;
3625 }
3626
3627 tree vec = make_tree_vec (len);
3628
3629 for (int i = 0; i < len; ++i)
3630 TREE_VEC_ELT (vec, i) = size_int (i);
3631
3632 return vec;
3633 }
3634 }
3635
3636 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3637 CALL. */
3638
3639 static tree
3640 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3641 tree in_decl)
3642 {
3643 if (!builtin_pack_call_p (call))
3644 return NULL_TREE;
3645
3646 tree fn = CALL_EXPR_FN (call);
3647
3648 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3649 return expand_integer_pack (call, args, complain, in_decl);
3650
3651 return NULL_TREE;
3652 }
3653
3654 /* Structure used to track the progress of find_parameter_packs_r. */
3655 struct find_parameter_pack_data
3656 {
3657 /* TREE_LIST that will contain all of the parameter packs found by
3658 the traversal. */
3659 tree* parameter_packs;
3660
3661 /* Set of AST nodes that have been visited by the traversal. */
3662 hash_set<tree> *visited;
3663
3664 /* True iff we're making a type pack expansion. */
3665 bool type_pack_expansion_p;
3666 };
3667
3668 /* Identifies all of the argument packs that occur in a template
3669 argument and appends them to the TREE_LIST inside DATA, which is a
3670 find_parameter_pack_data structure. This is a subroutine of
3671 make_pack_expansion and uses_parameter_packs. */
3672 static tree
3673 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3674 {
3675 tree t = *tp;
3676 struct find_parameter_pack_data* ppd =
3677 (struct find_parameter_pack_data*)data;
3678 bool parameter_pack_p = false;
3679
3680 /* Handle type aliases/typedefs. */
3681 if (TYPE_ALIAS_P (t))
3682 {
3683 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3684 cp_walk_tree (&TI_ARGS (tinfo),
3685 &find_parameter_packs_r,
3686 ppd, ppd->visited);
3687 *walk_subtrees = 0;
3688 return NULL_TREE;
3689 }
3690
3691 /* Identify whether this is a parameter pack or not. */
3692 switch (TREE_CODE (t))
3693 {
3694 case TEMPLATE_PARM_INDEX:
3695 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3696 parameter_pack_p = true;
3697 break;
3698
3699 case TEMPLATE_TYPE_PARM:
3700 t = TYPE_MAIN_VARIANT (t);
3701 /* FALLTHRU */
3702 case TEMPLATE_TEMPLATE_PARM:
3703 /* If the placeholder appears in the decl-specifier-seq of a function
3704 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3705 is a pack expansion, the invented template parameter is a template
3706 parameter pack. */
3707 if (ppd->type_pack_expansion_p && is_auto (t))
3708 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3709 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3710 parameter_pack_p = true;
3711 break;
3712
3713 case FIELD_DECL:
3714 case PARM_DECL:
3715 if (DECL_PACK_P (t))
3716 {
3717 /* We don't want to walk into the type of a PARM_DECL,
3718 because we don't want to see the type parameter pack. */
3719 *walk_subtrees = 0;
3720 parameter_pack_p = true;
3721 }
3722 break;
3723
3724 case VAR_DECL:
3725 if (DECL_PACK_P (t))
3726 {
3727 /* We don't want to walk into the type of a variadic capture proxy,
3728 because we don't want to see the type parameter pack. */
3729 *walk_subtrees = 0;
3730 parameter_pack_p = true;
3731 }
3732 else if (variable_template_specialization_p (t))
3733 {
3734 cp_walk_tree (&DECL_TI_ARGS (t),
3735 find_parameter_packs_r,
3736 ppd, ppd->visited);
3737 *walk_subtrees = 0;
3738 }
3739 break;
3740
3741 case CALL_EXPR:
3742 if (builtin_pack_call_p (t))
3743 parameter_pack_p = true;
3744 break;
3745
3746 case BASES:
3747 parameter_pack_p = true;
3748 break;
3749 default:
3750 /* Not a parameter pack. */
3751 break;
3752 }
3753
3754 if (parameter_pack_p)
3755 {
3756 /* Add this parameter pack to the list. */
3757 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3758 }
3759
3760 if (TYPE_P (t))
3761 cp_walk_tree (&TYPE_CONTEXT (t),
3762 &find_parameter_packs_r, ppd, ppd->visited);
3763
3764 /* This switch statement will return immediately if we don't find a
3765 parameter pack. */
3766 switch (TREE_CODE (t))
3767 {
3768 case TEMPLATE_PARM_INDEX:
3769 return NULL_TREE;
3770
3771 case BOUND_TEMPLATE_TEMPLATE_PARM:
3772 /* Check the template itself. */
3773 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3774 &find_parameter_packs_r, ppd, ppd->visited);
3775 /* Check the template arguments. */
3776 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3777 ppd->visited);
3778 *walk_subtrees = 0;
3779 return NULL_TREE;
3780
3781 case TEMPLATE_TYPE_PARM:
3782 case TEMPLATE_TEMPLATE_PARM:
3783 return NULL_TREE;
3784
3785 case PARM_DECL:
3786 return NULL_TREE;
3787
3788 case DECL_EXPR:
3789 /* Ignore the declaration of a capture proxy for a parameter pack. */
3790 if (is_capture_proxy (DECL_EXPR_DECL (t)))
3791 *walk_subtrees = 0;
3792 return NULL_TREE;
3793
3794 case RECORD_TYPE:
3795 if (TYPE_PTRMEMFUNC_P (t))
3796 return NULL_TREE;
3797 /* Fall through. */
3798
3799 case UNION_TYPE:
3800 case ENUMERAL_TYPE:
3801 if (TYPE_TEMPLATE_INFO (t))
3802 cp_walk_tree (&TYPE_TI_ARGS (t),
3803 &find_parameter_packs_r, ppd, ppd->visited);
3804
3805 *walk_subtrees = 0;
3806 return NULL_TREE;
3807
3808 case TEMPLATE_DECL:
3809 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3810 return NULL_TREE;
3811 gcc_fallthrough();
3812
3813 case CONSTRUCTOR:
3814 cp_walk_tree (&TREE_TYPE (t),
3815 &find_parameter_packs_r, ppd, ppd->visited);
3816 return NULL_TREE;
3817
3818 case TYPENAME_TYPE:
3819 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3820 ppd, ppd->visited);
3821 *walk_subtrees = 0;
3822 return NULL_TREE;
3823
3824 case TYPE_PACK_EXPANSION:
3825 case EXPR_PACK_EXPANSION:
3826 *walk_subtrees = 0;
3827 return NULL_TREE;
3828
3829 case INTEGER_TYPE:
3830 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3831 ppd, ppd->visited);
3832 *walk_subtrees = 0;
3833 return NULL_TREE;
3834
3835 case IDENTIFIER_NODE:
3836 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3837 ppd->visited);
3838 *walk_subtrees = 0;
3839 return NULL_TREE;
3840
3841 case LAMBDA_EXPR:
3842 {
3843 /* Look at explicit captures. */
3844 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t);
3845 cap; cap = TREE_CHAIN (cap))
3846 cp_walk_tree (&TREE_VALUE (cap), &find_parameter_packs_r, ppd,
3847 ppd->visited);
3848 /* Since we defer implicit capture, look in the parms and body. */
3849 tree fn = lambda_function (t);
3850 cp_walk_tree (&TREE_TYPE (fn), &find_parameter_packs_r, ppd,
3851 ppd->visited);
3852 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
3853 ppd->visited);
3854 *walk_subtrees = 0;
3855 return NULL_TREE;
3856 }
3857
3858 case DECLTYPE_TYPE:
3859 {
3860 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3861 type_pack_expansion_p to false so that any placeholders
3862 within the expression don't get marked as parameter packs. */
3863 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
3864 ppd->type_pack_expansion_p = false;
3865 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
3866 ppd, ppd->visited);
3867 ppd->type_pack_expansion_p = type_pack_expansion_p;
3868 *walk_subtrees = 0;
3869 return NULL_TREE;
3870 }
3871
3872 case IF_STMT:
3873 cp_walk_tree (&IF_COND (t), &find_parameter_packs_r,
3874 ppd, ppd->visited);
3875 cp_walk_tree (&THEN_CLAUSE (t), &find_parameter_packs_r,
3876 ppd, ppd->visited);
3877 cp_walk_tree (&ELSE_CLAUSE (t), &find_parameter_packs_r,
3878 ppd, ppd->visited);
3879 /* Don't walk into IF_STMT_EXTRA_ARGS. */
3880 *walk_subtrees = 0;
3881 return NULL_TREE;
3882
3883 default:
3884 return NULL_TREE;
3885 }
3886
3887 return NULL_TREE;
3888 }
3889
3890 /* Determines if the expression or type T uses any parameter packs. */
3891 bool
3892 uses_parameter_packs (tree t)
3893 {
3894 tree parameter_packs = NULL_TREE;
3895 struct find_parameter_pack_data ppd;
3896 ppd.parameter_packs = &parameter_packs;
3897 ppd.visited = new hash_set<tree>;
3898 ppd.type_pack_expansion_p = false;
3899 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3900 delete ppd.visited;
3901 return parameter_packs != NULL_TREE;
3902 }
3903
3904 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3905 representation a base-class initializer into a parameter pack
3906 expansion. If all goes well, the resulting node will be an
3907 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3908 respectively. */
3909 tree
3910 make_pack_expansion (tree arg, tsubst_flags_t complain)
3911 {
3912 tree result;
3913 tree parameter_packs = NULL_TREE;
3914 bool for_types = false;
3915 struct find_parameter_pack_data ppd;
3916
3917 if (!arg || arg == error_mark_node)
3918 return arg;
3919
3920 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
3921 {
3922 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3923 class initializer. In this case, the TREE_PURPOSE will be a
3924 _TYPE node (representing the base class expansion we're
3925 initializing) and the TREE_VALUE will be a TREE_LIST
3926 containing the initialization arguments.
3927
3928 The resulting expansion looks somewhat different from most
3929 expansions. Rather than returning just one _EXPANSION, we
3930 return a TREE_LIST whose TREE_PURPOSE is a
3931 TYPE_PACK_EXPANSION containing the bases that will be
3932 initialized. The TREE_VALUE will be identical to the
3933 original TREE_VALUE, which is a list of arguments that will
3934 be passed to each base. We do not introduce any new pack
3935 expansion nodes into the TREE_VALUE (although it is possible
3936 that some already exist), because the TREE_PURPOSE and
3937 TREE_VALUE all need to be expanded together with the same
3938 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3939 resulting TREE_PURPOSE will mention the parameter packs in
3940 both the bases and the arguments to the bases. */
3941 tree purpose;
3942 tree value;
3943 tree parameter_packs = NULL_TREE;
3944
3945 /* Determine which parameter packs will be used by the base
3946 class expansion. */
3947 ppd.visited = new hash_set<tree>;
3948 ppd.parameter_packs = &parameter_packs;
3949 ppd.type_pack_expansion_p = true;
3950 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
3951 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3952 &ppd, ppd.visited);
3953
3954 if (parameter_packs == NULL_TREE)
3955 {
3956 if (complain & tf_error)
3957 error ("base initializer expansion %qT contains no parameter packs",
3958 arg);
3959 delete ppd.visited;
3960 return error_mark_node;
3961 }
3962
3963 if (TREE_VALUE (arg) != void_type_node)
3964 {
3965 /* Collect the sets of parameter packs used in each of the
3966 initialization arguments. */
3967 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3968 {
3969 /* Determine which parameter packs will be expanded in this
3970 argument. */
3971 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3972 &ppd, ppd.visited);
3973 }
3974 }
3975
3976 delete ppd.visited;
3977
3978 /* Create the pack expansion type for the base type. */
3979 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3980 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3981 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3982 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
3983
3984 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3985 they will rarely be compared to anything. */
3986 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3987
3988 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3989 }
3990
3991 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3992 for_types = true;
3993
3994 /* Build the PACK_EXPANSION_* node. */
3995 result = for_types
3996 ? cxx_make_type (TYPE_PACK_EXPANSION)
3997 : make_node (EXPR_PACK_EXPANSION);
3998 SET_PACK_EXPANSION_PATTERN (result, arg);
3999 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
4000 {
4001 /* Propagate type and const-expression information. */
4002 TREE_TYPE (result) = TREE_TYPE (arg);
4003 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
4004 /* Mark this read now, since the expansion might be length 0. */
4005 mark_exp_read (arg);
4006 }
4007 else
4008 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4009 they will rarely be compared to anything. */
4010 SET_TYPE_STRUCTURAL_EQUALITY (result);
4011
4012 /* Determine which parameter packs will be expanded. */
4013 ppd.parameter_packs = &parameter_packs;
4014 ppd.visited = new hash_set<tree>;
4015 ppd.type_pack_expansion_p = TYPE_P (arg);
4016 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
4017 delete ppd.visited;
4018
4019 /* Make sure we found some parameter packs. */
4020 if (parameter_packs == NULL_TREE)
4021 {
4022 if (complain & tf_error)
4023 {
4024 if (TYPE_P (arg))
4025 error ("expansion pattern %qT contains no parameter packs", arg);
4026 else
4027 error ("expansion pattern %qE contains no parameter packs", arg);
4028 }
4029 return error_mark_node;
4030 }
4031 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
4032
4033 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
4034
4035 return result;
4036 }
4037
4038 /* Checks T for any "bare" parameter packs, which have not yet been
4039 expanded, and issues an error if any are found. This operation can
4040 only be done on full expressions or types (e.g., an expression
4041 statement, "if" condition, etc.), because we could have expressions like:
4042
4043 foo(f(g(h(args)))...)
4044
4045 where "args" is a parameter pack. check_for_bare_parameter_packs
4046 should not be called for the subexpressions args, h(args),
4047 g(h(args)), or f(g(h(args))), because we would produce erroneous
4048 error messages.
4049
4050 Returns TRUE and emits an error if there were bare parameter packs,
4051 returns FALSE otherwise. */
4052 bool
4053 check_for_bare_parameter_packs (tree t, location_t loc /* = UNKNOWN_LOCATION */)
4054 {
4055 tree parameter_packs = NULL_TREE;
4056 struct find_parameter_pack_data ppd;
4057
4058 if (!processing_template_decl || !t || t == error_mark_node)
4059 return false;
4060
4061 /* A lambda might use a parameter pack from the containing context. */
4062 if (current_class_type && LAMBDA_TYPE_P (current_class_type)
4063 && CLASSTYPE_TEMPLATE_INFO (current_class_type))
4064 return false;
4065
4066 if (TREE_CODE (t) == TYPE_DECL)
4067 t = TREE_TYPE (t);
4068
4069 ppd.parameter_packs = &parameter_packs;
4070 ppd.visited = new hash_set<tree>;
4071 ppd.type_pack_expansion_p = false;
4072 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4073 delete ppd.visited;
4074
4075 if (parameter_packs)
4076 {
4077 if (loc == UNKNOWN_LOCATION)
4078 loc = cp_expr_loc_or_loc (t, input_location);
4079 error_at (loc, "parameter packs not expanded with %<...%>:");
4080 while (parameter_packs)
4081 {
4082 tree pack = TREE_VALUE (parameter_packs);
4083 tree name = NULL_TREE;
4084
4085 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
4086 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
4087 name = TYPE_NAME (pack);
4088 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
4089 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
4090 else if (TREE_CODE (pack) == CALL_EXPR)
4091 name = DECL_NAME (CALL_EXPR_FN (pack));
4092 else
4093 name = DECL_NAME (pack);
4094
4095 if (name)
4096 inform (loc, " %qD", name);
4097 else
4098 inform (loc, " <anonymous>");
4099
4100 parameter_packs = TREE_CHAIN (parameter_packs);
4101 }
4102
4103 return true;
4104 }
4105
4106 return false;
4107 }
4108
4109 /* Expand any parameter packs that occur in the template arguments in
4110 ARGS. */
4111 tree
4112 expand_template_argument_pack (tree args)
4113 {
4114 if (args == error_mark_node)
4115 return error_mark_node;
4116
4117 tree result_args = NULL_TREE;
4118 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
4119 int num_result_args = -1;
4120 int non_default_args_count = -1;
4121
4122 /* First, determine if we need to expand anything, and the number of
4123 slots we'll need. */
4124 for (in_arg = 0; in_arg < nargs; ++in_arg)
4125 {
4126 tree arg = TREE_VEC_ELT (args, in_arg);
4127 if (arg == NULL_TREE)
4128 return args;
4129 if (ARGUMENT_PACK_P (arg))
4130 {
4131 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
4132 if (num_result_args < 0)
4133 num_result_args = in_arg + num_packed;
4134 else
4135 num_result_args += num_packed;
4136 }
4137 else
4138 {
4139 if (num_result_args >= 0)
4140 num_result_args++;
4141 }
4142 }
4143
4144 /* If no expansion is necessary, we're done. */
4145 if (num_result_args < 0)
4146 return args;
4147
4148 /* Expand arguments. */
4149 result_args = make_tree_vec (num_result_args);
4150 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
4151 non_default_args_count =
4152 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
4153 for (in_arg = 0; in_arg < nargs; ++in_arg)
4154 {
4155 tree arg = TREE_VEC_ELT (args, in_arg);
4156 if (ARGUMENT_PACK_P (arg))
4157 {
4158 tree packed = ARGUMENT_PACK_ARGS (arg);
4159 int i, num_packed = TREE_VEC_LENGTH (packed);
4160 for (i = 0; i < num_packed; ++i, ++out_arg)
4161 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
4162 if (non_default_args_count > 0)
4163 non_default_args_count += num_packed - 1;
4164 }
4165 else
4166 {
4167 TREE_VEC_ELT (result_args, out_arg) = arg;
4168 ++out_arg;
4169 }
4170 }
4171 if (non_default_args_count >= 0)
4172 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
4173 return result_args;
4174 }
4175
4176 /* Checks if DECL shadows a template parameter.
4177
4178 [temp.local]: A template-parameter shall not be redeclared within its
4179 scope (including nested scopes).
4180
4181 Emits an error and returns TRUE if the DECL shadows a parameter,
4182 returns FALSE otherwise. */
4183
4184 bool
4185 check_template_shadow (tree decl)
4186 {
4187 tree olddecl;
4188
4189 /* If we're not in a template, we can't possibly shadow a template
4190 parameter. */
4191 if (!current_template_parms)
4192 return true;
4193
4194 /* Figure out what we're shadowing. */
4195 decl = OVL_FIRST (decl);
4196 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4197
4198 /* If there's no previous binding for this name, we're not shadowing
4199 anything, let alone a template parameter. */
4200 if (!olddecl)
4201 return true;
4202
4203 /* If we're not shadowing a template parameter, we're done. Note
4204 that OLDDECL might be an OVERLOAD (or perhaps even an
4205 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4206 node. */
4207 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4208 return true;
4209
4210 /* We check for decl != olddecl to avoid bogus errors for using a
4211 name inside a class. We check TPFI to avoid duplicate errors for
4212 inline member templates. */
4213 if (decl == olddecl
4214 || (DECL_TEMPLATE_PARM_P (decl)
4215 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4216 return true;
4217
4218 /* Don't complain about the injected class name, as we've already
4219 complained about the class itself. */
4220 if (DECL_SELF_REFERENCE_P (decl))
4221 return false;
4222
4223 if (DECL_TEMPLATE_PARM_P (decl))
4224 error ("declaration of template parameter %q+D shadows "
4225 "template parameter", decl);
4226 else
4227 error ("declaration of %q+#D shadows template parameter", decl);
4228 inform (DECL_SOURCE_LOCATION (olddecl),
4229 "template parameter %qD declared here", olddecl);
4230 return false;
4231 }
4232
4233 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4234 ORIG_LEVEL, DECL, and TYPE. */
4235
4236 static tree
4237 build_template_parm_index (int index,
4238 int level,
4239 int orig_level,
4240 tree decl,
4241 tree type)
4242 {
4243 tree t = make_node (TEMPLATE_PARM_INDEX);
4244 TEMPLATE_PARM_IDX (t) = index;
4245 TEMPLATE_PARM_LEVEL (t) = level;
4246 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4247 TEMPLATE_PARM_DECL (t) = decl;
4248 TREE_TYPE (t) = type;
4249 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4250 TREE_READONLY (t) = TREE_READONLY (decl);
4251
4252 return t;
4253 }
4254
4255 /* Find the canonical type parameter for the given template type
4256 parameter. Returns the canonical type parameter, which may be TYPE
4257 if no such parameter existed. */
4258
4259 static tree
4260 canonical_type_parameter (tree type)
4261 {
4262 tree list;
4263 int idx = TEMPLATE_TYPE_IDX (type);
4264 if (!canonical_template_parms)
4265 vec_alloc (canonical_template_parms, idx + 1);
4266
4267 if (canonical_template_parms->length () <= (unsigned) idx)
4268 vec_safe_grow_cleared (canonical_template_parms, idx + 1);
4269
4270 list = (*canonical_template_parms)[idx];
4271 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4272 list = TREE_CHAIN (list);
4273
4274 if (list)
4275 return TREE_VALUE (list);
4276 else
4277 {
4278 (*canonical_template_parms)[idx]
4279 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4280 return type;
4281 }
4282 }
4283
4284 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4285 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4286 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4287 new one is created. */
4288
4289 static tree
4290 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4291 tsubst_flags_t complain)
4292 {
4293 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4294 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4295 != TEMPLATE_PARM_LEVEL (index) - levels)
4296 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4297 {
4298 tree orig_decl = TEMPLATE_PARM_DECL (index);
4299 tree decl, t;
4300
4301 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4302 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
4303 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4304 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4305 DECL_ARTIFICIAL (decl) = 1;
4306 SET_DECL_TEMPLATE_PARM_P (decl);
4307
4308 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4309 TEMPLATE_PARM_LEVEL (index) - levels,
4310 TEMPLATE_PARM_ORIG_LEVEL (index),
4311 decl, type);
4312 TEMPLATE_PARM_DESCENDANTS (index) = t;
4313 TEMPLATE_PARM_PARAMETER_PACK (t)
4314 = TEMPLATE_PARM_PARAMETER_PACK (index);
4315
4316 /* Template template parameters need this. */
4317 if (TREE_CODE (decl) == TEMPLATE_DECL)
4318 {
4319 DECL_TEMPLATE_RESULT (decl)
4320 = build_decl (DECL_SOURCE_LOCATION (decl),
4321 TYPE_DECL, DECL_NAME (decl), type);
4322 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl)) = true;
4323 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4324 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4325 }
4326 }
4327
4328 return TEMPLATE_PARM_DESCENDANTS (index);
4329 }
4330
4331 /* Process information from new template parameter PARM and append it
4332 to the LIST being built. This new parameter is a non-type
4333 parameter iff IS_NON_TYPE is true. This new parameter is a
4334 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4335 is in PARM_LOC. */
4336
4337 tree
4338 process_template_parm (tree list, location_t parm_loc, tree parm,
4339 bool is_non_type, bool is_parameter_pack)
4340 {
4341 tree decl = 0;
4342 int idx = 0;
4343
4344 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4345 tree defval = TREE_PURPOSE (parm);
4346 tree constr = TREE_TYPE (parm);
4347
4348 if (list)
4349 {
4350 tree p = tree_last (list);
4351
4352 if (p && TREE_VALUE (p) != error_mark_node)
4353 {
4354 p = TREE_VALUE (p);
4355 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4356 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4357 else
4358 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4359 }
4360
4361 ++idx;
4362 }
4363
4364 if (is_non_type)
4365 {
4366 parm = TREE_VALUE (parm);
4367
4368 SET_DECL_TEMPLATE_PARM_P (parm);
4369
4370 if (TREE_TYPE (parm) != error_mark_node)
4371 {
4372 /* [temp.param]
4373
4374 The top-level cv-qualifiers on the template-parameter are
4375 ignored when determining its type. */
4376 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4377 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4378 TREE_TYPE (parm) = error_mark_node;
4379 else if (uses_parameter_packs (TREE_TYPE (parm))
4380 && !is_parameter_pack
4381 /* If we're in a nested template parameter list, the template
4382 template parameter could be a parameter pack. */
4383 && processing_template_parmlist == 1)
4384 {
4385 /* This template parameter is not a parameter pack, but it
4386 should be. Complain about "bare" parameter packs. */
4387 check_for_bare_parameter_packs (TREE_TYPE (parm));
4388
4389 /* Recover by calling this a parameter pack. */
4390 is_parameter_pack = true;
4391 }
4392 }
4393
4394 /* A template parameter is not modifiable. */
4395 TREE_CONSTANT (parm) = 1;
4396 TREE_READONLY (parm) = 1;
4397 decl = build_decl (parm_loc,
4398 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4399 TREE_CONSTANT (decl) = 1;
4400 TREE_READONLY (decl) = 1;
4401 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4402 = build_template_parm_index (idx, processing_template_decl,
4403 processing_template_decl,
4404 decl, TREE_TYPE (parm));
4405
4406 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4407 = is_parameter_pack;
4408 }
4409 else
4410 {
4411 tree t;
4412 parm = TREE_VALUE (TREE_VALUE (parm));
4413
4414 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4415 {
4416 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4417 /* This is for distinguishing between real templates and template
4418 template parameters */
4419 TREE_TYPE (parm) = t;
4420 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4421 decl = parm;
4422 }
4423 else
4424 {
4425 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4426 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4427 decl = build_decl (parm_loc,
4428 TYPE_DECL, parm, t);
4429 }
4430
4431 TYPE_NAME (t) = decl;
4432 TYPE_STUB_DECL (t) = decl;
4433 parm = decl;
4434 TEMPLATE_TYPE_PARM_INDEX (t)
4435 = build_template_parm_index (idx, processing_template_decl,
4436 processing_template_decl,
4437 decl, TREE_TYPE (parm));
4438 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4439 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4440 }
4441 DECL_ARTIFICIAL (decl) = 1;
4442 SET_DECL_TEMPLATE_PARM_P (decl);
4443
4444 /* Build requirements for the type/template parameter.
4445 This must be done after SET_DECL_TEMPLATE_PARM_P or
4446 process_template_parm could fail. */
4447 tree reqs = finish_shorthand_constraint (parm, constr);
4448
4449 pushdecl (decl);
4450
4451 /* Build the parameter node linking the parameter declaration,
4452 its default argument (if any), and its constraints (if any). */
4453 parm = build_tree_list (defval, parm);
4454 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4455
4456 return chainon (list, parm);
4457 }
4458
4459 /* The end of a template parameter list has been reached. Process the
4460 tree list into a parameter vector, converting each parameter into a more
4461 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4462 as PARM_DECLs. */
4463
4464 tree
4465 end_template_parm_list (tree parms)
4466 {
4467 int nparms;
4468 tree parm, next;
4469 tree saved_parmlist = make_tree_vec (list_length (parms));
4470
4471 /* Pop the dummy parameter level and add the real one. */
4472 current_template_parms = TREE_CHAIN (current_template_parms);
4473
4474 current_template_parms
4475 = tree_cons (size_int (processing_template_decl),
4476 saved_parmlist, current_template_parms);
4477
4478 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4479 {
4480 next = TREE_CHAIN (parm);
4481 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4482 TREE_CHAIN (parm) = NULL_TREE;
4483 }
4484
4485 --processing_template_parmlist;
4486
4487 return saved_parmlist;
4488 }
4489
4490 // Explicitly indicate the end of the template parameter list. We assume
4491 // that the current template parameters have been constructed and/or
4492 // managed explicitly, as when creating new template template parameters
4493 // from a shorthand constraint.
4494 void
4495 end_template_parm_list ()
4496 {
4497 --processing_template_parmlist;
4498 }
4499
4500 /* end_template_decl is called after a template declaration is seen. */
4501
4502 void
4503 end_template_decl (void)
4504 {
4505 reset_specialization ();
4506
4507 if (! processing_template_decl)
4508 return;
4509
4510 /* This matches the pushlevel in begin_template_parm_list. */
4511 finish_scope ();
4512
4513 --processing_template_decl;
4514 current_template_parms = TREE_CHAIN (current_template_parms);
4515 }
4516
4517 /* Takes a TREE_LIST representing a template parameter and convert it
4518 into an argument suitable to be passed to the type substitution
4519 functions. Note that If the TREE_LIST contains an error_mark
4520 node, the returned argument is error_mark_node. */
4521
4522 tree
4523 template_parm_to_arg (tree t)
4524 {
4525
4526 if (t == NULL_TREE
4527 || TREE_CODE (t) != TREE_LIST)
4528 return t;
4529
4530 if (error_operand_p (TREE_VALUE (t)))
4531 return error_mark_node;
4532
4533 t = TREE_VALUE (t);
4534
4535 if (TREE_CODE (t) == TYPE_DECL
4536 || TREE_CODE (t) == TEMPLATE_DECL)
4537 {
4538 t = TREE_TYPE (t);
4539
4540 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4541 {
4542 /* Turn this argument into a TYPE_ARGUMENT_PACK
4543 with a single element, which expands T. */
4544 tree vec = make_tree_vec (1);
4545 if (CHECKING_P)
4546 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4547
4548 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4549
4550 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4551 SET_ARGUMENT_PACK_ARGS (t, vec);
4552 }
4553 }
4554 else
4555 {
4556 t = DECL_INITIAL (t);
4557
4558 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4559 {
4560 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4561 with a single element, which expands T. */
4562 tree vec = make_tree_vec (1);
4563 if (CHECKING_P)
4564 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4565
4566 t = convert_from_reference (t);
4567 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4568
4569 t = make_node (NONTYPE_ARGUMENT_PACK);
4570 SET_ARGUMENT_PACK_ARGS (t, vec);
4571 }
4572 else
4573 t = convert_from_reference (t);
4574 }
4575 return t;
4576 }
4577
4578 /* Given a single level of template parameters (a TREE_VEC), return it
4579 as a set of template arguments. */
4580
4581 static tree
4582 template_parms_level_to_args (tree parms)
4583 {
4584 tree a = copy_node (parms);
4585 TREE_TYPE (a) = NULL_TREE;
4586 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4587 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4588
4589 if (CHECKING_P)
4590 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4591
4592 return a;
4593 }
4594
4595 /* Given a set of template parameters, return them as a set of template
4596 arguments. The template parameters are represented as a TREE_VEC, in
4597 the form documented in cp-tree.h for template arguments. */
4598
4599 static tree
4600 template_parms_to_args (tree parms)
4601 {
4602 tree header;
4603 tree args = NULL_TREE;
4604 int length = TMPL_PARMS_DEPTH (parms);
4605 int l = length;
4606
4607 /* If there is only one level of template parameters, we do not
4608 create a TREE_VEC of TREE_VECs. Instead, we return a single
4609 TREE_VEC containing the arguments. */
4610 if (length > 1)
4611 args = make_tree_vec (length);
4612
4613 for (header = parms; header; header = TREE_CHAIN (header))
4614 {
4615 tree a = template_parms_level_to_args (TREE_VALUE (header));
4616
4617 if (length > 1)
4618 TREE_VEC_ELT (args, --l) = a;
4619 else
4620 args = a;
4621 }
4622
4623 return args;
4624 }
4625
4626 /* Within the declaration of a template, return the currently active
4627 template parameters as an argument TREE_VEC. */
4628
4629 static tree
4630 current_template_args (void)
4631 {
4632 return template_parms_to_args (current_template_parms);
4633 }
4634
4635 /* Update the declared TYPE by doing any lookups which were thought to be
4636 dependent, but are not now that we know the SCOPE of the declarator. */
4637
4638 tree
4639 maybe_update_decl_type (tree orig_type, tree scope)
4640 {
4641 tree type = orig_type;
4642
4643 if (type == NULL_TREE)
4644 return type;
4645
4646 if (TREE_CODE (orig_type) == TYPE_DECL)
4647 type = TREE_TYPE (type);
4648
4649 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4650 && dependent_type_p (type)
4651 /* Don't bother building up the args in this case. */
4652 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4653 {
4654 /* tsubst in the args corresponding to the template parameters,
4655 including auto if present. Most things will be unchanged, but
4656 make_typename_type and tsubst_qualified_id will resolve
4657 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4658 tree args = current_template_args ();
4659 tree auto_node = type_uses_auto (type);
4660 tree pushed;
4661 if (auto_node)
4662 {
4663 tree auto_vec = make_tree_vec (1);
4664 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4665 args = add_to_template_args (args, auto_vec);
4666 }
4667 pushed = push_scope (scope);
4668 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4669 if (pushed)
4670 pop_scope (scope);
4671 }
4672
4673 if (type == error_mark_node)
4674 return orig_type;
4675
4676 if (TREE_CODE (orig_type) == TYPE_DECL)
4677 {
4678 if (same_type_p (type, TREE_TYPE (orig_type)))
4679 type = orig_type;
4680 else
4681 type = TYPE_NAME (type);
4682 }
4683 return type;
4684 }
4685
4686 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4687 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4688 the new template is a member template. */
4689
4690 static tree
4691 build_template_decl (tree decl, tree parms, bool member_template_p)
4692 {
4693 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4694 SET_DECL_LANGUAGE (tmpl, DECL_LANGUAGE (decl));
4695 DECL_TEMPLATE_PARMS (tmpl) = parms;
4696 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4697 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4698 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4699
4700 return tmpl;
4701 }
4702
4703 struct template_parm_data
4704 {
4705 /* The level of the template parameters we are currently
4706 processing. */
4707 int level;
4708
4709 /* The index of the specialization argument we are currently
4710 processing. */
4711 int current_arg;
4712
4713 /* An array whose size is the number of template parameters. The
4714 elements are nonzero if the parameter has been used in any one
4715 of the arguments processed so far. */
4716 int* parms;
4717
4718 /* An array whose size is the number of template arguments. The
4719 elements are nonzero if the argument makes use of template
4720 parameters of this level. */
4721 int* arg_uses_template_parms;
4722 };
4723
4724 /* Subroutine of push_template_decl used to see if each template
4725 parameter in a partial specialization is used in the explicit
4726 argument list. If T is of the LEVEL given in DATA (which is
4727 treated as a template_parm_data*), then DATA->PARMS is marked
4728 appropriately. */
4729
4730 static int
4731 mark_template_parm (tree t, void* data)
4732 {
4733 int level;
4734 int idx;
4735 struct template_parm_data* tpd = (struct template_parm_data*) data;
4736
4737 template_parm_level_and_index (t, &level, &idx);
4738
4739 if (level == tpd->level)
4740 {
4741 tpd->parms[idx] = 1;
4742 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4743 }
4744
4745 /* In C++17 the type of a non-type argument is a deduced context. */
4746 if (cxx_dialect >= cxx17
4747 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4748 for_each_template_parm (TREE_TYPE (t),
4749 &mark_template_parm,
4750 data,
4751 NULL,
4752 /*include_nondeduced_p=*/false);
4753
4754 /* Return zero so that for_each_template_parm will continue the
4755 traversal of the tree; we want to mark *every* template parm. */
4756 return 0;
4757 }
4758
4759 /* Process the partial specialization DECL. */
4760
4761 static tree
4762 process_partial_specialization (tree decl)
4763 {
4764 tree type = TREE_TYPE (decl);
4765 tree tinfo = get_template_info (decl);
4766 tree maintmpl = TI_TEMPLATE (tinfo);
4767 tree specargs = TI_ARGS (tinfo);
4768 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4769 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4770 tree inner_parms;
4771 tree inst;
4772 int nargs = TREE_VEC_LENGTH (inner_args);
4773 int ntparms;
4774 int i;
4775 bool did_error_intro = false;
4776 struct template_parm_data tpd;
4777 struct template_parm_data tpd2;
4778
4779 gcc_assert (current_template_parms);
4780
4781 /* A concept cannot be specialized. */
4782 if (flag_concepts && variable_concept_p (maintmpl))
4783 {
4784 error ("specialization of variable concept %q#D", maintmpl);
4785 return error_mark_node;
4786 }
4787
4788 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4789 ntparms = TREE_VEC_LENGTH (inner_parms);
4790
4791 /* We check that each of the template parameters given in the
4792 partial specialization is used in the argument list to the
4793 specialization. For example:
4794
4795 template <class T> struct S;
4796 template <class T> struct S<T*>;
4797
4798 The second declaration is OK because `T*' uses the template
4799 parameter T, whereas
4800
4801 template <class T> struct S<int>;
4802
4803 is no good. Even trickier is:
4804
4805 template <class T>
4806 struct S1
4807 {
4808 template <class U>
4809 struct S2;
4810 template <class U>
4811 struct S2<T>;
4812 };
4813
4814 The S2<T> declaration is actually invalid; it is a
4815 full-specialization. Of course,
4816
4817 template <class U>
4818 struct S2<T (*)(U)>;
4819
4820 or some such would have been OK. */
4821 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4822 tpd.parms = XALLOCAVEC (int, ntparms);
4823 memset (tpd.parms, 0, sizeof (int) * ntparms);
4824
4825 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4826 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4827 for (i = 0; i < nargs; ++i)
4828 {
4829 tpd.current_arg = i;
4830 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4831 &mark_template_parm,
4832 &tpd,
4833 NULL,
4834 /*include_nondeduced_p=*/false);
4835 }
4836 for (i = 0; i < ntparms; ++i)
4837 if (tpd.parms[i] == 0)
4838 {
4839 /* One of the template parms was not used in a deduced context in the
4840 specialization. */
4841 if (!did_error_intro)
4842 {
4843 error ("template parameters not deducible in "
4844 "partial specialization:");
4845 did_error_intro = true;
4846 }
4847
4848 inform (input_location, " %qD",
4849 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4850 }
4851
4852 if (did_error_intro)
4853 return error_mark_node;
4854
4855 /* [temp.class.spec]
4856
4857 The argument list of the specialization shall not be identical to
4858 the implicit argument list of the primary template. */
4859 tree main_args
4860 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4861 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
4862 && (!flag_concepts
4863 || !strictly_subsumes (current_template_constraints (),
4864 get_constraints (maintmpl))))
4865 {
4866 if (!flag_concepts)
4867 error ("partial specialization %q+D does not specialize "
4868 "any template arguments; to define the primary template, "
4869 "remove the template argument list", decl);
4870 else
4871 error ("partial specialization %q+D does not specialize any "
4872 "template arguments and is not more constrained than "
4873 "the primary template; to define the primary template, "
4874 "remove the template argument list", decl);
4875 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4876 }
4877
4878 /* A partial specialization that replaces multiple parameters of the
4879 primary template with a pack expansion is less specialized for those
4880 parameters. */
4881 if (nargs < DECL_NTPARMS (maintmpl))
4882 {
4883 error ("partial specialization is not more specialized than the "
4884 "primary template because it replaces multiple parameters "
4885 "with a pack expansion");
4886 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4887 /* Avoid crash in process_partial_specialization. */
4888 return decl;
4889 }
4890
4891 /* If we aren't in a dependent class, we can actually try deduction. */
4892 else if (tpd.level == 1
4893 /* FIXME we should be able to handle a partial specialization of a
4894 partial instantiation, but currently we can't (c++/41727). */
4895 && TMPL_ARGS_DEPTH (specargs) == 1
4896 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
4897 {
4898 auto_diagnostic_group d;
4899 if (permerror (input_location, "partial specialization %qD is not "
4900 "more specialized than", decl))
4901 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
4902 maintmpl);
4903 }
4904
4905 /* [temp.class.spec]
4906
4907 A partially specialized non-type argument expression shall not
4908 involve template parameters of the partial specialization except
4909 when the argument expression is a simple identifier.
4910
4911 The type of a template parameter corresponding to a specialized
4912 non-type argument shall not be dependent on a parameter of the
4913 specialization.
4914
4915 Also, we verify that pack expansions only occur at the
4916 end of the argument list. */
4917 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4918 tpd2.parms = 0;
4919 for (i = 0; i < nargs; ++i)
4920 {
4921 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4922 tree arg = TREE_VEC_ELT (inner_args, i);
4923 tree packed_args = NULL_TREE;
4924 int j, len = 1;
4925
4926 if (ARGUMENT_PACK_P (arg))
4927 {
4928 /* Extract the arguments from the argument pack. We'll be
4929 iterating over these in the following loop. */
4930 packed_args = ARGUMENT_PACK_ARGS (arg);
4931 len = TREE_VEC_LENGTH (packed_args);
4932 }
4933
4934 for (j = 0; j < len; j++)
4935 {
4936 if (packed_args)
4937 /* Get the Jth argument in the parameter pack. */
4938 arg = TREE_VEC_ELT (packed_args, j);
4939
4940 if (PACK_EXPANSION_P (arg))
4941 {
4942 /* Pack expansions must come at the end of the
4943 argument list. */
4944 if ((packed_args && j < len - 1)
4945 || (!packed_args && i < nargs - 1))
4946 {
4947 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4948 error ("parameter pack argument %qE must be at the "
4949 "end of the template argument list", arg);
4950 else
4951 error ("parameter pack argument %qT must be at the "
4952 "end of the template argument list", arg);
4953 }
4954 }
4955
4956 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4957 /* We only care about the pattern. */
4958 arg = PACK_EXPANSION_PATTERN (arg);
4959
4960 if (/* These first two lines are the `non-type' bit. */
4961 !TYPE_P (arg)
4962 && TREE_CODE (arg) != TEMPLATE_DECL
4963 /* This next two lines are the `argument expression is not just a
4964 simple identifier' condition and also the `specialized
4965 non-type argument' bit. */
4966 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4967 && !(REFERENCE_REF_P (arg)
4968 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4969 {
4970 if ((!packed_args && tpd.arg_uses_template_parms[i])
4971 || (packed_args && uses_template_parms (arg)))
4972 error ("template argument %qE involves template parameter(s)",
4973 arg);
4974 else
4975 {
4976 /* Look at the corresponding template parameter,
4977 marking which template parameters its type depends
4978 upon. */
4979 tree type = TREE_TYPE (parm);
4980
4981 if (!tpd2.parms)
4982 {
4983 /* We haven't yet initialized TPD2. Do so now. */
4984 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4985 /* The number of parameters here is the number in the
4986 main template, which, as checked in the assertion
4987 above, is NARGS. */
4988 tpd2.parms = XALLOCAVEC (int, nargs);
4989 tpd2.level =
4990 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4991 }
4992
4993 /* Mark the template parameters. But this time, we're
4994 looking for the template parameters of the main
4995 template, not in the specialization. */
4996 tpd2.current_arg = i;
4997 tpd2.arg_uses_template_parms[i] = 0;
4998 memset (tpd2.parms, 0, sizeof (int) * nargs);
4999 for_each_template_parm (type,
5000 &mark_template_parm,
5001 &tpd2,
5002 NULL,
5003 /*include_nondeduced_p=*/false);
5004
5005 if (tpd2.arg_uses_template_parms [i])
5006 {
5007 /* The type depended on some template parameters.
5008 If they are fully specialized in the
5009 specialization, that's OK. */
5010 int j;
5011 int count = 0;
5012 for (j = 0; j < nargs; ++j)
5013 if (tpd2.parms[j] != 0
5014 && tpd.arg_uses_template_parms [j])
5015 ++count;
5016 if (count != 0)
5017 error_n (input_location, count,
5018 "type %qT of template argument %qE depends "
5019 "on a template parameter",
5020 "type %qT of template argument %qE depends "
5021 "on template parameters",
5022 type,
5023 arg);
5024 }
5025 }
5026 }
5027 }
5028 }
5029
5030 /* We should only get here once. */
5031 if (TREE_CODE (decl) == TYPE_DECL)
5032 gcc_assert (!COMPLETE_TYPE_P (type));
5033
5034 // Build the template decl.
5035 tree tmpl = build_template_decl (decl, current_template_parms,
5036 DECL_MEMBER_TEMPLATE_P (maintmpl));
5037 TREE_TYPE (tmpl) = type;
5038 DECL_TEMPLATE_RESULT (tmpl) = decl;
5039 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5040 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
5041 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
5042
5043 /* Give template template parms a DECL_CONTEXT of the template
5044 for which they are a parameter. */
5045 for (i = 0; i < ntparms; ++i)
5046 {
5047 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
5048 if (TREE_CODE (parm) == TEMPLATE_DECL)
5049 DECL_CONTEXT (parm) = tmpl;
5050 }
5051
5052 if (VAR_P (decl))
5053 /* We didn't register this in check_explicit_specialization so we could
5054 wait until the constraints were set. */
5055 decl = register_specialization (decl, maintmpl, specargs, false, 0);
5056 else
5057 associate_classtype_constraints (type);
5058
5059 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
5060 = tree_cons (specargs, tmpl,
5061 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
5062 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
5063
5064 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
5065 inst = TREE_CHAIN (inst))
5066 {
5067 tree instance = TREE_VALUE (inst);
5068 if (TYPE_P (instance)
5069 ? (COMPLETE_TYPE_P (instance)
5070 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
5071 : DECL_TEMPLATE_INSTANTIATION (instance))
5072 {
5073 tree spec = most_specialized_partial_spec (instance, tf_none);
5074 tree inst_decl = (DECL_P (instance)
5075 ? instance : TYPE_NAME (instance));
5076 if (!spec)
5077 /* OK */;
5078 else if (spec == error_mark_node)
5079 permerror (input_location,
5080 "declaration of %qD ambiguates earlier template "
5081 "instantiation for %qD", decl, inst_decl);
5082 else if (TREE_VALUE (spec) == tmpl)
5083 permerror (input_location,
5084 "partial specialization of %qD after instantiation "
5085 "of %qD", decl, inst_decl);
5086 }
5087 }
5088
5089 return decl;
5090 }
5091
5092 /* PARM is a template parameter of some form; return the corresponding
5093 TEMPLATE_PARM_INDEX. */
5094
5095 static tree
5096 get_template_parm_index (tree parm)
5097 {
5098 if (TREE_CODE (parm) == PARM_DECL
5099 || TREE_CODE (parm) == CONST_DECL)
5100 parm = DECL_INITIAL (parm);
5101 else if (TREE_CODE (parm) == TYPE_DECL
5102 || TREE_CODE (parm) == TEMPLATE_DECL)
5103 parm = TREE_TYPE (parm);
5104 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
5105 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
5106 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
5107 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
5108 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
5109 return parm;
5110 }
5111
5112 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5113 parameter packs used by the template parameter PARM. */
5114
5115 static void
5116 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
5117 {
5118 /* A type parm can't refer to another parm. */
5119 if (TREE_CODE (parm) == TYPE_DECL || parm == error_mark_node)
5120 return;
5121 else if (TREE_CODE (parm) == PARM_DECL)
5122 {
5123 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
5124 ppd, ppd->visited);
5125 return;
5126 }
5127
5128 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
5129
5130 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
5131 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
5132 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
5133 }
5134
5135 /* PARM is a template parameter pack. Return any parameter packs used in
5136 its type or the type of any of its template parameters. If there are
5137 any such packs, it will be instantiated into a fixed template parameter
5138 list by partial instantiation rather than be fully deduced. */
5139
5140 tree
5141 fixed_parameter_pack_p (tree parm)
5142 {
5143 /* This can only be true in a member template. */
5144 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
5145 return NULL_TREE;
5146 /* This can only be true for a parameter pack. */
5147 if (!template_parameter_pack_p (parm))
5148 return NULL_TREE;
5149 /* A type parm can't refer to another parm. */
5150 if (TREE_CODE (parm) == TYPE_DECL)
5151 return NULL_TREE;
5152
5153 tree parameter_packs = NULL_TREE;
5154 struct find_parameter_pack_data ppd;
5155 ppd.parameter_packs = &parameter_packs;
5156 ppd.visited = new hash_set<tree>;
5157 ppd.type_pack_expansion_p = false;
5158
5159 fixed_parameter_pack_p_1 (parm, &ppd);
5160
5161 delete ppd.visited;
5162 return parameter_packs;
5163 }
5164
5165 /* Check that a template declaration's use of default arguments and
5166 parameter packs is not invalid. Here, PARMS are the template
5167 parameters. IS_PRIMARY is true if DECL is the thing declared by
5168 a primary template. IS_PARTIAL is true if DECL is a partial
5169 specialization.
5170
5171 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5172 function template declaration or a friend class template
5173 declaration. In the function case, 1 indicates a declaration, 2
5174 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5175 emitted for extraneous default arguments.
5176
5177 Returns TRUE if there were no errors found, FALSE otherwise. */
5178
5179 bool
5180 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
5181 bool is_partial, int is_friend_decl)
5182 {
5183 const char *msg;
5184 int last_level_to_check;
5185 tree parm_level;
5186 bool no_errors = true;
5187
5188 /* [temp.param]
5189
5190 A default template-argument shall not be specified in a
5191 function template declaration or a function template definition, nor
5192 in the template-parameter-list of the definition of a member of a
5193 class template. */
5194
5195 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5196 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
5197 /* You can't have a function template declaration in a local
5198 scope, nor you can you define a member of a class template in a
5199 local scope. */
5200 return true;
5201
5202 if ((TREE_CODE (decl) == TYPE_DECL
5203 && TREE_TYPE (decl)
5204 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5205 || (TREE_CODE (decl) == FUNCTION_DECL
5206 && LAMBDA_FUNCTION_P (decl)))
5207 /* A lambda doesn't have an explicit declaration; don't complain
5208 about the parms of the enclosing class. */
5209 return true;
5210
5211 if (current_class_type
5212 && !TYPE_BEING_DEFINED (current_class_type)
5213 && DECL_LANG_SPECIFIC (decl)
5214 && DECL_DECLARES_FUNCTION_P (decl)
5215 /* If this is either a friend defined in the scope of the class
5216 or a member function. */
5217 && (DECL_FUNCTION_MEMBER_P (decl)
5218 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5219 : DECL_FRIEND_CONTEXT (decl)
5220 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5221 : false)
5222 /* And, if it was a member function, it really was defined in
5223 the scope of the class. */
5224 && (!DECL_FUNCTION_MEMBER_P (decl)
5225 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5226 /* We already checked these parameters when the template was
5227 declared, so there's no need to do it again now. This function
5228 was defined in class scope, but we're processing its body now
5229 that the class is complete. */
5230 return true;
5231
5232 /* Core issue 226 (C++0x only): the following only applies to class
5233 templates. */
5234 if (is_primary
5235 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5236 {
5237 /* [temp.param]
5238
5239 If a template-parameter has a default template-argument, all
5240 subsequent template-parameters shall have a default
5241 template-argument supplied. */
5242 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5243 {
5244 tree inner_parms = TREE_VALUE (parm_level);
5245 int ntparms = TREE_VEC_LENGTH (inner_parms);
5246 int seen_def_arg_p = 0;
5247 int i;
5248
5249 for (i = 0; i < ntparms; ++i)
5250 {
5251 tree parm = TREE_VEC_ELT (inner_parms, i);
5252
5253 if (parm == error_mark_node)
5254 continue;
5255
5256 if (TREE_PURPOSE (parm))
5257 seen_def_arg_p = 1;
5258 else if (seen_def_arg_p
5259 && !template_parameter_pack_p (TREE_VALUE (parm)))
5260 {
5261 error ("no default argument for %qD", TREE_VALUE (parm));
5262 /* For better subsequent error-recovery, we indicate that
5263 there should have been a default argument. */
5264 TREE_PURPOSE (parm) = error_mark_node;
5265 no_errors = false;
5266 }
5267 else if (!is_partial
5268 && !is_friend_decl
5269 /* Don't complain about an enclosing partial
5270 specialization. */
5271 && parm_level == parms
5272 && TREE_CODE (decl) == TYPE_DECL
5273 && i < ntparms - 1
5274 && template_parameter_pack_p (TREE_VALUE (parm))
5275 /* A fixed parameter pack will be partially
5276 instantiated into a fixed length list. */
5277 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5278 {
5279 /* A primary class template can only have one
5280 parameter pack, at the end of the template
5281 parameter list. */
5282
5283 error ("parameter pack %q+D must be at the end of the"
5284 " template parameter list", TREE_VALUE (parm));
5285
5286 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5287 = error_mark_node;
5288 no_errors = false;
5289 }
5290 }
5291 }
5292 }
5293
5294 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5295 || is_partial
5296 || !is_primary
5297 || is_friend_decl)
5298 /* For an ordinary class template, default template arguments are
5299 allowed at the innermost level, e.g.:
5300 template <class T = int>
5301 struct S {};
5302 but, in a partial specialization, they're not allowed even
5303 there, as we have in [temp.class.spec]:
5304
5305 The template parameter list of a specialization shall not
5306 contain default template argument values.
5307
5308 So, for a partial specialization, or for a function template
5309 (in C++98/C++03), we look at all of them. */
5310 ;
5311 else
5312 /* But, for a primary class template that is not a partial
5313 specialization we look at all template parameters except the
5314 innermost ones. */
5315 parms = TREE_CHAIN (parms);
5316
5317 /* Figure out what error message to issue. */
5318 if (is_friend_decl == 2)
5319 msg = G_("default template arguments may not be used in function template "
5320 "friend re-declaration");
5321 else if (is_friend_decl)
5322 msg = G_("default template arguments may not be used in template "
5323 "friend declarations");
5324 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5325 msg = G_("default template arguments may not be used in function templates "
5326 "without -std=c++11 or -std=gnu++11");
5327 else if (is_partial)
5328 msg = G_("default template arguments may not be used in "
5329 "partial specializations");
5330 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5331 msg = G_("default argument for template parameter for class enclosing %qD");
5332 else
5333 /* Per [temp.param]/9, "A default template-argument shall not be
5334 specified in the template-parameter-lists of the definition of
5335 a member of a class template that appears outside of the member's
5336 class.", thus if we aren't handling a member of a class template
5337 there is no need to examine the parameters. */
5338 return true;
5339
5340 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5341 /* If we're inside a class definition, there's no need to
5342 examine the parameters to the class itself. On the one
5343 hand, they will be checked when the class is defined, and,
5344 on the other, default arguments are valid in things like:
5345 template <class T = double>
5346 struct S { template <class U> void f(U); };
5347 Here the default argument for `S' has no bearing on the
5348 declaration of `f'. */
5349 last_level_to_check = template_class_depth (current_class_type) + 1;
5350 else
5351 /* Check everything. */
5352 last_level_to_check = 0;
5353
5354 for (parm_level = parms;
5355 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5356 parm_level = TREE_CHAIN (parm_level))
5357 {
5358 tree inner_parms = TREE_VALUE (parm_level);
5359 int i;
5360 int ntparms;
5361
5362 ntparms = TREE_VEC_LENGTH (inner_parms);
5363 for (i = 0; i < ntparms; ++i)
5364 {
5365 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5366 continue;
5367
5368 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5369 {
5370 if (msg)
5371 {
5372 no_errors = false;
5373 if (is_friend_decl == 2)
5374 return no_errors;
5375
5376 error (msg, decl);
5377 msg = 0;
5378 }
5379
5380 /* Clear out the default argument so that we are not
5381 confused later. */
5382 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5383 }
5384 }
5385
5386 /* At this point, if we're still interested in issuing messages,
5387 they must apply to classes surrounding the object declared. */
5388 if (msg)
5389 msg = G_("default argument for template parameter for class "
5390 "enclosing %qD");
5391 }
5392
5393 return no_errors;
5394 }
5395
5396 /* Worker for push_template_decl_real, called via
5397 for_each_template_parm. DATA is really an int, indicating the
5398 level of the parameters we are interested in. If T is a template
5399 parameter of that level, return nonzero. */
5400
5401 static int
5402 template_parm_this_level_p (tree t, void* data)
5403 {
5404 int this_level = *(int *)data;
5405 int level;
5406
5407 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5408 level = TEMPLATE_PARM_LEVEL (t);
5409 else
5410 level = TEMPLATE_TYPE_LEVEL (t);
5411 return level == this_level;
5412 }
5413
5414 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5415 DATA is really an int, indicating the innermost outer level of parameters.
5416 If T is a template parameter of that level or further out, return
5417 nonzero. */
5418
5419 static int
5420 template_parm_outer_level (tree t, void *data)
5421 {
5422 int this_level = *(int *)data;
5423 int level;
5424
5425 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5426 level = TEMPLATE_PARM_LEVEL (t);
5427 else
5428 level = TEMPLATE_TYPE_LEVEL (t);
5429 return level <= this_level;
5430 }
5431
5432 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5433 parameters given by current_template_args, or reuses a
5434 previously existing one, if appropriate. Returns the DECL, or an
5435 equivalent one, if it is replaced via a call to duplicate_decls.
5436
5437 If IS_FRIEND is true, DECL is a friend declaration. */
5438
5439 tree
5440 push_template_decl_real (tree decl, bool is_friend)
5441 {
5442 tree tmpl;
5443 tree args;
5444 tree info;
5445 tree ctx;
5446 bool is_primary;
5447 bool is_partial;
5448 int new_template_p = 0;
5449 /* True if the template is a member template, in the sense of
5450 [temp.mem]. */
5451 bool member_template_p = false;
5452
5453 if (decl == error_mark_node || !current_template_parms)
5454 return error_mark_node;
5455
5456 /* See if this is a partial specialization. */
5457 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5458 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5459 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5460 || (VAR_P (decl)
5461 && DECL_LANG_SPECIFIC (decl)
5462 && DECL_TEMPLATE_SPECIALIZATION (decl)
5463 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5464
5465 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5466 is_friend = true;
5467
5468 if (is_friend)
5469 /* For a friend, we want the context of the friend, not
5470 the type of which it is a friend. */
5471 ctx = CP_DECL_CONTEXT (decl);
5472 else if (CP_DECL_CONTEXT (decl)
5473 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5474 /* In the case of a virtual function, we want the class in which
5475 it is defined. */
5476 ctx = CP_DECL_CONTEXT (decl);
5477 else
5478 /* Otherwise, if we're currently defining some class, the DECL
5479 is assumed to be a member of the class. */
5480 ctx = current_scope ();
5481
5482 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5483 ctx = NULL_TREE;
5484
5485 if (!DECL_CONTEXT (decl))
5486 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5487
5488 /* See if this is a primary template. */
5489 if (is_friend && ctx
5490 && uses_template_parms_level (ctx, processing_template_decl))
5491 /* A friend template that specifies a class context, i.e.
5492 template <typename T> friend void A<T>::f();
5493 is not primary. */
5494 is_primary = false;
5495 else if (TREE_CODE (decl) == TYPE_DECL
5496 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5497 is_primary = false;
5498 else
5499 is_primary = template_parm_scope_p ();
5500
5501 if (is_primary)
5502 {
5503 warning (OPT_Wtemplates, "template %qD declared", decl);
5504
5505 if (DECL_CLASS_SCOPE_P (decl))
5506 member_template_p = true;
5507 if (TREE_CODE (decl) == TYPE_DECL
5508 && anon_aggrname_p (DECL_NAME (decl)))
5509 {
5510 error ("template class without a name");
5511 return error_mark_node;
5512 }
5513 else if (TREE_CODE (decl) == FUNCTION_DECL)
5514 {
5515 if (member_template_p)
5516 {
5517 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5518 error ("member template %qD may not have virt-specifiers", decl);
5519 }
5520 if (DECL_DESTRUCTOR_P (decl))
5521 {
5522 /* [temp.mem]
5523
5524 A destructor shall not be a member template. */
5525 error ("destructor %qD declared as member template", decl);
5526 return error_mark_node;
5527 }
5528 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5529 && (!prototype_p (TREE_TYPE (decl))
5530 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5531 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5532 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5533 == void_list_node)))
5534 {
5535 /* [basic.stc.dynamic.allocation]
5536
5537 An allocation function can be a function
5538 template. ... Template allocation functions shall
5539 have two or more parameters. */
5540 error ("invalid template declaration of %qD", decl);
5541 return error_mark_node;
5542 }
5543 }
5544 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5545 && CLASS_TYPE_P (TREE_TYPE (decl)))
5546 {
5547 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5548 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5549 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5550 {
5551 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5552 if (TREE_CODE (t) == TYPE_DECL)
5553 t = TREE_TYPE (t);
5554 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5555 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5556 }
5557 }
5558 else if (TREE_CODE (decl) == TYPE_DECL
5559 && TYPE_DECL_ALIAS_P (decl))
5560 /* alias-declaration */
5561 gcc_assert (!DECL_ARTIFICIAL (decl));
5562 else if (VAR_P (decl))
5563 /* C++14 variable template. */;
5564 else
5565 {
5566 error ("template declaration of %q#D", decl);
5567 return error_mark_node;
5568 }
5569 }
5570
5571 /* Check to see that the rules regarding the use of default
5572 arguments are not being violated. We check args for a friend
5573 functions when we know whether it's a definition, introducing
5574 declaration or re-declaration. */
5575 if (!is_friend || TREE_CODE (decl) != FUNCTION_DECL)
5576 check_default_tmpl_args (decl, current_template_parms,
5577 is_primary, is_partial, is_friend);
5578
5579 /* Ensure that there are no parameter packs in the type of this
5580 declaration that have not been expanded. */
5581 if (TREE_CODE (decl) == FUNCTION_DECL)
5582 {
5583 /* Check each of the arguments individually to see if there are
5584 any bare parameter packs. */
5585 tree type = TREE_TYPE (decl);
5586 tree arg = DECL_ARGUMENTS (decl);
5587 tree argtype = TYPE_ARG_TYPES (type);
5588
5589 while (arg && argtype)
5590 {
5591 if (!DECL_PACK_P (arg)
5592 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5593 {
5594 /* This is a PARM_DECL that contains unexpanded parameter
5595 packs. We have already complained about this in the
5596 check_for_bare_parameter_packs call, so just replace
5597 these types with ERROR_MARK_NODE. */
5598 TREE_TYPE (arg) = error_mark_node;
5599 TREE_VALUE (argtype) = error_mark_node;
5600 }
5601
5602 arg = DECL_CHAIN (arg);
5603 argtype = TREE_CHAIN (argtype);
5604 }
5605
5606 /* Check for bare parameter packs in the return type and the
5607 exception specifiers. */
5608 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5609 /* Errors were already issued, set return type to int
5610 as the frontend doesn't expect error_mark_node as
5611 the return type. */
5612 TREE_TYPE (type) = integer_type_node;
5613 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5614 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5615 }
5616 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5617 && TYPE_DECL_ALIAS_P (decl))
5618 ? DECL_ORIGINAL_TYPE (decl)
5619 : TREE_TYPE (decl)))
5620 {
5621 TREE_TYPE (decl) = error_mark_node;
5622 return error_mark_node;
5623 }
5624
5625 if (is_partial)
5626 return process_partial_specialization (decl);
5627
5628 args = current_template_args ();
5629
5630 if (!ctx
5631 || TREE_CODE (ctx) == FUNCTION_DECL
5632 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5633 || (TREE_CODE (decl) == TYPE_DECL
5634 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5635 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5636 {
5637 if (DECL_LANG_SPECIFIC (decl)
5638 && DECL_TEMPLATE_INFO (decl)
5639 && DECL_TI_TEMPLATE (decl))
5640 tmpl = DECL_TI_TEMPLATE (decl);
5641 /* If DECL is a TYPE_DECL for a class-template, then there won't
5642 be DECL_LANG_SPECIFIC. The information equivalent to
5643 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5644 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5645 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5646 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5647 {
5648 /* Since a template declaration already existed for this
5649 class-type, we must be redeclaring it here. Make sure
5650 that the redeclaration is valid. */
5651 redeclare_class_template (TREE_TYPE (decl),
5652 current_template_parms,
5653 current_template_constraints ());
5654 /* We don't need to create a new TEMPLATE_DECL; just use the
5655 one we already had. */
5656 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5657 }
5658 else
5659 {
5660 tmpl = build_template_decl (decl, current_template_parms,
5661 member_template_p);
5662 new_template_p = 1;
5663
5664 if (DECL_LANG_SPECIFIC (decl)
5665 && DECL_TEMPLATE_SPECIALIZATION (decl))
5666 {
5667 /* A specialization of a member template of a template
5668 class. */
5669 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5670 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5671 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5672 }
5673 }
5674 }
5675 else
5676 {
5677 tree a, t, current, parms;
5678 int i;
5679 tree tinfo = get_template_info (decl);
5680
5681 if (!tinfo)
5682 {
5683 error ("template definition of non-template %q#D", decl);
5684 return error_mark_node;
5685 }
5686
5687 tmpl = TI_TEMPLATE (tinfo);
5688
5689 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5690 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5691 && DECL_TEMPLATE_SPECIALIZATION (decl)
5692 && DECL_MEMBER_TEMPLATE_P (tmpl))
5693 {
5694 tree new_tmpl;
5695
5696 /* The declaration is a specialization of a member
5697 template, declared outside the class. Therefore, the
5698 innermost template arguments will be NULL, so we
5699 replace them with the arguments determined by the
5700 earlier call to check_explicit_specialization. */
5701 args = DECL_TI_ARGS (decl);
5702
5703 new_tmpl
5704 = build_template_decl (decl, current_template_parms,
5705 member_template_p);
5706 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5707 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5708 DECL_TI_TEMPLATE (decl) = new_tmpl;
5709 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5710 DECL_TEMPLATE_INFO (new_tmpl)
5711 = build_template_info (tmpl, args);
5712
5713 register_specialization (new_tmpl,
5714 most_general_template (tmpl),
5715 args,
5716 is_friend, 0);
5717 return decl;
5718 }
5719
5720 /* Make sure the template headers we got make sense. */
5721
5722 parms = DECL_TEMPLATE_PARMS (tmpl);
5723 i = TMPL_PARMS_DEPTH (parms);
5724 if (TMPL_ARGS_DEPTH (args) != i)
5725 {
5726 error ("expected %d levels of template parms for %q#D, got %d",
5727 i, decl, TMPL_ARGS_DEPTH (args));
5728 DECL_INTERFACE_KNOWN (decl) = 1;
5729 return error_mark_node;
5730 }
5731 else
5732 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5733 {
5734 a = TMPL_ARGS_LEVEL (args, i);
5735 t = INNERMOST_TEMPLATE_PARMS (parms);
5736
5737 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5738 {
5739 if (current == decl)
5740 error ("got %d template parameters for %q#D",
5741 TREE_VEC_LENGTH (a), decl);
5742 else
5743 error ("got %d template parameters for %q#T",
5744 TREE_VEC_LENGTH (a), current);
5745 error (" but %d required", TREE_VEC_LENGTH (t));
5746 /* Avoid crash in import_export_decl. */
5747 DECL_INTERFACE_KNOWN (decl) = 1;
5748 return error_mark_node;
5749 }
5750
5751 if (current == decl)
5752 current = ctx;
5753 else if (current == NULL_TREE)
5754 /* Can happen in erroneous input. */
5755 break;
5756 else
5757 current = get_containing_scope (current);
5758 }
5759
5760 /* Check that the parms are used in the appropriate qualifying scopes
5761 in the declarator. */
5762 if (!comp_template_args
5763 (TI_ARGS (tinfo),
5764 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5765 {
5766 error ("template arguments to %qD do not match original "
5767 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
5768 if (!uses_template_parms (TI_ARGS (tinfo)))
5769 inform (input_location, "use %<template<>%> for"
5770 " an explicit specialization");
5771 /* Avoid crash in import_export_decl. */
5772 DECL_INTERFACE_KNOWN (decl) = 1;
5773 return error_mark_node;
5774 }
5775 }
5776
5777 DECL_TEMPLATE_RESULT (tmpl) = decl;
5778 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5779
5780 /* Push template declarations for global functions and types. Note
5781 that we do not try to push a global template friend declared in a
5782 template class; such a thing may well depend on the template
5783 parameters of the class. */
5784 if (new_template_p && !ctx
5785 && !(is_friend && template_class_depth (current_class_type) > 0))
5786 {
5787 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5788 if (tmpl == error_mark_node)
5789 return error_mark_node;
5790
5791 /* Hide template friend classes that haven't been declared yet. */
5792 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5793 {
5794 DECL_ANTICIPATED (tmpl) = 1;
5795 DECL_FRIEND_P (tmpl) = 1;
5796 }
5797 }
5798
5799 if (is_primary)
5800 {
5801 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5802
5803 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5804
5805 /* Give template template parms a DECL_CONTEXT of the template
5806 for which they are a parameter. */
5807 parms = INNERMOST_TEMPLATE_PARMS (parms);
5808 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5809 {
5810 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5811 if (TREE_CODE (parm) == TEMPLATE_DECL)
5812 DECL_CONTEXT (parm) = tmpl;
5813 }
5814
5815 if (TREE_CODE (decl) == TYPE_DECL
5816 && TYPE_DECL_ALIAS_P (decl)
5817 && complex_alias_template_p (tmpl))
5818 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5819 }
5820
5821 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5822 back to its most general template. If TMPL is a specialization,
5823 ARGS may only have the innermost set of arguments. Add the missing
5824 argument levels if necessary. */
5825 if (DECL_TEMPLATE_INFO (tmpl))
5826 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5827
5828 info = build_template_info (tmpl, args);
5829
5830 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5831 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5832 else
5833 {
5834 if (is_primary)
5835 retrofit_lang_decl (decl);
5836 if (DECL_LANG_SPECIFIC (decl))
5837 DECL_TEMPLATE_INFO (decl) = info;
5838 }
5839
5840 if (flag_implicit_templates
5841 && !is_friend
5842 && TREE_PUBLIC (decl)
5843 && VAR_OR_FUNCTION_DECL_P (decl))
5844 /* Set DECL_COMDAT on template instantiations; if we force
5845 them to be emitted by explicit instantiation or -frepo,
5846 mark_needed will tell cgraph to do the right thing. */
5847 DECL_COMDAT (decl) = true;
5848
5849 return DECL_TEMPLATE_RESULT (tmpl);
5850 }
5851
5852 tree
5853 push_template_decl (tree decl)
5854 {
5855 return push_template_decl_real (decl, false);
5856 }
5857
5858 /* FN is an inheriting constructor that inherits from the constructor
5859 template INHERITED; turn FN into a constructor template with a matching
5860 template header. */
5861
5862 tree
5863 add_inherited_template_parms (tree fn, tree inherited)
5864 {
5865 tree inner_parms
5866 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5867 inner_parms = copy_node (inner_parms);
5868 tree parms
5869 = tree_cons (size_int (processing_template_decl + 1),
5870 inner_parms, current_template_parms);
5871 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5872 tree args = template_parms_to_args (parms);
5873 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5874 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5875 DECL_TEMPLATE_RESULT (tmpl) = fn;
5876 DECL_ARTIFICIAL (tmpl) = true;
5877 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5878 return tmpl;
5879 }
5880
5881 /* Called when a class template TYPE is redeclared with the indicated
5882 template PARMS, e.g.:
5883
5884 template <class T> struct S;
5885 template <class T> struct S {}; */
5886
5887 bool
5888 redeclare_class_template (tree type, tree parms, tree cons)
5889 {
5890 tree tmpl;
5891 tree tmpl_parms;
5892 int i;
5893
5894 if (!TYPE_TEMPLATE_INFO (type))
5895 {
5896 error ("%qT is not a template type", type);
5897 return false;
5898 }
5899
5900 tmpl = TYPE_TI_TEMPLATE (type);
5901 if (!PRIMARY_TEMPLATE_P (tmpl))
5902 /* The type is nested in some template class. Nothing to worry
5903 about here; there are no new template parameters for the nested
5904 type. */
5905 return true;
5906
5907 if (!parms)
5908 {
5909 error ("template specifiers not specified in declaration of %qD",
5910 tmpl);
5911 return false;
5912 }
5913
5914 parms = INNERMOST_TEMPLATE_PARMS (parms);
5915 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5916
5917 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5918 {
5919 error_n (input_location, TREE_VEC_LENGTH (parms),
5920 "redeclared with %d template parameter",
5921 "redeclared with %d template parameters",
5922 TREE_VEC_LENGTH (parms));
5923 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5924 "previous declaration %qD used %d template parameter",
5925 "previous declaration %qD used %d template parameters",
5926 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5927 return false;
5928 }
5929
5930 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5931 {
5932 tree tmpl_parm;
5933 tree parm;
5934 tree tmpl_default;
5935 tree parm_default;
5936
5937 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5938 || TREE_VEC_ELT (parms, i) == error_mark_node)
5939 continue;
5940
5941 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5942 if (error_operand_p (tmpl_parm))
5943 return false;
5944
5945 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5946 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5947 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5948
5949 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5950 TEMPLATE_DECL. */
5951 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5952 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5953 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5954 || (TREE_CODE (tmpl_parm) != PARM_DECL
5955 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5956 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5957 || (TREE_CODE (tmpl_parm) == PARM_DECL
5958 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5959 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5960 {
5961 error ("template parameter %q+#D", tmpl_parm);
5962 error ("redeclared here as %q#D", parm);
5963 return false;
5964 }
5965
5966 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5967 {
5968 /* We have in [temp.param]:
5969
5970 A template-parameter may not be given default arguments
5971 by two different declarations in the same scope. */
5972 error_at (input_location, "redefinition of default argument for %q#D", parm);
5973 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5974 "original definition appeared here");
5975 return false;
5976 }
5977
5978 if (parm_default != NULL_TREE)
5979 /* Update the previous template parameters (which are the ones
5980 that will really count) with the new default value. */
5981 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5982 else if (tmpl_default != NULL_TREE)
5983 /* Update the new parameters, too; they'll be used as the
5984 parameters for any members. */
5985 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5986
5987 /* Give each template template parm in this redeclaration a
5988 DECL_CONTEXT of the template for which they are a parameter. */
5989 if (TREE_CODE (parm) == TEMPLATE_DECL)
5990 {
5991 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5992 DECL_CONTEXT (parm) = tmpl;
5993 }
5994
5995 if (TREE_CODE (parm) == TYPE_DECL)
5996 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
5997 }
5998
5999 // Cannot redeclare a class template with a different set of constraints.
6000 if (!equivalent_constraints (get_constraints (tmpl), cons))
6001 {
6002 error_at (input_location, "redeclaration %q#D with different "
6003 "constraints", tmpl);
6004 inform (DECL_SOURCE_LOCATION (tmpl),
6005 "original declaration appeared here");
6006 }
6007
6008 return true;
6009 }
6010
6011 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
6012 to be used when the caller has already checked
6013 (processing_template_decl
6014 && !instantiation_dependent_expression_p (expr)
6015 && potential_constant_expression (expr))
6016 and cleared processing_template_decl. */
6017
6018 tree
6019 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
6020 {
6021 return tsubst_copy_and_build (expr,
6022 /*args=*/NULL_TREE,
6023 complain,
6024 /*in_decl=*/NULL_TREE,
6025 /*function_p=*/false,
6026 /*integral_constant_expression_p=*/true);
6027 }
6028
6029 /* Simplify EXPR if it is a non-dependent expression. Returns the
6030 (possibly simplified) expression. */
6031
6032 tree
6033 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
6034 {
6035 if (expr == NULL_TREE)
6036 return NULL_TREE;
6037
6038 /* If we're in a template, but EXPR isn't value dependent, simplify
6039 it. We're supposed to treat:
6040
6041 template <typename T> void f(T[1 + 1]);
6042 template <typename T> void f(T[2]);
6043
6044 as two declarations of the same function, for example. */
6045 if (processing_template_decl
6046 && is_nondependent_constant_expression (expr))
6047 {
6048 processing_template_decl_sentinel s;
6049 expr = instantiate_non_dependent_expr_internal (expr, complain);
6050 }
6051 return expr;
6052 }
6053
6054 tree
6055 instantiate_non_dependent_expr (tree expr)
6056 {
6057 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
6058 }
6059
6060 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6061 an uninstantiated expression. */
6062
6063 tree
6064 instantiate_non_dependent_or_null (tree expr)
6065 {
6066 if (expr == NULL_TREE)
6067 return NULL_TREE;
6068 if (processing_template_decl)
6069 {
6070 if (!is_nondependent_constant_expression (expr))
6071 expr = NULL_TREE;
6072 else
6073 {
6074 processing_template_decl_sentinel s;
6075 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
6076 }
6077 }
6078 return expr;
6079 }
6080
6081 /* True iff T is a specialization of a variable template. */
6082
6083 bool
6084 variable_template_specialization_p (tree t)
6085 {
6086 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
6087 return false;
6088 tree tmpl = DECL_TI_TEMPLATE (t);
6089 return variable_template_p (tmpl);
6090 }
6091
6092 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6093 template declaration, or a TYPE_DECL for an alias declaration. */
6094
6095 bool
6096 alias_type_or_template_p (tree t)
6097 {
6098 if (t == NULL_TREE)
6099 return false;
6100 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
6101 || (TYPE_P (t)
6102 && TYPE_NAME (t)
6103 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
6104 || DECL_ALIAS_TEMPLATE_P (t));
6105 }
6106
6107 /* Return TRUE iff T is a specialization of an alias template. */
6108
6109 bool
6110 alias_template_specialization_p (const_tree t)
6111 {
6112 /* It's an alias template specialization if it's an alias and its
6113 TYPE_NAME is a specialization of a primary template. */
6114 if (TYPE_ALIAS_P (t))
6115 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
6116 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo));
6117
6118 return false;
6119 }
6120
6121 /* An alias template is complex from a SFINAE perspective if a template-id
6122 using that alias can be ill-formed when the expansion is not, as with
6123 the void_t template. We determine this by checking whether the
6124 expansion for the alias template uses all its template parameters. */
6125
6126 struct uses_all_template_parms_data
6127 {
6128 int level;
6129 bool *seen;
6130 };
6131
6132 static int
6133 uses_all_template_parms_r (tree t, void *data_)
6134 {
6135 struct uses_all_template_parms_data &data
6136 = *(struct uses_all_template_parms_data*)data_;
6137 tree idx = get_template_parm_index (t);
6138
6139 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
6140 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
6141 return 0;
6142 }
6143
6144 static bool
6145 complex_alias_template_p (const_tree tmpl)
6146 {
6147 struct uses_all_template_parms_data data;
6148 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6149 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6150 data.level = TMPL_PARMS_DEPTH (parms);
6151 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
6152 data.seen = XALLOCAVEC (bool, len);
6153 for (int i = 0; i < len; ++i)
6154 data.seen[i] = false;
6155
6156 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
6157 for (int i = 0; i < len; ++i)
6158 if (!data.seen[i])
6159 return true;
6160 return false;
6161 }
6162
6163 /* Return TRUE iff T is a specialization of a complex alias template with
6164 dependent template-arguments. */
6165
6166 bool
6167 dependent_alias_template_spec_p (const_tree t)
6168 {
6169 if (!alias_template_specialization_p (t))
6170 return false;
6171
6172 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
6173 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo)))
6174 return false;
6175
6176 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
6177 if (!any_dependent_template_arguments_p (args))
6178 return false;
6179
6180 return true;
6181 }
6182
6183 /* Return the number of innermost template parameters in TMPL. */
6184
6185 static int
6186 num_innermost_template_parms (tree tmpl)
6187 {
6188 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6189 return TREE_VEC_LENGTH (parms);
6190 }
6191
6192 /* Return either TMPL or another template that it is equivalent to under DR
6193 1286: An alias that just changes the name of a template is equivalent to
6194 the other template. */
6195
6196 static tree
6197 get_underlying_template (tree tmpl)
6198 {
6199 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6200 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6201 {
6202 /* Determine if the alias is equivalent to an underlying template. */
6203 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6204 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6205 if (!tinfo)
6206 break;
6207
6208 tree underlying = TI_TEMPLATE (tinfo);
6209 if (!PRIMARY_TEMPLATE_P (underlying)
6210 || (num_innermost_template_parms (tmpl)
6211 != num_innermost_template_parms (underlying)))
6212 break;
6213
6214 tree alias_args = INNERMOST_TEMPLATE_ARGS
6215 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
6216 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6217 break;
6218
6219 /* Alias is equivalent. Strip it and repeat. */
6220 tmpl = underlying;
6221 }
6222
6223 return tmpl;
6224 }
6225
6226 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6227 must be a reference-to-function or a pointer-to-function type, as specified
6228 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6229 and check that the resulting function has external linkage. */
6230
6231 static tree
6232 convert_nontype_argument_function (tree type, tree expr,
6233 tsubst_flags_t complain)
6234 {
6235 tree fns = expr;
6236 tree fn, fn_no_ptr;
6237 linkage_kind linkage;
6238
6239 fn = instantiate_type (type, fns, tf_none);
6240 if (fn == error_mark_node)
6241 return error_mark_node;
6242
6243 if (value_dependent_expression_p (fn))
6244 goto accept;
6245
6246 fn_no_ptr = strip_fnptr_conv (fn);
6247 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6248 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6249 if (BASELINK_P (fn_no_ptr))
6250 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6251
6252 /* [temp.arg.nontype]/1
6253
6254 A template-argument for a non-type, non-template template-parameter
6255 shall be one of:
6256 [...]
6257 -- the address of an object or function with external [C++11: or
6258 internal] linkage. */
6259
6260 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6261 {
6262 if (complain & tf_error)
6263 {
6264 error ("%qE is not a valid template argument for type %qT",
6265 expr, type);
6266 if (TYPE_PTR_P (type))
6267 inform (input_location, "it must be the address of a function "
6268 "with external linkage");
6269 else
6270 inform (input_location, "it must be the name of a function with "
6271 "external linkage");
6272 }
6273 return NULL_TREE;
6274 }
6275
6276 linkage = decl_linkage (fn_no_ptr);
6277 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6278 {
6279 if (complain & tf_error)
6280 {
6281 if (cxx_dialect >= cxx11)
6282 error ("%qE is not a valid template argument for type %qT "
6283 "because %qD has no linkage",
6284 expr, type, fn_no_ptr);
6285 else
6286 error ("%qE is not a valid template argument for type %qT "
6287 "because %qD does not have external linkage",
6288 expr, type, fn_no_ptr);
6289 }
6290 return NULL_TREE;
6291 }
6292
6293 accept:
6294 if (TYPE_REF_P (type))
6295 {
6296 if (REFERENCE_REF_P (fn))
6297 fn = TREE_OPERAND (fn, 0);
6298 else
6299 fn = build_address (fn);
6300 }
6301 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6302 fn = build_nop (type, fn);
6303
6304 return fn;
6305 }
6306
6307 /* Subroutine of convert_nontype_argument.
6308 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6309 Emit an error otherwise. */
6310
6311 static bool
6312 check_valid_ptrmem_cst_expr (tree type, tree expr,
6313 tsubst_flags_t complain)
6314 {
6315 location_t loc = cp_expr_loc_or_loc (expr, input_location);
6316 tree orig_expr = expr;
6317 STRIP_NOPS (expr);
6318 if (null_ptr_cst_p (expr))
6319 return true;
6320 if (TREE_CODE (expr) == PTRMEM_CST
6321 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6322 PTRMEM_CST_CLASS (expr)))
6323 return true;
6324 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6325 return true;
6326 if (processing_template_decl
6327 && TREE_CODE (expr) == ADDR_EXPR
6328 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6329 return true;
6330 if (complain & tf_error)
6331 {
6332 error_at (loc, "%qE is not a valid template argument for type %qT",
6333 orig_expr, type);
6334 if (TREE_CODE (expr) != PTRMEM_CST)
6335 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6336 else
6337 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6338 }
6339 return false;
6340 }
6341
6342 /* Returns TRUE iff the address of OP is value-dependent.
6343
6344 14.6.2.4 [temp.dep.temp]:
6345 A non-integral non-type template-argument is dependent if its type is
6346 dependent or it has either of the following forms
6347 qualified-id
6348 & qualified-id
6349 and contains a nested-name-specifier which specifies a class-name that
6350 names a dependent type.
6351
6352 We generalize this to just say that the address of a member of a
6353 dependent class is value-dependent; the above doesn't cover the
6354 address of a static data member named with an unqualified-id. */
6355
6356 static bool
6357 has_value_dependent_address (tree op)
6358 {
6359 /* We could use get_inner_reference here, but there's no need;
6360 this is only relevant for template non-type arguments, which
6361 can only be expressed as &id-expression. */
6362 if (DECL_P (op))
6363 {
6364 tree ctx = CP_DECL_CONTEXT (op);
6365 if (TYPE_P (ctx) && dependent_type_p (ctx))
6366 return true;
6367 }
6368
6369 return false;
6370 }
6371
6372 /* The next set of functions are used for providing helpful explanatory
6373 diagnostics for failed overload resolution. Their messages should be
6374 indented by two spaces for consistency with the messages in
6375 call.c */
6376
6377 static int
6378 unify_success (bool /*explain_p*/)
6379 {
6380 return 0;
6381 }
6382
6383 /* Other failure functions should call this one, to provide a single function
6384 for setting a breakpoint on. */
6385
6386 static int
6387 unify_invalid (bool /*explain_p*/)
6388 {
6389 return 1;
6390 }
6391
6392 static int
6393 unify_parameter_deduction_failure (bool explain_p, tree parm)
6394 {
6395 if (explain_p)
6396 inform (input_location,
6397 " couldn't deduce template parameter %qD", parm);
6398 return unify_invalid (explain_p);
6399 }
6400
6401 static int
6402 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6403 {
6404 if (explain_p)
6405 inform (input_location,
6406 " types %qT and %qT have incompatible cv-qualifiers",
6407 parm, arg);
6408 return unify_invalid (explain_p);
6409 }
6410
6411 static int
6412 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6413 {
6414 if (explain_p)
6415 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6416 return unify_invalid (explain_p);
6417 }
6418
6419 static int
6420 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6421 {
6422 if (explain_p)
6423 inform (input_location,
6424 " template parameter %qD is not a parameter pack, but "
6425 "argument %qD is",
6426 parm, arg);
6427 return unify_invalid (explain_p);
6428 }
6429
6430 static int
6431 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6432 {
6433 if (explain_p)
6434 inform (input_location,
6435 " template argument %qE does not match "
6436 "pointer-to-member constant %qE",
6437 arg, parm);
6438 return unify_invalid (explain_p);
6439 }
6440
6441 static int
6442 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6443 {
6444 if (explain_p)
6445 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6446 return unify_invalid (explain_p);
6447 }
6448
6449 static int
6450 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6451 {
6452 if (explain_p)
6453 inform (input_location,
6454 " inconsistent parameter pack deduction with %qT and %qT",
6455 old_arg, new_arg);
6456 return unify_invalid (explain_p);
6457 }
6458
6459 static int
6460 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6461 {
6462 if (explain_p)
6463 {
6464 if (TYPE_P (parm))
6465 inform (input_location,
6466 " deduced conflicting types for parameter %qT (%qT and %qT)",
6467 parm, first, second);
6468 else
6469 inform (input_location,
6470 " deduced conflicting values for non-type parameter "
6471 "%qE (%qE and %qE)", parm, first, second);
6472 }
6473 return unify_invalid (explain_p);
6474 }
6475
6476 static int
6477 unify_vla_arg (bool explain_p, tree arg)
6478 {
6479 if (explain_p)
6480 inform (input_location,
6481 " variable-sized array type %qT is not "
6482 "a valid template argument",
6483 arg);
6484 return unify_invalid (explain_p);
6485 }
6486
6487 static int
6488 unify_method_type_error (bool explain_p, tree arg)
6489 {
6490 if (explain_p)
6491 inform (input_location,
6492 " member function type %qT is not a valid template argument",
6493 arg);
6494 return unify_invalid (explain_p);
6495 }
6496
6497 static int
6498 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6499 {
6500 if (explain_p)
6501 {
6502 if (least_p)
6503 inform_n (input_location, wanted,
6504 " candidate expects at least %d argument, %d provided",
6505 " candidate expects at least %d arguments, %d provided",
6506 wanted, have);
6507 else
6508 inform_n (input_location, wanted,
6509 " candidate expects %d argument, %d provided",
6510 " candidate expects %d arguments, %d provided",
6511 wanted, have);
6512 }
6513 return unify_invalid (explain_p);
6514 }
6515
6516 static int
6517 unify_too_many_arguments (bool explain_p, int have, int wanted)
6518 {
6519 return unify_arity (explain_p, have, wanted);
6520 }
6521
6522 static int
6523 unify_too_few_arguments (bool explain_p, int have, int wanted,
6524 bool least_p = false)
6525 {
6526 return unify_arity (explain_p, have, wanted, least_p);
6527 }
6528
6529 static int
6530 unify_arg_conversion (bool explain_p, tree to_type,
6531 tree from_type, tree arg)
6532 {
6533 if (explain_p)
6534 inform (cp_expr_loc_or_loc (arg, input_location),
6535 " cannot convert %qE (type %qT) to type %qT",
6536 arg, from_type, to_type);
6537 return unify_invalid (explain_p);
6538 }
6539
6540 static int
6541 unify_no_common_base (bool explain_p, enum template_base_result r,
6542 tree parm, tree arg)
6543 {
6544 if (explain_p)
6545 switch (r)
6546 {
6547 case tbr_ambiguous_baseclass:
6548 inform (input_location, " %qT is an ambiguous base class of %qT",
6549 parm, arg);
6550 break;
6551 default:
6552 inform (input_location, " %qT is not derived from %qT", arg, parm);
6553 break;
6554 }
6555 return unify_invalid (explain_p);
6556 }
6557
6558 static int
6559 unify_inconsistent_template_template_parameters (bool explain_p)
6560 {
6561 if (explain_p)
6562 inform (input_location,
6563 " template parameters of a template template argument are "
6564 "inconsistent with other deduced template arguments");
6565 return unify_invalid (explain_p);
6566 }
6567
6568 static int
6569 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6570 {
6571 if (explain_p)
6572 inform (input_location,
6573 " can't deduce a template for %qT from non-template type %qT",
6574 parm, arg);
6575 return unify_invalid (explain_p);
6576 }
6577
6578 static int
6579 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6580 {
6581 if (explain_p)
6582 inform (input_location,
6583 " template argument %qE does not match %qE", arg, parm);
6584 return unify_invalid (explain_p);
6585 }
6586
6587 /* True if T is a C++20 template parameter object to store the argument for a
6588 template parameter of class type. */
6589
6590 bool
6591 template_parm_object_p (const_tree t)
6592 {
6593 return (TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t) && DECL_NAME (t)
6594 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (t)), "_ZTA", 4));
6595 }
6596
6597 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
6598 argument for TYPE, points to an unsuitable object. */
6599
6600 static bool
6601 invalid_tparm_referent_p (tree type, tree expr, tsubst_flags_t complain)
6602 {
6603 switch (TREE_CODE (expr))
6604 {
6605 CASE_CONVERT:
6606 return invalid_tparm_referent_p (type, TREE_OPERAND (expr, 0),
6607 complain);
6608
6609 case TARGET_EXPR:
6610 return invalid_tparm_referent_p (type, TARGET_EXPR_INITIAL (expr),
6611 complain);
6612
6613 case CONSTRUCTOR:
6614 {
6615 unsigned i; tree elt;
6616 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), i, elt)
6617 if (invalid_tparm_referent_p (TREE_TYPE (elt), elt, complain))
6618 return true;
6619 }
6620 break;
6621
6622 case ADDR_EXPR:
6623 {
6624 tree decl = TREE_OPERAND (expr, 0);
6625
6626 if (!VAR_P (decl))
6627 {
6628 if (complain & tf_error)
6629 error ("%qE is not a valid template argument of type %qT "
6630 "because %qE is not a variable", expr, type, decl);
6631 return true;
6632 }
6633 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6634 {
6635 if (complain & tf_error)
6636 error ("%qE is not a valid template argument of type %qT "
6637 "in C++98 because %qD does not have external linkage",
6638 expr, type, decl);
6639 return true;
6640 }
6641 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
6642 && decl_linkage (decl) == lk_none)
6643 {
6644 if (complain & tf_error)
6645 error ("%qE is not a valid template argument of type %qT "
6646 "because %qD has no linkage", expr, type, decl);
6647 return true;
6648 }
6649 /* C++17: For a non-type template-parameter of reference or pointer
6650 type, the value of the constant expression shall not refer to (or
6651 for a pointer type, shall not be the address of):
6652 * a subobject (4.5),
6653 * a temporary object (15.2),
6654 * a string literal (5.13.5),
6655 * the result of a typeid expression (8.2.8), or
6656 * a predefined __func__ variable (11.4.1). */
6657 else if (DECL_ARTIFICIAL (decl))
6658 {
6659 if (complain & tf_error)
6660 error ("the address of %qD is not a valid template argument",
6661 decl);
6662 return true;
6663 }
6664 else if (!same_type_ignoring_top_level_qualifiers_p
6665 (strip_array_types (TREE_TYPE (type)),
6666 strip_array_types (TREE_TYPE (decl))))
6667 {
6668 if (complain & tf_error)
6669 error ("the address of the %qT subobject of %qD is not a "
6670 "valid template argument", TREE_TYPE (type), decl);
6671 return true;
6672 }
6673 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
6674 {
6675 if (complain & tf_error)
6676 error ("the address of %qD is not a valid template argument "
6677 "because it does not have static storage duration",
6678 decl);
6679 return true;
6680 }
6681 }
6682 break;
6683
6684 default:
6685 if (!INDIRECT_TYPE_P (type))
6686 /* We're only concerned about pointers and references here. */;
6687 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6688 /* Null pointer values are OK in C++11. */;
6689 else
6690 {
6691 if (VAR_P (expr))
6692 {
6693 if (complain & tf_error)
6694 error ("%qD is not a valid template argument "
6695 "because %qD is a variable, not the address of "
6696 "a variable", expr, expr);
6697 return true;
6698 }
6699 else
6700 {
6701 if (complain & tf_error)
6702 error ("%qE is not a valid template argument for %qT "
6703 "because it is not the address of a variable",
6704 expr, type);
6705 return true;
6706 }
6707 }
6708 }
6709 return false;
6710
6711 }
6712
6713 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
6714 template argument EXPR. */
6715
6716 static tree
6717 get_template_parm_object (tree expr, tsubst_flags_t complain)
6718 {
6719 if (TREE_CODE (expr) == TARGET_EXPR)
6720 expr = TARGET_EXPR_INITIAL (expr);
6721
6722 if (!TREE_CONSTANT (expr))
6723 {
6724 if ((complain & tf_error)
6725 && require_rvalue_constant_expression (expr))
6726 cxx_constant_value (expr);
6727 return error_mark_node;
6728 }
6729 if (invalid_tparm_referent_p (TREE_TYPE (expr), expr, complain))
6730 return error_mark_node;
6731
6732 tree name = mangle_template_parm_object (expr);
6733 tree decl = get_global_binding (name);
6734 if (decl)
6735 return decl;
6736
6737 tree type = cp_build_qualified_type (TREE_TYPE (expr), TYPE_QUAL_CONST);
6738 decl = create_temporary_var (type);
6739 TREE_STATIC (decl) = true;
6740 DECL_DECLARED_CONSTEXPR_P (decl) = true;
6741 TREE_READONLY (decl) = true;
6742 DECL_NAME (decl) = name;
6743 SET_DECL_ASSEMBLER_NAME (decl, name);
6744 DECL_CONTEXT (decl) = global_namespace;
6745 comdat_linkage (decl);
6746 pushdecl_top_level_and_finish (decl, expr);
6747 return decl;
6748 }
6749
6750 /* Attempt to convert the non-type template parameter EXPR to the
6751 indicated TYPE. If the conversion is successful, return the
6752 converted value. If the conversion is unsuccessful, return
6753 NULL_TREE if we issued an error message, or error_mark_node if we
6754 did not. We issue error messages for out-and-out bad template
6755 parameters, but not simply because the conversion failed, since we
6756 might be just trying to do argument deduction. Both TYPE and EXPR
6757 must be non-dependent.
6758
6759 The conversion follows the special rules described in
6760 [temp.arg.nontype], and it is much more strict than an implicit
6761 conversion.
6762
6763 This function is called twice for each template argument (see
6764 lookup_template_class for a more accurate description of this
6765 problem). This means that we need to handle expressions which
6766 are not valid in a C++ source, but can be created from the
6767 first call (for instance, casts to perform conversions). These
6768 hacks can go away after we fix the double coercion problem. */
6769
6770 static tree
6771 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6772 {
6773 tree expr_type;
6774 location_t loc = cp_expr_loc_or_loc (expr, input_location);
6775
6776 /* Detect immediately string literals as invalid non-type argument.
6777 This special-case is not needed for correctness (we would easily
6778 catch this later), but only to provide better diagnostic for this
6779 common user mistake. As suggested by DR 100, we do not mention
6780 linkage issues in the diagnostic as this is not the point. */
6781 if (TREE_CODE (expr) == STRING_CST && !CLASS_TYPE_P (type))
6782 {
6783 if (complain & tf_error)
6784 error ("%qE is not a valid template argument for type %qT "
6785 "because string literals can never be used in this context",
6786 expr, type);
6787 return NULL_TREE;
6788 }
6789
6790 /* Add the ADDR_EXPR now for the benefit of
6791 value_dependent_expression_p. */
6792 if (TYPE_PTROBV_P (type)
6793 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6794 {
6795 expr = decay_conversion (expr, complain);
6796 if (expr == error_mark_node)
6797 return error_mark_node;
6798 }
6799
6800 /* If we are in a template, EXPR may be non-dependent, but still
6801 have a syntactic, rather than semantic, form. For example, EXPR
6802 might be a SCOPE_REF, rather than the VAR_DECL to which the
6803 SCOPE_REF refers. Preserving the qualifying scope is necessary
6804 so that access checking can be performed when the template is
6805 instantiated -- but here we need the resolved form so that we can
6806 convert the argument. */
6807 bool non_dep = false;
6808 if (TYPE_REF_OBJ_P (type)
6809 && has_value_dependent_address (expr))
6810 /* If we want the address and it's value-dependent, don't fold. */;
6811 else if (processing_template_decl
6812 && is_nondependent_constant_expression (expr))
6813 non_dep = true;
6814 if (error_operand_p (expr))
6815 return error_mark_node;
6816 expr_type = TREE_TYPE (expr);
6817
6818 /* If the argument is non-dependent, perform any conversions in
6819 non-dependent context as well. */
6820 processing_template_decl_sentinel s (non_dep);
6821 if (non_dep)
6822 expr = instantiate_non_dependent_expr_internal (expr, complain);
6823
6824 if (value_dependent_expression_p (expr))
6825 expr = canonicalize_expr_argument (expr, complain);
6826
6827 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6828 to a non-type argument of "nullptr". */
6829 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
6830 expr = fold_simple (convert (type, expr));
6831
6832 /* In C++11, integral or enumeration non-type template arguments can be
6833 arbitrary constant expressions. Pointer and pointer to
6834 member arguments can be general constant expressions that evaluate
6835 to a null value, but otherwise still need to be of a specific form. */
6836 if (cxx_dialect >= cxx11)
6837 {
6838 if (TREE_CODE (expr) == PTRMEM_CST)
6839 /* A PTRMEM_CST is already constant, and a valid template
6840 argument for a parameter of pointer to member type, we just want
6841 to leave it in that form rather than lower it to a
6842 CONSTRUCTOR. */;
6843 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6844 || cxx_dialect >= cxx17)
6845 {
6846 /* C++17: A template-argument for a non-type template-parameter shall
6847 be a converted constant expression (8.20) of the type of the
6848 template-parameter. */
6849 expr = build_converted_constant_expr (type, expr, complain);
6850 if (expr == error_mark_node)
6851 /* Make sure we return NULL_TREE only if we have really issued
6852 an error, as described above. */
6853 return (complain & tf_error) ? NULL_TREE : error_mark_node;
6854 expr = maybe_constant_value (expr);
6855 expr = convert_from_reference (expr);
6856 }
6857 else if (TYPE_PTR_OR_PTRMEM_P (type))
6858 {
6859 tree folded = maybe_constant_value (expr);
6860 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6861 : null_member_pointer_value_p (folded))
6862 expr = folded;
6863 }
6864 }
6865
6866 if (TYPE_REF_P (type))
6867 expr = mark_lvalue_use (expr);
6868 else
6869 expr = mark_rvalue_use (expr);
6870
6871 /* HACK: Due to double coercion, we can get a
6872 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6873 which is the tree that we built on the first call (see
6874 below when coercing to reference to object or to reference to
6875 function). We just strip everything and get to the arg.
6876 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6877 for examples. */
6878 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6879 {
6880 tree probe_type, probe = expr;
6881 if (REFERENCE_REF_P (probe))
6882 probe = TREE_OPERAND (probe, 0);
6883 probe_type = TREE_TYPE (probe);
6884 if (TREE_CODE (probe) == NOP_EXPR)
6885 {
6886 /* ??? Maybe we could use convert_from_reference here, but we
6887 would need to relax its constraints because the NOP_EXPR
6888 could actually change the type to something more cv-qualified,
6889 and this is not folded by convert_from_reference. */
6890 tree addr = TREE_OPERAND (probe, 0);
6891 if (TYPE_REF_P (probe_type)
6892 && TREE_CODE (addr) == ADDR_EXPR
6893 && TYPE_PTR_P (TREE_TYPE (addr))
6894 && (same_type_ignoring_top_level_qualifiers_p
6895 (TREE_TYPE (probe_type),
6896 TREE_TYPE (TREE_TYPE (addr)))))
6897 {
6898 expr = TREE_OPERAND (addr, 0);
6899 expr_type = TREE_TYPE (probe_type);
6900 }
6901 }
6902 }
6903
6904 /* [temp.arg.nontype]/5, bullet 1
6905
6906 For a non-type template-parameter of integral or enumeration type,
6907 integral promotions (_conv.prom_) and integral conversions
6908 (_conv.integral_) are applied. */
6909 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6910 {
6911 if (cxx_dialect < cxx11)
6912 {
6913 tree t = build_converted_constant_expr (type, expr, complain);
6914 t = maybe_constant_value (t);
6915 if (t != error_mark_node)
6916 expr = t;
6917 }
6918
6919 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6920 return error_mark_node;
6921
6922 /* Notice that there are constant expressions like '4 % 0' which
6923 do not fold into integer constants. */
6924 if (TREE_CODE (expr) != INTEGER_CST
6925 && !value_dependent_expression_p (expr))
6926 {
6927 if (complain & tf_error)
6928 {
6929 int errs = errorcount, warns = warningcount + werrorcount;
6930 if (!require_potential_constant_expression (expr))
6931 expr = error_mark_node;
6932 else
6933 expr = cxx_constant_value (expr);
6934 if (errorcount > errs || warningcount + werrorcount > warns)
6935 inform (loc, "in template argument for type %qT", type);
6936 if (expr == error_mark_node)
6937 return NULL_TREE;
6938 /* else cxx_constant_value complained but gave us
6939 a real constant, so go ahead. */
6940 if (TREE_CODE (expr) != INTEGER_CST)
6941 {
6942 /* Some assemble time constant expressions like
6943 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
6944 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
6945 as we can emit them into .rodata initializers of
6946 variables, yet they can't fold into an INTEGER_CST at
6947 compile time. Refuse them here. */
6948 gcc_checking_assert (reduced_constant_expression_p (expr));
6949 error_at (loc, "template argument %qE for type %qT not "
6950 "a constant integer", expr, type);
6951 return NULL_TREE;
6952 }
6953 }
6954 else
6955 return NULL_TREE;
6956 }
6957
6958 /* Avoid typedef problems. */
6959 if (TREE_TYPE (expr) != type)
6960 expr = fold_convert (type, expr);
6961 }
6962 /* [temp.arg.nontype]/5, bullet 2
6963
6964 For a non-type template-parameter of type pointer to object,
6965 qualification conversions (_conv.qual_) and the array-to-pointer
6966 conversion (_conv.array_) are applied. */
6967 else if (TYPE_PTROBV_P (type))
6968 {
6969 tree decayed = expr;
6970
6971 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6972 decay_conversion or an explicit cast. If it's a problematic cast,
6973 we'll complain about it below. */
6974 if (TREE_CODE (expr) == NOP_EXPR)
6975 {
6976 tree probe = expr;
6977 STRIP_NOPS (probe);
6978 if (TREE_CODE (probe) == ADDR_EXPR
6979 && TYPE_PTR_P (TREE_TYPE (probe)))
6980 {
6981 expr = probe;
6982 expr_type = TREE_TYPE (expr);
6983 }
6984 }
6985
6986 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6987
6988 A template-argument for a non-type, non-template template-parameter
6989 shall be one of: [...]
6990
6991 -- the name of a non-type template-parameter;
6992 -- the address of an object or function with external linkage, [...]
6993 expressed as "& id-expression" where the & is optional if the name
6994 refers to a function or array, or if the corresponding
6995 template-parameter is a reference.
6996
6997 Here, we do not care about functions, as they are invalid anyway
6998 for a parameter of type pointer-to-object. */
6999
7000 if (value_dependent_expression_p (expr))
7001 /* Non-type template parameters are OK. */
7002 ;
7003 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7004 /* Null pointer values are OK in C++11. */;
7005 else if (TREE_CODE (expr) != ADDR_EXPR
7006 && !INDIRECT_TYPE_P (expr_type))
7007 /* Other values, like integer constants, might be valid
7008 non-type arguments of some other type. */
7009 return error_mark_node;
7010 else if (invalid_tparm_referent_p (type, expr, complain))
7011 return NULL_TREE;
7012
7013 expr = decayed;
7014
7015 expr = perform_qualification_conversions (type, expr);
7016 if (expr == error_mark_node)
7017 return error_mark_node;
7018 }
7019 /* [temp.arg.nontype]/5, bullet 3
7020
7021 For a non-type template-parameter of type reference to object, no
7022 conversions apply. The type referred to by the reference may be more
7023 cv-qualified than the (otherwise identical) type of the
7024 template-argument. The template-parameter is bound directly to the
7025 template-argument, which must be an lvalue. */
7026 else if (TYPE_REF_OBJ_P (type))
7027 {
7028 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
7029 expr_type))
7030 return error_mark_node;
7031
7032 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
7033 {
7034 if (complain & tf_error)
7035 error ("%qE is not a valid template argument for type %qT "
7036 "because of conflicts in cv-qualification", expr, type);
7037 return NULL_TREE;
7038 }
7039
7040 if (!lvalue_p (expr))
7041 {
7042 if (complain & tf_error)
7043 error ("%qE is not a valid template argument for type %qT "
7044 "because it is not an lvalue", expr, type);
7045 return NULL_TREE;
7046 }
7047
7048 /* [temp.arg.nontype]/1
7049
7050 A template-argument for a non-type, non-template template-parameter
7051 shall be one of: [...]
7052
7053 -- the address of an object or function with external linkage. */
7054 if (INDIRECT_REF_P (expr)
7055 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
7056 {
7057 expr = TREE_OPERAND (expr, 0);
7058 if (DECL_P (expr))
7059 {
7060 if (complain & tf_error)
7061 error ("%q#D is not a valid template argument for type %qT "
7062 "because a reference variable does not have a constant "
7063 "address", expr, type);
7064 return NULL_TREE;
7065 }
7066 }
7067
7068 if (TYPE_REF_OBJ_P (TREE_TYPE (expr))
7069 && value_dependent_expression_p (expr))
7070 /* OK, dependent reference. We don't want to ask whether a DECL is
7071 itself value-dependent, since what we want here is its address. */;
7072 else
7073 {
7074 expr = build_address (expr);
7075
7076 if (invalid_tparm_referent_p (type, expr, complain))
7077 return NULL_TREE;
7078 }
7079
7080 if (!same_type_p (type, TREE_TYPE (expr)))
7081 expr = build_nop (type, expr);
7082 }
7083 /* [temp.arg.nontype]/5, bullet 4
7084
7085 For a non-type template-parameter of type pointer to function, only
7086 the function-to-pointer conversion (_conv.func_) is applied. If the
7087 template-argument represents a set of overloaded functions (or a
7088 pointer to such), the matching function is selected from the set
7089 (_over.over_). */
7090 else if (TYPE_PTRFN_P (type))
7091 {
7092 /* If the argument is a template-id, we might not have enough
7093 context information to decay the pointer. */
7094 if (!type_unknown_p (expr_type))
7095 {
7096 expr = decay_conversion (expr, complain);
7097 if (expr == error_mark_node)
7098 return error_mark_node;
7099 }
7100
7101 if (cxx_dialect >= cxx11 && integer_zerop (expr))
7102 /* Null pointer values are OK in C++11. */
7103 return perform_qualification_conversions (type, expr);
7104
7105 expr = convert_nontype_argument_function (type, expr, complain);
7106 if (!expr || expr == error_mark_node)
7107 return expr;
7108 }
7109 /* [temp.arg.nontype]/5, bullet 5
7110
7111 For a non-type template-parameter of type reference to function, no
7112 conversions apply. If the template-argument represents a set of
7113 overloaded functions, the matching function is selected from the set
7114 (_over.over_). */
7115 else if (TYPE_REFFN_P (type))
7116 {
7117 if (TREE_CODE (expr) == ADDR_EXPR)
7118 {
7119 if (complain & tf_error)
7120 {
7121 error ("%qE is not a valid template argument for type %qT "
7122 "because it is a pointer", expr, type);
7123 inform (input_location, "try using %qE instead",
7124 TREE_OPERAND (expr, 0));
7125 }
7126 return NULL_TREE;
7127 }
7128
7129 expr = convert_nontype_argument_function (type, expr, complain);
7130 if (!expr || expr == error_mark_node)
7131 return expr;
7132 }
7133 /* [temp.arg.nontype]/5, bullet 6
7134
7135 For a non-type template-parameter of type pointer to member function,
7136 no conversions apply. If the template-argument represents a set of
7137 overloaded member functions, the matching member function is selected
7138 from the set (_over.over_). */
7139 else if (TYPE_PTRMEMFUNC_P (type))
7140 {
7141 expr = instantiate_type (type, expr, tf_none);
7142 if (expr == error_mark_node)
7143 return error_mark_node;
7144
7145 /* [temp.arg.nontype] bullet 1 says the pointer to member
7146 expression must be a pointer-to-member constant. */
7147 if (!value_dependent_expression_p (expr)
7148 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7149 return NULL_TREE;
7150
7151 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7152 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7153 if (fnptr_conv_p (type, TREE_TYPE (expr)))
7154 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
7155 }
7156 /* [temp.arg.nontype]/5, bullet 7
7157
7158 For a non-type template-parameter of type pointer to data member,
7159 qualification conversions (_conv.qual_) are applied. */
7160 else if (TYPE_PTRDATAMEM_P (type))
7161 {
7162 /* [temp.arg.nontype] bullet 1 says the pointer to member
7163 expression must be a pointer-to-member constant. */
7164 if (!value_dependent_expression_p (expr)
7165 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7166 return NULL_TREE;
7167
7168 expr = perform_qualification_conversions (type, expr);
7169 if (expr == error_mark_node)
7170 return expr;
7171 }
7172 else if (NULLPTR_TYPE_P (type))
7173 {
7174 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
7175 {
7176 if (complain & tf_error)
7177 error ("%qE is not a valid template argument for type %qT "
7178 "because it is of type %qT", expr, type, TREE_TYPE (expr));
7179 return NULL_TREE;
7180 }
7181 return expr;
7182 }
7183 else if (CLASS_TYPE_P (type))
7184 {
7185 /* Replace the argument with a reference to the corresponding template
7186 parameter object. */
7187 expr = get_template_parm_object (expr, complain);
7188 if (expr == error_mark_node)
7189 return NULL_TREE;
7190 }
7191 /* A template non-type parameter must be one of the above. */
7192 else
7193 gcc_unreachable ();
7194
7195 /* Sanity check: did we actually convert the argument to the
7196 right type? */
7197 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7198 (type, TREE_TYPE (expr)));
7199 return convert_from_reference (expr);
7200 }
7201
7202 /* Subroutine of coerce_template_template_parms, which returns 1 if
7203 PARM_PARM and ARG_PARM match using the rule for the template
7204 parameters of template template parameters. Both PARM and ARG are
7205 template parameters; the rest of the arguments are the same as for
7206 coerce_template_template_parms.
7207 */
7208 static int
7209 coerce_template_template_parm (tree parm,
7210 tree arg,
7211 tsubst_flags_t complain,
7212 tree in_decl,
7213 tree outer_args)
7214 {
7215 if (arg == NULL_TREE || error_operand_p (arg)
7216 || parm == NULL_TREE || error_operand_p (parm))
7217 return 0;
7218
7219 if (TREE_CODE (arg) != TREE_CODE (parm))
7220 return 0;
7221
7222 switch (TREE_CODE (parm))
7223 {
7224 case TEMPLATE_DECL:
7225 /* We encounter instantiations of templates like
7226 template <template <template <class> class> class TT>
7227 class C; */
7228 {
7229 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7230 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7231
7232 if (!coerce_template_template_parms
7233 (parmparm, argparm, complain, in_decl, outer_args))
7234 return 0;
7235 }
7236 /* Fall through. */
7237
7238 case TYPE_DECL:
7239 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
7240 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7241 /* Argument is a parameter pack but parameter is not. */
7242 return 0;
7243 break;
7244
7245 case PARM_DECL:
7246 /* The tsubst call is used to handle cases such as
7247
7248 template <int> class C {};
7249 template <class T, template <T> class TT> class D {};
7250 D<int, C> d;
7251
7252 i.e. the parameter list of TT depends on earlier parameters. */
7253 if (!uses_template_parms (TREE_TYPE (arg)))
7254 {
7255 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
7256 if (!uses_template_parms (t)
7257 && !same_type_p (t, TREE_TYPE (arg)))
7258 return 0;
7259 }
7260
7261 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
7262 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7263 /* Argument is a parameter pack but parameter is not. */
7264 return 0;
7265
7266 break;
7267
7268 default:
7269 gcc_unreachable ();
7270 }
7271
7272 return 1;
7273 }
7274
7275 /* Coerce template argument list ARGLIST for use with template
7276 template-parameter TEMPL. */
7277
7278 static tree
7279 coerce_template_args_for_ttp (tree templ, tree arglist,
7280 tsubst_flags_t complain)
7281 {
7282 /* Consider an example where a template template parameter declared as
7283
7284 template <class T, class U = std::allocator<T> > class TT
7285
7286 The template parameter level of T and U are one level larger than
7287 of TT. To proper process the default argument of U, say when an
7288 instantiation `TT<int>' is seen, we need to build the full
7289 arguments containing {int} as the innermost level. Outer levels,
7290 available when not appearing as default template argument, can be
7291 obtained from the arguments of the enclosing template.
7292
7293 Suppose that TT is later substituted with std::vector. The above
7294 instantiation is `TT<int, std::allocator<T> >' with TT at
7295 level 1, and T at level 2, while the template arguments at level 1
7296 becomes {std::vector} and the inner level 2 is {int}. */
7297
7298 tree outer = DECL_CONTEXT (templ);
7299 if (outer)
7300 {
7301 if (DECL_TEMPLATE_SPECIALIZATION (outer))
7302 /* We want arguments for the partial specialization, not arguments for
7303 the primary template. */
7304 outer = template_parms_to_args (DECL_TEMPLATE_PARMS (outer));
7305 else
7306 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7307 }
7308 else if (current_template_parms)
7309 {
7310 /* This is an argument of the current template, so we haven't set
7311 DECL_CONTEXT yet. */
7312 tree relevant_template_parms;
7313
7314 /* Parameter levels that are greater than the level of the given
7315 template template parm are irrelevant. */
7316 relevant_template_parms = current_template_parms;
7317 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7318 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7319 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7320
7321 outer = template_parms_to_args (relevant_template_parms);
7322 }
7323
7324 if (outer)
7325 arglist = add_to_template_args (outer, arglist);
7326
7327 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7328 return coerce_template_parms (parmlist, arglist, templ,
7329 complain,
7330 /*require_all_args=*/true,
7331 /*use_default_args=*/true);
7332 }
7333
7334 /* A cache of template template parameters with match-all default
7335 arguments. */
7336 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7337 static void
7338 store_defaulted_ttp (tree v, tree t)
7339 {
7340 if (!defaulted_ttp_cache)
7341 defaulted_ttp_cache = hash_map<tree,tree>::create_ggc (13);
7342 defaulted_ttp_cache->put (v, t);
7343 }
7344 static tree
7345 lookup_defaulted_ttp (tree v)
7346 {
7347 if (defaulted_ttp_cache)
7348 if (tree *p = defaulted_ttp_cache->get (v))
7349 return *p;
7350 return NULL_TREE;
7351 }
7352
7353 /* T is a bound template template-parameter. Copy its arguments into default
7354 arguments of the template template-parameter's template parameters. */
7355
7356 static tree
7357 add_defaults_to_ttp (tree otmpl)
7358 {
7359 if (tree c = lookup_defaulted_ttp (otmpl))
7360 return c;
7361
7362 tree ntmpl = copy_node (otmpl);
7363
7364 tree ntype = copy_node (TREE_TYPE (otmpl));
7365 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7366 TYPE_MAIN_VARIANT (ntype) = ntype;
7367 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7368 TYPE_NAME (ntype) = ntmpl;
7369 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7370
7371 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7372 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7373 TEMPLATE_PARM_DECL (idx) = ntmpl;
7374 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7375
7376 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7377 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7378 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7379 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7380 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7381 {
7382 tree o = TREE_VEC_ELT (vec, i);
7383 if (!template_parameter_pack_p (TREE_VALUE (o)))
7384 {
7385 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7386 TREE_PURPOSE (n) = any_targ_node;
7387 }
7388 }
7389
7390 store_defaulted_ttp (otmpl, ntmpl);
7391 return ntmpl;
7392 }
7393
7394 /* ARG is a bound potential template template-argument, and PARGS is a list
7395 of arguments for the corresponding template template-parameter. Adjust
7396 PARGS as appropriate for application to ARG's template, and if ARG is a
7397 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7398 arguments to the template template parameter. */
7399
7400 static tree
7401 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7402 {
7403 ++processing_template_decl;
7404 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7405 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7406 {
7407 /* When comparing two template template-parameters in partial ordering,
7408 rewrite the one currently being used as an argument to have default
7409 arguments for all parameters. */
7410 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7411 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7412 if (pargs != error_mark_node)
7413 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7414 TYPE_TI_ARGS (arg));
7415 }
7416 else
7417 {
7418 tree aparms
7419 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7420 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7421 /*require_all*/true,
7422 /*use_default*/true);
7423 }
7424 --processing_template_decl;
7425 return pargs;
7426 }
7427
7428 /* Subroutine of unify for the case when PARM is a
7429 BOUND_TEMPLATE_TEMPLATE_PARM. */
7430
7431 static int
7432 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7433 bool explain_p)
7434 {
7435 tree parmvec = TYPE_TI_ARGS (parm);
7436 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7437
7438 /* The template template parm might be variadic and the argument
7439 not, so flatten both argument lists. */
7440 parmvec = expand_template_argument_pack (parmvec);
7441 argvec = expand_template_argument_pack (argvec);
7442
7443 if (flag_new_ttp)
7444 {
7445 /* In keeping with P0522R0, adjust P's template arguments
7446 to apply to A's template; then flatten it again. */
7447 tree nparmvec = parmvec;
7448 nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7449 nparmvec = expand_template_argument_pack (nparmvec);
7450
7451 if (unify (tparms, targs, nparmvec, argvec,
7452 UNIFY_ALLOW_NONE, explain_p))
7453 return 1;
7454
7455 /* If the P0522 adjustment eliminated a pack expansion, deduce
7456 empty packs. */
7457 if (flag_new_ttp
7458 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7459 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7460 DEDUCE_EXACT, /*sub*/true, explain_p))
7461 return 1;
7462 }
7463 else
7464 {
7465 /* Deduce arguments T, i from TT<T> or TT<i>.
7466 We check each element of PARMVEC and ARGVEC individually
7467 rather than the whole TREE_VEC since they can have
7468 different number of elements, which is allowed under N2555. */
7469
7470 int len = TREE_VEC_LENGTH (parmvec);
7471
7472 /* Check if the parameters end in a pack, making them
7473 variadic. */
7474 int parm_variadic_p = 0;
7475 if (len > 0
7476 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7477 parm_variadic_p = 1;
7478
7479 for (int i = 0; i < len - parm_variadic_p; ++i)
7480 /* If the template argument list of P contains a pack
7481 expansion that is not the last template argument, the
7482 entire template argument list is a non-deduced
7483 context. */
7484 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7485 return unify_success (explain_p);
7486
7487 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7488 return unify_too_few_arguments (explain_p,
7489 TREE_VEC_LENGTH (argvec), len);
7490
7491 for (int i = 0; i < len - parm_variadic_p; ++i)
7492 if (unify (tparms, targs,
7493 TREE_VEC_ELT (parmvec, i),
7494 TREE_VEC_ELT (argvec, i),
7495 UNIFY_ALLOW_NONE, explain_p))
7496 return 1;
7497
7498 if (parm_variadic_p
7499 && unify_pack_expansion (tparms, targs,
7500 parmvec, argvec,
7501 DEDUCE_EXACT,
7502 /*subr=*/true, explain_p))
7503 return 1;
7504 }
7505
7506 return 0;
7507 }
7508
7509 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7510 template template parameters. Both PARM_PARMS and ARG_PARMS are
7511 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7512 or PARM_DECL.
7513
7514 Consider the example:
7515 template <class T> class A;
7516 template<template <class U> class TT> class B;
7517
7518 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7519 the parameters to A, and OUTER_ARGS contains A. */
7520
7521 static int
7522 coerce_template_template_parms (tree parm_parms,
7523 tree arg_parms,
7524 tsubst_flags_t complain,
7525 tree in_decl,
7526 tree outer_args)
7527 {
7528 int nparms, nargs, i;
7529 tree parm, arg;
7530 int variadic_p = 0;
7531
7532 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7533 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7534
7535 nparms = TREE_VEC_LENGTH (parm_parms);
7536 nargs = TREE_VEC_LENGTH (arg_parms);
7537
7538 if (flag_new_ttp)
7539 {
7540 /* P0522R0: A template template-parameter P is at least as specialized as
7541 a template template-argument A if, given the following rewrite to two
7542 function templates, the function template corresponding to P is at
7543 least as specialized as the function template corresponding to A
7544 according to the partial ordering rules for function templates
7545 ([temp.func.order]). Given an invented class template X with the
7546 template parameter list of A (including default arguments):
7547
7548 * Each of the two function templates has the same template parameters,
7549 respectively, as P or A.
7550
7551 * Each function template has a single function parameter whose type is
7552 a specialization of X with template arguments corresponding to the
7553 template parameters from the respective function template where, for
7554 each template parameter PP in the template parameter list of the
7555 function template, a corresponding template argument AA is formed. If
7556 PP declares a parameter pack, then AA is the pack expansion
7557 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7558
7559 If the rewrite produces an invalid type, then P is not at least as
7560 specialized as A. */
7561
7562 /* So coerce P's args to apply to A's parms, and then deduce between A's
7563 args and the converted args. If that succeeds, A is at least as
7564 specialized as P, so they match.*/
7565 tree pargs = template_parms_level_to_args (parm_parms);
7566 ++processing_template_decl;
7567 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7568 /*require_all*/true, /*use_default*/true);
7569 --processing_template_decl;
7570 if (pargs != error_mark_node)
7571 {
7572 tree targs = make_tree_vec (nargs);
7573 tree aargs = template_parms_level_to_args (arg_parms);
7574 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7575 /*explain*/false))
7576 return 1;
7577 }
7578 }
7579
7580 /* Determine whether we have a parameter pack at the end of the
7581 template template parameter's template parameter list. */
7582 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7583 {
7584 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7585
7586 if (error_operand_p (parm))
7587 return 0;
7588
7589 switch (TREE_CODE (parm))
7590 {
7591 case TEMPLATE_DECL:
7592 case TYPE_DECL:
7593 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7594 variadic_p = 1;
7595 break;
7596
7597 case PARM_DECL:
7598 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7599 variadic_p = 1;
7600 break;
7601
7602 default:
7603 gcc_unreachable ();
7604 }
7605 }
7606
7607 if (nargs != nparms
7608 && !(variadic_p && nargs >= nparms - 1))
7609 return 0;
7610
7611 /* Check all of the template parameters except the parameter pack at
7612 the end (if any). */
7613 for (i = 0; i < nparms - variadic_p; ++i)
7614 {
7615 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7616 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7617 continue;
7618
7619 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7620 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7621
7622 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7623 outer_args))
7624 return 0;
7625
7626 }
7627
7628 if (variadic_p)
7629 {
7630 /* Check each of the template parameters in the template
7631 argument against the template parameter pack at the end of
7632 the template template parameter. */
7633 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7634 return 0;
7635
7636 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7637
7638 for (; i < nargs; ++i)
7639 {
7640 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7641 continue;
7642
7643 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7644
7645 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7646 outer_args))
7647 return 0;
7648 }
7649 }
7650
7651 return 1;
7652 }
7653
7654 /* Verifies that the deduced template arguments (in TARGS) for the
7655 template template parameters (in TPARMS) represent valid bindings,
7656 by comparing the template parameter list of each template argument
7657 to the template parameter list of its corresponding template
7658 template parameter, in accordance with DR150. This
7659 routine can only be called after all template arguments have been
7660 deduced. It will return TRUE if all of the template template
7661 parameter bindings are okay, FALSE otherwise. */
7662 bool
7663 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7664 {
7665 int i, ntparms = TREE_VEC_LENGTH (tparms);
7666 bool ret = true;
7667
7668 /* We're dealing with template parms in this process. */
7669 ++processing_template_decl;
7670
7671 targs = INNERMOST_TEMPLATE_ARGS (targs);
7672
7673 for (i = 0; i < ntparms; ++i)
7674 {
7675 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7676 tree targ = TREE_VEC_ELT (targs, i);
7677
7678 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7679 {
7680 tree packed_args = NULL_TREE;
7681 int idx, len = 1;
7682
7683 if (ARGUMENT_PACK_P (targ))
7684 {
7685 /* Look inside the argument pack. */
7686 packed_args = ARGUMENT_PACK_ARGS (targ);
7687 len = TREE_VEC_LENGTH (packed_args);
7688 }
7689
7690 for (idx = 0; idx < len; ++idx)
7691 {
7692 tree targ_parms = NULL_TREE;
7693
7694 if (packed_args)
7695 /* Extract the next argument from the argument
7696 pack. */
7697 targ = TREE_VEC_ELT (packed_args, idx);
7698
7699 if (PACK_EXPANSION_P (targ))
7700 /* Look at the pattern of the pack expansion. */
7701 targ = PACK_EXPANSION_PATTERN (targ);
7702
7703 /* Extract the template parameters from the template
7704 argument. */
7705 if (TREE_CODE (targ) == TEMPLATE_DECL)
7706 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7707 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7708 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7709
7710 /* Verify that we can coerce the template template
7711 parameters from the template argument to the template
7712 parameter. This requires an exact match. */
7713 if (targ_parms
7714 && !coerce_template_template_parms
7715 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7716 targ_parms,
7717 tf_none,
7718 tparm,
7719 targs))
7720 {
7721 ret = false;
7722 goto out;
7723 }
7724 }
7725 }
7726 }
7727
7728 out:
7729
7730 --processing_template_decl;
7731 return ret;
7732 }
7733
7734 /* Since type attributes aren't mangled, we need to strip them from
7735 template type arguments. */
7736
7737 static tree
7738 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7739 {
7740 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7741 return arg;
7742 bool removed_attributes = false;
7743 tree canon = strip_typedefs (arg, &removed_attributes);
7744 if (removed_attributes
7745 && (complain & tf_warning))
7746 warning (OPT_Wignored_attributes,
7747 "ignoring attributes on template argument %qT", arg);
7748 return canon;
7749 }
7750
7751 /* And from inside dependent non-type arguments like sizeof(Type). */
7752
7753 static tree
7754 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7755 {
7756 if (!arg || arg == error_mark_node)
7757 return arg;
7758 bool removed_attributes = false;
7759 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7760 if (removed_attributes
7761 && (complain & tf_warning))
7762 warning (OPT_Wignored_attributes,
7763 "ignoring attributes in template argument %qE", arg);
7764 return canon;
7765 }
7766
7767 // A template declaration can be substituted for a constrained
7768 // template template parameter only when the argument is more
7769 // constrained than the parameter.
7770 static bool
7771 is_compatible_template_arg (tree parm, tree arg)
7772 {
7773 tree parm_cons = get_constraints (parm);
7774
7775 /* For now, allow constrained template template arguments
7776 and unconstrained template template parameters. */
7777 if (parm_cons == NULL_TREE)
7778 return true;
7779
7780 tree arg_cons = get_constraints (arg);
7781
7782 // If the template parameter is constrained, we need to rewrite its
7783 // constraints in terms of the ARG's template parameters. This ensures
7784 // that all of the template parameter types will have the same depth.
7785 //
7786 // Note that this is only valid when coerce_template_template_parm is
7787 // true for the innermost template parameters of PARM and ARG. In other
7788 // words, because coercion is successful, this conversion will be valid.
7789 if (parm_cons)
7790 {
7791 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7792 parm_cons = tsubst_constraint_info (parm_cons,
7793 INNERMOST_TEMPLATE_ARGS (args),
7794 tf_none, NULL_TREE);
7795 if (parm_cons == error_mark_node)
7796 return false;
7797 }
7798
7799 return subsumes (parm_cons, arg_cons);
7800 }
7801
7802 // Convert a placeholder argument into a binding to the original
7803 // parameter. The original parameter is saved as the TREE_TYPE of
7804 // ARG.
7805 static inline tree
7806 convert_wildcard_argument (tree parm, tree arg)
7807 {
7808 TREE_TYPE (arg) = parm;
7809 return arg;
7810 }
7811
7812 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
7813 because one of them is dependent. But we need to represent the
7814 conversion for the benefit of cp_tree_equal. */
7815
7816 static tree
7817 maybe_convert_nontype_argument (tree type, tree arg)
7818 {
7819 /* Auto parms get no conversion. */
7820 if (type_uses_auto (type))
7821 return arg;
7822 /* We don't need or want to add this conversion now if we're going to use the
7823 argument for deduction. */
7824 if (value_dependent_expression_p (arg))
7825 return arg;
7826
7827 type = cv_unqualified (type);
7828 tree argtype = TREE_TYPE (arg);
7829 if (same_type_p (type, argtype))
7830 return arg;
7831
7832 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
7833 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
7834 return arg;
7835 }
7836
7837 /* Convert the indicated template ARG as necessary to match the
7838 indicated template PARM. Returns the converted ARG, or
7839 error_mark_node if the conversion was unsuccessful. Error and
7840 warning messages are issued under control of COMPLAIN. This
7841 conversion is for the Ith parameter in the parameter list. ARGS is
7842 the full set of template arguments deduced so far. */
7843
7844 static tree
7845 convert_template_argument (tree parm,
7846 tree arg,
7847 tree args,
7848 tsubst_flags_t complain,
7849 int i,
7850 tree in_decl)
7851 {
7852 tree orig_arg;
7853 tree val;
7854 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7855
7856 if (parm == error_mark_node || error_operand_p (arg))
7857 return error_mark_node;
7858
7859 /* Trivially convert placeholders. */
7860 if (TREE_CODE (arg) == WILDCARD_DECL)
7861 return convert_wildcard_argument (parm, arg);
7862
7863 if (arg == any_targ_node)
7864 return arg;
7865
7866 if (TREE_CODE (arg) == TREE_LIST
7867 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7868 {
7869 /* The template argument was the name of some
7870 member function. That's usually
7871 invalid, but static members are OK. In any
7872 case, grab the underlying fields/functions
7873 and issue an error later if required. */
7874 orig_arg = TREE_VALUE (arg);
7875 TREE_TYPE (arg) = unknown_type_node;
7876 }
7877
7878 orig_arg = arg;
7879
7880 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7881 requires_type = (TREE_CODE (parm) == TYPE_DECL
7882 || requires_tmpl_type);
7883
7884 /* When determining whether an argument pack expansion is a template,
7885 look at the pattern. */
7886 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7887 arg = PACK_EXPANSION_PATTERN (arg);
7888
7889 /* Deal with an injected-class-name used as a template template arg. */
7890 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7891 {
7892 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7893 if (TREE_CODE (t) == TEMPLATE_DECL)
7894 {
7895 if (cxx_dialect >= cxx11)
7896 /* OK under DR 1004. */;
7897 else if (complain & tf_warning_or_error)
7898 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7899 " used as template template argument", TYPE_NAME (arg));
7900 else if (flag_pedantic_errors)
7901 t = arg;
7902
7903 arg = t;
7904 }
7905 }
7906
7907 is_tmpl_type =
7908 ((TREE_CODE (arg) == TEMPLATE_DECL
7909 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7910 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7911 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7912 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7913
7914 if (is_tmpl_type
7915 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7916 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7917 arg = TYPE_STUB_DECL (arg);
7918
7919 is_type = TYPE_P (arg) || is_tmpl_type;
7920
7921 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7922 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7923 {
7924 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7925 {
7926 if (complain & tf_error)
7927 error ("invalid use of destructor %qE as a type", orig_arg);
7928 return error_mark_node;
7929 }
7930
7931 permerror (input_location,
7932 "to refer to a type member of a template parameter, "
7933 "use %<typename %E%>", orig_arg);
7934
7935 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7936 TREE_OPERAND (arg, 1),
7937 typename_type,
7938 complain);
7939 arg = orig_arg;
7940 is_type = 1;
7941 }
7942 if (is_type != requires_type)
7943 {
7944 if (in_decl)
7945 {
7946 if (complain & tf_error)
7947 {
7948 error ("type/value mismatch at argument %d in template "
7949 "parameter list for %qD",
7950 i + 1, in_decl);
7951 if (is_type)
7952 inform (input_location,
7953 " expected a constant of type %qT, got %qT",
7954 TREE_TYPE (parm),
7955 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7956 else if (requires_tmpl_type)
7957 inform (input_location,
7958 " expected a class template, got %qE", orig_arg);
7959 else
7960 inform (input_location,
7961 " expected a type, got %qE", orig_arg);
7962 }
7963 }
7964 return error_mark_node;
7965 }
7966 if (is_tmpl_type ^ requires_tmpl_type)
7967 {
7968 if (in_decl && (complain & tf_error))
7969 {
7970 error ("type/value mismatch at argument %d in template "
7971 "parameter list for %qD",
7972 i + 1, in_decl);
7973 if (is_tmpl_type)
7974 inform (input_location,
7975 " expected a type, got %qT", DECL_NAME (arg));
7976 else
7977 inform (input_location,
7978 " expected a class template, got %qT", orig_arg);
7979 }
7980 return error_mark_node;
7981 }
7982
7983 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7984 /* We already did the appropriate conversion when packing args. */
7985 val = orig_arg;
7986 else if (is_type)
7987 {
7988 if (requires_tmpl_type)
7989 {
7990 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7991 /* The number of argument required is not known yet.
7992 Just accept it for now. */
7993 val = orig_arg;
7994 else
7995 {
7996 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7997 tree argparm;
7998
7999 /* Strip alias templates that are equivalent to another
8000 template. */
8001 arg = get_underlying_template (arg);
8002 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8003
8004 if (coerce_template_template_parms (parmparm, argparm,
8005 complain, in_decl,
8006 args))
8007 {
8008 val = arg;
8009
8010 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8011 TEMPLATE_DECL. */
8012 if (val != error_mark_node)
8013 {
8014 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
8015 val = TREE_TYPE (val);
8016 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
8017 val = make_pack_expansion (val, complain);
8018 }
8019 }
8020 else
8021 {
8022 if (in_decl && (complain & tf_error))
8023 {
8024 error ("type/value mismatch at argument %d in "
8025 "template parameter list for %qD",
8026 i + 1, in_decl);
8027 inform (input_location,
8028 " expected a template of type %qD, got %qT",
8029 parm, orig_arg);
8030 }
8031
8032 val = error_mark_node;
8033 }
8034
8035 // Check that the constraints are compatible before allowing the
8036 // substitution.
8037 if (val != error_mark_node)
8038 if (!is_compatible_template_arg (parm, arg))
8039 {
8040 if (in_decl && (complain & tf_error))
8041 {
8042 error ("constraint mismatch at argument %d in "
8043 "template parameter list for %qD",
8044 i + 1, in_decl);
8045 inform (input_location, " expected %qD but got %qD",
8046 parm, arg);
8047 }
8048 val = error_mark_node;
8049 }
8050 }
8051 }
8052 else
8053 val = orig_arg;
8054 /* We only form one instance of each template specialization.
8055 Therefore, if we use a non-canonical variant (i.e., a
8056 typedef), any future messages referring to the type will use
8057 the typedef, which is confusing if those future uses do not
8058 themselves also use the typedef. */
8059 if (TYPE_P (val))
8060 val = canonicalize_type_argument (val, complain);
8061 }
8062 else
8063 {
8064 tree t = TREE_TYPE (parm);
8065
8066 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm))
8067 > TMPL_ARGS_DEPTH (args))
8068 /* We don't have enough levels of args to do any substitution. This
8069 can happen in the context of -fnew-ttp-matching. */;
8070 else if (tree a = type_uses_auto (t))
8071 {
8072 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
8073 if (t == error_mark_node)
8074 return error_mark_node;
8075 }
8076 else
8077 t = tsubst (t, args, complain, in_decl);
8078
8079 if (invalid_nontype_parm_type_p (t, complain))
8080 return error_mark_node;
8081
8082 if (!type_dependent_expression_p (orig_arg)
8083 && !uses_template_parms (t))
8084 /* We used to call digest_init here. However, digest_init
8085 will report errors, which we don't want when complain
8086 is zero. More importantly, digest_init will try too
8087 hard to convert things: for example, `0' should not be
8088 converted to pointer type at this point according to
8089 the standard. Accepting this is not merely an
8090 extension, since deciding whether or not these
8091 conversions can occur is part of determining which
8092 function template to call, or whether a given explicit
8093 argument specification is valid. */
8094 val = convert_nontype_argument (t, orig_arg, complain);
8095 else
8096 {
8097 val = canonicalize_expr_argument (orig_arg, complain);
8098 val = maybe_convert_nontype_argument (t, val);
8099 }
8100
8101
8102 if (val == NULL_TREE)
8103 val = error_mark_node;
8104 else if (val == error_mark_node && (complain & tf_error))
8105 error ("could not convert template argument %qE from %qT to %qT",
8106 orig_arg, TREE_TYPE (orig_arg), t);
8107
8108 if (INDIRECT_REF_P (val))
8109 {
8110 /* Reject template arguments that are references to built-in
8111 functions with no library fallbacks. */
8112 const_tree inner = TREE_OPERAND (val, 0);
8113 const_tree innertype = TREE_TYPE (inner);
8114 if (innertype
8115 && TYPE_REF_P (innertype)
8116 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
8117 && TREE_OPERAND_LENGTH (inner) > 0
8118 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
8119 return error_mark_node;
8120 }
8121
8122 if (TREE_CODE (val) == SCOPE_REF)
8123 {
8124 /* Strip typedefs from the SCOPE_REF. */
8125 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
8126 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
8127 complain);
8128 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
8129 QUALIFIED_NAME_IS_TEMPLATE (val));
8130 }
8131 }
8132
8133 return val;
8134 }
8135
8136 /* Coerces the remaining template arguments in INNER_ARGS (from
8137 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8138 Returns the coerced argument pack. PARM_IDX is the position of this
8139 parameter in the template parameter list. ARGS is the original
8140 template argument list. */
8141 static tree
8142 coerce_template_parameter_pack (tree parms,
8143 int parm_idx,
8144 tree args,
8145 tree inner_args,
8146 int arg_idx,
8147 tree new_args,
8148 int* lost,
8149 tree in_decl,
8150 tsubst_flags_t complain)
8151 {
8152 tree parm = TREE_VEC_ELT (parms, parm_idx);
8153 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8154 tree packed_args;
8155 tree argument_pack;
8156 tree packed_parms = NULL_TREE;
8157
8158 if (arg_idx > nargs)
8159 arg_idx = nargs;
8160
8161 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
8162 {
8163 /* When the template parameter is a non-type template parameter pack
8164 or template template parameter pack whose type or template
8165 parameters use parameter packs, we know exactly how many arguments
8166 we are looking for. Build a vector of the instantiated decls for
8167 these template parameters in PACKED_PARMS. */
8168 /* We can't use make_pack_expansion here because it would interpret a
8169 _DECL as a use rather than a declaration. */
8170 tree decl = TREE_VALUE (parm);
8171 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
8172 SET_PACK_EXPANSION_PATTERN (exp, decl);
8173 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
8174 SET_TYPE_STRUCTURAL_EQUALITY (exp);
8175
8176 TREE_VEC_LENGTH (args)--;
8177 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
8178 TREE_VEC_LENGTH (args)++;
8179
8180 if (packed_parms == error_mark_node)
8181 return error_mark_node;
8182
8183 /* If we're doing a partial instantiation of a member template,
8184 verify that all of the types used for the non-type
8185 template parameter pack are, in fact, valid for non-type
8186 template parameters. */
8187 if (arg_idx < nargs
8188 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
8189 {
8190 int j, len = TREE_VEC_LENGTH (packed_parms);
8191 for (j = 0; j < len; ++j)
8192 {
8193 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
8194 if (invalid_nontype_parm_type_p (t, complain))
8195 return error_mark_node;
8196 }
8197 /* We don't know how many args we have yet, just
8198 use the unconverted ones for now. */
8199 return NULL_TREE;
8200 }
8201
8202 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
8203 }
8204 /* Check if we have a placeholder pack, which indicates we're
8205 in the context of a introduction list. In that case we want
8206 to match this pack to the single placeholder. */
8207 else if (arg_idx < nargs
8208 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
8209 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
8210 {
8211 nargs = arg_idx + 1;
8212 packed_args = make_tree_vec (1);
8213 }
8214 else
8215 packed_args = make_tree_vec (nargs - arg_idx);
8216
8217 /* Convert the remaining arguments, which will be a part of the
8218 parameter pack "parm". */
8219 int first_pack_arg = arg_idx;
8220 for (; arg_idx < nargs; ++arg_idx)
8221 {
8222 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
8223 tree actual_parm = TREE_VALUE (parm);
8224 int pack_idx = arg_idx - first_pack_arg;
8225
8226 if (packed_parms)
8227 {
8228 /* Once we've packed as many args as we have types, stop. */
8229 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
8230 break;
8231 else if (PACK_EXPANSION_P (arg))
8232 /* We don't know how many args we have yet, just
8233 use the unconverted ones for now. */
8234 return NULL_TREE;
8235 else
8236 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
8237 }
8238
8239 if (arg == error_mark_node)
8240 {
8241 if (complain & tf_error)
8242 error ("template argument %d is invalid", arg_idx + 1);
8243 }
8244 else
8245 arg = convert_template_argument (actual_parm,
8246 arg, new_args, complain, parm_idx,
8247 in_decl);
8248 if (arg == error_mark_node)
8249 (*lost)++;
8250 TREE_VEC_ELT (packed_args, pack_idx) = arg;
8251 }
8252
8253 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
8254 && TREE_VEC_LENGTH (packed_args) > 0)
8255 {
8256 if (complain & tf_error)
8257 error ("wrong number of template arguments (%d, should be %d)",
8258 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
8259 return error_mark_node;
8260 }
8261
8262 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
8263 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
8264 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
8265 else
8266 {
8267 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
8268 TREE_CONSTANT (argument_pack) = 1;
8269 }
8270
8271 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
8272 if (CHECKING_P)
8273 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
8274 TREE_VEC_LENGTH (packed_args));
8275 return argument_pack;
8276 }
8277
8278 /* Returns the number of pack expansions in the template argument vector
8279 ARGS. */
8280
8281 static int
8282 pack_expansion_args_count (tree args)
8283 {
8284 int i;
8285 int count = 0;
8286 if (args)
8287 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8288 {
8289 tree elt = TREE_VEC_ELT (args, i);
8290 if (elt && PACK_EXPANSION_P (elt))
8291 ++count;
8292 }
8293 return count;
8294 }
8295
8296 /* Convert all template arguments to their appropriate types, and
8297 return a vector containing the innermost resulting template
8298 arguments. If any error occurs, return error_mark_node. Error and
8299 warning messages are issued under control of COMPLAIN.
8300
8301 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8302 for arguments not specified in ARGS. Otherwise, if
8303 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8304 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8305 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8306 ARGS. */
8307
8308 static tree
8309 coerce_template_parms (tree parms,
8310 tree args,
8311 tree in_decl,
8312 tsubst_flags_t complain,
8313 bool require_all_args,
8314 bool use_default_args)
8315 {
8316 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8317 tree orig_inner_args;
8318 tree inner_args;
8319 tree new_args;
8320 tree new_inner_args;
8321 int saved_unevaluated_operand;
8322 int saved_inhibit_evaluation_warnings;
8323
8324 /* When used as a boolean value, indicates whether this is a
8325 variadic template parameter list. Since it's an int, we can also
8326 subtract it from nparms to get the number of non-variadic
8327 parameters. */
8328 int variadic_p = 0;
8329 int variadic_args_p = 0;
8330 int post_variadic_parms = 0;
8331
8332 /* Adjustment to nparms for fixed parameter packs. */
8333 int fixed_pack_adjust = 0;
8334 int fixed_packs = 0;
8335 int missing = 0;
8336
8337 /* Likewise for parameters with default arguments. */
8338 int default_p = 0;
8339
8340 if (args == error_mark_node)
8341 return error_mark_node;
8342
8343 nparms = TREE_VEC_LENGTH (parms);
8344
8345 /* Determine if there are any parameter packs or default arguments. */
8346 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8347 {
8348 tree parm = TREE_VEC_ELT (parms, parm_idx);
8349 if (variadic_p)
8350 ++post_variadic_parms;
8351 if (template_parameter_pack_p (TREE_VALUE (parm)))
8352 ++variadic_p;
8353 if (TREE_PURPOSE (parm))
8354 ++default_p;
8355 }
8356
8357 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8358 /* If there are no parameters that follow a parameter pack, we need to
8359 expand any argument packs so that we can deduce a parameter pack from
8360 some non-packed args followed by an argument pack, as in variadic85.C.
8361 If there are such parameters, we need to leave argument packs intact
8362 so the arguments are assigned properly. This can happen when dealing
8363 with a nested class inside a partial specialization of a class
8364 template, as in variadic92.C, or when deducing a template parameter pack
8365 from a sub-declarator, as in variadic114.C. */
8366 if (!post_variadic_parms)
8367 inner_args = expand_template_argument_pack (inner_args);
8368
8369 /* Count any pack expansion args. */
8370 variadic_args_p = pack_expansion_args_count (inner_args);
8371
8372 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8373 if ((nargs - variadic_args_p > nparms && !variadic_p)
8374 || (nargs < nparms - variadic_p
8375 && require_all_args
8376 && !variadic_args_p
8377 && (!use_default_args
8378 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8379 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8380 {
8381 bad_nargs:
8382 if (complain & tf_error)
8383 {
8384 if (variadic_p || default_p)
8385 {
8386 nparms -= variadic_p + default_p;
8387 error ("wrong number of template arguments "
8388 "(%d, should be at least %d)", nargs, nparms);
8389 }
8390 else
8391 error ("wrong number of template arguments "
8392 "(%d, should be %d)", nargs, nparms);
8393
8394 if (in_decl)
8395 inform (DECL_SOURCE_LOCATION (in_decl),
8396 "provided for %qD", in_decl);
8397 }
8398
8399 return error_mark_node;
8400 }
8401 /* We can't pass a pack expansion to a non-pack parameter of an alias
8402 template (DR 1430). */
8403 else if (in_decl
8404 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8405 || concept_template_p (in_decl))
8406 && variadic_args_p
8407 && nargs - variadic_args_p < nparms - variadic_p)
8408 {
8409 if (complain & tf_error)
8410 {
8411 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8412 {
8413 tree arg = TREE_VEC_ELT (inner_args, i);
8414 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8415
8416 if (PACK_EXPANSION_P (arg)
8417 && !template_parameter_pack_p (parm))
8418 {
8419 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8420 error_at (location_of (arg),
8421 "pack expansion argument for non-pack parameter "
8422 "%qD of alias template %qD", parm, in_decl);
8423 else
8424 error_at (location_of (arg),
8425 "pack expansion argument for non-pack parameter "
8426 "%qD of concept %qD", parm, in_decl);
8427 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8428 goto found;
8429 }
8430 }
8431 gcc_unreachable ();
8432 found:;
8433 }
8434 return error_mark_node;
8435 }
8436
8437 /* We need to evaluate the template arguments, even though this
8438 template-id may be nested within a "sizeof". */
8439 saved_unevaluated_operand = cp_unevaluated_operand;
8440 cp_unevaluated_operand = 0;
8441 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8442 c_inhibit_evaluation_warnings = 0;
8443 new_inner_args = make_tree_vec (nparms);
8444 new_args = add_outermost_template_args (args, new_inner_args);
8445 int pack_adjust = 0;
8446 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8447 {
8448 tree arg;
8449 tree parm;
8450
8451 /* Get the Ith template parameter. */
8452 parm = TREE_VEC_ELT (parms, parm_idx);
8453
8454 if (parm == error_mark_node)
8455 {
8456 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8457 continue;
8458 }
8459
8460 /* Calculate the next argument. */
8461 if (arg_idx < nargs)
8462 arg = TREE_VEC_ELT (inner_args, arg_idx);
8463 else
8464 arg = NULL_TREE;
8465
8466 if (template_parameter_pack_p (TREE_VALUE (parm))
8467 && !(arg && ARGUMENT_PACK_P (arg)))
8468 {
8469 /* Some arguments will be placed in the
8470 template parameter pack PARM. */
8471 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8472 inner_args, arg_idx,
8473 new_args, &lost,
8474 in_decl, complain);
8475
8476 if (arg == NULL_TREE)
8477 {
8478 /* We don't know how many args we have yet, just use the
8479 unconverted (and still packed) ones for now. */
8480 new_inner_args = orig_inner_args;
8481 arg_idx = nargs;
8482 break;
8483 }
8484
8485 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8486
8487 /* Store this argument. */
8488 if (arg == error_mark_node)
8489 {
8490 lost++;
8491 /* We are done with all of the arguments. */
8492 arg_idx = nargs;
8493 break;
8494 }
8495 else
8496 {
8497 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8498 arg_idx += pack_adjust;
8499 if (fixed_parameter_pack_p (TREE_VALUE (parm)))
8500 {
8501 ++fixed_packs;
8502 fixed_pack_adjust += pack_adjust;
8503 }
8504 }
8505
8506 continue;
8507 }
8508 else if (arg)
8509 {
8510 if (PACK_EXPANSION_P (arg))
8511 {
8512 /* "If every valid specialization of a variadic template
8513 requires an empty template parameter pack, the template is
8514 ill-formed, no diagnostic required." So check that the
8515 pattern works with this parameter. */
8516 tree pattern = PACK_EXPANSION_PATTERN (arg);
8517 tree conv = convert_template_argument (TREE_VALUE (parm),
8518 pattern, new_args,
8519 complain, parm_idx,
8520 in_decl);
8521 if (conv == error_mark_node)
8522 {
8523 if (complain & tf_error)
8524 inform (input_location, "so any instantiation with a "
8525 "non-empty parameter pack would be ill-formed");
8526 ++lost;
8527 }
8528 else if (TYPE_P (conv) && !TYPE_P (pattern))
8529 /* Recover from missing typename. */
8530 TREE_VEC_ELT (inner_args, arg_idx)
8531 = make_pack_expansion (conv, complain);
8532
8533 /* We don't know how many args we have yet, just
8534 use the unconverted ones for now. */
8535 new_inner_args = inner_args;
8536 arg_idx = nargs;
8537 break;
8538 }
8539 }
8540 else if (require_all_args)
8541 {
8542 /* There must be a default arg in this case. */
8543 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8544 complain, in_decl);
8545 /* The position of the first default template argument,
8546 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8547 Record that. */
8548 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8549 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8550 arg_idx - pack_adjust);
8551 }
8552 else
8553 break;
8554
8555 if (arg == error_mark_node)
8556 {
8557 if (complain & tf_error)
8558 error ("template argument %d is invalid", arg_idx + 1);
8559 }
8560 else if (!arg)
8561 {
8562 /* This can occur if there was an error in the template
8563 parameter list itself (which we would already have
8564 reported) that we are trying to recover from, e.g., a class
8565 template with a parameter list such as
8566 template<typename..., typename> (cpp0x/variadic150.C). */
8567 ++lost;
8568
8569 /* This can also happen with a fixed parameter pack (71834). */
8570 if (arg_idx >= nargs)
8571 ++missing;
8572 }
8573 else
8574 arg = convert_template_argument (TREE_VALUE (parm),
8575 arg, new_args, complain,
8576 parm_idx, in_decl);
8577
8578 if (arg == error_mark_node)
8579 lost++;
8580 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8581 }
8582 cp_unevaluated_operand = saved_unevaluated_operand;
8583 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8584
8585 if (missing || arg_idx < nargs - variadic_args_p)
8586 {
8587 /* If we had fixed parameter packs, we didn't know how many arguments we
8588 actually needed earlier; now we do. */
8589 nparms += fixed_pack_adjust;
8590 variadic_p -= fixed_packs;
8591 goto bad_nargs;
8592 }
8593
8594 if (arg_idx < nargs)
8595 {
8596 /* We had some pack expansion arguments that will only work if the packs
8597 are empty, but wait until instantiation time to complain.
8598 See variadic-ttp3.C. */
8599 int len = nparms + (nargs - arg_idx);
8600 tree args = make_tree_vec (len);
8601 int i = 0;
8602 for (; i < nparms; ++i)
8603 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (new_inner_args, i);
8604 for (; i < len; ++i, ++arg_idx)
8605 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (inner_args,
8606 arg_idx - pack_adjust);
8607 new_inner_args = args;
8608 }
8609
8610 if (lost)
8611 {
8612 gcc_assert (!(complain & tf_error) || seen_error ());
8613 return error_mark_node;
8614 }
8615
8616 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8617 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8618 TREE_VEC_LENGTH (new_inner_args));
8619
8620 return new_inner_args;
8621 }
8622
8623 /* Convert all template arguments to their appropriate types, and
8624 return a vector containing the innermost resulting template
8625 arguments. If any error occurs, return error_mark_node. Error and
8626 warning messages are not issued.
8627
8628 Note that no function argument deduction is performed, and default
8629 arguments are used to fill in unspecified arguments. */
8630 tree
8631 coerce_template_parms (tree parms, tree args, tree in_decl)
8632 {
8633 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8634 }
8635
8636 /* Convert all template arguments to their appropriate type, and
8637 instantiate default arguments as needed. This returns a vector
8638 containing the innermost resulting template arguments, or
8639 error_mark_node if unsuccessful. */
8640 tree
8641 coerce_template_parms (tree parms, tree args, tree in_decl,
8642 tsubst_flags_t complain)
8643 {
8644 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8645 }
8646
8647 /* Like coerce_template_parms. If PARMS represents all template
8648 parameters levels, this function returns a vector of vectors
8649 representing all the resulting argument levels. Note that in this
8650 case, only the innermost arguments are coerced because the
8651 outermost ones are supposed to have been coerced already.
8652
8653 Otherwise, if PARMS represents only (the innermost) vector of
8654 parameters, this function returns a vector containing just the
8655 innermost resulting arguments. */
8656
8657 static tree
8658 coerce_innermost_template_parms (tree parms,
8659 tree args,
8660 tree in_decl,
8661 tsubst_flags_t complain,
8662 bool require_all_args,
8663 bool use_default_args)
8664 {
8665 int parms_depth = TMPL_PARMS_DEPTH (parms);
8666 int args_depth = TMPL_ARGS_DEPTH (args);
8667 tree coerced_args;
8668
8669 if (parms_depth > 1)
8670 {
8671 coerced_args = make_tree_vec (parms_depth);
8672 tree level;
8673 int cur_depth;
8674
8675 for (level = parms, cur_depth = parms_depth;
8676 parms_depth > 0 && level != NULL_TREE;
8677 level = TREE_CHAIN (level), --cur_depth)
8678 {
8679 tree l;
8680 if (cur_depth == args_depth)
8681 l = coerce_template_parms (TREE_VALUE (level),
8682 args, in_decl, complain,
8683 require_all_args,
8684 use_default_args);
8685 else
8686 l = TMPL_ARGS_LEVEL (args, cur_depth);
8687
8688 if (l == error_mark_node)
8689 return error_mark_node;
8690
8691 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8692 }
8693 }
8694 else
8695 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8696 args, in_decl, complain,
8697 require_all_args,
8698 use_default_args);
8699 return coerced_args;
8700 }
8701
8702 /* Returns 1 if template args OT and NT are equivalent. */
8703
8704 int
8705 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
8706 {
8707 if (nt == ot)
8708 return 1;
8709 if (nt == NULL_TREE || ot == NULL_TREE)
8710 return false;
8711 if (nt == any_targ_node || ot == any_targ_node)
8712 return true;
8713
8714 if (TREE_CODE (nt) == TREE_VEC)
8715 /* For member templates */
8716 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8717 else if (PACK_EXPANSION_P (ot))
8718 return (PACK_EXPANSION_P (nt)
8719 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8720 PACK_EXPANSION_PATTERN (nt))
8721 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8722 PACK_EXPANSION_EXTRA_ARGS (nt)));
8723 else if (ARGUMENT_PACK_P (ot))
8724 {
8725 int i, len;
8726 tree opack, npack;
8727
8728 if (!ARGUMENT_PACK_P (nt))
8729 return 0;
8730
8731 opack = ARGUMENT_PACK_ARGS (ot);
8732 npack = ARGUMENT_PACK_ARGS (nt);
8733 len = TREE_VEC_LENGTH (opack);
8734 if (TREE_VEC_LENGTH (npack) != len)
8735 return 0;
8736 for (i = 0; i < len; ++i)
8737 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8738 TREE_VEC_ELT (npack, i)))
8739 return 0;
8740 return 1;
8741 }
8742 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8743 gcc_unreachable ();
8744 else if (TYPE_P (nt))
8745 {
8746 if (!TYPE_P (ot))
8747 return false;
8748 /* Don't treat an alias template specialization with dependent
8749 arguments as equivalent to its underlying type when used as a
8750 template argument; we need them to be distinct so that we
8751 substitute into the specialization arguments at instantiation
8752 time. And aliases can't be equivalent without being ==, so
8753 we don't need to look any deeper.
8754
8755 During partial ordering, however, we need to treat them normally so
8756 that we can order uses of the same alias with different
8757 cv-qualification (79960). */
8758 if (!partial_order
8759 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
8760 return false;
8761 else
8762 return same_type_p (ot, nt);
8763 }
8764 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8765 return 0;
8766 else
8767 {
8768 /* Try to treat a template non-type argument that has been converted
8769 to the parameter type as equivalent to one that hasn't yet. */
8770 for (enum tree_code code1 = TREE_CODE (ot);
8771 CONVERT_EXPR_CODE_P (code1)
8772 || code1 == NON_LVALUE_EXPR;
8773 code1 = TREE_CODE (ot))
8774 ot = TREE_OPERAND (ot, 0);
8775 for (enum tree_code code2 = TREE_CODE (nt);
8776 CONVERT_EXPR_CODE_P (code2)
8777 || code2 == NON_LVALUE_EXPR;
8778 code2 = TREE_CODE (nt))
8779 nt = TREE_OPERAND (nt, 0);
8780
8781 return cp_tree_equal (ot, nt);
8782 }
8783 }
8784
8785 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8786 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8787 NEWARG_PTR with the offending arguments if they are non-NULL. */
8788
8789 int
8790 comp_template_args (tree oldargs, tree newargs,
8791 tree *oldarg_ptr, tree *newarg_ptr,
8792 bool partial_order)
8793 {
8794 int i;
8795
8796 if (oldargs == newargs)
8797 return 1;
8798
8799 if (!oldargs || !newargs)
8800 return 0;
8801
8802 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
8803 return 0;
8804
8805 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
8806 {
8807 tree nt = TREE_VEC_ELT (newargs, i);
8808 tree ot = TREE_VEC_ELT (oldargs, i);
8809
8810 if (! template_args_equal (ot, nt, partial_order))
8811 {
8812 if (oldarg_ptr != NULL)
8813 *oldarg_ptr = ot;
8814 if (newarg_ptr != NULL)
8815 *newarg_ptr = nt;
8816 return 0;
8817 }
8818 }
8819 return 1;
8820 }
8821
8822 inline bool
8823 comp_template_args_porder (tree oargs, tree nargs)
8824 {
8825 return comp_template_args (oargs, nargs, NULL, NULL, true);
8826 }
8827
8828 /* Implement a freelist interface for objects of type T.
8829
8830 Head is a separate object, rather than a regular member, so that we
8831 can define it as a GTY deletable pointer, which is highly
8832 desirable. A data member could be declared that way, but then the
8833 containing object would implicitly get GTY((user)), which would
8834 prevent us from instantiating freelists as global objects.
8835 Although this way we can create freelist global objects, they're
8836 such thin wrappers that instantiating temporaries at every use
8837 loses nothing and saves permanent storage for the freelist object.
8838
8839 Member functions next, anew, poison and reinit have default
8840 implementations that work for most of the types we're interested
8841 in, but if they don't work for some type, they should be explicitly
8842 specialized. See the comments before them for requirements, and
8843 the example specializations for the tree_list_freelist. */
8844 template <typename T>
8845 class freelist
8846 {
8847 /* Return the next object in a chain. We could just do type
8848 punning, but if we access the object with its underlying type, we
8849 avoid strict-aliasing trouble. This needs only work between
8850 poison and reinit. */
8851 static T *&next (T *obj) { return obj->next; }
8852
8853 /* Return a newly allocated, uninitialized or minimally-initialized
8854 object of type T. Any initialization performed by anew should
8855 either remain across the life of the object and the execution of
8856 poison, or be redone by reinit. */
8857 static T *anew () { return ggc_alloc<T> (); }
8858
8859 /* Optionally scribble all over the bits holding the object, so that
8860 they become (mostly?) uninitialized memory. This is called while
8861 preparing to make the object part of the free list. */
8862 static void poison (T *obj) {
8863 T *p ATTRIBUTE_UNUSED = obj;
8864 T **q ATTRIBUTE_UNUSED = &next (obj);
8865
8866 #ifdef ENABLE_GC_CHECKING
8867 /* Poison the data, to indicate the data is garbage. */
8868 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, sizeof (*p)));
8869 memset (p, 0xa5, sizeof (*p));
8870 #endif
8871 /* Let valgrind know the object is free. */
8872 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, sizeof (*p)));
8873
8874 /* Let valgrind know the next portion of the object is available,
8875 but uninitialized. */
8876 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
8877 }
8878
8879 /* Bring an object that underwent at least one lifecycle after anew
8880 and before the most recent free and poison, back to a usable
8881 state, reinitializing whatever is needed for it to be
8882 functionally equivalent to an object just allocated and returned
8883 by anew. This may poison or clear the next field, used by
8884 freelist housekeeping after poison was called. */
8885 static void reinit (T *obj) {
8886 T **q ATTRIBUTE_UNUSED = &next (obj);
8887
8888 #ifdef ENABLE_GC_CHECKING
8889 memset (q, 0xa5, sizeof (*q));
8890 #endif
8891 /* Let valgrind know the entire object is available, but
8892 uninitialized. */
8893 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (*obj)));
8894 }
8895
8896 /* Reference a GTY-deletable pointer that points to the first object
8897 in the free list proper. */
8898 T *&head;
8899 public:
8900 /* Construct a freelist object chaining objects off of HEAD. */
8901 freelist (T *&head) : head(head) {}
8902
8903 /* Add OBJ to the free object list. The former head becomes OBJ's
8904 successor. */
8905 void free (T *obj)
8906 {
8907 poison (obj);
8908 next (obj) = head;
8909 head = obj;
8910 }
8911
8912 /* Take an object from the free list, if one is available, or
8913 allocate a new one. Objects taken from the free list should be
8914 regarded as filled with garbage, except for bits that are
8915 configured to be preserved across free and alloc. */
8916 T *alloc ()
8917 {
8918 if (head)
8919 {
8920 T *obj = head;
8921 head = next (head);
8922 reinit (obj);
8923 return obj;
8924 }
8925 else
8926 return anew ();
8927 }
8928 };
8929
8930 /* Explicitly specialize the interfaces for freelist<tree_node>: we
8931 want to allocate a TREE_LIST using the usual interface, and ensure
8932 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
8933 build_tree_list logic in reinit, so this could go out of sync. */
8934 template <>
8935 inline tree &
8936 freelist<tree_node>::next (tree obj)
8937 {
8938 return TREE_CHAIN (obj);
8939 }
8940 template <>
8941 inline tree
8942 freelist<tree_node>::anew ()
8943 {
8944 return build_tree_list (NULL, NULL);
8945 }
8946 template <>
8947 inline void
8948 freelist<tree_node>::poison (tree obj ATTRIBUTE_UNUSED)
8949 {
8950 int size ATTRIBUTE_UNUSED = sizeof (tree_list);
8951 tree p ATTRIBUTE_UNUSED = obj;
8952 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
8953 tree *q ATTRIBUTE_UNUSED = &next (obj);
8954
8955 #ifdef ENABLE_GC_CHECKING
8956 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
8957
8958 /* Poison the data, to indicate the data is garbage. */
8959 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, size));
8960 memset (p, 0xa5, size);
8961 #endif
8962 /* Let valgrind know the object is free. */
8963 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, size));
8964 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
8965 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
8966 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
8967
8968 #ifdef ENABLE_GC_CHECKING
8969 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b, sizeof (*b)));
8970 /* Keep TREE_CHAIN functional. */
8971 TREE_SET_CODE (obj, TREE_LIST);
8972 #else
8973 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
8974 #endif
8975 }
8976 template <>
8977 inline void
8978 freelist<tree_node>::reinit (tree obj ATTRIBUTE_UNUSED)
8979 {
8980 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
8981
8982 #ifdef ENABLE_GC_CHECKING
8983 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
8984 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
8985 memset (obj, 0, sizeof (tree_list));
8986 #endif
8987
8988 /* Let valgrind know the entire object is available, but
8989 uninitialized. */
8990 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
8991
8992 #ifdef ENABLE_GC_CHECKING
8993 TREE_SET_CODE (obj, TREE_LIST);
8994 #else
8995 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
8996 #endif
8997 }
8998
8999 /* Point to the first object in the TREE_LIST freelist. */
9000 static GTY((deletable)) tree tree_list_freelist_head;
9001 /* Return the/an actual TREE_LIST freelist. */
9002 static inline freelist<tree_node>
9003 tree_list_freelist ()
9004 {
9005 return tree_list_freelist_head;
9006 }
9007
9008 /* Point to the first object in the tinst_level freelist. */
9009 static GTY((deletable)) tinst_level *tinst_level_freelist_head;
9010 /* Return the/an actual tinst_level freelist. */
9011 static inline freelist<tinst_level>
9012 tinst_level_freelist ()
9013 {
9014 return tinst_level_freelist_head;
9015 }
9016
9017 /* Point to the first object in the pending_template freelist. */
9018 static GTY((deletable)) pending_template *pending_template_freelist_head;
9019 /* Return the/an actual pending_template freelist. */
9020 static inline freelist<pending_template>
9021 pending_template_freelist ()
9022 {
9023 return pending_template_freelist_head;
9024 }
9025
9026 /* Build the TREE_LIST object out of a split list, store it
9027 permanently, and return it. */
9028 tree
9029 tinst_level::to_list ()
9030 {
9031 gcc_assert (split_list_p ());
9032 tree ret = tree_list_freelist ().alloc ();
9033 TREE_PURPOSE (ret) = tldcl;
9034 TREE_VALUE (ret) = targs;
9035 tldcl = ret;
9036 targs = NULL;
9037 gcc_assert (tree_list_p ());
9038 return ret;
9039 }
9040
9041 const unsigned short tinst_level::refcount_infinity;
9042
9043 /* Increment OBJ's refcount unless it is already infinite. */
9044 static tinst_level *
9045 inc_refcount_use (tinst_level *obj)
9046 {
9047 if (obj && obj->refcount != tinst_level::refcount_infinity)
9048 ++obj->refcount;
9049 return obj;
9050 }
9051
9052 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9053 void
9054 tinst_level::free (tinst_level *obj)
9055 {
9056 if (obj->tree_list_p ())
9057 tree_list_freelist ().free (obj->get_node ());
9058 tinst_level_freelist ().free (obj);
9059 }
9060
9061 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9062 OBJ's DECL and OBJ, and start over with the tinst_level object that
9063 used to be referenced by OBJ's NEXT. */
9064 static void
9065 dec_refcount_use (tinst_level *obj)
9066 {
9067 while (obj
9068 && obj->refcount != tinst_level::refcount_infinity
9069 && !--obj->refcount)
9070 {
9071 tinst_level *next = obj->next;
9072 tinst_level::free (obj);
9073 obj = next;
9074 }
9075 }
9076
9077 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9078 and of the former PTR. Omitting the second argument is equivalent
9079 to passing (T*)NULL; this is allowed because passing the
9080 zero-valued integral constant NULL confuses type deduction and/or
9081 overload resolution. */
9082 template <typename T>
9083 static void
9084 set_refcount_ptr (T *& ptr, T *obj = NULL)
9085 {
9086 T *save = ptr;
9087 ptr = inc_refcount_use (obj);
9088 dec_refcount_use (save);
9089 }
9090
9091 static void
9092 add_pending_template (tree d)
9093 {
9094 tree ti = (TYPE_P (d)
9095 ? CLASSTYPE_TEMPLATE_INFO (d)
9096 : DECL_TEMPLATE_INFO (d));
9097 struct pending_template *pt;
9098 int level;
9099
9100 if (TI_PENDING_TEMPLATE_FLAG (ti))
9101 return;
9102
9103 /* We are called both from instantiate_decl, where we've already had a
9104 tinst_level pushed, and instantiate_template, where we haven't.
9105 Compensate. */
9106 gcc_assert (TREE_CODE (d) != TREE_LIST);
9107 level = !current_tinst_level
9108 || current_tinst_level->maybe_get_node () != d;
9109
9110 if (level)
9111 push_tinst_level (d);
9112
9113 pt = pending_template_freelist ().alloc ();
9114 pt->next = NULL;
9115 pt->tinst = NULL;
9116 set_refcount_ptr (pt->tinst, current_tinst_level);
9117 if (last_pending_template)
9118 last_pending_template->next = pt;
9119 else
9120 pending_templates = pt;
9121
9122 last_pending_template = pt;
9123
9124 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
9125
9126 if (level)
9127 pop_tinst_level ();
9128 }
9129
9130
9131 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9132 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9133 documentation for TEMPLATE_ID_EXPR. */
9134
9135 tree
9136 lookup_template_function (tree fns, tree arglist)
9137 {
9138 tree type;
9139
9140 if (fns == error_mark_node || arglist == error_mark_node)
9141 return error_mark_node;
9142
9143 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
9144
9145 if (!is_overloaded_fn (fns) && !identifier_p (fns))
9146 {
9147 error ("%q#D is not a function template", fns);
9148 return error_mark_node;
9149 }
9150
9151 if (BASELINK_P (fns))
9152 {
9153 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
9154 unknown_type_node,
9155 BASELINK_FUNCTIONS (fns),
9156 arglist);
9157 return fns;
9158 }
9159
9160 type = TREE_TYPE (fns);
9161 if (TREE_CODE (fns) == OVERLOAD || !type)
9162 type = unknown_type_node;
9163
9164 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
9165 }
9166
9167 /* Within the scope of a template class S<T>, the name S gets bound
9168 (in build_self_reference) to a TYPE_DECL for the class, not a
9169 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9170 or one of its enclosing classes, and that type is a template,
9171 return the associated TEMPLATE_DECL. Otherwise, the original
9172 DECL is returned.
9173
9174 Also handle the case when DECL is a TREE_LIST of ambiguous
9175 injected-class-names from different bases. */
9176
9177 tree
9178 maybe_get_template_decl_from_type_decl (tree decl)
9179 {
9180 if (decl == NULL_TREE)
9181 return decl;
9182
9183 /* DR 176: A lookup that finds an injected-class-name (10.2
9184 [class.member.lookup]) can result in an ambiguity in certain cases
9185 (for example, if it is found in more than one base class). If all of
9186 the injected-class-names that are found refer to specializations of
9187 the same class template, and if the name is followed by a
9188 template-argument-list, the reference refers to the class template
9189 itself and not a specialization thereof, and is not ambiguous. */
9190 if (TREE_CODE (decl) == TREE_LIST)
9191 {
9192 tree t, tmpl = NULL_TREE;
9193 for (t = decl; t; t = TREE_CHAIN (t))
9194 {
9195 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
9196 if (!tmpl)
9197 tmpl = elt;
9198 else if (tmpl != elt)
9199 break;
9200 }
9201 if (tmpl && t == NULL_TREE)
9202 return tmpl;
9203 else
9204 return decl;
9205 }
9206
9207 return (decl != NULL_TREE
9208 && DECL_SELF_REFERENCE_P (decl)
9209 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
9210 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
9211 }
9212
9213 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9214 parameters, find the desired type.
9215
9216 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9217
9218 IN_DECL, if non-NULL, is the template declaration we are trying to
9219 instantiate.
9220
9221 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9222 the class we are looking up.
9223
9224 Issue error and warning messages under control of COMPLAIN.
9225
9226 If the template class is really a local class in a template
9227 function, then the FUNCTION_CONTEXT is the function in which it is
9228 being instantiated.
9229
9230 ??? Note that this function is currently called *twice* for each
9231 template-id: the first time from the parser, while creating the
9232 incomplete type (finish_template_type), and the second type during the
9233 real instantiation (instantiate_template_class). This is surely something
9234 that we want to avoid. It also causes some problems with argument
9235 coercion (see convert_nontype_argument for more information on this). */
9236
9237 static tree
9238 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
9239 int entering_scope, tsubst_flags_t complain)
9240 {
9241 tree templ = NULL_TREE, parmlist;
9242 tree t;
9243 spec_entry **slot;
9244 spec_entry *entry;
9245 spec_entry elt;
9246 hashval_t hash;
9247
9248 if (identifier_p (d1))
9249 {
9250 tree value = innermost_non_namespace_value (d1);
9251 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
9252 templ = value;
9253 else
9254 {
9255 if (context)
9256 push_decl_namespace (context);
9257 templ = lookup_name (d1);
9258 templ = maybe_get_template_decl_from_type_decl (templ);
9259 if (context)
9260 pop_decl_namespace ();
9261 }
9262 if (templ)
9263 context = DECL_CONTEXT (templ);
9264 }
9265 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
9266 {
9267 tree type = TREE_TYPE (d1);
9268
9269 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9270 an implicit typename for the second A. Deal with it. */
9271 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
9272 type = TREE_TYPE (type);
9273
9274 if (CLASSTYPE_TEMPLATE_INFO (type))
9275 {
9276 templ = CLASSTYPE_TI_TEMPLATE (type);
9277 d1 = DECL_NAME (templ);
9278 }
9279 }
9280 else if (TREE_CODE (d1) == ENUMERAL_TYPE
9281 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
9282 {
9283 templ = TYPE_TI_TEMPLATE (d1);
9284 d1 = DECL_NAME (templ);
9285 }
9286 else if (DECL_TYPE_TEMPLATE_P (d1))
9287 {
9288 templ = d1;
9289 d1 = DECL_NAME (templ);
9290 context = DECL_CONTEXT (templ);
9291 }
9292 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
9293 {
9294 templ = d1;
9295 d1 = DECL_NAME (templ);
9296 }
9297
9298 /* Issue an error message if we didn't find a template. */
9299 if (! templ)
9300 {
9301 if (complain & tf_error)
9302 error ("%qT is not a template", d1);
9303 return error_mark_node;
9304 }
9305
9306 if (TREE_CODE (templ) != TEMPLATE_DECL
9307 /* Make sure it's a user visible template, if it was named by
9308 the user. */
9309 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
9310 && !PRIMARY_TEMPLATE_P (templ)))
9311 {
9312 if (complain & tf_error)
9313 {
9314 error ("non-template type %qT used as a template", d1);
9315 if (in_decl)
9316 error ("for template declaration %q+D", in_decl);
9317 }
9318 return error_mark_node;
9319 }
9320
9321 complain &= ~tf_user;
9322
9323 /* An alias that just changes the name of a template is equivalent to the
9324 other template, so if any of the arguments are pack expansions, strip
9325 the alias to avoid problems with a pack expansion passed to a non-pack
9326 alias template parameter (DR 1430). */
9327 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
9328 templ = get_underlying_template (templ);
9329
9330 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
9331 {
9332 tree parm;
9333 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
9334 if (arglist2 == error_mark_node
9335 || (!uses_template_parms (arglist2)
9336 && check_instantiated_args (templ, arglist2, complain)))
9337 return error_mark_node;
9338
9339 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
9340 return parm;
9341 }
9342 else
9343 {
9344 tree template_type = TREE_TYPE (templ);
9345 tree gen_tmpl;
9346 tree type_decl;
9347 tree found = NULL_TREE;
9348 int arg_depth;
9349 int parm_depth;
9350 int is_dependent_type;
9351 int use_partial_inst_tmpl = false;
9352
9353 if (template_type == error_mark_node)
9354 /* An error occurred while building the template TEMPL, and a
9355 diagnostic has most certainly been emitted for that
9356 already. Let's propagate that error. */
9357 return error_mark_node;
9358
9359 gen_tmpl = most_general_template (templ);
9360 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
9361 parm_depth = TMPL_PARMS_DEPTH (parmlist);
9362 arg_depth = TMPL_ARGS_DEPTH (arglist);
9363
9364 if (arg_depth == 1 && parm_depth > 1)
9365 {
9366 /* We've been given an incomplete set of template arguments.
9367 For example, given:
9368
9369 template <class T> struct S1 {
9370 template <class U> struct S2 {};
9371 template <class U> struct S2<U*> {};
9372 };
9373
9374 we will be called with an ARGLIST of `U*', but the
9375 TEMPLATE will be `template <class T> template
9376 <class U> struct S1<T>::S2'. We must fill in the missing
9377 arguments. */
9378 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
9379 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
9380 arg_depth = TMPL_ARGS_DEPTH (arglist);
9381 }
9382
9383 /* Now we should have enough arguments. */
9384 gcc_assert (parm_depth == arg_depth);
9385
9386 /* From here on, we're only interested in the most general
9387 template. */
9388
9389 /* Calculate the BOUND_ARGS. These will be the args that are
9390 actually tsubst'd into the definition to create the
9391 instantiation. */
9392 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
9393 complain,
9394 /*require_all_args=*/true,
9395 /*use_default_args=*/true);
9396
9397 if (arglist == error_mark_node)
9398 /* We were unable to bind the arguments. */
9399 return error_mark_node;
9400
9401 /* In the scope of a template class, explicit references to the
9402 template class refer to the type of the template, not any
9403 instantiation of it. For example, in:
9404
9405 template <class T> class C { void f(C<T>); }
9406
9407 the `C<T>' is just the same as `C'. Outside of the
9408 class, however, such a reference is an instantiation. */
9409 if (entering_scope
9410 || !PRIMARY_TEMPLATE_P (gen_tmpl)
9411 || currently_open_class (template_type))
9412 {
9413 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
9414
9415 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
9416 return template_type;
9417 }
9418
9419 /* If we already have this specialization, return it. */
9420 elt.tmpl = gen_tmpl;
9421 elt.args = arglist;
9422 elt.spec = NULL_TREE;
9423 hash = spec_hasher::hash (&elt);
9424 entry = type_specializations->find_with_hash (&elt, hash);
9425
9426 if (entry)
9427 return entry->spec;
9428
9429 /* If the the template's constraints are not satisfied,
9430 then we cannot form a valid type.
9431
9432 Note that the check is deferred until after the hash
9433 lookup. This prevents redundant checks on previously
9434 instantiated specializations. */
9435 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
9436 {
9437 if (complain & tf_error)
9438 {
9439 auto_diagnostic_group d;
9440 error ("template constraint failure");
9441 diagnose_constraints (input_location, gen_tmpl, arglist);
9442 }
9443 return error_mark_node;
9444 }
9445
9446 is_dependent_type = uses_template_parms (arglist);
9447
9448 /* If the deduced arguments are invalid, then the binding
9449 failed. */
9450 if (!is_dependent_type
9451 && check_instantiated_args (gen_tmpl,
9452 INNERMOST_TEMPLATE_ARGS (arglist),
9453 complain))
9454 return error_mark_node;
9455
9456 if (!is_dependent_type
9457 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9458 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
9459 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
9460 {
9461 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
9462 DECL_NAME (gen_tmpl),
9463 /*tag_scope=*/ts_global);
9464 return found;
9465 }
9466
9467 context = DECL_CONTEXT (gen_tmpl);
9468 if (context && TYPE_P (context))
9469 {
9470 context = tsubst_aggr_type (context, arglist, complain, in_decl, true);
9471 context = complete_type (context);
9472 }
9473 else
9474 context = tsubst (context, arglist, complain, in_decl);
9475
9476 if (context == error_mark_node)
9477 return error_mark_node;
9478
9479 if (!context)
9480 context = global_namespace;
9481
9482 /* Create the type. */
9483 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9484 {
9485 /* The user referred to a specialization of an alias
9486 template represented by GEN_TMPL.
9487
9488 [temp.alias]/2 says:
9489
9490 When a template-id refers to the specialization of an
9491 alias template, it is equivalent to the associated
9492 type obtained by substitution of its
9493 template-arguments for the template-parameters in the
9494 type-id of the alias template. */
9495
9496 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
9497 /* Note that the call above (by indirectly calling
9498 register_specialization in tsubst_decl) registers the
9499 TYPE_DECL representing the specialization of the alias
9500 template. So next time someone substitutes ARGLIST for
9501 the template parms into the alias template (GEN_TMPL),
9502 she'll get that TYPE_DECL back. */
9503
9504 if (t == error_mark_node)
9505 return t;
9506 }
9507 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
9508 {
9509 if (!is_dependent_type)
9510 {
9511 set_current_access_from_decl (TYPE_NAME (template_type));
9512 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
9513 tsubst (ENUM_UNDERLYING_TYPE (template_type),
9514 arglist, complain, in_decl),
9515 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
9516 arglist, complain, in_decl),
9517 SCOPED_ENUM_P (template_type), NULL);
9518
9519 if (t == error_mark_node)
9520 return t;
9521 }
9522 else
9523 {
9524 /* We don't want to call start_enum for this type, since
9525 the values for the enumeration constants may involve
9526 template parameters. And, no one should be interested
9527 in the enumeration constants for such a type. */
9528 t = cxx_make_type (ENUMERAL_TYPE);
9529 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
9530 }
9531 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
9532 ENUM_FIXED_UNDERLYING_TYPE_P (t)
9533 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
9534 }
9535 else if (CLASS_TYPE_P (template_type))
9536 {
9537 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9538 instantiated here. */
9539 gcc_assert (!LAMBDA_TYPE_P (template_type));
9540
9541 t = make_class_type (TREE_CODE (template_type));
9542 CLASSTYPE_DECLARED_CLASS (t)
9543 = CLASSTYPE_DECLARED_CLASS (template_type);
9544 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
9545
9546 /* A local class. Make sure the decl gets registered properly. */
9547 if (context == current_function_decl)
9548 if (pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current)
9549 == error_mark_node)
9550 return error_mark_node;
9551
9552 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
9553 /* This instantiation is another name for the primary
9554 template type. Set the TYPE_CANONICAL field
9555 appropriately. */
9556 TYPE_CANONICAL (t) = template_type;
9557 else if (any_template_arguments_need_structural_equality_p (arglist))
9558 /* Some of the template arguments require structural
9559 equality testing, so this template class requires
9560 structural equality testing. */
9561 SET_TYPE_STRUCTURAL_EQUALITY (t);
9562 }
9563 else
9564 gcc_unreachable ();
9565
9566 /* If we called start_enum or pushtag above, this information
9567 will already be set up. */
9568 if (!TYPE_NAME (t))
9569 {
9570 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
9571
9572 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
9573 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
9574 DECL_SOURCE_LOCATION (type_decl)
9575 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
9576 }
9577 else
9578 type_decl = TYPE_NAME (t);
9579
9580 if (CLASS_TYPE_P (template_type))
9581 {
9582 TREE_PRIVATE (type_decl)
9583 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
9584 TREE_PROTECTED (type_decl)
9585 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
9586 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
9587 {
9588 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
9589 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
9590 }
9591 }
9592
9593 if (OVERLOAD_TYPE_P (t)
9594 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9595 {
9596 static const char *tags[] = {"abi_tag", "may_alias"};
9597
9598 for (unsigned ix = 0; ix != 2; ix++)
9599 {
9600 tree attributes
9601 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
9602
9603 if (attributes)
9604 TYPE_ATTRIBUTES (t)
9605 = tree_cons (TREE_PURPOSE (attributes),
9606 TREE_VALUE (attributes),
9607 TYPE_ATTRIBUTES (t));
9608 }
9609 }
9610
9611 /* Let's consider the explicit specialization of a member
9612 of a class template specialization that is implicitly instantiated,
9613 e.g.:
9614 template<class T>
9615 struct S
9616 {
9617 template<class U> struct M {}; //#0
9618 };
9619
9620 template<>
9621 template<>
9622 struct S<int>::M<char> //#1
9623 {
9624 int i;
9625 };
9626 [temp.expl.spec]/4 says this is valid.
9627
9628 In this case, when we write:
9629 S<int>::M<char> m;
9630
9631 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9632 the one of #0.
9633
9634 When we encounter #1, we want to store the partial instantiation
9635 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9636
9637 For all cases other than this "explicit specialization of member of a
9638 class template", we just want to store the most general template into
9639 the CLASSTYPE_TI_TEMPLATE of M.
9640
9641 This case of "explicit specialization of member of a class template"
9642 only happens when:
9643 1/ the enclosing class is an instantiation of, and therefore not
9644 the same as, the context of the most general template, and
9645 2/ we aren't looking at the partial instantiation itself, i.e.
9646 the innermost arguments are not the same as the innermost parms of
9647 the most general template.
9648
9649 So it's only when 1/ and 2/ happens that we want to use the partial
9650 instantiation of the member template in lieu of its most general
9651 template. */
9652
9653 if (PRIMARY_TEMPLATE_P (gen_tmpl)
9654 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
9655 /* the enclosing class must be an instantiation... */
9656 && CLASS_TYPE_P (context)
9657 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
9658 {
9659 TREE_VEC_LENGTH (arglist)--;
9660 ++processing_template_decl;
9661 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
9662 tree partial_inst_args =
9663 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
9664 arglist, complain, NULL_TREE);
9665 --processing_template_decl;
9666 TREE_VEC_LENGTH (arglist)++;
9667 if (partial_inst_args == error_mark_node)
9668 return error_mark_node;
9669 use_partial_inst_tmpl =
9670 /*...and we must not be looking at the partial instantiation
9671 itself. */
9672 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
9673 partial_inst_args);
9674 }
9675
9676 if (!use_partial_inst_tmpl)
9677 /* This case is easy; there are no member templates involved. */
9678 found = gen_tmpl;
9679 else
9680 {
9681 /* This is a full instantiation of a member template. Find
9682 the partial instantiation of which this is an instance. */
9683
9684 /* Temporarily reduce by one the number of levels in the ARGLIST
9685 so as to avoid comparing the last set of arguments. */
9686 TREE_VEC_LENGTH (arglist)--;
9687 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
9688 TREE_VEC_LENGTH (arglist)++;
9689 /* FOUND is either a proper class type, or an alias
9690 template specialization. In the later case, it's a
9691 TYPE_DECL, resulting from the substituting of arguments
9692 for parameters in the TYPE_DECL of the alias template
9693 done earlier. So be careful while getting the template
9694 of FOUND. */
9695 found = (TREE_CODE (found) == TEMPLATE_DECL
9696 ? found
9697 : (TREE_CODE (found) == TYPE_DECL
9698 ? DECL_TI_TEMPLATE (found)
9699 : CLASSTYPE_TI_TEMPLATE (found)));
9700 }
9701
9702 // Build template info for the new specialization.
9703 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
9704
9705 elt.spec = t;
9706 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
9707 entry = ggc_alloc<spec_entry> ();
9708 *entry = elt;
9709 *slot = entry;
9710
9711 /* Note this use of the partial instantiation so we can check it
9712 later in maybe_process_partial_specialization. */
9713 DECL_TEMPLATE_INSTANTIATIONS (found)
9714 = tree_cons (arglist, t,
9715 DECL_TEMPLATE_INSTANTIATIONS (found));
9716
9717 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
9718 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9719 /* Now that the type has been registered on the instantiations
9720 list, we set up the enumerators. Because the enumeration
9721 constants may involve the enumeration type itself, we make
9722 sure to register the type first, and then create the
9723 constants. That way, doing tsubst_expr for the enumeration
9724 constants won't result in recursive calls here; we'll find
9725 the instantiation and exit above. */
9726 tsubst_enum (template_type, t, arglist);
9727
9728 if (CLASS_TYPE_P (template_type) && is_dependent_type)
9729 /* If the type makes use of template parameters, the
9730 code that generates debugging information will crash. */
9731 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
9732
9733 /* Possibly limit visibility based on template args. */
9734 TREE_PUBLIC (type_decl) = 1;
9735 determine_visibility (type_decl);
9736
9737 inherit_targ_abi_tags (t);
9738
9739 return t;
9740 }
9741 }
9742
9743 /* Wrapper for lookup_template_class_1. */
9744
9745 tree
9746 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
9747 int entering_scope, tsubst_flags_t complain)
9748 {
9749 tree ret;
9750 timevar_push (TV_TEMPLATE_INST);
9751 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
9752 entering_scope, complain);
9753 timevar_pop (TV_TEMPLATE_INST);
9754 return ret;
9755 }
9756
9757 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9758
9759 tree
9760 lookup_template_variable (tree templ, tree arglist)
9761 {
9762 /* The type of the expression is NULL_TREE since the template-id could refer
9763 to an explicit or partial specialization. */
9764 tree type = NULL_TREE;
9765 if (flag_concepts && variable_concept_p (templ))
9766 /* Except that concepts are always bool. */
9767 type = boolean_type_node;
9768 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
9769 }
9770
9771 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9772
9773 tree
9774 finish_template_variable (tree var, tsubst_flags_t complain)
9775 {
9776 tree templ = TREE_OPERAND (var, 0);
9777 tree arglist = TREE_OPERAND (var, 1);
9778
9779 /* We never want to return a VAR_DECL for a variable concept, since they
9780 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9781 bool concept_p = flag_concepts && variable_concept_p (templ);
9782 if (concept_p && processing_template_decl)
9783 return var;
9784
9785 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
9786 arglist = add_outermost_template_args (tmpl_args, arglist);
9787
9788 templ = most_general_template (templ);
9789 tree parms = DECL_TEMPLATE_PARMS (templ);
9790 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
9791 /*req_all*/true,
9792 /*use_default*/true);
9793
9794 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
9795 {
9796 if (complain & tf_error)
9797 {
9798 auto_diagnostic_group d;
9799 error ("use of invalid variable template %qE", var);
9800 diagnose_constraints (location_of (var), templ, arglist);
9801 }
9802 return error_mark_node;
9803 }
9804
9805 /* If a template-id refers to a specialization of a variable
9806 concept, then the expression is true if and only if the
9807 concept's constraints are satisfied by the given template
9808 arguments.
9809
9810 NOTE: This is an extension of Concepts Lite TS that
9811 allows constraints to be used in expressions. */
9812 if (concept_p)
9813 {
9814 tree decl = DECL_TEMPLATE_RESULT (templ);
9815 return evaluate_variable_concept (decl, arglist);
9816 }
9817
9818 return instantiate_template (templ, arglist, complain);
9819 }
9820
9821 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9822 TARGS template args, and instantiate it if it's not dependent. */
9823
9824 tree
9825 lookup_and_finish_template_variable (tree templ, tree targs,
9826 tsubst_flags_t complain)
9827 {
9828 templ = lookup_template_variable (templ, targs);
9829 if (!any_dependent_template_arguments_p (targs))
9830 {
9831 templ = finish_template_variable (templ, complain);
9832 mark_used (templ);
9833 }
9834
9835 return convert_from_reference (templ);
9836 }
9837
9838 \f
9839 struct pair_fn_data
9840 {
9841 tree_fn_t fn;
9842 tree_fn_t any_fn;
9843 void *data;
9844 /* True when we should also visit template parameters that occur in
9845 non-deduced contexts. */
9846 bool include_nondeduced_p;
9847 hash_set<tree> *visited;
9848 };
9849
9850 /* Called from for_each_template_parm via walk_tree. */
9851
9852 static tree
9853 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
9854 {
9855 tree t = *tp;
9856 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
9857 tree_fn_t fn = pfd->fn;
9858 void *data = pfd->data;
9859 tree result = NULL_TREE;
9860
9861 #define WALK_SUBTREE(NODE) \
9862 do \
9863 { \
9864 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9865 pfd->include_nondeduced_p, \
9866 pfd->any_fn); \
9867 if (result) goto out; \
9868 } \
9869 while (0)
9870
9871 if (pfd->any_fn && (*pfd->any_fn)(t, data))
9872 return t;
9873
9874 if (TYPE_P (t)
9875 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
9876 WALK_SUBTREE (TYPE_CONTEXT (t));
9877
9878 switch (TREE_CODE (t))
9879 {
9880 case RECORD_TYPE:
9881 if (TYPE_PTRMEMFUNC_P (t))
9882 break;
9883 /* Fall through. */
9884
9885 case UNION_TYPE:
9886 case ENUMERAL_TYPE:
9887 if (!TYPE_TEMPLATE_INFO (t))
9888 *walk_subtrees = 0;
9889 else
9890 WALK_SUBTREE (TYPE_TI_ARGS (t));
9891 break;
9892
9893 case INTEGER_TYPE:
9894 WALK_SUBTREE (TYPE_MIN_VALUE (t));
9895 WALK_SUBTREE (TYPE_MAX_VALUE (t));
9896 break;
9897
9898 case METHOD_TYPE:
9899 /* Since we're not going to walk subtrees, we have to do this
9900 explicitly here. */
9901 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
9902 /* Fall through. */
9903
9904 case FUNCTION_TYPE:
9905 /* Check the return type. */
9906 WALK_SUBTREE (TREE_TYPE (t));
9907
9908 /* Check the parameter types. Since default arguments are not
9909 instantiated until they are needed, the TYPE_ARG_TYPES may
9910 contain expressions that involve template parameters. But,
9911 no-one should be looking at them yet. And, once they're
9912 instantiated, they don't contain template parameters, so
9913 there's no point in looking at them then, either. */
9914 {
9915 tree parm;
9916
9917 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
9918 WALK_SUBTREE (TREE_VALUE (parm));
9919
9920 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9921 want walk_tree walking into them itself. */
9922 *walk_subtrees = 0;
9923 }
9924
9925 if (flag_noexcept_type)
9926 {
9927 tree spec = TYPE_RAISES_EXCEPTIONS (t);
9928 if (spec)
9929 WALK_SUBTREE (TREE_PURPOSE (spec));
9930 }
9931 break;
9932
9933 case TYPEOF_TYPE:
9934 case DECLTYPE_TYPE:
9935 case UNDERLYING_TYPE:
9936 if (pfd->include_nondeduced_p
9937 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
9938 pfd->visited,
9939 pfd->include_nondeduced_p,
9940 pfd->any_fn))
9941 return error_mark_node;
9942 *walk_subtrees = false;
9943 break;
9944
9945 case FUNCTION_DECL:
9946 case VAR_DECL:
9947 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9948 WALK_SUBTREE (DECL_TI_ARGS (t));
9949 /* Fall through. */
9950
9951 case PARM_DECL:
9952 case CONST_DECL:
9953 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
9954 WALK_SUBTREE (DECL_INITIAL (t));
9955 if (DECL_CONTEXT (t)
9956 && pfd->include_nondeduced_p)
9957 WALK_SUBTREE (DECL_CONTEXT (t));
9958 break;
9959
9960 case BOUND_TEMPLATE_TEMPLATE_PARM:
9961 /* Record template parameters such as `T' inside `TT<T>'. */
9962 WALK_SUBTREE (TYPE_TI_ARGS (t));
9963 /* Fall through. */
9964
9965 case TEMPLATE_TEMPLATE_PARM:
9966 case TEMPLATE_TYPE_PARM:
9967 case TEMPLATE_PARM_INDEX:
9968 if (fn && (*fn)(t, data))
9969 return t;
9970 else if (!fn)
9971 return t;
9972 break;
9973
9974 case TEMPLATE_DECL:
9975 /* A template template parameter is encountered. */
9976 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9977 WALK_SUBTREE (TREE_TYPE (t));
9978
9979 /* Already substituted template template parameter */
9980 *walk_subtrees = 0;
9981 break;
9982
9983 case TYPENAME_TYPE:
9984 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9985 partial instantiation. */
9986 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
9987 break;
9988
9989 case CONSTRUCTOR:
9990 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
9991 && pfd->include_nondeduced_p)
9992 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
9993 break;
9994
9995 case INDIRECT_REF:
9996 case COMPONENT_REF:
9997 /* If there's no type, then this thing must be some expression
9998 involving template parameters. */
9999 if (!fn && !TREE_TYPE (t))
10000 return error_mark_node;
10001 break;
10002
10003 case MODOP_EXPR:
10004 case CAST_EXPR:
10005 case IMPLICIT_CONV_EXPR:
10006 case REINTERPRET_CAST_EXPR:
10007 case CONST_CAST_EXPR:
10008 case STATIC_CAST_EXPR:
10009 case DYNAMIC_CAST_EXPR:
10010 case ARROW_EXPR:
10011 case DOTSTAR_EXPR:
10012 case TYPEID_EXPR:
10013 case PSEUDO_DTOR_EXPR:
10014 if (!fn)
10015 return error_mark_node;
10016 break;
10017
10018 default:
10019 break;
10020 }
10021
10022 #undef WALK_SUBTREE
10023
10024 /* We didn't find any template parameters we liked. */
10025 out:
10026 return result;
10027 }
10028
10029 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10030 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10031 call FN with the parameter and the DATA.
10032 If FN returns nonzero, the iteration is terminated, and
10033 for_each_template_parm returns 1. Otherwise, the iteration
10034 continues. If FN never returns a nonzero value, the value
10035 returned by for_each_template_parm is 0. If FN is NULL, it is
10036 considered to be the function which always returns 1.
10037
10038 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10039 parameters that occur in non-deduced contexts. When false, only
10040 visits those template parameters that can be deduced. */
10041
10042 static tree
10043 for_each_template_parm (tree t, tree_fn_t fn, void* data,
10044 hash_set<tree> *visited,
10045 bool include_nondeduced_p,
10046 tree_fn_t any_fn)
10047 {
10048 struct pair_fn_data pfd;
10049 tree result;
10050
10051 /* Set up. */
10052 pfd.fn = fn;
10053 pfd.any_fn = any_fn;
10054 pfd.data = data;
10055 pfd.include_nondeduced_p = include_nondeduced_p;
10056
10057 /* Walk the tree. (Conceptually, we would like to walk without
10058 duplicates, but for_each_template_parm_r recursively calls
10059 for_each_template_parm, so we would need to reorganize a fair
10060 bit to use walk_tree_without_duplicates, so we keep our own
10061 visited list.) */
10062 if (visited)
10063 pfd.visited = visited;
10064 else
10065 pfd.visited = new hash_set<tree>;
10066 result = cp_walk_tree (&t,
10067 for_each_template_parm_r,
10068 &pfd,
10069 pfd.visited);
10070
10071 /* Clean up. */
10072 if (!visited)
10073 {
10074 delete pfd.visited;
10075 pfd.visited = 0;
10076 }
10077
10078 return result;
10079 }
10080
10081 /* Returns true if T depends on any template parameter. */
10082
10083 int
10084 uses_template_parms (tree t)
10085 {
10086 if (t == NULL_TREE)
10087 return false;
10088
10089 bool dependent_p;
10090 int saved_processing_template_decl;
10091
10092 saved_processing_template_decl = processing_template_decl;
10093 if (!saved_processing_template_decl)
10094 processing_template_decl = 1;
10095 if (TYPE_P (t))
10096 dependent_p = dependent_type_p (t);
10097 else if (TREE_CODE (t) == TREE_VEC)
10098 dependent_p = any_dependent_template_arguments_p (t);
10099 else if (TREE_CODE (t) == TREE_LIST)
10100 dependent_p = (uses_template_parms (TREE_VALUE (t))
10101 || uses_template_parms (TREE_CHAIN (t)));
10102 else if (TREE_CODE (t) == TYPE_DECL)
10103 dependent_p = dependent_type_p (TREE_TYPE (t));
10104 else if (DECL_P (t)
10105 || EXPR_P (t)
10106 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
10107 || TREE_CODE (t) == OVERLOAD
10108 || BASELINK_P (t)
10109 || identifier_p (t)
10110 || TREE_CODE (t) == TRAIT_EXPR
10111 || TREE_CODE (t) == CONSTRUCTOR
10112 || CONSTANT_CLASS_P (t))
10113 dependent_p = (type_dependent_expression_p (t)
10114 || value_dependent_expression_p (t));
10115 else
10116 {
10117 gcc_assert (t == error_mark_node);
10118 dependent_p = false;
10119 }
10120
10121 processing_template_decl = saved_processing_template_decl;
10122
10123 return dependent_p;
10124 }
10125
10126 /* Returns true iff current_function_decl is an incompletely instantiated
10127 template. Useful instead of processing_template_decl because the latter
10128 is set to 0 during instantiate_non_dependent_expr. */
10129
10130 bool
10131 in_template_function (void)
10132 {
10133 tree fn = current_function_decl;
10134 bool ret;
10135 ++processing_template_decl;
10136 ret = (fn && DECL_LANG_SPECIFIC (fn)
10137 && DECL_TEMPLATE_INFO (fn)
10138 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
10139 --processing_template_decl;
10140 return ret;
10141 }
10142
10143 /* Returns true if T depends on any template parameter with level LEVEL. */
10144
10145 bool
10146 uses_template_parms_level (tree t, int level)
10147 {
10148 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
10149 /*include_nondeduced_p=*/true);
10150 }
10151
10152 /* Returns true if the signature of DECL depends on any template parameter from
10153 its enclosing class. */
10154
10155 bool
10156 uses_outer_template_parms (tree decl)
10157 {
10158 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
10159 if (depth == 0)
10160 return false;
10161 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
10162 &depth, NULL, /*include_nondeduced_p=*/true))
10163 return true;
10164 if (PRIMARY_TEMPLATE_P (decl)
10165 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10166 (DECL_TEMPLATE_PARMS (decl)),
10167 template_parm_outer_level,
10168 &depth, NULL, /*include_nondeduced_p=*/true))
10169 return true;
10170 tree ci = get_constraints (decl);
10171 if (ci)
10172 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
10173 if (ci && for_each_template_parm (ci, template_parm_outer_level,
10174 &depth, NULL, /*nondeduced*/true))
10175 return true;
10176 return false;
10177 }
10178
10179 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10180 ill-formed translation unit, i.e. a variable or function that isn't
10181 usable in a constant expression. */
10182
10183 static inline bool
10184 neglectable_inst_p (tree d)
10185 {
10186 return (d && DECL_P (d)
10187 && !undeduced_auto_decl (d)
10188 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
10189 : decl_maybe_constant_var_p (d)));
10190 }
10191
10192 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10193 neglectable and instantiated from within an erroneous instantiation. */
10194
10195 static bool
10196 limit_bad_template_recursion (tree decl)
10197 {
10198 struct tinst_level *lev = current_tinst_level;
10199 int errs = errorcount + sorrycount;
10200 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
10201 return false;
10202
10203 for (; lev; lev = lev->next)
10204 if (neglectable_inst_p (lev->maybe_get_node ()))
10205 break;
10206
10207 return (lev && errs > lev->errors);
10208 }
10209
10210 static int tinst_depth;
10211 extern int max_tinst_depth;
10212 int depth_reached;
10213
10214 static GTY(()) struct tinst_level *last_error_tinst_level;
10215
10216 /* We're starting to instantiate D; record the template instantiation context
10217 at LOC for diagnostics and to restore it later. */
10218
10219 static bool
10220 push_tinst_level_loc (tree tldcl, tree targs, location_t loc)
10221 {
10222 struct tinst_level *new_level;
10223
10224 if (tinst_depth >= max_tinst_depth)
10225 {
10226 /* Tell error.c not to try to instantiate any templates. */
10227 at_eof = 2;
10228 fatal_error (input_location,
10229 "template instantiation depth exceeds maximum of %d"
10230 " (use -ftemplate-depth= to increase the maximum)",
10231 max_tinst_depth);
10232 return false;
10233 }
10234
10235 /* If the current instantiation caused problems, don't let it instantiate
10236 anything else. Do allow deduction substitution and decls usable in
10237 constant expressions. */
10238 if (!targs && limit_bad_template_recursion (tldcl))
10239 return false;
10240
10241 /* When not -quiet, dump template instantiations other than functions, since
10242 announce_function will take care of those. */
10243 if (!quiet_flag && !targs
10244 && TREE_CODE (tldcl) != TREE_LIST
10245 && TREE_CODE (tldcl) != FUNCTION_DECL)
10246 fprintf (stderr, " %s", decl_as_string (tldcl, TFF_DECL_SPECIFIERS));
10247
10248 new_level = tinst_level_freelist ().alloc ();
10249 new_level->tldcl = tldcl;
10250 new_level->targs = targs;
10251 new_level->locus = loc;
10252 new_level->errors = errorcount + sorrycount;
10253 new_level->next = NULL;
10254 new_level->refcount = 0;
10255 set_refcount_ptr (new_level->next, current_tinst_level);
10256 set_refcount_ptr (current_tinst_level, new_level);
10257
10258 ++tinst_depth;
10259 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
10260 depth_reached = tinst_depth;
10261
10262 return true;
10263 }
10264
10265 /* We're starting substitution of TMPL<ARGS>; record the template
10266 substitution context for diagnostics and to restore it later. */
10267
10268 static bool
10269 push_tinst_level (tree tmpl, tree args)
10270 {
10271 return push_tinst_level_loc (tmpl, args, input_location);
10272 }
10273
10274 /* We're starting to instantiate D; record INPUT_LOCATION and the
10275 template instantiation context for diagnostics and to restore it
10276 later. */
10277
10278 bool
10279 push_tinst_level (tree d)
10280 {
10281 return push_tinst_level_loc (d, input_location);
10282 }
10283
10284 /* Likewise, but record LOC as the program location. */
10285
10286 bool
10287 push_tinst_level_loc (tree d, location_t loc)
10288 {
10289 gcc_assert (TREE_CODE (d) != TREE_LIST);
10290 return push_tinst_level_loc (d, NULL, loc);
10291 }
10292
10293 /* We're done instantiating this template; return to the instantiation
10294 context. */
10295
10296 void
10297 pop_tinst_level (void)
10298 {
10299 /* Restore the filename and line number stashed away when we started
10300 this instantiation. */
10301 input_location = current_tinst_level->locus;
10302 set_refcount_ptr (current_tinst_level, current_tinst_level->next);
10303 --tinst_depth;
10304 }
10305
10306 /* We're instantiating a deferred template; restore the template
10307 instantiation context in which the instantiation was requested, which
10308 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
10309
10310 static tree
10311 reopen_tinst_level (struct tinst_level *level)
10312 {
10313 struct tinst_level *t;
10314
10315 tinst_depth = 0;
10316 for (t = level; t; t = t->next)
10317 ++tinst_depth;
10318
10319 set_refcount_ptr (current_tinst_level, level);
10320 pop_tinst_level ();
10321 if (current_tinst_level)
10322 current_tinst_level->errors = errorcount+sorrycount;
10323 return level->maybe_get_node ();
10324 }
10325
10326 /* Returns the TINST_LEVEL which gives the original instantiation
10327 context. */
10328
10329 struct tinst_level *
10330 outermost_tinst_level (void)
10331 {
10332 struct tinst_level *level = current_tinst_level;
10333 if (level)
10334 while (level->next)
10335 level = level->next;
10336 return level;
10337 }
10338
10339 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
10340 vector of template arguments, as for tsubst.
10341
10342 Returns an appropriate tsubst'd friend declaration. */
10343
10344 static tree
10345 tsubst_friend_function (tree decl, tree args)
10346 {
10347 tree new_friend;
10348
10349 if (TREE_CODE (decl) == FUNCTION_DECL
10350 && DECL_TEMPLATE_INSTANTIATION (decl)
10351 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
10352 /* This was a friend declared with an explicit template
10353 argument list, e.g.:
10354
10355 friend void f<>(T);
10356
10357 to indicate that f was a template instantiation, not a new
10358 function declaration. Now, we have to figure out what
10359 instantiation of what template. */
10360 {
10361 tree template_id, arglist, fns;
10362 tree new_args;
10363 tree tmpl;
10364 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
10365
10366 /* Friend functions are looked up in the containing namespace scope.
10367 We must enter that scope, to avoid finding member functions of the
10368 current class with same name. */
10369 push_nested_namespace (ns);
10370 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
10371 tf_warning_or_error, NULL_TREE,
10372 /*integral_constant_expression_p=*/false);
10373 pop_nested_namespace (ns);
10374 arglist = tsubst (DECL_TI_ARGS (decl), args,
10375 tf_warning_or_error, NULL_TREE);
10376 template_id = lookup_template_function (fns, arglist);
10377
10378 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10379 tmpl = determine_specialization (template_id, new_friend,
10380 &new_args,
10381 /*need_member_template=*/0,
10382 TREE_VEC_LENGTH (args),
10383 tsk_none);
10384 return instantiate_template (tmpl, new_args, tf_error);
10385 }
10386
10387 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10388
10389 /* The NEW_FRIEND will look like an instantiation, to the
10390 compiler, but is not an instantiation from the point of view of
10391 the language. For example, we might have had:
10392
10393 template <class T> struct S {
10394 template <class U> friend void f(T, U);
10395 };
10396
10397 Then, in S<int>, template <class U> void f(int, U) is not an
10398 instantiation of anything. */
10399 if (new_friend == error_mark_node)
10400 return error_mark_node;
10401
10402 DECL_USE_TEMPLATE (new_friend) = 0;
10403 if (TREE_CODE (decl) == TEMPLATE_DECL)
10404 {
10405 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
10406 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
10407 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
10408 }
10409
10410 /* The mangled name for the NEW_FRIEND is incorrect. The function
10411 is not a template instantiation and should not be mangled like
10412 one. Therefore, we forget the mangling here; we'll recompute it
10413 later if we need it. */
10414 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
10415 {
10416 SET_DECL_RTL (new_friend, NULL);
10417 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
10418 }
10419
10420 if (DECL_NAMESPACE_SCOPE_P (new_friend))
10421 {
10422 tree old_decl;
10423 tree new_friend_template_info;
10424 tree new_friend_result_template_info;
10425 tree ns;
10426 int new_friend_is_defn;
10427
10428 /* We must save some information from NEW_FRIEND before calling
10429 duplicate decls since that function will free NEW_FRIEND if
10430 possible. */
10431 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
10432 new_friend_is_defn =
10433 (DECL_INITIAL (DECL_TEMPLATE_RESULT
10434 (template_for_substitution (new_friend)))
10435 != NULL_TREE);
10436 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
10437 {
10438 /* This declaration is a `primary' template. */
10439 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
10440
10441 new_friend_result_template_info
10442 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
10443 }
10444 else
10445 new_friend_result_template_info = NULL_TREE;
10446
10447 /* Inside pushdecl_namespace_level, we will push into the
10448 current namespace. However, the friend function should go
10449 into the namespace of the template. */
10450 ns = decl_namespace_context (new_friend);
10451 push_nested_namespace (ns);
10452 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
10453 pop_nested_namespace (ns);
10454
10455 if (old_decl == error_mark_node)
10456 return error_mark_node;
10457
10458 if (old_decl != new_friend)
10459 {
10460 /* This new friend declaration matched an existing
10461 declaration. For example, given:
10462
10463 template <class T> void f(T);
10464 template <class U> class C {
10465 template <class T> friend void f(T) {}
10466 };
10467
10468 the friend declaration actually provides the definition
10469 of `f', once C has been instantiated for some type. So,
10470 old_decl will be the out-of-class template declaration,
10471 while new_friend is the in-class definition.
10472
10473 But, if `f' was called before this point, the
10474 instantiation of `f' will have DECL_TI_ARGS corresponding
10475 to `T' but not to `U', references to which might appear
10476 in the definition of `f'. Previously, the most general
10477 template for an instantiation of `f' was the out-of-class
10478 version; now it is the in-class version. Therefore, we
10479 run through all specialization of `f', adding to their
10480 DECL_TI_ARGS appropriately. In particular, they need a
10481 new set of outer arguments, corresponding to the
10482 arguments for this class instantiation.
10483
10484 The same situation can arise with something like this:
10485
10486 friend void f(int);
10487 template <class T> class C {
10488 friend void f(T) {}
10489 };
10490
10491 when `C<int>' is instantiated. Now, `f(int)' is defined
10492 in the class. */
10493
10494 if (!new_friend_is_defn)
10495 /* On the other hand, if the in-class declaration does
10496 *not* provide a definition, then we don't want to alter
10497 existing definitions. We can just leave everything
10498 alone. */
10499 ;
10500 else
10501 {
10502 tree new_template = TI_TEMPLATE (new_friend_template_info);
10503 tree new_args = TI_ARGS (new_friend_template_info);
10504
10505 /* Overwrite whatever template info was there before, if
10506 any, with the new template information pertaining to
10507 the declaration. */
10508 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
10509
10510 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
10511 {
10512 /* We should have called reregister_specialization in
10513 duplicate_decls. */
10514 gcc_assert (retrieve_specialization (new_template,
10515 new_args, 0)
10516 == old_decl);
10517
10518 /* Instantiate it if the global has already been used. */
10519 if (DECL_ODR_USED (old_decl))
10520 instantiate_decl (old_decl, /*defer_ok=*/true,
10521 /*expl_inst_class_mem_p=*/false);
10522 }
10523 else
10524 {
10525 tree t;
10526
10527 /* Indicate that the old function template is a partial
10528 instantiation. */
10529 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
10530 = new_friend_result_template_info;
10531
10532 gcc_assert (new_template
10533 == most_general_template (new_template));
10534 gcc_assert (new_template != old_decl);
10535
10536 /* Reassign any specializations already in the hash table
10537 to the new more general template, and add the
10538 additional template args. */
10539 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
10540 t != NULL_TREE;
10541 t = TREE_CHAIN (t))
10542 {
10543 tree spec = TREE_VALUE (t);
10544 spec_entry elt;
10545
10546 elt.tmpl = old_decl;
10547 elt.args = DECL_TI_ARGS (spec);
10548 elt.spec = NULL_TREE;
10549
10550 decl_specializations->remove_elt (&elt);
10551
10552 DECL_TI_ARGS (spec)
10553 = add_outermost_template_args (new_args,
10554 DECL_TI_ARGS (spec));
10555
10556 register_specialization
10557 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
10558
10559 }
10560 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
10561 }
10562 }
10563
10564 /* The information from NEW_FRIEND has been merged into OLD_DECL
10565 by duplicate_decls. */
10566 new_friend = old_decl;
10567 }
10568 }
10569 else
10570 {
10571 tree context = DECL_CONTEXT (new_friend);
10572 bool dependent_p;
10573
10574 /* In the code
10575 template <class T> class C {
10576 template <class U> friend void C1<U>::f (); // case 1
10577 friend void C2<T>::f (); // case 2
10578 };
10579 we only need to make sure CONTEXT is a complete type for
10580 case 2. To distinguish between the two cases, we note that
10581 CONTEXT of case 1 remains dependent type after tsubst while
10582 this isn't true for case 2. */
10583 ++processing_template_decl;
10584 dependent_p = dependent_type_p (context);
10585 --processing_template_decl;
10586
10587 if (!dependent_p
10588 && !complete_type_or_else (context, NULL_TREE))
10589 return error_mark_node;
10590
10591 if (COMPLETE_TYPE_P (context))
10592 {
10593 tree fn = new_friend;
10594 /* do_friend adds the TEMPLATE_DECL for any member friend
10595 template even if it isn't a member template, i.e.
10596 template <class T> friend A<T>::f();
10597 Look through it in that case. */
10598 if (TREE_CODE (fn) == TEMPLATE_DECL
10599 && !PRIMARY_TEMPLATE_P (fn))
10600 fn = DECL_TEMPLATE_RESULT (fn);
10601 /* Check to see that the declaration is really present, and,
10602 possibly obtain an improved declaration. */
10603 fn = check_classfn (context, fn, NULL_TREE);
10604
10605 if (fn)
10606 new_friend = fn;
10607 }
10608 }
10609
10610 return new_friend;
10611 }
10612
10613 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
10614 template arguments, as for tsubst.
10615
10616 Returns an appropriate tsubst'd friend type or error_mark_node on
10617 failure. */
10618
10619 static tree
10620 tsubst_friend_class (tree friend_tmpl, tree args)
10621 {
10622 tree tmpl;
10623
10624 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
10625 {
10626 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
10627 return TREE_TYPE (tmpl);
10628 }
10629
10630 tree context = CP_DECL_CONTEXT (friend_tmpl);
10631 if (TREE_CODE (context) == NAMESPACE_DECL)
10632 push_nested_namespace (context);
10633 else
10634 push_nested_class (context);
10635
10636 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), /*prefer_type=*/false,
10637 /*non_class=*/false, /*block_p=*/false,
10638 /*namespaces_only=*/false, LOOKUP_HIDDEN);
10639
10640 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
10641 {
10642 /* The friend template has already been declared. Just
10643 check to see that the declarations match, and install any new
10644 default parameters. We must tsubst the default parameters,
10645 of course. We only need the innermost template parameters
10646 because that is all that redeclare_class_template will look
10647 at. */
10648 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
10649 > TMPL_ARGS_DEPTH (args))
10650 {
10651 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
10652 args, tf_warning_or_error);
10653 location_t saved_input_location = input_location;
10654 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
10655 tree cons = get_constraints (tmpl);
10656 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
10657 input_location = saved_input_location;
10658 }
10659 }
10660 else
10661 {
10662 /* The friend template has not already been declared. In this
10663 case, the instantiation of the template class will cause the
10664 injection of this template into the namespace scope. */
10665 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
10666
10667 if (tmpl != error_mark_node)
10668 {
10669 /* The new TMPL is not an instantiation of anything, so we
10670 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
10671 for the new type because that is supposed to be the
10672 corresponding template decl, i.e., TMPL. */
10673 DECL_USE_TEMPLATE (tmpl) = 0;
10674 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
10675 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
10676 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
10677 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
10678
10679 /* It is hidden. */
10680 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl));
10681 DECL_ANTICIPATED (tmpl)
10682 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl)) = true;
10683
10684 /* Inject this template into the enclosing namspace scope. */
10685 tmpl = pushdecl_namespace_level (tmpl, true);
10686 }
10687 }
10688
10689 if (TREE_CODE (context) == NAMESPACE_DECL)
10690 pop_nested_namespace (context);
10691 else
10692 pop_nested_class ();
10693
10694 return TREE_TYPE (tmpl);
10695 }
10696
10697 /* Returns zero if TYPE cannot be completed later due to circularity.
10698 Otherwise returns one. */
10699
10700 static int
10701 can_complete_type_without_circularity (tree type)
10702 {
10703 if (type == NULL_TREE || type == error_mark_node)
10704 return 0;
10705 else if (COMPLETE_TYPE_P (type))
10706 return 1;
10707 else if (TREE_CODE (type) == ARRAY_TYPE)
10708 return can_complete_type_without_circularity (TREE_TYPE (type));
10709 else if (CLASS_TYPE_P (type)
10710 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
10711 return 0;
10712 else
10713 return 1;
10714 }
10715
10716 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
10717 tsubst_flags_t, tree);
10718
10719 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10720 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10721
10722 static tree
10723 tsubst_attribute (tree t, tree *decl_p, tree args,
10724 tsubst_flags_t complain, tree in_decl)
10725 {
10726 gcc_assert (ATTR_IS_DEPENDENT (t));
10727
10728 tree val = TREE_VALUE (t);
10729 if (val == NULL_TREE)
10730 /* Nothing to do. */;
10731 else if ((flag_openmp || flag_openmp_simd)
10732 && is_attribute_p ("omp declare simd",
10733 get_attribute_name (t)))
10734 {
10735 tree clauses = TREE_VALUE (val);
10736 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
10737 complain, in_decl);
10738 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
10739 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
10740 tree parms = DECL_ARGUMENTS (*decl_p);
10741 clauses
10742 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
10743 if (clauses)
10744 val = build_tree_list (NULL_TREE, clauses);
10745 else
10746 val = NULL_TREE;
10747 }
10748 /* If the first attribute argument is an identifier, don't
10749 pass it through tsubst. Attributes like mode, format,
10750 cleanup and several target specific attributes expect it
10751 unmodified. */
10752 else if (attribute_takes_identifier_p (get_attribute_name (t)))
10753 {
10754 tree chain
10755 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
10756 /*integral_constant_expression_p=*/false);
10757 if (chain != TREE_CHAIN (val))
10758 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
10759 }
10760 else if (PACK_EXPANSION_P (val))
10761 {
10762 /* An attribute pack expansion. */
10763 tree purp = TREE_PURPOSE (t);
10764 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
10765 if (pack == error_mark_node)
10766 return error_mark_node;
10767 int len = TREE_VEC_LENGTH (pack);
10768 tree list = NULL_TREE;
10769 tree *q = &list;
10770 for (int i = 0; i < len; ++i)
10771 {
10772 tree elt = TREE_VEC_ELT (pack, i);
10773 *q = build_tree_list (purp, elt);
10774 q = &TREE_CHAIN (*q);
10775 }
10776 return list;
10777 }
10778 else
10779 val = tsubst_expr (val, args, complain, in_decl,
10780 /*integral_constant_expression_p=*/false);
10781
10782 if (val != TREE_VALUE (t))
10783 return build_tree_list (TREE_PURPOSE (t), val);
10784 return t;
10785 }
10786
10787 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10788 unchanged or a new TREE_LIST chain. */
10789
10790 static tree
10791 tsubst_attributes (tree attributes, tree args,
10792 tsubst_flags_t complain, tree in_decl)
10793 {
10794 tree last_dep = NULL_TREE;
10795
10796 for (tree t = attributes; t; t = TREE_CHAIN (t))
10797 if (ATTR_IS_DEPENDENT (t))
10798 {
10799 last_dep = t;
10800 attributes = copy_list (attributes);
10801 break;
10802 }
10803
10804 if (last_dep)
10805 for (tree *p = &attributes; *p; )
10806 {
10807 tree t = *p;
10808 if (ATTR_IS_DEPENDENT (t))
10809 {
10810 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
10811 if (subst != t)
10812 {
10813 *p = subst;
10814 while (*p)
10815 p = &TREE_CHAIN (*p);
10816 *p = TREE_CHAIN (t);
10817 continue;
10818 }
10819 }
10820 p = &TREE_CHAIN (*p);
10821 }
10822
10823 return attributes;
10824 }
10825
10826 /* Apply any attributes which had to be deferred until instantiation
10827 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10828 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10829
10830 static void
10831 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
10832 tree args, tsubst_flags_t complain, tree in_decl)
10833 {
10834 tree last_dep = NULL_TREE;
10835 tree t;
10836 tree *p;
10837
10838 if (attributes == NULL_TREE)
10839 return;
10840
10841 if (DECL_P (*decl_p))
10842 {
10843 if (TREE_TYPE (*decl_p) == error_mark_node)
10844 return;
10845 p = &DECL_ATTRIBUTES (*decl_p);
10846 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10847 to our attributes parameter. */
10848 gcc_assert (*p == attributes);
10849 }
10850 else
10851 {
10852 p = &TYPE_ATTRIBUTES (*decl_p);
10853 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10854 lookup_template_class_1, and should be preserved. */
10855 gcc_assert (*p != attributes);
10856 while (*p)
10857 p = &TREE_CHAIN (*p);
10858 }
10859
10860 for (t = attributes; t; t = TREE_CHAIN (t))
10861 if (ATTR_IS_DEPENDENT (t))
10862 {
10863 last_dep = t;
10864 attributes = copy_list (attributes);
10865 break;
10866 }
10867
10868 *p = attributes;
10869 if (last_dep)
10870 {
10871 tree late_attrs = NULL_TREE;
10872 tree *q = &late_attrs;
10873
10874 for (; *p; )
10875 {
10876 t = *p;
10877 if (ATTR_IS_DEPENDENT (t))
10878 {
10879 *p = TREE_CHAIN (t);
10880 TREE_CHAIN (t) = NULL_TREE;
10881 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
10882 while (*q)
10883 q = &TREE_CHAIN (*q);
10884 }
10885 else
10886 p = &TREE_CHAIN (t);
10887 }
10888
10889 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
10890 }
10891 }
10892
10893 /* Perform (or defer) access check for typedefs that were referenced
10894 from within the template TMPL code.
10895 This is a subroutine of instantiate_decl and instantiate_class_template.
10896 TMPL is the template to consider and TARGS is the list of arguments of
10897 that template. */
10898
10899 static void
10900 perform_typedefs_access_check (tree tmpl, tree targs)
10901 {
10902 location_t saved_location;
10903 unsigned i;
10904 qualified_typedef_usage_t *iter;
10905
10906 if (!tmpl
10907 || (!CLASS_TYPE_P (tmpl)
10908 && TREE_CODE (tmpl) != FUNCTION_DECL))
10909 return;
10910
10911 saved_location = input_location;
10912 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
10913 {
10914 tree type_decl = iter->typedef_decl;
10915 tree type_scope = iter->context;
10916
10917 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
10918 continue;
10919
10920 if (uses_template_parms (type_decl))
10921 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
10922 if (uses_template_parms (type_scope))
10923 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
10924
10925 /* Make access check error messages point to the location
10926 of the use of the typedef. */
10927 input_location = iter->locus;
10928 perform_or_defer_access_check (TYPE_BINFO (type_scope),
10929 type_decl, type_decl,
10930 tf_warning_or_error);
10931 }
10932 input_location = saved_location;
10933 }
10934
10935 static tree
10936 instantiate_class_template_1 (tree type)
10937 {
10938 tree templ, args, pattern, t, member;
10939 tree typedecl;
10940 tree pbinfo;
10941 tree base_list;
10942 unsigned int saved_maximum_field_alignment;
10943 tree fn_context;
10944
10945 if (type == error_mark_node)
10946 return error_mark_node;
10947
10948 if (COMPLETE_OR_OPEN_TYPE_P (type)
10949 || uses_template_parms (type))
10950 return type;
10951
10952 /* Figure out which template is being instantiated. */
10953 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
10954 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
10955
10956 /* Mark the type as in the process of being defined. */
10957 TYPE_BEING_DEFINED (type) = 1;
10958
10959 /* We may be in the middle of deferred access check. Disable
10960 it now. */
10961 deferring_access_check_sentinel acs (dk_no_deferred);
10962
10963 /* Determine what specialization of the original template to
10964 instantiate. */
10965 t = most_specialized_partial_spec (type, tf_warning_or_error);
10966 if (t == error_mark_node)
10967 return error_mark_node;
10968 else if (t)
10969 {
10970 /* This TYPE is actually an instantiation of a partial
10971 specialization. We replace the innermost set of ARGS with
10972 the arguments appropriate for substitution. For example,
10973 given:
10974
10975 template <class T> struct S {};
10976 template <class T> struct S<T*> {};
10977
10978 and supposing that we are instantiating S<int*>, ARGS will
10979 presently be {int*} -- but we need {int}. */
10980 pattern = TREE_TYPE (t);
10981 args = TREE_PURPOSE (t);
10982 }
10983 else
10984 {
10985 pattern = TREE_TYPE (templ);
10986 args = CLASSTYPE_TI_ARGS (type);
10987 }
10988
10989 /* If the template we're instantiating is incomplete, then clearly
10990 there's nothing we can do. */
10991 if (!COMPLETE_TYPE_P (pattern))
10992 {
10993 /* We can try again later. */
10994 TYPE_BEING_DEFINED (type) = 0;
10995 return type;
10996 }
10997
10998 /* If we've recursively instantiated too many templates, stop. */
10999 if (! push_tinst_level (type))
11000 return type;
11001
11002 int saved_unevaluated_operand = cp_unevaluated_operand;
11003 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11004
11005 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
11006 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
11007 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
11008 fn_context = error_mark_node;
11009 if (!fn_context)
11010 push_to_top_level ();
11011 else
11012 {
11013 cp_unevaluated_operand = 0;
11014 c_inhibit_evaluation_warnings = 0;
11015 }
11016 /* Use #pragma pack from the template context. */
11017 saved_maximum_field_alignment = maximum_field_alignment;
11018 maximum_field_alignment = TYPE_PRECISION (pattern);
11019
11020 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
11021
11022 /* Set the input location to the most specialized template definition.
11023 This is needed if tsubsting causes an error. */
11024 typedecl = TYPE_MAIN_DECL (pattern);
11025 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
11026 DECL_SOURCE_LOCATION (typedecl);
11027
11028 TYPE_PACKED (type) = TYPE_PACKED (pattern);
11029 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
11030 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
11031 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
11032 if (ANON_AGGR_TYPE_P (pattern))
11033 SET_ANON_AGGR_TYPE_P (type);
11034 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
11035 {
11036 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
11037 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
11038 /* Adjust visibility for template arguments. */
11039 determine_visibility (TYPE_MAIN_DECL (type));
11040 }
11041 if (CLASS_TYPE_P (type))
11042 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
11043
11044 pbinfo = TYPE_BINFO (pattern);
11045
11046 /* We should never instantiate a nested class before its enclosing
11047 class; we need to look up the nested class by name before we can
11048 instantiate it, and that lookup should instantiate the enclosing
11049 class. */
11050 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
11051 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
11052
11053 base_list = NULL_TREE;
11054 if (BINFO_N_BASE_BINFOS (pbinfo))
11055 {
11056 tree pbase_binfo;
11057 tree pushed_scope;
11058 int i;
11059
11060 /* We must enter the scope containing the type, as that is where
11061 the accessibility of types named in dependent bases are
11062 looked up from. */
11063 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
11064
11065 /* Substitute into each of the bases to determine the actual
11066 basetypes. */
11067 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
11068 {
11069 tree base;
11070 tree access = BINFO_BASE_ACCESS (pbinfo, i);
11071 tree expanded_bases = NULL_TREE;
11072 int idx, len = 1;
11073
11074 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
11075 {
11076 expanded_bases =
11077 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
11078 args, tf_error, NULL_TREE);
11079 if (expanded_bases == error_mark_node)
11080 continue;
11081
11082 len = TREE_VEC_LENGTH (expanded_bases);
11083 }
11084
11085 for (idx = 0; idx < len; idx++)
11086 {
11087 if (expanded_bases)
11088 /* Extract the already-expanded base class. */
11089 base = TREE_VEC_ELT (expanded_bases, idx);
11090 else
11091 /* Substitute to figure out the base class. */
11092 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
11093 NULL_TREE);
11094
11095 if (base == error_mark_node)
11096 continue;
11097
11098 base_list = tree_cons (access, base, base_list);
11099 if (BINFO_VIRTUAL_P (pbase_binfo))
11100 TREE_TYPE (base_list) = integer_type_node;
11101 }
11102 }
11103
11104 /* The list is now in reverse order; correct that. */
11105 base_list = nreverse (base_list);
11106
11107 if (pushed_scope)
11108 pop_scope (pushed_scope);
11109 }
11110 /* Now call xref_basetypes to set up all the base-class
11111 information. */
11112 xref_basetypes (type, base_list);
11113
11114 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
11115 (int) ATTR_FLAG_TYPE_IN_PLACE,
11116 args, tf_error, NULL_TREE);
11117 fixup_attribute_variants (type);
11118
11119 /* Now that our base classes are set up, enter the scope of the
11120 class, so that name lookups into base classes, etc. will work
11121 correctly. This is precisely analogous to what we do in
11122 begin_class_definition when defining an ordinary non-template
11123 class, except we also need to push the enclosing classes. */
11124 push_nested_class (type);
11125
11126 /* Now members are processed in the order of declaration. */
11127 for (member = CLASSTYPE_DECL_LIST (pattern);
11128 member; member = TREE_CHAIN (member))
11129 {
11130 tree t = TREE_VALUE (member);
11131
11132 if (TREE_PURPOSE (member))
11133 {
11134 if (TYPE_P (t))
11135 {
11136 if (LAMBDA_TYPE_P (t))
11137 /* A closure type for a lambda in an NSDMI or default argument.
11138 Ignore it; it will be regenerated when needed. */
11139 continue;
11140
11141 /* Build new CLASSTYPE_NESTED_UTDS. */
11142
11143 tree newtag;
11144 bool class_template_p;
11145
11146 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
11147 && TYPE_LANG_SPECIFIC (t)
11148 && CLASSTYPE_IS_TEMPLATE (t));
11149 /* If the member is a class template, then -- even after
11150 substitution -- there may be dependent types in the
11151 template argument list for the class. We increment
11152 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11153 that function will assume that no types are dependent
11154 when outside of a template. */
11155 if (class_template_p)
11156 ++processing_template_decl;
11157 newtag = tsubst (t, args, tf_error, NULL_TREE);
11158 if (class_template_p)
11159 --processing_template_decl;
11160 if (newtag == error_mark_node)
11161 continue;
11162
11163 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
11164 {
11165 tree name = TYPE_IDENTIFIER (t);
11166
11167 if (class_template_p)
11168 /* Unfortunately, lookup_template_class sets
11169 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11170 instantiation (i.e., for the type of a member
11171 template class nested within a template class.)
11172 This behavior is required for
11173 maybe_process_partial_specialization to work
11174 correctly, but is not accurate in this case;
11175 the TAG is not an instantiation of anything.
11176 (The corresponding TEMPLATE_DECL is an
11177 instantiation, but the TYPE is not.) */
11178 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
11179
11180 /* Now, we call pushtag to put this NEWTAG into the scope of
11181 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11182 pushtag calling push_template_decl. We don't have to do
11183 this for enums because it will already have been done in
11184 tsubst_enum. */
11185 if (name)
11186 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
11187 pushtag (name, newtag, /*tag_scope=*/ts_current);
11188 }
11189 }
11190 else if (DECL_DECLARES_FUNCTION_P (t))
11191 {
11192 tree r;
11193
11194 if (TREE_CODE (t) == TEMPLATE_DECL)
11195 ++processing_template_decl;
11196 r = tsubst (t, args, tf_error, NULL_TREE);
11197 if (TREE_CODE (t) == TEMPLATE_DECL)
11198 --processing_template_decl;
11199 set_current_access_from_decl (r);
11200 finish_member_declaration (r);
11201 /* Instantiate members marked with attribute used. */
11202 if (r != error_mark_node && DECL_PRESERVE_P (r))
11203 mark_used (r);
11204 if (TREE_CODE (r) == FUNCTION_DECL
11205 && DECL_OMP_DECLARE_REDUCTION_P (r))
11206 cp_check_omp_declare_reduction (r);
11207 }
11208 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
11209 && LAMBDA_TYPE_P (TREE_TYPE (t)))
11210 /* A closure type for a lambda in an NSDMI or default argument.
11211 Ignore it; it will be regenerated when needed. */;
11212 else
11213 {
11214 /* Build new TYPE_FIELDS. */
11215 if (TREE_CODE (t) == STATIC_ASSERT)
11216 {
11217 tree condition;
11218
11219 ++c_inhibit_evaluation_warnings;
11220 condition =
11221 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
11222 tf_warning_or_error, NULL_TREE,
11223 /*integral_constant_expression_p=*/true);
11224 --c_inhibit_evaluation_warnings;
11225
11226 finish_static_assert (condition,
11227 STATIC_ASSERT_MESSAGE (t),
11228 STATIC_ASSERT_SOURCE_LOCATION (t),
11229 /*member_p=*/true);
11230 }
11231 else if (TREE_CODE (t) != CONST_DECL)
11232 {
11233 tree r;
11234 tree vec = NULL_TREE;
11235 int len = 1;
11236
11237 /* The file and line for this declaration, to
11238 assist in error message reporting. Since we
11239 called push_tinst_level above, we don't need to
11240 restore these. */
11241 input_location = DECL_SOURCE_LOCATION (t);
11242
11243 if (TREE_CODE (t) == TEMPLATE_DECL)
11244 ++processing_template_decl;
11245 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
11246 if (TREE_CODE (t) == TEMPLATE_DECL)
11247 --processing_template_decl;
11248
11249 if (TREE_CODE (r) == TREE_VEC)
11250 {
11251 /* A capture pack became multiple fields. */
11252 vec = r;
11253 len = TREE_VEC_LENGTH (vec);
11254 }
11255
11256 for (int i = 0; i < len; ++i)
11257 {
11258 if (vec)
11259 r = TREE_VEC_ELT (vec, i);
11260 if (VAR_P (r))
11261 {
11262 /* In [temp.inst]:
11263
11264 [t]he initialization (and any associated
11265 side-effects) of a static data member does
11266 not occur unless the static data member is
11267 itself used in a way that requires the
11268 definition of the static data member to
11269 exist.
11270
11271 Therefore, we do not substitute into the
11272 initialized for the static data member here. */
11273 finish_static_data_member_decl
11274 (r,
11275 /*init=*/NULL_TREE,
11276 /*init_const_expr_p=*/false,
11277 /*asmspec_tree=*/NULL_TREE,
11278 /*flags=*/0);
11279 /* Instantiate members marked with attribute used. */
11280 if (r != error_mark_node && DECL_PRESERVE_P (r))
11281 mark_used (r);
11282 }
11283 else if (TREE_CODE (r) == FIELD_DECL)
11284 {
11285 /* Determine whether R has a valid type and can be
11286 completed later. If R is invalid, then its type
11287 is replaced by error_mark_node. */
11288 tree rtype = TREE_TYPE (r);
11289 if (can_complete_type_without_circularity (rtype))
11290 complete_type (rtype);
11291
11292 if (!complete_or_array_type_p (rtype))
11293 {
11294 /* If R's type couldn't be completed and
11295 it isn't a flexible array member (whose
11296 type is incomplete by definition) give
11297 an error. */
11298 cxx_incomplete_type_error (r, rtype);
11299 TREE_TYPE (r) = error_mark_node;
11300 }
11301 else if (TREE_CODE (rtype) == ARRAY_TYPE
11302 && TYPE_DOMAIN (rtype) == NULL_TREE
11303 && (TREE_CODE (type) == UNION_TYPE
11304 || TREE_CODE (type) == QUAL_UNION_TYPE))
11305 {
11306 error ("flexible array member %qD in union", r);
11307 TREE_TYPE (r) = error_mark_node;
11308 }
11309 }
11310
11311 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
11312 such a thing will already have been added to the field
11313 list by tsubst_enum in finish_member_declaration in the
11314 CLASSTYPE_NESTED_UTDS case above. */
11315 if (!(TREE_CODE (r) == TYPE_DECL
11316 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
11317 && DECL_ARTIFICIAL (r)))
11318 {
11319 set_current_access_from_decl (r);
11320 finish_member_declaration (r);
11321 }
11322 }
11323 }
11324 }
11325 }
11326 else
11327 {
11328 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
11329 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
11330 {
11331 /* Build new CLASSTYPE_FRIEND_CLASSES. */
11332
11333 tree friend_type = t;
11334 bool adjust_processing_template_decl = false;
11335
11336 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11337 {
11338 /* template <class T> friend class C; */
11339 friend_type = tsubst_friend_class (friend_type, args);
11340 adjust_processing_template_decl = true;
11341 }
11342 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
11343 {
11344 /* template <class T> friend class C::D; */
11345 friend_type = tsubst (friend_type, args,
11346 tf_warning_or_error, NULL_TREE);
11347 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11348 friend_type = TREE_TYPE (friend_type);
11349 adjust_processing_template_decl = true;
11350 }
11351 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
11352 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
11353 {
11354 /* This could be either
11355
11356 friend class T::C;
11357
11358 when dependent_type_p is false or
11359
11360 template <class U> friend class T::C;
11361
11362 otherwise. */
11363 /* Bump processing_template_decl in case this is something like
11364 template <class T> friend struct A<T>::B. */
11365 ++processing_template_decl;
11366 friend_type = tsubst (friend_type, args,
11367 tf_warning_or_error, NULL_TREE);
11368 if (dependent_type_p (friend_type))
11369 adjust_processing_template_decl = true;
11370 --processing_template_decl;
11371 }
11372 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
11373 && !CLASSTYPE_USE_TEMPLATE (friend_type)
11374 && TYPE_HIDDEN_P (friend_type))
11375 {
11376 /* friend class C;
11377
11378 where C hasn't been declared yet. Let's lookup name
11379 from namespace scope directly, bypassing any name that
11380 come from dependent base class. */
11381 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
11382
11383 /* The call to xref_tag_from_type does injection for friend
11384 classes. */
11385 push_nested_namespace (ns);
11386 friend_type =
11387 xref_tag_from_type (friend_type, NULL_TREE,
11388 /*tag_scope=*/ts_current);
11389 pop_nested_namespace (ns);
11390 }
11391 else if (uses_template_parms (friend_type))
11392 /* friend class C<T>; */
11393 friend_type = tsubst (friend_type, args,
11394 tf_warning_or_error, NULL_TREE);
11395 /* Otherwise it's
11396
11397 friend class C;
11398
11399 where C is already declared or
11400
11401 friend class C<int>;
11402
11403 We don't have to do anything in these cases. */
11404
11405 if (adjust_processing_template_decl)
11406 /* Trick make_friend_class into realizing that the friend
11407 we're adding is a template, not an ordinary class. It's
11408 important that we use make_friend_class since it will
11409 perform some error-checking and output cross-reference
11410 information. */
11411 ++processing_template_decl;
11412
11413 if (friend_type != error_mark_node)
11414 make_friend_class (type, friend_type, /*complain=*/false);
11415
11416 if (adjust_processing_template_decl)
11417 --processing_template_decl;
11418 }
11419 else
11420 {
11421 /* Build new DECL_FRIENDLIST. */
11422 tree r;
11423
11424 /* The file and line for this declaration, to
11425 assist in error message reporting. Since we
11426 called push_tinst_level above, we don't need to
11427 restore these. */
11428 input_location = DECL_SOURCE_LOCATION (t);
11429
11430 if (TREE_CODE (t) == TEMPLATE_DECL)
11431 {
11432 ++processing_template_decl;
11433 push_deferring_access_checks (dk_no_check);
11434 }
11435
11436 r = tsubst_friend_function (t, args);
11437 add_friend (type, r, /*complain=*/false);
11438 if (TREE_CODE (t) == TEMPLATE_DECL)
11439 {
11440 pop_deferring_access_checks ();
11441 --processing_template_decl;
11442 }
11443 }
11444 }
11445 }
11446
11447 if (fn_context)
11448 {
11449 /* Restore these before substituting into the lambda capture
11450 initializers. */
11451 cp_unevaluated_operand = saved_unevaluated_operand;
11452 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
11453 }
11454
11455 /* Set the file and line number information to whatever is given for
11456 the class itself. This puts error messages involving generated
11457 implicit functions at a predictable point, and the same point
11458 that would be used for non-template classes. */
11459 input_location = DECL_SOURCE_LOCATION (typedecl);
11460
11461 unreverse_member_declarations (type);
11462 finish_struct_1 (type);
11463 TYPE_BEING_DEFINED (type) = 0;
11464
11465 /* We don't instantiate default arguments for member functions. 14.7.1:
11466
11467 The implicit instantiation of a class template specialization causes
11468 the implicit instantiation of the declarations, but not of the
11469 definitions or default arguments, of the class member functions,
11470 member classes, static data members and member templates.... */
11471
11472 /* Some typedefs referenced from within the template code need to be access
11473 checked at template instantiation time, i.e now. These types were
11474 added to the template at parsing time. Let's get those and perform
11475 the access checks then. */
11476 perform_typedefs_access_check (pattern, args);
11477 perform_deferred_access_checks (tf_warning_or_error);
11478 pop_nested_class ();
11479 maximum_field_alignment = saved_maximum_field_alignment;
11480 if (!fn_context)
11481 pop_from_top_level ();
11482 pop_tinst_level ();
11483
11484 /* The vtable for a template class can be emitted in any translation
11485 unit in which the class is instantiated. When there is no key
11486 method, however, finish_struct_1 will already have added TYPE to
11487 the keyed_classes. */
11488 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
11489 vec_safe_push (keyed_classes, type);
11490
11491 return type;
11492 }
11493
11494 /* Wrapper for instantiate_class_template_1. */
11495
11496 tree
11497 instantiate_class_template (tree type)
11498 {
11499 tree ret;
11500 timevar_push (TV_TEMPLATE_INST);
11501 ret = instantiate_class_template_1 (type);
11502 timevar_pop (TV_TEMPLATE_INST);
11503 return ret;
11504 }
11505
11506 static tree
11507 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11508 {
11509 tree r;
11510
11511 if (!t)
11512 r = t;
11513 else if (TYPE_P (t))
11514 r = tsubst (t, args, complain, in_decl);
11515 else
11516 {
11517 if (!(complain & tf_warning))
11518 ++c_inhibit_evaluation_warnings;
11519 r = tsubst_expr (t, args, complain, in_decl,
11520 /*integral_constant_expression_p=*/true);
11521 if (!(complain & tf_warning))
11522 --c_inhibit_evaluation_warnings;
11523 }
11524 return r;
11525 }
11526
11527 /* Given a function parameter pack TMPL_PARM and some function parameters
11528 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
11529 and set *SPEC_P to point at the next point in the list. */
11530
11531 tree
11532 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
11533 {
11534 /* Collect all of the extra "packed" parameters into an
11535 argument pack. */
11536 tree parmvec;
11537 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
11538 tree spec_parm = *spec_p;
11539 int i, len;
11540
11541 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
11542 if (tmpl_parm
11543 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
11544 break;
11545
11546 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
11547 parmvec = make_tree_vec (len);
11548 spec_parm = *spec_p;
11549 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
11550 {
11551 tree elt = spec_parm;
11552 if (DECL_PACK_P (elt))
11553 elt = make_pack_expansion (elt);
11554 TREE_VEC_ELT (parmvec, i) = elt;
11555 }
11556
11557 /* Build the argument packs. */
11558 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
11559 *spec_p = spec_parm;
11560
11561 return argpack;
11562 }
11563
11564 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
11565 NONTYPE_ARGUMENT_PACK. */
11566
11567 static tree
11568 make_fnparm_pack (tree spec_parm)
11569 {
11570 return extract_fnparm_pack (NULL_TREE, &spec_parm);
11571 }
11572
11573 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
11574 pack expansion with no extra args, 2 if it has extra args, or 0
11575 if it is not a pack expansion. */
11576
11577 static int
11578 argument_pack_element_is_expansion_p (tree arg_pack, int i)
11579 {
11580 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
11581 if (i >= TREE_VEC_LENGTH (vec))
11582 return 0;
11583 tree elt = TREE_VEC_ELT (vec, i);
11584 if (DECL_P (elt))
11585 /* A decl pack is itself an expansion. */
11586 elt = TREE_TYPE (elt);
11587 if (!PACK_EXPANSION_P (elt))
11588 return 0;
11589 if (PACK_EXPANSION_EXTRA_ARGS (elt))
11590 return 2;
11591 return 1;
11592 }
11593
11594
11595 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11596
11597 static tree
11598 make_argument_pack_select (tree arg_pack, unsigned index)
11599 {
11600 tree aps = make_node (ARGUMENT_PACK_SELECT);
11601
11602 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
11603 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11604
11605 return aps;
11606 }
11607
11608 /* This is a subroutine of tsubst_pack_expansion.
11609
11610 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11611 mechanism to store the (non complete list of) arguments of the
11612 substitution and return a non substituted pack expansion, in order
11613 to wait for when we have enough arguments to really perform the
11614 substitution. */
11615
11616 static bool
11617 use_pack_expansion_extra_args_p (tree parm_packs,
11618 int arg_pack_len,
11619 bool has_empty_arg)
11620 {
11621 /* If one pack has an expansion and another pack has a normal
11622 argument or if one pack has an empty argument and an another
11623 one hasn't then tsubst_pack_expansion cannot perform the
11624 substitution and need to fall back on the
11625 PACK_EXPANSION_EXTRA mechanism. */
11626 if (parm_packs == NULL_TREE)
11627 return false;
11628 else if (has_empty_arg)
11629 return true;
11630
11631 bool has_expansion_arg = false;
11632 for (int i = 0 ; i < arg_pack_len; ++i)
11633 {
11634 bool has_non_expansion_arg = false;
11635 for (tree parm_pack = parm_packs;
11636 parm_pack;
11637 parm_pack = TREE_CHAIN (parm_pack))
11638 {
11639 tree arg = TREE_VALUE (parm_pack);
11640
11641 int exp = argument_pack_element_is_expansion_p (arg, i);
11642 if (exp == 2)
11643 /* We can't substitute a pack expansion with extra args into
11644 our pattern. */
11645 return true;
11646 else if (exp)
11647 has_expansion_arg = true;
11648 else
11649 has_non_expansion_arg = true;
11650 }
11651
11652 if (has_expansion_arg && has_non_expansion_arg)
11653 return true;
11654 }
11655 return false;
11656 }
11657
11658 /* [temp.variadic]/6 says that:
11659
11660 The instantiation of a pack expansion [...]
11661 produces a list E1,E2, ..., En, where N is the number of elements
11662 in the pack expansion parameters.
11663
11664 This subroutine of tsubst_pack_expansion produces one of these Ei.
11665
11666 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11667 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11668 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11669 INDEX is the index 'i' of the element Ei to produce. ARGS,
11670 COMPLAIN, and IN_DECL are the same parameters as for the
11671 tsubst_pack_expansion function.
11672
11673 The function returns the resulting Ei upon successful completion,
11674 or error_mark_node.
11675
11676 Note that this function possibly modifies the ARGS parameter, so
11677 it's the responsibility of the caller to restore it. */
11678
11679 static tree
11680 gen_elem_of_pack_expansion_instantiation (tree pattern,
11681 tree parm_packs,
11682 unsigned index,
11683 tree args /* This parm gets
11684 modified. */,
11685 tsubst_flags_t complain,
11686 tree in_decl)
11687 {
11688 tree t;
11689 bool ith_elem_is_expansion = false;
11690
11691 /* For each parameter pack, change the substitution of the parameter
11692 pack to the ith argument in its argument pack, then expand the
11693 pattern. */
11694 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
11695 {
11696 tree parm = TREE_PURPOSE (pack);
11697 tree arg_pack = TREE_VALUE (pack);
11698 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
11699
11700 ith_elem_is_expansion |=
11701 argument_pack_element_is_expansion_p (arg_pack, index);
11702
11703 /* Select the Ith argument from the pack. */
11704 if (TREE_CODE (parm) == PARM_DECL
11705 || VAR_P (parm)
11706 || TREE_CODE (parm) == FIELD_DECL)
11707 {
11708 if (index == 0)
11709 {
11710 aps = make_argument_pack_select (arg_pack, index);
11711 if (!mark_used (parm, complain) && !(complain & tf_error))
11712 return error_mark_node;
11713 register_local_specialization (aps, parm);
11714 }
11715 else
11716 aps = retrieve_local_specialization (parm);
11717 }
11718 else
11719 {
11720 int idx, level;
11721 template_parm_level_and_index (parm, &level, &idx);
11722
11723 if (index == 0)
11724 {
11725 aps = make_argument_pack_select (arg_pack, index);
11726 /* Update the corresponding argument. */
11727 TMPL_ARG (args, level, idx) = aps;
11728 }
11729 else
11730 /* Re-use the ARGUMENT_PACK_SELECT. */
11731 aps = TMPL_ARG (args, level, idx);
11732 }
11733 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11734 }
11735
11736 /* Substitute into the PATTERN with the (possibly altered)
11737 arguments. */
11738 if (pattern == in_decl)
11739 /* Expanding a fixed parameter pack from
11740 coerce_template_parameter_pack. */
11741 t = tsubst_decl (pattern, args, complain);
11742 else if (pattern == error_mark_node)
11743 t = error_mark_node;
11744 else if (constraint_p (pattern))
11745 {
11746 if (processing_template_decl)
11747 t = tsubst_constraint (pattern, args, complain, in_decl);
11748 else
11749 t = (constraints_satisfied_p (pattern, args)
11750 ? boolean_true_node : boolean_false_node);
11751 }
11752 else if (!TYPE_P (pattern))
11753 t = tsubst_expr (pattern, args, complain, in_decl,
11754 /*integral_constant_expression_p=*/false);
11755 else
11756 t = tsubst (pattern, args, complain, in_decl);
11757
11758 /* If the Ith argument pack element is a pack expansion, then
11759 the Ith element resulting from the substituting is going to
11760 be a pack expansion as well. */
11761 if (ith_elem_is_expansion)
11762 t = make_pack_expansion (t, complain);
11763
11764 return t;
11765 }
11766
11767 /* When the unexpanded parameter pack in a fold expression expands to an empty
11768 sequence, the value of the expression is as follows; the program is
11769 ill-formed if the operator is not listed in this table.
11770
11771 && true
11772 || false
11773 , void() */
11774
11775 tree
11776 expand_empty_fold (tree t, tsubst_flags_t complain)
11777 {
11778 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
11779 if (!FOLD_EXPR_MODIFY_P (t))
11780 switch (code)
11781 {
11782 case TRUTH_ANDIF_EXPR:
11783 return boolean_true_node;
11784 case TRUTH_ORIF_EXPR:
11785 return boolean_false_node;
11786 case COMPOUND_EXPR:
11787 return void_node;
11788 default:
11789 break;
11790 }
11791
11792 if (complain & tf_error)
11793 error_at (location_of (t),
11794 "fold of empty expansion over %O", code);
11795 return error_mark_node;
11796 }
11797
11798 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11799 form an expression that combines the two terms using the
11800 operator of T. */
11801
11802 static tree
11803 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
11804 {
11805 tree op = FOLD_EXPR_OP (t);
11806 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
11807
11808 // Handle compound assignment operators.
11809 if (FOLD_EXPR_MODIFY_P (t))
11810 return build_x_modify_expr (input_location, left, code, right, complain);
11811
11812 switch (code)
11813 {
11814 case COMPOUND_EXPR:
11815 return build_x_compound_expr (input_location, left, right, complain);
11816 case DOTSTAR_EXPR:
11817 return build_m_component_ref (left, right, complain);
11818 default:
11819 return build_x_binary_op (input_location, code,
11820 left, TREE_CODE (left),
11821 right, TREE_CODE (right),
11822 /*overload=*/NULL,
11823 complain);
11824 }
11825 }
11826
11827 /* Substitute ARGS into the pack of a fold expression T. */
11828
11829 static inline tree
11830 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11831 {
11832 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
11833 }
11834
11835 /* Substitute ARGS into the pack of a fold expression T. */
11836
11837 static inline tree
11838 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11839 {
11840 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
11841 }
11842
11843 /* Expand a PACK of arguments into a grouped as left fold.
11844 Given a pack containing elements A0, A1, ..., An and an
11845 operator @, this builds the expression:
11846
11847 ((A0 @ A1) @ A2) ... @ An
11848
11849 Note that PACK must not be empty.
11850
11851 The operator is defined by the original fold expression T. */
11852
11853 static tree
11854 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
11855 {
11856 tree left = TREE_VEC_ELT (pack, 0);
11857 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
11858 {
11859 tree right = TREE_VEC_ELT (pack, i);
11860 left = fold_expression (t, left, right, complain);
11861 }
11862 return left;
11863 }
11864
11865 /* Substitute into a unary left fold expression. */
11866
11867 static tree
11868 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
11869 tree in_decl)
11870 {
11871 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11872 if (pack == error_mark_node)
11873 return error_mark_node;
11874 if (PACK_EXPANSION_P (pack))
11875 {
11876 tree r = copy_node (t);
11877 FOLD_EXPR_PACK (r) = pack;
11878 return r;
11879 }
11880 if (TREE_VEC_LENGTH (pack) == 0)
11881 return expand_empty_fold (t, complain);
11882 else
11883 return expand_left_fold (t, pack, complain);
11884 }
11885
11886 /* Substitute into a binary left fold expression.
11887
11888 Do ths by building a single (non-empty) vector of argumnts and
11889 building the expression from those elements. */
11890
11891 static tree
11892 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
11893 tree in_decl)
11894 {
11895 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11896 if (pack == error_mark_node)
11897 return error_mark_node;
11898 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11899 if (init == error_mark_node)
11900 return error_mark_node;
11901
11902 if (PACK_EXPANSION_P (pack))
11903 {
11904 tree r = copy_node (t);
11905 FOLD_EXPR_PACK (r) = pack;
11906 FOLD_EXPR_INIT (r) = init;
11907 return r;
11908 }
11909
11910 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
11911 TREE_VEC_ELT (vec, 0) = init;
11912 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
11913 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
11914
11915 return expand_left_fold (t, vec, complain);
11916 }
11917
11918 /* Expand a PACK of arguments into a grouped as right fold.
11919 Given a pack containing elementns A0, A1, ..., and an
11920 operator @, this builds the expression:
11921
11922 A0@ ... (An-2 @ (An-1 @ An))
11923
11924 Note that PACK must not be empty.
11925
11926 The operator is defined by the original fold expression T. */
11927
11928 tree
11929 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
11930 {
11931 // Build the expression.
11932 int n = TREE_VEC_LENGTH (pack);
11933 tree right = TREE_VEC_ELT (pack, n - 1);
11934 for (--n; n != 0; --n)
11935 {
11936 tree left = TREE_VEC_ELT (pack, n - 1);
11937 right = fold_expression (t, left, right, complain);
11938 }
11939 return right;
11940 }
11941
11942 /* Substitute into a unary right fold expression. */
11943
11944 static tree
11945 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
11946 tree in_decl)
11947 {
11948 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11949 if (pack == error_mark_node)
11950 return error_mark_node;
11951 if (PACK_EXPANSION_P (pack))
11952 {
11953 tree r = copy_node (t);
11954 FOLD_EXPR_PACK (r) = pack;
11955 return r;
11956 }
11957 if (TREE_VEC_LENGTH (pack) == 0)
11958 return expand_empty_fold (t, complain);
11959 else
11960 return expand_right_fold (t, pack, complain);
11961 }
11962
11963 /* Substitute into a binary right fold expression.
11964
11965 Do ths by building a single (non-empty) vector of arguments and
11966 building the expression from those elements. */
11967
11968 static tree
11969 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
11970 tree in_decl)
11971 {
11972 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11973 if (pack == error_mark_node)
11974 return error_mark_node;
11975 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11976 if (init == error_mark_node)
11977 return error_mark_node;
11978
11979 if (PACK_EXPANSION_P (pack))
11980 {
11981 tree r = copy_node (t);
11982 FOLD_EXPR_PACK (r) = pack;
11983 FOLD_EXPR_INIT (r) = init;
11984 return r;
11985 }
11986
11987 int n = TREE_VEC_LENGTH (pack);
11988 tree vec = make_tree_vec (n + 1);
11989 for (int i = 0; i < n; ++i)
11990 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
11991 TREE_VEC_ELT (vec, n) = init;
11992
11993 return expand_right_fold (t, vec, complain);
11994 }
11995
11996 /* Walk through the pattern of a pack expansion, adding everything in
11997 local_specializations to a list. */
11998
11999 struct el_data
12000 {
12001 hash_set<tree> internal;
12002 tree extra;
12003 tsubst_flags_t complain;
12004
12005 el_data (tsubst_flags_t c)
12006 : extra (NULL_TREE), complain (c) {}
12007 };
12008 static tree
12009 extract_locals_r (tree *tp, int */*walk_subtrees*/, void *data_)
12010 {
12011 el_data &data = *reinterpret_cast<el_data*>(data_);
12012 tree *extra = &data.extra;
12013 tsubst_flags_t complain = data.complain;
12014
12015 if (TYPE_P (*tp) && typedef_variant_p (*tp))
12016 /* Remember local typedefs (85214). */
12017 tp = &TYPE_NAME (*tp);
12018
12019 if (TREE_CODE (*tp) == DECL_EXPR)
12020 data.internal.add (DECL_EXPR_DECL (*tp));
12021 else if (tree spec = retrieve_local_specialization (*tp))
12022 {
12023 if (data.internal.contains (*tp))
12024 /* Don't mess with variables declared within the pattern. */
12025 return NULL_TREE;
12026 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12027 {
12028 /* Maybe pull out the PARM_DECL for a partial instantiation. */
12029 tree args = ARGUMENT_PACK_ARGS (spec);
12030 if (TREE_VEC_LENGTH (args) == 1)
12031 {
12032 tree elt = TREE_VEC_ELT (args, 0);
12033 if (PACK_EXPANSION_P (elt))
12034 elt = PACK_EXPANSION_PATTERN (elt);
12035 if (DECL_PACK_P (elt))
12036 spec = elt;
12037 }
12038 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12039 {
12040 /* Handle lambda capture here, since we aren't doing any
12041 substitution now, and so tsubst_copy won't call
12042 process_outer_var_ref. */
12043 tree args = ARGUMENT_PACK_ARGS (spec);
12044 int len = TREE_VEC_LENGTH (args);
12045 for (int i = 0; i < len; ++i)
12046 {
12047 tree arg = TREE_VEC_ELT (args, i);
12048 tree carg = arg;
12049 if (outer_automatic_var_p (arg))
12050 carg = process_outer_var_ref (arg, complain);
12051 if (carg != arg)
12052 {
12053 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12054 proxies. */
12055 if (i == 0)
12056 {
12057 spec = copy_node (spec);
12058 args = copy_node (args);
12059 SET_ARGUMENT_PACK_ARGS (spec, args);
12060 register_local_specialization (spec, *tp);
12061 }
12062 TREE_VEC_ELT (args, i) = carg;
12063 }
12064 }
12065 }
12066 }
12067 if (outer_automatic_var_p (spec))
12068 spec = process_outer_var_ref (spec, complain);
12069 *extra = tree_cons (*tp, spec, *extra);
12070 }
12071 return NULL_TREE;
12072 }
12073 static tree
12074 extract_local_specs (tree pattern, tsubst_flags_t complain)
12075 {
12076 el_data data (complain);
12077 cp_walk_tree_without_duplicates (&pattern, extract_locals_r, &data);
12078 return data.extra;
12079 }
12080
12081 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12082 for use in PACK_EXPANSION_EXTRA_ARGS. */
12083
12084 tree
12085 build_extra_args (tree pattern, tree args, tsubst_flags_t complain)
12086 {
12087 tree extra = args;
12088 if (local_specializations)
12089 if (tree locals = extract_local_specs (pattern, complain))
12090 extra = tree_cons (NULL_TREE, extra, locals);
12091 return extra;
12092 }
12093
12094 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12095 normal template args to ARGS. */
12096
12097 tree
12098 add_extra_args (tree extra, tree args)
12099 {
12100 if (extra && TREE_CODE (extra) == TREE_LIST)
12101 {
12102 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
12103 {
12104 /* The partial instantiation involved local declarations collected in
12105 extract_local_specs; map from the general template to our local
12106 context. */
12107 tree gen = TREE_PURPOSE (elt);
12108 tree inst = TREE_VALUE (elt);
12109 if (DECL_P (inst))
12110 if (tree local = retrieve_local_specialization (inst))
12111 inst = local;
12112 /* else inst is already a full instantiation of the pack. */
12113 register_local_specialization (inst, gen);
12114 }
12115 gcc_assert (!TREE_PURPOSE (extra));
12116 extra = TREE_VALUE (extra);
12117 }
12118 return add_to_template_args (extra, args);
12119 }
12120
12121 /* Substitute ARGS into T, which is an pack expansion
12122 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12123 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12124 (if only a partial substitution could be performed) or
12125 ERROR_MARK_NODE if there was an error. */
12126 tree
12127 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
12128 tree in_decl)
12129 {
12130 tree pattern;
12131 tree pack, packs = NULL_TREE;
12132 bool unsubstituted_packs = false;
12133 bool unsubstituted_fn_pack = false;
12134 int i, len = -1;
12135 tree result;
12136 hash_map<tree, tree> *saved_local_specializations = NULL;
12137 bool need_local_specializations = false;
12138 int levels;
12139
12140 gcc_assert (PACK_EXPANSION_P (t));
12141 pattern = PACK_EXPANSION_PATTERN (t);
12142
12143 /* Add in any args remembered from an earlier partial instantiation. */
12144 args = add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
12145
12146 levels = TMPL_ARGS_DEPTH (args);
12147
12148 /* Determine the argument packs that will instantiate the parameter
12149 packs used in the expansion expression. While we're at it,
12150 compute the number of arguments to be expanded and make sure it
12151 is consistent. */
12152 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
12153 pack = TREE_CHAIN (pack))
12154 {
12155 tree parm_pack = TREE_VALUE (pack);
12156 tree arg_pack = NULL_TREE;
12157 tree orig_arg = NULL_TREE;
12158 int level = 0;
12159
12160 if (TREE_CODE (parm_pack) == BASES)
12161 {
12162 gcc_assert (parm_pack == pattern);
12163 if (BASES_DIRECT (parm_pack))
12164 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
12165 args, complain,
12166 in_decl, false),
12167 complain);
12168 else
12169 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
12170 args, complain, in_decl,
12171 false), complain);
12172 }
12173 else if (builtin_pack_call_p (parm_pack))
12174 {
12175 if (parm_pack != pattern)
12176 {
12177 if (complain & tf_error)
12178 sorry ("%qE is not the entire pattern of the pack expansion",
12179 parm_pack);
12180 return error_mark_node;
12181 }
12182 return expand_builtin_pack_call (parm_pack, args,
12183 complain, in_decl);
12184 }
12185 else if (TREE_CODE (parm_pack) == PARM_DECL)
12186 {
12187 /* We know we have correct local_specializations if this
12188 expansion is at function scope, or if we're dealing with a
12189 local parameter in a requires expression; for the latter,
12190 tsubst_requires_expr set it up appropriately. */
12191 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
12192 arg_pack = retrieve_local_specialization (parm_pack);
12193 else
12194 /* We can't rely on local_specializations for a parameter
12195 name used later in a function declaration (such as in a
12196 late-specified return type). Even if it exists, it might
12197 have the wrong value for a recursive call. */
12198 need_local_specializations = true;
12199
12200 if (!arg_pack)
12201 {
12202 /* This parameter pack was used in an unevaluated context. Just
12203 make a dummy decl, since it's only used for its type. */
12204 ++cp_unevaluated_operand;
12205 arg_pack = tsubst_decl (parm_pack, args, complain);
12206 --cp_unevaluated_operand;
12207 if (arg_pack && DECL_PACK_P (arg_pack))
12208 /* Partial instantiation of the parm_pack, we can't build
12209 up an argument pack yet. */
12210 arg_pack = NULL_TREE;
12211 else
12212 arg_pack = make_fnparm_pack (arg_pack);
12213 }
12214 else if (argument_pack_element_is_expansion_p (arg_pack, 0))
12215 /* This argument pack isn't fully instantiated yet. We set this
12216 flag rather than clear arg_pack because we do want to do the
12217 optimization below, and we don't want to substitute directly
12218 into the pattern (as that would expose a NONTYPE_ARGUMENT_PACK
12219 where it isn't expected). */
12220 unsubstituted_fn_pack = true;
12221 }
12222 else if (is_normal_capture_proxy (parm_pack))
12223 {
12224 arg_pack = retrieve_local_specialization (parm_pack);
12225 if (argument_pack_element_is_expansion_p (arg_pack, 0))
12226 unsubstituted_fn_pack = true;
12227 }
12228 else
12229 {
12230 int idx;
12231 template_parm_level_and_index (parm_pack, &level, &idx);
12232
12233 if (level <= levels)
12234 arg_pack = TMPL_ARG (args, level, idx);
12235 }
12236
12237 orig_arg = arg_pack;
12238 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12239 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12240
12241 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
12242 /* This can only happen if we forget to expand an argument
12243 pack somewhere else. Just return an error, silently. */
12244 {
12245 result = make_tree_vec (1);
12246 TREE_VEC_ELT (result, 0) = error_mark_node;
12247 return result;
12248 }
12249
12250 if (arg_pack)
12251 {
12252 int my_len =
12253 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
12254
12255 /* Don't bother trying to do a partial substitution with
12256 incomplete packs; we'll try again after deduction. */
12257 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
12258 return t;
12259
12260 if (len < 0)
12261 len = my_len;
12262 else if (len != my_len
12263 && !unsubstituted_fn_pack)
12264 {
12265 if (!(complain & tf_error))
12266 /* Fail quietly. */;
12267 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
12268 error ("mismatched argument pack lengths while expanding %qT",
12269 pattern);
12270 else
12271 error ("mismatched argument pack lengths while expanding %qE",
12272 pattern);
12273 return error_mark_node;
12274 }
12275
12276 /* Keep track of the parameter packs and their corresponding
12277 argument packs. */
12278 packs = tree_cons (parm_pack, arg_pack, packs);
12279 TREE_TYPE (packs) = orig_arg;
12280 }
12281 else
12282 {
12283 /* We can't substitute for this parameter pack. We use a flag as
12284 well as the missing_level counter because function parameter
12285 packs don't have a level. */
12286 gcc_assert (processing_template_decl || is_auto (parm_pack));
12287 unsubstituted_packs = true;
12288 }
12289 }
12290
12291 /* If the expansion is just T..., return the matching argument pack, unless
12292 we need to call convert_from_reference on all the elements. This is an
12293 important optimization; see c++/68422. */
12294 if (!unsubstituted_packs
12295 && TREE_PURPOSE (packs) == pattern)
12296 {
12297 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
12298
12299 /* If the argument pack is a single pack expansion, pull it out. */
12300 if (TREE_VEC_LENGTH (args) == 1
12301 && pack_expansion_args_count (args))
12302 return TREE_VEC_ELT (args, 0);
12303
12304 /* Types need no adjustment, nor does sizeof..., and if we still have
12305 some pack expansion args we won't do anything yet. */
12306 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
12307 || PACK_EXPANSION_SIZEOF_P (t)
12308 || pack_expansion_args_count (args))
12309 return args;
12310 /* Also optimize expression pack expansions if we can tell that the
12311 elements won't have reference type. */
12312 tree type = TREE_TYPE (pattern);
12313 if (type && !TYPE_REF_P (type)
12314 && !PACK_EXPANSION_P (type)
12315 && !WILDCARD_TYPE_P (type))
12316 return args;
12317 /* Otherwise use the normal path so we get convert_from_reference. */
12318 }
12319
12320 /* We cannot expand this expansion expression, because we don't have
12321 all of the argument packs we need. */
12322 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
12323 {
12324 /* We got some full packs, but we can't substitute them in until we
12325 have values for all the packs. So remember these until then. */
12326
12327 t = make_pack_expansion (pattern, complain);
12328 PACK_EXPANSION_EXTRA_ARGS (t)
12329 = build_extra_args (pattern, args, complain);
12330 return t;
12331 }
12332 else if (unsubstituted_packs)
12333 {
12334 /* There were no real arguments, we're just replacing a parameter
12335 pack with another version of itself. Substitute into the
12336 pattern and return a PACK_EXPANSION_*. The caller will need to
12337 deal with that. */
12338 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
12339 t = tsubst_expr (pattern, args, complain, in_decl,
12340 /*integral_constant_expression_p=*/false);
12341 else
12342 t = tsubst (pattern, args, complain, in_decl);
12343 t = make_pack_expansion (t, complain);
12344 return t;
12345 }
12346
12347 gcc_assert (len >= 0);
12348
12349 if (need_local_specializations)
12350 {
12351 /* We're in a late-specified return type, so create our own local
12352 specializations map; the current map is either NULL or (in the
12353 case of recursive unification) might have bindings that we don't
12354 want to use or alter. */
12355 saved_local_specializations = local_specializations;
12356 local_specializations = new hash_map<tree, tree>;
12357 }
12358
12359 /* For each argument in each argument pack, substitute into the
12360 pattern. */
12361 result = make_tree_vec (len);
12362 tree elem_args = copy_template_args (args);
12363 for (i = 0; i < len; ++i)
12364 {
12365 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
12366 i,
12367 elem_args, complain,
12368 in_decl);
12369 TREE_VEC_ELT (result, i) = t;
12370 if (t == error_mark_node)
12371 {
12372 result = error_mark_node;
12373 break;
12374 }
12375 }
12376
12377 /* Update ARGS to restore the substitution from parameter packs to
12378 their argument packs. */
12379 for (pack = packs; pack; pack = TREE_CHAIN (pack))
12380 {
12381 tree parm = TREE_PURPOSE (pack);
12382
12383 if (TREE_CODE (parm) == PARM_DECL
12384 || VAR_P (parm)
12385 || TREE_CODE (parm) == FIELD_DECL)
12386 register_local_specialization (TREE_TYPE (pack), parm);
12387 else
12388 {
12389 int idx, level;
12390
12391 if (TREE_VALUE (pack) == NULL_TREE)
12392 continue;
12393
12394 template_parm_level_and_index (parm, &level, &idx);
12395
12396 /* Update the corresponding argument. */
12397 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
12398 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
12399 TREE_TYPE (pack);
12400 else
12401 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
12402 }
12403 }
12404
12405 if (need_local_specializations)
12406 {
12407 delete local_specializations;
12408 local_specializations = saved_local_specializations;
12409 }
12410
12411 /* If the dependent pack arguments were such that we end up with only a
12412 single pack expansion again, there's no need to keep it in a TREE_VEC. */
12413 if (len == 1 && TREE_CODE (result) == TREE_VEC
12414 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
12415 return TREE_VEC_ELT (result, 0);
12416
12417 return result;
12418 }
12419
12420 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
12421 TMPL. We do this using DECL_PARM_INDEX, which should work even with
12422 parameter packs; all parms generated from a function parameter pack will
12423 have the same DECL_PARM_INDEX. */
12424
12425 tree
12426 get_pattern_parm (tree parm, tree tmpl)
12427 {
12428 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
12429 tree patparm;
12430
12431 if (DECL_ARTIFICIAL (parm))
12432 {
12433 for (patparm = DECL_ARGUMENTS (pattern);
12434 patparm; patparm = DECL_CHAIN (patparm))
12435 if (DECL_ARTIFICIAL (patparm)
12436 && DECL_NAME (parm) == DECL_NAME (patparm))
12437 break;
12438 }
12439 else
12440 {
12441 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
12442 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
12443 gcc_assert (DECL_PARM_INDEX (patparm)
12444 == DECL_PARM_INDEX (parm));
12445 }
12446
12447 return patparm;
12448 }
12449
12450 /* Make an argument pack out of the TREE_VEC VEC. */
12451
12452 static tree
12453 make_argument_pack (tree vec)
12454 {
12455 tree pack;
12456 tree elt = TREE_VEC_ELT (vec, 0);
12457 if (TYPE_P (elt))
12458 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
12459 else
12460 {
12461 pack = make_node (NONTYPE_ARGUMENT_PACK);
12462 TREE_CONSTANT (pack) = 1;
12463 }
12464 SET_ARGUMENT_PACK_ARGS (pack, vec);
12465 return pack;
12466 }
12467
12468 /* Return an exact copy of template args T that can be modified
12469 independently. */
12470
12471 static tree
12472 copy_template_args (tree t)
12473 {
12474 if (t == error_mark_node)
12475 return t;
12476
12477 int len = TREE_VEC_LENGTH (t);
12478 tree new_vec = make_tree_vec (len);
12479
12480 for (int i = 0; i < len; ++i)
12481 {
12482 tree elt = TREE_VEC_ELT (t, i);
12483 if (elt && TREE_CODE (elt) == TREE_VEC)
12484 elt = copy_template_args (elt);
12485 TREE_VEC_ELT (new_vec, i) = elt;
12486 }
12487
12488 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
12489 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
12490
12491 return new_vec;
12492 }
12493
12494 /* Substitute ARGS into the vector or list of template arguments T. */
12495
12496 static tree
12497 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12498 {
12499 tree orig_t = t;
12500 int len, need_new = 0, i, expanded_len_adjust = 0, out;
12501 tree *elts;
12502
12503 if (t == error_mark_node)
12504 return error_mark_node;
12505
12506 len = TREE_VEC_LENGTH (t);
12507 elts = XALLOCAVEC (tree, len);
12508
12509 for (i = 0; i < len; i++)
12510 {
12511 tree orig_arg = TREE_VEC_ELT (t, i);
12512 tree new_arg;
12513
12514 if (TREE_CODE (orig_arg) == TREE_VEC)
12515 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
12516 else if (PACK_EXPANSION_P (orig_arg))
12517 {
12518 /* Substitute into an expansion expression. */
12519 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
12520
12521 if (TREE_CODE (new_arg) == TREE_VEC)
12522 /* Add to the expanded length adjustment the number of
12523 expanded arguments. We subtract one from this
12524 measurement, because the argument pack expression
12525 itself is already counted as 1 in
12526 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
12527 the argument pack is empty. */
12528 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
12529 }
12530 else if (ARGUMENT_PACK_P (orig_arg))
12531 {
12532 /* Substitute into each of the arguments. */
12533 new_arg = TYPE_P (orig_arg)
12534 ? cxx_make_type (TREE_CODE (orig_arg))
12535 : make_node (TREE_CODE (orig_arg));
12536
12537 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
12538 args, complain, in_decl);
12539 if (pack_args == error_mark_node)
12540 new_arg = error_mark_node;
12541 else
12542 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
12543
12544 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
12545 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
12546 }
12547 else
12548 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
12549
12550 if (new_arg == error_mark_node)
12551 return error_mark_node;
12552
12553 elts[i] = new_arg;
12554 if (new_arg != orig_arg)
12555 need_new = 1;
12556 }
12557
12558 if (!need_new)
12559 return t;
12560
12561 /* Make space for the expanded arguments coming from template
12562 argument packs. */
12563 t = make_tree_vec (len + expanded_len_adjust);
12564 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
12565 arguments for a member template.
12566 In that case each TREE_VEC in ORIG_T represents a level of template
12567 arguments, and ORIG_T won't carry any non defaulted argument count.
12568 It will rather be the nested TREE_VECs that will carry one.
12569 In other words, ORIG_T carries a non defaulted argument count only
12570 if it doesn't contain any nested TREE_VEC. */
12571 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
12572 {
12573 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
12574 count += expanded_len_adjust;
12575 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
12576 }
12577 for (i = 0, out = 0; i < len; i++)
12578 {
12579 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
12580 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
12581 && TREE_CODE (elts[i]) == TREE_VEC)
12582 {
12583 int idx;
12584
12585 /* Now expand the template argument pack "in place". */
12586 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
12587 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
12588 }
12589 else
12590 {
12591 TREE_VEC_ELT (t, out) = elts[i];
12592 out++;
12593 }
12594 }
12595
12596 return t;
12597 }
12598
12599 /* Substitute ARGS into one level PARMS of template parameters. */
12600
12601 static tree
12602 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
12603 {
12604 if (parms == error_mark_node)
12605 return error_mark_node;
12606
12607 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
12608
12609 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
12610 {
12611 tree tuple = TREE_VEC_ELT (parms, i);
12612
12613 if (tuple == error_mark_node)
12614 continue;
12615
12616 TREE_VEC_ELT (new_vec, i) =
12617 tsubst_template_parm (tuple, args, complain);
12618 }
12619
12620 return new_vec;
12621 }
12622
12623 /* Return the result of substituting ARGS into the template parameters
12624 given by PARMS. If there are m levels of ARGS and m + n levels of
12625 PARMS, then the result will contain n levels of PARMS. For
12626 example, if PARMS is `template <class T> template <class U>
12627 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
12628 result will be `template <int*, double, class V>'. */
12629
12630 static tree
12631 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
12632 {
12633 tree r = NULL_TREE;
12634 tree* new_parms;
12635
12636 /* When substituting into a template, we must set
12637 PROCESSING_TEMPLATE_DECL as the template parameters may be
12638 dependent if they are based on one-another, and the dependency
12639 predicates are short-circuit outside of templates. */
12640 ++processing_template_decl;
12641
12642 for (new_parms = &r;
12643 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
12644 new_parms = &(TREE_CHAIN (*new_parms)),
12645 parms = TREE_CHAIN (parms))
12646 {
12647 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
12648 args, complain);
12649 *new_parms =
12650 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
12651 - TMPL_ARGS_DEPTH (args)),
12652 new_vec, NULL_TREE);
12653 }
12654
12655 --processing_template_decl;
12656
12657 return r;
12658 }
12659
12660 /* Return the result of substituting ARGS into one template parameter
12661 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
12662 parameter and which TREE_PURPOSE is the default argument of the
12663 template parameter. */
12664
12665 static tree
12666 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
12667 {
12668 tree default_value, parm_decl;
12669
12670 if (args == NULL_TREE
12671 || t == NULL_TREE
12672 || t == error_mark_node)
12673 return t;
12674
12675 gcc_assert (TREE_CODE (t) == TREE_LIST);
12676
12677 default_value = TREE_PURPOSE (t);
12678 parm_decl = TREE_VALUE (t);
12679
12680 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
12681 if (TREE_CODE (parm_decl) == PARM_DECL
12682 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
12683 parm_decl = error_mark_node;
12684 default_value = tsubst_template_arg (default_value, args,
12685 complain, NULL_TREE);
12686
12687 return build_tree_list (default_value, parm_decl);
12688 }
12689
12690 /* Substitute the ARGS into the indicated aggregate (or enumeration)
12691 type T. If T is not an aggregate or enumeration type, it is
12692 handled as if by tsubst. IN_DECL is as for tsubst. If
12693 ENTERING_SCOPE is nonzero, T is the context for a template which
12694 we are presently tsubst'ing. Return the substituted value. */
12695
12696 static tree
12697 tsubst_aggr_type (tree t,
12698 tree args,
12699 tsubst_flags_t complain,
12700 tree in_decl,
12701 int entering_scope)
12702 {
12703 if (t == NULL_TREE)
12704 return NULL_TREE;
12705
12706 switch (TREE_CODE (t))
12707 {
12708 case RECORD_TYPE:
12709 if (TYPE_PTRMEMFUNC_P (t))
12710 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
12711
12712 /* Fall through. */
12713 case ENUMERAL_TYPE:
12714 case UNION_TYPE:
12715 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
12716 {
12717 tree argvec;
12718 tree context;
12719 tree r;
12720 int saved_unevaluated_operand;
12721 int saved_inhibit_evaluation_warnings;
12722
12723 /* In "sizeof(X<I>)" we need to evaluate "I". */
12724 saved_unevaluated_operand = cp_unevaluated_operand;
12725 cp_unevaluated_operand = 0;
12726 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
12727 c_inhibit_evaluation_warnings = 0;
12728
12729 /* First, determine the context for the type we are looking
12730 up. */
12731 context = TYPE_CONTEXT (t);
12732 if (context && TYPE_P (context))
12733 {
12734 context = tsubst_aggr_type (context, args, complain,
12735 in_decl, /*entering_scope=*/1);
12736 /* If context is a nested class inside a class template,
12737 it may still need to be instantiated (c++/33959). */
12738 context = complete_type (context);
12739 }
12740
12741 /* Then, figure out what arguments are appropriate for the
12742 type we are trying to find. For example, given:
12743
12744 template <class T> struct S;
12745 template <class T, class U> void f(T, U) { S<U> su; }
12746
12747 and supposing that we are instantiating f<int, double>,
12748 then our ARGS will be {int, double}, but, when looking up
12749 S we only want {double}. */
12750 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
12751 complain, in_decl);
12752 if (argvec == error_mark_node)
12753 r = error_mark_node;
12754 else
12755 {
12756 r = lookup_template_class (t, argvec, in_decl, context,
12757 entering_scope, complain);
12758 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
12759 }
12760
12761 cp_unevaluated_operand = saved_unevaluated_operand;
12762 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
12763
12764 return r;
12765 }
12766 else
12767 /* This is not a template type, so there's nothing to do. */
12768 return t;
12769
12770 default:
12771 return tsubst (t, args, complain, in_decl);
12772 }
12773 }
12774
12775 static GTY((cache)) tree_cache_map *defarg_inst;
12776
12777 /* Substitute into the default argument ARG (a default argument for
12778 FN), which has the indicated TYPE. */
12779
12780 tree
12781 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
12782 tsubst_flags_t complain)
12783 {
12784 int errs = errorcount + sorrycount;
12785
12786 /* This can happen in invalid code. */
12787 if (TREE_CODE (arg) == DEFAULT_ARG)
12788 return arg;
12789
12790 tree parm = FUNCTION_FIRST_USER_PARM (fn);
12791 parm = chain_index (parmnum, parm);
12792 tree parmtype = TREE_TYPE (parm);
12793 if (DECL_BY_REFERENCE (parm))
12794 parmtype = TREE_TYPE (parmtype);
12795 if (parmtype == error_mark_node)
12796 return error_mark_node;
12797
12798 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
12799
12800 tree *slot;
12801 if (defarg_inst && (slot = defarg_inst->get (parm)))
12802 return *slot;
12803
12804 /* This default argument came from a template. Instantiate the
12805 default argument here, not in tsubst. In the case of
12806 something like:
12807
12808 template <class T>
12809 struct S {
12810 static T t();
12811 void f(T = t());
12812 };
12813
12814 we must be careful to do name lookup in the scope of S<T>,
12815 rather than in the current class. */
12816 push_to_top_level ();
12817 push_access_scope (fn);
12818 start_lambda_scope (parm);
12819
12820 /* The default argument expression may cause implicitly defined
12821 member functions to be synthesized, which will result in garbage
12822 collection. We must treat this situation as if we were within
12823 the body of function so as to avoid collecting live data on the
12824 stack. */
12825 ++function_depth;
12826 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
12827 complain, NULL_TREE,
12828 /*integral_constant_expression_p=*/false);
12829 --function_depth;
12830
12831 finish_lambda_scope ();
12832
12833 if (errorcount+sorrycount > errs
12834 && (complain & tf_warning_or_error))
12835 inform (input_location,
12836 " when instantiating default argument for call to %qD", fn);
12837
12838 /* Make sure the default argument is reasonable. */
12839 arg = check_default_argument (type, arg, complain);
12840
12841 pop_access_scope (fn);
12842 pop_from_top_level ();
12843
12844 if (arg != error_mark_node && !cp_unevaluated_operand)
12845 {
12846 if (!defarg_inst)
12847 defarg_inst = tree_cache_map::create_ggc (37);
12848 defarg_inst->put (parm, arg);
12849 }
12850
12851 return arg;
12852 }
12853
12854 /* Substitute into all the default arguments for FN. */
12855
12856 static void
12857 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
12858 {
12859 tree arg;
12860 tree tmpl_args;
12861
12862 tmpl_args = DECL_TI_ARGS (fn);
12863
12864 /* If this function is not yet instantiated, we certainly don't need
12865 its default arguments. */
12866 if (uses_template_parms (tmpl_args))
12867 return;
12868 /* Don't do this again for clones. */
12869 if (DECL_CLONED_FUNCTION_P (fn))
12870 return;
12871
12872 int i = 0;
12873 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
12874 arg;
12875 arg = TREE_CHAIN (arg), ++i)
12876 if (TREE_PURPOSE (arg))
12877 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
12878 TREE_VALUE (arg),
12879 TREE_PURPOSE (arg),
12880 complain);
12881 }
12882
12883 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
12884 static GTY((cache)) tree_cache_map *explicit_specifier_map;
12885
12886 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
12887
12888 void
12889 store_explicit_specifier (tree v, tree t)
12890 {
12891 if (!explicit_specifier_map)
12892 explicit_specifier_map = tree_cache_map::create_ggc (37);
12893 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v) = true;
12894 explicit_specifier_map->put (v, t);
12895 }
12896
12897 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
12898
12899 static tree
12900 lookup_explicit_specifier (tree v)
12901 {
12902 return *explicit_specifier_map->get (v);
12903 }
12904
12905 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12906
12907 static tree
12908 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
12909 tree lambda_fntype)
12910 {
12911 tree gen_tmpl, argvec;
12912 hashval_t hash = 0;
12913 tree in_decl = t;
12914
12915 /* Nobody should be tsubst'ing into non-template functions. */
12916 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
12917
12918 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
12919 {
12920 /* If T is not dependent, just return it. */
12921 if (!uses_template_parms (DECL_TI_ARGS (t)))
12922 return t;
12923
12924 /* Calculate the most general template of which R is a
12925 specialization. */
12926 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
12927
12928 /* We're substituting a lambda function under tsubst_lambda_expr but not
12929 directly from it; find the matching function we're already inside.
12930 But don't do this if T is a generic lambda with a single level of
12931 template parms, as in that case we're doing a normal instantiation. */
12932 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
12933 && (!generic_lambda_fn_p (t)
12934 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
12935 return enclosing_instantiation_of (t);
12936
12937 /* Calculate the complete set of arguments used to
12938 specialize R. */
12939 argvec = tsubst_template_args (DECL_TI_ARGS
12940 (DECL_TEMPLATE_RESULT
12941 (DECL_TI_TEMPLATE (t))),
12942 args, complain, in_decl);
12943 if (argvec == error_mark_node)
12944 return error_mark_node;
12945
12946 /* Check to see if we already have this specialization. */
12947 if (!lambda_fntype)
12948 {
12949 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12950 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
12951 return spec;
12952 }
12953
12954 /* We can see more levels of arguments than parameters if
12955 there was a specialization of a member template, like
12956 this:
12957
12958 template <class T> struct S { template <class U> void f(); }
12959 template <> template <class U> void S<int>::f(U);
12960
12961 Here, we'll be substituting into the specialization,
12962 because that's where we can find the code we actually
12963 want to generate, but we'll have enough arguments for
12964 the most general template.
12965
12966 We also deal with the peculiar case:
12967
12968 template <class T> struct S {
12969 template <class U> friend void f();
12970 };
12971 template <class U> void f() {}
12972 template S<int>;
12973 template void f<double>();
12974
12975 Here, the ARGS for the instantiation of will be {int,
12976 double}. But, we only need as many ARGS as there are
12977 levels of template parameters in CODE_PATTERN. We are
12978 careful not to get fooled into reducing the ARGS in
12979 situations like:
12980
12981 template <class T> struct S { template <class U> void f(U); }
12982 template <class T> template <> void S<T>::f(int) {}
12983
12984 which we can spot because the pattern will be a
12985 specialization in this case. */
12986 int args_depth = TMPL_ARGS_DEPTH (args);
12987 int parms_depth =
12988 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
12989
12990 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
12991 args = get_innermost_template_args (args, parms_depth);
12992 }
12993 else
12994 {
12995 /* This special case arises when we have something like this:
12996
12997 template <class T> struct S {
12998 friend void f<int>(int, double);
12999 };
13000
13001 Here, the DECL_TI_TEMPLATE for the friend declaration
13002 will be an IDENTIFIER_NODE. We are being called from
13003 tsubst_friend_function, and we want only to create a
13004 new decl (R) with appropriate types so that we can call
13005 determine_specialization. */
13006 gen_tmpl = NULL_TREE;
13007 argvec = NULL_TREE;
13008 }
13009
13010 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
13011 : NULL_TREE);
13012 tree ctx = closure ? closure : DECL_CONTEXT (t);
13013 bool member = ctx && TYPE_P (ctx);
13014
13015 if (member && !closure)
13016 ctx = tsubst_aggr_type (ctx, args,
13017 complain, t, /*entering_scope=*/1);
13018
13019 tree type = (lambda_fntype ? lambda_fntype
13020 : tsubst (TREE_TYPE (t), args,
13021 complain | tf_fndecl_type, in_decl));
13022 if (type == error_mark_node)
13023 return error_mark_node;
13024
13025 /* If we hit excessive deduction depth, the type is bogus even if
13026 it isn't error_mark_node, so don't build a decl. */
13027 if (excessive_deduction_depth)
13028 return error_mark_node;
13029
13030 /* We do NOT check for matching decls pushed separately at this
13031 point, as they may not represent instantiations of this
13032 template, and in any case are considered separate under the
13033 discrete model. */
13034 tree r = copy_decl (t);
13035 DECL_USE_TEMPLATE (r) = 0;
13036 TREE_TYPE (r) = type;
13037 /* Clear out the mangled name and RTL for the instantiation. */
13038 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13039 SET_DECL_RTL (r, NULL);
13040 /* Leave DECL_INITIAL set on deleted instantiations. */
13041 if (!DECL_DELETED_FN (r))
13042 DECL_INITIAL (r) = NULL_TREE;
13043 DECL_CONTEXT (r) = ctx;
13044
13045 /* Handle explicit(dependent-expr). */
13046 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
13047 {
13048 tree spec = lookup_explicit_specifier (t);
13049 spec = tsubst_copy_and_build (spec, args, complain, in_decl,
13050 /*function_p=*/false,
13051 /*i_c_e_p=*/true);
13052 spec = build_explicit_specifier (spec, complain);
13053 DECL_NONCONVERTING_P (r) = (spec == boolean_true_node);
13054 }
13055
13056 /* OpenMP UDRs have the only argument a reference to the declared
13057 type. We want to diagnose if the declared type is a reference,
13058 which is invalid, but as references to references are usually
13059 quietly merged, diagnose it here. */
13060 if (DECL_OMP_DECLARE_REDUCTION_P (t))
13061 {
13062 tree argtype
13063 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
13064 argtype = tsubst (argtype, args, complain, in_decl);
13065 if (TYPE_REF_P (argtype))
13066 error_at (DECL_SOURCE_LOCATION (t),
13067 "reference type %qT in "
13068 "%<#pragma omp declare reduction%>", argtype);
13069 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
13070 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
13071 argtype);
13072 }
13073
13074 if (member && DECL_CONV_FN_P (r))
13075 /* Type-conversion operator. Reconstruct the name, in
13076 case it's the name of one of the template's parameters. */
13077 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
13078
13079 tree parms = DECL_ARGUMENTS (t);
13080 if (closure)
13081 parms = DECL_CHAIN (parms);
13082 parms = tsubst (parms, args, complain, t);
13083 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
13084 DECL_CONTEXT (parm) = r;
13085 if (closure)
13086 {
13087 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
13088 DECL_CHAIN (tparm) = parms;
13089 parms = tparm;
13090 }
13091 DECL_ARGUMENTS (r) = parms;
13092 DECL_RESULT (r) = NULL_TREE;
13093
13094 TREE_STATIC (r) = 0;
13095 TREE_PUBLIC (r) = TREE_PUBLIC (t);
13096 DECL_EXTERNAL (r) = 1;
13097 /* If this is an instantiation of a function with internal
13098 linkage, we already know what object file linkage will be
13099 assigned to the instantiation. */
13100 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
13101 DECL_DEFER_OUTPUT (r) = 0;
13102 DECL_CHAIN (r) = NULL_TREE;
13103 DECL_PENDING_INLINE_INFO (r) = 0;
13104 DECL_PENDING_INLINE_P (r) = 0;
13105 DECL_SAVED_TREE (r) = NULL_TREE;
13106 DECL_STRUCT_FUNCTION (r) = NULL;
13107 TREE_USED (r) = 0;
13108 /* We'll re-clone as appropriate in instantiate_template. */
13109 DECL_CLONED_FUNCTION (r) = NULL_TREE;
13110
13111 /* If we aren't complaining now, return on error before we register
13112 the specialization so that we'll complain eventually. */
13113 if ((complain & tf_error) == 0
13114 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13115 && !grok_op_properties (r, /*complain=*/false))
13116 return error_mark_node;
13117
13118 /* When instantiating a constrained member, substitute
13119 into the constraints to create a new constraint. */
13120 if (tree ci = get_constraints (t))
13121 if (member)
13122 {
13123 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
13124 set_constraints (r, ci);
13125 }
13126
13127 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
13128 this in the special friend case mentioned above where
13129 GEN_TMPL is NULL. */
13130 if (gen_tmpl && !closure)
13131 {
13132 DECL_TEMPLATE_INFO (r)
13133 = build_template_info (gen_tmpl, argvec);
13134 SET_DECL_IMPLICIT_INSTANTIATION (r);
13135
13136 tree new_r
13137 = register_specialization (r, gen_tmpl, argvec, false, hash);
13138 if (new_r != r)
13139 /* We instantiated this while substituting into
13140 the type earlier (template/friend54.C). */
13141 return new_r;
13142
13143 /* We're not supposed to instantiate default arguments
13144 until they are called, for a template. But, for a
13145 declaration like:
13146
13147 template <class T> void f ()
13148 { extern void g(int i = T()); }
13149
13150 we should do the substitution when the template is
13151 instantiated. We handle the member function case in
13152 instantiate_class_template since the default arguments
13153 might refer to other members of the class. */
13154 if (!member
13155 && !PRIMARY_TEMPLATE_P (gen_tmpl)
13156 && !uses_template_parms (argvec))
13157 tsubst_default_arguments (r, complain);
13158 }
13159 else
13160 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13161
13162 /* Copy the list of befriending classes. */
13163 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
13164 *friends;
13165 friends = &TREE_CHAIN (*friends))
13166 {
13167 *friends = copy_node (*friends);
13168 TREE_VALUE (*friends)
13169 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
13170 }
13171
13172 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
13173 {
13174 maybe_retrofit_in_chrg (r);
13175 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
13176 return error_mark_node;
13177 /* If this is an instantiation of a member template, clone it.
13178 If it isn't, that'll be handled by
13179 clone_constructors_and_destructors. */
13180 if (PRIMARY_TEMPLATE_P (gen_tmpl))
13181 clone_function_decl (r, /*update_methods=*/false);
13182 }
13183 else if ((complain & tf_error) != 0
13184 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13185 && !grok_op_properties (r, /*complain=*/true))
13186 return error_mark_node;
13187
13188 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
13189 SET_DECL_FRIEND_CONTEXT (r,
13190 tsubst (DECL_FRIEND_CONTEXT (t),
13191 args, complain, in_decl));
13192
13193 /* Possibly limit visibility based on template args. */
13194 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13195 if (DECL_VISIBILITY_SPECIFIED (t))
13196 {
13197 DECL_VISIBILITY_SPECIFIED (r) = 0;
13198 DECL_ATTRIBUTES (r)
13199 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13200 }
13201 determine_visibility (r);
13202 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
13203 && !processing_template_decl)
13204 defaulted_late_check (r);
13205
13206 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13207 args, complain, in_decl);
13208 return r;
13209 }
13210
13211 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
13212
13213 static tree
13214 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
13215 tree lambda_fntype)
13216 {
13217 /* We can get here when processing a member function template,
13218 member class template, or template template parameter. */
13219 tree decl = DECL_TEMPLATE_RESULT (t);
13220 tree in_decl = t;
13221 tree spec;
13222 tree tmpl_args;
13223 tree full_args;
13224 tree r;
13225 hashval_t hash = 0;
13226
13227 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
13228 {
13229 /* Template template parameter is treated here. */
13230 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13231 if (new_type == error_mark_node)
13232 r = error_mark_node;
13233 /* If we get a real template back, return it. This can happen in
13234 the context of most_specialized_partial_spec. */
13235 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
13236 r = new_type;
13237 else
13238 /* The new TEMPLATE_DECL was built in
13239 reduce_template_parm_level. */
13240 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
13241 return r;
13242 }
13243
13244 if (!lambda_fntype)
13245 {
13246 /* We might already have an instance of this template.
13247 The ARGS are for the surrounding class type, so the
13248 full args contain the tsubst'd args for the context,
13249 plus the innermost args from the template decl. */
13250 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
13251 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
13252 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
13253 /* Because this is a template, the arguments will still be
13254 dependent, even after substitution. If
13255 PROCESSING_TEMPLATE_DECL is not set, the dependency
13256 predicates will short-circuit. */
13257 ++processing_template_decl;
13258 full_args = tsubst_template_args (tmpl_args, args,
13259 complain, in_decl);
13260 --processing_template_decl;
13261 if (full_args == error_mark_node)
13262 return error_mark_node;
13263
13264 /* If this is a default template template argument,
13265 tsubst might not have changed anything. */
13266 if (full_args == tmpl_args)
13267 return t;
13268
13269 hash = hash_tmpl_and_args (t, full_args);
13270 spec = retrieve_specialization (t, full_args, hash);
13271 if (spec != NULL_TREE)
13272 return spec;
13273 }
13274
13275 /* Make a new template decl. It will be similar to the
13276 original, but will record the current template arguments.
13277 We also create a new function declaration, which is just
13278 like the old one, but points to this new template, rather
13279 than the old one. */
13280 r = copy_decl (t);
13281 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
13282 DECL_CHAIN (r) = NULL_TREE;
13283
13284 // Build new template info linking to the original template decl.
13285 if (!lambda_fntype)
13286 {
13287 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
13288 SET_DECL_IMPLICIT_INSTANTIATION (r);
13289 }
13290 else
13291 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13292
13293 /* The template parameters for this new template are all the
13294 template parameters for the old template, except the
13295 outermost level of parameters. */
13296 DECL_TEMPLATE_PARMS (r)
13297 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
13298 complain);
13299
13300 if (TREE_CODE (decl) == TYPE_DECL
13301 && !TYPE_DECL_ALIAS_P (decl))
13302 {
13303 tree new_type;
13304 ++processing_template_decl;
13305 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13306 --processing_template_decl;
13307 if (new_type == error_mark_node)
13308 return error_mark_node;
13309
13310 TREE_TYPE (r) = new_type;
13311 /* For a partial specialization, we need to keep pointing to
13312 the primary template. */
13313 if (!DECL_TEMPLATE_SPECIALIZATION (t))
13314 CLASSTYPE_TI_TEMPLATE (new_type) = r;
13315 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
13316 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
13317 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
13318 }
13319 else
13320 {
13321 tree new_decl;
13322 ++processing_template_decl;
13323 if (TREE_CODE (decl) == FUNCTION_DECL)
13324 new_decl = tsubst_function_decl (decl, args, complain, lambda_fntype);
13325 else
13326 new_decl = tsubst (decl, args, complain, in_decl);
13327 --processing_template_decl;
13328 if (new_decl == error_mark_node)
13329 return error_mark_node;
13330
13331 DECL_TEMPLATE_RESULT (r) = new_decl;
13332 TREE_TYPE (r) = TREE_TYPE (new_decl);
13333 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
13334 if (lambda_fntype)
13335 {
13336 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
13337 DECL_TEMPLATE_INFO (new_decl) = build_template_info (r, args);
13338 }
13339 else
13340 {
13341 DECL_TI_TEMPLATE (new_decl) = r;
13342 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
13343 }
13344 }
13345
13346 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
13347 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
13348
13349 if (PRIMARY_TEMPLATE_P (t))
13350 DECL_PRIMARY_TEMPLATE (r) = r;
13351
13352 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
13353 && !lambda_fntype)
13354 /* Record this non-type partial instantiation. */
13355 register_specialization (r, t,
13356 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
13357 false, hash);
13358
13359 return r;
13360 }
13361
13362 /* True if FN is the op() for a lambda in an uninstantiated template. */
13363
13364 bool
13365 lambda_fn_in_template_p (tree fn)
13366 {
13367 if (!fn || !LAMBDA_FUNCTION_P (fn))
13368 return false;
13369 tree closure = DECL_CONTEXT (fn);
13370 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
13371 }
13372
13373 /* We're instantiating a variable from template function TCTX. Return the
13374 corresponding current enclosing scope. This gets complicated because lambda
13375 functions in templates are regenerated rather than instantiated, but generic
13376 lambda functions are subsequently instantiated. */
13377
13378 static tree
13379 enclosing_instantiation_of (tree otctx)
13380 {
13381 tree tctx = otctx;
13382 tree fn = current_function_decl;
13383 int lambda_count = 0;
13384
13385 for (; tctx && lambda_fn_in_template_p (tctx);
13386 tctx = decl_function_context (tctx))
13387 ++lambda_count;
13388 for (; fn; fn = decl_function_context (fn))
13389 {
13390 tree ofn = fn;
13391 int flambda_count = 0;
13392 for (; flambda_count < lambda_count && fn && LAMBDA_FUNCTION_P (fn);
13393 fn = decl_function_context (fn))
13394 ++flambda_count;
13395 if ((fn && DECL_TEMPLATE_INFO (fn))
13396 ? most_general_template (fn) != most_general_template (tctx)
13397 : fn != tctx)
13398 continue;
13399 gcc_assert (DECL_NAME (ofn) == DECL_NAME (otctx)
13400 || DECL_CONV_FN_P (ofn));
13401 return ofn;
13402 }
13403 gcc_unreachable ();
13404 }
13405
13406 /* Substitute the ARGS into the T, which is a _DECL. Return the
13407 result of the substitution. Issue error and warning messages under
13408 control of COMPLAIN. */
13409
13410 static tree
13411 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
13412 {
13413 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13414 location_t saved_loc;
13415 tree r = NULL_TREE;
13416 tree in_decl = t;
13417 hashval_t hash = 0;
13418
13419 /* Set the filename and linenumber to improve error-reporting. */
13420 saved_loc = input_location;
13421 input_location = DECL_SOURCE_LOCATION (t);
13422
13423 switch (TREE_CODE (t))
13424 {
13425 case TEMPLATE_DECL:
13426 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
13427 break;
13428
13429 case FUNCTION_DECL:
13430 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
13431 break;
13432
13433 case PARM_DECL:
13434 {
13435 tree type = NULL_TREE;
13436 int i, len = 1;
13437 tree expanded_types = NULL_TREE;
13438 tree prev_r = NULL_TREE;
13439 tree first_r = NULL_TREE;
13440
13441 if (DECL_PACK_P (t))
13442 {
13443 /* If there is a local specialization that isn't a
13444 parameter pack, it means that we're doing a "simple"
13445 substitution from inside tsubst_pack_expansion. Just
13446 return the local specialization (which will be a single
13447 parm). */
13448 tree spec = retrieve_local_specialization (t);
13449 if (spec
13450 && TREE_CODE (spec) == PARM_DECL
13451 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
13452 RETURN (spec);
13453
13454 /* Expand the TYPE_PACK_EXPANSION that provides the types for
13455 the parameters in this function parameter pack. */
13456 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
13457 complain, in_decl);
13458 if (TREE_CODE (expanded_types) == TREE_VEC)
13459 {
13460 len = TREE_VEC_LENGTH (expanded_types);
13461
13462 /* Zero-length parameter packs are boring. Just substitute
13463 into the chain. */
13464 if (len == 0)
13465 RETURN (tsubst (TREE_CHAIN (t), args, complain,
13466 TREE_CHAIN (t)));
13467 }
13468 else
13469 {
13470 /* All we did was update the type. Make a note of that. */
13471 type = expanded_types;
13472 expanded_types = NULL_TREE;
13473 }
13474 }
13475
13476 /* Loop through all of the parameters we'll build. When T is
13477 a function parameter pack, LEN is the number of expanded
13478 types in EXPANDED_TYPES; otherwise, LEN is 1. */
13479 r = NULL_TREE;
13480 for (i = 0; i < len; ++i)
13481 {
13482 prev_r = r;
13483 r = copy_node (t);
13484 if (DECL_TEMPLATE_PARM_P (t))
13485 SET_DECL_TEMPLATE_PARM_P (r);
13486
13487 if (expanded_types)
13488 /* We're on the Ith parameter of the function parameter
13489 pack. */
13490 {
13491 /* Get the Ith type. */
13492 type = TREE_VEC_ELT (expanded_types, i);
13493
13494 /* Rename the parameter to include the index. */
13495 DECL_NAME (r)
13496 = make_ith_pack_parameter_name (DECL_NAME (r), i);
13497 }
13498 else if (!type)
13499 /* We're dealing with a normal parameter. */
13500 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13501
13502 type = type_decays_to (type);
13503 TREE_TYPE (r) = type;
13504 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13505
13506 if (DECL_INITIAL (r))
13507 {
13508 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
13509 DECL_INITIAL (r) = TREE_TYPE (r);
13510 else
13511 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
13512 complain, in_decl);
13513 }
13514
13515 DECL_CONTEXT (r) = NULL_TREE;
13516
13517 if (!DECL_TEMPLATE_PARM_P (r))
13518 DECL_ARG_TYPE (r) = type_passed_as (type);
13519
13520 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13521 args, complain, in_decl);
13522
13523 /* Keep track of the first new parameter we
13524 generate. That's what will be returned to the
13525 caller. */
13526 if (!first_r)
13527 first_r = r;
13528
13529 /* Build a proper chain of parameters when substituting
13530 into a function parameter pack. */
13531 if (prev_r)
13532 DECL_CHAIN (prev_r) = r;
13533 }
13534
13535 /* If cp_unevaluated_operand is set, we're just looking for a
13536 single dummy parameter, so don't keep going. */
13537 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
13538 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
13539 complain, DECL_CHAIN (t));
13540
13541 /* FIRST_R contains the start of the chain we've built. */
13542 r = first_r;
13543 }
13544 break;
13545
13546 case FIELD_DECL:
13547 {
13548 tree type = NULL_TREE;
13549 tree vec = NULL_TREE;
13550 tree expanded_types = NULL_TREE;
13551 int len = 1;
13552
13553 if (PACK_EXPANSION_P (TREE_TYPE (t)))
13554 {
13555 /* This field is a lambda capture pack. Return a TREE_VEC of
13556 the expanded fields to instantiate_class_template_1. */
13557 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
13558 complain, in_decl);
13559 if (TREE_CODE (expanded_types) == TREE_VEC)
13560 {
13561 len = TREE_VEC_LENGTH (expanded_types);
13562 vec = make_tree_vec (len);
13563 }
13564 else
13565 {
13566 /* All we did was update the type. Make a note of that. */
13567 type = expanded_types;
13568 expanded_types = NULL_TREE;
13569 }
13570 }
13571
13572 for (int i = 0; i < len; ++i)
13573 {
13574 r = copy_decl (t);
13575 if (expanded_types)
13576 {
13577 type = TREE_VEC_ELT (expanded_types, i);
13578 DECL_NAME (r)
13579 = make_ith_pack_parameter_name (DECL_NAME (r), i);
13580 }
13581 else if (!type)
13582 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13583
13584 if (type == error_mark_node)
13585 RETURN (error_mark_node);
13586 TREE_TYPE (r) = type;
13587 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13588
13589 if (DECL_C_BIT_FIELD (r))
13590 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
13591 number of bits. */
13592 DECL_BIT_FIELD_REPRESENTATIVE (r)
13593 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
13594 complain, in_decl,
13595 /*integral_constant_expression_p=*/true);
13596 if (DECL_INITIAL (t))
13597 {
13598 /* Set up DECL_TEMPLATE_INFO so that we can get at the
13599 NSDMI in perform_member_init. Still set DECL_INITIAL
13600 so that we know there is one. */
13601 DECL_INITIAL (r) = void_node;
13602 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
13603 retrofit_lang_decl (r);
13604 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
13605 }
13606 /* We don't have to set DECL_CONTEXT here; it is set by
13607 finish_member_declaration. */
13608 DECL_CHAIN (r) = NULL_TREE;
13609
13610 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13611 args, complain, in_decl);
13612
13613 if (vec)
13614 TREE_VEC_ELT (vec, i) = r;
13615 }
13616
13617 if (vec)
13618 r = vec;
13619 }
13620 break;
13621
13622 case USING_DECL:
13623 /* We reach here only for member using decls. We also need to check
13624 uses_template_parms because DECL_DEPENDENT_P is not set for a
13625 using-declaration that designates a member of the current
13626 instantiation (c++/53549). */
13627 if (DECL_DEPENDENT_P (t)
13628 || uses_template_parms (USING_DECL_SCOPE (t)))
13629 {
13630 tree scope = USING_DECL_SCOPE (t);
13631 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
13632 if (PACK_EXPANSION_P (scope))
13633 {
13634 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
13635 int len = TREE_VEC_LENGTH (vec);
13636 r = make_tree_vec (len);
13637 for (int i = 0; i < len; ++i)
13638 {
13639 tree escope = TREE_VEC_ELT (vec, i);
13640 tree elt = do_class_using_decl (escope, name);
13641 if (!elt)
13642 {
13643 r = error_mark_node;
13644 break;
13645 }
13646 else
13647 {
13648 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
13649 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
13650 }
13651 TREE_VEC_ELT (r, i) = elt;
13652 }
13653 }
13654 else
13655 {
13656 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
13657 complain, in_decl);
13658 r = do_class_using_decl (inst_scope, name);
13659 if (!r)
13660 r = error_mark_node;
13661 else
13662 {
13663 TREE_PROTECTED (r) = TREE_PROTECTED (t);
13664 TREE_PRIVATE (r) = TREE_PRIVATE (t);
13665 }
13666 }
13667 }
13668 else
13669 {
13670 r = copy_node (t);
13671 DECL_CHAIN (r) = NULL_TREE;
13672 }
13673 break;
13674
13675 case TYPE_DECL:
13676 case VAR_DECL:
13677 {
13678 tree argvec = NULL_TREE;
13679 tree gen_tmpl = NULL_TREE;
13680 tree spec;
13681 tree tmpl = NULL_TREE;
13682 tree ctx;
13683 tree type = NULL_TREE;
13684 bool local_p;
13685
13686 if (TREE_TYPE (t) == error_mark_node)
13687 RETURN (error_mark_node);
13688
13689 if (TREE_CODE (t) == TYPE_DECL
13690 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
13691 {
13692 /* If this is the canonical decl, we don't have to
13693 mess with instantiations, and often we can't (for
13694 typename, template type parms and such). Note that
13695 TYPE_NAME is not correct for the above test if
13696 we've copied the type for a typedef. */
13697 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13698 if (type == error_mark_node)
13699 RETURN (error_mark_node);
13700 r = TYPE_NAME (type);
13701 break;
13702 }
13703
13704 /* Check to see if we already have the specialization we
13705 need. */
13706 spec = NULL_TREE;
13707 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
13708 {
13709 /* T is a static data member or namespace-scope entity.
13710 We have to substitute into namespace-scope variables
13711 (not just variable templates) because of cases like:
13712
13713 template <class T> void f() { extern T t; }
13714
13715 where the entity referenced is not known until
13716 instantiation time. */
13717 local_p = false;
13718 ctx = DECL_CONTEXT (t);
13719 if (DECL_CLASS_SCOPE_P (t))
13720 {
13721 ctx = tsubst_aggr_type (ctx, args,
13722 complain,
13723 in_decl, /*entering_scope=*/1);
13724 /* If CTX is unchanged, then T is in fact the
13725 specialization we want. That situation occurs when
13726 referencing a static data member within in its own
13727 class. We can use pointer equality, rather than
13728 same_type_p, because DECL_CONTEXT is always
13729 canonical... */
13730 if (ctx == DECL_CONTEXT (t)
13731 /* ... unless T is a member template; in which
13732 case our caller can be willing to create a
13733 specialization of that template represented
13734 by T. */
13735 && !(DECL_TI_TEMPLATE (t)
13736 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
13737 spec = t;
13738 }
13739
13740 if (!spec)
13741 {
13742 tmpl = DECL_TI_TEMPLATE (t);
13743 gen_tmpl = most_general_template (tmpl);
13744 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
13745 if (argvec != error_mark_node)
13746 argvec = (coerce_innermost_template_parms
13747 (DECL_TEMPLATE_PARMS (gen_tmpl),
13748 argvec, t, complain,
13749 /*all*/true, /*defarg*/true));
13750 if (argvec == error_mark_node)
13751 RETURN (error_mark_node);
13752 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13753 spec = retrieve_specialization (gen_tmpl, argvec, hash);
13754 }
13755 }
13756 else
13757 {
13758 /* A local variable. */
13759 local_p = true;
13760 /* Subsequent calls to pushdecl will fill this in. */
13761 ctx = NULL_TREE;
13762 /* Unless this is a reference to a static variable from an
13763 enclosing function, in which case we need to fill it in now. */
13764 if (TREE_STATIC (t))
13765 {
13766 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
13767 if (fn != current_function_decl)
13768 ctx = fn;
13769 }
13770 spec = retrieve_local_specialization (t);
13771 }
13772 /* If we already have the specialization we need, there is
13773 nothing more to do. */
13774 if (spec)
13775 {
13776 r = spec;
13777 break;
13778 }
13779
13780 /* Create a new node for the specialization we need. */
13781 if (type == NULL_TREE)
13782 {
13783 if (is_typedef_decl (t))
13784 type = DECL_ORIGINAL_TYPE (t);
13785 else
13786 type = TREE_TYPE (t);
13787 if (VAR_P (t)
13788 && VAR_HAD_UNKNOWN_BOUND (t)
13789 && type != error_mark_node)
13790 type = strip_array_domain (type);
13791 tree sub_args = args;
13792 if (tree auto_node = type_uses_auto (type))
13793 {
13794 /* Mask off any template args past the variable's context so we
13795 don't replace the auto with an unrelated argument. */
13796 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
13797 int extra = TMPL_ARGS_DEPTH (args) - nouter;
13798 if (extra > 0)
13799 /* This should never happen with the new lambda instantiation
13800 model, but keep the handling just in case. */
13801 gcc_assert (!CHECKING_P),
13802 sub_args = strip_innermost_template_args (args, extra);
13803 }
13804 type = tsubst (type, sub_args, complain, in_decl);
13805 /* Substituting the type might have recursively instantiated this
13806 same alias (c++/86171). */
13807 if (gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl)
13808 && (spec = retrieve_specialization (gen_tmpl, argvec, hash)))
13809 {
13810 r = spec;
13811 break;
13812 }
13813 }
13814 r = copy_decl (t);
13815 if (VAR_P (r))
13816 {
13817 DECL_INITIALIZED_P (r) = 0;
13818 DECL_TEMPLATE_INSTANTIATED (r) = 0;
13819 if (type == error_mark_node)
13820 RETURN (error_mark_node);
13821 if (TREE_CODE (type) == FUNCTION_TYPE)
13822 {
13823 /* It may seem that this case cannot occur, since:
13824
13825 typedef void f();
13826 void g() { f x; }
13827
13828 declares a function, not a variable. However:
13829
13830 typedef void f();
13831 template <typename T> void g() { T t; }
13832 template void g<f>();
13833
13834 is an attempt to declare a variable with function
13835 type. */
13836 error ("variable %qD has function type",
13837 /* R is not yet sufficiently initialized, so we
13838 just use its name. */
13839 DECL_NAME (r));
13840 RETURN (error_mark_node);
13841 }
13842 type = complete_type (type);
13843 /* Wait until cp_finish_decl to set this again, to handle
13844 circular dependency (template/instantiate6.C). */
13845 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
13846 type = check_var_type (DECL_NAME (r), type);
13847
13848 if (DECL_HAS_VALUE_EXPR_P (t))
13849 {
13850 tree ve = DECL_VALUE_EXPR (t);
13851 ve = tsubst_expr (ve, args, complain, in_decl,
13852 /*constant_expression_p=*/false);
13853 if (REFERENCE_REF_P (ve))
13854 {
13855 gcc_assert (TYPE_REF_P (type));
13856 ve = TREE_OPERAND (ve, 0);
13857 }
13858 SET_DECL_VALUE_EXPR (r, ve);
13859 }
13860 if (CP_DECL_THREAD_LOCAL_P (r)
13861 && !processing_template_decl)
13862 set_decl_tls_model (r, decl_default_tls_model (r));
13863 }
13864 else if (DECL_SELF_REFERENCE_P (t))
13865 SET_DECL_SELF_REFERENCE_P (r);
13866 TREE_TYPE (r) = type;
13867 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13868 DECL_CONTEXT (r) = ctx;
13869 /* Clear out the mangled name and RTL for the instantiation. */
13870 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13871 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
13872 SET_DECL_RTL (r, NULL);
13873 /* The initializer must not be expanded until it is required;
13874 see [temp.inst]. */
13875 DECL_INITIAL (r) = NULL_TREE;
13876 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
13877 if (VAR_P (r))
13878 {
13879 if (DECL_LANG_SPECIFIC (r))
13880 SET_DECL_DEPENDENT_INIT_P (r, false);
13881
13882 SET_DECL_MODE (r, VOIDmode);
13883
13884 /* Possibly limit visibility based on template args. */
13885 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13886 if (DECL_VISIBILITY_SPECIFIED (t))
13887 {
13888 DECL_VISIBILITY_SPECIFIED (r) = 0;
13889 DECL_ATTRIBUTES (r)
13890 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13891 }
13892 determine_visibility (r);
13893 }
13894
13895 if (!local_p)
13896 {
13897 /* A static data member declaration is always marked
13898 external when it is declared in-class, even if an
13899 initializer is present. We mimic the non-template
13900 processing here. */
13901 DECL_EXTERNAL (r) = 1;
13902 if (DECL_NAMESPACE_SCOPE_P (t))
13903 DECL_NOT_REALLY_EXTERN (r) = 1;
13904
13905 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
13906 SET_DECL_IMPLICIT_INSTANTIATION (r);
13907 register_specialization (r, gen_tmpl, argvec, false, hash);
13908 }
13909 else
13910 {
13911 if (DECL_LANG_SPECIFIC (r))
13912 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13913 if (!cp_unevaluated_operand)
13914 register_local_specialization (r, t);
13915 }
13916
13917 DECL_CHAIN (r) = NULL_TREE;
13918
13919 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
13920 /*flags=*/0,
13921 args, complain, in_decl);
13922
13923 /* Preserve a typedef that names a type. */
13924 if (is_typedef_decl (r) && type != error_mark_node)
13925 {
13926 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
13927 set_underlying_type (r);
13928 if (TYPE_DECL_ALIAS_P (r))
13929 /* An alias template specialization can be dependent
13930 even if its underlying type is not. */
13931 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
13932 }
13933
13934 layout_decl (r, 0);
13935 }
13936 break;
13937
13938 default:
13939 gcc_unreachable ();
13940 }
13941 #undef RETURN
13942
13943 out:
13944 /* Restore the file and line information. */
13945 input_location = saved_loc;
13946
13947 return r;
13948 }
13949
13950 /* Substitute into the ARG_TYPES of a function type.
13951 If END is a TREE_CHAIN, leave it and any following types
13952 un-substituted. */
13953
13954 static tree
13955 tsubst_arg_types (tree arg_types,
13956 tree args,
13957 tree end,
13958 tsubst_flags_t complain,
13959 tree in_decl)
13960 {
13961 tree remaining_arg_types;
13962 tree type = NULL_TREE;
13963 int i = 1;
13964 tree expanded_args = NULL_TREE;
13965 tree default_arg;
13966
13967 if (!arg_types || arg_types == void_list_node || arg_types == end)
13968 return arg_types;
13969
13970 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
13971 args, end, complain, in_decl);
13972 if (remaining_arg_types == error_mark_node)
13973 return error_mark_node;
13974
13975 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
13976 {
13977 /* For a pack expansion, perform substitution on the
13978 entire expression. Later on, we'll handle the arguments
13979 one-by-one. */
13980 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
13981 args, complain, in_decl);
13982
13983 if (TREE_CODE (expanded_args) == TREE_VEC)
13984 /* So that we'll spin through the parameters, one by one. */
13985 i = TREE_VEC_LENGTH (expanded_args);
13986 else
13987 {
13988 /* We only partially substituted into the parameter
13989 pack. Our type is TYPE_PACK_EXPANSION. */
13990 type = expanded_args;
13991 expanded_args = NULL_TREE;
13992 }
13993 }
13994
13995 while (i > 0) {
13996 --i;
13997
13998 if (expanded_args)
13999 type = TREE_VEC_ELT (expanded_args, i);
14000 else if (!type)
14001 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
14002
14003 if (type == error_mark_node)
14004 return error_mark_node;
14005 if (VOID_TYPE_P (type))
14006 {
14007 if (complain & tf_error)
14008 {
14009 error ("invalid parameter type %qT", type);
14010 if (in_decl)
14011 error ("in declaration %q+D", in_decl);
14012 }
14013 return error_mark_node;
14014 }
14015 /* DR 657. */
14016 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
14017 return error_mark_node;
14018
14019 /* Do array-to-pointer, function-to-pointer conversion, and ignore
14020 top-level qualifiers as required. */
14021 type = cv_unqualified (type_decays_to (type));
14022
14023 /* We do not substitute into default arguments here. The standard
14024 mandates that they be instantiated only when needed, which is
14025 done in build_over_call. */
14026 default_arg = TREE_PURPOSE (arg_types);
14027
14028 /* Except that we do substitute default arguments under tsubst_lambda_expr,
14029 since the new op() won't have any associated template arguments for us
14030 to refer to later. */
14031 if (lambda_fn_in_template_p (in_decl))
14032 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
14033 false/*fn*/, false/*constexpr*/);
14034
14035 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
14036 {
14037 /* We've instantiated a template before its default arguments
14038 have been parsed. This can happen for a nested template
14039 class, and is not an error unless we require the default
14040 argument in a call of this function. */
14041 remaining_arg_types =
14042 tree_cons (default_arg, type, remaining_arg_types);
14043 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
14044 }
14045 else
14046 remaining_arg_types =
14047 hash_tree_cons (default_arg, type, remaining_arg_types);
14048 }
14049
14050 return remaining_arg_types;
14051 }
14052
14053 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
14054 *not* handle the exception-specification for FNTYPE, because the
14055 initial substitution of explicitly provided template parameters
14056 during argument deduction forbids substitution into the
14057 exception-specification:
14058
14059 [temp.deduct]
14060
14061 All references in the function type of the function template to the
14062 corresponding template parameters are replaced by the specified tem-
14063 plate argument values. If a substitution in a template parameter or
14064 in the function type of the function template results in an invalid
14065 type, type deduction fails. [Note: The equivalent substitution in
14066 exception specifications is done only when the function is instanti-
14067 ated, at which point a program is ill-formed if the substitution
14068 results in an invalid type.] */
14069
14070 static tree
14071 tsubst_function_type (tree t,
14072 tree args,
14073 tsubst_flags_t complain,
14074 tree in_decl)
14075 {
14076 tree return_type;
14077 tree arg_types = NULL_TREE;
14078 tree fntype;
14079
14080 /* The TYPE_CONTEXT is not used for function/method types. */
14081 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
14082
14083 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
14084 failure. */
14085 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
14086
14087 if (late_return_type_p)
14088 {
14089 /* Substitute the argument types. */
14090 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
14091 complain, in_decl);
14092 if (arg_types == error_mark_node)
14093 return error_mark_node;
14094
14095 tree save_ccp = current_class_ptr;
14096 tree save_ccr = current_class_ref;
14097 tree this_type = (TREE_CODE (t) == METHOD_TYPE
14098 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
14099 bool do_inject = this_type && CLASS_TYPE_P (this_type);
14100 if (do_inject)
14101 {
14102 /* DR 1207: 'this' is in scope in the trailing return type. */
14103 inject_this_parameter (this_type, cp_type_quals (this_type));
14104 }
14105
14106 /* Substitute the return type. */
14107 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14108
14109 if (do_inject)
14110 {
14111 current_class_ptr = save_ccp;
14112 current_class_ref = save_ccr;
14113 }
14114 }
14115 else
14116 /* Substitute the return type. */
14117 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14118
14119 if (return_type == error_mark_node)
14120 return error_mark_node;
14121 /* DR 486 clarifies that creation of a function type with an
14122 invalid return type is a deduction failure. */
14123 if (TREE_CODE (return_type) == ARRAY_TYPE
14124 || TREE_CODE (return_type) == FUNCTION_TYPE)
14125 {
14126 if (complain & tf_error)
14127 {
14128 if (TREE_CODE (return_type) == ARRAY_TYPE)
14129 error ("function returning an array");
14130 else
14131 error ("function returning a function");
14132 }
14133 return error_mark_node;
14134 }
14135 /* And DR 657. */
14136 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
14137 return error_mark_node;
14138
14139 if (!late_return_type_p)
14140 {
14141 /* Substitute the argument types. */
14142 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
14143 complain, in_decl);
14144 if (arg_types == error_mark_node)
14145 return error_mark_node;
14146 }
14147
14148 /* Construct a new type node and return it. */
14149 if (TREE_CODE (t) == FUNCTION_TYPE)
14150 {
14151 fntype = build_function_type (return_type, arg_types);
14152 fntype = apply_memfn_quals (fntype, type_memfn_quals (t));
14153 }
14154 else
14155 {
14156 tree r = TREE_TYPE (TREE_VALUE (arg_types));
14157 /* Don't pick up extra function qualifiers from the basetype. */
14158 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
14159 if (! MAYBE_CLASS_TYPE_P (r))
14160 {
14161 /* [temp.deduct]
14162
14163 Type deduction may fail for any of the following
14164 reasons:
14165
14166 -- Attempting to create "pointer to member of T" when T
14167 is not a class type. */
14168 if (complain & tf_error)
14169 error ("creating pointer to member function of non-class type %qT",
14170 r);
14171 return error_mark_node;
14172 }
14173
14174 fntype = build_method_type_directly (r, return_type,
14175 TREE_CHAIN (arg_types));
14176 }
14177 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
14178
14179 /* See comment above. */
14180 tree raises = NULL_TREE;
14181 cp_ref_qualifier rqual = type_memfn_rqual (t);
14182 fntype = build_cp_fntype_variant (fntype, rqual, raises, late_return_type_p);
14183
14184 return fntype;
14185 }
14186
14187 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
14188 ARGS into that specification, and return the substituted
14189 specification. If there is no specification, return NULL_TREE. */
14190
14191 static tree
14192 tsubst_exception_specification (tree fntype,
14193 tree args,
14194 tsubst_flags_t complain,
14195 tree in_decl,
14196 bool defer_ok)
14197 {
14198 tree specs;
14199 tree new_specs;
14200
14201 specs = TYPE_RAISES_EXCEPTIONS (fntype);
14202 new_specs = NULL_TREE;
14203 if (specs && TREE_PURPOSE (specs))
14204 {
14205 /* A noexcept-specifier. */
14206 tree expr = TREE_PURPOSE (specs);
14207 if (TREE_CODE (expr) == INTEGER_CST)
14208 new_specs = expr;
14209 else if (defer_ok)
14210 {
14211 /* Defer instantiation of noexcept-specifiers to avoid
14212 excessive instantiations (c++/49107). */
14213 new_specs = make_node (DEFERRED_NOEXCEPT);
14214 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
14215 {
14216 /* We already partially instantiated this member template,
14217 so combine the new args with the old. */
14218 DEFERRED_NOEXCEPT_PATTERN (new_specs)
14219 = DEFERRED_NOEXCEPT_PATTERN (expr);
14220 DEFERRED_NOEXCEPT_ARGS (new_specs)
14221 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
14222 }
14223 else
14224 {
14225 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
14226 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
14227 }
14228 }
14229 else
14230 new_specs = tsubst_copy_and_build
14231 (expr, args, complain, in_decl, /*function_p=*/false,
14232 /*integral_constant_expression_p=*/true);
14233 new_specs = build_noexcept_spec (new_specs, complain);
14234 }
14235 else if (specs)
14236 {
14237 if (! TREE_VALUE (specs))
14238 new_specs = specs;
14239 else
14240 while (specs)
14241 {
14242 tree spec;
14243 int i, len = 1;
14244 tree expanded_specs = NULL_TREE;
14245
14246 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
14247 {
14248 /* Expand the pack expansion type. */
14249 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
14250 args, complain,
14251 in_decl);
14252
14253 if (expanded_specs == error_mark_node)
14254 return error_mark_node;
14255 else if (TREE_CODE (expanded_specs) == TREE_VEC)
14256 len = TREE_VEC_LENGTH (expanded_specs);
14257 else
14258 {
14259 /* We're substituting into a member template, so
14260 we got a TYPE_PACK_EXPANSION back. Add that
14261 expansion and move on. */
14262 gcc_assert (TREE_CODE (expanded_specs)
14263 == TYPE_PACK_EXPANSION);
14264 new_specs = add_exception_specifier (new_specs,
14265 expanded_specs,
14266 complain);
14267 specs = TREE_CHAIN (specs);
14268 continue;
14269 }
14270 }
14271
14272 for (i = 0; i < len; ++i)
14273 {
14274 if (expanded_specs)
14275 spec = TREE_VEC_ELT (expanded_specs, i);
14276 else
14277 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
14278 if (spec == error_mark_node)
14279 return spec;
14280 new_specs = add_exception_specifier (new_specs, spec,
14281 complain);
14282 }
14283
14284 specs = TREE_CHAIN (specs);
14285 }
14286 }
14287 return new_specs;
14288 }
14289
14290 /* Take the tree structure T and replace template parameters used
14291 therein with the argument vector ARGS. IN_DECL is an associated
14292 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
14293 Issue error and warning messages under control of COMPLAIN. Note
14294 that we must be relatively non-tolerant of extensions here, in
14295 order to preserve conformance; if we allow substitutions that
14296 should not be allowed, we may allow argument deductions that should
14297 not succeed, and therefore report ambiguous overload situations
14298 where there are none. In theory, we could allow the substitution,
14299 but indicate that it should have failed, and allow our caller to
14300 make sure that the right thing happens, but we don't try to do this
14301 yet.
14302
14303 This function is used for dealing with types, decls and the like;
14304 for expressions, use tsubst_expr or tsubst_copy. */
14305
14306 tree
14307 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14308 {
14309 enum tree_code code;
14310 tree type, r = NULL_TREE;
14311
14312 if (t == NULL_TREE || t == error_mark_node
14313 || t == integer_type_node
14314 || t == void_type_node
14315 || t == char_type_node
14316 || t == unknown_type_node
14317 || TREE_CODE (t) == NAMESPACE_DECL
14318 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
14319 return t;
14320
14321 if (DECL_P (t))
14322 return tsubst_decl (t, args, complain);
14323
14324 if (args == NULL_TREE)
14325 return t;
14326
14327 code = TREE_CODE (t);
14328
14329 if (code == IDENTIFIER_NODE)
14330 type = IDENTIFIER_TYPE_VALUE (t);
14331 else
14332 type = TREE_TYPE (t);
14333
14334 gcc_assert (type != unknown_type_node);
14335
14336 /* Reuse typedefs. We need to do this to handle dependent attributes,
14337 such as attribute aligned. */
14338 if (TYPE_P (t)
14339 && typedef_variant_p (t))
14340 {
14341 tree decl = TYPE_NAME (t);
14342
14343 if (alias_template_specialization_p (t))
14344 {
14345 /* DECL represents an alias template and we want to
14346 instantiate it. */
14347 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
14348 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
14349 r = instantiate_alias_template (tmpl, gen_args, complain);
14350 }
14351 else if (DECL_CLASS_SCOPE_P (decl)
14352 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
14353 && uses_template_parms (DECL_CONTEXT (decl)))
14354 {
14355 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
14356 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
14357 r = retrieve_specialization (tmpl, gen_args, 0);
14358 }
14359 else if (DECL_FUNCTION_SCOPE_P (decl)
14360 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
14361 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
14362 r = retrieve_local_specialization (decl);
14363 else
14364 /* The typedef is from a non-template context. */
14365 return t;
14366
14367 if (r)
14368 {
14369 r = TREE_TYPE (r);
14370 r = cp_build_qualified_type_real
14371 (r, cp_type_quals (t) | cp_type_quals (r),
14372 complain | tf_ignore_bad_quals);
14373 return r;
14374 }
14375 else
14376 {
14377 /* We don't have an instantiation yet, so drop the typedef. */
14378 int quals = cp_type_quals (t);
14379 t = DECL_ORIGINAL_TYPE (decl);
14380 t = cp_build_qualified_type_real (t, quals,
14381 complain | tf_ignore_bad_quals);
14382 }
14383 }
14384
14385 bool fndecl_type = (complain & tf_fndecl_type);
14386 complain &= ~tf_fndecl_type;
14387
14388 if (type
14389 && code != TYPENAME_TYPE
14390 && code != TEMPLATE_TYPE_PARM
14391 && code != TEMPLATE_PARM_INDEX
14392 && code != IDENTIFIER_NODE
14393 && code != FUNCTION_TYPE
14394 && code != METHOD_TYPE)
14395 type = tsubst (type, args, complain, in_decl);
14396 if (type == error_mark_node)
14397 return error_mark_node;
14398
14399 switch (code)
14400 {
14401 case RECORD_TYPE:
14402 case UNION_TYPE:
14403 case ENUMERAL_TYPE:
14404 return tsubst_aggr_type (t, args, complain, in_decl,
14405 /*entering_scope=*/0);
14406
14407 case ERROR_MARK:
14408 case IDENTIFIER_NODE:
14409 case VOID_TYPE:
14410 case REAL_TYPE:
14411 case COMPLEX_TYPE:
14412 case VECTOR_TYPE:
14413 case BOOLEAN_TYPE:
14414 case NULLPTR_TYPE:
14415 case LANG_TYPE:
14416 return t;
14417
14418 case INTEGER_TYPE:
14419 if (t == integer_type_node)
14420 return t;
14421
14422 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
14423 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
14424 return t;
14425
14426 {
14427 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
14428
14429 max = tsubst_expr (omax, args, complain, in_decl,
14430 /*integral_constant_expression_p=*/false);
14431
14432 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
14433 needed. */
14434 if (TREE_CODE (max) == NOP_EXPR
14435 && TREE_SIDE_EFFECTS (omax)
14436 && !TREE_TYPE (max))
14437 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
14438
14439 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
14440 with TREE_SIDE_EFFECTS that indicates this is not an integral
14441 constant expression. */
14442 if (processing_template_decl
14443 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
14444 {
14445 gcc_assert (TREE_CODE (max) == NOP_EXPR);
14446 TREE_SIDE_EFFECTS (max) = 1;
14447 }
14448
14449 return compute_array_index_type (NULL_TREE, max, complain);
14450 }
14451
14452 case TEMPLATE_TYPE_PARM:
14453 case TEMPLATE_TEMPLATE_PARM:
14454 case BOUND_TEMPLATE_TEMPLATE_PARM:
14455 case TEMPLATE_PARM_INDEX:
14456 {
14457 int idx;
14458 int level;
14459 int levels;
14460 tree arg = NULL_TREE;
14461
14462 /* Early in template argument deduction substitution, we don't
14463 want to reduce the level of 'auto', or it will be confused
14464 with a normal template parm in subsequent deduction. */
14465 if (is_auto (t) && (complain & tf_partial))
14466 return t;
14467
14468 r = NULL_TREE;
14469
14470 gcc_assert (TREE_VEC_LENGTH (args) > 0);
14471 template_parm_level_and_index (t, &level, &idx);
14472
14473 levels = TMPL_ARGS_DEPTH (args);
14474 if (level <= levels
14475 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
14476 {
14477 arg = TMPL_ARG (args, level, idx);
14478
14479 /* See through ARGUMENT_PACK_SELECT arguments. */
14480 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
14481 arg = argument_pack_select_arg (arg);
14482 }
14483
14484 if (arg == error_mark_node)
14485 return error_mark_node;
14486 else if (arg != NULL_TREE)
14487 {
14488 if (ARGUMENT_PACK_P (arg))
14489 /* If ARG is an argument pack, we don't actually want to
14490 perform a substitution here, because substitutions
14491 for argument packs are only done
14492 element-by-element. We can get to this point when
14493 substituting the type of a non-type template
14494 parameter pack, when that type actually contains
14495 template parameter packs from an outer template, e.g.,
14496
14497 template<typename... Types> struct A {
14498 template<Types... Values> struct B { };
14499 }; */
14500 return t;
14501
14502 if (code == TEMPLATE_TYPE_PARM)
14503 {
14504 int quals;
14505 gcc_assert (TYPE_P (arg));
14506
14507 quals = cp_type_quals (arg) | cp_type_quals (t);
14508
14509 return cp_build_qualified_type_real
14510 (arg, quals, complain | tf_ignore_bad_quals);
14511 }
14512 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
14513 {
14514 /* We are processing a type constructed from a
14515 template template parameter. */
14516 tree argvec = tsubst (TYPE_TI_ARGS (t),
14517 args, complain, in_decl);
14518 if (argvec == error_mark_node)
14519 return error_mark_node;
14520
14521 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
14522 || TREE_CODE (arg) == TEMPLATE_DECL
14523 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
14524
14525 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
14526 /* Consider this code:
14527
14528 template <template <class> class Template>
14529 struct Internal {
14530 template <class Arg> using Bind = Template<Arg>;
14531 };
14532
14533 template <template <class> class Template, class Arg>
14534 using Instantiate = Template<Arg>; //#0
14535
14536 template <template <class> class Template,
14537 class Argument>
14538 using Bind =
14539 Instantiate<Internal<Template>::template Bind,
14540 Argument>; //#1
14541
14542 When #1 is parsed, the
14543 BOUND_TEMPLATE_TEMPLATE_PARM representing the
14544 parameter `Template' in #0 matches the
14545 UNBOUND_CLASS_TEMPLATE representing the argument
14546 `Internal<Template>::template Bind'; We then want
14547 to assemble the type `Bind<Argument>' that can't
14548 be fully created right now, because
14549 `Internal<Template>' not being complete, the Bind
14550 template cannot be looked up in that context. So
14551 we need to "store" `Bind<Argument>' for later
14552 when the context of Bind becomes complete. Let's
14553 store that in a TYPENAME_TYPE. */
14554 return make_typename_type (TYPE_CONTEXT (arg),
14555 build_nt (TEMPLATE_ID_EXPR,
14556 TYPE_IDENTIFIER (arg),
14557 argvec),
14558 typename_type,
14559 complain);
14560
14561 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
14562 are resolving nested-types in the signature of a
14563 member function templates. Otherwise ARG is a
14564 TEMPLATE_DECL and is the real template to be
14565 instantiated. */
14566 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
14567 arg = TYPE_NAME (arg);
14568
14569 r = lookup_template_class (arg,
14570 argvec, in_decl,
14571 DECL_CONTEXT (arg),
14572 /*entering_scope=*/0,
14573 complain);
14574 return cp_build_qualified_type_real
14575 (r, cp_type_quals (t) | cp_type_quals (r), complain);
14576 }
14577 else if (code == TEMPLATE_TEMPLATE_PARM)
14578 return arg;
14579 else
14580 /* TEMPLATE_PARM_INDEX. */
14581 return convert_from_reference (unshare_expr (arg));
14582 }
14583
14584 if (level == 1)
14585 /* This can happen during the attempted tsubst'ing in
14586 unify. This means that we don't yet have any information
14587 about the template parameter in question. */
14588 return t;
14589
14590 /* If we get here, we must have been looking at a parm for a
14591 more deeply nested template. Make a new version of this
14592 template parameter, but with a lower level. */
14593 switch (code)
14594 {
14595 case TEMPLATE_TYPE_PARM:
14596 case TEMPLATE_TEMPLATE_PARM:
14597 case BOUND_TEMPLATE_TEMPLATE_PARM:
14598 if (cp_type_quals (t))
14599 {
14600 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
14601 r = cp_build_qualified_type_real
14602 (r, cp_type_quals (t),
14603 complain | (code == TEMPLATE_TYPE_PARM
14604 ? tf_ignore_bad_quals : 0));
14605 }
14606 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
14607 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
14608 && (r = (TEMPLATE_PARM_DESCENDANTS
14609 (TEMPLATE_TYPE_PARM_INDEX (t))))
14610 && (r = TREE_TYPE (r))
14611 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
14612 /* Break infinite recursion when substituting the constraints
14613 of a constrained placeholder. */;
14614 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
14615 && !PLACEHOLDER_TYPE_CONSTRAINTS (t)
14616 && !CLASS_PLACEHOLDER_TEMPLATE (t)
14617 && (arg = TEMPLATE_TYPE_PARM_INDEX (t),
14618 r = TEMPLATE_PARM_DESCENDANTS (arg))
14619 && (TEMPLATE_PARM_LEVEL (r)
14620 == TEMPLATE_PARM_LEVEL (arg) - levels))
14621 /* Cache the simple case of lowering a type parameter. */
14622 r = TREE_TYPE (r);
14623 else
14624 {
14625 r = copy_type (t);
14626 TEMPLATE_TYPE_PARM_INDEX (r)
14627 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
14628 r, levels, args, complain);
14629 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
14630 TYPE_MAIN_VARIANT (r) = r;
14631 TYPE_POINTER_TO (r) = NULL_TREE;
14632 TYPE_REFERENCE_TO (r) = NULL_TREE;
14633
14634 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
14635 {
14636 /* Propagate constraints on placeholders. */
14637 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
14638 PLACEHOLDER_TYPE_CONSTRAINTS (r)
14639 = tsubst_constraint (constr, args, complain, in_decl);
14640 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
14641 {
14642 pl = tsubst_copy (pl, args, complain, in_decl);
14643 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
14644 }
14645 }
14646
14647 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
14648 /* We have reduced the level of the template
14649 template parameter, but not the levels of its
14650 template parameters, so canonical_type_parameter
14651 will not be able to find the canonical template
14652 template parameter for this level. Thus, we
14653 require structural equality checking to compare
14654 TEMPLATE_TEMPLATE_PARMs. */
14655 SET_TYPE_STRUCTURAL_EQUALITY (r);
14656 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
14657 SET_TYPE_STRUCTURAL_EQUALITY (r);
14658 else
14659 TYPE_CANONICAL (r) = canonical_type_parameter (r);
14660
14661 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
14662 {
14663 tree tinfo = TYPE_TEMPLATE_INFO (t);
14664 /* We might need to substitute into the types of non-type
14665 template parameters. */
14666 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
14667 complain, in_decl);
14668 if (tmpl == error_mark_node)
14669 return error_mark_node;
14670 tree argvec = tsubst (TI_ARGS (tinfo), args,
14671 complain, in_decl);
14672 if (argvec == error_mark_node)
14673 return error_mark_node;
14674
14675 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
14676 = build_template_info (tmpl, argvec);
14677 }
14678 }
14679 break;
14680
14681 case TEMPLATE_PARM_INDEX:
14682 /* OK, now substitute the type of the non-type parameter. We
14683 couldn't do it earlier because it might be an auto parameter,
14684 and we wouldn't need to if we had an argument. */
14685 type = tsubst (type, args, complain, in_decl);
14686 if (type == error_mark_node)
14687 return error_mark_node;
14688 r = reduce_template_parm_level (t, type, levels, args, complain);
14689 break;
14690
14691 default:
14692 gcc_unreachable ();
14693 }
14694
14695 return r;
14696 }
14697
14698 case TREE_LIST:
14699 {
14700 tree purpose, value, chain;
14701
14702 if (t == void_list_node)
14703 return t;
14704
14705 purpose = TREE_PURPOSE (t);
14706 if (purpose)
14707 {
14708 purpose = tsubst (purpose, args, complain, in_decl);
14709 if (purpose == error_mark_node)
14710 return error_mark_node;
14711 }
14712 value = TREE_VALUE (t);
14713 if (value)
14714 {
14715 value = tsubst (value, args, complain, in_decl);
14716 if (value == error_mark_node)
14717 return error_mark_node;
14718 }
14719 chain = TREE_CHAIN (t);
14720 if (chain && chain != void_type_node)
14721 {
14722 chain = tsubst (chain, args, complain, in_decl);
14723 if (chain == error_mark_node)
14724 return error_mark_node;
14725 }
14726 if (purpose == TREE_PURPOSE (t)
14727 && value == TREE_VALUE (t)
14728 && chain == TREE_CHAIN (t))
14729 return t;
14730 return hash_tree_cons (purpose, value, chain);
14731 }
14732
14733 case TREE_BINFO:
14734 /* We should never be tsubsting a binfo. */
14735 gcc_unreachable ();
14736
14737 case TREE_VEC:
14738 /* A vector of template arguments. */
14739 gcc_assert (!type);
14740 return tsubst_template_args (t, args, complain, in_decl);
14741
14742 case POINTER_TYPE:
14743 case REFERENCE_TYPE:
14744 {
14745 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
14746 return t;
14747
14748 /* [temp.deduct]
14749
14750 Type deduction may fail for any of the following
14751 reasons:
14752
14753 -- Attempting to create a pointer to reference type.
14754 -- Attempting to create a reference to a reference type or
14755 a reference to void.
14756
14757 Core issue 106 says that creating a reference to a reference
14758 during instantiation is no longer a cause for failure. We
14759 only enforce this check in strict C++98 mode. */
14760 if ((TYPE_REF_P (type)
14761 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
14762 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
14763 {
14764 static location_t last_loc;
14765
14766 /* We keep track of the last time we issued this error
14767 message to avoid spewing a ton of messages during a
14768 single bad template instantiation. */
14769 if (complain & tf_error
14770 && last_loc != input_location)
14771 {
14772 if (VOID_TYPE_P (type))
14773 error ("forming reference to void");
14774 else if (code == POINTER_TYPE)
14775 error ("forming pointer to reference type %qT", type);
14776 else
14777 error ("forming reference to reference type %qT", type);
14778 last_loc = input_location;
14779 }
14780
14781 return error_mark_node;
14782 }
14783 else if (TREE_CODE (type) == FUNCTION_TYPE
14784 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
14785 || type_memfn_rqual (type) != REF_QUAL_NONE))
14786 {
14787 if (complain & tf_error)
14788 {
14789 if (code == POINTER_TYPE)
14790 error ("forming pointer to qualified function type %qT",
14791 type);
14792 else
14793 error ("forming reference to qualified function type %qT",
14794 type);
14795 }
14796 return error_mark_node;
14797 }
14798 else if (code == POINTER_TYPE)
14799 {
14800 r = build_pointer_type (type);
14801 if (TREE_CODE (type) == METHOD_TYPE)
14802 r = build_ptrmemfunc_type (r);
14803 }
14804 else if (TYPE_REF_P (type))
14805 /* In C++0x, during template argument substitution, when there is an
14806 attempt to create a reference to a reference type, reference
14807 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14808
14809 "If a template-argument for a template-parameter T names a type
14810 that is a reference to a type A, an attempt to create the type
14811 'lvalue reference to cv T' creates the type 'lvalue reference to
14812 A,' while an attempt to create the type type rvalue reference to
14813 cv T' creates the type T"
14814 */
14815 r = cp_build_reference_type
14816 (TREE_TYPE (type),
14817 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
14818 else
14819 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
14820 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
14821
14822 if (r != error_mark_node)
14823 /* Will this ever be needed for TYPE_..._TO values? */
14824 layout_type (r);
14825
14826 return r;
14827 }
14828 case OFFSET_TYPE:
14829 {
14830 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
14831 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
14832 {
14833 /* [temp.deduct]
14834
14835 Type deduction may fail for any of the following
14836 reasons:
14837
14838 -- Attempting to create "pointer to member of T" when T
14839 is not a class type. */
14840 if (complain & tf_error)
14841 error ("creating pointer to member of non-class type %qT", r);
14842 return error_mark_node;
14843 }
14844 if (TYPE_REF_P (type))
14845 {
14846 if (complain & tf_error)
14847 error ("creating pointer to member reference type %qT", type);
14848 return error_mark_node;
14849 }
14850 if (VOID_TYPE_P (type))
14851 {
14852 if (complain & tf_error)
14853 error ("creating pointer to member of type void");
14854 return error_mark_node;
14855 }
14856 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
14857 if (TREE_CODE (type) == FUNCTION_TYPE)
14858 {
14859 /* The type of the implicit object parameter gets its
14860 cv-qualifiers from the FUNCTION_TYPE. */
14861 tree memptr;
14862 tree method_type
14863 = build_memfn_type (type, r, type_memfn_quals (type),
14864 type_memfn_rqual (type));
14865 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
14866 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
14867 complain);
14868 }
14869 else
14870 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
14871 cp_type_quals (t),
14872 complain);
14873 }
14874 case FUNCTION_TYPE:
14875 case METHOD_TYPE:
14876 {
14877 tree fntype;
14878 tree specs;
14879 fntype = tsubst_function_type (t, args, complain, in_decl);
14880 if (fntype == error_mark_node)
14881 return error_mark_node;
14882
14883 /* Substitute the exception specification. */
14884 specs = tsubst_exception_specification (t, args, complain, in_decl,
14885 /*defer_ok*/fndecl_type);
14886 if (specs == error_mark_node)
14887 return error_mark_node;
14888 if (specs)
14889 fntype = build_exception_variant (fntype, specs);
14890 return fntype;
14891 }
14892 case ARRAY_TYPE:
14893 {
14894 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
14895 if (domain == error_mark_node)
14896 return error_mark_node;
14897
14898 /* As an optimization, we avoid regenerating the array type if
14899 it will obviously be the same as T. */
14900 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
14901 return t;
14902
14903 /* These checks should match the ones in create_array_type_for_decl.
14904
14905 [temp.deduct]
14906
14907 The deduction may fail for any of the following reasons:
14908
14909 -- Attempting to create an array with an element type that
14910 is void, a function type, or a reference type, or [DR337]
14911 an abstract class type. */
14912 if (VOID_TYPE_P (type)
14913 || TREE_CODE (type) == FUNCTION_TYPE
14914 || (TREE_CODE (type) == ARRAY_TYPE
14915 && TYPE_DOMAIN (type) == NULL_TREE)
14916 || TYPE_REF_P (type))
14917 {
14918 if (complain & tf_error)
14919 error ("creating array of %qT", type);
14920 return error_mark_node;
14921 }
14922
14923 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
14924 return error_mark_node;
14925
14926 r = build_cplus_array_type (type, domain);
14927
14928 if (!valid_array_size_p (input_location, r, in_decl,
14929 (complain & tf_error)))
14930 return error_mark_node;
14931
14932 if (TYPE_USER_ALIGN (t))
14933 {
14934 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
14935 TYPE_USER_ALIGN (r) = 1;
14936 }
14937
14938 return r;
14939 }
14940
14941 case TYPENAME_TYPE:
14942 {
14943 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14944 in_decl, /*entering_scope=*/1);
14945 if (ctx == error_mark_node)
14946 return error_mark_node;
14947
14948 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
14949 complain, in_decl);
14950 if (f == error_mark_node)
14951 return error_mark_node;
14952
14953 if (!MAYBE_CLASS_TYPE_P (ctx))
14954 {
14955 if (complain & tf_error)
14956 error ("%qT is not a class, struct, or union type", ctx);
14957 return error_mark_node;
14958 }
14959 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
14960 {
14961 /* Normally, make_typename_type does not require that the CTX
14962 have complete type in order to allow things like:
14963
14964 template <class T> struct S { typename S<T>::X Y; };
14965
14966 But, such constructs have already been resolved by this
14967 point, so here CTX really should have complete type, unless
14968 it's a partial instantiation. */
14969 ctx = complete_type (ctx);
14970 if (!COMPLETE_TYPE_P (ctx))
14971 {
14972 if (complain & tf_error)
14973 cxx_incomplete_type_error (NULL_TREE, ctx);
14974 return error_mark_node;
14975 }
14976 }
14977
14978 f = make_typename_type (ctx, f, typename_type,
14979 complain | tf_keep_type_decl);
14980 if (f == error_mark_node)
14981 return f;
14982 if (TREE_CODE (f) == TYPE_DECL)
14983 {
14984 complain |= tf_ignore_bad_quals;
14985 f = TREE_TYPE (f);
14986 }
14987
14988 if (TREE_CODE (f) != TYPENAME_TYPE)
14989 {
14990 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
14991 {
14992 if (complain & tf_error)
14993 error ("%qT resolves to %qT, which is not an enumeration type",
14994 t, f);
14995 else
14996 return error_mark_node;
14997 }
14998 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
14999 {
15000 if (complain & tf_error)
15001 error ("%qT resolves to %qT, which is is not a class type",
15002 t, f);
15003 else
15004 return error_mark_node;
15005 }
15006 }
15007
15008 return cp_build_qualified_type_real
15009 (f, cp_type_quals (f) | cp_type_quals (t), complain);
15010 }
15011
15012 case UNBOUND_CLASS_TEMPLATE:
15013 {
15014 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
15015 in_decl, /*entering_scope=*/1);
15016 tree name = TYPE_IDENTIFIER (t);
15017 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
15018
15019 if (ctx == error_mark_node || name == error_mark_node)
15020 return error_mark_node;
15021
15022 if (parm_list)
15023 parm_list = tsubst_template_parms (parm_list, args, complain);
15024 return make_unbound_class_template (ctx, name, parm_list, complain);
15025 }
15026
15027 case TYPEOF_TYPE:
15028 {
15029 tree type;
15030
15031 ++cp_unevaluated_operand;
15032 ++c_inhibit_evaluation_warnings;
15033
15034 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
15035 complain, in_decl,
15036 /*integral_constant_expression_p=*/false);
15037
15038 --cp_unevaluated_operand;
15039 --c_inhibit_evaluation_warnings;
15040
15041 type = finish_typeof (type);
15042 return cp_build_qualified_type_real (type,
15043 cp_type_quals (t)
15044 | cp_type_quals (type),
15045 complain);
15046 }
15047
15048 case DECLTYPE_TYPE:
15049 {
15050 tree type;
15051
15052 ++cp_unevaluated_operand;
15053 ++c_inhibit_evaluation_warnings;
15054
15055 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
15056 complain|tf_decltype, in_decl,
15057 /*function_p*/false,
15058 /*integral_constant_expression*/false);
15059
15060 if (DECLTYPE_FOR_INIT_CAPTURE (t))
15061 {
15062 if (type == NULL_TREE)
15063 {
15064 if (complain & tf_error)
15065 error ("empty initializer in lambda init-capture");
15066 type = error_mark_node;
15067 }
15068 else if (TREE_CODE (type) == TREE_LIST)
15069 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
15070 }
15071
15072 --cp_unevaluated_operand;
15073 --c_inhibit_evaluation_warnings;
15074
15075 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
15076 type = lambda_capture_field_type (type,
15077 DECLTYPE_FOR_INIT_CAPTURE (t),
15078 DECLTYPE_FOR_REF_CAPTURE (t));
15079 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
15080 type = lambda_proxy_type (type);
15081 else
15082 {
15083 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
15084 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
15085 && EXPR_P (type))
15086 /* In a template ~id could be either a complement expression
15087 or an unqualified-id naming a destructor; if instantiating
15088 it produces an expression, it's not an id-expression or
15089 member access. */
15090 id = false;
15091 type = finish_decltype_type (type, id, complain);
15092 }
15093 return cp_build_qualified_type_real (type,
15094 cp_type_quals (t)
15095 | cp_type_quals (type),
15096 complain | tf_ignore_bad_quals);
15097 }
15098
15099 case UNDERLYING_TYPE:
15100 {
15101 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
15102 complain, in_decl);
15103 return finish_underlying_type (type);
15104 }
15105
15106 case TYPE_ARGUMENT_PACK:
15107 case NONTYPE_ARGUMENT_PACK:
15108 {
15109 tree r;
15110
15111 if (code == NONTYPE_ARGUMENT_PACK)
15112 r = make_node (code);
15113 else
15114 r = cxx_make_type (code);
15115
15116 tree pack_args = ARGUMENT_PACK_ARGS (t);
15117 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
15118 SET_ARGUMENT_PACK_ARGS (r, pack_args);
15119
15120 return r;
15121 }
15122
15123 case VOID_CST:
15124 case INTEGER_CST:
15125 case REAL_CST:
15126 case STRING_CST:
15127 case PLUS_EXPR:
15128 case MINUS_EXPR:
15129 case NEGATE_EXPR:
15130 case NOP_EXPR:
15131 case INDIRECT_REF:
15132 case ADDR_EXPR:
15133 case CALL_EXPR:
15134 case ARRAY_REF:
15135 case SCOPE_REF:
15136 /* We should use one of the expression tsubsts for these codes. */
15137 gcc_unreachable ();
15138
15139 default:
15140 sorry ("use of %qs in template", get_tree_code_name (code));
15141 return error_mark_node;
15142 }
15143 }
15144
15145 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
15146 expression on the left-hand side of the "." or "->" operator. We
15147 only do the lookup if we had a dependent BASELINK. Otherwise we
15148 adjust it onto the instantiated heirarchy. */
15149
15150 static tree
15151 tsubst_baselink (tree baselink, tree object_type,
15152 tree args, tsubst_flags_t complain, tree in_decl)
15153 {
15154 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
15155 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
15156 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
15157
15158 tree optype = BASELINK_OPTYPE (baselink);
15159 optype = tsubst (optype, args, complain, in_decl);
15160
15161 tree template_args = NULL_TREE;
15162 bool template_id_p = false;
15163 tree fns = BASELINK_FUNCTIONS (baselink);
15164 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
15165 {
15166 template_id_p = true;
15167 template_args = TREE_OPERAND (fns, 1);
15168 fns = TREE_OPERAND (fns, 0);
15169 if (template_args)
15170 template_args = tsubst_template_args (template_args, args,
15171 complain, in_decl);
15172 }
15173
15174 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
15175 binfo_type = tsubst (binfo_type, args, complain, in_decl);
15176 bool dependent_p = binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink));
15177
15178 if (dependent_p)
15179 {
15180 tree name = OVL_NAME (fns);
15181 if (IDENTIFIER_CONV_OP_P (name))
15182 name = make_conv_op_name (optype);
15183
15184 if (name == complete_dtor_identifier)
15185 /* Treat as-if non-dependent below. */
15186 dependent_p = false;
15187
15188 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
15189 if (!baselink)
15190 {
15191 if ((complain & tf_error)
15192 && constructor_name_p (name, qualifying_scope))
15193 error ("cannot call constructor %<%T::%D%> directly",
15194 qualifying_scope, name);
15195 return error_mark_node;
15196 }
15197
15198 if (BASELINK_P (baselink))
15199 fns = BASELINK_FUNCTIONS (baselink);
15200 }
15201 else
15202 /* We're going to overwrite pieces below, make a duplicate. */
15203 baselink = copy_node (baselink);
15204
15205 /* If lookup found a single function, mark it as used at this point.
15206 (If lookup found multiple functions the one selected later by
15207 overload resolution will be marked as used at that point.) */
15208 if (!template_id_p && !really_overloaded_fn (fns))
15209 {
15210 tree fn = OVL_FIRST (fns);
15211 bool ok = mark_used (fn, complain);
15212 if (!ok && !(complain & tf_error))
15213 return error_mark_node;
15214 if (ok && BASELINK_P (baselink))
15215 /* We might have instantiated an auto function. */
15216 TREE_TYPE (baselink) = TREE_TYPE (fn);
15217 }
15218
15219 if (BASELINK_P (baselink))
15220 {
15221 /* Add back the template arguments, if present. */
15222 if (template_id_p)
15223 BASELINK_FUNCTIONS (baselink)
15224 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
15225
15226 /* Update the conversion operator type. */
15227 BASELINK_OPTYPE (baselink) = optype;
15228 }
15229
15230 if (!object_type)
15231 object_type = current_class_type;
15232
15233 if (qualified_p || !dependent_p)
15234 {
15235 baselink = adjust_result_of_qualified_name_lookup (baselink,
15236 qualifying_scope,
15237 object_type);
15238 if (!qualified_p)
15239 /* We need to call adjust_result_of_qualified_name_lookup in case the
15240 destructor names a base class, but we unset BASELINK_QUALIFIED_P
15241 so that we still get virtual function binding. */
15242 BASELINK_QUALIFIED_P (baselink) = false;
15243 }
15244
15245 return baselink;
15246 }
15247
15248 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
15249 true if the qualified-id will be a postfix-expression in-and-of
15250 itself; false if more of the postfix-expression follows the
15251 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
15252 of "&". */
15253
15254 static tree
15255 tsubst_qualified_id (tree qualified_id, tree args,
15256 tsubst_flags_t complain, tree in_decl,
15257 bool done, bool address_p)
15258 {
15259 tree expr;
15260 tree scope;
15261 tree name;
15262 bool is_template;
15263 tree template_args;
15264 location_t loc = UNKNOWN_LOCATION;
15265
15266 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
15267
15268 /* Figure out what name to look up. */
15269 name = TREE_OPERAND (qualified_id, 1);
15270 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
15271 {
15272 is_template = true;
15273 loc = EXPR_LOCATION (name);
15274 template_args = TREE_OPERAND (name, 1);
15275 if (template_args)
15276 template_args = tsubst_template_args (template_args, args,
15277 complain, in_decl);
15278 if (template_args == error_mark_node)
15279 return error_mark_node;
15280 name = TREE_OPERAND (name, 0);
15281 }
15282 else
15283 {
15284 is_template = false;
15285 template_args = NULL_TREE;
15286 }
15287
15288 /* Substitute into the qualifying scope. When there are no ARGS, we
15289 are just trying to simplify a non-dependent expression. In that
15290 case the qualifying scope may be dependent, and, in any case,
15291 substituting will not help. */
15292 scope = TREE_OPERAND (qualified_id, 0);
15293 if (args)
15294 {
15295 scope = tsubst (scope, args, complain, in_decl);
15296 expr = tsubst_copy (name, args, complain, in_decl);
15297 }
15298 else
15299 expr = name;
15300
15301 if (dependent_scope_p (scope))
15302 {
15303 if (is_template)
15304 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
15305 tree r = build_qualified_name (NULL_TREE, scope, expr,
15306 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
15307 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
15308 return r;
15309 }
15310
15311 if (!BASELINK_P (name) && !DECL_P (expr))
15312 {
15313 if (TREE_CODE (expr) == BIT_NOT_EXPR)
15314 {
15315 /* A BIT_NOT_EXPR is used to represent a destructor. */
15316 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
15317 {
15318 error ("qualifying type %qT does not match destructor name ~%qT",
15319 scope, TREE_OPERAND (expr, 0));
15320 expr = error_mark_node;
15321 }
15322 else
15323 expr = lookup_qualified_name (scope, complete_dtor_identifier,
15324 /*is_type_p=*/0, false);
15325 }
15326 else
15327 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
15328 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
15329 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
15330 {
15331 if (complain & tf_error)
15332 {
15333 error ("dependent-name %qE is parsed as a non-type, but "
15334 "instantiation yields a type", qualified_id);
15335 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
15336 }
15337 return error_mark_node;
15338 }
15339 }
15340
15341 if (DECL_P (expr))
15342 {
15343 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
15344 scope);
15345 /* Remember that there was a reference to this entity. */
15346 if (!mark_used (expr, complain) && !(complain & tf_error))
15347 return error_mark_node;
15348 }
15349
15350 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
15351 {
15352 if (complain & tf_error)
15353 qualified_name_lookup_error (scope,
15354 TREE_OPERAND (qualified_id, 1),
15355 expr, input_location);
15356 return error_mark_node;
15357 }
15358
15359 if (is_template)
15360 {
15361 /* We may be repeating a check already done during parsing, but
15362 if it was well-formed and passed then, it will pass again
15363 now, and if it didn't, we wouldn't have got here. The case
15364 we want to catch is when we couldn't tell then, and can now,
15365 namely when templ prior to substitution was an
15366 identifier. */
15367 if (flag_concepts && check_auto_in_tmpl_args (expr, template_args))
15368 return error_mark_node;
15369
15370 if (variable_template_p (expr))
15371 expr = lookup_and_finish_template_variable (expr, template_args,
15372 complain);
15373 else
15374 expr = lookup_template_function (expr, template_args);
15375 }
15376
15377 if (expr == error_mark_node && complain & tf_error)
15378 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
15379 expr, input_location);
15380 else if (TYPE_P (scope))
15381 {
15382 expr = (adjust_result_of_qualified_name_lookup
15383 (expr, scope, current_nonlambda_class_type ()));
15384 expr = (finish_qualified_id_expr
15385 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
15386 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
15387 /*template_arg_p=*/false, complain));
15388 }
15389
15390 /* Expressions do not generally have reference type. */
15391 if (TREE_CODE (expr) != SCOPE_REF
15392 /* However, if we're about to form a pointer-to-member, we just
15393 want the referenced member referenced. */
15394 && TREE_CODE (expr) != OFFSET_REF)
15395 expr = convert_from_reference (expr);
15396
15397 if (REF_PARENTHESIZED_P (qualified_id))
15398 expr = force_paren_expr (expr);
15399
15400 return expr;
15401 }
15402
15403 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
15404 initializer, DECL is the substituted VAR_DECL. Other arguments are as
15405 for tsubst. */
15406
15407 static tree
15408 tsubst_init (tree init, tree decl, tree args,
15409 tsubst_flags_t complain, tree in_decl)
15410 {
15411 if (!init)
15412 return NULL_TREE;
15413
15414 init = tsubst_expr (init, args, complain, in_decl, false);
15415
15416 if (!init && TREE_TYPE (decl) != error_mark_node)
15417 {
15418 /* If we had an initializer but it
15419 instantiated to nothing,
15420 value-initialize the object. This will
15421 only occur when the initializer was a
15422 pack expansion where the parameter packs
15423 used in that expansion were of length
15424 zero. */
15425 init = build_value_init (TREE_TYPE (decl),
15426 complain);
15427 if (TREE_CODE (init) == AGGR_INIT_EXPR)
15428 init = get_target_expr_sfinae (init, complain);
15429 if (TREE_CODE (init) == TARGET_EXPR)
15430 TARGET_EXPR_DIRECT_INIT_P (init) = true;
15431 }
15432
15433 return init;
15434 }
15435
15436 /* Like tsubst, but deals with expressions. This function just replaces
15437 template parms; to finish processing the resultant expression, use
15438 tsubst_copy_and_build or tsubst_expr. */
15439
15440 static tree
15441 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15442 {
15443 enum tree_code code;
15444 tree r;
15445
15446 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
15447 return t;
15448
15449 code = TREE_CODE (t);
15450
15451 switch (code)
15452 {
15453 case PARM_DECL:
15454 r = retrieve_local_specialization (t);
15455
15456 if (r == NULL_TREE)
15457 {
15458 /* We get here for a use of 'this' in an NSDMI. */
15459 if (DECL_NAME (t) == this_identifier && current_class_ptr)
15460 return current_class_ptr;
15461
15462 /* This can happen for a parameter name used later in a function
15463 declaration (such as in a late-specified return type). Just
15464 make a dummy decl, since it's only used for its type. */
15465 gcc_assert (cp_unevaluated_operand != 0);
15466 r = tsubst_decl (t, args, complain);
15467 /* Give it the template pattern as its context; its true context
15468 hasn't been instantiated yet and this is good enough for
15469 mangling. */
15470 DECL_CONTEXT (r) = DECL_CONTEXT (t);
15471 }
15472
15473 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
15474 r = argument_pack_select_arg (r);
15475 if (!mark_used (r, complain) && !(complain & tf_error))
15476 return error_mark_node;
15477 return r;
15478
15479 case CONST_DECL:
15480 {
15481 tree enum_type;
15482 tree v;
15483
15484 if (DECL_TEMPLATE_PARM_P (t))
15485 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
15486 /* There is no need to substitute into namespace-scope
15487 enumerators. */
15488 if (DECL_NAMESPACE_SCOPE_P (t))
15489 return t;
15490 /* If ARGS is NULL, then T is known to be non-dependent. */
15491 if (args == NULL_TREE)
15492 return scalar_constant_value (t);
15493
15494 /* Unfortunately, we cannot just call lookup_name here.
15495 Consider:
15496
15497 template <int I> int f() {
15498 enum E { a = I };
15499 struct S { void g() { E e = a; } };
15500 };
15501
15502 When we instantiate f<7>::S::g(), say, lookup_name is not
15503 clever enough to find f<7>::a. */
15504 enum_type
15505 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
15506 /*entering_scope=*/0);
15507
15508 for (v = TYPE_VALUES (enum_type);
15509 v != NULL_TREE;
15510 v = TREE_CHAIN (v))
15511 if (TREE_PURPOSE (v) == DECL_NAME (t))
15512 return TREE_VALUE (v);
15513
15514 /* We didn't find the name. That should never happen; if
15515 name-lookup found it during preliminary parsing, we
15516 should find it again here during instantiation. */
15517 gcc_unreachable ();
15518 }
15519 return t;
15520
15521 case FIELD_DECL:
15522 if (DECL_CONTEXT (t))
15523 {
15524 tree ctx;
15525
15526 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
15527 /*entering_scope=*/1);
15528 if (ctx != DECL_CONTEXT (t))
15529 {
15530 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
15531 if (!r)
15532 {
15533 if (complain & tf_error)
15534 error ("using invalid field %qD", t);
15535 return error_mark_node;
15536 }
15537 return r;
15538 }
15539 }
15540
15541 return t;
15542
15543 case VAR_DECL:
15544 case FUNCTION_DECL:
15545 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
15546 r = tsubst (t, args, complain, in_decl);
15547 else if (local_variable_p (t)
15548 && uses_template_parms (DECL_CONTEXT (t)))
15549 {
15550 r = retrieve_local_specialization (t);
15551 if (r == NULL_TREE)
15552 {
15553 /* First try name lookup to find the instantiation. */
15554 r = lookup_name (DECL_NAME (t));
15555 if (r && !is_capture_proxy (r))
15556 {
15557 /* Make sure that the one we found is the one we want. */
15558 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
15559 if (ctx != DECL_CONTEXT (r))
15560 r = NULL_TREE;
15561 }
15562
15563 if (r)
15564 /* OK */;
15565 else
15566 {
15567 /* This can happen for a variable used in a
15568 late-specified return type of a local lambda, or for a
15569 local static or constant. Building a new VAR_DECL
15570 should be OK in all those cases. */
15571 r = tsubst_decl (t, args, complain);
15572 if (local_specializations)
15573 /* Avoid infinite recursion (79640). */
15574 register_local_specialization (r, t);
15575 if (decl_maybe_constant_var_p (r))
15576 {
15577 /* We can't call cp_finish_decl, so handle the
15578 initializer by hand. */
15579 tree init = tsubst_init (DECL_INITIAL (t), r, args,
15580 complain, in_decl);
15581 if (!processing_template_decl)
15582 init = maybe_constant_init (init);
15583 if (processing_template_decl
15584 ? potential_constant_expression (init)
15585 : reduced_constant_expression_p (init))
15586 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
15587 = TREE_CONSTANT (r) = true;
15588 DECL_INITIAL (r) = init;
15589 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
15590 TREE_TYPE (r)
15591 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
15592 complain, adc_variable_type);
15593 }
15594 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
15595 || decl_constant_var_p (r)
15596 || errorcount || sorrycount);
15597 if (!processing_template_decl
15598 && !TREE_STATIC (r))
15599 r = process_outer_var_ref (r, complain);
15600 }
15601 /* Remember this for subsequent uses. */
15602 if (local_specializations)
15603 register_local_specialization (r, t);
15604 }
15605 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
15606 r = argument_pack_select_arg (r);
15607 }
15608 else
15609 r = t;
15610 if (!mark_used (r, complain))
15611 return error_mark_node;
15612 return r;
15613
15614 case NAMESPACE_DECL:
15615 return t;
15616
15617 case OVERLOAD:
15618 /* We must have marked any lookups as persistent. */
15619 gcc_assert (!OVL_LOOKUP_P (t) || OVL_USED_P (t));
15620 return t;
15621
15622 case BASELINK:
15623 return tsubst_baselink (t, current_nonlambda_class_type (),
15624 args, complain, in_decl);
15625
15626 case TEMPLATE_DECL:
15627 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
15628 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
15629 args, complain, in_decl);
15630 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
15631 return tsubst (t, args, complain, in_decl);
15632 else if (DECL_CLASS_SCOPE_P (t)
15633 && uses_template_parms (DECL_CONTEXT (t)))
15634 {
15635 /* Template template argument like the following example need
15636 special treatment:
15637
15638 template <template <class> class TT> struct C {};
15639 template <class T> struct D {
15640 template <class U> struct E {};
15641 C<E> c; // #1
15642 };
15643 D<int> d; // #2
15644
15645 We are processing the template argument `E' in #1 for
15646 the template instantiation #2. Originally, `E' is a
15647 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
15648 have to substitute this with one having context `D<int>'. */
15649
15650 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
15651 if (dependent_scope_p (context))
15652 {
15653 /* When rewriting a constructor into a deduction guide, a
15654 non-dependent name can become dependent, so memtmpl<args>
15655 becomes context::template memtmpl<args>. */
15656 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15657 return build_qualified_name (type, context, DECL_NAME (t),
15658 /*template*/true);
15659 }
15660 return lookup_field (context, DECL_NAME(t), 0, false);
15661 }
15662 else
15663 /* Ordinary template template argument. */
15664 return t;
15665
15666 case NON_LVALUE_EXPR:
15667 case VIEW_CONVERT_EXPR:
15668 {
15669 /* Handle location wrappers by substituting the wrapped node
15670 first, *then* reusing the resulting type. Doing the type
15671 first ensures that we handle template parameters and
15672 parameter pack expansions. */
15673 if (location_wrapper_p (t))
15674 {
15675 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args,
15676 complain, in_decl);
15677 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
15678 }
15679 tree op = TREE_OPERAND (t, 0);
15680 if (code == VIEW_CONVERT_EXPR
15681 && TREE_CODE (op) == TEMPLATE_PARM_INDEX)
15682 {
15683 /* Wrapper to make a C++20 template parameter object const. */
15684 op = tsubst_copy (op, args, complain, in_decl);
15685 if (TREE_CODE (op) == TEMPLATE_PARM_INDEX)
15686 {
15687 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15688 return build1 (code, type, op);
15689 }
15690 else
15691 {
15692 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (op)));
15693 return op;
15694 }
15695 }
15696 /* We shouldn't see any other uses of these in templates. */
15697 gcc_unreachable ();
15698 }
15699
15700 case CAST_EXPR:
15701 case REINTERPRET_CAST_EXPR:
15702 case CONST_CAST_EXPR:
15703 case STATIC_CAST_EXPR:
15704 case DYNAMIC_CAST_EXPR:
15705 case IMPLICIT_CONV_EXPR:
15706 case CONVERT_EXPR:
15707 case NOP_EXPR:
15708 {
15709 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15710 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15711 return build1 (code, type, op0);
15712 }
15713
15714 case SIZEOF_EXPR:
15715 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
15716 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
15717 {
15718 tree expanded, op = TREE_OPERAND (t, 0);
15719 int len = 0;
15720
15721 if (SIZEOF_EXPR_TYPE_P (t))
15722 op = TREE_TYPE (op);
15723
15724 ++cp_unevaluated_operand;
15725 ++c_inhibit_evaluation_warnings;
15726 /* We only want to compute the number of arguments. */
15727 if (PACK_EXPANSION_P (op))
15728 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
15729 else
15730 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
15731 args, complain, in_decl);
15732 --cp_unevaluated_operand;
15733 --c_inhibit_evaluation_warnings;
15734
15735 if (TREE_CODE (expanded) == TREE_VEC)
15736 {
15737 len = TREE_VEC_LENGTH (expanded);
15738 /* Set TREE_USED for the benefit of -Wunused. */
15739 for (int i = 0; i < len; i++)
15740 if (DECL_P (TREE_VEC_ELT (expanded, i)))
15741 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
15742 }
15743
15744 if (expanded == error_mark_node)
15745 return error_mark_node;
15746 else if (PACK_EXPANSION_P (expanded)
15747 || (TREE_CODE (expanded) == TREE_VEC
15748 && pack_expansion_args_count (expanded)))
15749
15750 {
15751 if (PACK_EXPANSION_P (expanded))
15752 /* OK. */;
15753 else if (TREE_VEC_LENGTH (expanded) == 1)
15754 expanded = TREE_VEC_ELT (expanded, 0);
15755 else
15756 expanded = make_argument_pack (expanded);
15757
15758 if (TYPE_P (expanded))
15759 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
15760 false,
15761 complain & tf_error);
15762 else
15763 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
15764 complain & tf_error);
15765 }
15766 else
15767 return build_int_cst (size_type_node, len);
15768 }
15769 if (SIZEOF_EXPR_TYPE_P (t))
15770 {
15771 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
15772 args, complain, in_decl);
15773 r = build1 (NOP_EXPR, r, error_mark_node);
15774 r = build1 (SIZEOF_EXPR,
15775 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
15776 SIZEOF_EXPR_TYPE_P (r) = 1;
15777 return r;
15778 }
15779 /* Fall through */
15780
15781 case INDIRECT_REF:
15782 case NEGATE_EXPR:
15783 case TRUTH_NOT_EXPR:
15784 case BIT_NOT_EXPR:
15785 case ADDR_EXPR:
15786 case UNARY_PLUS_EXPR: /* Unary + */
15787 case ALIGNOF_EXPR:
15788 case AT_ENCODE_EXPR:
15789 case ARROW_EXPR:
15790 case THROW_EXPR:
15791 case TYPEID_EXPR:
15792 case REALPART_EXPR:
15793 case IMAGPART_EXPR:
15794 case PAREN_EXPR:
15795 {
15796 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15797 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15798 r = build1 (code, type, op0);
15799 if (code == ALIGNOF_EXPR)
15800 ALIGNOF_EXPR_STD_P (r) = ALIGNOF_EXPR_STD_P (t);
15801 return r;
15802 }
15803
15804 case COMPONENT_REF:
15805 {
15806 tree object;
15807 tree name;
15808
15809 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15810 name = TREE_OPERAND (t, 1);
15811 if (TREE_CODE (name) == BIT_NOT_EXPR)
15812 {
15813 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15814 complain, in_decl);
15815 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15816 }
15817 else if (TREE_CODE (name) == SCOPE_REF
15818 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
15819 {
15820 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
15821 complain, in_decl);
15822 name = TREE_OPERAND (name, 1);
15823 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15824 complain, in_decl);
15825 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15826 name = build_qualified_name (/*type=*/NULL_TREE,
15827 base, name,
15828 /*template_p=*/false);
15829 }
15830 else if (BASELINK_P (name))
15831 name = tsubst_baselink (name,
15832 non_reference (TREE_TYPE (object)),
15833 args, complain,
15834 in_decl);
15835 else
15836 name = tsubst_copy (name, args, complain, in_decl);
15837 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
15838 }
15839
15840 case PLUS_EXPR:
15841 case MINUS_EXPR:
15842 case MULT_EXPR:
15843 case TRUNC_DIV_EXPR:
15844 case CEIL_DIV_EXPR:
15845 case FLOOR_DIV_EXPR:
15846 case ROUND_DIV_EXPR:
15847 case EXACT_DIV_EXPR:
15848 case BIT_AND_EXPR:
15849 case BIT_IOR_EXPR:
15850 case BIT_XOR_EXPR:
15851 case TRUNC_MOD_EXPR:
15852 case FLOOR_MOD_EXPR:
15853 case TRUTH_ANDIF_EXPR:
15854 case TRUTH_ORIF_EXPR:
15855 case TRUTH_AND_EXPR:
15856 case TRUTH_OR_EXPR:
15857 case RSHIFT_EXPR:
15858 case LSHIFT_EXPR:
15859 case RROTATE_EXPR:
15860 case LROTATE_EXPR:
15861 case EQ_EXPR:
15862 case NE_EXPR:
15863 case MAX_EXPR:
15864 case MIN_EXPR:
15865 case LE_EXPR:
15866 case GE_EXPR:
15867 case LT_EXPR:
15868 case GT_EXPR:
15869 case COMPOUND_EXPR:
15870 case DOTSTAR_EXPR:
15871 case MEMBER_REF:
15872 case PREDECREMENT_EXPR:
15873 case PREINCREMENT_EXPR:
15874 case POSTDECREMENT_EXPR:
15875 case POSTINCREMENT_EXPR:
15876 {
15877 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15878 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15879 return build_nt (code, op0, op1);
15880 }
15881
15882 case SCOPE_REF:
15883 {
15884 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15885 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15886 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
15887 QUALIFIED_NAME_IS_TEMPLATE (t));
15888 }
15889
15890 case ARRAY_REF:
15891 {
15892 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15893 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15894 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
15895 }
15896
15897 case CALL_EXPR:
15898 {
15899 int n = VL_EXP_OPERAND_LENGTH (t);
15900 tree result = build_vl_exp (CALL_EXPR, n);
15901 int i;
15902 for (i = 0; i < n; i++)
15903 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
15904 complain, in_decl);
15905 return result;
15906 }
15907
15908 case COND_EXPR:
15909 case MODOP_EXPR:
15910 case PSEUDO_DTOR_EXPR:
15911 case VEC_PERM_EXPR:
15912 {
15913 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15914 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15915 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15916 r = build_nt (code, op0, op1, op2);
15917 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
15918 return r;
15919 }
15920
15921 case NEW_EXPR:
15922 {
15923 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15924 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15925 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15926 r = build_nt (code, op0, op1, op2);
15927 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
15928 return r;
15929 }
15930
15931 case DELETE_EXPR:
15932 {
15933 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15934 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15935 r = build_nt (code, op0, op1);
15936 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
15937 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
15938 return r;
15939 }
15940
15941 case TEMPLATE_ID_EXPR:
15942 {
15943 /* Substituted template arguments */
15944 tree fn = TREE_OPERAND (t, 0);
15945 tree targs = TREE_OPERAND (t, 1);
15946
15947 fn = tsubst_copy (fn, args, complain, in_decl);
15948 if (targs)
15949 targs = tsubst_template_args (targs, args, complain, in_decl);
15950
15951 return lookup_template_function (fn, targs);
15952 }
15953
15954 case TREE_LIST:
15955 {
15956 tree purpose, value, chain;
15957
15958 if (t == void_list_node)
15959 return t;
15960
15961 purpose = TREE_PURPOSE (t);
15962 if (purpose)
15963 purpose = tsubst_copy (purpose, args, complain, in_decl);
15964 value = TREE_VALUE (t);
15965 if (value)
15966 value = tsubst_copy (value, args, complain, in_decl);
15967 chain = TREE_CHAIN (t);
15968 if (chain && chain != void_type_node)
15969 chain = tsubst_copy (chain, args, complain, in_decl);
15970 if (purpose == TREE_PURPOSE (t)
15971 && value == TREE_VALUE (t)
15972 && chain == TREE_CHAIN (t))
15973 return t;
15974 return tree_cons (purpose, value, chain);
15975 }
15976
15977 case RECORD_TYPE:
15978 case UNION_TYPE:
15979 case ENUMERAL_TYPE:
15980 case INTEGER_TYPE:
15981 case TEMPLATE_TYPE_PARM:
15982 case TEMPLATE_TEMPLATE_PARM:
15983 case BOUND_TEMPLATE_TEMPLATE_PARM:
15984 case TEMPLATE_PARM_INDEX:
15985 case POINTER_TYPE:
15986 case REFERENCE_TYPE:
15987 case OFFSET_TYPE:
15988 case FUNCTION_TYPE:
15989 case METHOD_TYPE:
15990 case ARRAY_TYPE:
15991 case TYPENAME_TYPE:
15992 case UNBOUND_CLASS_TEMPLATE:
15993 case TYPEOF_TYPE:
15994 case DECLTYPE_TYPE:
15995 case TYPE_DECL:
15996 return tsubst (t, args, complain, in_decl);
15997
15998 case USING_DECL:
15999 t = DECL_NAME (t);
16000 /* Fall through. */
16001 case IDENTIFIER_NODE:
16002 if (IDENTIFIER_CONV_OP_P (t))
16003 {
16004 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16005 return make_conv_op_name (new_type);
16006 }
16007 else
16008 return t;
16009
16010 case CONSTRUCTOR:
16011 /* This is handled by tsubst_copy_and_build. */
16012 gcc_unreachable ();
16013
16014 case VA_ARG_EXPR:
16015 {
16016 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16017 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16018 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
16019 }
16020
16021 case CLEANUP_POINT_EXPR:
16022 /* We shouldn't have built any of these during initial template
16023 generation. Instead, they should be built during instantiation
16024 in response to the saved STMT_IS_FULL_EXPR_P setting. */
16025 gcc_unreachable ();
16026
16027 case OFFSET_REF:
16028 {
16029 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16030 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16031 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16032 r = build2 (code, type, op0, op1);
16033 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
16034 if (!mark_used (TREE_OPERAND (r, 1), complain)
16035 && !(complain & tf_error))
16036 return error_mark_node;
16037 return r;
16038 }
16039
16040 case EXPR_PACK_EXPANSION:
16041 error ("invalid use of pack expansion expression");
16042 return error_mark_node;
16043
16044 case NONTYPE_ARGUMENT_PACK:
16045 error ("use %<...%> to expand argument pack");
16046 return error_mark_node;
16047
16048 case VOID_CST:
16049 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
16050 return t;
16051
16052 case INTEGER_CST:
16053 case REAL_CST:
16054 case STRING_CST:
16055 case COMPLEX_CST:
16056 {
16057 /* Instantiate any typedefs in the type. */
16058 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16059 r = fold_convert (type, t);
16060 gcc_assert (TREE_CODE (r) == code);
16061 return r;
16062 }
16063
16064 case PTRMEM_CST:
16065 /* These can sometimes show up in a partial instantiation, but never
16066 involve template parms. */
16067 gcc_assert (!uses_template_parms (t));
16068 return t;
16069
16070 case UNARY_LEFT_FOLD_EXPR:
16071 return tsubst_unary_left_fold (t, args, complain, in_decl);
16072 case UNARY_RIGHT_FOLD_EXPR:
16073 return tsubst_unary_right_fold (t, args, complain, in_decl);
16074 case BINARY_LEFT_FOLD_EXPR:
16075 return tsubst_binary_left_fold (t, args, complain, in_decl);
16076 case BINARY_RIGHT_FOLD_EXPR:
16077 return tsubst_binary_right_fold (t, args, complain, in_decl);
16078 case PREDICT_EXPR:
16079 return t;
16080
16081 case DEBUG_BEGIN_STMT:
16082 /* ??? There's no point in copying it for now, but maybe some
16083 day it will contain more information, such as a pointer back
16084 to the containing function, inlined copy or so. */
16085 return t;
16086
16087 default:
16088 /* We shouldn't get here, but keep going if !flag_checking. */
16089 if (flag_checking)
16090 gcc_unreachable ();
16091 return t;
16092 }
16093 }
16094
16095 /* Helper function for tsubst_omp_clauses, used for instantiation of
16096 OMP_CLAUSE_DECL of clauses. */
16097
16098 static tree
16099 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
16100 tree in_decl)
16101 {
16102 if (decl == NULL_TREE)
16103 return NULL_TREE;
16104
16105 /* Handle an OpenMP array section represented as a TREE_LIST (or
16106 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
16107 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
16108 TREE_LIST. We can handle it exactly the same as an array section
16109 (purpose, value, and a chain), even though the nomenclature
16110 (low_bound, length, etc) is different. */
16111 if (TREE_CODE (decl) == TREE_LIST)
16112 {
16113 tree low_bound
16114 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
16115 /*integral_constant_expression_p=*/false);
16116 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
16117 /*integral_constant_expression_p=*/false);
16118 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
16119 in_decl);
16120 if (TREE_PURPOSE (decl) == low_bound
16121 && TREE_VALUE (decl) == length
16122 && TREE_CHAIN (decl) == chain)
16123 return decl;
16124 tree ret = tree_cons (low_bound, length, chain);
16125 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
16126 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
16127 return ret;
16128 }
16129 tree ret = tsubst_expr (decl, args, complain, in_decl,
16130 /*integral_constant_expression_p=*/false);
16131 /* Undo convert_from_reference tsubst_expr could have called. */
16132 if (decl
16133 && REFERENCE_REF_P (ret)
16134 && !REFERENCE_REF_P (decl))
16135 ret = TREE_OPERAND (ret, 0);
16136 return ret;
16137 }
16138
16139 /* Like tsubst_copy, but specifically for OpenMP clauses. */
16140
16141 static tree
16142 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
16143 tree args, tsubst_flags_t complain, tree in_decl)
16144 {
16145 tree new_clauses = NULL_TREE, nc, oc;
16146 tree linear_no_step = NULL_TREE;
16147
16148 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
16149 {
16150 nc = copy_node (oc);
16151 OMP_CLAUSE_CHAIN (nc) = new_clauses;
16152 new_clauses = nc;
16153
16154 switch (OMP_CLAUSE_CODE (nc))
16155 {
16156 case OMP_CLAUSE_LASTPRIVATE:
16157 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
16158 {
16159 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
16160 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
16161 in_decl, /*integral_constant_expression_p=*/false);
16162 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
16163 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
16164 }
16165 /* FALLTHRU */
16166 case OMP_CLAUSE_PRIVATE:
16167 case OMP_CLAUSE_SHARED:
16168 case OMP_CLAUSE_FIRSTPRIVATE:
16169 case OMP_CLAUSE_COPYIN:
16170 case OMP_CLAUSE_COPYPRIVATE:
16171 case OMP_CLAUSE_UNIFORM:
16172 case OMP_CLAUSE_DEPEND:
16173 case OMP_CLAUSE_FROM:
16174 case OMP_CLAUSE_TO:
16175 case OMP_CLAUSE_MAP:
16176 case OMP_CLAUSE_USE_DEVICE_PTR:
16177 case OMP_CLAUSE_IS_DEVICE_PTR:
16178 OMP_CLAUSE_DECL (nc)
16179 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16180 in_decl);
16181 break;
16182 case OMP_CLAUSE_TILE:
16183 case OMP_CLAUSE_IF:
16184 case OMP_CLAUSE_NUM_THREADS:
16185 case OMP_CLAUSE_SCHEDULE:
16186 case OMP_CLAUSE_COLLAPSE:
16187 case OMP_CLAUSE_FINAL:
16188 case OMP_CLAUSE_DEVICE:
16189 case OMP_CLAUSE_DIST_SCHEDULE:
16190 case OMP_CLAUSE_NUM_TEAMS:
16191 case OMP_CLAUSE_THREAD_LIMIT:
16192 case OMP_CLAUSE_SAFELEN:
16193 case OMP_CLAUSE_SIMDLEN:
16194 case OMP_CLAUSE_NUM_TASKS:
16195 case OMP_CLAUSE_GRAINSIZE:
16196 case OMP_CLAUSE_PRIORITY:
16197 case OMP_CLAUSE_ORDERED:
16198 case OMP_CLAUSE_HINT:
16199 case OMP_CLAUSE_NUM_GANGS:
16200 case OMP_CLAUSE_NUM_WORKERS:
16201 case OMP_CLAUSE_VECTOR_LENGTH:
16202 case OMP_CLAUSE_WORKER:
16203 case OMP_CLAUSE_VECTOR:
16204 case OMP_CLAUSE_ASYNC:
16205 case OMP_CLAUSE_WAIT:
16206 OMP_CLAUSE_OPERAND (nc, 0)
16207 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
16208 in_decl, /*integral_constant_expression_p=*/false);
16209 break;
16210 case OMP_CLAUSE_REDUCTION:
16211 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
16212 {
16213 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
16214 if (TREE_CODE (placeholder) == SCOPE_REF)
16215 {
16216 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
16217 complain, in_decl);
16218 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
16219 = build_qualified_name (NULL_TREE, scope,
16220 TREE_OPERAND (placeholder, 1),
16221 false);
16222 }
16223 else
16224 gcc_assert (identifier_p (placeholder));
16225 }
16226 OMP_CLAUSE_DECL (nc)
16227 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16228 in_decl);
16229 break;
16230 case OMP_CLAUSE_GANG:
16231 case OMP_CLAUSE_ALIGNED:
16232 OMP_CLAUSE_DECL (nc)
16233 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16234 in_decl);
16235 OMP_CLAUSE_OPERAND (nc, 1)
16236 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
16237 in_decl, /*integral_constant_expression_p=*/false);
16238 break;
16239 case OMP_CLAUSE_LINEAR:
16240 OMP_CLAUSE_DECL (nc)
16241 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16242 in_decl);
16243 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
16244 {
16245 gcc_assert (!linear_no_step);
16246 linear_no_step = nc;
16247 }
16248 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
16249 OMP_CLAUSE_LINEAR_STEP (nc)
16250 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
16251 complain, in_decl);
16252 else
16253 OMP_CLAUSE_LINEAR_STEP (nc)
16254 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
16255 in_decl,
16256 /*integral_constant_expression_p=*/false);
16257 break;
16258 case OMP_CLAUSE_NOWAIT:
16259 case OMP_CLAUSE_DEFAULT:
16260 case OMP_CLAUSE_UNTIED:
16261 case OMP_CLAUSE_MERGEABLE:
16262 case OMP_CLAUSE_INBRANCH:
16263 case OMP_CLAUSE_NOTINBRANCH:
16264 case OMP_CLAUSE_PROC_BIND:
16265 case OMP_CLAUSE_FOR:
16266 case OMP_CLAUSE_PARALLEL:
16267 case OMP_CLAUSE_SECTIONS:
16268 case OMP_CLAUSE_TASKGROUP:
16269 case OMP_CLAUSE_NOGROUP:
16270 case OMP_CLAUSE_THREADS:
16271 case OMP_CLAUSE_SIMD:
16272 case OMP_CLAUSE_DEFAULTMAP:
16273 case OMP_CLAUSE_INDEPENDENT:
16274 case OMP_CLAUSE_AUTO:
16275 case OMP_CLAUSE_SEQ:
16276 case OMP_CLAUSE_IF_PRESENT:
16277 case OMP_CLAUSE_FINALIZE:
16278 break;
16279 default:
16280 gcc_unreachable ();
16281 }
16282 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
16283 switch (OMP_CLAUSE_CODE (nc))
16284 {
16285 case OMP_CLAUSE_SHARED:
16286 case OMP_CLAUSE_PRIVATE:
16287 case OMP_CLAUSE_FIRSTPRIVATE:
16288 case OMP_CLAUSE_LASTPRIVATE:
16289 case OMP_CLAUSE_COPYPRIVATE:
16290 case OMP_CLAUSE_LINEAR:
16291 case OMP_CLAUSE_REDUCTION:
16292 case OMP_CLAUSE_USE_DEVICE_PTR:
16293 case OMP_CLAUSE_IS_DEVICE_PTR:
16294 /* tsubst_expr on SCOPE_REF results in returning
16295 finish_non_static_data_member result. Undo that here. */
16296 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
16297 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
16298 == IDENTIFIER_NODE))
16299 {
16300 tree t = OMP_CLAUSE_DECL (nc);
16301 tree v = t;
16302 while (v)
16303 switch (TREE_CODE (v))
16304 {
16305 case COMPONENT_REF:
16306 case MEM_REF:
16307 case INDIRECT_REF:
16308 CASE_CONVERT:
16309 case POINTER_PLUS_EXPR:
16310 v = TREE_OPERAND (v, 0);
16311 continue;
16312 case PARM_DECL:
16313 if (DECL_CONTEXT (v) == current_function_decl
16314 && DECL_ARTIFICIAL (v)
16315 && DECL_NAME (v) == this_identifier)
16316 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
16317 /* FALLTHRU */
16318 default:
16319 v = NULL_TREE;
16320 break;
16321 }
16322 }
16323 else if (VAR_P (OMP_CLAUSE_DECL (oc))
16324 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
16325 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
16326 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
16327 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
16328 {
16329 tree decl = OMP_CLAUSE_DECL (nc);
16330 if (VAR_P (decl))
16331 {
16332 retrofit_lang_decl (decl);
16333 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
16334 }
16335 }
16336 break;
16337 default:
16338 break;
16339 }
16340 }
16341
16342 new_clauses = nreverse (new_clauses);
16343 if (ort != C_ORT_OMP_DECLARE_SIMD)
16344 {
16345 new_clauses = finish_omp_clauses (new_clauses, ort);
16346 if (linear_no_step)
16347 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
16348 if (nc == linear_no_step)
16349 {
16350 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
16351 break;
16352 }
16353 }
16354 return new_clauses;
16355 }
16356
16357 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
16358
16359 static tree
16360 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
16361 tree in_decl)
16362 {
16363 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
16364
16365 tree purpose, value, chain;
16366
16367 if (t == NULL)
16368 return t;
16369
16370 if (TREE_CODE (t) != TREE_LIST)
16371 return tsubst_copy_and_build (t, args, complain, in_decl,
16372 /*function_p=*/false,
16373 /*integral_constant_expression_p=*/false);
16374
16375 if (t == void_list_node)
16376 return t;
16377
16378 purpose = TREE_PURPOSE (t);
16379 if (purpose)
16380 purpose = RECUR (purpose);
16381 value = TREE_VALUE (t);
16382 if (value)
16383 {
16384 if (TREE_CODE (value) != LABEL_DECL)
16385 value = RECUR (value);
16386 else
16387 {
16388 value = lookup_label (DECL_NAME (value));
16389 gcc_assert (TREE_CODE (value) == LABEL_DECL);
16390 TREE_USED (value) = 1;
16391 }
16392 }
16393 chain = TREE_CHAIN (t);
16394 if (chain && chain != void_type_node)
16395 chain = RECUR (chain);
16396 return tree_cons (purpose, value, chain);
16397 #undef RECUR
16398 }
16399
16400 /* Used to temporarily communicate the list of #pragma omp parallel
16401 clauses to #pragma omp for instantiation if they are combined
16402 together. */
16403
16404 static tree *omp_parallel_combined_clauses;
16405
16406 /* Substitute one OMP_FOR iterator. */
16407
16408 static void
16409 tsubst_omp_for_iterator (tree t, int i, tree declv, tree orig_declv,
16410 tree initv, tree condv, tree incrv, tree *clauses,
16411 tree args, tsubst_flags_t complain, tree in_decl,
16412 bool integral_constant_expression_p)
16413 {
16414 #define RECUR(NODE) \
16415 tsubst_expr ((NODE), args, complain, in_decl, \
16416 integral_constant_expression_p)
16417 tree decl, init, cond, incr;
16418
16419 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
16420 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
16421
16422 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
16423 {
16424 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
16425 if (TREE_CODE (o) == TREE_LIST)
16426 TREE_VEC_ELT (orig_declv, i)
16427 = tree_cons (RECUR (TREE_PURPOSE (o)),
16428 RECUR (TREE_VALUE (o)), NULL_TREE);
16429 else
16430 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
16431 }
16432
16433 decl = TREE_OPERAND (init, 0);
16434 init = TREE_OPERAND (init, 1);
16435 tree decl_expr = NULL_TREE;
16436 if (init && TREE_CODE (init) == DECL_EXPR)
16437 {
16438 /* We need to jump through some hoops to handle declarations in the
16439 init-statement, since we might need to handle auto deduction,
16440 but we need to keep control of initialization. */
16441 decl_expr = init;
16442 init = DECL_INITIAL (DECL_EXPR_DECL (init));
16443 decl = tsubst_decl (decl, args, complain);
16444 }
16445 else
16446 {
16447 if (TREE_CODE (decl) == SCOPE_REF)
16448 {
16449 decl = RECUR (decl);
16450 if (TREE_CODE (decl) == COMPONENT_REF)
16451 {
16452 tree v = decl;
16453 while (v)
16454 switch (TREE_CODE (v))
16455 {
16456 case COMPONENT_REF:
16457 case MEM_REF:
16458 case INDIRECT_REF:
16459 CASE_CONVERT:
16460 case POINTER_PLUS_EXPR:
16461 v = TREE_OPERAND (v, 0);
16462 continue;
16463 case PARM_DECL:
16464 if (DECL_CONTEXT (v) == current_function_decl
16465 && DECL_ARTIFICIAL (v)
16466 && DECL_NAME (v) == this_identifier)
16467 {
16468 decl = TREE_OPERAND (decl, 1);
16469 decl = omp_privatize_field (decl, false);
16470 }
16471 /* FALLTHRU */
16472 default:
16473 v = NULL_TREE;
16474 break;
16475 }
16476 }
16477 }
16478 else
16479 decl = RECUR (decl);
16480 }
16481 init = RECUR (init);
16482
16483 tree auto_node = type_uses_auto (TREE_TYPE (decl));
16484 if (auto_node && init)
16485 TREE_TYPE (decl)
16486 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
16487
16488 gcc_assert (!type_dependent_expression_p (decl));
16489
16490 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
16491 {
16492 if (decl_expr)
16493 {
16494 /* Declare the variable, but don't let that initialize it. */
16495 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
16496 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
16497 RECUR (decl_expr);
16498 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
16499 }
16500
16501 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
16502 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
16503 if (TREE_CODE (incr) == MODIFY_EXPR)
16504 {
16505 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16506 tree rhs = RECUR (TREE_OPERAND (incr, 1));
16507 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
16508 NOP_EXPR, rhs, complain);
16509 }
16510 else
16511 incr = RECUR (incr);
16512 TREE_VEC_ELT (declv, i) = decl;
16513 TREE_VEC_ELT (initv, i) = init;
16514 TREE_VEC_ELT (condv, i) = cond;
16515 TREE_VEC_ELT (incrv, i) = incr;
16516 return;
16517 }
16518
16519 if (decl_expr)
16520 {
16521 /* Declare and initialize the variable. */
16522 RECUR (decl_expr);
16523 init = NULL_TREE;
16524 }
16525 else if (init)
16526 {
16527 tree *pc;
16528 int j;
16529 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
16530 {
16531 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
16532 {
16533 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
16534 && OMP_CLAUSE_DECL (*pc) == decl)
16535 break;
16536 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
16537 && OMP_CLAUSE_DECL (*pc) == decl)
16538 {
16539 if (j)
16540 break;
16541 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
16542 tree c = *pc;
16543 *pc = OMP_CLAUSE_CHAIN (c);
16544 OMP_CLAUSE_CHAIN (c) = *clauses;
16545 *clauses = c;
16546 }
16547 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
16548 && OMP_CLAUSE_DECL (*pc) == decl)
16549 {
16550 error ("iteration variable %qD should not be firstprivate",
16551 decl);
16552 *pc = OMP_CLAUSE_CHAIN (*pc);
16553 }
16554 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
16555 && OMP_CLAUSE_DECL (*pc) == decl)
16556 {
16557 error ("iteration variable %qD should not be reduction",
16558 decl);
16559 *pc = OMP_CLAUSE_CHAIN (*pc);
16560 }
16561 else
16562 pc = &OMP_CLAUSE_CHAIN (*pc);
16563 }
16564 if (*pc)
16565 break;
16566 }
16567 if (*pc == NULL_TREE)
16568 {
16569 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
16570 OMP_CLAUSE_DECL (c) = decl;
16571 c = finish_omp_clauses (c, C_ORT_OMP);
16572 if (c)
16573 {
16574 OMP_CLAUSE_CHAIN (c) = *clauses;
16575 *clauses = c;
16576 }
16577 }
16578 }
16579 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
16580 if (COMPARISON_CLASS_P (cond))
16581 {
16582 tree op0 = RECUR (TREE_OPERAND (cond, 0));
16583 tree op1 = RECUR (TREE_OPERAND (cond, 1));
16584 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
16585 }
16586 else
16587 cond = RECUR (cond);
16588 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
16589 switch (TREE_CODE (incr))
16590 {
16591 case PREINCREMENT_EXPR:
16592 case PREDECREMENT_EXPR:
16593 case POSTINCREMENT_EXPR:
16594 case POSTDECREMENT_EXPR:
16595 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
16596 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
16597 break;
16598 case MODIFY_EXPR:
16599 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
16600 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
16601 {
16602 tree rhs = TREE_OPERAND (incr, 1);
16603 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16604 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
16605 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
16606 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16607 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
16608 rhs0, rhs1));
16609 }
16610 else
16611 incr = RECUR (incr);
16612 break;
16613 case MODOP_EXPR:
16614 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
16615 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
16616 {
16617 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16618 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16619 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
16620 TREE_TYPE (decl), lhs,
16621 RECUR (TREE_OPERAND (incr, 2))));
16622 }
16623 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
16624 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
16625 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
16626 {
16627 tree rhs = TREE_OPERAND (incr, 2);
16628 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16629 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
16630 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
16631 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16632 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
16633 rhs0, rhs1));
16634 }
16635 else
16636 incr = RECUR (incr);
16637 break;
16638 default:
16639 incr = RECUR (incr);
16640 break;
16641 }
16642
16643 TREE_VEC_ELT (declv, i) = decl;
16644 TREE_VEC_ELT (initv, i) = init;
16645 TREE_VEC_ELT (condv, i) = cond;
16646 TREE_VEC_ELT (incrv, i) = incr;
16647 #undef RECUR
16648 }
16649
16650 /* Helper function of tsubst_expr, find OMP_TEAMS inside
16651 of OMP_TARGET's body. */
16652
16653 static tree
16654 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
16655 {
16656 *walk_subtrees = 0;
16657 switch (TREE_CODE (*tp))
16658 {
16659 case OMP_TEAMS:
16660 return *tp;
16661 case BIND_EXPR:
16662 case STATEMENT_LIST:
16663 *walk_subtrees = 1;
16664 break;
16665 default:
16666 break;
16667 }
16668 return NULL_TREE;
16669 }
16670
16671 /* Helper function for tsubst_expr. For decomposition declaration
16672 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
16673 also the corresponding decls representing the identifiers
16674 of the decomposition declaration. Return DECL if successful
16675 or error_mark_node otherwise, set *FIRST to the first decl
16676 in the list chained through DECL_CHAIN and *CNT to the number
16677 of such decls. */
16678
16679 static tree
16680 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
16681 tsubst_flags_t complain, tree in_decl, tree *first,
16682 unsigned int *cnt)
16683 {
16684 tree decl2, decl3, prev = decl;
16685 *cnt = 0;
16686 gcc_assert (DECL_NAME (decl) == NULL_TREE);
16687 for (decl2 = DECL_CHAIN (pattern_decl);
16688 decl2
16689 && VAR_P (decl2)
16690 && DECL_DECOMPOSITION_P (decl2)
16691 && DECL_NAME (decl2);
16692 decl2 = DECL_CHAIN (decl2))
16693 {
16694 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
16695 {
16696 gcc_assert (errorcount);
16697 return error_mark_node;
16698 }
16699 (*cnt)++;
16700 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
16701 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
16702 tree v = DECL_VALUE_EXPR (decl2);
16703 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
16704 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
16705 decl3 = tsubst (decl2, args, complain, in_decl);
16706 SET_DECL_VALUE_EXPR (decl2, v);
16707 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
16708 if (VAR_P (decl3))
16709 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
16710 else
16711 {
16712 gcc_assert (errorcount);
16713 decl = error_mark_node;
16714 continue;
16715 }
16716 maybe_push_decl (decl3);
16717 if (error_operand_p (decl3))
16718 decl = error_mark_node;
16719 else if (decl != error_mark_node
16720 && DECL_CHAIN (decl3) != prev
16721 && decl != prev)
16722 {
16723 gcc_assert (errorcount);
16724 decl = error_mark_node;
16725 }
16726 else
16727 prev = decl3;
16728 }
16729 *first = prev;
16730 return decl;
16731 }
16732
16733 /* Like tsubst_copy for expressions, etc. but also does semantic
16734 processing. */
16735
16736 tree
16737 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
16738 bool integral_constant_expression_p)
16739 {
16740 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
16741 #define RECUR(NODE) \
16742 tsubst_expr ((NODE), args, complain, in_decl, \
16743 integral_constant_expression_p)
16744
16745 tree stmt, tmp;
16746 tree r;
16747 location_t loc;
16748
16749 if (t == NULL_TREE || t == error_mark_node)
16750 return t;
16751
16752 loc = input_location;
16753 if (location_t eloc = cp_expr_location (t))
16754 input_location = eloc;
16755 if (STATEMENT_CODE_P (TREE_CODE (t)))
16756 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
16757
16758 switch (TREE_CODE (t))
16759 {
16760 case STATEMENT_LIST:
16761 {
16762 tree_stmt_iterator i;
16763 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
16764 RECUR (tsi_stmt (i));
16765 break;
16766 }
16767
16768 case CTOR_INITIALIZER:
16769 finish_mem_initializers (tsubst_initializer_list
16770 (TREE_OPERAND (t, 0), args));
16771 break;
16772
16773 case RETURN_EXPR:
16774 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
16775 break;
16776
16777 case EXPR_STMT:
16778 tmp = RECUR (EXPR_STMT_EXPR (t));
16779 if (EXPR_STMT_STMT_EXPR_RESULT (t))
16780 finish_stmt_expr_expr (tmp, cur_stmt_expr);
16781 else
16782 finish_expr_stmt (tmp);
16783 break;
16784
16785 case USING_STMT:
16786 finish_local_using_directive (USING_STMT_NAMESPACE (t),
16787 /*attribs=*/NULL_TREE);
16788 break;
16789
16790 case DECL_EXPR:
16791 {
16792 tree decl, pattern_decl;
16793 tree init;
16794
16795 pattern_decl = decl = DECL_EXPR_DECL (t);
16796 if (TREE_CODE (decl) == LABEL_DECL)
16797 finish_label_decl (DECL_NAME (decl));
16798 else if (TREE_CODE (decl) == USING_DECL)
16799 {
16800 tree scope = USING_DECL_SCOPE (decl);
16801 tree name = DECL_NAME (decl);
16802
16803 scope = tsubst (scope, args, complain, in_decl);
16804 decl = lookup_qualified_name (scope, name,
16805 /*is_type_p=*/false,
16806 /*complain=*/false);
16807 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
16808 qualified_name_lookup_error (scope, name, decl, input_location);
16809 else
16810 finish_local_using_decl (decl, scope, name);
16811 }
16812 else if (is_capture_proxy (decl)
16813 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
16814 {
16815 /* We're in tsubst_lambda_expr, we've already inserted a new
16816 capture proxy, so look it up and register it. */
16817 tree inst;
16818 if (DECL_PACK_P (decl))
16819 {
16820 inst = (retrieve_local_specialization
16821 (DECL_CAPTURED_VARIABLE (decl)));
16822 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK);
16823 }
16824 else
16825 {
16826 inst = lookup_name_real (DECL_NAME (decl), 0, 0,
16827 /*block_p=*/true, 0, LOOKUP_HIDDEN);
16828 gcc_assert (inst != decl && is_capture_proxy (inst));
16829 }
16830 register_local_specialization (inst, decl);
16831 break;
16832 }
16833 else if (DECL_PRETTY_FUNCTION_P (decl))
16834 decl = make_fname_decl (DECL_SOURCE_LOCATION (decl),
16835 DECL_NAME (decl),
16836 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
16837 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
16838 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
16839 /* Don't copy the old closure; we'll create a new one in
16840 tsubst_lambda_expr. */
16841 break;
16842 else
16843 {
16844 init = DECL_INITIAL (decl);
16845 decl = tsubst (decl, args, complain, in_decl);
16846 if (decl != error_mark_node)
16847 {
16848 /* By marking the declaration as instantiated, we avoid
16849 trying to instantiate it. Since instantiate_decl can't
16850 handle local variables, and since we've already done
16851 all that needs to be done, that's the right thing to
16852 do. */
16853 if (VAR_P (decl))
16854 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16855 if (VAR_P (decl) && !DECL_NAME (decl)
16856 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
16857 /* Anonymous aggregates are a special case. */
16858 finish_anon_union (decl);
16859 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
16860 {
16861 DECL_CONTEXT (decl) = current_function_decl;
16862 if (DECL_NAME (decl) == this_identifier)
16863 {
16864 tree lam = DECL_CONTEXT (current_function_decl);
16865 lam = CLASSTYPE_LAMBDA_EXPR (lam);
16866 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
16867 }
16868 insert_capture_proxy (decl);
16869 }
16870 else if (DECL_IMPLICIT_TYPEDEF_P (t))
16871 /* We already did a pushtag. */;
16872 else if (TREE_CODE (decl) == FUNCTION_DECL
16873 && DECL_OMP_DECLARE_REDUCTION_P (decl)
16874 && DECL_FUNCTION_SCOPE_P (pattern_decl))
16875 {
16876 DECL_CONTEXT (decl) = NULL_TREE;
16877 pushdecl (decl);
16878 DECL_CONTEXT (decl) = current_function_decl;
16879 cp_check_omp_declare_reduction (decl);
16880 }
16881 else
16882 {
16883 int const_init = false;
16884 unsigned int cnt = 0;
16885 tree first = NULL_TREE, ndecl = error_mark_node;
16886 maybe_push_decl (decl);
16887
16888 if (VAR_P (decl)
16889 && DECL_DECOMPOSITION_P (decl)
16890 && TREE_TYPE (pattern_decl) != error_mark_node)
16891 ndecl = tsubst_decomp_names (decl, pattern_decl, args,
16892 complain, in_decl, &first,
16893 &cnt);
16894
16895 init = tsubst_init (init, decl, args, complain, in_decl);
16896
16897 if (VAR_P (decl))
16898 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
16899 (pattern_decl));
16900
16901 if (ndecl != error_mark_node)
16902 cp_maybe_mangle_decomp (ndecl, first, cnt);
16903
16904 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
16905
16906 if (ndecl != error_mark_node)
16907 cp_finish_decomp (ndecl, first, cnt);
16908 }
16909 }
16910 }
16911
16912 break;
16913 }
16914
16915 case FOR_STMT:
16916 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
16917 RECUR (FOR_INIT_STMT (t));
16918 finish_init_stmt (stmt);
16919 tmp = RECUR (FOR_COND (t));
16920 finish_for_cond (tmp, stmt, false, 0);
16921 tmp = RECUR (FOR_EXPR (t));
16922 finish_for_expr (tmp, stmt);
16923 {
16924 bool prev = note_iteration_stmt_body_start ();
16925 RECUR (FOR_BODY (t));
16926 note_iteration_stmt_body_end (prev);
16927 }
16928 finish_for_stmt (stmt);
16929 break;
16930
16931 case RANGE_FOR_STMT:
16932 {
16933 /* Construct another range_for, if this is not a final
16934 substitution (for inside inside a generic lambda of a
16935 template). Otherwise convert to a regular for. */
16936 tree decl, expr;
16937 stmt = (processing_template_decl
16938 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
16939 : begin_for_stmt (NULL_TREE, NULL_TREE));
16940 RECUR (RANGE_FOR_INIT_STMT (t));
16941 decl = RANGE_FOR_DECL (t);
16942 decl = tsubst (decl, args, complain, in_decl);
16943 maybe_push_decl (decl);
16944 expr = RECUR (RANGE_FOR_EXPR (t));
16945
16946 tree decomp_first = NULL_TREE;
16947 unsigned decomp_cnt = 0;
16948 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
16949 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
16950 complain, in_decl,
16951 &decomp_first, &decomp_cnt);
16952
16953 if (processing_template_decl)
16954 {
16955 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
16956 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
16957 finish_range_for_decl (stmt, decl, expr);
16958 if (decomp_first && decl != error_mark_node)
16959 cp_finish_decomp (decl, decomp_first, decomp_cnt);
16960 }
16961 else
16962 {
16963 unsigned short unroll = (RANGE_FOR_UNROLL (t)
16964 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
16965 stmt = cp_convert_range_for (stmt, decl, expr,
16966 decomp_first, decomp_cnt,
16967 RANGE_FOR_IVDEP (t), unroll);
16968 }
16969
16970 bool prev = note_iteration_stmt_body_start ();
16971 RECUR (RANGE_FOR_BODY (t));
16972 note_iteration_stmt_body_end (prev);
16973 finish_for_stmt (stmt);
16974 }
16975 break;
16976
16977 case WHILE_STMT:
16978 stmt = begin_while_stmt ();
16979 tmp = RECUR (WHILE_COND (t));
16980 finish_while_stmt_cond (tmp, stmt, false, 0);
16981 {
16982 bool prev = note_iteration_stmt_body_start ();
16983 RECUR (WHILE_BODY (t));
16984 note_iteration_stmt_body_end (prev);
16985 }
16986 finish_while_stmt (stmt);
16987 break;
16988
16989 case DO_STMT:
16990 stmt = begin_do_stmt ();
16991 {
16992 bool prev = note_iteration_stmt_body_start ();
16993 RECUR (DO_BODY (t));
16994 note_iteration_stmt_body_end (prev);
16995 }
16996 finish_do_body (stmt);
16997 tmp = RECUR (DO_COND (t));
16998 finish_do_stmt (tmp, stmt, false, 0);
16999 break;
17000
17001 case IF_STMT:
17002 stmt = begin_if_stmt ();
17003 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
17004 if (IF_STMT_CONSTEXPR_P (t))
17005 args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args);
17006 tmp = RECUR (IF_COND (t));
17007 tmp = finish_if_stmt_cond (tmp, stmt);
17008 if (IF_STMT_CONSTEXPR_P (t)
17009 && instantiation_dependent_expression_p (tmp))
17010 {
17011 /* We're partially instantiating a generic lambda, but the condition
17012 of the constexpr if is still dependent. Don't substitute into the
17013 branches now, just remember the template arguments. */
17014 do_poplevel (IF_SCOPE (stmt));
17015 IF_COND (stmt) = IF_COND (t);
17016 THEN_CLAUSE (stmt) = THEN_CLAUSE (t);
17017 ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t);
17018 IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (t, args, complain);
17019 add_stmt (stmt);
17020 break;
17021 }
17022 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
17023 /* Don't instantiate the THEN_CLAUSE. */;
17024 else
17025 {
17026 tree folded = fold_non_dependent_expr (tmp, complain);
17027 bool inhibit = integer_zerop (folded);
17028 if (inhibit)
17029 ++c_inhibit_evaluation_warnings;
17030 RECUR (THEN_CLAUSE (t));
17031 if (inhibit)
17032 --c_inhibit_evaluation_warnings;
17033 }
17034 finish_then_clause (stmt);
17035
17036 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
17037 /* Don't instantiate the ELSE_CLAUSE. */;
17038 else if (ELSE_CLAUSE (t))
17039 {
17040 tree folded = fold_non_dependent_expr (tmp, complain);
17041 bool inhibit = integer_nonzerop (folded);
17042 begin_else_clause (stmt);
17043 if (inhibit)
17044 ++c_inhibit_evaluation_warnings;
17045 RECUR (ELSE_CLAUSE (t));
17046 if (inhibit)
17047 --c_inhibit_evaluation_warnings;
17048 finish_else_clause (stmt);
17049 }
17050
17051 finish_if_stmt (stmt);
17052 break;
17053
17054 case BIND_EXPR:
17055 if (BIND_EXPR_BODY_BLOCK (t))
17056 stmt = begin_function_body ();
17057 else
17058 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
17059 ? BCS_TRY_BLOCK : 0);
17060
17061 RECUR (BIND_EXPR_BODY (t));
17062
17063 if (BIND_EXPR_BODY_BLOCK (t))
17064 finish_function_body (stmt);
17065 else
17066 finish_compound_stmt (stmt);
17067 break;
17068
17069 case BREAK_STMT:
17070 finish_break_stmt ();
17071 break;
17072
17073 case CONTINUE_STMT:
17074 finish_continue_stmt ();
17075 break;
17076
17077 case SWITCH_STMT:
17078 stmt = begin_switch_stmt ();
17079 tmp = RECUR (SWITCH_STMT_COND (t));
17080 finish_switch_cond (tmp, stmt);
17081 RECUR (SWITCH_STMT_BODY (t));
17082 finish_switch_stmt (stmt);
17083 break;
17084
17085 case CASE_LABEL_EXPR:
17086 {
17087 tree low = RECUR (CASE_LOW (t));
17088 tree high = RECUR (CASE_HIGH (t));
17089 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
17090 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
17091 FALLTHROUGH_LABEL_P (CASE_LABEL (l))
17092 = FALLTHROUGH_LABEL_P (CASE_LABEL (t));
17093 }
17094 break;
17095
17096 case LABEL_EXPR:
17097 {
17098 tree decl = LABEL_EXPR_LABEL (t);
17099 tree label;
17100
17101 label = finish_label_stmt (DECL_NAME (decl));
17102 if (TREE_CODE (label) == LABEL_DECL)
17103 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
17104 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
17105 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
17106 }
17107 break;
17108
17109 case GOTO_EXPR:
17110 tmp = GOTO_DESTINATION (t);
17111 if (TREE_CODE (tmp) != LABEL_DECL)
17112 /* Computed goto's must be tsubst'd into. On the other hand,
17113 non-computed gotos must not be; the identifier in question
17114 will have no binding. */
17115 tmp = RECUR (tmp);
17116 else
17117 tmp = DECL_NAME (tmp);
17118 finish_goto_stmt (tmp);
17119 break;
17120
17121 case ASM_EXPR:
17122 {
17123 tree string = RECUR (ASM_STRING (t));
17124 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
17125 complain, in_decl);
17126 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
17127 complain, in_decl);
17128 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
17129 complain, in_decl);
17130 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
17131 complain, in_decl);
17132 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
17133 clobbers, labels);
17134 tree asm_expr = tmp;
17135 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
17136 asm_expr = TREE_OPERAND (asm_expr, 0);
17137 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
17138 }
17139 break;
17140
17141 case TRY_BLOCK:
17142 if (CLEANUP_P (t))
17143 {
17144 stmt = begin_try_block ();
17145 RECUR (TRY_STMTS (t));
17146 finish_cleanup_try_block (stmt);
17147 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
17148 }
17149 else
17150 {
17151 tree compound_stmt = NULL_TREE;
17152
17153 if (FN_TRY_BLOCK_P (t))
17154 stmt = begin_function_try_block (&compound_stmt);
17155 else
17156 stmt = begin_try_block ();
17157
17158 RECUR (TRY_STMTS (t));
17159
17160 if (FN_TRY_BLOCK_P (t))
17161 finish_function_try_block (stmt);
17162 else
17163 finish_try_block (stmt);
17164
17165 RECUR (TRY_HANDLERS (t));
17166 if (FN_TRY_BLOCK_P (t))
17167 finish_function_handler_sequence (stmt, compound_stmt);
17168 else
17169 finish_handler_sequence (stmt);
17170 }
17171 break;
17172
17173 case HANDLER:
17174 {
17175 tree decl = HANDLER_PARMS (t);
17176
17177 if (decl)
17178 {
17179 decl = tsubst (decl, args, complain, in_decl);
17180 /* Prevent instantiate_decl from trying to instantiate
17181 this variable. We've already done all that needs to be
17182 done. */
17183 if (decl != error_mark_node)
17184 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
17185 }
17186 stmt = begin_handler ();
17187 finish_handler_parms (decl, stmt);
17188 RECUR (HANDLER_BODY (t));
17189 finish_handler (stmt);
17190 }
17191 break;
17192
17193 case TAG_DEFN:
17194 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
17195 if (CLASS_TYPE_P (tmp))
17196 {
17197 /* Local classes are not independent templates; they are
17198 instantiated along with their containing function. And this
17199 way we don't have to deal with pushing out of one local class
17200 to instantiate a member of another local class. */
17201 /* Closures are handled by the LAMBDA_EXPR. */
17202 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
17203 complete_type (tmp);
17204 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
17205 if ((VAR_P (fld)
17206 || (TREE_CODE (fld) == FUNCTION_DECL
17207 && !DECL_ARTIFICIAL (fld)))
17208 && DECL_TEMPLATE_INSTANTIATION (fld))
17209 instantiate_decl (fld, /*defer_ok=*/false,
17210 /*expl_inst_class=*/false);
17211 }
17212 break;
17213
17214 case STATIC_ASSERT:
17215 {
17216 tree condition;
17217
17218 ++c_inhibit_evaluation_warnings;
17219 condition =
17220 tsubst_expr (STATIC_ASSERT_CONDITION (t),
17221 args,
17222 complain, in_decl,
17223 /*integral_constant_expression_p=*/true);
17224 --c_inhibit_evaluation_warnings;
17225
17226 finish_static_assert (condition,
17227 STATIC_ASSERT_MESSAGE (t),
17228 STATIC_ASSERT_SOURCE_LOCATION (t),
17229 /*member_p=*/false);
17230 }
17231 break;
17232
17233 case OACC_KERNELS:
17234 case OACC_PARALLEL:
17235 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
17236 in_decl);
17237 stmt = begin_omp_parallel ();
17238 RECUR (OMP_BODY (t));
17239 finish_omp_construct (TREE_CODE (t), stmt, tmp);
17240 break;
17241
17242 case OMP_PARALLEL:
17243 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
17244 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
17245 complain, in_decl);
17246 if (OMP_PARALLEL_COMBINED (t))
17247 omp_parallel_combined_clauses = &tmp;
17248 stmt = begin_omp_parallel ();
17249 RECUR (OMP_PARALLEL_BODY (t));
17250 gcc_assert (omp_parallel_combined_clauses == NULL);
17251 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
17252 = OMP_PARALLEL_COMBINED (t);
17253 pop_omp_privatization_clauses (r);
17254 break;
17255
17256 case OMP_TASK:
17257 r = push_omp_privatization_clauses (false);
17258 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
17259 complain, in_decl);
17260 stmt = begin_omp_task ();
17261 RECUR (OMP_TASK_BODY (t));
17262 finish_omp_task (tmp, stmt);
17263 pop_omp_privatization_clauses (r);
17264 break;
17265
17266 case OMP_FOR:
17267 case OMP_SIMD:
17268 case OMP_DISTRIBUTE:
17269 case OMP_TASKLOOP:
17270 case OACC_LOOP:
17271 {
17272 tree clauses, body, pre_body;
17273 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
17274 tree orig_declv = NULL_TREE;
17275 tree incrv = NULL_TREE;
17276 enum c_omp_region_type ort = C_ORT_OMP;
17277 int i;
17278
17279 if (TREE_CODE (t) == OACC_LOOP)
17280 ort = C_ORT_ACC;
17281
17282 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
17283 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
17284 in_decl);
17285 if (OMP_FOR_INIT (t) != NULL_TREE)
17286 {
17287 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17288 if (OMP_FOR_ORIG_DECLS (t))
17289 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17290 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17291 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17292 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17293 }
17294
17295 stmt = begin_omp_structured_block ();
17296
17297 pre_body = push_stmt_list ();
17298 RECUR (OMP_FOR_PRE_BODY (t));
17299 pre_body = pop_stmt_list (pre_body);
17300
17301 if (OMP_FOR_INIT (t) != NULL_TREE)
17302 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
17303 tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, condv,
17304 incrv, &clauses, args, complain, in_decl,
17305 integral_constant_expression_p);
17306 omp_parallel_combined_clauses = NULL;
17307
17308 body = push_stmt_list ();
17309 RECUR (OMP_FOR_BODY (t));
17310 body = pop_stmt_list (body);
17311
17312 if (OMP_FOR_INIT (t) != NULL_TREE)
17313 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
17314 orig_declv, initv, condv, incrv, body, pre_body,
17315 NULL, clauses);
17316 else
17317 {
17318 t = make_node (TREE_CODE (t));
17319 TREE_TYPE (t) = void_type_node;
17320 OMP_FOR_BODY (t) = body;
17321 OMP_FOR_PRE_BODY (t) = pre_body;
17322 OMP_FOR_CLAUSES (t) = clauses;
17323 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
17324 add_stmt (t);
17325 }
17326
17327 add_stmt (finish_omp_structured_block (stmt));
17328 pop_omp_privatization_clauses (r);
17329 }
17330 break;
17331
17332 case OMP_SECTIONS:
17333 omp_parallel_combined_clauses = NULL;
17334 /* FALLTHRU */
17335 case OMP_SINGLE:
17336 case OMP_TEAMS:
17337 case OMP_CRITICAL:
17338 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
17339 && OMP_TEAMS_COMBINED (t));
17340 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
17341 in_decl);
17342 stmt = push_stmt_list ();
17343 RECUR (OMP_BODY (t));
17344 stmt = pop_stmt_list (stmt);
17345
17346 t = copy_node (t);
17347 OMP_BODY (t) = stmt;
17348 OMP_CLAUSES (t) = tmp;
17349 add_stmt (t);
17350 pop_omp_privatization_clauses (r);
17351 break;
17352
17353 case OACC_DATA:
17354 case OMP_TARGET_DATA:
17355 case OMP_TARGET:
17356 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
17357 ? C_ORT_ACC : C_ORT_OMP, args, complain,
17358 in_decl);
17359 keep_next_level (true);
17360 stmt = begin_omp_structured_block ();
17361
17362 RECUR (OMP_BODY (t));
17363 stmt = finish_omp_structured_block (stmt);
17364
17365 t = copy_node (t);
17366 OMP_BODY (t) = stmt;
17367 OMP_CLAUSES (t) = tmp;
17368 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
17369 {
17370 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
17371 if (teams)
17372 {
17373 /* For combined target teams, ensure the num_teams and
17374 thread_limit clause expressions are evaluated on the host,
17375 before entering the target construct. */
17376 tree c;
17377 for (c = OMP_TEAMS_CLAUSES (teams);
17378 c; c = OMP_CLAUSE_CHAIN (c))
17379 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
17380 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
17381 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
17382 {
17383 tree expr = OMP_CLAUSE_OPERAND (c, 0);
17384 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
17385 if (expr == error_mark_node)
17386 continue;
17387 tmp = TARGET_EXPR_SLOT (expr);
17388 add_stmt (expr);
17389 OMP_CLAUSE_OPERAND (c, 0) = expr;
17390 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
17391 OMP_CLAUSE_FIRSTPRIVATE);
17392 OMP_CLAUSE_DECL (tc) = tmp;
17393 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
17394 OMP_TARGET_CLAUSES (t) = tc;
17395 }
17396 }
17397 }
17398 add_stmt (t);
17399 break;
17400
17401 case OACC_DECLARE:
17402 t = copy_node (t);
17403 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
17404 complain, in_decl);
17405 OACC_DECLARE_CLAUSES (t) = tmp;
17406 add_stmt (t);
17407 break;
17408
17409 case OMP_TARGET_UPDATE:
17410 case OMP_TARGET_ENTER_DATA:
17411 case OMP_TARGET_EXIT_DATA:
17412 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
17413 complain, in_decl);
17414 t = copy_node (t);
17415 OMP_STANDALONE_CLAUSES (t) = tmp;
17416 add_stmt (t);
17417 break;
17418
17419 case OACC_ENTER_DATA:
17420 case OACC_EXIT_DATA:
17421 case OACC_UPDATE:
17422 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
17423 complain, in_decl);
17424 t = copy_node (t);
17425 OMP_STANDALONE_CLAUSES (t) = tmp;
17426 add_stmt (t);
17427 break;
17428
17429 case OMP_ORDERED:
17430 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
17431 complain, in_decl);
17432 stmt = push_stmt_list ();
17433 RECUR (OMP_BODY (t));
17434 stmt = pop_stmt_list (stmt);
17435
17436 t = copy_node (t);
17437 OMP_BODY (t) = stmt;
17438 OMP_ORDERED_CLAUSES (t) = tmp;
17439 add_stmt (t);
17440 break;
17441
17442 case OMP_SECTION:
17443 case OMP_MASTER:
17444 case OMP_TASKGROUP:
17445 stmt = push_stmt_list ();
17446 RECUR (OMP_BODY (t));
17447 stmt = pop_stmt_list (stmt);
17448
17449 t = copy_node (t);
17450 OMP_BODY (t) = stmt;
17451 add_stmt (t);
17452 break;
17453
17454 case OMP_ATOMIC:
17455 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
17456 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
17457 {
17458 tree op1 = TREE_OPERAND (t, 1);
17459 tree rhs1 = NULL_TREE;
17460 tree lhs, rhs;
17461 if (TREE_CODE (op1) == COMPOUND_EXPR)
17462 {
17463 rhs1 = RECUR (TREE_OPERAND (op1, 0));
17464 op1 = TREE_OPERAND (op1, 1);
17465 }
17466 lhs = RECUR (TREE_OPERAND (op1, 0));
17467 rhs = RECUR (TREE_OPERAND (op1, 1));
17468 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
17469 NULL_TREE, NULL_TREE, rhs1,
17470 OMP_ATOMIC_SEQ_CST (t));
17471 }
17472 else
17473 {
17474 tree op1 = TREE_OPERAND (t, 1);
17475 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
17476 tree rhs1 = NULL_TREE;
17477 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
17478 enum tree_code opcode = NOP_EXPR;
17479 if (code == OMP_ATOMIC_READ)
17480 {
17481 v = RECUR (TREE_OPERAND (op1, 0));
17482 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
17483 }
17484 else if (code == OMP_ATOMIC_CAPTURE_OLD
17485 || code == OMP_ATOMIC_CAPTURE_NEW)
17486 {
17487 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
17488 v = RECUR (TREE_OPERAND (op1, 0));
17489 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
17490 if (TREE_CODE (op11) == COMPOUND_EXPR)
17491 {
17492 rhs1 = RECUR (TREE_OPERAND (op11, 0));
17493 op11 = TREE_OPERAND (op11, 1);
17494 }
17495 lhs = RECUR (TREE_OPERAND (op11, 0));
17496 rhs = RECUR (TREE_OPERAND (op11, 1));
17497 opcode = TREE_CODE (op11);
17498 if (opcode == MODIFY_EXPR)
17499 opcode = NOP_EXPR;
17500 }
17501 else
17502 {
17503 code = OMP_ATOMIC;
17504 lhs = RECUR (TREE_OPERAND (op1, 0));
17505 rhs = RECUR (TREE_OPERAND (op1, 1));
17506 }
17507 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
17508 OMP_ATOMIC_SEQ_CST (t));
17509 }
17510 break;
17511
17512 case TRANSACTION_EXPR:
17513 {
17514 int flags = 0;
17515 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
17516 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
17517
17518 if (TRANSACTION_EXPR_IS_STMT (t))
17519 {
17520 tree body = TRANSACTION_EXPR_BODY (t);
17521 tree noex = NULL_TREE;
17522 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
17523 {
17524 noex = MUST_NOT_THROW_COND (body);
17525 if (noex == NULL_TREE)
17526 noex = boolean_true_node;
17527 body = TREE_OPERAND (body, 0);
17528 }
17529 stmt = begin_transaction_stmt (input_location, NULL, flags);
17530 RECUR (body);
17531 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
17532 }
17533 else
17534 {
17535 stmt = build_transaction_expr (EXPR_LOCATION (t),
17536 RECUR (TRANSACTION_EXPR_BODY (t)),
17537 flags, NULL_TREE);
17538 RETURN (stmt);
17539 }
17540 }
17541 break;
17542
17543 case MUST_NOT_THROW_EXPR:
17544 {
17545 tree op0 = RECUR (TREE_OPERAND (t, 0));
17546 tree cond = RECUR (MUST_NOT_THROW_COND (t));
17547 RETURN (build_must_not_throw_expr (op0, cond));
17548 }
17549
17550 case EXPR_PACK_EXPANSION:
17551 error ("invalid use of pack expansion expression");
17552 RETURN (error_mark_node);
17553
17554 case NONTYPE_ARGUMENT_PACK:
17555 error ("use %<...%> to expand argument pack");
17556 RETURN (error_mark_node);
17557
17558 case COMPOUND_EXPR:
17559 tmp = RECUR (TREE_OPERAND (t, 0));
17560 if (tmp == NULL_TREE)
17561 /* If the first operand was a statement, we're done with it. */
17562 RETURN (RECUR (TREE_OPERAND (t, 1)));
17563 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
17564 RECUR (TREE_OPERAND (t, 1)),
17565 complain));
17566
17567 case ANNOTATE_EXPR:
17568 tmp = RECUR (TREE_OPERAND (t, 0));
17569 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
17570 TREE_TYPE (tmp), tmp,
17571 RECUR (TREE_OPERAND (t, 1)),
17572 RECUR (TREE_OPERAND (t, 2))));
17573
17574 default:
17575 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
17576
17577 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
17578 /*function_p=*/false,
17579 integral_constant_expression_p));
17580 }
17581
17582 RETURN (NULL_TREE);
17583 out:
17584 input_location = loc;
17585 return r;
17586 #undef RECUR
17587 #undef RETURN
17588 }
17589
17590 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
17591 function. For description of the body see comment above
17592 cp_parser_omp_declare_reduction_exprs. */
17593
17594 static void
17595 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
17596 {
17597 if (t == NULL_TREE || t == error_mark_node)
17598 return;
17599
17600 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
17601
17602 tree_stmt_iterator tsi;
17603 int i;
17604 tree stmts[7];
17605 memset (stmts, 0, sizeof stmts);
17606 for (i = 0, tsi = tsi_start (t);
17607 i < 7 && !tsi_end_p (tsi);
17608 i++, tsi_next (&tsi))
17609 stmts[i] = tsi_stmt (tsi);
17610 gcc_assert (tsi_end_p (tsi));
17611
17612 if (i >= 3)
17613 {
17614 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
17615 && TREE_CODE (stmts[1]) == DECL_EXPR);
17616 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
17617 args, complain, in_decl);
17618 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
17619 args, complain, in_decl);
17620 DECL_CONTEXT (omp_out) = current_function_decl;
17621 DECL_CONTEXT (omp_in) = current_function_decl;
17622 keep_next_level (true);
17623 tree block = begin_omp_structured_block ();
17624 tsubst_expr (stmts[2], args, complain, in_decl, false);
17625 block = finish_omp_structured_block (block);
17626 block = maybe_cleanup_point_expr_void (block);
17627 add_decl_expr (omp_out);
17628 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
17629 TREE_NO_WARNING (omp_out) = 1;
17630 add_decl_expr (omp_in);
17631 finish_expr_stmt (block);
17632 }
17633 if (i >= 6)
17634 {
17635 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
17636 && TREE_CODE (stmts[4]) == DECL_EXPR);
17637 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
17638 args, complain, in_decl);
17639 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
17640 args, complain, in_decl);
17641 DECL_CONTEXT (omp_priv) = current_function_decl;
17642 DECL_CONTEXT (omp_orig) = current_function_decl;
17643 keep_next_level (true);
17644 tree block = begin_omp_structured_block ();
17645 tsubst_expr (stmts[5], args, complain, in_decl, false);
17646 block = finish_omp_structured_block (block);
17647 block = maybe_cleanup_point_expr_void (block);
17648 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
17649 add_decl_expr (omp_priv);
17650 add_decl_expr (omp_orig);
17651 finish_expr_stmt (block);
17652 if (i == 7)
17653 add_decl_expr (omp_orig);
17654 }
17655 }
17656
17657 /* T is a postfix-expression that is not being used in a function
17658 call. Return the substituted version of T. */
17659
17660 static tree
17661 tsubst_non_call_postfix_expression (tree t, tree args,
17662 tsubst_flags_t complain,
17663 tree in_decl)
17664 {
17665 if (TREE_CODE (t) == SCOPE_REF)
17666 t = tsubst_qualified_id (t, args, complain, in_decl,
17667 /*done=*/false, /*address_p=*/false);
17668 else
17669 t = tsubst_copy_and_build (t, args, complain, in_decl,
17670 /*function_p=*/false,
17671 /*integral_constant_expression_p=*/false);
17672
17673 return t;
17674 }
17675
17676 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
17677 instantiation context. Instantiating a pack expansion containing a lambda
17678 might result in multiple lambdas all based on the same lambda in the
17679 template. */
17680
17681 tree
17682 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
17683 {
17684 tree oldfn = lambda_function (t);
17685 in_decl = oldfn;
17686
17687 tree r = build_lambda_expr ();
17688
17689 LAMBDA_EXPR_LOCATION (r)
17690 = LAMBDA_EXPR_LOCATION (t);
17691 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
17692 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
17693 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
17694
17695 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
17696 /* A lambda in a default argument outside a class gets no
17697 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
17698 tsubst_default_argument calls start_lambda_scope, so we need to
17699 specifically ignore it here, and use the global scope. */
17700 record_null_lambda_scope (r);
17701 else
17702 record_lambda_scope (r);
17703
17704 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
17705 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
17706
17707 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
17708 cap = TREE_CHAIN (cap))
17709 {
17710 tree field = TREE_PURPOSE (cap);
17711 if (PACK_EXPANSION_P (field))
17712 field = PACK_EXPANSION_PATTERN (field);
17713 field = tsubst_decl (field, args, complain);
17714
17715 if (field == error_mark_node)
17716 return error_mark_node;
17717
17718 tree init = TREE_VALUE (cap);
17719 if (PACK_EXPANSION_P (init))
17720 init = tsubst_pack_expansion (init, args, complain, in_decl);
17721 else
17722 init = tsubst_copy_and_build (init, args, complain, in_decl,
17723 /*fn*/false, /*constexpr*/false);
17724
17725 if (TREE_CODE (field) == TREE_VEC)
17726 {
17727 int len = TREE_VEC_LENGTH (field);
17728 gcc_assert (TREE_CODE (init) == TREE_VEC
17729 && TREE_VEC_LENGTH (init) == len);
17730 for (int i = 0; i < len; ++i)
17731 LAMBDA_EXPR_CAPTURE_LIST (r)
17732 = tree_cons (TREE_VEC_ELT (field, i),
17733 TREE_VEC_ELT (init, i),
17734 LAMBDA_EXPR_CAPTURE_LIST (r));
17735 }
17736 else
17737 {
17738 LAMBDA_EXPR_CAPTURE_LIST (r)
17739 = tree_cons (field, init, LAMBDA_EXPR_CAPTURE_LIST (r));
17740
17741 if (id_equal (DECL_NAME (field), "__this"))
17742 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
17743 }
17744 }
17745
17746 tree type = begin_lambda_type (r);
17747 if (type == error_mark_node)
17748 return error_mark_node;
17749
17750 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
17751 determine_visibility (TYPE_NAME (type));
17752
17753 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
17754
17755 tree oldtmpl = (generic_lambda_fn_p (oldfn)
17756 ? DECL_TI_TEMPLATE (oldfn)
17757 : NULL_TREE);
17758
17759 tree fntype = static_fn_type (oldfn);
17760 if (oldtmpl)
17761 ++processing_template_decl;
17762 fntype = tsubst (fntype, args, complain, in_decl);
17763 if (oldtmpl)
17764 --processing_template_decl;
17765
17766 if (fntype == error_mark_node)
17767 r = error_mark_node;
17768 else
17769 {
17770 /* Fix the type of 'this'. */
17771 fntype = build_memfn_type (fntype, type,
17772 type_memfn_quals (fntype),
17773 type_memfn_rqual (fntype));
17774 tree fn, tmpl;
17775 if (oldtmpl)
17776 {
17777 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
17778 fn = DECL_TEMPLATE_RESULT (tmpl);
17779 finish_member_declaration (tmpl);
17780 }
17781 else
17782 {
17783 tmpl = NULL_TREE;
17784 fn = tsubst_function_decl (oldfn, args, complain, fntype);
17785 finish_member_declaration (fn);
17786 }
17787
17788 /* Let finish_function set this. */
17789 DECL_DECLARED_CONSTEXPR_P (fn) = false;
17790
17791 bool nested = cfun;
17792 if (nested)
17793 push_function_context ();
17794 else
17795 /* Still increment function_depth so that we don't GC in the
17796 middle of an expression. */
17797 ++function_depth;
17798
17799 local_specialization_stack s (lss_copy);
17800
17801 tree body = start_lambda_function (fn, r);
17802
17803 register_parameter_specializations (oldfn, fn);
17804
17805 if (oldtmpl)
17806 {
17807 /* We might not partially instantiate some parts of the function, so
17808 copy these flags from the original template. */
17809 language_function *ol = DECL_STRUCT_FUNCTION (oldfn)->language;
17810 current_function_returns_value = ol->returns_value;
17811 current_function_returns_null = ol->returns_null;
17812 current_function_returns_abnormally = ol->returns_abnormally;
17813 current_function_infinite_loop = ol->infinite_loop;
17814 }
17815
17816 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
17817 /*constexpr*/false);
17818
17819 finish_lambda_function (body);
17820
17821 if (nested)
17822 pop_function_context ();
17823 else
17824 --function_depth;
17825
17826 /* The capture list was built up in reverse order; fix that now. */
17827 LAMBDA_EXPR_CAPTURE_LIST (r)
17828 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
17829
17830 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
17831
17832 maybe_add_lambda_conv_op (type);
17833 }
17834
17835 finish_struct (type, /*attr*/NULL_TREE);
17836
17837 insert_pending_capture_proxies ();
17838
17839 return r;
17840 }
17841
17842 /* Like tsubst but deals with expressions and performs semantic
17843 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
17844
17845 tree
17846 tsubst_copy_and_build (tree t,
17847 tree args,
17848 tsubst_flags_t complain,
17849 tree in_decl,
17850 bool function_p,
17851 bool integral_constant_expression_p)
17852 {
17853 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
17854 #define RECUR(NODE) \
17855 tsubst_copy_and_build (NODE, args, complain, in_decl, \
17856 /*function_p=*/false, \
17857 integral_constant_expression_p)
17858
17859 tree retval, op1;
17860 location_t loc;
17861
17862 if (t == NULL_TREE || t == error_mark_node)
17863 return t;
17864
17865 loc = input_location;
17866 if (location_t eloc = cp_expr_location (t))
17867 input_location = eloc;
17868
17869 /* N3276 decltype magic only applies to calls at the top level or on the
17870 right side of a comma. */
17871 tsubst_flags_t decltype_flag = (complain & tf_decltype);
17872 complain &= ~tf_decltype;
17873
17874 switch (TREE_CODE (t))
17875 {
17876 case USING_DECL:
17877 t = DECL_NAME (t);
17878 /* Fall through. */
17879 case IDENTIFIER_NODE:
17880 {
17881 tree decl;
17882 cp_id_kind idk;
17883 bool non_integral_constant_expression_p;
17884 const char *error_msg;
17885
17886 if (IDENTIFIER_CONV_OP_P (t))
17887 {
17888 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17889 t = make_conv_op_name (new_type);
17890 }
17891
17892 /* Look up the name. */
17893 decl = lookup_name (t);
17894
17895 /* By convention, expressions use ERROR_MARK_NODE to indicate
17896 failure, not NULL_TREE. */
17897 if (decl == NULL_TREE)
17898 decl = error_mark_node;
17899
17900 decl = finish_id_expression (t, decl, NULL_TREE,
17901 &idk,
17902 integral_constant_expression_p,
17903 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
17904 &non_integral_constant_expression_p,
17905 /*template_p=*/false,
17906 /*done=*/true,
17907 /*address_p=*/false,
17908 /*template_arg_p=*/false,
17909 &error_msg,
17910 input_location);
17911 if (error_msg)
17912 error (error_msg);
17913 if (!function_p && identifier_p (decl))
17914 {
17915 if (complain & tf_error)
17916 unqualified_name_lookup_error (decl);
17917 decl = error_mark_node;
17918 }
17919 RETURN (decl);
17920 }
17921
17922 case TEMPLATE_ID_EXPR:
17923 {
17924 tree object;
17925 tree templ = RECUR (TREE_OPERAND (t, 0));
17926 tree targs = TREE_OPERAND (t, 1);
17927
17928 if (targs)
17929 targs = tsubst_template_args (targs, args, complain, in_decl);
17930 if (targs == error_mark_node)
17931 RETURN (error_mark_node);
17932
17933 if (TREE_CODE (templ) == SCOPE_REF)
17934 {
17935 tree name = TREE_OPERAND (templ, 1);
17936 tree tid = lookup_template_function (name, targs);
17937 TREE_OPERAND (templ, 1) = tid;
17938 RETURN (templ);
17939 }
17940
17941 if (variable_template_p (templ))
17942 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
17943
17944 if (TREE_CODE (templ) == COMPONENT_REF)
17945 {
17946 object = TREE_OPERAND (templ, 0);
17947 templ = TREE_OPERAND (templ, 1);
17948 }
17949 else
17950 object = NULL_TREE;
17951 templ = lookup_template_function (templ, targs);
17952
17953 if (object)
17954 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
17955 object, templ, NULL_TREE));
17956 else
17957 RETURN (baselink_for_fns (templ));
17958 }
17959
17960 case INDIRECT_REF:
17961 {
17962 tree r = RECUR (TREE_OPERAND (t, 0));
17963
17964 if (REFERENCE_REF_P (t))
17965 {
17966 /* A type conversion to reference type will be enclosed in
17967 such an indirect ref, but the substitution of the cast
17968 will have also added such an indirect ref. */
17969 r = convert_from_reference (r);
17970 }
17971 else
17972 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
17973 complain|decltype_flag);
17974
17975 if (REF_PARENTHESIZED_P (t))
17976 r = force_paren_expr (r);
17977
17978 RETURN (r);
17979 }
17980
17981 case NOP_EXPR:
17982 {
17983 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17984 tree op0 = RECUR (TREE_OPERAND (t, 0));
17985 RETURN (build_nop (type, op0));
17986 }
17987
17988 case IMPLICIT_CONV_EXPR:
17989 {
17990 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17991 tree expr = RECUR (TREE_OPERAND (t, 0));
17992 if (dependent_type_p (type) || type_dependent_expression_p (expr))
17993 {
17994 retval = copy_node (t);
17995 TREE_TYPE (retval) = type;
17996 TREE_OPERAND (retval, 0) = expr;
17997 RETURN (retval);
17998 }
17999 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
18000 /* We'll pass this to convert_nontype_argument again, we don't need
18001 to actually perform any conversion here. */
18002 RETURN (expr);
18003 int flags = LOOKUP_IMPLICIT;
18004 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
18005 flags = LOOKUP_NORMAL;
18006 RETURN (perform_implicit_conversion_flags (type, expr, complain,
18007 flags));
18008 }
18009
18010 case CONVERT_EXPR:
18011 {
18012 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18013 tree op0 = RECUR (TREE_OPERAND (t, 0));
18014 if (op0 == error_mark_node)
18015 RETURN (error_mark_node);
18016 RETURN (build1 (CONVERT_EXPR, type, op0));
18017 }
18018
18019 case CAST_EXPR:
18020 case REINTERPRET_CAST_EXPR:
18021 case CONST_CAST_EXPR:
18022 case DYNAMIC_CAST_EXPR:
18023 case STATIC_CAST_EXPR:
18024 {
18025 tree type;
18026 tree op, r = NULL_TREE;
18027
18028 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18029 if (integral_constant_expression_p
18030 && !cast_valid_in_integral_constant_expression_p (type))
18031 {
18032 if (complain & tf_error)
18033 error ("a cast to a type other than an integral or "
18034 "enumeration type cannot appear in a constant-expression");
18035 RETURN (error_mark_node);
18036 }
18037
18038 op = RECUR (TREE_OPERAND (t, 0));
18039
18040 warning_sentinel s(warn_useless_cast);
18041 warning_sentinel s2(warn_ignored_qualifiers);
18042 switch (TREE_CODE (t))
18043 {
18044 case CAST_EXPR:
18045 r = build_functional_cast (type, op, complain);
18046 break;
18047 case REINTERPRET_CAST_EXPR:
18048 r = build_reinterpret_cast (type, op, complain);
18049 break;
18050 case CONST_CAST_EXPR:
18051 r = build_const_cast (type, op, complain);
18052 break;
18053 case DYNAMIC_CAST_EXPR:
18054 r = build_dynamic_cast (type, op, complain);
18055 break;
18056 case STATIC_CAST_EXPR:
18057 r = build_static_cast (type, op, complain);
18058 break;
18059 default:
18060 gcc_unreachable ();
18061 }
18062
18063 RETURN (r);
18064 }
18065
18066 case POSTDECREMENT_EXPR:
18067 case POSTINCREMENT_EXPR:
18068 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18069 args, complain, in_decl);
18070 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
18071 complain|decltype_flag));
18072
18073 case PREDECREMENT_EXPR:
18074 case PREINCREMENT_EXPR:
18075 case NEGATE_EXPR:
18076 case BIT_NOT_EXPR:
18077 case ABS_EXPR:
18078 case TRUTH_NOT_EXPR:
18079 case UNARY_PLUS_EXPR: /* Unary + */
18080 case REALPART_EXPR:
18081 case IMAGPART_EXPR:
18082 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
18083 RECUR (TREE_OPERAND (t, 0)),
18084 complain|decltype_flag));
18085
18086 case FIX_TRUNC_EXPR:
18087 gcc_unreachable ();
18088
18089 case ADDR_EXPR:
18090 op1 = TREE_OPERAND (t, 0);
18091 if (TREE_CODE (op1) == LABEL_DECL)
18092 RETURN (finish_label_address_expr (DECL_NAME (op1),
18093 EXPR_LOCATION (op1)));
18094 if (TREE_CODE (op1) == SCOPE_REF)
18095 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
18096 /*done=*/true, /*address_p=*/true);
18097 else
18098 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
18099 in_decl);
18100 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
18101 complain|decltype_flag));
18102
18103 case PLUS_EXPR:
18104 case MINUS_EXPR:
18105 case MULT_EXPR:
18106 case TRUNC_DIV_EXPR:
18107 case CEIL_DIV_EXPR:
18108 case FLOOR_DIV_EXPR:
18109 case ROUND_DIV_EXPR:
18110 case EXACT_DIV_EXPR:
18111 case BIT_AND_EXPR:
18112 case BIT_IOR_EXPR:
18113 case BIT_XOR_EXPR:
18114 case TRUNC_MOD_EXPR:
18115 case FLOOR_MOD_EXPR:
18116 case TRUTH_ANDIF_EXPR:
18117 case TRUTH_ORIF_EXPR:
18118 case TRUTH_AND_EXPR:
18119 case TRUTH_OR_EXPR:
18120 case RSHIFT_EXPR:
18121 case LSHIFT_EXPR:
18122 case RROTATE_EXPR:
18123 case LROTATE_EXPR:
18124 case EQ_EXPR:
18125 case NE_EXPR:
18126 case MAX_EXPR:
18127 case MIN_EXPR:
18128 case LE_EXPR:
18129 case GE_EXPR:
18130 case LT_EXPR:
18131 case GT_EXPR:
18132 case MEMBER_REF:
18133 case DOTSTAR_EXPR:
18134 {
18135 warning_sentinel s1(warn_type_limits);
18136 warning_sentinel s2(warn_div_by_zero);
18137 warning_sentinel s3(warn_logical_op);
18138 warning_sentinel s4(warn_tautological_compare);
18139 tree op0 = RECUR (TREE_OPERAND (t, 0));
18140 tree op1 = RECUR (TREE_OPERAND (t, 1));
18141 tree r = build_x_binary_op
18142 (input_location, TREE_CODE (t),
18143 op0,
18144 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
18145 ? ERROR_MARK
18146 : TREE_CODE (TREE_OPERAND (t, 0))),
18147 op1,
18148 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
18149 ? ERROR_MARK
18150 : TREE_CODE (TREE_OPERAND (t, 1))),
18151 /*overload=*/NULL,
18152 complain|decltype_flag);
18153 if (EXPR_P (r) && TREE_NO_WARNING (t))
18154 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
18155
18156 RETURN (r);
18157 }
18158
18159 case POINTER_PLUS_EXPR:
18160 {
18161 tree op0 = RECUR (TREE_OPERAND (t, 0));
18162 tree op1 = RECUR (TREE_OPERAND (t, 1));
18163 RETURN (fold_build_pointer_plus (op0, op1));
18164 }
18165
18166 case SCOPE_REF:
18167 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
18168 /*address_p=*/false));
18169 case ARRAY_REF:
18170 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18171 args, complain, in_decl);
18172 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
18173 RECUR (TREE_OPERAND (t, 1)),
18174 complain|decltype_flag));
18175
18176 case SIZEOF_EXPR:
18177 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
18178 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
18179 RETURN (tsubst_copy (t, args, complain, in_decl));
18180 /* Fall through */
18181
18182 case ALIGNOF_EXPR:
18183 {
18184 tree r;
18185
18186 op1 = TREE_OPERAND (t, 0);
18187 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
18188 op1 = TREE_TYPE (op1);
18189 bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR
18190 && ALIGNOF_EXPR_STD_P (t));
18191 if (!args)
18192 {
18193 /* When there are no ARGS, we are trying to evaluate a
18194 non-dependent expression from the parser. Trying to do
18195 the substitutions may not work. */
18196 if (!TYPE_P (op1))
18197 op1 = TREE_TYPE (op1);
18198 }
18199 else
18200 {
18201 ++cp_unevaluated_operand;
18202 ++c_inhibit_evaluation_warnings;
18203 if (TYPE_P (op1))
18204 op1 = tsubst (op1, args, complain, in_decl);
18205 else
18206 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
18207 /*function_p=*/false,
18208 /*integral_constant_expression_p=*/
18209 false);
18210 --cp_unevaluated_operand;
18211 --c_inhibit_evaluation_warnings;
18212 }
18213 if (TYPE_P (op1))
18214 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t), std_alignof,
18215 complain & tf_error);
18216 else
18217 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
18218 complain & tf_error);
18219 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
18220 {
18221 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
18222 {
18223 if (!processing_template_decl && TYPE_P (op1))
18224 {
18225 r = build_min (SIZEOF_EXPR, size_type_node,
18226 build1 (NOP_EXPR, op1, error_mark_node));
18227 SIZEOF_EXPR_TYPE_P (r) = 1;
18228 }
18229 else
18230 r = build_min (SIZEOF_EXPR, size_type_node, op1);
18231 TREE_SIDE_EFFECTS (r) = 0;
18232 TREE_READONLY (r) = 1;
18233 }
18234 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
18235 }
18236 RETURN (r);
18237 }
18238
18239 case AT_ENCODE_EXPR:
18240 {
18241 op1 = TREE_OPERAND (t, 0);
18242 ++cp_unevaluated_operand;
18243 ++c_inhibit_evaluation_warnings;
18244 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
18245 /*function_p=*/false,
18246 /*integral_constant_expression_p=*/false);
18247 --cp_unevaluated_operand;
18248 --c_inhibit_evaluation_warnings;
18249 RETURN (objc_build_encode_expr (op1));
18250 }
18251
18252 case NOEXCEPT_EXPR:
18253 op1 = TREE_OPERAND (t, 0);
18254 ++cp_unevaluated_operand;
18255 ++c_inhibit_evaluation_warnings;
18256 ++cp_noexcept_operand;
18257 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
18258 /*function_p=*/false,
18259 /*integral_constant_expression_p=*/false);
18260 --cp_unevaluated_operand;
18261 --c_inhibit_evaluation_warnings;
18262 --cp_noexcept_operand;
18263 RETURN (finish_noexcept_expr (op1, complain));
18264
18265 case MODOP_EXPR:
18266 {
18267 warning_sentinel s(warn_div_by_zero);
18268 tree lhs = RECUR (TREE_OPERAND (t, 0));
18269 tree rhs = RECUR (TREE_OPERAND (t, 2));
18270 tree r = build_x_modify_expr
18271 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
18272 complain|decltype_flag);
18273 /* TREE_NO_WARNING must be set if either the expression was
18274 parenthesized or it uses an operator such as >>= rather
18275 than plain assignment. In the former case, it was already
18276 set and must be copied. In the latter case,
18277 build_x_modify_expr sets it and it must not be reset
18278 here. */
18279 if (TREE_NO_WARNING (t))
18280 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
18281
18282 RETURN (r);
18283 }
18284
18285 case ARROW_EXPR:
18286 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18287 args, complain, in_decl);
18288 /* Remember that there was a reference to this entity. */
18289 if (DECL_P (op1)
18290 && !mark_used (op1, complain) && !(complain & tf_error))
18291 RETURN (error_mark_node);
18292 RETURN (build_x_arrow (input_location, op1, complain));
18293
18294 case NEW_EXPR:
18295 {
18296 tree placement = RECUR (TREE_OPERAND (t, 0));
18297 tree init = RECUR (TREE_OPERAND (t, 3));
18298 vec<tree, va_gc> *placement_vec;
18299 vec<tree, va_gc> *init_vec;
18300 tree ret;
18301
18302 if (placement == NULL_TREE)
18303 placement_vec = NULL;
18304 else
18305 {
18306 placement_vec = make_tree_vector ();
18307 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
18308 vec_safe_push (placement_vec, TREE_VALUE (placement));
18309 }
18310
18311 /* If there was an initializer in the original tree, but it
18312 instantiated to an empty list, then we should pass a
18313 non-NULL empty vector to tell build_new that it was an
18314 empty initializer() rather than no initializer. This can
18315 only happen when the initializer is a pack expansion whose
18316 parameter packs are of length zero. */
18317 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
18318 init_vec = NULL;
18319 else
18320 {
18321 init_vec = make_tree_vector ();
18322 if (init == void_node)
18323 gcc_assert (init_vec != NULL);
18324 else
18325 {
18326 for (; init != NULL_TREE; init = TREE_CHAIN (init))
18327 vec_safe_push (init_vec, TREE_VALUE (init));
18328 }
18329 }
18330
18331 /* Avoid passing an enclosing decl to valid_array_size_p. */
18332 in_decl = NULL_TREE;
18333
18334 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
18335 tree op2 = RECUR (TREE_OPERAND (t, 2));
18336 ret = build_new (&placement_vec, op1, op2, &init_vec,
18337 NEW_EXPR_USE_GLOBAL (t),
18338 complain);
18339
18340 if (placement_vec != NULL)
18341 release_tree_vector (placement_vec);
18342 if (init_vec != NULL)
18343 release_tree_vector (init_vec);
18344
18345 RETURN (ret);
18346 }
18347
18348 case DELETE_EXPR:
18349 {
18350 tree op0 = RECUR (TREE_OPERAND (t, 0));
18351 tree op1 = RECUR (TREE_OPERAND (t, 1));
18352 RETURN (delete_sanity (op0, op1,
18353 DELETE_EXPR_USE_VEC (t),
18354 DELETE_EXPR_USE_GLOBAL (t),
18355 complain));
18356 }
18357
18358 case COMPOUND_EXPR:
18359 {
18360 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
18361 complain & ~tf_decltype, in_decl,
18362 /*function_p=*/false,
18363 integral_constant_expression_p);
18364 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
18365 op0,
18366 RECUR (TREE_OPERAND (t, 1)),
18367 complain|decltype_flag));
18368 }
18369
18370 case CALL_EXPR:
18371 {
18372 tree function;
18373 vec<tree, va_gc> *call_args;
18374 unsigned int nargs, i;
18375 bool qualified_p;
18376 bool koenig_p;
18377 tree ret;
18378
18379 function = CALL_EXPR_FN (t);
18380 /* Internal function with no arguments. */
18381 if (function == NULL_TREE && call_expr_nargs (t) == 0)
18382 RETURN (t);
18383
18384 /* When we parsed the expression, we determined whether or
18385 not Koenig lookup should be performed. */
18386 koenig_p = KOENIG_LOOKUP_P (t);
18387 if (function == NULL_TREE)
18388 {
18389 koenig_p = false;
18390 qualified_p = false;
18391 }
18392 else if (TREE_CODE (function) == SCOPE_REF)
18393 {
18394 qualified_p = true;
18395 function = tsubst_qualified_id (function, args, complain, in_decl,
18396 /*done=*/false,
18397 /*address_p=*/false);
18398 }
18399 else if (koenig_p && identifier_p (function))
18400 {
18401 /* Do nothing; calling tsubst_copy_and_build on an identifier
18402 would incorrectly perform unqualified lookup again.
18403
18404 Note that we can also have an IDENTIFIER_NODE if the earlier
18405 unqualified lookup found a member function; in that case
18406 koenig_p will be false and we do want to do the lookup
18407 again to find the instantiated member function.
18408
18409 FIXME but doing that causes c++/15272, so we need to stop
18410 using IDENTIFIER_NODE in that situation. */
18411 qualified_p = false;
18412 }
18413 else
18414 {
18415 if (TREE_CODE (function) == COMPONENT_REF)
18416 {
18417 tree op = TREE_OPERAND (function, 1);
18418
18419 qualified_p = (TREE_CODE (op) == SCOPE_REF
18420 || (BASELINK_P (op)
18421 && BASELINK_QUALIFIED_P (op)));
18422 }
18423 else
18424 qualified_p = false;
18425
18426 if (TREE_CODE (function) == ADDR_EXPR
18427 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
18428 /* Avoid error about taking the address of a constructor. */
18429 function = TREE_OPERAND (function, 0);
18430
18431 function = tsubst_copy_and_build (function, args, complain,
18432 in_decl,
18433 !qualified_p,
18434 integral_constant_expression_p);
18435
18436 if (BASELINK_P (function))
18437 qualified_p = true;
18438 }
18439
18440 nargs = call_expr_nargs (t);
18441 call_args = make_tree_vector ();
18442 for (i = 0; i < nargs; ++i)
18443 {
18444 tree arg = CALL_EXPR_ARG (t, i);
18445
18446 if (!PACK_EXPANSION_P (arg))
18447 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
18448 else
18449 {
18450 /* Expand the pack expansion and push each entry onto
18451 CALL_ARGS. */
18452 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
18453 if (TREE_CODE (arg) == TREE_VEC)
18454 {
18455 unsigned int len, j;
18456
18457 len = TREE_VEC_LENGTH (arg);
18458 for (j = 0; j < len; ++j)
18459 {
18460 tree value = TREE_VEC_ELT (arg, j);
18461 if (value != NULL_TREE)
18462 value = convert_from_reference (value);
18463 vec_safe_push (call_args, value);
18464 }
18465 }
18466 else
18467 {
18468 /* A partial substitution. Add one entry. */
18469 vec_safe_push (call_args, arg);
18470 }
18471 }
18472 }
18473
18474 /* We do not perform argument-dependent lookup if normal
18475 lookup finds a non-function, in accordance with the
18476 expected resolution of DR 218. */
18477 if (koenig_p
18478 && ((is_overloaded_fn (function)
18479 /* If lookup found a member function, the Koenig lookup is
18480 not appropriate, even if an unqualified-name was used
18481 to denote the function. */
18482 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
18483 || identifier_p (function))
18484 /* Only do this when substitution turns a dependent call
18485 into a non-dependent call. */
18486 && type_dependent_expression_p_push (t)
18487 && !any_type_dependent_arguments_p (call_args))
18488 function = perform_koenig_lookup (function, call_args, tf_none);
18489
18490 if (function != NULL_TREE
18491 && identifier_p (function)
18492 && !any_type_dependent_arguments_p (call_args))
18493 {
18494 if (koenig_p && (complain & tf_warning_or_error))
18495 {
18496 /* For backwards compatibility and good diagnostics, try
18497 the unqualified lookup again if we aren't in SFINAE
18498 context. */
18499 tree unq = (tsubst_copy_and_build
18500 (function, args, complain, in_decl, true,
18501 integral_constant_expression_p));
18502 if (unq == error_mark_node)
18503 {
18504 release_tree_vector (call_args);
18505 RETURN (error_mark_node);
18506 }
18507
18508 if (unq != function)
18509 {
18510 /* In a lambda fn, we have to be careful to not
18511 introduce new this captures. Legacy code can't
18512 be using lambdas anyway, so it's ok to be
18513 stricter. */
18514 bool in_lambda = (current_class_type
18515 && LAMBDA_TYPE_P (current_class_type));
18516 char const *const msg
18517 = G_("%qD was not declared in this scope, "
18518 "and no declarations were found by "
18519 "argument-dependent lookup at the point "
18520 "of instantiation");
18521
18522 bool diag = true;
18523 if (in_lambda)
18524 error_at (cp_expr_loc_or_loc (t, input_location),
18525 msg, function);
18526 else
18527 diag = permerror (cp_expr_loc_or_loc (t, input_location),
18528 msg, function);
18529 if (diag)
18530 {
18531 tree fn = unq;
18532
18533 if (INDIRECT_REF_P (fn))
18534 fn = TREE_OPERAND (fn, 0);
18535 if (is_overloaded_fn (fn))
18536 fn = get_first_fn (fn);
18537
18538 if (!DECL_P (fn))
18539 /* Can't say anything more. */;
18540 else if (DECL_CLASS_SCOPE_P (fn))
18541 {
18542 location_t loc = cp_expr_loc_or_loc (t,
18543 input_location);
18544 inform (loc,
18545 "declarations in dependent base %qT are "
18546 "not found by unqualified lookup",
18547 DECL_CLASS_CONTEXT (fn));
18548 if (current_class_ptr)
18549 inform (loc,
18550 "use %<this->%D%> instead", function);
18551 else
18552 inform (loc,
18553 "use %<%T::%D%> instead",
18554 current_class_name, function);
18555 }
18556 else
18557 inform (DECL_SOURCE_LOCATION (fn),
18558 "%qD declared here, later in the "
18559 "translation unit", fn);
18560 if (in_lambda)
18561 {
18562 release_tree_vector (call_args);
18563 RETURN (error_mark_node);
18564 }
18565 }
18566
18567 function = unq;
18568 }
18569 }
18570 if (identifier_p (function))
18571 {
18572 if (complain & tf_error)
18573 unqualified_name_lookup_error (function);
18574 release_tree_vector (call_args);
18575 RETURN (error_mark_node);
18576 }
18577 }
18578
18579 /* Remember that there was a reference to this entity. */
18580 if (function != NULL_TREE
18581 && DECL_P (function)
18582 && !mark_used (function, complain) && !(complain & tf_error))
18583 {
18584 release_tree_vector (call_args);
18585 RETURN (error_mark_node);
18586 }
18587
18588 /* Put back tf_decltype for the actual call. */
18589 complain |= decltype_flag;
18590
18591 if (function == NULL_TREE)
18592 switch (CALL_EXPR_IFN (t))
18593 {
18594 case IFN_LAUNDER:
18595 gcc_assert (nargs == 1);
18596 if (vec_safe_length (call_args) != 1)
18597 {
18598 error_at (cp_expr_loc_or_loc (t, input_location),
18599 "wrong number of arguments to "
18600 "%<__builtin_launder%>");
18601 ret = error_mark_node;
18602 }
18603 else
18604 ret = finish_builtin_launder (cp_expr_loc_or_loc (t,
18605 input_location),
18606 (*call_args)[0], complain);
18607 break;
18608
18609 default:
18610 /* Unsupported internal function with arguments. */
18611 gcc_unreachable ();
18612 }
18613 else if (TREE_CODE (function) == OFFSET_REF
18614 || TREE_CODE (function) == DOTSTAR_EXPR
18615 || TREE_CODE (function) == MEMBER_REF)
18616 ret = build_offset_ref_call_from_tree (function, &call_args,
18617 complain);
18618 else if (TREE_CODE (function) == COMPONENT_REF)
18619 {
18620 tree instance = TREE_OPERAND (function, 0);
18621 tree fn = TREE_OPERAND (function, 1);
18622
18623 if (processing_template_decl
18624 && (type_dependent_expression_p (instance)
18625 || (!BASELINK_P (fn)
18626 && TREE_CODE (fn) != FIELD_DECL)
18627 || type_dependent_expression_p (fn)
18628 || any_type_dependent_arguments_p (call_args)))
18629 ret = build_min_nt_call_vec (function, call_args);
18630 else if (!BASELINK_P (fn))
18631 ret = finish_call_expr (function, &call_args,
18632 /*disallow_virtual=*/false,
18633 /*koenig_p=*/false,
18634 complain);
18635 else
18636 ret = (build_new_method_call
18637 (instance, fn,
18638 &call_args, NULL_TREE,
18639 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
18640 /*fn_p=*/NULL,
18641 complain));
18642 }
18643 else
18644 ret = finish_call_expr (function, &call_args,
18645 /*disallow_virtual=*/qualified_p,
18646 koenig_p,
18647 complain);
18648
18649 release_tree_vector (call_args);
18650
18651 if (ret != error_mark_node)
18652 {
18653 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
18654 bool ord = CALL_EXPR_ORDERED_ARGS (t);
18655 bool rev = CALL_EXPR_REVERSE_ARGS (t);
18656 bool thk = CALL_FROM_THUNK_P (t);
18657 if (op || ord || rev || thk)
18658 {
18659 function = extract_call_expr (ret);
18660 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
18661 CALL_EXPR_ORDERED_ARGS (function) = ord;
18662 CALL_EXPR_REVERSE_ARGS (function) = rev;
18663 if (thk)
18664 {
18665 if (TREE_CODE (function) == CALL_EXPR)
18666 CALL_FROM_THUNK_P (function) = true;
18667 else
18668 AGGR_INIT_FROM_THUNK_P (function) = true;
18669 /* The thunk location is not interesting. */
18670 SET_EXPR_LOCATION (function, UNKNOWN_LOCATION);
18671 }
18672 }
18673 }
18674
18675 RETURN (ret);
18676 }
18677
18678 case COND_EXPR:
18679 {
18680 tree cond = RECUR (TREE_OPERAND (t, 0));
18681 cond = mark_rvalue_use (cond);
18682 tree folded_cond = fold_non_dependent_expr (cond, complain);
18683 tree exp1, exp2;
18684
18685 if (TREE_CODE (folded_cond) == INTEGER_CST)
18686 {
18687 if (integer_zerop (folded_cond))
18688 {
18689 ++c_inhibit_evaluation_warnings;
18690 exp1 = RECUR (TREE_OPERAND (t, 1));
18691 --c_inhibit_evaluation_warnings;
18692 exp2 = RECUR (TREE_OPERAND (t, 2));
18693 }
18694 else
18695 {
18696 exp1 = RECUR (TREE_OPERAND (t, 1));
18697 ++c_inhibit_evaluation_warnings;
18698 exp2 = RECUR (TREE_OPERAND (t, 2));
18699 --c_inhibit_evaluation_warnings;
18700 }
18701 cond = folded_cond;
18702 }
18703 else
18704 {
18705 exp1 = RECUR (TREE_OPERAND (t, 1));
18706 exp2 = RECUR (TREE_OPERAND (t, 2));
18707 }
18708
18709 warning_sentinel s(warn_duplicated_branches);
18710 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
18711 cond, exp1, exp2, complain));
18712 }
18713
18714 case PSEUDO_DTOR_EXPR:
18715 {
18716 tree op0 = RECUR (TREE_OPERAND (t, 0));
18717 tree op1 = RECUR (TREE_OPERAND (t, 1));
18718 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
18719 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
18720 input_location));
18721 }
18722
18723 case TREE_LIST:
18724 {
18725 tree purpose, value, chain;
18726
18727 if (t == void_list_node)
18728 RETURN (t);
18729
18730 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
18731 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
18732 {
18733 /* We have pack expansions, so expand those and
18734 create a new list out of it. */
18735 tree purposevec = NULL_TREE;
18736 tree valuevec = NULL_TREE;
18737 tree chain;
18738 int i, len = -1;
18739
18740 /* Expand the argument expressions. */
18741 if (TREE_PURPOSE (t))
18742 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
18743 complain, in_decl);
18744 if (TREE_VALUE (t))
18745 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
18746 complain, in_decl);
18747
18748 /* Build the rest of the list. */
18749 chain = TREE_CHAIN (t);
18750 if (chain && chain != void_type_node)
18751 chain = RECUR (chain);
18752
18753 /* Determine the number of arguments. */
18754 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
18755 {
18756 len = TREE_VEC_LENGTH (purposevec);
18757 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
18758 }
18759 else if (TREE_CODE (valuevec) == TREE_VEC)
18760 len = TREE_VEC_LENGTH (valuevec);
18761 else
18762 {
18763 /* Since we only performed a partial substitution into
18764 the argument pack, we only RETURN (a single list
18765 node. */
18766 if (purposevec == TREE_PURPOSE (t)
18767 && valuevec == TREE_VALUE (t)
18768 && chain == TREE_CHAIN (t))
18769 RETURN (t);
18770
18771 RETURN (tree_cons (purposevec, valuevec, chain));
18772 }
18773
18774 /* Convert the argument vectors into a TREE_LIST */
18775 i = len;
18776 while (i > 0)
18777 {
18778 /* Grab the Ith values. */
18779 i--;
18780 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
18781 : NULL_TREE;
18782 value
18783 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
18784 : NULL_TREE;
18785
18786 /* Build the list (backwards). */
18787 chain = tree_cons (purpose, value, chain);
18788 }
18789
18790 RETURN (chain);
18791 }
18792
18793 purpose = TREE_PURPOSE (t);
18794 if (purpose)
18795 purpose = RECUR (purpose);
18796 value = TREE_VALUE (t);
18797 if (value)
18798 value = RECUR (value);
18799 chain = TREE_CHAIN (t);
18800 if (chain && chain != void_type_node)
18801 chain = RECUR (chain);
18802 if (purpose == TREE_PURPOSE (t)
18803 && value == TREE_VALUE (t)
18804 && chain == TREE_CHAIN (t))
18805 RETURN (t);
18806 RETURN (tree_cons (purpose, value, chain));
18807 }
18808
18809 case COMPONENT_REF:
18810 {
18811 tree object;
18812 tree object_type;
18813 tree member;
18814 tree r;
18815
18816 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18817 args, complain, in_decl);
18818 /* Remember that there was a reference to this entity. */
18819 if (DECL_P (object)
18820 && !mark_used (object, complain) && !(complain & tf_error))
18821 RETURN (error_mark_node);
18822 object_type = TREE_TYPE (object);
18823
18824 member = TREE_OPERAND (t, 1);
18825 if (BASELINK_P (member))
18826 member = tsubst_baselink (member,
18827 non_reference (TREE_TYPE (object)),
18828 args, complain, in_decl);
18829 else
18830 member = tsubst_copy (member, args, complain, in_decl);
18831 if (member == error_mark_node)
18832 RETURN (error_mark_node);
18833
18834 if (TREE_CODE (member) == FIELD_DECL)
18835 {
18836 r = finish_non_static_data_member (member, object, NULL_TREE);
18837 if (TREE_CODE (r) == COMPONENT_REF)
18838 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
18839 RETURN (r);
18840 }
18841 else if (type_dependent_expression_p (object))
18842 /* We can't do much here. */;
18843 else if (!CLASS_TYPE_P (object_type))
18844 {
18845 if (scalarish_type_p (object_type))
18846 {
18847 tree s = NULL_TREE;
18848 tree dtor = member;
18849
18850 if (TREE_CODE (dtor) == SCOPE_REF)
18851 {
18852 s = TREE_OPERAND (dtor, 0);
18853 dtor = TREE_OPERAND (dtor, 1);
18854 }
18855 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
18856 {
18857 dtor = TREE_OPERAND (dtor, 0);
18858 if (TYPE_P (dtor))
18859 RETURN (finish_pseudo_destructor_expr
18860 (object, s, dtor, input_location));
18861 }
18862 }
18863 }
18864 else if (TREE_CODE (member) == SCOPE_REF
18865 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
18866 {
18867 /* Lookup the template functions now that we know what the
18868 scope is. */
18869 tree scope = TREE_OPERAND (member, 0);
18870 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
18871 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
18872 member = lookup_qualified_name (scope, tmpl,
18873 /*is_type_p=*/false,
18874 /*complain=*/false);
18875 if (BASELINK_P (member))
18876 {
18877 BASELINK_FUNCTIONS (member)
18878 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
18879 args);
18880 member = (adjust_result_of_qualified_name_lookup
18881 (member, BINFO_TYPE (BASELINK_BINFO (member)),
18882 object_type));
18883 }
18884 else
18885 {
18886 qualified_name_lookup_error (scope, tmpl, member,
18887 input_location);
18888 RETURN (error_mark_node);
18889 }
18890 }
18891 else if (TREE_CODE (member) == SCOPE_REF
18892 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
18893 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
18894 {
18895 if (complain & tf_error)
18896 {
18897 if (TYPE_P (TREE_OPERAND (member, 0)))
18898 error ("%qT is not a class or namespace",
18899 TREE_OPERAND (member, 0));
18900 else
18901 error ("%qD is not a class or namespace",
18902 TREE_OPERAND (member, 0));
18903 }
18904 RETURN (error_mark_node);
18905 }
18906
18907 r = finish_class_member_access_expr (object, member,
18908 /*template_p=*/false,
18909 complain);
18910 if (TREE_CODE (r) == COMPONENT_REF)
18911 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
18912 RETURN (r);
18913 }
18914
18915 case THROW_EXPR:
18916 RETURN (build_throw
18917 (RECUR (TREE_OPERAND (t, 0))));
18918
18919 case CONSTRUCTOR:
18920 {
18921 vec<constructor_elt, va_gc> *n;
18922 constructor_elt *ce;
18923 unsigned HOST_WIDE_INT idx;
18924 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18925 bool process_index_p;
18926 int newlen;
18927 bool need_copy_p = false;
18928 tree r;
18929
18930 if (type == error_mark_node)
18931 RETURN (error_mark_node);
18932
18933 /* We do not want to process the index of aggregate
18934 initializers as they are identifier nodes which will be
18935 looked up by digest_init. */
18936 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
18937
18938 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
18939 newlen = vec_safe_length (n);
18940 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
18941 {
18942 if (ce->index && process_index_p
18943 /* An identifier index is looked up in the type
18944 being initialized, not the current scope. */
18945 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
18946 ce->index = RECUR (ce->index);
18947
18948 if (PACK_EXPANSION_P (ce->value))
18949 {
18950 /* Substitute into the pack expansion. */
18951 ce->value = tsubst_pack_expansion (ce->value, args, complain,
18952 in_decl);
18953
18954 if (ce->value == error_mark_node
18955 || PACK_EXPANSION_P (ce->value))
18956 ;
18957 else if (TREE_VEC_LENGTH (ce->value) == 1)
18958 /* Just move the argument into place. */
18959 ce->value = TREE_VEC_ELT (ce->value, 0);
18960 else
18961 {
18962 /* Update the length of the final CONSTRUCTOR
18963 arguments vector, and note that we will need to
18964 copy.*/
18965 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
18966 need_copy_p = true;
18967 }
18968 }
18969 else
18970 ce->value = RECUR (ce->value);
18971 }
18972
18973 if (need_copy_p)
18974 {
18975 vec<constructor_elt, va_gc> *old_n = n;
18976
18977 vec_alloc (n, newlen);
18978 FOR_EACH_VEC_ELT (*old_n, idx, ce)
18979 {
18980 if (TREE_CODE (ce->value) == TREE_VEC)
18981 {
18982 int i, len = TREE_VEC_LENGTH (ce->value);
18983 for (i = 0; i < len; ++i)
18984 CONSTRUCTOR_APPEND_ELT (n, 0,
18985 TREE_VEC_ELT (ce->value, i));
18986 }
18987 else
18988 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
18989 }
18990 }
18991
18992 r = build_constructor (init_list_type_node, n);
18993 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
18994
18995 if (TREE_HAS_CONSTRUCTOR (t))
18996 {
18997 fcl_t cl = fcl_functional;
18998 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
18999 cl = fcl_c99;
19000 RETURN (finish_compound_literal (type, r, complain, cl));
19001 }
19002
19003 TREE_TYPE (r) = type;
19004 RETURN (r);
19005 }
19006
19007 case TYPEID_EXPR:
19008 {
19009 tree operand_0 = TREE_OPERAND (t, 0);
19010 if (TYPE_P (operand_0))
19011 {
19012 operand_0 = tsubst (operand_0, args, complain, in_decl);
19013 RETURN (get_typeid (operand_0, complain));
19014 }
19015 else
19016 {
19017 operand_0 = RECUR (operand_0);
19018 RETURN (build_typeid (operand_0, complain));
19019 }
19020 }
19021
19022 case VAR_DECL:
19023 if (!args)
19024 RETURN (t);
19025 /* Fall through */
19026
19027 case PARM_DECL:
19028 {
19029 tree r = tsubst_copy (t, args, complain, in_decl);
19030 /* ??? We're doing a subset of finish_id_expression here. */
19031 if (VAR_P (r)
19032 && !processing_template_decl
19033 && !cp_unevaluated_operand
19034 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
19035 && CP_DECL_THREAD_LOCAL_P (r))
19036 {
19037 if (tree wrap = get_tls_wrapper_fn (r))
19038 /* Replace an evaluated use of the thread_local variable with
19039 a call to its wrapper. */
19040 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
19041 }
19042 else if (outer_automatic_var_p (r))
19043 r = process_outer_var_ref (r, complain);
19044
19045 if (!TYPE_REF_P (TREE_TYPE (t)))
19046 /* If the original type was a reference, we'll be wrapped in
19047 the appropriate INDIRECT_REF. */
19048 r = convert_from_reference (r);
19049 RETURN (r);
19050 }
19051
19052 case VA_ARG_EXPR:
19053 {
19054 tree op0 = RECUR (TREE_OPERAND (t, 0));
19055 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19056 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
19057 }
19058
19059 case OFFSETOF_EXPR:
19060 {
19061 tree object_ptr
19062 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
19063 in_decl, /*function_p=*/false,
19064 /*integral_constant_expression_p=*/false);
19065 RETURN (finish_offsetof (object_ptr,
19066 RECUR (TREE_OPERAND (t, 0)),
19067 EXPR_LOCATION (t)));
19068 }
19069
19070 case ADDRESSOF_EXPR:
19071 RETURN (cp_build_addressof (EXPR_LOCATION (t),
19072 RECUR (TREE_OPERAND (t, 0)), complain));
19073
19074 case TRAIT_EXPR:
19075 {
19076 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
19077 complain, in_decl);
19078
19079 tree type2 = TRAIT_EXPR_TYPE2 (t);
19080 if (type2 && TREE_CODE (type2) == TREE_LIST)
19081 type2 = RECUR (type2);
19082 else if (type2)
19083 type2 = tsubst (type2, args, complain, in_decl);
19084
19085 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
19086 }
19087
19088 case STMT_EXPR:
19089 {
19090 tree old_stmt_expr = cur_stmt_expr;
19091 tree stmt_expr = begin_stmt_expr ();
19092
19093 cur_stmt_expr = stmt_expr;
19094 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
19095 integral_constant_expression_p);
19096 stmt_expr = finish_stmt_expr (stmt_expr, false);
19097 cur_stmt_expr = old_stmt_expr;
19098
19099 /* If the resulting list of expression statement is empty,
19100 fold it further into void_node. */
19101 if (empty_expr_stmt_p (stmt_expr))
19102 stmt_expr = void_node;
19103
19104 RETURN (stmt_expr);
19105 }
19106
19107 case LAMBDA_EXPR:
19108 {
19109 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
19110
19111 RETURN (build_lambda_object (r));
19112 }
19113
19114 case TARGET_EXPR:
19115 /* We can get here for a constant initializer of non-dependent type.
19116 FIXME stop folding in cp_parser_initializer_clause. */
19117 {
19118 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
19119 complain);
19120 RETURN (r);
19121 }
19122
19123 case TRANSACTION_EXPR:
19124 RETURN (tsubst_expr(t, args, complain, in_decl,
19125 integral_constant_expression_p));
19126
19127 case PAREN_EXPR:
19128 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
19129
19130 case VEC_PERM_EXPR:
19131 {
19132 tree op0 = RECUR (TREE_OPERAND (t, 0));
19133 tree op1 = RECUR (TREE_OPERAND (t, 1));
19134 tree op2 = RECUR (TREE_OPERAND (t, 2));
19135 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
19136 complain));
19137 }
19138
19139 case REQUIRES_EXPR:
19140 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
19141
19142 case NON_LVALUE_EXPR:
19143 case VIEW_CONVERT_EXPR:
19144 if (location_wrapper_p (t))
19145 /* We need to do this here as well as in tsubst_copy so we get the
19146 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
19147 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)),
19148 EXPR_LOCATION (t)));
19149 /* fallthrough. */
19150
19151 default:
19152 /* Handle Objective-C++ constructs, if appropriate. */
19153 {
19154 tree subst
19155 = objcp_tsubst_copy_and_build (t, args, complain,
19156 in_decl, /*function_p=*/false);
19157 if (subst)
19158 RETURN (subst);
19159 }
19160 RETURN (tsubst_copy (t, args, complain, in_decl));
19161 }
19162
19163 #undef RECUR
19164 #undef RETURN
19165 out:
19166 input_location = loc;
19167 return retval;
19168 }
19169
19170 /* Verify that the instantiated ARGS are valid. For type arguments,
19171 make sure that the type's linkage is ok. For non-type arguments,
19172 make sure they are constants if they are integral or enumerations.
19173 Emit an error under control of COMPLAIN, and return TRUE on error. */
19174
19175 static bool
19176 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
19177 {
19178 if (dependent_template_arg_p (t))
19179 return false;
19180 if (ARGUMENT_PACK_P (t))
19181 {
19182 tree vec = ARGUMENT_PACK_ARGS (t);
19183 int len = TREE_VEC_LENGTH (vec);
19184 bool result = false;
19185 int i;
19186
19187 for (i = 0; i < len; ++i)
19188 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
19189 result = true;
19190 return result;
19191 }
19192 else if (TYPE_P (t))
19193 {
19194 /* [basic.link]: A name with no linkage (notably, the name
19195 of a class or enumeration declared in a local scope)
19196 shall not be used to declare an entity with linkage.
19197 This implies that names with no linkage cannot be used as
19198 template arguments
19199
19200 DR 757 relaxes this restriction for C++0x. */
19201 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
19202 : no_linkage_check (t, /*relaxed_p=*/false));
19203
19204 if (nt)
19205 {
19206 /* DR 488 makes use of a type with no linkage cause
19207 type deduction to fail. */
19208 if (complain & tf_error)
19209 {
19210 if (TYPE_UNNAMED_P (nt))
19211 error ("%qT is/uses unnamed type", t);
19212 else
19213 error ("template argument for %qD uses local type %qT",
19214 tmpl, t);
19215 }
19216 return true;
19217 }
19218 /* In order to avoid all sorts of complications, we do not
19219 allow variably-modified types as template arguments. */
19220 else if (variably_modified_type_p (t, NULL_TREE))
19221 {
19222 if (complain & tf_error)
19223 error ("%qT is a variably modified type", t);
19224 return true;
19225 }
19226 }
19227 /* Class template and alias template arguments should be OK. */
19228 else if (DECL_TYPE_TEMPLATE_P (t))
19229 ;
19230 /* A non-type argument of integral or enumerated type must be a
19231 constant. */
19232 else if (TREE_TYPE (t)
19233 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
19234 && !REFERENCE_REF_P (t)
19235 && !TREE_CONSTANT (t))
19236 {
19237 if (complain & tf_error)
19238 error ("integral expression %qE is not constant", t);
19239 return true;
19240 }
19241 return false;
19242 }
19243
19244 static bool
19245 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
19246 {
19247 int ix, len = DECL_NTPARMS (tmpl);
19248 bool result = false;
19249
19250 for (ix = 0; ix != len; ix++)
19251 {
19252 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
19253 result = true;
19254 }
19255 if (result && (complain & tf_error))
19256 error (" trying to instantiate %qD", tmpl);
19257 return result;
19258 }
19259
19260 /* We're out of SFINAE context now, so generate diagnostics for the access
19261 errors we saw earlier when instantiating D from TMPL and ARGS. */
19262
19263 static void
19264 recheck_decl_substitution (tree d, tree tmpl, tree args)
19265 {
19266 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
19267 tree type = TREE_TYPE (pattern);
19268 location_t loc = input_location;
19269
19270 push_access_scope (d);
19271 push_deferring_access_checks (dk_no_deferred);
19272 input_location = DECL_SOURCE_LOCATION (pattern);
19273 tsubst (type, args, tf_warning_or_error, d);
19274 input_location = loc;
19275 pop_deferring_access_checks ();
19276 pop_access_scope (d);
19277 }
19278
19279 /* Instantiate the indicated variable, function, or alias template TMPL with
19280 the template arguments in TARG_PTR. */
19281
19282 static tree
19283 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
19284 {
19285 tree targ_ptr = orig_args;
19286 tree fndecl;
19287 tree gen_tmpl;
19288 tree spec;
19289 bool access_ok = true;
19290
19291 if (tmpl == error_mark_node)
19292 return error_mark_node;
19293
19294 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
19295
19296 /* If this function is a clone, handle it specially. */
19297 if (DECL_CLONED_FUNCTION_P (tmpl))
19298 {
19299 tree spec;
19300 tree clone;
19301
19302 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
19303 DECL_CLONED_FUNCTION. */
19304 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
19305 targ_ptr, complain);
19306 if (spec == error_mark_node)
19307 return error_mark_node;
19308
19309 /* Look for the clone. */
19310 FOR_EACH_CLONE (clone, spec)
19311 if (DECL_NAME (clone) == DECL_NAME (tmpl))
19312 return clone;
19313 /* We should always have found the clone by now. */
19314 gcc_unreachable ();
19315 return NULL_TREE;
19316 }
19317
19318 if (targ_ptr == error_mark_node)
19319 return error_mark_node;
19320
19321 /* Check to see if we already have this specialization. */
19322 gen_tmpl = most_general_template (tmpl);
19323 if (TMPL_ARGS_DEPTH (targ_ptr)
19324 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
19325 /* targ_ptr only has the innermost template args, so add the outer ones
19326 from tmpl, which could be either a partial instantiation or gen_tmpl (in
19327 the case of a non-dependent call within a template definition). */
19328 targ_ptr = (add_outermost_template_args
19329 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
19330 targ_ptr));
19331
19332 /* It would be nice to avoid hashing here and then again in tsubst_decl,
19333 but it doesn't seem to be on the hot path. */
19334 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
19335
19336 gcc_assert (tmpl == gen_tmpl
19337 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
19338 == spec)
19339 || fndecl == NULL_TREE);
19340
19341 if (spec != NULL_TREE)
19342 {
19343 if (FNDECL_HAS_ACCESS_ERRORS (spec))
19344 {
19345 if (complain & tf_error)
19346 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
19347 return error_mark_node;
19348 }
19349 return spec;
19350 }
19351
19352 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
19353 complain))
19354 return error_mark_node;
19355
19356 /* We are building a FUNCTION_DECL, during which the access of its
19357 parameters and return types have to be checked. However this
19358 FUNCTION_DECL which is the desired context for access checking
19359 is not built yet. We solve this chicken-and-egg problem by
19360 deferring all checks until we have the FUNCTION_DECL. */
19361 push_deferring_access_checks (dk_deferred);
19362
19363 /* Instantiation of the function happens in the context of the function
19364 template, not the context of the overload resolution we're doing. */
19365 push_to_top_level ();
19366 /* If there are dependent arguments, e.g. because we're doing partial
19367 ordering, make sure processing_template_decl stays set. */
19368 if (uses_template_parms (targ_ptr))
19369 ++processing_template_decl;
19370 if (DECL_CLASS_SCOPE_P (gen_tmpl))
19371 {
19372 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
19373 complain, gen_tmpl, true);
19374 push_nested_class (ctx);
19375 }
19376
19377 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
19378
19379 fndecl = NULL_TREE;
19380 if (VAR_P (pattern))
19381 {
19382 /* We need to determine if we're using a partial or explicit
19383 specialization now, because the type of the variable could be
19384 different. */
19385 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
19386 tree elt = most_specialized_partial_spec (tid, complain);
19387 if (elt == error_mark_node)
19388 pattern = error_mark_node;
19389 else if (elt)
19390 {
19391 tree partial_tmpl = TREE_VALUE (elt);
19392 tree partial_args = TREE_PURPOSE (elt);
19393 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
19394 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
19395 }
19396 }
19397
19398 /* Substitute template parameters to obtain the specialization. */
19399 if (fndecl == NULL_TREE)
19400 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
19401 if (DECL_CLASS_SCOPE_P (gen_tmpl))
19402 pop_nested_class ();
19403 pop_from_top_level ();
19404
19405 if (fndecl == error_mark_node)
19406 {
19407 pop_deferring_access_checks ();
19408 return error_mark_node;
19409 }
19410
19411 /* The DECL_TI_TEMPLATE should always be the immediate parent
19412 template, not the most general template. */
19413 DECL_TI_TEMPLATE (fndecl) = tmpl;
19414 DECL_TI_ARGS (fndecl) = targ_ptr;
19415
19416 /* Now we know the specialization, compute access previously
19417 deferred. Do no access control for inheriting constructors,
19418 as we already checked access for the inherited constructor. */
19419 if (!(flag_new_inheriting_ctors
19420 && DECL_INHERITED_CTOR (fndecl)))
19421 {
19422 push_access_scope (fndecl);
19423 if (!perform_deferred_access_checks (complain))
19424 access_ok = false;
19425 pop_access_scope (fndecl);
19426 }
19427 pop_deferring_access_checks ();
19428
19429 /* If we've just instantiated the main entry point for a function,
19430 instantiate all the alternate entry points as well. We do this
19431 by cloning the instantiation of the main entry point, not by
19432 instantiating the template clones. */
19433 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
19434 clone_function_decl (fndecl, /*update_methods=*/false);
19435
19436 if (!access_ok)
19437 {
19438 if (!(complain & tf_error))
19439 {
19440 /* Remember to reinstantiate when we're out of SFINAE so the user
19441 can see the errors. */
19442 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
19443 }
19444 return error_mark_node;
19445 }
19446 return fndecl;
19447 }
19448
19449 /* Wrapper for instantiate_template_1. */
19450
19451 tree
19452 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
19453 {
19454 tree ret;
19455 timevar_push (TV_TEMPLATE_INST);
19456 ret = instantiate_template_1 (tmpl, orig_args, complain);
19457 timevar_pop (TV_TEMPLATE_INST);
19458 return ret;
19459 }
19460
19461 /* Instantiate the alias template TMPL with ARGS. Also push a template
19462 instantiation level, which instantiate_template doesn't do because
19463 functions and variables have sufficient context established by the
19464 callers. */
19465
19466 static tree
19467 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
19468 {
19469 if (tmpl == error_mark_node || args == error_mark_node)
19470 return error_mark_node;
19471 if (!push_tinst_level (tmpl, args))
19472 return error_mark_node;
19473
19474 args =
19475 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
19476 args, tmpl, complain,
19477 /*require_all_args=*/true,
19478 /*use_default_args=*/true);
19479
19480 tree r = instantiate_template (tmpl, args, complain);
19481 pop_tinst_level ();
19482
19483 return r;
19484 }
19485
19486 /* PARM is a template parameter pack for FN. Returns true iff
19487 PARM is used in a deducible way in the argument list of FN. */
19488
19489 static bool
19490 pack_deducible_p (tree parm, tree fn)
19491 {
19492 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
19493 for (; t; t = TREE_CHAIN (t))
19494 {
19495 tree type = TREE_VALUE (t);
19496 tree packs;
19497 if (!PACK_EXPANSION_P (type))
19498 continue;
19499 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
19500 packs; packs = TREE_CHAIN (packs))
19501 if (template_args_equal (TREE_VALUE (packs), parm))
19502 {
19503 /* The template parameter pack is used in a function parameter
19504 pack. If this is the end of the parameter list, the
19505 template parameter pack is deducible. */
19506 if (TREE_CHAIN (t) == void_list_node)
19507 return true;
19508 else
19509 /* Otherwise, not. Well, it could be deduced from
19510 a non-pack parameter, but doing so would end up with
19511 a deduction mismatch, so don't bother. */
19512 return false;
19513 }
19514 }
19515 /* The template parameter pack isn't used in any function parameter
19516 packs, but it might be used deeper, e.g. tuple<Args...>. */
19517 return true;
19518 }
19519
19520 /* Subroutine of fn_type_unification: check non-dependent parms for
19521 convertibility. */
19522
19523 static int
19524 check_non_deducible_conversions (tree parms, const tree *args, unsigned nargs,
19525 tree fn, unification_kind_t strict, int flags,
19526 struct conversion **convs, bool explain_p)
19527 {
19528 /* Non-constructor methods need to leave a conversion for 'this', which
19529 isn't included in nargs here. */
19530 unsigned offset = (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
19531 && !DECL_CONSTRUCTOR_P (fn));
19532
19533 for (unsigned ia = 0;
19534 parms && parms != void_list_node && ia < nargs; )
19535 {
19536 tree parm = TREE_VALUE (parms);
19537
19538 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19539 && (!TREE_CHAIN (parms)
19540 || TREE_CHAIN (parms) == void_list_node))
19541 /* For a function parameter pack that occurs at the end of the
19542 parameter-declaration-list, the type A of each remaining
19543 argument of the call is compared with the type P of the
19544 declarator-id of the function parameter pack. */
19545 break;
19546
19547 parms = TREE_CHAIN (parms);
19548
19549 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19550 /* For a function parameter pack that does not occur at the
19551 end of the parameter-declaration-list, the type of the
19552 parameter pack is a non-deduced context. */
19553 continue;
19554
19555 if (!uses_template_parms (parm))
19556 {
19557 tree arg = args[ia];
19558 conversion **conv_p = convs ? &convs[ia+offset] : NULL;
19559 int lflags = conv_flags (ia, nargs, fn, arg, flags);
19560
19561 if (check_non_deducible_conversion (parm, arg, strict, lflags,
19562 conv_p, explain_p))
19563 return 1;
19564 }
19565
19566 ++ia;
19567 }
19568
19569 return 0;
19570 }
19571
19572 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
19573 NARGS elements of the arguments that are being used when calling
19574 it. TARGS is a vector into which the deduced template arguments
19575 are placed.
19576
19577 Returns either a FUNCTION_DECL for the matching specialization of FN or
19578 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
19579 true, diagnostics will be printed to explain why it failed.
19580
19581 If FN is a conversion operator, or we are trying to produce a specific
19582 specialization, RETURN_TYPE is the return type desired.
19583
19584 The EXPLICIT_TARGS are explicit template arguments provided via a
19585 template-id.
19586
19587 The parameter STRICT is one of:
19588
19589 DEDUCE_CALL:
19590 We are deducing arguments for a function call, as in
19591 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
19592 deducing arguments for a call to the result of a conversion
19593 function template, as in [over.call.object].
19594
19595 DEDUCE_CONV:
19596 We are deducing arguments for a conversion function, as in
19597 [temp.deduct.conv].
19598
19599 DEDUCE_EXACT:
19600 We are deducing arguments when doing an explicit instantiation
19601 as in [temp.explicit], when determining an explicit specialization
19602 as in [temp.expl.spec], or when taking the address of a function
19603 template, as in [temp.deduct.funcaddr]. */
19604
19605 tree
19606 fn_type_unification (tree fn,
19607 tree explicit_targs,
19608 tree targs,
19609 const tree *args,
19610 unsigned int nargs,
19611 tree return_type,
19612 unification_kind_t strict,
19613 int flags,
19614 struct conversion **convs,
19615 bool explain_p,
19616 bool decltype_p)
19617 {
19618 tree parms;
19619 tree fntype;
19620 tree decl = NULL_TREE;
19621 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
19622 bool ok;
19623 static int deduction_depth;
19624
19625 tree orig_fn = fn;
19626 if (flag_new_inheriting_ctors)
19627 fn = strip_inheriting_ctors (fn);
19628
19629 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
19630 tree r = error_mark_node;
19631
19632 tree full_targs = targs;
19633 if (TMPL_ARGS_DEPTH (targs)
19634 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
19635 full_targs = (add_outermost_template_args
19636 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
19637 targs));
19638
19639 if (decltype_p)
19640 complain |= tf_decltype;
19641
19642 /* In C++0x, it's possible to have a function template whose type depends
19643 on itself recursively. This is most obvious with decltype, but can also
19644 occur with enumeration scope (c++/48969). So we need to catch infinite
19645 recursion and reject the substitution at deduction time; this function
19646 will return error_mark_node for any repeated substitution.
19647
19648 This also catches excessive recursion such as when f<N> depends on
19649 f<N-1> across all integers, and returns error_mark_node for all the
19650 substitutions back up to the initial one.
19651
19652 This is, of course, not reentrant. */
19653 if (excessive_deduction_depth)
19654 return error_mark_node;
19655 ++deduction_depth;
19656
19657 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
19658
19659 fntype = TREE_TYPE (fn);
19660 if (explicit_targs)
19661 {
19662 /* [temp.deduct]
19663
19664 The specified template arguments must match the template
19665 parameters in kind (i.e., type, nontype, template), and there
19666 must not be more arguments than there are parameters;
19667 otherwise type deduction fails.
19668
19669 Nontype arguments must match the types of the corresponding
19670 nontype template parameters, or must be convertible to the
19671 types of the corresponding nontype parameters as specified in
19672 _temp.arg.nontype_, otherwise type deduction fails.
19673
19674 All references in the function type of the function template
19675 to the corresponding template parameters are replaced by the
19676 specified template argument values. If a substitution in a
19677 template parameter or in the function type of the function
19678 template results in an invalid type, type deduction fails. */
19679 int i, len = TREE_VEC_LENGTH (tparms);
19680 location_t loc = input_location;
19681 bool incomplete = false;
19682
19683 if (explicit_targs == error_mark_node)
19684 goto fail;
19685
19686 if (TMPL_ARGS_DEPTH (explicit_targs)
19687 < TMPL_ARGS_DEPTH (full_targs))
19688 explicit_targs = add_outermost_template_args (full_targs,
19689 explicit_targs);
19690
19691 /* Adjust any explicit template arguments before entering the
19692 substitution context. */
19693 explicit_targs
19694 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
19695 complain,
19696 /*require_all_args=*/false,
19697 /*use_default_args=*/false));
19698 if (explicit_targs == error_mark_node)
19699 goto fail;
19700
19701 /* Substitute the explicit args into the function type. This is
19702 necessary so that, for instance, explicitly declared function
19703 arguments can match null pointed constants. If we were given
19704 an incomplete set of explicit args, we must not do semantic
19705 processing during substitution as we could create partial
19706 instantiations. */
19707 for (i = 0; i < len; i++)
19708 {
19709 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
19710 bool parameter_pack = false;
19711 tree targ = TREE_VEC_ELT (explicit_targs, i);
19712
19713 /* Dig out the actual parm. */
19714 if (TREE_CODE (parm) == TYPE_DECL
19715 || TREE_CODE (parm) == TEMPLATE_DECL)
19716 {
19717 parm = TREE_TYPE (parm);
19718 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
19719 }
19720 else if (TREE_CODE (parm) == PARM_DECL)
19721 {
19722 parm = DECL_INITIAL (parm);
19723 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
19724 }
19725
19726 if (!parameter_pack && targ == NULL_TREE)
19727 /* No explicit argument for this template parameter. */
19728 incomplete = true;
19729
19730 if (parameter_pack && pack_deducible_p (parm, fn))
19731 {
19732 /* Mark the argument pack as "incomplete". We could
19733 still deduce more arguments during unification.
19734 We remove this mark in type_unification_real. */
19735 if (targ)
19736 {
19737 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
19738 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
19739 = ARGUMENT_PACK_ARGS (targ);
19740 }
19741
19742 /* We have some incomplete argument packs. */
19743 incomplete = true;
19744 }
19745 }
19746
19747 if (!push_tinst_level (fn, explicit_targs))
19748 {
19749 excessive_deduction_depth = true;
19750 goto fail;
19751 }
19752 processing_template_decl += incomplete;
19753 input_location = DECL_SOURCE_LOCATION (fn);
19754 /* Ignore any access checks; we'll see them again in
19755 instantiate_template and they might have the wrong
19756 access path at this point. */
19757 push_deferring_access_checks (dk_deferred);
19758 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
19759 complain | tf_partial | tf_fndecl_type, NULL_TREE);
19760 pop_deferring_access_checks ();
19761 input_location = loc;
19762 processing_template_decl -= incomplete;
19763 pop_tinst_level ();
19764
19765 if (fntype == error_mark_node)
19766 goto fail;
19767
19768 /* Place the explicitly specified arguments in TARGS. */
19769 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
19770 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
19771 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
19772 }
19773
19774 /* Never do unification on the 'this' parameter. */
19775 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
19776
19777 if (return_type && strict == DEDUCE_CALL)
19778 {
19779 /* We're deducing for a call to the result of a template conversion
19780 function. The parms we really want are in return_type. */
19781 if (INDIRECT_TYPE_P (return_type))
19782 return_type = TREE_TYPE (return_type);
19783 parms = TYPE_ARG_TYPES (return_type);
19784 }
19785 else if (return_type)
19786 {
19787 tree *new_args;
19788
19789 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
19790 new_args = XALLOCAVEC (tree, nargs + 1);
19791 new_args[0] = return_type;
19792 memcpy (new_args + 1, args, nargs * sizeof (tree));
19793 args = new_args;
19794 ++nargs;
19795 }
19796
19797 /* We allow incomplete unification without an error message here
19798 because the standard doesn't seem to explicitly prohibit it. Our
19799 callers must be ready to deal with unification failures in any
19800 event. */
19801
19802 /* If we aren't explaining yet, push tinst context so we can see where
19803 any errors (e.g. from class instantiations triggered by instantiation
19804 of default template arguments) come from. If we are explaining, this
19805 context is redundant. */
19806 if (!explain_p && !push_tinst_level (fn, targs))
19807 {
19808 excessive_deduction_depth = true;
19809 goto fail;
19810 }
19811
19812 /* type_unification_real will pass back any access checks from default
19813 template argument substitution. */
19814 vec<deferred_access_check, va_gc> *checks;
19815 checks = NULL;
19816
19817 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19818 full_targs, parms, args, nargs, /*subr=*/0,
19819 strict, &checks, explain_p);
19820 if (!explain_p)
19821 pop_tinst_level ();
19822 if (!ok)
19823 goto fail;
19824
19825 /* Now that we have bindings for all of the template arguments,
19826 ensure that the arguments deduced for the template template
19827 parameters have compatible template parameter lists. We cannot
19828 check this property before we have deduced all template
19829 arguments, because the template parameter types of a template
19830 template parameter might depend on prior template parameters
19831 deduced after the template template parameter. The following
19832 ill-formed example illustrates this issue:
19833
19834 template<typename T, template<T> class C> void f(C<5>, T);
19835
19836 template<int N> struct X {};
19837
19838 void g() {
19839 f(X<5>(), 5l); // error: template argument deduction fails
19840 }
19841
19842 The template parameter list of 'C' depends on the template type
19843 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
19844 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
19845 time that we deduce 'C'. */
19846 if (!template_template_parm_bindings_ok_p
19847 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
19848 {
19849 unify_inconsistent_template_template_parameters (explain_p);
19850 goto fail;
19851 }
19852
19853 /* DR 1391: All parameters have args, now check non-dependent parms for
19854 convertibility. */
19855 if (check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
19856 convs, explain_p))
19857 goto fail;
19858
19859 /* All is well so far. Now, check:
19860
19861 [temp.deduct]
19862
19863 When all template arguments have been deduced, all uses of
19864 template parameters in nondeduced contexts are replaced with
19865 the corresponding deduced argument values. If the
19866 substitution results in an invalid type, as described above,
19867 type deduction fails. */
19868 if (!push_tinst_level (fn, targs))
19869 {
19870 excessive_deduction_depth = true;
19871 goto fail;
19872 }
19873
19874 /* Also collect access checks from the instantiation. */
19875 reopen_deferring_access_checks (checks);
19876
19877 decl = instantiate_template (fn, targs, complain);
19878
19879 checks = get_deferred_access_checks ();
19880 pop_deferring_access_checks ();
19881
19882 pop_tinst_level ();
19883
19884 if (decl == error_mark_node)
19885 goto fail;
19886
19887 /* Now perform any access checks encountered during substitution. */
19888 push_access_scope (decl);
19889 ok = perform_access_checks (checks, complain);
19890 pop_access_scope (decl);
19891 if (!ok)
19892 goto fail;
19893
19894 /* If we're looking for an exact match, check that what we got
19895 is indeed an exact match. It might not be if some template
19896 parameters are used in non-deduced contexts. But don't check
19897 for an exact match if we have dependent template arguments;
19898 in that case we're doing partial ordering, and we already know
19899 that we have two candidates that will provide the actual type. */
19900 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
19901 {
19902 tree substed = TREE_TYPE (decl);
19903 unsigned int i;
19904
19905 tree sarg
19906 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
19907 if (return_type)
19908 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
19909 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
19910 if (!same_type_p (args[i], TREE_VALUE (sarg)))
19911 {
19912 unify_type_mismatch (explain_p, args[i],
19913 TREE_VALUE (sarg));
19914 goto fail;
19915 }
19916 }
19917
19918 /* After doing deduction with the inherited constructor, actually return an
19919 instantiation of the inheriting constructor. */
19920 if (orig_fn != fn)
19921 decl = instantiate_template (orig_fn, targs, complain);
19922
19923 r = decl;
19924
19925 fail:
19926 --deduction_depth;
19927 if (excessive_deduction_depth)
19928 {
19929 if (deduction_depth == 0)
19930 /* Reset once we're all the way out. */
19931 excessive_deduction_depth = false;
19932 }
19933
19934 return r;
19935 }
19936
19937 /* Adjust types before performing type deduction, as described in
19938 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
19939 sections are symmetric. PARM is the type of a function parameter
19940 or the return type of the conversion function. ARG is the type of
19941 the argument passed to the call, or the type of the value
19942 initialized with the result of the conversion function.
19943 ARG_EXPR is the original argument expression, which may be null. */
19944
19945 static int
19946 maybe_adjust_types_for_deduction (unification_kind_t strict,
19947 tree* parm,
19948 tree* arg,
19949 tree arg_expr)
19950 {
19951 int result = 0;
19952
19953 switch (strict)
19954 {
19955 case DEDUCE_CALL:
19956 break;
19957
19958 case DEDUCE_CONV:
19959 /* Swap PARM and ARG throughout the remainder of this
19960 function; the handling is precisely symmetric since PARM
19961 will initialize ARG rather than vice versa. */
19962 std::swap (parm, arg);
19963 break;
19964
19965 case DEDUCE_EXACT:
19966 /* Core issue #873: Do the DR606 thing (see below) for these cases,
19967 too, but here handle it by stripping the reference from PARM
19968 rather than by adding it to ARG. */
19969 if (TYPE_REF_P (*parm)
19970 && TYPE_REF_IS_RVALUE (*parm)
19971 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19972 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19973 && TYPE_REF_P (*arg)
19974 && !TYPE_REF_IS_RVALUE (*arg))
19975 *parm = TREE_TYPE (*parm);
19976 /* Nothing else to do in this case. */
19977 return 0;
19978
19979 default:
19980 gcc_unreachable ();
19981 }
19982
19983 if (!TYPE_REF_P (*parm))
19984 {
19985 /* [temp.deduct.call]
19986
19987 If P is not a reference type:
19988
19989 --If A is an array type, the pointer type produced by the
19990 array-to-pointer standard conversion (_conv.array_) is
19991 used in place of A for type deduction; otherwise,
19992
19993 --If A is a function type, the pointer type produced by
19994 the function-to-pointer standard conversion
19995 (_conv.func_) is used in place of A for type deduction;
19996 otherwise,
19997
19998 --If A is a cv-qualified type, the top level
19999 cv-qualifiers of A's type are ignored for type
20000 deduction. */
20001 if (TREE_CODE (*arg) == ARRAY_TYPE)
20002 *arg = build_pointer_type (TREE_TYPE (*arg));
20003 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
20004 *arg = build_pointer_type (*arg);
20005 else
20006 *arg = TYPE_MAIN_VARIANT (*arg);
20007 }
20008
20009 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
20010 reference to a cv-unqualified template parameter that does not represent a
20011 template parameter of a class template (during class template argument
20012 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
20013 an lvalue, the type "lvalue reference to A" is used in place of A for type
20014 deduction. */
20015 if (TYPE_REF_P (*parm)
20016 && TYPE_REF_IS_RVALUE (*parm)
20017 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
20018 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
20019 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
20020 && (arg_expr ? lvalue_p (arg_expr)
20021 /* try_one_overload doesn't provide an arg_expr, but
20022 functions are always lvalues. */
20023 : TREE_CODE (*arg) == FUNCTION_TYPE))
20024 *arg = build_reference_type (*arg);
20025
20026 /* [temp.deduct.call]
20027
20028 If P is a cv-qualified type, the top level cv-qualifiers
20029 of P's type are ignored for type deduction. If P is a
20030 reference type, the type referred to by P is used for
20031 type deduction. */
20032 *parm = TYPE_MAIN_VARIANT (*parm);
20033 if (TYPE_REF_P (*parm))
20034 {
20035 *parm = TREE_TYPE (*parm);
20036 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
20037 }
20038
20039 /* DR 322. For conversion deduction, remove a reference type on parm
20040 too (which has been swapped into ARG). */
20041 if (strict == DEDUCE_CONV && TYPE_REF_P (*arg))
20042 *arg = TREE_TYPE (*arg);
20043
20044 return result;
20045 }
20046
20047 /* Subroutine of fn_type_unification. PARM is a function parameter of a
20048 template which doesn't contain any deducible template parameters; check if
20049 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
20050 unify_one_argument. */
20051
20052 static int
20053 check_non_deducible_conversion (tree parm, tree arg, int strict,
20054 int flags, struct conversion **conv_p,
20055 bool explain_p)
20056 {
20057 tree type;
20058
20059 if (!TYPE_P (arg))
20060 type = TREE_TYPE (arg);
20061 else
20062 type = arg;
20063
20064 if (same_type_p (parm, type))
20065 return unify_success (explain_p);
20066
20067 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
20068 if (strict == DEDUCE_CONV)
20069 {
20070 if (can_convert_arg (type, parm, NULL_TREE, flags, complain))
20071 return unify_success (explain_p);
20072 }
20073 else if (strict != DEDUCE_EXACT)
20074 {
20075 bool ok = false;
20076 tree conv_arg = TYPE_P (arg) ? NULL_TREE : arg;
20077 if (conv_p)
20078 /* Avoid recalculating this in add_function_candidate. */
20079 ok = (*conv_p
20080 = good_conversion (parm, type, conv_arg, flags, complain));
20081 else
20082 ok = can_convert_arg (parm, type, conv_arg, flags, complain);
20083 if (ok)
20084 return unify_success (explain_p);
20085 }
20086
20087 if (strict == DEDUCE_EXACT)
20088 return unify_type_mismatch (explain_p, parm, arg);
20089 else
20090 return unify_arg_conversion (explain_p, parm, type, arg);
20091 }
20092
20093 static bool uses_deducible_template_parms (tree type);
20094
20095 /* Returns true iff the expression EXPR is one from which a template
20096 argument can be deduced. In other words, if it's an undecorated
20097 use of a template non-type parameter. */
20098
20099 static bool
20100 deducible_expression (tree expr)
20101 {
20102 /* Strip implicit conversions. */
20103 while (CONVERT_EXPR_P (expr))
20104 expr = TREE_OPERAND (expr, 0);
20105 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
20106 }
20107
20108 /* Returns true iff the array domain DOMAIN uses a template parameter in a
20109 deducible way; that is, if it has a max value of <PARM> - 1. */
20110
20111 static bool
20112 deducible_array_bound (tree domain)
20113 {
20114 if (domain == NULL_TREE)
20115 return false;
20116
20117 tree max = TYPE_MAX_VALUE (domain);
20118 if (TREE_CODE (max) != MINUS_EXPR)
20119 return false;
20120
20121 return deducible_expression (TREE_OPERAND (max, 0));
20122 }
20123
20124 /* Returns true iff the template arguments ARGS use a template parameter
20125 in a deducible way. */
20126
20127 static bool
20128 deducible_template_args (tree args)
20129 {
20130 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
20131 {
20132 bool deducible;
20133 tree elt = TREE_VEC_ELT (args, i);
20134 if (ARGUMENT_PACK_P (elt))
20135 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
20136 else
20137 {
20138 if (PACK_EXPANSION_P (elt))
20139 elt = PACK_EXPANSION_PATTERN (elt);
20140 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
20141 deducible = true;
20142 else if (TYPE_P (elt))
20143 deducible = uses_deducible_template_parms (elt);
20144 else
20145 deducible = deducible_expression (elt);
20146 }
20147 if (deducible)
20148 return true;
20149 }
20150 return false;
20151 }
20152
20153 /* Returns true iff TYPE contains any deducible references to template
20154 parameters, as per 14.8.2.5. */
20155
20156 static bool
20157 uses_deducible_template_parms (tree type)
20158 {
20159 if (PACK_EXPANSION_P (type))
20160 type = PACK_EXPANSION_PATTERN (type);
20161
20162 /* T
20163 cv-list T
20164 TT<T>
20165 TT<i>
20166 TT<> */
20167 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
20168 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
20169 return true;
20170
20171 /* T*
20172 T&
20173 T&& */
20174 if (INDIRECT_TYPE_P (type))
20175 return uses_deducible_template_parms (TREE_TYPE (type));
20176
20177 /* T[integer-constant ]
20178 type [i] */
20179 if (TREE_CODE (type) == ARRAY_TYPE)
20180 return (uses_deducible_template_parms (TREE_TYPE (type))
20181 || deducible_array_bound (TYPE_DOMAIN (type)));
20182
20183 /* T type ::*
20184 type T::*
20185 T T::*
20186 T (type ::*)()
20187 type (T::*)()
20188 type (type ::*)(T)
20189 type (T::*)(T)
20190 T (type ::*)(T)
20191 T (T::*)()
20192 T (T::*)(T) */
20193 if (TYPE_PTRMEM_P (type))
20194 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
20195 || (uses_deducible_template_parms
20196 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
20197
20198 /* template-name <T> (where template-name refers to a class template)
20199 template-name <i> (where template-name refers to a class template) */
20200 if (CLASS_TYPE_P (type)
20201 && CLASSTYPE_TEMPLATE_INFO (type)
20202 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
20203 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
20204 (CLASSTYPE_TI_ARGS (type)));
20205
20206 /* type (T)
20207 T()
20208 T(T) */
20209 if (TREE_CODE (type) == FUNCTION_TYPE
20210 || TREE_CODE (type) == METHOD_TYPE)
20211 {
20212 if (uses_deducible_template_parms (TREE_TYPE (type)))
20213 return true;
20214 tree parm = TYPE_ARG_TYPES (type);
20215 if (TREE_CODE (type) == METHOD_TYPE)
20216 parm = TREE_CHAIN (parm);
20217 for (; parm; parm = TREE_CHAIN (parm))
20218 if (uses_deducible_template_parms (TREE_VALUE (parm)))
20219 return true;
20220 }
20221
20222 return false;
20223 }
20224
20225 /* Subroutine of type_unification_real and unify_pack_expansion to
20226 handle unification of a single P/A pair. Parameters are as
20227 for those functions. */
20228
20229 static int
20230 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
20231 int subr, unification_kind_t strict,
20232 bool explain_p)
20233 {
20234 tree arg_expr = NULL_TREE;
20235 int arg_strict;
20236
20237 if (arg == error_mark_node || parm == error_mark_node)
20238 return unify_invalid (explain_p);
20239 if (arg == unknown_type_node)
20240 /* We can't deduce anything from this, but we might get all the
20241 template args from other function args. */
20242 return unify_success (explain_p);
20243
20244 /* Implicit conversions (Clause 4) will be performed on a function
20245 argument to convert it to the type of the corresponding function
20246 parameter if the parameter type contains no template-parameters that
20247 participate in template argument deduction. */
20248 if (strict != DEDUCE_EXACT
20249 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
20250 /* For function parameters with no deducible template parameters,
20251 just return. We'll check non-dependent conversions later. */
20252 return unify_success (explain_p);
20253
20254 switch (strict)
20255 {
20256 case DEDUCE_CALL:
20257 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
20258 | UNIFY_ALLOW_MORE_CV_QUAL
20259 | UNIFY_ALLOW_DERIVED);
20260 break;
20261
20262 case DEDUCE_CONV:
20263 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
20264 break;
20265
20266 case DEDUCE_EXACT:
20267 arg_strict = UNIFY_ALLOW_NONE;
20268 break;
20269
20270 default:
20271 gcc_unreachable ();
20272 }
20273
20274 /* We only do these transformations if this is the top-level
20275 parameter_type_list in a call or declaration matching; in other
20276 situations (nested function declarators, template argument lists) we
20277 won't be comparing a type to an expression, and we don't do any type
20278 adjustments. */
20279 if (!subr)
20280 {
20281 if (!TYPE_P (arg))
20282 {
20283 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
20284 if (type_unknown_p (arg))
20285 {
20286 /* [temp.deduct.type] A template-argument can be
20287 deduced from a pointer to function or pointer
20288 to member function argument if the set of
20289 overloaded functions does not contain function
20290 templates and at most one of a set of
20291 overloaded functions provides a unique
20292 match. */
20293 resolve_overloaded_unification (tparms, targs, parm,
20294 arg, strict,
20295 arg_strict, explain_p);
20296 /* If a unique match was not found, this is a
20297 non-deduced context, so we still succeed. */
20298 return unify_success (explain_p);
20299 }
20300
20301 arg_expr = arg;
20302 arg = unlowered_expr_type (arg);
20303 if (arg == error_mark_node)
20304 return unify_invalid (explain_p);
20305 }
20306
20307 arg_strict |=
20308 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
20309 }
20310 else
20311 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
20312 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
20313 return unify_template_argument_mismatch (explain_p, parm, arg);
20314
20315 /* For deduction from an init-list we need the actual list. */
20316 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
20317 arg = arg_expr;
20318 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
20319 }
20320
20321 /* for_each_template_parm callback that always returns 0. */
20322
20323 static int
20324 zero_r (tree, void *)
20325 {
20326 return 0;
20327 }
20328
20329 /* for_each_template_parm any_fn callback to handle deduction of a template
20330 type argument from the type of an array bound. */
20331
20332 static int
20333 array_deduction_r (tree t, void *data)
20334 {
20335 tree_pair_p d = (tree_pair_p)data;
20336 tree &tparms = d->purpose;
20337 tree &targs = d->value;
20338
20339 if (TREE_CODE (t) == ARRAY_TYPE)
20340 if (tree dom = TYPE_DOMAIN (t))
20341 if (tree max = TYPE_MAX_VALUE (dom))
20342 {
20343 if (TREE_CODE (max) == MINUS_EXPR)
20344 max = TREE_OPERAND (max, 0);
20345 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
20346 unify (tparms, targs, TREE_TYPE (max), size_type_node,
20347 UNIFY_ALLOW_NONE, /*explain*/false);
20348 }
20349
20350 /* Keep walking. */
20351 return 0;
20352 }
20353
20354 /* Try to deduce any not-yet-deduced template type arguments from the type of
20355 an array bound. This is handled separately from unify because 14.8.2.5 says
20356 "The type of a type parameter is only deduced from an array bound if it is
20357 not otherwise deduced." */
20358
20359 static void
20360 try_array_deduction (tree tparms, tree targs, tree parm)
20361 {
20362 tree_pair_s data = { tparms, targs };
20363 hash_set<tree> visited;
20364 for_each_template_parm (parm, zero_r, &data, &visited,
20365 /*nondeduced*/false, array_deduction_r);
20366 }
20367
20368 /* Most parms like fn_type_unification.
20369
20370 If SUBR is 1, we're being called recursively (to unify the
20371 arguments of a function or method parameter of a function
20372 template).
20373
20374 CHECKS is a pointer to a vector of access checks encountered while
20375 substituting default template arguments. */
20376
20377 static int
20378 type_unification_real (tree tparms,
20379 tree full_targs,
20380 tree xparms,
20381 const tree *xargs,
20382 unsigned int xnargs,
20383 int subr,
20384 unification_kind_t strict,
20385 vec<deferred_access_check, va_gc> **checks,
20386 bool explain_p)
20387 {
20388 tree parm, arg;
20389 int i;
20390 int ntparms = TREE_VEC_LENGTH (tparms);
20391 int saw_undeduced = 0;
20392 tree parms;
20393 const tree *args;
20394 unsigned int nargs;
20395 unsigned int ia;
20396
20397 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
20398 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
20399 gcc_assert (ntparms > 0);
20400
20401 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
20402
20403 /* Reset the number of non-defaulted template arguments contained
20404 in TARGS. */
20405 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
20406
20407 again:
20408 parms = xparms;
20409 args = xargs;
20410 nargs = xnargs;
20411
20412 ia = 0;
20413 while (parms && parms != void_list_node
20414 && ia < nargs)
20415 {
20416 parm = TREE_VALUE (parms);
20417
20418 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
20419 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
20420 /* For a function parameter pack that occurs at the end of the
20421 parameter-declaration-list, the type A of each remaining
20422 argument of the call is compared with the type P of the
20423 declarator-id of the function parameter pack. */
20424 break;
20425
20426 parms = TREE_CHAIN (parms);
20427
20428 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
20429 /* For a function parameter pack that does not occur at the
20430 end of the parameter-declaration-list, the type of the
20431 parameter pack is a non-deduced context. */
20432 continue;
20433
20434 arg = args[ia];
20435 ++ia;
20436
20437 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
20438 explain_p))
20439 return 1;
20440 }
20441
20442 if (parms
20443 && parms != void_list_node
20444 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
20445 {
20446 /* Unify the remaining arguments with the pack expansion type. */
20447 tree argvec;
20448 tree parmvec = make_tree_vec (1);
20449
20450 /* Allocate a TREE_VEC and copy in all of the arguments */
20451 argvec = make_tree_vec (nargs - ia);
20452 for (i = 0; ia < nargs; ++ia, ++i)
20453 TREE_VEC_ELT (argvec, i) = args[ia];
20454
20455 /* Copy the parameter into parmvec. */
20456 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
20457 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
20458 /*subr=*/subr, explain_p))
20459 return 1;
20460
20461 /* Advance to the end of the list of parameters. */
20462 parms = TREE_CHAIN (parms);
20463 }
20464
20465 /* Fail if we've reached the end of the parm list, and more args
20466 are present, and the parm list isn't variadic. */
20467 if (ia < nargs && parms == void_list_node)
20468 return unify_too_many_arguments (explain_p, nargs, ia);
20469 /* Fail if parms are left and they don't have default values and
20470 they aren't all deduced as empty packs (c++/57397). This is
20471 consistent with sufficient_parms_p. */
20472 if (parms && parms != void_list_node
20473 && TREE_PURPOSE (parms) == NULL_TREE)
20474 {
20475 unsigned int count = nargs;
20476 tree p = parms;
20477 bool type_pack_p;
20478 do
20479 {
20480 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
20481 if (!type_pack_p)
20482 count++;
20483 p = TREE_CHAIN (p);
20484 }
20485 while (p && p != void_list_node);
20486 if (count != nargs)
20487 return unify_too_few_arguments (explain_p, ia, count,
20488 type_pack_p);
20489 }
20490
20491 if (!subr)
20492 {
20493 tsubst_flags_t complain = (explain_p
20494 ? tf_warning_or_error
20495 : tf_none);
20496 bool tried_array_deduction = (cxx_dialect < cxx17);
20497
20498 for (i = 0; i < ntparms; i++)
20499 {
20500 tree targ = TREE_VEC_ELT (targs, i);
20501 tree tparm = TREE_VEC_ELT (tparms, i);
20502
20503 /* Clear the "incomplete" flags on all argument packs now so that
20504 substituting them into later default arguments works. */
20505 if (targ && ARGUMENT_PACK_P (targ))
20506 {
20507 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
20508 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
20509 }
20510
20511 if (targ || tparm == error_mark_node)
20512 continue;
20513 tparm = TREE_VALUE (tparm);
20514
20515 if (TREE_CODE (tparm) == TYPE_DECL
20516 && !tried_array_deduction)
20517 {
20518 try_array_deduction (tparms, targs, xparms);
20519 tried_array_deduction = true;
20520 if (TREE_VEC_ELT (targs, i))
20521 continue;
20522 }
20523
20524 /* If this is an undeduced nontype parameter that depends on
20525 a type parameter, try another pass; its type may have been
20526 deduced from a later argument than the one from which
20527 this parameter can be deduced. */
20528 if (TREE_CODE (tparm) == PARM_DECL
20529 && uses_template_parms (TREE_TYPE (tparm))
20530 && saw_undeduced < 2)
20531 {
20532 saw_undeduced = 1;
20533 continue;
20534 }
20535
20536 /* Core issue #226 (C++0x) [temp.deduct]:
20537
20538 If a template argument has not been deduced, its
20539 default template argument, if any, is used.
20540
20541 When we are in C++98 mode, TREE_PURPOSE will either
20542 be NULL_TREE or ERROR_MARK_NODE, so we do not need
20543 to explicitly check cxx_dialect here. */
20544 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
20545 /* OK, there is a default argument. Wait until after the
20546 conversion check to do substitution. */
20547 continue;
20548
20549 /* If the type parameter is a parameter pack, then it will
20550 be deduced to an empty parameter pack. */
20551 if (template_parameter_pack_p (tparm))
20552 {
20553 tree arg;
20554
20555 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
20556 {
20557 arg = make_node (NONTYPE_ARGUMENT_PACK);
20558 TREE_CONSTANT (arg) = 1;
20559 }
20560 else
20561 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
20562
20563 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
20564
20565 TREE_VEC_ELT (targs, i) = arg;
20566 continue;
20567 }
20568
20569 return unify_parameter_deduction_failure (explain_p, tparm);
20570 }
20571
20572 /* Now substitute into the default template arguments. */
20573 for (i = 0; i < ntparms; i++)
20574 {
20575 tree targ = TREE_VEC_ELT (targs, i);
20576 tree tparm = TREE_VEC_ELT (tparms, i);
20577
20578 if (targ || tparm == error_mark_node)
20579 continue;
20580 tree parm = TREE_VALUE (tparm);
20581 tree arg = TREE_PURPOSE (tparm);
20582 reopen_deferring_access_checks (*checks);
20583 location_t save_loc = input_location;
20584 if (DECL_P (parm))
20585 input_location = DECL_SOURCE_LOCATION (parm);
20586
20587 if (saw_undeduced == 1
20588 && TREE_CODE (parm) == PARM_DECL
20589 && uses_template_parms (TREE_TYPE (parm)))
20590 {
20591 /* The type of this non-type parameter depends on undeduced
20592 parameters. Don't try to use its default argument yet,
20593 since we might deduce an argument for it on the next pass,
20594 but do check whether the arguments we already have cause
20595 substitution failure, so that that happens before we try
20596 later default arguments (78489). */
20597 ++processing_template_decl;
20598 tree type = tsubst (TREE_TYPE (parm), full_targs, complain,
20599 NULL_TREE);
20600 --processing_template_decl;
20601 if (type == error_mark_node)
20602 arg = error_mark_node;
20603 else
20604 arg = NULL_TREE;
20605 }
20606 else
20607 {
20608 tree substed = NULL_TREE;
20609 if (saw_undeduced == 1 && processing_template_decl == 0)
20610 {
20611 /* First instatiate in template context, in case we still
20612 depend on undeduced template parameters. */
20613 ++processing_template_decl;
20614 substed = tsubst_template_arg (arg, full_targs, complain,
20615 NULL_TREE);
20616 --processing_template_decl;
20617 if (substed != error_mark_node
20618 && !uses_template_parms (substed))
20619 /* We replaced all the tparms, substitute again out of
20620 template context. */
20621 substed = NULL_TREE;
20622 }
20623 if (!substed)
20624 substed = tsubst_template_arg (arg, full_targs, complain,
20625 NULL_TREE);
20626
20627 if (!uses_template_parms (substed))
20628 arg = convert_template_argument (parm, substed, full_targs,
20629 complain, i, NULL_TREE);
20630 else if (saw_undeduced == 1)
20631 arg = NULL_TREE;
20632 else
20633 arg = error_mark_node;
20634 }
20635
20636 input_location = save_loc;
20637 *checks = get_deferred_access_checks ();
20638 pop_deferring_access_checks ();
20639
20640 if (arg == error_mark_node)
20641 return 1;
20642 else if (arg)
20643 {
20644 TREE_VEC_ELT (targs, i) = arg;
20645 /* The position of the first default template argument,
20646 is also the number of non-defaulted arguments in TARGS.
20647 Record that. */
20648 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
20649 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
20650 }
20651 }
20652
20653 if (saw_undeduced++ == 1)
20654 goto again;
20655 }
20656
20657 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
20658 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
20659
20660 return unify_success (explain_p);
20661 }
20662
20663 /* Subroutine of type_unification_real. Args are like the variables
20664 at the call site. ARG is an overloaded function (or template-id);
20665 we try deducing template args from each of the overloads, and if
20666 only one succeeds, we go with that. Modifies TARGS and returns
20667 true on success. */
20668
20669 static bool
20670 resolve_overloaded_unification (tree tparms,
20671 tree targs,
20672 tree parm,
20673 tree arg,
20674 unification_kind_t strict,
20675 int sub_strict,
20676 bool explain_p)
20677 {
20678 tree tempargs = copy_node (targs);
20679 int good = 0;
20680 tree goodfn = NULL_TREE;
20681 bool addr_p;
20682
20683 if (TREE_CODE (arg) == ADDR_EXPR)
20684 {
20685 arg = TREE_OPERAND (arg, 0);
20686 addr_p = true;
20687 }
20688 else
20689 addr_p = false;
20690
20691 if (TREE_CODE (arg) == COMPONENT_REF)
20692 /* Handle `&x' where `x' is some static or non-static member
20693 function name. */
20694 arg = TREE_OPERAND (arg, 1);
20695
20696 if (TREE_CODE (arg) == OFFSET_REF)
20697 arg = TREE_OPERAND (arg, 1);
20698
20699 /* Strip baselink information. */
20700 if (BASELINK_P (arg))
20701 arg = BASELINK_FUNCTIONS (arg);
20702
20703 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
20704 {
20705 /* If we got some explicit template args, we need to plug them into
20706 the affected templates before we try to unify, in case the
20707 explicit args will completely resolve the templates in question. */
20708
20709 int ok = 0;
20710 tree expl_subargs = TREE_OPERAND (arg, 1);
20711 arg = TREE_OPERAND (arg, 0);
20712
20713 for (lkp_iterator iter (arg); iter; ++iter)
20714 {
20715 tree fn = *iter;
20716 tree subargs, elem;
20717
20718 if (TREE_CODE (fn) != TEMPLATE_DECL)
20719 continue;
20720
20721 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
20722 expl_subargs, NULL_TREE, tf_none,
20723 /*require_all_args=*/true,
20724 /*use_default_args=*/true);
20725 if (subargs != error_mark_node
20726 && !any_dependent_template_arguments_p (subargs))
20727 {
20728 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
20729 if (try_one_overload (tparms, targs, tempargs, parm,
20730 elem, strict, sub_strict, addr_p, explain_p)
20731 && (!goodfn || !same_type_p (goodfn, elem)))
20732 {
20733 goodfn = elem;
20734 ++good;
20735 }
20736 }
20737 else if (subargs)
20738 ++ok;
20739 }
20740 /* If no templates (or more than one) are fully resolved by the
20741 explicit arguments, this template-id is a non-deduced context; it
20742 could still be OK if we deduce all template arguments for the
20743 enclosing call through other arguments. */
20744 if (good != 1)
20745 good = ok;
20746 }
20747 else if (TREE_CODE (arg) != OVERLOAD
20748 && TREE_CODE (arg) != FUNCTION_DECL)
20749 /* If ARG is, for example, "(0, &f)" then its type will be unknown
20750 -- but the deduction does not succeed because the expression is
20751 not just the function on its own. */
20752 return false;
20753 else
20754 for (lkp_iterator iter (arg); iter; ++iter)
20755 {
20756 tree fn = *iter;
20757 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
20758 strict, sub_strict, addr_p, explain_p)
20759 && (!goodfn || !decls_match (goodfn, fn)))
20760 {
20761 goodfn = fn;
20762 ++good;
20763 }
20764 }
20765
20766 /* [temp.deduct.type] A template-argument can be deduced from a pointer
20767 to function or pointer to member function argument if the set of
20768 overloaded functions does not contain function templates and at most
20769 one of a set of overloaded functions provides a unique match.
20770
20771 So if we found multiple possibilities, we return success but don't
20772 deduce anything. */
20773
20774 if (good == 1)
20775 {
20776 int i = TREE_VEC_LENGTH (targs);
20777 for (; i--; )
20778 if (TREE_VEC_ELT (tempargs, i))
20779 {
20780 tree old = TREE_VEC_ELT (targs, i);
20781 tree new_ = TREE_VEC_ELT (tempargs, i);
20782 if (new_ && old && ARGUMENT_PACK_P (old)
20783 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
20784 /* Don't forget explicit template arguments in a pack. */
20785 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
20786 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
20787 TREE_VEC_ELT (targs, i) = new_;
20788 }
20789 }
20790 if (good)
20791 return true;
20792
20793 return false;
20794 }
20795
20796 /* Core DR 115: In contexts where deduction is done and fails, or in
20797 contexts where deduction is not done, if a template argument list is
20798 specified and it, along with any default template arguments, identifies
20799 a single function template specialization, then the template-id is an
20800 lvalue for the function template specialization. */
20801
20802 tree
20803 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
20804 {
20805 tree expr, offset, baselink;
20806 bool addr;
20807
20808 if (!type_unknown_p (orig_expr))
20809 return orig_expr;
20810
20811 expr = orig_expr;
20812 addr = false;
20813 offset = NULL_TREE;
20814 baselink = NULL_TREE;
20815
20816 if (TREE_CODE (expr) == ADDR_EXPR)
20817 {
20818 expr = TREE_OPERAND (expr, 0);
20819 addr = true;
20820 }
20821 if (TREE_CODE (expr) == OFFSET_REF)
20822 {
20823 offset = expr;
20824 expr = TREE_OPERAND (expr, 1);
20825 }
20826 if (BASELINK_P (expr))
20827 {
20828 baselink = expr;
20829 expr = BASELINK_FUNCTIONS (expr);
20830 }
20831
20832 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
20833 {
20834 int good = 0;
20835 tree goodfn = NULL_TREE;
20836
20837 /* If we got some explicit template args, we need to plug them into
20838 the affected templates before we try to unify, in case the
20839 explicit args will completely resolve the templates in question. */
20840
20841 tree expl_subargs = TREE_OPERAND (expr, 1);
20842 tree arg = TREE_OPERAND (expr, 0);
20843 tree badfn = NULL_TREE;
20844 tree badargs = NULL_TREE;
20845
20846 for (lkp_iterator iter (arg); iter; ++iter)
20847 {
20848 tree fn = *iter;
20849 tree subargs, elem;
20850
20851 if (TREE_CODE (fn) != TEMPLATE_DECL)
20852 continue;
20853
20854 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
20855 expl_subargs, NULL_TREE, tf_none,
20856 /*require_all_args=*/true,
20857 /*use_default_args=*/true);
20858 if (subargs != error_mark_node
20859 && !any_dependent_template_arguments_p (subargs))
20860 {
20861 elem = instantiate_template (fn, subargs, tf_none);
20862 if (elem == error_mark_node)
20863 {
20864 badfn = fn;
20865 badargs = subargs;
20866 }
20867 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
20868 {
20869 goodfn = elem;
20870 ++good;
20871 }
20872 }
20873 }
20874 if (good == 1)
20875 {
20876 mark_used (goodfn);
20877 expr = goodfn;
20878 if (baselink)
20879 expr = build_baselink (BASELINK_BINFO (baselink),
20880 BASELINK_ACCESS_BINFO (baselink),
20881 expr, BASELINK_OPTYPE (baselink));
20882 if (offset)
20883 {
20884 tree base
20885 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
20886 expr = build_offset_ref (base, expr, addr, complain);
20887 }
20888 if (addr)
20889 expr = cp_build_addr_expr (expr, complain);
20890 return expr;
20891 }
20892 else if (good == 0 && badargs && (complain & tf_error))
20893 /* There were no good options and at least one bad one, so let the
20894 user know what the problem is. */
20895 instantiate_template (badfn, badargs, complain);
20896 }
20897 return orig_expr;
20898 }
20899
20900 /* Subroutine of resolve_overloaded_unification; does deduction for a single
20901 overload. Fills TARGS with any deduced arguments, or error_mark_node if
20902 different overloads deduce different arguments for a given parm.
20903 ADDR_P is true if the expression for which deduction is being
20904 performed was of the form "& fn" rather than simply "fn".
20905
20906 Returns 1 on success. */
20907
20908 static int
20909 try_one_overload (tree tparms,
20910 tree orig_targs,
20911 tree targs,
20912 tree parm,
20913 tree arg,
20914 unification_kind_t strict,
20915 int sub_strict,
20916 bool addr_p,
20917 bool explain_p)
20918 {
20919 int nargs;
20920 tree tempargs;
20921 int i;
20922
20923 if (arg == error_mark_node)
20924 return 0;
20925
20926 /* [temp.deduct.type] A template-argument can be deduced from a pointer
20927 to function or pointer to member function argument if the set of
20928 overloaded functions does not contain function templates and at most
20929 one of a set of overloaded functions provides a unique match.
20930
20931 So if this is a template, just return success. */
20932
20933 if (uses_template_parms (arg))
20934 return 1;
20935
20936 if (TREE_CODE (arg) == METHOD_TYPE)
20937 arg = build_ptrmemfunc_type (build_pointer_type (arg));
20938 else if (addr_p)
20939 arg = build_pointer_type (arg);
20940
20941 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
20942
20943 /* We don't copy orig_targs for this because if we have already deduced
20944 some template args from previous args, unify would complain when we
20945 try to deduce a template parameter for the same argument, even though
20946 there isn't really a conflict. */
20947 nargs = TREE_VEC_LENGTH (targs);
20948 tempargs = make_tree_vec (nargs);
20949
20950 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
20951 return 0;
20952
20953 /* First make sure we didn't deduce anything that conflicts with
20954 explicitly specified args. */
20955 for (i = nargs; i--; )
20956 {
20957 tree elt = TREE_VEC_ELT (tempargs, i);
20958 tree oldelt = TREE_VEC_ELT (orig_targs, i);
20959
20960 if (!elt)
20961 /*NOP*/;
20962 else if (uses_template_parms (elt))
20963 /* Since we're unifying against ourselves, we will fill in
20964 template args used in the function parm list with our own
20965 template parms. Discard them. */
20966 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
20967 else if (oldelt && ARGUMENT_PACK_P (oldelt))
20968 {
20969 /* Check that the argument at each index of the deduced argument pack
20970 is equivalent to the corresponding explicitly specified argument.
20971 We may have deduced more arguments than were explicitly specified,
20972 and that's OK. */
20973
20974 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
20975 that's wrong if we deduce the same argument pack from multiple
20976 function arguments: it's only incomplete the first time. */
20977
20978 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
20979 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
20980
20981 if (TREE_VEC_LENGTH (deduced_pack)
20982 < TREE_VEC_LENGTH (explicit_pack))
20983 return 0;
20984
20985 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
20986 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
20987 TREE_VEC_ELT (deduced_pack, j)))
20988 return 0;
20989 }
20990 else if (oldelt && !template_args_equal (oldelt, elt))
20991 return 0;
20992 }
20993
20994 for (i = nargs; i--; )
20995 {
20996 tree elt = TREE_VEC_ELT (tempargs, i);
20997
20998 if (elt)
20999 TREE_VEC_ELT (targs, i) = elt;
21000 }
21001
21002 return 1;
21003 }
21004
21005 /* PARM is a template class (perhaps with unbound template
21006 parameters). ARG is a fully instantiated type. If ARG can be
21007 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
21008 TARGS are as for unify. */
21009
21010 static tree
21011 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
21012 bool explain_p)
21013 {
21014 tree copy_of_targs;
21015
21016 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
21017 return NULL_TREE;
21018 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21019 /* Matches anything. */;
21020 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
21021 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
21022 return NULL_TREE;
21023
21024 /* We need to make a new template argument vector for the call to
21025 unify. If we used TARGS, we'd clutter it up with the result of
21026 the attempted unification, even if this class didn't work out.
21027 We also don't want to commit ourselves to all the unifications
21028 we've already done, since unification is supposed to be done on
21029 an argument-by-argument basis. In other words, consider the
21030 following pathological case:
21031
21032 template <int I, int J, int K>
21033 struct S {};
21034
21035 template <int I, int J>
21036 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
21037
21038 template <int I, int J, int K>
21039 void f(S<I, J, K>, S<I, I, I>);
21040
21041 void g() {
21042 S<0, 0, 0> s0;
21043 S<0, 1, 2> s2;
21044
21045 f(s0, s2);
21046 }
21047
21048 Now, by the time we consider the unification involving `s2', we
21049 already know that we must have `f<0, 0, 0>'. But, even though
21050 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
21051 because there are two ways to unify base classes of S<0, 1, 2>
21052 with S<I, I, I>. If we kept the already deduced knowledge, we
21053 would reject the possibility I=1. */
21054 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
21055
21056 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21057 {
21058 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
21059 return NULL_TREE;
21060 return arg;
21061 }
21062
21063 /* If unification failed, we're done. */
21064 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
21065 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
21066 return NULL_TREE;
21067
21068 return arg;
21069 }
21070
21071 /* Given a template type PARM and a class type ARG, find the unique
21072 base type in ARG that is an instance of PARM. We do not examine
21073 ARG itself; only its base-classes. If there is not exactly one
21074 appropriate base class, return NULL_TREE. PARM may be the type of
21075 a partial specialization, as well as a plain template type. Used
21076 by unify. */
21077
21078 static enum template_base_result
21079 get_template_base (tree tparms, tree targs, tree parm, tree arg,
21080 bool explain_p, tree *result)
21081 {
21082 tree rval = NULL_TREE;
21083 tree binfo;
21084
21085 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
21086
21087 binfo = TYPE_BINFO (complete_type (arg));
21088 if (!binfo)
21089 {
21090 /* The type could not be completed. */
21091 *result = NULL_TREE;
21092 return tbr_incomplete_type;
21093 }
21094
21095 /* Walk in inheritance graph order. The search order is not
21096 important, and this avoids multiple walks of virtual bases. */
21097 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
21098 {
21099 tree r = try_class_unification (tparms, targs, parm,
21100 BINFO_TYPE (binfo), explain_p);
21101
21102 if (r)
21103 {
21104 /* If there is more than one satisfactory baseclass, then:
21105
21106 [temp.deduct.call]
21107
21108 If they yield more than one possible deduced A, the type
21109 deduction fails.
21110
21111 applies. */
21112 if (rval && !same_type_p (r, rval))
21113 {
21114 *result = NULL_TREE;
21115 return tbr_ambiguous_baseclass;
21116 }
21117
21118 rval = r;
21119 }
21120 }
21121
21122 *result = rval;
21123 return tbr_success;
21124 }
21125
21126 /* Returns the level of DECL, which declares a template parameter. */
21127
21128 static int
21129 template_decl_level (tree decl)
21130 {
21131 switch (TREE_CODE (decl))
21132 {
21133 case TYPE_DECL:
21134 case TEMPLATE_DECL:
21135 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
21136
21137 case PARM_DECL:
21138 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
21139
21140 default:
21141 gcc_unreachable ();
21142 }
21143 return 0;
21144 }
21145
21146 /* Decide whether ARG can be unified with PARM, considering only the
21147 cv-qualifiers of each type, given STRICT as documented for unify.
21148 Returns nonzero iff the unification is OK on that basis. */
21149
21150 static int
21151 check_cv_quals_for_unify (int strict, tree arg, tree parm)
21152 {
21153 int arg_quals = cp_type_quals (arg);
21154 int parm_quals = cp_type_quals (parm);
21155
21156 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
21157 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
21158 {
21159 /* Although a CVR qualifier is ignored when being applied to a
21160 substituted template parameter ([8.3.2]/1 for example), that
21161 does not allow us to unify "const T" with "int&" because both
21162 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
21163 It is ok when we're allowing additional CV qualifiers
21164 at the outer level [14.8.2.1]/3,1st bullet. */
21165 if ((TYPE_REF_P (arg)
21166 || TREE_CODE (arg) == FUNCTION_TYPE
21167 || TREE_CODE (arg) == METHOD_TYPE)
21168 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
21169 return 0;
21170
21171 if ((!INDIRECT_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
21172 && (parm_quals & TYPE_QUAL_RESTRICT))
21173 return 0;
21174 }
21175
21176 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
21177 && (arg_quals & parm_quals) != parm_quals)
21178 return 0;
21179
21180 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
21181 && (parm_quals & arg_quals) != arg_quals)
21182 return 0;
21183
21184 return 1;
21185 }
21186
21187 /* Determines the LEVEL and INDEX for the template parameter PARM. */
21188 void
21189 template_parm_level_and_index (tree parm, int* level, int* index)
21190 {
21191 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
21192 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
21193 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21194 {
21195 *index = TEMPLATE_TYPE_IDX (parm);
21196 *level = TEMPLATE_TYPE_LEVEL (parm);
21197 }
21198 else
21199 {
21200 *index = TEMPLATE_PARM_IDX (parm);
21201 *level = TEMPLATE_PARM_LEVEL (parm);
21202 }
21203 }
21204
21205 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
21206 do { \
21207 if (unify (TP, TA, P, A, S, EP)) \
21208 return 1; \
21209 } while (0)
21210
21211 /* Unifies the remaining arguments in PACKED_ARGS with the pack
21212 expansion at the end of PACKED_PARMS. Returns 0 if the type
21213 deduction succeeds, 1 otherwise. STRICT is the same as in
21214 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
21215 function call argument list. We'll need to adjust the arguments to make them
21216 types. SUBR tells us if this is from a recursive call to
21217 type_unification_real, or for comparing two template argument
21218 lists. */
21219
21220 static int
21221 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
21222 tree packed_args, unification_kind_t strict,
21223 bool subr, bool explain_p)
21224 {
21225 tree parm
21226 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
21227 tree pattern = PACK_EXPANSION_PATTERN (parm);
21228 tree pack, packs = NULL_TREE;
21229 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
21230
21231 /* Add in any args remembered from an earlier partial instantiation. */
21232 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
21233 int levels = TMPL_ARGS_DEPTH (targs);
21234
21235 packed_args = expand_template_argument_pack (packed_args);
21236
21237 int len = TREE_VEC_LENGTH (packed_args);
21238
21239 /* Determine the parameter packs we will be deducing from the
21240 pattern, and record their current deductions. */
21241 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
21242 pack; pack = TREE_CHAIN (pack))
21243 {
21244 tree parm_pack = TREE_VALUE (pack);
21245 int idx, level;
21246
21247 /* Only template parameter packs can be deduced, not e.g. function
21248 parameter packs or __bases or __integer_pack. */
21249 if (!TEMPLATE_PARM_P (parm_pack))
21250 continue;
21251
21252 /* Determine the index and level of this parameter pack. */
21253 template_parm_level_and_index (parm_pack, &level, &idx);
21254 if (level < levels)
21255 continue;
21256
21257 /* Keep track of the parameter packs and their corresponding
21258 argument packs. */
21259 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
21260 TREE_TYPE (packs) = make_tree_vec (len - start);
21261 }
21262
21263 /* Loop through all of the arguments that have not yet been
21264 unified and unify each with the pattern. */
21265 for (i = start; i < len; i++)
21266 {
21267 tree parm;
21268 bool any_explicit = false;
21269 tree arg = TREE_VEC_ELT (packed_args, i);
21270
21271 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
21272 or the element of its argument pack at the current index if
21273 this argument was explicitly specified. */
21274 for (pack = packs; pack; pack = TREE_CHAIN (pack))
21275 {
21276 int idx, level;
21277 tree arg, pargs;
21278 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
21279
21280 arg = NULL_TREE;
21281 if (TREE_VALUE (pack)
21282 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
21283 && (i - start < TREE_VEC_LENGTH (pargs)))
21284 {
21285 any_explicit = true;
21286 arg = TREE_VEC_ELT (pargs, i - start);
21287 }
21288 TMPL_ARG (targs, level, idx) = arg;
21289 }
21290
21291 /* If we had explicit template arguments, substitute them into the
21292 pattern before deduction. */
21293 if (any_explicit)
21294 {
21295 /* Some arguments might still be unspecified or dependent. */
21296 bool dependent;
21297 ++processing_template_decl;
21298 dependent = any_dependent_template_arguments_p (targs);
21299 if (!dependent)
21300 --processing_template_decl;
21301 parm = tsubst (pattern, targs,
21302 explain_p ? tf_warning_or_error : tf_none,
21303 NULL_TREE);
21304 if (dependent)
21305 --processing_template_decl;
21306 if (parm == error_mark_node)
21307 return 1;
21308 }
21309 else
21310 parm = pattern;
21311
21312 /* Unify the pattern with the current argument. */
21313 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
21314 explain_p))
21315 return 1;
21316
21317 /* For each parameter pack, collect the deduced value. */
21318 for (pack = packs; pack; pack = TREE_CHAIN (pack))
21319 {
21320 int idx, level;
21321 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
21322
21323 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
21324 TMPL_ARG (targs, level, idx);
21325 }
21326 }
21327
21328 /* Verify that the results of unification with the parameter packs
21329 produce results consistent with what we've seen before, and make
21330 the deduced argument packs available. */
21331 for (pack = packs; pack; pack = TREE_CHAIN (pack))
21332 {
21333 tree old_pack = TREE_VALUE (pack);
21334 tree new_args = TREE_TYPE (pack);
21335 int i, len = TREE_VEC_LENGTH (new_args);
21336 int idx, level;
21337 bool nondeduced_p = false;
21338
21339 /* By default keep the original deduced argument pack.
21340 If necessary, more specific code is going to update the
21341 resulting deduced argument later down in this function. */
21342 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
21343 TMPL_ARG (targs, level, idx) = old_pack;
21344
21345 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
21346 actually deduce anything. */
21347 for (i = 0; i < len && !nondeduced_p; ++i)
21348 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
21349 nondeduced_p = true;
21350 if (nondeduced_p)
21351 continue;
21352
21353 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
21354 {
21355 /* If we had fewer function args than explicit template args,
21356 just use the explicits. */
21357 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
21358 int explicit_len = TREE_VEC_LENGTH (explicit_args);
21359 if (len < explicit_len)
21360 new_args = explicit_args;
21361 }
21362
21363 if (!old_pack)
21364 {
21365 tree result;
21366 /* Build the deduced *_ARGUMENT_PACK. */
21367 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
21368 {
21369 result = make_node (NONTYPE_ARGUMENT_PACK);
21370 TREE_CONSTANT (result) = 1;
21371 }
21372 else
21373 result = cxx_make_type (TYPE_ARGUMENT_PACK);
21374
21375 SET_ARGUMENT_PACK_ARGS (result, new_args);
21376
21377 /* Note the deduced argument packs for this parameter
21378 pack. */
21379 TMPL_ARG (targs, level, idx) = result;
21380 }
21381 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
21382 && (ARGUMENT_PACK_ARGS (old_pack)
21383 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
21384 {
21385 /* We only had the explicitly-provided arguments before, but
21386 now we have a complete set of arguments. */
21387 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
21388
21389 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
21390 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
21391 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
21392 }
21393 else
21394 {
21395 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
21396 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
21397
21398 if (!comp_template_args (old_args, new_args,
21399 &bad_old_arg, &bad_new_arg))
21400 /* Inconsistent unification of this parameter pack. */
21401 return unify_parameter_pack_inconsistent (explain_p,
21402 bad_old_arg,
21403 bad_new_arg);
21404 }
21405 }
21406
21407 return unify_success (explain_p);
21408 }
21409
21410 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
21411 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
21412 parameters and return value are as for unify. */
21413
21414 static int
21415 unify_array_domain (tree tparms, tree targs,
21416 tree parm_dom, tree arg_dom,
21417 bool explain_p)
21418 {
21419 tree parm_max;
21420 tree arg_max;
21421 bool parm_cst;
21422 bool arg_cst;
21423
21424 /* Our representation of array types uses "N - 1" as the
21425 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
21426 not an integer constant. We cannot unify arbitrarily
21427 complex expressions, so we eliminate the MINUS_EXPRs
21428 here. */
21429 parm_max = TYPE_MAX_VALUE (parm_dom);
21430 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
21431 if (!parm_cst)
21432 {
21433 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
21434 parm_max = TREE_OPERAND (parm_max, 0);
21435 }
21436 arg_max = TYPE_MAX_VALUE (arg_dom);
21437 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
21438 if (!arg_cst)
21439 {
21440 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
21441 trying to unify the type of a variable with the type
21442 of a template parameter. For example:
21443
21444 template <unsigned int N>
21445 void f (char (&) [N]);
21446 int g();
21447 void h(int i) {
21448 char a[g(i)];
21449 f(a);
21450 }
21451
21452 Here, the type of the ARG will be "int [g(i)]", and
21453 may be a SAVE_EXPR, etc. */
21454 if (TREE_CODE (arg_max) != MINUS_EXPR)
21455 return unify_vla_arg (explain_p, arg_dom);
21456 arg_max = TREE_OPERAND (arg_max, 0);
21457 }
21458
21459 /* If only one of the bounds used a MINUS_EXPR, compensate
21460 by adding one to the other bound. */
21461 if (parm_cst && !arg_cst)
21462 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
21463 integer_type_node,
21464 parm_max,
21465 integer_one_node);
21466 else if (arg_cst && !parm_cst)
21467 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
21468 integer_type_node,
21469 arg_max,
21470 integer_one_node);
21471
21472 return unify (tparms, targs, parm_max, arg_max,
21473 UNIFY_ALLOW_INTEGER, explain_p);
21474 }
21475
21476 /* Returns whether T, a P or A in unify, is a type, template or expression. */
21477
21478 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
21479
21480 static pa_kind_t
21481 pa_kind (tree t)
21482 {
21483 if (PACK_EXPANSION_P (t))
21484 t = PACK_EXPANSION_PATTERN (t);
21485 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
21486 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
21487 || DECL_TYPE_TEMPLATE_P (t))
21488 return pa_tmpl;
21489 else if (TYPE_P (t))
21490 return pa_type;
21491 else
21492 return pa_expr;
21493 }
21494
21495 /* Deduce the value of template parameters. TPARMS is the (innermost)
21496 set of template parameters to a template. TARGS is the bindings
21497 for those template parameters, as determined thus far; TARGS may
21498 include template arguments for outer levels of template parameters
21499 as well. PARM is a parameter to a template function, or a
21500 subcomponent of that parameter; ARG is the corresponding argument.
21501 This function attempts to match PARM with ARG in a manner
21502 consistent with the existing assignments in TARGS. If more values
21503 are deduced, then TARGS is updated.
21504
21505 Returns 0 if the type deduction succeeds, 1 otherwise. The
21506 parameter STRICT is a bitwise or of the following flags:
21507
21508 UNIFY_ALLOW_NONE:
21509 Require an exact match between PARM and ARG.
21510 UNIFY_ALLOW_MORE_CV_QUAL:
21511 Allow the deduced ARG to be more cv-qualified (by qualification
21512 conversion) than ARG.
21513 UNIFY_ALLOW_LESS_CV_QUAL:
21514 Allow the deduced ARG to be less cv-qualified than ARG.
21515 UNIFY_ALLOW_DERIVED:
21516 Allow the deduced ARG to be a template base class of ARG,
21517 or a pointer to a template base class of the type pointed to by
21518 ARG.
21519 UNIFY_ALLOW_INTEGER:
21520 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
21521 case for more information.
21522 UNIFY_ALLOW_OUTER_LEVEL:
21523 This is the outermost level of a deduction. Used to determine validity
21524 of qualification conversions. A valid qualification conversion must
21525 have const qualified pointers leading up to the inner type which
21526 requires additional CV quals, except at the outer level, where const
21527 is not required [conv.qual]. It would be normal to set this flag in
21528 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
21529 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
21530 This is the outermost level of a deduction, and PARM can be more CV
21531 qualified at this point.
21532 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
21533 This is the outermost level of a deduction, and PARM can be less CV
21534 qualified at this point. */
21535
21536 static int
21537 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
21538 bool explain_p)
21539 {
21540 int idx;
21541 tree targ;
21542 tree tparm;
21543 int strict_in = strict;
21544 tsubst_flags_t complain = (explain_p
21545 ? tf_warning_or_error
21546 : tf_none);
21547
21548 /* I don't think this will do the right thing with respect to types.
21549 But the only case I've seen it in so far has been array bounds, where
21550 signedness is the only information lost, and I think that will be
21551 okay. */
21552 while (CONVERT_EXPR_P (parm))
21553 parm = TREE_OPERAND (parm, 0);
21554
21555 if (arg == error_mark_node)
21556 return unify_invalid (explain_p);
21557 if (arg == unknown_type_node
21558 || arg == init_list_type_node)
21559 /* We can't deduce anything from this, but we might get all the
21560 template args from other function args. */
21561 return unify_success (explain_p);
21562
21563 if (parm == any_targ_node || arg == any_targ_node)
21564 return unify_success (explain_p);
21565
21566 /* If PARM uses template parameters, then we can't bail out here,
21567 even if ARG == PARM, since we won't record unifications for the
21568 template parameters. We might need them if we're trying to
21569 figure out which of two things is more specialized. */
21570 if (arg == parm && !uses_template_parms (parm))
21571 return unify_success (explain_p);
21572
21573 /* Handle init lists early, so the rest of the function can assume
21574 we're dealing with a type. */
21575 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
21576 {
21577 tree elt, elttype;
21578 unsigned i;
21579 tree orig_parm = parm;
21580
21581 /* Replace T with std::initializer_list<T> for deduction. */
21582 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
21583 && flag_deduce_init_list)
21584 parm = listify (parm);
21585
21586 if (!is_std_init_list (parm)
21587 && TREE_CODE (parm) != ARRAY_TYPE)
21588 /* We can only deduce from an initializer list argument if the
21589 parameter is std::initializer_list or an array; otherwise this
21590 is a non-deduced context. */
21591 return unify_success (explain_p);
21592
21593 if (TREE_CODE (parm) == ARRAY_TYPE)
21594 elttype = TREE_TYPE (parm);
21595 else
21596 {
21597 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
21598 /* Deduction is defined in terms of a single type, so just punt
21599 on the (bizarre) std::initializer_list<T...>. */
21600 if (PACK_EXPANSION_P (elttype))
21601 return unify_success (explain_p);
21602 }
21603
21604 if (strict != DEDUCE_EXACT
21605 && TYPE_P (elttype)
21606 && !uses_deducible_template_parms (elttype))
21607 /* If ELTTYPE has no deducible template parms, skip deduction from
21608 the list elements. */;
21609 else
21610 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
21611 {
21612 int elt_strict = strict;
21613
21614 if (elt == error_mark_node)
21615 return unify_invalid (explain_p);
21616
21617 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
21618 {
21619 tree type = TREE_TYPE (elt);
21620 if (type == error_mark_node)
21621 return unify_invalid (explain_p);
21622 /* It should only be possible to get here for a call. */
21623 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
21624 elt_strict |= maybe_adjust_types_for_deduction
21625 (DEDUCE_CALL, &elttype, &type, elt);
21626 elt = type;
21627 }
21628
21629 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
21630 explain_p);
21631 }
21632
21633 if (TREE_CODE (parm) == ARRAY_TYPE
21634 && deducible_array_bound (TYPE_DOMAIN (parm)))
21635 {
21636 /* Also deduce from the length of the initializer list. */
21637 tree max = size_int (CONSTRUCTOR_NELTS (arg));
21638 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
21639 if (idx == error_mark_node)
21640 return unify_invalid (explain_p);
21641 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
21642 idx, explain_p);
21643 }
21644
21645 /* If the std::initializer_list<T> deduction worked, replace the
21646 deduced A with std::initializer_list<A>. */
21647 if (orig_parm != parm)
21648 {
21649 idx = TEMPLATE_TYPE_IDX (orig_parm);
21650 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
21651 targ = listify (targ);
21652 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
21653 }
21654 return unify_success (explain_p);
21655 }
21656
21657 /* If parm and arg aren't the same kind of thing (template, type, or
21658 expression), fail early. */
21659 if (pa_kind (parm) != pa_kind (arg))
21660 return unify_invalid (explain_p);
21661
21662 /* Immediately reject some pairs that won't unify because of
21663 cv-qualification mismatches. */
21664 if (TREE_CODE (arg) == TREE_CODE (parm)
21665 && TYPE_P (arg)
21666 /* It is the elements of the array which hold the cv quals of an array
21667 type, and the elements might be template type parms. We'll check
21668 when we recurse. */
21669 && TREE_CODE (arg) != ARRAY_TYPE
21670 /* We check the cv-qualifiers when unifying with template type
21671 parameters below. We want to allow ARG `const T' to unify with
21672 PARM `T' for example, when computing which of two templates
21673 is more specialized, for example. */
21674 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
21675 && !check_cv_quals_for_unify (strict_in, arg, parm))
21676 return unify_cv_qual_mismatch (explain_p, parm, arg);
21677
21678 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
21679 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
21680 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
21681 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
21682 strict &= ~UNIFY_ALLOW_DERIVED;
21683 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
21684 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
21685
21686 switch (TREE_CODE (parm))
21687 {
21688 case TYPENAME_TYPE:
21689 case SCOPE_REF:
21690 case UNBOUND_CLASS_TEMPLATE:
21691 /* In a type which contains a nested-name-specifier, template
21692 argument values cannot be deduced for template parameters used
21693 within the nested-name-specifier. */
21694 return unify_success (explain_p);
21695
21696 case TEMPLATE_TYPE_PARM:
21697 case TEMPLATE_TEMPLATE_PARM:
21698 case BOUND_TEMPLATE_TEMPLATE_PARM:
21699 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
21700 if (error_operand_p (tparm))
21701 return unify_invalid (explain_p);
21702
21703 if (TEMPLATE_TYPE_LEVEL (parm)
21704 != template_decl_level (tparm))
21705 /* The PARM is not one we're trying to unify. Just check
21706 to see if it matches ARG. */
21707 {
21708 if (TREE_CODE (arg) == TREE_CODE (parm)
21709 && (is_auto (parm) ? is_auto (arg)
21710 : same_type_p (parm, arg)))
21711 return unify_success (explain_p);
21712 else
21713 return unify_type_mismatch (explain_p, parm, arg);
21714 }
21715 idx = TEMPLATE_TYPE_IDX (parm);
21716 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
21717 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
21718 if (error_operand_p (tparm))
21719 return unify_invalid (explain_p);
21720
21721 /* Check for mixed types and values. */
21722 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
21723 && TREE_CODE (tparm) != TYPE_DECL)
21724 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
21725 && TREE_CODE (tparm) != TEMPLATE_DECL))
21726 gcc_unreachable ();
21727
21728 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21729 {
21730 if ((strict_in & UNIFY_ALLOW_DERIVED)
21731 && CLASS_TYPE_P (arg))
21732 {
21733 /* First try to match ARG directly. */
21734 tree t = try_class_unification (tparms, targs, parm, arg,
21735 explain_p);
21736 if (!t)
21737 {
21738 /* Otherwise, look for a suitable base of ARG, as below. */
21739 enum template_base_result r;
21740 r = get_template_base (tparms, targs, parm, arg,
21741 explain_p, &t);
21742 if (!t)
21743 return unify_no_common_base (explain_p, r, parm, arg);
21744 arg = t;
21745 }
21746 }
21747 /* ARG must be constructed from a template class or a template
21748 template parameter. */
21749 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
21750 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
21751 return unify_template_deduction_failure (explain_p, parm, arg);
21752
21753 /* Deduce arguments T, i from TT<T> or TT<i>. */
21754 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
21755 return 1;
21756
21757 arg = TYPE_TI_TEMPLATE (arg);
21758
21759 /* Fall through to deduce template name. */
21760 }
21761
21762 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
21763 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21764 {
21765 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
21766
21767 /* Simple cases: Value already set, does match or doesn't. */
21768 if (targ != NULL_TREE && template_args_equal (targ, arg))
21769 return unify_success (explain_p);
21770 else if (targ)
21771 return unify_inconsistency (explain_p, parm, targ, arg);
21772 }
21773 else
21774 {
21775 /* If PARM is `const T' and ARG is only `int', we don't have
21776 a match unless we are allowing additional qualification.
21777 If ARG is `const int' and PARM is just `T' that's OK;
21778 that binds `const int' to `T'. */
21779 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
21780 arg, parm))
21781 return unify_cv_qual_mismatch (explain_p, parm, arg);
21782
21783 /* Consider the case where ARG is `const volatile int' and
21784 PARM is `const T'. Then, T should be `volatile int'. */
21785 arg = cp_build_qualified_type_real
21786 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
21787 if (arg == error_mark_node)
21788 return unify_invalid (explain_p);
21789
21790 /* Simple cases: Value already set, does match or doesn't. */
21791 if (targ != NULL_TREE && same_type_p (targ, arg))
21792 return unify_success (explain_p);
21793 else if (targ)
21794 return unify_inconsistency (explain_p, parm, targ, arg);
21795
21796 /* Make sure that ARG is not a variable-sized array. (Note
21797 that were talking about variable-sized arrays (like
21798 `int[n]'), rather than arrays of unknown size (like
21799 `int[]').) We'll get very confused by such a type since
21800 the bound of the array is not constant, and therefore
21801 not mangleable. Besides, such types are not allowed in
21802 ISO C++, so we can do as we please here. We do allow
21803 them for 'auto' deduction, since that isn't ABI-exposed. */
21804 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
21805 return unify_vla_arg (explain_p, arg);
21806
21807 /* Strip typedefs as in convert_template_argument. */
21808 arg = canonicalize_type_argument (arg, tf_none);
21809 }
21810
21811 /* If ARG is a parameter pack or an expansion, we cannot unify
21812 against it unless PARM is also a parameter pack. */
21813 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
21814 && !template_parameter_pack_p (parm))
21815 return unify_parameter_pack_mismatch (explain_p, parm, arg);
21816
21817 /* If the argument deduction results is a METHOD_TYPE,
21818 then there is a problem.
21819 METHOD_TYPE doesn't map to any real C++ type the result of
21820 the deduction can not be of that type. */
21821 if (TREE_CODE (arg) == METHOD_TYPE)
21822 return unify_method_type_error (explain_p, arg);
21823
21824 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
21825 return unify_success (explain_p);
21826
21827 case TEMPLATE_PARM_INDEX:
21828 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
21829 if (error_operand_p (tparm))
21830 return unify_invalid (explain_p);
21831
21832 if (TEMPLATE_PARM_LEVEL (parm)
21833 != template_decl_level (tparm))
21834 {
21835 /* The PARM is not one we're trying to unify. Just check
21836 to see if it matches ARG. */
21837 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
21838 && cp_tree_equal (parm, arg));
21839 if (result)
21840 unify_expression_unequal (explain_p, parm, arg);
21841 return result;
21842 }
21843
21844 idx = TEMPLATE_PARM_IDX (parm);
21845 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
21846
21847 if (targ)
21848 {
21849 if ((strict & UNIFY_ALLOW_INTEGER)
21850 && TREE_TYPE (targ) && TREE_TYPE (arg)
21851 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
21852 /* We're deducing from an array bound, the type doesn't matter. */
21853 arg = fold_convert (TREE_TYPE (targ), arg);
21854 int x = !cp_tree_equal (targ, arg);
21855 if (x)
21856 unify_inconsistency (explain_p, parm, targ, arg);
21857 return x;
21858 }
21859
21860 /* [temp.deduct.type] If, in the declaration of a function template
21861 with a non-type template-parameter, the non-type
21862 template-parameter is used in an expression in the function
21863 parameter-list and, if the corresponding template-argument is
21864 deduced, the template-argument type shall match the type of the
21865 template-parameter exactly, except that a template-argument
21866 deduced from an array bound may be of any integral type.
21867 The non-type parameter might use already deduced type parameters. */
21868 tparm = TREE_TYPE (parm);
21869 if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs))
21870 /* We don't have enough levels of args to do any substitution. This
21871 can happen in the context of -fnew-ttp-matching. */;
21872 else
21873 {
21874 ++processing_template_decl;
21875 tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
21876 --processing_template_decl;
21877
21878 if (tree a = type_uses_auto (tparm))
21879 {
21880 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
21881 if (tparm == error_mark_node)
21882 return 1;
21883 }
21884 }
21885
21886 if (!TREE_TYPE (arg))
21887 /* Template-parameter dependent expression. Just accept it for now.
21888 It will later be processed in convert_template_argument. */
21889 ;
21890 else if (same_type_p (non_reference (TREE_TYPE (arg)),
21891 non_reference (tparm)))
21892 /* OK */;
21893 else if ((strict & UNIFY_ALLOW_INTEGER)
21894 && CP_INTEGRAL_TYPE_P (tparm))
21895 /* Convert the ARG to the type of PARM; the deduced non-type
21896 template argument must exactly match the types of the
21897 corresponding parameter. */
21898 arg = fold (build_nop (tparm, arg));
21899 else if (uses_template_parms (tparm))
21900 {
21901 /* We haven't deduced the type of this parameter yet. */
21902 if (cxx_dialect >= cxx17
21903 /* We deduce from array bounds in try_array_deduction. */
21904 && !(strict & UNIFY_ALLOW_INTEGER))
21905 {
21906 /* Deduce it from the non-type argument. */
21907 tree atype = TREE_TYPE (arg);
21908 RECUR_AND_CHECK_FAILURE (tparms, targs,
21909 tparm, atype,
21910 UNIFY_ALLOW_NONE, explain_p);
21911 }
21912 else
21913 /* Try again later. */
21914 return unify_success (explain_p);
21915 }
21916 else
21917 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
21918
21919 /* If ARG is a parameter pack or an expansion, we cannot unify
21920 against it unless PARM is also a parameter pack. */
21921 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
21922 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
21923 return unify_parameter_pack_mismatch (explain_p, parm, arg);
21924
21925 {
21926 bool removed_attr = false;
21927 arg = strip_typedefs_expr (arg, &removed_attr);
21928 }
21929 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
21930 return unify_success (explain_p);
21931
21932 case PTRMEM_CST:
21933 {
21934 /* A pointer-to-member constant can be unified only with
21935 another constant. */
21936 if (TREE_CODE (arg) != PTRMEM_CST)
21937 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
21938
21939 /* Just unify the class member. It would be useless (and possibly
21940 wrong, depending on the strict flags) to unify also
21941 PTRMEM_CST_CLASS, because we want to be sure that both parm and
21942 arg refer to the same variable, even if through different
21943 classes. For instance:
21944
21945 struct A { int x; };
21946 struct B : A { };
21947
21948 Unification of &A::x and &B::x must succeed. */
21949 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
21950 PTRMEM_CST_MEMBER (arg), strict, explain_p);
21951 }
21952
21953 case POINTER_TYPE:
21954 {
21955 if (!TYPE_PTR_P (arg))
21956 return unify_type_mismatch (explain_p, parm, arg);
21957
21958 /* [temp.deduct.call]
21959
21960 A can be another pointer or pointer to member type that can
21961 be converted to the deduced A via a qualification
21962 conversion (_conv.qual_).
21963
21964 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
21965 This will allow for additional cv-qualification of the
21966 pointed-to types if appropriate. */
21967
21968 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
21969 /* The derived-to-base conversion only persists through one
21970 level of pointers. */
21971 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
21972
21973 return unify (tparms, targs, TREE_TYPE (parm),
21974 TREE_TYPE (arg), strict, explain_p);
21975 }
21976
21977 case REFERENCE_TYPE:
21978 if (!TYPE_REF_P (arg))
21979 return unify_type_mismatch (explain_p, parm, arg);
21980 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21981 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21982
21983 case ARRAY_TYPE:
21984 if (TREE_CODE (arg) != ARRAY_TYPE)
21985 return unify_type_mismatch (explain_p, parm, arg);
21986 if ((TYPE_DOMAIN (parm) == NULL_TREE)
21987 != (TYPE_DOMAIN (arg) == NULL_TREE))
21988 return unify_type_mismatch (explain_p, parm, arg);
21989 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21990 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21991 if (TYPE_DOMAIN (parm) != NULL_TREE)
21992 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
21993 TYPE_DOMAIN (arg), explain_p);
21994 return unify_success (explain_p);
21995
21996 case REAL_TYPE:
21997 case COMPLEX_TYPE:
21998 case VECTOR_TYPE:
21999 case INTEGER_TYPE:
22000 case BOOLEAN_TYPE:
22001 case ENUMERAL_TYPE:
22002 case VOID_TYPE:
22003 case NULLPTR_TYPE:
22004 if (TREE_CODE (arg) != TREE_CODE (parm))
22005 return unify_type_mismatch (explain_p, parm, arg);
22006
22007 /* We have already checked cv-qualification at the top of the
22008 function. */
22009 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
22010 return unify_type_mismatch (explain_p, parm, arg);
22011
22012 /* As far as unification is concerned, this wins. Later checks
22013 will invalidate it if necessary. */
22014 return unify_success (explain_p);
22015
22016 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
22017 /* Type INTEGER_CST can come from ordinary constant template args. */
22018 case INTEGER_CST:
22019 while (CONVERT_EXPR_P (arg))
22020 arg = TREE_OPERAND (arg, 0);
22021
22022 if (TREE_CODE (arg) != INTEGER_CST)
22023 return unify_template_argument_mismatch (explain_p, parm, arg);
22024 return (tree_int_cst_equal (parm, arg)
22025 ? unify_success (explain_p)
22026 : unify_template_argument_mismatch (explain_p, parm, arg));
22027
22028 case TREE_VEC:
22029 {
22030 int i, len, argslen;
22031 int parm_variadic_p = 0;
22032
22033 if (TREE_CODE (arg) != TREE_VEC)
22034 return unify_template_argument_mismatch (explain_p, parm, arg);
22035
22036 len = TREE_VEC_LENGTH (parm);
22037 argslen = TREE_VEC_LENGTH (arg);
22038
22039 /* Check for pack expansions in the parameters. */
22040 for (i = 0; i < len; ++i)
22041 {
22042 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
22043 {
22044 if (i == len - 1)
22045 /* We can unify against something with a trailing
22046 parameter pack. */
22047 parm_variadic_p = 1;
22048 else
22049 /* [temp.deduct.type]/9: If the template argument list of
22050 P contains a pack expansion that is not the last
22051 template argument, the entire template argument list
22052 is a non-deduced context. */
22053 return unify_success (explain_p);
22054 }
22055 }
22056
22057 /* If we don't have enough arguments to satisfy the parameters
22058 (not counting the pack expression at the end), or we have
22059 too many arguments for a parameter list that doesn't end in
22060 a pack expression, we can't unify. */
22061 if (parm_variadic_p
22062 ? argslen < len - parm_variadic_p
22063 : argslen != len)
22064 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
22065
22066 /* Unify all of the parameters that precede the (optional)
22067 pack expression. */
22068 for (i = 0; i < len - parm_variadic_p; ++i)
22069 {
22070 RECUR_AND_CHECK_FAILURE (tparms, targs,
22071 TREE_VEC_ELT (parm, i),
22072 TREE_VEC_ELT (arg, i),
22073 UNIFY_ALLOW_NONE, explain_p);
22074 }
22075 if (parm_variadic_p)
22076 return unify_pack_expansion (tparms, targs, parm, arg,
22077 DEDUCE_EXACT,
22078 /*subr=*/true, explain_p);
22079 return unify_success (explain_p);
22080 }
22081
22082 case RECORD_TYPE:
22083 case UNION_TYPE:
22084 if (TREE_CODE (arg) != TREE_CODE (parm))
22085 return unify_type_mismatch (explain_p, parm, arg);
22086
22087 if (TYPE_PTRMEMFUNC_P (parm))
22088 {
22089 if (!TYPE_PTRMEMFUNC_P (arg))
22090 return unify_type_mismatch (explain_p, parm, arg);
22091
22092 return unify (tparms, targs,
22093 TYPE_PTRMEMFUNC_FN_TYPE (parm),
22094 TYPE_PTRMEMFUNC_FN_TYPE (arg),
22095 strict, explain_p);
22096 }
22097 else if (TYPE_PTRMEMFUNC_P (arg))
22098 return unify_type_mismatch (explain_p, parm, arg);
22099
22100 if (CLASSTYPE_TEMPLATE_INFO (parm))
22101 {
22102 tree t = NULL_TREE;
22103
22104 if (strict_in & UNIFY_ALLOW_DERIVED)
22105 {
22106 /* First, we try to unify the PARM and ARG directly. */
22107 t = try_class_unification (tparms, targs,
22108 parm, arg, explain_p);
22109
22110 if (!t)
22111 {
22112 /* Fallback to the special case allowed in
22113 [temp.deduct.call]:
22114
22115 If P is a class, and P has the form
22116 template-id, then A can be a derived class of
22117 the deduced A. Likewise, if P is a pointer to
22118 a class of the form template-id, A can be a
22119 pointer to a derived class pointed to by the
22120 deduced A. */
22121 enum template_base_result r;
22122 r = get_template_base (tparms, targs, parm, arg,
22123 explain_p, &t);
22124
22125 if (!t)
22126 {
22127 /* Don't give the derived diagnostic if we're
22128 already dealing with the same template. */
22129 bool same_template
22130 = (CLASSTYPE_TEMPLATE_INFO (arg)
22131 && (CLASSTYPE_TI_TEMPLATE (parm)
22132 == CLASSTYPE_TI_TEMPLATE (arg)));
22133 return unify_no_common_base (explain_p && !same_template,
22134 r, parm, arg);
22135 }
22136 }
22137 }
22138 else if (CLASSTYPE_TEMPLATE_INFO (arg)
22139 && (CLASSTYPE_TI_TEMPLATE (parm)
22140 == CLASSTYPE_TI_TEMPLATE (arg)))
22141 /* Perhaps PARM is something like S<U> and ARG is S<int>.
22142 Then, we should unify `int' and `U'. */
22143 t = arg;
22144 else
22145 /* There's no chance of unification succeeding. */
22146 return unify_type_mismatch (explain_p, parm, arg);
22147
22148 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
22149 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
22150 }
22151 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
22152 return unify_type_mismatch (explain_p, parm, arg);
22153 return unify_success (explain_p);
22154
22155 case METHOD_TYPE:
22156 case FUNCTION_TYPE:
22157 {
22158 unsigned int nargs;
22159 tree *args;
22160 tree a;
22161 unsigned int i;
22162
22163 if (TREE_CODE (arg) != TREE_CODE (parm))
22164 return unify_type_mismatch (explain_p, parm, arg);
22165
22166 /* CV qualifications for methods can never be deduced, they must
22167 match exactly. We need to check them explicitly here,
22168 because type_unification_real treats them as any other
22169 cv-qualified parameter. */
22170 if (TREE_CODE (parm) == METHOD_TYPE
22171 && (!check_cv_quals_for_unify
22172 (UNIFY_ALLOW_NONE,
22173 class_of_this_parm (arg),
22174 class_of_this_parm (parm))))
22175 return unify_cv_qual_mismatch (explain_p, parm, arg);
22176 if (TREE_CODE (arg) == FUNCTION_TYPE
22177 && type_memfn_quals (parm) != type_memfn_quals (arg))
22178 return unify_cv_qual_mismatch (explain_p, parm, arg);
22179 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
22180 return unify_type_mismatch (explain_p, parm, arg);
22181
22182 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
22183 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
22184
22185 nargs = list_length (TYPE_ARG_TYPES (arg));
22186 args = XALLOCAVEC (tree, nargs);
22187 for (a = TYPE_ARG_TYPES (arg), i = 0;
22188 a != NULL_TREE && a != void_list_node;
22189 a = TREE_CHAIN (a), ++i)
22190 args[i] = TREE_VALUE (a);
22191 nargs = i;
22192
22193 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
22194 args, nargs, 1, DEDUCE_EXACT,
22195 NULL, explain_p))
22196 return 1;
22197
22198 if (flag_noexcept_type)
22199 {
22200 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
22201 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
22202 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
22203 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
22204 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
22205 && uses_template_parms (TREE_PURPOSE (pspec)))
22206 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
22207 TREE_PURPOSE (aspec),
22208 UNIFY_ALLOW_NONE, explain_p);
22209 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
22210 return unify_type_mismatch (explain_p, parm, arg);
22211 }
22212
22213 return 0;
22214 }
22215
22216 case OFFSET_TYPE:
22217 /* Unify a pointer to member with a pointer to member function, which
22218 deduces the type of the member as a function type. */
22219 if (TYPE_PTRMEMFUNC_P (arg))
22220 {
22221 /* Check top-level cv qualifiers */
22222 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
22223 return unify_cv_qual_mismatch (explain_p, parm, arg);
22224
22225 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
22226 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
22227 UNIFY_ALLOW_NONE, explain_p);
22228
22229 /* Determine the type of the function we are unifying against. */
22230 tree fntype = static_fn_type (arg);
22231
22232 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
22233 }
22234
22235 if (TREE_CODE (arg) != OFFSET_TYPE)
22236 return unify_type_mismatch (explain_p, parm, arg);
22237 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
22238 TYPE_OFFSET_BASETYPE (arg),
22239 UNIFY_ALLOW_NONE, explain_p);
22240 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
22241 strict, explain_p);
22242
22243 case CONST_DECL:
22244 if (DECL_TEMPLATE_PARM_P (parm))
22245 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
22246 if (arg != scalar_constant_value (parm))
22247 return unify_template_argument_mismatch (explain_p, parm, arg);
22248 return unify_success (explain_p);
22249
22250 case FIELD_DECL:
22251 case TEMPLATE_DECL:
22252 /* Matched cases are handled by the ARG == PARM test above. */
22253 return unify_template_argument_mismatch (explain_p, parm, arg);
22254
22255 case VAR_DECL:
22256 /* We might get a variable as a non-type template argument in parm if the
22257 corresponding parameter is type-dependent. Make any necessary
22258 adjustments based on whether arg is a reference. */
22259 if (CONSTANT_CLASS_P (arg))
22260 parm = fold_non_dependent_expr (parm, complain);
22261 else if (REFERENCE_REF_P (arg))
22262 {
22263 tree sub = TREE_OPERAND (arg, 0);
22264 STRIP_NOPS (sub);
22265 if (TREE_CODE (sub) == ADDR_EXPR)
22266 arg = TREE_OPERAND (sub, 0);
22267 }
22268 /* Now use the normal expression code to check whether they match. */
22269 goto expr;
22270
22271 case TYPE_ARGUMENT_PACK:
22272 case NONTYPE_ARGUMENT_PACK:
22273 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
22274 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
22275
22276 case TYPEOF_TYPE:
22277 case DECLTYPE_TYPE:
22278 case UNDERLYING_TYPE:
22279 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
22280 or UNDERLYING_TYPE nodes. */
22281 return unify_success (explain_p);
22282
22283 case ERROR_MARK:
22284 /* Unification fails if we hit an error node. */
22285 return unify_invalid (explain_p);
22286
22287 case INDIRECT_REF:
22288 if (REFERENCE_REF_P (parm))
22289 {
22290 bool pexp = PACK_EXPANSION_P (arg);
22291 if (pexp)
22292 arg = PACK_EXPANSION_PATTERN (arg);
22293 if (REFERENCE_REF_P (arg))
22294 arg = TREE_OPERAND (arg, 0);
22295 if (pexp)
22296 arg = make_pack_expansion (arg, complain);
22297 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
22298 strict, explain_p);
22299 }
22300 /* FALLTHRU */
22301
22302 default:
22303 /* An unresolved overload is a nondeduced context. */
22304 if (is_overloaded_fn (parm) || type_unknown_p (parm))
22305 return unify_success (explain_p);
22306 gcc_assert (EXPR_P (parm) || TREE_CODE (parm) == TRAIT_EXPR);
22307 expr:
22308 /* We must be looking at an expression. This can happen with
22309 something like:
22310
22311 template <int I>
22312 void foo(S<I>, S<I + 2>);
22313
22314 This is a "nondeduced context":
22315
22316 [deduct.type]
22317
22318 The nondeduced contexts are:
22319
22320 --A type that is a template-id in which one or more of
22321 the template-arguments is an expression that references
22322 a template-parameter.
22323
22324 In these cases, we assume deduction succeeded, but don't
22325 actually infer any unifications. */
22326
22327 if (!uses_template_parms (parm)
22328 && !template_args_equal (parm, arg))
22329 return unify_expression_unequal (explain_p, parm, arg);
22330 else
22331 return unify_success (explain_p);
22332 }
22333 }
22334 #undef RECUR_AND_CHECK_FAILURE
22335 \f
22336 /* Note that DECL can be defined in this translation unit, if
22337 required. */
22338
22339 static void
22340 mark_definable (tree decl)
22341 {
22342 tree clone;
22343 DECL_NOT_REALLY_EXTERN (decl) = 1;
22344 FOR_EACH_CLONE (clone, decl)
22345 DECL_NOT_REALLY_EXTERN (clone) = 1;
22346 }
22347
22348 /* Called if RESULT is explicitly instantiated, or is a member of an
22349 explicitly instantiated class. */
22350
22351 void
22352 mark_decl_instantiated (tree result, int extern_p)
22353 {
22354 SET_DECL_EXPLICIT_INSTANTIATION (result);
22355
22356 /* If this entity has already been written out, it's too late to
22357 make any modifications. */
22358 if (TREE_ASM_WRITTEN (result))
22359 return;
22360
22361 /* For anonymous namespace we don't need to do anything. */
22362 if (decl_anon_ns_mem_p (result))
22363 {
22364 gcc_assert (!TREE_PUBLIC (result));
22365 return;
22366 }
22367
22368 if (TREE_CODE (result) != FUNCTION_DECL)
22369 /* The TREE_PUBLIC flag for function declarations will have been
22370 set correctly by tsubst. */
22371 TREE_PUBLIC (result) = 1;
22372
22373 /* This might have been set by an earlier implicit instantiation. */
22374 DECL_COMDAT (result) = 0;
22375
22376 if (extern_p)
22377 DECL_NOT_REALLY_EXTERN (result) = 0;
22378 else
22379 {
22380 mark_definable (result);
22381 mark_needed (result);
22382 /* Always make artificials weak. */
22383 if (DECL_ARTIFICIAL (result) && flag_weak)
22384 comdat_linkage (result);
22385 /* For WIN32 we also want to put explicit instantiations in
22386 linkonce sections. */
22387 else if (TREE_PUBLIC (result))
22388 maybe_make_one_only (result);
22389 if (TREE_CODE (result) == FUNCTION_DECL
22390 && DECL_TEMPLATE_INSTANTIATED (result))
22391 /* If the function has already been instantiated, clear DECL_EXTERNAL,
22392 since start_preparsed_function wouldn't have if we had an earlier
22393 extern explicit instantiation. */
22394 DECL_EXTERNAL (result) = 0;
22395 }
22396
22397 /* If EXTERN_P, then this function will not be emitted -- unless
22398 followed by an explicit instantiation, at which point its linkage
22399 will be adjusted. If !EXTERN_P, then this function will be
22400 emitted here. In neither circumstance do we want
22401 import_export_decl to adjust the linkage. */
22402 DECL_INTERFACE_KNOWN (result) = 1;
22403 }
22404
22405 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
22406 important template arguments. If any are missing, we check whether
22407 they're important by using error_mark_node for substituting into any
22408 args that were used for partial ordering (the ones between ARGS and END)
22409 and seeing if it bubbles up. */
22410
22411 static bool
22412 check_undeduced_parms (tree targs, tree args, tree end)
22413 {
22414 bool found = false;
22415 int i;
22416 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
22417 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
22418 {
22419 found = true;
22420 TREE_VEC_ELT (targs, i) = error_mark_node;
22421 }
22422 if (found)
22423 {
22424 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
22425 if (substed == error_mark_node)
22426 return true;
22427 }
22428 return false;
22429 }
22430
22431 /* Given two function templates PAT1 and PAT2, return:
22432
22433 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
22434 -1 if PAT2 is more specialized than PAT1.
22435 0 if neither is more specialized.
22436
22437 LEN indicates the number of parameters we should consider
22438 (defaulted parameters should not be considered).
22439
22440 The 1998 std underspecified function template partial ordering, and
22441 DR214 addresses the issue. We take pairs of arguments, one from
22442 each of the templates, and deduce them against each other. One of
22443 the templates will be more specialized if all the *other*
22444 template's arguments deduce against its arguments and at least one
22445 of its arguments *does* *not* deduce against the other template's
22446 corresponding argument. Deduction is done as for class templates.
22447 The arguments used in deduction have reference and top level cv
22448 qualifiers removed. Iff both arguments were originally reference
22449 types *and* deduction succeeds in both directions, an lvalue reference
22450 wins against an rvalue reference and otherwise the template
22451 with the more cv-qualified argument wins for that pairing (if
22452 neither is more cv-qualified, they both are equal). Unlike regular
22453 deduction, after all the arguments have been deduced in this way,
22454 we do *not* verify the deduced template argument values can be
22455 substituted into non-deduced contexts.
22456
22457 The logic can be a bit confusing here, because we look at deduce1 and
22458 targs1 to see if pat2 is at least as specialized, and vice versa; if we
22459 can find template arguments for pat1 to make arg1 look like arg2, that
22460 means that arg2 is at least as specialized as arg1. */
22461
22462 int
22463 more_specialized_fn (tree pat1, tree pat2, int len)
22464 {
22465 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
22466 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
22467 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
22468 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
22469 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
22470 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
22471 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
22472 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
22473 tree origs1, origs2;
22474 bool lose1 = false;
22475 bool lose2 = false;
22476
22477 /* Remove the this parameter from non-static member functions. If
22478 one is a non-static member function and the other is not a static
22479 member function, remove the first parameter from that function
22480 also. This situation occurs for operator functions where we
22481 locate both a member function (with this pointer) and non-member
22482 operator (with explicit first operand). */
22483 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
22484 {
22485 len--; /* LEN is the number of significant arguments for DECL1 */
22486 args1 = TREE_CHAIN (args1);
22487 if (!DECL_STATIC_FUNCTION_P (decl2))
22488 args2 = TREE_CHAIN (args2);
22489 }
22490 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
22491 {
22492 args2 = TREE_CHAIN (args2);
22493 if (!DECL_STATIC_FUNCTION_P (decl1))
22494 {
22495 len--;
22496 args1 = TREE_CHAIN (args1);
22497 }
22498 }
22499
22500 /* If only one is a conversion operator, they are unordered. */
22501 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
22502 return 0;
22503
22504 /* Consider the return type for a conversion function */
22505 if (DECL_CONV_FN_P (decl1))
22506 {
22507 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
22508 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
22509 len++;
22510 }
22511
22512 processing_template_decl++;
22513
22514 origs1 = args1;
22515 origs2 = args2;
22516
22517 while (len--
22518 /* Stop when an ellipsis is seen. */
22519 && args1 != NULL_TREE && args2 != NULL_TREE)
22520 {
22521 tree arg1 = TREE_VALUE (args1);
22522 tree arg2 = TREE_VALUE (args2);
22523 int deduce1, deduce2;
22524 int quals1 = -1;
22525 int quals2 = -1;
22526 int ref1 = 0;
22527 int ref2 = 0;
22528
22529 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
22530 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
22531 {
22532 /* When both arguments are pack expansions, we need only
22533 unify the patterns themselves. */
22534 arg1 = PACK_EXPANSION_PATTERN (arg1);
22535 arg2 = PACK_EXPANSION_PATTERN (arg2);
22536
22537 /* This is the last comparison we need to do. */
22538 len = 0;
22539 }
22540
22541 /* DR 1847: If a particular P contains no template-parameters that
22542 participate in template argument deduction, that P is not used to
22543 determine the ordering. */
22544 if (!uses_deducible_template_parms (arg1)
22545 && !uses_deducible_template_parms (arg2))
22546 goto next;
22547
22548 if (TYPE_REF_P (arg1))
22549 {
22550 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
22551 arg1 = TREE_TYPE (arg1);
22552 quals1 = cp_type_quals (arg1);
22553 }
22554
22555 if (TYPE_REF_P (arg2))
22556 {
22557 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
22558 arg2 = TREE_TYPE (arg2);
22559 quals2 = cp_type_quals (arg2);
22560 }
22561
22562 arg1 = TYPE_MAIN_VARIANT (arg1);
22563 arg2 = TYPE_MAIN_VARIANT (arg2);
22564
22565 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
22566 {
22567 int i, len2 = remaining_arguments (args2);
22568 tree parmvec = make_tree_vec (1);
22569 tree argvec = make_tree_vec (len2);
22570 tree ta = args2;
22571
22572 /* Setup the parameter vector, which contains only ARG1. */
22573 TREE_VEC_ELT (parmvec, 0) = arg1;
22574
22575 /* Setup the argument vector, which contains the remaining
22576 arguments. */
22577 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
22578 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
22579
22580 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
22581 argvec, DEDUCE_EXACT,
22582 /*subr=*/true, /*explain_p=*/false)
22583 == 0);
22584
22585 /* We cannot deduce in the other direction, because ARG1 is
22586 a pack expansion but ARG2 is not. */
22587 deduce2 = 0;
22588 }
22589 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
22590 {
22591 int i, len1 = remaining_arguments (args1);
22592 tree parmvec = make_tree_vec (1);
22593 tree argvec = make_tree_vec (len1);
22594 tree ta = args1;
22595
22596 /* Setup the parameter vector, which contains only ARG1. */
22597 TREE_VEC_ELT (parmvec, 0) = arg2;
22598
22599 /* Setup the argument vector, which contains the remaining
22600 arguments. */
22601 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
22602 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
22603
22604 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
22605 argvec, DEDUCE_EXACT,
22606 /*subr=*/true, /*explain_p=*/false)
22607 == 0);
22608
22609 /* We cannot deduce in the other direction, because ARG2 is
22610 a pack expansion but ARG1 is not.*/
22611 deduce1 = 0;
22612 }
22613
22614 else
22615 {
22616 /* The normal case, where neither argument is a pack
22617 expansion. */
22618 deduce1 = (unify (tparms1, targs1, arg1, arg2,
22619 UNIFY_ALLOW_NONE, /*explain_p=*/false)
22620 == 0);
22621 deduce2 = (unify (tparms2, targs2, arg2, arg1,
22622 UNIFY_ALLOW_NONE, /*explain_p=*/false)
22623 == 0);
22624 }
22625
22626 /* If we couldn't deduce arguments for tparms1 to make arg1 match
22627 arg2, then arg2 is not as specialized as arg1. */
22628 if (!deduce1)
22629 lose2 = true;
22630 if (!deduce2)
22631 lose1 = true;
22632
22633 /* "If, for a given type, deduction succeeds in both directions
22634 (i.e., the types are identical after the transformations above)
22635 and both P and A were reference types (before being replaced with
22636 the type referred to above):
22637 - if the type from the argument template was an lvalue reference and
22638 the type from the parameter template was not, the argument type is
22639 considered to be more specialized than the other; otherwise,
22640 - if the type from the argument template is more cv-qualified
22641 than the type from the parameter template (as described above),
22642 the argument type is considered to be more specialized than the other;
22643 otherwise,
22644 - neither type is more specialized than the other." */
22645
22646 if (deduce1 && deduce2)
22647 {
22648 if (ref1 && ref2 && ref1 != ref2)
22649 {
22650 if (ref1 > ref2)
22651 lose1 = true;
22652 else
22653 lose2 = true;
22654 }
22655 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
22656 {
22657 if ((quals1 & quals2) == quals2)
22658 lose2 = true;
22659 if ((quals1 & quals2) == quals1)
22660 lose1 = true;
22661 }
22662 }
22663
22664 if (lose1 && lose2)
22665 /* We've failed to deduce something in either direction.
22666 These must be unordered. */
22667 break;
22668
22669 next:
22670
22671 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
22672 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
22673 /* We have already processed all of the arguments in our
22674 handing of the pack expansion type. */
22675 len = 0;
22676
22677 args1 = TREE_CHAIN (args1);
22678 args2 = TREE_CHAIN (args2);
22679 }
22680
22681 /* "In most cases, all template parameters must have values in order for
22682 deduction to succeed, but for partial ordering purposes a template
22683 parameter may remain without a value provided it is not used in the
22684 types being used for partial ordering."
22685
22686 Thus, if we are missing any of the targs1 we need to substitute into
22687 origs1, then pat2 is not as specialized as pat1. This can happen when
22688 there is a nondeduced context. */
22689 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
22690 lose2 = true;
22691 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
22692 lose1 = true;
22693
22694 processing_template_decl--;
22695
22696 /* If both deductions succeed, the partial ordering selects the more
22697 constrained template. */
22698 if (!lose1 && !lose2)
22699 {
22700 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
22701 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
22702 lose1 = !subsumes_constraints (c1, c2);
22703 lose2 = !subsumes_constraints (c2, c1);
22704 }
22705
22706 /* All things being equal, if the next argument is a pack expansion
22707 for one function but not for the other, prefer the
22708 non-variadic function. FIXME this is bogus; see c++/41958. */
22709 if (lose1 == lose2
22710 && args1 && TREE_VALUE (args1)
22711 && args2 && TREE_VALUE (args2))
22712 {
22713 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
22714 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
22715 }
22716
22717 if (lose1 == lose2)
22718 return 0;
22719 else if (!lose1)
22720 return 1;
22721 else
22722 return -1;
22723 }
22724
22725 /* Determine which of two partial specializations of TMPL is more
22726 specialized.
22727
22728 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
22729 to the first partial specialization. The TREE_PURPOSE is the
22730 innermost set of template parameters for the partial
22731 specialization. PAT2 is similar, but for the second template.
22732
22733 Return 1 if the first partial specialization is more specialized;
22734 -1 if the second is more specialized; 0 if neither is more
22735 specialized.
22736
22737 See [temp.class.order] for information about determining which of
22738 two templates is more specialized. */
22739
22740 static int
22741 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
22742 {
22743 tree targs;
22744 int winner = 0;
22745 bool any_deductions = false;
22746
22747 tree tmpl1 = TREE_VALUE (pat1);
22748 tree tmpl2 = TREE_VALUE (pat2);
22749 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
22750 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
22751
22752 /* Just like what happens for functions, if we are ordering between
22753 different template specializations, we may encounter dependent
22754 types in the arguments, and we need our dependency check functions
22755 to behave correctly. */
22756 ++processing_template_decl;
22757 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
22758 if (targs)
22759 {
22760 --winner;
22761 any_deductions = true;
22762 }
22763
22764 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
22765 if (targs)
22766 {
22767 ++winner;
22768 any_deductions = true;
22769 }
22770 --processing_template_decl;
22771
22772 /* If both deductions succeed, the partial ordering selects the more
22773 constrained template. */
22774 if (!winner && any_deductions)
22775 return more_constrained (tmpl1, tmpl2);
22776
22777 /* In the case of a tie where at least one of the templates
22778 has a parameter pack at the end, the template with the most
22779 non-packed parameters wins. */
22780 if (winner == 0
22781 && any_deductions
22782 && (template_args_variadic_p (TREE_PURPOSE (pat1))
22783 || template_args_variadic_p (TREE_PURPOSE (pat2))))
22784 {
22785 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
22786 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
22787 int len1 = TREE_VEC_LENGTH (args1);
22788 int len2 = TREE_VEC_LENGTH (args2);
22789
22790 /* We don't count the pack expansion at the end. */
22791 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
22792 --len1;
22793 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
22794 --len2;
22795
22796 if (len1 > len2)
22797 return 1;
22798 else if (len1 < len2)
22799 return -1;
22800 }
22801
22802 return winner;
22803 }
22804
22805 /* Return the template arguments that will produce the function signature
22806 DECL from the function template FN, with the explicit template
22807 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
22808 also match. Return NULL_TREE if no satisfactory arguments could be
22809 found. */
22810
22811 static tree
22812 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
22813 {
22814 int ntparms = DECL_NTPARMS (fn);
22815 tree targs = make_tree_vec (ntparms);
22816 tree decl_type = TREE_TYPE (decl);
22817 tree decl_arg_types;
22818 tree *args;
22819 unsigned int nargs, ix;
22820 tree arg;
22821
22822 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
22823
22824 /* Never do unification on the 'this' parameter. */
22825 decl_arg_types = skip_artificial_parms_for (decl,
22826 TYPE_ARG_TYPES (decl_type));
22827
22828 nargs = list_length (decl_arg_types);
22829 args = XALLOCAVEC (tree, nargs);
22830 for (arg = decl_arg_types, ix = 0;
22831 arg != NULL_TREE && arg != void_list_node;
22832 arg = TREE_CHAIN (arg), ++ix)
22833 args[ix] = TREE_VALUE (arg);
22834
22835 if (fn_type_unification (fn, explicit_args, targs,
22836 args, ix,
22837 (check_rettype || DECL_CONV_FN_P (fn)
22838 ? TREE_TYPE (decl_type) : NULL_TREE),
22839 DEDUCE_EXACT, LOOKUP_NORMAL, NULL,
22840 /*explain_p=*/false,
22841 /*decltype*/false)
22842 == error_mark_node)
22843 return NULL_TREE;
22844
22845 return targs;
22846 }
22847
22848 /* Return the innermost template arguments that, when applied to a partial
22849 specialization SPEC_TMPL of TMPL, yield the ARGS.
22850
22851 For example, suppose we have:
22852
22853 template <class T, class U> struct S {};
22854 template <class T> struct S<T*, int> {};
22855
22856 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
22857 partial specialization and the ARGS will be {double*, int}. The resulting
22858 vector will be {double}, indicating that `T' is bound to `double'. */
22859
22860 static tree
22861 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
22862 {
22863 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
22864 tree spec_args
22865 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
22866 int i, ntparms = TREE_VEC_LENGTH (tparms);
22867 tree deduced_args;
22868 tree innermost_deduced_args;
22869
22870 innermost_deduced_args = make_tree_vec (ntparms);
22871 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
22872 {
22873 deduced_args = copy_node (args);
22874 SET_TMPL_ARGS_LEVEL (deduced_args,
22875 TMPL_ARGS_DEPTH (deduced_args),
22876 innermost_deduced_args);
22877 }
22878 else
22879 deduced_args = innermost_deduced_args;
22880
22881 bool tried_array_deduction = (cxx_dialect < cxx17);
22882 again:
22883 if (unify (tparms, deduced_args,
22884 INNERMOST_TEMPLATE_ARGS (spec_args),
22885 INNERMOST_TEMPLATE_ARGS (args),
22886 UNIFY_ALLOW_NONE, /*explain_p=*/false))
22887 return NULL_TREE;
22888
22889 for (i = 0; i < ntparms; ++i)
22890 if (! TREE_VEC_ELT (innermost_deduced_args, i))
22891 {
22892 if (!tried_array_deduction)
22893 {
22894 try_array_deduction (tparms, innermost_deduced_args,
22895 INNERMOST_TEMPLATE_ARGS (spec_args));
22896 tried_array_deduction = true;
22897 if (TREE_VEC_ELT (innermost_deduced_args, i))
22898 goto again;
22899 }
22900 return NULL_TREE;
22901 }
22902
22903 if (!push_tinst_level (spec_tmpl, deduced_args))
22904 {
22905 excessive_deduction_depth = true;
22906 return NULL_TREE;
22907 }
22908
22909 /* Verify that nondeduced template arguments agree with the type
22910 obtained from argument deduction.
22911
22912 For example:
22913
22914 struct A { typedef int X; };
22915 template <class T, class U> struct C {};
22916 template <class T> struct C<T, typename T::X> {};
22917
22918 Then with the instantiation `C<A, int>', we can deduce that
22919 `T' is `A' but unify () does not check whether `typename T::X'
22920 is `int'. */
22921 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
22922
22923 if (spec_args != error_mark_node)
22924 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
22925 INNERMOST_TEMPLATE_ARGS (spec_args),
22926 tmpl, tf_none, false, false);
22927
22928 pop_tinst_level ();
22929
22930 if (spec_args == error_mark_node
22931 /* We only need to check the innermost arguments; the other
22932 arguments will always agree. */
22933 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
22934 INNERMOST_TEMPLATE_ARGS (args)))
22935 return NULL_TREE;
22936
22937 /* Now that we have bindings for all of the template arguments,
22938 ensure that the arguments deduced for the template template
22939 parameters have compatible template parameter lists. See the use
22940 of template_template_parm_bindings_ok_p in fn_type_unification
22941 for more information. */
22942 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
22943 return NULL_TREE;
22944
22945 return deduced_args;
22946 }
22947
22948 // Compare two function templates T1 and T2 by deducing bindings
22949 // from one against the other. If both deductions succeed, compare
22950 // constraints to see which is more constrained.
22951 static int
22952 more_specialized_inst (tree t1, tree t2)
22953 {
22954 int fate = 0;
22955 int count = 0;
22956
22957 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
22958 {
22959 --fate;
22960 ++count;
22961 }
22962
22963 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
22964 {
22965 ++fate;
22966 ++count;
22967 }
22968
22969 // If both deductions succeed, then one may be more constrained.
22970 if (count == 2 && fate == 0)
22971 fate = more_constrained (t1, t2);
22972
22973 return fate;
22974 }
22975
22976 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
22977 Return the TREE_LIST node with the most specialized template, if
22978 any. If there is no most specialized template, the error_mark_node
22979 is returned.
22980
22981 Note that this function does not look at, or modify, the
22982 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
22983 returned is one of the elements of INSTANTIATIONS, callers may
22984 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
22985 and retrieve it from the value returned. */
22986
22987 tree
22988 most_specialized_instantiation (tree templates)
22989 {
22990 tree fn, champ;
22991
22992 ++processing_template_decl;
22993
22994 champ = templates;
22995 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
22996 {
22997 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
22998 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
22999 if (fate == -1)
23000 champ = fn;
23001 else if (!fate)
23002 {
23003 /* Equally specialized, move to next function. If there
23004 is no next function, nothing's most specialized. */
23005 fn = TREE_CHAIN (fn);
23006 champ = fn;
23007 if (!fn)
23008 break;
23009 }
23010 }
23011
23012 if (champ)
23013 /* Now verify that champ is better than everything earlier in the
23014 instantiation list. */
23015 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
23016 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
23017 {
23018 champ = NULL_TREE;
23019 break;
23020 }
23021 }
23022
23023 processing_template_decl--;
23024
23025 if (!champ)
23026 return error_mark_node;
23027
23028 return champ;
23029 }
23030
23031 /* If DECL is a specialization of some template, return the most
23032 general such template. Otherwise, returns NULL_TREE.
23033
23034 For example, given:
23035
23036 template <class T> struct S { template <class U> void f(U); };
23037
23038 if TMPL is `template <class U> void S<int>::f(U)' this will return
23039 the full template. This function will not trace past partial
23040 specializations, however. For example, given in addition:
23041
23042 template <class T> struct S<T*> { template <class U> void f(U); };
23043
23044 if TMPL is `template <class U> void S<int*>::f(U)' this will return
23045 `template <class T> template <class U> S<T*>::f(U)'. */
23046
23047 tree
23048 most_general_template (tree decl)
23049 {
23050 if (TREE_CODE (decl) != TEMPLATE_DECL)
23051 {
23052 if (tree tinfo = get_template_info (decl))
23053 decl = TI_TEMPLATE (tinfo);
23054 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
23055 template friend, or a FIELD_DECL for a capture pack. */
23056 if (TREE_CODE (decl) != TEMPLATE_DECL)
23057 return NULL_TREE;
23058 }
23059
23060 /* Look for more and more general templates. */
23061 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
23062 {
23063 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
23064 (See cp-tree.h for details.) */
23065 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
23066 break;
23067
23068 if (CLASS_TYPE_P (TREE_TYPE (decl))
23069 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
23070 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
23071 break;
23072
23073 /* Stop if we run into an explicitly specialized class template. */
23074 if (!DECL_NAMESPACE_SCOPE_P (decl)
23075 && DECL_CONTEXT (decl)
23076 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
23077 break;
23078
23079 decl = DECL_TI_TEMPLATE (decl);
23080 }
23081
23082 return decl;
23083 }
23084
23085 /* Return the most specialized of the template partial specializations
23086 which can produce TARGET, a specialization of some class or variable
23087 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
23088 a TEMPLATE_DECL node corresponding to the partial specialization, while
23089 the TREE_PURPOSE is the set of template arguments that must be
23090 substituted into the template pattern in order to generate TARGET.
23091
23092 If the choice of partial specialization is ambiguous, a diagnostic
23093 is issued, and the error_mark_node is returned. If there are no
23094 partial specializations matching TARGET, then NULL_TREE is
23095 returned, indicating that the primary template should be used. */
23096
23097 static tree
23098 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
23099 {
23100 tree list = NULL_TREE;
23101 tree t;
23102 tree champ;
23103 int fate;
23104 bool ambiguous_p;
23105 tree outer_args = NULL_TREE;
23106 tree tmpl, args;
23107
23108 if (TYPE_P (target))
23109 {
23110 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
23111 tmpl = TI_TEMPLATE (tinfo);
23112 args = TI_ARGS (tinfo);
23113 }
23114 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
23115 {
23116 tmpl = TREE_OPERAND (target, 0);
23117 args = TREE_OPERAND (target, 1);
23118 }
23119 else if (VAR_P (target))
23120 {
23121 tree tinfo = DECL_TEMPLATE_INFO (target);
23122 tmpl = TI_TEMPLATE (tinfo);
23123 args = TI_ARGS (tinfo);
23124 }
23125 else
23126 gcc_unreachable ();
23127
23128 tree main_tmpl = most_general_template (tmpl);
23129
23130 /* For determining which partial specialization to use, only the
23131 innermost args are interesting. */
23132 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
23133 {
23134 outer_args = strip_innermost_template_args (args, 1);
23135 args = INNERMOST_TEMPLATE_ARGS (args);
23136 }
23137
23138 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
23139 {
23140 tree spec_args;
23141 tree spec_tmpl = TREE_VALUE (t);
23142
23143 if (outer_args)
23144 {
23145 /* Substitute in the template args from the enclosing class. */
23146 ++processing_template_decl;
23147 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
23148 --processing_template_decl;
23149 }
23150
23151 if (spec_tmpl == error_mark_node)
23152 return error_mark_node;
23153
23154 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
23155 if (spec_args)
23156 {
23157 if (outer_args)
23158 spec_args = add_to_template_args (outer_args, spec_args);
23159
23160 /* Keep the candidate only if the constraints are satisfied,
23161 or if we're not compiling with concepts. */
23162 if (!flag_concepts
23163 || constraints_satisfied_p (spec_tmpl, spec_args))
23164 {
23165 list = tree_cons (spec_args, TREE_VALUE (t), list);
23166 TREE_TYPE (list) = TREE_TYPE (t);
23167 }
23168 }
23169 }
23170
23171 if (! list)
23172 return NULL_TREE;
23173
23174 ambiguous_p = false;
23175 t = list;
23176 champ = t;
23177 t = TREE_CHAIN (t);
23178 for (; t; t = TREE_CHAIN (t))
23179 {
23180 fate = more_specialized_partial_spec (tmpl, champ, t);
23181 if (fate == 1)
23182 ;
23183 else
23184 {
23185 if (fate == 0)
23186 {
23187 t = TREE_CHAIN (t);
23188 if (! t)
23189 {
23190 ambiguous_p = true;
23191 break;
23192 }
23193 }
23194 champ = t;
23195 }
23196 }
23197
23198 if (!ambiguous_p)
23199 for (t = list; t && t != champ; t = TREE_CHAIN (t))
23200 {
23201 fate = more_specialized_partial_spec (tmpl, champ, t);
23202 if (fate != 1)
23203 {
23204 ambiguous_p = true;
23205 break;
23206 }
23207 }
23208
23209 if (ambiguous_p)
23210 {
23211 const char *str;
23212 char *spaces = NULL;
23213 if (!(complain & tf_error))
23214 return error_mark_node;
23215 if (TYPE_P (target))
23216 error ("ambiguous template instantiation for %q#T", target);
23217 else
23218 error ("ambiguous template instantiation for %q#D", target);
23219 str = ngettext ("candidate is:", "candidates are:", list_length (list));
23220 for (t = list; t; t = TREE_CHAIN (t))
23221 {
23222 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
23223 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
23224 "%s %#qS", spaces ? spaces : str, subst);
23225 spaces = spaces ? spaces : get_spaces (str);
23226 }
23227 free (spaces);
23228 return error_mark_node;
23229 }
23230
23231 return champ;
23232 }
23233
23234 /* Explicitly instantiate DECL. */
23235
23236 void
23237 do_decl_instantiation (tree decl, tree storage)
23238 {
23239 tree result = NULL_TREE;
23240 int extern_p = 0;
23241
23242 if (!decl || decl == error_mark_node)
23243 /* An error occurred, for which grokdeclarator has already issued
23244 an appropriate message. */
23245 return;
23246 else if (! DECL_LANG_SPECIFIC (decl))
23247 {
23248 error ("explicit instantiation of non-template %q#D", decl);
23249 return;
23250 }
23251 else if (DECL_DECLARED_CONCEPT_P (decl))
23252 {
23253 if (VAR_P (decl))
23254 error ("explicit instantiation of variable concept %q#D", decl);
23255 else
23256 error ("explicit instantiation of function concept %q#D", decl);
23257 return;
23258 }
23259
23260 bool var_templ = (DECL_TEMPLATE_INFO (decl)
23261 && variable_template_p (DECL_TI_TEMPLATE (decl)));
23262
23263 if (VAR_P (decl) && !var_templ)
23264 {
23265 /* There is an asymmetry here in the way VAR_DECLs and
23266 FUNCTION_DECLs are handled by grokdeclarator. In the case of
23267 the latter, the DECL we get back will be marked as a
23268 template instantiation, and the appropriate
23269 DECL_TEMPLATE_INFO will be set up. This does not happen for
23270 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
23271 should handle VAR_DECLs as it currently handles
23272 FUNCTION_DECLs. */
23273 if (!DECL_CLASS_SCOPE_P (decl))
23274 {
23275 error ("%qD is not a static data member of a class template", decl);
23276 return;
23277 }
23278 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
23279 if (!result || !VAR_P (result))
23280 {
23281 error ("no matching template for %qD found", decl);
23282 return;
23283 }
23284 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
23285 {
23286 error ("type %qT for explicit instantiation %qD does not match "
23287 "declared type %qT", TREE_TYPE (result), decl,
23288 TREE_TYPE (decl));
23289 return;
23290 }
23291 }
23292 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
23293 {
23294 error ("explicit instantiation of %q#D", decl);
23295 return;
23296 }
23297 else
23298 result = decl;
23299
23300 /* Check for various error cases. Note that if the explicit
23301 instantiation is valid the RESULT will currently be marked as an
23302 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
23303 until we get here. */
23304
23305 if (DECL_TEMPLATE_SPECIALIZATION (result))
23306 {
23307 /* DR 259 [temp.spec].
23308
23309 Both an explicit instantiation and a declaration of an explicit
23310 specialization shall not appear in a program unless the explicit
23311 instantiation follows a declaration of the explicit specialization.
23312
23313 For a given set of template parameters, if an explicit
23314 instantiation of a template appears after a declaration of an
23315 explicit specialization for that template, the explicit
23316 instantiation has no effect. */
23317 return;
23318 }
23319 else if (DECL_EXPLICIT_INSTANTIATION (result))
23320 {
23321 /* [temp.spec]
23322
23323 No program shall explicitly instantiate any template more
23324 than once.
23325
23326 We check DECL_NOT_REALLY_EXTERN so as not to complain when
23327 the first instantiation was `extern' and the second is not,
23328 and EXTERN_P for the opposite case. */
23329 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
23330 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
23331 /* If an "extern" explicit instantiation follows an ordinary
23332 explicit instantiation, the template is instantiated. */
23333 if (extern_p)
23334 return;
23335 }
23336 else if (!DECL_IMPLICIT_INSTANTIATION (result))
23337 {
23338 error ("no matching template for %qD found", result);
23339 return;
23340 }
23341 else if (!DECL_TEMPLATE_INFO (result))
23342 {
23343 permerror (input_location, "explicit instantiation of non-template %q#D", result);
23344 return;
23345 }
23346
23347 if (storage == NULL_TREE)
23348 ;
23349 else if (storage == ridpointers[(int) RID_EXTERN])
23350 {
23351 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
23352 pedwarn (input_location, OPT_Wpedantic,
23353 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
23354 "instantiations");
23355 extern_p = 1;
23356 }
23357 else
23358 error ("storage class %qD applied to template instantiation", storage);
23359
23360 check_explicit_instantiation_namespace (result);
23361 mark_decl_instantiated (result, extern_p);
23362 if (! extern_p)
23363 instantiate_decl (result, /*defer_ok=*/true,
23364 /*expl_inst_class_mem_p=*/false);
23365 }
23366
23367 static void
23368 mark_class_instantiated (tree t, int extern_p)
23369 {
23370 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
23371 SET_CLASSTYPE_INTERFACE_KNOWN (t);
23372 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
23373 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
23374 if (! extern_p)
23375 {
23376 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
23377 rest_of_type_compilation (t, 1);
23378 }
23379 }
23380
23381 /* Called from do_type_instantiation through binding_table_foreach to
23382 do recursive instantiation for the type bound in ENTRY. */
23383 static void
23384 bt_instantiate_type_proc (binding_entry entry, void *data)
23385 {
23386 tree storage = *(tree *) data;
23387
23388 if (MAYBE_CLASS_TYPE_P (entry->type)
23389 && CLASSTYPE_TEMPLATE_INFO (entry->type)
23390 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
23391 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
23392 }
23393
23394 /* Perform an explicit instantiation of template class T. STORAGE, if
23395 non-null, is the RID for extern, inline or static. COMPLAIN is
23396 nonzero if this is called from the parser, zero if called recursively,
23397 since the standard is unclear (as detailed below). */
23398
23399 void
23400 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
23401 {
23402 int extern_p = 0;
23403 int nomem_p = 0;
23404 int static_p = 0;
23405 int previous_instantiation_extern_p = 0;
23406
23407 if (TREE_CODE (t) == TYPE_DECL)
23408 t = TREE_TYPE (t);
23409
23410 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
23411 {
23412 tree tmpl =
23413 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
23414 if (tmpl)
23415 error ("explicit instantiation of non-class template %qD", tmpl);
23416 else
23417 error ("explicit instantiation of non-template type %qT", t);
23418 return;
23419 }
23420
23421 complete_type (t);
23422
23423 if (!COMPLETE_TYPE_P (t))
23424 {
23425 if (complain & tf_error)
23426 error ("explicit instantiation of %q#T before definition of template",
23427 t);
23428 return;
23429 }
23430
23431 if (storage != NULL_TREE)
23432 {
23433 if (!in_system_header_at (input_location))
23434 {
23435 if (storage == ridpointers[(int) RID_EXTERN])
23436 {
23437 if (cxx_dialect == cxx98)
23438 pedwarn (input_location, OPT_Wpedantic,
23439 "ISO C++ 1998 forbids the use of %<extern%> on "
23440 "explicit instantiations");
23441 }
23442 else
23443 pedwarn (input_location, OPT_Wpedantic,
23444 "ISO C++ forbids the use of %qE"
23445 " on explicit instantiations", storage);
23446 }
23447
23448 if (storage == ridpointers[(int) RID_INLINE])
23449 nomem_p = 1;
23450 else if (storage == ridpointers[(int) RID_EXTERN])
23451 extern_p = 1;
23452 else if (storage == ridpointers[(int) RID_STATIC])
23453 static_p = 1;
23454 else
23455 {
23456 error ("storage class %qD applied to template instantiation",
23457 storage);
23458 extern_p = 0;
23459 }
23460 }
23461
23462 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
23463 {
23464 /* DR 259 [temp.spec].
23465
23466 Both an explicit instantiation and a declaration of an explicit
23467 specialization shall not appear in a program unless the explicit
23468 instantiation follows a declaration of the explicit specialization.
23469
23470 For a given set of template parameters, if an explicit
23471 instantiation of a template appears after a declaration of an
23472 explicit specialization for that template, the explicit
23473 instantiation has no effect. */
23474 return;
23475 }
23476 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
23477 {
23478 /* [temp.spec]
23479
23480 No program shall explicitly instantiate any template more
23481 than once.
23482
23483 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
23484 instantiation was `extern'. If EXTERN_P then the second is.
23485 These cases are OK. */
23486 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
23487
23488 if (!previous_instantiation_extern_p && !extern_p
23489 && (complain & tf_error))
23490 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
23491
23492 /* If we've already instantiated the template, just return now. */
23493 if (!CLASSTYPE_INTERFACE_ONLY (t))
23494 return;
23495 }
23496
23497 check_explicit_instantiation_namespace (TYPE_NAME (t));
23498 mark_class_instantiated (t, extern_p);
23499
23500 if (nomem_p)
23501 return;
23502
23503 /* In contrast to implicit instantiation, where only the
23504 declarations, and not the definitions, of members are
23505 instantiated, we have here:
23506
23507 [temp.explicit]
23508
23509 The explicit instantiation of a class template specialization
23510 implies the instantiation of all of its members not
23511 previously explicitly specialized in the translation unit
23512 containing the explicit instantiation.
23513
23514 Of course, we can't instantiate member template classes, since we
23515 don't have any arguments for them. Note that the standard is
23516 unclear on whether the instantiation of the members are
23517 *explicit* instantiations or not. However, the most natural
23518 interpretation is that it should be an explicit
23519 instantiation. */
23520 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
23521 if ((VAR_P (fld)
23522 || (TREE_CODE (fld) == FUNCTION_DECL
23523 && !static_p
23524 && user_provided_p (fld)))
23525 && DECL_TEMPLATE_INSTANTIATION (fld))
23526 {
23527 mark_decl_instantiated (fld, extern_p);
23528 if (! extern_p)
23529 instantiate_decl (fld, /*defer_ok=*/true,
23530 /*expl_inst_class_mem_p=*/true);
23531 }
23532
23533 if (CLASSTYPE_NESTED_UTDS (t))
23534 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
23535 bt_instantiate_type_proc, &storage);
23536 }
23537
23538 /* Given a function DECL, which is a specialization of TMPL, modify
23539 DECL to be a re-instantiation of TMPL with the same template
23540 arguments. TMPL should be the template into which tsubst'ing
23541 should occur for DECL, not the most general template.
23542
23543 One reason for doing this is a scenario like this:
23544
23545 template <class T>
23546 void f(const T&, int i);
23547
23548 void g() { f(3, 7); }
23549
23550 template <class T>
23551 void f(const T& t, const int i) { }
23552
23553 Note that when the template is first instantiated, with
23554 instantiate_template, the resulting DECL will have no name for the
23555 first parameter, and the wrong type for the second. So, when we go
23556 to instantiate the DECL, we regenerate it. */
23557
23558 static void
23559 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
23560 {
23561 /* The arguments used to instantiate DECL, from the most general
23562 template. */
23563 tree code_pattern;
23564
23565 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
23566
23567 /* Make sure that we can see identifiers, and compute access
23568 correctly. */
23569 push_access_scope (decl);
23570
23571 if (TREE_CODE (decl) == FUNCTION_DECL)
23572 {
23573 tree decl_parm;
23574 tree pattern_parm;
23575 tree specs;
23576 int args_depth;
23577 int parms_depth;
23578
23579 args_depth = TMPL_ARGS_DEPTH (args);
23580 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
23581 if (args_depth > parms_depth)
23582 args = get_innermost_template_args (args, parms_depth);
23583
23584 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
23585 args, tf_error, NULL_TREE,
23586 /*defer_ok*/false);
23587 if (specs && specs != error_mark_node)
23588 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
23589 specs);
23590
23591 /* Merge parameter declarations. */
23592 decl_parm = skip_artificial_parms_for (decl,
23593 DECL_ARGUMENTS (decl));
23594 pattern_parm
23595 = skip_artificial_parms_for (code_pattern,
23596 DECL_ARGUMENTS (code_pattern));
23597 while (decl_parm && !DECL_PACK_P (pattern_parm))
23598 {
23599 tree parm_type;
23600 tree attributes;
23601
23602 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
23603 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
23604 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
23605 NULL_TREE);
23606 parm_type = type_decays_to (parm_type);
23607 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
23608 TREE_TYPE (decl_parm) = parm_type;
23609 attributes = DECL_ATTRIBUTES (pattern_parm);
23610 if (DECL_ATTRIBUTES (decl_parm) != attributes)
23611 {
23612 DECL_ATTRIBUTES (decl_parm) = attributes;
23613 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
23614 }
23615 decl_parm = DECL_CHAIN (decl_parm);
23616 pattern_parm = DECL_CHAIN (pattern_parm);
23617 }
23618 /* Merge any parameters that match with the function parameter
23619 pack. */
23620 if (pattern_parm && DECL_PACK_P (pattern_parm))
23621 {
23622 int i, len;
23623 tree expanded_types;
23624 /* Expand the TYPE_PACK_EXPANSION that provides the types for
23625 the parameters in this function parameter pack. */
23626 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
23627 args, tf_error, NULL_TREE);
23628 len = TREE_VEC_LENGTH (expanded_types);
23629 for (i = 0; i < len; i++)
23630 {
23631 tree parm_type;
23632 tree attributes;
23633
23634 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
23635 /* Rename the parameter to include the index. */
23636 DECL_NAME (decl_parm) =
23637 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
23638 parm_type = TREE_VEC_ELT (expanded_types, i);
23639 parm_type = type_decays_to (parm_type);
23640 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
23641 TREE_TYPE (decl_parm) = parm_type;
23642 attributes = DECL_ATTRIBUTES (pattern_parm);
23643 if (DECL_ATTRIBUTES (decl_parm) != attributes)
23644 {
23645 DECL_ATTRIBUTES (decl_parm) = attributes;
23646 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
23647 }
23648 decl_parm = DECL_CHAIN (decl_parm);
23649 }
23650 }
23651 /* Merge additional specifiers from the CODE_PATTERN. */
23652 if (DECL_DECLARED_INLINE_P (code_pattern)
23653 && !DECL_DECLARED_INLINE_P (decl))
23654 DECL_DECLARED_INLINE_P (decl) = 1;
23655 }
23656 else if (VAR_P (decl))
23657 {
23658 start_lambda_scope (decl);
23659 DECL_INITIAL (decl) =
23660 tsubst_expr (DECL_INITIAL (code_pattern), args,
23661 tf_error, DECL_TI_TEMPLATE (decl),
23662 /*integral_constant_expression_p=*/false);
23663 finish_lambda_scope ();
23664 if (VAR_HAD_UNKNOWN_BOUND (decl))
23665 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
23666 tf_error, DECL_TI_TEMPLATE (decl));
23667 }
23668 else
23669 gcc_unreachable ();
23670
23671 pop_access_scope (decl);
23672 }
23673
23674 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
23675 substituted to get DECL. */
23676
23677 tree
23678 template_for_substitution (tree decl)
23679 {
23680 tree tmpl = DECL_TI_TEMPLATE (decl);
23681
23682 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
23683 for the instantiation. This is not always the most general
23684 template. Consider, for example:
23685
23686 template <class T>
23687 struct S { template <class U> void f();
23688 template <> void f<int>(); };
23689
23690 and an instantiation of S<double>::f<int>. We want TD to be the
23691 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
23692 while (/* An instantiation cannot have a definition, so we need a
23693 more general template. */
23694 DECL_TEMPLATE_INSTANTIATION (tmpl)
23695 /* We must also deal with friend templates. Given:
23696
23697 template <class T> struct S {
23698 template <class U> friend void f() {};
23699 };
23700
23701 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
23702 so far as the language is concerned, but that's still
23703 where we get the pattern for the instantiation from. On
23704 other hand, if the definition comes outside the class, say:
23705
23706 template <class T> struct S {
23707 template <class U> friend void f();
23708 };
23709 template <class U> friend void f() {}
23710
23711 we don't need to look any further. That's what the check for
23712 DECL_INITIAL is for. */
23713 || (TREE_CODE (decl) == FUNCTION_DECL
23714 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
23715 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
23716 {
23717 /* The present template, TD, should not be a definition. If it
23718 were a definition, we should be using it! Note that we
23719 cannot restructure the loop to just keep going until we find
23720 a template with a definition, since that might go too far if
23721 a specialization was declared, but not defined. */
23722
23723 /* Fetch the more general template. */
23724 tmpl = DECL_TI_TEMPLATE (tmpl);
23725 }
23726
23727 return tmpl;
23728 }
23729
23730 /* Returns true if we need to instantiate this template instance even if we
23731 know we aren't going to emit it. */
23732
23733 bool
23734 always_instantiate_p (tree decl)
23735 {
23736 /* We always instantiate inline functions so that we can inline them. An
23737 explicit instantiation declaration prohibits implicit instantiation of
23738 non-inline functions. With high levels of optimization, we would
23739 normally inline non-inline functions -- but we're not allowed to do
23740 that for "extern template" functions. Therefore, we check
23741 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
23742 return ((TREE_CODE (decl) == FUNCTION_DECL
23743 && (DECL_DECLARED_INLINE_P (decl)
23744 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
23745 /* And we need to instantiate static data members so that
23746 their initializers are available in integral constant
23747 expressions. */
23748 || (VAR_P (decl)
23749 && decl_maybe_constant_var_p (decl)));
23750 }
23751
23752 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
23753 instantiate it now, modifying TREE_TYPE (fn). Returns false on
23754 error, true otherwise. */
23755
23756 bool
23757 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
23758 {
23759 tree fntype, spec, noex, clone;
23760
23761 /* Don't instantiate a noexcept-specification from template context. */
23762 if (processing_template_decl
23763 && (!flag_noexcept_type || type_dependent_expression_p (fn)))
23764 return true;
23765
23766 if (DECL_CLONED_FUNCTION_P (fn))
23767 fn = DECL_CLONED_FUNCTION (fn);
23768 fntype = TREE_TYPE (fn);
23769 spec = TYPE_RAISES_EXCEPTIONS (fntype);
23770
23771 if (!spec || !TREE_PURPOSE (spec))
23772 return true;
23773
23774 noex = TREE_PURPOSE (spec);
23775
23776 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
23777 {
23778 static hash_set<tree>* fns = new hash_set<tree>;
23779 bool added = false;
23780 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
23781 spec = get_defaulted_eh_spec (fn, complain);
23782 else if (!(added = !fns->add (fn)))
23783 {
23784 /* If hash_set::add returns true, the element was already there. */
23785 location_t loc = cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex),
23786 DECL_SOURCE_LOCATION (fn));
23787 error_at (loc,
23788 "exception specification of %qD depends on itself",
23789 fn);
23790 spec = noexcept_false_spec;
23791 }
23792 else if (push_tinst_level (fn))
23793 {
23794 push_access_scope (fn);
23795 push_deferring_access_checks (dk_no_deferred);
23796 input_location = DECL_SOURCE_LOCATION (fn);
23797 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
23798 DEFERRED_NOEXCEPT_ARGS (noex),
23799 tf_warning_or_error, fn,
23800 /*function_p=*/false,
23801 /*integral_constant_expression_p=*/true);
23802 spec = build_noexcept_spec (noex, tf_warning_or_error);
23803 pop_deferring_access_checks ();
23804 pop_access_scope (fn);
23805 pop_tinst_level ();
23806 if (spec == error_mark_node)
23807 spec = noexcept_false_spec;
23808 }
23809 else
23810 spec = noexcept_false_spec;
23811
23812 if (added)
23813 fns->remove (fn);
23814
23815 if (spec == error_mark_node)
23816 return false;
23817
23818 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
23819 }
23820
23821 FOR_EACH_CLONE (clone, fn)
23822 {
23823 if (TREE_TYPE (clone) == fntype)
23824 TREE_TYPE (clone) = TREE_TYPE (fn);
23825 else
23826 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
23827 }
23828
23829 return true;
23830 }
23831
23832 /* We're starting to process the function INST, an instantiation of PATTERN;
23833 add their parameters to local_specializations. */
23834
23835 static void
23836 register_parameter_specializations (tree pattern, tree inst)
23837 {
23838 tree tmpl_parm = DECL_ARGUMENTS (pattern);
23839 tree spec_parm = DECL_ARGUMENTS (inst);
23840 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
23841 {
23842 register_local_specialization (spec_parm, tmpl_parm);
23843 spec_parm = skip_artificial_parms_for (inst, spec_parm);
23844 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
23845 }
23846 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
23847 {
23848 if (!DECL_PACK_P (tmpl_parm))
23849 {
23850 register_local_specialization (spec_parm, tmpl_parm);
23851 spec_parm = DECL_CHAIN (spec_parm);
23852 }
23853 else
23854 {
23855 /* Register the (value) argument pack as a specialization of
23856 TMPL_PARM, then move on. */
23857 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
23858 register_local_specialization (argpack, tmpl_parm);
23859 }
23860 }
23861 gcc_assert (!spec_parm);
23862 }
23863
23864 /* Produce the definition of D, a _DECL generated from a template. If
23865 DEFER_OK is true, then we don't have to actually do the
23866 instantiation now; we just have to do it sometime. Normally it is
23867 an error if this is an explicit instantiation but D is undefined.
23868 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
23869 instantiated class template. */
23870
23871 tree
23872 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
23873 {
23874 tree tmpl = DECL_TI_TEMPLATE (d);
23875 tree gen_args;
23876 tree args;
23877 tree td;
23878 tree code_pattern;
23879 tree spec;
23880 tree gen_tmpl;
23881 bool pattern_defined;
23882 location_t saved_loc = input_location;
23883 int saved_unevaluated_operand = cp_unevaluated_operand;
23884 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
23885 bool external_p;
23886 bool deleted_p;
23887
23888 /* This function should only be used to instantiate templates for
23889 functions and static member variables. */
23890 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
23891
23892 /* A concept is never instantiated. */
23893 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
23894
23895 /* Variables are never deferred; if instantiation is required, they
23896 are instantiated right away. That allows for better code in the
23897 case that an expression refers to the value of the variable --
23898 if the variable has a constant value the referring expression can
23899 take advantage of that fact. */
23900 if (VAR_P (d))
23901 defer_ok = false;
23902
23903 /* Don't instantiate cloned functions. Instead, instantiate the
23904 functions they cloned. */
23905 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
23906 d = DECL_CLONED_FUNCTION (d);
23907
23908 if (DECL_TEMPLATE_INSTANTIATED (d)
23909 || (TREE_CODE (d) == FUNCTION_DECL
23910 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
23911 || DECL_TEMPLATE_SPECIALIZATION (d))
23912 /* D has already been instantiated or explicitly specialized, so
23913 there's nothing for us to do here.
23914
23915 It might seem reasonable to check whether or not D is an explicit
23916 instantiation, and, if so, stop here. But when an explicit
23917 instantiation is deferred until the end of the compilation,
23918 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
23919 the instantiation. */
23920 return d;
23921
23922 /* Check to see whether we know that this template will be
23923 instantiated in some other file, as with "extern template"
23924 extension. */
23925 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
23926
23927 /* In general, we do not instantiate such templates. */
23928 if (external_p && !always_instantiate_p (d))
23929 return d;
23930
23931 gen_tmpl = most_general_template (tmpl);
23932 gen_args = DECL_TI_ARGS (d);
23933
23934 if (tmpl != gen_tmpl)
23935 /* We should already have the extra args. */
23936 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
23937 == TMPL_ARGS_DEPTH (gen_args));
23938 /* And what's in the hash table should match D. */
23939 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
23940 || spec == NULL_TREE);
23941
23942 /* This needs to happen before any tsubsting. */
23943 if (! push_tinst_level (d))
23944 return d;
23945
23946 timevar_push (TV_TEMPLATE_INST);
23947
23948 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
23949 for the instantiation. */
23950 td = template_for_substitution (d);
23951 args = gen_args;
23952
23953 if (VAR_P (d))
23954 {
23955 /* Look up an explicit specialization, if any. */
23956 tree tid = lookup_template_variable (gen_tmpl, gen_args);
23957 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
23958 if (elt && elt != error_mark_node)
23959 {
23960 td = TREE_VALUE (elt);
23961 args = TREE_PURPOSE (elt);
23962 }
23963 }
23964
23965 code_pattern = DECL_TEMPLATE_RESULT (td);
23966
23967 /* We should never be trying to instantiate a member of a class
23968 template or partial specialization. */
23969 gcc_assert (d != code_pattern);
23970
23971 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
23972 || DECL_TEMPLATE_SPECIALIZATION (td))
23973 /* In the case of a friend template whose definition is provided
23974 outside the class, we may have too many arguments. Drop the
23975 ones we don't need. The same is true for specializations. */
23976 args = get_innermost_template_args
23977 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
23978
23979 if (TREE_CODE (d) == FUNCTION_DECL)
23980 {
23981 deleted_p = DECL_DELETED_FN (code_pattern);
23982 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
23983 && DECL_INITIAL (code_pattern) != error_mark_node)
23984 || DECL_DEFAULTED_FN (code_pattern)
23985 || deleted_p);
23986 }
23987 else
23988 {
23989 deleted_p = false;
23990 if (DECL_CLASS_SCOPE_P (code_pattern))
23991 pattern_defined = (! DECL_IN_AGGR_P (code_pattern)
23992 || DECL_INLINE_VAR_P (code_pattern));
23993 else
23994 pattern_defined = ! DECL_EXTERNAL (code_pattern);
23995 }
23996
23997 /* We may be in the middle of deferred access check. Disable it now. */
23998 push_deferring_access_checks (dk_no_deferred);
23999
24000 /* Unless an explicit instantiation directive has already determined
24001 the linkage of D, remember that a definition is available for
24002 this entity. */
24003 if (pattern_defined
24004 && !DECL_INTERFACE_KNOWN (d)
24005 && !DECL_NOT_REALLY_EXTERN (d))
24006 mark_definable (d);
24007
24008 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
24009 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
24010 input_location = DECL_SOURCE_LOCATION (d);
24011
24012 /* If D is a member of an explicitly instantiated class template,
24013 and no definition is available, treat it like an implicit
24014 instantiation. */
24015 if (!pattern_defined && expl_inst_class_mem_p
24016 && DECL_EXPLICIT_INSTANTIATION (d))
24017 {
24018 /* Leave linkage flags alone on instantiations with anonymous
24019 visibility. */
24020 if (TREE_PUBLIC (d))
24021 {
24022 DECL_NOT_REALLY_EXTERN (d) = 0;
24023 DECL_INTERFACE_KNOWN (d) = 0;
24024 }
24025 SET_DECL_IMPLICIT_INSTANTIATION (d);
24026 }
24027
24028 /* Defer all other templates, unless we have been explicitly
24029 forbidden from doing so. */
24030 if (/* If there is no definition, we cannot instantiate the
24031 template. */
24032 ! pattern_defined
24033 /* If it's OK to postpone instantiation, do so. */
24034 || defer_ok
24035 /* If this is a static data member that will be defined
24036 elsewhere, we don't want to instantiate the entire data
24037 member, but we do want to instantiate the initializer so that
24038 we can substitute that elsewhere. */
24039 || (external_p && VAR_P (d))
24040 /* Handle here a deleted function too, avoid generating
24041 its body (c++/61080). */
24042 || deleted_p)
24043 {
24044 /* The definition of the static data member is now required so
24045 we must substitute the initializer. */
24046 if (VAR_P (d)
24047 && !DECL_INITIAL (d)
24048 && DECL_INITIAL (code_pattern))
24049 {
24050 tree ns;
24051 tree init;
24052 bool const_init = false;
24053 bool enter_context = DECL_CLASS_SCOPE_P (d);
24054
24055 ns = decl_namespace_context (d);
24056 push_nested_namespace (ns);
24057 if (enter_context)
24058 push_nested_class (DECL_CONTEXT (d));
24059 init = tsubst_expr (DECL_INITIAL (code_pattern),
24060 args,
24061 tf_warning_or_error, NULL_TREE,
24062 /*integral_constant_expression_p=*/false);
24063 /* If instantiating the initializer involved instantiating this
24064 again, don't call cp_finish_decl twice. */
24065 if (!DECL_INITIAL (d))
24066 {
24067 /* Make sure the initializer is still constant, in case of
24068 circular dependency (template/instantiate6.C). */
24069 const_init
24070 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
24071 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
24072 /*asmspec_tree=*/NULL_TREE,
24073 LOOKUP_ONLYCONVERTING);
24074 }
24075 if (enter_context)
24076 pop_nested_class ();
24077 pop_nested_namespace (ns);
24078 }
24079
24080 /* We restore the source position here because it's used by
24081 add_pending_template. */
24082 input_location = saved_loc;
24083
24084 if (at_eof && !pattern_defined
24085 && DECL_EXPLICIT_INSTANTIATION (d)
24086 && DECL_NOT_REALLY_EXTERN (d))
24087 /* [temp.explicit]
24088
24089 The definition of a non-exported function template, a
24090 non-exported member function template, or a non-exported
24091 member function or static data member of a class template
24092 shall be present in every translation unit in which it is
24093 explicitly instantiated. */
24094 permerror (input_location, "explicit instantiation of %qD "
24095 "but no definition available", d);
24096
24097 /* If we're in unevaluated context, we just wanted to get the
24098 constant value; this isn't an odr use, so don't queue
24099 a full instantiation. */
24100 if (cp_unevaluated_operand != 0)
24101 goto out;
24102 /* ??? Historically, we have instantiated inline functions, even
24103 when marked as "extern template". */
24104 if (!(external_p && VAR_P (d)))
24105 add_pending_template (d);
24106 goto out;
24107 }
24108 /* Tell the repository that D is available in this translation unit
24109 -- and see if it is supposed to be instantiated here. */
24110 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
24111 {
24112 /* In a PCH file, despite the fact that the repository hasn't
24113 requested instantiation in the PCH it is still possible that
24114 an instantiation will be required in a file that includes the
24115 PCH. */
24116 if (pch_file)
24117 add_pending_template (d);
24118 /* Instantiate inline functions so that the inliner can do its
24119 job, even though we'll not be emitting a copy of this
24120 function. */
24121 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
24122 goto out;
24123 }
24124
24125 bool push_to_top, nested;
24126 tree fn_context;
24127 fn_context = decl_function_context (d);
24128 if (LAMBDA_FUNCTION_P (d))
24129 /* tsubst_lambda_expr resolved any references to enclosing functions. */
24130 fn_context = NULL_TREE;
24131 nested = current_function_decl != NULL_TREE;
24132 push_to_top = !(nested && fn_context == current_function_decl);
24133
24134 vec<tree> omp_privatization_save;
24135 if (nested)
24136 save_omp_privatization_clauses (omp_privatization_save);
24137
24138 if (push_to_top)
24139 push_to_top_level ();
24140 else
24141 {
24142 gcc_assert (!processing_template_decl);
24143 push_function_context ();
24144 cp_unevaluated_operand = 0;
24145 c_inhibit_evaluation_warnings = 0;
24146 }
24147
24148 /* Mark D as instantiated so that recursive calls to
24149 instantiate_decl do not try to instantiate it again. */
24150 DECL_TEMPLATE_INSTANTIATED (d) = 1;
24151
24152 /* Regenerate the declaration in case the template has been modified
24153 by a subsequent redeclaration. */
24154 regenerate_decl_from_template (d, td, args);
24155
24156 /* We already set the file and line above. Reset them now in case
24157 they changed as a result of calling regenerate_decl_from_template. */
24158 input_location = DECL_SOURCE_LOCATION (d);
24159
24160 if (VAR_P (d))
24161 {
24162 tree init;
24163 bool const_init = false;
24164
24165 /* Clear out DECL_RTL; whatever was there before may not be right
24166 since we've reset the type of the declaration. */
24167 SET_DECL_RTL (d, NULL);
24168 DECL_IN_AGGR_P (d) = 0;
24169
24170 /* The initializer is placed in DECL_INITIAL by
24171 regenerate_decl_from_template so we don't need to
24172 push/pop_access_scope again here. Pull it out so that
24173 cp_finish_decl can process it. */
24174 init = DECL_INITIAL (d);
24175 DECL_INITIAL (d) = NULL_TREE;
24176 DECL_INITIALIZED_P (d) = 0;
24177
24178 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
24179 initializer. That function will defer actual emission until
24180 we have a chance to determine linkage. */
24181 DECL_EXTERNAL (d) = 0;
24182
24183 /* Enter the scope of D so that access-checking works correctly. */
24184 bool enter_context = DECL_CLASS_SCOPE_P (d);
24185 if (enter_context)
24186 push_nested_class (DECL_CONTEXT (d));
24187
24188 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
24189 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
24190
24191 if (enter_context)
24192 pop_nested_class ();
24193
24194 if (variable_template_p (gen_tmpl))
24195 note_variable_template_instantiation (d);
24196 }
24197 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
24198 synthesize_method (d);
24199 else if (TREE_CODE (d) == FUNCTION_DECL)
24200 {
24201 /* Set up the list of local specializations. */
24202 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
24203 tree block = NULL_TREE;
24204
24205 /* Set up context. */
24206 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
24207 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
24208 block = push_stmt_list ();
24209 else
24210 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
24211
24212 /* Some typedefs referenced from within the template code need to be
24213 access checked at template instantiation time, i.e now. These
24214 types were added to the template at parsing time. Let's get those
24215 and perform the access checks then. */
24216 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
24217 args);
24218
24219 /* Create substitution entries for the parameters. */
24220 register_parameter_specializations (code_pattern, d);
24221
24222 /* Substitute into the body of the function. */
24223 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
24224 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
24225 tf_warning_or_error, tmpl);
24226 else
24227 {
24228 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
24229 tf_warning_or_error, tmpl,
24230 /*integral_constant_expression_p=*/false);
24231
24232 /* Set the current input_location to the end of the function
24233 so that finish_function knows where we are. */
24234 input_location
24235 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
24236
24237 /* Remember if we saw an infinite loop in the template. */
24238 current_function_infinite_loop
24239 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
24240 }
24241
24242 /* Finish the function. */
24243 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
24244 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
24245 DECL_SAVED_TREE (d) = pop_stmt_list (block);
24246 else
24247 {
24248 d = finish_function (/*inline_p=*/false);
24249 expand_or_defer_fn (d);
24250 }
24251
24252 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
24253 cp_check_omp_declare_reduction (d);
24254 }
24255
24256 /* We're not deferring instantiation any more. */
24257 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
24258
24259 if (push_to_top)
24260 pop_from_top_level ();
24261 else
24262 pop_function_context ();
24263
24264 if (nested)
24265 restore_omp_privatization_clauses (omp_privatization_save);
24266
24267 out:
24268 pop_deferring_access_checks ();
24269 timevar_pop (TV_TEMPLATE_INST);
24270 pop_tinst_level ();
24271 input_location = saved_loc;
24272 cp_unevaluated_operand = saved_unevaluated_operand;
24273 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
24274
24275 return d;
24276 }
24277
24278 /* Run through the list of templates that we wish we could
24279 instantiate, and instantiate any we can. RETRIES is the
24280 number of times we retry pending template instantiation. */
24281
24282 void
24283 instantiate_pending_templates (int retries)
24284 {
24285 int reconsider;
24286 location_t saved_loc = input_location;
24287
24288 /* Instantiating templates may trigger vtable generation. This in turn
24289 may require further template instantiations. We place a limit here
24290 to avoid infinite loop. */
24291 if (pending_templates && retries >= max_tinst_depth)
24292 {
24293 tree decl = pending_templates->tinst->maybe_get_node ();
24294
24295 fatal_error (input_location,
24296 "template instantiation depth exceeds maximum of %d"
24297 " instantiating %q+D, possibly from virtual table generation"
24298 " (use -ftemplate-depth= to increase the maximum)",
24299 max_tinst_depth, decl);
24300 if (TREE_CODE (decl) == FUNCTION_DECL)
24301 /* Pretend that we defined it. */
24302 DECL_INITIAL (decl) = error_mark_node;
24303 return;
24304 }
24305
24306 do
24307 {
24308 struct pending_template **t = &pending_templates;
24309 struct pending_template *last = NULL;
24310 reconsider = 0;
24311 while (*t)
24312 {
24313 tree instantiation = reopen_tinst_level ((*t)->tinst);
24314 bool complete = false;
24315
24316 if (TYPE_P (instantiation))
24317 {
24318 if (!COMPLETE_TYPE_P (instantiation))
24319 {
24320 instantiate_class_template (instantiation);
24321 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
24322 for (tree fld = TYPE_FIELDS (instantiation);
24323 fld; fld = TREE_CHAIN (fld))
24324 if ((VAR_P (fld)
24325 || (TREE_CODE (fld) == FUNCTION_DECL
24326 && !DECL_ARTIFICIAL (fld)))
24327 && DECL_TEMPLATE_INSTANTIATION (fld))
24328 instantiate_decl (fld,
24329 /*defer_ok=*/false,
24330 /*expl_inst_class_mem_p=*/false);
24331
24332 if (COMPLETE_TYPE_P (instantiation))
24333 reconsider = 1;
24334 }
24335
24336 complete = COMPLETE_TYPE_P (instantiation);
24337 }
24338 else
24339 {
24340 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
24341 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
24342 {
24343 instantiation
24344 = instantiate_decl (instantiation,
24345 /*defer_ok=*/false,
24346 /*expl_inst_class_mem_p=*/false);
24347 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
24348 reconsider = 1;
24349 }
24350
24351 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
24352 || DECL_TEMPLATE_INSTANTIATED (instantiation));
24353 }
24354
24355 if (complete)
24356 {
24357 /* If INSTANTIATION has been instantiated, then we don't
24358 need to consider it again in the future. */
24359 struct pending_template *drop = *t;
24360 *t = (*t)->next;
24361 set_refcount_ptr (drop->tinst);
24362 pending_template_freelist ().free (drop);
24363 }
24364 else
24365 {
24366 last = *t;
24367 t = &(*t)->next;
24368 }
24369 tinst_depth = 0;
24370 set_refcount_ptr (current_tinst_level);
24371 }
24372 last_pending_template = last;
24373 }
24374 while (reconsider);
24375
24376 input_location = saved_loc;
24377 }
24378
24379 /* Substitute ARGVEC into T, which is a list of initializers for
24380 either base class or a non-static data member. The TREE_PURPOSEs
24381 are DECLs, and the TREE_VALUEs are the initializer values. Used by
24382 instantiate_decl. */
24383
24384 static tree
24385 tsubst_initializer_list (tree t, tree argvec)
24386 {
24387 tree inits = NULL_TREE;
24388 tree target_ctor = error_mark_node;
24389
24390 for (; t; t = TREE_CHAIN (t))
24391 {
24392 tree decl;
24393 tree init;
24394 tree expanded_bases = NULL_TREE;
24395 tree expanded_arguments = NULL_TREE;
24396 int i, len = 1;
24397
24398 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
24399 {
24400 tree expr;
24401 tree arg;
24402
24403 /* Expand the base class expansion type into separate base
24404 classes. */
24405 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
24406 tf_warning_or_error,
24407 NULL_TREE);
24408 if (expanded_bases == error_mark_node)
24409 continue;
24410
24411 /* We'll be building separate TREE_LISTs of arguments for
24412 each base. */
24413 len = TREE_VEC_LENGTH (expanded_bases);
24414 expanded_arguments = make_tree_vec (len);
24415 for (i = 0; i < len; i++)
24416 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
24417
24418 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
24419 expand each argument in the TREE_VALUE of t. */
24420 expr = make_node (EXPR_PACK_EXPANSION);
24421 PACK_EXPANSION_LOCAL_P (expr) = true;
24422 PACK_EXPANSION_PARAMETER_PACKS (expr) =
24423 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
24424
24425 if (TREE_VALUE (t) == void_type_node)
24426 /* VOID_TYPE_NODE is used to indicate
24427 value-initialization. */
24428 {
24429 for (i = 0; i < len; i++)
24430 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
24431 }
24432 else
24433 {
24434 /* Substitute parameter packs into each argument in the
24435 TREE_LIST. */
24436 in_base_initializer = 1;
24437 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
24438 {
24439 tree expanded_exprs;
24440
24441 /* Expand the argument. */
24442 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
24443 expanded_exprs
24444 = tsubst_pack_expansion (expr, argvec,
24445 tf_warning_or_error,
24446 NULL_TREE);
24447 if (expanded_exprs == error_mark_node)
24448 continue;
24449
24450 /* Prepend each of the expanded expressions to the
24451 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
24452 for (i = 0; i < len; i++)
24453 {
24454 TREE_VEC_ELT (expanded_arguments, i) =
24455 tree_cons (NULL_TREE,
24456 TREE_VEC_ELT (expanded_exprs, i),
24457 TREE_VEC_ELT (expanded_arguments, i));
24458 }
24459 }
24460 in_base_initializer = 0;
24461
24462 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
24463 since we built them backwards. */
24464 for (i = 0; i < len; i++)
24465 {
24466 TREE_VEC_ELT (expanded_arguments, i) =
24467 nreverse (TREE_VEC_ELT (expanded_arguments, i));
24468 }
24469 }
24470 }
24471
24472 for (i = 0; i < len; ++i)
24473 {
24474 if (expanded_bases)
24475 {
24476 decl = TREE_VEC_ELT (expanded_bases, i);
24477 decl = expand_member_init (decl);
24478 init = TREE_VEC_ELT (expanded_arguments, i);
24479 }
24480 else
24481 {
24482 tree tmp;
24483 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
24484 tf_warning_or_error, NULL_TREE);
24485
24486 decl = expand_member_init (decl);
24487 if (decl && !DECL_P (decl))
24488 in_base_initializer = 1;
24489
24490 init = TREE_VALUE (t);
24491 tmp = init;
24492 if (init != void_type_node)
24493 init = tsubst_expr (init, argvec,
24494 tf_warning_or_error, NULL_TREE,
24495 /*integral_constant_expression_p=*/false);
24496 if (init == NULL_TREE && tmp != NULL_TREE)
24497 /* If we had an initializer but it instantiated to nothing,
24498 value-initialize the object. This will only occur when
24499 the initializer was a pack expansion where the parameter
24500 packs used in that expansion were of length zero. */
24501 init = void_type_node;
24502 in_base_initializer = 0;
24503 }
24504
24505 if (target_ctor != error_mark_node
24506 && init != error_mark_node)
24507 {
24508 error ("mem-initializer for %qD follows constructor delegation",
24509 decl);
24510 return inits;
24511 }
24512 /* Look for a target constructor. */
24513 if (init != error_mark_node
24514 && decl && CLASS_TYPE_P (decl)
24515 && same_type_p (decl, current_class_type))
24516 {
24517 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
24518 if (inits)
24519 {
24520 error ("constructor delegation follows mem-initializer for %qD",
24521 TREE_PURPOSE (inits));
24522 continue;
24523 }
24524 target_ctor = init;
24525 }
24526
24527 if (decl)
24528 {
24529 init = build_tree_list (decl, init);
24530 TREE_CHAIN (init) = inits;
24531 inits = init;
24532 }
24533 }
24534 }
24535 return inits;
24536 }
24537
24538 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
24539
24540 static void
24541 set_current_access_from_decl (tree decl)
24542 {
24543 if (TREE_PRIVATE (decl))
24544 current_access_specifier = access_private_node;
24545 else if (TREE_PROTECTED (decl))
24546 current_access_specifier = access_protected_node;
24547 else
24548 current_access_specifier = access_public_node;
24549 }
24550
24551 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
24552 is the instantiation (which should have been created with
24553 start_enum) and ARGS are the template arguments to use. */
24554
24555 static void
24556 tsubst_enum (tree tag, tree newtag, tree args)
24557 {
24558 tree e;
24559
24560 if (SCOPED_ENUM_P (newtag))
24561 begin_scope (sk_scoped_enum, newtag);
24562
24563 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
24564 {
24565 tree value;
24566 tree decl;
24567
24568 decl = TREE_VALUE (e);
24569 /* Note that in a template enum, the TREE_VALUE is the
24570 CONST_DECL, not the corresponding INTEGER_CST. */
24571 value = tsubst_expr (DECL_INITIAL (decl),
24572 args, tf_warning_or_error, NULL_TREE,
24573 /*integral_constant_expression_p=*/true);
24574
24575 /* Give this enumeration constant the correct access. */
24576 set_current_access_from_decl (decl);
24577
24578 /* Actually build the enumerator itself. Here we're assuming that
24579 enumerators can't have dependent attributes. */
24580 build_enumerator (DECL_NAME (decl), value, newtag,
24581 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
24582 }
24583
24584 if (SCOPED_ENUM_P (newtag))
24585 finish_scope ();
24586
24587 finish_enum_value_list (newtag);
24588 finish_enum (newtag);
24589
24590 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
24591 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
24592 }
24593
24594 /* DECL is a FUNCTION_DECL that is a template specialization. Return
24595 its type -- but without substituting the innermost set of template
24596 arguments. So, innermost set of template parameters will appear in
24597 the type. */
24598
24599 tree
24600 get_mostly_instantiated_function_type (tree decl)
24601 {
24602 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
24603 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
24604 }
24605
24606 /* Return truthvalue if we're processing a template different from
24607 the last one involved in diagnostics. */
24608 bool
24609 problematic_instantiation_changed (void)
24610 {
24611 return current_tinst_level != last_error_tinst_level;
24612 }
24613
24614 /* Remember current template involved in diagnostics. */
24615 void
24616 record_last_problematic_instantiation (void)
24617 {
24618 set_refcount_ptr (last_error_tinst_level, current_tinst_level);
24619 }
24620
24621 struct tinst_level *
24622 current_instantiation (void)
24623 {
24624 return current_tinst_level;
24625 }
24626
24627 /* Return TRUE if current_function_decl is being instantiated, false
24628 otherwise. */
24629
24630 bool
24631 instantiating_current_function_p (void)
24632 {
24633 return (current_instantiation ()
24634 && (current_instantiation ()->maybe_get_node ()
24635 == current_function_decl));
24636 }
24637
24638 /* [temp.param] Check that template non-type parm TYPE is of an allowable
24639 type. Return false for ok, true for disallowed. Issue error and
24640 inform messages under control of COMPLAIN. */
24641
24642 static bool
24643 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
24644 {
24645 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
24646 return false;
24647 else if (TYPE_PTR_P (type))
24648 return false;
24649 else if (TYPE_REF_P (type)
24650 && !TYPE_REF_IS_RVALUE (type))
24651 return false;
24652 else if (TYPE_PTRMEM_P (type))
24653 return false;
24654 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
24655 return false;
24656 else if (TREE_CODE (type) == TYPENAME_TYPE)
24657 return false;
24658 else if (TREE_CODE (type) == DECLTYPE_TYPE)
24659 return false;
24660 else if (TREE_CODE (type) == NULLPTR_TYPE)
24661 return false;
24662 /* A bound template template parm could later be instantiated to have a valid
24663 nontype parm type via an alias template. */
24664 else if (cxx_dialect >= cxx11
24665 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
24666 return false;
24667 else if (CLASS_TYPE_P (type))
24668 {
24669 if (cxx_dialect < cxx2a)
24670 {
24671 error ("non-type template parameters of class type only available "
24672 "with -std=c++2a or -std=gnu++2a");
24673 return true;
24674 }
24675 if (!complete_type_or_else (type, NULL_TREE))
24676 return true;
24677 if (!literal_type_p (type))
24678 {
24679 error ("%qT is not a valid type for a template non-type parameter "
24680 "because it is not literal", type);
24681 explain_non_literal_class (type);
24682 return true;
24683 }
24684 if (cp_has_mutable_p (type))
24685 {
24686 error ("%qT is not a valid type for a template non-type parameter "
24687 "because it has a mutable member", type);
24688 return true;
24689 }
24690 /* FIXME check op<=> and strong structural equality once spaceship is
24691 implemented. */
24692 return false;
24693 }
24694
24695 if (complain & tf_error)
24696 {
24697 if (type == error_mark_node)
24698 inform (input_location, "invalid template non-type parameter");
24699 else
24700 error ("%q#T is not a valid type for a template non-type parameter",
24701 type);
24702 }
24703 return true;
24704 }
24705
24706 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
24707 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
24708
24709 static bool
24710 dependent_type_p_r (tree type)
24711 {
24712 tree scope;
24713
24714 /* [temp.dep.type]
24715
24716 A type is dependent if it is:
24717
24718 -- a template parameter. Template template parameters are types
24719 for us (since TYPE_P holds true for them) so we handle
24720 them here. */
24721 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
24722 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
24723 return true;
24724 /* -- a qualified-id with a nested-name-specifier which contains a
24725 class-name that names a dependent type or whose unqualified-id
24726 names a dependent type. */
24727 if (TREE_CODE (type) == TYPENAME_TYPE)
24728 return true;
24729
24730 /* An alias template specialization can be dependent even if the
24731 resulting type is not. */
24732 if (dependent_alias_template_spec_p (type))
24733 return true;
24734
24735 /* -- a cv-qualified type where the cv-unqualified type is
24736 dependent.
24737 No code is necessary for this bullet; the code below handles
24738 cv-qualified types, and we don't want to strip aliases with
24739 TYPE_MAIN_VARIANT because of DR 1558. */
24740 /* -- a compound type constructed from any dependent type. */
24741 if (TYPE_PTRMEM_P (type))
24742 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
24743 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
24744 (type)));
24745 else if (INDIRECT_TYPE_P (type))
24746 return dependent_type_p (TREE_TYPE (type));
24747 else if (TREE_CODE (type) == FUNCTION_TYPE
24748 || TREE_CODE (type) == METHOD_TYPE)
24749 {
24750 tree arg_type;
24751
24752 if (dependent_type_p (TREE_TYPE (type)))
24753 return true;
24754 for (arg_type = TYPE_ARG_TYPES (type);
24755 arg_type;
24756 arg_type = TREE_CHAIN (arg_type))
24757 if (dependent_type_p (TREE_VALUE (arg_type)))
24758 return true;
24759 if (cxx_dialect >= cxx17)
24760 /* A value-dependent noexcept-specifier makes the type dependent. */
24761 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
24762 if (tree noex = TREE_PURPOSE (spec))
24763 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
24764 affect overload resolution and treating it as dependent breaks
24765 things. */
24766 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
24767 && value_dependent_expression_p (noex))
24768 return true;
24769 return false;
24770 }
24771 /* -- an array type constructed from any dependent type or whose
24772 size is specified by a constant expression that is
24773 value-dependent.
24774
24775 We checked for type- and value-dependence of the bounds in
24776 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
24777 if (TREE_CODE (type) == ARRAY_TYPE)
24778 {
24779 if (TYPE_DOMAIN (type)
24780 && dependent_type_p (TYPE_DOMAIN (type)))
24781 return true;
24782 return dependent_type_p (TREE_TYPE (type));
24783 }
24784
24785 /* -- a template-id in which either the template name is a template
24786 parameter ... */
24787 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
24788 return true;
24789 /* ... or any of the template arguments is a dependent type or
24790 an expression that is type-dependent or value-dependent. */
24791 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
24792 && (any_dependent_template_arguments_p
24793 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
24794 return true;
24795
24796 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
24797 dependent; if the argument of the `typeof' expression is not
24798 type-dependent, then it should already been have resolved. */
24799 if (TREE_CODE (type) == TYPEOF_TYPE
24800 || TREE_CODE (type) == DECLTYPE_TYPE
24801 || TREE_CODE (type) == UNDERLYING_TYPE)
24802 return true;
24803
24804 /* A template argument pack is dependent if any of its packed
24805 arguments are. */
24806 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
24807 {
24808 tree args = ARGUMENT_PACK_ARGS (type);
24809 int i, len = TREE_VEC_LENGTH (args);
24810 for (i = 0; i < len; ++i)
24811 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
24812 return true;
24813 }
24814
24815 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
24816 be template parameters. */
24817 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
24818 return true;
24819
24820 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
24821 return true;
24822
24823 /* The standard does not specifically mention types that are local
24824 to template functions or local classes, but they should be
24825 considered dependent too. For example:
24826
24827 template <int I> void f() {
24828 enum E { a = I };
24829 S<sizeof (E)> s;
24830 }
24831
24832 The size of `E' cannot be known until the value of `I' has been
24833 determined. Therefore, `E' must be considered dependent. */
24834 scope = TYPE_CONTEXT (type);
24835 if (scope && TYPE_P (scope))
24836 return dependent_type_p (scope);
24837 /* Don't use type_dependent_expression_p here, as it can lead
24838 to infinite recursion trying to determine whether a lambda
24839 nested in a lambda is dependent (c++/47687). */
24840 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
24841 && DECL_LANG_SPECIFIC (scope)
24842 && DECL_TEMPLATE_INFO (scope)
24843 && (any_dependent_template_arguments_p
24844 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
24845 return true;
24846
24847 /* Other types are non-dependent. */
24848 return false;
24849 }
24850
24851 /* Returns TRUE if TYPE is dependent, in the sense of
24852 [temp.dep.type]. Note that a NULL type is considered dependent. */
24853
24854 bool
24855 dependent_type_p (tree type)
24856 {
24857 /* If there are no template parameters in scope, then there can't be
24858 any dependent types. */
24859 if (!processing_template_decl)
24860 {
24861 /* If we are not processing a template, then nobody should be
24862 providing us with a dependent type. */
24863 gcc_assert (type);
24864 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
24865 return false;
24866 }
24867
24868 /* If the type is NULL, we have not computed a type for the entity
24869 in question; in that case, the type is dependent. */
24870 if (!type)
24871 return true;
24872
24873 /* Erroneous types can be considered non-dependent. */
24874 if (type == error_mark_node)
24875 return false;
24876
24877 /* Getting here with global_type_node means we improperly called this
24878 function on the TREE_TYPE of an IDENTIFIER_NODE. */
24879 gcc_checking_assert (type != global_type_node);
24880
24881 /* If we have not already computed the appropriate value for TYPE,
24882 do so now. */
24883 if (!TYPE_DEPENDENT_P_VALID (type))
24884 {
24885 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
24886 TYPE_DEPENDENT_P_VALID (type) = 1;
24887 }
24888
24889 return TYPE_DEPENDENT_P (type);
24890 }
24891
24892 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
24893 lookup. In other words, a dependent type that is not the current
24894 instantiation. */
24895
24896 bool
24897 dependent_scope_p (tree scope)
24898 {
24899 return (scope && TYPE_P (scope) && dependent_type_p (scope)
24900 && !currently_open_class (scope));
24901 }
24902
24903 /* T is a SCOPE_REF. Return whether it represents a non-static member of
24904 an unknown base of 'this' (and is therefore instantiation-dependent). */
24905
24906 static bool
24907 unknown_base_ref_p (tree t)
24908 {
24909 if (!current_class_ptr)
24910 return false;
24911
24912 tree mem = TREE_OPERAND (t, 1);
24913 if (shared_member_p (mem))
24914 return false;
24915
24916 tree cur = current_nonlambda_class_type ();
24917 if (!any_dependent_bases_p (cur))
24918 return false;
24919
24920 tree ctx = TREE_OPERAND (t, 0);
24921 if (DERIVED_FROM_P (ctx, cur))
24922 return false;
24923
24924 return true;
24925 }
24926
24927 /* T is a SCOPE_REF; return whether we need to consider it
24928 instantiation-dependent so that we can check access at instantiation
24929 time even though we know which member it resolves to. */
24930
24931 static bool
24932 instantiation_dependent_scope_ref_p (tree t)
24933 {
24934 if (DECL_P (TREE_OPERAND (t, 1))
24935 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
24936 && !unknown_base_ref_p (t)
24937 && accessible_in_template_p (TREE_OPERAND (t, 0),
24938 TREE_OPERAND (t, 1)))
24939 return false;
24940 else
24941 return true;
24942 }
24943
24944 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
24945 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
24946 expression. */
24947
24948 /* Note that this predicate is not appropriate for general expressions;
24949 only constant expressions (that satisfy potential_constant_expression)
24950 can be tested for value dependence. */
24951
24952 bool
24953 value_dependent_expression_p (tree expression)
24954 {
24955 if (!processing_template_decl || expression == NULL_TREE)
24956 return false;
24957
24958 /* A type-dependent expression is also value-dependent. */
24959 if (type_dependent_expression_p (expression))
24960 return true;
24961
24962 switch (TREE_CODE (expression))
24963 {
24964 case BASELINK:
24965 /* A dependent member function of the current instantiation. */
24966 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
24967
24968 case FUNCTION_DECL:
24969 /* A dependent member function of the current instantiation. */
24970 if (DECL_CLASS_SCOPE_P (expression)
24971 && dependent_type_p (DECL_CONTEXT (expression)))
24972 return true;
24973 break;
24974
24975 case IDENTIFIER_NODE:
24976 /* A name that has not been looked up -- must be dependent. */
24977 return true;
24978
24979 case TEMPLATE_PARM_INDEX:
24980 /* A non-type template parm. */
24981 return true;
24982
24983 case CONST_DECL:
24984 /* A non-type template parm. */
24985 if (DECL_TEMPLATE_PARM_P (expression))
24986 return true;
24987 return value_dependent_expression_p (DECL_INITIAL (expression));
24988
24989 case VAR_DECL:
24990 /* A constant with literal type and is initialized
24991 with an expression that is value-dependent. */
24992 if (DECL_DEPENDENT_INIT_P (expression)
24993 /* FIXME cp_finish_decl doesn't fold reference initializers. */
24994 || TYPE_REF_P (TREE_TYPE (expression)))
24995 return true;
24996 if (DECL_HAS_VALUE_EXPR_P (expression))
24997 {
24998 tree value_expr = DECL_VALUE_EXPR (expression);
24999 if (value_dependent_expression_p (value_expr))
25000 return true;
25001 }
25002 return false;
25003
25004 case DYNAMIC_CAST_EXPR:
25005 case STATIC_CAST_EXPR:
25006 case CONST_CAST_EXPR:
25007 case REINTERPRET_CAST_EXPR:
25008 case CAST_EXPR:
25009 case IMPLICIT_CONV_EXPR:
25010 /* These expressions are value-dependent if the type to which
25011 the cast occurs is dependent or the expression being casted
25012 is value-dependent. */
25013 {
25014 tree type = TREE_TYPE (expression);
25015
25016 if (dependent_type_p (type))
25017 return true;
25018
25019 /* A functional cast has a list of operands. */
25020 expression = TREE_OPERAND (expression, 0);
25021 if (!expression)
25022 {
25023 /* If there are no operands, it must be an expression such
25024 as "int()". This should not happen for aggregate types
25025 because it would form non-constant expressions. */
25026 gcc_assert (cxx_dialect >= cxx11
25027 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
25028
25029 return false;
25030 }
25031
25032 if (TREE_CODE (expression) == TREE_LIST)
25033 return any_value_dependent_elements_p (expression);
25034
25035 return value_dependent_expression_p (expression);
25036 }
25037
25038 case SIZEOF_EXPR:
25039 if (SIZEOF_EXPR_TYPE_P (expression))
25040 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
25041 /* FALLTHRU */
25042 case ALIGNOF_EXPR:
25043 case TYPEID_EXPR:
25044 /* A `sizeof' expression is value-dependent if the operand is
25045 type-dependent or is a pack expansion. */
25046 expression = TREE_OPERAND (expression, 0);
25047 if (PACK_EXPANSION_P (expression))
25048 return true;
25049 else if (TYPE_P (expression))
25050 return dependent_type_p (expression);
25051 return instantiation_dependent_uneval_expression_p (expression);
25052
25053 case AT_ENCODE_EXPR:
25054 /* An 'encode' expression is value-dependent if the operand is
25055 type-dependent. */
25056 expression = TREE_OPERAND (expression, 0);
25057 return dependent_type_p (expression);
25058
25059 case NOEXCEPT_EXPR:
25060 expression = TREE_OPERAND (expression, 0);
25061 return instantiation_dependent_uneval_expression_p (expression);
25062
25063 case SCOPE_REF:
25064 /* All instantiation-dependent expressions should also be considered
25065 value-dependent. */
25066 return instantiation_dependent_scope_ref_p (expression);
25067
25068 case COMPONENT_REF:
25069 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
25070 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
25071
25072 case NONTYPE_ARGUMENT_PACK:
25073 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
25074 is value-dependent. */
25075 {
25076 tree values = ARGUMENT_PACK_ARGS (expression);
25077 int i, len = TREE_VEC_LENGTH (values);
25078
25079 for (i = 0; i < len; ++i)
25080 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
25081 return true;
25082
25083 return false;
25084 }
25085
25086 case TRAIT_EXPR:
25087 {
25088 tree type2 = TRAIT_EXPR_TYPE2 (expression);
25089
25090 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
25091 return true;
25092
25093 if (!type2)
25094 return false;
25095
25096 if (TREE_CODE (type2) != TREE_LIST)
25097 return dependent_type_p (type2);
25098
25099 for (; type2; type2 = TREE_CHAIN (type2))
25100 if (dependent_type_p (TREE_VALUE (type2)))
25101 return true;
25102
25103 return false;
25104 }
25105
25106 case MODOP_EXPR:
25107 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
25108 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
25109
25110 case ARRAY_REF:
25111 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
25112 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
25113
25114 case ADDR_EXPR:
25115 {
25116 tree op = TREE_OPERAND (expression, 0);
25117 return (value_dependent_expression_p (op)
25118 || has_value_dependent_address (op));
25119 }
25120
25121 case REQUIRES_EXPR:
25122 /* Treat all requires-expressions as value-dependent so
25123 we don't try to fold them. */
25124 return true;
25125
25126 case TYPE_REQ:
25127 return dependent_type_p (TREE_OPERAND (expression, 0));
25128
25129 case CALL_EXPR:
25130 {
25131 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
25132 return true;
25133 tree fn = get_callee_fndecl (expression);
25134 int i, nargs;
25135 nargs = call_expr_nargs (expression);
25136 for (i = 0; i < nargs; ++i)
25137 {
25138 tree op = CALL_EXPR_ARG (expression, i);
25139 /* In a call to a constexpr member function, look through the
25140 implicit ADDR_EXPR on the object argument so that it doesn't
25141 cause the call to be considered value-dependent. We also
25142 look through it in potential_constant_expression. */
25143 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
25144 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
25145 && TREE_CODE (op) == ADDR_EXPR)
25146 op = TREE_OPERAND (op, 0);
25147 if (value_dependent_expression_p (op))
25148 return true;
25149 }
25150 return false;
25151 }
25152
25153 case TEMPLATE_ID_EXPR:
25154 return variable_concept_p (TREE_OPERAND (expression, 0));
25155
25156 case CONSTRUCTOR:
25157 {
25158 unsigned ix;
25159 tree val;
25160 if (dependent_type_p (TREE_TYPE (expression)))
25161 return true;
25162 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
25163 if (value_dependent_expression_p (val))
25164 return true;
25165 return false;
25166 }
25167
25168 case STMT_EXPR:
25169 /* Treat a GNU statement expression as dependent to avoid crashing
25170 under instantiate_non_dependent_expr; it can't be constant. */
25171 return true;
25172
25173 default:
25174 /* A constant expression is value-dependent if any subexpression is
25175 value-dependent. */
25176 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
25177 {
25178 case tcc_reference:
25179 case tcc_unary:
25180 case tcc_comparison:
25181 case tcc_binary:
25182 case tcc_expression:
25183 case tcc_vl_exp:
25184 {
25185 int i, len = cp_tree_operand_length (expression);
25186
25187 for (i = 0; i < len; i++)
25188 {
25189 tree t = TREE_OPERAND (expression, i);
25190
25191 /* In some cases, some of the operands may be missing.
25192 (For example, in the case of PREDECREMENT_EXPR, the
25193 amount to increment by may be missing.) That doesn't
25194 make the expression dependent. */
25195 if (t && value_dependent_expression_p (t))
25196 return true;
25197 }
25198 }
25199 break;
25200 default:
25201 break;
25202 }
25203 break;
25204 }
25205
25206 /* The expression is not value-dependent. */
25207 return false;
25208 }
25209
25210 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
25211 [temp.dep.expr]. Note that an expression with no type is
25212 considered dependent. Other parts of the compiler arrange for an
25213 expression with type-dependent subexpressions to have no type, so
25214 this function doesn't have to be fully recursive. */
25215
25216 bool
25217 type_dependent_expression_p (tree expression)
25218 {
25219 if (!processing_template_decl)
25220 return false;
25221
25222 if (expression == NULL_TREE || expression == error_mark_node)
25223 return false;
25224
25225 STRIP_ANY_LOCATION_WRAPPER (expression);
25226
25227 /* An unresolved name is always dependent. */
25228 if (identifier_p (expression)
25229 || TREE_CODE (expression) == USING_DECL
25230 || TREE_CODE (expression) == WILDCARD_DECL)
25231 return true;
25232
25233 /* A fold expression is type-dependent. */
25234 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
25235 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
25236 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
25237 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
25238 return true;
25239
25240 /* Some expression forms are never type-dependent. */
25241 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
25242 || TREE_CODE (expression) == SIZEOF_EXPR
25243 || TREE_CODE (expression) == ALIGNOF_EXPR
25244 || TREE_CODE (expression) == AT_ENCODE_EXPR
25245 || TREE_CODE (expression) == NOEXCEPT_EXPR
25246 || TREE_CODE (expression) == TRAIT_EXPR
25247 || TREE_CODE (expression) == TYPEID_EXPR
25248 || TREE_CODE (expression) == DELETE_EXPR
25249 || TREE_CODE (expression) == VEC_DELETE_EXPR
25250 || TREE_CODE (expression) == THROW_EXPR
25251 || TREE_CODE (expression) == REQUIRES_EXPR)
25252 return false;
25253
25254 /* The types of these expressions depends only on the type to which
25255 the cast occurs. */
25256 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
25257 || TREE_CODE (expression) == STATIC_CAST_EXPR
25258 || TREE_CODE (expression) == CONST_CAST_EXPR
25259 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
25260 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
25261 || TREE_CODE (expression) == CAST_EXPR)
25262 return dependent_type_p (TREE_TYPE (expression));
25263
25264 /* The types of these expressions depends only on the type created
25265 by the expression. */
25266 if (TREE_CODE (expression) == NEW_EXPR
25267 || TREE_CODE (expression) == VEC_NEW_EXPR)
25268 {
25269 /* For NEW_EXPR tree nodes created inside a template, either
25270 the object type itself or a TREE_LIST may appear as the
25271 operand 1. */
25272 tree type = TREE_OPERAND (expression, 1);
25273 if (TREE_CODE (type) == TREE_LIST)
25274 /* This is an array type. We need to check array dimensions
25275 as well. */
25276 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
25277 || value_dependent_expression_p
25278 (TREE_OPERAND (TREE_VALUE (type), 1));
25279 else
25280 return dependent_type_p (type);
25281 }
25282
25283 if (TREE_CODE (expression) == SCOPE_REF)
25284 {
25285 tree scope = TREE_OPERAND (expression, 0);
25286 tree name = TREE_OPERAND (expression, 1);
25287
25288 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
25289 contains an identifier associated by name lookup with one or more
25290 declarations declared with a dependent type, or...a
25291 nested-name-specifier or qualified-id that names a member of an
25292 unknown specialization. */
25293 return (type_dependent_expression_p (name)
25294 || dependent_scope_p (scope));
25295 }
25296
25297 if (TREE_CODE (expression) == TEMPLATE_DECL
25298 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
25299 return uses_outer_template_parms (expression);
25300
25301 if (TREE_CODE (expression) == STMT_EXPR)
25302 expression = stmt_expr_value_expr (expression);
25303
25304 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
25305 {
25306 tree elt;
25307 unsigned i;
25308
25309 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
25310 {
25311 if (type_dependent_expression_p (elt))
25312 return true;
25313 }
25314 return false;
25315 }
25316
25317 /* A static data member of the current instantiation with incomplete
25318 array type is type-dependent, as the definition and specializations
25319 can have different bounds. */
25320 if (VAR_P (expression)
25321 && DECL_CLASS_SCOPE_P (expression)
25322 && dependent_type_p (DECL_CONTEXT (expression))
25323 && VAR_HAD_UNKNOWN_BOUND (expression))
25324 return true;
25325
25326 /* An array of unknown bound depending on a variadic parameter, eg:
25327
25328 template<typename... Args>
25329 void foo (Args... args)
25330 {
25331 int arr[] = { args... };
25332 }
25333
25334 template<int... vals>
25335 void bar ()
25336 {
25337 int arr[] = { vals... };
25338 }
25339
25340 If the array has no length and has an initializer, it must be that
25341 we couldn't determine its length in cp_complete_array_type because
25342 it is dependent. */
25343 if (VAR_P (expression)
25344 && TREE_TYPE (expression) != NULL_TREE
25345 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
25346 && !TYPE_DOMAIN (TREE_TYPE (expression))
25347 && DECL_INITIAL (expression))
25348 return true;
25349
25350 /* A function or variable template-id is type-dependent if it has any
25351 dependent template arguments. */
25352 if (VAR_OR_FUNCTION_DECL_P (expression)
25353 && DECL_LANG_SPECIFIC (expression)
25354 && DECL_TEMPLATE_INFO (expression))
25355 {
25356 /* Consider the innermost template arguments, since those are the ones
25357 that come from the template-id; the template arguments for the
25358 enclosing class do not make it type-dependent unless they are used in
25359 the type of the decl. */
25360 if (PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression))
25361 && (any_dependent_template_arguments_p
25362 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
25363 return true;
25364 }
25365
25366 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
25367 type-dependent. Checking this is important for functions with auto return
25368 type, which looks like a dependent type. */
25369 if (TREE_CODE (expression) == FUNCTION_DECL
25370 && !(DECL_CLASS_SCOPE_P (expression)
25371 && dependent_type_p (DECL_CONTEXT (expression)))
25372 && !(DECL_LANG_SPECIFIC (expression)
25373 && DECL_FRIEND_P (expression)
25374 && (!DECL_FRIEND_CONTEXT (expression)
25375 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
25376 && !DECL_LOCAL_FUNCTION_P (expression))
25377 {
25378 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
25379 || undeduced_auto_decl (expression));
25380 return false;
25381 }
25382
25383 /* Always dependent, on the number of arguments if nothing else. */
25384 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
25385 return true;
25386
25387 if (TREE_TYPE (expression) == unknown_type_node)
25388 {
25389 if (TREE_CODE (expression) == ADDR_EXPR)
25390 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
25391 if (TREE_CODE (expression) == COMPONENT_REF
25392 || TREE_CODE (expression) == OFFSET_REF)
25393 {
25394 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
25395 return true;
25396 expression = TREE_OPERAND (expression, 1);
25397 if (identifier_p (expression))
25398 return false;
25399 }
25400 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
25401 if (TREE_CODE (expression) == SCOPE_REF)
25402 return false;
25403
25404 if (BASELINK_P (expression))
25405 {
25406 if (BASELINK_OPTYPE (expression)
25407 && dependent_type_p (BASELINK_OPTYPE (expression)))
25408 return true;
25409 expression = BASELINK_FUNCTIONS (expression);
25410 }
25411
25412 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
25413 {
25414 if (any_dependent_template_arguments_p
25415 (TREE_OPERAND (expression, 1)))
25416 return true;
25417 expression = TREE_OPERAND (expression, 0);
25418 if (identifier_p (expression))
25419 return true;
25420 }
25421
25422 gcc_assert (TREE_CODE (expression) == OVERLOAD
25423 || TREE_CODE (expression) == FUNCTION_DECL);
25424
25425 for (lkp_iterator iter (expression); iter; ++iter)
25426 if (type_dependent_expression_p (*iter))
25427 return true;
25428
25429 return false;
25430 }
25431
25432 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
25433
25434 /* Dependent type attributes might not have made it from the decl to
25435 the type yet. */
25436 if (DECL_P (expression)
25437 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
25438 return true;
25439
25440 return (dependent_type_p (TREE_TYPE (expression)));
25441 }
25442
25443 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
25444 type-dependent if the expression refers to a member of the current
25445 instantiation and the type of the referenced member is dependent, or the
25446 class member access expression refers to a member of an unknown
25447 specialization.
25448
25449 This function returns true if the OBJECT in such a class member access
25450 expression is of an unknown specialization. */
25451
25452 bool
25453 type_dependent_object_expression_p (tree object)
25454 {
25455 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
25456 dependent. */
25457 if (TREE_CODE (object) == IDENTIFIER_NODE)
25458 return true;
25459 tree scope = TREE_TYPE (object);
25460 return (!scope || dependent_scope_p (scope));
25461 }
25462
25463 /* walk_tree callback function for instantiation_dependent_expression_p,
25464 below. Returns non-zero if a dependent subexpression is found. */
25465
25466 static tree
25467 instantiation_dependent_r (tree *tp, int *walk_subtrees,
25468 void * /*data*/)
25469 {
25470 if (TYPE_P (*tp))
25471 {
25472 /* We don't have to worry about decltype currently because decltype
25473 of an instantiation-dependent expr is a dependent type. This
25474 might change depending on the resolution of DR 1172. */
25475 *walk_subtrees = false;
25476 return NULL_TREE;
25477 }
25478 enum tree_code code = TREE_CODE (*tp);
25479 switch (code)
25480 {
25481 /* Don't treat an argument list as dependent just because it has no
25482 TREE_TYPE. */
25483 case TREE_LIST:
25484 case TREE_VEC:
25485 case NONTYPE_ARGUMENT_PACK:
25486 return NULL_TREE;
25487
25488 case TEMPLATE_PARM_INDEX:
25489 if (dependent_type_p (TREE_TYPE (*tp)))
25490 return *tp;
25491 /* We'll check value-dependence separately. */
25492 return NULL_TREE;
25493
25494 /* Handle expressions with type operands. */
25495 case SIZEOF_EXPR:
25496 case ALIGNOF_EXPR:
25497 case TYPEID_EXPR:
25498 case AT_ENCODE_EXPR:
25499 {
25500 tree op = TREE_OPERAND (*tp, 0);
25501 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
25502 op = TREE_TYPE (op);
25503 if (TYPE_P (op))
25504 {
25505 if (dependent_type_p (op))
25506 return *tp;
25507 else
25508 {
25509 *walk_subtrees = false;
25510 return NULL_TREE;
25511 }
25512 }
25513 break;
25514 }
25515
25516 case COMPONENT_REF:
25517 if (identifier_p (TREE_OPERAND (*tp, 1)))
25518 /* In a template, finish_class_member_access_expr creates a
25519 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
25520 type-dependent, so that we can check access control at
25521 instantiation time (PR 42277). See also Core issue 1273. */
25522 return *tp;
25523 break;
25524
25525 case SCOPE_REF:
25526 if (instantiation_dependent_scope_ref_p (*tp))
25527 return *tp;
25528 else
25529 break;
25530
25531 /* Treat statement-expressions as dependent. */
25532 case BIND_EXPR:
25533 return *tp;
25534
25535 /* Treat requires-expressions as dependent. */
25536 case REQUIRES_EXPR:
25537 return *tp;
25538
25539 case CALL_EXPR:
25540 /* Treat calls to function concepts as dependent. */
25541 if (function_concept_check_p (*tp))
25542 return *tp;
25543 break;
25544
25545 case TEMPLATE_ID_EXPR:
25546 /* And variable concepts. */
25547 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
25548 return *tp;
25549 break;
25550
25551 default:
25552 break;
25553 }
25554
25555 if (type_dependent_expression_p (*tp))
25556 return *tp;
25557 else
25558 return NULL_TREE;
25559 }
25560
25561 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
25562 sense defined by the ABI:
25563
25564 "An expression is instantiation-dependent if it is type-dependent
25565 or value-dependent, or it has a subexpression that is type-dependent
25566 or value-dependent."
25567
25568 Except don't actually check value-dependence for unevaluated expressions,
25569 because in sizeof(i) we don't care about the value of i. Checking
25570 type-dependence will in turn check value-dependence of array bounds/template
25571 arguments as needed. */
25572
25573 bool
25574 instantiation_dependent_uneval_expression_p (tree expression)
25575 {
25576 tree result;
25577
25578 if (!processing_template_decl)
25579 return false;
25580
25581 if (expression == error_mark_node)
25582 return false;
25583
25584 result = cp_walk_tree_without_duplicates (&expression,
25585 instantiation_dependent_r, NULL);
25586 return result != NULL_TREE;
25587 }
25588
25589 /* As above, but also check value-dependence of the expression as a whole. */
25590
25591 bool
25592 instantiation_dependent_expression_p (tree expression)
25593 {
25594 return (instantiation_dependent_uneval_expression_p (expression)
25595 || value_dependent_expression_p (expression));
25596 }
25597
25598 /* Like type_dependent_expression_p, but it also works while not processing
25599 a template definition, i.e. during substitution or mangling. */
25600
25601 bool
25602 type_dependent_expression_p_push (tree expr)
25603 {
25604 bool b;
25605 ++processing_template_decl;
25606 b = type_dependent_expression_p (expr);
25607 --processing_template_decl;
25608 return b;
25609 }
25610
25611 /* Returns TRUE if ARGS contains a type-dependent expression. */
25612
25613 bool
25614 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
25615 {
25616 unsigned int i;
25617 tree arg;
25618
25619 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
25620 {
25621 if (type_dependent_expression_p (arg))
25622 return true;
25623 }
25624 return false;
25625 }
25626
25627 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
25628 expressions) contains any type-dependent expressions. */
25629
25630 bool
25631 any_type_dependent_elements_p (const_tree list)
25632 {
25633 for (; list; list = TREE_CHAIN (list))
25634 if (type_dependent_expression_p (TREE_VALUE (list)))
25635 return true;
25636
25637 return false;
25638 }
25639
25640 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
25641 expressions) contains any value-dependent expressions. */
25642
25643 bool
25644 any_value_dependent_elements_p (const_tree list)
25645 {
25646 for (; list; list = TREE_CHAIN (list))
25647 if (value_dependent_expression_p (TREE_VALUE (list)))
25648 return true;
25649
25650 return false;
25651 }
25652
25653 /* Returns TRUE if the ARG (a template argument) is dependent. */
25654
25655 bool
25656 dependent_template_arg_p (tree arg)
25657 {
25658 if (!processing_template_decl)
25659 return false;
25660
25661 /* Assume a template argument that was wrongly written by the user
25662 is dependent. This is consistent with what
25663 any_dependent_template_arguments_p [that calls this function]
25664 does. */
25665 if (!arg || arg == error_mark_node)
25666 return true;
25667
25668 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
25669 arg = argument_pack_select_arg (arg);
25670
25671 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
25672 return true;
25673 if (TREE_CODE (arg) == TEMPLATE_DECL)
25674 {
25675 if (DECL_TEMPLATE_PARM_P (arg))
25676 return true;
25677 /* A member template of a dependent class is not necessarily
25678 type-dependent, but it is a dependent template argument because it
25679 will be a member of an unknown specialization to that template. */
25680 tree scope = CP_DECL_CONTEXT (arg);
25681 return TYPE_P (scope) && dependent_type_p (scope);
25682 }
25683 else if (ARGUMENT_PACK_P (arg))
25684 {
25685 tree args = ARGUMENT_PACK_ARGS (arg);
25686 int i, len = TREE_VEC_LENGTH (args);
25687 for (i = 0; i < len; ++i)
25688 {
25689 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
25690 return true;
25691 }
25692
25693 return false;
25694 }
25695 else if (TYPE_P (arg))
25696 return dependent_type_p (arg);
25697 else
25698 return (type_dependent_expression_p (arg)
25699 || value_dependent_expression_p (arg));
25700 }
25701
25702 /* Returns true if ARGS (a collection of template arguments) contains
25703 any types that require structural equality testing. */
25704
25705 bool
25706 any_template_arguments_need_structural_equality_p (tree args)
25707 {
25708 int i;
25709 int j;
25710
25711 if (!args)
25712 return false;
25713 if (args == error_mark_node)
25714 return true;
25715
25716 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
25717 {
25718 tree level = TMPL_ARGS_LEVEL (args, i + 1);
25719 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
25720 {
25721 tree arg = TREE_VEC_ELT (level, j);
25722 tree packed_args = NULL_TREE;
25723 int k, len = 1;
25724
25725 if (ARGUMENT_PACK_P (arg))
25726 {
25727 /* Look inside the argument pack. */
25728 packed_args = ARGUMENT_PACK_ARGS (arg);
25729 len = TREE_VEC_LENGTH (packed_args);
25730 }
25731
25732 for (k = 0; k < len; ++k)
25733 {
25734 if (packed_args)
25735 arg = TREE_VEC_ELT (packed_args, k);
25736
25737 if (error_operand_p (arg))
25738 return true;
25739 else if (TREE_CODE (arg) == TEMPLATE_DECL)
25740 continue;
25741 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
25742 return true;
25743 else if (!TYPE_P (arg) && TREE_TYPE (arg)
25744 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
25745 return true;
25746 }
25747 }
25748 }
25749
25750 return false;
25751 }
25752
25753 /* Returns true if ARGS (a collection of template arguments) contains
25754 any dependent arguments. */
25755
25756 bool
25757 any_dependent_template_arguments_p (const_tree args)
25758 {
25759 int i;
25760 int j;
25761
25762 if (!args)
25763 return false;
25764 if (args == error_mark_node)
25765 return true;
25766
25767 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
25768 {
25769 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
25770 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
25771 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
25772 return true;
25773 }
25774
25775 return false;
25776 }
25777
25778 /* Returns true if ARGS contains any errors. */
25779
25780 bool
25781 any_erroneous_template_args_p (const_tree args)
25782 {
25783 int i;
25784 int j;
25785
25786 if (args == error_mark_node)
25787 return true;
25788
25789 if (args && TREE_CODE (args) != TREE_VEC)
25790 {
25791 if (tree ti = get_template_info (args))
25792 args = TI_ARGS (ti);
25793 else
25794 args = NULL_TREE;
25795 }
25796
25797 if (!args)
25798 return false;
25799
25800 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
25801 {
25802 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
25803 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
25804 if (error_operand_p (TREE_VEC_ELT (level, j)))
25805 return true;
25806 }
25807
25808 return false;
25809 }
25810
25811 /* Returns TRUE if the template TMPL is type-dependent. */
25812
25813 bool
25814 dependent_template_p (tree tmpl)
25815 {
25816 if (TREE_CODE (tmpl) == OVERLOAD)
25817 {
25818 for (lkp_iterator iter (tmpl); iter; ++iter)
25819 if (dependent_template_p (*iter))
25820 return true;
25821 return false;
25822 }
25823
25824 /* Template template parameters are dependent. */
25825 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
25826 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
25827 return true;
25828 /* So are names that have not been looked up. */
25829 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
25830 return true;
25831 return false;
25832 }
25833
25834 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
25835
25836 bool
25837 dependent_template_id_p (tree tmpl, tree args)
25838 {
25839 return (dependent_template_p (tmpl)
25840 || any_dependent_template_arguments_p (args));
25841 }
25842
25843 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
25844 are dependent. */
25845
25846 bool
25847 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
25848 {
25849 int i;
25850
25851 if (!processing_template_decl)
25852 return false;
25853
25854 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
25855 {
25856 tree decl = TREE_VEC_ELT (declv, i);
25857 tree init = TREE_VEC_ELT (initv, i);
25858 tree cond = TREE_VEC_ELT (condv, i);
25859 tree incr = TREE_VEC_ELT (incrv, i);
25860
25861 if (type_dependent_expression_p (decl)
25862 || TREE_CODE (decl) == SCOPE_REF)
25863 return true;
25864
25865 if (init && type_dependent_expression_p (init))
25866 return true;
25867
25868 if (type_dependent_expression_p (cond))
25869 return true;
25870
25871 if (COMPARISON_CLASS_P (cond)
25872 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
25873 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
25874 return true;
25875
25876 if (TREE_CODE (incr) == MODOP_EXPR)
25877 {
25878 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
25879 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
25880 return true;
25881 }
25882 else if (type_dependent_expression_p (incr))
25883 return true;
25884 else if (TREE_CODE (incr) == MODIFY_EXPR)
25885 {
25886 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
25887 return true;
25888 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
25889 {
25890 tree t = TREE_OPERAND (incr, 1);
25891 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
25892 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
25893 return true;
25894 }
25895 }
25896 }
25897
25898 return false;
25899 }
25900
25901 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
25902 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
25903 no such TYPE can be found. Note that this function peers inside
25904 uninstantiated templates and therefore should be used only in
25905 extremely limited situations. ONLY_CURRENT_P restricts this
25906 peering to the currently open classes hierarchy (which is required
25907 when comparing types). */
25908
25909 tree
25910 resolve_typename_type (tree type, bool only_current_p)
25911 {
25912 tree scope;
25913 tree name;
25914 tree decl;
25915 int quals;
25916 tree pushed_scope;
25917 tree result;
25918
25919 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
25920
25921 scope = TYPE_CONTEXT (type);
25922 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
25923 gcc_checking_assert (uses_template_parms (scope));
25924
25925 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
25926 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
25927 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
25928 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
25929 identifier of the TYPENAME_TYPE anymore.
25930 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
25931 TYPENAME_TYPE instead, we avoid messing up with a possible
25932 typedef variant case. */
25933 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
25934
25935 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
25936 it first before we can figure out what NAME refers to. */
25937 if (TREE_CODE (scope) == TYPENAME_TYPE)
25938 {
25939 if (TYPENAME_IS_RESOLVING_P (scope))
25940 /* Given a class template A with a dependent base with nested type C,
25941 typedef typename A::C::C C will land us here, as trying to resolve
25942 the initial A::C leads to the local C typedef, which leads back to
25943 A::C::C. So we break the recursion now. */
25944 return type;
25945 else
25946 scope = resolve_typename_type (scope, only_current_p);
25947 }
25948 /* If we don't know what SCOPE refers to, then we cannot resolve the
25949 TYPENAME_TYPE. */
25950 if (!CLASS_TYPE_P (scope))
25951 return type;
25952 /* If this is a typedef, we don't want to look inside (c++/11987). */
25953 if (typedef_variant_p (type))
25954 return type;
25955 /* If SCOPE isn't the template itself, it will not have a valid
25956 TYPE_FIELDS list. */
25957 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
25958 /* scope is either the template itself or a compatible instantiation
25959 like X<T>, so look up the name in the original template. */
25960 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
25961 /* If scope has no fields, it can't be a current instantiation. Check this
25962 before currently_open_class to avoid infinite recursion (71515). */
25963 if (!TYPE_FIELDS (scope))
25964 return type;
25965 /* If the SCOPE is not the current instantiation, there's no reason
25966 to look inside it. */
25967 if (only_current_p && !currently_open_class (scope))
25968 return type;
25969 /* Enter the SCOPE so that name lookup will be resolved as if we
25970 were in the class definition. In particular, SCOPE will no
25971 longer be considered a dependent type. */
25972 pushed_scope = push_scope (scope);
25973 /* Look up the declaration. */
25974 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
25975 tf_warning_or_error);
25976
25977 result = NULL_TREE;
25978
25979 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
25980 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
25981 tree fullname = TYPENAME_TYPE_FULLNAME (type);
25982 if (!decl)
25983 /*nop*/;
25984 else if (identifier_p (fullname)
25985 && TREE_CODE (decl) == TYPE_DECL)
25986 {
25987 result = TREE_TYPE (decl);
25988 if (result == error_mark_node)
25989 result = NULL_TREE;
25990 }
25991 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
25992 && DECL_CLASS_TEMPLATE_P (decl))
25993 {
25994 /* Obtain the template and the arguments. */
25995 tree tmpl = TREE_OPERAND (fullname, 0);
25996 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
25997 {
25998 /* We get here with a plain identifier because a previous tentative
25999 parse of the nested-name-specifier as part of a ptr-operator saw
26000 ::template X<A>. The use of ::template is necessary in a
26001 ptr-operator, but wrong in a declarator-id.
26002
26003 [temp.names]: In a qualified-id of a declarator-id, the keyword
26004 template shall not appear at the top level. */
26005 pedwarn (cp_expr_loc_or_loc (fullname, input_location), OPT_Wpedantic,
26006 "keyword %<template%> not allowed in declarator-id");
26007 tmpl = decl;
26008 }
26009 tree args = TREE_OPERAND (fullname, 1);
26010 /* Instantiate the template. */
26011 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
26012 /*entering_scope=*/true,
26013 tf_error | tf_user);
26014 if (result == error_mark_node)
26015 result = NULL_TREE;
26016 }
26017
26018 /* Leave the SCOPE. */
26019 if (pushed_scope)
26020 pop_scope (pushed_scope);
26021
26022 /* If we failed to resolve it, return the original typename. */
26023 if (!result)
26024 return type;
26025
26026 /* If lookup found a typename type, resolve that too. */
26027 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
26028 {
26029 /* Ill-formed programs can cause infinite recursion here, so we
26030 must catch that. */
26031 TYPENAME_IS_RESOLVING_P (result) = 1;
26032 result = resolve_typename_type (result, only_current_p);
26033 TYPENAME_IS_RESOLVING_P (result) = 0;
26034 }
26035
26036 /* Qualify the resulting type. */
26037 quals = cp_type_quals (type);
26038 if (quals)
26039 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
26040
26041 return result;
26042 }
26043
26044 /* EXPR is an expression which is not type-dependent. Return a proxy
26045 for EXPR that can be used to compute the types of larger
26046 expressions containing EXPR. */
26047
26048 tree
26049 build_non_dependent_expr (tree expr)
26050 {
26051 tree orig_expr = expr;
26052 tree inner_expr;
26053
26054 /* When checking, try to get a constant value for all non-dependent
26055 expressions in order to expose bugs in *_dependent_expression_p
26056 and constexpr. This can affect code generation, see PR70704, so
26057 only do this for -fchecking=2. */
26058 if (flag_checking > 1
26059 && cxx_dialect >= cxx11
26060 /* Don't do this during nsdmi parsing as it can lead to
26061 unexpected recursive instantiations. */
26062 && !parsing_nsdmi ()
26063 /* Don't do this during concept expansion either and for
26064 the same reason. */
26065 && !expanding_concept ())
26066 fold_non_dependent_expr (expr, tf_none);
26067
26068 STRIP_ANY_LOCATION_WRAPPER (expr);
26069
26070 /* Preserve OVERLOADs; the functions must be available to resolve
26071 types. */
26072 inner_expr = expr;
26073 if (TREE_CODE (inner_expr) == STMT_EXPR)
26074 inner_expr = stmt_expr_value_expr (inner_expr);
26075 if (TREE_CODE (inner_expr) == ADDR_EXPR)
26076 inner_expr = TREE_OPERAND (inner_expr, 0);
26077 if (TREE_CODE (inner_expr) == COMPONENT_REF)
26078 inner_expr = TREE_OPERAND (inner_expr, 1);
26079 if (is_overloaded_fn (inner_expr)
26080 || TREE_CODE (inner_expr) == OFFSET_REF)
26081 return orig_expr;
26082 /* There is no need to return a proxy for a variable or enumerator. */
26083 if (VAR_P (expr) || TREE_CODE (expr) == CONST_DECL)
26084 return orig_expr;
26085 /* Preserve string constants; conversions from string constants to
26086 "char *" are allowed, even though normally a "const char *"
26087 cannot be used to initialize a "char *". */
26088 if (TREE_CODE (expr) == STRING_CST)
26089 return orig_expr;
26090 /* Preserve void and arithmetic constants, as an optimization -- there is no
26091 reason to create a new node. */
26092 if (TREE_CODE (expr) == VOID_CST
26093 || TREE_CODE (expr) == INTEGER_CST
26094 || TREE_CODE (expr) == REAL_CST)
26095 return orig_expr;
26096 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
26097 There is at least one place where we want to know that a
26098 particular expression is a throw-expression: when checking a ?:
26099 expression, there are special rules if the second or third
26100 argument is a throw-expression. */
26101 if (TREE_CODE (expr) == THROW_EXPR)
26102 return orig_expr;
26103
26104 /* Don't wrap an initializer list, we need to be able to look inside. */
26105 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
26106 return orig_expr;
26107
26108 /* Don't wrap a dummy object, we need to be able to test for it. */
26109 if (is_dummy_object (expr))
26110 return orig_expr;
26111
26112 if (TREE_CODE (expr) == COND_EXPR)
26113 return build3 (COND_EXPR,
26114 TREE_TYPE (expr),
26115 TREE_OPERAND (expr, 0),
26116 (TREE_OPERAND (expr, 1)
26117 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
26118 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
26119 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
26120 if (TREE_CODE (expr) == COMPOUND_EXPR
26121 && !COMPOUND_EXPR_OVERLOADED (expr))
26122 return build2 (COMPOUND_EXPR,
26123 TREE_TYPE (expr),
26124 TREE_OPERAND (expr, 0),
26125 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
26126
26127 /* If the type is unknown, it can't really be non-dependent */
26128 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
26129
26130 /* Otherwise, build a NON_DEPENDENT_EXPR. */
26131 return build1_loc (EXPR_LOCATION (orig_expr), NON_DEPENDENT_EXPR,
26132 TREE_TYPE (expr), expr);
26133 }
26134
26135 /* ARGS is a vector of expressions as arguments to a function call.
26136 Replace the arguments with equivalent non-dependent expressions.
26137 This modifies ARGS in place. */
26138
26139 void
26140 make_args_non_dependent (vec<tree, va_gc> *args)
26141 {
26142 unsigned int ix;
26143 tree arg;
26144
26145 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
26146 {
26147 tree newarg = build_non_dependent_expr (arg);
26148 if (newarg != arg)
26149 (*args)[ix] = newarg;
26150 }
26151 }
26152
26153 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
26154 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
26155 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
26156
26157 static tree
26158 make_auto_1 (tree name, bool set_canonical)
26159 {
26160 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
26161 TYPE_NAME (au) = build_decl (input_location,
26162 TYPE_DECL, name, au);
26163 TYPE_STUB_DECL (au) = TYPE_NAME (au);
26164 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
26165 (0, processing_template_decl + 1, processing_template_decl + 1,
26166 TYPE_NAME (au), NULL_TREE);
26167 if (set_canonical)
26168 TYPE_CANONICAL (au) = canonical_type_parameter (au);
26169 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
26170 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
26171
26172 return au;
26173 }
26174
26175 tree
26176 make_decltype_auto (void)
26177 {
26178 return make_auto_1 (decltype_auto_identifier, true);
26179 }
26180
26181 tree
26182 make_auto (void)
26183 {
26184 return make_auto_1 (auto_identifier, true);
26185 }
26186
26187 /* Return a C++17 deduction placeholder for class template TMPL. */
26188
26189 tree
26190 make_template_placeholder (tree tmpl)
26191 {
26192 tree t = make_auto_1 (DECL_NAME (tmpl), true);
26193 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
26194 return t;
26195 }
26196
26197 /* True iff T is a C++17 class template deduction placeholder. */
26198
26199 bool
26200 template_placeholder_p (tree t)
26201 {
26202 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
26203 }
26204
26205 /* Make a "constrained auto" type-specifier. This is an
26206 auto type with constraints that must be associated after
26207 deduction. The constraint is formed from the given
26208 CONC and its optional sequence of arguments, which are
26209 non-null if written as partial-concept-id. */
26210
26211 tree
26212 make_constrained_auto (tree con, tree args)
26213 {
26214 tree type = make_auto_1 (auto_identifier, false);
26215
26216 /* Build the constraint. */
26217 tree tmpl = DECL_TI_TEMPLATE (con);
26218 tree expr = VAR_P (con) ? tmpl : ovl_make (tmpl);
26219 expr = build_concept_check (expr, type, args);
26220
26221 tree constr = normalize_expression (expr);
26222 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
26223
26224 /* Our canonical type depends on the constraint. */
26225 TYPE_CANONICAL (type) = canonical_type_parameter (type);
26226
26227 /* Attach the constraint to the type declaration. */
26228 tree decl = TYPE_NAME (type);
26229 return decl;
26230 }
26231
26232 /* Given type ARG, return std::initializer_list<ARG>. */
26233
26234 static tree
26235 listify (tree arg)
26236 {
26237 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
26238
26239 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
26240 {
26241 gcc_rich_location richloc (input_location);
26242 maybe_add_include_fixit (&richloc, "<initializer_list>", false);
26243 error_at (&richloc,
26244 "deducing from brace-enclosed initializer list"
26245 " requires %<#include <initializer_list>%>");
26246
26247 return error_mark_node;
26248 }
26249 tree argvec = make_tree_vec (1);
26250 TREE_VEC_ELT (argvec, 0) = arg;
26251
26252 return lookup_template_class (std_init_list, argvec, NULL_TREE,
26253 NULL_TREE, 0, tf_warning_or_error);
26254 }
26255
26256 /* Replace auto in TYPE with std::initializer_list<auto>. */
26257
26258 static tree
26259 listify_autos (tree type, tree auto_node)
26260 {
26261 tree init_auto = listify (auto_node);
26262 tree argvec = make_tree_vec (1);
26263 TREE_VEC_ELT (argvec, 0) = init_auto;
26264 if (processing_template_decl)
26265 argvec = add_to_template_args (current_template_args (), argvec);
26266 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
26267 }
26268
26269 /* Hash traits for hashing possibly constrained 'auto'
26270 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
26271
26272 struct auto_hash : default_hash_traits<tree>
26273 {
26274 static inline hashval_t hash (tree);
26275 static inline bool equal (tree, tree);
26276 };
26277
26278 /* Hash the 'auto' T. */
26279
26280 inline hashval_t
26281 auto_hash::hash (tree t)
26282 {
26283 if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t)))
26284 /* Matching constrained-type-specifiers denote the same template
26285 parameter, so hash the constraint. */
26286 return hash_placeholder_constraint (c);
26287 else
26288 /* But unconstrained autos are all separate, so just hash the pointer. */
26289 return iterative_hash_object (t, 0);
26290 }
26291
26292 /* Compare two 'auto's. */
26293
26294 inline bool
26295 auto_hash::equal (tree t1, tree t2)
26296 {
26297 if (t1 == t2)
26298 return true;
26299
26300 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
26301 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
26302
26303 /* Two unconstrained autos are distinct. */
26304 if (!c1 || !c2)
26305 return false;
26306
26307 return equivalent_placeholder_constraints (c1, c2);
26308 }
26309
26310 /* for_each_template_parm callback for extract_autos: if t is a (possibly
26311 constrained) auto, add it to the vector. */
26312
26313 static int
26314 extract_autos_r (tree t, void *data)
26315 {
26316 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
26317 if (is_auto (t))
26318 {
26319 /* All the autos were built with index 0; fix that up now. */
26320 tree *p = hash.find_slot (t, INSERT);
26321 unsigned idx;
26322 if (*p)
26323 /* If this is a repeated constrained-type-specifier, use the index we
26324 chose before. */
26325 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
26326 else
26327 {
26328 /* Otherwise this is new, so use the current count. */
26329 *p = t;
26330 idx = hash.elements () - 1;
26331 }
26332 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
26333 }
26334
26335 /* Always keep walking. */
26336 return 0;
26337 }
26338
26339 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
26340 says they can appear anywhere in the type. */
26341
26342 static tree
26343 extract_autos (tree type)
26344 {
26345 hash_set<tree> visited;
26346 hash_table<auto_hash> hash (2);
26347
26348 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
26349
26350 tree tree_vec = make_tree_vec (hash.elements());
26351 for (hash_table<auto_hash>::iterator iter = hash.begin();
26352 iter != hash.end(); ++iter)
26353 {
26354 tree elt = *iter;
26355 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
26356 TREE_VEC_ELT (tree_vec, i)
26357 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
26358 }
26359
26360 return tree_vec;
26361 }
26362
26363 /* The stem for deduction guide names. */
26364 const char *const dguide_base = "__dguide_";
26365
26366 /* Return the name for a deduction guide for class template TMPL. */
26367
26368 tree
26369 dguide_name (tree tmpl)
26370 {
26371 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
26372 tree tname = TYPE_IDENTIFIER (type);
26373 char *buf = (char *) alloca (1 + strlen (dguide_base)
26374 + IDENTIFIER_LENGTH (tname));
26375 memcpy (buf, dguide_base, strlen (dguide_base));
26376 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
26377 IDENTIFIER_LENGTH (tname) + 1);
26378 tree dname = get_identifier (buf);
26379 TREE_TYPE (dname) = type;
26380 return dname;
26381 }
26382
26383 /* True if NAME is the name of a deduction guide. */
26384
26385 bool
26386 dguide_name_p (tree name)
26387 {
26388 return (TREE_CODE (name) == IDENTIFIER_NODE
26389 && TREE_TYPE (name)
26390 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
26391 strlen (dguide_base)));
26392 }
26393
26394 /* True if FN is a deduction guide. */
26395
26396 bool
26397 deduction_guide_p (const_tree fn)
26398 {
26399 if (DECL_P (fn))
26400 if (tree name = DECL_NAME (fn))
26401 return dguide_name_p (name);
26402 return false;
26403 }
26404
26405 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
26406
26407 bool
26408 copy_guide_p (const_tree fn)
26409 {
26410 gcc_assert (deduction_guide_p (fn));
26411 if (!DECL_ARTIFICIAL (fn))
26412 return false;
26413 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
26414 return (TREE_CHAIN (parms) == void_list_node
26415 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
26416 }
26417
26418 /* True if FN is a guide generated from a constructor template. */
26419
26420 bool
26421 template_guide_p (const_tree fn)
26422 {
26423 gcc_assert (deduction_guide_p (fn));
26424 if (!DECL_ARTIFICIAL (fn))
26425 return false;
26426 tree tmpl = DECL_TI_TEMPLATE (fn);
26427 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
26428 return PRIMARY_TEMPLATE_P (org);
26429 return false;
26430 }
26431
26432 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
26433 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
26434 template parameter types. Note that the handling of template template
26435 parameters relies on current_template_parms being set appropriately for the
26436 new template. */
26437
26438 static tree
26439 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
26440 tree tsubst_args, tsubst_flags_t complain)
26441 {
26442 if (olddecl == error_mark_node)
26443 return error_mark_node;
26444
26445 tree oldidx = get_template_parm_index (olddecl);
26446
26447 tree newtype;
26448 if (TREE_CODE (olddecl) == TYPE_DECL
26449 || TREE_CODE (olddecl) == TEMPLATE_DECL)
26450 {
26451 tree oldtype = TREE_TYPE (olddecl);
26452 newtype = cxx_make_type (TREE_CODE (oldtype));
26453 TYPE_MAIN_VARIANT (newtype) = newtype;
26454 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
26455 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
26456 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
26457 }
26458 else
26459 {
26460 newtype = TREE_TYPE (olddecl);
26461 if (type_uses_auto (newtype))
26462 {
26463 // Substitute once to fix references to other template parameters.
26464 newtype = tsubst (newtype, tsubst_args,
26465 complain|tf_partial, NULL_TREE);
26466 // Now substitute again to reduce the level of the auto.
26467 newtype = tsubst (newtype, current_template_args (),
26468 complain, NULL_TREE);
26469 }
26470 else
26471 newtype = tsubst (newtype, tsubst_args,
26472 complain, NULL_TREE);
26473 }
26474
26475 tree newdecl
26476 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
26477 DECL_NAME (olddecl), newtype);
26478 SET_DECL_TEMPLATE_PARM_P (newdecl);
26479
26480 tree newidx;
26481 if (TREE_CODE (olddecl) == TYPE_DECL
26482 || TREE_CODE (olddecl) == TEMPLATE_DECL)
26483 {
26484 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
26485 = build_template_parm_index (index, level, level,
26486 newdecl, newtype);
26487 TEMPLATE_PARM_PARAMETER_PACK (newidx)
26488 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
26489 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
26490 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
26491
26492 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
26493 {
26494 DECL_TEMPLATE_RESULT (newdecl)
26495 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
26496 DECL_NAME (olddecl), newtype);
26497 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
26498 // First create a copy (ttargs) of tsubst_args with an
26499 // additional level for the template template parameter's own
26500 // template parameters (ttparms).
26501 tree ttparms = (INNERMOST_TEMPLATE_PARMS
26502 (DECL_TEMPLATE_PARMS (olddecl)));
26503 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
26504 tree ttargs = make_tree_vec (depth + 1);
26505 for (int i = 0; i < depth; ++i)
26506 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
26507 TREE_VEC_ELT (ttargs, depth)
26508 = template_parms_level_to_args (ttparms);
26509 // Substitute ttargs into ttparms to fix references to
26510 // other template parameters.
26511 ttparms = tsubst_template_parms_level (ttparms, ttargs,
26512 complain|tf_partial);
26513 // Now substitute again with args based on tparms, to reduce
26514 // the level of the ttparms.
26515 ttargs = current_template_args ();
26516 ttparms = tsubst_template_parms_level (ttparms, ttargs,
26517 complain);
26518 // Finally, tack the adjusted parms onto tparms.
26519 ttparms = tree_cons (size_int (depth), ttparms,
26520 current_template_parms);
26521 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
26522 }
26523 }
26524 else
26525 {
26526 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
26527 tree newconst
26528 = build_decl (DECL_SOURCE_LOCATION (oldconst),
26529 TREE_CODE (oldconst),
26530 DECL_NAME (oldconst), newtype);
26531 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
26532 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
26533 SET_DECL_TEMPLATE_PARM_P (newconst);
26534 newidx = build_template_parm_index (index, level, level,
26535 newconst, newtype);
26536 TEMPLATE_PARM_PARAMETER_PACK (newidx)
26537 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
26538 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
26539 }
26540
26541 return newdecl;
26542 }
26543
26544 /* Returns a C++17 class deduction guide template based on the constructor
26545 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
26546 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
26547
26548 static tree
26549 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
26550 {
26551 tree type, tparms, targs, fparms, fargs, ci;
26552 bool memtmpl = false;
26553 bool explicit_p;
26554 location_t loc;
26555 tree fn_tmpl = NULL_TREE;
26556
26557 if (TYPE_P (ctor))
26558 {
26559 type = ctor;
26560 bool copy_p = TYPE_REF_P (type);
26561 if (copy_p)
26562 {
26563 type = TREE_TYPE (type);
26564 fparms = tree_cons (NULL_TREE, type, void_list_node);
26565 }
26566 else
26567 fparms = void_list_node;
26568
26569 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
26570 tparms = DECL_TEMPLATE_PARMS (ctmpl);
26571 targs = CLASSTYPE_TI_ARGS (type);
26572 ci = NULL_TREE;
26573 fargs = NULL_TREE;
26574 loc = DECL_SOURCE_LOCATION (ctmpl);
26575 explicit_p = false;
26576 }
26577 else
26578 {
26579 ++processing_template_decl;
26580 bool ok = true;
26581
26582 fn_tmpl
26583 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
26584 : DECL_TI_TEMPLATE (ctor));
26585 if (outer_args)
26586 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
26587 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
26588
26589 type = DECL_CONTEXT (ctor);
26590
26591 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
26592 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
26593 fully specialized args for the enclosing class. Strip those off, as
26594 the deduction guide won't have those template parameters. */
26595 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
26596 TMPL_PARMS_DEPTH (tparms));
26597 /* Discard the 'this' parameter. */
26598 fparms = FUNCTION_ARG_CHAIN (ctor);
26599 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
26600 ci = get_constraints (ctor);
26601 loc = DECL_SOURCE_LOCATION (ctor);
26602 explicit_p = DECL_NONCONVERTING_P (ctor);
26603
26604 if (PRIMARY_TEMPLATE_P (fn_tmpl))
26605 {
26606 memtmpl = true;
26607
26608 /* For a member template constructor, we need to flatten the two
26609 template parameter lists into one, and then adjust the function
26610 signature accordingly. This gets...complicated. */
26611 tree save_parms = current_template_parms;
26612
26613 /* For a member template we should have two levels of parms/args, one
26614 for the class and one for the constructor. We stripped
26615 specialized args for further enclosing classes above. */
26616 const int depth = 2;
26617 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
26618
26619 /* Template args for translating references to the two-level template
26620 parameters into references to the one-level template parameters we
26621 are creating. */
26622 tree tsubst_args = copy_node (targs);
26623 TMPL_ARGS_LEVEL (tsubst_args, depth)
26624 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
26625
26626 /* Template parms for the constructor template. */
26627 tree ftparms = TREE_VALUE (tparms);
26628 unsigned flen = TREE_VEC_LENGTH (ftparms);
26629 /* Template parms for the class template. */
26630 tparms = TREE_CHAIN (tparms);
26631 tree ctparms = TREE_VALUE (tparms);
26632 unsigned clen = TREE_VEC_LENGTH (ctparms);
26633 /* Template parms for the deduction guide start as a copy of the
26634 template parms for the class. We set current_template_parms for
26635 lookup_template_class_1. */
26636 current_template_parms = tparms = copy_node (tparms);
26637 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
26638 for (unsigned i = 0; i < clen; ++i)
26639 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
26640
26641 /* Now we need to rewrite the constructor parms to append them to the
26642 class parms. */
26643 for (unsigned i = 0; i < flen; ++i)
26644 {
26645 unsigned index = i + clen;
26646 unsigned level = 1;
26647 tree oldelt = TREE_VEC_ELT (ftparms, i);
26648 tree olddecl = TREE_VALUE (oldelt);
26649 tree newdecl = rewrite_template_parm (olddecl, index, level,
26650 tsubst_args, complain);
26651 if (newdecl == error_mark_node)
26652 ok = false;
26653 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
26654 tsubst_args, complain, ctor);
26655 tree list = build_tree_list (newdef, newdecl);
26656 TEMPLATE_PARM_CONSTRAINTS (list)
26657 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
26658 tsubst_args, complain, ctor);
26659 TREE_VEC_ELT (new_vec, index) = list;
26660 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
26661 }
26662
26663 /* Now we have a final set of template parms to substitute into the
26664 function signature. */
26665 targs = template_parms_to_args (tparms);
26666 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
26667 complain, ctor);
26668 fargs = tsubst (fargs, tsubst_args, complain, ctor);
26669 if (ci)
26670 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
26671
26672 current_template_parms = save_parms;
26673 }
26674
26675 --processing_template_decl;
26676 if (!ok)
26677 return error_mark_node;
26678 }
26679
26680 if (!memtmpl)
26681 {
26682 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
26683 tparms = copy_node (tparms);
26684 INNERMOST_TEMPLATE_PARMS (tparms)
26685 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
26686 }
26687
26688 tree fntype = build_function_type (type, fparms);
26689 tree ded_fn = build_lang_decl_loc (loc,
26690 FUNCTION_DECL,
26691 dguide_name (type), fntype);
26692 DECL_ARGUMENTS (ded_fn) = fargs;
26693 DECL_ARTIFICIAL (ded_fn) = true;
26694 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
26695 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
26696 DECL_ARTIFICIAL (ded_tmpl) = true;
26697 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
26698 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
26699 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
26700 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
26701 if (DECL_P (ctor))
26702 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
26703 if (ci)
26704 set_constraints (ded_tmpl, ci);
26705
26706 return ded_tmpl;
26707 }
26708
26709 /* Deduce template arguments for the class template placeholder PTYPE for
26710 template TMPL based on the initializer INIT, and return the resulting
26711 type. */
26712
26713 static tree
26714 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
26715 tsubst_flags_t complain)
26716 {
26717 if (!DECL_CLASS_TEMPLATE_P (tmpl))
26718 {
26719 /* We should have handled this in the caller. */
26720 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26721 return ptype;
26722 if (complain & tf_error)
26723 error ("non-class template %qT used without template arguments", tmpl);
26724 return error_mark_node;
26725 }
26726
26727 tree type = TREE_TYPE (tmpl);
26728
26729 bool try_list_ctor = false;
26730
26731 vec<tree,va_gc> *args;
26732 if (init == NULL_TREE
26733 || TREE_CODE (init) == TREE_LIST)
26734 args = make_tree_vector_from_list (init);
26735 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
26736 {
26737 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
26738 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
26739 {
26740 /* As an exception, the first phase in 16.3.1.7 (considering the
26741 initializer list as a single argument) is omitted if the
26742 initializer list consists of a single expression of type cv U,
26743 where U is a specialization of C or a class derived from a
26744 specialization of C. */
26745 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
26746 tree etype = TREE_TYPE (elt);
26747
26748 tree tparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
26749 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
26750 int err = unify (tparms, targs, type, etype,
26751 UNIFY_ALLOW_DERIVED, /*explain*/false);
26752 if (err == 0)
26753 try_list_ctor = false;
26754 ggc_free (targs);
26755 }
26756 if (try_list_ctor || is_std_init_list (type))
26757 args = make_tree_vector_single (init);
26758 else
26759 args = make_tree_vector_from_ctor (init);
26760 }
26761 else
26762 args = make_tree_vector_single (init);
26763
26764 tree dname = dguide_name (tmpl);
26765 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
26766 /*type*/false, /*complain*/false,
26767 /*hidden*/false);
26768 bool elided = false;
26769 if (cands == error_mark_node)
26770 cands = NULL_TREE;
26771
26772 /* Prune explicit deduction guides in copy-initialization context. */
26773 if (flags & LOOKUP_ONLYCONVERTING)
26774 {
26775 for (lkp_iterator iter (cands); !elided && iter; ++iter)
26776 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
26777 elided = true;
26778
26779 if (elided)
26780 {
26781 /* Found a nonconverting guide, prune the candidates. */
26782 tree pruned = NULL_TREE;
26783 for (lkp_iterator iter (cands); iter; ++iter)
26784 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
26785 pruned = lookup_add (*iter, pruned);
26786
26787 cands = pruned;
26788 }
26789 }
26790
26791 tree outer_args = NULL_TREE;
26792 if (DECL_CLASS_SCOPE_P (tmpl)
26793 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl)))
26794 {
26795 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
26796 type = TREE_TYPE (most_general_template (tmpl));
26797 }
26798
26799 bool saw_ctor = false;
26800 // FIXME cache artificial deduction guides
26801 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
26802 {
26803 /* Skip inherited constructors. */
26804 if (iter.using_p ())
26805 continue;
26806
26807 tree guide = build_deduction_guide (*iter, outer_args, complain);
26808 if (guide == error_mark_node)
26809 return error_mark_node;
26810 if ((flags & LOOKUP_ONLYCONVERTING)
26811 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
26812 elided = true;
26813 else
26814 cands = lookup_add (guide, cands);
26815
26816 saw_ctor = true;
26817 }
26818
26819 tree call = error_mark_node;
26820
26821 /* If this is list-initialization and the class has a list constructor, first
26822 try deducing from the list as a single argument, as [over.match.list]. */
26823 tree list_cands = NULL_TREE;
26824 if (try_list_ctor && cands)
26825 for (lkp_iterator iter (cands); iter; ++iter)
26826 {
26827 tree dg = *iter;
26828 if (is_list_ctor (dg))
26829 list_cands = lookup_add (dg, list_cands);
26830 }
26831 if (list_cands)
26832 {
26833 ++cp_unevaluated_operand;
26834 call = build_new_function_call (list_cands, &args, tf_decltype);
26835 --cp_unevaluated_operand;
26836
26837 if (call == error_mark_node)
26838 {
26839 /* That didn't work, now try treating the list as a sequence of
26840 arguments. */
26841 release_tree_vector (args);
26842 args = make_tree_vector_from_ctor (init);
26843 }
26844 }
26845
26846 /* Maybe generate an implicit deduction guide. */
26847 if (call == error_mark_node && args->length () < 2)
26848 {
26849 tree gtype = NULL_TREE;
26850
26851 if (args->length () == 1)
26852 /* Generate a copy guide. */
26853 gtype = build_reference_type (type);
26854 else if (!saw_ctor)
26855 /* Generate a default guide. */
26856 gtype = type;
26857
26858 if (gtype)
26859 {
26860 tree guide = build_deduction_guide (gtype, outer_args, complain);
26861 if (guide == error_mark_node)
26862 return error_mark_node;
26863 cands = lookup_add (guide, cands);
26864 }
26865 }
26866
26867 if (elided && !cands)
26868 {
26869 error ("cannot deduce template arguments for copy-initialization"
26870 " of %qT, as it has no non-explicit deduction guides or "
26871 "user-declared constructors", type);
26872 return error_mark_node;
26873 }
26874 else if (!cands && call == error_mark_node)
26875 {
26876 error ("cannot deduce template arguments of %qT, as it has no viable "
26877 "deduction guides", type);
26878 return error_mark_node;
26879 }
26880
26881 if (call == error_mark_node)
26882 {
26883 ++cp_unevaluated_operand;
26884 call = build_new_function_call (cands, &args, tf_decltype);
26885 --cp_unevaluated_operand;
26886 }
26887
26888 if (call == error_mark_node && (complain & tf_warning_or_error))
26889 {
26890 error ("class template argument deduction failed:");
26891
26892 ++cp_unevaluated_operand;
26893 call = build_new_function_call (cands, &args, complain | tf_decltype);
26894 --cp_unevaluated_operand;
26895
26896 if (elided)
26897 inform (input_location, "explicit deduction guides not considered "
26898 "for copy-initialization");
26899 }
26900
26901 release_tree_vector (args);
26902
26903 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
26904 }
26905
26906 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
26907 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
26908 The CONTEXT determines the context in which auto deduction is performed
26909 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
26910 OUTER_TARGS are used during template argument deduction
26911 (context == adc_unify) to properly substitute the result, and is ignored
26912 in other contexts.
26913
26914 For partial-concept-ids, extra args may be appended to the list of deduced
26915 template arguments prior to determining constraint satisfaction. */
26916
26917 tree
26918 do_auto_deduction (tree type, tree init, tree auto_node,
26919 tsubst_flags_t complain, auto_deduction_context context,
26920 tree outer_targs, int flags)
26921 {
26922 tree targs;
26923
26924 if (init == error_mark_node)
26925 return error_mark_node;
26926
26927 if (init && type_dependent_expression_p (init)
26928 && context != adc_unify)
26929 /* Defining a subset of type-dependent expressions that we can deduce
26930 from ahead of time isn't worth the trouble. */
26931 return type;
26932
26933 /* Similarly, we can't deduce from another undeduced decl. */
26934 if (init && undeduced_auto_decl (init))
26935 return type;
26936
26937 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
26938 /* C++17 class template argument deduction. */
26939 return do_class_deduction (type, tmpl, init, flags, complain);
26940
26941 if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
26942 /* Nothing we can do with this, even in deduction context. */
26943 return type;
26944
26945 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
26946 with either a new invented type template parameter U or, if the
26947 initializer is a braced-init-list (8.5.4), with
26948 std::initializer_list<U>. */
26949 if (BRACE_ENCLOSED_INITIALIZER_P (init))
26950 {
26951 if (!DIRECT_LIST_INIT_P (init))
26952 type = listify_autos (type, auto_node);
26953 else if (CONSTRUCTOR_NELTS (init) == 1)
26954 init = CONSTRUCTOR_ELT (init, 0)->value;
26955 else
26956 {
26957 if (complain & tf_warning_or_error)
26958 {
26959 if (permerror (input_location, "direct-list-initialization of "
26960 "%<auto%> requires exactly one element"))
26961 inform (input_location,
26962 "for deduction to %<std::initializer_list%>, use copy-"
26963 "list-initialization (i.e. add %<=%> before the %<{%>)");
26964 }
26965 type = listify_autos (type, auto_node);
26966 }
26967 }
26968
26969 if (type == error_mark_node)
26970 return error_mark_node;
26971
26972 init = resolve_nondeduced_context (init, complain);
26973
26974 if (context == adc_decomp_type
26975 && auto_node == type
26976 && init != error_mark_node
26977 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
26978 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
26979 and initializer has array type, deduce cv-qualified array type. */
26980 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
26981 complain);
26982 else if (AUTO_IS_DECLTYPE (auto_node))
26983 {
26984 bool id = (DECL_P (init)
26985 || ((TREE_CODE (init) == COMPONENT_REF
26986 || TREE_CODE (init) == SCOPE_REF)
26987 && !REF_PARENTHESIZED_P (init)));
26988 targs = make_tree_vec (1);
26989 TREE_VEC_ELT (targs, 0)
26990 = finish_decltype_type (init, id, tf_warning_or_error);
26991 if (type != auto_node)
26992 {
26993 if (complain & tf_error)
26994 error ("%qT as type rather than plain %<decltype(auto)%>", type);
26995 return error_mark_node;
26996 }
26997 }
26998 else
26999 {
27000 tree parms = build_tree_list (NULL_TREE, type);
27001 tree tparms;
27002
27003 if (flag_concepts)
27004 tparms = extract_autos (type);
27005 else
27006 {
27007 tparms = make_tree_vec (1);
27008 TREE_VEC_ELT (tparms, 0)
27009 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
27010 }
27011
27012 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
27013 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
27014 DEDUCE_CALL,
27015 NULL, /*explain_p=*/false);
27016 if (val > 0)
27017 {
27018 if (processing_template_decl)
27019 /* Try again at instantiation time. */
27020 return type;
27021 if (type && type != error_mark_node
27022 && (complain & tf_error))
27023 /* If type is error_mark_node a diagnostic must have been
27024 emitted by now. Also, having a mention to '<type error>'
27025 in the diagnostic is not really useful to the user. */
27026 {
27027 if (cfun && auto_node == current_function_auto_return_pattern
27028 && LAMBDA_FUNCTION_P (current_function_decl))
27029 error ("unable to deduce lambda return type from %qE", init);
27030 else
27031 error ("unable to deduce %qT from %qE", type, init);
27032 type_unification_real (tparms, targs, parms, &init, 1, 0,
27033 DEDUCE_CALL,
27034 NULL, /*explain_p=*/true);
27035 }
27036 return error_mark_node;
27037 }
27038 }
27039
27040 /* Check any placeholder constraints against the deduced type. */
27041 if (flag_concepts && !processing_template_decl)
27042 if (tree constr = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node)))
27043 {
27044 /* Use the deduced type to check the associated constraints. If we
27045 have a partial-concept-id, rebuild the argument list so that
27046 we check using the extra arguments. */
27047 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
27048 tree cargs = CHECK_CONSTR_ARGS (constr);
27049 if (TREE_VEC_LENGTH (cargs) > 1)
27050 {
27051 cargs = copy_node (cargs);
27052 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
27053 }
27054 else
27055 cargs = targs;
27056 if (!constraints_satisfied_p (constr, cargs))
27057 {
27058 if (complain & tf_warning_or_error)
27059 {
27060 auto_diagnostic_group d;
27061 switch (context)
27062 {
27063 case adc_unspecified:
27064 case adc_unify:
27065 error("placeholder constraints not satisfied");
27066 break;
27067 case adc_variable_type:
27068 case adc_decomp_type:
27069 error ("deduced initializer does not satisfy "
27070 "placeholder constraints");
27071 break;
27072 case adc_return_type:
27073 error ("deduced return type does not satisfy "
27074 "placeholder constraints");
27075 break;
27076 case adc_requirement:
27077 error ("deduced expression type does not satisfy "
27078 "placeholder constraints");
27079 break;
27080 }
27081 diagnose_constraints (input_location, constr, targs);
27082 }
27083 return error_mark_node;
27084 }
27085 }
27086
27087 if (processing_template_decl && context != adc_unify)
27088 outer_targs = current_template_args ();
27089 targs = add_to_template_args (outer_targs, targs);
27090 return tsubst (type, targs, complain, NULL_TREE);
27091 }
27092
27093 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
27094 result. */
27095
27096 tree
27097 splice_late_return_type (tree type, tree late_return_type)
27098 {
27099 if (is_auto (type))
27100 {
27101 if (late_return_type)
27102 return late_return_type;
27103
27104 tree idx = get_template_parm_index (type);
27105 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
27106 /* In an abbreviated function template we didn't know we were dealing
27107 with a function template when we saw the auto return type, so update
27108 it to have the correct level. */
27109 return make_auto_1 (TYPE_IDENTIFIER (type), true);
27110 }
27111 return type;
27112 }
27113
27114 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
27115 'decltype(auto)' or a deduced class template. */
27116
27117 bool
27118 is_auto (const_tree type)
27119 {
27120 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
27121 && (TYPE_IDENTIFIER (type) == auto_identifier
27122 || TYPE_IDENTIFIER (type) == decltype_auto_identifier
27123 || CLASS_PLACEHOLDER_TEMPLATE (type)))
27124 return true;
27125 else
27126 return false;
27127 }
27128
27129 /* for_each_template_parm callback for type_uses_auto. */
27130
27131 int
27132 is_auto_r (tree tp, void */*data*/)
27133 {
27134 return is_auto (tp);
27135 }
27136
27137 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
27138 a use of `auto'. Returns NULL_TREE otherwise. */
27139
27140 tree
27141 type_uses_auto (tree type)
27142 {
27143 if (type == NULL_TREE)
27144 return NULL_TREE;
27145 else if (flag_concepts)
27146 {
27147 /* The Concepts TS allows multiple autos in one type-specifier; just
27148 return the first one we find, do_auto_deduction will collect all of
27149 them. */
27150 if (uses_template_parms (type))
27151 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
27152 /*visited*/NULL, /*nondeduced*/false);
27153 else
27154 return NULL_TREE;
27155 }
27156 else
27157 return find_type_usage (type, is_auto);
27158 }
27159
27160 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
27161 concepts are enabled, auto is acceptable in template arguments, but
27162 only when TEMPL identifies a template class. Return TRUE if any
27163 such errors were reported. */
27164
27165 bool
27166 check_auto_in_tmpl_args (tree tmpl, tree args)
27167 {
27168 /* If there were previous errors, nevermind. */
27169 if (!args || TREE_CODE (args) != TREE_VEC)
27170 return false;
27171
27172 /* If TMPL is an identifier, we're parsing and we can't tell yet
27173 whether TMPL is supposed to be a type, a function or a variable.
27174 We'll only be able to tell during template substitution, so we
27175 expect to be called again then. If concepts are enabled and we
27176 know we have a type, we're ok. */
27177 if (flag_concepts
27178 && (identifier_p (tmpl)
27179 || (DECL_P (tmpl)
27180 && (DECL_TYPE_TEMPLATE_P (tmpl)
27181 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))))
27182 return false;
27183
27184 /* Quickly search for any occurrences of auto; usually there won't
27185 be any, and then we'll avoid allocating the vector. */
27186 if (!type_uses_auto (args))
27187 return false;
27188
27189 bool errors = false;
27190
27191 tree vec = extract_autos (args);
27192 for (int i = 0; i < TREE_VEC_LENGTH (vec); i++)
27193 {
27194 tree xauto = TREE_VALUE (TREE_VEC_ELT (vec, i));
27195 error_at (DECL_SOURCE_LOCATION (xauto),
27196 "invalid use of %qT in template argument", xauto);
27197 errors = true;
27198 }
27199
27200 return errors;
27201 }
27202
27203 /* For a given template T, return the vector of typedefs referenced
27204 in T for which access check is needed at T instantiation time.
27205 T is either a FUNCTION_DECL or a RECORD_TYPE.
27206 Those typedefs were added to T by the function
27207 append_type_to_template_for_access_check. */
27208
27209 vec<qualified_typedef_usage_t, va_gc> *
27210 get_types_needing_access_check (tree t)
27211 {
27212 tree ti;
27213 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
27214
27215 if (!t || t == error_mark_node)
27216 return NULL;
27217
27218 if (!(ti = get_template_info (t)))
27219 return NULL;
27220
27221 if (CLASS_TYPE_P (t)
27222 || TREE_CODE (t) == FUNCTION_DECL)
27223 {
27224 if (!TI_TEMPLATE (ti))
27225 return NULL;
27226
27227 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
27228 }
27229
27230 return result;
27231 }
27232
27233 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
27234 tied to T. That list of typedefs will be access checked at
27235 T instantiation time.
27236 T is either a FUNCTION_DECL or a RECORD_TYPE.
27237 TYPE_DECL is a TYPE_DECL node representing a typedef.
27238 SCOPE is the scope through which TYPE_DECL is accessed.
27239 LOCATION is the location of the usage point of TYPE_DECL.
27240
27241 This function is a subroutine of
27242 append_type_to_template_for_access_check. */
27243
27244 static void
27245 append_type_to_template_for_access_check_1 (tree t,
27246 tree type_decl,
27247 tree scope,
27248 location_t location)
27249 {
27250 qualified_typedef_usage_t typedef_usage;
27251 tree ti;
27252
27253 if (!t || t == error_mark_node)
27254 return;
27255
27256 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
27257 || CLASS_TYPE_P (t))
27258 && type_decl
27259 && TREE_CODE (type_decl) == TYPE_DECL
27260 && scope);
27261
27262 if (!(ti = get_template_info (t)))
27263 return;
27264
27265 gcc_assert (TI_TEMPLATE (ti));
27266
27267 typedef_usage.typedef_decl = type_decl;
27268 typedef_usage.context = scope;
27269 typedef_usage.locus = location;
27270
27271 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
27272 }
27273
27274 /* Append TYPE_DECL to the template TEMPL.
27275 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
27276 At TEMPL instanciation time, TYPE_DECL will be checked to see
27277 if it can be accessed through SCOPE.
27278 LOCATION is the location of the usage point of TYPE_DECL.
27279
27280 e.g. consider the following code snippet:
27281
27282 class C
27283 {
27284 typedef int myint;
27285 };
27286
27287 template<class U> struct S
27288 {
27289 C::myint mi; // <-- usage point of the typedef C::myint
27290 };
27291
27292 S<char> s;
27293
27294 At S<char> instantiation time, we need to check the access of C::myint
27295 In other words, we need to check the access of the myint typedef through
27296 the C scope. For that purpose, this function will add the myint typedef
27297 and the scope C through which its being accessed to a list of typedefs
27298 tied to the template S. That list will be walked at template instantiation
27299 time and access check performed on each typedefs it contains.
27300 Note that this particular code snippet should yield an error because
27301 myint is private to C. */
27302
27303 void
27304 append_type_to_template_for_access_check (tree templ,
27305 tree type_decl,
27306 tree scope,
27307 location_t location)
27308 {
27309 qualified_typedef_usage_t *iter;
27310 unsigned i;
27311
27312 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
27313
27314 /* Make sure we don't append the type to the template twice. */
27315 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
27316 if (iter->typedef_decl == type_decl && scope == iter->context)
27317 return;
27318
27319 append_type_to_template_for_access_check_1 (templ, type_decl,
27320 scope, location);
27321 }
27322
27323 /* Convert the generic type parameters in PARM that match the types given in the
27324 range [START_IDX, END_IDX) from the current_template_parms into generic type
27325 packs. */
27326
27327 tree
27328 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
27329 {
27330 tree current = current_template_parms;
27331 int depth = TMPL_PARMS_DEPTH (current);
27332 current = INNERMOST_TEMPLATE_PARMS (current);
27333 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
27334
27335 for (int i = 0; i < start_idx; ++i)
27336 TREE_VEC_ELT (replacement, i)
27337 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
27338
27339 for (int i = start_idx; i < end_idx; ++i)
27340 {
27341 /* Create a distinct parameter pack type from the current parm and add it
27342 to the replacement args to tsubst below into the generic function
27343 parameter. */
27344
27345 tree o = TREE_TYPE (TREE_VALUE
27346 (TREE_VEC_ELT (current, i)));
27347 tree t = copy_type (o);
27348 TEMPLATE_TYPE_PARM_INDEX (t)
27349 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
27350 o, 0, 0, tf_none);
27351 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
27352 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
27353 TYPE_MAIN_VARIANT (t) = t;
27354 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
27355 TYPE_CANONICAL (t) = canonical_type_parameter (t);
27356 TREE_VEC_ELT (replacement, i) = t;
27357 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
27358 }
27359
27360 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
27361 TREE_VEC_ELT (replacement, i)
27362 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
27363
27364 /* If there are more levels then build up the replacement with the outer
27365 template parms. */
27366 if (depth > 1)
27367 replacement = add_to_template_args (template_parms_to_args
27368 (TREE_CHAIN (current_template_parms)),
27369 replacement);
27370
27371 return tsubst (parm, replacement, tf_none, NULL_TREE);
27372 }
27373
27374 /* Entries in the decl_constraint hash table. */
27375 struct GTY((for_user)) constr_entry
27376 {
27377 tree decl;
27378 tree ci;
27379 };
27380
27381 /* Hashing function and equality for constraint entries. */
27382 struct constr_hasher : ggc_ptr_hash<constr_entry>
27383 {
27384 static hashval_t hash (constr_entry *e)
27385 {
27386 return (hashval_t)DECL_UID (e->decl);
27387 }
27388
27389 static bool equal (constr_entry *e1, constr_entry *e2)
27390 {
27391 return e1->decl == e2->decl;
27392 }
27393 };
27394
27395 /* A mapping from declarations to constraint information. Note that
27396 both templates and their underlying declarations are mapped to the
27397 same constraint information.
27398
27399 FIXME: This is defined in pt.c because garbage collection
27400 code is not being generated for constraint.cc. */
27401
27402 static GTY (()) hash_table<constr_hasher> *decl_constraints;
27403
27404 /* Returns the template constraints of declaration T. If T is not
27405 constrained, return NULL_TREE. Note that T must be non-null. */
27406
27407 tree
27408 get_constraints (tree t)
27409 {
27410 if (!flag_concepts)
27411 return NULL_TREE;
27412
27413 gcc_assert (DECL_P (t));
27414 if (TREE_CODE (t) == TEMPLATE_DECL)
27415 t = DECL_TEMPLATE_RESULT (t);
27416 constr_entry elt = { t, NULL_TREE };
27417 constr_entry* found = decl_constraints->find (&elt);
27418 if (found)
27419 return found->ci;
27420 else
27421 return NULL_TREE;
27422 }
27423
27424 /* Associate the given constraint information CI with the declaration
27425 T. If T is a template, then the constraints are associated with
27426 its underlying declaration. Don't build associations if CI is
27427 NULL_TREE. */
27428
27429 void
27430 set_constraints (tree t, tree ci)
27431 {
27432 if (!ci)
27433 return;
27434 gcc_assert (t && flag_concepts);
27435 if (TREE_CODE (t) == TEMPLATE_DECL)
27436 t = DECL_TEMPLATE_RESULT (t);
27437 gcc_assert (!get_constraints (t));
27438 constr_entry elt = {t, ci};
27439 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
27440 constr_entry* entry = ggc_alloc<constr_entry> ();
27441 *entry = elt;
27442 *slot = entry;
27443 }
27444
27445 /* Remove the associated constraints of the declaration T. */
27446
27447 void
27448 remove_constraints (tree t)
27449 {
27450 gcc_assert (DECL_P (t));
27451 if (TREE_CODE (t) == TEMPLATE_DECL)
27452 t = DECL_TEMPLATE_RESULT (t);
27453
27454 constr_entry elt = {t, NULL_TREE};
27455 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
27456 if (slot)
27457 decl_constraints->clear_slot (slot);
27458 }
27459
27460 /* Memoized satisfaction results for declarations. This
27461 maps the pair (constraint_info, arguments) to the result computed
27462 by constraints_satisfied_p. */
27463
27464 struct GTY((for_user)) constraint_sat_entry
27465 {
27466 tree ci;
27467 tree args;
27468 tree result;
27469 };
27470
27471 /* Hashing function and equality for constraint entries. */
27472
27473 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
27474 {
27475 static hashval_t hash (constraint_sat_entry *e)
27476 {
27477 hashval_t val = iterative_hash_object(e->ci, 0);
27478 return iterative_hash_template_arg (e->args, val);
27479 }
27480
27481 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
27482 {
27483 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
27484 }
27485 };
27486
27487 /* Memoized satisfaction results for concept checks. */
27488
27489 struct GTY((for_user)) concept_spec_entry
27490 {
27491 tree tmpl;
27492 tree args;
27493 tree result;
27494 };
27495
27496 /* Hashing function and equality for constraint entries. */
27497
27498 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
27499 {
27500 static hashval_t hash (concept_spec_entry *e)
27501 {
27502 return hash_tmpl_and_args (e->tmpl, e->args);
27503 }
27504
27505 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
27506 {
27507 ++comparing_specializations;
27508 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
27509 --comparing_specializations;
27510 return eq;
27511 }
27512 };
27513
27514 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
27515 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
27516
27517 /* Search for a memoized satisfaction result. Returns one of the
27518 truth value nodes if previously memoized, or NULL_TREE otherwise. */
27519
27520 tree
27521 lookup_constraint_satisfaction (tree ci, tree args)
27522 {
27523 constraint_sat_entry elt = { ci, args, NULL_TREE };
27524 constraint_sat_entry* found = constraint_memos->find (&elt);
27525 if (found)
27526 return found->result;
27527 else
27528 return NULL_TREE;
27529 }
27530
27531 /* Memoize the result of a satisfication test. Returns the saved result. */
27532
27533 tree
27534 memoize_constraint_satisfaction (tree ci, tree args, tree result)
27535 {
27536 constraint_sat_entry elt = {ci, args, result};
27537 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
27538 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
27539 *entry = elt;
27540 *slot = entry;
27541 return result;
27542 }
27543
27544 /* Search for a memoized satisfaction result for a concept. */
27545
27546 tree
27547 lookup_concept_satisfaction (tree tmpl, tree args)
27548 {
27549 concept_spec_entry elt = { tmpl, args, NULL_TREE };
27550 concept_spec_entry* found = concept_memos->find (&elt);
27551 if (found)
27552 return found->result;
27553 else
27554 return NULL_TREE;
27555 }
27556
27557 /* Memoize the result of a concept check. Returns the saved result. */
27558
27559 tree
27560 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
27561 {
27562 concept_spec_entry elt = {tmpl, args, result};
27563 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
27564 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
27565 *entry = elt;
27566 *slot = entry;
27567 return result;
27568 }
27569
27570 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
27571
27572 /* Returns a prior concept specialization. This returns the substituted
27573 and normalized constraints defined by the concept. */
27574
27575 tree
27576 get_concept_expansion (tree tmpl, tree args)
27577 {
27578 concept_spec_entry elt = { tmpl, args, NULL_TREE };
27579 concept_spec_entry* found = concept_expansions->find (&elt);
27580 if (found)
27581 return found->result;
27582 else
27583 return NULL_TREE;
27584 }
27585
27586 /* Save a concept expansion for later. */
27587
27588 tree
27589 save_concept_expansion (tree tmpl, tree args, tree def)
27590 {
27591 concept_spec_entry elt = {tmpl, args, def};
27592 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
27593 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
27594 *entry = elt;
27595 *slot = entry;
27596 return def;
27597 }
27598
27599 static hashval_t
27600 hash_subsumption_args (tree t1, tree t2)
27601 {
27602 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
27603 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
27604 int val = 0;
27605 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
27606 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
27607 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
27608 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
27609 return val;
27610 }
27611
27612 /* Compare the constraints of two subsumption entries. The LEFT1 and
27613 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
27614 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
27615
27616 static bool
27617 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
27618 {
27619 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
27620 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
27621 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
27622 CHECK_CONSTR_ARGS (right1)))
27623 return comp_template_args (CHECK_CONSTR_ARGS (left2),
27624 CHECK_CONSTR_ARGS (right2));
27625 return false;
27626 }
27627
27628 /* Key/value pair for learning and memoizing subsumption results. This
27629 associates a pair of check constraints (including arguments) with
27630 a boolean value indicating the result. */
27631
27632 struct GTY((for_user)) subsumption_entry
27633 {
27634 tree t1;
27635 tree t2;
27636 bool result;
27637 };
27638
27639 /* Hashing function and equality for constraint entries. */
27640
27641 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
27642 {
27643 static hashval_t hash (subsumption_entry *e)
27644 {
27645 return hash_subsumption_args (e->t1, e->t2);
27646 }
27647
27648 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
27649 {
27650 ++comparing_specializations;
27651 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
27652 --comparing_specializations;
27653 return eq;
27654 }
27655 };
27656
27657 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
27658
27659 /* Search for a previously cached subsumption result. */
27660
27661 bool*
27662 lookup_subsumption_result (tree t1, tree t2)
27663 {
27664 subsumption_entry elt = { t1, t2, false };
27665 subsumption_entry* found = subsumption_table->find (&elt);
27666 if (found)
27667 return &found->result;
27668 else
27669 return 0;
27670 }
27671
27672 /* Save a subsumption result. */
27673
27674 bool
27675 save_subsumption_result (tree t1, tree t2, bool result)
27676 {
27677 subsumption_entry elt = {t1, t2, result};
27678 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
27679 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
27680 *entry = elt;
27681 *slot = entry;
27682 return result;
27683 }
27684
27685 /* Set up the hash table for constraint association. */
27686
27687 void
27688 init_constraint_processing (void)
27689 {
27690 if (!flag_concepts)
27691 return;
27692
27693 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
27694 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
27695 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
27696 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
27697 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
27698 }
27699
27700 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
27701 0..N-1. */
27702
27703 void
27704 declare_integer_pack (void)
27705 {
27706 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
27707 build_function_type_list (integer_type_node,
27708 integer_type_node,
27709 NULL_TREE),
27710 NULL_TREE, ECF_CONST);
27711 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
27712 DECL_BUILT_IN_CLASS (ipfn) = BUILT_IN_FRONTEND;
27713 DECL_FUNCTION_CODE (ipfn)
27714 = (enum built_in_function) (int) CP_BUILT_IN_INTEGER_PACK;
27715 }
27716
27717 /* Set up the hash tables for template instantiations. */
27718
27719 void
27720 init_template_processing (void)
27721 {
27722 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
27723 type_specializations = hash_table<spec_hasher>::create_ggc (37);
27724
27725 if (cxx_dialect >= cxx11)
27726 declare_integer_pack ();
27727 }
27728
27729 /* Print stats about the template hash tables for -fstats. */
27730
27731 void
27732 print_template_statistics (void)
27733 {
27734 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
27735 "%f collisions\n", (long) decl_specializations->size (),
27736 (long) decl_specializations->elements (),
27737 decl_specializations->collisions ());
27738 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
27739 "%f collisions\n", (long) type_specializations->size (),
27740 (long) type_specializations->elements (),
27741 type_specializations->collisions ());
27742 }
27743
27744 #if CHECKING_P
27745
27746 namespace selftest {
27747
27748 /* Verify that build_non_dependent_expr () works, for various expressions,
27749 and that location wrappers don't affect the results. */
27750
27751 static void
27752 test_build_non_dependent_expr ()
27753 {
27754 location_t loc = BUILTINS_LOCATION;
27755
27756 /* Verify constants, without and with location wrappers. */
27757 tree int_cst = build_int_cst (integer_type_node, 42);
27758 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
27759
27760 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
27761 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
27762 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
27763
27764 tree string_lit = build_string (4, "foo");
27765 TREE_TYPE (string_lit) = char_array_type_node;
27766 string_lit = fix_string_type (string_lit);
27767 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
27768
27769 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
27770 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
27771 ASSERT_EQ (wrapped_string_lit,
27772 build_non_dependent_expr (wrapped_string_lit));
27773 }
27774
27775 /* Verify that type_dependent_expression_p () works correctly, even
27776 in the presence of location wrapper nodes. */
27777
27778 static void
27779 test_type_dependent_expression_p ()
27780 {
27781 location_t loc = BUILTINS_LOCATION;
27782
27783 tree name = get_identifier ("foo");
27784
27785 /* If no templates are involved, nothing is type-dependent. */
27786 gcc_assert (!processing_template_decl);
27787 ASSERT_FALSE (type_dependent_expression_p (name));
27788
27789 ++processing_template_decl;
27790
27791 /* Within a template, an unresolved name is always type-dependent. */
27792 ASSERT_TRUE (type_dependent_expression_p (name));
27793
27794 /* Ensure it copes with NULL_TREE and errors. */
27795 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
27796 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
27797
27798 /* A USING_DECL in a template should be type-dependent, even if wrapped
27799 with a location wrapper (PR c++/83799). */
27800 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
27801 TREE_TYPE (using_decl) = integer_type_node;
27802 ASSERT_TRUE (type_dependent_expression_p (using_decl));
27803 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
27804 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
27805 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
27806
27807 --processing_template_decl;
27808 }
27809
27810 /* Run all of the selftests within this file. */
27811
27812 void
27813 cp_pt_c_tests ()
27814 {
27815 test_build_non_dependent_expr ();
27816 test_type_dependent_expression_p ();
27817 }
27818
27819 } // namespace selftest
27820
27821 #endif /* #if CHECKING_P */
27822
27823 #include "gt-cp-pt.h"