]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/pt.c
call.c (resolve_args): Use cp_expr_loc_or_input_loc in one place.
[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_at (DECL_SOURCE_LOCATION (decl),
2141 "%qD is not a function template", fns);
2142 return error_mark_node;
2143 }
2144 else if (VAR_P (decl) && !variable_template_p (fns))
2145 {
2146 error ("%qD is not a variable template", fns);
2147 return error_mark_node;
2148 }
2149
2150 /* Count the number of template headers specified for this
2151 specialization. */
2152 header_count = 0;
2153 for (b = current_binding_level;
2154 b->kind == sk_template_parms;
2155 b = b->level_chain)
2156 ++header_count;
2157
2158 tree orig_fns = fns;
2159
2160 if (variable_template_p (fns))
2161 {
2162 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2163 targs = coerce_template_parms (parms, explicit_targs, fns,
2164 tf_warning_or_error,
2165 /*req_all*/true, /*use_defarg*/true);
2166 if (targs != error_mark_node)
2167 templates = tree_cons (targs, fns, templates);
2168 }
2169 else for (lkp_iterator iter (fns); iter; ++iter)
2170 {
2171 tree fn = *iter;
2172
2173 if (TREE_CODE (fn) == TEMPLATE_DECL)
2174 {
2175 tree decl_arg_types;
2176 tree fn_arg_types;
2177 tree insttype;
2178
2179 /* In case of explicit specialization, we need to check if
2180 the number of template headers appearing in the specialization
2181 is correct. This is usually done in check_explicit_specialization,
2182 but the check done there cannot be exhaustive when specializing
2183 member functions. Consider the following code:
2184
2185 template <> void A<int>::f(int);
2186 template <> template <> void A<int>::f(int);
2187
2188 Assuming that A<int> is not itself an explicit specialization
2189 already, the first line specializes "f" which is a non-template
2190 member function, whilst the second line specializes "f" which
2191 is a template member function. So both lines are syntactically
2192 correct, and check_explicit_specialization does not reject
2193 them.
2194
2195 Here, we can do better, as we are matching the specialization
2196 against the declarations. We count the number of template
2197 headers, and we check if they match TEMPLATE_COUNT + 1
2198 (TEMPLATE_COUNT is the number of qualifying template classes,
2199 plus there must be another header for the member template
2200 itself).
2201
2202 Notice that if header_count is zero, this is not a
2203 specialization but rather a template instantiation, so there
2204 is no check we can perform here. */
2205 if (header_count && header_count != template_count + 1)
2206 continue;
2207
2208 /* Check that the number of template arguments at the
2209 innermost level for DECL is the same as for FN. */
2210 if (current_binding_level->kind == sk_template_parms
2211 && !current_binding_level->explicit_spec_p
2212 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2213 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2214 (current_template_parms))))
2215 continue;
2216
2217 /* DECL might be a specialization of FN. */
2218 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2219 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2220
2221 /* For a non-static member function, we need to make sure
2222 that the const qualification is the same. Since
2223 get_bindings does not try to merge the "this" parameter,
2224 we must do the comparison explicitly. */
2225 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2226 {
2227 if (!same_type_p (TREE_VALUE (fn_arg_types),
2228 TREE_VALUE (decl_arg_types)))
2229 continue;
2230
2231 /* And the ref-qualification. */
2232 if (type_memfn_rqual (TREE_TYPE (decl))
2233 != type_memfn_rqual (TREE_TYPE (fn)))
2234 continue;
2235 }
2236
2237 /* Skip the "this" parameter and, for constructors of
2238 classes with virtual bases, the VTT parameter. A
2239 full specialization of a constructor will have a VTT
2240 parameter, but a template never will. */
2241 decl_arg_types
2242 = skip_artificial_parms_for (decl, decl_arg_types);
2243 fn_arg_types
2244 = skip_artificial_parms_for (fn, fn_arg_types);
2245
2246 /* Function templates cannot be specializations; there are
2247 no partial specializations of functions. Therefore, if
2248 the type of DECL does not match FN, there is no
2249 match.
2250
2251 Note that it should never be the case that we have both
2252 candidates added here, and for regular member functions
2253 below. */
2254 if (tsk == tsk_template)
2255 {
2256 if (compparms (fn_arg_types, decl_arg_types))
2257 candidates = tree_cons (NULL_TREE, fn, candidates);
2258 continue;
2259 }
2260
2261 /* See whether this function might be a specialization of this
2262 template. Suppress access control because we might be trying
2263 to make this specialization a friend, and we have already done
2264 access control for the declaration of the specialization. */
2265 push_deferring_access_checks (dk_no_check);
2266 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2267 pop_deferring_access_checks ();
2268
2269 if (!targs)
2270 /* We cannot deduce template arguments that when used to
2271 specialize TMPL will produce DECL. */
2272 continue;
2273
2274 if (uses_template_parms (targs))
2275 /* We deduced something involving 'auto', which isn't a valid
2276 template argument. */
2277 continue;
2278
2279 /* Remove, from the set of candidates, all those functions
2280 whose constraints are not satisfied. */
2281 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2282 continue;
2283
2284 // Then, try to form the new function type.
2285 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2286 if (insttype == error_mark_node)
2287 continue;
2288 fn_arg_types
2289 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2290 if (!compparms (fn_arg_types, decl_arg_types))
2291 continue;
2292
2293 /* Save this template, and the arguments deduced. */
2294 templates = tree_cons (targs, fn, templates);
2295 }
2296 else if (need_member_template)
2297 /* FN is an ordinary member function, and we need a
2298 specialization of a member template. */
2299 ;
2300 else if (TREE_CODE (fn) != FUNCTION_DECL)
2301 /* We can get IDENTIFIER_NODEs here in certain erroneous
2302 cases. */
2303 ;
2304 else if (!DECL_FUNCTION_MEMBER_P (fn))
2305 /* This is just an ordinary non-member function. Nothing can
2306 be a specialization of that. */
2307 ;
2308 else if (DECL_ARTIFICIAL (fn))
2309 /* Cannot specialize functions that are created implicitly. */
2310 ;
2311 else
2312 {
2313 tree decl_arg_types;
2314
2315 /* This is an ordinary member function. However, since
2316 we're here, we can assume its enclosing class is a
2317 template class. For example,
2318
2319 template <typename T> struct S { void f(); };
2320 template <> void S<int>::f() {}
2321
2322 Here, S<int>::f is a non-template, but S<int> is a
2323 template class. If FN has the same type as DECL, we
2324 might be in business. */
2325
2326 if (!DECL_TEMPLATE_INFO (fn))
2327 /* Its enclosing class is an explicit specialization
2328 of a template class. This is not a candidate. */
2329 continue;
2330
2331 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2332 TREE_TYPE (TREE_TYPE (fn))))
2333 /* The return types differ. */
2334 continue;
2335
2336 /* Adjust the type of DECL in case FN is a static member. */
2337 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2338 if (DECL_STATIC_FUNCTION_P (fn)
2339 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2340 decl_arg_types = TREE_CHAIN (decl_arg_types);
2341
2342 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2343 decl_arg_types))
2344 continue;
2345
2346 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2347 && (type_memfn_rqual (TREE_TYPE (decl))
2348 != type_memfn_rqual (TREE_TYPE (fn))))
2349 continue;
2350
2351 // If the deduced arguments do not satisfy the constraints,
2352 // this is not a candidate.
2353 if (flag_concepts && !constraints_satisfied_p (fn))
2354 continue;
2355
2356 // Add the candidate.
2357 candidates = tree_cons (NULL_TREE, fn, candidates);
2358 }
2359 }
2360
2361 if (templates && TREE_CHAIN (templates))
2362 {
2363 /* We have:
2364
2365 [temp.expl.spec]
2366
2367 It is possible for a specialization with a given function
2368 signature to be instantiated from more than one function
2369 template. In such cases, explicit specification of the
2370 template arguments must be used to uniquely identify the
2371 function template specialization being specialized.
2372
2373 Note that here, there's no suggestion that we're supposed to
2374 determine which of the candidate templates is most
2375 specialized. However, we, also have:
2376
2377 [temp.func.order]
2378
2379 Partial ordering of overloaded function template
2380 declarations is used in the following contexts to select
2381 the function template to which a function template
2382 specialization refers:
2383
2384 -- when an explicit specialization refers to a function
2385 template.
2386
2387 So, we do use the partial ordering rules, at least for now.
2388 This extension can only serve to make invalid programs valid,
2389 so it's safe. And, there is strong anecdotal evidence that
2390 the committee intended the partial ordering rules to apply;
2391 the EDG front end has that behavior, and John Spicer claims
2392 that the committee simply forgot to delete the wording in
2393 [temp.expl.spec]. */
2394 tree tmpl = most_specialized_instantiation (templates);
2395 if (tmpl != error_mark_node)
2396 {
2397 templates = tmpl;
2398 TREE_CHAIN (templates) = NULL_TREE;
2399 }
2400 }
2401
2402 // Concepts allows multiple declarations of member functions
2403 // with the same signature. Like above, we need to rely on
2404 // on the partial ordering of those candidates to determine which
2405 // is the best.
2406 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2407 {
2408 if (tree cand = most_constrained_function (candidates))
2409 {
2410 candidates = cand;
2411 TREE_CHAIN (cand) = NULL_TREE;
2412 }
2413 }
2414
2415 if (templates == NULL_TREE && candidates == NULL_TREE)
2416 {
2417 error ("template-id %qD for %q+D does not match any template "
2418 "declaration", template_id, decl);
2419 if (header_count && header_count != template_count + 1)
2420 inform (DECL_SOURCE_LOCATION (decl),
2421 "saw %d %<template<>%>, need %d for "
2422 "specializing a member function template",
2423 header_count, template_count + 1);
2424 else
2425 print_candidates (orig_fns);
2426 return error_mark_node;
2427 }
2428 else if ((templates && TREE_CHAIN (templates))
2429 || (candidates && TREE_CHAIN (candidates))
2430 || (templates && candidates))
2431 {
2432 error ("ambiguous template specialization %qD for %q+D",
2433 template_id, decl);
2434 candidates = chainon (candidates, templates);
2435 print_candidates (candidates);
2436 return error_mark_node;
2437 }
2438
2439 /* We have one, and exactly one, match. */
2440 if (candidates)
2441 {
2442 tree fn = TREE_VALUE (candidates);
2443 *targs_out = copy_node (DECL_TI_ARGS (fn));
2444
2445 /* Propagate the candidate's constraints to the declaration. */
2446 set_constraints (decl, get_constraints (fn));
2447
2448 /* DECL is a re-declaration or partial instantiation of a template
2449 function. */
2450 if (TREE_CODE (fn) == TEMPLATE_DECL)
2451 return fn;
2452 /* It was a specialization of an ordinary member function in a
2453 template class. */
2454 return DECL_TI_TEMPLATE (fn);
2455 }
2456
2457 /* It was a specialization of a template. */
2458 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2459 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2460 {
2461 *targs_out = copy_node (targs);
2462 SET_TMPL_ARGS_LEVEL (*targs_out,
2463 TMPL_ARGS_DEPTH (*targs_out),
2464 TREE_PURPOSE (templates));
2465 }
2466 else
2467 *targs_out = TREE_PURPOSE (templates);
2468 return TREE_VALUE (templates);
2469 }
2470
2471 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2472 but with the default argument values filled in from those in the
2473 TMPL_TYPES. */
2474
2475 static tree
2476 copy_default_args_to_explicit_spec_1 (tree spec_types,
2477 tree tmpl_types)
2478 {
2479 tree new_spec_types;
2480
2481 if (!spec_types)
2482 return NULL_TREE;
2483
2484 if (spec_types == void_list_node)
2485 return void_list_node;
2486
2487 /* Substitute into the rest of the list. */
2488 new_spec_types =
2489 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2490 TREE_CHAIN (tmpl_types));
2491
2492 /* Add the default argument for this parameter. */
2493 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2494 TREE_VALUE (spec_types),
2495 new_spec_types);
2496 }
2497
2498 /* DECL is an explicit specialization. Replicate default arguments
2499 from the template it specializes. (That way, code like:
2500
2501 template <class T> void f(T = 3);
2502 template <> void f(double);
2503 void g () { f (); }
2504
2505 works, as required.) An alternative approach would be to look up
2506 the correct default arguments at the call-site, but this approach
2507 is consistent with how implicit instantiations are handled. */
2508
2509 static void
2510 copy_default_args_to_explicit_spec (tree decl)
2511 {
2512 tree tmpl;
2513 tree spec_types;
2514 tree tmpl_types;
2515 tree new_spec_types;
2516 tree old_type;
2517 tree new_type;
2518 tree t;
2519 tree object_type = NULL_TREE;
2520 tree in_charge = NULL_TREE;
2521 tree vtt = NULL_TREE;
2522
2523 /* See if there's anything we need to do. */
2524 tmpl = DECL_TI_TEMPLATE (decl);
2525 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2526 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2527 if (TREE_PURPOSE (t))
2528 break;
2529 if (!t)
2530 return;
2531
2532 old_type = TREE_TYPE (decl);
2533 spec_types = TYPE_ARG_TYPES (old_type);
2534
2535 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2536 {
2537 /* Remove the this pointer, but remember the object's type for
2538 CV quals. */
2539 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2540 spec_types = TREE_CHAIN (spec_types);
2541 tmpl_types = TREE_CHAIN (tmpl_types);
2542
2543 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2544 {
2545 /* DECL may contain more parameters than TMPL due to the extra
2546 in-charge parameter in constructors and destructors. */
2547 in_charge = spec_types;
2548 spec_types = TREE_CHAIN (spec_types);
2549 }
2550 if (DECL_HAS_VTT_PARM_P (decl))
2551 {
2552 vtt = spec_types;
2553 spec_types = TREE_CHAIN (spec_types);
2554 }
2555 }
2556
2557 /* Compute the merged default arguments. */
2558 new_spec_types =
2559 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2560
2561 /* Compute the new FUNCTION_TYPE. */
2562 if (object_type)
2563 {
2564 if (vtt)
2565 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2566 TREE_VALUE (vtt),
2567 new_spec_types);
2568
2569 if (in_charge)
2570 /* Put the in-charge parameter back. */
2571 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2572 TREE_VALUE (in_charge),
2573 new_spec_types);
2574
2575 new_type = build_method_type_directly (object_type,
2576 TREE_TYPE (old_type),
2577 new_spec_types);
2578 }
2579 else
2580 new_type = build_function_type (TREE_TYPE (old_type),
2581 new_spec_types);
2582 new_type = cp_build_type_attribute_variant (new_type,
2583 TYPE_ATTRIBUTES (old_type));
2584 new_type = cxx_copy_lang_qualifiers (new_type, old_type);
2585
2586 TREE_TYPE (decl) = new_type;
2587 }
2588
2589 /* Return the number of template headers we expect to see for a definition
2590 or specialization of CTYPE or one of its non-template members. */
2591
2592 int
2593 num_template_headers_for_class (tree ctype)
2594 {
2595 int num_templates = 0;
2596
2597 while (ctype && CLASS_TYPE_P (ctype))
2598 {
2599 /* You're supposed to have one `template <...>' for every
2600 template class, but you don't need one for a full
2601 specialization. For example:
2602
2603 template <class T> struct S{};
2604 template <> struct S<int> { void f(); };
2605 void S<int>::f () {}
2606
2607 is correct; there shouldn't be a `template <>' for the
2608 definition of `S<int>::f'. */
2609 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2610 /* If CTYPE does not have template information of any
2611 kind, then it is not a template, nor is it nested
2612 within a template. */
2613 break;
2614 if (explicit_class_specialization_p (ctype))
2615 break;
2616 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2617 ++num_templates;
2618
2619 ctype = TYPE_CONTEXT (ctype);
2620 }
2621
2622 return num_templates;
2623 }
2624
2625 /* Do a simple sanity check on the template headers that precede the
2626 variable declaration DECL. */
2627
2628 void
2629 check_template_variable (tree decl)
2630 {
2631 tree ctx = CP_DECL_CONTEXT (decl);
2632 int wanted = num_template_headers_for_class (ctx);
2633 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2634 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2635 {
2636 if (cxx_dialect < cxx14)
2637 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2638 "variable templates only available with "
2639 "%<-std=c++14%> or %<-std=gnu++14%>");
2640
2641 // Namespace-scope variable templates should have a template header.
2642 ++wanted;
2643 }
2644 if (template_header_count > wanted)
2645 {
2646 auto_diagnostic_group d;
2647 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2648 "too many template headers for %qD "
2649 "(should be %d)",
2650 decl, wanted);
2651 if (warned && CLASS_TYPE_P (ctx)
2652 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2653 inform (DECL_SOURCE_LOCATION (decl),
2654 "members of an explicitly specialized class are defined "
2655 "without a template header");
2656 }
2657 }
2658
2659 /* An explicit specialization whose declarator-id or class-head-name is not
2660 qualified shall be declared in the nearest enclosing namespace of the
2661 template, or, if the namespace is inline (7.3.1), any namespace from its
2662 enclosing namespace set.
2663
2664 If the name declared in the explicit instantiation is an unqualified name,
2665 the explicit instantiation shall appear in the namespace where its template
2666 is declared or, if that namespace is inline (7.3.1), any namespace from its
2667 enclosing namespace set. */
2668
2669 void
2670 check_unqualified_spec_or_inst (tree t, location_t loc)
2671 {
2672 tree tmpl = most_general_template (t);
2673 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2674 && !is_nested_namespace (current_namespace,
2675 CP_DECL_CONTEXT (tmpl), true))
2676 {
2677 if (processing_specialization)
2678 permerror (loc, "explicit specialization of %qD outside its "
2679 "namespace must use a nested-name-specifier", tmpl);
2680 else if (processing_explicit_instantiation
2681 && cxx_dialect >= cxx11)
2682 /* This was allowed in C++98, so only pedwarn. */
2683 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2684 "outside its namespace must use a nested-name-"
2685 "specifier", tmpl);
2686 }
2687 }
2688
2689 /* Warn for a template specialization SPEC that is missing some of a set
2690 of function or type attributes that the template TEMPL is declared with.
2691 ATTRLIST is a list of additional attributes that SPEC should be taken
2692 to ultimately be declared with. */
2693
2694 static void
2695 warn_spec_missing_attributes (tree tmpl, tree spec, tree attrlist)
2696 {
2697 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
2698 tmpl = DECL_TEMPLATE_RESULT (tmpl);
2699
2700 /* Avoid warning if the difference between the primary and
2701 the specialization is not in one of the attributes below. */
2702 const char* const blacklist[] = {
2703 "alloc_align", "alloc_size", "assume_aligned", "format",
2704 "format_arg", "malloc", "nonnull", NULL
2705 };
2706
2707 /* Put together a list of the black listed attributes that the primary
2708 template is declared with that the specialization is not, in case
2709 it's not apparent from the most recent declaration of the primary. */
2710 pretty_printer str;
2711 unsigned nattrs = decls_mismatched_attributes (tmpl, spec, attrlist,
2712 blacklist, &str);
2713
2714 if (!nattrs)
2715 return;
2716
2717 auto_diagnostic_group d;
2718 if (warning_at (DECL_SOURCE_LOCATION (spec), OPT_Wmissing_attributes,
2719 "explicit specialization %q#D may be missing attributes",
2720 spec))
2721 inform (DECL_SOURCE_LOCATION (tmpl),
2722 nattrs > 1
2723 ? G_("missing primary template attributes %s")
2724 : G_("missing primary template attribute %s"),
2725 pp_formatted_text (&str));
2726 }
2727
2728 /* Check to see if the function just declared, as indicated in
2729 DECLARATOR, and in DECL, is a specialization of a function
2730 template. We may also discover that the declaration is an explicit
2731 instantiation at this point.
2732
2733 Returns DECL, or an equivalent declaration that should be used
2734 instead if all goes well. Issues an error message if something is
2735 amiss. Returns error_mark_node if the error is not easily
2736 recoverable.
2737
2738 FLAGS is a bitmask consisting of the following flags:
2739
2740 2: The function has a definition.
2741 4: The function is a friend.
2742
2743 The TEMPLATE_COUNT is the number of references to qualifying
2744 template classes that appeared in the name of the function. For
2745 example, in
2746
2747 template <class T> struct S { void f(); };
2748 void S<int>::f();
2749
2750 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2751 classes are not counted in the TEMPLATE_COUNT, so that in
2752
2753 template <class T> struct S {};
2754 template <> struct S<int> { void f(); }
2755 template <> void S<int>::f();
2756
2757 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2758 invalid; there should be no template <>.)
2759
2760 If the function is a specialization, it is marked as such via
2761 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2762 is set up correctly, and it is added to the list of specializations
2763 for that template. */
2764
2765 tree
2766 check_explicit_specialization (tree declarator,
2767 tree decl,
2768 int template_count,
2769 int flags,
2770 tree attrlist)
2771 {
2772 int have_def = flags & 2;
2773 int is_friend = flags & 4;
2774 bool is_concept = flags & 8;
2775 int specialization = 0;
2776 int explicit_instantiation = 0;
2777 int member_specialization = 0;
2778 tree ctype = DECL_CLASS_CONTEXT (decl);
2779 tree dname = DECL_NAME (decl);
2780 tmpl_spec_kind tsk;
2781
2782 if (is_friend)
2783 {
2784 if (!processing_specialization)
2785 tsk = tsk_none;
2786 else
2787 tsk = tsk_excessive_parms;
2788 }
2789 else
2790 tsk = current_tmpl_spec_kind (template_count);
2791
2792 switch (tsk)
2793 {
2794 case tsk_none:
2795 if (processing_specialization && !VAR_P (decl))
2796 {
2797 specialization = 1;
2798 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2799 }
2800 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2801 {
2802 if (is_friend)
2803 /* This could be something like:
2804
2805 template <class T> void f(T);
2806 class S { friend void f<>(int); } */
2807 specialization = 1;
2808 else
2809 {
2810 /* This case handles bogus declarations like template <>
2811 template <class T> void f<int>(); */
2812
2813 error_at (cp_expr_loc_or_input_loc (declarator),
2814 "template-id %qE in declaration of primary template",
2815 declarator);
2816 return decl;
2817 }
2818 }
2819 break;
2820
2821 case tsk_invalid_member_spec:
2822 /* The error has already been reported in
2823 check_specialization_scope. */
2824 return error_mark_node;
2825
2826 case tsk_invalid_expl_inst:
2827 error ("template parameter list used in explicit instantiation");
2828
2829 /* Fall through. */
2830
2831 case tsk_expl_inst:
2832 if (have_def)
2833 error ("definition provided for explicit instantiation");
2834
2835 explicit_instantiation = 1;
2836 break;
2837
2838 case tsk_excessive_parms:
2839 case tsk_insufficient_parms:
2840 if (tsk == tsk_excessive_parms)
2841 error ("too many template parameter lists in declaration of %qD",
2842 decl);
2843 else if (template_header_count)
2844 error("too few template parameter lists in declaration of %qD", decl);
2845 else
2846 error("explicit specialization of %qD must be introduced by "
2847 "%<template <>%>", decl);
2848
2849 /* Fall through. */
2850 case tsk_expl_spec:
2851 if (is_concept)
2852 error ("explicit specialization declared %<concept%>");
2853
2854 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2855 /* In cases like template<> constexpr bool v = true;
2856 We'll give an error in check_template_variable. */
2857 break;
2858
2859 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2860 if (ctype)
2861 member_specialization = 1;
2862 else
2863 specialization = 1;
2864 break;
2865
2866 case tsk_template:
2867 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2868 {
2869 /* This case handles bogus declarations like template <>
2870 template <class T> void f<int>(); */
2871
2872 if (!uses_template_parms (TREE_OPERAND (declarator, 1)))
2873 error_at (cp_expr_loc_or_input_loc (declarator),
2874 "template-id %qE in declaration of primary template",
2875 declarator);
2876 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2877 {
2878 /* Partial specialization of variable template. */
2879 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2880 specialization = 1;
2881 goto ok;
2882 }
2883 else if (cxx_dialect < cxx14)
2884 error_at (cp_expr_loc_or_input_loc (declarator),
2885 "non-type partial specialization %qE "
2886 "is not allowed", declarator);
2887 else
2888 error_at (cp_expr_loc_or_input_loc (declarator),
2889 "non-class, non-variable partial specialization %qE "
2890 "is not allowed", declarator);
2891 return decl;
2892 ok:;
2893 }
2894
2895 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2896 /* This is a specialization of a member template, without
2897 specialization the containing class. Something like:
2898
2899 template <class T> struct S {
2900 template <class U> void f (U);
2901 };
2902 template <> template <class U> void S<int>::f(U) {}
2903
2904 That's a specialization -- but of the entire template. */
2905 specialization = 1;
2906 break;
2907
2908 default:
2909 gcc_unreachable ();
2910 }
2911
2912 if ((specialization || member_specialization)
2913 /* This doesn't apply to variable templates. */
2914 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl)))
2915 {
2916 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2917 for (; t; t = TREE_CHAIN (t))
2918 if (TREE_PURPOSE (t))
2919 {
2920 permerror (input_location,
2921 "default argument specified in explicit specialization");
2922 break;
2923 }
2924 }
2925
2926 if (specialization || member_specialization || explicit_instantiation)
2927 {
2928 tree tmpl = NULL_TREE;
2929 tree targs = NULL_TREE;
2930 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2931
2932 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2933 if (!was_template_id)
2934 {
2935 tree fns;
2936
2937 gcc_assert (identifier_p (declarator));
2938 if (ctype)
2939 fns = dname;
2940 else
2941 {
2942 /* If there is no class context, the explicit instantiation
2943 must be at namespace scope. */
2944 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2945
2946 /* Find the namespace binding, using the declaration
2947 context. */
2948 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2949 false, true);
2950 if (fns == error_mark_node)
2951 /* If lookup fails, look for a friend declaration so we can
2952 give a better diagnostic. */
2953 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2954 /*type*/false, /*complain*/true,
2955 /*hidden*/true);
2956
2957 if (fns == error_mark_node || !is_overloaded_fn (fns))
2958 {
2959 error ("%qD is not a template function", dname);
2960 fns = error_mark_node;
2961 }
2962 }
2963
2964 declarator = lookup_template_function (fns, NULL_TREE);
2965 }
2966
2967 if (declarator == error_mark_node)
2968 return error_mark_node;
2969
2970 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2971 {
2972 if (!explicit_instantiation)
2973 /* A specialization in class scope. This is invalid,
2974 but the error will already have been flagged by
2975 check_specialization_scope. */
2976 return error_mark_node;
2977 else
2978 {
2979 /* It's not valid to write an explicit instantiation in
2980 class scope, e.g.:
2981
2982 class C { template void f(); }
2983
2984 This case is caught by the parser. However, on
2985 something like:
2986
2987 template class C { void f(); };
2988
2989 (which is invalid) we can get here. The error will be
2990 issued later. */
2991 ;
2992 }
2993
2994 return decl;
2995 }
2996 else if (ctype != NULL_TREE
2997 && (identifier_p (TREE_OPERAND (declarator, 0))))
2998 {
2999 // We'll match variable templates in start_decl.
3000 if (VAR_P (decl))
3001 return decl;
3002
3003 /* Find the list of functions in ctype that have the same
3004 name as the declared function. */
3005 tree name = TREE_OPERAND (declarator, 0);
3006
3007 if (constructor_name_p (name, ctype))
3008 {
3009 if (DECL_CONSTRUCTOR_P (decl)
3010 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
3011 : !CLASSTYPE_DESTRUCTOR (ctype))
3012 {
3013 /* From [temp.expl.spec]:
3014
3015 If such an explicit specialization for the member
3016 of a class template names an implicitly-declared
3017 special member function (clause _special_), the
3018 program is ill-formed.
3019
3020 Similar language is found in [temp.explicit]. */
3021 error ("specialization of implicitly-declared special member function");
3022 return error_mark_node;
3023 }
3024
3025 name = DECL_NAME (decl);
3026 }
3027
3028 /* For a type-conversion operator, We might be looking for
3029 `operator int' which will be a specialization of
3030 `operator T'. Grab all the conversion operators, and
3031 then select from them. */
3032 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
3033 ? conv_op_identifier : name);
3034
3035 if (fns == NULL_TREE)
3036 {
3037 error ("no member function %qD declared in %qT", name, ctype);
3038 return error_mark_node;
3039 }
3040 else
3041 TREE_OPERAND (declarator, 0) = fns;
3042 }
3043
3044 /* Figure out what exactly is being specialized at this point.
3045 Note that for an explicit instantiation, even one for a
3046 member function, we cannot tell a priori whether the
3047 instantiation is for a member template, or just a member
3048 function of a template class. Even if a member template is
3049 being instantiated, the member template arguments may be
3050 elided if they can be deduced from the rest of the
3051 declaration. */
3052 tmpl = determine_specialization (declarator, decl,
3053 &targs,
3054 member_specialization,
3055 template_count,
3056 tsk);
3057
3058 if (!tmpl || tmpl == error_mark_node)
3059 /* We couldn't figure out what this declaration was
3060 specializing. */
3061 return error_mark_node;
3062 else
3063 {
3064 if (TREE_CODE (decl) == FUNCTION_DECL
3065 && DECL_HIDDEN_FRIEND_P (tmpl))
3066 {
3067 auto_diagnostic_group d;
3068 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3069 "friend declaration %qD is not visible to "
3070 "explicit specialization", tmpl))
3071 inform (DECL_SOURCE_LOCATION (tmpl),
3072 "friend declaration here");
3073 }
3074 else if (!ctype && !is_friend
3075 && CP_DECL_CONTEXT (decl) == current_namespace)
3076 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
3077
3078 tree gen_tmpl = most_general_template (tmpl);
3079
3080 if (explicit_instantiation)
3081 {
3082 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3083 is done by do_decl_instantiation later. */
3084
3085 int arg_depth = TMPL_ARGS_DEPTH (targs);
3086 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3087
3088 if (arg_depth > parm_depth)
3089 {
3090 /* If TMPL is not the most general template (for
3091 example, if TMPL is a friend template that is
3092 injected into namespace scope), then there will
3093 be too many levels of TARGS. Remove some of them
3094 here. */
3095 int i;
3096 tree new_targs;
3097
3098 new_targs = make_tree_vec (parm_depth);
3099 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3100 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3101 = TREE_VEC_ELT (targs, i);
3102 targs = new_targs;
3103 }
3104
3105 return instantiate_template (tmpl, targs, tf_error);
3106 }
3107
3108 /* If we thought that the DECL was a member function, but it
3109 turns out to be specializing a static member function,
3110 make DECL a static member function as well. */
3111 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3112 && DECL_STATIC_FUNCTION_P (tmpl)
3113 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3114 revert_static_member_fn (decl);
3115
3116 /* If this is a specialization of a member template of a
3117 template class, we want to return the TEMPLATE_DECL, not
3118 the specialization of it. */
3119 if (tsk == tsk_template && !was_template_id)
3120 {
3121 tree result = DECL_TEMPLATE_RESULT (tmpl);
3122 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3123 DECL_INITIAL (result) = NULL_TREE;
3124 if (have_def)
3125 {
3126 tree parm;
3127 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3128 DECL_SOURCE_LOCATION (result)
3129 = DECL_SOURCE_LOCATION (decl);
3130 /* We want to use the argument list specified in the
3131 definition, not in the original declaration. */
3132 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3133 for (parm = DECL_ARGUMENTS (result); parm;
3134 parm = DECL_CHAIN (parm))
3135 DECL_CONTEXT (parm) = result;
3136 }
3137 return register_specialization (tmpl, gen_tmpl, targs,
3138 is_friend, 0);
3139 }
3140
3141 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3142 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3143
3144 if (was_template_id)
3145 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3146
3147 /* Inherit default function arguments from the template
3148 DECL is specializing. */
3149 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3150 copy_default_args_to_explicit_spec (decl);
3151
3152 /* This specialization has the same protection as the
3153 template it specializes. */
3154 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3155 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3156
3157 /* 7.1.1-1 [dcl.stc]
3158
3159 A storage-class-specifier shall not be specified in an
3160 explicit specialization...
3161
3162 The parser rejects these, so unless action is taken here,
3163 explicit function specializations will always appear with
3164 global linkage.
3165
3166 The action recommended by the C++ CWG in response to C++
3167 defect report 605 is to make the storage class and linkage
3168 of the explicit specialization match the templated function:
3169
3170 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3171 */
3172 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3173 {
3174 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3175 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3176
3177 /* A concept cannot be specialized. */
3178 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3179 {
3180 error ("explicit specialization of function concept %qD",
3181 gen_tmpl);
3182 return error_mark_node;
3183 }
3184
3185 /* This specialization has the same linkage and visibility as
3186 the function template it specializes. */
3187 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3188 if (! TREE_PUBLIC (decl))
3189 {
3190 DECL_INTERFACE_KNOWN (decl) = 1;
3191 DECL_NOT_REALLY_EXTERN (decl) = 1;
3192 }
3193 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3194 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3195 {
3196 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3197 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3198 }
3199 }
3200
3201 /* If DECL is a friend declaration, declared using an
3202 unqualified name, the namespace associated with DECL may
3203 have been set incorrectly. For example, in:
3204
3205 template <typename T> void f(T);
3206 namespace N {
3207 struct S { friend void f<int>(int); }
3208 }
3209
3210 we will have set the DECL_CONTEXT for the friend
3211 declaration to N, rather than to the global namespace. */
3212 if (DECL_NAMESPACE_SCOPE_P (decl))
3213 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3214
3215 if (is_friend && !have_def)
3216 /* This is not really a declaration of a specialization.
3217 It's just the name of an instantiation. But, it's not
3218 a request for an instantiation, either. */
3219 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3220 else if (TREE_CODE (decl) == FUNCTION_DECL)
3221 /* A specialization is not necessarily COMDAT. */
3222 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3223 && DECL_DECLARED_INLINE_P (decl));
3224 else if (VAR_P (decl))
3225 DECL_COMDAT (decl) = false;
3226
3227 /* If this is a full specialization, register it so that we can find
3228 it again. Partial specializations will be registered in
3229 process_partial_specialization. */
3230 if (!processing_template_decl)
3231 {
3232 warn_spec_missing_attributes (gen_tmpl, decl, attrlist);
3233
3234 decl = register_specialization (decl, gen_tmpl, targs,
3235 is_friend, 0);
3236 }
3237
3238
3239 /* A 'structor should already have clones. */
3240 gcc_assert (decl == error_mark_node
3241 || variable_template_p (tmpl)
3242 || !(DECL_CONSTRUCTOR_P (decl)
3243 || DECL_DESTRUCTOR_P (decl))
3244 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3245 }
3246 }
3247
3248 return decl;
3249 }
3250
3251 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3252 parameters. These are represented in the same format used for
3253 DECL_TEMPLATE_PARMS. */
3254
3255 int
3256 comp_template_parms (const_tree parms1, const_tree parms2)
3257 {
3258 const_tree p1;
3259 const_tree p2;
3260
3261 if (parms1 == parms2)
3262 return 1;
3263
3264 for (p1 = parms1, p2 = parms2;
3265 p1 != NULL_TREE && p2 != NULL_TREE;
3266 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3267 {
3268 tree t1 = TREE_VALUE (p1);
3269 tree t2 = TREE_VALUE (p2);
3270 int i;
3271
3272 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3273 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3274
3275 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3276 return 0;
3277
3278 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3279 {
3280 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3281 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3282
3283 /* If either of the template parameters are invalid, assume
3284 they match for the sake of error recovery. */
3285 if (error_operand_p (parm1) || error_operand_p (parm2))
3286 return 1;
3287
3288 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3289 return 0;
3290
3291 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3292 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3293 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3294 continue;
3295 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3296 return 0;
3297 }
3298 }
3299
3300 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3301 /* One set of parameters has more parameters lists than the
3302 other. */
3303 return 0;
3304
3305 return 1;
3306 }
3307
3308 /* Determine whether PARM is a parameter pack. */
3309
3310 bool
3311 template_parameter_pack_p (const_tree parm)
3312 {
3313 /* Determine if we have a non-type template parameter pack. */
3314 if (TREE_CODE (parm) == PARM_DECL)
3315 return (DECL_TEMPLATE_PARM_P (parm)
3316 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3317 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3318 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3319
3320 /* If this is a list of template parameters, we could get a
3321 TYPE_DECL or a TEMPLATE_DECL. */
3322 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3323 parm = TREE_TYPE (parm);
3324
3325 /* Otherwise it must be a type template parameter. */
3326 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3327 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3328 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3329 }
3330
3331 /* Determine if T is a function parameter pack. */
3332
3333 bool
3334 function_parameter_pack_p (const_tree t)
3335 {
3336 if (t && TREE_CODE (t) == PARM_DECL)
3337 return DECL_PACK_P (t);
3338 return false;
3339 }
3340
3341 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3342 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3343
3344 tree
3345 get_function_template_decl (const_tree primary_func_tmpl_inst)
3346 {
3347 if (! primary_func_tmpl_inst
3348 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3349 || ! primary_template_specialization_p (primary_func_tmpl_inst))
3350 return NULL;
3351
3352 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3353 }
3354
3355 /* Return true iff the function parameter PARAM_DECL was expanded
3356 from the function parameter pack PACK. */
3357
3358 bool
3359 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3360 {
3361 if (DECL_ARTIFICIAL (param_decl)
3362 || !function_parameter_pack_p (pack))
3363 return false;
3364
3365 /* The parameter pack and its pack arguments have the same
3366 DECL_PARM_INDEX. */
3367 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3368 }
3369
3370 /* Determine whether ARGS describes a variadic template args list,
3371 i.e., one that is terminated by a template argument pack. */
3372
3373 static bool
3374 template_args_variadic_p (tree args)
3375 {
3376 int nargs;
3377 tree last_parm;
3378
3379 if (args == NULL_TREE)
3380 return false;
3381
3382 args = INNERMOST_TEMPLATE_ARGS (args);
3383 nargs = TREE_VEC_LENGTH (args);
3384
3385 if (nargs == 0)
3386 return false;
3387
3388 last_parm = TREE_VEC_ELT (args, nargs - 1);
3389
3390 return ARGUMENT_PACK_P (last_parm);
3391 }
3392
3393 /* Generate a new name for the parameter pack name NAME (an
3394 IDENTIFIER_NODE) that incorporates its */
3395
3396 static tree
3397 make_ith_pack_parameter_name (tree name, int i)
3398 {
3399 /* Munge the name to include the parameter index. */
3400 #define NUMBUF_LEN 128
3401 char numbuf[NUMBUF_LEN];
3402 char* newname;
3403 int newname_len;
3404
3405 if (name == NULL_TREE)
3406 return name;
3407 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3408 newname_len = IDENTIFIER_LENGTH (name)
3409 + strlen (numbuf) + 2;
3410 newname = (char*)alloca (newname_len);
3411 snprintf (newname, newname_len,
3412 "%s#%i", IDENTIFIER_POINTER (name), i);
3413 return get_identifier (newname);
3414 }
3415
3416 /* Return true if T is a primary function, class or alias template
3417 specialization, not including the template pattern. */
3418
3419 bool
3420 primary_template_specialization_p (const_tree t)
3421 {
3422 if (!t)
3423 return false;
3424
3425 if (TREE_CODE (t) == FUNCTION_DECL || VAR_P (t))
3426 return (DECL_LANG_SPECIFIC (t)
3427 && DECL_USE_TEMPLATE (t)
3428 && DECL_TEMPLATE_INFO (t)
3429 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
3430 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3431 return (CLASSTYPE_TEMPLATE_INFO (t)
3432 && CLASSTYPE_USE_TEMPLATE (t)
3433 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
3434 else if (alias_template_specialization_p (t))
3435 return true;
3436 return false;
3437 }
3438
3439 /* Return true if PARM is a template template parameter. */
3440
3441 bool
3442 template_template_parameter_p (const_tree parm)
3443 {
3444 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3445 }
3446
3447 /* Return true iff PARM is a DECL representing a type template
3448 parameter. */
3449
3450 bool
3451 template_type_parameter_p (const_tree parm)
3452 {
3453 return (parm
3454 && (TREE_CODE (parm) == TYPE_DECL
3455 || TREE_CODE (parm) == TEMPLATE_DECL)
3456 && DECL_TEMPLATE_PARM_P (parm));
3457 }
3458
3459 /* Return the template parameters of T if T is a
3460 primary template instantiation, NULL otherwise. */
3461
3462 tree
3463 get_primary_template_innermost_parameters (const_tree t)
3464 {
3465 tree parms = NULL, template_info = NULL;
3466
3467 if ((template_info = get_template_info (t))
3468 && primary_template_specialization_p (t))
3469 parms = INNERMOST_TEMPLATE_PARMS
3470 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3471
3472 return parms;
3473 }
3474
3475 /* Return the template parameters of the LEVELth level from the full list
3476 of template parameters PARMS. */
3477
3478 tree
3479 get_template_parms_at_level (tree parms, int level)
3480 {
3481 tree p;
3482 if (!parms
3483 || TREE_CODE (parms) != TREE_LIST
3484 || level > TMPL_PARMS_DEPTH (parms))
3485 return NULL_TREE;
3486
3487 for (p = parms; p; p = TREE_CHAIN (p))
3488 if (TMPL_PARMS_DEPTH (p) == level)
3489 return p;
3490
3491 return NULL_TREE;
3492 }
3493
3494 /* Returns the template arguments of T if T is a template instantiation,
3495 NULL otherwise. */
3496
3497 tree
3498 get_template_innermost_arguments (const_tree t)
3499 {
3500 tree args = NULL, template_info = NULL;
3501
3502 if ((template_info = get_template_info (t))
3503 && TI_ARGS (template_info))
3504 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3505
3506 return args;
3507 }
3508
3509 /* Return the argument pack elements of T if T is a template argument pack,
3510 NULL otherwise. */
3511
3512 tree
3513 get_template_argument_pack_elems (const_tree t)
3514 {
3515 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3516 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3517 return NULL;
3518
3519 return ARGUMENT_PACK_ARGS (t);
3520 }
3521
3522 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3523 ARGUMENT_PACK_SELECT represents. */
3524
3525 static tree
3526 argument_pack_select_arg (tree t)
3527 {
3528 tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
3529 tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
3530
3531 /* If the selected argument is an expansion E, that most likely means we were
3532 called from gen_elem_of_pack_expansion_instantiation during the
3533 substituting of an argument pack (of which the Ith element is a pack
3534 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3535 In this case, the Ith element resulting from this substituting is going to
3536 be a pack expansion, which pattern is the pattern of E. Let's return the
3537 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3538 resulting pack expansion from it. */
3539 if (PACK_EXPANSION_P (arg))
3540 {
3541 /* Make sure we aren't throwing away arg info. */
3542 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
3543 arg = PACK_EXPANSION_PATTERN (arg);
3544 }
3545
3546 return arg;
3547 }
3548
3549
3550 /* True iff FN is a function representing a built-in variadic parameter
3551 pack. */
3552
3553 bool
3554 builtin_pack_fn_p (tree fn)
3555 {
3556 if (!fn
3557 || TREE_CODE (fn) != FUNCTION_DECL
3558 || !DECL_IS_BUILTIN (fn))
3559 return false;
3560
3561 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3562 return true;
3563
3564 return false;
3565 }
3566
3567 /* True iff CALL is a call to a function representing a built-in variadic
3568 parameter pack. */
3569
3570 static bool
3571 builtin_pack_call_p (tree call)
3572 {
3573 if (TREE_CODE (call) != CALL_EXPR)
3574 return false;
3575 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3576 }
3577
3578 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3579
3580 static tree
3581 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3582 tree in_decl)
3583 {
3584 tree ohi = CALL_EXPR_ARG (call, 0);
3585 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3586 false/*fn*/, true/*int_cst*/);
3587
3588 if (value_dependent_expression_p (hi))
3589 {
3590 if (hi != ohi)
3591 {
3592 call = copy_node (call);
3593 CALL_EXPR_ARG (call, 0) = hi;
3594 }
3595 tree ex = make_pack_expansion (call, complain);
3596 tree vec = make_tree_vec (1);
3597 TREE_VEC_ELT (vec, 0) = ex;
3598 return vec;
3599 }
3600 else
3601 {
3602 hi = cxx_constant_value (hi);
3603 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3604
3605 /* Calculate the largest value of len that won't make the size of the vec
3606 overflow an int. The compiler will exceed resource limits long before
3607 this, but it seems a decent place to diagnose. */
3608 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3609
3610 if (len < 0 || len > max)
3611 {
3612 if ((complain & tf_error)
3613 && hi != error_mark_node)
3614 error ("argument to %<__integer_pack%> must be between 0 and %d",
3615 max);
3616 return error_mark_node;
3617 }
3618
3619 tree vec = make_tree_vec (len);
3620
3621 for (int i = 0; i < len; ++i)
3622 TREE_VEC_ELT (vec, i) = size_int (i);
3623
3624 return vec;
3625 }
3626 }
3627
3628 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3629 CALL. */
3630
3631 static tree
3632 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3633 tree in_decl)
3634 {
3635 if (!builtin_pack_call_p (call))
3636 return NULL_TREE;
3637
3638 tree fn = CALL_EXPR_FN (call);
3639
3640 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3641 return expand_integer_pack (call, args, complain, in_decl);
3642
3643 return NULL_TREE;
3644 }
3645
3646 /* Structure used to track the progress of find_parameter_packs_r. */
3647 struct find_parameter_pack_data
3648 {
3649 /* TREE_LIST that will contain all of the parameter packs found by
3650 the traversal. */
3651 tree* parameter_packs;
3652
3653 /* Set of AST nodes that have been visited by the traversal. */
3654 hash_set<tree> *visited;
3655
3656 /* True iff we're making a type pack expansion. */
3657 bool type_pack_expansion_p;
3658 };
3659
3660 /* Identifies all of the argument packs that occur in a template
3661 argument and appends them to the TREE_LIST inside DATA, which is a
3662 find_parameter_pack_data structure. This is a subroutine of
3663 make_pack_expansion and uses_parameter_packs. */
3664 static tree
3665 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3666 {
3667 tree t = *tp;
3668 struct find_parameter_pack_data* ppd =
3669 (struct find_parameter_pack_data*)data;
3670 bool parameter_pack_p = false;
3671
3672 /* Handle type aliases/typedefs. */
3673 if (TYPE_ALIAS_P (t))
3674 {
3675 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3676 cp_walk_tree (&TI_ARGS (tinfo),
3677 &find_parameter_packs_r,
3678 ppd, ppd->visited);
3679 *walk_subtrees = 0;
3680 return NULL_TREE;
3681 }
3682
3683 /* Identify whether this is a parameter pack or not. */
3684 switch (TREE_CODE (t))
3685 {
3686 case TEMPLATE_PARM_INDEX:
3687 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3688 parameter_pack_p = true;
3689 break;
3690
3691 case TEMPLATE_TYPE_PARM:
3692 t = TYPE_MAIN_VARIANT (t);
3693 /* FALLTHRU */
3694 case TEMPLATE_TEMPLATE_PARM:
3695 /* If the placeholder appears in the decl-specifier-seq of a function
3696 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3697 is a pack expansion, the invented template parameter is a template
3698 parameter pack. */
3699 if (ppd->type_pack_expansion_p && is_auto (t))
3700 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3701 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3702 parameter_pack_p = true;
3703 break;
3704
3705 case FIELD_DECL:
3706 case PARM_DECL:
3707 if (DECL_PACK_P (t))
3708 {
3709 /* We don't want to walk into the type of a PARM_DECL,
3710 because we don't want to see the type parameter pack. */
3711 *walk_subtrees = 0;
3712 parameter_pack_p = true;
3713 }
3714 break;
3715
3716 case VAR_DECL:
3717 if (DECL_PACK_P (t))
3718 {
3719 /* We don't want to walk into the type of a variadic capture proxy,
3720 because we don't want to see the type parameter pack. */
3721 *walk_subtrees = 0;
3722 parameter_pack_p = true;
3723 }
3724 else if (variable_template_specialization_p (t))
3725 {
3726 cp_walk_tree (&DECL_TI_ARGS (t),
3727 find_parameter_packs_r,
3728 ppd, ppd->visited);
3729 *walk_subtrees = 0;
3730 }
3731 break;
3732
3733 case CALL_EXPR:
3734 if (builtin_pack_call_p (t))
3735 parameter_pack_p = true;
3736 break;
3737
3738 case BASES:
3739 parameter_pack_p = true;
3740 break;
3741 default:
3742 /* Not a parameter pack. */
3743 break;
3744 }
3745
3746 if (parameter_pack_p)
3747 {
3748 /* Add this parameter pack to the list. */
3749 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3750 }
3751
3752 if (TYPE_P (t))
3753 cp_walk_tree (&TYPE_CONTEXT (t),
3754 &find_parameter_packs_r, ppd, ppd->visited);
3755
3756 /* This switch statement will return immediately if we don't find a
3757 parameter pack. */
3758 switch (TREE_CODE (t))
3759 {
3760 case TEMPLATE_PARM_INDEX:
3761 return NULL_TREE;
3762
3763 case BOUND_TEMPLATE_TEMPLATE_PARM:
3764 /* Check the template itself. */
3765 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3766 &find_parameter_packs_r, ppd, ppd->visited);
3767 /* Check the template arguments. */
3768 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3769 ppd->visited);
3770 *walk_subtrees = 0;
3771 return NULL_TREE;
3772
3773 case TEMPLATE_TYPE_PARM:
3774 case TEMPLATE_TEMPLATE_PARM:
3775 return NULL_TREE;
3776
3777 case PARM_DECL:
3778 return NULL_TREE;
3779
3780 case DECL_EXPR:
3781 /* Ignore the declaration of a capture proxy for a parameter pack. */
3782 if (is_capture_proxy (DECL_EXPR_DECL (t)))
3783 *walk_subtrees = 0;
3784 return NULL_TREE;
3785
3786 case RECORD_TYPE:
3787 if (TYPE_PTRMEMFUNC_P (t))
3788 return NULL_TREE;
3789 /* Fall through. */
3790
3791 case UNION_TYPE:
3792 case ENUMERAL_TYPE:
3793 if (TYPE_TEMPLATE_INFO (t))
3794 cp_walk_tree (&TYPE_TI_ARGS (t),
3795 &find_parameter_packs_r, ppd, ppd->visited);
3796
3797 *walk_subtrees = 0;
3798 return NULL_TREE;
3799
3800 case TEMPLATE_DECL:
3801 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3802 return NULL_TREE;
3803 gcc_fallthrough();
3804
3805 case CONSTRUCTOR:
3806 cp_walk_tree (&TREE_TYPE (t),
3807 &find_parameter_packs_r, ppd, ppd->visited);
3808 return NULL_TREE;
3809
3810 case TYPENAME_TYPE:
3811 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3812 ppd, ppd->visited);
3813 *walk_subtrees = 0;
3814 return NULL_TREE;
3815
3816 case TYPE_PACK_EXPANSION:
3817 case EXPR_PACK_EXPANSION:
3818 *walk_subtrees = 0;
3819 return NULL_TREE;
3820
3821 case INTEGER_TYPE:
3822 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3823 ppd, ppd->visited);
3824 *walk_subtrees = 0;
3825 return NULL_TREE;
3826
3827 case IDENTIFIER_NODE:
3828 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3829 ppd->visited);
3830 *walk_subtrees = 0;
3831 return NULL_TREE;
3832
3833 case LAMBDA_EXPR:
3834 {
3835 /* Look at explicit captures. */
3836 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t);
3837 cap; cap = TREE_CHAIN (cap))
3838 cp_walk_tree (&TREE_VALUE (cap), &find_parameter_packs_r, ppd,
3839 ppd->visited);
3840 /* Since we defer implicit capture, look in the parms and body. */
3841 tree fn = lambda_function (t);
3842 cp_walk_tree (&TREE_TYPE (fn), &find_parameter_packs_r, ppd,
3843 ppd->visited);
3844 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
3845 ppd->visited);
3846 *walk_subtrees = 0;
3847 return NULL_TREE;
3848 }
3849
3850 case DECLTYPE_TYPE:
3851 {
3852 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3853 type_pack_expansion_p to false so that any placeholders
3854 within the expression don't get marked as parameter packs. */
3855 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
3856 ppd->type_pack_expansion_p = false;
3857 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
3858 ppd, ppd->visited);
3859 ppd->type_pack_expansion_p = type_pack_expansion_p;
3860 *walk_subtrees = 0;
3861 return NULL_TREE;
3862 }
3863
3864 case IF_STMT:
3865 cp_walk_tree (&IF_COND (t), &find_parameter_packs_r,
3866 ppd, ppd->visited);
3867 cp_walk_tree (&THEN_CLAUSE (t), &find_parameter_packs_r,
3868 ppd, ppd->visited);
3869 cp_walk_tree (&ELSE_CLAUSE (t), &find_parameter_packs_r,
3870 ppd, ppd->visited);
3871 /* Don't walk into IF_STMT_EXTRA_ARGS. */
3872 *walk_subtrees = 0;
3873 return NULL_TREE;
3874
3875 default:
3876 return NULL_TREE;
3877 }
3878
3879 return NULL_TREE;
3880 }
3881
3882 /* Determines if the expression or type T uses any parameter packs. */
3883 tree
3884 uses_parameter_packs (tree t)
3885 {
3886 tree parameter_packs = NULL_TREE;
3887 struct find_parameter_pack_data ppd;
3888 ppd.parameter_packs = &parameter_packs;
3889 ppd.visited = new hash_set<tree>;
3890 ppd.type_pack_expansion_p = false;
3891 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3892 delete ppd.visited;
3893 return parameter_packs;
3894 }
3895
3896 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3897 representation a base-class initializer into a parameter pack
3898 expansion. If all goes well, the resulting node will be an
3899 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3900 respectively. */
3901 tree
3902 make_pack_expansion (tree arg, tsubst_flags_t complain)
3903 {
3904 tree result;
3905 tree parameter_packs = NULL_TREE;
3906 bool for_types = false;
3907 struct find_parameter_pack_data ppd;
3908
3909 if (!arg || arg == error_mark_node)
3910 return arg;
3911
3912 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
3913 {
3914 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3915 class initializer. In this case, the TREE_PURPOSE will be a
3916 _TYPE node (representing the base class expansion we're
3917 initializing) and the TREE_VALUE will be a TREE_LIST
3918 containing the initialization arguments.
3919
3920 The resulting expansion looks somewhat different from most
3921 expansions. Rather than returning just one _EXPANSION, we
3922 return a TREE_LIST whose TREE_PURPOSE is a
3923 TYPE_PACK_EXPANSION containing the bases that will be
3924 initialized. The TREE_VALUE will be identical to the
3925 original TREE_VALUE, which is a list of arguments that will
3926 be passed to each base. We do not introduce any new pack
3927 expansion nodes into the TREE_VALUE (although it is possible
3928 that some already exist), because the TREE_PURPOSE and
3929 TREE_VALUE all need to be expanded together with the same
3930 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3931 resulting TREE_PURPOSE will mention the parameter packs in
3932 both the bases and the arguments to the bases. */
3933 tree purpose;
3934 tree value;
3935 tree parameter_packs = NULL_TREE;
3936
3937 /* Determine which parameter packs will be used by the base
3938 class expansion. */
3939 ppd.visited = new hash_set<tree>;
3940 ppd.parameter_packs = &parameter_packs;
3941 ppd.type_pack_expansion_p = false;
3942 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
3943 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3944 &ppd, ppd.visited);
3945
3946 if (parameter_packs == NULL_TREE)
3947 {
3948 if (complain & tf_error)
3949 error ("base initializer expansion %qT contains no parameter packs",
3950 arg);
3951 delete ppd.visited;
3952 return error_mark_node;
3953 }
3954
3955 if (TREE_VALUE (arg) != void_type_node)
3956 {
3957 /* Collect the sets of parameter packs used in each of the
3958 initialization arguments. */
3959 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3960 {
3961 /* Determine which parameter packs will be expanded in this
3962 argument. */
3963 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3964 &ppd, ppd.visited);
3965 }
3966 }
3967
3968 delete ppd.visited;
3969
3970 /* Create the pack expansion type for the base type. */
3971 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3972 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3973 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3974 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
3975
3976 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3977 they will rarely be compared to anything. */
3978 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3979
3980 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3981 }
3982
3983 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3984 for_types = true;
3985
3986 /* Build the PACK_EXPANSION_* node. */
3987 result = for_types
3988 ? cxx_make_type (TYPE_PACK_EXPANSION)
3989 : make_node (EXPR_PACK_EXPANSION);
3990 SET_PACK_EXPANSION_PATTERN (result, arg);
3991 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3992 {
3993 /* Propagate type and const-expression information. */
3994 TREE_TYPE (result) = TREE_TYPE (arg);
3995 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3996 /* Mark this read now, since the expansion might be length 0. */
3997 mark_exp_read (arg);
3998 }
3999 else
4000 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4001 they will rarely be compared to anything. */
4002 SET_TYPE_STRUCTURAL_EQUALITY (result);
4003
4004 /* Determine which parameter packs will be expanded. */
4005 ppd.parameter_packs = &parameter_packs;
4006 ppd.visited = new hash_set<tree>;
4007 ppd.type_pack_expansion_p = TYPE_P (arg);
4008 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
4009 delete ppd.visited;
4010
4011 /* Make sure we found some parameter packs. */
4012 if (parameter_packs == NULL_TREE)
4013 {
4014 if (complain & tf_error)
4015 {
4016 if (TYPE_P (arg))
4017 error ("expansion pattern %qT contains no parameter packs", arg);
4018 else
4019 error ("expansion pattern %qE contains no parameter packs", arg);
4020 }
4021 return error_mark_node;
4022 }
4023 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
4024
4025 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
4026
4027 return result;
4028 }
4029
4030 /* Checks T for any "bare" parameter packs, which have not yet been
4031 expanded, and issues an error if any are found. This operation can
4032 only be done on full expressions or types (e.g., an expression
4033 statement, "if" condition, etc.), because we could have expressions like:
4034
4035 foo(f(g(h(args)))...)
4036
4037 where "args" is a parameter pack. check_for_bare_parameter_packs
4038 should not be called for the subexpressions args, h(args),
4039 g(h(args)), or f(g(h(args))), because we would produce erroneous
4040 error messages.
4041
4042 Returns TRUE and emits an error if there were bare parameter packs,
4043 returns FALSE otherwise. */
4044 bool
4045 check_for_bare_parameter_packs (tree t, location_t loc /* = UNKNOWN_LOCATION */)
4046 {
4047 tree parameter_packs = NULL_TREE;
4048 struct find_parameter_pack_data ppd;
4049
4050 if (!processing_template_decl || !t || t == error_mark_node)
4051 return false;
4052
4053 /* A lambda might use a parameter pack from the containing context. */
4054 if (current_class_type && LAMBDA_TYPE_P (current_class_type)
4055 && CLASSTYPE_TEMPLATE_INFO (current_class_type))
4056 return false;
4057
4058 if (TREE_CODE (t) == TYPE_DECL)
4059 t = TREE_TYPE (t);
4060
4061 ppd.parameter_packs = &parameter_packs;
4062 ppd.visited = new hash_set<tree>;
4063 ppd.type_pack_expansion_p = false;
4064 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4065 delete ppd.visited;
4066
4067 if (parameter_packs)
4068 {
4069 if (loc == UNKNOWN_LOCATION)
4070 loc = cp_expr_loc_or_input_loc (t);
4071 error_at (loc, "parameter packs not expanded with %<...%>:");
4072 while (parameter_packs)
4073 {
4074 tree pack = TREE_VALUE (parameter_packs);
4075 tree name = NULL_TREE;
4076
4077 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
4078 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
4079 name = TYPE_NAME (pack);
4080 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
4081 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
4082 else if (TREE_CODE (pack) == CALL_EXPR)
4083 name = DECL_NAME (CALL_EXPR_FN (pack));
4084 else
4085 name = DECL_NAME (pack);
4086
4087 if (name)
4088 inform (loc, " %qD", name);
4089 else
4090 inform (loc, " %s", "<anonymous>");
4091
4092 parameter_packs = TREE_CHAIN (parameter_packs);
4093 }
4094
4095 return true;
4096 }
4097
4098 return false;
4099 }
4100
4101 /* Expand any parameter packs that occur in the template arguments in
4102 ARGS. */
4103 tree
4104 expand_template_argument_pack (tree args)
4105 {
4106 if (args == error_mark_node)
4107 return error_mark_node;
4108
4109 tree result_args = NULL_TREE;
4110 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
4111 int num_result_args = -1;
4112 int non_default_args_count = -1;
4113
4114 /* First, determine if we need to expand anything, and the number of
4115 slots we'll need. */
4116 for (in_arg = 0; in_arg < nargs; ++in_arg)
4117 {
4118 tree arg = TREE_VEC_ELT (args, in_arg);
4119 if (arg == NULL_TREE)
4120 return args;
4121 if (ARGUMENT_PACK_P (arg))
4122 {
4123 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
4124 if (num_result_args < 0)
4125 num_result_args = in_arg + num_packed;
4126 else
4127 num_result_args += num_packed;
4128 }
4129 else
4130 {
4131 if (num_result_args >= 0)
4132 num_result_args++;
4133 }
4134 }
4135
4136 /* If no expansion is necessary, we're done. */
4137 if (num_result_args < 0)
4138 return args;
4139
4140 /* Expand arguments. */
4141 result_args = make_tree_vec (num_result_args);
4142 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
4143 non_default_args_count =
4144 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
4145 for (in_arg = 0; in_arg < nargs; ++in_arg)
4146 {
4147 tree arg = TREE_VEC_ELT (args, in_arg);
4148 if (ARGUMENT_PACK_P (arg))
4149 {
4150 tree packed = ARGUMENT_PACK_ARGS (arg);
4151 int i, num_packed = TREE_VEC_LENGTH (packed);
4152 for (i = 0; i < num_packed; ++i, ++out_arg)
4153 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
4154 if (non_default_args_count > 0)
4155 non_default_args_count += num_packed - 1;
4156 }
4157 else
4158 {
4159 TREE_VEC_ELT (result_args, out_arg) = arg;
4160 ++out_arg;
4161 }
4162 }
4163 if (non_default_args_count >= 0)
4164 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
4165 return result_args;
4166 }
4167
4168 /* Checks if DECL shadows a template parameter.
4169
4170 [temp.local]: A template-parameter shall not be redeclared within its
4171 scope (including nested scopes).
4172
4173 Emits an error and returns TRUE if the DECL shadows a parameter,
4174 returns FALSE otherwise. */
4175
4176 bool
4177 check_template_shadow (tree decl)
4178 {
4179 tree olddecl;
4180
4181 /* If we're not in a template, we can't possibly shadow a template
4182 parameter. */
4183 if (!current_template_parms)
4184 return true;
4185
4186 /* Figure out what we're shadowing. */
4187 decl = OVL_FIRST (decl);
4188 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4189
4190 /* If there's no previous binding for this name, we're not shadowing
4191 anything, let alone a template parameter. */
4192 if (!olddecl)
4193 return true;
4194
4195 /* If we're not shadowing a template parameter, we're done. Note
4196 that OLDDECL might be an OVERLOAD (or perhaps even an
4197 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4198 node. */
4199 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4200 return true;
4201
4202 /* We check for decl != olddecl to avoid bogus errors for using a
4203 name inside a class. We check TPFI to avoid duplicate errors for
4204 inline member templates. */
4205 if (decl == olddecl
4206 || (DECL_TEMPLATE_PARM_P (decl)
4207 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4208 return true;
4209
4210 /* Don't complain about the injected class name, as we've already
4211 complained about the class itself. */
4212 if (DECL_SELF_REFERENCE_P (decl))
4213 return false;
4214
4215 if (DECL_TEMPLATE_PARM_P (decl))
4216 error ("declaration of template parameter %q+D shadows "
4217 "template parameter", decl);
4218 else
4219 error ("declaration of %q+#D shadows template parameter", decl);
4220 inform (DECL_SOURCE_LOCATION (olddecl),
4221 "template parameter %qD declared here", olddecl);
4222 return false;
4223 }
4224
4225 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4226 ORIG_LEVEL, DECL, and TYPE. */
4227
4228 static tree
4229 build_template_parm_index (int index,
4230 int level,
4231 int orig_level,
4232 tree decl,
4233 tree type)
4234 {
4235 tree t = make_node (TEMPLATE_PARM_INDEX);
4236 TEMPLATE_PARM_IDX (t) = index;
4237 TEMPLATE_PARM_LEVEL (t) = level;
4238 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4239 TEMPLATE_PARM_DECL (t) = decl;
4240 TREE_TYPE (t) = type;
4241 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4242 TREE_READONLY (t) = TREE_READONLY (decl);
4243
4244 return t;
4245 }
4246
4247 /* Find the canonical type parameter for the given template type
4248 parameter. Returns the canonical type parameter, which may be TYPE
4249 if no such parameter existed. */
4250
4251 static tree
4252 canonical_type_parameter (tree type)
4253 {
4254 tree list;
4255 int idx = TEMPLATE_TYPE_IDX (type);
4256 if (!canonical_template_parms)
4257 vec_alloc (canonical_template_parms, idx + 1);
4258
4259 if (canonical_template_parms->length () <= (unsigned) idx)
4260 vec_safe_grow_cleared (canonical_template_parms, idx + 1);
4261
4262 list = (*canonical_template_parms)[idx];
4263 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4264 list = TREE_CHAIN (list);
4265
4266 if (list)
4267 return TREE_VALUE (list);
4268 else
4269 {
4270 (*canonical_template_parms)[idx]
4271 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4272 return type;
4273 }
4274 }
4275
4276 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4277 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4278 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4279 new one is created. */
4280
4281 static tree
4282 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4283 tsubst_flags_t complain)
4284 {
4285 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4286 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4287 != TEMPLATE_PARM_LEVEL (index) - levels)
4288 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4289 {
4290 tree orig_decl = TEMPLATE_PARM_DECL (index);
4291 tree decl, t;
4292
4293 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4294 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
4295 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4296 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4297 DECL_ARTIFICIAL (decl) = 1;
4298 SET_DECL_TEMPLATE_PARM_P (decl);
4299
4300 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4301 TEMPLATE_PARM_LEVEL (index) - levels,
4302 TEMPLATE_PARM_ORIG_LEVEL (index),
4303 decl, type);
4304 TEMPLATE_PARM_DESCENDANTS (index) = t;
4305 TEMPLATE_PARM_PARAMETER_PACK (t)
4306 = TEMPLATE_PARM_PARAMETER_PACK (index);
4307
4308 /* Template template parameters need this. */
4309 if (TREE_CODE (decl) == TEMPLATE_DECL)
4310 {
4311 DECL_TEMPLATE_RESULT (decl)
4312 = build_decl (DECL_SOURCE_LOCATION (decl),
4313 TYPE_DECL, DECL_NAME (decl), type);
4314 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl)) = true;
4315 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4316 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4317 }
4318 }
4319
4320 return TEMPLATE_PARM_DESCENDANTS (index);
4321 }
4322
4323 /* Process information from new template parameter PARM and append it
4324 to the LIST being built. This new parameter is a non-type
4325 parameter iff IS_NON_TYPE is true. This new parameter is a
4326 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4327 is in PARM_LOC. */
4328
4329 tree
4330 process_template_parm (tree list, location_t parm_loc, tree parm,
4331 bool is_non_type, bool is_parameter_pack)
4332 {
4333 tree decl = 0;
4334 int idx = 0;
4335
4336 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4337 tree defval = TREE_PURPOSE (parm);
4338 tree constr = TREE_TYPE (parm);
4339
4340 if (list)
4341 {
4342 tree p = tree_last (list);
4343
4344 if (p && TREE_VALUE (p) != error_mark_node)
4345 {
4346 p = TREE_VALUE (p);
4347 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4348 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4349 else
4350 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4351 }
4352
4353 ++idx;
4354 }
4355
4356 if (is_non_type)
4357 {
4358 parm = TREE_VALUE (parm);
4359
4360 SET_DECL_TEMPLATE_PARM_P (parm);
4361
4362 if (TREE_TYPE (parm) != error_mark_node)
4363 {
4364 /* [temp.param]
4365
4366 The top-level cv-qualifiers on the template-parameter are
4367 ignored when determining its type. */
4368 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4369 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4370 TREE_TYPE (parm) = error_mark_node;
4371 else if (uses_parameter_packs (TREE_TYPE (parm))
4372 && !is_parameter_pack
4373 /* If we're in a nested template parameter list, the template
4374 template parameter could be a parameter pack. */
4375 && processing_template_parmlist == 1)
4376 {
4377 /* This template parameter is not a parameter pack, but it
4378 should be. Complain about "bare" parameter packs. */
4379 check_for_bare_parameter_packs (TREE_TYPE (parm));
4380
4381 /* Recover by calling this a parameter pack. */
4382 is_parameter_pack = true;
4383 }
4384 }
4385
4386 /* A template parameter is not modifiable. */
4387 TREE_CONSTANT (parm) = 1;
4388 TREE_READONLY (parm) = 1;
4389 decl = build_decl (parm_loc,
4390 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4391 TREE_CONSTANT (decl) = 1;
4392 TREE_READONLY (decl) = 1;
4393 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4394 = build_template_parm_index (idx, processing_template_decl,
4395 processing_template_decl,
4396 decl, TREE_TYPE (parm));
4397
4398 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4399 = is_parameter_pack;
4400 }
4401 else
4402 {
4403 tree t;
4404 parm = TREE_VALUE (TREE_VALUE (parm));
4405
4406 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4407 {
4408 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4409 /* This is for distinguishing between real templates and template
4410 template parameters */
4411 TREE_TYPE (parm) = t;
4412 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4413 decl = parm;
4414 }
4415 else
4416 {
4417 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4418 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4419 decl = build_decl (parm_loc,
4420 TYPE_DECL, parm, t);
4421 }
4422
4423 TYPE_NAME (t) = decl;
4424 TYPE_STUB_DECL (t) = decl;
4425 parm = decl;
4426 TEMPLATE_TYPE_PARM_INDEX (t)
4427 = build_template_parm_index (idx, processing_template_decl,
4428 processing_template_decl,
4429 decl, TREE_TYPE (parm));
4430 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4431 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4432 }
4433 DECL_ARTIFICIAL (decl) = 1;
4434 SET_DECL_TEMPLATE_PARM_P (decl);
4435
4436 /* Build requirements for the type/template parameter.
4437 This must be done after SET_DECL_TEMPLATE_PARM_P or
4438 process_template_parm could fail. */
4439 tree reqs = finish_shorthand_constraint (parm, constr);
4440
4441 decl = pushdecl (decl);
4442 if (!is_non_type)
4443 parm = decl;
4444
4445 /* Build the parameter node linking the parameter declaration,
4446 its default argument (if any), and its constraints (if any). */
4447 parm = build_tree_list (defval, parm);
4448 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4449
4450 return chainon (list, parm);
4451 }
4452
4453 /* The end of a template parameter list has been reached. Process the
4454 tree list into a parameter vector, converting each parameter into a more
4455 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4456 as PARM_DECLs. */
4457
4458 tree
4459 end_template_parm_list (tree parms)
4460 {
4461 int nparms;
4462 tree parm, next;
4463 tree saved_parmlist = make_tree_vec (list_length (parms));
4464
4465 /* Pop the dummy parameter level and add the real one. */
4466 current_template_parms = TREE_CHAIN (current_template_parms);
4467
4468 current_template_parms
4469 = tree_cons (size_int (processing_template_decl),
4470 saved_parmlist, current_template_parms);
4471
4472 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4473 {
4474 next = TREE_CHAIN (parm);
4475 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4476 TREE_CHAIN (parm) = NULL_TREE;
4477 }
4478
4479 --processing_template_parmlist;
4480
4481 return saved_parmlist;
4482 }
4483
4484 // Explicitly indicate the end of the template parameter list. We assume
4485 // that the current template parameters have been constructed and/or
4486 // managed explicitly, as when creating new template template parameters
4487 // from a shorthand constraint.
4488 void
4489 end_template_parm_list ()
4490 {
4491 --processing_template_parmlist;
4492 }
4493
4494 /* end_template_decl is called after a template declaration is seen. */
4495
4496 void
4497 end_template_decl (void)
4498 {
4499 reset_specialization ();
4500
4501 if (! processing_template_decl)
4502 return;
4503
4504 /* This matches the pushlevel in begin_template_parm_list. */
4505 finish_scope ();
4506
4507 --processing_template_decl;
4508 current_template_parms = TREE_CHAIN (current_template_parms);
4509 }
4510
4511 /* Takes a TREE_LIST representing a template parameter and convert it
4512 into an argument suitable to be passed to the type substitution
4513 functions. Note that If the TREE_LIST contains an error_mark
4514 node, the returned argument is error_mark_node. */
4515
4516 tree
4517 template_parm_to_arg (tree t)
4518 {
4519
4520 if (t == NULL_TREE
4521 || TREE_CODE (t) != TREE_LIST)
4522 return t;
4523
4524 if (error_operand_p (TREE_VALUE (t)))
4525 return error_mark_node;
4526
4527 t = TREE_VALUE (t);
4528
4529 if (TREE_CODE (t) == TYPE_DECL
4530 || TREE_CODE (t) == TEMPLATE_DECL)
4531 {
4532 t = TREE_TYPE (t);
4533
4534 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4535 {
4536 /* Turn this argument into a TYPE_ARGUMENT_PACK
4537 with a single element, which expands T. */
4538 tree vec = make_tree_vec (1);
4539 if (CHECKING_P)
4540 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4541
4542 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4543
4544 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4545 SET_ARGUMENT_PACK_ARGS (t, vec);
4546 }
4547 }
4548 else
4549 {
4550 t = DECL_INITIAL (t);
4551
4552 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4553 {
4554 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4555 with a single element, which expands T. */
4556 tree vec = make_tree_vec (1);
4557 if (CHECKING_P)
4558 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4559
4560 t = convert_from_reference (t);
4561 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4562
4563 t = make_node (NONTYPE_ARGUMENT_PACK);
4564 SET_ARGUMENT_PACK_ARGS (t, vec);
4565 }
4566 else
4567 t = convert_from_reference (t);
4568 }
4569 return t;
4570 }
4571
4572 /* Given a single level of template parameters (a TREE_VEC), return it
4573 as a set of template arguments. */
4574
4575 static tree
4576 template_parms_level_to_args (tree parms)
4577 {
4578 tree a = copy_node (parms);
4579 TREE_TYPE (a) = NULL_TREE;
4580 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4581 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4582
4583 if (CHECKING_P)
4584 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4585
4586 return a;
4587 }
4588
4589 /* Given a set of template parameters, return them as a set of template
4590 arguments. The template parameters are represented as a TREE_VEC, in
4591 the form documented in cp-tree.h for template arguments. */
4592
4593 static tree
4594 template_parms_to_args (tree parms)
4595 {
4596 tree header;
4597 tree args = NULL_TREE;
4598 int length = TMPL_PARMS_DEPTH (parms);
4599 int l = length;
4600
4601 /* If there is only one level of template parameters, we do not
4602 create a TREE_VEC of TREE_VECs. Instead, we return a single
4603 TREE_VEC containing the arguments. */
4604 if (length > 1)
4605 args = make_tree_vec (length);
4606
4607 for (header = parms; header; header = TREE_CHAIN (header))
4608 {
4609 tree a = template_parms_level_to_args (TREE_VALUE (header));
4610
4611 if (length > 1)
4612 TREE_VEC_ELT (args, --l) = a;
4613 else
4614 args = a;
4615 }
4616
4617 return args;
4618 }
4619
4620 /* Within the declaration of a template, return the currently active
4621 template parameters as an argument TREE_VEC. */
4622
4623 static tree
4624 current_template_args (void)
4625 {
4626 return template_parms_to_args (current_template_parms);
4627 }
4628
4629 /* Update the declared TYPE by doing any lookups which were thought to be
4630 dependent, but are not now that we know the SCOPE of the declarator. */
4631
4632 tree
4633 maybe_update_decl_type (tree orig_type, tree scope)
4634 {
4635 tree type = orig_type;
4636
4637 if (type == NULL_TREE)
4638 return type;
4639
4640 if (TREE_CODE (orig_type) == TYPE_DECL)
4641 type = TREE_TYPE (type);
4642
4643 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4644 && dependent_type_p (type)
4645 /* Don't bother building up the args in this case. */
4646 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4647 {
4648 /* tsubst in the args corresponding to the template parameters,
4649 including auto if present. Most things will be unchanged, but
4650 make_typename_type and tsubst_qualified_id will resolve
4651 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4652 tree args = current_template_args ();
4653 tree auto_node = type_uses_auto (type);
4654 tree pushed;
4655 if (auto_node)
4656 {
4657 tree auto_vec = make_tree_vec (1);
4658 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4659 args = add_to_template_args (args, auto_vec);
4660 }
4661 pushed = push_scope (scope);
4662 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4663 if (pushed)
4664 pop_scope (scope);
4665 }
4666
4667 if (type == error_mark_node)
4668 return orig_type;
4669
4670 if (TREE_CODE (orig_type) == TYPE_DECL)
4671 {
4672 if (same_type_p (type, TREE_TYPE (orig_type)))
4673 type = orig_type;
4674 else
4675 type = TYPE_NAME (type);
4676 }
4677 return type;
4678 }
4679
4680 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4681 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4682 the new template is a member template. */
4683
4684 static tree
4685 build_template_decl (tree decl, tree parms, bool member_template_p)
4686 {
4687 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4688 SET_DECL_LANGUAGE (tmpl, DECL_LANGUAGE (decl));
4689 DECL_TEMPLATE_PARMS (tmpl) = parms;
4690 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4691 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4692 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4693
4694 return tmpl;
4695 }
4696
4697 struct template_parm_data
4698 {
4699 /* The level of the template parameters we are currently
4700 processing. */
4701 int level;
4702
4703 /* The index of the specialization argument we are currently
4704 processing. */
4705 int current_arg;
4706
4707 /* An array whose size is the number of template parameters. The
4708 elements are nonzero if the parameter has been used in any one
4709 of the arguments processed so far. */
4710 int* parms;
4711
4712 /* An array whose size is the number of template arguments. The
4713 elements are nonzero if the argument makes use of template
4714 parameters of this level. */
4715 int* arg_uses_template_parms;
4716 };
4717
4718 /* Subroutine of push_template_decl used to see if each template
4719 parameter in a partial specialization is used in the explicit
4720 argument list. If T is of the LEVEL given in DATA (which is
4721 treated as a template_parm_data*), then DATA->PARMS is marked
4722 appropriately. */
4723
4724 static int
4725 mark_template_parm (tree t, void* data)
4726 {
4727 int level;
4728 int idx;
4729 struct template_parm_data* tpd = (struct template_parm_data*) data;
4730
4731 template_parm_level_and_index (t, &level, &idx);
4732
4733 if (level == tpd->level)
4734 {
4735 tpd->parms[idx] = 1;
4736 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4737 }
4738
4739 /* In C++17 the type of a non-type argument is a deduced context. */
4740 if (cxx_dialect >= cxx17
4741 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4742 for_each_template_parm (TREE_TYPE (t),
4743 &mark_template_parm,
4744 data,
4745 NULL,
4746 /*include_nondeduced_p=*/false);
4747
4748 /* Return zero so that for_each_template_parm will continue the
4749 traversal of the tree; we want to mark *every* template parm. */
4750 return 0;
4751 }
4752
4753 /* Process the partial specialization DECL. */
4754
4755 static tree
4756 process_partial_specialization (tree decl)
4757 {
4758 tree type = TREE_TYPE (decl);
4759 tree tinfo = get_template_info (decl);
4760 tree maintmpl = TI_TEMPLATE (tinfo);
4761 tree specargs = TI_ARGS (tinfo);
4762 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4763 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4764 tree inner_parms;
4765 tree inst;
4766 int nargs = TREE_VEC_LENGTH (inner_args);
4767 int ntparms;
4768 int i;
4769 bool did_error_intro = false;
4770 struct template_parm_data tpd;
4771 struct template_parm_data tpd2;
4772
4773 gcc_assert (current_template_parms);
4774
4775 /* A concept cannot be specialized. */
4776 if (flag_concepts && variable_concept_p (maintmpl))
4777 {
4778 error ("specialization of variable concept %q#D", maintmpl);
4779 return error_mark_node;
4780 }
4781
4782 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4783 ntparms = TREE_VEC_LENGTH (inner_parms);
4784
4785 /* We check that each of the template parameters given in the
4786 partial specialization is used in the argument list to the
4787 specialization. For example:
4788
4789 template <class T> struct S;
4790 template <class T> struct S<T*>;
4791
4792 The second declaration is OK because `T*' uses the template
4793 parameter T, whereas
4794
4795 template <class T> struct S<int>;
4796
4797 is no good. Even trickier is:
4798
4799 template <class T>
4800 struct S1
4801 {
4802 template <class U>
4803 struct S2;
4804 template <class U>
4805 struct S2<T>;
4806 };
4807
4808 The S2<T> declaration is actually invalid; it is a
4809 full-specialization. Of course,
4810
4811 template <class U>
4812 struct S2<T (*)(U)>;
4813
4814 or some such would have been OK. */
4815 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4816 tpd.parms = XALLOCAVEC (int, ntparms);
4817 memset (tpd.parms, 0, sizeof (int) * ntparms);
4818
4819 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4820 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4821 for (i = 0; i < nargs; ++i)
4822 {
4823 tpd.current_arg = i;
4824 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4825 &mark_template_parm,
4826 &tpd,
4827 NULL,
4828 /*include_nondeduced_p=*/false);
4829 }
4830 for (i = 0; i < ntparms; ++i)
4831 if (tpd.parms[i] == 0)
4832 {
4833 /* One of the template parms was not used in a deduced context in the
4834 specialization. */
4835 if (!did_error_intro)
4836 {
4837 error ("template parameters not deducible in "
4838 "partial specialization:");
4839 did_error_intro = true;
4840 }
4841
4842 inform (input_location, " %qD",
4843 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4844 }
4845
4846 if (did_error_intro)
4847 return error_mark_node;
4848
4849 /* [temp.class.spec]
4850
4851 The argument list of the specialization shall not be identical to
4852 the implicit argument list of the primary template. */
4853 tree main_args
4854 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4855 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
4856 && (!flag_concepts
4857 || !strictly_subsumes (current_template_constraints (),
4858 get_constraints (maintmpl))))
4859 {
4860 if (!flag_concepts)
4861 error ("partial specialization %q+D does not specialize "
4862 "any template arguments; to define the primary template, "
4863 "remove the template argument list", decl);
4864 else
4865 error ("partial specialization %q+D does not specialize any "
4866 "template arguments and is not more constrained than "
4867 "the primary template; to define the primary template, "
4868 "remove the template argument list", decl);
4869 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4870 }
4871
4872 /* A partial specialization that replaces multiple parameters of the
4873 primary template with a pack expansion is less specialized for those
4874 parameters. */
4875 if (nargs < DECL_NTPARMS (maintmpl))
4876 {
4877 error ("partial specialization is not more specialized than the "
4878 "primary template because it replaces multiple parameters "
4879 "with a pack expansion");
4880 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4881 /* Avoid crash in process_partial_specialization. */
4882 return decl;
4883 }
4884
4885 /* If we aren't in a dependent class, we can actually try deduction. */
4886 else if (tpd.level == 1
4887 /* FIXME we should be able to handle a partial specialization of a
4888 partial instantiation, but currently we can't (c++/41727). */
4889 && TMPL_ARGS_DEPTH (specargs) == 1
4890 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
4891 {
4892 auto_diagnostic_group d;
4893 if (permerror (input_location, "partial specialization %qD is not "
4894 "more specialized than", decl))
4895 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
4896 maintmpl);
4897 }
4898
4899 /* [temp.class.spec]
4900
4901 A partially specialized non-type argument expression shall not
4902 involve template parameters of the partial specialization except
4903 when the argument expression is a simple identifier.
4904
4905 The type of a template parameter corresponding to a specialized
4906 non-type argument shall not be dependent on a parameter of the
4907 specialization.
4908
4909 Also, we verify that pack expansions only occur at the
4910 end of the argument list. */
4911 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4912 tpd2.parms = 0;
4913 for (i = 0; i < nargs; ++i)
4914 {
4915 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4916 tree arg = TREE_VEC_ELT (inner_args, i);
4917 tree packed_args = NULL_TREE;
4918 int j, len = 1;
4919
4920 if (ARGUMENT_PACK_P (arg))
4921 {
4922 /* Extract the arguments from the argument pack. We'll be
4923 iterating over these in the following loop. */
4924 packed_args = ARGUMENT_PACK_ARGS (arg);
4925 len = TREE_VEC_LENGTH (packed_args);
4926 }
4927
4928 for (j = 0; j < len; j++)
4929 {
4930 if (packed_args)
4931 /* Get the Jth argument in the parameter pack. */
4932 arg = TREE_VEC_ELT (packed_args, j);
4933
4934 if (PACK_EXPANSION_P (arg))
4935 {
4936 /* Pack expansions must come at the end of the
4937 argument list. */
4938 if ((packed_args && j < len - 1)
4939 || (!packed_args && i < nargs - 1))
4940 {
4941 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4942 error ("parameter pack argument %qE must be at the "
4943 "end of the template argument list", arg);
4944 else
4945 error ("parameter pack argument %qT must be at the "
4946 "end of the template argument list", arg);
4947 }
4948 }
4949
4950 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4951 /* We only care about the pattern. */
4952 arg = PACK_EXPANSION_PATTERN (arg);
4953
4954 if (/* These first two lines are the `non-type' bit. */
4955 !TYPE_P (arg)
4956 && TREE_CODE (arg) != TEMPLATE_DECL
4957 /* This next two lines are the `argument expression is not just a
4958 simple identifier' condition and also the `specialized
4959 non-type argument' bit. */
4960 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4961 && !((REFERENCE_REF_P (arg)
4962 || TREE_CODE (arg) == VIEW_CONVERT_EXPR)
4963 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4964 {
4965 if ((!packed_args && tpd.arg_uses_template_parms[i])
4966 || (packed_args && uses_template_parms (arg)))
4967 error_at (cp_expr_loc_or_input_loc (arg),
4968 "template argument %qE involves template "
4969 "parameter(s)", arg);
4970 else
4971 {
4972 /* Look at the corresponding template parameter,
4973 marking which template parameters its type depends
4974 upon. */
4975 tree type = TREE_TYPE (parm);
4976
4977 if (!tpd2.parms)
4978 {
4979 /* We haven't yet initialized TPD2. Do so now. */
4980 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4981 /* The number of parameters here is the number in the
4982 main template, which, as checked in the assertion
4983 above, is NARGS. */
4984 tpd2.parms = XALLOCAVEC (int, nargs);
4985 tpd2.level =
4986 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4987 }
4988
4989 /* Mark the template parameters. But this time, we're
4990 looking for the template parameters of the main
4991 template, not in the specialization. */
4992 tpd2.current_arg = i;
4993 tpd2.arg_uses_template_parms[i] = 0;
4994 memset (tpd2.parms, 0, sizeof (int) * nargs);
4995 for_each_template_parm (type,
4996 &mark_template_parm,
4997 &tpd2,
4998 NULL,
4999 /*include_nondeduced_p=*/false);
5000
5001 if (tpd2.arg_uses_template_parms [i])
5002 {
5003 /* The type depended on some template parameters.
5004 If they are fully specialized in the
5005 specialization, that's OK. */
5006 int j;
5007 int count = 0;
5008 for (j = 0; j < nargs; ++j)
5009 if (tpd2.parms[j] != 0
5010 && tpd.arg_uses_template_parms [j])
5011 ++count;
5012 if (count != 0)
5013 error_n (input_location, count,
5014 "type %qT of template argument %qE depends "
5015 "on a template parameter",
5016 "type %qT of template argument %qE depends "
5017 "on template parameters",
5018 type,
5019 arg);
5020 }
5021 }
5022 }
5023 }
5024 }
5025
5026 /* We should only get here once. */
5027 if (TREE_CODE (decl) == TYPE_DECL)
5028 gcc_assert (!COMPLETE_TYPE_P (type));
5029
5030 // Build the template decl.
5031 tree tmpl = build_template_decl (decl, current_template_parms,
5032 DECL_MEMBER_TEMPLATE_P (maintmpl));
5033 TREE_TYPE (tmpl) = type;
5034 DECL_TEMPLATE_RESULT (tmpl) = decl;
5035 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5036 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
5037 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
5038
5039 /* Give template template parms a DECL_CONTEXT of the template
5040 for which they are a parameter. */
5041 for (i = 0; i < ntparms; ++i)
5042 {
5043 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
5044 if (TREE_CODE (parm) == TEMPLATE_DECL)
5045 DECL_CONTEXT (parm) = tmpl;
5046 }
5047
5048 if (VAR_P (decl))
5049 /* We didn't register this in check_explicit_specialization so we could
5050 wait until the constraints were set. */
5051 decl = register_specialization (decl, maintmpl, specargs, false, 0);
5052 else
5053 associate_classtype_constraints (type);
5054
5055 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
5056 = tree_cons (specargs, tmpl,
5057 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
5058 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
5059
5060 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
5061 inst = TREE_CHAIN (inst))
5062 {
5063 tree instance = TREE_VALUE (inst);
5064 if (TYPE_P (instance)
5065 ? (COMPLETE_TYPE_P (instance)
5066 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
5067 : DECL_TEMPLATE_INSTANTIATION (instance))
5068 {
5069 tree spec = most_specialized_partial_spec (instance, tf_none);
5070 tree inst_decl = (DECL_P (instance)
5071 ? instance : TYPE_NAME (instance));
5072 if (!spec)
5073 /* OK */;
5074 else if (spec == error_mark_node)
5075 permerror (input_location,
5076 "declaration of %qD ambiguates earlier template "
5077 "instantiation for %qD", decl, inst_decl);
5078 else if (TREE_VALUE (spec) == tmpl)
5079 permerror (input_location,
5080 "partial specialization of %qD after instantiation "
5081 "of %qD", decl, inst_decl);
5082 }
5083 }
5084
5085 return decl;
5086 }
5087
5088 /* PARM is a template parameter of some form; return the corresponding
5089 TEMPLATE_PARM_INDEX. */
5090
5091 static tree
5092 get_template_parm_index (tree parm)
5093 {
5094 if (TREE_CODE (parm) == PARM_DECL
5095 || TREE_CODE (parm) == CONST_DECL)
5096 parm = DECL_INITIAL (parm);
5097 else if (TREE_CODE (parm) == TYPE_DECL
5098 || TREE_CODE (parm) == TEMPLATE_DECL)
5099 parm = TREE_TYPE (parm);
5100 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
5101 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
5102 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
5103 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
5104 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
5105 return parm;
5106 }
5107
5108 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5109 parameter packs used by the template parameter PARM. */
5110
5111 static void
5112 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
5113 {
5114 /* A type parm can't refer to another parm. */
5115 if (TREE_CODE (parm) == TYPE_DECL || parm == error_mark_node)
5116 return;
5117 else if (TREE_CODE (parm) == PARM_DECL)
5118 {
5119 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
5120 ppd, ppd->visited);
5121 return;
5122 }
5123
5124 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
5125
5126 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
5127 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
5128 {
5129 tree p = TREE_VALUE (TREE_VEC_ELT (vec, i));
5130 if (template_parameter_pack_p (p))
5131 /* Any packs in the type are expanded by this parameter. */;
5132 else
5133 fixed_parameter_pack_p_1 (p, ppd);
5134 }
5135 }
5136
5137 /* PARM is a template parameter pack. Return any parameter packs used in
5138 its type or the type of any of its template parameters. If there are
5139 any such packs, it will be instantiated into a fixed template parameter
5140 list by partial instantiation rather than be fully deduced. */
5141
5142 tree
5143 fixed_parameter_pack_p (tree parm)
5144 {
5145 /* This can only be true in a member template. */
5146 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
5147 return NULL_TREE;
5148 /* This can only be true for a parameter pack. */
5149 if (!template_parameter_pack_p (parm))
5150 return NULL_TREE;
5151 /* A type parm can't refer to another parm. */
5152 if (TREE_CODE (parm) == TYPE_DECL)
5153 return NULL_TREE;
5154
5155 tree parameter_packs = NULL_TREE;
5156 struct find_parameter_pack_data ppd;
5157 ppd.parameter_packs = &parameter_packs;
5158 ppd.visited = new hash_set<tree>;
5159 ppd.type_pack_expansion_p = false;
5160
5161 fixed_parameter_pack_p_1 (parm, &ppd);
5162
5163 delete ppd.visited;
5164 return parameter_packs;
5165 }
5166
5167 /* Check that a template declaration's use of default arguments and
5168 parameter packs is not invalid. Here, PARMS are the template
5169 parameters. IS_PRIMARY is true if DECL is the thing declared by
5170 a primary template. IS_PARTIAL is true if DECL is a partial
5171 specialization.
5172
5173 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5174 function template declaration or a friend class template
5175 declaration. In the function case, 1 indicates a declaration, 2
5176 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5177 emitted for extraneous default arguments.
5178
5179 Returns TRUE if there were no errors found, FALSE otherwise. */
5180
5181 bool
5182 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
5183 bool is_partial, int is_friend_decl)
5184 {
5185 const char *msg;
5186 int last_level_to_check;
5187 tree parm_level;
5188 bool no_errors = true;
5189
5190 /* [temp.param]
5191
5192 A default template-argument shall not be specified in a
5193 function template declaration or a function template definition, nor
5194 in the template-parameter-list of the definition of a member of a
5195 class template. */
5196
5197 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5198 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
5199 /* You can't have a function template declaration in a local
5200 scope, nor you can you define a member of a class template in a
5201 local scope. */
5202 return true;
5203
5204 if ((TREE_CODE (decl) == TYPE_DECL
5205 && TREE_TYPE (decl)
5206 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5207 || (TREE_CODE (decl) == FUNCTION_DECL
5208 && LAMBDA_FUNCTION_P (decl)))
5209 /* A lambda doesn't have an explicit declaration; don't complain
5210 about the parms of the enclosing class. */
5211 return true;
5212
5213 if (current_class_type
5214 && !TYPE_BEING_DEFINED (current_class_type)
5215 && DECL_LANG_SPECIFIC (decl)
5216 && DECL_DECLARES_FUNCTION_P (decl)
5217 /* If this is either a friend defined in the scope of the class
5218 or a member function. */
5219 && (DECL_FUNCTION_MEMBER_P (decl)
5220 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5221 : DECL_FRIEND_CONTEXT (decl)
5222 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5223 : false)
5224 /* And, if it was a member function, it really was defined in
5225 the scope of the class. */
5226 && (!DECL_FUNCTION_MEMBER_P (decl)
5227 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5228 /* We already checked these parameters when the template was
5229 declared, so there's no need to do it again now. This function
5230 was defined in class scope, but we're processing its body now
5231 that the class is complete. */
5232 return true;
5233
5234 /* Core issue 226 (C++0x only): the following only applies to class
5235 templates. */
5236 if (is_primary
5237 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5238 {
5239 /* [temp.param]
5240
5241 If a template-parameter has a default template-argument, all
5242 subsequent template-parameters shall have a default
5243 template-argument supplied. */
5244 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5245 {
5246 tree inner_parms = TREE_VALUE (parm_level);
5247 int ntparms = TREE_VEC_LENGTH (inner_parms);
5248 int seen_def_arg_p = 0;
5249 int i;
5250
5251 for (i = 0; i < ntparms; ++i)
5252 {
5253 tree parm = TREE_VEC_ELT (inner_parms, i);
5254
5255 if (parm == error_mark_node)
5256 continue;
5257
5258 if (TREE_PURPOSE (parm))
5259 seen_def_arg_p = 1;
5260 else if (seen_def_arg_p
5261 && !template_parameter_pack_p (TREE_VALUE (parm)))
5262 {
5263 error ("no default argument for %qD", TREE_VALUE (parm));
5264 /* For better subsequent error-recovery, we indicate that
5265 there should have been a default argument. */
5266 TREE_PURPOSE (parm) = error_mark_node;
5267 no_errors = false;
5268 }
5269 else if (!is_partial
5270 && !is_friend_decl
5271 /* Don't complain about an enclosing partial
5272 specialization. */
5273 && parm_level == parms
5274 && TREE_CODE (decl) == TYPE_DECL
5275 && i < ntparms - 1
5276 && template_parameter_pack_p (TREE_VALUE (parm))
5277 /* A fixed parameter pack will be partially
5278 instantiated into a fixed length list. */
5279 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5280 {
5281 /* A primary class template can only have one
5282 parameter pack, at the end of the template
5283 parameter list. */
5284
5285 error ("parameter pack %q+D must be at the end of the"
5286 " template parameter list", TREE_VALUE (parm));
5287
5288 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5289 = error_mark_node;
5290 no_errors = false;
5291 }
5292 }
5293 }
5294 }
5295
5296 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5297 || is_partial
5298 || !is_primary
5299 || is_friend_decl)
5300 /* For an ordinary class template, default template arguments are
5301 allowed at the innermost level, e.g.:
5302 template <class T = int>
5303 struct S {};
5304 but, in a partial specialization, they're not allowed even
5305 there, as we have in [temp.class.spec]:
5306
5307 The template parameter list of a specialization shall not
5308 contain default template argument values.
5309
5310 So, for a partial specialization, or for a function template
5311 (in C++98/C++03), we look at all of them. */
5312 ;
5313 else
5314 /* But, for a primary class template that is not a partial
5315 specialization we look at all template parameters except the
5316 innermost ones. */
5317 parms = TREE_CHAIN (parms);
5318
5319 /* Figure out what error message to issue. */
5320 if (is_friend_decl == 2)
5321 msg = G_("default template arguments may not be used in function template "
5322 "friend re-declaration");
5323 else if (is_friend_decl)
5324 msg = G_("default template arguments may not be used in template "
5325 "friend declarations");
5326 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5327 msg = G_("default template arguments may not be used in function templates "
5328 "without %<-std=c++11%> or %<-std=gnu++11%>");
5329 else if (is_partial)
5330 msg = G_("default template arguments may not be used in "
5331 "partial specializations");
5332 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5333 msg = G_("default argument for template parameter for class enclosing %qD");
5334 else
5335 /* Per [temp.param]/9, "A default template-argument shall not be
5336 specified in the template-parameter-lists of the definition of
5337 a member of a class template that appears outside of the member's
5338 class.", thus if we aren't handling a member of a class template
5339 there is no need to examine the parameters. */
5340 return true;
5341
5342 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5343 /* If we're inside a class definition, there's no need to
5344 examine the parameters to the class itself. On the one
5345 hand, they will be checked when the class is defined, and,
5346 on the other, default arguments are valid in things like:
5347 template <class T = double>
5348 struct S { template <class U> void f(U); };
5349 Here the default argument for `S' has no bearing on the
5350 declaration of `f'. */
5351 last_level_to_check = template_class_depth (current_class_type) + 1;
5352 else
5353 /* Check everything. */
5354 last_level_to_check = 0;
5355
5356 for (parm_level = parms;
5357 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5358 parm_level = TREE_CHAIN (parm_level))
5359 {
5360 tree inner_parms = TREE_VALUE (parm_level);
5361 int i;
5362 int ntparms;
5363
5364 ntparms = TREE_VEC_LENGTH (inner_parms);
5365 for (i = 0; i < ntparms; ++i)
5366 {
5367 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5368 continue;
5369
5370 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5371 {
5372 if (msg)
5373 {
5374 no_errors = false;
5375 if (is_friend_decl == 2)
5376 return no_errors;
5377
5378 error (msg, decl);
5379 msg = 0;
5380 }
5381
5382 /* Clear out the default argument so that we are not
5383 confused later. */
5384 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5385 }
5386 }
5387
5388 /* At this point, if we're still interested in issuing messages,
5389 they must apply to classes surrounding the object declared. */
5390 if (msg)
5391 msg = G_("default argument for template parameter for class "
5392 "enclosing %qD");
5393 }
5394
5395 return no_errors;
5396 }
5397
5398 /* Worker for push_template_decl_real, called via
5399 for_each_template_parm. DATA is really an int, indicating the
5400 level of the parameters we are interested in. If T is a template
5401 parameter of that level, return nonzero. */
5402
5403 static int
5404 template_parm_this_level_p (tree t, void* data)
5405 {
5406 int this_level = *(int *)data;
5407 int level;
5408
5409 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5410 level = TEMPLATE_PARM_LEVEL (t);
5411 else
5412 level = TEMPLATE_TYPE_LEVEL (t);
5413 return level == this_level;
5414 }
5415
5416 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5417 DATA is really an int, indicating the innermost outer level of parameters.
5418 If T is a template parameter of that level or further out, return
5419 nonzero. */
5420
5421 static int
5422 template_parm_outer_level (tree t, void *data)
5423 {
5424 int this_level = *(int *)data;
5425 int level;
5426
5427 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5428 level = TEMPLATE_PARM_LEVEL (t);
5429 else
5430 level = TEMPLATE_TYPE_LEVEL (t);
5431 return level <= this_level;
5432 }
5433
5434 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5435 parameters given by current_template_args, or reuses a
5436 previously existing one, if appropriate. Returns the DECL, or an
5437 equivalent one, if it is replaced via a call to duplicate_decls.
5438
5439 If IS_FRIEND is true, DECL is a friend declaration. */
5440
5441 tree
5442 push_template_decl_real (tree decl, bool is_friend)
5443 {
5444 tree tmpl;
5445 tree args;
5446 tree info;
5447 tree ctx;
5448 bool is_primary;
5449 bool is_partial;
5450 int new_template_p = 0;
5451 /* True if the template is a member template, in the sense of
5452 [temp.mem]. */
5453 bool member_template_p = false;
5454
5455 if (decl == error_mark_node || !current_template_parms)
5456 return error_mark_node;
5457
5458 /* See if this is a partial specialization. */
5459 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5460 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5461 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5462 || (VAR_P (decl)
5463 && DECL_LANG_SPECIFIC (decl)
5464 && DECL_TEMPLATE_SPECIALIZATION (decl)
5465 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5466
5467 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5468 is_friend = true;
5469
5470 if (is_friend)
5471 /* For a friend, we want the context of the friend, not
5472 the type of which it is a friend. */
5473 ctx = CP_DECL_CONTEXT (decl);
5474 else if (CP_DECL_CONTEXT (decl)
5475 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5476 /* In the case of a virtual function, we want the class in which
5477 it is defined. */
5478 ctx = CP_DECL_CONTEXT (decl);
5479 else
5480 /* Otherwise, if we're currently defining some class, the DECL
5481 is assumed to be a member of the class. */
5482 ctx = current_scope ();
5483
5484 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5485 ctx = NULL_TREE;
5486
5487 if (!DECL_CONTEXT (decl))
5488 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5489
5490 /* See if this is a primary template. */
5491 if (is_friend && ctx
5492 && uses_template_parms_level (ctx, processing_template_decl))
5493 /* A friend template that specifies a class context, i.e.
5494 template <typename T> friend void A<T>::f();
5495 is not primary. */
5496 is_primary = false;
5497 else if (TREE_CODE (decl) == TYPE_DECL
5498 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5499 is_primary = false;
5500 else
5501 is_primary = template_parm_scope_p ();
5502
5503 if (is_primary)
5504 {
5505 warning (OPT_Wtemplates, "template %qD declared", decl);
5506
5507 if (DECL_CLASS_SCOPE_P (decl))
5508 member_template_p = true;
5509 if (TREE_CODE (decl) == TYPE_DECL
5510 && IDENTIFIER_ANON_P (DECL_NAME (decl)))
5511 {
5512 error ("template class without a name");
5513 return error_mark_node;
5514 }
5515 else if (TREE_CODE (decl) == FUNCTION_DECL)
5516 {
5517 if (member_template_p)
5518 {
5519 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5520 error ("member template %qD may not have virt-specifiers", decl);
5521 }
5522 if (DECL_DESTRUCTOR_P (decl))
5523 {
5524 /* [temp.mem]
5525
5526 A destructor shall not be a member template. */
5527 error_at (DECL_SOURCE_LOCATION (decl),
5528 "destructor %qD declared as member template", decl);
5529 return error_mark_node;
5530 }
5531 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5532 && (!prototype_p (TREE_TYPE (decl))
5533 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5534 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5535 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5536 == void_list_node)))
5537 {
5538 /* [basic.stc.dynamic.allocation]
5539
5540 An allocation function can be a function
5541 template. ... Template allocation functions shall
5542 have two or more parameters. */
5543 error ("invalid template declaration of %qD", decl);
5544 return error_mark_node;
5545 }
5546 }
5547 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5548 && CLASS_TYPE_P (TREE_TYPE (decl)))
5549 {
5550 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5551 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5552 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5553 {
5554 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5555 if (TREE_CODE (t) == TYPE_DECL)
5556 t = TREE_TYPE (t);
5557 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5558 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5559 }
5560 }
5561 else if (TREE_CODE (decl) == TYPE_DECL
5562 && TYPE_DECL_ALIAS_P (decl))
5563 /* alias-declaration */
5564 gcc_assert (!DECL_ARTIFICIAL (decl));
5565 else if (VAR_P (decl))
5566 /* C++14 variable template. */;
5567 else
5568 {
5569 error ("template declaration of %q#D", decl);
5570 return error_mark_node;
5571 }
5572 }
5573
5574 /* Check to see that the rules regarding the use of default
5575 arguments are not being violated. We check args for a friend
5576 functions when we know whether it's a definition, introducing
5577 declaration or re-declaration. */
5578 if (!is_friend || TREE_CODE (decl) != FUNCTION_DECL)
5579 check_default_tmpl_args (decl, current_template_parms,
5580 is_primary, is_partial, is_friend);
5581
5582 /* Ensure that there are no parameter packs in the type of this
5583 declaration that have not been expanded. */
5584 if (TREE_CODE (decl) == FUNCTION_DECL)
5585 {
5586 /* Check each of the arguments individually to see if there are
5587 any bare parameter packs. */
5588 tree type = TREE_TYPE (decl);
5589 tree arg = DECL_ARGUMENTS (decl);
5590 tree argtype = TYPE_ARG_TYPES (type);
5591
5592 while (arg && argtype)
5593 {
5594 if (!DECL_PACK_P (arg)
5595 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5596 {
5597 /* This is a PARM_DECL that contains unexpanded parameter
5598 packs. We have already complained about this in the
5599 check_for_bare_parameter_packs call, so just replace
5600 these types with ERROR_MARK_NODE. */
5601 TREE_TYPE (arg) = error_mark_node;
5602 TREE_VALUE (argtype) = error_mark_node;
5603 }
5604
5605 arg = DECL_CHAIN (arg);
5606 argtype = TREE_CHAIN (argtype);
5607 }
5608
5609 /* Check for bare parameter packs in the return type and the
5610 exception specifiers. */
5611 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5612 /* Errors were already issued, set return type to int
5613 as the frontend doesn't expect error_mark_node as
5614 the return type. */
5615 TREE_TYPE (type) = integer_type_node;
5616 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5617 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5618 }
5619 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5620 && TYPE_DECL_ALIAS_P (decl))
5621 ? DECL_ORIGINAL_TYPE (decl)
5622 : TREE_TYPE (decl)))
5623 {
5624 TREE_TYPE (decl) = error_mark_node;
5625 return error_mark_node;
5626 }
5627
5628 if (is_partial)
5629 return process_partial_specialization (decl);
5630
5631 args = current_template_args ();
5632
5633 if (!ctx
5634 || TREE_CODE (ctx) == FUNCTION_DECL
5635 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5636 || (TREE_CODE (decl) == TYPE_DECL
5637 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5638 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5639 {
5640 if (DECL_LANG_SPECIFIC (decl)
5641 && DECL_TEMPLATE_INFO (decl)
5642 && DECL_TI_TEMPLATE (decl))
5643 tmpl = DECL_TI_TEMPLATE (decl);
5644 /* If DECL is a TYPE_DECL for a class-template, then there won't
5645 be DECL_LANG_SPECIFIC. The information equivalent to
5646 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5647 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5648 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5649 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5650 {
5651 /* Since a template declaration already existed for this
5652 class-type, we must be redeclaring it here. Make sure
5653 that the redeclaration is valid. */
5654 redeclare_class_template (TREE_TYPE (decl),
5655 current_template_parms,
5656 current_template_constraints ());
5657 /* We don't need to create a new TEMPLATE_DECL; just use the
5658 one we already had. */
5659 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5660 }
5661 else
5662 {
5663 tmpl = build_template_decl (decl, current_template_parms,
5664 member_template_p);
5665 new_template_p = 1;
5666
5667 if (DECL_LANG_SPECIFIC (decl)
5668 && DECL_TEMPLATE_SPECIALIZATION (decl))
5669 {
5670 /* A specialization of a member template of a template
5671 class. */
5672 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5673 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5674 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5675 }
5676 }
5677 }
5678 else
5679 {
5680 tree a, t, current, parms;
5681 int i;
5682 tree tinfo = get_template_info (decl);
5683
5684 if (!tinfo)
5685 {
5686 error ("template definition of non-template %q#D", decl);
5687 return error_mark_node;
5688 }
5689
5690 tmpl = TI_TEMPLATE (tinfo);
5691
5692 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5693 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5694 && DECL_TEMPLATE_SPECIALIZATION (decl)
5695 && DECL_MEMBER_TEMPLATE_P (tmpl))
5696 {
5697 tree new_tmpl;
5698
5699 /* The declaration is a specialization of a member
5700 template, declared outside the class. Therefore, the
5701 innermost template arguments will be NULL, so we
5702 replace them with the arguments determined by the
5703 earlier call to check_explicit_specialization. */
5704 args = DECL_TI_ARGS (decl);
5705
5706 new_tmpl
5707 = build_template_decl (decl, current_template_parms,
5708 member_template_p);
5709 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5710 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5711 DECL_TI_TEMPLATE (decl) = new_tmpl;
5712 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5713 DECL_TEMPLATE_INFO (new_tmpl)
5714 = build_template_info (tmpl, args);
5715
5716 register_specialization (new_tmpl,
5717 most_general_template (tmpl),
5718 args,
5719 is_friend, 0);
5720 return decl;
5721 }
5722
5723 /* Make sure the template headers we got make sense. */
5724
5725 parms = DECL_TEMPLATE_PARMS (tmpl);
5726 i = TMPL_PARMS_DEPTH (parms);
5727 if (TMPL_ARGS_DEPTH (args) != i)
5728 {
5729 error ("expected %d levels of template parms for %q#D, got %d",
5730 i, decl, TMPL_ARGS_DEPTH (args));
5731 DECL_INTERFACE_KNOWN (decl) = 1;
5732 return error_mark_node;
5733 }
5734 else
5735 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5736 {
5737 a = TMPL_ARGS_LEVEL (args, i);
5738 t = INNERMOST_TEMPLATE_PARMS (parms);
5739
5740 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5741 {
5742 if (current == decl)
5743 error ("got %d template parameters for %q#D",
5744 TREE_VEC_LENGTH (a), decl);
5745 else
5746 error ("got %d template parameters for %q#T",
5747 TREE_VEC_LENGTH (a), current);
5748 error (" but %d required", TREE_VEC_LENGTH (t));
5749 /* Avoid crash in import_export_decl. */
5750 DECL_INTERFACE_KNOWN (decl) = 1;
5751 return error_mark_node;
5752 }
5753
5754 if (current == decl)
5755 current = ctx;
5756 else if (current == NULL_TREE)
5757 /* Can happen in erroneous input. */
5758 break;
5759 else
5760 current = get_containing_scope (current);
5761 }
5762
5763 /* Check that the parms are used in the appropriate qualifying scopes
5764 in the declarator. */
5765 if (!comp_template_args
5766 (TI_ARGS (tinfo),
5767 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5768 {
5769 error ("template arguments to %qD do not match original "
5770 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
5771 if (!uses_template_parms (TI_ARGS (tinfo)))
5772 inform (input_location, "use %<template<>%> for"
5773 " an explicit specialization");
5774 /* Avoid crash in import_export_decl. */
5775 DECL_INTERFACE_KNOWN (decl) = 1;
5776 return error_mark_node;
5777 }
5778 }
5779
5780 DECL_TEMPLATE_RESULT (tmpl) = decl;
5781 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5782
5783 /* Push template declarations for global functions and types. Note
5784 that we do not try to push a global template friend declared in a
5785 template class; such a thing may well depend on the template
5786 parameters of the class. */
5787 if (new_template_p && !ctx
5788 && !(is_friend && template_class_depth (current_class_type) > 0))
5789 {
5790 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5791 if (tmpl == error_mark_node)
5792 return error_mark_node;
5793
5794 /* Hide template friend classes that haven't been declared yet. */
5795 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5796 {
5797 DECL_ANTICIPATED (tmpl) = 1;
5798 DECL_FRIEND_P (tmpl) = 1;
5799 }
5800 }
5801
5802 if (is_primary)
5803 {
5804 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5805
5806 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5807
5808 /* Give template template parms a DECL_CONTEXT of the template
5809 for which they are a parameter. */
5810 parms = INNERMOST_TEMPLATE_PARMS (parms);
5811 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5812 {
5813 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5814 if (TREE_CODE (parm) == TEMPLATE_DECL)
5815 DECL_CONTEXT (parm) = tmpl;
5816 }
5817
5818 if (TREE_CODE (decl) == TYPE_DECL
5819 && TYPE_DECL_ALIAS_P (decl)
5820 && complex_alias_template_p (tmpl))
5821 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5822 }
5823
5824 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5825 back to its most general template. If TMPL is a specialization,
5826 ARGS may only have the innermost set of arguments. Add the missing
5827 argument levels if necessary. */
5828 if (DECL_TEMPLATE_INFO (tmpl))
5829 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5830
5831 info = build_template_info (tmpl, args);
5832
5833 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5834 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5835 else
5836 {
5837 if (is_primary)
5838 retrofit_lang_decl (decl);
5839 if (DECL_LANG_SPECIFIC (decl))
5840 DECL_TEMPLATE_INFO (decl) = info;
5841 }
5842
5843 if (flag_implicit_templates
5844 && !is_friend
5845 && TREE_PUBLIC (decl)
5846 && VAR_OR_FUNCTION_DECL_P (decl))
5847 /* Set DECL_COMDAT on template instantiations; if we force
5848 them to be emitted by explicit instantiation,
5849 mark_needed will tell cgraph to do the right thing. */
5850 DECL_COMDAT (decl) = true;
5851
5852 return DECL_TEMPLATE_RESULT (tmpl);
5853 }
5854
5855 tree
5856 push_template_decl (tree decl)
5857 {
5858 return push_template_decl_real (decl, false);
5859 }
5860
5861 /* FN is an inheriting constructor that inherits from the constructor
5862 template INHERITED; turn FN into a constructor template with a matching
5863 template header. */
5864
5865 tree
5866 add_inherited_template_parms (tree fn, tree inherited)
5867 {
5868 tree inner_parms
5869 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5870 inner_parms = copy_node (inner_parms);
5871 tree parms
5872 = tree_cons (size_int (processing_template_decl + 1),
5873 inner_parms, current_template_parms);
5874 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5875 tree args = template_parms_to_args (parms);
5876 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5877 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5878 DECL_TEMPLATE_RESULT (tmpl) = fn;
5879 DECL_ARTIFICIAL (tmpl) = true;
5880 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5881 return tmpl;
5882 }
5883
5884 /* Called when a class template TYPE is redeclared with the indicated
5885 template PARMS, e.g.:
5886
5887 template <class T> struct S;
5888 template <class T> struct S {}; */
5889
5890 bool
5891 redeclare_class_template (tree type, tree parms, tree cons)
5892 {
5893 tree tmpl;
5894 tree tmpl_parms;
5895 int i;
5896
5897 if (!TYPE_TEMPLATE_INFO (type))
5898 {
5899 error ("%qT is not a template type", type);
5900 return false;
5901 }
5902
5903 tmpl = TYPE_TI_TEMPLATE (type);
5904 if (!PRIMARY_TEMPLATE_P (tmpl))
5905 /* The type is nested in some template class. Nothing to worry
5906 about here; there are no new template parameters for the nested
5907 type. */
5908 return true;
5909
5910 if (!parms)
5911 {
5912 error ("template specifiers not specified in declaration of %qD",
5913 tmpl);
5914 return false;
5915 }
5916
5917 parms = INNERMOST_TEMPLATE_PARMS (parms);
5918 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5919
5920 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5921 {
5922 error_n (input_location, TREE_VEC_LENGTH (parms),
5923 "redeclared with %d template parameter",
5924 "redeclared with %d template parameters",
5925 TREE_VEC_LENGTH (parms));
5926 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5927 "previous declaration %qD used %d template parameter",
5928 "previous declaration %qD used %d template parameters",
5929 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5930 return false;
5931 }
5932
5933 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5934 {
5935 tree tmpl_parm;
5936 tree parm;
5937 tree tmpl_default;
5938 tree parm_default;
5939
5940 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5941 || TREE_VEC_ELT (parms, i) == error_mark_node)
5942 continue;
5943
5944 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5945 if (error_operand_p (tmpl_parm))
5946 return false;
5947
5948 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5949 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5950 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5951
5952 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5953 TEMPLATE_DECL. */
5954 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5955 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5956 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5957 || (TREE_CODE (tmpl_parm) != PARM_DECL
5958 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5959 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5960 || (TREE_CODE (tmpl_parm) == PARM_DECL
5961 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5962 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5963 {
5964 error ("template parameter %q+#D", tmpl_parm);
5965 error ("redeclared here as %q#D", parm);
5966 return false;
5967 }
5968
5969 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5970 {
5971 /* We have in [temp.param]:
5972
5973 A template-parameter may not be given default arguments
5974 by two different declarations in the same scope. */
5975 error_at (input_location, "redefinition of default argument for %q#D", parm);
5976 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5977 "original definition appeared here");
5978 return false;
5979 }
5980
5981 if (parm_default != NULL_TREE)
5982 /* Update the previous template parameters (which are the ones
5983 that will really count) with the new default value. */
5984 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5985 else if (tmpl_default != NULL_TREE)
5986 /* Update the new parameters, too; they'll be used as the
5987 parameters for any members. */
5988 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5989
5990 /* Give each template template parm in this redeclaration a
5991 DECL_CONTEXT of the template for which they are a parameter. */
5992 if (TREE_CODE (parm) == TEMPLATE_DECL)
5993 {
5994 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5995 DECL_CONTEXT (parm) = tmpl;
5996 }
5997
5998 if (TREE_CODE (parm) == TYPE_DECL)
5999 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
6000 }
6001
6002 // Cannot redeclare a class template with a different set of constraints.
6003 if (!equivalent_constraints (get_constraints (tmpl), cons))
6004 {
6005 error_at (input_location, "redeclaration %q#D with different "
6006 "constraints", tmpl);
6007 inform (DECL_SOURCE_LOCATION (tmpl),
6008 "original declaration appeared here");
6009 }
6010
6011 return true;
6012 }
6013
6014 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
6015 to be used when the caller has already checked
6016 (processing_template_decl
6017 && !instantiation_dependent_expression_p (expr)
6018 && potential_constant_expression (expr))
6019 and cleared processing_template_decl. */
6020
6021 tree
6022 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
6023 {
6024 return tsubst_copy_and_build (expr,
6025 /*args=*/NULL_TREE,
6026 complain,
6027 /*in_decl=*/NULL_TREE,
6028 /*function_p=*/false,
6029 /*integral_constant_expression_p=*/true);
6030 }
6031
6032 /* Simplify EXPR if it is a non-dependent expression. Returns the
6033 (possibly simplified) expression. */
6034
6035 tree
6036 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
6037 {
6038 if (expr == NULL_TREE)
6039 return NULL_TREE;
6040
6041 /* If we're in a template, but EXPR isn't value dependent, simplify
6042 it. We're supposed to treat:
6043
6044 template <typename T> void f(T[1 + 1]);
6045 template <typename T> void f(T[2]);
6046
6047 as two declarations of the same function, for example. */
6048 if (processing_template_decl
6049 && is_nondependent_constant_expression (expr))
6050 {
6051 processing_template_decl_sentinel s;
6052 expr = instantiate_non_dependent_expr_internal (expr, complain);
6053 }
6054 return expr;
6055 }
6056
6057 tree
6058 instantiate_non_dependent_expr (tree expr)
6059 {
6060 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
6061 }
6062
6063 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6064 an uninstantiated expression. */
6065
6066 tree
6067 instantiate_non_dependent_or_null (tree expr)
6068 {
6069 if (expr == NULL_TREE)
6070 return NULL_TREE;
6071 if (processing_template_decl)
6072 {
6073 if (!is_nondependent_constant_expression (expr))
6074 expr = NULL_TREE;
6075 else
6076 {
6077 processing_template_decl_sentinel s;
6078 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
6079 }
6080 }
6081 return expr;
6082 }
6083
6084 /* True iff T is a specialization of a variable template. */
6085
6086 bool
6087 variable_template_specialization_p (tree t)
6088 {
6089 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
6090 return false;
6091 tree tmpl = DECL_TI_TEMPLATE (t);
6092 return variable_template_p (tmpl);
6093 }
6094
6095 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6096 template declaration, or a TYPE_DECL for an alias declaration. */
6097
6098 bool
6099 alias_type_or_template_p (tree t)
6100 {
6101 if (t == NULL_TREE)
6102 return false;
6103 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
6104 || (TYPE_P (t)
6105 && TYPE_NAME (t)
6106 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
6107 || DECL_ALIAS_TEMPLATE_P (t));
6108 }
6109
6110 /* Return TRUE iff T is a specialization of an alias template. */
6111
6112 bool
6113 alias_template_specialization_p (const_tree t)
6114 {
6115 /* It's an alias template specialization if it's an alias and its
6116 TYPE_NAME is a specialization of a primary template. */
6117 if (TYPE_ALIAS_P (t))
6118 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
6119 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo));
6120
6121 return false;
6122 }
6123
6124 /* An alias template is complex from a SFINAE perspective if a template-id
6125 using that alias can be ill-formed when the expansion is not, as with
6126 the void_t template. We determine this by checking whether the
6127 expansion for the alias template uses all its template parameters. */
6128
6129 struct uses_all_template_parms_data
6130 {
6131 int level;
6132 bool *seen;
6133 };
6134
6135 static int
6136 uses_all_template_parms_r (tree t, void *data_)
6137 {
6138 struct uses_all_template_parms_data &data
6139 = *(struct uses_all_template_parms_data*)data_;
6140 tree idx = get_template_parm_index (t);
6141
6142 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
6143 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
6144 return 0;
6145 }
6146
6147 static bool
6148 complex_alias_template_p (const_tree tmpl)
6149 {
6150 struct uses_all_template_parms_data data;
6151 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6152 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6153 data.level = TMPL_PARMS_DEPTH (parms);
6154 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
6155 data.seen = XALLOCAVEC (bool, len);
6156 for (int i = 0; i < len; ++i)
6157 data.seen[i] = false;
6158
6159 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
6160 for (int i = 0; i < len; ++i)
6161 if (!data.seen[i])
6162 return true;
6163 return false;
6164 }
6165
6166 /* Return TRUE iff T is a specialization of a complex alias template with
6167 dependent template-arguments. */
6168
6169 bool
6170 dependent_alias_template_spec_p (const_tree t)
6171 {
6172 if (!alias_template_specialization_p (t))
6173 return false;
6174
6175 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
6176 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo)))
6177 return false;
6178
6179 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
6180 if (!any_dependent_template_arguments_p (args))
6181 return false;
6182
6183 return true;
6184 }
6185
6186 /* Return the number of innermost template parameters in TMPL. */
6187
6188 static int
6189 num_innermost_template_parms (tree tmpl)
6190 {
6191 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6192 return TREE_VEC_LENGTH (parms);
6193 }
6194
6195 /* Return either TMPL or another template that it is equivalent to under DR
6196 1286: An alias that just changes the name of a template is equivalent to
6197 the other template. */
6198
6199 static tree
6200 get_underlying_template (tree tmpl)
6201 {
6202 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6203 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6204 {
6205 /* Determine if the alias is equivalent to an underlying template. */
6206 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6207 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6208 if (!tinfo)
6209 break;
6210
6211 tree underlying = TI_TEMPLATE (tinfo);
6212 if (!PRIMARY_TEMPLATE_P (underlying)
6213 || (num_innermost_template_parms (tmpl)
6214 != num_innermost_template_parms (underlying)))
6215 break;
6216
6217 tree alias_args = INNERMOST_TEMPLATE_ARGS
6218 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
6219 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6220 break;
6221
6222 /* Alias is equivalent. Strip it and repeat. */
6223 tmpl = underlying;
6224 }
6225
6226 return tmpl;
6227 }
6228
6229 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6230 must be a reference-to-function or a pointer-to-function type, as specified
6231 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6232 and check that the resulting function has external linkage. */
6233
6234 static tree
6235 convert_nontype_argument_function (tree type, tree expr,
6236 tsubst_flags_t complain)
6237 {
6238 tree fns = expr;
6239 tree fn, fn_no_ptr;
6240 linkage_kind linkage;
6241
6242 fn = instantiate_type (type, fns, tf_none);
6243 if (fn == error_mark_node)
6244 return error_mark_node;
6245
6246 if (value_dependent_expression_p (fn))
6247 goto accept;
6248
6249 fn_no_ptr = strip_fnptr_conv (fn);
6250 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6251 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6252 if (BASELINK_P (fn_no_ptr))
6253 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6254
6255 /* [temp.arg.nontype]/1
6256
6257 A template-argument for a non-type, non-template template-parameter
6258 shall be one of:
6259 [...]
6260 -- the address of an object or function with external [C++11: or
6261 internal] linkage. */
6262
6263 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr);
6264 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6265 {
6266 if (complain & tf_error)
6267 {
6268 location_t loc = cp_expr_loc_or_input_loc (expr);
6269 error_at (loc, "%qE is not a valid template argument for type %qT",
6270 expr, type);
6271 if (TYPE_PTR_P (type))
6272 inform (loc, "it must be the address of a function "
6273 "with external linkage");
6274 else
6275 inform (loc, "it must be the name of a function with "
6276 "external linkage");
6277 }
6278 return NULL_TREE;
6279 }
6280
6281 linkage = decl_linkage (fn_no_ptr);
6282 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6283 {
6284 if (complain & tf_error)
6285 {
6286 location_t loc = cp_expr_loc_or_input_loc (expr);
6287 if (cxx_dialect >= cxx11)
6288 error_at (loc, "%qE is not a valid template argument for type "
6289 "%qT because %qD has no linkage",
6290 expr, type, fn_no_ptr);
6291 else
6292 error_at (loc, "%qE is not a valid template argument for type "
6293 "%qT because %qD does not have external linkage",
6294 expr, type, fn_no_ptr);
6295 }
6296 return NULL_TREE;
6297 }
6298
6299 accept:
6300 if (TYPE_REF_P (type))
6301 {
6302 if (REFERENCE_REF_P (fn))
6303 fn = TREE_OPERAND (fn, 0);
6304 else
6305 fn = build_address (fn);
6306 }
6307 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6308 fn = build_nop (type, fn);
6309
6310 return fn;
6311 }
6312
6313 /* Subroutine of convert_nontype_argument.
6314 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6315 Emit an error otherwise. */
6316
6317 static bool
6318 check_valid_ptrmem_cst_expr (tree type, tree expr,
6319 tsubst_flags_t complain)
6320 {
6321 tree orig_expr = expr;
6322 STRIP_NOPS (expr);
6323 if (null_ptr_cst_p (expr))
6324 return true;
6325 if (TREE_CODE (expr) == PTRMEM_CST
6326 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6327 PTRMEM_CST_CLASS (expr)))
6328 return true;
6329 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6330 return true;
6331 if (processing_template_decl
6332 && TREE_CODE (expr) == ADDR_EXPR
6333 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6334 return true;
6335 if (complain & tf_error)
6336 {
6337 location_t loc = cp_expr_loc_or_input_loc (orig_expr);
6338 error_at (loc, "%qE is not a valid template argument for type %qT",
6339 orig_expr, type);
6340 if (TREE_CODE (expr) != PTRMEM_CST)
6341 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6342 else
6343 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6344 }
6345 return false;
6346 }
6347
6348 /* Returns TRUE iff the address of OP is value-dependent.
6349
6350 14.6.2.4 [temp.dep.temp]:
6351 A non-integral non-type template-argument is dependent if its type is
6352 dependent or it has either of the following forms
6353 qualified-id
6354 & qualified-id
6355 and contains a nested-name-specifier which specifies a class-name that
6356 names a dependent type.
6357
6358 We generalize this to just say that the address of a member of a
6359 dependent class is value-dependent; the above doesn't cover the
6360 address of a static data member named with an unqualified-id. */
6361
6362 static bool
6363 has_value_dependent_address (tree op)
6364 {
6365 /* We could use get_inner_reference here, but there's no need;
6366 this is only relevant for template non-type arguments, which
6367 can only be expressed as &id-expression. */
6368 if (DECL_P (op))
6369 {
6370 tree ctx = CP_DECL_CONTEXT (op);
6371 if (TYPE_P (ctx) && dependent_type_p (ctx))
6372 return true;
6373 }
6374
6375 return false;
6376 }
6377
6378 /* The next set of functions are used for providing helpful explanatory
6379 diagnostics for failed overload resolution. Their messages should be
6380 indented by two spaces for consistency with the messages in
6381 call.c */
6382
6383 static int
6384 unify_success (bool /*explain_p*/)
6385 {
6386 return 0;
6387 }
6388
6389 /* Other failure functions should call this one, to provide a single function
6390 for setting a breakpoint on. */
6391
6392 static int
6393 unify_invalid (bool /*explain_p*/)
6394 {
6395 return 1;
6396 }
6397
6398 static int
6399 unify_parameter_deduction_failure (bool explain_p, tree parm)
6400 {
6401 if (explain_p)
6402 inform (input_location,
6403 " couldn%'t deduce template parameter %qD", parm);
6404 return unify_invalid (explain_p);
6405 }
6406
6407 static int
6408 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6409 {
6410 if (explain_p)
6411 inform (input_location,
6412 " types %qT and %qT have incompatible cv-qualifiers",
6413 parm, arg);
6414 return unify_invalid (explain_p);
6415 }
6416
6417 static int
6418 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6419 {
6420 if (explain_p)
6421 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6422 return unify_invalid (explain_p);
6423 }
6424
6425 static int
6426 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6427 {
6428 if (explain_p)
6429 inform (input_location,
6430 " template parameter %qD is not a parameter pack, but "
6431 "argument %qD is",
6432 parm, arg);
6433 return unify_invalid (explain_p);
6434 }
6435
6436 static int
6437 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6438 {
6439 if (explain_p)
6440 inform (input_location,
6441 " template argument %qE does not match "
6442 "pointer-to-member constant %qE",
6443 arg, parm);
6444 return unify_invalid (explain_p);
6445 }
6446
6447 static int
6448 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6449 {
6450 if (explain_p)
6451 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6452 return unify_invalid (explain_p);
6453 }
6454
6455 static int
6456 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6457 {
6458 if (explain_p)
6459 inform (input_location,
6460 " inconsistent parameter pack deduction with %qT and %qT",
6461 old_arg, new_arg);
6462 return unify_invalid (explain_p);
6463 }
6464
6465 static int
6466 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6467 {
6468 if (explain_p)
6469 {
6470 if (TYPE_P (parm))
6471 inform (input_location,
6472 " deduced conflicting types for parameter %qT (%qT and %qT)",
6473 parm, first, second);
6474 else
6475 inform (input_location,
6476 " deduced conflicting values for non-type parameter "
6477 "%qE (%qE and %qE)", parm, first, second);
6478 }
6479 return unify_invalid (explain_p);
6480 }
6481
6482 static int
6483 unify_vla_arg (bool explain_p, tree arg)
6484 {
6485 if (explain_p)
6486 inform (input_location,
6487 " variable-sized array type %qT is not "
6488 "a valid template argument",
6489 arg);
6490 return unify_invalid (explain_p);
6491 }
6492
6493 static int
6494 unify_method_type_error (bool explain_p, tree arg)
6495 {
6496 if (explain_p)
6497 inform (input_location,
6498 " member function type %qT is not a valid template argument",
6499 arg);
6500 return unify_invalid (explain_p);
6501 }
6502
6503 static int
6504 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6505 {
6506 if (explain_p)
6507 {
6508 if (least_p)
6509 inform_n (input_location, wanted,
6510 " candidate expects at least %d argument, %d provided",
6511 " candidate expects at least %d arguments, %d provided",
6512 wanted, have);
6513 else
6514 inform_n (input_location, wanted,
6515 " candidate expects %d argument, %d provided",
6516 " candidate expects %d arguments, %d provided",
6517 wanted, have);
6518 }
6519 return unify_invalid (explain_p);
6520 }
6521
6522 static int
6523 unify_too_many_arguments (bool explain_p, int have, int wanted)
6524 {
6525 return unify_arity (explain_p, have, wanted);
6526 }
6527
6528 static int
6529 unify_too_few_arguments (bool explain_p, int have, int wanted,
6530 bool least_p = false)
6531 {
6532 return unify_arity (explain_p, have, wanted, least_p);
6533 }
6534
6535 static int
6536 unify_arg_conversion (bool explain_p, tree to_type,
6537 tree from_type, tree arg)
6538 {
6539 if (explain_p)
6540 inform (cp_expr_loc_or_input_loc (arg),
6541 " cannot convert %qE (type %qT) to type %qT",
6542 arg, from_type, to_type);
6543 return unify_invalid (explain_p);
6544 }
6545
6546 static int
6547 unify_no_common_base (bool explain_p, enum template_base_result r,
6548 tree parm, tree arg)
6549 {
6550 if (explain_p)
6551 switch (r)
6552 {
6553 case tbr_ambiguous_baseclass:
6554 inform (input_location, " %qT is an ambiguous base class of %qT",
6555 parm, arg);
6556 break;
6557 default:
6558 inform (input_location, " %qT is not derived from %qT", arg, parm);
6559 break;
6560 }
6561 return unify_invalid (explain_p);
6562 }
6563
6564 static int
6565 unify_inconsistent_template_template_parameters (bool explain_p)
6566 {
6567 if (explain_p)
6568 inform (input_location,
6569 " template parameters of a template template argument are "
6570 "inconsistent with other deduced template arguments");
6571 return unify_invalid (explain_p);
6572 }
6573
6574 static int
6575 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6576 {
6577 if (explain_p)
6578 inform (input_location,
6579 " cannot deduce a template for %qT from non-template type %qT",
6580 parm, arg);
6581 return unify_invalid (explain_p);
6582 }
6583
6584 static int
6585 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6586 {
6587 if (explain_p)
6588 inform (input_location,
6589 " template argument %qE does not match %qE", arg, parm);
6590 return unify_invalid (explain_p);
6591 }
6592
6593 /* True if T is a C++20 template parameter object to store the argument for a
6594 template parameter of class type. */
6595
6596 bool
6597 template_parm_object_p (const_tree t)
6598 {
6599 return (TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t) && DECL_NAME (t)
6600 && !strncmp (IDENTIFIER_POINTER (DECL_NAME (t)), "_ZTA", 4));
6601 }
6602
6603 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
6604 argument for TYPE, points to an unsuitable object. */
6605
6606 static bool
6607 invalid_tparm_referent_p (tree type, tree expr, tsubst_flags_t complain)
6608 {
6609 switch (TREE_CODE (expr))
6610 {
6611 CASE_CONVERT:
6612 return invalid_tparm_referent_p (type, TREE_OPERAND (expr, 0),
6613 complain);
6614
6615 case TARGET_EXPR:
6616 return invalid_tparm_referent_p (type, TARGET_EXPR_INITIAL (expr),
6617 complain);
6618
6619 case CONSTRUCTOR:
6620 {
6621 unsigned i; tree elt;
6622 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), i, elt)
6623 if (invalid_tparm_referent_p (TREE_TYPE (elt), elt, complain))
6624 return true;
6625 }
6626 break;
6627
6628 case ADDR_EXPR:
6629 {
6630 tree decl = TREE_OPERAND (expr, 0);
6631
6632 if (!VAR_P (decl))
6633 {
6634 if (complain & tf_error)
6635 error_at (cp_expr_loc_or_input_loc (expr),
6636 "%qE is not a valid template argument of type %qT "
6637 "because %qE is not a variable", expr, type, decl);
6638 return true;
6639 }
6640 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6641 {
6642 if (complain & tf_error)
6643 error_at (cp_expr_loc_or_input_loc (expr),
6644 "%qE is not a valid template argument of type %qT "
6645 "in C++98 because %qD does not have external linkage",
6646 expr, type, decl);
6647 return true;
6648 }
6649 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
6650 && decl_linkage (decl) == lk_none)
6651 {
6652 if (complain & tf_error)
6653 error_at (cp_expr_loc_or_input_loc (expr),
6654 "%qE is not a valid template argument of type %qT "
6655 "because %qD has no linkage", expr, type, decl);
6656 return true;
6657 }
6658 /* C++17: For a non-type template-parameter of reference or pointer
6659 type, the value of the constant expression shall not refer to (or
6660 for a pointer type, shall not be the address of):
6661 * a subobject (4.5),
6662 * a temporary object (15.2),
6663 * a string literal (5.13.5),
6664 * the result of a typeid expression (8.2.8), or
6665 * a predefined __func__ variable (11.4.1). */
6666 else if (DECL_ARTIFICIAL (decl))
6667 {
6668 if (complain & tf_error)
6669 error ("the address of %qD is not a valid template argument",
6670 decl);
6671 return true;
6672 }
6673 else if (!same_type_ignoring_top_level_qualifiers_p
6674 (strip_array_types (TREE_TYPE (type)),
6675 strip_array_types (TREE_TYPE (decl))))
6676 {
6677 if (complain & tf_error)
6678 error ("the address of the %qT subobject of %qD is not a "
6679 "valid template argument", TREE_TYPE (type), decl);
6680 return true;
6681 }
6682 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
6683 {
6684 if (complain & tf_error)
6685 error ("the address of %qD is not a valid template argument "
6686 "because it does not have static storage duration",
6687 decl);
6688 return true;
6689 }
6690 }
6691 break;
6692
6693 default:
6694 if (!INDIRECT_TYPE_P (type))
6695 /* We're only concerned about pointers and references here. */;
6696 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6697 /* Null pointer values are OK in C++11. */;
6698 else
6699 {
6700 if (VAR_P (expr))
6701 {
6702 if (complain & tf_error)
6703 error ("%qD is not a valid template argument "
6704 "because %qD is a variable, not the address of "
6705 "a variable", expr, expr);
6706 return true;
6707 }
6708 else
6709 {
6710 if (complain & tf_error)
6711 error ("%qE is not a valid template argument for %qT "
6712 "because it is not the address of a variable",
6713 expr, type);
6714 return true;
6715 }
6716 }
6717 }
6718 return false;
6719
6720 }
6721
6722 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
6723 template argument EXPR. */
6724
6725 static tree
6726 get_template_parm_object (tree expr, tsubst_flags_t complain)
6727 {
6728 if (TREE_CODE (expr) == TARGET_EXPR)
6729 expr = TARGET_EXPR_INITIAL (expr);
6730
6731 if (!TREE_CONSTANT (expr))
6732 {
6733 if ((complain & tf_error)
6734 && require_rvalue_constant_expression (expr))
6735 cxx_constant_value (expr);
6736 return error_mark_node;
6737 }
6738 if (invalid_tparm_referent_p (TREE_TYPE (expr), expr, complain))
6739 return error_mark_node;
6740
6741 tree name = mangle_template_parm_object (expr);
6742 tree decl = get_global_binding (name);
6743 if (decl)
6744 return decl;
6745
6746 tree type = cp_build_qualified_type (TREE_TYPE (expr), TYPE_QUAL_CONST);
6747 decl = create_temporary_var (type);
6748 TREE_STATIC (decl) = true;
6749 DECL_DECLARED_CONSTEXPR_P (decl) = true;
6750 TREE_READONLY (decl) = true;
6751 DECL_NAME (decl) = name;
6752 SET_DECL_ASSEMBLER_NAME (decl, name);
6753 DECL_CONTEXT (decl) = global_namespace;
6754 comdat_linkage (decl);
6755 pushdecl_top_level_and_finish (decl, expr);
6756 return decl;
6757 }
6758
6759 /* Attempt to convert the non-type template parameter EXPR to the
6760 indicated TYPE. If the conversion is successful, return the
6761 converted value. If the conversion is unsuccessful, return
6762 NULL_TREE if we issued an error message, or error_mark_node if we
6763 did not. We issue error messages for out-and-out bad template
6764 parameters, but not simply because the conversion failed, since we
6765 might be just trying to do argument deduction. Both TYPE and EXPR
6766 must be non-dependent.
6767
6768 The conversion follows the special rules described in
6769 [temp.arg.nontype], and it is much more strict than an implicit
6770 conversion.
6771
6772 This function is called twice for each template argument (see
6773 lookup_template_class for a more accurate description of this
6774 problem). This means that we need to handle expressions which
6775 are not valid in a C++ source, but can be created from the
6776 first call (for instance, casts to perform conversions). These
6777 hacks can go away after we fix the double coercion problem. */
6778
6779 static tree
6780 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6781 {
6782 tree expr_type;
6783 location_t loc = cp_expr_loc_or_input_loc (expr);
6784
6785 /* Detect immediately string literals as invalid non-type argument.
6786 This special-case is not needed for correctness (we would easily
6787 catch this later), but only to provide better diagnostic for this
6788 common user mistake. As suggested by DR 100, we do not mention
6789 linkage issues in the diagnostic as this is not the point. */
6790 if (TREE_CODE (expr) == STRING_CST && !CLASS_TYPE_P (type))
6791 {
6792 if (complain & tf_error)
6793 error ("%qE is not a valid template argument for type %qT "
6794 "because string literals can never be used in this context",
6795 expr, type);
6796 return NULL_TREE;
6797 }
6798
6799 /* Add the ADDR_EXPR now for the benefit of
6800 value_dependent_expression_p. */
6801 if (TYPE_PTROBV_P (type)
6802 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6803 {
6804 expr = decay_conversion (expr, complain);
6805 if (expr == error_mark_node)
6806 return error_mark_node;
6807 }
6808
6809 /* If we are in a template, EXPR may be non-dependent, but still
6810 have a syntactic, rather than semantic, form. For example, EXPR
6811 might be a SCOPE_REF, rather than the VAR_DECL to which the
6812 SCOPE_REF refers. Preserving the qualifying scope is necessary
6813 so that access checking can be performed when the template is
6814 instantiated -- but here we need the resolved form so that we can
6815 convert the argument. */
6816 bool non_dep = false;
6817 if (TYPE_REF_OBJ_P (type)
6818 && has_value_dependent_address (expr))
6819 /* If we want the address and it's value-dependent, don't fold. */;
6820 else if (processing_template_decl
6821 && is_nondependent_constant_expression (expr))
6822 non_dep = true;
6823 if (error_operand_p (expr))
6824 return error_mark_node;
6825 expr_type = TREE_TYPE (expr);
6826
6827 /* If the argument is non-dependent, perform any conversions in
6828 non-dependent context as well. */
6829 processing_template_decl_sentinel s (non_dep);
6830 if (non_dep)
6831 expr = instantiate_non_dependent_expr_internal (expr, complain);
6832
6833 if (value_dependent_expression_p (expr))
6834 expr = canonicalize_expr_argument (expr, complain);
6835
6836 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6837 to a non-type argument of "nullptr". */
6838 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
6839 expr = fold_simple (convert (type, expr));
6840
6841 /* In C++11, integral or enumeration non-type template arguments can be
6842 arbitrary constant expressions. Pointer and pointer to
6843 member arguments can be general constant expressions that evaluate
6844 to a null value, but otherwise still need to be of a specific form. */
6845 if (cxx_dialect >= cxx11)
6846 {
6847 if (TREE_CODE (expr) == PTRMEM_CST && TYPE_PTRMEM_P (type))
6848 /* A PTRMEM_CST is already constant, and a valid template
6849 argument for a parameter of pointer to member type, we just want
6850 to leave it in that form rather than lower it to a
6851 CONSTRUCTOR. */;
6852 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6853 || cxx_dialect >= cxx17)
6854 {
6855 /* Calling build_converted_constant_expr might create a call to
6856 a conversion function with a value-dependent argument, which
6857 could invoke taking the address of a temporary representing
6858 the result of the conversion. */
6859 if (COMPOUND_LITERAL_P (expr)
6860 && CONSTRUCTOR_IS_DEPENDENT (expr)
6861 && MAYBE_CLASS_TYPE_P (expr_type)
6862 && TYPE_HAS_CONVERSION (expr_type))
6863 {
6864 expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
6865 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr) = true;
6866 return expr;
6867 }
6868 /* C++17: A template-argument for a non-type template-parameter shall
6869 be a converted constant expression (8.20) of the type of the
6870 template-parameter. */
6871 expr = build_converted_constant_expr (type, expr, complain);
6872 if (expr == error_mark_node)
6873 /* Make sure we return NULL_TREE only if we have really issued
6874 an error, as described above. */
6875 return (complain & tf_error) ? NULL_TREE : error_mark_node;
6876 expr = maybe_constant_value (expr, NULL_TREE,
6877 /*manifestly_const_eval=*/true);
6878 expr = convert_from_reference (expr);
6879 }
6880 else if (TYPE_PTR_OR_PTRMEM_P (type))
6881 {
6882 tree folded = maybe_constant_value (expr, NULL_TREE,
6883 /*manifestly_const_eval=*/true);
6884 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6885 : null_member_pointer_value_p (folded))
6886 expr = folded;
6887 }
6888 }
6889
6890 if (TYPE_REF_P (type))
6891 expr = mark_lvalue_use (expr);
6892 else
6893 expr = mark_rvalue_use (expr);
6894
6895 /* HACK: Due to double coercion, we can get a
6896 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6897 which is the tree that we built on the first call (see
6898 below when coercing to reference to object or to reference to
6899 function). We just strip everything and get to the arg.
6900 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6901 for examples. */
6902 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6903 {
6904 tree probe_type, probe = expr;
6905 if (REFERENCE_REF_P (probe))
6906 probe = TREE_OPERAND (probe, 0);
6907 probe_type = TREE_TYPE (probe);
6908 if (TREE_CODE (probe) == NOP_EXPR)
6909 {
6910 /* ??? Maybe we could use convert_from_reference here, but we
6911 would need to relax its constraints because the NOP_EXPR
6912 could actually change the type to something more cv-qualified,
6913 and this is not folded by convert_from_reference. */
6914 tree addr = TREE_OPERAND (probe, 0);
6915 if (TYPE_REF_P (probe_type)
6916 && TREE_CODE (addr) == ADDR_EXPR
6917 && TYPE_PTR_P (TREE_TYPE (addr))
6918 && (same_type_ignoring_top_level_qualifiers_p
6919 (TREE_TYPE (probe_type),
6920 TREE_TYPE (TREE_TYPE (addr)))))
6921 {
6922 expr = TREE_OPERAND (addr, 0);
6923 expr_type = TREE_TYPE (probe_type);
6924 }
6925 }
6926 }
6927
6928 /* [temp.arg.nontype]/5, bullet 1
6929
6930 For a non-type template-parameter of integral or enumeration type,
6931 integral promotions (_conv.prom_) and integral conversions
6932 (_conv.integral_) are applied. */
6933 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6934 {
6935 if (cxx_dialect < cxx11)
6936 {
6937 tree t = build_converted_constant_expr (type, expr, complain);
6938 t = maybe_constant_value (t);
6939 if (t != error_mark_node)
6940 expr = t;
6941 }
6942
6943 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6944 return error_mark_node;
6945
6946 /* Notice that there are constant expressions like '4 % 0' which
6947 do not fold into integer constants. */
6948 if (TREE_CODE (expr) != INTEGER_CST
6949 && !value_dependent_expression_p (expr))
6950 {
6951 if (complain & tf_error)
6952 {
6953 int errs = errorcount, warns = warningcount + werrorcount;
6954 if (!require_potential_constant_expression (expr))
6955 expr = error_mark_node;
6956 else
6957 expr = cxx_constant_value (expr);
6958 if (errorcount > errs || warningcount + werrorcount > warns)
6959 inform (loc, "in template argument for type %qT", type);
6960 if (expr == error_mark_node)
6961 return NULL_TREE;
6962 /* else cxx_constant_value complained but gave us
6963 a real constant, so go ahead. */
6964 if (TREE_CODE (expr) != INTEGER_CST)
6965 {
6966 /* Some assemble time constant expressions like
6967 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
6968 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
6969 as we can emit them into .rodata initializers of
6970 variables, yet they can't fold into an INTEGER_CST at
6971 compile time. Refuse them here. */
6972 gcc_checking_assert (reduced_constant_expression_p (expr));
6973 error_at (loc, "template argument %qE for type %qT not "
6974 "a constant integer", expr, type);
6975 return NULL_TREE;
6976 }
6977 }
6978 else
6979 return NULL_TREE;
6980 }
6981
6982 /* Avoid typedef problems. */
6983 if (TREE_TYPE (expr) != type)
6984 expr = fold_convert (type, expr);
6985 }
6986 /* [temp.arg.nontype]/5, bullet 2
6987
6988 For a non-type template-parameter of type pointer to object,
6989 qualification conversions (_conv.qual_) and the array-to-pointer
6990 conversion (_conv.array_) are applied. */
6991 else if (TYPE_PTROBV_P (type))
6992 {
6993 tree decayed = expr;
6994
6995 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6996 decay_conversion or an explicit cast. If it's a problematic cast,
6997 we'll complain about it below. */
6998 if (TREE_CODE (expr) == NOP_EXPR)
6999 {
7000 tree probe = expr;
7001 STRIP_NOPS (probe);
7002 if (TREE_CODE (probe) == ADDR_EXPR
7003 && TYPE_PTR_P (TREE_TYPE (probe)))
7004 {
7005 expr = probe;
7006 expr_type = TREE_TYPE (expr);
7007 }
7008 }
7009
7010 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
7011
7012 A template-argument for a non-type, non-template template-parameter
7013 shall be one of: [...]
7014
7015 -- the name of a non-type template-parameter;
7016 -- the address of an object or function with external linkage, [...]
7017 expressed as "& id-expression" where the & is optional if the name
7018 refers to a function or array, or if the corresponding
7019 template-parameter is a reference.
7020
7021 Here, we do not care about functions, as they are invalid anyway
7022 for a parameter of type pointer-to-object. */
7023
7024 if (value_dependent_expression_p (expr))
7025 /* Non-type template parameters are OK. */
7026 ;
7027 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7028 /* Null pointer values are OK in C++11. */;
7029 else if (TREE_CODE (expr) != ADDR_EXPR
7030 && !INDIRECT_TYPE_P (expr_type))
7031 /* Other values, like integer constants, might be valid
7032 non-type arguments of some other type. */
7033 return error_mark_node;
7034 else if (invalid_tparm_referent_p (type, expr, complain))
7035 return NULL_TREE;
7036
7037 expr = decayed;
7038
7039 expr = perform_qualification_conversions (type, expr);
7040 if (expr == error_mark_node)
7041 return error_mark_node;
7042 }
7043 /* [temp.arg.nontype]/5, bullet 3
7044
7045 For a non-type template-parameter of type reference to object, no
7046 conversions apply. The type referred to by the reference may be more
7047 cv-qualified than the (otherwise identical) type of the
7048 template-argument. The template-parameter is bound directly to the
7049 template-argument, which must be an lvalue. */
7050 else if (TYPE_REF_OBJ_P (type))
7051 {
7052 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
7053 expr_type))
7054 return error_mark_node;
7055
7056 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
7057 {
7058 if (complain & tf_error)
7059 error ("%qE is not a valid template argument for type %qT "
7060 "because of conflicts in cv-qualification", expr, type);
7061 return NULL_TREE;
7062 }
7063
7064 if (!lvalue_p (expr))
7065 {
7066 if (complain & tf_error)
7067 error ("%qE is not a valid template argument for type %qT "
7068 "because it is not an lvalue", expr, type);
7069 return NULL_TREE;
7070 }
7071
7072 /* [temp.arg.nontype]/1
7073
7074 A template-argument for a non-type, non-template template-parameter
7075 shall be one of: [...]
7076
7077 -- the address of an object or function with external linkage. */
7078 if (INDIRECT_REF_P (expr)
7079 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
7080 {
7081 expr = TREE_OPERAND (expr, 0);
7082 if (DECL_P (expr))
7083 {
7084 if (complain & tf_error)
7085 error ("%q#D is not a valid template argument for type %qT "
7086 "because a reference variable does not have a constant "
7087 "address", expr, type);
7088 return NULL_TREE;
7089 }
7090 }
7091
7092 if (TYPE_REF_OBJ_P (TREE_TYPE (expr))
7093 && value_dependent_expression_p (expr))
7094 /* OK, dependent reference. We don't want to ask whether a DECL is
7095 itself value-dependent, since what we want here is its address. */;
7096 else
7097 {
7098 expr = build_address (expr);
7099
7100 if (invalid_tparm_referent_p (type, expr, complain))
7101 return NULL_TREE;
7102 }
7103
7104 if (!same_type_p (type, TREE_TYPE (expr)))
7105 expr = build_nop (type, expr);
7106 }
7107 /* [temp.arg.nontype]/5, bullet 4
7108
7109 For a non-type template-parameter of type pointer to function, only
7110 the function-to-pointer conversion (_conv.func_) is applied. If the
7111 template-argument represents a set of overloaded functions (or a
7112 pointer to such), the matching function is selected from the set
7113 (_over.over_). */
7114 else if (TYPE_PTRFN_P (type))
7115 {
7116 /* If the argument is a template-id, we might not have enough
7117 context information to decay the pointer. */
7118 if (!type_unknown_p (expr_type))
7119 {
7120 expr = decay_conversion (expr, complain);
7121 if (expr == error_mark_node)
7122 return error_mark_node;
7123 }
7124
7125 if (cxx_dialect >= cxx11 && integer_zerop (expr))
7126 /* Null pointer values are OK in C++11. */
7127 return perform_qualification_conversions (type, expr);
7128
7129 expr = convert_nontype_argument_function (type, expr, complain);
7130 if (!expr || expr == error_mark_node)
7131 return expr;
7132 }
7133 /* [temp.arg.nontype]/5, bullet 5
7134
7135 For a non-type template-parameter of type reference to function, no
7136 conversions apply. If the template-argument represents a set of
7137 overloaded functions, the matching function is selected from the set
7138 (_over.over_). */
7139 else if (TYPE_REFFN_P (type))
7140 {
7141 if (TREE_CODE (expr) == ADDR_EXPR)
7142 {
7143 if (complain & tf_error)
7144 {
7145 error ("%qE is not a valid template argument for type %qT "
7146 "because it is a pointer", expr, type);
7147 inform (input_location, "try using %qE instead",
7148 TREE_OPERAND (expr, 0));
7149 }
7150 return NULL_TREE;
7151 }
7152
7153 expr = convert_nontype_argument_function (type, expr, complain);
7154 if (!expr || expr == error_mark_node)
7155 return expr;
7156 }
7157 /* [temp.arg.nontype]/5, bullet 6
7158
7159 For a non-type template-parameter of type pointer to member function,
7160 no conversions apply. If the template-argument represents a set of
7161 overloaded member functions, the matching member function is selected
7162 from the set (_over.over_). */
7163 else if (TYPE_PTRMEMFUNC_P (type))
7164 {
7165 expr = instantiate_type (type, expr, tf_none);
7166 if (expr == error_mark_node)
7167 return error_mark_node;
7168
7169 /* [temp.arg.nontype] bullet 1 says the pointer to member
7170 expression must be a pointer-to-member constant. */
7171 if (!value_dependent_expression_p (expr)
7172 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7173 return NULL_TREE;
7174
7175 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7176 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7177 if (fnptr_conv_p (type, TREE_TYPE (expr)))
7178 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
7179 }
7180 /* [temp.arg.nontype]/5, bullet 7
7181
7182 For a non-type template-parameter of type pointer to data member,
7183 qualification conversions (_conv.qual_) are applied. */
7184 else if (TYPE_PTRDATAMEM_P (type))
7185 {
7186 /* [temp.arg.nontype] bullet 1 says the pointer to member
7187 expression must be a pointer-to-member constant. */
7188 if (!value_dependent_expression_p (expr)
7189 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7190 return NULL_TREE;
7191
7192 expr = perform_qualification_conversions (type, expr);
7193 if (expr == error_mark_node)
7194 return expr;
7195 }
7196 else if (NULLPTR_TYPE_P (type))
7197 {
7198 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
7199 {
7200 if (complain & tf_error)
7201 error ("%qE is not a valid template argument for type %qT "
7202 "because it is of type %qT", expr, type, TREE_TYPE (expr));
7203 return NULL_TREE;
7204 }
7205 return expr;
7206 }
7207 else if (CLASS_TYPE_P (type))
7208 {
7209 /* Replace the argument with a reference to the corresponding template
7210 parameter object. */
7211 if (!value_dependent_expression_p (expr))
7212 expr = get_template_parm_object (expr, complain);
7213 if (expr == error_mark_node)
7214 return NULL_TREE;
7215 }
7216 /* A template non-type parameter must be one of the above. */
7217 else
7218 gcc_unreachable ();
7219
7220 /* Sanity check: did we actually convert the argument to the
7221 right type? */
7222 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7223 (type, TREE_TYPE (expr)));
7224 return convert_from_reference (expr);
7225 }
7226
7227 /* Subroutine of coerce_template_template_parms, which returns 1 if
7228 PARM_PARM and ARG_PARM match using the rule for the template
7229 parameters of template template parameters. Both PARM and ARG are
7230 template parameters; the rest of the arguments are the same as for
7231 coerce_template_template_parms.
7232 */
7233 static int
7234 coerce_template_template_parm (tree parm,
7235 tree arg,
7236 tsubst_flags_t complain,
7237 tree in_decl,
7238 tree outer_args)
7239 {
7240 if (arg == NULL_TREE || error_operand_p (arg)
7241 || parm == NULL_TREE || error_operand_p (parm))
7242 return 0;
7243
7244 if (TREE_CODE (arg) != TREE_CODE (parm))
7245 return 0;
7246
7247 switch (TREE_CODE (parm))
7248 {
7249 case TEMPLATE_DECL:
7250 /* We encounter instantiations of templates like
7251 template <template <template <class> class> class TT>
7252 class C; */
7253 {
7254 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7255 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7256
7257 if (!coerce_template_template_parms
7258 (parmparm, argparm, complain, in_decl, outer_args))
7259 return 0;
7260 }
7261 /* Fall through. */
7262
7263 case TYPE_DECL:
7264 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
7265 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7266 /* Argument is a parameter pack but parameter is not. */
7267 return 0;
7268 break;
7269
7270 case PARM_DECL:
7271 /* The tsubst call is used to handle cases such as
7272
7273 template <int> class C {};
7274 template <class T, template <T> class TT> class D {};
7275 D<int, C> d;
7276
7277 i.e. the parameter list of TT depends on earlier parameters. */
7278 if (!uses_template_parms (TREE_TYPE (arg)))
7279 {
7280 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
7281 if (!uses_template_parms (t)
7282 && !same_type_p (t, TREE_TYPE (arg)))
7283 return 0;
7284 }
7285
7286 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
7287 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7288 /* Argument is a parameter pack but parameter is not. */
7289 return 0;
7290
7291 break;
7292
7293 default:
7294 gcc_unreachable ();
7295 }
7296
7297 return 1;
7298 }
7299
7300 /* Coerce template argument list ARGLIST for use with template
7301 template-parameter TEMPL. */
7302
7303 static tree
7304 coerce_template_args_for_ttp (tree templ, tree arglist,
7305 tsubst_flags_t complain)
7306 {
7307 /* Consider an example where a template template parameter declared as
7308
7309 template <class T, class U = std::allocator<T> > class TT
7310
7311 The template parameter level of T and U are one level larger than
7312 of TT. To proper process the default argument of U, say when an
7313 instantiation `TT<int>' is seen, we need to build the full
7314 arguments containing {int} as the innermost level. Outer levels,
7315 available when not appearing as default template argument, can be
7316 obtained from the arguments of the enclosing template.
7317
7318 Suppose that TT is later substituted with std::vector. The above
7319 instantiation is `TT<int, std::allocator<T> >' with TT at
7320 level 1, and T at level 2, while the template arguments at level 1
7321 becomes {std::vector} and the inner level 2 is {int}. */
7322
7323 tree outer = DECL_CONTEXT (templ);
7324 if (outer)
7325 {
7326 if (DECL_TEMPLATE_SPECIALIZATION (outer))
7327 /* We want arguments for the partial specialization, not arguments for
7328 the primary template. */
7329 outer = template_parms_to_args (DECL_TEMPLATE_PARMS (outer));
7330 else
7331 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7332 }
7333 else if (current_template_parms)
7334 {
7335 /* This is an argument of the current template, so we haven't set
7336 DECL_CONTEXT yet. */
7337 tree relevant_template_parms;
7338
7339 /* Parameter levels that are greater than the level of the given
7340 template template parm are irrelevant. */
7341 relevant_template_parms = current_template_parms;
7342 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7343 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7344 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7345
7346 outer = template_parms_to_args (relevant_template_parms);
7347 }
7348
7349 if (outer)
7350 arglist = add_to_template_args (outer, arglist);
7351
7352 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7353 return coerce_template_parms (parmlist, arglist, templ,
7354 complain,
7355 /*require_all_args=*/true,
7356 /*use_default_args=*/true);
7357 }
7358
7359 /* A cache of template template parameters with match-all default
7360 arguments. */
7361 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7362
7363 /* T is a bound template template-parameter. Copy its arguments into default
7364 arguments of the template template-parameter's template parameters. */
7365
7366 static tree
7367 add_defaults_to_ttp (tree otmpl)
7368 {
7369 if (tree *c = hash_map_safe_get (defaulted_ttp_cache, otmpl))
7370 return *c;
7371
7372 tree ntmpl = copy_node (otmpl);
7373
7374 tree ntype = copy_node (TREE_TYPE (otmpl));
7375 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7376 TYPE_MAIN_VARIANT (ntype) = ntype;
7377 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7378 TYPE_NAME (ntype) = ntmpl;
7379 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7380
7381 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7382 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7383 TEMPLATE_PARM_DECL (idx) = ntmpl;
7384 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7385
7386 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7387 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7388 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7389 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7390 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7391 {
7392 tree o = TREE_VEC_ELT (vec, i);
7393 if (!template_parameter_pack_p (TREE_VALUE (o)))
7394 {
7395 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7396 TREE_PURPOSE (n) = any_targ_node;
7397 }
7398 }
7399
7400 hash_map_safe_put<hm_ggc> (defaulted_ttp_cache, otmpl, ntmpl);
7401 return ntmpl;
7402 }
7403
7404 /* ARG is a bound potential template template-argument, and PARGS is a list
7405 of arguments for the corresponding template template-parameter. Adjust
7406 PARGS as appropriate for application to ARG's template, and if ARG is a
7407 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7408 arguments to the template template parameter. */
7409
7410 static tree
7411 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7412 {
7413 ++processing_template_decl;
7414 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7415 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7416 {
7417 /* When comparing two template template-parameters in partial ordering,
7418 rewrite the one currently being used as an argument to have default
7419 arguments for all parameters. */
7420 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7421 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7422 if (pargs != error_mark_node)
7423 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7424 TYPE_TI_ARGS (arg));
7425 }
7426 else
7427 {
7428 tree aparms
7429 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7430 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7431 /*require_all*/true,
7432 /*use_default*/true);
7433 }
7434 --processing_template_decl;
7435 return pargs;
7436 }
7437
7438 /* Subroutine of unify for the case when PARM is a
7439 BOUND_TEMPLATE_TEMPLATE_PARM. */
7440
7441 static int
7442 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7443 bool explain_p)
7444 {
7445 tree parmvec = TYPE_TI_ARGS (parm);
7446 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7447
7448 /* The template template parm might be variadic and the argument
7449 not, so flatten both argument lists. */
7450 parmvec = expand_template_argument_pack (parmvec);
7451 argvec = expand_template_argument_pack (argvec);
7452
7453 if (flag_new_ttp)
7454 {
7455 /* In keeping with P0522R0, adjust P's template arguments
7456 to apply to A's template; then flatten it again. */
7457 tree nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7458 nparmvec = expand_template_argument_pack (nparmvec);
7459
7460 if (unify (tparms, targs, nparmvec, argvec,
7461 UNIFY_ALLOW_NONE, explain_p))
7462 return 1;
7463
7464 /* If the P0522 adjustment eliminated a pack expansion, deduce
7465 empty packs. */
7466 if (flag_new_ttp
7467 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7468 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7469 DEDUCE_EXACT, /*sub*/true, explain_p))
7470 return 1;
7471 }
7472 else
7473 {
7474 /* Deduce arguments T, i from TT<T> or TT<i>.
7475 We check each element of PARMVEC and ARGVEC individually
7476 rather than the whole TREE_VEC since they can have
7477 different number of elements, which is allowed under N2555. */
7478
7479 int len = TREE_VEC_LENGTH (parmvec);
7480
7481 /* Check if the parameters end in a pack, making them
7482 variadic. */
7483 int parm_variadic_p = 0;
7484 if (len > 0
7485 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7486 parm_variadic_p = 1;
7487
7488 for (int i = 0; i < len - parm_variadic_p; ++i)
7489 /* If the template argument list of P contains a pack
7490 expansion that is not the last template argument, the
7491 entire template argument list is a non-deduced
7492 context. */
7493 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7494 return unify_success (explain_p);
7495
7496 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7497 return unify_too_few_arguments (explain_p,
7498 TREE_VEC_LENGTH (argvec), len);
7499
7500 for (int i = 0; i < len - parm_variadic_p; ++i)
7501 if (unify (tparms, targs,
7502 TREE_VEC_ELT (parmvec, i),
7503 TREE_VEC_ELT (argvec, i),
7504 UNIFY_ALLOW_NONE, explain_p))
7505 return 1;
7506
7507 if (parm_variadic_p
7508 && unify_pack_expansion (tparms, targs,
7509 parmvec, argvec,
7510 DEDUCE_EXACT,
7511 /*subr=*/true, explain_p))
7512 return 1;
7513 }
7514
7515 return 0;
7516 }
7517
7518 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7519 template template parameters. Both PARM_PARMS and ARG_PARMS are
7520 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7521 or PARM_DECL.
7522
7523 Consider the example:
7524 template <class T> class A;
7525 template<template <class U> class TT> class B;
7526
7527 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7528 the parameters to A, and OUTER_ARGS contains A. */
7529
7530 static int
7531 coerce_template_template_parms (tree parm_parms,
7532 tree arg_parms,
7533 tsubst_flags_t complain,
7534 tree in_decl,
7535 tree outer_args)
7536 {
7537 int nparms, nargs, i;
7538 tree parm, arg;
7539 int variadic_p = 0;
7540
7541 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7542 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7543
7544 nparms = TREE_VEC_LENGTH (parm_parms);
7545 nargs = TREE_VEC_LENGTH (arg_parms);
7546
7547 if (flag_new_ttp)
7548 {
7549 /* P0522R0: A template template-parameter P is at least as specialized as
7550 a template template-argument A if, given the following rewrite to two
7551 function templates, the function template corresponding to P is at
7552 least as specialized as the function template corresponding to A
7553 according to the partial ordering rules for function templates
7554 ([temp.func.order]). Given an invented class template X with the
7555 template parameter list of A (including default arguments):
7556
7557 * Each of the two function templates has the same template parameters,
7558 respectively, as P or A.
7559
7560 * Each function template has a single function parameter whose type is
7561 a specialization of X with template arguments corresponding to the
7562 template parameters from the respective function template where, for
7563 each template parameter PP in the template parameter list of the
7564 function template, a corresponding template argument AA is formed. If
7565 PP declares a parameter pack, then AA is the pack expansion
7566 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7567
7568 If the rewrite produces an invalid type, then P is not at least as
7569 specialized as A. */
7570
7571 /* So coerce P's args to apply to A's parms, and then deduce between A's
7572 args and the converted args. If that succeeds, A is at least as
7573 specialized as P, so they match.*/
7574 tree pargs = template_parms_level_to_args (parm_parms);
7575 pargs = add_outermost_template_args (outer_args, pargs);
7576 ++processing_template_decl;
7577 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7578 /*require_all*/true, /*use_default*/true);
7579 --processing_template_decl;
7580 if (pargs != error_mark_node)
7581 {
7582 tree targs = make_tree_vec (nargs);
7583 tree aargs = template_parms_level_to_args (arg_parms);
7584 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7585 /*explain*/false))
7586 return 1;
7587 }
7588 }
7589
7590 /* Determine whether we have a parameter pack at the end of the
7591 template template parameter's template parameter list. */
7592 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7593 {
7594 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7595
7596 if (error_operand_p (parm))
7597 return 0;
7598
7599 switch (TREE_CODE (parm))
7600 {
7601 case TEMPLATE_DECL:
7602 case TYPE_DECL:
7603 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7604 variadic_p = 1;
7605 break;
7606
7607 case PARM_DECL:
7608 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7609 variadic_p = 1;
7610 break;
7611
7612 default:
7613 gcc_unreachable ();
7614 }
7615 }
7616
7617 if (nargs != nparms
7618 && !(variadic_p && nargs >= nparms - 1))
7619 return 0;
7620
7621 /* Check all of the template parameters except the parameter pack at
7622 the end (if any). */
7623 for (i = 0; i < nparms - variadic_p; ++i)
7624 {
7625 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7626 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7627 continue;
7628
7629 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7630 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7631
7632 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7633 outer_args))
7634 return 0;
7635
7636 }
7637
7638 if (variadic_p)
7639 {
7640 /* Check each of the template parameters in the template
7641 argument against the template parameter pack at the end of
7642 the template template parameter. */
7643 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7644 return 0;
7645
7646 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7647
7648 for (; i < nargs; ++i)
7649 {
7650 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7651 continue;
7652
7653 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7654
7655 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7656 outer_args))
7657 return 0;
7658 }
7659 }
7660
7661 return 1;
7662 }
7663
7664 /* Verifies that the deduced template arguments (in TARGS) for the
7665 template template parameters (in TPARMS) represent valid bindings,
7666 by comparing the template parameter list of each template argument
7667 to the template parameter list of its corresponding template
7668 template parameter, in accordance with DR150. This
7669 routine can only be called after all template arguments have been
7670 deduced. It will return TRUE if all of the template template
7671 parameter bindings are okay, FALSE otherwise. */
7672 bool
7673 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7674 {
7675 int i, ntparms = TREE_VEC_LENGTH (tparms);
7676 bool ret = true;
7677
7678 /* We're dealing with template parms in this process. */
7679 ++processing_template_decl;
7680
7681 targs = INNERMOST_TEMPLATE_ARGS (targs);
7682
7683 for (i = 0; i < ntparms; ++i)
7684 {
7685 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7686 tree targ = TREE_VEC_ELT (targs, i);
7687
7688 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7689 {
7690 tree packed_args = NULL_TREE;
7691 int idx, len = 1;
7692
7693 if (ARGUMENT_PACK_P (targ))
7694 {
7695 /* Look inside the argument pack. */
7696 packed_args = ARGUMENT_PACK_ARGS (targ);
7697 len = TREE_VEC_LENGTH (packed_args);
7698 }
7699
7700 for (idx = 0; idx < len; ++idx)
7701 {
7702 tree targ_parms = NULL_TREE;
7703
7704 if (packed_args)
7705 /* Extract the next argument from the argument
7706 pack. */
7707 targ = TREE_VEC_ELT (packed_args, idx);
7708
7709 if (PACK_EXPANSION_P (targ))
7710 /* Look at the pattern of the pack expansion. */
7711 targ = PACK_EXPANSION_PATTERN (targ);
7712
7713 /* Extract the template parameters from the template
7714 argument. */
7715 if (TREE_CODE (targ) == TEMPLATE_DECL)
7716 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7717 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7718 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7719
7720 /* Verify that we can coerce the template template
7721 parameters from the template argument to the template
7722 parameter. This requires an exact match. */
7723 if (targ_parms
7724 && !coerce_template_template_parms
7725 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7726 targ_parms,
7727 tf_none,
7728 tparm,
7729 targs))
7730 {
7731 ret = false;
7732 goto out;
7733 }
7734 }
7735 }
7736 }
7737
7738 out:
7739
7740 --processing_template_decl;
7741 return ret;
7742 }
7743
7744 /* Since type attributes aren't mangled, we need to strip them from
7745 template type arguments. */
7746
7747 static tree
7748 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7749 {
7750 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7751 return arg;
7752 bool removed_attributes = false;
7753 tree canon = strip_typedefs (arg, &removed_attributes);
7754 if (removed_attributes
7755 && (complain & tf_warning))
7756 warning (OPT_Wignored_attributes,
7757 "ignoring attributes on template argument %qT", arg);
7758 return canon;
7759 }
7760
7761 /* And from inside dependent non-type arguments like sizeof(Type). */
7762
7763 static tree
7764 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7765 {
7766 if (!arg || arg == error_mark_node)
7767 return arg;
7768 bool removed_attributes = false;
7769 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7770 if (removed_attributes
7771 && (complain & tf_warning))
7772 warning (OPT_Wignored_attributes,
7773 "ignoring attributes in template argument %qE", arg);
7774 return canon;
7775 }
7776
7777 // A template declaration can be substituted for a constrained
7778 // template template parameter only when the argument is more
7779 // constrained than the parameter.
7780 static bool
7781 is_compatible_template_arg (tree parm, tree arg)
7782 {
7783 tree parm_cons = get_constraints (parm);
7784
7785 /* For now, allow constrained template template arguments
7786 and unconstrained template template parameters. */
7787 if (parm_cons == NULL_TREE)
7788 return true;
7789
7790 tree arg_cons = get_constraints (arg);
7791
7792 /* If the template parameter is constrained, we need to rewrite its
7793 constraints in terms of the ARG's template parameters. This ensures
7794 that all of the template parameter types will have the same depth.
7795
7796 Note that this is only valid when coerce_template_template_parm is
7797 true for the innermost template parameters of PARM and ARG. In other
7798 words, because coercion is successful, this conversion will be valid. */
7799 if (parm_cons)
7800 {
7801 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7802 parm_cons = tsubst_constraint_info (parm_cons,
7803 INNERMOST_TEMPLATE_ARGS (args),
7804 tf_none, NULL_TREE);
7805 if (parm_cons == error_mark_node)
7806 return false;
7807 }
7808
7809 return subsumes (parm_cons, arg_cons);
7810 }
7811
7812 // Convert a placeholder argument into a binding to the original
7813 // parameter. The original parameter is saved as the TREE_TYPE of
7814 // ARG.
7815 static inline tree
7816 convert_wildcard_argument (tree parm, tree arg)
7817 {
7818 TREE_TYPE (arg) = parm;
7819 return arg;
7820 }
7821
7822 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
7823 because one of them is dependent. But we need to represent the
7824 conversion for the benefit of cp_tree_equal. */
7825
7826 static tree
7827 maybe_convert_nontype_argument (tree type, tree arg)
7828 {
7829 /* Auto parms get no conversion. */
7830 if (type_uses_auto (type))
7831 return arg;
7832 /* We don't need or want to add this conversion now if we're going to use the
7833 argument for deduction. */
7834 if (value_dependent_expression_p (arg))
7835 return arg;
7836
7837 type = cv_unqualified (type);
7838 tree argtype = TREE_TYPE (arg);
7839 if (same_type_p (type, argtype))
7840 return arg;
7841
7842 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
7843 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
7844 return arg;
7845 }
7846
7847 /* Convert the indicated template ARG as necessary to match the
7848 indicated template PARM. Returns the converted ARG, or
7849 error_mark_node if the conversion was unsuccessful. Error and
7850 warning messages are issued under control of COMPLAIN. This
7851 conversion is for the Ith parameter in the parameter list. ARGS is
7852 the full set of template arguments deduced so far. */
7853
7854 static tree
7855 convert_template_argument (tree parm,
7856 tree arg,
7857 tree args,
7858 tsubst_flags_t complain,
7859 int i,
7860 tree in_decl)
7861 {
7862 tree orig_arg;
7863 tree val;
7864 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7865
7866 if (parm == error_mark_node || error_operand_p (arg))
7867 return error_mark_node;
7868
7869 /* Trivially convert placeholders. */
7870 if (TREE_CODE (arg) == WILDCARD_DECL)
7871 return convert_wildcard_argument (parm, arg);
7872
7873 if (arg == any_targ_node)
7874 return arg;
7875
7876 if (TREE_CODE (arg) == TREE_LIST
7877 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7878 {
7879 /* The template argument was the name of some
7880 member function. That's usually
7881 invalid, but static members are OK. In any
7882 case, grab the underlying fields/functions
7883 and issue an error later if required. */
7884 TREE_TYPE (arg) = unknown_type_node;
7885 }
7886
7887 orig_arg = arg;
7888
7889 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7890 requires_type = (TREE_CODE (parm) == TYPE_DECL
7891 || requires_tmpl_type);
7892
7893 /* When determining whether an argument pack expansion is a template,
7894 look at the pattern. */
7895 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7896 arg = PACK_EXPANSION_PATTERN (arg);
7897
7898 /* Deal with an injected-class-name used as a template template arg. */
7899 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7900 {
7901 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7902 if (TREE_CODE (t) == TEMPLATE_DECL)
7903 {
7904 if (cxx_dialect >= cxx11)
7905 /* OK under DR 1004. */;
7906 else if (complain & tf_warning_or_error)
7907 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7908 " used as template template argument", TYPE_NAME (arg));
7909 else if (flag_pedantic_errors)
7910 t = arg;
7911
7912 arg = t;
7913 }
7914 }
7915
7916 is_tmpl_type =
7917 ((TREE_CODE (arg) == TEMPLATE_DECL
7918 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7919 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7920 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7921 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7922
7923 if (is_tmpl_type
7924 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7925 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7926 arg = TYPE_STUB_DECL (arg);
7927
7928 is_type = TYPE_P (arg) || is_tmpl_type;
7929
7930 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7931 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7932 {
7933 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7934 {
7935 if (complain & tf_error)
7936 error ("invalid use of destructor %qE as a type", orig_arg);
7937 return error_mark_node;
7938 }
7939
7940 permerror (input_location,
7941 "to refer to a type member of a template parameter, "
7942 "use %<typename %E%>", orig_arg);
7943
7944 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7945 TREE_OPERAND (arg, 1),
7946 typename_type,
7947 complain);
7948 arg = orig_arg;
7949 is_type = 1;
7950 }
7951 if (is_type != requires_type)
7952 {
7953 if (in_decl)
7954 {
7955 if (complain & tf_error)
7956 {
7957 error ("type/value mismatch at argument %d in template "
7958 "parameter list for %qD",
7959 i + 1, in_decl);
7960 if (is_type)
7961 {
7962 /* The template argument is a type, but we're expecting
7963 an expression. */
7964 inform (input_location,
7965 " expected a constant of type %qT, got %qT",
7966 TREE_TYPE (parm),
7967 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7968 /* [temp.arg]/2: "In a template-argument, an ambiguity
7969 between a type-id and an expression is resolved to a
7970 type-id, regardless of the form of the corresponding
7971 template-parameter." So give the user a clue. */
7972 if (TREE_CODE (arg) == FUNCTION_TYPE)
7973 inform (input_location, " ambiguous template argument "
7974 "for non-type template parameter is treated as "
7975 "function type");
7976 }
7977 else if (requires_tmpl_type)
7978 inform (input_location,
7979 " expected a class template, got %qE", orig_arg);
7980 else
7981 inform (input_location,
7982 " expected a type, got %qE", orig_arg);
7983 }
7984 }
7985 return error_mark_node;
7986 }
7987 if (is_tmpl_type ^ requires_tmpl_type)
7988 {
7989 if (in_decl && (complain & tf_error))
7990 {
7991 error ("type/value mismatch at argument %d in template "
7992 "parameter list for %qD",
7993 i + 1, in_decl);
7994 if (is_tmpl_type)
7995 inform (input_location,
7996 " expected a type, got %qT", DECL_NAME (arg));
7997 else
7998 inform (input_location,
7999 " expected a class template, got %qT", orig_arg);
8000 }
8001 return error_mark_node;
8002 }
8003
8004 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
8005 /* We already did the appropriate conversion when packing args. */
8006 val = orig_arg;
8007 else if (is_type)
8008 {
8009 if (requires_tmpl_type)
8010 {
8011 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
8012 /* The number of argument required is not known yet.
8013 Just accept it for now. */
8014 val = orig_arg;
8015 else
8016 {
8017 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
8018 tree argparm;
8019
8020 /* Strip alias templates that are equivalent to another
8021 template. */
8022 arg = get_underlying_template (arg);
8023 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8024
8025 if (coerce_template_template_parms (parmparm, argparm,
8026 complain, in_decl,
8027 args))
8028 {
8029 val = arg;
8030
8031 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8032 TEMPLATE_DECL. */
8033 if (val != error_mark_node)
8034 {
8035 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
8036 val = TREE_TYPE (val);
8037 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
8038 val = make_pack_expansion (val, complain);
8039 }
8040 }
8041 else
8042 {
8043 if (in_decl && (complain & tf_error))
8044 {
8045 error ("type/value mismatch at argument %d in "
8046 "template parameter list for %qD",
8047 i + 1, in_decl);
8048 inform (input_location,
8049 " expected a template of type %qD, got %qT",
8050 parm, orig_arg);
8051 }
8052
8053 val = error_mark_node;
8054 }
8055
8056 // Check that the constraints are compatible before allowing the
8057 // substitution.
8058 if (val != error_mark_node)
8059 if (!is_compatible_template_arg (parm, arg))
8060 {
8061 if (in_decl && (complain & tf_error))
8062 {
8063 error ("constraint mismatch at argument %d in "
8064 "template parameter list for %qD",
8065 i + 1, in_decl);
8066 inform (input_location, " expected %qD but got %qD",
8067 parm, arg);
8068 }
8069 val = error_mark_node;
8070 }
8071 }
8072 }
8073 else
8074 val = orig_arg;
8075 /* We only form one instance of each template specialization.
8076 Therefore, if we use a non-canonical variant (i.e., a
8077 typedef), any future messages referring to the type will use
8078 the typedef, which is confusing if those future uses do not
8079 themselves also use the typedef. */
8080 if (TYPE_P (val))
8081 val = canonicalize_type_argument (val, complain);
8082 }
8083 else
8084 {
8085 tree t = TREE_TYPE (parm);
8086
8087 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm))
8088 > TMPL_ARGS_DEPTH (args))
8089 /* We don't have enough levels of args to do any substitution. This
8090 can happen in the context of -fnew-ttp-matching. */;
8091 else if (tree a = type_uses_auto (t))
8092 {
8093 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
8094 if (t == error_mark_node)
8095 return error_mark_node;
8096 }
8097 else
8098 t = tsubst (t, args, complain, in_decl);
8099
8100 if (invalid_nontype_parm_type_p (t, complain))
8101 return error_mark_node;
8102
8103 if (t != TREE_TYPE (parm))
8104 t = canonicalize_type_argument (t, complain);
8105
8106 if (!type_dependent_expression_p (orig_arg)
8107 && !uses_template_parms (t))
8108 /* We used to call digest_init here. However, digest_init
8109 will report errors, which we don't want when complain
8110 is zero. More importantly, digest_init will try too
8111 hard to convert things: for example, `0' should not be
8112 converted to pointer type at this point according to
8113 the standard. Accepting this is not merely an
8114 extension, since deciding whether or not these
8115 conversions can occur is part of determining which
8116 function template to call, or whether a given explicit
8117 argument specification is valid. */
8118 val = convert_nontype_argument (t, orig_arg, complain);
8119 else
8120 {
8121 val = canonicalize_expr_argument (orig_arg, complain);
8122 val = maybe_convert_nontype_argument (t, val);
8123 }
8124
8125
8126 if (val == NULL_TREE)
8127 val = error_mark_node;
8128 else if (val == error_mark_node && (complain & tf_error))
8129 error_at (cp_expr_loc_or_input_loc (orig_arg),
8130 "could not convert template argument %qE from %qT to %qT",
8131 orig_arg, TREE_TYPE (orig_arg), t);
8132
8133 if (INDIRECT_REF_P (val))
8134 {
8135 /* Reject template arguments that are references to built-in
8136 functions with no library fallbacks. */
8137 const_tree inner = TREE_OPERAND (val, 0);
8138 const_tree innertype = TREE_TYPE (inner);
8139 if (innertype
8140 && TYPE_REF_P (innertype)
8141 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
8142 && TREE_OPERAND_LENGTH (inner) > 0
8143 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
8144 return error_mark_node;
8145 }
8146
8147 if (TREE_CODE (val) == SCOPE_REF)
8148 {
8149 /* Strip typedefs from the SCOPE_REF. */
8150 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
8151 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
8152 complain);
8153 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
8154 QUALIFIED_NAME_IS_TEMPLATE (val));
8155 }
8156 }
8157
8158 return val;
8159 }
8160
8161 /* Coerces the remaining template arguments in INNER_ARGS (from
8162 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8163 Returns the coerced argument pack. PARM_IDX is the position of this
8164 parameter in the template parameter list. ARGS is the original
8165 template argument list. */
8166 static tree
8167 coerce_template_parameter_pack (tree parms,
8168 int parm_idx,
8169 tree args,
8170 tree inner_args,
8171 int arg_idx,
8172 tree new_args,
8173 int* lost,
8174 tree in_decl,
8175 tsubst_flags_t complain)
8176 {
8177 tree parm = TREE_VEC_ELT (parms, parm_idx);
8178 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8179 tree packed_args;
8180 tree argument_pack;
8181 tree packed_parms = NULL_TREE;
8182
8183 if (arg_idx > nargs)
8184 arg_idx = nargs;
8185
8186 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
8187 {
8188 /* When the template parameter is a non-type template parameter pack
8189 or template template parameter pack whose type or template
8190 parameters use parameter packs, we know exactly how many arguments
8191 we are looking for. Build a vector of the instantiated decls for
8192 these template parameters in PACKED_PARMS. */
8193 /* We can't use make_pack_expansion here because it would interpret a
8194 _DECL as a use rather than a declaration. */
8195 tree decl = TREE_VALUE (parm);
8196 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
8197 SET_PACK_EXPANSION_PATTERN (exp, decl);
8198 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
8199 SET_TYPE_STRUCTURAL_EQUALITY (exp);
8200
8201 TREE_VEC_LENGTH (args)--;
8202 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
8203 TREE_VEC_LENGTH (args)++;
8204
8205 if (packed_parms == error_mark_node)
8206 return error_mark_node;
8207
8208 /* If we're doing a partial instantiation of a member template,
8209 verify that all of the types used for the non-type
8210 template parameter pack are, in fact, valid for non-type
8211 template parameters. */
8212 if (arg_idx < nargs
8213 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
8214 {
8215 int j, len = TREE_VEC_LENGTH (packed_parms);
8216 for (j = 0; j < len; ++j)
8217 {
8218 tree t = TREE_VEC_ELT (packed_parms, j);
8219 if (TREE_CODE (t) == PARM_DECL
8220 && invalid_nontype_parm_type_p (TREE_TYPE (t), complain))
8221 return error_mark_node;
8222 }
8223 /* We don't know how many args we have yet, just
8224 use the unconverted ones for now. */
8225 return NULL_TREE;
8226 }
8227
8228 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
8229 }
8230 /* Check if we have a placeholder pack, which indicates we're
8231 in the context of a introduction list. In that case we want
8232 to match this pack to the single placeholder. */
8233 else if (arg_idx < nargs
8234 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
8235 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
8236 {
8237 nargs = arg_idx + 1;
8238 packed_args = make_tree_vec (1);
8239 }
8240 else
8241 packed_args = make_tree_vec (nargs - arg_idx);
8242
8243 /* Convert the remaining arguments, which will be a part of the
8244 parameter pack "parm". */
8245 int first_pack_arg = arg_idx;
8246 for (; arg_idx < nargs; ++arg_idx)
8247 {
8248 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
8249 tree actual_parm = TREE_VALUE (parm);
8250 int pack_idx = arg_idx - first_pack_arg;
8251
8252 if (packed_parms)
8253 {
8254 /* Once we've packed as many args as we have types, stop. */
8255 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
8256 break;
8257 else if (PACK_EXPANSION_P (arg))
8258 /* We don't know how many args we have yet, just
8259 use the unconverted ones for now. */
8260 return NULL_TREE;
8261 else
8262 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
8263 }
8264
8265 if (arg == error_mark_node)
8266 {
8267 if (complain & tf_error)
8268 error ("template argument %d is invalid", arg_idx + 1);
8269 }
8270 else
8271 arg = convert_template_argument (actual_parm,
8272 arg, new_args, complain, parm_idx,
8273 in_decl);
8274 if (arg == error_mark_node)
8275 (*lost)++;
8276 TREE_VEC_ELT (packed_args, pack_idx) = arg;
8277 }
8278
8279 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
8280 && TREE_VEC_LENGTH (packed_args) > 0)
8281 {
8282 if (complain & tf_error)
8283 error ("wrong number of template arguments (%d, should be %d)",
8284 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
8285 return error_mark_node;
8286 }
8287
8288 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
8289 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
8290 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
8291 else
8292 {
8293 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
8294 TREE_CONSTANT (argument_pack) = 1;
8295 }
8296
8297 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
8298 if (CHECKING_P)
8299 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
8300 TREE_VEC_LENGTH (packed_args));
8301 return argument_pack;
8302 }
8303
8304 /* Returns the number of pack expansions in the template argument vector
8305 ARGS. */
8306
8307 static int
8308 pack_expansion_args_count (tree args)
8309 {
8310 int i;
8311 int count = 0;
8312 if (args)
8313 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8314 {
8315 tree elt = TREE_VEC_ELT (args, i);
8316 if (elt && PACK_EXPANSION_P (elt))
8317 ++count;
8318 }
8319 return count;
8320 }
8321
8322 /* Convert all template arguments to their appropriate types, and
8323 return a vector containing the innermost resulting template
8324 arguments. If any error occurs, return error_mark_node. Error and
8325 warning messages are issued under control of COMPLAIN.
8326
8327 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8328 for arguments not specified in ARGS. Otherwise, if
8329 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8330 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8331 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8332 ARGS. */
8333
8334 static tree
8335 coerce_template_parms (tree parms,
8336 tree args,
8337 tree in_decl,
8338 tsubst_flags_t complain,
8339 bool require_all_args,
8340 bool use_default_args)
8341 {
8342 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8343 tree orig_inner_args;
8344 tree inner_args;
8345 tree new_args;
8346 tree new_inner_args;
8347
8348 /* When used as a boolean value, indicates whether this is a
8349 variadic template parameter list. Since it's an int, we can also
8350 subtract it from nparms to get the number of non-variadic
8351 parameters. */
8352 int variadic_p = 0;
8353 int variadic_args_p = 0;
8354 int post_variadic_parms = 0;
8355
8356 /* Adjustment to nparms for fixed parameter packs. */
8357 int fixed_pack_adjust = 0;
8358 int fixed_packs = 0;
8359 int missing = 0;
8360
8361 /* Likewise for parameters with default arguments. */
8362 int default_p = 0;
8363
8364 if (args == error_mark_node)
8365 return error_mark_node;
8366
8367 nparms = TREE_VEC_LENGTH (parms);
8368
8369 /* Determine if there are any parameter packs or default arguments. */
8370 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8371 {
8372 tree parm = TREE_VEC_ELT (parms, parm_idx);
8373 if (variadic_p)
8374 ++post_variadic_parms;
8375 if (template_parameter_pack_p (TREE_VALUE (parm)))
8376 ++variadic_p;
8377 if (TREE_PURPOSE (parm))
8378 ++default_p;
8379 }
8380
8381 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8382 /* If there are no parameters that follow a parameter pack, we need to
8383 expand any argument packs so that we can deduce a parameter pack from
8384 some non-packed args followed by an argument pack, as in variadic85.C.
8385 If there are such parameters, we need to leave argument packs intact
8386 so the arguments are assigned properly. This can happen when dealing
8387 with a nested class inside a partial specialization of a class
8388 template, as in variadic92.C, or when deducing a template parameter pack
8389 from a sub-declarator, as in variadic114.C. */
8390 if (!post_variadic_parms)
8391 inner_args = expand_template_argument_pack (inner_args);
8392
8393 /* Count any pack expansion args. */
8394 variadic_args_p = pack_expansion_args_count (inner_args);
8395
8396 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8397 if ((nargs - variadic_args_p > nparms && !variadic_p)
8398 || (nargs < nparms - variadic_p
8399 && require_all_args
8400 && !variadic_args_p
8401 && (!use_default_args
8402 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8403 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8404 {
8405 bad_nargs:
8406 if (complain & tf_error)
8407 {
8408 if (variadic_p || default_p)
8409 {
8410 nparms -= variadic_p + default_p;
8411 error ("wrong number of template arguments "
8412 "(%d, should be at least %d)", nargs, nparms);
8413 }
8414 else
8415 error ("wrong number of template arguments "
8416 "(%d, should be %d)", nargs, nparms);
8417
8418 if (in_decl)
8419 inform (DECL_SOURCE_LOCATION (in_decl),
8420 "provided for %qD", in_decl);
8421 }
8422
8423 return error_mark_node;
8424 }
8425 /* We can't pass a pack expansion to a non-pack parameter of an alias
8426 template (DR 1430). */
8427 else if (in_decl
8428 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8429 || concept_template_p (in_decl))
8430 && variadic_args_p
8431 && nargs - variadic_args_p < nparms - variadic_p)
8432 {
8433 if (complain & tf_error)
8434 {
8435 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8436 {
8437 tree arg = TREE_VEC_ELT (inner_args, i);
8438 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8439
8440 if (PACK_EXPANSION_P (arg)
8441 && !template_parameter_pack_p (parm))
8442 {
8443 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8444 error_at (location_of (arg),
8445 "pack expansion argument for non-pack parameter "
8446 "%qD of alias template %qD", parm, in_decl);
8447 else
8448 error_at (location_of (arg),
8449 "pack expansion argument for non-pack parameter "
8450 "%qD of concept %qD", parm, in_decl);
8451 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8452 goto found;
8453 }
8454 }
8455 gcc_unreachable ();
8456 found:;
8457 }
8458 return error_mark_node;
8459 }
8460
8461 /* We need to evaluate the template arguments, even though this
8462 template-id may be nested within a "sizeof". */
8463 cp_evaluated ev;
8464
8465 new_inner_args = make_tree_vec (nparms);
8466 new_args = add_outermost_template_args (args, new_inner_args);
8467 int pack_adjust = 0;
8468 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8469 {
8470 tree arg;
8471 tree parm;
8472
8473 /* Get the Ith template parameter. */
8474 parm = TREE_VEC_ELT (parms, parm_idx);
8475
8476 if (parm == error_mark_node)
8477 {
8478 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8479 continue;
8480 }
8481
8482 /* Calculate the next argument. */
8483 if (arg_idx < nargs)
8484 arg = TREE_VEC_ELT (inner_args, arg_idx);
8485 else
8486 arg = NULL_TREE;
8487
8488 if (template_parameter_pack_p (TREE_VALUE (parm))
8489 && (arg || require_all_args || !(complain & tf_partial))
8490 && !(arg && ARGUMENT_PACK_P (arg)))
8491 {
8492 /* Some arguments will be placed in the
8493 template parameter pack PARM. */
8494 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8495 inner_args, arg_idx,
8496 new_args, &lost,
8497 in_decl, complain);
8498
8499 if (arg == NULL_TREE)
8500 {
8501 /* We don't know how many args we have yet, just use the
8502 unconverted (and still packed) ones for now. */
8503 new_inner_args = orig_inner_args;
8504 arg_idx = nargs;
8505 break;
8506 }
8507
8508 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8509
8510 /* Store this argument. */
8511 if (arg == error_mark_node)
8512 {
8513 lost++;
8514 /* We are done with all of the arguments. */
8515 arg_idx = nargs;
8516 break;
8517 }
8518 else
8519 {
8520 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8521 arg_idx += pack_adjust;
8522 if (fixed_parameter_pack_p (TREE_VALUE (parm)))
8523 {
8524 ++fixed_packs;
8525 fixed_pack_adjust += pack_adjust;
8526 }
8527 }
8528
8529 continue;
8530 }
8531 else if (arg)
8532 {
8533 if (PACK_EXPANSION_P (arg))
8534 {
8535 /* "If every valid specialization of a variadic template
8536 requires an empty template parameter pack, the template is
8537 ill-formed, no diagnostic required." So check that the
8538 pattern works with this parameter. */
8539 tree pattern = PACK_EXPANSION_PATTERN (arg);
8540 tree conv = convert_template_argument (TREE_VALUE (parm),
8541 pattern, new_args,
8542 complain, parm_idx,
8543 in_decl);
8544 if (conv == error_mark_node)
8545 {
8546 if (complain & tf_error)
8547 inform (input_location, "so any instantiation with a "
8548 "non-empty parameter pack would be ill-formed");
8549 ++lost;
8550 }
8551 else if (TYPE_P (conv) && !TYPE_P (pattern))
8552 /* Recover from missing typename. */
8553 TREE_VEC_ELT (inner_args, arg_idx)
8554 = make_pack_expansion (conv, complain);
8555
8556 /* We don't know how many args we have yet, just
8557 use the unconverted ones for now. */
8558 new_inner_args = inner_args;
8559 arg_idx = nargs;
8560 break;
8561 }
8562 }
8563 else if (require_all_args)
8564 {
8565 /* There must be a default arg in this case. */
8566 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8567 complain, in_decl);
8568 /* The position of the first default template argument,
8569 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8570 Record that. */
8571 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8572 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8573 arg_idx - pack_adjust);
8574 }
8575 else
8576 break;
8577
8578 if (arg == error_mark_node)
8579 {
8580 if (complain & tf_error)
8581 error ("template argument %d is invalid", arg_idx + 1);
8582 }
8583 else if (!arg)
8584 {
8585 /* This can occur if there was an error in the template
8586 parameter list itself (which we would already have
8587 reported) that we are trying to recover from, e.g., a class
8588 template with a parameter list such as
8589 template<typename..., typename> (cpp0x/variadic150.C). */
8590 ++lost;
8591
8592 /* This can also happen with a fixed parameter pack (71834). */
8593 if (arg_idx >= nargs)
8594 ++missing;
8595 }
8596 else
8597 arg = convert_template_argument (TREE_VALUE (parm),
8598 arg, new_args, complain,
8599 parm_idx, in_decl);
8600
8601 if (arg == error_mark_node)
8602 lost++;
8603 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8604 }
8605
8606 if (missing || arg_idx < nargs - variadic_args_p)
8607 {
8608 /* If we had fixed parameter packs, we didn't know how many arguments we
8609 actually needed earlier; now we do. */
8610 nparms += fixed_pack_adjust;
8611 variadic_p -= fixed_packs;
8612 goto bad_nargs;
8613 }
8614
8615 if (arg_idx < nargs)
8616 {
8617 /* We had some pack expansion arguments that will only work if the packs
8618 are empty, but wait until instantiation time to complain.
8619 See variadic-ttp3.C. */
8620 int len = nparms + (nargs - arg_idx);
8621 tree args = make_tree_vec (len);
8622 int i = 0;
8623 for (; i < nparms; ++i)
8624 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (new_inner_args, i);
8625 for (; i < len; ++i, ++arg_idx)
8626 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (inner_args,
8627 arg_idx - pack_adjust);
8628 new_inner_args = args;
8629 }
8630
8631 if (lost)
8632 {
8633 gcc_assert (!(complain & tf_error) || seen_error ());
8634 return error_mark_node;
8635 }
8636
8637 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8638 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8639 TREE_VEC_LENGTH (new_inner_args));
8640
8641 return new_inner_args;
8642 }
8643
8644 /* Convert all template arguments to their appropriate types, and
8645 return a vector containing the innermost resulting template
8646 arguments. If any error occurs, return error_mark_node. Error and
8647 warning messages are not issued.
8648
8649 Note that no function argument deduction is performed, and default
8650 arguments are used to fill in unspecified arguments. */
8651 tree
8652 coerce_template_parms (tree parms, tree args, tree in_decl)
8653 {
8654 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8655 }
8656
8657 /* Convert all template arguments to their appropriate type, and
8658 instantiate default arguments as needed. This returns a vector
8659 containing the innermost resulting template arguments, or
8660 error_mark_node if unsuccessful. */
8661 tree
8662 coerce_template_parms (tree parms, tree args, tree in_decl,
8663 tsubst_flags_t complain)
8664 {
8665 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8666 }
8667
8668 /* Like coerce_template_parms. If PARMS represents all template
8669 parameters levels, this function returns a vector of vectors
8670 representing all the resulting argument levels. Note that in this
8671 case, only the innermost arguments are coerced because the
8672 outermost ones are supposed to have been coerced already.
8673
8674 Otherwise, if PARMS represents only (the innermost) vector of
8675 parameters, this function returns a vector containing just the
8676 innermost resulting arguments. */
8677
8678 static tree
8679 coerce_innermost_template_parms (tree parms,
8680 tree args,
8681 tree in_decl,
8682 tsubst_flags_t complain,
8683 bool require_all_args,
8684 bool use_default_args)
8685 {
8686 int parms_depth = TMPL_PARMS_DEPTH (parms);
8687 int args_depth = TMPL_ARGS_DEPTH (args);
8688 tree coerced_args;
8689
8690 if (parms_depth > 1)
8691 {
8692 coerced_args = make_tree_vec (parms_depth);
8693 tree level;
8694 int cur_depth;
8695
8696 for (level = parms, cur_depth = parms_depth;
8697 parms_depth > 0 && level != NULL_TREE;
8698 level = TREE_CHAIN (level), --cur_depth)
8699 {
8700 tree l;
8701 if (cur_depth == args_depth)
8702 l = coerce_template_parms (TREE_VALUE (level),
8703 args, in_decl, complain,
8704 require_all_args,
8705 use_default_args);
8706 else
8707 l = TMPL_ARGS_LEVEL (args, cur_depth);
8708
8709 if (l == error_mark_node)
8710 return error_mark_node;
8711
8712 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8713 }
8714 }
8715 else
8716 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8717 args, in_decl, complain,
8718 require_all_args,
8719 use_default_args);
8720 return coerced_args;
8721 }
8722
8723 /* Returns 1 if template args OT and NT are equivalent. */
8724
8725 int
8726 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
8727 {
8728 if (nt == ot)
8729 return 1;
8730 if (nt == NULL_TREE || ot == NULL_TREE)
8731 return false;
8732 if (nt == any_targ_node || ot == any_targ_node)
8733 return true;
8734
8735 if (TREE_CODE (nt) == TREE_VEC)
8736 /* For member templates */
8737 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8738 else if (PACK_EXPANSION_P (ot))
8739 return (PACK_EXPANSION_P (nt)
8740 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8741 PACK_EXPANSION_PATTERN (nt))
8742 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8743 PACK_EXPANSION_EXTRA_ARGS (nt)));
8744 else if (ARGUMENT_PACK_P (ot))
8745 {
8746 int i, len;
8747 tree opack, npack;
8748
8749 if (!ARGUMENT_PACK_P (nt))
8750 return 0;
8751
8752 opack = ARGUMENT_PACK_ARGS (ot);
8753 npack = ARGUMENT_PACK_ARGS (nt);
8754 len = TREE_VEC_LENGTH (opack);
8755 if (TREE_VEC_LENGTH (npack) != len)
8756 return 0;
8757 for (i = 0; i < len; ++i)
8758 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8759 TREE_VEC_ELT (npack, i)))
8760 return 0;
8761 return 1;
8762 }
8763 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8764 gcc_unreachable ();
8765 else if (TYPE_P (nt))
8766 {
8767 if (!TYPE_P (ot))
8768 return false;
8769 /* Don't treat an alias template specialization with dependent
8770 arguments as equivalent to its underlying type when used as a
8771 template argument; we need them to be distinct so that we
8772 substitute into the specialization arguments at instantiation
8773 time. And aliases can't be equivalent without being ==, so
8774 we don't need to look any deeper.
8775
8776 During partial ordering, however, we need to treat them normally so
8777 that we can order uses of the same alias with different
8778 cv-qualification (79960). */
8779 if (!partial_order
8780 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
8781 return false;
8782 else
8783 return same_type_p (ot, nt);
8784 }
8785 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8786 return 0;
8787 else
8788 {
8789 /* Try to treat a template non-type argument that has been converted
8790 to the parameter type as equivalent to one that hasn't yet. */
8791 for (enum tree_code code1 = TREE_CODE (ot);
8792 CONVERT_EXPR_CODE_P (code1)
8793 || code1 == NON_LVALUE_EXPR;
8794 code1 = TREE_CODE (ot))
8795 ot = TREE_OPERAND (ot, 0);
8796 for (enum tree_code code2 = TREE_CODE (nt);
8797 CONVERT_EXPR_CODE_P (code2)
8798 || code2 == NON_LVALUE_EXPR;
8799 code2 = TREE_CODE (nt))
8800 nt = TREE_OPERAND (nt, 0);
8801
8802 return cp_tree_equal (ot, nt);
8803 }
8804 }
8805
8806 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8807 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8808 NEWARG_PTR with the offending arguments if they are non-NULL. */
8809
8810 int
8811 comp_template_args (tree oldargs, tree newargs,
8812 tree *oldarg_ptr, tree *newarg_ptr,
8813 bool partial_order)
8814 {
8815 int i;
8816
8817 if (oldargs == newargs)
8818 return 1;
8819
8820 if (!oldargs || !newargs)
8821 return 0;
8822
8823 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
8824 return 0;
8825
8826 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
8827 {
8828 tree nt = TREE_VEC_ELT (newargs, i);
8829 tree ot = TREE_VEC_ELT (oldargs, i);
8830
8831 if (! template_args_equal (ot, nt, partial_order))
8832 {
8833 if (oldarg_ptr != NULL)
8834 *oldarg_ptr = ot;
8835 if (newarg_ptr != NULL)
8836 *newarg_ptr = nt;
8837 return 0;
8838 }
8839 }
8840 return 1;
8841 }
8842
8843 inline bool
8844 comp_template_args_porder (tree oargs, tree nargs)
8845 {
8846 return comp_template_args (oargs, nargs, NULL, NULL, true);
8847 }
8848
8849 /* Implement a freelist interface for objects of type T.
8850
8851 Head is a separate object, rather than a regular member, so that we
8852 can define it as a GTY deletable pointer, which is highly
8853 desirable. A data member could be declared that way, but then the
8854 containing object would implicitly get GTY((user)), which would
8855 prevent us from instantiating freelists as global objects.
8856 Although this way we can create freelist global objects, they're
8857 such thin wrappers that instantiating temporaries at every use
8858 loses nothing and saves permanent storage for the freelist object.
8859
8860 Member functions next, anew, poison and reinit have default
8861 implementations that work for most of the types we're interested
8862 in, but if they don't work for some type, they should be explicitly
8863 specialized. See the comments before them for requirements, and
8864 the example specializations for the tree_list_freelist. */
8865 template <typename T>
8866 class freelist
8867 {
8868 /* Return the next object in a chain. We could just do type
8869 punning, but if we access the object with its underlying type, we
8870 avoid strict-aliasing trouble. This needs only work between
8871 poison and reinit. */
8872 static T *&next (T *obj) { return obj->next; }
8873
8874 /* Return a newly allocated, uninitialized or minimally-initialized
8875 object of type T. Any initialization performed by anew should
8876 either remain across the life of the object and the execution of
8877 poison, or be redone by reinit. */
8878 static T *anew () { return ggc_alloc<T> (); }
8879
8880 /* Optionally scribble all over the bits holding the object, so that
8881 they become (mostly?) uninitialized memory. This is called while
8882 preparing to make the object part of the free list. */
8883 static void poison (T *obj) {
8884 T *p ATTRIBUTE_UNUSED = obj;
8885 T **q ATTRIBUTE_UNUSED = &next (obj);
8886
8887 #ifdef ENABLE_GC_CHECKING
8888 /* Poison the data, to indicate the data is garbage. */
8889 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, sizeof (*p)));
8890 memset (p, 0xa5, sizeof (*p));
8891 #endif
8892 /* Let valgrind know the object is free. */
8893 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, sizeof (*p)));
8894
8895 /* Let valgrind know the next portion of the object is available,
8896 but uninitialized. */
8897 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
8898 }
8899
8900 /* Bring an object that underwent at least one lifecycle after anew
8901 and before the most recent free and poison, back to a usable
8902 state, reinitializing whatever is needed for it to be
8903 functionally equivalent to an object just allocated and returned
8904 by anew. This may poison or clear the next field, used by
8905 freelist housekeeping after poison was called. */
8906 static void reinit (T *obj) {
8907 T **q ATTRIBUTE_UNUSED = &next (obj);
8908
8909 #ifdef ENABLE_GC_CHECKING
8910 memset (q, 0xa5, sizeof (*q));
8911 #endif
8912 /* Let valgrind know the entire object is available, but
8913 uninitialized. */
8914 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (*obj)));
8915 }
8916
8917 /* Reference a GTY-deletable pointer that points to the first object
8918 in the free list proper. */
8919 T *&head;
8920 public:
8921 /* Construct a freelist object chaining objects off of HEAD. */
8922 freelist (T *&head) : head(head) {}
8923
8924 /* Add OBJ to the free object list. The former head becomes OBJ's
8925 successor. */
8926 void free (T *obj)
8927 {
8928 poison (obj);
8929 next (obj) = head;
8930 head = obj;
8931 }
8932
8933 /* Take an object from the free list, if one is available, or
8934 allocate a new one. Objects taken from the free list should be
8935 regarded as filled with garbage, except for bits that are
8936 configured to be preserved across free and alloc. */
8937 T *alloc ()
8938 {
8939 if (head)
8940 {
8941 T *obj = head;
8942 head = next (head);
8943 reinit (obj);
8944 return obj;
8945 }
8946 else
8947 return anew ();
8948 }
8949 };
8950
8951 /* Explicitly specialize the interfaces for freelist<tree_node>: we
8952 want to allocate a TREE_LIST using the usual interface, and ensure
8953 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
8954 build_tree_list logic in reinit, so this could go out of sync. */
8955 template <>
8956 inline tree &
8957 freelist<tree_node>::next (tree obj)
8958 {
8959 return TREE_CHAIN (obj);
8960 }
8961 template <>
8962 inline tree
8963 freelist<tree_node>::anew ()
8964 {
8965 return build_tree_list (NULL, NULL);
8966 }
8967 template <>
8968 inline void
8969 freelist<tree_node>::poison (tree obj ATTRIBUTE_UNUSED)
8970 {
8971 int size ATTRIBUTE_UNUSED = sizeof (tree_list);
8972 tree p ATTRIBUTE_UNUSED = obj;
8973 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
8974 tree *q ATTRIBUTE_UNUSED = &next (obj);
8975
8976 #ifdef ENABLE_GC_CHECKING
8977 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
8978
8979 /* Poison the data, to indicate the data is garbage. */
8980 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, size));
8981 memset (p, 0xa5, size);
8982 #endif
8983 /* Let valgrind know the object is free. */
8984 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, size));
8985 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
8986 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
8987 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
8988
8989 #ifdef ENABLE_GC_CHECKING
8990 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b, sizeof (*b)));
8991 /* Keep TREE_CHAIN functional. */
8992 TREE_SET_CODE (obj, TREE_LIST);
8993 #else
8994 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
8995 #endif
8996 }
8997 template <>
8998 inline void
8999 freelist<tree_node>::reinit (tree obj ATTRIBUTE_UNUSED)
9000 {
9001 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9002
9003 #ifdef ENABLE_GC_CHECKING
9004 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9005 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9006 memset (obj, 0, sizeof (tree_list));
9007 #endif
9008
9009 /* Let valgrind know the entire object is available, but
9010 uninitialized. */
9011 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9012
9013 #ifdef ENABLE_GC_CHECKING
9014 TREE_SET_CODE (obj, TREE_LIST);
9015 #else
9016 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9017 #endif
9018 }
9019
9020 /* Point to the first object in the TREE_LIST freelist. */
9021 static GTY((deletable)) tree tree_list_freelist_head;
9022 /* Return the/an actual TREE_LIST freelist. */
9023 static inline freelist<tree_node>
9024 tree_list_freelist ()
9025 {
9026 return tree_list_freelist_head;
9027 }
9028
9029 /* Point to the first object in the tinst_level freelist. */
9030 static GTY((deletable)) tinst_level *tinst_level_freelist_head;
9031 /* Return the/an actual tinst_level freelist. */
9032 static inline freelist<tinst_level>
9033 tinst_level_freelist ()
9034 {
9035 return tinst_level_freelist_head;
9036 }
9037
9038 /* Point to the first object in the pending_template freelist. */
9039 static GTY((deletable)) pending_template *pending_template_freelist_head;
9040 /* Return the/an actual pending_template freelist. */
9041 static inline freelist<pending_template>
9042 pending_template_freelist ()
9043 {
9044 return pending_template_freelist_head;
9045 }
9046
9047 /* Build the TREE_LIST object out of a split list, store it
9048 permanently, and return it. */
9049 tree
9050 tinst_level::to_list ()
9051 {
9052 gcc_assert (split_list_p ());
9053 tree ret = tree_list_freelist ().alloc ();
9054 TREE_PURPOSE (ret) = tldcl;
9055 TREE_VALUE (ret) = targs;
9056 tldcl = ret;
9057 targs = NULL;
9058 gcc_assert (tree_list_p ());
9059 return ret;
9060 }
9061
9062 const unsigned short tinst_level::refcount_infinity;
9063
9064 /* Increment OBJ's refcount unless it is already infinite. */
9065 static tinst_level *
9066 inc_refcount_use (tinst_level *obj)
9067 {
9068 if (obj && obj->refcount != tinst_level::refcount_infinity)
9069 ++obj->refcount;
9070 return obj;
9071 }
9072
9073 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9074 void
9075 tinst_level::free (tinst_level *obj)
9076 {
9077 if (obj->tree_list_p ())
9078 tree_list_freelist ().free (obj->get_node ());
9079 tinst_level_freelist ().free (obj);
9080 }
9081
9082 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9083 OBJ's DECL and OBJ, and start over with the tinst_level object that
9084 used to be referenced by OBJ's NEXT. */
9085 static void
9086 dec_refcount_use (tinst_level *obj)
9087 {
9088 while (obj
9089 && obj->refcount != tinst_level::refcount_infinity
9090 && !--obj->refcount)
9091 {
9092 tinst_level *next = obj->next;
9093 tinst_level::free (obj);
9094 obj = next;
9095 }
9096 }
9097
9098 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9099 and of the former PTR. Omitting the second argument is equivalent
9100 to passing (T*)NULL; this is allowed because passing the
9101 zero-valued integral constant NULL confuses type deduction and/or
9102 overload resolution. */
9103 template <typename T>
9104 static void
9105 set_refcount_ptr (T *& ptr, T *obj = NULL)
9106 {
9107 T *save = ptr;
9108 ptr = inc_refcount_use (obj);
9109 dec_refcount_use (save);
9110 }
9111
9112 static void
9113 add_pending_template (tree d)
9114 {
9115 tree ti = (TYPE_P (d)
9116 ? CLASSTYPE_TEMPLATE_INFO (d)
9117 : DECL_TEMPLATE_INFO (d));
9118 struct pending_template *pt;
9119 int level;
9120
9121 if (TI_PENDING_TEMPLATE_FLAG (ti))
9122 return;
9123
9124 /* We are called both from instantiate_decl, where we've already had a
9125 tinst_level pushed, and instantiate_template, where we haven't.
9126 Compensate. */
9127 gcc_assert (TREE_CODE (d) != TREE_LIST);
9128 level = !current_tinst_level
9129 || current_tinst_level->maybe_get_node () != d;
9130
9131 if (level)
9132 push_tinst_level (d);
9133
9134 pt = pending_template_freelist ().alloc ();
9135 pt->next = NULL;
9136 pt->tinst = NULL;
9137 set_refcount_ptr (pt->tinst, current_tinst_level);
9138 if (last_pending_template)
9139 last_pending_template->next = pt;
9140 else
9141 pending_templates = pt;
9142
9143 last_pending_template = pt;
9144
9145 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
9146
9147 if (level)
9148 pop_tinst_level ();
9149 }
9150
9151
9152 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9153 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9154 documentation for TEMPLATE_ID_EXPR. */
9155
9156 tree
9157 lookup_template_function (tree fns, tree arglist)
9158 {
9159 if (fns == error_mark_node || arglist == error_mark_node)
9160 return error_mark_node;
9161
9162 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
9163
9164 if (!is_overloaded_fn (fns) && !identifier_p (fns))
9165 {
9166 error ("%q#D is not a function template", fns);
9167 return error_mark_node;
9168 }
9169
9170 if (BASELINK_P (fns))
9171 {
9172 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
9173 unknown_type_node,
9174 BASELINK_FUNCTIONS (fns),
9175 arglist);
9176 return fns;
9177 }
9178
9179 return build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, arglist);
9180 }
9181
9182 /* Within the scope of a template class S<T>, the name S gets bound
9183 (in build_self_reference) to a TYPE_DECL for the class, not a
9184 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9185 or one of its enclosing classes, and that type is a template,
9186 return the associated TEMPLATE_DECL. Otherwise, the original
9187 DECL is returned.
9188
9189 Also handle the case when DECL is a TREE_LIST of ambiguous
9190 injected-class-names from different bases. */
9191
9192 tree
9193 maybe_get_template_decl_from_type_decl (tree decl)
9194 {
9195 if (decl == NULL_TREE)
9196 return decl;
9197
9198 /* DR 176: A lookup that finds an injected-class-name (10.2
9199 [class.member.lookup]) can result in an ambiguity in certain cases
9200 (for example, if it is found in more than one base class). If all of
9201 the injected-class-names that are found refer to specializations of
9202 the same class template, and if the name is followed by a
9203 template-argument-list, the reference refers to the class template
9204 itself and not a specialization thereof, and is not ambiguous. */
9205 if (TREE_CODE (decl) == TREE_LIST)
9206 {
9207 tree t, tmpl = NULL_TREE;
9208 for (t = decl; t; t = TREE_CHAIN (t))
9209 {
9210 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
9211 if (!tmpl)
9212 tmpl = elt;
9213 else if (tmpl != elt)
9214 break;
9215 }
9216 if (tmpl && t == NULL_TREE)
9217 return tmpl;
9218 else
9219 return decl;
9220 }
9221
9222 return (decl != NULL_TREE
9223 && DECL_SELF_REFERENCE_P (decl)
9224 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
9225 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
9226 }
9227
9228 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9229 parameters, find the desired type.
9230
9231 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9232
9233 IN_DECL, if non-NULL, is the template declaration we are trying to
9234 instantiate.
9235
9236 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9237 the class we are looking up.
9238
9239 Issue error and warning messages under control of COMPLAIN.
9240
9241 If the template class is really a local class in a template
9242 function, then the FUNCTION_CONTEXT is the function in which it is
9243 being instantiated.
9244
9245 ??? Note that this function is currently called *twice* for each
9246 template-id: the first time from the parser, while creating the
9247 incomplete type (finish_template_type), and the second type during the
9248 real instantiation (instantiate_template_class). This is surely something
9249 that we want to avoid. It also causes some problems with argument
9250 coercion (see convert_nontype_argument for more information on this). */
9251
9252 static tree
9253 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
9254 int entering_scope, tsubst_flags_t complain)
9255 {
9256 tree templ = NULL_TREE, parmlist;
9257 tree t;
9258 spec_entry **slot;
9259 spec_entry *entry;
9260 spec_entry elt;
9261 hashval_t hash;
9262
9263 if (identifier_p (d1))
9264 {
9265 tree value = innermost_non_namespace_value (d1);
9266 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
9267 templ = value;
9268 else
9269 {
9270 if (context)
9271 push_decl_namespace (context);
9272 templ = lookup_name (d1);
9273 templ = maybe_get_template_decl_from_type_decl (templ);
9274 if (context)
9275 pop_decl_namespace ();
9276 }
9277 if (templ)
9278 context = DECL_CONTEXT (templ);
9279 }
9280 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
9281 {
9282 tree type = TREE_TYPE (d1);
9283
9284 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9285 an implicit typename for the second A. Deal with it. */
9286 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
9287 type = TREE_TYPE (type);
9288
9289 if (CLASSTYPE_TEMPLATE_INFO (type))
9290 {
9291 templ = CLASSTYPE_TI_TEMPLATE (type);
9292 d1 = DECL_NAME (templ);
9293 }
9294 }
9295 else if (TREE_CODE (d1) == ENUMERAL_TYPE
9296 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
9297 {
9298 templ = TYPE_TI_TEMPLATE (d1);
9299 d1 = DECL_NAME (templ);
9300 }
9301 else if (DECL_TYPE_TEMPLATE_P (d1))
9302 {
9303 templ = d1;
9304 d1 = DECL_NAME (templ);
9305 context = DECL_CONTEXT (templ);
9306 }
9307 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
9308 {
9309 templ = d1;
9310 d1 = DECL_NAME (templ);
9311 }
9312
9313 /* Issue an error message if we didn't find a template. */
9314 if (! templ)
9315 {
9316 if (complain & tf_error)
9317 error ("%qT is not a template", d1);
9318 return error_mark_node;
9319 }
9320
9321 if (TREE_CODE (templ) != TEMPLATE_DECL
9322 /* Make sure it's a user visible template, if it was named by
9323 the user. */
9324 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
9325 && !PRIMARY_TEMPLATE_P (templ)))
9326 {
9327 if (complain & tf_error)
9328 {
9329 error ("non-template type %qT used as a template", d1);
9330 if (in_decl)
9331 error ("for template declaration %q+D", in_decl);
9332 }
9333 return error_mark_node;
9334 }
9335
9336 complain &= ~tf_user;
9337
9338 /* An alias that just changes the name of a template is equivalent to the
9339 other template, so if any of the arguments are pack expansions, strip
9340 the alias to avoid problems with a pack expansion passed to a non-pack
9341 alias template parameter (DR 1430). */
9342 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
9343 templ = get_underlying_template (templ);
9344
9345 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
9346 {
9347 tree parm;
9348 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
9349 if (arglist2 == error_mark_node
9350 || (!uses_template_parms (arglist2)
9351 && check_instantiated_args (templ, arglist2, complain)))
9352 return error_mark_node;
9353
9354 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
9355 return parm;
9356 }
9357 else
9358 {
9359 tree template_type = TREE_TYPE (templ);
9360 tree gen_tmpl;
9361 tree type_decl;
9362 tree found = NULL_TREE;
9363 int arg_depth;
9364 int parm_depth;
9365 int is_dependent_type;
9366 int use_partial_inst_tmpl = false;
9367
9368 if (template_type == error_mark_node)
9369 /* An error occurred while building the template TEMPL, and a
9370 diagnostic has most certainly been emitted for that
9371 already. Let's propagate that error. */
9372 return error_mark_node;
9373
9374 gen_tmpl = most_general_template (templ);
9375 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
9376 parm_depth = TMPL_PARMS_DEPTH (parmlist);
9377 arg_depth = TMPL_ARGS_DEPTH (arglist);
9378
9379 if (arg_depth == 1 && parm_depth > 1)
9380 {
9381 /* We've been given an incomplete set of template arguments.
9382 For example, given:
9383
9384 template <class T> struct S1 {
9385 template <class U> struct S2 {};
9386 template <class U> struct S2<U*> {};
9387 };
9388
9389 we will be called with an ARGLIST of `U*', but the
9390 TEMPLATE will be `template <class T> template
9391 <class U> struct S1<T>::S2'. We must fill in the missing
9392 arguments. */
9393 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
9394 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
9395 arg_depth = TMPL_ARGS_DEPTH (arglist);
9396 }
9397
9398 /* Now we should have enough arguments. */
9399 gcc_assert (parm_depth == arg_depth);
9400
9401 /* From here on, we're only interested in the most general
9402 template. */
9403
9404 /* Calculate the BOUND_ARGS. These will be the args that are
9405 actually tsubst'd into the definition to create the
9406 instantiation. */
9407 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
9408 complain,
9409 /*require_all_args=*/true,
9410 /*use_default_args=*/true);
9411
9412 if (arglist == error_mark_node)
9413 /* We were unable to bind the arguments. */
9414 return error_mark_node;
9415
9416 /* In the scope of a template class, explicit references to the
9417 template class refer to the type of the template, not any
9418 instantiation of it. For example, in:
9419
9420 template <class T> class C { void f(C<T>); }
9421
9422 the `C<T>' is just the same as `C'. Outside of the
9423 class, however, such a reference is an instantiation. */
9424 if (entering_scope
9425 || !PRIMARY_TEMPLATE_P (gen_tmpl)
9426 || currently_open_class (template_type))
9427 {
9428 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
9429
9430 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
9431 return template_type;
9432 }
9433
9434 /* If we already have this specialization, return it. */
9435 elt.tmpl = gen_tmpl;
9436 elt.args = arglist;
9437 elt.spec = NULL_TREE;
9438 hash = spec_hasher::hash (&elt);
9439 entry = type_specializations->find_with_hash (&elt, hash);
9440
9441 if (entry)
9442 return entry->spec;
9443
9444 /* If the the template's constraints are not satisfied,
9445 then we cannot form a valid type.
9446
9447 Note that the check is deferred until after the hash
9448 lookup. This prevents redundant checks on previously
9449 instantiated specializations. */
9450 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
9451 {
9452 if (complain & tf_error)
9453 {
9454 auto_diagnostic_group d;
9455 error ("template constraint failure");
9456 diagnose_constraints (input_location, gen_tmpl, arglist);
9457 }
9458 return error_mark_node;
9459 }
9460
9461 is_dependent_type = uses_template_parms (arglist);
9462
9463 /* If the deduced arguments are invalid, then the binding
9464 failed. */
9465 if (!is_dependent_type
9466 && check_instantiated_args (gen_tmpl,
9467 INNERMOST_TEMPLATE_ARGS (arglist),
9468 complain))
9469 return error_mark_node;
9470
9471 if (!is_dependent_type
9472 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9473 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
9474 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
9475 {
9476 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
9477 DECL_NAME (gen_tmpl),
9478 /*tag_scope=*/ts_global);
9479 return found;
9480 }
9481
9482 context = DECL_CONTEXT (gen_tmpl);
9483 if (context && TYPE_P (context))
9484 {
9485 context = tsubst_aggr_type (context, arglist, complain, in_decl, true);
9486 context = complete_type (context);
9487 }
9488 else
9489 context = tsubst (context, arglist, complain, in_decl);
9490
9491 if (context == error_mark_node)
9492 return error_mark_node;
9493
9494 if (!context)
9495 context = global_namespace;
9496
9497 /* Create the type. */
9498 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9499 {
9500 /* The user referred to a specialization of an alias
9501 template represented by GEN_TMPL.
9502
9503 [temp.alias]/2 says:
9504
9505 When a template-id refers to the specialization of an
9506 alias template, it is equivalent to the associated
9507 type obtained by substitution of its
9508 template-arguments for the template-parameters in the
9509 type-id of the alias template. */
9510
9511 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
9512 /* Note that the call above (by indirectly calling
9513 register_specialization in tsubst_decl) registers the
9514 TYPE_DECL representing the specialization of the alias
9515 template. So next time someone substitutes ARGLIST for
9516 the template parms into the alias template (GEN_TMPL),
9517 she'll get that TYPE_DECL back. */
9518
9519 if (t == error_mark_node)
9520 return t;
9521 }
9522 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
9523 {
9524 if (!is_dependent_type)
9525 {
9526 set_current_access_from_decl (TYPE_NAME (template_type));
9527 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
9528 tsubst (ENUM_UNDERLYING_TYPE (template_type),
9529 arglist, complain, in_decl),
9530 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
9531 arglist, complain, in_decl),
9532 SCOPED_ENUM_P (template_type), NULL);
9533
9534 if (t == error_mark_node)
9535 return t;
9536 }
9537 else
9538 {
9539 /* We don't want to call start_enum for this type, since
9540 the values for the enumeration constants may involve
9541 template parameters. And, no one should be interested
9542 in the enumeration constants for such a type. */
9543 t = cxx_make_type (ENUMERAL_TYPE);
9544 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
9545 }
9546 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
9547 ENUM_FIXED_UNDERLYING_TYPE_P (t)
9548 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
9549 }
9550 else if (CLASS_TYPE_P (template_type))
9551 {
9552 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9553 instantiated here. */
9554 gcc_assert (!LAMBDA_TYPE_P (template_type));
9555
9556 t = make_class_type (TREE_CODE (template_type));
9557 CLASSTYPE_DECLARED_CLASS (t)
9558 = CLASSTYPE_DECLARED_CLASS (template_type);
9559 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
9560
9561 /* A local class. Make sure the decl gets registered properly. */
9562 if (context == current_function_decl)
9563 if (pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current)
9564 == error_mark_node)
9565 return error_mark_node;
9566
9567 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
9568 /* This instantiation is another name for the primary
9569 template type. Set the TYPE_CANONICAL field
9570 appropriately. */
9571 TYPE_CANONICAL (t) = template_type;
9572 else if (any_template_arguments_need_structural_equality_p (arglist))
9573 /* Some of the template arguments require structural
9574 equality testing, so this template class requires
9575 structural equality testing. */
9576 SET_TYPE_STRUCTURAL_EQUALITY (t);
9577 }
9578 else
9579 gcc_unreachable ();
9580
9581 /* If we called start_enum or pushtag above, this information
9582 will already be set up. */
9583 if (!TYPE_NAME (t))
9584 {
9585 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
9586
9587 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
9588 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
9589 DECL_SOURCE_LOCATION (type_decl)
9590 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
9591 }
9592 else
9593 type_decl = TYPE_NAME (t);
9594
9595 if (CLASS_TYPE_P (template_type))
9596 {
9597 TREE_PRIVATE (type_decl)
9598 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
9599 TREE_PROTECTED (type_decl)
9600 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
9601 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
9602 {
9603 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
9604 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
9605 }
9606 }
9607
9608 if (OVERLOAD_TYPE_P (t)
9609 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9610 {
9611 static const char *tags[] = {"abi_tag", "may_alias"};
9612
9613 for (unsigned ix = 0; ix != 2; ix++)
9614 {
9615 tree attributes
9616 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
9617
9618 if (attributes)
9619 TYPE_ATTRIBUTES (t)
9620 = tree_cons (TREE_PURPOSE (attributes),
9621 TREE_VALUE (attributes),
9622 TYPE_ATTRIBUTES (t));
9623 }
9624 }
9625
9626 /* Let's consider the explicit specialization of a member
9627 of a class template specialization that is implicitly instantiated,
9628 e.g.:
9629 template<class T>
9630 struct S
9631 {
9632 template<class U> struct M {}; //#0
9633 };
9634
9635 template<>
9636 template<>
9637 struct S<int>::M<char> //#1
9638 {
9639 int i;
9640 };
9641 [temp.expl.spec]/4 says this is valid.
9642
9643 In this case, when we write:
9644 S<int>::M<char> m;
9645
9646 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9647 the one of #0.
9648
9649 When we encounter #1, we want to store the partial instantiation
9650 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9651
9652 For all cases other than this "explicit specialization of member of a
9653 class template", we just want to store the most general template into
9654 the CLASSTYPE_TI_TEMPLATE of M.
9655
9656 This case of "explicit specialization of member of a class template"
9657 only happens when:
9658 1/ the enclosing class is an instantiation of, and therefore not
9659 the same as, the context of the most general template, and
9660 2/ we aren't looking at the partial instantiation itself, i.e.
9661 the innermost arguments are not the same as the innermost parms of
9662 the most general template.
9663
9664 So it's only when 1/ and 2/ happens that we want to use the partial
9665 instantiation of the member template in lieu of its most general
9666 template. */
9667
9668 if (PRIMARY_TEMPLATE_P (gen_tmpl)
9669 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
9670 /* the enclosing class must be an instantiation... */
9671 && CLASS_TYPE_P (context)
9672 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
9673 {
9674 TREE_VEC_LENGTH (arglist)--;
9675 ++processing_template_decl;
9676 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
9677 tree partial_inst_args =
9678 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
9679 arglist, complain, NULL_TREE);
9680 --processing_template_decl;
9681 TREE_VEC_LENGTH (arglist)++;
9682 if (partial_inst_args == error_mark_node)
9683 return error_mark_node;
9684 use_partial_inst_tmpl =
9685 /*...and we must not be looking at the partial instantiation
9686 itself. */
9687 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
9688 partial_inst_args);
9689 }
9690
9691 if (!use_partial_inst_tmpl)
9692 /* This case is easy; there are no member templates involved. */
9693 found = gen_tmpl;
9694 else
9695 {
9696 /* This is a full instantiation of a member template. Find
9697 the partial instantiation of which this is an instance. */
9698
9699 /* Temporarily reduce by one the number of levels in the ARGLIST
9700 so as to avoid comparing the last set of arguments. */
9701 TREE_VEC_LENGTH (arglist)--;
9702 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
9703 TREE_VEC_LENGTH (arglist)++;
9704 /* FOUND is either a proper class type, or an alias
9705 template specialization. In the later case, it's a
9706 TYPE_DECL, resulting from the substituting of arguments
9707 for parameters in the TYPE_DECL of the alias template
9708 done earlier. So be careful while getting the template
9709 of FOUND. */
9710 found = (TREE_CODE (found) == TEMPLATE_DECL
9711 ? found
9712 : (TREE_CODE (found) == TYPE_DECL
9713 ? DECL_TI_TEMPLATE (found)
9714 : CLASSTYPE_TI_TEMPLATE (found)));
9715
9716 if (DECL_CLASS_TEMPLATE_P (found)
9717 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found)))
9718 {
9719 /* If this partial instantiation is specialized, we want to
9720 use it for hash table lookup. */
9721 elt.tmpl = found;
9722 elt.args = arglist = INNERMOST_TEMPLATE_ARGS (arglist);
9723 hash = spec_hasher::hash (&elt);
9724 }
9725 }
9726
9727 // Build template info for the new specialization.
9728 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
9729
9730 elt.spec = t;
9731 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
9732 gcc_checking_assert (*slot == NULL);
9733 entry = ggc_alloc<spec_entry> ();
9734 *entry = elt;
9735 *slot = entry;
9736
9737 /* Note this use of the partial instantiation so we can check it
9738 later in maybe_process_partial_specialization. */
9739 DECL_TEMPLATE_INSTANTIATIONS (found)
9740 = tree_cons (arglist, t,
9741 DECL_TEMPLATE_INSTANTIATIONS (found));
9742
9743 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
9744 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9745 /* Now that the type has been registered on the instantiations
9746 list, we set up the enumerators. Because the enumeration
9747 constants may involve the enumeration type itself, we make
9748 sure to register the type first, and then create the
9749 constants. That way, doing tsubst_expr for the enumeration
9750 constants won't result in recursive calls here; we'll find
9751 the instantiation and exit above. */
9752 tsubst_enum (template_type, t, arglist);
9753
9754 if (CLASS_TYPE_P (template_type) && is_dependent_type)
9755 /* If the type makes use of template parameters, the
9756 code that generates debugging information will crash. */
9757 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
9758
9759 /* Possibly limit visibility based on template args. */
9760 TREE_PUBLIC (type_decl) = 1;
9761 determine_visibility (type_decl);
9762
9763 inherit_targ_abi_tags (t);
9764
9765 return t;
9766 }
9767 }
9768
9769 /* Wrapper for lookup_template_class_1. */
9770
9771 tree
9772 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
9773 int entering_scope, tsubst_flags_t complain)
9774 {
9775 tree ret;
9776 timevar_push (TV_TEMPLATE_INST);
9777 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
9778 entering_scope, complain);
9779 timevar_pop (TV_TEMPLATE_INST);
9780 return ret;
9781 }
9782
9783 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9784
9785 tree
9786 lookup_template_variable (tree templ, tree arglist)
9787 {
9788 /* The type of the expression is NULL_TREE since the template-id could refer
9789 to an explicit or partial specialization. */
9790 tree type = NULL_TREE;
9791 if (flag_concepts && variable_concept_p (templ))
9792 /* Except that concepts are always bool. */
9793 type = boolean_type_node;
9794 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
9795 }
9796
9797 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9798
9799 tree
9800 finish_template_variable (tree var, tsubst_flags_t complain)
9801 {
9802 tree templ = TREE_OPERAND (var, 0);
9803 tree arglist = TREE_OPERAND (var, 1);
9804
9805 /* We never want to return a VAR_DECL for a variable concept, since they
9806 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9807 bool concept_p = flag_concepts && variable_concept_p (templ);
9808 if (concept_p && processing_template_decl)
9809 return var;
9810
9811 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
9812 arglist = add_outermost_template_args (tmpl_args, arglist);
9813
9814 templ = most_general_template (templ);
9815 tree parms = DECL_TEMPLATE_PARMS (templ);
9816 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
9817 /*req_all*/true,
9818 /*use_default*/true);
9819
9820 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
9821 {
9822 if (complain & tf_error)
9823 {
9824 auto_diagnostic_group d;
9825 error ("use of invalid variable template %qE", var);
9826 diagnose_constraints (location_of (var), templ, arglist);
9827 }
9828 return error_mark_node;
9829 }
9830
9831 /* If a template-id refers to a specialization of a variable
9832 concept, then the expression is true if and only if the
9833 concept's constraints are satisfied by the given template
9834 arguments.
9835
9836 NOTE: This is an extension of Concepts Lite TS that
9837 allows constraints to be used in expressions. */
9838 if (concept_p)
9839 {
9840 tree decl = DECL_TEMPLATE_RESULT (templ);
9841 return evaluate_variable_concept (decl, arglist);
9842 }
9843
9844 return instantiate_template (templ, arglist, complain);
9845 }
9846
9847 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9848 TARGS template args, and instantiate it if it's not dependent. */
9849
9850 tree
9851 lookup_and_finish_template_variable (tree templ, tree targs,
9852 tsubst_flags_t complain)
9853 {
9854 templ = lookup_template_variable (templ, targs);
9855 if (!any_dependent_template_arguments_p (targs))
9856 {
9857 templ = finish_template_variable (templ, complain);
9858 mark_used (templ);
9859 }
9860
9861 return convert_from_reference (templ);
9862 }
9863
9864 \f
9865 struct pair_fn_data
9866 {
9867 tree_fn_t fn;
9868 tree_fn_t any_fn;
9869 void *data;
9870 /* True when we should also visit template parameters that occur in
9871 non-deduced contexts. */
9872 bool include_nondeduced_p;
9873 hash_set<tree> *visited;
9874 };
9875
9876 /* Called from for_each_template_parm via walk_tree. */
9877
9878 static tree
9879 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
9880 {
9881 tree t = *tp;
9882 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
9883 tree_fn_t fn = pfd->fn;
9884 void *data = pfd->data;
9885 tree result = NULL_TREE;
9886
9887 #define WALK_SUBTREE(NODE) \
9888 do \
9889 { \
9890 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9891 pfd->include_nondeduced_p, \
9892 pfd->any_fn); \
9893 if (result) goto out; \
9894 } \
9895 while (0)
9896
9897 if (pfd->any_fn && (*pfd->any_fn)(t, data))
9898 return t;
9899
9900 if (TYPE_P (t)
9901 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
9902 WALK_SUBTREE (TYPE_CONTEXT (t));
9903
9904 switch (TREE_CODE (t))
9905 {
9906 case RECORD_TYPE:
9907 if (TYPE_PTRMEMFUNC_P (t))
9908 break;
9909 /* Fall through. */
9910
9911 case UNION_TYPE:
9912 case ENUMERAL_TYPE:
9913 if (!TYPE_TEMPLATE_INFO (t))
9914 *walk_subtrees = 0;
9915 else
9916 WALK_SUBTREE (TYPE_TI_ARGS (t));
9917 break;
9918
9919 case INTEGER_TYPE:
9920 WALK_SUBTREE (TYPE_MIN_VALUE (t));
9921 WALK_SUBTREE (TYPE_MAX_VALUE (t));
9922 break;
9923
9924 case METHOD_TYPE:
9925 /* Since we're not going to walk subtrees, we have to do this
9926 explicitly here. */
9927 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
9928 /* Fall through. */
9929
9930 case FUNCTION_TYPE:
9931 /* Check the return type. */
9932 WALK_SUBTREE (TREE_TYPE (t));
9933
9934 /* Check the parameter types. Since default arguments are not
9935 instantiated until they are needed, the TYPE_ARG_TYPES may
9936 contain expressions that involve template parameters. But,
9937 no-one should be looking at them yet. And, once they're
9938 instantiated, they don't contain template parameters, so
9939 there's no point in looking at them then, either. */
9940 {
9941 tree parm;
9942
9943 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
9944 WALK_SUBTREE (TREE_VALUE (parm));
9945
9946 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9947 want walk_tree walking into them itself. */
9948 *walk_subtrees = 0;
9949 }
9950
9951 if (flag_noexcept_type)
9952 {
9953 tree spec = TYPE_RAISES_EXCEPTIONS (t);
9954 if (spec)
9955 WALK_SUBTREE (TREE_PURPOSE (spec));
9956 }
9957 break;
9958
9959 case TYPEOF_TYPE:
9960 case DECLTYPE_TYPE:
9961 case UNDERLYING_TYPE:
9962 if (pfd->include_nondeduced_p
9963 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
9964 pfd->visited,
9965 pfd->include_nondeduced_p,
9966 pfd->any_fn))
9967 return error_mark_node;
9968 *walk_subtrees = false;
9969 break;
9970
9971 case FUNCTION_DECL:
9972 case VAR_DECL:
9973 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9974 WALK_SUBTREE (DECL_TI_ARGS (t));
9975 /* Fall through. */
9976
9977 case PARM_DECL:
9978 case CONST_DECL:
9979 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
9980 WALK_SUBTREE (DECL_INITIAL (t));
9981 if (DECL_CONTEXT (t)
9982 && pfd->include_nondeduced_p)
9983 WALK_SUBTREE (DECL_CONTEXT (t));
9984 break;
9985
9986 case BOUND_TEMPLATE_TEMPLATE_PARM:
9987 /* Record template parameters such as `T' inside `TT<T>'. */
9988 WALK_SUBTREE (TYPE_TI_ARGS (t));
9989 /* Fall through. */
9990
9991 case TEMPLATE_TEMPLATE_PARM:
9992 case TEMPLATE_TYPE_PARM:
9993 case TEMPLATE_PARM_INDEX:
9994 if (fn && (*fn)(t, data))
9995 return t;
9996 else if (!fn)
9997 return t;
9998 break;
9999
10000 case TEMPLATE_DECL:
10001 /* A template template parameter is encountered. */
10002 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10003 WALK_SUBTREE (TREE_TYPE (t));
10004
10005 /* Already substituted template template parameter */
10006 *walk_subtrees = 0;
10007 break;
10008
10009 case TYPENAME_TYPE:
10010 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10011 partial instantiation. */
10012 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
10013 break;
10014
10015 case CONSTRUCTOR:
10016 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
10017 && pfd->include_nondeduced_p)
10018 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
10019 break;
10020
10021 case INDIRECT_REF:
10022 case COMPONENT_REF:
10023 /* If there's no type, then this thing must be some expression
10024 involving template parameters. */
10025 if (!fn && !TREE_TYPE (t))
10026 return error_mark_node;
10027 break;
10028
10029 case MODOP_EXPR:
10030 case CAST_EXPR:
10031 case IMPLICIT_CONV_EXPR:
10032 case REINTERPRET_CAST_EXPR:
10033 case CONST_CAST_EXPR:
10034 case STATIC_CAST_EXPR:
10035 case DYNAMIC_CAST_EXPR:
10036 case ARROW_EXPR:
10037 case DOTSTAR_EXPR:
10038 case TYPEID_EXPR:
10039 case PSEUDO_DTOR_EXPR:
10040 if (!fn)
10041 return error_mark_node;
10042 break;
10043
10044 default:
10045 break;
10046 }
10047
10048 #undef WALK_SUBTREE
10049
10050 /* We didn't find any template parameters we liked. */
10051 out:
10052 return result;
10053 }
10054
10055 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10056 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10057 call FN with the parameter and the DATA.
10058 If FN returns nonzero, the iteration is terminated, and
10059 for_each_template_parm returns 1. Otherwise, the iteration
10060 continues. If FN never returns a nonzero value, the value
10061 returned by for_each_template_parm is 0. If FN is NULL, it is
10062 considered to be the function which always returns 1.
10063
10064 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10065 parameters that occur in non-deduced contexts. When false, only
10066 visits those template parameters that can be deduced. */
10067
10068 static tree
10069 for_each_template_parm (tree t, tree_fn_t fn, void* data,
10070 hash_set<tree> *visited,
10071 bool include_nondeduced_p,
10072 tree_fn_t any_fn)
10073 {
10074 struct pair_fn_data pfd;
10075 tree result;
10076
10077 /* Set up. */
10078 pfd.fn = fn;
10079 pfd.any_fn = any_fn;
10080 pfd.data = data;
10081 pfd.include_nondeduced_p = include_nondeduced_p;
10082
10083 /* Walk the tree. (Conceptually, we would like to walk without
10084 duplicates, but for_each_template_parm_r recursively calls
10085 for_each_template_parm, so we would need to reorganize a fair
10086 bit to use walk_tree_without_duplicates, so we keep our own
10087 visited list.) */
10088 if (visited)
10089 pfd.visited = visited;
10090 else
10091 pfd.visited = new hash_set<tree>;
10092 result = cp_walk_tree (&t,
10093 for_each_template_parm_r,
10094 &pfd,
10095 pfd.visited);
10096
10097 /* Clean up. */
10098 if (!visited)
10099 {
10100 delete pfd.visited;
10101 pfd.visited = 0;
10102 }
10103
10104 return result;
10105 }
10106
10107 /* Returns true if T depends on any template parameter. */
10108
10109 int
10110 uses_template_parms (tree t)
10111 {
10112 if (t == NULL_TREE)
10113 return false;
10114
10115 bool dependent_p;
10116 int saved_processing_template_decl;
10117
10118 saved_processing_template_decl = processing_template_decl;
10119 if (!saved_processing_template_decl)
10120 processing_template_decl = 1;
10121 if (TYPE_P (t))
10122 dependent_p = dependent_type_p (t);
10123 else if (TREE_CODE (t) == TREE_VEC)
10124 dependent_p = any_dependent_template_arguments_p (t);
10125 else if (TREE_CODE (t) == TREE_LIST)
10126 dependent_p = (uses_template_parms (TREE_VALUE (t))
10127 || uses_template_parms (TREE_CHAIN (t)));
10128 else if (TREE_CODE (t) == TYPE_DECL)
10129 dependent_p = dependent_type_p (TREE_TYPE (t));
10130 else if (DECL_P (t)
10131 || EXPR_P (t)
10132 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
10133 || TREE_CODE (t) == OVERLOAD
10134 || BASELINK_P (t)
10135 || identifier_p (t)
10136 || TREE_CODE (t) == TRAIT_EXPR
10137 || TREE_CODE (t) == CONSTRUCTOR
10138 || CONSTANT_CLASS_P (t))
10139 dependent_p = (type_dependent_expression_p (t)
10140 || value_dependent_expression_p (t));
10141 else
10142 {
10143 gcc_assert (t == error_mark_node);
10144 dependent_p = false;
10145 }
10146
10147 processing_template_decl = saved_processing_template_decl;
10148
10149 return dependent_p;
10150 }
10151
10152 /* Returns true iff current_function_decl is an incompletely instantiated
10153 template. Useful instead of processing_template_decl because the latter
10154 is set to 0 during instantiate_non_dependent_expr. */
10155
10156 bool
10157 in_template_function (void)
10158 {
10159 tree fn = current_function_decl;
10160 bool ret;
10161 ++processing_template_decl;
10162 ret = (fn && DECL_LANG_SPECIFIC (fn)
10163 && DECL_TEMPLATE_INFO (fn)
10164 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
10165 --processing_template_decl;
10166 return ret;
10167 }
10168
10169 /* Returns true if T depends on any template parameter with level LEVEL. */
10170
10171 bool
10172 uses_template_parms_level (tree t, int level)
10173 {
10174 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
10175 /*include_nondeduced_p=*/true);
10176 }
10177
10178 /* Returns true if the signature of DECL depends on any template parameter from
10179 its enclosing class. */
10180
10181 bool
10182 uses_outer_template_parms (tree decl)
10183 {
10184 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
10185 if (depth == 0)
10186 return false;
10187 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
10188 &depth, NULL, /*include_nondeduced_p=*/true))
10189 return true;
10190 if (PRIMARY_TEMPLATE_P (decl)
10191 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
10192 (DECL_TEMPLATE_PARMS (decl)),
10193 template_parm_outer_level,
10194 &depth, NULL, /*include_nondeduced_p=*/true))
10195 return true;
10196 tree ci = get_constraints (decl);
10197 if (ci)
10198 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
10199 if (ci && for_each_template_parm (ci, template_parm_outer_level,
10200 &depth, NULL, /*nondeduced*/true))
10201 return true;
10202 return false;
10203 }
10204
10205 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10206 ill-formed translation unit, i.e. a variable or function that isn't
10207 usable in a constant expression. */
10208
10209 static inline bool
10210 neglectable_inst_p (tree d)
10211 {
10212 return (d && DECL_P (d)
10213 && !undeduced_auto_decl (d)
10214 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
10215 : decl_maybe_constant_var_p (d)));
10216 }
10217
10218 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10219 neglectable and instantiated from within an erroneous instantiation. */
10220
10221 static bool
10222 limit_bad_template_recursion (tree decl)
10223 {
10224 struct tinst_level *lev = current_tinst_level;
10225 int errs = errorcount + sorrycount;
10226 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
10227 return false;
10228
10229 for (; lev; lev = lev->next)
10230 if (neglectable_inst_p (lev->maybe_get_node ()))
10231 break;
10232
10233 return (lev && errs > lev->errors);
10234 }
10235
10236 static int tinst_depth;
10237 extern int max_tinst_depth;
10238 int depth_reached;
10239
10240 static GTY(()) struct tinst_level *last_error_tinst_level;
10241
10242 /* We're starting to instantiate D; record the template instantiation context
10243 at LOC for diagnostics and to restore it later. */
10244
10245 static bool
10246 push_tinst_level_loc (tree tldcl, tree targs, location_t loc)
10247 {
10248 struct tinst_level *new_level;
10249
10250 if (tinst_depth >= max_tinst_depth)
10251 {
10252 /* Tell error.c not to try to instantiate any templates. */
10253 at_eof = 2;
10254 fatal_error (input_location,
10255 "template instantiation depth exceeds maximum of %d"
10256 " (use %<-ftemplate-depth=%> to increase the maximum)",
10257 max_tinst_depth);
10258 return false;
10259 }
10260
10261 /* If the current instantiation caused problems, don't let it instantiate
10262 anything else. Do allow deduction substitution and decls usable in
10263 constant expressions. */
10264 if (!targs && limit_bad_template_recursion (tldcl))
10265 return false;
10266
10267 /* When not -quiet, dump template instantiations other than functions, since
10268 announce_function will take care of those. */
10269 if (!quiet_flag && !targs
10270 && TREE_CODE (tldcl) != TREE_LIST
10271 && TREE_CODE (tldcl) != FUNCTION_DECL)
10272 fprintf (stderr, " %s", decl_as_string (tldcl, TFF_DECL_SPECIFIERS));
10273
10274 new_level = tinst_level_freelist ().alloc ();
10275 new_level->tldcl = tldcl;
10276 new_level->targs = targs;
10277 new_level->locus = loc;
10278 new_level->errors = errorcount + sorrycount;
10279 new_level->next = NULL;
10280 new_level->refcount = 0;
10281 set_refcount_ptr (new_level->next, current_tinst_level);
10282 set_refcount_ptr (current_tinst_level, new_level);
10283
10284 ++tinst_depth;
10285 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
10286 depth_reached = tinst_depth;
10287
10288 return true;
10289 }
10290
10291 /* We're starting substitution of TMPL<ARGS>; record the template
10292 substitution context for diagnostics and to restore it later. */
10293
10294 static bool
10295 push_tinst_level (tree tmpl, tree args)
10296 {
10297 return push_tinst_level_loc (tmpl, args, input_location);
10298 }
10299
10300 /* We're starting to instantiate D; record INPUT_LOCATION and the
10301 template instantiation context for diagnostics and to restore it
10302 later. */
10303
10304 bool
10305 push_tinst_level (tree d)
10306 {
10307 return push_tinst_level_loc (d, input_location);
10308 }
10309
10310 /* Likewise, but record LOC as the program location. */
10311
10312 bool
10313 push_tinst_level_loc (tree d, location_t loc)
10314 {
10315 gcc_assert (TREE_CODE (d) != TREE_LIST);
10316 return push_tinst_level_loc (d, NULL, loc);
10317 }
10318
10319 /* We're done instantiating this template; return to the instantiation
10320 context. */
10321
10322 void
10323 pop_tinst_level (void)
10324 {
10325 /* Restore the filename and line number stashed away when we started
10326 this instantiation. */
10327 input_location = current_tinst_level->locus;
10328 set_refcount_ptr (current_tinst_level, current_tinst_level->next);
10329 --tinst_depth;
10330 }
10331
10332 /* We're instantiating a deferred template; restore the template
10333 instantiation context in which the instantiation was requested, which
10334 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
10335
10336 static tree
10337 reopen_tinst_level (struct tinst_level *level)
10338 {
10339 struct tinst_level *t;
10340
10341 tinst_depth = 0;
10342 for (t = level; t; t = t->next)
10343 ++tinst_depth;
10344
10345 set_refcount_ptr (current_tinst_level, level);
10346 pop_tinst_level ();
10347 if (current_tinst_level)
10348 current_tinst_level->errors = errorcount+sorrycount;
10349 return level->maybe_get_node ();
10350 }
10351
10352 /* Returns the TINST_LEVEL which gives the original instantiation
10353 context. */
10354
10355 struct tinst_level *
10356 outermost_tinst_level (void)
10357 {
10358 struct tinst_level *level = current_tinst_level;
10359 if (level)
10360 while (level->next)
10361 level = level->next;
10362 return level;
10363 }
10364
10365 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
10366 vector of template arguments, as for tsubst.
10367
10368 Returns an appropriate tsubst'd friend declaration. */
10369
10370 static tree
10371 tsubst_friend_function (tree decl, tree args)
10372 {
10373 tree new_friend;
10374
10375 if (TREE_CODE (decl) == FUNCTION_DECL
10376 && DECL_TEMPLATE_INSTANTIATION (decl)
10377 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
10378 /* This was a friend declared with an explicit template
10379 argument list, e.g.:
10380
10381 friend void f<>(T);
10382
10383 to indicate that f was a template instantiation, not a new
10384 function declaration. Now, we have to figure out what
10385 instantiation of what template. */
10386 {
10387 tree template_id, arglist, fns;
10388 tree new_args;
10389 tree tmpl;
10390 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
10391
10392 /* Friend functions are looked up in the containing namespace scope.
10393 We must enter that scope, to avoid finding member functions of the
10394 current class with same name. */
10395 push_nested_namespace (ns);
10396 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
10397 tf_warning_or_error, NULL_TREE,
10398 /*integral_constant_expression_p=*/false);
10399 pop_nested_namespace (ns);
10400 arglist = tsubst (DECL_TI_ARGS (decl), args,
10401 tf_warning_or_error, NULL_TREE);
10402 template_id = lookup_template_function (fns, arglist);
10403
10404 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10405 tmpl = determine_specialization (template_id, new_friend,
10406 &new_args,
10407 /*need_member_template=*/0,
10408 TREE_VEC_LENGTH (args),
10409 tsk_none);
10410 return instantiate_template (tmpl, new_args, tf_error);
10411 }
10412
10413 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
10414
10415 /* The NEW_FRIEND will look like an instantiation, to the
10416 compiler, but is not an instantiation from the point of view of
10417 the language. For example, we might have had:
10418
10419 template <class T> struct S {
10420 template <class U> friend void f(T, U);
10421 };
10422
10423 Then, in S<int>, template <class U> void f(int, U) is not an
10424 instantiation of anything. */
10425 if (new_friend == error_mark_node)
10426 return error_mark_node;
10427
10428 DECL_USE_TEMPLATE (new_friend) = 0;
10429 if (TREE_CODE (decl) == TEMPLATE_DECL)
10430 {
10431 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
10432 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
10433 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
10434 }
10435
10436 /* The mangled name for the NEW_FRIEND is incorrect. The function
10437 is not a template instantiation and should not be mangled like
10438 one. Therefore, we forget the mangling here; we'll recompute it
10439 later if we need it. */
10440 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
10441 {
10442 SET_DECL_RTL (new_friend, NULL);
10443 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
10444 }
10445
10446 if (DECL_NAMESPACE_SCOPE_P (new_friend))
10447 {
10448 tree old_decl;
10449 tree new_friend_template_info;
10450 tree new_friend_result_template_info;
10451 tree ns;
10452 int new_friend_is_defn;
10453
10454 /* We must save some information from NEW_FRIEND before calling
10455 duplicate decls since that function will free NEW_FRIEND if
10456 possible. */
10457 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
10458 new_friend_is_defn =
10459 (DECL_INITIAL (DECL_TEMPLATE_RESULT
10460 (template_for_substitution (new_friend)))
10461 != NULL_TREE);
10462 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
10463 {
10464 /* This declaration is a `primary' template. */
10465 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
10466
10467 new_friend_result_template_info
10468 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
10469 }
10470 else
10471 new_friend_result_template_info = NULL_TREE;
10472
10473 /* Inside pushdecl_namespace_level, we will push into the
10474 current namespace. However, the friend function should go
10475 into the namespace of the template. */
10476 ns = decl_namespace_context (new_friend);
10477 push_nested_namespace (ns);
10478 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
10479 pop_nested_namespace (ns);
10480
10481 if (old_decl == error_mark_node)
10482 return error_mark_node;
10483
10484 if (old_decl != new_friend)
10485 {
10486 /* This new friend declaration matched an existing
10487 declaration. For example, given:
10488
10489 template <class T> void f(T);
10490 template <class U> class C {
10491 template <class T> friend void f(T) {}
10492 };
10493
10494 the friend declaration actually provides the definition
10495 of `f', once C has been instantiated for some type. So,
10496 old_decl will be the out-of-class template declaration,
10497 while new_friend is the in-class definition.
10498
10499 But, if `f' was called before this point, the
10500 instantiation of `f' will have DECL_TI_ARGS corresponding
10501 to `T' but not to `U', references to which might appear
10502 in the definition of `f'. Previously, the most general
10503 template for an instantiation of `f' was the out-of-class
10504 version; now it is the in-class version. Therefore, we
10505 run through all specialization of `f', adding to their
10506 DECL_TI_ARGS appropriately. In particular, they need a
10507 new set of outer arguments, corresponding to the
10508 arguments for this class instantiation.
10509
10510 The same situation can arise with something like this:
10511
10512 friend void f(int);
10513 template <class T> class C {
10514 friend void f(T) {}
10515 };
10516
10517 when `C<int>' is instantiated. Now, `f(int)' is defined
10518 in the class. */
10519
10520 if (!new_friend_is_defn)
10521 /* On the other hand, if the in-class declaration does
10522 *not* provide a definition, then we don't want to alter
10523 existing definitions. We can just leave everything
10524 alone. */
10525 ;
10526 else
10527 {
10528 tree new_template = TI_TEMPLATE (new_friend_template_info);
10529 tree new_args = TI_ARGS (new_friend_template_info);
10530
10531 /* Overwrite whatever template info was there before, if
10532 any, with the new template information pertaining to
10533 the declaration. */
10534 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
10535
10536 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
10537 {
10538 /* We should have called reregister_specialization in
10539 duplicate_decls. */
10540 gcc_assert (retrieve_specialization (new_template,
10541 new_args, 0)
10542 == old_decl);
10543
10544 /* Instantiate it if the global has already been used. */
10545 if (DECL_ODR_USED (old_decl))
10546 instantiate_decl (old_decl, /*defer_ok=*/true,
10547 /*expl_inst_class_mem_p=*/false);
10548 }
10549 else
10550 {
10551 tree t;
10552
10553 /* Indicate that the old function template is a partial
10554 instantiation. */
10555 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
10556 = new_friend_result_template_info;
10557
10558 gcc_assert (new_template
10559 == most_general_template (new_template));
10560 gcc_assert (new_template != old_decl);
10561
10562 /* Reassign any specializations already in the hash table
10563 to the new more general template, and add the
10564 additional template args. */
10565 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
10566 t != NULL_TREE;
10567 t = TREE_CHAIN (t))
10568 {
10569 tree spec = TREE_VALUE (t);
10570 spec_entry elt;
10571
10572 elt.tmpl = old_decl;
10573 elt.args = DECL_TI_ARGS (spec);
10574 elt.spec = NULL_TREE;
10575
10576 decl_specializations->remove_elt (&elt);
10577
10578 DECL_TI_ARGS (spec)
10579 = add_outermost_template_args (new_args,
10580 DECL_TI_ARGS (spec));
10581
10582 register_specialization
10583 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
10584
10585 }
10586 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
10587 }
10588 }
10589
10590 /* The information from NEW_FRIEND has been merged into OLD_DECL
10591 by duplicate_decls. */
10592 new_friend = old_decl;
10593 }
10594 }
10595 else
10596 {
10597 tree context = DECL_CONTEXT (new_friend);
10598 bool dependent_p;
10599
10600 /* In the code
10601 template <class T> class C {
10602 template <class U> friend void C1<U>::f (); // case 1
10603 friend void C2<T>::f (); // case 2
10604 };
10605 we only need to make sure CONTEXT is a complete type for
10606 case 2. To distinguish between the two cases, we note that
10607 CONTEXT of case 1 remains dependent type after tsubst while
10608 this isn't true for case 2. */
10609 ++processing_template_decl;
10610 dependent_p = dependent_type_p (context);
10611 --processing_template_decl;
10612
10613 if (!dependent_p
10614 && !complete_type_or_else (context, NULL_TREE))
10615 return error_mark_node;
10616
10617 if (COMPLETE_TYPE_P (context))
10618 {
10619 tree fn = new_friend;
10620 /* do_friend adds the TEMPLATE_DECL for any member friend
10621 template even if it isn't a member template, i.e.
10622 template <class T> friend A<T>::f();
10623 Look through it in that case. */
10624 if (TREE_CODE (fn) == TEMPLATE_DECL
10625 && !PRIMARY_TEMPLATE_P (fn))
10626 fn = DECL_TEMPLATE_RESULT (fn);
10627 /* Check to see that the declaration is really present, and,
10628 possibly obtain an improved declaration. */
10629 fn = check_classfn (context, fn, NULL_TREE);
10630
10631 if (fn)
10632 new_friend = fn;
10633 }
10634 }
10635
10636 return new_friend;
10637 }
10638
10639 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
10640 template arguments, as for tsubst.
10641
10642 Returns an appropriate tsubst'd friend type or error_mark_node on
10643 failure. */
10644
10645 static tree
10646 tsubst_friend_class (tree friend_tmpl, tree args)
10647 {
10648 tree tmpl;
10649
10650 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
10651 {
10652 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
10653 return TREE_TYPE (tmpl);
10654 }
10655
10656 tree context = CP_DECL_CONTEXT (friend_tmpl);
10657 if (TREE_CODE (context) == NAMESPACE_DECL)
10658 push_nested_namespace (context);
10659 else
10660 {
10661 context = tsubst (context, args, tf_error, NULL_TREE);
10662 push_nested_class (context);
10663 }
10664
10665 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), /*prefer_type=*/false,
10666 /*non_class=*/false, /*block_p=*/false,
10667 /*namespaces_only=*/false, LOOKUP_HIDDEN);
10668
10669 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
10670 {
10671 /* The friend template has already been declared. Just
10672 check to see that the declarations match, and install any new
10673 default parameters. We must tsubst the default parameters,
10674 of course. We only need the innermost template parameters
10675 because that is all that redeclare_class_template will look
10676 at. */
10677 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
10678 > TMPL_ARGS_DEPTH (args))
10679 {
10680 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
10681 args, tf_warning_or_error);
10682 location_t saved_input_location = input_location;
10683 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
10684 tree cons = get_constraints (tmpl);
10685 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
10686 input_location = saved_input_location;
10687 }
10688 }
10689 else
10690 {
10691 /* The friend template has not already been declared. In this
10692 case, the instantiation of the template class will cause the
10693 injection of this template into the namespace scope. */
10694 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
10695
10696 if (tmpl != error_mark_node)
10697 {
10698 /* The new TMPL is not an instantiation of anything, so we
10699 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
10700 for the new type because that is supposed to be the
10701 corresponding template decl, i.e., TMPL. */
10702 DECL_USE_TEMPLATE (tmpl) = 0;
10703 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
10704 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
10705 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
10706 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
10707
10708 /* It is hidden. */
10709 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl));
10710 DECL_ANTICIPATED (tmpl)
10711 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl)) = true;
10712
10713 /* Inject this template into the enclosing namspace scope. */
10714 tmpl = pushdecl_namespace_level (tmpl, true);
10715 }
10716 }
10717
10718 if (TREE_CODE (context) == NAMESPACE_DECL)
10719 pop_nested_namespace (context);
10720 else
10721 pop_nested_class ();
10722
10723 return TREE_TYPE (tmpl);
10724 }
10725
10726 /* Returns zero if TYPE cannot be completed later due to circularity.
10727 Otherwise returns one. */
10728
10729 static int
10730 can_complete_type_without_circularity (tree type)
10731 {
10732 if (type == NULL_TREE || type == error_mark_node)
10733 return 0;
10734 else if (COMPLETE_TYPE_P (type))
10735 return 1;
10736 else if (TREE_CODE (type) == ARRAY_TYPE)
10737 return can_complete_type_without_circularity (TREE_TYPE (type));
10738 else if (CLASS_TYPE_P (type)
10739 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
10740 return 0;
10741 else
10742 return 1;
10743 }
10744
10745 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
10746 tsubst_flags_t, tree);
10747
10748 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10749 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10750
10751 static tree
10752 tsubst_attribute (tree t, tree *decl_p, tree args,
10753 tsubst_flags_t complain, tree in_decl)
10754 {
10755 gcc_assert (ATTR_IS_DEPENDENT (t));
10756
10757 tree val = TREE_VALUE (t);
10758 if (val == NULL_TREE)
10759 /* Nothing to do. */;
10760 else if ((flag_openmp || flag_openmp_simd)
10761 && is_attribute_p ("omp declare simd",
10762 get_attribute_name (t)))
10763 {
10764 tree clauses = TREE_VALUE (val);
10765 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
10766 complain, in_decl);
10767 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
10768 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
10769 tree parms = DECL_ARGUMENTS (*decl_p);
10770 clauses
10771 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
10772 if (clauses)
10773 val = build_tree_list (NULL_TREE, clauses);
10774 else
10775 val = NULL_TREE;
10776 }
10777 /* If the first attribute argument is an identifier, don't
10778 pass it through tsubst. Attributes like mode, format,
10779 cleanup and several target specific attributes expect it
10780 unmodified. */
10781 else if (attribute_takes_identifier_p (get_attribute_name (t)))
10782 {
10783 tree chain
10784 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
10785 /*integral_constant_expression_p=*/false);
10786 if (chain != TREE_CHAIN (val))
10787 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
10788 }
10789 else if (PACK_EXPANSION_P (val))
10790 {
10791 /* An attribute pack expansion. */
10792 tree purp = TREE_PURPOSE (t);
10793 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
10794 if (pack == error_mark_node)
10795 return error_mark_node;
10796 int len = TREE_VEC_LENGTH (pack);
10797 tree list = NULL_TREE;
10798 tree *q = &list;
10799 for (int i = 0; i < len; ++i)
10800 {
10801 tree elt = TREE_VEC_ELT (pack, i);
10802 *q = build_tree_list (purp, elt);
10803 q = &TREE_CHAIN (*q);
10804 }
10805 return list;
10806 }
10807 else
10808 val = tsubst_expr (val, args, complain, in_decl,
10809 /*integral_constant_expression_p=*/false);
10810
10811 if (val != TREE_VALUE (t))
10812 return build_tree_list (TREE_PURPOSE (t), val);
10813 return t;
10814 }
10815
10816 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10817 unchanged or a new TREE_LIST chain. */
10818
10819 static tree
10820 tsubst_attributes (tree attributes, tree args,
10821 tsubst_flags_t complain, tree in_decl)
10822 {
10823 tree last_dep = NULL_TREE;
10824
10825 for (tree t = attributes; t; t = TREE_CHAIN (t))
10826 if (ATTR_IS_DEPENDENT (t))
10827 {
10828 last_dep = t;
10829 attributes = copy_list (attributes);
10830 break;
10831 }
10832
10833 if (last_dep)
10834 for (tree *p = &attributes; *p; )
10835 {
10836 tree t = *p;
10837 if (ATTR_IS_DEPENDENT (t))
10838 {
10839 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
10840 if (subst != t)
10841 {
10842 *p = subst;
10843 while (*p)
10844 p = &TREE_CHAIN (*p);
10845 *p = TREE_CHAIN (t);
10846 continue;
10847 }
10848 }
10849 p = &TREE_CHAIN (*p);
10850 }
10851
10852 return attributes;
10853 }
10854
10855 /* Apply any attributes which had to be deferred until instantiation
10856 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10857 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10858
10859 static void
10860 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
10861 tree args, tsubst_flags_t complain, tree in_decl)
10862 {
10863 tree last_dep = NULL_TREE;
10864 tree t;
10865 tree *p;
10866
10867 if (attributes == NULL_TREE)
10868 return;
10869
10870 if (DECL_P (*decl_p))
10871 {
10872 if (TREE_TYPE (*decl_p) == error_mark_node)
10873 return;
10874 p = &DECL_ATTRIBUTES (*decl_p);
10875 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10876 to our attributes parameter. */
10877 gcc_assert (*p == attributes);
10878 }
10879 else
10880 {
10881 p = &TYPE_ATTRIBUTES (*decl_p);
10882 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10883 lookup_template_class_1, and should be preserved. */
10884 gcc_assert (*p != attributes);
10885 while (*p)
10886 p = &TREE_CHAIN (*p);
10887 }
10888
10889 for (t = attributes; t; t = TREE_CHAIN (t))
10890 if (ATTR_IS_DEPENDENT (t))
10891 {
10892 last_dep = t;
10893 attributes = copy_list (attributes);
10894 break;
10895 }
10896
10897 *p = attributes;
10898 if (last_dep)
10899 {
10900 tree late_attrs = NULL_TREE;
10901 tree *q = &late_attrs;
10902
10903 for (; *p; )
10904 {
10905 t = *p;
10906 if (ATTR_IS_DEPENDENT (t))
10907 {
10908 *p = TREE_CHAIN (t);
10909 TREE_CHAIN (t) = NULL_TREE;
10910 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
10911 while (*q)
10912 q = &TREE_CHAIN (*q);
10913 }
10914 else
10915 p = &TREE_CHAIN (t);
10916 }
10917
10918 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
10919 }
10920 }
10921
10922 /* Perform (or defer) access check for typedefs that were referenced
10923 from within the template TMPL code.
10924 This is a subroutine of instantiate_decl and instantiate_class_template.
10925 TMPL is the template to consider and TARGS is the list of arguments of
10926 that template. */
10927
10928 static void
10929 perform_typedefs_access_check (tree tmpl, tree targs)
10930 {
10931 unsigned i;
10932 qualified_typedef_usage_t *iter;
10933
10934 if (!tmpl
10935 || (!CLASS_TYPE_P (tmpl)
10936 && TREE_CODE (tmpl) != FUNCTION_DECL))
10937 return;
10938
10939 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
10940 {
10941 tree type_decl = iter->typedef_decl;
10942 tree type_scope = iter->context;
10943
10944 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
10945 continue;
10946
10947 if (uses_template_parms (type_decl))
10948 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
10949 if (uses_template_parms (type_scope))
10950 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
10951
10952 /* Make access check error messages point to the location
10953 of the use of the typedef. */
10954 iloc_sentinel ils (iter->locus);
10955 perform_or_defer_access_check (TYPE_BINFO (type_scope),
10956 type_decl, type_decl,
10957 tf_warning_or_error);
10958 }
10959 }
10960
10961 static tree
10962 instantiate_class_template_1 (tree type)
10963 {
10964 tree templ, args, pattern, t, member;
10965 tree typedecl;
10966 tree pbinfo;
10967 tree base_list;
10968 unsigned int saved_maximum_field_alignment;
10969 tree fn_context;
10970
10971 if (type == error_mark_node)
10972 return error_mark_node;
10973
10974 if (COMPLETE_OR_OPEN_TYPE_P (type)
10975 || uses_template_parms (type))
10976 return type;
10977
10978 /* Figure out which template is being instantiated. */
10979 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
10980 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
10981
10982 /* Mark the type as in the process of being defined. */
10983 TYPE_BEING_DEFINED (type) = 1;
10984
10985 /* We may be in the middle of deferred access check. Disable
10986 it now. */
10987 deferring_access_check_sentinel acs (dk_no_deferred);
10988
10989 /* Determine what specialization of the original template to
10990 instantiate. */
10991 t = most_specialized_partial_spec (type, tf_warning_or_error);
10992 if (t == error_mark_node)
10993 return error_mark_node;
10994 else if (t)
10995 {
10996 /* This TYPE is actually an instantiation of a partial
10997 specialization. We replace the innermost set of ARGS with
10998 the arguments appropriate for substitution. For example,
10999 given:
11000
11001 template <class T> struct S {};
11002 template <class T> struct S<T*> {};
11003
11004 and supposing that we are instantiating S<int*>, ARGS will
11005 presently be {int*} -- but we need {int}. */
11006 pattern = TREE_TYPE (t);
11007 args = TREE_PURPOSE (t);
11008 }
11009 else
11010 {
11011 pattern = TREE_TYPE (templ);
11012 args = CLASSTYPE_TI_ARGS (type);
11013 }
11014
11015 /* If the template we're instantiating is incomplete, then clearly
11016 there's nothing we can do. */
11017 if (!COMPLETE_TYPE_P (pattern))
11018 {
11019 /* We can try again later. */
11020 TYPE_BEING_DEFINED (type) = 0;
11021 return type;
11022 }
11023
11024 /* If we've recursively instantiated too many templates, stop. */
11025 if (! push_tinst_level (type))
11026 return type;
11027
11028 int saved_unevaluated_operand = cp_unevaluated_operand;
11029 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11030
11031 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
11032 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
11033 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
11034 fn_context = error_mark_node;
11035 if (!fn_context)
11036 push_to_top_level ();
11037 else
11038 {
11039 cp_unevaluated_operand = 0;
11040 c_inhibit_evaluation_warnings = 0;
11041 }
11042 /* Use #pragma pack from the template context. */
11043 saved_maximum_field_alignment = maximum_field_alignment;
11044 maximum_field_alignment = TYPE_PRECISION (pattern);
11045
11046 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
11047
11048 /* Set the input location to the most specialized template definition.
11049 This is needed if tsubsting causes an error. */
11050 typedecl = TYPE_MAIN_DECL (pattern);
11051 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
11052 DECL_SOURCE_LOCATION (typedecl);
11053
11054 TYPE_PACKED (type) = TYPE_PACKED (pattern);
11055 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
11056 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
11057 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
11058 if (ANON_AGGR_TYPE_P (pattern))
11059 SET_ANON_AGGR_TYPE_P (type);
11060 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
11061 {
11062 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
11063 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
11064 /* Adjust visibility for template arguments. */
11065 determine_visibility (TYPE_MAIN_DECL (type));
11066 }
11067 if (CLASS_TYPE_P (type))
11068 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
11069
11070 pbinfo = TYPE_BINFO (pattern);
11071
11072 /* We should never instantiate a nested class before its enclosing
11073 class; we need to look up the nested class by name before we can
11074 instantiate it, and that lookup should instantiate the enclosing
11075 class. */
11076 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
11077 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
11078
11079 base_list = NULL_TREE;
11080 if (BINFO_N_BASE_BINFOS (pbinfo))
11081 {
11082 tree pbase_binfo;
11083 tree pushed_scope;
11084 int i;
11085
11086 /* We must enter the scope containing the type, as that is where
11087 the accessibility of types named in dependent bases are
11088 looked up from. */
11089 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
11090
11091 /* Substitute into each of the bases to determine the actual
11092 basetypes. */
11093 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
11094 {
11095 tree base;
11096 tree access = BINFO_BASE_ACCESS (pbinfo, i);
11097 tree expanded_bases = NULL_TREE;
11098 int idx, len = 1;
11099
11100 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
11101 {
11102 expanded_bases =
11103 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
11104 args, tf_error, NULL_TREE);
11105 if (expanded_bases == error_mark_node)
11106 continue;
11107
11108 len = TREE_VEC_LENGTH (expanded_bases);
11109 }
11110
11111 for (idx = 0; idx < len; idx++)
11112 {
11113 if (expanded_bases)
11114 /* Extract the already-expanded base class. */
11115 base = TREE_VEC_ELT (expanded_bases, idx);
11116 else
11117 /* Substitute to figure out the base class. */
11118 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
11119 NULL_TREE);
11120
11121 if (base == error_mark_node)
11122 continue;
11123
11124 base_list = tree_cons (access, base, base_list);
11125 if (BINFO_VIRTUAL_P (pbase_binfo))
11126 TREE_TYPE (base_list) = integer_type_node;
11127 }
11128 }
11129
11130 /* The list is now in reverse order; correct that. */
11131 base_list = nreverse (base_list);
11132
11133 if (pushed_scope)
11134 pop_scope (pushed_scope);
11135 }
11136 /* Now call xref_basetypes to set up all the base-class
11137 information. */
11138 xref_basetypes (type, base_list);
11139
11140 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
11141 (int) ATTR_FLAG_TYPE_IN_PLACE,
11142 args, tf_error, NULL_TREE);
11143 fixup_attribute_variants (type);
11144
11145 /* Now that our base classes are set up, enter the scope of the
11146 class, so that name lookups into base classes, etc. will work
11147 correctly. This is precisely analogous to what we do in
11148 begin_class_definition when defining an ordinary non-template
11149 class, except we also need to push the enclosing classes. */
11150 push_nested_class (type);
11151
11152 /* Now members are processed in the order of declaration. */
11153 for (member = CLASSTYPE_DECL_LIST (pattern);
11154 member; member = TREE_CHAIN (member))
11155 {
11156 tree t = TREE_VALUE (member);
11157
11158 if (TREE_PURPOSE (member))
11159 {
11160 if (TYPE_P (t))
11161 {
11162 if (LAMBDA_TYPE_P (t))
11163 /* A closure type for a lambda in an NSDMI or default argument.
11164 Ignore it; it will be regenerated when needed. */
11165 continue;
11166
11167 /* Build new CLASSTYPE_NESTED_UTDS. */
11168
11169 tree newtag;
11170 bool class_template_p;
11171
11172 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
11173 && TYPE_LANG_SPECIFIC (t)
11174 && CLASSTYPE_IS_TEMPLATE (t));
11175 /* If the member is a class template, then -- even after
11176 substitution -- there may be dependent types in the
11177 template argument list for the class. We increment
11178 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11179 that function will assume that no types are dependent
11180 when outside of a template. */
11181 if (class_template_p)
11182 ++processing_template_decl;
11183 newtag = tsubst (t, args, tf_error, NULL_TREE);
11184 if (class_template_p)
11185 --processing_template_decl;
11186 if (newtag == error_mark_node)
11187 continue;
11188
11189 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
11190 {
11191 tree name = TYPE_IDENTIFIER (t);
11192
11193 if (class_template_p)
11194 /* Unfortunately, lookup_template_class sets
11195 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11196 instantiation (i.e., for the type of a member
11197 template class nested within a template class.)
11198 This behavior is required for
11199 maybe_process_partial_specialization to work
11200 correctly, but is not accurate in this case;
11201 the TAG is not an instantiation of anything.
11202 (The corresponding TEMPLATE_DECL is an
11203 instantiation, but the TYPE is not.) */
11204 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
11205
11206 /* Now, we call pushtag to put this NEWTAG into the scope of
11207 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
11208 pushtag calling push_template_decl. We don't have to do
11209 this for enums because it will already have been done in
11210 tsubst_enum. */
11211 if (name)
11212 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
11213 pushtag (name, newtag, /*tag_scope=*/ts_current);
11214 }
11215 }
11216 else if (DECL_DECLARES_FUNCTION_P (t))
11217 {
11218 tree r;
11219
11220 if (TREE_CODE (t) == TEMPLATE_DECL)
11221 ++processing_template_decl;
11222 r = tsubst (t, args, tf_error, NULL_TREE);
11223 if (TREE_CODE (t) == TEMPLATE_DECL)
11224 --processing_template_decl;
11225 set_current_access_from_decl (r);
11226 finish_member_declaration (r);
11227 /* Instantiate members marked with attribute used. */
11228 if (r != error_mark_node && DECL_PRESERVE_P (r))
11229 mark_used (r);
11230 if (TREE_CODE (r) == FUNCTION_DECL
11231 && DECL_OMP_DECLARE_REDUCTION_P (r))
11232 cp_check_omp_declare_reduction (r);
11233 }
11234 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
11235 && LAMBDA_TYPE_P (TREE_TYPE (t)))
11236 /* A closure type for a lambda in an NSDMI or default argument.
11237 Ignore it; it will be regenerated when needed. */;
11238 else
11239 {
11240 /* Build new TYPE_FIELDS. */
11241 if (TREE_CODE (t) == STATIC_ASSERT)
11242 {
11243 tree condition;
11244
11245 ++c_inhibit_evaluation_warnings;
11246 condition =
11247 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
11248 tf_warning_or_error, NULL_TREE,
11249 /*integral_constant_expression_p=*/true);
11250 --c_inhibit_evaluation_warnings;
11251
11252 finish_static_assert (condition,
11253 STATIC_ASSERT_MESSAGE (t),
11254 STATIC_ASSERT_SOURCE_LOCATION (t),
11255 /*member_p=*/true);
11256 }
11257 else if (TREE_CODE (t) != CONST_DECL)
11258 {
11259 tree r;
11260 tree vec = NULL_TREE;
11261 int len = 1;
11262
11263 /* The file and line for this declaration, to
11264 assist in error message reporting. Since we
11265 called push_tinst_level above, we don't need to
11266 restore these. */
11267 input_location = DECL_SOURCE_LOCATION (t);
11268
11269 if (TREE_CODE (t) == TEMPLATE_DECL)
11270 ++processing_template_decl;
11271 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
11272 if (TREE_CODE (t) == TEMPLATE_DECL)
11273 --processing_template_decl;
11274
11275 if (TREE_CODE (r) == TREE_VEC)
11276 {
11277 /* A capture pack became multiple fields. */
11278 vec = r;
11279 len = TREE_VEC_LENGTH (vec);
11280 }
11281
11282 for (int i = 0; i < len; ++i)
11283 {
11284 if (vec)
11285 r = TREE_VEC_ELT (vec, i);
11286 if (VAR_P (r))
11287 {
11288 /* In [temp.inst]:
11289
11290 [t]he initialization (and any associated
11291 side-effects) of a static data member does
11292 not occur unless the static data member is
11293 itself used in a way that requires the
11294 definition of the static data member to
11295 exist.
11296
11297 Therefore, we do not substitute into the
11298 initialized for the static data member here. */
11299 finish_static_data_member_decl
11300 (r,
11301 /*init=*/NULL_TREE,
11302 /*init_const_expr_p=*/false,
11303 /*asmspec_tree=*/NULL_TREE,
11304 /*flags=*/0);
11305 /* Instantiate members marked with attribute used. */
11306 if (r != error_mark_node && DECL_PRESERVE_P (r))
11307 mark_used (r);
11308 }
11309 else if (TREE_CODE (r) == FIELD_DECL)
11310 {
11311 /* Determine whether R has a valid type and can be
11312 completed later. If R is invalid, then its type
11313 is replaced by error_mark_node. */
11314 tree rtype = TREE_TYPE (r);
11315 if (can_complete_type_without_circularity (rtype))
11316 complete_type (rtype);
11317
11318 if (!complete_or_array_type_p (rtype))
11319 {
11320 /* If R's type couldn't be completed and
11321 it isn't a flexible array member (whose
11322 type is incomplete by definition) give
11323 an error. */
11324 cxx_incomplete_type_error (r, rtype);
11325 TREE_TYPE (r) = error_mark_node;
11326 }
11327 else if (TREE_CODE (rtype) == ARRAY_TYPE
11328 && TYPE_DOMAIN (rtype) == NULL_TREE
11329 && (TREE_CODE (type) == UNION_TYPE
11330 || TREE_CODE (type) == QUAL_UNION_TYPE))
11331 {
11332 error ("flexible array member %qD in union", r);
11333 TREE_TYPE (r) = error_mark_node;
11334 }
11335 }
11336
11337 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
11338 such a thing will already have been added to the field
11339 list by tsubst_enum in finish_member_declaration in the
11340 CLASSTYPE_NESTED_UTDS case above. */
11341 if (!(TREE_CODE (r) == TYPE_DECL
11342 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
11343 && DECL_ARTIFICIAL (r)))
11344 {
11345 set_current_access_from_decl (r);
11346 finish_member_declaration (r);
11347 }
11348 }
11349 }
11350 }
11351 }
11352 else
11353 {
11354 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
11355 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
11356 {
11357 /* Build new CLASSTYPE_FRIEND_CLASSES. */
11358
11359 tree friend_type = t;
11360 bool adjust_processing_template_decl = false;
11361
11362 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11363 {
11364 /* template <class T> friend class C; */
11365 friend_type = tsubst_friend_class (friend_type, args);
11366 adjust_processing_template_decl = true;
11367 }
11368 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
11369 {
11370 /* template <class T> friend class C::D; */
11371 friend_type = tsubst (friend_type, args,
11372 tf_warning_or_error, NULL_TREE);
11373 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
11374 friend_type = TREE_TYPE (friend_type);
11375 adjust_processing_template_decl = true;
11376 }
11377 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
11378 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
11379 {
11380 /* This could be either
11381
11382 friend class T::C;
11383
11384 when dependent_type_p is false or
11385
11386 template <class U> friend class T::C;
11387
11388 otherwise. */
11389 /* Bump processing_template_decl in case this is something like
11390 template <class T> friend struct A<T>::B. */
11391 ++processing_template_decl;
11392 friend_type = tsubst (friend_type, args,
11393 tf_warning_or_error, NULL_TREE);
11394 if (dependent_type_p (friend_type))
11395 adjust_processing_template_decl = true;
11396 --processing_template_decl;
11397 }
11398 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
11399 && !CLASSTYPE_USE_TEMPLATE (friend_type)
11400 && TYPE_HIDDEN_P (friend_type))
11401 {
11402 /* friend class C;
11403
11404 where C hasn't been declared yet. Let's lookup name
11405 from namespace scope directly, bypassing any name that
11406 come from dependent base class. */
11407 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
11408
11409 /* The call to xref_tag_from_type does injection for friend
11410 classes. */
11411 push_nested_namespace (ns);
11412 friend_type =
11413 xref_tag_from_type (friend_type, NULL_TREE,
11414 /*tag_scope=*/ts_current);
11415 pop_nested_namespace (ns);
11416 }
11417 else if (uses_template_parms (friend_type))
11418 /* friend class C<T>; */
11419 friend_type = tsubst (friend_type, args,
11420 tf_warning_or_error, NULL_TREE);
11421 /* Otherwise it's
11422
11423 friend class C;
11424
11425 where C is already declared or
11426
11427 friend class C<int>;
11428
11429 We don't have to do anything in these cases. */
11430
11431 if (adjust_processing_template_decl)
11432 /* Trick make_friend_class into realizing that the friend
11433 we're adding is a template, not an ordinary class. It's
11434 important that we use make_friend_class since it will
11435 perform some error-checking and output cross-reference
11436 information. */
11437 ++processing_template_decl;
11438
11439 if (friend_type != error_mark_node)
11440 make_friend_class (type, friend_type, /*complain=*/false);
11441
11442 if (adjust_processing_template_decl)
11443 --processing_template_decl;
11444 }
11445 else
11446 {
11447 /* Build new DECL_FRIENDLIST. */
11448 tree r;
11449
11450 /* The file and line for this declaration, to
11451 assist in error message reporting. Since we
11452 called push_tinst_level above, we don't need to
11453 restore these. */
11454 input_location = DECL_SOURCE_LOCATION (t);
11455
11456 if (TREE_CODE (t) == TEMPLATE_DECL)
11457 {
11458 ++processing_template_decl;
11459 push_deferring_access_checks (dk_no_check);
11460 }
11461
11462 r = tsubst_friend_function (t, args);
11463 add_friend (type, r, /*complain=*/false);
11464 if (TREE_CODE (t) == TEMPLATE_DECL)
11465 {
11466 pop_deferring_access_checks ();
11467 --processing_template_decl;
11468 }
11469 }
11470 }
11471 }
11472
11473 if (fn_context)
11474 {
11475 /* Restore these before substituting into the lambda capture
11476 initializers. */
11477 cp_unevaluated_operand = saved_unevaluated_operand;
11478 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
11479 }
11480
11481 /* Set the file and line number information to whatever is given for
11482 the class itself. This puts error messages involving generated
11483 implicit functions at a predictable point, and the same point
11484 that would be used for non-template classes. */
11485 input_location = DECL_SOURCE_LOCATION (typedecl);
11486
11487 unreverse_member_declarations (type);
11488 finish_struct_1 (type);
11489 TYPE_BEING_DEFINED (type) = 0;
11490
11491 /* We don't instantiate default arguments for member functions. 14.7.1:
11492
11493 The implicit instantiation of a class template specialization causes
11494 the implicit instantiation of the declarations, but not of the
11495 definitions or default arguments, of the class member functions,
11496 member classes, static data members and member templates.... */
11497
11498 /* Some typedefs referenced from within the template code need to be access
11499 checked at template instantiation time, i.e now. These types were
11500 added to the template at parsing time. Let's get those and perform
11501 the access checks then. */
11502 perform_typedefs_access_check (pattern, args);
11503 perform_deferred_access_checks (tf_warning_or_error);
11504 pop_nested_class ();
11505 maximum_field_alignment = saved_maximum_field_alignment;
11506 if (!fn_context)
11507 pop_from_top_level ();
11508 pop_tinst_level ();
11509
11510 /* The vtable for a template class can be emitted in any translation
11511 unit in which the class is instantiated. When there is no key
11512 method, however, finish_struct_1 will already have added TYPE to
11513 the keyed_classes. */
11514 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
11515 vec_safe_push (keyed_classes, type);
11516
11517 return type;
11518 }
11519
11520 /* Wrapper for instantiate_class_template_1. */
11521
11522 tree
11523 instantiate_class_template (tree type)
11524 {
11525 tree ret;
11526 timevar_push (TV_TEMPLATE_INST);
11527 ret = instantiate_class_template_1 (type);
11528 timevar_pop (TV_TEMPLATE_INST);
11529 return ret;
11530 }
11531
11532 static tree
11533 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11534 {
11535 tree r;
11536
11537 if (!t)
11538 r = t;
11539 else if (TYPE_P (t))
11540 r = tsubst (t, args, complain, in_decl);
11541 else
11542 {
11543 if (!(complain & tf_warning))
11544 ++c_inhibit_evaluation_warnings;
11545 r = tsubst_expr (t, args, complain, in_decl,
11546 /*integral_constant_expression_p=*/true);
11547 if (!(complain & tf_warning))
11548 --c_inhibit_evaluation_warnings;
11549 }
11550 return r;
11551 }
11552
11553 /* Given a function parameter pack TMPL_PARM and some function parameters
11554 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
11555 and set *SPEC_P to point at the next point in the list. */
11556
11557 tree
11558 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
11559 {
11560 /* Collect all of the extra "packed" parameters into an
11561 argument pack. */
11562 tree parmvec;
11563 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
11564 tree spec_parm = *spec_p;
11565 int i, len;
11566
11567 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
11568 if (tmpl_parm
11569 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
11570 break;
11571
11572 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
11573 parmvec = make_tree_vec (len);
11574 spec_parm = *spec_p;
11575 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
11576 {
11577 tree elt = spec_parm;
11578 if (DECL_PACK_P (elt))
11579 elt = make_pack_expansion (elt);
11580 TREE_VEC_ELT (parmvec, i) = elt;
11581 }
11582
11583 /* Build the argument packs. */
11584 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
11585 *spec_p = spec_parm;
11586
11587 return argpack;
11588 }
11589
11590 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
11591 NONTYPE_ARGUMENT_PACK. */
11592
11593 static tree
11594 make_fnparm_pack (tree spec_parm)
11595 {
11596 return extract_fnparm_pack (NULL_TREE, &spec_parm);
11597 }
11598
11599 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
11600 pack expansion with no extra args, 2 if it has extra args, or 0
11601 if it is not a pack expansion. */
11602
11603 static int
11604 argument_pack_element_is_expansion_p (tree arg_pack, int i)
11605 {
11606 if (TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
11607 /* We're being called before this happens in tsubst_pack_expansion. */
11608 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
11609 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
11610 if (i >= TREE_VEC_LENGTH (vec))
11611 return 0;
11612 tree elt = TREE_VEC_ELT (vec, i);
11613 if (DECL_P (elt))
11614 /* A decl pack is itself an expansion. */
11615 elt = TREE_TYPE (elt);
11616 if (!PACK_EXPANSION_P (elt))
11617 return 0;
11618 if (PACK_EXPANSION_EXTRA_ARGS (elt))
11619 return 2;
11620 return 1;
11621 }
11622
11623
11624 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11625
11626 static tree
11627 make_argument_pack_select (tree arg_pack, unsigned index)
11628 {
11629 tree aps = make_node (ARGUMENT_PACK_SELECT);
11630
11631 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
11632 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11633
11634 return aps;
11635 }
11636
11637 /* This is a subroutine of tsubst_pack_expansion.
11638
11639 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11640 mechanism to store the (non complete list of) arguments of the
11641 substitution and return a non substituted pack expansion, in order
11642 to wait for when we have enough arguments to really perform the
11643 substitution. */
11644
11645 static bool
11646 use_pack_expansion_extra_args_p (tree parm_packs,
11647 int arg_pack_len,
11648 bool has_empty_arg)
11649 {
11650 /* If one pack has an expansion and another pack has a normal
11651 argument or if one pack has an empty argument and an another
11652 one hasn't then tsubst_pack_expansion cannot perform the
11653 substitution and need to fall back on the
11654 PACK_EXPANSION_EXTRA mechanism. */
11655 if (parm_packs == NULL_TREE)
11656 return false;
11657 else if (has_empty_arg)
11658 return true;
11659
11660 bool has_expansion_arg = false;
11661 for (int i = 0 ; i < arg_pack_len; ++i)
11662 {
11663 bool has_non_expansion_arg = false;
11664 for (tree parm_pack = parm_packs;
11665 parm_pack;
11666 parm_pack = TREE_CHAIN (parm_pack))
11667 {
11668 tree arg = TREE_VALUE (parm_pack);
11669
11670 int exp = argument_pack_element_is_expansion_p (arg, i);
11671 if (exp == 2)
11672 /* We can't substitute a pack expansion with extra args into
11673 our pattern. */
11674 return true;
11675 else if (exp)
11676 has_expansion_arg = true;
11677 else
11678 has_non_expansion_arg = true;
11679 }
11680
11681 if (has_expansion_arg && has_non_expansion_arg)
11682 return true;
11683 }
11684 return false;
11685 }
11686
11687 /* [temp.variadic]/6 says that:
11688
11689 The instantiation of a pack expansion [...]
11690 produces a list E1,E2, ..., En, where N is the number of elements
11691 in the pack expansion parameters.
11692
11693 This subroutine of tsubst_pack_expansion produces one of these Ei.
11694
11695 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11696 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11697 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11698 INDEX is the index 'i' of the element Ei to produce. ARGS,
11699 COMPLAIN, and IN_DECL are the same parameters as for the
11700 tsubst_pack_expansion function.
11701
11702 The function returns the resulting Ei upon successful completion,
11703 or error_mark_node.
11704
11705 Note that this function possibly modifies the ARGS parameter, so
11706 it's the responsibility of the caller to restore it. */
11707
11708 static tree
11709 gen_elem_of_pack_expansion_instantiation (tree pattern,
11710 tree parm_packs,
11711 unsigned index,
11712 tree args /* This parm gets
11713 modified. */,
11714 tsubst_flags_t complain,
11715 tree in_decl)
11716 {
11717 tree t;
11718 bool ith_elem_is_expansion = false;
11719
11720 /* For each parameter pack, change the substitution of the parameter
11721 pack to the ith argument in its argument pack, then expand the
11722 pattern. */
11723 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
11724 {
11725 tree parm = TREE_PURPOSE (pack);
11726 tree arg_pack = TREE_VALUE (pack);
11727 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
11728
11729 ith_elem_is_expansion |=
11730 argument_pack_element_is_expansion_p (arg_pack, index);
11731
11732 /* Select the Ith argument from the pack. */
11733 if (TREE_CODE (parm) == PARM_DECL
11734 || VAR_P (parm)
11735 || TREE_CODE (parm) == FIELD_DECL)
11736 {
11737 if (index == 0)
11738 {
11739 aps = make_argument_pack_select (arg_pack, index);
11740 if (!mark_used (parm, complain) && !(complain & tf_error))
11741 return error_mark_node;
11742 register_local_specialization (aps, parm);
11743 }
11744 else
11745 aps = retrieve_local_specialization (parm);
11746 }
11747 else
11748 {
11749 int idx, level;
11750 template_parm_level_and_index (parm, &level, &idx);
11751
11752 if (index == 0)
11753 {
11754 aps = make_argument_pack_select (arg_pack, index);
11755 /* Update the corresponding argument. */
11756 TMPL_ARG (args, level, idx) = aps;
11757 }
11758 else
11759 /* Re-use the ARGUMENT_PACK_SELECT. */
11760 aps = TMPL_ARG (args, level, idx);
11761 }
11762 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11763 }
11764
11765 /* Substitute into the PATTERN with the (possibly altered)
11766 arguments. */
11767 if (pattern == in_decl)
11768 /* Expanding a fixed parameter pack from
11769 coerce_template_parameter_pack. */
11770 t = tsubst_decl (pattern, args, complain);
11771 else if (pattern == error_mark_node)
11772 t = error_mark_node;
11773 else if (constraint_p (pattern))
11774 {
11775 if (processing_template_decl)
11776 t = tsubst_constraint (pattern, args, complain, in_decl);
11777 else
11778 t = (constraints_satisfied_p (pattern, args)
11779 ? boolean_true_node : boolean_false_node);
11780 }
11781 else if (!TYPE_P (pattern))
11782 t = tsubst_expr (pattern, args, complain, in_decl,
11783 /*integral_constant_expression_p=*/false);
11784 else
11785 t = tsubst (pattern, args, complain, in_decl);
11786
11787 /* If the Ith argument pack element is a pack expansion, then
11788 the Ith element resulting from the substituting is going to
11789 be a pack expansion as well. */
11790 if (ith_elem_is_expansion)
11791 t = make_pack_expansion (t, complain);
11792
11793 return t;
11794 }
11795
11796 /* When the unexpanded parameter pack in a fold expression expands to an empty
11797 sequence, the value of the expression is as follows; the program is
11798 ill-formed if the operator is not listed in this table.
11799
11800 && true
11801 || false
11802 , void() */
11803
11804 tree
11805 expand_empty_fold (tree t, tsubst_flags_t complain)
11806 {
11807 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
11808 if (!FOLD_EXPR_MODIFY_P (t))
11809 switch (code)
11810 {
11811 case TRUTH_ANDIF_EXPR:
11812 return boolean_true_node;
11813 case TRUTH_ORIF_EXPR:
11814 return boolean_false_node;
11815 case COMPOUND_EXPR:
11816 return void_node;
11817 default:
11818 break;
11819 }
11820
11821 if (complain & tf_error)
11822 error_at (location_of (t),
11823 "fold of empty expansion over %O", code);
11824 return error_mark_node;
11825 }
11826
11827 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11828 form an expression that combines the two terms using the
11829 operator of T. */
11830
11831 static tree
11832 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
11833 {
11834 tree op = FOLD_EXPR_OP (t);
11835 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
11836
11837 // Handle compound assignment operators.
11838 if (FOLD_EXPR_MODIFY_P (t))
11839 return build_x_modify_expr (input_location, left, code, right, complain);
11840
11841 switch (code)
11842 {
11843 case COMPOUND_EXPR:
11844 return build_x_compound_expr (input_location, left, right, complain);
11845 default:
11846 return build_x_binary_op (input_location, code,
11847 left, TREE_CODE (left),
11848 right, TREE_CODE (right),
11849 /*overload=*/NULL,
11850 complain);
11851 }
11852 }
11853
11854 /* Substitute ARGS into the pack of a fold expression T. */
11855
11856 static inline tree
11857 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11858 {
11859 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
11860 }
11861
11862 /* Substitute ARGS into the pack of a fold expression T. */
11863
11864 static inline tree
11865 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11866 {
11867 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
11868 }
11869
11870 /* Expand a PACK of arguments into a grouped as left fold.
11871 Given a pack containing elements A0, A1, ..., An and an
11872 operator @, this builds the expression:
11873
11874 ((A0 @ A1) @ A2) ... @ An
11875
11876 Note that PACK must not be empty.
11877
11878 The operator is defined by the original fold expression T. */
11879
11880 static tree
11881 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
11882 {
11883 tree left = TREE_VEC_ELT (pack, 0);
11884 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
11885 {
11886 tree right = TREE_VEC_ELT (pack, i);
11887 left = fold_expression (t, left, right, complain);
11888 }
11889 return left;
11890 }
11891
11892 /* Substitute into a unary left fold expression. */
11893
11894 static tree
11895 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
11896 tree in_decl)
11897 {
11898 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11899 if (pack == error_mark_node)
11900 return error_mark_node;
11901 if (PACK_EXPANSION_P (pack))
11902 {
11903 tree r = copy_node (t);
11904 FOLD_EXPR_PACK (r) = pack;
11905 return r;
11906 }
11907 if (TREE_VEC_LENGTH (pack) == 0)
11908 return expand_empty_fold (t, complain);
11909 else
11910 return expand_left_fold (t, pack, complain);
11911 }
11912
11913 /* Substitute into a binary left fold expression.
11914
11915 Do ths by building a single (non-empty) vector of argumnts and
11916 building the expression from those elements. */
11917
11918 static tree
11919 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
11920 tree in_decl)
11921 {
11922 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11923 if (pack == error_mark_node)
11924 return error_mark_node;
11925 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11926 if (init == error_mark_node)
11927 return error_mark_node;
11928
11929 if (PACK_EXPANSION_P (pack))
11930 {
11931 tree r = copy_node (t);
11932 FOLD_EXPR_PACK (r) = pack;
11933 FOLD_EXPR_INIT (r) = init;
11934 return r;
11935 }
11936
11937 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
11938 TREE_VEC_ELT (vec, 0) = init;
11939 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
11940 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
11941
11942 return expand_left_fold (t, vec, complain);
11943 }
11944
11945 /* Expand a PACK of arguments into a grouped as right fold.
11946 Given a pack containing elementns A0, A1, ..., and an
11947 operator @, this builds the expression:
11948
11949 A0@ ... (An-2 @ (An-1 @ An))
11950
11951 Note that PACK must not be empty.
11952
11953 The operator is defined by the original fold expression T. */
11954
11955 tree
11956 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
11957 {
11958 // Build the expression.
11959 int n = TREE_VEC_LENGTH (pack);
11960 tree right = TREE_VEC_ELT (pack, n - 1);
11961 for (--n; n != 0; --n)
11962 {
11963 tree left = TREE_VEC_ELT (pack, n - 1);
11964 right = fold_expression (t, left, right, complain);
11965 }
11966 return right;
11967 }
11968
11969 /* Substitute into a unary right fold expression. */
11970
11971 static tree
11972 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
11973 tree in_decl)
11974 {
11975 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11976 if (pack == error_mark_node)
11977 return error_mark_node;
11978 if (PACK_EXPANSION_P (pack))
11979 {
11980 tree r = copy_node (t);
11981 FOLD_EXPR_PACK (r) = pack;
11982 return r;
11983 }
11984 if (TREE_VEC_LENGTH (pack) == 0)
11985 return expand_empty_fold (t, complain);
11986 else
11987 return expand_right_fold (t, pack, complain);
11988 }
11989
11990 /* Substitute into a binary right fold expression.
11991
11992 Do ths by building a single (non-empty) vector of arguments and
11993 building the expression from those elements. */
11994
11995 static tree
11996 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
11997 tree in_decl)
11998 {
11999 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12000 if (pack == error_mark_node)
12001 return error_mark_node;
12002 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12003 if (init == error_mark_node)
12004 return error_mark_node;
12005
12006 if (PACK_EXPANSION_P (pack))
12007 {
12008 tree r = copy_node (t);
12009 FOLD_EXPR_PACK (r) = pack;
12010 FOLD_EXPR_INIT (r) = init;
12011 return r;
12012 }
12013
12014 int n = TREE_VEC_LENGTH (pack);
12015 tree vec = make_tree_vec (n + 1);
12016 for (int i = 0; i < n; ++i)
12017 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
12018 TREE_VEC_ELT (vec, n) = init;
12019
12020 return expand_right_fold (t, vec, complain);
12021 }
12022
12023 /* Walk through the pattern of a pack expansion, adding everything in
12024 local_specializations to a list. */
12025
12026 class el_data
12027 {
12028 public:
12029 hash_set<tree> internal;
12030 tree extra;
12031 tsubst_flags_t complain;
12032
12033 el_data (tsubst_flags_t c)
12034 : extra (NULL_TREE), complain (c) {}
12035 };
12036 static tree
12037 extract_locals_r (tree *tp, int */*walk_subtrees*/, void *data_)
12038 {
12039 el_data &data = *reinterpret_cast<el_data*>(data_);
12040 tree *extra = &data.extra;
12041 tsubst_flags_t complain = data.complain;
12042
12043 if (TYPE_P (*tp) && typedef_variant_p (*tp))
12044 /* Remember local typedefs (85214). */
12045 tp = &TYPE_NAME (*tp);
12046
12047 if (TREE_CODE (*tp) == DECL_EXPR)
12048 data.internal.add (DECL_EXPR_DECL (*tp));
12049 else if (tree spec = retrieve_local_specialization (*tp))
12050 {
12051 if (data.internal.contains (*tp))
12052 /* Don't mess with variables declared within the pattern. */
12053 return NULL_TREE;
12054 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12055 {
12056 /* Maybe pull out the PARM_DECL for a partial instantiation. */
12057 tree args = ARGUMENT_PACK_ARGS (spec);
12058 if (TREE_VEC_LENGTH (args) == 1)
12059 {
12060 tree elt = TREE_VEC_ELT (args, 0);
12061 if (PACK_EXPANSION_P (elt))
12062 elt = PACK_EXPANSION_PATTERN (elt);
12063 if (DECL_PACK_P (elt))
12064 spec = elt;
12065 }
12066 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12067 {
12068 /* Handle lambda capture here, since we aren't doing any
12069 substitution now, and so tsubst_copy won't call
12070 process_outer_var_ref. */
12071 tree args = ARGUMENT_PACK_ARGS (spec);
12072 int len = TREE_VEC_LENGTH (args);
12073 for (int i = 0; i < len; ++i)
12074 {
12075 tree arg = TREE_VEC_ELT (args, i);
12076 tree carg = arg;
12077 if (outer_automatic_var_p (arg))
12078 carg = process_outer_var_ref (arg, complain);
12079 if (carg != arg)
12080 {
12081 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12082 proxies. */
12083 if (i == 0)
12084 {
12085 spec = copy_node (spec);
12086 args = copy_node (args);
12087 SET_ARGUMENT_PACK_ARGS (spec, args);
12088 register_local_specialization (spec, *tp);
12089 }
12090 TREE_VEC_ELT (args, i) = carg;
12091 }
12092 }
12093 }
12094 }
12095 if (outer_automatic_var_p (spec))
12096 spec = process_outer_var_ref (spec, complain);
12097 *extra = tree_cons (*tp, spec, *extra);
12098 }
12099 return NULL_TREE;
12100 }
12101 static tree
12102 extract_local_specs (tree pattern, tsubst_flags_t complain)
12103 {
12104 el_data data (complain);
12105 cp_walk_tree_without_duplicates (&pattern, extract_locals_r, &data);
12106 return data.extra;
12107 }
12108
12109 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12110 for use in PACK_EXPANSION_EXTRA_ARGS. */
12111
12112 tree
12113 build_extra_args (tree pattern, tree args, tsubst_flags_t complain)
12114 {
12115 tree extra = args;
12116 if (local_specializations)
12117 if (tree locals = extract_local_specs (pattern, complain))
12118 extra = tree_cons (NULL_TREE, extra, locals);
12119 return extra;
12120 }
12121
12122 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12123 normal template args to ARGS. */
12124
12125 tree
12126 add_extra_args (tree extra, tree args)
12127 {
12128 if (extra && TREE_CODE (extra) == TREE_LIST)
12129 {
12130 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
12131 {
12132 /* The partial instantiation involved local declarations collected in
12133 extract_local_specs; map from the general template to our local
12134 context. */
12135 tree gen = TREE_PURPOSE (elt);
12136 tree inst = TREE_VALUE (elt);
12137 if (DECL_P (inst))
12138 if (tree local = retrieve_local_specialization (inst))
12139 inst = local;
12140 /* else inst is already a full instantiation of the pack. */
12141 register_local_specialization (inst, gen);
12142 }
12143 gcc_assert (!TREE_PURPOSE (extra));
12144 extra = TREE_VALUE (extra);
12145 }
12146 return add_to_template_args (extra, args);
12147 }
12148
12149 /* Substitute ARGS into T, which is an pack expansion
12150 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12151 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12152 (if only a partial substitution could be performed) or
12153 ERROR_MARK_NODE if there was an error. */
12154 tree
12155 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
12156 tree in_decl)
12157 {
12158 tree pattern;
12159 tree pack, packs = NULL_TREE;
12160 bool unsubstituted_packs = false;
12161 bool unsubstituted_fn_pack = false;
12162 int i, len = -1;
12163 tree result;
12164 hash_map<tree, tree> *saved_local_specializations = NULL;
12165 bool need_local_specializations = false;
12166 int levels;
12167
12168 gcc_assert (PACK_EXPANSION_P (t));
12169 pattern = PACK_EXPANSION_PATTERN (t);
12170
12171 /* Add in any args remembered from an earlier partial instantiation. */
12172 args = add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
12173
12174 levels = TMPL_ARGS_DEPTH (args);
12175
12176 /* Determine the argument packs that will instantiate the parameter
12177 packs used in the expansion expression. While we're at it,
12178 compute the number of arguments to be expanded and make sure it
12179 is consistent. */
12180 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
12181 pack = TREE_CHAIN (pack))
12182 {
12183 tree parm_pack = TREE_VALUE (pack);
12184 tree arg_pack = NULL_TREE;
12185 tree orig_arg = NULL_TREE;
12186 int level = 0;
12187
12188 if (TREE_CODE (parm_pack) == BASES)
12189 {
12190 gcc_assert (parm_pack == pattern);
12191 if (BASES_DIRECT (parm_pack))
12192 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
12193 args, complain,
12194 in_decl, false),
12195 complain);
12196 else
12197 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
12198 args, complain, in_decl,
12199 false), complain);
12200 }
12201 else if (builtin_pack_call_p (parm_pack))
12202 {
12203 if (parm_pack != pattern)
12204 {
12205 if (complain & tf_error)
12206 sorry ("%qE is not the entire pattern of the pack expansion",
12207 parm_pack);
12208 return error_mark_node;
12209 }
12210 return expand_builtin_pack_call (parm_pack, args,
12211 complain, in_decl);
12212 }
12213 else if (TREE_CODE (parm_pack) == PARM_DECL)
12214 {
12215 /* We know we have correct local_specializations if this
12216 expansion is at function scope, or if we're dealing with a
12217 local parameter in a requires expression; for the latter,
12218 tsubst_requires_expr set it up appropriately. */
12219 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
12220 arg_pack = retrieve_local_specialization (parm_pack);
12221 else
12222 /* We can't rely on local_specializations for a parameter
12223 name used later in a function declaration (such as in a
12224 late-specified return type). Even if it exists, it might
12225 have the wrong value for a recursive call. */
12226 need_local_specializations = true;
12227
12228 if (!arg_pack)
12229 {
12230 /* This parameter pack was used in an unevaluated context. Just
12231 make a dummy decl, since it's only used for its type. */
12232 ++cp_unevaluated_operand;
12233 arg_pack = tsubst_decl (parm_pack, args, complain);
12234 --cp_unevaluated_operand;
12235 if (arg_pack && DECL_PACK_P (arg_pack))
12236 /* Partial instantiation of the parm_pack, we can't build
12237 up an argument pack yet. */
12238 arg_pack = NULL_TREE;
12239 else
12240 arg_pack = make_fnparm_pack (arg_pack);
12241 }
12242 else if (argument_pack_element_is_expansion_p (arg_pack, 0))
12243 /* This argument pack isn't fully instantiated yet. We set this
12244 flag rather than clear arg_pack because we do want to do the
12245 optimization below, and we don't want to substitute directly
12246 into the pattern (as that would expose a NONTYPE_ARGUMENT_PACK
12247 where it isn't expected). */
12248 unsubstituted_fn_pack = true;
12249 }
12250 else if (is_capture_proxy (parm_pack))
12251 {
12252 arg_pack = retrieve_local_specialization (parm_pack);
12253 if (argument_pack_element_is_expansion_p (arg_pack, 0))
12254 unsubstituted_fn_pack = true;
12255 }
12256 else
12257 {
12258 int idx;
12259 template_parm_level_and_index (parm_pack, &level, &idx);
12260
12261 if (level <= levels)
12262 arg_pack = TMPL_ARG (args, level, idx);
12263 }
12264
12265 orig_arg = arg_pack;
12266 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12267 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12268
12269 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
12270 /* This can only happen if we forget to expand an argument
12271 pack somewhere else. Just return an error, silently. */
12272 {
12273 result = make_tree_vec (1);
12274 TREE_VEC_ELT (result, 0) = error_mark_node;
12275 return result;
12276 }
12277
12278 if (arg_pack)
12279 {
12280 int my_len =
12281 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
12282
12283 /* Don't bother trying to do a partial substitution with
12284 incomplete packs; we'll try again after deduction. */
12285 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
12286 return t;
12287
12288 if (len < 0)
12289 len = my_len;
12290 else if (len != my_len
12291 && !unsubstituted_fn_pack)
12292 {
12293 if (!(complain & tf_error))
12294 /* Fail quietly. */;
12295 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
12296 error ("mismatched argument pack lengths while expanding %qT",
12297 pattern);
12298 else
12299 error ("mismatched argument pack lengths while expanding %qE",
12300 pattern);
12301 return error_mark_node;
12302 }
12303
12304 /* Keep track of the parameter packs and their corresponding
12305 argument packs. */
12306 packs = tree_cons (parm_pack, arg_pack, packs);
12307 TREE_TYPE (packs) = orig_arg;
12308 }
12309 else
12310 {
12311 /* We can't substitute for this parameter pack. We use a flag as
12312 well as the missing_level counter because function parameter
12313 packs don't have a level. */
12314 gcc_assert (processing_template_decl || is_auto (parm_pack));
12315 unsubstituted_packs = true;
12316 }
12317 }
12318
12319 /* If the expansion is just T..., return the matching argument pack, unless
12320 we need to call convert_from_reference on all the elements. This is an
12321 important optimization; see c++/68422. */
12322 if (!unsubstituted_packs
12323 && TREE_PURPOSE (packs) == pattern)
12324 {
12325 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
12326
12327 /* If the argument pack is a single pack expansion, pull it out. */
12328 if (TREE_VEC_LENGTH (args) == 1
12329 && pack_expansion_args_count (args))
12330 return TREE_VEC_ELT (args, 0);
12331
12332 /* Types need no adjustment, nor does sizeof..., and if we still have
12333 some pack expansion args we won't do anything yet. */
12334 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
12335 || PACK_EXPANSION_SIZEOF_P (t)
12336 || pack_expansion_args_count (args))
12337 return args;
12338 /* Also optimize expression pack expansions if we can tell that the
12339 elements won't have reference type. */
12340 tree type = TREE_TYPE (pattern);
12341 if (type && !TYPE_REF_P (type)
12342 && !PACK_EXPANSION_P (type)
12343 && !WILDCARD_TYPE_P (type))
12344 return args;
12345 /* Otherwise use the normal path so we get convert_from_reference. */
12346 }
12347
12348 /* We cannot expand this expansion expression, because we don't have
12349 all of the argument packs we need. */
12350 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
12351 {
12352 /* We got some full packs, but we can't substitute them in until we
12353 have values for all the packs. So remember these until then. */
12354
12355 t = make_pack_expansion (pattern, complain);
12356 PACK_EXPANSION_EXTRA_ARGS (t)
12357 = build_extra_args (pattern, args, complain);
12358 return t;
12359 }
12360 else if (unsubstituted_packs)
12361 {
12362 /* There were no real arguments, we're just replacing a parameter
12363 pack with another version of itself. Substitute into the
12364 pattern and return a PACK_EXPANSION_*. The caller will need to
12365 deal with that. */
12366 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
12367 t = tsubst_expr (pattern, args, complain, in_decl,
12368 /*integral_constant_expression_p=*/false);
12369 else
12370 t = tsubst (pattern, args, complain, in_decl);
12371 t = make_pack_expansion (t, complain);
12372 return t;
12373 }
12374
12375 gcc_assert (len >= 0);
12376
12377 if (need_local_specializations)
12378 {
12379 /* We're in a late-specified return type, so create our own local
12380 specializations map; the current map is either NULL or (in the
12381 case of recursive unification) might have bindings that we don't
12382 want to use or alter. */
12383 saved_local_specializations = local_specializations;
12384 local_specializations = new hash_map<tree, tree>;
12385 }
12386
12387 /* For each argument in each argument pack, substitute into the
12388 pattern. */
12389 result = make_tree_vec (len);
12390 tree elem_args = copy_template_args (args);
12391 for (i = 0; i < len; ++i)
12392 {
12393 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
12394 i,
12395 elem_args, complain,
12396 in_decl);
12397 TREE_VEC_ELT (result, i) = t;
12398 if (t == error_mark_node)
12399 {
12400 result = error_mark_node;
12401 break;
12402 }
12403 }
12404
12405 /* Update ARGS to restore the substitution from parameter packs to
12406 their argument packs. */
12407 for (pack = packs; pack; pack = TREE_CHAIN (pack))
12408 {
12409 tree parm = TREE_PURPOSE (pack);
12410
12411 if (TREE_CODE (parm) == PARM_DECL
12412 || VAR_P (parm)
12413 || TREE_CODE (parm) == FIELD_DECL)
12414 register_local_specialization (TREE_TYPE (pack), parm);
12415 else
12416 {
12417 int idx, level;
12418
12419 if (TREE_VALUE (pack) == NULL_TREE)
12420 continue;
12421
12422 template_parm_level_and_index (parm, &level, &idx);
12423
12424 /* Update the corresponding argument. */
12425 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
12426 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
12427 TREE_TYPE (pack);
12428 else
12429 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
12430 }
12431 }
12432
12433 if (need_local_specializations)
12434 {
12435 delete local_specializations;
12436 local_specializations = saved_local_specializations;
12437 }
12438
12439 /* If the dependent pack arguments were such that we end up with only a
12440 single pack expansion again, there's no need to keep it in a TREE_VEC. */
12441 if (len == 1 && TREE_CODE (result) == TREE_VEC
12442 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
12443 return TREE_VEC_ELT (result, 0);
12444
12445 return result;
12446 }
12447
12448 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
12449 TMPL. We do this using DECL_PARM_INDEX, which should work even with
12450 parameter packs; all parms generated from a function parameter pack will
12451 have the same DECL_PARM_INDEX. */
12452
12453 tree
12454 get_pattern_parm (tree parm, tree tmpl)
12455 {
12456 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
12457 tree patparm;
12458
12459 if (DECL_ARTIFICIAL (parm))
12460 {
12461 for (patparm = DECL_ARGUMENTS (pattern);
12462 patparm; patparm = DECL_CHAIN (patparm))
12463 if (DECL_ARTIFICIAL (patparm)
12464 && DECL_NAME (parm) == DECL_NAME (patparm))
12465 break;
12466 }
12467 else
12468 {
12469 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
12470 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
12471 gcc_assert (DECL_PARM_INDEX (patparm)
12472 == DECL_PARM_INDEX (parm));
12473 }
12474
12475 return patparm;
12476 }
12477
12478 /* Make an argument pack out of the TREE_VEC VEC. */
12479
12480 static tree
12481 make_argument_pack (tree vec)
12482 {
12483 tree pack;
12484 tree elt = TREE_VEC_ELT (vec, 0);
12485 if (TYPE_P (elt))
12486 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
12487 else
12488 {
12489 pack = make_node (NONTYPE_ARGUMENT_PACK);
12490 TREE_CONSTANT (pack) = 1;
12491 }
12492 SET_ARGUMENT_PACK_ARGS (pack, vec);
12493 return pack;
12494 }
12495
12496 /* Return an exact copy of template args T that can be modified
12497 independently. */
12498
12499 static tree
12500 copy_template_args (tree t)
12501 {
12502 if (t == error_mark_node)
12503 return t;
12504
12505 int len = TREE_VEC_LENGTH (t);
12506 tree new_vec = make_tree_vec (len);
12507
12508 for (int i = 0; i < len; ++i)
12509 {
12510 tree elt = TREE_VEC_ELT (t, i);
12511 if (elt && TREE_CODE (elt) == TREE_VEC)
12512 elt = copy_template_args (elt);
12513 TREE_VEC_ELT (new_vec, i) = elt;
12514 }
12515
12516 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
12517 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
12518
12519 return new_vec;
12520 }
12521
12522 /* Substitute ARGS into the vector or list of template arguments T. */
12523
12524 static tree
12525 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12526 {
12527 tree orig_t = t;
12528 int len, need_new = 0, i, expanded_len_adjust = 0, out;
12529 tree *elts;
12530
12531 if (t == error_mark_node)
12532 return error_mark_node;
12533
12534 len = TREE_VEC_LENGTH (t);
12535 elts = XALLOCAVEC (tree, len);
12536
12537 for (i = 0; i < len; i++)
12538 {
12539 tree orig_arg = TREE_VEC_ELT (t, i);
12540 tree new_arg;
12541
12542 if (TREE_CODE (orig_arg) == TREE_VEC)
12543 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
12544 else if (PACK_EXPANSION_P (orig_arg))
12545 {
12546 /* Substitute into an expansion expression. */
12547 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
12548
12549 if (TREE_CODE (new_arg) == TREE_VEC)
12550 /* Add to the expanded length adjustment the number of
12551 expanded arguments. We subtract one from this
12552 measurement, because the argument pack expression
12553 itself is already counted as 1 in
12554 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
12555 the argument pack is empty. */
12556 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
12557 }
12558 else if (ARGUMENT_PACK_P (orig_arg))
12559 {
12560 /* Substitute into each of the arguments. */
12561 new_arg = TYPE_P (orig_arg)
12562 ? cxx_make_type (TREE_CODE (orig_arg))
12563 : make_node (TREE_CODE (orig_arg));
12564
12565 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
12566 args, complain, in_decl);
12567 if (pack_args == error_mark_node)
12568 new_arg = error_mark_node;
12569 else
12570 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
12571
12572 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
12573 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
12574 }
12575 else
12576 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
12577
12578 if (new_arg == error_mark_node)
12579 return error_mark_node;
12580
12581 elts[i] = new_arg;
12582 if (new_arg != orig_arg)
12583 need_new = 1;
12584 }
12585
12586 if (!need_new)
12587 return t;
12588
12589 /* Make space for the expanded arguments coming from template
12590 argument packs. */
12591 t = make_tree_vec (len + expanded_len_adjust);
12592 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
12593 arguments for a member template.
12594 In that case each TREE_VEC in ORIG_T represents a level of template
12595 arguments, and ORIG_T won't carry any non defaulted argument count.
12596 It will rather be the nested TREE_VECs that will carry one.
12597 In other words, ORIG_T carries a non defaulted argument count only
12598 if it doesn't contain any nested TREE_VEC. */
12599 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
12600 {
12601 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
12602 count += expanded_len_adjust;
12603 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
12604 }
12605 for (i = 0, out = 0; i < len; i++)
12606 {
12607 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
12608 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
12609 && TREE_CODE (elts[i]) == TREE_VEC)
12610 {
12611 int idx;
12612
12613 /* Now expand the template argument pack "in place". */
12614 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
12615 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
12616 }
12617 else
12618 {
12619 TREE_VEC_ELT (t, out) = elts[i];
12620 out++;
12621 }
12622 }
12623
12624 return t;
12625 }
12626
12627 /* Substitute ARGS into one level PARMS of template parameters. */
12628
12629 static tree
12630 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
12631 {
12632 if (parms == error_mark_node)
12633 return error_mark_node;
12634
12635 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
12636
12637 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
12638 {
12639 tree tuple = TREE_VEC_ELT (parms, i);
12640
12641 if (tuple == error_mark_node)
12642 continue;
12643
12644 TREE_VEC_ELT (new_vec, i) =
12645 tsubst_template_parm (tuple, args, complain);
12646 }
12647
12648 return new_vec;
12649 }
12650
12651 /* Return the result of substituting ARGS into the template parameters
12652 given by PARMS. If there are m levels of ARGS and m + n levels of
12653 PARMS, then the result will contain n levels of PARMS. For
12654 example, if PARMS is `template <class T> template <class U>
12655 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
12656 result will be `template <int*, double, class V>'. */
12657
12658 static tree
12659 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
12660 {
12661 tree r = NULL_TREE;
12662 tree* new_parms;
12663
12664 /* When substituting into a template, we must set
12665 PROCESSING_TEMPLATE_DECL as the template parameters may be
12666 dependent if they are based on one-another, and the dependency
12667 predicates are short-circuit outside of templates. */
12668 ++processing_template_decl;
12669
12670 for (new_parms = &r;
12671 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
12672 new_parms = &(TREE_CHAIN (*new_parms)),
12673 parms = TREE_CHAIN (parms))
12674 {
12675 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
12676 args, complain);
12677 *new_parms =
12678 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
12679 - TMPL_ARGS_DEPTH (args)),
12680 new_vec, NULL_TREE);
12681 }
12682
12683 --processing_template_decl;
12684
12685 return r;
12686 }
12687
12688 /* Return the result of substituting ARGS into one template parameter
12689 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
12690 parameter and which TREE_PURPOSE is the default argument of the
12691 template parameter. */
12692
12693 static tree
12694 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
12695 {
12696 tree default_value, parm_decl;
12697
12698 if (args == NULL_TREE
12699 || t == NULL_TREE
12700 || t == error_mark_node)
12701 return t;
12702
12703 gcc_assert (TREE_CODE (t) == TREE_LIST);
12704
12705 default_value = TREE_PURPOSE (t);
12706 parm_decl = TREE_VALUE (t);
12707
12708 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
12709 if (TREE_CODE (parm_decl) == PARM_DECL
12710 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
12711 parm_decl = error_mark_node;
12712 default_value = tsubst_template_arg (default_value, args,
12713 complain, NULL_TREE);
12714
12715 return build_tree_list (default_value, parm_decl);
12716 }
12717
12718 /* Substitute the ARGS into the indicated aggregate (or enumeration)
12719 type T. If T is not an aggregate or enumeration type, it is
12720 handled as if by tsubst. IN_DECL is as for tsubst. If
12721 ENTERING_SCOPE is nonzero, T is the context for a template which
12722 we are presently tsubst'ing. Return the substituted value. */
12723
12724 static tree
12725 tsubst_aggr_type (tree t,
12726 tree args,
12727 tsubst_flags_t complain,
12728 tree in_decl,
12729 int entering_scope)
12730 {
12731 if (t == NULL_TREE)
12732 return NULL_TREE;
12733
12734 switch (TREE_CODE (t))
12735 {
12736 case RECORD_TYPE:
12737 if (TYPE_PTRMEMFUNC_P (t))
12738 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
12739
12740 /* Fall through. */
12741 case ENUMERAL_TYPE:
12742 case UNION_TYPE:
12743 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
12744 {
12745 tree argvec;
12746 tree context;
12747 tree r;
12748
12749 /* In "sizeof(X<I>)" we need to evaluate "I". */
12750 cp_evaluated ev;
12751
12752 /* First, determine the context for the type we are looking
12753 up. */
12754 context = TYPE_CONTEXT (t);
12755 if (context && TYPE_P (context))
12756 {
12757 context = tsubst_aggr_type (context, args, complain,
12758 in_decl, /*entering_scope=*/1);
12759 /* If context is a nested class inside a class template,
12760 it may still need to be instantiated (c++/33959). */
12761 context = complete_type (context);
12762 }
12763
12764 /* Then, figure out what arguments are appropriate for the
12765 type we are trying to find. For example, given:
12766
12767 template <class T> struct S;
12768 template <class T, class U> void f(T, U) { S<U> su; }
12769
12770 and supposing that we are instantiating f<int, double>,
12771 then our ARGS will be {int, double}, but, when looking up
12772 S we only want {double}. */
12773 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
12774 complain, in_decl);
12775 if (argvec == error_mark_node)
12776 r = error_mark_node;
12777 else
12778 {
12779 r = lookup_template_class (t, argvec, in_decl, context,
12780 entering_scope, complain);
12781 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
12782 }
12783
12784 return r;
12785 }
12786 else
12787 /* This is not a template type, so there's nothing to do. */
12788 return t;
12789
12790 default:
12791 return tsubst (t, args, complain, in_decl);
12792 }
12793 }
12794
12795 static GTY((cache)) tree_cache_map *defarg_inst;
12796
12797 /* Substitute into the default argument ARG (a default argument for
12798 FN), which has the indicated TYPE. */
12799
12800 tree
12801 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
12802 tsubst_flags_t complain)
12803 {
12804 int errs = errorcount + sorrycount;
12805
12806 /* This can happen in invalid code. */
12807 if (TREE_CODE (arg) == DEFERRED_PARSE)
12808 return arg;
12809
12810 tree parm = FUNCTION_FIRST_USER_PARM (fn);
12811 parm = chain_index (parmnum, parm);
12812 tree parmtype = TREE_TYPE (parm);
12813 if (DECL_BY_REFERENCE (parm))
12814 parmtype = TREE_TYPE (parmtype);
12815 if (parmtype == error_mark_node)
12816 return error_mark_node;
12817
12818 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
12819
12820 tree *slot;
12821 if (defarg_inst && (slot = defarg_inst->get (parm)))
12822 return *slot;
12823
12824 /* This default argument came from a template. Instantiate the
12825 default argument here, not in tsubst. In the case of
12826 something like:
12827
12828 template <class T>
12829 struct S {
12830 static T t();
12831 void f(T = t());
12832 };
12833
12834 we must be careful to do name lookup in the scope of S<T>,
12835 rather than in the current class. */
12836 push_to_top_level ();
12837 push_access_scope (fn);
12838 push_deferring_access_checks (dk_no_deferred);
12839 start_lambda_scope (parm);
12840
12841 /* The default argument expression may cause implicitly defined
12842 member functions to be synthesized, which will result in garbage
12843 collection. We must treat this situation as if we were within
12844 the body of function so as to avoid collecting live data on the
12845 stack. */
12846 ++function_depth;
12847 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
12848 complain, NULL_TREE,
12849 /*integral_constant_expression_p=*/false);
12850 --function_depth;
12851
12852 finish_lambda_scope ();
12853
12854 /* Make sure the default argument is reasonable. */
12855 arg = check_default_argument (type, arg, complain);
12856
12857 if (errorcount+sorrycount > errs
12858 && (complain & tf_warning_or_error))
12859 inform (input_location,
12860 " when instantiating default argument for call to %qD", fn);
12861
12862 pop_deferring_access_checks ();
12863 pop_access_scope (fn);
12864 pop_from_top_level ();
12865
12866 if (arg != error_mark_node && !cp_unevaluated_operand)
12867 {
12868 if (!defarg_inst)
12869 defarg_inst = tree_cache_map::create_ggc (37);
12870 defarg_inst->put (parm, arg);
12871 }
12872
12873 return arg;
12874 }
12875
12876 /* Substitute into all the default arguments for FN. */
12877
12878 static void
12879 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
12880 {
12881 tree arg;
12882 tree tmpl_args;
12883
12884 tmpl_args = DECL_TI_ARGS (fn);
12885
12886 /* If this function is not yet instantiated, we certainly don't need
12887 its default arguments. */
12888 if (uses_template_parms (tmpl_args))
12889 return;
12890 /* Don't do this again for clones. */
12891 if (DECL_CLONED_FUNCTION_P (fn))
12892 return;
12893
12894 int i = 0;
12895 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
12896 arg;
12897 arg = TREE_CHAIN (arg), ++i)
12898 if (TREE_PURPOSE (arg))
12899 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
12900 TREE_VALUE (arg),
12901 TREE_PURPOSE (arg),
12902 complain);
12903 }
12904
12905 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
12906 static GTY((cache)) tree_cache_map *explicit_specifier_map;
12907
12908 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
12909
12910 void
12911 store_explicit_specifier (tree v, tree t)
12912 {
12913 if (!explicit_specifier_map)
12914 explicit_specifier_map = tree_cache_map::create_ggc (37);
12915 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v) = true;
12916 explicit_specifier_map->put (v, t);
12917 }
12918
12919 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
12920
12921 static tree
12922 lookup_explicit_specifier (tree v)
12923 {
12924 return *explicit_specifier_map->get (v);
12925 }
12926
12927 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12928
12929 static tree
12930 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
12931 tree lambda_fntype)
12932 {
12933 tree gen_tmpl, argvec;
12934 hashval_t hash = 0;
12935 tree in_decl = t;
12936
12937 /* Nobody should be tsubst'ing into non-template functions. */
12938 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
12939
12940 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
12941 {
12942 /* If T is not dependent, just return it. */
12943 if (!uses_template_parms (DECL_TI_ARGS (t))
12944 && !LAMBDA_FUNCTION_P (t))
12945 return t;
12946
12947 /* Calculate the most general template of which R is a
12948 specialization. */
12949 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
12950
12951 /* We're substituting a lambda function under tsubst_lambda_expr but not
12952 directly from it; find the matching function we're already inside.
12953 But don't do this if T is a generic lambda with a single level of
12954 template parms, as in that case we're doing a normal instantiation. */
12955 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
12956 && (!generic_lambda_fn_p (t)
12957 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
12958 return enclosing_instantiation_of (t);
12959
12960 /* Calculate the complete set of arguments used to
12961 specialize R. */
12962 argvec = tsubst_template_args (DECL_TI_ARGS
12963 (DECL_TEMPLATE_RESULT
12964 (DECL_TI_TEMPLATE (t))),
12965 args, complain, in_decl);
12966 if (argvec == error_mark_node)
12967 return error_mark_node;
12968
12969 /* Check to see if we already have this specialization. */
12970 if (!lambda_fntype)
12971 {
12972 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12973 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
12974 return spec;
12975 }
12976
12977 /* We can see more levels of arguments than parameters if
12978 there was a specialization of a member template, like
12979 this:
12980
12981 template <class T> struct S { template <class U> void f(); }
12982 template <> template <class U> void S<int>::f(U);
12983
12984 Here, we'll be substituting into the specialization,
12985 because that's where we can find the code we actually
12986 want to generate, but we'll have enough arguments for
12987 the most general template.
12988
12989 We also deal with the peculiar case:
12990
12991 template <class T> struct S {
12992 template <class U> friend void f();
12993 };
12994 template <class U> void f() {}
12995 template S<int>;
12996 template void f<double>();
12997
12998 Here, the ARGS for the instantiation of will be {int,
12999 double}. But, we only need as many ARGS as there are
13000 levels of template parameters in CODE_PATTERN. We are
13001 careful not to get fooled into reducing the ARGS in
13002 situations like:
13003
13004 template <class T> struct S { template <class U> void f(U); }
13005 template <class T> template <> void S<T>::f(int) {}
13006
13007 which we can spot because the pattern will be a
13008 specialization in this case. */
13009 int args_depth = TMPL_ARGS_DEPTH (args);
13010 int parms_depth =
13011 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
13012
13013 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
13014 args = get_innermost_template_args (args, parms_depth);
13015 }
13016 else
13017 {
13018 /* This special case arises when we have something like this:
13019
13020 template <class T> struct S {
13021 friend void f<int>(int, double);
13022 };
13023
13024 Here, the DECL_TI_TEMPLATE for the friend declaration
13025 will be an IDENTIFIER_NODE. We are being called from
13026 tsubst_friend_function, and we want only to create a
13027 new decl (R) with appropriate types so that we can call
13028 determine_specialization. */
13029 gen_tmpl = NULL_TREE;
13030 argvec = NULL_TREE;
13031 }
13032
13033 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
13034 : NULL_TREE);
13035 tree ctx = closure ? closure : DECL_CONTEXT (t);
13036 bool member = ctx && TYPE_P (ctx);
13037
13038 if (member && !closure)
13039 ctx = tsubst_aggr_type (ctx, args,
13040 complain, t, /*entering_scope=*/1);
13041
13042 tree type = (lambda_fntype ? lambda_fntype
13043 : tsubst (TREE_TYPE (t), args,
13044 complain | tf_fndecl_type, in_decl));
13045 if (type == error_mark_node)
13046 return error_mark_node;
13047
13048 /* If we hit excessive deduction depth, the type is bogus even if
13049 it isn't error_mark_node, so don't build a decl. */
13050 if (excessive_deduction_depth)
13051 return error_mark_node;
13052
13053 /* We do NOT check for matching decls pushed separately at this
13054 point, as they may not represent instantiations of this
13055 template, and in any case are considered separate under the
13056 discrete model. */
13057 tree r = copy_decl (t);
13058 DECL_USE_TEMPLATE (r) = 0;
13059 TREE_TYPE (r) = type;
13060 /* Clear out the mangled name and RTL for the instantiation. */
13061 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13062 SET_DECL_RTL (r, NULL);
13063 /* Leave DECL_INITIAL set on deleted instantiations. */
13064 if (!DECL_DELETED_FN (r))
13065 DECL_INITIAL (r) = NULL_TREE;
13066 DECL_CONTEXT (r) = ctx;
13067
13068 /* Handle explicit(dependent-expr). */
13069 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
13070 {
13071 tree spec = lookup_explicit_specifier (t);
13072 spec = tsubst_copy_and_build (spec, args, complain, in_decl,
13073 /*function_p=*/false,
13074 /*i_c_e_p=*/true);
13075 spec = build_explicit_specifier (spec, complain);
13076 DECL_NONCONVERTING_P (r) = (spec == boolean_true_node);
13077 }
13078
13079 /* OpenMP UDRs have the only argument a reference to the declared
13080 type. We want to diagnose if the declared type is a reference,
13081 which is invalid, but as references to references are usually
13082 quietly merged, diagnose it here. */
13083 if (DECL_OMP_DECLARE_REDUCTION_P (t))
13084 {
13085 tree argtype
13086 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
13087 argtype = tsubst (argtype, args, complain, in_decl);
13088 if (TYPE_REF_P (argtype))
13089 error_at (DECL_SOURCE_LOCATION (t),
13090 "reference type %qT in "
13091 "%<#pragma omp declare reduction%>", argtype);
13092 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
13093 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
13094 argtype);
13095 }
13096
13097 if (member && DECL_CONV_FN_P (r))
13098 /* Type-conversion operator. Reconstruct the name, in
13099 case it's the name of one of the template's parameters. */
13100 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
13101
13102 tree parms = DECL_ARGUMENTS (t);
13103 if (closure)
13104 parms = DECL_CHAIN (parms);
13105 parms = tsubst (parms, args, complain, t);
13106 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
13107 DECL_CONTEXT (parm) = r;
13108 if (closure)
13109 {
13110 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
13111 DECL_CHAIN (tparm) = parms;
13112 parms = tparm;
13113 }
13114 DECL_ARGUMENTS (r) = parms;
13115 DECL_RESULT (r) = NULL_TREE;
13116
13117 TREE_STATIC (r) = 0;
13118 TREE_PUBLIC (r) = TREE_PUBLIC (t);
13119 DECL_EXTERNAL (r) = 1;
13120 /* If this is an instantiation of a function with internal
13121 linkage, we already know what object file linkage will be
13122 assigned to the instantiation. */
13123 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
13124 DECL_DEFER_OUTPUT (r) = 0;
13125 DECL_CHAIN (r) = NULL_TREE;
13126 DECL_PENDING_INLINE_INFO (r) = 0;
13127 DECL_PENDING_INLINE_P (r) = 0;
13128 DECL_SAVED_TREE (r) = NULL_TREE;
13129 DECL_STRUCT_FUNCTION (r) = NULL;
13130 TREE_USED (r) = 0;
13131 /* We'll re-clone as appropriate in instantiate_template. */
13132 DECL_CLONED_FUNCTION (r) = NULL_TREE;
13133
13134 /* If we aren't complaining now, return on error before we register
13135 the specialization so that we'll complain eventually. */
13136 if ((complain & tf_error) == 0
13137 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13138 && !grok_op_properties (r, /*complain=*/false))
13139 return error_mark_node;
13140
13141 /* When instantiating a constrained member, substitute
13142 into the constraints to create a new constraint. */
13143 if (tree ci = get_constraints (t))
13144 if (member)
13145 {
13146 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
13147 set_constraints (r, ci);
13148 }
13149
13150 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
13151 SET_DECL_FRIEND_CONTEXT (r,
13152 tsubst (DECL_FRIEND_CONTEXT (t),
13153 args, complain, in_decl));
13154
13155 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
13156 this in the special friend case mentioned above where
13157 GEN_TMPL is NULL. */
13158 if (gen_tmpl && !closure)
13159 {
13160 DECL_TEMPLATE_INFO (r)
13161 = build_template_info (gen_tmpl, argvec);
13162 SET_DECL_IMPLICIT_INSTANTIATION (r);
13163
13164 tree new_r
13165 = register_specialization (r, gen_tmpl, argvec, false, hash);
13166 if (new_r != r)
13167 /* We instantiated this while substituting into
13168 the type earlier (template/friend54.C). */
13169 return new_r;
13170
13171 /* We're not supposed to instantiate default arguments
13172 until they are called, for a template. But, for a
13173 declaration like:
13174
13175 template <class T> void f ()
13176 { extern void g(int i = T()); }
13177
13178 we should do the substitution when the template is
13179 instantiated. We handle the member function case in
13180 instantiate_class_template since the default arguments
13181 might refer to other members of the class. */
13182 if (!member
13183 && !PRIMARY_TEMPLATE_P (gen_tmpl)
13184 && !uses_template_parms (argvec))
13185 tsubst_default_arguments (r, complain);
13186 }
13187 else
13188 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13189
13190 /* Copy the list of befriending classes. */
13191 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
13192 *friends;
13193 friends = &TREE_CHAIN (*friends))
13194 {
13195 *friends = copy_node (*friends);
13196 TREE_VALUE (*friends)
13197 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
13198 }
13199
13200 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
13201 {
13202 maybe_retrofit_in_chrg (r);
13203 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
13204 return error_mark_node;
13205 /* If this is an instantiation of a member template, clone it.
13206 If it isn't, that'll be handled by
13207 clone_constructors_and_destructors. */
13208 if (PRIMARY_TEMPLATE_P (gen_tmpl))
13209 clone_function_decl (r, /*update_methods=*/false);
13210 }
13211 else if ((complain & tf_error) != 0
13212 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
13213 && !grok_op_properties (r, /*complain=*/true))
13214 return error_mark_node;
13215
13216 /* Possibly limit visibility based on template args. */
13217 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13218 if (DECL_VISIBILITY_SPECIFIED (t))
13219 {
13220 DECL_VISIBILITY_SPECIFIED (r) = 0;
13221 DECL_ATTRIBUTES (r)
13222 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13223 }
13224 determine_visibility (r);
13225 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
13226 && !processing_template_decl)
13227 defaulted_late_check (r);
13228
13229 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13230 args, complain, in_decl);
13231 return r;
13232 }
13233
13234 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
13235
13236 static tree
13237 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
13238 tree lambda_fntype)
13239 {
13240 /* We can get here when processing a member function template,
13241 member class template, or template template parameter. */
13242 tree decl = DECL_TEMPLATE_RESULT (t);
13243 tree in_decl = t;
13244 tree spec;
13245 tree tmpl_args;
13246 tree full_args;
13247 tree r;
13248 hashval_t hash = 0;
13249
13250 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
13251 {
13252 /* Template template parameter is treated here. */
13253 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13254 if (new_type == error_mark_node)
13255 r = error_mark_node;
13256 /* If we get a real template back, return it. This can happen in
13257 the context of most_specialized_partial_spec. */
13258 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
13259 r = new_type;
13260 else
13261 /* The new TEMPLATE_DECL was built in
13262 reduce_template_parm_level. */
13263 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
13264 return r;
13265 }
13266
13267 if (!lambda_fntype)
13268 {
13269 /* We might already have an instance of this template.
13270 The ARGS are for the surrounding class type, so the
13271 full args contain the tsubst'd args for the context,
13272 plus the innermost args from the template decl. */
13273 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
13274 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
13275 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
13276 /* Because this is a template, the arguments will still be
13277 dependent, even after substitution. If
13278 PROCESSING_TEMPLATE_DECL is not set, the dependency
13279 predicates will short-circuit. */
13280 ++processing_template_decl;
13281 full_args = tsubst_template_args (tmpl_args, args,
13282 complain, in_decl);
13283 --processing_template_decl;
13284 if (full_args == error_mark_node)
13285 return error_mark_node;
13286
13287 /* If this is a default template template argument,
13288 tsubst might not have changed anything. */
13289 if (full_args == tmpl_args)
13290 return t;
13291
13292 hash = hash_tmpl_and_args (t, full_args);
13293 spec = retrieve_specialization (t, full_args, hash);
13294 if (spec != NULL_TREE)
13295 {
13296 if (TYPE_P (spec))
13297 /* Type partial instantiations are stored as the type by
13298 lookup_template_class_1, not here as the template. */
13299 spec = CLASSTYPE_TI_TEMPLATE (spec);
13300 return spec;
13301 }
13302 }
13303
13304 /* Make a new template decl. It will be similar to the
13305 original, but will record the current template arguments.
13306 We also create a new function declaration, which is just
13307 like the old one, but points to this new template, rather
13308 than the old one. */
13309 r = copy_decl (t);
13310 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
13311 DECL_CHAIN (r) = NULL_TREE;
13312
13313 // Build new template info linking to the original template decl.
13314 if (!lambda_fntype)
13315 {
13316 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
13317 SET_DECL_IMPLICIT_INSTANTIATION (r);
13318 }
13319 else
13320 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13321
13322 /* The template parameters for this new template are all the
13323 template parameters for the old template, except the
13324 outermost level of parameters. */
13325 DECL_TEMPLATE_PARMS (r)
13326 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
13327 complain);
13328
13329 if (TREE_CODE (decl) == TYPE_DECL
13330 && !TYPE_DECL_ALIAS_P (decl))
13331 {
13332 tree new_type;
13333 ++processing_template_decl;
13334 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13335 --processing_template_decl;
13336 if (new_type == error_mark_node)
13337 return error_mark_node;
13338
13339 TREE_TYPE (r) = new_type;
13340 /* For a partial specialization, we need to keep pointing to
13341 the primary template. */
13342 if (!DECL_TEMPLATE_SPECIALIZATION (t))
13343 CLASSTYPE_TI_TEMPLATE (new_type) = r;
13344 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
13345 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
13346 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
13347 }
13348 else
13349 {
13350 tree new_decl;
13351 ++processing_template_decl;
13352 if (TREE_CODE (decl) == FUNCTION_DECL)
13353 new_decl = tsubst_function_decl (decl, args, complain, lambda_fntype);
13354 else
13355 new_decl = tsubst (decl, args, complain, in_decl);
13356 --processing_template_decl;
13357 if (new_decl == error_mark_node)
13358 return error_mark_node;
13359
13360 DECL_TEMPLATE_RESULT (r) = new_decl;
13361 TREE_TYPE (r) = TREE_TYPE (new_decl);
13362 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
13363 if (lambda_fntype)
13364 {
13365 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
13366 DECL_TEMPLATE_INFO (new_decl) = build_template_info (r, args);
13367 }
13368 else
13369 {
13370 DECL_TI_TEMPLATE (new_decl) = r;
13371 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
13372 }
13373 }
13374
13375 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
13376 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
13377
13378 if (PRIMARY_TEMPLATE_P (t))
13379 DECL_PRIMARY_TEMPLATE (r) = r;
13380
13381 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
13382 && !lambda_fntype)
13383 /* Record this non-type partial instantiation. */
13384 register_specialization (r, t,
13385 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
13386 false, hash);
13387
13388 return r;
13389 }
13390
13391 /* True if FN is the op() for a lambda in an uninstantiated template. */
13392
13393 bool
13394 lambda_fn_in_template_p (tree fn)
13395 {
13396 if (!fn || !LAMBDA_FUNCTION_P (fn))
13397 return false;
13398 tree closure = DECL_CONTEXT (fn);
13399 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
13400 }
13401
13402 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
13403 which the above is true. */
13404
13405 bool
13406 instantiated_lambda_fn_p (tree fn)
13407 {
13408 if (!fn || !LAMBDA_FUNCTION_P (fn))
13409 return false;
13410 tree closure = DECL_CONTEXT (fn);
13411 tree lam = CLASSTYPE_LAMBDA_EXPR (closure);
13412 return LAMBDA_EXPR_INSTANTIATED (lam);
13413 }
13414
13415 /* We're instantiating a variable from template function TCTX. Return the
13416 corresponding current enclosing scope. This gets complicated because lambda
13417 functions in templates are regenerated rather than instantiated, but generic
13418 lambda functions are subsequently instantiated. */
13419
13420 static tree
13421 enclosing_instantiation_of (tree otctx)
13422 {
13423 tree tctx = otctx;
13424 tree fn = current_function_decl;
13425 int lambda_count = 0;
13426
13427 for (; tctx && (lambda_fn_in_template_p (tctx)
13428 || instantiated_lambda_fn_p (tctx));
13429 tctx = decl_function_context (tctx))
13430 ++lambda_count;
13431 for (; fn; fn = decl_function_context (fn))
13432 {
13433 tree ofn = fn;
13434 int flambda_count = 0;
13435 for (; fn && instantiated_lambda_fn_p (fn);
13436 fn = decl_function_context (fn))
13437 ++flambda_count;
13438 if ((fn && DECL_TEMPLATE_INFO (fn))
13439 ? most_general_template (fn) != most_general_template (tctx)
13440 : fn != tctx)
13441 continue;
13442 if (flambda_count != lambda_count)
13443 {
13444 gcc_assert (flambda_count > lambda_count);
13445 for (; flambda_count > lambda_count; --flambda_count)
13446 ofn = decl_function_context (ofn);
13447 }
13448 gcc_assert (DECL_NAME (ofn) == DECL_NAME (otctx)
13449 || DECL_CONV_FN_P (ofn));
13450 return ofn;
13451 }
13452 gcc_unreachable ();
13453 }
13454
13455 /* Substitute the ARGS into the T, which is a _DECL. Return the
13456 result of the substitution. Issue error and warning messages under
13457 control of COMPLAIN. */
13458
13459 static tree
13460 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
13461 {
13462 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
13463 location_t saved_loc;
13464 tree r = NULL_TREE;
13465 tree in_decl = t;
13466 hashval_t hash = 0;
13467
13468 /* Set the filename and linenumber to improve error-reporting. */
13469 saved_loc = input_location;
13470 input_location = DECL_SOURCE_LOCATION (t);
13471
13472 switch (TREE_CODE (t))
13473 {
13474 case TEMPLATE_DECL:
13475 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
13476 break;
13477
13478 case FUNCTION_DECL:
13479 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
13480 break;
13481
13482 case PARM_DECL:
13483 {
13484 tree type = NULL_TREE;
13485 int i, len = 1;
13486 tree expanded_types = NULL_TREE;
13487 tree prev_r = NULL_TREE;
13488 tree first_r = NULL_TREE;
13489
13490 if (DECL_PACK_P (t))
13491 {
13492 /* If there is a local specialization that isn't a
13493 parameter pack, it means that we're doing a "simple"
13494 substitution from inside tsubst_pack_expansion. Just
13495 return the local specialization (which will be a single
13496 parm). */
13497 tree spec = retrieve_local_specialization (t);
13498 if (spec
13499 && TREE_CODE (spec) == PARM_DECL
13500 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
13501 RETURN (spec);
13502
13503 /* Expand the TYPE_PACK_EXPANSION that provides the types for
13504 the parameters in this function parameter pack. */
13505 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
13506 complain, in_decl);
13507 if (TREE_CODE (expanded_types) == TREE_VEC)
13508 {
13509 len = TREE_VEC_LENGTH (expanded_types);
13510
13511 /* Zero-length parameter packs are boring. Just substitute
13512 into the chain. */
13513 if (len == 0)
13514 RETURN (tsubst (TREE_CHAIN (t), args, complain,
13515 TREE_CHAIN (t)));
13516 }
13517 else
13518 {
13519 /* All we did was update the type. Make a note of that. */
13520 type = expanded_types;
13521 expanded_types = NULL_TREE;
13522 }
13523 }
13524
13525 /* Loop through all of the parameters we'll build. When T is
13526 a function parameter pack, LEN is the number of expanded
13527 types in EXPANDED_TYPES; otherwise, LEN is 1. */
13528 r = NULL_TREE;
13529 for (i = 0; i < len; ++i)
13530 {
13531 prev_r = r;
13532 r = copy_node (t);
13533 if (DECL_TEMPLATE_PARM_P (t))
13534 SET_DECL_TEMPLATE_PARM_P (r);
13535
13536 if (expanded_types)
13537 /* We're on the Ith parameter of the function parameter
13538 pack. */
13539 {
13540 /* Get the Ith type. */
13541 type = TREE_VEC_ELT (expanded_types, i);
13542
13543 /* Rename the parameter to include the index. */
13544 DECL_NAME (r)
13545 = make_ith_pack_parameter_name (DECL_NAME (r), i);
13546 }
13547 else if (!type)
13548 /* We're dealing with a normal parameter. */
13549 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13550
13551 type = type_decays_to (type);
13552 TREE_TYPE (r) = type;
13553 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13554
13555 if (DECL_INITIAL (r))
13556 {
13557 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
13558 DECL_INITIAL (r) = TREE_TYPE (r);
13559 else
13560 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
13561 complain, in_decl);
13562 }
13563
13564 DECL_CONTEXT (r) = NULL_TREE;
13565
13566 if (!DECL_TEMPLATE_PARM_P (r))
13567 DECL_ARG_TYPE (r) = type_passed_as (type);
13568
13569 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13570 args, complain, in_decl);
13571
13572 /* Keep track of the first new parameter we
13573 generate. That's what will be returned to the
13574 caller. */
13575 if (!first_r)
13576 first_r = r;
13577
13578 /* Build a proper chain of parameters when substituting
13579 into a function parameter pack. */
13580 if (prev_r)
13581 DECL_CHAIN (prev_r) = r;
13582 }
13583
13584 /* If cp_unevaluated_operand is set, we're just looking for a
13585 single dummy parameter, so don't keep going. */
13586 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
13587 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
13588 complain, DECL_CHAIN (t));
13589
13590 /* FIRST_R contains the start of the chain we've built. */
13591 r = first_r;
13592 }
13593 break;
13594
13595 case FIELD_DECL:
13596 {
13597 tree type = NULL_TREE;
13598 tree vec = NULL_TREE;
13599 tree expanded_types = NULL_TREE;
13600 int len = 1;
13601
13602 if (PACK_EXPANSION_P (TREE_TYPE (t)))
13603 {
13604 /* This field is a lambda capture pack. Return a TREE_VEC of
13605 the expanded fields to instantiate_class_template_1. */
13606 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
13607 complain, in_decl);
13608 if (TREE_CODE (expanded_types) == TREE_VEC)
13609 {
13610 len = TREE_VEC_LENGTH (expanded_types);
13611 vec = make_tree_vec (len);
13612 }
13613 else
13614 {
13615 /* All we did was update the type. Make a note of that. */
13616 type = expanded_types;
13617 expanded_types = NULL_TREE;
13618 }
13619 }
13620
13621 for (int i = 0; i < len; ++i)
13622 {
13623 r = copy_decl (t);
13624 if (expanded_types)
13625 {
13626 type = TREE_VEC_ELT (expanded_types, i);
13627 DECL_NAME (r)
13628 = make_ith_pack_parameter_name (DECL_NAME (r), i);
13629 }
13630 else if (!type)
13631 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13632
13633 if (type == error_mark_node)
13634 RETURN (error_mark_node);
13635 TREE_TYPE (r) = type;
13636 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13637
13638 if (DECL_C_BIT_FIELD (r))
13639 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
13640 number of bits. */
13641 DECL_BIT_FIELD_REPRESENTATIVE (r)
13642 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
13643 complain, in_decl,
13644 /*integral_constant_expression_p=*/true);
13645 if (DECL_INITIAL (t))
13646 {
13647 /* Set up DECL_TEMPLATE_INFO so that we can get at the
13648 NSDMI in perform_member_init. Still set DECL_INITIAL
13649 so that we know there is one. */
13650 DECL_INITIAL (r) = void_node;
13651 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
13652 retrofit_lang_decl (r);
13653 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
13654 }
13655 /* We don't have to set DECL_CONTEXT here; it is set by
13656 finish_member_declaration. */
13657 DECL_CHAIN (r) = NULL_TREE;
13658
13659 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13660 args, complain, in_decl);
13661
13662 if (vec)
13663 TREE_VEC_ELT (vec, i) = r;
13664 }
13665
13666 if (vec)
13667 r = vec;
13668 }
13669 break;
13670
13671 case USING_DECL:
13672 /* We reach here only for member using decls. We also need to check
13673 uses_template_parms because DECL_DEPENDENT_P is not set for a
13674 using-declaration that designates a member of the current
13675 instantiation (c++/53549). */
13676 if (DECL_DEPENDENT_P (t)
13677 || uses_template_parms (USING_DECL_SCOPE (t)))
13678 {
13679 tree scope = USING_DECL_SCOPE (t);
13680 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
13681 if (PACK_EXPANSION_P (scope))
13682 {
13683 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
13684 int len = TREE_VEC_LENGTH (vec);
13685 r = make_tree_vec (len);
13686 for (int i = 0; i < len; ++i)
13687 {
13688 tree escope = TREE_VEC_ELT (vec, i);
13689 tree elt = do_class_using_decl (escope, name);
13690 if (!elt)
13691 {
13692 r = error_mark_node;
13693 break;
13694 }
13695 else
13696 {
13697 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
13698 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
13699 }
13700 TREE_VEC_ELT (r, i) = elt;
13701 }
13702 }
13703 else
13704 {
13705 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
13706 complain, in_decl);
13707 r = do_class_using_decl (inst_scope, name);
13708 if (!r)
13709 r = error_mark_node;
13710 else
13711 {
13712 TREE_PROTECTED (r) = TREE_PROTECTED (t);
13713 TREE_PRIVATE (r) = TREE_PRIVATE (t);
13714 }
13715 }
13716 }
13717 else
13718 {
13719 r = copy_node (t);
13720 DECL_CHAIN (r) = NULL_TREE;
13721 }
13722 break;
13723
13724 case TYPE_DECL:
13725 case VAR_DECL:
13726 {
13727 tree argvec = NULL_TREE;
13728 tree gen_tmpl = NULL_TREE;
13729 tree spec;
13730 tree tmpl = NULL_TREE;
13731 tree ctx;
13732 tree type = NULL_TREE;
13733 bool local_p;
13734
13735 if (TREE_TYPE (t) == error_mark_node)
13736 RETURN (error_mark_node);
13737
13738 if (TREE_CODE (t) == TYPE_DECL
13739 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
13740 {
13741 /* If this is the canonical decl, we don't have to
13742 mess with instantiations, and often we can't (for
13743 typename, template type parms and such). Note that
13744 TYPE_NAME is not correct for the above test if
13745 we've copied the type for a typedef. */
13746 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13747 if (type == error_mark_node)
13748 RETURN (error_mark_node);
13749 r = TYPE_NAME (type);
13750 break;
13751 }
13752
13753 /* Check to see if we already have the specialization we
13754 need. */
13755 spec = NULL_TREE;
13756 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
13757 {
13758 /* T is a static data member or namespace-scope entity.
13759 We have to substitute into namespace-scope variables
13760 (not just variable templates) because of cases like:
13761
13762 template <class T> void f() { extern T t; }
13763
13764 where the entity referenced is not known until
13765 instantiation time. */
13766 local_p = false;
13767 ctx = DECL_CONTEXT (t);
13768 if (DECL_CLASS_SCOPE_P (t))
13769 {
13770 ctx = tsubst_aggr_type (ctx, args,
13771 complain,
13772 in_decl, /*entering_scope=*/1);
13773 /* If CTX is unchanged, then T is in fact the
13774 specialization we want. That situation occurs when
13775 referencing a static data member within in its own
13776 class. We can use pointer equality, rather than
13777 same_type_p, because DECL_CONTEXT is always
13778 canonical... */
13779 if (ctx == DECL_CONTEXT (t)
13780 /* ... unless T is a member template; in which
13781 case our caller can be willing to create a
13782 specialization of that template represented
13783 by T. */
13784 && !(DECL_TI_TEMPLATE (t)
13785 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
13786 spec = t;
13787 }
13788
13789 if (!spec)
13790 {
13791 tmpl = DECL_TI_TEMPLATE (t);
13792 gen_tmpl = most_general_template (tmpl);
13793 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
13794 if (argvec != error_mark_node)
13795 argvec = (coerce_innermost_template_parms
13796 (DECL_TEMPLATE_PARMS (gen_tmpl),
13797 argvec, t, complain,
13798 /*all*/true, /*defarg*/true));
13799 if (argvec == error_mark_node)
13800 RETURN (error_mark_node);
13801 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13802 spec = retrieve_specialization (gen_tmpl, argvec, hash);
13803 }
13804 }
13805 else
13806 {
13807 /* A local variable. */
13808 local_p = true;
13809 /* Subsequent calls to pushdecl will fill this in. */
13810 ctx = NULL_TREE;
13811 /* Unless this is a reference to a static variable from an
13812 enclosing function, in which case we need to fill it in now. */
13813 if (TREE_STATIC (t))
13814 {
13815 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
13816 if (fn != current_function_decl)
13817 ctx = fn;
13818 }
13819 spec = retrieve_local_specialization (t);
13820 }
13821 /* If we already have the specialization we need, there is
13822 nothing more to do. */
13823 if (spec)
13824 {
13825 r = spec;
13826 break;
13827 }
13828
13829 /* Create a new node for the specialization we need. */
13830 if (type == NULL_TREE)
13831 {
13832 if (is_typedef_decl (t))
13833 type = DECL_ORIGINAL_TYPE (t);
13834 else
13835 type = TREE_TYPE (t);
13836 if (VAR_P (t)
13837 && VAR_HAD_UNKNOWN_BOUND (t)
13838 && type != error_mark_node)
13839 type = strip_array_domain (type);
13840 tree sub_args = args;
13841 if (tree auto_node = type_uses_auto (type))
13842 {
13843 /* Mask off any template args past the variable's context so we
13844 don't replace the auto with an unrelated argument. */
13845 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
13846 int extra = TMPL_ARGS_DEPTH (args) - nouter;
13847 if (extra > 0)
13848 /* This should never happen with the new lambda instantiation
13849 model, but keep the handling just in case. */
13850 gcc_assert (!CHECKING_P),
13851 sub_args = strip_innermost_template_args (args, extra);
13852 }
13853 type = tsubst (type, sub_args, complain, in_decl);
13854 /* Substituting the type might have recursively instantiated this
13855 same alias (c++/86171). */
13856 if (gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl)
13857 && (spec = retrieve_specialization (gen_tmpl, argvec, hash)))
13858 {
13859 r = spec;
13860 break;
13861 }
13862 }
13863 r = copy_decl (t);
13864 if (VAR_P (r))
13865 {
13866 DECL_INITIALIZED_P (r) = 0;
13867 DECL_TEMPLATE_INSTANTIATED (r) = 0;
13868 if (type == error_mark_node)
13869 RETURN (error_mark_node);
13870 if (TREE_CODE (type) == FUNCTION_TYPE)
13871 {
13872 /* It may seem that this case cannot occur, since:
13873
13874 typedef void f();
13875 void g() { f x; }
13876
13877 declares a function, not a variable. However:
13878
13879 typedef void f();
13880 template <typename T> void g() { T t; }
13881 template void g<f>();
13882
13883 is an attempt to declare a variable with function
13884 type. */
13885 error ("variable %qD has function type",
13886 /* R is not yet sufficiently initialized, so we
13887 just use its name. */
13888 DECL_NAME (r));
13889 RETURN (error_mark_node);
13890 }
13891 type = complete_type (type);
13892 /* Wait until cp_finish_decl to set this again, to handle
13893 circular dependency (template/instantiate6.C). */
13894 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
13895 type = check_var_type (DECL_NAME (r), type,
13896 DECL_SOURCE_LOCATION (r));
13897 if (DECL_HAS_VALUE_EXPR_P (t))
13898 {
13899 tree ve = DECL_VALUE_EXPR (t);
13900 ve = tsubst_expr (ve, args, complain, in_decl,
13901 /*constant_expression_p=*/false);
13902 if (REFERENCE_REF_P (ve))
13903 {
13904 gcc_assert (TYPE_REF_P (type));
13905 ve = TREE_OPERAND (ve, 0);
13906 }
13907 SET_DECL_VALUE_EXPR (r, ve);
13908 }
13909 if (CP_DECL_THREAD_LOCAL_P (r)
13910 && !processing_template_decl)
13911 set_decl_tls_model (r, decl_default_tls_model (r));
13912 }
13913 else if (DECL_SELF_REFERENCE_P (t))
13914 SET_DECL_SELF_REFERENCE_P (r);
13915 TREE_TYPE (r) = type;
13916 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13917 DECL_CONTEXT (r) = ctx;
13918 /* Clear out the mangled name and RTL for the instantiation. */
13919 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13920 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
13921 SET_DECL_RTL (r, NULL);
13922 /* The initializer must not be expanded until it is required;
13923 see [temp.inst]. */
13924 DECL_INITIAL (r) = NULL_TREE;
13925 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
13926 if (VAR_P (r))
13927 {
13928 if (DECL_LANG_SPECIFIC (r))
13929 SET_DECL_DEPENDENT_INIT_P (r, false);
13930
13931 SET_DECL_MODE (r, VOIDmode);
13932
13933 /* Possibly limit visibility based on template args. */
13934 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13935 if (DECL_VISIBILITY_SPECIFIED (t))
13936 {
13937 DECL_VISIBILITY_SPECIFIED (r) = 0;
13938 DECL_ATTRIBUTES (r)
13939 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13940 }
13941 determine_visibility (r);
13942 }
13943
13944 if (!local_p)
13945 {
13946 /* A static data member declaration is always marked
13947 external when it is declared in-class, even if an
13948 initializer is present. We mimic the non-template
13949 processing here. */
13950 DECL_EXTERNAL (r) = 1;
13951 if (DECL_NAMESPACE_SCOPE_P (t))
13952 DECL_NOT_REALLY_EXTERN (r) = 1;
13953
13954 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
13955 SET_DECL_IMPLICIT_INSTANTIATION (r);
13956 /* Remember whether we require constant initialization of
13957 a non-constant template variable. */
13958 TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (r))
13959 = TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (t));
13960 if (!error_operand_p (r) || (complain & tf_error))
13961 register_specialization (r, gen_tmpl, argvec, false, hash);
13962 }
13963 else
13964 {
13965 if (DECL_LANG_SPECIFIC (r))
13966 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13967 if (!cp_unevaluated_operand)
13968 register_local_specialization (r, t);
13969 }
13970
13971 DECL_CHAIN (r) = NULL_TREE;
13972
13973 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
13974 /*flags=*/0,
13975 args, complain, in_decl);
13976
13977 /* Preserve a typedef that names a type. */
13978 if (is_typedef_decl (r) && type != error_mark_node)
13979 {
13980 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
13981 set_underlying_type (r);
13982 if (TYPE_DECL_ALIAS_P (r))
13983 /* An alias template specialization can be dependent
13984 even if its underlying type is not. */
13985 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
13986 }
13987
13988 layout_decl (r, 0);
13989 }
13990 break;
13991
13992 default:
13993 gcc_unreachable ();
13994 }
13995 #undef RETURN
13996
13997 out:
13998 /* Restore the file and line information. */
13999 input_location = saved_loc;
14000
14001 return r;
14002 }
14003
14004 /* Substitute into the ARG_TYPES of a function type.
14005 If END is a TREE_CHAIN, leave it and any following types
14006 un-substituted. */
14007
14008 static tree
14009 tsubst_arg_types (tree arg_types,
14010 tree args,
14011 tree end,
14012 tsubst_flags_t complain,
14013 tree in_decl)
14014 {
14015 tree remaining_arg_types;
14016 tree type = NULL_TREE;
14017 int i = 1;
14018 tree expanded_args = NULL_TREE;
14019 tree default_arg;
14020
14021 if (!arg_types || arg_types == void_list_node || arg_types == end)
14022 return arg_types;
14023
14024 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
14025 args, end, complain, in_decl);
14026 if (remaining_arg_types == error_mark_node)
14027 return error_mark_node;
14028
14029 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
14030 {
14031 /* For a pack expansion, perform substitution on the
14032 entire expression. Later on, we'll handle the arguments
14033 one-by-one. */
14034 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
14035 args, complain, in_decl);
14036
14037 if (TREE_CODE (expanded_args) == TREE_VEC)
14038 /* So that we'll spin through the parameters, one by one. */
14039 i = TREE_VEC_LENGTH (expanded_args);
14040 else
14041 {
14042 /* We only partially substituted into the parameter
14043 pack. Our type is TYPE_PACK_EXPANSION. */
14044 type = expanded_args;
14045 expanded_args = NULL_TREE;
14046 }
14047 }
14048
14049 while (i > 0) {
14050 --i;
14051
14052 if (expanded_args)
14053 type = TREE_VEC_ELT (expanded_args, i);
14054 else if (!type)
14055 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
14056
14057 if (type == error_mark_node)
14058 return error_mark_node;
14059 if (VOID_TYPE_P (type))
14060 {
14061 if (complain & tf_error)
14062 {
14063 error ("invalid parameter type %qT", type);
14064 if (in_decl)
14065 error ("in declaration %q+D", in_decl);
14066 }
14067 return error_mark_node;
14068 }
14069 /* DR 657. */
14070 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
14071 return error_mark_node;
14072
14073 /* Do array-to-pointer, function-to-pointer conversion, and ignore
14074 top-level qualifiers as required. */
14075 type = cv_unqualified (type_decays_to (type));
14076
14077 /* We do not substitute into default arguments here. The standard
14078 mandates that they be instantiated only when needed, which is
14079 done in build_over_call. */
14080 default_arg = TREE_PURPOSE (arg_types);
14081
14082 /* Except that we do substitute default arguments under tsubst_lambda_expr,
14083 since the new op() won't have any associated template arguments for us
14084 to refer to later. */
14085 if (lambda_fn_in_template_p (in_decl))
14086 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
14087 false/*fn*/, false/*constexpr*/);
14088
14089 if (default_arg && TREE_CODE (default_arg) == DEFERRED_PARSE)
14090 {
14091 /* We've instantiated a template before its default arguments
14092 have been parsed. This can happen for a nested template
14093 class, and is not an error unless we require the default
14094 argument in a call of this function. */
14095 remaining_arg_types =
14096 tree_cons (default_arg, type, remaining_arg_types);
14097 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg),
14098 remaining_arg_types);
14099 }
14100 else
14101 remaining_arg_types =
14102 hash_tree_cons (default_arg, type, remaining_arg_types);
14103 }
14104
14105 return remaining_arg_types;
14106 }
14107
14108 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
14109 *not* handle the exception-specification for FNTYPE, because the
14110 initial substitution of explicitly provided template parameters
14111 during argument deduction forbids substitution into the
14112 exception-specification:
14113
14114 [temp.deduct]
14115
14116 All references in the function type of the function template to the
14117 corresponding template parameters are replaced by the specified tem-
14118 plate argument values. If a substitution in a template parameter or
14119 in the function type of the function template results in an invalid
14120 type, type deduction fails. [Note: The equivalent substitution in
14121 exception specifications is done only when the function is instanti-
14122 ated, at which point a program is ill-formed if the substitution
14123 results in an invalid type.] */
14124
14125 static tree
14126 tsubst_function_type (tree t,
14127 tree args,
14128 tsubst_flags_t complain,
14129 tree in_decl)
14130 {
14131 tree return_type;
14132 tree arg_types = NULL_TREE;
14133 tree fntype;
14134
14135 /* The TYPE_CONTEXT is not used for function/method types. */
14136 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
14137
14138 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
14139 failure. */
14140 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
14141
14142 if (late_return_type_p)
14143 {
14144 /* Substitute the argument types. */
14145 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
14146 complain, in_decl);
14147 if (arg_types == error_mark_node)
14148 return error_mark_node;
14149
14150 tree save_ccp = current_class_ptr;
14151 tree save_ccr = current_class_ref;
14152 tree this_type = (TREE_CODE (t) == METHOD_TYPE
14153 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
14154 bool do_inject = this_type && CLASS_TYPE_P (this_type);
14155 if (do_inject)
14156 {
14157 /* DR 1207: 'this' is in scope in the trailing return type. */
14158 inject_this_parameter (this_type, cp_type_quals (this_type));
14159 }
14160
14161 /* Substitute the return type. */
14162 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14163
14164 if (do_inject)
14165 {
14166 current_class_ptr = save_ccp;
14167 current_class_ref = save_ccr;
14168 }
14169 }
14170 else
14171 /* Substitute the return type. */
14172 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14173
14174 if (return_type == error_mark_node)
14175 return error_mark_node;
14176 /* DR 486 clarifies that creation of a function type with an
14177 invalid return type is a deduction failure. */
14178 if (TREE_CODE (return_type) == ARRAY_TYPE
14179 || TREE_CODE (return_type) == FUNCTION_TYPE)
14180 {
14181 if (complain & tf_error)
14182 {
14183 if (TREE_CODE (return_type) == ARRAY_TYPE)
14184 error ("function returning an array");
14185 else
14186 error ("function returning a function");
14187 }
14188 return error_mark_node;
14189 }
14190 /* And DR 657. */
14191 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
14192 return error_mark_node;
14193
14194 if (!late_return_type_p)
14195 {
14196 /* Substitute the argument types. */
14197 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
14198 complain, in_decl);
14199 if (arg_types == error_mark_node)
14200 return error_mark_node;
14201 }
14202
14203 /* Construct a new type node and return it. */
14204 if (TREE_CODE (t) == FUNCTION_TYPE)
14205 {
14206 fntype = build_function_type (return_type, arg_types);
14207 fntype = apply_memfn_quals (fntype, type_memfn_quals (t));
14208 }
14209 else
14210 {
14211 tree r = TREE_TYPE (TREE_VALUE (arg_types));
14212 /* Don't pick up extra function qualifiers from the basetype. */
14213 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
14214 if (! MAYBE_CLASS_TYPE_P (r))
14215 {
14216 /* [temp.deduct]
14217
14218 Type deduction may fail for any of the following
14219 reasons:
14220
14221 -- Attempting to create "pointer to member of T" when T
14222 is not a class type. */
14223 if (complain & tf_error)
14224 error ("creating pointer to member function of non-class type %qT",
14225 r);
14226 return error_mark_node;
14227 }
14228
14229 fntype = build_method_type_directly (r, return_type,
14230 TREE_CHAIN (arg_types));
14231 }
14232 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
14233
14234 /* See comment above. */
14235 tree raises = NULL_TREE;
14236 cp_ref_qualifier rqual = type_memfn_rqual (t);
14237 fntype = build_cp_fntype_variant (fntype, rqual, raises, late_return_type_p);
14238
14239 return fntype;
14240 }
14241
14242 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
14243 ARGS into that specification, and return the substituted
14244 specification. If there is no specification, return NULL_TREE. */
14245
14246 static tree
14247 tsubst_exception_specification (tree fntype,
14248 tree args,
14249 tsubst_flags_t complain,
14250 tree in_decl,
14251 bool defer_ok)
14252 {
14253 tree specs;
14254 tree new_specs;
14255
14256 specs = TYPE_RAISES_EXCEPTIONS (fntype);
14257 new_specs = NULL_TREE;
14258 if (specs && TREE_PURPOSE (specs))
14259 {
14260 /* A noexcept-specifier. */
14261 tree expr = TREE_PURPOSE (specs);
14262 if (TREE_CODE (expr) == INTEGER_CST)
14263 new_specs = expr;
14264 else if (defer_ok)
14265 {
14266 /* Defer instantiation of noexcept-specifiers to avoid
14267 excessive instantiations (c++/49107). */
14268 new_specs = make_node (DEFERRED_NOEXCEPT);
14269 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
14270 {
14271 /* We already partially instantiated this member template,
14272 so combine the new args with the old. */
14273 DEFERRED_NOEXCEPT_PATTERN (new_specs)
14274 = DEFERRED_NOEXCEPT_PATTERN (expr);
14275 DEFERRED_NOEXCEPT_ARGS (new_specs)
14276 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
14277 }
14278 else
14279 {
14280 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
14281 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
14282 }
14283 }
14284 else
14285 {
14286 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
14287 {
14288 args = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr),
14289 args);
14290 expr = DEFERRED_NOEXCEPT_PATTERN (expr);
14291 }
14292 new_specs = tsubst_copy_and_build
14293 (expr, args, complain, in_decl, /*function_p=*/false,
14294 /*integral_constant_expression_p=*/true);
14295 }
14296 new_specs = build_noexcept_spec (new_specs, complain);
14297 }
14298 else if (specs)
14299 {
14300 if (! TREE_VALUE (specs))
14301 new_specs = specs;
14302 else
14303 while (specs)
14304 {
14305 tree spec;
14306 int i, len = 1;
14307 tree expanded_specs = NULL_TREE;
14308
14309 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
14310 {
14311 /* Expand the pack expansion type. */
14312 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
14313 args, complain,
14314 in_decl);
14315
14316 if (expanded_specs == error_mark_node)
14317 return error_mark_node;
14318 else if (TREE_CODE (expanded_specs) == TREE_VEC)
14319 len = TREE_VEC_LENGTH (expanded_specs);
14320 else
14321 {
14322 /* We're substituting into a member template, so
14323 we got a TYPE_PACK_EXPANSION back. Add that
14324 expansion and move on. */
14325 gcc_assert (TREE_CODE (expanded_specs)
14326 == TYPE_PACK_EXPANSION);
14327 new_specs = add_exception_specifier (new_specs,
14328 expanded_specs,
14329 complain);
14330 specs = TREE_CHAIN (specs);
14331 continue;
14332 }
14333 }
14334
14335 for (i = 0; i < len; ++i)
14336 {
14337 if (expanded_specs)
14338 spec = TREE_VEC_ELT (expanded_specs, i);
14339 else
14340 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
14341 if (spec == error_mark_node)
14342 return spec;
14343 new_specs = add_exception_specifier (new_specs, spec,
14344 complain);
14345 }
14346
14347 specs = TREE_CHAIN (specs);
14348 }
14349 }
14350 return new_specs;
14351 }
14352
14353 /* Take the tree structure T and replace template parameters used
14354 therein with the argument vector ARGS. IN_DECL is an associated
14355 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
14356 Issue error and warning messages under control of COMPLAIN. Note
14357 that we must be relatively non-tolerant of extensions here, in
14358 order to preserve conformance; if we allow substitutions that
14359 should not be allowed, we may allow argument deductions that should
14360 not succeed, and therefore report ambiguous overload situations
14361 where there are none. In theory, we could allow the substitution,
14362 but indicate that it should have failed, and allow our caller to
14363 make sure that the right thing happens, but we don't try to do this
14364 yet.
14365
14366 This function is used for dealing with types, decls and the like;
14367 for expressions, use tsubst_expr or tsubst_copy. */
14368
14369 tree
14370 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14371 {
14372 enum tree_code code;
14373 tree type, r = NULL_TREE;
14374
14375 if (t == NULL_TREE || t == error_mark_node
14376 || t == integer_type_node
14377 || t == void_type_node
14378 || t == char_type_node
14379 || t == unknown_type_node
14380 || TREE_CODE (t) == NAMESPACE_DECL
14381 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
14382 return t;
14383
14384 if (DECL_P (t))
14385 return tsubst_decl (t, args, complain);
14386
14387 if (args == NULL_TREE)
14388 return t;
14389
14390 code = TREE_CODE (t);
14391
14392 if (code == IDENTIFIER_NODE)
14393 type = IDENTIFIER_TYPE_VALUE (t);
14394 else
14395 type = TREE_TYPE (t);
14396
14397 gcc_assert (type != unknown_type_node);
14398
14399 /* Reuse typedefs. We need to do this to handle dependent attributes,
14400 such as attribute aligned. */
14401 if (TYPE_P (t)
14402 && typedef_variant_p (t))
14403 {
14404 tree decl = TYPE_NAME (t);
14405
14406 if (alias_template_specialization_p (t))
14407 {
14408 /* DECL represents an alias template and we want to
14409 instantiate it. */
14410 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
14411 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
14412 r = instantiate_alias_template (tmpl, gen_args, complain);
14413 }
14414 else if (DECL_CLASS_SCOPE_P (decl)
14415 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
14416 && uses_template_parms (DECL_CONTEXT (decl)))
14417 {
14418 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
14419 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
14420 r = retrieve_specialization (tmpl, gen_args, 0);
14421 }
14422 else if (DECL_FUNCTION_SCOPE_P (decl)
14423 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
14424 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
14425 r = retrieve_local_specialization (decl);
14426 else
14427 /* The typedef is from a non-template context. */
14428 return t;
14429
14430 if (r)
14431 {
14432 r = TREE_TYPE (r);
14433 r = cp_build_qualified_type_real
14434 (r, cp_type_quals (t) | cp_type_quals (r),
14435 complain | tf_ignore_bad_quals);
14436 return r;
14437 }
14438 else
14439 {
14440 /* We don't have an instantiation yet, so drop the typedef. */
14441 int quals = cp_type_quals (t);
14442 t = DECL_ORIGINAL_TYPE (decl);
14443 t = cp_build_qualified_type_real (t, quals,
14444 complain | tf_ignore_bad_quals);
14445 }
14446 }
14447
14448 bool fndecl_type = (complain & tf_fndecl_type);
14449 complain &= ~tf_fndecl_type;
14450
14451 if (type
14452 && code != TYPENAME_TYPE
14453 && code != TEMPLATE_TYPE_PARM
14454 && code != TEMPLATE_PARM_INDEX
14455 && code != IDENTIFIER_NODE
14456 && code != FUNCTION_TYPE
14457 && code != METHOD_TYPE)
14458 type = tsubst (type, args, complain, in_decl);
14459 if (type == error_mark_node)
14460 return error_mark_node;
14461
14462 switch (code)
14463 {
14464 case RECORD_TYPE:
14465 case UNION_TYPE:
14466 case ENUMERAL_TYPE:
14467 return tsubst_aggr_type (t, args, complain, in_decl,
14468 /*entering_scope=*/0);
14469
14470 case ERROR_MARK:
14471 case IDENTIFIER_NODE:
14472 case VOID_TYPE:
14473 case REAL_TYPE:
14474 case COMPLEX_TYPE:
14475 case VECTOR_TYPE:
14476 case BOOLEAN_TYPE:
14477 case NULLPTR_TYPE:
14478 case LANG_TYPE:
14479 return t;
14480
14481 case INTEGER_TYPE:
14482 if (t == integer_type_node)
14483 return t;
14484
14485 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
14486 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
14487 return t;
14488
14489 {
14490 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
14491
14492 max = tsubst_expr (omax, args, complain, in_decl,
14493 /*integral_constant_expression_p=*/false);
14494
14495 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
14496 needed. */
14497 if (TREE_CODE (max) == NOP_EXPR
14498 && TREE_SIDE_EFFECTS (omax)
14499 && !TREE_TYPE (max))
14500 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
14501
14502 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
14503 with TREE_SIDE_EFFECTS that indicates this is not an integral
14504 constant expression. */
14505 if (processing_template_decl
14506 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
14507 {
14508 gcc_assert (TREE_CODE (max) == NOP_EXPR);
14509 TREE_SIDE_EFFECTS (max) = 1;
14510 }
14511
14512 return compute_array_index_type (NULL_TREE, max, complain);
14513 }
14514
14515 case TEMPLATE_TYPE_PARM:
14516 case TEMPLATE_TEMPLATE_PARM:
14517 case BOUND_TEMPLATE_TEMPLATE_PARM:
14518 case TEMPLATE_PARM_INDEX:
14519 {
14520 int idx;
14521 int level;
14522 int levels;
14523 tree arg = NULL_TREE;
14524
14525 /* Early in template argument deduction substitution, we don't
14526 want to reduce the level of 'auto', or it will be confused
14527 with a normal template parm in subsequent deduction. */
14528 if (is_auto (t) && (complain & tf_partial))
14529 return t;
14530
14531 r = NULL_TREE;
14532
14533 gcc_assert (TREE_VEC_LENGTH (args) > 0);
14534 template_parm_level_and_index (t, &level, &idx);
14535
14536 levels = TMPL_ARGS_DEPTH (args);
14537 if (level <= levels
14538 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
14539 {
14540 arg = TMPL_ARG (args, level, idx);
14541
14542 /* See through ARGUMENT_PACK_SELECT arguments. */
14543 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
14544 arg = argument_pack_select_arg (arg);
14545 }
14546
14547 if (arg == error_mark_node)
14548 return error_mark_node;
14549 else if (arg != NULL_TREE)
14550 {
14551 if (ARGUMENT_PACK_P (arg))
14552 /* If ARG is an argument pack, we don't actually want to
14553 perform a substitution here, because substitutions
14554 for argument packs are only done
14555 element-by-element. We can get to this point when
14556 substituting the type of a non-type template
14557 parameter pack, when that type actually contains
14558 template parameter packs from an outer template, e.g.,
14559
14560 template<typename... Types> struct A {
14561 template<Types... Values> struct B { };
14562 }; */
14563 return t;
14564
14565 if (code == TEMPLATE_TYPE_PARM)
14566 {
14567 int quals;
14568 gcc_assert (TYPE_P (arg));
14569
14570 quals = cp_type_quals (arg) | cp_type_quals (t);
14571
14572 return cp_build_qualified_type_real
14573 (arg, quals, complain | tf_ignore_bad_quals);
14574 }
14575 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
14576 {
14577 /* We are processing a type constructed from a
14578 template template parameter. */
14579 tree argvec = tsubst (TYPE_TI_ARGS (t),
14580 args, complain, in_decl);
14581 if (argvec == error_mark_node)
14582 return error_mark_node;
14583
14584 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
14585 || TREE_CODE (arg) == TEMPLATE_DECL
14586 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
14587
14588 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
14589 /* Consider this code:
14590
14591 template <template <class> class Template>
14592 struct Internal {
14593 template <class Arg> using Bind = Template<Arg>;
14594 };
14595
14596 template <template <class> class Template, class Arg>
14597 using Instantiate = Template<Arg>; //#0
14598
14599 template <template <class> class Template,
14600 class Argument>
14601 using Bind =
14602 Instantiate<Internal<Template>::template Bind,
14603 Argument>; //#1
14604
14605 When #1 is parsed, the
14606 BOUND_TEMPLATE_TEMPLATE_PARM representing the
14607 parameter `Template' in #0 matches the
14608 UNBOUND_CLASS_TEMPLATE representing the argument
14609 `Internal<Template>::template Bind'; We then want
14610 to assemble the type `Bind<Argument>' that can't
14611 be fully created right now, because
14612 `Internal<Template>' not being complete, the Bind
14613 template cannot be looked up in that context. So
14614 we need to "store" `Bind<Argument>' for later
14615 when the context of Bind becomes complete. Let's
14616 store that in a TYPENAME_TYPE. */
14617 return make_typename_type (TYPE_CONTEXT (arg),
14618 build_nt (TEMPLATE_ID_EXPR,
14619 TYPE_IDENTIFIER (arg),
14620 argvec),
14621 typename_type,
14622 complain);
14623
14624 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
14625 are resolving nested-types in the signature of a
14626 member function templates. Otherwise ARG is a
14627 TEMPLATE_DECL and is the real template to be
14628 instantiated. */
14629 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
14630 arg = TYPE_NAME (arg);
14631
14632 r = lookup_template_class (arg,
14633 argvec, in_decl,
14634 DECL_CONTEXT (arg),
14635 /*entering_scope=*/0,
14636 complain);
14637 return cp_build_qualified_type_real
14638 (r, cp_type_quals (t) | cp_type_quals (r), complain);
14639 }
14640 else if (code == TEMPLATE_TEMPLATE_PARM)
14641 return arg;
14642 else
14643 /* TEMPLATE_PARM_INDEX. */
14644 return convert_from_reference (unshare_expr (arg));
14645 }
14646
14647 if (level == 1)
14648 /* This can happen during the attempted tsubst'ing in
14649 unify. This means that we don't yet have any information
14650 about the template parameter in question. */
14651 return t;
14652
14653 /* If we get here, we must have been looking at a parm for a
14654 more deeply nested template. Make a new version of this
14655 template parameter, but with a lower level. */
14656 switch (code)
14657 {
14658 case TEMPLATE_TYPE_PARM:
14659 case TEMPLATE_TEMPLATE_PARM:
14660 case BOUND_TEMPLATE_TEMPLATE_PARM:
14661 if (cp_type_quals (t))
14662 {
14663 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
14664 r = cp_build_qualified_type_real
14665 (r, cp_type_quals (t),
14666 complain | (code == TEMPLATE_TYPE_PARM
14667 ? tf_ignore_bad_quals : 0));
14668 }
14669 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
14670 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
14671 && (r = (TEMPLATE_PARM_DESCENDANTS
14672 (TEMPLATE_TYPE_PARM_INDEX (t))))
14673 && (r = TREE_TYPE (r))
14674 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
14675 /* Break infinite recursion when substituting the constraints
14676 of a constrained placeholder. */;
14677 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
14678 && !PLACEHOLDER_TYPE_CONSTRAINTS (t)
14679 && !CLASS_PLACEHOLDER_TEMPLATE (t)
14680 && (arg = TEMPLATE_TYPE_PARM_INDEX (t),
14681 r = TEMPLATE_PARM_DESCENDANTS (arg))
14682 && (TEMPLATE_PARM_LEVEL (r)
14683 == TEMPLATE_PARM_LEVEL (arg) - levels))
14684 /* Cache the simple case of lowering a type parameter. */
14685 r = TREE_TYPE (r);
14686 else
14687 {
14688 r = copy_type (t);
14689 TEMPLATE_TYPE_PARM_INDEX (r)
14690 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
14691 r, levels, args, complain);
14692 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
14693 TYPE_MAIN_VARIANT (r) = r;
14694 TYPE_POINTER_TO (r) = NULL_TREE;
14695 TYPE_REFERENCE_TO (r) = NULL_TREE;
14696
14697 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
14698 {
14699 /* Propagate constraints on placeholders. */
14700 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
14701 PLACEHOLDER_TYPE_CONSTRAINTS (r)
14702 = tsubst_constraint (constr, args, complain, in_decl);
14703 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
14704 {
14705 pl = tsubst_copy (pl, args, complain, in_decl);
14706 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
14707 }
14708 }
14709
14710 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
14711 /* We have reduced the level of the template
14712 template parameter, but not the levels of its
14713 template parameters, so canonical_type_parameter
14714 will not be able to find the canonical template
14715 template parameter for this level. Thus, we
14716 require structural equality checking to compare
14717 TEMPLATE_TEMPLATE_PARMs. */
14718 SET_TYPE_STRUCTURAL_EQUALITY (r);
14719 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
14720 SET_TYPE_STRUCTURAL_EQUALITY (r);
14721 else
14722 TYPE_CANONICAL (r) = canonical_type_parameter (r);
14723
14724 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
14725 {
14726 tree tinfo = TYPE_TEMPLATE_INFO (t);
14727 /* We might need to substitute into the types of non-type
14728 template parameters. */
14729 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
14730 complain, in_decl);
14731 if (tmpl == error_mark_node)
14732 return error_mark_node;
14733 tree argvec = tsubst (TI_ARGS (tinfo), args,
14734 complain, in_decl);
14735 if (argvec == error_mark_node)
14736 return error_mark_node;
14737
14738 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
14739 = build_template_info (tmpl, argvec);
14740 }
14741 }
14742 break;
14743
14744 case TEMPLATE_PARM_INDEX:
14745 /* OK, now substitute the type of the non-type parameter. We
14746 couldn't do it earlier because it might be an auto parameter,
14747 and we wouldn't need to if we had an argument. */
14748 type = tsubst (type, args, complain, in_decl);
14749 if (type == error_mark_node)
14750 return error_mark_node;
14751 r = reduce_template_parm_level (t, type, levels, args, complain);
14752 break;
14753
14754 default:
14755 gcc_unreachable ();
14756 }
14757
14758 return r;
14759 }
14760
14761 case TREE_LIST:
14762 {
14763 tree purpose, value, chain;
14764
14765 if (t == void_list_node)
14766 return t;
14767
14768 purpose = TREE_PURPOSE (t);
14769 if (purpose)
14770 {
14771 purpose = tsubst (purpose, args, complain, in_decl);
14772 if (purpose == error_mark_node)
14773 return error_mark_node;
14774 }
14775 value = TREE_VALUE (t);
14776 if (value)
14777 {
14778 value = tsubst (value, args, complain, in_decl);
14779 if (value == error_mark_node)
14780 return error_mark_node;
14781 }
14782 chain = TREE_CHAIN (t);
14783 if (chain && chain != void_type_node)
14784 {
14785 chain = tsubst (chain, args, complain, in_decl);
14786 if (chain == error_mark_node)
14787 return error_mark_node;
14788 }
14789 if (purpose == TREE_PURPOSE (t)
14790 && value == TREE_VALUE (t)
14791 && chain == TREE_CHAIN (t))
14792 return t;
14793 return hash_tree_cons (purpose, value, chain);
14794 }
14795
14796 case TREE_BINFO:
14797 /* We should never be tsubsting a binfo. */
14798 gcc_unreachable ();
14799
14800 case TREE_VEC:
14801 /* A vector of template arguments. */
14802 gcc_assert (!type);
14803 return tsubst_template_args (t, args, complain, in_decl);
14804
14805 case POINTER_TYPE:
14806 case REFERENCE_TYPE:
14807 {
14808 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
14809 return t;
14810
14811 /* [temp.deduct]
14812
14813 Type deduction may fail for any of the following
14814 reasons:
14815
14816 -- Attempting to create a pointer to reference type.
14817 -- Attempting to create a reference to a reference type or
14818 a reference to void.
14819
14820 Core issue 106 says that creating a reference to a reference
14821 during instantiation is no longer a cause for failure. We
14822 only enforce this check in strict C++98 mode. */
14823 if ((TYPE_REF_P (type)
14824 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
14825 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
14826 {
14827 static location_t last_loc;
14828
14829 /* We keep track of the last time we issued this error
14830 message to avoid spewing a ton of messages during a
14831 single bad template instantiation. */
14832 if (complain & tf_error
14833 && last_loc != input_location)
14834 {
14835 if (VOID_TYPE_P (type))
14836 error ("forming reference to void");
14837 else if (code == POINTER_TYPE)
14838 error ("forming pointer to reference type %qT", type);
14839 else
14840 error ("forming reference to reference type %qT", type);
14841 last_loc = input_location;
14842 }
14843
14844 return error_mark_node;
14845 }
14846 else if (TREE_CODE (type) == FUNCTION_TYPE
14847 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
14848 || type_memfn_rqual (type) != REF_QUAL_NONE))
14849 {
14850 if (complain & tf_error)
14851 {
14852 if (code == POINTER_TYPE)
14853 error ("forming pointer to qualified function type %qT",
14854 type);
14855 else
14856 error ("forming reference to qualified function type %qT",
14857 type);
14858 }
14859 return error_mark_node;
14860 }
14861 else if (code == POINTER_TYPE)
14862 {
14863 r = build_pointer_type (type);
14864 if (TREE_CODE (type) == METHOD_TYPE)
14865 r = build_ptrmemfunc_type (r);
14866 }
14867 else if (TYPE_REF_P (type))
14868 /* In C++0x, during template argument substitution, when there is an
14869 attempt to create a reference to a reference type, reference
14870 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14871
14872 "If a template-argument for a template-parameter T names a type
14873 that is a reference to a type A, an attempt to create the type
14874 'lvalue reference to cv T' creates the type 'lvalue reference to
14875 A,' while an attempt to create the type type rvalue reference to
14876 cv T' creates the type T"
14877 */
14878 r = cp_build_reference_type
14879 (TREE_TYPE (type),
14880 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
14881 else
14882 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
14883 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
14884
14885 if (r != error_mark_node)
14886 /* Will this ever be needed for TYPE_..._TO values? */
14887 layout_type (r);
14888
14889 return r;
14890 }
14891 case OFFSET_TYPE:
14892 {
14893 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
14894 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
14895 {
14896 /* [temp.deduct]
14897
14898 Type deduction may fail for any of the following
14899 reasons:
14900
14901 -- Attempting to create "pointer to member of T" when T
14902 is not a class type. */
14903 if (complain & tf_error)
14904 error ("creating pointer to member of non-class type %qT", r);
14905 return error_mark_node;
14906 }
14907 if (TYPE_REF_P (type))
14908 {
14909 if (complain & tf_error)
14910 error ("creating pointer to member reference type %qT", type);
14911 return error_mark_node;
14912 }
14913 if (VOID_TYPE_P (type))
14914 {
14915 if (complain & tf_error)
14916 error ("creating pointer to member of type void");
14917 return error_mark_node;
14918 }
14919 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
14920 if (TREE_CODE (type) == FUNCTION_TYPE)
14921 {
14922 /* The type of the implicit object parameter gets its
14923 cv-qualifiers from the FUNCTION_TYPE. */
14924 tree memptr;
14925 tree method_type
14926 = build_memfn_type (type, r, type_memfn_quals (type),
14927 type_memfn_rqual (type));
14928 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
14929 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
14930 complain);
14931 }
14932 else
14933 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
14934 cp_type_quals (t),
14935 complain);
14936 }
14937 case FUNCTION_TYPE:
14938 case METHOD_TYPE:
14939 {
14940 tree fntype;
14941 tree specs;
14942 fntype = tsubst_function_type (t, args, complain, in_decl);
14943 if (fntype == error_mark_node)
14944 return error_mark_node;
14945
14946 /* Substitute the exception specification. */
14947 specs = tsubst_exception_specification (t, args, complain, in_decl,
14948 /*defer_ok*/fndecl_type);
14949 if (specs == error_mark_node)
14950 return error_mark_node;
14951 if (specs)
14952 fntype = build_exception_variant (fntype, specs);
14953 return fntype;
14954 }
14955 case ARRAY_TYPE:
14956 {
14957 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
14958 if (domain == error_mark_node)
14959 return error_mark_node;
14960
14961 /* As an optimization, we avoid regenerating the array type if
14962 it will obviously be the same as T. */
14963 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
14964 return t;
14965
14966 /* These checks should match the ones in create_array_type_for_decl.
14967
14968 [temp.deduct]
14969
14970 The deduction may fail for any of the following reasons:
14971
14972 -- Attempting to create an array with an element type that
14973 is void, a function type, or a reference type, or [DR337]
14974 an abstract class type. */
14975 if (VOID_TYPE_P (type)
14976 || TREE_CODE (type) == FUNCTION_TYPE
14977 || (TREE_CODE (type) == ARRAY_TYPE
14978 && TYPE_DOMAIN (type) == NULL_TREE)
14979 || TYPE_REF_P (type))
14980 {
14981 if (complain & tf_error)
14982 error ("creating array of %qT", type);
14983 return error_mark_node;
14984 }
14985
14986 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
14987 return error_mark_node;
14988
14989 r = build_cplus_array_type (type, domain);
14990
14991 if (!valid_array_size_p (input_location, r, in_decl,
14992 (complain & tf_error)))
14993 return error_mark_node;
14994
14995 if (TYPE_USER_ALIGN (t))
14996 {
14997 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
14998 TYPE_USER_ALIGN (r) = 1;
14999 }
15000
15001 return r;
15002 }
15003
15004 case TYPENAME_TYPE:
15005 {
15006 tree ctx = TYPE_CONTEXT (t);
15007 if (TREE_CODE (ctx) == TYPE_PACK_EXPANSION)
15008 {
15009 ctx = tsubst_pack_expansion (ctx, args, complain, in_decl);
15010 if (ctx == error_mark_node
15011 || TREE_VEC_LENGTH (ctx) > 1)
15012 return error_mark_node;
15013 if (TREE_VEC_LENGTH (ctx) == 0)
15014 {
15015 if (complain & tf_error)
15016 error ("%qD is instantiated for an empty pack",
15017 TYPENAME_TYPE_FULLNAME (t));
15018 return error_mark_node;
15019 }
15020 ctx = TREE_VEC_ELT (ctx, 0);
15021 }
15022 else
15023 ctx = tsubst_aggr_type (ctx, args, complain, in_decl,
15024 /*entering_scope=*/1);
15025 if (ctx == error_mark_node)
15026 return error_mark_node;
15027
15028 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
15029 complain, in_decl);
15030 if (f == error_mark_node)
15031 return error_mark_node;
15032
15033 if (!MAYBE_CLASS_TYPE_P (ctx))
15034 {
15035 if (complain & tf_error)
15036 error ("%qT is not a class, struct, or union type", ctx);
15037 return error_mark_node;
15038 }
15039 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
15040 {
15041 /* Normally, make_typename_type does not require that the CTX
15042 have complete type in order to allow things like:
15043
15044 template <class T> struct S { typename S<T>::X Y; };
15045
15046 But, such constructs have already been resolved by this
15047 point, so here CTX really should have complete type, unless
15048 it's a partial instantiation. */
15049 ctx = complete_type (ctx);
15050 if (!COMPLETE_TYPE_P (ctx))
15051 {
15052 if (complain & tf_error)
15053 cxx_incomplete_type_error (NULL_TREE, ctx);
15054 return error_mark_node;
15055 }
15056 }
15057
15058 f = make_typename_type (ctx, f, typename_type,
15059 complain | tf_keep_type_decl);
15060 if (f == error_mark_node)
15061 return f;
15062 if (TREE_CODE (f) == TYPE_DECL)
15063 {
15064 complain |= tf_ignore_bad_quals;
15065 f = TREE_TYPE (f);
15066 }
15067
15068 if (TREE_CODE (f) != TYPENAME_TYPE)
15069 {
15070 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
15071 {
15072 if (complain & tf_error)
15073 error ("%qT resolves to %qT, which is not an enumeration type",
15074 t, f);
15075 else
15076 return error_mark_node;
15077 }
15078 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
15079 {
15080 if (complain & tf_error)
15081 error ("%qT resolves to %qT, which is is not a class type",
15082 t, f);
15083 else
15084 return error_mark_node;
15085 }
15086 }
15087
15088 return cp_build_qualified_type_real
15089 (f, cp_type_quals (f) | cp_type_quals (t), complain);
15090 }
15091
15092 case UNBOUND_CLASS_TEMPLATE:
15093 {
15094 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
15095 in_decl, /*entering_scope=*/1);
15096 tree name = TYPE_IDENTIFIER (t);
15097 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
15098
15099 if (ctx == error_mark_node || name == error_mark_node)
15100 return error_mark_node;
15101
15102 if (parm_list)
15103 parm_list = tsubst_template_parms (parm_list, args, complain);
15104 return make_unbound_class_template (ctx, name, parm_list, complain);
15105 }
15106
15107 case TYPEOF_TYPE:
15108 {
15109 tree type;
15110
15111 ++cp_unevaluated_operand;
15112 ++c_inhibit_evaluation_warnings;
15113
15114 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
15115 complain, in_decl,
15116 /*integral_constant_expression_p=*/false);
15117
15118 --cp_unevaluated_operand;
15119 --c_inhibit_evaluation_warnings;
15120
15121 type = finish_typeof (type);
15122 return cp_build_qualified_type_real (type,
15123 cp_type_quals (t)
15124 | cp_type_quals (type),
15125 complain);
15126 }
15127
15128 case DECLTYPE_TYPE:
15129 {
15130 tree type;
15131
15132 ++cp_unevaluated_operand;
15133 ++c_inhibit_evaluation_warnings;
15134
15135 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
15136 complain|tf_decltype, in_decl,
15137 /*function_p*/false,
15138 /*integral_constant_expression*/false);
15139
15140 --cp_unevaluated_operand;
15141 --c_inhibit_evaluation_warnings;
15142
15143 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
15144 type = lambda_capture_field_type (type,
15145 false /*explicit_init*/,
15146 DECLTYPE_FOR_REF_CAPTURE (t));
15147 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
15148 type = lambda_proxy_type (type);
15149 else
15150 {
15151 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
15152 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
15153 && EXPR_P (type))
15154 /* In a template ~id could be either a complement expression
15155 or an unqualified-id naming a destructor; if instantiating
15156 it produces an expression, it's not an id-expression or
15157 member access. */
15158 id = false;
15159 type = finish_decltype_type (type, id, complain);
15160 }
15161 return cp_build_qualified_type_real (type,
15162 cp_type_quals (t)
15163 | cp_type_quals (type),
15164 complain | tf_ignore_bad_quals);
15165 }
15166
15167 case UNDERLYING_TYPE:
15168 {
15169 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
15170 complain, in_decl);
15171 return finish_underlying_type (type);
15172 }
15173
15174 case TYPE_ARGUMENT_PACK:
15175 case NONTYPE_ARGUMENT_PACK:
15176 {
15177 tree r;
15178
15179 if (code == NONTYPE_ARGUMENT_PACK)
15180 r = make_node (code);
15181 else
15182 r = cxx_make_type (code);
15183
15184 tree pack_args = ARGUMENT_PACK_ARGS (t);
15185 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
15186 SET_ARGUMENT_PACK_ARGS (r, pack_args);
15187
15188 return r;
15189 }
15190
15191 case VOID_CST:
15192 case INTEGER_CST:
15193 case REAL_CST:
15194 case STRING_CST:
15195 case PLUS_EXPR:
15196 case MINUS_EXPR:
15197 case NEGATE_EXPR:
15198 case NOP_EXPR:
15199 case INDIRECT_REF:
15200 case ADDR_EXPR:
15201 case CALL_EXPR:
15202 case ARRAY_REF:
15203 case SCOPE_REF:
15204 /* We should use one of the expression tsubsts for these codes. */
15205 gcc_unreachable ();
15206
15207 default:
15208 sorry ("use of %qs in template", get_tree_code_name (code));
15209 return error_mark_node;
15210 }
15211 }
15212
15213 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
15214 expression on the left-hand side of the "." or "->" operator. We
15215 only do the lookup if we had a dependent BASELINK. Otherwise we
15216 adjust it onto the instantiated heirarchy. */
15217
15218 static tree
15219 tsubst_baselink (tree baselink, tree object_type,
15220 tree args, tsubst_flags_t complain, tree in_decl)
15221 {
15222 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
15223 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
15224 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
15225
15226 tree optype = BASELINK_OPTYPE (baselink);
15227 optype = tsubst (optype, args, complain, in_decl);
15228
15229 tree template_args = NULL_TREE;
15230 bool template_id_p = false;
15231 tree fns = BASELINK_FUNCTIONS (baselink);
15232 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
15233 {
15234 template_id_p = true;
15235 template_args = TREE_OPERAND (fns, 1);
15236 fns = TREE_OPERAND (fns, 0);
15237 if (template_args)
15238 template_args = tsubst_template_args (template_args, args,
15239 complain, in_decl);
15240 }
15241
15242 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
15243 binfo_type = tsubst (binfo_type, args, complain, in_decl);
15244 bool dependent_p = binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink));
15245
15246 if (dependent_p)
15247 {
15248 tree name = OVL_NAME (fns);
15249 if (IDENTIFIER_CONV_OP_P (name))
15250 name = make_conv_op_name (optype);
15251
15252 if (name == complete_dtor_identifier)
15253 /* Treat as-if non-dependent below. */
15254 dependent_p = false;
15255
15256 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
15257 if (!baselink)
15258 {
15259 if ((complain & tf_error)
15260 && constructor_name_p (name, qualifying_scope))
15261 error ("cannot call constructor %<%T::%D%> directly",
15262 qualifying_scope, name);
15263 return error_mark_node;
15264 }
15265
15266 if (BASELINK_P (baselink))
15267 fns = BASELINK_FUNCTIONS (baselink);
15268 }
15269 else
15270 /* We're going to overwrite pieces below, make a duplicate. */
15271 baselink = copy_node (baselink);
15272
15273 /* If lookup found a single function, mark it as used at this point.
15274 (If lookup found multiple functions the one selected later by
15275 overload resolution will be marked as used at that point.) */
15276 if (!template_id_p && !really_overloaded_fn (fns))
15277 {
15278 tree fn = OVL_FIRST (fns);
15279 bool ok = mark_used (fn, complain);
15280 if (!ok && !(complain & tf_error))
15281 return error_mark_node;
15282 if (ok && BASELINK_P (baselink))
15283 /* We might have instantiated an auto function. */
15284 TREE_TYPE (baselink) = TREE_TYPE (fn);
15285 }
15286
15287 if (BASELINK_P (baselink))
15288 {
15289 /* Add back the template arguments, if present. */
15290 if (template_id_p)
15291 BASELINK_FUNCTIONS (baselink)
15292 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
15293
15294 /* Update the conversion operator type. */
15295 BASELINK_OPTYPE (baselink) = optype;
15296 }
15297
15298 if (!object_type)
15299 object_type = current_class_type;
15300
15301 if (qualified_p || !dependent_p)
15302 {
15303 baselink = adjust_result_of_qualified_name_lookup (baselink,
15304 qualifying_scope,
15305 object_type);
15306 if (!qualified_p)
15307 /* We need to call adjust_result_of_qualified_name_lookup in case the
15308 destructor names a base class, but we unset BASELINK_QUALIFIED_P
15309 so that we still get virtual function binding. */
15310 BASELINK_QUALIFIED_P (baselink) = false;
15311 }
15312
15313 return baselink;
15314 }
15315
15316 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
15317 true if the qualified-id will be a postfix-expression in-and-of
15318 itself; false if more of the postfix-expression follows the
15319 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
15320 of "&". */
15321
15322 static tree
15323 tsubst_qualified_id (tree qualified_id, tree args,
15324 tsubst_flags_t complain, tree in_decl,
15325 bool done, bool address_p)
15326 {
15327 tree expr;
15328 tree scope;
15329 tree name;
15330 bool is_template;
15331 tree template_args;
15332 location_t loc = UNKNOWN_LOCATION;
15333
15334 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
15335
15336 /* Figure out what name to look up. */
15337 name = TREE_OPERAND (qualified_id, 1);
15338 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
15339 {
15340 is_template = true;
15341 loc = EXPR_LOCATION (name);
15342 template_args = TREE_OPERAND (name, 1);
15343 if (template_args)
15344 template_args = tsubst_template_args (template_args, args,
15345 complain, in_decl);
15346 if (template_args == error_mark_node)
15347 return error_mark_node;
15348 name = TREE_OPERAND (name, 0);
15349 }
15350 else
15351 {
15352 is_template = false;
15353 template_args = NULL_TREE;
15354 }
15355
15356 /* Substitute into the qualifying scope. When there are no ARGS, we
15357 are just trying to simplify a non-dependent expression. In that
15358 case the qualifying scope may be dependent, and, in any case,
15359 substituting will not help. */
15360 scope = TREE_OPERAND (qualified_id, 0);
15361 if (args)
15362 {
15363 scope = tsubst (scope, args, complain, in_decl);
15364 expr = tsubst_copy (name, args, complain, in_decl);
15365 }
15366 else
15367 expr = name;
15368
15369 if (dependent_scope_p (scope))
15370 {
15371 if (is_template)
15372 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
15373 tree r = build_qualified_name (NULL_TREE, scope, expr,
15374 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
15375 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
15376 return r;
15377 }
15378
15379 if (!BASELINK_P (name) && !DECL_P (expr))
15380 {
15381 if (TREE_CODE (expr) == BIT_NOT_EXPR)
15382 {
15383 /* A BIT_NOT_EXPR is used to represent a destructor. */
15384 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
15385 {
15386 error ("qualifying type %qT does not match destructor name ~%qT",
15387 scope, TREE_OPERAND (expr, 0));
15388 expr = error_mark_node;
15389 }
15390 else
15391 expr = lookup_qualified_name (scope, complete_dtor_identifier,
15392 /*is_type_p=*/0, false);
15393 }
15394 else
15395 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
15396 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
15397 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
15398 {
15399 if (complain & tf_error)
15400 {
15401 error ("dependent-name %qE is parsed as a non-type, but "
15402 "instantiation yields a type", qualified_id);
15403 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
15404 }
15405 return error_mark_node;
15406 }
15407 }
15408
15409 if (DECL_P (expr))
15410 {
15411 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
15412 scope);
15413 /* Remember that there was a reference to this entity. */
15414 if (!mark_used (expr, complain) && !(complain & tf_error))
15415 return error_mark_node;
15416 }
15417
15418 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
15419 {
15420 if (complain & tf_error)
15421 qualified_name_lookup_error (scope,
15422 TREE_OPERAND (qualified_id, 1),
15423 expr, input_location);
15424 return error_mark_node;
15425 }
15426
15427 if (is_template)
15428 {
15429 /* We may be repeating a check already done during parsing, but
15430 if it was well-formed and passed then, it will pass again
15431 now, and if it didn't, we wouldn't have got here. The case
15432 we want to catch is when we couldn't tell then, and can now,
15433 namely when templ prior to substitution was an
15434 identifier. */
15435 if (flag_concepts && check_auto_in_tmpl_args (expr, template_args))
15436 return error_mark_node;
15437
15438 if (variable_template_p (expr))
15439 expr = lookup_and_finish_template_variable (expr, template_args,
15440 complain);
15441 else
15442 expr = lookup_template_function (expr, template_args);
15443 }
15444
15445 if (expr == error_mark_node && complain & tf_error)
15446 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
15447 expr, input_location);
15448 else if (TYPE_P (scope))
15449 {
15450 expr = (adjust_result_of_qualified_name_lookup
15451 (expr, scope, current_nonlambda_class_type ()));
15452 expr = (finish_qualified_id_expr
15453 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
15454 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
15455 /*template_arg_p=*/false, complain));
15456 }
15457
15458 /* Expressions do not generally have reference type. */
15459 if (TREE_CODE (expr) != SCOPE_REF
15460 /* However, if we're about to form a pointer-to-member, we just
15461 want the referenced member referenced. */
15462 && TREE_CODE (expr) != OFFSET_REF)
15463 expr = convert_from_reference (expr);
15464
15465 if (REF_PARENTHESIZED_P (qualified_id))
15466 expr = force_paren_expr (expr);
15467
15468 return expr;
15469 }
15470
15471 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
15472 initializer, DECL is the substituted VAR_DECL. Other arguments are as
15473 for tsubst. */
15474
15475 static tree
15476 tsubst_init (tree init, tree decl, tree args,
15477 tsubst_flags_t complain, tree in_decl)
15478 {
15479 if (!init)
15480 return NULL_TREE;
15481
15482 init = tsubst_expr (init, args, complain, in_decl, false);
15483
15484 tree type = TREE_TYPE (decl);
15485
15486 if (!init && type != error_mark_node)
15487 {
15488 if (tree auto_node = type_uses_auto (type))
15489 {
15490 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
15491 {
15492 if (complain & tf_error)
15493 error ("initializer for %q#D expands to an empty list "
15494 "of expressions", decl);
15495 return error_mark_node;
15496 }
15497 }
15498 else if (!dependent_type_p (type))
15499 {
15500 /* If we had an initializer but it
15501 instantiated to nothing,
15502 value-initialize the object. This will
15503 only occur when the initializer was a
15504 pack expansion where the parameter packs
15505 used in that expansion were of length
15506 zero. */
15507 init = build_value_init (type, complain);
15508 if (TREE_CODE (init) == AGGR_INIT_EXPR)
15509 init = get_target_expr_sfinae (init, complain);
15510 if (TREE_CODE (init) == TARGET_EXPR)
15511 TARGET_EXPR_DIRECT_INIT_P (init) = true;
15512 }
15513 }
15514
15515 return init;
15516 }
15517
15518 /* Like tsubst, but deals with expressions. This function just replaces
15519 template parms; to finish processing the resultant expression, use
15520 tsubst_copy_and_build or tsubst_expr. */
15521
15522 static tree
15523 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15524 {
15525 enum tree_code code;
15526 tree r;
15527
15528 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
15529 return t;
15530
15531 code = TREE_CODE (t);
15532
15533 switch (code)
15534 {
15535 case PARM_DECL:
15536 r = retrieve_local_specialization (t);
15537
15538 if (r == NULL_TREE)
15539 {
15540 /* We get here for a use of 'this' in an NSDMI. */
15541 if (DECL_NAME (t) == this_identifier && current_class_ptr)
15542 return current_class_ptr;
15543
15544 /* This can happen for a parameter name used later in a function
15545 declaration (such as in a late-specified return type). Just
15546 make a dummy decl, since it's only used for its type. */
15547 gcc_assert (cp_unevaluated_operand != 0);
15548 r = tsubst_decl (t, args, complain);
15549 /* Give it the template pattern as its context; its true context
15550 hasn't been instantiated yet and this is good enough for
15551 mangling. */
15552 DECL_CONTEXT (r) = DECL_CONTEXT (t);
15553 }
15554
15555 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
15556 r = argument_pack_select_arg (r);
15557 if (!mark_used (r, complain) && !(complain & tf_error))
15558 return error_mark_node;
15559 return r;
15560
15561 case CONST_DECL:
15562 {
15563 tree enum_type;
15564 tree v;
15565
15566 if (DECL_TEMPLATE_PARM_P (t))
15567 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
15568 /* There is no need to substitute into namespace-scope
15569 enumerators. */
15570 if (DECL_NAMESPACE_SCOPE_P (t))
15571 return t;
15572 /* If ARGS is NULL, then T is known to be non-dependent. */
15573 if (args == NULL_TREE)
15574 return scalar_constant_value (t);
15575
15576 /* Unfortunately, we cannot just call lookup_name here.
15577 Consider:
15578
15579 template <int I> int f() {
15580 enum E { a = I };
15581 struct S { void g() { E e = a; } };
15582 };
15583
15584 When we instantiate f<7>::S::g(), say, lookup_name is not
15585 clever enough to find f<7>::a. */
15586 enum_type
15587 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
15588 /*entering_scope=*/0);
15589
15590 for (v = TYPE_VALUES (enum_type);
15591 v != NULL_TREE;
15592 v = TREE_CHAIN (v))
15593 if (TREE_PURPOSE (v) == DECL_NAME (t))
15594 return TREE_VALUE (v);
15595
15596 /* We didn't find the name. That should never happen; if
15597 name-lookup found it during preliminary parsing, we
15598 should find it again here during instantiation. */
15599 gcc_unreachable ();
15600 }
15601 return t;
15602
15603 case FIELD_DECL:
15604 if (DECL_CONTEXT (t))
15605 {
15606 tree ctx;
15607
15608 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
15609 /*entering_scope=*/1);
15610 if (ctx != DECL_CONTEXT (t))
15611 {
15612 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
15613 if (!r)
15614 {
15615 if (complain & tf_error)
15616 error ("using invalid field %qD", t);
15617 return error_mark_node;
15618 }
15619 return r;
15620 }
15621 }
15622
15623 return t;
15624
15625 case VAR_DECL:
15626 case FUNCTION_DECL:
15627 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
15628 r = tsubst (t, args, complain, in_decl);
15629 else if (local_variable_p (t)
15630 && uses_template_parms (DECL_CONTEXT (t)))
15631 {
15632 r = retrieve_local_specialization (t);
15633 if (r == NULL_TREE)
15634 {
15635 /* First try name lookup to find the instantiation. */
15636 r = lookup_name (DECL_NAME (t));
15637 if (r)
15638 {
15639 if (!VAR_P (r))
15640 {
15641 /* During error-recovery we may find a non-variable,
15642 even an OVERLOAD: just bail out and avoid ICEs and
15643 duplicate diagnostics (c++/62207). */
15644 gcc_assert (seen_error ());
15645 return error_mark_node;
15646 }
15647 if (!is_capture_proxy (r))
15648 {
15649 /* Make sure the one we found is the one we want. */
15650 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
15651 if (ctx != DECL_CONTEXT (r))
15652 r = NULL_TREE;
15653 }
15654 }
15655
15656 if (r)
15657 /* OK */;
15658 else
15659 {
15660 /* This can happen for a variable used in a
15661 late-specified return type of a local lambda, or for a
15662 local static or constant. Building a new VAR_DECL
15663 should be OK in all those cases. */
15664 r = tsubst_decl (t, args, complain);
15665 if (local_specializations)
15666 /* Avoid infinite recursion (79640). */
15667 register_local_specialization (r, t);
15668 if (decl_maybe_constant_var_p (r))
15669 {
15670 /* We can't call cp_finish_decl, so handle the
15671 initializer by hand. */
15672 tree init = tsubst_init (DECL_INITIAL (t), r, args,
15673 complain, in_decl);
15674 if (!processing_template_decl)
15675 init = maybe_constant_init (init);
15676 if (processing_template_decl
15677 ? potential_constant_expression (init)
15678 : reduced_constant_expression_p (init))
15679 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
15680 = TREE_CONSTANT (r) = true;
15681 DECL_INITIAL (r) = init;
15682 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
15683 TREE_TYPE (r)
15684 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
15685 complain, adc_variable_type);
15686 }
15687 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
15688 || decl_constant_var_p (r)
15689 || seen_error ());
15690 if (!processing_template_decl
15691 && !TREE_STATIC (r))
15692 r = process_outer_var_ref (r, complain);
15693 }
15694 /* Remember this for subsequent uses. */
15695 if (local_specializations)
15696 register_local_specialization (r, t);
15697 }
15698 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
15699 r = argument_pack_select_arg (r);
15700 }
15701 else
15702 r = t;
15703 if (!mark_used (r, complain))
15704 return error_mark_node;
15705 return r;
15706
15707 case NAMESPACE_DECL:
15708 return t;
15709
15710 case OVERLOAD:
15711 return t;
15712
15713 case BASELINK:
15714 return tsubst_baselink (t, current_nonlambda_class_type (),
15715 args, complain, in_decl);
15716
15717 case TEMPLATE_DECL:
15718 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
15719 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
15720 args, complain, in_decl);
15721 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
15722 return tsubst (t, args, complain, in_decl);
15723 else if (DECL_CLASS_SCOPE_P (t)
15724 && uses_template_parms (DECL_CONTEXT (t)))
15725 {
15726 /* Template template argument like the following example need
15727 special treatment:
15728
15729 template <template <class> class TT> struct C {};
15730 template <class T> struct D {
15731 template <class U> struct E {};
15732 C<E> c; // #1
15733 };
15734 D<int> d; // #2
15735
15736 We are processing the template argument `E' in #1 for
15737 the template instantiation #2. Originally, `E' is a
15738 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
15739 have to substitute this with one having context `D<int>'. */
15740
15741 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
15742 if (dependent_scope_p (context))
15743 {
15744 /* When rewriting a constructor into a deduction guide, a
15745 non-dependent name can become dependent, so memtmpl<args>
15746 becomes context::template memtmpl<args>. */
15747 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15748 return build_qualified_name (type, context, DECL_NAME (t),
15749 /*template*/true);
15750 }
15751 return lookup_field (context, DECL_NAME(t), 0, false);
15752 }
15753 else
15754 /* Ordinary template template argument. */
15755 return t;
15756
15757 case NON_LVALUE_EXPR:
15758 case VIEW_CONVERT_EXPR:
15759 {
15760 /* Handle location wrappers by substituting the wrapped node
15761 first, *then* reusing the resulting type. Doing the type
15762 first ensures that we handle template parameters and
15763 parameter pack expansions. */
15764 if (location_wrapper_p (t))
15765 {
15766 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args,
15767 complain, in_decl);
15768 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
15769 }
15770 tree op = TREE_OPERAND (t, 0);
15771 if (code == VIEW_CONVERT_EXPR
15772 && TREE_CODE (op) == TEMPLATE_PARM_INDEX)
15773 {
15774 /* Wrapper to make a C++20 template parameter object const. */
15775 op = tsubst_copy (op, args, complain, in_decl);
15776 if (TREE_CODE (op) == TEMPLATE_PARM_INDEX)
15777 {
15778 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15779 return build1 (code, type, op);
15780 }
15781 else
15782 {
15783 gcc_assert (CP_TYPE_CONST_P (TREE_TYPE (op)));
15784 return op;
15785 }
15786 }
15787 /* We shouldn't see any other uses of these in templates. */
15788 gcc_unreachable ();
15789 }
15790
15791 case CAST_EXPR:
15792 case REINTERPRET_CAST_EXPR:
15793 case CONST_CAST_EXPR:
15794 case STATIC_CAST_EXPR:
15795 case DYNAMIC_CAST_EXPR:
15796 case IMPLICIT_CONV_EXPR:
15797 case CONVERT_EXPR:
15798 case NOP_EXPR:
15799 {
15800 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15801 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15802 return build1 (code, type, op0);
15803 }
15804
15805 case SIZEOF_EXPR:
15806 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
15807 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
15808 {
15809 tree expanded, op = TREE_OPERAND (t, 0);
15810 int len = 0;
15811
15812 if (SIZEOF_EXPR_TYPE_P (t))
15813 op = TREE_TYPE (op);
15814
15815 ++cp_unevaluated_operand;
15816 ++c_inhibit_evaluation_warnings;
15817 /* We only want to compute the number of arguments. */
15818 if (PACK_EXPANSION_P (op))
15819 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
15820 else
15821 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
15822 args, complain, in_decl);
15823 --cp_unevaluated_operand;
15824 --c_inhibit_evaluation_warnings;
15825
15826 if (TREE_CODE (expanded) == TREE_VEC)
15827 {
15828 len = TREE_VEC_LENGTH (expanded);
15829 /* Set TREE_USED for the benefit of -Wunused. */
15830 for (int i = 0; i < len; i++)
15831 if (DECL_P (TREE_VEC_ELT (expanded, i)))
15832 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
15833 }
15834
15835 if (expanded == error_mark_node)
15836 return error_mark_node;
15837 else if (PACK_EXPANSION_P (expanded)
15838 || (TREE_CODE (expanded) == TREE_VEC
15839 && pack_expansion_args_count (expanded)))
15840
15841 {
15842 if (PACK_EXPANSION_P (expanded))
15843 /* OK. */;
15844 else if (TREE_VEC_LENGTH (expanded) == 1)
15845 expanded = TREE_VEC_ELT (expanded, 0);
15846 else
15847 expanded = make_argument_pack (expanded);
15848
15849 if (TYPE_P (expanded))
15850 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
15851 false,
15852 complain & tf_error);
15853 else
15854 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
15855 complain & tf_error);
15856 }
15857 else
15858 return build_int_cst (size_type_node, len);
15859 }
15860 if (SIZEOF_EXPR_TYPE_P (t))
15861 {
15862 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
15863 args, complain, in_decl);
15864 r = build1 (NOP_EXPR, r, error_mark_node);
15865 r = build1 (SIZEOF_EXPR,
15866 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
15867 SIZEOF_EXPR_TYPE_P (r) = 1;
15868 return r;
15869 }
15870 /* Fall through */
15871
15872 case INDIRECT_REF:
15873 case NEGATE_EXPR:
15874 case TRUTH_NOT_EXPR:
15875 case BIT_NOT_EXPR:
15876 case ADDR_EXPR:
15877 case UNARY_PLUS_EXPR: /* Unary + */
15878 case ALIGNOF_EXPR:
15879 case AT_ENCODE_EXPR:
15880 case ARROW_EXPR:
15881 case THROW_EXPR:
15882 case TYPEID_EXPR:
15883 case REALPART_EXPR:
15884 case IMAGPART_EXPR:
15885 case PAREN_EXPR:
15886 {
15887 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15888 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15889 r = build1 (code, type, op0);
15890 if (code == ALIGNOF_EXPR)
15891 ALIGNOF_EXPR_STD_P (r) = ALIGNOF_EXPR_STD_P (t);
15892 return r;
15893 }
15894
15895 case COMPONENT_REF:
15896 {
15897 tree object;
15898 tree name;
15899
15900 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15901 name = TREE_OPERAND (t, 1);
15902 if (TREE_CODE (name) == BIT_NOT_EXPR)
15903 {
15904 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15905 complain, in_decl);
15906 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15907 }
15908 else if (TREE_CODE (name) == SCOPE_REF
15909 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
15910 {
15911 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
15912 complain, in_decl);
15913 name = TREE_OPERAND (name, 1);
15914 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15915 complain, in_decl);
15916 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15917 name = build_qualified_name (/*type=*/NULL_TREE,
15918 base, name,
15919 /*template_p=*/false);
15920 }
15921 else if (BASELINK_P (name))
15922 name = tsubst_baselink (name,
15923 non_reference (TREE_TYPE (object)),
15924 args, complain,
15925 in_decl);
15926 else
15927 name = tsubst_copy (name, args, complain, in_decl);
15928 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
15929 }
15930
15931 case PLUS_EXPR:
15932 case MINUS_EXPR:
15933 case MULT_EXPR:
15934 case TRUNC_DIV_EXPR:
15935 case CEIL_DIV_EXPR:
15936 case FLOOR_DIV_EXPR:
15937 case ROUND_DIV_EXPR:
15938 case EXACT_DIV_EXPR:
15939 case BIT_AND_EXPR:
15940 case BIT_IOR_EXPR:
15941 case BIT_XOR_EXPR:
15942 case TRUNC_MOD_EXPR:
15943 case FLOOR_MOD_EXPR:
15944 case TRUTH_ANDIF_EXPR:
15945 case TRUTH_ORIF_EXPR:
15946 case TRUTH_AND_EXPR:
15947 case TRUTH_OR_EXPR:
15948 case RSHIFT_EXPR:
15949 case LSHIFT_EXPR:
15950 case RROTATE_EXPR:
15951 case LROTATE_EXPR:
15952 case EQ_EXPR:
15953 case NE_EXPR:
15954 case MAX_EXPR:
15955 case MIN_EXPR:
15956 case LE_EXPR:
15957 case GE_EXPR:
15958 case LT_EXPR:
15959 case GT_EXPR:
15960 case COMPOUND_EXPR:
15961 case DOTSTAR_EXPR:
15962 case MEMBER_REF:
15963 case PREDECREMENT_EXPR:
15964 case PREINCREMENT_EXPR:
15965 case POSTDECREMENT_EXPR:
15966 case POSTINCREMENT_EXPR:
15967 {
15968 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15969 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15970 return build_nt (code, op0, op1);
15971 }
15972
15973 case SCOPE_REF:
15974 {
15975 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15976 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15977 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
15978 QUALIFIED_NAME_IS_TEMPLATE (t));
15979 }
15980
15981 case ARRAY_REF:
15982 {
15983 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15984 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15985 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
15986 }
15987
15988 case CALL_EXPR:
15989 {
15990 int n = VL_EXP_OPERAND_LENGTH (t);
15991 tree result = build_vl_exp (CALL_EXPR, n);
15992 int i;
15993 for (i = 0; i < n; i++)
15994 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
15995 complain, in_decl);
15996 return result;
15997 }
15998
15999 case COND_EXPR:
16000 case MODOP_EXPR:
16001 case PSEUDO_DTOR_EXPR:
16002 case VEC_PERM_EXPR:
16003 {
16004 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16005 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16006 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
16007 r = build_nt (code, op0, op1, op2);
16008 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16009 return r;
16010 }
16011
16012 case NEW_EXPR:
16013 {
16014 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16015 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16016 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
16017 r = build_nt (code, op0, op1, op2);
16018 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
16019 return r;
16020 }
16021
16022 case DELETE_EXPR:
16023 {
16024 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16025 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16026 r = build_nt (code, op0, op1);
16027 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
16028 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
16029 return r;
16030 }
16031
16032 case TEMPLATE_ID_EXPR:
16033 {
16034 /* Substituted template arguments */
16035 tree fn = TREE_OPERAND (t, 0);
16036 tree targs = TREE_OPERAND (t, 1);
16037
16038 fn = tsubst_copy (fn, args, complain, in_decl);
16039 if (targs)
16040 targs = tsubst_template_args (targs, args, complain, in_decl);
16041
16042 return lookup_template_function (fn, targs);
16043 }
16044
16045 case TREE_LIST:
16046 {
16047 tree purpose, value, chain;
16048
16049 if (t == void_list_node)
16050 return t;
16051
16052 purpose = TREE_PURPOSE (t);
16053 if (purpose)
16054 purpose = tsubst_copy (purpose, args, complain, in_decl);
16055 value = TREE_VALUE (t);
16056 if (value)
16057 value = tsubst_copy (value, args, complain, in_decl);
16058 chain = TREE_CHAIN (t);
16059 if (chain && chain != void_type_node)
16060 chain = tsubst_copy (chain, args, complain, in_decl);
16061 if (purpose == TREE_PURPOSE (t)
16062 && value == TREE_VALUE (t)
16063 && chain == TREE_CHAIN (t))
16064 return t;
16065 return tree_cons (purpose, value, chain);
16066 }
16067
16068 case RECORD_TYPE:
16069 case UNION_TYPE:
16070 case ENUMERAL_TYPE:
16071 case INTEGER_TYPE:
16072 case TEMPLATE_TYPE_PARM:
16073 case TEMPLATE_TEMPLATE_PARM:
16074 case BOUND_TEMPLATE_TEMPLATE_PARM:
16075 case TEMPLATE_PARM_INDEX:
16076 case POINTER_TYPE:
16077 case REFERENCE_TYPE:
16078 case OFFSET_TYPE:
16079 case FUNCTION_TYPE:
16080 case METHOD_TYPE:
16081 case ARRAY_TYPE:
16082 case TYPENAME_TYPE:
16083 case UNBOUND_CLASS_TEMPLATE:
16084 case TYPEOF_TYPE:
16085 case DECLTYPE_TYPE:
16086 case TYPE_DECL:
16087 return tsubst (t, args, complain, in_decl);
16088
16089 case USING_DECL:
16090 t = DECL_NAME (t);
16091 /* Fall through. */
16092 case IDENTIFIER_NODE:
16093 if (IDENTIFIER_CONV_OP_P (t))
16094 {
16095 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16096 return make_conv_op_name (new_type);
16097 }
16098 else
16099 return t;
16100
16101 case CONSTRUCTOR:
16102 /* This is handled by tsubst_copy_and_build. */
16103 gcc_unreachable ();
16104
16105 case VA_ARG_EXPR:
16106 {
16107 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16108 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16109 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
16110 }
16111
16112 case CLEANUP_POINT_EXPR:
16113 /* We shouldn't have built any of these during initial template
16114 generation. Instead, they should be built during instantiation
16115 in response to the saved STMT_IS_FULL_EXPR_P setting. */
16116 gcc_unreachable ();
16117
16118 case OFFSET_REF:
16119 {
16120 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16121 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16122 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
16123 r = build2 (code, type, op0, op1);
16124 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
16125 if (!mark_used (TREE_OPERAND (r, 1), complain)
16126 && !(complain & tf_error))
16127 return error_mark_node;
16128 return r;
16129 }
16130
16131 case EXPR_PACK_EXPANSION:
16132 error ("invalid use of pack expansion expression");
16133 return error_mark_node;
16134
16135 case NONTYPE_ARGUMENT_PACK:
16136 error ("use %<...%> to expand argument pack");
16137 return error_mark_node;
16138
16139 case VOID_CST:
16140 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
16141 return t;
16142
16143 case INTEGER_CST:
16144 case REAL_CST:
16145 case STRING_CST:
16146 case COMPLEX_CST:
16147 {
16148 /* Instantiate any typedefs in the type. */
16149 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16150 r = fold_convert (type, t);
16151 gcc_assert (TREE_CODE (r) == code);
16152 return r;
16153 }
16154
16155 case PTRMEM_CST:
16156 /* These can sometimes show up in a partial instantiation, but never
16157 involve template parms. */
16158 gcc_assert (!uses_template_parms (t));
16159 return t;
16160
16161 case UNARY_LEFT_FOLD_EXPR:
16162 return tsubst_unary_left_fold (t, args, complain, in_decl);
16163 case UNARY_RIGHT_FOLD_EXPR:
16164 return tsubst_unary_right_fold (t, args, complain, in_decl);
16165 case BINARY_LEFT_FOLD_EXPR:
16166 return tsubst_binary_left_fold (t, args, complain, in_decl);
16167 case BINARY_RIGHT_FOLD_EXPR:
16168 return tsubst_binary_right_fold (t, args, complain, in_decl);
16169 case PREDICT_EXPR:
16170 return t;
16171
16172 case DEBUG_BEGIN_STMT:
16173 /* ??? There's no point in copying it for now, but maybe some
16174 day it will contain more information, such as a pointer back
16175 to the containing function, inlined copy or so. */
16176 return t;
16177
16178 default:
16179 /* We shouldn't get here, but keep going if !flag_checking. */
16180 if (flag_checking)
16181 gcc_unreachable ();
16182 return t;
16183 }
16184 }
16185
16186 /* Helper function for tsubst_omp_clauses, used for instantiation of
16187 OMP_CLAUSE_DECL of clauses. */
16188
16189 static tree
16190 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
16191 tree in_decl, tree *iterator_cache)
16192 {
16193 if (decl == NULL_TREE)
16194 return NULL_TREE;
16195
16196 /* Handle OpenMP iterators. */
16197 if (TREE_CODE (decl) == TREE_LIST
16198 && TREE_PURPOSE (decl)
16199 && TREE_CODE (TREE_PURPOSE (decl)) == TREE_VEC)
16200 {
16201 tree ret;
16202 if (iterator_cache[0] == TREE_PURPOSE (decl))
16203 ret = iterator_cache[1];
16204 else
16205 {
16206 tree *tp = &ret;
16207 begin_scope (sk_omp, NULL);
16208 for (tree it = TREE_PURPOSE (decl); it; it = TREE_CHAIN (it))
16209 {
16210 *tp = copy_node (it);
16211 TREE_VEC_ELT (*tp, 0)
16212 = tsubst_decl (TREE_VEC_ELT (it, 0), args, complain);
16213 TREE_VEC_ELT (*tp, 1)
16214 = tsubst_expr (TREE_VEC_ELT (it, 1), args, complain, in_decl,
16215 /*integral_constant_expression_p=*/false);
16216 TREE_VEC_ELT (*tp, 2)
16217 = tsubst_expr (TREE_VEC_ELT (it, 2), args, complain, in_decl,
16218 /*integral_constant_expression_p=*/false);
16219 TREE_VEC_ELT (*tp, 3)
16220 = tsubst_expr (TREE_VEC_ELT (it, 3), args, complain, in_decl,
16221 /*integral_constant_expression_p=*/false);
16222 TREE_CHAIN (*tp) = NULL_TREE;
16223 tp = &TREE_CHAIN (*tp);
16224 }
16225 TREE_VEC_ELT (ret, 5) = poplevel (1, 1, 0);
16226 iterator_cache[0] = TREE_PURPOSE (decl);
16227 iterator_cache[1] = ret;
16228 }
16229 return build_tree_list (ret, tsubst_omp_clause_decl (TREE_VALUE (decl),
16230 args, complain,
16231 in_decl, NULL));
16232 }
16233
16234 /* Handle an OpenMP array section represented as a TREE_LIST (or
16235 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
16236 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
16237 TREE_LIST. We can handle it exactly the same as an array section
16238 (purpose, value, and a chain), even though the nomenclature
16239 (low_bound, length, etc) is different. */
16240 if (TREE_CODE (decl) == TREE_LIST)
16241 {
16242 tree low_bound
16243 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
16244 /*integral_constant_expression_p=*/false);
16245 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
16246 /*integral_constant_expression_p=*/false);
16247 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
16248 in_decl, NULL);
16249 if (TREE_PURPOSE (decl) == low_bound
16250 && TREE_VALUE (decl) == length
16251 && TREE_CHAIN (decl) == chain)
16252 return decl;
16253 tree ret = tree_cons (low_bound, length, chain);
16254 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
16255 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
16256 return ret;
16257 }
16258 tree ret = tsubst_expr (decl, args, complain, in_decl,
16259 /*integral_constant_expression_p=*/false);
16260 /* Undo convert_from_reference tsubst_expr could have called. */
16261 if (decl
16262 && REFERENCE_REF_P (ret)
16263 && !REFERENCE_REF_P (decl))
16264 ret = TREE_OPERAND (ret, 0);
16265 return ret;
16266 }
16267
16268 /* Like tsubst_copy, but specifically for OpenMP clauses. */
16269
16270 static tree
16271 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
16272 tree args, tsubst_flags_t complain, tree in_decl)
16273 {
16274 tree new_clauses = NULL_TREE, nc, oc;
16275 tree linear_no_step = NULL_TREE;
16276 tree iterator_cache[2] = { NULL_TREE, NULL_TREE };
16277
16278 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
16279 {
16280 nc = copy_node (oc);
16281 OMP_CLAUSE_CHAIN (nc) = new_clauses;
16282 new_clauses = nc;
16283
16284 switch (OMP_CLAUSE_CODE (nc))
16285 {
16286 case OMP_CLAUSE_LASTPRIVATE:
16287 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
16288 {
16289 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
16290 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
16291 in_decl, /*integral_constant_expression_p=*/false);
16292 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
16293 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
16294 }
16295 /* FALLTHRU */
16296 case OMP_CLAUSE_PRIVATE:
16297 case OMP_CLAUSE_SHARED:
16298 case OMP_CLAUSE_FIRSTPRIVATE:
16299 case OMP_CLAUSE_COPYIN:
16300 case OMP_CLAUSE_COPYPRIVATE:
16301 case OMP_CLAUSE_UNIFORM:
16302 case OMP_CLAUSE_DEPEND:
16303 case OMP_CLAUSE_FROM:
16304 case OMP_CLAUSE_TO:
16305 case OMP_CLAUSE_MAP:
16306 case OMP_CLAUSE_NONTEMPORAL:
16307 case OMP_CLAUSE_USE_DEVICE_PTR:
16308 case OMP_CLAUSE_USE_DEVICE_ADDR:
16309 case OMP_CLAUSE_IS_DEVICE_PTR:
16310 case OMP_CLAUSE_INCLUSIVE:
16311 case OMP_CLAUSE_EXCLUSIVE:
16312 OMP_CLAUSE_DECL (nc)
16313 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16314 in_decl, iterator_cache);
16315 break;
16316 case OMP_CLAUSE_TILE:
16317 case OMP_CLAUSE_IF:
16318 case OMP_CLAUSE_NUM_THREADS:
16319 case OMP_CLAUSE_SCHEDULE:
16320 case OMP_CLAUSE_COLLAPSE:
16321 case OMP_CLAUSE_FINAL:
16322 case OMP_CLAUSE_DEVICE:
16323 case OMP_CLAUSE_DIST_SCHEDULE:
16324 case OMP_CLAUSE_NUM_TEAMS:
16325 case OMP_CLAUSE_THREAD_LIMIT:
16326 case OMP_CLAUSE_SAFELEN:
16327 case OMP_CLAUSE_SIMDLEN:
16328 case OMP_CLAUSE_NUM_TASKS:
16329 case OMP_CLAUSE_GRAINSIZE:
16330 case OMP_CLAUSE_PRIORITY:
16331 case OMP_CLAUSE_ORDERED:
16332 case OMP_CLAUSE_HINT:
16333 case OMP_CLAUSE_NUM_GANGS:
16334 case OMP_CLAUSE_NUM_WORKERS:
16335 case OMP_CLAUSE_VECTOR_LENGTH:
16336 case OMP_CLAUSE_WORKER:
16337 case OMP_CLAUSE_VECTOR:
16338 case OMP_CLAUSE_ASYNC:
16339 case OMP_CLAUSE_WAIT:
16340 OMP_CLAUSE_OPERAND (nc, 0)
16341 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
16342 in_decl, /*integral_constant_expression_p=*/false);
16343 break;
16344 case OMP_CLAUSE_REDUCTION:
16345 case OMP_CLAUSE_IN_REDUCTION:
16346 case OMP_CLAUSE_TASK_REDUCTION:
16347 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
16348 {
16349 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
16350 if (TREE_CODE (placeholder) == SCOPE_REF)
16351 {
16352 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
16353 complain, in_decl);
16354 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
16355 = build_qualified_name (NULL_TREE, scope,
16356 TREE_OPERAND (placeholder, 1),
16357 false);
16358 }
16359 else
16360 gcc_assert (identifier_p (placeholder));
16361 }
16362 OMP_CLAUSE_DECL (nc)
16363 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16364 in_decl, NULL);
16365 break;
16366 case OMP_CLAUSE_GANG:
16367 case OMP_CLAUSE_ALIGNED:
16368 OMP_CLAUSE_DECL (nc)
16369 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16370 in_decl, NULL);
16371 OMP_CLAUSE_OPERAND (nc, 1)
16372 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
16373 in_decl, /*integral_constant_expression_p=*/false);
16374 break;
16375 case OMP_CLAUSE_LINEAR:
16376 OMP_CLAUSE_DECL (nc)
16377 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
16378 in_decl, NULL);
16379 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
16380 {
16381 gcc_assert (!linear_no_step);
16382 linear_no_step = nc;
16383 }
16384 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
16385 OMP_CLAUSE_LINEAR_STEP (nc)
16386 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
16387 complain, in_decl, NULL);
16388 else
16389 OMP_CLAUSE_LINEAR_STEP (nc)
16390 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
16391 in_decl,
16392 /*integral_constant_expression_p=*/false);
16393 break;
16394 case OMP_CLAUSE_NOWAIT:
16395 case OMP_CLAUSE_DEFAULT:
16396 case OMP_CLAUSE_UNTIED:
16397 case OMP_CLAUSE_MERGEABLE:
16398 case OMP_CLAUSE_INBRANCH:
16399 case OMP_CLAUSE_NOTINBRANCH:
16400 case OMP_CLAUSE_PROC_BIND:
16401 case OMP_CLAUSE_FOR:
16402 case OMP_CLAUSE_PARALLEL:
16403 case OMP_CLAUSE_SECTIONS:
16404 case OMP_CLAUSE_TASKGROUP:
16405 case OMP_CLAUSE_NOGROUP:
16406 case OMP_CLAUSE_THREADS:
16407 case OMP_CLAUSE_SIMD:
16408 case OMP_CLAUSE_DEFAULTMAP:
16409 case OMP_CLAUSE_ORDER:
16410 case OMP_CLAUSE_BIND:
16411 case OMP_CLAUSE_INDEPENDENT:
16412 case OMP_CLAUSE_AUTO:
16413 case OMP_CLAUSE_SEQ:
16414 case OMP_CLAUSE_IF_PRESENT:
16415 case OMP_CLAUSE_FINALIZE:
16416 break;
16417 default:
16418 gcc_unreachable ();
16419 }
16420 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
16421 switch (OMP_CLAUSE_CODE (nc))
16422 {
16423 case OMP_CLAUSE_SHARED:
16424 case OMP_CLAUSE_PRIVATE:
16425 case OMP_CLAUSE_FIRSTPRIVATE:
16426 case OMP_CLAUSE_LASTPRIVATE:
16427 case OMP_CLAUSE_COPYPRIVATE:
16428 case OMP_CLAUSE_LINEAR:
16429 case OMP_CLAUSE_REDUCTION:
16430 case OMP_CLAUSE_IN_REDUCTION:
16431 case OMP_CLAUSE_TASK_REDUCTION:
16432 case OMP_CLAUSE_USE_DEVICE_PTR:
16433 case OMP_CLAUSE_USE_DEVICE_ADDR:
16434 case OMP_CLAUSE_IS_DEVICE_PTR:
16435 case OMP_CLAUSE_INCLUSIVE:
16436 case OMP_CLAUSE_EXCLUSIVE:
16437 /* tsubst_expr on SCOPE_REF results in returning
16438 finish_non_static_data_member result. Undo that here. */
16439 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
16440 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
16441 == IDENTIFIER_NODE))
16442 {
16443 tree t = OMP_CLAUSE_DECL (nc);
16444 tree v = t;
16445 while (v)
16446 switch (TREE_CODE (v))
16447 {
16448 case COMPONENT_REF:
16449 case MEM_REF:
16450 case INDIRECT_REF:
16451 CASE_CONVERT:
16452 case POINTER_PLUS_EXPR:
16453 v = TREE_OPERAND (v, 0);
16454 continue;
16455 case PARM_DECL:
16456 if (DECL_CONTEXT (v) == current_function_decl
16457 && DECL_ARTIFICIAL (v)
16458 && DECL_NAME (v) == this_identifier)
16459 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
16460 /* FALLTHRU */
16461 default:
16462 v = NULL_TREE;
16463 break;
16464 }
16465 }
16466 else if (VAR_P (OMP_CLAUSE_DECL (oc))
16467 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
16468 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
16469 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
16470 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
16471 {
16472 tree decl = OMP_CLAUSE_DECL (nc);
16473 if (VAR_P (decl))
16474 {
16475 retrofit_lang_decl (decl);
16476 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
16477 }
16478 }
16479 break;
16480 default:
16481 break;
16482 }
16483 }
16484
16485 new_clauses = nreverse (new_clauses);
16486 if (ort != C_ORT_OMP_DECLARE_SIMD)
16487 {
16488 new_clauses = finish_omp_clauses (new_clauses, ort);
16489 if (linear_no_step)
16490 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
16491 if (nc == linear_no_step)
16492 {
16493 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
16494 break;
16495 }
16496 }
16497 return new_clauses;
16498 }
16499
16500 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
16501
16502 static tree
16503 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
16504 tree in_decl)
16505 {
16506 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
16507
16508 tree purpose, value, chain;
16509
16510 if (t == NULL)
16511 return t;
16512
16513 if (TREE_CODE (t) != TREE_LIST)
16514 return tsubst_copy_and_build (t, args, complain, in_decl,
16515 /*function_p=*/false,
16516 /*integral_constant_expression_p=*/false);
16517
16518 if (t == void_list_node)
16519 return t;
16520
16521 purpose = TREE_PURPOSE (t);
16522 if (purpose)
16523 purpose = RECUR (purpose);
16524 value = TREE_VALUE (t);
16525 if (value)
16526 {
16527 if (TREE_CODE (value) != LABEL_DECL)
16528 value = RECUR (value);
16529 else
16530 {
16531 value = lookup_label (DECL_NAME (value));
16532 gcc_assert (TREE_CODE (value) == LABEL_DECL);
16533 TREE_USED (value) = 1;
16534 }
16535 }
16536 chain = TREE_CHAIN (t);
16537 if (chain && chain != void_type_node)
16538 chain = RECUR (chain);
16539 return tree_cons (purpose, value, chain);
16540 #undef RECUR
16541 }
16542
16543 /* Used to temporarily communicate the list of #pragma omp parallel
16544 clauses to #pragma omp for instantiation if they are combined
16545 together. */
16546
16547 static tree *omp_parallel_combined_clauses;
16548
16549 static tree tsubst_decomp_names (tree, tree, tree, tsubst_flags_t, tree,
16550 tree *, unsigned int *);
16551
16552 /* Substitute one OMP_FOR iterator. */
16553
16554 static bool
16555 tsubst_omp_for_iterator (tree t, int i, tree declv, tree &orig_declv,
16556 tree initv, tree condv, tree incrv, tree *clauses,
16557 tree args, tsubst_flags_t complain, tree in_decl,
16558 bool integral_constant_expression_p)
16559 {
16560 #define RECUR(NODE) \
16561 tsubst_expr ((NODE), args, complain, in_decl, \
16562 integral_constant_expression_p)
16563 tree decl, init, cond = NULL_TREE, incr = NULL_TREE;
16564 bool ret = false;
16565
16566 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
16567 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
16568
16569 decl = TREE_OPERAND (init, 0);
16570 init = TREE_OPERAND (init, 1);
16571 tree decl_expr = NULL_TREE;
16572 bool range_for = TREE_VEC_ELT (OMP_FOR_COND (t), i) == global_namespace;
16573 if (range_for)
16574 {
16575 bool decomp = false;
16576 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
16577 {
16578 tree v = DECL_VALUE_EXPR (decl);
16579 if (TREE_CODE (v) == ARRAY_REF
16580 && VAR_P (TREE_OPERAND (v, 0))
16581 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
16582 {
16583 tree decomp_first = NULL_TREE;
16584 unsigned decomp_cnt = 0;
16585 tree d = tsubst_decl (TREE_OPERAND (v, 0), args, complain);
16586 maybe_push_decl (d);
16587 d = tsubst_decomp_names (d, TREE_OPERAND (v, 0), args, complain,
16588 in_decl, &decomp_first, &decomp_cnt);
16589 decomp = true;
16590 if (d == error_mark_node)
16591 decl = error_mark_node;
16592 else
16593 for (unsigned int i = 0; i < decomp_cnt; i++)
16594 {
16595 if (!DECL_HAS_VALUE_EXPR_P (decomp_first))
16596 {
16597 tree v = build_nt (ARRAY_REF, d,
16598 size_int (decomp_cnt - i - 1),
16599 NULL_TREE, NULL_TREE);
16600 SET_DECL_VALUE_EXPR (decomp_first, v);
16601 DECL_HAS_VALUE_EXPR_P (decomp_first) = 1;
16602 }
16603 fit_decomposition_lang_decl (decomp_first, d);
16604 decomp_first = DECL_CHAIN (decomp_first);
16605 }
16606 }
16607 }
16608 decl = tsubst_decl (decl, args, complain);
16609 if (!decomp)
16610 maybe_push_decl (decl);
16611 }
16612 else if (init && TREE_CODE (init) == DECL_EXPR)
16613 {
16614 /* We need to jump through some hoops to handle declarations in the
16615 init-statement, since we might need to handle auto deduction,
16616 but we need to keep control of initialization. */
16617 decl_expr = init;
16618 init = DECL_INITIAL (DECL_EXPR_DECL (init));
16619 decl = tsubst_decl (decl, args, complain);
16620 }
16621 else
16622 {
16623 if (TREE_CODE (decl) == SCOPE_REF)
16624 {
16625 decl = RECUR (decl);
16626 if (TREE_CODE (decl) == COMPONENT_REF)
16627 {
16628 tree v = decl;
16629 while (v)
16630 switch (TREE_CODE (v))
16631 {
16632 case COMPONENT_REF:
16633 case MEM_REF:
16634 case INDIRECT_REF:
16635 CASE_CONVERT:
16636 case POINTER_PLUS_EXPR:
16637 v = TREE_OPERAND (v, 0);
16638 continue;
16639 case PARM_DECL:
16640 if (DECL_CONTEXT (v) == current_function_decl
16641 && DECL_ARTIFICIAL (v)
16642 && DECL_NAME (v) == this_identifier)
16643 {
16644 decl = TREE_OPERAND (decl, 1);
16645 decl = omp_privatize_field (decl, false);
16646 }
16647 /* FALLTHRU */
16648 default:
16649 v = NULL_TREE;
16650 break;
16651 }
16652 }
16653 }
16654 else
16655 decl = RECUR (decl);
16656 }
16657 init = RECUR (init);
16658
16659 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
16660 {
16661 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
16662 if (TREE_CODE (o) == TREE_LIST)
16663 TREE_VEC_ELT (orig_declv, i)
16664 = tree_cons (RECUR (TREE_PURPOSE (o)),
16665 RECUR (TREE_VALUE (o)),
16666 NULL_TREE);
16667 else
16668 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
16669 }
16670
16671 if (range_for)
16672 {
16673 tree this_pre_body = NULL_TREE;
16674 tree orig_init = NULL_TREE;
16675 tree orig_decl = NULL_TREE;
16676 cp_convert_omp_range_for (this_pre_body, NULL, decl, orig_decl, init,
16677 orig_init, cond, incr);
16678 if (orig_decl)
16679 {
16680 if (orig_declv == NULL_TREE)
16681 orig_declv = copy_node (declv);
16682 TREE_VEC_ELT (orig_declv, i) = orig_decl;
16683 ret = true;
16684 }
16685 else if (orig_declv)
16686 TREE_VEC_ELT (orig_declv, i) = decl;
16687 }
16688
16689 tree auto_node = type_uses_auto (TREE_TYPE (decl));
16690 if (!range_for && auto_node && init)
16691 TREE_TYPE (decl)
16692 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
16693
16694 gcc_assert (!type_dependent_expression_p (decl));
16695
16696 if (!CLASS_TYPE_P (TREE_TYPE (decl)) || range_for)
16697 {
16698 if (decl_expr)
16699 {
16700 /* Declare the variable, but don't let that initialize it. */
16701 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
16702 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
16703 RECUR (decl_expr);
16704 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
16705 }
16706
16707 if (!range_for)
16708 {
16709 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
16710 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
16711 if (TREE_CODE (incr) == MODIFY_EXPR)
16712 {
16713 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16714 tree rhs = RECUR (TREE_OPERAND (incr, 1));
16715 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
16716 NOP_EXPR, rhs, complain);
16717 }
16718 else
16719 incr = RECUR (incr);
16720 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
16721 TREE_VEC_ELT (orig_declv, i) = decl;
16722 }
16723 TREE_VEC_ELT (declv, i) = decl;
16724 TREE_VEC_ELT (initv, i) = init;
16725 TREE_VEC_ELT (condv, i) = cond;
16726 TREE_VEC_ELT (incrv, i) = incr;
16727 return ret;
16728 }
16729
16730 if (decl_expr)
16731 {
16732 /* Declare and initialize the variable. */
16733 RECUR (decl_expr);
16734 init = NULL_TREE;
16735 }
16736 else if (init)
16737 {
16738 tree *pc;
16739 int j;
16740 for (j = ((omp_parallel_combined_clauses == NULL
16741 || TREE_CODE (t) == OMP_LOOP) ? 1 : 0); j < 2; j++)
16742 {
16743 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
16744 {
16745 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
16746 && OMP_CLAUSE_DECL (*pc) == decl)
16747 break;
16748 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
16749 && OMP_CLAUSE_DECL (*pc) == decl)
16750 {
16751 if (j)
16752 break;
16753 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
16754 tree c = *pc;
16755 *pc = OMP_CLAUSE_CHAIN (c);
16756 OMP_CLAUSE_CHAIN (c) = *clauses;
16757 *clauses = c;
16758 }
16759 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
16760 && OMP_CLAUSE_DECL (*pc) == decl)
16761 {
16762 error ("iteration variable %qD should not be firstprivate",
16763 decl);
16764 *pc = OMP_CLAUSE_CHAIN (*pc);
16765 }
16766 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
16767 && OMP_CLAUSE_DECL (*pc) == decl)
16768 {
16769 error ("iteration variable %qD should not be reduction",
16770 decl);
16771 *pc = OMP_CLAUSE_CHAIN (*pc);
16772 }
16773 else
16774 pc = &OMP_CLAUSE_CHAIN (*pc);
16775 }
16776 if (*pc)
16777 break;
16778 }
16779 if (*pc == NULL_TREE)
16780 {
16781 tree c = build_omp_clause (input_location,
16782 TREE_CODE (t) == OMP_LOOP
16783 ? OMP_CLAUSE_LASTPRIVATE
16784 : OMP_CLAUSE_PRIVATE);
16785 OMP_CLAUSE_DECL (c) = decl;
16786 c = finish_omp_clauses (c, C_ORT_OMP);
16787 if (c)
16788 {
16789 OMP_CLAUSE_CHAIN (c) = *clauses;
16790 *clauses = c;
16791 }
16792 }
16793 }
16794 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
16795 if (COMPARISON_CLASS_P (cond))
16796 {
16797 tree op0 = RECUR (TREE_OPERAND (cond, 0));
16798 tree op1 = RECUR (TREE_OPERAND (cond, 1));
16799 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
16800 }
16801 else
16802 cond = RECUR (cond);
16803 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
16804 switch (TREE_CODE (incr))
16805 {
16806 case PREINCREMENT_EXPR:
16807 case PREDECREMENT_EXPR:
16808 case POSTINCREMENT_EXPR:
16809 case POSTDECREMENT_EXPR:
16810 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
16811 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
16812 break;
16813 case MODIFY_EXPR:
16814 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
16815 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
16816 {
16817 tree rhs = TREE_OPERAND (incr, 1);
16818 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16819 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
16820 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
16821 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16822 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
16823 rhs0, rhs1));
16824 }
16825 else
16826 incr = RECUR (incr);
16827 break;
16828 case MODOP_EXPR:
16829 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
16830 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
16831 {
16832 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16833 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16834 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
16835 TREE_TYPE (decl), lhs,
16836 RECUR (TREE_OPERAND (incr, 2))));
16837 }
16838 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
16839 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
16840 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
16841 {
16842 tree rhs = TREE_OPERAND (incr, 2);
16843 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16844 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
16845 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
16846 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16847 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
16848 rhs0, rhs1));
16849 }
16850 else
16851 incr = RECUR (incr);
16852 break;
16853 default:
16854 incr = RECUR (incr);
16855 break;
16856 }
16857
16858 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
16859 TREE_VEC_ELT (orig_declv, i) = decl;
16860 TREE_VEC_ELT (declv, i) = decl;
16861 TREE_VEC_ELT (initv, i) = init;
16862 TREE_VEC_ELT (condv, i) = cond;
16863 TREE_VEC_ELT (incrv, i) = incr;
16864 return false;
16865 #undef RECUR
16866 }
16867
16868 /* Helper function of tsubst_expr, find OMP_TEAMS inside
16869 of OMP_TARGET's body. */
16870
16871 static tree
16872 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
16873 {
16874 *walk_subtrees = 0;
16875 switch (TREE_CODE (*tp))
16876 {
16877 case OMP_TEAMS:
16878 return *tp;
16879 case BIND_EXPR:
16880 case STATEMENT_LIST:
16881 *walk_subtrees = 1;
16882 break;
16883 default:
16884 break;
16885 }
16886 return NULL_TREE;
16887 }
16888
16889 /* Helper function for tsubst_expr. For decomposition declaration
16890 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
16891 also the corresponding decls representing the identifiers
16892 of the decomposition declaration. Return DECL if successful
16893 or error_mark_node otherwise, set *FIRST to the first decl
16894 in the list chained through DECL_CHAIN and *CNT to the number
16895 of such decls. */
16896
16897 static tree
16898 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
16899 tsubst_flags_t complain, tree in_decl, tree *first,
16900 unsigned int *cnt)
16901 {
16902 tree decl2, decl3, prev = decl;
16903 *cnt = 0;
16904 gcc_assert (DECL_NAME (decl) == NULL_TREE);
16905 for (decl2 = DECL_CHAIN (pattern_decl);
16906 decl2
16907 && VAR_P (decl2)
16908 && DECL_DECOMPOSITION_P (decl2)
16909 && DECL_NAME (decl2);
16910 decl2 = DECL_CHAIN (decl2))
16911 {
16912 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
16913 {
16914 gcc_assert (errorcount);
16915 return error_mark_node;
16916 }
16917 (*cnt)++;
16918 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
16919 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
16920 tree v = DECL_VALUE_EXPR (decl2);
16921 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
16922 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
16923 decl3 = tsubst (decl2, args, complain, in_decl);
16924 SET_DECL_VALUE_EXPR (decl2, v);
16925 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
16926 if (VAR_P (decl3))
16927 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
16928 else
16929 {
16930 gcc_assert (errorcount);
16931 decl = error_mark_node;
16932 continue;
16933 }
16934 maybe_push_decl (decl3);
16935 if (error_operand_p (decl3))
16936 decl = error_mark_node;
16937 else if (decl != error_mark_node
16938 && DECL_CHAIN (decl3) != prev
16939 && decl != prev)
16940 {
16941 gcc_assert (errorcount);
16942 decl = error_mark_node;
16943 }
16944 else
16945 prev = decl3;
16946 }
16947 *first = prev;
16948 return decl;
16949 }
16950
16951 /* Return the proper local_specialization for init-capture pack DECL. */
16952
16953 static tree
16954 lookup_init_capture_pack (tree decl)
16955 {
16956 /* We handle normal pack captures by forwarding to the specialization of the
16957 captured parameter. We can't do that for pack init-captures; we need them
16958 to have their own local_specialization. We created the individual
16959 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
16960 when we process the DECL_EXPR for the pack init-capture in the template.
16961 So, how do we find them? We don't know the capture proxy pack when
16962 building the individual resulting proxies, and we don't know the
16963 individual proxies when instantiating the pack. What we have in common is
16964 the FIELD_DECL.
16965
16966 So...when we instantiate the FIELD_DECL, we stick the result in
16967 local_specializations. Then at the DECL_EXPR we look up that result, see
16968 how many elements it has, synthesize the names, and look them up. */
16969
16970 tree cname = DECL_NAME (decl);
16971 tree val = DECL_VALUE_EXPR (decl);
16972 tree field = TREE_OPERAND (val, 1);
16973 gcc_assert (TREE_CODE (field) == FIELD_DECL);
16974 tree fpack = retrieve_local_specialization (field);
16975 if (fpack == error_mark_node)
16976 return error_mark_node;
16977
16978 int len = 1;
16979 tree vec = NULL_TREE;
16980 tree r = NULL_TREE;
16981 if (TREE_CODE (fpack) == TREE_VEC)
16982 {
16983 len = TREE_VEC_LENGTH (fpack);
16984 vec = make_tree_vec (len);
16985 r = make_node (NONTYPE_ARGUMENT_PACK);
16986 SET_ARGUMENT_PACK_ARGS (r, vec);
16987 }
16988 for (int i = 0; i < len; ++i)
16989 {
16990 tree ename = vec ? make_ith_pack_parameter_name (cname, i) : cname;
16991 tree elt = lookup_name_real (ename, 0, 0, true, 0, LOOKUP_NORMAL);
16992 if (vec)
16993 TREE_VEC_ELT (vec, i) = elt;
16994 else
16995 r = elt;
16996 }
16997 return r;
16998 }
16999
17000 /* Like tsubst_copy for expressions, etc. but also does semantic
17001 processing. */
17002
17003 tree
17004 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
17005 bool integral_constant_expression_p)
17006 {
17007 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
17008 #define RECUR(NODE) \
17009 tsubst_expr ((NODE), args, complain, in_decl, \
17010 integral_constant_expression_p)
17011
17012 tree stmt, tmp;
17013 tree r;
17014 location_t loc;
17015
17016 if (t == NULL_TREE || t == error_mark_node)
17017 return t;
17018
17019 loc = input_location;
17020 if (location_t eloc = cp_expr_location (t))
17021 input_location = eloc;
17022 if (STATEMENT_CODE_P (TREE_CODE (t)))
17023 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
17024
17025 switch (TREE_CODE (t))
17026 {
17027 case STATEMENT_LIST:
17028 {
17029 tree_stmt_iterator i;
17030 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
17031 RECUR (tsi_stmt (i));
17032 break;
17033 }
17034
17035 case CTOR_INITIALIZER:
17036 finish_mem_initializers (tsubst_initializer_list
17037 (TREE_OPERAND (t, 0), args));
17038 break;
17039
17040 case RETURN_EXPR:
17041 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
17042 break;
17043
17044 case EXPR_STMT:
17045 tmp = RECUR (EXPR_STMT_EXPR (t));
17046 if (EXPR_STMT_STMT_EXPR_RESULT (t))
17047 finish_stmt_expr_expr (tmp, cur_stmt_expr);
17048 else
17049 finish_expr_stmt (tmp);
17050 break;
17051
17052 case USING_STMT:
17053 finish_using_directive (USING_STMT_NAMESPACE (t), /*attribs=*/NULL_TREE);
17054 break;
17055
17056 case DECL_EXPR:
17057 {
17058 tree decl, pattern_decl;
17059 tree init;
17060
17061 pattern_decl = decl = DECL_EXPR_DECL (t);
17062 if (TREE_CODE (decl) == LABEL_DECL)
17063 finish_label_decl (DECL_NAME (decl));
17064 else if (TREE_CODE (decl) == USING_DECL)
17065 {
17066 tree scope = USING_DECL_SCOPE (decl);
17067 tree name = DECL_NAME (decl);
17068
17069 scope = tsubst (scope, args, complain, in_decl);
17070 finish_nonmember_using_decl (scope, name);
17071 }
17072 else if (is_capture_proxy (decl)
17073 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
17074 {
17075 /* We're in tsubst_lambda_expr, we've already inserted a new
17076 capture proxy, so look it up and register it. */
17077 tree inst;
17078 if (!DECL_PACK_P (decl))
17079 {
17080 inst = lookup_name_real (DECL_NAME (decl), /*prefer_type*/0,
17081 /*nonclass*/1, /*block_p=*/true,
17082 /*ns_only*/0, LOOKUP_HIDDEN);
17083 gcc_assert (inst != decl && is_capture_proxy (inst));
17084 }
17085 else if (is_normal_capture_proxy (decl))
17086 {
17087 inst = (retrieve_local_specialization
17088 (DECL_CAPTURED_VARIABLE (decl)));
17089 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK);
17090 }
17091 else
17092 inst = lookup_init_capture_pack (decl);
17093
17094 register_local_specialization (inst, decl);
17095 break;
17096 }
17097 else if (DECL_PRETTY_FUNCTION_P (decl))
17098 decl = make_fname_decl (DECL_SOURCE_LOCATION (decl),
17099 DECL_NAME (decl),
17100 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
17101 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
17102 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
17103 /* Don't copy the old closure; we'll create a new one in
17104 tsubst_lambda_expr. */
17105 break;
17106 else
17107 {
17108 init = DECL_INITIAL (decl);
17109 /* The following tsubst call will clear the DECL_TEMPLATE_INFO
17110 for local variables, so save if DECL was declared constinit. */
17111 const bool constinit_p
17112 = (VAR_P (decl)
17113 && DECL_LANG_SPECIFIC (decl)
17114 && DECL_TEMPLATE_INFO (decl)
17115 && TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (decl)));
17116 decl = tsubst (decl, args, complain, in_decl);
17117 if (decl != error_mark_node)
17118 {
17119 /* By marking the declaration as instantiated, we avoid
17120 trying to instantiate it. Since instantiate_decl can't
17121 handle local variables, and since we've already done
17122 all that needs to be done, that's the right thing to
17123 do. */
17124 if (VAR_P (decl))
17125 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
17126 if (VAR_P (decl) && !DECL_NAME (decl)
17127 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
17128 /* Anonymous aggregates are a special case. */
17129 finish_anon_union (decl);
17130 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
17131 {
17132 DECL_CONTEXT (decl) = current_function_decl;
17133 if (DECL_NAME (decl) == this_identifier)
17134 {
17135 tree lam = DECL_CONTEXT (current_function_decl);
17136 lam = CLASSTYPE_LAMBDA_EXPR (lam);
17137 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
17138 }
17139 insert_capture_proxy (decl);
17140 }
17141 else if (DECL_IMPLICIT_TYPEDEF_P (t))
17142 /* We already did a pushtag. */;
17143 else if (TREE_CODE (decl) == FUNCTION_DECL
17144 && DECL_OMP_DECLARE_REDUCTION_P (decl)
17145 && DECL_FUNCTION_SCOPE_P (pattern_decl))
17146 {
17147 DECL_CONTEXT (decl) = NULL_TREE;
17148 pushdecl (decl);
17149 DECL_CONTEXT (decl) = current_function_decl;
17150 cp_check_omp_declare_reduction (decl);
17151 }
17152 else
17153 {
17154 bool const_init = false;
17155 unsigned int cnt = 0;
17156 tree first = NULL_TREE, ndecl = error_mark_node;
17157 maybe_push_decl (decl);
17158
17159 if (VAR_P (decl)
17160 && DECL_DECOMPOSITION_P (decl)
17161 && TREE_TYPE (pattern_decl) != error_mark_node)
17162 ndecl = tsubst_decomp_names (decl, pattern_decl, args,
17163 complain, in_decl, &first,
17164 &cnt);
17165
17166 init = tsubst_init (init, decl, args, complain, in_decl);
17167
17168 if (VAR_P (decl))
17169 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
17170 (pattern_decl));
17171
17172 if (ndecl != error_mark_node)
17173 cp_maybe_mangle_decomp (ndecl, first, cnt);
17174
17175 cp_finish_decl (decl, init, const_init, NULL_TREE,
17176 constinit_p ? LOOKUP_CONSTINIT : 0);
17177
17178 if (ndecl != error_mark_node)
17179 cp_finish_decomp (ndecl, first, cnt);
17180 }
17181 }
17182 }
17183
17184 break;
17185 }
17186
17187 case FOR_STMT:
17188 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
17189 RECUR (FOR_INIT_STMT (t));
17190 finish_init_stmt (stmt);
17191 tmp = RECUR (FOR_COND (t));
17192 finish_for_cond (tmp, stmt, false, 0);
17193 tmp = RECUR (FOR_EXPR (t));
17194 finish_for_expr (tmp, stmt);
17195 {
17196 bool prev = note_iteration_stmt_body_start ();
17197 RECUR (FOR_BODY (t));
17198 note_iteration_stmt_body_end (prev);
17199 }
17200 finish_for_stmt (stmt);
17201 break;
17202
17203 case RANGE_FOR_STMT:
17204 {
17205 /* Construct another range_for, if this is not a final
17206 substitution (for inside inside a generic lambda of a
17207 template). Otherwise convert to a regular for. */
17208 tree decl, expr;
17209 stmt = (processing_template_decl
17210 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
17211 : begin_for_stmt (NULL_TREE, NULL_TREE));
17212 RECUR (RANGE_FOR_INIT_STMT (t));
17213 decl = RANGE_FOR_DECL (t);
17214 decl = tsubst (decl, args, complain, in_decl);
17215 maybe_push_decl (decl);
17216 expr = RECUR (RANGE_FOR_EXPR (t));
17217
17218 tree decomp_first = NULL_TREE;
17219 unsigned decomp_cnt = 0;
17220 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
17221 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
17222 complain, in_decl,
17223 &decomp_first, &decomp_cnt);
17224
17225 if (processing_template_decl)
17226 {
17227 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
17228 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
17229 finish_range_for_decl (stmt, decl, expr);
17230 if (decomp_first && decl != error_mark_node)
17231 cp_finish_decomp (decl, decomp_first, decomp_cnt);
17232 }
17233 else
17234 {
17235 unsigned short unroll = (RANGE_FOR_UNROLL (t)
17236 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
17237 stmt = cp_convert_range_for (stmt, decl, expr,
17238 decomp_first, decomp_cnt,
17239 RANGE_FOR_IVDEP (t), unroll);
17240 }
17241
17242 bool prev = note_iteration_stmt_body_start ();
17243 RECUR (RANGE_FOR_BODY (t));
17244 note_iteration_stmt_body_end (prev);
17245 finish_for_stmt (stmt);
17246 }
17247 break;
17248
17249 case WHILE_STMT:
17250 stmt = begin_while_stmt ();
17251 tmp = RECUR (WHILE_COND (t));
17252 finish_while_stmt_cond (tmp, stmt, false, 0);
17253 {
17254 bool prev = note_iteration_stmt_body_start ();
17255 RECUR (WHILE_BODY (t));
17256 note_iteration_stmt_body_end (prev);
17257 }
17258 finish_while_stmt (stmt);
17259 break;
17260
17261 case DO_STMT:
17262 stmt = begin_do_stmt ();
17263 {
17264 bool prev = note_iteration_stmt_body_start ();
17265 RECUR (DO_BODY (t));
17266 note_iteration_stmt_body_end (prev);
17267 }
17268 finish_do_body (stmt);
17269 tmp = RECUR (DO_COND (t));
17270 finish_do_stmt (tmp, stmt, false, 0);
17271 break;
17272
17273 case IF_STMT:
17274 stmt = begin_if_stmt ();
17275 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
17276 if (IF_STMT_CONSTEXPR_P (t))
17277 args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args);
17278 tmp = RECUR (IF_COND (t));
17279 tmp = finish_if_stmt_cond (tmp, stmt);
17280 if (IF_STMT_CONSTEXPR_P (t)
17281 && instantiation_dependent_expression_p (tmp))
17282 {
17283 /* We're partially instantiating a generic lambda, but the condition
17284 of the constexpr if is still dependent. Don't substitute into the
17285 branches now, just remember the template arguments. */
17286 do_poplevel (IF_SCOPE (stmt));
17287 IF_COND (stmt) = IF_COND (t);
17288 THEN_CLAUSE (stmt) = THEN_CLAUSE (t);
17289 ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t);
17290 IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (t, args, complain);
17291 add_stmt (stmt);
17292 break;
17293 }
17294 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
17295 /* Don't instantiate the THEN_CLAUSE. */;
17296 else
17297 {
17298 tree folded = fold_non_dependent_expr (tmp, complain);
17299 bool inhibit = integer_zerop (folded);
17300 if (inhibit)
17301 ++c_inhibit_evaluation_warnings;
17302 RECUR (THEN_CLAUSE (t));
17303 if (inhibit)
17304 --c_inhibit_evaluation_warnings;
17305 }
17306 finish_then_clause (stmt);
17307
17308 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
17309 /* Don't instantiate the ELSE_CLAUSE. */;
17310 else if (ELSE_CLAUSE (t))
17311 {
17312 tree folded = fold_non_dependent_expr (tmp, complain);
17313 bool inhibit = integer_nonzerop (folded);
17314 begin_else_clause (stmt);
17315 if (inhibit)
17316 ++c_inhibit_evaluation_warnings;
17317 RECUR (ELSE_CLAUSE (t));
17318 if (inhibit)
17319 --c_inhibit_evaluation_warnings;
17320 finish_else_clause (stmt);
17321 }
17322
17323 finish_if_stmt (stmt);
17324 break;
17325
17326 case BIND_EXPR:
17327 if (BIND_EXPR_BODY_BLOCK (t))
17328 stmt = begin_function_body ();
17329 else
17330 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
17331 ? BCS_TRY_BLOCK : 0);
17332
17333 RECUR (BIND_EXPR_BODY (t));
17334
17335 if (BIND_EXPR_BODY_BLOCK (t))
17336 finish_function_body (stmt);
17337 else
17338 finish_compound_stmt (stmt);
17339 break;
17340
17341 case BREAK_STMT:
17342 finish_break_stmt ();
17343 break;
17344
17345 case CONTINUE_STMT:
17346 finish_continue_stmt ();
17347 break;
17348
17349 case SWITCH_STMT:
17350 stmt = begin_switch_stmt ();
17351 tmp = RECUR (SWITCH_STMT_COND (t));
17352 finish_switch_cond (tmp, stmt);
17353 RECUR (SWITCH_STMT_BODY (t));
17354 finish_switch_stmt (stmt);
17355 break;
17356
17357 case CASE_LABEL_EXPR:
17358 {
17359 tree decl = CASE_LABEL (t);
17360 tree low = RECUR (CASE_LOW (t));
17361 tree high = RECUR (CASE_HIGH (t));
17362 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
17363 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
17364 {
17365 tree label = CASE_LABEL (l);
17366 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
17367 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
17368 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
17369 }
17370 }
17371 break;
17372
17373 case LABEL_EXPR:
17374 {
17375 tree decl = LABEL_EXPR_LABEL (t);
17376 tree label;
17377
17378 label = finish_label_stmt (DECL_NAME (decl));
17379 if (TREE_CODE (label) == LABEL_DECL)
17380 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
17381 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
17382 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
17383 }
17384 break;
17385
17386 case GOTO_EXPR:
17387 tmp = GOTO_DESTINATION (t);
17388 if (TREE_CODE (tmp) != LABEL_DECL)
17389 /* Computed goto's must be tsubst'd into. On the other hand,
17390 non-computed gotos must not be; the identifier in question
17391 will have no binding. */
17392 tmp = RECUR (tmp);
17393 else
17394 tmp = DECL_NAME (tmp);
17395 finish_goto_stmt (tmp);
17396 break;
17397
17398 case ASM_EXPR:
17399 {
17400 tree string = RECUR (ASM_STRING (t));
17401 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
17402 complain, in_decl);
17403 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
17404 complain, in_decl);
17405 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
17406 complain, in_decl);
17407 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
17408 complain, in_decl);
17409 tmp = finish_asm_stmt (EXPR_LOCATION (t), ASM_VOLATILE_P (t), string,
17410 outputs, inputs, clobbers, labels,
17411 ASM_INLINE_P (t));
17412 tree asm_expr = tmp;
17413 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
17414 asm_expr = TREE_OPERAND (asm_expr, 0);
17415 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
17416 }
17417 break;
17418
17419 case TRY_BLOCK:
17420 if (CLEANUP_P (t))
17421 {
17422 stmt = begin_try_block ();
17423 RECUR (TRY_STMTS (t));
17424 finish_cleanup_try_block (stmt);
17425 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
17426 }
17427 else
17428 {
17429 tree compound_stmt = NULL_TREE;
17430
17431 if (FN_TRY_BLOCK_P (t))
17432 stmt = begin_function_try_block (&compound_stmt);
17433 else
17434 stmt = begin_try_block ();
17435
17436 RECUR (TRY_STMTS (t));
17437
17438 if (FN_TRY_BLOCK_P (t))
17439 finish_function_try_block (stmt);
17440 else
17441 finish_try_block (stmt);
17442
17443 RECUR (TRY_HANDLERS (t));
17444 if (FN_TRY_BLOCK_P (t))
17445 finish_function_handler_sequence (stmt, compound_stmt);
17446 else
17447 finish_handler_sequence (stmt);
17448 }
17449 break;
17450
17451 case HANDLER:
17452 {
17453 tree decl = HANDLER_PARMS (t);
17454
17455 if (decl)
17456 {
17457 decl = tsubst (decl, args, complain, in_decl);
17458 /* Prevent instantiate_decl from trying to instantiate
17459 this variable. We've already done all that needs to be
17460 done. */
17461 if (decl != error_mark_node)
17462 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
17463 }
17464 stmt = begin_handler ();
17465 finish_handler_parms (decl, stmt);
17466 RECUR (HANDLER_BODY (t));
17467 finish_handler (stmt);
17468 }
17469 break;
17470
17471 case TAG_DEFN:
17472 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
17473 if (CLASS_TYPE_P (tmp))
17474 {
17475 /* Local classes are not independent templates; they are
17476 instantiated along with their containing function. And this
17477 way we don't have to deal with pushing out of one local class
17478 to instantiate a member of another local class. */
17479 /* Closures are handled by the LAMBDA_EXPR. */
17480 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
17481 complete_type (tmp);
17482 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
17483 if ((VAR_P (fld)
17484 || (TREE_CODE (fld) == FUNCTION_DECL
17485 && !DECL_ARTIFICIAL (fld)))
17486 && DECL_TEMPLATE_INSTANTIATION (fld))
17487 instantiate_decl (fld, /*defer_ok=*/false,
17488 /*expl_inst_class=*/false);
17489 }
17490 break;
17491
17492 case STATIC_ASSERT:
17493 {
17494 tree condition;
17495
17496 ++c_inhibit_evaluation_warnings;
17497 condition =
17498 tsubst_expr (STATIC_ASSERT_CONDITION (t),
17499 args,
17500 complain, in_decl,
17501 /*integral_constant_expression_p=*/true);
17502 --c_inhibit_evaluation_warnings;
17503
17504 finish_static_assert (condition,
17505 STATIC_ASSERT_MESSAGE (t),
17506 STATIC_ASSERT_SOURCE_LOCATION (t),
17507 /*member_p=*/false);
17508 }
17509 break;
17510
17511 case OACC_KERNELS:
17512 case OACC_PARALLEL:
17513 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
17514 in_decl);
17515 stmt = begin_omp_parallel ();
17516 RECUR (OMP_BODY (t));
17517 finish_omp_construct (TREE_CODE (t), stmt, tmp);
17518 break;
17519
17520 case OMP_PARALLEL:
17521 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
17522 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
17523 complain, in_decl);
17524 if (OMP_PARALLEL_COMBINED (t))
17525 omp_parallel_combined_clauses = &tmp;
17526 stmt = begin_omp_parallel ();
17527 RECUR (OMP_PARALLEL_BODY (t));
17528 gcc_assert (omp_parallel_combined_clauses == NULL);
17529 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
17530 = OMP_PARALLEL_COMBINED (t);
17531 pop_omp_privatization_clauses (r);
17532 break;
17533
17534 case OMP_TASK:
17535 if (OMP_TASK_BODY (t) == NULL_TREE)
17536 {
17537 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
17538 complain, in_decl);
17539 t = copy_node (t);
17540 OMP_TASK_CLAUSES (t) = tmp;
17541 add_stmt (t);
17542 break;
17543 }
17544 r = push_omp_privatization_clauses (false);
17545 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
17546 complain, in_decl);
17547 stmt = begin_omp_task ();
17548 RECUR (OMP_TASK_BODY (t));
17549 finish_omp_task (tmp, stmt);
17550 pop_omp_privatization_clauses (r);
17551 break;
17552
17553 case OMP_FOR:
17554 case OMP_LOOP:
17555 case OMP_SIMD:
17556 case OMP_DISTRIBUTE:
17557 case OMP_TASKLOOP:
17558 case OACC_LOOP:
17559 {
17560 tree clauses, body, pre_body;
17561 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
17562 tree orig_declv = NULL_TREE;
17563 tree incrv = NULL_TREE;
17564 enum c_omp_region_type ort = C_ORT_OMP;
17565 bool any_range_for = false;
17566 int i;
17567
17568 if (TREE_CODE (t) == OACC_LOOP)
17569 ort = C_ORT_ACC;
17570
17571 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
17572 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
17573 in_decl);
17574 if (OMP_FOR_INIT (t) != NULL_TREE)
17575 {
17576 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17577 if (OMP_FOR_ORIG_DECLS (t))
17578 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17579 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17580 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17581 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
17582 }
17583
17584 keep_next_level (true);
17585 stmt = begin_omp_structured_block ();
17586
17587 pre_body = push_stmt_list ();
17588 RECUR (OMP_FOR_PRE_BODY (t));
17589 pre_body = pop_stmt_list (pre_body);
17590
17591 if (OMP_FOR_INIT (t) != NULL_TREE)
17592 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
17593 any_range_for
17594 |= tsubst_omp_for_iterator (t, i, declv, orig_declv, initv,
17595 condv, incrv, &clauses, args,
17596 complain, in_decl,
17597 integral_constant_expression_p);
17598 omp_parallel_combined_clauses = NULL;
17599
17600 if (any_range_for)
17601 {
17602 gcc_assert (orig_declv);
17603 body = begin_omp_structured_block ();
17604 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
17605 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i)
17606 && TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST
17607 && TREE_CHAIN (TREE_VEC_ELT (orig_declv, i)))
17608 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
17609 TREE_VEC_ELT (declv, i));
17610 }
17611 else
17612 body = push_stmt_list ();
17613 RECUR (OMP_FOR_BODY (t));
17614 if (any_range_for)
17615 body = finish_omp_structured_block (body);
17616 else
17617 body = pop_stmt_list (body);
17618
17619 if (OMP_FOR_INIT (t) != NULL_TREE)
17620 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
17621 orig_declv, initv, condv, incrv, body, pre_body,
17622 NULL, clauses);
17623 else
17624 {
17625 t = make_node (TREE_CODE (t));
17626 TREE_TYPE (t) = void_type_node;
17627 OMP_FOR_BODY (t) = body;
17628 OMP_FOR_PRE_BODY (t) = pre_body;
17629 OMP_FOR_CLAUSES (t) = clauses;
17630 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
17631 add_stmt (t);
17632 }
17633
17634 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt),
17635 t));
17636 pop_omp_privatization_clauses (r);
17637 }
17638 break;
17639
17640 case OMP_SECTIONS:
17641 omp_parallel_combined_clauses = NULL;
17642 /* FALLTHRU */
17643 case OMP_SINGLE:
17644 case OMP_TEAMS:
17645 case OMP_CRITICAL:
17646 case OMP_TASKGROUP:
17647 case OMP_SCAN:
17648 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
17649 && OMP_TEAMS_COMBINED (t));
17650 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
17651 in_decl);
17652 if (TREE_CODE (t) == OMP_TEAMS)
17653 {
17654 keep_next_level (true);
17655 stmt = begin_omp_structured_block ();
17656 RECUR (OMP_BODY (t));
17657 stmt = finish_omp_structured_block (stmt);
17658 }
17659 else
17660 {
17661 stmt = push_stmt_list ();
17662 RECUR (OMP_BODY (t));
17663 stmt = pop_stmt_list (stmt);
17664 }
17665
17666 t = copy_node (t);
17667 OMP_BODY (t) = stmt;
17668 OMP_CLAUSES (t) = tmp;
17669 add_stmt (t);
17670 pop_omp_privatization_clauses (r);
17671 break;
17672
17673 case OMP_DEPOBJ:
17674 r = RECUR (OMP_DEPOBJ_DEPOBJ (t));
17675 if (OMP_DEPOBJ_CLAUSES (t) && OMP_DEPOBJ_CLAUSES (t) != error_mark_node)
17676 {
17677 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
17678 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t)) == OMP_CLAUSE)
17679 {
17680 tmp = tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t), C_ORT_OMP,
17681 args, complain, in_decl);
17682 if (tmp == NULL_TREE)
17683 tmp = error_mark_node;
17684 }
17685 else
17686 {
17687 kind = (enum omp_clause_depend_kind)
17688 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t));
17689 tmp = NULL_TREE;
17690 }
17691 finish_omp_depobj (EXPR_LOCATION (t), r, kind, tmp);
17692 }
17693 else
17694 finish_omp_depobj (EXPR_LOCATION (t), r,
17695 OMP_CLAUSE_DEPEND_SOURCE,
17696 OMP_DEPOBJ_CLAUSES (t));
17697 break;
17698
17699 case OACC_DATA:
17700 case OMP_TARGET_DATA:
17701 case OMP_TARGET:
17702 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
17703 ? C_ORT_ACC : C_ORT_OMP, args, complain,
17704 in_decl);
17705 keep_next_level (true);
17706 stmt = begin_omp_structured_block ();
17707
17708 RECUR (OMP_BODY (t));
17709 stmt = finish_omp_structured_block (stmt);
17710
17711 t = copy_node (t);
17712 OMP_BODY (t) = stmt;
17713 OMP_CLAUSES (t) = tmp;
17714 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
17715 {
17716 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
17717 if (teams)
17718 {
17719 /* For combined target teams, ensure the num_teams and
17720 thread_limit clause expressions are evaluated on the host,
17721 before entering the target construct. */
17722 tree c;
17723 for (c = OMP_TEAMS_CLAUSES (teams);
17724 c; c = OMP_CLAUSE_CHAIN (c))
17725 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
17726 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
17727 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
17728 {
17729 tree expr = OMP_CLAUSE_OPERAND (c, 0);
17730 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
17731 if (expr == error_mark_node)
17732 continue;
17733 tmp = TARGET_EXPR_SLOT (expr);
17734 add_stmt (expr);
17735 OMP_CLAUSE_OPERAND (c, 0) = expr;
17736 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
17737 OMP_CLAUSE_FIRSTPRIVATE);
17738 OMP_CLAUSE_DECL (tc) = tmp;
17739 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
17740 OMP_TARGET_CLAUSES (t) = tc;
17741 }
17742 }
17743 }
17744 add_stmt (t);
17745 break;
17746
17747 case OACC_DECLARE:
17748 t = copy_node (t);
17749 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
17750 complain, in_decl);
17751 OACC_DECLARE_CLAUSES (t) = tmp;
17752 add_stmt (t);
17753 break;
17754
17755 case OMP_TARGET_UPDATE:
17756 case OMP_TARGET_ENTER_DATA:
17757 case OMP_TARGET_EXIT_DATA:
17758 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
17759 complain, in_decl);
17760 t = copy_node (t);
17761 OMP_STANDALONE_CLAUSES (t) = tmp;
17762 add_stmt (t);
17763 break;
17764
17765 case OACC_ENTER_DATA:
17766 case OACC_EXIT_DATA:
17767 case OACC_UPDATE:
17768 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
17769 complain, in_decl);
17770 t = copy_node (t);
17771 OMP_STANDALONE_CLAUSES (t) = tmp;
17772 add_stmt (t);
17773 break;
17774
17775 case OMP_ORDERED:
17776 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
17777 complain, in_decl);
17778 stmt = push_stmt_list ();
17779 RECUR (OMP_BODY (t));
17780 stmt = pop_stmt_list (stmt);
17781
17782 t = copy_node (t);
17783 OMP_BODY (t) = stmt;
17784 OMP_ORDERED_CLAUSES (t) = tmp;
17785 add_stmt (t);
17786 break;
17787
17788 case OMP_SECTION:
17789 case OMP_MASTER:
17790 stmt = push_stmt_list ();
17791 RECUR (OMP_BODY (t));
17792 stmt = pop_stmt_list (stmt);
17793
17794 t = copy_node (t);
17795 OMP_BODY (t) = stmt;
17796 add_stmt (t);
17797 break;
17798
17799 case OMP_ATOMIC:
17800 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
17801 tmp = NULL_TREE;
17802 if (TREE_CODE (TREE_OPERAND (t, 0)) == OMP_CLAUSE)
17803 tmp = tsubst_omp_clauses (TREE_OPERAND (t, 0), C_ORT_OMP, args,
17804 complain, in_decl);
17805 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
17806 {
17807 tree op1 = TREE_OPERAND (t, 1);
17808 tree rhs1 = NULL_TREE;
17809 tree lhs, rhs;
17810 if (TREE_CODE (op1) == COMPOUND_EXPR)
17811 {
17812 rhs1 = RECUR (TREE_OPERAND (op1, 0));
17813 op1 = TREE_OPERAND (op1, 1);
17814 }
17815 lhs = RECUR (TREE_OPERAND (op1, 0));
17816 rhs = RECUR (TREE_OPERAND (op1, 1));
17817 finish_omp_atomic (EXPR_LOCATION (t), OMP_ATOMIC, TREE_CODE (op1),
17818 lhs, rhs, NULL_TREE, NULL_TREE, rhs1, tmp,
17819 OMP_ATOMIC_MEMORY_ORDER (t));
17820 }
17821 else
17822 {
17823 tree op1 = TREE_OPERAND (t, 1);
17824 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
17825 tree rhs1 = NULL_TREE;
17826 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
17827 enum tree_code opcode = NOP_EXPR;
17828 if (code == OMP_ATOMIC_READ)
17829 {
17830 v = RECUR (TREE_OPERAND (op1, 0));
17831 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
17832 }
17833 else if (code == OMP_ATOMIC_CAPTURE_OLD
17834 || code == OMP_ATOMIC_CAPTURE_NEW)
17835 {
17836 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
17837 v = RECUR (TREE_OPERAND (op1, 0));
17838 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
17839 if (TREE_CODE (op11) == COMPOUND_EXPR)
17840 {
17841 rhs1 = RECUR (TREE_OPERAND (op11, 0));
17842 op11 = TREE_OPERAND (op11, 1);
17843 }
17844 lhs = RECUR (TREE_OPERAND (op11, 0));
17845 rhs = RECUR (TREE_OPERAND (op11, 1));
17846 opcode = TREE_CODE (op11);
17847 if (opcode == MODIFY_EXPR)
17848 opcode = NOP_EXPR;
17849 }
17850 else
17851 {
17852 code = OMP_ATOMIC;
17853 lhs = RECUR (TREE_OPERAND (op1, 0));
17854 rhs = RECUR (TREE_OPERAND (op1, 1));
17855 }
17856 finish_omp_atomic (EXPR_LOCATION (t), code, opcode, lhs, rhs, v,
17857 lhs1, rhs1, tmp, OMP_ATOMIC_MEMORY_ORDER (t));
17858 }
17859 break;
17860
17861 case TRANSACTION_EXPR:
17862 {
17863 int flags = 0;
17864 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
17865 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
17866
17867 if (TRANSACTION_EXPR_IS_STMT (t))
17868 {
17869 tree body = TRANSACTION_EXPR_BODY (t);
17870 tree noex = NULL_TREE;
17871 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
17872 {
17873 noex = MUST_NOT_THROW_COND (body);
17874 if (noex == NULL_TREE)
17875 noex = boolean_true_node;
17876 body = TREE_OPERAND (body, 0);
17877 }
17878 stmt = begin_transaction_stmt (input_location, NULL, flags);
17879 RECUR (body);
17880 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
17881 }
17882 else
17883 {
17884 stmt = build_transaction_expr (EXPR_LOCATION (t),
17885 RECUR (TRANSACTION_EXPR_BODY (t)),
17886 flags, NULL_TREE);
17887 RETURN (stmt);
17888 }
17889 }
17890 break;
17891
17892 case MUST_NOT_THROW_EXPR:
17893 {
17894 tree op0 = RECUR (TREE_OPERAND (t, 0));
17895 tree cond = RECUR (MUST_NOT_THROW_COND (t));
17896 RETURN (build_must_not_throw_expr (op0, cond));
17897 }
17898
17899 case EXPR_PACK_EXPANSION:
17900 error ("invalid use of pack expansion expression");
17901 RETURN (error_mark_node);
17902
17903 case NONTYPE_ARGUMENT_PACK:
17904 error ("use %<...%> to expand argument pack");
17905 RETURN (error_mark_node);
17906
17907 case COMPOUND_EXPR:
17908 tmp = RECUR (TREE_OPERAND (t, 0));
17909 if (tmp == NULL_TREE)
17910 /* If the first operand was a statement, we're done with it. */
17911 RETURN (RECUR (TREE_OPERAND (t, 1)));
17912 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
17913 RECUR (TREE_OPERAND (t, 1)),
17914 complain));
17915
17916 case ANNOTATE_EXPR:
17917 tmp = RECUR (TREE_OPERAND (t, 0));
17918 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
17919 TREE_TYPE (tmp), tmp,
17920 RECUR (TREE_OPERAND (t, 1)),
17921 RECUR (TREE_OPERAND (t, 2))));
17922
17923 case PREDICT_EXPR:
17924 RETURN (add_stmt (copy_node (t)));
17925
17926 default:
17927 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
17928
17929 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
17930 /*function_p=*/false,
17931 integral_constant_expression_p));
17932 }
17933
17934 RETURN (NULL_TREE);
17935 out:
17936 input_location = loc;
17937 return r;
17938 #undef RECUR
17939 #undef RETURN
17940 }
17941
17942 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
17943 function. For description of the body see comment above
17944 cp_parser_omp_declare_reduction_exprs. */
17945
17946 static void
17947 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
17948 {
17949 if (t == NULL_TREE || t == error_mark_node)
17950 return;
17951
17952 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
17953
17954 tree_stmt_iterator tsi;
17955 int i;
17956 tree stmts[7];
17957 memset (stmts, 0, sizeof stmts);
17958 for (i = 0, tsi = tsi_start (t);
17959 i < 7 && !tsi_end_p (tsi);
17960 i++, tsi_next (&tsi))
17961 stmts[i] = tsi_stmt (tsi);
17962 gcc_assert (tsi_end_p (tsi));
17963
17964 if (i >= 3)
17965 {
17966 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
17967 && TREE_CODE (stmts[1]) == DECL_EXPR);
17968 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
17969 args, complain, in_decl);
17970 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
17971 args, complain, in_decl);
17972 DECL_CONTEXT (omp_out) = current_function_decl;
17973 DECL_CONTEXT (omp_in) = current_function_decl;
17974 keep_next_level (true);
17975 tree block = begin_omp_structured_block ();
17976 tsubst_expr (stmts[2], args, complain, in_decl, false);
17977 block = finish_omp_structured_block (block);
17978 block = maybe_cleanup_point_expr_void (block);
17979 add_decl_expr (omp_out);
17980 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
17981 TREE_NO_WARNING (omp_out) = 1;
17982 add_decl_expr (omp_in);
17983 finish_expr_stmt (block);
17984 }
17985 if (i >= 6)
17986 {
17987 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
17988 && TREE_CODE (stmts[4]) == DECL_EXPR);
17989 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
17990 args, complain, in_decl);
17991 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
17992 args, complain, in_decl);
17993 DECL_CONTEXT (omp_priv) = current_function_decl;
17994 DECL_CONTEXT (omp_orig) = current_function_decl;
17995 keep_next_level (true);
17996 tree block = begin_omp_structured_block ();
17997 tsubst_expr (stmts[5], args, complain, in_decl, false);
17998 block = finish_omp_structured_block (block);
17999 block = maybe_cleanup_point_expr_void (block);
18000 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
18001 add_decl_expr (omp_priv);
18002 add_decl_expr (omp_orig);
18003 finish_expr_stmt (block);
18004 if (i == 7)
18005 add_decl_expr (omp_orig);
18006 }
18007 }
18008
18009 /* T is a postfix-expression that is not being used in a function
18010 call. Return the substituted version of T. */
18011
18012 static tree
18013 tsubst_non_call_postfix_expression (tree t, tree args,
18014 tsubst_flags_t complain,
18015 tree in_decl)
18016 {
18017 if (TREE_CODE (t) == SCOPE_REF)
18018 t = tsubst_qualified_id (t, args, complain, in_decl,
18019 /*done=*/false, /*address_p=*/false);
18020 else
18021 t = tsubst_copy_and_build (t, args, complain, in_decl,
18022 /*function_p=*/false,
18023 /*integral_constant_expression_p=*/false);
18024
18025 return t;
18026 }
18027
18028 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
18029 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
18030 dependent init-capture. */
18031
18032 static void
18033 prepend_one_capture (tree field, tree init, tree &list,
18034 tsubst_flags_t complain)
18035 {
18036 if (tree auto_node = type_uses_auto (TREE_TYPE (field)))
18037 {
18038 tree type = NULL_TREE;
18039 if (!init)
18040 {
18041 if (complain & tf_error)
18042 error ("empty initializer in lambda init-capture");
18043 init = error_mark_node;
18044 }
18045 else if (TREE_CODE (init) == TREE_LIST)
18046 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
18047 if (!type)
18048 type = do_auto_deduction (TREE_TYPE (field), init, auto_node, complain);
18049 TREE_TYPE (field) = type;
18050 cp_apply_type_quals_to_decl (cp_type_quals (type), field);
18051 }
18052 list = tree_cons (field, init, list);
18053 }
18054
18055 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
18056 instantiation context. Instantiating a pack expansion containing a lambda
18057 might result in multiple lambdas all based on the same lambda in the
18058 template. */
18059
18060 tree
18061 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
18062 {
18063 tree oldfn = lambda_function (t);
18064 in_decl = oldfn;
18065
18066 tree r = build_lambda_expr ();
18067
18068 LAMBDA_EXPR_LOCATION (r)
18069 = LAMBDA_EXPR_LOCATION (t);
18070 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
18071 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
18072 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
18073 LAMBDA_EXPR_INSTANTIATED (r) = true;
18074
18075 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
18076 /* A lambda in a default argument outside a class gets no
18077 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
18078 tsubst_default_argument calls start_lambda_scope, so we need to
18079 specifically ignore it here, and use the global scope. */
18080 record_null_lambda_scope (r);
18081 else
18082 record_lambda_scope (r);
18083
18084 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
18085 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
18086
18087 vec<tree,va_gc>* field_packs = NULL;
18088
18089 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
18090 cap = TREE_CHAIN (cap))
18091 {
18092 tree ofield = TREE_PURPOSE (cap);
18093 if (PACK_EXPANSION_P (ofield))
18094 ofield = PACK_EXPANSION_PATTERN (ofield);
18095 tree field = tsubst_decl (ofield, args, complain);
18096
18097 if (DECL_PACK_P (ofield) && !DECL_NORMAL_CAPTURE_P (ofield))
18098 {
18099 /* Remember these for when we've pushed local_specializations. */
18100 vec_safe_push (field_packs, ofield);
18101 vec_safe_push (field_packs, field);
18102 }
18103
18104 if (field == error_mark_node)
18105 return error_mark_node;
18106
18107 tree init = TREE_VALUE (cap);
18108 if (PACK_EXPANSION_P (init))
18109 init = tsubst_pack_expansion (init, args, complain, in_decl);
18110 else
18111 init = tsubst_copy_and_build (init, args, complain, in_decl,
18112 /*fn*/false, /*constexpr*/false);
18113
18114 if (TREE_CODE (field) == TREE_VEC)
18115 {
18116 int len = TREE_VEC_LENGTH (field);
18117 gcc_assert (TREE_CODE (init) == TREE_VEC
18118 && TREE_VEC_LENGTH (init) == len);
18119 for (int i = 0; i < len; ++i)
18120 prepend_one_capture (TREE_VEC_ELT (field, i),
18121 TREE_VEC_ELT (init, i),
18122 LAMBDA_EXPR_CAPTURE_LIST (r),
18123 complain);
18124 }
18125 else
18126 {
18127 prepend_one_capture (field, init, LAMBDA_EXPR_CAPTURE_LIST (r),
18128 complain);
18129
18130 if (id_equal (DECL_NAME (field), "__this"))
18131 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
18132 }
18133 }
18134
18135 tree type = begin_lambda_type (r);
18136 if (type == error_mark_node)
18137 return error_mark_node;
18138
18139 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
18140 determine_visibility (TYPE_NAME (type));
18141
18142 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
18143
18144 tree oldtmpl = (generic_lambda_fn_p (oldfn)
18145 ? DECL_TI_TEMPLATE (oldfn)
18146 : NULL_TREE);
18147
18148 tree fntype = static_fn_type (oldfn);
18149 if (oldtmpl)
18150 ++processing_template_decl;
18151 fntype = tsubst (fntype, args, complain, in_decl);
18152 if (oldtmpl)
18153 --processing_template_decl;
18154
18155 if (fntype == error_mark_node)
18156 r = error_mark_node;
18157 else
18158 {
18159 /* The body of a lambda-expression is not a subexpression of the
18160 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
18161 which would be skipped if cp_unevaluated_operand. */
18162 cp_evaluated ev;
18163
18164 /* Fix the type of 'this'. */
18165 fntype = build_memfn_type (fntype, type,
18166 type_memfn_quals (fntype),
18167 type_memfn_rqual (fntype));
18168 tree fn, tmpl;
18169 if (oldtmpl)
18170 {
18171 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
18172 fn = DECL_TEMPLATE_RESULT (tmpl);
18173 finish_member_declaration (tmpl);
18174 }
18175 else
18176 {
18177 tmpl = NULL_TREE;
18178 fn = tsubst_function_decl (oldfn, args, complain, fntype);
18179 finish_member_declaration (fn);
18180 }
18181
18182 /* Let finish_function set this. */
18183 DECL_DECLARED_CONSTEXPR_P (fn) = false;
18184
18185 bool nested = cfun;
18186 if (nested)
18187 push_function_context ();
18188 else
18189 /* Still increment function_depth so that we don't GC in the
18190 middle of an expression. */
18191 ++function_depth;
18192
18193 local_specialization_stack s (lss_copy);
18194
18195 tree body = start_lambda_function (fn, r);
18196
18197 /* Now record them for lookup_init_capture_pack. */
18198 int fplen = vec_safe_length (field_packs);
18199 for (int i = 0; i < fplen; )
18200 {
18201 tree pack = (*field_packs)[i++];
18202 tree inst = (*field_packs)[i++];
18203 register_local_specialization (inst, pack);
18204 }
18205 release_tree_vector (field_packs);
18206
18207 register_parameter_specializations (oldfn, fn);
18208
18209 if (oldtmpl)
18210 {
18211 /* We might not partially instantiate some parts of the function, so
18212 copy these flags from the original template. */
18213 language_function *ol = DECL_STRUCT_FUNCTION (oldfn)->language;
18214 current_function_returns_value = ol->returns_value;
18215 current_function_returns_null = ol->returns_null;
18216 current_function_returns_abnormally = ol->returns_abnormally;
18217 current_function_infinite_loop = ol->infinite_loop;
18218 }
18219
18220 /* [temp.deduct] A lambda-expression appearing in a function type or a
18221 template parameter is not considered part of the immediate context for
18222 the purposes of template argument deduction. */
18223 complain = tf_warning_or_error;
18224
18225 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
18226 /*constexpr*/false);
18227
18228 finish_lambda_function (body);
18229
18230 if (nested)
18231 pop_function_context ();
18232 else
18233 --function_depth;
18234
18235 /* The capture list was built up in reverse order; fix that now. */
18236 LAMBDA_EXPR_CAPTURE_LIST (r)
18237 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
18238
18239 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
18240
18241 maybe_add_lambda_conv_op (type);
18242 }
18243
18244 finish_struct (type, /*attr*/NULL_TREE);
18245
18246 insert_pending_capture_proxies ();
18247
18248 return r;
18249 }
18250
18251 /* Like tsubst but deals with expressions and performs semantic
18252 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
18253
18254 tree
18255 tsubst_copy_and_build (tree t,
18256 tree args,
18257 tsubst_flags_t complain,
18258 tree in_decl,
18259 bool function_p,
18260 bool integral_constant_expression_p)
18261 {
18262 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
18263 #define RECUR(NODE) \
18264 tsubst_copy_and_build (NODE, args, complain, in_decl, \
18265 /*function_p=*/false, \
18266 integral_constant_expression_p)
18267
18268 tree retval, op1;
18269 location_t loc;
18270
18271 if (t == NULL_TREE || t == error_mark_node)
18272 return t;
18273
18274 loc = input_location;
18275 if (location_t eloc = cp_expr_location (t))
18276 input_location = eloc;
18277
18278 /* N3276 decltype magic only applies to calls at the top level or on the
18279 right side of a comma. */
18280 tsubst_flags_t decltype_flag = (complain & tf_decltype);
18281 complain &= ~tf_decltype;
18282
18283 switch (TREE_CODE (t))
18284 {
18285 case USING_DECL:
18286 t = DECL_NAME (t);
18287 /* Fall through. */
18288 case IDENTIFIER_NODE:
18289 {
18290 tree decl;
18291 cp_id_kind idk;
18292 bool non_integral_constant_expression_p;
18293 const char *error_msg;
18294
18295 if (IDENTIFIER_CONV_OP_P (t))
18296 {
18297 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18298 t = make_conv_op_name (new_type);
18299 }
18300
18301 /* Look up the name. */
18302 decl = lookup_name (t);
18303
18304 /* By convention, expressions use ERROR_MARK_NODE to indicate
18305 failure, not NULL_TREE. */
18306 if (decl == NULL_TREE)
18307 decl = error_mark_node;
18308
18309 decl = finish_id_expression (t, decl, NULL_TREE,
18310 &idk,
18311 integral_constant_expression_p,
18312 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
18313 &non_integral_constant_expression_p,
18314 /*template_p=*/false,
18315 /*done=*/true,
18316 /*address_p=*/false,
18317 /*template_arg_p=*/false,
18318 &error_msg,
18319 input_location);
18320 if (error_msg)
18321 error (error_msg);
18322 if (!function_p && identifier_p (decl))
18323 {
18324 if (complain & tf_error)
18325 unqualified_name_lookup_error (decl);
18326 decl = error_mark_node;
18327 }
18328 RETURN (decl);
18329 }
18330
18331 case TEMPLATE_ID_EXPR:
18332 {
18333 tree object;
18334 tree templ = RECUR (TREE_OPERAND (t, 0));
18335 tree targs = TREE_OPERAND (t, 1);
18336
18337 if (targs)
18338 targs = tsubst_template_args (targs, args, complain, in_decl);
18339 if (targs == error_mark_node)
18340 RETURN (error_mark_node);
18341
18342 if (TREE_CODE (templ) == SCOPE_REF)
18343 {
18344 tree name = TREE_OPERAND (templ, 1);
18345 tree tid = lookup_template_function (name, targs);
18346 TREE_OPERAND (templ, 1) = tid;
18347 RETURN (templ);
18348 }
18349
18350 if (variable_template_p (templ))
18351 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
18352
18353 if (TREE_CODE (templ) == COMPONENT_REF)
18354 {
18355 object = TREE_OPERAND (templ, 0);
18356 templ = TREE_OPERAND (templ, 1);
18357 }
18358 else
18359 object = NULL_TREE;
18360 templ = lookup_template_function (templ, targs);
18361
18362 if (object)
18363 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
18364 object, templ, NULL_TREE));
18365 else
18366 RETURN (baselink_for_fns (templ));
18367 }
18368
18369 case INDIRECT_REF:
18370 {
18371 tree r = RECUR (TREE_OPERAND (t, 0));
18372
18373 if (REFERENCE_REF_P (t))
18374 {
18375 /* A type conversion to reference type will be enclosed in
18376 such an indirect ref, but the substitution of the cast
18377 will have also added such an indirect ref. */
18378 r = convert_from_reference (r);
18379 }
18380 else
18381 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
18382 complain|decltype_flag);
18383
18384 if (REF_PARENTHESIZED_P (t))
18385 r = force_paren_expr (r);
18386
18387 RETURN (r);
18388 }
18389
18390 case NOP_EXPR:
18391 {
18392 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18393 tree op0 = RECUR (TREE_OPERAND (t, 0));
18394 RETURN (build_nop (type, op0));
18395 }
18396
18397 case IMPLICIT_CONV_EXPR:
18398 {
18399 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18400 tree expr = RECUR (TREE_OPERAND (t, 0));
18401 if (dependent_type_p (type) || type_dependent_expression_p (expr))
18402 {
18403 retval = copy_node (t);
18404 TREE_TYPE (retval) = type;
18405 TREE_OPERAND (retval, 0) = expr;
18406 RETURN (retval);
18407 }
18408 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
18409 /* We'll pass this to convert_nontype_argument again, we don't need
18410 to actually perform any conversion here. */
18411 RETURN (expr);
18412 int flags = LOOKUP_IMPLICIT;
18413 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
18414 flags = LOOKUP_NORMAL;
18415 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t))
18416 flags |= LOOKUP_NO_NARROWING;
18417 RETURN (perform_implicit_conversion_flags (type, expr, complain,
18418 flags));
18419 }
18420
18421 case CONVERT_EXPR:
18422 {
18423 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18424 tree op0 = RECUR (TREE_OPERAND (t, 0));
18425 if (op0 == error_mark_node)
18426 RETURN (error_mark_node);
18427 RETURN (build1 (CONVERT_EXPR, type, op0));
18428 }
18429
18430 case CAST_EXPR:
18431 case REINTERPRET_CAST_EXPR:
18432 case CONST_CAST_EXPR:
18433 case DYNAMIC_CAST_EXPR:
18434 case STATIC_CAST_EXPR:
18435 {
18436 tree type;
18437 tree op, r = NULL_TREE;
18438
18439 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18440 if (integral_constant_expression_p
18441 && !cast_valid_in_integral_constant_expression_p (type))
18442 {
18443 if (complain & tf_error)
18444 error ("a cast to a type other than an integral or "
18445 "enumeration type cannot appear in a constant-expression");
18446 RETURN (error_mark_node);
18447 }
18448
18449 op = RECUR (TREE_OPERAND (t, 0));
18450
18451 warning_sentinel s(warn_useless_cast);
18452 warning_sentinel s2(warn_ignored_qualifiers);
18453 switch (TREE_CODE (t))
18454 {
18455 case CAST_EXPR:
18456 r = build_functional_cast (type, op, complain);
18457 break;
18458 case REINTERPRET_CAST_EXPR:
18459 r = build_reinterpret_cast (type, op, complain);
18460 break;
18461 case CONST_CAST_EXPR:
18462 r = build_const_cast (type, op, complain);
18463 break;
18464 case DYNAMIC_CAST_EXPR:
18465 r = build_dynamic_cast (type, op, complain);
18466 break;
18467 case STATIC_CAST_EXPR:
18468 r = build_static_cast (type, op, complain);
18469 break;
18470 default:
18471 gcc_unreachable ();
18472 }
18473
18474 RETURN (r);
18475 }
18476
18477 case POSTDECREMENT_EXPR:
18478 case POSTINCREMENT_EXPR:
18479 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18480 args, complain, in_decl);
18481 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
18482 complain|decltype_flag));
18483
18484 case PREDECREMENT_EXPR:
18485 case PREINCREMENT_EXPR:
18486 case NEGATE_EXPR:
18487 case BIT_NOT_EXPR:
18488 case ABS_EXPR:
18489 case TRUTH_NOT_EXPR:
18490 case UNARY_PLUS_EXPR: /* Unary + */
18491 case REALPART_EXPR:
18492 case IMAGPART_EXPR:
18493 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
18494 RECUR (TREE_OPERAND (t, 0)),
18495 complain|decltype_flag));
18496
18497 case FIX_TRUNC_EXPR:
18498 gcc_unreachable ();
18499
18500 case ADDR_EXPR:
18501 op1 = TREE_OPERAND (t, 0);
18502 if (TREE_CODE (op1) == LABEL_DECL)
18503 RETURN (finish_label_address_expr (DECL_NAME (op1),
18504 EXPR_LOCATION (op1)));
18505 if (TREE_CODE (op1) == SCOPE_REF)
18506 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
18507 /*done=*/true, /*address_p=*/true);
18508 else
18509 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
18510 in_decl);
18511 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
18512 complain|decltype_flag));
18513
18514 case PLUS_EXPR:
18515 case MINUS_EXPR:
18516 case MULT_EXPR:
18517 case TRUNC_DIV_EXPR:
18518 case CEIL_DIV_EXPR:
18519 case FLOOR_DIV_EXPR:
18520 case ROUND_DIV_EXPR:
18521 case EXACT_DIV_EXPR:
18522 case BIT_AND_EXPR:
18523 case BIT_IOR_EXPR:
18524 case BIT_XOR_EXPR:
18525 case TRUNC_MOD_EXPR:
18526 case FLOOR_MOD_EXPR:
18527 case TRUTH_ANDIF_EXPR:
18528 case TRUTH_ORIF_EXPR:
18529 case TRUTH_AND_EXPR:
18530 case TRUTH_OR_EXPR:
18531 case RSHIFT_EXPR:
18532 case LSHIFT_EXPR:
18533 case RROTATE_EXPR:
18534 case LROTATE_EXPR:
18535 case EQ_EXPR:
18536 case NE_EXPR:
18537 case MAX_EXPR:
18538 case MIN_EXPR:
18539 case LE_EXPR:
18540 case GE_EXPR:
18541 case LT_EXPR:
18542 case GT_EXPR:
18543 case MEMBER_REF:
18544 case DOTSTAR_EXPR:
18545 {
18546 warning_sentinel s1(warn_type_limits);
18547 warning_sentinel s2(warn_div_by_zero);
18548 warning_sentinel s3(warn_logical_op);
18549 warning_sentinel s4(warn_tautological_compare);
18550 tree op0 = RECUR (TREE_OPERAND (t, 0));
18551 tree op1 = RECUR (TREE_OPERAND (t, 1));
18552 tree r = build_x_binary_op
18553 (input_location, TREE_CODE (t),
18554 op0,
18555 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
18556 ? ERROR_MARK
18557 : TREE_CODE (TREE_OPERAND (t, 0))),
18558 op1,
18559 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
18560 ? ERROR_MARK
18561 : TREE_CODE (TREE_OPERAND (t, 1))),
18562 /*overload=*/NULL,
18563 complain|decltype_flag);
18564 if (EXPR_P (r) && TREE_NO_WARNING (t))
18565 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
18566
18567 RETURN (r);
18568 }
18569
18570 case POINTER_PLUS_EXPR:
18571 {
18572 tree op0 = RECUR (TREE_OPERAND (t, 0));
18573 tree op1 = RECUR (TREE_OPERAND (t, 1));
18574 RETURN (fold_build_pointer_plus (op0, op1));
18575 }
18576
18577 case SCOPE_REF:
18578 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
18579 /*address_p=*/false));
18580 case ARRAY_REF:
18581 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18582 args, complain, in_decl);
18583 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
18584 RECUR (TREE_OPERAND (t, 1)),
18585 complain|decltype_flag));
18586
18587 case SIZEOF_EXPR:
18588 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
18589 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
18590 RETURN (tsubst_copy (t, args, complain, in_decl));
18591 /* Fall through */
18592
18593 case ALIGNOF_EXPR:
18594 {
18595 tree r;
18596
18597 op1 = TREE_OPERAND (t, 0);
18598 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
18599 op1 = TREE_TYPE (op1);
18600 bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR
18601 && ALIGNOF_EXPR_STD_P (t));
18602 if (!args)
18603 {
18604 /* When there are no ARGS, we are trying to evaluate a
18605 non-dependent expression from the parser. Trying to do
18606 the substitutions may not work. */
18607 if (!TYPE_P (op1))
18608 op1 = TREE_TYPE (op1);
18609 }
18610 else
18611 {
18612 ++cp_unevaluated_operand;
18613 ++c_inhibit_evaluation_warnings;
18614 if (TYPE_P (op1))
18615 op1 = tsubst (op1, args, complain, in_decl);
18616 else
18617 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
18618 /*function_p=*/false,
18619 /*integral_constant_expression_p=*/
18620 false);
18621 --cp_unevaluated_operand;
18622 --c_inhibit_evaluation_warnings;
18623 }
18624 if (TYPE_P (op1))
18625 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t), std_alignof,
18626 complain & tf_error);
18627 else
18628 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
18629 complain & tf_error);
18630 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
18631 {
18632 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
18633 {
18634 if (!processing_template_decl && TYPE_P (op1))
18635 {
18636 r = build_min (SIZEOF_EXPR, size_type_node,
18637 build1 (NOP_EXPR, op1, error_mark_node));
18638 SIZEOF_EXPR_TYPE_P (r) = 1;
18639 }
18640 else
18641 r = build_min (SIZEOF_EXPR, size_type_node, op1);
18642 TREE_SIDE_EFFECTS (r) = 0;
18643 TREE_READONLY (r) = 1;
18644 }
18645 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
18646 }
18647 RETURN (r);
18648 }
18649
18650 case AT_ENCODE_EXPR:
18651 {
18652 op1 = TREE_OPERAND (t, 0);
18653 ++cp_unevaluated_operand;
18654 ++c_inhibit_evaluation_warnings;
18655 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
18656 /*function_p=*/false,
18657 /*integral_constant_expression_p=*/false);
18658 --cp_unevaluated_operand;
18659 --c_inhibit_evaluation_warnings;
18660 RETURN (objc_build_encode_expr (op1));
18661 }
18662
18663 case NOEXCEPT_EXPR:
18664 op1 = TREE_OPERAND (t, 0);
18665 ++cp_unevaluated_operand;
18666 ++c_inhibit_evaluation_warnings;
18667 ++cp_noexcept_operand;
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 --cp_noexcept_operand;
18674 RETURN (finish_noexcept_expr (op1, complain));
18675
18676 case MODOP_EXPR:
18677 {
18678 warning_sentinel s(warn_div_by_zero);
18679 tree lhs = RECUR (TREE_OPERAND (t, 0));
18680 tree rhs = RECUR (TREE_OPERAND (t, 2));
18681 tree r = build_x_modify_expr
18682 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
18683 complain|decltype_flag);
18684 /* TREE_NO_WARNING must be set if either the expression was
18685 parenthesized or it uses an operator such as >>= rather
18686 than plain assignment. In the former case, it was already
18687 set and must be copied. In the latter case,
18688 build_x_modify_expr sets it and it must not be reset
18689 here. */
18690 if (TREE_NO_WARNING (t))
18691 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
18692
18693 RETURN (r);
18694 }
18695
18696 case ARROW_EXPR:
18697 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18698 args, complain, in_decl);
18699 /* Remember that there was a reference to this entity. */
18700 if (DECL_P (op1)
18701 && !mark_used (op1, complain) && !(complain & tf_error))
18702 RETURN (error_mark_node);
18703 RETURN (build_x_arrow (input_location, op1, complain));
18704
18705 case NEW_EXPR:
18706 {
18707 tree placement = RECUR (TREE_OPERAND (t, 0));
18708 tree init = RECUR (TREE_OPERAND (t, 3));
18709 vec<tree, va_gc> *placement_vec;
18710 vec<tree, va_gc> *init_vec;
18711 tree ret;
18712
18713 if (placement == NULL_TREE)
18714 placement_vec = NULL;
18715 else
18716 {
18717 placement_vec = make_tree_vector ();
18718 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
18719 vec_safe_push (placement_vec, TREE_VALUE (placement));
18720 }
18721
18722 /* If there was an initializer in the original tree, but it
18723 instantiated to an empty list, then we should pass a
18724 non-NULL empty vector to tell build_new that it was an
18725 empty initializer() rather than no initializer. This can
18726 only happen when the initializer is a pack expansion whose
18727 parameter packs are of length zero. */
18728 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
18729 init_vec = NULL;
18730 else
18731 {
18732 init_vec = make_tree_vector ();
18733 if (init == void_node)
18734 gcc_assert (init_vec != NULL);
18735 else
18736 {
18737 for (; init != NULL_TREE; init = TREE_CHAIN (init))
18738 vec_safe_push (init_vec, TREE_VALUE (init));
18739 }
18740 }
18741
18742 /* Avoid passing an enclosing decl to valid_array_size_p. */
18743 in_decl = NULL_TREE;
18744
18745 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
18746 tree op2 = RECUR (TREE_OPERAND (t, 2));
18747 ret = build_new (&placement_vec, op1, op2, &init_vec,
18748 NEW_EXPR_USE_GLOBAL (t),
18749 complain);
18750
18751 if (placement_vec != NULL)
18752 release_tree_vector (placement_vec);
18753 if (init_vec != NULL)
18754 release_tree_vector (init_vec);
18755
18756 RETURN (ret);
18757 }
18758
18759 case DELETE_EXPR:
18760 {
18761 tree op0 = RECUR (TREE_OPERAND (t, 0));
18762 tree op1 = RECUR (TREE_OPERAND (t, 1));
18763 RETURN (delete_sanity (op0, op1,
18764 DELETE_EXPR_USE_VEC (t),
18765 DELETE_EXPR_USE_GLOBAL (t),
18766 complain));
18767 }
18768
18769 case COMPOUND_EXPR:
18770 {
18771 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
18772 complain & ~tf_decltype, in_decl,
18773 /*function_p=*/false,
18774 integral_constant_expression_p);
18775 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
18776 op0,
18777 RECUR (TREE_OPERAND (t, 1)),
18778 complain|decltype_flag));
18779 }
18780
18781 case CALL_EXPR:
18782 {
18783 tree function;
18784 unsigned int nargs, i;
18785 bool qualified_p;
18786 bool koenig_p;
18787 tree ret;
18788
18789 function = CALL_EXPR_FN (t);
18790 /* Internal function with no arguments. */
18791 if (function == NULL_TREE && call_expr_nargs (t) == 0)
18792 RETURN (t);
18793
18794 /* When we parsed the expression, we determined whether or
18795 not Koenig lookup should be performed. */
18796 koenig_p = KOENIG_LOOKUP_P (t);
18797 if (function == NULL_TREE)
18798 {
18799 koenig_p = false;
18800 qualified_p = false;
18801 }
18802 else if (TREE_CODE (function) == SCOPE_REF)
18803 {
18804 qualified_p = true;
18805 function = tsubst_qualified_id (function, args, complain, in_decl,
18806 /*done=*/false,
18807 /*address_p=*/false);
18808 }
18809 else if (koenig_p && identifier_p (function))
18810 {
18811 /* Do nothing; calling tsubst_copy_and_build on an identifier
18812 would incorrectly perform unqualified lookup again.
18813
18814 Note that we can also have an IDENTIFIER_NODE if the earlier
18815 unqualified lookup found a member function; in that case
18816 koenig_p will be false and we do want to do the lookup
18817 again to find the instantiated member function.
18818
18819 FIXME but doing that causes c++/15272, so we need to stop
18820 using IDENTIFIER_NODE in that situation. */
18821 qualified_p = false;
18822 }
18823 else
18824 {
18825 if (TREE_CODE (function) == COMPONENT_REF)
18826 {
18827 tree op = TREE_OPERAND (function, 1);
18828
18829 qualified_p = (TREE_CODE (op) == SCOPE_REF
18830 || (BASELINK_P (op)
18831 && BASELINK_QUALIFIED_P (op)));
18832 }
18833 else
18834 qualified_p = false;
18835
18836 if (TREE_CODE (function) == ADDR_EXPR
18837 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
18838 /* Avoid error about taking the address of a constructor. */
18839 function = TREE_OPERAND (function, 0);
18840
18841 function = tsubst_copy_and_build (function, args, complain,
18842 in_decl,
18843 !qualified_p,
18844 integral_constant_expression_p);
18845
18846 if (BASELINK_P (function))
18847 qualified_p = true;
18848 }
18849
18850 nargs = call_expr_nargs (t);
18851 releasing_vec call_args;
18852 for (i = 0; i < nargs; ++i)
18853 {
18854 tree arg = CALL_EXPR_ARG (t, i);
18855
18856 if (!PACK_EXPANSION_P (arg))
18857 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
18858 else
18859 {
18860 /* Expand the pack expansion and push each entry onto
18861 CALL_ARGS. */
18862 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
18863 if (TREE_CODE (arg) == TREE_VEC)
18864 {
18865 unsigned int len, j;
18866
18867 len = TREE_VEC_LENGTH (arg);
18868 for (j = 0; j < len; ++j)
18869 {
18870 tree value = TREE_VEC_ELT (arg, j);
18871 if (value != NULL_TREE)
18872 value = convert_from_reference (value);
18873 vec_safe_push (call_args, value);
18874 }
18875 }
18876 else
18877 {
18878 /* A partial substitution. Add one entry. */
18879 vec_safe_push (call_args, arg);
18880 }
18881 }
18882 }
18883
18884 /* Stripped-down processing for a call in a thunk. Specifically, in
18885 the thunk template for a generic lambda. */
18886 if (CALL_FROM_THUNK_P (t))
18887 {
18888 /* Now that we've expanded any packs, the number of call args
18889 might be different. */
18890 unsigned int cargs = call_args->length ();
18891 tree thisarg = NULL_TREE;
18892 if (TREE_CODE (function) == COMPONENT_REF)
18893 {
18894 thisarg = TREE_OPERAND (function, 0);
18895 if (TREE_CODE (thisarg) == INDIRECT_REF)
18896 thisarg = TREE_OPERAND (thisarg, 0);
18897 function = TREE_OPERAND (function, 1);
18898 if (TREE_CODE (function) == BASELINK)
18899 function = BASELINK_FUNCTIONS (function);
18900 }
18901 /* We aren't going to do normal overload resolution, so force the
18902 template-id to resolve. */
18903 function = resolve_nondeduced_context (function, complain);
18904 for (unsigned i = 0; i < cargs; ++i)
18905 {
18906 /* In a thunk, pass through args directly, without any
18907 conversions. */
18908 tree arg = (*call_args)[i];
18909 while (TREE_CODE (arg) != PARM_DECL)
18910 arg = TREE_OPERAND (arg, 0);
18911 (*call_args)[i] = arg;
18912 }
18913 if (thisarg)
18914 {
18915 /* If there are no other args, just push 'this'. */
18916 if (cargs == 0)
18917 vec_safe_push (call_args, thisarg);
18918 else
18919 {
18920 /* Otherwise, shift the other args over to make room. */
18921 tree last = (*call_args)[cargs - 1];
18922 vec_safe_push (call_args, last);
18923 for (int i = cargs - 1; i > 0; --i)
18924 (*call_args)[i] = (*call_args)[i - 1];
18925 (*call_args)[0] = thisarg;
18926 }
18927 }
18928 ret = build_call_a (function, call_args->length (),
18929 call_args->address ());
18930 /* The thunk location is not interesting. */
18931 SET_EXPR_LOCATION (ret, UNKNOWN_LOCATION);
18932 CALL_FROM_THUNK_P (ret) = true;
18933 if (CLASS_TYPE_P (TREE_TYPE (ret)))
18934 CALL_EXPR_RETURN_SLOT_OPT (ret) = true;
18935
18936 RETURN (ret);
18937 }
18938
18939 /* We do not perform argument-dependent lookup if normal
18940 lookup finds a non-function, in accordance with the
18941 expected resolution of DR 218. */
18942 if (koenig_p
18943 && ((is_overloaded_fn (function)
18944 /* If lookup found a member function, the Koenig lookup is
18945 not appropriate, even if an unqualified-name was used
18946 to denote the function. */
18947 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
18948 || identifier_p (function))
18949 /* Only do this when substitution turns a dependent call
18950 into a non-dependent call. */
18951 && type_dependent_expression_p_push (t)
18952 && !any_type_dependent_arguments_p (call_args))
18953 function = perform_koenig_lookup (function, call_args, tf_none);
18954
18955 if (function != NULL_TREE
18956 && identifier_p (function)
18957 && !any_type_dependent_arguments_p (call_args))
18958 {
18959 if (koenig_p && (complain & tf_warning_or_error))
18960 {
18961 /* For backwards compatibility and good diagnostics, try
18962 the unqualified lookup again if we aren't in SFINAE
18963 context. */
18964 tree unq = (tsubst_copy_and_build
18965 (function, args, complain, in_decl, true,
18966 integral_constant_expression_p));
18967 if (unq == error_mark_node)
18968 RETURN (error_mark_node);
18969
18970 if (unq != function)
18971 {
18972 /* In a lambda fn, we have to be careful to not
18973 introduce new this captures. Legacy code can't
18974 be using lambdas anyway, so it's ok to be
18975 stricter. */
18976 bool in_lambda = (current_class_type
18977 && LAMBDA_TYPE_P (current_class_type));
18978 char const *const msg
18979 = G_("%qD was not declared in this scope, "
18980 "and no declarations were found by "
18981 "argument-dependent lookup at the point "
18982 "of instantiation");
18983
18984 bool diag = true;
18985 if (in_lambda)
18986 error_at (cp_expr_loc_or_input_loc (t),
18987 msg, function);
18988 else
18989 diag = permerror (cp_expr_loc_or_input_loc (t),
18990 msg, function);
18991 if (diag)
18992 {
18993 tree fn = unq;
18994
18995 if (INDIRECT_REF_P (fn))
18996 fn = TREE_OPERAND (fn, 0);
18997 if (is_overloaded_fn (fn))
18998 fn = get_first_fn (fn);
18999
19000 if (!DECL_P (fn))
19001 /* Can't say anything more. */;
19002 else if (DECL_CLASS_SCOPE_P (fn))
19003 {
19004 location_t loc = cp_expr_loc_or_input_loc (t);
19005 inform (loc,
19006 "declarations in dependent base %qT are "
19007 "not found by unqualified lookup",
19008 DECL_CLASS_CONTEXT (fn));
19009 if (current_class_ptr)
19010 inform (loc,
19011 "use %<this->%D%> instead", function);
19012 else
19013 inform (loc,
19014 "use %<%T::%D%> instead",
19015 current_class_name, function);
19016 }
19017 else
19018 inform (DECL_SOURCE_LOCATION (fn),
19019 "%qD declared here, later in the "
19020 "translation unit", fn);
19021 if (in_lambda)
19022 RETURN (error_mark_node);
19023 }
19024
19025 function = unq;
19026 }
19027 }
19028 if (identifier_p (function))
19029 {
19030 if (complain & tf_error)
19031 unqualified_name_lookup_error (function);
19032 RETURN (error_mark_node);
19033 }
19034 }
19035
19036 /* Remember that there was a reference to this entity. */
19037 if (function != NULL_TREE
19038 && DECL_P (function)
19039 && !mark_used (function, complain) && !(complain & tf_error))
19040 RETURN (error_mark_node);
19041
19042 /* Put back tf_decltype for the actual call. */
19043 complain |= decltype_flag;
19044
19045 if (function == NULL_TREE)
19046 switch (CALL_EXPR_IFN (t))
19047 {
19048 case IFN_LAUNDER:
19049 gcc_assert (nargs == 1);
19050 if (vec_safe_length (call_args) != 1)
19051 {
19052 error_at (cp_expr_loc_or_input_loc (t),
19053 "wrong number of arguments to "
19054 "%<__builtin_launder%>");
19055 ret = error_mark_node;
19056 }
19057 else
19058 ret = finish_builtin_launder (cp_expr_loc_or_input_loc (t),
19059 (*call_args)[0], complain);
19060 break;
19061
19062 case IFN_VEC_CONVERT:
19063 gcc_assert (nargs == 1);
19064 if (vec_safe_length (call_args) != 1)
19065 {
19066 error_at (cp_expr_loc_or_input_loc (t),
19067 "wrong number of arguments to "
19068 "%<__builtin_convertvector%>");
19069 ret = error_mark_node;
19070 break;
19071 }
19072 ret = cp_build_vec_convert ((*call_args)[0], input_location,
19073 tsubst (TREE_TYPE (t), args,
19074 complain, in_decl),
19075 complain);
19076 if (TREE_CODE (ret) == VIEW_CONVERT_EXPR)
19077 RETURN (ret);
19078 break;
19079
19080 default:
19081 /* Unsupported internal function with arguments. */
19082 gcc_unreachable ();
19083 }
19084 else if (TREE_CODE (function) == OFFSET_REF
19085 || TREE_CODE (function) == DOTSTAR_EXPR
19086 || TREE_CODE (function) == MEMBER_REF)
19087 ret = build_offset_ref_call_from_tree (function, &call_args,
19088 complain);
19089 else if (TREE_CODE (function) == COMPONENT_REF)
19090 {
19091 tree instance = TREE_OPERAND (function, 0);
19092 tree fn = TREE_OPERAND (function, 1);
19093
19094 if (processing_template_decl
19095 && (type_dependent_expression_p (instance)
19096 || (!BASELINK_P (fn)
19097 && TREE_CODE (fn) != FIELD_DECL)
19098 || type_dependent_expression_p (fn)
19099 || any_type_dependent_arguments_p (call_args)))
19100 ret = build_min_nt_call_vec (function, call_args);
19101 else if (!BASELINK_P (fn))
19102 ret = finish_call_expr (function, &call_args,
19103 /*disallow_virtual=*/false,
19104 /*koenig_p=*/false,
19105 complain);
19106 else
19107 ret = (build_new_method_call
19108 (instance, fn,
19109 &call_args, NULL_TREE,
19110 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
19111 /*fn_p=*/NULL,
19112 complain));
19113 }
19114 else
19115 ret = finish_call_expr (function, &call_args,
19116 /*disallow_virtual=*/qualified_p,
19117 koenig_p,
19118 complain);
19119
19120 if (ret != error_mark_node)
19121 {
19122 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
19123 bool ord = CALL_EXPR_ORDERED_ARGS (t);
19124 bool rev = CALL_EXPR_REVERSE_ARGS (t);
19125 if (op || ord || rev)
19126 {
19127 function = extract_call_expr (ret);
19128 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
19129 CALL_EXPR_ORDERED_ARGS (function) = ord;
19130 CALL_EXPR_REVERSE_ARGS (function) = rev;
19131 }
19132 }
19133
19134 RETURN (ret);
19135 }
19136
19137 case COND_EXPR:
19138 {
19139 tree cond = RECUR (TREE_OPERAND (t, 0));
19140 cond = mark_rvalue_use (cond);
19141 tree folded_cond = fold_non_dependent_expr (cond, complain);
19142 tree exp1, exp2;
19143
19144 if (TREE_CODE (folded_cond) == INTEGER_CST)
19145 {
19146 if (integer_zerop (folded_cond))
19147 {
19148 ++c_inhibit_evaluation_warnings;
19149 exp1 = RECUR (TREE_OPERAND (t, 1));
19150 --c_inhibit_evaluation_warnings;
19151 exp2 = RECUR (TREE_OPERAND (t, 2));
19152 }
19153 else
19154 {
19155 exp1 = RECUR (TREE_OPERAND (t, 1));
19156 ++c_inhibit_evaluation_warnings;
19157 exp2 = RECUR (TREE_OPERAND (t, 2));
19158 --c_inhibit_evaluation_warnings;
19159 }
19160 cond = folded_cond;
19161 }
19162 else
19163 {
19164 exp1 = RECUR (TREE_OPERAND (t, 1));
19165 exp2 = RECUR (TREE_OPERAND (t, 2));
19166 }
19167
19168 warning_sentinel s(warn_duplicated_branches);
19169 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
19170 cond, exp1, exp2, complain));
19171 }
19172
19173 case PSEUDO_DTOR_EXPR:
19174 {
19175 tree op0 = RECUR (TREE_OPERAND (t, 0));
19176 tree op1 = RECUR (TREE_OPERAND (t, 1));
19177 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
19178 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
19179 input_location));
19180 }
19181
19182 case TREE_LIST:
19183 {
19184 tree purpose, value, chain;
19185
19186 if (t == void_list_node)
19187 RETURN (t);
19188
19189 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
19190 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
19191 {
19192 /* We have pack expansions, so expand those and
19193 create a new list out of it. */
19194 tree purposevec = NULL_TREE;
19195 tree valuevec = NULL_TREE;
19196 tree chain;
19197 int i, len = -1;
19198
19199 /* Expand the argument expressions. */
19200 if (TREE_PURPOSE (t))
19201 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
19202 complain, in_decl);
19203 if (TREE_VALUE (t))
19204 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
19205 complain, in_decl);
19206
19207 /* Build the rest of the list. */
19208 chain = TREE_CHAIN (t);
19209 if (chain && chain != void_type_node)
19210 chain = RECUR (chain);
19211
19212 /* Determine the number of arguments. */
19213 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
19214 {
19215 len = TREE_VEC_LENGTH (purposevec);
19216 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
19217 }
19218 else if (TREE_CODE (valuevec) == TREE_VEC)
19219 len = TREE_VEC_LENGTH (valuevec);
19220 else
19221 {
19222 /* Since we only performed a partial substitution into
19223 the argument pack, we only RETURN (a single list
19224 node. */
19225 if (purposevec == TREE_PURPOSE (t)
19226 && valuevec == TREE_VALUE (t)
19227 && chain == TREE_CHAIN (t))
19228 RETURN (t);
19229
19230 RETURN (tree_cons (purposevec, valuevec, chain));
19231 }
19232
19233 /* Convert the argument vectors into a TREE_LIST */
19234 i = len;
19235 while (i > 0)
19236 {
19237 /* Grab the Ith values. */
19238 i--;
19239 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
19240 : NULL_TREE;
19241 value
19242 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
19243 : NULL_TREE;
19244
19245 /* Build the list (backwards). */
19246 chain = tree_cons (purpose, value, chain);
19247 }
19248
19249 RETURN (chain);
19250 }
19251
19252 purpose = TREE_PURPOSE (t);
19253 if (purpose)
19254 purpose = RECUR (purpose);
19255 value = TREE_VALUE (t);
19256 if (value)
19257 value = RECUR (value);
19258 chain = TREE_CHAIN (t);
19259 if (chain && chain != void_type_node)
19260 chain = RECUR (chain);
19261 if (purpose == TREE_PURPOSE (t)
19262 && value == TREE_VALUE (t)
19263 && chain == TREE_CHAIN (t))
19264 RETURN (t);
19265 RETURN (tree_cons (purpose, value, chain));
19266 }
19267
19268 case COMPONENT_REF:
19269 {
19270 tree object;
19271 tree object_type;
19272 tree member;
19273 tree r;
19274
19275 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19276 args, complain, in_decl);
19277 /* Remember that there was a reference to this entity. */
19278 if (DECL_P (object)
19279 && !mark_used (object, complain) && !(complain & tf_error))
19280 RETURN (error_mark_node);
19281 object_type = TREE_TYPE (object);
19282
19283 member = TREE_OPERAND (t, 1);
19284 if (BASELINK_P (member))
19285 member = tsubst_baselink (member,
19286 non_reference (TREE_TYPE (object)),
19287 args, complain, in_decl);
19288 else
19289 member = tsubst_copy (member, args, complain, in_decl);
19290 if (member == error_mark_node)
19291 RETURN (error_mark_node);
19292
19293 if (TREE_CODE (member) == FIELD_DECL)
19294 {
19295 r = finish_non_static_data_member (member, object, NULL_TREE);
19296 if (TREE_CODE (r) == COMPONENT_REF)
19297 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
19298 RETURN (r);
19299 }
19300 else if (type_dependent_expression_p (object))
19301 /* We can't do much here. */;
19302 else if (!CLASS_TYPE_P (object_type))
19303 {
19304 if (scalarish_type_p (object_type))
19305 {
19306 tree s = NULL_TREE;
19307 tree dtor = member;
19308
19309 if (TREE_CODE (dtor) == SCOPE_REF)
19310 {
19311 s = TREE_OPERAND (dtor, 0);
19312 dtor = TREE_OPERAND (dtor, 1);
19313 }
19314 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
19315 {
19316 dtor = TREE_OPERAND (dtor, 0);
19317 if (TYPE_P (dtor))
19318 RETURN (finish_pseudo_destructor_expr
19319 (object, s, dtor, input_location));
19320 }
19321 }
19322 }
19323 else if (TREE_CODE (member) == SCOPE_REF
19324 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
19325 {
19326 /* Lookup the template functions now that we know what the
19327 scope is. */
19328 tree scope = TREE_OPERAND (member, 0);
19329 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
19330 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
19331 member = lookup_qualified_name (scope, tmpl,
19332 /*is_type_p=*/false,
19333 /*complain=*/false);
19334 if (BASELINK_P (member))
19335 {
19336 BASELINK_FUNCTIONS (member)
19337 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
19338 args);
19339 member = (adjust_result_of_qualified_name_lookup
19340 (member, BINFO_TYPE (BASELINK_BINFO (member)),
19341 object_type));
19342 }
19343 else
19344 {
19345 qualified_name_lookup_error (scope, tmpl, member,
19346 input_location);
19347 RETURN (error_mark_node);
19348 }
19349 }
19350 else if (TREE_CODE (member) == SCOPE_REF
19351 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
19352 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
19353 {
19354 if (complain & tf_error)
19355 {
19356 if (TYPE_P (TREE_OPERAND (member, 0)))
19357 error ("%qT is not a class or namespace",
19358 TREE_OPERAND (member, 0));
19359 else
19360 error ("%qD is not a class or namespace",
19361 TREE_OPERAND (member, 0));
19362 }
19363 RETURN (error_mark_node);
19364 }
19365
19366 r = finish_class_member_access_expr (object, member,
19367 /*template_p=*/false,
19368 complain);
19369 if (TREE_CODE (r) == COMPONENT_REF)
19370 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
19371 RETURN (r);
19372 }
19373
19374 case THROW_EXPR:
19375 RETURN (build_throw
19376 (RECUR (TREE_OPERAND (t, 0))));
19377
19378 case CONSTRUCTOR:
19379 {
19380 vec<constructor_elt, va_gc> *n;
19381 constructor_elt *ce;
19382 unsigned HOST_WIDE_INT idx;
19383 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19384 bool process_index_p;
19385 int newlen;
19386 bool need_copy_p = false;
19387 tree r;
19388
19389 if (type == error_mark_node)
19390 RETURN (error_mark_node);
19391
19392 /* We do not want to process the index of aggregate
19393 initializers as they are identifier nodes which will be
19394 looked up by digest_init. */
19395 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
19396
19397 if (null_member_pointer_value_p (t))
19398 {
19399 gcc_assert (same_type_p (type, TREE_TYPE (t)));
19400 RETURN (t);
19401 }
19402
19403 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
19404 newlen = vec_safe_length (n);
19405 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
19406 {
19407 if (ce->index && process_index_p
19408 /* An identifier index is looked up in the type
19409 being initialized, not the current scope. */
19410 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
19411 ce->index = RECUR (ce->index);
19412
19413 if (PACK_EXPANSION_P (ce->value))
19414 {
19415 /* Substitute into the pack expansion. */
19416 ce->value = tsubst_pack_expansion (ce->value, args, complain,
19417 in_decl);
19418
19419 if (ce->value == error_mark_node
19420 || PACK_EXPANSION_P (ce->value))
19421 ;
19422 else if (TREE_VEC_LENGTH (ce->value) == 1)
19423 /* Just move the argument into place. */
19424 ce->value = TREE_VEC_ELT (ce->value, 0);
19425 else
19426 {
19427 /* Update the length of the final CONSTRUCTOR
19428 arguments vector, and note that we will need to
19429 copy.*/
19430 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
19431 need_copy_p = true;
19432 }
19433 }
19434 else
19435 ce->value = RECUR (ce->value);
19436 }
19437
19438 if (need_copy_p)
19439 {
19440 vec<constructor_elt, va_gc> *old_n = n;
19441
19442 vec_alloc (n, newlen);
19443 FOR_EACH_VEC_ELT (*old_n, idx, ce)
19444 {
19445 if (TREE_CODE (ce->value) == TREE_VEC)
19446 {
19447 int i, len = TREE_VEC_LENGTH (ce->value);
19448 for (i = 0; i < len; ++i)
19449 CONSTRUCTOR_APPEND_ELT (n, 0,
19450 TREE_VEC_ELT (ce->value, i));
19451 }
19452 else
19453 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
19454 }
19455 }
19456
19457 r = build_constructor (init_list_type_node, n);
19458 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
19459 CONSTRUCTOR_IS_DESIGNATED_INIT (r)
19460 = CONSTRUCTOR_IS_DESIGNATED_INIT (t);
19461
19462 if (TREE_HAS_CONSTRUCTOR (t))
19463 {
19464 fcl_t cl = fcl_functional;
19465 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
19466 cl = fcl_c99;
19467 RETURN (finish_compound_literal (type, r, complain, cl));
19468 }
19469
19470 TREE_TYPE (r) = type;
19471 RETURN (r);
19472 }
19473
19474 case TYPEID_EXPR:
19475 {
19476 tree operand_0 = TREE_OPERAND (t, 0);
19477 if (TYPE_P (operand_0))
19478 {
19479 operand_0 = tsubst (operand_0, args, complain, in_decl);
19480 RETURN (get_typeid (operand_0, complain));
19481 }
19482 else
19483 {
19484 operand_0 = RECUR (operand_0);
19485 RETURN (build_typeid (operand_0, complain));
19486 }
19487 }
19488
19489 case VAR_DECL:
19490 if (!args)
19491 RETURN (t);
19492 /* Fall through */
19493
19494 case PARM_DECL:
19495 {
19496 tree r = tsubst_copy (t, args, complain, in_decl);
19497 /* ??? We're doing a subset of finish_id_expression here. */
19498 if (tree wrap = maybe_get_tls_wrapper_call (r))
19499 /* Replace an evaluated use of the thread_local variable with
19500 a call to its wrapper. */
19501 r = wrap;
19502 else if (outer_automatic_var_p (r))
19503 r = process_outer_var_ref (r, complain);
19504
19505 if (!TYPE_REF_P (TREE_TYPE (t)))
19506 /* If the original type was a reference, we'll be wrapped in
19507 the appropriate INDIRECT_REF. */
19508 r = convert_from_reference (r);
19509 RETURN (r);
19510 }
19511
19512 case VA_ARG_EXPR:
19513 {
19514 tree op0 = RECUR (TREE_OPERAND (t, 0));
19515 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19516 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
19517 }
19518
19519 case OFFSETOF_EXPR:
19520 {
19521 tree object_ptr
19522 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
19523 in_decl, /*function_p=*/false,
19524 /*integral_constant_expression_p=*/false);
19525 RETURN (finish_offsetof (object_ptr,
19526 RECUR (TREE_OPERAND (t, 0)),
19527 EXPR_LOCATION (t)));
19528 }
19529
19530 case ADDRESSOF_EXPR:
19531 RETURN (cp_build_addressof (EXPR_LOCATION (t),
19532 RECUR (TREE_OPERAND (t, 0)), complain));
19533
19534 case TRAIT_EXPR:
19535 {
19536 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
19537 complain, in_decl);
19538
19539 tree type2 = TRAIT_EXPR_TYPE2 (t);
19540 if (type2 && TREE_CODE (type2) == TREE_LIST)
19541 type2 = RECUR (type2);
19542 else if (type2)
19543 type2 = tsubst (type2, args, complain, in_decl);
19544
19545 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t),
19546 TRAIT_EXPR_KIND (t), type1, type2));
19547 }
19548
19549 case STMT_EXPR:
19550 {
19551 tree old_stmt_expr = cur_stmt_expr;
19552 tree stmt_expr = begin_stmt_expr ();
19553
19554 cur_stmt_expr = stmt_expr;
19555 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
19556 integral_constant_expression_p);
19557 stmt_expr = finish_stmt_expr (stmt_expr, false);
19558 cur_stmt_expr = old_stmt_expr;
19559
19560 /* If the resulting list of expression statement is empty,
19561 fold it further into void_node. */
19562 if (empty_expr_stmt_p (stmt_expr))
19563 stmt_expr = void_node;
19564
19565 RETURN (stmt_expr);
19566 }
19567
19568 case LAMBDA_EXPR:
19569 {
19570 if (complain & tf_partial)
19571 {
19572 /* We don't have a full set of template arguments yet; don't touch
19573 the lambda at all. */
19574 gcc_assert (processing_template_decl);
19575 return t;
19576 }
19577 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
19578
19579 RETURN (build_lambda_object (r));
19580 }
19581
19582 case TARGET_EXPR:
19583 /* We can get here for a constant initializer of non-dependent type.
19584 FIXME stop folding in cp_parser_initializer_clause. */
19585 {
19586 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
19587 complain);
19588 RETURN (r);
19589 }
19590
19591 case TRANSACTION_EXPR:
19592 RETURN (tsubst_expr(t, args, complain, in_decl,
19593 integral_constant_expression_p));
19594
19595 case PAREN_EXPR:
19596 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
19597
19598 case VEC_PERM_EXPR:
19599 {
19600 tree op0 = RECUR (TREE_OPERAND (t, 0));
19601 tree op1 = RECUR (TREE_OPERAND (t, 1));
19602 tree op2 = RECUR (TREE_OPERAND (t, 2));
19603 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
19604 complain));
19605 }
19606
19607 case REQUIRES_EXPR:
19608 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
19609
19610 case RANGE_EXPR:
19611 /* No need to substitute further, a RANGE_EXPR will always be built
19612 with constant operands. */
19613 RETURN (t);
19614
19615 case NON_LVALUE_EXPR:
19616 case VIEW_CONVERT_EXPR:
19617 if (location_wrapper_p (t))
19618 /* We need to do this here as well as in tsubst_copy so we get the
19619 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
19620 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)),
19621 EXPR_LOCATION (t)));
19622 /* fallthrough. */
19623
19624 default:
19625 /* Handle Objective-C++ constructs, if appropriate. */
19626 {
19627 tree subst
19628 = objcp_tsubst_copy_and_build (t, args, complain,
19629 in_decl, /*function_p=*/false);
19630 if (subst)
19631 RETURN (subst);
19632 }
19633 RETURN (tsubst_copy (t, args, complain, in_decl));
19634 }
19635
19636 #undef RECUR
19637 #undef RETURN
19638 out:
19639 input_location = loc;
19640 return retval;
19641 }
19642
19643 /* Verify that the instantiated ARGS are valid. For type arguments,
19644 make sure that the type's linkage is ok. For non-type arguments,
19645 make sure they are constants if they are integral or enumerations.
19646 Emit an error under control of COMPLAIN, and return TRUE on error. */
19647
19648 static bool
19649 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
19650 {
19651 if (dependent_template_arg_p (t))
19652 return false;
19653 if (ARGUMENT_PACK_P (t))
19654 {
19655 tree vec = ARGUMENT_PACK_ARGS (t);
19656 int len = TREE_VEC_LENGTH (vec);
19657 bool result = false;
19658 int i;
19659
19660 for (i = 0; i < len; ++i)
19661 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
19662 result = true;
19663 return result;
19664 }
19665 else if (TYPE_P (t))
19666 {
19667 /* [basic.link]: A name with no linkage (notably, the name
19668 of a class or enumeration declared in a local scope)
19669 shall not be used to declare an entity with linkage.
19670 This implies that names with no linkage cannot be used as
19671 template arguments
19672
19673 DR 757 relaxes this restriction for C++0x. */
19674 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
19675 : no_linkage_check (t, /*relaxed_p=*/false));
19676
19677 if (nt)
19678 {
19679 /* DR 488 makes use of a type with no linkage cause
19680 type deduction to fail. */
19681 if (complain & tf_error)
19682 {
19683 if (TYPE_UNNAMED_P (nt))
19684 error ("%qT is/uses unnamed type", t);
19685 else
19686 error ("template argument for %qD uses local type %qT",
19687 tmpl, t);
19688 }
19689 return true;
19690 }
19691 /* In order to avoid all sorts of complications, we do not
19692 allow variably-modified types as template arguments. */
19693 else if (variably_modified_type_p (t, NULL_TREE))
19694 {
19695 if (complain & tf_error)
19696 error ("%qT is a variably modified type", t);
19697 return true;
19698 }
19699 }
19700 /* Class template and alias template arguments should be OK. */
19701 else if (DECL_TYPE_TEMPLATE_P (t))
19702 ;
19703 /* A non-type argument of integral or enumerated type must be a
19704 constant. */
19705 else if (TREE_TYPE (t)
19706 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
19707 && !REFERENCE_REF_P (t)
19708 && !TREE_CONSTANT (t))
19709 {
19710 if (complain & tf_error)
19711 error ("integral expression %qE is not constant", t);
19712 return true;
19713 }
19714 return false;
19715 }
19716
19717 static bool
19718 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
19719 {
19720 int ix, len = DECL_NTPARMS (tmpl);
19721 bool result = false;
19722
19723 for (ix = 0; ix != len; ix++)
19724 {
19725 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
19726 result = true;
19727 }
19728 if (result && (complain & tf_error))
19729 error (" trying to instantiate %qD", tmpl);
19730 return result;
19731 }
19732
19733 /* We're out of SFINAE context now, so generate diagnostics for the access
19734 errors we saw earlier when instantiating D from TMPL and ARGS. */
19735
19736 static void
19737 recheck_decl_substitution (tree d, tree tmpl, tree args)
19738 {
19739 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
19740 tree type = TREE_TYPE (pattern);
19741 location_t loc = input_location;
19742
19743 push_access_scope (d);
19744 push_deferring_access_checks (dk_no_deferred);
19745 input_location = DECL_SOURCE_LOCATION (pattern);
19746 tsubst (type, args, tf_warning_or_error, d);
19747 input_location = loc;
19748 pop_deferring_access_checks ();
19749 pop_access_scope (d);
19750 }
19751
19752 /* Instantiate the indicated variable, function, or alias template TMPL with
19753 the template arguments in TARG_PTR. */
19754
19755 static tree
19756 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
19757 {
19758 tree targ_ptr = orig_args;
19759 tree fndecl;
19760 tree gen_tmpl;
19761 tree spec;
19762 bool access_ok = true;
19763
19764 if (tmpl == error_mark_node)
19765 return error_mark_node;
19766
19767 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
19768
19769 /* If this function is a clone, handle it specially. */
19770 if (DECL_CLONED_FUNCTION_P (tmpl))
19771 {
19772 tree spec;
19773 tree clone;
19774
19775 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
19776 DECL_CLONED_FUNCTION. */
19777 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
19778 targ_ptr, complain);
19779 if (spec == error_mark_node)
19780 return error_mark_node;
19781
19782 /* Look for the clone. */
19783 FOR_EACH_CLONE (clone, spec)
19784 if (DECL_NAME (clone) == DECL_NAME (tmpl))
19785 return clone;
19786 /* We should always have found the clone by now. */
19787 gcc_unreachable ();
19788 return NULL_TREE;
19789 }
19790
19791 if (targ_ptr == error_mark_node)
19792 return error_mark_node;
19793
19794 /* Check to see if we already have this specialization. */
19795 gen_tmpl = most_general_template (tmpl);
19796 if (TMPL_ARGS_DEPTH (targ_ptr)
19797 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
19798 /* targ_ptr only has the innermost template args, so add the outer ones
19799 from tmpl, which could be either a partial instantiation or gen_tmpl (in
19800 the case of a non-dependent call within a template definition). */
19801 targ_ptr = (add_outermost_template_args
19802 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
19803 targ_ptr));
19804
19805 /* It would be nice to avoid hashing here and then again in tsubst_decl,
19806 but it doesn't seem to be on the hot path. */
19807 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
19808
19809 gcc_assert (tmpl == gen_tmpl
19810 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
19811 == spec)
19812 || fndecl == NULL_TREE);
19813
19814 if (spec != NULL_TREE)
19815 {
19816 if (FNDECL_HAS_ACCESS_ERRORS (spec))
19817 {
19818 if (complain & tf_error)
19819 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
19820 return error_mark_node;
19821 }
19822 return spec;
19823 }
19824
19825 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
19826 complain))
19827 return error_mark_node;
19828
19829 /* We are building a FUNCTION_DECL, during which the access of its
19830 parameters and return types have to be checked. However this
19831 FUNCTION_DECL which is the desired context for access checking
19832 is not built yet. We solve this chicken-and-egg problem by
19833 deferring all checks until we have the FUNCTION_DECL. */
19834 push_deferring_access_checks (dk_deferred);
19835
19836 /* Instantiation of the function happens in the context of the function
19837 template, not the context of the overload resolution we're doing. */
19838 push_to_top_level ();
19839 /* If there are dependent arguments, e.g. because we're doing partial
19840 ordering, make sure processing_template_decl stays set. */
19841 if (uses_template_parms (targ_ptr))
19842 ++processing_template_decl;
19843 if (DECL_CLASS_SCOPE_P (gen_tmpl))
19844 {
19845 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
19846 complain, gen_tmpl, true);
19847 push_nested_class (ctx);
19848 }
19849
19850 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
19851
19852 fndecl = NULL_TREE;
19853 if (VAR_P (pattern))
19854 {
19855 /* We need to determine if we're using a partial or explicit
19856 specialization now, because the type of the variable could be
19857 different. */
19858 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
19859 tree elt = most_specialized_partial_spec (tid, complain);
19860 if (elt == error_mark_node)
19861 pattern = error_mark_node;
19862 else if (elt)
19863 {
19864 tree partial_tmpl = TREE_VALUE (elt);
19865 tree partial_args = TREE_PURPOSE (elt);
19866 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
19867 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
19868 }
19869 }
19870
19871 /* Substitute template parameters to obtain the specialization. */
19872 if (fndecl == NULL_TREE)
19873 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
19874 if (DECL_CLASS_SCOPE_P (gen_tmpl))
19875 pop_nested_class ();
19876 pop_from_top_level ();
19877
19878 if (fndecl == error_mark_node)
19879 {
19880 pop_deferring_access_checks ();
19881 return error_mark_node;
19882 }
19883
19884 /* The DECL_TI_TEMPLATE should always be the immediate parent
19885 template, not the most general template. */
19886 DECL_TI_TEMPLATE (fndecl) = tmpl;
19887 DECL_TI_ARGS (fndecl) = targ_ptr;
19888
19889 /* Now we know the specialization, compute access previously
19890 deferred. Do no access control for inheriting constructors,
19891 as we already checked access for the inherited constructor. */
19892 if (!(flag_new_inheriting_ctors
19893 && DECL_INHERITED_CTOR (fndecl)))
19894 {
19895 push_access_scope (fndecl);
19896 if (!perform_deferred_access_checks (complain))
19897 access_ok = false;
19898 pop_access_scope (fndecl);
19899 }
19900 pop_deferring_access_checks ();
19901
19902 /* If we've just instantiated the main entry point for a function,
19903 instantiate all the alternate entry points as well. We do this
19904 by cloning the instantiation of the main entry point, not by
19905 instantiating the template clones. */
19906 if (tree chain = DECL_CHAIN (gen_tmpl))
19907 if (DECL_P (chain) && DECL_CLONED_FUNCTION_P (chain))
19908 clone_function_decl (fndecl, /*update_methods=*/false);
19909
19910 if (!access_ok)
19911 {
19912 if (!(complain & tf_error))
19913 {
19914 /* Remember to reinstantiate when we're out of SFINAE so the user
19915 can see the errors. */
19916 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
19917 }
19918 return error_mark_node;
19919 }
19920 return fndecl;
19921 }
19922
19923 /* Wrapper for instantiate_template_1. */
19924
19925 tree
19926 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
19927 {
19928 tree ret;
19929 timevar_push (TV_TEMPLATE_INST);
19930 ret = instantiate_template_1 (tmpl, orig_args, complain);
19931 timevar_pop (TV_TEMPLATE_INST);
19932 return ret;
19933 }
19934
19935 /* Instantiate the alias template TMPL with ARGS. Also push a template
19936 instantiation level, which instantiate_template doesn't do because
19937 functions and variables have sufficient context established by the
19938 callers. */
19939
19940 static tree
19941 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
19942 {
19943 if (tmpl == error_mark_node || args == error_mark_node)
19944 return error_mark_node;
19945 if (!push_tinst_level (tmpl, args))
19946 return error_mark_node;
19947
19948 args =
19949 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
19950 args, tmpl, complain,
19951 /*require_all_args=*/true,
19952 /*use_default_args=*/true);
19953
19954 tree r = instantiate_template (tmpl, args, complain);
19955 pop_tinst_level ();
19956
19957 return r;
19958 }
19959
19960 /* PARM is a template parameter pack for FN. Returns true iff
19961 PARM is used in a deducible way in the argument list of FN. */
19962
19963 static bool
19964 pack_deducible_p (tree parm, tree fn)
19965 {
19966 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
19967 for (; t; t = TREE_CHAIN (t))
19968 {
19969 tree type = TREE_VALUE (t);
19970 tree packs;
19971 if (!PACK_EXPANSION_P (type))
19972 continue;
19973 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
19974 packs; packs = TREE_CHAIN (packs))
19975 if (template_args_equal (TREE_VALUE (packs), parm))
19976 {
19977 /* The template parameter pack is used in a function parameter
19978 pack. If this is the end of the parameter list, the
19979 template parameter pack is deducible. */
19980 if (TREE_CHAIN (t) == void_list_node)
19981 return true;
19982 else
19983 /* Otherwise, not. Well, it could be deduced from
19984 a non-pack parameter, but doing so would end up with
19985 a deduction mismatch, so don't bother. */
19986 return false;
19987 }
19988 }
19989 /* The template parameter pack isn't used in any function parameter
19990 packs, but it might be used deeper, e.g. tuple<Args...>. */
19991 return true;
19992 }
19993
19994 /* Subroutine of fn_type_unification: check non-dependent parms for
19995 convertibility. */
19996
19997 static int
19998 check_non_deducible_conversions (tree parms, const tree *args, unsigned nargs,
19999 tree fn, unification_kind_t strict, int flags,
20000 struct conversion **convs, bool explain_p)
20001 {
20002 /* Non-constructor methods need to leave a conversion for 'this', which
20003 isn't included in nargs here. */
20004 unsigned offset = (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
20005 && !DECL_CONSTRUCTOR_P (fn));
20006
20007 for (unsigned ia = 0;
20008 parms && parms != void_list_node && ia < nargs; )
20009 {
20010 tree parm = TREE_VALUE (parms);
20011
20012 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
20013 && (!TREE_CHAIN (parms)
20014 || TREE_CHAIN (parms) == void_list_node))
20015 /* For a function parameter pack that occurs at the end of the
20016 parameter-declaration-list, the type A of each remaining
20017 argument of the call is compared with the type P of the
20018 declarator-id of the function parameter pack. */
20019 break;
20020
20021 parms = TREE_CHAIN (parms);
20022
20023 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
20024 /* For a function parameter pack that does not occur at the
20025 end of the parameter-declaration-list, the type of the
20026 parameter pack is a non-deduced context. */
20027 continue;
20028
20029 if (!uses_template_parms (parm))
20030 {
20031 tree arg = args[ia];
20032 conversion **conv_p = convs ? &convs[ia+offset] : NULL;
20033 int lflags = conv_flags (ia, nargs, fn, arg, flags);
20034
20035 if (check_non_deducible_conversion (parm, arg, strict, lflags,
20036 conv_p, explain_p))
20037 return 1;
20038 }
20039
20040 ++ia;
20041 }
20042
20043 return 0;
20044 }
20045
20046 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
20047 NARGS elements of the arguments that are being used when calling
20048 it. TARGS is a vector into which the deduced template arguments
20049 are placed.
20050
20051 Returns either a FUNCTION_DECL for the matching specialization of FN or
20052 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
20053 true, diagnostics will be printed to explain why it failed.
20054
20055 If FN is a conversion operator, or we are trying to produce a specific
20056 specialization, RETURN_TYPE is the return type desired.
20057
20058 The EXPLICIT_TARGS are explicit template arguments provided via a
20059 template-id.
20060
20061 The parameter STRICT is one of:
20062
20063 DEDUCE_CALL:
20064 We are deducing arguments for a function call, as in
20065 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
20066 deducing arguments for a call to the result of a conversion
20067 function template, as in [over.call.object].
20068
20069 DEDUCE_CONV:
20070 We are deducing arguments for a conversion function, as in
20071 [temp.deduct.conv].
20072
20073 DEDUCE_EXACT:
20074 We are deducing arguments when doing an explicit instantiation
20075 as in [temp.explicit], when determining an explicit specialization
20076 as in [temp.expl.spec], or when taking the address of a function
20077 template, as in [temp.deduct.funcaddr]. */
20078
20079 tree
20080 fn_type_unification (tree fn,
20081 tree explicit_targs,
20082 tree targs,
20083 const tree *args,
20084 unsigned int nargs,
20085 tree return_type,
20086 unification_kind_t strict,
20087 int flags,
20088 struct conversion **convs,
20089 bool explain_p,
20090 bool decltype_p)
20091 {
20092 tree parms;
20093 tree fntype;
20094 tree decl = NULL_TREE;
20095 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
20096 bool ok;
20097 static int deduction_depth;
20098 /* type_unification_real will pass back any access checks from default
20099 template argument substitution. */
20100 vec<deferred_access_check, va_gc> *checks = NULL;
20101 /* We don't have all the template args yet. */
20102 bool incomplete = true;
20103
20104 tree orig_fn = fn;
20105 if (flag_new_inheriting_ctors)
20106 fn = strip_inheriting_ctors (fn);
20107
20108 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
20109 tree r = error_mark_node;
20110
20111 tree full_targs = targs;
20112 if (TMPL_ARGS_DEPTH (targs)
20113 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
20114 full_targs = (add_outermost_template_args
20115 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
20116 targs));
20117
20118 if (decltype_p)
20119 complain |= tf_decltype;
20120
20121 /* In C++0x, it's possible to have a function template whose type depends
20122 on itself recursively. This is most obvious with decltype, but can also
20123 occur with enumeration scope (c++/48969). So we need to catch infinite
20124 recursion and reject the substitution at deduction time; this function
20125 will return error_mark_node for any repeated substitution.
20126
20127 This also catches excessive recursion such as when f<N> depends on
20128 f<N-1> across all integers, and returns error_mark_node for all the
20129 substitutions back up to the initial one.
20130
20131 This is, of course, not reentrant. */
20132 if (excessive_deduction_depth)
20133 return error_mark_node;
20134 ++deduction_depth;
20135
20136 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
20137
20138 fntype = TREE_TYPE (fn);
20139 if (explicit_targs)
20140 {
20141 /* [temp.deduct]
20142
20143 The specified template arguments must match the template
20144 parameters in kind (i.e., type, nontype, template), and there
20145 must not be more arguments than there are parameters;
20146 otherwise type deduction fails.
20147
20148 Nontype arguments must match the types of the corresponding
20149 nontype template parameters, or must be convertible to the
20150 types of the corresponding nontype parameters as specified in
20151 _temp.arg.nontype_, otherwise type deduction fails.
20152
20153 All references in the function type of the function template
20154 to the corresponding template parameters are replaced by the
20155 specified template argument values. If a substitution in a
20156 template parameter or in the function type of the function
20157 template results in an invalid type, type deduction fails. */
20158 int i, len = TREE_VEC_LENGTH (tparms);
20159 location_t loc = input_location;
20160 incomplete = false;
20161
20162 if (explicit_targs == error_mark_node)
20163 goto fail;
20164
20165 if (TMPL_ARGS_DEPTH (explicit_targs)
20166 < TMPL_ARGS_DEPTH (full_targs))
20167 explicit_targs = add_outermost_template_args (full_targs,
20168 explicit_targs);
20169
20170 /* Adjust any explicit template arguments before entering the
20171 substitution context. */
20172 explicit_targs
20173 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
20174 complain|tf_partial,
20175 /*require_all_args=*/false,
20176 /*use_default_args=*/false));
20177 if (explicit_targs == error_mark_node)
20178 goto fail;
20179
20180 /* Substitute the explicit args into the function type. This is
20181 necessary so that, for instance, explicitly declared function
20182 arguments can match null pointed constants. If we were given
20183 an incomplete set of explicit args, we must not do semantic
20184 processing during substitution as we could create partial
20185 instantiations. */
20186 for (i = 0; i < len; i++)
20187 {
20188 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
20189 bool parameter_pack = false;
20190 tree targ = TREE_VEC_ELT (explicit_targs, i);
20191
20192 /* Dig out the actual parm. */
20193 if (TREE_CODE (parm) == TYPE_DECL
20194 || TREE_CODE (parm) == TEMPLATE_DECL)
20195 {
20196 parm = TREE_TYPE (parm);
20197 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
20198 }
20199 else if (TREE_CODE (parm) == PARM_DECL)
20200 {
20201 parm = DECL_INITIAL (parm);
20202 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
20203 }
20204
20205 if (targ == NULL_TREE)
20206 /* No explicit argument for this template parameter. */
20207 incomplete = true;
20208 else if (parameter_pack && pack_deducible_p (parm, fn))
20209 {
20210 /* Mark the argument pack as "incomplete". We could
20211 still deduce more arguments during unification.
20212 We remove this mark in type_unification_real. */
20213 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
20214 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
20215 = ARGUMENT_PACK_ARGS (targ);
20216
20217 /* We have some incomplete argument packs. */
20218 incomplete = true;
20219 }
20220 }
20221
20222 if (incomplete)
20223 {
20224 if (!push_tinst_level (fn, explicit_targs))
20225 {
20226 excessive_deduction_depth = true;
20227 goto fail;
20228 }
20229 ++processing_template_decl;
20230 input_location = DECL_SOURCE_LOCATION (fn);
20231 /* Ignore any access checks; we'll see them again in
20232 instantiate_template and they might have the wrong
20233 access path at this point. */
20234 push_deferring_access_checks (dk_deferred);
20235 tsubst_flags_t ecomplain = complain | tf_partial | tf_fndecl_type;
20236 fntype = tsubst (TREE_TYPE (fn), explicit_targs, ecomplain, NULL_TREE);
20237 pop_deferring_access_checks ();
20238 input_location = loc;
20239 --processing_template_decl;
20240 pop_tinst_level ();
20241
20242 if (fntype == error_mark_node)
20243 goto fail;
20244 }
20245
20246 /* Place the explicitly specified arguments in TARGS. */
20247 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
20248 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
20249 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
20250 if (!incomplete && CHECKING_P
20251 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
20252 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
20253 (targs, NUM_TMPL_ARGS (explicit_targs));
20254 }
20255
20256 if (return_type && strict != DEDUCE_CALL)
20257 {
20258 tree *new_args = XALLOCAVEC (tree, nargs + 1);
20259 new_args[0] = return_type;
20260 memcpy (new_args + 1, args, nargs * sizeof (tree));
20261 args = new_args;
20262 ++nargs;
20263 }
20264
20265 if (!incomplete)
20266 goto deduced;
20267
20268 /* Never do unification on the 'this' parameter. */
20269 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
20270
20271 if (return_type && strict == DEDUCE_CALL)
20272 {
20273 /* We're deducing for a call to the result of a template conversion
20274 function. The parms we really want are in return_type. */
20275 if (INDIRECT_TYPE_P (return_type))
20276 return_type = TREE_TYPE (return_type);
20277 parms = TYPE_ARG_TYPES (return_type);
20278 }
20279 else if (return_type)
20280 {
20281 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
20282 }
20283
20284 /* We allow incomplete unification without an error message here
20285 because the standard doesn't seem to explicitly prohibit it. Our
20286 callers must be ready to deal with unification failures in any
20287 event. */
20288
20289 /* If we aren't explaining yet, push tinst context so we can see where
20290 any errors (e.g. from class instantiations triggered by instantiation
20291 of default template arguments) come from. If we are explaining, this
20292 context is redundant. */
20293 if (!explain_p && !push_tinst_level (fn, targs))
20294 {
20295 excessive_deduction_depth = true;
20296 goto fail;
20297 }
20298
20299 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
20300 full_targs, parms, args, nargs, /*subr=*/0,
20301 strict, &checks, explain_p);
20302 if (!explain_p)
20303 pop_tinst_level ();
20304 if (!ok)
20305 goto fail;
20306
20307 /* Now that we have bindings for all of the template arguments,
20308 ensure that the arguments deduced for the template template
20309 parameters have compatible template parameter lists. We cannot
20310 check this property before we have deduced all template
20311 arguments, because the template parameter types of a template
20312 template parameter might depend on prior template parameters
20313 deduced after the template template parameter. The following
20314 ill-formed example illustrates this issue:
20315
20316 template<typename T, template<T> class C> void f(C<5>, T);
20317
20318 template<int N> struct X {};
20319
20320 void g() {
20321 f(X<5>(), 5l); // error: template argument deduction fails
20322 }
20323
20324 The template parameter list of 'C' depends on the template type
20325 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
20326 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
20327 time that we deduce 'C'. */
20328 if (!template_template_parm_bindings_ok_p
20329 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
20330 {
20331 unify_inconsistent_template_template_parameters (explain_p);
20332 goto fail;
20333 }
20334
20335 /* DR 1391: All parameters have args, now check non-dependent parms for
20336 convertibility. */
20337 if (check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
20338 convs, explain_p))
20339 goto fail;
20340
20341 deduced:
20342 /* All is well so far. Now, check:
20343
20344 [temp.deduct]
20345
20346 When all template arguments have been deduced, all uses of
20347 template parameters in nondeduced contexts are replaced with
20348 the corresponding deduced argument values. If the
20349 substitution results in an invalid type, as described above,
20350 type deduction fails. */
20351 if (!push_tinst_level (fn, targs))
20352 {
20353 excessive_deduction_depth = true;
20354 goto fail;
20355 }
20356
20357 /* Also collect access checks from the instantiation. */
20358 reopen_deferring_access_checks (checks);
20359
20360 decl = instantiate_template (fn, targs, complain);
20361
20362 checks = get_deferred_access_checks ();
20363 pop_deferring_access_checks ();
20364
20365 pop_tinst_level ();
20366
20367 if (decl == error_mark_node)
20368 goto fail;
20369
20370 /* Now perform any access checks encountered during substitution. */
20371 push_access_scope (decl);
20372 ok = perform_access_checks (checks, complain);
20373 pop_access_scope (decl);
20374 if (!ok)
20375 goto fail;
20376
20377 /* If we're looking for an exact match, check that what we got
20378 is indeed an exact match. It might not be if some template
20379 parameters are used in non-deduced contexts. But don't check
20380 for an exact match if we have dependent template arguments;
20381 in that case we're doing partial ordering, and we already know
20382 that we have two candidates that will provide the actual type. */
20383 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
20384 {
20385 tree substed = TREE_TYPE (decl);
20386 unsigned int i;
20387
20388 tree sarg
20389 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
20390 if (return_type)
20391 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
20392 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
20393 if (!same_type_p (args[i], TREE_VALUE (sarg)))
20394 {
20395 unify_type_mismatch (explain_p, args[i],
20396 TREE_VALUE (sarg));
20397 goto fail;
20398 }
20399 }
20400
20401 /* After doing deduction with the inherited constructor, actually return an
20402 instantiation of the inheriting constructor. */
20403 if (orig_fn != fn)
20404 decl = instantiate_template (orig_fn, targs, complain);
20405
20406 r = decl;
20407
20408 fail:
20409 --deduction_depth;
20410 if (excessive_deduction_depth)
20411 {
20412 if (deduction_depth == 0)
20413 /* Reset once we're all the way out. */
20414 excessive_deduction_depth = false;
20415 }
20416
20417 return r;
20418 }
20419
20420 /* Adjust types before performing type deduction, as described in
20421 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
20422 sections are symmetric. PARM is the type of a function parameter
20423 or the return type of the conversion function. ARG is the type of
20424 the argument passed to the call, or the type of the value
20425 initialized with the result of the conversion function.
20426 ARG_EXPR is the original argument expression, which may be null. */
20427
20428 static int
20429 maybe_adjust_types_for_deduction (unification_kind_t strict,
20430 tree* parm,
20431 tree* arg,
20432 tree arg_expr)
20433 {
20434 int result = 0;
20435
20436 switch (strict)
20437 {
20438 case DEDUCE_CALL:
20439 break;
20440
20441 case DEDUCE_CONV:
20442 /* Swap PARM and ARG throughout the remainder of this
20443 function; the handling is precisely symmetric since PARM
20444 will initialize ARG rather than vice versa. */
20445 std::swap (parm, arg);
20446 break;
20447
20448 case DEDUCE_EXACT:
20449 /* Core issue #873: Do the DR606 thing (see below) for these cases,
20450 too, but here handle it by stripping the reference from PARM
20451 rather than by adding it to ARG. */
20452 if (TYPE_REF_P (*parm)
20453 && TYPE_REF_IS_RVALUE (*parm)
20454 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
20455 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
20456 && TYPE_REF_P (*arg)
20457 && !TYPE_REF_IS_RVALUE (*arg))
20458 *parm = TREE_TYPE (*parm);
20459 /* Nothing else to do in this case. */
20460 return 0;
20461
20462 default:
20463 gcc_unreachable ();
20464 }
20465
20466 if (!TYPE_REF_P (*parm))
20467 {
20468 /* [temp.deduct.call]
20469
20470 If P is not a reference type:
20471
20472 --If A is an array type, the pointer type produced by the
20473 array-to-pointer standard conversion (_conv.array_) is
20474 used in place of A for type deduction; otherwise,
20475
20476 --If A is a function type, the pointer type produced by
20477 the function-to-pointer standard conversion
20478 (_conv.func_) is used in place of A for type deduction;
20479 otherwise,
20480
20481 --If A is a cv-qualified type, the top level
20482 cv-qualifiers of A's type are ignored for type
20483 deduction. */
20484 if (TREE_CODE (*arg) == ARRAY_TYPE)
20485 *arg = build_pointer_type (TREE_TYPE (*arg));
20486 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
20487 *arg = build_pointer_type (*arg);
20488 else
20489 *arg = TYPE_MAIN_VARIANT (*arg);
20490 }
20491
20492 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
20493 reference to a cv-unqualified template parameter that does not represent a
20494 template parameter of a class template (during class template argument
20495 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
20496 an lvalue, the type "lvalue reference to A" is used in place of A for type
20497 deduction. */
20498 if (TYPE_REF_P (*parm)
20499 && TYPE_REF_IS_RVALUE (*parm)
20500 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
20501 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
20502 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
20503 && (arg_expr ? lvalue_p (arg_expr)
20504 /* try_one_overload doesn't provide an arg_expr, but
20505 functions are always lvalues. */
20506 : TREE_CODE (*arg) == FUNCTION_TYPE))
20507 *arg = build_reference_type (*arg);
20508
20509 /* [temp.deduct.call]
20510
20511 If P is a cv-qualified type, the top level cv-qualifiers
20512 of P's type are ignored for type deduction. If P is a
20513 reference type, the type referred to by P is used for
20514 type deduction. */
20515 *parm = TYPE_MAIN_VARIANT (*parm);
20516 if (TYPE_REF_P (*parm))
20517 {
20518 *parm = TREE_TYPE (*parm);
20519 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
20520 }
20521
20522 /* DR 322. For conversion deduction, remove a reference type on parm
20523 too (which has been swapped into ARG). */
20524 if (strict == DEDUCE_CONV && TYPE_REF_P (*arg))
20525 *arg = TREE_TYPE (*arg);
20526
20527 return result;
20528 }
20529
20530 /* Subroutine of fn_type_unification. PARM is a function parameter of a
20531 template which doesn't contain any deducible template parameters; check if
20532 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
20533 unify_one_argument. */
20534
20535 static int
20536 check_non_deducible_conversion (tree parm, tree arg, int strict,
20537 int flags, struct conversion **conv_p,
20538 bool explain_p)
20539 {
20540 tree type;
20541
20542 if (!TYPE_P (arg))
20543 type = TREE_TYPE (arg);
20544 else
20545 type = arg;
20546
20547 if (same_type_p (parm, type))
20548 return unify_success (explain_p);
20549
20550 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
20551 if (strict == DEDUCE_CONV)
20552 {
20553 if (can_convert_arg (type, parm, NULL_TREE, flags, complain))
20554 return unify_success (explain_p);
20555 }
20556 else if (strict != DEDUCE_EXACT)
20557 {
20558 bool ok = false;
20559 tree conv_arg = TYPE_P (arg) ? NULL_TREE : arg;
20560 if (conv_p)
20561 /* Avoid recalculating this in add_function_candidate. */
20562 ok = (*conv_p
20563 = good_conversion (parm, type, conv_arg, flags, complain));
20564 else
20565 ok = can_convert_arg (parm, type, conv_arg, flags, complain);
20566 if (ok)
20567 return unify_success (explain_p);
20568 }
20569
20570 if (strict == DEDUCE_EXACT)
20571 return unify_type_mismatch (explain_p, parm, arg);
20572 else
20573 return unify_arg_conversion (explain_p, parm, type, arg);
20574 }
20575
20576 static bool uses_deducible_template_parms (tree type);
20577
20578 /* Returns true iff the expression EXPR is one from which a template
20579 argument can be deduced. In other words, if it's an undecorated
20580 use of a template non-type parameter. */
20581
20582 static bool
20583 deducible_expression (tree expr)
20584 {
20585 /* Strip implicit conversions. */
20586 while (CONVERT_EXPR_P (expr))
20587 expr = TREE_OPERAND (expr, 0);
20588 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
20589 }
20590
20591 /* Returns true iff the array domain DOMAIN uses a template parameter in a
20592 deducible way; that is, if it has a max value of <PARM> - 1. */
20593
20594 static bool
20595 deducible_array_bound (tree domain)
20596 {
20597 if (domain == NULL_TREE)
20598 return false;
20599
20600 tree max = TYPE_MAX_VALUE (domain);
20601 if (TREE_CODE (max) != MINUS_EXPR)
20602 return false;
20603
20604 return deducible_expression (TREE_OPERAND (max, 0));
20605 }
20606
20607 /* Returns true iff the template arguments ARGS use a template parameter
20608 in a deducible way. */
20609
20610 static bool
20611 deducible_template_args (tree args)
20612 {
20613 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
20614 {
20615 bool deducible;
20616 tree elt = TREE_VEC_ELT (args, i);
20617 if (ARGUMENT_PACK_P (elt))
20618 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
20619 else
20620 {
20621 if (PACK_EXPANSION_P (elt))
20622 elt = PACK_EXPANSION_PATTERN (elt);
20623 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
20624 deducible = true;
20625 else if (TYPE_P (elt))
20626 deducible = uses_deducible_template_parms (elt);
20627 else
20628 deducible = deducible_expression (elt);
20629 }
20630 if (deducible)
20631 return true;
20632 }
20633 return false;
20634 }
20635
20636 /* Returns true iff TYPE contains any deducible references to template
20637 parameters, as per 14.8.2.5. */
20638
20639 static bool
20640 uses_deducible_template_parms (tree type)
20641 {
20642 if (PACK_EXPANSION_P (type))
20643 type = PACK_EXPANSION_PATTERN (type);
20644
20645 /* T
20646 cv-list T
20647 TT<T>
20648 TT<i>
20649 TT<> */
20650 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
20651 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
20652 return true;
20653
20654 /* T*
20655 T&
20656 T&& */
20657 if (INDIRECT_TYPE_P (type))
20658 return uses_deducible_template_parms (TREE_TYPE (type));
20659
20660 /* T[integer-constant ]
20661 type [i] */
20662 if (TREE_CODE (type) == ARRAY_TYPE)
20663 return (uses_deducible_template_parms (TREE_TYPE (type))
20664 || deducible_array_bound (TYPE_DOMAIN (type)));
20665
20666 /* T type ::*
20667 type T::*
20668 T T::*
20669 T (type ::*)()
20670 type (T::*)()
20671 type (type ::*)(T)
20672 type (T::*)(T)
20673 T (type ::*)(T)
20674 T (T::*)()
20675 T (T::*)(T) */
20676 if (TYPE_PTRMEM_P (type))
20677 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
20678 || (uses_deducible_template_parms
20679 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
20680
20681 /* template-name <T> (where template-name refers to a class template)
20682 template-name <i> (where template-name refers to a class template) */
20683 if (CLASS_TYPE_P (type)
20684 && CLASSTYPE_TEMPLATE_INFO (type)
20685 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
20686 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
20687 (CLASSTYPE_TI_ARGS (type)));
20688
20689 /* type (T)
20690 T()
20691 T(T) */
20692 if (FUNC_OR_METHOD_TYPE_P (type))
20693 {
20694 if (uses_deducible_template_parms (TREE_TYPE (type)))
20695 return true;
20696 tree parm = TYPE_ARG_TYPES (type);
20697 if (TREE_CODE (type) == METHOD_TYPE)
20698 parm = TREE_CHAIN (parm);
20699 for (; parm; parm = TREE_CHAIN (parm))
20700 if (uses_deducible_template_parms (TREE_VALUE (parm)))
20701 return true;
20702 }
20703
20704 return false;
20705 }
20706
20707 /* Subroutine of type_unification_real and unify_pack_expansion to
20708 handle unification of a single P/A pair. Parameters are as
20709 for those functions. */
20710
20711 static int
20712 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
20713 int subr, unification_kind_t strict,
20714 bool explain_p)
20715 {
20716 tree arg_expr = NULL_TREE;
20717 int arg_strict;
20718
20719 if (arg == error_mark_node || parm == error_mark_node)
20720 return unify_invalid (explain_p);
20721 if (arg == unknown_type_node)
20722 /* We can't deduce anything from this, but we might get all the
20723 template args from other function args. */
20724 return unify_success (explain_p);
20725
20726 /* Implicit conversions (Clause 4) will be performed on a function
20727 argument to convert it to the type of the corresponding function
20728 parameter if the parameter type contains no template-parameters that
20729 participate in template argument deduction. */
20730 if (strict != DEDUCE_EXACT
20731 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
20732 /* For function parameters with no deducible template parameters,
20733 just return. We'll check non-dependent conversions later. */
20734 return unify_success (explain_p);
20735
20736 switch (strict)
20737 {
20738 case DEDUCE_CALL:
20739 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
20740 | UNIFY_ALLOW_MORE_CV_QUAL
20741 | UNIFY_ALLOW_DERIVED);
20742 break;
20743
20744 case DEDUCE_CONV:
20745 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
20746 break;
20747
20748 case DEDUCE_EXACT:
20749 arg_strict = UNIFY_ALLOW_NONE;
20750 break;
20751
20752 default:
20753 gcc_unreachable ();
20754 }
20755
20756 /* We only do these transformations if this is the top-level
20757 parameter_type_list in a call or declaration matching; in other
20758 situations (nested function declarators, template argument lists) we
20759 won't be comparing a type to an expression, and we don't do any type
20760 adjustments. */
20761 if (!subr)
20762 {
20763 if (!TYPE_P (arg))
20764 {
20765 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
20766 if (type_unknown_p (arg))
20767 {
20768 /* [temp.deduct.type] A template-argument can be
20769 deduced from a pointer to function or pointer
20770 to member function argument if the set of
20771 overloaded functions does not contain function
20772 templates and at most one of a set of
20773 overloaded functions provides a unique
20774 match. */
20775 resolve_overloaded_unification (tparms, targs, parm,
20776 arg, strict,
20777 arg_strict, explain_p);
20778 /* If a unique match was not found, this is a
20779 non-deduced context, so we still succeed. */
20780 return unify_success (explain_p);
20781 }
20782
20783 arg_expr = arg;
20784 arg = unlowered_expr_type (arg);
20785 if (arg == error_mark_node)
20786 return unify_invalid (explain_p);
20787 }
20788
20789 arg_strict |=
20790 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
20791 }
20792 else
20793 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
20794 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
20795 return unify_template_argument_mismatch (explain_p, parm, arg);
20796
20797 /* For deduction from an init-list we need the actual list. */
20798 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
20799 arg = arg_expr;
20800 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
20801 }
20802
20803 /* for_each_template_parm callback that always returns 0. */
20804
20805 static int
20806 zero_r (tree, void *)
20807 {
20808 return 0;
20809 }
20810
20811 /* for_each_template_parm any_fn callback to handle deduction of a template
20812 type argument from the type of an array bound. */
20813
20814 static int
20815 array_deduction_r (tree t, void *data)
20816 {
20817 tree_pair_p d = (tree_pair_p)data;
20818 tree &tparms = d->purpose;
20819 tree &targs = d->value;
20820
20821 if (TREE_CODE (t) == ARRAY_TYPE)
20822 if (tree dom = TYPE_DOMAIN (t))
20823 if (tree max = TYPE_MAX_VALUE (dom))
20824 {
20825 if (TREE_CODE (max) == MINUS_EXPR)
20826 max = TREE_OPERAND (max, 0);
20827 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
20828 unify (tparms, targs, TREE_TYPE (max), size_type_node,
20829 UNIFY_ALLOW_NONE, /*explain*/false);
20830 }
20831
20832 /* Keep walking. */
20833 return 0;
20834 }
20835
20836 /* Try to deduce any not-yet-deduced template type arguments from the type of
20837 an array bound. This is handled separately from unify because 14.8.2.5 says
20838 "The type of a type parameter is only deduced from an array bound if it is
20839 not otherwise deduced." */
20840
20841 static void
20842 try_array_deduction (tree tparms, tree targs, tree parm)
20843 {
20844 tree_pair_s data = { tparms, targs };
20845 hash_set<tree> visited;
20846 for_each_template_parm (parm, zero_r, &data, &visited,
20847 /*nondeduced*/false, array_deduction_r);
20848 }
20849
20850 /* Most parms like fn_type_unification.
20851
20852 If SUBR is 1, we're being called recursively (to unify the
20853 arguments of a function or method parameter of a function
20854 template).
20855
20856 CHECKS is a pointer to a vector of access checks encountered while
20857 substituting default template arguments. */
20858
20859 static int
20860 type_unification_real (tree tparms,
20861 tree full_targs,
20862 tree xparms,
20863 const tree *xargs,
20864 unsigned int xnargs,
20865 int subr,
20866 unification_kind_t strict,
20867 vec<deferred_access_check, va_gc> **checks,
20868 bool explain_p)
20869 {
20870 tree parm, arg;
20871 int i;
20872 int ntparms = TREE_VEC_LENGTH (tparms);
20873 int saw_undeduced = 0;
20874 tree parms;
20875 const tree *args;
20876 unsigned int nargs;
20877 unsigned int ia;
20878
20879 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
20880 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
20881 gcc_assert (ntparms > 0);
20882
20883 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
20884
20885 /* Reset the number of non-defaulted template arguments contained
20886 in TARGS. */
20887 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
20888
20889 again:
20890 parms = xparms;
20891 args = xargs;
20892 nargs = xnargs;
20893
20894 ia = 0;
20895 while (parms && parms != void_list_node
20896 && ia < nargs)
20897 {
20898 parm = TREE_VALUE (parms);
20899
20900 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
20901 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
20902 /* For a function parameter pack that occurs at the end of the
20903 parameter-declaration-list, the type A of each remaining
20904 argument of the call is compared with the type P of the
20905 declarator-id of the function parameter pack. */
20906 break;
20907
20908 parms = TREE_CHAIN (parms);
20909
20910 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
20911 /* For a function parameter pack that does not occur at the
20912 end of the parameter-declaration-list, the type of the
20913 parameter pack is a non-deduced context. */
20914 continue;
20915
20916 arg = args[ia];
20917 ++ia;
20918
20919 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
20920 explain_p))
20921 return 1;
20922 }
20923
20924 if (parms
20925 && parms != void_list_node
20926 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
20927 {
20928 /* Unify the remaining arguments with the pack expansion type. */
20929 tree argvec;
20930 tree parmvec = make_tree_vec (1);
20931
20932 /* Allocate a TREE_VEC and copy in all of the arguments */
20933 argvec = make_tree_vec (nargs - ia);
20934 for (i = 0; ia < nargs; ++ia, ++i)
20935 TREE_VEC_ELT (argvec, i) = args[ia];
20936
20937 /* Copy the parameter into parmvec. */
20938 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
20939 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
20940 /*subr=*/subr, explain_p))
20941 return 1;
20942
20943 /* Advance to the end of the list of parameters. */
20944 parms = TREE_CHAIN (parms);
20945 }
20946
20947 /* Fail if we've reached the end of the parm list, and more args
20948 are present, and the parm list isn't variadic. */
20949 if (ia < nargs && parms == void_list_node)
20950 return unify_too_many_arguments (explain_p, nargs, ia);
20951 /* Fail if parms are left and they don't have default values and
20952 they aren't all deduced as empty packs (c++/57397). This is
20953 consistent with sufficient_parms_p. */
20954 if (parms && parms != void_list_node
20955 && TREE_PURPOSE (parms) == NULL_TREE)
20956 {
20957 unsigned int count = nargs;
20958 tree p = parms;
20959 bool type_pack_p;
20960 do
20961 {
20962 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
20963 if (!type_pack_p)
20964 count++;
20965 p = TREE_CHAIN (p);
20966 }
20967 while (p && p != void_list_node);
20968 if (count != nargs)
20969 return unify_too_few_arguments (explain_p, ia, count,
20970 type_pack_p);
20971 }
20972
20973 if (!subr)
20974 {
20975 tsubst_flags_t complain = (explain_p
20976 ? tf_warning_or_error
20977 : tf_none);
20978 bool tried_array_deduction = (cxx_dialect < cxx17);
20979
20980 for (i = 0; i < ntparms; i++)
20981 {
20982 tree targ = TREE_VEC_ELT (targs, i);
20983 tree tparm = TREE_VEC_ELT (tparms, i);
20984
20985 /* Clear the "incomplete" flags on all argument packs now so that
20986 substituting them into later default arguments works. */
20987 if (targ && ARGUMENT_PACK_P (targ))
20988 {
20989 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
20990 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
20991 }
20992
20993 if (targ || tparm == error_mark_node)
20994 continue;
20995 tparm = TREE_VALUE (tparm);
20996
20997 if (TREE_CODE (tparm) == TYPE_DECL
20998 && !tried_array_deduction)
20999 {
21000 try_array_deduction (tparms, targs, xparms);
21001 tried_array_deduction = true;
21002 if (TREE_VEC_ELT (targs, i))
21003 continue;
21004 }
21005
21006 /* If this is an undeduced nontype parameter that depends on
21007 a type parameter, try another pass; its type may have been
21008 deduced from a later argument than the one from which
21009 this parameter can be deduced. */
21010 if (TREE_CODE (tparm) == PARM_DECL
21011 && uses_template_parms (TREE_TYPE (tparm))
21012 && saw_undeduced < 2)
21013 {
21014 saw_undeduced = 1;
21015 continue;
21016 }
21017
21018 /* Core issue #226 (C++0x) [temp.deduct]:
21019
21020 If a template argument has not been deduced, its
21021 default template argument, if any, is used.
21022
21023 When we are in C++98 mode, TREE_PURPOSE will either
21024 be NULL_TREE or ERROR_MARK_NODE, so we do not need
21025 to explicitly check cxx_dialect here. */
21026 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
21027 /* OK, there is a default argument. Wait until after the
21028 conversion check to do substitution. */
21029 continue;
21030
21031 /* If the type parameter is a parameter pack, then it will
21032 be deduced to an empty parameter pack. */
21033 if (template_parameter_pack_p (tparm))
21034 {
21035 tree arg;
21036
21037 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
21038 {
21039 arg = make_node (NONTYPE_ARGUMENT_PACK);
21040 TREE_CONSTANT (arg) = 1;
21041 }
21042 else
21043 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
21044
21045 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
21046
21047 TREE_VEC_ELT (targs, i) = arg;
21048 continue;
21049 }
21050
21051 return unify_parameter_deduction_failure (explain_p, tparm);
21052 }
21053
21054 /* Now substitute into the default template arguments. */
21055 for (i = 0; i < ntparms; i++)
21056 {
21057 tree targ = TREE_VEC_ELT (targs, i);
21058 tree tparm = TREE_VEC_ELT (tparms, i);
21059
21060 if (targ || tparm == error_mark_node)
21061 continue;
21062 tree parm = TREE_VALUE (tparm);
21063 tree arg = TREE_PURPOSE (tparm);
21064 reopen_deferring_access_checks (*checks);
21065 location_t save_loc = input_location;
21066 if (DECL_P (parm))
21067 input_location = DECL_SOURCE_LOCATION (parm);
21068
21069 if (saw_undeduced == 1
21070 && TREE_CODE (parm) == PARM_DECL
21071 && uses_template_parms (TREE_TYPE (parm)))
21072 {
21073 /* The type of this non-type parameter depends on undeduced
21074 parameters. Don't try to use its default argument yet,
21075 since we might deduce an argument for it on the next pass,
21076 but do check whether the arguments we already have cause
21077 substitution failure, so that that happens before we try
21078 later default arguments (78489). */
21079 ++processing_template_decl;
21080 tree type = tsubst (TREE_TYPE (parm), full_targs, complain,
21081 NULL_TREE);
21082 --processing_template_decl;
21083 if (type == error_mark_node)
21084 arg = error_mark_node;
21085 else
21086 arg = NULL_TREE;
21087 }
21088 else
21089 {
21090 /* Even if the call is happening in template context, getting
21091 here means it's non-dependent, and a default argument is
21092 considered a separate definition under [temp.decls], so we can
21093 do this substitution without processing_template_decl. This
21094 is important if the default argument contains something that
21095 might be instantiation-dependent like access (87480). */
21096 processing_template_decl_sentinel s;
21097 tree substed = NULL_TREE;
21098 if (saw_undeduced == 1)
21099 {
21100 /* First instatiate in template context, in case we still
21101 depend on undeduced template parameters. */
21102 ++processing_template_decl;
21103 substed = tsubst_template_arg (arg, full_targs, complain,
21104 NULL_TREE);
21105 --processing_template_decl;
21106 if (substed != error_mark_node
21107 && !uses_template_parms (substed))
21108 /* We replaced all the tparms, substitute again out of
21109 template context. */
21110 substed = NULL_TREE;
21111 }
21112 if (!substed)
21113 substed = tsubst_template_arg (arg, full_targs, complain,
21114 NULL_TREE);
21115
21116 if (!uses_template_parms (substed))
21117 arg = convert_template_argument (parm, substed, full_targs,
21118 complain, i, NULL_TREE);
21119 else if (saw_undeduced == 1)
21120 arg = NULL_TREE;
21121 else
21122 arg = error_mark_node;
21123 }
21124
21125 input_location = save_loc;
21126 *checks = get_deferred_access_checks ();
21127 pop_deferring_access_checks ();
21128
21129 if (arg == error_mark_node)
21130 return 1;
21131 else if (arg)
21132 {
21133 TREE_VEC_ELT (targs, i) = arg;
21134 /* The position of the first default template argument,
21135 is also the number of non-defaulted arguments in TARGS.
21136 Record that. */
21137 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
21138 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
21139 }
21140 }
21141
21142 if (saw_undeduced++ == 1)
21143 goto again;
21144 }
21145
21146 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
21147 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
21148
21149 return unify_success (explain_p);
21150 }
21151
21152 /* Subroutine of type_unification_real. Args are like the variables
21153 at the call site. ARG is an overloaded function (or template-id);
21154 we try deducing template args from each of the overloads, and if
21155 only one succeeds, we go with that. Modifies TARGS and returns
21156 true on success. */
21157
21158 static bool
21159 resolve_overloaded_unification (tree tparms,
21160 tree targs,
21161 tree parm,
21162 tree arg,
21163 unification_kind_t strict,
21164 int sub_strict,
21165 bool explain_p)
21166 {
21167 tree tempargs = copy_node (targs);
21168 int good = 0;
21169 tree goodfn = NULL_TREE;
21170 bool addr_p;
21171
21172 if (TREE_CODE (arg) == ADDR_EXPR)
21173 {
21174 arg = TREE_OPERAND (arg, 0);
21175 addr_p = true;
21176 }
21177 else
21178 addr_p = false;
21179
21180 if (TREE_CODE (arg) == COMPONENT_REF)
21181 /* Handle `&x' where `x' is some static or non-static member
21182 function name. */
21183 arg = TREE_OPERAND (arg, 1);
21184
21185 if (TREE_CODE (arg) == OFFSET_REF)
21186 arg = TREE_OPERAND (arg, 1);
21187
21188 /* Strip baselink information. */
21189 if (BASELINK_P (arg))
21190 arg = BASELINK_FUNCTIONS (arg);
21191
21192 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
21193 {
21194 /* If we got some explicit template args, we need to plug them into
21195 the affected templates before we try to unify, in case the
21196 explicit args will completely resolve the templates in question. */
21197
21198 int ok = 0;
21199 tree expl_subargs = TREE_OPERAND (arg, 1);
21200 arg = TREE_OPERAND (arg, 0);
21201
21202 for (lkp_iterator iter (arg); iter; ++iter)
21203 {
21204 tree fn = *iter;
21205 tree subargs, elem;
21206
21207 if (TREE_CODE (fn) != TEMPLATE_DECL)
21208 continue;
21209
21210 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
21211 expl_subargs, NULL_TREE, tf_none,
21212 /*require_all_args=*/true,
21213 /*use_default_args=*/true);
21214 if (subargs != error_mark_node
21215 && !any_dependent_template_arguments_p (subargs))
21216 {
21217 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
21218 if (try_one_overload (tparms, targs, tempargs, parm,
21219 elem, strict, sub_strict, addr_p, explain_p)
21220 && (!goodfn || !same_type_p (goodfn, elem)))
21221 {
21222 goodfn = elem;
21223 ++good;
21224 }
21225 }
21226 else if (subargs)
21227 ++ok;
21228 }
21229 /* If no templates (or more than one) are fully resolved by the
21230 explicit arguments, this template-id is a non-deduced context; it
21231 could still be OK if we deduce all template arguments for the
21232 enclosing call through other arguments. */
21233 if (good != 1)
21234 good = ok;
21235 }
21236 else if (!OVL_P (arg))
21237 /* If ARG is, for example, "(0, &f)" then its type will be unknown
21238 -- but the deduction does not succeed because the expression is
21239 not just the function on its own. */
21240 return false;
21241 else
21242 for (lkp_iterator iter (arg); iter; ++iter)
21243 {
21244 tree fn = *iter;
21245 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
21246 strict, sub_strict, addr_p, explain_p)
21247 && (!goodfn || !decls_match (goodfn, fn)))
21248 {
21249 goodfn = fn;
21250 ++good;
21251 }
21252 }
21253
21254 /* [temp.deduct.type] A template-argument can be deduced from a pointer
21255 to function or pointer to member function argument if the set of
21256 overloaded functions does not contain function templates and at most
21257 one of a set of overloaded functions provides a unique match.
21258
21259 So if we found multiple possibilities, we return success but don't
21260 deduce anything. */
21261
21262 if (good == 1)
21263 {
21264 int i = TREE_VEC_LENGTH (targs);
21265 for (; i--; )
21266 if (TREE_VEC_ELT (tempargs, i))
21267 {
21268 tree old = TREE_VEC_ELT (targs, i);
21269 tree new_ = TREE_VEC_ELT (tempargs, i);
21270 if (new_ && old && ARGUMENT_PACK_P (old)
21271 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
21272 /* Don't forget explicit template arguments in a pack. */
21273 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
21274 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
21275 TREE_VEC_ELT (targs, i) = new_;
21276 }
21277 }
21278 if (good)
21279 return true;
21280
21281 return false;
21282 }
21283
21284 /* Core DR 115: In contexts where deduction is done and fails, or in
21285 contexts where deduction is not done, if a template argument list is
21286 specified and it, along with any default template arguments, identifies
21287 a single function template specialization, then the template-id is an
21288 lvalue for the function template specialization. */
21289
21290 tree
21291 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
21292 {
21293 tree expr, offset, baselink;
21294 bool addr;
21295
21296 if (!type_unknown_p (orig_expr))
21297 return orig_expr;
21298
21299 expr = orig_expr;
21300 addr = false;
21301 offset = NULL_TREE;
21302 baselink = NULL_TREE;
21303
21304 if (TREE_CODE (expr) == ADDR_EXPR)
21305 {
21306 expr = TREE_OPERAND (expr, 0);
21307 addr = true;
21308 }
21309 if (TREE_CODE (expr) == OFFSET_REF)
21310 {
21311 offset = expr;
21312 expr = TREE_OPERAND (expr, 1);
21313 }
21314 if (BASELINK_P (expr))
21315 {
21316 baselink = expr;
21317 expr = BASELINK_FUNCTIONS (expr);
21318 }
21319
21320 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
21321 {
21322 int good = 0;
21323 tree goodfn = NULL_TREE;
21324
21325 /* If we got some explicit template args, we need to plug them into
21326 the affected templates before we try to unify, in case the
21327 explicit args will completely resolve the templates in question. */
21328
21329 tree expl_subargs = TREE_OPERAND (expr, 1);
21330 tree arg = TREE_OPERAND (expr, 0);
21331 tree badfn = NULL_TREE;
21332 tree badargs = NULL_TREE;
21333
21334 for (lkp_iterator iter (arg); iter; ++iter)
21335 {
21336 tree fn = *iter;
21337 tree subargs, elem;
21338
21339 if (TREE_CODE (fn) != TEMPLATE_DECL)
21340 continue;
21341
21342 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
21343 expl_subargs, NULL_TREE, tf_none,
21344 /*require_all_args=*/true,
21345 /*use_default_args=*/true);
21346 if (subargs != error_mark_node
21347 && !any_dependent_template_arguments_p (subargs))
21348 {
21349 elem = instantiate_template (fn, subargs, tf_none);
21350 if (elem == error_mark_node)
21351 {
21352 badfn = fn;
21353 badargs = subargs;
21354 }
21355 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
21356 {
21357 goodfn = elem;
21358 ++good;
21359 }
21360 }
21361 }
21362 if (good == 1)
21363 {
21364 mark_used (goodfn);
21365 expr = goodfn;
21366 if (baselink)
21367 expr = build_baselink (BASELINK_BINFO (baselink),
21368 BASELINK_ACCESS_BINFO (baselink),
21369 expr, BASELINK_OPTYPE (baselink));
21370 if (offset)
21371 {
21372 tree base
21373 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
21374 expr = build_offset_ref (base, expr, addr, complain);
21375 }
21376 if (addr)
21377 expr = cp_build_addr_expr (expr, complain);
21378 return expr;
21379 }
21380 else if (good == 0 && badargs && (complain & tf_error))
21381 /* There were no good options and at least one bad one, so let the
21382 user know what the problem is. */
21383 instantiate_template (badfn, badargs, complain);
21384 }
21385 return orig_expr;
21386 }
21387
21388 /* As above, but error out if the expression remains overloaded. */
21389
21390 tree
21391 resolve_nondeduced_context_or_error (tree exp, tsubst_flags_t complain)
21392 {
21393 exp = resolve_nondeduced_context (exp, complain);
21394 if (type_unknown_p (exp))
21395 {
21396 if (complain & tf_error)
21397 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
21398 return error_mark_node;
21399 }
21400 return exp;
21401 }
21402
21403 /* Subroutine of resolve_overloaded_unification; does deduction for a single
21404 overload. Fills TARGS with any deduced arguments, or error_mark_node if
21405 different overloads deduce different arguments for a given parm.
21406 ADDR_P is true if the expression for which deduction is being
21407 performed was of the form "& fn" rather than simply "fn".
21408
21409 Returns 1 on success. */
21410
21411 static int
21412 try_one_overload (tree tparms,
21413 tree orig_targs,
21414 tree targs,
21415 tree parm,
21416 tree arg,
21417 unification_kind_t strict,
21418 int sub_strict,
21419 bool addr_p,
21420 bool explain_p)
21421 {
21422 int nargs;
21423 tree tempargs;
21424 int i;
21425
21426 if (arg == error_mark_node)
21427 return 0;
21428
21429 /* [temp.deduct.type] A template-argument can be deduced from a pointer
21430 to function or pointer to member function argument if the set of
21431 overloaded functions does not contain function templates and at most
21432 one of a set of overloaded functions provides a unique match.
21433
21434 So if this is a template, just return success. */
21435
21436 if (uses_template_parms (arg))
21437 return 1;
21438
21439 if (TREE_CODE (arg) == METHOD_TYPE)
21440 arg = build_ptrmemfunc_type (build_pointer_type (arg));
21441 else if (addr_p)
21442 arg = build_pointer_type (arg);
21443
21444 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
21445
21446 /* We don't copy orig_targs for this because if we have already deduced
21447 some template args from previous args, unify would complain when we
21448 try to deduce a template parameter for the same argument, even though
21449 there isn't really a conflict. */
21450 nargs = TREE_VEC_LENGTH (targs);
21451 tempargs = make_tree_vec (nargs);
21452
21453 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
21454 return 0;
21455
21456 /* First make sure we didn't deduce anything that conflicts with
21457 explicitly specified args. */
21458 for (i = nargs; i--; )
21459 {
21460 tree elt = TREE_VEC_ELT (tempargs, i);
21461 tree oldelt = TREE_VEC_ELT (orig_targs, i);
21462
21463 if (!elt)
21464 /*NOP*/;
21465 else if (uses_template_parms (elt))
21466 /* Since we're unifying against ourselves, we will fill in
21467 template args used in the function parm list with our own
21468 template parms. Discard them. */
21469 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
21470 else if (oldelt && ARGUMENT_PACK_P (oldelt))
21471 {
21472 /* Check that the argument at each index of the deduced argument pack
21473 is equivalent to the corresponding explicitly specified argument.
21474 We may have deduced more arguments than were explicitly specified,
21475 and that's OK. */
21476
21477 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
21478 that's wrong if we deduce the same argument pack from multiple
21479 function arguments: it's only incomplete the first time. */
21480
21481 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
21482 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
21483
21484 if (TREE_VEC_LENGTH (deduced_pack)
21485 < TREE_VEC_LENGTH (explicit_pack))
21486 return 0;
21487
21488 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
21489 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
21490 TREE_VEC_ELT (deduced_pack, j)))
21491 return 0;
21492 }
21493 else if (oldelt && !template_args_equal (oldelt, elt))
21494 return 0;
21495 }
21496
21497 for (i = nargs; i--; )
21498 {
21499 tree elt = TREE_VEC_ELT (tempargs, i);
21500
21501 if (elt)
21502 TREE_VEC_ELT (targs, i) = elt;
21503 }
21504
21505 return 1;
21506 }
21507
21508 /* PARM is a template class (perhaps with unbound template
21509 parameters). ARG is a fully instantiated type. If ARG can be
21510 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
21511 TARGS are as for unify. */
21512
21513 static tree
21514 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
21515 bool explain_p)
21516 {
21517 tree copy_of_targs;
21518
21519 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
21520 return NULL_TREE;
21521 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21522 /* Matches anything. */;
21523 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
21524 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
21525 return NULL_TREE;
21526
21527 /* We need to make a new template argument vector for the call to
21528 unify. If we used TARGS, we'd clutter it up with the result of
21529 the attempted unification, even if this class didn't work out.
21530 We also don't want to commit ourselves to all the unifications
21531 we've already done, since unification is supposed to be done on
21532 an argument-by-argument basis. In other words, consider the
21533 following pathological case:
21534
21535 template <int I, int J, int K>
21536 struct S {};
21537
21538 template <int I, int J>
21539 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
21540
21541 template <int I, int J, int K>
21542 void f(S<I, J, K>, S<I, I, I>);
21543
21544 void g() {
21545 S<0, 0, 0> s0;
21546 S<0, 1, 2> s2;
21547
21548 f(s0, s2);
21549 }
21550
21551 Now, by the time we consider the unification involving `s2', we
21552 already know that we must have `f<0, 0, 0>'. But, even though
21553 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
21554 because there are two ways to unify base classes of S<0, 1, 2>
21555 with S<I, I, I>. If we kept the already deduced knowledge, we
21556 would reject the possibility I=1. */
21557 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
21558
21559 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21560 {
21561 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
21562 return NULL_TREE;
21563 return arg;
21564 }
21565
21566 /* If unification failed, we're done. */
21567 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
21568 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
21569 return NULL_TREE;
21570
21571 return arg;
21572 }
21573
21574 /* Given a template type PARM and a class type ARG, find the unique
21575 base type in ARG that is an instance of PARM. We do not examine
21576 ARG itself; only its base-classes. If there is not exactly one
21577 appropriate base class, return NULL_TREE. PARM may be the type of
21578 a partial specialization, as well as a plain template type. Used
21579 by unify. */
21580
21581 static enum template_base_result
21582 get_template_base (tree tparms, tree targs, tree parm, tree arg,
21583 bool explain_p, tree *result)
21584 {
21585 tree rval = NULL_TREE;
21586 tree binfo;
21587
21588 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
21589
21590 binfo = TYPE_BINFO (complete_type (arg));
21591 if (!binfo)
21592 {
21593 /* The type could not be completed. */
21594 *result = NULL_TREE;
21595 return tbr_incomplete_type;
21596 }
21597
21598 /* Walk in inheritance graph order. The search order is not
21599 important, and this avoids multiple walks of virtual bases. */
21600 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
21601 {
21602 tree r = try_class_unification (tparms, targs, parm,
21603 BINFO_TYPE (binfo), explain_p);
21604
21605 if (r)
21606 {
21607 /* If there is more than one satisfactory baseclass, then:
21608
21609 [temp.deduct.call]
21610
21611 If they yield more than one possible deduced A, the type
21612 deduction fails.
21613
21614 applies. */
21615 if (rval && !same_type_p (r, rval))
21616 {
21617 *result = NULL_TREE;
21618 return tbr_ambiguous_baseclass;
21619 }
21620
21621 rval = r;
21622 }
21623 }
21624
21625 *result = rval;
21626 return tbr_success;
21627 }
21628
21629 /* Returns the level of DECL, which declares a template parameter. */
21630
21631 static int
21632 template_decl_level (tree decl)
21633 {
21634 switch (TREE_CODE (decl))
21635 {
21636 case TYPE_DECL:
21637 case TEMPLATE_DECL:
21638 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
21639
21640 case PARM_DECL:
21641 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
21642
21643 default:
21644 gcc_unreachable ();
21645 }
21646 return 0;
21647 }
21648
21649 /* Decide whether ARG can be unified with PARM, considering only the
21650 cv-qualifiers of each type, given STRICT as documented for unify.
21651 Returns nonzero iff the unification is OK on that basis. */
21652
21653 static int
21654 check_cv_quals_for_unify (int strict, tree arg, tree parm)
21655 {
21656 int arg_quals = cp_type_quals (arg);
21657 int parm_quals = cp_type_quals (parm);
21658
21659 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
21660 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
21661 {
21662 /* Although a CVR qualifier is ignored when being applied to a
21663 substituted template parameter ([8.3.2]/1 for example), that
21664 does not allow us to unify "const T" with "int&" because both
21665 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
21666 It is ok when we're allowing additional CV qualifiers
21667 at the outer level [14.8.2.1]/3,1st bullet. */
21668 if ((TYPE_REF_P (arg)
21669 || FUNC_OR_METHOD_TYPE_P (arg))
21670 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
21671 return 0;
21672
21673 if ((!INDIRECT_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
21674 && (parm_quals & TYPE_QUAL_RESTRICT))
21675 return 0;
21676 }
21677
21678 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
21679 && (arg_quals & parm_quals) != parm_quals)
21680 return 0;
21681
21682 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
21683 && (parm_quals & arg_quals) != arg_quals)
21684 return 0;
21685
21686 return 1;
21687 }
21688
21689 /* Determines the LEVEL and INDEX for the template parameter PARM. */
21690 void
21691 template_parm_level_and_index (tree parm, int* level, int* index)
21692 {
21693 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
21694 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
21695 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21696 {
21697 *index = TEMPLATE_TYPE_IDX (parm);
21698 *level = TEMPLATE_TYPE_LEVEL (parm);
21699 }
21700 else
21701 {
21702 *index = TEMPLATE_PARM_IDX (parm);
21703 *level = TEMPLATE_PARM_LEVEL (parm);
21704 }
21705 }
21706
21707 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
21708 do { \
21709 if (unify (TP, TA, P, A, S, EP)) \
21710 return 1; \
21711 } while (0)
21712
21713 /* Unifies the remaining arguments in PACKED_ARGS with the pack
21714 expansion at the end of PACKED_PARMS. Returns 0 if the type
21715 deduction succeeds, 1 otherwise. STRICT is the same as in
21716 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
21717 function call argument list. We'll need to adjust the arguments to make them
21718 types. SUBR tells us if this is from a recursive call to
21719 type_unification_real, or for comparing two template argument
21720 lists. */
21721
21722 static int
21723 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
21724 tree packed_args, unification_kind_t strict,
21725 bool subr, bool explain_p)
21726 {
21727 tree parm
21728 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
21729 tree pattern = PACK_EXPANSION_PATTERN (parm);
21730 tree pack, packs = NULL_TREE;
21731 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
21732
21733 /* Add in any args remembered from an earlier partial instantiation. */
21734 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
21735 int levels = TMPL_ARGS_DEPTH (targs);
21736
21737 packed_args = expand_template_argument_pack (packed_args);
21738
21739 int len = TREE_VEC_LENGTH (packed_args);
21740
21741 /* Determine the parameter packs we will be deducing from the
21742 pattern, and record their current deductions. */
21743 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
21744 pack; pack = TREE_CHAIN (pack))
21745 {
21746 tree parm_pack = TREE_VALUE (pack);
21747 int idx, level;
21748
21749 /* Only template parameter packs can be deduced, not e.g. function
21750 parameter packs or __bases or __integer_pack. */
21751 if (!TEMPLATE_PARM_P (parm_pack))
21752 continue;
21753
21754 /* Determine the index and level of this parameter pack. */
21755 template_parm_level_and_index (parm_pack, &level, &idx);
21756 if (level < levels)
21757 continue;
21758
21759 /* Keep track of the parameter packs and their corresponding
21760 argument packs. */
21761 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
21762 TREE_TYPE (packs) = make_tree_vec (len - start);
21763 }
21764
21765 /* Loop through all of the arguments that have not yet been
21766 unified and unify each with the pattern. */
21767 for (i = start; i < len; i++)
21768 {
21769 tree parm;
21770 bool any_explicit = false;
21771 tree arg = TREE_VEC_ELT (packed_args, i);
21772
21773 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
21774 or the element of its argument pack at the current index if
21775 this argument was explicitly specified. */
21776 for (pack = packs; pack; pack = TREE_CHAIN (pack))
21777 {
21778 int idx, level;
21779 tree arg, pargs;
21780 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
21781
21782 arg = NULL_TREE;
21783 if (TREE_VALUE (pack)
21784 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
21785 && (i - start < TREE_VEC_LENGTH (pargs)))
21786 {
21787 any_explicit = true;
21788 arg = TREE_VEC_ELT (pargs, i - start);
21789 }
21790 TMPL_ARG (targs, level, idx) = arg;
21791 }
21792
21793 /* If we had explicit template arguments, substitute them into the
21794 pattern before deduction. */
21795 if (any_explicit)
21796 {
21797 /* Some arguments might still be unspecified or dependent. */
21798 bool dependent;
21799 ++processing_template_decl;
21800 dependent = any_dependent_template_arguments_p (targs);
21801 if (!dependent)
21802 --processing_template_decl;
21803 parm = tsubst (pattern, targs,
21804 explain_p ? tf_warning_or_error : tf_none,
21805 NULL_TREE);
21806 if (dependent)
21807 --processing_template_decl;
21808 if (parm == error_mark_node)
21809 return 1;
21810 }
21811 else
21812 parm = pattern;
21813
21814 /* Unify the pattern with the current argument. */
21815 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
21816 explain_p))
21817 return 1;
21818
21819 /* For each parameter pack, collect the deduced value. */
21820 for (pack = packs; pack; pack = TREE_CHAIN (pack))
21821 {
21822 int idx, level;
21823 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
21824
21825 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
21826 TMPL_ARG (targs, level, idx);
21827 }
21828 }
21829
21830 /* Verify that the results of unification with the parameter packs
21831 produce results consistent with what we've seen before, and make
21832 the deduced argument packs available. */
21833 for (pack = packs; pack; pack = TREE_CHAIN (pack))
21834 {
21835 tree old_pack = TREE_VALUE (pack);
21836 tree new_args = TREE_TYPE (pack);
21837 int i, len = TREE_VEC_LENGTH (new_args);
21838 int idx, level;
21839 bool nondeduced_p = false;
21840
21841 /* By default keep the original deduced argument pack.
21842 If necessary, more specific code is going to update the
21843 resulting deduced argument later down in this function. */
21844 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
21845 TMPL_ARG (targs, level, idx) = old_pack;
21846
21847 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
21848 actually deduce anything. */
21849 for (i = 0; i < len && !nondeduced_p; ++i)
21850 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
21851 nondeduced_p = true;
21852 if (nondeduced_p)
21853 continue;
21854
21855 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
21856 {
21857 /* If we had fewer function args than explicit template args,
21858 just use the explicits. */
21859 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
21860 int explicit_len = TREE_VEC_LENGTH (explicit_args);
21861 if (len < explicit_len)
21862 new_args = explicit_args;
21863 }
21864
21865 if (!old_pack)
21866 {
21867 tree result;
21868 /* Build the deduced *_ARGUMENT_PACK. */
21869 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
21870 {
21871 result = make_node (NONTYPE_ARGUMENT_PACK);
21872 TREE_CONSTANT (result) = 1;
21873 }
21874 else
21875 result = cxx_make_type (TYPE_ARGUMENT_PACK);
21876
21877 SET_ARGUMENT_PACK_ARGS (result, new_args);
21878
21879 /* Note the deduced argument packs for this parameter
21880 pack. */
21881 TMPL_ARG (targs, level, idx) = result;
21882 }
21883 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
21884 && (ARGUMENT_PACK_ARGS (old_pack)
21885 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
21886 {
21887 /* We only had the explicitly-provided arguments before, but
21888 now we have a complete set of arguments. */
21889 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
21890
21891 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
21892 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
21893 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
21894 }
21895 else
21896 {
21897 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
21898 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
21899
21900 if (!comp_template_args (old_args, new_args,
21901 &bad_old_arg, &bad_new_arg))
21902 /* Inconsistent unification of this parameter pack. */
21903 return unify_parameter_pack_inconsistent (explain_p,
21904 bad_old_arg,
21905 bad_new_arg);
21906 }
21907 }
21908
21909 return unify_success (explain_p);
21910 }
21911
21912 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
21913 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
21914 parameters and return value are as for unify. */
21915
21916 static int
21917 unify_array_domain (tree tparms, tree targs,
21918 tree parm_dom, tree arg_dom,
21919 bool explain_p)
21920 {
21921 tree parm_max;
21922 tree arg_max;
21923 bool parm_cst;
21924 bool arg_cst;
21925
21926 /* Our representation of array types uses "N - 1" as the
21927 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
21928 not an integer constant. We cannot unify arbitrarily
21929 complex expressions, so we eliminate the MINUS_EXPRs
21930 here. */
21931 parm_max = TYPE_MAX_VALUE (parm_dom);
21932 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
21933 if (!parm_cst)
21934 {
21935 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
21936 parm_max = TREE_OPERAND (parm_max, 0);
21937 }
21938 arg_max = TYPE_MAX_VALUE (arg_dom);
21939 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
21940 if (!arg_cst)
21941 {
21942 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
21943 trying to unify the type of a variable with the type
21944 of a template parameter. For example:
21945
21946 template <unsigned int N>
21947 void f (char (&) [N]);
21948 int g();
21949 void h(int i) {
21950 char a[g(i)];
21951 f(a);
21952 }
21953
21954 Here, the type of the ARG will be "int [g(i)]", and
21955 may be a SAVE_EXPR, etc. */
21956 if (TREE_CODE (arg_max) != MINUS_EXPR)
21957 return unify_vla_arg (explain_p, arg_dom);
21958 arg_max = TREE_OPERAND (arg_max, 0);
21959 }
21960
21961 /* If only one of the bounds used a MINUS_EXPR, compensate
21962 by adding one to the other bound. */
21963 if (parm_cst && !arg_cst)
21964 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
21965 integer_type_node,
21966 parm_max,
21967 integer_one_node);
21968 else if (arg_cst && !parm_cst)
21969 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
21970 integer_type_node,
21971 arg_max,
21972 integer_one_node);
21973
21974 return unify (tparms, targs, parm_max, arg_max,
21975 UNIFY_ALLOW_INTEGER, explain_p);
21976 }
21977
21978 /* Returns whether T, a P or A in unify, is a type, template or expression. */
21979
21980 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
21981
21982 static pa_kind_t
21983 pa_kind (tree t)
21984 {
21985 if (PACK_EXPANSION_P (t))
21986 t = PACK_EXPANSION_PATTERN (t);
21987 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
21988 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
21989 || DECL_TYPE_TEMPLATE_P (t))
21990 return pa_tmpl;
21991 else if (TYPE_P (t))
21992 return pa_type;
21993 else
21994 return pa_expr;
21995 }
21996
21997 /* Deduce the value of template parameters. TPARMS is the (innermost)
21998 set of template parameters to a template. TARGS is the bindings
21999 for those template parameters, as determined thus far; TARGS may
22000 include template arguments for outer levels of template parameters
22001 as well. PARM is a parameter to a template function, or a
22002 subcomponent of that parameter; ARG is the corresponding argument.
22003 This function attempts to match PARM with ARG in a manner
22004 consistent with the existing assignments in TARGS. If more values
22005 are deduced, then TARGS is updated.
22006
22007 Returns 0 if the type deduction succeeds, 1 otherwise. The
22008 parameter STRICT is a bitwise or of the following flags:
22009
22010 UNIFY_ALLOW_NONE:
22011 Require an exact match between PARM and ARG.
22012 UNIFY_ALLOW_MORE_CV_QUAL:
22013 Allow the deduced ARG to be more cv-qualified (by qualification
22014 conversion) than ARG.
22015 UNIFY_ALLOW_LESS_CV_QUAL:
22016 Allow the deduced ARG to be less cv-qualified than ARG.
22017 UNIFY_ALLOW_DERIVED:
22018 Allow the deduced ARG to be a template base class of ARG,
22019 or a pointer to a template base class of the type pointed to by
22020 ARG.
22021 UNIFY_ALLOW_INTEGER:
22022 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
22023 case for more information.
22024 UNIFY_ALLOW_OUTER_LEVEL:
22025 This is the outermost level of a deduction. Used to determine validity
22026 of qualification conversions. A valid qualification conversion must
22027 have const qualified pointers leading up to the inner type which
22028 requires additional CV quals, except at the outer level, where const
22029 is not required [conv.qual]. It would be normal to set this flag in
22030 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
22031 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
22032 This is the outermost level of a deduction, and PARM can be more CV
22033 qualified at this point.
22034 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
22035 This is the outermost level of a deduction, and PARM can be less CV
22036 qualified at this point. */
22037
22038 static int
22039 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
22040 bool explain_p)
22041 {
22042 int idx;
22043 tree targ;
22044 tree tparm;
22045 int strict_in = strict;
22046 tsubst_flags_t complain = (explain_p
22047 ? tf_warning_or_error
22048 : tf_none);
22049
22050 /* I don't think this will do the right thing with respect to types.
22051 But the only case I've seen it in so far has been array bounds, where
22052 signedness is the only information lost, and I think that will be
22053 okay. */
22054 while (CONVERT_EXPR_P (parm))
22055 parm = TREE_OPERAND (parm, 0);
22056
22057 if (arg == error_mark_node)
22058 return unify_invalid (explain_p);
22059 if (arg == unknown_type_node
22060 || arg == init_list_type_node)
22061 /* We can't deduce anything from this, but we might get all the
22062 template args from other function args. */
22063 return unify_success (explain_p);
22064
22065 if (parm == any_targ_node || arg == any_targ_node)
22066 return unify_success (explain_p);
22067
22068 /* If PARM uses template parameters, then we can't bail out here,
22069 even if ARG == PARM, since we won't record unifications for the
22070 template parameters. We might need them if we're trying to
22071 figure out which of two things is more specialized. */
22072 if (arg == parm && !uses_template_parms (parm))
22073 return unify_success (explain_p);
22074
22075 /* Handle init lists early, so the rest of the function can assume
22076 we're dealing with a type. */
22077 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
22078 {
22079 tree elt, elttype;
22080 unsigned i;
22081 tree orig_parm = parm;
22082
22083 if (!is_std_init_list (parm)
22084 && TREE_CODE (parm) != ARRAY_TYPE)
22085 /* We can only deduce from an initializer list argument if the
22086 parameter is std::initializer_list or an array; otherwise this
22087 is a non-deduced context. */
22088 return unify_success (explain_p);
22089
22090 if (TREE_CODE (parm) == ARRAY_TYPE)
22091 elttype = TREE_TYPE (parm);
22092 else
22093 {
22094 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
22095 /* Deduction is defined in terms of a single type, so just punt
22096 on the (bizarre) std::initializer_list<T...>. */
22097 if (PACK_EXPANSION_P (elttype))
22098 return unify_success (explain_p);
22099 }
22100
22101 if (strict != DEDUCE_EXACT
22102 && TYPE_P (elttype)
22103 && !uses_deducible_template_parms (elttype))
22104 /* If ELTTYPE has no deducible template parms, skip deduction from
22105 the list elements. */;
22106 else
22107 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
22108 {
22109 int elt_strict = strict;
22110
22111 if (elt == error_mark_node)
22112 return unify_invalid (explain_p);
22113
22114 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
22115 {
22116 tree type = TREE_TYPE (elt);
22117 if (type == error_mark_node)
22118 return unify_invalid (explain_p);
22119 /* It should only be possible to get here for a call. */
22120 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
22121 elt_strict |= maybe_adjust_types_for_deduction
22122 (DEDUCE_CALL, &elttype, &type, elt);
22123 elt = type;
22124 }
22125
22126 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
22127 explain_p);
22128 }
22129
22130 if (TREE_CODE (parm) == ARRAY_TYPE
22131 && deducible_array_bound (TYPE_DOMAIN (parm)))
22132 {
22133 /* Also deduce from the length of the initializer list. */
22134 tree max = size_int (CONSTRUCTOR_NELTS (arg));
22135 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
22136 if (idx == error_mark_node)
22137 return unify_invalid (explain_p);
22138 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
22139 idx, explain_p);
22140 }
22141
22142 /* If the std::initializer_list<T> deduction worked, replace the
22143 deduced A with std::initializer_list<A>. */
22144 if (orig_parm != parm)
22145 {
22146 idx = TEMPLATE_TYPE_IDX (orig_parm);
22147 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
22148 targ = listify (targ);
22149 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
22150 }
22151 return unify_success (explain_p);
22152 }
22153
22154 /* If parm and arg aren't the same kind of thing (template, type, or
22155 expression), fail early. */
22156 if (pa_kind (parm) != pa_kind (arg))
22157 return unify_invalid (explain_p);
22158
22159 /* Immediately reject some pairs that won't unify because of
22160 cv-qualification mismatches. */
22161 if (TREE_CODE (arg) == TREE_CODE (parm)
22162 && TYPE_P (arg)
22163 /* It is the elements of the array which hold the cv quals of an array
22164 type, and the elements might be template type parms. We'll check
22165 when we recurse. */
22166 && TREE_CODE (arg) != ARRAY_TYPE
22167 /* We check the cv-qualifiers when unifying with template type
22168 parameters below. We want to allow ARG `const T' to unify with
22169 PARM `T' for example, when computing which of two templates
22170 is more specialized, for example. */
22171 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
22172 && !check_cv_quals_for_unify (strict_in, arg, parm))
22173 return unify_cv_qual_mismatch (explain_p, parm, arg);
22174
22175 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
22176 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
22177 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
22178 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
22179 strict &= ~UNIFY_ALLOW_DERIVED;
22180 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
22181 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
22182
22183 switch (TREE_CODE (parm))
22184 {
22185 case TYPENAME_TYPE:
22186 case SCOPE_REF:
22187 case UNBOUND_CLASS_TEMPLATE:
22188 /* In a type which contains a nested-name-specifier, template
22189 argument values cannot be deduced for template parameters used
22190 within the nested-name-specifier. */
22191 return unify_success (explain_p);
22192
22193 case TEMPLATE_TYPE_PARM:
22194 case TEMPLATE_TEMPLATE_PARM:
22195 case BOUND_TEMPLATE_TEMPLATE_PARM:
22196 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
22197 if (error_operand_p (tparm))
22198 return unify_invalid (explain_p);
22199
22200 if (TEMPLATE_TYPE_LEVEL (parm)
22201 != template_decl_level (tparm))
22202 /* The PARM is not one we're trying to unify. Just check
22203 to see if it matches ARG. */
22204 {
22205 if (TREE_CODE (arg) == TREE_CODE (parm)
22206 && (is_auto (parm) ? is_auto (arg)
22207 : same_type_p (parm, arg)))
22208 return unify_success (explain_p);
22209 else
22210 return unify_type_mismatch (explain_p, parm, arg);
22211 }
22212 idx = TEMPLATE_TYPE_IDX (parm);
22213 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
22214 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
22215 if (error_operand_p (tparm))
22216 return unify_invalid (explain_p);
22217
22218 /* Check for mixed types and values. */
22219 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22220 && TREE_CODE (tparm) != TYPE_DECL)
22221 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
22222 && TREE_CODE (tparm) != TEMPLATE_DECL))
22223 gcc_unreachable ();
22224
22225 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22226 {
22227 if ((strict_in & UNIFY_ALLOW_DERIVED)
22228 && CLASS_TYPE_P (arg))
22229 {
22230 /* First try to match ARG directly. */
22231 tree t = try_class_unification (tparms, targs, parm, arg,
22232 explain_p);
22233 if (!t)
22234 {
22235 /* Otherwise, look for a suitable base of ARG, as below. */
22236 enum template_base_result r;
22237 r = get_template_base (tparms, targs, parm, arg,
22238 explain_p, &t);
22239 if (!t)
22240 return unify_no_common_base (explain_p, r, parm, arg);
22241 arg = t;
22242 }
22243 }
22244 /* ARG must be constructed from a template class or a template
22245 template parameter. */
22246 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
22247 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
22248 return unify_template_deduction_failure (explain_p, parm, arg);
22249
22250 /* Deduce arguments T, i from TT<T> or TT<i>. */
22251 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
22252 return 1;
22253
22254 arg = TYPE_TI_TEMPLATE (arg);
22255
22256 /* Fall through to deduce template name. */
22257 }
22258
22259 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
22260 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22261 {
22262 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
22263
22264 /* Simple cases: Value already set, does match or doesn't. */
22265 if (targ != NULL_TREE && template_args_equal (targ, arg))
22266 return unify_success (explain_p);
22267 else if (targ)
22268 return unify_inconsistency (explain_p, parm, targ, arg);
22269 }
22270 else
22271 {
22272 /* If PARM is `const T' and ARG is only `int', we don't have
22273 a match unless we are allowing additional qualification.
22274 If ARG is `const int' and PARM is just `T' that's OK;
22275 that binds `const int' to `T'. */
22276 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
22277 arg, parm))
22278 return unify_cv_qual_mismatch (explain_p, parm, arg);
22279
22280 /* Consider the case where ARG is `const volatile int' and
22281 PARM is `const T'. Then, T should be `volatile int'. */
22282 arg = cp_build_qualified_type_real
22283 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
22284 if (arg == error_mark_node)
22285 return unify_invalid (explain_p);
22286
22287 /* Simple cases: Value already set, does match or doesn't. */
22288 if (targ != NULL_TREE && same_type_p (targ, arg))
22289 return unify_success (explain_p);
22290 else if (targ)
22291 return unify_inconsistency (explain_p, parm, targ, arg);
22292
22293 /* Make sure that ARG is not a variable-sized array. (Note
22294 that were talking about variable-sized arrays (like
22295 `int[n]'), rather than arrays of unknown size (like
22296 `int[]').) We'll get very confused by such a type since
22297 the bound of the array is not constant, and therefore
22298 not mangleable. Besides, such types are not allowed in
22299 ISO C++, so we can do as we please here. We do allow
22300 them for 'auto' deduction, since that isn't ABI-exposed. */
22301 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
22302 return unify_vla_arg (explain_p, arg);
22303
22304 /* Strip typedefs as in convert_template_argument. */
22305 arg = canonicalize_type_argument (arg, tf_none);
22306 }
22307
22308 /* If ARG is a parameter pack or an expansion, we cannot unify
22309 against it unless PARM is also a parameter pack. */
22310 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
22311 && !template_parameter_pack_p (parm))
22312 return unify_parameter_pack_mismatch (explain_p, parm, arg);
22313
22314 /* If the argument deduction results is a METHOD_TYPE,
22315 then there is a problem.
22316 METHOD_TYPE doesn't map to any real C++ type the result of
22317 the deduction cannot be of that type. */
22318 if (TREE_CODE (arg) == METHOD_TYPE)
22319 return unify_method_type_error (explain_p, arg);
22320
22321 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
22322 return unify_success (explain_p);
22323
22324 case TEMPLATE_PARM_INDEX:
22325 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
22326 if (error_operand_p (tparm))
22327 return unify_invalid (explain_p);
22328
22329 if (TEMPLATE_PARM_LEVEL (parm)
22330 != template_decl_level (tparm))
22331 {
22332 /* The PARM is not one we're trying to unify. Just check
22333 to see if it matches ARG. */
22334 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
22335 && cp_tree_equal (parm, arg));
22336 if (result)
22337 unify_expression_unequal (explain_p, parm, arg);
22338 return result;
22339 }
22340
22341 idx = TEMPLATE_PARM_IDX (parm);
22342 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
22343
22344 if (targ)
22345 {
22346 if ((strict & UNIFY_ALLOW_INTEGER)
22347 && TREE_TYPE (targ) && TREE_TYPE (arg)
22348 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
22349 /* We're deducing from an array bound, the type doesn't matter. */
22350 arg = fold_convert (TREE_TYPE (targ), arg);
22351 int x = !cp_tree_equal (targ, arg);
22352 if (x)
22353 unify_inconsistency (explain_p, parm, targ, arg);
22354 return x;
22355 }
22356
22357 /* [temp.deduct.type] If, in the declaration of a function template
22358 with a non-type template-parameter, the non-type
22359 template-parameter is used in an expression in the function
22360 parameter-list and, if the corresponding template-argument is
22361 deduced, the template-argument type shall match the type of the
22362 template-parameter exactly, except that a template-argument
22363 deduced from an array bound may be of any integral type.
22364 The non-type parameter might use already deduced type parameters. */
22365 tparm = TREE_TYPE (parm);
22366 if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs))
22367 /* We don't have enough levels of args to do any substitution. This
22368 can happen in the context of -fnew-ttp-matching. */;
22369 else
22370 {
22371 ++processing_template_decl;
22372 tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
22373 --processing_template_decl;
22374
22375 if (tree a = type_uses_auto (tparm))
22376 {
22377 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
22378 if (tparm == error_mark_node)
22379 return 1;
22380 }
22381 }
22382
22383 if (!TREE_TYPE (arg))
22384 /* Template-parameter dependent expression. Just accept it for now.
22385 It will later be processed in convert_template_argument. */
22386 ;
22387 else if (same_type_ignoring_top_level_qualifiers_p
22388 (non_reference (TREE_TYPE (arg)),
22389 non_reference (tparm)))
22390 /* OK. Ignore top-level quals here because a class-type template
22391 parameter object is const. */;
22392 else if ((strict & UNIFY_ALLOW_INTEGER)
22393 && CP_INTEGRAL_TYPE_P (tparm))
22394 /* Convert the ARG to the type of PARM; the deduced non-type
22395 template argument must exactly match the types of the
22396 corresponding parameter. */
22397 arg = fold (build_nop (tparm, arg));
22398 else if (uses_template_parms (tparm))
22399 {
22400 /* We haven't deduced the type of this parameter yet. */
22401 if (cxx_dialect >= cxx17
22402 /* We deduce from array bounds in try_array_deduction. */
22403 && !(strict & UNIFY_ALLOW_INTEGER))
22404 {
22405 /* Deduce it from the non-type argument. */
22406 tree atype = TREE_TYPE (arg);
22407 RECUR_AND_CHECK_FAILURE (tparms, targs,
22408 tparm, atype,
22409 UNIFY_ALLOW_NONE, explain_p);
22410 }
22411 else
22412 /* Try again later. */
22413 return unify_success (explain_p);
22414 }
22415 else
22416 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
22417
22418 /* If ARG is a parameter pack or an expansion, we cannot unify
22419 against it unless PARM is also a parameter pack. */
22420 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
22421 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
22422 return unify_parameter_pack_mismatch (explain_p, parm, arg);
22423
22424 {
22425 bool removed_attr = false;
22426 arg = strip_typedefs_expr (arg, &removed_attr);
22427 }
22428 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
22429 return unify_success (explain_p);
22430
22431 case PTRMEM_CST:
22432 {
22433 /* A pointer-to-member constant can be unified only with
22434 another constant. */
22435 if (TREE_CODE (arg) != PTRMEM_CST)
22436 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
22437
22438 /* Just unify the class member. It would be useless (and possibly
22439 wrong, depending on the strict flags) to unify also
22440 PTRMEM_CST_CLASS, because we want to be sure that both parm and
22441 arg refer to the same variable, even if through different
22442 classes. For instance:
22443
22444 struct A { int x; };
22445 struct B : A { };
22446
22447 Unification of &A::x and &B::x must succeed. */
22448 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
22449 PTRMEM_CST_MEMBER (arg), strict, explain_p);
22450 }
22451
22452 case POINTER_TYPE:
22453 {
22454 if (!TYPE_PTR_P (arg))
22455 return unify_type_mismatch (explain_p, parm, arg);
22456
22457 /* [temp.deduct.call]
22458
22459 A can be another pointer or pointer to member type that can
22460 be converted to the deduced A via a qualification
22461 conversion (_conv.qual_).
22462
22463 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
22464 This will allow for additional cv-qualification of the
22465 pointed-to types if appropriate. */
22466
22467 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
22468 /* The derived-to-base conversion only persists through one
22469 level of pointers. */
22470 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
22471
22472 return unify (tparms, targs, TREE_TYPE (parm),
22473 TREE_TYPE (arg), strict, explain_p);
22474 }
22475
22476 case REFERENCE_TYPE:
22477 if (!TYPE_REF_P (arg))
22478 return unify_type_mismatch (explain_p, parm, arg);
22479 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
22480 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
22481
22482 case ARRAY_TYPE:
22483 if (TREE_CODE (arg) != ARRAY_TYPE)
22484 return unify_type_mismatch (explain_p, parm, arg);
22485 if ((TYPE_DOMAIN (parm) == NULL_TREE)
22486 != (TYPE_DOMAIN (arg) == NULL_TREE))
22487 return unify_type_mismatch (explain_p, parm, arg);
22488 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
22489 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
22490 if (TYPE_DOMAIN (parm) != NULL_TREE)
22491 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
22492 TYPE_DOMAIN (arg), explain_p);
22493 return unify_success (explain_p);
22494
22495 case REAL_TYPE:
22496 case COMPLEX_TYPE:
22497 case VECTOR_TYPE:
22498 case INTEGER_TYPE:
22499 case BOOLEAN_TYPE:
22500 case ENUMERAL_TYPE:
22501 case VOID_TYPE:
22502 case NULLPTR_TYPE:
22503 if (TREE_CODE (arg) != TREE_CODE (parm))
22504 return unify_type_mismatch (explain_p, parm, arg);
22505
22506 /* We have already checked cv-qualification at the top of the
22507 function. */
22508 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
22509 return unify_type_mismatch (explain_p, parm, arg);
22510
22511 /* As far as unification is concerned, this wins. Later checks
22512 will invalidate it if necessary. */
22513 return unify_success (explain_p);
22514
22515 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
22516 /* Type INTEGER_CST can come from ordinary constant template args. */
22517 case INTEGER_CST:
22518 while (CONVERT_EXPR_P (arg))
22519 arg = TREE_OPERAND (arg, 0);
22520
22521 if (TREE_CODE (arg) != INTEGER_CST)
22522 return unify_template_argument_mismatch (explain_p, parm, arg);
22523 return (tree_int_cst_equal (parm, arg)
22524 ? unify_success (explain_p)
22525 : unify_template_argument_mismatch (explain_p, parm, arg));
22526
22527 case TREE_VEC:
22528 {
22529 int i, len, argslen;
22530 int parm_variadic_p = 0;
22531
22532 if (TREE_CODE (arg) != TREE_VEC)
22533 return unify_template_argument_mismatch (explain_p, parm, arg);
22534
22535 len = TREE_VEC_LENGTH (parm);
22536 argslen = TREE_VEC_LENGTH (arg);
22537
22538 /* Check for pack expansions in the parameters. */
22539 for (i = 0; i < len; ++i)
22540 {
22541 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
22542 {
22543 if (i == len - 1)
22544 /* We can unify against something with a trailing
22545 parameter pack. */
22546 parm_variadic_p = 1;
22547 else
22548 /* [temp.deduct.type]/9: If the template argument list of
22549 P contains a pack expansion that is not the last
22550 template argument, the entire template argument list
22551 is a non-deduced context. */
22552 return unify_success (explain_p);
22553 }
22554 }
22555
22556 /* If we don't have enough arguments to satisfy the parameters
22557 (not counting the pack expression at the end), or we have
22558 too many arguments for a parameter list that doesn't end in
22559 a pack expression, we can't unify. */
22560 if (parm_variadic_p
22561 ? argslen < len - parm_variadic_p
22562 : argslen != len)
22563 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
22564
22565 /* Unify all of the parameters that precede the (optional)
22566 pack expression. */
22567 for (i = 0; i < len - parm_variadic_p; ++i)
22568 {
22569 RECUR_AND_CHECK_FAILURE (tparms, targs,
22570 TREE_VEC_ELT (parm, i),
22571 TREE_VEC_ELT (arg, i),
22572 UNIFY_ALLOW_NONE, explain_p);
22573 }
22574 if (parm_variadic_p)
22575 return unify_pack_expansion (tparms, targs, parm, arg,
22576 DEDUCE_EXACT,
22577 /*subr=*/true, explain_p);
22578 return unify_success (explain_p);
22579 }
22580
22581 case RECORD_TYPE:
22582 case UNION_TYPE:
22583 if (TREE_CODE (arg) != TREE_CODE (parm))
22584 return unify_type_mismatch (explain_p, parm, arg);
22585
22586 if (TYPE_PTRMEMFUNC_P (parm))
22587 {
22588 if (!TYPE_PTRMEMFUNC_P (arg))
22589 return unify_type_mismatch (explain_p, parm, arg);
22590
22591 return unify (tparms, targs,
22592 TYPE_PTRMEMFUNC_FN_TYPE (parm),
22593 TYPE_PTRMEMFUNC_FN_TYPE (arg),
22594 strict, explain_p);
22595 }
22596 else if (TYPE_PTRMEMFUNC_P (arg))
22597 return unify_type_mismatch (explain_p, parm, arg);
22598
22599 if (CLASSTYPE_TEMPLATE_INFO (parm))
22600 {
22601 tree t = NULL_TREE;
22602
22603 if (strict_in & UNIFY_ALLOW_DERIVED)
22604 {
22605 /* First, we try to unify the PARM and ARG directly. */
22606 t = try_class_unification (tparms, targs,
22607 parm, arg, explain_p);
22608
22609 if (!t)
22610 {
22611 /* Fallback to the special case allowed in
22612 [temp.deduct.call]:
22613
22614 If P is a class, and P has the form
22615 template-id, then A can be a derived class of
22616 the deduced A. Likewise, if P is a pointer to
22617 a class of the form template-id, A can be a
22618 pointer to a derived class pointed to by the
22619 deduced A. */
22620 enum template_base_result r;
22621 r = get_template_base (tparms, targs, parm, arg,
22622 explain_p, &t);
22623
22624 if (!t)
22625 {
22626 /* Don't give the derived diagnostic if we're
22627 already dealing with the same template. */
22628 bool same_template
22629 = (CLASSTYPE_TEMPLATE_INFO (arg)
22630 && (CLASSTYPE_TI_TEMPLATE (parm)
22631 == CLASSTYPE_TI_TEMPLATE (arg)));
22632 return unify_no_common_base (explain_p && !same_template,
22633 r, parm, arg);
22634 }
22635 }
22636 }
22637 else if (CLASSTYPE_TEMPLATE_INFO (arg)
22638 && (CLASSTYPE_TI_TEMPLATE (parm)
22639 == CLASSTYPE_TI_TEMPLATE (arg)))
22640 /* Perhaps PARM is something like S<U> and ARG is S<int>.
22641 Then, we should unify `int' and `U'. */
22642 t = arg;
22643 else
22644 /* There's no chance of unification succeeding. */
22645 return unify_type_mismatch (explain_p, parm, arg);
22646
22647 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
22648 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
22649 }
22650 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
22651 return unify_type_mismatch (explain_p, parm, arg);
22652 return unify_success (explain_p);
22653
22654 case METHOD_TYPE:
22655 case FUNCTION_TYPE:
22656 {
22657 unsigned int nargs;
22658 tree *args;
22659 tree a;
22660 unsigned int i;
22661
22662 if (TREE_CODE (arg) != TREE_CODE (parm))
22663 return unify_type_mismatch (explain_p, parm, arg);
22664
22665 /* CV qualifications for methods can never be deduced, they must
22666 match exactly. We need to check them explicitly here,
22667 because type_unification_real treats them as any other
22668 cv-qualified parameter. */
22669 if (TREE_CODE (parm) == METHOD_TYPE
22670 && (!check_cv_quals_for_unify
22671 (UNIFY_ALLOW_NONE,
22672 class_of_this_parm (arg),
22673 class_of_this_parm (parm))))
22674 return unify_cv_qual_mismatch (explain_p, parm, arg);
22675 if (TREE_CODE (arg) == FUNCTION_TYPE
22676 && type_memfn_quals (parm) != type_memfn_quals (arg))
22677 return unify_cv_qual_mismatch (explain_p, parm, arg);
22678 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
22679 return unify_type_mismatch (explain_p, parm, arg);
22680
22681 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
22682 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
22683
22684 nargs = list_length (TYPE_ARG_TYPES (arg));
22685 args = XALLOCAVEC (tree, nargs);
22686 for (a = TYPE_ARG_TYPES (arg), i = 0;
22687 a != NULL_TREE && a != void_list_node;
22688 a = TREE_CHAIN (a), ++i)
22689 args[i] = TREE_VALUE (a);
22690 nargs = i;
22691
22692 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
22693 args, nargs, 1, DEDUCE_EXACT,
22694 NULL, explain_p))
22695 return 1;
22696
22697 if (flag_noexcept_type)
22698 {
22699 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
22700 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
22701 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
22702 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
22703 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
22704 && uses_template_parms (TREE_PURPOSE (pspec)))
22705 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
22706 TREE_PURPOSE (aspec),
22707 UNIFY_ALLOW_NONE, explain_p);
22708 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
22709 return unify_type_mismatch (explain_p, parm, arg);
22710 }
22711
22712 return 0;
22713 }
22714
22715 case OFFSET_TYPE:
22716 /* Unify a pointer to member with a pointer to member function, which
22717 deduces the type of the member as a function type. */
22718 if (TYPE_PTRMEMFUNC_P (arg))
22719 {
22720 /* Check top-level cv qualifiers */
22721 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
22722 return unify_cv_qual_mismatch (explain_p, parm, arg);
22723
22724 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
22725 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
22726 UNIFY_ALLOW_NONE, explain_p);
22727
22728 /* Determine the type of the function we are unifying against. */
22729 tree fntype = static_fn_type (arg);
22730
22731 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
22732 }
22733
22734 if (TREE_CODE (arg) != OFFSET_TYPE)
22735 return unify_type_mismatch (explain_p, parm, arg);
22736 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
22737 TYPE_OFFSET_BASETYPE (arg),
22738 UNIFY_ALLOW_NONE, explain_p);
22739 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
22740 strict, explain_p);
22741
22742 case CONST_DECL:
22743 if (DECL_TEMPLATE_PARM_P (parm))
22744 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
22745 if (arg != scalar_constant_value (parm))
22746 return unify_template_argument_mismatch (explain_p, parm, arg);
22747 return unify_success (explain_p);
22748
22749 case FIELD_DECL:
22750 case TEMPLATE_DECL:
22751 /* Matched cases are handled by the ARG == PARM test above. */
22752 return unify_template_argument_mismatch (explain_p, parm, arg);
22753
22754 case VAR_DECL:
22755 /* We might get a variable as a non-type template argument in parm if the
22756 corresponding parameter is type-dependent. Make any necessary
22757 adjustments based on whether arg is a reference. */
22758 if (CONSTANT_CLASS_P (arg))
22759 parm = fold_non_dependent_expr (parm, complain);
22760 else if (REFERENCE_REF_P (arg))
22761 {
22762 tree sub = TREE_OPERAND (arg, 0);
22763 STRIP_NOPS (sub);
22764 if (TREE_CODE (sub) == ADDR_EXPR)
22765 arg = TREE_OPERAND (sub, 0);
22766 }
22767 /* Now use the normal expression code to check whether they match. */
22768 goto expr;
22769
22770 case TYPE_ARGUMENT_PACK:
22771 case NONTYPE_ARGUMENT_PACK:
22772 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
22773 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
22774
22775 case TYPEOF_TYPE:
22776 case DECLTYPE_TYPE:
22777 case UNDERLYING_TYPE:
22778 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
22779 or UNDERLYING_TYPE nodes. */
22780 return unify_success (explain_p);
22781
22782 case ERROR_MARK:
22783 /* Unification fails if we hit an error node. */
22784 return unify_invalid (explain_p);
22785
22786 case INDIRECT_REF:
22787 if (REFERENCE_REF_P (parm))
22788 {
22789 bool pexp = PACK_EXPANSION_P (arg);
22790 if (pexp)
22791 arg = PACK_EXPANSION_PATTERN (arg);
22792 if (REFERENCE_REF_P (arg))
22793 arg = TREE_OPERAND (arg, 0);
22794 if (pexp)
22795 arg = make_pack_expansion (arg, complain);
22796 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
22797 strict, explain_p);
22798 }
22799 /* FALLTHRU */
22800
22801 default:
22802 /* An unresolved overload is a nondeduced context. */
22803 if (is_overloaded_fn (parm) || type_unknown_p (parm))
22804 return unify_success (explain_p);
22805 gcc_assert (EXPR_P (parm)
22806 || COMPOUND_LITERAL_P (parm)
22807 || TREE_CODE (parm) == TRAIT_EXPR);
22808 expr:
22809 /* We must be looking at an expression. This can happen with
22810 something like:
22811
22812 template <int I>
22813 void foo(S<I>, S<I + 2>);
22814
22815 or
22816
22817 template<typename T>
22818 void foo(A<T, T{}>);
22819
22820 This is a "non-deduced context":
22821
22822 [deduct.type]
22823
22824 The non-deduced contexts are:
22825
22826 --A non-type template argument or an array bound in which
22827 a subexpression references a template parameter.
22828
22829 In these cases, we assume deduction succeeded, but don't
22830 actually infer any unifications. */
22831
22832 if (!uses_template_parms (parm)
22833 && !template_args_equal (parm, arg))
22834 return unify_expression_unequal (explain_p, parm, arg);
22835 else
22836 return unify_success (explain_p);
22837 }
22838 }
22839 #undef RECUR_AND_CHECK_FAILURE
22840 \f
22841 /* Note that DECL can be defined in this translation unit, if
22842 required. */
22843
22844 static void
22845 mark_definable (tree decl)
22846 {
22847 tree clone;
22848 DECL_NOT_REALLY_EXTERN (decl) = 1;
22849 FOR_EACH_CLONE (clone, decl)
22850 DECL_NOT_REALLY_EXTERN (clone) = 1;
22851 }
22852
22853 /* Called if RESULT is explicitly instantiated, or is a member of an
22854 explicitly instantiated class. */
22855
22856 void
22857 mark_decl_instantiated (tree result, int extern_p)
22858 {
22859 SET_DECL_EXPLICIT_INSTANTIATION (result);
22860
22861 /* If this entity has already been written out, it's too late to
22862 make any modifications. */
22863 if (TREE_ASM_WRITTEN (result))
22864 return;
22865
22866 /* For anonymous namespace we don't need to do anything. */
22867 if (decl_anon_ns_mem_p (result))
22868 {
22869 gcc_assert (!TREE_PUBLIC (result));
22870 return;
22871 }
22872
22873 if (TREE_CODE (result) != FUNCTION_DECL)
22874 /* The TREE_PUBLIC flag for function declarations will have been
22875 set correctly by tsubst. */
22876 TREE_PUBLIC (result) = 1;
22877
22878 /* This might have been set by an earlier implicit instantiation. */
22879 DECL_COMDAT (result) = 0;
22880
22881 if (extern_p)
22882 DECL_NOT_REALLY_EXTERN (result) = 0;
22883 else
22884 {
22885 mark_definable (result);
22886 mark_needed (result);
22887 /* Always make artificials weak. */
22888 if (DECL_ARTIFICIAL (result) && flag_weak)
22889 comdat_linkage (result);
22890 /* For WIN32 we also want to put explicit instantiations in
22891 linkonce sections. */
22892 else if (TREE_PUBLIC (result))
22893 maybe_make_one_only (result);
22894 if (TREE_CODE (result) == FUNCTION_DECL
22895 && DECL_TEMPLATE_INSTANTIATED (result))
22896 /* If the function has already been instantiated, clear DECL_EXTERNAL,
22897 since start_preparsed_function wouldn't have if we had an earlier
22898 extern explicit instantiation. */
22899 DECL_EXTERNAL (result) = 0;
22900 }
22901
22902 /* If EXTERN_P, then this function will not be emitted -- unless
22903 followed by an explicit instantiation, at which point its linkage
22904 will be adjusted. If !EXTERN_P, then this function will be
22905 emitted here. In neither circumstance do we want
22906 import_export_decl to adjust the linkage. */
22907 DECL_INTERFACE_KNOWN (result) = 1;
22908 }
22909
22910 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
22911 important template arguments. If any are missing, we check whether
22912 they're important by using error_mark_node for substituting into any
22913 args that were used for partial ordering (the ones between ARGS and END)
22914 and seeing if it bubbles up. */
22915
22916 static bool
22917 check_undeduced_parms (tree targs, tree args, tree end)
22918 {
22919 bool found = false;
22920 int i;
22921 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
22922 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
22923 {
22924 found = true;
22925 TREE_VEC_ELT (targs, i) = error_mark_node;
22926 }
22927 if (found)
22928 {
22929 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
22930 if (substed == error_mark_node)
22931 return true;
22932 }
22933 return false;
22934 }
22935
22936 /* Given two function templates PAT1 and PAT2, return:
22937
22938 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
22939 -1 if PAT2 is more specialized than PAT1.
22940 0 if neither is more specialized.
22941
22942 LEN indicates the number of parameters we should consider
22943 (defaulted parameters should not be considered).
22944
22945 The 1998 std underspecified function template partial ordering, and
22946 DR214 addresses the issue. We take pairs of arguments, one from
22947 each of the templates, and deduce them against each other. One of
22948 the templates will be more specialized if all the *other*
22949 template's arguments deduce against its arguments and at least one
22950 of its arguments *does* *not* deduce against the other template's
22951 corresponding argument. Deduction is done as for class templates.
22952 The arguments used in deduction have reference and top level cv
22953 qualifiers removed. Iff both arguments were originally reference
22954 types *and* deduction succeeds in both directions, an lvalue reference
22955 wins against an rvalue reference and otherwise the template
22956 with the more cv-qualified argument wins for that pairing (if
22957 neither is more cv-qualified, they both are equal). Unlike regular
22958 deduction, after all the arguments have been deduced in this way,
22959 we do *not* verify the deduced template argument values can be
22960 substituted into non-deduced contexts.
22961
22962 The logic can be a bit confusing here, because we look at deduce1 and
22963 targs1 to see if pat2 is at least as specialized, and vice versa; if we
22964 can find template arguments for pat1 to make arg1 look like arg2, that
22965 means that arg2 is at least as specialized as arg1. */
22966
22967 int
22968 more_specialized_fn (tree pat1, tree pat2, int len)
22969 {
22970 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
22971 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
22972 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
22973 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
22974 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
22975 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
22976 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
22977 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
22978 tree origs1, origs2;
22979 bool lose1 = false;
22980 bool lose2 = false;
22981
22982 /* Remove the this parameter from non-static member functions. If
22983 one is a non-static member function and the other is not a static
22984 member function, remove the first parameter from that function
22985 also. This situation occurs for operator functions where we
22986 locate both a member function (with this pointer) and non-member
22987 operator (with explicit first operand). */
22988 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
22989 {
22990 len--; /* LEN is the number of significant arguments for DECL1 */
22991 args1 = TREE_CHAIN (args1);
22992 if (!DECL_STATIC_FUNCTION_P (decl2))
22993 args2 = TREE_CHAIN (args2);
22994 }
22995 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
22996 {
22997 args2 = TREE_CHAIN (args2);
22998 if (!DECL_STATIC_FUNCTION_P (decl1))
22999 {
23000 len--;
23001 args1 = TREE_CHAIN (args1);
23002 }
23003 }
23004
23005 /* If only one is a conversion operator, they are unordered. */
23006 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
23007 return 0;
23008
23009 /* Consider the return type for a conversion function */
23010 if (DECL_CONV_FN_P (decl1))
23011 {
23012 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
23013 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
23014 len++;
23015 }
23016
23017 processing_template_decl++;
23018
23019 origs1 = args1;
23020 origs2 = args2;
23021
23022 while (len--
23023 /* Stop when an ellipsis is seen. */
23024 && args1 != NULL_TREE && args2 != NULL_TREE)
23025 {
23026 tree arg1 = TREE_VALUE (args1);
23027 tree arg2 = TREE_VALUE (args2);
23028 int deduce1, deduce2;
23029 int quals1 = -1;
23030 int quals2 = -1;
23031 int ref1 = 0;
23032 int ref2 = 0;
23033
23034 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
23035 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
23036 {
23037 /* When both arguments are pack expansions, we need only
23038 unify the patterns themselves. */
23039 arg1 = PACK_EXPANSION_PATTERN (arg1);
23040 arg2 = PACK_EXPANSION_PATTERN (arg2);
23041
23042 /* This is the last comparison we need to do. */
23043 len = 0;
23044 }
23045
23046 /* DR 1847: If a particular P contains no template-parameters that
23047 participate in template argument deduction, that P is not used to
23048 determine the ordering. */
23049 if (!uses_deducible_template_parms (arg1)
23050 && !uses_deducible_template_parms (arg2))
23051 goto next;
23052
23053 if (TYPE_REF_P (arg1))
23054 {
23055 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
23056 arg1 = TREE_TYPE (arg1);
23057 quals1 = cp_type_quals (arg1);
23058 }
23059
23060 if (TYPE_REF_P (arg2))
23061 {
23062 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
23063 arg2 = TREE_TYPE (arg2);
23064 quals2 = cp_type_quals (arg2);
23065 }
23066
23067 arg1 = TYPE_MAIN_VARIANT (arg1);
23068 arg2 = TYPE_MAIN_VARIANT (arg2);
23069
23070 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
23071 {
23072 int i, len2 = remaining_arguments (args2);
23073 tree parmvec = make_tree_vec (1);
23074 tree argvec = make_tree_vec (len2);
23075 tree ta = args2;
23076
23077 /* Setup the parameter vector, which contains only ARG1. */
23078 TREE_VEC_ELT (parmvec, 0) = arg1;
23079
23080 /* Setup the argument vector, which contains the remaining
23081 arguments. */
23082 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
23083 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
23084
23085 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
23086 argvec, DEDUCE_EXACT,
23087 /*subr=*/true, /*explain_p=*/false)
23088 == 0);
23089
23090 /* We cannot deduce in the other direction, because ARG1 is
23091 a pack expansion but ARG2 is not. */
23092 deduce2 = 0;
23093 }
23094 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
23095 {
23096 int i, len1 = remaining_arguments (args1);
23097 tree parmvec = make_tree_vec (1);
23098 tree argvec = make_tree_vec (len1);
23099 tree ta = args1;
23100
23101 /* Setup the parameter vector, which contains only ARG1. */
23102 TREE_VEC_ELT (parmvec, 0) = arg2;
23103
23104 /* Setup the argument vector, which contains the remaining
23105 arguments. */
23106 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
23107 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
23108
23109 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
23110 argvec, DEDUCE_EXACT,
23111 /*subr=*/true, /*explain_p=*/false)
23112 == 0);
23113
23114 /* We cannot deduce in the other direction, because ARG2 is
23115 a pack expansion but ARG1 is not.*/
23116 deduce1 = 0;
23117 }
23118
23119 else
23120 {
23121 /* The normal case, where neither argument is a pack
23122 expansion. */
23123 deduce1 = (unify (tparms1, targs1, arg1, arg2,
23124 UNIFY_ALLOW_NONE, /*explain_p=*/false)
23125 == 0);
23126 deduce2 = (unify (tparms2, targs2, arg2, arg1,
23127 UNIFY_ALLOW_NONE, /*explain_p=*/false)
23128 == 0);
23129 }
23130
23131 /* If we couldn't deduce arguments for tparms1 to make arg1 match
23132 arg2, then arg2 is not as specialized as arg1. */
23133 if (!deduce1)
23134 lose2 = true;
23135 if (!deduce2)
23136 lose1 = true;
23137
23138 /* "If, for a given type, deduction succeeds in both directions
23139 (i.e., the types are identical after the transformations above)
23140 and both P and A were reference types (before being replaced with
23141 the type referred to above):
23142 - if the type from the argument template was an lvalue reference and
23143 the type from the parameter template was not, the argument type is
23144 considered to be more specialized than the other; otherwise,
23145 - if the type from the argument template is more cv-qualified
23146 than the type from the parameter template (as described above),
23147 the argument type is considered to be more specialized than the other;
23148 otherwise,
23149 - neither type is more specialized than the other." */
23150
23151 if (deduce1 && deduce2)
23152 {
23153 if (ref1 && ref2 && ref1 != ref2)
23154 {
23155 if (ref1 > ref2)
23156 lose1 = true;
23157 else
23158 lose2 = true;
23159 }
23160 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
23161 {
23162 if ((quals1 & quals2) == quals2)
23163 lose2 = true;
23164 if ((quals1 & quals2) == quals1)
23165 lose1 = true;
23166 }
23167 }
23168
23169 if (lose1 && lose2)
23170 /* We've failed to deduce something in either direction.
23171 These must be unordered. */
23172 break;
23173
23174 next:
23175
23176 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
23177 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
23178 /* We have already processed all of the arguments in our
23179 handing of the pack expansion type. */
23180 len = 0;
23181
23182 args1 = TREE_CHAIN (args1);
23183 args2 = TREE_CHAIN (args2);
23184 }
23185
23186 /* "In most cases, all template parameters must have values in order for
23187 deduction to succeed, but for partial ordering purposes a template
23188 parameter may remain without a value provided it is not used in the
23189 types being used for partial ordering."
23190
23191 Thus, if we are missing any of the targs1 we need to substitute into
23192 origs1, then pat2 is not as specialized as pat1. This can happen when
23193 there is a nondeduced context. */
23194 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
23195 lose2 = true;
23196 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
23197 lose1 = true;
23198
23199 processing_template_decl--;
23200
23201 /* If both deductions succeed, the partial ordering selects the more
23202 constrained template. */
23203 if (!lose1 && !lose2)
23204 {
23205 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
23206 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
23207 lose1 = !subsumes_constraints (c1, c2);
23208 lose2 = !subsumes_constraints (c2, c1);
23209 }
23210
23211 /* All things being equal, if the next argument is a pack expansion
23212 for one function but not for the other, prefer the
23213 non-variadic function. FIXME this is bogus; see c++/41958. */
23214 if (lose1 == lose2
23215 && args1 && TREE_VALUE (args1)
23216 && args2 && TREE_VALUE (args2))
23217 {
23218 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
23219 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
23220 }
23221
23222 if (lose1 == lose2)
23223 return 0;
23224 else if (!lose1)
23225 return 1;
23226 else
23227 return -1;
23228 }
23229
23230 /* Determine which of two partial specializations of TMPL is more
23231 specialized.
23232
23233 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
23234 to the first partial specialization. The TREE_PURPOSE is the
23235 innermost set of template parameters for the partial
23236 specialization. PAT2 is similar, but for the second template.
23237
23238 Return 1 if the first partial specialization is more specialized;
23239 -1 if the second is more specialized; 0 if neither is more
23240 specialized.
23241
23242 See [temp.class.order] for information about determining which of
23243 two templates is more specialized. */
23244
23245 static int
23246 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
23247 {
23248 tree targs;
23249 int winner = 0;
23250 bool any_deductions = false;
23251
23252 tree tmpl1 = TREE_VALUE (pat1);
23253 tree tmpl2 = TREE_VALUE (pat2);
23254 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
23255 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
23256
23257 /* Just like what happens for functions, if we are ordering between
23258 different template specializations, we may encounter dependent
23259 types in the arguments, and we need our dependency check functions
23260 to behave correctly. */
23261 ++processing_template_decl;
23262 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
23263 if (targs)
23264 {
23265 --winner;
23266 any_deductions = true;
23267 }
23268
23269 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
23270 if (targs)
23271 {
23272 ++winner;
23273 any_deductions = true;
23274 }
23275 --processing_template_decl;
23276
23277 /* If both deductions succeed, the partial ordering selects the more
23278 constrained template. */
23279 if (!winner && any_deductions)
23280 return more_constrained (tmpl1, tmpl2);
23281
23282 /* In the case of a tie where at least one of the templates
23283 has a parameter pack at the end, the template with the most
23284 non-packed parameters wins. */
23285 if (winner == 0
23286 && any_deductions
23287 && (template_args_variadic_p (TREE_PURPOSE (pat1))
23288 || template_args_variadic_p (TREE_PURPOSE (pat2))))
23289 {
23290 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
23291 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
23292 int len1 = TREE_VEC_LENGTH (args1);
23293 int len2 = TREE_VEC_LENGTH (args2);
23294
23295 /* We don't count the pack expansion at the end. */
23296 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
23297 --len1;
23298 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
23299 --len2;
23300
23301 if (len1 > len2)
23302 return 1;
23303 else if (len1 < len2)
23304 return -1;
23305 }
23306
23307 return winner;
23308 }
23309
23310 /* Return the template arguments that will produce the function signature
23311 DECL from the function template FN, with the explicit template
23312 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
23313 also match. Return NULL_TREE if no satisfactory arguments could be
23314 found. */
23315
23316 static tree
23317 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
23318 {
23319 int ntparms = DECL_NTPARMS (fn);
23320 tree targs = make_tree_vec (ntparms);
23321 tree decl_type = TREE_TYPE (decl);
23322 tree decl_arg_types;
23323 tree *args;
23324 unsigned int nargs, ix;
23325 tree arg;
23326
23327 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
23328
23329 /* Never do unification on the 'this' parameter. */
23330 decl_arg_types = skip_artificial_parms_for (decl,
23331 TYPE_ARG_TYPES (decl_type));
23332
23333 nargs = list_length (decl_arg_types);
23334 args = XALLOCAVEC (tree, nargs);
23335 for (arg = decl_arg_types, ix = 0;
23336 arg != NULL_TREE && arg != void_list_node;
23337 arg = TREE_CHAIN (arg), ++ix)
23338 args[ix] = TREE_VALUE (arg);
23339
23340 if (fn_type_unification (fn, explicit_args, targs,
23341 args, ix,
23342 (check_rettype || DECL_CONV_FN_P (fn)
23343 ? TREE_TYPE (decl_type) : NULL_TREE),
23344 DEDUCE_EXACT, LOOKUP_NORMAL, NULL,
23345 /*explain_p=*/false,
23346 /*decltype*/false)
23347 == error_mark_node)
23348 return NULL_TREE;
23349
23350 return targs;
23351 }
23352
23353 /* Return the innermost template arguments that, when applied to a partial
23354 specialization SPEC_TMPL of TMPL, yield the ARGS.
23355
23356 For example, suppose we have:
23357
23358 template <class T, class U> struct S {};
23359 template <class T> struct S<T*, int> {};
23360
23361 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
23362 partial specialization and the ARGS will be {double*, int}. The resulting
23363 vector will be {double}, indicating that `T' is bound to `double'. */
23364
23365 static tree
23366 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
23367 {
23368 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
23369 tree spec_args
23370 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
23371 int i, ntparms = TREE_VEC_LENGTH (tparms);
23372 tree deduced_args;
23373 tree innermost_deduced_args;
23374
23375 innermost_deduced_args = make_tree_vec (ntparms);
23376 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
23377 {
23378 deduced_args = copy_node (args);
23379 SET_TMPL_ARGS_LEVEL (deduced_args,
23380 TMPL_ARGS_DEPTH (deduced_args),
23381 innermost_deduced_args);
23382 }
23383 else
23384 deduced_args = innermost_deduced_args;
23385
23386 bool tried_array_deduction = (cxx_dialect < cxx17);
23387 again:
23388 if (unify (tparms, deduced_args,
23389 INNERMOST_TEMPLATE_ARGS (spec_args),
23390 INNERMOST_TEMPLATE_ARGS (args),
23391 UNIFY_ALLOW_NONE, /*explain_p=*/false))
23392 return NULL_TREE;
23393
23394 for (i = 0; i < ntparms; ++i)
23395 if (! TREE_VEC_ELT (innermost_deduced_args, i))
23396 {
23397 if (!tried_array_deduction)
23398 {
23399 try_array_deduction (tparms, innermost_deduced_args,
23400 INNERMOST_TEMPLATE_ARGS (spec_args));
23401 tried_array_deduction = true;
23402 if (TREE_VEC_ELT (innermost_deduced_args, i))
23403 goto again;
23404 }
23405 return NULL_TREE;
23406 }
23407
23408 if (!push_tinst_level (spec_tmpl, deduced_args))
23409 {
23410 excessive_deduction_depth = true;
23411 return NULL_TREE;
23412 }
23413
23414 /* Verify that nondeduced template arguments agree with the type
23415 obtained from argument deduction.
23416
23417 For example:
23418
23419 struct A { typedef int X; };
23420 template <class T, class U> struct C {};
23421 template <class T> struct C<T, typename T::X> {};
23422
23423 Then with the instantiation `C<A, int>', we can deduce that
23424 `T' is `A' but unify () does not check whether `typename T::X'
23425 is `int'. */
23426 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
23427
23428 if (spec_args != error_mark_node)
23429 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
23430 INNERMOST_TEMPLATE_ARGS (spec_args),
23431 tmpl, tf_none, false, false);
23432
23433 pop_tinst_level ();
23434
23435 if (spec_args == error_mark_node
23436 /* We only need to check the innermost arguments; the other
23437 arguments will always agree. */
23438 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
23439 INNERMOST_TEMPLATE_ARGS (args)))
23440 return NULL_TREE;
23441
23442 /* Now that we have bindings for all of the template arguments,
23443 ensure that the arguments deduced for the template template
23444 parameters have compatible template parameter lists. See the use
23445 of template_template_parm_bindings_ok_p in fn_type_unification
23446 for more information. */
23447 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
23448 return NULL_TREE;
23449
23450 return deduced_args;
23451 }
23452
23453 // Compare two function templates T1 and T2 by deducing bindings
23454 // from one against the other. If both deductions succeed, compare
23455 // constraints to see which is more constrained.
23456 static int
23457 more_specialized_inst (tree t1, tree t2)
23458 {
23459 int fate = 0;
23460 int count = 0;
23461
23462 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
23463 {
23464 --fate;
23465 ++count;
23466 }
23467
23468 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
23469 {
23470 ++fate;
23471 ++count;
23472 }
23473
23474 // If both deductions succeed, then one may be more constrained.
23475 if (count == 2 && fate == 0)
23476 fate = more_constrained (t1, t2);
23477
23478 return fate;
23479 }
23480
23481 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
23482 Return the TREE_LIST node with the most specialized template, if
23483 any. If there is no most specialized template, the error_mark_node
23484 is returned.
23485
23486 Note that this function does not look at, or modify, the
23487 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
23488 returned is one of the elements of INSTANTIATIONS, callers may
23489 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
23490 and retrieve it from the value returned. */
23491
23492 tree
23493 most_specialized_instantiation (tree templates)
23494 {
23495 tree fn, champ;
23496
23497 ++processing_template_decl;
23498
23499 champ = templates;
23500 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
23501 {
23502 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
23503 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
23504 if (fate == -1)
23505 champ = fn;
23506 else if (!fate)
23507 {
23508 /* Equally specialized, move to next function. If there
23509 is no next function, nothing's most specialized. */
23510 fn = TREE_CHAIN (fn);
23511 champ = fn;
23512 if (!fn)
23513 break;
23514 }
23515 }
23516
23517 if (champ)
23518 /* Now verify that champ is better than everything earlier in the
23519 instantiation list. */
23520 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
23521 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
23522 {
23523 champ = NULL_TREE;
23524 break;
23525 }
23526 }
23527
23528 processing_template_decl--;
23529
23530 if (!champ)
23531 return error_mark_node;
23532
23533 return champ;
23534 }
23535
23536 /* If DECL is a specialization of some template, return the most
23537 general such template. Otherwise, returns NULL_TREE.
23538
23539 For example, given:
23540
23541 template <class T> struct S { template <class U> void f(U); };
23542
23543 if TMPL is `template <class U> void S<int>::f(U)' this will return
23544 the full template. This function will not trace past partial
23545 specializations, however. For example, given in addition:
23546
23547 template <class T> struct S<T*> { template <class U> void f(U); };
23548
23549 if TMPL is `template <class U> void S<int*>::f(U)' this will return
23550 `template <class T> template <class U> S<T*>::f(U)'. */
23551
23552 tree
23553 most_general_template (tree decl)
23554 {
23555 if (TREE_CODE (decl) != TEMPLATE_DECL)
23556 {
23557 if (tree tinfo = get_template_info (decl))
23558 decl = TI_TEMPLATE (tinfo);
23559 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
23560 template friend, or a FIELD_DECL for a capture pack. */
23561 if (TREE_CODE (decl) != TEMPLATE_DECL)
23562 return NULL_TREE;
23563 }
23564
23565 /* Look for more and more general templates. */
23566 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
23567 {
23568 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
23569 (See cp-tree.h for details.) */
23570 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
23571 break;
23572
23573 if (CLASS_TYPE_P (TREE_TYPE (decl))
23574 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
23575 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
23576 break;
23577
23578 /* Stop if we run into an explicitly specialized class template. */
23579 if (!DECL_NAMESPACE_SCOPE_P (decl)
23580 && DECL_CONTEXT (decl)
23581 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
23582 break;
23583
23584 decl = DECL_TI_TEMPLATE (decl);
23585 }
23586
23587 return decl;
23588 }
23589
23590 /* Return the most specialized of the template partial specializations
23591 which can produce TARGET, a specialization of some class or variable
23592 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
23593 a TEMPLATE_DECL node corresponding to the partial specialization, while
23594 the TREE_PURPOSE is the set of template arguments that must be
23595 substituted into the template pattern in order to generate TARGET.
23596
23597 If the choice of partial specialization is ambiguous, a diagnostic
23598 is issued, and the error_mark_node is returned. If there are no
23599 partial specializations matching TARGET, then NULL_TREE is
23600 returned, indicating that the primary template should be used. */
23601
23602 static tree
23603 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
23604 {
23605 tree list = NULL_TREE;
23606 tree t;
23607 tree champ;
23608 int fate;
23609 bool ambiguous_p;
23610 tree outer_args = NULL_TREE;
23611 tree tmpl, args;
23612
23613 if (TYPE_P (target))
23614 {
23615 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
23616 tmpl = TI_TEMPLATE (tinfo);
23617 args = TI_ARGS (tinfo);
23618 }
23619 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
23620 {
23621 tmpl = TREE_OPERAND (target, 0);
23622 args = TREE_OPERAND (target, 1);
23623 }
23624 else if (VAR_P (target))
23625 {
23626 tree tinfo = DECL_TEMPLATE_INFO (target);
23627 tmpl = TI_TEMPLATE (tinfo);
23628 args = TI_ARGS (tinfo);
23629 }
23630 else
23631 gcc_unreachable ();
23632
23633 tree main_tmpl = most_general_template (tmpl);
23634
23635 /* For determining which partial specialization to use, only the
23636 innermost args are interesting. */
23637 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
23638 {
23639 outer_args = strip_innermost_template_args (args, 1);
23640 args = INNERMOST_TEMPLATE_ARGS (args);
23641 }
23642
23643 /* The caller hasn't called push_to_top_level yet, but we need
23644 get_partial_spec_bindings to be done in non-template context so that we'll
23645 fully resolve everything. */
23646 processing_template_decl_sentinel ptds;
23647
23648 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
23649 {
23650 tree spec_args;
23651 tree spec_tmpl = TREE_VALUE (t);
23652
23653 if (outer_args)
23654 {
23655 /* Substitute in the template args from the enclosing class. */
23656 ++processing_template_decl;
23657 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
23658 --processing_template_decl;
23659 }
23660
23661 if (spec_tmpl == error_mark_node)
23662 return error_mark_node;
23663
23664 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
23665 if (spec_args)
23666 {
23667 if (outer_args)
23668 spec_args = add_to_template_args (outer_args, spec_args);
23669
23670 /* Keep the candidate only if the constraints are satisfied,
23671 or if we're not compiling with concepts. */
23672 if (!flag_concepts
23673 || constraints_satisfied_p (spec_tmpl, spec_args))
23674 {
23675 list = tree_cons (spec_args, TREE_VALUE (t), list);
23676 TREE_TYPE (list) = TREE_TYPE (t);
23677 }
23678 }
23679 }
23680
23681 if (! list)
23682 return NULL_TREE;
23683
23684 ambiguous_p = false;
23685 t = list;
23686 champ = t;
23687 t = TREE_CHAIN (t);
23688 for (; t; t = TREE_CHAIN (t))
23689 {
23690 fate = more_specialized_partial_spec (tmpl, champ, t);
23691 if (fate == 1)
23692 ;
23693 else
23694 {
23695 if (fate == 0)
23696 {
23697 t = TREE_CHAIN (t);
23698 if (! t)
23699 {
23700 ambiguous_p = true;
23701 break;
23702 }
23703 }
23704 champ = t;
23705 }
23706 }
23707
23708 if (!ambiguous_p)
23709 for (t = list; t && t != champ; t = TREE_CHAIN (t))
23710 {
23711 fate = more_specialized_partial_spec (tmpl, champ, t);
23712 if (fate != 1)
23713 {
23714 ambiguous_p = true;
23715 break;
23716 }
23717 }
23718
23719 if (ambiguous_p)
23720 {
23721 const char *str;
23722 char *spaces = NULL;
23723 if (!(complain & tf_error))
23724 return error_mark_node;
23725 if (TYPE_P (target))
23726 error ("ambiguous template instantiation for %q#T", target);
23727 else
23728 error ("ambiguous template instantiation for %q#D", target);
23729 str = ngettext ("candidate is:", "candidates are:", list_length (list));
23730 for (t = list; t; t = TREE_CHAIN (t))
23731 {
23732 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
23733 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
23734 "%s %#qS", spaces ? spaces : str, subst);
23735 spaces = spaces ? spaces : get_spaces (str);
23736 }
23737 free (spaces);
23738 return error_mark_node;
23739 }
23740
23741 return champ;
23742 }
23743
23744 /* Explicitly instantiate DECL. */
23745
23746 void
23747 do_decl_instantiation (tree decl, tree storage)
23748 {
23749 tree result = NULL_TREE;
23750 int extern_p = 0;
23751
23752 if (!decl || decl == error_mark_node)
23753 /* An error occurred, for which grokdeclarator has already issued
23754 an appropriate message. */
23755 return;
23756 else if (! DECL_LANG_SPECIFIC (decl))
23757 {
23758 error ("explicit instantiation of non-template %q#D", decl);
23759 return;
23760 }
23761 else if (DECL_DECLARED_CONCEPT_P (decl))
23762 {
23763 if (VAR_P (decl))
23764 error ("explicit instantiation of variable concept %q#D", decl);
23765 else
23766 error ("explicit instantiation of function concept %q#D", decl);
23767 return;
23768 }
23769
23770 bool var_templ = (DECL_TEMPLATE_INFO (decl)
23771 && variable_template_p (DECL_TI_TEMPLATE (decl)));
23772
23773 if (VAR_P (decl) && !var_templ)
23774 {
23775 /* There is an asymmetry here in the way VAR_DECLs and
23776 FUNCTION_DECLs are handled by grokdeclarator. In the case of
23777 the latter, the DECL we get back will be marked as a
23778 template instantiation, and the appropriate
23779 DECL_TEMPLATE_INFO will be set up. This does not happen for
23780 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
23781 should handle VAR_DECLs as it currently handles
23782 FUNCTION_DECLs. */
23783 if (!DECL_CLASS_SCOPE_P (decl))
23784 {
23785 error ("%qD is not a static data member of a class template", decl);
23786 return;
23787 }
23788 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
23789 if (!result || !VAR_P (result))
23790 {
23791 error ("no matching template for %qD found", decl);
23792 return;
23793 }
23794 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
23795 {
23796 error ("type %qT for explicit instantiation %qD does not match "
23797 "declared type %qT", TREE_TYPE (result), decl,
23798 TREE_TYPE (decl));
23799 return;
23800 }
23801 }
23802 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
23803 {
23804 error ("explicit instantiation of %q#D", decl);
23805 return;
23806 }
23807 else
23808 result = decl;
23809
23810 /* Check for various error cases. Note that if the explicit
23811 instantiation is valid the RESULT will currently be marked as an
23812 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
23813 until we get here. */
23814
23815 if (DECL_TEMPLATE_SPECIALIZATION (result))
23816 {
23817 /* DR 259 [temp.spec].
23818
23819 Both an explicit instantiation and a declaration of an explicit
23820 specialization shall not appear in a program unless the explicit
23821 instantiation follows a declaration of the explicit specialization.
23822
23823 For a given set of template parameters, if an explicit
23824 instantiation of a template appears after a declaration of an
23825 explicit specialization for that template, the explicit
23826 instantiation has no effect. */
23827 return;
23828 }
23829 else if (DECL_EXPLICIT_INSTANTIATION (result))
23830 {
23831 /* [temp.spec]
23832
23833 No program shall explicitly instantiate any template more
23834 than once.
23835
23836 We check DECL_NOT_REALLY_EXTERN so as not to complain when
23837 the first instantiation was `extern' and the second is not,
23838 and EXTERN_P for the opposite case. */
23839 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
23840 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
23841 /* If an "extern" explicit instantiation follows an ordinary
23842 explicit instantiation, the template is instantiated. */
23843 if (extern_p)
23844 return;
23845 }
23846 else if (!DECL_IMPLICIT_INSTANTIATION (result))
23847 {
23848 error ("no matching template for %qD found", result);
23849 return;
23850 }
23851 else if (!DECL_TEMPLATE_INFO (result))
23852 {
23853 permerror (input_location, "explicit instantiation of non-template %q#D", result);
23854 return;
23855 }
23856
23857 if (storage == NULL_TREE)
23858 ;
23859 else if (storage == ridpointers[(int) RID_EXTERN])
23860 {
23861 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
23862 pedwarn (input_location, OPT_Wpedantic,
23863 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
23864 "instantiations");
23865 extern_p = 1;
23866 }
23867 else
23868 error ("storage class %qD applied to template instantiation", storage);
23869
23870 check_explicit_instantiation_namespace (result);
23871 mark_decl_instantiated (result, extern_p);
23872 if (! extern_p)
23873 instantiate_decl (result, /*defer_ok=*/true,
23874 /*expl_inst_class_mem_p=*/false);
23875 }
23876
23877 static void
23878 mark_class_instantiated (tree t, int extern_p)
23879 {
23880 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
23881 SET_CLASSTYPE_INTERFACE_KNOWN (t);
23882 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
23883 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
23884 if (! extern_p)
23885 {
23886 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
23887 rest_of_type_compilation (t, 1);
23888 }
23889 }
23890
23891 /* Called from do_type_instantiation through binding_table_foreach to
23892 do recursive instantiation for the type bound in ENTRY. */
23893 static void
23894 bt_instantiate_type_proc (binding_entry entry, void *data)
23895 {
23896 tree storage = *(tree *) data;
23897
23898 if (MAYBE_CLASS_TYPE_P (entry->type)
23899 && CLASSTYPE_TEMPLATE_INFO (entry->type)
23900 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
23901 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
23902 }
23903
23904 /* Perform an explicit instantiation of template class T. STORAGE, if
23905 non-null, is the RID for extern, inline or static. COMPLAIN is
23906 nonzero if this is called from the parser, zero if called recursively,
23907 since the standard is unclear (as detailed below). */
23908
23909 void
23910 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
23911 {
23912 int extern_p = 0;
23913 int nomem_p = 0;
23914 int static_p = 0;
23915 int previous_instantiation_extern_p = 0;
23916
23917 if (TREE_CODE (t) == TYPE_DECL)
23918 t = TREE_TYPE (t);
23919
23920 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
23921 {
23922 tree tmpl =
23923 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
23924 if (tmpl)
23925 error ("explicit instantiation of non-class template %qD", tmpl);
23926 else
23927 error ("explicit instantiation of non-template type %qT", t);
23928 return;
23929 }
23930
23931 complete_type (t);
23932
23933 if (!COMPLETE_TYPE_P (t))
23934 {
23935 if (complain & tf_error)
23936 error ("explicit instantiation of %q#T before definition of template",
23937 t);
23938 return;
23939 }
23940
23941 if (storage != NULL_TREE)
23942 {
23943 if (!in_system_header_at (input_location))
23944 {
23945 if (storage == ridpointers[(int) RID_EXTERN])
23946 {
23947 if (cxx_dialect == cxx98)
23948 pedwarn (input_location, OPT_Wpedantic,
23949 "ISO C++ 1998 forbids the use of %<extern%> on "
23950 "explicit instantiations");
23951 }
23952 else
23953 pedwarn (input_location, OPT_Wpedantic,
23954 "ISO C++ forbids the use of %qE"
23955 " on explicit instantiations", storage);
23956 }
23957
23958 if (storage == ridpointers[(int) RID_INLINE])
23959 nomem_p = 1;
23960 else if (storage == ridpointers[(int) RID_EXTERN])
23961 extern_p = 1;
23962 else if (storage == ridpointers[(int) RID_STATIC])
23963 static_p = 1;
23964 else
23965 {
23966 error ("storage class %qD applied to template instantiation",
23967 storage);
23968 extern_p = 0;
23969 }
23970 }
23971
23972 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
23973 {
23974 /* DR 259 [temp.spec].
23975
23976 Both an explicit instantiation and a declaration of an explicit
23977 specialization shall not appear in a program unless the explicit
23978 instantiation follows a declaration of the explicit specialization.
23979
23980 For a given set of template parameters, if an explicit
23981 instantiation of a template appears after a declaration of an
23982 explicit specialization for that template, the explicit
23983 instantiation has no effect. */
23984 return;
23985 }
23986 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
23987 {
23988 /* [temp.spec]
23989
23990 No program shall explicitly instantiate any template more
23991 than once.
23992
23993 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
23994 instantiation was `extern'. If EXTERN_P then the second is.
23995 These cases are OK. */
23996 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
23997
23998 if (!previous_instantiation_extern_p && !extern_p
23999 && (complain & tf_error))
24000 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
24001
24002 /* If we've already instantiated the template, just return now. */
24003 if (!CLASSTYPE_INTERFACE_ONLY (t))
24004 return;
24005 }
24006
24007 check_explicit_instantiation_namespace (TYPE_NAME (t));
24008 mark_class_instantiated (t, extern_p);
24009
24010 if (nomem_p)
24011 return;
24012
24013 /* In contrast to implicit instantiation, where only the
24014 declarations, and not the definitions, of members are
24015 instantiated, we have here:
24016
24017 [temp.explicit]
24018
24019 The explicit instantiation of a class template specialization
24020 implies the instantiation of all of its members not
24021 previously explicitly specialized in the translation unit
24022 containing the explicit instantiation.
24023
24024 Of course, we can't instantiate member template classes, since we
24025 don't have any arguments for them. Note that the standard is
24026 unclear on whether the instantiation of the members are
24027 *explicit* instantiations or not. However, the most natural
24028 interpretation is that it should be an explicit
24029 instantiation. */
24030 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
24031 if ((VAR_P (fld)
24032 || (TREE_CODE (fld) == FUNCTION_DECL
24033 && !static_p
24034 && user_provided_p (fld)))
24035 && DECL_TEMPLATE_INSTANTIATION (fld))
24036 {
24037 mark_decl_instantiated (fld, extern_p);
24038 if (! extern_p)
24039 instantiate_decl (fld, /*defer_ok=*/true,
24040 /*expl_inst_class_mem_p=*/true);
24041 }
24042
24043 if (CLASSTYPE_NESTED_UTDS (t))
24044 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
24045 bt_instantiate_type_proc, &storage);
24046 }
24047
24048 /* Given a function DECL, which is a specialization of TMPL, modify
24049 DECL to be a re-instantiation of TMPL with the same template
24050 arguments. TMPL should be the template into which tsubst'ing
24051 should occur for DECL, not the most general template.
24052
24053 One reason for doing this is a scenario like this:
24054
24055 template <class T>
24056 void f(const T&, int i);
24057
24058 void g() { f(3, 7); }
24059
24060 template <class T>
24061 void f(const T& t, const int i) { }
24062
24063 Note that when the template is first instantiated, with
24064 instantiate_template, the resulting DECL will have no name for the
24065 first parameter, and the wrong type for the second. So, when we go
24066 to instantiate the DECL, we regenerate it. */
24067
24068 static void
24069 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
24070 {
24071 /* The arguments used to instantiate DECL, from the most general
24072 template. */
24073 tree code_pattern;
24074
24075 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
24076
24077 /* Make sure that we can see identifiers, and compute access
24078 correctly. */
24079 push_access_scope (decl);
24080
24081 if (TREE_CODE (decl) == FUNCTION_DECL)
24082 {
24083 tree decl_parm;
24084 tree pattern_parm;
24085 tree specs;
24086 int args_depth;
24087 int parms_depth;
24088
24089 args_depth = TMPL_ARGS_DEPTH (args);
24090 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
24091 if (args_depth > parms_depth)
24092 args = get_innermost_template_args (args, parms_depth);
24093
24094 /* Instantiate a dynamic exception-specification. noexcept will be
24095 handled below. */
24096 if (tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern)))
24097 if (TREE_VALUE (raises))
24098 {
24099 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
24100 args, tf_error, NULL_TREE,
24101 /*defer_ok*/false);
24102 if (specs && specs != error_mark_node)
24103 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
24104 specs);
24105 }
24106
24107 /* Merge parameter declarations. */
24108 decl_parm = skip_artificial_parms_for (decl,
24109 DECL_ARGUMENTS (decl));
24110 pattern_parm
24111 = skip_artificial_parms_for (code_pattern,
24112 DECL_ARGUMENTS (code_pattern));
24113 while (decl_parm && !DECL_PACK_P (pattern_parm))
24114 {
24115 tree parm_type;
24116 tree attributes;
24117
24118 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
24119 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
24120 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
24121 NULL_TREE);
24122 parm_type = type_decays_to (parm_type);
24123 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
24124 TREE_TYPE (decl_parm) = parm_type;
24125 attributes = DECL_ATTRIBUTES (pattern_parm);
24126 if (DECL_ATTRIBUTES (decl_parm) != attributes)
24127 {
24128 DECL_ATTRIBUTES (decl_parm) = attributes;
24129 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
24130 }
24131 decl_parm = DECL_CHAIN (decl_parm);
24132 pattern_parm = DECL_CHAIN (pattern_parm);
24133 }
24134 /* Merge any parameters that match with the function parameter
24135 pack. */
24136 if (pattern_parm && DECL_PACK_P (pattern_parm))
24137 {
24138 int i, len;
24139 tree expanded_types;
24140 /* Expand the TYPE_PACK_EXPANSION that provides the types for
24141 the parameters in this function parameter pack. */
24142 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
24143 args, tf_error, NULL_TREE);
24144 len = TREE_VEC_LENGTH (expanded_types);
24145 for (i = 0; i < len; i++)
24146 {
24147 tree parm_type;
24148 tree attributes;
24149
24150 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
24151 /* Rename the parameter to include the index. */
24152 DECL_NAME (decl_parm) =
24153 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
24154 parm_type = TREE_VEC_ELT (expanded_types, i);
24155 parm_type = type_decays_to (parm_type);
24156 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
24157 TREE_TYPE (decl_parm) = parm_type;
24158 attributes = DECL_ATTRIBUTES (pattern_parm);
24159 if (DECL_ATTRIBUTES (decl_parm) != attributes)
24160 {
24161 DECL_ATTRIBUTES (decl_parm) = attributes;
24162 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
24163 }
24164 decl_parm = DECL_CHAIN (decl_parm);
24165 }
24166 }
24167 /* Merge additional specifiers from the CODE_PATTERN. */
24168 if (DECL_DECLARED_INLINE_P (code_pattern)
24169 && !DECL_DECLARED_INLINE_P (decl))
24170 DECL_DECLARED_INLINE_P (decl) = 1;
24171
24172 maybe_instantiate_noexcept (decl, tf_error);
24173 }
24174 else if (VAR_P (decl))
24175 {
24176 start_lambda_scope (decl);
24177 DECL_INITIAL (decl) =
24178 tsubst_init (DECL_INITIAL (code_pattern), decl, args,
24179 tf_error, DECL_TI_TEMPLATE (decl));
24180 finish_lambda_scope ();
24181 if (VAR_HAD_UNKNOWN_BOUND (decl))
24182 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
24183 tf_error, DECL_TI_TEMPLATE (decl));
24184 }
24185 else
24186 gcc_unreachable ();
24187
24188 pop_access_scope (decl);
24189 }
24190
24191 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
24192 substituted to get DECL. */
24193
24194 tree
24195 template_for_substitution (tree decl)
24196 {
24197 tree tmpl = DECL_TI_TEMPLATE (decl);
24198
24199 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
24200 for the instantiation. This is not always the most general
24201 template. Consider, for example:
24202
24203 template <class T>
24204 struct S { template <class U> void f();
24205 template <> void f<int>(); };
24206
24207 and an instantiation of S<double>::f<int>. We want TD to be the
24208 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
24209 while (/* An instantiation cannot have a definition, so we need a
24210 more general template. */
24211 DECL_TEMPLATE_INSTANTIATION (tmpl)
24212 /* We must also deal with friend templates. Given:
24213
24214 template <class T> struct S {
24215 template <class U> friend void f() {};
24216 };
24217
24218 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
24219 so far as the language is concerned, but that's still
24220 where we get the pattern for the instantiation from. On
24221 other hand, if the definition comes outside the class, say:
24222
24223 template <class T> struct S {
24224 template <class U> friend void f();
24225 };
24226 template <class U> friend void f() {}
24227
24228 we don't need to look any further. That's what the check for
24229 DECL_INITIAL is for. */
24230 || (TREE_CODE (decl) == FUNCTION_DECL
24231 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
24232 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
24233 {
24234 /* The present template, TD, should not be a definition. If it
24235 were a definition, we should be using it! Note that we
24236 cannot restructure the loop to just keep going until we find
24237 a template with a definition, since that might go too far if
24238 a specialization was declared, but not defined. */
24239
24240 /* Fetch the more general template. */
24241 tmpl = DECL_TI_TEMPLATE (tmpl);
24242 }
24243
24244 return tmpl;
24245 }
24246
24247 /* Returns true if we need to instantiate this template instance even if we
24248 know we aren't going to emit it. */
24249
24250 bool
24251 always_instantiate_p (tree decl)
24252 {
24253 /* We always instantiate inline functions so that we can inline them. An
24254 explicit instantiation declaration prohibits implicit instantiation of
24255 non-inline functions. With high levels of optimization, we would
24256 normally inline non-inline functions -- but we're not allowed to do
24257 that for "extern template" functions. Therefore, we check
24258 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
24259 return ((TREE_CODE (decl) == FUNCTION_DECL
24260 && (DECL_DECLARED_INLINE_P (decl)
24261 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
24262 /* And we need to instantiate static data members so that
24263 their initializers are available in integral constant
24264 expressions. */
24265 || (VAR_P (decl)
24266 && decl_maybe_constant_var_p (decl)));
24267 }
24268
24269 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
24270 instantiate it now, modifying TREE_TYPE (fn). Returns false on
24271 error, true otherwise. */
24272
24273 bool
24274 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
24275 {
24276 tree fntype, spec, noex, clone;
24277
24278 /* Don't instantiate a noexcept-specification from template context. */
24279 if (processing_template_decl
24280 && (!flag_noexcept_type || type_dependent_expression_p (fn)))
24281 return true;
24282
24283 if (DECL_CLONED_FUNCTION_P (fn))
24284 fn = DECL_CLONED_FUNCTION (fn);
24285
24286 tree orig_fn = NULL_TREE;
24287 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
24288 its FUNCTION_DECL for the rest of this function -- push_access_scope
24289 doesn't accept TEMPLATE_DECLs. */
24290 if (DECL_FUNCTION_TEMPLATE_P (fn))
24291 {
24292 orig_fn = fn;
24293 fn = DECL_TEMPLATE_RESULT (fn);
24294 }
24295
24296 fntype = TREE_TYPE (fn);
24297 spec = TYPE_RAISES_EXCEPTIONS (fntype);
24298
24299 if (!spec || !TREE_PURPOSE (spec))
24300 return true;
24301
24302 noex = TREE_PURPOSE (spec);
24303
24304 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
24305 {
24306 static hash_set<tree>* fns = new hash_set<tree>;
24307 bool added = false;
24308 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
24309 {
24310 spec = get_defaulted_eh_spec (fn, complain);
24311 if (spec == error_mark_node)
24312 /* This might have failed because of an unparsed DMI, so
24313 let's try again later. */
24314 return false;
24315 }
24316 else if (!(added = !fns->add (fn)))
24317 {
24318 /* If hash_set::add returns true, the element was already there. */
24319 location_t loc = cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex),
24320 DECL_SOURCE_LOCATION (fn));
24321 error_at (loc,
24322 "exception specification of %qD depends on itself",
24323 fn);
24324 spec = noexcept_false_spec;
24325 }
24326 else if (push_tinst_level (fn))
24327 {
24328 push_to_top_level ();
24329 push_access_scope (fn);
24330 push_deferring_access_checks (dk_no_deferred);
24331 input_location = DECL_SOURCE_LOCATION (fn);
24332
24333 /* If needed, set current_class_ptr for the benefit of
24334 tsubst_copy/PARM_DECL. */
24335 tree tdecl = DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (fn));
24336 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tdecl))
24337 {
24338 tree this_parm = DECL_ARGUMENTS (tdecl);
24339 current_class_ptr = NULL_TREE;
24340 current_class_ref = cp_build_fold_indirect_ref (this_parm);
24341 current_class_ptr = this_parm;
24342 }
24343
24344 /* If this function is represented by a TEMPLATE_DECL, then
24345 the deferred noexcept-specification might still contain
24346 dependent types, even after substitution. And we need the
24347 dependency check functions to work in build_noexcept_spec. */
24348 if (orig_fn)
24349 ++processing_template_decl;
24350
24351 /* Do deferred instantiation of the noexcept-specifier. */
24352 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
24353 DEFERRED_NOEXCEPT_ARGS (noex),
24354 tf_warning_or_error, fn,
24355 /*function_p=*/false,
24356 /*i_c_e_p=*/true);
24357
24358 /* Build up the noexcept-specification. */
24359 spec = build_noexcept_spec (noex, tf_warning_or_error);
24360
24361 if (orig_fn)
24362 --processing_template_decl;
24363
24364 pop_deferring_access_checks ();
24365 pop_access_scope (fn);
24366 pop_tinst_level ();
24367 pop_from_top_level ();
24368 }
24369 else
24370 spec = noexcept_false_spec;
24371
24372 if (added)
24373 fns->remove (fn);
24374
24375 if (spec == error_mark_node)
24376 {
24377 /* This failed with a hard error, so let's go with false. */
24378 gcc_assert (seen_error ());
24379 spec = noexcept_false_spec;
24380 }
24381
24382 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
24383 if (orig_fn)
24384 TREE_TYPE (orig_fn) = TREE_TYPE (fn);
24385 }
24386
24387 FOR_EACH_CLONE (clone, fn)
24388 {
24389 if (TREE_TYPE (clone) == fntype)
24390 TREE_TYPE (clone) = TREE_TYPE (fn);
24391 else
24392 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
24393 }
24394
24395 return true;
24396 }
24397
24398 /* We're starting to process the function INST, an instantiation of PATTERN;
24399 add their parameters to local_specializations. */
24400
24401 static void
24402 register_parameter_specializations (tree pattern, tree inst)
24403 {
24404 tree tmpl_parm = DECL_ARGUMENTS (pattern);
24405 tree spec_parm = DECL_ARGUMENTS (inst);
24406 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
24407 {
24408 register_local_specialization (spec_parm, tmpl_parm);
24409 spec_parm = skip_artificial_parms_for (inst, spec_parm);
24410 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
24411 }
24412 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
24413 {
24414 if (!DECL_PACK_P (tmpl_parm))
24415 {
24416 register_local_specialization (spec_parm, tmpl_parm);
24417 spec_parm = DECL_CHAIN (spec_parm);
24418 }
24419 else
24420 {
24421 /* Register the (value) argument pack as a specialization of
24422 TMPL_PARM, then move on. */
24423 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
24424 register_local_specialization (argpack, tmpl_parm);
24425 }
24426 }
24427 gcc_assert (!spec_parm);
24428 }
24429
24430 /* Produce the definition of D, a _DECL generated from a template. If
24431 DEFER_OK is true, then we don't have to actually do the
24432 instantiation now; we just have to do it sometime. Normally it is
24433 an error if this is an explicit instantiation but D is undefined.
24434 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
24435 instantiated class template. */
24436
24437 tree
24438 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
24439 {
24440 tree tmpl = DECL_TI_TEMPLATE (d);
24441 tree gen_args;
24442 tree args;
24443 tree td;
24444 tree code_pattern;
24445 tree spec;
24446 tree gen_tmpl;
24447 bool pattern_defined;
24448 location_t saved_loc = input_location;
24449 int saved_unevaluated_operand = cp_unevaluated_operand;
24450 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
24451 bool external_p;
24452 bool deleted_p;
24453
24454 /* This function should only be used to instantiate templates for
24455 functions and static member variables. */
24456 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
24457
24458 /* A concept is never instantiated. */
24459 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
24460
24461 /* Variables are never deferred; if instantiation is required, they
24462 are instantiated right away. That allows for better code in the
24463 case that an expression refers to the value of the variable --
24464 if the variable has a constant value the referring expression can
24465 take advantage of that fact. */
24466 if (VAR_P (d))
24467 defer_ok = false;
24468
24469 /* Don't instantiate cloned functions. Instead, instantiate the
24470 functions they cloned. */
24471 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
24472 d = DECL_CLONED_FUNCTION (d);
24473
24474 if (DECL_TEMPLATE_INSTANTIATED (d)
24475 || (TREE_CODE (d) == FUNCTION_DECL
24476 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
24477 || DECL_TEMPLATE_SPECIALIZATION (d))
24478 /* D has already been instantiated or explicitly specialized, so
24479 there's nothing for us to do here.
24480
24481 It might seem reasonable to check whether or not D is an explicit
24482 instantiation, and, if so, stop here. But when an explicit
24483 instantiation is deferred until the end of the compilation,
24484 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
24485 the instantiation. */
24486 return d;
24487
24488 /* Check to see whether we know that this template will be
24489 instantiated in some other file, as with "extern template"
24490 extension. */
24491 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
24492
24493 /* In general, we do not instantiate such templates. */
24494 if (external_p && !always_instantiate_p (d))
24495 return d;
24496
24497 gen_tmpl = most_general_template (tmpl);
24498 gen_args = DECL_TI_ARGS (d);
24499
24500 if (tmpl != gen_tmpl)
24501 /* We should already have the extra args. */
24502 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
24503 == TMPL_ARGS_DEPTH (gen_args));
24504 /* And what's in the hash table should match D. */
24505 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
24506 || spec == NULL_TREE);
24507
24508 /* This needs to happen before any tsubsting. */
24509 if (! push_tinst_level (d))
24510 return d;
24511
24512 timevar_push (TV_TEMPLATE_INST);
24513
24514 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
24515 for the instantiation. */
24516 td = template_for_substitution (d);
24517 args = gen_args;
24518
24519 if (VAR_P (d))
24520 {
24521 /* Look up an explicit specialization, if any. */
24522 tree tid = lookup_template_variable (gen_tmpl, gen_args);
24523 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
24524 if (elt && elt != error_mark_node)
24525 {
24526 td = TREE_VALUE (elt);
24527 args = TREE_PURPOSE (elt);
24528 }
24529 }
24530
24531 code_pattern = DECL_TEMPLATE_RESULT (td);
24532
24533 /* We should never be trying to instantiate a member of a class
24534 template or partial specialization. */
24535 gcc_assert (d != code_pattern);
24536
24537 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
24538 || DECL_TEMPLATE_SPECIALIZATION (td))
24539 /* In the case of a friend template whose definition is provided
24540 outside the class, we may have too many arguments. Drop the
24541 ones we don't need. The same is true for specializations. */
24542 args = get_innermost_template_args
24543 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
24544
24545 if (TREE_CODE (d) == FUNCTION_DECL)
24546 {
24547 deleted_p = DECL_DELETED_FN (code_pattern);
24548 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
24549 && DECL_INITIAL (code_pattern) != error_mark_node)
24550 || DECL_DEFAULTED_FN (code_pattern)
24551 || deleted_p);
24552 }
24553 else
24554 {
24555 deleted_p = false;
24556 if (DECL_CLASS_SCOPE_P (code_pattern))
24557 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
24558 else
24559 pattern_defined = ! DECL_EXTERNAL (code_pattern);
24560 }
24561
24562 /* We may be in the middle of deferred access check. Disable it now. */
24563 push_deferring_access_checks (dk_no_deferred);
24564
24565 /* Unless an explicit instantiation directive has already determined
24566 the linkage of D, remember that a definition is available for
24567 this entity. */
24568 if (pattern_defined
24569 && !DECL_INTERFACE_KNOWN (d)
24570 && !DECL_NOT_REALLY_EXTERN (d))
24571 mark_definable (d);
24572
24573 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
24574 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
24575 input_location = DECL_SOURCE_LOCATION (d);
24576
24577 /* If D is a member of an explicitly instantiated class template,
24578 and no definition is available, treat it like an implicit
24579 instantiation. */
24580 if (!pattern_defined && expl_inst_class_mem_p
24581 && DECL_EXPLICIT_INSTANTIATION (d))
24582 {
24583 /* Leave linkage flags alone on instantiations with anonymous
24584 visibility. */
24585 if (TREE_PUBLIC (d))
24586 {
24587 DECL_NOT_REALLY_EXTERN (d) = 0;
24588 DECL_INTERFACE_KNOWN (d) = 0;
24589 }
24590 SET_DECL_IMPLICIT_INSTANTIATION (d);
24591 }
24592
24593 /* Defer all other templates, unless we have been explicitly
24594 forbidden from doing so. */
24595 if (/* If there is no definition, we cannot instantiate the
24596 template. */
24597 ! pattern_defined
24598 /* If it's OK to postpone instantiation, do so. */
24599 || defer_ok
24600 /* If this is a static data member that will be defined
24601 elsewhere, we don't want to instantiate the entire data
24602 member, but we do want to instantiate the initializer so that
24603 we can substitute that elsewhere. */
24604 || (external_p && VAR_P (d))
24605 /* Handle here a deleted function too, avoid generating
24606 its body (c++/61080). */
24607 || deleted_p)
24608 {
24609 /* The definition of the static data member is now required so
24610 we must substitute the initializer. */
24611 if (VAR_P (d)
24612 && !DECL_INITIAL (d)
24613 && DECL_INITIAL (code_pattern))
24614 {
24615 tree ns;
24616 tree init;
24617 bool const_init = false;
24618 bool enter_context = DECL_CLASS_SCOPE_P (d);
24619
24620 ns = decl_namespace_context (d);
24621 push_nested_namespace (ns);
24622 if (enter_context)
24623 push_nested_class (DECL_CONTEXT (d));
24624 init = tsubst_expr (DECL_INITIAL (code_pattern),
24625 args,
24626 tf_warning_or_error, NULL_TREE,
24627 /*integral_constant_expression_p=*/false);
24628 /* If instantiating the initializer involved instantiating this
24629 again, don't call cp_finish_decl twice. */
24630 if (!DECL_INITIAL (d))
24631 {
24632 /* Make sure the initializer is still constant, in case of
24633 circular dependency (template/instantiate6.C). */
24634 const_init
24635 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
24636 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
24637 /*asmspec_tree=*/NULL_TREE,
24638 LOOKUP_ONLYCONVERTING);
24639 }
24640 if (enter_context)
24641 pop_nested_class ();
24642 pop_nested_namespace (ns);
24643 }
24644
24645 /* We restore the source position here because it's used by
24646 add_pending_template. */
24647 input_location = saved_loc;
24648
24649 if (at_eof && !pattern_defined
24650 && DECL_EXPLICIT_INSTANTIATION (d)
24651 && DECL_NOT_REALLY_EXTERN (d))
24652 /* [temp.explicit]
24653
24654 The definition of a non-exported function template, a
24655 non-exported member function template, or a non-exported
24656 member function or static data member of a class template
24657 shall be present in every translation unit in which it is
24658 explicitly instantiated. */
24659 permerror (input_location, "explicit instantiation of %qD "
24660 "but no definition available", d);
24661
24662 /* If we're in unevaluated context, we just wanted to get the
24663 constant value; this isn't an odr use, so don't queue
24664 a full instantiation. */
24665 if (cp_unevaluated_operand != 0)
24666 goto out;
24667 /* ??? Historically, we have instantiated inline functions, even
24668 when marked as "extern template". */
24669 if (!(external_p && VAR_P (d)))
24670 add_pending_template (d);
24671 goto out;
24672 }
24673
24674 bool push_to_top, nested;
24675 tree fn_context;
24676 fn_context = decl_function_context (d);
24677 if (LAMBDA_FUNCTION_P (d))
24678 /* tsubst_lambda_expr resolved any references to enclosing functions. */
24679 fn_context = NULL_TREE;
24680 nested = current_function_decl != NULL_TREE;
24681 push_to_top = !(nested && fn_context == current_function_decl);
24682
24683 vec<tree> omp_privatization_save;
24684 if (nested)
24685 save_omp_privatization_clauses (omp_privatization_save);
24686
24687 if (push_to_top)
24688 push_to_top_level ();
24689 else
24690 {
24691 gcc_assert (!processing_template_decl);
24692 push_function_context ();
24693 cp_unevaluated_operand = 0;
24694 c_inhibit_evaluation_warnings = 0;
24695 }
24696
24697 /* Mark D as instantiated so that recursive calls to
24698 instantiate_decl do not try to instantiate it again. */
24699 DECL_TEMPLATE_INSTANTIATED (d) = 1;
24700
24701 /* Regenerate the declaration in case the template has been modified
24702 by a subsequent redeclaration. */
24703 regenerate_decl_from_template (d, td, args);
24704
24705 /* We already set the file and line above. Reset them now in case
24706 they changed as a result of calling regenerate_decl_from_template. */
24707 input_location = DECL_SOURCE_LOCATION (d);
24708
24709 if (VAR_P (d))
24710 {
24711 tree init;
24712 bool const_init = false;
24713
24714 /* Clear out DECL_RTL; whatever was there before may not be right
24715 since we've reset the type of the declaration. */
24716 SET_DECL_RTL (d, NULL);
24717 DECL_IN_AGGR_P (d) = 0;
24718
24719 /* The initializer is placed in DECL_INITIAL by
24720 regenerate_decl_from_template so we don't need to
24721 push/pop_access_scope again here. Pull it out so that
24722 cp_finish_decl can process it. */
24723 init = DECL_INITIAL (d);
24724 DECL_INITIAL (d) = NULL_TREE;
24725 DECL_INITIALIZED_P (d) = 0;
24726
24727 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
24728 initializer. That function will defer actual emission until
24729 we have a chance to determine linkage. */
24730 DECL_EXTERNAL (d) = 0;
24731
24732 /* Enter the scope of D so that access-checking works correctly. */
24733 bool enter_context = DECL_CLASS_SCOPE_P (d);
24734 if (enter_context)
24735 push_nested_class (DECL_CONTEXT (d));
24736
24737 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
24738 int flags = (TINFO_VAR_DECLARED_CONSTINIT (DECL_TEMPLATE_INFO (d))
24739 ? LOOKUP_CONSTINIT : 0);
24740 cp_finish_decl (d, init, const_init, NULL_TREE, flags);
24741
24742 if (enter_context)
24743 pop_nested_class ();
24744
24745 if (variable_template_p (gen_tmpl))
24746 note_variable_template_instantiation (d);
24747 }
24748 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
24749 synthesize_method (d);
24750 else if (TREE_CODE (d) == FUNCTION_DECL)
24751 {
24752 /* Set up the list of local specializations. */
24753 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
24754 tree block = NULL_TREE;
24755
24756 /* Set up context. */
24757 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
24758 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
24759 block = push_stmt_list ();
24760 else
24761 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
24762
24763 /* Some typedefs referenced from within the template code need to be
24764 access checked at template instantiation time, i.e now. These
24765 types were added to the template at parsing time. Let's get those
24766 and perform the access checks then. */
24767 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
24768 args);
24769
24770 /* Create substitution entries for the parameters. */
24771 register_parameter_specializations (code_pattern, d);
24772
24773 /* Substitute into the body of the function. */
24774 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
24775 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
24776 tf_warning_or_error, tmpl);
24777 else
24778 {
24779 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
24780 tf_warning_or_error, tmpl,
24781 /*integral_constant_expression_p=*/false);
24782
24783 /* Set the current input_location to the end of the function
24784 so that finish_function knows where we are. */
24785 input_location
24786 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
24787
24788 /* Remember if we saw an infinite loop in the template. */
24789 current_function_infinite_loop
24790 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
24791 }
24792
24793 /* Finish the function. */
24794 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
24795 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
24796 DECL_SAVED_TREE (d) = pop_stmt_list (block);
24797 else
24798 {
24799 d = finish_function (/*inline_p=*/false);
24800 expand_or_defer_fn (d);
24801 }
24802
24803 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
24804 cp_check_omp_declare_reduction (d);
24805 }
24806
24807 /* We're not deferring instantiation any more. */
24808 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
24809
24810 if (push_to_top)
24811 pop_from_top_level ();
24812 else
24813 pop_function_context ();
24814
24815 if (nested)
24816 restore_omp_privatization_clauses (omp_privatization_save);
24817
24818 out:
24819 pop_deferring_access_checks ();
24820 timevar_pop (TV_TEMPLATE_INST);
24821 pop_tinst_level ();
24822 input_location = saved_loc;
24823 cp_unevaluated_operand = saved_unevaluated_operand;
24824 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
24825
24826 return d;
24827 }
24828
24829 /* Run through the list of templates that we wish we could
24830 instantiate, and instantiate any we can. RETRIES is the
24831 number of times we retry pending template instantiation. */
24832
24833 void
24834 instantiate_pending_templates (int retries)
24835 {
24836 int reconsider;
24837 location_t saved_loc = input_location;
24838
24839 /* Instantiating templates may trigger vtable generation. This in turn
24840 may require further template instantiations. We place a limit here
24841 to avoid infinite loop. */
24842 if (pending_templates && retries >= max_tinst_depth)
24843 {
24844 tree decl = pending_templates->tinst->maybe_get_node ();
24845
24846 fatal_error (input_location,
24847 "template instantiation depth exceeds maximum of %d"
24848 " instantiating %q+D, possibly from virtual table generation"
24849 " (use %<-ftemplate-depth=%> to increase the maximum)",
24850 max_tinst_depth, decl);
24851 if (TREE_CODE (decl) == FUNCTION_DECL)
24852 /* Pretend that we defined it. */
24853 DECL_INITIAL (decl) = error_mark_node;
24854 return;
24855 }
24856
24857 do
24858 {
24859 struct pending_template **t = &pending_templates;
24860 struct pending_template *last = NULL;
24861 reconsider = 0;
24862 while (*t)
24863 {
24864 tree instantiation = reopen_tinst_level ((*t)->tinst);
24865 bool complete = false;
24866
24867 if (TYPE_P (instantiation))
24868 {
24869 if (!COMPLETE_TYPE_P (instantiation))
24870 {
24871 instantiate_class_template (instantiation);
24872 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
24873 for (tree fld = TYPE_FIELDS (instantiation);
24874 fld; fld = TREE_CHAIN (fld))
24875 if ((VAR_P (fld)
24876 || (TREE_CODE (fld) == FUNCTION_DECL
24877 && !DECL_ARTIFICIAL (fld)))
24878 && DECL_TEMPLATE_INSTANTIATION (fld))
24879 instantiate_decl (fld,
24880 /*defer_ok=*/false,
24881 /*expl_inst_class_mem_p=*/false);
24882
24883 if (COMPLETE_TYPE_P (instantiation))
24884 reconsider = 1;
24885 }
24886
24887 complete = COMPLETE_TYPE_P (instantiation);
24888 }
24889 else
24890 {
24891 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
24892 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
24893 {
24894 instantiation
24895 = instantiate_decl (instantiation,
24896 /*defer_ok=*/false,
24897 /*expl_inst_class_mem_p=*/false);
24898 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
24899 reconsider = 1;
24900 }
24901
24902 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
24903 || DECL_TEMPLATE_INSTANTIATED (instantiation));
24904 }
24905
24906 if (complete)
24907 {
24908 /* If INSTANTIATION has been instantiated, then we don't
24909 need to consider it again in the future. */
24910 struct pending_template *drop = *t;
24911 *t = (*t)->next;
24912 set_refcount_ptr (drop->tinst);
24913 pending_template_freelist ().free (drop);
24914 }
24915 else
24916 {
24917 last = *t;
24918 t = &(*t)->next;
24919 }
24920 tinst_depth = 0;
24921 set_refcount_ptr (current_tinst_level);
24922 }
24923 last_pending_template = last;
24924 }
24925 while (reconsider);
24926
24927 input_location = saved_loc;
24928 }
24929
24930 /* Substitute ARGVEC into T, which is a list of initializers for
24931 either base class or a non-static data member. The TREE_PURPOSEs
24932 are DECLs, and the TREE_VALUEs are the initializer values. Used by
24933 instantiate_decl. */
24934
24935 static tree
24936 tsubst_initializer_list (tree t, tree argvec)
24937 {
24938 tree inits = NULL_TREE;
24939 tree target_ctor = error_mark_node;
24940
24941 for (; t; t = TREE_CHAIN (t))
24942 {
24943 tree decl;
24944 tree init;
24945 tree expanded_bases = NULL_TREE;
24946 tree expanded_arguments = NULL_TREE;
24947 int i, len = 1;
24948
24949 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
24950 {
24951 tree expr;
24952 tree arg;
24953
24954 /* Expand the base class expansion type into separate base
24955 classes. */
24956 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
24957 tf_warning_or_error,
24958 NULL_TREE);
24959 if (expanded_bases == error_mark_node)
24960 continue;
24961
24962 /* We'll be building separate TREE_LISTs of arguments for
24963 each base. */
24964 len = TREE_VEC_LENGTH (expanded_bases);
24965 expanded_arguments = make_tree_vec (len);
24966 for (i = 0; i < len; i++)
24967 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
24968
24969 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
24970 expand each argument in the TREE_VALUE of t. */
24971 expr = make_node (EXPR_PACK_EXPANSION);
24972 PACK_EXPANSION_LOCAL_P (expr) = true;
24973 PACK_EXPANSION_PARAMETER_PACKS (expr) =
24974 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
24975
24976 if (TREE_VALUE (t) == void_type_node)
24977 /* VOID_TYPE_NODE is used to indicate
24978 value-initialization. */
24979 {
24980 for (i = 0; i < len; i++)
24981 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
24982 }
24983 else
24984 {
24985 /* Substitute parameter packs into each argument in the
24986 TREE_LIST. */
24987 in_base_initializer = 1;
24988 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
24989 {
24990 tree expanded_exprs;
24991
24992 /* Expand the argument. */
24993 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
24994 expanded_exprs
24995 = tsubst_pack_expansion (expr, argvec,
24996 tf_warning_or_error,
24997 NULL_TREE);
24998 if (expanded_exprs == error_mark_node)
24999 continue;
25000
25001 /* Prepend each of the expanded expressions to the
25002 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
25003 for (i = 0; i < len; i++)
25004 {
25005 TREE_VEC_ELT (expanded_arguments, i) =
25006 tree_cons (NULL_TREE,
25007 TREE_VEC_ELT (expanded_exprs, i),
25008 TREE_VEC_ELT (expanded_arguments, i));
25009 }
25010 }
25011 in_base_initializer = 0;
25012
25013 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
25014 since we built them backwards. */
25015 for (i = 0; i < len; i++)
25016 {
25017 TREE_VEC_ELT (expanded_arguments, i) =
25018 nreverse (TREE_VEC_ELT (expanded_arguments, i));
25019 }
25020 }
25021 }
25022
25023 for (i = 0; i < len; ++i)
25024 {
25025 if (expanded_bases)
25026 {
25027 decl = TREE_VEC_ELT (expanded_bases, i);
25028 decl = expand_member_init (decl);
25029 init = TREE_VEC_ELT (expanded_arguments, i);
25030 }
25031 else
25032 {
25033 tree tmp;
25034 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
25035 tf_warning_or_error, NULL_TREE);
25036
25037 decl = expand_member_init (decl);
25038 if (decl && !DECL_P (decl))
25039 in_base_initializer = 1;
25040
25041 init = TREE_VALUE (t);
25042 tmp = init;
25043 if (init != void_type_node)
25044 init = tsubst_expr (init, argvec,
25045 tf_warning_or_error, NULL_TREE,
25046 /*integral_constant_expression_p=*/false);
25047 if (init == NULL_TREE && tmp != NULL_TREE)
25048 /* If we had an initializer but it instantiated to nothing,
25049 value-initialize the object. This will only occur when
25050 the initializer was a pack expansion where the parameter
25051 packs used in that expansion were of length zero. */
25052 init = void_type_node;
25053 in_base_initializer = 0;
25054 }
25055
25056 if (target_ctor != error_mark_node
25057 && init != error_mark_node)
25058 {
25059 error ("mem-initializer for %qD follows constructor delegation",
25060 decl);
25061 return inits;
25062 }
25063 /* Look for a target constructor. */
25064 if (init != error_mark_node
25065 && decl && CLASS_TYPE_P (decl)
25066 && same_type_p (decl, current_class_type))
25067 {
25068 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
25069 if (inits)
25070 {
25071 error ("constructor delegation follows mem-initializer for %qD",
25072 TREE_PURPOSE (inits));
25073 continue;
25074 }
25075 target_ctor = init;
25076 }
25077
25078 if (decl)
25079 {
25080 init = build_tree_list (decl, init);
25081 TREE_CHAIN (init) = inits;
25082 inits = init;
25083 }
25084 }
25085 }
25086 return inits;
25087 }
25088
25089 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
25090
25091 static void
25092 set_current_access_from_decl (tree decl)
25093 {
25094 if (TREE_PRIVATE (decl))
25095 current_access_specifier = access_private_node;
25096 else if (TREE_PROTECTED (decl))
25097 current_access_specifier = access_protected_node;
25098 else
25099 current_access_specifier = access_public_node;
25100 }
25101
25102 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
25103 is the instantiation (which should have been created with
25104 start_enum) and ARGS are the template arguments to use. */
25105
25106 static void
25107 tsubst_enum (tree tag, tree newtag, tree args)
25108 {
25109 tree e;
25110
25111 if (SCOPED_ENUM_P (newtag))
25112 begin_scope (sk_scoped_enum, newtag);
25113
25114 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
25115 {
25116 tree value;
25117 tree decl;
25118
25119 decl = TREE_VALUE (e);
25120 /* Note that in a template enum, the TREE_VALUE is the
25121 CONST_DECL, not the corresponding INTEGER_CST. */
25122 value = tsubst_expr (DECL_INITIAL (decl),
25123 args, tf_warning_or_error, NULL_TREE,
25124 /*integral_constant_expression_p=*/true);
25125
25126 /* Give this enumeration constant the correct access. */
25127 set_current_access_from_decl (decl);
25128
25129 /* Actually build the enumerator itself. Here we're assuming that
25130 enumerators can't have dependent attributes. */
25131 build_enumerator (DECL_NAME (decl), value, newtag,
25132 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
25133 }
25134
25135 if (SCOPED_ENUM_P (newtag))
25136 finish_scope ();
25137
25138 finish_enum_value_list (newtag);
25139 finish_enum (newtag);
25140
25141 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
25142 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
25143 }
25144
25145 /* DECL is a FUNCTION_DECL that is a template specialization. Return
25146 its type -- but without substituting the innermost set of template
25147 arguments. So, innermost set of template parameters will appear in
25148 the type. */
25149
25150 tree
25151 get_mostly_instantiated_function_type (tree decl)
25152 {
25153 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
25154 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
25155 }
25156
25157 /* Return truthvalue if we're processing a template different from
25158 the last one involved in diagnostics. */
25159 bool
25160 problematic_instantiation_changed (void)
25161 {
25162 return current_tinst_level != last_error_tinst_level;
25163 }
25164
25165 /* Remember current template involved in diagnostics. */
25166 void
25167 record_last_problematic_instantiation (void)
25168 {
25169 set_refcount_ptr (last_error_tinst_level, current_tinst_level);
25170 }
25171
25172 struct tinst_level *
25173 current_instantiation (void)
25174 {
25175 return current_tinst_level;
25176 }
25177
25178 /* Return TRUE if current_function_decl is being instantiated, false
25179 otherwise. */
25180
25181 bool
25182 instantiating_current_function_p (void)
25183 {
25184 return (current_instantiation ()
25185 && (current_instantiation ()->maybe_get_node ()
25186 == current_function_decl));
25187 }
25188
25189 /* [temp.param] Check that template non-type parm TYPE is of an allowable
25190 type. Return false for ok, true for disallowed. Issue error and
25191 inform messages under control of COMPLAIN. */
25192
25193 static bool
25194 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
25195 {
25196 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
25197 return false;
25198 else if (TYPE_PTR_P (type))
25199 return false;
25200 else if (TYPE_REF_P (type)
25201 && !TYPE_REF_IS_RVALUE (type))
25202 return false;
25203 else if (TYPE_PTRMEM_P (type))
25204 return false;
25205 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
25206 return false;
25207 else if (TREE_CODE (type) == TYPENAME_TYPE)
25208 return false;
25209 else if (TREE_CODE (type) == DECLTYPE_TYPE)
25210 return false;
25211 else if (TREE_CODE (type) == NULLPTR_TYPE)
25212 return false;
25213 /* A bound template template parm could later be instantiated to have a valid
25214 nontype parm type via an alias template. */
25215 else if (cxx_dialect >= cxx11
25216 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
25217 return false;
25218 else if (CLASS_TYPE_P (type))
25219 {
25220 if (cxx_dialect < cxx2a)
25221 {
25222 if (complain & tf_error)
25223 error ("non-type template parameters of class type only available "
25224 "with %<-std=c++2a%> or %<-std=gnu++2a%>");
25225 return true;
25226 }
25227 if (dependent_type_p (type))
25228 return false;
25229 if (!complete_type_or_else (type, NULL_TREE))
25230 return true;
25231 if (!literal_type_p (type))
25232 {
25233 error ("%qT is not a valid type for a template non-type parameter "
25234 "because it is not literal", type);
25235 explain_non_literal_class (type);
25236 return true;
25237 }
25238 if (cp_has_mutable_p (type))
25239 {
25240 error ("%qT is not a valid type for a template non-type parameter "
25241 "because it has a mutable member", type);
25242 return true;
25243 }
25244 /* FIXME check op<=> and strong structural equality once spaceship is
25245 implemented. */
25246 return false;
25247 }
25248
25249 if (complain & tf_error)
25250 {
25251 if (type == error_mark_node)
25252 inform (input_location, "invalid template non-type parameter");
25253 else
25254 error ("%q#T is not a valid type for a template non-type parameter",
25255 type);
25256 }
25257 return true;
25258 }
25259
25260 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
25261 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
25262
25263 static bool
25264 dependent_type_p_r (tree type)
25265 {
25266 tree scope;
25267
25268 /* [temp.dep.type]
25269
25270 A type is dependent if it is:
25271
25272 -- a template parameter. Template template parameters are types
25273 for us (since TYPE_P holds true for them) so we handle
25274 them here. */
25275 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
25276 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
25277 return true;
25278 /* -- a qualified-id with a nested-name-specifier which contains a
25279 class-name that names a dependent type or whose unqualified-id
25280 names a dependent type. */
25281 if (TREE_CODE (type) == TYPENAME_TYPE)
25282 return true;
25283
25284 /* An alias template specialization can be dependent even if the
25285 resulting type is not. */
25286 if (dependent_alias_template_spec_p (type))
25287 return true;
25288
25289 /* -- a cv-qualified type where the cv-unqualified type is
25290 dependent.
25291 No code is necessary for this bullet; the code below handles
25292 cv-qualified types, and we don't want to strip aliases with
25293 TYPE_MAIN_VARIANT because of DR 1558. */
25294 /* -- a compound type constructed from any dependent type. */
25295 if (TYPE_PTRMEM_P (type))
25296 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
25297 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
25298 (type)));
25299 else if (INDIRECT_TYPE_P (type))
25300 return dependent_type_p (TREE_TYPE (type));
25301 else if (FUNC_OR_METHOD_TYPE_P (type))
25302 {
25303 tree arg_type;
25304
25305 if (dependent_type_p (TREE_TYPE (type)))
25306 return true;
25307 for (arg_type = TYPE_ARG_TYPES (type);
25308 arg_type;
25309 arg_type = TREE_CHAIN (arg_type))
25310 if (dependent_type_p (TREE_VALUE (arg_type)))
25311 return true;
25312 if (cxx_dialect >= cxx17)
25313 /* A value-dependent noexcept-specifier makes the type dependent. */
25314 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
25315 if (tree noex = TREE_PURPOSE (spec))
25316 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
25317 affect overload resolution and treating it as dependent breaks
25318 things. Same for an unparsed noexcept expression. */
25319 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
25320 && TREE_CODE (noex) != DEFERRED_PARSE
25321 && value_dependent_expression_p (noex))
25322 return true;
25323 return false;
25324 }
25325 /* -- an array type constructed from any dependent type or whose
25326 size is specified by a constant expression that is
25327 value-dependent.
25328
25329 We checked for type- and value-dependence of the bounds in
25330 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
25331 if (TREE_CODE (type) == ARRAY_TYPE)
25332 {
25333 if (TYPE_DOMAIN (type)
25334 && dependent_type_p (TYPE_DOMAIN (type)))
25335 return true;
25336 return dependent_type_p (TREE_TYPE (type));
25337 }
25338
25339 /* -- a template-id in which either the template name is a template
25340 parameter ... */
25341 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
25342 return true;
25343 /* ... or any of the template arguments is a dependent type or
25344 an expression that is type-dependent or value-dependent. */
25345 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
25346 && (any_dependent_template_arguments_p
25347 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
25348 return true;
25349
25350 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
25351 dependent; if the argument of the `typeof' expression is not
25352 type-dependent, then it should already been have resolved. */
25353 if (TREE_CODE (type) == TYPEOF_TYPE
25354 || TREE_CODE (type) == DECLTYPE_TYPE
25355 || TREE_CODE (type) == UNDERLYING_TYPE)
25356 return true;
25357
25358 /* A template argument pack is dependent if any of its packed
25359 arguments are. */
25360 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
25361 {
25362 tree args = ARGUMENT_PACK_ARGS (type);
25363 int i, len = TREE_VEC_LENGTH (args);
25364 for (i = 0; i < len; ++i)
25365 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
25366 return true;
25367 }
25368
25369 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
25370 be template parameters. */
25371 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
25372 return true;
25373
25374 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
25375 return true;
25376
25377 /* The standard does not specifically mention types that are local
25378 to template functions or local classes, but they should be
25379 considered dependent too. For example:
25380
25381 template <int I> void f() {
25382 enum E { a = I };
25383 S<sizeof (E)> s;
25384 }
25385
25386 The size of `E' cannot be known until the value of `I' has been
25387 determined. Therefore, `E' must be considered dependent. */
25388 scope = TYPE_CONTEXT (type);
25389 if (scope && TYPE_P (scope))
25390 return dependent_type_p (scope);
25391 /* Don't use type_dependent_expression_p here, as it can lead
25392 to infinite recursion trying to determine whether a lambda
25393 nested in a lambda is dependent (c++/47687). */
25394 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
25395 && DECL_LANG_SPECIFIC (scope)
25396 && DECL_TEMPLATE_INFO (scope)
25397 && (any_dependent_template_arguments_p
25398 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
25399 return true;
25400
25401 /* Other types are non-dependent. */
25402 return false;
25403 }
25404
25405 /* Returns TRUE if TYPE is dependent, in the sense of
25406 [temp.dep.type]. Note that a NULL type is considered dependent. */
25407
25408 bool
25409 dependent_type_p (tree type)
25410 {
25411 /* If there are no template parameters in scope, then there can't be
25412 any dependent types. */
25413 if (!processing_template_decl)
25414 {
25415 /* If we are not processing a template, then nobody should be
25416 providing us with a dependent type. */
25417 gcc_assert (type);
25418 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
25419 return false;
25420 }
25421
25422 /* If the type is NULL, we have not computed a type for the entity
25423 in question; in that case, the type is dependent. */
25424 if (!type)
25425 return true;
25426
25427 /* Erroneous types can be considered non-dependent. */
25428 if (type == error_mark_node)
25429 return false;
25430
25431 /* Getting here with global_type_node means we improperly called this
25432 function on the TREE_TYPE of an IDENTIFIER_NODE. */
25433 gcc_checking_assert (type != global_type_node);
25434
25435 /* If we have not already computed the appropriate value for TYPE,
25436 do so now. */
25437 if (!TYPE_DEPENDENT_P_VALID (type))
25438 {
25439 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
25440 TYPE_DEPENDENT_P_VALID (type) = 1;
25441 }
25442
25443 return TYPE_DEPENDENT_P (type);
25444 }
25445
25446 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
25447 lookup. In other words, a dependent type that is not the current
25448 instantiation. */
25449
25450 bool
25451 dependent_scope_p (tree scope)
25452 {
25453 return (scope && TYPE_P (scope) && dependent_type_p (scope)
25454 && !currently_open_class (scope));
25455 }
25456
25457 /* T is a SCOPE_REF. Return whether it represents a non-static member of
25458 an unknown base of 'this' (and is therefore instantiation-dependent). */
25459
25460 static bool
25461 unknown_base_ref_p (tree t)
25462 {
25463 if (!current_class_ptr)
25464 return false;
25465
25466 tree mem = TREE_OPERAND (t, 1);
25467 if (shared_member_p (mem))
25468 return false;
25469
25470 tree cur = current_nonlambda_class_type ();
25471 if (!any_dependent_bases_p (cur))
25472 return false;
25473
25474 tree ctx = TREE_OPERAND (t, 0);
25475 if (DERIVED_FROM_P (ctx, cur))
25476 return false;
25477
25478 return true;
25479 }
25480
25481 /* T is a SCOPE_REF; return whether we need to consider it
25482 instantiation-dependent so that we can check access at instantiation
25483 time even though we know which member it resolves to. */
25484
25485 static bool
25486 instantiation_dependent_scope_ref_p (tree t)
25487 {
25488 if (DECL_P (TREE_OPERAND (t, 1))
25489 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
25490 && !unknown_base_ref_p (t)
25491 && accessible_in_template_p (TREE_OPERAND (t, 0),
25492 TREE_OPERAND (t, 1)))
25493 return false;
25494 else
25495 return true;
25496 }
25497
25498 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
25499 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
25500 expression. */
25501
25502 /* Note that this predicate is not appropriate for general expressions;
25503 only constant expressions (that satisfy potential_constant_expression)
25504 can be tested for value dependence. */
25505
25506 bool
25507 value_dependent_expression_p (tree expression)
25508 {
25509 if (!processing_template_decl || expression == NULL_TREE)
25510 return false;
25511
25512 /* A type-dependent expression is also value-dependent. */
25513 if (type_dependent_expression_p (expression))
25514 return true;
25515
25516 switch (TREE_CODE (expression))
25517 {
25518 case BASELINK:
25519 /* A dependent member function of the current instantiation. */
25520 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
25521
25522 case FUNCTION_DECL:
25523 /* A dependent member function of the current instantiation. */
25524 if (DECL_CLASS_SCOPE_P (expression)
25525 && dependent_type_p (DECL_CONTEXT (expression)))
25526 return true;
25527 break;
25528
25529 case IDENTIFIER_NODE:
25530 /* A name that has not been looked up -- must be dependent. */
25531 return true;
25532
25533 case TEMPLATE_PARM_INDEX:
25534 /* A non-type template parm. */
25535 return true;
25536
25537 case CONST_DECL:
25538 /* A non-type template parm. */
25539 if (DECL_TEMPLATE_PARM_P (expression))
25540 return true;
25541 return value_dependent_expression_p (DECL_INITIAL (expression));
25542
25543 case VAR_DECL:
25544 /* A constant with literal type and is initialized
25545 with an expression that is value-dependent. */
25546 if (DECL_DEPENDENT_INIT_P (expression)
25547 /* FIXME cp_finish_decl doesn't fold reference initializers. */
25548 || TYPE_REF_P (TREE_TYPE (expression)))
25549 return true;
25550 if (DECL_HAS_VALUE_EXPR_P (expression))
25551 {
25552 tree value_expr = DECL_VALUE_EXPR (expression);
25553 if (value_dependent_expression_p (value_expr)
25554 /* __PRETTY_FUNCTION__ inside a template function is dependent
25555 on the name of the function. */
25556 || (DECL_PRETTY_FUNCTION_P (expression)
25557 /* It might be used in a template, but not a template
25558 function, in which case its DECL_VALUE_EXPR will be
25559 "top level". */
25560 && value_expr == error_mark_node))
25561 return true;
25562 }
25563 return false;
25564
25565 case DYNAMIC_CAST_EXPR:
25566 case STATIC_CAST_EXPR:
25567 case CONST_CAST_EXPR:
25568 case REINTERPRET_CAST_EXPR:
25569 case CAST_EXPR:
25570 case IMPLICIT_CONV_EXPR:
25571 /* These expressions are value-dependent if the type to which
25572 the cast occurs is dependent or the expression being casted
25573 is value-dependent. */
25574 {
25575 tree type = TREE_TYPE (expression);
25576
25577 if (dependent_type_p (type))
25578 return true;
25579
25580 /* A functional cast has a list of operands. */
25581 expression = TREE_OPERAND (expression, 0);
25582 if (!expression)
25583 {
25584 /* If there are no operands, it must be an expression such
25585 as "int()". This should not happen for aggregate types
25586 because it would form non-constant expressions. */
25587 gcc_assert (cxx_dialect >= cxx11
25588 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
25589
25590 return false;
25591 }
25592
25593 if (TREE_CODE (expression) == TREE_LIST)
25594 return any_value_dependent_elements_p (expression);
25595
25596 return value_dependent_expression_p (expression);
25597 }
25598
25599 case SIZEOF_EXPR:
25600 if (SIZEOF_EXPR_TYPE_P (expression))
25601 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
25602 /* FALLTHRU */
25603 case ALIGNOF_EXPR:
25604 case TYPEID_EXPR:
25605 /* A `sizeof' expression is value-dependent if the operand is
25606 type-dependent or is a pack expansion. */
25607 expression = TREE_OPERAND (expression, 0);
25608 if (PACK_EXPANSION_P (expression))
25609 return true;
25610 else if (TYPE_P (expression))
25611 return dependent_type_p (expression);
25612 return instantiation_dependent_uneval_expression_p (expression);
25613
25614 case AT_ENCODE_EXPR:
25615 /* An 'encode' expression is value-dependent if the operand is
25616 type-dependent. */
25617 expression = TREE_OPERAND (expression, 0);
25618 return dependent_type_p (expression);
25619
25620 case NOEXCEPT_EXPR:
25621 expression = TREE_OPERAND (expression, 0);
25622 return instantiation_dependent_uneval_expression_p (expression);
25623
25624 case SCOPE_REF:
25625 /* All instantiation-dependent expressions should also be considered
25626 value-dependent. */
25627 return instantiation_dependent_scope_ref_p (expression);
25628
25629 case COMPONENT_REF:
25630 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
25631 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
25632
25633 case NONTYPE_ARGUMENT_PACK:
25634 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
25635 is value-dependent. */
25636 {
25637 tree values = ARGUMENT_PACK_ARGS (expression);
25638 int i, len = TREE_VEC_LENGTH (values);
25639
25640 for (i = 0; i < len; ++i)
25641 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
25642 return true;
25643
25644 return false;
25645 }
25646
25647 case TRAIT_EXPR:
25648 {
25649 tree type2 = TRAIT_EXPR_TYPE2 (expression);
25650
25651 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
25652 return true;
25653
25654 if (!type2)
25655 return false;
25656
25657 if (TREE_CODE (type2) != TREE_LIST)
25658 return dependent_type_p (type2);
25659
25660 for (; type2; type2 = TREE_CHAIN (type2))
25661 if (dependent_type_p (TREE_VALUE (type2)))
25662 return true;
25663
25664 return false;
25665 }
25666
25667 case MODOP_EXPR:
25668 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
25669 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
25670
25671 case ARRAY_REF:
25672 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
25673 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
25674
25675 case ADDR_EXPR:
25676 {
25677 tree op = TREE_OPERAND (expression, 0);
25678 return (value_dependent_expression_p (op)
25679 || has_value_dependent_address (op));
25680 }
25681
25682 case REQUIRES_EXPR:
25683 /* Treat all requires-expressions as value-dependent so
25684 we don't try to fold them. */
25685 return true;
25686
25687 case TYPE_REQ:
25688 return dependent_type_p (TREE_OPERAND (expression, 0));
25689
25690 case CALL_EXPR:
25691 {
25692 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
25693 return true;
25694 tree fn = get_callee_fndecl (expression);
25695 int i, nargs;
25696 nargs = call_expr_nargs (expression);
25697 for (i = 0; i < nargs; ++i)
25698 {
25699 tree op = CALL_EXPR_ARG (expression, i);
25700 /* In a call to a constexpr member function, look through the
25701 implicit ADDR_EXPR on the object argument so that it doesn't
25702 cause the call to be considered value-dependent. We also
25703 look through it in potential_constant_expression. */
25704 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
25705 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
25706 && TREE_CODE (op) == ADDR_EXPR)
25707 op = TREE_OPERAND (op, 0);
25708 if (value_dependent_expression_p (op))
25709 return true;
25710 }
25711 return false;
25712 }
25713
25714 case TEMPLATE_ID_EXPR:
25715 return variable_concept_p (TREE_OPERAND (expression, 0));
25716
25717 case CONSTRUCTOR:
25718 {
25719 unsigned ix;
25720 tree val;
25721 if (dependent_type_p (TREE_TYPE (expression)))
25722 return true;
25723 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
25724 if (value_dependent_expression_p (val))
25725 return true;
25726 return false;
25727 }
25728
25729 case STMT_EXPR:
25730 /* Treat a GNU statement expression as dependent to avoid crashing
25731 under instantiate_non_dependent_expr; it can't be constant. */
25732 return true;
25733
25734 default:
25735 /* A constant expression is value-dependent if any subexpression is
25736 value-dependent. */
25737 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
25738 {
25739 case tcc_reference:
25740 case tcc_unary:
25741 case tcc_comparison:
25742 case tcc_binary:
25743 case tcc_expression:
25744 case tcc_vl_exp:
25745 {
25746 int i, len = cp_tree_operand_length (expression);
25747
25748 for (i = 0; i < len; i++)
25749 {
25750 tree t = TREE_OPERAND (expression, i);
25751
25752 /* In some cases, some of the operands may be missing.
25753 (For example, in the case of PREDECREMENT_EXPR, the
25754 amount to increment by may be missing.) That doesn't
25755 make the expression dependent. */
25756 if (t && value_dependent_expression_p (t))
25757 return true;
25758 }
25759 }
25760 break;
25761 default:
25762 break;
25763 }
25764 break;
25765 }
25766
25767 /* The expression is not value-dependent. */
25768 return false;
25769 }
25770
25771 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
25772 [temp.dep.expr]. Note that an expression with no type is
25773 considered dependent. Other parts of the compiler arrange for an
25774 expression with type-dependent subexpressions to have no type, so
25775 this function doesn't have to be fully recursive. */
25776
25777 bool
25778 type_dependent_expression_p (tree expression)
25779 {
25780 if (!processing_template_decl)
25781 return false;
25782
25783 if (expression == NULL_TREE || expression == error_mark_node)
25784 return false;
25785
25786 STRIP_ANY_LOCATION_WRAPPER (expression);
25787
25788 /* An unresolved name is always dependent. */
25789 if (identifier_p (expression)
25790 || TREE_CODE (expression) == USING_DECL
25791 || TREE_CODE (expression) == WILDCARD_DECL)
25792 return true;
25793
25794 /* A lambda-expression in template context is dependent. dependent_type_p is
25795 true for a lambda in the scope of a class or function template, but that
25796 doesn't cover all template contexts, like a default template argument. */
25797 if (TREE_CODE (expression) == LAMBDA_EXPR)
25798 return true;
25799
25800 /* A fold expression is type-dependent. */
25801 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
25802 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
25803 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
25804 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
25805 return true;
25806
25807 /* Some expression forms are never type-dependent. */
25808 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
25809 || TREE_CODE (expression) == SIZEOF_EXPR
25810 || TREE_CODE (expression) == ALIGNOF_EXPR
25811 || TREE_CODE (expression) == AT_ENCODE_EXPR
25812 || TREE_CODE (expression) == NOEXCEPT_EXPR
25813 || TREE_CODE (expression) == TRAIT_EXPR
25814 || TREE_CODE (expression) == TYPEID_EXPR
25815 || TREE_CODE (expression) == DELETE_EXPR
25816 || TREE_CODE (expression) == VEC_DELETE_EXPR
25817 || TREE_CODE (expression) == THROW_EXPR
25818 || TREE_CODE (expression) == REQUIRES_EXPR)
25819 return false;
25820
25821 /* The types of these expressions depends only on the type to which
25822 the cast occurs. */
25823 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
25824 || TREE_CODE (expression) == STATIC_CAST_EXPR
25825 || TREE_CODE (expression) == CONST_CAST_EXPR
25826 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
25827 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
25828 || TREE_CODE (expression) == CAST_EXPR)
25829 return dependent_type_p (TREE_TYPE (expression));
25830
25831 /* The types of these expressions depends only on the type created
25832 by the expression. */
25833 if (TREE_CODE (expression) == NEW_EXPR
25834 || TREE_CODE (expression) == VEC_NEW_EXPR)
25835 {
25836 /* For NEW_EXPR tree nodes created inside a template, either
25837 the object type itself or a TREE_LIST may appear as the
25838 operand 1. */
25839 tree type = TREE_OPERAND (expression, 1);
25840 if (TREE_CODE (type) == TREE_LIST)
25841 /* This is an array type. We need to check array dimensions
25842 as well. */
25843 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
25844 || value_dependent_expression_p
25845 (TREE_OPERAND (TREE_VALUE (type), 1));
25846 else
25847 return dependent_type_p (type);
25848 }
25849
25850 if (TREE_CODE (expression) == SCOPE_REF)
25851 {
25852 tree scope = TREE_OPERAND (expression, 0);
25853 tree name = TREE_OPERAND (expression, 1);
25854
25855 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
25856 contains an identifier associated by name lookup with one or more
25857 declarations declared with a dependent type, or...a
25858 nested-name-specifier or qualified-id that names a member of an
25859 unknown specialization. */
25860 return (type_dependent_expression_p (name)
25861 || dependent_scope_p (scope));
25862 }
25863
25864 if (TREE_CODE (expression) == TEMPLATE_DECL
25865 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
25866 return uses_outer_template_parms (expression);
25867
25868 if (TREE_CODE (expression) == STMT_EXPR)
25869 expression = stmt_expr_value_expr (expression);
25870
25871 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
25872 {
25873 tree elt;
25874 unsigned i;
25875
25876 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
25877 {
25878 if (type_dependent_expression_p (elt))
25879 return true;
25880 }
25881 return false;
25882 }
25883
25884 /* A static data member of the current instantiation with incomplete
25885 array type is type-dependent, as the definition and specializations
25886 can have different bounds. */
25887 if (VAR_P (expression)
25888 && DECL_CLASS_SCOPE_P (expression)
25889 && dependent_type_p (DECL_CONTEXT (expression))
25890 && VAR_HAD_UNKNOWN_BOUND (expression))
25891 return true;
25892
25893 /* An array of unknown bound depending on a variadic parameter, eg:
25894
25895 template<typename... Args>
25896 void foo (Args... args)
25897 {
25898 int arr[] = { args... };
25899 }
25900
25901 template<int... vals>
25902 void bar ()
25903 {
25904 int arr[] = { vals... };
25905 }
25906
25907 If the array has no length and has an initializer, it must be that
25908 we couldn't determine its length in cp_complete_array_type because
25909 it is dependent. */
25910 if (VAR_P (expression)
25911 && TREE_TYPE (expression) != NULL_TREE
25912 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
25913 && !TYPE_DOMAIN (TREE_TYPE (expression))
25914 && DECL_INITIAL (expression))
25915 return true;
25916
25917 /* A function or variable template-id is type-dependent if it has any
25918 dependent template arguments. */
25919 if (VAR_OR_FUNCTION_DECL_P (expression)
25920 && DECL_LANG_SPECIFIC (expression)
25921 && DECL_TEMPLATE_INFO (expression))
25922 {
25923 /* Consider the innermost template arguments, since those are the ones
25924 that come from the template-id; the template arguments for the
25925 enclosing class do not make it type-dependent unless they are used in
25926 the type of the decl. */
25927 if (instantiates_primary_template_p (expression)
25928 && (any_dependent_template_arguments_p
25929 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
25930 return true;
25931 }
25932
25933 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
25934 type-dependent. Checking this is important for functions with auto return
25935 type, which looks like a dependent type. */
25936 if (TREE_CODE (expression) == FUNCTION_DECL
25937 && !(DECL_CLASS_SCOPE_P (expression)
25938 && dependent_type_p (DECL_CONTEXT (expression)))
25939 && !(DECL_LANG_SPECIFIC (expression)
25940 && DECL_FRIEND_P (expression)
25941 && (!DECL_FRIEND_CONTEXT (expression)
25942 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
25943 && !DECL_LOCAL_FUNCTION_P (expression))
25944 {
25945 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
25946 || undeduced_auto_decl (expression));
25947 return false;
25948 }
25949
25950 /* Always dependent, on the number of arguments if nothing else. */
25951 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
25952 return true;
25953
25954 if (TREE_TYPE (expression) == unknown_type_node)
25955 {
25956 if (TREE_CODE (expression) == ADDR_EXPR)
25957 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
25958 if (TREE_CODE (expression) == COMPONENT_REF
25959 || TREE_CODE (expression) == OFFSET_REF)
25960 {
25961 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
25962 return true;
25963 expression = TREE_OPERAND (expression, 1);
25964 if (identifier_p (expression))
25965 return false;
25966 }
25967 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
25968 if (TREE_CODE (expression) == SCOPE_REF)
25969 return false;
25970
25971 if (BASELINK_P (expression))
25972 {
25973 if (BASELINK_OPTYPE (expression)
25974 && dependent_type_p (BASELINK_OPTYPE (expression)))
25975 return true;
25976 expression = BASELINK_FUNCTIONS (expression);
25977 }
25978
25979 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
25980 {
25981 if (any_dependent_template_arguments_p
25982 (TREE_OPERAND (expression, 1)))
25983 return true;
25984 expression = TREE_OPERAND (expression, 0);
25985 if (identifier_p (expression))
25986 return true;
25987 }
25988
25989 gcc_assert (OVL_P (expression));
25990
25991 for (lkp_iterator iter (expression); iter; ++iter)
25992 if (type_dependent_expression_p (*iter))
25993 return true;
25994
25995 return false;
25996 }
25997
25998 /* The type of a non-type template parm declared with a placeholder type
25999 depends on the corresponding template argument, even though
26000 placeholders are not normally considered dependent. */
26001 if (TREE_CODE (expression) == TEMPLATE_PARM_INDEX
26002 && is_auto (TREE_TYPE (expression)))
26003 return true;
26004
26005 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
26006
26007 /* Dependent type attributes might not have made it from the decl to
26008 the type yet. */
26009 if (DECL_P (expression)
26010 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
26011 return true;
26012
26013 return (dependent_type_p (TREE_TYPE (expression)));
26014 }
26015
26016 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
26017 type-dependent if the expression refers to a member of the current
26018 instantiation and the type of the referenced member is dependent, or the
26019 class member access expression refers to a member of an unknown
26020 specialization.
26021
26022 This function returns true if the OBJECT in such a class member access
26023 expression is of an unknown specialization. */
26024
26025 bool
26026 type_dependent_object_expression_p (tree object)
26027 {
26028 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
26029 dependent. */
26030 if (TREE_CODE (object) == IDENTIFIER_NODE)
26031 return true;
26032 tree scope = TREE_TYPE (object);
26033 return (!scope || dependent_scope_p (scope));
26034 }
26035
26036 /* walk_tree callback function for instantiation_dependent_expression_p,
26037 below. Returns non-zero if a dependent subexpression is found. */
26038
26039 static tree
26040 instantiation_dependent_r (tree *tp, int *walk_subtrees,
26041 void * /*data*/)
26042 {
26043 if (TYPE_P (*tp))
26044 {
26045 /* We don't have to worry about decltype currently because decltype
26046 of an instantiation-dependent expr is a dependent type. This
26047 might change depending on the resolution of DR 1172. */
26048 *walk_subtrees = false;
26049 return NULL_TREE;
26050 }
26051 enum tree_code code = TREE_CODE (*tp);
26052 switch (code)
26053 {
26054 /* Don't treat an argument list as dependent just because it has no
26055 TREE_TYPE. */
26056 case TREE_LIST:
26057 case TREE_VEC:
26058 case NONTYPE_ARGUMENT_PACK:
26059 return NULL_TREE;
26060
26061 case TEMPLATE_PARM_INDEX:
26062 if (dependent_type_p (TREE_TYPE (*tp)))
26063 return *tp;
26064 if (TEMPLATE_PARM_PARAMETER_PACK (*tp))
26065 return *tp;
26066 /* We'll check value-dependence separately. */
26067 return NULL_TREE;
26068
26069 /* Handle expressions with type operands. */
26070 case SIZEOF_EXPR:
26071 case ALIGNOF_EXPR:
26072 case TYPEID_EXPR:
26073 case AT_ENCODE_EXPR:
26074 {
26075 tree op = TREE_OPERAND (*tp, 0);
26076 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
26077 op = TREE_TYPE (op);
26078 if (TYPE_P (op))
26079 {
26080 if (dependent_type_p (op))
26081 return *tp;
26082 else
26083 {
26084 *walk_subtrees = false;
26085 return NULL_TREE;
26086 }
26087 }
26088 break;
26089 }
26090
26091 case COMPONENT_REF:
26092 if (identifier_p (TREE_OPERAND (*tp, 1)))
26093 /* In a template, finish_class_member_access_expr creates a
26094 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
26095 type-dependent, so that we can check access control at
26096 instantiation time (PR 42277). See also Core issue 1273. */
26097 return *tp;
26098 break;
26099
26100 case SCOPE_REF:
26101 if (instantiation_dependent_scope_ref_p (*tp))
26102 return *tp;
26103 else
26104 break;
26105
26106 /* Treat statement-expressions as dependent. */
26107 case BIND_EXPR:
26108 return *tp;
26109
26110 /* Treat requires-expressions as dependent. */
26111 case REQUIRES_EXPR:
26112 return *tp;
26113
26114 case CALL_EXPR:
26115 /* Treat calls to function concepts as dependent. */
26116 if (function_concept_check_p (*tp))
26117 return *tp;
26118 break;
26119
26120 case TEMPLATE_ID_EXPR:
26121 /* And variable concepts. */
26122 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
26123 return *tp;
26124 break;
26125
26126 case CONSTRUCTOR:
26127 if (CONSTRUCTOR_IS_DEPENDENT (*tp))
26128 return *tp;
26129 break;
26130
26131 default:
26132 break;
26133 }
26134
26135 if (type_dependent_expression_p (*tp))
26136 return *tp;
26137 else
26138 return NULL_TREE;
26139 }
26140
26141 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
26142 sense defined by the ABI:
26143
26144 "An expression is instantiation-dependent if it is type-dependent
26145 or value-dependent, or it has a subexpression that is type-dependent
26146 or value-dependent."
26147
26148 Except don't actually check value-dependence for unevaluated expressions,
26149 because in sizeof(i) we don't care about the value of i. Checking
26150 type-dependence will in turn check value-dependence of array bounds/template
26151 arguments as needed. */
26152
26153 bool
26154 instantiation_dependent_uneval_expression_p (tree expression)
26155 {
26156 tree result;
26157
26158 if (!processing_template_decl)
26159 return false;
26160
26161 if (expression == error_mark_node)
26162 return false;
26163
26164 result = cp_walk_tree_without_duplicates (&expression,
26165 instantiation_dependent_r, NULL);
26166 return result != NULL_TREE;
26167 }
26168
26169 /* As above, but also check value-dependence of the expression as a whole. */
26170
26171 bool
26172 instantiation_dependent_expression_p (tree expression)
26173 {
26174 return (instantiation_dependent_uneval_expression_p (expression)
26175 || value_dependent_expression_p (expression));
26176 }
26177
26178 /* Like type_dependent_expression_p, but it also works while not processing
26179 a template definition, i.e. during substitution or mangling. */
26180
26181 bool
26182 type_dependent_expression_p_push (tree expr)
26183 {
26184 bool b;
26185 ++processing_template_decl;
26186 b = type_dependent_expression_p (expr);
26187 --processing_template_decl;
26188 return b;
26189 }
26190
26191 /* Returns TRUE if ARGS contains a type-dependent expression. */
26192
26193 bool
26194 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
26195 {
26196 unsigned int i;
26197 tree arg;
26198
26199 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
26200 {
26201 if (type_dependent_expression_p (arg))
26202 return true;
26203 }
26204 return false;
26205 }
26206
26207 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
26208 expressions) contains any type-dependent expressions. */
26209
26210 bool
26211 any_type_dependent_elements_p (const_tree list)
26212 {
26213 for (; list; list = TREE_CHAIN (list))
26214 if (type_dependent_expression_p (TREE_VALUE (list)))
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 value-dependent expressions. */
26222
26223 bool
26224 any_value_dependent_elements_p (const_tree list)
26225 {
26226 for (; list; list = TREE_CHAIN (list))
26227 if (value_dependent_expression_p (TREE_VALUE (list)))
26228 return true;
26229
26230 return false;
26231 }
26232
26233 /* Returns TRUE if the ARG (a template argument) is dependent. */
26234
26235 bool
26236 dependent_template_arg_p (tree arg)
26237 {
26238 if (!processing_template_decl)
26239 return false;
26240
26241 /* Assume a template argument that was wrongly written by the user
26242 is dependent. This is consistent with what
26243 any_dependent_template_arguments_p [that calls this function]
26244 does. */
26245 if (!arg || arg == error_mark_node)
26246 return true;
26247
26248 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
26249 arg = argument_pack_select_arg (arg);
26250
26251 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
26252 return true;
26253 if (TREE_CODE (arg) == TEMPLATE_DECL)
26254 {
26255 if (DECL_TEMPLATE_PARM_P (arg))
26256 return true;
26257 /* A member template of a dependent class is not necessarily
26258 type-dependent, but it is a dependent template argument because it
26259 will be a member of an unknown specialization to that template. */
26260 tree scope = CP_DECL_CONTEXT (arg);
26261 return TYPE_P (scope) && dependent_type_p (scope);
26262 }
26263 else if (ARGUMENT_PACK_P (arg))
26264 {
26265 tree args = ARGUMENT_PACK_ARGS (arg);
26266 int i, len = TREE_VEC_LENGTH (args);
26267 for (i = 0; i < len; ++i)
26268 {
26269 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
26270 return true;
26271 }
26272
26273 return false;
26274 }
26275 else if (TYPE_P (arg))
26276 return dependent_type_p (arg);
26277 else
26278 return (type_dependent_expression_p (arg)
26279 || value_dependent_expression_p (arg));
26280 }
26281
26282 /* Returns true if ARGS (a collection of template arguments) contains
26283 any types that require structural equality testing. */
26284
26285 bool
26286 any_template_arguments_need_structural_equality_p (tree args)
26287 {
26288 int i;
26289 int j;
26290
26291 if (!args)
26292 return false;
26293 if (args == error_mark_node)
26294 return true;
26295
26296 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
26297 {
26298 tree level = TMPL_ARGS_LEVEL (args, i + 1);
26299 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
26300 {
26301 tree arg = TREE_VEC_ELT (level, j);
26302 tree packed_args = NULL_TREE;
26303 int k, len = 1;
26304
26305 if (ARGUMENT_PACK_P (arg))
26306 {
26307 /* Look inside the argument pack. */
26308 packed_args = ARGUMENT_PACK_ARGS (arg);
26309 len = TREE_VEC_LENGTH (packed_args);
26310 }
26311
26312 for (k = 0; k < len; ++k)
26313 {
26314 if (packed_args)
26315 arg = TREE_VEC_ELT (packed_args, k);
26316
26317 if (error_operand_p (arg))
26318 return true;
26319 else if (TREE_CODE (arg) == TEMPLATE_DECL)
26320 continue;
26321 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
26322 return true;
26323 else if (!TYPE_P (arg) && TREE_TYPE (arg)
26324 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
26325 return true;
26326 }
26327 }
26328 }
26329
26330 return false;
26331 }
26332
26333 /* Returns true if ARGS (a collection of template arguments) contains
26334 any dependent arguments. */
26335
26336 bool
26337 any_dependent_template_arguments_p (const_tree args)
26338 {
26339 int i;
26340 int j;
26341
26342 if (!args)
26343 return false;
26344 if (args == error_mark_node)
26345 return true;
26346
26347 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
26348 {
26349 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
26350 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
26351 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
26352 return true;
26353 }
26354
26355 return false;
26356 }
26357
26358 /* Returns true if ARGS contains any errors. */
26359
26360 bool
26361 any_erroneous_template_args_p (const_tree args)
26362 {
26363 int i;
26364 int j;
26365
26366 if (args == error_mark_node)
26367 return true;
26368
26369 if (args && TREE_CODE (args) != TREE_VEC)
26370 {
26371 if (tree ti = get_template_info (args))
26372 args = TI_ARGS (ti);
26373 else
26374 args = NULL_TREE;
26375 }
26376
26377 if (!args)
26378 return false;
26379
26380 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
26381 {
26382 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
26383 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
26384 if (error_operand_p (TREE_VEC_ELT (level, j)))
26385 return true;
26386 }
26387
26388 return false;
26389 }
26390
26391 /* Returns TRUE if the template TMPL is type-dependent. */
26392
26393 bool
26394 dependent_template_p (tree tmpl)
26395 {
26396 if (TREE_CODE (tmpl) == OVERLOAD)
26397 {
26398 for (lkp_iterator iter (tmpl); iter; ++iter)
26399 if (dependent_template_p (*iter))
26400 return true;
26401 return false;
26402 }
26403
26404 /* Template template parameters are dependent. */
26405 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
26406 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
26407 return true;
26408 /* So are names that have not been looked up. */
26409 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
26410 return true;
26411 return false;
26412 }
26413
26414 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
26415
26416 bool
26417 dependent_template_id_p (tree tmpl, tree args)
26418 {
26419 return (dependent_template_p (tmpl)
26420 || any_dependent_template_arguments_p (args));
26421 }
26422
26423 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
26424 are dependent. */
26425
26426 bool
26427 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
26428 {
26429 int i;
26430
26431 if (!processing_template_decl)
26432 return false;
26433
26434 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
26435 {
26436 tree decl = TREE_VEC_ELT (declv, i);
26437 tree init = TREE_VEC_ELT (initv, i);
26438 tree cond = TREE_VEC_ELT (condv, i);
26439 tree incr = TREE_VEC_ELT (incrv, i);
26440
26441 if (type_dependent_expression_p (decl)
26442 || TREE_CODE (decl) == SCOPE_REF)
26443 return true;
26444
26445 if (init && type_dependent_expression_p (init))
26446 return true;
26447
26448 if (cond == global_namespace)
26449 return true;
26450
26451 if (type_dependent_expression_p (cond))
26452 return true;
26453
26454 if (COMPARISON_CLASS_P (cond)
26455 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
26456 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
26457 return true;
26458
26459 if (TREE_CODE (incr) == MODOP_EXPR)
26460 {
26461 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
26462 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
26463 return true;
26464 }
26465 else if (type_dependent_expression_p (incr))
26466 return true;
26467 else if (TREE_CODE (incr) == MODIFY_EXPR)
26468 {
26469 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
26470 return true;
26471 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
26472 {
26473 tree t = TREE_OPERAND (incr, 1);
26474 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
26475 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
26476 return true;
26477
26478 /* If this loop has a class iterator with != comparison
26479 with increment other than i++/++i/i--/--i, make sure the
26480 increment is constant. */
26481 if (CLASS_TYPE_P (TREE_TYPE (decl))
26482 && TREE_CODE (cond) == NE_EXPR)
26483 {
26484 if (TREE_OPERAND (t, 0) == decl)
26485 t = TREE_OPERAND (t, 1);
26486 else
26487 t = TREE_OPERAND (t, 0);
26488 if (TREE_CODE (t) != INTEGER_CST)
26489 return true;
26490 }
26491 }
26492 }
26493 }
26494
26495 return false;
26496 }
26497
26498 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
26499 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
26500 no such TYPE can be found. Note that this function peers inside
26501 uninstantiated templates and therefore should be used only in
26502 extremely limited situations. ONLY_CURRENT_P restricts this
26503 peering to the currently open classes hierarchy (which is required
26504 when comparing types). */
26505
26506 tree
26507 resolve_typename_type (tree type, bool only_current_p)
26508 {
26509 tree scope;
26510 tree name;
26511 tree decl;
26512 int quals;
26513 tree pushed_scope;
26514 tree result;
26515
26516 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
26517
26518 scope = TYPE_CONTEXT (type);
26519 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
26520 gcc_checking_assert (uses_template_parms (scope));
26521
26522 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
26523 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
26524 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
26525 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
26526 identifier of the TYPENAME_TYPE anymore.
26527 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
26528 TYPENAME_TYPE instead, we avoid messing up with a possible
26529 typedef variant case. */
26530 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
26531
26532 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
26533 it first before we can figure out what NAME refers to. */
26534 if (TREE_CODE (scope) == TYPENAME_TYPE)
26535 {
26536 if (TYPENAME_IS_RESOLVING_P (scope))
26537 /* Given a class template A with a dependent base with nested type C,
26538 typedef typename A::C::C C will land us here, as trying to resolve
26539 the initial A::C leads to the local C typedef, which leads back to
26540 A::C::C. So we break the recursion now. */
26541 return type;
26542 else
26543 scope = resolve_typename_type (scope, only_current_p);
26544 }
26545 /* If we don't know what SCOPE refers to, then we cannot resolve the
26546 TYPENAME_TYPE. */
26547 if (!CLASS_TYPE_P (scope))
26548 return type;
26549 /* If this is a typedef, we don't want to look inside (c++/11987). */
26550 if (typedef_variant_p (type))
26551 return type;
26552 /* If SCOPE isn't the template itself, it will not have a valid
26553 TYPE_FIELDS list. */
26554 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
26555 /* scope is either the template itself or a compatible instantiation
26556 like X<T>, so look up the name in the original template. */
26557 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
26558 /* If scope has no fields, it can't be a current instantiation. Check this
26559 before currently_open_class to avoid infinite recursion (71515). */
26560 if (!TYPE_FIELDS (scope))
26561 return type;
26562 /* If the SCOPE is not the current instantiation, there's no reason
26563 to look inside it. */
26564 if (only_current_p && !currently_open_class (scope))
26565 return type;
26566 /* Enter the SCOPE so that name lookup will be resolved as if we
26567 were in the class definition. In particular, SCOPE will no
26568 longer be considered a dependent type. */
26569 pushed_scope = push_scope (scope);
26570 /* Look up the declaration. */
26571 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
26572 tf_warning_or_error);
26573
26574 result = NULL_TREE;
26575
26576 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
26577 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
26578 tree fullname = TYPENAME_TYPE_FULLNAME (type);
26579 if (!decl)
26580 /*nop*/;
26581 else if (identifier_p (fullname)
26582 && TREE_CODE (decl) == TYPE_DECL)
26583 {
26584 result = TREE_TYPE (decl);
26585 if (result == error_mark_node)
26586 result = NULL_TREE;
26587 }
26588 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
26589 && DECL_CLASS_TEMPLATE_P (decl))
26590 {
26591 /* Obtain the template and the arguments. */
26592 tree tmpl = TREE_OPERAND (fullname, 0);
26593 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
26594 {
26595 /* We get here with a plain identifier because a previous tentative
26596 parse of the nested-name-specifier as part of a ptr-operator saw
26597 ::template X<A>. The use of ::template is necessary in a
26598 ptr-operator, but wrong in a declarator-id.
26599
26600 [temp.names]: In a qualified-id of a declarator-id, the keyword
26601 template shall not appear at the top level. */
26602 pedwarn (cp_expr_loc_or_input_loc (fullname), OPT_Wpedantic,
26603 "keyword %<template%> not allowed in declarator-id");
26604 tmpl = decl;
26605 }
26606 tree args = TREE_OPERAND (fullname, 1);
26607 /* Instantiate the template. */
26608 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
26609 /*entering_scope=*/true,
26610 tf_error | tf_user);
26611 if (result == error_mark_node)
26612 result = NULL_TREE;
26613 }
26614
26615 /* Leave the SCOPE. */
26616 if (pushed_scope)
26617 pop_scope (pushed_scope);
26618
26619 /* If we failed to resolve it, return the original typename. */
26620 if (!result)
26621 return type;
26622
26623 /* If lookup found a typename type, resolve that too. */
26624 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
26625 {
26626 /* Ill-formed programs can cause infinite recursion here, so we
26627 must catch that. */
26628 TYPENAME_IS_RESOLVING_P (result) = 1;
26629 result = resolve_typename_type (result, only_current_p);
26630 TYPENAME_IS_RESOLVING_P (result) = 0;
26631 }
26632
26633 /* Qualify the resulting type. */
26634 quals = cp_type_quals (type);
26635 if (quals)
26636 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
26637
26638 return result;
26639 }
26640
26641 /* EXPR is an expression which is not type-dependent. Return a proxy
26642 for EXPR that can be used to compute the types of larger
26643 expressions containing EXPR. */
26644
26645 tree
26646 build_non_dependent_expr (tree expr)
26647 {
26648 tree orig_expr = expr;
26649 tree inner_expr;
26650
26651 /* When checking, try to get a constant value for all non-dependent
26652 expressions in order to expose bugs in *_dependent_expression_p
26653 and constexpr. This can affect code generation, see PR70704, so
26654 only do this for -fchecking=2. */
26655 if (flag_checking > 1
26656 && cxx_dialect >= cxx11
26657 /* Don't do this during nsdmi parsing as it can lead to
26658 unexpected recursive instantiations. */
26659 && !parsing_nsdmi ()
26660 /* Don't do this during concept expansion either and for
26661 the same reason. */
26662 && !expanding_concept ())
26663 fold_non_dependent_expr (expr, tf_none);
26664
26665 STRIP_ANY_LOCATION_WRAPPER (expr);
26666
26667 /* Preserve OVERLOADs; the functions must be available to resolve
26668 types. */
26669 inner_expr = expr;
26670 if (TREE_CODE (inner_expr) == STMT_EXPR)
26671 inner_expr = stmt_expr_value_expr (inner_expr);
26672 if (TREE_CODE (inner_expr) == ADDR_EXPR)
26673 inner_expr = TREE_OPERAND (inner_expr, 0);
26674 if (TREE_CODE (inner_expr) == COMPONENT_REF)
26675 inner_expr = TREE_OPERAND (inner_expr, 1);
26676 if (is_overloaded_fn (inner_expr)
26677 || TREE_CODE (inner_expr) == OFFSET_REF)
26678 return orig_expr;
26679 /* There is no need to return a proxy for a variable or enumerator. */
26680 if (VAR_P (expr) || TREE_CODE (expr) == CONST_DECL)
26681 return orig_expr;
26682 /* Preserve string constants; conversions from string constants to
26683 "char *" are allowed, even though normally a "const char *"
26684 cannot be used to initialize a "char *". */
26685 if (TREE_CODE (expr) == STRING_CST)
26686 return orig_expr;
26687 /* Preserve void and arithmetic constants, as an optimization -- there is no
26688 reason to create a new node. */
26689 if (TREE_CODE (expr) == VOID_CST
26690 || TREE_CODE (expr) == INTEGER_CST
26691 || TREE_CODE (expr) == REAL_CST)
26692 return orig_expr;
26693 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
26694 There is at least one place where we want to know that a
26695 particular expression is a throw-expression: when checking a ?:
26696 expression, there are special rules if the second or third
26697 argument is a throw-expression. */
26698 if (TREE_CODE (expr) == THROW_EXPR)
26699 return orig_expr;
26700
26701 /* Don't wrap an initializer list, we need to be able to look inside. */
26702 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
26703 return orig_expr;
26704
26705 /* Don't wrap a dummy object, we need to be able to test for it. */
26706 if (is_dummy_object (expr))
26707 return orig_expr;
26708
26709 if (TREE_CODE (expr) == COND_EXPR)
26710 return build3 (COND_EXPR,
26711 TREE_TYPE (expr),
26712 build_non_dependent_expr (TREE_OPERAND (expr, 0)),
26713 (TREE_OPERAND (expr, 1)
26714 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
26715 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
26716 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
26717 if (TREE_CODE (expr) == COMPOUND_EXPR
26718 && !COMPOUND_EXPR_OVERLOADED (expr))
26719 return build2 (COMPOUND_EXPR,
26720 TREE_TYPE (expr),
26721 TREE_OPERAND (expr, 0),
26722 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
26723
26724 /* If the type is unknown, it can't really be non-dependent */
26725 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
26726
26727 /* Otherwise, build a NON_DEPENDENT_EXPR. */
26728 return build1_loc (EXPR_LOCATION (orig_expr), NON_DEPENDENT_EXPR,
26729 TREE_TYPE (expr), expr);
26730 }
26731
26732 /* ARGS is a vector of expressions as arguments to a function call.
26733 Replace the arguments with equivalent non-dependent expressions.
26734 This modifies ARGS in place. */
26735
26736 void
26737 make_args_non_dependent (vec<tree, va_gc> *args)
26738 {
26739 unsigned int ix;
26740 tree arg;
26741
26742 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
26743 {
26744 tree newarg = build_non_dependent_expr (arg);
26745 if (newarg != arg)
26746 (*args)[ix] = newarg;
26747 }
26748 }
26749
26750 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
26751 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
26752 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
26753
26754 static tree
26755 make_auto_1 (tree name, bool set_canonical)
26756 {
26757 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
26758 TYPE_NAME (au) = build_decl (input_location,
26759 TYPE_DECL, name, au);
26760 TYPE_STUB_DECL (au) = TYPE_NAME (au);
26761 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
26762 (0, processing_template_decl + 1, processing_template_decl + 1,
26763 TYPE_NAME (au), NULL_TREE);
26764 if (set_canonical)
26765 TYPE_CANONICAL (au) = canonical_type_parameter (au);
26766 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
26767 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
26768
26769 return au;
26770 }
26771
26772 tree
26773 make_decltype_auto (void)
26774 {
26775 return make_auto_1 (decltype_auto_identifier, true);
26776 }
26777
26778 tree
26779 make_auto (void)
26780 {
26781 return make_auto_1 (auto_identifier, true);
26782 }
26783
26784 /* Return a C++17 deduction placeholder for class template TMPL. */
26785
26786 tree
26787 make_template_placeholder (tree tmpl)
26788 {
26789 tree t = make_auto_1 (auto_identifier, false);
26790 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
26791 /* Our canonical type depends on the placeholder. */
26792 TYPE_CANONICAL (t) = canonical_type_parameter (t);
26793 return t;
26794 }
26795
26796 /* True iff T is a C++17 class template deduction placeholder. */
26797
26798 bool
26799 template_placeholder_p (tree t)
26800 {
26801 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
26802 }
26803
26804 /* Make a "constrained auto" type-specifier. This is an
26805 auto type with constraints that must be associated after
26806 deduction. The constraint is formed from the given
26807 CONC and its optional sequence of arguments, which are
26808 non-null if written as partial-concept-id. */
26809
26810 tree
26811 make_constrained_auto (tree con, tree args)
26812 {
26813 tree type = make_auto_1 (auto_identifier, false);
26814
26815 /* Build the constraint. */
26816 tree tmpl = DECL_TI_TEMPLATE (con);
26817 tree expr = VAR_P (con) ? tmpl : ovl_make (tmpl);
26818 expr = build_concept_check (expr, type, args);
26819
26820 tree constr = normalize_expression (expr);
26821 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
26822
26823 /* Our canonical type depends on the constraint. */
26824 TYPE_CANONICAL (type) = canonical_type_parameter (type);
26825
26826 /* Attach the constraint to the type declaration. */
26827 tree decl = TYPE_NAME (type);
26828 return decl;
26829 }
26830
26831 /* Given type ARG, return std::initializer_list<ARG>. */
26832
26833 static tree
26834 listify (tree arg)
26835 {
26836 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
26837
26838 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
26839 {
26840 gcc_rich_location richloc (input_location);
26841 maybe_add_include_fixit (&richloc, "<initializer_list>", false);
26842 error_at (&richloc,
26843 "deducing from brace-enclosed initializer list"
26844 " requires %<#include <initializer_list>%>");
26845
26846 return error_mark_node;
26847 }
26848 tree argvec = make_tree_vec (1);
26849 TREE_VEC_ELT (argvec, 0) = arg;
26850
26851 return lookup_template_class (std_init_list, argvec, NULL_TREE,
26852 NULL_TREE, 0, tf_warning_or_error);
26853 }
26854
26855 /* Replace auto in TYPE with std::initializer_list<auto>. */
26856
26857 static tree
26858 listify_autos (tree type, tree auto_node)
26859 {
26860 tree init_auto = listify (strip_top_quals (auto_node));
26861 tree argvec = make_tree_vec (1);
26862 TREE_VEC_ELT (argvec, 0) = init_auto;
26863 if (processing_template_decl)
26864 argvec = add_to_template_args (current_template_args (), argvec);
26865 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
26866 }
26867
26868 /* Hash traits for hashing possibly constrained 'auto'
26869 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
26870
26871 struct auto_hash : default_hash_traits<tree>
26872 {
26873 static inline hashval_t hash (tree);
26874 static inline bool equal (tree, tree);
26875 };
26876
26877 /* Hash the 'auto' T. */
26878
26879 inline hashval_t
26880 auto_hash::hash (tree t)
26881 {
26882 if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t)))
26883 /* Matching constrained-type-specifiers denote the same template
26884 parameter, so hash the constraint. */
26885 return hash_placeholder_constraint (c);
26886 else
26887 /* But unconstrained autos are all separate, so just hash the pointer. */
26888 return iterative_hash_object (t, 0);
26889 }
26890
26891 /* Compare two 'auto's. */
26892
26893 inline bool
26894 auto_hash::equal (tree t1, tree t2)
26895 {
26896 if (t1 == t2)
26897 return true;
26898
26899 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
26900 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
26901
26902 /* Two unconstrained autos are distinct. */
26903 if (!c1 || !c2)
26904 return false;
26905
26906 return equivalent_placeholder_constraints (c1, c2);
26907 }
26908
26909 /* for_each_template_parm callback for extract_autos: if t is a (possibly
26910 constrained) auto, add it to the vector. */
26911
26912 static int
26913 extract_autos_r (tree t, void *data)
26914 {
26915 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
26916 if (is_auto (t))
26917 {
26918 /* All the autos were built with index 0; fix that up now. */
26919 tree *p = hash.find_slot (t, INSERT);
26920 unsigned idx;
26921 if (*p)
26922 /* If this is a repeated constrained-type-specifier, use the index we
26923 chose before. */
26924 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
26925 else
26926 {
26927 /* Otherwise this is new, so use the current count. */
26928 *p = t;
26929 idx = hash.elements () - 1;
26930 }
26931 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
26932 }
26933
26934 /* Always keep walking. */
26935 return 0;
26936 }
26937
26938 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
26939 says they can appear anywhere in the type. */
26940
26941 static tree
26942 extract_autos (tree type)
26943 {
26944 hash_set<tree> visited;
26945 hash_table<auto_hash> hash (2);
26946
26947 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
26948
26949 tree tree_vec = make_tree_vec (hash.elements());
26950 for (hash_table<auto_hash>::iterator iter = hash.begin();
26951 iter != hash.end(); ++iter)
26952 {
26953 tree elt = *iter;
26954 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
26955 TREE_VEC_ELT (tree_vec, i)
26956 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
26957 }
26958
26959 return tree_vec;
26960 }
26961
26962 /* The stem for deduction guide names. */
26963 const char *const dguide_base = "__dguide_";
26964
26965 /* Return the name for a deduction guide for class template TMPL. */
26966
26967 tree
26968 dguide_name (tree tmpl)
26969 {
26970 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
26971 tree tname = TYPE_IDENTIFIER (type);
26972 char *buf = (char *) alloca (1 + strlen (dguide_base)
26973 + IDENTIFIER_LENGTH (tname));
26974 memcpy (buf, dguide_base, strlen (dguide_base));
26975 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
26976 IDENTIFIER_LENGTH (tname) + 1);
26977 tree dname = get_identifier (buf);
26978 TREE_TYPE (dname) = type;
26979 return dname;
26980 }
26981
26982 /* True if NAME is the name of a deduction guide. */
26983
26984 bool
26985 dguide_name_p (tree name)
26986 {
26987 return (TREE_CODE (name) == IDENTIFIER_NODE
26988 && TREE_TYPE (name)
26989 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
26990 strlen (dguide_base)));
26991 }
26992
26993 /* True if FN is a deduction guide. */
26994
26995 bool
26996 deduction_guide_p (const_tree fn)
26997 {
26998 if (DECL_P (fn))
26999 if (tree name = DECL_NAME (fn))
27000 return dguide_name_p (name);
27001 return false;
27002 }
27003
27004 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
27005
27006 bool
27007 copy_guide_p (const_tree fn)
27008 {
27009 gcc_assert (deduction_guide_p (fn));
27010 if (!DECL_ARTIFICIAL (fn))
27011 return false;
27012 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
27013 return (TREE_CHAIN (parms) == void_list_node
27014 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
27015 }
27016
27017 /* True if FN is a guide generated from a constructor template. */
27018
27019 bool
27020 template_guide_p (const_tree fn)
27021 {
27022 gcc_assert (deduction_guide_p (fn));
27023 if (!DECL_ARTIFICIAL (fn))
27024 return false;
27025 tree tmpl = DECL_TI_TEMPLATE (fn);
27026 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
27027 return PRIMARY_TEMPLATE_P (org);
27028 return false;
27029 }
27030
27031 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
27032 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
27033 template parameter types. Note that the handling of template template
27034 parameters relies on current_template_parms being set appropriately for the
27035 new template. */
27036
27037 static tree
27038 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
27039 tree tsubst_args, tsubst_flags_t complain)
27040 {
27041 if (olddecl == error_mark_node)
27042 return error_mark_node;
27043
27044 tree oldidx = get_template_parm_index (olddecl);
27045
27046 tree newtype;
27047 if (TREE_CODE (olddecl) == TYPE_DECL
27048 || TREE_CODE (olddecl) == TEMPLATE_DECL)
27049 {
27050 tree oldtype = TREE_TYPE (olddecl);
27051 newtype = cxx_make_type (TREE_CODE (oldtype));
27052 TYPE_MAIN_VARIANT (newtype) = newtype;
27053 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
27054 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
27055 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
27056 }
27057 else
27058 {
27059 newtype = TREE_TYPE (olddecl);
27060 if (type_uses_auto (newtype))
27061 {
27062 // Substitute once to fix references to other template parameters.
27063 newtype = tsubst (newtype, tsubst_args,
27064 complain|tf_partial, NULL_TREE);
27065 // Now substitute again to reduce the level of the auto.
27066 newtype = tsubst (newtype, current_template_args (),
27067 complain, NULL_TREE);
27068 }
27069 else
27070 newtype = tsubst (newtype, tsubst_args,
27071 complain, NULL_TREE);
27072 }
27073
27074 tree newdecl
27075 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
27076 DECL_NAME (olddecl), newtype);
27077 SET_DECL_TEMPLATE_PARM_P (newdecl);
27078
27079 tree newidx;
27080 if (TREE_CODE (olddecl) == TYPE_DECL
27081 || TREE_CODE (olddecl) == TEMPLATE_DECL)
27082 {
27083 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
27084 = build_template_parm_index (index, level, level,
27085 newdecl, newtype);
27086 TEMPLATE_PARM_PARAMETER_PACK (newidx)
27087 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
27088 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
27089 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
27090
27091 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
27092 {
27093 DECL_TEMPLATE_RESULT (newdecl)
27094 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
27095 DECL_NAME (olddecl), newtype);
27096 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
27097 // First create a copy (ttargs) of tsubst_args with an
27098 // additional level for the template template parameter's own
27099 // template parameters (ttparms).
27100 tree ttparms = (INNERMOST_TEMPLATE_PARMS
27101 (DECL_TEMPLATE_PARMS (olddecl)));
27102 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
27103 tree ttargs = make_tree_vec (depth + 1);
27104 for (int i = 0; i < depth; ++i)
27105 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
27106 TREE_VEC_ELT (ttargs, depth)
27107 = template_parms_level_to_args (ttparms);
27108 // Substitute ttargs into ttparms to fix references to
27109 // other template parameters.
27110 ttparms = tsubst_template_parms_level (ttparms, ttargs,
27111 complain|tf_partial);
27112 // Now substitute again with args based on tparms, to reduce
27113 // the level of the ttparms.
27114 ttargs = current_template_args ();
27115 ttparms = tsubst_template_parms_level (ttparms, ttargs,
27116 complain);
27117 // Finally, tack the adjusted parms onto tparms.
27118 ttparms = tree_cons (size_int (depth), ttparms,
27119 current_template_parms);
27120 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
27121 }
27122 }
27123 else
27124 {
27125 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
27126 tree newconst
27127 = build_decl (DECL_SOURCE_LOCATION (oldconst),
27128 TREE_CODE (oldconst),
27129 DECL_NAME (oldconst), newtype);
27130 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
27131 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
27132 SET_DECL_TEMPLATE_PARM_P (newconst);
27133 newidx = build_template_parm_index (index, level, level,
27134 newconst, newtype);
27135 TEMPLATE_PARM_PARAMETER_PACK (newidx)
27136 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
27137 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
27138 }
27139
27140 return newdecl;
27141 }
27142
27143 /* Returns a C++17 class deduction guide template based on the constructor
27144 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
27145 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
27146
27147 static tree
27148 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
27149 {
27150 tree type, tparms, targs, fparms, fargs, ci;
27151 bool memtmpl = false;
27152 bool explicit_p;
27153 location_t loc;
27154 tree fn_tmpl = NULL_TREE;
27155
27156 if (TYPE_P (ctor))
27157 {
27158 type = ctor;
27159 bool copy_p = TYPE_REF_P (type);
27160 if (copy_p)
27161 {
27162 type = TREE_TYPE (type);
27163 fparms = tree_cons (NULL_TREE, type, void_list_node);
27164 }
27165 else
27166 fparms = void_list_node;
27167
27168 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
27169 tparms = DECL_TEMPLATE_PARMS (ctmpl);
27170 targs = CLASSTYPE_TI_ARGS (type);
27171 ci = NULL_TREE;
27172 fargs = NULL_TREE;
27173 loc = DECL_SOURCE_LOCATION (ctmpl);
27174 explicit_p = false;
27175 }
27176 else
27177 {
27178 ++processing_template_decl;
27179 bool ok = true;
27180
27181 fn_tmpl
27182 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
27183 : DECL_TI_TEMPLATE (ctor));
27184 if (outer_args)
27185 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
27186 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
27187
27188 type = DECL_CONTEXT (ctor);
27189
27190 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
27191 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
27192 fully specialized args for the enclosing class. Strip those off, as
27193 the deduction guide won't have those template parameters. */
27194 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
27195 TMPL_PARMS_DEPTH (tparms));
27196 /* Discard the 'this' parameter. */
27197 fparms = FUNCTION_ARG_CHAIN (ctor);
27198 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
27199 ci = get_constraints (ctor);
27200 loc = DECL_SOURCE_LOCATION (ctor);
27201 explicit_p = DECL_NONCONVERTING_P (ctor);
27202
27203 if (PRIMARY_TEMPLATE_P (fn_tmpl))
27204 {
27205 memtmpl = true;
27206
27207 /* For a member template constructor, we need to flatten the two
27208 template parameter lists into one, and then adjust the function
27209 signature accordingly. This gets...complicated. */
27210 tree save_parms = current_template_parms;
27211
27212 /* For a member template we should have two levels of parms/args, one
27213 for the class and one for the constructor. We stripped
27214 specialized args for further enclosing classes above. */
27215 const int depth = 2;
27216 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
27217
27218 /* Template args for translating references to the two-level template
27219 parameters into references to the one-level template parameters we
27220 are creating. */
27221 tree tsubst_args = copy_node (targs);
27222 TMPL_ARGS_LEVEL (tsubst_args, depth)
27223 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
27224
27225 /* Template parms for the constructor template. */
27226 tree ftparms = TREE_VALUE (tparms);
27227 unsigned flen = TREE_VEC_LENGTH (ftparms);
27228 /* Template parms for the class template. */
27229 tparms = TREE_CHAIN (tparms);
27230 tree ctparms = TREE_VALUE (tparms);
27231 unsigned clen = TREE_VEC_LENGTH (ctparms);
27232 /* Template parms for the deduction guide start as a copy of the
27233 template parms for the class. We set current_template_parms for
27234 lookup_template_class_1. */
27235 current_template_parms = tparms = copy_node (tparms);
27236 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
27237 for (unsigned i = 0; i < clen; ++i)
27238 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
27239
27240 /* Now we need to rewrite the constructor parms to append them to the
27241 class parms. */
27242 for (unsigned i = 0; i < flen; ++i)
27243 {
27244 unsigned index = i + clen;
27245 unsigned level = 1;
27246 tree oldelt = TREE_VEC_ELT (ftparms, i);
27247 tree olddecl = TREE_VALUE (oldelt);
27248 tree newdecl = rewrite_template_parm (olddecl, index, level,
27249 tsubst_args, complain);
27250 if (newdecl == error_mark_node)
27251 ok = false;
27252 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
27253 tsubst_args, complain, ctor);
27254 tree list = build_tree_list (newdef, newdecl);
27255 TEMPLATE_PARM_CONSTRAINTS (list)
27256 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
27257 tsubst_args, complain, ctor);
27258 TREE_VEC_ELT (new_vec, index) = list;
27259 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
27260 }
27261
27262 /* Now we have a final set of template parms to substitute into the
27263 function signature. */
27264 targs = template_parms_to_args (tparms);
27265 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
27266 complain, ctor);
27267 if (fparms == error_mark_node)
27268 ok = false;
27269 fargs = tsubst (fargs, tsubst_args, complain, ctor);
27270 if (ci)
27271 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
27272
27273 current_template_parms = save_parms;
27274 }
27275
27276 --processing_template_decl;
27277 if (!ok)
27278 return error_mark_node;
27279 }
27280
27281 if (!memtmpl)
27282 {
27283 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
27284 tparms = copy_node (tparms);
27285 INNERMOST_TEMPLATE_PARMS (tparms)
27286 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
27287 }
27288
27289 tree fntype = build_function_type (type, fparms);
27290 tree ded_fn = build_lang_decl_loc (loc,
27291 FUNCTION_DECL,
27292 dguide_name (type), fntype);
27293 DECL_ARGUMENTS (ded_fn) = fargs;
27294 DECL_ARTIFICIAL (ded_fn) = true;
27295 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
27296 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
27297 DECL_ARTIFICIAL (ded_tmpl) = true;
27298 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
27299 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
27300 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
27301 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
27302 if (DECL_P (ctor))
27303 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
27304 if (ci)
27305 set_constraints (ded_tmpl, ci);
27306
27307 return ded_tmpl;
27308 }
27309
27310 /* Deduce template arguments for the class template placeholder PTYPE for
27311 template TMPL based on the initializer INIT, and return the resulting
27312 type. */
27313
27314 static tree
27315 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
27316 tsubst_flags_t complain)
27317 {
27318 if (!DECL_CLASS_TEMPLATE_P (tmpl))
27319 {
27320 /* We should have handled this in the caller. */
27321 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
27322 return ptype;
27323 if (complain & tf_error)
27324 error ("non-class template %qT used without template arguments", tmpl);
27325 return error_mark_node;
27326 }
27327 if (init && TREE_TYPE (init) == ptype)
27328 /* Using the template parm as its own argument. */
27329 return ptype;
27330
27331 tree type = TREE_TYPE (tmpl);
27332
27333 bool try_list_ctor = false;
27334
27335 releasing_vec rv_args = NULL;
27336 vec<tree,va_gc> *&args = *&rv_args;
27337 if (init == NULL_TREE
27338 || TREE_CODE (init) == TREE_LIST)
27339 args = make_tree_vector_from_list (init);
27340 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
27341 {
27342 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
27343 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
27344 {
27345 /* As an exception, the first phase in 16.3.1.7 (considering the
27346 initializer list as a single argument) is omitted if the
27347 initializer list consists of a single expression of type cv U,
27348 where U is a specialization of C or a class derived from a
27349 specialization of C. */
27350 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
27351 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
27352 {
27353 tree etype = TREE_TYPE (elt);
27354 tree tparms = (INNERMOST_TEMPLATE_PARMS
27355 (DECL_TEMPLATE_PARMS (tmpl)));
27356 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
27357 int err = unify (tparms, targs, type, etype,
27358 UNIFY_ALLOW_DERIVED, /*explain*/false);
27359 if (err == 0)
27360 try_list_ctor = false;
27361 ggc_free (targs);
27362 }
27363 }
27364 if (try_list_ctor || is_std_init_list (type))
27365 args = make_tree_vector_single (init);
27366 else
27367 args = make_tree_vector_from_ctor (init);
27368 }
27369 else
27370 args = make_tree_vector_single (init);
27371
27372 tree dname = dguide_name (tmpl);
27373 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
27374 /*type*/false, /*complain*/false,
27375 /*hidden*/false);
27376 bool elided = false;
27377 if (cands == error_mark_node)
27378 cands = NULL_TREE;
27379
27380 /* Prune explicit deduction guides in copy-initialization context. */
27381 if (flags & LOOKUP_ONLYCONVERTING)
27382 {
27383 for (lkp_iterator iter (cands); !elided && iter; ++iter)
27384 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
27385 elided = true;
27386
27387 if (elided)
27388 {
27389 /* Found a nonconverting guide, prune the candidates. */
27390 tree pruned = NULL_TREE;
27391 for (lkp_iterator iter (cands); iter; ++iter)
27392 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
27393 pruned = lookup_add (*iter, pruned);
27394
27395 cands = pruned;
27396 }
27397 }
27398
27399 tree outer_args = NULL_TREE;
27400 if (DECL_CLASS_SCOPE_P (tmpl)
27401 && CLASSTYPE_TEMPLATE_INSTANTIATION (DECL_CONTEXT (tmpl)))
27402 {
27403 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
27404 type = TREE_TYPE (most_general_template (tmpl));
27405 }
27406
27407 bool saw_ctor = false;
27408 // FIXME cache artificial deduction guides
27409 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
27410 {
27411 /* Skip inherited constructors. */
27412 if (iter.using_p ())
27413 continue;
27414
27415 tree guide = build_deduction_guide (*iter, outer_args, complain);
27416 if (guide == error_mark_node)
27417 return error_mark_node;
27418 if ((flags & LOOKUP_ONLYCONVERTING)
27419 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
27420 elided = true;
27421 else
27422 cands = lookup_add (guide, cands);
27423
27424 saw_ctor = true;
27425 }
27426
27427 tree call = error_mark_node;
27428
27429 /* If this is list-initialization and the class has a list constructor, first
27430 try deducing from the list as a single argument, as [over.match.list]. */
27431 tree list_cands = NULL_TREE;
27432 if (try_list_ctor && cands)
27433 for (lkp_iterator iter (cands); iter; ++iter)
27434 {
27435 tree dg = *iter;
27436 if (is_list_ctor (dg))
27437 list_cands = lookup_add (dg, list_cands);
27438 }
27439 if (list_cands)
27440 {
27441 ++cp_unevaluated_operand;
27442 call = build_new_function_call (list_cands, &args, tf_decltype);
27443 --cp_unevaluated_operand;
27444
27445 if (call == error_mark_node)
27446 {
27447 /* That didn't work, now try treating the list as a sequence of
27448 arguments. */
27449 release_tree_vector (args);
27450 args = make_tree_vector_from_ctor (init);
27451 }
27452 }
27453
27454 /* Maybe generate an implicit deduction guide. */
27455 if (call == error_mark_node && args->length () < 2)
27456 {
27457 tree gtype = NULL_TREE;
27458
27459 if (args->length () == 1)
27460 /* Generate a copy guide. */
27461 gtype = build_reference_type (type);
27462 else if (!saw_ctor)
27463 /* Generate a default guide. */
27464 gtype = type;
27465
27466 if (gtype)
27467 {
27468 tree guide = build_deduction_guide (gtype, outer_args, complain);
27469 if (guide == error_mark_node)
27470 return error_mark_node;
27471 cands = lookup_add (guide, cands);
27472 }
27473 }
27474
27475 if (elided && !cands)
27476 {
27477 error ("cannot deduce template arguments for copy-initialization"
27478 " of %qT, as it has no non-explicit deduction guides or "
27479 "user-declared constructors", type);
27480 return error_mark_node;
27481 }
27482 else if (!cands && call == error_mark_node)
27483 {
27484 error ("cannot deduce template arguments of %qT, as it has no viable "
27485 "deduction guides", type);
27486 return error_mark_node;
27487 }
27488
27489 if (call == error_mark_node)
27490 {
27491 ++cp_unevaluated_operand;
27492 call = build_new_function_call (cands, &args, tf_decltype);
27493 --cp_unevaluated_operand;
27494 }
27495
27496 if (call == error_mark_node && (complain & tf_warning_or_error))
27497 {
27498 error ("class template argument deduction failed:");
27499
27500 ++cp_unevaluated_operand;
27501 call = build_new_function_call (cands, &args, complain | tf_decltype);
27502 --cp_unevaluated_operand;
27503
27504 if (elided)
27505 inform (input_location, "explicit deduction guides not considered "
27506 "for copy-initialization");
27507 }
27508
27509 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
27510 }
27511
27512 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
27513 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
27514 The CONTEXT determines the context in which auto deduction is performed
27515 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
27516 OUTER_TARGS are used during template argument deduction
27517 (context == adc_unify) to properly substitute the result, and is ignored
27518 in other contexts.
27519
27520 For partial-concept-ids, extra args may be appended to the list of deduced
27521 template arguments prior to determining constraint satisfaction. */
27522
27523 tree
27524 do_auto_deduction (tree type, tree init, tree auto_node,
27525 tsubst_flags_t complain, auto_deduction_context context,
27526 tree outer_targs, int flags)
27527 {
27528 tree targs;
27529
27530 if (init == error_mark_node)
27531 return error_mark_node;
27532
27533 if (init && type_dependent_expression_p (init)
27534 && context != adc_unify)
27535 /* Defining a subset of type-dependent expressions that we can deduce
27536 from ahead of time isn't worth the trouble. */
27537 return type;
27538
27539 /* Similarly, we can't deduce from another undeduced decl. */
27540 if (init && undeduced_auto_decl (init))
27541 return type;
27542
27543 /* We may be doing a partial substitution, but we still want to replace
27544 auto_node. */
27545 complain &= ~tf_partial;
27546
27547 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
27548 /* C++17 class template argument deduction. */
27549 return do_class_deduction (type, tmpl, init, flags, complain);
27550
27551 if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
27552 /* Nothing we can do with this, even in deduction context. */
27553 return type;
27554
27555 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
27556 with either a new invented type template parameter U or, if the
27557 initializer is a braced-init-list (8.5.4), with
27558 std::initializer_list<U>. */
27559 if (BRACE_ENCLOSED_INITIALIZER_P (init))
27560 {
27561 if (!DIRECT_LIST_INIT_P (init))
27562 type = listify_autos (type, auto_node);
27563 else if (CONSTRUCTOR_NELTS (init) == 1)
27564 init = CONSTRUCTOR_ELT (init, 0)->value;
27565 else
27566 {
27567 if (complain & tf_warning_or_error)
27568 {
27569 if (permerror (input_location, "direct-list-initialization of "
27570 "%<auto%> requires exactly one element"))
27571 inform (input_location,
27572 "for deduction to %<std::initializer_list%>, use copy-"
27573 "list-initialization (i.e. add %<=%> before the %<{%>)");
27574 }
27575 type = listify_autos (type, auto_node);
27576 }
27577 }
27578
27579 if (type == error_mark_node)
27580 return error_mark_node;
27581
27582 init = resolve_nondeduced_context (init, complain);
27583
27584 if (context == adc_decomp_type
27585 && auto_node == type
27586 && init != error_mark_node
27587 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
27588 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
27589 and initializer has array type, deduce cv-qualified array type. */
27590 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
27591 complain);
27592 else if (AUTO_IS_DECLTYPE (auto_node))
27593 {
27594 tree stripped_init = tree_strip_any_location_wrapper (init);
27595 bool id = (DECL_P (stripped_init)
27596 || ((TREE_CODE (init) == COMPONENT_REF
27597 || TREE_CODE (init) == SCOPE_REF)
27598 && !REF_PARENTHESIZED_P (init)));
27599 targs = make_tree_vec (1);
27600 TREE_VEC_ELT (targs, 0)
27601 = finish_decltype_type (init, id, tf_warning_or_error);
27602 if (type != auto_node)
27603 {
27604 if (complain & tf_error)
27605 error ("%qT as type rather than plain %<decltype(auto)%>", type);
27606 return error_mark_node;
27607 }
27608 }
27609 else
27610 {
27611 if (error_operand_p (init))
27612 return error_mark_node;
27613
27614 tree parms = build_tree_list (NULL_TREE, type);
27615 tree tparms;
27616
27617 if (flag_concepts)
27618 tparms = extract_autos (type);
27619 else
27620 {
27621 tparms = make_tree_vec (1);
27622 TREE_VEC_ELT (tparms, 0)
27623 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
27624 }
27625
27626 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
27627 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
27628 DEDUCE_CALL,
27629 NULL, /*explain_p=*/false);
27630 if (val > 0)
27631 {
27632 if (processing_template_decl)
27633 /* Try again at instantiation time. */
27634 return type;
27635 if (type && type != error_mark_node
27636 && (complain & tf_error))
27637 /* If type is error_mark_node a diagnostic must have been
27638 emitted by now. Also, having a mention to '<type error>'
27639 in the diagnostic is not really useful to the user. */
27640 {
27641 if (cfun
27642 && FNDECL_USED_AUTO (current_function_decl)
27643 && (auto_node
27644 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl))
27645 && LAMBDA_FUNCTION_P (current_function_decl))
27646 error ("unable to deduce lambda return type from %qE", init);
27647 else
27648 error ("unable to deduce %qT from %qE", type, init);
27649 type_unification_real (tparms, targs, parms, &init, 1, 0,
27650 DEDUCE_CALL,
27651 NULL, /*explain_p=*/true);
27652 }
27653 return error_mark_node;
27654 }
27655 }
27656
27657 /* Check any placeholder constraints against the deduced type. */
27658 if (flag_concepts && !processing_template_decl)
27659 if (tree constr = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node)))
27660 {
27661 /* Use the deduced type to check the associated constraints. If we
27662 have a partial-concept-id, rebuild the argument list so that
27663 we check using the extra arguments. */
27664 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
27665 tree cargs = CHECK_CONSTR_ARGS (constr);
27666 if (TREE_VEC_LENGTH (cargs) > 1)
27667 {
27668 cargs = copy_node (cargs);
27669 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
27670 }
27671 else
27672 cargs = targs;
27673 if (!constraints_satisfied_p (constr, cargs))
27674 {
27675 if (complain & tf_warning_or_error)
27676 {
27677 auto_diagnostic_group d;
27678 switch (context)
27679 {
27680 case adc_unspecified:
27681 case adc_unify:
27682 error("placeholder constraints not satisfied");
27683 break;
27684 case adc_variable_type:
27685 case adc_decomp_type:
27686 error ("deduced initializer does not satisfy "
27687 "placeholder constraints");
27688 break;
27689 case adc_return_type:
27690 error ("deduced return type does not satisfy "
27691 "placeholder constraints");
27692 break;
27693 case adc_requirement:
27694 error ("deduced expression type does not satisfy "
27695 "placeholder constraints");
27696 break;
27697 }
27698 diagnose_constraints (input_location, constr, targs);
27699 }
27700 return error_mark_node;
27701 }
27702 }
27703
27704 if (processing_template_decl && context != adc_unify)
27705 outer_targs = current_template_args ();
27706 targs = add_to_template_args (outer_targs, targs);
27707 return tsubst (type, targs, complain, NULL_TREE);
27708 }
27709
27710 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
27711 result. */
27712
27713 tree
27714 splice_late_return_type (tree type, tree late_return_type)
27715 {
27716 if (is_auto (type))
27717 {
27718 if (late_return_type)
27719 return late_return_type;
27720
27721 tree idx = get_template_parm_index (type);
27722 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
27723 /* In an abbreviated function template we didn't know we were dealing
27724 with a function template when we saw the auto return type, so update
27725 it to have the correct level. */
27726 return make_auto_1 (TYPE_IDENTIFIER (type), true);
27727 }
27728 return type;
27729 }
27730
27731 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
27732 'decltype(auto)' or a deduced class template. */
27733
27734 bool
27735 is_auto (const_tree type)
27736 {
27737 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
27738 && (TYPE_IDENTIFIER (type) == auto_identifier
27739 || TYPE_IDENTIFIER (type) == decltype_auto_identifier))
27740 return true;
27741 else
27742 return false;
27743 }
27744
27745 /* for_each_template_parm callback for type_uses_auto. */
27746
27747 int
27748 is_auto_r (tree tp, void */*data*/)
27749 {
27750 return is_auto (tp);
27751 }
27752
27753 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
27754 a use of `auto'. Returns NULL_TREE otherwise. */
27755
27756 tree
27757 type_uses_auto (tree type)
27758 {
27759 if (type == NULL_TREE)
27760 return NULL_TREE;
27761 else if (flag_concepts)
27762 {
27763 /* The Concepts TS allows multiple autos in one type-specifier; just
27764 return the first one we find, do_auto_deduction will collect all of
27765 them. */
27766 if (uses_template_parms (type))
27767 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
27768 /*visited*/NULL, /*nondeduced*/false);
27769 else
27770 return NULL_TREE;
27771 }
27772 else
27773 return find_type_usage (type, is_auto);
27774 }
27775
27776 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
27777 concepts are enabled, auto is acceptable in template arguments, but
27778 only when TEMPL identifies a template class. Return TRUE if any
27779 such errors were reported. */
27780
27781 bool
27782 check_auto_in_tmpl_args (tree tmpl, tree args)
27783 {
27784 /* If there were previous errors, nevermind. */
27785 if (!args || TREE_CODE (args) != TREE_VEC)
27786 return false;
27787
27788 /* If TMPL is an identifier, we're parsing and we can't tell yet
27789 whether TMPL is supposed to be a type, a function or a variable.
27790 We'll only be able to tell during template substitution, so we
27791 expect to be called again then. If concepts are enabled and we
27792 know we have a type, we're ok. */
27793 if (flag_concepts
27794 && (identifier_p (tmpl)
27795 || (DECL_P (tmpl)
27796 && (DECL_TYPE_TEMPLATE_P (tmpl)
27797 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))))
27798 return false;
27799
27800 /* Quickly search for any occurrences of auto; usually there won't
27801 be any, and then we'll avoid allocating the vector. */
27802 if (!type_uses_auto (args))
27803 return false;
27804
27805 bool errors = false;
27806
27807 tree vec = extract_autos (args);
27808 for (int i = 0; i < TREE_VEC_LENGTH (vec); i++)
27809 {
27810 tree xauto = TREE_VALUE (TREE_VEC_ELT (vec, i));
27811 error_at (DECL_SOURCE_LOCATION (xauto),
27812 "invalid use of %qT in template argument", xauto);
27813 errors = true;
27814 }
27815
27816 return errors;
27817 }
27818
27819 /* For a given template T, return the vector of typedefs referenced
27820 in T for which access check is needed at T instantiation time.
27821 T is either a FUNCTION_DECL or a RECORD_TYPE.
27822 Those typedefs were added to T by the function
27823 append_type_to_template_for_access_check. */
27824
27825 vec<qualified_typedef_usage_t, va_gc> *
27826 get_types_needing_access_check (tree t)
27827 {
27828 tree ti;
27829 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
27830
27831 if (!t || t == error_mark_node)
27832 return NULL;
27833
27834 if (!(ti = get_template_info (t)))
27835 return NULL;
27836
27837 if (CLASS_TYPE_P (t)
27838 || TREE_CODE (t) == FUNCTION_DECL)
27839 {
27840 if (!TI_TEMPLATE (ti))
27841 return NULL;
27842
27843 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
27844 }
27845
27846 return result;
27847 }
27848
27849 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
27850 tied to T. That list of typedefs will be access checked at
27851 T instantiation time.
27852 T is either a FUNCTION_DECL or a RECORD_TYPE.
27853 TYPE_DECL is a TYPE_DECL node representing a typedef.
27854 SCOPE is the scope through which TYPE_DECL is accessed.
27855 LOCATION is the location of the usage point of TYPE_DECL.
27856
27857 This function is a subroutine of
27858 append_type_to_template_for_access_check. */
27859
27860 static void
27861 append_type_to_template_for_access_check_1 (tree t,
27862 tree type_decl,
27863 tree scope,
27864 location_t location)
27865 {
27866 qualified_typedef_usage_t typedef_usage;
27867 tree ti;
27868
27869 if (!t || t == error_mark_node)
27870 return;
27871
27872 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
27873 || CLASS_TYPE_P (t))
27874 && type_decl
27875 && TREE_CODE (type_decl) == TYPE_DECL
27876 && scope);
27877
27878 if (!(ti = get_template_info (t)))
27879 return;
27880
27881 gcc_assert (TI_TEMPLATE (ti));
27882
27883 typedef_usage.typedef_decl = type_decl;
27884 typedef_usage.context = scope;
27885 typedef_usage.locus = location;
27886
27887 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
27888 }
27889
27890 /* Append TYPE_DECL to the template TEMPL.
27891 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
27892 At TEMPL instanciation time, TYPE_DECL will be checked to see
27893 if it can be accessed through SCOPE.
27894 LOCATION is the location of the usage point of TYPE_DECL.
27895
27896 e.g. consider the following code snippet:
27897
27898 class C
27899 {
27900 typedef int myint;
27901 };
27902
27903 template<class U> struct S
27904 {
27905 C::myint mi; // <-- usage point of the typedef C::myint
27906 };
27907
27908 S<char> s;
27909
27910 At S<char> instantiation time, we need to check the access of C::myint
27911 In other words, we need to check the access of the myint typedef through
27912 the C scope. For that purpose, this function will add the myint typedef
27913 and the scope C through which its being accessed to a list of typedefs
27914 tied to the template S. That list will be walked at template instantiation
27915 time and access check performed on each typedefs it contains.
27916 Note that this particular code snippet should yield an error because
27917 myint is private to C. */
27918
27919 void
27920 append_type_to_template_for_access_check (tree templ,
27921 tree type_decl,
27922 tree scope,
27923 location_t location)
27924 {
27925 qualified_typedef_usage_t *iter;
27926 unsigned i;
27927
27928 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
27929
27930 /* Make sure we don't append the type to the template twice. */
27931 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
27932 if (iter->typedef_decl == type_decl && scope == iter->context)
27933 return;
27934
27935 append_type_to_template_for_access_check_1 (templ, type_decl,
27936 scope, location);
27937 }
27938
27939 /* Convert the generic type parameters in PARM that match the types given in the
27940 range [START_IDX, END_IDX) from the current_template_parms into generic type
27941 packs. */
27942
27943 tree
27944 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
27945 {
27946 tree current = current_template_parms;
27947 int depth = TMPL_PARMS_DEPTH (current);
27948 current = INNERMOST_TEMPLATE_PARMS (current);
27949 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
27950
27951 for (int i = 0; i < start_idx; ++i)
27952 TREE_VEC_ELT (replacement, i)
27953 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
27954
27955 for (int i = start_idx; i < end_idx; ++i)
27956 {
27957 /* Create a distinct parameter pack type from the current parm and add it
27958 to the replacement args to tsubst below into the generic function
27959 parameter. */
27960
27961 tree o = TREE_TYPE (TREE_VALUE
27962 (TREE_VEC_ELT (current, i)));
27963 tree t = copy_type (o);
27964 TEMPLATE_TYPE_PARM_INDEX (t)
27965 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
27966 o, 0, 0, tf_none);
27967 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
27968 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
27969 TYPE_MAIN_VARIANT (t) = t;
27970 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
27971 TYPE_CANONICAL (t) = canonical_type_parameter (t);
27972 TREE_VEC_ELT (replacement, i) = t;
27973 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
27974 }
27975
27976 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
27977 TREE_VEC_ELT (replacement, i)
27978 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
27979
27980 /* If there are more levels then build up the replacement with the outer
27981 template parms. */
27982 if (depth > 1)
27983 replacement = add_to_template_args (template_parms_to_args
27984 (TREE_CHAIN (current_template_parms)),
27985 replacement);
27986
27987 return tsubst (parm, replacement, tf_none, NULL_TREE);
27988 }
27989
27990 /* Entries in the decl_constraint hash table. */
27991 struct GTY((for_user)) constr_entry
27992 {
27993 tree decl;
27994 tree ci;
27995 };
27996
27997 /* Hashing function and equality for constraint entries. */
27998 struct constr_hasher : ggc_ptr_hash<constr_entry>
27999 {
28000 static hashval_t hash (constr_entry *e)
28001 {
28002 return (hashval_t)DECL_UID (e->decl);
28003 }
28004
28005 static bool equal (constr_entry *e1, constr_entry *e2)
28006 {
28007 return e1->decl == e2->decl;
28008 }
28009 };
28010
28011 /* A mapping from declarations to constraint information. Note that
28012 both templates and their underlying declarations are mapped to the
28013 same constraint information.
28014
28015 FIXME: This is defined in pt.c because garbage collection
28016 code is not being generated for constraint.cc. */
28017
28018 static GTY (()) hash_table<constr_hasher> *decl_constraints;
28019
28020 /* Returns the template constraints of declaration T. If T is not
28021 constrained, return NULL_TREE. Note that T must be non-null. */
28022
28023 tree
28024 get_constraints (tree t)
28025 {
28026 if (!flag_concepts)
28027 return NULL_TREE;
28028
28029 gcc_assert (DECL_P (t));
28030 if (TREE_CODE (t) == TEMPLATE_DECL)
28031 t = DECL_TEMPLATE_RESULT (t);
28032 constr_entry elt = { t, NULL_TREE };
28033 constr_entry* found = decl_constraints->find (&elt);
28034 if (found)
28035 return found->ci;
28036 else
28037 return NULL_TREE;
28038 }
28039
28040 /* Associate the given constraint information CI with the declaration
28041 T. If T is a template, then the constraints are associated with
28042 its underlying declaration. Don't build associations if CI is
28043 NULL_TREE. */
28044
28045 void
28046 set_constraints (tree t, tree ci)
28047 {
28048 if (!ci)
28049 return;
28050 gcc_assert (t && flag_concepts);
28051 if (TREE_CODE (t) == TEMPLATE_DECL)
28052 t = DECL_TEMPLATE_RESULT (t);
28053 gcc_assert (!get_constraints (t));
28054 constr_entry elt = {t, ci};
28055 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
28056 constr_entry* entry = ggc_alloc<constr_entry> ();
28057 *entry = elt;
28058 *slot = entry;
28059 }
28060
28061 /* Remove the associated constraints of the declaration T. */
28062
28063 void
28064 remove_constraints (tree t)
28065 {
28066 gcc_assert (DECL_P (t));
28067 if (TREE_CODE (t) == TEMPLATE_DECL)
28068 t = DECL_TEMPLATE_RESULT (t);
28069
28070 constr_entry elt = {t, NULL_TREE};
28071 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
28072 if (slot)
28073 decl_constraints->clear_slot (slot);
28074 }
28075
28076 /* Memoized satisfaction results for declarations. This
28077 maps the pair (constraint_info, arguments) to the result computed
28078 by constraints_satisfied_p. */
28079
28080 struct GTY((for_user)) constraint_sat_entry
28081 {
28082 tree ci;
28083 tree args;
28084 tree result;
28085 };
28086
28087 /* Hashing function and equality for constraint entries. */
28088
28089 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
28090 {
28091 static hashval_t hash (constraint_sat_entry *e)
28092 {
28093 hashval_t val = iterative_hash_object(e->ci, 0);
28094 return iterative_hash_template_arg (e->args, val);
28095 }
28096
28097 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
28098 {
28099 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
28100 }
28101 };
28102
28103 /* Memoized satisfaction results for concept checks. */
28104
28105 struct GTY((for_user)) concept_spec_entry
28106 {
28107 tree tmpl;
28108 tree args;
28109 tree result;
28110 };
28111
28112 /* Hashing function and equality for constraint entries. */
28113
28114 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
28115 {
28116 static hashval_t hash (concept_spec_entry *e)
28117 {
28118 return hash_tmpl_and_args (e->tmpl, e->args);
28119 }
28120
28121 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
28122 {
28123 ++comparing_specializations;
28124 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
28125 --comparing_specializations;
28126 return eq;
28127 }
28128 };
28129
28130 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
28131 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
28132
28133 /* Search for a memoized satisfaction result. Returns one of the
28134 truth value nodes if previously memoized, or NULL_TREE otherwise. */
28135
28136 tree
28137 lookup_constraint_satisfaction (tree ci, tree args)
28138 {
28139 constraint_sat_entry elt = { ci, args, NULL_TREE };
28140 constraint_sat_entry* found = constraint_memos->find (&elt);
28141 if (found)
28142 return found->result;
28143 else
28144 return NULL_TREE;
28145 }
28146
28147 /* Memoize the result of a satisfication test. Returns the saved result. */
28148
28149 tree
28150 memoize_constraint_satisfaction (tree ci, tree args, tree result)
28151 {
28152 constraint_sat_entry elt = {ci, args, result};
28153 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
28154 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
28155 *entry = elt;
28156 *slot = entry;
28157 return result;
28158 }
28159
28160 /* Search for a memoized satisfaction result for a concept. */
28161
28162 tree
28163 lookup_concept_satisfaction (tree tmpl, tree args)
28164 {
28165 concept_spec_entry elt = { tmpl, args, NULL_TREE };
28166 concept_spec_entry* found = concept_memos->find (&elt);
28167 if (found)
28168 return found->result;
28169 else
28170 return NULL_TREE;
28171 }
28172
28173 /* Memoize the result of a concept check. Returns the saved result. */
28174
28175 tree
28176 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
28177 {
28178 concept_spec_entry elt = {tmpl, args, result};
28179 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
28180 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
28181 *entry = elt;
28182 *slot = entry;
28183 return result;
28184 }
28185
28186 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
28187
28188 /* Returns a prior concept specialization. This returns the substituted
28189 and normalized constraints defined by the concept. */
28190
28191 tree
28192 get_concept_expansion (tree tmpl, tree args)
28193 {
28194 concept_spec_entry elt = { tmpl, args, NULL_TREE };
28195 concept_spec_entry* found = concept_expansions->find (&elt);
28196 if (found)
28197 return found->result;
28198 else
28199 return NULL_TREE;
28200 }
28201
28202 /* Save a concept expansion for later. */
28203
28204 tree
28205 save_concept_expansion (tree tmpl, tree args, tree def)
28206 {
28207 concept_spec_entry elt = {tmpl, args, def};
28208 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
28209 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
28210 *entry = elt;
28211 *slot = entry;
28212 return def;
28213 }
28214
28215 static hashval_t
28216 hash_subsumption_args (tree t1, tree t2)
28217 {
28218 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
28219 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
28220 int val = 0;
28221 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
28222 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
28223 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
28224 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
28225 return val;
28226 }
28227
28228 /* Compare the constraints of two subsumption entries. The LEFT1 and
28229 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
28230 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
28231
28232 static bool
28233 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
28234 {
28235 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
28236 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
28237 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
28238 CHECK_CONSTR_ARGS (right1)))
28239 return comp_template_args (CHECK_CONSTR_ARGS (left2),
28240 CHECK_CONSTR_ARGS (right2));
28241 return false;
28242 }
28243
28244 /* Key/value pair for learning and memoizing subsumption results. This
28245 associates a pair of check constraints (including arguments) with
28246 a boolean value indicating the result. */
28247
28248 struct GTY((for_user)) subsumption_entry
28249 {
28250 tree t1;
28251 tree t2;
28252 bool result;
28253 };
28254
28255 /* Hashing function and equality for constraint entries. */
28256
28257 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
28258 {
28259 static hashval_t hash (subsumption_entry *e)
28260 {
28261 return hash_subsumption_args (e->t1, e->t2);
28262 }
28263
28264 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
28265 {
28266 ++comparing_specializations;
28267 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
28268 --comparing_specializations;
28269 return eq;
28270 }
28271 };
28272
28273 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
28274
28275 /* Search for a previously cached subsumption result. */
28276
28277 bool*
28278 lookup_subsumption_result (tree t1, tree t2)
28279 {
28280 subsumption_entry elt = { t1, t2, false };
28281 subsumption_entry* found = subsumption_table->find (&elt);
28282 if (found)
28283 return &found->result;
28284 else
28285 return 0;
28286 }
28287
28288 /* Save a subsumption result. */
28289
28290 bool
28291 save_subsumption_result (tree t1, tree t2, bool result)
28292 {
28293 subsumption_entry elt = {t1, t2, result};
28294 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
28295 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
28296 *entry = elt;
28297 *slot = entry;
28298 return result;
28299 }
28300
28301 /* Set up the hash table for constraint association. */
28302
28303 void
28304 init_constraint_processing (void)
28305 {
28306 if (!flag_concepts)
28307 return;
28308
28309 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
28310 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
28311 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
28312 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
28313 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
28314 }
28315
28316 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
28317 0..N-1. */
28318
28319 void
28320 declare_integer_pack (void)
28321 {
28322 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
28323 build_function_type_list (integer_type_node,
28324 integer_type_node,
28325 NULL_TREE),
28326 NULL_TREE, ECF_CONST);
28327 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
28328 set_decl_built_in_function (ipfn, BUILT_IN_FRONTEND,
28329 CP_BUILT_IN_INTEGER_PACK);
28330 }
28331
28332 /* Set up the hash tables for template instantiations. */
28333
28334 void
28335 init_template_processing (void)
28336 {
28337 /* FIXME: enable sanitization (PR87847) */
28338 decl_specializations = hash_table<spec_hasher>::create_ggc (37, false);
28339 type_specializations = hash_table<spec_hasher>::create_ggc (37, false);
28340
28341 if (cxx_dialect >= cxx11)
28342 declare_integer_pack ();
28343 }
28344
28345 /* Print stats about the template hash tables for -fstats. */
28346
28347 void
28348 print_template_statistics (void)
28349 {
28350 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
28351 "%f collisions\n", (long) decl_specializations->size (),
28352 (long) decl_specializations->elements (),
28353 decl_specializations->collisions ());
28354 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
28355 "%f collisions\n", (long) type_specializations->size (),
28356 (long) type_specializations->elements (),
28357 type_specializations->collisions ());
28358 }
28359
28360 #if CHECKING_P
28361
28362 namespace selftest {
28363
28364 /* Verify that build_non_dependent_expr () works, for various expressions,
28365 and that location wrappers don't affect the results. */
28366
28367 static void
28368 test_build_non_dependent_expr ()
28369 {
28370 location_t loc = BUILTINS_LOCATION;
28371
28372 /* Verify constants, without and with location wrappers. */
28373 tree int_cst = build_int_cst (integer_type_node, 42);
28374 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
28375
28376 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
28377 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
28378 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
28379
28380 tree string_lit = build_string (4, "foo");
28381 TREE_TYPE (string_lit) = char_array_type_node;
28382 string_lit = fix_string_type (string_lit);
28383 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
28384
28385 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
28386 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
28387 ASSERT_EQ (wrapped_string_lit,
28388 build_non_dependent_expr (wrapped_string_lit));
28389 }
28390
28391 /* Verify that type_dependent_expression_p () works correctly, even
28392 in the presence of location wrapper nodes. */
28393
28394 static void
28395 test_type_dependent_expression_p ()
28396 {
28397 location_t loc = BUILTINS_LOCATION;
28398
28399 tree name = get_identifier ("foo");
28400
28401 /* If no templates are involved, nothing is type-dependent. */
28402 gcc_assert (!processing_template_decl);
28403 ASSERT_FALSE (type_dependent_expression_p (name));
28404
28405 ++processing_template_decl;
28406
28407 /* Within a template, an unresolved name is always type-dependent. */
28408 ASSERT_TRUE (type_dependent_expression_p (name));
28409
28410 /* Ensure it copes with NULL_TREE and errors. */
28411 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
28412 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
28413
28414 /* A USING_DECL in a template should be type-dependent, even if wrapped
28415 with a location wrapper (PR c++/83799). */
28416 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
28417 TREE_TYPE (using_decl) = integer_type_node;
28418 ASSERT_TRUE (type_dependent_expression_p (using_decl));
28419 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
28420 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
28421 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
28422
28423 --processing_template_decl;
28424 }
28425
28426 /* Run all of the selftests within this file. */
28427
28428 void
28429 cp_pt_c_tests ()
28430 {
28431 test_build_non_dependent_expr ();
28432 test_type_dependent_expression_p ();
28433 }
28434
28435 } // namespace selftest
28436
28437 #endif /* #if CHECKING_P */
28438
28439 #include "gt-cp-pt.h"