]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/pt.c
PR c++/91923 - failure-to-SFINAE with class type NTTP in C++17.
[thirdparty/gcc.git] / gcc / cp / pt.c
1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2019 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 /* Known bugs or deficiencies include:
23
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
26
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "cp-tree.h"
31 #include "timevar.h"
32 #include "stringpool.h"
33 #include "varasm.h"
34 #include "attribs.h"
35 #include "stor-layout.h"
36 #include "intl.h"
37 #include "c-family/c-objc.h"
38 #include "cp-objcp-common.h"
39 #include "toplev.h"
40 #include "tree-iterator.h"
41 #include "type-utils.h"
42 #include "gimplify.h"
43 #include "gcc-rich-location.h"
44 #include "selftest.h"
45
46 /* The type of functions taking a tree, and some additional data, and
47 returning an int. */
48 typedef int (*tree_fn_t) (tree, void*);
49
50 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
51 instantiations have been deferred, either because their definitions
52 were not yet available, or because we were putting off doing the work. */
53 struct GTY ((chain_next ("%h.next"))) pending_template
54 {
55 struct pending_template *next;
56 struct tinst_level *tinst;
57 };
58
59 static GTY(()) struct pending_template *pending_templates;
60 static GTY(()) struct pending_template *last_pending_template;
61
62 int processing_template_parmlist;
63 static int template_header_count;
64
65 static GTY(()) tree saved_trees;
66 static vec<int> inline_parm_levels;
67
68 static GTY(()) struct tinst_level *current_tinst_level;
69
70 static GTY(()) vec<tree, va_gc> *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 vec_safe_push (saved_access_scope, current_function_decl);
251 current_function_decl = t;
252 }
253 }
254
255 /* Restore the scope set up by push_access_scope. T is the node we
256 are processing. */
257
258 static void
259 pop_access_scope (tree t)
260 {
261 if (TREE_CODE (t) == FUNCTION_DECL)
262 current_function_decl = saved_access_scope->pop();
263
264 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
265 pop_nested_class ();
266 else
267 pop_from_top_level ();
268 }
269
270 /* Do any processing required when DECL (a member template
271 declaration) is finished. Returns the TEMPLATE_DECL corresponding
272 to DECL, unless it is a specialization, in which case the DECL
273 itself is returned. */
274
275 tree
276 finish_member_template_decl (tree decl)
277 {
278 if (decl == error_mark_node)
279 return error_mark_node;
280
281 gcc_assert (DECL_P (decl));
282
283 if (TREE_CODE (decl) == TYPE_DECL)
284 {
285 tree type;
286
287 type = TREE_TYPE (decl);
288 if (type == error_mark_node)
289 return error_mark_node;
290 if (MAYBE_CLASS_TYPE_P (type)
291 && CLASSTYPE_TEMPLATE_INFO (type)
292 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
293 {
294 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
295 check_member_template (tmpl);
296 return tmpl;
297 }
298 return NULL_TREE;
299 }
300 else if (TREE_CODE (decl) == FIELD_DECL)
301 error_at (DECL_SOURCE_LOCATION (decl),
302 "data member %qD cannot be a member template", decl);
303 else if (DECL_TEMPLATE_INFO (decl))
304 {
305 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
306 {
307 check_member_template (DECL_TI_TEMPLATE (decl));
308 return DECL_TI_TEMPLATE (decl);
309 }
310 else
311 return decl;
312 }
313 else
314 error_at (DECL_SOURCE_LOCATION (decl),
315 "invalid member template declaration %qD", decl);
316
317 return error_mark_node;
318 }
319
320 /* Create a template info node. */
321
322 tree
323 build_template_info (tree template_decl, tree template_args)
324 {
325 tree result = make_node (TEMPLATE_INFO);
326 TI_TEMPLATE (result) = template_decl;
327 TI_ARGS (result) = template_args;
328 return result;
329 }
330
331 /* Return the template info node corresponding to T, whatever T is. */
332
333 tree
334 get_template_info (const_tree t)
335 {
336 tree tinfo = NULL_TREE;
337
338 if (!t || t == error_mark_node)
339 return NULL;
340
341 if (TREE_CODE (t) == NAMESPACE_DECL
342 || TREE_CODE (t) == PARM_DECL)
343 return NULL;
344
345 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
346 tinfo = DECL_TEMPLATE_INFO (t);
347
348 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
349 t = TREE_TYPE (t);
350
351 if (OVERLOAD_TYPE_P (t))
352 tinfo = TYPE_TEMPLATE_INFO (t);
353 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
354 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
355
356 return tinfo;
357 }
358
359 /* Returns the template nesting level of the indicated class TYPE.
360
361 For example, in:
362 template <class T>
363 struct A
364 {
365 template <class U>
366 struct B {};
367 };
368
369 A<T>::B<U> has depth two, while A<T> has depth one.
370 Both A<T>::B<int> and A<int>::B<U> have depth one, if
371 they are instantiations, not specializations.
372
373 This function is guaranteed to return 0 if passed NULL_TREE so
374 that, for example, `template_class_depth (current_class_type)' is
375 always safe. */
376
377 int
378 template_class_depth (tree type)
379 {
380 int depth;
381
382 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
383 {
384 tree tinfo = get_template_info (type);
385
386 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
387 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
388 ++depth;
389
390 if (DECL_P (type))
391 type = CP_DECL_CONTEXT (type);
392 else if (LAMBDA_TYPE_P (type))
393 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
394 else
395 type = CP_TYPE_CONTEXT (type);
396 }
397
398 return depth;
399 }
400
401 /* Return TRUE if NODE instantiates a template that has arguments of
402 its own, be it directly a primary template or indirectly through a
403 partial specializations. */
404 static bool
405 instantiates_primary_template_p (tree node)
406 {
407 tree tinfo = get_template_info (node);
408 if (!tinfo)
409 return false;
410
411 tree tmpl = TI_TEMPLATE (tinfo);
412 if (PRIMARY_TEMPLATE_P (tmpl))
413 return true;
414
415 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl))
416 return false;
417
418 /* So now we know we have a specialization, but it could be a full
419 or a partial specialization. To tell which, compare the depth of
420 its template arguments with those of its context. */
421
422 tree ctxt = DECL_CONTEXT (tmpl);
423 tree ctinfo = get_template_info (ctxt);
424 if (!ctinfo)
425 return true;
426
427 return (TMPL_ARGS_DEPTH (TI_ARGS (tinfo))
428 > TMPL_ARGS_DEPTH (TI_ARGS (ctinfo)));
429 }
430
431 /* Subroutine of maybe_begin_member_template_processing.
432 Returns true if processing DECL needs us to push template parms. */
433
434 static bool
435 inline_needs_template_parms (tree decl, bool nsdmi)
436 {
437 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
438 return false;
439
440 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
441 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
442 }
443
444 /* Subroutine of maybe_begin_member_template_processing.
445 Push the template parms in PARMS, starting from LEVELS steps into the
446 chain, and ending at the beginning, since template parms are listed
447 innermost first. */
448
449 static void
450 push_inline_template_parms_recursive (tree parmlist, int levels)
451 {
452 tree parms = TREE_VALUE (parmlist);
453 int i;
454
455 if (levels > 1)
456 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
457
458 ++processing_template_decl;
459 current_template_parms
460 = tree_cons (size_int (processing_template_decl),
461 parms, current_template_parms);
462 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
463
464 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
465 NULL);
466 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
467 {
468 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
469
470 if (error_operand_p (parm))
471 continue;
472
473 gcc_assert (DECL_P (parm));
474
475 switch (TREE_CODE (parm))
476 {
477 case TYPE_DECL:
478 case TEMPLATE_DECL:
479 pushdecl (parm);
480 break;
481
482 case PARM_DECL:
483 /* Push the CONST_DECL. */
484 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
485 break;
486
487 default:
488 gcc_unreachable ();
489 }
490 }
491 }
492
493 /* Restore the template parameter context for a member template, a
494 friend template defined in a class definition, or a non-template
495 member of template class. */
496
497 void
498 maybe_begin_member_template_processing (tree decl)
499 {
500 tree parms;
501 int levels = 0;
502 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
503
504 if (nsdmi)
505 {
506 tree ctx = DECL_CONTEXT (decl);
507 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
508 /* Disregard full specializations (c++/60999). */
509 && uses_template_parms (ctx)
510 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
511 }
512
513 if (inline_needs_template_parms (decl, nsdmi))
514 {
515 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
516 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
517
518 if (DECL_TEMPLATE_SPECIALIZATION (decl))
519 {
520 --levels;
521 parms = TREE_CHAIN (parms);
522 }
523
524 push_inline_template_parms_recursive (parms, levels);
525 }
526
527 /* Remember how many levels of template parameters we pushed so that
528 we can pop them later. */
529 inline_parm_levels.safe_push (levels);
530 }
531
532 /* Undo the effects of maybe_begin_member_template_processing. */
533
534 void
535 maybe_end_member_template_processing (void)
536 {
537 int i;
538 int last;
539
540 if (inline_parm_levels.length () == 0)
541 return;
542
543 last = inline_parm_levels.pop ();
544 for (i = 0; i < last; ++i)
545 {
546 --processing_template_decl;
547 current_template_parms = TREE_CHAIN (current_template_parms);
548 poplevel (0, 0, 0);
549 }
550 }
551
552 /* Return a new template argument vector which contains all of ARGS,
553 but has as its innermost set of arguments the EXTRA_ARGS. */
554
555 static tree
556 add_to_template_args (tree args, tree extra_args)
557 {
558 tree new_args;
559 int extra_depth;
560 int i;
561 int j;
562
563 if (args == NULL_TREE || extra_args == error_mark_node)
564 return extra_args;
565
566 extra_depth = TMPL_ARGS_DEPTH (extra_args);
567 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
568
569 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
570 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
571
572 for (j = 1; j <= extra_depth; ++j, ++i)
573 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
574
575 return new_args;
576 }
577
578 /* Like add_to_template_args, but only the outermost ARGS are added to
579 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
580 (EXTRA_ARGS) levels are added. This function is used to combine
581 the template arguments from a partial instantiation with the
582 template arguments used to attain the full instantiation from the
583 partial instantiation. */
584
585 static tree
586 add_outermost_template_args (tree args, tree extra_args)
587 {
588 tree new_args;
589
590 /* If there are more levels of EXTRA_ARGS than there are ARGS,
591 something very fishy is going on. */
592 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
593
594 /* If *all* the new arguments will be the EXTRA_ARGS, just return
595 them. */
596 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
597 return extra_args;
598
599 /* For the moment, we make ARGS look like it contains fewer levels. */
600 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
601
602 new_args = add_to_template_args (args, extra_args);
603
604 /* Now, we restore ARGS to its full dimensions. */
605 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
606
607 return new_args;
608 }
609
610 /* Return the N levels of innermost template arguments from the ARGS. */
611
612 tree
613 get_innermost_template_args (tree args, int n)
614 {
615 tree new_args;
616 int extra_levels;
617 int i;
618
619 gcc_assert (n >= 0);
620
621 /* If N is 1, just return the innermost set of template arguments. */
622 if (n == 1)
623 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
624
625 /* If we're not removing anything, just return the arguments we were
626 given. */
627 extra_levels = TMPL_ARGS_DEPTH (args) - n;
628 gcc_assert (extra_levels >= 0);
629 if (extra_levels == 0)
630 return args;
631
632 /* Make a new set of arguments, not containing the outer arguments. */
633 new_args = make_tree_vec (n);
634 for (i = 1; i <= n; ++i)
635 SET_TMPL_ARGS_LEVEL (new_args, i,
636 TMPL_ARGS_LEVEL (args, i + extra_levels));
637
638 return new_args;
639 }
640
641 /* The inverse of get_innermost_template_args: Return all but the innermost
642 EXTRA_LEVELS levels of template arguments from the ARGS. */
643
644 static tree
645 strip_innermost_template_args (tree args, int extra_levels)
646 {
647 tree new_args;
648 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
649 int i;
650
651 gcc_assert (n >= 0);
652
653 /* If N is 1, just return the outermost set of template arguments. */
654 if (n == 1)
655 return TMPL_ARGS_LEVEL (args, 1);
656
657 /* If we're not removing anything, just return the arguments we were
658 given. */
659 gcc_assert (extra_levels >= 0);
660 if (extra_levels == 0)
661 return args;
662
663 /* Make a new set of arguments, not containing the inner arguments. */
664 new_args = make_tree_vec (n);
665 for (i = 1; i <= n; ++i)
666 SET_TMPL_ARGS_LEVEL (new_args, i,
667 TMPL_ARGS_LEVEL (args, i));
668
669 return new_args;
670 }
671
672 /* We've got a template header coming up; push to a new level for storing
673 the parms. */
674
675 void
676 begin_template_parm_list (void)
677 {
678 /* We use a non-tag-transparent scope here, which causes pushtag to
679 put tags in this scope, rather than in the enclosing class or
680 namespace scope. This is the right thing, since we want
681 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
682 global template class, push_template_decl handles putting the
683 TEMPLATE_DECL into top-level scope. For a nested template class,
684 e.g.:
685
686 template <class T> struct S1 {
687 template <class T> struct S2 {};
688 };
689
690 pushtag contains special code to insert the TEMPLATE_DECL for S2
691 at the right scope. */
692 begin_scope (sk_template_parms, NULL);
693 ++processing_template_decl;
694 ++processing_template_parmlist;
695 note_template_header (0);
696
697 /* Add a dummy parameter level while we process the parameter list. */
698 current_template_parms
699 = tree_cons (size_int (processing_template_decl),
700 make_tree_vec (0),
701 current_template_parms);
702 }
703
704 /* This routine is called when a specialization is declared. If it is
705 invalid to declare a specialization here, an error is reported and
706 false is returned, otherwise this routine will return true. */
707
708 static bool
709 check_specialization_scope (void)
710 {
711 tree scope = current_scope ();
712
713 /* [temp.expl.spec]
714
715 An explicit specialization shall be declared in the namespace of
716 which the template is a member, or, for member templates, in the
717 namespace of which the enclosing class or enclosing class
718 template is a member. An explicit specialization of a member
719 function, member class or static data member of a class template
720 shall be declared in the namespace of which the class template
721 is a member. */
722 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
723 {
724 error ("explicit specialization in non-namespace scope %qD", scope);
725 return false;
726 }
727
728 /* [temp.expl.spec]
729
730 In an explicit specialization declaration for a member of a class
731 template or a member template that appears in namespace scope,
732 the member template and some of its enclosing class templates may
733 remain unspecialized, except that the declaration shall not
734 explicitly specialize a class member template if its enclosing
735 class templates are not explicitly specialized as well. */
736 if (current_template_parms)
737 {
738 error ("enclosing class templates are not explicitly specialized");
739 return false;
740 }
741
742 return true;
743 }
744
745 /* We've just seen template <>. */
746
747 bool
748 begin_specialization (void)
749 {
750 begin_scope (sk_template_spec, NULL);
751 note_template_header (1);
752 return check_specialization_scope ();
753 }
754
755 /* Called at then end of processing a declaration preceded by
756 template<>. */
757
758 void
759 end_specialization (void)
760 {
761 finish_scope ();
762 reset_specialization ();
763 }
764
765 /* Any template <>'s that we have seen thus far are not referring to a
766 function specialization. */
767
768 void
769 reset_specialization (void)
770 {
771 processing_specialization = 0;
772 template_header_count = 0;
773 }
774
775 /* We've just seen a template header. If SPECIALIZATION is nonzero,
776 it was of the form template <>. */
777
778 static void
779 note_template_header (int specialization)
780 {
781 processing_specialization = specialization;
782 template_header_count++;
783 }
784
785 /* We're beginning an explicit instantiation. */
786
787 void
788 begin_explicit_instantiation (void)
789 {
790 gcc_assert (!processing_explicit_instantiation);
791 processing_explicit_instantiation = true;
792 }
793
794
795 void
796 end_explicit_instantiation (void)
797 {
798 gcc_assert (processing_explicit_instantiation);
799 processing_explicit_instantiation = false;
800 }
801
802 /* An explicit specialization or partial specialization of TMPL is being
803 declared. Check that the namespace in which the specialization is
804 occurring is permissible. Returns false iff it is invalid to
805 specialize TMPL in the current namespace. */
806
807 static bool
808 check_specialization_namespace (tree tmpl)
809 {
810 tree tpl_ns = decl_namespace_context (tmpl);
811
812 /* [tmpl.expl.spec]
813
814 An explicit specialization shall be declared in a namespace enclosing the
815 specialized template. An explicit specialization whose declarator-id is
816 not qualified shall be declared in the nearest enclosing namespace of the
817 template, or, if the namespace is inline (7.3.1), any namespace from its
818 enclosing namespace set. */
819 if (current_scope() != DECL_CONTEXT (tmpl)
820 && !at_namespace_scope_p ())
821 {
822 error ("specialization of %qD must appear at namespace scope", tmpl);
823 return false;
824 }
825
826 if (is_nested_namespace (current_namespace, tpl_ns, cxx_dialect < cxx11))
827 /* Same or enclosing namespace. */
828 return true;
829 else
830 {
831 auto_diagnostic_group d;
832 if (permerror (input_location,
833 "specialization of %qD in different namespace", tmpl))
834 inform (DECL_SOURCE_LOCATION (tmpl),
835 " from definition of %q#D", tmpl);
836 return false;
837 }
838 }
839
840 /* SPEC is an explicit instantiation. Check that it is valid to
841 perform this explicit instantiation in the current namespace. */
842
843 static void
844 check_explicit_instantiation_namespace (tree spec)
845 {
846 tree ns;
847
848 /* DR 275: An explicit instantiation shall appear in an enclosing
849 namespace of its template. */
850 ns = decl_namespace_context (spec);
851 if (!is_nested_namespace (current_namespace, ns))
852 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
853 "(which does not enclose namespace %qD)",
854 spec, current_namespace, ns);
855 }
856
857 // Returns the type of a template specialization only if that
858 // specialization needs to be defined. Otherwise (e.g., if the type has
859 // already been defined), the function returns NULL_TREE.
860 static tree
861 maybe_new_partial_specialization (tree type)
862 {
863 // An implicit instantiation of an incomplete type implies
864 // the definition of a new class template.
865 //
866 // template<typename T>
867 // struct S;
868 //
869 // template<typename T>
870 // struct S<T*>;
871 //
872 // Here, S<T*> is an implicit instantiation of S whose type
873 // is incomplete.
874 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
875 return type;
876
877 // It can also be the case that TYPE is a completed specialization.
878 // Continuing the previous example, suppose we also declare:
879 //
880 // template<typename T>
881 // requires Integral<T>
882 // struct S<T*>;
883 //
884 // Here, S<T*> refers to the specialization S<T*> defined
885 // above. However, we need to differentiate definitions because
886 // we intend to define a new partial specialization. In this case,
887 // we rely on the fact that the constraints are different for
888 // this declaration than that above.
889 //
890 // Note that we also get here for injected class names and
891 // late-parsed template definitions. We must ensure that we
892 // do not create new type declarations for those cases.
893 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
894 {
895 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
896 tree args = CLASSTYPE_TI_ARGS (type);
897
898 // If there are no template parameters, this cannot be a new
899 // partial template specializtion?
900 if (!current_template_parms)
901 return NULL_TREE;
902
903 // The injected-class-name is not a new partial specialization.
904 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
905 return NULL_TREE;
906
907 // If the constraints are not the same as those of the primary
908 // then, we can probably create a new specialization.
909 tree type_constr = current_template_constraints ();
910
911 if (type == TREE_TYPE (tmpl))
912 {
913 tree main_constr = get_constraints (tmpl);
914 if (equivalent_constraints (type_constr, main_constr))
915 return NULL_TREE;
916 }
917
918 // Also, if there's a pre-existing specialization with matching
919 // constraints, then this also isn't new.
920 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
921 while (specs)
922 {
923 tree spec_tmpl = TREE_VALUE (specs);
924 tree spec_args = TREE_PURPOSE (specs);
925 tree spec_constr = get_constraints (spec_tmpl);
926 if (comp_template_args (args, spec_args)
927 && equivalent_constraints (type_constr, spec_constr))
928 return NULL_TREE;
929 specs = TREE_CHAIN (specs);
930 }
931
932 // Create a new type node (and corresponding type decl)
933 // for the newly declared specialization.
934 tree t = make_class_type (TREE_CODE (type));
935 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
936 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
937
938 /* We only need a separate type node for storing the definition of this
939 partial specialization; uses of S<T*> are unconstrained, so all are
940 equivalent. So keep TYPE_CANONICAL the same. */
941 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
942
943 // Build the corresponding type decl.
944 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
945 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
946 DECL_SOURCE_LOCATION (d) = input_location;
947
948 return t;
949 }
950
951 return NULL_TREE;
952 }
953
954 /* The TYPE is being declared. If it is a template type, that means it
955 is a partial specialization. Do appropriate error-checking. */
956
957 tree
958 maybe_process_partial_specialization (tree type)
959 {
960 tree context;
961
962 if (type == error_mark_node)
963 return error_mark_node;
964
965 /* A lambda that appears in specialization context is not itself a
966 specialization. */
967 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
968 return type;
969
970 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
971 {
972 error ("name of class shadows template template parameter %qD",
973 TYPE_NAME (type));
974 return error_mark_node;
975 }
976
977 context = TYPE_CONTEXT (type);
978
979 if (TYPE_ALIAS_P (type))
980 {
981 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
982
983 if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
984 error ("specialization of alias template %qD",
985 TI_TEMPLATE (tinfo));
986 else
987 error ("explicit specialization of non-template %qT", type);
988 return error_mark_node;
989 }
990 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
991 {
992 /* This is for ordinary explicit specialization and partial
993 specialization of a template class such as:
994
995 template <> class C<int>;
996
997 or:
998
999 template <class T> class C<T*>;
1000
1001 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
1002
1003 if (tree t = maybe_new_partial_specialization (type))
1004 {
1005 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
1006 && !at_namespace_scope_p ())
1007 return error_mark_node;
1008 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
1009 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
1010 if (processing_template_decl)
1011 {
1012 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
1013 if (decl == error_mark_node)
1014 return error_mark_node;
1015 return TREE_TYPE (decl);
1016 }
1017 }
1018 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
1019 error ("specialization of %qT after instantiation", type);
1020 else if (errorcount && !processing_specialization
1021 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
1022 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
1023 /* Trying to define a specialization either without a template<> header
1024 or in an inappropriate place. We've already given an error, so just
1025 bail now so we don't actually define the specialization. */
1026 return error_mark_node;
1027 }
1028 else if (CLASS_TYPE_P (type)
1029 && !CLASSTYPE_USE_TEMPLATE (type)
1030 && CLASSTYPE_TEMPLATE_INFO (type)
1031 && context && CLASS_TYPE_P (context)
1032 && CLASSTYPE_TEMPLATE_INFO (context))
1033 {
1034 /* This is for an explicit specialization of member class
1035 template according to [temp.expl.spec/18]:
1036
1037 template <> template <class U> class C<int>::D;
1038
1039 The context `C<int>' must be an implicit instantiation.
1040 Otherwise this is just a member class template declared
1041 earlier like:
1042
1043 template <> class C<int> { template <class U> class D; };
1044 template <> template <class U> class C<int>::D;
1045
1046 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1047 while in the second case, `C<int>::D' is a primary template
1048 and `C<T>::D' may not exist. */
1049
1050 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1051 && !COMPLETE_TYPE_P (type))
1052 {
1053 tree t;
1054 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1055
1056 if (current_namespace
1057 != decl_namespace_context (tmpl))
1058 {
1059 if (permerror (input_location,
1060 "specialization of %qD in different namespace",
1061 type))
1062 inform (DECL_SOURCE_LOCATION (tmpl),
1063 "from definition of %q#D", tmpl);
1064 }
1065
1066 /* Check for invalid specialization after instantiation:
1067
1068 template <> template <> class C<int>::D<int>;
1069 template <> template <class U> class C<int>::D; */
1070
1071 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1072 t; t = TREE_CHAIN (t))
1073 {
1074 tree inst = TREE_VALUE (t);
1075 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1076 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1077 {
1078 /* We already have a full specialization of this partial
1079 instantiation, or a full specialization has been
1080 looked up but not instantiated. Reassign it to the
1081 new member specialization template. */
1082 spec_entry elt;
1083 spec_entry *entry;
1084
1085 elt.tmpl = most_general_template (tmpl);
1086 elt.args = CLASSTYPE_TI_ARGS (inst);
1087 elt.spec = inst;
1088
1089 type_specializations->remove_elt (&elt);
1090
1091 elt.tmpl = tmpl;
1092 CLASSTYPE_TI_ARGS (inst)
1093 = elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1094
1095 spec_entry **slot
1096 = type_specializations->find_slot (&elt, INSERT);
1097 entry = ggc_alloc<spec_entry> ();
1098 *entry = elt;
1099 *slot = entry;
1100 }
1101 else
1102 /* But if we've had an implicit instantiation, that's a
1103 problem ([temp.expl.spec]/6). */
1104 error ("specialization %qT after instantiation %qT",
1105 type, inst);
1106 }
1107
1108 /* Mark TYPE as a specialization. And as a result, we only
1109 have one level of template argument for the innermost
1110 class template. */
1111 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1112 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1113 CLASSTYPE_TI_ARGS (type)
1114 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1115 }
1116 }
1117 else if (processing_specialization)
1118 {
1119 /* Someday C++0x may allow for enum template specialization. */
1120 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1121 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1122 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1123 "of %qD not allowed by ISO C++", type);
1124 else
1125 {
1126 error ("explicit specialization of non-template %qT", type);
1127 return error_mark_node;
1128 }
1129 }
1130
1131 return type;
1132 }
1133
1134 /* Returns nonzero if we can optimize the retrieval of specializations
1135 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1136 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1137
1138 static inline bool
1139 optimize_specialization_lookup_p (tree tmpl)
1140 {
1141 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1142 && DECL_CLASS_SCOPE_P (tmpl)
1143 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1144 parameter. */
1145 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1146 /* The optimized lookup depends on the fact that the
1147 template arguments for the member function template apply
1148 purely to the containing class, which is not true if the
1149 containing class is an explicit or partial
1150 specialization. */
1151 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1152 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1153 && !DECL_CONV_FN_P (tmpl)
1154 /* It is possible to have a template that is not a member
1155 template and is not a member of a template class:
1156
1157 template <typename T>
1158 struct S { friend A::f(); };
1159
1160 Here, the friend function is a template, but the context does
1161 not have template information. The optimized lookup relies
1162 on having ARGS be the template arguments for both the class
1163 and the function template. */
1164 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1165 }
1166
1167 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1168 gone through coerce_template_parms by now. */
1169
1170 static void
1171 verify_unstripped_args_1 (tree inner)
1172 {
1173 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1174 {
1175 tree arg = TREE_VEC_ELT (inner, i);
1176 if (TREE_CODE (arg) == TEMPLATE_DECL)
1177 /* OK */;
1178 else if (TYPE_P (arg))
1179 gcc_assert (strip_typedefs (arg, NULL) == arg);
1180 else if (ARGUMENT_PACK_P (arg))
1181 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg));
1182 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1183 /* Allow typedefs on the type of a non-type argument, since a
1184 parameter can have them. */;
1185 else
1186 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1187 }
1188 }
1189
1190 static void
1191 verify_unstripped_args (tree args)
1192 {
1193 ++processing_template_decl;
1194 if (!any_dependent_template_arguments_p (args))
1195 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args));
1196 --processing_template_decl;
1197 }
1198
1199 /* Retrieve the specialization (in the sense of [temp.spec] - a
1200 specialization is either an instantiation or an explicit
1201 specialization) of TMPL for the given template ARGS. If there is
1202 no such specialization, return NULL_TREE. The ARGS are a vector of
1203 arguments, or a vector of vectors of arguments, in the case of
1204 templates with more than one level of parameters.
1205
1206 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1207 then we search for a partial specialization matching ARGS. This
1208 parameter is ignored if TMPL is not a class template.
1209
1210 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1211 result is a NONTYPE_ARGUMENT_PACK. */
1212
1213 static tree
1214 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1215 {
1216 if (tmpl == NULL_TREE)
1217 return NULL_TREE;
1218
1219 if (args == error_mark_node)
1220 return NULL_TREE;
1221
1222 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1223 || TREE_CODE (tmpl) == FIELD_DECL);
1224
1225 /* There should be as many levels of arguments as there are
1226 levels of parameters. */
1227 gcc_assert (TMPL_ARGS_DEPTH (args)
1228 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1229 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1230 : template_class_depth (DECL_CONTEXT (tmpl))));
1231
1232 if (flag_checking)
1233 verify_unstripped_args (args);
1234
1235 /* Lambda functions in templates aren't instantiated normally, but through
1236 tsubst_lambda_expr. */
1237 if (lambda_fn_in_template_p (tmpl))
1238 return NULL_TREE;
1239
1240 if (optimize_specialization_lookup_p (tmpl))
1241 {
1242 /* The template arguments actually apply to the containing
1243 class. Find the class specialization with those
1244 arguments. */
1245 tree class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1246 tree class_specialization
1247 = retrieve_specialization (class_template, args, 0);
1248 if (!class_specialization)
1249 return NULL_TREE;
1250
1251 /* Find the instance of TMPL. */
1252 tree fns = get_class_binding (class_specialization, DECL_NAME (tmpl));
1253 for (ovl_iterator iter (fns); iter; ++iter)
1254 {
1255 tree fn = *iter;
1256 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1257 /* using-declarations can add base methods to the method vec,
1258 and we don't want those here. */
1259 && DECL_CONTEXT (fn) == class_specialization)
1260 return fn;
1261 }
1262 return NULL_TREE;
1263 }
1264 else
1265 {
1266 spec_entry *found;
1267 spec_entry elt;
1268 hash_table<spec_hasher> *specializations;
1269
1270 elt.tmpl = tmpl;
1271 elt.args = args;
1272 elt.spec = NULL_TREE;
1273
1274 if (DECL_CLASS_TEMPLATE_P (tmpl))
1275 specializations = type_specializations;
1276 else
1277 specializations = decl_specializations;
1278
1279 if (hash == 0)
1280 hash = spec_hasher::hash (&elt);
1281 found = specializations->find_with_hash (&elt, hash);
1282 if (found)
1283 return found->spec;
1284 }
1285
1286 return NULL_TREE;
1287 }
1288
1289 /* Like retrieve_specialization, but for local declarations. */
1290
1291 tree
1292 retrieve_local_specialization (tree tmpl)
1293 {
1294 if (local_specializations == NULL)
1295 return NULL_TREE;
1296
1297 tree *slot = local_specializations->get (tmpl);
1298 return slot ? *slot : NULL_TREE;
1299 }
1300
1301 /* Returns nonzero iff DECL is a specialization of TMPL. */
1302
1303 int
1304 is_specialization_of (tree decl, tree tmpl)
1305 {
1306 tree t;
1307
1308 if (TREE_CODE (decl) == FUNCTION_DECL)
1309 {
1310 for (t = decl;
1311 t != NULL_TREE;
1312 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1313 if (t == tmpl)
1314 return 1;
1315 }
1316 else
1317 {
1318 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1319
1320 for (t = TREE_TYPE (decl);
1321 t != NULL_TREE;
1322 t = CLASSTYPE_USE_TEMPLATE (t)
1323 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1324 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1325 return 1;
1326 }
1327
1328 return 0;
1329 }
1330
1331 /* Returns nonzero iff DECL is a specialization of friend declaration
1332 FRIEND_DECL according to [temp.friend]. */
1333
1334 bool
1335 is_specialization_of_friend (tree decl, tree friend_decl)
1336 {
1337 bool need_template = true;
1338 int template_depth;
1339
1340 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1341 || TREE_CODE (decl) == TYPE_DECL);
1342
1343 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1344 of a template class, we want to check if DECL is a specialization
1345 if this. */
1346 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1347 && DECL_TEMPLATE_INFO (friend_decl)
1348 && !DECL_USE_TEMPLATE (friend_decl))
1349 {
1350 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1351 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1352 need_template = false;
1353 }
1354 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1355 && !PRIMARY_TEMPLATE_P (friend_decl))
1356 need_template = false;
1357
1358 /* There is nothing to do if this is not a template friend. */
1359 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1360 return false;
1361
1362 if (is_specialization_of (decl, friend_decl))
1363 return true;
1364
1365 /* [temp.friend/6]
1366 A member of a class template may be declared to be a friend of a
1367 non-template class. In this case, the corresponding member of
1368 every specialization of the class template is a friend of the
1369 class granting friendship.
1370
1371 For example, given a template friend declaration
1372
1373 template <class T> friend void A<T>::f();
1374
1375 the member function below is considered a friend
1376
1377 template <> struct A<int> {
1378 void f();
1379 };
1380
1381 For this type of template friend, TEMPLATE_DEPTH below will be
1382 nonzero. To determine if DECL is a friend of FRIEND, we first
1383 check if the enclosing class is a specialization of another. */
1384
1385 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1386 if (template_depth
1387 && DECL_CLASS_SCOPE_P (decl)
1388 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1389 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1390 {
1391 /* Next, we check the members themselves. In order to handle
1392 a few tricky cases, such as when FRIEND_DECL's are
1393
1394 template <class T> friend void A<T>::g(T t);
1395 template <class T> template <T t> friend void A<T>::h();
1396
1397 and DECL's are
1398
1399 void A<int>::g(int);
1400 template <int> void A<int>::h();
1401
1402 we need to figure out ARGS, the template arguments from
1403 the context of DECL. This is required for template substitution
1404 of `T' in the function parameter of `g' and template parameter
1405 of `h' in the above examples. Here ARGS corresponds to `int'. */
1406
1407 tree context = DECL_CONTEXT (decl);
1408 tree args = NULL_TREE;
1409 int current_depth = 0;
1410
1411 while (current_depth < template_depth)
1412 {
1413 if (CLASSTYPE_TEMPLATE_INFO (context))
1414 {
1415 if (current_depth == 0)
1416 args = TYPE_TI_ARGS (context);
1417 else
1418 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1419 current_depth++;
1420 }
1421 context = TYPE_CONTEXT (context);
1422 }
1423
1424 if (TREE_CODE (decl) == FUNCTION_DECL)
1425 {
1426 bool is_template;
1427 tree friend_type;
1428 tree decl_type;
1429 tree friend_args_type;
1430 tree decl_args_type;
1431
1432 /* Make sure that both DECL and FRIEND_DECL are templates or
1433 non-templates. */
1434 is_template = DECL_TEMPLATE_INFO (decl)
1435 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1436 if (need_template ^ is_template)
1437 return false;
1438 else if (is_template)
1439 {
1440 /* If both are templates, check template parameter list. */
1441 tree friend_parms
1442 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1443 args, tf_none);
1444 if (!comp_template_parms
1445 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1446 friend_parms))
1447 return false;
1448
1449 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1450 }
1451 else
1452 decl_type = TREE_TYPE (decl);
1453
1454 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1455 tf_none, NULL_TREE);
1456 if (friend_type == error_mark_node)
1457 return false;
1458
1459 /* Check if return types match. */
1460 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1461 return false;
1462
1463 /* Check if function parameter types match, ignoring the
1464 `this' parameter. */
1465 friend_args_type = TYPE_ARG_TYPES (friend_type);
1466 decl_args_type = TYPE_ARG_TYPES (decl_type);
1467 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1468 friend_args_type = TREE_CHAIN (friend_args_type);
1469 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1470 decl_args_type = TREE_CHAIN (decl_args_type);
1471
1472 return compparms (decl_args_type, friend_args_type);
1473 }
1474 else
1475 {
1476 /* DECL is a TYPE_DECL */
1477 bool is_template;
1478 tree decl_type = TREE_TYPE (decl);
1479
1480 /* Make sure that both DECL and FRIEND_DECL are templates or
1481 non-templates. */
1482 is_template
1483 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1484 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1485
1486 if (need_template ^ is_template)
1487 return false;
1488 else if (is_template)
1489 {
1490 tree friend_parms;
1491 /* If both are templates, check the name of the two
1492 TEMPLATE_DECL's first because is_friend didn't. */
1493 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1494 != DECL_NAME (friend_decl))
1495 return false;
1496
1497 /* Now check template parameter list. */
1498 friend_parms
1499 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1500 args, tf_none);
1501 return comp_template_parms
1502 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1503 friend_parms);
1504 }
1505 else
1506 return (DECL_NAME (decl)
1507 == DECL_NAME (friend_decl));
1508 }
1509 }
1510 return false;
1511 }
1512
1513 /* Register the specialization SPEC as a specialization of TMPL with
1514 the indicated ARGS. IS_FRIEND indicates whether the specialization
1515 is actually just a friend declaration. ATTRLIST is the list of
1516 attributes that the specialization is declared with or NULL when
1517 it isn't. Returns SPEC, or an equivalent prior declaration, if
1518 available.
1519
1520 We also store instantiations of field packs in the hash table, even
1521 though they are not themselves templates, to make lookup easier. */
1522
1523 static tree
1524 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1525 hashval_t hash)
1526 {
1527 tree fn;
1528 spec_entry **slot = NULL;
1529 spec_entry elt;
1530
1531 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1532 || (TREE_CODE (tmpl) == FIELD_DECL
1533 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1534
1535 if (TREE_CODE (spec) == FUNCTION_DECL
1536 && uses_template_parms (DECL_TI_ARGS (spec)))
1537 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1538 register it; we want the corresponding TEMPLATE_DECL instead.
1539 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1540 the more obvious `uses_template_parms (spec)' to avoid problems
1541 with default function arguments. In particular, given
1542 something like this:
1543
1544 template <class T> void f(T t1, T t = T())
1545
1546 the default argument expression is not substituted for in an
1547 instantiation unless and until it is actually needed. */
1548 return spec;
1549
1550 if (optimize_specialization_lookup_p (tmpl))
1551 /* We don't put these specializations in the hash table, but we might
1552 want to give an error about a mismatch. */
1553 fn = retrieve_specialization (tmpl, args, 0);
1554 else
1555 {
1556 elt.tmpl = tmpl;
1557 elt.args = args;
1558 elt.spec = spec;
1559
1560 if (hash == 0)
1561 hash = spec_hasher::hash (&elt);
1562
1563 slot =
1564 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1565 if (*slot)
1566 fn = ((spec_entry *) *slot)->spec;
1567 else
1568 fn = NULL_TREE;
1569 }
1570
1571 /* We can sometimes try to re-register a specialization that we've
1572 already got. In particular, regenerate_decl_from_template calls
1573 duplicate_decls which will update the specialization list. But,
1574 we'll still get called again here anyhow. It's more convenient
1575 to simply allow this than to try to prevent it. */
1576 if (fn == spec)
1577 return spec;
1578 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1579 {
1580 if (DECL_TEMPLATE_INSTANTIATION (fn))
1581 {
1582 if (DECL_ODR_USED (fn)
1583 || DECL_EXPLICIT_INSTANTIATION (fn))
1584 {
1585 error ("specialization of %qD after instantiation",
1586 fn);
1587 return error_mark_node;
1588 }
1589 else
1590 {
1591 tree clone;
1592 /* This situation should occur only if the first
1593 specialization is an implicit instantiation, the
1594 second is an explicit specialization, and the
1595 implicit instantiation has not yet been used. That
1596 situation can occur if we have implicitly
1597 instantiated a member function and then specialized
1598 it later.
1599
1600 We can also wind up here if a friend declaration that
1601 looked like an instantiation turns out to be a
1602 specialization:
1603
1604 template <class T> void foo(T);
1605 class S { friend void foo<>(int) };
1606 template <> void foo(int);
1607
1608 We transform the existing DECL in place so that any
1609 pointers to it become pointers to the updated
1610 declaration.
1611
1612 If there was a definition for the template, but not
1613 for the specialization, we want this to look as if
1614 there were no definition, and vice versa. */
1615 DECL_INITIAL (fn) = NULL_TREE;
1616 duplicate_decls (spec, fn, is_friend);
1617 /* The call to duplicate_decls will have applied
1618 [temp.expl.spec]:
1619
1620 An explicit specialization of a function template
1621 is inline only if it is explicitly declared to be,
1622 and independently of whether its function template
1623 is.
1624
1625 to the primary function; now copy the inline bits to
1626 the various clones. */
1627 FOR_EACH_CLONE (clone, fn)
1628 {
1629 DECL_DECLARED_INLINE_P (clone)
1630 = DECL_DECLARED_INLINE_P (fn);
1631 DECL_SOURCE_LOCATION (clone)
1632 = DECL_SOURCE_LOCATION (fn);
1633 DECL_DELETED_FN (clone)
1634 = DECL_DELETED_FN (fn);
1635 }
1636 check_specialization_namespace (tmpl);
1637
1638 return fn;
1639 }
1640 }
1641 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1642 {
1643 tree dd = duplicate_decls (spec, fn, is_friend);
1644 if (dd == error_mark_node)
1645 /* We've already complained in duplicate_decls. */
1646 return error_mark_node;
1647
1648 if (dd == NULL_TREE && DECL_INITIAL (spec))
1649 /* Dup decl failed, but this is a new definition. Set the
1650 line number so any errors match this new
1651 definition. */
1652 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1653
1654 return fn;
1655 }
1656 }
1657 else if (fn)
1658 return duplicate_decls (spec, fn, is_friend);
1659
1660 /* A specialization must be declared in the same namespace as the
1661 template it is specializing. */
1662 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1663 && !check_specialization_namespace (tmpl))
1664 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1665
1666 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1667 {
1668 spec_entry *entry = ggc_alloc<spec_entry> ();
1669 gcc_assert (tmpl && args && spec);
1670 *entry = elt;
1671 *slot = entry;
1672 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1673 && PRIMARY_TEMPLATE_P (tmpl)
1674 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1675 || variable_template_p (tmpl))
1676 /* If TMPL is a forward declaration of a template function, keep a list
1677 of all specializations in case we need to reassign them to a friend
1678 template later in tsubst_friend_function.
1679
1680 Also keep a list of all variable template instantiations so that
1681 process_partial_specialization can check whether a later partial
1682 specialization would have used it. */
1683 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1684 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1685 }
1686
1687 return spec;
1688 }
1689
1690 /* Returns true iff two spec_entry nodes are equivalent. */
1691
1692 int comparing_specializations;
1693
1694 bool
1695 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1696 {
1697 int equal;
1698
1699 ++comparing_specializations;
1700 equal = (e1->tmpl == e2->tmpl
1701 && comp_template_args (e1->args, e2->args));
1702 if (equal && flag_concepts
1703 /* tmpl could be a FIELD_DECL for a capture pack. */
1704 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1705 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1706 && uses_template_parms (e1->args))
1707 {
1708 /* Partial specializations of a variable template can be distinguished by
1709 constraints. */
1710 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1711 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1712 equal = equivalent_constraints (c1, c2);
1713 }
1714 --comparing_specializations;
1715
1716 return equal;
1717 }
1718
1719 /* Returns a hash for a template TMPL and template arguments ARGS. */
1720
1721 static hashval_t
1722 hash_tmpl_and_args (tree tmpl, tree args)
1723 {
1724 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1725 return iterative_hash_template_arg (args, val);
1726 }
1727
1728 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1729 ignoring SPEC. */
1730
1731 hashval_t
1732 spec_hasher::hash (spec_entry *e)
1733 {
1734 return hash_tmpl_and_args (e->tmpl, e->args);
1735 }
1736
1737 /* Recursively calculate a hash value for a template argument ARG, for use
1738 in the hash tables of template specializations. */
1739
1740 hashval_t
1741 iterative_hash_template_arg (tree arg, hashval_t val)
1742 {
1743 unsigned HOST_WIDE_INT i;
1744 enum tree_code code;
1745 char tclass;
1746
1747 if (arg == NULL_TREE)
1748 return iterative_hash_object (arg, val);
1749
1750 if (!TYPE_P (arg))
1751 STRIP_NOPS (arg);
1752
1753 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1754 gcc_unreachable ();
1755
1756 code = TREE_CODE (arg);
1757 tclass = TREE_CODE_CLASS (code);
1758
1759 val = iterative_hash_object (code, val);
1760
1761 switch (code)
1762 {
1763 case ERROR_MARK:
1764 return val;
1765
1766 case IDENTIFIER_NODE:
1767 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1768
1769 case TREE_VEC:
1770 {
1771 int i, len = TREE_VEC_LENGTH (arg);
1772 for (i = 0; i < len; ++i)
1773 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1774 return val;
1775 }
1776
1777 case TYPE_PACK_EXPANSION:
1778 case EXPR_PACK_EXPANSION:
1779 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1780 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1781
1782 case TYPE_ARGUMENT_PACK:
1783 case NONTYPE_ARGUMENT_PACK:
1784 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1785
1786 case TREE_LIST:
1787 for (; arg; arg = TREE_CHAIN (arg))
1788 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1789 return val;
1790
1791 case OVERLOAD:
1792 for (lkp_iterator iter (arg); iter; ++iter)
1793 val = iterative_hash_template_arg (*iter, val);
1794 return val;
1795
1796 case CONSTRUCTOR:
1797 {
1798 tree field, value;
1799 iterative_hash_template_arg (TREE_TYPE (arg), val);
1800 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1801 {
1802 val = iterative_hash_template_arg (field, val);
1803 val = iterative_hash_template_arg (value, val);
1804 }
1805 return val;
1806 }
1807
1808 case PARM_DECL:
1809 if (!DECL_ARTIFICIAL (arg))
1810 {
1811 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1812 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1813 }
1814 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1815
1816 case TARGET_EXPR:
1817 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1818
1819 case PTRMEM_CST:
1820 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1821 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1822
1823 case TEMPLATE_PARM_INDEX:
1824 val = iterative_hash_template_arg
1825 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1826 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1827 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1828
1829 case TRAIT_EXPR:
1830 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1831 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1832 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1833
1834 case BASELINK:
1835 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1836 val);
1837 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1838 val);
1839
1840 case MODOP_EXPR:
1841 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1842 code = TREE_CODE (TREE_OPERAND (arg, 1));
1843 val = iterative_hash_object (code, val);
1844 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1845
1846 case LAMBDA_EXPR:
1847 /* [temp.over.link] Two lambda-expressions are never considered
1848 equivalent.
1849
1850 So just hash the closure type. */
1851 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1852
1853 case CAST_EXPR:
1854 case IMPLICIT_CONV_EXPR:
1855 case STATIC_CAST_EXPR:
1856 case REINTERPRET_CAST_EXPR:
1857 case CONST_CAST_EXPR:
1858 case DYNAMIC_CAST_EXPR:
1859 case NEW_EXPR:
1860 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1861 /* Now hash operands as usual. */
1862 break;
1863
1864 case CALL_EXPR:
1865 {
1866 tree fn = CALL_EXPR_FN (arg);
1867 if (tree name = dependent_name (fn))
1868 {
1869 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
1870 val = iterative_hash_template_arg (TREE_OPERAND (fn, 1), val);
1871 fn = name;
1872 }
1873 val = iterative_hash_template_arg (fn, val);
1874 call_expr_arg_iterator ai;
1875 for (tree x = first_call_expr_arg (arg, &ai); x;
1876 x = next_call_expr_arg (&ai))
1877 val = iterative_hash_template_arg (x, val);
1878 return val;
1879 }
1880
1881 default:
1882 break;
1883 }
1884
1885 switch (tclass)
1886 {
1887 case tcc_type:
1888 if (alias_template_specialization_p (arg))
1889 {
1890 // We want an alias specialization that survived strip_typedefs
1891 // to hash differently from its TYPE_CANONICAL, to avoid hash
1892 // collisions that compare as different in template_args_equal.
1893 // These could be dependent specializations that strip_typedefs
1894 // left alone, or untouched specializations because
1895 // coerce_template_parms returns the unconverted template
1896 // arguments if it sees incomplete argument packs.
1897 tree ti = TYPE_ALIAS_TEMPLATE_INFO (arg);
1898 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1899 }
1900 if (TYPE_CANONICAL (arg))
1901 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1902 val);
1903 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1904 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1905 /* Otherwise just compare the types during lookup. */
1906 return val;
1907
1908 case tcc_declaration:
1909 case tcc_constant:
1910 return iterative_hash_expr (arg, val);
1911
1912 default:
1913 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1914 {
1915 unsigned n = cp_tree_operand_length (arg);
1916 for (i = 0; i < n; ++i)
1917 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1918 return val;
1919 }
1920 }
1921 gcc_unreachable ();
1922 return 0;
1923 }
1924
1925 /* Unregister the specialization SPEC as a specialization of TMPL.
1926 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1927 if the SPEC was listed as a specialization of TMPL.
1928
1929 Note that SPEC has been ggc_freed, so we can't look inside it. */
1930
1931 bool
1932 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1933 {
1934 spec_entry *entry;
1935 spec_entry elt;
1936
1937 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1938 elt.args = TI_ARGS (tinfo);
1939 elt.spec = NULL_TREE;
1940
1941 entry = decl_specializations->find (&elt);
1942 if (entry != NULL)
1943 {
1944 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1945 gcc_assert (new_spec != NULL_TREE);
1946 entry->spec = new_spec;
1947 return 1;
1948 }
1949
1950 return 0;
1951 }
1952
1953 /* Like register_specialization, but for local declarations. We are
1954 registering SPEC, an instantiation of TMPL. */
1955
1956 void
1957 register_local_specialization (tree spec, tree tmpl)
1958 {
1959 gcc_assert (tmpl != spec);
1960 local_specializations->put (tmpl, spec);
1961 }
1962
1963 /* TYPE is a class type. Returns true if TYPE is an explicitly
1964 specialized class. */
1965
1966 bool
1967 explicit_class_specialization_p (tree type)
1968 {
1969 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1970 return false;
1971 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1972 }
1973
1974 /* Print the list of functions at FNS, going through all the overloads
1975 for each element of the list. Alternatively, FNS cannot be a
1976 TREE_LIST, in which case it will be printed together with all the
1977 overloads.
1978
1979 MORE and *STR should respectively be FALSE and NULL when the function
1980 is called from the outside. They are used internally on recursive
1981 calls. print_candidates manages the two parameters and leaves NULL
1982 in *STR when it ends. */
1983
1984 static void
1985 print_candidates_1 (tree fns, char **str, bool more = false)
1986 {
1987 if (TREE_CODE (fns) == TREE_LIST)
1988 for (; fns; fns = TREE_CHAIN (fns))
1989 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
1990 else
1991 for (lkp_iterator iter (fns); iter;)
1992 {
1993 tree cand = *iter;
1994 ++iter;
1995
1996 const char *pfx = *str;
1997 if (!pfx)
1998 {
1999 if (more || iter)
2000 pfx = _("candidates are:");
2001 else
2002 pfx = _("candidate is:");
2003 *str = get_spaces (pfx);
2004 }
2005 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
2006 }
2007 }
2008
2009 /* Print the list of candidate FNS in an error message. FNS can also
2010 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
2011
2012 void
2013 print_candidates (tree fns)
2014 {
2015 char *str = NULL;
2016 print_candidates_1 (fns, &str);
2017 free (str);
2018 }
2019
2020 /* Get a (possibly) constrained template declaration for the
2021 purpose of ordering candidates. */
2022 static tree
2023 get_template_for_ordering (tree list)
2024 {
2025 gcc_assert (TREE_CODE (list) == TREE_LIST);
2026 tree f = TREE_VALUE (list);
2027 if (tree ti = DECL_TEMPLATE_INFO (f))
2028 return TI_TEMPLATE (ti);
2029 return f;
2030 }
2031
2032 /* Among candidates having the same signature, return the
2033 most constrained or NULL_TREE if there is no best candidate.
2034 If the signatures of candidates vary (e.g., template
2035 specialization vs. member function), then there can be no
2036 most constrained.
2037
2038 Note that we don't compare constraints on the functions
2039 themselves, but rather those of their templates. */
2040 static tree
2041 most_constrained_function (tree candidates)
2042 {
2043 // Try to find the best candidate in a first pass.
2044 tree champ = candidates;
2045 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
2046 {
2047 int winner = more_constrained (get_template_for_ordering (champ),
2048 get_template_for_ordering (c));
2049 if (winner == -1)
2050 champ = c; // The candidate is more constrained
2051 else if (winner == 0)
2052 return NULL_TREE; // Neither is more constrained
2053 }
2054
2055 // Verify that the champ is better than previous candidates.
2056 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2057 if (!more_constrained (get_template_for_ordering (champ),
2058 get_template_for_ordering (c)))
2059 return NULL_TREE;
2060 }
2061
2062 return champ;
2063 }
2064
2065
2066 /* Returns the template (one of the functions given by TEMPLATE_ID)
2067 which can be specialized to match the indicated DECL with the
2068 explicit template args given in TEMPLATE_ID. The DECL may be
2069 NULL_TREE if none is available. In that case, the functions in
2070 TEMPLATE_ID are non-members.
2071
2072 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2073 specialization of a member template.
2074
2075 The TEMPLATE_COUNT is the number of references to qualifying
2076 template classes that appeared in the name of the function. See
2077 check_explicit_specialization for a more accurate description.
2078
2079 TSK indicates what kind of template declaration (if any) is being
2080 declared. TSK_TEMPLATE indicates that the declaration given by
2081 DECL, though a FUNCTION_DECL, has template parameters, and is
2082 therefore a template function.
2083
2084 The template args (those explicitly specified and those deduced)
2085 are output in a newly created vector *TARGS_OUT.
2086
2087 If it is impossible to determine the result, an error message is
2088 issued. The error_mark_node is returned to indicate failure. */
2089
2090 static tree
2091 determine_specialization (tree template_id,
2092 tree decl,
2093 tree* targs_out,
2094 int need_member_template,
2095 int template_count,
2096 tmpl_spec_kind tsk)
2097 {
2098 tree fns;
2099 tree targs;
2100 tree explicit_targs;
2101 tree candidates = NULL_TREE;
2102
2103 /* A TREE_LIST of templates of which DECL may be a specialization.
2104 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2105 corresponding TREE_PURPOSE is the set of template arguments that,
2106 when used to instantiate the template, would produce a function
2107 with the signature of DECL. */
2108 tree templates = NULL_TREE;
2109 int header_count;
2110 cp_binding_level *b;
2111
2112 *targs_out = NULL_TREE;
2113
2114 if (template_id == error_mark_node || decl == error_mark_node)
2115 return error_mark_node;
2116
2117 /* We shouldn't be specializing a member template of an
2118 unspecialized class template; we already gave an error in
2119 check_specialization_scope, now avoid crashing. */
2120 if (!VAR_P (decl)
2121 && template_count && DECL_CLASS_SCOPE_P (decl)
2122 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2123 {
2124 gcc_assert (errorcount);
2125 return error_mark_node;
2126 }
2127
2128 fns = TREE_OPERAND (template_id, 0);
2129 explicit_targs = TREE_OPERAND (template_id, 1);
2130
2131 if (fns == error_mark_node)
2132 return error_mark_node;
2133
2134 /* Check for baselinks. */
2135 if (BASELINK_P (fns))
2136 fns = BASELINK_FUNCTIONS (fns);
2137
2138 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2139 {
2140 error ("%qD is not a function template", fns);
2141 return error_mark_node;
2142 }
2143 else if (VAR_P (decl) && !variable_template_p (fns))
2144 {
2145 error ("%qD is not a variable template", fns);
2146 return error_mark_node;
2147 }
2148
2149 /* Count the number of template headers specified for this
2150 specialization. */
2151 header_count = 0;
2152 for (b = current_binding_level;
2153 b->kind == sk_template_parms;
2154 b = b->level_chain)
2155 ++header_count;
2156
2157 tree orig_fns = fns;
2158
2159 if (variable_template_p (fns))
2160 {
2161 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2162 targs = coerce_template_parms (parms, explicit_targs, fns,
2163 tf_warning_or_error,
2164 /*req_all*/true, /*use_defarg*/true);
2165 if (targs != error_mark_node)
2166 templates = tree_cons (targs, fns, templates);
2167 }
2168 else for (lkp_iterator iter (fns); iter; ++iter)
2169 {
2170 tree fn = *iter;
2171
2172 if (TREE_CODE (fn) == TEMPLATE_DECL)
2173 {
2174 tree decl_arg_types;
2175 tree fn_arg_types;
2176 tree insttype;
2177
2178 /* In case of explicit specialization, we need to check if
2179 the number of template headers appearing in the specialization
2180 is correct. This is usually done in check_explicit_specialization,
2181 but the check done there cannot be exhaustive when specializing
2182 member functions. Consider the following code:
2183
2184 template <> void A<int>::f(int);
2185 template <> template <> void A<int>::f(int);
2186
2187 Assuming that A<int> is not itself an explicit specialization
2188 already, the first line specializes "f" which is a non-template
2189 member function, whilst the second line specializes "f" which
2190 is a template member function. So both lines are syntactically
2191 correct, and check_explicit_specialization does not reject
2192 them.
2193
2194 Here, we can do better, as we are matching the specialization
2195 against the declarations. We count the number of template
2196 headers, and we check if they match TEMPLATE_COUNT + 1
2197 (TEMPLATE_COUNT is the number of qualifying template classes,
2198 plus there must be another header for the member template
2199 itself).
2200
2201 Notice that if header_count is zero, this is not a
2202 specialization but rather a template instantiation, so there
2203 is no check we can perform here. */
2204 if (header_count && header_count != template_count + 1)
2205 continue;
2206
2207 /* Check that the number of template arguments at the
2208 innermost level for DECL is the same as for FN. */
2209 if (current_binding_level->kind == sk_template_parms
2210 && !current_binding_level->explicit_spec_p
2211 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2212 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2213 (current_template_parms))))
2214 continue;
2215
2216 /* DECL might be a specialization of FN. */
2217 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2218 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2219
2220 /* For a non-static member function, we need to make sure
2221 that the const qualification is the same. Since
2222 get_bindings does not try to merge the "this" parameter,
2223 we must do the comparison explicitly. */
2224 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2225 {
2226 if (!same_type_p (TREE_VALUE (fn_arg_types),
2227 TREE_VALUE (decl_arg_types)))
2228 continue;
2229
2230 /* And the ref-qualification. */
2231 if (type_memfn_rqual (TREE_TYPE (decl))
2232 != type_memfn_rqual (TREE_TYPE (fn)))
2233 continue;
2234 }
2235
2236 /* Skip the "this" parameter and, for constructors of
2237 classes with virtual bases, the VTT parameter. A
2238 full specialization of a constructor will have a VTT
2239 parameter, but a template never will. */
2240 decl_arg_types
2241 = skip_artificial_parms_for (decl, decl_arg_types);
2242 fn_arg_types
2243 = skip_artificial_parms_for (fn, fn_arg_types);
2244
2245 /* Function templates cannot be specializations; there are
2246 no partial specializations of functions. Therefore, if
2247 the type of DECL does not match FN, there is no
2248 match.
2249
2250 Note that it should never be the case that we have both
2251 candidates added here, and for regular member functions
2252 below. */
2253 if (tsk == tsk_template)
2254 {
2255 if (compparms (fn_arg_types, decl_arg_types))
2256 candidates = tree_cons (NULL_TREE, fn, candidates);
2257 continue;
2258 }
2259
2260 /* See whether this function might be a specialization of this
2261 template. Suppress access control because we might be trying
2262 to make this specialization a friend, and we have already done
2263 access control for the declaration of the specialization. */
2264 push_deferring_access_checks (dk_no_check);
2265 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2266 pop_deferring_access_checks ();
2267
2268 if (!targs)
2269 /* We cannot deduce template arguments that when used to
2270 specialize TMPL will produce DECL. */
2271 continue;
2272
2273 if (uses_template_parms (targs))
2274 /* We deduced something involving 'auto', which isn't a valid
2275 template argument. */
2276 continue;
2277
2278 /* Remove, from the set of candidates, all those functions
2279 whose constraints are not satisfied. */
2280 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2281 continue;
2282
2283 // Then, try to form the new function type.
2284 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2285 if (insttype == error_mark_node)
2286 continue;
2287 fn_arg_types
2288 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2289 if (!compparms (fn_arg_types, decl_arg_types))
2290 continue;
2291
2292 /* Save this template, and the arguments deduced. */
2293 templates = tree_cons (targs, fn, templates);
2294 }
2295 else if (need_member_template)
2296 /* FN is an ordinary member function, and we need a
2297 specialization of a member template. */
2298 ;
2299 else if (TREE_CODE (fn) != FUNCTION_DECL)
2300 /* We can get IDENTIFIER_NODEs here in certain erroneous
2301 cases. */
2302 ;
2303 else if (!DECL_FUNCTION_MEMBER_P (fn))
2304 /* This is just an ordinary non-member function. Nothing can
2305 be a specialization of that. */
2306 ;
2307 else if (DECL_ARTIFICIAL (fn))
2308 /* Cannot specialize functions that are created implicitly. */
2309 ;
2310 else
2311 {
2312 tree decl_arg_types;
2313
2314 /* This is an ordinary member function. However, since
2315 we're here, we can assume its enclosing class is a
2316 template class. For example,
2317
2318 template <typename T> struct S { void f(); };
2319 template <> void S<int>::f() {}
2320
2321 Here, S<int>::f is a non-template, but S<int> is a
2322 template class. If FN has the same type as DECL, we
2323 might be in business. */
2324
2325 if (!DECL_TEMPLATE_INFO (fn))
2326 /* Its enclosing class is an explicit specialization
2327 of a template class. This is not a candidate. */
2328 continue;
2329
2330 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2331 TREE_TYPE (TREE_TYPE (fn))))
2332 /* The return types differ. */
2333 continue;
2334
2335 /* Adjust the type of DECL in case FN is a static member. */
2336 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2337 if (DECL_STATIC_FUNCTION_P (fn)
2338 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2339 decl_arg_types = TREE_CHAIN (decl_arg_types);
2340
2341 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2342 decl_arg_types))
2343 continue;
2344
2345 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2346 && (type_memfn_rqual (TREE_TYPE (decl))
2347 != type_memfn_rqual (TREE_TYPE (fn))))
2348 continue;
2349
2350 // If the deduced arguments do not satisfy the constraints,
2351 // this is not a candidate.
2352 if (flag_concepts && !constraints_satisfied_p (fn))
2353 continue;
2354
2355 // Add the candidate.
2356 candidates = tree_cons (NULL_TREE, fn, candidates);
2357 }
2358 }
2359
2360 if (templates && TREE_CHAIN (templates))
2361 {
2362 /* We have:
2363
2364 [temp.expl.spec]
2365
2366 It is possible for a specialization with a given function
2367 signature to be instantiated from more than one function
2368 template. In such cases, explicit specification of the
2369 template arguments must be used to uniquely identify the
2370 function template specialization being specialized.
2371
2372 Note that here, there's no suggestion that we're supposed to
2373 determine which of the candidate templates is most
2374 specialized. However, we, also have:
2375
2376 [temp.func.order]
2377
2378 Partial ordering of overloaded function template
2379 declarations is used in the following contexts to select
2380 the function template to which a function template
2381 specialization refers:
2382
2383 -- when an explicit specialization refers to a function
2384 template.
2385
2386 So, we do use the partial ordering rules, at least for now.
2387 This extension can only serve to make invalid programs valid,
2388 so it's safe. And, there is strong anecdotal evidence that
2389 the committee intended the partial ordering rules to apply;
2390 the EDG front end has that behavior, and John Spicer claims
2391 that the committee simply forgot to delete the wording in
2392 [temp.expl.spec]. */
2393 tree tmpl = most_specialized_instantiation (templates);
2394 if (tmpl != error_mark_node)
2395 {
2396 templates = tmpl;
2397 TREE_CHAIN (templates) = NULL_TREE;
2398 }
2399 }
2400
2401 // Concepts allows multiple declarations of member functions
2402 // with the same signature. Like above, we need to rely on
2403 // on the partial ordering of those candidates to determine which
2404 // is the best.
2405 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2406 {
2407 if (tree cand = most_constrained_function (candidates))
2408 {
2409 candidates = cand;
2410 TREE_CHAIN (cand) = NULL_TREE;
2411 }
2412 }
2413
2414 if (templates == NULL_TREE && candidates == NULL_TREE)
2415 {
2416 error ("template-id %qD for %q+D does not match any template "
2417 "declaration", template_id, decl);
2418 if (header_count && header_count != template_count + 1)
2419 inform (input_location, "saw %d %<template<>%>, need %d for "
2420 "specializing a member function template",
2421 header_count, template_count + 1);
2422 else
2423 print_candidates (orig_fns);
2424 return error_mark_node;
2425 }
2426 else if ((templates && TREE_CHAIN (templates))
2427 || (candidates && TREE_CHAIN (candidates))
2428 || (templates && candidates))
2429 {
2430 error ("ambiguous template specialization %qD for %q+D",
2431 template_id, decl);
2432 candidates = chainon (candidates, templates);
2433 print_candidates (candidates);
2434 return error_mark_node;
2435 }
2436
2437 /* We have one, and exactly one, match. */
2438 if (candidates)
2439 {
2440 tree fn = TREE_VALUE (candidates);
2441 *targs_out = copy_node (DECL_TI_ARGS (fn));
2442
2443 /* Propagate the candidate's constraints to the declaration. */
2444 set_constraints (decl, get_constraints (fn));
2445
2446 /* DECL is a re-declaration or partial instantiation of a template
2447 function. */
2448 if (TREE_CODE (fn) == TEMPLATE_DECL)
2449 return fn;
2450 /* It was a specialization of an ordinary member function in a
2451 template class. */
2452 return DECL_TI_TEMPLATE (fn);
2453 }
2454
2455 /* It was a specialization of a template. */
2456 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2457 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2458 {
2459 *targs_out = copy_node (targs);
2460 SET_TMPL_ARGS_LEVEL (*targs_out,
2461 TMPL_ARGS_DEPTH (*targs_out),
2462 TREE_PURPOSE (templates));
2463 }
2464 else
2465 *targs_out = TREE_PURPOSE (templates);
2466 return TREE_VALUE (templates);
2467 }
2468
2469 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2470 but with the default argument values filled in from those in the
2471 TMPL_TYPES. */
2472
2473 static tree
2474 copy_default_args_to_explicit_spec_1 (tree spec_types,
2475 tree tmpl_types)
2476 {
2477 tree new_spec_types;
2478
2479 if (!spec_types)
2480 return NULL_TREE;
2481
2482 if (spec_types == void_list_node)
2483 return void_list_node;
2484
2485 /* Substitute into the rest of the list. */
2486 new_spec_types =
2487 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2488 TREE_CHAIN (tmpl_types));
2489
2490 /* Add the default argument for this parameter. */
2491 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2492 TREE_VALUE (spec_types),
2493 new_spec_types);
2494 }
2495
2496 /* DECL is an explicit specialization. Replicate default arguments
2497 from the template it specializes. (That way, code like:
2498
2499 template <class T> void f(T = 3);
2500 template <> void f(double);
2501 void g () { f (); }
2502
2503 works, as required.) An alternative approach would be to look up
2504 the correct default arguments at the call-site, but this approach
2505 is consistent with how implicit instantiations are handled. */
2506
2507 static void
2508 copy_default_args_to_explicit_spec (tree decl)
2509 {
2510 tree tmpl;
2511 tree spec_types;
2512 tree tmpl_types;
2513 tree new_spec_types;
2514 tree old_type;
2515 tree new_type;
2516 tree t;
2517 tree object_type = NULL_TREE;
2518 tree in_charge = NULL_TREE;
2519 tree vtt = NULL_TREE;
2520
2521 /* See if there's anything we need to do. */
2522 tmpl = DECL_TI_TEMPLATE (decl);
2523 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2524 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2525 if (TREE_PURPOSE (t))
2526 break;
2527 if (!t)
2528 return;
2529
2530 old_type = TREE_TYPE (decl);
2531 spec_types = TYPE_ARG_TYPES (old_type);
2532
2533 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2534 {
2535 /* Remove the this pointer, but remember the object's type for
2536 CV quals. */
2537 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2538 spec_types = TREE_CHAIN (spec_types);
2539 tmpl_types = TREE_CHAIN (tmpl_types);
2540
2541 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2542 {
2543 /* DECL may contain more parameters than TMPL due to the extra
2544 in-charge parameter in constructors and destructors. */
2545 in_charge = spec_types;
2546 spec_types = TREE_CHAIN (spec_types);
2547 }
2548 if (DECL_HAS_VTT_PARM_P (decl))
2549 {
2550 vtt = spec_types;
2551 spec_types = TREE_CHAIN (spec_types);
2552 }
2553 }
2554
2555 /* Compute the merged default arguments. */
2556 new_spec_types =
2557 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2558
2559 /* Compute the new FUNCTION_TYPE. */
2560 if (object_type)
2561 {
2562 if (vtt)
2563 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2564 TREE_VALUE (vtt),
2565 new_spec_types);
2566
2567 if (in_charge)
2568 /* Put the in-charge parameter back. */
2569 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2570 TREE_VALUE (in_charge),
2571 new_spec_types);
2572
2573 new_type = build_method_type_directly (object_type,
2574 TREE_TYPE (old_type),
2575 new_spec_types);
2576 }
2577 else
2578 new_type = build_function_type (TREE_TYPE (old_type),
2579 new_spec_types);
2580 new_type = cp_build_type_attribute_variant (new_type,
2581 TYPE_ATTRIBUTES (old_type));
2582 new_type = cxx_copy_lang_qualifiers (new_type, old_type);
2583
2584 TREE_TYPE (decl) = new_type;
2585 }
2586
2587 /* Return the number of template headers we expect to see for a definition
2588 or specialization of CTYPE or one of its non-template members. */
2589
2590 int
2591 num_template_headers_for_class (tree ctype)
2592 {
2593 int num_templates = 0;
2594
2595 while (ctype && CLASS_TYPE_P (ctype))
2596 {
2597 /* You're supposed to have one `template <...>' for every
2598 template class, but you don't need one for a full
2599 specialization. For example:
2600
2601 template <class T> struct S{};
2602 template <> struct S<int> { void f(); };
2603 void S<int>::f () {}
2604
2605 is correct; there shouldn't be a `template <>' for the
2606 definition of `S<int>::f'. */
2607 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2608 /* If CTYPE does not have template information of any
2609 kind, then it is not a template, nor is it nested
2610 within a template. */
2611 break;
2612 if (explicit_class_specialization_p (ctype))
2613 break;
2614 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2615 ++num_templates;
2616
2617 ctype = TYPE_CONTEXT (ctype);
2618 }
2619
2620 return num_templates;
2621 }
2622
2623 /* Do a simple sanity check on the template headers that precede the
2624 variable declaration DECL. */
2625
2626 void
2627 check_template_variable (tree decl)
2628 {
2629 tree ctx = CP_DECL_CONTEXT (decl);
2630 int wanted = num_template_headers_for_class (ctx);
2631 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2632 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2633 {
2634 if (cxx_dialect < cxx14)
2635 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2636 "variable templates only available with "
2637 "%<-std=c++14%> or %<-std=gnu++14%>");
2638
2639 // Namespace-scope variable templates should have a template header.
2640 ++wanted;
2641 }
2642 if (template_header_count > wanted)
2643 {
2644 auto_diagnostic_group d;
2645 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2646 "too many template headers for %qD "
2647 "(should be %d)",
2648 decl, wanted);
2649 if (warned && CLASS_TYPE_P (ctx)
2650 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2651 inform (DECL_SOURCE_LOCATION (decl),
2652 "members of an explicitly specialized class are defined "
2653 "without a template header");
2654 }
2655 }
2656
2657 /* An explicit specialization whose declarator-id or class-head-name is not
2658 qualified shall be declared in the nearest enclosing namespace of the
2659 template, or, if the namespace is inline (7.3.1), any namespace from its
2660 enclosing namespace set.
2661
2662 If the name declared in the explicit instantiation is an unqualified name,
2663 the explicit instantiation shall appear in the namespace where its template
2664 is declared or, if that namespace is inline (7.3.1), any namespace from its
2665 enclosing namespace set. */
2666
2667 void
2668 check_unqualified_spec_or_inst (tree t, location_t loc)
2669 {
2670 tree tmpl = most_general_template (t);
2671 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2672 && !is_nested_namespace (current_namespace,
2673 CP_DECL_CONTEXT (tmpl), true))
2674 {
2675 if (processing_specialization)
2676 permerror (loc, "explicit specialization of %qD outside its "
2677 "namespace must use a nested-name-specifier", tmpl);
2678 else if (processing_explicit_instantiation
2679 && cxx_dialect >= cxx11)
2680 /* This was allowed in C++98, so only pedwarn. */
2681 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2682 "outside its namespace must use a nested-name-"
2683 "specifier", tmpl);
2684 }
2685 }
2686
2687 /* Warn for a template specialization SPEC that is missing some of a set
2688 of function or type attributes that the template TEMPL is declared with.
2689 ATTRLIST is a list of additional attributes that SPEC should be taken
2690 to ultimately be declared with. */
2691
2692 static void
2693 warn_spec_missing_attributes (tree tmpl, tree spec, tree attrlist)
2694 {
2695 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
2696 tmpl = DECL_TEMPLATE_RESULT (tmpl);
2697
2698 /* Avoid warning if the difference between the primary and
2699 the specialization is not in one of the attributes below. */
2700 const char* const blacklist[] = {
2701 "alloc_align", "alloc_size", "assume_aligned", "format",
2702 "format_arg", "malloc", "nonnull", NULL
2703 };
2704
2705 /* Put together a list of the black listed attributes that the primary
2706 template is declared with that the specialization is not, in case
2707 it's not apparent from the most recent declaration of the primary. */
2708 pretty_printer str;
2709 unsigned nattrs = decls_mismatched_attributes (tmpl, spec, attrlist,
2710 blacklist, &str);
2711
2712 if (!nattrs)
2713 return;
2714
2715 auto_diagnostic_group d;
2716 if (warning_at (DECL_SOURCE_LOCATION (spec), OPT_Wmissing_attributes,
2717 "explicit specialization %q#D may be missing attributes",
2718 spec))
2719 inform (DECL_SOURCE_LOCATION (tmpl),
2720 nattrs > 1
2721 ? G_("missing primary template attributes %s")
2722 : G_("missing primary template attribute %s"),
2723 pp_formatted_text (&str));
2724 }
2725
2726 /* Check to see if the function just declared, as indicated in
2727 DECLARATOR, and in DECL, is a specialization of a function
2728 template. We may also discover that the declaration is an explicit
2729 instantiation at this point.
2730
2731 Returns DECL, or an equivalent declaration that should be used
2732 instead if all goes well. Issues an error message if something is
2733 amiss. Returns error_mark_node if the error is not easily
2734 recoverable.
2735
2736 FLAGS is a bitmask consisting of the following flags:
2737
2738 2: The function has a definition.
2739 4: The function is a friend.
2740
2741 The TEMPLATE_COUNT is the number of references to qualifying
2742 template classes that appeared in the name of the function. For
2743 example, in
2744
2745 template <class T> struct S { void f(); };
2746 void S<int>::f();
2747
2748 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2749 classes are not counted in the TEMPLATE_COUNT, so that in
2750
2751 template <class T> struct S {};
2752 template <> struct S<int> { void f(); }
2753 template <> void S<int>::f();
2754
2755 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2756 invalid; there should be no template <>.)
2757
2758 If the function is a specialization, it is marked as such via
2759 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2760 is set up correctly, and it is added to the list of specializations
2761 for that template. */
2762
2763 tree
2764 check_explicit_specialization (tree declarator,
2765 tree decl,
2766 int template_count,
2767 int flags,
2768 tree attrlist)
2769 {
2770 int have_def = flags & 2;
2771 int is_friend = flags & 4;
2772 bool is_concept = flags & 8;
2773 int specialization = 0;
2774 int explicit_instantiation = 0;
2775 int member_specialization = 0;
2776 tree ctype = DECL_CLASS_CONTEXT (decl);
2777 tree dname = DECL_NAME (decl);
2778 tmpl_spec_kind tsk;
2779
2780 if (is_friend)
2781 {
2782 if (!processing_specialization)
2783 tsk = tsk_none;
2784 else
2785 tsk = tsk_excessive_parms;
2786 }
2787 else
2788 tsk = current_tmpl_spec_kind (template_count);
2789
2790 switch (tsk)
2791 {
2792 case tsk_none:
2793 if (processing_specialization && !VAR_P (decl))
2794 {
2795 specialization = 1;
2796 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2797 }
2798 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2799 {
2800 if (is_friend)
2801 /* This could be something like:
2802
2803 template <class T> void f(T);
2804 class S { friend void f<>(int); } */
2805 specialization = 1;
2806 else
2807 {
2808 /* This case handles bogus declarations like template <>
2809 template <class T> void f<int>(); */
2810
2811 error_at (cp_expr_loc_or_input_loc (declarator),
2812 "template-id %qE in declaration of primary template",
2813 declarator);
2814 return decl;
2815 }
2816 }
2817 break;
2818
2819 case tsk_invalid_member_spec:
2820 /* The error has already been reported in
2821 check_specialization_scope. */
2822 return error_mark_node;
2823
2824 case tsk_invalid_expl_inst:
2825 error ("template parameter list used in explicit instantiation");
2826
2827 /* Fall through. */
2828
2829 case tsk_expl_inst:
2830 if (have_def)
2831 error ("definition provided for explicit instantiation");
2832
2833 explicit_instantiation = 1;
2834 break;
2835
2836 case tsk_excessive_parms:
2837 case tsk_insufficient_parms:
2838 if (tsk == tsk_excessive_parms)
2839 error ("too many template parameter lists in declaration of %qD",
2840 decl);
2841 else if (template_header_count)
2842 error("too few template parameter lists in declaration of %qD", decl);
2843 else
2844 error("explicit specialization of %qD must be introduced by "
2845 "%<template <>%>", decl);
2846
2847 /* Fall through. */
2848 case tsk_expl_spec:
2849 if (is_concept)
2850 error ("explicit specialization declared %<concept%>");
2851
2852 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2853 /* In cases like template<> constexpr bool v = true;
2854 We'll give an error in check_template_variable. */
2855 break;
2856
2857 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2858 if (ctype)
2859 member_specialization = 1;
2860 else
2861 specialization = 1;
2862 break;
2863
2864 case tsk_template:
2865 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2866 {
2867 /* This case handles bogus declarations like template <>
2868 template <class T> void f<int>(); */
2869
2870 if (!uses_template_parms (TREE_OPERAND (declarator, 1)))
2871 error_at (cp_expr_loc_or_input_loc (declarator),
2872 "template-id %qE in declaration of primary template",
2873 declarator);
2874 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2875 {
2876 /* Partial specialization of variable template. */
2877 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2878 specialization = 1;
2879 goto ok;
2880 }
2881 else if (cxx_dialect < cxx14)
2882 error_at (cp_expr_loc_or_input_loc (declarator),
2883 "non-type partial specialization %qE "
2884 "is not allowed", declarator);
2885 else
2886 error_at (cp_expr_loc_or_input_loc (declarator),
2887 "non-class, non-variable partial specialization %qE "
2888 "is not allowed", declarator);
2889 return decl;
2890 ok:;
2891 }
2892
2893 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2894 /* This is a specialization of a member template, without
2895 specialization the containing class. Something like:
2896
2897 template <class T> struct S {
2898 template <class U> void f (U);
2899 };
2900 template <> template <class U> void S<int>::f(U) {}
2901
2902 That's a specialization -- but of the entire template. */
2903 specialization = 1;
2904 break;
2905
2906 default:
2907 gcc_unreachable ();
2908 }
2909
2910 if ((specialization || member_specialization)
2911 /* This doesn't apply to variable templates. */
2912 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl)))
2913 {
2914 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2915 for (; t; t = TREE_CHAIN (t))
2916 if (TREE_PURPOSE (t))
2917 {
2918 permerror (input_location,
2919 "default argument specified in explicit specialization");
2920 break;
2921 }
2922 }
2923
2924 if (specialization || member_specialization || explicit_instantiation)
2925 {
2926 tree tmpl = NULL_TREE;
2927 tree targs = NULL_TREE;
2928 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2929
2930 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2931 if (!was_template_id)
2932 {
2933 tree fns;
2934
2935 gcc_assert (identifier_p (declarator));
2936 if (ctype)
2937 fns = dname;
2938 else
2939 {
2940 /* If there is no class context, the explicit instantiation
2941 must be at namespace scope. */
2942 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2943
2944 /* Find the namespace binding, using the declaration
2945 context. */
2946 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2947 false, true);
2948 if (fns == error_mark_node)
2949 /* If lookup fails, look for a friend declaration so we can
2950 give a better diagnostic. */
2951 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2952 /*type*/false, /*complain*/true,
2953 /*hidden*/true);
2954
2955 if (fns == error_mark_node || !is_overloaded_fn (fns))
2956 {
2957 error ("%qD is not a template function", dname);
2958 fns = error_mark_node;
2959 }
2960 }
2961
2962 declarator = lookup_template_function (fns, NULL_TREE);
2963 }
2964
2965 if (declarator == error_mark_node)
2966 return error_mark_node;
2967
2968 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2969 {
2970 if (!explicit_instantiation)
2971 /* A specialization in class scope. This is invalid,
2972 but the error will already have been flagged by
2973 check_specialization_scope. */
2974 return error_mark_node;
2975 else
2976 {
2977 /* It's not valid to write an explicit instantiation in
2978 class scope, e.g.:
2979
2980 class C { template void f(); }
2981
2982 This case is caught by the parser. However, on
2983 something like:
2984
2985 template class C { void f(); };
2986
2987 (which is invalid) we can get here. The error will be
2988 issued later. */
2989 ;
2990 }
2991
2992 return decl;
2993 }
2994 else if (ctype != NULL_TREE
2995 && (identifier_p (TREE_OPERAND (declarator, 0))))
2996 {
2997 // We'll match variable templates in start_decl.
2998 if (VAR_P (decl))
2999 return decl;
3000
3001 /* Find the list of functions in ctype that have the same
3002 name as the declared function. */
3003 tree name = TREE_OPERAND (declarator, 0);
3004
3005 if (constructor_name_p (name, ctype))
3006 {
3007 if (DECL_CONSTRUCTOR_P (decl)
3008 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
3009 : !CLASSTYPE_DESTRUCTOR (ctype))
3010 {
3011 /* From [temp.expl.spec]:
3012
3013 If such an explicit specialization for the member
3014 of a class template names an implicitly-declared
3015 special member function (clause _special_), the
3016 program is ill-formed.
3017
3018 Similar language is found in [temp.explicit]. */
3019 error ("specialization of implicitly-declared special member function");
3020 return error_mark_node;
3021 }
3022
3023 name = DECL_NAME (decl);
3024 }
3025
3026 /* For a type-conversion operator, We might be looking for
3027 `operator int' which will be a specialization of
3028 `operator T'. Grab all the conversion operators, and
3029 then select from them. */
3030 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
3031 ? conv_op_identifier : name);
3032
3033 if (fns == NULL_TREE)
3034 {
3035 error ("no member function %qD declared in %qT", name, ctype);
3036 return error_mark_node;
3037 }
3038 else
3039 TREE_OPERAND (declarator, 0) = fns;
3040 }
3041
3042 /* Figure out what exactly is being specialized at this point.
3043 Note that for an explicit instantiation, even one for a
3044 member function, we cannot tell a priori whether the
3045 instantiation is for a member template, or just a member
3046 function of a template class. Even if a member template is
3047 being instantiated, the member template arguments may be
3048 elided if they can be deduced from the rest of the
3049 declaration. */
3050 tmpl = determine_specialization (declarator, decl,
3051 &targs,
3052 member_specialization,
3053 template_count,
3054 tsk);
3055
3056 if (!tmpl || tmpl == error_mark_node)
3057 /* We couldn't figure out what this declaration was
3058 specializing. */
3059 return error_mark_node;
3060 else
3061 {
3062 if (TREE_CODE (decl) == FUNCTION_DECL
3063 && DECL_HIDDEN_FRIEND_P (tmpl))
3064 {
3065 auto_diagnostic_group d;
3066 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3067 "friend declaration %qD is not visible to "
3068 "explicit specialization", tmpl))
3069 inform (DECL_SOURCE_LOCATION (tmpl),
3070 "friend declaration here");
3071 }
3072 else if (!ctype && !is_friend
3073 && CP_DECL_CONTEXT (decl) == current_namespace)
3074 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
3075
3076 tree gen_tmpl = most_general_template (tmpl);
3077
3078 if (explicit_instantiation)
3079 {
3080 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3081 is done by do_decl_instantiation later. */
3082
3083 int arg_depth = TMPL_ARGS_DEPTH (targs);
3084 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3085
3086 if (arg_depth > parm_depth)
3087 {
3088 /* If TMPL is not the most general template (for
3089 example, if TMPL is a friend template that is
3090 injected into namespace scope), then there will
3091 be too many levels of TARGS. Remove some of them
3092 here. */
3093 int i;
3094 tree new_targs;
3095
3096 new_targs = make_tree_vec (parm_depth);
3097 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3098 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3099 = TREE_VEC_ELT (targs, i);
3100 targs = new_targs;
3101 }
3102
3103 return instantiate_template (tmpl, targs, tf_error);
3104 }
3105
3106 /* If we thought that the DECL was a member function, but it
3107 turns out to be specializing a static member function,
3108 make DECL a static member function as well. */
3109 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3110 && DECL_STATIC_FUNCTION_P (tmpl)
3111 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3112 revert_static_member_fn (decl);
3113
3114 /* If this is a specialization of a member template of a
3115 template class, we want to return the TEMPLATE_DECL, not
3116 the specialization of it. */
3117 if (tsk == tsk_template && !was_template_id)
3118 {
3119 tree result = DECL_TEMPLATE_RESULT (tmpl);
3120 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3121 DECL_INITIAL (result) = NULL_TREE;
3122 if (have_def)
3123 {
3124 tree parm;
3125 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3126 DECL_SOURCE_LOCATION (result)
3127 = DECL_SOURCE_LOCATION (decl);
3128 /* We want to use the argument list specified in the
3129 definition, not in the original declaration. */
3130 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3131 for (parm = DECL_ARGUMENTS (result); parm;
3132 parm = DECL_CHAIN (parm))
3133 DECL_CONTEXT (parm) = result;
3134 }
3135 return register_specialization (tmpl, gen_tmpl, targs,
3136 is_friend, 0);
3137 }
3138
3139 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3140 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3141
3142 if (was_template_id)
3143 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3144
3145 /* Inherit default function arguments from the template
3146 DECL is specializing. */
3147 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3148 copy_default_args_to_explicit_spec (decl);
3149
3150 /* This specialization has the same protection as the
3151 template it specializes. */
3152 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3153 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3154
3155 /* 7.1.1-1 [dcl.stc]
3156
3157 A storage-class-specifier shall not be specified in an
3158 explicit specialization...
3159
3160 The parser rejects these, so unless action is taken here,
3161 explicit function specializations will always appear with
3162 global linkage.
3163
3164 The action recommended by the C++ CWG in response to C++
3165 defect report 605 is to make the storage class and linkage
3166 of the explicit specialization match the templated function:
3167
3168 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3169 */
3170 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3171 {
3172 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3173 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3174
3175 /* A concept cannot be specialized. */
3176 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3177 {
3178 error ("explicit specialization of function concept %qD",
3179 gen_tmpl);
3180 return error_mark_node;
3181 }
3182
3183 /* This specialization has the same linkage and visibility as
3184 the function template it specializes. */
3185 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3186 if (! TREE_PUBLIC (decl))
3187 {
3188 DECL_INTERFACE_KNOWN (decl) = 1;
3189 DECL_NOT_REALLY_EXTERN (decl) = 1;
3190 }
3191 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3192 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3193 {
3194 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3195 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3196 }
3197 }
3198
3199 /* If DECL is a friend declaration, declared using an
3200 unqualified name, the namespace associated with DECL may
3201 have been set incorrectly. For example, in:
3202
3203 template <typename T> void f(T);
3204 namespace N {
3205 struct S { friend void f<int>(int); }
3206 }
3207
3208 we will have set the DECL_CONTEXT for the friend
3209 declaration to N, rather than to the global namespace. */
3210 if (DECL_NAMESPACE_SCOPE_P (decl))
3211 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3212
3213 if (is_friend && !have_def)
3214 /* This is not really a declaration of a specialization.
3215 It's just the name of an instantiation. But, it's not
3216 a request for an instantiation, either. */
3217 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3218 else if (TREE_CODE (decl) == FUNCTION_DECL)
3219 /* A specialization is not necessarily COMDAT. */
3220 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3221 && DECL_DECLARED_INLINE_P (decl));
3222 else if (VAR_P (decl))
3223 DECL_COMDAT (decl) = false;
3224
3225 /* If this is a full specialization, register it so that we can find
3226 it again. Partial specializations will be registered in
3227 process_partial_specialization. */
3228 if (!processing_template_decl)
3229 {
3230 warn_spec_missing_attributes (gen_tmpl, decl, attrlist);
3231
3232 decl = register_specialization (decl, gen_tmpl, targs,
3233 is_friend, 0);
3234 }
3235
3236
3237 /* A 'structor should already have clones. */
3238 gcc_assert (decl == error_mark_node
3239 || variable_template_p (tmpl)
3240 || !(DECL_CONSTRUCTOR_P (decl)
3241 || DECL_DESTRUCTOR_P (decl))
3242 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3243 }
3244 }
3245
3246 return decl;
3247 }
3248
3249 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3250 parameters. These are represented in the same format used for
3251 DECL_TEMPLATE_PARMS. */
3252
3253 int
3254 comp_template_parms (const_tree parms1, const_tree parms2)
3255 {
3256 const_tree p1;
3257 const_tree p2;
3258
3259 if (parms1 == parms2)
3260 return 1;
3261
3262 for (p1 = parms1, p2 = parms2;
3263 p1 != NULL_TREE && p2 != NULL_TREE;
3264 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3265 {
3266 tree t1 = TREE_VALUE (p1);
3267 tree t2 = TREE_VALUE (p2);
3268 int i;
3269
3270 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3271 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3272
3273 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3274 return 0;
3275
3276 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3277 {
3278 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3279 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3280
3281 /* If either of the template parameters are invalid, assume
3282 they match for the sake of error recovery. */
3283 if (error_operand_p (parm1) || error_operand_p (parm2))
3284 return 1;
3285
3286 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3287 return 0;
3288
3289 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3290 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3291 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3292 continue;
3293 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3294 return 0;
3295 }
3296 }
3297
3298 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3299 /* One set of parameters has more parameters lists than the
3300 other. */
3301 return 0;
3302
3303 return 1;
3304 }
3305
3306 /* Determine whether PARM is a parameter pack. */
3307
3308 bool
3309 template_parameter_pack_p (const_tree parm)
3310 {
3311 /* Determine if we have a non-type template parameter pack. */
3312 if (TREE_CODE (parm) == PARM_DECL)
3313 return (DECL_TEMPLATE_PARM_P (parm)
3314 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3315 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3316 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3317
3318 /* If this is a list of template parameters, we could get a
3319 TYPE_DECL or a TEMPLATE_DECL. */
3320 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3321 parm = TREE_TYPE (parm);
3322
3323 /* Otherwise it must be a type template parameter. */
3324 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3325 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3326 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3327 }
3328
3329 /* Determine if T is a function parameter pack. */
3330
3331 bool
3332 function_parameter_pack_p (const_tree t)
3333 {
3334 if (t && TREE_CODE (t) == PARM_DECL)
3335 return DECL_PACK_P (t);
3336 return false;
3337 }
3338
3339 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3340 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3341
3342 tree
3343 get_function_template_decl (const_tree primary_func_tmpl_inst)
3344 {
3345 if (! primary_func_tmpl_inst
3346 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3347 || ! primary_template_specialization_p (primary_func_tmpl_inst))
3348 return NULL;
3349
3350 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3351 }
3352
3353 /* Return true iff the function parameter PARAM_DECL was expanded
3354 from the function parameter pack PACK. */
3355
3356 bool
3357 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3358 {
3359 if (DECL_ARTIFICIAL (param_decl)
3360 || !function_parameter_pack_p (pack))
3361 return false;
3362
3363 /* The parameter pack and its pack arguments have the same
3364 DECL_PARM_INDEX. */
3365 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3366 }
3367
3368 /* Determine whether ARGS describes a variadic template args list,
3369 i.e., one that is terminated by a template argument pack. */
3370
3371 static bool
3372 template_args_variadic_p (tree args)
3373 {
3374 int nargs;
3375 tree last_parm;
3376
3377 if (args == NULL_TREE)
3378 return false;
3379
3380 args = INNERMOST_TEMPLATE_ARGS (args);
3381 nargs = TREE_VEC_LENGTH (args);
3382
3383 if (nargs == 0)
3384 return false;
3385
3386 last_parm = TREE_VEC_ELT (args, nargs - 1);
3387
3388 return ARGUMENT_PACK_P (last_parm);
3389 }
3390
3391 /* Generate a new name for the parameter pack name NAME (an
3392 IDENTIFIER_NODE) that incorporates its */
3393
3394 static tree
3395 make_ith_pack_parameter_name (tree name, int i)
3396 {
3397 /* Munge the name to include the parameter index. */
3398 #define NUMBUF_LEN 128
3399 char numbuf[NUMBUF_LEN];
3400 char* newname;
3401 int newname_len;
3402
3403 if (name == NULL_TREE)
3404 return name;
3405 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3406 newname_len = IDENTIFIER_LENGTH (name)
3407 + strlen (numbuf) + 2;
3408 newname = (char*)alloca (newname_len);
3409 snprintf (newname, newname_len,
3410 "%s#%i", IDENTIFIER_POINTER (name), i);
3411 return get_identifier (newname);
3412 }
3413
3414 /* Return true if T is a primary function, class or alias template
3415 specialization, not including the template pattern. */
3416
3417 bool
3418 primary_template_specialization_p (const_tree t)
3419 {
3420 if (!t)
3421 return false;
3422
3423 if (TREE_CODE (t) == FUNCTION_DECL || VAR_P (t))
3424 return (DECL_LANG_SPECIFIC (t)
3425 && DECL_USE_TEMPLATE (t)
3426 && DECL_TEMPLATE_INFO (t)
3427 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
3428 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3429 return (CLASSTYPE_TEMPLATE_INFO (t)
3430 && CLASSTYPE_USE_TEMPLATE (t)
3431 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
3432 else if (alias_template_specialization_p (t))
3433 return true;
3434 return false;
3435 }
3436
3437 /* Return true if PARM is a template template parameter. */
3438
3439 bool
3440 template_template_parameter_p (const_tree parm)
3441 {
3442 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3443 }
3444
3445 /* Return true iff PARM is a DECL representing a type template
3446 parameter. */
3447
3448 bool
3449 template_type_parameter_p (const_tree parm)
3450 {
3451 return (parm
3452 && (TREE_CODE (parm) == TYPE_DECL
3453 || TREE_CODE (parm) == TEMPLATE_DECL)
3454 && DECL_TEMPLATE_PARM_P (parm));
3455 }
3456
3457 /* Return the template parameters of T if T is a
3458 primary template instantiation, NULL otherwise. */
3459
3460 tree
3461 get_primary_template_innermost_parameters (const_tree t)
3462 {
3463 tree parms = NULL, template_info = NULL;
3464
3465 if ((template_info = get_template_info (t))
3466 && primary_template_specialization_p (t))
3467 parms = INNERMOST_TEMPLATE_PARMS
3468 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3469
3470 return parms;
3471 }
3472
3473 /* Return the template parameters of the LEVELth level from the full list
3474 of template parameters PARMS. */
3475
3476 tree
3477 get_template_parms_at_level (tree parms, int level)
3478 {
3479 tree p;
3480 if (!parms
3481 || TREE_CODE (parms) != TREE_LIST
3482 || level > TMPL_PARMS_DEPTH (parms))
3483 return NULL_TREE;
3484
3485 for (p = parms; p; p = TREE_CHAIN (p))
3486 if (TMPL_PARMS_DEPTH (p) == level)
3487 return p;
3488
3489 return NULL_TREE;
3490 }
3491
3492 /* Returns the template arguments of T if T is a template instantiation,
3493 NULL otherwise. */
3494
3495 tree
3496 get_template_innermost_arguments (const_tree t)
3497 {
3498 tree args = NULL, template_info = NULL;
3499
3500 if ((template_info = get_template_info (t))
3501 && TI_ARGS (template_info))
3502 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3503
3504 return args;
3505 }
3506
3507 /* Return the argument pack elements of T if T is a template argument pack,
3508 NULL otherwise. */
3509
3510 tree
3511 get_template_argument_pack_elems (const_tree t)
3512 {
3513 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3514 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3515 return NULL;
3516
3517 return ARGUMENT_PACK_ARGS (t);
3518 }
3519
3520 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3521 ARGUMENT_PACK_SELECT represents. */
3522
3523 static tree
3524 argument_pack_select_arg (tree t)
3525 {
3526 tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
3527 tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
3528
3529 /* If the selected argument is an expansion E, that most likely means we were
3530 called from gen_elem_of_pack_expansion_instantiation during the
3531 substituting of an argument pack (of which the Ith element is a pack
3532 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3533 In this case, the Ith element resulting from this substituting is going to
3534 be a pack expansion, which pattern is the pattern of E. Let's return the
3535 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3536 resulting pack expansion from it. */
3537 if (PACK_EXPANSION_P (arg))
3538 {
3539 /* Make sure we aren't throwing away arg info. */
3540 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
3541 arg = PACK_EXPANSION_PATTERN (arg);
3542 }
3543
3544 return arg;
3545 }
3546
3547
3548 /* True iff FN is a function representing a built-in variadic parameter
3549 pack. */
3550
3551 bool
3552 builtin_pack_fn_p (tree fn)
3553 {
3554 if (!fn
3555 || TREE_CODE (fn) != FUNCTION_DECL
3556 || !DECL_IS_BUILTIN (fn))
3557 return false;
3558
3559 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3560 return true;
3561
3562 return false;
3563 }
3564
3565 /* True iff CALL is a call to a function representing a built-in variadic
3566 parameter pack. */
3567
3568 static bool
3569 builtin_pack_call_p (tree call)
3570 {
3571 if (TREE_CODE (call) != CALL_EXPR)
3572 return false;
3573 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3574 }
3575
3576 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3577
3578 static tree
3579 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3580 tree in_decl)
3581 {
3582 tree ohi = CALL_EXPR_ARG (call, 0);
3583 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3584 false/*fn*/, true/*int_cst*/);
3585
3586 if (value_dependent_expression_p (hi))
3587 {
3588 if (hi != ohi)
3589 {
3590 call = copy_node (call);
3591 CALL_EXPR_ARG (call, 0) = hi;
3592 }
3593 tree ex = make_pack_expansion (call, complain);
3594 tree vec = make_tree_vec (1);
3595 TREE_VEC_ELT (vec, 0) = ex;
3596 return vec;
3597 }
3598 else
3599 {
3600 hi = cxx_constant_value (hi);
3601 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3602
3603 /* Calculate the largest value of len that won't make the size of the vec
3604 overflow an int. The compiler will exceed resource limits long before
3605 this, but it seems a decent place to diagnose. */
3606 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3607
3608 if (len < 0 || len > max)
3609 {
3610 if ((complain & tf_error)
3611 && hi != error_mark_node)
3612 error ("argument to %<__integer_pack%> must be between 0 and %d",
3613 max);
3614 return error_mark_node;
3615 }
3616
3617 tree vec = make_tree_vec (len);
3618
3619 for (int i = 0; i < len; ++i)
3620 TREE_VEC_ELT (vec, i) = size_int (i);
3621
3622 return vec;
3623 }
3624 }
3625
3626 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3627 CALL. */
3628
3629 static tree
3630 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3631 tree in_decl)
3632 {
3633 if (!builtin_pack_call_p (call))
3634 return NULL_TREE;
3635
3636 tree fn = CALL_EXPR_FN (call);
3637
3638 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3639 return expand_integer_pack (call, args, complain, in_decl);
3640
3641 return NULL_TREE;
3642 }
3643
3644 /* Structure used to track the progress of find_parameter_packs_r. */
3645 struct find_parameter_pack_data
3646 {
3647 /* TREE_LIST that will contain all of the parameter packs found by
3648 the traversal. */
3649 tree* parameter_packs;
3650
3651 /* Set of AST nodes that have been visited by the traversal. */
3652 hash_set<tree> *visited;
3653
3654 /* True iff we're making a type pack expansion. */
3655 bool type_pack_expansion_p;
3656 };
3657
3658 /* Identifies all of the argument packs that occur in a template
3659 argument and appends them to the TREE_LIST inside DATA, which is a
3660 find_parameter_pack_data structure. This is a subroutine of
3661 make_pack_expansion and uses_parameter_packs. */
3662 static tree
3663 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3664 {
3665 tree t = *tp;
3666 struct find_parameter_pack_data* ppd =
3667 (struct find_parameter_pack_data*)data;
3668 bool parameter_pack_p = false;
3669
3670 /* Handle type aliases/typedefs. */
3671 if (TYPE_ALIAS_P (t))
3672 {
3673 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3674 cp_walk_tree (&TI_ARGS (tinfo),
3675 &find_parameter_packs_r,
3676 ppd, ppd->visited);
3677 *walk_subtrees = 0;
3678 return NULL_TREE;
3679 }
3680
3681 /* Identify whether this is a parameter pack or not. */
3682 switch (TREE_CODE (t))
3683 {
3684 case TEMPLATE_PARM_INDEX:
3685 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3686 parameter_pack_p = true;
3687 break;
3688
3689 case TEMPLATE_TYPE_PARM:
3690 t = TYPE_MAIN_VARIANT (t);
3691 /* FALLTHRU */
3692 case TEMPLATE_TEMPLATE_PARM:
3693 /* If the placeholder appears in the decl-specifier-seq of a function
3694 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3695 is a pack expansion, the invented template parameter is a template
3696 parameter pack. */
3697 if (ppd->type_pack_expansion_p && is_auto (t))
3698 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3699 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3700 parameter_pack_p = true;
3701 break;
3702
3703 case FIELD_DECL:
3704 case PARM_DECL:
3705 if (DECL_PACK_P (t))
3706 {
3707 /* We don't want to walk into the type of a PARM_DECL,
3708 because we don't want to see the type parameter pack. */
3709 *walk_subtrees = 0;
3710 parameter_pack_p = true;
3711 }
3712 break;
3713
3714 case VAR_DECL:
3715 if (DECL_PACK_P (t))
3716 {
3717 /* We don't want to walk into the type of a variadic capture proxy,
3718 because we don't want to see the type parameter pack. */
3719 *walk_subtrees = 0;
3720 parameter_pack_p = true;
3721 }
3722 else if (variable_template_specialization_p (t))
3723 {
3724 cp_walk_tree (&DECL_TI_ARGS (t),
3725 find_parameter_packs_r,
3726 ppd, ppd->visited);
3727 *walk_subtrees = 0;
3728 }
3729 break;
3730
3731 case CALL_EXPR:
3732 if (builtin_pack_call_p (t))
3733 parameter_pack_p = true;
3734 break;
3735
3736 case BASES:
3737 parameter_pack_p = true;
3738 break;
3739 default:
3740 /* Not a parameter pack. */
3741 break;
3742 }
3743
3744 if (parameter_pack_p)
3745 {
3746 /* Add this parameter pack to the list. */
3747 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3748 }
3749
3750 if (TYPE_P (t))
3751 cp_walk_tree (&TYPE_CONTEXT (t),
3752 &find_parameter_packs_r, ppd, ppd->visited);
3753
3754 /* This switch statement will return immediately if we don't find a
3755 parameter pack. */
3756 switch (TREE_CODE (t))
3757 {
3758 case TEMPLATE_PARM_INDEX:
3759 return NULL_TREE;
3760
3761 case BOUND_TEMPLATE_TEMPLATE_PARM:
3762 /* Check the template itself. */
3763 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3764 &find_parameter_packs_r, ppd, ppd->visited);
3765 /* Check the template arguments. */
3766 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3767 ppd->visited);
3768 *walk_subtrees = 0;
3769 return NULL_TREE;
3770
3771 case TEMPLATE_TYPE_PARM:
3772 case TEMPLATE_TEMPLATE_PARM:
3773 return NULL_TREE;
3774
3775 case PARM_DECL:
3776 return NULL_TREE;
3777
3778 case DECL_EXPR:
3779 /* Ignore the declaration of a capture proxy for a parameter pack. */
3780 if (is_capture_proxy (DECL_EXPR_DECL (t)))
3781 *walk_subtrees = 0;
3782 return NULL_TREE;
3783
3784 case RECORD_TYPE:
3785 if (TYPE_PTRMEMFUNC_P (t))
3786 return NULL_TREE;
3787 /* Fall through. */
3788
3789 case UNION_TYPE:
3790 case ENUMERAL_TYPE:
3791 if (TYPE_TEMPLATE_INFO (t))
3792 cp_walk_tree (&TYPE_TI_ARGS (t),
3793 &find_parameter_packs_r, ppd, ppd->visited);
3794
3795 *walk_subtrees = 0;
3796 return NULL_TREE;
3797
3798 case TEMPLATE_DECL:
3799 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3800 return NULL_TREE;
3801 gcc_fallthrough();
3802
3803 case CONSTRUCTOR:
3804 cp_walk_tree (&TREE_TYPE (t),
3805 &find_parameter_packs_r, ppd, ppd->visited);
3806 return NULL_TREE;
3807
3808 case TYPENAME_TYPE:
3809 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3810 ppd, ppd->visited);
3811 *walk_subtrees = 0;
3812 return NULL_TREE;
3813
3814 case TYPE_PACK_EXPANSION:
3815 case EXPR_PACK_EXPANSION:
3816 *walk_subtrees = 0;
3817 return NULL_TREE;
3818
3819 case INTEGER_TYPE:
3820 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3821 ppd, ppd->visited);
3822 *walk_subtrees = 0;
3823 return NULL_TREE;
3824
3825 case IDENTIFIER_NODE:
3826 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3827 ppd->visited);
3828 *walk_subtrees = 0;
3829 return NULL_TREE;
3830
3831 case LAMBDA_EXPR:
3832 {
3833 /* Look at explicit captures. */
3834 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t);
3835 cap; cap = TREE_CHAIN (cap))
3836 cp_walk_tree (&TREE_VALUE (cap), &find_parameter_packs_r, ppd,
3837 ppd->visited);
3838 /* Since we defer implicit capture, look in the parms and body. */
3839 tree fn = lambda_function (t);
3840 cp_walk_tree (&TREE_TYPE (fn), &find_parameter_packs_r, ppd,
3841 ppd->visited);
3842 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
3843 ppd->visited);
3844 *walk_subtrees = 0;
3845 return NULL_TREE;
3846 }
3847
3848 case DECLTYPE_TYPE:
3849 {
3850 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3851 type_pack_expansion_p to false so that any placeholders
3852 within the expression don't get marked as parameter packs. */
3853 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
3854 ppd->type_pack_expansion_p = false;
3855 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
3856 ppd, ppd->visited);
3857 ppd->type_pack_expansion_p = type_pack_expansion_p;
3858 *walk_subtrees = 0;
3859 return NULL_TREE;
3860 }
3861
3862 case IF_STMT:
3863 cp_walk_tree (&IF_COND (t), &find_parameter_packs_r,
3864 ppd, ppd->visited);
3865 cp_walk_tree (&THEN_CLAUSE (t), &find_parameter_packs_r,
3866 ppd, ppd->visited);
3867 cp_walk_tree (&ELSE_CLAUSE (t), &find_parameter_packs_r,
3868 ppd, ppd->visited);
3869 /* Don't walk into IF_STMT_EXTRA_ARGS. */
3870 *walk_subtrees = 0;
3871 return NULL_TREE;
3872
3873 default:
3874 return NULL_TREE;
3875 }
3876
3877 return NULL_TREE;
3878 }
3879
3880 /* Determines if the expression or type T uses any parameter packs. */
3881 tree
3882 uses_parameter_packs (tree t)
3883 {
3884 tree parameter_packs = NULL_TREE;
3885 struct find_parameter_pack_data ppd;
3886 ppd.parameter_packs = &parameter_packs;
3887 ppd.visited = new hash_set<tree>;
3888 ppd.type_pack_expansion_p = false;
3889 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3890 delete ppd.visited;
3891 return parameter_packs;
3892 }
3893
3894 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3895 representation a base-class initializer into a parameter pack
3896 expansion. If all goes well, the resulting node will be an
3897 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3898 respectively. */
3899 tree
3900 make_pack_expansion (tree arg, tsubst_flags_t complain)
3901 {
3902 tree result;
3903 tree parameter_packs = NULL_TREE;
3904 bool for_types = false;
3905 struct find_parameter_pack_data ppd;
3906
3907 if (!arg || arg == error_mark_node)
3908 return arg;
3909
3910 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
3911 {
3912 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3913 class initializer. In this case, the TREE_PURPOSE will be a
3914 _TYPE node (representing the base class expansion we're
3915 initializing) and the TREE_VALUE will be a TREE_LIST
3916 containing the initialization arguments.
3917
3918 The resulting expansion looks somewhat different from most
3919 expansions. Rather than returning just one _EXPANSION, we
3920 return a TREE_LIST whose TREE_PURPOSE is a
3921 TYPE_PACK_EXPANSION containing the bases that will be
3922 initialized. The TREE_VALUE will be identical to the
3923 original TREE_VALUE, which is a list of arguments that will
3924 be passed to each base. We do not introduce any new pack
3925 expansion nodes into the TREE_VALUE (although it is possible
3926 that some already exist), because the TREE_PURPOSE and
3927 TREE_VALUE all need to be expanded together with the same
3928 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3929 resulting TREE_PURPOSE will mention the parameter packs in
3930 both the bases and the arguments to the bases. */
3931 tree purpose;
3932 tree value;
3933 tree parameter_packs = NULL_TREE;
3934
3935 /* Determine which parameter packs will be used by the base
3936 class expansion. */
3937 ppd.visited = new hash_set<tree>;
3938 ppd.parameter_packs = &parameter_packs;
3939 ppd.type_pack_expansion_p = false;
3940 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
3941 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3942 &ppd, ppd.visited);
3943
3944 if (parameter_packs == NULL_TREE)
3945 {
3946 if (complain & tf_error)
3947 error ("base initializer expansion %qT contains no parameter packs",
3948 arg);
3949 delete ppd.visited;
3950 return error_mark_node;
3951 }
3952
3953 if (TREE_VALUE (arg) != void_type_node)
3954 {
3955 /* Collect the sets of parameter packs used in each of the
3956 initialization arguments. */
3957 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3958 {
3959 /* Determine which parameter packs will be expanded in this
3960 argument. */
3961 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3962 &ppd, ppd.visited);
3963 }
3964 }
3965
3966 delete ppd.visited;
3967
3968 /* Create the pack expansion type for the base type. */
3969 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3970 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3971 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3972 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
3973
3974 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3975 they will rarely be compared to anything. */
3976 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3977
3978 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3979 }
3980
3981 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3982 for_types = true;
3983
3984 /* Build the PACK_EXPANSION_* node. */
3985 result = for_types
3986 ? cxx_make_type (TYPE_PACK_EXPANSION)
3987 : make_node (EXPR_PACK_EXPANSION);
3988 SET_PACK_EXPANSION_PATTERN (result, arg);
3989 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3990 {
3991 /* Propagate type and const-expression information. */
3992 TREE_TYPE (result) = TREE_TYPE (arg);
3993 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3994 /* Mark this read now, since the expansion might be length 0. */
3995 mark_exp_read (arg);
3996 }
3997 else
3998 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3999 they will rarely be compared to anything. */
4000 SET_TYPE_STRUCTURAL_EQUALITY (result);
4001
4002 /* Determine which parameter packs will be expanded. */
4003 ppd.parameter_packs = &parameter_packs;
4004 ppd.visited = new hash_set<tree>;
4005 ppd.type_pack_expansion_p = TYPE_P (arg);
4006 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
4007 delete ppd.visited;
4008
4009 /* Make sure we found some parameter packs. */
4010 if (parameter_packs == NULL_TREE)
4011 {
4012 if (complain & tf_error)
4013 {
4014 if (TYPE_P (arg))
4015 error ("expansion pattern %qT contains no parameter packs", arg);
4016 else
4017 error ("expansion pattern %qE contains no parameter packs", arg);
4018 }
4019 return error_mark_node;
4020 }
4021 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
4022
4023 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
4024
4025 return result;
4026 }
4027
4028 /* Checks T for any "bare" parameter packs, which have not yet been
4029 expanded, and issues an error if any are found. This operation can
4030 only be done on full expressions or types (e.g., an expression
4031 statement, "if" condition, etc.), because we could have expressions like:
4032
4033 foo(f(g(h(args)))...)
4034
4035 where "args" is a parameter pack. check_for_bare_parameter_packs
4036 should not be called for the subexpressions args, h(args),
4037 g(h(args)), or f(g(h(args))), because we would produce erroneous
4038 error messages.
4039
4040 Returns TRUE and emits an error if there were bare parameter packs,
4041 returns FALSE otherwise. */
4042 bool
4043 check_for_bare_parameter_packs (tree t, location_t loc /* = UNKNOWN_LOCATION */)
4044 {
4045 tree parameter_packs = NULL_TREE;
4046 struct find_parameter_pack_data ppd;
4047
4048 if (!processing_template_decl || !t || t == error_mark_node)
4049 return false;
4050
4051 /* A lambda might use a parameter pack from the containing context. */
4052 if (current_class_type && LAMBDA_TYPE_P (current_class_type)
4053 && CLASSTYPE_TEMPLATE_INFO (current_class_type))
4054 return false;
4055
4056 if (TREE_CODE (t) == TYPE_DECL)
4057 t = TREE_TYPE (t);
4058
4059 ppd.parameter_packs = &parameter_packs;
4060 ppd.visited = new hash_set<tree>;
4061 ppd.type_pack_expansion_p = false;
4062 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4063 delete ppd.visited;
4064
4065 if (parameter_packs)
4066 {
4067 if (loc == UNKNOWN_LOCATION)
4068 loc = cp_expr_loc_or_input_loc (t);
4069 error_at (loc, "parameter packs not expanded with %<...%>:");
4070 while (parameter_packs)
4071 {
4072 tree pack = TREE_VALUE (parameter_packs);
4073 tree name = NULL_TREE;
4074
4075 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
4076 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
4077 name = TYPE_NAME (pack);
4078 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
4079 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
4080 else if (TREE_CODE (pack) == CALL_EXPR)
4081 name = DECL_NAME (CALL_EXPR_FN (pack));
4082 else
4083 name = DECL_NAME (pack);
4084
4085 if (name)
4086 inform (loc, " %qD", name);
4087 else
4088 inform (loc, " %s", "<anonymous>");
4089
4090 parameter_packs = TREE_CHAIN (parameter_packs);
4091 }
4092
4093 return true;
4094 }
4095
4096 return false;
4097 }
4098
4099 /* Expand any parameter packs that occur in the template arguments in
4100 ARGS. */
4101 tree
4102 expand_template_argument_pack (tree args)
4103 {
4104 if (args == error_mark_node)
4105 return error_mark_node;
4106
4107 tree result_args = NULL_TREE;
4108 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
4109 int num_result_args = -1;
4110 int non_default_args_count = -1;
4111
4112 /* First, determine if we need to expand anything, and the number of
4113 slots we'll need. */
4114 for (in_arg = 0; in_arg < nargs; ++in_arg)
4115 {
4116 tree arg = TREE_VEC_ELT (args, in_arg);
4117 if (arg == NULL_TREE)
4118 return args;
4119 if (ARGUMENT_PACK_P (arg))
4120 {
4121 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
4122 if (num_result_args < 0)
4123 num_result_args = in_arg + num_packed;
4124 else
4125 num_result_args += num_packed;
4126 }
4127 else
4128 {
4129 if (num_result_args >= 0)
4130 num_result_args++;
4131 }
4132 }
4133
4134 /* If no expansion is necessary, we're done. */
4135 if (num_result_args < 0)
4136 return args;
4137
4138 /* Expand arguments. */
4139 result_args = make_tree_vec (num_result_args);
4140 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
4141 non_default_args_count =
4142 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
4143 for (in_arg = 0; in_arg < nargs; ++in_arg)
4144 {
4145 tree arg = TREE_VEC_ELT (args, in_arg);
4146 if (ARGUMENT_PACK_P (arg))
4147 {
4148 tree packed = ARGUMENT_PACK_ARGS (arg);
4149 int i, num_packed = TREE_VEC_LENGTH (packed);
4150 for (i = 0; i < num_packed; ++i, ++out_arg)
4151 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
4152 if (non_default_args_count > 0)
4153 non_default_args_count += num_packed - 1;
4154 }
4155 else
4156 {
4157 TREE_VEC_ELT (result_args, out_arg) = arg;
4158 ++out_arg;
4159 }
4160 }
4161 if (non_default_args_count >= 0)
4162 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
4163 return result_args;
4164 }
4165
4166 /* Checks if DECL shadows a template parameter.
4167
4168 [temp.local]: A template-parameter shall not be redeclared within its
4169 scope (including nested scopes).
4170
4171 Emits an error and returns TRUE if the DECL shadows a parameter,
4172 returns FALSE otherwise. */
4173
4174 bool
4175 check_template_shadow (tree decl)
4176 {
4177 tree olddecl;
4178
4179 /* If we're not in a template, we can't possibly shadow a template
4180 parameter. */
4181 if (!current_template_parms)
4182 return true;
4183
4184 /* Figure out what we're shadowing. */
4185 decl = OVL_FIRST (decl);
4186 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4187
4188 /* If there's no previous binding for this name, we're not shadowing
4189 anything, let alone a template parameter. */
4190 if (!olddecl)
4191 return true;
4192
4193 /* If we're not shadowing a template parameter, we're done. Note
4194 that OLDDECL might be an OVERLOAD (or perhaps even an
4195 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4196 node. */
4197 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4198 return true;
4199
4200 /* We check for decl != olddecl to avoid bogus errors for using a
4201 name inside a class. We check TPFI to avoid duplicate errors for
4202 inline member templates. */
4203 if (decl == olddecl
4204 || (DECL_TEMPLATE_PARM_P (decl)
4205 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4206 return true;
4207
4208 /* Don't complain about the injected class name, as we've already
4209 complained about the class itself. */
4210 if (DECL_SELF_REFERENCE_P (decl))
4211 return false;
4212
4213 if (DECL_TEMPLATE_PARM_P (decl))
4214 error ("declaration of template parameter %q+D shadows "
4215 "template parameter", decl);
4216 else
4217 error ("declaration of %q+#D shadows template parameter", decl);
4218 inform (DECL_SOURCE_LOCATION (olddecl),
4219 "template parameter %qD declared here", olddecl);
4220 return false;
4221 }
4222
4223 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4224 ORIG_LEVEL, DECL, and TYPE. */
4225
4226 static tree
4227 build_template_parm_index (int index,
4228 int level,
4229 int orig_level,
4230 tree decl,
4231 tree type)
4232 {
4233 tree t = make_node (TEMPLATE_PARM_INDEX);
4234 TEMPLATE_PARM_IDX (t) = index;
4235 TEMPLATE_PARM_LEVEL (t) = level;
4236 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4237 TEMPLATE_PARM_DECL (t) = decl;
4238 TREE_TYPE (t) = type;
4239 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4240 TREE_READONLY (t) = TREE_READONLY (decl);
4241
4242 return t;
4243 }
4244
4245 /* Find the canonical type parameter for the given template type
4246 parameter. Returns the canonical type parameter, which may be TYPE
4247 if no such parameter existed. */
4248
4249 static tree
4250 canonical_type_parameter (tree type)
4251 {
4252 tree list;
4253 int idx = TEMPLATE_TYPE_IDX (type);
4254 if (!canonical_template_parms)
4255 vec_alloc (canonical_template_parms, idx + 1);
4256
4257 if (canonical_template_parms->length () <= (unsigned) idx)
4258 vec_safe_grow_cleared (canonical_template_parms, idx + 1);
4259
4260 list = (*canonical_template_parms)[idx];
4261 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4262 list = TREE_CHAIN (list);
4263
4264 if (list)
4265 return TREE_VALUE (list);
4266 else
4267 {
4268 (*canonical_template_parms)[idx]
4269 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4270 return type;
4271 }
4272 }
4273
4274 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4275 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4276 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4277 new one is created. */
4278
4279 static tree
4280 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4281 tsubst_flags_t complain)
4282 {
4283 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4284 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4285 != TEMPLATE_PARM_LEVEL (index) - levels)
4286 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4287 {
4288 tree orig_decl = TEMPLATE_PARM_DECL (index);
4289 tree decl, t;
4290
4291 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4292 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
4293 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4294 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4295 DECL_ARTIFICIAL (decl) = 1;
4296 SET_DECL_TEMPLATE_PARM_P (decl);
4297
4298 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4299 TEMPLATE_PARM_LEVEL (index) - levels,
4300 TEMPLATE_PARM_ORIG_LEVEL (index),
4301 decl, type);
4302 TEMPLATE_PARM_DESCENDANTS (index) = t;
4303 TEMPLATE_PARM_PARAMETER_PACK (t)
4304 = TEMPLATE_PARM_PARAMETER_PACK (index);
4305
4306 /* Template template parameters need this. */
4307 if (TREE_CODE (decl) == TEMPLATE_DECL)
4308 {
4309 DECL_TEMPLATE_RESULT (decl)
4310 = build_decl (DECL_SOURCE_LOCATION (decl),
4311 TYPE_DECL, DECL_NAME (decl), type);
4312 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl)) = true;
4313 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4314 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4315 }
4316 }
4317
4318 return TEMPLATE_PARM_DESCENDANTS (index);
4319 }
4320
4321 /* Process information from new template parameter PARM and append it
4322 to the LIST being built. This new parameter is a non-type
4323 parameter iff IS_NON_TYPE is true. This new parameter is a
4324 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4325 is in PARM_LOC. */
4326
4327 tree
4328 process_template_parm (tree list, location_t parm_loc, tree parm,
4329 bool is_non_type, bool is_parameter_pack)
4330 {
4331 tree decl = 0;
4332 int idx = 0;
4333
4334 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4335 tree defval = TREE_PURPOSE (parm);
4336 tree constr = TREE_TYPE (parm);
4337
4338 if (list)
4339 {
4340 tree p = tree_last (list);
4341
4342 if (p && TREE_VALUE (p) != error_mark_node)
4343 {
4344 p = TREE_VALUE (p);
4345 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4346 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4347 else
4348 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4349 }
4350
4351 ++idx;
4352 }
4353
4354 if (is_non_type)
4355 {
4356 parm = TREE_VALUE (parm);
4357
4358 SET_DECL_TEMPLATE_PARM_P (parm);
4359
4360 if (TREE_TYPE (parm) != error_mark_node)
4361 {
4362 /* [temp.param]
4363
4364 The top-level cv-qualifiers on the template-parameter are
4365 ignored when determining its type. */
4366 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4367 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4368 TREE_TYPE (parm) = error_mark_node;
4369 else if (uses_parameter_packs (TREE_TYPE (parm))
4370 && !is_parameter_pack
4371 /* If we're in a nested template parameter list, the template
4372 template parameter could be a parameter pack. */
4373 && processing_template_parmlist == 1)
4374 {
4375 /* This template parameter is not a parameter pack, but it
4376 should be. Complain about "bare" parameter packs. */
4377 check_for_bare_parameter_packs (TREE_TYPE (parm));
4378
4379 /* Recover by calling this a parameter pack. */
4380 is_parameter_pack = true;
4381 }
4382 }
4383
4384 /* A template parameter is not modifiable. */
4385 TREE_CONSTANT (parm) = 1;
4386 TREE_READONLY (parm) = 1;
4387 decl = build_decl (parm_loc,
4388 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4389 TREE_CONSTANT (decl) = 1;
4390 TREE_READONLY (decl) = 1;
4391 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4392 = build_template_parm_index (idx, processing_template_decl,
4393 processing_template_decl,
4394 decl, TREE_TYPE (parm));
4395
4396 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4397 = is_parameter_pack;
4398 }
4399 else
4400 {
4401 tree t;
4402 parm = TREE_VALUE (TREE_VALUE (parm));
4403
4404 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4405 {
4406 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4407 /* This is for distinguishing between real templates and template
4408 template parameters */
4409 TREE_TYPE (parm) = t;
4410 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4411 decl = parm;
4412 }
4413 else
4414 {
4415 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4416 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4417 decl = build_decl (parm_loc,
4418 TYPE_DECL, parm, t);
4419 }
4420
4421 TYPE_NAME (t) = decl;
4422 TYPE_STUB_DECL (t) = decl;
4423 parm = decl;
4424 TEMPLATE_TYPE_PARM_INDEX (t)
4425 = build_template_parm_index (idx, processing_template_decl,
4426 processing_template_decl,
4427 decl, TREE_TYPE (parm));
4428 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4429 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4430 }
4431 DECL_ARTIFICIAL (decl) = 1;
4432 SET_DECL_TEMPLATE_PARM_P (decl);
4433
4434 /* Build requirements for the type/template parameter.
4435 This must be done after SET_DECL_TEMPLATE_PARM_P or
4436 process_template_parm could fail. */
4437 tree reqs = finish_shorthand_constraint (parm, constr);
4438
4439 decl = pushdecl (decl);
4440 if (!is_non_type)
4441 parm = decl;
4442
4443 /* Build the parameter node linking the parameter declaration,
4444 its default argument (if any), and its constraints (if any). */
4445 parm = build_tree_list (defval, parm);
4446 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4447
4448 return chainon (list, parm);
4449 }
4450
4451 /* The end of a template parameter list has been reached. Process the
4452 tree list into a parameter vector, converting each parameter into a more
4453 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4454 as PARM_DECLs. */
4455
4456 tree
4457 end_template_parm_list (tree parms)
4458 {
4459 int nparms;
4460 tree parm, next;
4461 tree saved_parmlist = make_tree_vec (list_length (parms));
4462
4463 /* Pop the dummy parameter level and add the real one. */
4464 current_template_parms = TREE_CHAIN (current_template_parms);
4465
4466 current_template_parms
4467 = tree_cons (size_int (processing_template_decl),
4468 saved_parmlist, current_template_parms);
4469
4470 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4471 {
4472 next = TREE_CHAIN (parm);
4473 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4474 TREE_CHAIN (parm) = NULL_TREE;
4475 }
4476
4477 --processing_template_parmlist;
4478
4479 return saved_parmlist;
4480 }
4481
4482 // Explicitly indicate the end of the template parameter list. We assume
4483 // that the current template parameters have been constructed and/or
4484 // managed explicitly, as when creating new template template parameters
4485 // from a shorthand constraint.
4486 void
4487 end_template_parm_list ()
4488 {
4489 --processing_template_parmlist;
4490 }
4491
4492 /* end_template_decl is called after a template declaration is seen. */
4493
4494 void
4495 end_template_decl (void)
4496 {
4497 reset_specialization ();
4498
4499 if (! processing_template_decl)
4500 return;
4501
4502 /* This matches the pushlevel in begin_template_parm_list. */
4503 finish_scope ();
4504
4505 --processing_template_decl;
4506 current_template_parms = TREE_CHAIN (current_template_parms);
4507 }
4508
4509 /* Takes a TREE_LIST representing a template parameter and convert it
4510 into an argument suitable to be passed to the type substitution
4511 functions. Note that If the TREE_LIST contains an error_mark
4512 node, the returned argument is error_mark_node. */
4513
4514 tree
4515 template_parm_to_arg (tree t)
4516 {
4517
4518 if (t == NULL_TREE
4519 || TREE_CODE (t) != TREE_LIST)
4520 return t;
4521
4522 if (error_operand_p (TREE_VALUE (t)))
4523 return error_mark_node;
4524
4525 t = TREE_VALUE (t);
4526
4527 if (TREE_CODE (t) == TYPE_DECL
4528 || TREE_CODE (t) == TEMPLATE_DECL)
4529 {
4530 t = TREE_TYPE (t);
4531
4532 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4533 {
4534 /* Turn this argument into a TYPE_ARGUMENT_PACK
4535 with a single element, which expands T. */
4536 tree vec = make_tree_vec (1);
4537 if (CHECKING_P)
4538 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4539
4540 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4541
4542 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4543 SET_ARGUMENT_PACK_ARGS (t, vec);
4544 }
4545 }
4546 else
4547 {
4548 t = DECL_INITIAL (t);
4549
4550 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4551 {
4552 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4553 with a single element, which expands T. */
4554 tree vec = make_tree_vec (1);
4555 if (CHECKING_P)
4556 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4557
4558 t = convert_from_reference (t);
4559 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4560
4561 t = make_node (NONTYPE_ARGUMENT_PACK);
4562 SET_ARGUMENT_PACK_ARGS (t, vec);
4563 }
4564 else
4565 t = convert_from_reference (t);
4566 }
4567 return t;
4568 }
4569
4570 /* Given a single level of template parameters (a TREE_VEC), return it
4571 as a set of template arguments. */
4572
4573 static tree
4574 template_parms_level_to_args (tree parms)
4575 {
4576 tree a = copy_node (parms);
4577 TREE_TYPE (a) = NULL_TREE;
4578 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4579 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4580
4581 if (CHECKING_P)
4582 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4583
4584 return a;
4585 }
4586
4587 /* Given a set of template parameters, return them as a set of template
4588 arguments. The template parameters are represented as a TREE_VEC, in
4589 the form documented in cp-tree.h for template arguments. */
4590
4591 static tree
4592 template_parms_to_args (tree parms)
4593 {
4594 tree header;
4595 tree args = NULL_TREE;
4596 int length = TMPL_PARMS_DEPTH (parms);
4597 int l = length;
4598
4599 /* If there is only one level of template parameters, we do not
4600 create a TREE_VEC of TREE_VECs. Instead, we return a single
4601 TREE_VEC containing the arguments. */
4602 if (length > 1)
4603 args = make_tree_vec (length);
4604
4605 for (header = parms; header; header = TREE_CHAIN (header))
4606 {
4607 tree a = template_parms_level_to_args (TREE_VALUE (header));
4608
4609 if (length > 1)
4610 TREE_VEC_ELT (args, --l) = a;
4611 else
4612 args = a;
4613 }
4614
4615 return args;
4616 }
4617
4618 /* Within the declaration of a template, return the currently active
4619 template parameters as an argument TREE_VEC. */
4620
4621 static tree
4622 current_template_args (void)
4623 {
4624 return template_parms_to_args (current_template_parms);
4625 }
4626
4627 /* Update the declared TYPE by doing any lookups which were thought to be
4628 dependent, but are not now that we know the SCOPE of the declarator. */
4629
4630 tree
4631 maybe_update_decl_type (tree orig_type, tree scope)
4632 {
4633 tree type = orig_type;
4634
4635 if (type == NULL_TREE)
4636 return type;
4637
4638 if (TREE_CODE (orig_type) == TYPE_DECL)
4639 type = TREE_TYPE (type);
4640
4641 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4642 && dependent_type_p (type)
4643 /* Don't bother building up the args in this case. */
4644 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4645 {
4646 /* tsubst in the args corresponding to the template parameters,
4647 including auto if present. Most things will be unchanged, but
4648 make_typename_type and tsubst_qualified_id will resolve
4649 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4650 tree args = current_template_args ();
4651 tree auto_node = type_uses_auto (type);
4652 tree pushed;
4653 if (auto_node)
4654 {
4655 tree auto_vec = make_tree_vec (1);
4656 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4657 args = add_to_template_args (args, auto_vec);
4658 }
4659 pushed = push_scope (scope);
4660 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4661 if (pushed)
4662 pop_scope (scope);
4663 }
4664
4665 if (type == error_mark_node)
4666 return orig_type;
4667
4668 if (TREE_CODE (orig_type) == TYPE_DECL)
4669 {
4670 if (same_type_p (type, TREE_TYPE (orig_type)))
4671 type = orig_type;
4672 else
4673 type = TYPE_NAME (type);
4674 }
4675 return type;
4676 }
4677
4678 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4679 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4680 the new template is a member template. */
4681
4682 static tree
4683 build_template_decl (tree decl, tree parms, bool member_template_p)
4684 {
4685 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4686 SET_DECL_LANGUAGE (tmpl, DECL_LANGUAGE (decl));
4687 DECL_TEMPLATE_PARMS (tmpl) = parms;
4688 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4689 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4690 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4691
4692 return tmpl;
4693 }
4694
4695 struct template_parm_data
4696 {
4697 /* The level of the template parameters we are currently
4698 processing. */
4699 int level;
4700
4701 /* The index of the specialization argument we are currently
4702 processing. */
4703 int current_arg;
4704
4705 /* An array whose size is the number of template parameters. The
4706 elements are nonzero if the parameter has been used in any one
4707 of the arguments processed so far. */
4708 int* parms;
4709
4710 /* An array whose size is the number of template arguments. The
4711 elements are nonzero if the argument makes use of template
4712 parameters of this level. */
4713 int* arg_uses_template_parms;
4714 };
4715
4716 /* Subroutine of push_template_decl used to see if each template
4717 parameter in a partial specialization is used in the explicit
4718 argument list. If T is of the LEVEL given in DATA (which is
4719 treated as a template_parm_data*), then DATA->PARMS is marked
4720 appropriately. */
4721
4722 static int
4723 mark_template_parm (tree t, void* data)
4724 {
4725 int level;
4726 int idx;
4727 struct template_parm_data* tpd = (struct template_parm_data*) data;
4728
4729 template_parm_level_and_index (t, &level, &idx);
4730
4731 if (level == tpd->level)
4732 {
4733 tpd->parms[idx] = 1;
4734 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4735 }
4736
4737 /* In C++17 the type of a non-type argument is a deduced context. */
4738 if (cxx_dialect >= cxx17
4739 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4740 for_each_template_parm (TREE_TYPE (t),
4741 &mark_template_parm,
4742 data,
4743 NULL,
4744 /*include_nondeduced_p=*/false);
4745
4746 /* Return zero so that for_each_template_parm will continue the
4747 traversal of the tree; we want to mark *every* template parm. */
4748 return 0;
4749 }
4750
4751 /* Process the partial specialization DECL. */
4752
4753 static tree
4754 process_partial_specialization (tree decl)
4755 {
4756 tree type = TREE_TYPE (decl);
4757 tree tinfo = get_template_info (decl);
4758 tree maintmpl = TI_TEMPLATE (tinfo);
4759 tree specargs = TI_ARGS (tinfo);
4760 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4761 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4762 tree inner_parms;
4763 tree inst;
4764 int nargs = TREE_VEC_LENGTH (inner_args);
4765 int ntparms;
4766 int i;
4767 bool did_error_intro = false;
4768 struct template_parm_data tpd;
4769 struct template_parm_data tpd2;
4770
4771 gcc_assert (current_template_parms);
4772
4773 /* A concept cannot be specialized. */
4774 if (flag_concepts && variable_concept_p (maintmpl))
4775 {
4776 error ("specialization of variable concept %q#D", maintmpl);
4777 return error_mark_node;
4778 }
4779
4780 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4781 ntparms = TREE_VEC_LENGTH (inner_parms);
4782
4783 /* We check that each of the template parameters given in the
4784 partial specialization is used in the argument list to the
4785 specialization. For example:
4786
4787 template <class T> struct S;
4788 template <class T> struct S<T*>;
4789
4790 The second declaration is OK because `T*' uses the template
4791 parameter T, whereas
4792
4793 template <class T> struct S<int>;
4794
4795 is no good. Even trickier is:
4796
4797 template <class T>
4798 struct S1
4799 {
4800 template <class U>
4801 struct S2;
4802 template <class U>
4803 struct S2<T>;
4804 };
4805
4806 The S2<T> declaration is actually invalid; it is a
4807 full-specialization. Of course,
4808
4809 template <class U>
4810 struct S2<T (*)(U)>;
4811
4812 or some such would have been OK. */
4813 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4814 tpd.parms = XALLOCAVEC (int, ntparms);
4815 memset (tpd.parms, 0, sizeof (int) * ntparms);
4816
4817 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4818 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4819 for (i = 0; i < nargs; ++i)
4820 {
4821 tpd.current_arg = i;
4822 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4823 &mark_template_parm,
4824 &tpd,
4825 NULL,
4826 /*include_nondeduced_p=*/false);
4827 }
4828 for (i = 0; i < ntparms; ++i)
4829 if (tpd.parms[i] == 0)
4830 {
4831 /* One of the template parms was not used in a deduced context in the
4832 specialization. */
4833 if (!did_error_intro)
4834 {
4835 error ("template parameters not deducible in "
4836 "partial specialization:");
4837 did_error_intro = true;
4838 }
4839
4840 inform (input_location, " %qD",
4841 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4842 }
4843
4844 if (did_error_intro)
4845 return error_mark_node;
4846
4847 /* [temp.class.spec]
4848
4849 The argument list of the specialization shall not be identical to
4850 the implicit argument list of the primary template. */
4851 tree main_args
4852 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4853 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
4854 && (!flag_concepts
4855 || !strictly_subsumes (current_template_constraints (),
4856 get_constraints (maintmpl))))
4857 {
4858 if (!flag_concepts)
4859 error ("partial specialization %q+D does not specialize "
4860 "any template arguments; to define the primary template, "
4861 "remove the template argument list", decl);
4862 else
4863 error ("partial specialization %q+D does not specialize any "
4864 "template arguments and is not more constrained than "
4865 "the primary template; to define the primary template, "
4866 "remove the template argument list", decl);
4867 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4868 }
4869
4870 /* A partial specialization that replaces multiple parameters of the
4871 primary template with a pack expansion is less specialized for those
4872 parameters. */
4873 if (nargs < DECL_NTPARMS (maintmpl))
4874 {
4875 error ("partial specialization is not more specialized than the "
4876 "primary template because it replaces multiple parameters "
4877 "with a pack expansion");
4878 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4879 /* Avoid crash in process_partial_specialization. */
4880 return decl;
4881 }
4882
4883 /* If we aren't in a dependent class, we can actually try deduction. */
4884 else if (tpd.level == 1
4885 /* FIXME we should be able to handle a partial specialization of a
4886 partial instantiation, but currently we can't (c++/41727). */
4887 && TMPL_ARGS_DEPTH (specargs) == 1
4888 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
4889 {
4890 auto_diagnostic_group d;
4891 if (permerror (input_location, "partial specialization %qD is not "
4892 "more specialized than", decl))
4893 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
4894 maintmpl);
4895 }
4896
4897 /* [temp.class.spec]
4898
4899 A partially specialized non-type argument expression shall not
4900 involve template parameters of the partial specialization except
4901 when the argument expression is a simple identifier.
4902
4903 The type of a template parameter corresponding to a specialized
4904 non-type argument shall not be dependent on a parameter of the
4905 specialization.
4906
4907 Also, we verify that pack expansions only occur at the
4908 end of the argument list. */
4909 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4910 tpd2.parms = 0;
4911 for (i = 0; i < nargs; ++i)
4912 {
4913 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4914 tree arg = TREE_VEC_ELT (inner_args, i);
4915 tree packed_args = NULL_TREE;
4916 int j, len = 1;
4917
4918 if (ARGUMENT_PACK_P (arg))
4919 {
4920 /* Extract the arguments from the argument pack. We'll be
4921 iterating over these in the following loop. */
4922 packed_args = ARGUMENT_PACK_ARGS (arg);
4923 len = TREE_VEC_LENGTH (packed_args);
4924 }
4925
4926 for (j = 0; j < len; j++)
4927 {
4928 if (packed_args)
4929 /* Get the Jth argument in the parameter pack. */
4930 arg = TREE_VEC_ELT (packed_args, j);
4931
4932 if (PACK_EXPANSION_P (arg))
4933 {
4934 /* Pack expansions must come at the end of the
4935 argument list. */
4936 if ((packed_args && j < len - 1)
4937 || (!packed_args && i < nargs - 1))
4938 {
4939 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4940 error ("parameter pack argument %qE must be at the "
4941 "end of the template argument list", arg);
4942 else
4943 error ("parameter pack argument %qT must be at the "
4944 "end of the template argument list", arg);
4945 }
4946 }
4947
4948 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4949 /* We only care about the pattern. */
4950 arg = PACK_EXPANSION_PATTERN (arg);
4951
4952 if (/* These first two lines are the `non-type' bit. */
4953 !TYPE_P (arg)
4954 && TREE_CODE (arg) != TEMPLATE_DECL
4955 /* This next two lines are the `argument expression is not just a
4956 simple identifier' condition and also the `specialized
4957 non-type argument' bit. */
4958 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4959 && !((REFERENCE_REF_P (arg)
4960 || TREE_CODE (arg) == VIEW_CONVERT_EXPR)
4961 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4962 {
4963 if ((!packed_args && tpd.arg_uses_template_parms[i])
4964 || (packed_args && uses_template_parms (arg)))
4965 error_at (cp_expr_loc_or_input_loc (arg),
4966 "template argument %qE involves template "
4967 "parameter(s)", arg);
4968 else
4969 {
4970 /* Look at the corresponding template parameter,
4971 marking which template parameters its type depends
4972 upon. */
4973 tree type = TREE_TYPE (parm);
4974
4975 if (!tpd2.parms)
4976 {
4977 /* We haven't yet initialized TPD2. Do so now. */
4978 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4979 /* The number of parameters here is the number in the
4980 main template, which, as checked in the assertion
4981 above, is NARGS. */
4982 tpd2.parms = XALLOCAVEC (int, nargs);
4983 tpd2.level =
4984 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4985 }
4986
4987 /* Mark the template parameters. But this time, we're
4988 looking for the template parameters of the main
4989 template, not in the specialization. */
4990 tpd2.current_arg = i;
4991 tpd2.arg_uses_template_parms[i] = 0;
4992 memset (tpd2.parms, 0, sizeof (int) * nargs);
4993 for_each_template_parm (type,
4994 &mark_template_parm,
4995 &tpd2,
4996 NULL,
4997 /*include_nondeduced_p=*/false);
4998
4999 if (tpd2.arg_uses_template_parms [i])
5000 {
5001 /* The type depended on some template parameters.
5002 If they are fully specialized in the
5003 specialization, that's OK. */
5004 int j;
5005 int count = 0;
5006 for (j = 0; j < nargs; ++j)
5007 if (tpd2.parms[j] != 0
5008 && tpd.arg_uses_template_parms [j])
5009 ++count;
5010 if (count != 0)
5011 error_n (input_location, count,
5012 "type %qT of template argument %qE depends "
5013 "on a template parameter",
5014 "type %qT of template argument %qE depends "
5015 "on template parameters",
5016 type,
5017 arg);
5018 }
5019 }
5020 }
5021 }
5022 }
5023
5024 /* We should only get here once. */
5025 if (TREE_CODE (decl) == TYPE_DECL)
5026 gcc_assert (!COMPLETE_TYPE_P (type));
5027
5028 // Build the template decl.
5029 tree tmpl = build_template_decl (decl, current_template_parms,
5030 DECL_MEMBER_TEMPLATE_P (maintmpl));
5031 TREE_TYPE (tmpl) = type;
5032 DECL_TEMPLATE_RESULT (tmpl) = decl;
5033 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5034 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
5035 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
5036
5037 /* Give template template parms a DECL_CONTEXT of the template
5038 for which they are a parameter. */
5039 for (i = 0; i < ntparms; ++i)
5040 {
5041 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
5042 if (TREE_CODE (parm) == TEMPLATE_DECL)
5043 DECL_CONTEXT (parm) = tmpl;
5044 }
5045
5046 if (VAR_P (decl))
5047 /* We didn't register this in check_explicit_specialization so we could
5048 wait until the constraints were set. */
5049 decl = register_specialization (decl, maintmpl, specargs, false, 0);
5050 else
5051 associate_classtype_constraints (type);
5052
5053 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
5054 = tree_cons (specargs, tmpl,
5055 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
5056 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
5057
5058 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
5059 inst = TREE_CHAIN (inst))
5060 {
5061 tree instance = TREE_VALUE (inst);
5062 if (TYPE_P (instance)
5063 ? (COMPLETE_TYPE_P (instance)
5064 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
5065 : DECL_TEMPLATE_INSTANTIATION (instance))
5066 {
5067 tree spec = most_specialized_partial_spec (instance, tf_none);
5068 tree inst_decl = (DECL_P (instance)
5069 ? instance : TYPE_NAME (instance));
5070 if (!spec)
5071 /* OK */;
5072 else if (spec == error_mark_node)
5073 permerror (input_location,
5074 "declaration of %qD ambiguates earlier template "
5075 "instantiation for %qD", decl, inst_decl);
5076 else if (TREE_VALUE (spec) == tmpl)
5077 permerror (input_location,
5078 "partial specialization of %qD after instantiation "
5079 "of %qD", decl, inst_decl);
5080 }
5081 }
5082
5083 return decl;
5084 }
5085
5086 /* PARM is a template parameter of some form; return the corresponding
5087 TEMPLATE_PARM_INDEX. */
5088
5089 static tree
5090 get_template_parm_index (tree parm)
5091 {
5092 if (TREE_CODE (parm) == PARM_DECL
5093 || TREE_CODE (parm) == CONST_DECL)
5094 parm = DECL_INITIAL (parm);
5095 else if (TREE_CODE (parm) == TYPE_DECL
5096 || TREE_CODE (parm) == TEMPLATE_DECL)
5097 parm = TREE_TYPE (parm);
5098 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
5099 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
5100 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
5101 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
5102 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
5103 return parm;
5104 }
5105
5106 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5107 parameter packs used by the template parameter PARM. */
5108
5109 static void
5110 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
5111 {
5112 /* A type parm can't refer to another parm. */
5113 if (TREE_CODE (parm) == TYPE_DECL || parm == error_mark_node)
5114 return;
5115 else if (TREE_CODE (parm) == PARM_DECL)
5116 {
5117 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
5118 ppd, ppd->visited);
5119 return;
5120 }
5121
5122 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
5123
5124 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
5125 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
5126 {
5127 tree p = TREE_VALUE (TREE_VEC_ELT (vec, i));
5128 if (template_parameter_pack_p (p))
5129 /* Any packs in the type are expanded by this parameter. */;
5130 else
5131 fixed_parameter_pack_p_1 (p, ppd);
5132 }
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 && IDENTIFIER_ANON_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_at (DECL_SOURCE_LOCATION (decl),
5526 "destructor %qD declared as member template", decl);
5527 return error_mark_node;
5528 }
5529 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5530 && (!prototype_p (TREE_TYPE (decl))
5531 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5532 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5533 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5534 == void_list_node)))
5535 {
5536 /* [basic.stc.dynamic.allocation]
5537
5538 An allocation function can be a function
5539 template. ... Template allocation functions shall
5540 have two or more parameters. */
5541 error ("invalid template declaration of %qD", decl);
5542 return error_mark_node;
5543 }
5544 }
5545 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5546 && CLASS_TYPE_P (TREE_TYPE (decl)))
5547 {
5548 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5549 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5550 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5551 {
5552 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5553 if (TREE_CODE (t) == TYPE_DECL)
5554 t = TREE_TYPE (t);
5555 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5556 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5557 }
5558 }
5559 else if (TREE_CODE (decl) == TYPE_DECL
5560 && TYPE_DECL_ALIAS_P (decl))
5561 /* alias-declaration */
5562 gcc_assert (!DECL_ARTIFICIAL (decl));
5563 else if (VAR_P (decl))
5564 /* C++14 variable template. */;
5565 else
5566 {
5567 error ("template declaration of %q#D", decl);
5568 return error_mark_node;
5569 }
5570 }
5571
5572 /* Check to see that the rules regarding the use of default
5573 arguments are not being violated. We check args for a friend
5574 functions when we know whether it's a definition, introducing
5575 declaration or re-declaration. */
5576 if (!is_friend || TREE_CODE (decl) != FUNCTION_DECL)
5577 check_default_tmpl_args (decl, current_template_parms,
5578 is_primary, is_partial, is_friend);
5579
5580 /* Ensure that there are no parameter packs in the type of this
5581 declaration that have not been expanded. */
5582 if (TREE_CODE (decl) == FUNCTION_DECL)
5583 {
5584 /* Check each of the arguments individually to see if there are
5585 any bare parameter packs. */
5586 tree type = TREE_TYPE (decl);
5587 tree arg = DECL_ARGUMENTS (decl);
5588 tree argtype = TYPE_ARG_TYPES (type);
5589
5590 while (arg && argtype)
5591 {
5592 if (!DECL_PACK_P (arg)
5593 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5594 {
5595 /* This is a PARM_DECL that contains unexpanded parameter
5596 packs. We have already complained about this in the
5597 check_for_bare_parameter_packs call, so just replace
5598 these types with ERROR_MARK_NODE. */
5599 TREE_TYPE (arg) = error_mark_node;
5600 TREE_VALUE (argtype) = error_mark_node;
5601 }
5602
5603 arg = DECL_CHAIN (arg);
5604 argtype = TREE_CHAIN (argtype);
5605 }
5606
5607 /* Check for bare parameter packs in the return type and the
5608 exception specifiers. */
5609 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5610 /* Errors were already issued, set return type to int
5611 as the frontend doesn't expect error_mark_node as
5612 the return type. */
5613 TREE_TYPE (type) = integer_type_node;
5614 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5615 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5616 }
5617 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5618 && TYPE_DECL_ALIAS_P (decl))
5619 ? DECL_ORIGINAL_TYPE (decl)
5620 : TREE_TYPE (decl)))
5621 {
5622 TREE_TYPE (decl) = error_mark_node;
5623 return error_mark_node;
5624 }
5625
5626 if (is_partial)
5627 return process_partial_specialization (decl);
5628
5629 args = current_template_args ();
5630
5631 if (!ctx
5632 || TREE_CODE (ctx) == FUNCTION_DECL
5633 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5634 || (TREE_CODE (decl) == TYPE_DECL
5635 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5636 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5637 {
5638 if (DECL_LANG_SPECIFIC (decl)
5639 && DECL_TEMPLATE_INFO (decl)
5640 && DECL_TI_TEMPLATE (decl))
5641 tmpl = DECL_TI_TEMPLATE (decl);
5642 /* If DECL is a TYPE_DECL for a class-template, then there won't
5643 be DECL_LANG_SPECIFIC. The information equivalent to
5644 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5645 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5646 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5647 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5648 {
5649 /* Since a template declaration already existed for this
5650 class-type, we must be redeclaring it here. Make sure
5651 that the redeclaration is valid. */
5652 redeclare_class_template (TREE_TYPE (decl),
5653 current_template_parms,
5654 current_template_constraints ());
5655 /* We don't need to create a new TEMPLATE_DECL; just use the
5656 one we already had. */
5657 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5658 }
5659 else
5660 {
5661 tmpl = build_template_decl (decl, current_template_parms,
5662 member_template_p);
5663 new_template_p = 1;
5664
5665 if (DECL_LANG_SPECIFIC (decl)
5666 && DECL_TEMPLATE_SPECIALIZATION (decl))
5667 {
5668 /* A specialization of a member template of a template
5669 class. */
5670 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5671 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5672 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5673 }
5674 }
5675 }
5676 else
5677 {
5678 tree a, t, current, parms;
5679 int i;
5680 tree tinfo = get_template_info (decl);
5681
5682 if (!tinfo)
5683 {
5684 error ("template definition of non-template %q#D", decl);
5685 return error_mark_node;
5686 }
5687
5688 tmpl = TI_TEMPLATE (tinfo);
5689
5690 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5691 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5692 && DECL_TEMPLATE_SPECIALIZATION (decl)
5693 && DECL_MEMBER_TEMPLATE_P (tmpl))
5694 {
5695 tree new_tmpl;
5696
5697 /* The declaration is a specialization of a member
5698 template, declared outside the class. Therefore, the
5699 innermost template arguments will be NULL, so we
5700 replace them with the arguments determined by the
5701 earlier call to check_explicit_specialization. */
5702 args = DECL_TI_ARGS (decl);
5703
5704 new_tmpl
5705 = build_template_decl (decl, current_template_parms,
5706 member_template_p);
5707 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5708 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5709 DECL_TI_TEMPLATE (decl) = new_tmpl;
5710 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5711 DECL_TEMPLATE_INFO (new_tmpl)
5712 = build_template_info (tmpl, args);
5713
5714 register_specialization (new_tmpl,
5715 most_general_template (tmpl),
5716 args,
5717 is_friend, 0);
5718 return decl;
5719 }
5720
5721 /* Make sure the template headers we got make sense. */
5722
5723 parms = DECL_TEMPLATE_PARMS (tmpl);
5724 i = TMPL_PARMS_DEPTH (parms);
5725 if (TMPL_ARGS_DEPTH (args) != i)
5726 {
5727 error ("expected %d levels of template parms for %q#D, got %d",
5728 i, decl, TMPL_ARGS_DEPTH (args));
5729 DECL_INTERFACE_KNOWN (decl) = 1;
5730 return error_mark_node;
5731 }
5732 else
5733 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5734 {
5735 a = TMPL_ARGS_LEVEL (args, i);
5736 t = INNERMOST_TEMPLATE_PARMS (parms);
5737
5738 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5739 {
5740 if (current == decl)
5741 error ("got %d template parameters for %q#D",
5742 TREE_VEC_LENGTH (a), decl);
5743 else
5744 error ("got %d template parameters for %q#T",
5745 TREE_VEC_LENGTH (a), current);
5746 error (" but %d required", TREE_VEC_LENGTH (t));
5747 /* Avoid crash in import_export_decl. */
5748 DECL_INTERFACE_KNOWN (decl) = 1;
5749 return error_mark_node;
5750 }
5751
5752 if (current == decl)
5753 current = ctx;
5754 else if (current == NULL_TREE)
5755 /* Can happen in erroneous input. */
5756 break;
5757 else
5758 current = get_containing_scope (current);
5759 }
5760
5761 /* Check that the parms are used in the appropriate qualifying scopes
5762 in the declarator. */
5763 if (!comp_template_args
5764 (TI_ARGS (tinfo),
5765 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5766 {
5767 error ("template arguments to %qD do not match original "
5768 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
5769 if (!uses_template_parms (TI_ARGS (tinfo)))
5770 inform (input_location, "use %<template<>%> for"
5771 " an explicit specialization");
5772 /* Avoid crash in import_export_decl. */
5773 DECL_INTERFACE_KNOWN (decl) = 1;
5774 return error_mark_node;
5775 }
5776 }
5777
5778 DECL_TEMPLATE_RESULT (tmpl) = decl;
5779 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5780
5781 /* Push template declarations for global functions and types. Note
5782 that we do not try to push a global template friend declared in a
5783 template class; such a thing may well depend on the template
5784 parameters of the class. */
5785 if (new_template_p && !ctx
5786 && !(is_friend && template_class_depth (current_class_type) > 0))
5787 {
5788 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5789 if (tmpl == error_mark_node)
5790 return error_mark_node;
5791
5792 /* Hide template friend classes that haven't been declared yet. */
5793 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5794 {
5795 DECL_ANTICIPATED (tmpl) = 1;
5796 DECL_FRIEND_P (tmpl) = 1;
5797 }
5798 }
5799
5800 if (is_primary)
5801 {
5802 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5803
5804 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5805
5806 /* Give template template parms a DECL_CONTEXT of the template
5807 for which they are a parameter. */
5808 parms = INNERMOST_TEMPLATE_PARMS (parms);
5809 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5810 {
5811 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5812 if (TREE_CODE (parm) == TEMPLATE_DECL)
5813 DECL_CONTEXT (parm) = tmpl;
5814 }
5815
5816 if (TREE_CODE (decl) == TYPE_DECL
5817 && TYPE_DECL_ALIAS_P (decl)
5818 && complex_alias_template_p (tmpl))
5819 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5820 }
5821
5822 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5823 back to its most general template. If TMPL is a specialization,
5824 ARGS may only have the innermost set of arguments. Add the missing
5825 argument levels if necessary. */
5826 if (DECL_TEMPLATE_INFO (tmpl))
5827 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5828
5829 info = build_template_info (tmpl, args);
5830
5831 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5832 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5833 else
5834 {
5835 if (is_primary)
5836 retrofit_lang_decl (decl);
5837 if (DECL_LANG_SPECIFIC (decl))
5838 DECL_TEMPLATE_INFO (decl) = info;
5839 }
5840
5841 if (flag_implicit_templates
5842 && !is_friend
5843 && TREE_PUBLIC (decl)
5844 && VAR_OR_FUNCTION_DECL_P (decl))
5845 /* Set DECL_COMDAT on template instantiations; if we force
5846 them to be emitted by explicit instantiation,
5847 mark_needed will tell cgraph to do the right thing. */
5848 DECL_COMDAT (decl) = true;
5849
5850 return DECL_TEMPLATE_RESULT (tmpl);
5851 }
5852
5853 tree
5854 push_template_decl (tree decl)
5855 {
5856 return push_template_decl_real (decl, false);
5857 }
5858
5859 /* FN is an inheriting constructor that inherits from the constructor
5860 template INHERITED; turn FN into a constructor template with a matching
5861 template header. */
5862
5863 tree
5864 add_inherited_template_parms (tree fn, tree inherited)
5865 {
5866 tree inner_parms
5867 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5868 inner_parms = copy_node (inner_parms);
5869 tree parms
5870 = tree_cons (size_int (processing_template_decl + 1),
5871 inner_parms, current_template_parms);
5872 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5873 tree args = template_parms_to_args (parms);
5874 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5875 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5876 DECL_TEMPLATE_RESULT (tmpl) = fn;
5877 DECL_ARTIFICIAL (tmpl) = true;
5878 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5879 return tmpl;
5880 }
5881
5882 /* Called when a class template TYPE is redeclared with the indicated
5883 template PARMS, e.g.:
5884
5885 template <class T> struct S;
5886 template <class T> struct S {}; */
5887
5888 bool
5889 redeclare_class_template (tree type, tree parms, tree cons)
5890 {
5891 tree tmpl;
5892 tree tmpl_parms;
5893 int i;
5894
5895 if (!TYPE_TEMPLATE_INFO (type))
5896 {
5897 error ("%qT is not a template type", type);
5898 return false;
5899 }
5900
5901 tmpl = TYPE_TI_TEMPLATE (type);
5902 if (!PRIMARY_TEMPLATE_P (tmpl))
5903 /* The type is nested in some template class. Nothing to worry
5904 about here; there are no new template parameters for the nested
5905 type. */
5906 return true;
5907
5908 if (!parms)
5909 {
5910 error ("template specifiers not specified in declaration of %qD",
5911 tmpl);
5912 return false;
5913 }
5914
5915 parms = INNERMOST_TEMPLATE_PARMS (parms);
5916 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5917
5918 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5919 {
5920 error_n (input_location, TREE_VEC_LENGTH (parms),
5921 "redeclared with %d template parameter",
5922 "redeclared with %d template parameters",
5923 TREE_VEC_LENGTH (parms));
5924 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5925 "previous declaration %qD used %d template parameter",
5926 "previous declaration %qD used %d template parameters",
5927 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5928 return false;
5929 }
5930
5931 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5932 {
5933 tree tmpl_parm;
5934 tree parm;
5935 tree tmpl_default;
5936 tree parm_default;
5937
5938 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5939 || TREE_VEC_ELT (parms, i) == error_mark_node)
5940 continue;
5941
5942 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5943 if (error_operand_p (tmpl_parm))
5944 return false;
5945
5946 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5947 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5948 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5949
5950 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5951 TEMPLATE_DECL. */
5952 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5953 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5954 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5955 || (TREE_CODE (tmpl_parm) != PARM_DECL
5956 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5957 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5958 || (TREE_CODE (tmpl_parm) == PARM_DECL
5959 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5960 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5961 {
5962 error ("template parameter %q+#D", tmpl_parm);
5963 error ("redeclared here as %q#D", parm);
5964 return false;
5965 }
5966
5967 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5968 {
5969 /* We have in [temp.param]:
5970
5971 A template-parameter may not be given default arguments
5972 by two different declarations in the same scope. */
5973 error_at (input_location, "redefinition of default argument for %q#D", parm);
5974 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5975 "original definition appeared here");
5976 return false;
5977 }
5978
5979 if (parm_default != NULL_TREE)
5980 /* Update the previous template parameters (which are the ones
5981 that will really count) with the new default value. */
5982 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5983 else if (tmpl_default != NULL_TREE)
5984 /* Update the new parameters, too; they'll be used as the
5985 parameters for any members. */
5986 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5987
5988 /* Give each template template parm in this redeclaration a
5989 DECL_CONTEXT of the template for which they are a parameter. */
5990 if (TREE_CODE (parm) == TEMPLATE_DECL)
5991 {
5992 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5993 DECL_CONTEXT (parm) = tmpl;
5994 }
5995
5996 if (TREE_CODE (parm) == TYPE_DECL)
5997 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
5998 }
5999
6000 // Cannot redeclare a class template with a different set of constraints.
6001 if (!equivalent_constraints (get_constraints (tmpl), cons))
6002 {
6003 error_at (input_location, "redeclaration %q#D with different "
6004 "constraints", tmpl);
6005 inform (DECL_SOURCE_LOCATION (tmpl),
6006 "original declaration appeared here");
6007 }
6008
6009 return true;
6010 }
6011
6012 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
6013 to be used when the caller has already checked
6014 (processing_template_decl
6015 && !instantiation_dependent_expression_p (expr)
6016 && potential_constant_expression (expr))
6017 and cleared processing_template_decl. */
6018
6019 tree
6020 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
6021 {
6022 return tsubst_copy_and_build (expr,
6023 /*args=*/NULL_TREE,
6024 complain,
6025 /*in_decl=*/NULL_TREE,
6026 /*function_p=*/false,
6027 /*integral_constant_expression_p=*/true);
6028 }
6029
6030 /* Simplify EXPR if it is a non-dependent expression. Returns the
6031 (possibly simplified) expression. */
6032
6033 tree
6034 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
6035 {
6036 if (expr == NULL_TREE)
6037 return NULL_TREE;
6038
6039 /* If we're in a template, but EXPR isn't value dependent, simplify
6040 it. We're supposed to treat:
6041
6042 template <typename T> void f(T[1 + 1]);
6043 template <typename T> void f(T[2]);
6044
6045 as two declarations of the same function, for example. */
6046 if (processing_template_decl
6047 && is_nondependent_constant_expression (expr))
6048 {
6049 processing_template_decl_sentinel s;
6050 expr = instantiate_non_dependent_expr_internal (expr, complain);
6051 }
6052 return expr;
6053 }
6054
6055 tree
6056 instantiate_non_dependent_expr (tree expr)
6057 {
6058 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
6059 }
6060
6061 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6062 an uninstantiated expression. */
6063
6064 tree
6065 instantiate_non_dependent_or_null (tree expr)
6066 {
6067 if (expr == NULL_TREE)
6068 return NULL_TREE;
6069 if (processing_template_decl)
6070 {
6071 if (!is_nondependent_constant_expression (expr))
6072 expr = NULL_TREE;
6073 else
6074 {
6075 processing_template_decl_sentinel s;
6076 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
6077 }
6078 }
6079 return expr;
6080 }
6081
6082 /* True iff T is a specialization of a variable template. */
6083
6084 bool
6085 variable_template_specialization_p (tree t)
6086 {
6087 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
6088 return false;
6089 tree tmpl = DECL_TI_TEMPLATE (t);
6090 return variable_template_p (tmpl);
6091 }
6092
6093 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6094 template declaration, or a TYPE_DECL for an alias declaration. */
6095
6096 bool
6097 alias_type_or_template_p (tree t)
6098 {
6099 if (t == NULL_TREE)
6100 return false;
6101 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
6102 || (TYPE_P (t)
6103 && TYPE_NAME (t)
6104 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
6105 || DECL_ALIAS_TEMPLATE_P (t));
6106 }
6107
6108 /* Return TRUE iff T is a specialization of an alias template. */
6109
6110 bool
6111 alias_template_specialization_p (const_tree t)
6112 {
6113 /* It's an alias template specialization if it's an alias and its
6114 TYPE_NAME is a specialization of a primary template. */
6115 if (TYPE_ALIAS_P (t))
6116 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
6117 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo));
6118
6119 return false;
6120 }
6121
6122 /* An alias template is complex from a SFINAE perspective if a template-id
6123 using that alias can be ill-formed when the expansion is not, as with
6124 the void_t template. We determine this by checking whether the
6125 expansion for the alias template uses all its template parameters. */
6126
6127 struct uses_all_template_parms_data
6128 {
6129 int level;
6130 bool *seen;
6131 };
6132
6133 static int
6134 uses_all_template_parms_r (tree t, void *data_)
6135 {
6136 struct uses_all_template_parms_data &data
6137 = *(struct uses_all_template_parms_data*)data_;
6138 tree idx = get_template_parm_index (t);
6139
6140 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
6141 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
6142 return 0;
6143 }
6144
6145 static bool
6146 complex_alias_template_p (const_tree tmpl)
6147 {
6148 struct uses_all_template_parms_data data;
6149 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6150 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6151 data.level = TMPL_PARMS_DEPTH (parms);
6152 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
6153 data.seen = XALLOCAVEC (bool, len);
6154 for (int i = 0; i < len; ++i)
6155 data.seen[i] = false;
6156
6157 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
6158 for (int i = 0; i < len; ++i)
6159 if (!data.seen[i])
6160 return true;
6161 return false;
6162 }
6163
6164 /* Return TRUE iff T is a specialization of a complex alias template with
6165 dependent template-arguments. */
6166
6167 bool
6168 dependent_alias_template_spec_p (const_tree t)
6169 {
6170 if (!alias_template_specialization_p (t))
6171 return false;
6172
6173 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
6174 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo)))
6175 return false;
6176
6177 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
6178 if (!any_dependent_template_arguments_p (args))
6179 return false;
6180
6181 return true;
6182 }
6183
6184 /* Return the number of innermost template parameters in TMPL. */
6185
6186 static int
6187 num_innermost_template_parms (tree tmpl)
6188 {
6189 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6190 return TREE_VEC_LENGTH (parms);
6191 }
6192
6193 /* Return either TMPL or another template that it is equivalent to under DR
6194 1286: An alias that just changes the name of a template is equivalent to
6195 the other template. */
6196
6197 static tree
6198 get_underlying_template (tree tmpl)
6199 {
6200 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6201 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6202 {
6203 /* Determine if the alias is equivalent to an underlying template. */
6204 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6205 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6206 if (!tinfo)
6207 break;
6208
6209 tree underlying = TI_TEMPLATE (tinfo);
6210 if (!PRIMARY_TEMPLATE_P (underlying)
6211 || (num_innermost_template_parms (tmpl)
6212 != num_innermost_template_parms (underlying)))
6213 break;
6214
6215 tree alias_args = INNERMOST_TEMPLATE_ARGS
6216 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
6217 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6218 break;
6219
6220 /* Alias is equivalent. Strip it and repeat. */
6221 tmpl = underlying;
6222 }
6223
6224 return tmpl;
6225 }
6226
6227 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6228 must be a reference-to-function or a pointer-to-function type, as specified
6229 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6230 and check that the resulting function has external linkage. */
6231
6232 static tree
6233 convert_nontype_argument_function (tree type, tree expr,
6234 tsubst_flags_t complain)
6235 {
6236 tree fns = expr;
6237 tree fn, fn_no_ptr;
6238 linkage_kind linkage;
6239
6240 fn = instantiate_type (type, fns, tf_none);
6241 if (fn == error_mark_node)
6242 return error_mark_node;
6243
6244 if (value_dependent_expression_p (fn))
6245 goto accept;
6246
6247 fn_no_ptr = strip_fnptr_conv (fn);
6248 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6249 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6250 if (BASELINK_P (fn_no_ptr))
6251 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6252
6253 /* [temp.arg.nontype]/1
6254
6255 A template-argument for a non-type, non-template template-parameter
6256 shall be one of:
6257 [...]
6258 -- the address of an object or function with external [C++11: or
6259 internal] linkage. */
6260
6261 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr);
6262 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6263 {
6264 if (complain & tf_error)
6265 {
6266 location_t loc = cp_expr_loc_or_input_loc (expr);
6267 error_at (loc, "%qE is not a valid template argument for type %qT",
6268 expr, type);
6269 if (TYPE_PTR_P (type))
6270 inform (loc, "it must be the address of a function "
6271 "with external linkage");
6272 else
6273 inform (loc, "it must be the name of a function with "
6274 "external linkage");
6275 }
6276 return NULL_TREE;
6277 }
6278
6279 linkage = decl_linkage (fn_no_ptr);
6280 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6281 {
6282 if (complain & tf_error)
6283 {
6284 location_t loc = cp_expr_loc_or_input_loc (expr);
6285 if (cxx_dialect >= cxx11)
6286 error_at (loc, "%qE is not a valid template argument for type "
6287 "%qT because %qD has no linkage",
6288 expr, type, fn_no_ptr);
6289 else
6290 error_at (loc, "%qE is not a valid template argument for type "
6291 "%qT because %qD does not have external linkage",
6292 expr, type, fn_no_ptr);
6293 }
6294 return NULL_TREE;
6295 }
6296
6297 accept:
6298 if (TYPE_REF_P (type))
6299 {
6300 if (REFERENCE_REF_P (fn))
6301 fn = TREE_OPERAND (fn, 0);
6302 else
6303 fn = build_address (fn);
6304 }
6305 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6306 fn = build_nop (type, fn);
6307
6308 return fn;
6309 }
6310
6311 /* Subroutine of convert_nontype_argument.
6312 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6313 Emit an error otherwise. */
6314
6315 static bool
6316 check_valid_ptrmem_cst_expr (tree type, tree expr,
6317 tsubst_flags_t complain)
6318 {
6319 tree orig_expr = expr;
6320 STRIP_NOPS (expr);
6321 if (null_ptr_cst_p (expr))
6322 return true;
6323 if (TREE_CODE (expr) == PTRMEM_CST
6324 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6325 PTRMEM_CST_CLASS (expr)))
6326 return true;
6327 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6328 return true;
6329 if (processing_template_decl
6330 && TREE_CODE (expr) == ADDR_EXPR
6331 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6332 return true;
6333 if (complain & tf_error)
6334 {
6335 location_t loc = cp_expr_loc_or_input_loc (orig_expr);
6336 error_at (loc, "%qE is not a valid template argument for type %qT",
6337 orig_expr, type);
6338 if (TREE_CODE (expr) != PTRMEM_CST)
6339 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6340 else
6341 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6342 }
6343 return false;
6344 }
6345
6346 /* Returns TRUE iff the address of OP is value-dependent.
6347
6348 14.6.2.4 [temp.dep.temp]:
6349 A non-integral non-type template-argument is dependent if its type is
6350 dependent or it has either of the following forms
6351 qualified-id
6352 & qualified-id
6353 and contains a nested-name-specifier which specifies a class-name that
6354 names a dependent type.
6355
6356 We generalize this to just say that the address of a member of a
6357 dependent class is value-dependent; the above doesn't cover the
6358 address of a static data member named with an unqualified-id. */
6359
6360 static bool
6361 has_value_dependent_address (tree op)
6362 {
6363 /* We could use get_inner_reference here, but there's no need;
6364 this is only relevant for template non-type arguments, which
6365 can only be expressed as &id-expression. */
6366 if (DECL_P (op))
6367 {
6368 tree ctx = CP_DECL_CONTEXT (op);
6369 if (TYPE_P (ctx) && dependent_type_p (ctx))
6370 return true;
6371 }
6372
6373 return false;
6374 }
6375
6376 /* The next set of functions are used for providing helpful explanatory
6377 diagnostics for failed overload resolution. Their messages should be
6378 indented by two spaces for consistency with the messages in
6379 call.c */
6380
6381 static int
6382 unify_success (bool /*explain_p*/)
6383 {
6384 return 0;
6385 }
6386
6387 /* Other failure functions should call this one, to provide a single function
6388 for setting a breakpoint on. */
6389
6390 static int
6391 unify_invalid (bool /*explain_p*/)
6392 {
6393 return 1;
6394 }
6395
6396 static int
6397 unify_parameter_deduction_failure (bool explain_p, tree parm)
6398 {
6399 if (explain_p)
6400 inform (input_location,
6401 " couldn%'t deduce template parameter %qD", parm);
6402 return unify_invalid (explain_p);
6403 }
6404
6405 static int
6406 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6407 {
6408 if (explain_p)
6409 inform (input_location,
6410 " types %qT and %qT have incompatible cv-qualifiers",
6411 parm, arg);
6412 return unify_invalid (explain_p);
6413 }
6414
6415 static int
6416 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6417 {
6418 if (explain_p)
6419 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6420 return unify_invalid (explain_p);
6421 }
6422
6423 static int
6424 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6425 {
6426 if (explain_p)
6427 inform (input_location,
6428 " template parameter %qD is not a parameter pack, but "
6429 "argument %qD is",
6430 parm, arg);
6431 return unify_invalid (explain_p);
6432 }
6433
6434 static int
6435 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6436 {
6437 if (explain_p)
6438 inform (input_location,
6439 " template argument %qE does not match "
6440 "pointer-to-member constant %qE",
6441 arg, parm);
6442 return unify_invalid (explain_p);
6443 }
6444
6445 static int
6446 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6447 {
6448 if (explain_p)
6449 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6450 return unify_invalid (explain_p);
6451 }
6452
6453 static int
6454 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6455 {
6456 if (explain_p)
6457 inform (input_location,
6458 " inconsistent parameter pack deduction with %qT and %qT",
6459 old_arg, new_arg);
6460 return unify_invalid (explain_p);
6461 }
6462
6463 static int
6464 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6465 {
6466 if (explain_p)
6467 {
6468 if (TYPE_P (parm))
6469 inform (input_location,
6470 " deduced conflicting types for parameter %qT (%qT and %qT)",
6471 parm, first, second);
6472 else
6473 inform (input_location,
6474 " deduced conflicting values for non-type parameter "
6475 "%qE (%qE and %qE)", parm, first, second);
6476 }
6477 return unify_invalid (explain_p);
6478 }
6479
6480 static int
6481 unify_vla_arg (bool explain_p, tree arg)
6482 {
6483 if (explain_p)
6484 inform (input_location,
6485 " variable-sized array type %qT is not "
6486 "a valid template argument",
6487 arg);
6488 return unify_invalid (explain_p);
6489 }
6490
6491 static int
6492 unify_method_type_error (bool explain_p, tree arg)
6493 {
6494 if (explain_p)
6495 inform (input_location,
6496 " member function type %qT is not a valid template argument",
6497 arg);
6498 return unify_invalid (explain_p);
6499 }
6500
6501 static int
6502 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6503 {
6504 if (explain_p)
6505 {
6506 if (least_p)
6507 inform_n (input_location, wanted,
6508 " candidate expects at least %d argument, %d provided",
6509 " candidate expects at least %d arguments, %d provided",
6510 wanted, have);
6511 else
6512 inform_n (input_location, wanted,
6513 " candidate expects %d argument, %d provided",
6514 " candidate expects %d arguments, %d provided",
6515 wanted, have);
6516 }
6517 return unify_invalid (explain_p);
6518 }
6519
6520 static int
6521 unify_too_many_arguments (bool explain_p, int have, int wanted)
6522 {
6523 return unify_arity (explain_p, have, wanted);
6524 }
6525
6526 static int
6527 unify_too_few_arguments (bool explain_p, int have, int wanted,
6528 bool least_p = false)
6529 {
6530 return unify_arity (explain_p, have, wanted, least_p);
6531 }
6532
6533 static int
6534 unify_arg_conversion (bool explain_p, tree to_type,
6535 tree from_type, tree arg)
6536 {
6537 if (explain_p)
6538 inform (cp_expr_loc_or_input_loc (arg),
6539 " cannot convert %qE (type %qT) to type %qT",
6540 arg, from_type, to_type);
6541 return unify_invalid (explain_p);
6542 }
6543
6544 static int
6545 unify_no_common_base (bool explain_p, enum template_base_result r,
6546 tree parm, tree arg)
6547 {
6548 if (explain_p)
6549 switch (r)
6550 {
6551 case tbr_ambiguous_baseclass:
6552 inform (input_location, " %qT is an ambiguous base class of %qT",
6553 parm, arg);
6554 break;
6555 default:
6556 inform (input_location, " %qT is not derived from %qT", arg, parm);
6557 break;
6558 }
6559 return unify_invalid (explain_p);
6560 }
6561
6562 static int
6563 unify_inconsistent_template_template_parameters (bool explain_p)
6564 {
6565 if (explain_p)
6566 inform (input_location,
6567 " template parameters of a template template argument are "
6568 "inconsistent with other deduced template arguments");
6569 return unify_invalid (explain_p);
6570 }
6571
6572 static int
6573 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6574 {
6575 if (explain_p)
6576 inform (input_location,
6577 " cannot deduce a template for %qT from non-template type %qT",
6578 parm, arg);
6579 return unify_invalid (explain_p);
6580 }
6581
6582 static int
6583 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6584 {
6585 if (explain_p)
6586 inform (input_location,
6587 " template argument %qE does not match %qE", arg, parm);
6588 return unify_invalid (explain_p);
6589 }
6590
6591 /* True if T is a C++20 template parameter object to store the argument for a
6592 template parameter of class type. */
6593
6594 bool
6595 template_parm_object_p (const_tree t)
6596 {
6597 return (TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t) && DECL_NAME (t)
6598 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (t)), "_ZTA", 4));
6599 }
6600
6601 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
6602 argument for TYPE, points to an unsuitable object. */
6603
6604 static bool
6605 invalid_tparm_referent_p (tree type, tree expr, tsubst_flags_t complain)
6606 {
6607 switch (TREE_CODE (expr))
6608 {
6609 CASE_CONVERT:
6610 return invalid_tparm_referent_p (type, TREE_OPERAND (expr, 0),
6611 complain);
6612
6613 case TARGET_EXPR:
6614 return invalid_tparm_referent_p (type, TARGET_EXPR_INITIAL (expr),
6615 complain);
6616
6617 case CONSTRUCTOR:
6618 {
6619 unsigned i; tree elt;
6620 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), i, elt)
6621 if (invalid_tparm_referent_p (TREE_TYPE (elt), elt, complain))
6622 return true;
6623 }
6624 break;
6625
6626 case ADDR_EXPR:
6627 {
6628 tree decl = TREE_OPERAND (expr, 0);
6629
6630 if (!VAR_P (decl))
6631 {
6632 if (complain & tf_error)
6633 error_at (cp_expr_loc_or_input_loc (expr),
6634 "%qE is not a valid template argument of type %qT "
6635 "because %qE is not a variable", expr, type, decl);
6636 return true;
6637 }
6638 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6639 {
6640 if (complain & tf_error)
6641 error_at (cp_expr_loc_or_input_loc (expr),
6642 "%qE is not a valid template argument of type %qT "
6643 "in C++98 because %qD does not have external linkage",
6644 expr, type, decl);
6645 return true;
6646 }
6647 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
6648 && decl_linkage (decl) == lk_none)
6649 {
6650 if (complain & tf_error)
6651 error_at (cp_expr_loc_or_input_loc (expr),
6652 "%qE is not a valid template argument of type %qT "
6653 "because %qD has no linkage", expr, type, decl);
6654 return true;
6655 }
6656 /* C++17: For a non-type template-parameter of reference or pointer
6657 type, the value of the constant expression shall not refer to (or
6658 for a pointer type, shall not be the address of):
6659 * a subobject (4.5),
6660 * a temporary object (15.2),
6661 * a string literal (5.13.5),
6662 * the result of a typeid expression (8.2.8), or
6663 * a predefined __func__ variable (11.4.1). */
6664 else if (DECL_ARTIFICIAL (decl))
6665 {
6666 if (complain & tf_error)
6667 error ("the address of %qD is not a valid template argument",
6668 decl);
6669 return true;
6670 }
6671 else if (!same_type_ignoring_top_level_qualifiers_p
6672 (strip_array_types (TREE_TYPE (type)),
6673 strip_array_types (TREE_TYPE (decl))))
6674 {
6675 if (complain & tf_error)
6676 error ("the address of the %qT subobject of %qD is not a "
6677 "valid template argument", TREE_TYPE (type), decl);
6678 return true;
6679 }
6680 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
6681 {
6682 if (complain & tf_error)
6683 error ("the address of %qD is not a valid template argument "
6684 "because it does not have static storage duration",
6685 decl);
6686 return true;
6687 }
6688 }
6689 break;
6690
6691 default:
6692 if (!INDIRECT_TYPE_P (type))
6693 /* We're only concerned about pointers and references here. */;
6694 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6695 /* Null pointer values are OK in C++11. */;
6696 else
6697 {
6698 if (VAR_P (expr))
6699 {
6700 if (complain & tf_error)
6701 error ("%qD is not a valid template argument "
6702 "because %qD is a variable, not the address of "
6703 "a variable", expr, expr);
6704 return true;
6705 }
6706 else
6707 {
6708 if (complain & tf_error)
6709 error ("%qE is not a valid template argument for %qT "
6710 "because it is not the address of a variable",
6711 expr, type);
6712 return true;
6713 }
6714 }
6715 }
6716 return false;
6717
6718 }
6719
6720 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
6721 template argument EXPR. */
6722
6723 static tree
6724 get_template_parm_object (tree expr, tsubst_flags_t complain)
6725 {
6726 if (TREE_CODE (expr) == TARGET_EXPR)
6727 expr = TARGET_EXPR_INITIAL (expr);
6728
6729 if (!TREE_CONSTANT (expr))
6730 {
6731 if ((complain & tf_error)
6732 && require_rvalue_constant_expression (expr))
6733 cxx_constant_value (expr);
6734 return error_mark_node;
6735 }
6736 if (invalid_tparm_referent_p (TREE_TYPE (expr), expr, complain))
6737 return error_mark_node;
6738
6739 tree name = mangle_template_parm_object (expr);
6740 tree decl = get_global_binding (name);
6741 if (decl)
6742 return decl;
6743
6744 tree type = cp_build_qualified_type (TREE_TYPE (expr), TYPE_QUAL_CONST);
6745 decl = create_temporary_var (type);
6746 TREE_STATIC (decl) = true;
6747 DECL_DECLARED_CONSTEXPR_P (decl) = true;
6748 TREE_READONLY (decl) = true;
6749 DECL_NAME (decl) = name;
6750 SET_DECL_ASSEMBLER_NAME (decl, name);
6751 DECL_CONTEXT (decl) = global_namespace;
6752 comdat_linkage (decl);
6753 pushdecl_top_level_and_finish (decl, expr);
6754 return decl;
6755 }
6756
6757 /* Attempt to convert the non-type template parameter EXPR to the
6758 indicated TYPE. If the conversion is successful, return the
6759 converted value. If the conversion is unsuccessful, return
6760 NULL_TREE if we issued an error message, or error_mark_node if we
6761 did not. We issue error messages for out-and-out bad template
6762 parameters, but not simply because the conversion failed, since we
6763 might be just trying to do argument deduction. Both TYPE and EXPR
6764 must be non-dependent.
6765
6766 The conversion follows the special rules described in
6767 [temp.arg.nontype], and it is much more strict than an implicit
6768 conversion.
6769
6770 This function is called twice for each template argument (see
6771 lookup_template_class for a more accurate description of this
6772 problem). This means that we need to handle expressions which
6773 are not valid in a C++ source, but can be created from the
6774 first call (for instance, casts to perform conversions). These
6775 hacks can go away after we fix the double coercion problem. */
6776
6777 static tree
6778 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6779 {
6780 tree expr_type;
6781 location_t loc = cp_expr_loc_or_input_loc (expr);
6782
6783 /* Detect immediately string literals as invalid non-type argument.
6784 This special-case is not needed for correctness (we would easily
6785 catch this later), but only to provide better diagnostic for this
6786 common user mistake. As suggested by DR 100, we do not mention
6787 linkage issues in the diagnostic as this is not the point. */
6788 if (TREE_CODE (expr) == STRING_CST && !CLASS_TYPE_P (type))
6789 {
6790 if (complain & tf_error)
6791 error ("%qE is not a valid template argument for type %qT "
6792 "because string literals can never be used in this context",
6793 expr, type);
6794 return NULL_TREE;
6795 }
6796
6797 /* Add the ADDR_EXPR now for the benefit of
6798 value_dependent_expression_p. */
6799 if (TYPE_PTROBV_P (type)
6800 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6801 {
6802 expr = decay_conversion (expr, complain);
6803 if (expr == error_mark_node)
6804 return error_mark_node;
6805 }
6806
6807 /* If we are in a template, EXPR may be non-dependent, but still
6808 have a syntactic, rather than semantic, form. For example, EXPR
6809 might be a SCOPE_REF, rather than the VAR_DECL to which the
6810 SCOPE_REF refers. Preserving the qualifying scope is necessary
6811 so that access checking can be performed when the template is
6812 instantiated -- but here we need the resolved form so that we can
6813 convert the argument. */
6814 bool non_dep = false;
6815 if (TYPE_REF_OBJ_P (type)
6816 && has_value_dependent_address (expr))
6817 /* If we want the address and it's value-dependent, don't fold. */;
6818 else if (processing_template_decl
6819 && is_nondependent_constant_expression (expr))
6820 non_dep = true;
6821 if (error_operand_p (expr))
6822 return error_mark_node;
6823 expr_type = TREE_TYPE (expr);
6824
6825 /* If the argument is non-dependent, perform any conversions in
6826 non-dependent context as well. */
6827 processing_template_decl_sentinel s (non_dep);
6828 if (non_dep)
6829 expr = instantiate_non_dependent_expr_internal (expr, complain);
6830
6831 if (value_dependent_expression_p (expr))
6832 expr = canonicalize_expr_argument (expr, complain);
6833
6834 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6835 to a non-type argument of "nullptr". */
6836 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
6837 expr = fold_simple (convert (type, expr));
6838
6839 /* In C++11, integral or enumeration non-type template arguments can be
6840 arbitrary constant expressions. Pointer and pointer to
6841 member arguments can be general constant expressions that evaluate
6842 to a null value, but otherwise still need to be of a specific form. */
6843 if (cxx_dialect >= cxx11)
6844 {
6845 if (TREE_CODE (expr) == PTRMEM_CST && TYPE_PTRMEM_P (type))
6846 /* A PTRMEM_CST is already constant, and a valid template
6847 argument for a parameter of pointer to member type, we just want
6848 to leave it in that form rather than lower it to a
6849 CONSTRUCTOR. */;
6850 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6851 || cxx_dialect >= cxx17)
6852 {
6853 /* Calling build_converted_constant_expr might create a call to
6854 a conversion function with a value-dependent argument, which
6855 could invoke taking the address of a temporary representing
6856 the result of the conversion. */
6857 if (COMPOUND_LITERAL_P (expr)
6858 && CONSTRUCTOR_IS_DEPENDENT (expr)
6859 && MAYBE_CLASS_TYPE_P (expr_type)
6860 && TYPE_HAS_CONVERSION (expr_type))
6861 {
6862 expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
6863 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr) = true;
6864 return expr;
6865 }
6866 /* C++17: A template-argument for a non-type template-parameter shall
6867 be a converted constant expression (8.20) of the type of the
6868 template-parameter. */
6869 expr = build_converted_constant_expr (type, expr, complain);
6870 if (expr == error_mark_node)
6871 /* Make sure we return NULL_TREE only if we have really issued
6872 an error, as described above. */
6873 return (complain & tf_error) ? NULL_TREE : error_mark_node;
6874 expr = maybe_constant_value (expr, NULL_TREE,
6875 /*manifestly_const_eval=*/true);
6876 expr = convert_from_reference (expr);
6877 }
6878 else if (TYPE_PTR_OR_PTRMEM_P (type))
6879 {
6880 tree folded = maybe_constant_value (expr, NULL_TREE,
6881 /*manifestly_const_eval=*/true);
6882 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6883 : null_member_pointer_value_p (folded))
6884 expr = folded;
6885 }
6886 }
6887
6888 if (TYPE_REF_P (type))
6889 expr = mark_lvalue_use (expr);
6890 else
6891 expr = mark_rvalue_use (expr);
6892
6893 /* HACK: Due to double coercion, we can get a
6894 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6895 which is the tree that we built on the first call (see
6896 below when coercing to reference to object or to reference to
6897 function). We just strip everything and get to the arg.
6898 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6899 for examples. */
6900 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6901 {
6902 tree probe_type, probe = expr;
6903 if (REFERENCE_REF_P (probe))
6904 probe = TREE_OPERAND (probe, 0);
6905 probe_type = TREE_TYPE (probe);
6906 if (TREE_CODE (probe) == NOP_EXPR)
6907 {
6908 /* ??? Maybe we could use convert_from_reference here, but we
6909 would need to relax its constraints because the NOP_EXPR
6910 could actually change the type to something more cv-qualified,
6911 and this is not folded by convert_from_reference. */
6912 tree addr = TREE_OPERAND (probe, 0);
6913 if (TYPE_REF_P (probe_type)
6914 && TREE_CODE (addr) == ADDR_EXPR
6915 && TYPE_PTR_P (TREE_TYPE (addr))
6916 && (same_type_ignoring_top_level_qualifiers_p
6917 (TREE_TYPE (probe_type),
6918 TREE_TYPE (TREE_TYPE (addr)))))
6919 {
6920 expr = TREE_OPERAND (addr, 0);
6921 expr_type = TREE_TYPE (probe_type);
6922 }
6923 }
6924 }
6925
6926 /* [temp.arg.nontype]/5, bullet 1
6927
6928 For a non-type template-parameter of integral or enumeration type,
6929 integral promotions (_conv.prom_) and integral conversions
6930 (_conv.integral_) are applied. */
6931 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6932 {
6933 if (cxx_dialect < cxx11)
6934 {
6935 tree t = build_converted_constant_expr (type, expr, complain);
6936 t = maybe_constant_value (t);
6937 if (t != error_mark_node)
6938 expr = t;
6939 }
6940
6941 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6942 return error_mark_node;
6943
6944 /* Notice that there are constant expressions like '4 % 0' which
6945 do not fold into integer constants. */
6946 if (TREE_CODE (expr) != INTEGER_CST
6947 && !value_dependent_expression_p (expr))
6948 {
6949 if (complain & tf_error)
6950 {
6951 int errs = errorcount, warns = warningcount + werrorcount;
6952 if (!require_potential_constant_expression (expr))
6953 expr = error_mark_node;
6954 else
6955 expr = cxx_constant_value (expr);
6956 if (errorcount > errs || warningcount + werrorcount > warns)
6957 inform (loc, "in template argument for type %qT", type);
6958 if (expr == error_mark_node)
6959 return NULL_TREE;
6960 /* else cxx_constant_value complained but gave us
6961 a real constant, so go ahead. */
6962 if (TREE_CODE (expr) != INTEGER_CST)
6963 {
6964 /* Some assemble time constant expressions like
6965 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
6966 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
6967 as we can emit them into .rodata initializers of
6968 variables, yet they can't fold into an INTEGER_CST at
6969 compile time. Refuse them here. */
6970 gcc_checking_assert (reduced_constant_expression_p (expr));
6971 error_at (loc, "template argument %qE for type %qT not "
6972 "a constant integer", expr, type);
6973 return NULL_TREE;
6974 }
6975 }
6976 else
6977 return NULL_TREE;
6978 }
6979
6980 /* Avoid typedef problems. */
6981 if (TREE_TYPE (expr) != type)
6982 expr = fold_convert (type, expr);
6983 }
6984 /* [temp.arg.nontype]/5, bullet 2
6985
6986 For a non-type template-parameter of type pointer to object,
6987 qualification conversions (_conv.qual_) and the array-to-pointer
6988 conversion (_conv.array_) are applied. */
6989 else if (TYPE_PTROBV_P (type))
6990 {
6991 tree decayed = expr;
6992
6993 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6994 decay_conversion or an explicit cast. If it's a problematic cast,
6995 we'll complain about it below. */
6996 if (TREE_CODE (expr) == NOP_EXPR)
6997 {
6998 tree probe = expr;
6999 STRIP_NOPS (probe);
7000 if (TREE_CODE (probe) == ADDR_EXPR
7001 && TYPE_PTR_P (TREE_TYPE (probe)))
7002 {
7003 expr = probe;
7004 expr_type = TREE_TYPE (expr);
7005 }
7006 }
7007
7008 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
7009
7010 A template-argument for a non-type, non-template template-parameter
7011 shall be one of: [...]
7012
7013 -- the name of a non-type template-parameter;
7014 -- the address of an object or function with external linkage, [...]
7015 expressed as "& id-expression" where the & is optional if the name
7016 refers to a function or array, or if the corresponding
7017 template-parameter is a reference.
7018
7019 Here, we do not care about functions, as they are invalid anyway
7020 for a parameter of type pointer-to-object. */
7021
7022 if (value_dependent_expression_p (expr))
7023 /* Non-type template parameters are OK. */
7024 ;
7025 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7026 /* Null pointer values are OK in C++11. */;
7027 else if (TREE_CODE (expr) != ADDR_EXPR
7028 && !INDIRECT_TYPE_P (expr_type))
7029 /* Other values, like integer constants, might be valid
7030 non-type arguments of some other type. */
7031 return error_mark_node;
7032 else if (invalid_tparm_referent_p (type, expr, complain))
7033 return NULL_TREE;
7034
7035 expr = decayed;
7036
7037 expr = perform_qualification_conversions (type, expr);
7038 if (expr == error_mark_node)
7039 return error_mark_node;
7040 }
7041 /* [temp.arg.nontype]/5, bullet 3
7042
7043 For a non-type template-parameter of type reference to object, no
7044 conversions apply. The type referred to by the reference may be more
7045 cv-qualified than the (otherwise identical) type of the
7046 template-argument. The template-parameter is bound directly to the
7047 template-argument, which must be an lvalue. */
7048 else if (TYPE_REF_OBJ_P (type))
7049 {
7050 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
7051 expr_type))
7052 return error_mark_node;
7053
7054 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
7055 {
7056 if (complain & tf_error)
7057 error ("%qE is not a valid template argument for type %qT "
7058 "because of conflicts in cv-qualification", expr, type);
7059 return NULL_TREE;
7060 }
7061
7062 if (!lvalue_p (expr))
7063 {
7064 if (complain & tf_error)
7065 error ("%qE is not a valid template argument for type %qT "
7066 "because it is not an lvalue", expr, type);
7067 return NULL_TREE;
7068 }
7069
7070 /* [temp.arg.nontype]/1
7071
7072 A template-argument for a non-type, non-template template-parameter
7073 shall be one of: [...]
7074
7075 -- the address of an object or function with external linkage. */
7076 if (INDIRECT_REF_P (expr)
7077 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
7078 {
7079 expr = TREE_OPERAND (expr, 0);
7080 if (DECL_P (expr))
7081 {
7082 if (complain & tf_error)
7083 error ("%q#D is not a valid template argument for type %qT "
7084 "because a reference variable does not have a constant "
7085 "address", expr, type);
7086 return NULL_TREE;
7087 }
7088 }
7089
7090 if (TYPE_REF_OBJ_P (TREE_TYPE (expr))
7091 && value_dependent_expression_p (expr))
7092 /* OK, dependent reference. We don't want to ask whether a DECL is
7093 itself value-dependent, since what we want here is its address. */;
7094 else
7095 {
7096 expr = build_address (expr);
7097
7098 if (invalid_tparm_referent_p (type, expr, complain))
7099 return NULL_TREE;
7100 }
7101
7102 if (!same_type_p (type, TREE_TYPE (expr)))
7103 expr = build_nop (type, expr);
7104 }
7105 /* [temp.arg.nontype]/5, bullet 4
7106
7107 For a non-type template-parameter of type pointer to function, only
7108 the function-to-pointer conversion (_conv.func_) is applied. If the
7109 template-argument represents a set of overloaded functions (or a
7110 pointer to such), the matching function is selected from the set
7111 (_over.over_). */
7112 else if (TYPE_PTRFN_P (type))
7113 {
7114 /* If the argument is a template-id, we might not have enough
7115 context information to decay the pointer. */
7116 if (!type_unknown_p (expr_type))
7117 {
7118 expr = decay_conversion (expr, complain);
7119 if (expr == error_mark_node)
7120 return error_mark_node;
7121 }
7122
7123 if (cxx_dialect >= cxx11 && integer_zerop (expr))
7124 /* Null pointer values are OK in C++11. */
7125 return perform_qualification_conversions (type, expr);
7126
7127 expr = convert_nontype_argument_function (type, expr, complain);
7128 if (!expr || expr == error_mark_node)
7129 return expr;
7130 }
7131 /* [temp.arg.nontype]/5, bullet 5
7132
7133 For a non-type template-parameter of type reference to function, no
7134 conversions apply. If the template-argument represents a set of
7135 overloaded functions, the matching function is selected from the set
7136 (_over.over_). */
7137 else if (TYPE_REFFN_P (type))
7138 {
7139 if (TREE_CODE (expr) == ADDR_EXPR)
7140 {
7141 if (complain & tf_error)
7142 {
7143 error ("%qE is not a valid template argument for type %qT "
7144 "because it is a pointer", expr, type);
7145 inform (input_location, "try using %qE instead",
7146 TREE_OPERAND (expr, 0));
7147 }
7148 return NULL_TREE;
7149 }
7150
7151 expr = convert_nontype_argument_function (type, expr, complain);
7152 if (!expr || expr == error_mark_node)
7153 return expr;
7154 }
7155 /* [temp.arg.nontype]/5, bullet 6
7156
7157 For a non-type template-parameter of type pointer to member function,
7158 no conversions apply. If the template-argument represents a set of
7159 overloaded member functions, the matching member function is selected
7160 from the set (_over.over_). */
7161 else if (TYPE_PTRMEMFUNC_P (type))
7162 {
7163 expr = instantiate_type (type, expr, tf_none);
7164 if (expr == error_mark_node)
7165 return error_mark_node;
7166
7167 /* [temp.arg.nontype] bullet 1 says the pointer to member
7168 expression must be a pointer-to-member constant. */
7169 if (!value_dependent_expression_p (expr)
7170 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7171 return NULL_TREE;
7172
7173 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7174 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7175 if (fnptr_conv_p (type, TREE_TYPE (expr)))
7176 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
7177 }
7178 /* [temp.arg.nontype]/5, bullet 7
7179
7180 For a non-type template-parameter of type pointer to data member,
7181 qualification conversions (_conv.qual_) are applied. */
7182 else if (TYPE_PTRDATAMEM_P (type))
7183 {
7184 /* [temp.arg.nontype] bullet 1 says the pointer to member
7185 expression must be a pointer-to-member constant. */
7186 if (!value_dependent_expression_p (expr)
7187 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7188 return NULL_TREE;
7189
7190 expr = perform_qualification_conversions (type, expr);
7191 if (expr == error_mark_node)
7192 return expr;
7193 }
7194 else if (NULLPTR_TYPE_P (type))
7195 {
7196 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
7197 {
7198 if (complain & tf_error)
7199 error ("%qE is not a valid template argument for type %qT "
7200 "because it is of type %qT", expr, type, TREE_TYPE (expr));
7201 return NULL_TREE;
7202 }
7203 return expr;
7204 }
7205 else if (CLASS_TYPE_P (type))
7206 {
7207 /* Replace the argument with a reference to the corresponding template
7208 parameter object. */
7209 if (!value_dependent_expression_p (expr))
7210 expr = get_template_parm_object (expr, complain);
7211 if (expr == error_mark_node)
7212 return NULL_TREE;
7213 }
7214 /* A template non-type parameter must be one of the above. */
7215 else
7216 gcc_unreachable ();
7217
7218 /* Sanity check: did we actually convert the argument to the
7219 right type? */
7220 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7221 (type, TREE_TYPE (expr)));
7222 return convert_from_reference (expr);
7223 }
7224
7225 /* Subroutine of coerce_template_template_parms, which returns 1 if
7226 PARM_PARM and ARG_PARM match using the rule for the template
7227 parameters of template template parameters. Both PARM and ARG are
7228 template parameters; the rest of the arguments are the same as for
7229 coerce_template_template_parms.
7230 */
7231 static int
7232 coerce_template_template_parm (tree parm,
7233 tree arg,
7234 tsubst_flags_t complain,
7235 tree in_decl,
7236 tree outer_args)
7237 {
7238 if (arg == NULL_TREE || error_operand_p (arg)
7239 || parm == NULL_TREE || error_operand_p (parm))
7240 return 0;
7241
7242 if (TREE_CODE (arg) != TREE_CODE (parm))
7243 return 0;
7244
7245 switch (TREE_CODE (parm))
7246 {
7247 case TEMPLATE_DECL:
7248 /* We encounter instantiations of templates like
7249 template <template <template <class> class> class TT>
7250 class C; */
7251 {
7252 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7253 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7254
7255 if (!coerce_template_template_parms
7256 (parmparm, argparm, complain, in_decl, outer_args))
7257 return 0;
7258 }
7259 /* Fall through. */
7260
7261 case TYPE_DECL:
7262 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
7263 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7264 /* Argument is a parameter pack but parameter is not. */
7265 return 0;
7266 break;
7267
7268 case PARM_DECL:
7269 /* The tsubst call is used to handle cases such as
7270
7271 template <int> class C {};
7272 template <class T, template <T> class TT> class D {};
7273 D<int, C> d;
7274
7275 i.e. the parameter list of TT depends on earlier parameters. */
7276 if (!uses_template_parms (TREE_TYPE (arg)))
7277 {
7278 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
7279 if (!uses_template_parms (t)
7280 && !same_type_p (t, TREE_TYPE (arg)))
7281 return 0;
7282 }
7283
7284 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
7285 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7286 /* Argument is a parameter pack but parameter is not. */
7287 return 0;
7288
7289 break;
7290
7291 default:
7292 gcc_unreachable ();
7293 }
7294
7295 return 1;
7296 }
7297
7298 /* Coerce template argument list ARGLIST for use with template
7299 template-parameter TEMPL. */
7300
7301 static tree
7302 coerce_template_args_for_ttp (tree templ, tree arglist,
7303 tsubst_flags_t complain)
7304 {
7305 /* Consider an example where a template template parameter declared as
7306
7307 template <class T, class U = std::allocator<T> > class TT
7308
7309 The template parameter level of T and U are one level larger than
7310 of TT. To proper process the default argument of U, say when an
7311 instantiation `TT<int>' is seen, we need to build the full
7312 arguments containing {int} as the innermost level. Outer levels,
7313 available when not appearing as default template argument, can be
7314 obtained from the arguments of the enclosing template.
7315
7316 Suppose that TT is later substituted with std::vector. The above
7317 instantiation is `TT<int, std::allocator<T> >' with TT at
7318 level 1, and T at level 2, while the template arguments at level 1
7319 becomes {std::vector} and the inner level 2 is {int}. */
7320
7321 tree outer = DECL_CONTEXT (templ);
7322 if (outer)
7323 {
7324 if (DECL_TEMPLATE_SPECIALIZATION (outer))
7325 /* We want arguments for the partial specialization, not arguments for
7326 the primary template. */
7327 outer = template_parms_to_args (DECL_TEMPLATE_PARMS (outer));
7328 else
7329 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7330 }
7331 else if (current_template_parms)
7332 {
7333 /* This is an argument of the current template, so we haven't set
7334 DECL_CONTEXT yet. */
7335 tree relevant_template_parms;
7336
7337 /* Parameter levels that are greater than the level of the given
7338 template template parm are irrelevant. */
7339 relevant_template_parms = current_template_parms;
7340 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7341 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7342 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7343
7344 outer = template_parms_to_args (relevant_template_parms);
7345 }
7346
7347 if (outer)
7348 arglist = add_to_template_args (outer, arglist);
7349
7350 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7351 return coerce_template_parms (parmlist, arglist, templ,
7352 complain,
7353 /*require_all_args=*/true,
7354 /*use_default_args=*/true);
7355 }
7356
7357 /* A cache of template template parameters with match-all default
7358 arguments. */
7359 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7360 static void
7361 store_defaulted_ttp (tree v, tree t)
7362 {
7363 if (!defaulted_ttp_cache)
7364 defaulted_ttp_cache = hash_map<tree,tree>::create_ggc (13);
7365 defaulted_ttp_cache->put (v, t);
7366 }
7367 static tree
7368 lookup_defaulted_ttp (tree v)
7369 {
7370 if (defaulted_ttp_cache)
7371 if (tree *p = defaulted_ttp_cache->get (v))
7372 return *p;
7373 return NULL_TREE;
7374 }
7375
7376 /* T is a bound template template-parameter. Copy its arguments into default
7377 arguments of the template template-parameter's template parameters. */
7378
7379 static tree
7380 add_defaults_to_ttp (tree otmpl)
7381 {
7382 if (tree c = lookup_defaulted_ttp (otmpl))
7383 return c;
7384
7385 tree ntmpl = copy_node (otmpl);
7386
7387 tree ntype = copy_node (TREE_TYPE (otmpl));
7388 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7389 TYPE_MAIN_VARIANT (ntype) = ntype;
7390 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7391 TYPE_NAME (ntype) = ntmpl;
7392 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7393
7394 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7395 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7396 TEMPLATE_PARM_DECL (idx) = ntmpl;
7397 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7398
7399 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7400 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7401 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7402 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7403 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7404 {
7405 tree o = TREE_VEC_ELT (vec, i);
7406 if (!template_parameter_pack_p (TREE_VALUE (o)))
7407 {
7408 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7409 TREE_PURPOSE (n) = any_targ_node;
7410 }
7411 }
7412
7413 store_defaulted_ttp (otmpl, ntmpl);
7414 return ntmpl;
7415 }
7416
7417 /* ARG is a bound potential template template-argument, and PARGS is a list
7418 of arguments for the corresponding template template-parameter. Adjust
7419 PARGS as appropriate for application to ARG's template, and if ARG is a
7420 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7421 arguments to the template template parameter. */
7422
7423 static tree
7424 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7425 {
7426 ++processing_template_decl;
7427 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7428 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7429 {
7430 /* When comparing two template template-parameters in partial ordering,
7431 rewrite the one currently being used as an argument to have default
7432 arguments for all parameters. */
7433 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7434 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7435 if (pargs != error_mark_node)
7436 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7437 TYPE_TI_ARGS (arg));
7438 }
7439 else
7440 {
7441 tree aparms
7442 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7443 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7444 /*require_all*/true,
7445 /*use_default*/true);
7446 }
7447 --processing_template_decl;
7448 return pargs;
7449 }
7450
7451 /* Subroutine of unify for the case when PARM is a
7452 BOUND_TEMPLATE_TEMPLATE_PARM. */
7453
7454 static int
7455 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7456 bool explain_p)
7457 {
7458 tree parmvec = TYPE_TI_ARGS (parm);
7459 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7460
7461 /* The template template parm might be variadic and the argument
7462 not, so flatten both argument lists. */
7463 parmvec = expand_template_argument_pack (parmvec);
7464 argvec = expand_template_argument_pack (argvec);
7465
7466 if (flag_new_ttp)
7467 {
7468 /* In keeping with P0522R0, adjust P's template arguments
7469 to apply to A's template; then flatten it again. */
7470 tree nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7471 nparmvec = expand_template_argument_pack (nparmvec);
7472
7473 if (unify (tparms, targs, nparmvec, argvec,
7474 UNIFY_ALLOW_NONE, explain_p))
7475 return 1;
7476
7477 /* If the P0522 adjustment eliminated a pack expansion, deduce
7478 empty packs. */
7479 if (flag_new_ttp
7480 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7481 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7482 DEDUCE_EXACT, /*sub*/true, explain_p))
7483 return 1;
7484 }
7485 else
7486 {
7487 /* Deduce arguments T, i from TT<T> or TT<i>.
7488 We check each element of PARMVEC and ARGVEC individually
7489 rather than the whole TREE_VEC since they can have
7490 different number of elements, which is allowed under N2555. */
7491
7492 int len = TREE_VEC_LENGTH (parmvec);
7493
7494 /* Check if the parameters end in a pack, making them
7495 variadic. */
7496 int parm_variadic_p = 0;
7497 if (len > 0
7498 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7499 parm_variadic_p = 1;
7500
7501 for (int i = 0; i < len - parm_variadic_p; ++i)
7502 /* If the template argument list of P contains a pack
7503 expansion that is not the last template argument, the
7504 entire template argument list is a non-deduced
7505 context. */
7506 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7507 return unify_success (explain_p);
7508
7509 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7510 return unify_too_few_arguments (explain_p,
7511 TREE_VEC_LENGTH (argvec), len);
7512
7513 for (int i = 0; i < len - parm_variadic_p; ++i)
7514 if (unify (tparms, targs,
7515 TREE_VEC_ELT (parmvec, i),
7516 TREE_VEC_ELT (argvec, i),
7517 UNIFY_ALLOW_NONE, explain_p))
7518 return 1;
7519
7520 if (parm_variadic_p
7521 && unify_pack_expansion (tparms, targs,
7522 parmvec, argvec,
7523 DEDUCE_EXACT,
7524 /*subr=*/true, explain_p))
7525 return 1;
7526 }
7527
7528 return 0;
7529 }
7530
7531 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7532 template template parameters. Both PARM_PARMS and ARG_PARMS are
7533 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7534 or PARM_DECL.
7535
7536 Consider the example:
7537 template <class T> class A;
7538 template<template <class U> class TT> class B;
7539
7540 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7541 the parameters to A, and OUTER_ARGS contains A. */
7542
7543 static int
7544 coerce_template_template_parms (tree parm_parms,
7545 tree arg_parms,
7546 tsubst_flags_t complain,
7547 tree in_decl,
7548 tree outer_args)
7549 {
7550 int nparms, nargs, i;
7551 tree parm, arg;
7552 int variadic_p = 0;
7553
7554 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7555 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7556
7557 nparms = TREE_VEC_LENGTH (parm_parms);
7558 nargs = TREE_VEC_LENGTH (arg_parms);
7559
7560 if (flag_new_ttp)
7561 {
7562 /* P0522R0: A template template-parameter P is at least as specialized as
7563 a template template-argument A if, given the following rewrite to two
7564 function templates, the function template corresponding to P is at
7565 least as specialized as the function template corresponding to A
7566 according to the partial ordering rules for function templates
7567 ([temp.func.order]). Given an invented class template X with the
7568 template parameter list of A (including default arguments):
7569
7570 * Each of the two function templates has the same template parameters,
7571 respectively, as P or A.
7572
7573 * Each function template has a single function parameter whose type is
7574 a specialization of X with template arguments corresponding to the
7575 template parameters from the respective function template where, for
7576 each template parameter PP in the template parameter list of the
7577 function template, a corresponding template argument AA is formed. If
7578 PP declares a parameter pack, then AA is the pack expansion
7579 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7580
7581 If the rewrite produces an invalid type, then P is not at least as
7582 specialized as A. */
7583
7584 /* So coerce P's args to apply to A's parms, and then deduce between A's
7585 args and the converted args. If that succeeds, A is at least as
7586 specialized as P, so they match.*/
7587 tree pargs = template_parms_level_to_args (parm_parms);
7588 pargs = add_outermost_template_args (outer_args, pargs);
7589 ++processing_template_decl;
7590 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7591 /*require_all*/true, /*use_default*/true);
7592 --processing_template_decl;
7593 if (pargs != error_mark_node)
7594 {
7595 tree targs = make_tree_vec (nargs);
7596 tree aargs = template_parms_level_to_args (arg_parms);
7597 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7598 /*explain*/false))
7599 return 1;
7600 }
7601 }
7602
7603 /* Determine whether we have a parameter pack at the end of the
7604 template template parameter's template parameter list. */
7605 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7606 {
7607 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7608
7609 if (error_operand_p (parm))
7610 return 0;
7611
7612 switch (TREE_CODE (parm))
7613 {
7614 case TEMPLATE_DECL:
7615 case TYPE_DECL:
7616 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7617 variadic_p = 1;
7618 break;
7619
7620 case PARM_DECL:
7621 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7622 variadic_p = 1;
7623 break;
7624
7625 default:
7626 gcc_unreachable ();
7627 }
7628 }
7629
7630 if (nargs != nparms
7631 && !(variadic_p && nargs >= nparms - 1))
7632 return 0;
7633
7634 /* Check all of the template parameters except the parameter pack at
7635 the end (if any). */
7636 for (i = 0; i < nparms - variadic_p; ++i)
7637 {
7638 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7639 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7640 continue;
7641
7642 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
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 if (variadic_p)
7652 {
7653 /* Check each of the template parameters in the template
7654 argument against the template parameter pack at the end of
7655 the template template parameter. */
7656 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7657 return 0;
7658
7659 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7660
7661 for (; i < nargs; ++i)
7662 {
7663 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7664 continue;
7665
7666 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7667
7668 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7669 outer_args))
7670 return 0;
7671 }
7672 }
7673
7674 return 1;
7675 }
7676
7677 /* Verifies that the deduced template arguments (in TARGS) for the
7678 template template parameters (in TPARMS) represent valid bindings,
7679 by comparing the template parameter list of each template argument
7680 to the template parameter list of its corresponding template
7681 template parameter, in accordance with DR150. This
7682 routine can only be called after all template arguments have been
7683 deduced. It will return TRUE if all of the template template
7684 parameter bindings are okay, FALSE otherwise. */
7685 bool
7686 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7687 {
7688 int i, ntparms = TREE_VEC_LENGTH (tparms);
7689 bool ret = true;
7690
7691 /* We're dealing with template parms in this process. */
7692 ++processing_template_decl;
7693
7694 targs = INNERMOST_TEMPLATE_ARGS (targs);
7695
7696 for (i = 0; i < ntparms; ++i)
7697 {
7698 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7699 tree targ = TREE_VEC_ELT (targs, i);
7700
7701 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7702 {
7703 tree packed_args = NULL_TREE;
7704 int idx, len = 1;
7705
7706 if (ARGUMENT_PACK_P (targ))
7707 {
7708 /* Look inside the argument pack. */
7709 packed_args = ARGUMENT_PACK_ARGS (targ);
7710 len = TREE_VEC_LENGTH (packed_args);
7711 }
7712
7713 for (idx = 0; idx < len; ++idx)
7714 {
7715 tree targ_parms = NULL_TREE;
7716
7717 if (packed_args)
7718 /* Extract the next argument from the argument
7719 pack. */
7720 targ = TREE_VEC_ELT (packed_args, idx);
7721
7722 if (PACK_EXPANSION_P (targ))
7723 /* Look at the pattern of the pack expansion. */
7724 targ = PACK_EXPANSION_PATTERN (targ);
7725
7726 /* Extract the template parameters from the template
7727 argument. */
7728 if (TREE_CODE (targ) == TEMPLATE_DECL)
7729 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7730 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7731 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7732
7733 /* Verify that we can coerce the template template
7734 parameters from the template argument to the template
7735 parameter. This requires an exact match. */
7736 if (targ_parms
7737 && !coerce_template_template_parms
7738 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7739 targ_parms,
7740 tf_none,
7741 tparm,
7742 targs))
7743 {
7744 ret = false;
7745 goto out;
7746 }
7747 }
7748 }
7749 }
7750
7751 out:
7752
7753 --processing_template_decl;
7754 return ret;
7755 }
7756
7757 /* Since type attributes aren't mangled, we need to strip them from
7758 template type arguments. */
7759
7760 static tree
7761 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7762 {
7763 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7764 return arg;
7765 bool removed_attributes = false;
7766 tree canon = strip_typedefs (arg, &removed_attributes);
7767 if (removed_attributes
7768 && (complain & tf_warning))
7769 warning (OPT_Wignored_attributes,
7770 "ignoring attributes on template argument %qT", arg);
7771 return canon;
7772 }
7773
7774 /* And from inside dependent non-type arguments like sizeof(Type). */
7775
7776 static tree
7777 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7778 {
7779 if (!arg || arg == error_mark_node)
7780 return arg;
7781 bool removed_attributes = false;
7782 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7783 if (removed_attributes
7784 && (complain & tf_warning))
7785 warning (OPT_Wignored_attributes,
7786 "ignoring attributes in template argument %qE", arg);
7787 return canon;
7788 }
7789
7790 // A template declaration can be substituted for a constrained
7791 // template template parameter only when the argument is more
7792 // constrained than the parameter.
7793 static bool
7794 is_compatible_template_arg (tree parm, tree arg)
7795 {
7796 tree parm_cons = get_constraints (parm);
7797
7798 /* For now, allow constrained template template arguments
7799 and unconstrained template template parameters. */
7800 if (parm_cons == NULL_TREE)
7801 return true;
7802
7803 tree arg_cons = get_constraints (arg);
7804
7805 /* If the template parameter is constrained, we need to rewrite its
7806 constraints in terms of the ARG's template parameters. This ensures
7807 that all of the template parameter types will have the same depth.
7808
7809 Note that this is only valid when coerce_template_template_parm is
7810 true for the innermost template parameters of PARM and ARG. In other
7811 words, because coercion is successful, this conversion will be valid. */
7812 if (parm_cons)
7813 {
7814 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7815 parm_cons = tsubst_constraint_info (parm_cons,
7816 INNERMOST_TEMPLATE_ARGS (args),
7817 tf_none, NULL_TREE);
7818 if (parm_cons == error_mark_node)
7819 return false;
7820 }
7821
7822 return subsumes (parm_cons, arg_cons);
7823 }
7824
7825 // Convert a placeholder argument into a binding to the original
7826 // parameter. The original parameter is saved as the TREE_TYPE of
7827 // ARG.
7828 static inline tree
7829 convert_wildcard_argument (tree parm, tree arg)
7830 {
7831 TREE_TYPE (arg) = parm;
7832 return arg;
7833 }
7834
7835 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
7836 because one of them is dependent. But we need to represent the
7837 conversion for the benefit of cp_tree_equal. */
7838
7839 static tree
7840 maybe_convert_nontype_argument (tree type, tree arg)
7841 {
7842 /* Auto parms get no conversion. */
7843 if (type_uses_auto (type))
7844 return arg;
7845 /* We don't need or want to add this conversion now if we're going to use the
7846 argument for deduction. */
7847 if (value_dependent_expression_p (arg))
7848 return arg;
7849
7850 type = cv_unqualified (type);
7851 tree argtype = TREE_TYPE (arg);
7852 if (same_type_p (type, argtype))
7853 return arg;
7854
7855 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
7856 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
7857 return arg;
7858 }
7859
7860 /* Convert the indicated template ARG as necessary to match the
7861 indicated template PARM. Returns the converted ARG, or
7862 error_mark_node if the conversion was unsuccessful. Error and
7863 warning messages are issued under control of COMPLAIN. This
7864 conversion is for the Ith parameter in the parameter list. ARGS is
7865 the full set of template arguments deduced so far. */
7866
7867 static tree
7868 convert_template_argument (tree parm,
7869 tree arg,
7870 tree args,
7871 tsubst_flags_t complain,
7872 int i,
7873 tree in_decl)
7874 {
7875 tree orig_arg;
7876 tree val;
7877 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7878
7879 if (parm == error_mark_node || error_operand_p (arg))
7880 return error_mark_node;
7881
7882 /* Trivially convert placeholders. */
7883 if (TREE_CODE (arg) == WILDCARD_DECL)
7884 return convert_wildcard_argument (parm, arg);
7885
7886 if (arg == any_targ_node)
7887 return arg;
7888
7889 if (TREE_CODE (arg) == TREE_LIST
7890 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7891 {
7892 /* The template argument was the name of some
7893 member function. That's usually
7894 invalid, but static members are OK. In any
7895 case, grab the underlying fields/functions
7896 and issue an error later if required. */
7897 TREE_TYPE (arg) = unknown_type_node;
7898 }
7899
7900 orig_arg = arg;
7901
7902 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7903 requires_type = (TREE_CODE (parm) == TYPE_DECL
7904 || requires_tmpl_type);
7905
7906 /* When determining whether an argument pack expansion is a template,
7907 look at the pattern. */
7908 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7909 arg = PACK_EXPANSION_PATTERN (arg);
7910
7911 /* Deal with an injected-class-name used as a template template arg. */
7912 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7913 {
7914 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7915 if (TREE_CODE (t) == TEMPLATE_DECL)
7916 {
7917 if (cxx_dialect >= cxx11)
7918 /* OK under DR 1004. */;
7919 else if (complain & tf_warning_or_error)
7920 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7921 " used as template template argument", TYPE_NAME (arg));
7922 else if (flag_pedantic_errors)
7923 t = arg;
7924
7925 arg = t;
7926 }
7927 }
7928
7929 is_tmpl_type =
7930 ((TREE_CODE (arg) == TEMPLATE_DECL
7931 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7932 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7933 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7934 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7935
7936 if (is_tmpl_type
7937 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7938 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7939 arg = TYPE_STUB_DECL (arg);
7940
7941 is_type = TYPE_P (arg) || is_tmpl_type;
7942
7943 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7944 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7945 {
7946 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7947 {
7948 if (complain & tf_error)
7949 error ("invalid use of destructor %qE as a type", orig_arg);
7950 return error_mark_node;
7951 }
7952
7953 permerror (input_location,
7954 "to refer to a type member of a template parameter, "
7955 "use %<typename %E%>", orig_arg);
7956
7957 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7958 TREE_OPERAND (arg, 1),
7959 typename_type,
7960 complain);
7961 arg = orig_arg;
7962 is_type = 1;
7963 }
7964 if (is_type != requires_type)
7965 {
7966 if (in_decl)
7967 {
7968 if (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_type)
7974 {
7975 /* The template argument is a type, but we're expecting
7976 an expression. */
7977 inform (input_location,
7978 " expected a constant of type %qT, got %qT",
7979 TREE_TYPE (parm),
7980 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7981 /* [temp.arg]/2: "In a template-argument, an ambiguity
7982 between a type-id and an expression is resolved to a
7983 type-id, regardless of the form of the corresponding
7984 template-parameter." So give the user a clue. */
7985 if (TREE_CODE (arg) == FUNCTION_TYPE)
7986 inform (input_location, " ambiguous template argument "
7987 "for non-type template parameter is treated as "
7988 "function type");
7989 }
7990 else if (requires_tmpl_type)
7991 inform (input_location,
7992 " expected a class template, got %qE", orig_arg);
7993 else
7994 inform (input_location,
7995 " expected a type, got %qE", orig_arg);
7996 }
7997 }
7998 return error_mark_node;
7999 }
8000 if (is_tmpl_type ^ requires_tmpl_type)
8001 {
8002 if (in_decl && (complain & tf_error))
8003 {
8004 error ("type/value mismatch at argument %d in template "
8005 "parameter list for %qD",
8006 i + 1, in_decl);
8007 if (is_tmpl_type)
8008 inform (input_location,
8009 " expected a type, got %qT", DECL_NAME (arg));
8010 else
8011 inform (input_location,
8012 " expected a class template, got %qT", orig_arg);
8013 }
8014 return error_mark_node;
8015 }
8016
8017 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
8018 /* We already did the appropriate conversion when packing args. */
8019 val = orig_arg;
8020 else if (is_type)
8021 {
8022 if (requires_tmpl_type)
8023 {
8024 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
8025 /* The number of argument required is not known yet.
8026 Just accept it for now. */
8027 val = orig_arg;
8028 else
8029 {
8030 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
8031 tree argparm;
8032
8033 /* Strip alias templates that are equivalent to another
8034 template. */
8035 arg = get_underlying_template (arg);
8036 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8037
8038 if (coerce_template_template_parms (parmparm, argparm,
8039 complain, in_decl,
8040 args))
8041 {
8042 val = arg;
8043
8044 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8045 TEMPLATE_DECL. */
8046 if (val != error_mark_node)
8047 {
8048 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
8049 val = TREE_TYPE (val);
8050 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
8051 val = make_pack_expansion (val, complain);
8052 }
8053 }
8054 else
8055 {
8056 if (in_decl && (complain & tf_error))
8057 {
8058 error ("type/value mismatch at argument %d in "
8059 "template parameter list for %qD",
8060 i + 1, in_decl);
8061 inform (input_location,
8062 " expected a template of type %qD, got %qT",
8063 parm, orig_arg);
8064 }
8065
8066 val = error_mark_node;
8067 }
8068
8069 // Check that the constraints are compatible before allowing the
8070 // substitution.
8071 if (val != error_mark_node)
8072 if (!is_compatible_template_arg (parm, arg))
8073 {
8074 if (in_decl && (complain & tf_error))
8075 {
8076 error ("constraint mismatch at argument %d in "
8077 "template parameter list for %qD",
8078 i + 1, in_decl);
8079 inform (input_location, " expected %qD but got %qD",
8080 parm, arg);
8081 }
8082 val = error_mark_node;
8083 }
8084 }
8085 }
8086 else
8087 val = orig_arg;
8088 /* We only form one instance of each template specialization.
8089 Therefore, if we use a non-canonical variant (i.e., a
8090 typedef), any future messages referring to the type will use
8091 the typedef, which is confusing if those future uses do not
8092 themselves also use the typedef. */
8093 if (TYPE_P (val))
8094 val = canonicalize_type_argument (val, complain);
8095 }
8096 else
8097 {
8098 tree t = TREE_TYPE (parm);
8099
8100 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm))
8101 > TMPL_ARGS_DEPTH (args))
8102 /* We don't have enough levels of args to do any substitution. This
8103 can happen in the context of -fnew-ttp-matching. */;
8104 else if (tree a = type_uses_auto (t))
8105 {
8106 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
8107 if (t == error_mark_node)
8108 return error_mark_node;
8109 }
8110 else
8111 t = tsubst (t, args, complain, in_decl);
8112
8113 if (invalid_nontype_parm_type_p (t, complain))
8114 return error_mark_node;
8115
8116 if (t != TREE_TYPE (parm))
8117 t = canonicalize_type_argument (t, complain);
8118
8119 if (!type_dependent_expression_p (orig_arg)
8120 && !uses_template_parms (t))
8121 /* We used to call digest_init here. However, digest_init
8122 will report errors, which we don't want when complain
8123 is zero. More importantly, digest_init will try too
8124 hard to convert things: for example, `0' should not be
8125 converted to pointer type at this point according to
8126 the standard. Accepting this is not merely an
8127 extension, since deciding whether or not these
8128 conversions can occur is part of determining which
8129 function template to call, or whether a given explicit
8130 argument specification is valid. */
8131 val = convert_nontype_argument (t, orig_arg, complain);
8132 else
8133 {
8134 val = canonicalize_expr_argument (orig_arg, complain);
8135 val = maybe_convert_nontype_argument (t, val);
8136 }
8137
8138
8139 if (val == NULL_TREE)
8140 val = error_mark_node;
8141 else if (val == error_mark_node && (complain & tf_error))
8142 error_at (cp_expr_loc_or_input_loc (orig_arg),
8143 "could not convert template argument %qE from %qT to %qT",
8144 orig_arg, TREE_TYPE (orig_arg), t);
8145
8146 if (INDIRECT_REF_P (val))
8147 {
8148 /* Reject template arguments that are references to built-in
8149 functions with no library fallbacks. */
8150 const_tree inner = TREE_OPERAND (val, 0);
8151 const_tree innertype = TREE_TYPE (inner);
8152 if (innertype
8153 && TYPE_REF_P (innertype)
8154 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
8155 && TREE_OPERAND_LENGTH (inner) > 0
8156 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
8157 return error_mark_node;
8158 }
8159
8160 if (TREE_CODE (val) == SCOPE_REF)
8161 {
8162 /* Strip typedefs from the SCOPE_REF. */
8163 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
8164 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
8165 complain);
8166 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
8167 QUALIFIED_NAME_IS_TEMPLATE (val));
8168 }
8169 }
8170
8171 return val;
8172 }
8173
8174 /* Coerces the remaining template arguments in INNER_ARGS (from
8175 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8176 Returns the coerced argument pack. PARM_IDX is the position of this
8177 parameter in the template parameter list. ARGS is the original
8178 template argument list. */
8179 static tree
8180 coerce_template_parameter_pack (tree parms,
8181 int parm_idx,
8182 tree args,
8183 tree inner_args,
8184 int arg_idx,
8185 tree new_args,
8186 int* lost,
8187 tree in_decl,
8188 tsubst_flags_t complain)
8189 {
8190 tree parm = TREE_VEC_ELT (parms, parm_idx);
8191 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8192 tree packed_args;
8193 tree argument_pack;
8194 tree packed_parms = NULL_TREE;
8195
8196 if (arg_idx > nargs)
8197 arg_idx = nargs;
8198
8199 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
8200 {
8201 /* When the template parameter is a non-type template parameter pack
8202 or template template parameter pack whose type or template
8203 parameters use parameter packs, we know exactly how many arguments
8204 we are looking for. Build a vector of the instantiated decls for
8205 these template parameters in PACKED_PARMS. */
8206 /* We can't use make_pack_expansion here because it would interpret a
8207 _DECL as a use rather than a declaration. */
8208 tree decl = TREE_VALUE (parm);
8209 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
8210 SET_PACK_EXPANSION_PATTERN (exp, decl);
8211 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
8212 SET_TYPE_STRUCTURAL_EQUALITY (exp);
8213
8214 TREE_VEC_LENGTH (args)--;
8215 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
8216 TREE_VEC_LENGTH (args)++;
8217
8218 if (packed_parms == error_mark_node)
8219 return error_mark_node;
8220
8221 /* If we're doing a partial instantiation of a member template,
8222 verify that all of the types used for the non-type
8223 template parameter pack are, in fact, valid for non-type
8224 template parameters. */
8225 if (arg_idx < nargs
8226 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
8227 {
8228 int j, len = TREE_VEC_LENGTH (packed_parms);
8229 for (j = 0; j < len; ++j)
8230 {
8231 tree t = TREE_VEC_ELT (packed_parms, j);
8232 if (TREE_CODE (t) == PARM_DECL
8233 && invalid_nontype_parm_type_p (TREE_TYPE (t), complain))
8234 return error_mark_node;
8235 }
8236 /* We don't know how many args we have yet, just
8237 use the unconverted ones for now. */
8238 return NULL_TREE;
8239 }
8240
8241 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
8242 }
8243 /* Check if we have a placeholder pack, which indicates we're
8244 in the context of a introduction list. In that case we want
8245 to match this pack to the single placeholder. */
8246 else if (arg_idx < nargs
8247 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
8248 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
8249 {
8250 nargs = arg_idx + 1;
8251 packed_args = make_tree_vec (1);
8252 }
8253 else
8254 packed_args = make_tree_vec (nargs - arg_idx);
8255
8256 /* Convert the remaining arguments, which will be a part of the
8257 parameter pack "parm". */
8258 int first_pack_arg = arg_idx;
8259 for (; arg_idx < nargs; ++arg_idx)
8260 {
8261 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
8262 tree actual_parm = TREE_VALUE (parm);
8263 int pack_idx = arg_idx - first_pack_arg;
8264
8265 if (packed_parms)
8266 {
8267 /* Once we've packed as many args as we have types, stop. */
8268 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
8269 break;
8270 else if (PACK_EXPANSION_P (arg))
8271 /* We don't know how many args we have yet, just
8272 use the unconverted ones for now. */
8273 return NULL_TREE;
8274 else
8275 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
8276 }
8277
8278 if (arg == error_mark_node)
8279 {
8280 if (complain & tf_error)
8281 error ("template argument %d is invalid", arg_idx + 1);
8282 }
8283 else
8284 arg = convert_template_argument (actual_parm,
8285 arg, new_args, complain, parm_idx,
8286 in_decl);
8287 if (arg == error_mark_node)
8288 (*lost)++;
8289 TREE_VEC_ELT (packed_args, pack_idx) = arg;
8290 }
8291
8292 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
8293 && TREE_VEC_LENGTH (packed_args) > 0)
8294 {
8295 if (complain & tf_error)
8296 error ("wrong number of template arguments (%d, should be %d)",
8297 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
8298 return error_mark_node;
8299 }
8300
8301 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
8302 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
8303 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
8304 else
8305 {
8306 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
8307 TREE_CONSTANT (argument_pack) = 1;
8308 }
8309
8310 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
8311 if (CHECKING_P)
8312 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
8313 TREE_VEC_LENGTH (packed_args));
8314 return argument_pack;
8315 }
8316
8317 /* Returns the number of pack expansions in the template argument vector
8318 ARGS. */
8319
8320 static int
8321 pack_expansion_args_count (tree args)
8322 {
8323 int i;
8324 int count = 0;
8325 if (args)
8326 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8327 {
8328 tree elt = TREE_VEC_ELT (args, i);
8329 if (elt && PACK_EXPANSION_P (elt))
8330 ++count;
8331 }
8332 return count;
8333 }
8334
8335 /* Convert all template arguments to their appropriate types, and
8336 return a vector containing the innermost resulting template
8337 arguments. If any error occurs, return error_mark_node. Error and
8338 warning messages are issued under control of COMPLAIN.
8339
8340 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8341 for arguments not specified in ARGS. Otherwise, if
8342 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8343 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8344 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8345 ARGS. */
8346
8347 static tree
8348 coerce_template_parms (tree parms,
8349 tree args,
8350 tree in_decl,
8351 tsubst_flags_t complain,
8352 bool require_all_args,
8353 bool use_default_args)
8354 {
8355 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8356 tree orig_inner_args;
8357 tree inner_args;
8358 tree new_args;
8359 tree new_inner_args;
8360
8361 /* When used as a boolean value, indicates whether this is a
8362 variadic template parameter list. Since it's an int, we can also
8363 subtract it from nparms to get the number of non-variadic
8364 parameters. */
8365 int variadic_p = 0;
8366 int variadic_args_p = 0;
8367 int post_variadic_parms = 0;
8368
8369 /* Adjustment to nparms for fixed parameter packs. */
8370 int fixed_pack_adjust = 0;
8371 int fixed_packs = 0;
8372 int missing = 0;
8373
8374 /* Likewise for parameters with default arguments. */
8375 int default_p = 0;
8376
8377 if (args == error_mark_node)
8378 return error_mark_node;
8379
8380 nparms = TREE_VEC_LENGTH (parms);
8381
8382 /* Determine if there are any parameter packs or default arguments. */
8383 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8384 {
8385 tree parm = TREE_VEC_ELT (parms, parm_idx);
8386 if (variadic_p)
8387 ++post_variadic_parms;
8388 if (template_parameter_pack_p (TREE_VALUE (parm)))
8389 ++variadic_p;
8390 if (TREE_PURPOSE (parm))
8391 ++default_p;
8392 }
8393
8394 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8395 /* If there are no parameters that follow a parameter pack, we need to
8396 expand any argument packs so that we can deduce a parameter pack from
8397 some non-packed args followed by an argument pack, as in variadic85.C.
8398 If there are such parameters, we need to leave argument packs intact
8399 so the arguments are assigned properly. This can happen when dealing
8400 with a nested class inside a partial specialization of a class
8401 template, as in variadic92.C, or when deducing a template parameter pack
8402 from a sub-declarator, as in variadic114.C. */
8403 if (!post_variadic_parms)
8404 inner_args = expand_template_argument_pack (inner_args);
8405
8406 /* Count any pack expansion args. */
8407 variadic_args_p = pack_expansion_args_count (inner_args);
8408
8409 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8410 if ((nargs - variadic_args_p > nparms && !variadic_p)
8411 || (nargs < nparms - variadic_p
8412 && require_all_args
8413 && !variadic_args_p
8414 && (!use_default_args
8415 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8416 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8417 {
8418 bad_nargs:
8419 if (complain & tf_error)
8420 {
8421 if (variadic_p || default_p)
8422 {
8423 nparms -= variadic_p + default_p;
8424 error ("wrong number of template arguments "
8425 "(%d, should be at least %d)", nargs, nparms);
8426 }
8427 else
8428 error ("wrong number of template arguments "
8429 "(%d, should be %d)", nargs, nparms);
8430
8431 if (in_decl)
8432 inform (DECL_SOURCE_LOCATION (in_decl),
8433 "provided for %qD", in_decl);
8434 }
8435
8436 return error_mark_node;
8437 }
8438 /* We can't pass a pack expansion to a non-pack parameter of an alias
8439 template (DR 1430). */
8440 else if (in_decl
8441 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8442 || concept_template_p (in_decl))
8443 && variadic_args_p
8444 && nargs - variadic_args_p < nparms - variadic_p)
8445 {
8446 if (complain & tf_error)
8447 {
8448 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8449 {
8450 tree arg = TREE_VEC_ELT (inner_args, i);
8451 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8452
8453 if (PACK_EXPANSION_P (arg)
8454 && !template_parameter_pack_p (parm))
8455 {
8456 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8457 error_at (location_of (arg),
8458 "pack expansion argument for non-pack parameter "
8459 "%qD of alias template %qD", parm, in_decl);
8460 else
8461 error_at (location_of (arg),
8462 "pack expansion argument for non-pack parameter "
8463 "%qD of concept %qD", parm, in_decl);
8464 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8465 goto found;
8466 }
8467 }
8468 gcc_unreachable ();
8469 found:;
8470 }
8471 return error_mark_node;
8472 }
8473
8474 /* We need to evaluate the template arguments, even though this
8475 template-id may be nested within a "sizeof". */
8476 cp_evaluated ev;
8477
8478 new_inner_args = make_tree_vec (nparms);
8479 new_args = add_outermost_template_args (args, new_inner_args);
8480 int pack_adjust = 0;
8481 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8482 {
8483 tree arg;
8484 tree parm;
8485
8486 /* Get the Ith template parameter. */
8487 parm = TREE_VEC_ELT (parms, parm_idx);
8488
8489 if (parm == error_mark_node)
8490 {
8491 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8492 continue;
8493 }
8494
8495 /* Calculate the next argument. */
8496 if (arg_idx < nargs)
8497 arg = TREE_VEC_ELT (inner_args, arg_idx);
8498 else
8499 arg = NULL_TREE;
8500
8501 if (template_parameter_pack_p (TREE_VALUE (parm))
8502 && (arg || require_all_args || !(complain & tf_partial))
8503 && !(arg && ARGUMENT_PACK_P (arg)))
8504 {
8505 /* Some arguments will be placed in the
8506 template parameter pack PARM. */
8507 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8508 inner_args, arg_idx,
8509 new_args, &lost,
8510 in_decl, complain);
8511
8512 if (arg == NULL_TREE)
8513 {
8514 /* We don't know how many args we have yet, just use the
8515 unconverted (and still packed) ones for now. */
8516 new_inner_args = orig_inner_args;
8517 arg_idx = nargs;
8518 break;
8519 }
8520
8521 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8522
8523 /* Store this argument. */
8524 if (arg == error_mark_node)
8525 {
8526 lost++;
8527 /* We are done with all of the arguments. */
8528 arg_idx = nargs;
8529 break;
8530 }
8531 else
8532 {
8533 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8534 arg_idx += pack_adjust;
8535 if (fixed_parameter_pack_p (TREE_VALUE (parm)))
8536 {
8537 ++fixed_packs;
8538 fixed_pack_adjust += pack_adjust;
8539 }
8540 }
8541
8542 continue;
8543 }
8544 else if (arg)
8545 {
8546 if (PACK_EXPANSION_P (arg))
8547 {
8548 /* "If every valid specialization of a variadic template
8549 requires an empty template parameter pack, the template is
8550 ill-formed, no diagnostic required." So check that the
8551 pattern works with this parameter. */
8552 tree pattern = PACK_EXPANSION_PATTERN (arg);
8553 tree conv = convert_template_argument (TREE_VALUE (parm),
8554 pattern, new_args,
8555 complain, parm_idx,
8556 in_decl);
8557 if (conv == error_mark_node)
8558 {
8559 if (complain & tf_error)
8560 inform (input_location, "so any instantiation with a "
8561 "non-empty parameter pack would be ill-formed");
8562 ++lost;
8563 }
8564 else if (TYPE_P (conv) && !TYPE_P (pattern))
8565 /* Recover from missing typename. */
8566 TREE_VEC_ELT (inner_args, arg_idx)
8567 = make_pack_expansion (conv, complain);
8568
8569 /* We don't know how many args we have yet, just
8570 use the unconverted ones for now. */
8571 new_inner_args = inner_args;
8572 arg_idx = nargs;
8573 break;
8574 }
8575 }
8576 else if (require_all_args)
8577 {
8578 /* There must be a default arg in this case. */
8579 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8580 complain, in_decl);
8581 /* The position of the first default template argument,
8582 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8583 Record that. */
8584 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8585 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8586 arg_idx - pack_adjust);
8587 }
8588 else
8589 break;
8590
8591 if (arg == error_mark_node)
8592 {
8593 if (complain & tf_error)
8594 error ("template argument %d is invalid", arg_idx + 1);
8595 }
8596 else if (!arg)
8597 {
8598 /* This can occur if there was an error in the template
8599 parameter list itself (which we would already have
8600 reported) that we are trying to recover from, e.g., a class
8601 template with a parameter list such as
8602 template<typename..., typename> (cpp0x/variadic150.C). */
8603 ++lost;
8604
8605 /* This can also happen with a fixed parameter pack (71834). */
8606 if (arg_idx >= nargs)
8607 ++missing;
8608 }
8609 else
8610 arg = convert_template_argument (TREE_VALUE (parm),
8611 arg, new_args, complain,
8612 parm_idx, in_decl);
8613
8614 if (arg == error_mark_node)
8615 lost++;
8616 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8617 }
8618
8619 if (missing || arg_idx < nargs - variadic_args_p)
8620 {
8621 /* If we had fixed parameter packs, we didn't know how many arguments we
8622 actually needed earlier; now we do. */
8623 nparms += fixed_pack_adjust;
8624 variadic_p -= fixed_packs;
8625 goto bad_nargs;
8626 }
8627
8628 if (arg_idx < nargs)
8629 {
8630 /* We had some pack expansion arguments that will only work if the packs
8631 are empty, but wait until instantiation time to complain.
8632 See variadic-ttp3.C. */
8633 int len = nparms + (nargs - arg_idx);
8634 tree args = make_tree_vec (len);
8635 int i = 0;
8636 for (; i < nparms; ++i)
8637 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (new_inner_args, i);
8638 for (; i < len; ++i, ++arg_idx)
8639 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (inner_args,
8640 arg_idx - pack_adjust);
8641 new_inner_args = args;
8642 }
8643
8644 if (lost)
8645 {
8646 gcc_assert (!(complain & tf_error) || seen_error ());
8647 return error_mark_node;
8648 }
8649
8650 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8651 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8652 TREE_VEC_LENGTH (new_inner_args));
8653
8654 return new_inner_args;
8655 }
8656
8657 /* Convert all template arguments to their appropriate types, and
8658 return a vector containing the innermost resulting template
8659 arguments. If any error occurs, return error_mark_node. Error and
8660 warning messages are not issued.
8661
8662 Note that no function argument deduction is performed, and default
8663 arguments are used to fill in unspecified arguments. */
8664 tree
8665 coerce_template_parms (tree parms, tree args, tree in_decl)
8666 {
8667 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8668 }
8669
8670 /* Convert all template arguments to their appropriate type, and
8671 instantiate default arguments as needed. This returns a vector
8672 containing the innermost resulting template arguments, or
8673 error_mark_node if unsuccessful. */
8674 tree
8675 coerce_template_parms (tree parms, tree args, tree in_decl,
8676 tsubst_flags_t complain)
8677 {
8678 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8679 }
8680
8681 /* Like coerce_template_parms. If PARMS represents all template
8682 parameters levels, this function returns a vector of vectors
8683 representing all the resulting argument levels. Note that in this
8684 case, only the innermost arguments are coerced because the
8685 outermost ones are supposed to have been coerced already.
8686
8687 Otherwise, if PARMS represents only (the innermost) vector of
8688 parameters, this function returns a vector containing just the
8689 innermost resulting arguments. */
8690
8691 static tree
8692 coerce_innermost_template_parms (tree parms,
8693 tree args,
8694 tree in_decl,
8695 tsubst_flags_t complain,
8696 bool require_all_args,
8697 bool use_default_args)
8698 {
8699 int parms_depth = TMPL_PARMS_DEPTH (parms);
8700 int args_depth = TMPL_ARGS_DEPTH (args);
8701 tree coerced_args;
8702
8703 if (parms_depth > 1)
8704 {
8705 coerced_args = make_tree_vec (parms_depth);
8706 tree level;
8707 int cur_depth;
8708
8709 for (level = parms, cur_depth = parms_depth;
8710 parms_depth > 0 && level != NULL_TREE;
8711 level = TREE_CHAIN (level), --cur_depth)
8712 {
8713 tree l;
8714 if (cur_depth == args_depth)
8715 l = coerce_template_parms (TREE_VALUE (level),
8716 args, in_decl, complain,
8717 require_all_args,
8718 use_default_args);
8719 else
8720 l = TMPL_ARGS_LEVEL (args, cur_depth);
8721
8722 if (l == error_mark_node)
8723 return error_mark_node;
8724
8725 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8726 }
8727 }
8728 else
8729 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8730 args, in_decl, complain,
8731 require_all_args,
8732 use_default_args);
8733 return coerced_args;
8734 }
8735
8736 /* Returns 1 if template args OT and NT are equivalent. */
8737
8738 int
8739 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
8740 {
8741 if (nt == ot)
8742 return 1;
8743 if (nt == NULL_TREE || ot == NULL_TREE)
8744 return false;
8745 if (nt == any_targ_node || ot == any_targ_node)
8746 return true;
8747
8748 if (TREE_CODE (nt) == TREE_VEC)
8749 /* For member templates */
8750 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8751 else if (PACK_EXPANSION_P (ot))
8752 return (PACK_EXPANSION_P (nt)
8753 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8754 PACK_EXPANSION_PATTERN (nt))
8755 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8756 PACK_EXPANSION_EXTRA_ARGS (nt)));
8757 else if (ARGUMENT_PACK_P (ot))
8758 {
8759 int i, len;
8760 tree opack, npack;
8761
8762 if (!ARGUMENT_PACK_P (nt))
8763 return 0;
8764
8765 opack = ARGUMENT_PACK_ARGS (ot);
8766 npack = ARGUMENT_PACK_ARGS (nt);
8767 len = TREE_VEC_LENGTH (opack);
8768 if (TREE_VEC_LENGTH (npack) != len)
8769 return 0;
8770 for (i = 0; i < len; ++i)
8771 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8772 TREE_VEC_ELT (npack, i)))
8773 return 0;
8774 return 1;
8775 }
8776 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8777 gcc_unreachable ();
8778 else if (TYPE_P (nt))
8779 {
8780 if (!TYPE_P (ot))
8781 return false;
8782 /* Don't treat an alias template specialization with dependent
8783 arguments as equivalent to its underlying type when used as a
8784 template argument; we need them to be distinct so that we
8785 substitute into the specialization arguments at instantiation
8786 time. And aliases can't be equivalent without being ==, so
8787 we don't need to look any deeper.
8788
8789 During partial ordering, however, we need to treat them normally so
8790 that we can order uses of the same alias with different
8791 cv-qualification (79960). */
8792 if (!partial_order
8793 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
8794 return false;
8795 else
8796 return same_type_p (ot, nt);
8797 }
8798 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8799 return 0;
8800 else
8801 {
8802 /* Try to treat a template non-type argument that has been converted
8803 to the parameter type as equivalent to one that hasn't yet. */
8804 for (enum tree_code code1 = TREE_CODE (ot);
8805 CONVERT_EXPR_CODE_P (code1)
8806 || code1 == NON_LVALUE_EXPR;
8807 code1 = TREE_CODE (ot))
8808 ot = TREE_OPERAND (ot, 0);
8809 for (enum tree_code code2 = TREE_CODE (nt);
8810 CONVERT_EXPR_CODE_P (code2)
8811 || code2 == NON_LVALUE_EXPR;
8812 code2 = TREE_CODE (nt))
8813 nt = TREE_OPERAND (nt, 0);
8814
8815 return cp_tree_equal (ot, nt);
8816 }
8817 }
8818
8819 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8820 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8821 NEWARG_PTR with the offending arguments if they are non-NULL. */
8822
8823 int
8824 comp_template_args (tree oldargs, tree newargs,
8825 tree *oldarg_ptr, tree *newarg_ptr,
8826 bool partial_order)
8827 {
8828 int i;
8829
8830 if (oldargs == newargs)
8831 return 1;
8832
8833 if (!oldargs || !newargs)
8834 return 0;
8835
8836 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
8837 return 0;
8838
8839 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
8840 {
8841 tree nt = TREE_VEC_ELT (newargs, i);
8842 tree ot = TREE_VEC_ELT (oldargs, i);
8843
8844 if (! template_args_equal (ot, nt, partial_order))
8845 {
8846 if (oldarg_ptr != NULL)
8847 *oldarg_ptr = ot;
8848 if (newarg_ptr != NULL)
8849 *newarg_ptr = nt;
8850 return 0;
8851 }
8852 }
8853 return 1;
8854 }
8855
8856 inline bool
8857 comp_template_args_porder (tree oargs, tree nargs)
8858 {
8859 return comp_template_args (oargs, nargs, NULL, NULL, true);
8860 }
8861
8862 /* Implement a freelist interface for objects of type T.
8863
8864 Head is a separate object, rather than a regular member, so that we
8865 can define it as a GTY deletable pointer, which is highly
8866 desirable. A data member could be declared that way, but then the
8867 containing object would implicitly get GTY((user)), which would
8868 prevent us from instantiating freelists as global objects.
8869 Although this way we can create freelist global objects, they're
8870 such thin wrappers that instantiating temporaries at every use
8871 loses nothing and saves permanent storage for the freelist object.
8872
8873 Member functions next, anew, poison and reinit have default
8874 implementations that work for most of the types we're interested
8875 in, but if they don't work for some type, they should be explicitly
8876 specialized. See the comments before them for requirements, and
8877 the example specializations for the tree_list_freelist. */
8878 template <typename T>
8879 class freelist
8880 {
8881 /* Return the next object in a chain. We could just do type
8882 punning, but if we access the object with its underlying type, we
8883 avoid strict-aliasing trouble. This needs only work between
8884 poison and reinit. */
8885 static T *&next (T *obj) { return obj->next; }
8886
8887 /* Return a newly allocated, uninitialized or minimally-initialized
8888 object of type T. Any initialization performed by anew should
8889 either remain across the life of the object and the execution of
8890 poison, or be redone by reinit. */
8891 static T *anew () { return ggc_alloc<T> (); }
8892
8893 /* Optionally scribble all over the bits holding the object, so that
8894 they become (mostly?) uninitialized memory. This is called while
8895 preparing to make the object part of the free list. */
8896 static void poison (T *obj) {
8897 T *p ATTRIBUTE_UNUSED = obj;
8898 T **q ATTRIBUTE_UNUSED = &next (obj);
8899
8900 #ifdef ENABLE_GC_CHECKING
8901 /* Poison the data, to indicate the data is garbage. */
8902 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, sizeof (*p)));
8903 memset (p, 0xa5, sizeof (*p));
8904 #endif
8905 /* Let valgrind know the object is free. */
8906 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, sizeof (*p)));
8907
8908 /* Let valgrind know the next portion of the object is available,
8909 but uninitialized. */
8910 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
8911 }
8912
8913 /* Bring an object that underwent at least one lifecycle after anew
8914 and before the most recent free and poison, back to a usable
8915 state, reinitializing whatever is needed for it to be
8916 functionally equivalent to an object just allocated and returned
8917 by anew. This may poison or clear the next field, used by
8918 freelist housekeeping after poison was called. */
8919 static void reinit (T *obj) {
8920 T **q ATTRIBUTE_UNUSED = &next (obj);
8921
8922 #ifdef ENABLE_GC_CHECKING
8923 memset (q, 0xa5, sizeof (*q));
8924 #endif
8925 /* Let valgrind know the entire object is available, but
8926 uninitialized. */
8927 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (*obj)));
8928 }
8929
8930 /* Reference a GTY-deletable pointer that points to the first object
8931 in the free list proper. */
8932 T *&head;
8933 public:
8934 /* Construct a freelist object chaining objects off of HEAD. */
8935 freelist (T *&head) : head(head) {}
8936
8937 /* Add OBJ to the free object list. The former head becomes OBJ's
8938 successor. */
8939 void free (T *obj)
8940 {
8941 poison (obj);
8942 next (obj) = head;
8943 head = obj;
8944 }
8945
8946 /* Take an object from the free list, if one is available, or
8947 allocate a new one. Objects taken from the free list should be
8948 regarded as filled with garbage, except for bits that are
8949 configured to be preserved across free and alloc. */
8950 T *alloc ()
8951 {
8952 if (head)
8953 {
8954 T *obj = head;
8955 head = next (head);
8956 reinit (obj);
8957 return obj;
8958 }
8959 else
8960 return anew ();
8961 }
8962 };
8963
8964 /* Explicitly specialize the interfaces for freelist<tree_node>: we
8965 want to allocate a TREE_LIST using the usual interface, and ensure
8966 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
8967 build_tree_list logic in reinit, so this could go out of sync. */
8968 template <>
8969 inline tree &
8970 freelist<tree_node>::next (tree obj)
8971 {
8972 return TREE_CHAIN (obj);
8973 }
8974 template <>
8975 inline tree
8976 freelist<tree_node>::anew ()
8977 {
8978 return build_tree_list (NULL, NULL);
8979 }
8980 template <>
8981 inline void
8982 freelist<tree_node>::poison (tree obj ATTRIBUTE_UNUSED)
8983 {
8984 int size ATTRIBUTE_UNUSED = sizeof (tree_list);
8985 tree p ATTRIBUTE_UNUSED = obj;
8986 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
8987 tree *q ATTRIBUTE_UNUSED = &next (obj);
8988
8989 #ifdef ENABLE_GC_CHECKING
8990 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
8991
8992 /* Poison the data, to indicate the data is garbage. */
8993 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, size));
8994 memset (p, 0xa5, size);
8995 #endif
8996 /* Let valgrind know the object is free. */
8997 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, size));
8998 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
8999 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9000 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9001
9002 #ifdef ENABLE_GC_CHECKING
9003 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b, sizeof (*b)));
9004 /* Keep TREE_CHAIN functional. */
9005 TREE_SET_CODE (obj, TREE_LIST);
9006 #else
9007 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9008 #endif
9009 }
9010 template <>
9011 inline void
9012 freelist<tree_node>::reinit (tree obj ATTRIBUTE_UNUSED)
9013 {
9014 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9015
9016 #ifdef ENABLE_GC_CHECKING
9017 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9018 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9019 memset (obj, 0, sizeof (tree_list));
9020 #endif
9021
9022 /* Let valgrind know the entire object is available, but
9023 uninitialized. */
9024 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9025
9026 #ifdef ENABLE_GC_CHECKING
9027 TREE_SET_CODE (obj, TREE_LIST);
9028 #else
9029 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9030 #endif
9031 }
9032
9033 /* Point to the first object in the TREE_LIST freelist. */
9034 static GTY((deletable)) tree tree_list_freelist_head;
9035 /* Return the/an actual TREE_LIST freelist. */
9036 static inline freelist<tree_node>
9037 tree_list_freelist ()
9038 {
9039 return tree_list_freelist_head;
9040 }
9041
9042 /* Point to the first object in the tinst_level freelist. */
9043 static GTY((deletable)) tinst_level *tinst_level_freelist_head;
9044 /* Return the/an actual tinst_level freelist. */
9045 static inline freelist<tinst_level>
9046 tinst_level_freelist ()
9047 {
9048 return tinst_level_freelist_head;
9049 }
9050
9051 /* Point to the first object in the pending_template freelist. */
9052 static GTY((deletable)) pending_template *pending_template_freelist_head;
9053 /* Return the/an actual pending_template freelist. */
9054 static inline freelist<pending_template>
9055 pending_template_freelist ()
9056 {
9057 return pending_template_freelist_head;
9058 }
9059
9060 /* Build the TREE_LIST object out of a split list, store it
9061 permanently, and return it. */
9062 tree
9063 tinst_level::to_list ()
9064 {
9065 gcc_assert (split_list_p ());
9066 tree ret = tree_list_freelist ().alloc ();
9067 TREE_PURPOSE (ret) = tldcl;
9068 TREE_VALUE (ret) = targs;
9069 tldcl = ret;
9070 targs = NULL;
9071 gcc_assert (tree_list_p ());
9072 return ret;
9073 }
9074
9075 const unsigned short tinst_level::refcount_infinity;
9076
9077 /* Increment OBJ's refcount unless it is already infinite. */
9078 static tinst_level *
9079 inc_refcount_use (tinst_level *obj)
9080 {
9081 if (obj && obj->refcount != tinst_level::refcount_infinity)
9082 ++obj->refcount;
9083 return obj;
9084 }
9085
9086 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9087 void
9088 tinst_level::free (tinst_level *obj)
9089 {
9090 if (obj->tree_list_p ())
9091 tree_list_freelist ().free (obj->get_node ());
9092 tinst_level_freelist ().free (obj);
9093 }
9094
9095 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9096 OBJ's DECL and OBJ, and start over with the tinst_level object that
9097 used to be referenced by OBJ's NEXT. */
9098 static void
9099 dec_refcount_use (tinst_level *obj)
9100 {
9101 while (obj
9102 && obj->refcount != tinst_level::refcount_infinity
9103 && !--obj->refcount)
9104 {
9105 tinst_level *next = obj->next;
9106 tinst_level::free (obj);
9107 obj = next;
9108 }
9109 }
9110
9111 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9112 and of the former PTR. Omitting the second argument is equivalent
9113 to passing (T*)NULL; this is allowed because passing the
9114 zero-valued integral constant NULL confuses type deduction and/or
9115 overload resolution. */
9116 template <typename T>
9117 static void
9118 set_refcount_ptr (T *& ptr, T *obj = NULL)
9119 {
9120 T *save = ptr;
9121 ptr = inc_refcount_use (obj);
9122 dec_refcount_use (save);
9123 }
9124
9125 static void
9126 add_pending_template (tree d)
9127 {
9128 tree ti = (TYPE_P (d)
9129 ? CLASSTYPE_TEMPLATE_INFO (d)
9130 : DECL_TEMPLATE_INFO (d));
9131 struct pending_template *pt;
9132 int level;
9133
9134 if (TI_PENDING_TEMPLATE_FLAG (ti))
9135 return;
9136
9137 /* We are called both from instantiate_decl, where we've already had a
9138 tinst_level pushed, and instantiate_template, where we haven't.
9139 Compensate. */
9140 gcc_assert (TREE_CODE (d) != TREE_LIST);
9141 level = !current_tinst_level
9142 || current_tinst_level->maybe_get_node () != d;
9143
9144 if (level)
9145 push_tinst_level (d);
9146
9147 pt = pending_template_freelist ().alloc ();
9148 pt->next = NULL;
9149 pt->tinst = NULL;
9150 set_refcount_ptr (pt->tinst, current_tinst_level);
9151 if (last_pending_template)
9152 last_pending_template->next = pt;
9153 else
9154 pending_templates = pt;
9155
9156 last_pending_template = pt;
9157
9158 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
9159
9160 if (level)
9161 pop_tinst_level ();
9162 }
9163
9164
9165 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9166 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9167 documentation for TEMPLATE_ID_EXPR. */
9168
9169 tree
9170 lookup_template_function (tree fns, tree arglist)
9171 {
9172 if (fns == error_mark_node || arglist == error_mark_node)
9173 return error_mark_node;
9174
9175 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
9176
9177 if (!is_overloaded_fn (fns) && !identifier_p (fns))
9178 {
9179 error ("%q#D is not a function template", fns);
9180 return error_mark_node;
9181 }
9182
9183 if (BASELINK_P (fns))
9184 {
9185 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
9186 unknown_type_node,
9187 BASELINK_FUNCTIONS (fns),
9188 arglist);
9189 return fns;
9190 }
9191
9192 return build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, arglist);
9193 }
9194
9195 /* Within the scope of a template class S<T>, the name S gets bound
9196 (in build_self_reference) to a TYPE_DECL for the class, not a
9197 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9198 or one of its enclosing classes, and that type is a template,
9199 return the associated TEMPLATE_DECL. Otherwise, the original
9200 DECL is returned.
9201
9202 Also handle the case when DECL is a TREE_LIST of ambiguous
9203 injected-class-names from different bases. */
9204
9205 tree
9206 maybe_get_template_decl_from_type_decl (tree decl)
9207 {
9208 if (decl == NULL_TREE)
9209 return decl;
9210
9211 /* DR 176: A lookup that finds an injected-class-name (10.2
9212 [class.member.lookup]) can result in an ambiguity in certain cases
9213 (for example, if it is found in more than one base class). If all of
9214 the injected-class-names that are found refer to specializations of
9215 the same class template, and if the name is followed by a
9216 template-argument-list, the reference refers to the class template
9217 itself and not a specialization thereof, and is not ambiguous. */
9218 if (TREE_CODE (decl) == TREE_LIST)
9219 {
9220 tree t, tmpl = NULL_TREE;
9221 for (t = decl; t; t = TREE_CHAIN (t))
9222 {
9223 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
9224 if (!tmpl)
9225 tmpl = elt;
9226 else if (tmpl != elt)
9227 break;
9228 }
9229 if (tmpl && t == NULL_TREE)
9230 return tmpl;
9231 else
9232 return decl;
9233 }
9234
9235 return (decl != NULL_TREE
9236 && DECL_SELF_REFERENCE_P (decl)
9237 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
9238 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
9239 }
9240
9241 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9242 parameters, find the desired type.
9243
9244 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9245
9246 IN_DECL, if non-NULL, is the template declaration we are trying to
9247 instantiate.
9248
9249 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9250 the class we are looking up.
9251
9252 Issue error and warning messages under control of COMPLAIN.
9253
9254 If the template class is really a local class in a template
9255 function, then the FUNCTION_CONTEXT is the function in which it is
9256 being instantiated.
9257
9258 ??? Note that this function is currently called *twice* for each
9259 template-id: the first time from the parser, while creating the
9260 incomplete type (finish_template_type), and the second type during the
9261 real instantiation (instantiate_template_class). This is surely something
9262 that we want to avoid. It also causes some problems with argument
9263 coercion (see convert_nontype_argument for more information on this). */
9264
9265 static tree
9266 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
9267 int entering_scope, tsubst_flags_t complain)
9268 {
9269 tree templ = NULL_TREE, parmlist;
9270 tree t;
9271 spec_entry **slot;
9272 spec_entry *entry;
9273 spec_entry elt;
9274 hashval_t hash;
9275
9276 if (identifier_p (d1))
9277 {
9278 tree value = innermost_non_namespace_value (d1);
9279 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
9280 templ = value;
9281 else
9282 {
9283 if (context)
9284 push_decl_namespace (context);
9285 templ = lookup_name (d1);
9286 templ = maybe_get_template_decl_from_type_decl (templ);
9287 if (context)
9288 pop_decl_namespace ();
9289 }
9290 if (templ)
9291 context = DECL_CONTEXT (templ);
9292 }
9293 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
9294 {
9295 tree type = TREE_TYPE (d1);
9296
9297 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9298 an implicit typename for the second A. Deal with it. */
9299 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
9300 type = TREE_TYPE (type);
9301
9302 if (CLASSTYPE_TEMPLATE_INFO (type))
9303 {
9304 templ = CLASSTYPE_TI_TEMPLATE (type);
9305 d1 = DECL_NAME (templ);
9306 }
9307 }
9308 else if (TREE_CODE (d1) == ENUMERAL_TYPE
9309 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
9310 {
9311 templ = TYPE_TI_TEMPLATE (d1);
9312 d1 = DECL_NAME (templ);
9313 }
9314 else if (DECL_TYPE_TEMPLATE_P (d1))
9315 {
9316 templ = d1;
9317 d1 = DECL_NAME (templ);
9318 context = DECL_CONTEXT (templ);
9319 }
9320 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
9321 {
9322 templ = d1;
9323 d1 = DECL_NAME (templ);
9324 }
9325
9326 /* Issue an error message if we didn't find a template. */
9327 if (! templ)
9328 {
9329 if (complain & tf_error)
9330 error ("%qT is not a template", d1);
9331 return error_mark_node;
9332 }
9333
9334 if (TREE_CODE (templ) != TEMPLATE_DECL
9335 /* Make sure it's a user visible template, if it was named by
9336 the user. */
9337 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
9338 && !PRIMARY_TEMPLATE_P (templ)))
9339 {
9340 if (complain & tf_error)
9341 {
9342 error ("non-template type %qT used as a template", d1);
9343 if (in_decl)
9344 error ("for template declaration %q+D", in_decl);
9345 }
9346 return error_mark_node;
9347 }
9348
9349 complain &= ~tf_user;
9350
9351 /* An alias that just changes the name of a template is equivalent to the
9352 other template, so if any of the arguments are pack expansions, strip
9353 the alias to avoid problems with a pack expansion passed to a non-pack
9354 alias template parameter (DR 1430). */
9355 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
9356 templ = get_underlying_template (templ);
9357
9358 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
9359 {
9360 tree parm;
9361 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
9362 if (arglist2 == error_mark_node
9363 || (!uses_template_parms (arglist2)
9364 && check_instantiated_args (templ, arglist2, complain)))
9365 return error_mark_node;
9366
9367 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
9368 return parm;
9369 }
9370 else
9371 {
9372 tree template_type = TREE_TYPE (templ);
9373 tree gen_tmpl;
9374 tree type_decl;
9375 tree found = NULL_TREE;
9376 int arg_depth;
9377 int parm_depth;
9378 int is_dependent_type;
9379 int use_partial_inst_tmpl = false;
9380
9381 if (template_type == error_mark_node)
9382 /* An error occurred while building the template TEMPL, and a
9383 diagnostic has most certainly been emitted for that
9384 already. Let's propagate that error. */
9385 return error_mark_node;
9386
9387 gen_tmpl = most_general_template (templ);
9388 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
9389 parm_depth = TMPL_PARMS_DEPTH (parmlist);
9390 arg_depth = TMPL_ARGS_DEPTH (arglist);
9391
9392 if (arg_depth == 1 && parm_depth > 1)
9393 {
9394 /* We've been given an incomplete set of template arguments.
9395 For example, given:
9396
9397 template <class T> struct S1 {
9398 template <class U> struct S2 {};
9399 template <class U> struct S2<U*> {};
9400 };
9401
9402 we will be called with an ARGLIST of `U*', but the
9403 TEMPLATE will be `template <class T> template
9404 <class U> struct S1<T>::S2'. We must fill in the missing
9405 arguments. */
9406 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
9407 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
9408 arg_depth = TMPL_ARGS_DEPTH (arglist);
9409 }
9410
9411 /* Now we should have enough arguments. */
9412 gcc_assert (parm_depth == arg_depth);
9413
9414 /* From here on, we're only interested in the most general
9415 template. */
9416
9417 /* Calculate the BOUND_ARGS. These will be the args that are
9418 actually tsubst'd into the definition to create the
9419 instantiation. */
9420 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
9421 complain,
9422 /*require_all_args=*/true,
9423 /*use_default_args=*/true);
9424
9425 if (arglist == error_mark_node)
9426 /* We were unable to bind the arguments. */
9427 return error_mark_node;
9428
9429 /* In the scope of a template class, explicit references to the
9430 template class refer to the type of the template, not any
9431 instantiation of it. For example, in:
9432
9433 template <class T> class C { void f(C<T>); }
9434
9435 the `C<T>' is just the same as `C'. Outside of the
9436 class, however, such a reference is an instantiation. */
9437 if (entering_scope
9438 || !PRIMARY_TEMPLATE_P (gen_tmpl)
9439 || currently_open_class (template_type))
9440 {
9441 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
9442
9443 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
9444 return template_type;
9445 }
9446
9447 /* If we already have this specialization, return it. */
9448 elt.tmpl = gen_tmpl;
9449 elt.args = arglist;
9450 elt.spec = NULL_TREE;
9451 hash = spec_hasher::hash (&elt);
9452 entry = type_specializations->find_with_hash (&elt, hash);
9453
9454 if (entry)
9455 return entry->spec;
9456
9457 /* If the the template's constraints are not satisfied,
9458 then we cannot form a valid type.
9459
9460 Note that the check is deferred until after the hash
9461 lookup. This prevents redundant checks on previously
9462 instantiated specializations. */
9463 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
9464 {
9465 if (complain & tf_error)
9466 {
9467 auto_diagnostic_group d;
9468 error ("template constraint failure");
9469 diagnose_constraints (input_location, gen_tmpl, arglist);
9470 }
9471 return error_mark_node;
9472 }
9473
9474 is_dependent_type = uses_template_parms (arglist);
9475
9476 /* If the deduced arguments are invalid, then the binding
9477 failed. */
9478 if (!is_dependent_type
9479 && check_instantiated_args (gen_tmpl,
9480 INNERMOST_TEMPLATE_ARGS (arglist),
9481 complain))
9482 return error_mark_node;
9483
9484 if (!is_dependent_type
9485 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9486 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
9487 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
9488 {
9489 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
9490 DECL_NAME (gen_tmpl),
9491 /*tag_scope=*/ts_global);
9492 return found;
9493 }
9494
9495 context = DECL_CONTEXT (gen_tmpl);
9496 if (context && TYPE_P (context))
9497 {
9498 context = tsubst_aggr_type (context, arglist, complain, in_decl, true);
9499 context = complete_type (context);
9500 }
9501 else
9502 context = tsubst (context, arglist, complain, in_decl);
9503
9504 if (context == error_mark_node)
9505 return error_mark_node;
9506
9507 if (!context)
9508 context = global_namespace;
9509
9510 /* Create the type. */
9511 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9512 {
9513 /* The user referred to a specialization of an alias
9514 template represented by GEN_TMPL.
9515
9516 [temp.alias]/2 says:
9517
9518 When a template-id refers to the specialization of an
9519 alias template, it is equivalent to the associated
9520 type obtained by substitution of its
9521 template-arguments for the template-parameters in the
9522 type-id of the alias template. */
9523
9524 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
9525 /* Note that the call above (by indirectly calling
9526 register_specialization in tsubst_decl) registers the
9527 TYPE_DECL representing the specialization of the alias
9528 template. So next time someone substitutes ARGLIST for
9529 the template parms into the alias template (GEN_TMPL),
9530 she'll get that TYPE_DECL back. */
9531
9532 if (t == error_mark_node)
9533 return t;
9534 }
9535 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
9536 {
9537 if (!is_dependent_type)
9538 {
9539 set_current_access_from_decl (TYPE_NAME (template_type));
9540 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
9541 tsubst (ENUM_UNDERLYING_TYPE (template_type),
9542 arglist, complain, in_decl),
9543 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
9544 arglist, complain, in_decl),
9545 SCOPED_ENUM_P (template_type), NULL);
9546
9547 if (t == error_mark_node)
9548 return t;
9549 }
9550 else
9551 {
9552 /* We don't want to call start_enum for this type, since
9553 the values for the enumeration constants may involve
9554 template parameters. And, no one should be interested
9555 in the enumeration constants for such a type. */
9556 t = cxx_make_type (ENUMERAL_TYPE);
9557 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
9558 }
9559 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
9560 ENUM_FIXED_UNDERLYING_TYPE_P (t)
9561 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
9562 }
9563 else if (CLASS_TYPE_P (template_type))
9564 {
9565 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9566 instantiated here. */
9567 gcc_assert (!LAMBDA_TYPE_P (template_type));
9568
9569 t = make_class_type (TREE_CODE (template_type));
9570 CLASSTYPE_DECLARED_CLASS (t)
9571 = CLASSTYPE_DECLARED_CLASS (template_type);
9572 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
9573
9574 /* A local class. Make sure the decl gets registered properly. */
9575 if (context == current_function_decl)
9576 if (pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current)
9577 == error_mark_node)
9578 return error_mark_node;
9579
9580 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
9581 /* This instantiation is another name for the primary
9582 template type. Set the TYPE_CANONICAL field
9583 appropriately. */
9584 TYPE_CANONICAL (t) = template_type;
9585 else if (any_template_arguments_need_structural_equality_p (arglist))
9586 /* Some of the template arguments require structural
9587 equality testing, so this template class requires
9588 structural equality testing. */
9589 SET_TYPE_STRUCTURAL_EQUALITY (t);
9590 }
9591 else
9592 gcc_unreachable ();
9593
9594 /* If we called start_enum or pushtag above, this information
9595 will already be set up. */
9596 if (!TYPE_NAME (t))
9597 {
9598 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
9599
9600 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
9601 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
9602 DECL_SOURCE_LOCATION (type_decl)
9603 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
9604 }
9605 else
9606 type_decl = TYPE_NAME (t);
9607
9608 if (CLASS_TYPE_P (template_type))
9609 {
9610 TREE_PRIVATE (type_decl)
9611 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
9612 TREE_PROTECTED (type_decl)
9613 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
9614 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
9615 {
9616 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
9617 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
9618 }
9619 }
9620
9621 if (OVERLOAD_TYPE_P (t)
9622 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9623 {
9624 static const char *tags[] = {"abi_tag", "may_alias"};
9625
9626 for (unsigned ix = 0; ix != 2; ix++)
9627 {
9628 tree attributes
9629 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
9630
9631 if (attributes)
9632 TYPE_ATTRIBUTES (t)
9633 = tree_cons (TREE_PURPOSE (attributes),
9634 TREE_VALUE (attributes),
9635 TYPE_ATTRIBUTES (t));
9636 }
9637 }
9638
9639 /* Let's consider the explicit specialization of a member
9640 of a class template specialization that is implicitly instantiated,
9641 e.g.:
9642 template<class T>
9643 struct S
9644 {
9645 template<class U> struct M {}; //#0
9646 };
9647
9648 template<>
9649 template<>
9650 struct S<int>::M<char> //#1
9651 {
9652 int i;
9653 };
9654 [temp.expl.spec]/4 says this is valid.
9655
9656 In this case, when we write:
9657 S<int>::M<char> m;
9658
9659 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9660 the one of #0.
9661
9662 When we encounter #1, we want to store the partial instantiation
9663 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9664
9665 For all cases other than this "explicit specialization of member of a
9666 class template", we just want to store the most general template into
9667 the CLASSTYPE_TI_TEMPLATE of M.
9668
9669 This case of "explicit specialization of member of a class template"
9670 only happens when:
9671 1/ the enclosing class is an instantiation of, and therefore not
9672 the same as, the context of the most general template, and
9673 2/ we aren't looking at the partial instantiation itself, i.e.
9674 the innermost arguments are not the same as the innermost parms of
9675 the most general template.
9676
9677 So it's only when 1/ and 2/ happens that we want to use the partial
9678 instantiation of the member template in lieu of its most general
9679 template. */
9680
9681 if (PRIMARY_TEMPLATE_P (gen_tmpl)
9682 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
9683 /* the enclosing class must be an instantiation... */
9684 && CLASS_TYPE_P (context)
9685 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
9686 {
9687 TREE_VEC_LENGTH (arglist)--;
9688 ++processing_template_decl;
9689 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
9690 tree partial_inst_args =
9691 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
9692 arglist, complain, NULL_TREE);
9693 --processing_template_decl;
9694 TREE_VEC_LENGTH (arglist)++;
9695 if (partial_inst_args == error_mark_node)
9696 return error_mark_node;
9697 use_partial_inst_tmpl =
9698 /*...and we must not be looking at the partial instantiation
9699 itself. */
9700 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
9701 partial_inst_args);
9702 }
9703
9704 if (!use_partial_inst_tmpl)
9705 /* This case is easy; there are no member templates involved. */
9706 found = gen_tmpl;
9707 else
9708 {
9709 /* This is a full instantiation of a member template. Find
9710 the partial instantiation of which this is an instance. */
9711
9712 /* Temporarily reduce by one the number of levels in the ARGLIST
9713 so as to avoid comparing the last set of arguments. */
9714 TREE_VEC_LENGTH (arglist)--;
9715 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
9716 TREE_VEC_LENGTH (arglist)++;
9717 /* FOUND is either a proper class type, or an alias
9718 template specialization. In the later case, it's a
9719 TYPE_DECL, resulting from the substituting of arguments
9720 for parameters in the TYPE_DECL of the alias template
9721 done earlier. So be careful while getting the template
9722 of FOUND. */
9723 found = (TREE_CODE (found) == TEMPLATE_DECL
9724 ? found
9725 : (TREE_CODE (found) == TYPE_DECL
9726 ? DECL_TI_TEMPLATE (found)
9727 : CLASSTYPE_TI_TEMPLATE (found)));
9728
9729 if (DECL_CLASS_TEMPLATE_P (found)
9730 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found)))
9731 {
9732 /* If this partial instantiation is specialized, we want to
9733 use it for hash table lookup. */
9734 elt.tmpl = found;
9735 elt.args = arglist = INNERMOST_TEMPLATE_ARGS (arglist);
9736 hash = spec_hasher::hash (&elt);
9737 }
9738 }
9739
9740 // Build template info for the new specialization.
9741 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
9742
9743 elt.spec = t;
9744 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
9745 gcc_checking_assert (*slot == NULL);
9746 entry = ggc_alloc<spec_entry> ();
9747 *entry = elt;
9748 *slot = entry;
9749
9750 /* Note this use of the partial instantiation so we can check it
9751 later in maybe_process_partial_specialization. */
9752 DECL_TEMPLATE_INSTANTIATIONS (found)
9753 = tree_cons (arglist, t,
9754 DECL_TEMPLATE_INSTANTIATIONS (found));
9755
9756 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
9757 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9758 /* Now that the type has been registered on the instantiations
9759 list, we set up the enumerators. Because the enumeration
9760 constants may involve the enumeration type itself, we make
9761 sure to register the type first, and then create the
9762 constants. That way, doing tsubst_expr for the enumeration
9763 constants won't result in recursive calls here; we'll find
9764 the instantiation and exit above. */
9765 tsubst_enum (template_type, t, arglist);
9766
9767 if (CLASS_TYPE_P (template_type) && is_dependent_type)
9768 /* If the type makes use of template parameters, the
9769 code that generates debugging information will crash. */
9770 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
9771
9772 /* Possibly limit visibility based on template args. */
9773 TREE_PUBLIC (type_decl) = 1;
9774 determine_visibility (type_decl);
9775
9776 inherit_targ_abi_tags (t);
9777
9778 return t;
9779 }
9780 }
9781
9782 /* Wrapper for lookup_template_class_1. */
9783
9784 tree
9785 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
9786 int entering_scope, tsubst_flags_t complain)
9787 {
9788 tree ret;
9789 timevar_push (TV_TEMPLATE_INST);
9790 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
9791 entering_scope, complain);
9792 timevar_pop (TV_TEMPLATE_INST);
9793 return ret;
9794 }
9795
9796 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9797
9798 tree
9799 lookup_template_variable (tree templ, tree arglist)
9800 {
9801 /* The type of the expression is NULL_TREE since the template-id could refer
9802 to an explicit or partial specialization. */
9803 tree type = NULL_TREE;
9804 if (flag_concepts && variable_concept_p (templ))
9805 /* Except that concepts are always bool. */
9806 type = boolean_type_node;
9807 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
9808 }
9809
9810 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9811
9812 tree
9813 finish_template_variable (tree var, tsubst_flags_t complain)
9814 {
9815 tree templ = TREE_OPERAND (var, 0);
9816 tree arglist = TREE_OPERAND (var, 1);
9817
9818 /* We never want to return a VAR_DECL for a variable concept, since they
9819 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9820 bool concept_p = flag_concepts && variable_concept_p (templ);
9821 if (concept_p && processing_template_decl)
9822 return var;
9823
9824 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
9825 arglist = add_outermost_template_args (tmpl_args, arglist);
9826
9827 templ = most_general_template (templ);
9828 tree parms = DECL_TEMPLATE_PARMS (templ);
9829 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
9830 /*req_all*/true,
9831 /*use_default*/true);
9832
9833 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
9834 {
9835 if (complain & tf_error)
9836 {
9837 auto_diagnostic_group d;
9838 error ("use of invalid variable template %qE", var);
9839 diagnose_constraints (location_of (var), templ, arglist);
9840 }
9841 return error_mark_node;
9842 }
9843
9844 /* If a template-id refers to a specialization of a variable
9845 concept, then the expression is true if and only if the
9846 concept's constraints are satisfied by the given template
9847 arguments.
9848
9849 NOTE: This is an extension of Concepts Lite TS that
9850 allows constraints to be used in expressions. */
9851 if (concept_p)
9852 {
9853 tree decl = DECL_TEMPLATE_RESULT (templ);
9854 return evaluate_variable_concept (decl, arglist);
9855 }
9856
9857 return instantiate_template (templ, arglist, complain);
9858 }
9859
9860 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9861 TARGS template args, and instantiate it if it's not dependent. */
9862
9863 tree
9864 lookup_and_finish_template_variable (tree templ, tree targs,
9865 tsubst_flags_t complain)
9866 {
9867 templ = lookup_template_variable (templ, targs);
9868 if (!any_dependent_template_arguments_p (targs))
9869 {
9870 templ = finish_template_variable (templ, complain);
9871 mark_used (templ);
9872 }
9873
9874 return convert_from_reference (templ);
9875 }
9876
9877 \f
9878 struct pair_fn_data
9879 {
9880 tree_fn_t fn;
9881 tree_fn_t any_fn;
9882 void *data;
9883 /* True when we should also visit template parameters that occur in
9884 non-deduced contexts. */
9885 bool include_nondeduced_p;
9886 hash_set<tree> *visited;
9887 };
9888
9889 /* Called from for_each_template_parm via walk_tree. */
9890
9891 static tree
9892 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
9893 {
9894 tree t = *tp;
9895 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
9896 tree_fn_t fn = pfd->fn;
9897 void *data = pfd->data;
9898 tree result = NULL_TREE;
9899
9900 #define WALK_SUBTREE(NODE) \
9901 do \
9902 { \
9903 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9904 pfd->include_nondeduced_p, \
9905 pfd->any_fn); \
9906 if (result) goto out; \
9907 } \
9908 while (0)
9909
9910 if (pfd->any_fn && (*pfd->any_fn)(t, data))
9911 return t;
9912
9913 if (TYPE_P (t)
9914 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
9915 WALK_SUBTREE (TYPE_CONTEXT (t));
9916
9917 switch (TREE_CODE (t))
9918 {
9919 case RECORD_TYPE:
9920 if (TYPE_PTRMEMFUNC_P (t))
9921 break;
9922 /* Fall through. */
9923
9924 case UNION_TYPE:
9925 case ENUMERAL_TYPE:
9926 if (!TYPE_TEMPLATE_INFO (t))
9927 *walk_subtrees = 0;
9928 else
9929 WALK_SUBTREE (TYPE_TI_ARGS (t));
9930 break;
9931
9932 case INTEGER_TYPE:
9933 WALK_SUBTREE (TYPE_MIN_VALUE (t));
9934 WALK_SUBTREE (TYPE_MAX_VALUE (t));
9935 break;
9936
9937 case METHOD_TYPE:
9938 /* Since we're not going to walk subtrees, we have to do this
9939 explicitly here. */
9940 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
9941 /* Fall through. */
9942
9943 case FUNCTION_TYPE:
9944 /* Check the return type. */
9945 WALK_SUBTREE (TREE_TYPE (t));
9946
9947 /* Check the parameter types. Since default arguments are not
9948 instantiated until they are needed, the TYPE_ARG_TYPES may
9949 contain expressions that involve template parameters. But,
9950 no-one should be looking at them yet. And, once they're
9951 instantiated, they don't contain template parameters, so
9952 there's no point in looking at them then, either. */
9953 {
9954 tree parm;
9955
9956 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
9957 WALK_SUBTREE (TREE_VALUE (parm));
9958
9959 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9960 want walk_tree walking into them itself. */
9961 *walk_subtrees = 0;
9962 }
9963
9964 if (flag_noexcept_type)
9965 {
9966 tree spec = TYPE_RAISES_EXCEPTIONS (t);
9967 if (spec)
9968 WALK_SUBTREE (TREE_PURPOSE (spec));
9969 }
9970 break;
9971
9972 case TYPEOF_TYPE:
9973 case DECLTYPE_TYPE:
9974 case UNDERLYING_TYPE:
9975 if (pfd->include_nondeduced_p
9976 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
9977 pfd->visited,
9978 pfd->include_nondeduced_p,
9979 pfd->any_fn))
9980 return error_mark_node;
9981 *walk_subtrees = false;
9982 break;
9983
9984 case FUNCTION_DECL:
9985 case VAR_DECL:
9986 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9987 WALK_SUBTREE (DECL_TI_ARGS (t));
9988 /* Fall through. */
9989
9990 case PARM_DECL:
9991 case CONST_DECL:
9992 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
9993 WALK_SUBTREE (DECL_INITIAL (t));
9994 if (DECL_CONTEXT (t)
9995 && pfd->include_nondeduced_p)
9996 WALK_SUBTREE (DECL_CONTEXT (t));
9997 break;
9998
9999 case BOUND_TEMPLATE_TEMPLATE_PARM:
10000 /* Record template parameters such as `T' inside `TT<T>'. */
10001 WALK_SUBTREE (TYPE_TI_ARGS (t));
10002 /* Fall through. */
10003
10004 case TEMPLATE_TEMPLATE_PARM:
10005 case TEMPLATE_TYPE_PARM:
10006 case TEMPLATE_PARM_INDEX:
10007 if (fn && (*fn)(t, data))
10008 return t;
10009 else if (!fn)
10010 return t;
10011 break;
10012
10013 case TEMPLATE_DECL:
10014 /* A template template parameter is encountered. */
10015 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10016 WALK_SUBTREE (TREE_TYPE (t));
10017
10018 /* Already substituted template template parameter */
10019 *walk_subtrees = 0;
10020 break;
10021
10022 case TYPENAME_TYPE:
10023 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10024 partial instantiation. */
10025 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
10026 break;
10027
10028 case CONSTRUCTOR:
10029 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
10030 && pfd->include_nondeduced_p)
10031 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
10032 break;
10033
10034 case INDIRECT_REF:
10035 case COMPONENT_REF:
10036 /* If there's no type, then this thing must be some expression
10037 involving template parameters. */
10038 if (!fn && !TREE_TYPE (t))
10039 return error_mark_node;
10040 break;
10041
10042 case MODOP_EXPR:
10043 case CAST_EXPR:
10044 case IMPLICIT_CONV_EXPR:
10045 case REINTERPRET_CAST_EXPR:
10046 case CONST_CAST_EXPR:
10047 case STATIC_CAST_EXPR:
10048 case DYNAMIC_CAST_EXPR:
10049 case ARROW_EXPR:
10050 case DOTSTAR_EXPR:
10051 case TYPEID_EXPR:
10052 case PSEUDO_DTOR_EXPR:
10053 if (!fn)
10054 return error_mark_node;
10055 break;
10056
10057 default:
10058 break;
10059 }
10060
10061 #undef WALK_SUBTREE
10062
10063 /* We didn't find any template parameters we liked. */
10064 out:
10065 return result;
10066 }
10067
10068 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10069 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10070 call FN with the parameter and the DATA.
10071 If FN returns nonzero, the iteration is terminated, and
10072 for_each_template_parm returns 1. Otherwise, the iteration
10073 continues. If FN never returns a nonzero value, the value
10074 returned by for_each_template_parm is 0. If FN is NULL, it is
10075 considered to be the function which always returns 1.
10076
10077 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10078 parameters that occur in non-deduced contexts. When false, only
10079 visits those template parameters that can be deduced. */
10080
10081 static tree
10082 for_each_template_parm (tree t, tree_fn_t fn, void* data,
10083 hash_set<tree> *visited,
10084 bool include_nondeduced_p,
10085 tree_fn_t any_fn)
10086 {
10087 struct pair_fn_data pfd;
10088 tree result;
10089
10090 /* Set up. */
10091 pfd.fn = fn;
10092 pfd.any_fn = any_fn;
10093 pfd.data = data;
10094 pfd.include_nondeduced_p = include_nondeduced_p;
10095
10096 /* Walk the tree. (Conceptually, we would like to walk without
10097 duplicates, but for_each_template_parm_r recursively calls
10098 for_each_template_parm, so we would need to reorganize a fair
10099 bit to use walk_tree_without_duplicates, so we keep our own
10100 visited list.) */
10101 if (visited)
10102 pfd.visited = visited;
10103 else
10104 pfd.visited = new hash_set<tree>;
10105 result = cp_walk_tree (&t,
10106 for_each_template_parm_r,
10107 &pfd,
10108 pfd.visited);
10109
10110 /* Clean up. */
10111 if (!visited)
10112 {
10113 delete pfd.visited;
10114 pfd.visited = 0;
10115 }
10116
10117 return result;
10118 }
10119
10120 /* Returns true if T depends on any template parameter. */
10121
10122 int
10123 uses_template_parms (tree t)
10124 {
10125 if (t == NULL_TREE)
10126 return false;
10127
10128 bool dependent_p;
10129 int saved_processing_template_decl;
10130
10131 saved_processing_template_decl = processing_template_decl;
10132 if (!saved_processing_template_decl)
10133 processing_template_decl = 1;
10134 if (TYPE_P (t))
10135 dependent_p = dependent_type_p (t);
10136 else if (TREE_CODE (t) == TREE_VEC)
10137 dependent_p = any_dependent_template_arguments_p (t);
10138 else if (TREE_CODE (t) == TREE_LIST)
10139 dependent_p = (uses_template_parms (TREE_VALUE (t))
10140 || uses_template_parms (TREE_CHAIN (t)));
10141 else if (TREE_CODE (t) == TYPE_DECL)
10142 dependent_p = dependent_type_p (TREE_TYPE (t));
10143 else if (DECL_P (t)
10144 || EXPR_P (t)
10145 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
10146 || TREE_CODE (t) == OVERLOAD
10147 || BASELINK_P (t)
10148 || identifier_p (t)
10149 || TREE_CODE (t) == TRAIT_EXPR
10150 || TREE_CODE (t) == CONSTRUCTOR
10151 || CONSTANT_CLASS_P (t))
10152 dependent_p = (type_dependent_expression_p (t)
10153 || value_dependent_expression_p (t));
10154 else
10155 {
10156 gcc_assert (t == error_mark_node);
10157 dependent_p = false;
10158 }
10159
10160 processing_template_decl = saved_processing_template_decl;
10161
10162 return dependent_p;
10163 }
10164
10165 /* Returns true iff current_function_decl is an incompletely instantiated
10166 template. Useful instead of processing_template_decl because the latter
10167 is set to 0 during instantiate_non_dependent_expr. */
10168
10169 bool
10170 in_template_function (void)
10171 {
10172 tree fn = current_function_decl;
10173 bool ret;
10174 ++processing_template_decl;
10175 ret = (fn && DECL_LANG_SPECIFIC (fn)
10176 && DECL_TEMPLATE_INFO (fn)
10177 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
10178 --processing_template_decl;
10179 return ret;
10180 }
10181
10182 /* Returns true if T depends on any template parameter with level LEVEL. */
10183
10184 bool
10185 uses_template_parms_level (tree t, int level)
10186 {
10187 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
10188 /*include_nondeduced_p=*/true);
10189 }
10190
10191 /* Returns true if the signature of DECL depends on any template parameter from
10192 its enclosing class. */
10193
10194 bool
10195 uses_outer_template_parms (tree decl)
10196 {
10197 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
10198 if (depth == 0)
10199 return false;
10200 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
10201 &depth, NULL, /*include_nondeduced_p=*/true))
10202 return true;
10203 if (PRIMARY_TEMPLATE_P (decl)
10204 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10205 (DECL_TEMPLATE_PARMS (decl)),
10206 template_parm_outer_level,
10207 &depth, NULL, /*include_nondeduced_p=*/true))
10208 return true;
10209 tree ci = get_constraints (decl);
10210 if (ci)
10211 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
10212 if (ci && for_each_template_parm (ci, template_parm_outer_level,
10213 &depth, NULL, /*nondeduced*/true))
10214 return true;
10215 return false;
10216 }
10217
10218 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10219 ill-formed translation unit, i.e. a variable or function that isn't
10220 usable in a constant expression. */
10221
10222 static inline bool
10223 neglectable_inst_p (tree d)
10224 {
10225 return (d && DECL_P (d)
10226 && !undeduced_auto_decl (d)
10227 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
10228 : decl_maybe_constant_var_p (d)));
10229 }
10230
10231 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10232 neglectable and instantiated from within an erroneous instantiation. */
10233
10234 static bool
10235 limit_bad_template_recursion (tree decl)
10236 {
10237 struct tinst_level *lev = current_tinst_level;
10238 int errs = errorcount + sorrycount;
10239 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
10240 return false;
10241
10242 for (; lev; lev = lev->next)
10243 if (neglectable_inst_p (lev->maybe_get_node ()))
10244 break;
10245
10246 return (lev && errs > lev->errors);
10247 }
10248
10249 static int tinst_depth;
10250 extern int max_tinst_depth;
10251 int depth_reached;
10252
10253 static GTY(()) struct tinst_level *last_error_tinst_level;
10254
10255 /* We're starting to instantiate D; record the template instantiation context
10256 at LOC for diagnostics and to restore it later. */
10257
10258 static bool
10259 push_tinst_level_loc (tree tldcl, tree targs, location_t loc)
10260 {
10261 struct tinst_level *new_level;
10262
10263 if (tinst_depth >= max_tinst_depth)
10264 {
10265 /* Tell error.c not to try to instantiate any templates. */
10266 at_eof = 2;
10267 fatal_error (input_location,
10268 "template instantiation depth exceeds maximum of %d"
10269 " (use %<-ftemplate-depth=%> to increase the maximum)",
10270 max_tinst_depth);
10271 return false;
10272 }
10273
10274 /* If the current instantiation caused problems, don't let it instantiate
10275 anything else. Do allow deduction substitution and decls usable in
10276 constant expressions. */
10277 if (!targs && limit_bad_template_recursion (tldcl))
10278 return false;
10279
10280 /* When not -quiet, dump template instantiations other than functions, since
10281 announce_function will take care of those. */
10282 if (!quiet_flag && !targs
10283 && TREE_CODE (tldcl) != TREE_LIST
10284 && TREE_CODE (tldcl) != FUNCTION_DECL)
10285 fprintf (stderr, " %s", decl_as_string (tldcl, TFF_DECL_SPECIFIERS));
10286
10287 new_level = tinst_level_freelist ().alloc ();
10288 new_level->tldcl = tldcl;
10289 new_level->targs = targs;
10290 new_level->locus = loc;
10291 new_level->errors = errorcount + sorrycount;
10292 new_level->next = NULL;
10293 new_level->refcount = 0;
10294 set_refcount_ptr (new_level->next, current_tinst_level);
10295 set_refcount_ptr (current_tinst_level, new_level);
10296
10297 ++tinst_depth;
10298 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
10299 depth_reached = tinst_depth;
10300
10301 return true;
10302 }
10303
10304 /* We're starting substitution of TMPL<ARGS>; record the template
10305 substitution context for diagnostics and to restore it later. */
10306
10307 static bool
10308 push_tinst_level (tree tmpl, tree args)
10309 {
10310 return push_tinst_level_loc (tmpl, args, input_location);
10311 }
10312
10313 /* We're starting to instantiate D; record INPUT_LOCATION and the
10314 template instantiation context for diagnostics and to restore it
10315 later. */
10316
10317 bool
10318 push_tinst_level (tree d)
10319 {
10320 return push_tinst_level_loc (d, input_location);
10321 }
10322
10323 /* Likewise, but record LOC as the program location. */
10324
10325 bool
10326 push_tinst_level_loc (tree d, location_t loc)
10327 {
10328 gcc_assert (TREE_CODE (d) != TREE_LIST);
10329 return push_tinst_level_loc (d, NULL, loc);
10330 }
10331
10332 /* We're done instantiating this template; return to the instantiation
10333 context. */
10334
10335 void
10336 pop_tinst_level (void)
10337 {
10338 /* Restore the filename and line number stashed away when we started
10339 this instantiation. */
10340 input_location = current_tinst_level->locus;
10341 set_refcount_ptr (current_tinst_level, current_tinst_level->next);
10342 --tinst_depth;
10343 }
10344
10345 /* We're instantiating a deferred template; restore the template
10346 instantiation context in which the instantiation was requested, which
10347 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
10348
10349 static tree
10350 reopen_tinst_level (struct tinst_level *level)
10351 {
10352 struct tinst_level *t;
10353
10354 tinst_depth = 0;
10355 for (t = level; t; t = t->next)
10356 ++tinst_depth;
10357
10358 set_refcount_ptr (current_tinst_level, level);
10359 pop_tinst_level ();
10360 if (current_tinst_level)
10361 current_tinst_level->errors = errorcount+sorrycount;
10362 return level->maybe_get_node ();
10363 }
10364
10365 /* Returns the TINST_LEVEL which gives the original instantiation
10366 context. */
10367
10368 struct tinst_level *
10369 outermost_tinst_level (void)
10370 {
10371 struct tinst_level *level = current_tinst_level;
10372 if (level)
10373 while (level->next)
10374 level = level->next;
10375 return level;
10376 }
10377
10378 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
10379 vector of template arguments, as for tsubst.
10380
10381 Returns an appropriate tsubst'd friend declaration. */
10382
10383 static tree
10384 tsubst_friend_function (tree decl, tree args)
10385 {
10386 tree new_friend;
10387
10388 if (TREE_CODE (decl) == FUNCTION_DECL
10389 && DECL_TEMPLATE_INSTANTIATION (decl)
10390 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
10391 /* This was a friend declared with an explicit template
10392 argument list, e.g.:
10393
10394 friend void f<>(T);
10395
10396 to indicate that f was a template instantiation, not a new
10397 function declaration. Now, we have to figure out what
10398 instantiation of what template. */
10399 {
10400 tree template_id, arglist, fns;
10401 tree new_args;
10402 tree tmpl;
10403 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
10404
10405 /* Friend functions are looked up in the containing namespace scope.
10406 We must enter that scope, to avoid finding member functions of the
10407 current class with same name. */
10408 push_nested_namespace (ns);
10409 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
10410 tf_warning_or_error, NULL_TREE,
10411 /*integral_constant_expression_p=*/false);
10412 pop_nested_namespace (ns);
10413 arglist = tsubst (DECL_TI_ARGS (decl), args,
10414 tf_warning_or_error, NULL_TREE);
10415 template_id = lookup_template_function (fns, arglist);
10416
10417 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10418 tmpl = determine_specialization (template_id, new_friend,
10419 &new_args,
10420 /*need_member_template=*/0,
10421 TREE_VEC_LENGTH (args),
10422 tsk_none);
10423 return instantiate_template (tmpl, new_args, tf_error);
10424 }
10425
10426 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10427
10428 /* The NEW_FRIEND will look like an instantiation, to the
10429 compiler, but is not an instantiation from the point of view of
10430 the language. For example, we might have had:
10431
10432 template <class T> struct S {
10433 template <class U> friend void f(T, U);
10434 };
10435
10436 Then, in S<int>, template <class U> void f(int, U) is not an
10437 instantiation of anything. */
10438 if (new_friend == error_mark_node)
10439 return error_mark_node;
10440
10441 DECL_USE_TEMPLATE (new_friend) = 0;
10442 if (TREE_CODE (decl) == TEMPLATE_DECL)
10443 {
10444 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
10445 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
10446 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
10447 }
10448
10449 /* The mangled name for the NEW_FRIEND is incorrect. The function
10450 is not a template instantiation and should not be mangled like
10451 one. Therefore, we forget the mangling here; we'll recompute it
10452 later if we need it. */
10453 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
10454 {
10455 SET_DECL_RTL (new_friend, NULL);
10456 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
10457 }
10458
10459 if (DECL_NAMESPACE_SCOPE_P (new_friend))
10460 {
10461 tree old_decl;
10462 tree new_friend_template_info;
10463 tree new_friend_result_template_info;
10464 tree ns;
10465 int new_friend_is_defn;
10466
10467 /* We must save some information from NEW_FRIEND before calling
10468 duplicate decls since that function will free NEW_FRIEND if
10469 possible. */
10470 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
10471 new_friend_is_defn =
10472 (DECL_INITIAL (DECL_TEMPLATE_RESULT
10473 (template_for_substitution (new_friend)))
10474 != NULL_TREE);
10475 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
10476 {
10477 /* This declaration is a `primary' template. */
10478 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
10479
10480 new_friend_result_template_info
10481 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
10482 }
10483 else
10484 new_friend_result_template_info = NULL_TREE;
10485
10486 /* Inside pushdecl_namespace_level, we will push into the
10487 current namespace. However, the friend function should go
10488 into the namespace of the template. */
10489 ns = decl_namespace_context (new_friend);
10490 push_nested_namespace (ns);
10491 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
10492 pop_nested_namespace (ns);
10493
10494 if (old_decl == error_mark_node)
10495 return error_mark_node;
10496
10497 if (old_decl != new_friend)
10498 {
10499 /* This new friend declaration matched an existing
10500 declaration. For example, given:
10501
10502 template <class T> void f(T);
10503 template <class U> class C {
10504 template <class T> friend void f(T) {}
10505 };
10506
10507 the friend declaration actually provides the definition
10508 of `f', once C has been instantiated for some type. So,
10509 old_decl will be the out-of-class template declaration,
10510 while new_friend is the in-class definition.
10511
10512 But, if `f' was called before this point, the
10513 instantiation of `f' will have DECL_TI_ARGS corresponding
10514 to `T' but not to `U', references to which might appear
10515 in the definition of `f'. Previously, the most general
10516 template for an instantiation of `f' was the out-of-class
10517 version; now it is the in-class version. Therefore, we
10518 run through all specialization of `f', adding to their
10519 DECL_TI_ARGS appropriately. In particular, they need a
10520 new set of outer arguments, corresponding to the
10521 arguments for this class instantiation.
10522
10523 The same situation can arise with something like this:
10524
10525 friend void f(int);
10526 template <class T> class C {
10527 friend void f(T) {}
10528 };
10529
10530 when `C<int>' is instantiated. Now, `f(int)' is defined
10531 in the class. */
10532
10533 if (!new_friend_is_defn)
10534 /* On the other hand, if the in-class declaration does
10535 *not* provide a definition, then we don't want to alter
10536 existing definitions. We can just leave everything
10537 alone. */
10538 ;
10539 else
10540 {
10541 tree new_template = TI_TEMPLATE (new_friend_template_info);
10542 tree new_args = TI_ARGS (new_friend_template_info);
10543
10544 /* Overwrite whatever template info was there before, if
10545 any, with the new template information pertaining to
10546 the declaration. */
10547 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
10548
10549 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
10550 {
10551 /* We should have called reregister_specialization in
10552 duplicate_decls. */
10553 gcc_assert (retrieve_specialization (new_template,
10554 new_args, 0)
10555 == old_decl);
10556
10557 /* Instantiate it if the global has already been used. */
10558 if (DECL_ODR_USED (old_decl))
10559 instantiate_decl (old_decl, /*defer_ok=*/true,
10560 /*expl_inst_class_mem_p=*/false);
10561 }
10562 else
10563 {
10564 tree t;
10565
10566 /* Indicate that the old function template is a partial
10567 instantiation. */
10568 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
10569 = new_friend_result_template_info;
10570
10571 gcc_assert (new_template
10572 == most_general_template (new_template));
10573 gcc_assert (new_template != old_decl);
10574
10575 /* Reassign any specializations already in the hash table
10576 to the new more general template, and add the
10577 additional template args. */
10578 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
10579 t != NULL_TREE;
10580 t = TREE_CHAIN (t))
10581 {
10582 tree spec = TREE_VALUE (t);
10583 spec_entry elt;
10584
10585 elt.tmpl = old_decl;
10586 elt.args = DECL_TI_ARGS (spec);
10587 elt.spec = NULL_TREE;
10588
10589 decl_specializations->remove_elt (&elt);
10590
10591 DECL_TI_ARGS (spec)
10592 = add_outermost_template_args (new_args,
10593 DECL_TI_ARGS (spec));
10594
10595 register_specialization
10596 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
10597
10598 }
10599 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
10600 }
10601 }
10602
10603 /* The information from NEW_FRIEND has been merged into OLD_DECL
10604 by duplicate_decls. */
10605 new_friend = old_decl;
10606 }
10607 }
10608 else
10609 {
10610 tree context = DECL_CONTEXT (new_friend);
10611 bool dependent_p;
10612
10613 /* In the code
10614 template <class T> class C {
10615 template <class U> friend void C1<U>::f (); // case 1
10616 friend void C2<T>::f (); // case 2
10617 };
10618 we only need to make sure CONTEXT is a complete type for
10619 case 2. To distinguish between the two cases, we note that
10620 CONTEXT of case 1 remains dependent type after tsubst while
10621 this isn't true for case 2. */
10622 ++processing_template_decl;
10623 dependent_p = dependent_type_p (context);
10624 --processing_template_decl;
10625
10626 if (!dependent_p
10627 && !complete_type_or_else (context, NULL_TREE))
10628 return error_mark_node;
10629
10630 if (COMPLETE_TYPE_P (context))
10631 {
10632 tree fn = new_friend;
10633 /* do_friend adds the TEMPLATE_DECL for any member friend
10634 template even if it isn't a member template, i.e.
10635 template <class T> friend A<T>::f();
10636 Look through it in that case. */
10637 if (TREE_CODE (fn) == TEMPLATE_DECL
10638 && !PRIMARY_TEMPLATE_P (fn))
10639 fn = DECL_TEMPLATE_RESULT (fn);
10640 /* Check to see that the declaration is really present, and,
10641 possibly obtain an improved declaration. */
10642 fn = check_classfn (context, fn, NULL_TREE);
10643
10644 if (fn)
10645 new_friend = fn;
10646 }
10647 }
10648
10649 return new_friend;
10650 }
10651
10652 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
10653 template arguments, as for tsubst.
10654
10655 Returns an appropriate tsubst'd friend type or error_mark_node on
10656 failure. */
10657
10658 static tree
10659 tsubst_friend_class (tree friend_tmpl, tree args)
10660 {
10661 tree tmpl;
10662
10663 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
10664 {
10665 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
10666 return TREE_TYPE (tmpl);
10667 }
10668
10669 tree context = CP_DECL_CONTEXT (friend_tmpl);
10670 if (TREE_CODE (context) == NAMESPACE_DECL)
10671 push_nested_namespace (context);
10672 else
10673 {
10674 context = tsubst (context, args, tf_error, NULL_TREE);
10675 push_nested_class (context);
10676 }
10677
10678 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), /*prefer_type=*/false,
10679 /*non_class=*/false, /*block_p=*/false,
10680 /*namespaces_only=*/false, LOOKUP_HIDDEN);
10681
10682 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
10683 {
10684 /* The friend template has already been declared. Just
10685 check to see that the declarations match, and install any new
10686 default parameters. We must tsubst the default parameters,
10687 of course. We only need the innermost template parameters
10688 because that is all that redeclare_class_template will look
10689 at. */
10690 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
10691 > TMPL_ARGS_DEPTH (args))
10692 {
10693 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
10694 args, tf_warning_or_error);
10695 location_t saved_input_location = input_location;
10696 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
10697 tree cons = get_constraints (tmpl);
10698 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
10699 input_location = saved_input_location;
10700 }
10701 }
10702 else
10703 {
10704 /* The friend template has not already been declared. In this
10705 case, the instantiation of the template class will cause the
10706 injection of this template into the namespace scope. */
10707 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
10708
10709 if (tmpl != error_mark_node)
10710 {
10711 /* The new TMPL is not an instantiation of anything, so we
10712 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
10713 for the new type because that is supposed to be the
10714 corresponding template decl, i.e., TMPL. */
10715 DECL_USE_TEMPLATE (tmpl) = 0;
10716 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
10717 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
10718 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
10719 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
10720
10721 /* It is hidden. */
10722 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl));
10723 DECL_ANTICIPATED (tmpl)
10724 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl)) = true;
10725
10726 /* Inject this template into the enclosing namspace scope. */
10727 tmpl = pushdecl_namespace_level (tmpl, true);
10728 }
10729 }
10730
10731 if (TREE_CODE (context) == NAMESPACE_DECL)
10732 pop_nested_namespace (context);
10733 else
10734 pop_nested_class ();
10735
10736 return TREE_TYPE (tmpl);
10737 }
10738
10739 /* Returns zero if TYPE cannot be completed later due to circularity.
10740 Otherwise returns one. */
10741
10742 static int
10743 can_complete_type_without_circularity (tree type)
10744 {
10745 if (type == NULL_TREE || type == error_mark_node)
10746 return 0;
10747 else if (COMPLETE_TYPE_P (type))
10748 return 1;
10749 else if (TREE_CODE (type) == ARRAY_TYPE)
10750 return can_complete_type_without_circularity (TREE_TYPE (type));
10751 else if (CLASS_TYPE_P (type)
10752 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
10753 return 0;
10754 else
10755 return 1;
10756 }
10757
10758 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
10759 tsubst_flags_t, tree);
10760
10761 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10762 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10763
10764 static tree
10765 tsubst_attribute (tree t, tree *decl_p, tree args,
10766 tsubst_flags_t complain, tree in_decl)
10767 {
10768 gcc_assert (ATTR_IS_DEPENDENT (t));
10769
10770 tree val = TREE_VALUE (t);
10771 if (val == NULL_TREE)
10772 /* Nothing to do. */;
10773 else if ((flag_openmp || flag_openmp_simd)
10774 && is_attribute_p ("omp declare simd",
10775 get_attribute_name (t)))
10776 {
10777 tree clauses = TREE_VALUE (val);
10778 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
10779 complain, in_decl);
10780 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
10781 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
10782 tree parms = DECL_ARGUMENTS (*decl_p);
10783 clauses
10784 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
10785 if (clauses)
10786 val = build_tree_list (NULL_TREE, clauses);
10787 else
10788 val = NULL_TREE;
10789 }
10790 /* If the first attribute argument is an identifier, don't
10791 pass it through tsubst. Attributes like mode, format,
10792 cleanup and several target specific attributes expect it
10793 unmodified. */
10794 else if (attribute_takes_identifier_p (get_attribute_name (t)))
10795 {
10796 tree chain
10797 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
10798 /*integral_constant_expression_p=*/false);
10799 if (chain != TREE_CHAIN (val))
10800 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
10801 }
10802 else if (PACK_EXPANSION_P (val))
10803 {
10804 /* An attribute pack expansion. */
10805 tree purp = TREE_PURPOSE (t);
10806 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
10807 if (pack == error_mark_node)
10808 return error_mark_node;
10809 int len = TREE_VEC_LENGTH (pack);
10810 tree list = NULL_TREE;
10811 tree *q = &list;
10812 for (int i = 0; i < len; ++i)
10813 {
10814 tree elt = TREE_VEC_ELT (pack, i);
10815 *q = build_tree_list (purp, elt);
10816 q = &TREE_CHAIN (*q);
10817 }
10818 return list;
10819 }
10820 else
10821 val = tsubst_expr (val, args, complain, in_decl,
10822 /*integral_constant_expression_p=*/false);
10823
10824 if (val != TREE_VALUE (t))
10825 return build_tree_list (TREE_PURPOSE (t), val);
10826 return t;
10827 }
10828
10829 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10830 unchanged or a new TREE_LIST chain. */
10831
10832 static tree
10833 tsubst_attributes (tree attributes, tree args,
10834 tsubst_flags_t complain, tree in_decl)
10835 {
10836 tree last_dep = NULL_TREE;
10837
10838 for (tree t = attributes; t; t = TREE_CHAIN (t))
10839 if (ATTR_IS_DEPENDENT (t))
10840 {
10841 last_dep = t;
10842 attributes = copy_list (attributes);
10843 break;
10844 }
10845
10846 if (last_dep)
10847 for (tree *p = &attributes; *p; )
10848 {
10849 tree t = *p;
10850 if (ATTR_IS_DEPENDENT (t))
10851 {
10852 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
10853 if (subst != t)
10854 {
10855 *p = subst;
10856 while (*p)
10857 p = &TREE_CHAIN (*p);
10858 *p = TREE_CHAIN (t);
10859 continue;
10860 }
10861 }
10862 p = &TREE_CHAIN (*p);
10863 }
10864
10865 return attributes;
10866 }
10867
10868 /* Apply any attributes which had to be deferred until instantiation
10869 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10870 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10871
10872 static void
10873 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
10874 tree args, tsubst_flags_t complain, tree in_decl)
10875 {
10876 tree last_dep = NULL_TREE;
10877 tree t;
10878 tree *p;
10879
10880 if (attributes == NULL_TREE)
10881 return;
10882
10883 if (DECL_P (*decl_p))
10884 {
10885 if (TREE_TYPE (*decl_p) == error_mark_node)
10886 return;
10887 p = &DECL_ATTRIBUTES (*decl_p);
10888 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10889 to our attributes parameter. */
10890 gcc_assert (*p == attributes);
10891 }
10892 else
10893 {
10894 p = &TYPE_ATTRIBUTES (*decl_p);
10895 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10896 lookup_template_class_1, and should be preserved. */
10897 gcc_assert (*p != attributes);
10898 while (*p)
10899 p = &TREE_CHAIN (*p);
10900 }
10901
10902 for (t = attributes; t; t = TREE_CHAIN (t))
10903 if (ATTR_IS_DEPENDENT (t))
10904 {
10905 last_dep = t;
10906 attributes = copy_list (attributes);
10907 break;
10908 }
10909
10910 *p = attributes;
10911 if (last_dep)
10912 {
10913 tree late_attrs = NULL_TREE;
10914 tree *q = &late_attrs;
10915
10916 for (; *p; )
10917 {
10918 t = *p;
10919 if (ATTR_IS_DEPENDENT (t))
10920 {
10921 *p = TREE_CHAIN (t);
10922 TREE_CHAIN (t) = NULL_TREE;
10923 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
10924 while (*q)
10925 q = &TREE_CHAIN (*q);
10926 }
10927 else
10928 p = &TREE_CHAIN (t);
10929 }
10930
10931 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
10932 }
10933 }
10934
10935 /* Perform (or defer) access check for typedefs that were referenced
10936 from within the template TMPL code.
10937 This is a subroutine of instantiate_decl and instantiate_class_template.
10938 TMPL is the template to consider and TARGS is the list of arguments of
10939 that template. */
10940
10941 static void
10942 perform_typedefs_access_check (tree tmpl, tree targs)
10943 {
10944 unsigned i;
10945 qualified_typedef_usage_t *iter;
10946
10947 if (!tmpl
10948 || (!CLASS_TYPE_P (tmpl)
10949 && TREE_CODE (tmpl) != FUNCTION_DECL))
10950 return;
10951
10952 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
10953 {
10954 tree type_decl = iter->typedef_decl;
10955 tree type_scope = iter->context;
10956
10957 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
10958 continue;
10959
10960 if (uses_template_parms (type_decl))
10961 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
10962 if (uses_template_parms (type_scope))
10963 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
10964
10965 /* Make access check error messages point to the location
10966 of the use of the typedef. */
10967 iloc_sentinel ils (iter->locus);
10968 perform_or_defer_access_check (TYPE_BINFO (type_scope),
10969 type_decl, type_decl,
10970 tf_warning_or_error);
10971 }
10972 }
10973
10974 static tree
10975 instantiate_class_template_1 (tree type)
10976 {
10977 tree templ, args, pattern, t, member;
10978 tree typedecl;
10979 tree pbinfo;
10980 tree base_list;
10981 unsigned int saved_maximum_field_alignment;
10982 tree fn_context;
10983
10984 if (type == error_mark_node)
10985 return error_mark_node;
10986
10987 if (COMPLETE_OR_OPEN_TYPE_P (type)
10988 || uses_template_parms (type))
10989 return type;
10990
10991 /* Figure out which template is being instantiated. */
10992 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
10993 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
10994
10995 /* Mark the type as in the process of being defined. */
10996 TYPE_BEING_DEFINED (type) = 1;
10997
10998 /* We may be in the middle of deferred access check. Disable
10999 it now. */
11000 deferring_access_check_sentinel acs (dk_no_deferred);
11001
11002 /* Determine what specialization of the original template to
11003 instantiate. */
11004 t = most_specialized_partial_spec (type, tf_warning_or_error);
11005 if (t == error_mark_node)
11006 return error_mark_node;
11007 else if (t)
11008 {
11009 /* This TYPE is actually an instantiation of a partial
11010 specialization. We replace the innermost set of ARGS with
11011 the arguments appropriate for substitution. For example,
11012 given:
11013
11014 template <class T> struct S {};
11015 template <class T> struct S<T*> {};
11016
11017 and supposing that we are instantiating S<int*>, ARGS will
11018 presently be {int*} -- but we need {int}. */
11019 pattern = TREE_TYPE (t);
11020 args = TREE_PURPOSE (t);
11021 }
11022 else
11023 {
11024 pattern = TREE_TYPE (templ);
11025 args = CLASSTYPE_TI_ARGS (type);
11026 }
11027
11028 /* If the template we're instantiating is incomplete, then clearly
11029 there's nothing we can do. */
11030 if (!COMPLETE_TYPE_P (pattern))
11031 {
11032 /* We can try again later. */
11033 TYPE_BEING_DEFINED (type) = 0;
11034 return type;
11035 }
11036
11037 /* If we've recursively instantiated too many templates, stop. */
11038 if (! push_tinst_level (type))
11039 return type;
11040
11041 int saved_unevaluated_operand = cp_unevaluated_operand;
11042 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11043
11044 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
11045 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
11046 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
11047 fn_context = error_mark_node;
11048 if (!fn_context)
11049 push_to_top_level ();
11050 else
11051 {
11052 cp_unevaluated_operand = 0;
11053 c_inhibit_evaluation_warnings = 0;
11054 }
11055 /* Use #pragma pack from the template context. */
11056 saved_maximum_field_alignment = maximum_field_alignment;
11057 maximum_field_alignment = TYPE_PRECISION (pattern);
11058
11059 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
11060
11061 /* Set the input location to the most specialized template definition.
11062 This is needed if tsubsting causes an error. */
11063 typedecl = TYPE_MAIN_DECL (pattern);
11064 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
11065 DECL_SOURCE_LOCATION (typedecl);
11066
11067 TYPE_PACKED (type) = TYPE_PACKED (pattern);
11068 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
11069 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
11070 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
11071 if (ANON_AGGR_TYPE_P (pattern))
11072 SET_ANON_AGGR_TYPE_P (type);
11073 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
11074 {
11075 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
11076 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
11077 /* Adjust visibility for template arguments. */
11078 determine_visibility (TYPE_MAIN_DECL (type));
11079 }
11080 if (CLASS_TYPE_P (type))
11081 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
11082
11083 pbinfo = TYPE_BINFO (pattern);
11084
11085 /* We should never instantiate a nested class before its enclosing
11086 class; we need to look up the nested class by name before we can
11087 instantiate it, and that lookup should instantiate the enclosing
11088 class. */
11089 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
11090 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
11091
11092 base_list = NULL_TREE;
11093 if (BINFO_N_BASE_BINFOS (pbinfo))
11094 {
11095 tree pbase_binfo;
11096 tree pushed_scope;
11097 int i;
11098
11099 /* We must enter the scope containing the type, as that is where
11100 the accessibility of types named in dependent bases are
11101 looked up from. */
11102 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
11103
11104 /* Substitute into each of the bases to determine the actual
11105 basetypes. */
11106 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
11107 {
11108 tree base;
11109 tree access = BINFO_BASE_ACCESS (pbinfo, i);
11110 tree expanded_bases = NULL_TREE;
11111 int idx, len = 1;
11112
11113 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
11114 {
11115 expanded_bases =
11116 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
11117 args, tf_error, NULL_TREE);
11118 if (expanded_bases == error_mark_node)
11119 continue;
11120
11121 len = TREE_VEC_LENGTH (expanded_bases);
11122 }
11123
11124 for (idx = 0; idx < len; idx++)
11125 {
11126 if (expanded_bases)
11127 /* Extract the already-expanded base class. */
11128 base = TREE_VEC_ELT (expanded_bases, idx);
11129 else
11130 /* Substitute to figure out the base class. */
11131 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
11132 NULL_TREE);
11133
11134 if (base == error_mark_node)
11135 continue;
11136
11137 base_list = tree_cons (access, base, base_list);
11138 if (BINFO_VIRTUAL_P (pbase_binfo))
11139 TREE_TYPE (base_list) = integer_type_node;
11140 }
11141 }
11142
11143 /* The list is now in reverse order; correct that. */
11144 base_list = nreverse (base_list);
11145
11146 if (pushed_scope)
11147 pop_scope (pushed_scope);
11148 }
11149 /* Now call xref_basetypes to set up all the base-class
11150 information. */
11151 xref_basetypes (type, base_list);
11152
11153 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
11154 (int) ATTR_FLAG_TYPE_IN_PLACE,
11155 args, tf_error, NULL_TREE);
11156 fixup_attribute_variants (type);
11157
11158 /* Now that our base classes are set up, enter the scope of the
11159 class, so that name lookups into base classes, etc. will work
11160 correctly. This is precisely analogous to what we do in
11161 begin_class_definition when defining an ordinary non-template
11162 class, except we also need to push the enclosing classes. */
11163 push_nested_class (type);
11164
11165 /* Now members are processed in the order of declaration. */
11166 for (member = CLASSTYPE_DECL_LIST (pattern);
11167 member; member = TREE_CHAIN (member))
11168 {
11169 tree t = TREE_VALUE (member);
11170
11171 if (TREE_PURPOSE (member))
11172 {
11173 if (TYPE_P (t))
11174 {
11175 if (LAMBDA_TYPE_P (t))
11176 /* A closure type for a lambda in an NSDMI or default argument.
11177 Ignore it; it will be regenerated when needed. */
11178 continue;
11179
11180 /* Build new CLASSTYPE_NESTED_UTDS. */
11181
11182 tree newtag;
11183 bool class_template_p;
11184
11185 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
11186 && TYPE_LANG_SPECIFIC (t)
11187 && CLASSTYPE_IS_TEMPLATE (t));
11188 /* If the member is a class template, then -- even after
11189 substitution -- there may be dependent types in the
11190 template argument list for the class. We increment
11191 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11192 that function will assume that no types are dependent
11193 when outside of a template. */
11194 if (class_template_p)
11195 ++processing_template_decl;
11196 newtag = tsubst (t, args, tf_error, NULL_TREE);
11197 if (class_template_p)
11198 --processing_template_decl;
11199 if (newtag == error_mark_node)
11200 continue;
11201
11202 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
11203 {
11204 tree name = TYPE_IDENTIFIER (t);
11205
11206 if (class_template_p)
11207 /* Unfortunately, lookup_template_class sets
11208 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11209 instantiation (i.e., for the type of a member
11210 template class nested within a template class.)
11211 This behavior is required for
11212 maybe_process_partial_specialization to work
11213 correctly, but is not accurate in this case;
11214 the TAG is not an instantiation of anything.
11215 (The corresponding TEMPLATE_DECL is an
11216 instantiation, but the TYPE is not.) */
11217 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
11218
11219 /* Now, we call pushtag to put this NEWTAG into the scope of
11220 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11221 pushtag calling push_template_decl. We don't have to do
11222 this for enums because it will already have been done in
11223 tsubst_enum. */
11224 if (name)
11225 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
11226 pushtag (name, newtag, /*tag_scope=*/ts_current);
11227 }
11228 }
11229 else if (DECL_DECLARES_FUNCTION_P (t))
11230 {
11231 tree r;
11232
11233 if (TREE_CODE (t) == TEMPLATE_DECL)
11234 ++processing_template_decl;
11235 r = tsubst (t, args, tf_error, NULL_TREE);
11236 if (TREE_CODE (t) == TEMPLATE_DECL)
11237 --processing_template_decl;
11238 set_current_access_from_decl (r);
11239 finish_member_declaration (r);
11240 /* Instantiate members marked with attribute used. */
11241 if (r != error_mark_node && DECL_PRESERVE_P (r))
11242 mark_used (r);
11243 if (TREE_CODE (r) == FUNCTION_DECL
11244 && DECL_OMP_DECLARE_REDUCTION_P (r))
11245 cp_check_omp_declare_reduction (r);
11246 }
11247 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
11248 && LAMBDA_TYPE_P (TREE_TYPE (t)))
11249 /* A closure type for a lambda in an NSDMI or default argument.
11250 Ignore it; it will be regenerated when needed. */;
11251 else
11252 {
11253 /* Build new TYPE_FIELDS. */
11254 if (TREE_CODE (t) == STATIC_ASSERT)
11255 {
11256 tree condition;
11257
11258 ++c_inhibit_evaluation_warnings;
11259 condition =
11260 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
11261 tf_warning_or_error, NULL_TREE,
11262 /*integral_constant_expression_p=*/true);
11263 --c_inhibit_evaluation_warnings;
11264
11265 finish_static_assert (condition,
11266 STATIC_ASSERT_MESSAGE (t),
11267 STATIC_ASSERT_SOURCE_LOCATION (t),
11268 /*member_p=*/true);
11269 }
11270 else if (TREE_CODE (t) != CONST_DECL)
11271 {
11272 tree r;
11273 tree vec = NULL_TREE;
11274 int len = 1;
11275
11276 /* The file and line for this declaration, to
11277 assist in error message reporting. Since we
11278 called push_tinst_level above, we don't need to
11279 restore these. */
11280 input_location = DECL_SOURCE_LOCATION (t);
11281
11282 if (TREE_CODE (t) == TEMPLATE_DECL)
11283 ++processing_template_decl;
11284 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
11285 if (TREE_CODE (t) == TEMPLATE_DECL)
11286 --processing_template_decl;
11287
11288 if (TREE_CODE (r) == TREE_VEC)
11289 {
11290 /* A capture pack became multiple fields. */
11291 vec = r;
11292 len = TREE_VEC_LENGTH (vec);
11293 }
11294
11295 for (int i = 0; i < len; ++i)
11296 {
11297 if (vec)
11298 r = TREE_VEC_ELT (vec, i);
11299 if (VAR_P (r))
11300 {
11301 /* In [temp.inst]:
11302
11303 [t]he initialization (and any associated
11304 side-effects) of a static data member does
11305 not occur unless the static data member is
11306 itself used in a way that requires the
11307 definition of the static data member to
11308 exist.
11309
11310 Therefore, we do not substitute into the
11311 initialized for the static data member here. */
11312 finish_static_data_member_decl
11313 (r,
11314 /*init=*/NULL_TREE,
11315 /*init_const_expr_p=*/false,
11316 /*asmspec_tree=*/NULL_TREE,
11317 /*flags=*/0);
11318 /* Instantiate members marked with attribute used. */
11319 if (r != error_mark_node && DECL_PRESERVE_P (r))
11320 mark_used (r);
11321 }
11322 else if (TREE_CODE (r) == FIELD_DECL)
11323 {
11324 /* Determine whether R has a valid type and can be
11325 completed later. If R is invalid, then its type
11326 is replaced by error_mark_node. */
11327 tree rtype = TREE_TYPE (r);
11328 if (can_complete_type_without_circularity (rtype))
11329 complete_type (rtype);
11330
11331 if (!complete_or_array_type_p (rtype))
11332 {
11333 /* If R's type couldn't be completed and
11334 it isn't a flexible array member (whose
11335 type is incomplete by definition) give
11336 an error. */
11337 cxx_incomplete_type_error (r, rtype);
11338 TREE_TYPE (r) = error_mark_node;
11339 }
11340 else if (TREE_CODE (rtype) == ARRAY_TYPE
11341 && TYPE_DOMAIN (rtype) == NULL_TREE
11342 && (TREE_CODE (type) == UNION_TYPE
11343 || TREE_CODE (type) == QUAL_UNION_TYPE))
11344 {
11345 error ("flexible array member %qD in union", r);
11346 TREE_TYPE (r) = error_mark_node;
11347 }
11348 }
11349
11350 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
11351 such a thing will already have been added to the field
11352 list by tsubst_enum in finish_member_declaration in the
11353 CLASSTYPE_NESTED_UTDS case above. */
11354 if (!(TREE_CODE (r) == TYPE_DECL
11355 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
11356 && DECL_ARTIFICIAL (r)))
11357 {
11358 set_current_access_from_decl (r);
11359 finish_member_declaration (r);
11360 }
11361 }
11362 }
11363 }
11364 }
11365 else
11366 {
11367 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
11368 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
11369 {
11370 /* Build new CLASSTYPE_FRIEND_CLASSES. */
11371
11372 tree friend_type = t;
11373 bool adjust_processing_template_decl = false;
11374
11375 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11376 {
11377 /* template <class T> friend class C; */
11378 friend_type = tsubst_friend_class (friend_type, args);
11379 adjust_processing_template_decl = true;
11380 }
11381 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
11382 {
11383 /* template <class T> friend class C::D; */
11384 friend_type = tsubst (friend_type, args,
11385 tf_warning_or_error, NULL_TREE);
11386 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11387 friend_type = TREE_TYPE (friend_type);
11388 adjust_processing_template_decl = true;
11389 }
11390 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
11391 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
11392 {
11393 /* This could be either
11394
11395 friend class T::C;
11396
11397 when dependent_type_p is false or
11398
11399 template <class U> friend class T::C;
11400
11401 otherwise. */
11402 /* Bump processing_template_decl in case this is something like
11403 template <class T> friend struct A<T>::B. */
11404 ++processing_template_decl;
11405 friend_type = tsubst (friend_type, args,
11406 tf_warning_or_error, NULL_TREE);
11407 if (dependent_type_p (friend_type))
11408 adjust_processing_template_decl = true;
11409 --processing_template_decl;
11410 }
11411 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
11412 && !CLASSTYPE_USE_TEMPLATE (friend_type)
11413 && TYPE_HIDDEN_P (friend_type))
11414 {
11415 /* friend class C;
11416
11417 where C hasn't been declared yet. Let's lookup name
11418 from namespace scope directly, bypassing any name that
11419 come from dependent base class. */
11420 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
11421
11422 /* The call to xref_tag_from_type does injection for friend
11423 classes. */
11424 push_nested_namespace (ns);
11425 friend_type =
11426 xref_tag_from_type (friend_type, NULL_TREE,
11427 /*tag_scope=*/ts_current);
11428 pop_nested_namespace (ns);
11429 }
11430 else if (uses_template_parms (friend_type))
11431 /* friend class C<T>; */
11432 friend_type = tsubst (friend_type, args,
11433 tf_warning_or_error, NULL_TREE);
11434 /* Otherwise it's
11435
11436 friend class C;
11437
11438 where C is already declared or
11439
11440 friend class C<int>;
11441
11442 We don't have to do anything in these cases. */
11443
11444 if (adjust_processing_template_decl)
11445 /* Trick make_friend_class into realizing that the friend
11446 we're adding is a template, not an ordinary class. It's
11447 important that we use make_friend_class since it will
11448 perform some error-checking and output cross-reference
11449 information. */
11450 ++processing_template_decl;
11451
11452 if (friend_type != error_mark_node)
11453 make_friend_class (type, friend_type, /*complain=*/false);
11454
11455 if (adjust_processing_template_decl)
11456 --processing_template_decl;
11457 }
11458 else
11459 {
11460 /* Build new DECL_FRIENDLIST. */
11461 tree r;
11462
11463 /* The file and line for this declaration, to
11464 assist in error message reporting. Since we
11465 called push_tinst_level above, we don't need to
11466 restore these. */
11467 input_location = DECL_SOURCE_LOCATION (t);
11468
11469 if (TREE_CODE (t) == TEMPLATE_DECL)
11470 {
11471 ++processing_template_decl;
11472 push_deferring_access_checks (dk_no_check);
11473 }
11474
11475 r = tsubst_friend_function (t, args);
11476 add_friend (type, r, /*complain=*/false);
11477 if (TREE_CODE (t) == TEMPLATE_DECL)
11478 {
11479 pop_deferring_access_checks ();
11480 --processing_template_decl;
11481 }
11482 }
11483 }
11484 }
11485
11486 if (fn_context)
11487 {
11488 /* Restore these before substituting into the lambda capture
11489 initializers. */
11490 cp_unevaluated_operand = saved_unevaluated_operand;
11491 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
11492 }
11493
11494 /* Set the file and line number information to whatever is given for
11495 the class itself. This puts error messages involving generated
11496 implicit functions at a predictable point, and the same point
11497 that would be used for non-template classes. */
11498 input_location = DECL_SOURCE_LOCATION (typedecl);
11499
11500 unreverse_member_declarations (type);
11501 finish_struct_1 (type);
11502 TYPE_BEING_DEFINED (type) = 0;
11503
11504 /* We don't instantiate default arguments for member functions. 14.7.1:
11505
11506 The implicit instantiation of a class template specialization causes
11507 the implicit instantiation of the declarations, but not of the
11508 definitions or default arguments, of the class member functions,
11509 member classes, static data members and member templates.... */
11510
11511 /* Some typedefs referenced from within the template code need to be access
11512 checked at template instantiation time, i.e now. These types were
11513 added to the template at parsing time. Let's get those and perform
11514 the access checks then. */
11515 perform_typedefs_access_check (pattern, args);
11516 perform_deferred_access_checks (tf_warning_or_error);
11517 pop_nested_class ();
11518 maximum_field_alignment = saved_maximum_field_alignment;
11519 if (!fn_context)
11520 pop_from_top_level ();
11521 pop_tinst_level ();
11522
11523 /* The vtable for a template class can be emitted in any translation
11524 unit in which the class is instantiated. When there is no key
11525 method, however, finish_struct_1 will already have added TYPE to
11526 the keyed_classes. */
11527 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
11528 vec_safe_push (keyed_classes, type);
11529
11530 return type;
11531 }
11532
11533 /* Wrapper for instantiate_class_template_1. */
11534
11535 tree
11536 instantiate_class_template (tree type)
11537 {
11538 tree ret;
11539 timevar_push (TV_TEMPLATE_INST);
11540 ret = instantiate_class_template_1 (type);
11541 timevar_pop (TV_TEMPLATE_INST);
11542 return ret;
11543 }
11544
11545 static tree
11546 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11547 {
11548 tree r;
11549
11550 if (!t)
11551 r = t;
11552 else if (TYPE_P (t))
11553 r = tsubst (t, args, complain, in_decl);
11554 else
11555 {
11556 if (!(complain & tf_warning))
11557 ++c_inhibit_evaluation_warnings;
11558 r = tsubst_expr (t, args, complain, in_decl,
11559 /*integral_constant_expression_p=*/true);
11560 if (!(complain & tf_warning))
11561 --c_inhibit_evaluation_warnings;
11562 }
11563 return r;
11564 }
11565
11566 /* Given a function parameter pack TMPL_PARM and some function parameters
11567 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
11568 and set *SPEC_P to point at the next point in the list. */
11569
11570 tree
11571 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
11572 {
11573 /* Collect all of the extra "packed" parameters into an
11574 argument pack. */
11575 tree parmvec;
11576 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
11577 tree spec_parm = *spec_p;
11578 int i, len;
11579
11580 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
11581 if (tmpl_parm
11582 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
11583 break;
11584
11585 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
11586 parmvec = make_tree_vec (len);
11587 spec_parm = *spec_p;
11588 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
11589 {
11590 tree elt = spec_parm;
11591 if (DECL_PACK_P (elt))
11592 elt = make_pack_expansion (elt);
11593 TREE_VEC_ELT (parmvec, i) = elt;
11594 }
11595
11596 /* Build the argument packs. */
11597 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
11598 *spec_p = spec_parm;
11599
11600 return argpack;
11601 }
11602
11603 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
11604 NONTYPE_ARGUMENT_PACK. */
11605
11606 static tree
11607 make_fnparm_pack (tree spec_parm)
11608 {
11609 return extract_fnparm_pack (NULL_TREE, &spec_parm);
11610 }
11611
11612 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
11613 pack expansion with no extra args, 2 if it has extra args, or 0
11614 if it is not a pack expansion. */
11615
11616 static int
11617 argument_pack_element_is_expansion_p (tree arg_pack, int i)
11618 {
11619 if (TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
11620 /* We're being called before this happens in tsubst_pack_expansion. */
11621 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
11622 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
11623 if (i >= TREE_VEC_LENGTH (vec))
11624 return 0;
11625 tree elt = TREE_VEC_ELT (vec, i);
11626 if (DECL_P (elt))
11627 /* A decl pack is itself an expansion. */
11628 elt = TREE_TYPE (elt);
11629 if (!PACK_EXPANSION_P (elt))
11630 return 0;
11631 if (PACK_EXPANSION_EXTRA_ARGS (elt))
11632 return 2;
11633 return 1;
11634 }
11635
11636
11637 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11638
11639 static tree
11640 make_argument_pack_select (tree arg_pack, unsigned index)
11641 {
11642 tree aps = make_node (ARGUMENT_PACK_SELECT);
11643
11644 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
11645 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11646
11647 return aps;
11648 }
11649
11650 /* This is a subroutine of tsubst_pack_expansion.
11651
11652 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11653 mechanism to store the (non complete list of) arguments of the
11654 substitution and return a non substituted pack expansion, in order
11655 to wait for when we have enough arguments to really perform the
11656 substitution. */
11657
11658 static bool
11659 use_pack_expansion_extra_args_p (tree parm_packs,
11660 int arg_pack_len,
11661 bool has_empty_arg)
11662 {
11663 /* If one pack has an expansion and another pack has a normal
11664 argument or if one pack has an empty argument and an another
11665 one hasn't then tsubst_pack_expansion cannot perform the
11666 substitution and need to fall back on the
11667 PACK_EXPANSION_EXTRA mechanism. */
11668 if (parm_packs == NULL_TREE)
11669 return false;
11670 else if (has_empty_arg)
11671 return true;
11672
11673 bool has_expansion_arg = false;
11674 for (int i = 0 ; i < arg_pack_len; ++i)
11675 {
11676 bool has_non_expansion_arg = false;
11677 for (tree parm_pack = parm_packs;
11678 parm_pack;
11679 parm_pack = TREE_CHAIN (parm_pack))
11680 {
11681 tree arg = TREE_VALUE (parm_pack);
11682
11683 int exp = argument_pack_element_is_expansion_p (arg, i);
11684 if (exp == 2)
11685 /* We can't substitute a pack expansion with extra args into
11686 our pattern. */
11687 return true;
11688 else if (exp)
11689 has_expansion_arg = true;
11690 else
11691 has_non_expansion_arg = true;
11692 }
11693
11694 if (has_expansion_arg && has_non_expansion_arg)
11695 return true;
11696 }
11697 return false;
11698 }
11699
11700 /* [temp.variadic]/6 says that:
11701
11702 The instantiation of a pack expansion [...]
11703 produces a list E1,E2, ..., En, where N is the number of elements
11704 in the pack expansion parameters.
11705
11706 This subroutine of tsubst_pack_expansion produces one of these Ei.
11707
11708 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11709 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11710 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11711 INDEX is the index 'i' of the element Ei to produce. ARGS,
11712 COMPLAIN, and IN_DECL are the same parameters as for the
11713 tsubst_pack_expansion function.
11714
11715 The function returns the resulting Ei upon successful completion,
11716 or error_mark_node.
11717
11718 Note that this function possibly modifies the ARGS parameter, so
11719 it's the responsibility of the caller to restore it. */
11720
11721 static tree
11722 gen_elem_of_pack_expansion_instantiation (tree pattern,
11723 tree parm_packs,
11724 unsigned index,
11725 tree args /* This parm gets
11726 modified. */,
11727 tsubst_flags_t complain,
11728 tree in_decl)
11729 {
11730 tree t;
11731 bool ith_elem_is_expansion = false;
11732
11733 /* For each parameter pack, change the substitution of the parameter
11734 pack to the ith argument in its argument pack, then expand the
11735 pattern. */
11736 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
11737 {
11738 tree parm = TREE_PURPOSE (pack);
11739 tree arg_pack = TREE_VALUE (pack);
11740 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
11741
11742 ith_elem_is_expansion |=
11743 argument_pack_element_is_expansion_p (arg_pack, index);
11744
11745 /* Select the Ith argument from the pack. */
11746 if (TREE_CODE (parm) == PARM_DECL
11747 || VAR_P (parm)
11748 || TREE_CODE (parm) == FIELD_DECL)
11749 {
11750 if (index == 0)
11751 {
11752 aps = make_argument_pack_select (arg_pack, index);
11753 if (!mark_used (parm, complain) && !(complain & tf_error))
11754 return error_mark_node;
11755 register_local_specialization (aps, parm);
11756 }
11757 else
11758 aps = retrieve_local_specialization (parm);
11759 }
11760 else
11761 {
11762 int idx, level;
11763 template_parm_level_and_index (parm, &level, &idx);
11764
11765 if (index == 0)
11766 {
11767 aps = make_argument_pack_select (arg_pack, index);
11768 /* Update the corresponding argument. */
11769 TMPL_ARG (args, level, idx) = aps;
11770 }
11771 else
11772 /* Re-use the ARGUMENT_PACK_SELECT. */
11773 aps = TMPL_ARG (args, level, idx);
11774 }
11775 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11776 }
11777
11778 /* Substitute into the PATTERN with the (possibly altered)
11779 arguments. */
11780 if (pattern == in_decl)
11781 /* Expanding a fixed parameter pack from
11782 coerce_template_parameter_pack. */
11783 t = tsubst_decl (pattern, args, complain);
11784 else if (pattern == error_mark_node)
11785 t = error_mark_node;
11786 else if (constraint_p (pattern))
11787 {
11788 if (processing_template_decl)
11789 t = tsubst_constraint (pattern, args, complain, in_decl);
11790 else
11791 t = (constraints_satisfied_p (pattern, args)
11792 ? boolean_true_node : boolean_false_node);
11793 }
11794 else if (!TYPE_P (pattern))
11795 t = tsubst_expr (pattern, args, complain, in_decl,
11796 /*integral_constant_expression_p=*/false);
11797 else
11798 t = tsubst (pattern, args, complain, in_decl);
11799
11800 /* If the Ith argument pack element is a pack expansion, then
11801 the Ith element resulting from the substituting is going to
11802 be a pack expansion as well. */
11803 if (ith_elem_is_expansion)
11804 t = make_pack_expansion (t, complain);
11805
11806 return t;
11807 }
11808
11809 /* When the unexpanded parameter pack in a fold expression expands to an empty
11810 sequence, the value of the expression is as follows; the program is
11811 ill-formed if the operator is not listed in this table.
11812
11813 && true
11814 || false
11815 , void() */
11816
11817 tree
11818 expand_empty_fold (tree t, tsubst_flags_t complain)
11819 {
11820 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
11821 if (!FOLD_EXPR_MODIFY_P (t))
11822 switch (code)
11823 {
11824 case TRUTH_ANDIF_EXPR:
11825 return boolean_true_node;
11826 case TRUTH_ORIF_EXPR:
11827 return boolean_false_node;
11828 case COMPOUND_EXPR:
11829 return void_node;
11830 default:
11831 break;
11832 }
11833
11834 if (complain & tf_error)
11835 error_at (location_of (t),
11836 "fold of empty expansion over %O", code);
11837 return error_mark_node;
11838 }
11839
11840 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11841 form an expression that combines the two terms using the
11842 operator of T. */
11843
11844 static tree
11845 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
11846 {
11847 tree op = FOLD_EXPR_OP (t);
11848 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
11849
11850 // Handle compound assignment operators.
11851 if (FOLD_EXPR_MODIFY_P (t))
11852 return build_x_modify_expr (input_location, left, code, right, complain);
11853
11854 switch (code)
11855 {
11856 case COMPOUND_EXPR:
11857 return build_x_compound_expr (input_location, left, right, complain);
11858 default:
11859 return build_x_binary_op (input_location, code,
11860 left, TREE_CODE (left),
11861 right, TREE_CODE (right),
11862 /*overload=*/NULL,
11863 complain);
11864 }
11865 }
11866
11867 /* Substitute ARGS into the pack of a fold expression T. */
11868
11869 static inline tree
11870 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11871 {
11872 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
11873 }
11874
11875 /* Substitute ARGS into the pack of a fold expression T. */
11876
11877 static inline tree
11878 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11879 {
11880 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
11881 }
11882
11883 /* Expand a PACK of arguments into a grouped as left fold.
11884 Given a pack containing elements A0, A1, ..., An and an
11885 operator @, this builds the expression:
11886
11887 ((A0 @ A1) @ A2) ... @ An
11888
11889 Note that PACK must not be empty.
11890
11891 The operator is defined by the original fold expression T. */
11892
11893 static tree
11894 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
11895 {
11896 tree left = TREE_VEC_ELT (pack, 0);
11897 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
11898 {
11899 tree right = TREE_VEC_ELT (pack, i);
11900 left = fold_expression (t, left, right, complain);
11901 }
11902 return left;
11903 }
11904
11905 /* Substitute into a unary left fold expression. */
11906
11907 static tree
11908 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
11909 tree in_decl)
11910 {
11911 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11912 if (pack == error_mark_node)
11913 return error_mark_node;
11914 if (PACK_EXPANSION_P (pack))
11915 {
11916 tree r = copy_node (t);
11917 FOLD_EXPR_PACK (r) = pack;
11918 return r;
11919 }
11920 if (TREE_VEC_LENGTH (pack) == 0)
11921 return expand_empty_fold (t, complain);
11922 else
11923 return expand_left_fold (t, pack, complain);
11924 }
11925
11926 /* Substitute into a binary left fold expression.
11927
11928 Do ths by building a single (non-empty) vector of argumnts and
11929 building the expression from those elements. */
11930
11931 static tree
11932 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
11933 tree in_decl)
11934 {
11935 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11936 if (pack == error_mark_node)
11937 return error_mark_node;
11938 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11939 if (init == error_mark_node)
11940 return error_mark_node;
11941
11942 if (PACK_EXPANSION_P (pack))
11943 {
11944 tree r = copy_node (t);
11945 FOLD_EXPR_PACK (r) = pack;
11946 FOLD_EXPR_INIT (r) = init;
11947 return r;
11948 }
11949
11950 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
11951 TREE_VEC_ELT (vec, 0) = init;
11952 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
11953 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
11954
11955 return expand_left_fold (t, vec, complain);
11956 }
11957
11958 /* Expand a PACK of arguments into a grouped as right fold.
11959 Given a pack containing elementns A0, A1, ..., and an
11960 operator @, this builds the expression:
11961
11962 A0@ ... (An-2 @ (An-1 @ An))
11963
11964 Note that PACK must not be empty.
11965
11966 The operator is defined by the original fold expression T. */
11967
11968 tree
11969 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
11970 {
11971 // Build the expression.
11972 int n = TREE_VEC_LENGTH (pack);
11973 tree right = TREE_VEC_ELT (pack, n - 1);
11974 for (--n; n != 0; --n)
11975 {
11976 tree left = TREE_VEC_ELT (pack, n - 1);
11977 right = fold_expression (t, left, right, complain);
11978 }
11979 return right;
11980 }
11981
11982 /* Substitute into a unary right fold expression. */
11983
11984 static tree
11985 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
11986 tree in_decl)
11987 {
11988 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11989 if (pack == error_mark_node)
11990 return error_mark_node;
11991 if (PACK_EXPANSION_P (pack))
11992 {
11993 tree r = copy_node (t);
11994 FOLD_EXPR_PACK (r) = pack;
11995 return r;
11996 }
11997 if (TREE_VEC_LENGTH (pack) == 0)
11998 return expand_empty_fold (t, complain);
11999 else
12000 return expand_right_fold (t, pack, complain);
12001 }
12002
12003 /* Substitute into a binary right fold expression.
12004
12005 Do ths by building a single (non-empty) vector of arguments and
12006 building the expression from those elements. */
12007
12008 static tree
12009 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
12010 tree in_decl)
12011 {
12012 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12013 if (pack == error_mark_node)
12014 return error_mark_node;
12015 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12016 if (init == error_mark_node)
12017 return error_mark_node;
12018
12019 if (PACK_EXPANSION_P (pack))
12020 {
12021 tree r = copy_node (t);
12022 FOLD_EXPR_PACK (r) = pack;
12023 FOLD_EXPR_INIT (r) = init;
12024 return r;
12025 }
12026
12027 int n = TREE_VEC_LENGTH (pack);
12028 tree vec = make_tree_vec (n + 1);
12029 for (int i = 0; i < n; ++i)
12030 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
12031 TREE_VEC_ELT (vec, n) = init;
12032
12033 return expand_right_fold (t, vec, complain);
12034 }
12035
12036 /* Walk through the pattern of a pack expansion, adding everything in
12037 local_specializations to a list. */
12038
12039 class el_data
12040 {
12041 public:
12042 hash_set<tree> internal;
12043 tree extra;
12044 tsubst_flags_t complain;
12045
12046 el_data (tsubst_flags_t c)
12047 : extra (NULL_TREE), complain (c) {}
12048 };
12049 static tree
12050 extract_locals_r (tree *tp, int */*walk_subtrees*/, void *data_)
12051 {
12052 el_data &data = *reinterpret_cast<el_data*>(data_);
12053 tree *extra = &data.extra;
12054 tsubst_flags_t complain = data.complain;
12055
12056 if (TYPE_P (*tp) && typedef_variant_p (*tp))
12057 /* Remember local typedefs (85214). */
12058 tp = &TYPE_NAME (*tp);
12059
12060 if (TREE_CODE (*tp) == DECL_EXPR)
12061 data.internal.add (DECL_EXPR_DECL (*tp));
12062 else if (tree spec = retrieve_local_specialization (*tp))
12063 {
12064 if (data.internal.contains (*tp))
12065 /* Don't mess with variables declared within the pattern. */
12066 return NULL_TREE;
12067 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12068 {
12069 /* Maybe pull out the PARM_DECL for a partial instantiation. */
12070 tree args = ARGUMENT_PACK_ARGS (spec);
12071 if (TREE_VEC_LENGTH (args) == 1)
12072 {
12073 tree elt = TREE_VEC_ELT (args, 0);
12074 if (PACK_EXPANSION_P (elt))
12075 elt = PACK_EXPANSION_PATTERN (elt);
12076 if (DECL_PACK_P (elt))
12077 spec = elt;
12078 }
12079 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12080 {
12081 /* Handle lambda capture here, since we aren't doing any
12082 substitution now, and so tsubst_copy won't call
12083 process_outer_var_ref. */
12084 tree args = ARGUMENT_PACK_ARGS (spec);
12085 int len = TREE_VEC_LENGTH (args);
12086 for (int i = 0; i < len; ++i)
12087 {
12088 tree arg = TREE_VEC_ELT (args, i);
12089 tree carg = arg;
12090 if (outer_automatic_var_p (arg))
12091 carg = process_outer_var_ref (arg, complain);
12092 if (carg != arg)
12093 {
12094 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12095 proxies. */
12096 if (i == 0)
12097 {
12098 spec = copy_node (spec);
12099 args = copy_node (args);
12100 SET_ARGUMENT_PACK_ARGS (spec, args);
12101 register_local_specialization (spec, *tp);
12102 }
12103 TREE_VEC_ELT (args, i) = carg;
12104 }
12105 }
12106 }
12107 }
12108 if (outer_automatic_var_p (spec))
12109 spec = process_outer_var_ref (spec, complain);
12110 *extra = tree_cons (*tp, spec, *extra);
12111 }
12112 return NULL_TREE;
12113 }
12114 static tree
12115 extract_local_specs (tree pattern, tsubst_flags_t complain)
12116 {
12117 el_data data (complain);
12118 cp_walk_tree_without_duplicates (&pattern, extract_locals_r, &data);
12119 return data.extra;
12120 }
12121
12122 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12123 for use in PACK_EXPANSION_EXTRA_ARGS. */
12124
12125 tree
12126 build_extra_args (tree pattern, tree args, tsubst_flags_t complain)
12127 {
12128 tree extra = args;
12129 if (local_specializations)
12130 if (tree locals = extract_local_specs (pattern, complain))
12131 extra = tree_cons (NULL_TREE, extra, locals);
12132 return extra;
12133 }
12134
12135 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12136 normal template args to ARGS. */
12137
12138 tree
12139 add_extra_args (tree extra, tree args)
12140 {
12141 if (extra && TREE_CODE (extra) == TREE_LIST)
12142 {
12143 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
12144 {
12145 /* The partial instantiation involved local declarations collected in
12146 extract_local_specs; map from the general template to our local
12147 context. */
12148 tree gen = TREE_PURPOSE (elt);
12149 tree inst = TREE_VALUE (elt);
12150 if (DECL_P (inst))
12151 if (tree local = retrieve_local_specialization (inst))
12152 inst = local;
12153 /* else inst is already a full instantiation of the pack. */
12154 register_local_specialization (inst, gen);
12155 }
12156 gcc_assert (!TREE_PURPOSE (extra));
12157 extra = TREE_VALUE (extra);
12158 }
12159 return add_to_template_args (extra, args);
12160 }
12161
12162 /* Substitute ARGS into T, which is an pack expansion
12163 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12164 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12165 (if only a partial substitution could be performed) or
12166 ERROR_MARK_NODE if there was an error. */
12167 tree
12168 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
12169 tree in_decl)
12170 {
12171 tree pattern;
12172 tree pack, packs = NULL_TREE;
12173 bool unsubstituted_packs = false;
12174 bool unsubstituted_fn_pack = false;
12175 int i, len = -1;
12176 tree result;
12177 hash_map<tree, tree> *saved_local_specializations = NULL;
12178 bool need_local_specializations = false;
12179 int levels;
12180
12181 gcc_assert (PACK_EXPANSION_P (t));
12182 pattern = PACK_EXPANSION_PATTERN (t);
12183
12184 /* Add in any args remembered from an earlier partial instantiation. */
12185 args = add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
12186
12187 levels = TMPL_ARGS_DEPTH (args);
12188
12189 /* Determine the argument packs that will instantiate the parameter
12190 packs used in the expansion expression. While we're at it,
12191 compute the number of arguments to be expanded and make sure it
12192 is consistent. */
12193 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
12194 pack = TREE_CHAIN (pack))
12195 {
12196 tree parm_pack = TREE_VALUE (pack);
12197 tree arg_pack = NULL_TREE;
12198 tree orig_arg = NULL_TREE;
12199 int level = 0;
12200
12201 if (TREE_CODE (parm_pack) == BASES)
12202 {
12203 gcc_assert (parm_pack == pattern);
12204 if (BASES_DIRECT (parm_pack))
12205 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
12206 args, complain,
12207 in_decl, false),
12208 complain);
12209 else
12210 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
12211 args, complain, in_decl,
12212 false), complain);
12213 }
12214 else if (builtin_pack_call_p (parm_pack))
12215 {
12216 if (parm_pack != pattern)
12217 {
12218 if (complain & tf_error)
12219 sorry ("%qE is not the entire pattern of the pack expansion",
12220 parm_pack);
12221 return error_mark_node;
12222 }
12223 return expand_builtin_pack_call (parm_pack, args,
12224 complain, in_decl);
12225 }
12226 else if (TREE_CODE (parm_pack) == PARM_DECL)
12227 {
12228 /* We know we have correct local_specializations if this
12229 expansion is at function scope, or if we're dealing with a
12230 local parameter in a requires expression; for the latter,
12231 tsubst_requires_expr set it up appropriately. */
12232 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
12233 arg_pack = retrieve_local_specialization (parm_pack);
12234 else
12235 /* We can't rely on local_specializations for a parameter
12236 name used later in a function declaration (such as in a
12237 late-specified return type). Even if it exists, it might
12238 have the wrong value for a recursive call. */
12239 need_local_specializations = true;
12240
12241 if (!arg_pack)
12242 {
12243 /* This parameter pack was used in an unevaluated context. Just
12244 make a dummy decl, since it's only used for its type. */
12245 ++cp_unevaluated_operand;
12246 arg_pack = tsubst_decl (parm_pack, args, complain);
12247 --cp_unevaluated_operand;
12248 if (arg_pack && DECL_PACK_P (arg_pack))
12249 /* Partial instantiation of the parm_pack, we can't build
12250 up an argument pack yet. */
12251 arg_pack = NULL_TREE;
12252 else
12253 arg_pack = make_fnparm_pack (arg_pack);
12254 }
12255 else if (argument_pack_element_is_expansion_p (arg_pack, 0))
12256 /* This argument pack isn't fully instantiated yet. We set this
12257 flag rather than clear arg_pack because we do want to do the
12258 optimization below, and we don't want to substitute directly
12259 into the pattern (as that would expose a NONTYPE_ARGUMENT_PACK
12260 where it isn't expected). */
12261 unsubstituted_fn_pack = true;
12262 }
12263 else if (is_capture_proxy (parm_pack))
12264 {
12265 arg_pack = retrieve_local_specialization (parm_pack);
12266 if (argument_pack_element_is_expansion_p (arg_pack, 0))
12267 unsubstituted_fn_pack = true;
12268 }
12269 else
12270 {
12271 int idx;
12272 template_parm_level_and_index (parm_pack, &level, &idx);
12273
12274 if (level <= levels)
12275 arg_pack = TMPL_ARG (args, level, idx);
12276 }
12277
12278 orig_arg = arg_pack;
12279 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12280 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12281
12282 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
12283 /* This can only happen if we forget to expand an argument
12284 pack somewhere else. Just return an error, silently. */
12285 {
12286 result = make_tree_vec (1);
12287 TREE_VEC_ELT (result, 0) = error_mark_node;
12288 return result;
12289 }
12290
12291 if (arg_pack)
12292 {
12293 int my_len =
12294 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
12295
12296 /* Don't bother trying to do a partial substitution with
12297 incomplete packs; we'll try again after deduction. */
12298 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
12299 return t;
12300
12301 if (len < 0)
12302 len = my_len;
12303 else if (len != my_len
12304 && !unsubstituted_fn_pack)
12305 {
12306 if (!(complain & tf_error))
12307 /* Fail quietly. */;
12308 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
12309 error ("mismatched argument pack lengths while expanding %qT",
12310 pattern);
12311 else
12312 error ("mismatched argument pack lengths while expanding %qE",
12313 pattern);
12314 return error_mark_node;
12315 }
12316
12317 /* Keep track of the parameter packs and their corresponding
12318 argument packs. */
12319 packs = tree_cons (parm_pack, arg_pack, packs);
12320 TREE_TYPE (packs) = orig_arg;
12321 }
12322 else
12323 {
12324 /* We can't substitute for this parameter pack. We use a flag as
12325 well as the missing_level counter because function parameter
12326 packs don't have a level. */
12327 gcc_assert (processing_template_decl || is_auto (parm_pack));
12328 unsubstituted_packs = true;
12329 }
12330 }
12331
12332 /* If the expansion is just T..., return the matching argument pack, unless
12333 we need to call convert_from_reference on all the elements. This is an
12334 important optimization; see c++/68422. */
12335 if (!unsubstituted_packs
12336 && TREE_PURPOSE (packs) == pattern)
12337 {
12338 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
12339
12340 /* If the argument pack is a single pack expansion, pull it out. */
12341 if (TREE_VEC_LENGTH (args) == 1
12342 && pack_expansion_args_count (args))
12343 return TREE_VEC_ELT (args, 0);
12344
12345 /* Types need no adjustment, nor does sizeof..., and if we still have
12346 some pack expansion args we won't do anything yet. */
12347 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
12348 || PACK_EXPANSION_SIZEOF_P (t)
12349 || pack_expansion_args_count (args))
12350 return args;
12351 /* Also optimize expression pack expansions if we can tell that the
12352 elements won't have reference type. */
12353 tree type = TREE_TYPE (pattern);
12354 if (type && !TYPE_REF_P (type)
12355 && !PACK_EXPANSION_P (type)
12356 && !WILDCARD_TYPE_P (type))
12357 return args;
12358 /* Otherwise use the normal path so we get convert_from_reference. */
12359 }
12360
12361 /* We cannot expand this expansion expression, because we don't have
12362 all of the argument packs we need. */
12363 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
12364 {
12365 /* We got some full packs, but we can't substitute them in until we
12366 have values for all the packs. So remember these until then. */
12367
12368 t = make_pack_expansion (pattern, complain);
12369 PACK_EXPANSION_EXTRA_ARGS (t)
12370 = build_extra_args (pattern, args, complain);
12371 return t;
12372 }
12373 else if (unsubstituted_packs)
12374 {
12375 /* There were no real arguments, we're just replacing a parameter
12376 pack with another version of itself. Substitute into the
12377 pattern and return a PACK_EXPANSION_*. The caller will need to
12378 deal with that. */
12379 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
12380 t = tsubst_expr (pattern, args, complain, in_decl,
12381 /*integral_constant_expression_p=*/false);
12382 else
12383 t = tsubst (pattern, args, complain, in_decl);
12384 t = make_pack_expansion (t, complain);
12385 return t;
12386 }
12387
12388 gcc_assert (len >= 0);
12389
12390 if (need_local_specializations)
12391 {
12392 /* We're in a late-specified return type, so create our own local
12393 specializations map; the current map is either NULL or (in the
12394 case of recursive unification) might have bindings that we don't
12395 want to use or alter. */
12396 saved_local_specializations = local_specializations;
12397 local_specializations = new hash_map<tree, tree>;
12398 }
12399
12400 /* For each argument in each argument pack, substitute into the
12401 pattern. */
12402 result = make_tree_vec (len);
12403 tree elem_args = copy_template_args (args);
12404 for (i = 0; i < len; ++i)
12405 {
12406 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
12407 i,
12408 elem_args, complain,
12409 in_decl);
12410 TREE_VEC_ELT (result, i) = t;
12411 if (t == error_mark_node)
12412 {
12413 result = error_mark_node;
12414 break;
12415 }
12416 }
12417
12418 /* Update ARGS to restore the substitution from parameter packs to
12419 their argument packs. */
12420 for (pack = packs; pack; pack = TREE_CHAIN (pack))
12421 {
12422 tree parm = TREE_PURPOSE (pack);
12423
12424 if (TREE_CODE (parm) == PARM_DECL
12425 || VAR_P (parm)
12426 || TREE_CODE (parm) == FIELD_DECL)
12427 register_local_specialization (TREE_TYPE (pack), parm);
12428 else
12429 {
12430 int idx, level;
12431
12432 if (TREE_VALUE (pack) == NULL_TREE)
12433 continue;
12434
12435 template_parm_level_and_index (parm, &level, &idx);
12436
12437 /* Update the corresponding argument. */
12438 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
12439 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
12440 TREE_TYPE (pack);
12441 else
12442 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
12443 }
12444 }
12445
12446 if (need_local_specializations)
12447 {
12448 delete local_specializations;
12449 local_specializations = saved_local_specializations;
12450 }
12451
12452 /* If the dependent pack arguments were such that we end up with only a
12453 single pack expansion again, there's no need to keep it in a TREE_VEC. */
12454 if (len == 1 && TREE_CODE (result) == TREE_VEC
12455 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
12456 return TREE_VEC_ELT (result, 0);
12457
12458 return result;
12459 }
12460
12461 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
12462 TMPL. We do this using DECL_PARM_INDEX, which should work even with
12463 parameter packs; all parms generated from a function parameter pack will
12464 have the same DECL_PARM_INDEX. */
12465
12466 tree
12467 get_pattern_parm (tree parm, tree tmpl)
12468 {
12469 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
12470 tree patparm;
12471
12472 if (DECL_ARTIFICIAL (parm))
12473 {
12474 for (patparm = DECL_ARGUMENTS (pattern);
12475 patparm; patparm = DECL_CHAIN (patparm))
12476 if (DECL_ARTIFICIAL (patparm)
12477 && DECL_NAME (parm) == DECL_NAME (patparm))
12478 break;
12479 }
12480 else
12481 {
12482 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
12483 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
12484 gcc_assert (DECL_PARM_INDEX (patparm)
12485 == DECL_PARM_INDEX (parm));
12486 }
12487
12488 return patparm;
12489 }
12490
12491 /* Make an argument pack out of the TREE_VEC VEC. */
12492
12493 static tree
12494 make_argument_pack (tree vec)
12495 {
12496 tree pack;
12497 tree elt = TREE_VEC_ELT (vec, 0);
12498 if (TYPE_P (elt))
12499 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
12500 else
12501 {
12502 pack = make_node (NONTYPE_ARGUMENT_PACK);
12503 TREE_CONSTANT (pack) = 1;
12504 }
12505 SET_ARGUMENT_PACK_ARGS (pack, vec);
12506 return pack;
12507 }
12508
12509 /* Return an exact copy of template args T that can be modified
12510 independently. */
12511
12512 static tree
12513 copy_template_args (tree t)
12514 {
12515 if (t == error_mark_node)
12516 return t;
12517
12518 int len = TREE_VEC_LENGTH (t);
12519 tree new_vec = make_tree_vec (len);
12520
12521 for (int i = 0; i < len; ++i)
12522 {
12523 tree elt = TREE_VEC_ELT (t, i);
12524 if (elt && TREE_CODE (elt) == TREE_VEC)
12525 elt = copy_template_args (elt);
12526 TREE_VEC_ELT (new_vec, i) = elt;
12527 }
12528
12529 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
12530 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
12531
12532 return new_vec;
12533 }
12534
12535 /* Substitute ARGS into the vector or list of template arguments T. */
12536
12537 static tree
12538 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12539 {
12540 tree orig_t = t;
12541 int len, need_new = 0, i, expanded_len_adjust = 0, out;
12542 tree *elts;
12543
12544 if (t == error_mark_node)
12545 return error_mark_node;
12546
12547 len = TREE_VEC_LENGTH (t);
12548 elts = XALLOCAVEC (tree, len);
12549
12550 for (i = 0; i < len; i++)
12551 {
12552 tree orig_arg = TREE_VEC_ELT (t, i);
12553 tree new_arg;
12554
12555 if (TREE_CODE (orig_arg) == TREE_VEC)
12556 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
12557 else if (PACK_EXPANSION_P (orig_arg))
12558 {
12559 /* Substitute into an expansion expression. */
12560 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
12561
12562 if (TREE_CODE (new_arg) == TREE_VEC)
12563 /* Add to the expanded length adjustment the number of
12564 expanded arguments. We subtract one from this
12565 measurement, because the argument pack expression
12566 itself is already counted as 1 in
12567 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
12568 the argument pack is empty. */
12569 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
12570 }
12571 else if (ARGUMENT_PACK_P (orig_arg))
12572 {
12573 /* Substitute into each of the arguments. */
12574 new_arg = TYPE_P (orig_arg)
12575 ? cxx_make_type (TREE_CODE (orig_arg))
12576 : make_node (TREE_CODE (orig_arg));
12577
12578 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
12579 args, complain, in_decl);
12580 if (pack_args == error_mark_node)
12581 new_arg = error_mark_node;
12582 else
12583 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
12584
12585 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
12586 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
12587 }
12588 else
12589 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
12590
12591 if (new_arg == error_mark_node)
12592 return error_mark_node;
12593
12594 elts[i] = new_arg;
12595 if (new_arg != orig_arg)
12596 need_new = 1;
12597 }
12598
12599 if (!need_new)
12600 return t;
12601
12602 /* Make space for the expanded arguments coming from template
12603 argument packs. */
12604 t = make_tree_vec (len + expanded_len_adjust);
12605 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
12606 arguments for a member template.
12607 In that case each TREE_VEC in ORIG_T represents a level of template
12608 arguments, and ORIG_T won't carry any non defaulted argument count.
12609 It will rather be the nested TREE_VECs that will carry one.
12610 In other words, ORIG_T carries a non defaulted argument count only
12611 if it doesn't contain any nested TREE_VEC. */
12612 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
12613 {
12614 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
12615 count += expanded_len_adjust;
12616 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
12617 }
12618 for (i = 0, out = 0; i < len; i++)
12619 {
12620 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
12621 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
12622 && TREE_CODE (elts[i]) == TREE_VEC)
12623 {
12624 int idx;
12625
12626 /* Now expand the template argument pack "in place". */
12627 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
12628 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
12629 }
12630 else
12631 {
12632 TREE_VEC_ELT (t, out) = elts[i];
12633 out++;
12634 }
12635 }
12636
12637 return t;
12638 }
12639
12640 /* Substitute ARGS into one level PARMS of template parameters. */
12641
12642 static tree
12643 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
12644 {
12645 if (parms == error_mark_node)
12646 return error_mark_node;
12647
12648 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
12649
12650 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
12651 {
12652 tree tuple = TREE_VEC_ELT (parms, i);
12653
12654 if (tuple == error_mark_node)
12655 continue;
12656
12657 TREE_VEC_ELT (new_vec, i) =
12658 tsubst_template_parm (tuple, args, complain);
12659 }
12660
12661 return new_vec;
12662 }
12663
12664 /* Return the result of substituting ARGS into the template parameters
12665 given by PARMS. If there are m levels of ARGS and m + n levels of
12666 PARMS, then the result will contain n levels of PARMS. For
12667 example, if PARMS is `template <class T> template <class U>
12668 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
12669 result will be `template <int*, double, class V>'. */
12670
12671 static tree
12672 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
12673 {
12674 tree r = NULL_TREE;
12675 tree* new_parms;
12676
12677 /* When substituting into a template, we must set
12678 PROCESSING_TEMPLATE_DECL as the template parameters may be
12679 dependent if they are based on one-another, and the dependency
12680 predicates are short-circuit outside of templates. */
12681 ++processing_template_decl;
12682
12683 for (new_parms = &r;
12684 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
12685 new_parms = &(TREE_CHAIN (*new_parms)),
12686 parms = TREE_CHAIN (parms))
12687 {
12688 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
12689 args, complain);
12690 *new_parms =
12691 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
12692 - TMPL_ARGS_DEPTH (args)),
12693 new_vec, NULL_TREE);
12694 }
12695
12696 --processing_template_decl;
12697
12698 return r;
12699 }
12700
12701 /* Return the result of substituting ARGS into one template parameter
12702 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
12703 parameter and which TREE_PURPOSE is the default argument of the
12704 template parameter. */
12705
12706 static tree
12707 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
12708 {
12709 tree default_value, parm_decl;
12710
12711 if (args == NULL_TREE
12712 || t == NULL_TREE
12713 || t == error_mark_node)
12714 return t;
12715
12716 gcc_assert (TREE_CODE (t) == TREE_LIST);
12717
12718 default_value = TREE_PURPOSE (t);
12719 parm_decl = TREE_VALUE (t);
12720
12721 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
12722 if (TREE_CODE (parm_decl) == PARM_DECL
12723 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
12724 parm_decl = error_mark_node;
12725 default_value = tsubst_template_arg (default_value, args,
12726 complain, NULL_TREE);
12727
12728 return build_tree_list (default_value, parm_decl);
12729 }
12730
12731 /* Substitute the ARGS into the indicated aggregate (or enumeration)
12732 type T. If T is not an aggregate or enumeration type, it is
12733 handled as if by tsubst. IN_DECL is as for tsubst. If
12734 ENTERING_SCOPE is nonzero, T is the context for a template which
12735 we are presently tsubst'ing. Return the substituted value. */
12736
12737 static tree
12738 tsubst_aggr_type (tree t,
12739 tree args,
12740 tsubst_flags_t complain,
12741 tree in_decl,
12742 int entering_scope)
12743 {
12744 if (t == NULL_TREE)
12745 return NULL_TREE;
12746
12747 switch (TREE_CODE (t))
12748 {
12749 case RECORD_TYPE:
12750 if (TYPE_PTRMEMFUNC_P (t))
12751 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
12752
12753 /* Fall through. */
12754 case ENUMERAL_TYPE:
12755 case UNION_TYPE:
12756 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
12757 {
12758 tree argvec;
12759 tree context;
12760 tree r;
12761
12762 /* In "sizeof(X<I>)" we need to evaluate "I". */
12763 cp_evaluated ev;
12764
12765 /* First, determine the context for the type we are looking
12766 up. */
12767 context = TYPE_CONTEXT (t);
12768 if (context && TYPE_P (context))
12769 {
12770 context = tsubst_aggr_type (context, args, complain,
12771 in_decl, /*entering_scope=*/1);
12772 /* If context is a nested class inside a class template,
12773 it may still need to be instantiated (c++/33959). */
12774 context = complete_type (context);
12775 }
12776
12777 /* Then, figure out what arguments are appropriate for the
12778 type we are trying to find. For example, given:
12779
12780 template <class T> struct S;
12781 template <class T, class U> void f(T, U) { S<U> su; }
12782
12783 and supposing that we are instantiating f<int, double>,
12784 then our ARGS will be {int, double}, but, when looking up
12785 S we only want {double}. */
12786 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
12787 complain, in_decl);
12788 if (argvec == error_mark_node)
12789 r = error_mark_node;
12790 else
12791 {
12792 r = lookup_template_class (t, argvec, in_decl, context,
12793 entering_scope, complain);
12794 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
12795 }
12796
12797 return r;
12798 }
12799 else
12800 /* This is not a template type, so there's nothing to do. */
12801 return t;
12802
12803 default:
12804 return tsubst (t, args, complain, in_decl);
12805 }
12806 }
12807
12808 static GTY((cache)) tree_cache_map *defarg_inst;
12809
12810 /* Substitute into the default argument ARG (a default argument for
12811 FN), which has the indicated TYPE. */
12812
12813 tree
12814 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
12815 tsubst_flags_t complain)
12816 {
12817 int errs = errorcount + sorrycount;
12818
12819 /* This can happen in invalid code. */
12820 if (TREE_CODE (arg) == DEFERRED_PARSE)
12821 return arg;
12822
12823 tree parm = FUNCTION_FIRST_USER_PARM (fn);
12824 parm = chain_index (parmnum, parm);
12825 tree parmtype = TREE_TYPE (parm);
12826 if (DECL_BY_REFERENCE (parm))
12827 parmtype = TREE_TYPE (parmtype);
12828 if (parmtype == error_mark_node)
12829 return error_mark_node;
12830
12831 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
12832
12833 tree *slot;
12834 if (defarg_inst && (slot = defarg_inst->get (parm)))
12835 return *slot;
12836
12837 /* This default argument came from a template. Instantiate the
12838 default argument here, not in tsubst. In the case of
12839 something like:
12840
12841 template <class T>
12842 struct S {
12843 static T t();
12844 void f(T = t());
12845 };
12846
12847 we must be careful to do name lookup in the scope of S<T>,
12848 rather than in the current class. */
12849 push_to_top_level ();
12850 push_access_scope (fn);
12851 push_deferring_access_checks (dk_no_deferred);
12852 start_lambda_scope (parm);
12853
12854 /* The default argument expression may cause implicitly defined
12855 member functions to be synthesized, which will result in garbage
12856 collection. We must treat this situation as if we were within
12857 the body of function so as to avoid collecting live data on the
12858 stack. */
12859 ++function_depth;
12860 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
12861 complain, NULL_TREE,
12862 /*integral_constant_expression_p=*/false);
12863 --function_depth;
12864
12865 finish_lambda_scope ();
12866
12867 /* Make sure the default argument is reasonable. */
12868 arg = check_default_argument (type, arg, complain);
12869
12870 if (errorcount+sorrycount > errs
12871 && (complain & tf_warning_or_error))
12872 inform (input_location,
12873 " when instantiating default argument for call to %qD", fn);
12874
12875 pop_deferring_access_checks ();
12876 pop_access_scope (fn);
12877 pop_from_top_level ();
12878
12879 if (arg != error_mark_node && !cp_unevaluated_operand)
12880 {
12881 if (!defarg_inst)
12882 defarg_inst = tree_cache_map::create_ggc (37);
12883 defarg_inst->put (parm, arg);
12884 }
12885
12886 return arg;
12887 }
12888
12889 /* Substitute into all the default arguments for FN. */
12890
12891 static void
12892 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
12893 {
12894 tree arg;
12895 tree tmpl_args;
12896
12897 tmpl_args = DECL_TI_ARGS (fn);
12898
12899 /* If this function is not yet instantiated, we certainly don't need
12900 its default arguments. */
12901 if (uses_template_parms (tmpl_args))
12902 return;
12903 /* Don't do this again for clones. */
12904 if (DECL_CLONED_FUNCTION_P (fn))
12905 return;
12906
12907 int i = 0;
12908 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
12909 arg;
12910 arg = TREE_CHAIN (arg), ++i)
12911 if (TREE_PURPOSE (arg))
12912 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
12913 TREE_VALUE (arg),
12914 TREE_PURPOSE (arg),
12915 complain);
12916 }
12917
12918 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
12919 static GTY((cache)) tree_cache_map *explicit_specifier_map;
12920
12921 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
12922
12923 void
12924 store_explicit_specifier (tree v, tree t)
12925 {
12926 if (!explicit_specifier_map)
12927 explicit_specifier_map = tree_cache_map::create_ggc (37);
12928 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v) = true;
12929 explicit_specifier_map->put (v, t);
12930 }
12931
12932 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
12933
12934 static tree
12935 lookup_explicit_specifier (tree v)
12936 {
12937 return *explicit_specifier_map->get (v);
12938 }
12939
12940 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12941
12942 static tree
12943 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
12944 tree lambda_fntype)
12945 {
12946 tree gen_tmpl, argvec;
12947 hashval_t hash = 0;
12948 tree in_decl = t;
12949
12950 /* Nobody should be tsubst'ing into non-template functions. */
12951 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
12952
12953 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
12954 {
12955 /* If T is not dependent, just return it. */
12956 if (!uses_template_parms (DECL_TI_ARGS (t))
12957 && !LAMBDA_FUNCTION_P (t))
12958 return t;
12959
12960 /* Calculate the most general template of which R is a
12961 specialization. */
12962 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
12963
12964 /* We're substituting a lambda function under tsubst_lambda_expr but not
12965 directly from it; find the matching function we're already inside.
12966 But don't do this if T is a generic lambda with a single level of
12967 template parms, as in that case we're doing a normal instantiation. */
12968 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
12969 && (!generic_lambda_fn_p (t)
12970 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
12971 return enclosing_instantiation_of (t);
12972
12973 /* Calculate the complete set of arguments used to
12974 specialize R. */
12975 argvec = tsubst_template_args (DECL_TI_ARGS
12976 (DECL_TEMPLATE_RESULT
12977 (DECL_TI_TEMPLATE (t))),
12978 args, complain, in_decl);
12979 if (argvec == error_mark_node)
12980 return error_mark_node;
12981
12982 /* Check to see if we already have this specialization. */
12983 if (!lambda_fntype)
12984 {
12985 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12986 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
12987 return spec;
12988 }
12989
12990 /* We can see more levels of arguments than parameters if
12991 there was a specialization of a member template, like
12992 this:
12993
12994 template <class T> struct S { template <class U> void f(); }
12995 template <> template <class U> void S<int>::f(U);
12996
12997 Here, we'll be substituting into the specialization,
12998 because that's where we can find the code we actually
12999 want to generate, but we'll have enough arguments for
13000 the most general template.
13001
13002 We also deal with the peculiar case:
13003
13004 template <class T> struct S {
13005 template <class U> friend void f();
13006 };
13007 template <class U> void f() {}
13008 template S<int>;
13009 template void f<double>();
13010
13011 Here, the ARGS for the instantiation of will be {int,
13012 double}. But, we only need as many ARGS as there are
13013 levels of template parameters in CODE_PATTERN. We are
13014 careful not to get fooled into reducing the ARGS in
13015 situations like:
13016
13017 template <class T> struct S { template <class U> void f(U); }
13018 template <class T> template <> void S<T>::f(int) {}
13019
13020 which we can spot because the pattern will be a
13021 specialization in this case. */
13022 int args_depth = TMPL_ARGS_DEPTH (args);
13023 int parms_depth =
13024 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
13025
13026 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
13027 args = get_innermost_template_args (args, parms_depth);
13028 }
13029 else
13030 {
13031 /* This special case arises when we have something like this:
13032
13033 template <class T> struct S {
13034 friend void f<int>(int, double);
13035 };
13036
13037 Here, the DECL_TI_TEMPLATE for the friend declaration
13038 will be an IDENTIFIER_NODE. We are being called from
13039 tsubst_friend_function, and we want only to create a
13040 new decl (R) with appropriate types so that we can call
13041 determine_specialization. */
13042 gen_tmpl = NULL_TREE;
13043 argvec = NULL_TREE;
13044 }
13045
13046 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
13047 : NULL_TREE);
13048 tree ctx = closure ? closure : DECL_CONTEXT (t);
13049 bool member = ctx && TYPE_P (ctx);
13050
13051 if (member && !closure)
13052 ctx = tsubst_aggr_type (ctx, args,
13053 complain, t, /*entering_scope=*/1);
13054
13055 tree type = (lambda_fntype ? lambda_fntype
13056 : tsubst (TREE_TYPE (t), args,
13057 complain | tf_fndecl_type, in_decl));
13058 if (type == error_mark_node)
13059 return error_mark_node;
13060
13061 /* If we hit excessive deduction depth, the type is bogus even if
13062 it isn't error_mark_node, so don't build a decl. */
13063 if (excessive_deduction_depth)
13064 return error_mark_node;
13065
13066 /* We do NOT check for matching decls pushed separately at this
13067 point, as they may not represent instantiations of this
13068 template, and in any case are considered separate under the
13069 discrete model. */
13070 tree r = copy_decl (t);
13071 DECL_USE_TEMPLATE (r) = 0;
13072 TREE_TYPE (r) = type;
13073 /* Clear out the mangled name and RTL for the instantiation. */
13074 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13075 SET_DECL_RTL (r, NULL);
13076 /* Leave DECL_INITIAL set on deleted instantiations. */
13077 if (!DECL_DELETED_FN (r))
13078 DECL_INITIAL (r) = NULL_TREE;
13079 DECL_CONTEXT (r) = ctx;
13080
13081 /* Handle explicit(dependent-expr). */
13082 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
13083 {
13084 tree spec = lookup_explicit_specifier (t);
13085 spec = tsubst_copy_and_build (spec, args, complain, in_decl,
13086 /*function_p=*/false,
13087 /*i_c_e_p=*/true);
13088 spec = build_explicit_specifier (spec, complain);
13089 DECL_NONCONVERTING_P (r) = (spec == boolean_true_node);
13090 }
13091
13092 /* OpenMP UDRs have the only argument a reference to the declared
13093 type. We want to diagnose if the declared type is a reference,
13094 which is invalid, but as references to references are usually
13095 quietly merged, diagnose it here. */
13096 if (DECL_OMP_DECLARE_REDUCTION_P (t))
13097 {
13098 tree argtype
13099 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
13100 argtype = tsubst (argtype, args, complain, in_decl);
13101 if (TYPE_REF_P (argtype))
13102 error_at (DECL_SOURCE_LOCATION (t),
13103 "reference type %qT in "
13104 "%<#pragma omp declare reduction%>", argtype);
13105 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
13106 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
13107 argtype);
13108 }
13109
13110 if (member && DECL_CONV_FN_P (r))
13111 /* Type-conversion operator. Reconstruct the name, in
13112 case it's the name of one of the template's parameters. */
13113 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
13114
13115 tree parms = DECL_ARGUMENTS (t);
13116 if (closure)
13117 parms = DECL_CHAIN (parms);
13118 parms = tsubst (parms, args, complain, t);
13119 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
13120 DECL_CONTEXT (parm) = r;
13121 if (closure)
13122 {
13123 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
13124 DECL_CHAIN (tparm) = parms;
13125 parms = tparm;
13126 }
13127 DECL_ARGUMENTS (r) = parms;
13128 DECL_RESULT (r) = NULL_TREE;
13129
13130 TREE_STATIC (r) = 0;
13131 TREE_PUBLIC (r) = TREE_PUBLIC (t);
13132 DECL_EXTERNAL (r) = 1;
13133 /* If this is an instantiation of a function with internal
13134 linkage, we already know what object file linkage will be
13135 assigned to the instantiation. */
13136 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
13137 DECL_DEFER_OUTPUT (r) = 0;
13138 DECL_CHAIN (r) = NULL_TREE;
13139 DECL_PENDING_INLINE_INFO (r) = 0;
13140 DECL_PENDING_INLINE_P (r) = 0;
13141 DECL_SAVED_TREE (r) = NULL_TREE;
13142 DECL_STRUCT_FUNCTION (r) = NULL;
13143 TREE_USED (r) = 0;
13144 /* We'll re-clone as appropriate in instantiate_template. */
13145 DECL_CLONED_FUNCTION (r) = NULL_TREE;
13146
13147 /* If we aren't complaining now, return on error before we register
13148 the specialization so that we'll complain eventually. */
13149 if ((complain & tf_error) == 0
13150 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13151 && !grok_op_properties (r, /*complain=*/false))
13152 return error_mark_node;
13153
13154 /* When instantiating a constrained member, substitute
13155 into the constraints to create a new constraint. */
13156 if (tree ci = get_constraints (t))
13157 if (member)
13158 {
13159 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
13160 set_constraints (r, ci);
13161 }
13162
13163 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
13164 SET_DECL_FRIEND_CONTEXT (r,
13165 tsubst (DECL_FRIEND_CONTEXT (t),
13166 args, complain, in_decl));
13167
13168 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
13169 this in the special friend case mentioned above where
13170 GEN_TMPL is NULL. */
13171 if (gen_tmpl && !closure)
13172 {
13173 DECL_TEMPLATE_INFO (r)
13174 = build_template_info (gen_tmpl, argvec);
13175 SET_DECL_IMPLICIT_INSTANTIATION (r);
13176
13177 tree new_r
13178 = register_specialization (r, gen_tmpl, argvec, false, hash);
13179 if (new_r != r)
13180 /* We instantiated this while substituting into
13181 the type earlier (template/friend54.C). */
13182 return new_r;
13183
13184 /* We're not supposed to instantiate default arguments
13185 until they are called, for a template. But, for a
13186 declaration like:
13187
13188 template <class T> void f ()
13189 { extern void g(int i = T()); }
13190
13191 we should do the substitution when the template is
13192 instantiated. We handle the member function case in
13193 instantiate_class_template since the default arguments
13194 might refer to other members of the class. */
13195 if (!member
13196 && !PRIMARY_TEMPLATE_P (gen_tmpl)
13197 && !uses_template_parms (argvec))
13198 tsubst_default_arguments (r, complain);
13199 }
13200 else
13201 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13202
13203 /* Copy the list of befriending classes. */
13204 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
13205 *friends;
13206 friends = &TREE_CHAIN (*friends))
13207 {
13208 *friends = copy_node (*friends);
13209 TREE_VALUE (*friends)
13210 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
13211 }
13212
13213 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
13214 {
13215 maybe_retrofit_in_chrg (r);
13216 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
13217 return error_mark_node;
13218 /* If this is an instantiation of a member template, clone it.
13219 If it isn't, that'll be handled by
13220 clone_constructors_and_destructors. */
13221 if (PRIMARY_TEMPLATE_P (gen_tmpl))
13222 clone_function_decl (r, /*update_methods=*/false);
13223 }
13224 else if ((complain & tf_error) != 0
13225 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13226 && !grok_op_properties (r, /*complain=*/true))
13227 return error_mark_node;
13228
13229 /* Possibly limit visibility based on template args. */
13230 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13231 if (DECL_VISIBILITY_SPECIFIED (t))
13232 {
13233 DECL_VISIBILITY_SPECIFIED (r) = 0;
13234 DECL_ATTRIBUTES (r)
13235 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13236 }
13237 determine_visibility (r);
13238 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
13239 && !processing_template_decl)
13240 defaulted_late_check (r);
13241
13242 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13243 args, complain, in_decl);
13244 return r;
13245 }
13246
13247 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
13248
13249 static tree
13250 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
13251 tree lambda_fntype)
13252 {
13253 /* We can get here when processing a member function template,
13254 member class template, or template template parameter. */
13255 tree decl = DECL_TEMPLATE_RESULT (t);
13256 tree in_decl = t;
13257 tree spec;
13258 tree tmpl_args;
13259 tree full_args;
13260 tree r;
13261 hashval_t hash = 0;
13262
13263 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
13264 {
13265 /* Template template parameter is treated here. */
13266 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13267 if (new_type == error_mark_node)
13268 r = error_mark_node;
13269 /* If we get a real template back, return it. This can happen in
13270 the context of most_specialized_partial_spec. */
13271 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
13272 r = new_type;
13273 else
13274 /* The new TEMPLATE_DECL was built in
13275 reduce_template_parm_level. */
13276 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
13277 return r;
13278 }
13279
13280 if (!lambda_fntype)
13281 {
13282 /* We might already have an instance of this template.
13283 The ARGS are for the surrounding class type, so the
13284 full args contain the tsubst'd args for the context,
13285 plus the innermost args from the template decl. */
13286 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
13287 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
13288 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
13289 /* Because this is a template, the arguments will still be
13290 dependent, even after substitution. If
13291 PROCESSING_TEMPLATE_DECL is not set, the dependency
13292 predicates will short-circuit. */
13293 ++processing_template_decl;
13294 full_args = tsubst_template_args (tmpl_args, args,
13295 complain, in_decl);
13296 --processing_template_decl;
13297 if (full_args == error_mark_node)
13298 return error_mark_node;
13299
13300 /* If this is a default template template argument,
13301 tsubst might not have changed anything. */
13302 if (full_args == tmpl_args)
13303 return t;
13304
13305 hash = hash_tmpl_and_args (t, full_args);
13306 spec = retrieve_specialization (t, full_args, hash);
13307 if (spec != NULL_TREE)
13308 {
13309 if (TYPE_P (spec))
13310 /* Type partial instantiations are stored as the type by
13311 lookup_template_class_1, not here as the template. */
13312 spec = CLASSTYPE_TI_TEMPLATE (spec);
13313 return spec;
13314 }
13315 }
13316
13317 /* Make a new template decl. It will be similar to the
13318 original, but will record the current template arguments.
13319 We also create a new function declaration, which is just
13320 like the old one, but points to this new template, rather
13321 than the old one. */
13322 r = copy_decl (t);
13323 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
13324 DECL_CHAIN (r) = NULL_TREE;
13325
13326 // Build new template info linking to the original template decl.
13327 if (!lambda_fntype)
13328 {
13329 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
13330 SET_DECL_IMPLICIT_INSTANTIATION (r);
13331 }
13332 else
13333 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13334
13335 /* The template parameters for this new template are all the
13336 template parameters for the old template, except the
13337 outermost level of parameters. */
13338 DECL_TEMPLATE_PARMS (r)
13339 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
13340 complain);
13341
13342 if (TREE_CODE (decl) == TYPE_DECL
13343 && !TYPE_DECL_ALIAS_P (decl))
13344 {
13345 tree new_type;
13346 ++processing_template_decl;
13347 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13348 --processing_template_decl;
13349 if (new_type == error_mark_node)
13350 return error_mark_node;
13351
13352 TREE_TYPE (r) = new_type;
13353 /* For a partial specialization, we need to keep pointing to
13354 the primary template. */
13355 if (!DECL_TEMPLATE_SPECIALIZATION (t))
13356 CLASSTYPE_TI_TEMPLATE (new_type) = r;
13357 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
13358 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
13359 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
13360 }
13361 else
13362 {
13363 tree new_decl;
13364 ++processing_template_decl;
13365 if (TREE_CODE (decl) == FUNCTION_DECL)
13366 new_decl = tsubst_function_decl (decl, args, complain, lambda_fntype);
13367 else
13368 new_decl = tsubst (decl, args, complain, in_decl);
13369 --processing_template_decl;
13370 if (new_decl == error_mark_node)
13371 return error_mark_node;
13372
13373 DECL_TEMPLATE_RESULT (r) = new_decl;
13374 TREE_TYPE (r) = TREE_TYPE (new_decl);
13375 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
13376 if (lambda_fntype)
13377 {
13378 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
13379 DECL_TEMPLATE_INFO (new_decl) = build_template_info (r, args);
13380 }
13381 else
13382 {
13383 DECL_TI_TEMPLATE (new_decl) = r;
13384 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
13385 }
13386 }
13387
13388 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
13389 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
13390
13391 if (PRIMARY_TEMPLATE_P (t))
13392 DECL_PRIMARY_TEMPLATE (r) = r;
13393
13394 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
13395 && !lambda_fntype)
13396 /* Record this non-type partial instantiation. */
13397 register_specialization (r, t,
13398 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
13399 false, hash);
13400
13401 return r;
13402 }
13403
13404 /* True if FN is the op() for a lambda in an uninstantiated template. */
13405
13406 bool
13407 lambda_fn_in_template_p (tree fn)
13408 {
13409 if (!fn || !LAMBDA_FUNCTION_P (fn))
13410 return false;
13411 tree closure = DECL_CONTEXT (fn);
13412 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
13413 }
13414
13415 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
13416 which the above is true. */
13417
13418 bool
13419 instantiated_lambda_fn_p (tree fn)
13420 {
13421 if (!fn || !LAMBDA_FUNCTION_P (fn))
13422 return false;
13423 tree closure = DECL_CONTEXT (fn);
13424 tree lam = CLASSTYPE_LAMBDA_EXPR (closure);
13425 return LAMBDA_EXPR_INSTANTIATED (lam);
13426 }
13427
13428 /* We're instantiating a variable from template function TCTX. Return the
13429 corresponding current enclosing scope. This gets complicated because lambda
13430 functions in templates are regenerated rather than instantiated, but generic
13431 lambda functions are subsequently instantiated. */
13432
13433 static tree
13434 enclosing_instantiation_of (tree otctx)
13435 {
13436 tree tctx = otctx;
13437 tree fn = current_function_decl;
13438 int lambda_count = 0;
13439
13440 for (; tctx && (lambda_fn_in_template_p (tctx)
13441 || instantiated_lambda_fn_p (tctx));
13442 tctx = decl_function_context (tctx))
13443 ++lambda_count;
13444 for (; fn; fn = decl_function_context (fn))
13445 {
13446 tree ofn = fn;
13447 int flambda_count = 0;
13448 for (; fn && instantiated_lambda_fn_p (fn);
13449 fn = decl_function_context (fn))
13450 ++flambda_count;
13451 if ((fn && DECL_TEMPLATE_INFO (fn))
13452 ? most_general_template (fn) != most_general_template (tctx)
13453 : fn != tctx)
13454 continue;
13455 if (flambda_count != lambda_count)
13456 {
13457 gcc_assert (flambda_count > lambda_count);
13458 for (; flambda_count > lambda_count; --flambda_count)
13459 ofn = decl_function_context (ofn);
13460 }
13461 gcc_assert (DECL_NAME (ofn) == DECL_NAME (otctx)
13462 || DECL_CONV_FN_P (ofn));
13463 return ofn;
13464 }
13465 gcc_unreachable ();
13466 }
13467
13468 /* Substitute the ARGS into the T, which is a _DECL. Return the
13469 result of the substitution. Issue error and warning messages under
13470 control of COMPLAIN. */
13471
13472 static tree
13473 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
13474 {
13475 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13476 location_t saved_loc;
13477 tree r = NULL_TREE;
13478 tree in_decl = t;
13479 hashval_t hash = 0;
13480
13481 /* Set the filename and linenumber to improve error-reporting. */
13482 saved_loc = input_location;
13483 input_location = DECL_SOURCE_LOCATION (t);
13484
13485 switch (TREE_CODE (t))
13486 {
13487 case TEMPLATE_DECL:
13488 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
13489 break;
13490
13491 case FUNCTION_DECL:
13492 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
13493 break;
13494
13495 case PARM_DECL:
13496 {
13497 tree type = NULL_TREE;
13498 int i, len = 1;
13499 tree expanded_types = NULL_TREE;
13500 tree prev_r = NULL_TREE;
13501 tree first_r = NULL_TREE;
13502
13503 if (DECL_PACK_P (t))
13504 {
13505 /* If there is a local specialization that isn't a
13506 parameter pack, it means that we're doing a "simple"
13507 substitution from inside tsubst_pack_expansion. Just
13508 return the local specialization (which will be a single
13509 parm). */
13510 tree spec = retrieve_local_specialization (t);
13511 if (spec
13512 && TREE_CODE (spec) == PARM_DECL
13513 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
13514 RETURN (spec);
13515
13516 /* Expand the TYPE_PACK_EXPANSION that provides the types for
13517 the parameters in this function parameter pack. */
13518 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
13519 complain, in_decl);
13520 if (TREE_CODE (expanded_types) == TREE_VEC)
13521 {
13522 len = TREE_VEC_LENGTH (expanded_types);
13523
13524 /* Zero-length parameter packs are boring. Just substitute
13525 into the chain. */
13526 if (len == 0)
13527 RETURN (tsubst (TREE_CHAIN (t), args, complain,
13528 TREE_CHAIN (t)));
13529 }
13530 else
13531 {
13532 /* All we did was update the type. Make a note of that. */
13533 type = expanded_types;
13534 expanded_types = NULL_TREE;
13535 }
13536 }
13537
13538 /* Loop through all of the parameters we'll build. When T is
13539 a function parameter pack, LEN is the number of expanded
13540 types in EXPANDED_TYPES; otherwise, LEN is 1. */
13541 r = NULL_TREE;
13542 for (i = 0; i < len; ++i)
13543 {
13544 prev_r = r;
13545 r = copy_node (t);
13546 if (DECL_TEMPLATE_PARM_P (t))
13547 SET_DECL_TEMPLATE_PARM_P (r);
13548
13549 if (expanded_types)
13550 /* We're on the Ith parameter of the function parameter
13551 pack. */
13552 {
13553 /* Get the Ith type. */
13554 type = TREE_VEC_ELT (expanded_types, i);
13555
13556 /* Rename the parameter to include the index. */
13557 DECL_NAME (r)
13558 = make_ith_pack_parameter_name (DECL_NAME (r), i);
13559 }
13560 else if (!type)
13561 /* We're dealing with a normal parameter. */
13562 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13563
13564 type = type_decays_to (type);
13565 TREE_TYPE (r) = type;
13566 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13567
13568 if (DECL_INITIAL (r))
13569 {
13570 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
13571 DECL_INITIAL (r) = TREE_TYPE (r);
13572 else
13573 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
13574 complain, in_decl);
13575 }
13576
13577 DECL_CONTEXT (r) = NULL_TREE;
13578
13579 if (!DECL_TEMPLATE_PARM_P (r))
13580 DECL_ARG_TYPE (r) = type_passed_as (type);
13581
13582 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13583 args, complain, in_decl);
13584
13585 /* Keep track of the first new parameter we
13586 generate. That's what will be returned to the
13587 caller. */
13588 if (!first_r)
13589 first_r = r;
13590
13591 /* Build a proper chain of parameters when substituting
13592 into a function parameter pack. */
13593 if (prev_r)
13594 DECL_CHAIN (prev_r) = r;
13595 }
13596
13597 /* If cp_unevaluated_operand is set, we're just looking for a
13598 single dummy parameter, so don't keep going. */
13599 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
13600 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
13601 complain, DECL_CHAIN (t));
13602
13603 /* FIRST_R contains the start of the chain we've built. */
13604 r = first_r;
13605 }
13606 break;
13607
13608 case FIELD_DECL:
13609 {
13610 tree type = NULL_TREE;
13611 tree vec = NULL_TREE;
13612 tree expanded_types = NULL_TREE;
13613 int len = 1;
13614
13615 if (PACK_EXPANSION_P (TREE_TYPE (t)))
13616 {
13617 /* This field is a lambda capture pack. Return a TREE_VEC of
13618 the expanded fields to instantiate_class_template_1. */
13619 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
13620 complain, in_decl);
13621 if (TREE_CODE (expanded_types) == TREE_VEC)
13622 {
13623 len = TREE_VEC_LENGTH (expanded_types);
13624 vec = make_tree_vec (len);
13625 }
13626 else
13627 {
13628 /* All we did was update the type. Make a note of that. */
13629 type = expanded_types;
13630 expanded_types = NULL_TREE;
13631 }
13632 }
13633
13634 for (int i = 0; i < len; ++i)
13635 {
13636 r = copy_decl (t);
13637 if (expanded_types)
13638 {
13639 type = TREE_VEC_ELT (expanded_types, i);
13640 DECL_NAME (r)
13641 = make_ith_pack_parameter_name (DECL_NAME (r), i);
13642 }
13643 else if (!type)
13644 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13645
13646 if (type == error_mark_node)
13647 RETURN (error_mark_node);
13648 TREE_TYPE (r) = type;
13649 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13650
13651 if (DECL_C_BIT_FIELD (r))
13652 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
13653 number of bits. */
13654 DECL_BIT_FIELD_REPRESENTATIVE (r)
13655 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
13656 complain, in_decl,
13657 /*integral_constant_expression_p=*/true);
13658 if (DECL_INITIAL (t))
13659 {
13660 /* Set up DECL_TEMPLATE_INFO so that we can get at the
13661 NSDMI in perform_member_init. Still set DECL_INITIAL
13662 so that we know there is one. */
13663 DECL_INITIAL (r) = void_node;
13664 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
13665 retrofit_lang_decl (r);
13666 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
13667 }
13668 /* We don't have to set DECL_CONTEXT here; it is set by
13669 finish_member_declaration. */
13670 DECL_CHAIN (r) = NULL_TREE;
13671
13672 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13673 args, complain, in_decl);
13674
13675 if (vec)
13676 TREE_VEC_ELT (vec, i) = r;
13677 }
13678
13679 if (vec)
13680 r = vec;
13681 }
13682 break;
13683
13684 case USING_DECL:
13685 /* We reach here only for member using decls. We also need to check
13686 uses_template_parms because DECL_DEPENDENT_P is not set for a
13687 using-declaration that designates a member of the current
13688 instantiation (c++/53549). */
13689 if (DECL_DEPENDENT_P (t)
13690 || uses_template_parms (USING_DECL_SCOPE (t)))
13691 {
13692 tree scope = USING_DECL_SCOPE (t);
13693 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
13694 if (PACK_EXPANSION_P (scope))
13695 {
13696 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
13697 int len = TREE_VEC_LENGTH (vec);
13698 r = make_tree_vec (len);
13699 for (int i = 0; i < len; ++i)
13700 {
13701 tree escope = TREE_VEC_ELT (vec, i);
13702 tree elt = do_class_using_decl (escope, name);
13703 if (!elt)
13704 {
13705 r = error_mark_node;
13706 break;
13707 }
13708 else
13709 {
13710 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
13711 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
13712 }
13713 TREE_VEC_ELT (r, i) = elt;
13714 }
13715 }
13716 else
13717 {
13718 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
13719 complain, in_decl);
13720 r = do_class_using_decl (inst_scope, name);
13721 if (!r)
13722 r = error_mark_node;
13723 else
13724 {
13725 TREE_PROTECTED (r) = TREE_PROTECTED (t);
13726 TREE_PRIVATE (r) = TREE_PRIVATE (t);
13727 }
13728 }
13729 }
13730 else
13731 {
13732 r = copy_node (t);
13733 DECL_CHAIN (r) = NULL_TREE;
13734 }
13735 break;
13736
13737 case TYPE_DECL:
13738 case VAR_DECL:
13739 {
13740 tree argvec = NULL_TREE;
13741 tree gen_tmpl = NULL_TREE;
13742 tree spec;
13743 tree tmpl = NULL_TREE;
13744 tree ctx;
13745 tree type = NULL_TREE;
13746 bool local_p;
13747
13748 if (TREE_TYPE (t) == error_mark_node)
13749 RETURN (error_mark_node);
13750
13751 if (TREE_CODE (t) == TYPE_DECL
13752 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
13753 {
13754 /* If this is the canonical decl, we don't have to
13755 mess with instantiations, and often we can't (for
13756 typename, template type parms and such). Note that
13757 TYPE_NAME is not correct for the above test if
13758 we've copied the type for a typedef. */
13759 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13760 if (type == error_mark_node)
13761 RETURN (error_mark_node);
13762 r = TYPE_NAME (type);
13763 break;
13764 }
13765
13766 /* Check to see if we already have the specialization we
13767 need. */
13768 spec = NULL_TREE;
13769 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
13770 {
13771 /* T is a static data member or namespace-scope entity.
13772 We have to substitute into namespace-scope variables
13773 (not just variable templates) because of cases like:
13774
13775 template <class T> void f() { extern T t; }
13776
13777 where the entity referenced is not known until
13778 instantiation time. */
13779 local_p = false;
13780 ctx = DECL_CONTEXT (t);
13781 if (DECL_CLASS_SCOPE_P (t))
13782 {
13783 ctx = tsubst_aggr_type (ctx, args,
13784 complain,
13785 in_decl, /*entering_scope=*/1);
13786 /* If CTX is unchanged, then T is in fact the
13787 specialization we want. That situation occurs when
13788 referencing a static data member within in its own
13789 class. We can use pointer equality, rather than
13790 same_type_p, because DECL_CONTEXT is always
13791 canonical... */
13792 if (ctx == DECL_CONTEXT (t)
13793 /* ... unless T is a member template; in which
13794 case our caller can be willing to create a
13795 specialization of that template represented
13796 by T. */
13797 && !(DECL_TI_TEMPLATE (t)
13798 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
13799 spec = t;
13800 }
13801
13802 if (!spec)
13803 {
13804 tmpl = DECL_TI_TEMPLATE (t);
13805 gen_tmpl = most_general_template (tmpl);
13806 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
13807 if (argvec != error_mark_node)
13808 argvec = (coerce_innermost_template_parms
13809 (DECL_TEMPLATE_PARMS (gen_tmpl),
13810 argvec, t, complain,
13811 /*all*/true, /*defarg*/true));
13812 if (argvec == error_mark_node)
13813 RETURN (error_mark_node);
13814 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13815 spec = retrieve_specialization (gen_tmpl, argvec, hash);
13816 }
13817 }
13818 else
13819 {
13820 /* A local variable. */
13821 local_p = true;
13822 /* Subsequent calls to pushdecl will fill this in. */
13823 ctx = NULL_TREE;
13824 /* Unless this is a reference to a static variable from an
13825 enclosing function, in which case we need to fill it in now. */
13826 if (TREE_STATIC (t))
13827 {
13828 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
13829 if (fn != current_function_decl)
13830 ctx = fn;
13831 }
13832 spec = retrieve_local_specialization (t);
13833 }
13834 /* If we already have the specialization we need, there is
13835 nothing more to do. */
13836 if (spec)
13837 {
13838 r = spec;
13839 break;
13840 }
13841
13842 /* Create a new node for the specialization we need. */
13843 if (type == NULL_TREE)
13844 {
13845 if (is_typedef_decl (t))
13846 type = DECL_ORIGINAL_TYPE (t);
13847 else
13848 type = TREE_TYPE (t);
13849 if (VAR_P (t)
13850 && VAR_HAD_UNKNOWN_BOUND (t)
13851 && type != error_mark_node)
13852 type = strip_array_domain (type);
13853 tree sub_args = args;
13854 if (tree auto_node = type_uses_auto (type))
13855 {
13856 /* Mask off any template args past the variable's context so we
13857 don't replace the auto with an unrelated argument. */
13858 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
13859 int extra = TMPL_ARGS_DEPTH (args) - nouter;
13860 if (extra > 0)
13861 /* This should never happen with the new lambda instantiation
13862 model, but keep the handling just in case. */
13863 gcc_assert (!CHECKING_P),
13864 sub_args = strip_innermost_template_args (args, extra);
13865 }
13866 type = tsubst (type, sub_args, complain, in_decl);
13867 /* Substituting the type might have recursively instantiated this
13868 same alias (c++/86171). */
13869 if (gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl)
13870 && (spec = retrieve_specialization (gen_tmpl, argvec, hash)))
13871 {
13872 r = spec;
13873 break;
13874 }
13875 }
13876 r = copy_decl (t);
13877 if (VAR_P (r))
13878 {
13879 DECL_INITIALIZED_P (r) = 0;
13880 DECL_TEMPLATE_INSTANTIATED (r) = 0;
13881 if (type == error_mark_node)
13882 RETURN (error_mark_node);
13883 if (TREE_CODE (type) == FUNCTION_TYPE)
13884 {
13885 /* It may seem that this case cannot occur, since:
13886
13887 typedef void f();
13888 void g() { f x; }
13889
13890 declares a function, not a variable. However:
13891
13892 typedef void f();
13893 template <typename T> void g() { T t; }
13894 template void g<f>();
13895
13896 is an attempt to declare a variable with function
13897 type. */
13898 error ("variable %qD has function type",
13899 /* R is not yet sufficiently initialized, so we
13900 just use its name. */
13901 DECL_NAME (r));
13902 RETURN (error_mark_node);
13903 }
13904 type = complete_type (type);
13905 /* Wait until cp_finish_decl to set this again, to handle
13906 circular dependency (template/instantiate6.C). */
13907 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
13908 type = check_var_type (DECL_NAME (r), type,
13909 DECL_SOURCE_LOCATION (r));
13910 if (DECL_HAS_VALUE_EXPR_P (t))
13911 {
13912 tree ve = DECL_VALUE_EXPR (t);
13913 ve = tsubst_expr (ve, args, complain, in_decl,
13914 /*constant_expression_p=*/false);
13915 if (REFERENCE_REF_P (ve))
13916 {
13917 gcc_assert (TYPE_REF_P (type));
13918 ve = TREE_OPERAND (ve, 0);
13919 }
13920 SET_DECL_VALUE_EXPR (r, ve);
13921 }
13922 if (CP_DECL_THREAD_LOCAL_P (r)
13923 && !processing_template_decl)
13924 set_decl_tls_model (r, decl_default_tls_model (r));
13925 }
13926 else if (DECL_SELF_REFERENCE_P (t))
13927 SET_DECL_SELF_REFERENCE_P (r);
13928 TREE_TYPE (r) = type;
13929 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13930 DECL_CONTEXT (r) = ctx;
13931 /* Clear out the mangled name and RTL for the instantiation. */
13932 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13933 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
13934 SET_DECL_RTL (r, NULL);
13935 /* The initializer must not be expanded until it is required;
13936 see [temp.inst]. */
13937 DECL_INITIAL (r) = NULL_TREE;
13938 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
13939 if (VAR_P (r))
13940 {
13941 if (DECL_LANG_SPECIFIC (r))
13942 SET_DECL_DEPENDENT_INIT_P (r, false);
13943
13944 SET_DECL_MODE (r, VOIDmode);
13945
13946 /* Possibly limit visibility based on template args. */
13947 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13948 if (DECL_VISIBILITY_SPECIFIED (t))
13949 {
13950 DECL_VISIBILITY_SPECIFIED (r) = 0;
13951 DECL_ATTRIBUTES (r)
13952 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13953 }
13954 determine_visibility (r);
13955 }
13956
13957 if (!local_p)
13958 {
13959 /* A static data member declaration is always marked
13960 external when it is declared in-class, even if an
13961 initializer is present. We mimic the non-template
13962 processing here. */
13963 DECL_EXTERNAL (r) = 1;
13964 if (DECL_NAMESPACE_SCOPE_P (t))
13965 DECL_NOT_REALLY_EXTERN (r) = 1;
13966
13967 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
13968 SET_DECL_IMPLICIT_INSTANTIATION (r);
13969 /* Remember whether we require constant initialization of
13970 a non-constant template variable. */
13971 TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (r))
13972 = TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (t));
13973 if (!error_operand_p (r) || (complain & tf_error))
13974 register_specialization (r, gen_tmpl, argvec, false, hash);
13975 }
13976 else
13977 {
13978 if (DECL_LANG_SPECIFIC (r))
13979 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13980 if (!cp_unevaluated_operand)
13981 register_local_specialization (r, t);
13982 }
13983
13984 DECL_CHAIN (r) = NULL_TREE;
13985
13986 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
13987 /*flags=*/0,
13988 args, complain, in_decl);
13989
13990 /* Preserve a typedef that names a type. */
13991 if (is_typedef_decl (r) && type != error_mark_node)
13992 {
13993 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
13994 set_underlying_type (r);
13995 if (TYPE_DECL_ALIAS_P (r))
13996 /* An alias template specialization can be dependent
13997 even if its underlying type is not. */
13998 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
13999 }
14000
14001 layout_decl (r, 0);
14002 }
14003 break;
14004
14005 default:
14006 gcc_unreachable ();
14007 }
14008 #undef RETURN
14009
14010 out:
14011 /* Restore the file and line information. */
14012 input_location = saved_loc;
14013
14014 return r;
14015 }
14016
14017 /* Substitute into the ARG_TYPES of a function type.
14018 If END is a TREE_CHAIN, leave it and any following types
14019 un-substituted. */
14020
14021 static tree
14022 tsubst_arg_types (tree arg_types,
14023 tree args,
14024 tree end,
14025 tsubst_flags_t complain,
14026 tree in_decl)
14027 {
14028 tree remaining_arg_types;
14029 tree type = NULL_TREE;
14030 int i = 1;
14031 tree expanded_args = NULL_TREE;
14032 tree default_arg;
14033
14034 if (!arg_types || arg_types == void_list_node || arg_types == end)
14035 return arg_types;
14036
14037 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
14038 args, end, complain, in_decl);
14039 if (remaining_arg_types == error_mark_node)
14040 return error_mark_node;
14041
14042 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
14043 {
14044 /* For a pack expansion, perform substitution on the
14045 entire expression. Later on, we'll handle the arguments
14046 one-by-one. */
14047 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
14048 args, complain, in_decl);
14049
14050 if (TREE_CODE (expanded_args) == TREE_VEC)
14051 /* So that we'll spin through the parameters, one by one. */
14052 i = TREE_VEC_LENGTH (expanded_args);
14053 else
14054 {
14055 /* We only partially substituted into the parameter
14056 pack. Our type is TYPE_PACK_EXPANSION. */
14057 type = expanded_args;
14058 expanded_args = NULL_TREE;
14059 }
14060 }
14061
14062 while (i > 0) {
14063 --i;
14064
14065 if (expanded_args)
14066 type = TREE_VEC_ELT (expanded_args, i);
14067 else if (!type)
14068 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
14069
14070 if (type == error_mark_node)
14071 return error_mark_node;
14072 if (VOID_TYPE_P (type))
14073 {
14074 if (complain & tf_error)
14075 {
14076 error ("invalid parameter type %qT", type);
14077 if (in_decl)
14078 error ("in declaration %q+D", in_decl);
14079 }
14080 return error_mark_node;
14081 }
14082 /* DR 657. */
14083 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
14084 return error_mark_node;
14085
14086 /* Do array-to-pointer, function-to-pointer conversion, and ignore
14087 top-level qualifiers as required. */
14088 type = cv_unqualified (type_decays_to (type));
14089
14090 /* We do not substitute into default arguments here. The standard
14091 mandates that they be instantiated only when needed, which is
14092 done in build_over_call. */
14093 default_arg = TREE_PURPOSE (arg_types);
14094
14095 /* Except that we do substitute default arguments under tsubst_lambda_expr,
14096 since the new op() won't have any associated template arguments for us
14097 to refer to later. */
14098 if (lambda_fn_in_template_p (in_decl))
14099 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
14100 false/*fn*/, false/*constexpr*/);
14101
14102 if (default_arg && TREE_CODE (default_arg) == DEFERRED_PARSE)
14103 {
14104 /* We've instantiated a template before its default arguments
14105 have been parsed. This can happen for a nested template
14106 class, and is not an error unless we require the default
14107 argument in a call of this function. */
14108 remaining_arg_types =
14109 tree_cons (default_arg, type, remaining_arg_types);
14110 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg),
14111 remaining_arg_types);
14112 }
14113 else
14114 remaining_arg_types =
14115 hash_tree_cons (default_arg, type, remaining_arg_types);
14116 }
14117
14118 return remaining_arg_types;
14119 }
14120
14121 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
14122 *not* handle the exception-specification for FNTYPE, because the
14123 initial substitution of explicitly provided template parameters
14124 during argument deduction forbids substitution into the
14125 exception-specification:
14126
14127 [temp.deduct]
14128
14129 All references in the function type of the function template to the
14130 corresponding template parameters are replaced by the specified tem-
14131 plate argument values. If a substitution in a template parameter or
14132 in the function type of the function template results in an invalid
14133 type, type deduction fails. [Note: The equivalent substitution in
14134 exception specifications is done only when the function is instanti-
14135 ated, at which point a program is ill-formed if the substitution
14136 results in an invalid type.] */
14137
14138 static tree
14139 tsubst_function_type (tree t,
14140 tree args,
14141 tsubst_flags_t complain,
14142 tree in_decl)
14143 {
14144 tree return_type;
14145 tree arg_types = NULL_TREE;
14146 tree fntype;
14147
14148 /* The TYPE_CONTEXT is not used for function/method types. */
14149 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
14150
14151 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
14152 failure. */
14153 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
14154
14155 if (late_return_type_p)
14156 {
14157 /* Substitute the argument types. */
14158 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
14159 complain, in_decl);
14160 if (arg_types == error_mark_node)
14161 return error_mark_node;
14162
14163 tree save_ccp = current_class_ptr;
14164 tree save_ccr = current_class_ref;
14165 tree this_type = (TREE_CODE (t) == METHOD_TYPE
14166 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
14167 bool do_inject = this_type && CLASS_TYPE_P (this_type);
14168 if (do_inject)
14169 {
14170 /* DR 1207: 'this' is in scope in the trailing return type. */
14171 inject_this_parameter (this_type, cp_type_quals (this_type));
14172 }
14173
14174 /* Substitute the return type. */
14175 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14176
14177 if (do_inject)
14178 {
14179 current_class_ptr = save_ccp;
14180 current_class_ref = save_ccr;
14181 }
14182 }
14183 else
14184 /* Substitute the return type. */
14185 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14186
14187 if (return_type == error_mark_node)
14188 return error_mark_node;
14189 /* DR 486 clarifies that creation of a function type with an
14190 invalid return type is a deduction failure. */
14191 if (TREE_CODE (return_type) == ARRAY_TYPE
14192 || TREE_CODE (return_type) == FUNCTION_TYPE)
14193 {
14194 if (complain & tf_error)
14195 {
14196 if (TREE_CODE (return_type) == ARRAY_TYPE)
14197 error ("function returning an array");
14198 else
14199 error ("function returning a function");
14200 }
14201 return error_mark_node;
14202 }
14203 /* And DR 657. */
14204 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
14205 return error_mark_node;
14206
14207 if (!late_return_type_p)
14208 {
14209 /* Substitute the argument types. */
14210 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
14211 complain, in_decl);
14212 if (arg_types == error_mark_node)
14213 return error_mark_node;
14214 }
14215
14216 /* Construct a new type node and return it. */
14217 if (TREE_CODE (t) == FUNCTION_TYPE)
14218 {
14219 fntype = build_function_type (return_type, arg_types);
14220 fntype = apply_memfn_quals (fntype, type_memfn_quals (t));
14221 }
14222 else
14223 {
14224 tree r = TREE_TYPE (TREE_VALUE (arg_types));
14225 /* Don't pick up extra function qualifiers from the basetype. */
14226 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
14227 if (! MAYBE_CLASS_TYPE_P (r))
14228 {
14229 /* [temp.deduct]
14230
14231 Type deduction may fail for any of the following
14232 reasons:
14233
14234 -- Attempting to create "pointer to member of T" when T
14235 is not a class type. */
14236 if (complain & tf_error)
14237 error ("creating pointer to member function of non-class type %qT",
14238 r);
14239 return error_mark_node;
14240 }
14241
14242 fntype = build_method_type_directly (r, return_type,
14243 TREE_CHAIN (arg_types));
14244 }
14245 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
14246
14247 /* See comment above. */
14248 tree raises = NULL_TREE;
14249 cp_ref_qualifier rqual = type_memfn_rqual (t);
14250 fntype = build_cp_fntype_variant (fntype, rqual, raises, late_return_type_p);
14251
14252 return fntype;
14253 }
14254
14255 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
14256 ARGS into that specification, and return the substituted
14257 specification. If there is no specification, return NULL_TREE. */
14258
14259 static tree
14260 tsubst_exception_specification (tree fntype,
14261 tree args,
14262 tsubst_flags_t complain,
14263 tree in_decl,
14264 bool defer_ok)
14265 {
14266 tree specs;
14267 tree new_specs;
14268
14269 specs = TYPE_RAISES_EXCEPTIONS (fntype);
14270 new_specs = NULL_TREE;
14271 if (specs && TREE_PURPOSE (specs))
14272 {
14273 /* A noexcept-specifier. */
14274 tree expr = TREE_PURPOSE (specs);
14275 if (TREE_CODE (expr) == INTEGER_CST)
14276 new_specs = expr;
14277 else if (defer_ok)
14278 {
14279 /* Defer instantiation of noexcept-specifiers to avoid
14280 excessive instantiations (c++/49107). */
14281 new_specs = make_node (DEFERRED_NOEXCEPT);
14282 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
14283 {
14284 /* We already partially instantiated this member template,
14285 so combine the new args with the old. */
14286 DEFERRED_NOEXCEPT_PATTERN (new_specs)
14287 = DEFERRED_NOEXCEPT_PATTERN (expr);
14288 DEFERRED_NOEXCEPT_ARGS (new_specs)
14289 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
14290 }
14291 else
14292 {
14293 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
14294 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
14295 }
14296 }
14297 else
14298 {
14299 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
14300 {
14301 args = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr),
14302 args);
14303 expr = DEFERRED_NOEXCEPT_PATTERN (expr);
14304 }
14305 new_specs = tsubst_copy_and_build
14306 (expr, args, complain, in_decl, /*function_p=*/false,
14307 /*integral_constant_expression_p=*/true);
14308 }
14309 new_specs = build_noexcept_spec (new_specs, complain);
14310 }
14311 else if (specs)
14312 {
14313 if (! TREE_VALUE (specs))
14314 new_specs = specs;
14315 else
14316 while (specs)
14317 {
14318 tree spec;
14319 int i, len = 1;
14320 tree expanded_specs = NULL_TREE;
14321
14322 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
14323 {
14324 /* Expand the pack expansion type. */
14325 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
14326 args, complain,
14327 in_decl);
14328
14329 if (expanded_specs == error_mark_node)
14330 return error_mark_node;
14331 else if (TREE_CODE (expanded_specs) == TREE_VEC)
14332 len = TREE_VEC_LENGTH (expanded_specs);
14333 else
14334 {
14335 /* We're substituting into a member template, so
14336 we got a TYPE_PACK_EXPANSION back. Add that
14337 expansion and move on. */
14338 gcc_assert (TREE_CODE (expanded_specs)
14339 == TYPE_PACK_EXPANSION);
14340 new_specs = add_exception_specifier (new_specs,
14341 expanded_specs,
14342 complain);
14343 specs = TREE_CHAIN (specs);
14344 continue;
14345 }
14346 }
14347
14348 for (i = 0; i < len; ++i)
14349 {
14350 if (expanded_specs)
14351 spec = TREE_VEC_ELT (expanded_specs, i);
14352 else
14353 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
14354 if (spec == error_mark_node)
14355 return spec;
14356 new_specs = add_exception_specifier (new_specs, spec,
14357 complain);
14358 }
14359
14360 specs = TREE_CHAIN (specs);
14361 }
14362 }
14363 return new_specs;
14364 }
14365
14366 /* Take the tree structure T and replace template parameters used
14367 therein with the argument vector ARGS. IN_DECL is an associated
14368 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
14369 Issue error and warning messages under control of COMPLAIN. Note
14370 that we must be relatively non-tolerant of extensions here, in
14371 order to preserve conformance; if we allow substitutions that
14372 should not be allowed, we may allow argument deductions that should
14373 not succeed, and therefore report ambiguous overload situations
14374 where there are none. In theory, we could allow the substitution,
14375 but indicate that it should have failed, and allow our caller to
14376 make sure that the right thing happens, but we don't try to do this
14377 yet.
14378
14379 This function is used for dealing with types, decls and the like;
14380 for expressions, use tsubst_expr or tsubst_copy. */
14381
14382 tree
14383 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14384 {
14385 enum tree_code code;
14386 tree type, r = NULL_TREE;
14387
14388 if (t == NULL_TREE || t == error_mark_node
14389 || t == integer_type_node
14390 || t == void_type_node
14391 || t == char_type_node
14392 || t == unknown_type_node
14393 || TREE_CODE (t) == NAMESPACE_DECL
14394 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
14395 return t;
14396
14397 if (DECL_P (t))
14398 return tsubst_decl (t, args, complain);
14399
14400 if (args == NULL_TREE)
14401 return t;
14402
14403 code = TREE_CODE (t);
14404
14405 if (code == IDENTIFIER_NODE)
14406 type = IDENTIFIER_TYPE_VALUE (t);
14407 else
14408 type = TREE_TYPE (t);
14409
14410 gcc_assert (type != unknown_type_node);
14411
14412 /* Reuse typedefs. We need to do this to handle dependent attributes,
14413 such as attribute aligned. */
14414 if (TYPE_P (t)
14415 && typedef_variant_p (t))
14416 {
14417 tree decl = TYPE_NAME (t);
14418
14419 if (alias_template_specialization_p (t))
14420 {
14421 /* DECL represents an alias template and we want to
14422 instantiate it. */
14423 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
14424 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
14425 r = instantiate_alias_template (tmpl, gen_args, complain);
14426 }
14427 else if (DECL_CLASS_SCOPE_P (decl)
14428 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
14429 && uses_template_parms (DECL_CONTEXT (decl)))
14430 {
14431 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
14432 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
14433 r = retrieve_specialization (tmpl, gen_args, 0);
14434 }
14435 else if (DECL_FUNCTION_SCOPE_P (decl)
14436 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
14437 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
14438 r = retrieve_local_specialization (decl);
14439 else
14440 /* The typedef is from a non-template context. */
14441 return t;
14442
14443 if (r)
14444 {
14445 r = TREE_TYPE (r);
14446 r = cp_build_qualified_type_real
14447 (r, cp_type_quals (t) | cp_type_quals (r),
14448 complain | tf_ignore_bad_quals);
14449 return r;
14450 }
14451 else
14452 {
14453 /* We don't have an instantiation yet, so drop the typedef. */
14454 int quals = cp_type_quals (t);
14455 t = DECL_ORIGINAL_TYPE (decl);
14456 t = cp_build_qualified_type_real (t, quals,
14457 complain | tf_ignore_bad_quals);
14458 }
14459 }
14460
14461 bool fndecl_type = (complain & tf_fndecl_type);
14462 complain &= ~tf_fndecl_type;
14463
14464 if (type
14465 && code != TYPENAME_TYPE
14466 && code != TEMPLATE_TYPE_PARM
14467 && code != TEMPLATE_PARM_INDEX
14468 && code != IDENTIFIER_NODE
14469 && code != FUNCTION_TYPE
14470 && code != METHOD_TYPE)
14471 type = tsubst (type, args, complain, in_decl);
14472 if (type == error_mark_node)
14473 return error_mark_node;
14474
14475 switch (code)
14476 {
14477 case RECORD_TYPE:
14478 case UNION_TYPE:
14479 case ENUMERAL_TYPE:
14480 return tsubst_aggr_type (t, args, complain, in_decl,
14481 /*entering_scope=*/0);
14482
14483 case ERROR_MARK:
14484 case IDENTIFIER_NODE:
14485 case VOID_TYPE:
14486 case REAL_TYPE:
14487 case COMPLEX_TYPE:
14488 case VECTOR_TYPE:
14489 case BOOLEAN_TYPE:
14490 case NULLPTR_TYPE:
14491 case LANG_TYPE:
14492 return t;
14493
14494 case INTEGER_TYPE:
14495 if (t == integer_type_node)
14496 return t;
14497
14498 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
14499 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
14500 return t;
14501
14502 {
14503 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
14504
14505 max = tsubst_expr (omax, args, complain, in_decl,
14506 /*integral_constant_expression_p=*/false);
14507
14508 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
14509 needed. */
14510 if (TREE_CODE (max) == NOP_EXPR
14511 && TREE_SIDE_EFFECTS (omax)
14512 && !TREE_TYPE (max))
14513 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
14514
14515 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
14516 with TREE_SIDE_EFFECTS that indicates this is not an integral
14517 constant expression. */
14518 if (processing_template_decl
14519 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
14520 {
14521 gcc_assert (TREE_CODE (max) == NOP_EXPR);
14522 TREE_SIDE_EFFECTS (max) = 1;
14523 }
14524
14525 return compute_array_index_type (NULL_TREE, max, complain);
14526 }
14527
14528 case TEMPLATE_TYPE_PARM:
14529 case TEMPLATE_TEMPLATE_PARM:
14530 case BOUND_TEMPLATE_TEMPLATE_PARM:
14531 case TEMPLATE_PARM_INDEX:
14532 {
14533 int idx;
14534 int level;
14535 int levels;
14536 tree arg = NULL_TREE;
14537
14538 /* Early in template argument deduction substitution, we don't
14539 want to reduce the level of 'auto', or it will be confused
14540 with a normal template parm in subsequent deduction. */
14541 if (is_auto (t) && (complain & tf_partial))
14542 return t;
14543
14544 r = NULL_TREE;
14545
14546 gcc_assert (TREE_VEC_LENGTH (args) > 0);
14547 template_parm_level_and_index (t, &level, &idx);
14548
14549 levels = TMPL_ARGS_DEPTH (args);
14550 if (level <= levels
14551 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
14552 {
14553 arg = TMPL_ARG (args, level, idx);
14554
14555 /* See through ARGUMENT_PACK_SELECT arguments. */
14556 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
14557 arg = argument_pack_select_arg (arg);
14558 }
14559
14560 if (arg == error_mark_node)
14561 return error_mark_node;
14562 else if (arg != NULL_TREE)
14563 {
14564 if (ARGUMENT_PACK_P (arg))
14565 /* If ARG is an argument pack, we don't actually want to
14566 perform a substitution here, because substitutions
14567 for argument packs are only done
14568 element-by-element. We can get to this point when
14569 substituting the type of a non-type template
14570 parameter pack, when that type actually contains
14571 template parameter packs from an outer template, e.g.,
14572
14573 template<typename... Types> struct A {
14574 template<Types... Values> struct B { };
14575 }; */
14576 return t;
14577
14578 if (code == TEMPLATE_TYPE_PARM)
14579 {
14580 int quals;
14581 gcc_assert (TYPE_P (arg));
14582
14583 quals = cp_type_quals (arg) | cp_type_quals (t);
14584
14585 return cp_build_qualified_type_real
14586 (arg, quals, complain | tf_ignore_bad_quals);
14587 }
14588 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
14589 {
14590 /* We are processing a type constructed from a
14591 template template parameter. */
14592 tree argvec = tsubst (TYPE_TI_ARGS (t),
14593 args, complain, in_decl);
14594 if (argvec == error_mark_node)
14595 return error_mark_node;
14596
14597 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
14598 || TREE_CODE (arg) == TEMPLATE_DECL
14599 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
14600
14601 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
14602 /* Consider this code:
14603
14604 template <template <class> class Template>
14605 struct Internal {
14606 template <class Arg> using Bind = Template<Arg>;
14607 };
14608
14609 template <template <class> class Template, class Arg>
14610 using Instantiate = Template<Arg>; //#0
14611
14612 template <template <class> class Template,
14613 class Argument>
14614 using Bind =
14615 Instantiate<Internal<Template>::template Bind,
14616 Argument>; //#1
14617
14618 When #1 is parsed, the
14619 BOUND_TEMPLATE_TEMPLATE_PARM representing the
14620 parameter `Template' in #0 matches the
14621 UNBOUND_CLASS_TEMPLATE representing the argument
14622 `Internal<Template>::template Bind'; We then want
14623 to assemble the type `Bind<Argument>' that can't
14624 be fully created right now, because
14625 `Internal<Template>' not being complete, the Bind
14626 template cannot be looked up in that context. So
14627 we need to "store" `Bind<Argument>' for later
14628 when the context of Bind becomes complete. Let's
14629 store that in a TYPENAME_TYPE. */
14630 return make_typename_type (TYPE_CONTEXT (arg),
14631 build_nt (TEMPLATE_ID_EXPR,
14632 TYPE_IDENTIFIER (arg),
14633 argvec),
14634 typename_type,
14635 complain);
14636
14637 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
14638 are resolving nested-types in the signature of a
14639 member function templates. Otherwise ARG is a
14640 TEMPLATE_DECL and is the real template to be
14641 instantiated. */
14642 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
14643 arg = TYPE_NAME (arg);
14644
14645 r = lookup_template_class (arg,
14646 argvec, in_decl,
14647 DECL_CONTEXT (arg),
14648 /*entering_scope=*/0,
14649 complain);
14650 return cp_build_qualified_type_real
14651 (r, cp_type_quals (t) | cp_type_quals (r), complain);
14652 }
14653 else if (code == TEMPLATE_TEMPLATE_PARM)
14654 return arg;
14655 else
14656 /* TEMPLATE_PARM_INDEX. */
14657 return convert_from_reference (unshare_expr (arg));
14658 }
14659
14660 if (level == 1)
14661 /* This can happen during the attempted tsubst'ing in
14662 unify. This means that we don't yet have any information
14663 about the template parameter in question. */
14664 return t;
14665
14666 /* If we get here, we must have been looking at a parm for a
14667 more deeply nested template. Make a new version of this
14668 template parameter, but with a lower level. */
14669 switch (code)
14670 {
14671 case TEMPLATE_TYPE_PARM:
14672 case TEMPLATE_TEMPLATE_PARM:
14673 case BOUND_TEMPLATE_TEMPLATE_PARM:
14674 if (cp_type_quals (t))
14675 {
14676 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
14677 r = cp_build_qualified_type_real
14678 (r, cp_type_quals (t),
14679 complain | (code == TEMPLATE_TYPE_PARM
14680 ? tf_ignore_bad_quals : 0));
14681 }
14682 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
14683 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
14684 && (r = (TEMPLATE_PARM_DESCENDANTS
14685 (TEMPLATE_TYPE_PARM_INDEX (t))))
14686 && (r = TREE_TYPE (r))
14687 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
14688 /* Break infinite recursion when substituting the constraints
14689 of a constrained placeholder. */;
14690 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
14691 && !PLACEHOLDER_TYPE_CONSTRAINTS (t)
14692 && !CLASS_PLACEHOLDER_TEMPLATE (t)
14693 && (arg = TEMPLATE_TYPE_PARM_INDEX (t),
14694 r = TEMPLATE_PARM_DESCENDANTS (arg))
14695 && (TEMPLATE_PARM_LEVEL (r)
14696 == TEMPLATE_PARM_LEVEL (arg) - levels))
14697 /* Cache the simple case of lowering a type parameter. */
14698 r = TREE_TYPE (r);
14699 else
14700 {
14701 r = copy_type (t);
14702 TEMPLATE_TYPE_PARM_INDEX (r)
14703 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
14704 r, levels, args, complain);
14705 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
14706 TYPE_MAIN_VARIANT (r) = r;
14707 TYPE_POINTER_TO (r) = NULL_TREE;
14708 TYPE_REFERENCE_TO (r) = NULL_TREE;
14709
14710 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
14711 {
14712 /* Propagate constraints on placeholders. */
14713 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
14714 PLACEHOLDER_TYPE_CONSTRAINTS (r)
14715 = tsubst_constraint (constr, args, complain, in_decl);
14716 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
14717 {
14718 pl = tsubst_copy (pl, args, complain, in_decl);
14719 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
14720 }
14721 }
14722
14723 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
14724 /* We have reduced the level of the template
14725 template parameter, but not the levels of its
14726 template parameters, so canonical_type_parameter
14727 will not be able to find the canonical template
14728 template parameter for this level. Thus, we
14729 require structural equality checking to compare
14730 TEMPLATE_TEMPLATE_PARMs. */
14731 SET_TYPE_STRUCTURAL_EQUALITY (r);
14732 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
14733 SET_TYPE_STRUCTURAL_EQUALITY (r);
14734 else
14735 TYPE_CANONICAL (r) = canonical_type_parameter (r);
14736
14737 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
14738 {
14739 tree tinfo = TYPE_TEMPLATE_INFO (t);
14740 /* We might need to substitute into the types of non-type
14741 template parameters. */
14742 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
14743 complain, in_decl);
14744 if (tmpl == error_mark_node)
14745 return error_mark_node;
14746 tree argvec = tsubst (TI_ARGS (tinfo), args,
14747 complain, in_decl);
14748 if (argvec == error_mark_node)
14749 return error_mark_node;
14750
14751 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
14752 = build_template_info (tmpl, argvec);
14753 }
14754 }
14755 break;
14756
14757 case TEMPLATE_PARM_INDEX:
14758 /* OK, now substitute the type of the non-type parameter. We
14759 couldn't do it earlier because it might be an auto parameter,
14760 and we wouldn't need to if we had an argument. */
14761 type = tsubst (type, args, complain, in_decl);
14762 if (type == error_mark_node)
14763 return error_mark_node;
14764 r = reduce_template_parm_level (t, type, levels, args, complain);
14765 break;
14766
14767 default:
14768 gcc_unreachable ();
14769 }
14770
14771 return r;
14772 }
14773
14774 case TREE_LIST:
14775 {
14776 tree purpose, value, chain;
14777
14778 if (t == void_list_node)
14779 return t;
14780
14781 purpose = TREE_PURPOSE (t);
14782 if (purpose)
14783 {
14784 purpose = tsubst (purpose, args, complain, in_decl);
14785 if (purpose == error_mark_node)
14786 return error_mark_node;
14787 }
14788 value = TREE_VALUE (t);
14789 if (value)
14790 {
14791 value = tsubst (value, args, complain, in_decl);
14792 if (value == error_mark_node)
14793 return error_mark_node;
14794 }
14795 chain = TREE_CHAIN (t);
14796 if (chain && chain != void_type_node)
14797 {
14798 chain = tsubst (chain, args, complain, in_decl);
14799 if (chain == error_mark_node)
14800 return error_mark_node;
14801 }
14802 if (purpose == TREE_PURPOSE (t)
14803 && value == TREE_VALUE (t)
14804 && chain == TREE_CHAIN (t))
14805 return t;
14806 return hash_tree_cons (purpose, value, chain);
14807 }
14808
14809 case TREE_BINFO:
14810 /* We should never be tsubsting a binfo. */
14811 gcc_unreachable ();
14812
14813 case TREE_VEC:
14814 /* A vector of template arguments. */
14815 gcc_assert (!type);
14816 return tsubst_template_args (t, args, complain, in_decl);
14817
14818 case POINTER_TYPE:
14819 case REFERENCE_TYPE:
14820 {
14821 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
14822 return t;
14823
14824 /* [temp.deduct]
14825
14826 Type deduction may fail for any of the following
14827 reasons:
14828
14829 -- Attempting to create a pointer to reference type.
14830 -- Attempting to create a reference to a reference type or
14831 a reference to void.
14832
14833 Core issue 106 says that creating a reference to a reference
14834 during instantiation is no longer a cause for failure. We
14835 only enforce this check in strict C++98 mode. */
14836 if ((TYPE_REF_P (type)
14837 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
14838 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
14839 {
14840 static location_t last_loc;
14841
14842 /* We keep track of the last time we issued this error
14843 message to avoid spewing a ton of messages during a
14844 single bad template instantiation. */
14845 if (complain & tf_error
14846 && last_loc != input_location)
14847 {
14848 if (VOID_TYPE_P (type))
14849 error ("forming reference to void");
14850 else if (code == POINTER_TYPE)
14851 error ("forming pointer to reference type %qT", type);
14852 else
14853 error ("forming reference to reference type %qT", type);
14854 last_loc = input_location;
14855 }
14856
14857 return error_mark_node;
14858 }
14859 else if (TREE_CODE (type) == FUNCTION_TYPE
14860 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
14861 || type_memfn_rqual (type) != REF_QUAL_NONE))
14862 {
14863 if (complain & tf_error)
14864 {
14865 if (code == POINTER_TYPE)
14866 error ("forming pointer to qualified function type %qT",
14867 type);
14868 else
14869 error ("forming reference to qualified function type %qT",
14870 type);
14871 }
14872 return error_mark_node;
14873 }
14874 else if (code == POINTER_TYPE)
14875 {
14876 r = build_pointer_type (type);
14877 if (TREE_CODE (type) == METHOD_TYPE)
14878 r = build_ptrmemfunc_type (r);
14879 }
14880 else if (TYPE_REF_P (type))
14881 /* In C++0x, during template argument substitution, when there is an
14882 attempt to create a reference to a reference type, reference
14883 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14884
14885 "If a template-argument for a template-parameter T names a type
14886 that is a reference to a type A, an attempt to create the type
14887 'lvalue reference to cv T' creates the type 'lvalue reference to
14888 A,' while an attempt to create the type type rvalue reference to
14889 cv T' creates the type T"
14890 */
14891 r = cp_build_reference_type
14892 (TREE_TYPE (type),
14893 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
14894 else
14895 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
14896 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
14897
14898 if (r != error_mark_node)
14899 /* Will this ever be needed for TYPE_..._TO values? */
14900 layout_type (r);
14901
14902 return r;
14903 }
14904 case OFFSET_TYPE:
14905 {
14906 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
14907 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
14908 {
14909 /* [temp.deduct]
14910
14911 Type deduction may fail for any of the following
14912 reasons:
14913
14914 -- Attempting to create "pointer to member of T" when T
14915 is not a class type. */
14916 if (complain & tf_error)
14917 error ("creating pointer to member of non-class type %qT", r);
14918 return error_mark_node;
14919 }
14920 if (TYPE_REF_P (type))
14921 {
14922 if (complain & tf_error)
14923 error ("creating pointer to member reference type %qT", type);
14924 return error_mark_node;
14925 }
14926 if (VOID_TYPE_P (type))
14927 {
14928 if (complain & tf_error)
14929 error ("creating pointer to member of type void");
14930 return error_mark_node;
14931 }
14932 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
14933 if (TREE_CODE (type) == FUNCTION_TYPE)
14934 {
14935 /* The type of the implicit object parameter gets its
14936 cv-qualifiers from the FUNCTION_TYPE. */
14937 tree memptr;
14938 tree method_type
14939 = build_memfn_type (type, r, type_memfn_quals (type),
14940 type_memfn_rqual (type));
14941 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
14942 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
14943 complain);
14944 }
14945 else
14946 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
14947 cp_type_quals (t),
14948 complain);
14949 }
14950 case FUNCTION_TYPE:
14951 case METHOD_TYPE:
14952 {
14953 tree fntype;
14954 tree specs;
14955 fntype = tsubst_function_type (t, args, complain, in_decl);
14956 if (fntype == error_mark_node)
14957 return error_mark_node;
14958
14959 /* Substitute the exception specification. */
14960 specs = tsubst_exception_specification (t, args, complain, in_decl,
14961 /*defer_ok*/fndecl_type);
14962 if (specs == error_mark_node)
14963 return error_mark_node;
14964 if (specs)
14965 fntype = build_exception_variant (fntype, specs);
14966 return fntype;
14967 }
14968 case ARRAY_TYPE:
14969 {
14970 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
14971 if (domain == error_mark_node)
14972 return error_mark_node;
14973
14974 /* As an optimization, we avoid regenerating the array type if
14975 it will obviously be the same as T. */
14976 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
14977 return t;
14978
14979 /* These checks should match the ones in create_array_type_for_decl.
14980
14981 [temp.deduct]
14982
14983 The deduction may fail for any of the following reasons:
14984
14985 -- Attempting to create an array with an element type that
14986 is void, a function type, or a reference type, or [DR337]
14987 an abstract class type. */
14988 if (VOID_TYPE_P (type)
14989 || TREE_CODE (type) == FUNCTION_TYPE
14990 || (TREE_CODE (type) == ARRAY_TYPE
14991 && TYPE_DOMAIN (type) == NULL_TREE)
14992 || TYPE_REF_P (type))
14993 {
14994 if (complain & tf_error)
14995 error ("creating array of %qT", type);
14996 return error_mark_node;
14997 }
14998
14999 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
15000 return error_mark_node;
15001
15002 r = build_cplus_array_type (type, domain);
15003
15004 if (!valid_array_size_p (input_location, r, in_decl,
15005 (complain & tf_error)))
15006 return error_mark_node;
15007
15008 if (TYPE_USER_ALIGN (t))
15009 {
15010 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
15011 TYPE_USER_ALIGN (r) = 1;
15012 }
15013
15014 return r;
15015 }
15016
15017 case TYPENAME_TYPE:
15018 {
15019 tree ctx = TYPE_CONTEXT (t);
15020 if (TREE_CODE (ctx) == TYPE_PACK_EXPANSION)
15021 {
15022 ctx = tsubst_pack_expansion (ctx, args, complain, in_decl);
15023 if (ctx == error_mark_node
15024 || TREE_VEC_LENGTH (ctx) > 1)
15025 return error_mark_node;
15026 if (TREE_VEC_LENGTH (ctx) == 0)
15027 {
15028 if (complain & tf_error)
15029 error ("%qD is instantiated for an empty pack",
15030 TYPENAME_TYPE_FULLNAME (t));
15031 return error_mark_node;
15032 }
15033 ctx = TREE_VEC_ELT (ctx, 0);
15034 }
15035 else
15036 ctx = tsubst_aggr_type (ctx, args, complain, in_decl,
15037 /*entering_scope=*/1);
15038 if (ctx == error_mark_node)
15039 return error_mark_node;
15040
15041 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
15042 complain, in_decl);
15043 if (f == error_mark_node)
15044 return error_mark_node;
15045
15046 if (!MAYBE_CLASS_TYPE_P (ctx))
15047 {
15048 if (complain & tf_error)
15049 error ("%qT is not a class, struct, or union type", ctx);
15050 return error_mark_node;
15051 }
15052 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
15053 {
15054 /* Normally, make_typename_type does not require that the CTX
15055 have complete type in order to allow things like:
15056
15057 template <class T> struct S { typename S<T>::X Y; };
15058
15059 But, such constructs have already been resolved by this
15060 point, so here CTX really should have complete type, unless
15061 it's a partial instantiation. */
15062 ctx = complete_type (ctx);
15063 if (!COMPLETE_TYPE_P (ctx))
15064 {
15065 if (complain & tf_error)
15066 cxx_incomplete_type_error (NULL_TREE, ctx);
15067 return error_mark_node;
15068 }
15069 }
15070
15071 f = make_typename_type (ctx, f, typename_type,
15072 complain | tf_keep_type_decl);
15073 if (f == error_mark_node)
15074 return f;
15075 if (TREE_CODE (f) == TYPE_DECL)
15076 {
15077 complain |= tf_ignore_bad_quals;
15078 f = TREE_TYPE (f);
15079 }
15080
15081 if (TREE_CODE (f) != TYPENAME_TYPE)
15082 {
15083 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
15084 {
15085 if (complain & tf_error)
15086 error ("%qT resolves to %qT, which is not an enumeration type",
15087 t, f);
15088 else
15089 return error_mark_node;
15090 }
15091 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
15092 {
15093 if (complain & tf_error)
15094 error ("%qT resolves to %qT, which is is not a class type",
15095 t, f);
15096 else
15097 return error_mark_node;
15098 }
15099 }
15100
15101 return cp_build_qualified_type_real
15102 (f, cp_type_quals (f) | cp_type_quals (t), complain);
15103 }
15104
15105 case UNBOUND_CLASS_TEMPLATE:
15106 {
15107 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
15108 in_decl, /*entering_scope=*/1);
15109 tree name = TYPE_IDENTIFIER (t);
15110 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
15111
15112 if (ctx == error_mark_node || name == error_mark_node)
15113 return error_mark_node;
15114
15115 if (parm_list)
15116 parm_list = tsubst_template_parms (parm_list, args, complain);
15117 return make_unbound_class_template (ctx, name, parm_list, complain);
15118 }
15119
15120 case TYPEOF_TYPE:
15121 {
15122 tree type;
15123
15124 ++cp_unevaluated_operand;
15125 ++c_inhibit_evaluation_warnings;
15126
15127 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
15128 complain, in_decl,
15129 /*integral_constant_expression_p=*/false);
15130
15131 --cp_unevaluated_operand;
15132 --c_inhibit_evaluation_warnings;
15133
15134 type = finish_typeof (type);
15135 return cp_build_qualified_type_real (type,
15136 cp_type_quals (t)
15137 | cp_type_quals (type),
15138 complain);
15139 }
15140
15141 case DECLTYPE_TYPE:
15142 {
15143 tree type;
15144
15145 ++cp_unevaluated_operand;
15146 ++c_inhibit_evaluation_warnings;
15147
15148 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
15149 complain|tf_decltype, in_decl,
15150 /*function_p*/false,
15151 /*integral_constant_expression*/false);
15152
15153 --cp_unevaluated_operand;
15154 --c_inhibit_evaluation_warnings;
15155
15156 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
15157 type = lambda_capture_field_type (type,
15158 false /*explicit_init*/,
15159 DECLTYPE_FOR_REF_CAPTURE (t));
15160 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
15161 type = lambda_proxy_type (type);
15162 else
15163 {
15164 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
15165 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
15166 && EXPR_P (type))
15167 /* In a template ~id could be either a complement expression
15168 or an unqualified-id naming a destructor; if instantiating
15169 it produces an expression, it's not an id-expression or
15170 member access. */
15171 id = false;
15172 type = finish_decltype_type (type, id, complain);
15173 }
15174 return cp_build_qualified_type_real (type,
15175 cp_type_quals (t)
15176 | cp_type_quals (type),
15177 complain | tf_ignore_bad_quals);
15178 }
15179
15180 case UNDERLYING_TYPE:
15181 {
15182 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
15183 complain, in_decl);
15184 return finish_underlying_type (type);
15185 }
15186
15187 case TYPE_ARGUMENT_PACK:
15188 case NONTYPE_ARGUMENT_PACK:
15189 {
15190 tree r;
15191
15192 if (code == NONTYPE_ARGUMENT_PACK)
15193 r = make_node (code);
15194 else
15195 r = cxx_make_type (code);
15196
15197 tree pack_args = ARGUMENT_PACK_ARGS (t);
15198 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
15199 SET_ARGUMENT_PACK_ARGS (r, pack_args);
15200
15201 return r;
15202 }
15203
15204 case VOID_CST:
15205 case INTEGER_CST:
15206 case REAL_CST:
15207 case STRING_CST:
15208 case PLUS_EXPR:
15209 case MINUS_EXPR:
15210 case NEGATE_EXPR:
15211 case NOP_EXPR:
15212 case INDIRECT_REF:
15213 case ADDR_EXPR:
15214 case CALL_EXPR:
15215 case ARRAY_REF:
15216 case SCOPE_REF:
15217 /* We should use one of the expression tsubsts for these codes. */
15218 gcc_unreachable ();
15219
15220 default:
15221 sorry ("use of %qs in template", get_tree_code_name (code));
15222 return error_mark_node;
15223 }
15224 }
15225
15226 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
15227 expression on the left-hand side of the "." or "->" operator. We
15228 only do the lookup if we had a dependent BASELINK. Otherwise we
15229 adjust it onto the instantiated heirarchy. */
15230
15231 static tree
15232 tsubst_baselink (tree baselink, tree object_type,
15233 tree args, tsubst_flags_t complain, tree in_decl)
15234 {
15235 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
15236 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
15237 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
15238
15239 tree optype = BASELINK_OPTYPE (baselink);
15240 optype = tsubst (optype, args, complain, in_decl);
15241
15242 tree template_args = NULL_TREE;
15243 bool template_id_p = false;
15244 tree fns = BASELINK_FUNCTIONS (baselink);
15245 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
15246 {
15247 template_id_p = true;
15248 template_args = TREE_OPERAND (fns, 1);
15249 fns = TREE_OPERAND (fns, 0);
15250 if (template_args)
15251 template_args = tsubst_template_args (template_args, args,
15252 complain, in_decl);
15253 }
15254
15255 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
15256 binfo_type = tsubst (binfo_type, args, complain, in_decl);
15257 bool dependent_p = binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink));
15258
15259 if (dependent_p)
15260 {
15261 tree name = OVL_NAME (fns);
15262 if (IDENTIFIER_CONV_OP_P (name))
15263 name = make_conv_op_name (optype);
15264
15265 if (name == complete_dtor_identifier)
15266 /* Treat as-if non-dependent below. */
15267 dependent_p = false;
15268
15269 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
15270 if (!baselink)
15271 {
15272 if ((complain & tf_error)
15273 && constructor_name_p (name, qualifying_scope))
15274 error ("cannot call constructor %<%T::%D%> directly",
15275 qualifying_scope, name);
15276 return error_mark_node;
15277 }
15278
15279 if (BASELINK_P (baselink))
15280 fns = BASELINK_FUNCTIONS (baselink);
15281 }
15282 else
15283 /* We're going to overwrite pieces below, make a duplicate. */
15284 baselink = copy_node (baselink);
15285
15286 /* If lookup found a single function, mark it as used at this point.
15287 (If lookup found multiple functions the one selected later by
15288 overload resolution will be marked as used at that point.) */
15289 if (!template_id_p && !really_overloaded_fn (fns))
15290 {
15291 tree fn = OVL_FIRST (fns);
15292 bool ok = mark_used (fn, complain);
15293 if (!ok && !(complain & tf_error))
15294 return error_mark_node;
15295 if (ok && BASELINK_P (baselink))
15296 /* We might have instantiated an auto function. */
15297 TREE_TYPE (baselink) = TREE_TYPE (fn);
15298 }
15299
15300 if (BASELINK_P (baselink))
15301 {
15302 /* Add back the template arguments, if present. */
15303 if (template_id_p)
15304 BASELINK_FUNCTIONS (baselink)
15305 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
15306
15307 /* Update the conversion operator type. */
15308 BASELINK_OPTYPE (baselink) = optype;
15309 }
15310
15311 if (!object_type)
15312 object_type = current_class_type;
15313
15314 if (qualified_p || !dependent_p)
15315 {
15316 baselink = adjust_result_of_qualified_name_lookup (baselink,
15317 qualifying_scope,
15318 object_type);
15319 if (!qualified_p)
15320 /* We need to call adjust_result_of_qualified_name_lookup in case the
15321 destructor names a base class, but we unset BASELINK_QUALIFIED_P
15322 so that we still get virtual function binding. */
15323 BASELINK_QUALIFIED_P (baselink) = false;
15324 }
15325
15326 return baselink;
15327 }
15328
15329 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
15330 true if the qualified-id will be a postfix-expression in-and-of
15331 itself; false if more of the postfix-expression follows the
15332 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
15333 of "&". */
15334
15335 static tree
15336 tsubst_qualified_id (tree qualified_id, tree args,
15337 tsubst_flags_t complain, tree in_decl,
15338 bool done, bool address_p)
15339 {
15340 tree expr;
15341 tree scope;
15342 tree name;
15343 bool is_template;
15344 tree template_args;
15345 location_t loc = UNKNOWN_LOCATION;
15346
15347 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
15348
15349 /* Figure out what name to look up. */
15350 name = TREE_OPERAND (qualified_id, 1);
15351 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
15352 {
15353 is_template = true;
15354 loc = EXPR_LOCATION (name);
15355 template_args = TREE_OPERAND (name, 1);
15356 if (template_args)
15357 template_args = tsubst_template_args (template_args, args,
15358 complain, in_decl);
15359 if (template_args == error_mark_node)
15360 return error_mark_node;
15361 name = TREE_OPERAND (name, 0);
15362 }
15363 else
15364 {
15365 is_template = false;
15366 template_args = NULL_TREE;
15367 }
15368
15369 /* Substitute into the qualifying scope. When there are no ARGS, we
15370 are just trying to simplify a non-dependent expression. In that
15371 case the qualifying scope may be dependent, and, in any case,
15372 substituting will not help. */
15373 scope = TREE_OPERAND (qualified_id, 0);
15374 if (args)
15375 {
15376 scope = tsubst (scope, args, complain, in_decl);
15377 expr = tsubst_copy (name, args, complain, in_decl);
15378 }
15379 else
15380 expr = name;
15381
15382 if (dependent_scope_p (scope))
15383 {
15384 if (is_template)
15385 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
15386 tree r = build_qualified_name (NULL_TREE, scope, expr,
15387 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
15388 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
15389 return r;
15390 }
15391
15392 if (!BASELINK_P (name) && !DECL_P (expr))
15393 {
15394 if (TREE_CODE (expr) == BIT_NOT_EXPR)
15395 {
15396 /* A BIT_NOT_EXPR is used to represent a destructor. */
15397 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
15398 {
15399 error ("qualifying type %qT does not match destructor name ~%qT",
15400 scope, TREE_OPERAND (expr, 0));
15401 expr = error_mark_node;
15402 }
15403 else
15404 expr = lookup_qualified_name (scope, complete_dtor_identifier,
15405 /*is_type_p=*/0, false);
15406 }
15407 else
15408 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
15409 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
15410 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
15411 {
15412 if (complain & tf_error)
15413 {
15414 error ("dependent-name %qE is parsed as a non-type, but "
15415 "instantiation yields a type", qualified_id);
15416 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
15417 }
15418 return error_mark_node;
15419 }
15420 }
15421
15422 if (DECL_P (expr))
15423 {
15424 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
15425 scope);
15426 /* Remember that there was a reference to this entity. */
15427 if (!mark_used (expr, complain) && !(complain & tf_error))
15428 return error_mark_node;
15429 }
15430
15431 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
15432 {
15433 if (complain & tf_error)
15434 qualified_name_lookup_error (scope,
15435 TREE_OPERAND (qualified_id, 1),
15436 expr, input_location);
15437 return error_mark_node;
15438 }
15439
15440 if (is_template)
15441 {
15442 /* We may be repeating a check already done during parsing, but
15443 if it was well-formed and passed then, it will pass again
15444 now, and if it didn't, we wouldn't have got here. The case
15445 we want to catch is when we couldn't tell then, and can now,
15446 namely when templ prior to substitution was an
15447 identifier. */
15448 if (flag_concepts && check_auto_in_tmpl_args (expr, template_args))
15449 return error_mark_node;
15450
15451 if (variable_template_p (expr))
15452 expr = lookup_and_finish_template_variable (expr, template_args,
15453 complain);
15454 else
15455 expr = lookup_template_function (expr, template_args);
15456 }
15457
15458 if (expr == error_mark_node && complain & tf_error)
15459 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
15460 expr, input_location);
15461 else if (TYPE_P (scope))
15462 {
15463 expr = (adjust_result_of_qualified_name_lookup
15464 (expr, scope, current_nonlambda_class_type ()));
15465 expr = (finish_qualified_id_expr
15466 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
15467 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
15468 /*template_arg_p=*/false, complain));
15469 }
15470
15471 /* Expressions do not generally have reference type. */
15472 if (TREE_CODE (expr) != SCOPE_REF
15473 /* However, if we're about to form a pointer-to-member, we just
15474 want the referenced member referenced. */
15475 && TREE_CODE (expr) != OFFSET_REF)
15476 expr = convert_from_reference (expr);
15477
15478 if (REF_PARENTHESIZED_P (qualified_id))
15479 expr = force_paren_expr (expr);
15480
15481 return expr;
15482 }
15483
15484 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
15485 initializer, DECL is the substituted VAR_DECL. Other arguments are as
15486 for tsubst. */
15487
15488 static tree
15489 tsubst_init (tree init, tree decl, tree args,
15490 tsubst_flags_t complain, tree in_decl)
15491 {
15492 if (!init)
15493 return NULL_TREE;
15494
15495 init = tsubst_expr (init, args, complain, in_decl, false);
15496
15497 tree type = TREE_TYPE (decl);
15498
15499 if (!init && type != error_mark_node)
15500 {
15501 if (tree auto_node = type_uses_auto (type))
15502 {
15503 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
15504 {
15505 if (complain & tf_error)
15506 error ("initializer for %q#D expands to an empty list "
15507 "of expressions", decl);
15508 return error_mark_node;
15509 }
15510 }
15511 else if (!dependent_type_p (type))
15512 {
15513 /* If we had an initializer but it
15514 instantiated to nothing,
15515 value-initialize the object. This will
15516 only occur when the initializer was a
15517 pack expansion where the parameter packs
15518 used in that expansion were of length
15519 zero. */
15520 init = build_value_init (type, complain);
15521 if (TREE_CODE (init) == AGGR_INIT_EXPR)
15522 init = get_target_expr_sfinae (init, complain);
15523 if (TREE_CODE (init) == TARGET_EXPR)
15524 TARGET_EXPR_DIRECT_INIT_P (init) = true;
15525 }
15526 }
15527
15528 return init;
15529 }
15530
15531 /* Like tsubst, but deals with expressions. This function just replaces
15532 template parms; to finish processing the resultant expression, use
15533 tsubst_copy_and_build or tsubst_expr. */
15534
15535 static tree
15536 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15537 {
15538 enum tree_code code;
15539 tree r;
15540
15541 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
15542 return t;
15543
15544 code = TREE_CODE (t);
15545
15546 switch (code)
15547 {
15548 case PARM_DECL:
15549 r = retrieve_local_specialization (t);
15550
15551 if (r == NULL_TREE)
15552 {
15553 /* We get here for a use of 'this' in an NSDMI. */
15554 if (DECL_NAME (t) == this_identifier && current_class_ptr)
15555 return current_class_ptr;
15556
15557 /* This can happen for a parameter name used later in a function
15558 declaration (such as in a late-specified return type). Just
15559 make a dummy decl, since it's only used for its type. */
15560 gcc_assert (cp_unevaluated_operand != 0);
15561 r = tsubst_decl (t, args, complain);
15562 /* Give it the template pattern as its context; its true context
15563 hasn't been instantiated yet and this is good enough for
15564 mangling. */
15565 DECL_CONTEXT (r) = DECL_CONTEXT (t);
15566 }
15567
15568 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
15569 r = argument_pack_select_arg (r);
15570 if (!mark_used (r, complain) && !(complain & tf_error))
15571 return error_mark_node;
15572 return r;
15573
15574 case CONST_DECL:
15575 {
15576 tree enum_type;
15577 tree v;
15578
15579 if (DECL_TEMPLATE_PARM_P (t))
15580 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
15581 /* There is no need to substitute into namespace-scope
15582 enumerators. */
15583 if (DECL_NAMESPACE_SCOPE_P (t))
15584 return t;
15585 /* If ARGS is NULL, then T is known to be non-dependent. */
15586 if (args == NULL_TREE)
15587 return scalar_constant_value (t);
15588
15589 /* Unfortunately, we cannot just call lookup_name here.
15590 Consider:
15591
15592 template <int I> int f() {
15593 enum E { a = I };
15594 struct S { void g() { E e = a; } };
15595 };
15596
15597 When we instantiate f<7>::S::g(), say, lookup_name is not
15598 clever enough to find f<7>::a. */
15599 enum_type
15600 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
15601 /*entering_scope=*/0);
15602
15603 for (v = TYPE_VALUES (enum_type);
15604 v != NULL_TREE;
15605 v = TREE_CHAIN (v))
15606 if (TREE_PURPOSE (v) == DECL_NAME (t))
15607 return TREE_VALUE (v);
15608
15609 /* We didn't find the name. That should never happen; if
15610 name-lookup found it during preliminary parsing, we
15611 should find it again here during instantiation. */
15612 gcc_unreachable ();
15613 }
15614 return t;
15615
15616 case FIELD_DECL:
15617 if (DECL_CONTEXT (t))
15618 {
15619 tree ctx;
15620
15621 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
15622 /*entering_scope=*/1);
15623 if (ctx != DECL_CONTEXT (t))
15624 {
15625 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
15626 if (!r)
15627 {
15628 if (complain & tf_error)
15629 error ("using invalid field %qD", t);
15630 return error_mark_node;
15631 }
15632 return r;
15633 }
15634 }
15635
15636 return t;
15637
15638 case VAR_DECL:
15639 case FUNCTION_DECL:
15640 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
15641 r = tsubst (t, args, complain, in_decl);
15642 else if (local_variable_p (t)
15643 && uses_template_parms (DECL_CONTEXT (t)))
15644 {
15645 r = retrieve_local_specialization (t);
15646 if (r == NULL_TREE)
15647 {
15648 /* First try name lookup to find the instantiation. */
15649 r = lookup_name (DECL_NAME (t));
15650 if (r)
15651 {
15652 if (!VAR_P (r))
15653 {
15654 /* During error-recovery we may find a non-variable,
15655 even an OVERLOAD: just bail out and avoid ICEs and
15656 duplicate diagnostics (c++/62207). */
15657 gcc_assert (seen_error ());
15658 return error_mark_node;
15659 }
15660 if (!is_capture_proxy (r))
15661 {
15662 /* Make sure the one we found is the one we want. */
15663 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
15664 if (ctx != DECL_CONTEXT (r))
15665 r = NULL_TREE;
15666 }
15667 }
15668
15669 if (r)
15670 /* OK */;
15671 else
15672 {
15673 /* This can happen for a variable used in a
15674 late-specified return type of a local lambda, or for a
15675 local static or constant. Building a new VAR_DECL
15676 should be OK in all those cases. */
15677 r = tsubst_decl (t, args, complain);
15678 if (local_specializations)
15679 /* Avoid infinite recursion (79640). */
15680 register_local_specialization (r, t);
15681 if (decl_maybe_constant_var_p (r))
15682 {
15683 /* We can't call cp_finish_decl, so handle the
15684 initializer by hand. */
15685 tree init = tsubst_init (DECL_INITIAL (t), r, args,
15686 complain, in_decl);
15687 if (!processing_template_decl)
15688 init = maybe_constant_init (init);
15689 if (processing_template_decl
15690 ? potential_constant_expression (init)
15691 : reduced_constant_expression_p (init))
15692 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
15693 = TREE_CONSTANT (r) = true;
15694 DECL_INITIAL (r) = init;
15695 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
15696 TREE_TYPE (r)
15697 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
15698 complain, adc_variable_type);
15699 }
15700 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
15701 || decl_constant_var_p (r)
15702 || seen_error ());
15703 if (!processing_template_decl
15704 && !TREE_STATIC (r))
15705 r = process_outer_var_ref (r, complain);
15706 }
15707 /* Remember this for subsequent uses. */
15708 if (local_specializations)
15709 register_local_specialization (r, t);
15710 }
15711 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
15712 r = argument_pack_select_arg (r);
15713 }
15714 else
15715 r = t;
15716 if (!mark_used (r, complain))
15717 return error_mark_node;
15718 return r;
15719
15720 case NAMESPACE_DECL:
15721 return t;
15722
15723 case OVERLOAD:
15724 return t;
15725
15726 case BASELINK:
15727 return tsubst_baselink (t, current_nonlambda_class_type (),
15728 args, complain, in_decl);
15729
15730 case TEMPLATE_DECL:
15731 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
15732 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
15733 args, complain, in_decl);
15734 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
15735 return tsubst (t, args, complain, in_decl);
15736 else if (DECL_CLASS_SCOPE_P (t)
15737 && uses_template_parms (DECL_CONTEXT (t)))
15738 {
15739 /* Template template argument like the following example need
15740 special treatment:
15741
15742 template <template <class> class TT> struct C {};
15743 template <class T> struct D {
15744 template <class U> struct E {};
15745 C<E> c; // #1
15746 };
15747 D<int> d; // #2
15748
15749 We are processing the template argument `E' in #1 for
15750 the template instantiation #2. Originally, `E' is a
15751 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
15752 have to substitute this with one having context `D<int>'. */
15753
15754 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
15755 if (dependent_scope_p (context))
15756 {
15757 /* When rewriting a constructor into a deduction guide, a
15758 non-dependent name can become dependent, so memtmpl<args>
15759 becomes context::template memtmpl<args>. */
15760 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15761 return build_qualified_name (type, context, DECL_NAME (t),
15762 /*template*/true);
15763 }
15764 return lookup_field (context, DECL_NAME(t), 0, false);
15765 }
15766 else
15767 /* Ordinary template template argument. */
15768 return t;
15769
15770 case NON_LVALUE_EXPR:
15771 case VIEW_CONVERT_EXPR:
15772 {
15773 /* Handle location wrappers by substituting the wrapped node
15774 first, *then* reusing the resulting type. Doing the type
15775 first ensures that we handle template parameters and
15776 parameter pack expansions. */
15777 if (location_wrapper_p (t))
15778 {
15779 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args,
15780 complain, in_decl);
15781 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
15782 }
15783 tree op = TREE_OPERAND (t, 0);
15784 if (code == VIEW_CONVERT_EXPR
15785 && TREE_CODE (op) == TEMPLATE_PARM_INDEX)
15786 {
15787 /* Wrapper to make a C++20 template parameter object const. */
15788 op = tsubst_copy (op, args, complain, in_decl);
15789 if (TREE_CODE (op) == TEMPLATE_PARM_INDEX)
15790 {
15791 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15792 return build1 (code, type, op);
15793 }
15794 else
15795 {
15796 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (op)));
15797 return op;
15798 }
15799 }
15800 /* We shouldn't see any other uses of these in templates. */
15801 gcc_unreachable ();
15802 }
15803
15804 case CAST_EXPR:
15805 case REINTERPRET_CAST_EXPR:
15806 case CONST_CAST_EXPR:
15807 case STATIC_CAST_EXPR:
15808 case DYNAMIC_CAST_EXPR:
15809 case IMPLICIT_CONV_EXPR:
15810 case CONVERT_EXPR:
15811 case NOP_EXPR:
15812 {
15813 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15814 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15815 return build1 (code, type, op0);
15816 }
15817
15818 case SIZEOF_EXPR:
15819 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
15820 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
15821 {
15822 tree expanded, op = TREE_OPERAND (t, 0);
15823 int len = 0;
15824
15825 if (SIZEOF_EXPR_TYPE_P (t))
15826 op = TREE_TYPE (op);
15827
15828 ++cp_unevaluated_operand;
15829 ++c_inhibit_evaluation_warnings;
15830 /* We only want to compute the number of arguments. */
15831 if (PACK_EXPANSION_P (op))
15832 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
15833 else
15834 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
15835 args, complain, in_decl);
15836 --cp_unevaluated_operand;
15837 --c_inhibit_evaluation_warnings;
15838
15839 if (TREE_CODE (expanded) == TREE_VEC)
15840 {
15841 len = TREE_VEC_LENGTH (expanded);
15842 /* Set TREE_USED for the benefit of -Wunused. */
15843 for (int i = 0; i < len; i++)
15844 if (DECL_P (TREE_VEC_ELT (expanded, i)))
15845 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
15846 }
15847
15848 if (expanded == error_mark_node)
15849 return error_mark_node;
15850 else if (PACK_EXPANSION_P (expanded)
15851 || (TREE_CODE (expanded) == TREE_VEC
15852 && pack_expansion_args_count (expanded)))
15853
15854 {
15855 if (PACK_EXPANSION_P (expanded))
15856 /* OK. */;
15857 else if (TREE_VEC_LENGTH (expanded) == 1)
15858 expanded = TREE_VEC_ELT (expanded, 0);
15859 else
15860 expanded = make_argument_pack (expanded);
15861
15862 if (TYPE_P (expanded))
15863 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
15864 false,
15865 complain & tf_error);
15866 else
15867 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
15868 complain & tf_error);
15869 }
15870 else
15871 return build_int_cst (size_type_node, len);
15872 }
15873 if (SIZEOF_EXPR_TYPE_P (t))
15874 {
15875 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
15876 args, complain, in_decl);
15877 r = build1 (NOP_EXPR, r, error_mark_node);
15878 r = build1 (SIZEOF_EXPR,
15879 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
15880 SIZEOF_EXPR_TYPE_P (r) = 1;
15881 return r;
15882 }
15883 /* Fall through */
15884
15885 case INDIRECT_REF:
15886 case NEGATE_EXPR:
15887 case TRUTH_NOT_EXPR:
15888 case BIT_NOT_EXPR:
15889 case ADDR_EXPR:
15890 case UNARY_PLUS_EXPR: /* Unary + */
15891 case ALIGNOF_EXPR:
15892 case AT_ENCODE_EXPR:
15893 case ARROW_EXPR:
15894 case THROW_EXPR:
15895 case TYPEID_EXPR:
15896 case REALPART_EXPR:
15897 case IMAGPART_EXPR:
15898 case PAREN_EXPR:
15899 {
15900 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15901 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15902 r = build1 (code, type, op0);
15903 if (code == ALIGNOF_EXPR)
15904 ALIGNOF_EXPR_STD_P (r) = ALIGNOF_EXPR_STD_P (t);
15905 return r;
15906 }
15907
15908 case COMPONENT_REF:
15909 {
15910 tree object;
15911 tree name;
15912
15913 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15914 name = TREE_OPERAND (t, 1);
15915 if (TREE_CODE (name) == BIT_NOT_EXPR)
15916 {
15917 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15918 complain, in_decl);
15919 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15920 }
15921 else if (TREE_CODE (name) == SCOPE_REF
15922 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
15923 {
15924 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
15925 complain, in_decl);
15926 name = TREE_OPERAND (name, 1);
15927 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15928 complain, in_decl);
15929 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15930 name = build_qualified_name (/*type=*/NULL_TREE,
15931 base, name,
15932 /*template_p=*/false);
15933 }
15934 else if (BASELINK_P (name))
15935 name = tsubst_baselink (name,
15936 non_reference (TREE_TYPE (object)),
15937 args, complain,
15938 in_decl);
15939 else
15940 name = tsubst_copy (name, args, complain, in_decl);
15941 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
15942 }
15943
15944 case PLUS_EXPR:
15945 case MINUS_EXPR:
15946 case MULT_EXPR:
15947 case TRUNC_DIV_EXPR:
15948 case CEIL_DIV_EXPR:
15949 case FLOOR_DIV_EXPR:
15950 case ROUND_DIV_EXPR:
15951 case EXACT_DIV_EXPR:
15952 case BIT_AND_EXPR:
15953 case BIT_IOR_EXPR:
15954 case BIT_XOR_EXPR:
15955 case TRUNC_MOD_EXPR:
15956 case FLOOR_MOD_EXPR:
15957 case TRUTH_ANDIF_EXPR:
15958 case TRUTH_ORIF_EXPR:
15959 case TRUTH_AND_EXPR:
15960 case TRUTH_OR_EXPR:
15961 case RSHIFT_EXPR:
15962 case LSHIFT_EXPR:
15963 case RROTATE_EXPR:
15964 case LROTATE_EXPR:
15965 case EQ_EXPR:
15966 case NE_EXPR:
15967 case MAX_EXPR:
15968 case MIN_EXPR:
15969 case LE_EXPR:
15970 case GE_EXPR:
15971 case LT_EXPR:
15972 case GT_EXPR:
15973 case COMPOUND_EXPR:
15974 case DOTSTAR_EXPR:
15975 case MEMBER_REF:
15976 case PREDECREMENT_EXPR:
15977 case PREINCREMENT_EXPR:
15978 case POSTDECREMENT_EXPR:
15979 case POSTINCREMENT_EXPR:
15980 {
15981 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15982 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15983 return build_nt (code, op0, op1);
15984 }
15985
15986 case SCOPE_REF:
15987 {
15988 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15989 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15990 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
15991 QUALIFIED_NAME_IS_TEMPLATE (t));
15992 }
15993
15994 case ARRAY_REF:
15995 {
15996 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15997 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15998 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
15999 }
16000
16001 case CALL_EXPR:
16002 {
16003 int n = VL_EXP_OPERAND_LENGTH (t);
16004 tree result = build_vl_exp (CALL_EXPR, n);
16005 int i;
16006 for (i = 0; i < n; i++)
16007 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
16008 complain, in_decl);
16009 return result;
16010 }
16011
16012 case COND_EXPR:
16013 case MODOP_EXPR:
16014 case PSEUDO_DTOR_EXPR:
16015 case VEC_PERM_EXPR:
16016 {
16017 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16018 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16019 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
16020 r = build_nt (code, op0, op1, op2);
16021 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16022 return r;
16023 }
16024
16025 case NEW_EXPR:
16026 {
16027 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16028 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16029 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
16030 r = build_nt (code, op0, op1, op2);
16031 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
16032 return r;
16033 }
16034
16035 case DELETE_EXPR:
16036 {
16037 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16038 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16039 r = build_nt (code, op0, op1);
16040 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
16041 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
16042 return r;
16043 }
16044
16045 case TEMPLATE_ID_EXPR:
16046 {
16047 /* Substituted template arguments */
16048 tree fn = TREE_OPERAND (t, 0);
16049 tree targs = TREE_OPERAND (t, 1);
16050
16051 fn = tsubst_copy (fn, args, complain, in_decl);
16052 if (targs)
16053 targs = tsubst_template_args (targs, args, complain, in_decl);
16054
16055 return lookup_template_function (fn, targs);
16056 }
16057
16058 case TREE_LIST:
16059 {
16060 tree purpose, value, chain;
16061
16062 if (t == void_list_node)
16063 return t;
16064
16065 purpose = TREE_PURPOSE (t);
16066 if (purpose)
16067 purpose = tsubst_copy (purpose, args, complain, in_decl);
16068 value = TREE_VALUE (t);
16069 if (value)
16070 value = tsubst_copy (value, args, complain, in_decl);
16071 chain = TREE_CHAIN (t);
16072 if (chain && chain != void_type_node)
16073 chain = tsubst_copy (chain, args, complain, in_decl);
16074 if (purpose == TREE_PURPOSE (t)
16075 && value == TREE_VALUE (t)
16076 && chain == TREE_CHAIN (t))
16077 return t;
16078 return tree_cons (purpose, value, chain);
16079 }
16080
16081 case RECORD_TYPE:
16082 case UNION_TYPE:
16083 case ENUMERAL_TYPE:
16084 case INTEGER_TYPE:
16085 case TEMPLATE_TYPE_PARM:
16086 case TEMPLATE_TEMPLATE_PARM:
16087 case BOUND_TEMPLATE_TEMPLATE_PARM:
16088 case TEMPLATE_PARM_INDEX:
16089 case POINTER_TYPE:
16090 case REFERENCE_TYPE:
16091 case OFFSET_TYPE:
16092 case FUNCTION_TYPE:
16093 case METHOD_TYPE:
16094 case ARRAY_TYPE:
16095 case TYPENAME_TYPE:
16096 case UNBOUND_CLASS_TEMPLATE:
16097 case TYPEOF_TYPE:
16098 case DECLTYPE_TYPE:
16099 case TYPE_DECL:
16100 return tsubst (t, args, complain, in_decl);
16101
16102 case USING_DECL:
16103 t = DECL_NAME (t);
16104 /* Fall through. */
16105 case IDENTIFIER_NODE:
16106 if (IDENTIFIER_CONV_OP_P (t))
16107 {
16108 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16109 return make_conv_op_name (new_type);
16110 }
16111 else
16112 return t;
16113
16114 case CONSTRUCTOR:
16115 /* This is handled by tsubst_copy_and_build. */
16116 gcc_unreachable ();
16117
16118 case VA_ARG_EXPR:
16119 {
16120 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16121 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16122 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
16123 }
16124
16125 case CLEANUP_POINT_EXPR:
16126 /* We shouldn't have built any of these during initial template
16127 generation. Instead, they should be built during instantiation
16128 in response to the saved STMT_IS_FULL_EXPR_P setting. */
16129 gcc_unreachable ();
16130
16131 case OFFSET_REF:
16132 {
16133 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16134 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16135 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16136 r = build2 (code, type, op0, op1);
16137 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
16138 if (!mark_used (TREE_OPERAND (r, 1), complain)
16139 && !(complain & tf_error))
16140 return error_mark_node;
16141 return r;
16142 }
16143
16144 case EXPR_PACK_EXPANSION:
16145 error ("invalid use of pack expansion expression");
16146 return error_mark_node;
16147
16148 case NONTYPE_ARGUMENT_PACK:
16149 error ("use %<...%> to expand argument pack");
16150 return error_mark_node;
16151
16152 case VOID_CST:
16153 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
16154 return t;
16155
16156 case INTEGER_CST:
16157 case REAL_CST:
16158 case STRING_CST:
16159 case COMPLEX_CST:
16160 {
16161 /* Instantiate any typedefs in the type. */
16162 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16163 r = fold_convert (type, t);
16164 gcc_assert (TREE_CODE (r) == code);
16165 return r;
16166 }
16167
16168 case PTRMEM_CST:
16169 /* These can sometimes show up in a partial instantiation, but never
16170 involve template parms. */
16171 gcc_assert (!uses_template_parms (t));
16172 return t;
16173
16174 case UNARY_LEFT_FOLD_EXPR:
16175 return tsubst_unary_left_fold (t, args, complain, in_decl);
16176 case UNARY_RIGHT_FOLD_EXPR:
16177 return tsubst_unary_right_fold (t, args, complain, in_decl);
16178 case BINARY_LEFT_FOLD_EXPR:
16179 return tsubst_binary_left_fold (t, args, complain, in_decl);
16180 case BINARY_RIGHT_FOLD_EXPR:
16181 return tsubst_binary_right_fold (t, args, complain, in_decl);
16182 case PREDICT_EXPR:
16183 return t;
16184
16185 case DEBUG_BEGIN_STMT:
16186 /* ??? There's no point in copying it for now, but maybe some
16187 day it will contain more information, such as a pointer back
16188 to the containing function, inlined copy or so. */
16189 return t;
16190
16191 default:
16192 /* We shouldn't get here, but keep going if !flag_checking. */
16193 if (flag_checking)
16194 gcc_unreachable ();
16195 return t;
16196 }
16197 }
16198
16199 /* Helper function for tsubst_omp_clauses, used for instantiation of
16200 OMP_CLAUSE_DECL of clauses. */
16201
16202 static tree
16203 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
16204 tree in_decl, tree *iterator_cache)
16205 {
16206 if (decl == NULL_TREE)
16207 return NULL_TREE;
16208
16209 /* Handle OpenMP iterators. */
16210 if (TREE_CODE (decl) == TREE_LIST
16211 && TREE_PURPOSE (decl)
16212 && TREE_CODE (TREE_PURPOSE (decl)) == TREE_VEC)
16213 {
16214 tree ret;
16215 if (iterator_cache[0] == TREE_PURPOSE (decl))
16216 ret = iterator_cache[1];
16217 else
16218 {
16219 tree *tp = &ret;
16220 begin_scope (sk_omp, NULL);
16221 for (tree it = TREE_PURPOSE (decl); it; it = TREE_CHAIN (it))
16222 {
16223 *tp = copy_node (it);
16224 TREE_VEC_ELT (*tp, 0)
16225 = tsubst_decl (TREE_VEC_ELT (it, 0), args, complain);
16226 TREE_VEC_ELT (*tp, 1)
16227 = tsubst_expr (TREE_VEC_ELT (it, 1), args, complain, in_decl,
16228 /*integral_constant_expression_p=*/false);
16229 TREE_VEC_ELT (*tp, 2)
16230 = tsubst_expr (TREE_VEC_ELT (it, 2), args, complain, in_decl,
16231 /*integral_constant_expression_p=*/false);
16232 TREE_VEC_ELT (*tp, 3)
16233 = tsubst_expr (TREE_VEC_ELT (it, 3), args, complain, in_decl,
16234 /*integral_constant_expression_p=*/false);
16235 TREE_CHAIN (*tp) = NULL_TREE;
16236 tp = &TREE_CHAIN (*tp);
16237 }
16238 TREE_VEC_ELT (ret, 5) = poplevel (1, 1, 0);
16239 iterator_cache[0] = TREE_PURPOSE (decl);
16240 iterator_cache[1] = ret;
16241 }
16242 return build_tree_list (ret, tsubst_omp_clause_decl (TREE_VALUE (decl),
16243 args, complain,
16244 in_decl, NULL));
16245 }
16246
16247 /* Handle an OpenMP array section represented as a TREE_LIST (or
16248 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
16249 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
16250 TREE_LIST. We can handle it exactly the same as an array section
16251 (purpose, value, and a chain), even though the nomenclature
16252 (low_bound, length, etc) is different. */
16253 if (TREE_CODE (decl) == TREE_LIST)
16254 {
16255 tree low_bound
16256 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
16257 /*integral_constant_expression_p=*/false);
16258 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
16259 /*integral_constant_expression_p=*/false);
16260 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
16261 in_decl, NULL);
16262 if (TREE_PURPOSE (decl) == low_bound
16263 && TREE_VALUE (decl) == length
16264 && TREE_CHAIN (decl) == chain)
16265 return decl;
16266 tree ret = tree_cons (low_bound, length, chain);
16267 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
16268 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
16269 return ret;
16270 }
16271 tree ret = tsubst_expr (decl, args, complain, in_decl,
16272 /*integral_constant_expression_p=*/false);
16273 /* Undo convert_from_reference tsubst_expr could have called. */
16274 if (decl
16275 && REFERENCE_REF_P (ret)
16276 && !REFERENCE_REF_P (decl))
16277 ret = TREE_OPERAND (ret, 0);
16278 return ret;
16279 }
16280
16281 /* Like tsubst_copy, but specifically for OpenMP clauses. */
16282
16283 static tree
16284 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
16285 tree args, tsubst_flags_t complain, tree in_decl)
16286 {
16287 tree new_clauses = NULL_TREE, nc, oc;
16288 tree linear_no_step = NULL_TREE;
16289 tree iterator_cache[2] = { NULL_TREE, NULL_TREE };
16290
16291 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
16292 {
16293 nc = copy_node (oc);
16294 OMP_CLAUSE_CHAIN (nc) = new_clauses;
16295 new_clauses = nc;
16296
16297 switch (OMP_CLAUSE_CODE (nc))
16298 {
16299 case OMP_CLAUSE_LASTPRIVATE:
16300 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
16301 {
16302 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
16303 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
16304 in_decl, /*integral_constant_expression_p=*/false);
16305 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
16306 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
16307 }
16308 /* FALLTHRU */
16309 case OMP_CLAUSE_PRIVATE:
16310 case OMP_CLAUSE_SHARED:
16311 case OMP_CLAUSE_FIRSTPRIVATE:
16312 case OMP_CLAUSE_COPYIN:
16313 case OMP_CLAUSE_COPYPRIVATE:
16314 case OMP_CLAUSE_UNIFORM:
16315 case OMP_CLAUSE_DEPEND:
16316 case OMP_CLAUSE_FROM:
16317 case OMP_CLAUSE_TO:
16318 case OMP_CLAUSE_MAP:
16319 case OMP_CLAUSE_NONTEMPORAL:
16320 case OMP_CLAUSE_USE_DEVICE_PTR:
16321 case OMP_CLAUSE_USE_DEVICE_ADDR:
16322 case OMP_CLAUSE_IS_DEVICE_PTR:
16323 case OMP_CLAUSE_INCLUSIVE:
16324 case OMP_CLAUSE_EXCLUSIVE:
16325 OMP_CLAUSE_DECL (nc)
16326 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16327 in_decl, iterator_cache);
16328 break;
16329 case OMP_CLAUSE_TILE:
16330 case OMP_CLAUSE_IF:
16331 case OMP_CLAUSE_NUM_THREADS:
16332 case OMP_CLAUSE_SCHEDULE:
16333 case OMP_CLAUSE_COLLAPSE:
16334 case OMP_CLAUSE_FINAL:
16335 case OMP_CLAUSE_DEVICE:
16336 case OMP_CLAUSE_DIST_SCHEDULE:
16337 case OMP_CLAUSE_NUM_TEAMS:
16338 case OMP_CLAUSE_THREAD_LIMIT:
16339 case OMP_CLAUSE_SAFELEN:
16340 case OMP_CLAUSE_SIMDLEN:
16341 case OMP_CLAUSE_NUM_TASKS:
16342 case OMP_CLAUSE_GRAINSIZE:
16343 case OMP_CLAUSE_PRIORITY:
16344 case OMP_CLAUSE_ORDERED:
16345 case OMP_CLAUSE_HINT:
16346 case OMP_CLAUSE_NUM_GANGS:
16347 case OMP_CLAUSE_NUM_WORKERS:
16348 case OMP_CLAUSE_VECTOR_LENGTH:
16349 case OMP_CLAUSE_WORKER:
16350 case OMP_CLAUSE_VECTOR:
16351 case OMP_CLAUSE_ASYNC:
16352 case OMP_CLAUSE_WAIT:
16353 OMP_CLAUSE_OPERAND (nc, 0)
16354 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
16355 in_decl, /*integral_constant_expression_p=*/false);
16356 break;
16357 case OMP_CLAUSE_REDUCTION:
16358 case OMP_CLAUSE_IN_REDUCTION:
16359 case OMP_CLAUSE_TASK_REDUCTION:
16360 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
16361 {
16362 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
16363 if (TREE_CODE (placeholder) == SCOPE_REF)
16364 {
16365 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
16366 complain, in_decl);
16367 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
16368 = build_qualified_name (NULL_TREE, scope,
16369 TREE_OPERAND (placeholder, 1),
16370 false);
16371 }
16372 else
16373 gcc_assert (identifier_p (placeholder));
16374 }
16375 OMP_CLAUSE_DECL (nc)
16376 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16377 in_decl, NULL);
16378 break;
16379 case OMP_CLAUSE_GANG:
16380 case OMP_CLAUSE_ALIGNED:
16381 OMP_CLAUSE_DECL (nc)
16382 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16383 in_decl, NULL);
16384 OMP_CLAUSE_OPERAND (nc, 1)
16385 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
16386 in_decl, /*integral_constant_expression_p=*/false);
16387 break;
16388 case OMP_CLAUSE_LINEAR:
16389 OMP_CLAUSE_DECL (nc)
16390 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16391 in_decl, NULL);
16392 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
16393 {
16394 gcc_assert (!linear_no_step);
16395 linear_no_step = nc;
16396 }
16397 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
16398 OMP_CLAUSE_LINEAR_STEP (nc)
16399 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
16400 complain, in_decl, NULL);
16401 else
16402 OMP_CLAUSE_LINEAR_STEP (nc)
16403 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
16404 in_decl,
16405 /*integral_constant_expression_p=*/false);
16406 break;
16407 case OMP_CLAUSE_NOWAIT:
16408 case OMP_CLAUSE_DEFAULT:
16409 case OMP_CLAUSE_UNTIED:
16410 case OMP_CLAUSE_MERGEABLE:
16411 case OMP_CLAUSE_INBRANCH:
16412 case OMP_CLAUSE_NOTINBRANCH:
16413 case OMP_CLAUSE_PROC_BIND:
16414 case OMP_CLAUSE_FOR:
16415 case OMP_CLAUSE_PARALLEL:
16416 case OMP_CLAUSE_SECTIONS:
16417 case OMP_CLAUSE_TASKGROUP:
16418 case OMP_CLAUSE_NOGROUP:
16419 case OMP_CLAUSE_THREADS:
16420 case OMP_CLAUSE_SIMD:
16421 case OMP_CLAUSE_DEFAULTMAP:
16422 case OMP_CLAUSE_ORDER:
16423 case OMP_CLAUSE_BIND:
16424 case OMP_CLAUSE_INDEPENDENT:
16425 case OMP_CLAUSE_AUTO:
16426 case OMP_CLAUSE_SEQ:
16427 case OMP_CLAUSE_IF_PRESENT:
16428 case OMP_CLAUSE_FINALIZE:
16429 break;
16430 default:
16431 gcc_unreachable ();
16432 }
16433 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
16434 switch (OMP_CLAUSE_CODE (nc))
16435 {
16436 case OMP_CLAUSE_SHARED:
16437 case OMP_CLAUSE_PRIVATE:
16438 case OMP_CLAUSE_FIRSTPRIVATE:
16439 case OMP_CLAUSE_LASTPRIVATE:
16440 case OMP_CLAUSE_COPYPRIVATE:
16441 case OMP_CLAUSE_LINEAR:
16442 case OMP_CLAUSE_REDUCTION:
16443 case OMP_CLAUSE_IN_REDUCTION:
16444 case OMP_CLAUSE_TASK_REDUCTION:
16445 case OMP_CLAUSE_USE_DEVICE_PTR:
16446 case OMP_CLAUSE_USE_DEVICE_ADDR:
16447 case OMP_CLAUSE_IS_DEVICE_PTR:
16448 case OMP_CLAUSE_INCLUSIVE:
16449 case OMP_CLAUSE_EXCLUSIVE:
16450 /* tsubst_expr on SCOPE_REF results in returning
16451 finish_non_static_data_member result. Undo that here. */
16452 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
16453 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
16454 == IDENTIFIER_NODE))
16455 {
16456 tree t = OMP_CLAUSE_DECL (nc);
16457 tree v = t;
16458 while (v)
16459 switch (TREE_CODE (v))
16460 {
16461 case COMPONENT_REF:
16462 case MEM_REF:
16463 case INDIRECT_REF:
16464 CASE_CONVERT:
16465 case POINTER_PLUS_EXPR:
16466 v = TREE_OPERAND (v, 0);
16467 continue;
16468 case PARM_DECL:
16469 if (DECL_CONTEXT (v) == current_function_decl
16470 && DECL_ARTIFICIAL (v)
16471 && DECL_NAME (v) == this_identifier)
16472 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
16473 /* FALLTHRU */
16474 default:
16475 v = NULL_TREE;
16476 break;
16477 }
16478 }
16479 else if (VAR_P (OMP_CLAUSE_DECL (oc))
16480 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
16481 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
16482 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
16483 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
16484 {
16485 tree decl = OMP_CLAUSE_DECL (nc);
16486 if (VAR_P (decl))
16487 {
16488 retrofit_lang_decl (decl);
16489 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
16490 }
16491 }
16492 break;
16493 default:
16494 break;
16495 }
16496 }
16497
16498 new_clauses = nreverse (new_clauses);
16499 if (ort != C_ORT_OMP_DECLARE_SIMD)
16500 {
16501 new_clauses = finish_omp_clauses (new_clauses, ort);
16502 if (linear_no_step)
16503 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
16504 if (nc == linear_no_step)
16505 {
16506 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
16507 break;
16508 }
16509 }
16510 return new_clauses;
16511 }
16512
16513 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
16514
16515 static tree
16516 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
16517 tree in_decl)
16518 {
16519 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
16520
16521 tree purpose, value, chain;
16522
16523 if (t == NULL)
16524 return t;
16525
16526 if (TREE_CODE (t) != TREE_LIST)
16527 return tsubst_copy_and_build (t, args, complain, in_decl,
16528 /*function_p=*/false,
16529 /*integral_constant_expression_p=*/false);
16530
16531 if (t == void_list_node)
16532 return t;
16533
16534 purpose = TREE_PURPOSE (t);
16535 if (purpose)
16536 purpose = RECUR (purpose);
16537 value = TREE_VALUE (t);
16538 if (value)
16539 {
16540 if (TREE_CODE (value) != LABEL_DECL)
16541 value = RECUR (value);
16542 else
16543 {
16544 value = lookup_label (DECL_NAME (value));
16545 gcc_assert (TREE_CODE (value) == LABEL_DECL);
16546 TREE_USED (value) = 1;
16547 }
16548 }
16549 chain = TREE_CHAIN (t);
16550 if (chain && chain != void_type_node)
16551 chain = RECUR (chain);
16552 return tree_cons (purpose, value, chain);
16553 #undef RECUR
16554 }
16555
16556 /* Used to temporarily communicate the list of #pragma omp parallel
16557 clauses to #pragma omp for instantiation if they are combined
16558 together. */
16559
16560 static tree *omp_parallel_combined_clauses;
16561
16562 static tree tsubst_decomp_names (tree, tree, tree, tsubst_flags_t, tree,
16563 tree *, unsigned int *);
16564
16565 /* Substitute one OMP_FOR iterator. */
16566
16567 static bool
16568 tsubst_omp_for_iterator (tree t, int i, tree declv, tree &orig_declv,
16569 tree initv, tree condv, tree incrv, tree *clauses,
16570 tree args, tsubst_flags_t complain, tree in_decl,
16571 bool integral_constant_expression_p)
16572 {
16573 #define RECUR(NODE) \
16574 tsubst_expr ((NODE), args, complain, in_decl, \
16575 integral_constant_expression_p)
16576 tree decl, init, cond = NULL_TREE, incr = NULL_TREE;
16577 bool ret = false;
16578
16579 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
16580 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
16581
16582 decl = TREE_OPERAND (init, 0);
16583 init = TREE_OPERAND (init, 1);
16584 tree decl_expr = NULL_TREE;
16585 bool range_for = TREE_VEC_ELT (OMP_FOR_COND (t), i) == global_namespace;
16586 if (range_for)
16587 {
16588 bool decomp = false;
16589 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
16590 {
16591 tree v = DECL_VALUE_EXPR (decl);
16592 if (TREE_CODE (v) == ARRAY_REF
16593 && VAR_P (TREE_OPERAND (v, 0))
16594 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
16595 {
16596 tree decomp_first = NULL_TREE;
16597 unsigned decomp_cnt = 0;
16598 tree d = tsubst_decl (TREE_OPERAND (v, 0), args, complain);
16599 maybe_push_decl (d);
16600 d = tsubst_decomp_names (d, TREE_OPERAND (v, 0), args, complain,
16601 in_decl, &decomp_first, &decomp_cnt);
16602 decomp = true;
16603 if (d == error_mark_node)
16604 decl = error_mark_node;
16605 else
16606 for (unsigned int i = 0; i < decomp_cnt; i++)
16607 {
16608 if (!DECL_HAS_VALUE_EXPR_P (decomp_first))
16609 {
16610 tree v = build_nt (ARRAY_REF, d,
16611 size_int (decomp_cnt - i - 1),
16612 NULL_TREE, NULL_TREE);
16613 SET_DECL_VALUE_EXPR (decomp_first, v);
16614 DECL_HAS_VALUE_EXPR_P (decomp_first) = 1;
16615 }
16616 fit_decomposition_lang_decl (decomp_first, d);
16617 decomp_first = DECL_CHAIN (decomp_first);
16618 }
16619 }
16620 }
16621 decl = tsubst_decl (decl, args, complain);
16622 if (!decomp)
16623 maybe_push_decl (decl);
16624 }
16625 else if (init && TREE_CODE (init) == DECL_EXPR)
16626 {
16627 /* We need to jump through some hoops to handle declarations in the
16628 init-statement, since we might need to handle auto deduction,
16629 but we need to keep control of initialization. */
16630 decl_expr = init;
16631 init = DECL_INITIAL (DECL_EXPR_DECL (init));
16632 decl = tsubst_decl (decl, args, complain);
16633 }
16634 else
16635 {
16636 if (TREE_CODE (decl) == SCOPE_REF)
16637 {
16638 decl = RECUR (decl);
16639 if (TREE_CODE (decl) == COMPONENT_REF)
16640 {
16641 tree v = decl;
16642 while (v)
16643 switch (TREE_CODE (v))
16644 {
16645 case COMPONENT_REF:
16646 case MEM_REF:
16647 case INDIRECT_REF:
16648 CASE_CONVERT:
16649 case POINTER_PLUS_EXPR:
16650 v = TREE_OPERAND (v, 0);
16651 continue;
16652 case PARM_DECL:
16653 if (DECL_CONTEXT (v) == current_function_decl
16654 && DECL_ARTIFICIAL (v)
16655 && DECL_NAME (v) == this_identifier)
16656 {
16657 decl = TREE_OPERAND (decl, 1);
16658 decl = omp_privatize_field (decl, false);
16659 }
16660 /* FALLTHRU */
16661 default:
16662 v = NULL_TREE;
16663 break;
16664 }
16665 }
16666 }
16667 else
16668 decl = RECUR (decl);
16669 }
16670 init = RECUR (init);
16671
16672 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
16673 {
16674 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
16675 if (TREE_CODE (o) == TREE_LIST)
16676 TREE_VEC_ELT (orig_declv, i)
16677 = tree_cons (RECUR (TREE_PURPOSE (o)),
16678 RECUR (TREE_VALUE (o)),
16679 NULL_TREE);
16680 else
16681 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
16682 }
16683
16684 if (range_for)
16685 {
16686 tree this_pre_body = NULL_TREE;
16687 tree orig_init = NULL_TREE;
16688 tree orig_decl = NULL_TREE;
16689 cp_convert_omp_range_for (this_pre_body, NULL, decl, orig_decl, init,
16690 orig_init, cond, incr);
16691 if (orig_decl)
16692 {
16693 if (orig_declv == NULL_TREE)
16694 orig_declv = copy_node (declv);
16695 TREE_VEC_ELT (orig_declv, i) = orig_decl;
16696 ret = true;
16697 }
16698 else if (orig_declv)
16699 TREE_VEC_ELT (orig_declv, i) = decl;
16700 }
16701
16702 tree auto_node = type_uses_auto (TREE_TYPE (decl));
16703 if (!range_for && auto_node && init)
16704 TREE_TYPE (decl)
16705 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
16706
16707 gcc_assert (!type_dependent_expression_p (decl));
16708
16709 if (!CLASS_TYPE_P (TREE_TYPE (decl)) || range_for)
16710 {
16711 if (decl_expr)
16712 {
16713 /* Declare the variable, but don't let that initialize it. */
16714 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
16715 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
16716 RECUR (decl_expr);
16717 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
16718 }
16719
16720 if (!range_for)
16721 {
16722 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
16723 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
16724 if (TREE_CODE (incr) == MODIFY_EXPR)
16725 {
16726 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16727 tree rhs = RECUR (TREE_OPERAND (incr, 1));
16728 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
16729 NOP_EXPR, rhs, complain);
16730 }
16731 else
16732 incr = RECUR (incr);
16733 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
16734 TREE_VEC_ELT (orig_declv, i) = decl;
16735 }
16736 TREE_VEC_ELT (declv, i) = decl;
16737 TREE_VEC_ELT (initv, i) = init;
16738 TREE_VEC_ELT (condv, i) = cond;
16739 TREE_VEC_ELT (incrv, i) = incr;
16740 return ret;
16741 }
16742
16743 if (decl_expr)
16744 {
16745 /* Declare and initialize the variable. */
16746 RECUR (decl_expr);
16747 init = NULL_TREE;
16748 }
16749 else if (init)
16750 {
16751 tree *pc;
16752 int j;
16753 for (j = ((omp_parallel_combined_clauses == NULL
16754 || TREE_CODE (t) == OMP_LOOP) ? 1 : 0); j < 2; j++)
16755 {
16756 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
16757 {
16758 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
16759 && OMP_CLAUSE_DECL (*pc) == decl)
16760 break;
16761 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
16762 && OMP_CLAUSE_DECL (*pc) == decl)
16763 {
16764 if (j)
16765 break;
16766 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
16767 tree c = *pc;
16768 *pc = OMP_CLAUSE_CHAIN (c);
16769 OMP_CLAUSE_CHAIN (c) = *clauses;
16770 *clauses = c;
16771 }
16772 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
16773 && OMP_CLAUSE_DECL (*pc) == decl)
16774 {
16775 error ("iteration variable %qD should not be firstprivate",
16776 decl);
16777 *pc = OMP_CLAUSE_CHAIN (*pc);
16778 }
16779 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
16780 && OMP_CLAUSE_DECL (*pc) == decl)
16781 {
16782 error ("iteration variable %qD should not be reduction",
16783 decl);
16784 *pc = OMP_CLAUSE_CHAIN (*pc);
16785 }
16786 else
16787 pc = &OMP_CLAUSE_CHAIN (*pc);
16788 }
16789 if (*pc)
16790 break;
16791 }
16792 if (*pc == NULL_TREE)
16793 {
16794 tree c = build_omp_clause (input_location,
16795 TREE_CODE (t) == OMP_LOOP
16796 ? OMP_CLAUSE_LASTPRIVATE
16797 : OMP_CLAUSE_PRIVATE);
16798 OMP_CLAUSE_DECL (c) = decl;
16799 c = finish_omp_clauses (c, C_ORT_OMP);
16800 if (c)
16801 {
16802 OMP_CLAUSE_CHAIN (c) = *clauses;
16803 *clauses = c;
16804 }
16805 }
16806 }
16807 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
16808 if (COMPARISON_CLASS_P (cond))
16809 {
16810 tree op0 = RECUR (TREE_OPERAND (cond, 0));
16811 tree op1 = RECUR (TREE_OPERAND (cond, 1));
16812 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
16813 }
16814 else
16815 cond = RECUR (cond);
16816 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
16817 switch (TREE_CODE (incr))
16818 {
16819 case PREINCREMENT_EXPR:
16820 case PREDECREMENT_EXPR:
16821 case POSTINCREMENT_EXPR:
16822 case POSTDECREMENT_EXPR:
16823 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
16824 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
16825 break;
16826 case MODIFY_EXPR:
16827 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
16828 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
16829 {
16830 tree rhs = TREE_OPERAND (incr, 1);
16831 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16832 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
16833 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
16834 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16835 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
16836 rhs0, rhs1));
16837 }
16838 else
16839 incr = RECUR (incr);
16840 break;
16841 case MODOP_EXPR:
16842 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
16843 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
16844 {
16845 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16846 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16847 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
16848 TREE_TYPE (decl), lhs,
16849 RECUR (TREE_OPERAND (incr, 2))));
16850 }
16851 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
16852 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
16853 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
16854 {
16855 tree rhs = TREE_OPERAND (incr, 2);
16856 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16857 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
16858 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
16859 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16860 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
16861 rhs0, rhs1));
16862 }
16863 else
16864 incr = RECUR (incr);
16865 break;
16866 default:
16867 incr = RECUR (incr);
16868 break;
16869 }
16870
16871 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
16872 TREE_VEC_ELT (orig_declv, i) = decl;
16873 TREE_VEC_ELT (declv, i) = decl;
16874 TREE_VEC_ELT (initv, i) = init;
16875 TREE_VEC_ELT (condv, i) = cond;
16876 TREE_VEC_ELT (incrv, i) = incr;
16877 return false;
16878 #undef RECUR
16879 }
16880
16881 /* Helper function of tsubst_expr, find OMP_TEAMS inside
16882 of OMP_TARGET's body. */
16883
16884 static tree
16885 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
16886 {
16887 *walk_subtrees = 0;
16888 switch (TREE_CODE (*tp))
16889 {
16890 case OMP_TEAMS:
16891 return *tp;
16892 case BIND_EXPR:
16893 case STATEMENT_LIST:
16894 *walk_subtrees = 1;
16895 break;
16896 default:
16897 break;
16898 }
16899 return NULL_TREE;
16900 }
16901
16902 /* Helper function for tsubst_expr. For decomposition declaration
16903 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
16904 also the corresponding decls representing the identifiers
16905 of the decomposition declaration. Return DECL if successful
16906 or error_mark_node otherwise, set *FIRST to the first decl
16907 in the list chained through DECL_CHAIN and *CNT to the number
16908 of such decls. */
16909
16910 static tree
16911 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
16912 tsubst_flags_t complain, tree in_decl, tree *first,
16913 unsigned int *cnt)
16914 {
16915 tree decl2, decl3, prev = decl;
16916 *cnt = 0;
16917 gcc_assert (DECL_NAME (decl) == NULL_TREE);
16918 for (decl2 = DECL_CHAIN (pattern_decl);
16919 decl2
16920 && VAR_P (decl2)
16921 && DECL_DECOMPOSITION_P (decl2)
16922 && DECL_NAME (decl2);
16923 decl2 = DECL_CHAIN (decl2))
16924 {
16925 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
16926 {
16927 gcc_assert (errorcount);
16928 return error_mark_node;
16929 }
16930 (*cnt)++;
16931 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
16932 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
16933 tree v = DECL_VALUE_EXPR (decl2);
16934 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
16935 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
16936 decl3 = tsubst (decl2, args, complain, in_decl);
16937 SET_DECL_VALUE_EXPR (decl2, v);
16938 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
16939 if (VAR_P (decl3))
16940 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
16941 else
16942 {
16943 gcc_assert (errorcount);
16944 decl = error_mark_node;
16945 continue;
16946 }
16947 maybe_push_decl (decl3);
16948 if (error_operand_p (decl3))
16949 decl = error_mark_node;
16950 else if (decl != error_mark_node
16951 && DECL_CHAIN (decl3) != prev
16952 && decl != prev)
16953 {
16954 gcc_assert (errorcount);
16955 decl = error_mark_node;
16956 }
16957 else
16958 prev = decl3;
16959 }
16960 *first = prev;
16961 return decl;
16962 }
16963
16964 /* Return the proper local_specialization for init-capture pack DECL. */
16965
16966 static tree
16967 lookup_init_capture_pack (tree decl)
16968 {
16969 /* We handle normal pack captures by forwarding to the specialization of the
16970 captured parameter. We can't do that for pack init-captures; we need them
16971 to have their own local_specialization. We created the individual
16972 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
16973 when we process the DECL_EXPR for the pack init-capture in the template.
16974 So, how do we find them? We don't know the capture proxy pack when
16975 building the individual resulting proxies, and we don't know the
16976 individual proxies when instantiating the pack. What we have in common is
16977 the FIELD_DECL.
16978
16979 So...when we instantiate the FIELD_DECL, we stick the result in
16980 local_specializations. Then at the DECL_EXPR we look up that result, see
16981 how many elements it has, synthesize the names, and look them up. */
16982
16983 tree cname = DECL_NAME (decl);
16984 tree val = DECL_VALUE_EXPR (decl);
16985 tree field = TREE_OPERAND (val, 1);
16986 gcc_assert (TREE_CODE (field) == FIELD_DECL);
16987 tree fpack = retrieve_local_specialization (field);
16988 if (fpack == error_mark_node)
16989 return error_mark_node;
16990
16991 int len = 1;
16992 tree vec = NULL_TREE;
16993 tree r = NULL_TREE;
16994 if (TREE_CODE (fpack) == TREE_VEC)
16995 {
16996 len = TREE_VEC_LENGTH (fpack);
16997 vec = make_tree_vec (len);
16998 r = make_node (NONTYPE_ARGUMENT_PACK);
16999 SET_ARGUMENT_PACK_ARGS (r, vec);
17000 }
17001 for (int i = 0; i < len; ++i)
17002 {
17003 tree ename = vec ? make_ith_pack_parameter_name (cname, i) : cname;
17004 tree elt = lookup_name_real (ename, 0, 0, true, 0, LOOKUP_NORMAL);
17005 if (vec)
17006 TREE_VEC_ELT (vec, i) = elt;
17007 else
17008 r = elt;
17009 }
17010 return r;
17011 }
17012
17013 /* Like tsubst_copy for expressions, etc. but also does semantic
17014 processing. */
17015
17016 tree
17017 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
17018 bool integral_constant_expression_p)
17019 {
17020 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
17021 #define RECUR(NODE) \
17022 tsubst_expr ((NODE), args, complain, in_decl, \
17023 integral_constant_expression_p)
17024
17025 tree stmt, tmp;
17026 tree r;
17027 location_t loc;
17028
17029 if (t == NULL_TREE || t == error_mark_node)
17030 return t;
17031
17032 loc = input_location;
17033 if (location_t eloc = cp_expr_location (t))
17034 input_location = eloc;
17035 if (STATEMENT_CODE_P (TREE_CODE (t)))
17036 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
17037
17038 switch (TREE_CODE (t))
17039 {
17040 case STATEMENT_LIST:
17041 {
17042 tree_stmt_iterator i;
17043 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
17044 RECUR (tsi_stmt (i));
17045 break;
17046 }
17047
17048 case CTOR_INITIALIZER:
17049 finish_mem_initializers (tsubst_initializer_list
17050 (TREE_OPERAND (t, 0), args));
17051 break;
17052
17053 case RETURN_EXPR:
17054 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
17055 break;
17056
17057 case EXPR_STMT:
17058 tmp = RECUR (EXPR_STMT_EXPR (t));
17059 if (EXPR_STMT_STMT_EXPR_RESULT (t))
17060 finish_stmt_expr_expr (tmp, cur_stmt_expr);
17061 else
17062 finish_expr_stmt (tmp);
17063 break;
17064
17065 case USING_STMT:
17066 finish_using_directive (USING_STMT_NAMESPACE (t), /*attribs=*/NULL_TREE);
17067 break;
17068
17069 case DECL_EXPR:
17070 {
17071 tree decl, pattern_decl;
17072 tree init;
17073
17074 pattern_decl = decl = DECL_EXPR_DECL (t);
17075 if (TREE_CODE (decl) == LABEL_DECL)
17076 finish_label_decl (DECL_NAME (decl));
17077 else if (TREE_CODE (decl) == USING_DECL)
17078 {
17079 tree scope = USING_DECL_SCOPE (decl);
17080 tree name = DECL_NAME (decl);
17081
17082 scope = tsubst (scope, args, complain, in_decl);
17083 finish_nonmember_using_decl (scope, name);
17084 }
17085 else if (is_capture_proxy (decl)
17086 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
17087 {
17088 /* We're in tsubst_lambda_expr, we've already inserted a new
17089 capture proxy, so look it up and register it. */
17090 tree inst;
17091 if (!DECL_PACK_P (decl))
17092 {
17093 inst = lookup_name_real (DECL_NAME (decl), /*prefer_type*/0,
17094 /*nonclass*/1, /*block_p=*/true,
17095 /*ns_only*/0, LOOKUP_HIDDEN);
17096 gcc_assert (inst != decl && is_capture_proxy (inst));
17097 }
17098 else if (is_normal_capture_proxy (decl))
17099 {
17100 inst = (retrieve_local_specialization
17101 (DECL_CAPTURED_VARIABLE (decl)));
17102 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK);
17103 }
17104 else
17105 inst = lookup_init_capture_pack (decl);
17106
17107 register_local_specialization (inst, decl);
17108 break;
17109 }
17110 else if (DECL_PRETTY_FUNCTION_P (decl))
17111 decl = make_fname_decl (DECL_SOURCE_LOCATION (decl),
17112 DECL_NAME (decl),
17113 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
17114 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
17115 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
17116 /* Don't copy the old closure; we'll create a new one in
17117 tsubst_lambda_expr. */
17118 break;
17119 else
17120 {
17121 init = DECL_INITIAL (decl);
17122 /* The following tsubst call will clear the DECL_TEMPLATE_INFO
17123 for local variables, so save if DECL was declared constinit. */
17124 const bool constinit_p
17125 = (VAR_P (decl)
17126 && DECL_LANG_SPECIFIC (decl)
17127 && DECL_TEMPLATE_INFO (decl)
17128 && TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (decl)));
17129 decl = tsubst (decl, args, complain, in_decl);
17130 if (decl != error_mark_node)
17131 {
17132 /* By marking the declaration as instantiated, we avoid
17133 trying to instantiate it. Since instantiate_decl can't
17134 handle local variables, and since we've already done
17135 all that needs to be done, that's the right thing to
17136 do. */
17137 if (VAR_P (decl))
17138 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
17139 if (VAR_P (decl) && !DECL_NAME (decl)
17140 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
17141 /* Anonymous aggregates are a special case. */
17142 finish_anon_union (decl);
17143 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
17144 {
17145 DECL_CONTEXT (decl) = current_function_decl;
17146 if (DECL_NAME (decl) == this_identifier)
17147 {
17148 tree lam = DECL_CONTEXT (current_function_decl);
17149 lam = CLASSTYPE_LAMBDA_EXPR (lam);
17150 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
17151 }
17152 insert_capture_proxy (decl);
17153 }
17154 else if (DECL_IMPLICIT_TYPEDEF_P (t))
17155 /* We already did a pushtag. */;
17156 else if (TREE_CODE (decl) == FUNCTION_DECL
17157 && DECL_OMP_DECLARE_REDUCTION_P (decl)
17158 && DECL_FUNCTION_SCOPE_P (pattern_decl))
17159 {
17160 DECL_CONTEXT (decl) = NULL_TREE;
17161 pushdecl (decl);
17162 DECL_CONTEXT (decl) = current_function_decl;
17163 cp_check_omp_declare_reduction (decl);
17164 }
17165 else
17166 {
17167 bool const_init = false;
17168 unsigned int cnt = 0;
17169 tree first = NULL_TREE, ndecl = error_mark_node;
17170 maybe_push_decl (decl);
17171
17172 if (VAR_P (decl)
17173 && DECL_DECOMPOSITION_P (decl)
17174 && TREE_TYPE (pattern_decl) != error_mark_node)
17175 ndecl = tsubst_decomp_names (decl, pattern_decl, args,
17176 complain, in_decl, &first,
17177 &cnt);
17178
17179 init = tsubst_init (init, decl, args, complain, in_decl);
17180
17181 if (VAR_P (decl))
17182 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
17183 (pattern_decl));
17184
17185 if (ndecl != error_mark_node)
17186 cp_maybe_mangle_decomp (ndecl, first, cnt);
17187
17188 cp_finish_decl (decl, init, const_init, NULL_TREE,
17189 constinit_p ? LOOKUP_CONSTINIT : 0);
17190
17191 if (ndecl != error_mark_node)
17192 cp_finish_decomp (ndecl, first, cnt);
17193 }
17194 }
17195 }
17196
17197 break;
17198 }
17199
17200 case FOR_STMT:
17201 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
17202 RECUR (FOR_INIT_STMT (t));
17203 finish_init_stmt (stmt);
17204 tmp = RECUR (FOR_COND (t));
17205 finish_for_cond (tmp, stmt, false, 0);
17206 tmp = RECUR (FOR_EXPR (t));
17207 finish_for_expr (tmp, stmt);
17208 {
17209 bool prev = note_iteration_stmt_body_start ();
17210 RECUR (FOR_BODY (t));
17211 note_iteration_stmt_body_end (prev);
17212 }
17213 finish_for_stmt (stmt);
17214 break;
17215
17216 case RANGE_FOR_STMT:
17217 {
17218 /* Construct another range_for, if this is not a final
17219 substitution (for inside inside a generic lambda of a
17220 template). Otherwise convert to a regular for. */
17221 tree decl, expr;
17222 stmt = (processing_template_decl
17223 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
17224 : begin_for_stmt (NULL_TREE, NULL_TREE));
17225 RECUR (RANGE_FOR_INIT_STMT (t));
17226 decl = RANGE_FOR_DECL (t);
17227 decl = tsubst (decl, args, complain, in_decl);
17228 maybe_push_decl (decl);
17229 expr = RECUR (RANGE_FOR_EXPR (t));
17230
17231 tree decomp_first = NULL_TREE;
17232 unsigned decomp_cnt = 0;
17233 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
17234 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
17235 complain, in_decl,
17236 &decomp_first, &decomp_cnt);
17237
17238 if (processing_template_decl)
17239 {
17240 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
17241 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
17242 finish_range_for_decl (stmt, decl, expr);
17243 if (decomp_first && decl != error_mark_node)
17244 cp_finish_decomp (decl, decomp_first, decomp_cnt);
17245 }
17246 else
17247 {
17248 unsigned short unroll = (RANGE_FOR_UNROLL (t)
17249 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
17250 stmt = cp_convert_range_for (stmt, decl, expr,
17251 decomp_first, decomp_cnt,
17252 RANGE_FOR_IVDEP (t), unroll);
17253 }
17254
17255 bool prev = note_iteration_stmt_body_start ();
17256 RECUR (RANGE_FOR_BODY (t));
17257 note_iteration_stmt_body_end (prev);
17258 finish_for_stmt (stmt);
17259 }
17260 break;
17261
17262 case WHILE_STMT:
17263 stmt = begin_while_stmt ();
17264 tmp = RECUR (WHILE_COND (t));
17265 finish_while_stmt_cond (tmp, stmt, false, 0);
17266 {
17267 bool prev = note_iteration_stmt_body_start ();
17268 RECUR (WHILE_BODY (t));
17269 note_iteration_stmt_body_end (prev);
17270 }
17271 finish_while_stmt (stmt);
17272 break;
17273
17274 case DO_STMT:
17275 stmt = begin_do_stmt ();
17276 {
17277 bool prev = note_iteration_stmt_body_start ();
17278 RECUR (DO_BODY (t));
17279 note_iteration_stmt_body_end (prev);
17280 }
17281 finish_do_body (stmt);
17282 tmp = RECUR (DO_COND (t));
17283 finish_do_stmt (tmp, stmt, false, 0);
17284 break;
17285
17286 case IF_STMT:
17287 stmt = begin_if_stmt ();
17288 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
17289 if (IF_STMT_CONSTEXPR_P (t))
17290 args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args);
17291 tmp = RECUR (IF_COND (t));
17292 tmp = finish_if_stmt_cond (tmp, stmt);
17293 if (IF_STMT_CONSTEXPR_P (t)
17294 && instantiation_dependent_expression_p (tmp))
17295 {
17296 /* We're partially instantiating a generic lambda, but the condition
17297 of the constexpr if is still dependent. Don't substitute into the
17298 branches now, just remember the template arguments. */
17299 do_poplevel (IF_SCOPE (stmt));
17300 IF_COND (stmt) = IF_COND (t);
17301 THEN_CLAUSE (stmt) = THEN_CLAUSE (t);
17302 ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t);
17303 IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (t, args, complain);
17304 add_stmt (stmt);
17305 break;
17306 }
17307 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
17308 /* Don't instantiate the THEN_CLAUSE. */;
17309 else
17310 {
17311 tree folded = fold_non_dependent_expr (tmp, complain);
17312 bool inhibit = integer_zerop (folded);
17313 if (inhibit)
17314 ++c_inhibit_evaluation_warnings;
17315 RECUR (THEN_CLAUSE (t));
17316 if (inhibit)
17317 --c_inhibit_evaluation_warnings;
17318 }
17319 finish_then_clause (stmt);
17320
17321 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
17322 /* Don't instantiate the ELSE_CLAUSE. */;
17323 else if (ELSE_CLAUSE (t))
17324 {
17325 tree folded = fold_non_dependent_expr (tmp, complain);
17326 bool inhibit = integer_nonzerop (folded);
17327 begin_else_clause (stmt);
17328 if (inhibit)
17329 ++c_inhibit_evaluation_warnings;
17330 RECUR (ELSE_CLAUSE (t));
17331 if (inhibit)
17332 --c_inhibit_evaluation_warnings;
17333 finish_else_clause (stmt);
17334 }
17335
17336 finish_if_stmt (stmt);
17337 break;
17338
17339 case BIND_EXPR:
17340 if (BIND_EXPR_BODY_BLOCK (t))
17341 stmt = begin_function_body ();
17342 else
17343 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
17344 ? BCS_TRY_BLOCK : 0);
17345
17346 RECUR (BIND_EXPR_BODY (t));
17347
17348 if (BIND_EXPR_BODY_BLOCK (t))
17349 finish_function_body (stmt);
17350 else
17351 finish_compound_stmt (stmt);
17352 break;
17353
17354 case BREAK_STMT:
17355 finish_break_stmt ();
17356 break;
17357
17358 case CONTINUE_STMT:
17359 finish_continue_stmt ();
17360 break;
17361
17362 case SWITCH_STMT:
17363 stmt = begin_switch_stmt ();
17364 tmp = RECUR (SWITCH_STMT_COND (t));
17365 finish_switch_cond (tmp, stmt);
17366 RECUR (SWITCH_STMT_BODY (t));
17367 finish_switch_stmt (stmt);
17368 break;
17369
17370 case CASE_LABEL_EXPR:
17371 {
17372 tree decl = CASE_LABEL (t);
17373 tree low = RECUR (CASE_LOW (t));
17374 tree high = RECUR (CASE_HIGH (t));
17375 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
17376 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
17377 {
17378 tree label = CASE_LABEL (l);
17379 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
17380 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
17381 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
17382 }
17383 }
17384 break;
17385
17386 case LABEL_EXPR:
17387 {
17388 tree decl = LABEL_EXPR_LABEL (t);
17389 tree label;
17390
17391 label = finish_label_stmt (DECL_NAME (decl));
17392 if (TREE_CODE (label) == LABEL_DECL)
17393 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
17394 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
17395 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
17396 }
17397 break;
17398
17399 case GOTO_EXPR:
17400 tmp = GOTO_DESTINATION (t);
17401 if (TREE_CODE (tmp) != LABEL_DECL)
17402 /* Computed goto's must be tsubst'd into. On the other hand,
17403 non-computed gotos must not be; the identifier in question
17404 will have no binding. */
17405 tmp = RECUR (tmp);
17406 else
17407 tmp = DECL_NAME (tmp);
17408 finish_goto_stmt (tmp);
17409 break;
17410
17411 case ASM_EXPR:
17412 {
17413 tree string = RECUR (ASM_STRING (t));
17414 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
17415 complain, in_decl);
17416 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
17417 complain, in_decl);
17418 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
17419 complain, in_decl);
17420 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
17421 complain, in_decl);
17422 tmp = finish_asm_stmt (EXPR_LOCATION (t), ASM_VOLATILE_P (t), string,
17423 outputs, inputs, clobbers, labels,
17424 ASM_INLINE_P (t));
17425 tree asm_expr = tmp;
17426 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
17427 asm_expr = TREE_OPERAND (asm_expr, 0);
17428 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
17429 }
17430 break;
17431
17432 case TRY_BLOCK:
17433 if (CLEANUP_P (t))
17434 {
17435 stmt = begin_try_block ();
17436 RECUR (TRY_STMTS (t));
17437 finish_cleanup_try_block (stmt);
17438 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
17439 }
17440 else
17441 {
17442 tree compound_stmt = NULL_TREE;
17443
17444 if (FN_TRY_BLOCK_P (t))
17445 stmt = begin_function_try_block (&compound_stmt);
17446 else
17447 stmt = begin_try_block ();
17448
17449 RECUR (TRY_STMTS (t));
17450
17451 if (FN_TRY_BLOCK_P (t))
17452 finish_function_try_block (stmt);
17453 else
17454 finish_try_block (stmt);
17455
17456 RECUR (TRY_HANDLERS (t));
17457 if (FN_TRY_BLOCK_P (t))
17458 finish_function_handler_sequence (stmt, compound_stmt);
17459 else
17460 finish_handler_sequence (stmt);
17461 }
17462 break;
17463
17464 case HANDLER:
17465 {
17466 tree decl = HANDLER_PARMS (t);
17467
17468 if (decl)
17469 {
17470 decl = tsubst (decl, args, complain, in_decl);
17471 /* Prevent instantiate_decl from trying to instantiate
17472 this variable. We've already done all that needs to be
17473 done. */
17474 if (decl != error_mark_node)
17475 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
17476 }
17477 stmt = begin_handler ();
17478 finish_handler_parms (decl, stmt);
17479 RECUR (HANDLER_BODY (t));
17480 finish_handler (stmt);
17481 }
17482 break;
17483
17484 case TAG_DEFN:
17485 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
17486 if (CLASS_TYPE_P (tmp))
17487 {
17488 /* Local classes are not independent templates; they are
17489 instantiated along with their containing function. And this
17490 way we don't have to deal with pushing out of one local class
17491 to instantiate a member of another local class. */
17492 /* Closures are handled by the LAMBDA_EXPR. */
17493 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
17494 complete_type (tmp);
17495 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
17496 if ((VAR_P (fld)
17497 || (TREE_CODE (fld) == FUNCTION_DECL
17498 && !DECL_ARTIFICIAL (fld)))
17499 && DECL_TEMPLATE_INSTANTIATION (fld))
17500 instantiate_decl (fld, /*defer_ok=*/false,
17501 /*expl_inst_class=*/false);
17502 }
17503 break;
17504
17505 case STATIC_ASSERT:
17506 {
17507 tree condition;
17508
17509 ++c_inhibit_evaluation_warnings;
17510 condition =
17511 tsubst_expr (STATIC_ASSERT_CONDITION (t),
17512 args,
17513 complain, in_decl,
17514 /*integral_constant_expression_p=*/true);
17515 --c_inhibit_evaluation_warnings;
17516
17517 finish_static_assert (condition,
17518 STATIC_ASSERT_MESSAGE (t),
17519 STATIC_ASSERT_SOURCE_LOCATION (t),
17520 /*member_p=*/false);
17521 }
17522 break;
17523
17524 case OACC_KERNELS:
17525 case OACC_PARALLEL:
17526 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
17527 in_decl);
17528 stmt = begin_omp_parallel ();
17529 RECUR (OMP_BODY (t));
17530 finish_omp_construct (TREE_CODE (t), stmt, tmp);
17531 break;
17532
17533 case OMP_PARALLEL:
17534 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
17535 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
17536 complain, in_decl);
17537 if (OMP_PARALLEL_COMBINED (t))
17538 omp_parallel_combined_clauses = &tmp;
17539 stmt = begin_omp_parallel ();
17540 RECUR (OMP_PARALLEL_BODY (t));
17541 gcc_assert (omp_parallel_combined_clauses == NULL);
17542 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
17543 = OMP_PARALLEL_COMBINED (t);
17544 pop_omp_privatization_clauses (r);
17545 break;
17546
17547 case OMP_TASK:
17548 if (OMP_TASK_BODY (t) == NULL_TREE)
17549 {
17550 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
17551 complain, in_decl);
17552 t = copy_node (t);
17553 OMP_TASK_CLAUSES (t) = tmp;
17554 add_stmt (t);
17555 break;
17556 }
17557 r = push_omp_privatization_clauses (false);
17558 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
17559 complain, in_decl);
17560 stmt = begin_omp_task ();
17561 RECUR (OMP_TASK_BODY (t));
17562 finish_omp_task (tmp, stmt);
17563 pop_omp_privatization_clauses (r);
17564 break;
17565
17566 case OMP_FOR:
17567 case OMP_LOOP:
17568 case OMP_SIMD:
17569 case OMP_DISTRIBUTE:
17570 case OMP_TASKLOOP:
17571 case OACC_LOOP:
17572 {
17573 tree clauses, body, pre_body;
17574 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
17575 tree orig_declv = NULL_TREE;
17576 tree incrv = NULL_TREE;
17577 enum c_omp_region_type ort = C_ORT_OMP;
17578 bool any_range_for = false;
17579 int i;
17580
17581 if (TREE_CODE (t) == OACC_LOOP)
17582 ort = C_ORT_ACC;
17583
17584 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
17585 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
17586 in_decl);
17587 if (OMP_FOR_INIT (t) != NULL_TREE)
17588 {
17589 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17590 if (OMP_FOR_ORIG_DECLS (t))
17591 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17592 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17593 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17594 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17595 }
17596
17597 keep_next_level (true);
17598 stmt = begin_omp_structured_block ();
17599
17600 pre_body = push_stmt_list ();
17601 RECUR (OMP_FOR_PRE_BODY (t));
17602 pre_body = pop_stmt_list (pre_body);
17603
17604 if (OMP_FOR_INIT (t) != NULL_TREE)
17605 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
17606 any_range_for
17607 |= tsubst_omp_for_iterator (t, i, declv, orig_declv, initv,
17608 condv, incrv, &clauses, args,
17609 complain, in_decl,
17610 integral_constant_expression_p);
17611 omp_parallel_combined_clauses = NULL;
17612
17613 if (any_range_for)
17614 {
17615 gcc_assert (orig_declv);
17616 body = begin_omp_structured_block ();
17617 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
17618 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i)
17619 && TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST
17620 && TREE_CHAIN (TREE_VEC_ELT (orig_declv, i)))
17621 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
17622 TREE_VEC_ELT (declv, i));
17623 }
17624 else
17625 body = push_stmt_list ();
17626 RECUR (OMP_FOR_BODY (t));
17627 if (any_range_for)
17628 body = finish_omp_structured_block (body);
17629 else
17630 body = pop_stmt_list (body);
17631
17632 if (OMP_FOR_INIT (t) != NULL_TREE)
17633 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
17634 orig_declv, initv, condv, incrv, body, pre_body,
17635 NULL, clauses);
17636 else
17637 {
17638 t = make_node (TREE_CODE (t));
17639 TREE_TYPE (t) = void_type_node;
17640 OMP_FOR_BODY (t) = body;
17641 OMP_FOR_PRE_BODY (t) = pre_body;
17642 OMP_FOR_CLAUSES (t) = clauses;
17643 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
17644 add_stmt (t);
17645 }
17646
17647 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt),
17648 t));
17649 pop_omp_privatization_clauses (r);
17650 }
17651 break;
17652
17653 case OMP_SECTIONS:
17654 omp_parallel_combined_clauses = NULL;
17655 /* FALLTHRU */
17656 case OMP_SINGLE:
17657 case OMP_TEAMS:
17658 case OMP_CRITICAL:
17659 case OMP_TASKGROUP:
17660 case OMP_SCAN:
17661 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
17662 && OMP_TEAMS_COMBINED (t));
17663 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
17664 in_decl);
17665 if (TREE_CODE (t) == OMP_TEAMS)
17666 {
17667 keep_next_level (true);
17668 stmt = begin_omp_structured_block ();
17669 RECUR (OMP_BODY (t));
17670 stmt = finish_omp_structured_block (stmt);
17671 }
17672 else
17673 {
17674 stmt = push_stmt_list ();
17675 RECUR (OMP_BODY (t));
17676 stmt = pop_stmt_list (stmt);
17677 }
17678
17679 t = copy_node (t);
17680 OMP_BODY (t) = stmt;
17681 OMP_CLAUSES (t) = tmp;
17682 add_stmt (t);
17683 pop_omp_privatization_clauses (r);
17684 break;
17685
17686 case OMP_DEPOBJ:
17687 r = RECUR (OMP_DEPOBJ_DEPOBJ (t));
17688 if (OMP_DEPOBJ_CLAUSES (t) && OMP_DEPOBJ_CLAUSES (t) != error_mark_node)
17689 {
17690 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
17691 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t)) == OMP_CLAUSE)
17692 {
17693 tmp = tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t), C_ORT_OMP,
17694 args, complain, in_decl);
17695 if (tmp == NULL_TREE)
17696 tmp = error_mark_node;
17697 }
17698 else
17699 {
17700 kind = (enum omp_clause_depend_kind)
17701 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t));
17702 tmp = NULL_TREE;
17703 }
17704 finish_omp_depobj (EXPR_LOCATION (t), r, kind, tmp);
17705 }
17706 else
17707 finish_omp_depobj (EXPR_LOCATION (t), r,
17708 OMP_CLAUSE_DEPEND_SOURCE,
17709 OMP_DEPOBJ_CLAUSES (t));
17710 break;
17711
17712 case OACC_DATA:
17713 case OMP_TARGET_DATA:
17714 case OMP_TARGET:
17715 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
17716 ? C_ORT_ACC : C_ORT_OMP, args, complain,
17717 in_decl);
17718 keep_next_level (true);
17719 stmt = begin_omp_structured_block ();
17720
17721 RECUR (OMP_BODY (t));
17722 stmt = finish_omp_structured_block (stmt);
17723
17724 t = copy_node (t);
17725 OMP_BODY (t) = stmt;
17726 OMP_CLAUSES (t) = tmp;
17727 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
17728 {
17729 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
17730 if (teams)
17731 {
17732 /* For combined target teams, ensure the num_teams and
17733 thread_limit clause expressions are evaluated on the host,
17734 before entering the target construct. */
17735 tree c;
17736 for (c = OMP_TEAMS_CLAUSES (teams);
17737 c; c = OMP_CLAUSE_CHAIN (c))
17738 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
17739 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
17740 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
17741 {
17742 tree expr = OMP_CLAUSE_OPERAND (c, 0);
17743 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
17744 if (expr == error_mark_node)
17745 continue;
17746 tmp = TARGET_EXPR_SLOT (expr);
17747 add_stmt (expr);
17748 OMP_CLAUSE_OPERAND (c, 0) = expr;
17749 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
17750 OMP_CLAUSE_FIRSTPRIVATE);
17751 OMP_CLAUSE_DECL (tc) = tmp;
17752 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
17753 OMP_TARGET_CLAUSES (t) = tc;
17754 }
17755 }
17756 }
17757 add_stmt (t);
17758 break;
17759
17760 case OACC_DECLARE:
17761 t = copy_node (t);
17762 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
17763 complain, in_decl);
17764 OACC_DECLARE_CLAUSES (t) = tmp;
17765 add_stmt (t);
17766 break;
17767
17768 case OMP_TARGET_UPDATE:
17769 case OMP_TARGET_ENTER_DATA:
17770 case OMP_TARGET_EXIT_DATA:
17771 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
17772 complain, in_decl);
17773 t = copy_node (t);
17774 OMP_STANDALONE_CLAUSES (t) = tmp;
17775 add_stmt (t);
17776 break;
17777
17778 case OACC_ENTER_DATA:
17779 case OACC_EXIT_DATA:
17780 case OACC_UPDATE:
17781 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
17782 complain, in_decl);
17783 t = copy_node (t);
17784 OMP_STANDALONE_CLAUSES (t) = tmp;
17785 add_stmt (t);
17786 break;
17787
17788 case OMP_ORDERED:
17789 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
17790 complain, in_decl);
17791 stmt = push_stmt_list ();
17792 RECUR (OMP_BODY (t));
17793 stmt = pop_stmt_list (stmt);
17794
17795 t = copy_node (t);
17796 OMP_BODY (t) = stmt;
17797 OMP_ORDERED_CLAUSES (t) = tmp;
17798 add_stmt (t);
17799 break;
17800
17801 case OMP_SECTION:
17802 case OMP_MASTER:
17803 stmt = push_stmt_list ();
17804 RECUR (OMP_BODY (t));
17805 stmt = pop_stmt_list (stmt);
17806
17807 t = copy_node (t);
17808 OMP_BODY (t) = stmt;
17809 add_stmt (t);
17810 break;
17811
17812 case OMP_ATOMIC:
17813 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
17814 tmp = NULL_TREE;
17815 if (TREE_CODE (TREE_OPERAND (t, 0)) == OMP_CLAUSE)
17816 tmp = tsubst_omp_clauses (TREE_OPERAND (t, 0), C_ORT_OMP, args,
17817 complain, in_decl);
17818 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
17819 {
17820 tree op1 = TREE_OPERAND (t, 1);
17821 tree rhs1 = NULL_TREE;
17822 tree lhs, rhs;
17823 if (TREE_CODE (op1) == COMPOUND_EXPR)
17824 {
17825 rhs1 = RECUR (TREE_OPERAND (op1, 0));
17826 op1 = TREE_OPERAND (op1, 1);
17827 }
17828 lhs = RECUR (TREE_OPERAND (op1, 0));
17829 rhs = RECUR (TREE_OPERAND (op1, 1));
17830 finish_omp_atomic (EXPR_LOCATION (t), OMP_ATOMIC, TREE_CODE (op1),
17831 lhs, rhs, NULL_TREE, NULL_TREE, rhs1, tmp,
17832 OMP_ATOMIC_MEMORY_ORDER (t));
17833 }
17834 else
17835 {
17836 tree op1 = TREE_OPERAND (t, 1);
17837 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
17838 tree rhs1 = NULL_TREE;
17839 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
17840 enum tree_code opcode = NOP_EXPR;
17841 if (code == OMP_ATOMIC_READ)
17842 {
17843 v = RECUR (TREE_OPERAND (op1, 0));
17844 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
17845 }
17846 else if (code == OMP_ATOMIC_CAPTURE_OLD
17847 || code == OMP_ATOMIC_CAPTURE_NEW)
17848 {
17849 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
17850 v = RECUR (TREE_OPERAND (op1, 0));
17851 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
17852 if (TREE_CODE (op11) == COMPOUND_EXPR)
17853 {
17854 rhs1 = RECUR (TREE_OPERAND (op11, 0));
17855 op11 = TREE_OPERAND (op11, 1);
17856 }
17857 lhs = RECUR (TREE_OPERAND (op11, 0));
17858 rhs = RECUR (TREE_OPERAND (op11, 1));
17859 opcode = TREE_CODE (op11);
17860 if (opcode == MODIFY_EXPR)
17861 opcode = NOP_EXPR;
17862 }
17863 else
17864 {
17865 code = OMP_ATOMIC;
17866 lhs = RECUR (TREE_OPERAND (op1, 0));
17867 rhs = RECUR (TREE_OPERAND (op1, 1));
17868 }
17869 finish_omp_atomic (EXPR_LOCATION (t), code, opcode, lhs, rhs, v,
17870 lhs1, rhs1, tmp, OMP_ATOMIC_MEMORY_ORDER (t));
17871 }
17872 break;
17873
17874 case TRANSACTION_EXPR:
17875 {
17876 int flags = 0;
17877 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
17878 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
17879
17880 if (TRANSACTION_EXPR_IS_STMT (t))
17881 {
17882 tree body = TRANSACTION_EXPR_BODY (t);
17883 tree noex = NULL_TREE;
17884 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
17885 {
17886 noex = MUST_NOT_THROW_COND (body);
17887 if (noex == NULL_TREE)
17888 noex = boolean_true_node;
17889 body = TREE_OPERAND (body, 0);
17890 }
17891 stmt = begin_transaction_stmt (input_location, NULL, flags);
17892 RECUR (body);
17893 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
17894 }
17895 else
17896 {
17897 stmt = build_transaction_expr (EXPR_LOCATION (t),
17898 RECUR (TRANSACTION_EXPR_BODY (t)),
17899 flags, NULL_TREE);
17900 RETURN (stmt);
17901 }
17902 }
17903 break;
17904
17905 case MUST_NOT_THROW_EXPR:
17906 {
17907 tree op0 = RECUR (TREE_OPERAND (t, 0));
17908 tree cond = RECUR (MUST_NOT_THROW_COND (t));
17909 RETURN (build_must_not_throw_expr (op0, cond));
17910 }
17911
17912 case EXPR_PACK_EXPANSION:
17913 error ("invalid use of pack expansion expression");
17914 RETURN (error_mark_node);
17915
17916 case NONTYPE_ARGUMENT_PACK:
17917 error ("use %<...%> to expand argument pack");
17918 RETURN (error_mark_node);
17919
17920 case COMPOUND_EXPR:
17921 tmp = RECUR (TREE_OPERAND (t, 0));
17922 if (tmp == NULL_TREE)
17923 /* If the first operand was a statement, we're done with it. */
17924 RETURN (RECUR (TREE_OPERAND (t, 1)));
17925 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
17926 RECUR (TREE_OPERAND (t, 1)),
17927 complain));
17928
17929 case ANNOTATE_EXPR:
17930 tmp = RECUR (TREE_OPERAND (t, 0));
17931 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
17932 TREE_TYPE (tmp), tmp,
17933 RECUR (TREE_OPERAND (t, 1)),
17934 RECUR (TREE_OPERAND (t, 2))));
17935
17936 case PREDICT_EXPR:
17937 RETURN (add_stmt (copy_node (t)));
17938
17939 default:
17940 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
17941
17942 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
17943 /*function_p=*/false,
17944 integral_constant_expression_p));
17945 }
17946
17947 RETURN (NULL_TREE);
17948 out:
17949 input_location = loc;
17950 return r;
17951 #undef RECUR
17952 #undef RETURN
17953 }
17954
17955 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
17956 function. For description of the body see comment above
17957 cp_parser_omp_declare_reduction_exprs. */
17958
17959 static void
17960 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
17961 {
17962 if (t == NULL_TREE || t == error_mark_node)
17963 return;
17964
17965 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
17966
17967 tree_stmt_iterator tsi;
17968 int i;
17969 tree stmts[7];
17970 memset (stmts, 0, sizeof stmts);
17971 for (i = 0, tsi = tsi_start (t);
17972 i < 7 && !tsi_end_p (tsi);
17973 i++, tsi_next (&tsi))
17974 stmts[i] = tsi_stmt (tsi);
17975 gcc_assert (tsi_end_p (tsi));
17976
17977 if (i >= 3)
17978 {
17979 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
17980 && TREE_CODE (stmts[1]) == DECL_EXPR);
17981 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
17982 args, complain, in_decl);
17983 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
17984 args, complain, in_decl);
17985 DECL_CONTEXT (omp_out) = current_function_decl;
17986 DECL_CONTEXT (omp_in) = current_function_decl;
17987 keep_next_level (true);
17988 tree block = begin_omp_structured_block ();
17989 tsubst_expr (stmts[2], args, complain, in_decl, false);
17990 block = finish_omp_structured_block (block);
17991 block = maybe_cleanup_point_expr_void (block);
17992 add_decl_expr (omp_out);
17993 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
17994 TREE_NO_WARNING (omp_out) = 1;
17995 add_decl_expr (omp_in);
17996 finish_expr_stmt (block);
17997 }
17998 if (i >= 6)
17999 {
18000 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
18001 && TREE_CODE (stmts[4]) == DECL_EXPR);
18002 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
18003 args, complain, in_decl);
18004 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
18005 args, complain, in_decl);
18006 DECL_CONTEXT (omp_priv) = current_function_decl;
18007 DECL_CONTEXT (omp_orig) = current_function_decl;
18008 keep_next_level (true);
18009 tree block = begin_omp_structured_block ();
18010 tsubst_expr (stmts[5], args, complain, in_decl, false);
18011 block = finish_omp_structured_block (block);
18012 block = maybe_cleanup_point_expr_void (block);
18013 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
18014 add_decl_expr (omp_priv);
18015 add_decl_expr (omp_orig);
18016 finish_expr_stmt (block);
18017 if (i == 7)
18018 add_decl_expr (omp_orig);
18019 }
18020 }
18021
18022 /* T is a postfix-expression that is not being used in a function
18023 call. Return the substituted version of T. */
18024
18025 static tree
18026 tsubst_non_call_postfix_expression (tree t, tree args,
18027 tsubst_flags_t complain,
18028 tree in_decl)
18029 {
18030 if (TREE_CODE (t) == SCOPE_REF)
18031 t = tsubst_qualified_id (t, args, complain, in_decl,
18032 /*done=*/false, /*address_p=*/false);
18033 else
18034 t = tsubst_copy_and_build (t, args, complain, in_decl,
18035 /*function_p=*/false,
18036 /*integral_constant_expression_p=*/false);
18037
18038 return t;
18039 }
18040
18041 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
18042 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
18043 dependent init-capture. */
18044
18045 static void
18046 prepend_one_capture (tree field, tree init, tree &list,
18047 tsubst_flags_t complain)
18048 {
18049 if (tree auto_node = type_uses_auto (TREE_TYPE (field)))
18050 {
18051 tree type = NULL_TREE;
18052 if (!init)
18053 {
18054 if (complain & tf_error)
18055 error ("empty initializer in lambda init-capture");
18056 init = error_mark_node;
18057 }
18058 else if (TREE_CODE (init) == TREE_LIST)
18059 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
18060 if (!type)
18061 type = do_auto_deduction (TREE_TYPE (field), init, auto_node, complain);
18062 TREE_TYPE (field) = type;
18063 cp_apply_type_quals_to_decl (cp_type_quals (type), field);
18064 }
18065 list = tree_cons (field, init, list);
18066 }
18067
18068 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
18069 instantiation context. Instantiating a pack expansion containing a lambda
18070 might result in multiple lambdas all based on the same lambda in the
18071 template. */
18072
18073 tree
18074 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
18075 {
18076 tree oldfn = lambda_function (t);
18077 in_decl = oldfn;
18078
18079 tree r = build_lambda_expr ();
18080
18081 LAMBDA_EXPR_LOCATION (r)
18082 = LAMBDA_EXPR_LOCATION (t);
18083 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
18084 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
18085 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
18086 LAMBDA_EXPR_INSTANTIATED (r) = true;
18087
18088 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
18089 /* A lambda in a default argument outside a class gets no
18090 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
18091 tsubst_default_argument calls start_lambda_scope, so we need to
18092 specifically ignore it here, and use the global scope. */
18093 record_null_lambda_scope (r);
18094 else
18095 record_lambda_scope (r);
18096
18097 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
18098 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
18099
18100 vec<tree,va_gc>* field_packs = NULL;
18101
18102 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
18103 cap = TREE_CHAIN (cap))
18104 {
18105 tree ofield = TREE_PURPOSE (cap);
18106 if (PACK_EXPANSION_P (ofield))
18107 ofield = PACK_EXPANSION_PATTERN (ofield);
18108 tree field = tsubst_decl (ofield, args, complain);
18109
18110 if (DECL_PACK_P (ofield) && !DECL_NORMAL_CAPTURE_P (ofield))
18111 {
18112 /* Remember these for when we've pushed local_specializations. */
18113 vec_safe_push (field_packs, ofield);
18114 vec_safe_push (field_packs, field);
18115 }
18116
18117 if (field == error_mark_node)
18118 return error_mark_node;
18119
18120 tree init = TREE_VALUE (cap);
18121 if (PACK_EXPANSION_P (init))
18122 init = tsubst_pack_expansion (init, args, complain, in_decl);
18123 else
18124 init = tsubst_copy_and_build (init, args, complain, in_decl,
18125 /*fn*/false, /*constexpr*/false);
18126
18127 if (TREE_CODE (field) == TREE_VEC)
18128 {
18129 int len = TREE_VEC_LENGTH (field);
18130 gcc_assert (TREE_CODE (init) == TREE_VEC
18131 && TREE_VEC_LENGTH (init) == len);
18132 for (int i = 0; i < len; ++i)
18133 prepend_one_capture (TREE_VEC_ELT (field, i),
18134 TREE_VEC_ELT (init, i),
18135 LAMBDA_EXPR_CAPTURE_LIST (r),
18136 complain);
18137 }
18138 else
18139 {
18140 prepend_one_capture (field, init, LAMBDA_EXPR_CAPTURE_LIST (r),
18141 complain);
18142
18143 if (id_equal (DECL_NAME (field), "__this"))
18144 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
18145 }
18146 }
18147
18148 tree type = begin_lambda_type (r);
18149 if (type == error_mark_node)
18150 return error_mark_node;
18151
18152 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
18153 determine_visibility (TYPE_NAME (type));
18154
18155 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
18156
18157 tree oldtmpl = (generic_lambda_fn_p (oldfn)
18158 ? DECL_TI_TEMPLATE (oldfn)
18159 : NULL_TREE);
18160
18161 tree fntype = static_fn_type (oldfn);
18162 if (oldtmpl)
18163 ++processing_template_decl;
18164 fntype = tsubst (fntype, args, complain, in_decl);
18165 if (oldtmpl)
18166 --processing_template_decl;
18167
18168 if (fntype == error_mark_node)
18169 r = error_mark_node;
18170 else
18171 {
18172 /* The body of a lambda-expression is not a subexpression of the
18173 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
18174 which would be skipped if cp_unevaluated_operand. */
18175 cp_evaluated ev;
18176
18177 /* Fix the type of 'this'. */
18178 fntype = build_memfn_type (fntype, type,
18179 type_memfn_quals (fntype),
18180 type_memfn_rqual (fntype));
18181 tree fn, tmpl;
18182 if (oldtmpl)
18183 {
18184 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
18185 fn = DECL_TEMPLATE_RESULT (tmpl);
18186 finish_member_declaration (tmpl);
18187 }
18188 else
18189 {
18190 tmpl = NULL_TREE;
18191 fn = tsubst_function_decl (oldfn, args, complain, fntype);
18192 finish_member_declaration (fn);
18193 }
18194
18195 /* Let finish_function set this. */
18196 DECL_DECLARED_CONSTEXPR_P (fn) = false;
18197
18198 bool nested = cfun;
18199 if (nested)
18200 push_function_context ();
18201 else
18202 /* Still increment function_depth so that we don't GC in the
18203 middle of an expression. */
18204 ++function_depth;
18205
18206 local_specialization_stack s (lss_copy);
18207
18208 tree body = start_lambda_function (fn, r);
18209
18210 /* Now record them for lookup_init_capture_pack. */
18211 int fplen = vec_safe_length (field_packs);
18212 for (int i = 0; i < fplen; )
18213 {
18214 tree pack = (*field_packs)[i++];
18215 tree inst = (*field_packs)[i++];
18216 register_local_specialization (inst, pack);
18217 }
18218 release_tree_vector (field_packs);
18219
18220 register_parameter_specializations (oldfn, fn);
18221
18222 if (oldtmpl)
18223 {
18224 /* We might not partially instantiate some parts of the function, so
18225 copy these flags from the original template. */
18226 language_function *ol = DECL_STRUCT_FUNCTION (oldfn)->language;
18227 current_function_returns_value = ol->returns_value;
18228 current_function_returns_null = ol->returns_null;
18229 current_function_returns_abnormally = ol->returns_abnormally;
18230 current_function_infinite_loop = ol->infinite_loop;
18231 }
18232
18233 /* [temp.deduct] A lambda-expression appearing in a function type or a
18234 template parameter is not considered part of the immediate context for
18235 the purposes of template argument deduction. */
18236 complain = tf_warning_or_error;
18237
18238 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
18239 /*constexpr*/false);
18240
18241 finish_lambda_function (body);
18242
18243 if (nested)
18244 pop_function_context ();
18245 else
18246 --function_depth;
18247
18248 /* The capture list was built up in reverse order; fix that now. */
18249 LAMBDA_EXPR_CAPTURE_LIST (r)
18250 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
18251
18252 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
18253
18254 maybe_add_lambda_conv_op (type);
18255 }
18256
18257 finish_struct (type, /*attr*/NULL_TREE);
18258
18259 insert_pending_capture_proxies ();
18260
18261 return r;
18262 }
18263
18264 /* Like tsubst but deals with expressions and performs semantic
18265 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
18266
18267 tree
18268 tsubst_copy_and_build (tree t,
18269 tree args,
18270 tsubst_flags_t complain,
18271 tree in_decl,
18272 bool function_p,
18273 bool integral_constant_expression_p)
18274 {
18275 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
18276 #define RECUR(NODE) \
18277 tsubst_copy_and_build (NODE, args, complain, in_decl, \
18278 /*function_p=*/false, \
18279 integral_constant_expression_p)
18280
18281 tree retval, op1;
18282 location_t loc;
18283
18284 if (t == NULL_TREE || t == error_mark_node)
18285 return t;
18286
18287 loc = input_location;
18288 if (location_t eloc = cp_expr_location (t))
18289 input_location = eloc;
18290
18291 /* N3276 decltype magic only applies to calls at the top level or on the
18292 right side of a comma. */
18293 tsubst_flags_t decltype_flag = (complain & tf_decltype);
18294 complain &= ~tf_decltype;
18295
18296 switch (TREE_CODE (t))
18297 {
18298 case USING_DECL:
18299 t = DECL_NAME (t);
18300 /* Fall through. */
18301 case IDENTIFIER_NODE:
18302 {
18303 tree decl;
18304 cp_id_kind idk;
18305 bool non_integral_constant_expression_p;
18306 const char *error_msg;
18307
18308 if (IDENTIFIER_CONV_OP_P (t))
18309 {
18310 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18311 t = make_conv_op_name (new_type);
18312 }
18313
18314 /* Look up the name. */
18315 decl = lookup_name (t);
18316
18317 /* By convention, expressions use ERROR_MARK_NODE to indicate
18318 failure, not NULL_TREE. */
18319 if (decl == NULL_TREE)
18320 decl = error_mark_node;
18321
18322 decl = finish_id_expression (t, decl, NULL_TREE,
18323 &idk,
18324 integral_constant_expression_p,
18325 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
18326 &non_integral_constant_expression_p,
18327 /*template_p=*/false,
18328 /*done=*/true,
18329 /*address_p=*/false,
18330 /*template_arg_p=*/false,
18331 &error_msg,
18332 input_location);
18333 if (error_msg)
18334 error (error_msg);
18335 if (!function_p && identifier_p (decl))
18336 {
18337 if (complain & tf_error)
18338 unqualified_name_lookup_error (decl);
18339 decl = error_mark_node;
18340 }
18341 RETURN (decl);
18342 }
18343
18344 case TEMPLATE_ID_EXPR:
18345 {
18346 tree object;
18347 tree templ = RECUR (TREE_OPERAND (t, 0));
18348 tree targs = TREE_OPERAND (t, 1);
18349
18350 if (targs)
18351 targs = tsubst_template_args (targs, args, complain, in_decl);
18352 if (targs == error_mark_node)
18353 RETURN (error_mark_node);
18354
18355 if (TREE_CODE (templ) == SCOPE_REF)
18356 {
18357 tree name = TREE_OPERAND (templ, 1);
18358 tree tid = lookup_template_function (name, targs);
18359 TREE_OPERAND (templ, 1) = tid;
18360 RETURN (templ);
18361 }
18362
18363 if (variable_template_p (templ))
18364 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
18365
18366 if (TREE_CODE (templ) == COMPONENT_REF)
18367 {
18368 object = TREE_OPERAND (templ, 0);
18369 templ = TREE_OPERAND (templ, 1);
18370 }
18371 else
18372 object = NULL_TREE;
18373 templ = lookup_template_function (templ, targs);
18374
18375 if (object)
18376 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
18377 object, templ, NULL_TREE));
18378 else
18379 RETURN (baselink_for_fns (templ));
18380 }
18381
18382 case INDIRECT_REF:
18383 {
18384 tree r = RECUR (TREE_OPERAND (t, 0));
18385
18386 if (REFERENCE_REF_P (t))
18387 {
18388 /* A type conversion to reference type will be enclosed in
18389 such an indirect ref, but the substitution of the cast
18390 will have also added such an indirect ref. */
18391 r = convert_from_reference (r);
18392 }
18393 else
18394 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
18395 complain|decltype_flag);
18396
18397 if (REF_PARENTHESIZED_P (t))
18398 r = force_paren_expr (r);
18399
18400 RETURN (r);
18401 }
18402
18403 case NOP_EXPR:
18404 {
18405 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18406 tree op0 = RECUR (TREE_OPERAND (t, 0));
18407 RETURN (build_nop (type, op0));
18408 }
18409
18410 case IMPLICIT_CONV_EXPR:
18411 {
18412 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18413 tree expr = RECUR (TREE_OPERAND (t, 0));
18414 if (dependent_type_p (type) || type_dependent_expression_p (expr))
18415 {
18416 retval = copy_node (t);
18417 TREE_TYPE (retval) = type;
18418 TREE_OPERAND (retval, 0) = expr;
18419 RETURN (retval);
18420 }
18421 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
18422 /* We'll pass this to convert_nontype_argument again, we don't need
18423 to actually perform any conversion here. */
18424 RETURN (expr);
18425 int flags = LOOKUP_IMPLICIT;
18426 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
18427 flags = LOOKUP_NORMAL;
18428 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t))
18429 flags |= LOOKUP_NO_NARROWING;
18430 RETURN (perform_implicit_conversion_flags (type, expr, complain,
18431 flags));
18432 }
18433
18434 case CONVERT_EXPR:
18435 {
18436 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18437 tree op0 = RECUR (TREE_OPERAND (t, 0));
18438 if (op0 == error_mark_node)
18439 RETURN (error_mark_node);
18440 RETURN (build1 (CONVERT_EXPR, type, op0));
18441 }
18442
18443 case CAST_EXPR:
18444 case REINTERPRET_CAST_EXPR:
18445 case CONST_CAST_EXPR:
18446 case DYNAMIC_CAST_EXPR:
18447 case STATIC_CAST_EXPR:
18448 {
18449 tree type;
18450 tree op, r = NULL_TREE;
18451
18452 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18453 if (integral_constant_expression_p
18454 && !cast_valid_in_integral_constant_expression_p (type))
18455 {
18456 if (complain & tf_error)
18457 error ("a cast to a type other than an integral or "
18458 "enumeration type cannot appear in a constant-expression");
18459 RETURN (error_mark_node);
18460 }
18461
18462 op = RECUR (TREE_OPERAND (t, 0));
18463
18464 warning_sentinel s(warn_useless_cast);
18465 warning_sentinel s2(warn_ignored_qualifiers);
18466 switch (TREE_CODE (t))
18467 {
18468 case CAST_EXPR:
18469 r = build_functional_cast (type, op, complain);
18470 break;
18471 case REINTERPRET_CAST_EXPR:
18472 r = build_reinterpret_cast (type, op, complain);
18473 break;
18474 case CONST_CAST_EXPR:
18475 r = build_const_cast (type, op, complain);
18476 break;
18477 case DYNAMIC_CAST_EXPR:
18478 r = build_dynamic_cast (type, op, complain);
18479 break;
18480 case STATIC_CAST_EXPR:
18481 r = build_static_cast (type, op, complain);
18482 break;
18483 default:
18484 gcc_unreachable ();
18485 }
18486
18487 RETURN (r);
18488 }
18489
18490 case POSTDECREMENT_EXPR:
18491 case POSTINCREMENT_EXPR:
18492 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18493 args, complain, in_decl);
18494 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
18495 complain|decltype_flag));
18496
18497 case PREDECREMENT_EXPR:
18498 case PREINCREMENT_EXPR:
18499 case NEGATE_EXPR:
18500 case BIT_NOT_EXPR:
18501 case ABS_EXPR:
18502 case TRUTH_NOT_EXPR:
18503 case UNARY_PLUS_EXPR: /* Unary + */
18504 case REALPART_EXPR:
18505 case IMAGPART_EXPR:
18506 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
18507 RECUR (TREE_OPERAND (t, 0)),
18508 complain|decltype_flag));
18509
18510 case FIX_TRUNC_EXPR:
18511 gcc_unreachable ();
18512
18513 case ADDR_EXPR:
18514 op1 = TREE_OPERAND (t, 0);
18515 if (TREE_CODE (op1) == LABEL_DECL)
18516 RETURN (finish_label_address_expr (DECL_NAME (op1),
18517 EXPR_LOCATION (op1)));
18518 if (TREE_CODE (op1) == SCOPE_REF)
18519 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
18520 /*done=*/true, /*address_p=*/true);
18521 else
18522 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
18523 in_decl);
18524 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
18525 complain|decltype_flag));
18526
18527 case PLUS_EXPR:
18528 case MINUS_EXPR:
18529 case MULT_EXPR:
18530 case TRUNC_DIV_EXPR:
18531 case CEIL_DIV_EXPR:
18532 case FLOOR_DIV_EXPR:
18533 case ROUND_DIV_EXPR:
18534 case EXACT_DIV_EXPR:
18535 case BIT_AND_EXPR:
18536 case BIT_IOR_EXPR:
18537 case BIT_XOR_EXPR:
18538 case TRUNC_MOD_EXPR:
18539 case FLOOR_MOD_EXPR:
18540 case TRUTH_ANDIF_EXPR:
18541 case TRUTH_ORIF_EXPR:
18542 case TRUTH_AND_EXPR:
18543 case TRUTH_OR_EXPR:
18544 case RSHIFT_EXPR:
18545 case LSHIFT_EXPR:
18546 case RROTATE_EXPR:
18547 case LROTATE_EXPR:
18548 case EQ_EXPR:
18549 case NE_EXPR:
18550 case MAX_EXPR:
18551 case MIN_EXPR:
18552 case LE_EXPR:
18553 case GE_EXPR:
18554 case LT_EXPR:
18555 case GT_EXPR:
18556 case MEMBER_REF:
18557 case DOTSTAR_EXPR:
18558 {
18559 warning_sentinel s1(warn_type_limits);
18560 warning_sentinel s2(warn_div_by_zero);
18561 warning_sentinel s3(warn_logical_op);
18562 warning_sentinel s4(warn_tautological_compare);
18563 tree op0 = RECUR (TREE_OPERAND (t, 0));
18564 tree op1 = RECUR (TREE_OPERAND (t, 1));
18565 tree r = build_x_binary_op
18566 (input_location, TREE_CODE (t),
18567 op0,
18568 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
18569 ? ERROR_MARK
18570 : TREE_CODE (TREE_OPERAND (t, 0))),
18571 op1,
18572 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
18573 ? ERROR_MARK
18574 : TREE_CODE (TREE_OPERAND (t, 1))),
18575 /*overload=*/NULL,
18576 complain|decltype_flag);
18577 if (EXPR_P (r) && TREE_NO_WARNING (t))
18578 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
18579
18580 RETURN (r);
18581 }
18582
18583 case POINTER_PLUS_EXPR:
18584 {
18585 tree op0 = RECUR (TREE_OPERAND (t, 0));
18586 tree op1 = RECUR (TREE_OPERAND (t, 1));
18587 RETURN (fold_build_pointer_plus (op0, op1));
18588 }
18589
18590 case SCOPE_REF:
18591 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
18592 /*address_p=*/false));
18593 case ARRAY_REF:
18594 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18595 args, complain, in_decl);
18596 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
18597 RECUR (TREE_OPERAND (t, 1)),
18598 complain|decltype_flag));
18599
18600 case SIZEOF_EXPR:
18601 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
18602 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
18603 RETURN (tsubst_copy (t, args, complain, in_decl));
18604 /* Fall through */
18605
18606 case ALIGNOF_EXPR:
18607 {
18608 tree r;
18609
18610 op1 = TREE_OPERAND (t, 0);
18611 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
18612 op1 = TREE_TYPE (op1);
18613 bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR
18614 && ALIGNOF_EXPR_STD_P (t));
18615 if (!args)
18616 {
18617 /* When there are no ARGS, we are trying to evaluate a
18618 non-dependent expression from the parser. Trying to do
18619 the substitutions may not work. */
18620 if (!TYPE_P (op1))
18621 op1 = TREE_TYPE (op1);
18622 }
18623 else
18624 {
18625 ++cp_unevaluated_operand;
18626 ++c_inhibit_evaluation_warnings;
18627 if (TYPE_P (op1))
18628 op1 = tsubst (op1, args, complain, in_decl);
18629 else
18630 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
18631 /*function_p=*/false,
18632 /*integral_constant_expression_p=*/
18633 false);
18634 --cp_unevaluated_operand;
18635 --c_inhibit_evaluation_warnings;
18636 }
18637 if (TYPE_P (op1))
18638 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t), std_alignof,
18639 complain & tf_error);
18640 else
18641 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
18642 complain & tf_error);
18643 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
18644 {
18645 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
18646 {
18647 if (!processing_template_decl && TYPE_P (op1))
18648 {
18649 r = build_min (SIZEOF_EXPR, size_type_node,
18650 build1 (NOP_EXPR, op1, error_mark_node));
18651 SIZEOF_EXPR_TYPE_P (r) = 1;
18652 }
18653 else
18654 r = build_min (SIZEOF_EXPR, size_type_node, op1);
18655 TREE_SIDE_EFFECTS (r) = 0;
18656 TREE_READONLY (r) = 1;
18657 }
18658 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
18659 }
18660 RETURN (r);
18661 }
18662
18663 case AT_ENCODE_EXPR:
18664 {
18665 op1 = TREE_OPERAND (t, 0);
18666 ++cp_unevaluated_operand;
18667 ++c_inhibit_evaluation_warnings;
18668 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
18669 /*function_p=*/false,
18670 /*integral_constant_expression_p=*/false);
18671 --cp_unevaluated_operand;
18672 --c_inhibit_evaluation_warnings;
18673 RETURN (objc_build_encode_expr (op1));
18674 }
18675
18676 case NOEXCEPT_EXPR:
18677 op1 = TREE_OPERAND (t, 0);
18678 ++cp_unevaluated_operand;
18679 ++c_inhibit_evaluation_warnings;
18680 ++cp_noexcept_operand;
18681 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
18682 /*function_p=*/false,
18683 /*integral_constant_expression_p=*/false);
18684 --cp_unevaluated_operand;
18685 --c_inhibit_evaluation_warnings;
18686 --cp_noexcept_operand;
18687 RETURN (finish_noexcept_expr (op1, complain));
18688
18689 case MODOP_EXPR:
18690 {
18691 warning_sentinel s(warn_div_by_zero);
18692 tree lhs = RECUR (TREE_OPERAND (t, 0));
18693 tree rhs = RECUR (TREE_OPERAND (t, 2));
18694 tree r = build_x_modify_expr
18695 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
18696 complain|decltype_flag);
18697 /* TREE_NO_WARNING must be set if either the expression was
18698 parenthesized or it uses an operator such as >>= rather
18699 than plain assignment. In the former case, it was already
18700 set and must be copied. In the latter case,
18701 build_x_modify_expr sets it and it must not be reset
18702 here. */
18703 if (TREE_NO_WARNING (t))
18704 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
18705
18706 RETURN (r);
18707 }
18708
18709 case ARROW_EXPR:
18710 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18711 args, complain, in_decl);
18712 /* Remember that there was a reference to this entity. */
18713 if (DECL_P (op1)
18714 && !mark_used (op1, complain) && !(complain & tf_error))
18715 RETURN (error_mark_node);
18716 RETURN (build_x_arrow (input_location, op1, complain));
18717
18718 case NEW_EXPR:
18719 {
18720 tree placement = RECUR (TREE_OPERAND (t, 0));
18721 tree init = RECUR (TREE_OPERAND (t, 3));
18722 vec<tree, va_gc> *placement_vec;
18723 vec<tree, va_gc> *init_vec;
18724 tree ret;
18725
18726 if (placement == NULL_TREE)
18727 placement_vec = NULL;
18728 else
18729 {
18730 placement_vec = make_tree_vector ();
18731 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
18732 vec_safe_push (placement_vec, TREE_VALUE (placement));
18733 }
18734
18735 /* If there was an initializer in the original tree, but it
18736 instantiated to an empty list, then we should pass a
18737 non-NULL empty vector to tell build_new that it was an
18738 empty initializer() rather than no initializer. This can
18739 only happen when the initializer is a pack expansion whose
18740 parameter packs are of length zero. */
18741 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
18742 init_vec = NULL;
18743 else
18744 {
18745 init_vec = make_tree_vector ();
18746 if (init == void_node)
18747 gcc_assert (init_vec != NULL);
18748 else
18749 {
18750 for (; init != NULL_TREE; init = TREE_CHAIN (init))
18751 vec_safe_push (init_vec, TREE_VALUE (init));
18752 }
18753 }
18754
18755 /* Avoid passing an enclosing decl to valid_array_size_p. */
18756 in_decl = NULL_TREE;
18757
18758 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
18759 tree op2 = RECUR (TREE_OPERAND (t, 2));
18760 ret = build_new (&placement_vec, op1, op2, &init_vec,
18761 NEW_EXPR_USE_GLOBAL (t),
18762 complain);
18763
18764 if (placement_vec != NULL)
18765 release_tree_vector (placement_vec);
18766 if (init_vec != NULL)
18767 release_tree_vector (init_vec);
18768
18769 RETURN (ret);
18770 }
18771
18772 case DELETE_EXPR:
18773 {
18774 tree op0 = RECUR (TREE_OPERAND (t, 0));
18775 tree op1 = RECUR (TREE_OPERAND (t, 1));
18776 RETURN (delete_sanity (op0, op1,
18777 DELETE_EXPR_USE_VEC (t),
18778 DELETE_EXPR_USE_GLOBAL (t),
18779 complain));
18780 }
18781
18782 case COMPOUND_EXPR:
18783 {
18784 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
18785 complain & ~tf_decltype, in_decl,
18786 /*function_p=*/false,
18787 integral_constant_expression_p);
18788 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
18789 op0,
18790 RECUR (TREE_OPERAND (t, 1)),
18791 complain|decltype_flag));
18792 }
18793
18794 case CALL_EXPR:
18795 {
18796 tree function;
18797 unsigned int nargs, i;
18798 bool qualified_p;
18799 bool koenig_p;
18800 tree ret;
18801
18802 function = CALL_EXPR_FN (t);
18803 /* Internal function with no arguments. */
18804 if (function == NULL_TREE && call_expr_nargs (t) == 0)
18805 RETURN (t);
18806
18807 /* When we parsed the expression, we determined whether or
18808 not Koenig lookup should be performed. */
18809 koenig_p = KOENIG_LOOKUP_P (t);
18810 if (function == NULL_TREE)
18811 {
18812 koenig_p = false;
18813 qualified_p = false;
18814 }
18815 else if (TREE_CODE (function) == SCOPE_REF)
18816 {
18817 qualified_p = true;
18818 function = tsubst_qualified_id (function, args, complain, in_decl,
18819 /*done=*/false,
18820 /*address_p=*/false);
18821 }
18822 else if (koenig_p && identifier_p (function))
18823 {
18824 /* Do nothing; calling tsubst_copy_and_build on an identifier
18825 would incorrectly perform unqualified lookup again.
18826
18827 Note that we can also have an IDENTIFIER_NODE if the earlier
18828 unqualified lookup found a member function; in that case
18829 koenig_p will be false and we do want to do the lookup
18830 again to find the instantiated member function.
18831
18832 FIXME but doing that causes c++/15272, so we need to stop
18833 using IDENTIFIER_NODE in that situation. */
18834 qualified_p = false;
18835 }
18836 else
18837 {
18838 if (TREE_CODE (function) == COMPONENT_REF)
18839 {
18840 tree op = TREE_OPERAND (function, 1);
18841
18842 qualified_p = (TREE_CODE (op) == SCOPE_REF
18843 || (BASELINK_P (op)
18844 && BASELINK_QUALIFIED_P (op)));
18845 }
18846 else
18847 qualified_p = false;
18848
18849 if (TREE_CODE (function) == ADDR_EXPR
18850 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
18851 /* Avoid error about taking the address of a constructor. */
18852 function = TREE_OPERAND (function, 0);
18853
18854 function = tsubst_copy_and_build (function, args, complain,
18855 in_decl,
18856 !qualified_p,
18857 integral_constant_expression_p);
18858
18859 if (BASELINK_P (function))
18860 qualified_p = true;
18861 }
18862
18863 nargs = call_expr_nargs (t);
18864 releasing_vec call_args;
18865 for (i = 0; i < nargs; ++i)
18866 {
18867 tree arg = CALL_EXPR_ARG (t, i);
18868
18869 if (!PACK_EXPANSION_P (arg))
18870 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
18871 else
18872 {
18873 /* Expand the pack expansion and push each entry onto
18874 CALL_ARGS. */
18875 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
18876 if (TREE_CODE (arg) == TREE_VEC)
18877 {
18878 unsigned int len, j;
18879
18880 len = TREE_VEC_LENGTH (arg);
18881 for (j = 0; j < len; ++j)
18882 {
18883 tree value = TREE_VEC_ELT (arg, j);
18884 if (value != NULL_TREE)
18885 value = convert_from_reference (value);
18886 vec_safe_push (call_args, value);
18887 }
18888 }
18889 else
18890 {
18891 /* A partial substitution. Add one entry. */
18892 vec_safe_push (call_args, arg);
18893 }
18894 }
18895 }
18896
18897 /* Stripped-down processing for a call in a thunk. Specifically, in
18898 the thunk template for a generic lambda. */
18899 if (CALL_FROM_THUNK_P (t))
18900 {
18901 /* Now that we've expanded any packs, the number of call args
18902 might be different. */
18903 unsigned int cargs = call_args->length ();
18904 tree thisarg = NULL_TREE;
18905 if (TREE_CODE (function) == COMPONENT_REF)
18906 {
18907 thisarg = TREE_OPERAND (function, 0);
18908 if (TREE_CODE (thisarg) == INDIRECT_REF)
18909 thisarg = TREE_OPERAND (thisarg, 0);
18910 function = TREE_OPERAND (function, 1);
18911 if (TREE_CODE (function) == BASELINK)
18912 function = BASELINK_FUNCTIONS (function);
18913 }
18914 /* We aren't going to do normal overload resolution, so force the
18915 template-id to resolve. */
18916 function = resolve_nondeduced_context (function, complain);
18917 for (unsigned i = 0; i < cargs; ++i)
18918 {
18919 /* In a thunk, pass through args directly, without any
18920 conversions. */
18921 tree arg = (*call_args)[i];
18922 while (TREE_CODE (arg) != PARM_DECL)
18923 arg = TREE_OPERAND (arg, 0);
18924 (*call_args)[i] = arg;
18925 }
18926 if (thisarg)
18927 {
18928 /* If there are no other args, just push 'this'. */
18929 if (cargs == 0)
18930 vec_safe_push (call_args, thisarg);
18931 else
18932 {
18933 /* Otherwise, shift the other args over to make room. */
18934 tree last = (*call_args)[cargs - 1];
18935 vec_safe_push (call_args, last);
18936 for (int i = cargs - 1; i > 0; --i)
18937 (*call_args)[i] = (*call_args)[i - 1];
18938 (*call_args)[0] = thisarg;
18939 }
18940 }
18941 ret = build_call_a (function, call_args->length (),
18942 call_args->address ());
18943 /* The thunk location is not interesting. */
18944 SET_EXPR_LOCATION (ret, UNKNOWN_LOCATION);
18945 CALL_FROM_THUNK_P (ret) = true;
18946 if (CLASS_TYPE_P (TREE_TYPE (ret)))
18947 CALL_EXPR_RETURN_SLOT_OPT (ret) = true;
18948
18949 RETURN (ret);
18950 }
18951
18952 /* We do not perform argument-dependent lookup if normal
18953 lookup finds a non-function, in accordance with the
18954 expected resolution of DR 218. */
18955 if (koenig_p
18956 && ((is_overloaded_fn (function)
18957 /* If lookup found a member function, the Koenig lookup is
18958 not appropriate, even if an unqualified-name was used
18959 to denote the function. */
18960 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
18961 || identifier_p (function))
18962 /* Only do this when substitution turns a dependent call
18963 into a non-dependent call. */
18964 && type_dependent_expression_p_push (t)
18965 && !any_type_dependent_arguments_p (call_args))
18966 function = perform_koenig_lookup (function, call_args, tf_none);
18967
18968 if (function != NULL_TREE
18969 && identifier_p (function)
18970 && !any_type_dependent_arguments_p (call_args))
18971 {
18972 if (koenig_p && (complain & tf_warning_or_error))
18973 {
18974 /* For backwards compatibility and good diagnostics, try
18975 the unqualified lookup again if we aren't in SFINAE
18976 context. */
18977 tree unq = (tsubst_copy_and_build
18978 (function, args, complain, in_decl, true,
18979 integral_constant_expression_p));
18980 if (unq == error_mark_node)
18981 RETURN (error_mark_node);
18982
18983 if (unq != function)
18984 {
18985 /* In a lambda fn, we have to be careful to not
18986 introduce new this captures. Legacy code can't
18987 be using lambdas anyway, so it's ok to be
18988 stricter. */
18989 bool in_lambda = (current_class_type
18990 && LAMBDA_TYPE_P (current_class_type));
18991 char const *const msg
18992 = G_("%qD was not declared in this scope, "
18993 "and no declarations were found by "
18994 "argument-dependent lookup at the point "
18995 "of instantiation");
18996
18997 bool diag = true;
18998 if (in_lambda)
18999 error_at (cp_expr_loc_or_input_loc (t),
19000 msg, function);
19001 else
19002 diag = permerror (cp_expr_loc_or_input_loc (t),
19003 msg, function);
19004 if (diag)
19005 {
19006 tree fn = unq;
19007
19008 if (INDIRECT_REF_P (fn))
19009 fn = TREE_OPERAND (fn, 0);
19010 if (is_overloaded_fn (fn))
19011 fn = get_first_fn (fn);
19012
19013 if (!DECL_P (fn))
19014 /* Can't say anything more. */;
19015 else if (DECL_CLASS_SCOPE_P (fn))
19016 {
19017 location_t loc = cp_expr_loc_or_input_loc (t);
19018 inform (loc,
19019 "declarations in dependent base %qT are "
19020 "not found by unqualified lookup",
19021 DECL_CLASS_CONTEXT (fn));
19022 if (current_class_ptr)
19023 inform (loc,
19024 "use %<this->%D%> instead", function);
19025 else
19026 inform (loc,
19027 "use %<%T::%D%> instead",
19028 current_class_name, function);
19029 }
19030 else
19031 inform (DECL_SOURCE_LOCATION (fn),
19032 "%qD declared here, later in the "
19033 "translation unit", fn);
19034 if (in_lambda)
19035 RETURN (error_mark_node);
19036 }
19037
19038 function = unq;
19039 }
19040 }
19041 if (identifier_p (function))
19042 {
19043 if (complain & tf_error)
19044 unqualified_name_lookup_error (function);
19045 RETURN (error_mark_node);
19046 }
19047 }
19048
19049 /* Remember that there was a reference to this entity. */
19050 if (function != NULL_TREE
19051 && DECL_P (function)
19052 && !mark_used (function, complain) && !(complain & tf_error))
19053 RETURN (error_mark_node);
19054
19055 /* Put back tf_decltype for the actual call. */
19056 complain |= decltype_flag;
19057
19058 if (function == NULL_TREE)
19059 switch (CALL_EXPR_IFN (t))
19060 {
19061 case IFN_LAUNDER:
19062 gcc_assert (nargs == 1);
19063 if (vec_safe_length (call_args) != 1)
19064 {
19065 error_at (cp_expr_loc_or_input_loc (t),
19066 "wrong number of arguments to "
19067 "%<__builtin_launder%>");
19068 ret = error_mark_node;
19069 }
19070 else
19071 ret = finish_builtin_launder (cp_expr_loc_or_input_loc (t),
19072 (*call_args)[0], complain);
19073 break;
19074
19075 case IFN_VEC_CONVERT:
19076 gcc_assert (nargs == 1);
19077 if (vec_safe_length (call_args) != 1)
19078 {
19079 error_at (cp_expr_loc_or_input_loc (t),
19080 "wrong number of arguments to "
19081 "%<__builtin_convertvector%>");
19082 ret = error_mark_node;
19083 break;
19084 }
19085 ret = cp_build_vec_convert ((*call_args)[0], input_location,
19086 tsubst (TREE_TYPE (t), args,
19087 complain, in_decl),
19088 complain);
19089 if (TREE_CODE (ret) == VIEW_CONVERT_EXPR)
19090 RETURN (ret);
19091 break;
19092
19093 default:
19094 /* Unsupported internal function with arguments. */
19095 gcc_unreachable ();
19096 }
19097 else if (TREE_CODE (function) == OFFSET_REF
19098 || TREE_CODE (function) == DOTSTAR_EXPR
19099 || TREE_CODE (function) == MEMBER_REF)
19100 ret = build_offset_ref_call_from_tree (function, &call_args,
19101 complain);
19102 else if (TREE_CODE (function) == COMPONENT_REF)
19103 {
19104 tree instance = TREE_OPERAND (function, 0);
19105 tree fn = TREE_OPERAND (function, 1);
19106
19107 if (processing_template_decl
19108 && (type_dependent_expression_p (instance)
19109 || (!BASELINK_P (fn)
19110 && TREE_CODE (fn) != FIELD_DECL)
19111 || type_dependent_expression_p (fn)
19112 || any_type_dependent_arguments_p (call_args)))
19113 ret = build_min_nt_call_vec (function, call_args);
19114 else if (!BASELINK_P (fn))
19115 ret = finish_call_expr (function, &call_args,
19116 /*disallow_virtual=*/false,
19117 /*koenig_p=*/false,
19118 complain);
19119 else
19120 ret = (build_new_method_call
19121 (instance, fn,
19122 &call_args, NULL_TREE,
19123 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
19124 /*fn_p=*/NULL,
19125 complain));
19126 }
19127 else
19128 ret = finish_call_expr (function, &call_args,
19129 /*disallow_virtual=*/qualified_p,
19130 koenig_p,
19131 complain);
19132
19133 if (ret != error_mark_node)
19134 {
19135 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
19136 bool ord = CALL_EXPR_ORDERED_ARGS (t);
19137 bool rev = CALL_EXPR_REVERSE_ARGS (t);
19138 if (op || ord || rev)
19139 {
19140 function = extract_call_expr (ret);
19141 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
19142 CALL_EXPR_ORDERED_ARGS (function) = ord;
19143 CALL_EXPR_REVERSE_ARGS (function) = rev;
19144 }
19145 }
19146
19147 RETURN (ret);
19148 }
19149
19150 case COND_EXPR:
19151 {
19152 tree cond = RECUR (TREE_OPERAND (t, 0));
19153 cond = mark_rvalue_use (cond);
19154 tree folded_cond = fold_non_dependent_expr (cond, complain);
19155 tree exp1, exp2;
19156
19157 if (TREE_CODE (folded_cond) == INTEGER_CST)
19158 {
19159 if (integer_zerop (folded_cond))
19160 {
19161 ++c_inhibit_evaluation_warnings;
19162 exp1 = RECUR (TREE_OPERAND (t, 1));
19163 --c_inhibit_evaluation_warnings;
19164 exp2 = RECUR (TREE_OPERAND (t, 2));
19165 }
19166 else
19167 {
19168 exp1 = RECUR (TREE_OPERAND (t, 1));
19169 ++c_inhibit_evaluation_warnings;
19170 exp2 = RECUR (TREE_OPERAND (t, 2));
19171 --c_inhibit_evaluation_warnings;
19172 }
19173 cond = folded_cond;
19174 }
19175 else
19176 {
19177 exp1 = RECUR (TREE_OPERAND (t, 1));
19178 exp2 = RECUR (TREE_OPERAND (t, 2));
19179 }
19180
19181 warning_sentinel s(warn_duplicated_branches);
19182 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
19183 cond, exp1, exp2, complain));
19184 }
19185
19186 case PSEUDO_DTOR_EXPR:
19187 {
19188 tree op0 = RECUR (TREE_OPERAND (t, 0));
19189 tree op1 = RECUR (TREE_OPERAND (t, 1));
19190 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
19191 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
19192 input_location));
19193 }
19194
19195 case TREE_LIST:
19196 {
19197 tree purpose, value, chain;
19198
19199 if (t == void_list_node)
19200 RETURN (t);
19201
19202 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
19203 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
19204 {
19205 /* We have pack expansions, so expand those and
19206 create a new list out of it. */
19207 tree purposevec = NULL_TREE;
19208 tree valuevec = NULL_TREE;
19209 tree chain;
19210 int i, len = -1;
19211
19212 /* Expand the argument expressions. */
19213 if (TREE_PURPOSE (t))
19214 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
19215 complain, in_decl);
19216 if (TREE_VALUE (t))
19217 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
19218 complain, in_decl);
19219
19220 /* Build the rest of the list. */
19221 chain = TREE_CHAIN (t);
19222 if (chain && chain != void_type_node)
19223 chain = RECUR (chain);
19224
19225 /* Determine the number of arguments. */
19226 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
19227 {
19228 len = TREE_VEC_LENGTH (purposevec);
19229 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
19230 }
19231 else if (TREE_CODE (valuevec) == TREE_VEC)
19232 len = TREE_VEC_LENGTH (valuevec);
19233 else
19234 {
19235 /* Since we only performed a partial substitution into
19236 the argument pack, we only RETURN (a single list
19237 node. */
19238 if (purposevec == TREE_PURPOSE (t)
19239 && valuevec == TREE_VALUE (t)
19240 && chain == TREE_CHAIN (t))
19241 RETURN (t);
19242
19243 RETURN (tree_cons (purposevec, valuevec, chain));
19244 }
19245
19246 /* Convert the argument vectors into a TREE_LIST */
19247 i = len;
19248 while (i > 0)
19249 {
19250 /* Grab the Ith values. */
19251 i--;
19252 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
19253 : NULL_TREE;
19254 value
19255 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
19256 : NULL_TREE;
19257
19258 /* Build the list (backwards). */
19259 chain = tree_cons (purpose, value, chain);
19260 }
19261
19262 RETURN (chain);
19263 }
19264
19265 purpose = TREE_PURPOSE (t);
19266 if (purpose)
19267 purpose = RECUR (purpose);
19268 value = TREE_VALUE (t);
19269 if (value)
19270 value = RECUR (value);
19271 chain = TREE_CHAIN (t);
19272 if (chain && chain != void_type_node)
19273 chain = RECUR (chain);
19274 if (purpose == TREE_PURPOSE (t)
19275 && value == TREE_VALUE (t)
19276 && chain == TREE_CHAIN (t))
19277 RETURN (t);
19278 RETURN (tree_cons (purpose, value, chain));
19279 }
19280
19281 case COMPONENT_REF:
19282 {
19283 tree object;
19284 tree object_type;
19285 tree member;
19286 tree r;
19287
19288 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19289 args, complain, in_decl);
19290 /* Remember that there was a reference to this entity. */
19291 if (DECL_P (object)
19292 && !mark_used (object, complain) && !(complain & tf_error))
19293 RETURN (error_mark_node);
19294 object_type = TREE_TYPE (object);
19295
19296 member = TREE_OPERAND (t, 1);
19297 if (BASELINK_P (member))
19298 member = tsubst_baselink (member,
19299 non_reference (TREE_TYPE (object)),
19300 args, complain, in_decl);
19301 else
19302 member = tsubst_copy (member, args, complain, in_decl);
19303 if (member == error_mark_node)
19304 RETURN (error_mark_node);
19305
19306 if (TREE_CODE (member) == FIELD_DECL)
19307 {
19308 r = finish_non_static_data_member (member, object, NULL_TREE);
19309 if (TREE_CODE (r) == COMPONENT_REF)
19310 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
19311 RETURN (r);
19312 }
19313 else if (type_dependent_expression_p (object))
19314 /* We can't do much here. */;
19315 else if (!CLASS_TYPE_P (object_type))
19316 {
19317 if (scalarish_type_p (object_type))
19318 {
19319 tree s = NULL_TREE;
19320 tree dtor = member;
19321
19322 if (TREE_CODE (dtor) == SCOPE_REF)
19323 {
19324 s = TREE_OPERAND (dtor, 0);
19325 dtor = TREE_OPERAND (dtor, 1);
19326 }
19327 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
19328 {
19329 dtor = TREE_OPERAND (dtor, 0);
19330 if (TYPE_P (dtor))
19331 RETURN (finish_pseudo_destructor_expr
19332 (object, s, dtor, input_location));
19333 }
19334 }
19335 }
19336 else if (TREE_CODE (member) == SCOPE_REF
19337 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
19338 {
19339 /* Lookup the template functions now that we know what the
19340 scope is. */
19341 tree scope = TREE_OPERAND (member, 0);
19342 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
19343 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
19344 member = lookup_qualified_name (scope, tmpl,
19345 /*is_type_p=*/false,
19346 /*complain=*/false);
19347 if (BASELINK_P (member))
19348 {
19349 BASELINK_FUNCTIONS (member)
19350 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
19351 args);
19352 member = (adjust_result_of_qualified_name_lookup
19353 (member, BINFO_TYPE (BASELINK_BINFO (member)),
19354 object_type));
19355 }
19356 else
19357 {
19358 qualified_name_lookup_error (scope, tmpl, member,
19359 input_location);
19360 RETURN (error_mark_node);
19361 }
19362 }
19363 else if (TREE_CODE (member) == SCOPE_REF
19364 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
19365 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
19366 {
19367 if (complain & tf_error)
19368 {
19369 if (TYPE_P (TREE_OPERAND (member, 0)))
19370 error ("%qT is not a class or namespace",
19371 TREE_OPERAND (member, 0));
19372 else
19373 error ("%qD is not a class or namespace",
19374 TREE_OPERAND (member, 0));
19375 }
19376 RETURN (error_mark_node);
19377 }
19378
19379 r = finish_class_member_access_expr (object, member,
19380 /*template_p=*/false,
19381 complain);
19382 if (TREE_CODE (r) == COMPONENT_REF)
19383 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
19384 RETURN (r);
19385 }
19386
19387 case THROW_EXPR:
19388 RETURN (build_throw
19389 (RECUR (TREE_OPERAND (t, 0))));
19390
19391 case CONSTRUCTOR:
19392 {
19393 vec<constructor_elt, va_gc> *n;
19394 constructor_elt *ce;
19395 unsigned HOST_WIDE_INT idx;
19396 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19397 bool process_index_p;
19398 int newlen;
19399 bool need_copy_p = false;
19400 tree r;
19401
19402 if (type == error_mark_node)
19403 RETURN (error_mark_node);
19404
19405 /* We do not want to process the index of aggregate
19406 initializers as they are identifier nodes which will be
19407 looked up by digest_init. */
19408 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
19409
19410 if (null_member_pointer_value_p (t))
19411 {
19412 gcc_assert (same_type_p (type, TREE_TYPE (t)));
19413 RETURN (t);
19414 }
19415
19416 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
19417 newlen = vec_safe_length (n);
19418 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
19419 {
19420 if (ce->index && process_index_p
19421 /* An identifier index is looked up in the type
19422 being initialized, not the current scope. */
19423 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
19424 ce->index = RECUR (ce->index);
19425
19426 if (PACK_EXPANSION_P (ce->value))
19427 {
19428 /* Substitute into the pack expansion. */
19429 ce->value = tsubst_pack_expansion (ce->value, args, complain,
19430 in_decl);
19431
19432 if (ce->value == error_mark_node
19433 || PACK_EXPANSION_P (ce->value))
19434 ;
19435 else if (TREE_VEC_LENGTH (ce->value) == 1)
19436 /* Just move the argument into place. */
19437 ce->value = TREE_VEC_ELT (ce->value, 0);
19438 else
19439 {
19440 /* Update the length of the final CONSTRUCTOR
19441 arguments vector, and note that we will need to
19442 copy.*/
19443 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
19444 need_copy_p = true;
19445 }
19446 }
19447 else
19448 ce->value = RECUR (ce->value);
19449 }
19450
19451 if (need_copy_p)
19452 {
19453 vec<constructor_elt, va_gc> *old_n = n;
19454
19455 vec_alloc (n, newlen);
19456 FOR_EACH_VEC_ELT (*old_n, idx, ce)
19457 {
19458 if (TREE_CODE (ce->value) == TREE_VEC)
19459 {
19460 int i, len = TREE_VEC_LENGTH (ce->value);
19461 for (i = 0; i < len; ++i)
19462 CONSTRUCTOR_APPEND_ELT (n, 0,
19463 TREE_VEC_ELT (ce->value, i));
19464 }
19465 else
19466 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
19467 }
19468 }
19469
19470 r = build_constructor (init_list_type_node, n);
19471 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
19472 CONSTRUCTOR_IS_DESIGNATED_INIT (r)
19473 = CONSTRUCTOR_IS_DESIGNATED_INIT (t);
19474
19475 if (TREE_HAS_CONSTRUCTOR (t))
19476 {
19477 fcl_t cl = fcl_functional;
19478 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
19479 cl = fcl_c99;
19480 RETURN (finish_compound_literal (type, r, complain, cl));
19481 }
19482
19483 TREE_TYPE (r) = type;
19484 RETURN (r);
19485 }
19486
19487 case TYPEID_EXPR:
19488 {
19489 tree operand_0 = TREE_OPERAND (t, 0);
19490 if (TYPE_P (operand_0))
19491 {
19492 operand_0 = tsubst (operand_0, args, complain, in_decl);
19493 RETURN (get_typeid (operand_0, complain));
19494 }
19495 else
19496 {
19497 operand_0 = RECUR (operand_0);
19498 RETURN (build_typeid (operand_0, complain));
19499 }
19500 }
19501
19502 case VAR_DECL:
19503 if (!args)
19504 RETURN (t);
19505 /* Fall through */
19506
19507 case PARM_DECL:
19508 {
19509 tree r = tsubst_copy (t, args, complain, in_decl);
19510 /* ??? We're doing a subset of finish_id_expression here. */
19511 if (tree wrap = maybe_get_tls_wrapper_call (r))
19512 /* Replace an evaluated use of the thread_local variable with
19513 a call to its wrapper. */
19514 r = wrap;
19515 else if (outer_automatic_var_p (r))
19516 r = process_outer_var_ref (r, complain);
19517
19518 if (!TYPE_REF_P (TREE_TYPE (t)))
19519 /* If the original type was a reference, we'll be wrapped in
19520 the appropriate INDIRECT_REF. */
19521 r = convert_from_reference (r);
19522 RETURN (r);
19523 }
19524
19525 case VA_ARG_EXPR:
19526 {
19527 tree op0 = RECUR (TREE_OPERAND (t, 0));
19528 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19529 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
19530 }
19531
19532 case OFFSETOF_EXPR:
19533 {
19534 tree object_ptr
19535 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
19536 in_decl, /*function_p=*/false,
19537 /*integral_constant_expression_p=*/false);
19538 RETURN (finish_offsetof (object_ptr,
19539 RECUR (TREE_OPERAND (t, 0)),
19540 EXPR_LOCATION (t)));
19541 }
19542
19543 case ADDRESSOF_EXPR:
19544 RETURN (cp_build_addressof (EXPR_LOCATION (t),
19545 RECUR (TREE_OPERAND (t, 0)), complain));
19546
19547 case TRAIT_EXPR:
19548 {
19549 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
19550 complain, in_decl);
19551
19552 tree type2 = TRAIT_EXPR_TYPE2 (t);
19553 if (type2 && TREE_CODE (type2) == TREE_LIST)
19554 type2 = RECUR (type2);
19555 else if (type2)
19556 type2 = tsubst (type2, args, complain, in_decl);
19557
19558 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t),
19559 TRAIT_EXPR_KIND (t), type1, type2));
19560 }
19561
19562 case STMT_EXPR:
19563 {
19564 tree old_stmt_expr = cur_stmt_expr;
19565 tree stmt_expr = begin_stmt_expr ();
19566
19567 cur_stmt_expr = stmt_expr;
19568 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
19569 integral_constant_expression_p);
19570 stmt_expr = finish_stmt_expr (stmt_expr, false);
19571 cur_stmt_expr = old_stmt_expr;
19572
19573 /* If the resulting list of expression statement is empty,
19574 fold it further into void_node. */
19575 if (empty_expr_stmt_p (stmt_expr))
19576 stmt_expr = void_node;
19577
19578 RETURN (stmt_expr);
19579 }
19580
19581 case LAMBDA_EXPR:
19582 {
19583 if (complain & tf_partial)
19584 {
19585 /* We don't have a full set of template arguments yet; don't touch
19586 the lambda at all. */
19587 gcc_assert (processing_template_decl);
19588 return t;
19589 }
19590 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
19591
19592 RETURN (build_lambda_object (r));
19593 }
19594
19595 case TARGET_EXPR:
19596 /* We can get here for a constant initializer of non-dependent type.
19597 FIXME stop folding in cp_parser_initializer_clause. */
19598 {
19599 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
19600 complain);
19601 RETURN (r);
19602 }
19603
19604 case TRANSACTION_EXPR:
19605 RETURN (tsubst_expr(t, args, complain, in_decl,
19606 integral_constant_expression_p));
19607
19608 case PAREN_EXPR:
19609 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
19610
19611 case VEC_PERM_EXPR:
19612 {
19613 tree op0 = RECUR (TREE_OPERAND (t, 0));
19614 tree op1 = RECUR (TREE_OPERAND (t, 1));
19615 tree op2 = RECUR (TREE_OPERAND (t, 2));
19616 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
19617 complain));
19618 }
19619
19620 case REQUIRES_EXPR:
19621 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
19622
19623 case RANGE_EXPR:
19624 /* No need to substitute further, a RANGE_EXPR will always be built
19625 with constant operands. */
19626 RETURN (t);
19627
19628 case NON_LVALUE_EXPR:
19629 case VIEW_CONVERT_EXPR:
19630 if (location_wrapper_p (t))
19631 /* We need to do this here as well as in tsubst_copy so we get the
19632 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
19633 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)),
19634 EXPR_LOCATION (t)));
19635 /* fallthrough. */
19636
19637 default:
19638 /* Handle Objective-C++ constructs, if appropriate. */
19639 {
19640 tree subst
19641 = objcp_tsubst_copy_and_build (t, args, complain,
19642 in_decl, /*function_p=*/false);
19643 if (subst)
19644 RETURN (subst);
19645 }
19646 RETURN (tsubst_copy (t, args, complain, in_decl));
19647 }
19648
19649 #undef RECUR
19650 #undef RETURN
19651 out:
19652 input_location = loc;
19653 return retval;
19654 }
19655
19656 /* Verify that the instantiated ARGS are valid. For type arguments,
19657 make sure that the type's linkage is ok. For non-type arguments,
19658 make sure they are constants if they are integral or enumerations.
19659 Emit an error under control of COMPLAIN, and return TRUE on error. */
19660
19661 static bool
19662 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
19663 {
19664 if (dependent_template_arg_p (t))
19665 return false;
19666 if (ARGUMENT_PACK_P (t))
19667 {
19668 tree vec = ARGUMENT_PACK_ARGS (t);
19669 int len = TREE_VEC_LENGTH (vec);
19670 bool result = false;
19671 int i;
19672
19673 for (i = 0; i < len; ++i)
19674 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
19675 result = true;
19676 return result;
19677 }
19678 else if (TYPE_P (t))
19679 {
19680 /* [basic.link]: A name with no linkage (notably, the name
19681 of a class or enumeration declared in a local scope)
19682 shall not be used to declare an entity with linkage.
19683 This implies that names with no linkage cannot be used as
19684 template arguments
19685
19686 DR 757 relaxes this restriction for C++0x. */
19687 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
19688 : no_linkage_check (t, /*relaxed_p=*/false));
19689
19690 if (nt)
19691 {
19692 /* DR 488 makes use of a type with no linkage cause
19693 type deduction to fail. */
19694 if (complain & tf_error)
19695 {
19696 if (TYPE_UNNAMED_P (nt))
19697 error ("%qT is/uses unnamed type", t);
19698 else
19699 error ("template argument for %qD uses local type %qT",
19700 tmpl, t);
19701 }
19702 return true;
19703 }
19704 /* In order to avoid all sorts of complications, we do not
19705 allow variably-modified types as template arguments. */
19706 else if (variably_modified_type_p (t, NULL_TREE))
19707 {
19708 if (complain & tf_error)
19709 error ("%qT is a variably modified type", t);
19710 return true;
19711 }
19712 }
19713 /* Class template and alias template arguments should be OK. */
19714 else if (DECL_TYPE_TEMPLATE_P (t))
19715 ;
19716 /* A non-type argument of integral or enumerated type must be a
19717 constant. */
19718 else if (TREE_TYPE (t)
19719 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
19720 && !REFERENCE_REF_P (t)
19721 && !TREE_CONSTANT (t))
19722 {
19723 if (complain & tf_error)
19724 error ("integral expression %qE is not constant", t);
19725 return true;
19726 }
19727 return false;
19728 }
19729
19730 static bool
19731 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
19732 {
19733 int ix, len = DECL_NTPARMS (tmpl);
19734 bool result = false;
19735
19736 for (ix = 0; ix != len; ix++)
19737 {
19738 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
19739 result = true;
19740 }
19741 if (result && (complain & tf_error))
19742 error (" trying to instantiate %qD", tmpl);
19743 return result;
19744 }
19745
19746 /* We're out of SFINAE context now, so generate diagnostics for the access
19747 errors we saw earlier when instantiating D from TMPL and ARGS. */
19748
19749 static void
19750 recheck_decl_substitution (tree d, tree tmpl, tree args)
19751 {
19752 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
19753 tree type = TREE_TYPE (pattern);
19754 location_t loc = input_location;
19755
19756 push_access_scope (d);
19757 push_deferring_access_checks (dk_no_deferred);
19758 input_location = DECL_SOURCE_LOCATION (pattern);
19759 tsubst (type, args, tf_warning_or_error, d);
19760 input_location = loc;
19761 pop_deferring_access_checks ();
19762 pop_access_scope (d);
19763 }
19764
19765 /* Instantiate the indicated variable, function, or alias template TMPL with
19766 the template arguments in TARG_PTR. */
19767
19768 static tree
19769 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
19770 {
19771 tree targ_ptr = orig_args;
19772 tree fndecl;
19773 tree gen_tmpl;
19774 tree spec;
19775 bool access_ok = true;
19776
19777 if (tmpl == error_mark_node)
19778 return error_mark_node;
19779
19780 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
19781
19782 /* If this function is a clone, handle it specially. */
19783 if (DECL_CLONED_FUNCTION_P (tmpl))
19784 {
19785 tree spec;
19786 tree clone;
19787
19788 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
19789 DECL_CLONED_FUNCTION. */
19790 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
19791 targ_ptr, complain);
19792 if (spec == error_mark_node)
19793 return error_mark_node;
19794
19795 /* Look for the clone. */
19796 FOR_EACH_CLONE (clone, spec)
19797 if (DECL_NAME (clone) == DECL_NAME (tmpl))
19798 return clone;
19799 /* We should always have found the clone by now. */
19800 gcc_unreachable ();
19801 return NULL_TREE;
19802 }
19803
19804 if (targ_ptr == error_mark_node)
19805 return error_mark_node;
19806
19807 /* Check to see if we already have this specialization. */
19808 gen_tmpl = most_general_template (tmpl);
19809 if (TMPL_ARGS_DEPTH (targ_ptr)
19810 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
19811 /* targ_ptr only has the innermost template args, so add the outer ones
19812 from tmpl, which could be either a partial instantiation or gen_tmpl (in
19813 the case of a non-dependent call within a template definition). */
19814 targ_ptr = (add_outermost_template_args
19815 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
19816 targ_ptr));
19817
19818 /* It would be nice to avoid hashing here and then again in tsubst_decl,
19819 but it doesn't seem to be on the hot path. */
19820 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
19821
19822 gcc_assert (tmpl == gen_tmpl
19823 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
19824 == spec)
19825 || fndecl == NULL_TREE);
19826
19827 if (spec != NULL_TREE)
19828 {
19829 if (FNDECL_HAS_ACCESS_ERRORS (spec))
19830 {
19831 if (complain & tf_error)
19832 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
19833 return error_mark_node;
19834 }
19835 return spec;
19836 }
19837
19838 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
19839 complain))
19840 return error_mark_node;
19841
19842 /* We are building a FUNCTION_DECL, during which the access of its
19843 parameters and return types have to be checked. However this
19844 FUNCTION_DECL which is the desired context for access checking
19845 is not built yet. We solve this chicken-and-egg problem by
19846 deferring all checks until we have the FUNCTION_DECL. */
19847 push_deferring_access_checks (dk_deferred);
19848
19849 /* Instantiation of the function happens in the context of the function
19850 template, not the context of the overload resolution we're doing. */
19851 push_to_top_level ();
19852 /* If there are dependent arguments, e.g. because we're doing partial
19853 ordering, make sure processing_template_decl stays set. */
19854 if (uses_template_parms (targ_ptr))
19855 ++processing_template_decl;
19856 if (DECL_CLASS_SCOPE_P (gen_tmpl))
19857 {
19858 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
19859 complain, gen_tmpl, true);
19860 push_nested_class (ctx);
19861 }
19862
19863 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
19864
19865 fndecl = NULL_TREE;
19866 if (VAR_P (pattern))
19867 {
19868 /* We need to determine if we're using a partial or explicit
19869 specialization now, because the type of the variable could be
19870 different. */
19871 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
19872 tree elt = most_specialized_partial_spec (tid, complain);
19873 if (elt == error_mark_node)
19874 pattern = error_mark_node;
19875 else if (elt)
19876 {
19877 tree partial_tmpl = TREE_VALUE (elt);
19878 tree partial_args = TREE_PURPOSE (elt);
19879 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
19880 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
19881 }
19882 }
19883
19884 /* Substitute template parameters to obtain the specialization. */
19885 if (fndecl == NULL_TREE)
19886 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
19887 if (DECL_CLASS_SCOPE_P (gen_tmpl))
19888 pop_nested_class ();
19889 pop_from_top_level ();
19890
19891 if (fndecl == error_mark_node)
19892 {
19893 pop_deferring_access_checks ();
19894 return error_mark_node;
19895 }
19896
19897 /* The DECL_TI_TEMPLATE should always be the immediate parent
19898 template, not the most general template. */
19899 DECL_TI_TEMPLATE (fndecl) = tmpl;
19900 DECL_TI_ARGS (fndecl) = targ_ptr;
19901
19902 /* Now we know the specialization, compute access previously
19903 deferred. Do no access control for inheriting constructors,
19904 as we already checked access for the inherited constructor. */
19905 if (!(flag_new_inheriting_ctors
19906 && DECL_INHERITED_CTOR (fndecl)))
19907 {
19908 push_access_scope (fndecl);
19909 if (!perform_deferred_access_checks (complain))
19910 access_ok = false;
19911 pop_access_scope (fndecl);
19912 }
19913 pop_deferring_access_checks ();
19914
19915 /* If we've just instantiated the main entry point for a function,
19916 instantiate all the alternate entry points as well. We do this
19917 by cloning the instantiation of the main entry point, not by
19918 instantiating the template clones. */
19919 if (tree chain = DECL_CHAIN (gen_tmpl))
19920 if (DECL_P (chain) && DECL_CLONED_FUNCTION_P (chain))
19921 clone_function_decl (fndecl, /*update_methods=*/false);
19922
19923 if (!access_ok)
19924 {
19925 if (!(complain & tf_error))
19926 {
19927 /* Remember to reinstantiate when we're out of SFINAE so the user
19928 can see the errors. */
19929 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
19930 }
19931 return error_mark_node;
19932 }
19933 return fndecl;
19934 }
19935
19936 /* Wrapper for instantiate_template_1. */
19937
19938 tree
19939 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
19940 {
19941 tree ret;
19942 timevar_push (TV_TEMPLATE_INST);
19943 ret = instantiate_template_1 (tmpl, orig_args, complain);
19944 timevar_pop (TV_TEMPLATE_INST);
19945 return ret;
19946 }
19947
19948 /* Instantiate the alias template TMPL with ARGS. Also push a template
19949 instantiation level, which instantiate_template doesn't do because
19950 functions and variables have sufficient context established by the
19951 callers. */
19952
19953 static tree
19954 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
19955 {
19956 if (tmpl == error_mark_node || args == error_mark_node)
19957 return error_mark_node;
19958 if (!push_tinst_level (tmpl, args))
19959 return error_mark_node;
19960
19961 args =
19962 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
19963 args, tmpl, complain,
19964 /*require_all_args=*/true,
19965 /*use_default_args=*/true);
19966
19967 tree r = instantiate_template (tmpl, args, complain);
19968 pop_tinst_level ();
19969
19970 return r;
19971 }
19972
19973 /* PARM is a template parameter pack for FN. Returns true iff
19974 PARM is used in a deducible way in the argument list of FN. */
19975
19976 static bool
19977 pack_deducible_p (tree parm, tree fn)
19978 {
19979 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
19980 for (; t; t = TREE_CHAIN (t))
19981 {
19982 tree type = TREE_VALUE (t);
19983 tree packs;
19984 if (!PACK_EXPANSION_P (type))
19985 continue;
19986 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
19987 packs; packs = TREE_CHAIN (packs))
19988 if (template_args_equal (TREE_VALUE (packs), parm))
19989 {
19990 /* The template parameter pack is used in a function parameter
19991 pack. If this is the end of the parameter list, the
19992 template parameter pack is deducible. */
19993 if (TREE_CHAIN (t) == void_list_node)
19994 return true;
19995 else
19996 /* Otherwise, not. Well, it could be deduced from
19997 a non-pack parameter, but doing so would end up with
19998 a deduction mismatch, so don't bother. */
19999 return false;
20000 }
20001 }
20002 /* The template parameter pack isn't used in any function parameter
20003 packs, but it might be used deeper, e.g. tuple<Args...>. */
20004 return true;
20005 }
20006
20007 /* Subroutine of fn_type_unification: check non-dependent parms for
20008 convertibility. */
20009
20010 static int
20011 check_non_deducible_conversions (tree parms, const tree *args, unsigned nargs,
20012 tree fn, unification_kind_t strict, int flags,
20013 struct conversion **convs, bool explain_p)
20014 {
20015 /* Non-constructor methods need to leave a conversion for 'this', which
20016 isn't included in nargs here. */
20017 unsigned offset = (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
20018 && !DECL_CONSTRUCTOR_P (fn));
20019
20020 for (unsigned ia = 0;
20021 parms && parms != void_list_node && ia < nargs; )
20022 {
20023 tree parm = TREE_VALUE (parms);
20024
20025 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
20026 && (!TREE_CHAIN (parms)
20027 || TREE_CHAIN (parms) == void_list_node))
20028 /* For a function parameter pack that occurs at the end of the
20029 parameter-declaration-list, the type A of each remaining
20030 argument of the call is compared with the type P of the
20031 declarator-id of the function parameter pack. */
20032 break;
20033
20034 parms = TREE_CHAIN (parms);
20035
20036 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
20037 /* For a function parameter pack that does not occur at the
20038 end of the parameter-declaration-list, the type of the
20039 parameter pack is a non-deduced context. */
20040 continue;
20041
20042 if (!uses_template_parms (parm))
20043 {
20044 tree arg = args[ia];
20045 conversion **conv_p = convs ? &convs[ia+offset] : NULL;
20046 int lflags = conv_flags (ia, nargs, fn, arg, flags);
20047
20048 if (check_non_deducible_conversion (parm, arg, strict, lflags,
20049 conv_p, explain_p))
20050 return 1;
20051 }
20052
20053 ++ia;
20054 }
20055
20056 return 0;
20057 }
20058
20059 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
20060 NARGS elements of the arguments that are being used when calling
20061 it. TARGS is a vector into which the deduced template arguments
20062 are placed.
20063
20064 Returns either a FUNCTION_DECL for the matching specialization of FN or
20065 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
20066 true, diagnostics will be printed to explain why it failed.
20067
20068 If FN is a conversion operator, or we are trying to produce a specific
20069 specialization, RETURN_TYPE is the return type desired.
20070
20071 The EXPLICIT_TARGS are explicit template arguments provided via a
20072 template-id.
20073
20074 The parameter STRICT is one of:
20075
20076 DEDUCE_CALL:
20077 We are deducing arguments for a function call, as in
20078 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
20079 deducing arguments for a call to the result of a conversion
20080 function template, as in [over.call.object].
20081
20082 DEDUCE_CONV:
20083 We are deducing arguments for a conversion function, as in
20084 [temp.deduct.conv].
20085
20086 DEDUCE_EXACT:
20087 We are deducing arguments when doing an explicit instantiation
20088 as in [temp.explicit], when determining an explicit specialization
20089 as in [temp.expl.spec], or when taking the address of a function
20090 template, as in [temp.deduct.funcaddr]. */
20091
20092 tree
20093 fn_type_unification (tree fn,
20094 tree explicit_targs,
20095 tree targs,
20096 const tree *args,
20097 unsigned int nargs,
20098 tree return_type,
20099 unification_kind_t strict,
20100 int flags,
20101 struct conversion **convs,
20102 bool explain_p,
20103 bool decltype_p)
20104 {
20105 tree parms;
20106 tree fntype;
20107 tree decl = NULL_TREE;
20108 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
20109 bool ok;
20110 static int deduction_depth;
20111 /* type_unification_real will pass back any access checks from default
20112 template argument substitution. */
20113 vec<deferred_access_check, va_gc> *checks = NULL;
20114 /* We don't have all the template args yet. */
20115 bool incomplete = true;
20116
20117 tree orig_fn = fn;
20118 if (flag_new_inheriting_ctors)
20119 fn = strip_inheriting_ctors (fn);
20120
20121 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
20122 tree r = error_mark_node;
20123
20124 tree full_targs = targs;
20125 if (TMPL_ARGS_DEPTH (targs)
20126 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
20127 full_targs = (add_outermost_template_args
20128 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
20129 targs));
20130
20131 if (decltype_p)
20132 complain |= tf_decltype;
20133
20134 /* In C++0x, it's possible to have a function template whose type depends
20135 on itself recursively. This is most obvious with decltype, but can also
20136 occur with enumeration scope (c++/48969). So we need to catch infinite
20137 recursion and reject the substitution at deduction time; this function
20138 will return error_mark_node for any repeated substitution.
20139
20140 This also catches excessive recursion such as when f<N> depends on
20141 f<N-1> across all integers, and returns error_mark_node for all the
20142 substitutions back up to the initial one.
20143
20144 This is, of course, not reentrant. */
20145 if (excessive_deduction_depth)
20146 return error_mark_node;
20147 ++deduction_depth;
20148
20149 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
20150
20151 fntype = TREE_TYPE (fn);
20152 if (explicit_targs)
20153 {
20154 /* [temp.deduct]
20155
20156 The specified template arguments must match the template
20157 parameters in kind (i.e., type, nontype, template), and there
20158 must not be more arguments than there are parameters;
20159 otherwise type deduction fails.
20160
20161 Nontype arguments must match the types of the corresponding
20162 nontype template parameters, or must be convertible to the
20163 types of the corresponding nontype parameters as specified in
20164 _temp.arg.nontype_, otherwise type deduction fails.
20165
20166 All references in the function type of the function template
20167 to the corresponding template parameters are replaced by the
20168 specified template argument values. If a substitution in a
20169 template parameter or in the function type of the function
20170 template results in an invalid type, type deduction fails. */
20171 int i, len = TREE_VEC_LENGTH (tparms);
20172 location_t loc = input_location;
20173 incomplete = false;
20174
20175 if (explicit_targs == error_mark_node)
20176 goto fail;
20177
20178 if (TMPL_ARGS_DEPTH (explicit_targs)
20179 < TMPL_ARGS_DEPTH (full_targs))
20180 explicit_targs = add_outermost_template_args (full_targs,
20181 explicit_targs);
20182
20183 /* Adjust any explicit template arguments before entering the
20184 substitution context. */
20185 explicit_targs
20186 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
20187 complain|tf_partial,
20188 /*require_all_args=*/false,
20189 /*use_default_args=*/false));
20190 if (explicit_targs == error_mark_node)
20191 goto fail;
20192
20193 /* Substitute the explicit args into the function type. This is
20194 necessary so that, for instance, explicitly declared function
20195 arguments can match null pointed constants. If we were given
20196 an incomplete set of explicit args, we must not do semantic
20197 processing during substitution as we could create partial
20198 instantiations. */
20199 for (i = 0; i < len; i++)
20200 {
20201 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
20202 bool parameter_pack = false;
20203 tree targ = TREE_VEC_ELT (explicit_targs, i);
20204
20205 /* Dig out the actual parm. */
20206 if (TREE_CODE (parm) == TYPE_DECL
20207 || TREE_CODE (parm) == TEMPLATE_DECL)
20208 {
20209 parm = TREE_TYPE (parm);
20210 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
20211 }
20212 else if (TREE_CODE (parm) == PARM_DECL)
20213 {
20214 parm = DECL_INITIAL (parm);
20215 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
20216 }
20217
20218 if (targ == NULL_TREE)
20219 /* No explicit argument for this template parameter. */
20220 incomplete = true;
20221 else if (parameter_pack && pack_deducible_p (parm, fn))
20222 {
20223 /* Mark the argument pack as "incomplete". We could
20224 still deduce more arguments during unification.
20225 We remove this mark in type_unification_real. */
20226 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
20227 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
20228 = ARGUMENT_PACK_ARGS (targ);
20229
20230 /* We have some incomplete argument packs. */
20231 incomplete = true;
20232 }
20233 }
20234
20235 if (incomplete)
20236 {
20237 if (!push_tinst_level (fn, explicit_targs))
20238 {
20239 excessive_deduction_depth = true;
20240 goto fail;
20241 }
20242 ++processing_template_decl;
20243 input_location = DECL_SOURCE_LOCATION (fn);
20244 /* Ignore any access checks; we'll see them again in
20245 instantiate_template and they might have the wrong
20246 access path at this point. */
20247 push_deferring_access_checks (dk_deferred);
20248 tsubst_flags_t ecomplain = complain | tf_partial | tf_fndecl_type;
20249 fntype = tsubst (TREE_TYPE (fn), explicit_targs, ecomplain, NULL_TREE);
20250 pop_deferring_access_checks ();
20251 input_location = loc;
20252 --processing_template_decl;
20253 pop_tinst_level ();
20254
20255 if (fntype == error_mark_node)
20256 goto fail;
20257 }
20258
20259 /* Place the explicitly specified arguments in TARGS. */
20260 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
20261 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
20262 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
20263 if (!incomplete && CHECKING_P
20264 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
20265 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
20266 (targs, NUM_TMPL_ARGS (explicit_targs));
20267 }
20268
20269 if (return_type && strict != DEDUCE_CALL)
20270 {
20271 tree *new_args = XALLOCAVEC (tree, nargs + 1);
20272 new_args[0] = return_type;
20273 memcpy (new_args + 1, args, nargs * sizeof (tree));
20274 args = new_args;
20275 ++nargs;
20276 }
20277
20278 if (!incomplete)
20279 goto deduced;
20280
20281 /* Never do unification on the 'this' parameter. */
20282 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
20283
20284 if (return_type && strict == DEDUCE_CALL)
20285 {
20286 /* We're deducing for a call to the result of a template conversion
20287 function. The parms we really want are in return_type. */
20288 if (INDIRECT_TYPE_P (return_type))
20289 return_type = TREE_TYPE (return_type);
20290 parms = TYPE_ARG_TYPES (return_type);
20291 }
20292 else if (return_type)
20293 {
20294 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
20295 }
20296
20297 /* We allow incomplete unification without an error message here
20298 because the standard doesn't seem to explicitly prohibit it. Our
20299 callers must be ready to deal with unification failures in any
20300 event. */
20301
20302 /* If we aren't explaining yet, push tinst context so we can see where
20303 any errors (e.g. from class instantiations triggered by instantiation
20304 of default template arguments) come from. If we are explaining, this
20305 context is redundant. */
20306 if (!explain_p && !push_tinst_level (fn, targs))
20307 {
20308 excessive_deduction_depth = true;
20309 goto fail;
20310 }
20311
20312 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
20313 full_targs, parms, args, nargs, /*subr=*/0,
20314 strict, &checks, explain_p);
20315 if (!explain_p)
20316 pop_tinst_level ();
20317 if (!ok)
20318 goto fail;
20319
20320 /* Now that we have bindings for all of the template arguments,
20321 ensure that the arguments deduced for the template template
20322 parameters have compatible template parameter lists. We cannot
20323 check this property before we have deduced all template
20324 arguments, because the template parameter types of a template
20325 template parameter might depend on prior template parameters
20326 deduced after the template template parameter. The following
20327 ill-formed example illustrates this issue:
20328
20329 template<typename T, template<T> class C> void f(C<5>, T);
20330
20331 template<int N> struct X {};
20332
20333 void g() {
20334 f(X<5>(), 5l); // error: template argument deduction fails
20335 }
20336
20337 The template parameter list of 'C' depends on the template type
20338 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
20339 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
20340 time that we deduce 'C'. */
20341 if (!template_template_parm_bindings_ok_p
20342 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
20343 {
20344 unify_inconsistent_template_template_parameters (explain_p);
20345 goto fail;
20346 }
20347
20348 /* DR 1391: All parameters have args, now check non-dependent parms for
20349 convertibility. */
20350 if (check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
20351 convs, explain_p))
20352 goto fail;
20353
20354 deduced:
20355 /* All is well so far. Now, check:
20356
20357 [temp.deduct]
20358
20359 When all template arguments have been deduced, all uses of
20360 template parameters in nondeduced contexts are replaced with
20361 the corresponding deduced argument values. If the
20362 substitution results in an invalid type, as described above,
20363 type deduction fails. */
20364 if (!push_tinst_level (fn, targs))
20365 {
20366 excessive_deduction_depth = true;
20367 goto fail;
20368 }
20369
20370 /* Also collect access checks from the instantiation. */
20371 reopen_deferring_access_checks (checks);
20372
20373 decl = instantiate_template (fn, targs, complain);
20374
20375 checks = get_deferred_access_checks ();
20376 pop_deferring_access_checks ();
20377
20378 pop_tinst_level ();
20379
20380 if (decl == error_mark_node)
20381 goto fail;
20382
20383 /* Now perform any access checks encountered during substitution. */
20384 push_access_scope (decl);
20385 ok = perform_access_checks (checks, complain);
20386 pop_access_scope (decl);
20387 if (!ok)
20388 goto fail;
20389
20390 /* If we're looking for an exact match, check that what we got
20391 is indeed an exact match. It might not be if some template
20392 parameters are used in non-deduced contexts. But don't check
20393 for an exact match if we have dependent template arguments;
20394 in that case we're doing partial ordering, and we already know
20395 that we have two candidates that will provide the actual type. */
20396 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
20397 {
20398 tree substed = TREE_TYPE (decl);
20399 unsigned int i;
20400
20401 tree sarg
20402 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
20403 if (return_type)
20404 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
20405 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
20406 if (!same_type_p (args[i], TREE_VALUE (sarg)))
20407 {
20408 unify_type_mismatch (explain_p, args[i],
20409 TREE_VALUE (sarg));
20410 goto fail;
20411 }
20412 }
20413
20414 /* After doing deduction with the inherited constructor, actually return an
20415 instantiation of the inheriting constructor. */
20416 if (orig_fn != fn)
20417 decl = instantiate_template (orig_fn, targs, complain);
20418
20419 r = decl;
20420
20421 fail:
20422 --deduction_depth;
20423 if (excessive_deduction_depth)
20424 {
20425 if (deduction_depth == 0)
20426 /* Reset once we're all the way out. */
20427 excessive_deduction_depth = false;
20428 }
20429
20430 return r;
20431 }
20432
20433 /* Adjust types before performing type deduction, as described in
20434 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
20435 sections are symmetric. PARM is the type of a function parameter
20436 or the return type of the conversion function. ARG is the type of
20437 the argument passed to the call, or the type of the value
20438 initialized with the result of the conversion function.
20439 ARG_EXPR is the original argument expression, which may be null. */
20440
20441 static int
20442 maybe_adjust_types_for_deduction (unification_kind_t strict,
20443 tree* parm,
20444 tree* arg,
20445 tree arg_expr)
20446 {
20447 int result = 0;
20448
20449 switch (strict)
20450 {
20451 case DEDUCE_CALL:
20452 break;
20453
20454 case DEDUCE_CONV:
20455 /* Swap PARM and ARG throughout the remainder of this
20456 function; the handling is precisely symmetric since PARM
20457 will initialize ARG rather than vice versa. */
20458 std::swap (parm, arg);
20459 break;
20460
20461 case DEDUCE_EXACT:
20462 /* Core issue #873: Do the DR606 thing (see below) for these cases,
20463 too, but here handle it by stripping the reference from PARM
20464 rather than by adding it to ARG. */
20465 if (TYPE_REF_P (*parm)
20466 && TYPE_REF_IS_RVALUE (*parm)
20467 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
20468 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
20469 && TYPE_REF_P (*arg)
20470 && !TYPE_REF_IS_RVALUE (*arg))
20471 *parm = TREE_TYPE (*parm);
20472 /* Nothing else to do in this case. */
20473 return 0;
20474
20475 default:
20476 gcc_unreachable ();
20477 }
20478
20479 if (!TYPE_REF_P (*parm))
20480 {
20481 /* [temp.deduct.call]
20482
20483 If P is not a reference type:
20484
20485 --If A is an array type, the pointer type produced by the
20486 array-to-pointer standard conversion (_conv.array_) is
20487 used in place of A for type deduction; otherwise,
20488
20489 --If A is a function type, the pointer type produced by
20490 the function-to-pointer standard conversion
20491 (_conv.func_) is used in place of A for type deduction;
20492 otherwise,
20493
20494 --If A is a cv-qualified type, the top level
20495 cv-qualifiers of A's type are ignored for type
20496 deduction. */
20497 if (TREE_CODE (*arg) == ARRAY_TYPE)
20498 *arg = build_pointer_type (TREE_TYPE (*arg));
20499 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
20500 *arg = build_pointer_type (*arg);
20501 else
20502 *arg = TYPE_MAIN_VARIANT (*arg);
20503 }
20504
20505 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
20506 reference to a cv-unqualified template parameter that does not represent a
20507 template parameter of a class template (during class template argument
20508 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
20509 an lvalue, the type "lvalue reference to A" is used in place of A for type
20510 deduction. */
20511 if (TYPE_REF_P (*parm)
20512 && TYPE_REF_IS_RVALUE (*parm)
20513 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
20514 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
20515 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
20516 && (arg_expr ? lvalue_p (arg_expr)
20517 /* try_one_overload doesn't provide an arg_expr, but
20518 functions are always lvalues. */
20519 : TREE_CODE (*arg) == FUNCTION_TYPE))
20520 *arg = build_reference_type (*arg);
20521
20522 /* [temp.deduct.call]
20523
20524 If P is a cv-qualified type, the top level cv-qualifiers
20525 of P's type are ignored for type deduction. If P is a
20526 reference type, the type referred to by P is used for
20527 type deduction. */
20528 *parm = TYPE_MAIN_VARIANT (*parm);
20529 if (TYPE_REF_P (*parm))
20530 {
20531 *parm = TREE_TYPE (*parm);
20532 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
20533 }
20534
20535 /* DR 322. For conversion deduction, remove a reference type on parm
20536 too (which has been swapped into ARG). */
20537 if (strict == DEDUCE_CONV && TYPE_REF_P (*arg))
20538 *arg = TREE_TYPE (*arg);
20539
20540 return result;
20541 }
20542
20543 /* Subroutine of fn_type_unification. PARM is a function parameter of a
20544 template which doesn't contain any deducible template parameters; check if
20545 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
20546 unify_one_argument. */
20547
20548 static int
20549 check_non_deducible_conversion (tree parm, tree arg, int strict,
20550 int flags, struct conversion **conv_p,
20551 bool explain_p)
20552 {
20553 tree type;
20554
20555 if (!TYPE_P (arg))
20556 type = TREE_TYPE (arg);
20557 else
20558 type = arg;
20559
20560 if (same_type_p (parm, type))
20561 return unify_success (explain_p);
20562
20563 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
20564 if (strict == DEDUCE_CONV)
20565 {
20566 if (can_convert_arg (type, parm, NULL_TREE, flags, complain))
20567 return unify_success (explain_p);
20568 }
20569 else if (strict != DEDUCE_EXACT)
20570 {
20571 bool ok = false;
20572 tree conv_arg = TYPE_P (arg) ? NULL_TREE : arg;
20573 if (conv_p)
20574 /* Avoid recalculating this in add_function_candidate. */
20575 ok = (*conv_p
20576 = good_conversion (parm, type, conv_arg, flags, complain));
20577 else
20578 ok = can_convert_arg (parm, type, conv_arg, flags, complain);
20579 if (ok)
20580 return unify_success (explain_p);
20581 }
20582
20583 if (strict == DEDUCE_EXACT)
20584 return unify_type_mismatch (explain_p, parm, arg);
20585 else
20586 return unify_arg_conversion (explain_p, parm, type, arg);
20587 }
20588
20589 static bool uses_deducible_template_parms (tree type);
20590
20591 /* Returns true iff the expression EXPR is one from which a template
20592 argument can be deduced. In other words, if it's an undecorated
20593 use of a template non-type parameter. */
20594
20595 static bool
20596 deducible_expression (tree expr)
20597 {
20598 /* Strip implicit conversions. */
20599 while (CONVERT_EXPR_P (expr))
20600 expr = TREE_OPERAND (expr, 0);
20601 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
20602 }
20603
20604 /* Returns true iff the array domain DOMAIN uses a template parameter in a
20605 deducible way; that is, if it has a max value of <PARM> - 1. */
20606
20607 static bool
20608 deducible_array_bound (tree domain)
20609 {
20610 if (domain == NULL_TREE)
20611 return false;
20612
20613 tree max = TYPE_MAX_VALUE (domain);
20614 if (TREE_CODE (max) != MINUS_EXPR)
20615 return false;
20616
20617 return deducible_expression (TREE_OPERAND (max, 0));
20618 }
20619
20620 /* Returns true iff the template arguments ARGS use a template parameter
20621 in a deducible way. */
20622
20623 static bool
20624 deducible_template_args (tree args)
20625 {
20626 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
20627 {
20628 bool deducible;
20629 tree elt = TREE_VEC_ELT (args, i);
20630 if (ARGUMENT_PACK_P (elt))
20631 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
20632 else
20633 {
20634 if (PACK_EXPANSION_P (elt))
20635 elt = PACK_EXPANSION_PATTERN (elt);
20636 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
20637 deducible = true;
20638 else if (TYPE_P (elt))
20639 deducible = uses_deducible_template_parms (elt);
20640 else
20641 deducible = deducible_expression (elt);
20642 }
20643 if (deducible)
20644 return true;
20645 }
20646 return false;
20647 }
20648
20649 /* Returns true iff TYPE contains any deducible references to template
20650 parameters, as per 14.8.2.5. */
20651
20652 static bool
20653 uses_deducible_template_parms (tree type)
20654 {
20655 if (PACK_EXPANSION_P (type))
20656 type = PACK_EXPANSION_PATTERN (type);
20657
20658 /* T
20659 cv-list T
20660 TT<T>
20661 TT<i>
20662 TT<> */
20663 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
20664 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
20665 return true;
20666
20667 /* T*
20668 T&
20669 T&& */
20670 if (INDIRECT_TYPE_P (type))
20671 return uses_deducible_template_parms (TREE_TYPE (type));
20672
20673 /* T[integer-constant ]
20674 type [i] */
20675 if (TREE_CODE (type) == ARRAY_TYPE)
20676 return (uses_deducible_template_parms (TREE_TYPE (type))
20677 || deducible_array_bound (TYPE_DOMAIN (type)));
20678
20679 /* T type ::*
20680 type T::*
20681 T T::*
20682 T (type ::*)()
20683 type (T::*)()
20684 type (type ::*)(T)
20685 type (T::*)(T)
20686 T (type ::*)(T)
20687 T (T::*)()
20688 T (T::*)(T) */
20689 if (TYPE_PTRMEM_P (type))
20690 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
20691 || (uses_deducible_template_parms
20692 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
20693
20694 /* template-name <T> (where template-name refers to a class template)
20695 template-name <i> (where template-name refers to a class template) */
20696 if (CLASS_TYPE_P (type)
20697 && CLASSTYPE_TEMPLATE_INFO (type)
20698 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
20699 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
20700 (CLASSTYPE_TI_ARGS (type)));
20701
20702 /* type (T)
20703 T()
20704 T(T) */
20705 if (FUNC_OR_METHOD_TYPE_P (type))
20706 {
20707 if (uses_deducible_template_parms (TREE_TYPE (type)))
20708 return true;
20709 tree parm = TYPE_ARG_TYPES (type);
20710 if (TREE_CODE (type) == METHOD_TYPE)
20711 parm = TREE_CHAIN (parm);
20712 for (; parm; parm = TREE_CHAIN (parm))
20713 if (uses_deducible_template_parms (TREE_VALUE (parm)))
20714 return true;
20715 }
20716
20717 return false;
20718 }
20719
20720 /* Subroutine of type_unification_real and unify_pack_expansion to
20721 handle unification of a single P/A pair. Parameters are as
20722 for those functions. */
20723
20724 static int
20725 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
20726 int subr, unification_kind_t strict,
20727 bool explain_p)
20728 {
20729 tree arg_expr = NULL_TREE;
20730 int arg_strict;
20731
20732 if (arg == error_mark_node || parm == error_mark_node)
20733 return unify_invalid (explain_p);
20734 if (arg == unknown_type_node)
20735 /* We can't deduce anything from this, but we might get all the
20736 template args from other function args. */
20737 return unify_success (explain_p);
20738
20739 /* Implicit conversions (Clause 4) will be performed on a function
20740 argument to convert it to the type of the corresponding function
20741 parameter if the parameter type contains no template-parameters that
20742 participate in template argument deduction. */
20743 if (strict != DEDUCE_EXACT
20744 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
20745 /* For function parameters with no deducible template parameters,
20746 just return. We'll check non-dependent conversions later. */
20747 return unify_success (explain_p);
20748
20749 switch (strict)
20750 {
20751 case DEDUCE_CALL:
20752 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
20753 | UNIFY_ALLOW_MORE_CV_QUAL
20754 | UNIFY_ALLOW_DERIVED);
20755 break;
20756
20757 case DEDUCE_CONV:
20758 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
20759 break;
20760
20761 case DEDUCE_EXACT:
20762 arg_strict = UNIFY_ALLOW_NONE;
20763 break;
20764
20765 default:
20766 gcc_unreachable ();
20767 }
20768
20769 /* We only do these transformations if this is the top-level
20770 parameter_type_list in a call or declaration matching; in other
20771 situations (nested function declarators, template argument lists) we
20772 won't be comparing a type to an expression, and we don't do any type
20773 adjustments. */
20774 if (!subr)
20775 {
20776 if (!TYPE_P (arg))
20777 {
20778 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
20779 if (type_unknown_p (arg))
20780 {
20781 /* [temp.deduct.type] A template-argument can be
20782 deduced from a pointer to function or pointer
20783 to member function argument if the set of
20784 overloaded functions does not contain function
20785 templates and at most one of a set of
20786 overloaded functions provides a unique
20787 match. */
20788 resolve_overloaded_unification (tparms, targs, parm,
20789 arg, strict,
20790 arg_strict, explain_p);
20791 /* If a unique match was not found, this is a
20792 non-deduced context, so we still succeed. */
20793 return unify_success (explain_p);
20794 }
20795
20796 arg_expr = arg;
20797 arg = unlowered_expr_type (arg);
20798 if (arg == error_mark_node)
20799 return unify_invalid (explain_p);
20800 }
20801
20802 arg_strict |=
20803 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
20804 }
20805 else
20806 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
20807 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
20808 return unify_template_argument_mismatch (explain_p, parm, arg);
20809
20810 /* For deduction from an init-list we need the actual list. */
20811 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
20812 arg = arg_expr;
20813 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
20814 }
20815
20816 /* for_each_template_parm callback that always returns 0. */
20817
20818 static int
20819 zero_r (tree, void *)
20820 {
20821 return 0;
20822 }
20823
20824 /* for_each_template_parm any_fn callback to handle deduction of a template
20825 type argument from the type of an array bound. */
20826
20827 static int
20828 array_deduction_r (tree t, void *data)
20829 {
20830 tree_pair_p d = (tree_pair_p)data;
20831 tree &tparms = d->purpose;
20832 tree &targs = d->value;
20833
20834 if (TREE_CODE (t) == ARRAY_TYPE)
20835 if (tree dom = TYPE_DOMAIN (t))
20836 if (tree max = TYPE_MAX_VALUE (dom))
20837 {
20838 if (TREE_CODE (max) == MINUS_EXPR)
20839 max = TREE_OPERAND (max, 0);
20840 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
20841 unify (tparms, targs, TREE_TYPE (max), size_type_node,
20842 UNIFY_ALLOW_NONE, /*explain*/false);
20843 }
20844
20845 /* Keep walking. */
20846 return 0;
20847 }
20848
20849 /* Try to deduce any not-yet-deduced template type arguments from the type of
20850 an array bound. This is handled separately from unify because 14.8.2.5 says
20851 "The type of a type parameter is only deduced from an array bound if it is
20852 not otherwise deduced." */
20853
20854 static void
20855 try_array_deduction (tree tparms, tree targs, tree parm)
20856 {
20857 tree_pair_s data = { tparms, targs };
20858 hash_set<tree> visited;
20859 for_each_template_parm (parm, zero_r, &data, &visited,
20860 /*nondeduced*/false, array_deduction_r);
20861 }
20862
20863 /* Most parms like fn_type_unification.
20864
20865 If SUBR is 1, we're being called recursively (to unify the
20866 arguments of a function or method parameter of a function
20867 template).
20868
20869 CHECKS is a pointer to a vector of access checks encountered while
20870 substituting default template arguments. */
20871
20872 static int
20873 type_unification_real (tree tparms,
20874 tree full_targs,
20875 tree xparms,
20876 const tree *xargs,
20877 unsigned int xnargs,
20878 int subr,
20879 unification_kind_t strict,
20880 vec<deferred_access_check, va_gc> **checks,
20881 bool explain_p)
20882 {
20883 tree parm, arg;
20884 int i;
20885 int ntparms = TREE_VEC_LENGTH (tparms);
20886 int saw_undeduced = 0;
20887 tree parms;
20888 const tree *args;
20889 unsigned int nargs;
20890 unsigned int ia;
20891
20892 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
20893 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
20894 gcc_assert (ntparms > 0);
20895
20896 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
20897
20898 /* Reset the number of non-defaulted template arguments contained
20899 in TARGS. */
20900 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
20901
20902 again:
20903 parms = xparms;
20904 args = xargs;
20905 nargs = xnargs;
20906
20907 ia = 0;
20908 while (parms && parms != void_list_node
20909 && ia < nargs)
20910 {
20911 parm = TREE_VALUE (parms);
20912
20913 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
20914 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
20915 /* For a function parameter pack that occurs at the end of the
20916 parameter-declaration-list, the type A of each remaining
20917 argument of the call is compared with the type P of the
20918 declarator-id of the function parameter pack. */
20919 break;
20920
20921 parms = TREE_CHAIN (parms);
20922
20923 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
20924 /* For a function parameter pack that does not occur at the
20925 end of the parameter-declaration-list, the type of the
20926 parameter pack is a non-deduced context. */
20927 continue;
20928
20929 arg = args[ia];
20930 ++ia;
20931
20932 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
20933 explain_p))
20934 return 1;
20935 }
20936
20937 if (parms
20938 && parms != void_list_node
20939 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
20940 {
20941 /* Unify the remaining arguments with the pack expansion type. */
20942 tree argvec;
20943 tree parmvec = make_tree_vec (1);
20944
20945 /* Allocate a TREE_VEC and copy in all of the arguments */
20946 argvec = make_tree_vec (nargs - ia);
20947 for (i = 0; ia < nargs; ++ia, ++i)
20948 TREE_VEC_ELT (argvec, i) = args[ia];
20949
20950 /* Copy the parameter into parmvec. */
20951 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
20952 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
20953 /*subr=*/subr, explain_p))
20954 return 1;
20955
20956 /* Advance to the end of the list of parameters. */
20957 parms = TREE_CHAIN (parms);
20958 }
20959
20960 /* Fail if we've reached the end of the parm list, and more args
20961 are present, and the parm list isn't variadic. */
20962 if (ia < nargs && parms == void_list_node)
20963 return unify_too_many_arguments (explain_p, nargs, ia);
20964 /* Fail if parms are left and they don't have default values and
20965 they aren't all deduced as empty packs (c++/57397). This is
20966 consistent with sufficient_parms_p. */
20967 if (parms && parms != void_list_node
20968 && TREE_PURPOSE (parms) == NULL_TREE)
20969 {
20970 unsigned int count = nargs;
20971 tree p = parms;
20972 bool type_pack_p;
20973 do
20974 {
20975 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
20976 if (!type_pack_p)
20977 count++;
20978 p = TREE_CHAIN (p);
20979 }
20980 while (p && p != void_list_node);
20981 if (count != nargs)
20982 return unify_too_few_arguments (explain_p, ia, count,
20983 type_pack_p);
20984 }
20985
20986 if (!subr)
20987 {
20988 tsubst_flags_t complain = (explain_p
20989 ? tf_warning_or_error
20990 : tf_none);
20991 bool tried_array_deduction = (cxx_dialect < cxx17);
20992
20993 for (i = 0; i < ntparms; i++)
20994 {
20995 tree targ = TREE_VEC_ELT (targs, i);
20996 tree tparm = TREE_VEC_ELT (tparms, i);
20997
20998 /* Clear the "incomplete" flags on all argument packs now so that
20999 substituting them into later default arguments works. */
21000 if (targ && ARGUMENT_PACK_P (targ))
21001 {
21002 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
21003 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
21004 }
21005
21006 if (targ || tparm == error_mark_node)
21007 continue;
21008 tparm = TREE_VALUE (tparm);
21009
21010 if (TREE_CODE (tparm) == TYPE_DECL
21011 && !tried_array_deduction)
21012 {
21013 try_array_deduction (tparms, targs, xparms);
21014 tried_array_deduction = true;
21015 if (TREE_VEC_ELT (targs, i))
21016 continue;
21017 }
21018
21019 /* If this is an undeduced nontype parameter that depends on
21020 a type parameter, try another pass; its type may have been
21021 deduced from a later argument than the one from which
21022 this parameter can be deduced. */
21023 if (TREE_CODE (tparm) == PARM_DECL
21024 && uses_template_parms (TREE_TYPE (tparm))
21025 && saw_undeduced < 2)
21026 {
21027 saw_undeduced = 1;
21028 continue;
21029 }
21030
21031 /* Core issue #226 (C++0x) [temp.deduct]:
21032
21033 If a template argument has not been deduced, its
21034 default template argument, if any, is used.
21035
21036 When we are in C++98 mode, TREE_PURPOSE will either
21037 be NULL_TREE or ERROR_MARK_NODE, so we do not need
21038 to explicitly check cxx_dialect here. */
21039 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
21040 /* OK, there is a default argument. Wait until after the
21041 conversion check to do substitution. */
21042 continue;
21043
21044 /* If the type parameter is a parameter pack, then it will
21045 be deduced to an empty parameter pack. */
21046 if (template_parameter_pack_p (tparm))
21047 {
21048 tree arg;
21049
21050 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
21051 {
21052 arg = make_node (NONTYPE_ARGUMENT_PACK);
21053 TREE_CONSTANT (arg) = 1;
21054 }
21055 else
21056 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
21057
21058 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
21059
21060 TREE_VEC_ELT (targs, i) = arg;
21061 continue;
21062 }
21063
21064 return unify_parameter_deduction_failure (explain_p, tparm);
21065 }
21066
21067 /* Now substitute into the default template arguments. */
21068 for (i = 0; i < ntparms; i++)
21069 {
21070 tree targ = TREE_VEC_ELT (targs, i);
21071 tree tparm = TREE_VEC_ELT (tparms, i);
21072
21073 if (targ || tparm == error_mark_node)
21074 continue;
21075 tree parm = TREE_VALUE (tparm);
21076 tree arg = TREE_PURPOSE (tparm);
21077 reopen_deferring_access_checks (*checks);
21078 location_t save_loc = input_location;
21079 if (DECL_P (parm))
21080 input_location = DECL_SOURCE_LOCATION (parm);
21081
21082 if (saw_undeduced == 1
21083 && TREE_CODE (parm) == PARM_DECL
21084 && uses_template_parms (TREE_TYPE (parm)))
21085 {
21086 /* The type of this non-type parameter depends on undeduced
21087 parameters. Don't try to use its default argument yet,
21088 since we might deduce an argument for it on the next pass,
21089 but do check whether the arguments we already have cause
21090 substitution failure, so that that happens before we try
21091 later default arguments (78489). */
21092 ++processing_template_decl;
21093 tree type = tsubst (TREE_TYPE (parm), full_targs, complain,
21094 NULL_TREE);
21095 --processing_template_decl;
21096 if (type == error_mark_node)
21097 arg = error_mark_node;
21098 else
21099 arg = NULL_TREE;
21100 }
21101 else
21102 {
21103 /* Even if the call is happening in template context, getting
21104 here means it's non-dependent, and a default argument is
21105 considered a separate definition under [temp.decls], so we can
21106 do this substitution without processing_template_decl. This
21107 is important if the default argument contains something that
21108 might be instantiation-dependent like access (87480). */
21109 processing_template_decl_sentinel s;
21110 tree substed = NULL_TREE;
21111 if (saw_undeduced == 1)
21112 {
21113 /* First instatiate in template context, in case we still
21114 depend on undeduced template parameters. */
21115 ++processing_template_decl;
21116 substed = tsubst_template_arg (arg, full_targs, complain,
21117 NULL_TREE);
21118 --processing_template_decl;
21119 if (substed != error_mark_node
21120 && !uses_template_parms (substed))
21121 /* We replaced all the tparms, substitute again out of
21122 template context. */
21123 substed = NULL_TREE;
21124 }
21125 if (!substed)
21126 substed = tsubst_template_arg (arg, full_targs, complain,
21127 NULL_TREE);
21128
21129 if (!uses_template_parms (substed))
21130 arg = convert_template_argument (parm, substed, full_targs,
21131 complain, i, NULL_TREE);
21132 else if (saw_undeduced == 1)
21133 arg = NULL_TREE;
21134 else
21135 arg = error_mark_node;
21136 }
21137
21138 input_location = save_loc;
21139 *checks = get_deferred_access_checks ();
21140 pop_deferring_access_checks ();
21141
21142 if (arg == error_mark_node)
21143 return 1;
21144 else if (arg)
21145 {
21146 TREE_VEC_ELT (targs, i) = arg;
21147 /* The position of the first default template argument,
21148 is also the number of non-defaulted arguments in TARGS.
21149 Record that. */
21150 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
21151 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
21152 }
21153 }
21154
21155 if (saw_undeduced++ == 1)
21156 goto again;
21157 }
21158
21159 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
21160 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
21161
21162 return unify_success (explain_p);
21163 }
21164
21165 /* Subroutine of type_unification_real. Args are like the variables
21166 at the call site. ARG is an overloaded function (or template-id);
21167 we try deducing template args from each of the overloads, and if
21168 only one succeeds, we go with that. Modifies TARGS and returns
21169 true on success. */
21170
21171 static bool
21172 resolve_overloaded_unification (tree tparms,
21173 tree targs,
21174 tree parm,
21175 tree arg,
21176 unification_kind_t strict,
21177 int sub_strict,
21178 bool explain_p)
21179 {
21180 tree tempargs = copy_node (targs);
21181 int good = 0;
21182 tree goodfn = NULL_TREE;
21183 bool addr_p;
21184
21185 if (TREE_CODE (arg) == ADDR_EXPR)
21186 {
21187 arg = TREE_OPERAND (arg, 0);
21188 addr_p = true;
21189 }
21190 else
21191 addr_p = false;
21192
21193 if (TREE_CODE (arg) == COMPONENT_REF)
21194 /* Handle `&x' where `x' is some static or non-static member
21195 function name. */
21196 arg = TREE_OPERAND (arg, 1);
21197
21198 if (TREE_CODE (arg) == OFFSET_REF)
21199 arg = TREE_OPERAND (arg, 1);
21200
21201 /* Strip baselink information. */
21202 if (BASELINK_P (arg))
21203 arg = BASELINK_FUNCTIONS (arg);
21204
21205 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
21206 {
21207 /* If we got some explicit template args, we need to plug them into
21208 the affected templates before we try to unify, in case the
21209 explicit args will completely resolve the templates in question. */
21210
21211 int ok = 0;
21212 tree expl_subargs = TREE_OPERAND (arg, 1);
21213 arg = TREE_OPERAND (arg, 0);
21214
21215 for (lkp_iterator iter (arg); iter; ++iter)
21216 {
21217 tree fn = *iter;
21218 tree subargs, elem;
21219
21220 if (TREE_CODE (fn) != TEMPLATE_DECL)
21221 continue;
21222
21223 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
21224 expl_subargs, NULL_TREE, tf_none,
21225 /*require_all_args=*/true,
21226 /*use_default_args=*/true);
21227 if (subargs != error_mark_node
21228 && !any_dependent_template_arguments_p (subargs))
21229 {
21230 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
21231 if (try_one_overload (tparms, targs, tempargs, parm,
21232 elem, strict, sub_strict, addr_p, explain_p)
21233 && (!goodfn || !same_type_p (goodfn, elem)))
21234 {
21235 goodfn = elem;
21236 ++good;
21237 }
21238 }
21239 else if (subargs)
21240 ++ok;
21241 }
21242 /* If no templates (or more than one) are fully resolved by the
21243 explicit arguments, this template-id is a non-deduced context; it
21244 could still be OK if we deduce all template arguments for the
21245 enclosing call through other arguments. */
21246 if (good != 1)
21247 good = ok;
21248 }
21249 else if (!OVL_P (arg))
21250 /* If ARG is, for example, "(0, &f)" then its type will be unknown
21251 -- but the deduction does not succeed because the expression is
21252 not just the function on its own. */
21253 return false;
21254 else
21255 for (lkp_iterator iter (arg); iter; ++iter)
21256 {
21257 tree fn = *iter;
21258 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
21259 strict, sub_strict, addr_p, explain_p)
21260 && (!goodfn || !decls_match (goodfn, fn)))
21261 {
21262 goodfn = fn;
21263 ++good;
21264 }
21265 }
21266
21267 /* [temp.deduct.type] A template-argument can be deduced from a pointer
21268 to function or pointer to member function argument if the set of
21269 overloaded functions does not contain function templates and at most
21270 one of a set of overloaded functions provides a unique match.
21271
21272 So if we found multiple possibilities, we return success but don't
21273 deduce anything. */
21274
21275 if (good == 1)
21276 {
21277 int i = TREE_VEC_LENGTH (targs);
21278 for (; i--; )
21279 if (TREE_VEC_ELT (tempargs, i))
21280 {
21281 tree old = TREE_VEC_ELT (targs, i);
21282 tree new_ = TREE_VEC_ELT (tempargs, i);
21283 if (new_ && old && ARGUMENT_PACK_P (old)
21284 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
21285 /* Don't forget explicit template arguments in a pack. */
21286 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
21287 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
21288 TREE_VEC_ELT (targs, i) = new_;
21289 }
21290 }
21291 if (good)
21292 return true;
21293
21294 return false;
21295 }
21296
21297 /* Core DR 115: In contexts where deduction is done and fails, or in
21298 contexts where deduction is not done, if a template argument list is
21299 specified and it, along with any default template arguments, identifies
21300 a single function template specialization, then the template-id is an
21301 lvalue for the function template specialization. */
21302
21303 tree
21304 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
21305 {
21306 tree expr, offset, baselink;
21307 bool addr;
21308
21309 if (!type_unknown_p (orig_expr))
21310 return orig_expr;
21311
21312 expr = orig_expr;
21313 addr = false;
21314 offset = NULL_TREE;
21315 baselink = NULL_TREE;
21316
21317 if (TREE_CODE (expr) == ADDR_EXPR)
21318 {
21319 expr = TREE_OPERAND (expr, 0);
21320 addr = true;
21321 }
21322 if (TREE_CODE (expr) == OFFSET_REF)
21323 {
21324 offset = expr;
21325 expr = TREE_OPERAND (expr, 1);
21326 }
21327 if (BASELINK_P (expr))
21328 {
21329 baselink = expr;
21330 expr = BASELINK_FUNCTIONS (expr);
21331 }
21332
21333 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
21334 {
21335 int good = 0;
21336 tree goodfn = NULL_TREE;
21337
21338 /* If we got some explicit template args, we need to plug them into
21339 the affected templates before we try to unify, in case the
21340 explicit args will completely resolve the templates in question. */
21341
21342 tree expl_subargs = TREE_OPERAND (expr, 1);
21343 tree arg = TREE_OPERAND (expr, 0);
21344 tree badfn = NULL_TREE;
21345 tree badargs = NULL_TREE;
21346
21347 for (lkp_iterator iter (arg); iter; ++iter)
21348 {
21349 tree fn = *iter;
21350 tree subargs, elem;
21351
21352 if (TREE_CODE (fn) != TEMPLATE_DECL)
21353 continue;
21354
21355 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
21356 expl_subargs, NULL_TREE, tf_none,
21357 /*require_all_args=*/true,
21358 /*use_default_args=*/true);
21359 if (subargs != error_mark_node
21360 && !any_dependent_template_arguments_p (subargs))
21361 {
21362 elem = instantiate_template (fn, subargs, tf_none);
21363 if (elem == error_mark_node)
21364 {
21365 badfn = fn;
21366 badargs = subargs;
21367 }
21368 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
21369 {
21370 goodfn = elem;
21371 ++good;
21372 }
21373 }
21374 }
21375 if (good == 1)
21376 {
21377 mark_used (goodfn);
21378 expr = goodfn;
21379 if (baselink)
21380 expr = build_baselink (BASELINK_BINFO (baselink),
21381 BASELINK_ACCESS_BINFO (baselink),
21382 expr, BASELINK_OPTYPE (baselink));
21383 if (offset)
21384 {
21385 tree base
21386 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
21387 expr = build_offset_ref (base, expr, addr, complain);
21388 }
21389 if (addr)
21390 expr = cp_build_addr_expr (expr, complain);
21391 return expr;
21392 }
21393 else if (good == 0 && badargs && (complain & tf_error))
21394 /* There were no good options and at least one bad one, so let the
21395 user know what the problem is. */
21396 instantiate_template (badfn, badargs, complain);
21397 }
21398 return orig_expr;
21399 }
21400
21401 /* As above, but error out if the expression remains overloaded. */
21402
21403 tree
21404 resolve_nondeduced_context_or_error (tree exp, tsubst_flags_t complain)
21405 {
21406 exp = resolve_nondeduced_context (exp, complain);
21407 if (type_unknown_p (exp))
21408 {
21409 if (complain & tf_error)
21410 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
21411 return error_mark_node;
21412 }
21413 return exp;
21414 }
21415
21416 /* Subroutine of resolve_overloaded_unification; does deduction for a single
21417 overload. Fills TARGS with any deduced arguments, or error_mark_node if
21418 different overloads deduce different arguments for a given parm.
21419 ADDR_P is true if the expression for which deduction is being
21420 performed was of the form "& fn" rather than simply "fn".
21421
21422 Returns 1 on success. */
21423
21424 static int
21425 try_one_overload (tree tparms,
21426 tree orig_targs,
21427 tree targs,
21428 tree parm,
21429 tree arg,
21430 unification_kind_t strict,
21431 int sub_strict,
21432 bool addr_p,
21433 bool explain_p)
21434 {
21435 int nargs;
21436 tree tempargs;
21437 int i;
21438
21439 if (arg == error_mark_node)
21440 return 0;
21441
21442 /* [temp.deduct.type] A template-argument can be deduced from a pointer
21443 to function or pointer to member function argument if the set of
21444 overloaded functions does not contain function templates and at most
21445 one of a set of overloaded functions provides a unique match.
21446
21447 So if this is a template, just return success. */
21448
21449 if (uses_template_parms (arg))
21450 return 1;
21451
21452 if (TREE_CODE (arg) == METHOD_TYPE)
21453 arg = build_ptrmemfunc_type (build_pointer_type (arg));
21454 else if (addr_p)
21455 arg = build_pointer_type (arg);
21456
21457 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
21458
21459 /* We don't copy orig_targs for this because if we have already deduced
21460 some template args from previous args, unify would complain when we
21461 try to deduce a template parameter for the same argument, even though
21462 there isn't really a conflict. */
21463 nargs = TREE_VEC_LENGTH (targs);
21464 tempargs = make_tree_vec (nargs);
21465
21466 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
21467 return 0;
21468
21469 /* First make sure we didn't deduce anything that conflicts with
21470 explicitly specified args. */
21471 for (i = nargs; i--; )
21472 {
21473 tree elt = TREE_VEC_ELT (tempargs, i);
21474 tree oldelt = TREE_VEC_ELT (orig_targs, i);
21475
21476 if (!elt)
21477 /*NOP*/;
21478 else if (uses_template_parms (elt))
21479 /* Since we're unifying against ourselves, we will fill in
21480 template args used in the function parm list with our own
21481 template parms. Discard them. */
21482 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
21483 else if (oldelt && ARGUMENT_PACK_P (oldelt))
21484 {
21485 /* Check that the argument at each index of the deduced argument pack
21486 is equivalent to the corresponding explicitly specified argument.
21487 We may have deduced more arguments than were explicitly specified,
21488 and that's OK. */
21489
21490 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
21491 that's wrong if we deduce the same argument pack from multiple
21492 function arguments: it's only incomplete the first time. */
21493
21494 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
21495 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
21496
21497 if (TREE_VEC_LENGTH (deduced_pack)
21498 < TREE_VEC_LENGTH (explicit_pack))
21499 return 0;
21500
21501 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
21502 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
21503 TREE_VEC_ELT (deduced_pack, j)))
21504 return 0;
21505 }
21506 else if (oldelt && !template_args_equal (oldelt, elt))
21507 return 0;
21508 }
21509
21510 for (i = nargs; i--; )
21511 {
21512 tree elt = TREE_VEC_ELT (tempargs, i);
21513
21514 if (elt)
21515 TREE_VEC_ELT (targs, i) = elt;
21516 }
21517
21518 return 1;
21519 }
21520
21521 /* PARM is a template class (perhaps with unbound template
21522 parameters). ARG is a fully instantiated type. If ARG can be
21523 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
21524 TARGS are as for unify. */
21525
21526 static tree
21527 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
21528 bool explain_p)
21529 {
21530 tree copy_of_targs;
21531
21532 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
21533 return NULL_TREE;
21534 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21535 /* Matches anything. */;
21536 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
21537 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
21538 return NULL_TREE;
21539
21540 /* We need to make a new template argument vector for the call to
21541 unify. If we used TARGS, we'd clutter it up with the result of
21542 the attempted unification, even if this class didn't work out.
21543 We also don't want to commit ourselves to all the unifications
21544 we've already done, since unification is supposed to be done on
21545 an argument-by-argument basis. In other words, consider the
21546 following pathological case:
21547
21548 template <int I, int J, int K>
21549 struct S {};
21550
21551 template <int I, int J>
21552 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
21553
21554 template <int I, int J, int K>
21555 void f(S<I, J, K>, S<I, I, I>);
21556
21557 void g() {
21558 S<0, 0, 0> s0;
21559 S<0, 1, 2> s2;
21560
21561 f(s0, s2);
21562 }
21563
21564 Now, by the time we consider the unification involving `s2', we
21565 already know that we must have `f<0, 0, 0>'. But, even though
21566 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
21567 because there are two ways to unify base classes of S<0, 1, 2>
21568 with S<I, I, I>. If we kept the already deduced knowledge, we
21569 would reject the possibility I=1. */
21570 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
21571
21572 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21573 {
21574 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
21575 return NULL_TREE;
21576 return arg;
21577 }
21578
21579 /* If unification failed, we're done. */
21580 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
21581 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
21582 return NULL_TREE;
21583
21584 return arg;
21585 }
21586
21587 /* Given a template type PARM and a class type ARG, find the unique
21588 base type in ARG that is an instance of PARM. We do not examine
21589 ARG itself; only its base-classes. If there is not exactly one
21590 appropriate base class, return NULL_TREE. PARM may be the type of
21591 a partial specialization, as well as a plain template type. Used
21592 by unify. */
21593
21594 static enum template_base_result
21595 get_template_base (tree tparms, tree targs, tree parm, tree arg,
21596 bool explain_p, tree *result)
21597 {
21598 tree rval = NULL_TREE;
21599 tree binfo;
21600
21601 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
21602
21603 binfo = TYPE_BINFO (complete_type (arg));
21604 if (!binfo)
21605 {
21606 /* The type could not be completed. */
21607 *result = NULL_TREE;
21608 return tbr_incomplete_type;
21609 }
21610
21611 /* Walk in inheritance graph order. The search order is not
21612 important, and this avoids multiple walks of virtual bases. */
21613 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
21614 {
21615 tree r = try_class_unification (tparms, targs, parm,
21616 BINFO_TYPE (binfo), explain_p);
21617
21618 if (r)
21619 {
21620 /* If there is more than one satisfactory baseclass, then:
21621
21622 [temp.deduct.call]
21623
21624 If they yield more than one possible deduced A, the type
21625 deduction fails.
21626
21627 applies. */
21628 if (rval && !same_type_p (r, rval))
21629 {
21630 *result = NULL_TREE;
21631 return tbr_ambiguous_baseclass;
21632 }
21633
21634 rval = r;
21635 }
21636 }
21637
21638 *result = rval;
21639 return tbr_success;
21640 }
21641
21642 /* Returns the level of DECL, which declares a template parameter. */
21643
21644 static int
21645 template_decl_level (tree decl)
21646 {
21647 switch (TREE_CODE (decl))
21648 {
21649 case TYPE_DECL:
21650 case TEMPLATE_DECL:
21651 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
21652
21653 case PARM_DECL:
21654 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
21655
21656 default:
21657 gcc_unreachable ();
21658 }
21659 return 0;
21660 }
21661
21662 /* Decide whether ARG can be unified with PARM, considering only the
21663 cv-qualifiers of each type, given STRICT as documented for unify.
21664 Returns nonzero iff the unification is OK on that basis. */
21665
21666 static int
21667 check_cv_quals_for_unify (int strict, tree arg, tree parm)
21668 {
21669 int arg_quals = cp_type_quals (arg);
21670 int parm_quals = cp_type_quals (parm);
21671
21672 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
21673 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
21674 {
21675 /* Although a CVR qualifier is ignored when being applied to a
21676 substituted template parameter ([8.3.2]/1 for example), that
21677 does not allow us to unify "const T" with "int&" because both
21678 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
21679 It is ok when we're allowing additional CV qualifiers
21680 at the outer level [14.8.2.1]/3,1st bullet. */
21681 if ((TYPE_REF_P (arg)
21682 || FUNC_OR_METHOD_TYPE_P (arg))
21683 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
21684 return 0;
21685
21686 if ((!INDIRECT_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
21687 && (parm_quals & TYPE_QUAL_RESTRICT))
21688 return 0;
21689 }
21690
21691 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
21692 && (arg_quals & parm_quals) != parm_quals)
21693 return 0;
21694
21695 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
21696 && (parm_quals & arg_quals) != arg_quals)
21697 return 0;
21698
21699 return 1;
21700 }
21701
21702 /* Determines the LEVEL and INDEX for the template parameter PARM. */
21703 void
21704 template_parm_level_and_index (tree parm, int* level, int* index)
21705 {
21706 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
21707 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
21708 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21709 {
21710 *index = TEMPLATE_TYPE_IDX (parm);
21711 *level = TEMPLATE_TYPE_LEVEL (parm);
21712 }
21713 else
21714 {
21715 *index = TEMPLATE_PARM_IDX (parm);
21716 *level = TEMPLATE_PARM_LEVEL (parm);
21717 }
21718 }
21719
21720 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
21721 do { \
21722 if (unify (TP, TA, P, A, S, EP)) \
21723 return 1; \
21724 } while (0)
21725
21726 /* Unifies the remaining arguments in PACKED_ARGS with the pack
21727 expansion at the end of PACKED_PARMS. Returns 0 if the type
21728 deduction succeeds, 1 otherwise. STRICT is the same as in
21729 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
21730 function call argument list. We'll need to adjust the arguments to make them
21731 types. SUBR tells us if this is from a recursive call to
21732 type_unification_real, or for comparing two template argument
21733 lists. */
21734
21735 static int
21736 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
21737 tree packed_args, unification_kind_t strict,
21738 bool subr, bool explain_p)
21739 {
21740 tree parm
21741 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
21742 tree pattern = PACK_EXPANSION_PATTERN (parm);
21743 tree pack, packs = NULL_TREE;
21744 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
21745
21746 /* Add in any args remembered from an earlier partial instantiation. */
21747 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
21748 int levels = TMPL_ARGS_DEPTH (targs);
21749
21750 packed_args = expand_template_argument_pack (packed_args);
21751
21752 int len = TREE_VEC_LENGTH (packed_args);
21753
21754 /* Determine the parameter packs we will be deducing from the
21755 pattern, and record their current deductions. */
21756 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
21757 pack; pack = TREE_CHAIN (pack))
21758 {
21759 tree parm_pack = TREE_VALUE (pack);
21760 int idx, level;
21761
21762 /* Only template parameter packs can be deduced, not e.g. function
21763 parameter packs or __bases or __integer_pack. */
21764 if (!TEMPLATE_PARM_P (parm_pack))
21765 continue;
21766
21767 /* Determine the index and level of this parameter pack. */
21768 template_parm_level_and_index (parm_pack, &level, &idx);
21769 if (level < levels)
21770 continue;
21771
21772 /* Keep track of the parameter packs and their corresponding
21773 argument packs. */
21774 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
21775 TREE_TYPE (packs) = make_tree_vec (len - start);
21776 }
21777
21778 /* Loop through all of the arguments that have not yet been
21779 unified and unify each with the pattern. */
21780 for (i = start; i < len; i++)
21781 {
21782 tree parm;
21783 bool any_explicit = false;
21784 tree arg = TREE_VEC_ELT (packed_args, i);
21785
21786 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
21787 or the element of its argument pack at the current index if
21788 this argument was explicitly specified. */
21789 for (pack = packs; pack; pack = TREE_CHAIN (pack))
21790 {
21791 int idx, level;
21792 tree arg, pargs;
21793 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
21794
21795 arg = NULL_TREE;
21796 if (TREE_VALUE (pack)
21797 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
21798 && (i - start < TREE_VEC_LENGTH (pargs)))
21799 {
21800 any_explicit = true;
21801 arg = TREE_VEC_ELT (pargs, i - start);
21802 }
21803 TMPL_ARG (targs, level, idx) = arg;
21804 }
21805
21806 /* If we had explicit template arguments, substitute them into the
21807 pattern before deduction. */
21808 if (any_explicit)
21809 {
21810 /* Some arguments might still be unspecified or dependent. */
21811 bool dependent;
21812 ++processing_template_decl;
21813 dependent = any_dependent_template_arguments_p (targs);
21814 if (!dependent)
21815 --processing_template_decl;
21816 parm = tsubst (pattern, targs,
21817 explain_p ? tf_warning_or_error : tf_none,
21818 NULL_TREE);
21819 if (dependent)
21820 --processing_template_decl;
21821 if (parm == error_mark_node)
21822 return 1;
21823 }
21824 else
21825 parm = pattern;
21826
21827 /* Unify the pattern with the current argument. */
21828 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
21829 explain_p))
21830 return 1;
21831
21832 /* For each parameter pack, collect the deduced value. */
21833 for (pack = packs; pack; pack = TREE_CHAIN (pack))
21834 {
21835 int idx, level;
21836 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
21837
21838 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
21839 TMPL_ARG (targs, level, idx);
21840 }
21841 }
21842
21843 /* Verify that the results of unification with the parameter packs
21844 produce results consistent with what we've seen before, and make
21845 the deduced argument packs available. */
21846 for (pack = packs; pack; pack = TREE_CHAIN (pack))
21847 {
21848 tree old_pack = TREE_VALUE (pack);
21849 tree new_args = TREE_TYPE (pack);
21850 int i, len = TREE_VEC_LENGTH (new_args);
21851 int idx, level;
21852 bool nondeduced_p = false;
21853
21854 /* By default keep the original deduced argument pack.
21855 If necessary, more specific code is going to update the
21856 resulting deduced argument later down in this function. */
21857 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
21858 TMPL_ARG (targs, level, idx) = old_pack;
21859
21860 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
21861 actually deduce anything. */
21862 for (i = 0; i < len && !nondeduced_p; ++i)
21863 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
21864 nondeduced_p = true;
21865 if (nondeduced_p)
21866 continue;
21867
21868 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
21869 {
21870 /* If we had fewer function args than explicit template args,
21871 just use the explicits. */
21872 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
21873 int explicit_len = TREE_VEC_LENGTH (explicit_args);
21874 if (len < explicit_len)
21875 new_args = explicit_args;
21876 }
21877
21878 if (!old_pack)
21879 {
21880 tree result;
21881 /* Build the deduced *_ARGUMENT_PACK. */
21882 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
21883 {
21884 result = make_node (NONTYPE_ARGUMENT_PACK);
21885 TREE_CONSTANT (result) = 1;
21886 }
21887 else
21888 result = cxx_make_type (TYPE_ARGUMENT_PACK);
21889
21890 SET_ARGUMENT_PACK_ARGS (result, new_args);
21891
21892 /* Note the deduced argument packs for this parameter
21893 pack. */
21894 TMPL_ARG (targs, level, idx) = result;
21895 }
21896 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
21897 && (ARGUMENT_PACK_ARGS (old_pack)
21898 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
21899 {
21900 /* We only had the explicitly-provided arguments before, but
21901 now we have a complete set of arguments. */
21902 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
21903
21904 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
21905 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
21906 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
21907 }
21908 else
21909 {
21910 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
21911 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
21912
21913 if (!comp_template_args (old_args, new_args,
21914 &bad_old_arg, &bad_new_arg))
21915 /* Inconsistent unification of this parameter pack. */
21916 return unify_parameter_pack_inconsistent (explain_p,
21917 bad_old_arg,
21918 bad_new_arg);
21919 }
21920 }
21921
21922 return unify_success (explain_p);
21923 }
21924
21925 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
21926 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
21927 parameters and return value are as for unify. */
21928
21929 static int
21930 unify_array_domain (tree tparms, tree targs,
21931 tree parm_dom, tree arg_dom,
21932 bool explain_p)
21933 {
21934 tree parm_max;
21935 tree arg_max;
21936 bool parm_cst;
21937 bool arg_cst;
21938
21939 /* Our representation of array types uses "N - 1" as the
21940 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
21941 not an integer constant. We cannot unify arbitrarily
21942 complex expressions, so we eliminate the MINUS_EXPRs
21943 here. */
21944 parm_max = TYPE_MAX_VALUE (parm_dom);
21945 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
21946 if (!parm_cst)
21947 {
21948 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
21949 parm_max = TREE_OPERAND (parm_max, 0);
21950 }
21951 arg_max = TYPE_MAX_VALUE (arg_dom);
21952 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
21953 if (!arg_cst)
21954 {
21955 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
21956 trying to unify the type of a variable with the type
21957 of a template parameter. For example:
21958
21959 template <unsigned int N>
21960 void f (char (&) [N]);
21961 int g();
21962 void h(int i) {
21963 char a[g(i)];
21964 f(a);
21965 }
21966
21967 Here, the type of the ARG will be "int [g(i)]", and
21968 may be a SAVE_EXPR, etc. */
21969 if (TREE_CODE (arg_max) != MINUS_EXPR)
21970 return unify_vla_arg (explain_p, arg_dom);
21971 arg_max = TREE_OPERAND (arg_max, 0);
21972 }
21973
21974 /* If only one of the bounds used a MINUS_EXPR, compensate
21975 by adding one to the other bound. */
21976 if (parm_cst && !arg_cst)
21977 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
21978 integer_type_node,
21979 parm_max,
21980 integer_one_node);
21981 else if (arg_cst && !parm_cst)
21982 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
21983 integer_type_node,
21984 arg_max,
21985 integer_one_node);
21986
21987 return unify (tparms, targs, parm_max, arg_max,
21988 UNIFY_ALLOW_INTEGER, explain_p);
21989 }
21990
21991 /* Returns whether T, a P or A in unify, is a type, template or expression. */
21992
21993 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
21994
21995 static pa_kind_t
21996 pa_kind (tree t)
21997 {
21998 if (PACK_EXPANSION_P (t))
21999 t = PACK_EXPANSION_PATTERN (t);
22000 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
22001 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
22002 || DECL_TYPE_TEMPLATE_P (t))
22003 return pa_tmpl;
22004 else if (TYPE_P (t))
22005 return pa_type;
22006 else
22007 return pa_expr;
22008 }
22009
22010 /* Deduce the value of template parameters. TPARMS is the (innermost)
22011 set of template parameters to a template. TARGS is the bindings
22012 for those template parameters, as determined thus far; TARGS may
22013 include template arguments for outer levels of template parameters
22014 as well. PARM is a parameter to a template function, or a
22015 subcomponent of that parameter; ARG is the corresponding argument.
22016 This function attempts to match PARM with ARG in a manner
22017 consistent with the existing assignments in TARGS. If more values
22018 are deduced, then TARGS is updated.
22019
22020 Returns 0 if the type deduction succeeds, 1 otherwise. The
22021 parameter STRICT is a bitwise or of the following flags:
22022
22023 UNIFY_ALLOW_NONE:
22024 Require an exact match between PARM and ARG.
22025 UNIFY_ALLOW_MORE_CV_QUAL:
22026 Allow the deduced ARG to be more cv-qualified (by qualification
22027 conversion) than ARG.
22028 UNIFY_ALLOW_LESS_CV_QUAL:
22029 Allow the deduced ARG to be less cv-qualified than ARG.
22030 UNIFY_ALLOW_DERIVED:
22031 Allow the deduced ARG to be a template base class of ARG,
22032 or a pointer to a template base class of the type pointed to by
22033 ARG.
22034 UNIFY_ALLOW_INTEGER:
22035 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
22036 case for more information.
22037 UNIFY_ALLOW_OUTER_LEVEL:
22038 This is the outermost level of a deduction. Used to determine validity
22039 of qualification conversions. A valid qualification conversion must
22040 have const qualified pointers leading up to the inner type which
22041 requires additional CV quals, except at the outer level, where const
22042 is not required [conv.qual]. It would be normal to set this flag in
22043 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
22044 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
22045 This is the outermost level of a deduction, and PARM can be more CV
22046 qualified at this point.
22047 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
22048 This is the outermost level of a deduction, and PARM can be less CV
22049 qualified at this point. */
22050
22051 static int
22052 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
22053 bool explain_p)
22054 {
22055 int idx;
22056 tree targ;
22057 tree tparm;
22058 int strict_in = strict;
22059 tsubst_flags_t complain = (explain_p
22060 ? tf_warning_or_error
22061 : tf_none);
22062
22063 /* I don't think this will do the right thing with respect to types.
22064 But the only case I've seen it in so far has been array bounds, where
22065 signedness is the only information lost, and I think that will be
22066 okay. */
22067 while (CONVERT_EXPR_P (parm))
22068 parm = TREE_OPERAND (parm, 0);
22069
22070 if (arg == error_mark_node)
22071 return unify_invalid (explain_p);
22072 if (arg == unknown_type_node
22073 || arg == init_list_type_node)
22074 /* We can't deduce anything from this, but we might get all the
22075 template args from other function args. */
22076 return unify_success (explain_p);
22077
22078 if (parm == any_targ_node || arg == any_targ_node)
22079 return unify_success (explain_p);
22080
22081 /* If PARM uses template parameters, then we can't bail out here,
22082 even if ARG == PARM, since we won't record unifications for the
22083 template parameters. We might need them if we're trying to
22084 figure out which of two things is more specialized. */
22085 if (arg == parm && !uses_template_parms (parm))
22086 return unify_success (explain_p);
22087
22088 /* Handle init lists early, so the rest of the function can assume
22089 we're dealing with a type. */
22090 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
22091 {
22092 tree elt, elttype;
22093 unsigned i;
22094 tree orig_parm = parm;
22095
22096 if (!is_std_init_list (parm)
22097 && TREE_CODE (parm) != ARRAY_TYPE)
22098 /* We can only deduce from an initializer list argument if the
22099 parameter is std::initializer_list or an array; otherwise this
22100 is a non-deduced context. */
22101 return unify_success (explain_p);
22102
22103 if (TREE_CODE (parm) == ARRAY_TYPE)
22104 elttype = TREE_TYPE (parm);
22105 else
22106 {
22107 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
22108 /* Deduction is defined in terms of a single type, so just punt
22109 on the (bizarre) std::initializer_list<T...>. */
22110 if (PACK_EXPANSION_P (elttype))
22111 return unify_success (explain_p);
22112 }
22113
22114 if (strict != DEDUCE_EXACT
22115 && TYPE_P (elttype)
22116 && !uses_deducible_template_parms (elttype))
22117 /* If ELTTYPE has no deducible template parms, skip deduction from
22118 the list elements. */;
22119 else
22120 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
22121 {
22122 int elt_strict = strict;
22123
22124 if (elt == error_mark_node)
22125 return unify_invalid (explain_p);
22126
22127 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
22128 {
22129 tree type = TREE_TYPE (elt);
22130 if (type == error_mark_node)
22131 return unify_invalid (explain_p);
22132 /* It should only be possible to get here for a call. */
22133 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
22134 elt_strict |= maybe_adjust_types_for_deduction
22135 (DEDUCE_CALL, &elttype, &type, elt);
22136 elt = type;
22137 }
22138
22139 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
22140 explain_p);
22141 }
22142
22143 if (TREE_CODE (parm) == ARRAY_TYPE
22144 && deducible_array_bound (TYPE_DOMAIN (parm)))
22145 {
22146 /* Also deduce from the length of the initializer list. */
22147 tree max = size_int (CONSTRUCTOR_NELTS (arg));
22148 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
22149 if (idx == error_mark_node)
22150 return unify_invalid (explain_p);
22151 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
22152 idx, explain_p);
22153 }
22154
22155 /* If the std::initializer_list<T> deduction worked, replace the
22156 deduced A with std::initializer_list<A>. */
22157 if (orig_parm != parm)
22158 {
22159 idx = TEMPLATE_TYPE_IDX (orig_parm);
22160 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
22161 targ = listify (targ);
22162 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
22163 }
22164 return unify_success (explain_p);
22165 }
22166
22167 /* If parm and arg aren't the same kind of thing (template, type, or
22168 expression), fail early. */
22169 if (pa_kind (parm) != pa_kind (arg))
22170 return unify_invalid (explain_p);
22171
22172 /* Immediately reject some pairs that won't unify because of
22173 cv-qualification mismatches. */
22174 if (TREE_CODE (arg) == TREE_CODE (parm)
22175 && TYPE_P (arg)
22176 /* It is the elements of the array which hold the cv quals of an array
22177 type, and the elements might be template type parms. We'll check
22178 when we recurse. */
22179 && TREE_CODE (arg) != ARRAY_TYPE
22180 /* We check the cv-qualifiers when unifying with template type
22181 parameters below. We want to allow ARG `const T' to unify with
22182 PARM `T' for example, when computing which of two templates
22183 is more specialized, for example. */
22184 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
22185 && !check_cv_quals_for_unify (strict_in, arg, parm))
22186 return unify_cv_qual_mismatch (explain_p, parm, arg);
22187
22188 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
22189 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
22190 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
22191 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
22192 strict &= ~UNIFY_ALLOW_DERIVED;
22193 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
22194 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
22195
22196 switch (TREE_CODE (parm))
22197 {
22198 case TYPENAME_TYPE:
22199 case SCOPE_REF:
22200 case UNBOUND_CLASS_TEMPLATE:
22201 /* In a type which contains a nested-name-specifier, template
22202 argument values cannot be deduced for template parameters used
22203 within the nested-name-specifier. */
22204 return unify_success (explain_p);
22205
22206 case TEMPLATE_TYPE_PARM:
22207 case TEMPLATE_TEMPLATE_PARM:
22208 case BOUND_TEMPLATE_TEMPLATE_PARM:
22209 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
22210 if (error_operand_p (tparm))
22211 return unify_invalid (explain_p);
22212
22213 if (TEMPLATE_TYPE_LEVEL (parm)
22214 != template_decl_level (tparm))
22215 /* The PARM is not one we're trying to unify. Just check
22216 to see if it matches ARG. */
22217 {
22218 if (TREE_CODE (arg) == TREE_CODE (parm)
22219 && (is_auto (parm) ? is_auto (arg)
22220 : same_type_p (parm, arg)))
22221 return unify_success (explain_p);
22222 else
22223 return unify_type_mismatch (explain_p, parm, arg);
22224 }
22225 idx = TEMPLATE_TYPE_IDX (parm);
22226 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
22227 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
22228 if (error_operand_p (tparm))
22229 return unify_invalid (explain_p);
22230
22231 /* Check for mixed types and values. */
22232 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22233 && TREE_CODE (tparm) != TYPE_DECL)
22234 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
22235 && TREE_CODE (tparm) != TEMPLATE_DECL))
22236 gcc_unreachable ();
22237
22238 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22239 {
22240 if ((strict_in & UNIFY_ALLOW_DERIVED)
22241 && CLASS_TYPE_P (arg))
22242 {
22243 /* First try to match ARG directly. */
22244 tree t = try_class_unification (tparms, targs, parm, arg,
22245 explain_p);
22246 if (!t)
22247 {
22248 /* Otherwise, look for a suitable base of ARG, as below. */
22249 enum template_base_result r;
22250 r = get_template_base (tparms, targs, parm, arg,
22251 explain_p, &t);
22252 if (!t)
22253 return unify_no_common_base (explain_p, r, parm, arg);
22254 arg = t;
22255 }
22256 }
22257 /* ARG must be constructed from a template class or a template
22258 template parameter. */
22259 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
22260 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
22261 return unify_template_deduction_failure (explain_p, parm, arg);
22262
22263 /* Deduce arguments T, i from TT<T> or TT<i>. */
22264 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
22265 return 1;
22266
22267 arg = TYPE_TI_TEMPLATE (arg);
22268
22269 /* Fall through to deduce template name. */
22270 }
22271
22272 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
22273 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22274 {
22275 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
22276
22277 /* Simple cases: Value already set, does match or doesn't. */
22278 if (targ != NULL_TREE && template_args_equal (targ, arg))
22279 return unify_success (explain_p);
22280 else if (targ)
22281 return unify_inconsistency (explain_p, parm, targ, arg);
22282 }
22283 else
22284 {
22285 /* If PARM is `const T' and ARG is only `int', we don't have
22286 a match unless we are allowing additional qualification.
22287 If ARG is `const int' and PARM is just `T' that's OK;
22288 that binds `const int' to `T'. */
22289 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
22290 arg, parm))
22291 return unify_cv_qual_mismatch (explain_p, parm, arg);
22292
22293 /* Consider the case where ARG is `const volatile int' and
22294 PARM is `const T'. Then, T should be `volatile int'. */
22295 arg = cp_build_qualified_type_real
22296 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
22297 if (arg == error_mark_node)
22298 return unify_invalid (explain_p);
22299
22300 /* Simple cases: Value already set, does match or doesn't. */
22301 if (targ != NULL_TREE && same_type_p (targ, arg))
22302 return unify_success (explain_p);
22303 else if (targ)
22304 return unify_inconsistency (explain_p, parm, targ, arg);
22305
22306 /* Make sure that ARG is not a variable-sized array. (Note
22307 that were talking about variable-sized arrays (like
22308 `int[n]'), rather than arrays of unknown size (like
22309 `int[]').) We'll get very confused by such a type since
22310 the bound of the array is not constant, and therefore
22311 not mangleable. Besides, such types are not allowed in
22312 ISO C++, so we can do as we please here. We do allow
22313 them for 'auto' deduction, since that isn't ABI-exposed. */
22314 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
22315 return unify_vla_arg (explain_p, arg);
22316
22317 /* Strip typedefs as in convert_template_argument. */
22318 arg = canonicalize_type_argument (arg, tf_none);
22319 }
22320
22321 /* If ARG is a parameter pack or an expansion, we cannot unify
22322 against it unless PARM is also a parameter pack. */
22323 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
22324 && !template_parameter_pack_p (parm))
22325 return unify_parameter_pack_mismatch (explain_p, parm, arg);
22326
22327 /* If the argument deduction results is a METHOD_TYPE,
22328 then there is a problem.
22329 METHOD_TYPE doesn't map to any real C++ type the result of
22330 the deduction cannot be of that type. */
22331 if (TREE_CODE (arg) == METHOD_TYPE)
22332 return unify_method_type_error (explain_p, arg);
22333
22334 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
22335 return unify_success (explain_p);
22336
22337 case TEMPLATE_PARM_INDEX:
22338 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
22339 if (error_operand_p (tparm))
22340 return unify_invalid (explain_p);
22341
22342 if (TEMPLATE_PARM_LEVEL (parm)
22343 != template_decl_level (tparm))
22344 {
22345 /* The PARM is not one we're trying to unify. Just check
22346 to see if it matches ARG. */
22347 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
22348 && cp_tree_equal (parm, arg));
22349 if (result)
22350 unify_expression_unequal (explain_p, parm, arg);
22351 return result;
22352 }
22353
22354 idx = TEMPLATE_PARM_IDX (parm);
22355 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
22356
22357 if (targ)
22358 {
22359 if ((strict & UNIFY_ALLOW_INTEGER)
22360 && TREE_TYPE (targ) && TREE_TYPE (arg)
22361 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
22362 /* We're deducing from an array bound, the type doesn't matter. */
22363 arg = fold_convert (TREE_TYPE (targ), arg);
22364 int x = !cp_tree_equal (targ, arg);
22365 if (x)
22366 unify_inconsistency (explain_p, parm, targ, arg);
22367 return x;
22368 }
22369
22370 /* [temp.deduct.type] If, in the declaration of a function template
22371 with a non-type template-parameter, the non-type
22372 template-parameter is used in an expression in the function
22373 parameter-list and, if the corresponding template-argument is
22374 deduced, the template-argument type shall match the type of the
22375 template-parameter exactly, except that a template-argument
22376 deduced from an array bound may be of any integral type.
22377 The non-type parameter might use already deduced type parameters. */
22378 tparm = TREE_TYPE (parm);
22379 if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs))
22380 /* We don't have enough levels of args to do any substitution. This
22381 can happen in the context of -fnew-ttp-matching. */;
22382 else
22383 {
22384 ++processing_template_decl;
22385 tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
22386 --processing_template_decl;
22387
22388 if (tree a = type_uses_auto (tparm))
22389 {
22390 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
22391 if (tparm == error_mark_node)
22392 return 1;
22393 }
22394 }
22395
22396 if (!TREE_TYPE (arg))
22397 /* Template-parameter dependent expression. Just accept it for now.
22398 It will later be processed in convert_template_argument. */
22399 ;
22400 else if (same_type_ignoring_top_level_qualifiers_p
22401 (non_reference (TREE_TYPE (arg)),
22402 non_reference (tparm)))
22403 /* OK. Ignore top-level quals here because a class-type template
22404 parameter object is const. */;
22405 else if ((strict & UNIFY_ALLOW_INTEGER)
22406 && CP_INTEGRAL_TYPE_P (tparm))
22407 /* Convert the ARG to the type of PARM; the deduced non-type
22408 template argument must exactly match the types of the
22409 corresponding parameter. */
22410 arg = fold (build_nop (tparm, arg));
22411 else if (uses_template_parms (tparm))
22412 {
22413 /* We haven't deduced the type of this parameter yet. */
22414 if (cxx_dialect >= cxx17
22415 /* We deduce from array bounds in try_array_deduction. */
22416 && !(strict & UNIFY_ALLOW_INTEGER))
22417 {
22418 /* Deduce it from the non-type argument. */
22419 tree atype = TREE_TYPE (arg);
22420 RECUR_AND_CHECK_FAILURE (tparms, targs,
22421 tparm, atype,
22422 UNIFY_ALLOW_NONE, explain_p);
22423 }
22424 else
22425 /* Try again later. */
22426 return unify_success (explain_p);
22427 }
22428 else
22429 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
22430
22431 /* If ARG is a parameter pack or an expansion, we cannot unify
22432 against it unless PARM is also a parameter pack. */
22433 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
22434 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
22435 return unify_parameter_pack_mismatch (explain_p, parm, arg);
22436
22437 {
22438 bool removed_attr = false;
22439 arg = strip_typedefs_expr (arg, &removed_attr);
22440 }
22441 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
22442 return unify_success (explain_p);
22443
22444 case PTRMEM_CST:
22445 {
22446 /* A pointer-to-member constant can be unified only with
22447 another constant. */
22448 if (TREE_CODE (arg) != PTRMEM_CST)
22449 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
22450
22451 /* Just unify the class member. It would be useless (and possibly
22452 wrong, depending on the strict flags) to unify also
22453 PTRMEM_CST_CLASS, because we want to be sure that both parm and
22454 arg refer to the same variable, even if through different
22455 classes. For instance:
22456
22457 struct A { int x; };
22458 struct B : A { };
22459
22460 Unification of &A::x and &B::x must succeed. */
22461 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
22462 PTRMEM_CST_MEMBER (arg), strict, explain_p);
22463 }
22464
22465 case POINTER_TYPE:
22466 {
22467 if (!TYPE_PTR_P (arg))
22468 return unify_type_mismatch (explain_p, parm, arg);
22469
22470 /* [temp.deduct.call]
22471
22472 A can be another pointer or pointer to member type that can
22473 be converted to the deduced A via a qualification
22474 conversion (_conv.qual_).
22475
22476 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
22477 This will allow for additional cv-qualification of the
22478 pointed-to types if appropriate. */
22479
22480 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
22481 /* The derived-to-base conversion only persists through one
22482 level of pointers. */
22483 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
22484
22485 return unify (tparms, targs, TREE_TYPE (parm),
22486 TREE_TYPE (arg), strict, explain_p);
22487 }
22488
22489 case REFERENCE_TYPE:
22490 if (!TYPE_REF_P (arg))
22491 return unify_type_mismatch (explain_p, parm, arg);
22492 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
22493 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
22494
22495 case ARRAY_TYPE:
22496 if (TREE_CODE (arg) != ARRAY_TYPE)
22497 return unify_type_mismatch (explain_p, parm, arg);
22498 if ((TYPE_DOMAIN (parm) == NULL_TREE)
22499 != (TYPE_DOMAIN (arg) == NULL_TREE))
22500 return unify_type_mismatch (explain_p, parm, arg);
22501 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
22502 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
22503 if (TYPE_DOMAIN (parm) != NULL_TREE)
22504 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
22505 TYPE_DOMAIN (arg), explain_p);
22506 return unify_success (explain_p);
22507
22508 case REAL_TYPE:
22509 case COMPLEX_TYPE:
22510 case VECTOR_TYPE:
22511 case INTEGER_TYPE:
22512 case BOOLEAN_TYPE:
22513 case ENUMERAL_TYPE:
22514 case VOID_TYPE:
22515 case NULLPTR_TYPE:
22516 if (TREE_CODE (arg) != TREE_CODE (parm))
22517 return unify_type_mismatch (explain_p, parm, arg);
22518
22519 /* We have already checked cv-qualification at the top of the
22520 function. */
22521 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
22522 return unify_type_mismatch (explain_p, parm, arg);
22523
22524 /* As far as unification is concerned, this wins. Later checks
22525 will invalidate it if necessary. */
22526 return unify_success (explain_p);
22527
22528 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
22529 /* Type INTEGER_CST can come from ordinary constant template args. */
22530 case INTEGER_CST:
22531 while (CONVERT_EXPR_P (arg))
22532 arg = TREE_OPERAND (arg, 0);
22533
22534 if (TREE_CODE (arg) != INTEGER_CST)
22535 return unify_template_argument_mismatch (explain_p, parm, arg);
22536 return (tree_int_cst_equal (parm, arg)
22537 ? unify_success (explain_p)
22538 : unify_template_argument_mismatch (explain_p, parm, arg));
22539
22540 case TREE_VEC:
22541 {
22542 int i, len, argslen;
22543 int parm_variadic_p = 0;
22544
22545 if (TREE_CODE (arg) != TREE_VEC)
22546 return unify_template_argument_mismatch (explain_p, parm, arg);
22547
22548 len = TREE_VEC_LENGTH (parm);
22549 argslen = TREE_VEC_LENGTH (arg);
22550
22551 /* Check for pack expansions in the parameters. */
22552 for (i = 0; i < len; ++i)
22553 {
22554 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
22555 {
22556 if (i == len - 1)
22557 /* We can unify against something with a trailing
22558 parameter pack. */
22559 parm_variadic_p = 1;
22560 else
22561 /* [temp.deduct.type]/9: If the template argument list of
22562 P contains a pack expansion that is not the last
22563 template argument, the entire template argument list
22564 is a non-deduced context. */
22565 return unify_success (explain_p);
22566 }
22567 }
22568
22569 /* If we don't have enough arguments to satisfy the parameters
22570 (not counting the pack expression at the end), or we have
22571 too many arguments for a parameter list that doesn't end in
22572 a pack expression, we can't unify. */
22573 if (parm_variadic_p
22574 ? argslen < len - parm_variadic_p
22575 : argslen != len)
22576 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
22577
22578 /* Unify all of the parameters that precede the (optional)
22579 pack expression. */
22580 for (i = 0; i < len - parm_variadic_p; ++i)
22581 {
22582 RECUR_AND_CHECK_FAILURE (tparms, targs,
22583 TREE_VEC_ELT (parm, i),
22584 TREE_VEC_ELT (arg, i),
22585 UNIFY_ALLOW_NONE, explain_p);
22586 }
22587 if (parm_variadic_p)
22588 return unify_pack_expansion (tparms, targs, parm, arg,
22589 DEDUCE_EXACT,
22590 /*subr=*/true, explain_p);
22591 return unify_success (explain_p);
22592 }
22593
22594 case RECORD_TYPE:
22595 case UNION_TYPE:
22596 if (TREE_CODE (arg) != TREE_CODE (parm))
22597 return unify_type_mismatch (explain_p, parm, arg);
22598
22599 if (TYPE_PTRMEMFUNC_P (parm))
22600 {
22601 if (!TYPE_PTRMEMFUNC_P (arg))
22602 return unify_type_mismatch (explain_p, parm, arg);
22603
22604 return unify (tparms, targs,
22605 TYPE_PTRMEMFUNC_FN_TYPE (parm),
22606 TYPE_PTRMEMFUNC_FN_TYPE (arg),
22607 strict, explain_p);
22608 }
22609 else if (TYPE_PTRMEMFUNC_P (arg))
22610 return unify_type_mismatch (explain_p, parm, arg);
22611
22612 if (CLASSTYPE_TEMPLATE_INFO (parm))
22613 {
22614 tree t = NULL_TREE;
22615
22616 if (strict_in & UNIFY_ALLOW_DERIVED)
22617 {
22618 /* First, we try to unify the PARM and ARG directly. */
22619 t = try_class_unification (tparms, targs,
22620 parm, arg, explain_p);
22621
22622 if (!t)
22623 {
22624 /* Fallback to the special case allowed in
22625 [temp.deduct.call]:
22626
22627 If P is a class, and P has the form
22628 template-id, then A can be a derived class of
22629 the deduced A. Likewise, if P is a pointer to
22630 a class of the form template-id, A can be a
22631 pointer to a derived class pointed to by the
22632 deduced A. */
22633 enum template_base_result r;
22634 r = get_template_base (tparms, targs, parm, arg,
22635 explain_p, &t);
22636
22637 if (!t)
22638 {
22639 /* Don't give the derived diagnostic if we're
22640 already dealing with the same template. */
22641 bool same_template
22642 = (CLASSTYPE_TEMPLATE_INFO (arg)
22643 && (CLASSTYPE_TI_TEMPLATE (parm)
22644 == CLASSTYPE_TI_TEMPLATE (arg)));
22645 return unify_no_common_base (explain_p && !same_template,
22646 r, parm, arg);
22647 }
22648 }
22649 }
22650 else if (CLASSTYPE_TEMPLATE_INFO (arg)
22651 && (CLASSTYPE_TI_TEMPLATE (parm)
22652 == CLASSTYPE_TI_TEMPLATE (arg)))
22653 /* Perhaps PARM is something like S<U> and ARG is S<int>.
22654 Then, we should unify `int' and `U'. */
22655 t = arg;
22656 else
22657 /* There's no chance of unification succeeding. */
22658 return unify_type_mismatch (explain_p, parm, arg);
22659
22660 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
22661 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
22662 }
22663 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
22664 return unify_type_mismatch (explain_p, parm, arg);
22665 return unify_success (explain_p);
22666
22667 case METHOD_TYPE:
22668 case FUNCTION_TYPE:
22669 {
22670 unsigned int nargs;
22671 tree *args;
22672 tree a;
22673 unsigned int i;
22674
22675 if (TREE_CODE (arg) != TREE_CODE (parm))
22676 return unify_type_mismatch (explain_p, parm, arg);
22677
22678 /* CV qualifications for methods can never be deduced, they must
22679 match exactly. We need to check them explicitly here,
22680 because type_unification_real treats them as any other
22681 cv-qualified parameter. */
22682 if (TREE_CODE (parm) == METHOD_TYPE
22683 && (!check_cv_quals_for_unify
22684 (UNIFY_ALLOW_NONE,
22685 class_of_this_parm (arg),
22686 class_of_this_parm (parm))))
22687 return unify_cv_qual_mismatch (explain_p, parm, arg);
22688 if (TREE_CODE (arg) == FUNCTION_TYPE
22689 && type_memfn_quals (parm) != type_memfn_quals (arg))
22690 return unify_cv_qual_mismatch (explain_p, parm, arg);
22691 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
22692 return unify_type_mismatch (explain_p, parm, arg);
22693
22694 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
22695 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
22696
22697 nargs = list_length (TYPE_ARG_TYPES (arg));
22698 args = XALLOCAVEC (tree, nargs);
22699 for (a = TYPE_ARG_TYPES (arg), i = 0;
22700 a != NULL_TREE && a != void_list_node;
22701 a = TREE_CHAIN (a), ++i)
22702 args[i] = TREE_VALUE (a);
22703 nargs = i;
22704
22705 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
22706 args, nargs, 1, DEDUCE_EXACT,
22707 NULL, explain_p))
22708 return 1;
22709
22710 if (flag_noexcept_type)
22711 {
22712 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
22713 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
22714 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
22715 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
22716 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
22717 && uses_template_parms (TREE_PURPOSE (pspec)))
22718 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
22719 TREE_PURPOSE (aspec),
22720 UNIFY_ALLOW_NONE, explain_p);
22721 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
22722 return unify_type_mismatch (explain_p, parm, arg);
22723 }
22724
22725 return 0;
22726 }
22727
22728 case OFFSET_TYPE:
22729 /* Unify a pointer to member with a pointer to member function, which
22730 deduces the type of the member as a function type. */
22731 if (TYPE_PTRMEMFUNC_P (arg))
22732 {
22733 /* Check top-level cv qualifiers */
22734 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
22735 return unify_cv_qual_mismatch (explain_p, parm, arg);
22736
22737 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
22738 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
22739 UNIFY_ALLOW_NONE, explain_p);
22740
22741 /* Determine the type of the function we are unifying against. */
22742 tree fntype = static_fn_type (arg);
22743
22744 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
22745 }
22746
22747 if (TREE_CODE (arg) != OFFSET_TYPE)
22748 return unify_type_mismatch (explain_p, parm, arg);
22749 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
22750 TYPE_OFFSET_BASETYPE (arg),
22751 UNIFY_ALLOW_NONE, explain_p);
22752 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
22753 strict, explain_p);
22754
22755 case CONST_DECL:
22756 if (DECL_TEMPLATE_PARM_P (parm))
22757 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
22758 if (arg != scalar_constant_value (parm))
22759 return unify_template_argument_mismatch (explain_p, parm, arg);
22760 return unify_success (explain_p);
22761
22762 case FIELD_DECL:
22763 case TEMPLATE_DECL:
22764 /* Matched cases are handled by the ARG == PARM test above. */
22765 return unify_template_argument_mismatch (explain_p, parm, arg);
22766
22767 case VAR_DECL:
22768 /* We might get a variable as a non-type template argument in parm if the
22769 corresponding parameter is type-dependent. Make any necessary
22770 adjustments based on whether arg is a reference. */
22771 if (CONSTANT_CLASS_P (arg))
22772 parm = fold_non_dependent_expr (parm, complain);
22773 else if (REFERENCE_REF_P (arg))
22774 {
22775 tree sub = TREE_OPERAND (arg, 0);
22776 STRIP_NOPS (sub);
22777 if (TREE_CODE (sub) == ADDR_EXPR)
22778 arg = TREE_OPERAND (sub, 0);
22779 }
22780 /* Now use the normal expression code to check whether they match. */
22781 goto expr;
22782
22783 case TYPE_ARGUMENT_PACK:
22784 case NONTYPE_ARGUMENT_PACK:
22785 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
22786 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
22787
22788 case TYPEOF_TYPE:
22789 case DECLTYPE_TYPE:
22790 case UNDERLYING_TYPE:
22791 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
22792 or UNDERLYING_TYPE nodes. */
22793 return unify_success (explain_p);
22794
22795 case ERROR_MARK:
22796 /* Unification fails if we hit an error node. */
22797 return unify_invalid (explain_p);
22798
22799 case INDIRECT_REF:
22800 if (REFERENCE_REF_P (parm))
22801 {
22802 bool pexp = PACK_EXPANSION_P (arg);
22803 if (pexp)
22804 arg = PACK_EXPANSION_PATTERN (arg);
22805 if (REFERENCE_REF_P (arg))
22806 arg = TREE_OPERAND (arg, 0);
22807 if (pexp)
22808 arg = make_pack_expansion (arg, complain);
22809 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
22810 strict, explain_p);
22811 }
22812 /* FALLTHRU */
22813
22814 default:
22815 /* An unresolved overload is a nondeduced context. */
22816 if (is_overloaded_fn (parm) || type_unknown_p (parm))
22817 return unify_success (explain_p);
22818 gcc_assert (EXPR_P (parm)
22819 || COMPOUND_LITERAL_P (parm)
22820 || TREE_CODE (parm) == TRAIT_EXPR);
22821 expr:
22822 /* We must be looking at an expression. This can happen with
22823 something like:
22824
22825 template <int I>
22826 void foo(S<I>, S<I + 2>);
22827
22828 or
22829
22830 template<typename T>
22831 void foo(A<T, T{}>);
22832
22833 This is a "non-deduced context":
22834
22835 [deduct.type]
22836
22837 The non-deduced contexts are:
22838
22839 --A non-type template argument or an array bound in which
22840 a subexpression references a template parameter.
22841
22842 In these cases, we assume deduction succeeded, but don't
22843 actually infer any unifications. */
22844
22845 if (!uses_template_parms (parm)
22846 && !template_args_equal (parm, arg))
22847 return unify_expression_unequal (explain_p, parm, arg);
22848 else
22849 return unify_success (explain_p);
22850 }
22851 }
22852 #undef RECUR_AND_CHECK_FAILURE
22853 \f
22854 /* Note that DECL can be defined in this translation unit, if
22855 required. */
22856
22857 static void
22858 mark_definable (tree decl)
22859 {
22860 tree clone;
22861 DECL_NOT_REALLY_EXTERN (decl) = 1;
22862 FOR_EACH_CLONE (clone, decl)
22863 DECL_NOT_REALLY_EXTERN (clone) = 1;
22864 }
22865
22866 /* Called if RESULT is explicitly instantiated, or is a member of an
22867 explicitly instantiated class. */
22868
22869 void
22870 mark_decl_instantiated (tree result, int extern_p)
22871 {
22872 SET_DECL_EXPLICIT_INSTANTIATION (result);
22873
22874 /* If this entity has already been written out, it's too late to
22875 make any modifications. */
22876 if (TREE_ASM_WRITTEN (result))
22877 return;
22878
22879 /* For anonymous namespace we don't need to do anything. */
22880 if (decl_anon_ns_mem_p (result))
22881 {
22882 gcc_assert (!TREE_PUBLIC (result));
22883 return;
22884 }
22885
22886 if (TREE_CODE (result) != FUNCTION_DECL)
22887 /* The TREE_PUBLIC flag for function declarations will have been
22888 set correctly by tsubst. */
22889 TREE_PUBLIC (result) = 1;
22890
22891 /* This might have been set by an earlier implicit instantiation. */
22892 DECL_COMDAT (result) = 0;
22893
22894 if (extern_p)
22895 DECL_NOT_REALLY_EXTERN (result) = 0;
22896 else
22897 {
22898 mark_definable (result);
22899 mark_needed (result);
22900 /* Always make artificials weak. */
22901 if (DECL_ARTIFICIAL (result) && flag_weak)
22902 comdat_linkage (result);
22903 /* For WIN32 we also want to put explicit instantiations in
22904 linkonce sections. */
22905 else if (TREE_PUBLIC (result))
22906 maybe_make_one_only (result);
22907 if (TREE_CODE (result) == FUNCTION_DECL
22908 && DECL_TEMPLATE_INSTANTIATED (result))
22909 /* If the function has already been instantiated, clear DECL_EXTERNAL,
22910 since start_preparsed_function wouldn't have if we had an earlier
22911 extern explicit instantiation. */
22912 DECL_EXTERNAL (result) = 0;
22913 }
22914
22915 /* If EXTERN_P, then this function will not be emitted -- unless
22916 followed by an explicit instantiation, at which point its linkage
22917 will be adjusted. If !EXTERN_P, then this function will be
22918 emitted here. In neither circumstance do we want
22919 import_export_decl to adjust the linkage. */
22920 DECL_INTERFACE_KNOWN (result) = 1;
22921 }
22922
22923 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
22924 important template arguments. If any are missing, we check whether
22925 they're important by using error_mark_node for substituting into any
22926 args that were used for partial ordering (the ones between ARGS and END)
22927 and seeing if it bubbles up. */
22928
22929 static bool
22930 check_undeduced_parms (tree targs, tree args, tree end)
22931 {
22932 bool found = false;
22933 int i;
22934 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
22935 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
22936 {
22937 found = true;
22938 TREE_VEC_ELT (targs, i) = error_mark_node;
22939 }
22940 if (found)
22941 {
22942 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
22943 if (substed == error_mark_node)
22944 return true;
22945 }
22946 return false;
22947 }
22948
22949 /* Given two function templates PAT1 and PAT2, return:
22950
22951 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
22952 -1 if PAT2 is more specialized than PAT1.
22953 0 if neither is more specialized.
22954
22955 LEN indicates the number of parameters we should consider
22956 (defaulted parameters should not be considered).
22957
22958 The 1998 std underspecified function template partial ordering, and
22959 DR214 addresses the issue. We take pairs of arguments, one from
22960 each of the templates, and deduce them against each other. One of
22961 the templates will be more specialized if all the *other*
22962 template's arguments deduce against its arguments and at least one
22963 of its arguments *does* *not* deduce against the other template's
22964 corresponding argument. Deduction is done as for class templates.
22965 The arguments used in deduction have reference and top level cv
22966 qualifiers removed. Iff both arguments were originally reference
22967 types *and* deduction succeeds in both directions, an lvalue reference
22968 wins against an rvalue reference and otherwise the template
22969 with the more cv-qualified argument wins for that pairing (if
22970 neither is more cv-qualified, they both are equal). Unlike regular
22971 deduction, after all the arguments have been deduced in this way,
22972 we do *not* verify the deduced template argument values can be
22973 substituted into non-deduced contexts.
22974
22975 The logic can be a bit confusing here, because we look at deduce1 and
22976 targs1 to see if pat2 is at least as specialized, and vice versa; if we
22977 can find template arguments for pat1 to make arg1 look like arg2, that
22978 means that arg2 is at least as specialized as arg1. */
22979
22980 int
22981 more_specialized_fn (tree pat1, tree pat2, int len)
22982 {
22983 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
22984 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
22985 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
22986 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
22987 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
22988 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
22989 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
22990 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
22991 tree origs1, origs2;
22992 bool lose1 = false;
22993 bool lose2 = false;
22994
22995 /* Remove the this parameter from non-static member functions. If
22996 one is a non-static member function and the other is not a static
22997 member function, remove the first parameter from that function
22998 also. This situation occurs for operator functions where we
22999 locate both a member function (with this pointer) and non-member
23000 operator (with explicit first operand). */
23001 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
23002 {
23003 len--; /* LEN is the number of significant arguments for DECL1 */
23004 args1 = TREE_CHAIN (args1);
23005 if (!DECL_STATIC_FUNCTION_P (decl2))
23006 args2 = TREE_CHAIN (args2);
23007 }
23008 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
23009 {
23010 args2 = TREE_CHAIN (args2);
23011 if (!DECL_STATIC_FUNCTION_P (decl1))
23012 {
23013 len--;
23014 args1 = TREE_CHAIN (args1);
23015 }
23016 }
23017
23018 /* If only one is a conversion operator, they are unordered. */
23019 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
23020 return 0;
23021
23022 /* Consider the return type for a conversion function */
23023 if (DECL_CONV_FN_P (decl1))
23024 {
23025 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
23026 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
23027 len++;
23028 }
23029
23030 processing_template_decl++;
23031
23032 origs1 = args1;
23033 origs2 = args2;
23034
23035 while (len--
23036 /* Stop when an ellipsis is seen. */
23037 && args1 != NULL_TREE && args2 != NULL_TREE)
23038 {
23039 tree arg1 = TREE_VALUE (args1);
23040 tree arg2 = TREE_VALUE (args2);
23041 int deduce1, deduce2;
23042 int quals1 = -1;
23043 int quals2 = -1;
23044 int ref1 = 0;
23045 int ref2 = 0;
23046
23047 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
23048 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
23049 {
23050 /* When both arguments are pack expansions, we need only
23051 unify the patterns themselves. */
23052 arg1 = PACK_EXPANSION_PATTERN (arg1);
23053 arg2 = PACK_EXPANSION_PATTERN (arg2);
23054
23055 /* This is the last comparison we need to do. */
23056 len = 0;
23057 }
23058
23059 /* DR 1847: If a particular P contains no template-parameters that
23060 participate in template argument deduction, that P is not used to
23061 determine the ordering. */
23062 if (!uses_deducible_template_parms (arg1)
23063 && !uses_deducible_template_parms (arg2))
23064 goto next;
23065
23066 if (TYPE_REF_P (arg1))
23067 {
23068 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
23069 arg1 = TREE_TYPE (arg1);
23070 quals1 = cp_type_quals (arg1);
23071 }
23072
23073 if (TYPE_REF_P (arg2))
23074 {
23075 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
23076 arg2 = TREE_TYPE (arg2);
23077 quals2 = cp_type_quals (arg2);
23078 }
23079
23080 arg1 = TYPE_MAIN_VARIANT (arg1);
23081 arg2 = TYPE_MAIN_VARIANT (arg2);
23082
23083 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
23084 {
23085 int i, len2 = remaining_arguments (args2);
23086 tree parmvec = make_tree_vec (1);
23087 tree argvec = make_tree_vec (len2);
23088 tree ta = args2;
23089
23090 /* Setup the parameter vector, which contains only ARG1. */
23091 TREE_VEC_ELT (parmvec, 0) = arg1;
23092
23093 /* Setup the argument vector, which contains the remaining
23094 arguments. */
23095 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
23096 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
23097
23098 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
23099 argvec, DEDUCE_EXACT,
23100 /*subr=*/true, /*explain_p=*/false)
23101 == 0);
23102
23103 /* We cannot deduce in the other direction, because ARG1 is
23104 a pack expansion but ARG2 is not. */
23105 deduce2 = 0;
23106 }
23107 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
23108 {
23109 int i, len1 = remaining_arguments (args1);
23110 tree parmvec = make_tree_vec (1);
23111 tree argvec = make_tree_vec (len1);
23112 tree ta = args1;
23113
23114 /* Setup the parameter vector, which contains only ARG1. */
23115 TREE_VEC_ELT (parmvec, 0) = arg2;
23116
23117 /* Setup the argument vector, which contains the remaining
23118 arguments. */
23119 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
23120 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
23121
23122 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
23123 argvec, DEDUCE_EXACT,
23124 /*subr=*/true, /*explain_p=*/false)
23125 == 0);
23126
23127 /* We cannot deduce in the other direction, because ARG2 is
23128 a pack expansion but ARG1 is not.*/
23129 deduce1 = 0;
23130 }
23131
23132 else
23133 {
23134 /* The normal case, where neither argument is a pack
23135 expansion. */
23136 deduce1 = (unify (tparms1, targs1, arg1, arg2,
23137 UNIFY_ALLOW_NONE, /*explain_p=*/false)
23138 == 0);
23139 deduce2 = (unify (tparms2, targs2, arg2, arg1,
23140 UNIFY_ALLOW_NONE, /*explain_p=*/false)
23141 == 0);
23142 }
23143
23144 /* If we couldn't deduce arguments for tparms1 to make arg1 match
23145 arg2, then arg2 is not as specialized as arg1. */
23146 if (!deduce1)
23147 lose2 = true;
23148 if (!deduce2)
23149 lose1 = true;
23150
23151 /* "If, for a given type, deduction succeeds in both directions
23152 (i.e., the types are identical after the transformations above)
23153 and both P and A were reference types (before being replaced with
23154 the type referred to above):
23155 - if the type from the argument template was an lvalue reference and
23156 the type from the parameter template was not, the argument type is
23157 considered to be more specialized than the other; otherwise,
23158 - if the type from the argument template is more cv-qualified
23159 than the type from the parameter template (as described above),
23160 the argument type is considered to be more specialized than the other;
23161 otherwise,
23162 - neither type is more specialized than the other." */
23163
23164 if (deduce1 && deduce2)
23165 {
23166 if (ref1 && ref2 && ref1 != ref2)
23167 {
23168 if (ref1 > ref2)
23169 lose1 = true;
23170 else
23171 lose2 = true;
23172 }
23173 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
23174 {
23175 if ((quals1 & quals2) == quals2)
23176 lose2 = true;
23177 if ((quals1 & quals2) == quals1)
23178 lose1 = true;
23179 }
23180 }
23181
23182 if (lose1 && lose2)
23183 /* We've failed to deduce something in either direction.
23184 These must be unordered. */
23185 break;
23186
23187 next:
23188
23189 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
23190 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
23191 /* We have already processed all of the arguments in our
23192 handing of the pack expansion type. */
23193 len = 0;
23194
23195 args1 = TREE_CHAIN (args1);
23196 args2 = TREE_CHAIN (args2);
23197 }
23198
23199 /* "In most cases, all template parameters must have values in order for
23200 deduction to succeed, but for partial ordering purposes a template
23201 parameter may remain without a value provided it is not used in the
23202 types being used for partial ordering."
23203
23204 Thus, if we are missing any of the targs1 we need to substitute into
23205 origs1, then pat2 is not as specialized as pat1. This can happen when
23206 there is a nondeduced context. */
23207 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
23208 lose2 = true;
23209 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
23210 lose1 = true;
23211
23212 processing_template_decl--;
23213
23214 /* If both deductions succeed, the partial ordering selects the more
23215 constrained template. */
23216 if (!lose1 && !lose2)
23217 {
23218 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
23219 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
23220 lose1 = !subsumes_constraints (c1, c2);
23221 lose2 = !subsumes_constraints (c2, c1);
23222 }
23223
23224 /* All things being equal, if the next argument is a pack expansion
23225 for one function but not for the other, prefer the
23226 non-variadic function. FIXME this is bogus; see c++/41958. */
23227 if (lose1 == lose2
23228 && args1 && TREE_VALUE (args1)
23229 && args2 && TREE_VALUE (args2))
23230 {
23231 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
23232 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
23233 }
23234
23235 if (lose1 == lose2)
23236 return 0;
23237 else if (!lose1)
23238 return 1;
23239 else
23240 return -1;
23241 }
23242
23243 /* Determine which of two partial specializations of TMPL is more
23244 specialized.
23245
23246 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
23247 to the first partial specialization. The TREE_PURPOSE is the
23248 innermost set of template parameters for the partial
23249 specialization. PAT2 is similar, but for the second template.
23250
23251 Return 1 if the first partial specialization is more specialized;
23252 -1 if the second is more specialized; 0 if neither is more
23253 specialized.
23254
23255 See [temp.class.order] for information about determining which of
23256 two templates is more specialized. */
23257
23258 static int
23259 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
23260 {
23261 tree targs;
23262 int winner = 0;
23263 bool any_deductions = false;
23264
23265 tree tmpl1 = TREE_VALUE (pat1);
23266 tree tmpl2 = TREE_VALUE (pat2);
23267 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
23268 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
23269
23270 /* Just like what happens for functions, if we are ordering between
23271 different template specializations, we may encounter dependent
23272 types in the arguments, and we need our dependency check functions
23273 to behave correctly. */
23274 ++processing_template_decl;
23275 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
23276 if (targs)
23277 {
23278 --winner;
23279 any_deductions = true;
23280 }
23281
23282 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
23283 if (targs)
23284 {
23285 ++winner;
23286 any_deductions = true;
23287 }
23288 --processing_template_decl;
23289
23290 /* If both deductions succeed, the partial ordering selects the more
23291 constrained template. */
23292 if (!winner && any_deductions)
23293 return more_constrained (tmpl1, tmpl2);
23294
23295 /* In the case of a tie where at least one of the templates
23296 has a parameter pack at the end, the template with the most
23297 non-packed parameters wins. */
23298 if (winner == 0
23299 && any_deductions
23300 && (template_args_variadic_p (TREE_PURPOSE (pat1))
23301 || template_args_variadic_p (TREE_PURPOSE (pat2))))
23302 {
23303 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
23304 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
23305 int len1 = TREE_VEC_LENGTH (args1);
23306 int len2 = TREE_VEC_LENGTH (args2);
23307
23308 /* We don't count the pack expansion at the end. */
23309 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
23310 --len1;
23311 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
23312 --len2;
23313
23314 if (len1 > len2)
23315 return 1;
23316 else if (len1 < len2)
23317 return -1;
23318 }
23319
23320 return winner;
23321 }
23322
23323 /* Return the template arguments that will produce the function signature
23324 DECL from the function template FN, with the explicit template
23325 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
23326 also match. Return NULL_TREE if no satisfactory arguments could be
23327 found. */
23328
23329 static tree
23330 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
23331 {
23332 int ntparms = DECL_NTPARMS (fn);
23333 tree targs = make_tree_vec (ntparms);
23334 tree decl_type = TREE_TYPE (decl);
23335 tree decl_arg_types;
23336 tree *args;
23337 unsigned int nargs, ix;
23338 tree arg;
23339
23340 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
23341
23342 /* Never do unification on the 'this' parameter. */
23343 decl_arg_types = skip_artificial_parms_for (decl,
23344 TYPE_ARG_TYPES (decl_type));
23345
23346 nargs = list_length (decl_arg_types);
23347 args = XALLOCAVEC (tree, nargs);
23348 for (arg = decl_arg_types, ix = 0;
23349 arg != NULL_TREE && arg != void_list_node;
23350 arg = TREE_CHAIN (arg), ++ix)
23351 args[ix] = TREE_VALUE (arg);
23352
23353 if (fn_type_unification (fn, explicit_args, targs,
23354 args, ix,
23355 (check_rettype || DECL_CONV_FN_P (fn)
23356 ? TREE_TYPE (decl_type) : NULL_TREE),
23357 DEDUCE_EXACT, LOOKUP_NORMAL, NULL,
23358 /*explain_p=*/false,
23359 /*decltype*/false)
23360 == error_mark_node)
23361 return NULL_TREE;
23362
23363 return targs;
23364 }
23365
23366 /* Return the innermost template arguments that, when applied to a partial
23367 specialization SPEC_TMPL of TMPL, yield the ARGS.
23368
23369 For example, suppose we have:
23370
23371 template <class T, class U> struct S {};
23372 template <class T> struct S<T*, int> {};
23373
23374 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
23375 partial specialization and the ARGS will be {double*, int}. The resulting
23376 vector will be {double}, indicating that `T' is bound to `double'. */
23377
23378 static tree
23379 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
23380 {
23381 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
23382 tree spec_args
23383 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
23384 int i, ntparms = TREE_VEC_LENGTH (tparms);
23385 tree deduced_args;
23386 tree innermost_deduced_args;
23387
23388 innermost_deduced_args = make_tree_vec (ntparms);
23389 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
23390 {
23391 deduced_args = copy_node (args);
23392 SET_TMPL_ARGS_LEVEL (deduced_args,
23393 TMPL_ARGS_DEPTH (deduced_args),
23394 innermost_deduced_args);
23395 }
23396 else
23397 deduced_args = innermost_deduced_args;
23398
23399 bool tried_array_deduction = (cxx_dialect < cxx17);
23400 again:
23401 if (unify (tparms, deduced_args,
23402 INNERMOST_TEMPLATE_ARGS (spec_args),
23403 INNERMOST_TEMPLATE_ARGS (args),
23404 UNIFY_ALLOW_NONE, /*explain_p=*/false))
23405 return NULL_TREE;
23406
23407 for (i = 0; i < ntparms; ++i)
23408 if (! TREE_VEC_ELT (innermost_deduced_args, i))
23409 {
23410 if (!tried_array_deduction)
23411 {
23412 try_array_deduction (tparms, innermost_deduced_args,
23413 INNERMOST_TEMPLATE_ARGS (spec_args));
23414 tried_array_deduction = true;
23415 if (TREE_VEC_ELT (innermost_deduced_args, i))
23416 goto again;
23417 }
23418 return NULL_TREE;
23419 }
23420
23421 if (!push_tinst_level (spec_tmpl, deduced_args))
23422 {
23423 excessive_deduction_depth = true;
23424 return NULL_TREE;
23425 }
23426
23427 /* Verify that nondeduced template arguments agree with the type
23428 obtained from argument deduction.
23429
23430 For example:
23431
23432 struct A { typedef int X; };
23433 template <class T, class U> struct C {};
23434 template <class T> struct C<T, typename T::X> {};
23435
23436 Then with the instantiation `C<A, int>', we can deduce that
23437 `T' is `A' but unify () does not check whether `typename T::X'
23438 is `int'. */
23439 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
23440
23441 if (spec_args != error_mark_node)
23442 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
23443 INNERMOST_TEMPLATE_ARGS (spec_args),
23444 tmpl, tf_none, false, false);
23445
23446 pop_tinst_level ();
23447
23448 if (spec_args == error_mark_node
23449 /* We only need to check the innermost arguments; the other
23450 arguments will always agree. */
23451 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
23452 INNERMOST_TEMPLATE_ARGS (args)))
23453 return NULL_TREE;
23454
23455 /* Now that we have bindings for all of the template arguments,
23456 ensure that the arguments deduced for the template template
23457 parameters have compatible template parameter lists. See the use
23458 of template_template_parm_bindings_ok_p in fn_type_unification
23459 for more information. */
23460 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
23461 return NULL_TREE;
23462
23463 return deduced_args;
23464 }
23465
23466 // Compare two function templates T1 and T2 by deducing bindings
23467 // from one against the other. If both deductions succeed, compare
23468 // constraints to see which is more constrained.
23469 static int
23470 more_specialized_inst (tree t1, tree t2)
23471 {
23472 int fate = 0;
23473 int count = 0;
23474
23475 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
23476 {
23477 --fate;
23478 ++count;
23479 }
23480
23481 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
23482 {
23483 ++fate;
23484 ++count;
23485 }
23486
23487 // If both deductions succeed, then one may be more constrained.
23488 if (count == 2 && fate == 0)
23489 fate = more_constrained (t1, t2);
23490
23491 return fate;
23492 }
23493
23494 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
23495 Return the TREE_LIST node with the most specialized template, if
23496 any. If there is no most specialized template, the error_mark_node
23497 is returned.
23498
23499 Note that this function does not look at, or modify, the
23500 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
23501 returned is one of the elements of INSTANTIATIONS, callers may
23502 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
23503 and retrieve it from the value returned. */
23504
23505 tree
23506 most_specialized_instantiation (tree templates)
23507 {
23508 tree fn, champ;
23509
23510 ++processing_template_decl;
23511
23512 champ = templates;
23513 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
23514 {
23515 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
23516 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
23517 if (fate == -1)
23518 champ = fn;
23519 else if (!fate)
23520 {
23521 /* Equally specialized, move to next function. If there
23522 is no next function, nothing's most specialized. */
23523 fn = TREE_CHAIN (fn);
23524 champ = fn;
23525 if (!fn)
23526 break;
23527 }
23528 }
23529
23530 if (champ)
23531 /* Now verify that champ is better than everything earlier in the
23532 instantiation list. */
23533 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
23534 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
23535 {
23536 champ = NULL_TREE;
23537 break;
23538 }
23539 }
23540
23541 processing_template_decl--;
23542
23543 if (!champ)
23544 return error_mark_node;
23545
23546 return champ;
23547 }
23548
23549 /* If DECL is a specialization of some template, return the most
23550 general such template. Otherwise, returns NULL_TREE.
23551
23552 For example, given:
23553
23554 template <class T> struct S { template <class U> void f(U); };
23555
23556 if TMPL is `template <class U> void S<int>::f(U)' this will return
23557 the full template. This function will not trace past partial
23558 specializations, however. For example, given in addition:
23559
23560 template <class T> struct S<T*> { template <class U> void f(U); };
23561
23562 if TMPL is `template <class U> void S<int*>::f(U)' this will return
23563 `template <class T> template <class U> S<T*>::f(U)'. */
23564
23565 tree
23566 most_general_template (tree decl)
23567 {
23568 if (TREE_CODE (decl) != TEMPLATE_DECL)
23569 {
23570 if (tree tinfo = get_template_info (decl))
23571 decl = TI_TEMPLATE (tinfo);
23572 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
23573 template friend, or a FIELD_DECL for a capture pack. */
23574 if (TREE_CODE (decl) != TEMPLATE_DECL)
23575 return NULL_TREE;
23576 }
23577
23578 /* Look for more and more general templates. */
23579 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
23580 {
23581 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
23582 (See cp-tree.h for details.) */
23583 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
23584 break;
23585
23586 if (CLASS_TYPE_P (TREE_TYPE (decl))
23587 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
23588 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
23589 break;
23590
23591 /* Stop if we run into an explicitly specialized class template. */
23592 if (!DECL_NAMESPACE_SCOPE_P (decl)
23593 && DECL_CONTEXT (decl)
23594 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
23595 break;
23596
23597 decl = DECL_TI_TEMPLATE (decl);
23598 }
23599
23600 return decl;
23601 }
23602
23603 /* Return the most specialized of the template partial specializations
23604 which can produce TARGET, a specialization of some class or variable
23605 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
23606 a TEMPLATE_DECL node corresponding to the partial specialization, while
23607 the TREE_PURPOSE is the set of template arguments that must be
23608 substituted into the template pattern in order to generate TARGET.
23609
23610 If the choice of partial specialization is ambiguous, a diagnostic
23611 is issued, and the error_mark_node is returned. If there are no
23612 partial specializations matching TARGET, then NULL_TREE is
23613 returned, indicating that the primary template should be used. */
23614
23615 static tree
23616 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
23617 {
23618 tree list = NULL_TREE;
23619 tree t;
23620 tree champ;
23621 int fate;
23622 bool ambiguous_p;
23623 tree outer_args = NULL_TREE;
23624 tree tmpl, args;
23625
23626 if (TYPE_P (target))
23627 {
23628 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
23629 tmpl = TI_TEMPLATE (tinfo);
23630 args = TI_ARGS (tinfo);
23631 }
23632 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
23633 {
23634 tmpl = TREE_OPERAND (target, 0);
23635 args = TREE_OPERAND (target, 1);
23636 }
23637 else if (VAR_P (target))
23638 {
23639 tree tinfo = DECL_TEMPLATE_INFO (target);
23640 tmpl = TI_TEMPLATE (tinfo);
23641 args = TI_ARGS (tinfo);
23642 }
23643 else
23644 gcc_unreachable ();
23645
23646 tree main_tmpl = most_general_template (tmpl);
23647
23648 /* For determining which partial specialization to use, only the
23649 innermost args are interesting. */
23650 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
23651 {
23652 outer_args = strip_innermost_template_args (args, 1);
23653 args = INNERMOST_TEMPLATE_ARGS (args);
23654 }
23655
23656 /* The caller hasn't called push_to_top_level yet, but we need
23657 get_partial_spec_bindings to be done in non-template context so that we'll
23658 fully resolve everything. */
23659 processing_template_decl_sentinel ptds;
23660
23661 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
23662 {
23663 tree spec_args;
23664 tree spec_tmpl = TREE_VALUE (t);
23665
23666 if (outer_args)
23667 {
23668 /* Substitute in the template args from the enclosing class. */
23669 ++processing_template_decl;
23670 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
23671 --processing_template_decl;
23672 }
23673
23674 if (spec_tmpl == error_mark_node)
23675 return error_mark_node;
23676
23677 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
23678 if (spec_args)
23679 {
23680 if (outer_args)
23681 spec_args = add_to_template_args (outer_args, spec_args);
23682
23683 /* Keep the candidate only if the constraints are satisfied,
23684 or if we're not compiling with concepts. */
23685 if (!flag_concepts
23686 || constraints_satisfied_p (spec_tmpl, spec_args))
23687 {
23688 list = tree_cons (spec_args, TREE_VALUE (t), list);
23689 TREE_TYPE (list) = TREE_TYPE (t);
23690 }
23691 }
23692 }
23693
23694 if (! list)
23695 return NULL_TREE;
23696
23697 ambiguous_p = false;
23698 t = list;
23699 champ = t;
23700 t = TREE_CHAIN (t);
23701 for (; t; t = TREE_CHAIN (t))
23702 {
23703 fate = more_specialized_partial_spec (tmpl, champ, t);
23704 if (fate == 1)
23705 ;
23706 else
23707 {
23708 if (fate == 0)
23709 {
23710 t = TREE_CHAIN (t);
23711 if (! t)
23712 {
23713 ambiguous_p = true;
23714 break;
23715 }
23716 }
23717 champ = t;
23718 }
23719 }
23720
23721 if (!ambiguous_p)
23722 for (t = list; t && t != champ; t = TREE_CHAIN (t))
23723 {
23724 fate = more_specialized_partial_spec (tmpl, champ, t);
23725 if (fate != 1)
23726 {
23727 ambiguous_p = true;
23728 break;
23729 }
23730 }
23731
23732 if (ambiguous_p)
23733 {
23734 const char *str;
23735 char *spaces = NULL;
23736 if (!(complain & tf_error))
23737 return error_mark_node;
23738 if (TYPE_P (target))
23739 error ("ambiguous template instantiation for %q#T", target);
23740 else
23741 error ("ambiguous template instantiation for %q#D", target);
23742 str = ngettext ("candidate is:", "candidates are:", list_length (list));
23743 for (t = list; t; t = TREE_CHAIN (t))
23744 {
23745 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
23746 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
23747 "%s %#qS", spaces ? spaces : str, subst);
23748 spaces = spaces ? spaces : get_spaces (str);
23749 }
23750 free (spaces);
23751 return error_mark_node;
23752 }
23753
23754 return champ;
23755 }
23756
23757 /* Explicitly instantiate DECL. */
23758
23759 void
23760 do_decl_instantiation (tree decl, tree storage)
23761 {
23762 tree result = NULL_TREE;
23763 int extern_p = 0;
23764
23765 if (!decl || decl == error_mark_node)
23766 /* An error occurred, for which grokdeclarator has already issued
23767 an appropriate message. */
23768 return;
23769 else if (! DECL_LANG_SPECIFIC (decl))
23770 {
23771 error ("explicit instantiation of non-template %q#D", decl);
23772 return;
23773 }
23774 else if (DECL_DECLARED_CONCEPT_P (decl))
23775 {
23776 if (VAR_P (decl))
23777 error ("explicit instantiation of variable concept %q#D", decl);
23778 else
23779 error ("explicit instantiation of function concept %q#D", decl);
23780 return;
23781 }
23782
23783 bool var_templ = (DECL_TEMPLATE_INFO (decl)
23784 && variable_template_p (DECL_TI_TEMPLATE (decl)));
23785
23786 if (VAR_P (decl) && !var_templ)
23787 {
23788 /* There is an asymmetry here in the way VAR_DECLs and
23789 FUNCTION_DECLs are handled by grokdeclarator. In the case of
23790 the latter, the DECL we get back will be marked as a
23791 template instantiation, and the appropriate
23792 DECL_TEMPLATE_INFO will be set up. This does not happen for
23793 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
23794 should handle VAR_DECLs as it currently handles
23795 FUNCTION_DECLs. */
23796 if (!DECL_CLASS_SCOPE_P (decl))
23797 {
23798 error ("%qD is not a static data member of a class template", decl);
23799 return;
23800 }
23801 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
23802 if (!result || !VAR_P (result))
23803 {
23804 error ("no matching template for %qD found", decl);
23805 return;
23806 }
23807 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
23808 {
23809 error ("type %qT for explicit instantiation %qD does not match "
23810 "declared type %qT", TREE_TYPE (result), decl,
23811 TREE_TYPE (decl));
23812 return;
23813 }
23814 }
23815 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
23816 {
23817 error ("explicit instantiation of %q#D", decl);
23818 return;
23819 }
23820 else
23821 result = decl;
23822
23823 /* Check for various error cases. Note that if the explicit
23824 instantiation is valid the RESULT will currently be marked as an
23825 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
23826 until we get here. */
23827
23828 if (DECL_TEMPLATE_SPECIALIZATION (result))
23829 {
23830 /* DR 259 [temp.spec].
23831
23832 Both an explicit instantiation and a declaration of an explicit
23833 specialization shall not appear in a program unless the explicit
23834 instantiation follows a declaration of the explicit specialization.
23835
23836 For a given set of template parameters, if an explicit
23837 instantiation of a template appears after a declaration of an
23838 explicit specialization for that template, the explicit
23839 instantiation has no effect. */
23840 return;
23841 }
23842 else if (DECL_EXPLICIT_INSTANTIATION (result))
23843 {
23844 /* [temp.spec]
23845
23846 No program shall explicitly instantiate any template more
23847 than once.
23848
23849 We check DECL_NOT_REALLY_EXTERN so as not to complain when
23850 the first instantiation was `extern' and the second is not,
23851 and EXTERN_P for the opposite case. */
23852 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
23853 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
23854 /* If an "extern" explicit instantiation follows an ordinary
23855 explicit instantiation, the template is instantiated. */
23856 if (extern_p)
23857 return;
23858 }
23859 else if (!DECL_IMPLICIT_INSTANTIATION (result))
23860 {
23861 error ("no matching template for %qD found", result);
23862 return;
23863 }
23864 else if (!DECL_TEMPLATE_INFO (result))
23865 {
23866 permerror (input_location, "explicit instantiation of non-template %q#D", result);
23867 return;
23868 }
23869
23870 if (storage == NULL_TREE)
23871 ;
23872 else if (storage == ridpointers[(int) RID_EXTERN])
23873 {
23874 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
23875 pedwarn (input_location, OPT_Wpedantic,
23876 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
23877 "instantiations");
23878 extern_p = 1;
23879 }
23880 else
23881 error ("storage class %qD applied to template instantiation", storage);
23882
23883 check_explicit_instantiation_namespace (result);
23884 mark_decl_instantiated (result, extern_p);
23885 if (! extern_p)
23886 instantiate_decl (result, /*defer_ok=*/true,
23887 /*expl_inst_class_mem_p=*/false);
23888 }
23889
23890 static void
23891 mark_class_instantiated (tree t, int extern_p)
23892 {
23893 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
23894 SET_CLASSTYPE_INTERFACE_KNOWN (t);
23895 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
23896 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
23897 if (! extern_p)
23898 {
23899 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
23900 rest_of_type_compilation (t, 1);
23901 }
23902 }
23903
23904 /* Called from do_type_instantiation through binding_table_foreach to
23905 do recursive instantiation for the type bound in ENTRY. */
23906 static void
23907 bt_instantiate_type_proc (binding_entry entry, void *data)
23908 {
23909 tree storage = *(tree *) data;
23910
23911 if (MAYBE_CLASS_TYPE_P (entry->type)
23912 && CLASSTYPE_TEMPLATE_INFO (entry->type)
23913 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
23914 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
23915 }
23916
23917 /* Perform an explicit instantiation of template class T. STORAGE, if
23918 non-null, is the RID for extern, inline or static. COMPLAIN is
23919 nonzero if this is called from the parser, zero if called recursively,
23920 since the standard is unclear (as detailed below). */
23921
23922 void
23923 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
23924 {
23925 int extern_p = 0;
23926 int nomem_p = 0;
23927 int static_p = 0;
23928 int previous_instantiation_extern_p = 0;
23929
23930 if (TREE_CODE (t) == TYPE_DECL)
23931 t = TREE_TYPE (t);
23932
23933 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
23934 {
23935 tree tmpl =
23936 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
23937 if (tmpl)
23938 error ("explicit instantiation of non-class template %qD", tmpl);
23939 else
23940 error ("explicit instantiation of non-template type %qT", t);
23941 return;
23942 }
23943
23944 complete_type (t);
23945
23946 if (!COMPLETE_TYPE_P (t))
23947 {
23948 if (complain & tf_error)
23949 error ("explicit instantiation of %q#T before definition of template",
23950 t);
23951 return;
23952 }
23953
23954 if (storage != NULL_TREE)
23955 {
23956 if (!in_system_header_at (input_location))
23957 {
23958 if (storage == ridpointers[(int) RID_EXTERN])
23959 {
23960 if (cxx_dialect == cxx98)
23961 pedwarn (input_location, OPT_Wpedantic,
23962 "ISO C++ 1998 forbids the use of %<extern%> on "
23963 "explicit instantiations");
23964 }
23965 else
23966 pedwarn (input_location, OPT_Wpedantic,
23967 "ISO C++ forbids the use of %qE"
23968 " on explicit instantiations", storage);
23969 }
23970
23971 if (storage == ridpointers[(int) RID_INLINE])
23972 nomem_p = 1;
23973 else if (storage == ridpointers[(int) RID_EXTERN])
23974 extern_p = 1;
23975 else if (storage == ridpointers[(int) RID_STATIC])
23976 static_p = 1;
23977 else
23978 {
23979 error ("storage class %qD applied to template instantiation",
23980 storage);
23981 extern_p = 0;
23982 }
23983 }
23984
23985 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
23986 {
23987 /* DR 259 [temp.spec].
23988
23989 Both an explicit instantiation and a declaration of an explicit
23990 specialization shall not appear in a program unless the explicit
23991 instantiation follows a declaration of the explicit specialization.
23992
23993 For a given set of template parameters, if an explicit
23994 instantiation of a template appears after a declaration of an
23995 explicit specialization for that template, the explicit
23996 instantiation has no effect. */
23997 return;
23998 }
23999 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
24000 {
24001 /* [temp.spec]
24002
24003 No program shall explicitly instantiate any template more
24004 than once.
24005
24006 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
24007 instantiation was `extern'. If EXTERN_P then the second is.
24008 These cases are OK. */
24009 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
24010
24011 if (!previous_instantiation_extern_p && !extern_p
24012 && (complain & tf_error))
24013 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
24014
24015 /* If we've already instantiated the template, just return now. */
24016 if (!CLASSTYPE_INTERFACE_ONLY (t))
24017 return;
24018 }
24019
24020 check_explicit_instantiation_namespace (TYPE_NAME (t));
24021 mark_class_instantiated (t, extern_p);
24022
24023 if (nomem_p)
24024 return;
24025
24026 /* In contrast to implicit instantiation, where only the
24027 declarations, and not the definitions, of members are
24028 instantiated, we have here:
24029
24030 [temp.explicit]
24031
24032 The explicit instantiation of a class template specialization
24033 implies the instantiation of all of its members not
24034 previously explicitly specialized in the translation unit
24035 containing the explicit instantiation.
24036
24037 Of course, we can't instantiate member template classes, since we
24038 don't have any arguments for them. Note that the standard is
24039 unclear on whether the instantiation of the members are
24040 *explicit* instantiations or not. However, the most natural
24041 interpretation is that it should be an explicit
24042 instantiation. */
24043 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
24044 if ((VAR_P (fld)
24045 || (TREE_CODE (fld) == FUNCTION_DECL
24046 && !static_p
24047 && user_provided_p (fld)))
24048 && DECL_TEMPLATE_INSTANTIATION (fld))
24049 {
24050 mark_decl_instantiated (fld, extern_p);
24051 if (! extern_p)
24052 instantiate_decl (fld, /*defer_ok=*/true,
24053 /*expl_inst_class_mem_p=*/true);
24054 }
24055
24056 if (CLASSTYPE_NESTED_UTDS (t))
24057 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
24058 bt_instantiate_type_proc, &storage);
24059 }
24060
24061 /* Given a function DECL, which is a specialization of TMPL, modify
24062 DECL to be a re-instantiation of TMPL with the same template
24063 arguments. TMPL should be the template into which tsubst'ing
24064 should occur for DECL, not the most general template.
24065
24066 One reason for doing this is a scenario like this:
24067
24068 template <class T>
24069 void f(const T&, int i);
24070
24071 void g() { f(3, 7); }
24072
24073 template <class T>
24074 void f(const T& t, const int i) { }
24075
24076 Note that when the template is first instantiated, with
24077 instantiate_template, the resulting DECL will have no name for the
24078 first parameter, and the wrong type for the second. So, when we go
24079 to instantiate the DECL, we regenerate it. */
24080
24081 static void
24082 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
24083 {
24084 /* The arguments used to instantiate DECL, from the most general
24085 template. */
24086 tree code_pattern;
24087
24088 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
24089
24090 /* Make sure that we can see identifiers, and compute access
24091 correctly. */
24092 push_access_scope (decl);
24093
24094 if (TREE_CODE (decl) == FUNCTION_DECL)
24095 {
24096 tree decl_parm;
24097 tree pattern_parm;
24098 tree specs;
24099 int args_depth;
24100 int parms_depth;
24101
24102 args_depth = TMPL_ARGS_DEPTH (args);
24103 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
24104 if (args_depth > parms_depth)
24105 args = get_innermost_template_args (args, parms_depth);
24106
24107 /* Instantiate a dynamic exception-specification. noexcept will be
24108 handled below. */
24109 if (tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern)))
24110 if (TREE_VALUE (raises))
24111 {
24112 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
24113 args, tf_error, NULL_TREE,
24114 /*defer_ok*/false);
24115 if (specs && specs != error_mark_node)
24116 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
24117 specs);
24118 }
24119
24120 /* Merge parameter declarations. */
24121 decl_parm = skip_artificial_parms_for (decl,
24122 DECL_ARGUMENTS (decl));
24123 pattern_parm
24124 = skip_artificial_parms_for (code_pattern,
24125 DECL_ARGUMENTS (code_pattern));
24126 while (decl_parm && !DECL_PACK_P (pattern_parm))
24127 {
24128 tree parm_type;
24129 tree attributes;
24130
24131 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
24132 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
24133 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
24134 NULL_TREE);
24135 parm_type = type_decays_to (parm_type);
24136 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
24137 TREE_TYPE (decl_parm) = parm_type;
24138 attributes = DECL_ATTRIBUTES (pattern_parm);
24139 if (DECL_ATTRIBUTES (decl_parm) != attributes)
24140 {
24141 DECL_ATTRIBUTES (decl_parm) = attributes;
24142 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
24143 }
24144 decl_parm = DECL_CHAIN (decl_parm);
24145 pattern_parm = DECL_CHAIN (pattern_parm);
24146 }
24147 /* Merge any parameters that match with the function parameter
24148 pack. */
24149 if (pattern_parm && DECL_PACK_P (pattern_parm))
24150 {
24151 int i, len;
24152 tree expanded_types;
24153 /* Expand the TYPE_PACK_EXPANSION that provides the types for
24154 the parameters in this function parameter pack. */
24155 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
24156 args, tf_error, NULL_TREE);
24157 len = TREE_VEC_LENGTH (expanded_types);
24158 for (i = 0; i < len; i++)
24159 {
24160 tree parm_type;
24161 tree attributes;
24162
24163 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
24164 /* Rename the parameter to include the index. */
24165 DECL_NAME (decl_parm) =
24166 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
24167 parm_type = TREE_VEC_ELT (expanded_types, i);
24168 parm_type = type_decays_to (parm_type);
24169 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
24170 TREE_TYPE (decl_parm) = parm_type;
24171 attributes = DECL_ATTRIBUTES (pattern_parm);
24172 if (DECL_ATTRIBUTES (decl_parm) != attributes)
24173 {
24174 DECL_ATTRIBUTES (decl_parm) = attributes;
24175 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
24176 }
24177 decl_parm = DECL_CHAIN (decl_parm);
24178 }
24179 }
24180 /* Merge additional specifiers from the CODE_PATTERN. */
24181 if (DECL_DECLARED_INLINE_P (code_pattern)
24182 && !DECL_DECLARED_INLINE_P (decl))
24183 DECL_DECLARED_INLINE_P (decl) = 1;
24184
24185 maybe_instantiate_noexcept (decl, tf_error);
24186 }
24187 else if (VAR_P (decl))
24188 {
24189 start_lambda_scope (decl);
24190 DECL_INITIAL (decl) =
24191 tsubst_init (DECL_INITIAL (code_pattern), decl, args,
24192 tf_error, DECL_TI_TEMPLATE (decl));
24193 finish_lambda_scope ();
24194 if (VAR_HAD_UNKNOWN_BOUND (decl))
24195 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
24196 tf_error, DECL_TI_TEMPLATE (decl));
24197 }
24198 else
24199 gcc_unreachable ();
24200
24201 pop_access_scope (decl);
24202 }
24203
24204 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
24205 substituted to get DECL. */
24206
24207 tree
24208 template_for_substitution (tree decl)
24209 {
24210 tree tmpl = DECL_TI_TEMPLATE (decl);
24211
24212 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
24213 for the instantiation. This is not always the most general
24214 template. Consider, for example:
24215
24216 template <class T>
24217 struct S { template <class U> void f();
24218 template <> void f<int>(); };
24219
24220 and an instantiation of S<double>::f<int>. We want TD to be the
24221 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
24222 while (/* An instantiation cannot have a definition, so we need a
24223 more general template. */
24224 DECL_TEMPLATE_INSTANTIATION (tmpl)
24225 /* We must also deal with friend templates. Given:
24226
24227 template <class T> struct S {
24228 template <class U> friend void f() {};
24229 };
24230
24231 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
24232 so far as the language is concerned, but that's still
24233 where we get the pattern for the instantiation from. On
24234 other hand, if the definition comes outside the class, say:
24235
24236 template <class T> struct S {
24237 template <class U> friend void f();
24238 };
24239 template <class U> friend void f() {}
24240
24241 we don't need to look any further. That's what the check for
24242 DECL_INITIAL is for. */
24243 || (TREE_CODE (decl) == FUNCTION_DECL
24244 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
24245 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
24246 {
24247 /* The present template, TD, should not be a definition. If it
24248 were a definition, we should be using it! Note that we
24249 cannot restructure the loop to just keep going until we find
24250 a template with a definition, since that might go too far if
24251 a specialization was declared, but not defined. */
24252
24253 /* Fetch the more general template. */
24254 tmpl = DECL_TI_TEMPLATE (tmpl);
24255 }
24256
24257 return tmpl;
24258 }
24259
24260 /* Returns true if we need to instantiate this template instance even if we
24261 know we aren't going to emit it. */
24262
24263 bool
24264 always_instantiate_p (tree decl)
24265 {
24266 /* We always instantiate inline functions so that we can inline them. An
24267 explicit instantiation declaration prohibits implicit instantiation of
24268 non-inline functions. With high levels of optimization, we would
24269 normally inline non-inline functions -- but we're not allowed to do
24270 that for "extern template" functions. Therefore, we check
24271 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
24272 return ((TREE_CODE (decl) == FUNCTION_DECL
24273 && (DECL_DECLARED_INLINE_P (decl)
24274 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
24275 /* And we need to instantiate static data members so that
24276 their initializers are available in integral constant
24277 expressions. */
24278 || (VAR_P (decl)
24279 && decl_maybe_constant_var_p (decl)));
24280 }
24281
24282 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
24283 instantiate it now, modifying TREE_TYPE (fn). Returns false on
24284 error, true otherwise. */
24285
24286 bool
24287 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
24288 {
24289 tree fntype, spec, noex, clone;
24290
24291 /* Don't instantiate a noexcept-specification from template context. */
24292 if (processing_template_decl
24293 && (!flag_noexcept_type || type_dependent_expression_p (fn)))
24294 return true;
24295
24296 if (DECL_CLONED_FUNCTION_P (fn))
24297 fn = DECL_CLONED_FUNCTION (fn);
24298
24299 tree orig_fn = NULL_TREE;
24300 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
24301 its FUNCTION_DECL for the rest of this function -- push_access_scope
24302 doesn't accept TEMPLATE_DECLs. */
24303 if (DECL_FUNCTION_TEMPLATE_P (fn))
24304 {
24305 orig_fn = fn;
24306 fn = DECL_TEMPLATE_RESULT (fn);
24307 }
24308
24309 fntype = TREE_TYPE (fn);
24310 spec = TYPE_RAISES_EXCEPTIONS (fntype);
24311
24312 if (!spec || !TREE_PURPOSE (spec))
24313 return true;
24314
24315 noex = TREE_PURPOSE (spec);
24316
24317 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
24318 {
24319 static hash_set<tree>* fns = new hash_set<tree>;
24320 bool added = false;
24321 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
24322 {
24323 spec = get_defaulted_eh_spec (fn, complain);
24324 if (spec == error_mark_node)
24325 /* This might have failed because of an unparsed DMI, so
24326 let's try again later. */
24327 return false;
24328 }
24329 else if (!(added = !fns->add (fn)))
24330 {
24331 /* If hash_set::add returns true, the element was already there. */
24332 location_t loc = cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex),
24333 DECL_SOURCE_LOCATION (fn));
24334 error_at (loc,
24335 "exception specification of %qD depends on itself",
24336 fn);
24337 spec = noexcept_false_spec;
24338 }
24339 else if (push_tinst_level (fn))
24340 {
24341 push_to_top_level ();
24342 push_access_scope (fn);
24343 push_deferring_access_checks (dk_no_deferred);
24344 input_location = DECL_SOURCE_LOCATION (fn);
24345
24346 /* If needed, set current_class_ptr for the benefit of
24347 tsubst_copy/PARM_DECL. */
24348 tree tdecl = DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (fn));
24349 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tdecl))
24350 {
24351 tree this_parm = DECL_ARGUMENTS (tdecl);
24352 current_class_ptr = NULL_TREE;
24353 current_class_ref = cp_build_fold_indirect_ref (this_parm);
24354 current_class_ptr = this_parm;
24355 }
24356
24357 /* If this function is represented by a TEMPLATE_DECL, then
24358 the deferred noexcept-specification might still contain
24359 dependent types, even after substitution. And we need the
24360 dependency check functions to work in build_noexcept_spec. */
24361 if (orig_fn)
24362 ++processing_template_decl;
24363
24364 /* Do deferred instantiation of the noexcept-specifier. */
24365 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
24366 DEFERRED_NOEXCEPT_ARGS (noex),
24367 tf_warning_or_error, fn,
24368 /*function_p=*/false,
24369 /*i_c_e_p=*/true);
24370
24371 /* Build up the noexcept-specification. */
24372 spec = build_noexcept_spec (noex, tf_warning_or_error);
24373
24374 if (orig_fn)
24375 --processing_template_decl;
24376
24377 pop_deferring_access_checks ();
24378 pop_access_scope (fn);
24379 pop_tinst_level ();
24380 pop_from_top_level ();
24381 }
24382 else
24383 spec = noexcept_false_spec;
24384
24385 if (added)
24386 fns->remove (fn);
24387
24388 if (spec == error_mark_node)
24389 {
24390 /* This failed with a hard error, so let's go with false. */
24391 gcc_assert (seen_error ());
24392 spec = noexcept_false_spec;
24393 }
24394
24395 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
24396 if (orig_fn)
24397 TREE_TYPE (orig_fn) = TREE_TYPE (fn);
24398 }
24399
24400 FOR_EACH_CLONE (clone, fn)
24401 {
24402 if (TREE_TYPE (clone) == fntype)
24403 TREE_TYPE (clone) = TREE_TYPE (fn);
24404 else
24405 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
24406 }
24407
24408 return true;
24409 }
24410
24411 /* We're starting to process the function INST, an instantiation of PATTERN;
24412 add their parameters to local_specializations. */
24413
24414 static void
24415 register_parameter_specializations (tree pattern, tree inst)
24416 {
24417 tree tmpl_parm = DECL_ARGUMENTS (pattern);
24418 tree spec_parm = DECL_ARGUMENTS (inst);
24419 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
24420 {
24421 register_local_specialization (spec_parm, tmpl_parm);
24422 spec_parm = skip_artificial_parms_for (inst, spec_parm);
24423 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
24424 }
24425 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
24426 {
24427 if (!DECL_PACK_P (tmpl_parm))
24428 {
24429 register_local_specialization (spec_parm, tmpl_parm);
24430 spec_parm = DECL_CHAIN (spec_parm);
24431 }
24432 else
24433 {
24434 /* Register the (value) argument pack as a specialization of
24435 TMPL_PARM, then move on. */
24436 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
24437 register_local_specialization (argpack, tmpl_parm);
24438 }
24439 }
24440 gcc_assert (!spec_parm);
24441 }
24442
24443 /* Produce the definition of D, a _DECL generated from a template. If
24444 DEFER_OK is true, then we don't have to actually do the
24445 instantiation now; we just have to do it sometime. Normally it is
24446 an error if this is an explicit instantiation but D is undefined.
24447 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
24448 instantiated class template. */
24449
24450 tree
24451 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
24452 {
24453 tree tmpl = DECL_TI_TEMPLATE (d);
24454 tree gen_args;
24455 tree args;
24456 tree td;
24457 tree code_pattern;
24458 tree spec;
24459 tree gen_tmpl;
24460 bool pattern_defined;
24461 location_t saved_loc = input_location;
24462 int saved_unevaluated_operand = cp_unevaluated_operand;
24463 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
24464 bool external_p;
24465 bool deleted_p;
24466
24467 /* This function should only be used to instantiate templates for
24468 functions and static member variables. */
24469 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
24470
24471 /* A concept is never instantiated. */
24472 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
24473
24474 /* Variables are never deferred; if instantiation is required, they
24475 are instantiated right away. That allows for better code in the
24476 case that an expression refers to the value of the variable --
24477 if the variable has a constant value the referring expression can
24478 take advantage of that fact. */
24479 if (VAR_P (d))
24480 defer_ok = false;
24481
24482 /* Don't instantiate cloned functions. Instead, instantiate the
24483 functions they cloned. */
24484 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
24485 d = DECL_CLONED_FUNCTION (d);
24486
24487 if (DECL_TEMPLATE_INSTANTIATED (d)
24488 || (TREE_CODE (d) == FUNCTION_DECL
24489 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
24490 || DECL_TEMPLATE_SPECIALIZATION (d))
24491 /* D has already been instantiated or explicitly specialized, so
24492 there's nothing for us to do here.
24493
24494 It might seem reasonable to check whether or not D is an explicit
24495 instantiation, and, if so, stop here. But when an explicit
24496 instantiation is deferred until the end of the compilation,
24497 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
24498 the instantiation. */
24499 return d;
24500
24501 /* Check to see whether we know that this template will be
24502 instantiated in some other file, as with "extern template"
24503 extension. */
24504 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
24505
24506 /* In general, we do not instantiate such templates. */
24507 if (external_p && !always_instantiate_p (d))
24508 return d;
24509
24510 gen_tmpl = most_general_template (tmpl);
24511 gen_args = DECL_TI_ARGS (d);
24512
24513 if (tmpl != gen_tmpl)
24514 /* We should already have the extra args. */
24515 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
24516 == TMPL_ARGS_DEPTH (gen_args));
24517 /* And what's in the hash table should match D. */
24518 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
24519 || spec == NULL_TREE);
24520
24521 /* This needs to happen before any tsubsting. */
24522 if (! push_tinst_level (d))
24523 return d;
24524
24525 timevar_push (TV_TEMPLATE_INST);
24526
24527 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
24528 for the instantiation. */
24529 td = template_for_substitution (d);
24530 args = gen_args;
24531
24532 if (VAR_P (d))
24533 {
24534 /* Look up an explicit specialization, if any. */
24535 tree tid = lookup_template_variable (gen_tmpl, gen_args);
24536 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
24537 if (elt && elt != error_mark_node)
24538 {
24539 td = TREE_VALUE (elt);
24540 args = TREE_PURPOSE (elt);
24541 }
24542 }
24543
24544 code_pattern = DECL_TEMPLATE_RESULT (td);
24545
24546 /* We should never be trying to instantiate a member of a class
24547 template or partial specialization. */
24548 gcc_assert (d != code_pattern);
24549
24550 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
24551 || DECL_TEMPLATE_SPECIALIZATION (td))
24552 /* In the case of a friend template whose definition is provided
24553 outside the class, we may have too many arguments. Drop the
24554 ones we don't need. The same is true for specializations. */
24555 args = get_innermost_template_args
24556 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
24557
24558 if (TREE_CODE (d) == FUNCTION_DECL)
24559 {
24560 deleted_p = DECL_DELETED_FN (code_pattern);
24561 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
24562 && DECL_INITIAL (code_pattern) != error_mark_node)
24563 || DECL_DEFAULTED_FN (code_pattern)
24564 || deleted_p);
24565 }
24566 else
24567 {
24568 deleted_p = false;
24569 if (DECL_CLASS_SCOPE_P (code_pattern))
24570 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
24571 else
24572 pattern_defined = ! DECL_EXTERNAL (code_pattern);
24573 }
24574
24575 /* We may be in the middle of deferred access check. Disable it now. */
24576 push_deferring_access_checks (dk_no_deferred);
24577
24578 /* Unless an explicit instantiation directive has already determined
24579 the linkage of D, remember that a definition is available for
24580 this entity. */
24581 if (pattern_defined
24582 && !DECL_INTERFACE_KNOWN (d)
24583 && !DECL_NOT_REALLY_EXTERN (d))
24584 mark_definable (d);
24585
24586 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
24587 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
24588 input_location = DECL_SOURCE_LOCATION (d);
24589
24590 /* If D is a member of an explicitly instantiated class template,
24591 and no definition is available, treat it like an implicit
24592 instantiation. */
24593 if (!pattern_defined && expl_inst_class_mem_p
24594 && DECL_EXPLICIT_INSTANTIATION (d))
24595 {
24596 /* Leave linkage flags alone on instantiations with anonymous
24597 visibility. */
24598 if (TREE_PUBLIC (d))
24599 {
24600 DECL_NOT_REALLY_EXTERN (d) = 0;
24601 DECL_INTERFACE_KNOWN (d) = 0;
24602 }
24603 SET_DECL_IMPLICIT_INSTANTIATION (d);
24604 }
24605
24606 /* Defer all other templates, unless we have been explicitly
24607 forbidden from doing so. */
24608 if (/* If there is no definition, we cannot instantiate the
24609 template. */
24610 ! pattern_defined
24611 /* If it's OK to postpone instantiation, do so. */
24612 || defer_ok
24613 /* If this is a static data member that will be defined
24614 elsewhere, we don't want to instantiate the entire data
24615 member, but we do want to instantiate the initializer so that
24616 we can substitute that elsewhere. */
24617 || (external_p && VAR_P (d))
24618 /* Handle here a deleted function too, avoid generating
24619 its body (c++/61080). */
24620 || deleted_p)
24621 {
24622 /* The definition of the static data member is now required so
24623 we must substitute the initializer. */
24624 if (VAR_P (d)
24625 && !DECL_INITIAL (d)
24626 && DECL_INITIAL (code_pattern))
24627 {
24628 tree ns;
24629 tree init;
24630 bool const_init = false;
24631 bool enter_context = DECL_CLASS_SCOPE_P (d);
24632
24633 ns = decl_namespace_context (d);
24634 push_nested_namespace (ns);
24635 if (enter_context)
24636 push_nested_class (DECL_CONTEXT (d));
24637 init = tsubst_expr (DECL_INITIAL (code_pattern),
24638 args,
24639 tf_warning_or_error, NULL_TREE,
24640 /*integral_constant_expression_p=*/false);
24641 /* If instantiating the initializer involved instantiating this
24642 again, don't call cp_finish_decl twice. */
24643 if (!DECL_INITIAL (d))
24644 {
24645 /* Make sure the initializer is still constant, in case of
24646 circular dependency (template/instantiate6.C). */
24647 const_init
24648 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
24649 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
24650 /*asmspec_tree=*/NULL_TREE,
24651 LOOKUP_ONLYCONVERTING);
24652 }
24653 if (enter_context)
24654 pop_nested_class ();
24655 pop_nested_namespace (ns);
24656 }
24657
24658 /* We restore the source position here because it's used by
24659 add_pending_template. */
24660 input_location = saved_loc;
24661
24662 if (at_eof && !pattern_defined
24663 && DECL_EXPLICIT_INSTANTIATION (d)
24664 && DECL_NOT_REALLY_EXTERN (d))
24665 /* [temp.explicit]
24666
24667 The definition of a non-exported function template, a
24668 non-exported member function template, or a non-exported
24669 member function or static data member of a class template
24670 shall be present in every translation unit in which it is
24671 explicitly instantiated. */
24672 permerror (input_location, "explicit instantiation of %qD "
24673 "but no definition available", d);
24674
24675 /* If we're in unevaluated context, we just wanted to get the
24676 constant value; this isn't an odr use, so don't queue
24677 a full instantiation. */
24678 if (cp_unevaluated_operand != 0)
24679 goto out;
24680 /* ??? Historically, we have instantiated inline functions, even
24681 when marked as "extern template". */
24682 if (!(external_p && VAR_P (d)))
24683 add_pending_template (d);
24684 goto out;
24685 }
24686
24687 bool push_to_top, nested;
24688 tree fn_context;
24689 fn_context = decl_function_context (d);
24690 if (LAMBDA_FUNCTION_P (d))
24691 /* tsubst_lambda_expr resolved any references to enclosing functions. */
24692 fn_context = NULL_TREE;
24693 nested = current_function_decl != NULL_TREE;
24694 push_to_top = !(nested && fn_context == current_function_decl);
24695
24696 vec<tree> omp_privatization_save;
24697 if (nested)
24698 save_omp_privatization_clauses (omp_privatization_save);
24699
24700 if (push_to_top)
24701 push_to_top_level ();
24702 else
24703 {
24704 gcc_assert (!processing_template_decl);
24705 push_function_context ();
24706 cp_unevaluated_operand = 0;
24707 c_inhibit_evaluation_warnings = 0;
24708 }
24709
24710 /* Mark D as instantiated so that recursive calls to
24711 instantiate_decl do not try to instantiate it again. */
24712 DECL_TEMPLATE_INSTANTIATED (d) = 1;
24713
24714 /* Regenerate the declaration in case the template has been modified
24715 by a subsequent redeclaration. */
24716 regenerate_decl_from_template (d, td, args);
24717
24718 /* We already set the file and line above. Reset them now in case
24719 they changed as a result of calling regenerate_decl_from_template. */
24720 input_location = DECL_SOURCE_LOCATION (d);
24721
24722 if (VAR_P (d))
24723 {
24724 tree init;
24725 bool const_init = false;
24726
24727 /* Clear out DECL_RTL; whatever was there before may not be right
24728 since we've reset the type of the declaration. */
24729 SET_DECL_RTL (d, NULL);
24730 DECL_IN_AGGR_P (d) = 0;
24731
24732 /* The initializer is placed in DECL_INITIAL by
24733 regenerate_decl_from_template so we don't need to
24734 push/pop_access_scope again here. Pull it out so that
24735 cp_finish_decl can process it. */
24736 init = DECL_INITIAL (d);
24737 DECL_INITIAL (d) = NULL_TREE;
24738 DECL_INITIALIZED_P (d) = 0;
24739
24740 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
24741 initializer. That function will defer actual emission until
24742 we have a chance to determine linkage. */
24743 DECL_EXTERNAL (d) = 0;
24744
24745 /* Enter the scope of D so that access-checking works correctly. */
24746 bool enter_context = DECL_CLASS_SCOPE_P (d);
24747 if (enter_context)
24748 push_nested_class (DECL_CONTEXT (d));
24749
24750 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
24751 int flags = (TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (d))
24752 ? LOOKUP_CONSTINIT : 0);
24753 cp_finish_decl (d, init, const_init, NULL_TREE, flags);
24754
24755 if (enter_context)
24756 pop_nested_class ();
24757
24758 if (variable_template_p (gen_tmpl))
24759 note_variable_template_instantiation (d);
24760 }
24761 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
24762 synthesize_method (d);
24763 else if (TREE_CODE (d) == FUNCTION_DECL)
24764 {
24765 /* Set up the list of local specializations. */
24766 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
24767 tree block = NULL_TREE;
24768
24769 /* Set up context. */
24770 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
24771 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
24772 block = push_stmt_list ();
24773 else
24774 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
24775
24776 /* Some typedefs referenced from within the template code need to be
24777 access checked at template instantiation time, i.e now. These
24778 types were added to the template at parsing time. Let's get those
24779 and perform the access checks then. */
24780 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
24781 args);
24782
24783 /* Create substitution entries for the parameters. */
24784 register_parameter_specializations (code_pattern, d);
24785
24786 /* Substitute into the body of the function. */
24787 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
24788 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
24789 tf_warning_or_error, tmpl);
24790 else
24791 {
24792 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
24793 tf_warning_or_error, tmpl,
24794 /*integral_constant_expression_p=*/false);
24795
24796 /* Set the current input_location to the end of the function
24797 so that finish_function knows where we are. */
24798 input_location
24799 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
24800
24801 /* Remember if we saw an infinite loop in the template. */
24802 current_function_infinite_loop
24803 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
24804 }
24805
24806 /* Finish the function. */
24807 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
24808 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
24809 DECL_SAVED_TREE (d) = pop_stmt_list (block);
24810 else
24811 {
24812 d = finish_function (/*inline_p=*/false);
24813 expand_or_defer_fn (d);
24814 }
24815
24816 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
24817 cp_check_omp_declare_reduction (d);
24818 }
24819
24820 /* We're not deferring instantiation any more. */
24821 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
24822
24823 if (push_to_top)
24824 pop_from_top_level ();
24825 else
24826 pop_function_context ();
24827
24828 if (nested)
24829 restore_omp_privatization_clauses (omp_privatization_save);
24830
24831 out:
24832 pop_deferring_access_checks ();
24833 timevar_pop (TV_TEMPLATE_INST);
24834 pop_tinst_level ();
24835 input_location = saved_loc;
24836 cp_unevaluated_operand = saved_unevaluated_operand;
24837 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
24838
24839 return d;
24840 }
24841
24842 /* Run through the list of templates that we wish we could
24843 instantiate, and instantiate any we can. RETRIES is the
24844 number of times we retry pending template instantiation. */
24845
24846 void
24847 instantiate_pending_templates (int retries)
24848 {
24849 int reconsider;
24850 location_t saved_loc = input_location;
24851
24852 /* Instantiating templates may trigger vtable generation. This in turn
24853 may require further template instantiations. We place a limit here
24854 to avoid infinite loop. */
24855 if (pending_templates && retries >= max_tinst_depth)
24856 {
24857 tree decl = pending_templates->tinst->maybe_get_node ();
24858
24859 fatal_error (input_location,
24860 "template instantiation depth exceeds maximum of %d"
24861 " instantiating %q+D, possibly from virtual table generation"
24862 " (use %<-ftemplate-depth=%> to increase the maximum)",
24863 max_tinst_depth, decl);
24864 if (TREE_CODE (decl) == FUNCTION_DECL)
24865 /* Pretend that we defined it. */
24866 DECL_INITIAL (decl) = error_mark_node;
24867 return;
24868 }
24869
24870 do
24871 {
24872 struct pending_template **t = &pending_templates;
24873 struct pending_template *last = NULL;
24874 reconsider = 0;
24875 while (*t)
24876 {
24877 tree instantiation = reopen_tinst_level ((*t)->tinst);
24878 bool complete = false;
24879
24880 if (TYPE_P (instantiation))
24881 {
24882 if (!COMPLETE_TYPE_P (instantiation))
24883 {
24884 instantiate_class_template (instantiation);
24885 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
24886 for (tree fld = TYPE_FIELDS (instantiation);
24887 fld; fld = TREE_CHAIN (fld))
24888 if ((VAR_P (fld)
24889 || (TREE_CODE (fld) == FUNCTION_DECL
24890 && !DECL_ARTIFICIAL (fld)))
24891 && DECL_TEMPLATE_INSTANTIATION (fld))
24892 instantiate_decl (fld,
24893 /*defer_ok=*/false,
24894 /*expl_inst_class_mem_p=*/false);
24895
24896 if (COMPLETE_TYPE_P (instantiation))
24897 reconsider = 1;
24898 }
24899
24900 complete = COMPLETE_TYPE_P (instantiation);
24901 }
24902 else
24903 {
24904 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
24905 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
24906 {
24907 instantiation
24908 = instantiate_decl (instantiation,
24909 /*defer_ok=*/false,
24910 /*expl_inst_class_mem_p=*/false);
24911 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
24912 reconsider = 1;
24913 }
24914
24915 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
24916 || DECL_TEMPLATE_INSTANTIATED (instantiation));
24917 }
24918
24919 if (complete)
24920 {
24921 /* If INSTANTIATION has been instantiated, then we don't
24922 need to consider it again in the future. */
24923 struct pending_template *drop = *t;
24924 *t = (*t)->next;
24925 set_refcount_ptr (drop->tinst);
24926 pending_template_freelist ().free (drop);
24927 }
24928 else
24929 {
24930 last = *t;
24931 t = &(*t)->next;
24932 }
24933 tinst_depth = 0;
24934 set_refcount_ptr (current_tinst_level);
24935 }
24936 last_pending_template = last;
24937 }
24938 while (reconsider);
24939
24940 input_location = saved_loc;
24941 }
24942
24943 /* Substitute ARGVEC into T, which is a list of initializers for
24944 either base class or a non-static data member. The TREE_PURPOSEs
24945 are DECLs, and the TREE_VALUEs are the initializer values. Used by
24946 instantiate_decl. */
24947
24948 static tree
24949 tsubst_initializer_list (tree t, tree argvec)
24950 {
24951 tree inits = NULL_TREE;
24952 tree target_ctor = error_mark_node;
24953
24954 for (; t; t = TREE_CHAIN (t))
24955 {
24956 tree decl;
24957 tree init;
24958 tree expanded_bases = NULL_TREE;
24959 tree expanded_arguments = NULL_TREE;
24960 int i, len = 1;
24961
24962 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
24963 {
24964 tree expr;
24965 tree arg;
24966
24967 /* Expand the base class expansion type into separate base
24968 classes. */
24969 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
24970 tf_warning_or_error,
24971 NULL_TREE);
24972 if (expanded_bases == error_mark_node)
24973 continue;
24974
24975 /* We'll be building separate TREE_LISTs of arguments for
24976 each base. */
24977 len = TREE_VEC_LENGTH (expanded_bases);
24978 expanded_arguments = make_tree_vec (len);
24979 for (i = 0; i < len; i++)
24980 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
24981
24982 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
24983 expand each argument in the TREE_VALUE of t. */
24984 expr = make_node (EXPR_PACK_EXPANSION);
24985 PACK_EXPANSION_LOCAL_P (expr) = true;
24986 PACK_EXPANSION_PARAMETER_PACKS (expr) =
24987 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
24988
24989 if (TREE_VALUE (t) == void_type_node)
24990 /* VOID_TYPE_NODE is used to indicate
24991 value-initialization. */
24992 {
24993 for (i = 0; i < len; i++)
24994 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
24995 }
24996 else
24997 {
24998 /* Substitute parameter packs into each argument in the
24999 TREE_LIST. */
25000 in_base_initializer = 1;
25001 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
25002 {
25003 tree expanded_exprs;
25004
25005 /* Expand the argument. */
25006 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
25007 expanded_exprs
25008 = tsubst_pack_expansion (expr, argvec,
25009 tf_warning_or_error,
25010 NULL_TREE);
25011 if (expanded_exprs == error_mark_node)
25012 continue;
25013
25014 /* Prepend each of the expanded expressions to the
25015 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
25016 for (i = 0; i < len; i++)
25017 {
25018 TREE_VEC_ELT (expanded_arguments, i) =
25019 tree_cons (NULL_TREE,
25020 TREE_VEC_ELT (expanded_exprs, i),
25021 TREE_VEC_ELT (expanded_arguments, i));
25022 }
25023 }
25024 in_base_initializer = 0;
25025
25026 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
25027 since we built them backwards. */
25028 for (i = 0; i < len; i++)
25029 {
25030 TREE_VEC_ELT (expanded_arguments, i) =
25031 nreverse (TREE_VEC_ELT (expanded_arguments, i));
25032 }
25033 }
25034 }
25035
25036 for (i = 0; i < len; ++i)
25037 {
25038 if (expanded_bases)
25039 {
25040 decl = TREE_VEC_ELT (expanded_bases, i);
25041 decl = expand_member_init (decl);
25042 init = TREE_VEC_ELT (expanded_arguments, i);
25043 }
25044 else
25045 {
25046 tree tmp;
25047 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
25048 tf_warning_or_error, NULL_TREE);
25049
25050 decl = expand_member_init (decl);
25051 if (decl && !DECL_P (decl))
25052 in_base_initializer = 1;
25053
25054 init = TREE_VALUE (t);
25055 tmp = init;
25056 if (init != void_type_node)
25057 init = tsubst_expr (init, argvec,
25058 tf_warning_or_error, NULL_TREE,
25059 /*integral_constant_expression_p=*/false);
25060 if (init == NULL_TREE && tmp != NULL_TREE)
25061 /* If we had an initializer but it instantiated to nothing,
25062 value-initialize the object. This will only occur when
25063 the initializer was a pack expansion where the parameter
25064 packs used in that expansion were of length zero. */
25065 init = void_type_node;
25066 in_base_initializer = 0;
25067 }
25068
25069 if (target_ctor != error_mark_node
25070 && init != error_mark_node)
25071 {
25072 error ("mem-initializer for %qD follows constructor delegation",
25073 decl);
25074 return inits;
25075 }
25076 /* Look for a target constructor. */
25077 if (init != error_mark_node
25078 && decl && CLASS_TYPE_P (decl)
25079 && same_type_p (decl, current_class_type))
25080 {
25081 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
25082 if (inits)
25083 {
25084 error ("constructor delegation follows mem-initializer for %qD",
25085 TREE_PURPOSE (inits));
25086 continue;
25087 }
25088 target_ctor = init;
25089 }
25090
25091 if (decl)
25092 {
25093 init = build_tree_list (decl, init);
25094 TREE_CHAIN (init) = inits;
25095 inits = init;
25096 }
25097 }
25098 }
25099 return inits;
25100 }
25101
25102 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
25103
25104 static void
25105 set_current_access_from_decl (tree decl)
25106 {
25107 if (TREE_PRIVATE (decl))
25108 current_access_specifier = access_private_node;
25109 else if (TREE_PROTECTED (decl))
25110 current_access_specifier = access_protected_node;
25111 else
25112 current_access_specifier = access_public_node;
25113 }
25114
25115 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
25116 is the instantiation (which should have been created with
25117 start_enum) and ARGS are the template arguments to use. */
25118
25119 static void
25120 tsubst_enum (tree tag, tree newtag, tree args)
25121 {
25122 tree e;
25123
25124 if (SCOPED_ENUM_P (newtag))
25125 begin_scope (sk_scoped_enum, newtag);
25126
25127 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
25128 {
25129 tree value;
25130 tree decl;
25131
25132 decl = TREE_VALUE (e);
25133 /* Note that in a template enum, the TREE_VALUE is the
25134 CONST_DECL, not the corresponding INTEGER_CST. */
25135 value = tsubst_expr (DECL_INITIAL (decl),
25136 args, tf_warning_or_error, NULL_TREE,
25137 /*integral_constant_expression_p=*/true);
25138
25139 /* Give this enumeration constant the correct access. */
25140 set_current_access_from_decl (decl);
25141
25142 /* Actually build the enumerator itself. Here we're assuming that
25143 enumerators can't have dependent attributes. */
25144 build_enumerator (DECL_NAME (decl), value, newtag,
25145 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
25146 }
25147
25148 if (SCOPED_ENUM_P (newtag))
25149 finish_scope ();
25150
25151 finish_enum_value_list (newtag);
25152 finish_enum (newtag);
25153
25154 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
25155 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
25156 }
25157
25158 /* DECL is a FUNCTION_DECL that is a template specialization. Return
25159 its type -- but without substituting the innermost set of template
25160 arguments. So, innermost set of template parameters will appear in
25161 the type. */
25162
25163 tree
25164 get_mostly_instantiated_function_type (tree decl)
25165 {
25166 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
25167 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
25168 }
25169
25170 /* Return truthvalue if we're processing a template different from
25171 the last one involved in diagnostics. */
25172 bool
25173 problematic_instantiation_changed (void)
25174 {
25175 return current_tinst_level != last_error_tinst_level;
25176 }
25177
25178 /* Remember current template involved in diagnostics. */
25179 void
25180 record_last_problematic_instantiation (void)
25181 {
25182 set_refcount_ptr (last_error_tinst_level, current_tinst_level);
25183 }
25184
25185 struct tinst_level *
25186 current_instantiation (void)
25187 {
25188 return current_tinst_level;
25189 }
25190
25191 /* Return TRUE if current_function_decl is being instantiated, false
25192 otherwise. */
25193
25194 bool
25195 instantiating_current_function_p (void)
25196 {
25197 return (current_instantiation ()
25198 && (current_instantiation ()->maybe_get_node ()
25199 == current_function_decl));
25200 }
25201
25202 /* [temp.param] Check that template non-type parm TYPE is of an allowable
25203 type. Return false for ok, true for disallowed. Issue error and
25204 inform messages under control of COMPLAIN. */
25205
25206 static bool
25207 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
25208 {
25209 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
25210 return false;
25211 else if (TYPE_PTR_P (type))
25212 return false;
25213 else if (TYPE_REF_P (type)
25214 && !TYPE_REF_IS_RVALUE (type))
25215 return false;
25216 else if (TYPE_PTRMEM_P (type))
25217 return false;
25218 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
25219 return false;
25220 else if (TREE_CODE (type) == TYPENAME_TYPE)
25221 return false;
25222 else if (TREE_CODE (type) == DECLTYPE_TYPE)
25223 return false;
25224 else if (TREE_CODE (type) == NULLPTR_TYPE)
25225 return false;
25226 /* A bound template template parm could later be instantiated to have a valid
25227 nontype parm type via an alias template. */
25228 else if (cxx_dialect >= cxx11
25229 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
25230 return false;
25231 else if (CLASS_TYPE_P (type))
25232 {
25233 if (cxx_dialect < cxx2a)
25234 {
25235 if (complain & tf_error)
25236 error ("non-type template parameters of class type only available "
25237 "with %<-std=c++2a%> or %<-std=gnu++2a%>");
25238 return true;
25239 }
25240 if (dependent_type_p (type))
25241 return false;
25242 if (!complete_type_or_else (type, NULL_TREE))
25243 return true;
25244 if (!literal_type_p (type))
25245 {
25246 error ("%qT is not a valid type for a template non-type parameter "
25247 "because it is not literal", type);
25248 explain_non_literal_class (type);
25249 return true;
25250 }
25251 if (cp_has_mutable_p (type))
25252 {
25253 error ("%qT is not a valid type for a template non-type parameter "
25254 "because it has a mutable member", type);
25255 return true;
25256 }
25257 /* FIXME check op<=> and strong structural equality once spaceship is
25258 implemented. */
25259 return false;
25260 }
25261
25262 if (complain & tf_error)
25263 {
25264 if (type == error_mark_node)
25265 inform (input_location, "invalid template non-type parameter");
25266 else
25267 error ("%q#T is not a valid type for a template non-type parameter",
25268 type);
25269 }
25270 return true;
25271 }
25272
25273 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
25274 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
25275
25276 static bool
25277 dependent_type_p_r (tree type)
25278 {
25279 tree scope;
25280
25281 /* [temp.dep.type]
25282
25283 A type is dependent if it is:
25284
25285 -- a template parameter. Template template parameters are types
25286 for us (since TYPE_P holds true for them) so we handle
25287 them here. */
25288 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
25289 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
25290 return true;
25291 /* -- a qualified-id with a nested-name-specifier which contains a
25292 class-name that names a dependent type or whose unqualified-id
25293 names a dependent type. */
25294 if (TREE_CODE (type) == TYPENAME_TYPE)
25295 return true;
25296
25297 /* An alias template specialization can be dependent even if the
25298 resulting type is not. */
25299 if (dependent_alias_template_spec_p (type))
25300 return true;
25301
25302 /* -- a cv-qualified type where the cv-unqualified type is
25303 dependent.
25304 No code is necessary for this bullet; the code below handles
25305 cv-qualified types, and we don't want to strip aliases with
25306 TYPE_MAIN_VARIANT because of DR 1558. */
25307 /* -- a compound type constructed from any dependent type. */
25308 if (TYPE_PTRMEM_P (type))
25309 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
25310 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
25311 (type)));
25312 else if (INDIRECT_TYPE_P (type))
25313 return dependent_type_p (TREE_TYPE (type));
25314 else if (FUNC_OR_METHOD_TYPE_P (type))
25315 {
25316 tree arg_type;
25317
25318 if (dependent_type_p (TREE_TYPE (type)))
25319 return true;
25320 for (arg_type = TYPE_ARG_TYPES (type);
25321 arg_type;
25322 arg_type = TREE_CHAIN (arg_type))
25323 if (dependent_type_p (TREE_VALUE (arg_type)))
25324 return true;
25325 if (cxx_dialect >= cxx17)
25326 /* A value-dependent noexcept-specifier makes the type dependent. */
25327 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
25328 if (tree noex = TREE_PURPOSE (spec))
25329 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
25330 affect overload resolution and treating it as dependent breaks
25331 things. Same for an unparsed noexcept expression. */
25332 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
25333 && TREE_CODE (noex) != DEFERRED_PARSE
25334 && value_dependent_expression_p (noex))
25335 return true;
25336 return false;
25337 }
25338 /* -- an array type constructed from any dependent type or whose
25339 size is specified by a constant expression that is
25340 value-dependent.
25341
25342 We checked for type- and value-dependence of the bounds in
25343 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
25344 if (TREE_CODE (type) == ARRAY_TYPE)
25345 {
25346 if (TYPE_DOMAIN (type)
25347 && dependent_type_p (TYPE_DOMAIN (type)))
25348 return true;
25349 return dependent_type_p (TREE_TYPE (type));
25350 }
25351
25352 /* -- a template-id in which either the template name is a template
25353 parameter ... */
25354 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
25355 return true;
25356 /* ... or any of the template arguments is a dependent type or
25357 an expression that is type-dependent or value-dependent. */
25358 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
25359 && (any_dependent_template_arguments_p
25360 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
25361 return true;
25362
25363 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
25364 dependent; if the argument of the `typeof' expression is not
25365 type-dependent, then it should already been have resolved. */
25366 if (TREE_CODE (type) == TYPEOF_TYPE
25367 || TREE_CODE (type) == DECLTYPE_TYPE
25368 || TREE_CODE (type) == UNDERLYING_TYPE)
25369 return true;
25370
25371 /* A template argument pack is dependent if any of its packed
25372 arguments are. */
25373 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
25374 {
25375 tree args = ARGUMENT_PACK_ARGS (type);
25376 int i, len = TREE_VEC_LENGTH (args);
25377 for (i = 0; i < len; ++i)
25378 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
25379 return true;
25380 }
25381
25382 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
25383 be template parameters. */
25384 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
25385 return true;
25386
25387 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
25388 return true;
25389
25390 /* The standard does not specifically mention types that are local
25391 to template functions or local classes, but they should be
25392 considered dependent too. For example:
25393
25394 template <int I> void f() {
25395 enum E { a = I };
25396 S<sizeof (E)> s;
25397 }
25398
25399 The size of `E' cannot be known until the value of `I' has been
25400 determined. Therefore, `E' must be considered dependent. */
25401 scope = TYPE_CONTEXT (type);
25402 if (scope && TYPE_P (scope))
25403 return dependent_type_p (scope);
25404 /* Don't use type_dependent_expression_p here, as it can lead
25405 to infinite recursion trying to determine whether a lambda
25406 nested in a lambda is dependent (c++/47687). */
25407 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
25408 && DECL_LANG_SPECIFIC (scope)
25409 && DECL_TEMPLATE_INFO (scope)
25410 && (any_dependent_template_arguments_p
25411 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
25412 return true;
25413
25414 /* Other types are non-dependent. */
25415 return false;
25416 }
25417
25418 /* Returns TRUE if TYPE is dependent, in the sense of
25419 [temp.dep.type]. Note that a NULL type is considered dependent. */
25420
25421 bool
25422 dependent_type_p (tree type)
25423 {
25424 /* If there are no template parameters in scope, then there can't be
25425 any dependent types. */
25426 if (!processing_template_decl)
25427 {
25428 /* If we are not processing a template, then nobody should be
25429 providing us with a dependent type. */
25430 gcc_assert (type);
25431 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
25432 return false;
25433 }
25434
25435 /* If the type is NULL, we have not computed a type for the entity
25436 in question; in that case, the type is dependent. */
25437 if (!type)
25438 return true;
25439
25440 /* Erroneous types can be considered non-dependent. */
25441 if (type == error_mark_node)
25442 return false;
25443
25444 /* Getting here with global_type_node means we improperly called this
25445 function on the TREE_TYPE of an IDENTIFIER_NODE. */
25446 gcc_checking_assert (type != global_type_node);
25447
25448 /* If we have not already computed the appropriate value for TYPE,
25449 do so now. */
25450 if (!TYPE_DEPENDENT_P_VALID (type))
25451 {
25452 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
25453 TYPE_DEPENDENT_P_VALID (type) = 1;
25454 }
25455
25456 return TYPE_DEPENDENT_P (type);
25457 }
25458
25459 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
25460 lookup. In other words, a dependent type that is not the current
25461 instantiation. */
25462
25463 bool
25464 dependent_scope_p (tree scope)
25465 {
25466 return (scope && TYPE_P (scope) && dependent_type_p (scope)
25467 && !currently_open_class (scope));
25468 }
25469
25470 /* T is a SCOPE_REF. Return whether it represents a non-static member of
25471 an unknown base of 'this' (and is therefore instantiation-dependent). */
25472
25473 static bool
25474 unknown_base_ref_p (tree t)
25475 {
25476 if (!current_class_ptr)
25477 return false;
25478
25479 tree mem = TREE_OPERAND (t, 1);
25480 if (shared_member_p (mem))
25481 return false;
25482
25483 tree cur = current_nonlambda_class_type ();
25484 if (!any_dependent_bases_p (cur))
25485 return false;
25486
25487 tree ctx = TREE_OPERAND (t, 0);
25488 if (DERIVED_FROM_P (ctx, cur))
25489 return false;
25490
25491 return true;
25492 }
25493
25494 /* T is a SCOPE_REF; return whether we need to consider it
25495 instantiation-dependent so that we can check access at instantiation
25496 time even though we know which member it resolves to. */
25497
25498 static bool
25499 instantiation_dependent_scope_ref_p (tree t)
25500 {
25501 if (DECL_P (TREE_OPERAND (t, 1))
25502 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
25503 && !unknown_base_ref_p (t)
25504 && accessible_in_template_p (TREE_OPERAND (t, 0),
25505 TREE_OPERAND (t, 1)))
25506 return false;
25507 else
25508 return true;
25509 }
25510
25511 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
25512 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
25513 expression. */
25514
25515 /* Note that this predicate is not appropriate for general expressions;
25516 only constant expressions (that satisfy potential_constant_expression)
25517 can be tested for value dependence. */
25518
25519 bool
25520 value_dependent_expression_p (tree expression)
25521 {
25522 if (!processing_template_decl || expression == NULL_TREE)
25523 return false;
25524
25525 /* A type-dependent expression is also value-dependent. */
25526 if (type_dependent_expression_p (expression))
25527 return true;
25528
25529 switch (TREE_CODE (expression))
25530 {
25531 case BASELINK:
25532 /* A dependent member function of the current instantiation. */
25533 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
25534
25535 case FUNCTION_DECL:
25536 /* A dependent member function of the current instantiation. */
25537 if (DECL_CLASS_SCOPE_P (expression)
25538 && dependent_type_p (DECL_CONTEXT (expression)))
25539 return true;
25540 break;
25541
25542 case IDENTIFIER_NODE:
25543 /* A name that has not been looked up -- must be dependent. */
25544 return true;
25545
25546 case TEMPLATE_PARM_INDEX:
25547 /* A non-type template parm. */
25548 return true;
25549
25550 case CONST_DECL:
25551 /* A non-type template parm. */
25552 if (DECL_TEMPLATE_PARM_P (expression))
25553 return true;
25554 return value_dependent_expression_p (DECL_INITIAL (expression));
25555
25556 case VAR_DECL:
25557 /* A constant with literal type and is initialized
25558 with an expression that is value-dependent. */
25559 if (DECL_DEPENDENT_INIT_P (expression)
25560 /* FIXME cp_finish_decl doesn't fold reference initializers. */
25561 || TYPE_REF_P (TREE_TYPE (expression)))
25562 return true;
25563 if (DECL_HAS_VALUE_EXPR_P (expression))
25564 {
25565 tree value_expr = DECL_VALUE_EXPR (expression);
25566 if (value_dependent_expression_p (value_expr)
25567 /* __PRETTY_FUNCTION__ inside a template function is dependent
25568 on the name of the function. */
25569 || (DECL_PRETTY_FUNCTION_P (expression)
25570 /* It might be used in a template, but not a template
25571 function, in which case its DECL_VALUE_EXPR will be
25572 "top level". */
25573 && value_expr == error_mark_node))
25574 return true;
25575 }
25576 return false;
25577
25578 case DYNAMIC_CAST_EXPR:
25579 case STATIC_CAST_EXPR:
25580 case CONST_CAST_EXPR:
25581 case REINTERPRET_CAST_EXPR:
25582 case CAST_EXPR:
25583 case IMPLICIT_CONV_EXPR:
25584 /* These expressions are value-dependent if the type to which
25585 the cast occurs is dependent or the expression being casted
25586 is value-dependent. */
25587 {
25588 tree type = TREE_TYPE (expression);
25589
25590 if (dependent_type_p (type))
25591 return true;
25592
25593 /* A functional cast has a list of operands. */
25594 expression = TREE_OPERAND (expression, 0);
25595 if (!expression)
25596 {
25597 /* If there are no operands, it must be an expression such
25598 as "int()". This should not happen for aggregate types
25599 because it would form non-constant expressions. */
25600 gcc_assert (cxx_dialect >= cxx11
25601 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
25602
25603 return false;
25604 }
25605
25606 if (TREE_CODE (expression) == TREE_LIST)
25607 return any_value_dependent_elements_p (expression);
25608
25609 return value_dependent_expression_p (expression);
25610 }
25611
25612 case SIZEOF_EXPR:
25613 if (SIZEOF_EXPR_TYPE_P (expression))
25614 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
25615 /* FALLTHRU */
25616 case ALIGNOF_EXPR:
25617 case TYPEID_EXPR:
25618 /* A `sizeof' expression is value-dependent if the operand is
25619 type-dependent or is a pack expansion. */
25620 expression = TREE_OPERAND (expression, 0);
25621 if (PACK_EXPANSION_P (expression))
25622 return true;
25623 else if (TYPE_P (expression))
25624 return dependent_type_p (expression);
25625 return instantiation_dependent_uneval_expression_p (expression);
25626
25627 case AT_ENCODE_EXPR:
25628 /* An 'encode' expression is value-dependent if the operand is
25629 type-dependent. */
25630 expression = TREE_OPERAND (expression, 0);
25631 return dependent_type_p (expression);
25632
25633 case NOEXCEPT_EXPR:
25634 expression = TREE_OPERAND (expression, 0);
25635 return instantiation_dependent_uneval_expression_p (expression);
25636
25637 case SCOPE_REF:
25638 /* All instantiation-dependent expressions should also be considered
25639 value-dependent. */
25640 return instantiation_dependent_scope_ref_p (expression);
25641
25642 case COMPONENT_REF:
25643 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
25644 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
25645
25646 case NONTYPE_ARGUMENT_PACK:
25647 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
25648 is value-dependent. */
25649 {
25650 tree values = ARGUMENT_PACK_ARGS (expression);
25651 int i, len = TREE_VEC_LENGTH (values);
25652
25653 for (i = 0; i < len; ++i)
25654 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
25655 return true;
25656
25657 return false;
25658 }
25659
25660 case TRAIT_EXPR:
25661 {
25662 tree type2 = TRAIT_EXPR_TYPE2 (expression);
25663
25664 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
25665 return true;
25666
25667 if (!type2)
25668 return false;
25669
25670 if (TREE_CODE (type2) != TREE_LIST)
25671 return dependent_type_p (type2);
25672
25673 for (; type2; type2 = TREE_CHAIN (type2))
25674 if (dependent_type_p (TREE_VALUE (type2)))
25675 return true;
25676
25677 return false;
25678 }
25679
25680 case MODOP_EXPR:
25681 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
25682 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
25683
25684 case ARRAY_REF:
25685 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
25686 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
25687
25688 case ADDR_EXPR:
25689 {
25690 tree op = TREE_OPERAND (expression, 0);
25691 return (value_dependent_expression_p (op)
25692 || has_value_dependent_address (op));
25693 }
25694
25695 case REQUIRES_EXPR:
25696 /* Treat all requires-expressions as value-dependent so
25697 we don't try to fold them. */
25698 return true;
25699
25700 case TYPE_REQ:
25701 return dependent_type_p (TREE_OPERAND (expression, 0));
25702
25703 case CALL_EXPR:
25704 {
25705 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
25706 return true;
25707 tree fn = get_callee_fndecl (expression);
25708 int i, nargs;
25709 nargs = call_expr_nargs (expression);
25710 for (i = 0; i < nargs; ++i)
25711 {
25712 tree op = CALL_EXPR_ARG (expression, i);
25713 /* In a call to a constexpr member function, look through the
25714 implicit ADDR_EXPR on the object argument so that it doesn't
25715 cause the call to be considered value-dependent. We also
25716 look through it in potential_constant_expression. */
25717 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
25718 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
25719 && TREE_CODE (op) == ADDR_EXPR)
25720 op = TREE_OPERAND (op, 0);
25721 if (value_dependent_expression_p (op))
25722 return true;
25723 }
25724 return false;
25725 }
25726
25727 case TEMPLATE_ID_EXPR:
25728 return variable_concept_p (TREE_OPERAND (expression, 0));
25729
25730 case CONSTRUCTOR:
25731 {
25732 unsigned ix;
25733 tree val;
25734 if (dependent_type_p (TREE_TYPE (expression)))
25735 return true;
25736 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
25737 if (value_dependent_expression_p (val))
25738 return true;
25739 return false;
25740 }
25741
25742 case STMT_EXPR:
25743 /* Treat a GNU statement expression as dependent to avoid crashing
25744 under instantiate_non_dependent_expr; it can't be constant. */
25745 return true;
25746
25747 default:
25748 /* A constant expression is value-dependent if any subexpression is
25749 value-dependent. */
25750 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
25751 {
25752 case tcc_reference:
25753 case tcc_unary:
25754 case tcc_comparison:
25755 case tcc_binary:
25756 case tcc_expression:
25757 case tcc_vl_exp:
25758 {
25759 int i, len = cp_tree_operand_length (expression);
25760
25761 for (i = 0; i < len; i++)
25762 {
25763 tree t = TREE_OPERAND (expression, i);
25764
25765 /* In some cases, some of the operands may be missing.
25766 (For example, in the case of PREDECREMENT_EXPR, the
25767 amount to increment by may be missing.) That doesn't
25768 make the expression dependent. */
25769 if (t && value_dependent_expression_p (t))
25770 return true;
25771 }
25772 }
25773 break;
25774 default:
25775 break;
25776 }
25777 break;
25778 }
25779
25780 /* The expression is not value-dependent. */
25781 return false;
25782 }
25783
25784 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
25785 [temp.dep.expr]. Note that an expression with no type is
25786 considered dependent. Other parts of the compiler arrange for an
25787 expression with type-dependent subexpressions to have no type, so
25788 this function doesn't have to be fully recursive. */
25789
25790 bool
25791 type_dependent_expression_p (tree expression)
25792 {
25793 if (!processing_template_decl)
25794 return false;
25795
25796 if (expression == NULL_TREE || expression == error_mark_node)
25797 return false;
25798
25799 STRIP_ANY_LOCATION_WRAPPER (expression);
25800
25801 /* An unresolved name is always dependent. */
25802 if (identifier_p (expression)
25803 || TREE_CODE (expression) == USING_DECL
25804 || TREE_CODE (expression) == WILDCARD_DECL)
25805 return true;
25806
25807 /* A lambda-expression in template context is dependent. dependent_type_p is
25808 true for a lambda in the scope of a class or function template, but that
25809 doesn't cover all template contexts, like a default template argument. */
25810 if (TREE_CODE (expression) == LAMBDA_EXPR)
25811 return true;
25812
25813 /* A fold expression is type-dependent. */
25814 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
25815 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
25816 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
25817 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
25818 return true;
25819
25820 /* Some expression forms are never type-dependent. */
25821 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
25822 || TREE_CODE (expression) == SIZEOF_EXPR
25823 || TREE_CODE (expression) == ALIGNOF_EXPR
25824 || TREE_CODE (expression) == AT_ENCODE_EXPR
25825 || TREE_CODE (expression) == NOEXCEPT_EXPR
25826 || TREE_CODE (expression) == TRAIT_EXPR
25827 || TREE_CODE (expression) == TYPEID_EXPR
25828 || TREE_CODE (expression) == DELETE_EXPR
25829 || TREE_CODE (expression) == VEC_DELETE_EXPR
25830 || TREE_CODE (expression) == THROW_EXPR
25831 || TREE_CODE (expression) == REQUIRES_EXPR)
25832 return false;
25833
25834 /* The types of these expressions depends only on the type to which
25835 the cast occurs. */
25836 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
25837 || TREE_CODE (expression) == STATIC_CAST_EXPR
25838 || TREE_CODE (expression) == CONST_CAST_EXPR
25839 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
25840 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
25841 || TREE_CODE (expression) == CAST_EXPR)
25842 return dependent_type_p (TREE_TYPE (expression));
25843
25844 /* The types of these expressions depends only on the type created
25845 by the expression. */
25846 if (TREE_CODE (expression) == NEW_EXPR
25847 || TREE_CODE (expression) == VEC_NEW_EXPR)
25848 {
25849 /* For NEW_EXPR tree nodes created inside a template, either
25850 the object type itself or a TREE_LIST may appear as the
25851 operand 1. */
25852 tree type = TREE_OPERAND (expression, 1);
25853 if (TREE_CODE (type) == TREE_LIST)
25854 /* This is an array type. We need to check array dimensions
25855 as well. */
25856 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
25857 || value_dependent_expression_p
25858 (TREE_OPERAND (TREE_VALUE (type), 1));
25859 else
25860 return dependent_type_p (type);
25861 }
25862
25863 if (TREE_CODE (expression) == SCOPE_REF)
25864 {
25865 tree scope = TREE_OPERAND (expression, 0);
25866 tree name = TREE_OPERAND (expression, 1);
25867
25868 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
25869 contains an identifier associated by name lookup with one or more
25870 declarations declared with a dependent type, or...a
25871 nested-name-specifier or qualified-id that names a member of an
25872 unknown specialization. */
25873 return (type_dependent_expression_p (name)
25874 || dependent_scope_p (scope));
25875 }
25876
25877 if (TREE_CODE (expression) == TEMPLATE_DECL
25878 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
25879 return uses_outer_template_parms (expression);
25880
25881 if (TREE_CODE (expression) == STMT_EXPR)
25882 expression = stmt_expr_value_expr (expression);
25883
25884 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
25885 {
25886 tree elt;
25887 unsigned i;
25888
25889 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
25890 {
25891 if (type_dependent_expression_p (elt))
25892 return true;
25893 }
25894 return false;
25895 }
25896
25897 /* A static data member of the current instantiation with incomplete
25898 array type is type-dependent, as the definition and specializations
25899 can have different bounds. */
25900 if (VAR_P (expression)
25901 && DECL_CLASS_SCOPE_P (expression)
25902 && dependent_type_p (DECL_CONTEXT (expression))
25903 && VAR_HAD_UNKNOWN_BOUND (expression))
25904 return true;
25905
25906 /* An array of unknown bound depending on a variadic parameter, eg:
25907
25908 template<typename... Args>
25909 void foo (Args... args)
25910 {
25911 int arr[] = { args... };
25912 }
25913
25914 template<int... vals>
25915 void bar ()
25916 {
25917 int arr[] = { vals... };
25918 }
25919
25920 If the array has no length and has an initializer, it must be that
25921 we couldn't determine its length in cp_complete_array_type because
25922 it is dependent. */
25923 if (VAR_P (expression)
25924 && TREE_TYPE (expression) != NULL_TREE
25925 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
25926 && !TYPE_DOMAIN (TREE_TYPE (expression))
25927 && DECL_INITIAL (expression))
25928 return true;
25929
25930 /* A function or variable template-id is type-dependent if it has any
25931 dependent template arguments. */
25932 if (VAR_OR_FUNCTION_DECL_P (expression)
25933 && DECL_LANG_SPECIFIC (expression)
25934 && DECL_TEMPLATE_INFO (expression))
25935 {
25936 /* Consider the innermost template arguments, since those are the ones
25937 that come from the template-id; the template arguments for the
25938 enclosing class do not make it type-dependent unless they are used in
25939 the type of the decl. */
25940 if (instantiates_primary_template_p (expression)
25941 && (any_dependent_template_arguments_p
25942 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
25943 return true;
25944 }
25945
25946 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
25947 type-dependent. Checking this is important for functions with auto return
25948 type, which looks like a dependent type. */
25949 if (TREE_CODE (expression) == FUNCTION_DECL
25950 && !(DECL_CLASS_SCOPE_P (expression)
25951 && dependent_type_p (DECL_CONTEXT (expression)))
25952 && !(DECL_LANG_SPECIFIC (expression)
25953 && DECL_FRIEND_P (expression)
25954 && (!DECL_FRIEND_CONTEXT (expression)
25955 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
25956 && !DECL_LOCAL_FUNCTION_P (expression))
25957 {
25958 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
25959 || undeduced_auto_decl (expression));
25960 return false;
25961 }
25962
25963 /* Always dependent, on the number of arguments if nothing else. */
25964 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
25965 return true;
25966
25967 if (TREE_TYPE (expression) == unknown_type_node)
25968 {
25969 if (TREE_CODE (expression) == ADDR_EXPR)
25970 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
25971 if (TREE_CODE (expression) == COMPONENT_REF
25972 || TREE_CODE (expression) == OFFSET_REF)
25973 {
25974 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
25975 return true;
25976 expression = TREE_OPERAND (expression, 1);
25977 if (identifier_p (expression))
25978 return false;
25979 }
25980 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
25981 if (TREE_CODE (expression) == SCOPE_REF)
25982 return false;
25983
25984 if (BASELINK_P (expression))
25985 {
25986 if (BASELINK_OPTYPE (expression)
25987 && dependent_type_p (BASELINK_OPTYPE (expression)))
25988 return true;
25989 expression = BASELINK_FUNCTIONS (expression);
25990 }
25991
25992 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
25993 {
25994 if (any_dependent_template_arguments_p
25995 (TREE_OPERAND (expression, 1)))
25996 return true;
25997 expression = TREE_OPERAND (expression, 0);
25998 if (identifier_p (expression))
25999 return true;
26000 }
26001
26002 gcc_assert (OVL_P (expression));
26003
26004 for (lkp_iterator iter (expression); iter; ++iter)
26005 if (type_dependent_expression_p (*iter))
26006 return true;
26007
26008 return false;
26009 }
26010
26011 /* The type of a non-type template parm declared with a placeholder type
26012 depends on the corresponding template argument, even though
26013 placeholders are not normally considered dependent. */
26014 if (TREE_CODE (expression) == TEMPLATE_PARM_INDEX
26015 && is_auto (TREE_TYPE (expression)))
26016 return true;
26017
26018 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
26019
26020 /* Dependent type attributes might not have made it from the decl to
26021 the type yet. */
26022 if (DECL_P (expression)
26023 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
26024 return true;
26025
26026 return (dependent_type_p (TREE_TYPE (expression)));
26027 }
26028
26029 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
26030 type-dependent if the expression refers to a member of the current
26031 instantiation and the type of the referenced member is dependent, or the
26032 class member access expression refers to a member of an unknown
26033 specialization.
26034
26035 This function returns true if the OBJECT in such a class member access
26036 expression is of an unknown specialization. */
26037
26038 bool
26039 type_dependent_object_expression_p (tree object)
26040 {
26041 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
26042 dependent. */
26043 if (TREE_CODE (object) == IDENTIFIER_NODE)
26044 return true;
26045 tree scope = TREE_TYPE (object);
26046 return (!scope || dependent_scope_p (scope));
26047 }
26048
26049 /* walk_tree callback function for instantiation_dependent_expression_p,
26050 below. Returns non-zero if a dependent subexpression is found. */
26051
26052 static tree
26053 instantiation_dependent_r (tree *tp, int *walk_subtrees,
26054 void * /*data*/)
26055 {
26056 if (TYPE_P (*tp))
26057 {
26058 /* We don't have to worry about decltype currently because decltype
26059 of an instantiation-dependent expr is a dependent type. This
26060 might change depending on the resolution of DR 1172. */
26061 *walk_subtrees = false;
26062 return NULL_TREE;
26063 }
26064 enum tree_code code = TREE_CODE (*tp);
26065 switch (code)
26066 {
26067 /* Don't treat an argument list as dependent just because it has no
26068 TREE_TYPE. */
26069 case TREE_LIST:
26070 case TREE_VEC:
26071 case NONTYPE_ARGUMENT_PACK:
26072 return NULL_TREE;
26073
26074 case TEMPLATE_PARM_INDEX:
26075 if (dependent_type_p (TREE_TYPE (*tp)))
26076 return *tp;
26077 if (TEMPLATE_PARM_PARAMETER_PACK (*tp))
26078 return *tp;
26079 /* We'll check value-dependence separately. */
26080 return NULL_TREE;
26081
26082 /* Handle expressions with type operands. */
26083 case SIZEOF_EXPR:
26084 case ALIGNOF_EXPR:
26085 case TYPEID_EXPR:
26086 case AT_ENCODE_EXPR:
26087 {
26088 tree op = TREE_OPERAND (*tp, 0);
26089 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
26090 op = TREE_TYPE (op);
26091 if (TYPE_P (op))
26092 {
26093 if (dependent_type_p (op))
26094 return *tp;
26095 else
26096 {
26097 *walk_subtrees = false;
26098 return NULL_TREE;
26099 }
26100 }
26101 break;
26102 }
26103
26104 case COMPONENT_REF:
26105 if (identifier_p (TREE_OPERAND (*tp, 1)))
26106 /* In a template, finish_class_member_access_expr creates a
26107 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
26108 type-dependent, so that we can check access control at
26109 instantiation time (PR 42277). See also Core issue 1273. */
26110 return *tp;
26111 break;
26112
26113 case SCOPE_REF:
26114 if (instantiation_dependent_scope_ref_p (*tp))
26115 return *tp;
26116 else
26117 break;
26118
26119 /* Treat statement-expressions as dependent. */
26120 case BIND_EXPR:
26121 return *tp;
26122
26123 /* Treat requires-expressions as dependent. */
26124 case REQUIRES_EXPR:
26125 return *tp;
26126
26127 case CALL_EXPR:
26128 /* Treat calls to function concepts as dependent. */
26129 if (function_concept_check_p (*tp))
26130 return *tp;
26131 break;
26132
26133 case TEMPLATE_ID_EXPR:
26134 /* And variable concepts. */
26135 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
26136 return *tp;
26137 break;
26138
26139 case CONSTRUCTOR:
26140 if (CONSTRUCTOR_IS_DEPENDENT (*tp))
26141 return *tp;
26142 break;
26143
26144 default:
26145 break;
26146 }
26147
26148 if (type_dependent_expression_p (*tp))
26149 return *tp;
26150 else
26151 return NULL_TREE;
26152 }
26153
26154 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
26155 sense defined by the ABI:
26156
26157 "An expression is instantiation-dependent if it is type-dependent
26158 or value-dependent, or it has a subexpression that is type-dependent
26159 or value-dependent."
26160
26161 Except don't actually check value-dependence for unevaluated expressions,
26162 because in sizeof(i) we don't care about the value of i. Checking
26163 type-dependence will in turn check value-dependence of array bounds/template
26164 arguments as needed. */
26165
26166 bool
26167 instantiation_dependent_uneval_expression_p (tree expression)
26168 {
26169 tree result;
26170
26171 if (!processing_template_decl)
26172 return false;
26173
26174 if (expression == error_mark_node)
26175 return false;
26176
26177 result = cp_walk_tree_without_duplicates (&expression,
26178 instantiation_dependent_r, NULL);
26179 return result != NULL_TREE;
26180 }
26181
26182 /* As above, but also check value-dependence of the expression as a whole. */
26183
26184 bool
26185 instantiation_dependent_expression_p (tree expression)
26186 {
26187 return (instantiation_dependent_uneval_expression_p (expression)
26188 || value_dependent_expression_p (expression));
26189 }
26190
26191 /* Like type_dependent_expression_p, but it also works while not processing
26192 a template definition, i.e. during substitution or mangling. */
26193
26194 bool
26195 type_dependent_expression_p_push (tree expr)
26196 {
26197 bool b;
26198 ++processing_template_decl;
26199 b = type_dependent_expression_p (expr);
26200 --processing_template_decl;
26201 return b;
26202 }
26203
26204 /* Returns TRUE if ARGS contains a type-dependent expression. */
26205
26206 bool
26207 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
26208 {
26209 unsigned int i;
26210 tree arg;
26211
26212 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
26213 {
26214 if (type_dependent_expression_p (arg))
26215 return true;
26216 }
26217 return false;
26218 }
26219
26220 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
26221 expressions) contains any type-dependent expressions. */
26222
26223 bool
26224 any_type_dependent_elements_p (const_tree list)
26225 {
26226 for (; list; list = TREE_CHAIN (list))
26227 if (type_dependent_expression_p (TREE_VALUE (list)))
26228 return true;
26229
26230 return false;
26231 }
26232
26233 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
26234 expressions) contains any value-dependent expressions. */
26235
26236 bool
26237 any_value_dependent_elements_p (const_tree list)
26238 {
26239 for (; list; list = TREE_CHAIN (list))
26240 if (value_dependent_expression_p (TREE_VALUE (list)))
26241 return true;
26242
26243 return false;
26244 }
26245
26246 /* Returns TRUE if the ARG (a template argument) is dependent. */
26247
26248 bool
26249 dependent_template_arg_p (tree arg)
26250 {
26251 if (!processing_template_decl)
26252 return false;
26253
26254 /* Assume a template argument that was wrongly written by the user
26255 is dependent. This is consistent with what
26256 any_dependent_template_arguments_p [that calls this function]
26257 does. */
26258 if (!arg || arg == error_mark_node)
26259 return true;
26260
26261 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
26262 arg = argument_pack_select_arg (arg);
26263
26264 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
26265 return true;
26266 if (TREE_CODE (arg) == TEMPLATE_DECL)
26267 {
26268 if (DECL_TEMPLATE_PARM_P (arg))
26269 return true;
26270 /* A member template of a dependent class is not necessarily
26271 type-dependent, but it is a dependent template argument because it
26272 will be a member of an unknown specialization to that template. */
26273 tree scope = CP_DECL_CONTEXT (arg);
26274 return TYPE_P (scope) && dependent_type_p (scope);
26275 }
26276 else if (ARGUMENT_PACK_P (arg))
26277 {
26278 tree args = ARGUMENT_PACK_ARGS (arg);
26279 int i, len = TREE_VEC_LENGTH (args);
26280 for (i = 0; i < len; ++i)
26281 {
26282 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
26283 return true;
26284 }
26285
26286 return false;
26287 }
26288 else if (TYPE_P (arg))
26289 return dependent_type_p (arg);
26290 else
26291 return (type_dependent_expression_p (arg)
26292 || value_dependent_expression_p (arg));
26293 }
26294
26295 /* Returns true if ARGS (a collection of template arguments) contains
26296 any types that require structural equality testing. */
26297
26298 bool
26299 any_template_arguments_need_structural_equality_p (tree args)
26300 {
26301 int i;
26302 int j;
26303
26304 if (!args)
26305 return false;
26306 if (args == error_mark_node)
26307 return true;
26308
26309 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
26310 {
26311 tree level = TMPL_ARGS_LEVEL (args, i + 1);
26312 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
26313 {
26314 tree arg = TREE_VEC_ELT (level, j);
26315 tree packed_args = NULL_TREE;
26316 int k, len = 1;
26317
26318 if (ARGUMENT_PACK_P (arg))
26319 {
26320 /* Look inside the argument pack. */
26321 packed_args = ARGUMENT_PACK_ARGS (arg);
26322 len = TREE_VEC_LENGTH (packed_args);
26323 }
26324
26325 for (k = 0; k < len; ++k)
26326 {
26327 if (packed_args)
26328 arg = TREE_VEC_ELT (packed_args, k);
26329
26330 if (error_operand_p (arg))
26331 return true;
26332 else if (TREE_CODE (arg) == TEMPLATE_DECL)
26333 continue;
26334 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
26335 return true;
26336 else if (!TYPE_P (arg) && TREE_TYPE (arg)
26337 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
26338 return true;
26339 }
26340 }
26341 }
26342
26343 return false;
26344 }
26345
26346 /* Returns true if ARGS (a collection of template arguments) contains
26347 any dependent arguments. */
26348
26349 bool
26350 any_dependent_template_arguments_p (const_tree args)
26351 {
26352 int i;
26353 int j;
26354
26355 if (!args)
26356 return false;
26357 if (args == error_mark_node)
26358 return true;
26359
26360 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
26361 {
26362 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
26363 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
26364 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
26365 return true;
26366 }
26367
26368 return false;
26369 }
26370
26371 /* Returns true if ARGS contains any errors. */
26372
26373 bool
26374 any_erroneous_template_args_p (const_tree args)
26375 {
26376 int i;
26377 int j;
26378
26379 if (args == error_mark_node)
26380 return true;
26381
26382 if (args && TREE_CODE (args) != TREE_VEC)
26383 {
26384 if (tree ti = get_template_info (args))
26385 args = TI_ARGS (ti);
26386 else
26387 args = NULL_TREE;
26388 }
26389
26390 if (!args)
26391 return false;
26392
26393 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
26394 {
26395 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
26396 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
26397 if (error_operand_p (TREE_VEC_ELT (level, j)))
26398 return true;
26399 }
26400
26401 return false;
26402 }
26403
26404 /* Returns TRUE if the template TMPL is type-dependent. */
26405
26406 bool
26407 dependent_template_p (tree tmpl)
26408 {
26409 if (TREE_CODE (tmpl) == OVERLOAD)
26410 {
26411 for (lkp_iterator iter (tmpl); iter; ++iter)
26412 if (dependent_template_p (*iter))
26413 return true;
26414 return false;
26415 }
26416
26417 /* Template template parameters are dependent. */
26418 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
26419 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
26420 return true;
26421 /* So are names that have not been looked up. */
26422 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
26423 return true;
26424 return false;
26425 }
26426
26427 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
26428
26429 bool
26430 dependent_template_id_p (tree tmpl, tree args)
26431 {
26432 return (dependent_template_p (tmpl)
26433 || any_dependent_template_arguments_p (args));
26434 }
26435
26436 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
26437 are dependent. */
26438
26439 bool
26440 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
26441 {
26442 int i;
26443
26444 if (!processing_template_decl)
26445 return false;
26446
26447 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
26448 {
26449 tree decl = TREE_VEC_ELT (declv, i);
26450 tree init = TREE_VEC_ELT (initv, i);
26451 tree cond = TREE_VEC_ELT (condv, i);
26452 tree incr = TREE_VEC_ELT (incrv, i);
26453
26454 if (type_dependent_expression_p (decl)
26455 || TREE_CODE (decl) == SCOPE_REF)
26456 return true;
26457
26458 if (init && type_dependent_expression_p (init))
26459 return true;
26460
26461 if (cond == global_namespace)
26462 return true;
26463
26464 if (type_dependent_expression_p (cond))
26465 return true;
26466
26467 if (COMPARISON_CLASS_P (cond)
26468 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
26469 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
26470 return true;
26471
26472 if (TREE_CODE (incr) == MODOP_EXPR)
26473 {
26474 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
26475 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
26476 return true;
26477 }
26478 else if (type_dependent_expression_p (incr))
26479 return true;
26480 else if (TREE_CODE (incr) == MODIFY_EXPR)
26481 {
26482 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
26483 return true;
26484 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
26485 {
26486 tree t = TREE_OPERAND (incr, 1);
26487 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
26488 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
26489 return true;
26490
26491 /* If this loop has a class iterator with != comparison
26492 with increment other than i++/++i/i--/--i, make sure the
26493 increment is constant. */
26494 if (CLASS_TYPE_P (TREE_TYPE (decl))
26495 && TREE_CODE (cond) == NE_EXPR)
26496 {
26497 if (TREE_OPERAND (t, 0) == decl)
26498 t = TREE_OPERAND (t, 1);
26499 else
26500 t = TREE_OPERAND (t, 0);
26501 if (TREE_CODE (t) != INTEGER_CST)
26502 return true;
26503 }
26504 }
26505 }
26506 }
26507
26508 return false;
26509 }
26510
26511 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
26512 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
26513 no such TYPE can be found. Note that this function peers inside
26514 uninstantiated templates and therefore should be used only in
26515 extremely limited situations. ONLY_CURRENT_P restricts this
26516 peering to the currently open classes hierarchy (which is required
26517 when comparing types). */
26518
26519 tree
26520 resolve_typename_type (tree type, bool only_current_p)
26521 {
26522 tree scope;
26523 tree name;
26524 tree decl;
26525 int quals;
26526 tree pushed_scope;
26527 tree result;
26528
26529 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
26530
26531 scope = TYPE_CONTEXT (type);
26532 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
26533 gcc_checking_assert (uses_template_parms (scope));
26534
26535 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
26536 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
26537 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
26538 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
26539 identifier of the TYPENAME_TYPE anymore.
26540 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
26541 TYPENAME_TYPE instead, we avoid messing up with a possible
26542 typedef variant case. */
26543 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
26544
26545 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
26546 it first before we can figure out what NAME refers to. */
26547 if (TREE_CODE (scope) == TYPENAME_TYPE)
26548 {
26549 if (TYPENAME_IS_RESOLVING_P (scope))
26550 /* Given a class template A with a dependent base with nested type C,
26551 typedef typename A::C::C C will land us here, as trying to resolve
26552 the initial A::C leads to the local C typedef, which leads back to
26553 A::C::C. So we break the recursion now. */
26554 return type;
26555 else
26556 scope = resolve_typename_type (scope, only_current_p);
26557 }
26558 /* If we don't know what SCOPE refers to, then we cannot resolve the
26559 TYPENAME_TYPE. */
26560 if (!CLASS_TYPE_P (scope))
26561 return type;
26562 /* If this is a typedef, we don't want to look inside (c++/11987). */
26563 if (typedef_variant_p (type))
26564 return type;
26565 /* If SCOPE isn't the template itself, it will not have a valid
26566 TYPE_FIELDS list. */
26567 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
26568 /* scope is either the template itself or a compatible instantiation
26569 like X<T>, so look up the name in the original template. */
26570 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
26571 /* If scope has no fields, it can't be a current instantiation. Check this
26572 before currently_open_class to avoid infinite recursion (71515). */
26573 if (!TYPE_FIELDS (scope))
26574 return type;
26575 /* If the SCOPE is not the current instantiation, there's no reason
26576 to look inside it. */
26577 if (only_current_p && !currently_open_class (scope))
26578 return type;
26579 /* Enter the SCOPE so that name lookup will be resolved as if we
26580 were in the class definition. In particular, SCOPE will no
26581 longer be considered a dependent type. */
26582 pushed_scope = push_scope (scope);
26583 /* Look up the declaration. */
26584 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
26585 tf_warning_or_error);
26586
26587 result = NULL_TREE;
26588
26589 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
26590 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
26591 tree fullname = TYPENAME_TYPE_FULLNAME (type);
26592 if (!decl)
26593 /*nop*/;
26594 else if (identifier_p (fullname)
26595 && TREE_CODE (decl) == TYPE_DECL)
26596 {
26597 result = TREE_TYPE (decl);
26598 if (result == error_mark_node)
26599 result = NULL_TREE;
26600 }
26601 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
26602 && DECL_CLASS_TEMPLATE_P (decl))
26603 {
26604 /* Obtain the template and the arguments. */
26605 tree tmpl = TREE_OPERAND (fullname, 0);
26606 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
26607 {
26608 /* We get here with a plain identifier because a previous tentative
26609 parse of the nested-name-specifier as part of a ptr-operator saw
26610 ::template X<A>. The use of ::template is necessary in a
26611 ptr-operator, but wrong in a declarator-id.
26612
26613 [temp.names]: In a qualified-id of a declarator-id, the keyword
26614 template shall not appear at the top level. */
26615 pedwarn (cp_expr_loc_or_input_loc (fullname), OPT_Wpedantic,
26616 "keyword %<template%> not allowed in declarator-id");
26617 tmpl = decl;
26618 }
26619 tree args = TREE_OPERAND (fullname, 1);
26620 /* Instantiate the template. */
26621 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
26622 /*entering_scope=*/true,
26623 tf_error | tf_user);
26624 if (result == error_mark_node)
26625 result = NULL_TREE;
26626 }
26627
26628 /* Leave the SCOPE. */
26629 if (pushed_scope)
26630 pop_scope (pushed_scope);
26631
26632 /* If we failed to resolve it, return the original typename. */
26633 if (!result)
26634 return type;
26635
26636 /* If lookup found a typename type, resolve that too. */
26637 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
26638 {
26639 /* Ill-formed programs can cause infinite recursion here, so we
26640 must catch that. */
26641 TYPENAME_IS_RESOLVING_P (result) = 1;
26642 result = resolve_typename_type (result, only_current_p);
26643 TYPENAME_IS_RESOLVING_P (result) = 0;
26644 }
26645
26646 /* Qualify the resulting type. */
26647 quals = cp_type_quals (type);
26648 if (quals)
26649 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
26650
26651 return result;
26652 }
26653
26654 /* EXPR is an expression which is not type-dependent. Return a proxy
26655 for EXPR that can be used to compute the types of larger
26656 expressions containing EXPR. */
26657
26658 tree
26659 build_non_dependent_expr (tree expr)
26660 {
26661 tree orig_expr = expr;
26662 tree inner_expr;
26663
26664 /* When checking, try to get a constant value for all non-dependent
26665 expressions in order to expose bugs in *_dependent_expression_p
26666 and constexpr. This can affect code generation, see PR70704, so
26667 only do this for -fchecking=2. */
26668 if (flag_checking > 1
26669 && cxx_dialect >= cxx11
26670 /* Don't do this during nsdmi parsing as it can lead to
26671 unexpected recursive instantiations. */
26672 && !parsing_nsdmi ()
26673 /* Don't do this during concept expansion either and for
26674 the same reason. */
26675 && !expanding_concept ())
26676 fold_non_dependent_expr (expr, tf_none);
26677
26678 STRIP_ANY_LOCATION_WRAPPER (expr);
26679
26680 /* Preserve OVERLOADs; the functions must be available to resolve
26681 types. */
26682 inner_expr = expr;
26683 if (TREE_CODE (inner_expr) == STMT_EXPR)
26684 inner_expr = stmt_expr_value_expr (inner_expr);
26685 if (TREE_CODE (inner_expr) == ADDR_EXPR)
26686 inner_expr = TREE_OPERAND (inner_expr, 0);
26687 if (TREE_CODE (inner_expr) == COMPONENT_REF)
26688 inner_expr = TREE_OPERAND (inner_expr, 1);
26689 if (is_overloaded_fn (inner_expr)
26690 || TREE_CODE (inner_expr) == OFFSET_REF)
26691 return orig_expr;
26692 /* There is no need to return a proxy for a variable or enumerator. */
26693 if (VAR_P (expr) || TREE_CODE (expr) == CONST_DECL)
26694 return orig_expr;
26695 /* Preserve string constants; conversions from string constants to
26696 "char *" are allowed, even though normally a "const char *"
26697 cannot be used to initialize a "char *". */
26698 if (TREE_CODE (expr) == STRING_CST)
26699 return orig_expr;
26700 /* Preserve void and arithmetic constants, as an optimization -- there is no
26701 reason to create a new node. */
26702 if (TREE_CODE (expr) == VOID_CST
26703 || TREE_CODE (expr) == INTEGER_CST
26704 || TREE_CODE (expr) == REAL_CST)
26705 return orig_expr;
26706 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
26707 There is at least one place where we want to know that a
26708 particular expression is a throw-expression: when checking a ?:
26709 expression, there are special rules if the second or third
26710 argument is a throw-expression. */
26711 if (TREE_CODE (expr) == THROW_EXPR)
26712 return orig_expr;
26713
26714 /* Don't wrap an initializer list, we need to be able to look inside. */
26715 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
26716 return orig_expr;
26717
26718 /* Don't wrap a dummy object, we need to be able to test for it. */
26719 if (is_dummy_object (expr))
26720 return orig_expr;
26721
26722 if (TREE_CODE (expr) == COND_EXPR)
26723 return build3 (COND_EXPR,
26724 TREE_TYPE (expr),
26725 build_non_dependent_expr (TREE_OPERAND (expr, 0)),
26726 (TREE_OPERAND (expr, 1)
26727 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
26728 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
26729 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
26730 if (TREE_CODE (expr) == COMPOUND_EXPR
26731 && !COMPOUND_EXPR_OVERLOADED (expr))
26732 return build2 (COMPOUND_EXPR,
26733 TREE_TYPE (expr),
26734 TREE_OPERAND (expr, 0),
26735 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
26736
26737 /* If the type is unknown, it can't really be non-dependent */
26738 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
26739
26740 /* Otherwise, build a NON_DEPENDENT_EXPR. */
26741 return build1_loc (EXPR_LOCATION (orig_expr), NON_DEPENDENT_EXPR,
26742 TREE_TYPE (expr), expr);
26743 }
26744
26745 /* ARGS is a vector of expressions as arguments to a function call.
26746 Replace the arguments with equivalent non-dependent expressions.
26747 This modifies ARGS in place. */
26748
26749 void
26750 make_args_non_dependent (vec<tree, va_gc> *args)
26751 {
26752 unsigned int ix;
26753 tree arg;
26754
26755 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
26756 {
26757 tree newarg = build_non_dependent_expr (arg);
26758 if (newarg != arg)
26759 (*args)[ix] = newarg;
26760 }
26761 }
26762
26763 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
26764 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
26765 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
26766
26767 static tree
26768 make_auto_1 (tree name, bool set_canonical)
26769 {
26770 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
26771 TYPE_NAME (au) = build_decl (input_location,
26772 TYPE_DECL, name, au);
26773 TYPE_STUB_DECL (au) = TYPE_NAME (au);
26774 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
26775 (0, processing_template_decl + 1, processing_template_decl + 1,
26776 TYPE_NAME (au), NULL_TREE);
26777 if (set_canonical)
26778 TYPE_CANONICAL (au) = canonical_type_parameter (au);
26779 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
26780 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
26781
26782 return au;
26783 }
26784
26785 tree
26786 make_decltype_auto (void)
26787 {
26788 return make_auto_1 (decltype_auto_identifier, true);
26789 }
26790
26791 tree
26792 make_auto (void)
26793 {
26794 return make_auto_1 (auto_identifier, true);
26795 }
26796
26797 /* Return a C++17 deduction placeholder for class template TMPL. */
26798
26799 tree
26800 make_template_placeholder (tree tmpl)
26801 {
26802 tree t = make_auto_1 (auto_identifier, false);
26803 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
26804 /* Our canonical type depends on the placeholder. */
26805 TYPE_CANONICAL (t) = canonical_type_parameter (t);
26806 return t;
26807 }
26808
26809 /* True iff T is a C++17 class template deduction placeholder. */
26810
26811 bool
26812 template_placeholder_p (tree t)
26813 {
26814 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
26815 }
26816
26817 /* Make a "constrained auto" type-specifier. This is an
26818 auto type with constraints that must be associated after
26819 deduction. The constraint is formed from the given
26820 CONC and its optional sequence of arguments, which are
26821 non-null if written as partial-concept-id. */
26822
26823 tree
26824 make_constrained_auto (tree con, tree args)
26825 {
26826 tree type = make_auto_1 (auto_identifier, false);
26827
26828 /* Build the constraint. */
26829 tree tmpl = DECL_TI_TEMPLATE (con);
26830 tree expr = VAR_P (con) ? tmpl : ovl_make (tmpl);
26831 expr = build_concept_check (expr, type, args);
26832
26833 tree constr = normalize_expression (expr);
26834 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
26835
26836 /* Our canonical type depends on the constraint. */
26837 TYPE_CANONICAL (type) = canonical_type_parameter (type);
26838
26839 /* Attach the constraint to the type declaration. */
26840 tree decl = TYPE_NAME (type);
26841 return decl;
26842 }
26843
26844 /* Given type ARG, return std::initializer_list<ARG>. */
26845
26846 static tree
26847 listify (tree arg)
26848 {
26849 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
26850
26851 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
26852 {
26853 gcc_rich_location richloc (input_location);
26854 maybe_add_include_fixit (&richloc, "<initializer_list>", false);
26855 error_at (&richloc,
26856 "deducing from brace-enclosed initializer list"
26857 " requires %<#include <initializer_list>%>");
26858
26859 return error_mark_node;
26860 }
26861 tree argvec = make_tree_vec (1);
26862 TREE_VEC_ELT (argvec, 0) = arg;
26863
26864 return lookup_template_class (std_init_list, argvec, NULL_TREE,
26865 NULL_TREE, 0, tf_warning_or_error);
26866 }
26867
26868 /* Replace auto in TYPE with std::initializer_list<auto>. */
26869
26870 static tree
26871 listify_autos (tree type, tree auto_node)
26872 {
26873 tree init_auto = listify (strip_top_quals (auto_node));
26874 tree argvec = make_tree_vec (1);
26875 TREE_VEC_ELT (argvec, 0) = init_auto;
26876 if (processing_template_decl)
26877 argvec = add_to_template_args (current_template_args (), argvec);
26878 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
26879 }
26880
26881 /* Hash traits for hashing possibly constrained 'auto'
26882 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
26883
26884 struct auto_hash : default_hash_traits<tree>
26885 {
26886 static inline hashval_t hash (tree);
26887 static inline bool equal (tree, tree);
26888 };
26889
26890 /* Hash the 'auto' T. */
26891
26892 inline hashval_t
26893 auto_hash::hash (tree t)
26894 {
26895 if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t)))
26896 /* Matching constrained-type-specifiers denote the same template
26897 parameter, so hash the constraint. */
26898 return hash_placeholder_constraint (c);
26899 else
26900 /* But unconstrained autos are all separate, so just hash the pointer. */
26901 return iterative_hash_object (t, 0);
26902 }
26903
26904 /* Compare two 'auto's. */
26905
26906 inline bool
26907 auto_hash::equal (tree t1, tree t2)
26908 {
26909 if (t1 == t2)
26910 return true;
26911
26912 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
26913 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
26914
26915 /* Two unconstrained autos are distinct. */
26916 if (!c1 || !c2)
26917 return false;
26918
26919 return equivalent_placeholder_constraints (c1, c2);
26920 }
26921
26922 /* for_each_template_parm callback for extract_autos: if t is a (possibly
26923 constrained) auto, add it to the vector. */
26924
26925 static int
26926 extract_autos_r (tree t, void *data)
26927 {
26928 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
26929 if (is_auto (t))
26930 {
26931 /* All the autos were built with index 0; fix that up now. */
26932 tree *p = hash.find_slot (t, INSERT);
26933 unsigned idx;
26934 if (*p)
26935 /* If this is a repeated constrained-type-specifier, use the index we
26936 chose before. */
26937 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
26938 else
26939 {
26940 /* Otherwise this is new, so use the current count. */
26941 *p = t;
26942 idx = hash.elements () - 1;
26943 }
26944 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
26945 }
26946
26947 /* Always keep walking. */
26948 return 0;
26949 }
26950
26951 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
26952 says they can appear anywhere in the type. */
26953
26954 static tree
26955 extract_autos (tree type)
26956 {
26957 hash_set<tree> visited;
26958 hash_table<auto_hash> hash (2);
26959
26960 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
26961
26962 tree tree_vec = make_tree_vec (hash.elements());
26963 for (hash_table<auto_hash>::iterator iter = hash.begin();
26964 iter != hash.end(); ++iter)
26965 {
26966 tree elt = *iter;
26967 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
26968 TREE_VEC_ELT (tree_vec, i)
26969 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
26970 }
26971
26972 return tree_vec;
26973 }
26974
26975 /* The stem for deduction guide names. */
26976 const char *const dguide_base = "__dguide_";
26977
26978 /* Return the name for a deduction guide for class template TMPL. */
26979
26980 tree
26981 dguide_name (tree tmpl)
26982 {
26983 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
26984 tree tname = TYPE_IDENTIFIER (type);
26985 char *buf = (char *) alloca (1 + strlen (dguide_base)
26986 + IDENTIFIER_LENGTH (tname));
26987 memcpy (buf, dguide_base, strlen (dguide_base));
26988 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
26989 IDENTIFIER_LENGTH (tname) + 1);
26990 tree dname = get_identifier (buf);
26991 TREE_TYPE (dname) = type;
26992 return dname;
26993 }
26994
26995 /* True if NAME is the name of a deduction guide. */
26996
26997 bool
26998 dguide_name_p (tree name)
26999 {
27000 return (TREE_CODE (name) == IDENTIFIER_NODE
27001 && TREE_TYPE (name)
27002 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
27003 strlen (dguide_base)));
27004 }
27005
27006 /* True if FN is a deduction guide. */
27007
27008 bool
27009 deduction_guide_p (const_tree fn)
27010 {
27011 if (DECL_P (fn))
27012 if (tree name = DECL_NAME (fn))
27013 return dguide_name_p (name);
27014 return false;
27015 }
27016
27017 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
27018
27019 bool
27020 copy_guide_p (const_tree fn)
27021 {
27022 gcc_assert (deduction_guide_p (fn));
27023 if (!DECL_ARTIFICIAL (fn))
27024 return false;
27025 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
27026 return (TREE_CHAIN (parms) == void_list_node
27027 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
27028 }
27029
27030 /* True if FN is a guide generated from a constructor template. */
27031
27032 bool
27033 template_guide_p (const_tree fn)
27034 {
27035 gcc_assert (deduction_guide_p (fn));
27036 if (!DECL_ARTIFICIAL (fn))
27037 return false;
27038 tree tmpl = DECL_TI_TEMPLATE (fn);
27039 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
27040 return PRIMARY_TEMPLATE_P (org);
27041 return false;
27042 }
27043
27044 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
27045 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
27046 template parameter types. Note that the handling of template template
27047 parameters relies on current_template_parms being set appropriately for the
27048 new template. */
27049
27050 static tree
27051 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
27052 tree tsubst_args, tsubst_flags_t complain)
27053 {
27054 if (olddecl == error_mark_node)
27055 return error_mark_node;
27056
27057 tree oldidx = get_template_parm_index (olddecl);
27058
27059 tree newtype;
27060 if (TREE_CODE (olddecl) == TYPE_DECL
27061 || TREE_CODE (olddecl) == TEMPLATE_DECL)
27062 {
27063 tree oldtype = TREE_TYPE (olddecl);
27064 newtype = cxx_make_type (TREE_CODE (oldtype));
27065 TYPE_MAIN_VARIANT (newtype) = newtype;
27066 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
27067 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
27068 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
27069 }
27070 else
27071 {
27072 newtype = TREE_TYPE (olddecl);
27073 if (type_uses_auto (newtype))
27074 {
27075 // Substitute once to fix references to other template parameters.
27076 newtype = tsubst (newtype, tsubst_args,
27077 complain|tf_partial, NULL_TREE);
27078 // Now substitute again to reduce the level of the auto.
27079 newtype = tsubst (newtype, current_template_args (),
27080 complain, NULL_TREE);
27081 }
27082 else
27083 newtype = tsubst (newtype, tsubst_args,
27084 complain, NULL_TREE);
27085 }
27086
27087 tree newdecl
27088 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
27089 DECL_NAME (olddecl), newtype);
27090 SET_DECL_TEMPLATE_PARM_P (newdecl);
27091
27092 tree newidx;
27093 if (TREE_CODE (olddecl) == TYPE_DECL
27094 || TREE_CODE (olddecl) == TEMPLATE_DECL)
27095 {
27096 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
27097 = build_template_parm_index (index, level, level,
27098 newdecl, newtype);
27099 TEMPLATE_PARM_PARAMETER_PACK (newidx)
27100 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
27101 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
27102 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
27103
27104 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
27105 {
27106 DECL_TEMPLATE_RESULT (newdecl)
27107 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
27108 DECL_NAME (olddecl), newtype);
27109 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
27110 // First create a copy (ttargs) of tsubst_args with an
27111 // additional level for the template template parameter's own
27112 // template parameters (ttparms).
27113 tree ttparms = (INNERMOST_TEMPLATE_PARMS
27114 (DECL_TEMPLATE_PARMS (olddecl)));
27115 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
27116 tree ttargs = make_tree_vec (depth + 1);
27117 for (int i = 0; i < depth; ++i)
27118 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
27119 TREE_VEC_ELT (ttargs, depth)
27120 = template_parms_level_to_args (ttparms);
27121 // Substitute ttargs into ttparms to fix references to
27122 // other template parameters.
27123 ttparms = tsubst_template_parms_level (ttparms, ttargs,
27124 complain|tf_partial);
27125 // Now substitute again with args based on tparms, to reduce
27126 // the level of the ttparms.
27127 ttargs = current_template_args ();
27128 ttparms = tsubst_template_parms_level (ttparms, ttargs,
27129 complain);
27130 // Finally, tack the adjusted parms onto tparms.
27131 ttparms = tree_cons (size_int (depth), ttparms,
27132 current_template_parms);
27133 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
27134 }
27135 }
27136 else
27137 {
27138 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
27139 tree newconst
27140 = build_decl (DECL_SOURCE_LOCATION (oldconst),
27141 TREE_CODE (oldconst),
27142 DECL_NAME (oldconst), newtype);
27143 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
27144 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
27145 SET_DECL_TEMPLATE_PARM_P (newconst);
27146 newidx = build_template_parm_index (index, level, level,
27147 newconst, newtype);
27148 TEMPLATE_PARM_PARAMETER_PACK (newidx)
27149 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
27150 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
27151 }
27152
27153 return newdecl;
27154 }
27155
27156 /* Returns a C++17 class deduction guide template based on the constructor
27157 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
27158 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
27159
27160 static tree
27161 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
27162 {
27163 tree type, tparms, targs, fparms, fargs, ci;
27164 bool memtmpl = false;
27165 bool explicit_p;
27166 location_t loc;
27167 tree fn_tmpl = NULL_TREE;
27168
27169 if (TYPE_P (ctor))
27170 {
27171 type = ctor;
27172 bool copy_p = TYPE_REF_P (type);
27173 if (copy_p)
27174 {
27175 type = TREE_TYPE (type);
27176 fparms = tree_cons (NULL_TREE, type, void_list_node);
27177 }
27178 else
27179 fparms = void_list_node;
27180
27181 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
27182 tparms = DECL_TEMPLATE_PARMS (ctmpl);
27183 targs = CLASSTYPE_TI_ARGS (type);
27184 ci = NULL_TREE;
27185 fargs = NULL_TREE;
27186 loc = DECL_SOURCE_LOCATION (ctmpl);
27187 explicit_p = false;
27188 }
27189 else
27190 {
27191 ++processing_template_decl;
27192 bool ok = true;
27193
27194 fn_tmpl
27195 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
27196 : DECL_TI_TEMPLATE (ctor));
27197 if (outer_args)
27198 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
27199 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
27200
27201 type = DECL_CONTEXT (ctor);
27202
27203 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
27204 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
27205 fully specialized args for the enclosing class. Strip those off, as
27206 the deduction guide won't have those template parameters. */
27207 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
27208 TMPL_PARMS_DEPTH (tparms));
27209 /* Discard the 'this' parameter. */
27210 fparms = FUNCTION_ARG_CHAIN (ctor);
27211 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
27212 ci = get_constraints (ctor);
27213 loc = DECL_SOURCE_LOCATION (ctor);
27214 explicit_p = DECL_NONCONVERTING_P (ctor);
27215
27216 if (PRIMARY_TEMPLATE_P (fn_tmpl))
27217 {
27218 memtmpl = true;
27219
27220 /* For a member template constructor, we need to flatten the two
27221 template parameter lists into one, and then adjust the function
27222 signature accordingly. This gets...complicated. */
27223 tree save_parms = current_template_parms;
27224
27225 /* For a member template we should have two levels of parms/args, one
27226 for the class and one for the constructor. We stripped
27227 specialized args for further enclosing classes above. */
27228 const int depth = 2;
27229 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
27230
27231 /* Template args for translating references to the two-level template
27232 parameters into references to the one-level template parameters we
27233 are creating. */
27234 tree tsubst_args = copy_node (targs);
27235 TMPL_ARGS_LEVEL (tsubst_args, depth)
27236 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
27237
27238 /* Template parms for the constructor template. */
27239 tree ftparms = TREE_VALUE (tparms);
27240 unsigned flen = TREE_VEC_LENGTH (ftparms);
27241 /* Template parms for the class template. */
27242 tparms = TREE_CHAIN (tparms);
27243 tree ctparms = TREE_VALUE (tparms);
27244 unsigned clen = TREE_VEC_LENGTH (ctparms);
27245 /* Template parms for the deduction guide start as a copy of the
27246 template parms for the class. We set current_template_parms for
27247 lookup_template_class_1. */
27248 current_template_parms = tparms = copy_node (tparms);
27249 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
27250 for (unsigned i = 0; i < clen; ++i)
27251 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
27252
27253 /* Now we need to rewrite the constructor parms to append them to the
27254 class parms. */
27255 for (unsigned i = 0; i < flen; ++i)
27256 {
27257 unsigned index = i + clen;
27258 unsigned level = 1;
27259 tree oldelt = TREE_VEC_ELT (ftparms, i);
27260 tree olddecl = TREE_VALUE (oldelt);
27261 tree newdecl = rewrite_template_parm (olddecl, index, level,
27262 tsubst_args, complain);
27263 if (newdecl == error_mark_node)
27264 ok = false;
27265 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
27266 tsubst_args, complain, ctor);
27267 tree list = build_tree_list (newdef, newdecl);
27268 TEMPLATE_PARM_CONSTRAINTS (list)
27269 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
27270 tsubst_args, complain, ctor);
27271 TREE_VEC_ELT (new_vec, index) = list;
27272 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
27273 }
27274
27275 /* Now we have a final set of template parms to substitute into the
27276 function signature. */
27277 targs = template_parms_to_args (tparms);
27278 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
27279 complain, ctor);
27280 if (fparms == error_mark_node)
27281 ok = false;
27282 fargs = tsubst (fargs, tsubst_args, complain, ctor);
27283 if (ci)
27284 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
27285
27286 current_template_parms = save_parms;
27287 }
27288
27289 --processing_template_decl;
27290 if (!ok)
27291 return error_mark_node;
27292 }
27293
27294 if (!memtmpl)
27295 {
27296 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
27297 tparms = copy_node (tparms);
27298 INNERMOST_TEMPLATE_PARMS (tparms)
27299 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
27300 }
27301
27302 tree fntype = build_function_type (type, fparms);
27303 tree ded_fn = build_lang_decl_loc (loc,
27304 FUNCTION_DECL,
27305 dguide_name (type), fntype);
27306 DECL_ARGUMENTS (ded_fn) = fargs;
27307 DECL_ARTIFICIAL (ded_fn) = true;
27308 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
27309 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
27310 DECL_ARTIFICIAL (ded_tmpl) = true;
27311 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
27312 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
27313 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
27314 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
27315 if (DECL_P (ctor))
27316 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
27317 if (ci)
27318 set_constraints (ded_tmpl, ci);
27319
27320 return ded_tmpl;
27321 }
27322
27323 /* Deduce template arguments for the class template placeholder PTYPE for
27324 template TMPL based on the initializer INIT, and return the resulting
27325 type. */
27326
27327 static tree
27328 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
27329 tsubst_flags_t complain)
27330 {
27331 if (!DECL_CLASS_TEMPLATE_P (tmpl))
27332 {
27333 /* We should have handled this in the caller. */
27334 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
27335 return ptype;
27336 if (complain & tf_error)
27337 error ("non-class template %qT used without template arguments", tmpl);
27338 return error_mark_node;
27339 }
27340 if (init && TREE_TYPE (init) == ptype)
27341 /* Using the template parm as its own argument. */
27342 return ptype;
27343
27344 tree type = TREE_TYPE (tmpl);
27345
27346 bool try_list_ctor = false;
27347
27348 releasing_vec rv_args = NULL;
27349 vec<tree,va_gc> *&args = *&rv_args;
27350 if (init == NULL_TREE
27351 || TREE_CODE (init) == TREE_LIST)
27352 args = make_tree_vector_from_list (init);
27353 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
27354 {
27355 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
27356 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
27357 {
27358 /* As an exception, the first phase in 16.3.1.7 (considering the
27359 initializer list as a single argument) is omitted if the
27360 initializer list consists of a single expression of type cv U,
27361 where U is a specialization of C or a class derived from a
27362 specialization of C. */
27363 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
27364 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
27365 {
27366 tree etype = TREE_TYPE (elt);
27367 tree tparms = (INNERMOST_TEMPLATE_PARMS
27368 (DECL_TEMPLATE_PARMS (tmpl)));
27369 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
27370 int err = unify (tparms, targs, type, etype,
27371 UNIFY_ALLOW_DERIVED, /*explain*/false);
27372 if (err == 0)
27373 try_list_ctor = false;
27374 ggc_free (targs);
27375 }
27376 }
27377 if (try_list_ctor || is_std_init_list (type))
27378 args = make_tree_vector_single (init);
27379 else
27380 args = make_tree_vector_from_ctor (init);
27381 }
27382 else
27383 args = make_tree_vector_single (init);
27384
27385 tree dname = dguide_name (tmpl);
27386 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
27387 /*type*/false, /*complain*/false,
27388 /*hidden*/false);
27389 bool elided = false;
27390 if (cands == error_mark_node)
27391 cands = NULL_TREE;
27392
27393 /* Prune explicit deduction guides in copy-initialization context. */
27394 if (flags & LOOKUP_ONLYCONVERTING)
27395 {
27396 for (lkp_iterator iter (cands); !elided && iter; ++iter)
27397 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
27398 elided = true;
27399
27400 if (elided)
27401 {
27402 /* Found a nonconverting guide, prune the candidates. */
27403 tree pruned = NULL_TREE;
27404 for (lkp_iterator iter (cands); iter; ++iter)
27405 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
27406 pruned = lookup_add (*iter, pruned);
27407
27408 cands = pruned;
27409 }
27410 }
27411
27412 tree outer_args = NULL_TREE;
27413 if (DECL_CLASS_SCOPE_P (tmpl)
27414 && CLASSTYPE_TEMPLATE_INSTANTIATION (DECL_CONTEXT (tmpl)))
27415 {
27416 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
27417 type = TREE_TYPE (most_general_template (tmpl));
27418 }
27419
27420 bool saw_ctor = false;
27421 // FIXME cache artificial deduction guides
27422 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
27423 {
27424 /* Skip inherited constructors. */
27425 if (iter.using_p ())
27426 continue;
27427
27428 tree guide = build_deduction_guide (*iter, outer_args, complain);
27429 if (guide == error_mark_node)
27430 return error_mark_node;
27431 if ((flags & LOOKUP_ONLYCONVERTING)
27432 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
27433 elided = true;
27434 else
27435 cands = lookup_add (guide, cands);
27436
27437 saw_ctor = true;
27438 }
27439
27440 tree call = error_mark_node;
27441
27442 /* If this is list-initialization and the class has a list constructor, first
27443 try deducing from the list as a single argument, as [over.match.list]. */
27444 tree list_cands = NULL_TREE;
27445 if (try_list_ctor && cands)
27446 for (lkp_iterator iter (cands); iter; ++iter)
27447 {
27448 tree dg = *iter;
27449 if (is_list_ctor (dg))
27450 list_cands = lookup_add (dg, list_cands);
27451 }
27452 if (list_cands)
27453 {
27454 ++cp_unevaluated_operand;
27455 call = build_new_function_call (list_cands, &args, tf_decltype);
27456 --cp_unevaluated_operand;
27457
27458 if (call == error_mark_node)
27459 {
27460 /* That didn't work, now try treating the list as a sequence of
27461 arguments. */
27462 release_tree_vector (args);
27463 args = make_tree_vector_from_ctor (init);
27464 }
27465 }
27466
27467 /* Maybe generate an implicit deduction guide. */
27468 if (call == error_mark_node && args->length () < 2)
27469 {
27470 tree gtype = NULL_TREE;
27471
27472 if (args->length () == 1)
27473 /* Generate a copy guide. */
27474 gtype = build_reference_type (type);
27475 else if (!saw_ctor)
27476 /* Generate a default guide. */
27477 gtype = type;
27478
27479 if (gtype)
27480 {
27481 tree guide = build_deduction_guide (gtype, outer_args, complain);
27482 if (guide == error_mark_node)
27483 return error_mark_node;
27484 cands = lookup_add (guide, cands);
27485 }
27486 }
27487
27488 if (elided && !cands)
27489 {
27490 error ("cannot deduce template arguments for copy-initialization"
27491 " of %qT, as it has no non-explicit deduction guides or "
27492 "user-declared constructors", type);
27493 return error_mark_node;
27494 }
27495 else if (!cands && call == error_mark_node)
27496 {
27497 error ("cannot deduce template arguments of %qT, as it has no viable "
27498 "deduction guides", type);
27499 return error_mark_node;
27500 }
27501
27502 if (call == error_mark_node)
27503 {
27504 ++cp_unevaluated_operand;
27505 call = build_new_function_call (cands, &args, tf_decltype);
27506 --cp_unevaluated_operand;
27507 }
27508
27509 if (call == error_mark_node && (complain & tf_warning_or_error))
27510 {
27511 error ("class template argument deduction failed:");
27512
27513 ++cp_unevaluated_operand;
27514 call = build_new_function_call (cands, &args, complain | tf_decltype);
27515 --cp_unevaluated_operand;
27516
27517 if (elided)
27518 inform (input_location, "explicit deduction guides not considered "
27519 "for copy-initialization");
27520 }
27521
27522 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
27523 }
27524
27525 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
27526 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
27527 The CONTEXT determines the context in which auto deduction is performed
27528 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
27529 OUTER_TARGS are used during template argument deduction
27530 (context == adc_unify) to properly substitute the result, and is ignored
27531 in other contexts.
27532
27533 For partial-concept-ids, extra args may be appended to the list of deduced
27534 template arguments prior to determining constraint satisfaction. */
27535
27536 tree
27537 do_auto_deduction (tree type, tree init, tree auto_node,
27538 tsubst_flags_t complain, auto_deduction_context context,
27539 tree outer_targs, int flags)
27540 {
27541 tree targs;
27542
27543 if (init == error_mark_node)
27544 return error_mark_node;
27545
27546 if (init && type_dependent_expression_p (init)
27547 && context != adc_unify)
27548 /* Defining a subset of type-dependent expressions that we can deduce
27549 from ahead of time isn't worth the trouble. */
27550 return type;
27551
27552 /* Similarly, we can't deduce from another undeduced decl. */
27553 if (init && undeduced_auto_decl (init))
27554 return type;
27555
27556 /* We may be doing a partial substitution, but we still want to replace
27557 auto_node. */
27558 complain &= ~tf_partial;
27559
27560 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
27561 /* C++17 class template argument deduction. */
27562 return do_class_deduction (type, tmpl, init, flags, complain);
27563
27564 if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
27565 /* Nothing we can do with this, even in deduction context. */
27566 return type;
27567
27568 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
27569 with either a new invented type template parameter U or, if the
27570 initializer is a braced-init-list (8.5.4), with
27571 std::initializer_list<U>. */
27572 if (BRACE_ENCLOSED_INITIALIZER_P (init))
27573 {
27574 if (!DIRECT_LIST_INIT_P (init))
27575 type = listify_autos (type, auto_node);
27576 else if (CONSTRUCTOR_NELTS (init) == 1)
27577 init = CONSTRUCTOR_ELT (init, 0)->value;
27578 else
27579 {
27580 if (complain & tf_warning_or_error)
27581 {
27582 if (permerror (input_location, "direct-list-initialization of "
27583 "%<auto%> requires exactly one element"))
27584 inform (input_location,
27585 "for deduction to %<std::initializer_list%>, use copy-"
27586 "list-initialization (i.e. add %<=%> before the %<{%>)");
27587 }
27588 type = listify_autos (type, auto_node);
27589 }
27590 }
27591
27592 if (type == error_mark_node)
27593 return error_mark_node;
27594
27595 init = resolve_nondeduced_context (init, complain);
27596
27597 if (context == adc_decomp_type
27598 && auto_node == type
27599 && init != error_mark_node
27600 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
27601 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
27602 and initializer has array type, deduce cv-qualified array type. */
27603 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
27604 complain);
27605 else if (AUTO_IS_DECLTYPE (auto_node))
27606 {
27607 tree stripped_init = tree_strip_any_location_wrapper (init);
27608 bool id = (DECL_P (stripped_init)
27609 || ((TREE_CODE (init) == COMPONENT_REF
27610 || TREE_CODE (init) == SCOPE_REF)
27611 && !REF_PARENTHESIZED_P (init)));
27612 targs = make_tree_vec (1);
27613 TREE_VEC_ELT (targs, 0)
27614 = finish_decltype_type (init, id, tf_warning_or_error);
27615 if (type != auto_node)
27616 {
27617 if (complain & tf_error)
27618 error ("%qT as type rather than plain %<decltype(auto)%>", type);
27619 return error_mark_node;
27620 }
27621 }
27622 else
27623 {
27624 if (error_operand_p (init))
27625 return error_mark_node;
27626
27627 tree parms = build_tree_list (NULL_TREE, type);
27628 tree tparms;
27629
27630 if (flag_concepts)
27631 tparms = extract_autos (type);
27632 else
27633 {
27634 tparms = make_tree_vec (1);
27635 TREE_VEC_ELT (tparms, 0)
27636 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
27637 }
27638
27639 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
27640 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
27641 DEDUCE_CALL,
27642 NULL, /*explain_p=*/false);
27643 if (val > 0)
27644 {
27645 if (processing_template_decl)
27646 /* Try again at instantiation time. */
27647 return type;
27648 if (type && type != error_mark_node
27649 && (complain & tf_error))
27650 /* If type is error_mark_node a diagnostic must have been
27651 emitted by now. Also, having a mention to '<type error>'
27652 in the diagnostic is not really useful to the user. */
27653 {
27654 if (cfun
27655 && FNDECL_USED_AUTO (current_function_decl)
27656 && (auto_node
27657 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl))
27658 && LAMBDA_FUNCTION_P (current_function_decl))
27659 error ("unable to deduce lambda return type from %qE", init);
27660 else
27661 error ("unable to deduce %qT from %qE", type, init);
27662 type_unification_real (tparms, targs, parms, &init, 1, 0,
27663 DEDUCE_CALL,
27664 NULL, /*explain_p=*/true);
27665 }
27666 return error_mark_node;
27667 }
27668 }
27669
27670 /* Check any placeholder constraints against the deduced type. */
27671 if (flag_concepts && !processing_template_decl)
27672 if (tree constr = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node)))
27673 {
27674 /* Use the deduced type to check the associated constraints. If we
27675 have a partial-concept-id, rebuild the argument list so that
27676 we check using the extra arguments. */
27677 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
27678 tree cargs = CHECK_CONSTR_ARGS (constr);
27679 if (TREE_VEC_LENGTH (cargs) > 1)
27680 {
27681 cargs = copy_node (cargs);
27682 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
27683 }
27684 else
27685 cargs = targs;
27686 if (!constraints_satisfied_p (constr, cargs))
27687 {
27688 if (complain & tf_warning_or_error)
27689 {
27690 auto_diagnostic_group d;
27691 switch (context)
27692 {
27693 case adc_unspecified:
27694 case adc_unify:
27695 error("placeholder constraints not satisfied");
27696 break;
27697 case adc_variable_type:
27698 case adc_decomp_type:
27699 error ("deduced initializer does not satisfy "
27700 "placeholder constraints");
27701 break;
27702 case adc_return_type:
27703 error ("deduced return type does not satisfy "
27704 "placeholder constraints");
27705 break;
27706 case adc_requirement:
27707 error ("deduced expression type does not satisfy "
27708 "placeholder constraints");
27709 break;
27710 }
27711 diagnose_constraints (input_location, constr, targs);
27712 }
27713 return error_mark_node;
27714 }
27715 }
27716
27717 if (processing_template_decl && context != adc_unify)
27718 outer_targs = current_template_args ();
27719 targs = add_to_template_args (outer_targs, targs);
27720 return tsubst (type, targs, complain, NULL_TREE);
27721 }
27722
27723 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
27724 result. */
27725
27726 tree
27727 splice_late_return_type (tree type, tree late_return_type)
27728 {
27729 if (is_auto (type))
27730 {
27731 if (late_return_type)
27732 return late_return_type;
27733
27734 tree idx = get_template_parm_index (type);
27735 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
27736 /* In an abbreviated function template we didn't know we were dealing
27737 with a function template when we saw the auto return type, so update
27738 it to have the correct level. */
27739 return make_auto_1 (TYPE_IDENTIFIER (type), true);
27740 }
27741 return type;
27742 }
27743
27744 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
27745 'decltype(auto)' or a deduced class template. */
27746
27747 bool
27748 is_auto (const_tree type)
27749 {
27750 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
27751 && (TYPE_IDENTIFIER (type) == auto_identifier
27752 || TYPE_IDENTIFIER (type) == decltype_auto_identifier))
27753 return true;
27754 else
27755 return false;
27756 }
27757
27758 /* for_each_template_parm callback for type_uses_auto. */
27759
27760 int
27761 is_auto_r (tree tp, void */*data*/)
27762 {
27763 return is_auto (tp);
27764 }
27765
27766 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
27767 a use of `auto'. Returns NULL_TREE otherwise. */
27768
27769 tree
27770 type_uses_auto (tree type)
27771 {
27772 if (type == NULL_TREE)
27773 return NULL_TREE;
27774 else if (flag_concepts)
27775 {
27776 /* The Concepts TS allows multiple autos in one type-specifier; just
27777 return the first one we find, do_auto_deduction will collect all of
27778 them. */
27779 if (uses_template_parms (type))
27780 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
27781 /*visited*/NULL, /*nondeduced*/false);
27782 else
27783 return NULL_TREE;
27784 }
27785 else
27786 return find_type_usage (type, is_auto);
27787 }
27788
27789 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
27790 concepts are enabled, auto is acceptable in template arguments, but
27791 only when TEMPL identifies a template class. Return TRUE if any
27792 such errors were reported. */
27793
27794 bool
27795 check_auto_in_tmpl_args (tree tmpl, tree args)
27796 {
27797 /* If there were previous errors, nevermind. */
27798 if (!args || TREE_CODE (args) != TREE_VEC)
27799 return false;
27800
27801 /* If TMPL is an identifier, we're parsing and we can't tell yet
27802 whether TMPL is supposed to be a type, a function or a variable.
27803 We'll only be able to tell during template substitution, so we
27804 expect to be called again then. If concepts are enabled and we
27805 know we have a type, we're ok. */
27806 if (flag_concepts
27807 && (identifier_p (tmpl)
27808 || (DECL_P (tmpl)
27809 && (DECL_TYPE_TEMPLATE_P (tmpl)
27810 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))))
27811 return false;
27812
27813 /* Quickly search for any occurrences of auto; usually there won't
27814 be any, and then we'll avoid allocating the vector. */
27815 if (!type_uses_auto (args))
27816 return false;
27817
27818 bool errors = false;
27819
27820 tree vec = extract_autos (args);
27821 for (int i = 0; i < TREE_VEC_LENGTH (vec); i++)
27822 {
27823 tree xauto = TREE_VALUE (TREE_VEC_ELT (vec, i));
27824 error_at (DECL_SOURCE_LOCATION (xauto),
27825 "invalid use of %qT in template argument", xauto);
27826 errors = true;
27827 }
27828
27829 return errors;
27830 }
27831
27832 /* For a given template T, return the vector of typedefs referenced
27833 in T for which access check is needed at T instantiation time.
27834 T is either a FUNCTION_DECL or a RECORD_TYPE.
27835 Those typedefs were added to T by the function
27836 append_type_to_template_for_access_check. */
27837
27838 vec<qualified_typedef_usage_t, va_gc> *
27839 get_types_needing_access_check (tree t)
27840 {
27841 tree ti;
27842 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
27843
27844 if (!t || t == error_mark_node)
27845 return NULL;
27846
27847 if (!(ti = get_template_info (t)))
27848 return NULL;
27849
27850 if (CLASS_TYPE_P (t)
27851 || TREE_CODE (t) == FUNCTION_DECL)
27852 {
27853 if (!TI_TEMPLATE (ti))
27854 return NULL;
27855
27856 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
27857 }
27858
27859 return result;
27860 }
27861
27862 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
27863 tied to T. That list of typedefs will be access checked at
27864 T instantiation time.
27865 T is either a FUNCTION_DECL or a RECORD_TYPE.
27866 TYPE_DECL is a TYPE_DECL node representing a typedef.
27867 SCOPE is the scope through which TYPE_DECL is accessed.
27868 LOCATION is the location of the usage point of TYPE_DECL.
27869
27870 This function is a subroutine of
27871 append_type_to_template_for_access_check. */
27872
27873 static void
27874 append_type_to_template_for_access_check_1 (tree t,
27875 tree type_decl,
27876 tree scope,
27877 location_t location)
27878 {
27879 qualified_typedef_usage_t typedef_usage;
27880 tree ti;
27881
27882 if (!t || t == error_mark_node)
27883 return;
27884
27885 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
27886 || CLASS_TYPE_P (t))
27887 && type_decl
27888 && TREE_CODE (type_decl) == TYPE_DECL
27889 && scope);
27890
27891 if (!(ti = get_template_info (t)))
27892 return;
27893
27894 gcc_assert (TI_TEMPLATE (ti));
27895
27896 typedef_usage.typedef_decl = type_decl;
27897 typedef_usage.context = scope;
27898 typedef_usage.locus = location;
27899
27900 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
27901 }
27902
27903 /* Append TYPE_DECL to the template TEMPL.
27904 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
27905 At TEMPL instanciation time, TYPE_DECL will be checked to see
27906 if it can be accessed through SCOPE.
27907 LOCATION is the location of the usage point of TYPE_DECL.
27908
27909 e.g. consider the following code snippet:
27910
27911 class C
27912 {
27913 typedef int myint;
27914 };
27915
27916 template<class U> struct S
27917 {
27918 C::myint mi; // <-- usage point of the typedef C::myint
27919 };
27920
27921 S<char> s;
27922
27923 At S<char> instantiation time, we need to check the access of C::myint
27924 In other words, we need to check the access of the myint typedef through
27925 the C scope. For that purpose, this function will add the myint typedef
27926 and the scope C through which its being accessed to a list of typedefs
27927 tied to the template S. That list will be walked at template instantiation
27928 time and access check performed on each typedefs it contains.
27929 Note that this particular code snippet should yield an error because
27930 myint is private to C. */
27931
27932 void
27933 append_type_to_template_for_access_check (tree templ,
27934 tree type_decl,
27935 tree scope,
27936 location_t location)
27937 {
27938 qualified_typedef_usage_t *iter;
27939 unsigned i;
27940
27941 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
27942
27943 /* Make sure we don't append the type to the template twice. */
27944 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
27945 if (iter->typedef_decl == type_decl && scope == iter->context)
27946 return;
27947
27948 append_type_to_template_for_access_check_1 (templ, type_decl,
27949 scope, location);
27950 }
27951
27952 /* Convert the generic type parameters in PARM that match the types given in the
27953 range [START_IDX, END_IDX) from the current_template_parms into generic type
27954 packs. */
27955
27956 tree
27957 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
27958 {
27959 tree current = current_template_parms;
27960 int depth = TMPL_PARMS_DEPTH (current);
27961 current = INNERMOST_TEMPLATE_PARMS (current);
27962 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
27963
27964 for (int i = 0; i < start_idx; ++i)
27965 TREE_VEC_ELT (replacement, i)
27966 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
27967
27968 for (int i = start_idx; i < end_idx; ++i)
27969 {
27970 /* Create a distinct parameter pack type from the current parm and add it
27971 to the replacement args to tsubst below into the generic function
27972 parameter. */
27973
27974 tree o = TREE_TYPE (TREE_VALUE
27975 (TREE_VEC_ELT (current, i)));
27976 tree t = copy_type (o);
27977 TEMPLATE_TYPE_PARM_INDEX (t)
27978 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
27979 o, 0, 0, tf_none);
27980 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
27981 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
27982 TYPE_MAIN_VARIANT (t) = t;
27983 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
27984 TYPE_CANONICAL (t) = canonical_type_parameter (t);
27985 TREE_VEC_ELT (replacement, i) = t;
27986 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
27987 }
27988
27989 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
27990 TREE_VEC_ELT (replacement, i)
27991 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
27992
27993 /* If there are more levels then build up the replacement with the outer
27994 template parms. */
27995 if (depth > 1)
27996 replacement = add_to_template_args (template_parms_to_args
27997 (TREE_CHAIN (current_template_parms)),
27998 replacement);
27999
28000 return tsubst (parm, replacement, tf_none, NULL_TREE);
28001 }
28002
28003 /* Entries in the decl_constraint hash table. */
28004 struct GTY((for_user)) constr_entry
28005 {
28006 tree decl;
28007 tree ci;
28008 };
28009
28010 /* Hashing function and equality for constraint entries. */
28011 struct constr_hasher : ggc_ptr_hash<constr_entry>
28012 {
28013 static hashval_t hash (constr_entry *e)
28014 {
28015 return (hashval_t)DECL_UID (e->decl);
28016 }
28017
28018 static bool equal (constr_entry *e1, constr_entry *e2)
28019 {
28020 return e1->decl == e2->decl;
28021 }
28022 };
28023
28024 /* A mapping from declarations to constraint information. Note that
28025 both templates and their underlying declarations are mapped to the
28026 same constraint information.
28027
28028 FIXME: This is defined in pt.c because garbage collection
28029 code is not being generated for constraint.cc. */
28030
28031 static GTY (()) hash_table<constr_hasher> *decl_constraints;
28032
28033 /* Returns the template constraints of declaration T. If T is not
28034 constrained, return NULL_TREE. Note that T must be non-null. */
28035
28036 tree
28037 get_constraints (tree t)
28038 {
28039 if (!flag_concepts)
28040 return NULL_TREE;
28041
28042 gcc_assert (DECL_P (t));
28043 if (TREE_CODE (t) == TEMPLATE_DECL)
28044 t = DECL_TEMPLATE_RESULT (t);
28045 constr_entry elt = { t, NULL_TREE };
28046 constr_entry* found = decl_constraints->find (&elt);
28047 if (found)
28048 return found->ci;
28049 else
28050 return NULL_TREE;
28051 }
28052
28053 /* Associate the given constraint information CI with the declaration
28054 T. If T is a template, then the constraints are associated with
28055 its underlying declaration. Don't build associations if CI is
28056 NULL_TREE. */
28057
28058 void
28059 set_constraints (tree t, tree ci)
28060 {
28061 if (!ci)
28062 return;
28063 gcc_assert (t && flag_concepts);
28064 if (TREE_CODE (t) == TEMPLATE_DECL)
28065 t = DECL_TEMPLATE_RESULT (t);
28066 gcc_assert (!get_constraints (t));
28067 constr_entry elt = {t, ci};
28068 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
28069 constr_entry* entry = ggc_alloc<constr_entry> ();
28070 *entry = elt;
28071 *slot = entry;
28072 }
28073
28074 /* Remove the associated constraints of the declaration T. */
28075
28076 void
28077 remove_constraints (tree t)
28078 {
28079 gcc_assert (DECL_P (t));
28080 if (TREE_CODE (t) == TEMPLATE_DECL)
28081 t = DECL_TEMPLATE_RESULT (t);
28082
28083 constr_entry elt = {t, NULL_TREE};
28084 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
28085 if (slot)
28086 decl_constraints->clear_slot (slot);
28087 }
28088
28089 /* Memoized satisfaction results for declarations. This
28090 maps the pair (constraint_info, arguments) to the result computed
28091 by constraints_satisfied_p. */
28092
28093 struct GTY((for_user)) constraint_sat_entry
28094 {
28095 tree ci;
28096 tree args;
28097 tree result;
28098 };
28099
28100 /* Hashing function and equality for constraint entries. */
28101
28102 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
28103 {
28104 static hashval_t hash (constraint_sat_entry *e)
28105 {
28106 hashval_t val = iterative_hash_object(e->ci, 0);
28107 return iterative_hash_template_arg (e->args, val);
28108 }
28109
28110 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
28111 {
28112 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
28113 }
28114 };
28115
28116 /* Memoized satisfaction results for concept checks. */
28117
28118 struct GTY((for_user)) concept_spec_entry
28119 {
28120 tree tmpl;
28121 tree args;
28122 tree result;
28123 };
28124
28125 /* Hashing function and equality for constraint entries. */
28126
28127 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
28128 {
28129 static hashval_t hash (concept_spec_entry *e)
28130 {
28131 return hash_tmpl_and_args (e->tmpl, e->args);
28132 }
28133
28134 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
28135 {
28136 ++comparing_specializations;
28137 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
28138 --comparing_specializations;
28139 return eq;
28140 }
28141 };
28142
28143 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
28144 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
28145
28146 /* Search for a memoized satisfaction result. Returns one of the
28147 truth value nodes if previously memoized, or NULL_TREE otherwise. */
28148
28149 tree
28150 lookup_constraint_satisfaction (tree ci, tree args)
28151 {
28152 constraint_sat_entry elt = { ci, args, NULL_TREE };
28153 constraint_sat_entry* found = constraint_memos->find (&elt);
28154 if (found)
28155 return found->result;
28156 else
28157 return NULL_TREE;
28158 }
28159
28160 /* Memoize the result of a satisfication test. Returns the saved result. */
28161
28162 tree
28163 memoize_constraint_satisfaction (tree ci, tree args, tree result)
28164 {
28165 constraint_sat_entry elt = {ci, args, result};
28166 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
28167 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
28168 *entry = elt;
28169 *slot = entry;
28170 return result;
28171 }
28172
28173 /* Search for a memoized satisfaction result for a concept. */
28174
28175 tree
28176 lookup_concept_satisfaction (tree tmpl, tree args)
28177 {
28178 concept_spec_entry elt = { tmpl, args, NULL_TREE };
28179 concept_spec_entry* found = concept_memos->find (&elt);
28180 if (found)
28181 return found->result;
28182 else
28183 return NULL_TREE;
28184 }
28185
28186 /* Memoize the result of a concept check. Returns the saved result. */
28187
28188 tree
28189 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
28190 {
28191 concept_spec_entry elt = {tmpl, args, result};
28192 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
28193 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
28194 *entry = elt;
28195 *slot = entry;
28196 return result;
28197 }
28198
28199 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
28200
28201 /* Returns a prior concept specialization. This returns the substituted
28202 and normalized constraints defined by the concept. */
28203
28204 tree
28205 get_concept_expansion (tree tmpl, tree args)
28206 {
28207 concept_spec_entry elt = { tmpl, args, NULL_TREE };
28208 concept_spec_entry* found = concept_expansions->find (&elt);
28209 if (found)
28210 return found->result;
28211 else
28212 return NULL_TREE;
28213 }
28214
28215 /* Save a concept expansion for later. */
28216
28217 tree
28218 save_concept_expansion (tree tmpl, tree args, tree def)
28219 {
28220 concept_spec_entry elt = {tmpl, args, def};
28221 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
28222 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
28223 *entry = elt;
28224 *slot = entry;
28225 return def;
28226 }
28227
28228 static hashval_t
28229 hash_subsumption_args (tree t1, tree t2)
28230 {
28231 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
28232 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
28233 int val = 0;
28234 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
28235 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
28236 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
28237 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
28238 return val;
28239 }
28240
28241 /* Compare the constraints of two subsumption entries. The LEFT1 and
28242 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
28243 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
28244
28245 static bool
28246 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
28247 {
28248 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
28249 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
28250 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
28251 CHECK_CONSTR_ARGS (right1)))
28252 return comp_template_args (CHECK_CONSTR_ARGS (left2),
28253 CHECK_CONSTR_ARGS (right2));
28254 return false;
28255 }
28256
28257 /* Key/value pair for learning and memoizing subsumption results. This
28258 associates a pair of check constraints (including arguments) with
28259 a boolean value indicating the result. */
28260
28261 struct GTY((for_user)) subsumption_entry
28262 {
28263 tree t1;
28264 tree t2;
28265 bool result;
28266 };
28267
28268 /* Hashing function and equality for constraint entries. */
28269
28270 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
28271 {
28272 static hashval_t hash (subsumption_entry *e)
28273 {
28274 return hash_subsumption_args (e->t1, e->t2);
28275 }
28276
28277 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
28278 {
28279 ++comparing_specializations;
28280 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
28281 --comparing_specializations;
28282 return eq;
28283 }
28284 };
28285
28286 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
28287
28288 /* Search for a previously cached subsumption result. */
28289
28290 bool*
28291 lookup_subsumption_result (tree t1, tree t2)
28292 {
28293 subsumption_entry elt = { t1, t2, false };
28294 subsumption_entry* found = subsumption_table->find (&elt);
28295 if (found)
28296 return &found->result;
28297 else
28298 return 0;
28299 }
28300
28301 /* Save a subsumption result. */
28302
28303 bool
28304 save_subsumption_result (tree t1, tree t2, bool result)
28305 {
28306 subsumption_entry elt = {t1, t2, result};
28307 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
28308 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
28309 *entry = elt;
28310 *slot = entry;
28311 return result;
28312 }
28313
28314 /* Set up the hash table for constraint association. */
28315
28316 void
28317 init_constraint_processing (void)
28318 {
28319 if (!flag_concepts)
28320 return;
28321
28322 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
28323 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
28324 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
28325 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
28326 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
28327 }
28328
28329 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
28330 0..N-1. */
28331
28332 void
28333 declare_integer_pack (void)
28334 {
28335 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
28336 build_function_type_list (integer_type_node,
28337 integer_type_node,
28338 NULL_TREE),
28339 NULL_TREE, ECF_CONST);
28340 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
28341 set_decl_built_in_function (ipfn, BUILT_IN_FRONTEND,
28342 CP_BUILT_IN_INTEGER_PACK);
28343 }
28344
28345 /* Set up the hash tables for template instantiations. */
28346
28347 void
28348 init_template_processing (void)
28349 {
28350 /* FIXME: enable sanitization (PR87847) */
28351 decl_specializations = hash_table<spec_hasher>::create_ggc (37, false);
28352 type_specializations = hash_table<spec_hasher>::create_ggc (37, false);
28353
28354 if (cxx_dialect >= cxx11)
28355 declare_integer_pack ();
28356 }
28357
28358 /* Print stats about the template hash tables for -fstats. */
28359
28360 void
28361 print_template_statistics (void)
28362 {
28363 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
28364 "%f collisions\n", (long) decl_specializations->size (),
28365 (long) decl_specializations->elements (),
28366 decl_specializations->collisions ());
28367 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
28368 "%f collisions\n", (long) type_specializations->size (),
28369 (long) type_specializations->elements (),
28370 type_specializations->collisions ());
28371 }
28372
28373 #if CHECKING_P
28374
28375 namespace selftest {
28376
28377 /* Verify that build_non_dependent_expr () works, for various expressions,
28378 and that location wrappers don't affect the results. */
28379
28380 static void
28381 test_build_non_dependent_expr ()
28382 {
28383 location_t loc = BUILTINS_LOCATION;
28384
28385 /* Verify constants, without and with location wrappers. */
28386 tree int_cst = build_int_cst (integer_type_node, 42);
28387 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
28388
28389 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
28390 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
28391 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
28392
28393 tree string_lit = build_string (4, "foo");
28394 TREE_TYPE (string_lit) = char_array_type_node;
28395 string_lit = fix_string_type (string_lit);
28396 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
28397
28398 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
28399 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
28400 ASSERT_EQ (wrapped_string_lit,
28401 build_non_dependent_expr (wrapped_string_lit));
28402 }
28403
28404 /* Verify that type_dependent_expression_p () works correctly, even
28405 in the presence of location wrapper nodes. */
28406
28407 static void
28408 test_type_dependent_expression_p ()
28409 {
28410 location_t loc = BUILTINS_LOCATION;
28411
28412 tree name = get_identifier ("foo");
28413
28414 /* If no templates are involved, nothing is type-dependent. */
28415 gcc_assert (!processing_template_decl);
28416 ASSERT_FALSE (type_dependent_expression_p (name));
28417
28418 ++processing_template_decl;
28419
28420 /* Within a template, an unresolved name is always type-dependent. */
28421 ASSERT_TRUE (type_dependent_expression_p (name));
28422
28423 /* Ensure it copes with NULL_TREE and errors. */
28424 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
28425 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
28426
28427 /* A USING_DECL in a template should be type-dependent, even if wrapped
28428 with a location wrapper (PR c++/83799). */
28429 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
28430 TREE_TYPE (using_decl) = integer_type_node;
28431 ASSERT_TRUE (type_dependent_expression_p (using_decl));
28432 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
28433 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
28434 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
28435
28436 --processing_template_decl;
28437 }
28438
28439 /* Run all of the selftests within this file. */
28440
28441 void
28442 cp_pt_c_tests ()
28443 {
28444 test_build_non_dependent_expr ();
28445 test_type_dependent_expression_p ();
28446 }
28447
28448 } // namespace selftest
28449
28450 #endif /* #if CHECKING_P */
28451
28452 #include "gt-cp-pt.h"