c++: excessive instantiation during CTAD [PR101174]
[official-gcc.git] / gcc / cp / pt.c
blob5c55507203a3fdccb6a8cc971683d7912050f20c
1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2021 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
6 This file is part of GCC.
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.
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.
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/>. */
22 /* Known bugs or deficiencies include:
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".
27 Fixed by: C++20 modules. */
29 #include "config.h"
30 #include "system.h"
31 #include "coretypes.h"
32 #include "cp-tree.h"
33 #include "timevar.h"
34 #include "stringpool.h"
35 #include "varasm.h"
36 #include "attribs.h"
37 #include "stor-layout.h"
38 #include "intl.h"
39 #include "c-family/c-objc.h"
40 #include "cp-objcp-common.h"
41 #include "toplev.h"
42 #include "tree-iterator.h"
43 #include "type-utils.h"
44 #include "gimplify.h"
45 #include "gcc-rich-location.h"
46 #include "selftest.h"
47 #include "target.h"
49 /* The type of functions taking a tree, and some additional data, and
50 returning an int. */
51 typedef int (*tree_fn_t) (tree, void*);
53 /* The PENDING_TEMPLATES is a list of templates whose instantiations
54 have been deferred, either because their definitions were not yet
55 available, or because we were putting off doing the work. */
56 struct GTY ((chain_next ("%h.next"))) pending_template
58 struct pending_template *next;
59 struct tinst_level *tinst;
62 static GTY(()) struct pending_template *pending_templates;
63 static GTY(()) struct pending_template *last_pending_template;
65 int processing_template_parmlist;
66 static int template_header_count;
68 static vec<int> inline_parm_levels;
70 static GTY(()) struct tinst_level *current_tinst_level;
72 static GTY(()) vec<tree, va_gc> *saved_access_scope;
74 /* Live only within one (recursive) call to tsubst_expr. We use
75 this to pass the statement expression node from the STMT_EXPR
76 to the EXPR_STMT that is its result. */
77 static tree cur_stmt_expr;
79 // -------------------------------------------------------------------------- //
80 // Local Specialization Stack
82 // Implementation of the RAII helper for creating new local
83 // specializations.
84 local_specialization_stack::local_specialization_stack (lss_policy policy)
85 : saved (local_specializations)
87 if (policy == lss_nop)
89 else if (policy == lss_blank || !saved)
90 local_specializations = new hash_map<tree, tree>;
91 else
92 local_specializations = new hash_map<tree, tree>(*saved);
95 local_specialization_stack::~local_specialization_stack ()
97 if (local_specializations != saved)
99 delete local_specializations;
100 local_specializations = saved;
104 /* True if we've recursed into fn_type_unification too many times. */
105 static bool excessive_deduction_depth;
107 struct spec_hasher : ggc_ptr_hash<spec_entry>
109 static hashval_t hash (spec_entry *);
110 static bool equal (spec_entry *, spec_entry *);
113 /* The general template is not in these tables. */
114 typedef hash_table<spec_hasher> spec_hash_table;
115 static GTY (()) spec_hash_table *decl_specializations;
116 static GTY (()) spec_hash_table *type_specializations;
118 /* Contains canonical template parameter types. The vector is indexed by
119 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
120 TREE_LIST, whose TREE_VALUEs contain the canonical template
121 parameters of various types and levels. */
122 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
124 #define UNIFY_ALLOW_NONE 0
125 #define UNIFY_ALLOW_MORE_CV_QUAL 1
126 #define UNIFY_ALLOW_LESS_CV_QUAL 2
127 #define UNIFY_ALLOW_DERIVED 4
128 #define UNIFY_ALLOW_INTEGER 8
129 #define UNIFY_ALLOW_OUTER_LEVEL 16
130 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
131 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
133 enum template_base_result {
134 tbr_incomplete_type,
135 tbr_ambiguous_baseclass,
136 tbr_success
139 static bool resolve_overloaded_unification (tree, tree, tree, tree,
140 unification_kind_t, int,
141 bool);
142 static int try_one_overload (tree, tree, tree, tree, tree,
143 unification_kind_t, int, bool, bool);
144 static int unify (tree, tree, tree, tree, int, bool);
145 static void add_pending_template (tree);
146 static tree reopen_tinst_level (struct tinst_level *);
147 static tree tsubst_initializer_list (tree, tree);
148 static tree get_partial_spec_bindings (tree, tree, tree);
149 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
150 bool, bool);
151 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
152 bool, bool);
153 static void tsubst_enum (tree, tree, tree);
154 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
155 static int check_non_deducible_conversion (tree, tree, int, int,
156 struct conversion **, bool);
157 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
158 tree);
159 static int type_unification_real (tree, tree, tree, const tree *,
160 unsigned int, int, unification_kind_t,
161 vec<deferred_access_check, va_gc> **,
162 bool);
163 static void note_template_header (int);
164 static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
165 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
166 static tree convert_template_argument (tree, tree, tree,
167 tsubst_flags_t, int, tree);
168 static tree for_each_template_parm (tree, tree_fn_t, void*,
169 hash_set<tree> *, bool, tree_fn_t = NULL);
170 static tree expand_template_argument_pack (tree);
171 static tree build_template_parm_index (int, int, int, tree, tree);
172 static bool inline_needs_template_parms (tree, bool);
173 static void push_inline_template_parms_recursive (tree, int);
174 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
175 static int mark_template_parm (tree, void *);
176 static int template_parm_this_level_p (tree, void *);
177 static tree tsubst_friend_function (tree, tree);
178 static tree tsubst_friend_class (tree, tree);
179 static int can_complete_type_without_circularity (tree);
180 static tree get_bindings (tree, tree, tree, bool);
181 static int template_decl_level (tree);
182 static int check_cv_quals_for_unify (int, tree, tree);
183 static int unify_pack_expansion (tree, tree, tree,
184 tree, unification_kind_t, bool, bool);
185 static tree copy_template_args (tree);
186 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
187 tree most_specialized_partial_spec (tree, tsubst_flags_t);
188 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
189 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
190 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
191 static bool check_specialization_scope (void);
192 static tree process_partial_specialization (tree);
193 static enum template_base_result get_template_base (tree, tree, tree, tree,
194 bool , tree *);
195 static tree try_class_unification (tree, tree, tree, tree, bool);
196 static bool class_nttp_const_wrapper_p (tree t);
197 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
198 tree, tree);
199 static bool template_template_parm_bindings_ok_p (tree, tree);
200 static void tsubst_default_arguments (tree, tsubst_flags_t);
201 static tree for_each_template_parm_r (tree *, int *, void *);
202 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
203 static void copy_default_args_to_explicit_spec (tree);
204 static bool invalid_nontype_parm_type_p (tree, tsubst_flags_t);
205 static bool dependent_template_arg_p (tree);
206 static bool any_template_arguments_need_structural_equality_p (tree);
207 static bool dependent_type_p_r (tree);
208 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
209 static tree tsubst_decl (tree, tree, tsubst_flags_t);
210 static void perform_instantiation_time_access_checks (tree, tree);
211 static tree listify (tree);
212 static tree listify_autos (tree, tree);
213 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
214 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
215 static bool complex_alias_template_p (const_tree tmpl);
216 static tree get_underlying_template (tree);
217 static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
218 static tree canonicalize_expr_argument (tree, tsubst_flags_t);
219 static tree make_argument_pack (tree);
220 static void register_parameter_specializations (tree, tree);
221 static tree enclosing_instantiation_of (tree tctx);
222 static void instantiate_body (tree pattern, tree args, tree d, bool nested);
224 /* Make the current scope suitable for access checking when we are
225 processing T. T can be FUNCTION_DECL for instantiated function
226 template, VAR_DECL for static member variable, or TYPE_DECL for
227 alias template (needed by instantiate_decl). */
229 void
230 push_access_scope (tree t)
232 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
233 || TREE_CODE (t) == TYPE_DECL);
235 if (DECL_FRIEND_CONTEXT (t))
236 push_nested_class (DECL_FRIEND_CONTEXT (t));
237 else if (DECL_CLASS_SCOPE_P (t))
238 push_nested_class (DECL_CONTEXT (t));
239 else if (deduction_guide_p (t) && DECL_ARTIFICIAL (t))
240 /* An artificial deduction guide should have the same access as
241 the constructor. */
242 push_nested_class (TREE_TYPE (TREE_TYPE (t)));
243 else
244 push_to_top_level ();
246 if (TREE_CODE (t) == FUNCTION_DECL)
248 vec_safe_push (saved_access_scope, current_function_decl);
249 current_function_decl = t;
253 /* Restore the scope set up by push_access_scope. T is the node we
254 are processing. */
256 void
257 pop_access_scope (tree t)
259 if (TREE_CODE (t) == FUNCTION_DECL)
260 current_function_decl = saved_access_scope->pop();
262 if (DECL_FRIEND_CONTEXT (t)
263 || DECL_CLASS_SCOPE_P (t)
264 || (deduction_guide_p (t) && DECL_ARTIFICIAL (t)))
265 pop_nested_class ();
266 else
267 pop_from_top_level ();
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. */
275 tree
276 finish_member_template_decl (tree decl)
278 if (decl == error_mark_node)
279 return error_mark_node;
281 gcc_assert (DECL_P (decl));
283 if (TREE_CODE (decl) == TYPE_DECL)
285 tree type;
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))
294 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
295 check_member_template (tmpl);
296 return tmpl;
298 return NULL_TREE;
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))
305 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
307 check_member_template (DECL_TI_TEMPLATE (decl));
308 return DECL_TI_TEMPLATE (decl);
310 else
311 return decl;
313 else
314 error_at (DECL_SOURCE_LOCATION (decl),
315 "invalid member template declaration %qD", decl);
317 return error_mark_node;
320 /* Create a template info node. */
322 tree
323 build_template_info (tree template_decl, tree template_args)
325 tree result = make_node (TEMPLATE_INFO);
326 TI_TEMPLATE (result) = template_decl;
327 TI_ARGS (result) = template_args;
328 return result;
331 /* Return the template info node corresponding to T, whatever T is. */
333 tree
334 get_template_info (const_tree t)
336 tree tinfo = NULL_TREE;
338 if (!t || t == error_mark_node)
339 return NULL;
341 if (TREE_CODE (t) == NAMESPACE_DECL
342 || TREE_CODE (t) == PARM_DECL)
343 return NULL;
345 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
346 tinfo = DECL_TEMPLATE_INFO (t);
348 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
349 t = TREE_TYPE (t);
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);
356 return tinfo;
359 /* Returns the template nesting level of the indicated class TYPE.
361 For example, in:
362 template <class T>
363 struct A
365 template <class U>
366 struct B {};
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.
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. */
378 template_class_depth (tree type)
380 int depth;
382 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
384 tree tinfo = get_template_info (type);
386 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
387 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
388 ++depth;
390 if (DECL_P (type))
392 if (tree fctx = DECL_FRIEND_CONTEXT (type))
393 type = fctx;
394 else
395 type = CP_DECL_CONTEXT (type);
397 else if (LAMBDA_TYPE_P (type) && LAMBDA_TYPE_EXTRA_SCOPE (type))
398 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
399 else
400 type = CP_TYPE_CONTEXT (type);
403 return depth;
406 /* Return TRUE if NODE instantiates a template that has arguments of
407 its own, be it directly a primary template or indirectly through a
408 partial specializations. */
409 static bool
410 instantiates_primary_template_p (tree node)
412 tree tinfo = get_template_info (node);
413 if (!tinfo)
414 return false;
416 tree tmpl = TI_TEMPLATE (tinfo);
417 if (PRIMARY_TEMPLATE_P (tmpl))
418 return true;
420 if (!DECL_TEMPLATE_SPECIALIZATION (tmpl))
421 return false;
423 /* So now we know we have a specialization, but it could be a full
424 or a partial specialization. To tell which, compare the depth of
425 its template arguments with those of its context. */
427 tree ctxt = DECL_CONTEXT (tmpl);
428 tree ctinfo = get_template_info (ctxt);
429 if (!ctinfo)
430 return true;
432 return (TMPL_ARGS_DEPTH (TI_ARGS (tinfo))
433 > TMPL_ARGS_DEPTH (TI_ARGS (ctinfo)));
436 /* Subroutine of maybe_begin_member_template_processing.
437 Returns true if processing DECL needs us to push template parms. */
439 static bool
440 inline_needs_template_parms (tree decl, bool nsdmi)
442 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
443 return false;
445 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
446 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
449 /* Subroutine of maybe_begin_member_template_processing.
450 Push the template parms in PARMS, starting from LEVELS steps into the
451 chain, and ending at the beginning, since template parms are listed
452 innermost first. */
454 static void
455 push_inline_template_parms_recursive (tree parmlist, int levels)
457 tree parms = TREE_VALUE (parmlist);
458 int i;
460 if (levels > 1)
461 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
463 ++processing_template_decl;
464 current_template_parms
465 = tree_cons (size_int (processing_template_decl),
466 parms, current_template_parms);
467 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
469 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
470 NULL);
471 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
473 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
475 if (error_operand_p (parm))
476 continue;
478 gcc_assert (DECL_P (parm));
480 switch (TREE_CODE (parm))
482 case TYPE_DECL:
483 case TEMPLATE_DECL:
484 pushdecl (parm);
485 break;
487 case PARM_DECL:
488 /* Push the CONST_DECL. */
489 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
490 break;
492 default:
493 gcc_unreachable ();
498 /* Restore the template parameter context for a member template, a
499 friend template defined in a class definition, or a non-template
500 member of template class. */
502 void
503 maybe_begin_member_template_processing (tree decl)
505 tree parms;
506 int levels = 0;
507 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
509 if (nsdmi)
511 tree ctx = DECL_CONTEXT (decl);
512 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
513 /* Disregard full specializations (c++/60999). */
514 && uses_template_parms (ctx)
515 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
518 if (inline_needs_template_parms (decl, nsdmi))
520 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
521 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
523 if (DECL_TEMPLATE_SPECIALIZATION (decl))
525 --levels;
526 parms = TREE_CHAIN (parms);
529 push_inline_template_parms_recursive (parms, levels);
532 /* Remember how many levels of template parameters we pushed so that
533 we can pop them later. */
534 inline_parm_levels.safe_push (levels);
537 /* Undo the effects of maybe_begin_member_template_processing. */
539 void
540 maybe_end_member_template_processing (void)
542 int i;
543 int last;
545 if (inline_parm_levels.length () == 0)
546 return;
548 last = inline_parm_levels.pop ();
549 for (i = 0; i < last; ++i)
551 --processing_template_decl;
552 current_template_parms = TREE_CHAIN (current_template_parms);
553 poplevel (0, 0, 0);
557 /* Return a new template argument vector which contains all of ARGS,
558 but has as its innermost set of arguments the EXTRA_ARGS. */
560 tree
561 add_to_template_args (tree args, tree extra_args)
563 tree new_args;
564 int extra_depth;
565 int i;
566 int j;
568 if (args == NULL_TREE || extra_args == error_mark_node)
569 return extra_args;
571 extra_depth = TMPL_ARGS_DEPTH (extra_args);
572 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
574 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
575 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
577 for (j = 1; j <= extra_depth; ++j, ++i)
578 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
580 return new_args;
583 /* Like add_to_template_args, but only the outermost ARGS are added to
584 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
585 (EXTRA_ARGS) levels are added. This function is used to combine
586 the template arguments from a partial instantiation with the
587 template arguments used to attain the full instantiation from the
588 partial instantiation.
590 If ARGS is a TEMPLATE_DECL, use its parameters as args. */
592 tree
593 add_outermost_template_args (tree args, tree extra_args)
595 tree new_args;
597 if (!args)
598 return extra_args;
599 if (TREE_CODE (args) == TEMPLATE_DECL)
601 tree ti = get_template_info (DECL_TEMPLATE_RESULT (args));
602 args = TI_ARGS (ti);
605 /* If there are more levels of EXTRA_ARGS than there are ARGS,
606 something very fishy is going on. */
607 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
609 /* If *all* the new arguments will be the EXTRA_ARGS, just return
610 them. */
611 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
612 return extra_args;
614 /* For the moment, we make ARGS look like it contains fewer levels. */
615 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
617 new_args = add_to_template_args (args, extra_args);
619 /* Now, we restore ARGS to its full dimensions. */
620 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
622 return new_args;
625 /* Return the N levels of innermost template arguments from the ARGS. */
627 tree
628 get_innermost_template_args (tree args, int n)
630 tree new_args;
631 int extra_levels;
632 int i;
634 gcc_assert (n >= 0);
636 /* If N is 1, just return the innermost set of template arguments. */
637 if (n == 1)
638 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
640 /* If we're not removing anything, just return the arguments we were
641 given. */
642 extra_levels = TMPL_ARGS_DEPTH (args) - n;
643 gcc_assert (extra_levels >= 0);
644 if (extra_levels == 0)
645 return args;
647 /* Make a new set of arguments, not containing the outer arguments. */
648 new_args = make_tree_vec (n);
649 for (i = 1; i <= n; ++i)
650 SET_TMPL_ARGS_LEVEL (new_args, i,
651 TMPL_ARGS_LEVEL (args, i + extra_levels));
653 return new_args;
656 /* The inverse of get_innermost_template_args: Return all but the innermost
657 EXTRA_LEVELS levels of template arguments from the ARGS. */
659 static tree
660 strip_innermost_template_args (tree args, int extra_levels)
662 tree new_args;
663 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
664 int i;
666 gcc_assert (n >= 0);
668 /* If N is 1, just return the outermost set of template arguments. */
669 if (n == 1)
670 return TMPL_ARGS_LEVEL (args, 1);
672 /* If we're not removing anything, just return the arguments we were
673 given. */
674 gcc_assert (extra_levels >= 0);
675 if (extra_levels == 0)
676 return args;
678 /* Make a new set of arguments, not containing the inner arguments. */
679 new_args = make_tree_vec (n);
680 for (i = 1; i <= n; ++i)
681 SET_TMPL_ARGS_LEVEL (new_args, i,
682 TMPL_ARGS_LEVEL (args, i));
684 return new_args;
687 /* We've got a template header coming up; push to a new level for storing
688 the parms. */
690 void
691 begin_template_parm_list (void)
693 /* We use a non-tag-transparent scope here, which causes pushtag to
694 put tags in this scope, rather than in the enclosing class or
695 namespace scope. This is the right thing, since we want
696 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
697 global template class, push_template_decl handles putting the
698 TEMPLATE_DECL into top-level scope. For a nested template class,
699 e.g.:
701 template <class T> struct S1 {
702 template <class T> struct S2 {};
705 pushtag contains special code to insert the TEMPLATE_DECL for S2
706 at the right scope. */
707 begin_scope (sk_template_parms, NULL);
708 ++processing_template_decl;
709 ++processing_template_parmlist;
710 note_template_header (0);
712 /* Add a dummy parameter level while we process the parameter list. */
713 current_template_parms
714 = tree_cons (size_int (processing_template_decl),
715 make_tree_vec (0),
716 current_template_parms);
719 /* This routine is called when a specialization is declared. If it is
720 invalid to declare a specialization here, an error is reported and
721 false is returned, otherwise this routine will return true. */
723 static bool
724 check_specialization_scope (void)
726 tree scope = current_scope ();
728 /* [temp.expl.spec]
730 An explicit specialization shall be declared in the namespace of
731 which the template is a member, or, for member templates, in the
732 namespace of which the enclosing class or enclosing class
733 template is a member. An explicit specialization of a member
734 function, member class or static data member of a class template
735 shall be declared in the namespace of which the class template
736 is a member. */
737 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
739 error ("explicit specialization in non-namespace scope %qD", scope);
740 return false;
743 /* [temp.expl.spec]
745 In an explicit specialization declaration for a member of a class
746 template or a member template that appears in namespace scope,
747 the member template and some of its enclosing class templates may
748 remain unspecialized, except that the declaration shall not
749 explicitly specialize a class member template if its enclosing
750 class templates are not explicitly specialized as well. */
751 if (current_template_parms)
753 error ("enclosing class templates are not explicitly specialized");
754 return false;
757 return true;
760 /* We've just seen template <>. */
762 bool
763 begin_specialization (void)
765 begin_scope (sk_template_spec, NULL);
766 note_template_header (1);
767 return check_specialization_scope ();
770 /* Called at then end of processing a declaration preceded by
771 template<>. */
773 void
774 end_specialization (void)
776 finish_scope ();
777 reset_specialization ();
780 /* Any template <>'s that we have seen thus far are not referring to a
781 function specialization. */
783 void
784 reset_specialization (void)
786 processing_specialization = 0;
787 template_header_count = 0;
790 /* We've just seen a template header. If SPECIALIZATION is nonzero,
791 it was of the form template <>. */
793 static void
794 note_template_header (int specialization)
796 processing_specialization = specialization;
797 template_header_count++;
800 /* We're beginning an explicit instantiation. */
802 void
803 begin_explicit_instantiation (void)
805 gcc_assert (!processing_explicit_instantiation);
806 processing_explicit_instantiation = true;
810 void
811 end_explicit_instantiation (void)
813 gcc_assert (processing_explicit_instantiation);
814 processing_explicit_instantiation = false;
817 /* An explicit specialization or partial specialization of TMPL is being
818 declared. Check that the namespace in which the specialization is
819 occurring is permissible. Returns false iff it is invalid to
820 specialize TMPL in the current namespace. */
822 static bool
823 check_specialization_namespace (tree tmpl)
825 tree tpl_ns = decl_namespace_context (tmpl);
827 /* [tmpl.expl.spec]
829 An explicit specialization shall be declared in a namespace enclosing the
830 specialized template. An explicit specialization whose declarator-id is
831 not qualified shall be declared in the nearest enclosing namespace of the
832 template, or, if the namespace is inline (7.3.1), any namespace from its
833 enclosing namespace set. */
834 if (current_scope() != DECL_CONTEXT (tmpl)
835 && !at_namespace_scope_p ())
837 error ("specialization of %qD must appear at namespace scope", tmpl);
838 return false;
841 if (is_nested_namespace (current_namespace, tpl_ns, cxx_dialect < cxx11))
842 /* Same or enclosing namespace. */
843 return true;
844 else
846 auto_diagnostic_group d;
847 if (permerror (input_location,
848 "specialization of %qD in different namespace", tmpl))
849 inform (DECL_SOURCE_LOCATION (tmpl),
850 " from definition of %q#D", tmpl);
851 return false;
855 /* SPEC is an explicit instantiation. Check that it is valid to
856 perform this explicit instantiation in the current namespace. */
858 static void
859 check_explicit_instantiation_namespace (tree spec)
861 tree ns;
863 /* DR 275: An explicit instantiation shall appear in an enclosing
864 namespace of its template. */
865 ns = decl_namespace_context (spec);
866 if (!is_nested_namespace (current_namespace, ns))
867 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
868 "(which does not enclose namespace %qD)",
869 spec, current_namespace, ns);
872 /* Returns the type of a template specialization only if that
873 specialization needs to be defined. Otherwise (e.g., if the type has
874 already been defined), the function returns NULL_TREE. */
876 static tree
877 maybe_new_partial_specialization (tree type)
879 /* An implicit instantiation of an incomplete type implies
880 the definition of a new class template.
882 template<typename T>
883 struct S;
885 template<typename T>
886 struct S<T*>;
888 Here, S<T*> is an implicit instantiation of S whose type
889 is incomplete. */
890 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
891 return type;
893 /* It can also be the case that TYPE is a completed specialization.
894 Continuing the previous example, suppose we also declare:
896 template<typename T>
897 requires Integral<T>
898 struct S<T*>;
900 Here, S<T*> refers to the specialization S<T*> defined
901 above. However, we need to differentiate definitions because
902 we intend to define a new partial specialization. In this case,
903 we rely on the fact that the constraints are different for
904 this declaration than that above.
906 Note that we also get here for injected class names and
907 late-parsed template definitions. We must ensure that we
908 do not create new type declarations for those cases. */
909 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
911 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
912 tree args = CLASSTYPE_TI_ARGS (type);
914 /* If there are no template parameters, this cannot be a new
915 partial template specialization? */
916 if (!current_template_parms)
917 return NULL_TREE;
919 /* The injected-class-name is not a new partial specialization. */
920 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
921 return NULL_TREE;
923 /* If the constraints are not the same as those of the primary
924 then, we can probably create a new specialization. */
925 tree type_constr = current_template_constraints ();
927 if (type == TREE_TYPE (tmpl))
929 tree main_constr = get_constraints (tmpl);
930 if (equivalent_constraints (type_constr, main_constr))
931 return NULL_TREE;
934 /* Also, if there's a pre-existing specialization with matching
935 constraints, then this also isn't new. */
936 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
937 while (specs)
939 tree spec_tmpl = TREE_VALUE (specs);
940 tree spec_args = TREE_PURPOSE (specs);
941 tree spec_constr = get_constraints (spec_tmpl);
942 if (comp_template_args (args, spec_args)
943 && equivalent_constraints (type_constr, spec_constr))
944 return NULL_TREE;
945 specs = TREE_CHAIN (specs);
948 /* Create a new type node (and corresponding type decl)
949 for the newly declared specialization. */
950 tree t = make_class_type (TREE_CODE (type));
951 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
952 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
954 /* We only need a separate type node for storing the definition of this
955 partial specialization; uses of S<T*> are unconstrained, so all are
956 equivalent. So keep TYPE_CANONICAL the same. */
957 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
959 /* Build the corresponding type decl. */
960 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
961 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
962 DECL_SOURCE_LOCATION (d) = input_location;
963 TREE_PRIVATE (d) = (current_access_specifier == access_private_node);
964 TREE_PROTECTED (d) = (current_access_specifier == access_protected_node);
966 set_instantiating_module (d);
967 DECL_MODULE_EXPORT_P (d) = DECL_MODULE_EXPORT_P (tmpl);
969 return t;
972 return NULL_TREE;
975 /* The TYPE is being declared. If it is a template type, that means it
976 is a partial specialization. Do appropriate error-checking. */
978 tree
979 maybe_process_partial_specialization (tree type)
981 tree context;
983 if (type == error_mark_node)
984 return error_mark_node;
986 /* A lambda that appears in specialization context is not itself a
987 specialization. */
988 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
989 return type;
991 /* An injected-class-name is not a specialization. */
992 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
993 return type;
995 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
997 error ("name of class shadows template template parameter %qD",
998 TYPE_NAME (type));
999 return error_mark_node;
1002 context = TYPE_CONTEXT (type);
1004 if (TYPE_ALIAS_P (type))
1006 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
1008 if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
1009 error ("specialization of alias template %qD",
1010 TI_TEMPLATE (tinfo));
1011 else
1012 error ("explicit specialization of non-template %qT", type);
1013 return error_mark_node;
1015 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
1017 /* This is for ordinary explicit specialization and partial
1018 specialization of a template class such as:
1020 template <> class C<int>;
1024 template <class T> class C<T*>;
1026 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
1028 if (tree t = maybe_new_partial_specialization (type))
1030 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
1031 && !at_namespace_scope_p ())
1032 return error_mark_node;
1033 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
1034 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
1035 if (processing_template_decl)
1037 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
1038 if (decl == error_mark_node)
1039 return error_mark_node;
1040 return TREE_TYPE (decl);
1043 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
1044 error ("specialization of %qT after instantiation", type);
1045 else if (errorcount && !processing_specialization
1046 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
1047 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
1048 /* Trying to define a specialization either without a template<> header
1049 or in an inappropriate place. We've already given an error, so just
1050 bail now so we don't actually define the specialization. */
1051 return error_mark_node;
1053 else if (CLASS_TYPE_P (type)
1054 && !CLASSTYPE_USE_TEMPLATE (type)
1055 && CLASSTYPE_TEMPLATE_INFO (type)
1056 && context && CLASS_TYPE_P (context)
1057 && CLASSTYPE_TEMPLATE_INFO (context))
1059 /* This is for an explicit specialization of member class
1060 template according to [temp.expl.spec/18]:
1062 template <> template <class U> class C<int>::D;
1064 The context `C<int>' must be an implicit instantiation.
1065 Otherwise this is just a member class template declared
1066 earlier like:
1068 template <> class C<int> { template <class U> class D; };
1069 template <> template <class U> class C<int>::D;
1071 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1072 while in the second case, `C<int>::D' is a primary template
1073 and `C<T>::D' may not exist. */
1075 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1076 && !COMPLETE_TYPE_P (type))
1078 tree t;
1079 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1081 if (current_namespace
1082 != decl_namespace_context (tmpl))
1084 if (permerror (input_location,
1085 "specialization of %qD in different namespace",
1086 type))
1087 inform (DECL_SOURCE_LOCATION (tmpl),
1088 "from definition of %q#D", tmpl);
1091 /* Check for invalid specialization after instantiation:
1093 template <> template <> class C<int>::D<int>;
1094 template <> template <class U> class C<int>::D; */
1096 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1097 t; t = TREE_CHAIN (t))
1099 tree inst = TREE_VALUE (t);
1100 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1101 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1103 /* We already have a full specialization of this partial
1104 instantiation, or a full specialization has been
1105 looked up but not instantiated. Reassign it to the
1106 new member specialization template. */
1107 spec_entry elt;
1108 spec_entry *entry;
1110 elt.tmpl = most_general_template (tmpl);
1111 elt.args = CLASSTYPE_TI_ARGS (inst);
1112 elt.spec = inst;
1114 type_specializations->remove_elt (&elt);
1116 elt.tmpl = tmpl;
1117 CLASSTYPE_TI_ARGS (inst)
1118 = elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1120 spec_entry **slot
1121 = type_specializations->find_slot (&elt, INSERT);
1122 entry = ggc_alloc<spec_entry> ();
1123 *entry = elt;
1124 *slot = entry;
1126 else
1127 /* But if we've had an implicit instantiation, that's a
1128 problem ([temp.expl.spec]/6). */
1129 error ("specialization %qT after instantiation %qT",
1130 type, inst);
1133 /* Mark TYPE as a specialization. And as a result, we only
1134 have one level of template argument for the innermost
1135 class template. */
1136 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1137 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1138 CLASSTYPE_TI_ARGS (type)
1139 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1142 else if (processing_specialization)
1144 /* Someday C++0x may allow for enum template specialization. */
1145 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1146 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1147 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1148 "of %qD not allowed by ISO C++", type);
1149 else
1151 error ("explicit specialization of non-template %qT", type);
1152 return error_mark_node;
1156 return type;
1159 /* Returns nonzero if we can optimize the retrieval of specializations
1160 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1161 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1163 static inline bool
1164 optimize_specialization_lookup_p (tree tmpl)
1166 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1167 && DECL_CLASS_SCOPE_P (tmpl)
1168 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1169 parameter. */
1170 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1171 /* The optimized lookup depends on the fact that the
1172 template arguments for the member function template apply
1173 purely to the containing class, which is not true if the
1174 containing class is an explicit or partial
1175 specialization. */
1176 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1177 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1178 && !DECL_CONV_FN_P (tmpl)
1179 /* It is possible to have a template that is not a member
1180 template and is not a member of a template class:
1182 template <typename T>
1183 struct S { friend A::f(); };
1185 Here, the friend function is a template, but the context does
1186 not have template information. The optimized lookup relies
1187 on having ARGS be the template arguments for both the class
1188 and the function template. */
1189 && !DECL_UNIQUE_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1192 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1193 gone through coerce_template_parms by now. */
1195 static void
1196 verify_unstripped_args_1 (tree inner)
1198 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1200 tree arg = TREE_VEC_ELT (inner, i);
1201 if (TREE_CODE (arg) == TEMPLATE_DECL)
1202 /* OK */;
1203 else if (TYPE_P (arg))
1204 gcc_assert (strip_typedefs (arg, NULL) == arg);
1205 else if (ARGUMENT_PACK_P (arg))
1206 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg));
1207 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1208 /* Allow typedefs on the type of a non-type argument, since a
1209 parameter can have them. */;
1210 else
1211 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1215 static void
1216 verify_unstripped_args (tree args)
1218 ++processing_template_decl;
1219 if (!any_dependent_template_arguments_p (args))
1220 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args));
1221 --processing_template_decl;
1224 /* Retrieve the specialization (in the sense of [temp.spec] - a
1225 specialization is either an instantiation or an explicit
1226 specialization) of TMPL for the given template ARGS. If there is
1227 no such specialization, return NULL_TREE. The ARGS are a vector of
1228 arguments, or a vector of vectors of arguments, in the case of
1229 templates with more than one level of parameters.
1231 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1232 then we search for a partial specialization matching ARGS. This
1233 parameter is ignored if TMPL is not a class template.
1235 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1236 result is a NONTYPE_ARGUMENT_PACK. */
1238 static tree
1239 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1241 if (tmpl == NULL_TREE)
1242 return NULL_TREE;
1244 if (args == error_mark_node)
1245 return NULL_TREE;
1247 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1248 || TREE_CODE (tmpl) == FIELD_DECL);
1250 /* There should be as many levels of arguments as there are
1251 levels of parameters. */
1252 gcc_assert (TMPL_ARGS_DEPTH (args)
1253 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1254 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1255 : template_class_depth (DECL_CONTEXT (tmpl))));
1257 if (flag_checking)
1258 verify_unstripped_args (args);
1260 /* Lambda functions in templates aren't instantiated normally, but through
1261 tsubst_lambda_expr. */
1262 if (lambda_fn_in_template_p (tmpl))
1263 return NULL_TREE;
1265 if (optimize_specialization_lookup_p (tmpl))
1267 /* The template arguments actually apply to the containing
1268 class. Find the class specialization with those
1269 arguments. */
1270 tree class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1271 tree class_specialization
1272 = retrieve_specialization (class_template, args, 0);
1273 if (!class_specialization)
1274 return NULL_TREE;
1276 /* Find the instance of TMPL. */
1277 tree fns = get_class_binding (class_specialization, DECL_NAME (tmpl));
1278 for (ovl_iterator iter (fns); iter; ++iter)
1280 tree fn = *iter;
1281 if (tree ti = get_template_info (fn))
1282 if (TI_TEMPLATE (ti) == tmpl
1283 /* using-declarations can bring in a different
1284 instantiation of tmpl as a member of a different
1285 instantiation of tmpl's class. We don't want those
1286 here. */
1287 && DECL_CONTEXT (fn) == class_specialization)
1288 return fn;
1290 return NULL_TREE;
1292 else
1294 spec_entry *found;
1295 spec_entry elt;
1296 spec_hash_table *specializations;
1298 elt.tmpl = tmpl;
1299 elt.args = args;
1300 elt.spec = NULL_TREE;
1302 if (DECL_CLASS_TEMPLATE_P (tmpl))
1303 specializations = type_specializations;
1304 else
1305 specializations = decl_specializations;
1307 if (hash == 0)
1308 hash = spec_hasher::hash (&elt);
1309 found = specializations->find_with_hash (&elt, hash);
1310 if (found)
1311 return found->spec;
1314 return NULL_TREE;
1317 /* Like retrieve_specialization, but for local declarations. */
1319 tree
1320 retrieve_local_specialization (tree tmpl)
1322 if (local_specializations == NULL)
1323 return NULL_TREE;
1325 tree *slot = local_specializations->get (tmpl);
1326 return slot ? *slot : NULL_TREE;
1329 /* Returns nonzero iff DECL is a specialization of TMPL. */
1332 is_specialization_of (tree decl, tree tmpl)
1334 tree t;
1336 if (TREE_CODE (decl) == FUNCTION_DECL)
1338 for (t = decl;
1339 t != NULL_TREE;
1340 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1341 if (t == tmpl)
1342 return 1;
1344 else
1346 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1348 for (t = TREE_TYPE (decl);
1349 t != NULL_TREE;
1350 t = CLASSTYPE_USE_TEMPLATE (t)
1351 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1352 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1353 return 1;
1356 return 0;
1359 /* Returns nonzero iff DECL is a specialization of friend declaration
1360 FRIEND_DECL according to [temp.friend]. */
1362 bool
1363 is_specialization_of_friend (tree decl, tree friend_decl)
1365 bool need_template = true;
1366 int template_depth;
1368 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1369 || TREE_CODE (decl) == TYPE_DECL);
1371 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1372 of a template class, we want to check if DECL is a specialization
1373 if this. */
1374 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1375 && DECL_TEMPLATE_INFO (friend_decl)
1376 && !DECL_USE_TEMPLATE (friend_decl))
1378 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1379 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1380 need_template = false;
1382 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1383 && !PRIMARY_TEMPLATE_P (friend_decl))
1384 need_template = false;
1386 /* There is nothing to do if this is not a template friend. */
1387 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1388 return false;
1390 if (is_specialization_of (decl, friend_decl))
1391 return true;
1393 /* [temp.friend/6]
1394 A member of a class template may be declared to be a friend of a
1395 non-template class. In this case, the corresponding member of
1396 every specialization of the class template is a friend of the
1397 class granting friendship.
1399 For example, given a template friend declaration
1401 template <class T> friend void A<T>::f();
1403 the member function below is considered a friend
1405 template <> struct A<int> {
1406 void f();
1409 For this type of template friend, TEMPLATE_DEPTH below will be
1410 nonzero. To determine if DECL is a friend of FRIEND, we first
1411 check if the enclosing class is a specialization of another. */
1413 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1414 if (template_depth
1415 && DECL_CLASS_SCOPE_P (decl)
1416 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1417 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1419 /* Next, we check the members themselves. In order to handle
1420 a few tricky cases, such as when FRIEND_DECL's are
1422 template <class T> friend void A<T>::g(T t);
1423 template <class T> template <T t> friend void A<T>::h();
1425 and DECL's are
1427 void A<int>::g(int);
1428 template <int> void A<int>::h();
1430 we need to figure out ARGS, the template arguments from
1431 the context of DECL. This is required for template substitution
1432 of `T' in the function parameter of `g' and template parameter
1433 of `h' in the above examples. Here ARGS corresponds to `int'. */
1435 tree context = DECL_CONTEXT (decl);
1436 tree args = NULL_TREE;
1437 int current_depth = 0;
1439 while (current_depth < template_depth)
1441 if (CLASSTYPE_TEMPLATE_INFO (context))
1443 if (current_depth == 0)
1444 args = TYPE_TI_ARGS (context);
1445 else
1446 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1447 current_depth++;
1449 context = TYPE_CONTEXT (context);
1452 if (TREE_CODE (decl) == FUNCTION_DECL)
1454 bool is_template;
1455 tree friend_type;
1456 tree decl_type;
1457 tree friend_args_type;
1458 tree decl_args_type;
1460 /* Make sure that both DECL and FRIEND_DECL are templates or
1461 non-templates. */
1462 is_template = DECL_TEMPLATE_INFO (decl)
1463 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1464 if (need_template ^ is_template)
1465 return false;
1466 else if (is_template)
1468 /* If both are templates, check template parameter list. */
1469 tree friend_parms
1470 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1471 args, tf_none);
1472 if (!comp_template_parms
1473 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1474 friend_parms))
1475 return false;
1477 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1479 else
1480 decl_type = TREE_TYPE (decl);
1482 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1483 tf_none, NULL_TREE);
1484 if (friend_type == error_mark_node)
1485 return false;
1487 /* Check if return types match. */
1488 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1489 return false;
1491 /* Check if function parameter types match, ignoring the
1492 `this' parameter. */
1493 friend_args_type = TYPE_ARG_TYPES (friend_type);
1494 decl_args_type = TYPE_ARG_TYPES (decl_type);
1495 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1496 friend_args_type = TREE_CHAIN (friend_args_type);
1497 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1498 decl_args_type = TREE_CHAIN (decl_args_type);
1500 return compparms (decl_args_type, friend_args_type);
1502 else
1504 /* DECL is a TYPE_DECL */
1505 bool is_template;
1506 tree decl_type = TREE_TYPE (decl);
1508 /* Make sure that both DECL and FRIEND_DECL are templates or
1509 non-templates. */
1510 is_template
1511 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1512 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1514 if (need_template ^ is_template)
1515 return false;
1516 else if (is_template)
1518 tree friend_parms;
1519 /* If both are templates, check the name of the two
1520 TEMPLATE_DECL's first because is_friend didn't. */
1521 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1522 != DECL_NAME (friend_decl))
1523 return false;
1525 /* Now check template parameter list. */
1526 friend_parms
1527 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1528 args, tf_none);
1529 return comp_template_parms
1530 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1531 friend_parms);
1533 else
1534 return (DECL_NAME (decl)
1535 == DECL_NAME (friend_decl));
1538 return false;
1541 /* Register the specialization SPEC as a specialization of TMPL with
1542 the indicated ARGS. IS_FRIEND indicates whether the specialization
1543 is actually just a friend declaration. ATTRLIST is the list of
1544 attributes that the specialization is declared with or NULL when
1545 it isn't. Returns SPEC, or an equivalent prior declaration, if
1546 available.
1548 We also store instantiations of field packs in the hash table, even
1549 though they are not themselves templates, to make lookup easier. */
1551 static tree
1552 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1553 hashval_t hash)
1555 tree fn;
1556 spec_entry **slot = NULL;
1557 spec_entry elt;
1559 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1560 || (TREE_CODE (tmpl) == FIELD_DECL
1561 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1563 if (TREE_CODE (spec) == FUNCTION_DECL
1564 && uses_template_parms (DECL_TI_ARGS (spec)))
1565 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1566 register it; we want the corresponding TEMPLATE_DECL instead.
1567 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1568 the more obvious `uses_template_parms (spec)' to avoid problems
1569 with default function arguments. In particular, given
1570 something like this:
1572 template <class T> void f(T t1, T t = T())
1574 the default argument expression is not substituted for in an
1575 instantiation unless and until it is actually needed. */
1576 return spec;
1578 if (optimize_specialization_lookup_p (tmpl))
1579 /* We don't put these specializations in the hash table, but we might
1580 want to give an error about a mismatch. */
1581 fn = retrieve_specialization (tmpl, args, 0);
1582 else
1584 elt.tmpl = tmpl;
1585 elt.args = args;
1586 elt.spec = spec;
1588 if (hash == 0)
1589 hash = spec_hasher::hash (&elt);
1591 slot = decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1592 if (*slot)
1593 fn = (*slot)->spec;
1594 else
1595 fn = NULL_TREE;
1598 /* We can sometimes try to re-register a specialization that we've
1599 already got. In particular, regenerate_decl_from_template calls
1600 duplicate_decls which will update the specialization list. But,
1601 we'll still get called again here anyhow. It's more convenient
1602 to simply allow this than to try to prevent it. */
1603 if (fn == spec)
1604 return spec;
1605 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1607 if (DECL_TEMPLATE_INSTANTIATION (fn))
1609 if (DECL_ODR_USED (fn)
1610 || DECL_EXPLICIT_INSTANTIATION (fn))
1612 error ("specialization of %qD after instantiation",
1613 fn);
1614 return error_mark_node;
1616 else
1618 tree clone;
1619 /* This situation should occur only if the first
1620 specialization is an implicit instantiation, the
1621 second is an explicit specialization, and the
1622 implicit instantiation has not yet been used. That
1623 situation can occur if we have implicitly
1624 instantiated a member function and then specialized
1625 it later.
1627 We can also wind up here if a friend declaration that
1628 looked like an instantiation turns out to be a
1629 specialization:
1631 template <class T> void foo(T);
1632 class S { friend void foo<>(int) };
1633 template <> void foo(int);
1635 We transform the existing DECL in place so that any
1636 pointers to it become pointers to the updated
1637 declaration.
1639 If there was a definition for the template, but not
1640 for the specialization, we want this to look as if
1641 there were no definition, and vice versa. */
1642 DECL_INITIAL (fn) = NULL_TREE;
1643 duplicate_decls (spec, fn, /*hiding=*/is_friend);
1644 /* The call to duplicate_decls will have applied
1645 [temp.expl.spec]:
1647 An explicit specialization of a function template
1648 is inline only if it is explicitly declared to be,
1649 and independently of whether its function template
1652 to the primary function; now copy the inline bits to
1653 the various clones. */
1654 FOR_EACH_CLONE (clone, fn)
1656 DECL_DECLARED_INLINE_P (clone)
1657 = DECL_DECLARED_INLINE_P (fn);
1658 DECL_SOURCE_LOCATION (clone)
1659 = DECL_SOURCE_LOCATION (fn);
1660 DECL_DELETED_FN (clone)
1661 = DECL_DELETED_FN (fn);
1663 check_specialization_namespace (tmpl);
1665 return fn;
1668 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1670 tree dd = duplicate_decls (spec, fn, /*hiding=*/is_friend);
1671 if (dd == error_mark_node)
1672 /* We've already complained in duplicate_decls. */
1673 return error_mark_node;
1675 if (dd == NULL_TREE && DECL_INITIAL (spec))
1676 /* Dup decl failed, but this is a new definition. Set the
1677 line number so any errors match this new
1678 definition. */
1679 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1681 return fn;
1684 else if (fn)
1685 return duplicate_decls (spec, fn, /*hiding=*/is_friend);
1687 /* A specialization must be declared in the same namespace as the
1688 template it is specializing. */
1689 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1690 && !check_specialization_namespace (tmpl))
1691 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1693 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1695 spec_entry *entry = ggc_alloc<spec_entry> ();
1696 gcc_assert (tmpl && args && spec);
1697 *entry = elt;
1698 *slot = entry;
1699 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1700 && PRIMARY_TEMPLATE_P (tmpl)
1701 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1702 || variable_template_p (tmpl))
1703 /* If TMPL is a forward declaration of a template function, keep a list
1704 of all specializations in case we need to reassign them to a friend
1705 template later in tsubst_friend_function.
1707 Also keep a list of all variable template instantiations so that
1708 process_partial_specialization can check whether a later partial
1709 specialization would have used it. */
1710 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1711 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1714 return spec;
1717 /* Restricts tree and type comparisons. */
1718 int comparing_specializations;
1719 int comparing_dependent_aliases;
1721 /* Returns true iff two spec_entry nodes are equivalent. */
1723 bool
1724 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1726 int equal;
1728 ++comparing_specializations;
1729 ++comparing_dependent_aliases;
1730 ++processing_template_decl;
1731 equal = (e1->tmpl == e2->tmpl
1732 && comp_template_args (e1->args, e2->args));
1733 if (equal && flag_concepts
1734 /* tmpl could be a FIELD_DECL for a capture pack. */
1735 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1736 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1737 && uses_template_parms (e1->args))
1739 /* Partial specializations of a variable template can be distinguished by
1740 constraints. */
1741 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1742 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1743 equal = equivalent_constraints (c1, c2);
1745 --processing_template_decl;
1746 --comparing_dependent_aliases;
1747 --comparing_specializations;
1749 return equal;
1752 /* Returns a hash for a template TMPL and template arguments ARGS. */
1754 static hashval_t
1755 hash_tmpl_and_args (tree tmpl, tree args)
1757 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1758 return iterative_hash_template_arg (args, val);
1761 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1762 ignoring SPEC. */
1764 hashval_t
1765 spec_hasher::hash (spec_entry *e)
1767 return hash_tmpl_and_args (e->tmpl, e->args);
1770 /* Recursively calculate a hash value for a template argument ARG, for use
1771 in the hash tables of template specializations. We must be
1772 careful to (at least) skip the same entities template_args_equal
1773 does. */
1775 hashval_t
1776 iterative_hash_template_arg (tree arg, hashval_t val)
1778 if (arg == NULL_TREE)
1779 return iterative_hash_object (arg, val);
1781 if (!TYPE_P (arg))
1782 /* Strip nop-like things, but not the same as STRIP_NOPS. */
1783 while (CONVERT_EXPR_P (arg)
1784 || TREE_CODE (arg) == NON_LVALUE_EXPR
1785 || class_nttp_const_wrapper_p (arg))
1786 arg = TREE_OPERAND (arg, 0);
1788 enum tree_code code = TREE_CODE (arg);
1790 val = iterative_hash_object (code, val);
1792 switch (code)
1794 case ARGUMENT_PACK_SELECT:
1795 gcc_unreachable ();
1797 case ERROR_MARK:
1798 return val;
1800 case IDENTIFIER_NODE:
1801 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1803 case TREE_VEC:
1804 for (int i = 0, len = TREE_VEC_LENGTH (arg); i < len; ++i)
1805 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1806 return val;
1808 case TYPE_PACK_EXPANSION:
1809 case EXPR_PACK_EXPANSION:
1810 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1811 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1813 case TYPE_ARGUMENT_PACK:
1814 case NONTYPE_ARGUMENT_PACK:
1815 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1817 case TREE_LIST:
1818 for (; arg; arg = TREE_CHAIN (arg))
1819 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1820 return val;
1822 case OVERLOAD:
1823 for (lkp_iterator iter (arg); iter; ++iter)
1824 val = iterative_hash_template_arg (*iter, val);
1825 return val;
1827 case CONSTRUCTOR:
1829 tree field, value;
1830 unsigned i;
1831 iterative_hash_template_arg (TREE_TYPE (arg), val);
1832 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1834 val = iterative_hash_template_arg (field, val);
1835 val = iterative_hash_template_arg (value, val);
1837 return val;
1840 case PARM_DECL:
1841 if (!DECL_ARTIFICIAL (arg))
1843 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1844 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1846 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1848 case TARGET_EXPR:
1849 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1851 case PTRMEM_CST:
1852 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1853 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1855 case TEMPLATE_PARM_INDEX:
1856 val = iterative_hash_template_arg
1857 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1858 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1859 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1861 case TRAIT_EXPR:
1862 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1863 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1864 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1866 case BASELINK:
1867 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1868 val);
1869 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1870 val);
1872 case MODOP_EXPR:
1873 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1874 code = TREE_CODE (TREE_OPERAND (arg, 1));
1875 val = iterative_hash_object (code, val);
1876 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1878 case LAMBDA_EXPR:
1879 /* [temp.over.link] Two lambda-expressions are never considered
1880 equivalent.
1882 So just hash the closure type. */
1883 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1885 case CAST_EXPR:
1886 case IMPLICIT_CONV_EXPR:
1887 case STATIC_CAST_EXPR:
1888 case REINTERPRET_CAST_EXPR:
1889 case CONST_CAST_EXPR:
1890 case DYNAMIC_CAST_EXPR:
1891 case NEW_EXPR:
1892 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1893 /* Now hash operands as usual. */
1894 break;
1896 case CALL_EXPR:
1898 tree fn = CALL_EXPR_FN (arg);
1899 if (tree name = dependent_name (fn))
1901 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
1902 val = iterative_hash_template_arg (TREE_OPERAND (fn, 1), val);
1903 fn = name;
1905 val = iterative_hash_template_arg (fn, val);
1906 call_expr_arg_iterator ai;
1907 for (tree x = first_call_expr_arg (arg, &ai); x;
1908 x = next_call_expr_arg (&ai))
1909 val = iterative_hash_template_arg (x, val);
1910 return val;
1913 default:
1914 break;
1917 char tclass = TREE_CODE_CLASS (code);
1918 switch (tclass)
1920 case tcc_type:
1921 if (tree ats = alias_template_specialization_p (arg, nt_transparent))
1923 // We want an alias specialization that survived strip_typedefs
1924 // to hash differently from its TYPE_CANONICAL, to avoid hash
1925 // collisions that compare as different in template_args_equal.
1926 // These could be dependent specializations that strip_typedefs
1927 // left alone, or untouched specializations because
1928 // coerce_template_parms returns the unconverted template
1929 // arguments if it sees incomplete argument packs.
1930 tree ti = TYPE_ALIAS_TEMPLATE_INFO (ats);
1931 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1934 switch (TREE_CODE (arg))
1936 case TEMPLATE_TEMPLATE_PARM:
1938 tree tpi = TEMPLATE_TYPE_PARM_INDEX (arg);
1940 /* Do not recurse with TPI directly, as that is unbounded
1941 recursion. */
1942 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (tpi), val);
1943 val = iterative_hash_object (TEMPLATE_PARM_IDX (tpi), val);
1945 break;
1947 case DECLTYPE_TYPE:
1948 val = iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1949 break;
1951 default:
1952 if (tree canonical = TYPE_CANONICAL (arg))
1953 val = iterative_hash_object (TYPE_HASH (canonical), val);
1954 break;
1957 return val;
1959 case tcc_declaration:
1960 case tcc_constant:
1961 return iterative_hash_expr (arg, val);
1963 default:
1964 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1965 for (int i = 0, n = cp_tree_operand_length (arg); i < n; ++i)
1966 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1967 return val;
1970 gcc_unreachable ();
1971 return 0;
1974 /* Unregister the specialization SPEC as a specialization of TMPL.
1975 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1976 if the SPEC was listed as a specialization of TMPL.
1978 Note that SPEC has been ggc_freed, so we can't look inside it. */
1980 bool
1981 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1983 spec_entry *entry;
1984 spec_entry elt;
1986 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1987 elt.args = TI_ARGS (tinfo);
1988 elt.spec = NULL_TREE;
1990 entry = decl_specializations->find (&elt);
1991 if (entry != NULL)
1993 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1994 gcc_assert (new_spec != NULL_TREE);
1995 entry->spec = new_spec;
1996 return 1;
1999 return 0;
2002 /* Like register_specialization, but for local declarations. We are
2003 registering SPEC, an instantiation of TMPL. */
2005 void
2006 register_local_specialization (tree spec, tree tmpl)
2008 gcc_assert (tmpl != spec);
2009 local_specializations->put (tmpl, spec);
2012 /* TYPE is a class type. Returns true if TYPE is an explicitly
2013 specialized class. */
2015 bool
2016 explicit_class_specialization_p (tree type)
2018 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
2019 return false;
2020 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
2023 /* Print the list of functions at FNS, going through all the overloads
2024 for each element of the list. Alternatively, FNS cannot be a
2025 TREE_LIST, in which case it will be printed together with all the
2026 overloads.
2028 MORE and *STR should respectively be FALSE and NULL when the function
2029 is called from the outside. They are used internally on recursive
2030 calls. print_candidates manages the two parameters and leaves NULL
2031 in *STR when it ends. */
2033 static void
2034 print_candidates_1 (tree fns, char **str, bool more = false)
2036 if (TREE_CODE (fns) == TREE_LIST)
2037 for (; fns; fns = TREE_CHAIN (fns))
2038 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
2039 else
2040 for (lkp_iterator iter (fns); iter;)
2042 tree cand = *iter;
2043 ++iter;
2045 const char *pfx = *str;
2046 if (!pfx)
2048 if (more || iter)
2049 pfx = _("candidates are:");
2050 else
2051 pfx = _("candidate is:");
2052 *str = get_spaces (pfx);
2054 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
2058 /* Print the list of candidate FNS in an error message. FNS can also
2059 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
2061 void
2062 print_candidates (tree fns)
2064 char *str = NULL;
2065 print_candidates_1 (fns, &str);
2066 free (str);
2069 /* Get a (possibly) constrained template declaration for the
2070 purpose of ordering candidates. */
2071 static tree
2072 get_template_for_ordering (tree list)
2074 gcc_assert (TREE_CODE (list) == TREE_LIST);
2075 tree f = TREE_VALUE (list);
2076 if (tree ti = DECL_TEMPLATE_INFO (f))
2077 return TI_TEMPLATE (ti);
2078 return f;
2081 /* Among candidates having the same signature, return the
2082 most constrained or NULL_TREE if there is no best candidate.
2083 If the signatures of candidates vary (e.g., template
2084 specialization vs. member function), then there can be no
2085 most constrained.
2087 Note that we don't compare constraints on the functions
2088 themselves, but rather those of their templates. */
2089 static tree
2090 most_constrained_function (tree candidates)
2092 // Try to find the best candidate in a first pass.
2093 tree champ = candidates;
2094 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
2096 int winner = more_constrained (get_template_for_ordering (champ),
2097 get_template_for_ordering (c));
2098 if (winner == -1)
2099 champ = c; // The candidate is more constrained
2100 else if (winner == 0)
2101 return NULL_TREE; // Neither is more constrained
2104 // Verify that the champ is better than previous candidates.
2105 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2106 if (!more_constrained (get_template_for_ordering (champ),
2107 get_template_for_ordering (c)))
2108 return NULL_TREE;
2111 return champ;
2115 /* Returns the template (one of the functions given by TEMPLATE_ID)
2116 which can be specialized to match the indicated DECL with the
2117 explicit template args given in TEMPLATE_ID. The DECL may be
2118 NULL_TREE if none is available. In that case, the functions in
2119 TEMPLATE_ID are non-members.
2121 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2122 specialization of a member template.
2124 The TEMPLATE_COUNT is the number of references to qualifying
2125 template classes that appeared in the name of the function. See
2126 check_explicit_specialization for a more accurate description.
2128 TSK indicates what kind of template declaration (if any) is being
2129 declared. TSK_TEMPLATE indicates that the declaration given by
2130 DECL, though a FUNCTION_DECL, has template parameters, and is
2131 therefore a template function.
2133 The template args (those explicitly specified and those deduced)
2134 are output in a newly created vector *TARGS_OUT.
2136 If it is impossible to determine the result, an error message is
2137 issued. The error_mark_node is returned to indicate failure. */
2139 static tree
2140 determine_specialization (tree template_id,
2141 tree decl,
2142 tree* targs_out,
2143 int need_member_template,
2144 int template_count,
2145 tmpl_spec_kind tsk)
2147 tree fns;
2148 tree targs;
2149 tree explicit_targs;
2150 tree candidates = NULL_TREE;
2152 /* A TREE_LIST of templates of which DECL may be a specialization.
2153 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2154 corresponding TREE_PURPOSE is the set of template arguments that,
2155 when used to instantiate the template, would produce a function
2156 with the signature of DECL. */
2157 tree templates = NULL_TREE;
2158 int header_count;
2159 cp_binding_level *b;
2161 *targs_out = NULL_TREE;
2163 if (template_id == error_mark_node || decl == error_mark_node)
2164 return error_mark_node;
2166 /* We shouldn't be specializing a member template of an
2167 unspecialized class template; we already gave an error in
2168 check_specialization_scope, now avoid crashing. */
2169 if (!VAR_P (decl)
2170 && template_count && DECL_CLASS_SCOPE_P (decl)
2171 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2173 gcc_assert (errorcount);
2174 return error_mark_node;
2177 fns = TREE_OPERAND (template_id, 0);
2178 explicit_targs = TREE_OPERAND (template_id, 1);
2180 if (fns == error_mark_node)
2181 return error_mark_node;
2183 /* Check for baselinks. */
2184 if (BASELINK_P (fns))
2185 fns = BASELINK_FUNCTIONS (fns);
2187 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2189 error_at (DECL_SOURCE_LOCATION (decl),
2190 "%qD is not a function template", fns);
2191 return error_mark_node;
2193 else if (VAR_P (decl) && !variable_template_p (fns))
2195 error ("%qD is not a variable template", fns);
2196 return error_mark_node;
2199 /* Count the number of template headers specified for this
2200 specialization. */
2201 header_count = 0;
2202 for (b = current_binding_level;
2203 b->kind == sk_template_parms;
2204 b = b->level_chain)
2205 ++header_count;
2207 tree orig_fns = fns;
2208 bool header_mismatch = false;
2210 if (variable_template_p (fns))
2212 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2213 targs = coerce_template_parms (parms, explicit_targs, fns,
2214 tf_warning_or_error,
2215 /*req_all*/true, /*use_defarg*/true);
2216 if (targs != error_mark_node)
2217 templates = tree_cons (targs, fns, templates);
2219 else for (lkp_iterator iter (fns); iter; ++iter)
2221 tree fn = *iter;
2223 if (TREE_CODE (fn) == TEMPLATE_DECL)
2225 tree decl_arg_types;
2226 tree fn_arg_types;
2227 tree insttype;
2229 /* In case of explicit specialization, we need to check if
2230 the number of template headers appearing in the specialization
2231 is correct. This is usually done in check_explicit_specialization,
2232 but the check done there cannot be exhaustive when specializing
2233 member functions. Consider the following code:
2235 template <> void A<int>::f(int);
2236 template <> template <> void A<int>::f(int);
2238 Assuming that A<int> is not itself an explicit specialization
2239 already, the first line specializes "f" which is a non-template
2240 member function, whilst the second line specializes "f" which
2241 is a template member function. So both lines are syntactically
2242 correct, and check_explicit_specialization does not reject
2243 them.
2245 Here, we can do better, as we are matching the specialization
2246 against the declarations. We count the number of template
2247 headers, and we check if they match TEMPLATE_COUNT + 1
2248 (TEMPLATE_COUNT is the number of qualifying template classes,
2249 plus there must be another header for the member template
2250 itself).
2252 Notice that if header_count is zero, this is not a
2253 specialization but rather a template instantiation, so there
2254 is no check we can perform here. */
2255 if (header_count && header_count != template_count + 1)
2257 header_mismatch = true;
2258 continue;
2261 /* Check that the number of template arguments at the
2262 innermost level for DECL is the same as for FN. */
2263 if (current_binding_level->kind == sk_template_parms
2264 && !current_binding_level->explicit_spec_p
2265 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2266 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2267 (current_template_parms))))
2268 continue;
2270 /* DECL might be a specialization of FN. */
2271 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2272 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2274 /* For a non-static member function, we need to make sure
2275 that the const qualification is the same. Since
2276 get_bindings does not try to merge the "this" parameter,
2277 we must do the comparison explicitly. */
2278 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2280 if (!same_type_p (TREE_VALUE (fn_arg_types),
2281 TREE_VALUE (decl_arg_types)))
2282 continue;
2284 /* And the ref-qualification. */
2285 if (type_memfn_rqual (TREE_TYPE (decl))
2286 != type_memfn_rqual (TREE_TYPE (fn)))
2287 continue;
2290 /* Skip the "this" parameter and, for constructors of
2291 classes with virtual bases, the VTT parameter. A
2292 full specialization of a constructor will have a VTT
2293 parameter, but a template never will. */
2294 decl_arg_types
2295 = skip_artificial_parms_for (decl, decl_arg_types);
2296 fn_arg_types
2297 = skip_artificial_parms_for (fn, fn_arg_types);
2299 /* Function templates cannot be specializations; there are
2300 no partial specializations of functions. Therefore, if
2301 the type of DECL does not match FN, there is no
2302 match.
2304 Note that it should never be the case that we have both
2305 candidates added here, and for regular member functions
2306 below. */
2307 if (tsk == tsk_template)
2309 if (!comp_template_parms (DECL_TEMPLATE_PARMS (fn),
2310 current_template_parms))
2311 continue;
2312 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2313 TREE_TYPE (TREE_TYPE (fn))))
2314 continue;
2315 if (!compparms (fn_arg_types, decl_arg_types))
2316 continue;
2318 tree freq = get_trailing_function_requirements (fn);
2319 tree dreq = get_trailing_function_requirements (decl);
2320 if (!freq != !dreq)
2321 continue;
2322 if (freq)
2324 tree fargs = DECL_TI_ARGS (fn);
2325 tsubst_flags_t complain = tf_none;
2326 freq = tsubst_constraint (freq, fargs, complain, fn);
2327 if (!cp_tree_equal (freq, dreq))
2328 continue;
2331 candidates = tree_cons (NULL_TREE, fn, candidates);
2332 continue;
2335 /* See whether this function might be a specialization of this
2336 template. Suppress access control because we might be trying
2337 to make this specialization a friend, and we have already done
2338 access control for the declaration of the specialization. */
2339 push_deferring_access_checks (dk_no_check);
2340 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2341 pop_deferring_access_checks ();
2343 if (!targs)
2344 /* We cannot deduce template arguments that when used to
2345 specialize TMPL will produce DECL. */
2346 continue;
2348 if (uses_template_parms (targs))
2349 /* We deduced something involving 'auto', which isn't a valid
2350 template argument. */
2351 continue;
2353 /* Remove, from the set of candidates, all those functions
2354 whose constraints are not satisfied. */
2355 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2356 continue;
2358 // Then, try to form the new function type.
2359 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2360 if (insttype == error_mark_node)
2361 continue;
2362 fn_arg_types
2363 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2364 if (!compparms (fn_arg_types, decl_arg_types))
2365 continue;
2367 /* Save this template, and the arguments deduced. */
2368 templates = tree_cons (targs, fn, templates);
2370 else if (need_member_template)
2371 /* FN is an ordinary member function, and we need a
2372 specialization of a member template. */
2374 else if (TREE_CODE (fn) != FUNCTION_DECL)
2375 /* We can get IDENTIFIER_NODEs here in certain erroneous
2376 cases. */
2378 else if (!DECL_FUNCTION_MEMBER_P (fn))
2379 /* This is just an ordinary non-member function. Nothing can
2380 be a specialization of that. */
2382 else if (DECL_ARTIFICIAL (fn))
2383 /* Cannot specialize functions that are created implicitly. */
2385 else
2387 tree decl_arg_types;
2389 /* This is an ordinary member function. However, since
2390 we're here, we can assume its enclosing class is a
2391 template class. For example,
2393 template <typename T> struct S { void f(); };
2394 template <> void S<int>::f() {}
2396 Here, S<int>::f is a non-template, but S<int> is a
2397 template class. If FN has the same type as DECL, we
2398 might be in business. */
2400 if (!DECL_TEMPLATE_INFO (fn))
2401 /* Its enclosing class is an explicit specialization
2402 of a template class. This is not a candidate. */
2403 continue;
2405 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2406 TREE_TYPE (TREE_TYPE (fn))))
2407 /* The return types differ. */
2408 continue;
2410 /* Adjust the type of DECL in case FN is a static member. */
2411 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2412 if (DECL_STATIC_FUNCTION_P (fn)
2413 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2414 decl_arg_types = TREE_CHAIN (decl_arg_types);
2416 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2417 decl_arg_types))
2418 continue;
2420 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2421 && (type_memfn_rqual (TREE_TYPE (decl))
2422 != type_memfn_rqual (TREE_TYPE (fn))))
2423 continue;
2425 // If the deduced arguments do not satisfy the constraints,
2426 // this is not a candidate.
2427 if (flag_concepts && !constraints_satisfied_p (fn))
2428 continue;
2430 // Add the candidate.
2431 candidates = tree_cons (NULL_TREE, fn, candidates);
2435 if (templates && TREE_CHAIN (templates))
2437 /* We have:
2439 [temp.expl.spec]
2441 It is possible for a specialization with a given function
2442 signature to be instantiated from more than one function
2443 template. In such cases, explicit specification of the
2444 template arguments must be used to uniquely identify the
2445 function template specialization being specialized.
2447 Note that here, there's no suggestion that we're supposed to
2448 determine which of the candidate templates is most
2449 specialized. However, we, also have:
2451 [temp.func.order]
2453 Partial ordering of overloaded function template
2454 declarations is used in the following contexts to select
2455 the function template to which a function template
2456 specialization refers:
2458 -- when an explicit specialization refers to a function
2459 template.
2461 So, we do use the partial ordering rules, at least for now.
2462 This extension can only serve to make invalid programs valid,
2463 so it's safe. And, there is strong anecdotal evidence that
2464 the committee intended the partial ordering rules to apply;
2465 the EDG front end has that behavior, and John Spicer claims
2466 that the committee simply forgot to delete the wording in
2467 [temp.expl.spec]. */
2468 tree tmpl = most_specialized_instantiation (templates);
2469 if (tmpl != error_mark_node)
2471 templates = tmpl;
2472 TREE_CHAIN (templates) = NULL_TREE;
2476 // Concepts allows multiple declarations of member functions
2477 // with the same signature. Like above, we need to rely on
2478 // on the partial ordering of those candidates to determine which
2479 // is the best.
2480 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2482 if (tree cand = most_constrained_function (candidates))
2484 candidates = cand;
2485 TREE_CHAIN (cand) = NULL_TREE;
2489 if (templates == NULL_TREE && candidates == NULL_TREE)
2491 error ("template-id %qD for %q+D does not match any template "
2492 "declaration", template_id, decl);
2493 if (header_mismatch)
2494 inform (DECL_SOURCE_LOCATION (decl),
2495 "saw %d %<template<>%>, need %d for "
2496 "specializing a member function template",
2497 header_count, template_count + 1);
2498 print_candidates (orig_fns);
2499 return error_mark_node;
2501 else if ((templates && TREE_CHAIN (templates))
2502 || (candidates && TREE_CHAIN (candidates))
2503 || (templates && candidates))
2505 error ("ambiguous template specialization %qD for %q+D",
2506 template_id, decl);
2507 candidates = chainon (candidates, templates);
2508 print_candidates (candidates);
2509 return error_mark_node;
2512 /* We have one, and exactly one, match. */
2513 if (candidates)
2515 tree fn = TREE_VALUE (candidates);
2516 *targs_out = copy_node (DECL_TI_ARGS (fn));
2518 /* Propagate the candidate's constraints to the declaration. */
2519 if (tsk != tsk_template)
2520 set_constraints (decl, get_constraints (fn));
2522 /* DECL is a re-declaration or partial instantiation of a template
2523 function. */
2524 if (TREE_CODE (fn) == TEMPLATE_DECL)
2525 return fn;
2526 /* It was a specialization of an ordinary member function in a
2527 template class. */
2528 return DECL_TI_TEMPLATE (fn);
2531 /* It was a specialization of a template. */
2532 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2533 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2535 *targs_out = copy_node (targs);
2536 SET_TMPL_ARGS_LEVEL (*targs_out,
2537 TMPL_ARGS_DEPTH (*targs_out),
2538 TREE_PURPOSE (templates));
2540 else
2541 *targs_out = TREE_PURPOSE (templates);
2542 return TREE_VALUE (templates);
2545 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2546 but with the default argument values filled in from those in the
2547 TMPL_TYPES. */
2549 static tree
2550 copy_default_args_to_explicit_spec_1 (tree spec_types,
2551 tree tmpl_types)
2553 tree new_spec_types;
2555 if (!spec_types)
2556 return NULL_TREE;
2558 if (spec_types == void_list_node)
2559 return void_list_node;
2561 /* Substitute into the rest of the list. */
2562 new_spec_types =
2563 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2564 TREE_CHAIN (tmpl_types));
2566 /* Add the default argument for this parameter. */
2567 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2568 TREE_VALUE (spec_types),
2569 new_spec_types);
2572 /* DECL is an explicit specialization. Replicate default arguments
2573 from the template it specializes. (That way, code like:
2575 template <class T> void f(T = 3);
2576 template <> void f(double);
2577 void g () { f (); }
2579 works, as required.) An alternative approach would be to look up
2580 the correct default arguments at the call-site, but this approach
2581 is consistent with how implicit instantiations are handled. */
2583 static void
2584 copy_default_args_to_explicit_spec (tree decl)
2586 tree tmpl;
2587 tree spec_types;
2588 tree tmpl_types;
2589 tree new_spec_types;
2590 tree old_type;
2591 tree new_type;
2592 tree t;
2593 tree object_type = NULL_TREE;
2594 tree in_charge = NULL_TREE;
2595 tree vtt = NULL_TREE;
2597 /* See if there's anything we need to do. */
2598 tmpl = DECL_TI_TEMPLATE (decl);
2599 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2600 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2601 if (TREE_PURPOSE (t))
2602 break;
2603 if (!t)
2604 return;
2606 old_type = TREE_TYPE (decl);
2607 spec_types = TYPE_ARG_TYPES (old_type);
2609 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2611 /* Remove the this pointer, but remember the object's type for
2612 CV quals. */
2613 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2614 spec_types = TREE_CHAIN (spec_types);
2615 tmpl_types = TREE_CHAIN (tmpl_types);
2617 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2619 /* DECL may contain more parameters than TMPL due to the extra
2620 in-charge parameter in constructors and destructors. */
2621 in_charge = spec_types;
2622 spec_types = TREE_CHAIN (spec_types);
2624 if (DECL_HAS_VTT_PARM_P (decl))
2626 vtt = spec_types;
2627 spec_types = TREE_CHAIN (spec_types);
2631 /* Compute the merged default arguments. */
2632 new_spec_types =
2633 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2635 /* Compute the new FUNCTION_TYPE. */
2636 if (object_type)
2638 if (vtt)
2639 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2640 TREE_VALUE (vtt),
2641 new_spec_types);
2643 if (in_charge)
2644 /* Put the in-charge parameter back. */
2645 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2646 TREE_VALUE (in_charge),
2647 new_spec_types);
2649 new_type = build_method_type_directly (object_type,
2650 TREE_TYPE (old_type),
2651 new_spec_types);
2653 else
2654 new_type = build_function_type (TREE_TYPE (old_type),
2655 new_spec_types);
2656 new_type = cp_build_type_attribute_variant (new_type,
2657 TYPE_ATTRIBUTES (old_type));
2658 new_type = cxx_copy_lang_qualifiers (new_type, old_type);
2660 TREE_TYPE (decl) = new_type;
2663 /* Return the number of template headers we expect to see for a definition
2664 or specialization of CTYPE or one of its non-template members. */
2667 num_template_headers_for_class (tree ctype)
2669 int num_templates = 0;
2671 while (ctype && CLASS_TYPE_P (ctype))
2673 /* You're supposed to have one `template <...>' for every
2674 template class, but you don't need one for a full
2675 specialization. For example:
2677 template <class T> struct S{};
2678 template <> struct S<int> { void f(); };
2679 void S<int>::f () {}
2681 is correct; there shouldn't be a `template <>' for the
2682 definition of `S<int>::f'. */
2683 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2684 /* If CTYPE does not have template information of any
2685 kind, then it is not a template, nor is it nested
2686 within a template. */
2687 break;
2688 if (explicit_class_specialization_p (ctype))
2689 break;
2690 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2691 ++num_templates;
2693 ctype = TYPE_CONTEXT (ctype);
2696 return num_templates;
2699 /* Do a simple sanity check on the template headers that precede the
2700 variable declaration DECL. */
2702 void
2703 check_template_variable (tree decl)
2705 tree ctx = CP_DECL_CONTEXT (decl);
2706 int wanted = num_template_headers_for_class (ctx);
2707 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2708 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2710 if (cxx_dialect < cxx14)
2711 pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__14_extensions,
2712 "variable templates only available with "
2713 "%<-std=c++14%> or %<-std=gnu++14%>");
2715 // Namespace-scope variable templates should have a template header.
2716 ++wanted;
2718 if (template_header_count > wanted)
2720 auto_diagnostic_group d;
2721 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2722 "too many template headers for %qD "
2723 "(should be %d)",
2724 decl, wanted);
2725 if (warned && CLASS_TYPE_P (ctx)
2726 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2727 inform (DECL_SOURCE_LOCATION (decl),
2728 "members of an explicitly specialized class are defined "
2729 "without a template header");
2733 /* An explicit specialization whose declarator-id or class-head-name is not
2734 qualified shall be declared in the nearest enclosing namespace of the
2735 template, or, if the namespace is inline (7.3.1), any namespace from its
2736 enclosing namespace set.
2738 If the name declared in the explicit instantiation is an unqualified name,
2739 the explicit instantiation shall appear in the namespace where its template
2740 is declared or, if that namespace is inline (7.3.1), any namespace from its
2741 enclosing namespace set. */
2743 void
2744 check_unqualified_spec_or_inst (tree t, location_t loc)
2746 tree tmpl = most_general_template (t);
2747 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2748 && !is_nested_namespace (current_namespace,
2749 CP_DECL_CONTEXT (tmpl), true))
2751 if (processing_specialization)
2752 permerror (loc, "explicit specialization of %qD outside its "
2753 "namespace must use a nested-name-specifier", tmpl);
2754 else if (processing_explicit_instantiation
2755 && cxx_dialect >= cxx11)
2756 /* This was allowed in C++98, so only pedwarn. */
2757 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2758 "outside its namespace must use a nested-name-"
2759 "specifier", tmpl);
2763 /* Warn for a template specialization SPEC that is missing some of a set
2764 of function or type attributes that the template TEMPL is declared with.
2765 ATTRLIST is a list of additional attributes that SPEC should be taken
2766 to ultimately be declared with. */
2768 static void
2769 warn_spec_missing_attributes (tree tmpl, tree spec, tree attrlist)
2771 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
2772 tmpl = DECL_TEMPLATE_RESULT (tmpl);
2774 /* Avoid warning if the difference between the primary and
2775 the specialization is not in one of the attributes below. */
2776 const char* const blacklist[] = {
2777 "alloc_align", "alloc_size", "assume_aligned", "format",
2778 "format_arg", "malloc", "nonnull", NULL
2781 /* Put together a list of the black listed attributes that the primary
2782 template is declared with that the specialization is not, in case
2783 it's not apparent from the most recent declaration of the primary. */
2784 pretty_printer str;
2785 unsigned nattrs = decls_mismatched_attributes (tmpl, spec, attrlist,
2786 blacklist, &str);
2788 if (!nattrs)
2789 return;
2791 auto_diagnostic_group d;
2792 if (warning_at (DECL_SOURCE_LOCATION (spec), OPT_Wmissing_attributes,
2793 "explicit specialization %q#D may be missing attributes",
2794 spec))
2795 inform (DECL_SOURCE_LOCATION (tmpl),
2796 nattrs > 1
2797 ? G_("missing primary template attributes %s")
2798 : G_("missing primary template attribute %s"),
2799 pp_formatted_text (&str));
2802 /* Check to see if the function just declared, as indicated in
2803 DECLARATOR, and in DECL, is a specialization of a function
2804 template. We may also discover that the declaration is an explicit
2805 instantiation at this point.
2807 Returns DECL, or an equivalent declaration that should be used
2808 instead if all goes well. Issues an error message if something is
2809 amiss. Returns error_mark_node if the error is not easily
2810 recoverable.
2812 FLAGS is a bitmask consisting of the following flags:
2814 2: The function has a definition.
2815 4: The function is a friend.
2817 The TEMPLATE_COUNT is the number of references to qualifying
2818 template classes that appeared in the name of the function. For
2819 example, in
2821 template <class T> struct S { void f(); };
2822 void S<int>::f();
2824 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2825 classes are not counted in the TEMPLATE_COUNT, so that in
2827 template <class T> struct S {};
2828 template <> struct S<int> { void f(); }
2829 template <> void S<int>::f();
2831 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2832 invalid; there should be no template <>.)
2834 If the function is a specialization, it is marked as such via
2835 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2836 is set up correctly, and it is added to the list of specializations
2837 for that template. */
2839 tree
2840 check_explicit_specialization (tree declarator,
2841 tree decl,
2842 int template_count,
2843 int flags,
2844 tree attrlist)
2846 int have_def = flags & 2;
2847 int is_friend = flags & 4;
2848 bool is_concept = flags & 8;
2849 int specialization = 0;
2850 int explicit_instantiation = 0;
2851 int member_specialization = 0;
2852 tree ctype = DECL_CLASS_CONTEXT (decl);
2853 tree dname = DECL_NAME (decl);
2854 tmpl_spec_kind tsk;
2856 if (is_friend)
2858 if (!processing_specialization)
2859 tsk = tsk_none;
2860 else
2861 tsk = tsk_excessive_parms;
2863 else
2864 tsk = current_tmpl_spec_kind (template_count);
2866 switch (tsk)
2868 case tsk_none:
2869 if (processing_specialization && !VAR_P (decl))
2871 specialization = 1;
2872 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2874 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2876 if (is_friend)
2877 /* This could be something like:
2879 template <class T> void f(T);
2880 class S { friend void f<>(int); } */
2881 specialization = 1;
2882 else
2884 /* This case handles bogus declarations like template <>
2885 template <class T> void f<int>(); */
2887 error_at (cp_expr_loc_or_input_loc (declarator),
2888 "template-id %qE in declaration of primary template",
2889 declarator);
2890 return decl;
2893 break;
2895 case tsk_invalid_member_spec:
2896 /* The error has already been reported in
2897 check_specialization_scope. */
2898 return error_mark_node;
2900 case tsk_invalid_expl_inst:
2901 error ("template parameter list used in explicit instantiation");
2903 /* Fall through. */
2905 case tsk_expl_inst:
2906 if (have_def)
2907 error ("definition provided for explicit instantiation");
2909 explicit_instantiation = 1;
2910 break;
2912 case tsk_excessive_parms:
2913 case tsk_insufficient_parms:
2914 if (tsk == tsk_excessive_parms)
2915 error ("too many template parameter lists in declaration of %qD",
2916 decl);
2917 else if (template_header_count)
2918 error("too few template parameter lists in declaration of %qD", decl);
2919 else
2920 error("explicit specialization of %qD must be introduced by "
2921 "%<template <>%>", decl);
2923 /* Fall through. */
2924 case tsk_expl_spec:
2925 if (is_concept)
2926 error ("explicit specialization declared %<concept%>");
2928 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2929 /* In cases like template<> constexpr bool v = true;
2930 We'll give an error in check_template_variable. */
2931 break;
2933 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2934 if (ctype)
2935 member_specialization = 1;
2936 else
2937 specialization = 1;
2938 break;
2940 case tsk_template:
2941 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2943 /* This case handles bogus declarations like template <>
2944 template <class T> void f<int>(); */
2946 if (!uses_template_parms (TREE_OPERAND (declarator, 1)))
2947 error_at (cp_expr_loc_or_input_loc (declarator),
2948 "template-id %qE in declaration of primary template",
2949 declarator);
2950 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2952 /* Partial specialization of variable template. */
2953 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2954 specialization = 1;
2955 goto ok;
2957 else if (cxx_dialect < cxx14)
2958 error_at (cp_expr_loc_or_input_loc (declarator),
2959 "non-type partial specialization %qE "
2960 "is not allowed", declarator);
2961 else
2962 error_at (cp_expr_loc_or_input_loc (declarator),
2963 "non-class, non-variable partial specialization %qE "
2964 "is not allowed", declarator);
2965 return decl;
2966 ok:;
2969 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2970 /* This is a specialization of a member template, without
2971 specialization the containing class. Something like:
2973 template <class T> struct S {
2974 template <class U> void f (U);
2976 template <> template <class U> void S<int>::f(U) {}
2978 That's a specialization -- but of the entire template. */
2979 specialization = 1;
2980 break;
2982 default:
2983 gcc_unreachable ();
2986 if ((specialization || member_specialization)
2987 /* This doesn't apply to variable templates. */
2988 && FUNC_OR_METHOD_TYPE_P (TREE_TYPE (decl)))
2990 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2991 for (; t; t = TREE_CHAIN (t))
2992 if (TREE_PURPOSE (t))
2994 permerror (input_location,
2995 "default argument specified in explicit specialization");
2996 break;
3000 if (specialization || member_specialization || explicit_instantiation)
3002 tree tmpl = NULL_TREE;
3003 tree targs = NULL_TREE;
3004 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
3005 bool found_hidden = false;
3007 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
3008 if (!was_template_id)
3010 tree fns;
3012 gcc_assert (identifier_p (declarator));
3013 if (ctype)
3014 fns = dname;
3015 else
3017 /* If there is no class context, the explicit instantiation
3018 must be at namespace scope. */
3019 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
3021 /* Find the namespace binding, using the declaration
3022 context. */
3023 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
3024 LOOK_want::NORMAL, true);
3025 if (fns == error_mark_node)
3027 /* If lookup fails, look for a friend declaration so we can
3028 give a better diagnostic. */
3029 fns = (lookup_qualified_name
3030 (CP_DECL_CONTEXT (decl), dname,
3031 LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND,
3032 /*complain*/true));
3033 found_hidden = true;
3036 if (fns == error_mark_node || !is_overloaded_fn (fns))
3038 error ("%qD is not a template function", dname);
3039 fns = error_mark_node;
3043 declarator = lookup_template_function (fns, NULL_TREE);
3046 if (declarator == error_mark_node)
3047 return error_mark_node;
3049 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
3051 if (!explicit_instantiation)
3052 /* A specialization in class scope. This is invalid,
3053 but the error will already have been flagged by
3054 check_specialization_scope. */
3055 return error_mark_node;
3056 else
3058 /* It's not valid to write an explicit instantiation in
3059 class scope, e.g.:
3061 class C { template void f(); }
3063 This case is caught by the parser. However, on
3064 something like:
3066 template class C { void f(); };
3068 (which is invalid) we can get here. The error will be
3069 issued later. */
3073 return decl;
3075 else if (ctype != NULL_TREE
3076 && (identifier_p (TREE_OPERAND (declarator, 0))))
3078 // We'll match variable templates in start_decl.
3079 if (VAR_P (decl))
3080 return decl;
3082 /* Find the list of functions in ctype that have the same
3083 name as the declared function. */
3084 tree name = TREE_OPERAND (declarator, 0);
3086 if (constructor_name_p (name, ctype))
3088 if (DECL_CONSTRUCTOR_P (decl)
3089 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
3090 : !CLASSTYPE_DESTRUCTOR (ctype))
3092 /* From [temp.expl.spec]:
3094 If such an explicit specialization for the member
3095 of a class template names an implicitly-declared
3096 special member function (clause _special_), the
3097 program is ill-formed.
3099 Similar language is found in [temp.explicit]. */
3100 error ("specialization of implicitly-declared special member function");
3101 return error_mark_node;
3104 name = DECL_NAME (decl);
3107 /* For a type-conversion operator, We might be looking for
3108 `operator int' which will be a specialization of
3109 `operator T'. Grab all the conversion operators, and
3110 then select from them. */
3111 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
3112 ? conv_op_identifier : name);
3114 if (fns == NULL_TREE)
3116 error ("no member function %qD declared in %qT", name, ctype);
3117 return error_mark_node;
3119 else
3120 TREE_OPERAND (declarator, 0) = fns;
3123 /* Figure out what exactly is being specialized at this point.
3124 Note that for an explicit instantiation, even one for a
3125 member function, we cannot tell a priori whether the
3126 instantiation is for a member template, or just a member
3127 function of a template class. Even if a member template is
3128 being instantiated, the member template arguments may be
3129 elided if they can be deduced from the rest of the
3130 declaration. */
3131 tmpl = determine_specialization (declarator, decl,
3132 &targs,
3133 member_specialization,
3134 template_count,
3135 tsk);
3137 if (!tmpl || tmpl == error_mark_node)
3138 /* We couldn't figure out what this declaration was
3139 specializing. */
3140 return error_mark_node;
3141 else
3143 if (found_hidden && TREE_CODE (decl) == FUNCTION_DECL)
3145 auto_diagnostic_group d;
3146 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3147 "friend declaration %qD is not visible to "
3148 "explicit specialization", tmpl))
3149 inform (DECL_SOURCE_LOCATION (tmpl),
3150 "friend declaration here");
3153 if (!ctype && !is_friend
3154 && CP_DECL_CONTEXT (decl) == current_namespace)
3155 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
3157 tree gen_tmpl = most_general_template (tmpl);
3159 if (explicit_instantiation)
3161 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3162 is done by do_decl_instantiation later. */
3164 int arg_depth = TMPL_ARGS_DEPTH (targs);
3165 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3167 if (arg_depth > parm_depth)
3169 /* If TMPL is not the most general template (for
3170 example, if TMPL is a friend template that is
3171 injected into namespace scope), then there will
3172 be too many levels of TARGS. Remove some of them
3173 here. */
3174 int i;
3175 tree new_targs;
3177 new_targs = make_tree_vec (parm_depth);
3178 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3179 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3180 = TREE_VEC_ELT (targs, i);
3181 targs = new_targs;
3184 return instantiate_template (tmpl, targs, tf_error);
3187 /* If we thought that the DECL was a member function, but it
3188 turns out to be specializing a static member function,
3189 make DECL a static member function as well. */
3190 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3191 && DECL_STATIC_FUNCTION_P (tmpl)
3192 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3193 revert_static_member_fn (decl);
3195 /* If this is a specialization of a member template of a
3196 template class, we want to return the TEMPLATE_DECL, not
3197 the specialization of it. */
3198 if (tsk == tsk_template && !was_template_id)
3200 tree result = DECL_TEMPLATE_RESULT (tmpl);
3201 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3202 DECL_INITIAL (result) = NULL_TREE;
3203 if (have_def)
3205 tree parm;
3206 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3207 DECL_SOURCE_LOCATION (result)
3208 = DECL_SOURCE_LOCATION (decl);
3209 /* We want to use the argument list specified in the
3210 definition, not in the original declaration. */
3211 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3212 for (parm = DECL_ARGUMENTS (result); parm;
3213 parm = DECL_CHAIN (parm))
3214 DECL_CONTEXT (parm) = result;
3216 return register_specialization (tmpl, gen_tmpl, targs,
3217 is_friend, 0);
3220 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3221 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3223 if (was_template_id)
3224 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3226 /* Inherit default function arguments from the template
3227 DECL is specializing. */
3228 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3229 copy_default_args_to_explicit_spec (decl);
3231 /* This specialization has the same protection as the
3232 template it specializes. */
3233 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3234 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3236 /* 7.1.1-1 [dcl.stc]
3238 A storage-class-specifier shall not be specified in an
3239 explicit specialization...
3241 The parser rejects these, so unless action is taken here,
3242 explicit function specializations will always appear with
3243 global linkage.
3245 The action recommended by the C++ CWG in response to C++
3246 defect report 605 is to make the storage class and linkage
3247 of the explicit specialization match the templated function:
3249 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3251 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3253 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3254 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3256 /* A concept cannot be specialized. */
3257 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3259 error ("explicit specialization of function concept %qD",
3260 gen_tmpl);
3261 return error_mark_node;
3264 /* This specialization has the same linkage and visibility as
3265 the function template it specializes. */
3266 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3267 if (! TREE_PUBLIC (decl))
3269 DECL_INTERFACE_KNOWN (decl) = 1;
3270 DECL_NOT_REALLY_EXTERN (decl) = 1;
3272 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3273 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3275 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3276 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3280 /* If DECL is a friend declaration, declared using an
3281 unqualified name, the namespace associated with DECL may
3282 have been set incorrectly. For example, in:
3284 template <typename T> void f(T);
3285 namespace N {
3286 struct S { friend void f<int>(int); }
3289 we will have set the DECL_CONTEXT for the friend
3290 declaration to N, rather than to the global namespace. */
3291 if (DECL_NAMESPACE_SCOPE_P (decl))
3292 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3294 if (is_friend && !have_def)
3295 /* This is not really a declaration of a specialization.
3296 It's just the name of an instantiation. But, it's not
3297 a request for an instantiation, either. */
3298 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3299 else if (TREE_CODE (decl) == FUNCTION_DECL)
3300 /* A specialization is not necessarily COMDAT. */
3301 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3302 && DECL_DECLARED_INLINE_P (decl));
3303 else if (VAR_P (decl))
3304 DECL_COMDAT (decl) = false;
3306 /* If this is a full specialization, register it so that we can find
3307 it again. Partial specializations will be registered in
3308 process_partial_specialization. */
3309 if (!processing_template_decl)
3311 warn_spec_missing_attributes (gen_tmpl, decl, attrlist);
3313 decl = register_specialization (decl, gen_tmpl, targs,
3314 is_friend, 0);
3318 /* A 'structor should already have clones. */
3319 gcc_assert (decl == error_mark_node
3320 || variable_template_p (tmpl)
3321 || !(DECL_CONSTRUCTOR_P (decl)
3322 || DECL_DESTRUCTOR_P (decl))
3323 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3327 return decl;
3330 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3331 parameters. These are represented in the same format used for
3332 DECL_TEMPLATE_PARMS. */
3335 comp_template_parms (const_tree parms1, const_tree parms2)
3337 const_tree p1;
3338 const_tree p2;
3340 if (parms1 == parms2)
3341 return 1;
3343 for (p1 = parms1, p2 = parms2;
3344 p1 != NULL_TREE && p2 != NULL_TREE;
3345 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3347 tree t1 = TREE_VALUE (p1);
3348 tree t2 = TREE_VALUE (p2);
3349 int i;
3351 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3352 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3354 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3355 return 0;
3357 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3359 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3360 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3362 /* If either of the template parameters are invalid, assume
3363 they match for the sake of error recovery. */
3364 if (error_operand_p (parm1) || error_operand_p (parm2))
3365 return 1;
3367 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3368 return 0;
3370 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3371 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3372 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3373 continue;
3374 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3375 return 0;
3379 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3380 /* One set of parameters has more parameters lists than the
3381 other. */
3382 return 0;
3384 return 1;
3387 /* Returns true if two template parameters are declared with
3388 equivalent constraints. */
3390 static bool
3391 template_parameter_constraints_equivalent_p (const_tree parm1, const_tree parm2)
3393 tree req1 = TREE_TYPE (parm1);
3394 tree req2 = TREE_TYPE (parm2);
3395 if (!req1 != !req2)
3396 return false;
3397 if (req1)
3398 return cp_tree_equal (req1, req2);
3399 return true;
3402 /* Returns true when two template parameters are equivalent. */
3404 static bool
3405 template_parameters_equivalent_p (const_tree parm1, const_tree parm2)
3407 tree decl1 = TREE_VALUE (parm1);
3408 tree decl2 = TREE_VALUE (parm2);
3410 /* If either of the template parameters are invalid, assume
3411 they match for the sake of error recovery. */
3412 if (error_operand_p (decl1) || error_operand_p (decl2))
3413 return true;
3415 /* ... they declare parameters of the same kind. */
3416 if (TREE_CODE (decl1) != TREE_CODE (decl2))
3417 return false;
3419 /* ... one parameter was introduced by a parameter declaration, then
3420 both are. This case arises as a result of eagerly rewriting declarations
3421 during parsing. */
3422 if (DECL_VIRTUAL_P (decl1) != DECL_VIRTUAL_P (decl2))
3423 return false;
3425 /* ... if either declares a pack, they both do. */
3426 if (template_parameter_pack_p (decl1) != template_parameter_pack_p (decl2))
3427 return false;
3429 if (TREE_CODE (decl1) == PARM_DECL)
3431 /* ... if they declare non-type parameters, the types are equivalent. */
3432 if (!same_type_p (TREE_TYPE (decl1), TREE_TYPE (decl2)))
3433 return false;
3435 else if (TREE_CODE (decl2) == TEMPLATE_DECL)
3437 /* ... if they declare template template parameters, their template
3438 parameter lists are equivalent. */
3439 if (!template_heads_equivalent_p (decl1, decl2))
3440 return false;
3443 /* ... if they are declared with a qualified-concept name, they both
3444 are, and those names are equivalent. */
3445 return template_parameter_constraints_equivalent_p (parm1, parm2);
3448 /* Returns true if two template parameters lists are equivalent.
3449 Two template parameter lists are equivalent if they have the
3450 same length and their corresponding parameters are equivalent.
3452 PARMS1 and PARMS2 are TREE_LISTs containing TREE_VECs: the
3453 data structure returned by DECL_TEMPLATE_PARMS.
3455 This is generally the same implementation as comp_template_parms
3456 except that it also the concept names and arguments used to
3457 introduce parameters. */
3459 static bool
3460 template_parameter_lists_equivalent_p (const_tree parms1, const_tree parms2)
3462 if (parms1 == parms2)
3463 return true;
3465 const_tree p1 = parms1;
3466 const_tree p2 = parms2;
3467 while (p1 != NULL_TREE && p2 != NULL_TREE)
3469 tree list1 = TREE_VALUE (p1);
3470 tree list2 = TREE_VALUE (p2);
3472 if (TREE_VEC_LENGTH (list1) != TREE_VEC_LENGTH (list2))
3473 return 0;
3475 for (int i = 0; i < TREE_VEC_LENGTH (list2); ++i)
3477 tree parm1 = TREE_VEC_ELT (list1, i);
3478 tree parm2 = TREE_VEC_ELT (list2, i);
3479 if (!template_parameters_equivalent_p (parm1, parm2))
3480 return false;
3483 p1 = TREE_CHAIN (p1);
3484 p2 = TREE_CHAIN (p2);
3487 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3488 return false;
3490 return true;
3493 /* Return true if the requires-clause of the template parameter lists are
3494 equivalent and false otherwise. */
3495 static bool
3496 template_requirements_equivalent_p (const_tree parms1, const_tree parms2)
3498 tree req1 = TEMPLATE_PARMS_CONSTRAINTS (parms1);
3499 tree req2 = TEMPLATE_PARMS_CONSTRAINTS (parms2);
3500 if ((req1 != NULL_TREE) != (req2 != NULL_TREE))
3501 return false;
3502 if (!cp_tree_equal (req1, req2))
3503 return false;
3504 return true;
3507 /* Returns true if two template heads are equivalent. 17.6.6.1p6:
3508 Two template heads are equivalent if their template parameter
3509 lists are equivalent and their requires clauses are equivalent.
3511 In pre-C++20, this is equivalent to calling comp_template_parms
3512 for the template parameters of TMPL1 and TMPL2. */
3514 bool
3515 template_heads_equivalent_p (const_tree tmpl1, const_tree tmpl2)
3517 tree parms1 = DECL_TEMPLATE_PARMS (tmpl1);
3518 tree parms2 = DECL_TEMPLATE_PARMS (tmpl2);
3520 /* Don't change the matching rules for pre-C++20. */
3521 if (cxx_dialect < cxx20)
3522 return comp_template_parms (parms1, parms2);
3524 /* ... have the same number of template parameters, and their
3525 corresponding parameters are equivalent. */
3526 if (!template_parameter_lists_equivalent_p (parms1, parms2))
3527 return false;
3529 /* ... if either has a requires-clause, they both do and their
3530 corresponding constraint-expressions are equivalent. */
3531 return template_requirements_equivalent_p (parms1, parms2);
3534 /* Determine whether PARM is a parameter pack. */
3536 bool
3537 template_parameter_pack_p (const_tree parm)
3539 /* Determine if we have a non-type template parameter pack. */
3540 if (TREE_CODE (parm) == PARM_DECL)
3541 return (DECL_TEMPLATE_PARM_P (parm)
3542 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3543 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3544 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3546 /* If this is a list of template parameters, we could get a
3547 TYPE_DECL or a TEMPLATE_DECL. */
3548 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3549 parm = TREE_TYPE (parm);
3551 /* Otherwise it must be a type template parameter. */
3552 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3553 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3554 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3557 /* Determine if T is a function parameter pack. */
3559 bool
3560 function_parameter_pack_p (const_tree t)
3562 if (t && TREE_CODE (t) == PARM_DECL)
3563 return DECL_PACK_P (t);
3564 return false;
3567 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3568 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3570 tree
3571 get_function_template_decl (const_tree primary_func_tmpl_inst)
3573 if (! primary_func_tmpl_inst
3574 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3575 || ! primary_template_specialization_p (primary_func_tmpl_inst))
3576 return NULL;
3578 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3581 /* Return true iff the function parameter PARAM_DECL was expanded
3582 from the function parameter pack PACK. */
3584 bool
3585 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3587 if (DECL_ARTIFICIAL (param_decl)
3588 || !function_parameter_pack_p (pack))
3589 return false;
3591 /* The parameter pack and its pack arguments have the same
3592 DECL_PARM_INDEX. */
3593 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3596 /* Determine whether ARGS describes a variadic template args list,
3597 i.e., one that is terminated by a template argument pack. */
3599 static bool
3600 template_args_variadic_p (tree args)
3602 int nargs;
3603 tree last_parm;
3605 if (args == NULL_TREE)
3606 return false;
3608 args = INNERMOST_TEMPLATE_ARGS (args);
3609 nargs = TREE_VEC_LENGTH (args);
3611 if (nargs == 0)
3612 return false;
3614 last_parm = TREE_VEC_ELT (args, nargs - 1);
3616 return ARGUMENT_PACK_P (last_parm);
3619 /* Generate a new name for the parameter pack name NAME (an
3620 IDENTIFIER_NODE) that incorporates its */
3622 static tree
3623 make_ith_pack_parameter_name (tree name, int i)
3625 /* Munge the name to include the parameter index. */
3626 #define NUMBUF_LEN 128
3627 char numbuf[NUMBUF_LEN];
3628 char* newname;
3629 int newname_len;
3631 if (name == NULL_TREE)
3632 return name;
3633 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3634 newname_len = IDENTIFIER_LENGTH (name)
3635 + strlen (numbuf) + 2;
3636 newname = (char*)alloca (newname_len);
3637 snprintf (newname, newname_len,
3638 "%s#%i", IDENTIFIER_POINTER (name), i);
3639 return get_identifier (newname);
3642 /* Return true if T is a primary function, class or alias template
3643 specialization, not including the template pattern. */
3645 bool
3646 primary_template_specialization_p (const_tree t)
3648 if (!t)
3649 return false;
3651 if (VAR_OR_FUNCTION_DECL_P (t))
3652 return (DECL_LANG_SPECIFIC (t)
3653 && DECL_USE_TEMPLATE (t)
3654 && DECL_TEMPLATE_INFO (t)
3655 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
3656 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3657 return (CLASSTYPE_TEMPLATE_INFO (t)
3658 && CLASSTYPE_USE_TEMPLATE (t)
3659 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
3660 else if (alias_template_specialization_p (t, nt_transparent))
3661 return true;
3662 return false;
3665 /* Return true if PARM is a template template parameter. */
3667 bool
3668 template_template_parameter_p (const_tree parm)
3670 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3673 /* Return true iff PARM is a DECL representing a type template
3674 parameter. */
3676 bool
3677 template_type_parameter_p (const_tree parm)
3679 return (parm
3680 && (TREE_CODE (parm) == TYPE_DECL
3681 || TREE_CODE (parm) == TEMPLATE_DECL)
3682 && DECL_TEMPLATE_PARM_P (parm));
3685 /* Return the template parameters of T if T is a
3686 primary template instantiation, NULL otherwise. */
3688 tree
3689 get_primary_template_innermost_parameters (const_tree t)
3691 tree parms = NULL, template_info = NULL;
3693 if ((template_info = get_template_info (t))
3694 && primary_template_specialization_p (t))
3695 parms = INNERMOST_TEMPLATE_PARMS
3696 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3698 return parms;
3701 /* Returns the template arguments of T if T is a template instantiation,
3702 NULL otherwise. */
3704 tree
3705 get_template_innermost_arguments (const_tree t)
3707 tree args = NULL, template_info = NULL;
3709 if ((template_info = get_template_info (t))
3710 && TI_ARGS (template_info))
3711 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3713 return args;
3716 /* Return the argument pack elements of T if T is a template argument pack,
3717 NULL otherwise. */
3719 tree
3720 get_template_argument_pack_elems (const_tree t)
3722 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3723 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3724 return NULL;
3726 return ARGUMENT_PACK_ARGS (t);
3729 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3730 ARGUMENT_PACK_SELECT represents. */
3732 static tree
3733 argument_pack_select_arg (tree t)
3735 tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
3736 tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
3738 /* If the selected argument is an expansion E, that most likely means we were
3739 called from gen_elem_of_pack_expansion_instantiation during the
3740 substituting of an argument pack (of which the Ith element is a pack
3741 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3742 In this case, the Ith element resulting from this substituting is going to
3743 be a pack expansion, which pattern is the pattern of E. Let's return the
3744 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3745 resulting pack expansion from it. */
3746 if (PACK_EXPANSION_P (arg))
3748 /* Make sure we aren't throwing away arg info. */
3749 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
3750 arg = PACK_EXPANSION_PATTERN (arg);
3753 return arg;
3757 /* True iff FN is a function representing a built-in variadic parameter
3758 pack. */
3760 bool
3761 builtin_pack_fn_p (tree fn)
3763 if (!fn
3764 || TREE_CODE (fn) != FUNCTION_DECL
3765 || !DECL_IS_UNDECLARED_BUILTIN (fn))
3766 return false;
3768 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3769 return true;
3771 return false;
3774 /* True iff CALL is a call to a function representing a built-in variadic
3775 parameter pack. */
3777 static bool
3778 builtin_pack_call_p (tree call)
3780 if (TREE_CODE (call) != CALL_EXPR)
3781 return false;
3782 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3785 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3787 static tree
3788 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3789 tree in_decl)
3791 tree ohi = CALL_EXPR_ARG (call, 0);
3792 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3793 false/*fn*/, true/*int_cst*/);
3795 if (value_dependent_expression_p (hi))
3797 if (hi != ohi)
3799 call = copy_node (call);
3800 CALL_EXPR_ARG (call, 0) = hi;
3802 tree ex = make_pack_expansion (call, complain);
3803 tree vec = make_tree_vec (1);
3804 TREE_VEC_ELT (vec, 0) = ex;
3805 return vec;
3807 else
3809 hi = cxx_constant_value (hi);
3810 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3812 /* Calculate the largest value of len that won't make the size of the vec
3813 overflow an int. The compiler will exceed resource limits long before
3814 this, but it seems a decent place to diagnose. */
3815 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3817 if (len < 0 || len > max)
3819 if ((complain & tf_error)
3820 && hi != error_mark_node)
3821 error ("argument to %<__integer_pack%> must be between 0 and %d",
3822 max);
3823 return error_mark_node;
3826 tree vec = make_tree_vec (len);
3828 for (int i = 0; i < len; ++i)
3829 TREE_VEC_ELT (vec, i) = size_int (i);
3831 return vec;
3835 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3836 CALL. */
3838 static tree
3839 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3840 tree in_decl)
3842 if (!builtin_pack_call_p (call))
3843 return NULL_TREE;
3845 tree fn = CALL_EXPR_FN (call);
3847 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3848 return expand_integer_pack (call, args, complain, in_decl);
3850 return NULL_TREE;
3853 /* Structure used to track the progress of find_parameter_packs_r. */
3854 struct find_parameter_pack_data
3856 /* TREE_LIST that will contain all of the parameter packs found by
3857 the traversal. */
3858 tree* parameter_packs;
3860 /* Set of AST nodes that have been visited by the traversal. */
3861 hash_set<tree> *visited;
3863 /* True iff we're making a type pack expansion. */
3864 bool type_pack_expansion_p;
3867 /* Identifies all of the argument packs that occur in a template
3868 argument and appends them to the TREE_LIST inside DATA, which is a
3869 find_parameter_pack_data structure. This is a subroutine of
3870 make_pack_expansion and uses_parameter_packs. */
3871 static tree
3872 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3874 tree t = *tp;
3875 struct find_parameter_pack_data* ppd =
3876 (struct find_parameter_pack_data*)data;
3877 bool parameter_pack_p = false;
3879 #define WALK_SUBTREE(NODE) \
3880 cp_walk_tree (&(NODE), &find_parameter_packs_r, \
3881 ppd, ppd->visited) \
3883 /* Don't look through typedefs; we are interested in whether a
3884 parameter pack is actually written in the expression/type we're
3885 looking at, not the target type. */
3886 if (TYPE_P (t) && typedef_variant_p (t))
3888 /* But do look at arguments for an alias template. */
3889 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3890 cp_walk_tree (&TI_ARGS (tinfo),
3891 &find_parameter_packs_r,
3892 ppd, ppd->visited);
3893 *walk_subtrees = 0;
3894 return NULL_TREE;
3897 /* Identify whether this is a parameter pack or not. */
3898 switch (TREE_CODE (t))
3900 case TEMPLATE_PARM_INDEX:
3901 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3902 parameter_pack_p = true;
3903 break;
3905 case TEMPLATE_TYPE_PARM:
3906 t = TYPE_MAIN_VARIANT (t);
3907 /* FALLTHRU */
3908 case TEMPLATE_TEMPLATE_PARM:
3909 /* If the placeholder appears in the decl-specifier-seq of a function
3910 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3911 is a pack expansion, the invented template parameter is a template
3912 parameter pack. */
3913 if (ppd->type_pack_expansion_p && is_auto (t))
3914 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3915 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3916 parameter_pack_p = true;
3917 break;
3919 case FIELD_DECL:
3920 case PARM_DECL:
3921 if (DECL_PACK_P (t))
3923 /* We don't want to walk into the type of a PARM_DECL,
3924 because we don't want to see the type parameter pack. */
3925 *walk_subtrees = 0;
3926 parameter_pack_p = true;
3928 break;
3930 case VAR_DECL:
3931 if (DECL_PACK_P (t))
3933 /* We don't want to walk into the type of a variadic capture proxy,
3934 because we don't want to see the type parameter pack. */
3935 *walk_subtrees = 0;
3936 parameter_pack_p = true;
3938 else if (variable_template_specialization_p (t))
3940 cp_walk_tree (&DECL_TI_ARGS (t),
3941 find_parameter_packs_r,
3942 ppd, ppd->visited);
3943 *walk_subtrees = 0;
3945 break;
3947 case CALL_EXPR:
3948 if (builtin_pack_call_p (t))
3949 parameter_pack_p = true;
3950 break;
3952 case BASES:
3953 parameter_pack_p = true;
3954 break;
3955 default:
3956 /* Not a parameter pack. */
3957 break;
3960 if (parameter_pack_p)
3962 /* Add this parameter pack to the list. */
3963 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3966 if (TYPE_P (t))
3967 cp_walk_tree (&TYPE_CONTEXT (t),
3968 &find_parameter_packs_r, ppd, ppd->visited);
3970 /* This switch statement will return immediately if we don't find a
3971 parameter pack. ??? Should some of these be in cp_walk_subtrees? */
3972 switch (TREE_CODE (t))
3974 case BOUND_TEMPLATE_TEMPLATE_PARM:
3975 /* Check the template itself. */
3976 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3977 &find_parameter_packs_r, ppd, ppd->visited);
3978 return NULL_TREE;
3980 case DECL_EXPR:
3982 tree decl = DECL_EXPR_DECL (t);
3983 /* Ignore the declaration of a capture proxy for a parameter pack. */
3984 if (is_capture_proxy (decl))
3985 *walk_subtrees = 0;
3986 if (is_typedef_decl (decl))
3987 /* Since we stop at typedefs above, we need to look through them at
3988 the point of the DECL_EXPR. */
3989 cp_walk_tree (&DECL_ORIGINAL_TYPE (decl),
3990 &find_parameter_packs_r, ppd, ppd->visited);
3991 return NULL_TREE;
3994 case TEMPLATE_DECL:
3995 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3996 return NULL_TREE;
3997 cp_walk_tree (&TREE_TYPE (t),
3998 &find_parameter_packs_r, ppd, ppd->visited);
3999 return NULL_TREE;
4001 case TYPE_PACK_EXPANSION:
4002 case EXPR_PACK_EXPANSION:
4003 *walk_subtrees = 0;
4004 return NULL_TREE;
4006 case INTEGER_TYPE:
4007 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
4008 ppd, ppd->visited);
4009 *walk_subtrees = 0;
4010 return NULL_TREE;
4012 case IDENTIFIER_NODE:
4013 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
4014 ppd->visited);
4015 *walk_subtrees = 0;
4016 return NULL_TREE;
4018 case LAMBDA_EXPR:
4020 /* Since we defer implicit capture, look in the parms and body. */
4021 tree fn = lambda_function (t);
4022 cp_walk_tree (&TREE_TYPE (fn), &find_parameter_packs_r, ppd,
4023 ppd->visited);
4024 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
4025 ppd->visited);
4026 return NULL_TREE;
4029 case DECLTYPE_TYPE:
4031 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
4032 type_pack_expansion_p to false so that any placeholders
4033 within the expression don't get marked as parameter packs. */
4034 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
4035 ppd->type_pack_expansion_p = false;
4036 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
4037 ppd, ppd->visited);
4038 ppd->type_pack_expansion_p = type_pack_expansion_p;
4039 *walk_subtrees = 0;
4040 return NULL_TREE;
4043 case IF_STMT:
4044 cp_walk_tree (&IF_COND (t), &find_parameter_packs_r,
4045 ppd, ppd->visited);
4046 cp_walk_tree (&THEN_CLAUSE (t), &find_parameter_packs_r,
4047 ppd, ppd->visited);
4048 cp_walk_tree (&ELSE_CLAUSE (t), &find_parameter_packs_r,
4049 ppd, ppd->visited);
4050 /* Don't walk into IF_STMT_EXTRA_ARGS. */
4051 *walk_subtrees = 0;
4052 return NULL_TREE;
4054 case TAG_DEFN:
4055 t = TREE_TYPE (t);
4056 if (CLASS_TYPE_P (t))
4057 /* Local class, need to look through the whole definition. */
4058 for (tree bb : BINFO_BASE_BINFOS (TYPE_BINFO (t)))
4059 cp_walk_tree (&BINFO_TYPE (bb), &find_parameter_packs_r,
4060 ppd, ppd->visited);
4061 else
4062 /* Enum, look at the values. */
4063 for (tree l = TYPE_VALUES (t); l; l = TREE_CHAIN (l))
4064 cp_walk_tree (&DECL_INITIAL (TREE_VALUE (l)),
4065 &find_parameter_packs_r,
4066 ppd, ppd->visited);
4067 return NULL_TREE;
4069 case FUNCTION_TYPE:
4070 case METHOD_TYPE:
4071 WALK_SUBTREE (TYPE_RAISES_EXCEPTIONS (t));
4072 break;
4074 default:
4075 return NULL_TREE;
4078 #undef WALK_SUBTREE
4080 return NULL_TREE;
4083 /* Determines if the expression or type T uses any parameter packs. */
4084 tree
4085 uses_parameter_packs (tree t)
4087 tree parameter_packs = NULL_TREE;
4088 struct find_parameter_pack_data ppd;
4089 ppd.parameter_packs = &parameter_packs;
4090 ppd.visited = new hash_set<tree>;
4091 ppd.type_pack_expansion_p = false;
4092 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4093 delete ppd.visited;
4094 return parameter_packs;
4097 /* Turn ARG, which may be an expression, type, or a TREE_LIST
4098 representation a base-class initializer into a parameter pack
4099 expansion. If all goes well, the resulting node will be an
4100 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
4101 respectively. */
4102 tree
4103 make_pack_expansion (tree arg, tsubst_flags_t complain)
4105 tree result;
4106 tree parameter_packs = NULL_TREE;
4107 bool for_types = false;
4108 struct find_parameter_pack_data ppd;
4110 if (!arg || arg == error_mark_node)
4111 return arg;
4113 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
4115 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
4116 class initializer. In this case, the TREE_PURPOSE will be a
4117 _TYPE node (representing the base class expansion we're
4118 initializing) and the TREE_VALUE will be a TREE_LIST
4119 containing the initialization arguments.
4121 The resulting expansion looks somewhat different from most
4122 expansions. Rather than returning just one _EXPANSION, we
4123 return a TREE_LIST whose TREE_PURPOSE is a
4124 TYPE_PACK_EXPANSION containing the bases that will be
4125 initialized. The TREE_VALUE will be identical to the
4126 original TREE_VALUE, which is a list of arguments that will
4127 be passed to each base. We do not introduce any new pack
4128 expansion nodes into the TREE_VALUE (although it is possible
4129 that some already exist), because the TREE_PURPOSE and
4130 TREE_VALUE all need to be expanded together with the same
4131 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
4132 resulting TREE_PURPOSE will mention the parameter packs in
4133 both the bases and the arguments to the bases. */
4134 tree purpose;
4135 tree value;
4136 tree parameter_packs = NULL_TREE;
4138 /* Determine which parameter packs will be used by the base
4139 class expansion. */
4140 ppd.visited = new hash_set<tree>;
4141 ppd.parameter_packs = &parameter_packs;
4142 ppd.type_pack_expansion_p = false;
4143 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
4144 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
4145 &ppd, ppd.visited);
4147 if (parameter_packs == NULL_TREE)
4149 if (complain & tf_error)
4150 error ("base initializer expansion %qT contains no parameter packs",
4151 arg);
4152 delete ppd.visited;
4153 return error_mark_node;
4156 if (TREE_VALUE (arg) != void_type_node)
4158 /* Collect the sets of parameter packs used in each of the
4159 initialization arguments. */
4160 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
4162 /* Determine which parameter packs will be expanded in this
4163 argument. */
4164 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
4165 &ppd, ppd.visited);
4169 delete ppd.visited;
4171 /* Create the pack expansion type for the base type. */
4172 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
4173 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
4174 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
4175 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
4177 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4178 they will rarely be compared to anything. */
4179 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
4181 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
4184 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
4185 for_types = true;
4187 /* Build the PACK_EXPANSION_* node. */
4188 result = for_types
4189 ? cxx_make_type (TYPE_PACK_EXPANSION)
4190 : make_node (EXPR_PACK_EXPANSION);
4191 SET_PACK_EXPANSION_PATTERN (result, arg);
4192 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
4194 /* Propagate type and const-expression information. */
4195 TREE_TYPE (result) = TREE_TYPE (arg);
4196 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
4197 /* Mark this read now, since the expansion might be length 0. */
4198 mark_exp_read (arg);
4200 else
4201 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
4202 they will rarely be compared to anything. */
4203 SET_TYPE_STRUCTURAL_EQUALITY (result);
4205 /* Determine which parameter packs will be expanded. */
4206 ppd.parameter_packs = &parameter_packs;
4207 ppd.visited = new hash_set<tree>;
4208 ppd.type_pack_expansion_p = TYPE_P (arg);
4209 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
4210 delete ppd.visited;
4212 /* Make sure we found some parameter packs. */
4213 if (parameter_packs == NULL_TREE)
4215 if (complain & tf_error)
4217 if (TYPE_P (arg))
4218 error ("expansion pattern %qT contains no parameter packs", arg);
4219 else
4220 error ("expansion pattern %qE contains no parameter packs", arg);
4222 return error_mark_node;
4224 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
4226 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
4228 return result;
4231 /* Checks T for any "bare" parameter packs, which have not yet been
4232 expanded, and issues an error if any are found. This operation can
4233 only be done on full expressions or types (e.g., an expression
4234 statement, "if" condition, etc.), because we could have expressions like:
4236 foo(f(g(h(args)))...)
4238 where "args" is a parameter pack. check_for_bare_parameter_packs
4239 should not be called for the subexpressions args, h(args),
4240 g(h(args)), or f(g(h(args))), because we would produce erroneous
4241 error messages.
4243 Returns TRUE and emits an error if there were bare parameter packs,
4244 returns FALSE otherwise. */
4245 bool
4246 check_for_bare_parameter_packs (tree t, location_t loc /* = UNKNOWN_LOCATION */)
4248 tree parameter_packs = NULL_TREE;
4249 struct find_parameter_pack_data ppd;
4251 if (!processing_template_decl || !t || t == error_mark_node)
4252 return false;
4254 if (TREE_CODE (t) == TYPE_DECL)
4255 t = TREE_TYPE (t);
4257 ppd.parameter_packs = &parameter_packs;
4258 ppd.visited = new hash_set<tree>;
4259 ppd.type_pack_expansion_p = false;
4260 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4261 delete ppd.visited;
4263 /* It's OK for a lambda to have an unexpanded parameter pack from the
4264 containing context, but do complain about unexpanded capture packs. */
4265 if (current_class_type && LAMBDA_TYPE_P (current_class_type)
4266 && CLASSTYPE_TEMPLATE_INFO (current_class_type))
4267 for (; parameter_packs;
4268 parameter_packs = TREE_CHAIN (parameter_packs))
4270 tree pack = TREE_VALUE (parameter_packs);
4271 if (is_capture_proxy (pack))
4272 break;
4275 if (parameter_packs)
4277 if (loc == UNKNOWN_LOCATION)
4278 loc = cp_expr_loc_or_input_loc (t);
4279 error_at (loc, "parameter packs not expanded with %<...%>:");
4280 while (parameter_packs)
4282 tree pack = TREE_VALUE (parameter_packs);
4283 tree name = NULL_TREE;
4285 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
4286 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
4287 name = TYPE_NAME (pack);
4288 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
4289 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
4290 else if (TREE_CODE (pack) == CALL_EXPR)
4291 name = DECL_NAME (CALL_EXPR_FN (pack));
4292 else
4293 name = DECL_NAME (pack);
4295 if (name)
4296 inform (loc, " %qD", name);
4297 else
4298 inform (loc, " %s", "<anonymous>");
4300 parameter_packs = TREE_CHAIN (parameter_packs);
4303 return true;
4306 return false;
4309 /* Expand any parameter packs that occur in the template arguments in
4310 ARGS. */
4311 tree
4312 expand_template_argument_pack (tree args)
4314 if (args == error_mark_node)
4315 return error_mark_node;
4317 tree result_args = NULL_TREE;
4318 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
4319 int num_result_args = -1;
4320 int non_default_args_count = -1;
4322 /* First, determine if we need to expand anything, and the number of
4323 slots we'll need. */
4324 for (in_arg = 0; in_arg < nargs; ++in_arg)
4326 tree arg = TREE_VEC_ELT (args, in_arg);
4327 if (arg == NULL_TREE)
4328 return args;
4329 if (ARGUMENT_PACK_P (arg))
4331 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
4332 if (num_result_args < 0)
4333 num_result_args = in_arg + num_packed;
4334 else
4335 num_result_args += num_packed;
4337 else
4339 if (num_result_args >= 0)
4340 num_result_args++;
4344 /* If no expansion is necessary, we're done. */
4345 if (num_result_args < 0)
4346 return args;
4348 /* Expand arguments. */
4349 result_args = make_tree_vec (num_result_args);
4350 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
4351 non_default_args_count =
4352 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
4353 for (in_arg = 0; in_arg < nargs; ++in_arg)
4355 tree arg = TREE_VEC_ELT (args, in_arg);
4356 if (ARGUMENT_PACK_P (arg))
4358 tree packed = ARGUMENT_PACK_ARGS (arg);
4359 int i, num_packed = TREE_VEC_LENGTH (packed);
4360 for (i = 0; i < num_packed; ++i, ++out_arg)
4361 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
4362 if (non_default_args_count > 0)
4363 non_default_args_count += num_packed - 1;
4365 else
4367 TREE_VEC_ELT (result_args, out_arg) = arg;
4368 ++out_arg;
4371 if (non_default_args_count >= 0)
4372 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
4373 return result_args;
4376 /* Checks if DECL shadows a template parameter.
4378 [temp.local]: A template-parameter shall not be redeclared within its
4379 scope (including nested scopes).
4381 Emits an error and returns TRUE if the DECL shadows a parameter,
4382 returns FALSE otherwise. */
4384 bool
4385 check_template_shadow (tree decl)
4387 tree olddecl;
4389 /* If we're not in a template, we can't possibly shadow a template
4390 parameter. */
4391 if (!current_template_parms)
4392 return true;
4394 /* Figure out what we're shadowing. */
4395 decl = OVL_FIRST (decl);
4396 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4398 /* If there's no previous binding for this name, we're not shadowing
4399 anything, let alone a template parameter. */
4400 if (!olddecl)
4401 return true;
4403 /* If we're not shadowing a template parameter, we're done. Note
4404 that OLDDECL might be an OVERLOAD (or perhaps even an
4405 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4406 node. */
4407 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4408 return true;
4410 /* We check for decl != olddecl to avoid bogus errors for using a
4411 name inside a class. We check TPFI to avoid duplicate errors for
4412 inline member templates. */
4413 if (decl == olddecl
4414 || (DECL_TEMPLATE_PARM_P (decl)
4415 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4416 return true;
4418 /* Don't complain about the injected class name, as we've already
4419 complained about the class itself. */
4420 if (DECL_SELF_REFERENCE_P (decl))
4421 return false;
4423 if (DECL_TEMPLATE_PARM_P (decl))
4424 error ("declaration of template parameter %q+D shadows "
4425 "template parameter", decl);
4426 else
4427 error ("declaration of %q+#D shadows template parameter", decl);
4428 inform (DECL_SOURCE_LOCATION (olddecl),
4429 "template parameter %qD declared here", olddecl);
4430 return false;
4433 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4434 ORIG_LEVEL, DECL, and TYPE. */
4436 static tree
4437 build_template_parm_index (int index,
4438 int level,
4439 int orig_level,
4440 tree decl,
4441 tree type)
4443 tree t = make_node (TEMPLATE_PARM_INDEX);
4444 TEMPLATE_PARM_IDX (t) = index;
4445 TEMPLATE_PARM_LEVEL (t) = level;
4446 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4447 TEMPLATE_PARM_DECL (t) = decl;
4448 TREE_TYPE (t) = type;
4449 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4450 TREE_READONLY (t) = TREE_READONLY (decl);
4452 return t;
4455 /* Find the canonical type parameter for the given template type
4456 parameter. Returns the canonical type parameter, which may be TYPE
4457 if no such parameter existed. */
4459 tree
4460 canonical_type_parameter (tree type)
4462 int idx = TEMPLATE_TYPE_IDX (type);
4464 gcc_assert (TREE_CODE (type) != TEMPLATE_TEMPLATE_PARM);
4466 if (vec_safe_length (canonical_template_parms) <= (unsigned) idx)
4467 vec_safe_grow_cleared (canonical_template_parms, idx + 1, true);
4469 for (tree list = (*canonical_template_parms)[idx];
4470 list; list = TREE_CHAIN (list))
4471 if (comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4472 return TREE_VALUE (list);
4474 (*canonical_template_parms)[idx]
4475 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4476 return type;
4479 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4480 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4481 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4482 new one is created. */
4484 static tree
4485 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4486 tsubst_flags_t complain)
4488 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4489 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4490 != TEMPLATE_PARM_LEVEL (index) - levels)
4491 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4493 tree orig_decl = TEMPLATE_PARM_DECL (index);
4495 tree decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4496 TREE_CODE (orig_decl), DECL_NAME (orig_decl),
4497 type);
4498 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4499 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4500 DECL_VIRTUAL_P (decl) = DECL_VIRTUAL_P (orig_decl);
4501 DECL_ARTIFICIAL (decl) = 1;
4502 SET_DECL_TEMPLATE_PARM_P (decl);
4504 tree tpi = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4505 TEMPLATE_PARM_LEVEL (index) - levels,
4506 TEMPLATE_PARM_ORIG_LEVEL (index),
4507 decl, type);
4508 TEMPLATE_PARM_DESCENDANTS (index) = tpi;
4509 TEMPLATE_PARM_PARAMETER_PACK (tpi)
4510 = TEMPLATE_PARM_PARAMETER_PACK (index);
4512 /* Template template parameters need this. */
4513 tree inner = decl;
4514 if (TREE_CODE (decl) == TEMPLATE_DECL)
4516 inner = build_decl (DECL_SOURCE_LOCATION (decl),
4517 TYPE_DECL, DECL_NAME (decl), type);
4518 DECL_TEMPLATE_RESULT (decl) = inner;
4519 DECL_ARTIFICIAL (inner) = true;
4520 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4521 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4524 /* Attach the TPI to the decl. */
4525 if (TREE_CODE (inner) == TYPE_DECL)
4526 TEMPLATE_TYPE_PARM_INDEX (type) = tpi;
4527 else
4528 DECL_INITIAL (decl) = tpi;
4531 return TEMPLATE_PARM_DESCENDANTS (index);
4534 /* Process information from new template parameter PARM and append it
4535 to the LIST being built. This new parameter is a non-type
4536 parameter iff IS_NON_TYPE is true. This new parameter is a
4537 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4538 is in PARM_LOC. */
4540 tree
4541 process_template_parm (tree list, location_t parm_loc, tree parm,
4542 bool is_non_type, bool is_parameter_pack)
4544 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4545 tree prev = NULL_TREE;
4546 int idx = 0;
4548 if (list)
4550 prev = tree_last (list);
4552 tree p = TREE_VALUE (prev);
4553 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4554 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4555 else if (TREE_CODE (p) == PARM_DECL)
4556 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4558 ++idx;
4561 tree decl = NULL_TREE;
4562 tree defval = TREE_PURPOSE (parm);
4563 tree constr = TREE_TYPE (parm);
4565 if (is_non_type)
4567 parm = TREE_VALUE (parm);
4569 SET_DECL_TEMPLATE_PARM_P (parm);
4571 if (TREE_TYPE (parm) != error_mark_node)
4573 /* [temp.param]
4575 The top-level cv-qualifiers on the template-parameter are
4576 ignored when determining its type. */
4577 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4578 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4579 TREE_TYPE (parm) = error_mark_node;
4580 else if (uses_parameter_packs (TREE_TYPE (parm))
4581 && !is_parameter_pack
4582 /* If we're in a nested template parameter list, the template
4583 template parameter could be a parameter pack. */
4584 && processing_template_parmlist == 1)
4586 /* This template parameter is not a parameter pack, but it
4587 should be. Complain about "bare" parameter packs. */
4588 check_for_bare_parameter_packs (TREE_TYPE (parm));
4590 /* Recover by calling this a parameter pack. */
4591 is_parameter_pack = true;
4595 /* A template parameter is not modifiable. */
4596 TREE_CONSTANT (parm) = 1;
4597 TREE_READONLY (parm) = 1;
4598 decl = build_decl (parm_loc,
4599 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4600 TREE_CONSTANT (decl) = 1;
4601 TREE_READONLY (decl) = 1;
4602 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4603 = build_template_parm_index (idx, processing_template_decl,
4604 processing_template_decl,
4605 decl, TREE_TYPE (parm));
4607 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4608 = is_parameter_pack;
4610 else
4612 tree t;
4613 parm = TREE_VALUE (TREE_VALUE (parm));
4615 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4617 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4618 /* This is for distinguishing between real templates and template
4619 template parameters */
4620 TREE_TYPE (parm) = t;
4622 /* any_template_parm_r expects to be able to get the targs of a
4623 DECL_TEMPLATE_RESULT. */
4624 tree result = DECL_TEMPLATE_RESULT (parm);
4625 TREE_TYPE (result) = t;
4626 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (parm));
4627 tree tinfo = build_template_info (parm, args);
4628 retrofit_lang_decl (result);
4629 DECL_TEMPLATE_INFO (result) = tinfo;
4631 decl = parm;
4633 else
4635 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4636 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4637 decl = build_decl (parm_loc,
4638 TYPE_DECL, parm, t);
4641 TYPE_NAME (t) = decl;
4642 TYPE_STUB_DECL (t) = decl;
4643 parm = decl;
4644 TEMPLATE_TYPE_PARM_INDEX (t)
4645 = build_template_parm_index (idx, processing_template_decl,
4646 processing_template_decl,
4647 decl, TREE_TYPE (parm));
4648 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4649 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
4650 SET_TYPE_STRUCTURAL_EQUALITY (t);
4651 else
4652 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4654 DECL_ARTIFICIAL (decl) = 1;
4655 SET_DECL_TEMPLATE_PARM_P (decl);
4657 /* Build requirements for the type/template parameter.
4658 This must be done after SET_DECL_TEMPLATE_PARM_P or
4659 process_template_parm could fail. */
4660 tree reqs = finish_shorthand_constraint (parm, constr);
4662 decl = pushdecl (decl);
4663 if (!is_non_type)
4664 parm = decl;
4666 /* Build the parameter node linking the parameter declaration,
4667 its default argument (if any), and its constraints (if any). */
4668 parm = build_tree_list (defval, parm);
4669 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4671 if (prev)
4672 TREE_CHAIN (prev) = parm;
4673 else
4674 list = parm;
4676 return list;
4679 /* The end of a template parameter list has been reached. Process the
4680 tree list into a parameter vector, converting each parameter into a more
4681 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4682 as PARM_DECLs. */
4684 tree
4685 end_template_parm_list (tree parms)
4687 tree saved_parmlist = make_tree_vec (list_length (parms));
4689 /* Pop the dummy parameter level and add the real one. We do not
4690 morph the dummy parameter in place, as it might have been
4691 captured by a (nested) template-template-parm. */
4692 current_template_parms = TREE_CHAIN (current_template_parms);
4694 current_template_parms
4695 = tree_cons (size_int (processing_template_decl),
4696 saved_parmlist, current_template_parms);
4698 for (unsigned ix = 0; parms; ix++)
4700 tree parm = parms;
4701 parms = TREE_CHAIN (parms);
4702 TREE_CHAIN (parm) = NULL_TREE;
4704 TREE_VEC_ELT (saved_parmlist, ix) = parm;
4707 --processing_template_parmlist;
4709 return saved_parmlist;
4712 // Explicitly indicate the end of the template parameter list. We assume
4713 // that the current template parameters have been constructed and/or
4714 // managed explicitly, as when creating new template template parameters
4715 // from a shorthand constraint.
4716 void
4717 end_template_parm_list ()
4719 --processing_template_parmlist;
4722 /* end_template_decl is called after a template declaration is seen. */
4724 void
4725 end_template_decl (void)
4727 reset_specialization ();
4729 if (! processing_template_decl)
4730 return;
4732 /* This matches the pushlevel in begin_template_parm_list. */
4733 finish_scope ();
4735 --processing_template_decl;
4736 current_template_parms = TREE_CHAIN (current_template_parms);
4739 /* Takes a TEMPLATE_PARM_P or DECL_TEMPLATE_PARM_P node or a TREE_LIST
4740 thereof, and converts it into an argument suitable to be passed to
4741 the type substitution functions. Note that if the TREE_LIST contains
4742 an error_mark node, the returned argument is error_mark_node. */
4744 tree
4745 template_parm_to_arg (tree t)
4747 if (!t)
4748 return NULL_TREE;
4750 if (TREE_CODE (t) == TREE_LIST)
4751 t = TREE_VALUE (t);
4753 if (error_operand_p (t))
4754 return error_mark_node;
4756 if (DECL_P (t) && DECL_TEMPLATE_PARM_P (t))
4758 if (TREE_CODE (t) == TYPE_DECL
4759 || TREE_CODE (t) == TEMPLATE_DECL)
4760 t = TREE_TYPE (t);
4761 else
4762 t = DECL_INITIAL (t);
4765 gcc_assert (TEMPLATE_PARM_P (t));
4767 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
4768 || TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM)
4770 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4772 /* Turn this argument into a TYPE_ARGUMENT_PACK
4773 with a single element, which expands T. */
4774 tree vec = make_tree_vec (1);
4775 if (CHECKING_P)
4776 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4778 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4780 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4781 SET_ARGUMENT_PACK_ARGS (t, vec);
4784 else
4786 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4788 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4789 with a single element, which expands T. */
4790 tree vec = make_tree_vec (1);
4791 if (CHECKING_P)
4792 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4794 t = convert_from_reference (t);
4795 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4797 t = make_node (NONTYPE_ARGUMENT_PACK);
4798 SET_ARGUMENT_PACK_ARGS (t, vec);
4800 else
4801 t = convert_from_reference (t);
4803 return t;
4806 /* Given a single level of template parameters (a TREE_VEC), return it
4807 as a set of template arguments. */
4809 tree
4810 template_parms_level_to_args (tree parms)
4812 tree a = copy_node (parms);
4813 TREE_TYPE (a) = NULL_TREE;
4814 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4815 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4817 if (CHECKING_P)
4818 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4820 return a;
4823 /* Given a set of template parameters, return them as a set of template
4824 arguments. The template parameters are represented as a TREE_VEC, in
4825 the form documented in cp-tree.h for template arguments. */
4827 tree
4828 template_parms_to_args (tree parms)
4830 tree header;
4831 tree args = NULL_TREE;
4832 int length = TMPL_PARMS_DEPTH (parms);
4833 int l = length;
4835 /* If there is only one level of template parameters, we do not
4836 create a TREE_VEC of TREE_VECs. Instead, we return a single
4837 TREE_VEC containing the arguments. */
4838 if (length > 1)
4839 args = make_tree_vec (length);
4841 for (header = parms; header; header = TREE_CHAIN (header))
4843 tree a = template_parms_level_to_args (TREE_VALUE (header));
4845 if (length > 1)
4846 TREE_VEC_ELT (args, --l) = a;
4847 else
4848 args = a;
4851 return args;
4854 /* Within the declaration of a template, return the currently active
4855 template parameters as an argument TREE_VEC. */
4857 static tree
4858 current_template_args (void)
4860 return template_parms_to_args (current_template_parms);
4863 /* Return the fully generic arguments for of TMPL, i.e. what
4864 current_template_args would be while parsing it. */
4866 tree
4867 generic_targs_for (tree tmpl)
4869 if (tmpl == NULL_TREE)
4870 return NULL_TREE;
4871 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
4872 || DECL_TEMPLATE_SPECIALIZATION (tmpl))
4873 /* DECL_TEMPLATE_RESULT doesn't have the arguments we want. For a template
4874 template parameter, it has no TEMPLATE_INFO; for a partial
4875 specialization, it has the arguments for the primary template, and we
4876 want the arguments for the partial specialization. */;
4877 else if (tree result = DECL_TEMPLATE_RESULT (tmpl))
4878 if (tree ti = get_template_info (result))
4879 return TI_ARGS (ti);
4880 return template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl));
4883 /* Update the declared TYPE by doing any lookups which were thought to be
4884 dependent, but are not now that we know the SCOPE of the declarator. */
4886 tree
4887 maybe_update_decl_type (tree orig_type, tree scope)
4889 tree type = orig_type;
4891 if (type == NULL_TREE)
4892 return type;
4894 if (TREE_CODE (orig_type) == TYPE_DECL)
4895 type = TREE_TYPE (type);
4897 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4898 && dependent_type_p (type)
4899 /* Don't bother building up the args in this case. */
4900 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4902 /* tsubst in the args corresponding to the template parameters,
4903 including auto if present. Most things will be unchanged, but
4904 make_typename_type and tsubst_qualified_id will resolve
4905 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4906 tree args = current_template_args ();
4907 tree auto_node = type_uses_auto (type);
4908 tree pushed;
4909 if (auto_node)
4911 tree auto_vec = make_tree_vec (1);
4912 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4913 args = add_to_template_args (args, auto_vec);
4915 pushed = push_scope (scope);
4916 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4917 if (pushed)
4918 pop_scope (scope);
4921 if (type == error_mark_node)
4922 return orig_type;
4924 if (TREE_CODE (orig_type) == TYPE_DECL)
4926 if (same_type_p (type, TREE_TYPE (orig_type)))
4927 type = orig_type;
4928 else
4929 type = TYPE_NAME (type);
4931 return type;
4934 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4935 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4936 the new template is a member template. */
4938 static tree
4939 build_template_decl (tree decl, tree parms, bool member_template_p)
4941 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4942 SET_DECL_LANGUAGE (tmpl, DECL_LANGUAGE (decl));
4943 DECL_TEMPLATE_PARMS (tmpl) = parms;
4944 DECL_TEMPLATE_RESULT (tmpl) = decl;
4945 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4946 TREE_TYPE (tmpl) = TREE_TYPE (decl);
4947 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4948 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4950 /* Propagate module information from the decl. */
4951 DECL_MODULE_EXPORT_P (tmpl) = DECL_MODULE_EXPORT_P (decl);
4953 return tmpl;
4956 struct template_parm_data
4958 /* The level of the template parameters we are currently
4959 processing. */
4960 int level;
4962 /* The index of the specialization argument we are currently
4963 processing. */
4964 int current_arg;
4966 /* An array whose size is the number of template parameters. The
4967 elements are nonzero if the parameter has been used in any one
4968 of the arguments processed so far. */
4969 int* parms;
4971 /* An array whose size is the number of template arguments. The
4972 elements are nonzero if the argument makes use of template
4973 parameters of this level. */
4974 int* arg_uses_template_parms;
4977 /* Subroutine of push_template_decl used to see if each template
4978 parameter in a partial specialization is used in the explicit
4979 argument list. If T is of the LEVEL given in DATA (which is
4980 treated as a template_parm_data*), then DATA->PARMS is marked
4981 appropriately. */
4983 static int
4984 mark_template_parm (tree t, void* data)
4986 int level;
4987 int idx;
4988 struct template_parm_data* tpd = (struct template_parm_data*) data;
4990 template_parm_level_and_index (t, &level, &idx);
4992 if (level == tpd->level)
4994 tpd->parms[idx] = 1;
4995 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4998 /* In C++17 the type of a non-type argument is a deduced context. */
4999 if (cxx_dialect >= cxx17
5000 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5001 for_each_template_parm (TREE_TYPE (t),
5002 &mark_template_parm,
5003 data,
5004 NULL,
5005 /*include_nondeduced_p=*/false);
5007 /* Return zero so that for_each_template_parm will continue the
5008 traversal of the tree; we want to mark *every* template parm. */
5009 return 0;
5012 /* Process the partial specialization DECL. */
5014 static tree
5015 process_partial_specialization (tree decl)
5017 tree type = TREE_TYPE (decl);
5018 tree tinfo = get_template_info (decl);
5019 tree maintmpl = TI_TEMPLATE (tinfo);
5020 tree specargs = TI_ARGS (tinfo);
5021 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
5022 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
5023 tree inner_parms;
5024 tree inst;
5025 int nargs = TREE_VEC_LENGTH (inner_args);
5026 int ntparms;
5027 int i;
5028 bool did_error_intro = false;
5029 struct template_parm_data tpd;
5030 struct template_parm_data tpd2;
5032 gcc_assert (current_template_parms);
5034 /* A concept cannot be specialized. */
5035 if (flag_concepts && variable_concept_p (maintmpl))
5037 error ("specialization of variable concept %q#D", maintmpl);
5038 return error_mark_node;
5041 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5042 ntparms = TREE_VEC_LENGTH (inner_parms);
5044 /* We check that each of the template parameters given in the
5045 partial specialization is used in the argument list to the
5046 specialization. For example:
5048 template <class T> struct S;
5049 template <class T> struct S<T*>;
5051 The second declaration is OK because `T*' uses the template
5052 parameter T, whereas
5054 template <class T> struct S<int>;
5056 is no good. Even trickier is:
5058 template <class T>
5059 struct S1
5061 template <class U>
5062 struct S2;
5063 template <class U>
5064 struct S2<T>;
5067 The S2<T> declaration is actually invalid; it is a
5068 full-specialization. Of course,
5070 template <class U>
5071 struct S2<T (*)(U)>;
5073 or some such would have been OK. */
5074 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
5075 tpd.parms = XALLOCAVEC (int, ntparms);
5076 memset (tpd.parms, 0, sizeof (int) * ntparms);
5078 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
5079 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
5080 for (i = 0; i < nargs; ++i)
5082 tpd.current_arg = i;
5083 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
5084 &mark_template_parm,
5085 &tpd,
5086 NULL,
5087 /*include_nondeduced_p=*/false);
5089 for (i = 0; i < ntparms; ++i)
5090 if (tpd.parms[i] == 0)
5092 /* One of the template parms was not used in a deduced context in the
5093 specialization. */
5094 if (!did_error_intro)
5096 error ("template parameters not deducible in "
5097 "partial specialization:");
5098 did_error_intro = true;
5101 inform (input_location, " %qD",
5102 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
5105 if (did_error_intro)
5106 return error_mark_node;
5108 /* [temp.class.spec]
5110 The argument list of the specialization shall not be identical to
5111 the implicit argument list of the primary template. */
5112 tree main_args
5113 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
5114 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
5115 && (!flag_concepts
5116 || !strictly_subsumes (current_template_constraints (), maintmpl)))
5118 if (!flag_concepts)
5119 error ("partial specialization %q+D does not specialize "
5120 "any template arguments; to define the primary template, "
5121 "remove the template argument list", decl);
5122 else
5123 error ("partial specialization %q+D does not specialize any "
5124 "template arguments and is not more constrained than "
5125 "the primary template; to define the primary template, "
5126 "remove the template argument list", decl);
5127 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5130 /* A partial specialization that replaces multiple parameters of the
5131 primary template with a pack expansion is less specialized for those
5132 parameters. */
5133 if (nargs < DECL_NTPARMS (maintmpl))
5135 error ("partial specialization is not more specialized than the "
5136 "primary template because it replaces multiple parameters "
5137 "with a pack expansion");
5138 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5139 /* Avoid crash in process_partial_specialization. */
5140 return decl;
5143 else if (nargs > DECL_NTPARMS (maintmpl))
5145 error ("too many arguments for partial specialization %qT", type);
5146 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
5147 /* Avoid crash below. */
5148 return decl;
5151 /* If we aren't in a dependent class, we can actually try deduction. */
5152 else if (tpd.level == 1
5153 /* FIXME we should be able to handle a partial specialization of a
5154 partial instantiation, but currently we can't (c++/41727). */
5155 && TMPL_ARGS_DEPTH (specargs) == 1
5156 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
5158 auto_diagnostic_group d;
5159 if (permerror (input_location, "partial specialization %qD is not "
5160 "more specialized than", decl))
5161 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
5162 maintmpl);
5165 /* [temp.spec.partial]
5167 The type of a template parameter corresponding to a specialized
5168 non-type argument shall not be dependent on a parameter of the
5169 specialization.
5171 Also, we verify that pack expansions only occur at the
5172 end of the argument list. */
5173 tpd2.parms = 0;
5174 for (i = 0; i < nargs; ++i)
5176 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
5177 tree arg = TREE_VEC_ELT (inner_args, i);
5178 tree packed_args = NULL_TREE;
5179 int j, len = 1;
5181 if (ARGUMENT_PACK_P (arg))
5183 /* Extract the arguments from the argument pack. We'll be
5184 iterating over these in the following loop. */
5185 packed_args = ARGUMENT_PACK_ARGS (arg);
5186 len = TREE_VEC_LENGTH (packed_args);
5189 for (j = 0; j < len; j++)
5191 if (packed_args)
5192 /* Get the Jth argument in the parameter pack. */
5193 arg = TREE_VEC_ELT (packed_args, j);
5195 if (PACK_EXPANSION_P (arg))
5197 /* Pack expansions must come at the end of the
5198 argument list. */
5199 if ((packed_args && j < len - 1)
5200 || (!packed_args && i < nargs - 1))
5202 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5203 error ("parameter pack argument %qE must be at the "
5204 "end of the template argument list", arg);
5205 else
5206 error ("parameter pack argument %qT must be at the "
5207 "end of the template argument list", arg);
5211 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
5212 /* We only care about the pattern. */
5213 arg = PACK_EXPANSION_PATTERN (arg);
5215 if (/* These first two lines are the `non-type' bit. */
5216 !TYPE_P (arg)
5217 && TREE_CODE (arg) != TEMPLATE_DECL
5218 /* This next two lines are the `argument expression is not just a
5219 simple identifier' condition and also the `specialized
5220 non-type argument' bit. */
5221 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
5222 && !((REFERENCE_REF_P (arg)
5223 || TREE_CODE (arg) == VIEW_CONVERT_EXPR)
5224 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
5226 /* Look at the corresponding template parameter,
5227 marking which template parameters its type depends
5228 upon. */
5229 tree type = TREE_TYPE (parm);
5231 if (!tpd2.parms)
5233 /* We haven't yet initialized TPD2. Do so now. */
5234 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
5235 /* The number of parameters here is the number in the
5236 main template, which, as checked in the assertion
5237 above, is NARGS. */
5238 tpd2.parms = XALLOCAVEC (int, nargs);
5239 tpd2.level =
5240 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
5243 /* Mark the template parameters. But this time, we're
5244 looking for the template parameters of the main
5245 template, not in the specialization. */
5246 tpd2.current_arg = i;
5247 tpd2.arg_uses_template_parms[i] = 0;
5248 memset (tpd2.parms, 0, sizeof (int) * nargs);
5249 for_each_template_parm (type,
5250 &mark_template_parm,
5251 &tpd2,
5252 NULL,
5253 /*include_nondeduced_p=*/false);
5255 if (tpd2.arg_uses_template_parms [i])
5257 /* The type depended on some template parameters.
5258 If they are fully specialized in the
5259 specialization, that's OK. */
5260 int j;
5261 int count = 0;
5262 for (j = 0; j < nargs; ++j)
5263 if (tpd2.parms[j] != 0
5264 && tpd.arg_uses_template_parms [j])
5265 ++count;
5266 if (count != 0)
5267 error_n (input_location, count,
5268 "type %qT of template argument %qE depends "
5269 "on a template parameter",
5270 "type %qT of template argument %qE depends "
5271 "on template parameters",
5272 type,
5273 arg);
5279 /* We should only get here once. */
5280 if (TREE_CODE (decl) == TYPE_DECL)
5281 gcc_assert (!COMPLETE_TYPE_P (type));
5283 // Build the template decl.
5284 tree tmpl = build_template_decl (decl, current_template_parms,
5285 DECL_MEMBER_TEMPLATE_P (maintmpl));
5286 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5287 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
5288 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
5290 /* Give template template parms a DECL_CONTEXT of the template
5291 for which they are a parameter. */
5292 for (i = 0; i < ntparms; ++i)
5294 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
5295 if (TREE_CODE (parm) == TEMPLATE_DECL)
5296 DECL_CONTEXT (parm) = tmpl;
5299 if (VAR_P (decl))
5300 /* We didn't register this in check_explicit_specialization so we could
5301 wait until the constraints were set. */
5302 decl = register_specialization (decl, maintmpl, specargs, false, 0);
5303 else
5304 associate_classtype_constraints (type);
5306 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
5307 = tree_cons (specargs, tmpl,
5308 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
5309 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
5311 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
5312 inst = TREE_CHAIN (inst))
5314 tree instance = TREE_VALUE (inst);
5315 if (TYPE_P (instance)
5316 ? (COMPLETE_TYPE_P (instance)
5317 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
5318 : DECL_TEMPLATE_INSTANTIATION (instance))
5320 tree spec = most_specialized_partial_spec (instance, tf_none);
5321 tree inst_decl = (DECL_P (instance)
5322 ? instance : TYPE_NAME (instance));
5323 if (!spec)
5324 /* OK */;
5325 else if (spec == error_mark_node)
5326 permerror (input_location,
5327 "declaration of %qD ambiguates earlier template "
5328 "instantiation for %qD", decl, inst_decl);
5329 else if (TREE_VALUE (spec) == tmpl)
5330 permerror (input_location,
5331 "partial specialization of %qD after instantiation "
5332 "of %qD", decl, inst_decl);
5336 return decl;
5339 /* PARM is a template parameter of some form; return the corresponding
5340 TEMPLATE_PARM_INDEX. */
5342 static tree
5343 get_template_parm_index (tree parm)
5345 if (TREE_CODE (parm) == PARM_DECL
5346 || TREE_CODE (parm) == CONST_DECL)
5347 parm = DECL_INITIAL (parm);
5348 else if (TREE_CODE (parm) == TYPE_DECL
5349 || TREE_CODE (parm) == TEMPLATE_DECL)
5350 parm = TREE_TYPE (parm);
5351 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
5352 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
5353 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
5354 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
5355 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
5356 return parm;
5359 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5360 parameter packs used by the template parameter PARM. */
5362 static void
5363 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
5365 /* A type parm can't refer to another parm. */
5366 if (TREE_CODE (parm) == TYPE_DECL || parm == error_mark_node)
5367 return;
5368 else if (TREE_CODE (parm) == PARM_DECL)
5370 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
5371 ppd, ppd->visited);
5372 return;
5375 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
5377 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
5378 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
5380 tree p = TREE_VALUE (TREE_VEC_ELT (vec, i));
5381 if (template_parameter_pack_p (p))
5382 /* Any packs in the type are expanded by this parameter. */;
5383 else
5384 fixed_parameter_pack_p_1 (p, ppd);
5388 /* PARM is a template parameter pack. Return any parameter packs used in
5389 its type or the type of any of its template parameters. If there are
5390 any such packs, it will be instantiated into a fixed template parameter
5391 list by partial instantiation rather than be fully deduced. */
5393 tree
5394 fixed_parameter_pack_p (tree parm)
5396 /* This can only be true in a member template. */
5397 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
5398 return NULL_TREE;
5399 /* This can only be true for a parameter pack. */
5400 if (!template_parameter_pack_p (parm))
5401 return NULL_TREE;
5402 /* A type parm can't refer to another parm. */
5403 if (TREE_CODE (parm) == TYPE_DECL)
5404 return NULL_TREE;
5406 tree parameter_packs = NULL_TREE;
5407 struct find_parameter_pack_data ppd;
5408 ppd.parameter_packs = &parameter_packs;
5409 ppd.visited = new hash_set<tree>;
5410 ppd.type_pack_expansion_p = false;
5412 fixed_parameter_pack_p_1 (parm, &ppd);
5414 delete ppd.visited;
5415 return parameter_packs;
5418 /* Check that a template declaration's use of default arguments and
5419 parameter packs is not invalid. Here, PARMS are the template
5420 parameters. IS_PRIMARY is true if DECL is the thing declared by
5421 a primary template. IS_PARTIAL is true if DECL is a partial
5422 specialization.
5424 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5425 function template declaration or a friend class template
5426 declaration. In the function case, 1 indicates a declaration, 2
5427 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5428 emitted for extraneous default arguments.
5430 Returns TRUE if there were no errors found, FALSE otherwise. */
5432 bool
5433 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
5434 bool is_partial, int is_friend_decl)
5436 const char *msg;
5437 int last_level_to_check;
5438 tree parm_level;
5439 bool no_errors = true;
5441 /* [temp.param]
5443 A default template-argument shall not be specified in a
5444 function template declaration or a function template definition, nor
5445 in the template-parameter-list of the definition of a member of a
5446 class template. */
5448 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5449 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_DECL_P (decl)))
5450 /* You can't have a function template declaration in a local
5451 scope, nor you can you define a member of a class template in a
5452 local scope. */
5453 return true;
5455 if ((TREE_CODE (decl) == TYPE_DECL
5456 && TREE_TYPE (decl)
5457 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5458 || (TREE_CODE (decl) == FUNCTION_DECL
5459 && LAMBDA_FUNCTION_P (decl)))
5460 /* A lambda doesn't have an explicit declaration; don't complain
5461 about the parms of the enclosing class. */
5462 return true;
5464 if (current_class_type
5465 && !TYPE_BEING_DEFINED (current_class_type)
5466 && DECL_LANG_SPECIFIC (decl)
5467 && DECL_DECLARES_FUNCTION_P (decl)
5468 /* If this is either a friend defined in the scope of the class
5469 or a member function. */
5470 && (DECL_FUNCTION_MEMBER_P (decl)
5471 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5472 : DECL_FRIEND_CONTEXT (decl)
5473 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5474 : false)
5475 /* And, if it was a member function, it really was defined in
5476 the scope of the class. */
5477 && (!DECL_FUNCTION_MEMBER_P (decl)
5478 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5479 /* We already checked these parameters when the template was
5480 declared, so there's no need to do it again now. This function
5481 was defined in class scope, but we're processing its body now
5482 that the class is complete. */
5483 return true;
5485 /* Core issue 226 (C++0x only): the following only applies to class
5486 templates. */
5487 if (is_primary
5488 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5490 /* [temp.param]
5492 If a template-parameter has a default template-argument, all
5493 subsequent template-parameters shall have a default
5494 template-argument supplied. */
5495 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5497 tree inner_parms = TREE_VALUE (parm_level);
5498 int ntparms = TREE_VEC_LENGTH (inner_parms);
5499 int seen_def_arg_p = 0;
5500 int i;
5502 for (i = 0; i < ntparms; ++i)
5504 tree parm = TREE_VEC_ELT (inner_parms, i);
5506 if (parm == error_mark_node)
5507 continue;
5509 if (TREE_PURPOSE (parm))
5510 seen_def_arg_p = 1;
5511 else if (seen_def_arg_p
5512 && !template_parameter_pack_p (TREE_VALUE (parm)))
5514 error ("no default argument for %qD", TREE_VALUE (parm));
5515 /* For better subsequent error-recovery, we indicate that
5516 there should have been a default argument. */
5517 TREE_PURPOSE (parm) = error_mark_node;
5518 no_errors = false;
5520 else if (!is_partial
5521 && !is_friend_decl
5522 /* Don't complain about an enclosing partial
5523 specialization. */
5524 && parm_level == parms
5525 && (TREE_CODE (decl) == TYPE_DECL || VAR_P (decl))
5526 && i < ntparms - 1
5527 && template_parameter_pack_p (TREE_VALUE (parm))
5528 /* A fixed parameter pack will be partially
5529 instantiated into a fixed length list. */
5530 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5532 /* A primary class template, primary variable template
5533 (DR 2032), or alias template can only have one
5534 parameter pack, at the end of the template
5535 parameter list. */
5537 error ("parameter pack %q+D must be at the end of the"
5538 " template parameter list", TREE_VALUE (parm));
5540 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5541 = error_mark_node;
5542 no_errors = false;
5548 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5549 || is_partial
5550 || !is_primary
5551 || is_friend_decl)
5552 /* For an ordinary class template, default template arguments are
5553 allowed at the innermost level, e.g.:
5554 template <class T = int>
5555 struct S {};
5556 but, in a partial specialization, they're not allowed even
5557 there, as we have in [temp.class.spec]:
5559 The template parameter list of a specialization shall not
5560 contain default template argument values.
5562 So, for a partial specialization, or for a function template
5563 (in C++98/C++03), we look at all of them. */
5565 else
5566 /* But, for a primary class template that is not a partial
5567 specialization we look at all template parameters except the
5568 innermost ones. */
5569 parms = TREE_CHAIN (parms);
5571 /* Figure out what error message to issue. */
5572 if (is_friend_decl == 2)
5573 msg = G_("default template arguments may not be used in function template "
5574 "friend re-declaration");
5575 else if (is_friend_decl)
5576 msg = G_("default template arguments may not be used in template "
5577 "friend declarations");
5578 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5579 msg = G_("default template arguments may not be used in function templates "
5580 "without %<-std=c++11%> or %<-std=gnu++11%>");
5581 else if (is_partial)
5582 msg = G_("default template arguments may not be used in "
5583 "partial specializations");
5584 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5585 msg = G_("default argument for template parameter for class enclosing %qD");
5586 else
5587 /* Per [temp.param]/9, "A default template-argument shall not be
5588 specified in the template-parameter-lists of the definition of
5589 a member of a class template that appears outside of the member's
5590 class.", thus if we aren't handling a member of a class template
5591 there is no need to examine the parameters. */
5592 return true;
5594 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5595 /* If we're inside a class definition, there's no need to
5596 examine the parameters to the class itself. On the one
5597 hand, they will be checked when the class is defined, and,
5598 on the other, default arguments are valid in things like:
5599 template <class T = double>
5600 struct S { template <class U> void f(U); };
5601 Here the default argument for `S' has no bearing on the
5602 declaration of `f'. */
5603 last_level_to_check = template_class_depth (current_class_type) + 1;
5604 else
5605 /* Check everything. */
5606 last_level_to_check = 0;
5608 for (parm_level = parms;
5609 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5610 parm_level = TREE_CHAIN (parm_level))
5612 tree inner_parms = TREE_VALUE (parm_level);
5613 int i;
5614 int ntparms;
5616 ntparms = TREE_VEC_LENGTH (inner_parms);
5617 for (i = 0; i < ntparms; ++i)
5619 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5620 continue;
5622 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5624 if (msg)
5626 no_errors = false;
5627 if (is_friend_decl == 2)
5628 return no_errors;
5630 error (msg, decl);
5631 msg = 0;
5634 /* Clear out the default argument so that we are not
5635 confused later. */
5636 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5640 /* At this point, if we're still interested in issuing messages,
5641 they must apply to classes surrounding the object declared. */
5642 if (msg)
5643 msg = G_("default argument for template parameter for class "
5644 "enclosing %qD");
5647 return no_errors;
5650 /* Worker for push_template_decl_real, called via
5651 for_each_template_parm. DATA is really an int, indicating the
5652 level of the parameters we are interested in. If T is a template
5653 parameter of that level, return nonzero. */
5655 static int
5656 template_parm_this_level_p (tree t, void* data)
5658 int this_level = *(int *)data;
5659 int level;
5661 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5662 level = TEMPLATE_PARM_LEVEL (t);
5663 else
5664 level = TEMPLATE_TYPE_LEVEL (t);
5665 return level == this_level;
5668 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5669 DATA is really an int, indicating the innermost outer level of parameters.
5670 If T is a template parameter of that level or further out, return
5671 nonzero. */
5673 static int
5674 template_parm_outer_level (tree t, void *data)
5676 int this_level = *(int *)data;
5677 int level;
5679 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5680 level = TEMPLATE_PARM_LEVEL (t);
5681 else
5682 level = TEMPLATE_TYPE_LEVEL (t);
5683 return level <= this_level;
5686 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5687 parameters given by current_template_args, or reuses a
5688 previously existing one, if appropriate. Returns the DECL, or an
5689 equivalent one, if it is replaced via a call to duplicate_decls.
5691 If IS_FRIEND is true, DECL is a friend declaration. */
5693 tree
5694 push_template_decl (tree decl, bool is_friend)
5696 if (decl == error_mark_node || !current_template_parms)
5697 return error_mark_node;
5699 /* See if this is a partial specialization. */
5700 bool is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5701 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5702 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5703 || (VAR_P (decl)
5704 && DECL_LANG_SPECIFIC (decl)
5705 && DECL_TEMPLATE_SPECIALIZATION (decl)
5706 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5708 /* No surprising friend functions. */
5709 gcc_checking_assert (is_friend
5710 || !(TREE_CODE (decl) == FUNCTION_DECL
5711 && DECL_UNIQUE_FRIEND_P (decl)));
5713 tree ctx;
5714 if (is_friend)
5715 /* For a friend, we want the context of the friend, not
5716 the type of which it is a friend. */
5717 ctx = CP_DECL_CONTEXT (decl);
5718 else if (CP_DECL_CONTEXT (decl)
5719 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5720 /* In the case of a virtual function, we want the class in which
5721 it is defined. */
5722 ctx = CP_DECL_CONTEXT (decl);
5723 else
5724 /* Otherwise, if we're currently defining some class, the DECL
5725 is assumed to be a member of the class. */
5726 ctx = current_scope ();
5728 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5729 ctx = NULL_TREE;
5731 if (!DECL_CONTEXT (decl))
5732 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5734 /* See if this is a primary template. */
5735 bool is_primary = false;
5736 if (is_friend && ctx
5737 && uses_template_parms_level (ctx, processing_template_decl))
5738 /* A friend template that specifies a class context, i.e.
5739 template <typename T> friend void A<T>::f();
5740 is not primary. */
5742 else if (TREE_CODE (decl) == TYPE_DECL && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5743 /* Lambdas are not primary. */
5745 else
5746 is_primary = template_parm_scope_p ();
5748 /* True if the template is a member template, in the sense of
5749 [temp.mem]. */
5750 bool member_template_p = false;
5752 if (is_primary)
5754 warning (OPT_Wtemplates, "template %qD declared", decl);
5756 if (DECL_CLASS_SCOPE_P (decl))
5757 member_template_p = true;
5759 if (TREE_CODE (decl) == TYPE_DECL
5760 && IDENTIFIER_ANON_P (DECL_NAME (decl)))
5762 error ("template class without a name");
5763 return error_mark_node;
5765 else if (TREE_CODE (decl) == FUNCTION_DECL)
5767 if (member_template_p)
5769 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5770 error ("member template %qD may not have virt-specifiers", decl);
5772 if (DECL_DESTRUCTOR_P (decl))
5774 /* [temp.mem]
5776 A destructor shall not be a member template. */
5777 error_at (DECL_SOURCE_LOCATION (decl),
5778 "destructor %qD declared as member template", decl);
5779 return error_mark_node;
5781 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5782 && (!prototype_p (TREE_TYPE (decl))
5783 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5784 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5785 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5786 == void_list_node)))
5788 /* [basic.stc.dynamic.allocation]
5790 An allocation function can be a function
5791 template. ... Template allocation functions shall
5792 have two or more parameters. */
5793 error ("invalid template declaration of %qD", decl);
5794 return error_mark_node;
5797 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5798 && CLASS_TYPE_P (TREE_TYPE (decl)))
5800 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5801 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5802 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5804 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5805 if (TREE_CODE (t) == TYPE_DECL)
5806 t = TREE_TYPE (t);
5807 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5808 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5811 else if (TREE_CODE (decl) == TYPE_DECL
5812 && TYPE_DECL_ALIAS_P (decl))
5813 /* alias-declaration */
5814 gcc_assert (!DECL_ARTIFICIAL (decl));
5815 else if (VAR_P (decl))
5816 /* C++14 variable template. */;
5817 else if (TREE_CODE (decl) == CONCEPT_DECL)
5818 /* C++20 concept definitions. */;
5819 else
5821 error ("template declaration of %q#D", decl);
5822 return error_mark_node;
5826 bool local_p = (!DECL_IMPLICIT_TYPEDEF_P (decl)
5827 && ((ctx && TREE_CODE (ctx) == FUNCTION_DECL)
5828 || (VAR_OR_FUNCTION_DECL_P (decl)
5829 && DECL_LOCAL_DECL_P (decl))));
5831 /* Check to see that the rules regarding the use of default
5832 arguments are not being violated. We check args for a friend
5833 functions when we know whether it's a definition, introducing
5834 declaration or re-declaration. */
5835 if (!local_p && (!is_friend || TREE_CODE (decl) != FUNCTION_DECL))
5836 check_default_tmpl_args (decl, current_template_parms,
5837 is_primary, is_partial, is_friend);
5839 /* Ensure that there are no parameter packs in the type of this
5840 declaration that have not been expanded. */
5841 if (TREE_CODE (decl) == FUNCTION_DECL)
5843 /* Check each of the arguments individually to see if there are
5844 any bare parameter packs. */
5845 tree type = TREE_TYPE (decl);
5846 tree arg = DECL_ARGUMENTS (decl);
5847 tree argtype = TYPE_ARG_TYPES (type);
5849 while (arg && argtype)
5851 if (!DECL_PACK_P (arg)
5852 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5854 /* This is a PARM_DECL that contains unexpanded parameter
5855 packs. We have already complained about this in the
5856 check_for_bare_parameter_packs call, so just replace
5857 these types with ERROR_MARK_NODE. */
5858 TREE_TYPE (arg) = error_mark_node;
5859 TREE_VALUE (argtype) = error_mark_node;
5862 arg = DECL_CHAIN (arg);
5863 argtype = TREE_CHAIN (argtype);
5866 /* Check for bare parameter packs in the return type and the
5867 exception specifiers. */
5868 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5869 /* Errors were already issued, set return type to int
5870 as the frontend doesn't expect error_mark_node as
5871 the return type. */
5872 TREE_TYPE (type) = integer_type_node;
5873 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5874 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5876 else if (check_for_bare_parameter_packs (is_typedef_decl (decl)
5877 ? DECL_ORIGINAL_TYPE (decl)
5878 : TREE_TYPE (decl)))
5880 TREE_TYPE (decl) = error_mark_node;
5881 return error_mark_node;
5884 if (is_partial)
5885 return process_partial_specialization (decl);
5887 tree args = current_template_args ();
5888 tree tmpl = NULL_TREE;
5889 bool new_template_p = false;
5890 if (local_p)
5892 /* Does not get a template head. */
5893 tmpl = NULL_TREE;
5894 gcc_checking_assert (!is_primary);
5896 else if (!ctx
5897 || TREE_CODE (ctx) == FUNCTION_DECL
5898 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5899 || (TREE_CODE (decl) == TYPE_DECL && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5900 || (is_friend && !(DECL_LANG_SPECIFIC (decl)
5901 && DECL_TEMPLATE_INFO (decl))))
5903 if (DECL_LANG_SPECIFIC (decl)
5904 && DECL_TEMPLATE_INFO (decl)
5905 && DECL_TI_TEMPLATE (decl))
5906 tmpl = DECL_TI_TEMPLATE (decl);
5907 /* If DECL is a TYPE_DECL for a class-template, then there won't
5908 be DECL_LANG_SPECIFIC. The information equivalent to
5909 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5910 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5911 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5912 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5914 /* Since a template declaration already existed for this
5915 class-type, we must be redeclaring it here. Make sure
5916 that the redeclaration is valid. */
5917 redeclare_class_template (TREE_TYPE (decl),
5918 current_template_parms,
5919 current_template_constraints ());
5920 /* We don't need to create a new TEMPLATE_DECL; just use the
5921 one we already had. */
5922 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5924 else
5926 tmpl = build_template_decl (decl, current_template_parms,
5927 member_template_p);
5928 new_template_p = true;
5930 if (DECL_LANG_SPECIFIC (decl)
5931 && DECL_TEMPLATE_SPECIALIZATION (decl))
5933 /* A specialization of a member template of a template
5934 class. */
5935 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5936 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5937 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5941 else
5943 tree a, t, current, parms;
5944 int i;
5945 tree tinfo = get_template_info (decl);
5947 if (!tinfo)
5949 error ("template definition of non-template %q#D", decl);
5950 return error_mark_node;
5953 tmpl = TI_TEMPLATE (tinfo);
5955 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5956 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5957 && DECL_TEMPLATE_SPECIALIZATION (decl)
5958 && DECL_MEMBER_TEMPLATE_P (tmpl))
5960 /* The declaration is a specialization of a member
5961 template, declared outside the class. Therefore, the
5962 innermost template arguments will be NULL, so we
5963 replace them with the arguments determined by the
5964 earlier call to check_explicit_specialization. */
5965 args = DECL_TI_ARGS (decl);
5967 tree new_tmpl
5968 = build_template_decl (decl, current_template_parms,
5969 member_template_p);
5970 DECL_TI_TEMPLATE (decl) = new_tmpl;
5971 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5972 DECL_TEMPLATE_INFO (new_tmpl)
5973 = build_template_info (tmpl, args);
5975 register_specialization (new_tmpl,
5976 most_general_template (tmpl),
5977 args,
5978 is_friend, 0);
5979 return decl;
5982 /* Make sure the template headers we got make sense. */
5984 parms = DECL_TEMPLATE_PARMS (tmpl);
5985 i = TMPL_PARMS_DEPTH (parms);
5986 if (TMPL_ARGS_DEPTH (args) != i)
5988 error ("expected %d levels of template parms for %q#D, got %d",
5989 i, decl, TMPL_ARGS_DEPTH (args));
5990 DECL_INTERFACE_KNOWN (decl) = 1;
5991 return error_mark_node;
5993 else
5994 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5996 a = TMPL_ARGS_LEVEL (args, i);
5997 t = INNERMOST_TEMPLATE_PARMS (parms);
5999 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
6001 if (current == decl)
6002 error ("got %d template parameters for %q#D",
6003 TREE_VEC_LENGTH (a), decl);
6004 else
6005 error ("got %d template parameters for %q#T",
6006 TREE_VEC_LENGTH (a), current);
6007 error (" but %d required", TREE_VEC_LENGTH (t));
6008 /* Avoid crash in import_export_decl. */
6009 DECL_INTERFACE_KNOWN (decl) = 1;
6010 return error_mark_node;
6013 if (current == decl)
6014 current = ctx;
6015 else if (current == NULL_TREE)
6016 /* Can happen in erroneous input. */
6017 break;
6018 else
6019 current = get_containing_scope (current);
6022 /* Check that the parms are used in the appropriate qualifying scopes
6023 in the declarator. */
6024 if (!comp_template_args
6025 (TI_ARGS (tinfo),
6026 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
6028 error ("template arguments to %qD do not match original "
6029 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
6030 if (!uses_template_parms (TI_ARGS (tinfo)))
6031 inform (input_location, "use %<template<>%> for"
6032 " an explicit specialization");
6033 /* Avoid crash in import_export_decl. */
6034 DECL_INTERFACE_KNOWN (decl) = 1;
6035 return error_mark_node;
6039 gcc_checking_assert (!tmpl || DECL_TEMPLATE_RESULT (tmpl) == decl);
6041 if (new_template_p)
6043 /* Push template declarations for global functions and types.
6044 Note that we do not try to push a global template friend
6045 declared in a template class; such a thing may well depend on
6046 the template parameters of the class and we'll push it when
6047 instantiating the befriending class. */
6048 if (!ctx
6049 && !(is_friend && template_class_depth (current_class_type) > 0))
6051 tree pushed = pushdecl_namespace_level (tmpl, /*hiding=*/is_friend);
6052 if (pushed == error_mark_node)
6053 return error_mark_node;
6055 /* pushdecl may have found an existing template. */
6056 if (pushed != tmpl)
6058 decl = DECL_TEMPLATE_RESULT (pushed);
6059 tmpl = NULL_TREE;
6062 else if (is_friend)
6064 /* Record this decl as belonging to the current class. It's
6065 not chained onto anything else. */
6066 DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (tmpl) = true;
6067 gcc_checking_assert (!DECL_CHAIN (tmpl));
6068 DECL_CHAIN (tmpl) = current_scope ();
6071 else if (tmpl)
6072 /* The type may have been completed, or (erroneously) changed. */
6073 TREE_TYPE (tmpl) = TREE_TYPE (decl);
6075 if (tmpl)
6077 if (is_primary)
6079 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6081 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
6083 /* Give template template parms a DECL_CONTEXT of the template
6084 for which they are a parameter. */
6085 parms = INNERMOST_TEMPLATE_PARMS (parms);
6086 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
6088 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6089 if (TREE_CODE (parm) == TEMPLATE_DECL)
6090 DECL_CONTEXT (parm) = tmpl;
6093 if (TREE_CODE (decl) == TYPE_DECL
6094 && TYPE_DECL_ALIAS_P (decl))
6096 if (tree constr
6097 = TEMPLATE_PARMS_CONSTRAINTS (DECL_TEMPLATE_PARMS (tmpl)))
6099 /* ??? Why don't we do this here for all templates? */
6100 constr = build_constraints (constr, NULL_TREE);
6101 set_constraints (decl, constr);
6103 if (complex_alias_template_p (tmpl))
6104 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
6108 /* The DECL_TI_ARGS of DECL contains full set of arguments
6109 referring wback to its most general template. If TMPL is a
6110 specialization, ARGS may only have the innermost set of
6111 arguments. Add the missing argument levels if necessary. */
6112 if (DECL_TEMPLATE_INFO (tmpl))
6113 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
6115 tree info = build_template_info (tmpl, args);
6117 if (DECL_IMPLICIT_TYPEDEF_P (decl))
6118 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
6119 else
6121 retrofit_lang_decl (decl);
6122 DECL_TEMPLATE_INFO (decl) = info;
6126 if (flag_implicit_templates
6127 && !is_friend
6128 && TREE_PUBLIC (decl)
6129 && VAR_OR_FUNCTION_DECL_P (decl))
6130 /* Set DECL_COMDAT on template instantiations; if we force
6131 them to be emitted by explicit instantiation,
6132 mark_needed will tell cgraph to do the right thing. */
6133 DECL_COMDAT (decl) = true;
6135 gcc_checking_assert (!tmpl || DECL_TEMPLATE_RESULT (tmpl) == decl);
6137 return decl;
6140 /* FN is an inheriting constructor that inherits from the constructor
6141 template INHERITED; turn FN into a constructor template with a matching
6142 template header. */
6144 tree
6145 add_inherited_template_parms (tree fn, tree inherited)
6147 tree inner_parms
6148 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
6149 inner_parms = copy_node (inner_parms);
6150 tree parms
6151 = tree_cons (size_int (processing_template_decl + 1),
6152 inner_parms, current_template_parms);
6153 tree tmpl = build_template_decl (fn, parms, /*member*/true);
6154 tree args = template_parms_to_args (parms);
6155 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
6156 DECL_ARTIFICIAL (tmpl) = true;
6157 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
6158 return tmpl;
6161 /* Called when a class template TYPE is redeclared with the indicated
6162 template PARMS, e.g.:
6164 template <class T> struct S;
6165 template <class T> struct S {}; */
6167 bool
6168 redeclare_class_template (tree type, tree parms, tree cons)
6170 tree tmpl;
6171 tree tmpl_parms;
6172 int i;
6174 if (!TYPE_TEMPLATE_INFO (type))
6176 error ("%qT is not a template type", type);
6177 return false;
6180 tmpl = TYPE_TI_TEMPLATE (type);
6181 if (!PRIMARY_TEMPLATE_P (tmpl))
6182 /* The type is nested in some template class. Nothing to worry
6183 about here; there are no new template parameters for the nested
6184 type. */
6185 return true;
6187 if (!parms)
6189 error ("template specifiers not specified in declaration of %qD",
6190 tmpl);
6191 return false;
6194 parms = INNERMOST_TEMPLATE_PARMS (parms);
6195 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
6197 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
6199 error_n (input_location, TREE_VEC_LENGTH (parms),
6200 "redeclared with %d template parameter",
6201 "redeclared with %d template parameters",
6202 TREE_VEC_LENGTH (parms));
6203 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
6204 "previous declaration %qD used %d template parameter",
6205 "previous declaration %qD used %d template parameters",
6206 tmpl, TREE_VEC_LENGTH (tmpl_parms));
6207 return false;
6210 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
6212 tree tmpl_parm;
6213 tree parm;
6214 tree tmpl_default;
6215 tree parm_default;
6217 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
6218 || TREE_VEC_ELT (parms, i) == error_mark_node)
6219 continue;
6221 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
6222 if (error_operand_p (tmpl_parm))
6223 return false;
6225 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
6226 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
6227 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
6229 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
6230 TEMPLATE_DECL. */
6231 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
6232 || (TREE_CODE (tmpl_parm) != TYPE_DECL
6233 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
6234 || (TREE_CODE (tmpl_parm) != PARM_DECL
6235 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
6236 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
6237 || (TREE_CODE (tmpl_parm) == PARM_DECL
6238 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
6239 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
6241 auto_diagnostic_group d;
6242 error ("template parameter %q+#D", tmpl_parm);
6243 inform (input_location, "redeclared here as %q#D", parm);
6244 return false;
6247 /* The parameters can be declared to introduce different
6248 constraints. */
6249 tree p1 = TREE_VEC_ELT (tmpl_parms, i);
6250 tree p2 = TREE_VEC_ELT (parms, i);
6251 if (!template_parameter_constraints_equivalent_p (p1, p2))
6253 auto_diagnostic_group d;
6254 error ("declaration of template parameter %q+#D with different "
6255 "constraints", parm);
6256 inform (DECL_SOURCE_LOCATION (tmpl_parm),
6257 "original declaration appeared here");
6258 return false;
6261 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
6263 /* We have in [temp.param]:
6265 A template-parameter may not be given default arguments
6266 by two different declarations in the same scope. */
6267 auto_diagnostic_group d;
6268 error_at (input_location, "redefinition of default argument for %q#D", parm);
6269 inform (DECL_SOURCE_LOCATION (tmpl_parm),
6270 "original definition appeared here");
6271 return false;
6274 if (parm_default != NULL_TREE)
6275 /* Update the previous template parameters (which are the ones
6276 that will really count) with the new default value. */
6277 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
6278 else if (tmpl_default != NULL_TREE)
6279 /* Update the new parameters, too; they'll be used as the
6280 parameters for any members. */
6281 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
6283 /* Give each template template parm in this redeclaration a
6284 DECL_CONTEXT of the template for which they are a parameter. */
6285 if (TREE_CODE (parm) == TEMPLATE_DECL)
6287 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
6288 DECL_CONTEXT (parm) = tmpl;
6291 if (TREE_CODE (parm) == TYPE_DECL)
6292 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
6295 tree ci = get_constraints (tmpl);
6296 tree req1 = ci ? CI_TEMPLATE_REQS (ci) : NULL_TREE;
6297 tree req2 = cons ? CI_TEMPLATE_REQS (cons) : NULL_TREE;
6299 /* Two classes with different constraints declare different entities. */
6300 if (!cp_tree_equal (req1, req2))
6302 auto_diagnostic_group d;
6303 error_at (input_location, "redeclaration %q#D with different "
6304 "constraints", tmpl);
6305 inform (DECL_SOURCE_LOCATION (tmpl),
6306 "original declaration appeared here");
6307 return false;
6310 return true;
6313 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
6314 to be used when the caller has already checked
6315 (processing_template_decl
6316 && !instantiation_dependent_expression_p (expr)
6317 && potential_constant_expression (expr))
6318 and cleared processing_template_decl. */
6320 tree
6321 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
6323 return tsubst_copy_and_build (expr,
6324 /*args=*/NULL_TREE,
6325 complain,
6326 /*in_decl=*/NULL_TREE,
6327 /*function_p=*/false,
6328 /*integral_constant_expression_p=*/true);
6331 /* Simplify EXPR if it is a non-dependent expression. Returns the
6332 (possibly simplified) expression. */
6334 tree
6335 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
6337 if (expr == NULL_TREE)
6338 return NULL_TREE;
6340 /* If we're in a template, but EXPR isn't value dependent, simplify
6341 it. We're supposed to treat:
6343 template <typename T> void f(T[1 + 1]);
6344 template <typename T> void f(T[2]);
6346 as two declarations of the same function, for example. */
6347 if (processing_template_decl
6348 && is_nondependent_constant_expression (expr))
6350 processing_template_decl_sentinel s;
6351 expr = instantiate_non_dependent_expr_internal (expr, complain);
6353 return expr;
6356 tree
6357 instantiate_non_dependent_expr (tree expr)
6359 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
6362 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6363 an uninstantiated expression. */
6365 tree
6366 instantiate_non_dependent_or_null (tree expr)
6368 if (expr == NULL_TREE)
6369 return NULL_TREE;
6370 if (processing_template_decl)
6372 if (!is_nondependent_constant_expression (expr))
6373 expr = NULL_TREE;
6374 else
6376 processing_template_decl_sentinel s;
6377 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
6380 return expr;
6383 /* True iff T is a specialization of a variable template. */
6385 bool
6386 variable_template_specialization_p (tree t)
6388 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
6389 return false;
6390 tree tmpl = DECL_TI_TEMPLATE (t);
6391 return variable_template_p (tmpl);
6394 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6395 template declaration, or a TYPE_DECL for an alias declaration. */
6397 bool
6398 alias_type_or_template_p (tree t)
6400 if (t == NULL_TREE)
6401 return false;
6402 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
6403 || (TYPE_P (t)
6404 && TYPE_NAME (t)
6405 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
6406 || DECL_ALIAS_TEMPLATE_P (t));
6409 /* If T is a specialization of an alias template, return it; otherwise return
6410 NULL_TREE. If TRANSPARENT_TYPEDEFS is true, look through other aliases. */
6412 tree
6413 alias_template_specialization_p (const_tree t,
6414 bool transparent_typedefs)
6416 if (!TYPE_P (t))
6417 return NULL_TREE;
6419 /* It's an alias template specialization if it's an alias and its
6420 TYPE_NAME is a specialization of a primary template. */
6421 if (typedef_variant_p (t))
6423 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
6424 if (PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo)))
6425 return CONST_CAST_TREE (t);
6426 if (transparent_typedefs)
6427 return alias_template_specialization_p (DECL_ORIGINAL_TYPE
6428 (TYPE_NAME (t)),
6429 transparent_typedefs);
6432 return NULL_TREE;
6435 /* An alias template is complex from a SFINAE perspective if a template-id
6436 using that alias can be ill-formed when the expansion is not, as with
6437 the void_t template. We determine this by checking whether the
6438 expansion for the alias template uses all its template parameters. */
6440 struct uses_all_template_parms_data
6442 int level;
6443 bool *seen;
6446 static int
6447 uses_all_template_parms_r (tree t, void *data_)
6449 struct uses_all_template_parms_data &data
6450 = *(struct uses_all_template_parms_data*)data_;
6451 tree idx = get_template_parm_index (t);
6453 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
6454 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
6455 return 0;
6458 /* for_each_template_parm any_fn callback for complex_alias_template_p. */
6460 static int
6461 complex_pack_expansion_r (tree t, void *data_)
6463 /* An alias template with a pack expansion that expands a pack from the
6464 enclosing class needs to be considered complex, to avoid confusion with
6465 the same pack being used as an argument to the alias's own template
6466 parameter (91966). */
6467 if (!PACK_EXPANSION_P (t))
6468 return 0;
6469 struct uses_all_template_parms_data &data
6470 = *(struct uses_all_template_parms_data*)data_;
6471 for (tree pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
6472 pack = TREE_CHAIN (pack))
6474 tree parm_pack = TREE_VALUE (pack);
6475 if (!TEMPLATE_PARM_P (parm_pack))
6476 continue;
6477 int idx, level;
6478 template_parm_level_and_index (parm_pack, &level, &idx);
6479 if (level < data.level)
6480 return 1;
6482 return 0;
6485 static bool
6486 complex_alias_template_p (const_tree tmpl)
6488 /* A renaming alias isn't complex. */
6489 if (get_underlying_template (CONST_CAST_TREE (tmpl)) != tmpl)
6490 return false;
6492 /* Any other constrained alias is complex. */
6493 if (get_constraints (tmpl))
6494 return true;
6496 struct uses_all_template_parms_data data;
6497 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6498 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6499 data.level = TMPL_PARMS_DEPTH (parms);
6500 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
6501 data.seen = XALLOCAVEC (bool, len);
6502 for (int i = 0; i < len; ++i)
6503 data.seen[i] = false;
6505 if (for_each_template_parm (pat, uses_all_template_parms_r, &data,
6506 NULL, true, complex_pack_expansion_r))
6507 return true;
6508 for (int i = 0; i < len; ++i)
6509 if (!data.seen[i])
6510 return true;
6511 return false;
6514 /* If T is a specialization of a complex alias template with dependent
6515 template-arguments, return it; otherwise return NULL_TREE. If T is a
6516 typedef to such a specialization, return the specialization. */
6518 tree
6519 dependent_alias_template_spec_p (const_tree t, bool transparent_typedefs)
6521 if (t == error_mark_node)
6522 return NULL_TREE;
6523 gcc_assert (TYPE_P (t));
6525 if (!typedef_variant_p (t))
6526 return NULL_TREE;
6528 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
6529 if (tinfo
6530 && TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo))
6531 && (any_dependent_template_arguments_p
6532 (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)))))
6533 return CONST_CAST_TREE (t);
6535 if (transparent_typedefs)
6537 tree utype = DECL_ORIGINAL_TYPE (TYPE_NAME (t));
6538 return dependent_alias_template_spec_p (utype, transparent_typedefs);
6541 return NULL_TREE;
6544 /* Return the number of innermost template parameters in TMPL. */
6546 static int
6547 num_innermost_template_parms (const_tree tmpl)
6549 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6550 return TREE_VEC_LENGTH (parms);
6553 /* Return either TMPL or another template that it is equivalent to under DR
6554 1286: An alias that just changes the name of a template is equivalent to
6555 the other template. */
6557 static tree
6558 get_underlying_template (tree tmpl)
6560 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6561 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6563 /* Determine if the alias is equivalent to an underlying template. */
6564 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6565 /* The underlying type may have been ill-formed. Don't proceed. */
6566 if (!orig_type)
6567 break;
6568 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6569 if (!tinfo)
6570 break;
6572 tree underlying = TI_TEMPLATE (tinfo);
6573 if (!PRIMARY_TEMPLATE_P (underlying)
6574 || (num_innermost_template_parms (tmpl)
6575 != num_innermost_template_parms (underlying)))
6576 break;
6578 /* Does the alias add cv-quals? */
6579 if (TYPE_QUALS (TREE_TYPE (underlying)) != TYPE_QUALS (TREE_TYPE (tmpl)))
6580 break;
6582 tree alias_args = INNERMOST_TEMPLATE_ARGS (generic_targs_for (tmpl));
6583 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6584 break;
6586 /* If TMPL adds or changes any constraints, it isn't equivalent. I think
6587 it's appropriate to treat a less-constrained alias as equivalent. */
6588 if (!at_least_as_constrained (underlying, tmpl))
6589 break;
6591 /* Alias is equivalent. Strip it and repeat. */
6592 tmpl = underlying;
6595 return tmpl;
6598 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6599 must be a reference-to-function or a pointer-to-function type, as specified
6600 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6601 and check that the resulting function has external linkage. */
6603 static tree
6604 convert_nontype_argument_function (tree type, tree expr,
6605 tsubst_flags_t complain)
6607 tree fns = expr;
6608 tree fn, fn_no_ptr;
6609 linkage_kind linkage;
6611 fn = instantiate_type (type, fns, tf_none);
6612 if (fn == error_mark_node)
6613 return error_mark_node;
6615 if (value_dependent_expression_p (fn))
6616 goto accept;
6618 fn_no_ptr = fn;
6619 if (REFERENCE_REF_P (fn_no_ptr))
6620 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6621 fn_no_ptr = strip_fnptr_conv (fn_no_ptr);
6622 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6623 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6624 if (BASELINK_P (fn_no_ptr))
6625 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6627 /* [temp.arg.nontype]/1
6629 A template-argument for a non-type, non-template template-parameter
6630 shall be one of:
6631 [...]
6632 -- the address of an object or function with external [C++11: or
6633 internal] linkage. */
6635 STRIP_ANY_LOCATION_WRAPPER (fn_no_ptr);
6636 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6638 if (complain & tf_error)
6640 location_t loc = cp_expr_loc_or_input_loc (expr);
6641 error_at (loc, "%qE is not a valid template argument for type %qT",
6642 expr, type);
6643 if (TYPE_PTR_P (type))
6644 inform (loc, "it must be the address of a function "
6645 "with external linkage");
6646 else
6647 inform (loc, "it must be the name of a function with "
6648 "external linkage");
6650 return NULL_TREE;
6653 linkage = decl_linkage (fn_no_ptr);
6654 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6656 if (complain & tf_error)
6658 location_t loc = cp_expr_loc_or_input_loc (expr);
6659 if (cxx_dialect >= cxx11)
6660 error_at (loc, "%qE is not a valid template argument for type "
6661 "%qT because %qD has no linkage",
6662 expr, type, fn_no_ptr);
6663 else
6664 error_at (loc, "%qE is not a valid template argument for type "
6665 "%qT because %qD does not have external linkage",
6666 expr, type, fn_no_ptr);
6668 return NULL_TREE;
6671 accept:
6672 if (TYPE_REF_P (type))
6674 if (REFERENCE_REF_P (fn))
6675 fn = TREE_OPERAND (fn, 0);
6676 else
6677 fn = build_address (fn);
6679 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6680 fn = build_nop (type, fn);
6682 return fn;
6685 /* Subroutine of convert_nontype_argument.
6686 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6687 Emit an error otherwise. */
6689 static bool
6690 check_valid_ptrmem_cst_expr (tree type, tree expr,
6691 tsubst_flags_t complain)
6693 tree orig_expr = expr;
6694 STRIP_NOPS (expr);
6695 if (null_ptr_cst_p (expr))
6696 return true;
6697 if (TREE_CODE (expr) == PTRMEM_CST
6698 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6699 PTRMEM_CST_CLASS (expr)))
6700 return true;
6701 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6702 return true;
6703 if (processing_template_decl
6704 && TREE_CODE (expr) == ADDR_EXPR
6705 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6706 return true;
6707 if (complain & tf_error)
6709 location_t loc = cp_expr_loc_or_input_loc (orig_expr);
6710 error_at (loc, "%qE is not a valid template argument for type %qT",
6711 orig_expr, type);
6712 if (TREE_CODE (expr) != PTRMEM_CST)
6713 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6714 else
6715 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6717 return false;
6720 /* Returns TRUE iff the address of OP is value-dependent.
6722 14.6.2.4 [temp.dep.temp]:
6723 A non-integral non-type template-argument is dependent if its type is
6724 dependent or it has either of the following forms
6725 qualified-id
6726 & qualified-id
6727 and contains a nested-name-specifier which specifies a class-name that
6728 names a dependent type.
6730 We generalize this to just say that the address of a member of a
6731 dependent class is value-dependent; the above doesn't cover the
6732 address of a static data member named with an unqualified-id. */
6734 static bool
6735 has_value_dependent_address (tree op)
6737 STRIP_ANY_LOCATION_WRAPPER (op);
6739 /* We could use get_inner_reference here, but there's no need;
6740 this is only relevant for template non-type arguments, which
6741 can only be expressed as &id-expression. */
6742 if (DECL_P (op))
6744 tree ctx = CP_DECL_CONTEXT (op);
6745 if (TYPE_P (ctx) && dependent_type_p (ctx))
6746 return true;
6749 return false;
6752 /* The next set of functions are used for providing helpful explanatory
6753 diagnostics for failed overload resolution. Their messages should be
6754 indented by two spaces for consistency with the messages in
6755 call.c */
6757 static int
6758 unify_success (bool /*explain_p*/)
6760 return 0;
6763 /* Other failure functions should call this one, to provide a single function
6764 for setting a breakpoint on. */
6766 static int
6767 unify_invalid (bool /*explain_p*/)
6769 return 1;
6772 static int
6773 unify_parameter_deduction_failure (bool explain_p, tree parm)
6775 if (explain_p)
6776 inform (input_location,
6777 " couldn%'t deduce template parameter %qD", parm);
6778 return unify_invalid (explain_p);
6781 static int
6782 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6784 if (explain_p)
6785 inform (input_location,
6786 " types %qT and %qT have incompatible cv-qualifiers",
6787 parm, arg);
6788 return unify_invalid (explain_p);
6791 static int
6792 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6794 if (explain_p)
6795 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6796 return unify_invalid (explain_p);
6799 static int
6800 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6802 if (explain_p)
6803 inform (input_location,
6804 " template parameter %qD is not a parameter pack, but "
6805 "argument %qD is",
6806 parm, arg);
6807 return unify_invalid (explain_p);
6810 static int
6811 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6813 if (explain_p)
6814 inform (input_location,
6815 " template argument %qE does not match "
6816 "pointer-to-member constant %qE",
6817 arg, parm);
6818 return unify_invalid (explain_p);
6821 static int
6822 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6824 if (explain_p)
6825 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6826 return unify_invalid (explain_p);
6829 static int
6830 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6832 if (explain_p)
6833 inform (input_location,
6834 " inconsistent parameter pack deduction with %qT and %qT",
6835 old_arg, new_arg);
6836 return unify_invalid (explain_p);
6839 static int
6840 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6842 if (explain_p)
6844 if (TYPE_P (parm))
6845 inform (input_location,
6846 " deduced conflicting types for parameter %qT (%qT and %qT)",
6847 parm, first, second);
6848 else
6849 inform (input_location,
6850 " deduced conflicting values for non-type parameter "
6851 "%qE (%qE and %qE)", parm, first, second);
6853 return unify_invalid (explain_p);
6856 static int
6857 unify_vla_arg (bool explain_p, tree arg)
6859 if (explain_p)
6860 inform (input_location,
6861 " variable-sized array type %qT is not "
6862 "a valid template argument",
6863 arg);
6864 return unify_invalid (explain_p);
6867 static int
6868 unify_method_type_error (bool explain_p, tree arg)
6870 if (explain_p)
6871 inform (input_location,
6872 " member function type %qT is not a valid template argument",
6873 arg);
6874 return unify_invalid (explain_p);
6877 static int
6878 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6880 if (explain_p)
6882 if (least_p)
6883 inform_n (input_location, wanted,
6884 " candidate expects at least %d argument, %d provided",
6885 " candidate expects at least %d arguments, %d provided",
6886 wanted, have);
6887 else
6888 inform_n (input_location, wanted,
6889 " candidate expects %d argument, %d provided",
6890 " candidate expects %d arguments, %d provided",
6891 wanted, have);
6893 return unify_invalid (explain_p);
6896 static int
6897 unify_too_many_arguments (bool explain_p, int have, int wanted)
6899 return unify_arity (explain_p, have, wanted);
6902 static int
6903 unify_too_few_arguments (bool explain_p, int have, int wanted,
6904 bool least_p = false)
6906 return unify_arity (explain_p, have, wanted, least_p);
6909 static int
6910 unify_arg_conversion (bool explain_p, tree to_type,
6911 tree from_type, tree arg)
6913 if (explain_p)
6914 inform (cp_expr_loc_or_input_loc (arg),
6915 " cannot convert %qE (type %qT) to type %qT",
6916 arg, from_type, to_type);
6917 return unify_invalid (explain_p);
6920 static int
6921 unify_no_common_base (bool explain_p, enum template_base_result r,
6922 tree parm, tree arg)
6924 if (explain_p)
6925 switch (r)
6927 case tbr_ambiguous_baseclass:
6928 inform (input_location, " %qT is an ambiguous base class of %qT",
6929 parm, arg);
6930 break;
6931 default:
6932 inform (input_location, " %qT is not derived from %qT", arg, parm);
6933 break;
6935 return unify_invalid (explain_p);
6938 static int
6939 unify_inconsistent_template_template_parameters (bool explain_p)
6941 if (explain_p)
6942 inform (input_location,
6943 " template parameters of a template template argument are "
6944 "inconsistent with other deduced template arguments");
6945 return unify_invalid (explain_p);
6948 static int
6949 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6951 if (explain_p)
6952 inform (input_location,
6953 " cannot deduce a template for %qT from non-template type %qT",
6954 parm, arg);
6955 return unify_invalid (explain_p);
6958 static int
6959 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6961 if (explain_p)
6962 inform (input_location,
6963 " template argument %qE does not match %qE", arg, parm);
6964 return unify_invalid (explain_p);
6967 /* True if T is a C++20 template parameter object to store the argument for a
6968 template parameter of class type. */
6970 bool
6971 template_parm_object_p (const_tree t)
6973 return (TREE_CODE (t) == VAR_DECL && DECL_ARTIFICIAL (t) && DECL_NAME (t)
6974 && startswith (IDENTIFIER_POINTER (DECL_NAME (t)), "_ZTA"));
6977 /* Subroutine of convert_nontype_argument, to check whether EXPR, as an
6978 argument for TYPE, points to an unsuitable object.
6980 Also adjust the type of the index in C++20 array subobject references. */
6982 static bool
6983 invalid_tparm_referent_p (tree type, tree expr, tsubst_flags_t complain)
6985 switch (TREE_CODE (expr))
6987 CASE_CONVERT:
6988 return invalid_tparm_referent_p (type, TREE_OPERAND (expr, 0),
6989 complain);
6991 case TARGET_EXPR:
6992 return invalid_tparm_referent_p (type, TARGET_EXPR_INITIAL (expr),
6993 complain);
6995 case CONSTRUCTOR:
6997 unsigned i; tree elt;
6998 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expr), i, elt)
6999 if (invalid_tparm_referent_p (TREE_TYPE (elt), elt, complain))
7000 return true;
7002 break;
7004 case ADDR_EXPR:
7006 tree decl = TREE_OPERAND (expr, 0);
7008 if (cxx_dialect >= cxx20)
7009 while (TREE_CODE (decl) == COMPONENT_REF
7010 || TREE_CODE (decl) == ARRAY_REF)
7012 tree &op = TREE_OPERAND (decl, 1);
7013 if (TREE_CODE (decl) == ARRAY_REF
7014 && TREE_CODE (op) == INTEGER_CST)
7015 /* Canonicalize array offsets to ptrdiff_t; how they were
7016 written doesn't matter for subobject identity. */
7017 op = fold_convert (ptrdiff_type_node, op);
7018 decl = TREE_OPERAND (decl, 0);
7021 if (!VAR_P (decl))
7023 if (complain & tf_error)
7024 error_at (cp_expr_loc_or_input_loc (expr),
7025 "%qE is not a valid template argument of type %qT "
7026 "because %qE is not a variable", expr, type, decl);
7027 return true;
7029 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
7031 if (complain & tf_error)
7032 error_at (cp_expr_loc_or_input_loc (expr),
7033 "%qE is not a valid template argument of type %qT "
7034 "in C++98 because %qD does not have external linkage",
7035 expr, type, decl);
7036 return true;
7038 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
7039 && decl_linkage (decl) == lk_none)
7041 if (complain & tf_error)
7042 error_at (cp_expr_loc_or_input_loc (expr),
7043 "%qE is not a valid template argument of type %qT "
7044 "because %qD has no linkage", expr, type, decl);
7045 return true;
7047 /* C++17: For a non-type template-parameter of reference or pointer
7048 type, the value of the constant expression shall not refer to (or
7049 for a pointer type, shall not be the address of):
7050 * a subobject (4.5),
7051 * a temporary object (15.2),
7052 * a string literal (5.13.5),
7053 * the result of a typeid expression (8.2.8), or
7054 * a predefined __func__ variable (11.4.1). */
7055 else if (DECL_ARTIFICIAL (decl))
7057 if (complain & tf_error)
7058 error ("the address of %qD is not a valid template argument",
7059 decl);
7060 return true;
7062 else if (cxx_dialect < cxx20
7063 && !(same_type_ignoring_top_level_qualifiers_p
7064 (strip_array_types (TREE_TYPE (type)),
7065 strip_array_types (TREE_TYPE (decl)))))
7067 if (complain & tf_error)
7068 error ("the address of the %qT subobject of %qD is not a "
7069 "valid template argument", TREE_TYPE (type), decl);
7070 return true;
7072 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
7074 if (complain & tf_error)
7075 error ("the address of %qD is not a valid template argument "
7076 "because it does not have static storage duration",
7077 decl);
7078 return true;
7081 break;
7083 default:
7084 if (!INDIRECT_TYPE_P (type))
7085 /* We're only concerned about pointers and references here. */;
7086 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7087 /* Null pointer values are OK in C++11. */;
7088 else
7090 if (VAR_P (expr))
7092 if (complain & tf_error)
7093 error ("%qD is not a valid template argument "
7094 "because %qD is a variable, not the address of "
7095 "a variable", expr, expr);
7096 return true;
7098 else
7100 if (complain & tf_error)
7101 error ("%qE is not a valid template argument for %qT "
7102 "because it is not the address of a variable",
7103 expr, type);
7104 return true;
7108 return false;
7112 /* The template arguments corresponding to template parameter objects of types
7113 that contain pointers to members. */
7115 static GTY(()) hash_map<tree, tree> *tparm_obj_values;
7117 /* Return a VAR_DECL for the C++20 template parameter object corresponding to
7118 template argument EXPR. */
7120 static tree
7121 get_template_parm_object (tree expr, tsubst_flags_t complain)
7123 if (TREE_CODE (expr) == TARGET_EXPR)
7124 expr = TARGET_EXPR_INITIAL (expr);
7126 if (!TREE_CONSTANT (expr))
7128 if ((complain & tf_error)
7129 && require_rvalue_constant_expression (expr))
7130 cxx_constant_value (expr);
7131 return error_mark_node;
7133 if (invalid_tparm_referent_p (TREE_TYPE (expr), expr, complain))
7134 return error_mark_node;
7136 /* This is no longer a compound literal. */
7137 gcc_assert (!TREE_HAS_CONSTRUCTOR (expr));
7139 tree name = mangle_template_parm_object (expr);
7140 tree decl = get_global_binding (name);
7141 if (decl)
7142 return decl;
7144 tree type = cp_build_qualified_type (TREE_TYPE (expr), TYPE_QUAL_CONST);
7145 decl = create_temporary_var (type);
7146 DECL_CONTEXT (decl) = NULL_TREE;
7147 TREE_STATIC (decl) = true;
7148 DECL_DECLARED_CONSTEXPR_P (decl) = true;
7149 TREE_READONLY (decl) = true;
7150 DECL_NAME (decl) = name;
7151 SET_DECL_ASSEMBLER_NAME (decl, name);
7152 comdat_linkage (decl);
7154 if (!zero_init_p (type))
7156 /* If EXPR contains any PTRMEM_CST, they will get clobbered by
7157 lower_var_init before we're done mangling. So store the original
7158 value elsewhere. */
7159 tree copy = unshare_constructor (expr);
7160 hash_map_safe_put<hm_ggc> (tparm_obj_values, decl, copy);
7163 pushdecl_top_level_and_finish (decl, expr);
7165 return decl;
7168 /* Return the actual template argument corresponding to template parameter
7169 object VAR. */
7171 tree
7172 tparm_object_argument (tree var)
7174 if (zero_init_p (TREE_TYPE (var)))
7175 return DECL_INITIAL (var);
7176 return *(tparm_obj_values->get (var));
7179 /* Attempt to convert the non-type template parameter EXPR to the
7180 indicated TYPE. If the conversion is successful, return the
7181 converted value. If the conversion is unsuccessful, return
7182 NULL_TREE if we issued an error message, or error_mark_node if we
7183 did not. We issue error messages for out-and-out bad template
7184 parameters, but not simply because the conversion failed, since we
7185 might be just trying to do argument deduction. Both TYPE and EXPR
7186 must be non-dependent.
7188 The conversion follows the special rules described in
7189 [temp.arg.nontype], and it is much more strict than an implicit
7190 conversion.
7192 This function is called twice for each template argument (see
7193 lookup_template_class for a more accurate description of this
7194 problem). This means that we need to handle expressions which
7195 are not valid in a C++ source, but can be created from the
7196 first call (for instance, casts to perform conversions). These
7197 hacks can go away after we fix the double coercion problem. */
7199 static tree
7200 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
7202 tree expr_type;
7203 location_t loc = cp_expr_loc_or_input_loc (expr);
7205 /* Detect immediately string literals as invalid non-type argument.
7206 This special-case is not needed for correctness (we would easily
7207 catch this later), but only to provide better diagnostic for this
7208 common user mistake. As suggested by DR 100, we do not mention
7209 linkage issues in the diagnostic as this is not the point. */
7210 if (TREE_CODE (expr) == STRING_CST && !CLASS_TYPE_P (type))
7212 if (complain & tf_error)
7213 error ("%qE is not a valid template argument for type %qT "
7214 "because string literals can never be used in this context",
7215 expr, type);
7216 return NULL_TREE;
7219 /* Add the ADDR_EXPR now for the benefit of
7220 value_dependent_expression_p. */
7221 if (TYPE_PTROBV_P (type)
7222 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
7224 expr = decay_conversion (expr, complain);
7225 if (expr == error_mark_node)
7226 return error_mark_node;
7229 /* If we are in a template, EXPR may be non-dependent, but still
7230 have a syntactic, rather than semantic, form. For example, EXPR
7231 might be a SCOPE_REF, rather than the VAR_DECL to which the
7232 SCOPE_REF refers. Preserving the qualifying scope is necessary
7233 so that access checking can be performed when the template is
7234 instantiated -- but here we need the resolved form so that we can
7235 convert the argument. */
7236 bool non_dep = false;
7237 if (TYPE_REF_OBJ_P (type)
7238 && has_value_dependent_address (expr))
7239 /* If we want the address and it's value-dependent, don't fold. */;
7240 else if (processing_template_decl
7241 && is_nondependent_constant_expression (expr))
7242 non_dep = true;
7243 if (error_operand_p (expr))
7244 return error_mark_node;
7245 expr_type = TREE_TYPE (expr);
7247 /* If the argument is non-dependent, perform any conversions in
7248 non-dependent context as well. */
7249 processing_template_decl_sentinel s (non_dep);
7250 if (non_dep)
7251 expr = instantiate_non_dependent_expr_internal (expr, complain);
7253 const bool val_dep_p = value_dependent_expression_p (expr);
7254 if (val_dep_p)
7255 expr = canonicalize_expr_argument (expr, complain);
7257 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
7258 to a non-type argument of "nullptr". */
7259 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
7260 expr = fold_simple (convert (type, expr));
7262 /* In C++11, integral or enumeration non-type template arguments can be
7263 arbitrary constant expressions. Pointer and pointer to
7264 member arguments can be general constant expressions that evaluate
7265 to a null value, but otherwise still need to be of a specific form. */
7266 if (cxx_dialect >= cxx11)
7268 if (TREE_CODE (expr) == PTRMEM_CST && TYPE_PTRMEM_P (type))
7269 /* A PTRMEM_CST is already constant, and a valid template
7270 argument for a parameter of pointer to member type, we just want
7271 to leave it in that form rather than lower it to a
7272 CONSTRUCTOR. */;
7273 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7274 || cxx_dialect >= cxx17)
7276 /* C++17: A template-argument for a non-type template-parameter shall
7277 be a converted constant expression (8.20) of the type of the
7278 template-parameter. */
7279 expr = build_converted_constant_expr (type, expr, complain);
7280 if (expr == error_mark_node)
7281 /* Make sure we return NULL_TREE only if we have really issued
7282 an error, as described above. */
7283 return (complain & tf_error) ? NULL_TREE : error_mark_node;
7284 else if (TREE_CODE (expr) == IMPLICIT_CONV_EXPR)
7286 IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr) = true;
7287 return expr;
7289 expr = maybe_constant_value (expr, NULL_TREE,
7290 /*manifestly_const_eval=*/true);
7291 expr = convert_from_reference (expr);
7293 else if (TYPE_PTR_OR_PTRMEM_P (type))
7295 tree folded = maybe_constant_value (expr, NULL_TREE,
7296 /*manifestly_const_eval=*/true);
7297 if (TYPE_PTR_P (type) ? integer_zerop (folded)
7298 : null_member_pointer_value_p (folded))
7299 expr = folded;
7303 if (TYPE_REF_P (type))
7304 expr = mark_lvalue_use (expr);
7305 else
7306 expr = mark_rvalue_use (expr);
7308 /* HACK: Due to double coercion, we can get a
7309 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
7310 which is the tree that we built on the first call (see
7311 below when coercing to reference to object or to reference to
7312 function). We just strip everything and get to the arg.
7313 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
7314 for examples. */
7315 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
7317 tree probe_type, probe = expr;
7318 if (REFERENCE_REF_P (probe))
7319 probe = TREE_OPERAND (probe, 0);
7320 probe_type = TREE_TYPE (probe);
7321 if (TREE_CODE (probe) == NOP_EXPR)
7323 /* ??? Maybe we could use convert_from_reference here, but we
7324 would need to relax its constraints because the NOP_EXPR
7325 could actually change the type to something more cv-qualified,
7326 and this is not folded by convert_from_reference. */
7327 tree addr = TREE_OPERAND (probe, 0);
7328 if (TYPE_REF_P (probe_type)
7329 && TREE_CODE (addr) == ADDR_EXPR
7330 && TYPE_PTR_P (TREE_TYPE (addr))
7331 && (same_type_ignoring_top_level_qualifiers_p
7332 (TREE_TYPE (probe_type),
7333 TREE_TYPE (TREE_TYPE (addr)))))
7335 expr = TREE_OPERAND (addr, 0);
7336 expr_type = TREE_TYPE (probe_type);
7341 /* [temp.arg.nontype]/5, bullet 1
7343 For a non-type template-parameter of integral or enumeration type,
7344 integral promotions (_conv.prom_) and integral conversions
7345 (_conv.integral_) are applied. */
7346 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
7347 || TREE_CODE (type) == REAL_TYPE)
7349 if (cxx_dialect < cxx11)
7351 tree t = build_converted_constant_expr (type, expr, complain);
7352 t = maybe_constant_value (t);
7353 if (t != error_mark_node)
7354 expr = t;
7357 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
7358 return error_mark_node;
7360 /* Notice that there are constant expressions like '4 % 0' which
7361 do not fold into integer constants. */
7362 if (!CONSTANT_CLASS_P (expr) && !val_dep_p)
7364 if (complain & tf_error)
7366 int errs = errorcount, warns = warningcount + werrorcount;
7367 if (!require_potential_constant_expression (expr))
7368 expr = error_mark_node;
7369 else
7370 expr = cxx_constant_value (expr);
7371 if (errorcount > errs || warningcount + werrorcount > warns)
7372 inform (loc, "in template argument for type %qT", type);
7373 if (expr == error_mark_node)
7374 return NULL_TREE;
7375 /* else cxx_constant_value complained but gave us
7376 a real constant, so go ahead. */
7377 if (!CONSTANT_CLASS_P (expr))
7379 /* Some assemble time constant expressions like
7380 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
7381 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
7382 as we can emit them into .rodata initializers of
7383 variables, yet they can't fold into an INTEGER_CST at
7384 compile time. Refuse them here. */
7385 gcc_checking_assert (reduced_constant_expression_p (expr));
7386 error_at (loc, "template argument %qE for type %qT not "
7387 "a compile-time constant", expr, type);
7388 return NULL_TREE;
7391 else
7392 return NULL_TREE;
7395 /* Avoid typedef problems. */
7396 if (TREE_TYPE (expr) != type)
7397 expr = fold_convert (type, expr);
7399 /* [temp.arg.nontype]/5, bullet 2
7401 For a non-type template-parameter of type pointer to object,
7402 qualification conversions (_conv.qual_) and the array-to-pointer
7403 conversion (_conv.array_) are applied. */
7404 else if (TYPE_PTROBV_P (type))
7406 tree decayed = expr;
7408 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
7409 decay_conversion or an explicit cast. If it's a problematic cast,
7410 we'll complain about it below. */
7411 if (TREE_CODE (expr) == NOP_EXPR)
7413 tree probe = expr;
7414 STRIP_NOPS (probe);
7415 if (TREE_CODE (probe) == ADDR_EXPR
7416 && TYPE_PTR_P (TREE_TYPE (probe)))
7418 expr = probe;
7419 expr_type = TREE_TYPE (expr);
7423 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
7425 A template-argument for a non-type, non-template template-parameter
7426 shall be one of: [...]
7428 -- the name of a non-type template-parameter;
7429 -- the address of an object or function with external linkage, [...]
7430 expressed as "& id-expression" where the & is optional if the name
7431 refers to a function or array, or if the corresponding
7432 template-parameter is a reference.
7434 Here, we do not care about functions, as they are invalid anyway
7435 for a parameter of type pointer-to-object. */
7437 if (val_dep_p)
7438 /* Non-type template parameters are OK. */
7440 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
7441 /* Null pointer values are OK in C++11. */;
7442 else if (TREE_CODE (expr) != ADDR_EXPR
7443 && !INDIRECT_TYPE_P (expr_type))
7444 /* Other values, like integer constants, might be valid
7445 non-type arguments of some other type. */
7446 return error_mark_node;
7447 else if (invalid_tparm_referent_p (type, expr, complain))
7448 return NULL_TREE;
7450 expr = decayed;
7452 expr = perform_qualification_conversions (type, expr);
7453 if (expr == error_mark_node)
7454 return error_mark_node;
7456 /* [temp.arg.nontype]/5, bullet 3
7458 For a non-type template-parameter of type reference to object, no
7459 conversions apply. The type referred to by the reference may be more
7460 cv-qualified than the (otherwise identical) type of the
7461 template-argument. The template-parameter is bound directly to the
7462 template-argument, which must be an lvalue. */
7463 else if (TYPE_REF_OBJ_P (type))
7465 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
7466 expr_type))
7467 return error_mark_node;
7469 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
7471 if (complain & tf_error)
7472 error ("%qE is not a valid template argument for type %qT "
7473 "because of conflicts in cv-qualification", expr, type);
7474 return NULL_TREE;
7477 if (!lvalue_p (expr))
7479 if (complain & tf_error)
7480 error ("%qE is not a valid template argument for type %qT "
7481 "because it is not an lvalue", expr, type);
7482 return NULL_TREE;
7485 /* [temp.arg.nontype]/1
7487 A template-argument for a non-type, non-template template-parameter
7488 shall be one of: [...]
7490 -- the address of an object or function with external linkage. */
7491 if (INDIRECT_REF_P (expr)
7492 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
7494 expr = TREE_OPERAND (expr, 0);
7495 if (DECL_P (expr))
7497 if (complain & tf_error)
7498 error ("%q#D is not a valid template argument for type %qT "
7499 "because a reference variable does not have a constant "
7500 "address", expr, type);
7501 return NULL_TREE;
7505 if (TYPE_REF_OBJ_P (TREE_TYPE (expr)) && val_dep_p)
7506 /* OK, dependent reference. We don't want to ask whether a DECL is
7507 itself value-dependent, since what we want here is its address. */;
7508 else
7510 expr = build_address (expr);
7512 if (invalid_tparm_referent_p (type, expr, complain))
7513 return NULL_TREE;
7516 if (!same_type_p (type, TREE_TYPE (expr)))
7517 expr = build_nop (type, expr);
7519 /* [temp.arg.nontype]/5, bullet 4
7521 For a non-type template-parameter of type pointer to function, only
7522 the function-to-pointer conversion (_conv.func_) is applied. If the
7523 template-argument represents a set of overloaded functions (or a
7524 pointer to such), the matching function is selected from the set
7525 (_over.over_). */
7526 else if (TYPE_PTRFN_P (type))
7528 /* If the argument is a template-id, we might not have enough
7529 context information to decay the pointer. */
7530 if (!type_unknown_p (expr_type))
7532 expr = decay_conversion (expr, complain);
7533 if (expr == error_mark_node)
7534 return error_mark_node;
7537 if (cxx_dialect >= cxx11 && integer_zerop (expr))
7538 /* Null pointer values are OK in C++11. */
7539 return perform_qualification_conversions (type, expr);
7541 expr = convert_nontype_argument_function (type, expr, complain);
7542 if (!expr || expr == error_mark_node)
7543 return expr;
7545 /* [temp.arg.nontype]/5, bullet 5
7547 For a non-type template-parameter of type reference to function, no
7548 conversions apply. If the template-argument represents a set of
7549 overloaded functions, the matching function is selected from the set
7550 (_over.over_). */
7551 else if (TYPE_REFFN_P (type))
7553 if (TREE_CODE (expr) == ADDR_EXPR)
7555 if (complain & tf_error)
7557 error ("%qE is not a valid template argument for type %qT "
7558 "because it is a pointer", expr, type);
7559 inform (input_location, "try using %qE instead",
7560 TREE_OPERAND (expr, 0));
7562 return NULL_TREE;
7565 expr = convert_nontype_argument_function (type, expr, complain);
7566 if (!expr || expr == error_mark_node)
7567 return expr;
7569 /* [temp.arg.nontype]/5, bullet 6
7571 For a non-type template-parameter of type pointer to member function,
7572 no conversions apply. If the template-argument represents a set of
7573 overloaded member functions, the matching member function is selected
7574 from the set (_over.over_). */
7575 else if (TYPE_PTRMEMFUNC_P (type))
7577 expr = instantiate_type (type, expr, tf_none);
7578 if (expr == error_mark_node)
7579 return error_mark_node;
7581 /* [temp.arg.nontype] bullet 1 says the pointer to member
7582 expression must be a pointer-to-member constant. */
7583 if (!val_dep_p
7584 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7585 return NULL_TREE;
7587 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7588 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7589 if (fnptr_conv_p (type, TREE_TYPE (expr)))
7590 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
7592 /* [temp.arg.nontype]/5, bullet 7
7594 For a non-type template-parameter of type pointer to data member,
7595 qualification conversions (_conv.qual_) are applied. */
7596 else if (TYPE_PTRDATAMEM_P (type))
7598 /* [temp.arg.nontype] bullet 1 says the pointer to member
7599 expression must be a pointer-to-member constant. */
7600 if (!val_dep_p
7601 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7602 return NULL_TREE;
7604 expr = perform_qualification_conversions (type, expr);
7605 if (expr == error_mark_node)
7606 return expr;
7608 else if (NULLPTR_TYPE_P (type))
7610 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
7612 if (complain & tf_error)
7613 error ("%qE is not a valid template argument for type %qT "
7614 "because it is of type %qT", expr, type, TREE_TYPE (expr));
7615 return NULL_TREE;
7617 return expr;
7619 else if (CLASS_TYPE_P (type))
7621 /* Replace the argument with a reference to the corresponding template
7622 parameter object. */
7623 if (!val_dep_p)
7624 expr = get_template_parm_object (expr, complain);
7625 if (expr == error_mark_node)
7626 return NULL_TREE;
7628 /* A template non-type parameter must be one of the above. */
7629 else
7630 gcc_unreachable ();
7632 /* Sanity check: did we actually convert the argument to the
7633 right type? */
7634 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7635 (type, TREE_TYPE (expr)));
7636 return convert_from_reference (expr);
7639 /* Subroutine of coerce_template_template_parms, which returns 1 if
7640 PARM_PARM and ARG_PARM match using the rule for the template
7641 parameters of template template parameters. Both PARM and ARG are
7642 template parameters; the rest of the arguments are the same as for
7643 coerce_template_template_parms.
7645 static int
7646 coerce_template_template_parm (tree parm,
7647 tree arg,
7648 tsubst_flags_t complain,
7649 tree in_decl,
7650 tree outer_args)
7652 if (arg == NULL_TREE || error_operand_p (arg)
7653 || parm == NULL_TREE || error_operand_p (parm))
7654 return 0;
7656 if (TREE_CODE (arg) != TREE_CODE (parm))
7657 return 0;
7659 switch (TREE_CODE (parm))
7661 case TEMPLATE_DECL:
7662 /* We encounter instantiations of templates like
7663 template <template <template <class> class> class TT>
7664 class C; */
7666 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7667 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7669 if (!coerce_template_template_parms
7670 (parmparm, argparm, complain, in_decl, outer_args))
7671 return 0;
7673 /* Fall through. */
7675 case TYPE_DECL:
7676 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
7677 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7678 /* Argument is a parameter pack but parameter is not. */
7679 return 0;
7680 break;
7682 case PARM_DECL:
7683 /* The tsubst call is used to handle cases such as
7685 template <int> class C {};
7686 template <class T, template <T> class TT> class D {};
7687 D<int, C> d;
7689 i.e. the parameter list of TT depends on earlier parameters. */
7690 if (!uses_template_parms (TREE_TYPE (arg)))
7692 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
7693 if (!uses_template_parms (t)
7694 && !same_type_p (t, TREE_TYPE (arg)))
7695 return 0;
7698 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
7699 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7700 /* Argument is a parameter pack but parameter is not. */
7701 return 0;
7703 break;
7705 default:
7706 gcc_unreachable ();
7709 return 1;
7712 /* Coerce template argument list ARGLIST for use with template
7713 template-parameter TEMPL. */
7715 static tree
7716 coerce_template_args_for_ttp (tree templ, tree arglist,
7717 tsubst_flags_t complain)
7719 /* Consider an example where a template template parameter declared as
7721 template <class T, class U = std::allocator<T> > class TT
7723 The template parameter level of T and U are one level larger than
7724 of TT. To proper process the default argument of U, say when an
7725 instantiation `TT<int>' is seen, we need to build the full
7726 arguments containing {int} as the innermost level. Outer levels,
7727 available when not appearing as default template argument, can be
7728 obtained from the arguments of the enclosing template.
7730 Suppose that TT is later substituted with std::vector. The above
7731 instantiation is `TT<int, std::allocator<T> >' with TT at
7732 level 1, and T at level 2, while the template arguments at level 1
7733 becomes {std::vector} and the inner level 2 is {int}. */
7735 tree outer = DECL_CONTEXT (templ);
7736 if (outer)
7737 outer = generic_targs_for (outer);
7738 else if (current_template_parms)
7740 /* This is an argument of the current template, so we haven't set
7741 DECL_CONTEXT yet. */
7742 tree relevant_template_parms;
7744 /* Parameter levels that are greater than the level of the given
7745 template template parm are irrelevant. */
7746 relevant_template_parms = current_template_parms;
7747 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7748 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7749 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7751 outer = template_parms_to_args (relevant_template_parms);
7754 if (outer)
7755 arglist = add_to_template_args (outer, arglist);
7757 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7758 return coerce_template_parms (parmlist, arglist, templ,
7759 complain,
7760 /*require_all_args=*/true,
7761 /*use_default_args=*/true);
7764 /* A cache of template template parameters with match-all default
7765 arguments. */
7766 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7768 /* T is a bound template template-parameter. Copy its arguments into default
7769 arguments of the template template-parameter's template parameters. */
7771 static tree
7772 add_defaults_to_ttp (tree otmpl)
7774 if (tree *c = hash_map_safe_get (defaulted_ttp_cache, otmpl))
7775 return *c;
7777 tree ntmpl = copy_node (otmpl);
7779 tree ntype = copy_node (TREE_TYPE (otmpl));
7780 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7781 TYPE_MAIN_VARIANT (ntype) = ntype;
7782 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7783 TYPE_NAME (ntype) = ntmpl;
7784 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7786 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7787 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7788 TEMPLATE_PARM_DECL (idx) = ntmpl;
7789 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7791 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7792 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7793 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7794 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7795 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7797 tree o = TREE_VEC_ELT (vec, i);
7798 if (!template_parameter_pack_p (TREE_VALUE (o)))
7800 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7801 TREE_PURPOSE (n) = any_targ_node;
7805 hash_map_safe_put<hm_ggc> (defaulted_ttp_cache, otmpl, ntmpl);
7806 return ntmpl;
7809 /* ARG is a bound potential template template-argument, and PARGS is a list
7810 of arguments for the corresponding template template-parameter. Adjust
7811 PARGS as appropriate for application to ARG's template, and if ARG is a
7812 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7813 arguments to the template template parameter. */
7815 static tree
7816 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7818 ++processing_template_decl;
7819 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7820 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7822 /* When comparing two template template-parameters in partial ordering,
7823 rewrite the one currently being used as an argument to have default
7824 arguments for all parameters. */
7825 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7826 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7827 if (pargs != error_mark_node)
7828 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7829 TYPE_TI_ARGS (arg));
7831 else
7833 tree aparms
7834 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7835 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7836 /*require_all*/true,
7837 /*use_default*/true);
7839 --processing_template_decl;
7840 return pargs;
7843 /* Subroutine of unify for the case when PARM is a
7844 BOUND_TEMPLATE_TEMPLATE_PARM. */
7846 static int
7847 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7848 bool explain_p)
7850 tree parmvec = TYPE_TI_ARGS (parm);
7851 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7853 /* The template template parm might be variadic and the argument
7854 not, so flatten both argument lists. */
7855 parmvec = expand_template_argument_pack (parmvec);
7856 argvec = expand_template_argument_pack (argvec);
7858 if (flag_new_ttp)
7860 /* In keeping with P0522R0, adjust P's template arguments
7861 to apply to A's template; then flatten it again. */
7862 tree nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7863 nparmvec = expand_template_argument_pack (nparmvec);
7865 if (unify (tparms, targs, nparmvec, argvec,
7866 UNIFY_ALLOW_NONE, explain_p))
7867 return 1;
7869 /* If the P0522 adjustment eliminated a pack expansion, deduce
7870 empty packs. */
7871 if (flag_new_ttp
7872 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7873 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7874 DEDUCE_EXACT, /*sub*/true, explain_p))
7875 return 1;
7877 else
7879 /* Deduce arguments T, i from TT<T> or TT<i>.
7880 We check each element of PARMVEC and ARGVEC individually
7881 rather than the whole TREE_VEC since they can have
7882 different number of elements, which is allowed under N2555. */
7884 int len = TREE_VEC_LENGTH (parmvec);
7886 /* Check if the parameters end in a pack, making them
7887 variadic. */
7888 int parm_variadic_p = 0;
7889 if (len > 0
7890 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7891 parm_variadic_p = 1;
7893 for (int i = 0; i < len - parm_variadic_p; ++i)
7894 /* If the template argument list of P contains a pack
7895 expansion that is not the last template argument, the
7896 entire template argument list is a non-deduced
7897 context. */
7898 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7899 return unify_success (explain_p);
7901 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7902 return unify_too_few_arguments (explain_p,
7903 TREE_VEC_LENGTH (argvec), len);
7905 for (int i = 0; i < len - parm_variadic_p; ++i)
7906 if (unify (tparms, targs,
7907 TREE_VEC_ELT (parmvec, i),
7908 TREE_VEC_ELT (argvec, i),
7909 UNIFY_ALLOW_NONE, explain_p))
7910 return 1;
7912 if (parm_variadic_p
7913 && unify_pack_expansion (tparms, targs,
7914 parmvec, argvec,
7915 DEDUCE_EXACT,
7916 /*subr=*/true, explain_p))
7917 return 1;
7920 return 0;
7923 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7924 template template parameters. Both PARM_PARMS and ARG_PARMS are
7925 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7926 or PARM_DECL.
7928 Consider the example:
7929 template <class T> class A;
7930 template<template <class U> class TT> class B;
7932 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7933 the parameters to A, and OUTER_ARGS contains A. */
7935 static int
7936 coerce_template_template_parms (tree parm_parms,
7937 tree arg_parms,
7938 tsubst_flags_t complain,
7939 tree in_decl,
7940 tree outer_args)
7942 int nparms, nargs, i;
7943 tree parm, arg;
7944 int variadic_p = 0;
7946 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7947 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7949 nparms = TREE_VEC_LENGTH (parm_parms);
7950 nargs = TREE_VEC_LENGTH (arg_parms);
7952 if (flag_new_ttp)
7954 /* P0522R0: A template template-parameter P is at least as specialized as
7955 a template template-argument A if, given the following rewrite to two
7956 function templates, the function template corresponding to P is at
7957 least as specialized as the function template corresponding to A
7958 according to the partial ordering rules for function templates
7959 ([temp.func.order]). Given an invented class template X with the
7960 template parameter list of A (including default arguments):
7962 * Each of the two function templates has the same template parameters,
7963 respectively, as P or A.
7965 * Each function template has a single function parameter whose type is
7966 a specialization of X with template arguments corresponding to the
7967 template parameters from the respective function template where, for
7968 each template parameter PP in the template parameter list of the
7969 function template, a corresponding template argument AA is formed. If
7970 PP declares a parameter pack, then AA is the pack expansion
7971 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7973 If the rewrite produces an invalid type, then P is not at least as
7974 specialized as A. */
7976 /* So coerce P's args to apply to A's parms, and then deduce between A's
7977 args and the converted args. If that succeeds, A is at least as
7978 specialized as P, so they match.*/
7979 tree pargs = template_parms_level_to_args (parm_parms);
7980 pargs = add_outermost_template_args (outer_args, pargs);
7981 ++processing_template_decl;
7982 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7983 /*require_all*/true, /*use_default*/true);
7984 --processing_template_decl;
7985 if (pargs != error_mark_node)
7987 tree targs = make_tree_vec (nargs);
7988 tree aargs = template_parms_level_to_args (arg_parms);
7989 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7990 /*explain*/false))
7991 return 1;
7995 /* Determine whether we have a parameter pack at the end of the
7996 template template parameter's template parameter list. */
7997 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7999 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
8001 if (error_operand_p (parm))
8002 return 0;
8004 switch (TREE_CODE (parm))
8006 case TEMPLATE_DECL:
8007 case TYPE_DECL:
8008 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
8009 variadic_p = 1;
8010 break;
8012 case PARM_DECL:
8013 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
8014 variadic_p = 1;
8015 break;
8017 default:
8018 gcc_unreachable ();
8022 if (nargs != nparms
8023 && !(variadic_p && nargs >= nparms - 1))
8024 return 0;
8026 /* Check all of the template parameters except the parameter pack at
8027 the end (if any). */
8028 for (i = 0; i < nparms - variadic_p; ++i)
8030 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
8031 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
8032 continue;
8034 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
8035 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
8037 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
8038 outer_args))
8039 return 0;
8043 if (variadic_p)
8045 /* Check each of the template parameters in the template
8046 argument against the template parameter pack at the end of
8047 the template template parameter. */
8048 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
8049 return 0;
8051 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
8053 for (; i < nargs; ++i)
8055 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
8056 continue;
8058 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
8060 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
8061 outer_args))
8062 return 0;
8066 return 1;
8069 /* Verifies that the deduced template arguments (in TARGS) for the
8070 template template parameters (in TPARMS) represent valid bindings,
8071 by comparing the template parameter list of each template argument
8072 to the template parameter list of its corresponding template
8073 template parameter, in accordance with DR150. This
8074 routine can only be called after all template arguments have been
8075 deduced. It will return TRUE if all of the template template
8076 parameter bindings are okay, FALSE otherwise. */
8077 bool
8078 template_template_parm_bindings_ok_p (tree tparms, tree targs)
8080 int i, ntparms = TREE_VEC_LENGTH (tparms);
8081 bool ret = true;
8083 /* We're dealing with template parms in this process. */
8084 ++processing_template_decl;
8086 targs = INNERMOST_TEMPLATE_ARGS (targs);
8088 for (i = 0; i < ntparms; ++i)
8090 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
8091 tree targ = TREE_VEC_ELT (targs, i);
8093 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
8095 tree packed_args = NULL_TREE;
8096 int idx, len = 1;
8098 if (ARGUMENT_PACK_P (targ))
8100 /* Look inside the argument pack. */
8101 packed_args = ARGUMENT_PACK_ARGS (targ);
8102 len = TREE_VEC_LENGTH (packed_args);
8105 for (idx = 0; idx < len; ++idx)
8107 tree targ_parms = NULL_TREE;
8109 if (packed_args)
8110 /* Extract the next argument from the argument
8111 pack. */
8112 targ = TREE_VEC_ELT (packed_args, idx);
8114 if (PACK_EXPANSION_P (targ))
8115 /* Look at the pattern of the pack expansion. */
8116 targ = PACK_EXPANSION_PATTERN (targ);
8118 /* Extract the template parameters from the template
8119 argument. */
8120 if (TREE_CODE (targ) == TEMPLATE_DECL)
8121 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
8122 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
8123 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
8125 /* Verify that we can coerce the template template
8126 parameters from the template argument to the template
8127 parameter. This requires an exact match. */
8128 if (targ_parms
8129 && !coerce_template_template_parms
8130 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
8131 targ_parms,
8132 tf_none,
8133 tparm,
8134 targs))
8136 ret = false;
8137 goto out;
8143 out:
8145 --processing_template_decl;
8146 return ret;
8149 /* Since type attributes aren't mangled, we need to strip them from
8150 template type arguments. */
8152 tree
8153 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
8155 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
8156 return arg;
8157 bool removed_attributes = false;
8158 tree canon = strip_typedefs (arg, &removed_attributes);
8159 if (removed_attributes
8160 && (complain & tf_warning))
8161 warning (OPT_Wignored_attributes,
8162 "ignoring attributes on template argument %qT", arg);
8163 return canon;
8166 /* And from inside dependent non-type arguments like sizeof(Type). */
8168 static tree
8169 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
8171 if (!arg || arg == error_mark_node)
8172 return arg;
8173 bool removed_attributes = false;
8174 tree canon = strip_typedefs_expr (arg, &removed_attributes);
8175 if (removed_attributes
8176 && (complain & tf_warning))
8177 warning (OPT_Wignored_attributes,
8178 "ignoring attributes in template argument %qE", arg);
8179 return canon;
8182 /* A template declaration can be substituted for a constrained
8183 template template parameter only when the argument is no more
8184 constrained than the parameter. */
8186 static bool
8187 is_compatible_template_arg (tree parm, tree arg)
8189 tree parm_cons = get_constraints (parm);
8191 /* For now, allow constrained template template arguments
8192 and unconstrained template template parameters. */
8193 if (parm_cons == NULL_TREE)
8194 return true;
8196 /* If the template parameter is constrained, we need to rewrite its
8197 constraints in terms of the ARG's template parameters. This ensures
8198 that all of the template parameter types will have the same depth.
8200 Note that this is only valid when coerce_template_template_parm is
8201 true for the innermost template parameters of PARM and ARG. In other
8202 words, because coercion is successful, this conversion will be valid. */
8203 tree new_args = NULL_TREE;
8204 if (parm_cons)
8206 tree aparms = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8207 new_args = template_parms_level_to_args (aparms);
8208 parm_cons = tsubst_constraint_info (parm_cons, new_args,
8209 tf_none, NULL_TREE);
8210 if (parm_cons == error_mark_node)
8211 return false;
8214 return weakly_subsumes (parm_cons, arg);
8217 // Convert a placeholder argument into a binding to the original
8218 // parameter. The original parameter is saved as the TREE_TYPE of
8219 // ARG.
8220 static inline tree
8221 convert_wildcard_argument (tree parm, tree arg)
8223 TREE_TYPE (arg) = parm;
8224 return arg;
8227 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
8228 because one of them is dependent. But we need to represent the
8229 conversion for the benefit of cp_tree_equal. */
8231 static tree
8232 maybe_convert_nontype_argument (tree type, tree arg)
8234 /* Auto parms get no conversion. */
8235 if (type_uses_auto (type))
8236 return arg;
8237 /* We don't need or want to add this conversion now if we're going to use the
8238 argument for deduction. */
8239 if (value_dependent_expression_p (arg))
8240 return arg;
8242 type = cv_unqualified (type);
8243 tree argtype = TREE_TYPE (arg);
8244 if (same_type_p (type, argtype))
8245 return arg;
8247 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
8248 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
8249 return arg;
8252 /* Convert the indicated template ARG as necessary to match the
8253 indicated template PARM. Returns the converted ARG, or
8254 error_mark_node if the conversion was unsuccessful. Error and
8255 warning messages are issued under control of COMPLAIN. This
8256 conversion is for the Ith parameter in the parameter list. ARGS is
8257 the full set of template arguments deduced so far. */
8259 static tree
8260 convert_template_argument (tree parm,
8261 tree arg,
8262 tree args,
8263 tsubst_flags_t complain,
8264 int i,
8265 tree in_decl)
8267 tree orig_arg;
8268 tree val;
8269 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
8271 if (parm == error_mark_node || error_operand_p (arg))
8272 return error_mark_node;
8274 /* Trivially convert placeholders. */
8275 if (TREE_CODE (arg) == WILDCARD_DECL)
8276 return convert_wildcard_argument (parm, arg);
8278 if (arg == any_targ_node)
8279 return arg;
8281 if (TREE_CODE (arg) == TREE_LIST
8282 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
8284 /* The template argument was the name of some
8285 member function. That's usually
8286 invalid, but static members are OK. In any
8287 case, grab the underlying fields/functions
8288 and issue an error later if required. */
8289 TREE_TYPE (arg) = unknown_type_node;
8292 orig_arg = arg;
8294 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
8295 requires_type = (TREE_CODE (parm) == TYPE_DECL
8296 || requires_tmpl_type);
8298 /* When determining whether an argument pack expansion is a template,
8299 look at the pattern. */
8300 if (PACK_EXPANSION_P (arg))
8301 arg = PACK_EXPANSION_PATTERN (arg);
8303 /* Deal with an injected-class-name used as a template template arg. */
8304 if (requires_tmpl_type && CLASS_TYPE_P (arg))
8306 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
8307 if (TREE_CODE (t) == TEMPLATE_DECL)
8309 if (cxx_dialect >= cxx11)
8310 /* OK under DR 1004. */;
8311 else if (complain & tf_warning_or_error)
8312 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
8313 " used as template template argument", TYPE_NAME (arg));
8314 else if (flag_pedantic_errors)
8315 t = arg;
8317 arg = t;
8321 is_tmpl_type =
8322 ((TREE_CODE (arg) == TEMPLATE_DECL
8323 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
8324 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
8325 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8326 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
8328 if (is_tmpl_type
8329 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
8330 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
8331 arg = TYPE_STUB_DECL (arg);
8333 is_type = TYPE_P (arg) || is_tmpl_type;
8335 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
8336 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
8338 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
8340 if (complain & tf_error)
8341 error ("invalid use of destructor %qE as a type", orig_arg);
8342 return error_mark_node;
8345 permerror (input_location,
8346 "to refer to a type member of a template parameter, "
8347 "use %<typename %E%>", orig_arg);
8349 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
8350 TREE_OPERAND (arg, 1),
8351 typename_type,
8352 complain);
8353 arg = orig_arg;
8354 is_type = 1;
8356 if (is_type != requires_type)
8358 if (in_decl)
8360 if (complain & tf_error)
8362 error ("type/value mismatch at argument %d in template "
8363 "parameter list for %qD",
8364 i + 1, in_decl);
8365 if (is_type)
8367 /* The template argument is a type, but we're expecting
8368 an expression. */
8369 inform (input_location,
8370 " expected a constant of type %qT, got %qT",
8371 TREE_TYPE (parm),
8372 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
8373 /* [temp.arg]/2: "In a template-argument, an ambiguity
8374 between a type-id and an expression is resolved to a
8375 type-id, regardless of the form of the corresponding
8376 template-parameter." So give the user a clue. */
8377 if (TREE_CODE (arg) == FUNCTION_TYPE)
8378 inform (input_location, " ambiguous template argument "
8379 "for non-type template parameter is treated as "
8380 "function type");
8382 else if (requires_tmpl_type)
8383 inform (input_location,
8384 " expected a class template, got %qE", orig_arg);
8385 else
8386 inform (input_location,
8387 " expected a type, got %qE", orig_arg);
8390 return error_mark_node;
8392 if (is_tmpl_type ^ requires_tmpl_type)
8394 if (in_decl && (complain & tf_error))
8396 error ("type/value mismatch at argument %d in template "
8397 "parameter list for %qD",
8398 i + 1, in_decl);
8399 if (is_tmpl_type)
8400 inform (input_location,
8401 " expected a type, got %qT", DECL_NAME (arg));
8402 else
8403 inform (input_location,
8404 " expected a class template, got %qT", orig_arg);
8406 return error_mark_node;
8409 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
8410 /* We already did the appropriate conversion when packing args. */
8411 val = orig_arg;
8412 else if (is_type)
8414 if (requires_tmpl_type)
8416 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
8417 /* The number of argument required is not known yet.
8418 Just accept it for now. */
8419 val = orig_arg;
8420 else
8422 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
8423 tree argparm;
8425 /* Strip alias templates that are equivalent to another
8426 template. */
8427 arg = get_underlying_template (arg);
8428 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
8430 if (coerce_template_template_parms (parmparm, argparm,
8431 complain, in_decl,
8432 args))
8434 val = arg;
8436 /* TEMPLATE_TEMPLATE_PARM node is preferred over
8437 TEMPLATE_DECL. */
8438 if (val != error_mark_node)
8440 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
8441 val = TREE_TYPE (val);
8442 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
8443 val = make_pack_expansion (val, complain);
8446 else
8448 if (in_decl && (complain & tf_error))
8450 error ("type/value mismatch at argument %d in "
8451 "template parameter list for %qD",
8452 i + 1, in_decl);
8453 inform (input_location,
8454 " expected a template of type %qD, got %qT",
8455 parm, orig_arg);
8458 val = error_mark_node;
8461 // Check that the constraints are compatible before allowing the
8462 // substitution.
8463 if (val != error_mark_node)
8464 if (!is_compatible_template_arg (parm, arg))
8466 if (in_decl && (complain & tf_error))
8468 error ("constraint mismatch at argument %d in "
8469 "template parameter list for %qD",
8470 i + 1, in_decl);
8471 inform (input_location, " expected %qD but got %qD",
8472 parm, arg);
8474 val = error_mark_node;
8478 else
8479 val = orig_arg;
8480 /* We only form one instance of each template specialization.
8481 Therefore, if we use a non-canonical variant (i.e., a
8482 typedef), any future messages referring to the type will use
8483 the typedef, which is confusing if those future uses do not
8484 themselves also use the typedef. */
8485 if (TYPE_P (val))
8486 val = canonicalize_type_argument (val, complain);
8488 else
8490 tree t = TREE_TYPE (parm);
8492 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm))
8493 > TMPL_ARGS_DEPTH (args))
8494 /* We don't have enough levels of args to do any substitution. This
8495 can happen in the context of -fnew-ttp-matching. */;
8496 else if (tree a = type_uses_auto (t))
8498 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
8499 if (t == error_mark_node)
8500 return error_mark_node;
8502 else
8503 t = tsubst (t, args, complain, in_decl);
8505 if (invalid_nontype_parm_type_p (t, complain))
8506 return error_mark_node;
8508 /* Drop top-level cv-qualifiers on the substituted/deduced type of
8509 this non-type template parameter, as per [temp.param]/6. */
8510 t = cv_unqualified (t);
8512 if (t != TREE_TYPE (parm))
8513 t = canonicalize_type_argument (t, complain);
8515 if (!type_dependent_expression_p (orig_arg)
8516 && !uses_template_parms (t))
8517 /* We used to call digest_init here. However, digest_init
8518 will report errors, which we don't want when complain
8519 is zero. More importantly, digest_init will try too
8520 hard to convert things: for example, `0' should not be
8521 converted to pointer type at this point according to
8522 the standard. Accepting this is not merely an
8523 extension, since deciding whether or not these
8524 conversions can occur is part of determining which
8525 function template to call, or whether a given explicit
8526 argument specification is valid. */
8527 val = convert_nontype_argument (t, orig_arg, complain);
8528 else
8530 val = canonicalize_expr_argument (orig_arg, complain);
8531 val = maybe_convert_nontype_argument (t, val);
8535 if (val == NULL_TREE)
8536 val = error_mark_node;
8537 else if (val == error_mark_node && (complain & tf_error))
8538 error_at (cp_expr_loc_or_input_loc (orig_arg),
8539 "could not convert template argument %qE from %qT to %qT",
8540 orig_arg, TREE_TYPE (orig_arg), t);
8542 if (INDIRECT_REF_P (val))
8544 /* Reject template arguments that are references to built-in
8545 functions with no library fallbacks. */
8546 const_tree inner = TREE_OPERAND (val, 0);
8547 const_tree innertype = TREE_TYPE (inner);
8548 if (innertype
8549 && TYPE_REF_P (innertype)
8550 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
8551 && TREE_OPERAND_LENGTH (inner) > 0
8552 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
8553 return error_mark_node;
8556 if (TREE_CODE (val) == SCOPE_REF)
8558 /* Strip typedefs from the SCOPE_REF. */
8559 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
8560 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
8561 complain);
8562 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
8563 QUALIFIED_NAME_IS_TEMPLATE (val));
8567 return val;
8570 /* Coerces the remaining template arguments in INNER_ARGS (from
8571 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8572 Returns the coerced argument pack. PARM_IDX is the position of this
8573 parameter in the template parameter list. ARGS is the original
8574 template argument list. */
8575 static tree
8576 coerce_template_parameter_pack (tree parms,
8577 int parm_idx,
8578 tree args,
8579 tree inner_args,
8580 int arg_idx,
8581 tree new_args,
8582 int* lost,
8583 tree in_decl,
8584 tsubst_flags_t complain)
8586 tree parm = TREE_VEC_ELT (parms, parm_idx);
8587 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8588 tree packed_args;
8589 tree argument_pack;
8590 tree packed_parms = NULL_TREE;
8592 if (arg_idx > nargs)
8593 arg_idx = nargs;
8595 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
8597 /* When the template parameter is a non-type template parameter pack
8598 or template template parameter pack whose type or template
8599 parameters use parameter packs, we know exactly how many arguments
8600 we are looking for. Build a vector of the instantiated decls for
8601 these template parameters in PACKED_PARMS. */
8602 /* We can't use make_pack_expansion here because it would interpret a
8603 _DECL as a use rather than a declaration. */
8604 tree decl = TREE_VALUE (parm);
8605 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
8606 SET_PACK_EXPANSION_PATTERN (exp, decl);
8607 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
8608 SET_TYPE_STRUCTURAL_EQUALITY (exp);
8610 TREE_VEC_LENGTH (args)--;
8611 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
8612 TREE_VEC_LENGTH (args)++;
8614 if (packed_parms == error_mark_node)
8615 return error_mark_node;
8617 /* If we're doing a partial instantiation of a member template,
8618 verify that all of the types used for the non-type
8619 template parameter pack are, in fact, valid for non-type
8620 template parameters. */
8621 if (arg_idx < nargs
8622 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
8624 int j, len = TREE_VEC_LENGTH (packed_parms);
8625 for (j = 0; j < len; ++j)
8627 tree t = TREE_VEC_ELT (packed_parms, j);
8628 if (TREE_CODE (t) == PARM_DECL
8629 && invalid_nontype_parm_type_p (TREE_TYPE (t), complain))
8630 return error_mark_node;
8632 /* We don't know how many args we have yet, just
8633 use the unconverted ones for now. */
8634 return NULL_TREE;
8637 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
8639 /* Check if we have a placeholder pack, which indicates we're
8640 in the context of a introduction list. In that case we want
8641 to match this pack to the single placeholder. */
8642 else if (arg_idx < nargs
8643 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
8644 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
8646 nargs = arg_idx + 1;
8647 packed_args = make_tree_vec (1);
8649 else
8650 packed_args = make_tree_vec (nargs - arg_idx);
8652 /* Convert the remaining arguments, which will be a part of the
8653 parameter pack "parm". */
8654 int first_pack_arg = arg_idx;
8655 for (; arg_idx < nargs; ++arg_idx)
8657 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
8658 tree actual_parm = TREE_VALUE (parm);
8659 int pack_idx = arg_idx - first_pack_arg;
8661 if (packed_parms)
8663 /* Once we've packed as many args as we have types, stop. */
8664 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
8665 break;
8666 else if (PACK_EXPANSION_P (arg))
8667 /* We don't know how many args we have yet, just
8668 use the unconverted ones for now. */
8669 return NULL_TREE;
8670 else
8671 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
8674 if (arg == error_mark_node)
8676 if (complain & tf_error)
8677 error ("template argument %d is invalid", arg_idx + 1);
8679 else
8680 arg = convert_template_argument (actual_parm,
8681 arg, new_args, complain, parm_idx,
8682 in_decl);
8683 if (arg == error_mark_node)
8684 (*lost)++;
8685 TREE_VEC_ELT (packed_args, pack_idx) = arg;
8688 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
8689 && TREE_VEC_LENGTH (packed_args) > 0)
8691 if (complain & tf_error)
8692 error ("wrong number of template arguments (%d, should be %d)",
8693 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
8694 return error_mark_node;
8697 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
8698 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
8699 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
8700 else
8702 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
8703 TREE_CONSTANT (argument_pack) = 1;
8706 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
8707 if (CHECKING_P)
8708 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
8709 TREE_VEC_LENGTH (packed_args));
8710 return argument_pack;
8713 /* Returns the number of pack expansions in the template argument vector
8714 ARGS. */
8716 static int
8717 pack_expansion_args_count (tree args)
8719 int i;
8720 int count = 0;
8721 if (args)
8722 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8724 tree elt = TREE_VEC_ELT (args, i);
8725 if (elt && PACK_EXPANSION_P (elt))
8726 ++count;
8728 return count;
8731 /* Convert all template arguments to their appropriate types, and
8732 return a vector containing the innermost resulting template
8733 arguments. If any error occurs, return error_mark_node. Error and
8734 warning messages are issued under control of COMPLAIN.
8736 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8737 for arguments not specified in ARGS. Otherwise, if
8738 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8739 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8740 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8741 ARGS. */
8743 static tree
8744 coerce_template_parms (tree parms,
8745 tree args,
8746 tree in_decl,
8747 tsubst_flags_t complain,
8748 bool require_all_args,
8749 bool use_default_args)
8751 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8752 tree orig_inner_args;
8753 tree inner_args;
8754 tree new_args;
8755 tree new_inner_args;
8757 /* When used as a boolean value, indicates whether this is a
8758 variadic template parameter list. Since it's an int, we can also
8759 subtract it from nparms to get the number of non-variadic
8760 parameters. */
8761 int variadic_p = 0;
8762 int variadic_args_p = 0;
8763 int post_variadic_parms = 0;
8765 /* Adjustment to nparms for fixed parameter packs. */
8766 int fixed_pack_adjust = 0;
8767 int fixed_packs = 0;
8768 int missing = 0;
8770 /* Likewise for parameters with default arguments. */
8771 int default_p = 0;
8773 if (args == error_mark_node)
8774 return error_mark_node;
8776 nparms = TREE_VEC_LENGTH (parms);
8778 /* Determine if there are any parameter packs or default arguments. */
8779 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8781 tree parm = TREE_VEC_ELT (parms, parm_idx);
8782 if (variadic_p)
8783 ++post_variadic_parms;
8784 if (template_parameter_pack_p (TREE_VALUE (parm)))
8785 ++variadic_p;
8786 if (TREE_PURPOSE (parm))
8787 ++default_p;
8790 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8791 /* If there are no parameters that follow a parameter pack, we need to
8792 expand any argument packs so that we can deduce a parameter pack from
8793 some non-packed args followed by an argument pack, as in variadic85.C.
8794 If there are such parameters, we need to leave argument packs intact
8795 so the arguments are assigned properly. This can happen when dealing
8796 with a nested class inside a partial specialization of a class
8797 template, as in variadic92.C, or when deducing a template parameter pack
8798 from a sub-declarator, as in variadic114.C. */
8799 if (!post_variadic_parms)
8800 inner_args = expand_template_argument_pack (inner_args);
8802 /* Count any pack expansion args. */
8803 variadic_args_p = pack_expansion_args_count (inner_args);
8805 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8806 if ((nargs - variadic_args_p > nparms && !variadic_p)
8807 || (nargs < nparms - variadic_p
8808 && require_all_args
8809 && !variadic_args_p
8810 && (!use_default_args
8811 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8812 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8814 bad_nargs:
8815 if (complain & tf_error)
8817 if (variadic_p || default_p)
8819 nparms -= variadic_p + default_p;
8820 error ("wrong number of template arguments "
8821 "(%d, should be at least %d)", nargs, nparms);
8823 else
8824 error ("wrong number of template arguments "
8825 "(%d, should be %d)", nargs, nparms);
8827 if (in_decl)
8828 inform (DECL_SOURCE_LOCATION (in_decl),
8829 "provided for %qD", in_decl);
8832 return error_mark_node;
8834 /* We can't pass a pack expansion to a non-pack parameter of an alias
8835 template (DR 1430). */
8836 else if (in_decl
8837 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8838 || concept_definition_p (in_decl))
8839 && variadic_args_p
8840 && nargs - variadic_args_p < nparms - variadic_p)
8842 if (complain & tf_error)
8844 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8846 tree arg = TREE_VEC_ELT (inner_args, i);
8847 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8849 if (PACK_EXPANSION_P (arg)
8850 && !template_parameter_pack_p (parm))
8852 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8853 error_at (location_of (arg),
8854 "pack expansion argument for non-pack parameter "
8855 "%qD of alias template %qD", parm, in_decl);
8856 else
8857 error_at (location_of (arg),
8858 "pack expansion argument for non-pack parameter "
8859 "%qD of concept %qD", parm, in_decl);
8860 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8861 goto found;
8864 gcc_unreachable ();
8865 found:;
8867 return error_mark_node;
8870 /* We need to evaluate the template arguments, even though this
8871 template-id may be nested within a "sizeof". */
8872 cp_evaluated ev;
8874 new_inner_args = make_tree_vec (nparms);
8875 new_args = add_outermost_template_args (args, new_inner_args);
8876 int pack_adjust = 0;
8877 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8879 tree arg;
8880 tree parm;
8882 /* Get the Ith template parameter. */
8883 parm = TREE_VEC_ELT (parms, parm_idx);
8885 if (parm == error_mark_node)
8887 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8888 continue;
8891 /* Calculate the next argument. */
8892 if (arg_idx < nargs)
8893 arg = TREE_VEC_ELT (inner_args, arg_idx);
8894 else
8895 arg = NULL_TREE;
8897 if (template_parameter_pack_p (TREE_VALUE (parm))
8898 && (arg || require_all_args || !(complain & tf_partial))
8899 && !(arg && ARGUMENT_PACK_P (arg)))
8901 /* Some arguments will be placed in the
8902 template parameter pack PARM. */
8903 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8904 inner_args, arg_idx,
8905 new_args, &lost,
8906 in_decl, complain);
8908 if (arg == NULL_TREE)
8910 /* We don't know how many args we have yet, just use the
8911 unconverted (and still packed) ones for now. */
8912 new_inner_args = orig_inner_args;
8913 arg_idx = nargs;
8914 break;
8917 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8919 /* Store this argument. */
8920 if (arg == error_mark_node)
8922 lost++;
8923 /* We are done with all of the arguments. */
8924 arg_idx = nargs;
8925 break;
8927 else
8929 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8930 arg_idx += pack_adjust;
8931 if (fixed_parameter_pack_p (TREE_VALUE (parm)))
8933 ++fixed_packs;
8934 fixed_pack_adjust += pack_adjust;
8938 continue;
8940 else if (arg)
8942 if (PACK_EXPANSION_P (arg))
8944 /* "If every valid specialization of a variadic template
8945 requires an empty template parameter pack, the template is
8946 ill-formed, no diagnostic required." So check that the
8947 pattern works with this parameter. */
8948 tree pattern = PACK_EXPANSION_PATTERN (arg);
8949 tree conv = convert_template_argument (TREE_VALUE (parm),
8950 pattern, new_args,
8951 complain, parm_idx,
8952 in_decl);
8953 if (conv == error_mark_node)
8955 if (complain & tf_error)
8956 inform (input_location, "so any instantiation with a "
8957 "non-empty parameter pack would be ill-formed");
8958 ++lost;
8960 else if (TYPE_P (conv) && !TYPE_P (pattern))
8961 /* Recover from missing typename. */
8962 TREE_VEC_ELT (inner_args, arg_idx)
8963 = make_pack_expansion (conv, complain);
8965 /* We don't know how many args we have yet, just
8966 use the unconverted ones for now. */
8967 new_inner_args = inner_args;
8968 arg_idx = nargs;
8969 break;
8972 else if (require_all_args)
8974 /* There must be a default arg in this case. */
8975 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8976 complain, in_decl);
8977 /* The position of the first default template argument,
8978 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8979 Record that. */
8980 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8981 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8982 arg_idx - pack_adjust);
8984 else
8985 break;
8987 if (arg == error_mark_node)
8989 if (complain & tf_error)
8990 error ("template argument %d is invalid", arg_idx + 1);
8992 else if (!arg)
8994 /* This can occur if there was an error in the template
8995 parameter list itself (which we would already have
8996 reported) that we are trying to recover from, e.g., a class
8997 template with a parameter list such as
8998 template<typename..., typename> (cpp0x/variadic150.C). */
8999 ++lost;
9001 /* This can also happen with a fixed parameter pack (71834). */
9002 if (arg_idx >= nargs)
9003 ++missing;
9005 else
9006 arg = convert_template_argument (TREE_VALUE (parm),
9007 arg, new_args, complain,
9008 parm_idx, in_decl);
9010 if (arg == error_mark_node)
9011 lost++;
9013 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
9016 if (missing || arg_idx < nargs - variadic_args_p)
9018 /* If we had fixed parameter packs, we didn't know how many arguments we
9019 actually needed earlier; now we do. */
9020 nparms += fixed_pack_adjust;
9021 variadic_p -= fixed_packs;
9022 goto bad_nargs;
9025 if (arg_idx < nargs)
9027 /* We had some pack expansion arguments that will only work if the packs
9028 are empty, but wait until instantiation time to complain.
9029 See variadic-ttp3.C. */
9031 /* Except that we can't provide empty packs to alias templates or
9032 concepts when there are no corresponding parameters. Basically,
9033 we can get here with this:
9035 template<typename T> concept C = true;
9037 template<typename... Args>
9038 requires C<Args...>
9039 void f();
9041 When parsing C<Args...>, we try to form a concept check of
9042 C<?, Args...>. Without the extra check for substituting an empty
9043 pack past the last parameter, we can accept the check as valid.
9045 FIXME: This may be valid for alias templates (but I doubt it).
9047 FIXME: The error could be better also. */
9048 if (in_decl && concept_definition_p (in_decl))
9050 if (complain & tf_error)
9051 error_at (location_of (TREE_VEC_ELT (args, arg_idx)),
9052 "too many arguments");
9053 return error_mark_node;
9056 int len = nparms + (nargs - arg_idx);
9057 tree args = make_tree_vec (len);
9058 int i = 0;
9059 for (; i < nparms; ++i)
9060 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (new_inner_args, i);
9061 for (; i < len; ++i, ++arg_idx)
9062 TREE_VEC_ELT (args, i) = TREE_VEC_ELT (inner_args,
9063 arg_idx - pack_adjust);
9064 new_inner_args = args;
9067 if (lost)
9069 gcc_assert (!(complain & tf_error) || seen_error ());
9070 return error_mark_node;
9073 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
9074 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
9075 TREE_VEC_LENGTH (new_inner_args));
9077 return new_inner_args;
9080 /* Convert all template arguments to their appropriate types, and
9081 return a vector containing the innermost resulting template
9082 arguments. If any error occurs, return error_mark_node. Error and
9083 warning messages are not issued.
9085 Note that no function argument deduction is performed, and default
9086 arguments are used to fill in unspecified arguments. */
9087 tree
9088 coerce_template_parms (tree parms, tree args, tree in_decl)
9090 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
9093 /* Convert all template arguments to their appropriate type, and
9094 instantiate default arguments as needed. This returns a vector
9095 containing the innermost resulting template arguments, or
9096 error_mark_node if unsuccessful. */
9097 tree
9098 coerce_template_parms (tree parms, tree args, tree in_decl,
9099 tsubst_flags_t complain)
9101 return coerce_template_parms (parms, args, in_decl, complain, true, true);
9104 /* Like coerce_template_parms. If PARMS represents all template
9105 parameters levels, this function returns a vector of vectors
9106 representing all the resulting argument levels. Note that in this
9107 case, only the innermost arguments are coerced because the
9108 outermost ones are supposed to have been coerced already.
9110 Otherwise, if PARMS represents only (the innermost) vector of
9111 parameters, this function returns a vector containing just the
9112 innermost resulting arguments. */
9114 static tree
9115 coerce_innermost_template_parms (tree parms,
9116 tree args,
9117 tree in_decl,
9118 tsubst_flags_t complain,
9119 bool require_all_args,
9120 bool use_default_args)
9122 int parms_depth = TMPL_PARMS_DEPTH (parms);
9123 int args_depth = TMPL_ARGS_DEPTH (args);
9124 tree coerced_args;
9126 if (parms_depth > 1)
9128 coerced_args = make_tree_vec (parms_depth);
9129 tree level;
9130 int cur_depth;
9132 for (level = parms, cur_depth = parms_depth;
9133 parms_depth > 0 && level != NULL_TREE;
9134 level = TREE_CHAIN (level), --cur_depth)
9136 tree l;
9137 if (cur_depth == args_depth)
9138 l = coerce_template_parms (TREE_VALUE (level),
9139 args, in_decl, complain,
9140 require_all_args,
9141 use_default_args);
9142 else
9143 l = TMPL_ARGS_LEVEL (args, cur_depth);
9145 if (l == error_mark_node)
9146 return error_mark_node;
9148 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
9151 else
9152 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
9153 args, in_decl, complain,
9154 require_all_args,
9155 use_default_args);
9156 return coerced_args;
9159 /* Returns true if T is a wrapper to make a C++20 template parameter
9160 object const. */
9162 static bool
9163 class_nttp_const_wrapper_p (tree t)
9165 if (cxx_dialect < cxx20)
9166 return false;
9167 return (TREE_CODE (t) == VIEW_CONVERT_EXPR
9168 && CP_TYPE_CONST_P (TREE_TYPE (t))
9169 && TREE_CODE (TREE_OPERAND (t, 0)) == TEMPLATE_PARM_INDEX);
9172 /* Returns 1 if template args OT and NT are equivalent. */
9175 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
9177 if (nt == ot)
9178 return 1;
9179 if (nt == NULL_TREE || ot == NULL_TREE)
9180 return false;
9181 if (nt == any_targ_node || ot == any_targ_node)
9182 return true;
9184 if (class_nttp_const_wrapper_p (nt))
9185 nt = TREE_OPERAND (nt, 0);
9186 if (class_nttp_const_wrapper_p (ot))
9187 ot = TREE_OPERAND (ot, 0);
9189 /* DR 1558: Don't treat an alias template specialization with dependent
9190 arguments as equivalent to its underlying type when used as a template
9191 argument; we need them to be distinct so that we substitute into the
9192 specialization arguments at instantiation time. And aliases can't be
9193 equivalent without being ==, so we don't need to look any deeper.
9195 During partial ordering, however, we need to treat them normally so we can
9196 order uses of the same alias with different cv-qualification (79960). */
9197 auto cso = make_temp_override (comparing_dependent_aliases);
9198 if (!partial_order)
9199 ++comparing_dependent_aliases;
9201 if (TREE_CODE (nt) == TREE_VEC || TREE_CODE (ot) == TREE_VEC)
9202 /* For member templates */
9203 return TREE_CODE (ot) == TREE_CODE (nt) && comp_template_args (ot, nt);
9204 else if (PACK_EXPANSION_P (ot) || PACK_EXPANSION_P (nt))
9205 return (PACK_EXPANSION_P (ot) && PACK_EXPANSION_P (nt)
9206 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
9207 PACK_EXPANSION_PATTERN (nt))
9208 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
9209 PACK_EXPANSION_EXTRA_ARGS (nt)));
9210 else if (ARGUMENT_PACK_P (ot) || ARGUMENT_PACK_P (nt))
9211 return cp_tree_equal (ot, nt);
9212 else if (TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
9213 gcc_unreachable ();
9214 else if (TYPE_P (nt) || TYPE_P (ot))
9216 if (!(TYPE_P (nt) && TYPE_P (ot)))
9217 return false;
9218 return same_type_p (ot, nt);
9220 else
9222 /* Try to treat a template non-type argument that has been converted
9223 to the parameter type as equivalent to one that hasn't yet. */
9224 for (enum tree_code code1 = TREE_CODE (ot);
9225 CONVERT_EXPR_CODE_P (code1)
9226 || code1 == NON_LVALUE_EXPR;
9227 code1 = TREE_CODE (ot))
9228 ot = TREE_OPERAND (ot, 0);
9230 for (enum tree_code code2 = TREE_CODE (nt);
9231 CONVERT_EXPR_CODE_P (code2)
9232 || code2 == NON_LVALUE_EXPR;
9233 code2 = TREE_CODE (nt))
9234 nt = TREE_OPERAND (nt, 0);
9236 return cp_tree_equal (ot, nt);
9240 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
9241 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
9242 NEWARG_PTR with the offending arguments if they are non-NULL. */
9245 comp_template_args (tree oldargs, tree newargs,
9246 tree *oldarg_ptr, tree *newarg_ptr,
9247 bool partial_order)
9249 int i;
9251 if (oldargs == newargs)
9252 return 1;
9254 if (!oldargs || !newargs)
9255 return 0;
9257 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
9258 return 0;
9260 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
9262 tree nt = TREE_VEC_ELT (newargs, i);
9263 tree ot = TREE_VEC_ELT (oldargs, i);
9265 if (! template_args_equal (ot, nt, partial_order))
9267 if (oldarg_ptr != NULL)
9268 *oldarg_ptr = ot;
9269 if (newarg_ptr != NULL)
9270 *newarg_ptr = nt;
9271 return 0;
9274 return 1;
9277 inline bool
9278 comp_template_args_porder (tree oargs, tree nargs)
9280 return comp_template_args (oargs, nargs, NULL, NULL, true);
9283 /* Implement a freelist interface for objects of type T.
9285 Head is a separate object, rather than a regular member, so that we
9286 can define it as a GTY deletable pointer, which is highly
9287 desirable. A data member could be declared that way, but then the
9288 containing object would implicitly get GTY((user)), which would
9289 prevent us from instantiating freelists as global objects.
9290 Although this way we can create freelist global objects, they're
9291 such thin wrappers that instantiating temporaries at every use
9292 loses nothing and saves permanent storage for the freelist object.
9294 Member functions next, anew, poison and reinit have default
9295 implementations that work for most of the types we're interested
9296 in, but if they don't work for some type, they should be explicitly
9297 specialized. See the comments before them for requirements, and
9298 the example specializations for the tree_list_freelist. */
9299 template <typename T>
9300 class freelist
9302 /* Return the next object in a chain. We could just do type
9303 punning, but if we access the object with its underlying type, we
9304 avoid strict-aliasing trouble. This needs only work between
9305 poison and reinit. */
9306 static T *&next (T *obj) { return obj->next; }
9308 /* Return a newly allocated, uninitialized or minimally-initialized
9309 object of type T. Any initialization performed by anew should
9310 either remain across the life of the object and the execution of
9311 poison, or be redone by reinit. */
9312 static T *anew () { return ggc_alloc<T> (); }
9314 /* Optionally scribble all over the bits holding the object, so that
9315 they become (mostly?) uninitialized memory. This is called while
9316 preparing to make the object part of the free list. */
9317 static void poison (T *obj) {
9318 T *p ATTRIBUTE_UNUSED = obj;
9319 T **q ATTRIBUTE_UNUSED = &next (obj);
9321 #ifdef ENABLE_GC_CHECKING
9322 /* Poison the data, to indicate the data is garbage. */
9323 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, sizeof (*p)));
9324 memset (p, 0xa5, sizeof (*p));
9325 #endif
9326 /* Let valgrind know the object is free. */
9327 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, sizeof (*p)));
9329 /* Let valgrind know the next portion of the object is available,
9330 but uninitialized. */
9331 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9334 /* Bring an object that underwent at least one lifecycle after anew
9335 and before the most recent free and poison, back to a usable
9336 state, reinitializing whatever is needed for it to be
9337 functionally equivalent to an object just allocated and returned
9338 by anew. This may poison or clear the next field, used by
9339 freelist housekeeping after poison was called. */
9340 static void reinit (T *obj) {
9341 T **q ATTRIBUTE_UNUSED = &next (obj);
9343 #ifdef ENABLE_GC_CHECKING
9344 memset (q, 0xa5, sizeof (*q));
9345 #endif
9346 /* Let valgrind know the entire object is available, but
9347 uninitialized. */
9348 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (*obj)));
9351 /* Reference a GTY-deletable pointer that points to the first object
9352 in the free list proper. */
9353 T *&head;
9354 public:
9355 /* Construct a freelist object chaining objects off of HEAD. */
9356 freelist (T *&head) : head(head) {}
9358 /* Add OBJ to the free object list. The former head becomes OBJ's
9359 successor. */
9360 void free (T *obj)
9362 poison (obj);
9363 next (obj) = head;
9364 head = obj;
9367 /* Take an object from the free list, if one is available, or
9368 allocate a new one. Objects taken from the free list should be
9369 regarded as filled with garbage, except for bits that are
9370 configured to be preserved across free and alloc. */
9371 T *alloc ()
9373 if (head)
9375 T *obj = head;
9376 head = next (head);
9377 reinit (obj);
9378 return obj;
9380 else
9381 return anew ();
9385 /* Explicitly specialize the interfaces for freelist<tree_node>: we
9386 want to allocate a TREE_LIST using the usual interface, and ensure
9387 TREE_CHAIN remains functional. Alas, we have to duplicate a bit of
9388 build_tree_list logic in reinit, so this could go out of sync. */
9389 template <>
9390 inline tree &
9391 freelist<tree_node>::next (tree obj)
9393 return TREE_CHAIN (obj);
9395 template <>
9396 inline tree
9397 freelist<tree_node>::anew ()
9399 return build_tree_list (NULL, NULL);
9401 template <>
9402 inline void
9403 freelist<tree_node>::poison (tree obj ATTRIBUTE_UNUSED)
9405 int size ATTRIBUTE_UNUSED = sizeof (tree_list);
9406 tree p ATTRIBUTE_UNUSED = obj;
9407 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9408 tree *q ATTRIBUTE_UNUSED = &next (obj);
9410 #ifdef ENABLE_GC_CHECKING
9411 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9413 /* Poison the data, to indicate the data is garbage. */
9414 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (p, size));
9415 memset (p, 0xa5, size);
9416 #endif
9417 /* Let valgrind know the object is free. */
9418 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_NOACCESS (p, size));
9419 /* But we still want to use the TREE_CODE and TREE_CHAIN parts. */
9420 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9421 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (q, sizeof (*q)));
9423 #ifdef ENABLE_GC_CHECKING
9424 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (b, sizeof (*b)));
9425 /* Keep TREE_CHAIN functional. */
9426 TREE_SET_CODE (obj, TREE_LIST);
9427 #else
9428 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9429 #endif
9431 template <>
9432 inline void
9433 freelist<tree_node>::reinit (tree obj ATTRIBUTE_UNUSED)
9435 tree_base *b ATTRIBUTE_UNUSED = &obj->base;
9437 #ifdef ENABLE_GC_CHECKING
9438 gcc_checking_assert (TREE_CODE (obj) == TREE_LIST);
9439 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9440 memset (obj, 0, sizeof (tree_list));
9441 #endif
9443 /* Let valgrind know the entire object is available, but
9444 uninitialized. */
9445 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_UNDEFINED (obj, sizeof (tree_list)));
9447 #ifdef ENABLE_GC_CHECKING
9448 TREE_SET_CODE (obj, TREE_LIST);
9449 #else
9450 VALGRIND_DISCARD (VALGRIND_MAKE_MEM_DEFINED (b, sizeof (*b)));
9451 #endif
9454 /* Point to the first object in the TREE_LIST freelist. */
9455 static GTY((deletable)) tree tree_list_freelist_head;
9456 /* Return the/an actual TREE_LIST freelist. */
9457 static inline freelist<tree_node>
9458 tree_list_freelist ()
9460 return tree_list_freelist_head;
9463 /* Point to the first object in the tinst_level freelist. */
9464 static GTY((deletable)) tinst_level *tinst_level_freelist_head;
9465 /* Return the/an actual tinst_level freelist. */
9466 static inline freelist<tinst_level>
9467 tinst_level_freelist ()
9469 return tinst_level_freelist_head;
9472 /* Point to the first object in the pending_template freelist. */
9473 static GTY((deletable)) pending_template *pending_template_freelist_head;
9474 /* Return the/an actual pending_template freelist. */
9475 static inline freelist<pending_template>
9476 pending_template_freelist ()
9478 return pending_template_freelist_head;
9481 /* Build the TREE_LIST object out of a split list, store it
9482 permanently, and return it. */
9483 tree
9484 tinst_level::to_list ()
9486 gcc_assert (split_list_p ());
9487 tree ret = tree_list_freelist ().alloc ();
9488 TREE_PURPOSE (ret) = tldcl;
9489 TREE_VALUE (ret) = targs;
9490 tldcl = ret;
9491 targs = NULL;
9492 gcc_assert (tree_list_p ());
9493 return ret;
9496 const unsigned short tinst_level::refcount_infinity;
9498 /* Increment OBJ's refcount unless it is already infinite. */
9499 static tinst_level *
9500 inc_refcount_use (tinst_level *obj)
9502 if (obj && obj->refcount != tinst_level::refcount_infinity)
9503 ++obj->refcount;
9504 return obj;
9507 /* Release storage for OBJ and node, if it's a TREE_LIST. */
9508 void
9509 tinst_level::free (tinst_level *obj)
9511 if (obj->tree_list_p ())
9512 tree_list_freelist ().free (obj->get_node ());
9513 tinst_level_freelist ().free (obj);
9516 /* Decrement OBJ's refcount if not infinite. If it reaches zero, release
9517 OBJ's DECL and OBJ, and start over with the tinst_level object that
9518 used to be referenced by OBJ's NEXT. */
9519 static void
9520 dec_refcount_use (tinst_level *obj)
9522 while (obj
9523 && obj->refcount != tinst_level::refcount_infinity
9524 && !--obj->refcount)
9526 tinst_level *next = obj->next;
9527 tinst_level::free (obj);
9528 obj = next;
9532 /* Modify PTR so that it points to OBJ, adjusting the refcounts of OBJ
9533 and of the former PTR. Omitting the second argument is equivalent
9534 to passing (T*)NULL; this is allowed because passing the
9535 zero-valued integral constant NULL confuses type deduction and/or
9536 overload resolution. */
9537 template <typename T>
9538 static void
9539 set_refcount_ptr (T *& ptr, T *obj = NULL)
9541 T *save = ptr;
9542 ptr = inc_refcount_use (obj);
9543 dec_refcount_use (save);
9546 static void
9547 add_pending_template (tree d)
9549 tree ti = (TYPE_P (d)
9550 ? CLASSTYPE_TEMPLATE_INFO (d)
9551 : DECL_TEMPLATE_INFO (d));
9552 struct pending_template *pt;
9553 int level;
9555 if (TI_PENDING_TEMPLATE_FLAG (ti))
9556 return;
9558 /* We are called both from instantiate_decl, where we've already had a
9559 tinst_level pushed, and instantiate_template, where we haven't.
9560 Compensate. */
9561 gcc_assert (TREE_CODE (d) != TREE_LIST);
9562 level = !current_tinst_level
9563 || current_tinst_level->maybe_get_node () != d;
9565 if (level)
9566 push_tinst_level (d);
9568 pt = pending_template_freelist ().alloc ();
9569 pt->next = NULL;
9570 pt->tinst = NULL;
9571 set_refcount_ptr (pt->tinst, current_tinst_level);
9572 if (last_pending_template)
9573 last_pending_template->next = pt;
9574 else
9575 pending_templates = pt;
9577 last_pending_template = pt;
9579 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
9581 if (level)
9582 pop_tinst_level ();
9586 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
9587 ARGLIST. Valid choices for FNS are given in the cp-tree.def
9588 documentation for TEMPLATE_ID_EXPR. */
9590 tree
9591 lookup_template_function (tree fns, tree arglist)
9593 if (fns == error_mark_node || arglist == error_mark_node)
9594 return error_mark_node;
9596 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
9598 if (!is_overloaded_fn (fns) && !identifier_p (fns))
9600 error ("%q#D is not a function template", fns);
9601 return error_mark_node;
9604 if (BASELINK_P (fns))
9606 fns = copy_node (fns);
9607 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
9608 unknown_type_node,
9609 BASELINK_FUNCTIONS (fns),
9610 arglist);
9611 return fns;
9614 return build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, arglist);
9617 /* Within the scope of a template class S<T>, the name S gets bound
9618 (in build_self_reference) to a TYPE_DECL for the class, not a
9619 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
9620 or one of its enclosing classes, and that type is a template,
9621 return the associated TEMPLATE_DECL. Otherwise, the original
9622 DECL is returned.
9624 Also handle the case when DECL is a TREE_LIST of ambiguous
9625 injected-class-names from different bases. */
9627 tree
9628 maybe_get_template_decl_from_type_decl (tree decl)
9630 if (decl == NULL_TREE)
9631 return decl;
9633 /* DR 176: A lookup that finds an injected-class-name (10.2
9634 [class.member.lookup]) can result in an ambiguity in certain cases
9635 (for example, if it is found in more than one base class). If all of
9636 the injected-class-names that are found refer to specializations of
9637 the same class template, and if the name is followed by a
9638 template-argument-list, the reference refers to the class template
9639 itself and not a specialization thereof, and is not ambiguous. */
9640 if (TREE_CODE (decl) == TREE_LIST)
9642 tree t, tmpl = NULL_TREE;
9643 for (t = decl; t; t = TREE_CHAIN (t))
9645 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
9646 if (!tmpl)
9647 tmpl = elt;
9648 else if (tmpl != elt)
9649 break;
9651 if (tmpl && t == NULL_TREE)
9652 return tmpl;
9653 else
9654 return decl;
9657 return (decl != NULL_TREE
9658 && DECL_SELF_REFERENCE_P (decl)
9659 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
9660 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
9663 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
9664 parameters, find the desired type.
9666 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
9668 IN_DECL, if non-NULL, is the template declaration we are trying to
9669 instantiate.
9671 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
9672 the class we are looking up.
9674 Issue error and warning messages under control of COMPLAIN.
9676 If the template class is really a local class in a template
9677 function, then the FUNCTION_CONTEXT is the function in which it is
9678 being instantiated.
9680 ??? Note that this function is currently called *twice* for each
9681 template-id: the first time from the parser, while creating the
9682 incomplete type (finish_template_type), and the second type during the
9683 real instantiation (instantiate_template_class). This is surely something
9684 that we want to avoid. It also causes some problems with argument
9685 coercion (see convert_nontype_argument for more information on this). */
9687 static tree
9688 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
9689 int entering_scope, tsubst_flags_t complain)
9691 tree templ = NULL_TREE, parmlist;
9692 tree t;
9693 spec_entry **slot;
9694 spec_entry *entry;
9695 spec_entry elt;
9696 hashval_t hash;
9698 if (identifier_p (d1))
9700 tree value = innermost_non_namespace_value (d1);
9701 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
9702 templ = value;
9703 else
9705 if (context)
9706 push_decl_namespace (context);
9707 templ = lookup_name (d1);
9708 templ = maybe_get_template_decl_from_type_decl (templ);
9709 if (context)
9710 pop_decl_namespace ();
9712 if (templ)
9713 context = DECL_CONTEXT (templ);
9715 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
9717 tree type = TREE_TYPE (d1);
9719 /* If we are declaring a constructor, say A<T>::A<T>, we will get
9720 an implicit typename for the second A. Deal with it. */
9721 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
9722 type = TREE_TYPE (type);
9724 if (CLASSTYPE_TEMPLATE_INFO (type))
9726 templ = CLASSTYPE_TI_TEMPLATE (type);
9727 d1 = DECL_NAME (templ);
9730 else if (TREE_CODE (d1) == ENUMERAL_TYPE
9731 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
9733 templ = TYPE_TI_TEMPLATE (d1);
9734 d1 = DECL_NAME (templ);
9736 else if (DECL_TYPE_TEMPLATE_P (d1))
9738 templ = d1;
9739 d1 = DECL_NAME (templ);
9740 context = DECL_CONTEXT (templ);
9742 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
9744 templ = d1;
9745 d1 = DECL_NAME (templ);
9748 /* Issue an error message if we didn't find a template. */
9749 if (! templ)
9751 if (complain & tf_error)
9752 error ("%qT is not a template", d1);
9753 return error_mark_node;
9756 if (TREE_CODE (templ) != TEMPLATE_DECL
9757 /* Make sure it's a user visible template, if it was named by
9758 the user. */
9759 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
9760 && !PRIMARY_TEMPLATE_P (templ)))
9762 if (complain & tf_error)
9764 error ("non-template type %qT used as a template", d1);
9765 if (in_decl)
9766 error ("for template declaration %q+D", in_decl);
9768 return error_mark_node;
9771 complain &= ~tf_user;
9773 /* An alias that just changes the name of a template is equivalent to the
9774 other template, so if any of the arguments are pack expansions, strip
9775 the alias to avoid problems with a pack expansion passed to a non-pack
9776 alias template parameter (DR 1430). */
9777 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
9778 templ = get_underlying_template (templ);
9780 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
9782 tree parm;
9783 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
9784 if (arglist2 == error_mark_node
9785 || (!uses_template_parms (arglist2)
9786 && check_instantiated_args (templ, arglist2, complain)))
9787 return error_mark_node;
9789 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
9790 return parm;
9792 else
9794 tree template_type = TREE_TYPE (templ);
9795 tree gen_tmpl;
9796 tree type_decl;
9797 tree found = NULL_TREE;
9798 int arg_depth;
9799 int parm_depth;
9800 int is_dependent_type;
9801 int use_partial_inst_tmpl = false;
9803 if (template_type == error_mark_node)
9804 /* An error occurred while building the template TEMPL, and a
9805 diagnostic has most certainly been emitted for that
9806 already. Let's propagate that error. */
9807 return error_mark_node;
9809 gen_tmpl = most_general_template (templ);
9810 if (modules_p ())
9811 lazy_load_pendings (gen_tmpl);
9813 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
9814 parm_depth = TMPL_PARMS_DEPTH (parmlist);
9815 arg_depth = TMPL_ARGS_DEPTH (arglist);
9817 if (arg_depth == 1 && parm_depth > 1)
9819 /* We've been given an incomplete set of template arguments.
9820 For example, given:
9822 template <class T> struct S1 {
9823 template <class U> struct S2 {};
9824 template <class U> struct S2<U*> {};
9827 we will be called with an ARGLIST of `U*', but the
9828 TEMPLATE will be `template <class T> template
9829 <class U> struct S1<T>::S2'. We must fill in the missing
9830 arguments. */
9831 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
9832 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
9833 arg_depth = TMPL_ARGS_DEPTH (arglist);
9836 /* Now we should have enough arguments. */
9837 gcc_assert (parm_depth == arg_depth);
9839 /* From here on, we're only interested in the most general
9840 template. */
9842 /* Shortcut looking up the current class scope again. */
9843 if (current_class_type)
9844 if (tree ti = CLASSTYPE_TEMPLATE_INFO (current_class_type))
9845 if (gen_tmpl == most_general_template (TI_TEMPLATE (ti))
9846 && comp_template_args (arglist, TI_ARGS (ti)))
9847 return current_class_type;
9849 /* Calculate the BOUND_ARGS. These will be the args that are
9850 actually tsubst'd into the definition to create the
9851 instantiation. */
9852 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
9853 complain,
9854 /*require_all_args=*/true,
9855 /*use_default_args=*/true);
9857 if (arglist == error_mark_node)
9858 /* We were unable to bind the arguments. */
9859 return error_mark_node;
9861 /* In the scope of a template class, explicit references to the
9862 template class refer to the type of the template, not any
9863 instantiation of it. For example, in:
9865 template <class T> class C { void f(C<T>); }
9867 the `C<T>' is just the same as `C'. Outside of the
9868 class, however, such a reference is an instantiation. */
9869 if (entering_scope
9870 || !PRIMARY_TEMPLATE_P (gen_tmpl)
9871 || currently_open_class (template_type))
9873 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
9875 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
9876 return template_type;
9879 /* If we already have this specialization, return it. */
9880 elt.tmpl = gen_tmpl;
9881 elt.args = arglist;
9882 elt.spec = NULL_TREE;
9883 hash = spec_hasher::hash (&elt);
9884 entry = type_specializations->find_with_hash (&elt, hash);
9886 if (entry)
9887 return entry->spec;
9889 /* If the template's constraints are not satisfied,
9890 then we cannot form a valid type.
9892 Note that the check is deferred until after the hash
9893 lookup. This prevents redundant checks on previously
9894 instantiated specializations. */
9895 if (flag_concepts
9896 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl)
9897 && !constraints_satisfied_p (gen_tmpl, arglist))
9899 if (complain & tf_error)
9901 auto_diagnostic_group d;
9902 error ("template constraint failure for %qD", gen_tmpl);
9903 diagnose_constraints (input_location, gen_tmpl, arglist);
9905 return error_mark_node;
9908 is_dependent_type = uses_template_parms (arglist);
9910 /* If the deduced arguments are invalid, then the binding
9911 failed. */
9912 if (!is_dependent_type
9913 && check_instantiated_args (gen_tmpl,
9914 INNERMOST_TEMPLATE_ARGS (arglist),
9915 complain))
9916 return error_mark_node;
9918 if (!is_dependent_type
9919 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9920 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
9921 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
9922 /* This occurs when the user has tried to define a tagged type
9923 in a scope that forbids it. We emitted an error during the
9924 parse. We didn't complete the bail out then, so here we
9925 are. */
9926 return error_mark_node;
9928 context = DECL_CONTEXT (gen_tmpl);
9929 if (context && TYPE_P (context))
9931 context = tsubst_aggr_type (context, arglist, complain, in_decl, true);
9932 context = complete_type (context);
9934 else
9935 context = tsubst (context, arglist, complain, in_decl);
9937 if (context == error_mark_node)
9938 return error_mark_node;
9940 if (!context)
9941 context = global_namespace;
9943 /* Create the type. */
9944 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9946 /* The user referred to a specialization of an alias
9947 template represented by GEN_TMPL.
9949 [temp.alias]/2 says:
9951 When a template-id refers to the specialization of an
9952 alias template, it is equivalent to the associated
9953 type obtained by substitution of its
9954 template-arguments for the template-parameters in the
9955 type-id of the alias template. */
9957 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
9958 /* Note that the call above (by indirectly calling
9959 register_specialization in tsubst_decl) registers the
9960 TYPE_DECL representing the specialization of the alias
9961 template. So next time someone substitutes ARGLIST for
9962 the template parms into the alias template (GEN_TMPL),
9963 she'll get that TYPE_DECL back. */
9965 if (t == error_mark_node)
9966 return t;
9968 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
9970 if (!is_dependent_type)
9972 set_current_access_from_decl (TYPE_NAME (template_type));
9973 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
9974 tsubst (ENUM_UNDERLYING_TYPE (template_type),
9975 arglist, complain, in_decl),
9976 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
9977 arglist, complain, in_decl),
9978 SCOPED_ENUM_P (template_type), NULL);
9980 if (t == error_mark_node)
9981 return t;
9983 else
9985 /* We don't want to call start_enum for this type, since
9986 the values for the enumeration constants may involve
9987 template parameters. And, no one should be interested
9988 in the enumeration constants for such a type. */
9989 t = cxx_make_type (ENUMERAL_TYPE);
9990 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
9992 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
9993 ENUM_FIXED_UNDERLYING_TYPE_P (t)
9994 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
9996 else if (CLASS_TYPE_P (template_type))
9998 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9999 instantiated here. */
10000 gcc_assert (!LAMBDA_TYPE_P (template_type));
10002 t = make_class_type (TREE_CODE (template_type));
10003 CLASSTYPE_DECLARED_CLASS (t)
10004 = CLASSTYPE_DECLARED_CLASS (template_type);
10005 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
10007 /* A local class. Make sure the decl gets registered properly. */
10008 if (context == current_function_decl)
10009 if (pushtag (DECL_NAME (gen_tmpl), t)
10010 == error_mark_node)
10011 return error_mark_node;
10013 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
10014 /* This instantiation is another name for the primary
10015 template type. Set the TYPE_CANONICAL field
10016 appropriately. */
10017 TYPE_CANONICAL (t) = template_type;
10018 else if (any_template_arguments_need_structural_equality_p (arglist))
10019 /* Some of the template arguments require structural
10020 equality testing, so this template class requires
10021 structural equality testing. */
10022 SET_TYPE_STRUCTURAL_EQUALITY (t);
10024 else
10025 gcc_unreachable ();
10027 /* If we called start_enum or pushtag above, this information
10028 will already be set up. */
10029 type_decl = TYPE_NAME (t);
10030 if (!type_decl)
10032 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
10034 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
10035 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
10036 DECL_SOURCE_LOCATION (type_decl)
10037 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
10040 set_instantiating_module (type_decl);
10041 /* Although GEN_TMPL is the TEMPLATE_DECL, it has the same value
10042 of export flag. We want to propagate this because it might
10043 be a friend declaration that pushes a new hidden binding. */
10044 DECL_MODULE_EXPORT_P (type_decl) = DECL_MODULE_EXPORT_P (gen_tmpl);
10046 if (CLASS_TYPE_P (template_type))
10048 TREE_PRIVATE (type_decl)
10049 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
10050 TREE_PROTECTED (type_decl)
10051 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
10052 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
10054 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
10055 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
10059 if (OVERLOAD_TYPE_P (t)
10060 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
10062 static const char *tags[] = {"abi_tag", "may_alias"};
10064 for (unsigned ix = 0; ix != 2; ix++)
10066 tree attributes
10067 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
10069 if (attributes)
10070 TYPE_ATTRIBUTES (t)
10071 = tree_cons (TREE_PURPOSE (attributes),
10072 TREE_VALUE (attributes),
10073 TYPE_ATTRIBUTES (t));
10077 /* Let's consider the explicit specialization of a member
10078 of a class template specialization that is implicitly instantiated,
10079 e.g.:
10080 template<class T>
10081 struct S
10083 template<class U> struct M {}; //#0
10086 template<>
10087 template<>
10088 struct S<int>::M<char> //#1
10090 int i;
10092 [temp.expl.spec]/4 says this is valid.
10094 In this case, when we write:
10095 S<int>::M<char> m;
10097 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
10098 the one of #0.
10100 When we encounter #1, we want to store the partial instantiation
10101 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
10103 For all cases other than this "explicit specialization of member of a
10104 class template", we just want to store the most general template into
10105 the CLASSTYPE_TI_TEMPLATE of M.
10107 This case of "explicit specialization of member of a class template"
10108 only happens when:
10109 1/ the enclosing class is an instantiation of, and therefore not
10110 the same as, the context of the most general template, and
10111 2/ we aren't looking at the partial instantiation itself, i.e.
10112 the innermost arguments are not the same as the innermost parms of
10113 the most general template.
10115 So it's only when 1/ and 2/ happens that we want to use the partial
10116 instantiation of the member template in lieu of its most general
10117 template. */
10119 if (PRIMARY_TEMPLATE_P (gen_tmpl)
10120 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
10121 /* the enclosing class must be an instantiation... */
10122 && CLASS_TYPE_P (context)
10123 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
10125 TREE_VEC_LENGTH (arglist)--;
10126 ++processing_template_decl;
10127 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
10128 tree partial_inst_args =
10129 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
10130 arglist, complain, NULL_TREE);
10131 --processing_template_decl;
10132 TREE_VEC_LENGTH (arglist)++;
10133 if (partial_inst_args == error_mark_node)
10134 return error_mark_node;
10135 use_partial_inst_tmpl =
10136 /*...and we must not be looking at the partial instantiation
10137 itself. */
10138 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
10139 partial_inst_args);
10142 if (!use_partial_inst_tmpl)
10143 /* This case is easy; there are no member templates involved. */
10144 found = gen_tmpl;
10145 else
10147 /* This is a full instantiation of a member template. Find
10148 the partial instantiation of which this is an instance. */
10150 /* Temporarily reduce by one the number of levels in the ARGLIST
10151 so as to avoid comparing the last set of arguments. */
10152 TREE_VEC_LENGTH (arglist)--;
10153 /* We don't use COMPLAIN in the following call because this isn't
10154 the immediate context of deduction. For instance, tf_partial
10155 could be set here as we might be at the beginning of template
10156 argument deduction when any explicitly specified template
10157 arguments are substituted into the function type. tf_partial
10158 could lead into trouble because we wouldn't find the partial
10159 instantiation that might have been created outside tf_partial
10160 context, because the levels of template parameters wouldn't
10161 match, because in a tf_partial context, tsubst doesn't reduce
10162 TEMPLATE_PARM_LEVEL. */
10163 found = tsubst (gen_tmpl, arglist, tf_none, NULL_TREE);
10164 TREE_VEC_LENGTH (arglist)++;
10165 /* FOUND is either a proper class type, or an alias
10166 template specialization. In the later case, it's a
10167 TYPE_DECL, resulting from the substituting of arguments
10168 for parameters in the TYPE_DECL of the alias template
10169 done earlier. So be careful while getting the template
10170 of FOUND. */
10171 found = (TREE_CODE (found) == TEMPLATE_DECL
10172 ? found
10173 : (TREE_CODE (found) == TYPE_DECL
10174 ? DECL_TI_TEMPLATE (found)
10175 : CLASSTYPE_TI_TEMPLATE (found)));
10177 if (DECL_CLASS_TEMPLATE_P (found)
10178 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (found)))
10180 /* If this partial instantiation is specialized, we want to
10181 use it for hash table lookup. */
10182 elt.tmpl = found;
10183 elt.args = arglist = INNERMOST_TEMPLATE_ARGS (arglist);
10184 hash = spec_hasher::hash (&elt);
10188 /* Build template info for the new specialization. */
10189 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
10191 elt.spec = t;
10192 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
10193 gcc_checking_assert (*slot == NULL);
10194 entry = ggc_alloc<spec_entry> ();
10195 *entry = elt;
10196 *slot = entry;
10198 /* Note this use of the partial instantiation so we can check it
10199 later in maybe_process_partial_specialization. */
10200 DECL_TEMPLATE_INSTANTIATIONS (found)
10201 = tree_cons (arglist, t,
10202 DECL_TEMPLATE_INSTANTIATIONS (found));
10204 if (TREE_CODE (template_type) == ENUMERAL_TYPE
10205 && !uses_template_parms (current_nonlambda_scope ())
10206 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
10207 /* Now that the type has been registered on the instantiations
10208 list, we set up the enumerators. Because the enumeration
10209 constants may involve the enumeration type itself, we make
10210 sure to register the type first, and then create the
10211 constants. That way, doing tsubst_expr for the enumeration
10212 constants won't result in recursive calls here; we'll find
10213 the instantiation and exit above. */
10214 tsubst_enum (template_type, t, arglist);
10216 if (CLASS_TYPE_P (template_type) && is_dependent_type)
10217 /* If the type makes use of template parameters, the
10218 code that generates debugging information will crash. */
10219 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
10221 /* Possibly limit visibility based on template args. */
10222 TREE_PUBLIC (type_decl) = 1;
10223 determine_visibility (type_decl);
10225 inherit_targ_abi_tags (t);
10227 return t;
10231 /* Wrapper for lookup_template_class_1. */
10233 tree
10234 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
10235 int entering_scope, tsubst_flags_t complain)
10237 tree ret;
10238 timevar_push (TV_TEMPLATE_INST);
10239 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
10240 entering_scope, complain);
10241 timevar_pop (TV_TEMPLATE_INST);
10242 return ret;
10245 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
10247 tree
10248 lookup_template_variable (tree templ, tree arglist)
10250 if (flag_concepts && variable_concept_p (templ))
10251 return build_concept_check (templ, arglist, tf_none);
10253 /* The type of the expression is NULL_TREE since the template-id could refer
10254 to an explicit or partial specialization. */
10255 return build2 (TEMPLATE_ID_EXPR, NULL_TREE, templ, arglist);
10258 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
10260 tree
10261 finish_template_variable (tree var, tsubst_flags_t complain)
10263 tree templ = TREE_OPERAND (var, 0);
10264 tree arglist = TREE_OPERAND (var, 1);
10266 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
10267 arglist = add_outermost_template_args (tmpl_args, arglist);
10269 templ = most_general_template (templ);
10270 tree parms = DECL_TEMPLATE_PARMS (templ);
10271 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
10272 /*req_all*/true,
10273 /*use_default*/true);
10274 if (arglist == error_mark_node)
10275 return error_mark_node;
10277 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
10279 if (complain & tf_error)
10281 auto_diagnostic_group d;
10282 error ("use of invalid variable template %qE", var);
10283 diagnose_constraints (location_of (var), templ, arglist);
10285 return error_mark_node;
10288 return instantiate_template (templ, arglist, complain);
10291 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
10292 TARGS template args, and instantiate it if it's not dependent. */
10294 tree
10295 lookup_and_finish_template_variable (tree templ, tree targs,
10296 tsubst_flags_t complain)
10298 templ = lookup_template_variable (templ, targs);
10299 if (!any_dependent_template_arguments_p (targs))
10301 templ = finish_template_variable (templ, complain);
10302 mark_used (templ);
10305 return convert_from_reference (templ);
10308 /* If the set of template parameters PARMS contains a template parameter
10309 at the given LEVEL and INDEX, then return this parameter. Otherwise
10310 return NULL_TREE. */
10312 static tree
10313 corresponding_template_parameter (tree parms, int level, int index)
10315 while (TMPL_PARMS_DEPTH (parms) > level)
10316 parms = TREE_CHAIN (parms);
10318 if (TMPL_PARMS_DEPTH (parms) != level
10319 || TREE_VEC_LENGTH (TREE_VALUE (parms)) <= index)
10320 return NULL_TREE;
10322 tree t = TREE_VALUE (TREE_VEC_ELT (TREE_VALUE (parms), index));
10323 /* As in template_parm_to_arg. */
10324 if (TREE_CODE (t) == TYPE_DECL || TREE_CODE (t) == TEMPLATE_DECL)
10325 t = TREE_TYPE (t);
10326 else
10327 t = DECL_INITIAL (t);
10329 gcc_assert (TEMPLATE_PARM_P (t));
10330 return t;
10333 /* Return the template parameter from PARMS that positionally corresponds
10334 to the template parameter PARM, or else return NULL_TREE. */
10336 static tree
10337 corresponding_template_parameter (tree parms, tree parm)
10339 int level, index;
10340 template_parm_level_and_index (parm, &level, &index);
10341 return corresponding_template_parameter (parms, level, index);
10345 struct pair_fn_data
10347 tree_fn_t fn;
10348 tree_fn_t any_fn;
10349 void *data;
10350 /* True when we should also visit template parameters that occur in
10351 non-deduced contexts. */
10352 bool include_nondeduced_p;
10353 hash_set<tree> *visited;
10356 /* Called from for_each_template_parm via walk_tree. */
10358 static tree
10359 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
10361 tree t = *tp;
10362 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
10363 tree_fn_t fn = pfd->fn;
10364 void *data = pfd->data;
10365 tree result = NULL_TREE;
10367 #define WALK_SUBTREE(NODE) \
10368 do \
10370 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
10371 pfd->include_nondeduced_p, \
10372 pfd->any_fn); \
10373 if (result) goto out; \
10375 while (0)
10377 if (pfd->any_fn && (*pfd->any_fn)(t, data))
10378 return t;
10380 if (TYPE_P (t)
10381 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
10382 WALK_SUBTREE (TYPE_CONTEXT (t));
10384 switch (TREE_CODE (t))
10386 case RECORD_TYPE:
10387 if (TYPE_PTRMEMFUNC_P (t))
10388 break;
10389 /* Fall through. */
10391 case UNION_TYPE:
10392 case ENUMERAL_TYPE:
10393 if (!TYPE_TEMPLATE_INFO (t))
10394 *walk_subtrees = 0;
10395 else
10396 WALK_SUBTREE (TYPE_TI_ARGS (t));
10397 break;
10399 case INTEGER_TYPE:
10400 WALK_SUBTREE (TYPE_MIN_VALUE (t));
10401 WALK_SUBTREE (TYPE_MAX_VALUE (t));
10402 break;
10404 case METHOD_TYPE:
10405 /* Since we're not going to walk subtrees, we have to do this
10406 explicitly here. */
10407 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
10408 /* Fall through. */
10410 case FUNCTION_TYPE:
10411 /* Check the return type. */
10412 WALK_SUBTREE (TREE_TYPE (t));
10414 /* Check the parameter types. Since default arguments are not
10415 instantiated until they are needed, the TYPE_ARG_TYPES may
10416 contain expressions that involve template parameters. But,
10417 no-one should be looking at them yet. And, once they're
10418 instantiated, they don't contain template parameters, so
10419 there's no point in looking at them then, either. */
10421 tree parm;
10423 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
10424 WALK_SUBTREE (TREE_VALUE (parm));
10426 /* Since we've already handled the TYPE_ARG_TYPES, we don't
10427 want walk_tree walking into them itself. */
10428 *walk_subtrees = 0;
10431 if (flag_noexcept_type)
10433 tree spec = TYPE_RAISES_EXCEPTIONS (t);
10434 if (spec)
10435 WALK_SUBTREE (TREE_PURPOSE (spec));
10437 break;
10439 case TYPEOF_TYPE:
10440 case DECLTYPE_TYPE:
10441 case UNDERLYING_TYPE:
10442 if (pfd->include_nondeduced_p
10443 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
10444 pfd->visited,
10445 pfd->include_nondeduced_p,
10446 pfd->any_fn))
10447 return error_mark_node;
10448 *walk_subtrees = false;
10449 break;
10451 case FUNCTION_DECL:
10452 case VAR_DECL:
10453 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
10454 WALK_SUBTREE (DECL_TI_ARGS (t));
10455 /* Fall through. */
10457 case PARM_DECL:
10458 case CONST_DECL:
10459 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
10460 WALK_SUBTREE (DECL_INITIAL (t));
10461 if (DECL_CONTEXT (t)
10462 && pfd->include_nondeduced_p)
10463 WALK_SUBTREE (DECL_CONTEXT (t));
10464 break;
10466 case BOUND_TEMPLATE_TEMPLATE_PARM:
10467 /* Record template parameters such as `T' inside `TT<T>'. */
10468 WALK_SUBTREE (TYPE_TI_ARGS (t));
10469 /* Fall through. */
10471 case TEMPLATE_TEMPLATE_PARM:
10472 case TEMPLATE_TYPE_PARM:
10473 case TEMPLATE_PARM_INDEX:
10474 if (fn && (*fn)(t, data))
10475 return t;
10476 else if (!fn)
10477 return t;
10478 break;
10480 case TEMPLATE_DECL:
10481 /* A template template parameter is encountered. */
10482 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10483 WALK_SUBTREE (TREE_TYPE (t));
10485 /* Already substituted template template parameter */
10486 *walk_subtrees = 0;
10487 break;
10489 case TYPENAME_TYPE:
10490 /* A template-id in a TYPENAME_TYPE might be a deduced context after
10491 partial instantiation. */
10492 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
10493 *walk_subtrees = 0;
10494 break;
10496 case CONSTRUCTOR:
10497 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
10498 && pfd->include_nondeduced_p)
10499 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
10500 break;
10502 case INDIRECT_REF:
10503 case COMPONENT_REF:
10504 /* If there's no type, then this thing must be some expression
10505 involving template parameters. */
10506 if (!fn && !TREE_TYPE (t))
10507 return error_mark_node;
10508 break;
10510 case TRAIT_EXPR:
10511 case PLUS_EXPR:
10512 case MULT_EXPR:
10513 case SCOPE_REF:
10514 /* These are non-deduced contexts. */
10515 if (!pfd->include_nondeduced_p)
10516 *walk_subtrees = 0;
10517 break;
10519 case MODOP_EXPR:
10520 case CAST_EXPR:
10521 case IMPLICIT_CONV_EXPR:
10522 case REINTERPRET_CAST_EXPR:
10523 case CONST_CAST_EXPR:
10524 case STATIC_CAST_EXPR:
10525 case DYNAMIC_CAST_EXPR:
10526 case ARROW_EXPR:
10527 case DOTSTAR_EXPR:
10528 case TYPEID_EXPR:
10529 case PSEUDO_DTOR_EXPR:
10530 if (!fn)
10531 return error_mark_node;
10532 break;
10534 case REQUIRES_EXPR:
10536 if (!fn)
10537 return error_mark_node;
10539 /* Recursively walk the type of each constraint variable. */
10540 tree p = TREE_OPERAND (t, 0);
10541 while (p)
10543 WALK_SUBTREE (TREE_TYPE (p));
10544 p = TREE_CHAIN (p);
10547 break;
10549 default:
10550 break;
10553 #undef WALK_SUBTREE
10555 /* We didn't find any template parameters we liked. */
10556 out:
10557 return result;
10560 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
10561 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
10562 call FN with the parameter and the DATA.
10563 If FN returns nonzero, the iteration is terminated, and
10564 for_each_template_parm returns 1. Otherwise, the iteration
10565 continues. If FN never returns a nonzero value, the value
10566 returned by for_each_template_parm is 0. If FN is NULL, it is
10567 considered to be the function which always returns 1.
10569 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
10570 parameters that occur in non-deduced contexts. When false, only
10571 visits those template parameters that can be deduced. */
10573 static tree
10574 for_each_template_parm (tree t, tree_fn_t fn, void* data,
10575 hash_set<tree> *visited,
10576 bool include_nondeduced_p,
10577 tree_fn_t any_fn)
10579 struct pair_fn_data pfd;
10580 tree result;
10582 /* Set up. */
10583 pfd.fn = fn;
10584 pfd.any_fn = any_fn;
10585 pfd.data = data;
10586 pfd.include_nondeduced_p = include_nondeduced_p;
10588 /* Walk the tree. (Conceptually, we would like to walk without
10589 duplicates, but for_each_template_parm_r recursively calls
10590 for_each_template_parm, so we would need to reorganize a fair
10591 bit to use walk_tree_without_duplicates, so we keep our own
10592 visited list.) */
10593 if (visited)
10594 pfd.visited = visited;
10595 else
10596 pfd.visited = new hash_set<tree>;
10597 result = cp_walk_tree (&t,
10598 for_each_template_parm_r,
10599 &pfd,
10600 pfd.visited);
10602 /* Clean up. */
10603 if (!visited)
10605 delete pfd.visited;
10606 pfd.visited = 0;
10609 return result;
10612 struct find_template_parameter_info
10614 explicit find_template_parameter_info (tree ctx_parms)
10615 : parm_list (NULL_TREE),
10616 ctx_parms (ctx_parms),
10617 max_depth (TMPL_PARMS_DEPTH (ctx_parms))
10620 hash_set<tree> visited;
10621 hash_set<tree> parms;
10622 tree parm_list;
10623 tree ctx_parms;
10624 int max_depth;
10627 /* Appends the declaration of T to the list in DATA. */
10629 static int
10630 keep_template_parm (tree t, void* data)
10632 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10634 /* Template parameters declared within the expression are not part of
10635 the parameter mapping. For example, in this concept:
10637 template<typename T>
10638 concept C = requires { <expr> } -> same_as<int>;
10640 the return specifier same_as<int> declares a new decltype parameter
10641 that must not be part of the parameter mapping. The same is true
10642 for generic lambda parameters, lambda template parameters, etc. */
10643 int level;
10644 int index;
10645 template_parm_level_and_index (t, &level, &index);
10646 if (level > ftpi->max_depth)
10647 return 0;
10649 if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
10650 /* We want the underlying TEMPLATE_TEMPLATE_PARM, not the
10651 BOUND_TEMPLATE_TEMPLATE_PARM itself. */
10652 t = TREE_TYPE (TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (t));
10654 /* This template parameter might be an argument to a cached dependent
10655 specalization that was formed earlier inside some other template, in
10656 which case the parameter is not among the ones that are in-scope.
10657 Look in CTX_PARMS to find the corresponding in-scope template
10658 parameter, and use it instead. */
10659 if (tree in_scope = corresponding_template_parameter (ftpi->ctx_parms, t))
10660 t = in_scope;
10662 /* Arguments like const T yield parameters like const T. This means that
10663 a template-id like X<T, const T> would yield two distinct parameters:
10664 T and const T. Adjust types to their unqualified versions. */
10665 if (TYPE_P (t))
10666 t = TYPE_MAIN_VARIANT (t);
10667 if (!ftpi->parms.add (t))
10668 ftpi->parm_list = tree_cons (NULL_TREE, t, ftpi->parm_list);
10670 /* Verify the parameter we found has a valid index. */
10671 if (flag_checking)
10673 tree parms = ftpi->ctx_parms;
10674 while (TMPL_PARMS_DEPTH (parms) > level)
10675 parms = TREE_CHAIN (parms);
10676 if (int len = TREE_VEC_LENGTH (TREE_VALUE (parms)))
10677 gcc_assert (index < len);
10680 return 0;
10683 /* Ensure that we recursively examine certain terms that are not normally
10684 visited in for_each_template_parm_r. */
10686 static int
10687 any_template_parm_r (tree t, void *data)
10689 find_template_parameter_info *ftpi = (find_template_parameter_info*)data;
10691 #define WALK_SUBTREE(NODE) \
10692 do \
10694 for_each_template_parm (NODE, keep_template_parm, data, \
10695 &ftpi->visited, true, \
10696 any_template_parm_r); \
10698 while (0)
10700 /* A mention of a member alias/typedef is a use of all of its template
10701 arguments, including those from the enclosing class, so we don't use
10702 alias_template_specialization_p here. */
10703 if (TYPE_P (t) && typedef_variant_p (t))
10704 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
10705 WALK_SUBTREE (TI_ARGS (tinfo));
10707 switch (TREE_CODE (t))
10709 case TEMPLATE_TYPE_PARM:
10710 /* Type constraints of a placeholder type may contain parameters. */
10711 if (is_auto (t))
10712 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
10713 WALK_SUBTREE (constr);
10714 break;
10716 case TEMPLATE_ID_EXPR:
10717 /* Search through references to variable templates. */
10718 WALK_SUBTREE (TREE_OPERAND (t, 0));
10719 WALK_SUBTREE (TREE_OPERAND (t, 1));
10720 break;
10722 case TEMPLATE_PARM_INDEX:
10723 case PARM_DECL:
10724 /* A parameter or constraint variable may also depend on a template
10725 parameter without explicitly naming it. */
10726 WALK_SUBTREE (TREE_TYPE (t));
10727 break;
10729 case TEMPLATE_DECL:
10731 /* If T is a member template that shares template parameters with
10732 ctx_parms, we need to mark all those parameters for mapping. */
10733 tree dparms = DECL_TEMPLATE_PARMS (t);
10734 tree cparms = ftpi->ctx_parms;
10735 while (TMPL_PARMS_DEPTH (dparms) > ftpi->max_depth)
10736 dparms = TREE_CHAIN (dparms);
10737 while (TMPL_PARMS_DEPTH (cparms) > TMPL_PARMS_DEPTH (dparms))
10738 cparms = TREE_CHAIN (cparms);
10739 while (dparms
10740 && (TREE_TYPE (TREE_VALUE (dparms))
10741 != TREE_TYPE (TREE_VALUE (cparms))))
10742 dparms = TREE_CHAIN (dparms),
10743 cparms = TREE_CHAIN (cparms);
10744 if (dparms)
10746 int ddepth = TMPL_PARMS_DEPTH (dparms);
10747 tree dargs = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (t)));
10748 for (int i = 0; i < ddepth; ++i)
10749 WALK_SUBTREE (TMPL_ARGS_LEVEL (dargs, i+1));
10752 break;
10754 case LAMBDA_EXPR:
10756 /* Look in the parms and body. */
10757 tree fn = lambda_function (t);
10758 WALK_SUBTREE (TREE_TYPE (fn));
10759 WALK_SUBTREE (DECL_SAVED_TREE (fn));
10761 break;
10763 case IDENTIFIER_NODE:
10764 if (IDENTIFIER_CONV_OP_P (t))
10765 /* The conversion-type-id of a conversion operator may be dependent. */
10766 WALK_SUBTREE (TREE_TYPE (t));
10767 break;
10769 default:
10770 break;
10773 /* Keep walking. */
10774 return 0;
10777 /* Returns a list of unique template parameters found within T, where CTX_PARMS
10778 are the template parameters in scope. */
10780 tree
10781 find_template_parameters (tree t, tree ctx_parms)
10783 if (!ctx_parms)
10784 return NULL_TREE;
10786 find_template_parameter_info ftpi (ctx_parms);
10787 for_each_template_parm (t, keep_template_parm, &ftpi, &ftpi.visited,
10788 /*include_nondeduced*/true, any_template_parm_r);
10789 return ftpi.parm_list;
10792 /* Returns true if T depends on any template parameter. */
10795 uses_template_parms (tree t)
10797 if (t == NULL_TREE)
10798 return false;
10800 bool dependent_p;
10801 int saved_processing_template_decl;
10803 saved_processing_template_decl = processing_template_decl;
10804 if (!saved_processing_template_decl)
10805 processing_template_decl = 1;
10806 if (TYPE_P (t))
10807 dependent_p = dependent_type_p (t);
10808 else if (TREE_CODE (t) == TREE_VEC)
10809 dependent_p = any_dependent_template_arguments_p (t);
10810 else if (TREE_CODE (t) == TREE_LIST)
10811 dependent_p = (uses_template_parms (TREE_VALUE (t))
10812 || uses_template_parms (TREE_CHAIN (t)));
10813 else if (TREE_CODE (t) == TYPE_DECL)
10814 dependent_p = dependent_type_p (TREE_TYPE (t));
10815 else if (t == error_mark_node)
10816 dependent_p = false;
10817 else
10818 dependent_p = instantiation_dependent_expression_p (t);
10820 processing_template_decl = saved_processing_template_decl;
10822 return dependent_p;
10825 /* Returns true iff we're processing an incompletely instantiated function
10826 template. Useful instead of processing_template_decl because the latter
10827 is set to 0 during instantiate_non_dependent_expr. */
10829 bool
10830 in_template_function (void)
10832 /* Inspect the less volatile cfun->decl instead of current_function_decl;
10833 the latter might get set for e.g. access checking during satisfaction. */
10834 tree fn = cfun ? cfun->decl : NULL_TREE;
10835 bool ret;
10836 ++processing_template_decl;
10837 ret = (fn && DECL_LANG_SPECIFIC (fn)
10838 && DECL_TEMPLATE_INFO (fn)
10839 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
10840 --processing_template_decl;
10841 return ret;
10844 /* Returns true if T depends on any template parameter with level LEVEL. */
10846 bool
10847 uses_template_parms_level (tree t, int level)
10849 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
10850 /*include_nondeduced_p=*/true);
10853 /* Returns true if the signature of DECL depends on any template parameter from
10854 its enclosing class. */
10856 bool
10857 uses_outer_template_parms (tree decl)
10859 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
10860 if (depth == 0)
10861 return false;
10862 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
10863 &depth, NULL, /*include_nondeduced_p=*/true))
10864 return true;
10865 if (PRIMARY_TEMPLATE_P (decl)
10866 || DECL_TEMPLATE_TEMPLATE_PARM_P (decl))
10868 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (decl));
10869 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
10871 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
10872 tree defarg = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
10873 if (TREE_CODE (parm) == PARM_DECL
10874 && for_each_template_parm (TREE_TYPE (parm),
10875 template_parm_outer_level,
10876 &depth, NULL, /*nondeduced*/true))
10877 return true;
10878 if (TREE_CODE (parm) == TEMPLATE_DECL
10879 && uses_outer_template_parms (parm))
10880 return true;
10881 if (defarg
10882 && for_each_template_parm (defarg, template_parm_outer_level,
10883 &depth, NULL, /*nondeduced*/true))
10884 return true;
10887 tree ci = get_constraints (decl);
10888 if (ci)
10889 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
10890 if (ci && for_each_template_parm (ci, template_parm_outer_level,
10891 &depth, NULL, /*nondeduced*/true))
10892 return true;
10893 return false;
10896 /* Returns TRUE iff INST is an instantiation we don't need to do in an
10897 ill-formed translation unit, i.e. a variable or function that isn't
10898 usable in a constant expression. */
10900 static inline bool
10901 neglectable_inst_p (tree d)
10903 return (d && DECL_P (d)
10904 && !undeduced_auto_decl (d)
10905 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
10906 : decl_maybe_constant_var_p (d)));
10909 /* Returns TRUE iff we should refuse to instantiate DECL because it's
10910 neglectable and instantiated from within an erroneous instantiation. */
10912 static bool
10913 limit_bad_template_recursion (tree decl)
10915 struct tinst_level *lev = current_tinst_level;
10916 int errs = errorcount + sorrycount;
10917 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
10918 return false;
10920 for (; lev; lev = lev->next)
10921 if (neglectable_inst_p (lev->maybe_get_node ()))
10922 break;
10924 return (lev && errs > lev->errors);
10927 static int tinst_depth;
10928 extern int max_tinst_depth;
10929 int depth_reached;
10931 static GTY(()) struct tinst_level *last_error_tinst_level;
10933 /* We're starting to instantiate D; record the template instantiation context
10934 at LOC for diagnostics and to restore it later. */
10936 bool
10937 push_tinst_level_loc (tree tldcl, tree targs, location_t loc)
10939 struct tinst_level *new_level;
10941 if (tinst_depth >= max_tinst_depth)
10943 /* Tell error.c not to try to instantiate any templates. */
10944 at_eof = 2;
10945 fatal_error (input_location,
10946 "template instantiation depth exceeds maximum of %d"
10947 " (use %<-ftemplate-depth=%> to increase the maximum)",
10948 max_tinst_depth);
10949 return false;
10952 /* If the current instantiation caused problems, don't let it instantiate
10953 anything else. Do allow deduction substitution and decls usable in
10954 constant expressions. */
10955 if (!targs && limit_bad_template_recursion (tldcl))
10957 /* Avoid no_linkage_errors and unused function warnings for this
10958 decl. */
10959 TREE_NO_WARNING (tldcl) = 1;
10960 return false;
10963 /* When not -quiet, dump template instantiations other than functions, since
10964 announce_function will take care of those. */
10965 if (!quiet_flag && !targs
10966 && TREE_CODE (tldcl) != TREE_LIST
10967 && TREE_CODE (tldcl) != FUNCTION_DECL)
10968 fprintf (stderr, " %s", decl_as_string (tldcl, TFF_DECL_SPECIFIERS));
10970 new_level = tinst_level_freelist ().alloc ();
10971 new_level->tldcl = tldcl;
10972 new_level->targs = targs;
10973 new_level->locus = loc;
10974 new_level->errors = errorcount + sorrycount;
10975 new_level->next = NULL;
10976 new_level->refcount = 0;
10977 new_level->path = new_level->visible = nullptr;
10978 set_refcount_ptr (new_level->next, current_tinst_level);
10979 set_refcount_ptr (current_tinst_level, new_level);
10981 ++tinst_depth;
10982 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
10983 depth_reached = tinst_depth;
10985 return true;
10988 /* We're starting substitution of TMPL<ARGS>; record the template
10989 substitution context for diagnostics and to restore it later. */
10991 bool
10992 push_tinst_level (tree tmpl, tree args)
10994 return push_tinst_level_loc (tmpl, args, input_location);
10997 /* We're starting to instantiate D; record INPUT_LOCATION and the
10998 template instantiation context for diagnostics and to restore it
10999 later. */
11001 bool
11002 push_tinst_level (tree d)
11004 return push_tinst_level_loc (d, input_location);
11007 /* Likewise, but record LOC as the program location. */
11009 bool
11010 push_tinst_level_loc (tree d, location_t loc)
11012 gcc_assert (TREE_CODE (d) != TREE_LIST);
11013 return push_tinst_level_loc (d, NULL, loc);
11016 /* We're done instantiating this template; return to the instantiation
11017 context. */
11019 void
11020 pop_tinst_level (void)
11022 /* Restore the filename and line number stashed away when we started
11023 this instantiation. */
11024 input_location = current_tinst_level->locus;
11025 set_refcount_ptr (current_tinst_level, current_tinst_level->next);
11026 --tinst_depth;
11029 /* We're instantiating a deferred template; restore the template
11030 instantiation context in which the instantiation was requested, which
11031 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
11033 static tree
11034 reopen_tinst_level (struct tinst_level *level)
11036 struct tinst_level *t;
11038 tinst_depth = 0;
11039 for (t = level; t; t = t->next)
11040 ++tinst_depth;
11042 set_refcount_ptr (current_tinst_level, level);
11043 pop_tinst_level ();
11044 if (current_tinst_level)
11045 current_tinst_level->errors = errorcount+sorrycount;
11046 return level->maybe_get_node ();
11049 /* Returns the TINST_LEVEL which gives the original instantiation
11050 context. */
11052 struct tinst_level *
11053 outermost_tinst_level (void)
11055 struct tinst_level *level = current_tinst_level;
11056 if (level)
11057 while (level->next)
11058 level = level->next;
11059 return level;
11062 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
11063 vector of template arguments, as for tsubst.
11065 Returns an appropriate tsubst'd friend declaration. */
11067 static tree
11068 tsubst_friend_function (tree decl, tree args)
11070 tree new_friend;
11072 if (TREE_CODE (decl) == FUNCTION_DECL
11073 && DECL_TEMPLATE_INSTANTIATION (decl)
11074 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
11075 /* This was a friend declared with an explicit template
11076 argument list, e.g.:
11078 friend void f<>(T);
11080 to indicate that f was a template instantiation, not a new
11081 function declaration. Now, we have to figure out what
11082 instantiation of what template. */
11084 tree template_id, arglist, fns;
11085 tree new_args;
11086 tree tmpl;
11087 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
11089 /* Friend functions are looked up in the containing namespace scope.
11090 We must enter that scope, to avoid finding member functions of the
11091 current class with same name. */
11092 push_nested_namespace (ns);
11093 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
11094 tf_warning_or_error, NULL_TREE,
11095 /*integral_constant_expression_p=*/false);
11096 pop_nested_namespace (ns);
11097 arglist = tsubst (DECL_TI_ARGS (decl), args,
11098 tf_warning_or_error, NULL_TREE);
11099 template_id = lookup_template_function (fns, arglist);
11101 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
11102 tmpl = determine_specialization (template_id, new_friend,
11103 &new_args,
11104 /*need_member_template=*/0,
11105 TREE_VEC_LENGTH (args),
11106 tsk_none);
11107 return instantiate_template (tmpl, new_args, tf_error);
11110 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
11111 if (new_friend == error_mark_node)
11112 return error_mark_node;
11114 /* The NEW_FRIEND will look like an instantiation, to the
11115 compiler, but is not an instantiation from the point of view of
11116 the language. For example, we might have had:
11118 template <class T> struct S {
11119 template <class U> friend void f(T, U);
11122 Then, in S<int>, template <class U> void f(int, U) is not an
11123 instantiation of anything. */
11125 DECL_USE_TEMPLATE (new_friend) = 0;
11126 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
11128 DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (new_friend) = false;
11129 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
11130 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
11131 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
11133 /* Substitute TEMPLATE_PARMS_CONSTRAINTS so that parameter levels will
11134 match in decls_match. */
11135 tree parms = DECL_TEMPLATE_PARMS (new_friend);
11136 tree treqs = TEMPLATE_PARMS_CONSTRAINTS (parms);
11137 treqs = maybe_substitute_reqs_for (treqs, new_friend);
11138 TEMPLATE_PARMS_CONSTRAINTS (parms) = treqs;
11141 /* The mangled name for the NEW_FRIEND is incorrect. The function
11142 is not a template instantiation and should not be mangled like
11143 one. Therefore, we forget the mangling here; we'll recompute it
11144 later if we need it. */
11145 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
11147 SET_DECL_RTL (new_friend, NULL);
11148 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
11151 if (DECL_NAMESPACE_SCOPE_P (new_friend))
11153 tree old_decl;
11154 tree ns;
11156 /* We must save some information from NEW_FRIEND before calling
11157 duplicate decls since that function will free NEW_FRIEND if
11158 possible. */
11159 tree new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
11160 tree new_friend_result_template_info = NULL_TREE;
11161 bool new_friend_is_defn =
11162 (DECL_INITIAL (DECL_TEMPLATE_RESULT
11163 (template_for_substitution (new_friend)))
11164 != NULL_TREE);
11165 tree not_tmpl = new_friend;
11167 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
11169 /* This declaration is a `primary' template. */
11170 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
11172 not_tmpl = DECL_TEMPLATE_RESULT (new_friend);
11173 new_friend_result_template_info = DECL_TEMPLATE_INFO (not_tmpl);
11176 /* Inside pushdecl_namespace_level, we will push into the
11177 current namespace. However, the friend function should go
11178 into the namespace of the template. */
11179 ns = decl_namespace_context (new_friend);
11180 push_nested_namespace (ns);
11181 old_decl = pushdecl_namespace_level (new_friend, /*hiding=*/true);
11182 pop_nested_namespace (ns);
11184 if (old_decl == error_mark_node)
11185 return error_mark_node;
11187 if (old_decl != new_friend)
11189 /* This new friend declaration matched an existing
11190 declaration. For example, given:
11192 template <class T> void f(T);
11193 template <class U> class C {
11194 template <class T> friend void f(T) {}
11197 the friend declaration actually provides the definition
11198 of `f', once C has been instantiated for some type. So,
11199 old_decl will be the out-of-class template declaration,
11200 while new_friend is the in-class definition.
11202 But, if `f' was called before this point, the
11203 instantiation of `f' will have DECL_TI_ARGS corresponding
11204 to `T' but not to `U', references to which might appear
11205 in the definition of `f'. Previously, the most general
11206 template for an instantiation of `f' was the out-of-class
11207 version; now it is the in-class version. Therefore, we
11208 run through all specialization of `f', adding to their
11209 DECL_TI_ARGS appropriately. In particular, they need a
11210 new set of outer arguments, corresponding to the
11211 arguments for this class instantiation.
11213 The same situation can arise with something like this:
11215 friend void f(int);
11216 template <class T> class C {
11217 friend void f(T) {}
11220 when `C<int>' is instantiated. Now, `f(int)' is defined
11221 in the class. */
11223 if (!new_friend_is_defn)
11224 /* On the other hand, if the in-class declaration does
11225 *not* provide a definition, then we don't want to alter
11226 existing definitions. We can just leave everything
11227 alone. */
11229 else
11231 tree new_template = TI_TEMPLATE (new_friend_template_info);
11232 tree new_args = TI_ARGS (new_friend_template_info);
11234 /* Overwrite whatever template info was there before, if
11235 any, with the new template information pertaining to
11236 the declaration. */
11237 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
11239 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
11241 /* We should have called reregister_specialization in
11242 duplicate_decls. */
11243 gcc_assert (retrieve_specialization (new_template,
11244 new_args, 0)
11245 == old_decl);
11247 /* Instantiate it if the global has already been used. */
11248 if (DECL_ODR_USED (old_decl))
11249 instantiate_decl (old_decl, /*defer_ok=*/true,
11250 /*expl_inst_class_mem_p=*/false);
11252 else
11254 tree t;
11256 /* Indicate that the old function template is a partial
11257 instantiation. */
11258 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
11259 = new_friend_result_template_info;
11261 gcc_assert (new_template
11262 == most_general_template (new_template));
11263 gcc_assert (new_template != old_decl);
11265 /* Reassign any specializations already in the hash table
11266 to the new more general template, and add the
11267 additional template args. */
11268 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
11269 t != NULL_TREE;
11270 t = TREE_CHAIN (t))
11272 tree spec = TREE_VALUE (t);
11273 spec_entry elt;
11275 elt.tmpl = old_decl;
11276 elt.args = DECL_TI_ARGS (spec);
11277 elt.spec = NULL_TREE;
11279 decl_specializations->remove_elt (&elt);
11281 DECL_TI_ARGS (spec)
11282 = add_outermost_template_args (new_args,
11283 DECL_TI_ARGS (spec));
11285 register_specialization
11286 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
11289 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
11293 /* The information from NEW_FRIEND has been merged into OLD_DECL
11294 by duplicate_decls. */
11295 new_friend = old_decl;
11298 else
11300 tree context = DECL_CONTEXT (new_friend);
11301 bool dependent_p;
11303 /* In the code
11304 template <class T> class C {
11305 template <class U> friend void C1<U>::f (); // case 1
11306 friend void C2<T>::f (); // case 2
11308 we only need to make sure CONTEXT is a complete type for
11309 case 2. To distinguish between the two cases, we note that
11310 CONTEXT of case 1 remains dependent type after tsubst while
11311 this isn't true for case 2. */
11312 ++processing_template_decl;
11313 dependent_p = dependent_type_p (context);
11314 --processing_template_decl;
11316 if (!dependent_p
11317 && !complete_type_or_else (context, NULL_TREE))
11318 return error_mark_node;
11320 if (COMPLETE_TYPE_P (context))
11322 tree fn = new_friend;
11323 /* do_friend adds the TEMPLATE_DECL for any member friend
11324 template even if it isn't a member template, i.e.
11325 template <class T> friend A<T>::f();
11326 Look through it in that case. */
11327 if (TREE_CODE (fn) == TEMPLATE_DECL
11328 && !PRIMARY_TEMPLATE_P (fn))
11329 fn = DECL_TEMPLATE_RESULT (fn);
11330 /* Check to see that the declaration is really present, and,
11331 possibly obtain an improved declaration. */
11332 fn = check_classfn (context, fn, NULL_TREE);
11334 if (fn)
11335 new_friend = fn;
11339 return new_friend;
11342 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
11343 template arguments, as for tsubst.
11345 Returns an appropriate tsubst'd friend type or error_mark_node on
11346 failure. */
11348 static tree
11349 tsubst_friend_class (tree friend_tmpl, tree args)
11351 tree tmpl;
11353 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
11355 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
11356 return TREE_TYPE (tmpl);
11359 tree context = CP_DECL_CONTEXT (friend_tmpl);
11360 if (TREE_CODE (context) == NAMESPACE_DECL)
11361 push_nested_namespace (context);
11362 else
11364 context = tsubst (context, args, tf_error, NULL_TREE);
11365 push_nested_class (context);
11368 tmpl = lookup_name (DECL_NAME (friend_tmpl), LOOK_where::CLASS_NAMESPACE,
11369 LOOK_want::NORMAL | LOOK_want::HIDDEN_FRIEND);
11371 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
11373 /* The friend template has already been declared. Just
11374 check to see that the declarations match, and install any new
11375 default parameters. We must tsubst the default parameters,
11376 of course. We only need the innermost template parameters
11377 because that is all that redeclare_class_template will look
11378 at. */
11379 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
11380 > TMPL_ARGS_DEPTH (args))
11382 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
11383 args, tf_warning_or_error);
11384 location_t saved_input_location = input_location;
11385 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
11386 tree cons = get_constraints (tmpl);
11387 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
11388 input_location = saved_input_location;
11391 else
11393 /* The friend template has not already been declared. In this
11394 case, the instantiation of the template class will cause the
11395 injection of this template into the namespace scope. */
11396 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
11398 if (tmpl != error_mark_node)
11400 /* The new TMPL is not an instantiation of anything, so we
11401 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
11402 for the new type because that is supposed to be the
11403 corresponding template decl, i.e., TMPL. */
11404 DECL_USE_TEMPLATE (tmpl) = 0;
11405 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
11406 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
11407 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
11408 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
11410 /* Substitute into and set the constraints on the new declaration. */
11411 if (tree ci = get_constraints (friend_tmpl))
11413 ++processing_template_decl;
11414 ci = tsubst_constraint_info (ci, args, tf_warning_or_error,
11415 DECL_FRIEND_CONTEXT (friend_tmpl));
11416 --processing_template_decl;
11417 set_constraints (tmpl, ci);
11420 /* Inject this template into the enclosing namspace scope. */
11421 tmpl = pushdecl_namespace_level (tmpl, /*hiding=*/true);
11425 if (TREE_CODE (context) == NAMESPACE_DECL)
11426 pop_nested_namespace (context);
11427 else
11428 pop_nested_class ();
11430 return TREE_TYPE (tmpl);
11433 /* Returns zero if TYPE cannot be completed later due to circularity.
11434 Otherwise returns one. */
11436 static int
11437 can_complete_type_without_circularity (tree type)
11439 if (type == NULL_TREE || type == error_mark_node)
11440 return 0;
11441 else if (COMPLETE_TYPE_P (type))
11442 return 1;
11443 else if (TREE_CODE (type) == ARRAY_TYPE)
11444 return can_complete_type_without_circularity (TREE_TYPE (type));
11445 else if (CLASS_TYPE_P (type)
11446 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
11447 return 0;
11448 else
11449 return 1;
11452 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
11453 tsubst_flags_t, tree);
11455 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
11456 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
11458 static tree
11459 tsubst_attribute (tree t, tree *decl_p, tree args,
11460 tsubst_flags_t complain, tree in_decl)
11462 gcc_assert (ATTR_IS_DEPENDENT (t));
11464 tree val = TREE_VALUE (t);
11465 if (val == NULL_TREE)
11466 /* Nothing to do. */;
11467 else if ((flag_openmp || flag_openmp_simd)
11468 && is_attribute_p ("omp declare simd",
11469 get_attribute_name (t)))
11471 tree clauses = TREE_VALUE (val);
11472 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
11473 complain, in_decl);
11474 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11475 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11476 tree parms = DECL_ARGUMENTS (*decl_p);
11477 clauses
11478 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
11479 if (clauses)
11480 val = build_tree_list (NULL_TREE, clauses);
11481 else
11482 val = NULL_TREE;
11484 else if (flag_openmp
11485 && is_attribute_p ("omp declare variant base",
11486 get_attribute_name (t)))
11488 ++cp_unevaluated_operand;
11489 tree varid
11490 = tsubst_expr (TREE_PURPOSE (val), args, complain,
11491 in_decl, /*integral_constant_expression_p=*/false);
11492 --cp_unevaluated_operand;
11493 tree chain = TREE_CHAIN (val);
11494 location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
11495 tree ctx = copy_list (TREE_VALUE (val));
11496 tree simd = get_identifier ("simd");
11497 tree score = get_identifier (" score");
11498 tree condition = get_identifier ("condition");
11499 for (tree t1 = ctx; t1; t1 = TREE_CHAIN (t1))
11501 const char *set = IDENTIFIER_POINTER (TREE_PURPOSE (t1));
11502 TREE_VALUE (t1) = copy_list (TREE_VALUE (t1));
11503 for (tree t2 = TREE_VALUE (t1); t2; t2 = TREE_CHAIN (t2))
11505 if (TREE_PURPOSE (t2) == simd && set[0] == 'c')
11507 tree clauses = TREE_VALUE (t2);
11508 clauses = tsubst_omp_clauses (clauses,
11509 C_ORT_OMP_DECLARE_SIMD, args,
11510 complain, in_decl);
11511 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
11512 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
11513 TREE_VALUE (t2) = clauses;
11515 else
11517 TREE_VALUE (t2) = copy_list (TREE_VALUE (t2));
11518 for (tree t3 = TREE_VALUE (t2); t3; t3 = TREE_CHAIN (t3))
11519 if (TREE_VALUE (t3))
11521 bool allow_string
11522 = ((TREE_PURPOSE (t2) != condition || set[0] != 'u')
11523 && TREE_PURPOSE (t3) != score);
11524 tree v = TREE_VALUE (t3);
11525 if (TREE_CODE (v) == STRING_CST && allow_string)
11526 continue;
11527 v = tsubst_expr (v, args, complain, in_decl, true);
11528 v = fold_non_dependent_expr (v);
11529 if (!INTEGRAL_TYPE_P (TREE_TYPE (v))
11530 || (TREE_PURPOSE (t3) == score
11531 ? TREE_CODE (v) != INTEGER_CST
11532 : !tree_fits_shwi_p (v)))
11534 location_t loc
11535 = cp_expr_loc_or_loc (TREE_VALUE (t3),
11536 match_loc);
11537 if (TREE_PURPOSE (t3) == score)
11538 error_at (loc, "score argument must be "
11539 "constant integer expression");
11540 else if (allow_string)
11541 error_at (loc, "property must be constant "
11542 "integer expression or string "
11543 "literal");
11544 else
11545 error_at (loc, "property must be constant "
11546 "integer expression");
11547 return NULL_TREE;
11549 else if (TREE_PURPOSE (t3) == score
11550 && tree_int_cst_sgn (v) < 0)
11552 location_t loc
11553 = cp_expr_loc_or_loc (TREE_VALUE (t3),
11554 match_loc);
11555 error_at (loc, "score argument must be "
11556 "non-negative");
11557 return NULL_TREE;
11559 TREE_VALUE (t3) = v;
11564 val = tree_cons (varid, ctx, chain);
11566 /* If the first attribute argument is an identifier, don't
11567 pass it through tsubst. Attributes like mode, format,
11568 cleanup and several target specific attributes expect it
11569 unmodified. */
11570 else if (attribute_takes_identifier_p (get_attribute_name (t)))
11572 tree chain
11573 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
11574 /*integral_constant_expression_p=*/false);
11575 if (chain != TREE_CHAIN (val))
11576 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
11578 else if (PACK_EXPANSION_P (val))
11580 /* An attribute pack expansion. */
11581 tree purp = TREE_PURPOSE (t);
11582 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
11583 if (pack == error_mark_node)
11584 return error_mark_node;
11585 int len = TREE_VEC_LENGTH (pack);
11586 tree list = NULL_TREE;
11587 tree *q = &list;
11588 for (int i = 0; i < len; ++i)
11590 tree elt = TREE_VEC_ELT (pack, i);
11591 *q = build_tree_list (purp, elt);
11592 q = &TREE_CHAIN (*q);
11594 return list;
11596 else
11597 val = tsubst_expr (val, args, complain, in_decl,
11598 /*integral_constant_expression_p=*/false);
11600 if (val == error_mark_node)
11601 return error_mark_node;
11602 if (val != TREE_VALUE (t))
11603 return build_tree_list (TREE_PURPOSE (t), val);
11604 return t;
11607 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
11608 unchanged or a new TREE_LIST chain. */
11610 static tree
11611 tsubst_attributes (tree attributes, tree args,
11612 tsubst_flags_t complain, tree in_decl)
11614 tree last_dep = NULL_TREE;
11616 for (tree t = attributes; t; t = TREE_CHAIN (t))
11617 if (ATTR_IS_DEPENDENT (t))
11619 last_dep = t;
11620 attributes = copy_list (attributes);
11621 break;
11624 if (last_dep)
11625 for (tree *p = &attributes; *p; )
11627 tree t = *p;
11628 if (ATTR_IS_DEPENDENT (t))
11630 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
11631 if (subst != t)
11633 *p = subst;
11634 while (*p)
11635 p = &TREE_CHAIN (*p);
11636 *p = TREE_CHAIN (t);
11637 continue;
11640 p = &TREE_CHAIN (*p);
11643 return attributes;
11646 /* Apply any attributes which had to be deferred until instantiation
11647 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
11648 ARGS, COMPLAIN, IN_DECL are as tsubst. Returns true normally,
11649 false on error. */
11651 static bool
11652 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
11653 tree args, tsubst_flags_t complain, tree in_decl)
11655 tree t;
11656 tree *p;
11658 if (attributes == NULL_TREE)
11659 return true;
11661 if (DECL_P (*decl_p))
11663 if (TREE_TYPE (*decl_p) == error_mark_node)
11664 return false;
11665 p = &DECL_ATTRIBUTES (*decl_p);
11666 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
11667 to our attributes parameter. */
11668 gcc_assert (*p == attributes);
11670 else
11672 p = &TYPE_ATTRIBUTES (*decl_p);
11673 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
11674 lookup_template_class_1, and should be preserved. */
11675 gcc_assert (*p != attributes);
11676 while (*p)
11677 p = &TREE_CHAIN (*p);
11680 /* save_template_attributes puts the dependent attributes at the beginning of
11681 the list; find the non-dependent ones. */
11682 for (t = attributes; t; t = TREE_CHAIN (t))
11683 if (!ATTR_IS_DEPENDENT (t))
11684 break;
11685 tree nondep = t;
11687 /* Apply any non-dependent attributes. */
11688 *p = nondep;
11690 /* And then any dependent ones. */
11691 tree late_attrs = NULL_TREE;
11692 tree *q = &late_attrs;
11693 for (t = attributes; t != nondep; t = TREE_CHAIN (t))
11695 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
11696 if (*q == error_mark_node)
11697 return false;
11698 if (*q == t)
11700 *q = copy_node (t);
11701 TREE_CHAIN (*q) = NULL_TREE;
11703 while (*q)
11704 q = &TREE_CHAIN (*q);
11707 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
11709 return true;
11712 /* The template TMPL is being instantiated with the template arguments TARGS.
11713 Perform the access checks that we deferred when parsing the template. */
11715 static void
11716 perform_instantiation_time_access_checks (tree tmpl, tree targs)
11718 unsigned i;
11719 deferred_access_check *chk;
11721 if (!CLASS_TYPE_P (tmpl) && TREE_CODE (tmpl) != FUNCTION_DECL)
11722 return;
11724 if (vec<deferred_access_check, va_gc> *access_checks
11725 = TI_DEFERRED_ACCESS_CHECKS (get_template_info (tmpl)))
11726 FOR_EACH_VEC_ELT (*access_checks, i, chk)
11728 tree decl = chk->decl;
11729 tree diag_decl = chk->diag_decl;
11730 tree type_scope = TREE_TYPE (chk->binfo);
11732 if (uses_template_parms (type_scope))
11733 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
11735 /* Make access check error messages point to the location
11736 of the use of the typedef. */
11737 iloc_sentinel ils (chk->loc);
11738 perform_or_defer_access_check (TYPE_BINFO (type_scope),
11739 decl, diag_decl, tf_warning_or_error);
11743 static tree
11744 instantiate_class_template_1 (tree type)
11746 tree templ, args, pattern, t, member;
11747 tree typedecl;
11748 tree pbinfo;
11749 tree base_list;
11750 unsigned int saved_maximum_field_alignment;
11751 tree fn_context;
11753 if (type == error_mark_node)
11754 return error_mark_node;
11756 if (COMPLETE_OR_OPEN_TYPE_P (type)
11757 || uses_template_parms (type))
11758 return type;
11760 /* Figure out which template is being instantiated. */
11761 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
11762 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
11764 /* Mark the type as in the process of being defined. */
11765 TYPE_BEING_DEFINED (type) = 1;
11767 /* We may be in the middle of deferred access check. Disable
11768 it now. */
11769 deferring_access_check_sentinel acs (dk_no_deferred);
11771 /* Determine what specialization of the original template to
11772 instantiate. */
11773 t = most_specialized_partial_spec (type, tf_warning_or_error);
11774 if (t == error_mark_node)
11775 return error_mark_node;
11776 else if (t)
11778 /* This TYPE is actually an instantiation of a partial
11779 specialization. We replace the innermost set of ARGS with
11780 the arguments appropriate for substitution. For example,
11781 given:
11783 template <class T> struct S {};
11784 template <class T> struct S<T*> {};
11786 and supposing that we are instantiating S<int*>, ARGS will
11787 presently be {int*} -- but we need {int}. */
11788 pattern = TREE_TYPE (t);
11789 args = TREE_PURPOSE (t);
11791 else
11793 pattern = TREE_TYPE (templ);
11794 args = CLASSTYPE_TI_ARGS (type);
11797 /* If the template we're instantiating is incomplete, then clearly
11798 there's nothing we can do. */
11799 if (!COMPLETE_TYPE_P (pattern))
11801 /* We can try again later. */
11802 TYPE_BEING_DEFINED (type) = 0;
11803 return type;
11806 /* If we've recursively instantiated too many templates, stop. */
11807 if (! push_tinst_level (type))
11808 return type;
11810 int saved_unevaluated_operand = cp_unevaluated_operand;
11811 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11813 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
11814 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
11815 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
11816 fn_context = error_mark_node;
11817 if (!fn_context)
11818 push_to_top_level ();
11819 else
11821 cp_unevaluated_operand = 0;
11822 c_inhibit_evaluation_warnings = 0;
11824 /* Use #pragma pack from the template context. */
11825 saved_maximum_field_alignment = maximum_field_alignment;
11826 maximum_field_alignment = TYPE_PRECISION (pattern);
11828 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
11830 /* Set the input location to the most specialized template definition.
11831 This is needed if tsubsting causes an error. */
11832 typedecl = TYPE_MAIN_DECL (pattern);
11833 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
11834 DECL_SOURCE_LOCATION (typedecl);
11836 set_instantiating_module (TYPE_NAME (type));
11838 TYPE_PACKED (type) = TYPE_PACKED (pattern);
11839 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
11840 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
11841 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
11842 if (ANON_AGGR_TYPE_P (pattern))
11843 SET_ANON_AGGR_TYPE_P (type);
11844 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
11846 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
11847 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
11848 /* Adjust visibility for template arguments. */
11849 determine_visibility (TYPE_MAIN_DECL (type));
11851 if (CLASS_TYPE_P (type))
11852 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
11854 pbinfo = TYPE_BINFO (pattern);
11856 /* We should never instantiate a nested class before its enclosing
11857 class; we need to look up the nested class by name before we can
11858 instantiate it, and that lookup should instantiate the enclosing
11859 class. */
11860 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
11861 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
11863 base_list = NULL_TREE;
11864 /* Defer access checking while we substitute into the types named in
11865 the base-clause. */
11866 push_deferring_access_checks (dk_deferred);
11867 if (BINFO_N_BASE_BINFOS (pbinfo))
11869 tree pbase_binfo;
11870 int i;
11872 /* Substitute into each of the bases to determine the actual
11873 basetypes. */
11874 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
11876 tree base;
11877 tree access = BINFO_BASE_ACCESS (pbinfo, i);
11878 tree expanded_bases = NULL_TREE;
11879 int idx, len = 1;
11881 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
11883 expanded_bases =
11884 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
11885 args, tf_error, NULL_TREE);
11886 if (expanded_bases == error_mark_node)
11887 continue;
11889 len = TREE_VEC_LENGTH (expanded_bases);
11892 for (idx = 0; idx < len; idx++)
11894 if (expanded_bases)
11895 /* Extract the already-expanded base class. */
11896 base = TREE_VEC_ELT (expanded_bases, idx);
11897 else
11898 /* Substitute to figure out the base class. */
11899 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
11900 NULL_TREE);
11902 if (base == error_mark_node)
11903 continue;
11905 base_list = tree_cons (access, base, base_list);
11906 if (BINFO_VIRTUAL_P (pbase_binfo))
11907 TREE_TYPE (base_list) = integer_type_node;
11911 /* The list is now in reverse order; correct that. */
11912 base_list = nreverse (base_list);
11914 /* Now call xref_basetypes to set up all the base-class
11915 information. */
11916 xref_basetypes (type, base_list);
11918 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
11919 (int) ATTR_FLAG_TYPE_IN_PLACE,
11920 args, tf_error, NULL_TREE);
11921 fixup_attribute_variants (type);
11923 /* Now that our base classes are set up, enter the scope of the
11924 class, so that name lookups into base classes, etc. will work
11925 correctly. This is precisely analogous to what we do in
11926 begin_class_definition when defining an ordinary non-template
11927 class, except we also need to push the enclosing classes. */
11928 push_nested_class (type);
11930 /* Now check accessibility of the types named in its base-clause,
11931 relative to the scope of the class. */
11932 pop_to_parent_deferring_access_checks ();
11934 /* A vector to hold members marked with attribute used. */
11935 auto_vec<tree> used;
11937 /* Now members are processed in the order of declaration. */
11938 for (member = CLASSTYPE_DECL_LIST (pattern);
11939 member; member = TREE_CHAIN (member))
11941 tree t = TREE_VALUE (member);
11943 if (TREE_PURPOSE (member))
11945 if (TYPE_P (t))
11947 if (LAMBDA_TYPE_P (t))
11948 /* A closure type for a lambda in an NSDMI or default argument.
11949 Ignore it; it will be regenerated when needed. */
11950 continue;
11952 bool class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
11953 && TYPE_LANG_SPECIFIC (t)
11954 && CLASSTYPE_IS_TEMPLATE (t));
11956 /* If the member is a class template, then -- even after
11957 substitution -- there may be dependent types in the
11958 template argument list for the class. We increment
11959 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
11960 that function will assume that no types are dependent
11961 when outside of a template. */
11962 if (class_template_p)
11963 ++processing_template_decl;
11964 tree newtag = tsubst (t, args, tf_error, NULL_TREE);
11965 if (class_template_p)
11966 --processing_template_decl;
11967 if (newtag == error_mark_node)
11968 continue;
11970 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
11972 tree name = TYPE_IDENTIFIER (t);
11974 if (class_template_p)
11975 /* Unfortunately, lookup_template_class sets
11976 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
11977 instantiation (i.e., for the type of a member
11978 template class nested within a template class.)
11979 This behavior is required for
11980 maybe_process_partial_specialization to work
11981 correctly, but is not accurate in this case;
11982 the TAG is not an instantiation of anything.
11983 (The corresponding TEMPLATE_DECL is an
11984 instantiation, but the TYPE is not.) */
11985 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
11987 /* Now, install the tag. We don't use pushtag
11988 because that does too much work -- creating an
11989 implicit typedef, which we've already done. */
11990 set_identifier_type_value (name, TYPE_NAME (newtag));
11991 maybe_add_class_template_decl_list (type, newtag, false);
11992 TREE_PUBLIC (TYPE_NAME (newtag)) = true;
11993 determine_visibility (TYPE_NAME (newtag));
11996 else if (DECL_DECLARES_FUNCTION_P (t))
11998 tree r;
12000 if (TREE_CODE (t) == TEMPLATE_DECL)
12001 ++processing_template_decl;
12002 r = tsubst (t, args, tf_error, NULL_TREE);
12003 if (TREE_CODE (t) == TEMPLATE_DECL)
12004 --processing_template_decl;
12005 set_current_access_from_decl (r);
12006 finish_member_declaration (r);
12007 /* Instantiate members marked with attribute used. */
12008 if (r != error_mark_node && DECL_PRESERVE_P (r))
12009 used.safe_push (r);
12010 if (TREE_CODE (r) == FUNCTION_DECL
12011 && DECL_OMP_DECLARE_REDUCTION_P (r))
12012 cp_check_omp_declare_reduction (r);
12014 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
12015 && LAMBDA_TYPE_P (TREE_TYPE (t)))
12016 /* A closure type for a lambda in an NSDMI or default argument.
12017 Ignore it; it will be regenerated when needed. */;
12018 else
12020 /* Build new TYPE_FIELDS. */
12021 if (TREE_CODE (t) == STATIC_ASSERT)
12022 tsubst_expr (t, args, tf_warning_or_error, NULL_TREE,
12023 /*integral_constant_expression_p=*/true);
12024 else if (TREE_CODE (t) != CONST_DECL)
12026 tree r;
12027 tree vec = NULL_TREE;
12028 int len = 1;
12030 gcc_checking_assert (TREE_CODE (t) != CONST_DECL);
12031 /* The file and line for this declaration, to
12032 assist in error message reporting. Since we
12033 called push_tinst_level above, we don't need to
12034 restore these. */
12035 input_location = DECL_SOURCE_LOCATION (t);
12037 if (TREE_CODE (t) == TEMPLATE_DECL)
12038 ++processing_template_decl;
12039 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
12040 if (TREE_CODE (t) == TEMPLATE_DECL)
12041 --processing_template_decl;
12043 if (TREE_CODE (r) == TREE_VEC)
12045 /* A capture pack became multiple fields. */
12046 vec = r;
12047 len = TREE_VEC_LENGTH (vec);
12050 for (int i = 0; i < len; ++i)
12052 if (vec)
12053 r = TREE_VEC_ELT (vec, i);
12054 if (VAR_P (r))
12056 /* In [temp.inst]:
12058 [t]he initialization (and any associated
12059 side-effects) of a static data member does
12060 not occur unless the static data member is
12061 itself used in a way that requires the
12062 definition of the static data member to
12063 exist.
12065 Therefore, we do not substitute into the
12066 initialized for the static data member here. */
12067 finish_static_data_member_decl
12069 /*init=*/NULL_TREE,
12070 /*init_const_expr_p=*/false,
12071 /*asmspec_tree=*/NULL_TREE,
12072 /*flags=*/0);
12073 /* Instantiate members marked with attribute used. */
12074 if (r != error_mark_node && DECL_PRESERVE_P (r))
12075 used.safe_push (r);
12077 else if (TREE_CODE (r) == FIELD_DECL)
12079 /* Determine whether R has a valid type and can be
12080 completed later. If R is invalid, then its type
12081 is replaced by error_mark_node. */
12082 tree rtype = TREE_TYPE (r);
12083 if (can_complete_type_without_circularity (rtype))
12084 complete_type (rtype);
12086 if (!complete_or_array_type_p (rtype))
12088 /* If R's type couldn't be completed and
12089 it isn't a flexible array member (whose
12090 type is incomplete by definition) give
12091 an error. */
12092 cxx_incomplete_type_error (r, rtype);
12093 TREE_TYPE (r) = error_mark_node;
12095 else if (TREE_CODE (rtype) == ARRAY_TYPE
12096 && TYPE_DOMAIN (rtype) == NULL_TREE
12097 && (TREE_CODE (type) == UNION_TYPE
12098 || TREE_CODE (type) == QUAL_UNION_TYPE))
12100 error ("flexible array member %qD in union", r);
12101 TREE_TYPE (r) = error_mark_node;
12103 else if (!verify_type_context (input_location,
12104 TCTX_FIELD, rtype))
12105 TREE_TYPE (r) = error_mark_node;
12108 /* If it is a TYPE_DECL for a class-scoped
12109 ENUMERAL_TYPE, such a thing will already have
12110 been added to the field list by tsubst_enum
12111 in finish_member_declaration case above. */
12112 if (!(TREE_CODE (r) == TYPE_DECL
12113 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
12114 && DECL_ARTIFICIAL (r)))
12116 set_current_access_from_decl (r);
12117 finish_member_declaration (r);
12123 else
12125 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
12126 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12128 /* Build new CLASSTYPE_FRIEND_CLASSES. */
12130 tree friend_type = t;
12131 bool adjust_processing_template_decl = false;
12133 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
12135 /* template <class T> friend class C; */
12136 friend_type = tsubst_friend_class (friend_type, args);
12137 adjust_processing_template_decl = true;
12139 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
12141 /* template <class T> friend class C::D; */
12142 friend_type = tsubst (friend_type, args,
12143 tf_warning_or_error, NULL_TREE);
12144 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
12145 friend_type = TREE_TYPE (friend_type);
12146 adjust_processing_template_decl = true;
12148 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
12149 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
12151 /* This could be either
12153 friend class T::C;
12155 when dependent_type_p is false or
12157 template <class U> friend class T::C;
12159 otherwise. */
12160 /* Bump processing_template_decl in case this is something like
12161 template <class T> friend struct A<T>::B. */
12162 ++processing_template_decl;
12163 friend_type = tsubst (friend_type, args,
12164 tf_warning_or_error, NULL_TREE);
12165 if (dependent_type_p (friend_type))
12166 adjust_processing_template_decl = true;
12167 --processing_template_decl;
12169 else if (uses_template_parms (friend_type))
12170 /* friend class C<T>; */
12171 friend_type = tsubst (friend_type, args,
12172 tf_warning_or_error, NULL_TREE);
12174 /* Otherwise it's
12176 friend class C;
12178 where C is already declared or
12180 friend class C<int>;
12182 We don't have to do anything in these cases. */
12184 if (adjust_processing_template_decl)
12185 /* Trick make_friend_class into realizing that the friend
12186 we're adding is a template, not an ordinary class. It's
12187 important that we use make_friend_class since it will
12188 perform some error-checking and output cross-reference
12189 information. */
12190 ++processing_template_decl;
12192 if (friend_type != error_mark_node)
12193 make_friend_class (type, friend_type, /*complain=*/false);
12195 if (adjust_processing_template_decl)
12196 --processing_template_decl;
12198 else
12200 /* Build new DECL_FRIENDLIST. */
12201 tree r;
12203 /* The file and line for this declaration, to
12204 assist in error message reporting. Since we
12205 called push_tinst_level above, we don't need to
12206 restore these. */
12207 input_location = DECL_SOURCE_LOCATION (t);
12209 if (TREE_CODE (t) == TEMPLATE_DECL)
12211 ++processing_template_decl;
12212 push_deferring_access_checks (dk_no_check);
12215 r = tsubst_friend_function (t, args);
12216 add_friend (type, r, /*complain=*/false);
12217 if (TREE_CODE (t) == TEMPLATE_DECL)
12219 pop_deferring_access_checks ();
12220 --processing_template_decl;
12226 if (fn_context)
12228 /* Restore these before substituting into the lambda capture
12229 initializers. */
12230 cp_unevaluated_operand = saved_unevaluated_operand;
12231 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
12234 /* Set the file and line number information to whatever is given for
12235 the class itself. This puts error messages involving generated
12236 implicit functions at a predictable point, and the same point
12237 that would be used for non-template classes. */
12238 input_location = DECL_SOURCE_LOCATION (typedecl);
12240 unreverse_member_declarations (type);
12241 finish_struct_1 (type);
12242 TYPE_BEING_DEFINED (type) = 0;
12244 /* We don't instantiate default arguments for member functions. 14.7.1:
12246 The implicit instantiation of a class template specialization causes
12247 the implicit instantiation of the declarations, but not of the
12248 definitions or default arguments, of the class member functions,
12249 member classes, static data members and member templates.... */
12251 perform_instantiation_time_access_checks (pattern, args);
12252 perform_deferred_access_checks (tf_warning_or_error);
12253 pop_nested_class ();
12254 maximum_field_alignment = saved_maximum_field_alignment;
12255 if (!fn_context)
12256 pop_from_top_level ();
12257 pop_tinst_level ();
12259 /* The vtable for a template class can be emitted in any translation
12260 unit in which the class is instantiated. When there is no key
12261 method, however, finish_struct_1 will already have added TYPE to
12262 the keyed_classes. */
12263 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
12264 vec_safe_push (keyed_classes, type);
12266 /* Now that we've gone through all the members, instantiate those
12267 marked with attribute used. */
12268 for (tree x : used)
12269 mark_used (x);
12271 return type;
12274 /* Wrapper for instantiate_class_template_1. */
12276 tree
12277 instantiate_class_template (tree type)
12279 tree ret;
12280 timevar_push (TV_TEMPLATE_INST);
12281 ret = instantiate_class_template_1 (type);
12282 timevar_pop (TV_TEMPLATE_INST);
12283 return ret;
12286 tree
12287 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12289 tree r;
12291 if (!t)
12292 r = t;
12293 else if (TYPE_P (t))
12294 r = tsubst (t, args, complain, in_decl);
12295 else
12297 if (!(complain & tf_warning))
12298 ++c_inhibit_evaluation_warnings;
12299 r = tsubst_expr (t, args, complain, in_decl,
12300 /*integral_constant_expression_p=*/true);
12301 if (!(complain & tf_warning))
12302 --c_inhibit_evaluation_warnings;
12305 return r;
12308 /* Given a function parameter pack TMPL_PARM and some function parameters
12309 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
12310 and set *SPEC_P to point at the next point in the list. */
12312 tree
12313 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
12315 /* Collect all of the extra "packed" parameters into an
12316 argument pack. */
12317 tree argpack;
12318 tree spec_parm = *spec_p;
12319 int len;
12321 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
12322 if (tmpl_parm
12323 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
12324 break;
12326 spec_parm = *spec_p;
12327 if (len == 1 && DECL_PACK_P (spec_parm))
12329 /* The instantiation is still a parameter pack; don't wrap it in a
12330 NONTYPE_ARGUMENT_PACK. */
12331 argpack = spec_parm;
12332 spec_parm = DECL_CHAIN (spec_parm);
12334 else
12336 /* Fill in PARMVEC with all of the parameters. */
12337 tree parmvec = make_tree_vec (len);
12338 argpack = make_node (NONTYPE_ARGUMENT_PACK);
12339 for (int i = 0; i < len; i++)
12341 tree elt = spec_parm;
12342 if (DECL_PACK_P (elt))
12343 elt = make_pack_expansion (elt);
12344 TREE_VEC_ELT (parmvec, i) = elt;
12345 spec_parm = DECL_CHAIN (spec_parm);
12348 /* Build the argument packs. */
12349 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
12351 *spec_p = spec_parm;
12353 return argpack;
12356 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
12357 NONTYPE_ARGUMENT_PACK. */
12359 static tree
12360 make_fnparm_pack (tree spec_parm)
12362 return extract_fnparm_pack (NULL_TREE, &spec_parm);
12365 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
12366 pack expansion with no extra args, 2 if it has extra args, or 0
12367 if it is not a pack expansion. */
12369 static int
12370 argument_pack_element_is_expansion_p (tree arg_pack, int i)
12372 if (TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
12373 /* We're being called before this happens in tsubst_pack_expansion. */
12374 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
12375 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
12376 if (i >= TREE_VEC_LENGTH (vec))
12377 return 0;
12378 tree elt = TREE_VEC_ELT (vec, i);
12379 if (DECL_P (elt))
12380 /* A decl pack is itself an expansion. */
12381 elt = TREE_TYPE (elt);
12382 if (!PACK_EXPANSION_P (elt))
12383 return 0;
12384 if (PACK_EXPANSION_EXTRA_ARGS (elt))
12385 return 2;
12386 return 1;
12390 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
12392 static tree
12393 make_argument_pack_select (tree arg_pack, unsigned index)
12395 tree aps = make_node (ARGUMENT_PACK_SELECT);
12397 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
12398 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12400 return aps;
12403 /* This is a subroutine of tsubst_pack_expansion.
12405 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
12406 mechanism to store the (non complete list of) arguments of the
12407 substitution and return a non substituted pack expansion, in order
12408 to wait for when we have enough arguments to really perform the
12409 substitution. */
12411 static bool
12412 use_pack_expansion_extra_args_p (tree parm_packs,
12413 int arg_pack_len,
12414 bool has_empty_arg)
12416 /* If one pack has an expansion and another pack has a normal
12417 argument or if one pack has an empty argument and an another
12418 one hasn't then tsubst_pack_expansion cannot perform the
12419 substitution and need to fall back on the
12420 PACK_EXPANSION_EXTRA mechanism. */
12421 if (parm_packs == NULL_TREE)
12422 return false;
12423 else if (has_empty_arg)
12425 /* If all the actual packs are pack expansions, we can still
12426 subsitute directly. */
12427 for (tree p = parm_packs; p; p = TREE_CHAIN (p))
12429 tree a = TREE_VALUE (p);
12430 if (TREE_CODE (a) == ARGUMENT_PACK_SELECT)
12431 a = ARGUMENT_PACK_SELECT_FROM_PACK (a);
12432 a = ARGUMENT_PACK_ARGS (a);
12433 if (TREE_VEC_LENGTH (a) == 1)
12434 a = TREE_VEC_ELT (a, 0);
12435 if (PACK_EXPANSION_P (a))
12436 continue;
12437 return true;
12439 return false;
12442 for (int i = 0 ; i < arg_pack_len; ++i)
12444 bool has_expansion_arg = false;
12445 bool has_non_expansion_arg = false;
12446 for (tree parm_pack = parm_packs;
12447 parm_pack;
12448 parm_pack = TREE_CHAIN (parm_pack))
12450 tree arg = TREE_VALUE (parm_pack);
12452 int exp = argument_pack_element_is_expansion_p (arg, i);
12453 if (exp == 2)
12454 /* We can't substitute a pack expansion with extra args into
12455 our pattern. */
12456 return true;
12457 else if (exp)
12458 has_expansion_arg = true;
12459 else
12460 has_non_expansion_arg = true;
12463 if (has_expansion_arg && has_non_expansion_arg)
12465 gcc_checking_assert (false);
12466 return true;
12469 return false;
12472 /* [temp.variadic]/6 says that:
12474 The instantiation of a pack expansion [...]
12475 produces a list E1,E2, ..., En, where N is the number of elements
12476 in the pack expansion parameters.
12478 This subroutine of tsubst_pack_expansion produces one of these Ei.
12480 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
12481 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
12482 PATTERN, and each TREE_VALUE is its corresponding argument pack.
12483 INDEX is the index 'i' of the element Ei to produce. ARGS,
12484 COMPLAIN, and IN_DECL are the same parameters as for the
12485 tsubst_pack_expansion function.
12487 The function returns the resulting Ei upon successful completion,
12488 or error_mark_node.
12490 Note that this function possibly modifies the ARGS parameter, so
12491 it's the responsibility of the caller to restore it. */
12493 static tree
12494 gen_elem_of_pack_expansion_instantiation (tree pattern,
12495 tree parm_packs,
12496 unsigned index,
12497 tree args /* This parm gets
12498 modified. */,
12499 tsubst_flags_t complain,
12500 tree in_decl)
12502 tree t;
12503 bool ith_elem_is_expansion = false;
12505 /* For each parameter pack, change the substitution of the parameter
12506 pack to the ith argument in its argument pack, then expand the
12507 pattern. */
12508 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
12510 tree parm = TREE_PURPOSE (pack);
12511 tree arg_pack = TREE_VALUE (pack);
12512 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
12514 ith_elem_is_expansion |=
12515 argument_pack_element_is_expansion_p (arg_pack, index);
12517 /* Select the Ith argument from the pack. */
12518 if (TREE_CODE (parm) == PARM_DECL
12519 || VAR_P (parm)
12520 || TREE_CODE (parm) == FIELD_DECL)
12522 if (index == 0)
12524 aps = make_argument_pack_select (arg_pack, index);
12525 if (!mark_used (parm, complain) && !(complain & tf_error))
12526 return error_mark_node;
12527 register_local_specialization (aps, parm);
12529 else
12530 aps = retrieve_local_specialization (parm);
12532 else
12534 int idx, level;
12535 template_parm_level_and_index (parm, &level, &idx);
12537 if (index == 0)
12539 aps = make_argument_pack_select (arg_pack, index);
12540 /* Update the corresponding argument. */
12541 TMPL_ARG (args, level, idx) = aps;
12543 else
12544 /* Re-use the ARGUMENT_PACK_SELECT. */
12545 aps = TMPL_ARG (args, level, idx);
12547 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
12550 /* Substitute into the PATTERN with the (possibly altered)
12551 arguments. */
12552 if (pattern == in_decl)
12553 /* Expanding a fixed parameter pack from
12554 coerce_template_parameter_pack. */
12555 t = tsubst_decl (pattern, args, complain);
12556 else if (pattern == error_mark_node)
12557 t = error_mark_node;
12558 else if (!TYPE_P (pattern))
12559 t = tsubst_expr (pattern, args, complain, in_decl,
12560 /*integral_constant_expression_p=*/false);
12561 else
12562 t = tsubst (pattern, args, complain, in_decl);
12564 /* If the Ith argument pack element is a pack expansion, then
12565 the Ith element resulting from the substituting is going to
12566 be a pack expansion as well. */
12567 if (ith_elem_is_expansion)
12568 t = make_pack_expansion (t, complain);
12570 return t;
12573 /* When the unexpanded parameter pack in a fold expression expands to an empty
12574 sequence, the value of the expression is as follows; the program is
12575 ill-formed if the operator is not listed in this table.
12577 && true
12578 || false
12579 , void() */
12581 tree
12582 expand_empty_fold (tree t, tsubst_flags_t complain)
12584 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
12585 if (!FOLD_EXPR_MODIFY_P (t))
12586 switch (code)
12588 case TRUTH_ANDIF_EXPR:
12589 return boolean_true_node;
12590 case TRUTH_ORIF_EXPR:
12591 return boolean_false_node;
12592 case COMPOUND_EXPR:
12593 return void_node;
12594 default:
12595 break;
12598 if (complain & tf_error)
12599 error_at (location_of (t),
12600 "fold of empty expansion over %O", code);
12601 return error_mark_node;
12604 /* Given a fold-expression T and a current LEFT and RIGHT operand,
12605 form an expression that combines the two terms using the
12606 operator of T. */
12608 static tree
12609 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
12611 tree op = FOLD_EXPR_OP (t);
12612 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
12614 // Handle compound assignment operators.
12615 if (FOLD_EXPR_MODIFY_P (t))
12616 return build_x_modify_expr (input_location, left, code, right, complain);
12618 warning_sentinel s(warn_parentheses);
12619 switch (code)
12621 case COMPOUND_EXPR:
12622 return build_x_compound_expr (input_location, left, right, complain);
12623 default:
12624 return build_x_binary_op (input_location, code,
12625 left, TREE_CODE (left),
12626 right, TREE_CODE (right),
12627 /*overload=*/NULL,
12628 complain);
12632 /* Substitute ARGS into the pack of a fold expression T. */
12634 static inline tree
12635 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12637 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
12640 /* Substitute ARGS into the pack of a fold expression T. */
12642 static inline tree
12643 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12645 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
12648 /* Expand a PACK of arguments into a grouped as left fold.
12649 Given a pack containing elements A0, A1, ..., An and an
12650 operator @, this builds the expression:
12652 ((A0 @ A1) @ A2) ... @ An
12654 Note that PACK must not be empty.
12656 The operator is defined by the original fold expression T. */
12658 static tree
12659 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
12661 tree left = TREE_VEC_ELT (pack, 0);
12662 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
12664 tree right = TREE_VEC_ELT (pack, i);
12665 left = fold_expression (t, left, right, complain);
12667 return left;
12670 /* Substitute into a unary left fold expression. */
12672 static tree
12673 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
12674 tree in_decl)
12676 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12677 if (pack == error_mark_node)
12678 return error_mark_node;
12679 if (PACK_EXPANSION_P (pack))
12681 tree r = copy_node (t);
12682 FOLD_EXPR_PACK (r) = pack;
12683 return r;
12685 if (TREE_VEC_LENGTH (pack) == 0)
12686 return expand_empty_fold (t, complain);
12687 else
12688 return expand_left_fold (t, pack, complain);
12691 /* Substitute into a binary left fold expression.
12693 Do ths by building a single (non-empty) vector of argumnts and
12694 building the expression from those elements. */
12696 static tree
12697 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
12698 tree in_decl)
12700 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12701 if (pack == error_mark_node)
12702 return error_mark_node;
12703 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12704 if (init == error_mark_node)
12705 return error_mark_node;
12707 if (PACK_EXPANSION_P (pack))
12709 tree r = copy_node (t);
12710 FOLD_EXPR_PACK (r) = pack;
12711 FOLD_EXPR_INIT (r) = init;
12712 return r;
12715 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
12716 TREE_VEC_ELT (vec, 0) = init;
12717 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
12718 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
12720 return expand_left_fold (t, vec, complain);
12723 /* Expand a PACK of arguments into a grouped as right fold.
12724 Given a pack containing elementns A0, A1, ..., and an
12725 operator @, this builds the expression:
12727 A0@ ... (An-2 @ (An-1 @ An))
12729 Note that PACK must not be empty.
12731 The operator is defined by the original fold expression T. */
12733 tree
12734 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
12736 // Build the expression.
12737 int n = TREE_VEC_LENGTH (pack);
12738 tree right = TREE_VEC_ELT (pack, n - 1);
12739 for (--n; n != 0; --n)
12741 tree left = TREE_VEC_ELT (pack, n - 1);
12742 right = fold_expression (t, left, right, complain);
12744 return right;
12747 /* Substitute into a unary right fold expression. */
12749 static tree
12750 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
12751 tree in_decl)
12753 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12754 if (pack == error_mark_node)
12755 return error_mark_node;
12756 if (PACK_EXPANSION_P (pack))
12758 tree r = copy_node (t);
12759 FOLD_EXPR_PACK (r) = pack;
12760 return r;
12762 if (TREE_VEC_LENGTH (pack) == 0)
12763 return expand_empty_fold (t, complain);
12764 else
12765 return expand_right_fold (t, pack, complain);
12768 /* Substitute into a binary right fold expression.
12770 Do ths by building a single (non-empty) vector of arguments and
12771 building the expression from those elements. */
12773 static tree
12774 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
12775 tree in_decl)
12777 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
12778 if (pack == error_mark_node)
12779 return error_mark_node;
12780 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
12781 if (init == error_mark_node)
12782 return error_mark_node;
12784 if (PACK_EXPANSION_P (pack))
12786 tree r = copy_node (t);
12787 FOLD_EXPR_PACK (r) = pack;
12788 FOLD_EXPR_INIT (r) = init;
12789 return r;
12792 int n = TREE_VEC_LENGTH (pack);
12793 tree vec = make_tree_vec (n + 1);
12794 for (int i = 0; i < n; ++i)
12795 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
12796 TREE_VEC_ELT (vec, n) = init;
12798 return expand_right_fold (t, vec, complain);
12801 /* Walk through the pattern of a pack expansion, adding everything in
12802 local_specializations to a list. */
12804 class el_data
12806 public:
12807 /* Set of variables declared within the pattern. */
12808 hash_set<tree> internal;
12809 /* Set of AST nodes that have been visited by the traversal. */
12810 hash_set<tree> visited;
12811 /* List of local_specializations used within the pattern. */
12812 tree extra;
12813 tsubst_flags_t complain;
12815 el_data (tsubst_flags_t c)
12816 : extra (NULL_TREE), complain (c) {}
12818 static tree
12819 extract_locals_r (tree *tp, int */*walk_subtrees*/, void *data_)
12821 el_data &data = *reinterpret_cast<el_data*>(data_);
12822 tree *extra = &data.extra;
12823 tsubst_flags_t complain = data.complain;
12825 if (TYPE_P (*tp) && typedef_variant_p (*tp))
12826 /* Remember local typedefs (85214). */
12827 tp = &TYPE_NAME (*tp);
12829 if (TREE_CODE (*tp) == DECL_EXPR)
12831 tree decl = DECL_EXPR_DECL (*tp);
12832 data.internal.add (decl);
12833 if (VAR_P (decl)
12834 && DECL_DECOMPOSITION_P (decl)
12835 && TREE_TYPE (decl) != error_mark_node)
12837 gcc_assert (DECL_NAME (decl) == NULL_TREE);
12838 for (tree decl2 = DECL_CHAIN (decl);
12839 decl2
12840 && VAR_P (decl2)
12841 && DECL_DECOMPOSITION_P (decl2)
12842 && DECL_NAME (decl2)
12843 && TREE_TYPE (decl2) != error_mark_node;
12844 decl2 = DECL_CHAIN (decl2))
12846 gcc_assert (DECL_DECOMP_BASE (decl2) == decl);
12847 data.internal.add (decl2);
12851 else if (TREE_CODE (*tp) == LAMBDA_EXPR)
12853 /* Since we defer implicit capture, look in the parms and body. */
12854 tree fn = lambda_function (*tp);
12855 cp_walk_tree (&TREE_TYPE (fn), &extract_locals_r, &data,
12856 &data.visited);
12857 cp_walk_tree (&DECL_SAVED_TREE (fn), &extract_locals_r, &data,
12858 &data.visited);
12860 else if (tree spec = retrieve_local_specialization (*tp))
12862 if (data.internal.contains (*tp))
12863 /* Don't mess with variables declared within the pattern. */
12864 return NULL_TREE;
12865 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12867 /* Maybe pull out the PARM_DECL for a partial instantiation. */
12868 tree args = ARGUMENT_PACK_ARGS (spec);
12869 if (TREE_VEC_LENGTH (args) == 1)
12871 tree elt = TREE_VEC_ELT (args, 0);
12872 if (PACK_EXPANSION_P (elt))
12873 elt = PACK_EXPANSION_PATTERN (elt);
12874 if (DECL_PACK_P (elt))
12875 spec = elt;
12877 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
12879 /* Handle lambda capture here, since we aren't doing any
12880 substitution now, and so tsubst_copy won't call
12881 process_outer_var_ref. */
12882 tree args = ARGUMENT_PACK_ARGS (spec);
12883 int len = TREE_VEC_LENGTH (args);
12884 for (int i = 0; i < len; ++i)
12886 tree arg = TREE_VEC_ELT (args, i);
12887 tree carg = arg;
12888 if (outer_automatic_var_p (arg))
12889 carg = process_outer_var_ref (arg, complain);
12890 if (carg != arg)
12892 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
12893 proxies. */
12894 if (i == 0)
12896 spec = copy_node (spec);
12897 args = copy_node (args);
12898 SET_ARGUMENT_PACK_ARGS (spec, args);
12899 register_local_specialization (spec, *tp);
12901 TREE_VEC_ELT (args, i) = carg;
12906 if (outer_automatic_var_p (spec))
12907 spec = process_outer_var_ref (spec, complain);
12908 *extra = tree_cons (*tp, spec, *extra);
12910 return NULL_TREE;
12912 static tree
12913 extract_local_specs (tree pattern, tsubst_flags_t complain)
12915 el_data data (complain);
12916 cp_walk_tree (&pattern, extract_locals_r, &data, &data.visited);
12917 return data.extra;
12920 /* Extract any uses of local_specializations from PATTERN and add them to ARGS
12921 for use in PACK_EXPANSION_EXTRA_ARGS. */
12923 tree
12924 build_extra_args (tree pattern, tree args, tsubst_flags_t complain)
12926 tree extra = args;
12927 if (local_specializations)
12928 if (tree locals = extract_local_specs (pattern, complain))
12929 extra = tree_cons (NULL_TREE, extra, locals);
12930 return extra;
12933 /* Apply any local specializations from PACK_EXPANSION_EXTRA_ARGS and add the
12934 normal template args to ARGS. */
12936 tree
12937 add_extra_args (tree extra, tree args)
12939 if (extra && TREE_CODE (extra) == TREE_LIST)
12941 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
12943 /* The partial instantiation involved local declarations collected in
12944 extract_local_specs; map from the general template to our local
12945 context. */
12946 tree gen = TREE_PURPOSE (elt);
12947 tree inst = TREE_VALUE (elt);
12948 if (DECL_P (inst))
12949 if (tree local = retrieve_local_specialization (inst))
12950 inst = local;
12951 /* else inst is already a full instantiation of the pack. */
12952 register_local_specialization (inst, gen);
12954 gcc_assert (!TREE_PURPOSE (extra));
12955 extra = TREE_VALUE (extra);
12957 #if 1
12958 /* I think we should always be able to substitute dependent args into the
12959 pattern. If that turns out to be incorrect in some cases, enable the
12960 alternate code (and add complain/in_decl parms to this function). */
12961 gcc_checking_assert (!uses_template_parms (extra));
12962 #else
12963 if (!uses_template_parms (extra))
12965 gcc_unreachable ();
12966 extra = tsubst_template_args (extra, args, complain, in_decl);
12967 args = add_outermost_template_args (args, extra);
12969 else
12970 #endif
12971 args = add_to_template_args (extra, args);
12972 return args;
12975 /* Substitute ARGS into T, which is an pack expansion
12976 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
12977 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
12978 (if only a partial substitution could be performed) or
12979 ERROR_MARK_NODE if there was an error. */
12980 tree
12981 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
12982 tree in_decl)
12984 tree pattern;
12985 tree pack, packs = NULL_TREE;
12986 bool unsubstituted_packs = false;
12987 int i, len = -1;
12988 tree result;
12989 bool need_local_specializations = false;
12990 int levels;
12992 gcc_assert (PACK_EXPANSION_P (t));
12993 pattern = PACK_EXPANSION_PATTERN (t);
12995 /* Add in any args remembered from an earlier partial instantiation. */
12996 args = add_extra_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
12998 levels = TMPL_ARGS_DEPTH (args);
13000 /* Determine the argument packs that will instantiate the parameter
13001 packs used in the expansion expression. While we're at it,
13002 compute the number of arguments to be expanded and make sure it
13003 is consistent. */
13004 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
13005 pack = TREE_CHAIN (pack))
13007 tree parm_pack = TREE_VALUE (pack);
13008 tree arg_pack = NULL_TREE;
13009 tree orig_arg = NULL_TREE;
13010 int level = 0;
13012 if (TREE_CODE (parm_pack) == BASES)
13014 gcc_assert (parm_pack == pattern);
13015 if (BASES_DIRECT (parm_pack))
13016 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
13017 args, complain,
13018 in_decl, false),
13019 complain);
13020 else
13021 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
13022 args, complain, in_decl,
13023 false), complain);
13025 else if (builtin_pack_call_p (parm_pack))
13027 if (parm_pack != pattern)
13029 if (complain & tf_error)
13030 sorry ("%qE is not the entire pattern of the pack expansion",
13031 parm_pack);
13032 return error_mark_node;
13034 return expand_builtin_pack_call (parm_pack, args,
13035 complain, in_decl);
13037 else if (TREE_CODE (parm_pack) == PARM_DECL)
13039 /* We know we have correct local_specializations if this
13040 expansion is at function scope, or if we're dealing with a
13041 local parameter in a requires expression; for the latter,
13042 tsubst_requires_expr set it up appropriately. */
13043 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
13044 arg_pack = retrieve_local_specialization (parm_pack);
13045 else
13046 /* We can't rely on local_specializations for a parameter
13047 name used later in a function declaration (such as in a
13048 late-specified return type). Even if it exists, it might
13049 have the wrong value for a recursive call. */
13050 need_local_specializations = true;
13052 if (!arg_pack)
13054 /* This parameter pack was used in an unevaluated context. Just
13055 make a dummy decl, since it's only used for its type. */
13056 ++cp_unevaluated_operand;
13057 arg_pack = tsubst_decl (parm_pack, args, complain);
13058 --cp_unevaluated_operand;
13059 if (arg_pack && DECL_PACK_P (arg_pack))
13060 /* Partial instantiation of the parm_pack, we can't build
13061 up an argument pack yet. */
13062 arg_pack = NULL_TREE;
13063 else
13064 arg_pack = make_fnparm_pack (arg_pack);
13066 else if (DECL_PACK_P (arg_pack))
13067 /* This argument pack isn't fully instantiated yet. */
13068 arg_pack = NULL_TREE;
13070 else if (is_capture_proxy (parm_pack))
13072 arg_pack = retrieve_local_specialization (parm_pack);
13073 if (DECL_PACK_P (arg_pack))
13074 arg_pack = NULL_TREE;
13076 else
13078 int idx;
13079 template_parm_level_and_index (parm_pack, &level, &idx);
13080 if (level <= levels)
13081 arg_pack = TMPL_ARG (args, level, idx);
13083 if (arg_pack && TREE_CODE (arg_pack) == TEMPLATE_TYPE_PARM
13084 && TEMPLATE_TYPE_PARAMETER_PACK (arg_pack))
13085 arg_pack = NULL_TREE;
13088 orig_arg = arg_pack;
13089 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
13090 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
13092 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
13093 /* This can only happen if we forget to expand an argument
13094 pack somewhere else. Just return an error, silently. */
13096 result = make_tree_vec (1);
13097 TREE_VEC_ELT (result, 0) = error_mark_node;
13098 return result;
13101 if (arg_pack)
13103 int my_len =
13104 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
13106 /* Don't bother trying to do a partial substitution with
13107 incomplete packs; we'll try again after deduction. */
13108 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
13109 return t;
13111 if (len < 0)
13112 len = my_len;
13113 else if (len != my_len)
13115 if (!(complain & tf_error))
13116 /* Fail quietly. */;
13117 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
13118 error ("mismatched argument pack lengths while expanding %qT",
13119 pattern);
13120 else
13121 error ("mismatched argument pack lengths while expanding %qE",
13122 pattern);
13123 return error_mark_node;
13126 /* Keep track of the parameter packs and their corresponding
13127 argument packs. */
13128 packs = tree_cons (parm_pack, arg_pack, packs);
13129 TREE_TYPE (packs) = orig_arg;
13131 else
13133 /* We can't substitute for this parameter pack. We use a flag as
13134 well as the missing_level counter because function parameter
13135 packs don't have a level. */
13136 gcc_assert (processing_template_decl || is_auto (parm_pack));
13137 unsubstituted_packs = true;
13141 /* If the expansion is just T..., return the matching argument pack, unless
13142 we need to call convert_from_reference on all the elements. This is an
13143 important optimization; see c++/68422. */
13144 if (!unsubstituted_packs
13145 && TREE_PURPOSE (packs) == pattern)
13147 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
13149 /* If the argument pack is a single pack expansion, pull it out. */
13150 if (TREE_VEC_LENGTH (args) == 1
13151 && pack_expansion_args_count (args))
13152 return TREE_VEC_ELT (args, 0);
13154 /* Types need no adjustment, nor does sizeof..., and if we still have
13155 some pack expansion args we won't do anything yet. */
13156 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
13157 || PACK_EXPANSION_SIZEOF_P (t)
13158 || pack_expansion_args_count (args))
13159 return args;
13160 /* Also optimize expression pack expansions if we can tell that the
13161 elements won't have reference type. */
13162 tree type = TREE_TYPE (pattern);
13163 if (type && !TYPE_REF_P (type)
13164 && !PACK_EXPANSION_P (type)
13165 && !WILDCARD_TYPE_P (type))
13166 return args;
13167 /* Otherwise use the normal path so we get convert_from_reference. */
13170 /* We cannot expand this expansion expression, because we don't have
13171 all of the argument packs we need. */
13172 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
13174 /* We got some full packs, but we can't substitute them in until we
13175 have values for all the packs. So remember these until then. */
13177 t = make_pack_expansion (pattern, complain);
13178 PACK_EXPANSION_EXTRA_ARGS (t)
13179 = build_extra_args (pattern, args, complain);
13180 return t;
13183 /* If NEED_LOCAL_SPECIALIZATIONS then we're in a late-specified return
13184 type, so create our own local specializations map; the current map is
13185 either NULL or (in the case of recursive unification) might have
13186 bindings that we don't want to use or alter. */
13187 local_specialization_stack lss (need_local_specializations
13188 ? lss_blank : lss_nop);
13190 if (unsubstituted_packs)
13192 /* There were no real arguments, we're just replacing a parameter
13193 pack with another version of itself. Substitute into the
13194 pattern and return a PACK_EXPANSION_*. The caller will need to
13195 deal with that. */
13196 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
13197 result = tsubst_expr (pattern, args, complain, in_decl,
13198 /*integral_constant_expression_p=*/false);
13199 else
13200 result = tsubst (pattern, args, complain, in_decl);
13201 result = make_pack_expansion (result, complain);
13202 PACK_EXPANSION_LOCAL_P (result) = PACK_EXPANSION_LOCAL_P (t);
13203 PACK_EXPANSION_SIZEOF_P (result) = PACK_EXPANSION_SIZEOF_P (t);
13204 if (PACK_EXPANSION_AUTO_P (t))
13206 /* This is a fake auto... pack expansion created in add_capture with
13207 _PACKS that don't appear in the pattern. Copy one over. */
13208 packs = PACK_EXPANSION_PARAMETER_PACKS (t);
13209 pack = retrieve_local_specialization (TREE_VALUE (packs));
13210 gcc_checking_assert (DECL_PACK_P (pack));
13211 PACK_EXPANSION_PARAMETER_PACKS (result)
13212 = build_tree_list (NULL_TREE, pack);
13213 PACK_EXPANSION_AUTO_P (result) = true;
13215 return result;
13218 gcc_assert (len >= 0);
13220 /* For each argument in each argument pack, substitute into the
13221 pattern. */
13222 result = make_tree_vec (len);
13223 tree elem_args = copy_template_args (args);
13224 for (i = 0; i < len; ++i)
13226 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
13228 elem_args, complain,
13229 in_decl);
13230 TREE_VEC_ELT (result, i) = t;
13231 if (t == error_mark_node)
13233 result = error_mark_node;
13234 break;
13238 /* Update ARGS to restore the substitution from parameter packs to
13239 their argument packs. */
13240 for (pack = packs; pack; pack = TREE_CHAIN (pack))
13242 tree parm = TREE_PURPOSE (pack);
13244 if (TREE_CODE (parm) == PARM_DECL
13245 || VAR_P (parm)
13246 || TREE_CODE (parm) == FIELD_DECL)
13247 register_local_specialization (TREE_TYPE (pack), parm);
13248 else
13250 int idx, level;
13252 if (TREE_VALUE (pack) == NULL_TREE)
13253 continue;
13255 template_parm_level_and_index (parm, &level, &idx);
13257 /* Update the corresponding argument. */
13258 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
13259 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
13260 TREE_TYPE (pack);
13261 else
13262 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
13266 /* If the dependent pack arguments were such that we end up with only a
13267 single pack expansion again, there's no need to keep it in a TREE_VEC. */
13268 if (len == 1 && TREE_CODE (result) == TREE_VEC
13269 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
13270 return TREE_VEC_ELT (result, 0);
13272 return result;
13275 /* Make an argument pack out of the TREE_VEC VEC. */
13277 static tree
13278 make_argument_pack (tree vec)
13280 tree pack;
13282 if (TYPE_P (TREE_VEC_ELT (vec, 0)))
13283 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
13284 else
13286 pack = make_node (NONTYPE_ARGUMENT_PACK);
13287 TREE_CONSTANT (pack) = 1;
13289 SET_ARGUMENT_PACK_ARGS (pack, vec);
13290 return pack;
13293 /* Return an exact copy of template args T that can be modified
13294 independently. */
13296 static tree
13297 copy_template_args (tree t)
13299 if (t == error_mark_node)
13300 return t;
13302 int len = TREE_VEC_LENGTH (t);
13303 tree new_vec = make_tree_vec (len);
13305 for (int i = 0; i < len; ++i)
13307 tree elt = TREE_VEC_ELT (t, i);
13308 if (elt && TREE_CODE (elt) == TREE_VEC)
13309 elt = copy_template_args (elt);
13310 TREE_VEC_ELT (new_vec, i) = elt;
13313 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
13314 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
13316 return new_vec;
13319 /* Substitute ARGS into the *_ARGUMENT_PACK orig_arg. */
13321 tree
13322 tsubst_argument_pack (tree orig_arg, tree args, tsubst_flags_t complain,
13323 tree in_decl)
13325 /* Substitute into each of the arguments. */
13326 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
13327 args, complain, in_decl);
13328 tree new_arg = error_mark_node;
13329 if (pack_args != error_mark_node)
13331 if (TYPE_P (orig_arg))
13333 new_arg = cxx_make_type (TREE_CODE (orig_arg));
13334 SET_TYPE_STRUCTURAL_EQUALITY (new_arg);
13336 else
13338 new_arg = make_node (TREE_CODE (orig_arg));
13339 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
13342 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
13345 return new_arg;
13348 /* Substitute ARGS into the vector or list of template arguments T. */
13350 tree
13351 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13353 tree orig_t = t;
13354 int len, need_new = 0, i, expanded_len_adjust = 0, out;
13355 tree *elts;
13357 if (t == error_mark_node)
13358 return error_mark_node;
13360 len = TREE_VEC_LENGTH (t);
13361 elts = XALLOCAVEC (tree, len);
13363 for (i = 0; i < len; i++)
13365 tree orig_arg = TREE_VEC_ELT (t, i);
13366 tree new_arg;
13368 if (TREE_CODE (orig_arg) == TREE_VEC)
13369 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
13370 else if (PACK_EXPANSION_P (orig_arg))
13372 /* Substitute into an expansion expression. */
13373 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
13375 if (TREE_CODE (new_arg) == TREE_VEC)
13376 /* Add to the expanded length adjustment the number of
13377 expanded arguments. We subtract one from this
13378 measurement, because the argument pack expression
13379 itself is already counted as 1 in
13380 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
13381 the argument pack is empty. */
13382 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
13384 else if (ARGUMENT_PACK_P (orig_arg))
13385 new_arg = tsubst_argument_pack (orig_arg, args, complain, in_decl);
13386 else
13387 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
13389 if (new_arg == error_mark_node)
13390 return error_mark_node;
13392 elts[i] = new_arg;
13393 if (new_arg != orig_arg)
13394 need_new = 1;
13397 if (!need_new)
13398 return t;
13400 /* Make space for the expanded arguments coming from template
13401 argument packs. */
13402 t = make_tree_vec (len + expanded_len_adjust);
13403 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
13404 arguments for a member template.
13405 In that case each TREE_VEC in ORIG_T represents a level of template
13406 arguments, and ORIG_T won't carry any non defaulted argument count.
13407 It will rather be the nested TREE_VECs that will carry one.
13408 In other words, ORIG_T carries a non defaulted argument count only
13409 if it doesn't contain any nested TREE_VEC. */
13410 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
13412 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
13413 count += expanded_len_adjust;
13414 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
13416 for (i = 0, out = 0; i < len; i++)
13418 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
13419 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
13420 && TREE_CODE (elts[i]) == TREE_VEC)
13422 int idx;
13424 /* Now expand the template argument pack "in place". */
13425 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
13426 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
13428 else
13430 TREE_VEC_ELT (t, out) = elts[i];
13431 out++;
13435 return t;
13438 /* Substitute ARGS into one level PARMS of template parameters. */
13440 static tree
13441 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
13443 if (parms == error_mark_node)
13444 return error_mark_node;
13446 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
13448 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
13450 tree tuple = TREE_VEC_ELT (parms, i);
13452 if (tuple == error_mark_node)
13453 continue;
13455 TREE_VEC_ELT (new_vec, i) =
13456 tsubst_template_parm (tuple, args, complain);
13459 return new_vec;
13462 /* Return the result of substituting ARGS into the template parameters
13463 given by PARMS. If there are m levels of ARGS and m + n levels of
13464 PARMS, then the result will contain n levels of PARMS. For
13465 example, if PARMS is `template <class T> template <class U>
13466 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
13467 result will be `template <int*, double, class V>'. */
13469 static tree
13470 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
13472 tree r = NULL_TREE;
13473 tree* new_parms;
13475 /* When substituting into a template, we must set
13476 PROCESSING_TEMPLATE_DECL as the template parameters may be
13477 dependent if they are based on one-another, and the dependency
13478 predicates are short-circuit outside of templates. */
13479 ++processing_template_decl;
13481 for (new_parms = &r;
13482 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
13483 new_parms = &(TREE_CHAIN (*new_parms)),
13484 parms = TREE_CHAIN (parms))
13486 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
13487 args, complain);
13488 *new_parms =
13489 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
13490 - TMPL_ARGS_DEPTH (args)),
13491 new_vec, NULL_TREE);
13492 TEMPLATE_PARMS_CONSTRAINTS (*new_parms)
13493 = TEMPLATE_PARMS_CONSTRAINTS (parms);
13496 --processing_template_decl;
13498 return r;
13501 /* Return the result of substituting ARGS into one template parameter
13502 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
13503 parameter and which TREE_PURPOSE is the default argument of the
13504 template parameter. */
13506 static tree
13507 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
13509 tree default_value, parm_decl;
13511 if (args == NULL_TREE
13512 || t == NULL_TREE
13513 || t == error_mark_node)
13514 return t;
13516 gcc_assert (TREE_CODE (t) == TREE_LIST);
13518 default_value = TREE_PURPOSE (t);
13519 parm_decl = TREE_VALUE (t);
13520 tree constraint = TEMPLATE_PARM_CONSTRAINTS (t);
13522 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
13523 if (TREE_CODE (parm_decl) == PARM_DECL
13524 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
13525 parm_decl = error_mark_node;
13526 default_value = tsubst_template_arg (default_value, args,
13527 complain, NULL_TREE);
13528 constraint = tsubst_constraint (constraint, args, complain, NULL_TREE);
13530 tree r = build_tree_list (default_value, parm_decl);
13531 TEMPLATE_PARM_CONSTRAINTS (r) = constraint;
13532 return r;
13535 /* Substitute the ARGS into the indicated aggregate (or enumeration)
13536 type T. If T is not an aggregate or enumeration type, it is
13537 handled as if by tsubst. IN_DECL is as for tsubst. If
13538 ENTERING_SCOPE is nonzero, T is the context for a template which
13539 we are presently tsubst'ing. Return the substituted value. */
13541 static tree
13542 tsubst_aggr_type (tree t,
13543 tree args,
13544 tsubst_flags_t complain,
13545 tree in_decl,
13546 int entering_scope)
13548 if (t == NULL_TREE)
13549 return NULL_TREE;
13551 switch (TREE_CODE (t))
13553 case RECORD_TYPE:
13554 if (TYPE_PTRMEMFUNC_P (t))
13555 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
13557 /* Fall through. */
13558 case ENUMERAL_TYPE:
13559 case UNION_TYPE:
13560 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
13562 tree argvec;
13563 tree context;
13564 tree r;
13566 /* In "sizeof(X<I>)" we need to evaluate "I". */
13567 cp_evaluated ev;
13569 /* First, determine the context for the type we are looking
13570 up. */
13571 context = TYPE_CONTEXT (t);
13572 if (context && TYPE_P (context))
13574 context = tsubst_aggr_type (context, args, complain,
13575 in_decl, /*entering_scope=*/1);
13576 /* If context is a nested class inside a class template,
13577 it may still need to be instantiated (c++/33959). */
13578 context = complete_type (context);
13581 /* Then, figure out what arguments are appropriate for the
13582 type we are trying to find. For example, given:
13584 template <class T> struct S;
13585 template <class T, class U> void f(T, U) { S<U> su; }
13587 and supposing that we are instantiating f<int, double>,
13588 then our ARGS will be {int, double}, but, when looking up
13589 S we only want {double}. */
13590 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
13591 complain, in_decl);
13592 if (argvec == error_mark_node)
13593 r = error_mark_node;
13594 else if (!entering_scope
13595 && cxx_dialect >= cxx17 && dependent_scope_p (context))
13597 /* See maybe_dependent_member_ref. */
13598 tree name = TYPE_IDENTIFIER (t);
13599 tree fullname = name;
13600 if (instantiates_primary_template_p (t))
13601 fullname = build_nt (TEMPLATE_ID_EXPR, name,
13602 INNERMOST_TEMPLATE_ARGS (argvec));
13603 return build_typename_type (context, name, fullname,
13604 typename_type);
13606 else
13608 r = lookup_template_class (t, argvec, in_decl, context,
13609 entering_scope, complain);
13610 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
13613 return r;
13615 else
13616 /* This is not a template type, so there's nothing to do. */
13617 return t;
13619 default:
13620 return tsubst (t, args, complain, in_decl);
13624 static GTY((cache)) decl_tree_cache_map *defarg_inst;
13626 /* Substitute into the default argument ARG (a default argument for
13627 FN), which has the indicated TYPE. */
13629 tree
13630 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
13631 tsubst_flags_t complain)
13633 int errs = errorcount + sorrycount;
13635 /* This can happen in invalid code. */
13636 if (TREE_CODE (arg) == DEFERRED_PARSE)
13637 return arg;
13639 /* Shortcut {}. */
13640 if (BRACE_ENCLOSED_INITIALIZER_P (arg)
13641 && CONSTRUCTOR_NELTS (arg) == 0)
13642 return arg;
13644 tree parm = FUNCTION_FIRST_USER_PARM (fn);
13645 parm = chain_index (parmnum, parm);
13646 tree parmtype = TREE_TYPE (parm);
13647 if (DECL_BY_REFERENCE (parm))
13648 parmtype = TREE_TYPE (parmtype);
13649 if (parmtype == error_mark_node)
13650 return error_mark_node;
13652 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
13654 tree *slot;
13655 if (defarg_inst && (slot = defarg_inst->get (parm)))
13656 return *slot;
13658 /* This default argument came from a template. Instantiate the
13659 default argument here, not in tsubst. In the case of
13660 something like:
13662 template <class T>
13663 struct S {
13664 static T t();
13665 void f(T = t());
13668 we must be careful to do name lookup in the scope of S<T>,
13669 rather than in the current class. */
13670 push_to_top_level ();
13671 push_access_scope (fn);
13672 push_deferring_access_checks (dk_no_deferred);
13673 start_lambda_scope (parm);
13675 /* The default argument expression may cause implicitly defined
13676 member functions to be synthesized, which will result in garbage
13677 collection. We must treat this situation as if we were within
13678 the body of function so as to avoid collecting live data on the
13679 stack. */
13680 ++function_depth;
13681 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
13682 complain, NULL_TREE,
13683 /*integral_constant_expression_p=*/false);
13684 --function_depth;
13686 finish_lambda_scope ();
13688 /* Make sure the default argument is reasonable. */
13689 arg = check_default_argument (type, arg, complain);
13691 if (errorcount+sorrycount > errs
13692 && (complain & tf_warning_or_error))
13693 inform (input_location,
13694 " when instantiating default argument for call to %qD", fn);
13696 pop_deferring_access_checks ();
13697 pop_access_scope (fn);
13698 pop_from_top_level ();
13700 if (arg != error_mark_node && !cp_unevaluated_operand)
13702 if (!defarg_inst)
13703 defarg_inst = decl_tree_cache_map::create_ggc (37);
13704 defarg_inst->put (parm, arg);
13707 return arg;
13710 /* Substitute into all the default arguments for FN. */
13712 static void
13713 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
13715 tree arg;
13716 tree tmpl_args;
13718 tmpl_args = DECL_TI_ARGS (fn);
13720 /* If this function is not yet instantiated, we certainly don't need
13721 its default arguments. */
13722 if (uses_template_parms (tmpl_args))
13723 return;
13724 /* Don't do this again for clones. */
13725 if (DECL_CLONED_FUNCTION_P (fn))
13726 return;
13728 int i = 0;
13729 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
13730 arg;
13731 arg = TREE_CHAIN (arg), ++i)
13732 if (TREE_PURPOSE (arg))
13733 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
13734 TREE_VALUE (arg),
13735 TREE_PURPOSE (arg),
13736 complain);
13739 /* Hash table mapping a FUNCTION_DECL to its dependent explicit-specifier. */
13740 static GTY((cache)) decl_tree_cache_map *explicit_specifier_map;
13742 /* Store a pair to EXPLICIT_SPECIFIER_MAP. */
13744 void
13745 store_explicit_specifier (tree v, tree t)
13747 if (!explicit_specifier_map)
13748 explicit_specifier_map = decl_tree_cache_map::create_ggc (37);
13749 DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (v) = true;
13750 explicit_specifier_map->put (v, t);
13753 /* Lookup an element in EXPLICIT_SPECIFIER_MAP. */
13755 static tree
13756 lookup_explicit_specifier (tree v)
13758 return *explicit_specifier_map->get (v);
13761 /* Given T, a FUNCTION_TYPE or METHOD_TYPE, construct and return a corresponding
13762 FUNCTION_TYPE or METHOD_TYPE whose return type is RETURN_TYPE, argument types
13763 are ARG_TYPES, and exception specification is RAISES, and otherwise is
13764 identical to T. */
13766 static tree
13767 rebuild_function_or_method_type (tree t, tree return_type, tree arg_types,
13768 tree raises, tsubst_flags_t complain)
13770 gcc_assert (FUNC_OR_METHOD_TYPE_P (t));
13772 tree new_type;
13773 if (TREE_CODE (t) == FUNCTION_TYPE)
13775 new_type = build_function_type (return_type, arg_types);
13776 new_type = apply_memfn_quals (new_type, type_memfn_quals (t));
13778 else
13780 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13781 /* Don't pick up extra function qualifiers from the basetype. */
13782 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13783 if (! MAYBE_CLASS_TYPE_P (r))
13785 /* [temp.deduct]
13787 Type deduction may fail for any of the following
13788 reasons:
13790 -- Attempting to create "pointer to member of T" when T
13791 is not a class type. */
13792 if (complain & tf_error)
13793 error ("creating pointer to member function of non-class type %qT",
13795 return error_mark_node;
13798 new_type = build_method_type_directly (r, return_type,
13799 TREE_CHAIN (arg_types));
13801 new_type = cp_build_type_attribute_variant (new_type, TYPE_ATTRIBUTES (t));
13803 cp_ref_qualifier rqual = type_memfn_rqual (t);
13804 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13805 return build_cp_fntype_variant (new_type, rqual, raises, late_return_type_p);
13808 /* Check if the function type of DECL, a FUNCTION_DECL, agrees with the type of
13809 each of its formal parameters. If there is a disagreement then rebuild
13810 DECL's function type according to its formal parameter types, as part of a
13811 resolution for Core issues 1001/1322. */
13813 static void
13814 maybe_rebuild_function_decl_type (tree decl)
13816 bool function_type_needs_rebuilding = false;
13817 if (tree parm_list = FUNCTION_FIRST_USER_PARM (decl))
13819 tree parm_type_list = FUNCTION_FIRST_USER_PARMTYPE (decl);
13820 while (parm_type_list && parm_type_list != void_list_node)
13822 tree parm_type = TREE_VALUE (parm_type_list);
13823 tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
13824 if (!same_type_p (parm_type, formal_parm_type_unqual))
13826 function_type_needs_rebuilding = true;
13827 break;
13830 parm_list = DECL_CHAIN (parm_list);
13831 parm_type_list = TREE_CHAIN (parm_type_list);
13835 if (!function_type_needs_rebuilding)
13836 return;
13838 const tree fntype = TREE_TYPE (decl);
13839 tree parm_list = DECL_ARGUMENTS (decl);
13840 tree old_parm_type_list = TYPE_ARG_TYPES (fntype);
13841 tree new_parm_type_list = NULL_TREE;
13842 tree *q = &new_parm_type_list;
13843 for (int skip = num_artificial_parms_for (decl); skip > 0; skip--)
13845 *q = copy_node (old_parm_type_list);
13846 parm_list = DECL_CHAIN (parm_list);
13847 old_parm_type_list = TREE_CHAIN (old_parm_type_list);
13848 q = &TREE_CHAIN (*q);
13850 while (old_parm_type_list && old_parm_type_list != void_list_node)
13852 *q = copy_node (old_parm_type_list);
13853 tree *new_parm_type = &TREE_VALUE (*q);
13854 tree formal_parm_type_unqual = strip_top_quals (TREE_TYPE (parm_list));
13855 if (!same_type_p (*new_parm_type, formal_parm_type_unqual))
13856 *new_parm_type = formal_parm_type_unqual;
13858 parm_list = DECL_CHAIN (parm_list);
13859 old_parm_type_list = TREE_CHAIN (old_parm_type_list);
13860 q = &TREE_CHAIN (*q);
13862 if (old_parm_type_list == void_list_node)
13863 *q = void_list_node;
13865 TREE_TYPE (decl)
13866 = rebuild_function_or_method_type (fntype,
13867 TREE_TYPE (fntype), new_parm_type_list,
13868 TYPE_RAISES_EXCEPTIONS (fntype), tf_none);
13871 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
13873 static tree
13874 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
13875 tree lambda_fntype)
13877 tree gen_tmpl = NULL_TREE, argvec = NULL_TREE;
13878 hashval_t hash = 0;
13879 tree in_decl = t;
13881 /* Nobody should be tsubst'ing into non-template functions. */
13882 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE
13883 || DECL_LOCAL_DECL_P (t));
13885 if (DECL_LOCAL_DECL_P (t))
13887 if (tree spec = retrieve_local_specialization (t))
13888 return spec;
13890 else if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
13892 /* If T is not dependent, just return it. */
13893 if (!uses_template_parms (DECL_TI_ARGS (t))
13894 && !LAMBDA_FUNCTION_P (t))
13895 return t;
13897 /* Calculate the most general template of which R is a
13898 specialization. */
13899 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
13901 /* We're substituting a lambda function under tsubst_lambda_expr but not
13902 directly from it; find the matching function we're already inside.
13903 But don't do this if T is a generic lambda with a single level of
13904 template parms, as in that case we're doing a normal instantiation. */
13905 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
13906 && (!generic_lambda_fn_p (t)
13907 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
13908 return enclosing_instantiation_of (t);
13910 /* Calculate the complete set of arguments used to
13911 specialize R. */
13912 argvec = tsubst_template_args (DECL_TI_ARGS
13913 (DECL_TEMPLATE_RESULT
13914 (DECL_TI_TEMPLATE (t))),
13915 args, complain, in_decl);
13916 if (argvec == error_mark_node)
13917 return error_mark_node;
13919 /* Check to see if we already have this specialization. */
13920 if (!lambda_fntype)
13922 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13923 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
13924 return spec;
13927 else
13929 /* This special case arises when we have something like this:
13931 template <class T> struct S {
13932 friend void f<int>(int, double);
13935 Here, the DECL_TI_TEMPLATE for the friend declaration
13936 will be an IDENTIFIER_NODE. We are being called from
13937 tsubst_friend_function, and we want only to create a
13938 new decl (R) with appropriate types so that we can call
13939 determine_specialization. */
13940 gen_tmpl = NULL_TREE;
13941 argvec = NULL_TREE;
13944 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
13945 : NULL_TREE);
13946 tree ctx = closure ? closure : DECL_CONTEXT (t);
13947 bool member = ctx && TYPE_P (ctx);
13949 if (member && !closure)
13950 ctx = tsubst_aggr_type (ctx, args,
13951 complain, t, /*entering_scope=*/1);
13953 tree type = (lambda_fntype ? lambda_fntype
13954 : tsubst (TREE_TYPE (t), args,
13955 complain | tf_fndecl_type, in_decl));
13956 if (type == error_mark_node)
13957 return error_mark_node;
13959 /* If we hit excessive deduction depth, the type is bogus even if
13960 it isn't error_mark_node, so don't build a decl. */
13961 if (excessive_deduction_depth)
13962 return error_mark_node;
13964 /* We do NOT check for matching decls pushed separately at this
13965 point, as they may not represent instantiations of this
13966 template, and in any case are considered separate under the
13967 discrete model. */
13968 tree r = copy_decl (t);
13969 DECL_USE_TEMPLATE (r) = 0;
13970 TREE_TYPE (r) = type;
13971 /* Clear out the mangled name and RTL for the instantiation. */
13972 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13973 SET_DECL_RTL (r, NULL);
13974 /* Leave DECL_INITIAL set on deleted instantiations. */
13975 if (!DECL_DELETED_FN (r))
13976 DECL_INITIAL (r) = NULL_TREE;
13977 DECL_CONTEXT (r) = ctx;
13978 set_instantiating_module (r);
13980 /* Handle explicit(dependent-expr). */
13981 if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
13983 tree spec = lookup_explicit_specifier (t);
13984 spec = tsubst_copy_and_build (spec, args, complain, in_decl,
13985 /*function_p=*/false,
13986 /*i_c_e_p=*/true);
13987 spec = build_explicit_specifier (spec, complain);
13988 DECL_NONCONVERTING_P (r) = (spec == boolean_true_node);
13991 /* OpenMP UDRs have the only argument a reference to the declared
13992 type. We want to diagnose if the declared type is a reference,
13993 which is invalid, but as references to references are usually
13994 quietly merged, diagnose it here. */
13995 if (DECL_OMP_DECLARE_REDUCTION_P (t))
13997 tree argtype
13998 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
13999 argtype = tsubst (argtype, args, complain, in_decl);
14000 if (TYPE_REF_P (argtype))
14001 error_at (DECL_SOURCE_LOCATION (t),
14002 "reference type %qT in "
14003 "%<#pragma omp declare reduction%>", argtype);
14004 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
14005 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
14006 argtype);
14009 if (member && DECL_CONV_FN_P (r))
14010 /* Type-conversion operator. Reconstruct the name, in
14011 case it's the name of one of the template's parameters. */
14012 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
14014 tree parms = DECL_ARGUMENTS (t);
14015 if (closure)
14016 parms = DECL_CHAIN (parms);
14017 parms = tsubst (parms, args, complain, t);
14018 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
14019 DECL_CONTEXT (parm) = r;
14020 if (closure)
14022 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
14023 DECL_NAME (tparm) = closure_identifier;
14024 DECL_CHAIN (tparm) = parms;
14025 parms = tparm;
14027 DECL_ARGUMENTS (r) = parms;
14028 DECL_RESULT (r) = NULL_TREE;
14030 maybe_rebuild_function_decl_type (r);
14032 TREE_STATIC (r) = 0;
14033 TREE_PUBLIC (r) = TREE_PUBLIC (t);
14034 DECL_EXTERNAL (r) = 1;
14035 /* If this is an instantiation of a function with internal
14036 linkage, we already know what object file linkage will be
14037 assigned to the instantiation. */
14038 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
14039 DECL_DEFER_OUTPUT (r) = 0;
14040 DECL_CHAIN (r) = NULL_TREE;
14041 DECL_PENDING_INLINE_INFO (r) = 0;
14042 DECL_PENDING_INLINE_P (r) = 0;
14043 DECL_SAVED_TREE (r) = NULL_TREE;
14044 DECL_STRUCT_FUNCTION (r) = NULL;
14045 TREE_USED (r) = 0;
14046 /* We'll re-clone as appropriate in instantiate_template. */
14047 DECL_CLONED_FUNCTION (r) = NULL_TREE;
14049 /* If we aren't complaining now, return on error before we register
14050 the specialization so that we'll complain eventually. */
14051 if ((complain & tf_error) == 0
14052 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
14053 && !grok_op_properties (r, /*complain=*/false))
14054 return error_mark_node;
14056 /* Associate the constraints directly with the instantiation. We
14057 don't substitute through the constraints; that's only done when
14058 they are checked. */
14059 if (tree ci = get_constraints (t))
14060 set_constraints (r, ci);
14062 if (DECL_FRIEND_CONTEXT (t))
14063 SET_DECL_FRIEND_CONTEXT (r,
14064 tsubst (DECL_FRIEND_CONTEXT (t),
14065 args, complain, in_decl));
14067 if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14068 args, complain, in_decl))
14069 return error_mark_node;
14071 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
14072 this in the special friend case mentioned above where
14073 GEN_TMPL is NULL. */
14074 if (gen_tmpl && !closure)
14076 DECL_TEMPLATE_INFO (r)
14077 = build_template_info (gen_tmpl, argvec);
14078 SET_DECL_IMPLICIT_INSTANTIATION (r);
14080 tree new_r
14081 = register_specialization (r, gen_tmpl, argvec, false, hash);
14082 if (new_r != r)
14083 /* We instantiated this while substituting into
14084 the type earlier (template/friend54.C). */
14085 return new_r;
14087 /* We're not supposed to instantiate default arguments
14088 until they are called, for a template. But, for a
14089 declaration like:
14091 template <class T> void f ()
14092 { extern void g(int i = T()); }
14094 we should do the substitution when the template is
14095 instantiated. We handle the member function case in
14096 instantiate_class_template since the default arguments
14097 might refer to other members of the class. */
14098 if (!member
14099 && !PRIMARY_TEMPLATE_P (gen_tmpl)
14100 && !uses_template_parms (argvec))
14101 tsubst_default_arguments (r, complain);
14103 else if (DECL_LOCAL_DECL_P (r))
14105 if (!cp_unevaluated_operand)
14106 register_local_specialization (r, t);
14108 else
14109 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14111 /* Copy the list of befriending classes. */
14112 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
14113 *friends;
14114 friends = &TREE_CHAIN (*friends))
14116 *friends = copy_node (*friends);
14117 TREE_VALUE (*friends)
14118 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
14121 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
14123 maybe_retrofit_in_chrg (r);
14124 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
14125 return error_mark_node;
14126 /* If this is an instantiation of a member template, clone it.
14127 If it isn't, that'll be handled by
14128 clone_constructors_and_destructors. */
14129 if (PRIMARY_TEMPLATE_P (gen_tmpl))
14130 clone_cdtor (r, /*update_methods=*/false);
14132 else if ((complain & tf_error) != 0
14133 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
14134 && !grok_op_properties (r, /*complain=*/true))
14135 return error_mark_node;
14137 /* Possibly limit visibility based on template args. */
14138 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
14139 if (DECL_VISIBILITY_SPECIFIED (t))
14141 DECL_VISIBILITY_SPECIFIED (r) = 0;
14142 DECL_ATTRIBUTES (r)
14143 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
14145 determine_visibility (r);
14146 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
14147 && !processing_template_decl)
14148 defaulted_late_check (r);
14150 if (flag_openmp)
14151 if (tree attr = lookup_attribute ("omp declare variant base",
14152 DECL_ATTRIBUTES (r)))
14153 omp_declare_variant_finalize (r, attr);
14155 return r;
14158 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
14160 static tree
14161 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
14162 tree lambda_fntype)
14164 /* We can get here when processing a member function template,
14165 member class template, or template template parameter. */
14166 tree decl = DECL_TEMPLATE_RESULT (t);
14167 tree in_decl = t;
14168 tree spec;
14169 tree tmpl_args;
14170 tree full_args;
14171 tree r;
14172 hashval_t hash = 0;
14174 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
14176 /* Template template parameter is treated here. */
14177 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14178 if (new_type == error_mark_node)
14179 r = error_mark_node;
14180 /* If we get a real template back, return it. This can happen in
14181 the context of most_specialized_partial_spec. */
14182 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
14183 r = new_type;
14184 else
14185 /* The new TEMPLATE_DECL was built in
14186 reduce_template_parm_level. */
14187 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
14188 return r;
14191 if (!lambda_fntype)
14193 /* We might already have an instance of this template.
14194 The ARGS are for the surrounding class type, so the
14195 full args contain the tsubst'd args for the context,
14196 plus the innermost args from the template decl. */
14197 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
14198 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
14199 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
14200 /* Because this is a template, the arguments will still be
14201 dependent, even after substitution. If
14202 PROCESSING_TEMPLATE_DECL is not set, the dependency
14203 predicates will short-circuit. */
14204 ++processing_template_decl;
14205 full_args = tsubst_template_args (tmpl_args, args,
14206 complain, in_decl);
14207 --processing_template_decl;
14208 if (full_args == error_mark_node)
14209 return error_mark_node;
14211 /* If this is a default template template argument,
14212 tsubst might not have changed anything. */
14213 if (full_args == tmpl_args)
14214 return t;
14216 hash = hash_tmpl_and_args (t, full_args);
14217 spec = retrieve_specialization (t, full_args, hash);
14218 if (spec != NULL_TREE)
14220 if (TYPE_P (spec))
14221 /* Type partial instantiations are stored as the type by
14222 lookup_template_class_1, not here as the template. */
14223 spec = CLASSTYPE_TI_TEMPLATE (spec);
14224 return spec;
14228 /* Make a new template decl. It will be similar to the
14229 original, but will record the current template arguments.
14230 We also create a new function declaration, which is just
14231 like the old one, but points to this new template, rather
14232 than the old one. */
14233 r = copy_decl (t);
14234 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
14235 DECL_CHAIN (r) = NULL_TREE;
14237 // Build new template info linking to the original template decl.
14238 if (!lambda_fntype)
14240 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
14241 SET_DECL_IMPLICIT_INSTANTIATION (r);
14243 else
14244 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14246 /* The template parameters for this new template are all the
14247 template parameters for the old template, except the
14248 outermost level of parameters. */
14249 auto tparm_guard = make_temp_override (current_template_parms);
14250 DECL_TEMPLATE_PARMS (r)
14251 = current_template_parms
14252 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
14253 complain);
14255 bool class_p = false;
14256 tree inner = decl;
14257 ++processing_template_decl;
14258 if (TREE_CODE (inner) == FUNCTION_DECL)
14259 inner = tsubst_function_decl (inner, args, complain, lambda_fntype);
14260 else
14262 if (TREE_CODE (inner) == TYPE_DECL && !TYPE_DECL_ALIAS_P (inner))
14264 class_p = true;
14265 inner = TREE_TYPE (inner);
14267 if (class_p)
14268 inner = tsubst_aggr_type (inner, args, complain,
14269 in_decl, /*entering*/1);
14270 else
14271 inner = tsubst (inner, args, complain, in_decl);
14273 --processing_template_decl;
14274 if (inner == error_mark_node)
14275 return error_mark_node;
14277 if (class_p)
14279 /* For a partial specialization, we need to keep pointing to
14280 the primary template. */
14281 if (!DECL_TEMPLATE_SPECIALIZATION (t))
14282 CLASSTYPE_TI_TEMPLATE (inner) = r;
14284 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (inner);
14285 inner = TYPE_MAIN_DECL (inner);
14287 else if (lambda_fntype)
14289 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
14290 DECL_TEMPLATE_INFO (inner) = build_template_info (r, args);
14292 else
14294 DECL_TI_TEMPLATE (inner) = r;
14295 DECL_TI_ARGS (r) = DECL_TI_ARGS (inner);
14298 DECL_TEMPLATE_RESULT (r) = inner;
14299 TREE_TYPE (r) = TREE_TYPE (inner);
14300 DECL_CONTEXT (r) = DECL_CONTEXT (inner);
14302 if (modules_p ())
14304 /* Propagate module information from the decl. */
14305 DECL_MODULE_EXPORT_P (r) = DECL_MODULE_EXPORT_P (inner);
14306 if (DECL_LANG_SPECIFIC (inner))
14307 /* If this is a constrained template, the above tsubst of
14308 inner can find the unconstrained template, which may have
14309 come from an import. This is ok, because we don't
14310 register this instantiation (see below). */
14311 gcc_checking_assert (!DECL_MODULE_IMPORT_P (inner)
14312 || (TEMPLATE_PARMS_CONSTRAINTS
14313 (DECL_TEMPLATE_PARMS (t))));
14316 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
14317 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
14319 if (PRIMARY_TEMPLATE_P (t))
14320 DECL_PRIMARY_TEMPLATE (r) = r;
14322 if (TREE_CODE (decl) == FUNCTION_DECL && !lambda_fntype)
14323 /* Record this non-type partial instantiation. */
14324 register_specialization (r, t,
14325 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
14326 false, hash);
14328 return r;
14331 /* True if FN is the op() for a lambda in an uninstantiated template. */
14333 bool
14334 lambda_fn_in_template_p (tree fn)
14336 if (!fn || !LAMBDA_FUNCTION_P (fn))
14337 return false;
14338 tree closure = DECL_CONTEXT (fn);
14339 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
14342 /* True if FN is the substitution (via tsubst_lambda_expr) of a function for
14343 which the above is true. */
14345 bool
14346 regenerated_lambda_fn_p (tree fn)
14348 if (!fn || !LAMBDA_FUNCTION_P (fn))
14349 return false;
14350 tree closure = DECL_CONTEXT (fn);
14351 tree lam = CLASSTYPE_LAMBDA_EXPR (closure);
14352 return LAMBDA_EXPR_REGEN_INFO (lam) != NULL_TREE;
14355 /* Return the LAMBDA_EXPR from which T was ultimately regenerated.
14356 If T is not a regenerated LAMBDA_EXPR, return T. */
14358 tree
14359 most_general_lambda (tree t)
14361 while (tree ti = LAMBDA_EXPR_REGEN_INFO (t))
14362 t = TI_TEMPLATE (ti);
14363 return t;
14366 /* We're instantiating a variable from template function TCTX. Return the
14367 corresponding current enclosing scope. We can match them up using
14368 DECL_SOURCE_LOCATION because lambdas only ever have one source location, and
14369 the DECL_SOURCE_LOCATION for a function instantiation is updated to match
14370 the template definition in regenerate_decl_from_template. */
14372 static tree
14373 enclosing_instantiation_of (tree tctx)
14375 tree fn = current_function_decl;
14377 /* We shouldn't ever need to do this for other artificial functions. */
14378 gcc_assert (!DECL_ARTIFICIAL (tctx) || LAMBDA_FUNCTION_P (tctx));
14380 for (; fn; fn = decl_function_context (fn))
14381 if (DECL_SOURCE_LOCATION (fn) == DECL_SOURCE_LOCATION (tctx))
14382 return fn;
14383 gcc_unreachable ();
14386 /* Substitute the ARGS into the T, which is a _DECL. Return the
14387 result of the substitution. Issue error and warning messages under
14388 control of COMPLAIN. */
14390 static tree
14391 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
14393 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
14394 location_t saved_loc;
14395 tree r = NULL_TREE;
14396 tree in_decl = t;
14397 hashval_t hash = 0;
14399 /* Set the filename and linenumber to improve error-reporting. */
14400 saved_loc = input_location;
14401 input_location = DECL_SOURCE_LOCATION (t);
14403 switch (TREE_CODE (t))
14405 case TEMPLATE_DECL:
14406 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
14407 break;
14409 case FUNCTION_DECL:
14410 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
14411 break;
14413 case PARM_DECL:
14415 tree type = NULL_TREE;
14416 int i, len = 1;
14417 tree expanded_types = NULL_TREE;
14418 tree prev_r = NULL_TREE;
14419 tree first_r = NULL_TREE;
14421 if (DECL_PACK_P (t))
14423 /* If there is a local specialization that isn't a
14424 parameter pack, it means that we're doing a "simple"
14425 substitution from inside tsubst_pack_expansion. Just
14426 return the local specialization (which will be a single
14427 parm). */
14428 tree spec = retrieve_local_specialization (t);
14429 if (spec
14430 && TREE_CODE (spec) == PARM_DECL
14431 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
14432 RETURN (spec);
14434 /* Expand the TYPE_PACK_EXPANSION that provides the types for
14435 the parameters in this function parameter pack. */
14436 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
14437 complain, in_decl);
14438 if (TREE_CODE (expanded_types) == TREE_VEC)
14440 len = TREE_VEC_LENGTH (expanded_types);
14442 /* Zero-length parameter packs are boring. Just substitute
14443 into the chain. */
14444 if (len == 0 && !cp_unevaluated_operand)
14445 RETURN (tsubst (TREE_CHAIN (t), args, complain,
14446 TREE_CHAIN (t)));
14448 else
14450 /* All we did was update the type. Make a note of that. */
14451 type = expanded_types;
14452 expanded_types = NULL_TREE;
14456 /* Loop through all of the parameters we'll build. When T is
14457 a function parameter pack, LEN is the number of expanded
14458 types in EXPANDED_TYPES; otherwise, LEN is 1. */
14459 r = NULL_TREE;
14460 for (i = 0; i < len; ++i)
14462 prev_r = r;
14463 r = copy_node (t);
14464 if (DECL_TEMPLATE_PARM_P (t))
14465 SET_DECL_TEMPLATE_PARM_P (r);
14467 if (expanded_types)
14468 /* We're on the Ith parameter of the function parameter
14469 pack. */
14471 /* Get the Ith type. */
14472 type = TREE_VEC_ELT (expanded_types, i);
14474 /* Rename the parameter to include the index. */
14475 DECL_NAME (r)
14476 = make_ith_pack_parameter_name (DECL_NAME (r), i);
14478 else if (!type)
14479 /* We're dealing with a normal parameter. */
14480 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14482 type = type_decays_to (type);
14483 TREE_TYPE (r) = type;
14484 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14486 if (DECL_INITIAL (r))
14488 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
14489 DECL_INITIAL (r) = TREE_TYPE (r);
14490 else
14491 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
14492 complain, in_decl);
14495 DECL_CONTEXT (r) = NULL_TREE;
14497 if (!DECL_TEMPLATE_PARM_P (r))
14498 DECL_ARG_TYPE (r) = type_passed_as (type);
14500 if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14501 args, complain, in_decl))
14502 return error_mark_node;
14504 /* Keep track of the first new parameter we
14505 generate. That's what will be returned to the
14506 caller. */
14507 if (!first_r)
14508 first_r = r;
14510 /* Build a proper chain of parameters when substituting
14511 into a function parameter pack. */
14512 if (prev_r)
14513 DECL_CHAIN (prev_r) = r;
14516 /* If cp_unevaluated_operand is set, we're just looking for a
14517 single dummy parameter, so don't keep going. */
14518 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
14519 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
14520 complain, DECL_CHAIN (t));
14522 /* FIRST_R contains the start of the chain we've built. */
14523 r = first_r;
14525 break;
14527 case FIELD_DECL:
14529 tree type = NULL_TREE;
14530 tree vec = NULL_TREE;
14531 tree expanded_types = NULL_TREE;
14532 int len = 1;
14534 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14536 /* This field is a lambda capture pack. Return a TREE_VEC of
14537 the expanded fields to instantiate_class_template_1. */
14538 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
14539 complain, in_decl);
14540 if (TREE_CODE (expanded_types) == TREE_VEC)
14542 len = TREE_VEC_LENGTH (expanded_types);
14543 vec = make_tree_vec (len);
14545 else
14547 /* All we did was update the type. Make a note of that. */
14548 type = expanded_types;
14549 expanded_types = NULL_TREE;
14553 for (int i = 0; i < len; ++i)
14555 r = copy_decl (t);
14556 if (expanded_types)
14558 type = TREE_VEC_ELT (expanded_types, i);
14559 DECL_NAME (r)
14560 = make_ith_pack_parameter_name (DECL_NAME (r), i);
14562 else if (!type)
14563 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14565 if (type == error_mark_node)
14566 RETURN (error_mark_node);
14567 TREE_TYPE (r) = type;
14568 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14570 if (DECL_C_BIT_FIELD (r))
14571 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
14572 number of bits. */
14573 DECL_BIT_FIELD_REPRESENTATIVE (r)
14574 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
14575 complain, in_decl,
14576 /*integral_constant_expression_p=*/true);
14577 if (DECL_INITIAL (t))
14579 /* Set up DECL_TEMPLATE_INFO so that we can get at the
14580 NSDMI in perform_member_init. Still set DECL_INITIAL
14581 so that we know there is one. */
14582 DECL_INITIAL (r) = void_node;
14583 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
14584 retrofit_lang_decl (r);
14585 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
14587 /* We don't have to set DECL_CONTEXT here; it is set by
14588 finish_member_declaration. */
14589 DECL_CHAIN (r) = NULL_TREE;
14591 if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
14592 args, complain, in_decl))
14593 return error_mark_node;
14595 if (vec)
14596 TREE_VEC_ELT (vec, i) = r;
14599 if (vec)
14600 r = vec;
14602 break;
14604 case USING_DECL:
14605 /* We reach here only for member using decls. We also need to check
14606 uses_template_parms because DECL_DEPENDENT_P is not set for a
14607 using-declaration that designates a member of the current
14608 instantiation (c++/53549). */
14609 if (DECL_DEPENDENT_P (t)
14610 || uses_template_parms (USING_DECL_SCOPE (t)))
14612 tree scope = USING_DECL_SCOPE (t);
14613 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
14614 if (PACK_EXPANSION_P (scope))
14616 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
14617 int len = TREE_VEC_LENGTH (vec);
14618 r = make_tree_vec (len);
14619 for (int i = 0; i < len; ++i)
14621 tree escope = TREE_VEC_ELT (vec, i);
14622 tree elt = do_class_using_decl (escope, name);
14623 if (!elt)
14625 r = error_mark_node;
14626 break;
14628 else
14630 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
14631 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
14633 TREE_VEC_ELT (r, i) = elt;
14636 else
14638 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
14639 complain, in_decl);
14640 r = do_class_using_decl (inst_scope, name);
14641 if (!r)
14642 r = error_mark_node;
14643 else
14645 TREE_PROTECTED (r) = TREE_PROTECTED (t);
14646 TREE_PRIVATE (r) = TREE_PRIVATE (t);
14650 else
14652 r = copy_node (t);
14653 DECL_CHAIN (r) = NULL_TREE;
14655 break;
14657 case TYPE_DECL:
14658 case VAR_DECL:
14660 tree argvec = NULL_TREE;
14661 tree gen_tmpl = NULL_TREE;
14662 tree tmpl = NULL_TREE;
14663 tree type = NULL_TREE;
14665 if (TREE_TYPE (t) == error_mark_node)
14666 RETURN (error_mark_node);
14668 if (TREE_CODE (t) == TYPE_DECL
14669 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
14671 /* If this is the canonical decl, we don't have to
14672 mess with instantiations, and often we can't (for
14673 typename, template type parms and such). Note that
14674 TYPE_NAME is not correct for the above test if
14675 we've copied the type for a typedef. */
14676 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14677 if (type == error_mark_node)
14678 RETURN (error_mark_node);
14679 r = TYPE_NAME (type);
14680 break;
14683 /* Check to see if we already have the specialization we
14684 need. */
14685 tree spec = NULL_TREE;
14686 bool local_p = false;
14687 tree ctx = DECL_CONTEXT (t);
14688 if (!(VAR_P (t) && DECL_LOCAL_DECL_P (t))
14689 && (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t)))
14691 local_p = false;
14692 if (DECL_CLASS_SCOPE_P (t))
14694 ctx = tsubst_aggr_type (ctx, args,
14695 complain,
14696 in_decl, /*entering_scope=*/1);
14697 /* If CTX is unchanged, then T is in fact the
14698 specialization we want. That situation occurs when
14699 referencing a static data member within in its own
14700 class. We can use pointer equality, rather than
14701 same_type_p, because DECL_CONTEXT is always
14702 canonical... */
14703 if (ctx == DECL_CONTEXT (t)
14704 /* ... unless T is a member template; in which
14705 case our caller can be willing to create a
14706 specialization of that template represented
14707 by T. */
14708 && !(DECL_TI_TEMPLATE (t)
14709 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
14710 spec = t;
14713 if (!spec)
14715 tmpl = DECL_TI_TEMPLATE (t);
14716 gen_tmpl = most_general_template (tmpl);
14717 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
14718 if (argvec != error_mark_node)
14719 argvec = (coerce_innermost_template_parms
14720 (DECL_TEMPLATE_PARMS (gen_tmpl),
14721 argvec, t, complain,
14722 /*all*/true, /*defarg*/true));
14723 if (argvec == error_mark_node)
14724 RETURN (error_mark_node);
14725 hash = hash_tmpl_and_args (gen_tmpl, argvec);
14726 spec = retrieve_specialization (gen_tmpl, argvec, hash);
14729 else
14731 if (!(VAR_P (t) && DECL_LOCAL_DECL_P (t)))
14732 /* Subsequent calls to pushdecl will fill this in. */
14733 ctx = NULL_TREE;
14734 /* A local variable. */
14735 local_p = true;
14736 /* Unless this is a reference to a static variable from an
14737 enclosing function, in which case we need to fill it in now. */
14738 if (TREE_STATIC (t))
14740 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
14741 if (fn != current_function_decl)
14742 ctx = fn;
14744 spec = retrieve_local_specialization (t);
14746 /* If we already have the specialization we need, there is
14747 nothing more to do. */
14748 if (spec)
14750 r = spec;
14751 break;
14754 /* Create a new node for the specialization we need. */
14755 if (type == NULL_TREE)
14757 if (is_typedef_decl (t))
14758 type = DECL_ORIGINAL_TYPE (t);
14759 else
14760 type = TREE_TYPE (t);
14761 if (VAR_P (t)
14762 && VAR_HAD_UNKNOWN_BOUND (t)
14763 && type != error_mark_node)
14764 type = strip_array_domain (type);
14765 tree sub_args = args;
14766 if (tree auto_node = type_uses_auto (type))
14768 /* Mask off any template args past the variable's context so we
14769 don't replace the auto with an unrelated argument. */
14770 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
14771 int extra = TMPL_ARGS_DEPTH (args) - nouter;
14772 if (extra > 0)
14773 /* This should never happen with the new lambda instantiation
14774 model, but keep the handling just in case. */
14775 gcc_assert (!CHECKING_P),
14776 sub_args = strip_innermost_template_args (args, extra);
14778 type = tsubst (type, sub_args, complain, in_decl);
14779 /* Substituting the type might have recursively instantiated this
14780 same alias (c++/86171). */
14781 if (gen_tmpl && DECL_ALIAS_TEMPLATE_P (gen_tmpl)
14782 && (spec = retrieve_specialization (gen_tmpl, argvec, hash)))
14784 r = spec;
14785 break;
14788 r = copy_decl (t);
14789 if (VAR_P (r))
14791 DECL_INITIALIZED_P (r) = 0;
14792 DECL_TEMPLATE_INSTANTIATED (r) = 0;
14793 if (type == error_mark_node)
14794 RETURN (error_mark_node);
14795 if (TREE_CODE (type) == FUNCTION_TYPE)
14797 /* It may seem that this case cannot occur, since:
14799 typedef void f();
14800 void g() { f x; }
14802 declares a function, not a variable. However:
14804 typedef void f();
14805 template <typename T> void g() { T t; }
14806 template void g<f>();
14808 is an attempt to declare a variable with function
14809 type. */
14810 error ("variable %qD has function type",
14811 /* R is not yet sufficiently initialized, so we
14812 just use its name. */
14813 DECL_NAME (r));
14814 RETURN (error_mark_node);
14816 type = complete_type (type);
14817 /* Wait until cp_finish_decl to set this again, to handle
14818 circular dependency (template/instantiate6.C). */
14819 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
14820 type = check_var_type (DECL_NAME (r), type,
14821 DECL_SOURCE_LOCATION (r));
14822 if (DECL_HAS_VALUE_EXPR_P (t))
14824 tree ve = DECL_VALUE_EXPR (t);
14825 /* If the DECL_VALUE_EXPR is converted to the declared type,
14826 preserve the identity so that gimplify_type_sizes works. */
14827 bool nop = (TREE_CODE (ve) == NOP_EXPR);
14828 if (nop)
14829 ve = TREE_OPERAND (ve, 0);
14830 ve = tsubst_expr (ve, args, complain, in_decl,
14831 /*constant_expression_p=*/false);
14832 if (REFERENCE_REF_P (ve))
14834 gcc_assert (TYPE_REF_P (type));
14835 ve = TREE_OPERAND (ve, 0);
14837 if (nop)
14838 ve = build_nop (type, ve);
14839 else if (DECL_LANG_SPECIFIC (t)
14840 && DECL_OMP_PRIVATIZED_MEMBER (t)
14841 && TREE_CODE (ve) == COMPONENT_REF
14842 && TREE_CODE (TREE_OPERAND (ve, 1)) == FIELD_DECL
14843 && DECL_BIT_FIELD_TYPE (TREE_OPERAND (ve, 1)) == type)
14844 type = TREE_TYPE (ve);
14845 else
14846 gcc_checking_assert (TYPE_MAIN_VARIANT (TREE_TYPE (ve))
14847 == TYPE_MAIN_VARIANT (type));
14848 SET_DECL_VALUE_EXPR (r, ve);
14850 if (CP_DECL_THREAD_LOCAL_P (r)
14851 && !processing_template_decl)
14852 set_decl_tls_model (r, decl_default_tls_model (r));
14854 else if (DECL_SELF_REFERENCE_P (t))
14855 SET_DECL_SELF_REFERENCE_P (r);
14856 TREE_TYPE (r) = type;
14857 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
14858 DECL_CONTEXT (r) = ctx;
14859 /* Clear out the mangled name and RTL for the instantiation. */
14860 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
14861 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
14862 SET_DECL_RTL (r, NULL);
14863 set_instantiating_module (r);
14865 /* The initializer must not be expanded until it is required;
14866 see [temp.inst]. */
14867 DECL_INITIAL (r) = NULL_TREE;
14868 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
14869 if (VAR_P (r))
14871 if (DECL_LANG_SPECIFIC (r))
14872 SET_DECL_DEPENDENT_INIT_P (r, false);
14874 SET_DECL_MODE (r, VOIDmode);
14876 /* Possibly limit visibility based on template args. */
14877 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
14878 if (DECL_VISIBILITY_SPECIFIED (t))
14880 DECL_VISIBILITY_SPECIFIED (r) = 0;
14881 DECL_ATTRIBUTES (r)
14882 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
14884 determine_visibility (r);
14887 if (!local_p)
14889 /* A static data member declaration is always marked
14890 external when it is declared in-class, even if an
14891 initializer is present. We mimic the non-template
14892 processing here. */
14893 DECL_EXTERNAL (r) = 1;
14894 if (DECL_NAMESPACE_SCOPE_P (t))
14895 DECL_NOT_REALLY_EXTERN (r) = 1;
14897 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
14898 SET_DECL_IMPLICIT_INSTANTIATION (r);
14899 if (!error_operand_p (r) || (complain & tf_error))
14900 register_specialization (r, gen_tmpl, argvec, false, hash);
14902 else
14904 if (DECL_LANG_SPECIFIC (r))
14905 DECL_TEMPLATE_INFO (r) = NULL_TREE;
14906 if (!cp_unevaluated_operand)
14907 register_local_specialization (r, t);
14910 DECL_CHAIN (r) = NULL_TREE;
14912 if (!apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
14913 /*flags=*/0,
14914 args, complain, in_decl))
14915 return error_mark_node;
14917 /* Preserve a typedef that names a type. */
14918 if (is_typedef_decl (r) && type != error_mark_node)
14920 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
14921 set_underlying_type (r);
14924 layout_decl (r, 0);
14926 break;
14928 default:
14929 gcc_unreachable ();
14931 #undef RETURN
14933 out:
14934 /* Restore the file and line information. */
14935 input_location = saved_loc;
14937 return r;
14940 /* Substitute into the complete parameter type list PARMS. */
14942 tree
14943 tsubst_function_parms (tree parms,
14944 tree args,
14945 tsubst_flags_t complain,
14946 tree in_decl)
14948 return tsubst_arg_types (parms, args, NULL_TREE, complain, in_decl);
14951 /* Substitute into the ARG_TYPES of a function type.
14952 If END is a TREE_CHAIN, leave it and any following types
14953 un-substituted. */
14955 static tree
14956 tsubst_arg_types (tree arg_types,
14957 tree args,
14958 tree end,
14959 tsubst_flags_t complain,
14960 tree in_decl)
14962 tree type = NULL_TREE;
14963 int len = 1;
14964 tree expanded_args = NULL_TREE;
14966 if (!arg_types || arg_types == void_list_node || arg_types == end)
14967 return arg_types;
14969 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
14971 /* For a pack expansion, perform substitution on the
14972 entire expression. Later on, we'll handle the arguments
14973 one-by-one. */
14974 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
14975 args, complain, in_decl);
14977 if (TREE_CODE (expanded_args) == TREE_VEC)
14978 /* So that we'll spin through the parameters, one by one. */
14979 len = TREE_VEC_LENGTH (expanded_args);
14980 else
14982 /* We only partially substituted into the parameter
14983 pack. Our type is TYPE_PACK_EXPANSION. */
14984 type = expanded_args;
14985 expanded_args = NULL_TREE;
14988 else
14989 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
14991 /* Check if a substituted type is erroneous before substituting into
14992 the rest of the chain. */
14993 for (int i = 0; i < len; i++)
14995 if (expanded_args)
14996 type = TREE_VEC_ELT (expanded_args, i);
14998 if (type == error_mark_node)
14999 return error_mark_node;
15000 if (VOID_TYPE_P (type))
15002 if (complain & tf_error)
15004 error ("invalid parameter type %qT", type);
15005 if (in_decl)
15006 error ("in declaration %q+D", in_decl);
15008 return error_mark_node;
15012 /* We do not substitute into default arguments here. The standard
15013 mandates that they be instantiated only when needed, which is
15014 done in build_over_call. */
15015 tree default_arg = TREE_PURPOSE (arg_types);
15017 /* Except that we do substitute default arguments under tsubst_lambda_expr,
15018 since the new op() won't have any associated template arguments for us
15019 to refer to later. */
15020 if (lambda_fn_in_template_p (in_decl))
15021 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
15022 false/*fn*/, false/*constexpr*/);
15024 tree remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
15025 args, end, complain, in_decl);
15026 if (remaining_arg_types == error_mark_node)
15027 return error_mark_node;
15029 for (int i = len-1; i >= 0; i--)
15031 if (expanded_args)
15032 type = TREE_VEC_ELT (expanded_args, i);
15034 /* Do array-to-pointer, function-to-pointer conversion, and ignore
15035 top-level qualifiers as required. */
15036 type = cv_unqualified (type_decays_to (type));
15038 if (default_arg && TREE_CODE (default_arg) == DEFERRED_PARSE)
15040 /* We've instantiated a template before its default arguments
15041 have been parsed. This can happen for a nested template
15042 class, and is not an error unless we require the default
15043 argument in a call of this function. */
15044 remaining_arg_types
15045 = tree_cons (default_arg, type, remaining_arg_types);
15046 vec_safe_push (DEFPARSE_INSTANTIATIONS (default_arg),
15047 remaining_arg_types);
15049 else
15050 remaining_arg_types
15051 = hash_tree_cons (default_arg, type, remaining_arg_types);
15054 return remaining_arg_types;
15057 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
15058 *not* handle the exception-specification for FNTYPE, because the
15059 initial substitution of explicitly provided template parameters
15060 during argument deduction forbids substitution into the
15061 exception-specification:
15063 [temp.deduct]
15065 All references in the function type of the function template to the
15066 corresponding template parameters are replaced by the specified tem-
15067 plate argument values. If a substitution in a template parameter or
15068 in the function type of the function template results in an invalid
15069 type, type deduction fails. [Note: The equivalent substitution in
15070 exception specifications is done only when the function is instanti-
15071 ated, at which point a program is ill-formed if the substitution
15072 results in an invalid type.] */
15074 static tree
15075 tsubst_function_type (tree t,
15076 tree args,
15077 tsubst_flags_t complain,
15078 tree in_decl)
15080 tree return_type;
15081 tree arg_types = NULL_TREE;
15083 /* The TYPE_CONTEXT is not used for function/method types. */
15084 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
15086 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
15087 failure. */
15088 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
15090 if (late_return_type_p)
15092 /* Substitute the argument types. */
15093 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
15094 complain, in_decl);
15095 if (arg_types == error_mark_node)
15096 return error_mark_node;
15098 tree save_ccp = current_class_ptr;
15099 tree save_ccr = current_class_ref;
15100 tree this_type = (TREE_CODE (t) == METHOD_TYPE
15101 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
15102 bool do_inject = this_type && CLASS_TYPE_P (this_type);
15103 if (do_inject)
15105 /* DR 1207: 'this' is in scope in the trailing return type. */
15106 inject_this_parameter (this_type, cp_type_quals (this_type));
15109 /* Substitute the return type. */
15110 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15112 if (do_inject)
15114 current_class_ptr = save_ccp;
15115 current_class_ref = save_ccr;
15118 else
15119 /* Substitute the return type. */
15120 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15122 if (return_type == error_mark_node)
15123 return error_mark_node;
15124 /* DR 486 clarifies that creation of a function type with an
15125 invalid return type is a deduction failure. */
15126 if (TREE_CODE (return_type) == ARRAY_TYPE
15127 || TREE_CODE (return_type) == FUNCTION_TYPE)
15129 if (complain & tf_error)
15131 if (TREE_CODE (return_type) == ARRAY_TYPE)
15132 error ("function returning an array");
15133 else
15134 error ("function returning a function");
15136 return error_mark_node;
15139 if (!late_return_type_p)
15141 /* Substitute the argument types. */
15142 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
15143 complain, in_decl);
15144 if (arg_types == error_mark_node)
15145 return error_mark_node;
15148 /* Construct a new type node and return it. */
15149 return rebuild_function_or_method_type (t, return_type, arg_types,
15150 /*raises=*/NULL_TREE, complain);
15153 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
15154 ARGS into that specification, and return the substituted
15155 specification. If there is no specification, return NULL_TREE. */
15157 static tree
15158 tsubst_exception_specification (tree fntype,
15159 tree args,
15160 tsubst_flags_t complain,
15161 tree in_decl,
15162 bool defer_ok)
15164 tree specs;
15165 tree new_specs;
15167 specs = TYPE_RAISES_EXCEPTIONS (fntype);
15168 new_specs = NULL_TREE;
15169 if (specs && TREE_PURPOSE (specs))
15171 /* A noexcept-specifier. */
15172 tree expr = TREE_PURPOSE (specs);
15173 if (TREE_CODE (expr) == INTEGER_CST)
15174 new_specs = expr;
15175 else if (defer_ok)
15177 /* Defer instantiation of noexcept-specifiers to avoid
15178 excessive instantiations (c++/49107). */
15179 new_specs = make_node (DEFERRED_NOEXCEPT);
15180 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
15182 /* We already partially instantiated this member template,
15183 so combine the new args with the old. */
15184 DEFERRED_NOEXCEPT_PATTERN (new_specs)
15185 = DEFERRED_NOEXCEPT_PATTERN (expr);
15186 DEFERRED_NOEXCEPT_ARGS (new_specs)
15187 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
15189 else
15191 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
15192 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
15195 else
15197 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
15199 args = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr),
15200 args);
15201 expr = DEFERRED_NOEXCEPT_PATTERN (expr);
15203 new_specs = tsubst_copy_and_build
15204 (expr, args, complain, in_decl, /*function_p=*/false,
15205 /*integral_constant_expression_p=*/true);
15207 new_specs = build_noexcept_spec (new_specs, complain);
15208 /* We've instantiated a template before a noexcept-specifier
15209 contained therein has been parsed. This can happen for
15210 a nested template class:
15212 struct S {
15213 template<typename> struct B { B() noexcept(...); };
15214 struct A : B<int> { ... use B() ... };
15217 where completing B<int> will trigger instantiating the
15218 noexcept, even though we only parse it at the end of S. */
15219 if (UNPARSED_NOEXCEPT_SPEC_P (specs))
15221 gcc_checking_assert (defer_ok);
15222 vec_safe_push (DEFPARSE_INSTANTIATIONS (expr), new_specs);
15225 else if (specs)
15227 if (! TREE_VALUE (specs))
15228 new_specs = specs;
15229 else
15230 while (specs)
15232 tree spec;
15233 int i, len = 1;
15234 tree expanded_specs = NULL_TREE;
15236 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
15238 /* Expand the pack expansion type. */
15239 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
15240 args, complain,
15241 in_decl);
15243 if (expanded_specs == error_mark_node)
15244 return error_mark_node;
15245 else if (TREE_CODE (expanded_specs) == TREE_VEC)
15246 len = TREE_VEC_LENGTH (expanded_specs);
15247 else
15249 /* We're substituting into a member template, so
15250 we got a TYPE_PACK_EXPANSION back. Add that
15251 expansion and move on. */
15252 gcc_assert (TREE_CODE (expanded_specs)
15253 == TYPE_PACK_EXPANSION);
15254 new_specs = add_exception_specifier (new_specs,
15255 expanded_specs,
15256 complain);
15257 specs = TREE_CHAIN (specs);
15258 continue;
15262 for (i = 0; i < len; ++i)
15264 if (expanded_specs)
15265 spec = TREE_VEC_ELT (expanded_specs, i);
15266 else
15267 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
15268 if (spec == error_mark_node)
15269 return spec;
15270 new_specs = add_exception_specifier (new_specs, spec,
15271 complain);
15274 specs = TREE_CHAIN (specs);
15277 return new_specs;
15280 /* Substitute through a TREE_LIST of types or expressions, handling pack
15281 expansions. */
15283 tree
15284 tsubst_tree_list (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15286 if (t == void_list_node)
15287 return t;
15289 tree purpose = TREE_PURPOSE (t);
15290 tree purposevec = NULL_TREE;
15291 if (!purpose)
15293 else if (PACK_EXPANSION_P (purpose))
15295 purpose = tsubst_pack_expansion (purpose, args, complain, in_decl);
15296 if (TREE_CODE (purpose) == TREE_VEC)
15297 purposevec = purpose;
15299 else if (TYPE_P (purpose))
15300 purpose = tsubst (purpose, args, complain, in_decl);
15301 else
15302 purpose = tsubst_copy_and_build (purpose, args, complain, in_decl);
15303 if (purpose == error_mark_node || purposevec == error_mark_node)
15304 return error_mark_node;
15306 tree value = TREE_VALUE (t);
15307 tree valuevec = NULL_TREE;
15308 if (!value)
15310 else if (PACK_EXPANSION_P (value))
15312 value = tsubst_pack_expansion (value, args, complain, in_decl);
15313 if (TREE_CODE (value) == TREE_VEC)
15314 valuevec = value;
15316 else if (TYPE_P (value))
15317 value = tsubst (value, args, complain, in_decl);
15318 else
15319 value = tsubst_copy_and_build (value, args, complain, in_decl);
15320 if (value == error_mark_node || valuevec == error_mark_node)
15321 return error_mark_node;
15323 tree chain = TREE_CHAIN (t);
15324 if (!chain)
15326 else if (TREE_CODE (chain) == TREE_LIST)
15327 chain = tsubst_tree_list (chain, args, complain, in_decl);
15328 else if (TYPE_P (chain))
15329 chain = tsubst (chain, args, complain, in_decl);
15330 else
15331 chain = tsubst_copy_and_build (chain, args, complain, in_decl);
15332 if (chain == error_mark_node)
15333 return error_mark_node;
15335 if (purpose == TREE_PURPOSE (t)
15336 && value == TREE_VALUE (t)
15337 && chain == TREE_CHAIN (t))
15338 return t;
15340 int len;
15341 /* Determine the number of arguments. */
15342 if (purposevec)
15344 len = TREE_VEC_LENGTH (purposevec);
15345 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
15347 else if (valuevec)
15348 len = TREE_VEC_LENGTH (valuevec);
15349 else
15350 len = 1;
15352 for (int i = len; i-- > 0; )
15354 if (purposevec)
15355 purpose = TREE_VEC_ELT (purposevec, i);
15356 if (valuevec)
15357 value = TREE_VEC_ELT (valuevec, i);
15359 if (value && TYPE_P (value))
15360 chain = hash_tree_cons (purpose, value, chain);
15361 else
15362 chain = tree_cons (purpose, value, chain);
15365 return chain;
15368 /* Take the tree structure T and replace template parameters used
15369 therein with the argument vector ARGS. IN_DECL is an associated
15370 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
15371 Issue error and warning messages under control of COMPLAIN. Note
15372 that we must be relatively non-tolerant of extensions here, in
15373 order to preserve conformance; if we allow substitutions that
15374 should not be allowed, we may allow argument deductions that should
15375 not succeed, and therefore report ambiguous overload situations
15376 where there are none. In theory, we could allow the substitution,
15377 but indicate that it should have failed, and allow our caller to
15378 make sure that the right thing happens, but we don't try to do this
15379 yet.
15381 This function is used for dealing with types, decls and the like;
15382 for expressions, use tsubst_expr or tsubst_copy. */
15384 tree
15385 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
15387 enum tree_code code;
15388 tree type, r = NULL_TREE;
15390 if (t == NULL_TREE || t == error_mark_node
15391 || t == integer_type_node
15392 || t == void_type_node
15393 || t == char_type_node
15394 || t == unknown_type_node
15395 || TREE_CODE (t) == NAMESPACE_DECL
15396 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
15397 return t;
15399 if (DECL_P (t))
15400 return tsubst_decl (t, args, complain);
15402 if (args == NULL_TREE)
15403 return t;
15405 code = TREE_CODE (t);
15407 gcc_assert (code != IDENTIFIER_NODE);
15408 type = TREE_TYPE (t);
15410 gcc_assert (type != unknown_type_node);
15412 /* Reuse typedefs. We need to do this to handle dependent attributes,
15413 such as attribute aligned. */
15414 if (TYPE_P (t)
15415 && typedef_variant_p (t))
15417 tree decl = TYPE_NAME (t);
15419 if (alias_template_specialization_p (t, nt_opaque))
15421 /* DECL represents an alias template and we want to
15422 instantiate it. */
15423 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
15424 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
15425 r = instantiate_alias_template (tmpl, gen_args, complain);
15427 else if (DECL_CLASS_SCOPE_P (decl)
15428 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
15429 && uses_template_parms (DECL_CONTEXT (decl)))
15431 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
15432 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
15433 r = retrieve_specialization (tmpl, gen_args, 0);
15435 else if (DECL_FUNCTION_SCOPE_P (decl)
15436 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
15437 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
15438 r = retrieve_local_specialization (decl);
15439 else
15440 /* The typedef is from a non-template context. */
15441 return t;
15443 if (r)
15445 r = TREE_TYPE (r);
15446 r = cp_build_qualified_type_real
15447 (r, cp_type_quals (t) | cp_type_quals (r),
15448 complain | tf_ignore_bad_quals);
15449 return r;
15451 else
15453 /* We don't have an instantiation yet, so drop the typedef. */
15454 int quals = cp_type_quals (t);
15455 t = DECL_ORIGINAL_TYPE (decl);
15456 t = cp_build_qualified_type_real (t, quals,
15457 complain | tf_ignore_bad_quals);
15461 bool fndecl_type = (complain & tf_fndecl_type);
15462 complain &= ~tf_fndecl_type;
15464 if (type
15465 && code != TYPENAME_TYPE
15466 && code != TEMPLATE_TYPE_PARM
15467 && code != TEMPLATE_PARM_INDEX
15468 && code != IDENTIFIER_NODE
15469 && code != FUNCTION_TYPE
15470 && code != METHOD_TYPE)
15471 type = tsubst (type, args, complain, in_decl);
15472 if (type == error_mark_node)
15473 return error_mark_node;
15475 switch (code)
15477 case RECORD_TYPE:
15478 case UNION_TYPE:
15479 case ENUMERAL_TYPE:
15480 return tsubst_aggr_type (t, args, complain, in_decl,
15481 /*entering_scope=*/0);
15483 case ERROR_MARK:
15484 case IDENTIFIER_NODE:
15485 case VOID_TYPE:
15486 case OPAQUE_TYPE:
15487 case REAL_TYPE:
15488 case COMPLEX_TYPE:
15489 case VECTOR_TYPE:
15490 case BOOLEAN_TYPE:
15491 case NULLPTR_TYPE:
15492 case LANG_TYPE:
15493 return t;
15495 case INTEGER_TYPE:
15496 if (t == integer_type_node)
15497 return t;
15499 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
15500 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
15501 return t;
15504 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
15506 max = tsubst_expr (omax, args, complain, in_decl,
15507 /*integral_constant_expression_p=*/false);
15509 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
15510 needed. */
15511 if (TREE_CODE (max) == NOP_EXPR
15512 && TREE_SIDE_EFFECTS (omax)
15513 && !TREE_TYPE (max))
15514 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
15516 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
15517 with TREE_SIDE_EFFECTS that indicates this is not an integral
15518 constant expression. */
15519 if (processing_template_decl
15520 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
15522 gcc_assert (TREE_CODE (max) == NOP_EXPR);
15523 TREE_SIDE_EFFECTS (max) = 1;
15526 return compute_array_index_type (NULL_TREE, max, complain);
15529 case TEMPLATE_TYPE_PARM:
15530 case TEMPLATE_TEMPLATE_PARM:
15531 case BOUND_TEMPLATE_TEMPLATE_PARM:
15532 case TEMPLATE_PARM_INDEX:
15534 int idx;
15535 int level;
15536 int levels;
15537 tree arg = NULL_TREE;
15539 r = NULL_TREE;
15541 gcc_assert (TREE_VEC_LENGTH (args) > 0);
15542 template_parm_level_and_index (t, &level, &idx);
15544 levels = TMPL_ARGS_DEPTH (args);
15545 if (level <= levels
15546 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
15548 arg = TMPL_ARG (args, level, idx);
15550 /* See through ARGUMENT_PACK_SELECT arguments. */
15551 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
15552 arg = argument_pack_select_arg (arg);
15555 if (arg == error_mark_node)
15556 return error_mark_node;
15557 else if (arg != NULL_TREE)
15559 if (ARGUMENT_PACK_P (arg))
15560 /* If ARG is an argument pack, we don't actually want to
15561 perform a substitution here, because substitutions
15562 for argument packs are only done
15563 element-by-element. We can get to this point when
15564 substituting the type of a non-type template
15565 parameter pack, when that type actually contains
15566 template parameter packs from an outer template, e.g.,
15568 template<typename... Types> struct A {
15569 template<Types... Values> struct B { };
15570 }; */
15571 return t;
15573 if (code == TEMPLATE_TYPE_PARM)
15575 int quals;
15577 /* When building concept checks for the purpose of
15578 deducing placeholders, we can end up with wildcards
15579 where types are expected. Adjust this to the deduced
15580 value. */
15581 if (TREE_CODE (arg) == WILDCARD_DECL)
15582 arg = TREE_TYPE (TREE_TYPE (arg));
15584 gcc_assert (TYPE_P (arg));
15586 quals = cp_type_quals (arg) | cp_type_quals (t);
15588 return cp_build_qualified_type_real
15589 (arg, quals, complain | tf_ignore_bad_quals);
15591 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
15593 /* We are processing a type constructed from a
15594 template template parameter. */
15595 tree argvec = tsubst (TYPE_TI_ARGS (t),
15596 args, complain, in_decl);
15597 if (argvec == error_mark_node)
15598 return error_mark_node;
15600 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
15601 || TREE_CODE (arg) == TEMPLATE_DECL
15602 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
15604 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
15605 /* Consider this code:
15607 template <template <class> class Template>
15608 struct Internal {
15609 template <class Arg> using Bind = Template<Arg>;
15612 template <template <class> class Template, class Arg>
15613 using Instantiate = Template<Arg>; //#0
15615 template <template <class> class Template,
15616 class Argument>
15617 using Bind =
15618 Instantiate<Internal<Template>::template Bind,
15619 Argument>; //#1
15621 When #1 is parsed, the
15622 BOUND_TEMPLATE_TEMPLATE_PARM representing the
15623 parameter `Template' in #0 matches the
15624 UNBOUND_CLASS_TEMPLATE representing the argument
15625 `Internal<Template>::template Bind'; We then want
15626 to assemble the type `Bind<Argument>' that can't
15627 be fully created right now, because
15628 `Internal<Template>' not being complete, the Bind
15629 template cannot be looked up in that context. So
15630 we need to "store" `Bind<Argument>' for later
15631 when the context of Bind becomes complete. Let's
15632 store that in a TYPENAME_TYPE. */
15633 return make_typename_type (TYPE_CONTEXT (arg),
15634 build_nt (TEMPLATE_ID_EXPR,
15635 TYPE_IDENTIFIER (arg),
15636 argvec),
15637 typename_type,
15638 complain);
15640 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
15641 are resolving nested-types in the signature of a
15642 member function templates. Otherwise ARG is a
15643 TEMPLATE_DECL and is the real template to be
15644 instantiated. */
15645 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
15646 arg = TYPE_NAME (arg);
15648 r = lookup_template_class (arg,
15649 argvec, in_decl,
15650 DECL_CONTEXT (arg),
15651 /*entering_scope=*/0,
15652 complain);
15653 return cp_build_qualified_type_real
15654 (r, cp_type_quals (t) | cp_type_quals (r), complain);
15656 else if (code == TEMPLATE_TEMPLATE_PARM)
15657 return arg;
15658 else
15659 /* TEMPLATE_PARM_INDEX. */
15660 return convert_from_reference (unshare_expr (arg));
15663 if (level == 1)
15664 /* This can happen during the attempted tsubst'ing in
15665 unify. This means that we don't yet have any information
15666 about the template parameter in question. */
15667 return t;
15669 /* Early in template argument deduction substitution, we don't
15670 want to reduce the level of 'auto', or it will be confused
15671 with a normal template parm in subsequent deduction.
15672 Similarly, don't reduce the level of template parameters to
15673 avoid mismatches when deducing their types. */
15674 if (complain & tf_partial)
15675 return t;
15677 /* If we get here, we must have been looking at a parm for a
15678 more deeply nested template. Make a new version of this
15679 template parameter, but with a lower level. */
15680 switch (code)
15682 case TEMPLATE_TYPE_PARM:
15683 case TEMPLATE_TEMPLATE_PARM:
15684 case BOUND_TEMPLATE_TEMPLATE_PARM:
15685 if (cp_type_quals (t))
15687 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
15688 r = cp_build_qualified_type_real
15689 (r, cp_type_quals (t),
15690 complain | (code == TEMPLATE_TYPE_PARM
15691 ? tf_ignore_bad_quals : 0));
15693 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
15694 && PLACEHOLDER_TYPE_CONSTRAINTS_INFO (t)
15695 && (r = (TEMPLATE_PARM_DESCENDANTS
15696 (TEMPLATE_TYPE_PARM_INDEX (t))))
15697 && (r = TREE_TYPE (r))
15698 && !PLACEHOLDER_TYPE_CONSTRAINTS_INFO (r))
15699 /* Break infinite recursion when substituting the constraints
15700 of a constrained placeholder. */;
15701 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
15702 && !PLACEHOLDER_TYPE_CONSTRAINTS_INFO (t)
15703 && !CLASS_PLACEHOLDER_TEMPLATE (t)
15704 && (arg = TEMPLATE_TYPE_PARM_INDEX (t),
15705 r = TEMPLATE_PARM_DESCENDANTS (arg))
15706 && (TEMPLATE_PARM_LEVEL (r)
15707 == TEMPLATE_PARM_LEVEL (arg) - levels))
15708 /* Cache the simple case of lowering a type parameter. */
15709 r = TREE_TYPE (r);
15710 else
15712 r = copy_type (t);
15713 TEMPLATE_TYPE_PARM_INDEX (r)
15714 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
15715 r, levels, args, complain);
15716 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
15717 TYPE_MAIN_VARIANT (r) = r;
15718 TYPE_POINTER_TO (r) = NULL_TREE;
15719 TYPE_REFERENCE_TO (r) = NULL_TREE;
15721 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
15723 /* Propagate constraints on placeholders since they are
15724 only instantiated during satisfaction. */
15725 if (tree ci = PLACEHOLDER_TYPE_CONSTRAINTS_INFO (t))
15726 PLACEHOLDER_TYPE_CONSTRAINTS_INFO (r) = ci;
15727 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
15729 pl = tsubst_copy (pl, args, complain, in_decl);
15730 if (TREE_CODE (pl) == TEMPLATE_TEMPLATE_PARM)
15731 pl = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (pl);
15732 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
15736 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
15737 /* We have reduced the level of the template
15738 template parameter, but not the levels of its
15739 template parameters, so canonical_type_parameter
15740 will not be able to find the canonical template
15741 template parameter for this level. Thus, we
15742 require structural equality checking to compare
15743 TEMPLATE_TEMPLATE_PARMs. */
15744 SET_TYPE_STRUCTURAL_EQUALITY (r);
15745 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
15746 SET_TYPE_STRUCTURAL_EQUALITY (r);
15747 else
15748 TYPE_CANONICAL (r) = canonical_type_parameter (r);
15750 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
15752 tree tinfo = TYPE_TEMPLATE_INFO (t);
15753 /* We might need to substitute into the types of non-type
15754 template parameters. */
15755 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
15756 complain, in_decl);
15757 if (tmpl == error_mark_node)
15758 return error_mark_node;
15759 tree argvec = tsubst (TI_ARGS (tinfo), args,
15760 complain, in_decl);
15761 if (argvec == error_mark_node)
15762 return error_mark_node;
15764 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
15765 = build_template_info (tmpl, argvec);
15768 break;
15770 case TEMPLATE_PARM_INDEX:
15771 /* OK, now substitute the type of the non-type parameter. We
15772 couldn't do it earlier because it might be an auto parameter,
15773 and we wouldn't need to if we had an argument. */
15774 type = tsubst (type, args, complain, in_decl);
15775 if (type == error_mark_node)
15776 return error_mark_node;
15777 r = reduce_template_parm_level (t, type, levels, args, complain);
15778 break;
15780 default:
15781 gcc_unreachable ();
15784 return r;
15787 case TREE_LIST:
15788 return tsubst_tree_list (t, args, complain, in_decl);
15790 case TREE_BINFO:
15791 /* We should never be tsubsting a binfo. */
15792 gcc_unreachable ();
15794 case TREE_VEC:
15795 /* A vector of template arguments. */
15796 gcc_assert (!type);
15797 return tsubst_template_args (t, args, complain, in_decl);
15799 case POINTER_TYPE:
15800 case REFERENCE_TYPE:
15802 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
15803 return t;
15805 /* [temp.deduct]
15807 Type deduction may fail for any of the following
15808 reasons:
15810 -- Attempting to create a pointer to reference type.
15811 -- Attempting to create a reference to a reference type or
15812 a reference to void.
15814 Core issue 106 says that creating a reference to a reference
15815 during instantiation is no longer a cause for failure. We
15816 only enforce this check in strict C++98 mode. */
15817 if ((TYPE_REF_P (type)
15818 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
15819 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
15821 static location_t last_loc;
15823 /* We keep track of the last time we issued this error
15824 message to avoid spewing a ton of messages during a
15825 single bad template instantiation. */
15826 if (complain & tf_error
15827 && last_loc != input_location)
15829 if (VOID_TYPE_P (type))
15830 error ("forming reference to void");
15831 else if (code == POINTER_TYPE)
15832 error ("forming pointer to reference type %qT", type);
15833 else
15834 error ("forming reference to reference type %qT", type);
15835 last_loc = input_location;
15838 return error_mark_node;
15840 else if (TREE_CODE (type) == FUNCTION_TYPE
15841 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
15842 || type_memfn_rqual (type) != REF_QUAL_NONE))
15844 if (complain & tf_error)
15846 if (code == POINTER_TYPE)
15847 error ("forming pointer to qualified function type %qT",
15848 type);
15849 else
15850 error ("forming reference to qualified function type %qT",
15851 type);
15853 return error_mark_node;
15855 else if (code == POINTER_TYPE)
15857 r = build_pointer_type (type);
15858 if (TREE_CODE (type) == METHOD_TYPE)
15859 r = build_ptrmemfunc_type (r);
15861 else if (TYPE_REF_P (type))
15862 /* In C++0x, during template argument substitution, when there is an
15863 attempt to create a reference to a reference type, reference
15864 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
15866 "If a template-argument for a template-parameter T names a type
15867 that is a reference to a type A, an attempt to create the type
15868 'lvalue reference to cv T' creates the type 'lvalue reference to
15869 A,' while an attempt to create the type type rvalue reference to
15870 cv T' creates the type T"
15872 r = cp_build_reference_type
15873 (TREE_TYPE (type),
15874 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
15875 else
15876 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
15877 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
15879 if (r != error_mark_node)
15880 /* Will this ever be needed for TYPE_..._TO values? */
15881 layout_type (r);
15883 return r;
15885 case OFFSET_TYPE:
15887 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
15888 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
15890 /* [temp.deduct]
15892 Type deduction may fail for any of the following
15893 reasons:
15895 -- Attempting to create "pointer to member of T" when T
15896 is not a class type. */
15897 if (complain & tf_error)
15898 error ("creating pointer to member of non-class type %qT", r);
15899 return error_mark_node;
15901 if (TYPE_REF_P (type))
15903 if (complain & tf_error)
15904 error ("creating pointer to member reference type %qT", type);
15905 return error_mark_node;
15907 if (VOID_TYPE_P (type))
15909 if (complain & tf_error)
15910 error ("creating pointer to member of type void");
15911 return error_mark_node;
15913 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
15914 if (TREE_CODE (type) == FUNCTION_TYPE)
15916 /* The type of the implicit object parameter gets its
15917 cv-qualifiers from the FUNCTION_TYPE. */
15918 tree memptr;
15919 tree method_type
15920 = build_memfn_type (type, r, type_memfn_quals (type),
15921 type_memfn_rqual (type));
15922 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
15923 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
15924 complain);
15926 else
15927 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
15928 cp_type_quals (t),
15929 complain);
15931 case FUNCTION_TYPE:
15932 case METHOD_TYPE:
15934 tree fntype;
15935 tree specs;
15936 fntype = tsubst_function_type (t, args, complain, in_decl);
15937 if (fntype == error_mark_node)
15938 return error_mark_node;
15940 /* Substitute the exception specification. */
15941 specs = tsubst_exception_specification (t, args, complain, in_decl,
15942 /*defer_ok*/fndecl_type);
15943 if (specs == error_mark_node)
15944 return error_mark_node;
15945 if (specs)
15946 fntype = build_exception_variant (fntype, specs);
15947 return fntype;
15949 case ARRAY_TYPE:
15951 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
15952 if (domain == error_mark_node)
15953 return error_mark_node;
15955 /* As an optimization, we avoid regenerating the array type if
15956 it will obviously be the same as T. */
15957 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
15958 return t;
15960 /* These checks should match the ones in create_array_type_for_decl.
15962 [temp.deduct]
15964 The deduction may fail for any of the following reasons:
15966 -- Attempting to create an array with an element type that
15967 is void, a function type, or a reference type, or [DR337]
15968 an abstract class type. */
15969 if (VOID_TYPE_P (type)
15970 || TREE_CODE (type) == FUNCTION_TYPE
15971 || (TREE_CODE (type) == ARRAY_TYPE
15972 && TYPE_DOMAIN (type) == NULL_TREE)
15973 || TYPE_REF_P (type))
15975 if (complain & tf_error)
15976 error ("creating array of %qT", type);
15977 return error_mark_node;
15980 if (!verify_type_context (input_location, TCTX_ARRAY_ELEMENT, type,
15981 !(complain & tf_error)))
15982 return error_mark_node;
15984 r = build_cplus_array_type (type, domain);
15986 if (!valid_array_size_p (input_location, r, in_decl,
15987 (complain & tf_error)))
15988 return error_mark_node;
15990 if (TYPE_USER_ALIGN (t))
15992 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
15993 TYPE_USER_ALIGN (r) = 1;
15996 return r;
15999 case TYPENAME_TYPE:
16001 tree ctx = TYPE_CONTEXT (t);
16002 if (TREE_CODE (ctx) == TYPE_PACK_EXPANSION)
16004 ctx = tsubst_pack_expansion (ctx, args, complain, in_decl);
16005 if (ctx == error_mark_node
16006 || TREE_VEC_LENGTH (ctx) > 1)
16007 return error_mark_node;
16008 if (TREE_VEC_LENGTH (ctx) == 0)
16010 if (complain & tf_error)
16011 error ("%qD is instantiated for an empty pack",
16012 TYPENAME_TYPE_FULLNAME (t));
16013 return error_mark_node;
16015 ctx = TREE_VEC_ELT (ctx, 0);
16017 else
16018 ctx = tsubst_aggr_type (ctx, args, complain, in_decl,
16019 /*entering_scope=*/1);
16020 if (ctx == error_mark_node)
16021 return error_mark_node;
16023 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
16024 complain, in_decl);
16025 if (f == error_mark_node)
16026 return error_mark_node;
16028 if (!MAYBE_CLASS_TYPE_P (ctx))
16030 if (complain & tf_error)
16031 error ("%qT is not a class, struct, or union type", ctx);
16032 return error_mark_node;
16034 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
16036 /* Normally, make_typename_type does not require that the CTX
16037 have complete type in order to allow things like:
16039 template <class T> struct S { typename S<T>::X Y; };
16041 But, such constructs have already been resolved by this
16042 point, so here CTX really should have complete type, unless
16043 it's a partial instantiation. */
16044 if (!complete_type_or_maybe_complain (ctx, NULL_TREE, complain))
16045 return error_mark_node;
16048 f = make_typename_type (ctx, f, typename_type,
16049 complain | tf_keep_type_decl);
16050 if (f == error_mark_node)
16051 return f;
16052 if (TREE_CODE (f) == TYPE_DECL)
16054 complain |= tf_ignore_bad_quals;
16055 f = TREE_TYPE (f);
16058 if (TREE_CODE (f) != TYPENAME_TYPE)
16060 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
16062 if (complain & tf_error)
16063 error ("%qT resolves to %qT, which is not an enumeration type",
16064 t, f);
16065 else
16066 return error_mark_node;
16068 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
16070 if (complain & tf_error)
16071 error ("%qT resolves to %qT, which is not a class type",
16072 t, f);
16073 else
16074 return error_mark_node;
16078 return cp_build_qualified_type_real
16079 (f, cp_type_quals (f) | cp_type_quals (t), complain);
16082 case UNBOUND_CLASS_TEMPLATE:
16084 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
16085 in_decl, /*entering_scope=*/1);
16086 tree name = TYPE_IDENTIFIER (t);
16087 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
16089 if (ctx == error_mark_node || name == error_mark_node)
16090 return error_mark_node;
16092 if (parm_list)
16093 parm_list = tsubst_template_parms (parm_list, args, complain);
16094 return make_unbound_class_template (ctx, name, parm_list, complain);
16097 case TYPEOF_TYPE:
16099 tree type;
16101 ++cp_unevaluated_operand;
16102 ++c_inhibit_evaluation_warnings;
16104 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
16105 complain, in_decl,
16106 /*integral_constant_expression_p=*/false);
16108 --cp_unevaluated_operand;
16109 --c_inhibit_evaluation_warnings;
16111 type = finish_typeof (type);
16112 return cp_build_qualified_type_real (type,
16113 cp_type_quals (t)
16114 | cp_type_quals (type),
16115 complain);
16118 case DECLTYPE_TYPE:
16120 tree type;
16122 ++cp_unevaluated_operand;
16123 ++c_inhibit_evaluation_warnings;
16125 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
16126 complain|tf_decltype, in_decl,
16127 /*function_p*/false,
16128 /*integral_constant_expression*/false);
16130 --cp_unevaluated_operand;
16131 --c_inhibit_evaluation_warnings;
16133 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
16134 type = lambda_capture_field_type (type,
16135 false /*explicit_init*/,
16136 DECLTYPE_FOR_REF_CAPTURE (t));
16137 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
16138 type = lambda_proxy_type (type);
16139 else
16141 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
16142 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
16143 && EXPR_P (type))
16144 /* In a template ~id could be either a complement expression
16145 or an unqualified-id naming a destructor; if instantiating
16146 it produces an expression, it's not an id-expression or
16147 member access. */
16148 id = false;
16149 type = finish_decltype_type (type, id, complain);
16151 return cp_build_qualified_type_real (type,
16152 cp_type_quals (t)
16153 | cp_type_quals (type),
16154 complain | tf_ignore_bad_quals);
16157 case UNDERLYING_TYPE:
16159 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
16160 complain, in_decl);
16161 return finish_underlying_type (type);
16164 case TYPE_ARGUMENT_PACK:
16165 case NONTYPE_ARGUMENT_PACK:
16166 return tsubst_argument_pack (t, args, complain, in_decl);
16168 case VOID_CST:
16169 case INTEGER_CST:
16170 case REAL_CST:
16171 case STRING_CST:
16172 case PLUS_EXPR:
16173 case MINUS_EXPR:
16174 case NEGATE_EXPR:
16175 case NOP_EXPR:
16176 case INDIRECT_REF:
16177 case ADDR_EXPR:
16178 case CALL_EXPR:
16179 case ARRAY_REF:
16180 case SCOPE_REF:
16181 /* We should use one of the expression tsubsts for these codes. */
16182 gcc_unreachable ();
16184 default:
16185 sorry ("use of %qs in template", get_tree_code_name (code));
16186 return error_mark_node;
16190 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
16191 expression on the left-hand side of the "." or "->" operator. We
16192 only do the lookup if we had a dependent BASELINK. Otherwise we
16193 adjust it onto the instantiated heirarchy. */
16195 static tree
16196 tsubst_baselink (tree baselink, tree object_type,
16197 tree args, tsubst_flags_t complain, tree in_decl)
16199 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
16200 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
16201 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
16203 tree optype = BASELINK_OPTYPE (baselink);
16204 optype = tsubst (optype, args, complain, in_decl);
16206 tree template_args = NULL_TREE;
16207 bool template_id_p = false;
16208 tree fns = BASELINK_FUNCTIONS (baselink);
16209 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
16211 template_id_p = true;
16212 template_args = TREE_OPERAND (fns, 1);
16213 fns = TREE_OPERAND (fns, 0);
16214 if (template_args)
16215 template_args = tsubst_template_args (template_args, args,
16216 complain, in_decl);
16219 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
16220 binfo_type = tsubst (binfo_type, args, complain, in_decl);
16221 bool dependent_p = binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink));
16223 if (dependent_p)
16225 tree name = OVL_NAME (fns);
16226 if (IDENTIFIER_CONV_OP_P (name))
16227 name = make_conv_op_name (optype);
16229 /* See maybe_dependent_member_ref. */
16230 if (dependent_scope_p (qualifying_scope))
16232 if (template_id_p)
16233 name = build2 (TEMPLATE_ID_EXPR, unknown_type_node, name,
16234 template_args);
16235 return build_qualified_name (NULL_TREE, qualifying_scope, name,
16236 /* ::template */false);
16239 if (name == complete_dtor_identifier)
16240 /* Treat as-if non-dependent below. */
16241 dependent_p = false;
16243 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1,
16244 complain);
16245 if (!baselink)
16247 if ((complain & tf_error)
16248 && constructor_name_p (name, qualifying_scope))
16249 error ("cannot call constructor %<%T::%D%> directly",
16250 qualifying_scope, name);
16251 return error_mark_node;
16254 if (BASELINK_P (baselink))
16255 fns = BASELINK_FUNCTIONS (baselink);
16257 else
16259 /* We're going to overwrite pieces below, make a duplicate. */
16260 baselink = copy_node (baselink);
16262 if (qualifying_scope != BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink)))
16264 /* The decl we found was from non-dependent scope, but we still need
16265 to update the binfos for the instantiated qualifying_scope. */
16266 BASELINK_ACCESS_BINFO (baselink) = TYPE_BINFO (qualifying_scope);
16267 BASELINK_BINFO (baselink) = lookup_base (qualifying_scope, binfo_type,
16268 ba_unique, nullptr, complain);
16272 /* If lookup found a single function, mark it as used at this point.
16273 (If lookup found multiple functions the one selected later by
16274 overload resolution will be marked as used at that point.) */
16275 if (!template_id_p && !really_overloaded_fn (fns))
16277 tree fn = OVL_FIRST (fns);
16278 bool ok = mark_used (fn, complain);
16279 if (!ok && !(complain & tf_error))
16280 return error_mark_node;
16281 if (ok && BASELINK_P (baselink))
16282 /* We might have instantiated an auto function. */
16283 TREE_TYPE (baselink) = TREE_TYPE (fn);
16286 if (BASELINK_P (baselink))
16288 /* Add back the template arguments, if present. */
16289 if (template_id_p)
16290 BASELINK_FUNCTIONS (baselink)
16291 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
16293 /* Update the conversion operator type. */
16294 BASELINK_OPTYPE (baselink) = optype;
16297 if (!object_type)
16298 object_type = current_class_type;
16300 if (qualified_p || !dependent_p)
16302 baselink = adjust_result_of_qualified_name_lookup (baselink,
16303 qualifying_scope,
16304 object_type);
16305 if (!qualified_p)
16306 /* We need to call adjust_result_of_qualified_name_lookup in case the
16307 destructor names a base class, but we unset BASELINK_QUALIFIED_P
16308 so that we still get virtual function binding. */
16309 BASELINK_QUALIFIED_P (baselink) = false;
16312 return baselink;
16315 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
16316 true if the qualified-id will be a postfix-expression in-and-of
16317 itself; false if more of the postfix-expression follows the
16318 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
16319 of "&". */
16321 static tree
16322 tsubst_qualified_id (tree qualified_id, tree args,
16323 tsubst_flags_t complain, tree in_decl,
16324 bool done, bool address_p)
16326 tree expr;
16327 tree scope;
16328 tree name;
16329 bool is_template;
16330 tree template_args;
16331 location_t loc = EXPR_LOCATION (qualified_id);
16333 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
16335 /* Figure out what name to look up. */
16336 name = TREE_OPERAND (qualified_id, 1);
16337 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16339 is_template = true;
16340 template_args = TREE_OPERAND (name, 1);
16341 if (template_args)
16342 template_args = tsubst_template_args (template_args, args,
16343 complain, in_decl);
16344 if (template_args == error_mark_node)
16345 return error_mark_node;
16346 name = TREE_OPERAND (name, 0);
16348 else
16350 is_template = false;
16351 template_args = NULL_TREE;
16354 /* Substitute into the qualifying scope. When there are no ARGS, we
16355 are just trying to simplify a non-dependent expression. In that
16356 case the qualifying scope may be dependent, and, in any case,
16357 substituting will not help. */
16358 scope = TREE_OPERAND (qualified_id, 0);
16359 if (args)
16361 scope = tsubst (scope, args, complain, in_decl);
16362 expr = tsubst_copy (name, args, complain, in_decl);
16364 else
16365 expr = name;
16367 if (dependent_scope_p (scope))
16369 if (is_template)
16370 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
16371 tree r = build_qualified_name (NULL_TREE, scope, expr,
16372 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
16373 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
16374 return r;
16377 if (!BASELINK_P (name) && !DECL_P (expr))
16379 if (TREE_CODE (expr) == BIT_NOT_EXPR)
16381 /* A BIT_NOT_EXPR is used to represent a destructor. */
16382 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
16384 error ("qualifying type %qT does not match destructor name ~%qT",
16385 scope, TREE_OPERAND (expr, 0));
16386 expr = error_mark_node;
16388 else
16389 expr = lookup_qualified_name (scope, complete_dtor_identifier,
16390 LOOK_want::NORMAL, false);
16392 else
16393 expr = lookup_qualified_name (scope, expr, LOOK_want::NORMAL, false);
16394 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
16395 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
16397 if (complain & tf_error)
16399 error ("dependent-name %qE is parsed as a non-type, but "
16400 "instantiation yields a type", qualified_id);
16401 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
16403 return error_mark_node;
16407 if (DECL_P (expr))
16409 if (!check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
16410 scope, complain))
16411 return error_mark_node;
16412 /* Remember that there was a reference to this entity. */
16413 if (!mark_used (expr, complain) && !(complain & tf_error))
16414 return error_mark_node;
16417 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
16419 if (complain & tf_error)
16420 qualified_name_lookup_error (scope,
16421 TREE_OPERAND (qualified_id, 1),
16422 expr, input_location);
16423 return error_mark_node;
16426 if (is_template)
16428 /* We may be repeating a check already done during parsing, but
16429 if it was well-formed and passed then, it will pass again
16430 now, and if it didn't, we wouldn't have got here. The case
16431 we want to catch is when we couldn't tell then, and can now,
16432 namely when templ prior to substitution was an
16433 identifier. */
16434 if (flag_concepts && check_auto_in_tmpl_args (expr, template_args))
16435 return error_mark_node;
16437 if (variable_template_p (expr))
16438 expr = lookup_and_finish_template_variable (expr, template_args,
16439 complain);
16440 else
16441 expr = lookup_template_function (expr, template_args);
16444 if (expr == error_mark_node && complain & tf_error)
16445 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
16446 expr, input_location);
16447 else if (TYPE_P (scope))
16449 expr = (adjust_result_of_qualified_name_lookup
16450 (expr, scope, current_nonlambda_class_type ()));
16451 expr = (finish_qualified_id_expr
16452 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
16453 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
16454 /*template_arg_p=*/false, complain));
16457 /* Expressions do not generally have reference type. */
16458 if (TREE_CODE (expr) != SCOPE_REF
16459 /* However, if we're about to form a pointer-to-member, we just
16460 want the referenced member referenced. */
16461 && TREE_CODE (expr) != OFFSET_REF)
16462 expr = convert_from_reference (expr);
16464 if (REF_PARENTHESIZED_P (qualified_id))
16465 expr = force_paren_expr (expr);
16467 expr = maybe_wrap_with_location (expr, loc);
16469 return expr;
16472 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
16473 initializer, DECL is the substituted VAR_DECL. Other arguments are as
16474 for tsubst. */
16476 static tree
16477 tsubst_init (tree init, tree decl, tree args,
16478 tsubst_flags_t complain, tree in_decl)
16480 if (!init)
16481 return NULL_TREE;
16483 init = tsubst_expr (init, args, complain, in_decl, false);
16485 tree type = TREE_TYPE (decl);
16487 if (!init && type != error_mark_node)
16489 if (tree auto_node = type_uses_auto (type))
16491 if (!CLASS_PLACEHOLDER_TEMPLATE (auto_node))
16493 if (complain & tf_error)
16494 error ("initializer for %q#D expands to an empty list "
16495 "of expressions", decl);
16496 return error_mark_node;
16499 else if (!dependent_type_p (type))
16501 /* If we had an initializer but it
16502 instantiated to nothing,
16503 value-initialize the object. This will
16504 only occur when the initializer was a
16505 pack expansion where the parameter packs
16506 used in that expansion were of length
16507 zero. */
16508 init = build_value_init (type, complain);
16509 if (TREE_CODE (init) == AGGR_INIT_EXPR)
16510 init = get_target_expr_sfinae (init, complain);
16511 if (TREE_CODE (init) == TARGET_EXPR)
16512 TARGET_EXPR_DIRECT_INIT_P (init) = true;
16516 return init;
16519 /* If T is a reference to a dependent member of the current instantiation C and
16520 we are trying to refer to that member in a partial instantiation of C,
16521 return a SCOPE_REF; otherwise, return NULL_TREE.
16523 This can happen when forming a C++17 deduction guide, as in PR96199. */
16525 static tree
16526 maybe_dependent_member_ref (tree t, tree args, tsubst_flags_t complain,
16527 tree in_decl)
16529 if (cxx_dialect < cxx17)
16530 return NULL_TREE;
16532 tree ctx = context_for_name_lookup (t);
16533 if (!CLASS_TYPE_P (ctx))
16534 return NULL_TREE;
16536 ctx = tsubst (ctx, args, complain, in_decl);
16537 if (dependent_scope_p (ctx))
16538 return build_qualified_name (NULL_TREE, ctx, DECL_NAME (t),
16539 /*template_p=*/false);
16541 return NULL_TREE;
16544 /* Like tsubst, but deals with expressions. This function just replaces
16545 template parms; to finish processing the resultant expression, use
16546 tsubst_copy_and_build or tsubst_expr. */
16548 static tree
16549 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16551 enum tree_code code;
16552 tree r;
16554 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
16555 return t;
16557 code = TREE_CODE (t);
16559 switch (code)
16561 case PARM_DECL:
16562 r = retrieve_local_specialization (t);
16564 if (r == NULL_TREE)
16566 /* We get here for a use of 'this' in an NSDMI. */
16567 if (DECL_NAME (t) == this_identifier && current_class_ptr)
16568 return current_class_ptr;
16570 /* This can happen for a parameter name used later in a function
16571 declaration (such as in a late-specified return type). Just
16572 make a dummy decl, since it's only used for its type. */
16573 gcc_assert (cp_unevaluated_operand != 0);
16574 r = tsubst_decl (t, args, complain);
16575 /* Give it the template pattern as its context; its true context
16576 hasn't been instantiated yet and this is good enough for
16577 mangling. */
16578 DECL_CONTEXT (r) = DECL_CONTEXT (t);
16581 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
16582 r = argument_pack_select_arg (r);
16583 if (!mark_used (r, complain) && !(complain & tf_error))
16584 return error_mark_node;
16585 return r;
16587 case CONST_DECL:
16589 tree enum_type;
16590 tree v;
16592 if (DECL_TEMPLATE_PARM_P (t))
16593 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
16594 /* There is no need to substitute into namespace-scope
16595 enumerators. */
16596 if (DECL_NAMESPACE_SCOPE_P (t))
16597 return t;
16598 /* If ARGS is NULL, then T is known to be non-dependent. */
16599 if (args == NULL_TREE)
16600 return scalar_constant_value (t);
16602 if (tree ref = maybe_dependent_member_ref (t, args, complain, in_decl))
16603 return ref;
16605 /* Unfortunately, we cannot just call lookup_name here.
16606 Consider:
16608 template <int I> int f() {
16609 enum E { a = I };
16610 struct S { void g() { E e = a; } };
16613 When we instantiate f<7>::S::g(), say, lookup_name is not
16614 clever enough to find f<7>::a. */
16615 enum_type
16616 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
16617 /*entering_scope=*/0);
16619 for (v = TYPE_VALUES (enum_type);
16620 v != NULL_TREE;
16621 v = TREE_CHAIN (v))
16622 if (TREE_PURPOSE (v) == DECL_NAME (t))
16623 return TREE_VALUE (v);
16625 /* We didn't find the name. That should never happen; if
16626 name-lookup found it during preliminary parsing, we
16627 should find it again here during instantiation. */
16628 gcc_unreachable ();
16630 return t;
16632 case FIELD_DECL:
16633 if (DECL_CONTEXT (t))
16635 tree ctx;
16637 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
16638 /*entering_scope=*/1);
16639 if (ctx != DECL_CONTEXT (t))
16641 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
16642 if (!r)
16644 if (complain & tf_error)
16645 error ("using invalid field %qD", t);
16646 return error_mark_node;
16648 return r;
16652 return t;
16654 case VAR_DECL:
16655 if (tree ref = maybe_dependent_member_ref (t, args, complain, in_decl))
16656 return ref;
16657 gcc_fallthrough();
16658 case FUNCTION_DECL:
16659 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
16660 r = tsubst (t, args, complain, in_decl);
16661 else if (DECL_LOCAL_DECL_P (t))
16663 /* Local specialization will usually have been created when
16664 we instantiated the DECL_EXPR_DECL. */
16665 r = retrieve_local_specialization (t);
16666 if (!r)
16668 /* We're in a generic lambda referencing a local extern
16669 from an outer block-scope of a non-template. */
16670 gcc_checking_assert (LAMBDA_FUNCTION_P (current_function_decl));
16671 r = t;
16674 else if (local_variable_p (t)
16675 && uses_template_parms (DECL_CONTEXT (t)))
16677 r = retrieve_local_specialization (t);
16678 if (r == NULL_TREE)
16680 /* First try name lookup to find the instantiation. */
16681 r = lookup_name (DECL_NAME (t));
16682 if (r)
16684 if (!VAR_P (r))
16686 /* During error-recovery we may find a non-variable,
16687 even an OVERLOAD: just bail out and avoid ICEs and
16688 duplicate diagnostics (c++/62207). */
16689 gcc_assert (seen_error ());
16690 return error_mark_node;
16692 if (!is_capture_proxy (r))
16694 /* Make sure the one we found is the one we want. */
16695 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
16696 if (ctx != DECL_CONTEXT (r))
16697 r = NULL_TREE;
16701 if (r)
16702 /* OK */;
16703 else
16705 /* This can happen for a variable used in a
16706 late-specified return type of a local lambda, or for a
16707 local static or constant. Building a new VAR_DECL
16708 should be OK in all those cases. */
16709 r = tsubst_decl (t, args, complain);
16710 if (local_specializations)
16711 /* Avoid infinite recursion (79640). */
16712 register_local_specialization (r, t);
16713 if (decl_maybe_constant_var_p (r))
16715 /* We can't call cp_finish_decl, so handle the
16716 initializer by hand. */
16717 tree init = tsubst_init (DECL_INITIAL (t), r, args,
16718 complain, in_decl);
16719 if (!processing_template_decl)
16720 init = maybe_constant_init (init);
16721 if (processing_template_decl
16722 ? potential_constant_expression (init)
16723 : reduced_constant_expression_p (init))
16724 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
16725 = TREE_CONSTANT (r) = true;
16726 DECL_INITIAL (r) = init;
16727 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
16728 TREE_TYPE (r)
16729 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
16730 complain, adc_variable_type);
16732 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
16733 || decl_constant_var_p (r)
16734 || seen_error ());
16735 if (!processing_template_decl
16736 && !TREE_STATIC (r))
16737 r = process_outer_var_ref (r, complain);
16739 /* Remember this for subsequent uses. */
16740 if (local_specializations)
16741 register_local_specialization (r, t);
16743 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
16744 r = argument_pack_select_arg (r);
16746 else
16747 r = t;
16748 if (!mark_used (r, complain))
16749 return error_mark_node;
16750 return r;
16752 case NAMESPACE_DECL:
16753 return t;
16755 case OVERLOAD:
16756 return t;
16758 case BASELINK:
16759 return tsubst_baselink (t, current_nonlambda_class_type (),
16760 args, complain, in_decl);
16762 case TEMPLATE_DECL:
16763 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
16764 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
16765 args, complain, in_decl);
16766 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
16767 return tsubst (t, args, complain, in_decl);
16768 else if (DECL_CLASS_SCOPE_P (t)
16769 && uses_template_parms (DECL_CONTEXT (t)))
16771 /* Template template argument like the following example need
16772 special treatment:
16774 template <template <class> class TT> struct C {};
16775 template <class T> struct D {
16776 template <class U> struct E {};
16777 C<E> c; // #1
16779 D<int> d; // #2
16781 We are processing the template argument `E' in #1 for
16782 the template instantiation #2. Originally, `E' is a
16783 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
16784 have to substitute this with one having context `D<int>'. */
16786 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
16787 if (dependent_scope_p (context))
16789 /* When rewriting a constructor into a deduction guide, a
16790 non-dependent name can become dependent, so memtmpl<args>
16791 becomes context::template memtmpl<args>. */
16792 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16793 return build_qualified_name (type, context, DECL_NAME (t),
16794 /*template*/true);
16796 return lookup_field (context, DECL_NAME(t), 0, false);
16798 else
16799 /* Ordinary template template argument. */
16800 return t;
16802 case NON_LVALUE_EXPR:
16803 case VIEW_CONVERT_EXPR:
16805 /* Handle location wrappers by substituting the wrapped node
16806 first, *then* reusing the resulting type. Doing the type
16807 first ensures that we handle template parameters and
16808 parameter pack expansions. */
16809 if (location_wrapper_p (t))
16811 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args,
16812 complain, in_decl);
16813 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
16815 tree op = TREE_OPERAND (t, 0);
16816 if (code == VIEW_CONVERT_EXPR
16817 && TREE_CODE (op) == TEMPLATE_PARM_INDEX)
16819 /* Wrapper to make a C++20 template parameter object const. */
16820 op = tsubst_copy (op, args, complain, in_decl);
16821 if (!CP_TYPE_CONST_P (TREE_TYPE (op)))
16823 /* The template argument is not const, presumably because
16824 it is still dependent, and so not the const template parm
16825 object. */
16826 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16827 gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p
16828 (type, TREE_TYPE (op)));
16829 if (TREE_CODE (op) == CONSTRUCTOR
16830 || TREE_CODE (op) == IMPLICIT_CONV_EXPR)
16832 /* Don't add a wrapper to these. */
16833 op = copy_node (op);
16834 TREE_TYPE (op) = type;
16836 else
16837 /* Do add a wrapper otherwise (in particular, if op is
16838 another TEMPLATE_PARM_INDEX). */
16839 op = build1 (code, type, op);
16841 return op;
16843 /* force_paren_expr can also create a VIEW_CONVERT_EXPR. */
16844 else if (code == VIEW_CONVERT_EXPR && REF_PARENTHESIZED_P (t))
16846 op = tsubst_copy (op, args, complain, in_decl);
16847 op = build1 (code, TREE_TYPE (op), op);
16848 REF_PARENTHESIZED_P (op) = true;
16849 return op;
16851 /* We shouldn't see any other uses of these in templates. */
16852 gcc_unreachable ();
16855 case CAST_EXPR:
16856 case REINTERPRET_CAST_EXPR:
16857 case CONST_CAST_EXPR:
16858 case STATIC_CAST_EXPR:
16859 case DYNAMIC_CAST_EXPR:
16860 case IMPLICIT_CONV_EXPR:
16861 case CONVERT_EXPR:
16862 case NOP_EXPR:
16864 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16865 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16866 return build1 (code, type, op0);
16869 case BIT_CAST_EXPR:
16871 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16872 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16873 r = build_min (BIT_CAST_EXPR, type, op0);
16874 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
16875 return r;
16878 case SIZEOF_EXPR:
16879 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
16880 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
16882 tree expanded, op = TREE_OPERAND (t, 0);
16883 int len = 0;
16885 if (SIZEOF_EXPR_TYPE_P (t))
16886 op = TREE_TYPE (op);
16888 ++cp_unevaluated_operand;
16889 ++c_inhibit_evaluation_warnings;
16890 /* We only want to compute the number of arguments. */
16891 if (PACK_EXPANSION_P (op))
16892 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
16893 else
16894 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
16895 args, complain, in_decl);
16896 --cp_unevaluated_operand;
16897 --c_inhibit_evaluation_warnings;
16899 if (TREE_CODE (expanded) == TREE_VEC)
16901 len = TREE_VEC_LENGTH (expanded);
16902 /* Set TREE_USED for the benefit of -Wunused. */
16903 for (int i = 0; i < len; i++)
16904 if (DECL_P (TREE_VEC_ELT (expanded, i)))
16905 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
16908 if (expanded == error_mark_node)
16909 return error_mark_node;
16910 else if (PACK_EXPANSION_P (expanded)
16911 || (TREE_CODE (expanded) == TREE_VEC
16912 && pack_expansion_args_count (expanded)))
16915 if (PACK_EXPANSION_P (expanded))
16916 /* OK. */;
16917 else if (TREE_VEC_LENGTH (expanded) == 1)
16918 expanded = TREE_VEC_ELT (expanded, 0);
16919 else
16920 expanded = make_argument_pack (expanded);
16922 if (TYPE_P (expanded))
16923 return cxx_sizeof_or_alignof_type (input_location,
16924 expanded, SIZEOF_EXPR,
16925 false,
16926 complain & tf_error);
16927 else
16928 return cxx_sizeof_or_alignof_expr (input_location,
16929 expanded, SIZEOF_EXPR,
16930 false,
16931 complain & tf_error);
16933 else
16934 return build_int_cst (size_type_node, len);
16936 if (SIZEOF_EXPR_TYPE_P (t))
16938 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
16939 args, complain, in_decl);
16940 r = build1 (NOP_EXPR, r, error_mark_node);
16941 r = build1 (SIZEOF_EXPR,
16942 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
16943 SIZEOF_EXPR_TYPE_P (r) = 1;
16944 return r;
16946 /* Fall through */
16948 case INDIRECT_REF:
16949 case NEGATE_EXPR:
16950 case TRUTH_NOT_EXPR:
16951 case BIT_NOT_EXPR:
16952 case ADDR_EXPR:
16953 case UNARY_PLUS_EXPR: /* Unary + */
16954 case ALIGNOF_EXPR:
16955 case AT_ENCODE_EXPR:
16956 case ARROW_EXPR:
16957 case THROW_EXPR:
16958 case TYPEID_EXPR:
16959 case REALPART_EXPR:
16960 case IMAGPART_EXPR:
16961 case PAREN_EXPR:
16963 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16964 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16965 r = build1 (code, type, op0);
16966 if (code == ALIGNOF_EXPR)
16967 ALIGNOF_EXPR_STD_P (r) = ALIGNOF_EXPR_STD_P (t);
16968 return r;
16971 case COMPONENT_REF:
16973 tree object;
16974 tree name;
16976 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
16977 name = TREE_OPERAND (t, 1);
16978 if (TREE_CODE (name) == BIT_NOT_EXPR)
16980 name = tsubst_copy (TREE_OPERAND (name, 0), args,
16981 complain, in_decl);
16982 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
16984 else if (TREE_CODE (name) == SCOPE_REF
16985 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
16987 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
16988 complain, in_decl);
16989 name = TREE_OPERAND (name, 1);
16990 name = tsubst_copy (TREE_OPERAND (name, 0), args,
16991 complain, in_decl);
16992 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
16993 name = build_qualified_name (/*type=*/NULL_TREE,
16994 base, name,
16995 /*template_p=*/false);
16997 else if (BASELINK_P (name))
16998 name = tsubst_baselink (name,
16999 non_reference (TREE_TYPE (object)),
17000 args, complain,
17001 in_decl);
17002 else
17003 name = tsubst_copy (name, args, complain, in_decl);
17004 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
17007 case PLUS_EXPR:
17008 case MINUS_EXPR:
17009 case MULT_EXPR:
17010 case TRUNC_DIV_EXPR:
17011 case CEIL_DIV_EXPR:
17012 case FLOOR_DIV_EXPR:
17013 case ROUND_DIV_EXPR:
17014 case EXACT_DIV_EXPR:
17015 case BIT_AND_EXPR:
17016 case BIT_IOR_EXPR:
17017 case BIT_XOR_EXPR:
17018 case TRUNC_MOD_EXPR:
17019 case FLOOR_MOD_EXPR:
17020 case TRUTH_ANDIF_EXPR:
17021 case TRUTH_ORIF_EXPR:
17022 case TRUTH_AND_EXPR:
17023 case TRUTH_OR_EXPR:
17024 case RSHIFT_EXPR:
17025 case LSHIFT_EXPR:
17026 case EQ_EXPR:
17027 case NE_EXPR:
17028 case MAX_EXPR:
17029 case MIN_EXPR:
17030 case LE_EXPR:
17031 case GE_EXPR:
17032 case LT_EXPR:
17033 case GT_EXPR:
17034 case COMPOUND_EXPR:
17035 case DOTSTAR_EXPR:
17036 case MEMBER_REF:
17037 case PREDECREMENT_EXPR:
17038 case PREINCREMENT_EXPR:
17039 case POSTDECREMENT_EXPR:
17040 case POSTINCREMENT_EXPR:
17042 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17043 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17044 return build_nt (code, op0, op1);
17047 case SCOPE_REF:
17049 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17050 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17051 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
17052 QUALIFIED_NAME_IS_TEMPLATE (t));
17055 case ARRAY_REF:
17057 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17058 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17059 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
17062 case CALL_EXPR:
17064 int n = VL_EXP_OPERAND_LENGTH (t);
17065 tree result = build_vl_exp (CALL_EXPR, n);
17066 int i;
17067 for (i = 0; i < n; i++)
17068 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
17069 complain, in_decl);
17070 return result;
17073 case COND_EXPR:
17074 case MODOP_EXPR:
17075 case PSEUDO_DTOR_EXPR:
17076 case VEC_PERM_EXPR:
17078 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17079 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17080 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
17081 r = build_nt (code, op0, op1, op2);
17082 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17083 return r;
17086 case NEW_EXPR:
17088 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17089 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17090 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
17091 r = build_nt (code, op0, op1, op2);
17092 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
17093 return r;
17096 case DELETE_EXPR:
17098 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17099 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17100 r = build_nt (code, op0, op1);
17101 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
17102 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
17103 return r;
17106 case TEMPLATE_ID_EXPR:
17108 /* Substituted template arguments */
17109 tree tmpl = TREE_OPERAND (t, 0);
17110 tree targs = TREE_OPERAND (t, 1);
17112 tmpl = tsubst_copy (tmpl, args, complain, in_decl);
17113 if (targs)
17114 targs = tsubst_template_args (targs, args, complain, in_decl);
17116 if (variable_template_p (tmpl))
17117 return lookup_template_variable (tmpl, targs);
17118 else
17119 return lookup_template_function (tmpl, targs);
17122 case TREE_LIST:
17124 tree purpose, value, chain;
17126 if (t == void_list_node)
17127 return t;
17129 purpose = TREE_PURPOSE (t);
17130 if (purpose)
17131 purpose = tsubst_copy (purpose, args, complain, in_decl);
17132 value = TREE_VALUE (t);
17133 if (value)
17134 value = tsubst_copy (value, args, complain, in_decl);
17135 chain = TREE_CHAIN (t);
17136 if (chain && chain != void_type_node)
17137 chain = tsubst_copy (chain, args, complain, in_decl);
17138 if (purpose == TREE_PURPOSE (t)
17139 && value == TREE_VALUE (t)
17140 && chain == TREE_CHAIN (t))
17141 return t;
17142 return tree_cons (purpose, value, chain);
17145 case RECORD_TYPE:
17146 case UNION_TYPE:
17147 case ENUMERAL_TYPE:
17148 case INTEGER_TYPE:
17149 case TEMPLATE_TYPE_PARM:
17150 case TEMPLATE_TEMPLATE_PARM:
17151 case BOUND_TEMPLATE_TEMPLATE_PARM:
17152 case TEMPLATE_PARM_INDEX:
17153 case POINTER_TYPE:
17154 case REFERENCE_TYPE:
17155 case OFFSET_TYPE:
17156 case FUNCTION_TYPE:
17157 case METHOD_TYPE:
17158 case ARRAY_TYPE:
17159 case TYPENAME_TYPE:
17160 case UNBOUND_CLASS_TEMPLATE:
17161 case TYPEOF_TYPE:
17162 case DECLTYPE_TYPE:
17163 case TYPE_DECL:
17164 return tsubst (t, args, complain, in_decl);
17166 case USING_DECL:
17167 t = DECL_NAME (t);
17168 /* Fall through. */
17169 case IDENTIFIER_NODE:
17170 if (IDENTIFIER_CONV_OP_P (t))
17172 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17173 return make_conv_op_name (new_type);
17175 else
17176 return t;
17178 case CONSTRUCTOR:
17179 /* This is handled by tsubst_copy_and_build. */
17180 gcc_unreachable ();
17182 case VA_ARG_EXPR:
17184 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17185 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17186 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
17189 case CLEANUP_POINT_EXPR:
17190 /* We shouldn't have built any of these during initial template
17191 generation. Instead, they should be built during instantiation
17192 in response to the saved STMT_IS_FULL_EXPR_P setting. */
17193 gcc_unreachable ();
17195 case OFFSET_REF:
17197 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17198 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
17199 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
17200 r = build2 (code, type, op0, op1);
17201 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
17202 if (!mark_used (TREE_OPERAND (r, 1), complain)
17203 && !(complain & tf_error))
17204 return error_mark_node;
17205 return r;
17208 case EXPR_PACK_EXPANSION:
17209 error ("invalid use of pack expansion expression");
17210 return error_mark_node;
17212 case NONTYPE_ARGUMENT_PACK:
17213 error ("use %<...%> to expand argument pack");
17214 return error_mark_node;
17216 case VOID_CST:
17217 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
17218 return t;
17220 case INTEGER_CST:
17221 case REAL_CST:
17222 case COMPLEX_CST:
17224 /* Instantiate any typedefs in the type. */
17225 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17226 r = fold_convert (type, t);
17227 gcc_assert (TREE_CODE (r) == code);
17228 return r;
17231 case STRING_CST:
17233 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17234 r = t;
17235 if (type != TREE_TYPE (t))
17237 r = copy_node (t);
17238 TREE_TYPE (r) = type;
17240 return r;
17243 case PTRMEM_CST:
17244 /* These can sometimes show up in a partial instantiation, but never
17245 involve template parms. */
17246 gcc_assert (!uses_template_parms (t));
17247 return t;
17249 case UNARY_LEFT_FOLD_EXPR:
17250 return tsubst_unary_left_fold (t, args, complain, in_decl);
17251 case UNARY_RIGHT_FOLD_EXPR:
17252 return tsubst_unary_right_fold (t, args, complain, in_decl);
17253 case BINARY_LEFT_FOLD_EXPR:
17254 return tsubst_binary_left_fold (t, args, complain, in_decl);
17255 case BINARY_RIGHT_FOLD_EXPR:
17256 return tsubst_binary_right_fold (t, args, complain, in_decl);
17257 case PREDICT_EXPR:
17258 return t;
17260 case DEBUG_BEGIN_STMT:
17261 /* ??? There's no point in copying it for now, but maybe some
17262 day it will contain more information, such as a pointer back
17263 to the containing function, inlined copy or so. */
17264 return t;
17266 case CO_AWAIT_EXPR:
17267 return tsubst_expr (t, args, complain, in_decl,
17268 /*integral_constant_expression_p=*/false);
17269 break;
17271 default:
17272 /* We shouldn't get here, but keep going if !flag_checking. */
17273 if (flag_checking)
17274 gcc_unreachable ();
17275 return t;
17279 /* Helper function for tsubst_omp_clauses, used for instantiation of
17280 OMP_CLAUSE_DECL of clauses. */
17282 static tree
17283 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
17284 tree in_decl, tree *iterator_cache)
17286 if (decl == NULL_TREE)
17287 return NULL_TREE;
17289 /* Handle OpenMP iterators. */
17290 if (TREE_CODE (decl) == TREE_LIST
17291 && TREE_PURPOSE (decl)
17292 && TREE_CODE (TREE_PURPOSE (decl)) == TREE_VEC)
17294 tree ret;
17295 if (iterator_cache[0] == TREE_PURPOSE (decl))
17296 ret = iterator_cache[1];
17297 else
17299 tree *tp = &ret;
17300 begin_scope (sk_omp, NULL);
17301 for (tree it = TREE_PURPOSE (decl); it; it = TREE_CHAIN (it))
17303 *tp = copy_node (it);
17304 TREE_VEC_ELT (*tp, 0)
17305 = tsubst_decl (TREE_VEC_ELT (it, 0), args, complain);
17306 TREE_VEC_ELT (*tp, 1)
17307 = tsubst_expr (TREE_VEC_ELT (it, 1), args, complain, in_decl,
17308 /*integral_constant_expression_p=*/false);
17309 TREE_VEC_ELT (*tp, 2)
17310 = tsubst_expr (TREE_VEC_ELT (it, 2), args, complain, in_decl,
17311 /*integral_constant_expression_p=*/false);
17312 TREE_VEC_ELT (*tp, 3)
17313 = tsubst_expr (TREE_VEC_ELT (it, 3), args, complain, in_decl,
17314 /*integral_constant_expression_p=*/false);
17315 TREE_CHAIN (*tp) = NULL_TREE;
17316 tp = &TREE_CHAIN (*tp);
17318 TREE_VEC_ELT (ret, 5) = poplevel (1, 1, 0);
17319 iterator_cache[0] = TREE_PURPOSE (decl);
17320 iterator_cache[1] = ret;
17322 return build_tree_list (ret, tsubst_omp_clause_decl (TREE_VALUE (decl),
17323 args, complain,
17324 in_decl, NULL));
17327 /* Handle an OpenMP array section represented as a TREE_LIST (or
17328 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
17329 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
17330 TREE_LIST. We can handle it exactly the same as an array section
17331 (purpose, value, and a chain), even though the nomenclature
17332 (low_bound, length, etc) is different. */
17333 if (TREE_CODE (decl) == TREE_LIST)
17335 tree low_bound
17336 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
17337 /*integral_constant_expression_p=*/false);
17338 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
17339 /*integral_constant_expression_p=*/false);
17340 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
17341 in_decl, NULL);
17342 if (TREE_PURPOSE (decl) == low_bound
17343 && TREE_VALUE (decl) == length
17344 && TREE_CHAIN (decl) == chain)
17345 return decl;
17346 tree ret = tree_cons (low_bound, length, chain);
17347 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
17348 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
17349 return ret;
17351 tree ret = tsubst_expr (decl, args, complain, in_decl,
17352 /*integral_constant_expression_p=*/false);
17353 /* Undo convert_from_reference tsubst_expr could have called. */
17354 if (decl
17355 && REFERENCE_REF_P (ret)
17356 && !REFERENCE_REF_P (decl))
17357 ret = TREE_OPERAND (ret, 0);
17358 return ret;
17361 /* Like tsubst_copy, but specifically for OpenMP clauses. */
17363 static tree
17364 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
17365 tree args, tsubst_flags_t complain, tree in_decl)
17367 tree new_clauses = NULL_TREE, nc, oc;
17368 tree linear_no_step = NULL_TREE;
17369 tree iterator_cache[2] = { NULL_TREE, NULL_TREE };
17371 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
17373 nc = copy_node (oc);
17374 OMP_CLAUSE_CHAIN (nc) = new_clauses;
17375 new_clauses = nc;
17377 switch (OMP_CLAUSE_CODE (nc))
17379 case OMP_CLAUSE_LASTPRIVATE:
17380 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
17382 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
17383 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
17384 in_decl, /*integral_constant_expression_p=*/false);
17385 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
17386 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
17388 /* FALLTHRU */
17389 case OMP_CLAUSE_PRIVATE:
17390 case OMP_CLAUSE_SHARED:
17391 case OMP_CLAUSE_FIRSTPRIVATE:
17392 case OMP_CLAUSE_COPYIN:
17393 case OMP_CLAUSE_COPYPRIVATE:
17394 case OMP_CLAUSE_UNIFORM:
17395 case OMP_CLAUSE_DEPEND:
17396 case OMP_CLAUSE_AFFINITY:
17397 case OMP_CLAUSE_FROM:
17398 case OMP_CLAUSE_TO:
17399 case OMP_CLAUSE_MAP:
17400 case OMP_CLAUSE__CACHE_:
17401 case OMP_CLAUSE_NONTEMPORAL:
17402 case OMP_CLAUSE_USE_DEVICE_PTR:
17403 case OMP_CLAUSE_USE_DEVICE_ADDR:
17404 case OMP_CLAUSE_IS_DEVICE_PTR:
17405 case OMP_CLAUSE_INCLUSIVE:
17406 case OMP_CLAUSE_EXCLUSIVE:
17407 OMP_CLAUSE_DECL (nc)
17408 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17409 in_decl, iterator_cache);
17410 break;
17411 case OMP_CLAUSE_TILE:
17412 case OMP_CLAUSE_IF:
17413 case OMP_CLAUSE_NUM_THREADS:
17414 case OMP_CLAUSE_SCHEDULE:
17415 case OMP_CLAUSE_COLLAPSE:
17416 case OMP_CLAUSE_FINAL:
17417 case OMP_CLAUSE_DEVICE:
17418 case OMP_CLAUSE_DIST_SCHEDULE:
17419 case OMP_CLAUSE_NUM_TEAMS:
17420 case OMP_CLAUSE_THREAD_LIMIT:
17421 case OMP_CLAUSE_SAFELEN:
17422 case OMP_CLAUSE_SIMDLEN:
17423 case OMP_CLAUSE_NUM_TASKS:
17424 case OMP_CLAUSE_GRAINSIZE:
17425 case OMP_CLAUSE_PRIORITY:
17426 case OMP_CLAUSE_ORDERED:
17427 case OMP_CLAUSE_HINT:
17428 case OMP_CLAUSE_NUM_GANGS:
17429 case OMP_CLAUSE_NUM_WORKERS:
17430 case OMP_CLAUSE_VECTOR_LENGTH:
17431 case OMP_CLAUSE_WORKER:
17432 case OMP_CLAUSE_VECTOR:
17433 case OMP_CLAUSE_ASYNC:
17434 case OMP_CLAUSE_WAIT:
17435 case OMP_CLAUSE_DETACH:
17436 OMP_CLAUSE_OPERAND (nc, 0)
17437 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
17438 in_decl, /*integral_constant_expression_p=*/false);
17439 break;
17440 case OMP_CLAUSE_REDUCTION:
17441 case OMP_CLAUSE_IN_REDUCTION:
17442 case OMP_CLAUSE_TASK_REDUCTION:
17443 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
17445 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
17446 if (TREE_CODE (placeholder) == SCOPE_REF)
17448 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
17449 complain, in_decl);
17450 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
17451 = build_qualified_name (NULL_TREE, scope,
17452 TREE_OPERAND (placeholder, 1),
17453 false);
17455 else
17456 gcc_assert (identifier_p (placeholder));
17458 OMP_CLAUSE_DECL (nc)
17459 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17460 in_decl, NULL);
17461 break;
17462 case OMP_CLAUSE_GANG:
17463 case OMP_CLAUSE_ALIGNED:
17464 case OMP_CLAUSE_ALLOCATE:
17465 OMP_CLAUSE_DECL (nc)
17466 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17467 in_decl, NULL);
17468 OMP_CLAUSE_OPERAND (nc, 1)
17469 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
17470 in_decl, /*integral_constant_expression_p=*/false);
17471 break;
17472 case OMP_CLAUSE_LINEAR:
17473 OMP_CLAUSE_DECL (nc)
17474 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
17475 in_decl, NULL);
17476 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
17478 gcc_assert (!linear_no_step);
17479 linear_no_step = nc;
17481 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
17482 OMP_CLAUSE_LINEAR_STEP (nc)
17483 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
17484 complain, in_decl, NULL);
17485 else
17486 OMP_CLAUSE_LINEAR_STEP (nc)
17487 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
17488 in_decl,
17489 /*integral_constant_expression_p=*/false);
17490 break;
17491 case OMP_CLAUSE_NOWAIT:
17492 case OMP_CLAUSE_DEFAULT:
17493 case OMP_CLAUSE_UNTIED:
17494 case OMP_CLAUSE_MERGEABLE:
17495 case OMP_CLAUSE_INBRANCH:
17496 case OMP_CLAUSE_NOTINBRANCH:
17497 case OMP_CLAUSE_PROC_BIND:
17498 case OMP_CLAUSE_FOR:
17499 case OMP_CLAUSE_PARALLEL:
17500 case OMP_CLAUSE_SECTIONS:
17501 case OMP_CLAUSE_TASKGROUP:
17502 case OMP_CLAUSE_NOGROUP:
17503 case OMP_CLAUSE_THREADS:
17504 case OMP_CLAUSE_SIMD:
17505 case OMP_CLAUSE_DEFAULTMAP:
17506 case OMP_CLAUSE_ORDER:
17507 case OMP_CLAUSE_BIND:
17508 case OMP_CLAUSE_INDEPENDENT:
17509 case OMP_CLAUSE_AUTO:
17510 case OMP_CLAUSE_SEQ:
17511 case OMP_CLAUSE_IF_PRESENT:
17512 case OMP_CLAUSE_FINALIZE:
17513 break;
17514 default:
17515 gcc_unreachable ();
17517 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
17518 switch (OMP_CLAUSE_CODE (nc))
17520 case OMP_CLAUSE_SHARED:
17521 case OMP_CLAUSE_PRIVATE:
17522 case OMP_CLAUSE_FIRSTPRIVATE:
17523 case OMP_CLAUSE_LASTPRIVATE:
17524 case OMP_CLAUSE_COPYPRIVATE:
17525 case OMP_CLAUSE_LINEAR:
17526 case OMP_CLAUSE_REDUCTION:
17527 case OMP_CLAUSE_IN_REDUCTION:
17528 case OMP_CLAUSE_TASK_REDUCTION:
17529 case OMP_CLAUSE_USE_DEVICE_PTR:
17530 case OMP_CLAUSE_USE_DEVICE_ADDR:
17531 case OMP_CLAUSE_IS_DEVICE_PTR:
17532 case OMP_CLAUSE_INCLUSIVE:
17533 case OMP_CLAUSE_EXCLUSIVE:
17534 case OMP_CLAUSE_ALLOCATE:
17535 /* tsubst_expr on SCOPE_REF results in returning
17536 finish_non_static_data_member result. Undo that here. */
17537 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
17538 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
17539 == IDENTIFIER_NODE))
17541 tree t = OMP_CLAUSE_DECL (nc);
17542 tree v = t;
17543 while (v)
17544 switch (TREE_CODE (v))
17546 case COMPONENT_REF:
17547 case MEM_REF:
17548 case INDIRECT_REF:
17549 CASE_CONVERT:
17550 case POINTER_PLUS_EXPR:
17551 v = TREE_OPERAND (v, 0);
17552 continue;
17553 case PARM_DECL:
17554 if (DECL_CONTEXT (v) == current_function_decl
17555 && DECL_ARTIFICIAL (v)
17556 && DECL_NAME (v) == this_identifier)
17557 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
17558 /* FALLTHRU */
17559 default:
17560 v = NULL_TREE;
17561 break;
17564 else if (VAR_P (OMP_CLAUSE_DECL (oc))
17565 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
17566 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
17567 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
17568 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
17570 tree decl = OMP_CLAUSE_DECL (nc);
17571 if (VAR_P (decl))
17573 retrofit_lang_decl (decl);
17574 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
17577 break;
17578 default:
17579 break;
17583 new_clauses = nreverse (new_clauses);
17584 if (ort != C_ORT_OMP_DECLARE_SIMD)
17586 new_clauses = finish_omp_clauses (new_clauses, ort);
17587 if (linear_no_step)
17588 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
17589 if (nc == linear_no_step)
17591 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
17592 break;
17595 return new_clauses;
17598 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
17600 static tree
17601 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
17602 tree in_decl)
17604 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
17606 tree purpose, value, chain;
17608 if (t == NULL)
17609 return t;
17611 if (TREE_CODE (t) != TREE_LIST)
17612 return tsubst_copy_and_build (t, args, complain, in_decl,
17613 /*function_p=*/false,
17614 /*integral_constant_expression_p=*/false);
17616 if (t == void_list_node)
17617 return t;
17619 purpose = TREE_PURPOSE (t);
17620 if (purpose)
17621 purpose = RECUR (purpose);
17622 value = TREE_VALUE (t);
17623 if (value)
17625 if (TREE_CODE (value) != LABEL_DECL)
17626 value = RECUR (value);
17627 else
17629 value = lookup_label (DECL_NAME (value));
17630 gcc_assert (TREE_CODE (value) == LABEL_DECL);
17631 TREE_USED (value) = 1;
17634 chain = TREE_CHAIN (t);
17635 if (chain && chain != void_type_node)
17636 chain = RECUR (chain);
17637 return tree_cons (purpose, value, chain);
17638 #undef RECUR
17641 /* Used to temporarily communicate the list of #pragma omp parallel
17642 clauses to #pragma omp for instantiation if they are combined
17643 together. */
17645 static tree *omp_parallel_combined_clauses;
17647 static tree tsubst_decomp_names (tree, tree, tree, tsubst_flags_t, tree,
17648 tree *, unsigned int *);
17650 /* Substitute one OMP_FOR iterator. */
17652 static bool
17653 tsubst_omp_for_iterator (tree t, int i, tree declv, tree &orig_declv,
17654 tree initv, tree condv, tree incrv, tree *clauses,
17655 tree args, tsubst_flags_t complain, tree in_decl,
17656 bool integral_constant_expression_p)
17658 #define RECUR(NODE) \
17659 tsubst_expr ((NODE), args, complain, in_decl, \
17660 integral_constant_expression_p)
17661 tree decl, init, cond = NULL_TREE, incr = NULL_TREE;
17662 bool ret = false;
17664 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
17665 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
17667 decl = TREE_OPERAND (init, 0);
17668 init = TREE_OPERAND (init, 1);
17669 tree decl_expr = NULL_TREE;
17670 bool range_for = TREE_VEC_ELT (OMP_FOR_COND (t), i) == global_namespace;
17671 if (range_for)
17673 bool decomp = false;
17674 if (decl != error_mark_node && DECL_HAS_VALUE_EXPR_P (decl))
17676 tree v = DECL_VALUE_EXPR (decl);
17677 if (TREE_CODE (v) == ARRAY_REF
17678 && VAR_P (TREE_OPERAND (v, 0))
17679 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
17681 tree decomp_first = NULL_TREE;
17682 unsigned decomp_cnt = 0;
17683 tree d = tsubst_decl (TREE_OPERAND (v, 0), args, complain);
17684 maybe_push_decl (d);
17685 d = tsubst_decomp_names (d, TREE_OPERAND (v, 0), args, complain,
17686 in_decl, &decomp_first, &decomp_cnt);
17687 decomp = true;
17688 if (d == error_mark_node)
17689 decl = error_mark_node;
17690 else
17691 for (unsigned int i = 0; i < decomp_cnt; i++)
17693 if (!DECL_HAS_VALUE_EXPR_P (decomp_first))
17695 tree v = build_nt (ARRAY_REF, d,
17696 size_int (decomp_cnt - i - 1),
17697 NULL_TREE, NULL_TREE);
17698 SET_DECL_VALUE_EXPR (decomp_first, v);
17699 DECL_HAS_VALUE_EXPR_P (decomp_first) = 1;
17701 fit_decomposition_lang_decl (decomp_first, d);
17702 decomp_first = DECL_CHAIN (decomp_first);
17706 decl = tsubst_decl (decl, args, complain);
17707 if (!decomp)
17708 maybe_push_decl (decl);
17710 else if (init && TREE_CODE (init) == DECL_EXPR)
17712 /* We need to jump through some hoops to handle declarations in the
17713 init-statement, since we might need to handle auto deduction,
17714 but we need to keep control of initialization. */
17715 decl_expr = init;
17716 init = DECL_INITIAL (DECL_EXPR_DECL (init));
17717 decl = tsubst_decl (decl, args, complain);
17719 else
17721 if (TREE_CODE (decl) == SCOPE_REF)
17723 decl = RECUR (decl);
17724 if (TREE_CODE (decl) == COMPONENT_REF)
17726 tree v = decl;
17727 while (v)
17728 switch (TREE_CODE (v))
17730 case COMPONENT_REF:
17731 case MEM_REF:
17732 case INDIRECT_REF:
17733 CASE_CONVERT:
17734 case POINTER_PLUS_EXPR:
17735 v = TREE_OPERAND (v, 0);
17736 continue;
17737 case PARM_DECL:
17738 if (DECL_CONTEXT (v) == current_function_decl
17739 && DECL_ARTIFICIAL (v)
17740 && DECL_NAME (v) == this_identifier)
17742 decl = TREE_OPERAND (decl, 1);
17743 decl = omp_privatize_field (decl, false);
17745 /* FALLTHRU */
17746 default:
17747 v = NULL_TREE;
17748 break;
17752 else
17753 decl = RECUR (decl);
17755 if (init && TREE_CODE (init) == TREE_VEC)
17757 init = copy_node (init);
17758 TREE_VEC_ELT (init, 0)
17759 = tsubst_decl (TREE_VEC_ELT (init, 0), args, complain);
17760 TREE_VEC_ELT (init, 1) = RECUR (TREE_VEC_ELT (init, 1));
17761 TREE_VEC_ELT (init, 2) = RECUR (TREE_VEC_ELT (init, 2));
17763 else
17764 init = RECUR (init);
17766 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
17768 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
17769 if (TREE_CODE (o) == TREE_LIST)
17770 TREE_VEC_ELT (orig_declv, i)
17771 = tree_cons (RECUR (TREE_PURPOSE (o)),
17772 RECUR (TREE_VALUE (o)),
17773 NULL_TREE);
17774 else
17775 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
17778 if (range_for)
17780 tree this_pre_body = NULL_TREE;
17781 tree orig_init = NULL_TREE;
17782 tree orig_decl = NULL_TREE;
17783 cp_convert_omp_range_for (this_pre_body, NULL, decl, orig_decl, init,
17784 orig_init, cond, incr);
17785 if (orig_decl)
17787 if (orig_declv == NULL_TREE)
17788 orig_declv = copy_node (declv);
17789 TREE_VEC_ELT (orig_declv, i) = orig_decl;
17790 ret = true;
17792 else if (orig_declv)
17793 TREE_VEC_ELT (orig_declv, i) = decl;
17796 tree auto_node = type_uses_auto (TREE_TYPE (decl));
17797 if (!range_for && auto_node && init)
17798 TREE_TYPE (decl)
17799 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
17801 gcc_assert (!type_dependent_expression_p (decl));
17803 if (!CLASS_TYPE_P (TREE_TYPE (decl)) || range_for)
17805 if (decl_expr)
17807 /* Declare the variable, but don't let that initialize it. */
17808 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
17809 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
17810 RECUR (decl_expr);
17811 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
17814 if (!range_for)
17816 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
17817 if (COMPARISON_CLASS_P (cond)
17818 && TREE_CODE (TREE_OPERAND (cond, 1)) == TREE_VEC)
17820 tree lhs = RECUR (TREE_OPERAND (cond, 0));
17821 tree rhs = copy_node (TREE_OPERAND (cond, 1));
17822 TREE_VEC_ELT (rhs, 0)
17823 = tsubst_decl (TREE_VEC_ELT (rhs, 0), args, complain);
17824 TREE_VEC_ELT (rhs, 1) = RECUR (TREE_VEC_ELT (rhs, 1));
17825 TREE_VEC_ELT (rhs, 2) = RECUR (TREE_VEC_ELT (rhs, 2));
17826 cond = build2 (TREE_CODE (cond), TREE_TYPE (cond),
17827 lhs, rhs);
17829 else
17830 cond = RECUR (cond);
17831 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
17832 if (TREE_CODE (incr) == MODIFY_EXPR)
17834 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17835 tree rhs = RECUR (TREE_OPERAND (incr, 1));
17836 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
17837 NOP_EXPR, rhs, complain);
17839 else
17840 incr = RECUR (incr);
17841 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
17842 TREE_VEC_ELT (orig_declv, i) = decl;
17844 TREE_VEC_ELT (declv, i) = decl;
17845 TREE_VEC_ELT (initv, i) = init;
17846 TREE_VEC_ELT (condv, i) = cond;
17847 TREE_VEC_ELT (incrv, i) = incr;
17848 return ret;
17851 if (decl_expr)
17853 /* Declare and initialize the variable. */
17854 RECUR (decl_expr);
17855 init = NULL_TREE;
17857 else if (init)
17859 tree *pc;
17860 int j;
17861 for (j = ((omp_parallel_combined_clauses == NULL
17862 || TREE_CODE (t) == OMP_LOOP) ? 1 : 0); j < 2; j++)
17864 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
17866 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
17867 && OMP_CLAUSE_DECL (*pc) == decl)
17868 break;
17869 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
17870 && OMP_CLAUSE_DECL (*pc) == decl)
17872 if (j)
17873 break;
17874 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
17875 tree c = *pc;
17876 *pc = OMP_CLAUSE_CHAIN (c);
17877 OMP_CLAUSE_CHAIN (c) = *clauses;
17878 *clauses = c;
17880 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
17881 && OMP_CLAUSE_DECL (*pc) == decl)
17883 error ("iteration variable %qD should not be firstprivate",
17884 decl);
17885 *pc = OMP_CLAUSE_CHAIN (*pc);
17887 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
17888 && OMP_CLAUSE_DECL (*pc) == decl)
17890 error ("iteration variable %qD should not be reduction",
17891 decl);
17892 *pc = OMP_CLAUSE_CHAIN (*pc);
17894 else
17895 pc = &OMP_CLAUSE_CHAIN (*pc);
17897 if (*pc)
17898 break;
17900 if (*pc == NULL_TREE)
17902 tree c = build_omp_clause (input_location,
17903 TREE_CODE (t) == OMP_LOOP
17904 ? OMP_CLAUSE_LASTPRIVATE
17905 : OMP_CLAUSE_PRIVATE);
17906 OMP_CLAUSE_DECL (c) = decl;
17907 c = finish_omp_clauses (c, C_ORT_OMP);
17908 if (c)
17910 OMP_CLAUSE_CHAIN (c) = *clauses;
17911 *clauses = c;
17915 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
17916 if (COMPARISON_CLASS_P (cond))
17918 tree op0 = RECUR (TREE_OPERAND (cond, 0));
17919 tree op1 = RECUR (TREE_OPERAND (cond, 1));
17920 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
17922 else
17923 cond = RECUR (cond);
17924 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
17925 switch (TREE_CODE (incr))
17927 case PREINCREMENT_EXPR:
17928 case PREDECREMENT_EXPR:
17929 case POSTINCREMENT_EXPR:
17930 case POSTDECREMENT_EXPR:
17931 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
17932 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
17933 break;
17934 case MODIFY_EXPR:
17935 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
17936 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
17938 tree rhs = TREE_OPERAND (incr, 1);
17939 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17940 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
17941 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
17942 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17943 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
17944 rhs0, rhs1));
17946 else
17947 incr = RECUR (incr);
17948 break;
17949 case MODOP_EXPR:
17950 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
17951 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
17953 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17954 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17955 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
17956 TREE_TYPE (decl), lhs,
17957 RECUR (TREE_OPERAND (incr, 2))));
17959 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
17960 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
17961 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
17963 tree rhs = TREE_OPERAND (incr, 2);
17964 tree lhs = RECUR (TREE_OPERAND (incr, 0));
17965 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
17966 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
17967 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
17968 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
17969 rhs0, rhs1));
17971 else
17972 incr = RECUR (incr);
17973 break;
17974 default:
17975 incr = RECUR (incr);
17976 break;
17979 if (orig_declv && !OMP_FOR_ORIG_DECLS (t))
17980 TREE_VEC_ELT (orig_declv, i) = decl;
17981 TREE_VEC_ELT (declv, i) = decl;
17982 TREE_VEC_ELT (initv, i) = init;
17983 TREE_VEC_ELT (condv, i) = cond;
17984 TREE_VEC_ELT (incrv, i) = incr;
17985 return false;
17986 #undef RECUR
17989 /* Helper function of tsubst_expr, find OMP_TEAMS inside
17990 of OMP_TARGET's body. */
17992 static tree
17993 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
17995 *walk_subtrees = 0;
17996 switch (TREE_CODE (*tp))
17998 case OMP_TEAMS:
17999 return *tp;
18000 case BIND_EXPR:
18001 case STATEMENT_LIST:
18002 *walk_subtrees = 1;
18003 break;
18004 default:
18005 break;
18007 return NULL_TREE;
18010 /* Helper function for tsubst_expr. For decomposition declaration
18011 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
18012 also the corresponding decls representing the identifiers
18013 of the decomposition declaration. Return DECL if successful
18014 or error_mark_node otherwise, set *FIRST to the first decl
18015 in the list chained through DECL_CHAIN and *CNT to the number
18016 of such decls. */
18018 static tree
18019 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
18020 tsubst_flags_t complain, tree in_decl, tree *first,
18021 unsigned int *cnt)
18023 tree decl2, decl3, prev = decl;
18024 *cnt = 0;
18025 gcc_assert (DECL_NAME (decl) == NULL_TREE);
18026 for (decl2 = DECL_CHAIN (pattern_decl);
18027 decl2
18028 && VAR_P (decl2)
18029 && DECL_DECOMPOSITION_P (decl2)
18030 && DECL_NAME (decl2);
18031 decl2 = DECL_CHAIN (decl2))
18033 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
18035 gcc_assert (errorcount);
18036 return error_mark_node;
18038 (*cnt)++;
18039 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
18040 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
18041 tree v = DECL_VALUE_EXPR (decl2);
18042 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
18043 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
18044 decl3 = tsubst (decl2, args, complain, in_decl);
18045 SET_DECL_VALUE_EXPR (decl2, v);
18046 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
18047 if (VAR_P (decl3))
18048 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
18049 else
18051 gcc_assert (errorcount);
18052 decl = error_mark_node;
18053 continue;
18055 maybe_push_decl (decl3);
18056 if (error_operand_p (decl3))
18057 decl = error_mark_node;
18058 else if (decl != error_mark_node
18059 && DECL_CHAIN (decl3) != prev
18060 && decl != prev)
18062 gcc_assert (errorcount);
18063 decl = error_mark_node;
18065 else
18066 prev = decl3;
18068 *first = prev;
18069 return decl;
18072 /* Return the proper local_specialization for init-capture pack DECL. */
18074 static tree
18075 lookup_init_capture_pack (tree decl)
18077 /* We handle normal pack captures by forwarding to the specialization of the
18078 captured parameter. We can't do that for pack init-captures; we need them
18079 to have their own local_specialization. We created the individual
18080 VAR_DECLs (if any) under build_capture_proxy, and we need to collect them
18081 when we process the DECL_EXPR for the pack init-capture in the template.
18082 So, how do we find them? We don't know the capture proxy pack when
18083 building the individual resulting proxies, and we don't know the
18084 individual proxies when instantiating the pack. What we have in common is
18085 the FIELD_DECL.
18087 So...when we instantiate the FIELD_DECL, we stick the result in
18088 local_specializations. Then at the DECL_EXPR we look up that result, see
18089 how many elements it has, synthesize the names, and look them up. */
18091 tree cname = DECL_NAME (decl);
18092 tree val = DECL_VALUE_EXPR (decl);
18093 tree field = TREE_OPERAND (val, 1);
18094 gcc_assert (TREE_CODE (field) == FIELD_DECL);
18095 tree fpack = retrieve_local_specialization (field);
18096 if (fpack == error_mark_node)
18097 return error_mark_node;
18099 int len = 1;
18100 tree vec = NULL_TREE;
18101 tree r = NULL_TREE;
18102 if (TREE_CODE (fpack) == TREE_VEC)
18104 len = TREE_VEC_LENGTH (fpack);
18105 vec = make_tree_vec (len);
18106 r = make_node (NONTYPE_ARGUMENT_PACK);
18107 SET_ARGUMENT_PACK_ARGS (r, vec);
18109 for (int i = 0; i < len; ++i)
18111 tree ename = vec ? make_ith_pack_parameter_name (cname, i) : cname;
18112 tree elt = lookup_name (ename);
18113 if (vec)
18114 TREE_VEC_ELT (vec, i) = elt;
18115 else
18116 r = elt;
18118 return r;
18121 /* Like tsubst_copy for expressions, etc. but also does semantic
18122 processing. */
18124 tree
18125 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
18126 bool integral_constant_expression_p)
18128 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
18129 #define RECUR(NODE) \
18130 tsubst_expr ((NODE), args, complain, in_decl, \
18131 integral_constant_expression_p)
18133 tree stmt, tmp;
18134 tree r;
18135 location_t loc;
18137 if (t == NULL_TREE || t == error_mark_node)
18138 return t;
18140 loc = input_location;
18141 if (location_t eloc = cp_expr_location (t))
18142 input_location = eloc;
18143 if (STATEMENT_CODE_P (TREE_CODE (t)))
18144 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
18146 switch (TREE_CODE (t))
18148 case STATEMENT_LIST:
18150 for (tree stmt : tsi_range (t))
18151 RECUR (stmt);
18152 break;
18155 case CTOR_INITIALIZER:
18156 finish_mem_initializers (tsubst_initializer_list
18157 (TREE_OPERAND (t, 0), args));
18158 break;
18160 case RETURN_EXPR:
18161 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
18162 break;
18164 case CO_RETURN_EXPR:
18165 finish_co_return_stmt (input_location, RECUR (TREE_OPERAND (t, 0)));
18166 break;
18168 case CO_YIELD_EXPR:
18169 stmt = finish_co_yield_expr (input_location,
18170 RECUR (TREE_OPERAND (t, 0)));
18171 RETURN (stmt);
18172 break;
18174 case CO_AWAIT_EXPR:
18175 stmt = finish_co_await_expr (input_location,
18176 RECUR (TREE_OPERAND (t, 0)));
18177 RETURN (stmt);
18178 break;
18180 case EXPR_STMT:
18181 tmp = RECUR (EXPR_STMT_EXPR (t));
18182 if (EXPR_STMT_STMT_EXPR_RESULT (t))
18183 finish_stmt_expr_expr (tmp, cur_stmt_expr);
18184 else
18185 finish_expr_stmt (tmp);
18186 break;
18188 case USING_STMT:
18189 finish_using_directive (USING_STMT_NAMESPACE (t), /*attribs=*/NULL_TREE);
18190 break;
18192 case DECL_EXPR:
18194 tree decl, pattern_decl;
18195 tree init;
18197 pattern_decl = decl = DECL_EXPR_DECL (t);
18198 if (TREE_CODE (decl) == LABEL_DECL)
18199 finish_label_decl (DECL_NAME (decl));
18200 else if (TREE_CODE (decl) == USING_DECL)
18202 tree scope = USING_DECL_SCOPE (decl);
18203 if (DECL_DEPENDENT_P (decl))
18205 scope = tsubst (scope, args, complain, in_decl);
18206 if (!MAYBE_CLASS_TYPE_P (scope)
18207 && TREE_CODE (scope) != ENUMERAL_TYPE)
18209 if (complain & tf_error)
18210 error_at (DECL_SOURCE_LOCATION (decl), "%qT is not a "
18211 "class, namespace, or enumeration", scope);
18212 return error_mark_node;
18214 finish_nonmember_using_decl (scope, DECL_NAME (decl));
18216 else
18218 /* This is a non-dependent using-decl, and we'll have
18219 used the names it found during template parsing. We do
18220 not want to do the lookup again, because we might not
18221 find the things we found then. */
18222 gcc_checking_assert (scope == tsubst (scope, args,
18223 complain, in_decl));
18224 /* We still need to push the bindings so that we can look up
18225 this name later. */
18226 push_using_decl_bindings (DECL_NAME (decl),
18227 USING_DECL_DECLS (decl));
18230 else if (is_capture_proxy (decl)
18231 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
18233 /* We're in tsubst_lambda_expr, we've already inserted a new
18234 capture proxy, so look it up and register it. */
18235 tree inst;
18236 if (!DECL_PACK_P (decl))
18238 inst = lookup_name (DECL_NAME (decl), LOOK_where::BLOCK,
18239 LOOK_want::HIDDEN_LAMBDA);
18240 gcc_assert (inst != decl && is_capture_proxy (inst));
18242 else if (is_normal_capture_proxy (decl))
18244 inst = (retrieve_local_specialization
18245 (DECL_CAPTURED_VARIABLE (decl)));
18246 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK
18247 || DECL_PACK_P (inst));
18249 else
18250 inst = lookup_init_capture_pack (decl);
18252 register_local_specialization (inst, decl);
18253 break;
18255 else if (DECL_PRETTY_FUNCTION_P (decl))
18256 decl = make_fname_decl (DECL_SOURCE_LOCATION (decl),
18257 DECL_NAME (decl),
18258 true/*DECL_PRETTY_FUNCTION_P (decl)*/);
18259 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
18260 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
18261 /* Don't copy the old closure; we'll create a new one in
18262 tsubst_lambda_expr. */
18263 break;
18264 else
18266 init = DECL_INITIAL (decl);
18267 decl = tsubst (decl, args, complain, in_decl);
18268 if (decl != error_mark_node)
18270 /* By marking the declaration as instantiated, we avoid
18271 trying to instantiate it. Since instantiate_decl can't
18272 handle local variables, and since we've already done
18273 all that needs to be done, that's the right thing to
18274 do. */
18275 if (VAR_P (decl))
18276 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
18277 if (VAR_P (decl) && !DECL_NAME (decl)
18278 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
18279 /* Anonymous aggregates are a special case. */
18280 finish_anon_union (decl);
18281 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
18283 DECL_CONTEXT (decl) = current_function_decl;
18284 if (DECL_NAME (decl) == this_identifier)
18286 tree lam = DECL_CONTEXT (current_function_decl);
18287 lam = CLASSTYPE_LAMBDA_EXPR (lam);
18288 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
18290 insert_capture_proxy (decl);
18292 else if (DECL_IMPLICIT_TYPEDEF_P (t))
18293 /* We already did a pushtag. */;
18294 else if (VAR_OR_FUNCTION_DECL_P (decl)
18295 && DECL_LOCAL_DECL_P (decl))
18297 if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
18298 DECL_CONTEXT (decl) = NULL_TREE;
18299 decl = pushdecl (decl);
18300 if (TREE_CODE (decl) == FUNCTION_DECL
18301 && DECL_OMP_DECLARE_REDUCTION_P (decl)
18302 && cp_check_omp_declare_reduction (decl))
18303 instantiate_body (pattern_decl, args, decl, true);
18305 else
18307 bool const_init = false;
18308 unsigned int cnt = 0;
18309 tree first = NULL_TREE, ndecl = error_mark_node;
18310 tree asmspec_tree = NULL_TREE;
18311 maybe_push_decl (decl);
18313 if (VAR_P (decl)
18314 && DECL_DECOMPOSITION_P (decl)
18315 && TREE_TYPE (pattern_decl) != error_mark_node)
18316 ndecl = tsubst_decomp_names (decl, pattern_decl, args,
18317 complain, in_decl, &first,
18318 &cnt);
18320 init = tsubst_init (init, decl, args, complain, in_decl);
18322 if (VAR_P (decl))
18323 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
18324 (pattern_decl));
18326 if (ndecl != error_mark_node)
18327 cp_maybe_mangle_decomp (ndecl, first, cnt);
18329 /* In a non-template function, VLA type declarations are
18330 handled in grokdeclarator; for templates, handle them
18331 now. */
18332 predeclare_vla (decl);
18334 if (VAR_P (decl) && DECL_HARD_REGISTER (pattern_decl))
18336 tree id = DECL_ASSEMBLER_NAME (pattern_decl);
18337 const char *asmspec = IDENTIFIER_POINTER (id);
18338 gcc_assert (asmspec[0] == '*');
18339 asmspec_tree
18340 = build_string (IDENTIFIER_LENGTH (id) - 1,
18341 asmspec + 1);
18342 TREE_TYPE (asmspec_tree) = char_array_type_node;
18345 cp_finish_decl (decl, init, const_init, asmspec_tree, 0);
18347 if (ndecl != error_mark_node)
18348 cp_finish_decomp (ndecl, first, cnt);
18353 break;
18356 case FOR_STMT:
18357 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
18358 RECUR (FOR_INIT_STMT (t));
18359 finish_init_stmt (stmt);
18360 tmp = RECUR (FOR_COND (t));
18361 finish_for_cond (tmp, stmt, false, 0);
18362 tmp = RECUR (FOR_EXPR (t));
18363 finish_for_expr (tmp, stmt);
18365 bool prev = note_iteration_stmt_body_start ();
18366 RECUR (FOR_BODY (t));
18367 note_iteration_stmt_body_end (prev);
18369 finish_for_stmt (stmt);
18370 break;
18372 case RANGE_FOR_STMT:
18374 /* Construct another range_for, if this is not a final
18375 substitution (for inside a generic lambda of a
18376 template). Otherwise convert to a regular for. */
18377 tree decl, expr;
18378 stmt = (processing_template_decl
18379 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
18380 : begin_for_stmt (NULL_TREE, NULL_TREE));
18381 RECUR (RANGE_FOR_INIT_STMT (t));
18382 decl = RANGE_FOR_DECL (t);
18383 decl = tsubst (decl, args, complain, in_decl);
18384 maybe_push_decl (decl);
18385 expr = RECUR (RANGE_FOR_EXPR (t));
18387 tree decomp_first = NULL_TREE;
18388 unsigned decomp_cnt = 0;
18389 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
18390 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
18391 complain, in_decl,
18392 &decomp_first, &decomp_cnt);
18394 if (processing_template_decl)
18396 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
18397 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
18398 finish_range_for_decl (stmt, decl, expr);
18399 if (decomp_first && decl != error_mark_node)
18400 cp_finish_decomp (decl, decomp_first, decomp_cnt);
18402 else
18404 unsigned short unroll = (RANGE_FOR_UNROLL (t)
18405 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
18406 stmt = cp_convert_range_for (stmt, decl, expr,
18407 decomp_first, decomp_cnt,
18408 RANGE_FOR_IVDEP (t), unroll);
18411 bool prev = note_iteration_stmt_body_start ();
18412 RECUR (RANGE_FOR_BODY (t));
18413 note_iteration_stmt_body_end (prev);
18414 finish_for_stmt (stmt);
18416 break;
18418 case WHILE_STMT:
18419 stmt = begin_while_stmt ();
18420 tmp = RECUR (WHILE_COND (t));
18421 finish_while_stmt_cond (tmp, stmt, false, 0);
18423 bool prev = note_iteration_stmt_body_start ();
18424 RECUR (WHILE_BODY (t));
18425 note_iteration_stmt_body_end (prev);
18427 finish_while_stmt (stmt);
18428 break;
18430 case DO_STMT:
18431 stmt = begin_do_stmt ();
18433 bool prev = note_iteration_stmt_body_start ();
18434 RECUR (DO_BODY (t));
18435 note_iteration_stmt_body_end (prev);
18437 finish_do_body (stmt);
18438 tmp = RECUR (DO_COND (t));
18439 finish_do_stmt (tmp, stmt, false, 0);
18440 break;
18442 case IF_STMT:
18443 stmt = begin_if_stmt ();
18444 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
18445 IF_STMT_CONSTEVAL_P (stmt) = IF_STMT_CONSTEVAL_P (t);
18446 if (IF_STMT_CONSTEXPR_P (t))
18447 args = add_extra_args (IF_STMT_EXTRA_ARGS (t), args);
18448 tmp = RECUR (IF_COND (t));
18449 tmp = finish_if_stmt_cond (tmp, stmt);
18450 if (IF_STMT_CONSTEXPR_P (t)
18451 && instantiation_dependent_expression_p (tmp))
18453 /* We're partially instantiating a generic lambda, but the condition
18454 of the constexpr if is still dependent. Don't substitute into the
18455 branches now, just remember the template arguments. */
18456 do_poplevel (IF_SCOPE (stmt));
18457 IF_COND (stmt) = IF_COND (t);
18458 THEN_CLAUSE (stmt) = THEN_CLAUSE (t);
18459 ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t);
18460 IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (t, args, complain);
18461 add_stmt (stmt);
18462 break;
18464 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
18465 /* Don't instantiate the THEN_CLAUSE. */;
18466 else if (IF_STMT_CONSTEVAL_P (t))
18468 bool save_in_consteval_if_p = in_consteval_if_p;
18469 in_consteval_if_p = true;
18470 RECUR (THEN_CLAUSE (t));
18471 in_consteval_if_p = save_in_consteval_if_p;
18473 else
18475 tree folded = fold_non_dependent_expr (tmp, complain);
18476 bool inhibit = integer_zerop (folded);
18477 if (inhibit)
18478 ++c_inhibit_evaluation_warnings;
18479 RECUR (THEN_CLAUSE (t));
18480 if (inhibit)
18481 --c_inhibit_evaluation_warnings;
18483 finish_then_clause (stmt);
18485 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
18486 /* Don't instantiate the ELSE_CLAUSE. */;
18487 else if (ELSE_CLAUSE (t))
18489 tree folded = fold_non_dependent_expr (tmp, complain);
18490 bool inhibit = integer_nonzerop (folded);
18491 begin_else_clause (stmt);
18492 if (inhibit)
18493 ++c_inhibit_evaluation_warnings;
18494 RECUR (ELSE_CLAUSE (t));
18495 if (inhibit)
18496 --c_inhibit_evaluation_warnings;
18497 finish_else_clause (stmt);
18500 finish_if_stmt (stmt);
18501 break;
18503 case BIND_EXPR:
18504 if (BIND_EXPR_BODY_BLOCK (t))
18505 stmt = begin_function_body ();
18506 else
18507 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
18508 ? BCS_TRY_BLOCK : 0);
18510 RECUR (BIND_EXPR_BODY (t));
18512 if (BIND_EXPR_BODY_BLOCK (t))
18513 finish_function_body (stmt);
18514 else
18515 finish_compound_stmt (stmt);
18516 break;
18518 case BREAK_STMT:
18519 finish_break_stmt ();
18520 break;
18522 case CONTINUE_STMT:
18523 finish_continue_stmt ();
18524 break;
18526 case SWITCH_STMT:
18527 stmt = begin_switch_stmt ();
18528 tmp = RECUR (SWITCH_STMT_COND (t));
18529 finish_switch_cond (tmp, stmt);
18530 RECUR (SWITCH_STMT_BODY (t));
18531 finish_switch_stmt (stmt);
18532 break;
18534 case CASE_LABEL_EXPR:
18536 tree decl = CASE_LABEL (t);
18537 tree low = RECUR (CASE_LOW (t));
18538 tree high = RECUR (CASE_HIGH (t));
18539 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
18540 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
18542 tree label = CASE_LABEL (l);
18543 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
18544 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
18545 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
18548 break;
18550 case LABEL_EXPR:
18552 tree decl = LABEL_EXPR_LABEL (t);
18553 tree label;
18555 label = finish_label_stmt (DECL_NAME (decl));
18556 if (TREE_CODE (label) == LABEL_DECL)
18557 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
18558 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
18559 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
18561 break;
18563 case GOTO_EXPR:
18564 tmp = GOTO_DESTINATION (t);
18565 if (TREE_CODE (tmp) != LABEL_DECL)
18566 /* Computed goto's must be tsubst'd into. On the other hand,
18567 non-computed gotos must not be; the identifier in question
18568 will have no binding. */
18569 tmp = RECUR (tmp);
18570 else
18571 tmp = DECL_NAME (tmp);
18572 finish_goto_stmt (tmp);
18573 break;
18575 case ASM_EXPR:
18577 tree string = RECUR (ASM_STRING (t));
18578 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
18579 complain, in_decl);
18580 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
18581 complain, in_decl);
18582 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
18583 complain, in_decl);
18584 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
18585 complain, in_decl);
18586 tmp = finish_asm_stmt (EXPR_LOCATION (t), ASM_VOLATILE_P (t), string,
18587 outputs, inputs, clobbers, labels,
18588 ASM_INLINE_P (t));
18589 tree asm_expr = tmp;
18590 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
18591 asm_expr = TREE_OPERAND (asm_expr, 0);
18592 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
18594 break;
18596 case TRY_BLOCK:
18597 if (CLEANUP_P (t))
18599 stmt = begin_try_block ();
18600 RECUR (TRY_STMTS (t));
18601 finish_cleanup_try_block (stmt);
18602 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
18604 else
18606 tree compound_stmt = NULL_TREE;
18608 if (FN_TRY_BLOCK_P (t))
18609 stmt = begin_function_try_block (&compound_stmt);
18610 else
18611 stmt = begin_try_block ();
18613 RECUR (TRY_STMTS (t));
18615 if (FN_TRY_BLOCK_P (t))
18616 finish_function_try_block (stmt);
18617 else
18618 finish_try_block (stmt);
18620 RECUR (TRY_HANDLERS (t));
18621 if (FN_TRY_BLOCK_P (t))
18622 finish_function_handler_sequence (stmt, compound_stmt);
18623 else
18624 finish_handler_sequence (stmt);
18626 break;
18628 case HANDLER:
18630 tree decl = HANDLER_PARMS (t);
18632 if (decl)
18634 decl = tsubst (decl, args, complain, in_decl);
18635 /* Prevent instantiate_decl from trying to instantiate
18636 this variable. We've already done all that needs to be
18637 done. */
18638 if (decl != error_mark_node)
18639 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
18641 stmt = begin_handler ();
18642 finish_handler_parms (decl, stmt);
18643 RECUR (HANDLER_BODY (t));
18644 finish_handler (stmt);
18646 break;
18648 case TAG_DEFN:
18649 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
18650 if (CLASS_TYPE_P (tmp))
18652 /* Local classes are not independent templates; they are
18653 instantiated along with their containing function. And this
18654 way we don't have to deal with pushing out of one local class
18655 to instantiate a member of another local class. */
18656 /* Closures are handled by the LAMBDA_EXPR. */
18657 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
18658 complete_type (tmp);
18659 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
18660 if ((VAR_P (fld)
18661 || (TREE_CODE (fld) == FUNCTION_DECL
18662 && !DECL_ARTIFICIAL (fld)))
18663 && DECL_TEMPLATE_INSTANTIATION (fld))
18664 instantiate_decl (fld, /*defer_ok=*/false,
18665 /*expl_inst_class=*/false);
18667 break;
18669 case STATIC_ASSERT:
18671 tree condition;
18673 ++c_inhibit_evaluation_warnings;
18674 condition =
18675 tsubst_expr (STATIC_ASSERT_CONDITION (t),
18676 args,
18677 complain, in_decl,
18678 /*integral_constant_expression_p=*/true);
18679 --c_inhibit_evaluation_warnings;
18681 finish_static_assert (condition,
18682 STATIC_ASSERT_MESSAGE (t),
18683 STATIC_ASSERT_SOURCE_LOCATION (t),
18684 /*member_p=*/false, /*show_expr_p=*/true);
18686 break;
18688 case OACC_KERNELS:
18689 case OACC_PARALLEL:
18690 case OACC_SERIAL:
18691 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
18692 in_decl);
18693 stmt = begin_omp_parallel ();
18694 RECUR (OMP_BODY (t));
18695 finish_omp_construct (TREE_CODE (t), stmt, tmp);
18696 break;
18698 case OMP_PARALLEL:
18699 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
18700 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
18701 complain, in_decl);
18702 if (OMP_PARALLEL_COMBINED (t))
18703 omp_parallel_combined_clauses = &tmp;
18704 stmt = begin_omp_parallel ();
18705 RECUR (OMP_PARALLEL_BODY (t));
18706 gcc_assert (omp_parallel_combined_clauses == NULL);
18707 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
18708 = OMP_PARALLEL_COMBINED (t);
18709 pop_omp_privatization_clauses (r);
18710 break;
18712 case OMP_TASK:
18713 if (OMP_TASK_BODY (t) == NULL_TREE)
18715 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
18716 complain, in_decl);
18717 t = copy_node (t);
18718 OMP_TASK_CLAUSES (t) = tmp;
18719 add_stmt (t);
18720 break;
18722 r = push_omp_privatization_clauses (false);
18723 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
18724 complain, in_decl);
18725 stmt = begin_omp_task ();
18726 RECUR (OMP_TASK_BODY (t));
18727 finish_omp_task (tmp, stmt);
18728 pop_omp_privatization_clauses (r);
18729 break;
18731 case OMP_FOR:
18732 case OMP_LOOP:
18733 case OMP_SIMD:
18734 case OMP_DISTRIBUTE:
18735 case OMP_TASKLOOP:
18736 case OACC_LOOP:
18738 tree clauses, body, pre_body;
18739 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
18740 tree orig_declv = NULL_TREE;
18741 tree incrv = NULL_TREE;
18742 enum c_omp_region_type ort = C_ORT_OMP;
18743 bool any_range_for = false;
18744 int i;
18746 if (TREE_CODE (t) == OACC_LOOP)
18747 ort = C_ORT_ACC;
18749 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
18750 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
18751 in_decl);
18752 if (OMP_FOR_INIT (t) != NULL_TREE)
18754 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18755 if (OMP_FOR_ORIG_DECLS (t))
18756 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18757 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18758 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18759 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
18762 keep_next_level (true);
18763 stmt = begin_omp_structured_block ();
18765 pre_body = push_stmt_list ();
18766 RECUR (OMP_FOR_PRE_BODY (t));
18767 pre_body = pop_stmt_list (pre_body);
18769 if (OMP_FOR_INIT (t) != NULL_TREE)
18770 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
18771 any_range_for
18772 |= tsubst_omp_for_iterator (t, i, declv, orig_declv, initv,
18773 condv, incrv, &clauses, args,
18774 complain, in_decl,
18775 integral_constant_expression_p);
18776 omp_parallel_combined_clauses = NULL;
18778 if (any_range_for)
18780 gcc_assert (orig_declv);
18781 body = begin_omp_structured_block ();
18782 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
18783 if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i)
18784 && TREE_CODE (TREE_VEC_ELT (orig_declv, i)) == TREE_LIST
18785 && TREE_CHAIN (TREE_VEC_ELT (orig_declv, i)))
18786 cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
18787 TREE_VEC_ELT (declv, i));
18789 else
18790 body = push_stmt_list ();
18791 RECUR (OMP_FOR_BODY (t));
18792 if (any_range_for)
18793 body = finish_omp_structured_block (body);
18794 else
18795 body = pop_stmt_list (body);
18797 if (OMP_FOR_INIT (t) != NULL_TREE)
18798 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
18799 orig_declv, initv, condv, incrv, body, pre_body,
18800 NULL, clauses);
18801 else
18803 t = make_node (TREE_CODE (t));
18804 TREE_TYPE (t) = void_type_node;
18805 OMP_FOR_BODY (t) = body;
18806 OMP_FOR_PRE_BODY (t) = pre_body;
18807 OMP_FOR_CLAUSES (t) = clauses;
18808 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
18809 add_stmt (t);
18812 add_stmt (finish_omp_for_block (finish_omp_structured_block (stmt),
18813 t));
18814 pop_omp_privatization_clauses (r);
18816 break;
18818 case OMP_SECTIONS:
18819 omp_parallel_combined_clauses = NULL;
18820 /* FALLTHRU */
18821 case OMP_SINGLE:
18822 case OMP_TEAMS:
18823 case OMP_CRITICAL:
18824 case OMP_TASKGROUP:
18825 case OMP_SCAN:
18826 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
18827 && OMP_TEAMS_COMBINED (t));
18828 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
18829 in_decl);
18830 if (TREE_CODE (t) == OMP_TEAMS)
18832 keep_next_level (true);
18833 stmt = begin_omp_structured_block ();
18834 RECUR (OMP_BODY (t));
18835 stmt = finish_omp_structured_block (stmt);
18837 else
18839 stmt = push_stmt_list ();
18840 RECUR (OMP_BODY (t));
18841 stmt = pop_stmt_list (stmt);
18844 if (TREE_CODE (t) == OMP_CRITICAL
18845 && tmp != NULL_TREE
18846 && integer_nonzerop (OMP_CLAUSE_HINT_EXPR (tmp)))
18848 error_at (OMP_CLAUSE_LOCATION (tmp),
18849 "%<#pragma omp critical%> with %<hint%> clause requires "
18850 "a name, except when %<omp_sync_hint_none%> is used");
18851 RETURN (error_mark_node);
18853 t = copy_node (t);
18854 OMP_BODY (t) = stmt;
18855 OMP_CLAUSES (t) = tmp;
18856 add_stmt (t);
18857 pop_omp_privatization_clauses (r);
18858 break;
18860 case OMP_DEPOBJ:
18861 r = RECUR (OMP_DEPOBJ_DEPOBJ (t));
18862 if (OMP_DEPOBJ_CLAUSES (t) && OMP_DEPOBJ_CLAUSES (t) != error_mark_node)
18864 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_SOURCE;
18865 if (TREE_CODE (OMP_DEPOBJ_CLAUSES (t)) == OMP_CLAUSE)
18867 tmp = tsubst_omp_clauses (OMP_DEPOBJ_CLAUSES (t), C_ORT_OMP,
18868 args, complain, in_decl);
18869 if (tmp == NULL_TREE)
18870 tmp = error_mark_node;
18872 else
18874 kind = (enum omp_clause_depend_kind)
18875 tree_to_uhwi (OMP_DEPOBJ_CLAUSES (t));
18876 tmp = NULL_TREE;
18878 finish_omp_depobj (EXPR_LOCATION (t), r, kind, tmp);
18880 else
18881 finish_omp_depobj (EXPR_LOCATION (t), r,
18882 OMP_CLAUSE_DEPEND_SOURCE,
18883 OMP_DEPOBJ_CLAUSES (t));
18884 break;
18886 case OACC_DATA:
18887 case OMP_TARGET_DATA:
18888 case OMP_TARGET:
18889 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
18890 ? C_ORT_ACC : C_ORT_OMP, args, complain,
18891 in_decl);
18892 keep_next_level (true);
18893 stmt = begin_omp_structured_block ();
18895 RECUR (OMP_BODY (t));
18896 stmt = finish_omp_structured_block (stmt);
18898 t = copy_node (t);
18899 OMP_BODY (t) = stmt;
18900 OMP_CLAUSES (t) = tmp;
18901 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
18903 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
18904 if (teams)
18906 /* For combined target teams, ensure the num_teams and
18907 thread_limit clause expressions are evaluated on the host,
18908 before entering the target construct. */
18909 tree c;
18910 for (c = OMP_TEAMS_CLAUSES (teams);
18911 c; c = OMP_CLAUSE_CHAIN (c))
18912 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
18913 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
18914 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
18916 tree expr = OMP_CLAUSE_OPERAND (c, 0);
18917 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
18918 if (expr == error_mark_node)
18919 continue;
18920 tmp = TARGET_EXPR_SLOT (expr);
18921 add_stmt (expr);
18922 OMP_CLAUSE_OPERAND (c, 0) = expr;
18923 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
18924 OMP_CLAUSE_FIRSTPRIVATE);
18925 OMP_CLAUSE_DECL (tc) = tmp;
18926 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
18927 OMP_TARGET_CLAUSES (t) = tc;
18931 add_stmt (t);
18932 break;
18934 case OACC_DECLARE:
18935 t = copy_node (t);
18936 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
18937 complain, in_decl);
18938 OACC_DECLARE_CLAUSES (t) = tmp;
18939 add_stmt (t);
18940 break;
18942 case OMP_TARGET_UPDATE:
18943 case OMP_TARGET_ENTER_DATA:
18944 case OMP_TARGET_EXIT_DATA:
18945 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
18946 complain, in_decl);
18947 t = copy_node (t);
18948 OMP_STANDALONE_CLAUSES (t) = tmp;
18949 add_stmt (t);
18950 break;
18952 case OACC_CACHE:
18953 case OACC_ENTER_DATA:
18954 case OACC_EXIT_DATA:
18955 case OACC_UPDATE:
18956 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
18957 complain, in_decl);
18958 t = copy_node (t);
18959 OMP_STANDALONE_CLAUSES (t) = tmp;
18960 add_stmt (t);
18961 break;
18963 case OMP_ORDERED:
18964 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
18965 complain, in_decl);
18966 stmt = push_stmt_list ();
18967 RECUR (OMP_BODY (t));
18968 stmt = pop_stmt_list (stmt);
18970 t = copy_node (t);
18971 OMP_BODY (t) = stmt;
18972 OMP_ORDERED_CLAUSES (t) = tmp;
18973 add_stmt (t);
18974 break;
18976 case OMP_MASTER:
18977 omp_parallel_combined_clauses = NULL;
18978 /* FALLTHRU */
18979 case OMP_SECTION:
18980 stmt = push_stmt_list ();
18981 RECUR (OMP_BODY (t));
18982 stmt = pop_stmt_list (stmt);
18984 t = copy_node (t);
18985 OMP_BODY (t) = stmt;
18986 add_stmt (t);
18987 break;
18989 case OMP_ATOMIC:
18990 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
18991 tmp = NULL_TREE;
18992 if (TREE_CODE (TREE_OPERAND (t, 0)) == OMP_CLAUSE)
18993 tmp = tsubst_omp_clauses (TREE_OPERAND (t, 0), C_ORT_OMP, args,
18994 complain, in_decl);
18995 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
18997 tree op1 = TREE_OPERAND (t, 1);
18998 tree rhs1 = NULL_TREE;
18999 tree lhs, rhs;
19000 if (TREE_CODE (op1) == COMPOUND_EXPR)
19002 rhs1 = RECUR (TREE_OPERAND (op1, 0));
19003 op1 = TREE_OPERAND (op1, 1);
19005 lhs = RECUR (TREE_OPERAND (op1, 0));
19006 rhs = RECUR (TREE_OPERAND (op1, 1));
19007 finish_omp_atomic (EXPR_LOCATION (t), OMP_ATOMIC, TREE_CODE (op1),
19008 lhs, rhs, NULL_TREE, NULL_TREE, rhs1, tmp,
19009 OMP_ATOMIC_MEMORY_ORDER (t));
19011 else
19013 tree op1 = TREE_OPERAND (t, 1);
19014 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
19015 tree rhs1 = NULL_TREE;
19016 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
19017 enum tree_code opcode = NOP_EXPR;
19018 if (code == OMP_ATOMIC_READ)
19020 v = RECUR (TREE_OPERAND (op1, 0));
19021 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
19023 else if (code == OMP_ATOMIC_CAPTURE_OLD
19024 || code == OMP_ATOMIC_CAPTURE_NEW)
19026 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
19027 v = RECUR (TREE_OPERAND (op1, 0));
19028 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
19029 if (TREE_CODE (op11) == COMPOUND_EXPR)
19031 rhs1 = RECUR (TREE_OPERAND (op11, 0));
19032 op11 = TREE_OPERAND (op11, 1);
19034 lhs = RECUR (TREE_OPERAND (op11, 0));
19035 rhs = RECUR (TREE_OPERAND (op11, 1));
19036 opcode = TREE_CODE (op11);
19037 if (opcode == MODIFY_EXPR)
19038 opcode = NOP_EXPR;
19040 else
19042 code = OMP_ATOMIC;
19043 lhs = RECUR (TREE_OPERAND (op1, 0));
19044 rhs = RECUR (TREE_OPERAND (op1, 1));
19046 finish_omp_atomic (EXPR_LOCATION (t), code, opcode, lhs, rhs, v,
19047 lhs1, rhs1, tmp, OMP_ATOMIC_MEMORY_ORDER (t));
19049 break;
19051 case TRANSACTION_EXPR:
19053 int flags = 0;
19054 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
19055 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
19057 if (TRANSACTION_EXPR_IS_STMT (t))
19059 tree body = TRANSACTION_EXPR_BODY (t);
19060 tree noex = NULL_TREE;
19061 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
19063 noex = MUST_NOT_THROW_COND (body);
19064 if (noex == NULL_TREE)
19065 noex = boolean_true_node;
19066 body = TREE_OPERAND (body, 0);
19068 stmt = begin_transaction_stmt (input_location, NULL, flags);
19069 RECUR (body);
19070 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
19072 else
19074 stmt = build_transaction_expr (EXPR_LOCATION (t),
19075 RECUR (TRANSACTION_EXPR_BODY (t)),
19076 flags, NULL_TREE);
19077 RETURN (stmt);
19080 break;
19082 case MUST_NOT_THROW_EXPR:
19084 tree op0 = RECUR (TREE_OPERAND (t, 0));
19085 tree cond = RECUR (MUST_NOT_THROW_COND (t));
19086 RETURN (build_must_not_throw_expr (op0, cond));
19089 case EXPR_PACK_EXPANSION:
19090 error ("invalid use of pack expansion expression");
19091 RETURN (error_mark_node);
19093 case NONTYPE_ARGUMENT_PACK:
19094 error ("use %<...%> to expand argument pack");
19095 RETURN (error_mark_node);
19097 case COMPOUND_EXPR:
19098 tmp = RECUR (TREE_OPERAND (t, 0));
19099 if (tmp == NULL_TREE)
19100 /* If the first operand was a statement, we're done with it. */
19101 RETURN (RECUR (TREE_OPERAND (t, 1)));
19102 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
19103 RECUR (TREE_OPERAND (t, 1)),
19104 complain));
19106 case ANNOTATE_EXPR:
19107 tmp = RECUR (TREE_OPERAND (t, 0));
19108 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
19109 TREE_TYPE (tmp), tmp,
19110 RECUR (TREE_OPERAND (t, 1)),
19111 RECUR (TREE_OPERAND (t, 2))));
19113 case PREDICT_EXPR:
19114 RETURN (add_stmt (copy_node (t)));
19116 default:
19117 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
19119 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
19120 /*function_p=*/false,
19121 integral_constant_expression_p));
19124 RETURN (NULL_TREE);
19125 out:
19126 input_location = loc;
19127 return r;
19128 #undef RECUR
19129 #undef RETURN
19132 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
19133 function. For description of the body see comment above
19134 cp_parser_omp_declare_reduction_exprs. */
19136 static void
19137 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
19139 if (t == NULL_TREE || t == error_mark_node)
19140 return;
19142 gcc_assert (TREE_CODE (t) == STATEMENT_LIST && current_function_decl);
19144 tree_stmt_iterator tsi;
19145 int i;
19146 tree stmts[7];
19147 memset (stmts, 0, sizeof stmts);
19148 for (i = 0, tsi = tsi_start (t);
19149 i < 7 && !tsi_end_p (tsi);
19150 i++, tsi_next (&tsi))
19151 stmts[i] = tsi_stmt (tsi);
19152 gcc_assert (tsi_end_p (tsi));
19154 if (i >= 3)
19156 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
19157 && TREE_CODE (stmts[1]) == DECL_EXPR);
19158 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
19159 args, complain, in_decl);
19160 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
19161 args, complain, in_decl);
19162 /* tsubsting a local var_decl leaves DECL_CONTEXT null, as we
19163 expect to be pushing it. */
19164 DECL_CONTEXT (omp_out) = current_function_decl;
19165 DECL_CONTEXT (omp_in) = current_function_decl;
19166 keep_next_level (true);
19167 tree block = begin_omp_structured_block ();
19168 tsubst_expr (stmts[2], args, complain, in_decl, false);
19169 block = finish_omp_structured_block (block);
19170 block = maybe_cleanup_point_expr_void (block);
19171 add_decl_expr (omp_out);
19172 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
19173 TREE_NO_WARNING (omp_out) = 1;
19174 add_decl_expr (omp_in);
19175 finish_expr_stmt (block);
19177 if (i >= 6)
19179 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
19180 && TREE_CODE (stmts[4]) == DECL_EXPR);
19181 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
19182 args, complain, in_decl);
19183 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
19184 args, complain, in_decl);
19185 DECL_CONTEXT (omp_priv) = current_function_decl;
19186 DECL_CONTEXT (omp_orig) = current_function_decl;
19187 keep_next_level (true);
19188 tree block = begin_omp_structured_block ();
19189 tsubst_expr (stmts[5], args, complain, in_decl, false);
19190 block = finish_omp_structured_block (block);
19191 block = maybe_cleanup_point_expr_void (block);
19192 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
19193 add_decl_expr (omp_priv);
19194 add_decl_expr (omp_orig);
19195 finish_expr_stmt (block);
19196 if (i == 7)
19197 add_decl_expr (omp_orig);
19201 /* T is a postfix-expression that is not being used in a function
19202 call. Return the substituted version of T. */
19204 static tree
19205 tsubst_non_call_postfix_expression (tree t, tree args,
19206 tsubst_flags_t complain,
19207 tree in_decl)
19209 if (TREE_CODE (t) == SCOPE_REF)
19210 t = tsubst_qualified_id (t, args, complain, in_decl,
19211 /*done=*/false, /*address_p=*/false);
19212 else
19213 t = tsubst_copy_and_build (t, args, complain, in_decl,
19214 /*function_p=*/false,
19215 /*integral_constant_expression_p=*/false);
19217 return t;
19220 /* Subroutine of tsubst_lambda_expr: add the FIELD/INIT capture pair to the
19221 LAMBDA_EXPR_CAPTURE_LIST passed in LIST. Do deduction for a previously
19222 dependent init-capture. */
19224 static void
19225 prepend_one_capture (tree field, tree init, tree &list,
19226 tsubst_flags_t complain)
19228 if (tree auto_node = type_uses_auto (TREE_TYPE (field)))
19230 tree type = NULL_TREE;
19231 if (!init)
19233 if (complain & tf_error)
19234 error ("empty initializer in lambda init-capture");
19235 init = error_mark_node;
19237 else if (TREE_CODE (init) == TREE_LIST)
19238 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
19239 if (!type)
19240 type = do_auto_deduction (TREE_TYPE (field), init, auto_node, complain);
19241 TREE_TYPE (field) = type;
19242 cp_apply_type_quals_to_decl (cp_type_quals (type), field);
19244 list = tree_cons (field, init, list);
19247 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
19248 instantiation context. Instantiating a pack expansion containing a lambda
19249 might result in multiple lambdas all based on the same lambda in the
19250 template. */
19252 tree
19253 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
19255 tree oldfn = lambda_function (t);
19256 in_decl = oldfn;
19258 tree r = build_lambda_expr ();
19260 LAMBDA_EXPR_LOCATION (r)
19261 = LAMBDA_EXPR_LOCATION (t);
19262 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
19263 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
19264 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
19265 if (tree ti = LAMBDA_EXPR_REGEN_INFO (t))
19266 LAMBDA_EXPR_REGEN_INFO (r)
19267 = build_template_info (t, add_to_template_args (TI_ARGS (ti), args));
19268 else
19269 LAMBDA_EXPR_REGEN_INFO (r)
19270 = build_template_info (t, args);
19272 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
19273 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
19275 vec<tree,va_gc>* field_packs = NULL;
19277 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
19278 cap = TREE_CHAIN (cap))
19280 tree ofield = TREE_PURPOSE (cap);
19281 tree init = TREE_VALUE (cap);
19282 if (PACK_EXPANSION_P (init))
19283 init = tsubst_pack_expansion (init, args, complain, in_decl);
19284 else
19285 init = tsubst_copy_and_build (init, args, complain, in_decl,
19286 /*fn*/false, /*constexpr*/false);
19288 if (init == error_mark_node)
19289 return error_mark_node;
19291 if (init && TREE_CODE (init) == TREE_LIST)
19292 init = build_x_compound_expr_from_list (init, ELK_INIT, complain);
19294 if (!processing_template_decl
19295 && init && TREE_CODE (init) != TREE_VEC
19296 && variably_modified_type_p (TREE_TYPE (init), NULL_TREE))
19298 /* For a VLA, simply tsubsting the field type won't work, we need to
19299 go through add_capture again. XXX do we want to do this for all
19300 captures? */
19301 tree name = (get_identifier
19302 (IDENTIFIER_POINTER (DECL_NAME (ofield)) + 2));
19303 tree ftype = TREE_TYPE (ofield);
19304 bool by_ref = (TYPE_REF_P (ftype)
19305 || (TREE_CODE (ftype) == DECLTYPE_TYPE
19306 && DECLTYPE_FOR_REF_CAPTURE (ftype)));
19307 add_capture (r, name, init, by_ref, !DECL_NORMAL_CAPTURE_P (ofield));
19308 continue;
19311 if (PACK_EXPANSION_P (ofield))
19312 ofield = PACK_EXPANSION_PATTERN (ofield);
19313 tree field = tsubst_decl (ofield, args, complain);
19315 if (DECL_PACK_P (ofield) && !DECL_NORMAL_CAPTURE_P (ofield))
19317 /* Remember these for when we've pushed local_specializations. */
19318 vec_safe_push (field_packs, ofield);
19319 vec_safe_push (field_packs, field);
19322 if (field == error_mark_node)
19323 return error_mark_node;
19325 if (TREE_CODE (field) == TREE_VEC)
19327 int len = TREE_VEC_LENGTH (field);
19328 gcc_assert (TREE_CODE (init) == TREE_VEC
19329 && TREE_VEC_LENGTH (init) == len);
19330 for (int i = 0; i < len; ++i)
19331 prepend_one_capture (TREE_VEC_ELT (field, i),
19332 TREE_VEC_ELT (init, i),
19333 LAMBDA_EXPR_CAPTURE_LIST (r),
19334 complain);
19336 else
19338 prepend_one_capture (field, init, LAMBDA_EXPR_CAPTURE_LIST (r),
19339 complain);
19341 if (id_equal (DECL_NAME (field), "__this"))
19342 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
19346 tree type = begin_lambda_type (r);
19347 if (type == error_mark_node)
19348 return error_mark_node;
19350 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
19351 /* A lambda in a default argument outside a class gets no
19352 LAMBDA_EXPR_EXTRA_SCOPE, as specified by the ABI. But
19353 tsubst_default_argument calls start_lambda_scope, so we need to
19354 specifically ignore it here, and use the global scope. */
19355 record_null_lambda_scope (r);
19356 else
19357 record_lambda_scope (r);
19359 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
19360 determine_visibility (TYPE_NAME (type));
19362 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
19364 tree oldtmpl = (generic_lambda_fn_p (oldfn)
19365 ? DECL_TI_TEMPLATE (oldfn)
19366 : NULL_TREE);
19368 tree fntype = static_fn_type (oldfn);
19369 if (oldtmpl)
19370 ++processing_template_decl;
19371 fntype = tsubst (fntype, args, complain, in_decl);
19372 if (oldtmpl)
19373 --processing_template_decl;
19375 if (fntype == error_mark_node)
19376 r = error_mark_node;
19377 else
19379 /* The body of a lambda-expression is not a subexpression of the
19380 enclosing expression. Parms are to have DECL_CHAIN tsubsted,
19381 which would be skipped if cp_unevaluated_operand. */
19382 cp_evaluated ev;
19384 /* Fix the type of 'this'. */
19385 fntype = build_memfn_type (fntype, type,
19386 type_memfn_quals (fntype),
19387 type_memfn_rqual (fntype));
19388 tree fn, tmpl;
19389 if (oldtmpl)
19391 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
19392 if (tmpl == error_mark_node)
19394 r = error_mark_node;
19395 goto out;
19397 fn = DECL_TEMPLATE_RESULT (tmpl);
19398 finish_member_declaration (tmpl);
19400 else
19402 tmpl = NULL_TREE;
19403 fn = tsubst_function_decl (oldfn, args, complain, fntype);
19404 if (fn == error_mark_node)
19406 r = error_mark_node;
19407 goto out;
19409 finish_member_declaration (fn);
19412 /* Let finish_function set this. */
19413 DECL_DECLARED_CONSTEXPR_P (fn) = false;
19415 bool nested = cfun;
19416 if (nested)
19417 push_function_context ();
19418 else
19419 /* Still increment function_depth so that we don't GC in the
19420 middle of an expression. */
19421 ++function_depth;
19423 local_specialization_stack s (lss_copy);
19425 bool save_in_consteval_if_p = in_consteval_if_p;
19426 in_consteval_if_p = false;
19428 tree body = start_lambda_function (fn, r);
19430 /* Now record them for lookup_init_capture_pack. */
19431 int fplen = vec_safe_length (field_packs);
19432 for (int i = 0; i < fplen; )
19434 tree pack = (*field_packs)[i++];
19435 tree inst = (*field_packs)[i++];
19436 register_local_specialization (inst, pack);
19438 release_tree_vector (field_packs);
19440 register_parameter_specializations (oldfn, fn);
19442 if (oldtmpl)
19444 /* We might not partially instantiate some parts of the function, so
19445 copy these flags from the original template. */
19446 language_function *ol = DECL_STRUCT_FUNCTION (oldfn)->language;
19447 current_function_returns_value = ol->returns_value;
19448 current_function_returns_null = ol->returns_null;
19449 current_function_returns_abnormally = ol->returns_abnormally;
19450 current_function_infinite_loop = ol->infinite_loop;
19453 /* [temp.deduct] A lambda-expression appearing in a function type or a
19454 template parameter is not considered part of the immediate context for
19455 the purposes of template argument deduction. */
19456 complain = tf_warning_or_error;
19458 tree saved = DECL_SAVED_TREE (oldfn);
19459 if (TREE_CODE (saved) == BIND_EXPR && BIND_EXPR_BODY_BLOCK (saved))
19460 /* We already have a body block from start_lambda_function, we don't
19461 need another to confuse NRV (91217). */
19462 saved = BIND_EXPR_BODY (saved);
19464 tsubst_expr (saved, args, complain, r, /*constexpr*/false);
19466 finish_lambda_function (body);
19468 in_consteval_if_p = save_in_consteval_if_p;
19470 if (nested)
19471 pop_function_context ();
19472 else
19473 --function_depth;
19475 /* The capture list was built up in reverse order; fix that now. */
19476 LAMBDA_EXPR_CAPTURE_LIST (r)
19477 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
19479 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
19481 maybe_add_lambda_conv_op (type);
19484 out:
19485 finish_struct (type, /*attr*/NULL_TREE);
19487 insert_pending_capture_proxies ();
19489 return r;
19492 /* Like tsubst but deals with expressions and performs semantic
19493 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)" or
19494 "F<TARGS> (ARGS)". */
19496 tree
19497 tsubst_copy_and_build (tree t,
19498 tree args,
19499 tsubst_flags_t complain,
19500 tree in_decl,
19501 bool function_p,
19502 bool integral_constant_expression_p)
19504 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
19505 #define RECUR(NODE) \
19506 tsubst_copy_and_build (NODE, args, complain, in_decl, \
19507 /*function_p=*/false, \
19508 integral_constant_expression_p)
19510 tree retval, op1;
19511 location_t save_loc;
19513 if (t == NULL_TREE || t == error_mark_node)
19514 return t;
19516 save_loc = input_location;
19517 if (location_t eloc = cp_expr_location (t))
19518 input_location = eloc;
19520 /* N3276 decltype magic only applies to calls at the top level or on the
19521 right side of a comma. */
19522 tsubst_flags_t decltype_flag = (complain & tf_decltype);
19523 complain &= ~tf_decltype;
19525 switch (TREE_CODE (t))
19527 case USING_DECL:
19528 t = DECL_NAME (t);
19529 /* Fall through. */
19530 case IDENTIFIER_NODE:
19532 tree decl;
19533 cp_id_kind idk;
19534 bool non_integral_constant_expression_p;
19535 const char *error_msg;
19537 if (IDENTIFIER_CONV_OP_P (t))
19539 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19540 t = make_conv_op_name (new_type);
19543 /* Look up the name. */
19544 decl = lookup_name (t);
19546 /* By convention, expressions use ERROR_MARK_NODE to indicate
19547 failure, not NULL_TREE. */
19548 if (decl == NULL_TREE)
19549 decl = error_mark_node;
19551 decl = finish_id_expression (t, decl, NULL_TREE,
19552 &idk,
19553 integral_constant_expression_p,
19554 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
19555 &non_integral_constant_expression_p,
19556 /*template_p=*/false,
19557 /*done=*/true,
19558 /*address_p=*/false,
19559 /*template_arg_p=*/false,
19560 &error_msg,
19561 input_location);
19562 if (error_msg)
19563 error (error_msg);
19564 if (!function_p && identifier_p (decl))
19566 if (complain & tf_error)
19567 unqualified_name_lookup_error (decl);
19568 decl = error_mark_node;
19570 RETURN (decl);
19573 case TEMPLATE_ID_EXPR:
19575 tree object;
19576 tree templ = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
19577 complain, in_decl,
19578 function_p,
19579 integral_constant_expression_p);
19580 tree targs = TREE_OPERAND (t, 1);
19582 if (targs)
19583 targs = tsubst_template_args (targs, args, complain, in_decl);
19584 if (targs == error_mark_node)
19585 RETURN (error_mark_node);
19587 if (TREE_CODE (templ) == SCOPE_REF)
19589 tree name = TREE_OPERAND (templ, 1);
19590 tree tid = lookup_template_function (name, targs);
19591 TREE_OPERAND (templ, 1) = tid;
19592 RETURN (templ);
19595 if (concept_definition_p (templ))
19597 tree check = build_concept_check (templ, targs, complain);
19598 if (check == error_mark_node)
19599 RETURN (error_mark_node);
19601 tree id = unpack_concept_check (check);
19603 /* If we built a function concept check, return the underlying
19604 template-id. So we can evaluate it as a function call. */
19605 if (function_concept_p (TREE_OPERAND (id, 0)))
19606 RETURN (id);
19608 RETURN (check);
19611 if (variable_template_p (templ))
19613 tree r = lookup_and_finish_template_variable (templ, targs,
19614 complain);
19615 r = maybe_wrap_with_location (r, EXPR_LOCATION (t));
19616 RETURN (r);
19619 if (TREE_CODE (templ) == COMPONENT_REF)
19621 object = TREE_OPERAND (templ, 0);
19622 templ = TREE_OPERAND (templ, 1);
19624 else
19625 object = NULL_TREE;
19627 tree tid = lookup_template_function (templ, targs);
19629 if (object)
19630 RETURN (build3 (COMPONENT_REF, TREE_TYPE (tid),
19631 object, tid, NULL_TREE));
19632 else if (identifier_p (templ))
19634 /* C++20 P0846: we can encounter an IDENTIFIER_NODE here when
19635 name lookup found nothing when parsing the template name. */
19636 gcc_assert (cxx_dialect >= cxx20 || seen_error ());
19637 RETURN (tid);
19639 else
19640 RETURN (baselink_for_fns (tid));
19643 case INDIRECT_REF:
19645 tree r = RECUR (TREE_OPERAND (t, 0));
19647 if (REFERENCE_REF_P (t))
19649 /* A type conversion to reference type will be enclosed in
19650 such an indirect ref, but the substitution of the cast
19651 will have also added such an indirect ref. */
19652 r = convert_from_reference (r);
19654 else
19655 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
19656 complain|decltype_flag);
19658 if (REF_PARENTHESIZED_P (t))
19659 r = force_paren_expr (r);
19661 RETURN (r);
19664 case NOP_EXPR:
19666 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19667 tree op0 = RECUR (TREE_OPERAND (t, 0));
19668 RETURN (build_nop (type, op0));
19671 case IMPLICIT_CONV_EXPR:
19673 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19674 tree expr = RECUR (TREE_OPERAND (t, 0));
19675 if (dependent_type_p (type) || type_dependent_expression_p (expr))
19677 retval = copy_node (t);
19678 TREE_TYPE (retval) = type;
19679 TREE_OPERAND (retval, 0) = expr;
19680 RETURN (retval);
19682 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
19683 /* We'll pass this to convert_nontype_argument again, we don't need
19684 to actually perform any conversion here. */
19685 RETURN (expr);
19686 int flags = LOOKUP_IMPLICIT;
19687 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
19688 flags = LOOKUP_NORMAL;
19689 if (IMPLICIT_CONV_EXPR_BRACED_INIT (t))
19690 flags |= LOOKUP_NO_NARROWING;
19691 RETURN (perform_implicit_conversion_flags (type, expr, complain,
19692 flags));
19695 case CONVERT_EXPR:
19697 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19698 tree op0 = RECUR (TREE_OPERAND (t, 0));
19699 if (op0 == error_mark_node)
19700 RETURN (error_mark_node);
19701 RETURN (build1 (CONVERT_EXPR, type, op0));
19704 case CAST_EXPR:
19705 case REINTERPRET_CAST_EXPR:
19706 case CONST_CAST_EXPR:
19707 case DYNAMIC_CAST_EXPR:
19708 case STATIC_CAST_EXPR:
19710 tree type;
19711 tree op, r = NULL_TREE;
19713 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19714 if (integral_constant_expression_p
19715 && !cast_valid_in_integral_constant_expression_p (type))
19717 if (complain & tf_error)
19718 error ("a cast to a type other than an integral or "
19719 "enumeration type cannot appear in a constant-expression");
19720 RETURN (error_mark_node);
19723 op = RECUR (TREE_OPERAND (t, 0));
19725 warning_sentinel s(warn_useless_cast);
19726 warning_sentinel s2(warn_ignored_qualifiers);
19727 warning_sentinel s3(warn_int_in_bool_context);
19728 switch (TREE_CODE (t))
19730 case CAST_EXPR:
19731 r = build_functional_cast (input_location, type, op, complain);
19732 break;
19733 case REINTERPRET_CAST_EXPR:
19734 r = build_reinterpret_cast (input_location, type, op, complain);
19735 break;
19736 case CONST_CAST_EXPR:
19737 r = build_const_cast (input_location, type, op, complain);
19738 break;
19739 case DYNAMIC_CAST_EXPR:
19740 r = build_dynamic_cast (input_location, type, op, complain);
19741 break;
19742 case STATIC_CAST_EXPR:
19743 r = build_static_cast (input_location, type, op, complain);
19744 if (IMPLICIT_RVALUE_P (t))
19745 set_implicit_rvalue_p (r);
19746 break;
19747 default:
19748 gcc_unreachable ();
19751 RETURN (r);
19754 case BIT_CAST_EXPR:
19756 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
19757 tree op0 = RECUR (TREE_OPERAND (t, 0));
19758 RETURN (cp_build_bit_cast (EXPR_LOCATION (t), type, op0, complain));
19761 case POSTDECREMENT_EXPR:
19762 case POSTINCREMENT_EXPR:
19763 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19764 args, complain, in_decl);
19765 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
19766 complain|decltype_flag));
19768 case PREDECREMENT_EXPR:
19769 case PREINCREMENT_EXPR:
19770 case NEGATE_EXPR:
19771 case BIT_NOT_EXPR:
19772 case ABS_EXPR:
19773 case TRUTH_NOT_EXPR:
19774 case UNARY_PLUS_EXPR: /* Unary + */
19775 case REALPART_EXPR:
19776 case IMAGPART_EXPR:
19777 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
19778 RECUR (TREE_OPERAND (t, 0)),
19779 complain|decltype_flag));
19781 case FIX_TRUNC_EXPR:
19782 /* convert_like should have created an IMPLICIT_CONV_EXPR. */
19783 gcc_unreachable ();
19785 case ADDR_EXPR:
19786 op1 = TREE_OPERAND (t, 0);
19787 if (TREE_CODE (op1) == LABEL_DECL)
19788 RETURN (finish_label_address_expr (DECL_NAME (op1),
19789 EXPR_LOCATION (op1)));
19790 if (TREE_CODE (op1) == SCOPE_REF)
19791 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
19792 /*done=*/true, /*address_p=*/true);
19793 else
19794 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
19795 in_decl);
19796 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
19797 complain|decltype_flag));
19799 case PLUS_EXPR:
19800 case MINUS_EXPR:
19801 case MULT_EXPR:
19802 case TRUNC_DIV_EXPR:
19803 case CEIL_DIV_EXPR:
19804 case FLOOR_DIV_EXPR:
19805 case ROUND_DIV_EXPR:
19806 case EXACT_DIV_EXPR:
19807 case BIT_AND_EXPR:
19808 case BIT_IOR_EXPR:
19809 case BIT_XOR_EXPR:
19810 case TRUNC_MOD_EXPR:
19811 case FLOOR_MOD_EXPR:
19812 case TRUTH_ANDIF_EXPR:
19813 case TRUTH_ORIF_EXPR:
19814 case TRUTH_AND_EXPR:
19815 case TRUTH_OR_EXPR:
19816 case RSHIFT_EXPR:
19817 case LSHIFT_EXPR:
19818 case EQ_EXPR:
19819 case NE_EXPR:
19820 case MAX_EXPR:
19821 case MIN_EXPR:
19822 case LE_EXPR:
19823 case GE_EXPR:
19824 case LT_EXPR:
19825 case GT_EXPR:
19826 case SPACESHIP_EXPR:
19827 case MEMBER_REF:
19828 case DOTSTAR_EXPR:
19830 /* If either OP0 or OP1 was value- or type-dependent, suppress
19831 warnings that depend on the range of the types involved. */
19832 tree op0 = TREE_OPERAND (t, 0);
19833 tree op1 = TREE_OPERAND (t, 1);
19834 auto dep_p = [](tree t) {
19835 ++processing_template_decl;
19836 bool r = (potential_constant_expression (t)
19837 ? value_dependent_expression_p (t)
19838 : type_dependent_expression_p (t));
19839 --processing_template_decl;
19840 return r;
19842 const bool was_dep = dep_p (op0) || dep_p (op1);
19843 op0 = RECUR (op0);
19844 op1 = RECUR (op1);
19846 warning_sentinel s1(warn_type_limits, was_dep);
19847 warning_sentinel s2(warn_div_by_zero, was_dep);
19848 warning_sentinel s3(warn_logical_op, was_dep);
19849 warning_sentinel s4(warn_tautological_compare, was_dep);
19851 tree r = build_x_binary_op
19852 (input_location, TREE_CODE (t),
19853 op0,
19854 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
19855 ? ERROR_MARK
19856 : TREE_CODE (TREE_OPERAND (t, 0))),
19857 op1,
19858 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
19859 ? ERROR_MARK
19860 : TREE_CODE (TREE_OPERAND (t, 1))),
19861 /*overload=*/NULL,
19862 complain|decltype_flag);
19863 if (EXPR_P (r) && TREE_NO_WARNING (t))
19864 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
19866 RETURN (r);
19869 case POINTER_PLUS_EXPR:
19871 tree op0 = RECUR (TREE_OPERAND (t, 0));
19872 if (op0 == error_mark_node)
19873 RETURN (error_mark_node);
19874 tree op1 = RECUR (TREE_OPERAND (t, 1));
19875 if (op1 == error_mark_node)
19876 RETURN (error_mark_node);
19877 RETURN (fold_build_pointer_plus (op0, op1));
19880 case SCOPE_REF:
19881 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
19882 /*address_p=*/false));
19884 case BASELINK:
19885 RETURN (tsubst_baselink (t, current_nonlambda_class_type (),
19886 args, complain, in_decl));
19888 case ARRAY_REF:
19889 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
19890 args, complain, in_decl);
19891 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
19892 RECUR (TREE_OPERAND (t, 1)),
19893 complain|decltype_flag));
19895 case SIZEOF_EXPR:
19896 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
19897 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
19898 RETURN (tsubst_copy (t, args, complain, in_decl));
19899 /* Fall through */
19901 case ALIGNOF_EXPR:
19903 tree r;
19905 op1 = TREE_OPERAND (t, 0);
19906 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
19907 op1 = TREE_TYPE (op1);
19908 bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR
19909 && ALIGNOF_EXPR_STD_P (t));
19910 if (!args)
19912 /* When there are no ARGS, we are trying to evaluate a
19913 non-dependent expression from the parser. Trying to do
19914 the substitutions may not work. */
19915 if (!TYPE_P (op1))
19916 op1 = TREE_TYPE (op1);
19918 else
19920 ++cp_unevaluated_operand;
19921 ++c_inhibit_evaluation_warnings;
19922 if (TYPE_P (op1))
19923 op1 = tsubst (op1, args, complain, in_decl);
19924 else
19925 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19926 /*function_p=*/false,
19927 /*integral_constant_expression_p=*/
19928 false);
19929 --cp_unevaluated_operand;
19930 --c_inhibit_evaluation_warnings;
19932 if (TYPE_P (op1))
19933 r = cxx_sizeof_or_alignof_type (input_location,
19934 op1, TREE_CODE (t), std_alignof,
19935 complain & tf_error);
19936 else
19937 r = cxx_sizeof_or_alignof_expr (input_location,
19938 op1, TREE_CODE (t), std_alignof,
19939 complain & tf_error);
19940 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
19942 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
19944 if (!processing_template_decl && TYPE_P (op1))
19946 r = build_min (SIZEOF_EXPR, size_type_node,
19947 build1 (NOP_EXPR, op1, error_mark_node));
19948 SIZEOF_EXPR_TYPE_P (r) = 1;
19950 else
19951 r = build_min (SIZEOF_EXPR, size_type_node, op1);
19952 TREE_SIDE_EFFECTS (r) = 0;
19953 TREE_READONLY (r) = 1;
19955 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
19957 RETURN (r);
19960 case AT_ENCODE_EXPR:
19962 op1 = TREE_OPERAND (t, 0);
19963 ++cp_unevaluated_operand;
19964 ++c_inhibit_evaluation_warnings;
19965 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19966 /*function_p=*/false,
19967 /*integral_constant_expression_p=*/false);
19968 --cp_unevaluated_operand;
19969 --c_inhibit_evaluation_warnings;
19970 RETURN (objc_build_encode_expr (op1));
19973 case NOEXCEPT_EXPR:
19974 op1 = TREE_OPERAND (t, 0);
19975 ++cp_unevaluated_operand;
19976 ++c_inhibit_evaluation_warnings;
19977 ++cp_noexcept_operand;
19978 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
19979 /*function_p=*/false,
19980 /*integral_constant_expression_p=*/false);
19981 --cp_unevaluated_operand;
19982 --c_inhibit_evaluation_warnings;
19983 --cp_noexcept_operand;
19984 RETURN (finish_noexcept_expr (op1, complain));
19986 case MODOP_EXPR:
19988 warning_sentinel s(warn_div_by_zero);
19989 tree lhs = RECUR (TREE_OPERAND (t, 0));
19990 tree rhs = RECUR (TREE_OPERAND (t, 2));
19991 tree r = build_x_modify_expr
19992 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
19993 complain|decltype_flag);
19994 /* TREE_NO_WARNING must be set if either the expression was
19995 parenthesized or it uses an operator such as >>= rather
19996 than plain assignment. In the former case, it was already
19997 set and must be copied. In the latter case,
19998 build_x_modify_expr sets it and it must not be reset
19999 here. */
20000 if (TREE_NO_WARNING (t))
20001 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
20003 RETURN (r);
20006 case ARROW_EXPR:
20007 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
20008 args, complain, in_decl);
20009 /* Remember that there was a reference to this entity. */
20010 if (DECL_P (op1)
20011 && !mark_used (op1, complain) && !(complain & tf_error))
20012 RETURN (error_mark_node);
20013 RETURN (build_x_arrow (input_location, op1, complain));
20015 case NEW_EXPR:
20017 tree placement = RECUR (TREE_OPERAND (t, 0));
20018 tree init = RECUR (TREE_OPERAND (t, 3));
20019 vec<tree, va_gc> *placement_vec;
20020 vec<tree, va_gc> *init_vec;
20021 tree ret;
20022 location_t loc = EXPR_LOCATION (t);
20024 if (placement == NULL_TREE)
20025 placement_vec = NULL;
20026 else if (placement == error_mark_node)
20027 RETURN (error_mark_node);
20028 else
20030 placement_vec = make_tree_vector ();
20031 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
20032 vec_safe_push (placement_vec, TREE_VALUE (placement));
20035 /* If there was an initializer in the original tree, but it
20036 instantiated to an empty list, then we should pass a
20037 non-NULL empty vector to tell build_new that it was an
20038 empty initializer() rather than no initializer. This can
20039 only happen when the initializer is a pack expansion whose
20040 parameter packs are of length zero. */
20041 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
20042 init_vec = NULL;
20043 else if (init == error_mark_node)
20044 RETURN (error_mark_node);
20045 else
20047 init_vec = make_tree_vector ();
20048 if (init == void_node)
20049 gcc_assert (init_vec != NULL);
20050 else
20052 for (; init != NULL_TREE; init = TREE_CHAIN (init))
20053 vec_safe_push (init_vec, TREE_VALUE (init));
20057 /* Avoid passing an enclosing decl to valid_array_size_p. */
20058 in_decl = NULL_TREE;
20060 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
20061 tree op2 = RECUR (TREE_OPERAND (t, 2));
20062 ret = build_new (loc, &placement_vec, op1, op2,
20063 &init_vec, NEW_EXPR_USE_GLOBAL (t),
20064 complain);
20066 if (placement_vec != NULL)
20067 release_tree_vector (placement_vec);
20068 if (init_vec != NULL)
20069 release_tree_vector (init_vec);
20071 RETURN (ret);
20074 case DELETE_EXPR:
20076 tree op0 = RECUR (TREE_OPERAND (t, 0));
20077 tree op1 = RECUR (TREE_OPERAND (t, 1));
20078 RETURN (delete_sanity (input_location, op0, op1,
20079 DELETE_EXPR_USE_VEC (t),
20080 DELETE_EXPR_USE_GLOBAL (t),
20081 complain));
20084 case COMPOUND_EXPR:
20086 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
20087 complain & ~tf_decltype, in_decl,
20088 /*function_p=*/false,
20089 integral_constant_expression_p);
20090 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
20091 op0,
20092 RECUR (TREE_OPERAND (t, 1)),
20093 complain|decltype_flag));
20096 case CALL_EXPR:
20098 tree function;
20099 unsigned int nargs, i;
20100 bool qualified_p;
20101 bool koenig_p;
20102 tree ret;
20104 function = CALL_EXPR_FN (t);
20105 /* Internal function with no arguments. */
20106 if (function == NULL_TREE && call_expr_nargs (t) == 0)
20107 RETURN (t);
20109 /* When we parsed the expression, we determined whether or
20110 not Koenig lookup should be performed. */
20111 koenig_p = KOENIG_LOOKUP_P (t);
20112 if (function == NULL_TREE)
20114 koenig_p = false;
20115 qualified_p = false;
20117 else if (TREE_CODE (function) == SCOPE_REF)
20119 qualified_p = true;
20120 function = tsubst_qualified_id (function, args, complain, in_decl,
20121 /*done=*/false,
20122 /*address_p=*/false);
20124 else if (koenig_p && identifier_p (function))
20126 /* Do nothing; calling tsubst_copy_and_build on an identifier
20127 would incorrectly perform unqualified lookup again.
20129 Note that we can also have an IDENTIFIER_NODE if the earlier
20130 unqualified lookup found a member function; in that case
20131 koenig_p will be false and we do want to do the lookup
20132 again to find the instantiated member function.
20134 FIXME but doing that causes c++/15272, so we need to stop
20135 using IDENTIFIER_NODE in that situation. */
20136 qualified_p = false;
20138 else
20140 if (TREE_CODE (function) == COMPONENT_REF)
20142 tree op = TREE_OPERAND (function, 1);
20144 qualified_p = (TREE_CODE (op) == SCOPE_REF
20145 || (BASELINK_P (op)
20146 && BASELINK_QUALIFIED_P (op)));
20148 else
20149 qualified_p = false;
20151 if (TREE_CODE (function) == ADDR_EXPR
20152 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
20153 /* Avoid error about taking the address of a constructor. */
20154 function = TREE_OPERAND (function, 0);
20156 tsubst_flags_t subcomplain = complain;
20157 if (koenig_p && TREE_CODE (function) == FUNCTION_DECL)
20158 /* When KOENIG_P, we don't want to mark_used the callee before
20159 augmenting the overload set via ADL, so during this initial
20160 substitution we disable mark_used by setting tf_conv (68942). */
20161 subcomplain |= tf_conv;
20162 function = tsubst_copy_and_build (function, args, subcomplain,
20163 in_decl,
20164 !qualified_p,
20165 integral_constant_expression_p);
20167 if (BASELINK_P (function))
20168 qualified_p = true;
20171 nargs = call_expr_nargs (t);
20172 releasing_vec call_args;
20173 for (i = 0; i < nargs; ++i)
20175 tree arg = CALL_EXPR_ARG (t, i);
20177 if (!PACK_EXPANSION_P (arg))
20178 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
20179 else
20181 /* Expand the pack expansion and push each entry onto
20182 CALL_ARGS. */
20183 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
20184 if (TREE_CODE (arg) == TREE_VEC)
20186 unsigned int len, j;
20188 len = TREE_VEC_LENGTH (arg);
20189 for (j = 0; j < len; ++j)
20191 tree value = TREE_VEC_ELT (arg, j);
20192 if (value != NULL_TREE)
20193 value = convert_from_reference (value);
20194 vec_safe_push (call_args, value);
20197 else
20199 /* A partial substitution. Add one entry. */
20200 vec_safe_push (call_args, arg);
20205 /* Stripped-down processing for a call in a thunk. Specifically, in
20206 the thunk template for a generic lambda. */
20207 if (call_from_lambda_thunk_p (t))
20209 /* Now that we've expanded any packs, the number of call args
20210 might be different. */
20211 unsigned int cargs = call_args->length ();
20212 tree thisarg = NULL_TREE;
20213 if (TREE_CODE (function) == COMPONENT_REF)
20215 thisarg = TREE_OPERAND (function, 0);
20216 if (TREE_CODE (thisarg) == INDIRECT_REF)
20217 thisarg = TREE_OPERAND (thisarg, 0);
20218 function = TREE_OPERAND (function, 1);
20219 if (TREE_CODE (function) == BASELINK)
20220 function = BASELINK_FUNCTIONS (function);
20222 /* We aren't going to do normal overload resolution, so force the
20223 template-id to resolve. */
20224 function = resolve_nondeduced_context (function, complain);
20225 for (unsigned i = 0; i < cargs; ++i)
20227 /* In a thunk, pass through args directly, without any
20228 conversions. */
20229 tree arg = (*call_args)[i];
20230 while (TREE_CODE (arg) != PARM_DECL)
20231 arg = TREE_OPERAND (arg, 0);
20232 (*call_args)[i] = arg;
20234 if (thisarg)
20236 /* If there are no other args, just push 'this'. */
20237 if (cargs == 0)
20238 vec_safe_push (call_args, thisarg);
20239 else
20241 /* Otherwise, shift the other args over to make room. */
20242 tree last = (*call_args)[cargs - 1];
20243 vec_safe_push (call_args, last);
20244 for (int i = cargs - 1; i > 0; --i)
20245 (*call_args)[i] = (*call_args)[i - 1];
20246 (*call_args)[0] = thisarg;
20249 ret = build_call_a (function, call_args->length (),
20250 call_args->address ());
20251 /* The thunk location is not interesting. */
20252 SET_EXPR_LOCATION (ret, UNKNOWN_LOCATION);
20253 CALL_FROM_THUNK_P (ret) = true;
20254 if (CLASS_TYPE_P (TREE_TYPE (ret)))
20255 CALL_EXPR_RETURN_SLOT_OPT (ret) = true;
20257 RETURN (ret);
20260 /* We do not perform argument-dependent lookup if normal
20261 lookup finds a non-function, in accordance with the
20262 resolution of DR 218. */
20263 if (koenig_p
20264 && ((is_overloaded_fn (function)
20265 /* If lookup found a member function, the Koenig lookup is
20266 not appropriate, even if an unqualified-name was used
20267 to denote the function. */
20268 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
20269 || identifier_p (function)
20270 /* C++20 P0846: Lookup found nothing. */
20271 || (TREE_CODE (function) == TEMPLATE_ID_EXPR
20272 && identifier_p (TREE_OPERAND (function, 0))))
20273 /* Only do this when substitution turns a dependent call
20274 into a non-dependent call. */
20275 && type_dependent_expression_p_push (t)
20276 && !any_type_dependent_arguments_p (call_args))
20277 function = perform_koenig_lookup (function, call_args, tf_none);
20279 if (function != NULL_TREE
20280 && (identifier_p (function)
20281 || (TREE_CODE (function) == TEMPLATE_ID_EXPR
20282 && identifier_p (TREE_OPERAND (function, 0))))
20283 && !any_type_dependent_arguments_p (call_args))
20285 if (TREE_CODE (function) == TEMPLATE_ID_EXPR)
20286 function = TREE_OPERAND (function, 0);
20287 if (koenig_p && (complain & tf_warning_or_error))
20289 /* For backwards compatibility and good diagnostics, try
20290 the unqualified lookup again if we aren't in SFINAE
20291 context. */
20292 tree unq = (tsubst_copy_and_build
20293 (function, args, complain, in_decl, true,
20294 integral_constant_expression_p));
20295 if (unq == error_mark_node)
20296 RETURN (error_mark_node);
20298 if (unq != function)
20300 /* In a lambda fn, we have to be careful to not
20301 introduce new this captures. Legacy code can't
20302 be using lambdas anyway, so it's ok to be
20303 stricter. */
20304 bool in_lambda = (current_class_type
20305 && LAMBDA_TYPE_P (current_class_type));
20306 char const *const msg
20307 = G_("%qD was not declared in this scope, "
20308 "and no declarations were found by "
20309 "argument-dependent lookup at the point "
20310 "of instantiation");
20312 bool diag = true;
20313 if (in_lambda)
20314 error_at (cp_expr_loc_or_input_loc (t),
20315 msg, function);
20316 else
20317 diag = permerror (cp_expr_loc_or_input_loc (t),
20318 msg, function);
20319 if (diag)
20321 tree fn = unq;
20323 if (INDIRECT_REF_P (fn))
20324 fn = TREE_OPERAND (fn, 0);
20325 if (is_overloaded_fn (fn))
20326 fn = get_first_fn (fn);
20328 if (!DECL_P (fn))
20329 /* Can't say anything more. */;
20330 else if (DECL_CLASS_SCOPE_P (fn))
20332 location_t loc = cp_expr_loc_or_input_loc (t);
20333 inform (loc,
20334 "declarations in dependent base %qT are "
20335 "not found by unqualified lookup",
20336 DECL_CLASS_CONTEXT (fn));
20337 if (current_class_ptr)
20338 inform (loc,
20339 "use %<this->%D%> instead", function);
20340 else
20341 inform (loc,
20342 "use %<%T::%D%> instead",
20343 current_class_name, function);
20345 else
20346 inform (DECL_SOURCE_LOCATION (fn),
20347 "%qD declared here, later in the "
20348 "translation unit", fn);
20349 if (in_lambda)
20350 RETURN (error_mark_node);
20353 function = unq;
20356 if (identifier_p (function))
20358 if (complain & tf_error)
20359 unqualified_name_lookup_error (function);
20360 RETURN (error_mark_node);
20364 /* Remember that there was a reference to this entity. */
20365 if (function != NULL_TREE
20366 && DECL_P (function)
20367 && !mark_used (function, complain) && !(complain & tf_error))
20368 RETURN (error_mark_node);
20370 /* Put back tf_decltype for the actual call. */
20371 complain |= decltype_flag;
20373 if (function == NULL_TREE)
20374 switch (CALL_EXPR_IFN (t))
20376 case IFN_LAUNDER:
20377 gcc_assert (nargs == 1);
20378 if (vec_safe_length (call_args) != 1)
20380 error_at (cp_expr_loc_or_input_loc (t),
20381 "wrong number of arguments to "
20382 "%<__builtin_launder%>");
20383 ret = error_mark_node;
20385 else
20386 ret = finish_builtin_launder (cp_expr_loc_or_input_loc (t),
20387 (*call_args)[0], complain);
20388 break;
20390 case IFN_VEC_CONVERT:
20391 gcc_assert (nargs == 1);
20392 if (vec_safe_length (call_args) != 1)
20394 error_at (cp_expr_loc_or_input_loc (t),
20395 "wrong number of arguments to "
20396 "%<__builtin_convertvector%>");
20397 ret = error_mark_node;
20398 break;
20400 ret = cp_build_vec_convert ((*call_args)[0], input_location,
20401 tsubst (TREE_TYPE (t), args,
20402 complain, in_decl),
20403 complain);
20404 if (TREE_CODE (ret) == VIEW_CONVERT_EXPR)
20405 RETURN (ret);
20406 break;
20408 case IFN_SHUFFLEVECTOR:
20410 ret = build_x_shufflevector (input_location, call_args,
20411 complain);
20412 if (ret != error_mark_node)
20413 RETURN (ret);
20414 break;
20417 default:
20418 /* Unsupported internal function with arguments. */
20419 gcc_unreachable ();
20421 else if (TREE_CODE (function) == OFFSET_REF
20422 || TREE_CODE (function) == DOTSTAR_EXPR
20423 || TREE_CODE (function) == MEMBER_REF)
20424 ret = build_offset_ref_call_from_tree (function, &call_args,
20425 complain);
20426 else if (TREE_CODE (function) == COMPONENT_REF)
20428 tree instance = TREE_OPERAND (function, 0);
20429 tree fn = TREE_OPERAND (function, 1);
20431 if (processing_template_decl
20432 && (type_dependent_expression_p (instance)
20433 || (!BASELINK_P (fn)
20434 && TREE_CODE (fn) != FIELD_DECL)
20435 || type_dependent_expression_p (fn)
20436 || any_type_dependent_arguments_p (call_args)))
20437 ret = build_min_nt_call_vec (function, call_args);
20438 else if (!BASELINK_P (fn))
20439 ret = finish_call_expr (function, &call_args,
20440 /*disallow_virtual=*/false,
20441 /*koenig_p=*/false,
20442 complain);
20443 else
20444 ret = (build_new_method_call
20445 (instance, fn,
20446 &call_args, NULL_TREE,
20447 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
20448 /*fn_p=*/NULL,
20449 complain));
20451 else if (concept_check_p (function))
20453 /* FUNCTION is a template-id referring to a concept definition. */
20454 tree id = unpack_concept_check (function);
20455 tree tmpl = TREE_OPERAND (id, 0);
20456 tree args = TREE_OPERAND (id, 1);
20458 /* Calls to standard and variable concepts should have been
20459 previously diagnosed. */
20460 gcc_assert (function_concept_p (tmpl));
20462 /* Ensure the result is wrapped as a call expression. */
20463 ret = build_concept_check (tmpl, args, tf_warning_or_error);
20465 else
20466 ret = finish_call_expr (function, &call_args,
20467 /*disallow_virtual=*/qualified_p,
20468 koenig_p,
20469 complain);
20471 if (ret != error_mark_node)
20473 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
20474 bool ord = CALL_EXPR_ORDERED_ARGS (t);
20475 bool rev = CALL_EXPR_REVERSE_ARGS (t);
20476 if (op || ord || rev)
20478 function = extract_call_expr (ret);
20479 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
20480 CALL_EXPR_ORDERED_ARGS (function) = ord;
20481 CALL_EXPR_REVERSE_ARGS (function) = rev;
20485 RETURN (ret);
20488 case COND_EXPR:
20490 tree cond = RECUR (TREE_OPERAND (t, 0));
20491 cond = mark_rvalue_use (cond);
20492 tree folded_cond = fold_non_dependent_expr (cond, complain);
20493 tree exp1, exp2;
20495 if (TREE_CODE (folded_cond) == INTEGER_CST)
20497 if (integer_zerop (folded_cond))
20499 ++c_inhibit_evaluation_warnings;
20500 exp1 = RECUR (TREE_OPERAND (t, 1));
20501 --c_inhibit_evaluation_warnings;
20502 exp2 = RECUR (TREE_OPERAND (t, 2));
20504 else
20506 exp1 = RECUR (TREE_OPERAND (t, 1));
20507 ++c_inhibit_evaluation_warnings;
20508 exp2 = RECUR (TREE_OPERAND (t, 2));
20509 --c_inhibit_evaluation_warnings;
20511 cond = folded_cond;
20513 else
20515 exp1 = RECUR (TREE_OPERAND (t, 1));
20516 exp2 = RECUR (TREE_OPERAND (t, 2));
20519 warning_sentinel s(warn_duplicated_branches);
20520 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
20521 cond, exp1, exp2, complain));
20524 case PSEUDO_DTOR_EXPR:
20526 tree op0 = RECUR (TREE_OPERAND (t, 0));
20527 tree op1 = RECUR (TREE_OPERAND (t, 1));
20528 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
20529 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
20530 input_location));
20533 case TREE_LIST:
20534 RETURN (tsubst_tree_list (t, args, complain, in_decl));
20536 case COMPONENT_REF:
20538 tree object;
20539 tree object_type;
20540 tree member;
20541 tree r;
20543 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
20544 args, complain, in_decl);
20545 /* Remember that there was a reference to this entity. */
20546 if (DECL_P (object)
20547 && !mark_used (object, complain) && !(complain & tf_error))
20548 RETURN (error_mark_node);
20549 object_type = TREE_TYPE (object);
20551 member = TREE_OPERAND (t, 1);
20552 if (BASELINK_P (member))
20553 member = tsubst_baselink (member,
20554 non_reference (TREE_TYPE (object)),
20555 args, complain, in_decl);
20556 else
20557 member = tsubst_copy (member, args, complain, in_decl);
20558 if (member == error_mark_node)
20559 RETURN (error_mark_node);
20561 if (TREE_CODE (member) == FIELD_DECL)
20563 r = finish_non_static_data_member (member, object, NULL_TREE);
20564 if (TREE_CODE (r) == COMPONENT_REF)
20565 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
20566 RETURN (r);
20568 else if (type_dependent_expression_p (object))
20569 /* We can't do much here. */;
20570 else if (!CLASS_TYPE_P (object_type))
20572 if (scalarish_type_p (object_type))
20574 tree s = NULL_TREE;
20575 tree dtor = member;
20577 if (TREE_CODE (dtor) == SCOPE_REF)
20579 s = TREE_OPERAND (dtor, 0);
20580 dtor = TREE_OPERAND (dtor, 1);
20582 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
20584 dtor = TREE_OPERAND (dtor, 0);
20585 if (TYPE_P (dtor))
20586 RETURN (finish_pseudo_destructor_expr
20587 (object, s, dtor, input_location));
20591 else if (TREE_CODE (member) == SCOPE_REF
20592 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
20594 /* Lookup the template functions now that we know what the
20595 scope is. */
20596 tree scope = TREE_OPERAND (member, 0);
20597 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
20598 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
20599 member = lookup_qualified_name (scope, tmpl, LOOK_want::NORMAL,
20600 /*complain=*/false);
20601 if (BASELINK_P (member))
20603 BASELINK_FUNCTIONS (member)
20604 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
20605 args);
20606 member = (adjust_result_of_qualified_name_lookup
20607 (member, BINFO_TYPE (BASELINK_BINFO (member)),
20608 object_type));
20610 else
20612 qualified_name_lookup_error (scope, tmpl, member,
20613 input_location);
20614 RETURN (error_mark_node);
20617 else if (TREE_CODE (member) == SCOPE_REF
20618 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
20619 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
20621 if (complain & tf_error)
20623 if (TYPE_P (TREE_OPERAND (member, 0)))
20624 error ("%qT is not a class or namespace",
20625 TREE_OPERAND (member, 0));
20626 else
20627 error ("%qD is not a class or namespace",
20628 TREE_OPERAND (member, 0));
20630 RETURN (error_mark_node);
20633 r = finish_class_member_access_expr (object, member,
20634 /*template_p=*/false,
20635 complain);
20636 if (TREE_CODE (r) == COMPONENT_REF)
20637 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
20638 RETURN (r);
20641 case THROW_EXPR:
20642 RETURN (build_throw
20643 (input_location, RECUR (TREE_OPERAND (t, 0))));
20645 case CONSTRUCTOR:
20647 vec<constructor_elt, va_gc> *n;
20648 constructor_elt *ce;
20649 unsigned HOST_WIDE_INT idx;
20650 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20651 bool process_index_p;
20652 int newlen;
20653 bool need_copy_p = false;
20654 tree r;
20656 if (type == error_mark_node)
20657 RETURN (error_mark_node);
20659 /* We do not want to process the index of aggregate
20660 initializers as they are identifier nodes which will be
20661 looked up by digest_init. */
20662 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
20664 if (null_member_pointer_value_p (t))
20666 gcc_assert (same_type_p (type, TREE_TYPE (t)));
20667 RETURN (t);
20670 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
20671 newlen = vec_safe_length (n);
20672 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
20674 if (ce->index && process_index_p
20675 /* An identifier index is looked up in the type
20676 being initialized, not the current scope. */
20677 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
20678 ce->index = RECUR (ce->index);
20680 if (PACK_EXPANSION_P (ce->value))
20682 /* Substitute into the pack expansion. */
20683 ce->value = tsubst_pack_expansion (ce->value, args, complain,
20684 in_decl);
20686 if (ce->value == error_mark_node
20687 || PACK_EXPANSION_P (ce->value))
20689 else if (TREE_VEC_LENGTH (ce->value) == 1)
20690 /* Just move the argument into place. */
20691 ce->value = TREE_VEC_ELT (ce->value, 0);
20692 else
20694 /* Update the length of the final CONSTRUCTOR
20695 arguments vector, and note that we will need to
20696 copy.*/
20697 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
20698 need_copy_p = true;
20701 else
20702 ce->value = RECUR (ce->value);
20705 if (need_copy_p)
20707 vec<constructor_elt, va_gc> *old_n = n;
20709 vec_alloc (n, newlen);
20710 FOR_EACH_VEC_ELT (*old_n, idx, ce)
20712 if (TREE_CODE (ce->value) == TREE_VEC)
20714 int i, len = TREE_VEC_LENGTH (ce->value);
20715 for (i = 0; i < len; ++i)
20716 CONSTRUCTOR_APPEND_ELT (n, 0,
20717 TREE_VEC_ELT (ce->value, i));
20719 else
20720 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
20724 r = build_constructor (init_list_type_node, n);
20725 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
20726 CONSTRUCTOR_IS_DESIGNATED_INIT (r)
20727 = CONSTRUCTOR_IS_DESIGNATED_INIT (t);
20729 if (TREE_HAS_CONSTRUCTOR (t))
20731 fcl_t cl = fcl_functional;
20732 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
20733 cl = fcl_c99;
20734 RETURN (finish_compound_literal (type, r, complain, cl));
20737 TREE_TYPE (r) = type;
20738 RETURN (r);
20741 case TYPEID_EXPR:
20743 tree operand_0 = TREE_OPERAND (t, 0);
20744 if (TYPE_P (operand_0))
20746 operand_0 = tsubst (operand_0, args, complain, in_decl);
20747 RETURN (get_typeid (operand_0, complain));
20749 else
20751 operand_0 = RECUR (operand_0);
20752 RETURN (build_typeid (operand_0, complain));
20756 case VAR_DECL:
20757 if (!args)
20758 RETURN (t);
20759 /* Fall through */
20761 case PARM_DECL:
20763 tree r = tsubst_copy (t, args, complain, in_decl);
20764 /* ??? We're doing a subset of finish_id_expression here. */
20765 if (tree wrap = maybe_get_tls_wrapper_call (r))
20766 /* Replace an evaluated use of the thread_local variable with
20767 a call to its wrapper. */
20768 r = wrap;
20769 else if (outer_automatic_var_p (r))
20770 r = process_outer_var_ref (r, complain);
20772 if (!TYPE_REF_P (TREE_TYPE (t)))
20773 /* If the original type was a reference, we'll be wrapped in
20774 the appropriate INDIRECT_REF. */
20775 r = convert_from_reference (r);
20776 RETURN (r);
20779 case VA_ARG_EXPR:
20781 tree op0 = RECUR (TREE_OPERAND (t, 0));
20782 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
20783 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
20786 case OFFSETOF_EXPR:
20788 tree object_ptr
20789 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
20790 in_decl, /*function_p=*/false,
20791 /*integral_constant_expression_p=*/false);
20792 RETURN (finish_offsetof (object_ptr,
20793 RECUR (TREE_OPERAND (t, 0)),
20794 EXPR_LOCATION (t)));
20797 case ADDRESSOF_EXPR:
20798 RETURN (cp_build_addressof (EXPR_LOCATION (t),
20799 RECUR (TREE_OPERAND (t, 0)), complain));
20801 case TRAIT_EXPR:
20803 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
20804 complain, in_decl);
20805 tree type2 = tsubst (TRAIT_EXPR_TYPE2 (t), args,
20806 complain, in_decl);
20807 RETURN (finish_trait_expr (TRAIT_EXPR_LOCATION (t),
20808 TRAIT_EXPR_KIND (t), type1, type2));
20811 case STMT_EXPR:
20813 tree old_stmt_expr = cur_stmt_expr;
20814 tree stmt_expr = begin_stmt_expr ();
20816 cur_stmt_expr = stmt_expr;
20817 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
20818 integral_constant_expression_p);
20819 stmt_expr = finish_stmt_expr (stmt_expr, false);
20820 cur_stmt_expr = old_stmt_expr;
20822 /* If the resulting list of expression statement is empty,
20823 fold it further into void_node. */
20824 if (empty_expr_stmt_p (stmt_expr))
20825 stmt_expr = void_node;
20827 RETURN (stmt_expr);
20830 case LAMBDA_EXPR:
20832 if (complain & tf_partial)
20834 /* We don't have a full set of template arguments yet; don't touch
20835 the lambda at all. */
20836 gcc_assert (processing_template_decl);
20837 return t;
20839 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
20841 RETURN (build_lambda_object (r));
20844 case TARGET_EXPR:
20845 /* We can get here for a constant initializer of non-dependent type.
20846 FIXME stop folding in cp_parser_initializer_clause. */
20848 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
20849 complain);
20850 RETURN (r);
20853 case TRANSACTION_EXPR:
20854 RETURN (tsubst_expr(t, args, complain, in_decl,
20855 integral_constant_expression_p));
20857 case PAREN_EXPR:
20858 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
20860 case VEC_PERM_EXPR:
20862 tree op0 = RECUR (TREE_OPERAND (t, 0));
20863 tree op1 = RECUR (TREE_OPERAND (t, 1));
20864 tree op2 = RECUR (TREE_OPERAND (t, 2));
20865 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
20866 complain));
20869 case REQUIRES_EXPR:
20871 tree r = tsubst_requires_expr (t, args, tf_none, in_decl);
20872 RETURN (r);
20875 case RANGE_EXPR:
20876 /* No need to substitute further, a RANGE_EXPR will always be built
20877 with constant operands. */
20878 RETURN (t);
20880 case NON_LVALUE_EXPR:
20881 case VIEW_CONVERT_EXPR:
20882 if (location_wrapper_p (t))
20883 /* We need to do this here as well as in tsubst_copy so we get the
20884 other tsubst_copy_and_build semantics for a PARM_DECL operand. */
20885 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)),
20886 EXPR_LOCATION (t)));
20887 /* fallthrough. */
20889 default:
20890 /* Handle Objective-C++ constructs, if appropriate. */
20892 tree subst
20893 = objcp_tsubst_copy_and_build (t, args, complain,
20894 in_decl, /*function_p=*/false);
20895 if (subst)
20896 RETURN (subst);
20898 RETURN (tsubst_copy (t, args, complain, in_decl));
20901 #undef RECUR
20902 #undef RETURN
20903 out:
20904 input_location = save_loc;
20905 return retval;
20908 /* Verify that the instantiated ARGS are valid. For type arguments,
20909 make sure that the type's linkage is ok. For non-type arguments,
20910 make sure they are constants if they are integral or enumerations.
20911 Emit an error under control of COMPLAIN, and return TRUE on error. */
20913 static bool
20914 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
20916 if (dependent_template_arg_p (t))
20917 return false;
20918 if (ARGUMENT_PACK_P (t))
20920 tree vec = ARGUMENT_PACK_ARGS (t);
20921 int len = TREE_VEC_LENGTH (vec);
20922 bool result = false;
20923 int i;
20925 for (i = 0; i < len; ++i)
20926 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
20927 result = true;
20928 return result;
20930 else if (TYPE_P (t))
20932 /* [basic.link]: A name with no linkage (notably, the name
20933 of a class or enumeration declared in a local scope)
20934 shall not be used to declare an entity with linkage.
20935 This implies that names with no linkage cannot be used as
20936 template arguments
20938 DR 757 relaxes this restriction for C++0x. */
20939 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
20940 : no_linkage_check (t, /*relaxed_p=*/false));
20942 if (nt)
20944 /* DR 488 makes use of a type with no linkage cause
20945 type deduction to fail. */
20946 if (complain & tf_error)
20948 if (TYPE_UNNAMED_P (nt))
20949 error ("%qT is/uses unnamed type", t);
20950 else
20951 error ("template argument for %qD uses local type %qT",
20952 tmpl, t);
20954 return true;
20956 /* In order to avoid all sorts of complications, we do not
20957 allow variably-modified types as template arguments. */
20958 else if (variably_modified_type_p (t, NULL_TREE))
20960 if (complain & tf_error)
20961 error ("%qT is a variably modified type", t);
20962 return true;
20965 /* Class template and alias template arguments should be OK. */
20966 else if (DECL_TYPE_TEMPLATE_P (t))
20968 /* A non-type argument of integral or enumerated type must be a
20969 constant. */
20970 else if (TREE_TYPE (t)
20971 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
20972 && !REFERENCE_REF_P (t)
20973 && !TREE_CONSTANT (t))
20975 if (complain & tf_error)
20976 error ("integral expression %qE is not constant", t);
20977 return true;
20979 return false;
20982 static bool
20983 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
20985 int ix, len = DECL_NTPARMS (tmpl);
20986 bool result = false;
20988 for (ix = 0; ix != len; ix++)
20990 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
20991 result = true;
20993 if (result && (complain & tf_error))
20994 error (" trying to instantiate %qD", tmpl);
20995 return result;
20998 /* We're out of SFINAE context now, so generate diagnostics for the access
20999 errors we saw earlier when instantiating D from TMPL and ARGS. */
21001 static void
21002 recheck_decl_substitution (tree d, tree tmpl, tree args)
21004 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
21005 tree type = TREE_TYPE (pattern);
21006 location_t loc = input_location;
21008 push_access_scope (d);
21009 push_deferring_access_checks (dk_no_deferred);
21010 input_location = DECL_SOURCE_LOCATION (pattern);
21011 tsubst (type, args, tf_warning_or_error, d);
21012 input_location = loc;
21013 pop_deferring_access_checks ();
21014 pop_access_scope (d);
21017 /* Instantiate the indicated variable, function, or alias template TMPL with
21018 the template arguments in TARG_PTR. */
21020 static tree
21021 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
21023 tree targ_ptr = orig_args;
21024 tree fndecl;
21025 tree gen_tmpl;
21026 tree spec;
21027 bool access_ok = true;
21029 if (tmpl == error_mark_node)
21030 return error_mark_node;
21032 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
21034 if (modules_p ())
21035 lazy_load_pendings (tmpl);
21037 /* If this function is a clone, handle it specially. */
21038 if (DECL_CLONED_FUNCTION_P (tmpl))
21040 tree spec;
21041 tree clone;
21043 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
21044 DECL_CLONED_FUNCTION. */
21045 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
21046 targ_ptr, complain);
21047 if (spec == error_mark_node)
21048 return error_mark_node;
21050 /* Look for the clone. */
21051 FOR_EACH_CLONE (clone, spec)
21052 if (DECL_NAME (clone) == DECL_NAME (tmpl))
21053 return clone;
21054 /* We should always have found the clone by now. */
21055 gcc_unreachable ();
21056 return NULL_TREE;
21059 if (targ_ptr == error_mark_node)
21060 return error_mark_node;
21062 /* Check to see if we already have this specialization. */
21063 gen_tmpl = most_general_template (tmpl);
21064 if (TMPL_ARGS_DEPTH (targ_ptr)
21065 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
21066 /* targ_ptr only has the innermost template args, so add the outer ones
21067 from tmpl, which could be either a partial instantiation or gen_tmpl (in
21068 the case of a non-dependent call within a template definition). */
21069 targ_ptr = (add_outermost_template_args
21070 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
21071 targ_ptr));
21073 /* It would be nice to avoid hashing here and then again in tsubst_decl,
21074 but it doesn't seem to be on the hot path. */
21075 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
21077 gcc_checking_assert (tmpl == gen_tmpl
21078 || ((fndecl
21079 = retrieve_specialization (tmpl, orig_args, 0))
21080 == spec)
21081 || fndecl == NULL_TREE);
21083 if (spec != NULL_TREE)
21085 if (FNDECL_HAS_ACCESS_ERRORS (spec))
21087 if (complain & tf_error)
21088 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
21089 return error_mark_node;
21091 return spec;
21094 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
21095 complain))
21096 return error_mark_node;
21098 /* We are building a FUNCTION_DECL, during which the access of its
21099 parameters and return types have to be checked. However this
21100 FUNCTION_DECL which is the desired context for access checking
21101 is not built yet. We solve this chicken-and-egg problem by
21102 deferring all checks until we have the FUNCTION_DECL. */
21103 push_deferring_access_checks (dk_deferred);
21105 /* Instantiation of the function happens in the context of the function
21106 template, not the context of the overload resolution we're doing. */
21107 push_to_top_level ();
21108 /* If there are dependent arguments, e.g. because we're doing partial
21109 ordering, make sure processing_template_decl stays set. */
21110 if (uses_template_parms (targ_ptr))
21111 ++processing_template_decl;
21112 if (DECL_CLASS_SCOPE_P (gen_tmpl))
21114 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
21115 complain, gen_tmpl, true);
21116 push_nested_class (ctx);
21119 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
21121 fndecl = NULL_TREE;
21122 if (VAR_P (pattern))
21124 /* We need to determine if we're using a partial or explicit
21125 specialization now, because the type of the variable could be
21126 different. */
21127 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
21128 tree elt = most_specialized_partial_spec (tid, complain);
21129 if (elt == error_mark_node)
21130 pattern = error_mark_node;
21131 else if (elt)
21133 tree partial_tmpl = TREE_VALUE (elt);
21134 tree partial_args = TREE_PURPOSE (elt);
21135 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
21136 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
21140 /* Substitute template parameters to obtain the specialization. */
21141 if (fndecl == NULL_TREE)
21142 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
21143 if (DECL_CLASS_SCOPE_P (gen_tmpl))
21144 pop_nested_class ();
21145 pop_from_top_level ();
21147 if (fndecl == error_mark_node)
21149 pop_deferring_access_checks ();
21150 return error_mark_node;
21153 /* The DECL_TI_TEMPLATE should always be the immediate parent
21154 template, not the most general template. */
21155 DECL_TI_TEMPLATE (fndecl) = tmpl;
21156 DECL_TI_ARGS (fndecl) = targ_ptr;
21158 set_instantiating_module (fndecl);
21160 /* Now we know the specialization, compute access previously
21161 deferred. Do no access control for inheriting constructors,
21162 as we already checked access for the inherited constructor. */
21163 if (!(flag_new_inheriting_ctors
21164 && DECL_INHERITED_CTOR (fndecl)))
21166 push_access_scope (fndecl);
21167 if (!perform_deferred_access_checks (complain))
21168 access_ok = false;
21169 pop_access_scope (fndecl);
21171 pop_deferring_access_checks ();
21173 /* If we've just instantiated the main entry point for a function,
21174 instantiate all the alternate entry points as well. We do this
21175 by cloning the instantiation of the main entry point, not by
21176 instantiating the template clones. */
21177 if (tree chain = DECL_CHAIN (gen_tmpl))
21178 if (DECL_P (chain) && DECL_CLONED_FUNCTION_P (chain))
21179 clone_cdtor (fndecl, /*update_methods=*/false);
21181 if (!access_ok)
21183 if (!(complain & tf_error))
21185 /* Remember to reinstantiate when we're out of SFINAE so the user
21186 can see the errors. */
21187 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
21189 return error_mark_node;
21191 return fndecl;
21194 /* Wrapper for instantiate_template_1. */
21196 tree
21197 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
21199 tree ret;
21200 timevar_push (TV_TEMPLATE_INST);
21201 ret = instantiate_template_1 (tmpl, orig_args, complain);
21202 timevar_pop (TV_TEMPLATE_INST);
21203 return ret;
21206 /* Instantiate the alias template TMPL with ARGS. Also push a template
21207 instantiation level, which instantiate_template doesn't do because
21208 functions and variables have sufficient context established by the
21209 callers. */
21211 static tree
21212 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
21214 if (tmpl == error_mark_node || args == error_mark_node)
21215 return error_mark_node;
21217 args =
21218 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
21219 args, tmpl, complain,
21220 /*require_all_args=*/true,
21221 /*use_default_args=*/true);
21223 /* FIXME check for satisfaction in check_instantiated_args. */
21224 if (flag_concepts
21225 && !any_dependent_template_arguments_p (args)
21226 && !constraints_satisfied_p (tmpl, args))
21228 if (complain & tf_error)
21230 auto_diagnostic_group d;
21231 error ("template constraint failure for %qD", tmpl);
21232 diagnose_constraints (input_location, tmpl, args);
21234 return error_mark_node;
21237 if (!push_tinst_level (tmpl, args))
21238 return error_mark_node;
21239 tree r = instantiate_template (tmpl, args, complain);
21240 pop_tinst_level ();
21242 if (tree d = dependent_alias_template_spec_p (TREE_TYPE (r), nt_opaque))
21244 /* An alias template specialization can be dependent
21245 even if its underlying type is not. */
21246 TYPE_DEPENDENT_P (d) = true;
21247 TYPE_DEPENDENT_P_VALID (d) = true;
21248 /* Sometimes a dependent alias spec is equivalent to its expansion,
21249 sometimes not. So always use structural_comptypes. */
21250 SET_TYPE_STRUCTURAL_EQUALITY (d);
21253 return r;
21256 /* PARM is a template parameter pack for FN. Returns true iff
21257 PARM is used in a deducible way in the argument list of FN. */
21259 static bool
21260 pack_deducible_p (tree parm, tree fn)
21262 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
21263 for (; t; t = TREE_CHAIN (t))
21265 tree type = TREE_VALUE (t);
21266 tree packs;
21267 if (!PACK_EXPANSION_P (type))
21268 continue;
21269 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
21270 packs; packs = TREE_CHAIN (packs))
21271 if (template_args_equal (TREE_VALUE (packs), parm))
21273 /* The template parameter pack is used in a function parameter
21274 pack. If this is the end of the parameter list, the
21275 template parameter pack is deducible. */
21276 if (TREE_CHAIN (t) == void_list_node)
21277 return true;
21278 else
21279 /* Otherwise, not. Well, it could be deduced from
21280 a non-pack parameter, but doing so would end up with
21281 a deduction mismatch, so don't bother. */
21282 return false;
21285 /* The template parameter pack isn't used in any function parameter
21286 packs, but it might be used deeper, e.g. tuple<Args...>. */
21287 return true;
21290 /* Subroutine of fn_type_unification: check non-dependent parms for
21291 convertibility. */
21293 static int
21294 check_non_deducible_conversions (tree parms, const tree *args, unsigned nargs,
21295 tree fn, unification_kind_t strict, int flags,
21296 struct conversion **convs, bool explain_p)
21298 /* Non-constructor methods need to leave a conversion for 'this', which
21299 isn't included in nargs here. */
21300 unsigned offset = (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
21301 && !DECL_CONSTRUCTOR_P (fn));
21303 for (unsigned ia = 0;
21304 parms && parms != void_list_node && ia < nargs; )
21306 tree parm = TREE_VALUE (parms);
21308 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
21309 && (!TREE_CHAIN (parms)
21310 || TREE_CHAIN (parms) == void_list_node))
21311 /* For a function parameter pack that occurs at the end of the
21312 parameter-declaration-list, the type A of each remaining
21313 argument of the call is compared with the type P of the
21314 declarator-id of the function parameter pack. */
21315 break;
21317 parms = TREE_CHAIN (parms);
21319 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
21320 /* For a function parameter pack that does not occur at the
21321 end of the parameter-declaration-list, the type of the
21322 parameter pack is a non-deduced context. */
21323 continue;
21325 if (!uses_template_parms (parm))
21327 tree arg = args[ia];
21328 conversion **conv_p = convs ? &convs[ia+offset] : NULL;
21329 int lflags = conv_flags (ia, nargs, fn, arg, flags);
21331 if (check_non_deducible_conversion (parm, arg, strict, lflags,
21332 conv_p, explain_p))
21333 return 1;
21336 ++ia;
21339 return 0;
21342 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
21343 NARGS elements of the arguments that are being used when calling
21344 it. TARGS is a vector into which the deduced template arguments
21345 are placed.
21347 Returns either a FUNCTION_DECL for the matching specialization of FN or
21348 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
21349 true, diagnostics will be printed to explain why it failed.
21351 If FN is a conversion operator, or we are trying to produce a specific
21352 specialization, RETURN_TYPE is the return type desired.
21354 The EXPLICIT_TARGS are explicit template arguments provided via a
21355 template-id.
21357 The parameter STRICT is one of:
21359 DEDUCE_CALL:
21360 We are deducing arguments for a function call, as in
21361 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
21362 deducing arguments for a call to the result of a conversion
21363 function template, as in [over.call.object].
21365 DEDUCE_CONV:
21366 We are deducing arguments for a conversion function, as in
21367 [temp.deduct.conv].
21369 DEDUCE_EXACT:
21370 We are deducing arguments when doing an explicit instantiation
21371 as in [temp.explicit], when determining an explicit specialization
21372 as in [temp.expl.spec], or when taking the address of a function
21373 template, as in [temp.deduct.funcaddr]. */
21375 tree
21376 fn_type_unification (tree fn,
21377 tree explicit_targs,
21378 tree targs,
21379 const tree *args,
21380 unsigned int nargs,
21381 tree return_type,
21382 unification_kind_t strict,
21383 int flags,
21384 struct conversion **convs,
21385 bool explain_p,
21386 bool decltype_p)
21388 tree parms;
21389 tree fntype;
21390 tree decl = NULL_TREE;
21391 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
21392 bool ok;
21393 static int deduction_depth;
21394 /* type_unification_real will pass back any access checks from default
21395 template argument substitution. */
21396 vec<deferred_access_check, va_gc> *checks = NULL;
21397 /* We don't have all the template args yet. */
21398 bool incomplete = true;
21400 tree orig_fn = fn;
21401 if (flag_new_inheriting_ctors)
21402 fn = strip_inheriting_ctors (fn);
21404 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
21405 tree r = error_mark_node;
21407 tree full_targs = targs;
21408 if (TMPL_ARGS_DEPTH (targs)
21409 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
21410 full_targs = (add_outermost_template_args
21411 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
21412 targs));
21414 if (decltype_p)
21415 complain |= tf_decltype;
21417 /* In C++0x, it's possible to have a function template whose type depends
21418 on itself recursively. This is most obvious with decltype, but can also
21419 occur with enumeration scope (c++/48969). So we need to catch infinite
21420 recursion and reject the substitution at deduction time; this function
21421 will return error_mark_node for any repeated substitution.
21423 This also catches excessive recursion such as when f<N> depends on
21424 f<N-1> across all integers, and returns error_mark_node for all the
21425 substitutions back up to the initial one.
21427 This is, of course, not reentrant. */
21428 if (excessive_deduction_depth)
21429 return error_mark_node;
21430 ++deduction_depth;
21432 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
21434 fntype = TREE_TYPE (fn);
21435 if (explicit_targs)
21437 /* [temp.deduct]
21439 The specified template arguments must match the template
21440 parameters in kind (i.e., type, nontype, template), and there
21441 must not be more arguments than there are parameters;
21442 otherwise type deduction fails.
21444 Nontype arguments must match the types of the corresponding
21445 nontype template parameters, or must be convertible to the
21446 types of the corresponding nontype parameters as specified in
21447 _temp.arg.nontype_, otherwise type deduction fails.
21449 All references in the function type of the function template
21450 to the corresponding template parameters are replaced by the
21451 specified template argument values. If a substitution in a
21452 template parameter or in the function type of the function
21453 template results in an invalid type, type deduction fails. */
21454 int i, len = TREE_VEC_LENGTH (tparms);
21455 location_t loc = input_location;
21456 incomplete = false;
21458 if (explicit_targs == error_mark_node)
21459 goto fail;
21461 if (TMPL_ARGS_DEPTH (explicit_targs)
21462 < TMPL_ARGS_DEPTH (full_targs))
21463 explicit_targs = add_outermost_template_args (full_targs,
21464 explicit_targs);
21466 /* Adjust any explicit template arguments before entering the
21467 substitution context. */
21468 explicit_targs
21469 = (coerce_template_parms (tparms, explicit_targs, fn,
21470 complain|tf_partial,
21471 /*require_all_args=*/false,
21472 /*use_default_args=*/false));
21473 if (explicit_targs == error_mark_node)
21474 goto fail;
21476 /* Substitute the explicit args into the function type. This is
21477 necessary so that, for instance, explicitly declared function
21478 arguments can match null pointed constants. If we were given
21479 an incomplete set of explicit args, we must not do semantic
21480 processing during substitution as we could create partial
21481 instantiations. */
21482 for (i = 0; i < len; i++)
21484 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
21485 bool parameter_pack = false;
21486 tree targ = TREE_VEC_ELT (explicit_targs, i);
21488 /* Dig out the actual parm. */
21489 if (TREE_CODE (parm) == TYPE_DECL
21490 || TREE_CODE (parm) == TEMPLATE_DECL)
21492 parm = TREE_TYPE (parm);
21493 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
21495 else if (TREE_CODE (parm) == PARM_DECL)
21497 parm = DECL_INITIAL (parm);
21498 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
21501 if (targ == NULL_TREE)
21502 /* No explicit argument for this template parameter. */
21503 incomplete = true;
21504 else if (parameter_pack && pack_deducible_p (parm, fn))
21506 /* Mark the argument pack as "incomplete". We could
21507 still deduce more arguments during unification.
21508 We remove this mark in type_unification_real. */
21509 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
21510 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
21511 = ARGUMENT_PACK_ARGS (targ);
21513 /* We have some incomplete argument packs. */
21514 incomplete = true;
21518 if (incomplete)
21520 if (!push_tinst_level (fn, explicit_targs))
21522 excessive_deduction_depth = true;
21523 goto fail;
21525 ++processing_template_decl;
21526 input_location = DECL_SOURCE_LOCATION (fn);
21527 /* Ignore any access checks; we'll see them again in
21528 instantiate_template and they might have the wrong
21529 access path at this point. */
21530 push_deferring_access_checks (dk_deferred);
21531 tsubst_flags_t ecomplain = complain | tf_partial | tf_fndecl_type;
21532 fntype = tsubst (TREE_TYPE (fn), explicit_targs, ecomplain, NULL_TREE);
21533 pop_deferring_access_checks ();
21534 input_location = loc;
21535 --processing_template_decl;
21536 pop_tinst_level ();
21538 if (fntype == error_mark_node)
21539 goto fail;
21542 /* Place the explicitly specified arguments in TARGS. */
21543 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
21544 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
21545 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
21546 if (!incomplete && CHECKING_P
21547 && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
21548 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT
21549 (targs, NUM_TMPL_ARGS (explicit_targs));
21552 if (return_type && strict != DEDUCE_CALL)
21554 tree *new_args = XALLOCAVEC (tree, nargs + 1);
21555 new_args[0] = return_type;
21556 memcpy (new_args + 1, args, nargs * sizeof (tree));
21557 args = new_args;
21558 ++nargs;
21561 if (!incomplete)
21562 goto deduced;
21564 /* Never do unification on the 'this' parameter. */
21565 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
21567 if (return_type && strict == DEDUCE_CALL)
21569 /* We're deducing for a call to the result of a template conversion
21570 function. The parms we really want are in return_type. */
21571 if (INDIRECT_TYPE_P (return_type))
21572 return_type = TREE_TYPE (return_type);
21573 parms = TYPE_ARG_TYPES (return_type);
21575 else if (return_type)
21577 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
21580 /* We allow incomplete unification without an error message here
21581 because the standard doesn't seem to explicitly prohibit it. Our
21582 callers must be ready to deal with unification failures in any
21583 event. */
21585 /* If we aren't explaining yet, push tinst context so we can see where
21586 any errors (e.g. from class instantiations triggered by instantiation
21587 of default template arguments) come from. If we are explaining, this
21588 context is redundant. */
21589 if (!explain_p && !push_tinst_level (fn, targs))
21591 excessive_deduction_depth = true;
21592 goto fail;
21595 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
21596 full_targs, parms, args, nargs, /*subr=*/0,
21597 strict, &checks, explain_p);
21598 if (!explain_p)
21599 pop_tinst_level ();
21600 if (!ok)
21601 goto fail;
21603 /* Now that we have bindings for all of the template arguments,
21604 ensure that the arguments deduced for the template template
21605 parameters have compatible template parameter lists. We cannot
21606 check this property before we have deduced all template
21607 arguments, because the template parameter types of a template
21608 template parameter might depend on prior template parameters
21609 deduced after the template template parameter. The following
21610 ill-formed example illustrates this issue:
21612 template<typename T, template<T> class C> void f(C<5>, T);
21614 template<int N> struct X {};
21616 void g() {
21617 f(X<5>(), 5l); // error: template argument deduction fails
21620 The template parameter list of 'C' depends on the template type
21621 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
21622 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
21623 time that we deduce 'C'. */
21624 if (!template_template_parm_bindings_ok_p
21625 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
21627 unify_inconsistent_template_template_parameters (explain_p);
21628 goto fail;
21631 deduced:
21633 /* CWG2369: Check satisfaction before non-deducible conversions. */
21634 if (!constraints_satisfied_p (fn, targs))
21636 if (explain_p)
21637 diagnose_constraints (DECL_SOURCE_LOCATION (fn), fn, targs);
21638 goto fail;
21641 /* DR 1391: All parameters have args, now check non-dependent parms for
21642 convertibility. We don't do this if all args were explicitly specified,
21643 as the standard says that we substitute explicit args immediately. */
21644 if (incomplete
21645 && check_non_deducible_conversions (parms, args, nargs, fn, strict, flags,
21646 convs, explain_p))
21647 goto fail;
21649 /* All is well so far. Now, check:
21651 [temp.deduct]
21653 When all template arguments have been deduced, all uses of
21654 template parameters in nondeduced contexts are replaced with
21655 the corresponding deduced argument values. If the
21656 substitution results in an invalid type, as described above,
21657 type deduction fails. */
21658 if (!push_tinst_level (fn, targs))
21660 excessive_deduction_depth = true;
21661 goto fail;
21664 /* Also collect access checks from the instantiation. */
21665 reopen_deferring_access_checks (checks);
21667 decl = instantiate_template (fn, targs, complain);
21669 checks = get_deferred_access_checks ();
21670 pop_deferring_access_checks ();
21672 pop_tinst_level ();
21674 if (decl == error_mark_node)
21675 goto fail;
21677 /* Now perform any access checks encountered during substitution. */
21678 push_access_scope (decl);
21679 ok = perform_access_checks (checks, complain);
21680 pop_access_scope (decl);
21681 if (!ok)
21682 goto fail;
21684 /* If we're looking for an exact match, check that what we got
21685 is indeed an exact match. It might not be if some template
21686 parameters are used in non-deduced contexts. But don't check
21687 for an exact match if we have dependent template arguments;
21688 in that case we're doing partial ordering, and we already know
21689 that we have two candidates that will provide the actual type. */
21690 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
21692 tree substed = TREE_TYPE (decl);
21693 unsigned int i;
21695 tree sarg
21696 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
21697 if (return_type)
21698 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
21699 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
21700 if (!same_type_p (args[i], TREE_VALUE (sarg)))
21702 unify_type_mismatch (explain_p, args[i],
21703 TREE_VALUE (sarg));
21704 goto fail;
21708 /* After doing deduction with the inherited constructor, actually return an
21709 instantiation of the inheriting constructor. */
21710 if (orig_fn != fn)
21711 decl = instantiate_template (orig_fn, targs, complain);
21713 r = decl;
21715 fail:
21716 --deduction_depth;
21717 if (excessive_deduction_depth)
21719 if (deduction_depth == 0)
21720 /* Reset once we're all the way out. */
21721 excessive_deduction_depth = false;
21724 return r;
21727 /* Adjust types before performing type deduction, as described in
21728 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
21729 sections are symmetric. PARM is the type of a function parameter
21730 or the return type of the conversion function. ARG is the type of
21731 the argument passed to the call, or the type of the value
21732 initialized with the result of the conversion function.
21733 ARG_EXPR is the original argument expression, which may be null. */
21735 static int
21736 maybe_adjust_types_for_deduction (unification_kind_t strict,
21737 tree* parm,
21738 tree* arg,
21739 tree arg_expr)
21741 int result = 0;
21743 switch (strict)
21745 case DEDUCE_CALL:
21746 break;
21748 case DEDUCE_CONV:
21749 /* Swap PARM and ARG throughout the remainder of this
21750 function; the handling is precisely symmetric since PARM
21751 will initialize ARG rather than vice versa. */
21752 std::swap (parm, arg);
21753 break;
21755 case DEDUCE_EXACT:
21756 /* Core issue #873: Do the DR606 thing (see below) for these cases,
21757 too, but here handle it by stripping the reference from PARM
21758 rather than by adding it to ARG. */
21759 if (TYPE_REF_P (*parm)
21760 && TYPE_REF_IS_RVALUE (*parm)
21761 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
21762 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
21763 && TYPE_REF_P (*arg)
21764 && !TYPE_REF_IS_RVALUE (*arg))
21765 *parm = TREE_TYPE (*parm);
21766 /* Nothing else to do in this case. */
21767 return 0;
21769 default:
21770 gcc_unreachable ();
21773 if (!TYPE_REF_P (*parm))
21775 /* [temp.deduct.call]
21777 If P is not a reference type:
21779 --If A is an array type, the pointer type produced by the
21780 array-to-pointer standard conversion (_conv.array_) is
21781 used in place of A for type deduction; otherwise,
21783 --If A is a function type, the pointer type produced by
21784 the function-to-pointer standard conversion
21785 (_conv.func_) is used in place of A for type deduction;
21786 otherwise,
21788 --If A is a cv-qualified type, the top level
21789 cv-qualifiers of A's type are ignored for type
21790 deduction. */
21791 if (TREE_CODE (*arg) == ARRAY_TYPE)
21792 *arg = build_pointer_type (TREE_TYPE (*arg));
21793 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
21794 *arg = build_pointer_type (*arg);
21795 else
21796 *arg = TYPE_MAIN_VARIANT (*arg);
21799 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
21800 reference to a cv-unqualified template parameter that does not represent a
21801 template parameter of a class template (during class template argument
21802 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
21803 an lvalue, the type "lvalue reference to A" is used in place of A for type
21804 deduction. */
21805 if (TYPE_REF_P (*parm)
21806 && TYPE_REF_IS_RVALUE (*parm)
21807 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
21808 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
21809 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
21810 && (arg_expr ? lvalue_p (arg_expr)
21811 /* try_one_overload doesn't provide an arg_expr, but
21812 functions are always lvalues. */
21813 : TREE_CODE (*arg) == FUNCTION_TYPE))
21814 *arg = build_reference_type (*arg);
21816 /* [temp.deduct.call]
21818 If P is a cv-qualified type, the top level cv-qualifiers
21819 of P's type are ignored for type deduction. If P is a
21820 reference type, the type referred to by P is used for
21821 type deduction. */
21822 *parm = TYPE_MAIN_VARIANT (*parm);
21823 if (TYPE_REF_P (*parm))
21825 *parm = TREE_TYPE (*parm);
21826 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
21829 /* DR 322. For conversion deduction, remove a reference type on parm
21830 too (which has been swapped into ARG). */
21831 if (strict == DEDUCE_CONV && TYPE_REF_P (*arg))
21832 *arg = TREE_TYPE (*arg);
21834 return result;
21837 /* Subroutine of fn_type_unification. PARM is a function parameter of a
21838 template which doesn't contain any deducible template parameters; check if
21839 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
21840 unify_one_argument. */
21842 static int
21843 check_non_deducible_conversion (tree parm, tree arg, int strict,
21844 int flags, struct conversion **conv_p,
21845 bool explain_p)
21847 tree type;
21849 if (!TYPE_P (arg))
21850 type = TREE_TYPE (arg);
21851 else
21852 type = arg;
21854 if (same_type_p (parm, type))
21855 return unify_success (explain_p);
21857 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
21858 if (strict == DEDUCE_CONV)
21860 if (can_convert_arg (type, parm, NULL_TREE, flags, complain))
21861 return unify_success (explain_p);
21863 else if (strict != DEDUCE_EXACT)
21865 bool ok = false;
21866 tree conv_arg = TYPE_P (arg) ? NULL_TREE : arg;
21867 if (conv_p)
21868 /* Avoid recalculating this in add_function_candidate. */
21869 ok = (*conv_p
21870 = good_conversion (parm, type, conv_arg, flags, complain));
21871 else
21872 ok = can_convert_arg (parm, type, conv_arg, flags, complain);
21873 if (ok)
21874 return unify_success (explain_p);
21877 if (strict == DEDUCE_EXACT)
21878 return unify_type_mismatch (explain_p, parm, arg);
21879 else
21880 return unify_arg_conversion (explain_p, parm, type, arg);
21883 static bool uses_deducible_template_parms (tree type);
21885 /* Returns true iff the expression EXPR is one from which a template
21886 argument can be deduced. In other words, if it's an undecorated
21887 use of a template non-type parameter. */
21889 static bool
21890 deducible_expression (tree expr)
21892 /* Strip implicit conversions and implicit INDIRECT_REFs. */
21893 while (CONVERT_EXPR_P (expr)
21894 || TREE_CODE (expr) == VIEW_CONVERT_EXPR
21895 || REFERENCE_REF_P (expr))
21896 expr = TREE_OPERAND (expr, 0);
21897 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
21900 /* Returns true iff the array domain DOMAIN uses a template parameter in a
21901 deducible way; that is, if it has a max value of <PARM> - 1. */
21903 static bool
21904 deducible_array_bound (tree domain)
21906 if (domain == NULL_TREE)
21907 return false;
21909 tree max = TYPE_MAX_VALUE (domain);
21910 if (TREE_CODE (max) != MINUS_EXPR)
21911 return false;
21913 return deducible_expression (TREE_OPERAND (max, 0));
21916 /* Returns true iff the template arguments ARGS use a template parameter
21917 in a deducible way. */
21919 static bool
21920 deducible_template_args (tree args)
21922 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
21924 bool deducible;
21925 tree elt = TREE_VEC_ELT (args, i);
21926 if (ARGUMENT_PACK_P (elt))
21927 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
21928 else
21930 if (PACK_EXPANSION_P (elt))
21931 elt = PACK_EXPANSION_PATTERN (elt);
21932 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
21933 deducible = true;
21934 else if (TYPE_P (elt))
21935 deducible = uses_deducible_template_parms (elt);
21936 else
21937 deducible = deducible_expression (elt);
21939 if (deducible)
21940 return true;
21942 return false;
21945 /* Returns true iff TYPE contains any deducible references to template
21946 parameters, as per 14.8.2.5. */
21948 static bool
21949 uses_deducible_template_parms (tree type)
21951 if (PACK_EXPANSION_P (type))
21952 type = PACK_EXPANSION_PATTERN (type);
21954 /* T
21955 cv-list T
21956 TT<T>
21957 TT<i>
21958 TT<> */
21959 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
21960 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
21961 return true;
21963 /* T*
21965 T&& */
21966 if (INDIRECT_TYPE_P (type))
21967 return uses_deducible_template_parms (TREE_TYPE (type));
21969 /* T[integer-constant ]
21970 type [i] */
21971 if (TREE_CODE (type) == ARRAY_TYPE)
21972 return (uses_deducible_template_parms (TREE_TYPE (type))
21973 || deducible_array_bound (TYPE_DOMAIN (type)));
21975 /* T type ::*
21976 type T::*
21977 T T::*
21978 T (type ::*)()
21979 type (T::*)()
21980 type (type ::*)(T)
21981 type (T::*)(T)
21982 T (type ::*)(T)
21983 T (T::*)()
21984 T (T::*)(T) */
21985 if (TYPE_PTRMEM_P (type))
21986 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
21987 || (uses_deducible_template_parms
21988 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
21990 /* template-name <T> (where template-name refers to a class template)
21991 template-name <i> (where template-name refers to a class template) */
21992 if (CLASS_TYPE_P (type)
21993 && CLASSTYPE_TEMPLATE_INFO (type)
21994 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
21995 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
21996 (CLASSTYPE_TI_ARGS (type)));
21998 /* type (T)
22000 T(T) */
22001 if (FUNC_OR_METHOD_TYPE_P (type))
22003 if (uses_deducible_template_parms (TREE_TYPE (type)))
22004 return true;
22005 tree parm = TYPE_ARG_TYPES (type);
22006 if (TREE_CODE (type) == METHOD_TYPE)
22007 parm = TREE_CHAIN (parm);
22008 for (; parm; parm = TREE_CHAIN (parm))
22009 if (uses_deducible_template_parms (TREE_VALUE (parm)))
22010 return true;
22013 return false;
22016 /* Subroutine of type_unification_real and unify_pack_expansion to
22017 handle unification of a single P/A pair. Parameters are as
22018 for those functions. */
22020 static int
22021 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
22022 int subr, unification_kind_t strict,
22023 bool explain_p)
22025 tree arg_expr = NULL_TREE;
22026 int arg_strict;
22028 if (arg == error_mark_node || parm == error_mark_node)
22029 return unify_invalid (explain_p);
22030 if (arg == unknown_type_node)
22031 /* We can't deduce anything from this, but we might get all the
22032 template args from other function args. */
22033 return unify_success (explain_p);
22035 /* Implicit conversions (Clause 4) will be performed on a function
22036 argument to convert it to the type of the corresponding function
22037 parameter if the parameter type contains no template-parameters that
22038 participate in template argument deduction. */
22039 if (strict != DEDUCE_EXACT
22040 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
22041 /* For function parameters with no deducible template parameters,
22042 just return. We'll check non-dependent conversions later. */
22043 return unify_success (explain_p);
22045 switch (strict)
22047 case DEDUCE_CALL:
22048 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
22049 | UNIFY_ALLOW_MORE_CV_QUAL
22050 | UNIFY_ALLOW_DERIVED);
22051 break;
22053 case DEDUCE_CONV:
22054 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
22055 break;
22057 case DEDUCE_EXACT:
22058 arg_strict = UNIFY_ALLOW_NONE;
22059 break;
22061 default:
22062 gcc_unreachable ();
22065 /* We only do these transformations if this is the top-level
22066 parameter_type_list in a call or declaration matching; in other
22067 situations (nested function declarators, template argument lists) we
22068 won't be comparing a type to an expression, and we don't do any type
22069 adjustments. */
22070 if (!subr)
22072 if (!TYPE_P (arg))
22074 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
22075 if (type_unknown_p (arg))
22077 /* [temp.deduct.type] A template-argument can be
22078 deduced from a pointer to function or pointer
22079 to member function argument if the set of
22080 overloaded functions does not contain function
22081 templates and at most one of a set of
22082 overloaded functions provides a unique
22083 match. */
22084 resolve_overloaded_unification (tparms, targs, parm,
22085 arg, strict,
22086 arg_strict, explain_p);
22087 /* If a unique match was not found, this is a
22088 non-deduced context, so we still succeed. */
22089 return unify_success (explain_p);
22092 arg_expr = arg;
22093 arg = unlowered_expr_type (arg);
22094 if (arg == error_mark_node)
22095 return unify_invalid (explain_p);
22098 arg_strict |=
22099 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
22101 else
22102 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
22103 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
22104 return unify_template_argument_mismatch (explain_p, parm, arg);
22106 /* For deduction from an init-list we need the actual list. */
22107 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
22108 arg = arg_expr;
22109 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
22112 /* for_each_template_parm callback that always returns 0. */
22114 static int
22115 zero_r (tree, void *)
22117 return 0;
22120 /* for_each_template_parm any_fn callback to handle deduction of a template
22121 type argument from the type of an array bound. */
22123 static int
22124 array_deduction_r (tree t, void *data)
22126 tree_pair_p d = (tree_pair_p)data;
22127 tree &tparms = d->purpose;
22128 tree &targs = d->value;
22130 if (TREE_CODE (t) == ARRAY_TYPE)
22131 if (tree dom = TYPE_DOMAIN (t))
22132 if (tree max = TYPE_MAX_VALUE (dom))
22134 if (TREE_CODE (max) == MINUS_EXPR)
22135 max = TREE_OPERAND (max, 0);
22136 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
22137 unify (tparms, targs, TREE_TYPE (max), size_type_node,
22138 UNIFY_ALLOW_NONE, /*explain*/false);
22141 /* Keep walking. */
22142 return 0;
22145 /* Try to deduce any not-yet-deduced template type arguments from the type of
22146 an array bound. This is handled separately from unify because 14.8.2.5 says
22147 "The type of a type parameter is only deduced from an array bound if it is
22148 not otherwise deduced." */
22150 static void
22151 try_array_deduction (tree tparms, tree targs, tree parm)
22153 tree_pair_s data = { tparms, targs };
22154 hash_set<tree> visited;
22155 for_each_template_parm (parm, zero_r, &data, &visited,
22156 /*nondeduced*/false, array_deduction_r);
22159 /* Most parms like fn_type_unification.
22161 If SUBR is 1, we're being called recursively (to unify the
22162 arguments of a function or method parameter of a function
22163 template).
22165 CHECKS is a pointer to a vector of access checks encountered while
22166 substituting default template arguments. */
22168 static int
22169 type_unification_real (tree tparms,
22170 tree full_targs,
22171 tree xparms,
22172 const tree *xargs,
22173 unsigned int xnargs,
22174 int subr,
22175 unification_kind_t strict,
22176 vec<deferred_access_check, va_gc> **checks,
22177 bool explain_p)
22179 tree parm, arg;
22180 int i;
22181 int ntparms = TREE_VEC_LENGTH (tparms);
22182 int saw_undeduced = 0;
22183 tree parms;
22184 const tree *args;
22185 unsigned int nargs;
22186 unsigned int ia;
22188 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
22189 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
22190 gcc_assert (ntparms > 0);
22192 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
22194 /* Reset the number of non-defaulted template arguments contained
22195 in TARGS. */
22196 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
22198 again:
22199 parms = xparms;
22200 args = xargs;
22201 nargs = xnargs;
22203 ia = 0;
22204 while (parms && parms != void_list_node
22205 && ia < nargs)
22207 parm = TREE_VALUE (parms);
22209 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
22210 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
22211 /* For a function parameter pack that occurs at the end of the
22212 parameter-declaration-list, the type A of each remaining
22213 argument of the call is compared with the type P of the
22214 declarator-id of the function parameter pack. */
22215 break;
22217 parms = TREE_CHAIN (parms);
22219 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
22220 /* For a function parameter pack that does not occur at the
22221 end of the parameter-declaration-list, the type of the
22222 parameter pack is a non-deduced context. */
22223 continue;
22225 arg = args[ia];
22226 ++ia;
22228 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
22229 explain_p))
22230 return 1;
22233 if (parms
22234 && parms != void_list_node
22235 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
22237 /* Unify the remaining arguments with the pack expansion type. */
22238 tree argvec;
22239 tree parmvec = make_tree_vec (1);
22241 /* Allocate a TREE_VEC and copy in all of the arguments */
22242 argvec = make_tree_vec (nargs - ia);
22243 for (i = 0; ia < nargs; ++ia, ++i)
22244 TREE_VEC_ELT (argvec, i) = args[ia];
22246 /* Copy the parameter into parmvec. */
22247 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
22248 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
22249 /*subr=*/subr, explain_p))
22250 return 1;
22252 /* Advance to the end of the list of parameters. */
22253 parms = TREE_CHAIN (parms);
22256 /* Fail if we've reached the end of the parm list, and more args
22257 are present, and the parm list isn't variadic. */
22258 if (ia < nargs && parms == void_list_node)
22259 return unify_too_many_arguments (explain_p, nargs, ia);
22260 /* Fail if parms are left and they don't have default values and
22261 they aren't all deduced as empty packs (c++/57397). This is
22262 consistent with sufficient_parms_p. */
22263 if (parms && parms != void_list_node
22264 && TREE_PURPOSE (parms) == NULL_TREE)
22266 unsigned int count = nargs;
22267 tree p = parms;
22268 bool type_pack_p;
22271 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
22272 if (!type_pack_p)
22273 count++;
22274 p = TREE_CHAIN (p);
22276 while (p && p != void_list_node);
22277 if (count != nargs)
22278 return unify_too_few_arguments (explain_p, ia, count,
22279 type_pack_p);
22282 if (!subr)
22284 tsubst_flags_t complain = (explain_p
22285 ? tf_warning_or_error
22286 : tf_none);
22287 bool tried_array_deduction = (cxx_dialect < cxx17);
22289 for (i = 0; i < ntparms; i++)
22291 tree targ = TREE_VEC_ELT (targs, i);
22292 tree tparm = TREE_VEC_ELT (tparms, i);
22294 /* Clear the "incomplete" flags on all argument packs now so that
22295 substituting them into later default arguments works. */
22296 if (targ && ARGUMENT_PACK_P (targ))
22298 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
22299 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
22302 if (targ || tparm == error_mark_node)
22303 continue;
22304 tparm = TREE_VALUE (tparm);
22306 if (TREE_CODE (tparm) == TYPE_DECL
22307 && !tried_array_deduction)
22309 try_array_deduction (tparms, targs, xparms);
22310 tried_array_deduction = true;
22311 if (TREE_VEC_ELT (targs, i))
22312 continue;
22315 /* If this is an undeduced nontype parameter that depends on
22316 a type parameter, try another pass; its type may have been
22317 deduced from a later argument than the one from which
22318 this parameter can be deduced. */
22319 if (TREE_CODE (tparm) == PARM_DECL
22320 && uses_template_parms (TREE_TYPE (tparm))
22321 && saw_undeduced < 2)
22323 saw_undeduced = 1;
22324 continue;
22327 /* Core issue #226 (C++0x) [temp.deduct]:
22329 If a template argument has not been deduced, its
22330 default template argument, if any, is used.
22332 When we are in C++98 mode, TREE_PURPOSE will either
22333 be NULL_TREE or ERROR_MARK_NODE, so we do not need
22334 to explicitly check cxx_dialect here. */
22335 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
22336 /* OK, there is a default argument. Wait until after the
22337 conversion check to do substitution. */
22338 continue;
22340 /* If the type parameter is a parameter pack, then it will
22341 be deduced to an empty parameter pack. */
22342 if (template_parameter_pack_p (tparm))
22344 tree arg;
22346 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
22348 arg = make_node (NONTYPE_ARGUMENT_PACK);
22349 TREE_CONSTANT (arg) = 1;
22351 else
22352 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
22354 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
22356 TREE_VEC_ELT (targs, i) = arg;
22357 continue;
22360 return unify_parameter_deduction_failure (explain_p, tparm);
22363 /* Now substitute into the default template arguments. */
22364 for (i = 0; i < ntparms; i++)
22366 tree targ = TREE_VEC_ELT (targs, i);
22367 tree tparm = TREE_VEC_ELT (tparms, i);
22369 if (targ || tparm == error_mark_node)
22370 continue;
22371 tree parm = TREE_VALUE (tparm);
22372 tree arg = TREE_PURPOSE (tparm);
22373 reopen_deferring_access_checks (*checks);
22374 location_t save_loc = input_location;
22375 if (DECL_P (parm))
22376 input_location = DECL_SOURCE_LOCATION (parm);
22378 if (saw_undeduced == 1
22379 && TREE_CODE (parm) == PARM_DECL
22380 && uses_template_parms (TREE_TYPE (parm)))
22382 /* The type of this non-type parameter depends on undeduced
22383 parameters. Don't try to use its default argument yet,
22384 since we might deduce an argument for it on the next pass,
22385 but do check whether the arguments we already have cause
22386 substitution failure, so that that happens before we try
22387 later default arguments (78489). */
22388 ++processing_template_decl;
22389 tree type = tsubst (TREE_TYPE (parm), full_targs, complain,
22390 NULL_TREE);
22391 --processing_template_decl;
22392 if (type == error_mark_node)
22393 arg = error_mark_node;
22394 else
22395 arg = NULL_TREE;
22397 else
22399 /* Even if the call is happening in template context, getting
22400 here means it's non-dependent, and a default argument is
22401 considered a separate definition under [temp.decls], so we can
22402 do this substitution without processing_template_decl. This
22403 is important if the default argument contains something that
22404 might be instantiation-dependent like access (87480). */
22405 processing_template_decl_sentinel s;
22406 tree substed = NULL_TREE;
22407 if (saw_undeduced == 1)
22409 /* First instatiate in template context, in case we still
22410 depend on undeduced template parameters. */
22411 ++processing_template_decl;
22412 substed = tsubst_template_arg (arg, full_targs, complain,
22413 NULL_TREE);
22414 --processing_template_decl;
22415 if (substed != error_mark_node
22416 && !uses_template_parms (substed))
22417 /* We replaced all the tparms, substitute again out of
22418 template context. */
22419 substed = NULL_TREE;
22421 if (!substed)
22422 substed = tsubst_template_arg (arg, full_targs, complain,
22423 NULL_TREE);
22425 if (!uses_template_parms (substed))
22426 arg = convert_template_argument (parm, substed, full_targs,
22427 complain, i, NULL_TREE);
22428 else if (saw_undeduced == 1)
22429 arg = NULL_TREE;
22430 else
22431 arg = error_mark_node;
22434 input_location = save_loc;
22435 *checks = get_deferred_access_checks ();
22436 pop_deferring_access_checks ();
22438 if (arg == error_mark_node)
22439 return 1;
22440 else if (arg)
22442 TREE_VEC_ELT (targs, i) = arg;
22443 /* The position of the first default template argument,
22444 is also the number of non-defaulted arguments in TARGS.
22445 Record that. */
22446 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
22447 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
22451 if (saw_undeduced++ == 1)
22452 goto again;
22455 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
22456 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
22458 return unify_success (explain_p);
22461 /* Subroutine of type_unification_real. Args are like the variables
22462 at the call site. ARG is an overloaded function (or template-id);
22463 we try deducing template args from each of the overloads, and if
22464 only one succeeds, we go with that. Modifies TARGS and returns
22465 true on success. */
22467 static bool
22468 resolve_overloaded_unification (tree tparms,
22469 tree targs,
22470 tree parm,
22471 tree arg,
22472 unification_kind_t strict,
22473 int sub_strict,
22474 bool explain_p)
22476 tree tempargs = copy_node (targs);
22477 int good = 0;
22478 tree goodfn = NULL_TREE;
22479 bool addr_p;
22481 if (TREE_CODE (arg) == ADDR_EXPR)
22483 arg = TREE_OPERAND (arg, 0);
22484 addr_p = true;
22486 else
22487 addr_p = false;
22489 if (TREE_CODE (arg) == COMPONENT_REF)
22490 /* Handle `&x' where `x' is some static or non-static member
22491 function name. */
22492 arg = TREE_OPERAND (arg, 1);
22494 if (TREE_CODE (arg) == OFFSET_REF)
22495 arg = TREE_OPERAND (arg, 1);
22497 /* Strip baselink information. */
22498 if (BASELINK_P (arg))
22499 arg = BASELINK_FUNCTIONS (arg);
22501 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
22503 /* If we got some explicit template args, we need to plug them into
22504 the affected templates before we try to unify, in case the
22505 explicit args will completely resolve the templates in question. */
22507 int ok = 0;
22508 tree expl_subargs = TREE_OPERAND (arg, 1);
22509 arg = TREE_OPERAND (arg, 0);
22511 for (lkp_iterator iter (arg); iter; ++iter)
22513 tree fn = *iter;
22514 tree subargs, elem;
22516 if (TREE_CODE (fn) != TEMPLATE_DECL)
22517 continue;
22519 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
22520 expl_subargs, NULL_TREE, tf_none,
22521 /*require_all_args=*/true,
22522 /*use_default_args=*/true);
22523 if (subargs != error_mark_node
22524 && !any_dependent_template_arguments_p (subargs))
22526 fn = instantiate_template (fn, subargs, tf_none);
22527 if (!constraints_satisfied_p (fn))
22528 continue;
22529 if (undeduced_auto_decl (fn))
22531 /* Instantiate the function to deduce its return type. */
22532 ++function_depth;
22533 instantiate_decl (fn, /*defer*/false, /*class*/false);
22534 --function_depth;
22537 if (flag_noexcept_type)
22538 maybe_instantiate_noexcept (fn, tf_none);
22540 elem = TREE_TYPE (fn);
22541 if (try_one_overload (tparms, targs, tempargs, parm,
22542 elem, strict, sub_strict, addr_p, explain_p)
22543 && (!goodfn || !same_type_p (goodfn, elem)))
22545 goodfn = elem;
22546 ++good;
22549 else if (subargs)
22550 ++ok;
22552 /* If no templates (or more than one) are fully resolved by the
22553 explicit arguments, this template-id is a non-deduced context; it
22554 could still be OK if we deduce all template arguments for the
22555 enclosing call through other arguments. */
22556 if (good != 1)
22557 good = ok;
22559 else if (!OVL_P (arg))
22560 /* If ARG is, for example, "(0, &f)" then its type will be unknown
22561 -- but the deduction does not succeed because the expression is
22562 not just the function on its own. */
22563 return false;
22564 else
22565 for (lkp_iterator iter (arg); iter; ++iter)
22567 tree fn = *iter;
22568 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
22569 strict, sub_strict, addr_p, explain_p)
22570 && (!goodfn || !decls_match (goodfn, fn)))
22572 goodfn = fn;
22573 ++good;
22577 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22578 to function or pointer to member function argument if the set of
22579 overloaded functions does not contain function templates and at most
22580 one of a set of overloaded functions provides a unique match.
22582 So if we found multiple possibilities, we return success but don't
22583 deduce anything. */
22585 if (good == 1)
22587 int i = TREE_VEC_LENGTH (targs);
22588 for (; i--; )
22589 if (TREE_VEC_ELT (tempargs, i))
22591 tree old = TREE_VEC_ELT (targs, i);
22592 tree new_ = TREE_VEC_ELT (tempargs, i);
22593 if (new_ && old && ARGUMENT_PACK_P (old)
22594 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
22595 /* Don't forget explicit template arguments in a pack. */
22596 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
22597 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
22598 TREE_VEC_ELT (targs, i) = new_;
22601 if (good)
22602 return true;
22604 return false;
22607 /* Core DR 115: In contexts where deduction is done and fails, or in
22608 contexts where deduction is not done, if a template argument list is
22609 specified and it, along with any default template arguments, identifies
22610 a single function template specialization, then the template-id is an
22611 lvalue for the function template specialization. */
22613 tree
22614 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
22616 tree expr, offset, baselink;
22617 bool addr;
22619 if (!type_unknown_p (orig_expr))
22620 return orig_expr;
22622 expr = orig_expr;
22623 addr = false;
22624 offset = NULL_TREE;
22625 baselink = NULL_TREE;
22627 if (TREE_CODE (expr) == ADDR_EXPR)
22629 expr = TREE_OPERAND (expr, 0);
22630 addr = true;
22632 if (TREE_CODE (expr) == OFFSET_REF)
22634 offset = expr;
22635 expr = TREE_OPERAND (expr, 1);
22637 if (BASELINK_P (expr))
22639 baselink = expr;
22640 expr = BASELINK_FUNCTIONS (expr);
22643 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
22645 int good = 0;
22646 tree goodfn = NULL_TREE;
22648 /* If we got some explicit template args, we need to plug them into
22649 the affected templates before we try to unify, in case the
22650 explicit args will completely resolve the templates in question. */
22652 tree expl_subargs = TREE_OPERAND (expr, 1);
22653 tree arg = TREE_OPERAND (expr, 0);
22654 tree badfn = NULL_TREE;
22655 tree badargs = NULL_TREE;
22657 for (lkp_iterator iter (arg); iter; ++iter)
22659 tree fn = *iter;
22660 tree subargs, elem;
22662 if (TREE_CODE (fn) != TEMPLATE_DECL)
22663 continue;
22665 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
22666 expl_subargs, NULL_TREE, tf_none,
22667 /*require_all_args=*/true,
22668 /*use_default_args=*/true);
22669 if (subargs != error_mark_node
22670 && !any_dependent_template_arguments_p (subargs))
22672 elem = instantiate_template (fn, subargs, tf_none);
22673 if (elem == error_mark_node)
22675 badfn = fn;
22676 badargs = subargs;
22678 else if (elem && (!goodfn || !decls_match (goodfn, elem))
22679 && constraints_satisfied_p (elem))
22681 goodfn = elem;
22682 ++good;
22686 if (good == 1)
22688 mark_used (goodfn);
22689 expr = goodfn;
22690 if (baselink)
22691 expr = build_baselink (BASELINK_BINFO (baselink),
22692 BASELINK_ACCESS_BINFO (baselink),
22693 expr, BASELINK_OPTYPE (baselink));
22694 if (offset)
22696 tree base
22697 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
22698 expr = build_offset_ref (base, expr, addr, complain);
22700 if (addr)
22701 expr = cp_build_addr_expr (expr, complain);
22702 return expr;
22704 else if (good == 0 && badargs && (complain & tf_error))
22705 /* There were no good options and at least one bad one, so let the
22706 user know what the problem is. */
22707 instantiate_template (badfn, badargs, complain);
22709 return orig_expr;
22712 /* As above, but error out if the expression remains overloaded. */
22714 tree
22715 resolve_nondeduced_context_or_error (tree exp, tsubst_flags_t complain)
22717 exp = resolve_nondeduced_context (exp, complain);
22718 if (type_unknown_p (exp))
22720 if (complain & tf_error)
22721 cxx_incomplete_type_error (exp, TREE_TYPE (exp));
22722 return error_mark_node;
22724 return exp;
22727 /* Subroutine of resolve_overloaded_unification; does deduction for a single
22728 overload. Fills TARGS with any deduced arguments, or error_mark_node if
22729 different overloads deduce different arguments for a given parm.
22730 ADDR_P is true if the expression for which deduction is being
22731 performed was of the form "& fn" rather than simply "fn".
22733 Returns 1 on success. */
22735 static int
22736 try_one_overload (tree tparms,
22737 tree orig_targs,
22738 tree targs,
22739 tree parm,
22740 tree arg,
22741 unification_kind_t strict,
22742 int sub_strict,
22743 bool addr_p,
22744 bool explain_p)
22746 int nargs;
22747 tree tempargs;
22748 int i;
22750 if (arg == error_mark_node)
22751 return 0;
22753 /* [temp.deduct.type] A template-argument can be deduced from a pointer
22754 to function or pointer to member function argument if the set of
22755 overloaded functions does not contain function templates and at most
22756 one of a set of overloaded functions provides a unique match.
22758 So if this is a template, just return success. */
22760 if (uses_template_parms (arg))
22761 return 1;
22763 if (TREE_CODE (arg) == METHOD_TYPE)
22764 arg = build_ptrmemfunc_type (build_pointer_type (arg));
22765 else if (addr_p)
22766 arg = build_pointer_type (arg);
22768 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
22770 /* We don't copy orig_targs for this because if we have already deduced
22771 some template args from previous args, unify would complain when we
22772 try to deduce a template parameter for the same argument, even though
22773 there isn't really a conflict. */
22774 nargs = TREE_VEC_LENGTH (targs);
22775 tempargs = make_tree_vec (nargs);
22777 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
22778 return 0;
22780 /* First make sure we didn't deduce anything that conflicts with
22781 explicitly specified args. */
22782 for (i = nargs; i--; )
22784 tree elt = TREE_VEC_ELT (tempargs, i);
22785 tree oldelt = TREE_VEC_ELT (orig_targs, i);
22787 if (!elt)
22788 /*NOP*/;
22789 else if (uses_template_parms (elt))
22790 /* Since we're unifying against ourselves, we will fill in
22791 template args used in the function parm list with our own
22792 template parms. Discard them. */
22793 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
22794 else if (oldelt && ARGUMENT_PACK_P (oldelt))
22796 /* Check that the argument at each index of the deduced argument pack
22797 is equivalent to the corresponding explicitly specified argument.
22798 We may have deduced more arguments than were explicitly specified,
22799 and that's OK. */
22801 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
22802 that's wrong if we deduce the same argument pack from multiple
22803 function arguments: it's only incomplete the first time. */
22805 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
22806 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
22808 if (TREE_VEC_LENGTH (deduced_pack)
22809 < TREE_VEC_LENGTH (explicit_pack))
22810 return 0;
22812 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
22813 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
22814 TREE_VEC_ELT (deduced_pack, j)))
22815 return 0;
22817 else if (oldelt && !template_args_equal (oldelt, elt))
22818 return 0;
22821 for (i = nargs; i--; )
22823 tree elt = TREE_VEC_ELT (tempargs, i);
22825 if (elt)
22826 TREE_VEC_ELT (targs, i) = elt;
22829 return 1;
22832 /* PARM is a template class (perhaps with unbound template
22833 parameters). ARG is a fully instantiated type. If ARG can be
22834 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
22835 TARGS are as for unify. */
22837 static tree
22838 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
22839 bool explain_p)
22841 tree copy_of_targs;
22843 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
22844 return NULL_TREE;
22845 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22846 /* Matches anything. */;
22847 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
22848 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
22849 return NULL_TREE;
22851 /* We need to make a new template argument vector for the call to
22852 unify. If we used TARGS, we'd clutter it up with the result of
22853 the attempted unification, even if this class didn't work out.
22854 We also don't want to commit ourselves to all the unifications
22855 we've already done, since unification is supposed to be done on
22856 an argument-by-argument basis. In other words, consider the
22857 following pathological case:
22859 template <int I, int J, int K>
22860 struct S {};
22862 template <int I, int J>
22863 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
22865 template <int I, int J, int K>
22866 void f(S<I, J, K>, S<I, I, I>);
22868 void g() {
22869 S<0, 0, 0> s0;
22870 S<0, 1, 2> s2;
22872 f(s0, s2);
22875 Now, by the time we consider the unification involving `s2', we
22876 already know that we must have `f<0, 0, 0>'. But, even though
22877 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
22878 because there are two ways to unify base classes of S<0, 1, 2>
22879 with S<I, I, I>. If we kept the already deduced knowledge, we
22880 would reject the possibility I=1. */
22881 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
22883 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
22885 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
22886 return NULL_TREE;
22887 return arg;
22890 /* If unification failed, we're done. */
22891 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
22892 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
22893 return NULL_TREE;
22895 return arg;
22898 /* Given a template type PARM and a class type ARG, find the unique
22899 base type in ARG that is an instance of PARM. We do not examine
22900 ARG itself; only its base-classes. If there is not exactly one
22901 appropriate base class, return NULL_TREE. PARM may be the type of
22902 a partial specialization, as well as a plain template type. Used
22903 by unify. */
22905 static enum template_base_result
22906 get_template_base (tree tparms, tree targs, tree parm, tree arg,
22907 bool explain_p, tree *result)
22909 tree rval = NULL_TREE;
22910 tree binfo;
22912 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
22914 binfo = TYPE_BINFO (complete_type (arg));
22915 if (!binfo)
22917 /* The type could not be completed. */
22918 *result = NULL_TREE;
22919 return tbr_incomplete_type;
22922 /* Walk in inheritance graph order. The search order is not
22923 important, and this avoids multiple walks of virtual bases. */
22924 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
22926 tree r = try_class_unification (tparms, targs, parm,
22927 BINFO_TYPE (binfo), explain_p);
22929 if (r)
22931 /* If there is more than one satisfactory baseclass, then:
22933 [temp.deduct.call]
22935 If they yield more than one possible deduced A, the type
22936 deduction fails.
22938 applies. */
22939 if (rval && !same_type_p (r, rval))
22941 /* [temp.deduct.call]/4.3: If there is a class C that is a
22942 (direct or indirect) base class of D and derived (directly or
22943 indirectly) from a class B and that would be a valid deduced
22944 A, the deduced A cannot be B or pointer to B, respectively. */
22945 if (DERIVED_FROM_P (r, rval))
22946 /* Ignore r. */
22947 continue;
22948 else if (DERIVED_FROM_P (rval, r))
22949 /* Ignore rval. */;
22950 else
22952 *result = NULL_TREE;
22953 return tbr_ambiguous_baseclass;
22957 rval = r;
22961 *result = rval;
22962 return tbr_success;
22965 /* Returns the level of DECL, which declares a template parameter. */
22967 static int
22968 template_decl_level (tree decl)
22970 switch (TREE_CODE (decl))
22972 case TYPE_DECL:
22973 case TEMPLATE_DECL:
22974 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
22976 case PARM_DECL:
22977 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
22979 default:
22980 gcc_unreachable ();
22982 return 0;
22985 /* Decide whether ARG can be unified with PARM, considering only the
22986 cv-qualifiers of each type, given STRICT as documented for unify.
22987 Returns nonzero iff the unification is OK on that basis. */
22989 static int
22990 check_cv_quals_for_unify (int strict, tree arg, tree parm)
22992 int arg_quals = cp_type_quals (arg);
22993 int parm_quals = cp_type_quals (parm);
22995 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
22996 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
22998 /* Although a CVR qualifier is ignored when being applied to a
22999 substituted template parameter ([8.3.2]/1 for example), that
23000 does not allow us to unify "const T" with "int&" because both
23001 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
23002 It is ok when we're allowing additional CV qualifiers
23003 at the outer level [14.8.2.1]/3,1st bullet. */
23004 if ((TYPE_REF_P (arg)
23005 || FUNC_OR_METHOD_TYPE_P (arg))
23006 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
23007 return 0;
23009 if ((!INDIRECT_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
23010 && (parm_quals & TYPE_QUAL_RESTRICT))
23011 return 0;
23014 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
23015 && (arg_quals & parm_quals) != parm_quals)
23016 return 0;
23018 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
23019 && (parm_quals & arg_quals) != arg_quals)
23020 return 0;
23022 return 1;
23025 /* Determines the LEVEL and INDEX for the template parameter PARM. */
23026 void
23027 template_parm_level_and_index (tree parm, int* level, int* index)
23029 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
23030 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
23031 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
23033 *index = TEMPLATE_TYPE_IDX (parm);
23034 *level = TEMPLATE_TYPE_LEVEL (parm);
23036 else
23038 *index = TEMPLATE_PARM_IDX (parm);
23039 *level = TEMPLATE_PARM_LEVEL (parm);
23043 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
23044 do { \
23045 if (unify (TP, TA, P, A, S, EP)) \
23046 return 1; \
23047 } while (0)
23049 /* Unifies the remaining arguments in PACKED_ARGS with the pack
23050 expansion at the end of PACKED_PARMS. Returns 0 if the type
23051 deduction succeeds, 1 otherwise. STRICT is the same as in
23052 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
23053 function call argument list. We'll need to adjust the arguments to make them
23054 types. SUBR tells us if this is from a recursive call to
23055 type_unification_real, or for comparing two template argument
23056 lists. */
23058 static int
23059 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
23060 tree packed_args, unification_kind_t strict,
23061 bool subr, bool explain_p)
23063 tree parm
23064 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
23065 tree pattern = PACK_EXPANSION_PATTERN (parm);
23066 tree pack, packs = NULL_TREE;
23067 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
23069 /* Add in any args remembered from an earlier partial instantiation. */
23070 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
23071 int levels = TMPL_ARGS_DEPTH (targs);
23073 packed_args = expand_template_argument_pack (packed_args);
23075 int len = TREE_VEC_LENGTH (packed_args);
23077 /* Determine the parameter packs we will be deducing from the
23078 pattern, and record their current deductions. */
23079 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
23080 pack; pack = TREE_CHAIN (pack))
23082 tree parm_pack = TREE_VALUE (pack);
23083 int idx, level;
23085 /* Only template parameter packs can be deduced, not e.g. function
23086 parameter packs or __bases or __integer_pack. */
23087 if (!TEMPLATE_PARM_P (parm_pack))
23088 continue;
23090 /* Determine the index and level of this parameter pack. */
23091 template_parm_level_and_index (parm_pack, &level, &idx);
23092 if (level < levels)
23093 continue;
23095 /* Keep track of the parameter packs and their corresponding
23096 argument packs. */
23097 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
23098 TREE_TYPE (packs) = make_tree_vec (len - start);
23101 /* Loop through all of the arguments that have not yet been
23102 unified and unify each with the pattern. */
23103 for (i = start; i < len; i++)
23105 tree parm;
23106 bool any_explicit = false;
23107 tree arg = TREE_VEC_ELT (packed_args, i);
23109 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
23110 or the element of its argument pack at the current index if
23111 this argument was explicitly specified. */
23112 for (pack = packs; pack; pack = TREE_CHAIN (pack))
23114 int idx, level;
23115 tree arg, pargs;
23116 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
23118 arg = NULL_TREE;
23119 if (TREE_VALUE (pack)
23120 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
23121 && (i - start < TREE_VEC_LENGTH (pargs)))
23123 any_explicit = true;
23124 arg = TREE_VEC_ELT (pargs, i - start);
23126 TMPL_ARG (targs, level, idx) = arg;
23129 /* If we had explicit template arguments, substitute them into the
23130 pattern before deduction. */
23131 if (any_explicit)
23133 /* Some arguments might still be unspecified or dependent. */
23134 bool dependent;
23135 ++processing_template_decl;
23136 dependent = any_dependent_template_arguments_p (targs);
23137 if (!dependent)
23138 --processing_template_decl;
23139 parm = tsubst (pattern, targs,
23140 explain_p ? tf_warning_or_error : tf_none,
23141 NULL_TREE);
23142 if (dependent)
23143 --processing_template_decl;
23144 if (parm == error_mark_node)
23145 return 1;
23147 else
23148 parm = pattern;
23150 /* Unify the pattern with the current argument. */
23151 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
23152 explain_p))
23153 return 1;
23155 /* For each parameter pack, collect the deduced value. */
23156 for (pack = packs; pack; pack = TREE_CHAIN (pack))
23158 int idx, level;
23159 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
23161 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
23162 TMPL_ARG (targs, level, idx);
23166 /* Verify that the results of unification with the parameter packs
23167 produce results consistent with what we've seen before, and make
23168 the deduced argument packs available. */
23169 for (pack = packs; pack; pack = TREE_CHAIN (pack))
23171 tree old_pack = TREE_VALUE (pack);
23172 tree new_args = TREE_TYPE (pack);
23173 int i, len = TREE_VEC_LENGTH (new_args);
23174 int idx, level;
23175 bool nondeduced_p = false;
23177 /* By default keep the original deduced argument pack.
23178 If necessary, more specific code is going to update the
23179 resulting deduced argument later down in this function. */
23180 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
23181 TMPL_ARG (targs, level, idx) = old_pack;
23183 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
23184 actually deduce anything. */
23185 for (i = 0; i < len && !nondeduced_p; ++i)
23186 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
23187 nondeduced_p = true;
23188 if (nondeduced_p)
23189 continue;
23191 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
23193 /* If we had fewer function args than explicit template args,
23194 just use the explicits. */
23195 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
23196 int explicit_len = TREE_VEC_LENGTH (explicit_args);
23197 if (len < explicit_len)
23198 new_args = explicit_args;
23201 if (!old_pack)
23203 tree result;
23204 /* Build the deduced *_ARGUMENT_PACK. */
23205 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
23207 result = make_node (NONTYPE_ARGUMENT_PACK);
23208 TREE_CONSTANT (result) = 1;
23210 else
23211 result = cxx_make_type (TYPE_ARGUMENT_PACK);
23213 SET_ARGUMENT_PACK_ARGS (result, new_args);
23215 /* Note the deduced argument packs for this parameter
23216 pack. */
23217 TMPL_ARG (targs, level, idx) = result;
23219 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
23220 && (ARGUMENT_PACK_ARGS (old_pack)
23221 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
23223 /* We only had the explicitly-provided arguments before, but
23224 now we have a complete set of arguments. */
23225 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
23227 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
23228 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
23229 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
23231 else
23233 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
23234 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
23235 temp_override<int> ovl (TREE_VEC_LENGTH (old_args));
23236 /* During template argument deduction for the aggregate deduction
23237 candidate, the number of elements in a trailing parameter pack
23238 is only deduced from the number of remaining function
23239 arguments if it is not otherwise deduced. */
23240 if (cxx_dialect >= cxx20
23241 && TREE_VEC_LENGTH (new_args) < TREE_VEC_LENGTH (old_args)
23242 && builtin_guide_p (TPARMS_PRIMARY_TEMPLATE (tparms)))
23243 TREE_VEC_LENGTH (old_args) = TREE_VEC_LENGTH (new_args);
23244 if (!comp_template_args (old_args, new_args,
23245 &bad_old_arg, &bad_new_arg))
23246 /* Inconsistent unification of this parameter pack. */
23247 return unify_parameter_pack_inconsistent (explain_p,
23248 bad_old_arg,
23249 bad_new_arg);
23253 return unify_success (explain_p);
23256 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
23257 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
23258 parameters and return value are as for unify. */
23260 static int
23261 unify_array_domain (tree tparms, tree targs,
23262 tree parm_dom, tree arg_dom,
23263 bool explain_p)
23265 tree parm_max;
23266 tree arg_max;
23267 bool parm_cst;
23268 bool arg_cst;
23270 /* Our representation of array types uses "N - 1" as the
23271 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
23272 not an integer constant. We cannot unify arbitrarily
23273 complex expressions, so we eliminate the MINUS_EXPRs
23274 here. */
23275 parm_max = TYPE_MAX_VALUE (parm_dom);
23276 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
23277 if (!parm_cst)
23279 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
23280 parm_max = TREE_OPERAND (parm_max, 0);
23282 arg_max = TYPE_MAX_VALUE (arg_dom);
23283 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
23284 if (!arg_cst)
23286 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
23287 trying to unify the type of a variable with the type
23288 of a template parameter. For example:
23290 template <unsigned int N>
23291 void f (char (&) [N]);
23292 int g();
23293 void h(int i) {
23294 char a[g(i)];
23295 f(a);
23298 Here, the type of the ARG will be "int [g(i)]", and
23299 may be a SAVE_EXPR, etc. */
23300 if (TREE_CODE (arg_max) != MINUS_EXPR)
23301 return unify_vla_arg (explain_p, arg_dom);
23302 arg_max = TREE_OPERAND (arg_max, 0);
23305 /* If only one of the bounds used a MINUS_EXPR, compensate
23306 by adding one to the other bound. */
23307 if (parm_cst && !arg_cst)
23308 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
23309 integer_type_node,
23310 parm_max,
23311 integer_one_node);
23312 else if (arg_cst && !parm_cst)
23313 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
23314 integer_type_node,
23315 arg_max,
23316 integer_one_node);
23318 return unify (tparms, targs, parm_max, arg_max,
23319 UNIFY_ALLOW_INTEGER, explain_p);
23322 /* Returns whether T, a P or A in unify, is a type, template or expression. */
23324 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
23326 static pa_kind_t
23327 pa_kind (tree t)
23329 if (PACK_EXPANSION_P (t))
23330 t = PACK_EXPANSION_PATTERN (t);
23331 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
23332 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
23333 || DECL_TYPE_TEMPLATE_P (t))
23334 return pa_tmpl;
23335 else if (TYPE_P (t))
23336 return pa_type;
23337 else
23338 return pa_expr;
23341 /* Deduce the value of template parameters. TPARMS is the (innermost)
23342 set of template parameters to a template. TARGS is the bindings
23343 for those template parameters, as determined thus far; TARGS may
23344 include template arguments for outer levels of template parameters
23345 as well. PARM is a parameter to a template function, or a
23346 subcomponent of that parameter; ARG is the corresponding argument.
23347 This function attempts to match PARM with ARG in a manner
23348 consistent with the existing assignments in TARGS. If more values
23349 are deduced, then TARGS is updated.
23351 Returns 0 if the type deduction succeeds, 1 otherwise. The
23352 parameter STRICT is a bitwise or of the following flags:
23354 UNIFY_ALLOW_NONE:
23355 Require an exact match between PARM and ARG.
23356 UNIFY_ALLOW_MORE_CV_QUAL:
23357 Allow the deduced ARG to be more cv-qualified (by qualification
23358 conversion) than ARG.
23359 UNIFY_ALLOW_LESS_CV_QUAL:
23360 Allow the deduced ARG to be less cv-qualified than ARG.
23361 UNIFY_ALLOW_DERIVED:
23362 Allow the deduced ARG to be a template base class of ARG,
23363 or a pointer to a template base class of the type pointed to by
23364 ARG.
23365 UNIFY_ALLOW_INTEGER:
23366 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
23367 case for more information.
23368 UNIFY_ALLOW_OUTER_LEVEL:
23369 This is the outermost level of a deduction. Used to determine validity
23370 of qualification conversions. A valid qualification conversion must
23371 have const qualified pointers leading up to the inner type which
23372 requires additional CV quals, except at the outer level, where const
23373 is not required [conv.qual]. It would be normal to set this flag in
23374 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
23375 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
23376 This is the outermost level of a deduction, and PARM can be more CV
23377 qualified at this point.
23378 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
23379 This is the outermost level of a deduction, and PARM can be less CV
23380 qualified at this point. */
23382 static int
23383 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
23384 bool explain_p)
23386 int idx;
23387 tree targ;
23388 tree tparm;
23389 int strict_in = strict;
23390 tsubst_flags_t complain = (explain_p
23391 ? tf_warning_or_error
23392 : tf_none);
23394 /* I don't think this will do the right thing with respect to types.
23395 But the only case I've seen it in so far has been array bounds, where
23396 signedness is the only information lost, and I think that will be
23397 okay. VIEW_CONVERT_EXPR can appear with class NTTP, thanks to
23398 finish_id_expression_1, and are also OK. */
23399 while (CONVERT_EXPR_P (parm) || TREE_CODE (parm) == VIEW_CONVERT_EXPR)
23400 parm = TREE_OPERAND (parm, 0);
23402 if (arg == error_mark_node)
23403 return unify_invalid (explain_p);
23404 if (arg == unknown_type_node
23405 || arg == init_list_type_node)
23406 /* We can't deduce anything from this, but we might get all the
23407 template args from other function args. */
23408 return unify_success (explain_p);
23410 if (parm == any_targ_node || arg == any_targ_node)
23411 return unify_success (explain_p);
23413 /* If PARM uses template parameters, then we can't bail out here,
23414 even if ARG == PARM, since we won't record unifications for the
23415 template parameters. We might need them if we're trying to
23416 figure out which of two things is more specialized. */
23417 if (arg == parm && !uses_template_parms (parm))
23418 return unify_success (explain_p);
23420 /* Handle init lists early, so the rest of the function can assume
23421 we're dealing with a type. */
23422 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
23424 tree elt, elttype;
23425 unsigned i;
23426 tree orig_parm = parm;
23428 if (!is_std_init_list (parm)
23429 && TREE_CODE (parm) != ARRAY_TYPE)
23430 /* We can only deduce from an initializer list argument if the
23431 parameter is std::initializer_list or an array; otherwise this
23432 is a non-deduced context. */
23433 return unify_success (explain_p);
23435 if (TREE_CODE (parm) == ARRAY_TYPE)
23436 elttype = TREE_TYPE (parm);
23437 else
23439 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
23440 /* Deduction is defined in terms of a single type, so just punt
23441 on the (bizarre) std::initializer_list<T...>. */
23442 if (PACK_EXPANSION_P (elttype))
23443 return unify_success (explain_p);
23446 if (strict != DEDUCE_EXACT
23447 && TYPE_P (elttype)
23448 && !uses_deducible_template_parms (elttype))
23449 /* If ELTTYPE has no deducible template parms, skip deduction from
23450 the list elements. */;
23451 else
23452 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
23454 int elt_strict = strict;
23456 if (elt == error_mark_node)
23457 return unify_invalid (explain_p);
23459 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
23461 tree type = TREE_TYPE (elt);
23462 if (type == error_mark_node)
23463 return unify_invalid (explain_p);
23464 /* It should only be possible to get here for a call. */
23465 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
23466 elt_strict |= maybe_adjust_types_for_deduction
23467 (DEDUCE_CALL, &elttype, &type, elt);
23468 elt = type;
23471 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
23472 explain_p);
23475 if (TREE_CODE (parm) == ARRAY_TYPE
23476 && deducible_array_bound (TYPE_DOMAIN (parm)))
23478 /* Also deduce from the length of the initializer list. */
23479 tree max = size_int (CONSTRUCTOR_NELTS (arg));
23480 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
23481 if (idx == error_mark_node)
23482 return unify_invalid (explain_p);
23483 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
23484 idx, explain_p);
23487 /* If the std::initializer_list<T> deduction worked, replace the
23488 deduced A with std::initializer_list<A>. */
23489 if (orig_parm != parm)
23491 idx = TEMPLATE_TYPE_IDX (orig_parm);
23492 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23493 targ = listify (targ);
23494 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
23496 return unify_success (explain_p);
23499 /* If parm and arg aren't the same kind of thing (template, type, or
23500 expression), fail early. */
23501 if (pa_kind (parm) != pa_kind (arg))
23502 return unify_invalid (explain_p);
23504 /* Immediately reject some pairs that won't unify because of
23505 cv-qualification mismatches. */
23506 if (TREE_CODE (arg) == TREE_CODE (parm)
23507 && TYPE_P (arg)
23508 /* It is the elements of the array which hold the cv quals of an array
23509 type, and the elements might be template type parms. We'll check
23510 when we recurse. */
23511 && TREE_CODE (arg) != ARRAY_TYPE
23512 /* We check the cv-qualifiers when unifying with template type
23513 parameters below. We want to allow ARG `const T' to unify with
23514 PARM `T' for example, when computing which of two templates
23515 is more specialized, for example. */
23516 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
23517 && !check_cv_quals_for_unify (strict_in, arg, parm))
23518 return unify_cv_qual_mismatch (explain_p, parm, arg);
23520 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
23521 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
23522 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
23523 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
23524 strict &= ~UNIFY_ALLOW_DERIVED;
23525 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
23526 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
23528 switch (TREE_CODE (parm))
23530 case TYPENAME_TYPE:
23531 case SCOPE_REF:
23532 case UNBOUND_CLASS_TEMPLATE:
23533 /* In a type which contains a nested-name-specifier, template
23534 argument values cannot be deduced for template parameters used
23535 within the nested-name-specifier. */
23536 return unify_success (explain_p);
23538 case TEMPLATE_TYPE_PARM:
23539 case TEMPLATE_TEMPLATE_PARM:
23540 case BOUND_TEMPLATE_TEMPLATE_PARM:
23541 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
23542 if (error_operand_p (tparm))
23543 return unify_invalid (explain_p);
23545 if (TEMPLATE_TYPE_LEVEL (parm)
23546 != template_decl_level (tparm))
23547 /* The PARM is not one we're trying to unify. Just check
23548 to see if it matches ARG. */
23550 if (TREE_CODE (arg) == TREE_CODE (parm)
23551 && (is_auto (parm) ? is_auto (arg)
23552 : same_type_p (parm, arg)))
23553 return unify_success (explain_p);
23554 else
23555 return unify_type_mismatch (explain_p, parm, arg);
23557 idx = TEMPLATE_TYPE_IDX (parm);
23558 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23559 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
23560 if (error_operand_p (tparm))
23561 return unify_invalid (explain_p);
23563 /* Check for mixed types and values. */
23564 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
23565 && TREE_CODE (tparm) != TYPE_DECL)
23566 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
23567 && TREE_CODE (tparm) != TEMPLATE_DECL))
23568 gcc_unreachable ();
23570 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
23572 if ((strict_in & UNIFY_ALLOW_DERIVED)
23573 && CLASS_TYPE_P (arg))
23575 /* First try to match ARG directly. */
23576 tree t = try_class_unification (tparms, targs, parm, arg,
23577 explain_p);
23578 if (!t)
23580 /* Otherwise, look for a suitable base of ARG, as below. */
23581 enum template_base_result r;
23582 r = get_template_base (tparms, targs, parm, arg,
23583 explain_p, &t);
23584 if (!t)
23585 return unify_no_common_base (explain_p, r, parm, arg);
23586 arg = t;
23589 /* ARG must be constructed from a template class or a template
23590 template parameter. */
23591 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
23592 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
23593 return unify_template_deduction_failure (explain_p, parm, arg);
23595 /* Deduce arguments T, i from TT<T> or TT<i>. */
23596 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
23597 return 1;
23599 arg = TYPE_TI_TEMPLATE (arg);
23600 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg))
23601 /* If the template is a template template parameter, use the
23602 TEMPLATE_TEMPLATE_PARM for matching. */
23603 arg = TREE_TYPE (arg);
23605 /* Fall through to deduce template name. */
23608 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
23609 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
23611 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
23613 /* Simple cases: Value already set, does match or doesn't. */
23614 if (targ != NULL_TREE && template_args_equal (targ, arg))
23615 return unify_success (explain_p);
23616 else if (targ)
23617 return unify_inconsistency (explain_p, parm, targ, arg);
23619 else
23621 /* If PARM is `const T' and ARG is only `int', we don't have
23622 a match unless we are allowing additional qualification.
23623 If ARG is `const int' and PARM is just `T' that's OK;
23624 that binds `const int' to `T'. */
23625 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
23626 arg, parm))
23627 return unify_cv_qual_mismatch (explain_p, parm, arg);
23629 /* Consider the case where ARG is `const volatile int' and
23630 PARM is `const T'. Then, T should be `volatile int'. */
23631 arg = cp_build_qualified_type_real
23632 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
23633 if (arg == error_mark_node)
23634 return unify_invalid (explain_p);
23636 /* Simple cases: Value already set, does match or doesn't. */
23637 if (targ != NULL_TREE && same_type_p (targ, arg))
23638 return unify_success (explain_p);
23639 else if (targ)
23640 return unify_inconsistency (explain_p, parm, targ, arg);
23642 /* Make sure that ARG is not a variable-sized array. (Note
23643 that were talking about variable-sized arrays (like
23644 `int[n]'), rather than arrays of unknown size (like
23645 `int[]').) We'll get very confused by such a type since
23646 the bound of the array is not constant, and therefore
23647 not mangleable. Besides, such types are not allowed in
23648 ISO C++, so we can do as we please here. We do allow
23649 them for 'auto' deduction, since that isn't ABI-exposed. */
23650 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
23651 return unify_vla_arg (explain_p, arg);
23653 /* Strip typedefs as in convert_template_argument. */
23654 arg = canonicalize_type_argument (arg, tf_none);
23657 /* If ARG is a parameter pack or an expansion, we cannot unify
23658 against it unless PARM is also a parameter pack. */
23659 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
23660 && !template_parameter_pack_p (parm))
23661 return unify_parameter_pack_mismatch (explain_p, parm, arg);
23663 /* If the argument deduction results is a METHOD_TYPE,
23664 then there is a problem.
23665 METHOD_TYPE doesn't map to any real C++ type the result of
23666 the deduction cannot be of that type. */
23667 if (TREE_CODE (arg) == METHOD_TYPE)
23668 return unify_method_type_error (explain_p, arg);
23670 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
23671 return unify_success (explain_p);
23673 case TEMPLATE_PARM_INDEX:
23674 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
23675 if (error_operand_p (tparm))
23676 return unify_invalid (explain_p);
23678 if (TEMPLATE_PARM_LEVEL (parm)
23679 != template_decl_level (tparm))
23681 /* The PARM is not one we're trying to unify. Just check
23682 to see if it matches ARG. */
23683 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
23684 && cp_tree_equal (parm, arg));
23685 if (result)
23686 unify_expression_unequal (explain_p, parm, arg);
23687 return result;
23690 idx = TEMPLATE_PARM_IDX (parm);
23691 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
23693 if (targ)
23695 if ((strict & UNIFY_ALLOW_INTEGER)
23696 && TREE_TYPE (targ) && TREE_TYPE (arg)
23697 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
23698 /* We're deducing from an array bound, the type doesn't matter. */
23699 arg = fold_convert (TREE_TYPE (targ), arg);
23700 int x = !cp_tree_equal (targ, arg);
23701 if (x)
23702 unify_inconsistency (explain_p, parm, targ, arg);
23703 return x;
23706 /* [temp.deduct.type] If, in the declaration of a function template
23707 with a non-type template-parameter, the non-type
23708 template-parameter is used in an expression in the function
23709 parameter-list and, if the corresponding template-argument is
23710 deduced, the template-argument type shall match the type of the
23711 template-parameter exactly, except that a template-argument
23712 deduced from an array bound may be of any integral type.
23713 The non-type parameter might use already deduced type parameters. */
23714 tparm = TREE_TYPE (parm);
23715 if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs))
23716 /* We don't have enough levels of args to do any substitution. This
23717 can happen in the context of -fnew-ttp-matching. */;
23718 else
23720 ++processing_template_decl;
23721 tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
23722 --processing_template_decl;
23724 if (tree a = type_uses_auto (tparm))
23726 tparm = do_auto_deduction (tparm, arg, a,
23727 complain, adc_unify, targs);
23728 if (tparm == error_mark_node)
23729 return 1;
23733 if (!TREE_TYPE (arg))
23734 /* Template-parameter dependent expression. Just accept it for now.
23735 It will later be processed in convert_template_argument. */
23737 else if (same_type_ignoring_top_level_qualifiers_p
23738 (non_reference (TREE_TYPE (arg)),
23739 non_reference (tparm)))
23740 /* OK. Ignore top-level quals here because a class-type template
23741 parameter object is const. */;
23742 else if ((strict & UNIFY_ALLOW_INTEGER)
23743 && CP_INTEGRAL_TYPE_P (tparm))
23744 /* Convert the ARG to the type of PARM; the deduced non-type
23745 template argument must exactly match the types of the
23746 corresponding parameter. */
23747 arg = fold (build_nop (tparm, arg));
23748 else if (uses_template_parms (tparm))
23750 /* We haven't deduced the type of this parameter yet. */
23751 if (cxx_dialect >= cxx17
23752 /* We deduce from array bounds in try_array_deduction. */
23753 && !(strict & UNIFY_ALLOW_INTEGER)
23754 && TEMPLATE_PARM_LEVEL (parm) <= TMPL_ARGS_DEPTH (targs))
23756 /* Deduce it from the non-type argument. */
23757 tree atype = TREE_TYPE (arg);
23758 RECUR_AND_CHECK_FAILURE (tparms, targs,
23759 tparm, atype,
23760 UNIFY_ALLOW_NONE, explain_p);
23761 /* Now check whether the type of this parameter is still
23762 dependent, and give up if so. */
23763 ++processing_template_decl;
23764 tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
23765 --processing_template_decl;
23766 if (uses_template_parms (tparm))
23767 return unify_success (explain_p);
23769 else
23770 /* Try again later. */
23771 return unify_success (explain_p);
23773 else
23774 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
23776 /* If ARG is a parameter pack or an expansion, we cannot unify
23777 against it unless PARM is also a parameter pack. */
23778 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
23779 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
23780 return unify_parameter_pack_mismatch (explain_p, parm, arg);
23783 bool removed_attr = false;
23784 arg = strip_typedefs_expr (arg, &removed_attr);
23786 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
23787 return unify_success (explain_p);
23789 case PTRMEM_CST:
23791 /* A pointer-to-member constant can be unified only with
23792 another constant. */
23793 if (TREE_CODE (arg) != PTRMEM_CST)
23794 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
23796 /* Just unify the class member. It would be useless (and possibly
23797 wrong, depending on the strict flags) to unify also
23798 PTRMEM_CST_CLASS, because we want to be sure that both parm and
23799 arg refer to the same variable, even if through different
23800 classes. For instance:
23802 struct A { int x; };
23803 struct B : A { };
23805 Unification of &A::x and &B::x must succeed. */
23806 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
23807 PTRMEM_CST_MEMBER (arg), strict, explain_p);
23810 case POINTER_TYPE:
23812 if (!TYPE_PTR_P (arg))
23813 return unify_type_mismatch (explain_p, parm, arg);
23815 /* [temp.deduct.call]
23817 A can be another pointer or pointer to member type that can
23818 be converted to the deduced A via a qualification
23819 conversion (_conv.qual_).
23821 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
23822 This will allow for additional cv-qualification of the
23823 pointed-to types if appropriate. */
23825 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
23826 /* The derived-to-base conversion only persists through one
23827 level of pointers. */
23828 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
23830 return unify (tparms, targs, TREE_TYPE (parm),
23831 TREE_TYPE (arg), strict, explain_p);
23834 case REFERENCE_TYPE:
23835 if (!TYPE_REF_P (arg))
23836 return unify_type_mismatch (explain_p, parm, arg);
23837 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23838 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
23840 case ARRAY_TYPE:
23841 if (TREE_CODE (arg) != ARRAY_TYPE)
23842 return unify_type_mismatch (explain_p, parm, arg);
23843 if ((TYPE_DOMAIN (parm) == NULL_TREE)
23844 != (TYPE_DOMAIN (arg) == NULL_TREE))
23845 return unify_type_mismatch (explain_p, parm, arg);
23846 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
23847 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
23848 if (TYPE_DOMAIN (parm) != NULL_TREE)
23849 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
23850 TYPE_DOMAIN (arg), explain_p);
23851 return unify_success (explain_p);
23853 case REAL_TYPE:
23854 case COMPLEX_TYPE:
23855 case VECTOR_TYPE:
23856 case INTEGER_TYPE:
23857 case BOOLEAN_TYPE:
23858 case ENUMERAL_TYPE:
23859 case VOID_TYPE:
23860 case OPAQUE_TYPE:
23861 case NULLPTR_TYPE:
23862 if (TREE_CODE (arg) != TREE_CODE (parm))
23863 return unify_type_mismatch (explain_p, parm, arg);
23865 /* We have already checked cv-qualification at the top of the
23866 function. */
23867 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
23868 return unify_type_mismatch (explain_p, parm, arg);
23870 /* As far as unification is concerned, this wins. Later checks
23871 will invalidate it if necessary. */
23872 return unify_success (explain_p);
23874 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
23875 /* Type INTEGER_CST can come from ordinary constant template args. */
23876 case INTEGER_CST:
23877 while (CONVERT_EXPR_P (arg))
23878 arg = TREE_OPERAND (arg, 0);
23880 if (TREE_CODE (arg) != INTEGER_CST)
23881 return unify_template_argument_mismatch (explain_p, parm, arg);
23882 return (tree_int_cst_equal (parm, arg)
23883 ? unify_success (explain_p)
23884 : unify_template_argument_mismatch (explain_p, parm, arg));
23886 case TREE_VEC:
23888 int i, len, argslen;
23889 int parm_variadic_p = 0;
23891 if (TREE_CODE (arg) != TREE_VEC)
23892 return unify_template_argument_mismatch (explain_p, parm, arg);
23894 len = TREE_VEC_LENGTH (parm);
23895 argslen = TREE_VEC_LENGTH (arg);
23897 /* Check for pack expansions in the parameters. */
23898 for (i = 0; i < len; ++i)
23900 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
23902 if (i == len - 1)
23903 /* We can unify against something with a trailing
23904 parameter pack. */
23905 parm_variadic_p = 1;
23906 else
23907 /* [temp.deduct.type]/9: If the template argument list of
23908 P contains a pack expansion that is not the last
23909 template argument, the entire template argument list
23910 is a non-deduced context. */
23911 return unify_success (explain_p);
23915 /* If we don't have enough arguments to satisfy the parameters
23916 (not counting the pack expression at the end), or we have
23917 too many arguments for a parameter list that doesn't end in
23918 a pack expression, we can't unify. */
23919 if (parm_variadic_p
23920 ? argslen < len - parm_variadic_p
23921 : argslen != len)
23922 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
23924 /* Unify all of the parameters that precede the (optional)
23925 pack expression. */
23926 for (i = 0; i < len - parm_variadic_p; ++i)
23928 RECUR_AND_CHECK_FAILURE (tparms, targs,
23929 TREE_VEC_ELT (parm, i),
23930 TREE_VEC_ELT (arg, i),
23931 UNIFY_ALLOW_NONE, explain_p);
23933 if (parm_variadic_p)
23934 return unify_pack_expansion (tparms, targs, parm, arg,
23935 DEDUCE_EXACT,
23936 /*subr=*/true, explain_p);
23937 return unify_success (explain_p);
23940 case RECORD_TYPE:
23941 case UNION_TYPE:
23942 if (TREE_CODE (arg) != TREE_CODE (parm))
23943 return unify_type_mismatch (explain_p, parm, arg);
23945 if (TYPE_PTRMEMFUNC_P (parm))
23947 if (!TYPE_PTRMEMFUNC_P (arg))
23948 return unify_type_mismatch (explain_p, parm, arg);
23950 return unify (tparms, targs,
23951 TYPE_PTRMEMFUNC_FN_TYPE (parm),
23952 TYPE_PTRMEMFUNC_FN_TYPE (arg),
23953 strict, explain_p);
23955 else if (TYPE_PTRMEMFUNC_P (arg))
23956 return unify_type_mismatch (explain_p, parm, arg);
23958 if (CLASSTYPE_TEMPLATE_INFO (parm))
23960 tree t = NULL_TREE;
23962 if (strict_in & UNIFY_ALLOW_DERIVED)
23964 /* First, we try to unify the PARM and ARG directly. */
23965 t = try_class_unification (tparms, targs,
23966 parm, arg, explain_p);
23968 if (!t)
23970 /* Fallback to the special case allowed in
23971 [temp.deduct.call]:
23973 If P is a class, and P has the form
23974 template-id, then A can be a derived class of
23975 the deduced A. Likewise, if P is a pointer to
23976 a class of the form template-id, A can be a
23977 pointer to a derived class pointed to by the
23978 deduced A. */
23979 enum template_base_result r;
23980 r = get_template_base (tparms, targs, parm, arg,
23981 explain_p, &t);
23983 if (!t)
23985 /* Don't give the derived diagnostic if we're
23986 already dealing with the same template. */
23987 bool same_template
23988 = (CLASSTYPE_TEMPLATE_INFO (arg)
23989 && (CLASSTYPE_TI_TEMPLATE (parm)
23990 == CLASSTYPE_TI_TEMPLATE (arg)));
23991 return unify_no_common_base (explain_p && !same_template,
23992 r, parm, arg);
23996 else if (CLASSTYPE_TEMPLATE_INFO (arg)
23997 && (CLASSTYPE_TI_TEMPLATE (parm)
23998 == CLASSTYPE_TI_TEMPLATE (arg)))
23999 /* Perhaps PARM is something like S<U> and ARG is S<int>.
24000 Then, we should unify `int' and `U'. */
24001 t = arg;
24002 else
24003 /* There's no chance of unification succeeding. */
24004 return unify_type_mismatch (explain_p, parm, arg);
24006 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
24007 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
24009 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
24010 return unify_type_mismatch (explain_p, parm, arg);
24011 return unify_success (explain_p);
24013 case METHOD_TYPE:
24014 case FUNCTION_TYPE:
24016 unsigned int nargs;
24017 tree *args;
24018 tree a;
24019 unsigned int i;
24021 if (TREE_CODE (arg) != TREE_CODE (parm))
24022 return unify_type_mismatch (explain_p, parm, arg);
24024 /* CV qualifications for methods can never be deduced, they must
24025 match exactly. We need to check them explicitly here,
24026 because type_unification_real treats them as any other
24027 cv-qualified parameter. */
24028 if (TREE_CODE (parm) == METHOD_TYPE
24029 && (!check_cv_quals_for_unify
24030 (UNIFY_ALLOW_NONE,
24031 class_of_this_parm (arg),
24032 class_of_this_parm (parm))))
24033 return unify_cv_qual_mismatch (explain_p, parm, arg);
24034 if (TREE_CODE (arg) == FUNCTION_TYPE
24035 && type_memfn_quals (parm) != type_memfn_quals (arg))
24036 return unify_cv_qual_mismatch (explain_p, parm, arg);
24037 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
24038 return unify_type_mismatch (explain_p, parm, arg);
24040 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
24041 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
24043 nargs = list_length (TYPE_ARG_TYPES (arg));
24044 args = XALLOCAVEC (tree, nargs);
24045 for (a = TYPE_ARG_TYPES (arg), i = 0;
24046 a != NULL_TREE && a != void_list_node;
24047 a = TREE_CHAIN (a), ++i)
24048 args[i] = TREE_VALUE (a);
24049 nargs = i;
24051 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
24052 args, nargs, 1, DEDUCE_EXACT,
24053 NULL, explain_p))
24054 return 1;
24056 if (flag_noexcept_type)
24058 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
24059 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
24060 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
24061 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
24062 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
24063 && uses_template_parms (TREE_PURPOSE (pspec)))
24064 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
24065 TREE_PURPOSE (aspec),
24066 UNIFY_ALLOW_NONE, explain_p);
24067 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
24068 return unify_type_mismatch (explain_p, parm, arg);
24071 return 0;
24074 case OFFSET_TYPE:
24075 /* Unify a pointer to member with a pointer to member function, which
24076 deduces the type of the member as a function type. */
24077 if (TYPE_PTRMEMFUNC_P (arg))
24079 /* Check top-level cv qualifiers */
24080 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
24081 return unify_cv_qual_mismatch (explain_p, parm, arg);
24083 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
24084 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
24085 UNIFY_ALLOW_NONE, explain_p);
24087 /* Determine the type of the function we are unifying against. */
24088 tree fntype = static_fn_type (arg);
24090 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
24093 if (TREE_CODE (arg) != OFFSET_TYPE)
24094 return unify_type_mismatch (explain_p, parm, arg);
24095 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
24096 TYPE_OFFSET_BASETYPE (arg),
24097 UNIFY_ALLOW_NONE, explain_p);
24098 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
24099 strict, explain_p);
24101 case CONST_DECL:
24102 if (DECL_TEMPLATE_PARM_P (parm))
24103 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
24104 if (arg != scalar_constant_value (parm))
24105 return unify_template_argument_mismatch (explain_p, parm, arg);
24106 return unify_success (explain_p);
24108 case FIELD_DECL:
24109 case TEMPLATE_DECL:
24110 /* Matched cases are handled by the ARG == PARM test above. */
24111 return unify_template_argument_mismatch (explain_p, parm, arg);
24113 case VAR_DECL:
24114 /* We might get a variable as a non-type template argument in parm if the
24115 corresponding parameter is type-dependent. Make any necessary
24116 adjustments based on whether arg is a reference. */
24117 if (CONSTANT_CLASS_P (arg))
24118 parm = fold_non_dependent_expr (parm, complain);
24119 else if (REFERENCE_REF_P (arg))
24121 tree sub = TREE_OPERAND (arg, 0);
24122 STRIP_NOPS (sub);
24123 if (TREE_CODE (sub) == ADDR_EXPR)
24124 arg = TREE_OPERAND (sub, 0);
24126 /* Now use the normal expression code to check whether they match. */
24127 goto expr;
24129 case TYPE_ARGUMENT_PACK:
24130 case NONTYPE_ARGUMENT_PACK:
24131 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
24132 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
24134 case TYPEOF_TYPE:
24135 case DECLTYPE_TYPE:
24136 case UNDERLYING_TYPE:
24137 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
24138 or UNDERLYING_TYPE nodes. */
24139 return unify_success (explain_p);
24141 case ERROR_MARK:
24142 /* Unification fails if we hit an error node. */
24143 return unify_invalid (explain_p);
24145 case INDIRECT_REF:
24146 if (REFERENCE_REF_P (parm))
24148 bool pexp = PACK_EXPANSION_P (arg);
24149 if (pexp)
24150 arg = PACK_EXPANSION_PATTERN (arg);
24151 if (REFERENCE_REF_P (arg))
24152 arg = TREE_OPERAND (arg, 0);
24153 if (pexp)
24154 arg = make_pack_expansion (arg, complain);
24155 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
24156 strict, explain_p);
24158 /* FALLTHRU */
24160 default:
24161 /* An unresolved overload is a nondeduced context. */
24162 if (is_overloaded_fn (parm) || type_unknown_p (parm))
24163 return unify_success (explain_p);
24164 gcc_assert (EXPR_P (parm)
24165 || COMPOUND_LITERAL_P (parm)
24166 || TREE_CODE (parm) == TRAIT_EXPR);
24167 expr:
24168 /* We must be looking at an expression. This can happen with
24169 something like:
24171 template <int I>
24172 void foo(S<I>, S<I + 2>);
24176 template<typename T>
24177 void foo(A<T, T{}>);
24179 This is a "non-deduced context":
24181 [deduct.type]
24183 The non-deduced contexts are:
24185 --A non-type template argument or an array bound in which
24186 a subexpression references a template parameter.
24188 In these cases, we assume deduction succeeded, but don't
24189 actually infer any unifications. */
24191 if (!uses_template_parms (parm)
24192 && !template_args_equal (parm, arg))
24193 return unify_expression_unequal (explain_p, parm, arg);
24194 else
24195 return unify_success (explain_p);
24198 #undef RECUR_AND_CHECK_FAILURE
24200 /* Note that DECL can be defined in this translation unit, if
24201 required. */
24203 static void
24204 mark_definable (tree decl)
24206 tree clone;
24207 DECL_NOT_REALLY_EXTERN (decl) = 1;
24208 FOR_EACH_CLONE (clone, decl)
24209 DECL_NOT_REALLY_EXTERN (clone) = 1;
24212 /* Called if RESULT is explicitly instantiated, or is a member of an
24213 explicitly instantiated class. */
24215 void
24216 mark_decl_instantiated (tree result, int extern_p)
24218 SET_DECL_EXPLICIT_INSTANTIATION (result);
24220 /* If this entity has already been written out, it's too late to
24221 make any modifications. */
24222 if (TREE_ASM_WRITTEN (result))
24223 return;
24225 /* consteval functions are never emitted. */
24226 if (TREE_CODE (result) == FUNCTION_DECL
24227 && DECL_IMMEDIATE_FUNCTION_P (result))
24228 return;
24230 /* For anonymous namespace we don't need to do anything. */
24231 if (decl_anon_ns_mem_p (result))
24233 gcc_assert (!TREE_PUBLIC (result));
24234 return;
24237 if (TREE_CODE (result) != FUNCTION_DECL)
24238 /* The TREE_PUBLIC flag for function declarations will have been
24239 set correctly by tsubst. */
24240 TREE_PUBLIC (result) = 1;
24242 /* This might have been set by an earlier implicit instantiation. */
24243 DECL_COMDAT (result) = 0;
24245 if (extern_p)
24247 DECL_EXTERNAL (result) = 1;
24248 DECL_NOT_REALLY_EXTERN (result) = 0;
24250 else
24252 mark_definable (result);
24253 mark_needed (result);
24254 /* Always make artificials weak. */
24255 if (DECL_ARTIFICIAL (result) && flag_weak)
24256 comdat_linkage (result);
24257 /* For WIN32 we also want to put explicit instantiations in
24258 linkonce sections. */
24259 else if (TREE_PUBLIC (result))
24260 maybe_make_one_only (result);
24261 if (TREE_CODE (result) == FUNCTION_DECL
24262 && DECL_TEMPLATE_INSTANTIATED (result))
24263 /* If the function has already been instantiated, clear DECL_EXTERNAL,
24264 since start_preparsed_function wouldn't have if we had an earlier
24265 extern explicit instantiation. */
24266 DECL_EXTERNAL (result) = 0;
24269 /* If EXTERN_P, then this function will not be emitted -- unless
24270 followed by an explicit instantiation, at which point its linkage
24271 will be adjusted. If !EXTERN_P, then this function will be
24272 emitted here. In neither circumstance do we want
24273 import_export_decl to adjust the linkage. */
24274 DECL_INTERFACE_KNOWN (result) = 1;
24277 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
24278 important template arguments. If any are missing, we check whether
24279 they're important by using error_mark_node for substituting into any
24280 args that were used for partial ordering (the ones between ARGS and END)
24281 and seeing if it bubbles up. */
24283 static bool
24284 check_undeduced_parms (tree targs, tree args, tree end)
24286 bool found = false;
24287 int i;
24288 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
24289 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
24291 found = true;
24292 TREE_VEC_ELT (targs, i) = error_mark_node;
24294 if (found)
24296 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
24297 if (substed == error_mark_node)
24298 return true;
24300 return false;
24303 /* Given two function templates PAT1 and PAT2, return:
24305 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
24306 -1 if PAT2 is more specialized than PAT1.
24307 0 if neither is more specialized.
24309 LEN indicates the number of parameters we should consider
24310 (defaulted parameters should not be considered).
24312 The 1998 std underspecified function template partial ordering, and
24313 DR214 addresses the issue. We take pairs of arguments, one from
24314 each of the templates, and deduce them against each other. One of
24315 the templates will be more specialized if all the *other*
24316 template's arguments deduce against its arguments and at least one
24317 of its arguments *does* *not* deduce against the other template's
24318 corresponding argument. Deduction is done as for class templates.
24319 The arguments used in deduction have reference and top level cv
24320 qualifiers removed. Iff both arguments were originally reference
24321 types *and* deduction succeeds in both directions, an lvalue reference
24322 wins against an rvalue reference and otherwise the template
24323 with the more cv-qualified argument wins for that pairing (if
24324 neither is more cv-qualified, they both are equal). Unlike regular
24325 deduction, after all the arguments have been deduced in this way,
24326 we do *not* verify the deduced template argument values can be
24327 substituted into non-deduced contexts.
24329 The logic can be a bit confusing here, because we look at deduce1 and
24330 targs1 to see if pat2 is at least as specialized, and vice versa; if we
24331 can find template arguments for pat1 to make arg1 look like arg2, that
24332 means that arg2 is at least as specialized as arg1. */
24335 more_specialized_fn (tree pat1, tree pat2, int len)
24337 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
24338 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
24339 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
24340 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
24341 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
24342 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
24343 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
24344 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
24345 tree origs1, origs2;
24346 bool lose1 = false;
24347 bool lose2 = false;
24349 /* Remove the this parameter from non-static member functions. If
24350 one is a non-static member function and the other is not a static
24351 member function, remove the first parameter from that function
24352 also. This situation occurs for operator functions where we
24353 locate both a member function (with this pointer) and non-member
24354 operator (with explicit first operand). */
24355 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
24357 len--; /* LEN is the number of significant arguments for DECL1 */
24358 args1 = TREE_CHAIN (args1);
24359 if (!DECL_STATIC_FUNCTION_P (decl2))
24360 args2 = TREE_CHAIN (args2);
24362 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
24364 args2 = TREE_CHAIN (args2);
24365 if (!DECL_STATIC_FUNCTION_P (decl1))
24367 len--;
24368 args1 = TREE_CHAIN (args1);
24372 /* If only one is a conversion operator, they are unordered. */
24373 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
24374 return 0;
24376 /* Consider the return type for a conversion function */
24377 if (DECL_CONV_FN_P (decl1))
24379 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
24380 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
24381 len++;
24384 processing_template_decl++;
24386 origs1 = args1;
24387 origs2 = args2;
24389 while (len--
24390 /* Stop when an ellipsis is seen. */
24391 && args1 != NULL_TREE && args2 != NULL_TREE)
24393 tree arg1 = TREE_VALUE (args1);
24394 tree arg2 = TREE_VALUE (args2);
24395 int deduce1, deduce2;
24396 int quals1 = -1;
24397 int quals2 = -1;
24398 int ref1 = 0;
24399 int ref2 = 0;
24401 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
24402 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
24404 /* When both arguments are pack expansions, we need only
24405 unify the patterns themselves. */
24406 arg1 = PACK_EXPANSION_PATTERN (arg1);
24407 arg2 = PACK_EXPANSION_PATTERN (arg2);
24409 /* This is the last comparison we need to do. */
24410 len = 0;
24413 if (TYPE_REF_P (arg1))
24415 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
24416 arg1 = TREE_TYPE (arg1);
24417 quals1 = cp_type_quals (arg1);
24420 if (TYPE_REF_P (arg2))
24422 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
24423 arg2 = TREE_TYPE (arg2);
24424 quals2 = cp_type_quals (arg2);
24427 arg1 = TYPE_MAIN_VARIANT (arg1);
24428 arg2 = TYPE_MAIN_VARIANT (arg2);
24430 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
24432 int i, len2 = remaining_arguments (args2);
24433 tree parmvec = make_tree_vec (1);
24434 tree argvec = make_tree_vec (len2);
24435 tree ta = args2;
24437 /* Setup the parameter vector, which contains only ARG1. */
24438 TREE_VEC_ELT (parmvec, 0) = arg1;
24440 /* Setup the argument vector, which contains the remaining
24441 arguments. */
24442 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
24443 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
24445 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
24446 argvec, DEDUCE_EXACT,
24447 /*subr=*/true, /*explain_p=*/false)
24448 == 0);
24450 /* We cannot deduce in the other direction, because ARG1 is
24451 a pack expansion but ARG2 is not. */
24452 deduce2 = 0;
24454 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
24456 int i, len1 = remaining_arguments (args1);
24457 tree parmvec = make_tree_vec (1);
24458 tree argvec = make_tree_vec (len1);
24459 tree ta = args1;
24461 /* Setup the parameter vector, which contains only ARG1. */
24462 TREE_VEC_ELT (parmvec, 0) = arg2;
24464 /* Setup the argument vector, which contains the remaining
24465 arguments. */
24466 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
24467 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
24469 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
24470 argvec, DEDUCE_EXACT,
24471 /*subr=*/true, /*explain_p=*/false)
24472 == 0);
24474 /* We cannot deduce in the other direction, because ARG2 is
24475 a pack expansion but ARG1 is not.*/
24476 deduce1 = 0;
24479 else
24481 /* The normal case, where neither argument is a pack
24482 expansion. */
24483 deduce1 = (unify (tparms1, targs1, arg1, arg2,
24484 UNIFY_ALLOW_NONE, /*explain_p=*/false)
24485 == 0);
24486 deduce2 = (unify (tparms2, targs2, arg2, arg1,
24487 UNIFY_ALLOW_NONE, /*explain_p=*/false)
24488 == 0);
24491 /* If we couldn't deduce arguments for tparms1 to make arg1 match
24492 arg2, then arg2 is not as specialized as arg1. */
24493 if (!deduce1)
24494 lose2 = true;
24495 if (!deduce2)
24496 lose1 = true;
24498 /* "If, for a given type, deduction succeeds in both directions
24499 (i.e., the types are identical after the transformations above)
24500 and both P and A were reference types (before being replaced with
24501 the type referred to above):
24502 - if the type from the argument template was an lvalue reference and
24503 the type from the parameter template was not, the argument type is
24504 considered to be more specialized than the other; otherwise,
24505 - if the type from the argument template is more cv-qualified
24506 than the type from the parameter template (as described above),
24507 the argument type is considered to be more specialized than the other;
24508 otherwise,
24509 - neither type is more specialized than the other." */
24511 if (deduce1 && deduce2)
24513 if (ref1 && ref2 && ref1 != ref2)
24515 if (ref1 > ref2)
24516 lose1 = true;
24517 else
24518 lose2 = true;
24520 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
24522 if ((quals1 & quals2) == quals2)
24523 lose2 = true;
24524 if ((quals1 & quals2) == quals1)
24525 lose1 = true;
24529 if (lose1 && lose2)
24530 /* We've failed to deduce something in either direction.
24531 These must be unordered. */
24532 break;
24534 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
24535 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
24536 /* We have already processed all of the arguments in our
24537 handing of the pack expansion type. */
24538 len = 0;
24540 args1 = TREE_CHAIN (args1);
24541 args2 = TREE_CHAIN (args2);
24544 /* "In most cases, all template parameters must have values in order for
24545 deduction to succeed, but for partial ordering purposes a template
24546 parameter may remain without a value provided it is not used in the
24547 types being used for partial ordering."
24549 Thus, if we are missing any of the targs1 we need to substitute into
24550 origs1, then pat2 is not as specialized as pat1. This can happen when
24551 there is a nondeduced context. */
24552 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
24553 lose2 = true;
24554 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
24555 lose1 = true;
24557 processing_template_decl--;
24559 /* If both deductions succeed, the partial ordering selects the more
24560 constrained template. */
24561 /* P2113: If the corresponding template-parameters of the
24562 template-parameter-lists are not equivalent ([temp.over.link]) or if
24563 the function parameters that positionally correspond between the two
24564 templates are not of the same type, neither template is more
24565 specialized than the other. */
24566 if (!lose1 && !lose2
24567 && comp_template_parms (DECL_TEMPLATE_PARMS (pat1),
24568 DECL_TEMPLATE_PARMS (pat2))
24569 && compparms (origs1, origs2))
24571 int winner = more_constrained (decl1, decl2);
24572 if (winner > 0)
24573 lose2 = true;
24574 else if (winner < 0)
24575 lose1 = true;
24578 /* All things being equal, if the next argument is a pack expansion
24579 for one function but not for the other, prefer the
24580 non-variadic function. FIXME this is bogus; see c++/41958. */
24581 if (lose1 == lose2
24582 && args1 && TREE_VALUE (args1)
24583 && args2 && TREE_VALUE (args2))
24585 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
24586 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
24589 if (lose1 == lose2)
24590 return 0;
24591 else if (!lose1)
24592 return 1;
24593 else
24594 return -1;
24597 /* Determine which of two partial specializations of TMPL is more
24598 specialized.
24600 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
24601 to the first partial specialization. The TREE_PURPOSE is the
24602 innermost set of template parameters for the partial
24603 specialization. PAT2 is similar, but for the second template.
24605 Return 1 if the first partial specialization is more specialized;
24606 -1 if the second is more specialized; 0 if neither is more
24607 specialized.
24609 See [temp.class.order] for information about determining which of
24610 two templates is more specialized. */
24612 static int
24613 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
24615 tree targs;
24616 int winner = 0;
24617 bool any_deductions = false;
24619 tree tmpl1 = TREE_VALUE (pat1);
24620 tree tmpl2 = TREE_VALUE (pat2);
24621 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
24622 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
24624 /* Just like what happens for functions, if we are ordering between
24625 different template specializations, we may encounter dependent
24626 types in the arguments, and we need our dependency check functions
24627 to behave correctly. */
24628 ++processing_template_decl;
24629 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
24630 if (targs)
24632 --winner;
24633 any_deductions = true;
24636 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
24637 if (targs)
24639 ++winner;
24640 any_deductions = true;
24642 --processing_template_decl;
24644 /* If both deductions succeed, the partial ordering selects the more
24645 constrained template. */
24646 if (!winner && any_deductions)
24647 winner = more_constrained (tmpl1, tmpl2);
24649 /* In the case of a tie where at least one of the templates
24650 has a parameter pack at the end, the template with the most
24651 non-packed parameters wins. */
24652 if (winner == 0
24653 && any_deductions
24654 && (template_args_variadic_p (TREE_PURPOSE (pat1))
24655 || template_args_variadic_p (TREE_PURPOSE (pat2))))
24657 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
24658 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
24659 int len1 = TREE_VEC_LENGTH (args1);
24660 int len2 = TREE_VEC_LENGTH (args2);
24662 /* We don't count the pack expansion at the end. */
24663 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
24664 --len1;
24665 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
24666 --len2;
24668 if (len1 > len2)
24669 return 1;
24670 else if (len1 < len2)
24671 return -1;
24674 return winner;
24677 /* Return the template arguments that will produce the function signature
24678 DECL from the function template FN, with the explicit template
24679 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
24680 also match. Return NULL_TREE if no satisfactory arguments could be
24681 found. */
24683 static tree
24684 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
24686 int ntparms = DECL_NTPARMS (fn);
24687 tree targs = make_tree_vec (ntparms);
24688 tree decl_type = TREE_TYPE (decl);
24689 tree decl_arg_types;
24690 tree *args;
24691 unsigned int nargs, ix;
24692 tree arg;
24694 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
24696 /* Never do unification on the 'this' parameter. */
24697 decl_arg_types = skip_artificial_parms_for (decl,
24698 TYPE_ARG_TYPES (decl_type));
24700 nargs = list_length (decl_arg_types);
24701 args = XALLOCAVEC (tree, nargs);
24702 for (arg = decl_arg_types, ix = 0;
24703 arg != NULL_TREE && arg != void_list_node;
24704 arg = TREE_CHAIN (arg), ++ix)
24705 args[ix] = TREE_VALUE (arg);
24707 if (fn_type_unification (fn, explicit_args, targs,
24708 args, ix,
24709 (check_rettype || DECL_CONV_FN_P (fn)
24710 ? TREE_TYPE (decl_type) : NULL_TREE),
24711 DEDUCE_EXACT, LOOKUP_NORMAL, NULL,
24712 /*explain_p=*/false,
24713 /*decltype*/false)
24714 == error_mark_node)
24715 return NULL_TREE;
24717 return targs;
24720 /* Return the innermost template arguments that, when applied to a partial
24721 specialization SPEC_TMPL of TMPL, yield the ARGS.
24723 For example, suppose we have:
24725 template <class T, class U> struct S {};
24726 template <class T> struct S<T*, int> {};
24728 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
24729 partial specialization and the ARGS will be {double*, int}. The resulting
24730 vector will be {double}, indicating that `T' is bound to `double'. */
24732 static tree
24733 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
24735 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
24736 tree spec_args
24737 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
24738 int i, ntparms = TREE_VEC_LENGTH (tparms);
24739 tree deduced_args;
24740 tree innermost_deduced_args;
24742 innermost_deduced_args = make_tree_vec (ntparms);
24743 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
24745 deduced_args = copy_node (args);
24746 SET_TMPL_ARGS_LEVEL (deduced_args,
24747 TMPL_ARGS_DEPTH (deduced_args),
24748 innermost_deduced_args);
24750 else
24751 deduced_args = innermost_deduced_args;
24753 bool tried_array_deduction = (cxx_dialect < cxx17);
24754 again:
24755 if (unify (tparms, deduced_args,
24756 INNERMOST_TEMPLATE_ARGS (spec_args),
24757 INNERMOST_TEMPLATE_ARGS (args),
24758 UNIFY_ALLOW_NONE, /*explain_p=*/false))
24759 return NULL_TREE;
24761 for (i = 0; i < ntparms; ++i)
24762 if (! TREE_VEC_ELT (innermost_deduced_args, i))
24764 if (!tried_array_deduction)
24766 try_array_deduction (tparms, innermost_deduced_args,
24767 INNERMOST_TEMPLATE_ARGS (spec_args));
24768 tried_array_deduction = true;
24769 if (TREE_VEC_ELT (innermost_deduced_args, i))
24770 goto again;
24772 return NULL_TREE;
24775 if (!push_tinst_level (spec_tmpl, deduced_args))
24777 excessive_deduction_depth = true;
24778 return NULL_TREE;
24781 /* Verify that nondeduced template arguments agree with the type
24782 obtained from argument deduction.
24784 For example:
24786 struct A { typedef int X; };
24787 template <class T, class U> struct C {};
24788 template <class T> struct C<T, typename T::X> {};
24790 Then with the instantiation `C<A, int>', we can deduce that
24791 `T' is `A' but unify () does not check whether `typename T::X'
24792 is `int'. */
24793 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
24795 if (spec_args != error_mark_node)
24796 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
24797 INNERMOST_TEMPLATE_ARGS (spec_args),
24798 tmpl, tf_none, false, false);
24800 pop_tinst_level ();
24802 if (spec_args == error_mark_node
24803 /* We only need to check the innermost arguments; the other
24804 arguments will always agree. */
24805 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
24806 INNERMOST_TEMPLATE_ARGS (args)))
24807 return NULL_TREE;
24809 /* Now that we have bindings for all of the template arguments,
24810 ensure that the arguments deduced for the template template
24811 parameters have compatible template parameter lists. See the use
24812 of template_template_parm_bindings_ok_p in fn_type_unification
24813 for more information. */
24814 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
24815 return NULL_TREE;
24817 return deduced_args;
24820 // Compare two function templates T1 and T2 by deducing bindings
24821 // from one against the other. If both deductions succeed, compare
24822 // constraints to see which is more constrained.
24823 static int
24824 more_specialized_inst (tree t1, tree t2)
24826 int fate = 0;
24827 int count = 0;
24829 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
24831 --fate;
24832 ++count;
24835 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
24837 ++fate;
24838 ++count;
24841 // If both deductions succeed, then one may be more constrained.
24842 if (count == 2 && fate == 0)
24843 fate = more_constrained (t1, t2);
24845 return fate;
24848 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
24849 Return the TREE_LIST node with the most specialized template, if
24850 any. If there is no most specialized template, the error_mark_node
24851 is returned.
24853 Note that this function does not look at, or modify, the
24854 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
24855 returned is one of the elements of INSTANTIATIONS, callers may
24856 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
24857 and retrieve it from the value returned. */
24859 tree
24860 most_specialized_instantiation (tree templates)
24862 tree fn, champ;
24864 ++processing_template_decl;
24866 champ = templates;
24867 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
24869 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
24870 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
24871 if (fate == -1)
24872 champ = fn;
24873 else if (!fate)
24875 /* Equally specialized, move to next function. If there
24876 is no next function, nothing's most specialized. */
24877 fn = TREE_CHAIN (fn);
24878 champ = fn;
24879 if (!fn)
24880 break;
24884 if (champ)
24885 /* Now verify that champ is better than everything earlier in the
24886 instantiation list. */
24887 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
24888 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
24890 champ = NULL_TREE;
24891 break;
24895 processing_template_decl--;
24897 if (!champ)
24898 return error_mark_node;
24900 return champ;
24903 /* If DECL is a specialization of some template, return the most
24904 general such template. Otherwise, returns NULL_TREE.
24906 For example, given:
24908 template <class T> struct S { template <class U> void f(U); };
24910 if TMPL is `template <class U> void S<int>::f(U)' this will return
24911 the full template. This function will not trace past partial
24912 specializations, however. For example, given in addition:
24914 template <class T> struct S<T*> { template <class U> void f(U); };
24916 if TMPL is `template <class U> void S<int*>::f(U)' this will return
24917 `template <class T> template <class U> S<T*>::f(U)'. */
24919 tree
24920 most_general_template (tree decl)
24922 if (TREE_CODE (decl) != TEMPLATE_DECL)
24924 if (tree tinfo = get_template_info (decl))
24925 decl = TI_TEMPLATE (tinfo);
24926 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
24927 template friend, or a FIELD_DECL for a capture pack. */
24928 if (TREE_CODE (decl) != TEMPLATE_DECL)
24929 return NULL_TREE;
24932 /* Look for more and more general templates. */
24933 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
24935 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
24936 (See cp-tree.h for details.) */
24937 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
24938 break;
24940 if (CLASS_TYPE_P (TREE_TYPE (decl))
24941 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
24942 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
24943 break;
24945 /* Stop if we run into an explicitly specialized class template. */
24946 if (!DECL_NAMESPACE_SCOPE_P (decl)
24947 && DECL_CONTEXT (decl)
24948 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
24949 break;
24951 decl = DECL_TI_TEMPLATE (decl);
24954 return decl;
24957 /* Return the most specialized of the template partial specializations
24958 which can produce TARGET, a specialization of some class or variable
24959 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
24960 a TEMPLATE_DECL node corresponding to the partial specialization, while
24961 the TREE_PURPOSE is the set of template arguments that must be
24962 substituted into the template pattern in order to generate TARGET.
24964 If the choice of partial specialization is ambiguous, a diagnostic
24965 is issued, and the error_mark_node is returned. If there are no
24966 partial specializations matching TARGET, then NULL_TREE is
24967 returned, indicating that the primary template should be used. */
24969 tree
24970 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
24972 tree list = NULL_TREE;
24973 tree t;
24974 tree champ;
24975 int fate;
24976 bool ambiguous_p;
24977 tree outer_args = NULL_TREE;
24978 tree tmpl, args;
24980 if (TYPE_P (target))
24982 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
24983 tmpl = TI_TEMPLATE (tinfo);
24984 args = TI_ARGS (tinfo);
24986 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
24988 tmpl = TREE_OPERAND (target, 0);
24989 args = TREE_OPERAND (target, 1);
24991 else if (VAR_P (target))
24993 tree tinfo = DECL_TEMPLATE_INFO (target);
24994 tmpl = TI_TEMPLATE (tinfo);
24995 args = TI_ARGS (tinfo);
24997 else
24998 gcc_unreachable ();
25000 tree main_tmpl = most_general_template (tmpl);
25002 /* For determining which partial specialization to use, only the
25003 innermost args are interesting. */
25004 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
25006 outer_args = strip_innermost_template_args (args, 1);
25007 args = INNERMOST_TEMPLATE_ARGS (args);
25010 /* The caller hasn't called push_to_top_level yet, but we need
25011 get_partial_spec_bindings to be done in non-template context so that we'll
25012 fully resolve everything. */
25013 processing_template_decl_sentinel ptds;
25015 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
25017 const tree ospec_tmpl = TREE_VALUE (t);
25019 tree spec_tmpl;
25020 if (outer_args)
25022 /* Substitute in the template args from the enclosing class. */
25023 ++processing_template_decl;
25024 spec_tmpl = tsubst (ospec_tmpl, outer_args, tf_none, NULL_TREE);
25025 --processing_template_decl;
25026 if (spec_tmpl == error_mark_node)
25027 return error_mark_node;
25029 else
25030 spec_tmpl = ospec_tmpl;
25032 tree spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
25033 if (spec_args)
25035 if (outer_args)
25036 spec_args = add_to_template_args (outer_args, spec_args);
25038 /* Keep the candidate only if the constraints are satisfied,
25039 or if we're not compiling with concepts. */
25040 if (!flag_concepts
25041 || constraints_satisfied_p (ospec_tmpl, spec_args))
25043 list = tree_cons (spec_args, ospec_tmpl, list);
25044 TREE_TYPE (list) = TREE_TYPE (t);
25049 if (! list)
25050 return NULL_TREE;
25052 ambiguous_p = false;
25053 t = list;
25054 champ = t;
25055 t = TREE_CHAIN (t);
25056 for (; t; t = TREE_CHAIN (t))
25058 fate = more_specialized_partial_spec (tmpl, champ, t);
25059 if (fate == 1)
25061 else
25063 if (fate == 0)
25065 t = TREE_CHAIN (t);
25066 if (! t)
25068 ambiguous_p = true;
25069 break;
25072 champ = t;
25076 if (!ambiguous_p)
25077 for (t = list; t && t != champ; t = TREE_CHAIN (t))
25079 fate = more_specialized_partial_spec (tmpl, champ, t);
25080 if (fate != 1)
25082 ambiguous_p = true;
25083 break;
25087 if (ambiguous_p)
25089 const char *str;
25090 char *spaces = NULL;
25091 if (!(complain & tf_error))
25092 return error_mark_node;
25093 if (TYPE_P (target))
25094 error ("ambiguous template instantiation for %q#T", target);
25095 else
25096 error ("ambiguous template instantiation for %q#D", target);
25097 str = ngettext ("candidate is:", "candidates are:", list_length (list));
25098 for (t = list; t; t = TREE_CHAIN (t))
25100 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
25101 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
25102 "%s %#qS", spaces ? spaces : str, subst);
25103 spaces = spaces ? spaces : get_spaces (str);
25105 free (spaces);
25106 return error_mark_node;
25109 return champ;
25112 /* Explicitly instantiate DECL. */
25114 void
25115 do_decl_instantiation (tree decl, tree storage)
25117 tree result = NULL_TREE;
25118 int extern_p = 0;
25120 if (!decl || decl == error_mark_node)
25121 /* An error occurred, for which grokdeclarator has already issued
25122 an appropriate message. */
25123 return;
25124 else if (! DECL_LANG_SPECIFIC (decl))
25126 error ("explicit instantiation of non-template %q#D", decl);
25127 return;
25129 else if (DECL_DECLARED_CONCEPT_P (decl))
25131 if (VAR_P (decl))
25132 error ("explicit instantiation of variable concept %q#D", decl);
25133 else
25134 error ("explicit instantiation of function concept %q#D", decl);
25135 return;
25138 bool var_templ = (DECL_TEMPLATE_INFO (decl)
25139 && variable_template_p (DECL_TI_TEMPLATE (decl)));
25141 if (VAR_P (decl) && !var_templ)
25143 /* There is an asymmetry here in the way VAR_DECLs and
25144 FUNCTION_DECLs are handled by grokdeclarator. In the case of
25145 the latter, the DECL we get back will be marked as a
25146 template instantiation, and the appropriate
25147 DECL_TEMPLATE_INFO will be set up. This does not happen for
25148 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
25149 should handle VAR_DECLs as it currently handles
25150 FUNCTION_DECLs. */
25151 if (!DECL_CLASS_SCOPE_P (decl))
25153 error ("%qD is not a static data member of a class template", decl);
25154 return;
25156 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
25157 if (!result || !VAR_P (result))
25159 error ("no matching template for %qD found", decl);
25160 return;
25162 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
25164 error ("type %qT for explicit instantiation %qD does not match "
25165 "declared type %qT", TREE_TYPE (result), decl,
25166 TREE_TYPE (decl));
25167 return;
25170 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
25172 error ("explicit instantiation of %q#D", decl);
25173 return;
25175 else
25176 result = decl;
25178 /* Check for various error cases. Note that if the explicit
25179 instantiation is valid the RESULT will currently be marked as an
25180 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
25181 until we get here. */
25183 if (DECL_TEMPLATE_SPECIALIZATION (result))
25185 /* DR 259 [temp.spec].
25187 Both an explicit instantiation and a declaration of an explicit
25188 specialization shall not appear in a program unless the explicit
25189 instantiation follows a declaration of the explicit specialization.
25191 For a given set of template parameters, if an explicit
25192 instantiation of a template appears after a declaration of an
25193 explicit specialization for that template, the explicit
25194 instantiation has no effect. */
25195 return;
25197 else if (DECL_EXPLICIT_INSTANTIATION (result))
25199 /* [temp.spec]
25201 No program shall explicitly instantiate any template more
25202 than once.
25204 We check DECL_NOT_REALLY_EXTERN so as not to complain when
25205 the first instantiation was `extern' and the second is not,
25206 and EXTERN_P for the opposite case. */
25207 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
25208 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
25209 /* If an "extern" explicit instantiation follows an ordinary
25210 explicit instantiation, the template is instantiated. */
25211 if (extern_p)
25212 return;
25214 else if (!DECL_IMPLICIT_INSTANTIATION (result))
25216 error ("no matching template for %qD found", result);
25217 return;
25219 else if (!DECL_TEMPLATE_INFO (result))
25221 permerror (input_location, "explicit instantiation of non-template %q#D", result);
25222 return;
25225 if (storage == NULL_TREE)
25227 else if (storage == ridpointers[(int) RID_EXTERN])
25229 if (cxx_dialect == cxx98)
25230 pedwarn (input_location, OPT_Wpedantic,
25231 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
25232 "instantiations");
25233 extern_p = 1;
25235 else
25236 error ("storage class %qD applied to template instantiation", storage);
25238 check_explicit_instantiation_namespace (result);
25239 mark_decl_instantiated (result, extern_p);
25240 if (! extern_p)
25241 instantiate_decl (result, /*defer_ok=*/true,
25242 /*expl_inst_class_mem_p=*/false);
25245 static void
25246 mark_class_instantiated (tree t, int extern_p)
25248 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
25249 SET_CLASSTYPE_INTERFACE_KNOWN (t);
25250 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
25251 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
25252 if (! extern_p)
25254 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
25255 rest_of_type_compilation (t, 1);
25259 /* Perform an explicit instantiation of template class T. STORAGE, if
25260 non-null, is the RID for extern, inline or static. COMPLAIN is
25261 nonzero if this is called from the parser, zero if called recursively,
25262 since the standard is unclear (as detailed below). */
25264 void
25265 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
25267 if (!(CLASS_TYPE_P (t) && CLASSTYPE_TEMPLATE_INFO (t)))
25269 if (tree ti = TYPE_TEMPLATE_INFO (t))
25270 error ("explicit instantiation of non-class template %qD",
25271 TI_TEMPLATE (ti));
25272 else
25273 error ("explicit instantiation of non-template type %qT", t);
25274 return;
25277 complete_type (t);
25279 if (!COMPLETE_TYPE_P (t))
25281 if (complain & tf_error)
25282 error ("explicit instantiation of %q#T before definition of template",
25284 return;
25287 /* At most one of these will be true. */
25288 bool extern_p = false;
25289 bool nomem_p = false;
25290 bool static_p = false;
25292 if (storage != NULL_TREE)
25294 if (storage == ridpointers[(int) RID_EXTERN])
25296 if (cxx_dialect == cxx98)
25297 pedwarn (input_location, OPT_Wpedantic,
25298 "ISO C++ 1998 forbids the use of %<extern%> on "
25299 "explicit instantiations");
25301 else
25302 pedwarn (input_location, OPT_Wpedantic,
25303 "ISO C++ forbids the use of %qE"
25304 " on explicit instantiations", storage);
25306 if (storage == ridpointers[(int) RID_INLINE])
25307 nomem_p = true;
25308 else if (storage == ridpointers[(int) RID_EXTERN])
25309 extern_p = true;
25310 else if (storage == ridpointers[(int) RID_STATIC])
25311 static_p = true;
25312 else
25313 error ("storage class %qD applied to template instantiation",
25314 storage);
25317 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
25318 /* DR 259 [temp.spec].
25320 Both an explicit instantiation and a declaration of an explicit
25321 specialization shall not appear in a program unless the
25322 explicit instantiation follows a declaration of the explicit
25323 specialization.
25325 For a given set of template parameters, if an explicit
25326 instantiation of a template appears after a declaration of an
25327 explicit specialization for that template, the explicit
25328 instantiation has no effect. */
25329 return;
25331 if (CLASSTYPE_EXPLICIT_INSTANTIATION (t) && !CLASSTYPE_INTERFACE_ONLY (t))
25333 /* We've already instantiated the template. */
25335 /* [temp.spec]
25337 No program shall explicitly instantiate any template more
25338 than once.
25340 If EXTERN_P then this is ok. */
25341 if (!extern_p && (complain & tf_error))
25342 permerror (input_location,
25343 "duplicate explicit instantiation of %q#T", t);
25345 return;
25348 check_explicit_instantiation_namespace (TYPE_NAME (t));
25349 mark_class_instantiated (t, extern_p);
25351 if (nomem_p)
25352 return;
25354 /* In contrast to implicit instantiation, where only the
25355 declarations, and not the definitions, of members are
25356 instantiated, we have here:
25358 [temp.explicit]
25360 An explicit instantiation that names a class template
25361 specialization is also an explicit instantiation of the same
25362 kind (declaration or definition) of each of its members (not
25363 including members inherited from base classes and members
25364 that are templates) that has not been previously explicitly
25365 specialized in the translation unit containing the explicit
25366 instantiation, provided that the associated constraints, if
25367 any, of that member are satisfied by the template arguments
25368 of the explicit instantiation. */
25369 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
25370 if ((VAR_P (fld)
25371 || (TREE_CODE (fld) == FUNCTION_DECL
25372 && !static_p
25373 && user_provided_p (fld)))
25374 && DECL_TEMPLATE_INSTANTIATION (fld)
25375 && constraints_satisfied_p (fld))
25377 mark_decl_instantiated (fld, extern_p);
25378 if (! extern_p)
25379 instantiate_decl (fld, /*defer_ok=*/true,
25380 /*expl_inst_class_mem_p=*/true);
25382 else if (DECL_IMPLICIT_TYPEDEF_P (fld))
25384 tree type = TREE_TYPE (fld);
25386 if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
25387 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
25388 do_type_instantiation (type, storage, 0);
25392 /* Given a function DECL, which is a specialization of TMPL, modify
25393 DECL to be a re-instantiation of TMPL with the same template
25394 arguments. TMPL should be the template into which tsubst'ing
25395 should occur for DECL, not the most general template.
25397 One reason for doing this is a scenario like this:
25399 template <class T>
25400 void f(const T&, int i);
25402 void g() { f(3, 7); }
25404 template <class T>
25405 void f(const T& t, const int i) { }
25407 Note that when the template is first instantiated, with
25408 instantiate_template, the resulting DECL will have no name for the
25409 first parameter, and the wrong type for the second. So, when we go
25410 to instantiate the DECL, we regenerate it. */
25412 static void
25413 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
25415 /* The arguments used to instantiate DECL, from the most general
25416 template. */
25417 tree code_pattern;
25419 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
25421 /* Make sure that we can see identifiers, and compute access
25422 correctly. */
25423 push_access_scope (decl);
25425 if (TREE_CODE (decl) == FUNCTION_DECL)
25427 tree specs;
25428 int args_depth;
25429 int parms_depth;
25431 /* Use the source location of the definition. */
25432 DECL_SOURCE_LOCATION (decl) = DECL_SOURCE_LOCATION (tmpl);
25434 args_depth = TMPL_ARGS_DEPTH (args);
25435 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
25436 if (args_depth > parms_depth)
25437 args = get_innermost_template_args (args, parms_depth);
25439 /* Instantiate a dynamic exception-specification. noexcept will be
25440 handled below. */
25441 if (tree raises = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (code_pattern)))
25442 if (TREE_VALUE (raises))
25444 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
25445 args, tf_error, NULL_TREE,
25446 /*defer_ok*/false);
25447 if (specs && specs != error_mark_node)
25448 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
25449 specs);
25452 /* Merge parameter declarations. */
25453 if (tree pattern_parm
25454 = skip_artificial_parms_for (code_pattern,
25455 DECL_ARGUMENTS (code_pattern)))
25457 tree *p = &DECL_ARGUMENTS (decl);
25458 for (int skip = num_artificial_parms_for (decl); skip; --skip)
25459 p = &DECL_CHAIN (*p);
25460 *p = tsubst_decl (pattern_parm, args, tf_error);
25461 for (tree t = *p; t; t = DECL_CHAIN (t))
25462 DECL_CONTEXT (t) = decl;
25465 /* Merge additional specifiers from the CODE_PATTERN. */
25466 if (DECL_DECLARED_INLINE_P (code_pattern)
25467 && !DECL_DECLARED_INLINE_P (decl))
25468 DECL_DECLARED_INLINE_P (decl) = 1;
25470 maybe_instantiate_noexcept (decl, tf_error);
25472 else if (VAR_P (decl))
25474 start_lambda_scope (decl);
25475 DECL_INITIAL (decl) =
25476 tsubst_init (DECL_INITIAL (code_pattern), decl, args,
25477 tf_error, DECL_TI_TEMPLATE (decl));
25478 finish_lambda_scope ();
25479 if (VAR_HAD_UNKNOWN_BOUND (decl))
25480 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
25481 tf_error, DECL_TI_TEMPLATE (decl));
25483 else
25484 gcc_unreachable ();
25486 pop_access_scope (decl);
25489 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
25490 substituted to get DECL. */
25492 tree
25493 template_for_substitution (tree decl)
25495 tree tmpl = DECL_TI_TEMPLATE (decl);
25497 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
25498 for the instantiation. This is not always the most general
25499 template. Consider, for example:
25501 template <class T>
25502 struct S { template <class U> void f();
25503 template <> void f<int>(); };
25505 and an instantiation of S<double>::f<int>. We want TD to be the
25506 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
25507 while (/* An instantiation cannot have a definition, so we need a
25508 more general template. */
25509 DECL_TEMPLATE_INSTANTIATION (tmpl)
25510 /* We must also deal with friend templates. Given:
25512 template <class T> struct S {
25513 template <class U> friend void f() {};
25516 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
25517 so far as the language is concerned, but that's still
25518 where we get the pattern for the instantiation from. On
25519 other hand, if the definition comes outside the class, say:
25521 template <class T> struct S {
25522 template <class U> friend void f();
25524 template <class U> friend void f() {}
25526 we don't need to look any further. That's what the check for
25527 DECL_INITIAL is for. */
25528 || (TREE_CODE (decl) == FUNCTION_DECL
25529 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
25530 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
25532 /* The present template, TD, should not be a definition. If it
25533 were a definition, we should be using it! Note that we
25534 cannot restructure the loop to just keep going until we find
25535 a template with a definition, since that might go too far if
25536 a specialization was declared, but not defined. */
25538 /* Fetch the more general template. */
25539 tmpl = DECL_TI_TEMPLATE (tmpl);
25542 return tmpl;
25545 /* Returns true if we need to instantiate this template instance even if we
25546 know we aren't going to emit it. */
25548 bool
25549 always_instantiate_p (tree decl)
25551 /* We always instantiate inline functions so that we can inline them. An
25552 explicit instantiation declaration prohibits implicit instantiation of
25553 non-inline functions. With high levels of optimization, we would
25554 normally inline non-inline functions -- but we're not allowed to do
25555 that for "extern template" functions. Therefore, we check
25556 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
25557 return ((TREE_CODE (decl) == FUNCTION_DECL
25558 && (DECL_DECLARED_INLINE_P (decl)
25559 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
25560 /* And we need to instantiate static data members so that
25561 their initializers are available in integral constant
25562 expressions. */
25563 || (VAR_P (decl)
25564 && decl_maybe_constant_var_p (decl)));
25567 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
25568 instantiate it now, modifying TREE_TYPE (fn). Returns false on
25569 error, true otherwise. */
25571 bool
25572 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
25574 if (fn == error_mark_node)
25575 return false;
25577 /* Don't instantiate a noexcept-specification from template context. */
25578 if (processing_template_decl
25579 && (!flag_noexcept_type || type_dependent_expression_p (fn)))
25580 return true;
25582 if (DECL_MAYBE_DELETED (fn))
25584 if (fn == current_function_decl)
25585 /* We're in start_preparsed_function, keep going. */
25586 return true;
25588 ++function_depth;
25589 synthesize_method (fn);
25590 --function_depth;
25591 return !DECL_MAYBE_DELETED (fn);
25594 tree fntype = TREE_TYPE (fn);
25595 tree spec = TYPE_RAISES_EXCEPTIONS (fntype);
25597 if (!spec || !TREE_PURPOSE (spec))
25598 return true;
25600 tree noex = TREE_PURPOSE (spec);
25601 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
25602 && TREE_CODE (noex) != DEFERRED_PARSE)
25603 return true;
25605 tree orig_fn = NULL_TREE;
25606 /* For a member friend template we can get a TEMPLATE_DECL. Let's use
25607 its FUNCTION_DECL for the rest of this function -- push_access_scope
25608 doesn't accept TEMPLATE_DECLs. */
25609 if (DECL_FUNCTION_TEMPLATE_P (fn))
25611 orig_fn = fn;
25612 fn = DECL_TEMPLATE_RESULT (fn);
25615 if (DECL_CLONED_FUNCTION_P (fn))
25617 tree prime = DECL_CLONED_FUNCTION (fn);
25618 if (!maybe_instantiate_noexcept (prime, complain))
25619 return false;
25620 spec = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (prime));
25622 else if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
25624 static hash_set<tree>* fns = new hash_set<tree>;
25625 bool added = false;
25626 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
25628 spec = get_defaulted_eh_spec (fn, complain);
25629 if (spec == error_mark_node)
25630 /* This might have failed because of an unparsed DMI, so
25631 let's try again later. */
25632 return false;
25634 else if (!(added = !fns->add (fn)))
25636 /* If hash_set::add returns true, the element was already there. */
25637 location_t loc = cp_expr_loc_or_loc (DEFERRED_NOEXCEPT_PATTERN (noex),
25638 DECL_SOURCE_LOCATION (fn));
25639 error_at (loc,
25640 "exception specification of %qD depends on itself",
25641 fn);
25642 spec = noexcept_false_spec;
25644 else if (push_tinst_level (fn))
25646 push_to_top_level ();
25647 push_access_scope (fn);
25648 push_deferring_access_checks (dk_no_deferred);
25649 input_location = DECL_SOURCE_LOCATION (fn);
25651 if (!DECL_LOCAL_DECL_P (fn))
25653 /* If needed, set current_class_ptr for the benefit of
25654 tsubst_copy/PARM_DECL. The exception pattern will
25655 refer to the parm of the template, not the
25656 instantiation. */
25657 tree tdecl = DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (fn));
25658 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (tdecl))
25660 tree this_parm = DECL_ARGUMENTS (tdecl);
25661 current_class_ptr = NULL_TREE;
25662 current_class_ref = cp_build_fold_indirect_ref (this_parm);
25663 current_class_ptr = this_parm;
25667 /* If this function is represented by a TEMPLATE_DECL, then
25668 the deferred noexcept-specification might still contain
25669 dependent types, even after substitution. And we need the
25670 dependency check functions to work in build_noexcept_spec. */
25671 if (orig_fn)
25672 ++processing_template_decl;
25674 /* Do deferred instantiation of the noexcept-specifier. */
25675 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
25676 DEFERRED_NOEXCEPT_ARGS (noex),
25677 tf_warning_or_error, fn,
25678 /*function_p=*/false,
25679 /*i_c_e_p=*/true);
25681 /* Build up the noexcept-specification. */
25682 spec = build_noexcept_spec (noex, tf_warning_or_error);
25684 if (orig_fn)
25685 --processing_template_decl;
25687 pop_deferring_access_checks ();
25688 pop_access_scope (fn);
25689 pop_tinst_level ();
25690 pop_from_top_level ();
25692 else
25693 spec = noexcept_false_spec;
25695 if (added)
25696 fns->remove (fn);
25699 if (spec == error_mark_node)
25701 /* This failed with a hard error, so let's go with false. */
25702 gcc_assert (seen_error ());
25703 spec = noexcept_false_spec;
25706 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
25707 if (orig_fn)
25708 TREE_TYPE (orig_fn) = TREE_TYPE (fn);
25710 return true;
25713 /* We're starting to process the function INST, an instantiation of PATTERN;
25714 add their parameters to local_specializations. */
25716 static void
25717 register_parameter_specializations (tree pattern, tree inst)
25719 tree tmpl_parm = DECL_ARGUMENTS (pattern);
25720 tree spec_parm = DECL_ARGUMENTS (inst);
25721 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
25723 register_local_specialization (spec_parm, tmpl_parm);
25724 spec_parm = skip_artificial_parms_for (inst, spec_parm);
25725 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
25727 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
25729 if (!DECL_PACK_P (tmpl_parm))
25731 register_local_specialization (spec_parm, tmpl_parm);
25732 spec_parm = DECL_CHAIN (spec_parm);
25734 else
25736 /* Register the (value) argument pack as a specialization of
25737 TMPL_PARM, then move on. */
25738 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
25739 register_local_specialization (argpack, tmpl_parm);
25742 gcc_assert (!spec_parm);
25745 /* Instantiate the body of D using PATTERN with ARGS. We have
25746 already determined PATTERN is the correct template to use.
25747 NESTED_P is true if this is a nested function, in which case
25748 PATTERN will be a FUNCTION_DECL not a TEMPLATE_DECL. */
25750 static void
25751 instantiate_body (tree pattern, tree args, tree d, bool nested_p)
25753 tree td = NULL_TREE;
25754 tree code_pattern = pattern;
25756 if (!nested_p)
25758 td = pattern;
25759 code_pattern = DECL_TEMPLATE_RESULT (td);
25761 else
25762 /* Only OMP reductions are nested. */
25763 gcc_checking_assert (DECL_OMP_DECLARE_REDUCTION_P (code_pattern));
25765 vec<tree> omp_privatization_save;
25766 if (current_function_decl)
25767 save_omp_privatization_clauses (omp_privatization_save);
25769 bool push_to_top
25770 = !(current_function_decl
25771 && !LAMBDA_FUNCTION_P (d)
25772 && decl_function_context (d) == current_function_decl);
25774 if (push_to_top)
25775 push_to_top_level ();
25776 else
25778 gcc_assert (!processing_template_decl);
25779 push_function_context ();
25780 cp_unevaluated_operand = 0;
25781 c_inhibit_evaluation_warnings = 0;
25784 if (VAR_P (d))
25786 /* The variable might be a lambda's extra scope, and that
25787 lambda's visibility depends on D's. */
25788 maybe_commonize_var (d);
25789 determine_visibility (d);
25792 /* Mark D as instantiated so that recursive calls to
25793 instantiate_decl do not try to instantiate it again. */
25794 DECL_TEMPLATE_INSTANTIATED (d) = 1;
25796 if (td)
25797 /* Regenerate the declaration in case the template has been modified
25798 by a subsequent redeclaration. */
25799 regenerate_decl_from_template (d, td, args);
25801 /* We already set the file and line above. Reset them now in case
25802 they changed as a result of calling regenerate_decl_from_template. */
25803 input_location = DECL_SOURCE_LOCATION (d);
25805 if (VAR_P (d))
25807 /* Clear out DECL_RTL; whatever was there before may not be right
25808 since we've reset the type of the declaration. */
25809 SET_DECL_RTL (d, NULL);
25810 DECL_IN_AGGR_P (d) = 0;
25812 /* The initializer is placed in DECL_INITIAL by
25813 regenerate_decl_from_template so we don't need to
25814 push/pop_access_scope again here. Pull it out so that
25815 cp_finish_decl can process it. */
25816 bool const_init = false;
25817 tree init = DECL_INITIAL (d);
25818 DECL_INITIAL (d) = NULL_TREE;
25819 DECL_INITIALIZED_P (d) = 0;
25821 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
25822 initializer. That function will defer actual emission until
25823 we have a chance to determine linkage. */
25824 DECL_EXTERNAL (d) = 0;
25826 /* Enter the scope of D so that access-checking works correctly. */
25827 bool enter_context = DECL_CLASS_SCOPE_P (d);
25828 if (enter_context)
25829 push_nested_class (DECL_CONTEXT (d));
25831 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
25832 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
25834 if (enter_context)
25835 pop_nested_class ();
25837 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
25838 synthesize_method (d);
25839 else if (TREE_CODE (d) == FUNCTION_DECL)
25841 /* Set up the list of local specializations. */
25842 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
25843 tree block = NULL_TREE;
25845 /* Set up context. */
25846 if (nested_p)
25847 block = push_stmt_list ();
25848 else
25850 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
25852 perform_instantiation_time_access_checks (code_pattern, args);
25855 /* Create substitution entries for the parameters. */
25856 register_parameter_specializations (code_pattern, d);
25858 /* Substitute into the body of the function. */
25859 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
25860 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
25861 tf_warning_or_error, d);
25862 else
25864 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
25865 tf_warning_or_error, DECL_TI_TEMPLATE (d),
25866 /*integral_constant_expression_p=*/false);
25868 /* Set the current input_location to the end of the function
25869 so that finish_function knows where we are. */
25870 input_location
25871 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
25873 /* Remember if we saw an infinite loop in the template. */
25874 current_function_infinite_loop
25875 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
25878 /* Finish the function. */
25879 if (nested_p)
25880 DECL_SAVED_TREE (d) = pop_stmt_list (block);
25881 else
25883 d = finish_function (/*inline_p=*/false);
25884 expand_or_defer_fn (d);
25887 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
25888 cp_check_omp_declare_reduction (d);
25891 /* We're not deferring instantiation any more. */
25892 if (!nested_p)
25893 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
25895 if (push_to_top)
25896 pop_from_top_level ();
25897 else
25898 pop_function_context ();
25900 if (current_function_decl)
25901 restore_omp_privatization_clauses (omp_privatization_save);
25904 /* Produce the definition of D, a _DECL generated from a template. If
25905 DEFER_OK is true, then we don't have to actually do the
25906 instantiation now; we just have to do it sometime. Normally it is
25907 an error if this is an explicit instantiation but D is undefined.
25908 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
25909 instantiated class template. */
25911 tree
25912 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
25914 tree tmpl = DECL_TI_TEMPLATE (d);
25915 tree gen_args;
25916 tree args;
25917 tree td;
25918 tree code_pattern;
25919 tree spec;
25920 tree gen_tmpl;
25921 bool pattern_defined;
25922 location_t saved_loc = input_location;
25923 int saved_unevaluated_operand = cp_unevaluated_operand;
25924 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
25925 bool external_p;
25926 bool deleted_p;
25928 /* This function should only be used to instantiate templates for
25929 functions and static member variables. */
25930 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
25932 /* A concept is never instantiated. */
25933 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
25935 gcc_checking_assert (!DECL_FUNCTION_SCOPE_P (d));
25937 if (modules_p ())
25938 /* We may have a pending instantiation of D itself. */
25939 lazy_load_pendings (d);
25941 /* Variables are never deferred; if instantiation is required, they
25942 are instantiated right away. That allows for better code in the
25943 case that an expression refers to the value of the variable --
25944 if the variable has a constant value the referring expression can
25945 take advantage of that fact. */
25946 if (VAR_P (d))
25947 defer_ok = false;
25949 /* Don't instantiate cloned functions. Instead, instantiate the
25950 functions they cloned. */
25951 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
25952 d = DECL_CLONED_FUNCTION (d);
25954 if (DECL_TEMPLATE_INSTANTIATED (d)
25955 || TREE_TYPE (d) == error_mark_node
25956 || (TREE_CODE (d) == FUNCTION_DECL
25957 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
25958 || DECL_TEMPLATE_SPECIALIZATION (d))
25959 /* D has already been instantiated or explicitly specialized, so
25960 there's nothing for us to do here.
25962 It might seem reasonable to check whether or not D is an explicit
25963 instantiation, and, if so, stop here. But when an explicit
25964 instantiation is deferred until the end of the compilation,
25965 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
25966 the instantiation. */
25967 return d;
25969 /* Check to see whether we know that this template will be
25970 instantiated in some other file, as with "extern template"
25971 extension. */
25972 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
25974 /* In general, we do not instantiate such templates. */
25975 if (external_p && !always_instantiate_p (d))
25976 return d;
25978 gen_tmpl = most_general_template (tmpl);
25979 gen_args = DECL_TI_ARGS (d);
25981 /* We should already have the extra args. */
25982 gcc_checking_assert (tmpl == gen_tmpl
25983 || (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
25984 == TMPL_ARGS_DEPTH (gen_args)));
25985 /* And what's in the hash table should match D. */
25986 gcc_checking_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0))
25987 == d
25988 || spec == NULL_TREE);
25990 /* This needs to happen before any tsubsting. */
25991 if (! push_tinst_level (d))
25992 return d;
25994 timevar_push (TV_TEMPLATE_INST);
25996 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
25997 for the instantiation. */
25998 td = template_for_substitution (d);
25999 args = gen_args;
26001 if (VAR_P (d))
26003 /* Look up an explicit specialization, if any. */
26004 tree tid = lookup_template_variable (gen_tmpl, gen_args);
26005 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
26006 if (elt && elt != error_mark_node)
26008 td = TREE_VALUE (elt);
26009 args = TREE_PURPOSE (elt);
26013 code_pattern = DECL_TEMPLATE_RESULT (td);
26015 /* We should never be trying to instantiate a member of a class
26016 template or partial specialization. */
26017 gcc_assert (d != code_pattern);
26019 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
26020 || DECL_TEMPLATE_SPECIALIZATION (td))
26021 /* In the case of a friend template whose definition is provided
26022 outside the class, we may have too many arguments. Drop the
26023 ones we don't need. The same is true for specializations. */
26024 args = get_innermost_template_args
26025 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
26027 if (TREE_CODE (d) == FUNCTION_DECL)
26029 deleted_p = DECL_DELETED_FN (code_pattern);
26030 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
26031 && DECL_INITIAL (code_pattern) != error_mark_node)
26032 || DECL_DEFAULTED_FN (code_pattern)
26033 || deleted_p);
26035 else
26037 deleted_p = false;
26038 if (DECL_CLASS_SCOPE_P (code_pattern))
26039 pattern_defined = ! DECL_IN_AGGR_P (code_pattern);
26040 else
26041 pattern_defined = ! DECL_EXTERNAL (code_pattern);
26044 /* We may be in the middle of deferred access check. Disable it now. */
26045 push_deferring_access_checks (dk_no_deferred);
26047 /* Unless an explicit instantiation directive has already determined
26048 the linkage of D, remember that a definition is available for
26049 this entity. */
26050 if (pattern_defined
26051 && !DECL_INTERFACE_KNOWN (d)
26052 && !DECL_NOT_REALLY_EXTERN (d))
26053 mark_definable (d);
26055 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
26056 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
26057 input_location = DECL_SOURCE_LOCATION (d);
26059 /* If D is a member of an explicitly instantiated class template,
26060 and no definition is available, treat it like an implicit
26061 instantiation. */
26062 if (!pattern_defined && expl_inst_class_mem_p
26063 && DECL_EXPLICIT_INSTANTIATION (d))
26065 /* Leave linkage flags alone on instantiations with anonymous
26066 visibility. */
26067 if (TREE_PUBLIC (d))
26069 DECL_NOT_REALLY_EXTERN (d) = 0;
26070 DECL_INTERFACE_KNOWN (d) = 0;
26072 SET_DECL_IMPLICIT_INSTANTIATION (d);
26075 /* Defer all other templates, unless we have been explicitly
26076 forbidden from doing so. */
26077 if (/* If there is no definition, we cannot instantiate the
26078 template. */
26079 ! pattern_defined
26080 /* If it's OK to postpone instantiation, do so. */
26081 || defer_ok
26082 /* If this is a static data member that will be defined
26083 elsewhere, we don't want to instantiate the entire data
26084 member, but we do want to instantiate the initializer so that
26085 we can substitute that elsewhere. */
26086 || (external_p && VAR_P (d))
26087 /* Handle here a deleted function too, avoid generating
26088 its body (c++/61080). */
26089 || deleted_p)
26091 /* The definition of the static data member is now required so
26092 we must substitute the initializer. */
26093 if (VAR_P (d)
26094 && !DECL_INITIAL (d)
26095 && DECL_INITIAL (code_pattern))
26097 tree ns;
26098 tree init;
26099 bool const_init = false;
26100 bool enter_context = DECL_CLASS_SCOPE_P (d);
26102 ns = decl_namespace_context (d);
26103 push_nested_namespace (ns);
26104 if (enter_context)
26105 push_nested_class (DECL_CONTEXT (d));
26106 init = tsubst_expr (DECL_INITIAL (code_pattern),
26107 args,
26108 tf_warning_or_error, NULL_TREE,
26109 /*integral_constant_expression_p=*/false);
26110 /* If instantiating the initializer involved instantiating this
26111 again, don't call cp_finish_decl twice. */
26112 if (!DECL_INITIAL (d))
26114 /* Make sure the initializer is still constant, in case of
26115 circular dependency (template/instantiate6.C). */
26116 const_init
26117 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
26118 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
26119 /*asmspec_tree=*/NULL_TREE,
26120 LOOKUP_ONLYCONVERTING);
26122 if (enter_context)
26123 pop_nested_class ();
26124 pop_nested_namespace (ns);
26127 /* We restore the source position here because it's used by
26128 add_pending_template. */
26129 input_location = saved_loc;
26131 if (at_eof && !pattern_defined
26132 && DECL_EXPLICIT_INSTANTIATION (d)
26133 && DECL_NOT_REALLY_EXTERN (d))
26134 /* [temp.explicit]
26136 The definition of a non-exported function template, a
26137 non-exported member function template, or a non-exported
26138 member function or static data member of a class template
26139 shall be present in every translation unit in which it is
26140 explicitly instantiated. */
26141 permerror (input_location, "explicit instantiation of %qD "
26142 "but no definition available", d);
26144 /* If we're in unevaluated context, we just wanted to get the
26145 constant value; this isn't an odr use, so don't queue
26146 a full instantiation. */
26147 if (!cp_unevaluated_operand
26148 /* ??? Historically, we have instantiated inline functions, even
26149 when marked as "extern template". */
26150 && !(external_p && VAR_P (d)))
26151 add_pending_template (d);
26153 else
26155 set_instantiating_module (d);
26156 if (variable_template_p (gen_tmpl))
26157 note_variable_template_instantiation (d);
26158 instantiate_body (td, args, d, false);
26161 pop_deferring_access_checks ();
26162 timevar_pop (TV_TEMPLATE_INST);
26163 pop_tinst_level ();
26164 input_location = saved_loc;
26165 cp_unevaluated_operand = saved_unevaluated_operand;
26166 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
26168 return d;
26171 /* Run through the list of templates that we wish we could
26172 instantiate, and instantiate any we can. RETRIES is the
26173 number of times we retry pending template instantiation. */
26175 void
26176 instantiate_pending_templates (int retries)
26178 int reconsider;
26179 location_t saved_loc = input_location;
26181 /* Instantiating templates may trigger vtable generation. This in turn
26182 may require further template instantiations. We place a limit here
26183 to avoid infinite loop. */
26184 if (pending_templates && retries >= max_tinst_depth)
26186 tree decl = pending_templates->tinst->maybe_get_node ();
26188 fatal_error (input_location,
26189 "template instantiation depth exceeds maximum of %d"
26190 " instantiating %q+D, possibly from virtual table generation"
26191 " (use %<-ftemplate-depth=%> to increase the maximum)",
26192 max_tinst_depth, decl);
26193 if (TREE_CODE (decl) == FUNCTION_DECL)
26194 /* Pretend that we defined it. */
26195 DECL_INITIAL (decl) = error_mark_node;
26196 return;
26201 struct pending_template **t = &pending_templates;
26202 struct pending_template *last = NULL;
26203 reconsider = 0;
26204 while (*t)
26206 tree instantiation = reopen_tinst_level ((*t)->tinst);
26207 bool complete = false;
26209 if (TYPE_P (instantiation))
26211 if (!COMPLETE_TYPE_P (instantiation))
26213 instantiate_class_template (instantiation);
26214 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
26215 for (tree fld = TYPE_FIELDS (instantiation);
26216 fld; fld = TREE_CHAIN (fld))
26217 if ((VAR_P (fld)
26218 || (TREE_CODE (fld) == FUNCTION_DECL
26219 && !DECL_ARTIFICIAL (fld)))
26220 && DECL_TEMPLATE_INSTANTIATION (fld))
26221 instantiate_decl (fld,
26222 /*defer_ok=*/false,
26223 /*expl_inst_class_mem_p=*/false);
26225 if (COMPLETE_TYPE_P (instantiation))
26226 reconsider = 1;
26229 complete = COMPLETE_TYPE_P (instantiation);
26231 else
26233 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
26234 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
26236 instantiation
26237 = instantiate_decl (instantiation,
26238 /*defer_ok=*/false,
26239 /*expl_inst_class_mem_p=*/false);
26240 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
26241 reconsider = 1;
26244 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
26245 || DECL_TEMPLATE_INSTANTIATED (instantiation));
26248 if (complete)
26250 /* If INSTANTIATION has been instantiated, then we don't
26251 need to consider it again in the future. */
26252 struct pending_template *drop = *t;
26253 *t = (*t)->next;
26254 set_refcount_ptr (drop->tinst);
26255 pending_template_freelist ().free (drop);
26257 else
26259 last = *t;
26260 t = &(*t)->next;
26262 tinst_depth = 0;
26263 set_refcount_ptr (current_tinst_level);
26265 last_pending_template = last;
26267 while (reconsider);
26269 input_location = saved_loc;
26272 /* Substitute ARGVEC into T, which is a list of initializers for
26273 either base class or a non-static data member. The TREE_PURPOSEs
26274 are DECLs, and the TREE_VALUEs are the initializer values. Used by
26275 instantiate_decl. */
26277 static tree
26278 tsubst_initializer_list (tree t, tree argvec)
26280 tree inits = NULL_TREE;
26281 tree target_ctor = error_mark_node;
26283 for (; t; t = TREE_CHAIN (t))
26285 tree decl;
26286 tree init;
26287 tree expanded_bases = NULL_TREE;
26288 tree expanded_arguments = NULL_TREE;
26289 int i, len = 1;
26291 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
26293 tree expr;
26294 tree arg;
26296 /* Expand the base class expansion type into separate base
26297 classes. */
26298 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
26299 tf_warning_or_error,
26300 NULL_TREE);
26301 if (expanded_bases == error_mark_node)
26302 continue;
26304 /* We'll be building separate TREE_LISTs of arguments for
26305 each base. */
26306 len = TREE_VEC_LENGTH (expanded_bases);
26307 expanded_arguments = make_tree_vec (len);
26308 for (i = 0; i < len; i++)
26309 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
26311 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
26312 expand each argument in the TREE_VALUE of t. */
26313 expr = make_node (EXPR_PACK_EXPANSION);
26314 PACK_EXPANSION_LOCAL_P (expr) = true;
26315 PACK_EXPANSION_PARAMETER_PACKS (expr) =
26316 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
26318 if (TREE_VALUE (t) == void_type_node)
26319 /* VOID_TYPE_NODE is used to indicate
26320 value-initialization. */
26322 for (i = 0; i < len; i++)
26323 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
26325 else
26327 /* Substitute parameter packs into each argument in the
26328 TREE_LIST. */
26329 in_base_initializer = 1;
26330 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
26332 tree expanded_exprs;
26334 /* Expand the argument. */
26335 tree value;
26336 if (TREE_CODE (TREE_VALUE (arg)) == EXPR_PACK_EXPANSION)
26337 value = TREE_VALUE (arg);
26338 else
26340 value = expr;
26341 SET_PACK_EXPANSION_PATTERN (value, TREE_VALUE (arg));
26343 expanded_exprs
26344 = tsubst_pack_expansion (value, argvec,
26345 tf_warning_or_error,
26346 NULL_TREE);
26347 if (expanded_exprs == error_mark_node)
26348 continue;
26350 /* Prepend each of the expanded expressions to the
26351 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
26352 for (i = 0; i < len; i++)
26353 if (TREE_CODE (TREE_VALUE (arg)) == EXPR_PACK_EXPANSION)
26354 for (int j = 0; j < TREE_VEC_LENGTH (expanded_exprs); j++)
26355 TREE_VEC_ELT (expanded_arguments, i)
26356 = tree_cons (NULL_TREE,
26357 TREE_VEC_ELT (expanded_exprs, j),
26358 TREE_VEC_ELT (expanded_arguments, i));
26359 else
26360 TREE_VEC_ELT (expanded_arguments, i)
26361 = tree_cons (NULL_TREE,
26362 TREE_VEC_ELT (expanded_exprs, i),
26363 TREE_VEC_ELT (expanded_arguments, i));
26365 in_base_initializer = 0;
26367 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
26368 since we built them backwards. */
26369 for (i = 0; i < len; i++)
26371 TREE_VEC_ELT (expanded_arguments, i) =
26372 nreverse (TREE_VEC_ELT (expanded_arguments, i));
26377 for (i = 0; i < len; ++i)
26379 if (expanded_bases)
26381 decl = TREE_VEC_ELT (expanded_bases, i);
26382 decl = expand_member_init (decl);
26383 init = TREE_VEC_ELT (expanded_arguments, i);
26385 else
26387 tree tmp;
26388 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
26389 tf_warning_or_error, NULL_TREE);
26391 decl = expand_member_init (decl);
26392 if (decl && !DECL_P (decl))
26393 in_base_initializer = 1;
26395 init = TREE_VALUE (t);
26396 tmp = init;
26397 if (init != void_type_node)
26398 init = tsubst_expr (init, argvec,
26399 tf_warning_or_error, NULL_TREE,
26400 /*integral_constant_expression_p=*/false);
26401 if (init == NULL_TREE && tmp != NULL_TREE)
26402 /* If we had an initializer but it instantiated to nothing,
26403 value-initialize the object. This will only occur when
26404 the initializer was a pack expansion where the parameter
26405 packs used in that expansion were of length zero. */
26406 init = void_type_node;
26407 in_base_initializer = 0;
26410 if (target_ctor != error_mark_node
26411 && init != error_mark_node)
26413 error ("mem-initializer for %qD follows constructor delegation",
26414 decl);
26415 return inits;
26417 /* Look for a target constructor. */
26418 if (init != error_mark_node
26419 && decl && CLASS_TYPE_P (decl)
26420 && same_type_p (decl, current_class_type))
26422 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
26423 if (inits)
26425 error ("constructor delegation follows mem-initializer for %qD",
26426 TREE_PURPOSE (inits));
26427 continue;
26429 target_ctor = init;
26432 if (decl)
26434 init = build_tree_list (decl, init);
26435 /* Carry over the dummy TREE_TYPE node containing the source
26436 location. */
26437 TREE_TYPE (init) = TREE_TYPE (t);
26438 TREE_CHAIN (init) = inits;
26439 inits = init;
26443 return inits;
26446 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
26447 is the instantiation (which should have been created with
26448 start_enum) and ARGS are the template arguments to use. */
26450 static void
26451 tsubst_enum (tree tag, tree newtag, tree args)
26453 tree e;
26455 if (SCOPED_ENUM_P (newtag))
26456 begin_scope (sk_scoped_enum, newtag);
26458 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
26460 tree value;
26461 tree decl;
26463 decl = TREE_VALUE (e);
26464 /* Note that in a template enum, the TREE_VALUE is the
26465 CONST_DECL, not the corresponding INTEGER_CST. */
26466 value = tsubst_expr (DECL_INITIAL (decl),
26467 args, tf_warning_or_error, NULL_TREE,
26468 /*integral_constant_expression_p=*/true);
26470 /* Give this enumeration constant the correct access. */
26471 set_current_access_from_decl (decl);
26473 /* Actually build the enumerator itself. Here we're assuming that
26474 enumerators can't have dependent attributes. */
26475 build_enumerator (DECL_NAME (decl), value, newtag,
26476 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
26479 if (SCOPED_ENUM_P (newtag))
26480 finish_scope ();
26482 finish_enum_value_list (newtag);
26483 finish_enum (newtag);
26485 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
26486 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
26489 /* DECL is a FUNCTION_DECL that is a template specialization. Return
26490 its type -- but without substituting the innermost set of template
26491 arguments. So, innermost set of template parameters will appear in
26492 the type. */
26494 tree
26495 get_mostly_instantiated_function_type (tree decl)
26497 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
26498 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
26501 /* Return truthvalue if we're processing a template different from
26502 the last one involved in diagnostics. */
26503 bool
26504 problematic_instantiation_changed (void)
26506 return current_tinst_level != last_error_tinst_level;
26509 /* Remember current template involved in diagnostics. */
26510 void
26511 record_last_problematic_instantiation (void)
26513 set_refcount_ptr (last_error_tinst_level, current_tinst_level);
26516 struct tinst_level *
26517 current_instantiation (void)
26519 return current_tinst_level;
26522 /* Return TRUE if current_function_decl is being instantiated, false
26523 otherwise. */
26525 bool
26526 instantiating_current_function_p (void)
26528 return (current_instantiation ()
26529 && (current_instantiation ()->maybe_get_node ()
26530 == current_function_decl));
26533 /* [temp.param] Check that template non-type parm TYPE is of an allowable
26534 type. Return false for ok, true for disallowed. Issue error and
26535 inform messages under control of COMPLAIN. */
26537 static bool
26538 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
26540 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
26541 return false;
26542 else if (TYPE_PTR_P (type))
26543 return false;
26544 else if (TYPE_REF_P (type)
26545 && !TYPE_REF_IS_RVALUE (type))
26546 return false;
26547 else if (TYPE_PTRMEM_P (type))
26548 return false;
26549 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
26551 if (CLASS_PLACEHOLDER_TEMPLATE (type) && cxx_dialect < cxx20)
26553 if (complain & tf_error)
26554 error ("non-type template parameters of deduced class type only "
26555 "available with %<-std=c++20%> or %<-std=gnu++20%>");
26556 return true;
26558 return false;
26560 else if (TREE_CODE (type) == TYPENAME_TYPE)
26561 return false;
26562 else if (TREE_CODE (type) == DECLTYPE_TYPE)
26563 return false;
26564 else if (TREE_CODE (type) == NULLPTR_TYPE)
26565 return false;
26566 /* A bound template template parm could later be instantiated to have a valid
26567 nontype parm type via an alias template. */
26568 else if (cxx_dialect >= cxx11
26569 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
26570 return false;
26571 else if (TREE_CODE (type) == COMPLEX_TYPE)
26572 /* Fall through. */;
26573 else if (VOID_TYPE_P (type))
26574 /* Fall through. */;
26575 else if (cxx_dialect >= cxx20)
26577 if (dependent_type_p (type))
26578 return false;
26579 if (!complete_type_or_maybe_complain (type, NULL_TREE, complain))
26580 return true;
26581 if (structural_type_p (type))
26582 return false;
26583 if (complain & tf_error)
26585 auto_diagnostic_group d;
26586 error ("%qT is not a valid type for a template non-type "
26587 "parameter because it is not structural", type);
26588 structural_type_p (type, true);
26590 return true;
26592 else if (CLASS_TYPE_P (type))
26594 if (complain & tf_error)
26595 error ("non-type template parameters of class type only available "
26596 "with %<-std=c++20%> or %<-std=gnu++20%>");
26597 return true;
26600 if (complain & tf_error)
26602 if (type == error_mark_node)
26603 inform (input_location, "invalid template non-type parameter");
26604 else
26605 error ("%q#T is not a valid type for a template non-type parameter",
26606 type);
26608 return true;
26611 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
26612 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
26614 static bool
26615 dependent_type_p_r (tree type)
26617 tree scope;
26619 /* [temp.dep.type]
26621 A type is dependent if it is:
26623 -- a template parameter. Template template parameters are types
26624 for us (since TYPE_P holds true for them) so we handle
26625 them here. */
26626 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
26627 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
26628 return true;
26629 /* -- a qualified-id with a nested-name-specifier which contains a
26630 class-name that names a dependent type or whose unqualified-id
26631 names a dependent type. */
26632 if (TREE_CODE (type) == TYPENAME_TYPE)
26633 return true;
26635 /* An alias template specialization can be dependent even if the
26636 resulting type is not. */
26637 if (dependent_alias_template_spec_p (type, nt_transparent))
26638 return true;
26640 /* -- a cv-qualified type where the cv-unqualified type is
26641 dependent.
26642 No code is necessary for this bullet; the code below handles
26643 cv-qualified types, and we don't want to strip aliases with
26644 TYPE_MAIN_VARIANT because of DR 1558. */
26645 /* -- a compound type constructed from any dependent type. */
26646 if (TYPE_PTRMEM_P (type))
26647 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
26648 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
26649 (type)));
26650 else if (INDIRECT_TYPE_P (type))
26651 return dependent_type_p (TREE_TYPE (type));
26652 else if (FUNC_OR_METHOD_TYPE_P (type))
26654 tree arg_type;
26656 if (dependent_type_p (TREE_TYPE (type)))
26657 return true;
26658 for (arg_type = TYPE_ARG_TYPES (type);
26659 arg_type;
26660 arg_type = TREE_CHAIN (arg_type))
26661 if (dependent_type_p (TREE_VALUE (arg_type)))
26662 return true;
26663 if (cxx_dialect >= cxx17)
26664 /* A value-dependent noexcept-specifier makes the type dependent. */
26665 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
26666 if (tree noex = TREE_PURPOSE (spec))
26667 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
26668 affect overload resolution and treating it as dependent breaks
26669 things. Same for an unparsed noexcept expression. */
26670 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
26671 && TREE_CODE (noex) != DEFERRED_PARSE
26672 && value_dependent_expression_p (noex))
26673 return true;
26674 return false;
26676 /* -- an array type constructed from any dependent type or whose
26677 size is specified by a constant expression that is
26678 value-dependent.
26680 We checked for type- and value-dependence of the bounds in
26681 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
26682 if (TREE_CODE (type) == ARRAY_TYPE)
26684 if (TYPE_DOMAIN (type)
26685 && dependent_type_p (TYPE_DOMAIN (type)))
26686 return true;
26687 return dependent_type_p (TREE_TYPE (type));
26690 /* -- a template-id in which either the template name is a template
26691 parameter ... */
26692 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
26693 return true;
26694 /* ... or any of the template arguments is a dependent type or
26695 an expression that is type-dependent or value-dependent. */
26696 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
26697 && (any_dependent_template_arguments_p
26698 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
26699 return true;
26701 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
26702 dependent; if the argument of the `typeof' expression is not
26703 type-dependent, then it should already been have resolved. */
26704 if (TREE_CODE (type) == TYPEOF_TYPE
26705 || TREE_CODE (type) == DECLTYPE_TYPE
26706 || TREE_CODE (type) == UNDERLYING_TYPE)
26707 return true;
26709 /* A template argument pack is dependent if any of its packed
26710 arguments are. */
26711 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
26713 tree args = ARGUMENT_PACK_ARGS (type);
26714 int i, len = TREE_VEC_LENGTH (args);
26715 for (i = 0; i < len; ++i)
26716 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
26717 return true;
26720 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
26721 be template parameters. */
26722 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
26723 return true;
26725 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
26726 return true;
26728 /* The standard does not specifically mention types that are local
26729 to template functions or local classes, but they should be
26730 considered dependent too. For example:
26732 template <int I> void f() {
26733 enum E { a = I };
26734 S<sizeof (E)> s;
26737 The size of `E' cannot be known until the value of `I' has been
26738 determined. Therefore, `E' must be considered dependent. */
26739 scope = TYPE_CONTEXT (type);
26740 if (scope && TYPE_P (scope))
26741 return dependent_type_p (scope);
26742 /* Don't use type_dependent_expression_p here, as it can lead
26743 to infinite recursion trying to determine whether a lambda
26744 nested in a lambda is dependent (c++/47687). */
26745 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
26746 && DECL_LANG_SPECIFIC (scope)
26747 && DECL_TEMPLATE_INFO (scope)
26748 && (any_dependent_template_arguments_p
26749 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
26750 return true;
26752 /* Other types are non-dependent. */
26753 return false;
26756 /* Returns TRUE if TYPE is dependent, in the sense of
26757 [temp.dep.type]. Note that a NULL type is considered dependent. */
26759 bool
26760 dependent_type_p (tree type)
26762 /* If there are no template parameters in scope, then there can't be
26763 any dependent types. */
26764 if (!processing_template_decl)
26766 /* If we are not processing a template, then nobody should be
26767 providing us with a dependent type. */
26768 gcc_assert (type);
26769 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
26770 return false;
26773 /* If the type is NULL, we have not computed a type for the entity
26774 in question; in that case, the type is dependent. */
26775 if (!type)
26776 return true;
26778 /* Erroneous types can be considered non-dependent. */
26779 if (type == error_mark_node)
26780 return false;
26782 /* If we have not already computed the appropriate value for TYPE,
26783 do so now. */
26784 if (!TYPE_DEPENDENT_P_VALID (type))
26786 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
26787 TYPE_DEPENDENT_P_VALID (type) = 1;
26790 return TYPE_DEPENDENT_P (type);
26793 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
26794 lookup. In other words, a dependent type that is not the current
26795 instantiation. */
26797 bool
26798 dependent_scope_p (tree scope)
26800 return (scope && TYPE_P (scope) && dependent_type_p (scope)
26801 && !currently_open_class (scope));
26804 /* T is a SCOPE_REF. Return whether it represents a non-static member of
26805 an unknown base of 'this' (and is therefore instantiation-dependent). */
26807 static bool
26808 unknown_base_ref_p (tree t)
26810 if (!current_class_ptr)
26811 return false;
26813 tree mem = TREE_OPERAND (t, 1);
26814 if (shared_member_p (mem))
26815 return false;
26817 tree cur = current_nonlambda_class_type ();
26818 if (!any_dependent_bases_p (cur))
26819 return false;
26821 tree ctx = TREE_OPERAND (t, 0);
26822 if (DERIVED_FROM_P (ctx, cur))
26823 return false;
26825 return true;
26828 /* T is a SCOPE_REF; return whether we need to consider it
26829 instantiation-dependent so that we can check access at instantiation
26830 time even though we know which member it resolves to. */
26832 static bool
26833 instantiation_dependent_scope_ref_p (tree t)
26835 if (DECL_P (TREE_OPERAND (t, 1))
26836 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
26837 && !unknown_base_ref_p (t)
26838 && accessible_in_template_p (TREE_OPERAND (t, 0),
26839 TREE_OPERAND (t, 1)))
26840 return false;
26841 else
26842 return true;
26845 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
26846 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
26847 expression. */
26849 /* Note that this predicate is not appropriate for general expressions;
26850 only constant expressions (that satisfy potential_constant_expression)
26851 can be tested for value dependence. */
26853 bool
26854 value_dependent_expression_p (tree expression)
26856 if (!processing_template_decl || expression == NULL_TREE)
26857 return false;
26859 /* A type-dependent expression is also value-dependent. */
26860 if (type_dependent_expression_p (expression))
26861 return true;
26863 switch (TREE_CODE (expression))
26865 case BASELINK:
26866 /* A dependent member function of the current instantiation. */
26867 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
26869 case FUNCTION_DECL:
26870 /* A dependent member function of the current instantiation. */
26871 if (DECL_CLASS_SCOPE_P (expression)
26872 && dependent_type_p (DECL_CONTEXT (expression)))
26873 return true;
26874 break;
26876 case IDENTIFIER_NODE:
26877 /* A name that has not been looked up -- must be dependent. */
26878 return true;
26880 case TEMPLATE_PARM_INDEX:
26881 /* A non-type template parm. */
26882 return true;
26884 case CONST_DECL:
26885 /* A non-type template parm. */
26886 if (DECL_TEMPLATE_PARM_P (expression))
26887 return true;
26888 return value_dependent_expression_p (DECL_INITIAL (expression));
26890 case VAR_DECL:
26891 /* A constant with literal type and is initialized
26892 with an expression that is value-dependent. */
26893 if (DECL_DEPENDENT_INIT_P (expression)
26894 /* FIXME cp_finish_decl doesn't fold reference initializers. */
26895 || TYPE_REF_P (TREE_TYPE (expression)))
26896 return true;
26897 if (DECL_HAS_VALUE_EXPR_P (expression))
26899 tree value_expr = DECL_VALUE_EXPR (expression);
26900 if (value_dependent_expression_p (value_expr)
26901 /* __PRETTY_FUNCTION__ inside a template function is dependent
26902 on the name of the function. */
26903 || (DECL_PRETTY_FUNCTION_P (expression)
26904 /* It might be used in a template, but not a template
26905 function, in which case its DECL_VALUE_EXPR will be
26906 "top level". */
26907 && value_expr == error_mark_node))
26908 return true;
26910 return false;
26912 case DYNAMIC_CAST_EXPR:
26913 case STATIC_CAST_EXPR:
26914 case CONST_CAST_EXPR:
26915 case REINTERPRET_CAST_EXPR:
26916 case CAST_EXPR:
26917 case IMPLICIT_CONV_EXPR:
26918 /* These expressions are value-dependent if the type to which
26919 the cast occurs is dependent or the expression being casted
26920 is value-dependent. */
26922 tree type = TREE_TYPE (expression);
26924 if (dependent_type_p (type))
26925 return true;
26927 /* A functional cast has a list of operands. */
26928 expression = TREE_OPERAND (expression, 0);
26929 if (!expression)
26931 /* If there are no operands, it must be an expression such
26932 as "int()". This should not happen for aggregate types
26933 because it would form non-constant expressions. */
26934 gcc_assert (cxx_dialect >= cxx11
26935 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
26937 return false;
26940 if (TREE_CODE (expression) == TREE_LIST)
26941 return any_value_dependent_elements_p (expression);
26943 return value_dependent_expression_p (expression);
26946 case SIZEOF_EXPR:
26947 if (SIZEOF_EXPR_TYPE_P (expression))
26948 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
26949 /* FALLTHRU */
26950 case ALIGNOF_EXPR:
26951 case TYPEID_EXPR:
26952 /* A `sizeof' expression is value-dependent if the operand is
26953 type-dependent or is a pack expansion. */
26954 expression = TREE_OPERAND (expression, 0);
26955 if (PACK_EXPANSION_P (expression))
26956 return true;
26957 else if (TYPE_P (expression))
26958 return dependent_type_p (expression);
26959 return instantiation_dependent_uneval_expression_p (expression);
26961 case AT_ENCODE_EXPR:
26962 /* An 'encode' expression is value-dependent if the operand is
26963 type-dependent. */
26964 expression = TREE_OPERAND (expression, 0);
26965 return dependent_type_p (expression);
26967 case NOEXCEPT_EXPR:
26968 expression = TREE_OPERAND (expression, 0);
26969 return instantiation_dependent_uneval_expression_p (expression);
26971 case SCOPE_REF:
26972 /* All instantiation-dependent expressions should also be considered
26973 value-dependent. */
26974 return instantiation_dependent_scope_ref_p (expression);
26976 case COMPONENT_REF:
26977 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
26978 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
26980 case NONTYPE_ARGUMENT_PACK:
26981 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
26982 is value-dependent. */
26984 tree values = ARGUMENT_PACK_ARGS (expression);
26985 int i, len = TREE_VEC_LENGTH (values);
26987 for (i = 0; i < len; ++i)
26988 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
26989 return true;
26991 return false;
26994 case TRAIT_EXPR:
26996 tree type2 = TRAIT_EXPR_TYPE2 (expression);
26998 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
26999 return true;
27001 if (!type2)
27002 return false;
27004 if (TREE_CODE (type2) != TREE_LIST)
27005 return dependent_type_p (type2);
27007 for (; type2; type2 = TREE_CHAIN (type2))
27008 if (dependent_type_p (TREE_VALUE (type2)))
27009 return true;
27011 return false;
27014 case MODOP_EXPR:
27015 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
27016 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
27018 case ARRAY_REF:
27019 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
27020 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
27022 case ADDR_EXPR:
27024 tree op = TREE_OPERAND (expression, 0);
27025 return (value_dependent_expression_p (op)
27026 || has_value_dependent_address (op));
27029 case REQUIRES_EXPR:
27030 /* Treat all requires-expressions as value-dependent so
27031 we don't try to fold them. */
27032 return true;
27034 case TYPE_REQ:
27035 return dependent_type_p (TREE_OPERAND (expression, 0));
27037 case CALL_EXPR:
27039 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
27040 return true;
27041 tree fn = get_callee_fndecl (expression);
27042 int i, nargs;
27043 nargs = call_expr_nargs (expression);
27044 for (i = 0; i < nargs; ++i)
27046 tree op = CALL_EXPR_ARG (expression, i);
27047 /* In a call to a constexpr member function, look through the
27048 implicit ADDR_EXPR on the object argument so that it doesn't
27049 cause the call to be considered value-dependent. We also
27050 look through it in potential_constant_expression. */
27051 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
27052 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
27053 && TREE_CODE (op) == ADDR_EXPR)
27054 op = TREE_OPERAND (op, 0);
27055 if (value_dependent_expression_p (op))
27056 return true;
27058 return false;
27061 case TEMPLATE_ID_EXPR:
27062 return concept_definition_p (TREE_OPERAND (expression, 0));
27064 case CONSTRUCTOR:
27066 unsigned ix;
27067 tree val;
27068 if (dependent_type_p (TREE_TYPE (expression)))
27069 return true;
27070 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
27071 if (value_dependent_expression_p (val))
27072 return true;
27073 return false;
27076 case STMT_EXPR:
27077 /* Treat a GNU statement expression as dependent to avoid crashing
27078 under instantiate_non_dependent_expr; it can't be constant. */
27079 return true;
27081 default:
27082 /* A constant expression is value-dependent if any subexpression is
27083 value-dependent. */
27084 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
27086 case tcc_reference:
27087 case tcc_unary:
27088 case tcc_comparison:
27089 case tcc_binary:
27090 case tcc_expression:
27091 case tcc_vl_exp:
27093 int i, len = cp_tree_operand_length (expression);
27095 for (i = 0; i < len; i++)
27097 tree t = TREE_OPERAND (expression, i);
27099 /* In some cases, some of the operands may be missing.
27100 (For example, in the case of PREDECREMENT_EXPR, the
27101 amount to increment by may be missing.) That doesn't
27102 make the expression dependent. */
27103 if (t && value_dependent_expression_p (t))
27104 return true;
27107 break;
27108 default:
27109 break;
27111 break;
27114 /* The expression is not value-dependent. */
27115 return false;
27118 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
27119 [temp.dep.expr]. Note that an expression with no type is
27120 considered dependent. Other parts of the compiler arrange for an
27121 expression with type-dependent subexpressions to have no type, so
27122 this function doesn't have to be fully recursive. */
27124 bool
27125 type_dependent_expression_p (tree expression)
27127 if (!processing_template_decl)
27128 return false;
27130 if (expression == NULL_TREE || expression == error_mark_node)
27131 return false;
27133 STRIP_ANY_LOCATION_WRAPPER (expression);
27135 /* An unresolved name is always dependent. */
27136 if (identifier_p (expression)
27137 || TREE_CODE (expression) == USING_DECL
27138 || TREE_CODE (expression) == WILDCARD_DECL)
27139 return true;
27141 /* A lambda-expression in template context is dependent. dependent_type_p is
27142 true for a lambda in the scope of a class or function template, but that
27143 doesn't cover all template contexts, like a default template argument. */
27144 if (TREE_CODE (expression) == LAMBDA_EXPR)
27145 return true;
27147 /* A fold expression is type-dependent. */
27148 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
27149 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
27150 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
27151 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
27152 return true;
27154 /* Some expression forms are never type-dependent. */
27155 if (TREE_CODE (expression) == SIZEOF_EXPR
27156 || TREE_CODE (expression) == ALIGNOF_EXPR
27157 || TREE_CODE (expression) == AT_ENCODE_EXPR
27158 || TREE_CODE (expression) == NOEXCEPT_EXPR
27159 || TREE_CODE (expression) == TRAIT_EXPR
27160 || TREE_CODE (expression) == TYPEID_EXPR
27161 || TREE_CODE (expression) == DELETE_EXPR
27162 || TREE_CODE (expression) == VEC_DELETE_EXPR
27163 || TREE_CODE (expression) == THROW_EXPR
27164 || TREE_CODE (expression) == REQUIRES_EXPR)
27165 return false;
27167 /* The types of these expressions depends only on the type to which
27168 the cast occurs. */
27169 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
27170 || TREE_CODE (expression) == STATIC_CAST_EXPR
27171 || TREE_CODE (expression) == CONST_CAST_EXPR
27172 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
27173 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
27174 || TREE_CODE (expression) == CAST_EXPR)
27175 return dependent_type_p (TREE_TYPE (expression));
27177 /* The types of these expressions depends only on the type created
27178 by the expression. */
27179 if (TREE_CODE (expression) == NEW_EXPR
27180 || TREE_CODE (expression) == VEC_NEW_EXPR)
27182 /* For NEW_EXPR tree nodes created inside a template, either
27183 the object type itself or a TREE_LIST may appear as the
27184 operand 1. */
27185 tree type = TREE_OPERAND (expression, 1);
27186 if (TREE_CODE (type) == TREE_LIST)
27187 /* This is an array type. We need to check array dimensions
27188 as well. */
27189 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
27190 || value_dependent_expression_p
27191 (TREE_OPERAND (TREE_VALUE (type), 1));
27192 /* Array type whose dimension has to be deduced. */
27193 else if (TREE_CODE (type) == ARRAY_TYPE
27194 && TREE_OPERAND (expression, 2) == NULL_TREE)
27195 return true;
27196 else
27197 return dependent_type_p (type);
27200 if (TREE_CODE (expression) == SCOPE_REF)
27202 tree scope = TREE_OPERAND (expression, 0);
27203 tree name = TREE_OPERAND (expression, 1);
27205 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
27206 contains an identifier associated by name lookup with one or more
27207 declarations declared with a dependent type, or...a
27208 nested-name-specifier or qualified-id that names a member of an
27209 unknown specialization. */
27210 return (type_dependent_expression_p (name)
27211 || dependent_scope_p (scope));
27214 if (TREE_CODE (expression) == TEMPLATE_DECL
27215 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
27216 return uses_outer_template_parms (expression);
27218 if (TREE_CODE (expression) == STMT_EXPR)
27219 expression = stmt_expr_value_expr (expression);
27221 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
27223 tree elt;
27224 unsigned i;
27226 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
27228 if (type_dependent_expression_p (elt))
27229 return true;
27231 return false;
27234 /* A static data member of the current instantiation with incomplete
27235 array type is type-dependent, as the definition and specializations
27236 can have different bounds. */
27237 if (VAR_P (expression)
27238 && DECL_CLASS_SCOPE_P (expression)
27239 && dependent_type_p (DECL_CONTEXT (expression))
27240 && VAR_HAD_UNKNOWN_BOUND (expression))
27241 return true;
27243 /* An array of unknown bound depending on a variadic parameter, eg:
27245 template<typename... Args>
27246 void foo (Args... args)
27248 int arr[] = { args... };
27251 template<int... vals>
27252 void bar ()
27254 int arr[] = { vals... };
27257 If the array has no length and has an initializer, it must be that
27258 we couldn't determine its length in cp_complete_array_type because
27259 it is dependent. */
27260 if (VAR_P (expression)
27261 && TREE_TYPE (expression) != NULL_TREE
27262 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
27263 && !TYPE_DOMAIN (TREE_TYPE (expression))
27264 && DECL_INITIAL (expression))
27265 return true;
27267 /* A function or variable template-id is type-dependent if it has any
27268 dependent template arguments. */
27269 if (VAR_OR_FUNCTION_DECL_P (expression)
27270 && DECL_LANG_SPECIFIC (expression)
27271 && DECL_TEMPLATE_INFO (expression))
27273 /* Consider the innermost template arguments, since those are the ones
27274 that come from the template-id; the template arguments for the
27275 enclosing class do not make it type-dependent unless they are used in
27276 the type of the decl. */
27277 if (instantiates_primary_template_p (expression)
27278 && (any_dependent_template_arguments_p
27279 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
27280 return true;
27283 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
27284 type-dependent. Checking this is important for functions with auto return
27285 type, which looks like a dependent type. */
27286 if (TREE_CODE (expression) == FUNCTION_DECL
27287 && !(DECL_CLASS_SCOPE_P (expression)
27288 && dependent_type_p (DECL_CONTEXT (expression)))
27289 && !(DECL_LANG_SPECIFIC (expression)
27290 && DECL_UNIQUE_FRIEND_P (expression)
27291 && (!DECL_FRIEND_CONTEXT (expression)
27292 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
27293 && !DECL_LOCAL_DECL_P (expression))
27295 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
27296 || undeduced_auto_decl (expression));
27297 return false;
27300 /* Always dependent, on the number of arguments if nothing else. */
27301 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
27302 return true;
27304 if (TREE_TYPE (expression) == unknown_type_node)
27306 if (TREE_CODE (expression) == ADDR_EXPR)
27307 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
27308 if (TREE_CODE (expression) == COMPONENT_REF
27309 || TREE_CODE (expression) == OFFSET_REF)
27311 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
27312 return true;
27313 expression = TREE_OPERAND (expression, 1);
27314 if (identifier_p (expression))
27315 return false;
27317 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
27318 if (TREE_CODE (expression) == SCOPE_REF)
27319 return false;
27321 /* CO_AWAIT/YIELD_EXPR with unknown type is always dependent. */
27322 if (TREE_CODE (expression) == CO_AWAIT_EXPR
27323 || TREE_CODE (expression) == CO_YIELD_EXPR)
27324 return true;
27326 if (BASELINK_P (expression))
27328 if (BASELINK_OPTYPE (expression)
27329 && dependent_type_p (BASELINK_OPTYPE (expression)))
27330 return true;
27331 expression = BASELINK_FUNCTIONS (expression);
27334 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
27336 if (any_dependent_template_arguments_p
27337 (TREE_OPERAND (expression, 1)))
27338 return true;
27339 expression = TREE_OPERAND (expression, 0);
27340 if (identifier_p (expression))
27341 return true;
27344 gcc_assert (OVL_P (expression));
27346 for (lkp_iterator iter (expression); iter; ++iter)
27347 if (type_dependent_expression_p (*iter))
27348 return true;
27350 return false;
27353 /* The type of a non-type template parm declared with a placeholder type
27354 depends on the corresponding template argument, even though
27355 placeholders are not normally considered dependent. */
27356 if (TREE_CODE (expression) == TEMPLATE_PARM_INDEX
27357 && is_auto (TREE_TYPE (expression)))
27358 return true;
27360 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
27362 /* Dependent type attributes might not have made it from the decl to
27363 the type yet. */
27364 if (DECL_P (expression)
27365 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
27366 return true;
27368 return (dependent_type_p (TREE_TYPE (expression)));
27371 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
27372 type-dependent if the expression refers to a member of the current
27373 instantiation and the type of the referenced member is dependent, or the
27374 class member access expression refers to a member of an unknown
27375 specialization.
27377 This function returns true if the OBJECT in such a class member access
27378 expression is of an unknown specialization. */
27380 bool
27381 type_dependent_object_expression_p (tree object)
27383 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
27384 dependent. */
27385 if (TREE_CODE (object) == IDENTIFIER_NODE)
27386 return true;
27387 tree scope = TREE_TYPE (object);
27388 return (!scope || dependent_scope_p (scope));
27391 /* walk_tree callback function for instantiation_dependent_expression_p,
27392 below. Returns non-zero if a dependent subexpression is found. */
27394 static tree
27395 instantiation_dependent_r (tree *tp, int *walk_subtrees,
27396 void * /*data*/)
27398 if (TYPE_P (*tp))
27400 /* We don't have to worry about decltype currently because decltype
27401 of an instantiation-dependent expr is a dependent type. This
27402 might change depending on the resolution of DR 1172. */
27403 *walk_subtrees = false;
27404 return NULL_TREE;
27406 enum tree_code code = TREE_CODE (*tp);
27407 switch (code)
27409 /* Don't treat an argument list as dependent just because it has no
27410 TREE_TYPE. */
27411 case TREE_LIST:
27412 case TREE_VEC:
27413 case NONTYPE_ARGUMENT_PACK:
27414 return NULL_TREE;
27416 case TEMPLATE_PARM_INDEX:
27417 if (dependent_type_p (TREE_TYPE (*tp)))
27418 return *tp;
27419 if (TEMPLATE_PARM_PARAMETER_PACK (*tp))
27420 return *tp;
27421 /* We'll check value-dependence separately. */
27422 return NULL_TREE;
27424 /* Handle expressions with type operands. */
27425 case SIZEOF_EXPR:
27426 case ALIGNOF_EXPR:
27427 case TYPEID_EXPR:
27428 case AT_ENCODE_EXPR:
27430 tree op = TREE_OPERAND (*tp, 0);
27431 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
27432 op = TREE_TYPE (op);
27433 if (TYPE_P (op))
27435 if (dependent_type_p (op))
27436 return *tp;
27437 else
27439 *walk_subtrees = false;
27440 return NULL_TREE;
27443 break;
27446 case COMPONENT_REF:
27447 if (identifier_p (TREE_OPERAND (*tp, 1)))
27448 /* In a template, finish_class_member_access_expr creates a
27449 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
27450 type-dependent, so that we can check access control at
27451 instantiation time (PR 42277). See also Core issue 1273. */
27452 return *tp;
27453 break;
27455 case SCOPE_REF:
27456 if (instantiation_dependent_scope_ref_p (*tp))
27457 return *tp;
27458 else
27459 break;
27461 /* Treat statement-expressions as dependent. */
27462 case BIND_EXPR:
27463 return *tp;
27465 /* Treat requires-expressions as dependent. */
27466 case REQUIRES_EXPR:
27467 return *tp;
27469 case CALL_EXPR:
27470 /* Treat concept checks as dependent. */
27471 if (concept_check_p (*tp))
27472 return *tp;
27473 break;
27475 case TEMPLATE_ID_EXPR:
27476 /* Treat concept checks as dependent. */
27477 if (concept_check_p (*tp))
27478 return *tp;
27479 break;
27481 case CONSTRUCTOR:
27482 if (CONSTRUCTOR_IS_DEPENDENT (*tp))
27483 return *tp;
27484 break;
27486 default:
27487 break;
27490 if (type_dependent_expression_p (*tp))
27491 return *tp;
27492 else
27493 return NULL_TREE;
27496 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
27497 sense defined by the ABI:
27499 "An expression is instantiation-dependent if it is type-dependent
27500 or value-dependent, or it has a subexpression that is type-dependent
27501 or value-dependent."
27503 Except don't actually check value-dependence for unevaluated expressions,
27504 because in sizeof(i) we don't care about the value of i. Checking
27505 type-dependence will in turn check value-dependence of array bounds/template
27506 arguments as needed. */
27508 bool
27509 instantiation_dependent_uneval_expression_p (tree expression)
27511 tree result;
27513 if (!processing_template_decl)
27514 return false;
27516 if (expression == error_mark_node)
27517 return false;
27519 result = cp_walk_tree_without_duplicates (&expression,
27520 instantiation_dependent_r, NULL);
27521 return result != NULL_TREE;
27524 /* As above, but also check value-dependence of the expression as a whole. */
27526 bool
27527 instantiation_dependent_expression_p (tree expression)
27529 return (instantiation_dependent_uneval_expression_p (expression)
27530 || (processing_template_decl
27531 && potential_constant_expression (expression)
27532 && value_dependent_expression_p (expression)));
27535 /* Like type_dependent_expression_p, but it also works while not processing
27536 a template definition, i.e. during substitution or mangling. */
27538 bool
27539 type_dependent_expression_p_push (tree expr)
27541 bool b;
27542 ++processing_template_decl;
27543 b = type_dependent_expression_p (expr);
27544 --processing_template_decl;
27545 return b;
27548 /* Returns TRUE if ARGS contains a type-dependent expression. */
27550 bool
27551 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
27553 unsigned int i;
27554 tree arg;
27556 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
27558 if (type_dependent_expression_p (arg))
27559 return true;
27561 return false;
27564 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27565 expressions) contains any type-dependent expressions. */
27567 bool
27568 any_type_dependent_elements_p (const_tree list)
27570 for (; list; list = TREE_CHAIN (list))
27571 if (type_dependent_expression_p (TREE_VALUE (list)))
27572 return true;
27574 return false;
27577 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
27578 expressions) contains any value-dependent expressions. */
27580 bool
27581 any_value_dependent_elements_p (const_tree list)
27583 for (; list; list = TREE_CHAIN (list))
27584 if (value_dependent_expression_p (TREE_VALUE (list)))
27585 return true;
27587 return false;
27590 /* Returns TRUE if the ARG (a template argument) is dependent. */
27592 bool
27593 dependent_template_arg_p (tree arg)
27595 if (!processing_template_decl)
27596 return false;
27598 /* Assume a template argument that was wrongly written by the user
27599 is dependent. This is consistent with what
27600 any_dependent_template_arguments_p [that calls this function]
27601 does. */
27602 if (!arg || arg == error_mark_node)
27603 return true;
27605 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
27606 arg = argument_pack_select_arg (arg);
27608 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
27609 return true;
27610 if (TREE_CODE (arg) == TEMPLATE_DECL)
27612 if (DECL_TEMPLATE_PARM_P (arg))
27613 return true;
27614 /* A member template of a dependent class is not necessarily
27615 type-dependent, but it is a dependent template argument because it
27616 will be a member of an unknown specialization to that template. */
27617 tree scope = CP_DECL_CONTEXT (arg);
27618 return TYPE_P (scope) && dependent_type_p (scope);
27620 else if (ARGUMENT_PACK_P (arg))
27622 tree args = ARGUMENT_PACK_ARGS (arg);
27623 int i, len = TREE_VEC_LENGTH (args);
27624 for (i = 0; i < len; ++i)
27626 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
27627 return true;
27630 return false;
27632 else if (TYPE_P (arg))
27633 return dependent_type_p (arg);
27634 else
27635 return value_dependent_expression_p (arg);
27638 /* Returns true if ARGS (a collection of template arguments) contains
27639 any types that require structural equality testing. */
27641 bool
27642 any_template_arguments_need_structural_equality_p (tree args)
27644 int i;
27645 int j;
27647 if (!args)
27648 return false;
27649 if (args == error_mark_node)
27650 return true;
27652 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27654 tree level = TMPL_ARGS_LEVEL (args, i + 1);
27655 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27657 tree arg = TREE_VEC_ELT (level, j);
27658 tree packed_args = NULL_TREE;
27659 int k, len = 1;
27661 if (ARGUMENT_PACK_P (arg))
27663 /* Look inside the argument pack. */
27664 packed_args = ARGUMENT_PACK_ARGS (arg);
27665 len = TREE_VEC_LENGTH (packed_args);
27668 for (k = 0; k < len; ++k)
27670 if (packed_args)
27671 arg = TREE_VEC_ELT (packed_args, k);
27673 if (error_operand_p (arg))
27674 return true;
27675 else if (TREE_CODE (arg) == TEMPLATE_DECL)
27676 continue;
27677 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
27678 return true;
27679 else if (!TYPE_P (arg) && TREE_TYPE (arg)
27680 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
27681 return true;
27686 return false;
27689 /* Returns true if ARGS (a collection of template arguments) contains
27690 any dependent arguments. */
27692 bool
27693 any_dependent_template_arguments_p (const_tree args)
27695 int i;
27696 int j;
27698 if (!args)
27699 return false;
27700 if (args == error_mark_node)
27701 return true;
27703 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27705 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
27706 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27707 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
27708 return true;
27711 return false;
27714 /* Returns true if ARGS contains any errors. */
27716 bool
27717 any_erroneous_template_args_p (const_tree args)
27719 int i;
27720 int j;
27722 if (args == error_mark_node)
27723 return true;
27725 if (args && TREE_CODE (args) != TREE_VEC)
27727 if (tree ti = get_template_info (args))
27728 args = TI_ARGS (ti);
27729 else
27730 args = NULL_TREE;
27733 if (!args)
27734 return false;
27736 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
27738 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
27739 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
27740 if (error_operand_p (TREE_VEC_ELT (level, j)))
27741 return true;
27744 return false;
27747 /* Returns TRUE if the template TMPL is type-dependent. */
27749 bool
27750 dependent_template_p (tree tmpl)
27752 if (TREE_CODE (tmpl) == OVERLOAD)
27754 for (lkp_iterator iter (tmpl); iter; ++iter)
27755 if (dependent_template_p (*iter))
27756 return true;
27757 return false;
27760 /* Template template parameters are dependent. */
27761 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
27762 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
27763 return true;
27764 /* So are names that have not been looked up. */
27765 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
27766 return true;
27767 return false;
27770 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
27772 bool
27773 dependent_template_id_p (tree tmpl, tree args)
27775 return (dependent_template_p (tmpl)
27776 || any_dependent_template_arguments_p (args));
27779 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
27780 are dependent. */
27782 bool
27783 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
27785 int i;
27787 if (!processing_template_decl)
27788 return false;
27790 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
27792 tree decl = TREE_VEC_ELT (declv, i);
27793 tree init = TREE_VEC_ELT (initv, i);
27794 tree cond = TREE_VEC_ELT (condv, i);
27795 tree incr = TREE_VEC_ELT (incrv, i);
27797 if (type_dependent_expression_p (decl)
27798 || TREE_CODE (decl) == SCOPE_REF)
27799 return true;
27801 if (init && type_dependent_expression_p (init))
27802 return true;
27804 if (cond == global_namespace)
27805 return true;
27807 if (type_dependent_expression_p (cond))
27808 return true;
27810 if (COMPARISON_CLASS_P (cond)
27811 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
27812 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
27813 return true;
27815 if (TREE_CODE (incr) == MODOP_EXPR)
27817 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
27818 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
27819 return true;
27821 else if (type_dependent_expression_p (incr))
27822 return true;
27823 else if (TREE_CODE (incr) == MODIFY_EXPR)
27825 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
27826 return true;
27827 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
27829 tree t = TREE_OPERAND (incr, 1);
27830 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
27831 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
27832 return true;
27834 /* If this loop has a class iterator with != comparison
27835 with increment other than i++/++i/i--/--i, make sure the
27836 increment is constant. */
27837 if (CLASS_TYPE_P (TREE_TYPE (decl))
27838 && TREE_CODE (cond) == NE_EXPR)
27840 if (TREE_OPERAND (t, 0) == decl)
27841 t = TREE_OPERAND (t, 1);
27842 else
27843 t = TREE_OPERAND (t, 0);
27844 if (TREE_CODE (t) != INTEGER_CST)
27845 return true;
27851 return false;
27854 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
27855 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
27856 no such TYPE can be found. Note that this function peers inside
27857 uninstantiated templates and therefore should be used only in
27858 extremely limited situations. ONLY_CURRENT_P restricts this
27859 peering to the currently open classes hierarchy (which is required
27860 when comparing types). */
27862 tree
27863 resolve_typename_type (tree type, bool only_current_p)
27865 tree scope;
27866 tree name;
27867 tree decl;
27868 int quals;
27869 tree pushed_scope;
27870 tree result;
27872 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
27874 scope = TYPE_CONTEXT (type);
27875 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
27876 gcc_checking_assert (uses_template_parms (scope));
27878 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
27879 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of a
27880 TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL
27881 representing the typedef. In that case TYPE_IDENTIFIER (type) is
27882 not the non-qualified identifier of the TYPENAME_TYPE anymore.
27883 So by getting the TYPE_IDENTIFIER of the _main declaration_ of
27884 the TYPENAME_TYPE instead, we avoid messing up with a possible
27885 typedef variant case. */
27886 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
27888 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
27889 it first before we can figure out what NAME refers to. */
27890 if (TREE_CODE (scope) == TYPENAME_TYPE)
27892 if (TYPENAME_IS_RESOLVING_P (scope))
27893 /* Given a class template A with a dependent base with nested type C,
27894 typedef typename A::C::C C will land us here, as trying to resolve
27895 the initial A::C leads to the local C typedef, which leads back to
27896 A::C::C. So we break the recursion now. */
27897 return type;
27898 else
27899 scope = resolve_typename_type (scope, only_current_p);
27901 /* If we don't know what SCOPE refers to, then we cannot resolve the
27902 TYPENAME_TYPE. */
27903 if (!CLASS_TYPE_P (scope))
27904 return type;
27905 /* If this is a typedef, we don't want to look inside (c++/11987). */
27906 if (typedef_variant_p (type))
27907 return type;
27908 /* If SCOPE isn't the template itself, it will not have a valid
27909 TYPE_FIELDS list. */
27910 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
27911 /* scope is either the template itself or a compatible instantiation
27912 like X<T>, so look up the name in the original template. */
27913 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
27914 /* If scope has no fields, it can't be a current instantiation. Check this
27915 before currently_open_class to avoid infinite recursion (71515). */
27916 if (!TYPE_FIELDS (scope))
27917 return type;
27918 /* If the SCOPE is not the current instantiation, there's no reason
27919 to look inside it. */
27920 if (only_current_p && !currently_open_class (scope))
27921 return type;
27922 /* Enter the SCOPE so that name lookup will be resolved as if we
27923 were in the class definition. In particular, SCOPE will no
27924 longer be considered a dependent type. */
27925 pushed_scope = push_scope (scope);
27926 /* Look up the declaration. */
27927 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
27928 tf_warning_or_error);
27930 result = NULL_TREE;
27932 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
27933 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
27934 tree fullname = TYPENAME_TYPE_FULLNAME (type);
27935 if (!decl)
27936 /*nop*/;
27937 else if (identifier_p (fullname)
27938 && TREE_CODE (decl) == TYPE_DECL)
27940 result = TREE_TYPE (decl);
27941 if (result == error_mark_node)
27942 result = NULL_TREE;
27944 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
27945 && DECL_CLASS_TEMPLATE_P (decl))
27947 /* Obtain the template and the arguments. */
27948 tree tmpl = TREE_OPERAND (fullname, 0);
27949 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
27951 /* We get here with a plain identifier because a previous tentative
27952 parse of the nested-name-specifier as part of a ptr-operator saw
27953 ::template X<A>. The use of ::template is necessary in a
27954 ptr-operator, but wrong in a declarator-id.
27956 [temp.names]: In a qualified-id of a declarator-id, the keyword
27957 template shall not appear at the top level. */
27958 pedwarn (cp_expr_loc_or_input_loc (fullname), OPT_Wpedantic,
27959 "keyword %<template%> not allowed in declarator-id");
27960 tmpl = decl;
27962 tree args = TREE_OPERAND (fullname, 1);
27963 /* Instantiate the template. */
27964 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
27965 /*entering_scope=*/true,
27966 tf_error | tf_user);
27967 if (result == error_mark_node)
27968 result = NULL_TREE;
27971 /* Leave the SCOPE. */
27972 if (pushed_scope)
27973 pop_scope (pushed_scope);
27975 /* If we failed to resolve it, return the original typename. */
27976 if (!result)
27977 return type;
27979 /* If lookup found a typename type, resolve that too. */
27980 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
27982 /* Ill-formed programs can cause infinite recursion here, so we
27983 must catch that. */
27984 TYPENAME_IS_RESOLVING_P (result) = 1;
27985 result = resolve_typename_type (result, only_current_p);
27986 TYPENAME_IS_RESOLVING_P (result) = 0;
27989 /* Qualify the resulting type. */
27990 quals = cp_type_quals (type);
27991 if (quals)
27992 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
27994 return result;
27997 /* EXPR is an expression which is not type-dependent. Return a proxy
27998 for EXPR that can be used to compute the types of larger
27999 expressions containing EXPR. */
28001 tree
28002 build_non_dependent_expr (tree expr)
28004 tree orig_expr = expr;
28005 tree inner_expr;
28007 /* When checking, try to get a constant value for all non-dependent
28008 expressions in order to expose bugs in *_dependent_expression_p
28009 and constexpr. This can affect code generation, see PR70704, so
28010 only do this for -fchecking=2. */
28011 if (flag_checking > 1
28012 && cxx_dialect >= cxx11
28013 /* Don't do this during nsdmi parsing as it can lead to
28014 unexpected recursive instantiations. */
28015 && !parsing_nsdmi ()
28016 /* Don't do this during concept processing either and for
28017 the same reason. */
28018 && !processing_constraint_expression_p ())
28019 fold_non_dependent_expr (expr, tf_none);
28021 STRIP_ANY_LOCATION_WRAPPER (expr);
28023 /* Preserve OVERLOADs; the functions must be available to resolve
28024 types. */
28025 inner_expr = expr;
28026 if (TREE_CODE (inner_expr) == STMT_EXPR)
28027 inner_expr = stmt_expr_value_expr (inner_expr);
28028 if (TREE_CODE (inner_expr) == ADDR_EXPR)
28029 inner_expr = TREE_OPERAND (inner_expr, 0);
28030 if (TREE_CODE (inner_expr) == COMPONENT_REF)
28031 inner_expr = TREE_OPERAND (inner_expr, 1);
28032 if (is_overloaded_fn (inner_expr)
28033 || TREE_CODE (inner_expr) == OFFSET_REF)
28034 return orig_expr;
28035 /* There is no need to return a proxy for a variable or enumerator. */
28036 if (VAR_P (expr) || TREE_CODE (expr) == CONST_DECL)
28037 return orig_expr;
28038 /* Preserve string constants; conversions from string constants to
28039 "char *" are allowed, even though normally a "const char *"
28040 cannot be used to initialize a "char *". */
28041 if (TREE_CODE (expr) == STRING_CST)
28042 return orig_expr;
28043 /* Preserve void and arithmetic constants, as an optimization -- there is no
28044 reason to create a new node. */
28045 if (TREE_CODE (expr) == VOID_CST
28046 || TREE_CODE (expr) == INTEGER_CST
28047 || TREE_CODE (expr) == REAL_CST)
28048 return orig_expr;
28049 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
28050 There is at least one place where we want to know that a
28051 particular expression is a throw-expression: when checking a ?:
28052 expression, there are special rules if the second or third
28053 argument is a throw-expression. */
28054 if (TREE_CODE (expr) == THROW_EXPR)
28055 return orig_expr;
28057 /* Don't wrap an initializer list, we need to be able to look inside. */
28058 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
28059 return orig_expr;
28061 /* Don't wrap a dummy object, we need to be able to test for it. */
28062 if (is_dummy_object (expr))
28063 return orig_expr;
28065 if (TREE_CODE (expr) == COND_EXPR)
28066 return build3 (COND_EXPR,
28067 TREE_TYPE (expr),
28068 build_non_dependent_expr (TREE_OPERAND (expr, 0)),
28069 (TREE_OPERAND (expr, 1)
28070 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
28071 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
28072 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
28073 if (TREE_CODE (expr) == COMPOUND_EXPR
28074 && !COMPOUND_EXPR_OVERLOADED (expr))
28075 return build2 (COMPOUND_EXPR,
28076 TREE_TYPE (expr),
28077 TREE_OPERAND (expr, 0),
28078 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
28080 /* If the type is unknown, it can't really be non-dependent */
28081 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
28083 /* Otherwise, build a NON_DEPENDENT_EXPR. */
28084 return build1_loc (EXPR_LOCATION (orig_expr), NON_DEPENDENT_EXPR,
28085 TREE_TYPE (expr), expr);
28088 /* ARGS is a vector of expressions as arguments to a function call.
28089 Replace the arguments with equivalent non-dependent expressions.
28090 This modifies ARGS in place. */
28092 void
28093 make_args_non_dependent (vec<tree, va_gc> *args)
28095 unsigned int ix;
28096 tree arg;
28098 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
28100 tree newarg = build_non_dependent_expr (arg);
28101 if (newarg != arg)
28102 (*args)[ix] = newarg;
28106 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
28107 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
28108 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
28110 static tree
28111 make_auto_1 (tree name, bool set_canonical)
28113 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
28114 TYPE_NAME (au) = build_decl (input_location, TYPE_DECL, name, au);
28115 TYPE_STUB_DECL (au) = TYPE_NAME (au);
28116 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
28117 (0, processing_template_decl + 1, processing_template_decl + 1,
28118 TYPE_NAME (au), NULL_TREE);
28119 if (set_canonical)
28120 TYPE_CANONICAL (au) = canonical_type_parameter (au);
28121 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
28122 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
28123 if (name == decltype_auto_identifier)
28124 AUTO_IS_DECLTYPE (au) = true;
28126 return au;
28129 tree
28130 make_decltype_auto (void)
28132 return make_auto_1 (decltype_auto_identifier, true);
28135 tree
28136 make_auto (void)
28138 return make_auto_1 (auto_identifier, true);
28141 /* Return a C++17 deduction placeholder for class template TMPL. */
28143 tree
28144 make_template_placeholder (tree tmpl)
28146 tree t = make_auto_1 (auto_identifier, false);
28147 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
28148 /* Our canonical type depends on the placeholder. */
28149 TYPE_CANONICAL (t) = canonical_type_parameter (t);
28150 return t;
28153 /* True iff T is a C++17 class template deduction placeholder. */
28155 bool
28156 template_placeholder_p (tree t)
28158 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
28161 /* Make a "constrained auto" type-specifier. This is an auto or
28162 decltype(auto) type with constraints that must be associated after
28163 deduction. The constraint is formed from the given concept CON
28164 and its optional sequence of template arguments ARGS.
28166 TYPE must be the result of make_auto_type or make_decltype_auto_type. */
28168 static tree
28169 make_constrained_placeholder_type (tree type, tree con, tree args)
28171 /* Build the constraint. */
28172 tree tmpl = DECL_TI_TEMPLATE (con);
28173 tree expr = tmpl;
28174 if (TREE_CODE (con) == FUNCTION_DECL)
28175 expr = ovl_make (tmpl);
28176 ++processing_template_decl;
28177 expr = build_concept_check (expr, type, args, tf_warning_or_error);
28178 --processing_template_decl;
28180 PLACEHOLDER_TYPE_CONSTRAINTS_INFO (type)
28181 = build_tree_list (current_template_parms, expr);
28183 /* Our canonical type depends on the constraint. */
28184 TYPE_CANONICAL (type) = canonical_type_parameter (type);
28186 /* Attach the constraint to the type declaration. */
28187 return TYPE_NAME (type);
28190 /* Make a "constrained auto" type-specifier. */
28192 tree
28193 make_constrained_auto (tree con, tree args)
28195 tree type = make_auto_1 (auto_identifier, false);
28196 return make_constrained_placeholder_type (type, con, args);
28199 /* Make a "constrained decltype(auto)" type-specifier. */
28201 tree
28202 make_constrained_decltype_auto (tree con, tree args)
28204 tree type = make_auto_1 (decltype_auto_identifier, false);
28205 return make_constrained_placeholder_type (type, con, args);
28208 /* Returns true if the placeholder type constraint T has any dependent
28209 (explicit) template arguments. */
28211 static bool
28212 placeholder_type_constraint_dependent_p (tree t)
28214 tree id = unpack_concept_check (t);
28215 tree args = TREE_OPERAND (id, 1);
28216 tree first = TREE_VEC_ELT (args, 0);
28217 if (ARGUMENT_PACK_P (first))
28219 args = expand_template_argument_pack (args);
28220 first = TREE_VEC_ELT (args, 0);
28222 gcc_checking_assert (TREE_CODE (first) == WILDCARD_DECL
28223 || is_auto (first));
28224 for (int i = 1; i < TREE_VEC_LENGTH (args); ++i)
28225 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
28226 return true;
28227 return false;
28230 /* Build and return a concept definition. Like other templates, the
28231 CONCEPT_DECL node is wrapped by a TEMPLATE_DECL. This returns the
28232 the TEMPLATE_DECL. */
28234 tree
28235 finish_concept_definition (cp_expr id, tree init)
28237 gcc_assert (identifier_p (id));
28238 gcc_assert (processing_template_decl);
28240 location_t loc = id.get_location();
28242 /* A concept-definition shall not have associated constraints. */
28243 if (TEMPLATE_PARMS_CONSTRAINTS (current_template_parms))
28245 error_at (loc, "a concept cannot be constrained");
28246 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = NULL_TREE;
28249 /* A concept-definition shall appear in namespace scope. Templates
28250 aren't allowed in block scope, so we only need to check for class
28251 scope. */
28252 if (TYPE_P (current_scope()) || !DECL_NAMESPACE_SCOPE_P (current_scope ()))
28254 error_at (loc, "concept %qE not in namespace scope", *id);
28255 return error_mark_node;
28258 /* Initially build the concept declaration; its type is bool. */
28259 tree decl = build_lang_decl_loc (loc, CONCEPT_DECL, *id, boolean_type_node);
28260 DECL_CONTEXT (decl) = current_scope ();
28261 DECL_INITIAL (decl) = init;
28263 set_originating_module (decl, false);
28265 /* Push the enclosing template. */
28266 return push_template_decl (decl);
28269 /* Given type ARG, return std::initializer_list<ARG>. */
28271 static tree
28272 listify (tree arg)
28274 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
28276 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
28278 gcc_rich_location richloc (input_location);
28279 maybe_add_include_fixit (&richloc, "<initializer_list>", false);
28280 error_at (&richloc,
28281 "deducing from brace-enclosed initializer list"
28282 " requires %<#include <initializer_list>%>");
28284 return error_mark_node;
28286 tree argvec = make_tree_vec (1);
28287 TREE_VEC_ELT (argvec, 0) = arg;
28289 return lookup_template_class (std_init_list, argvec, NULL_TREE,
28290 NULL_TREE, 0, tf_warning_or_error);
28293 /* Replace auto in TYPE with std::initializer_list<auto>. */
28295 static tree
28296 listify_autos (tree type, tree auto_node)
28298 tree init_auto = listify (strip_top_quals (auto_node));
28299 tree argvec = make_tree_vec (1);
28300 TREE_VEC_ELT (argvec, 0) = init_auto;
28301 if (processing_template_decl)
28302 argvec = add_to_template_args (current_template_args (), argvec);
28303 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
28306 /* Hash traits for hashing possibly constrained 'auto'
28307 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
28309 struct auto_hash : default_hash_traits<tree>
28311 static inline hashval_t hash (tree);
28312 static inline bool equal (tree, tree);
28315 /* Hash the 'auto' T. */
28317 inline hashval_t
28318 auto_hash::hash (tree t)
28320 if (tree c = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (t)))
28321 /* Matching constrained-type-specifiers denote the same template
28322 parameter, so hash the constraint. */
28323 return hash_placeholder_constraint (c);
28324 else
28325 /* But unconstrained autos are all separate, so just hash the pointer. */
28326 return iterative_hash_object (t, 0);
28329 /* Compare two 'auto's. */
28331 inline bool
28332 auto_hash::equal (tree t1, tree t2)
28334 if (t1 == t2)
28335 return true;
28337 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
28338 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
28340 /* Two unconstrained autos are distinct. */
28341 if (!c1 || !c2)
28342 return false;
28344 return equivalent_placeholder_constraints (c1, c2);
28347 /* for_each_template_parm callback for extract_autos: if t is a (possibly
28348 constrained) auto, add it to the vector. */
28350 static int
28351 extract_autos_r (tree t, void *data)
28353 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
28354 if (is_auto (t))
28356 /* All the autos were built with index 0; fix that up now. */
28357 tree *p = hash.find_slot (t, INSERT);
28358 unsigned idx;
28359 if (*p)
28360 /* If this is a repeated constrained-type-specifier, use the index we
28361 chose before. */
28362 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
28363 else
28365 /* Otherwise this is new, so use the current count. */
28366 *p = t;
28367 idx = hash.elements () - 1;
28369 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
28372 /* Always keep walking. */
28373 return 0;
28376 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
28377 says they can appear anywhere in the type. */
28379 static tree
28380 extract_autos (tree type)
28382 hash_set<tree> visited;
28383 hash_table<auto_hash> hash (2);
28385 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
28387 tree tree_vec = make_tree_vec (hash.elements());
28388 for (hash_table<auto_hash>::iterator iter = hash.begin();
28389 iter != hash.end(); ++iter)
28391 tree elt = *iter;
28392 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
28393 TREE_VEC_ELT (tree_vec, i)
28394 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
28397 return tree_vec;
28400 /* The stem for deduction guide names. */
28401 const char *const dguide_base = "__dguide_";
28403 /* Return the name for a deduction guide for class template TMPL. */
28405 tree
28406 dguide_name (tree tmpl)
28408 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
28409 tree tname = TYPE_IDENTIFIER (type);
28410 char *buf = (char *) alloca (1 + strlen (dguide_base)
28411 + IDENTIFIER_LENGTH (tname));
28412 memcpy (buf, dguide_base, strlen (dguide_base));
28413 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
28414 IDENTIFIER_LENGTH (tname) + 1);
28415 tree dname = get_identifier (buf);
28416 TREE_TYPE (dname) = type;
28417 return dname;
28420 /* True if NAME is the name of a deduction guide. */
28422 bool
28423 dguide_name_p (tree name)
28425 return (TREE_CODE (name) == IDENTIFIER_NODE
28426 && TREE_TYPE (name)
28427 && startswith (IDENTIFIER_POINTER (name), dguide_base));
28430 /* True if FN is a deduction guide. */
28432 bool
28433 deduction_guide_p (const_tree fn)
28435 if (DECL_P (fn))
28436 if (tree name = DECL_NAME (fn))
28437 return dguide_name_p (name);
28438 return false;
28441 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
28443 bool
28444 copy_guide_p (const_tree fn)
28446 gcc_assert (deduction_guide_p (fn));
28447 if (!DECL_ARTIFICIAL (fn))
28448 return false;
28449 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
28450 return (TREE_CHAIN (parms) == void_list_node
28451 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
28454 /* True if FN is a guide generated from a constructor template. */
28456 bool
28457 template_guide_p (const_tree fn)
28459 gcc_assert (deduction_guide_p (fn));
28460 if (!DECL_ARTIFICIAL (fn))
28461 return false;
28462 tree tmpl = DECL_TI_TEMPLATE (fn);
28463 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
28464 return PRIMARY_TEMPLATE_P (org);
28465 return false;
28468 /* True if FN is an aggregate initialization guide or the copy deduction
28469 guide. */
28471 bool
28472 builtin_guide_p (const_tree fn)
28474 if (!deduction_guide_p (fn))
28475 return false;
28476 if (!DECL_ARTIFICIAL (fn))
28477 /* Explicitly declared. */
28478 return false;
28479 if (DECL_ABSTRACT_ORIGIN (fn))
28480 /* Derived from a constructor. */
28481 return false;
28482 return true;
28485 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
28486 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
28487 template parameter types. Note that the handling of template template
28488 parameters relies on current_template_parms being set appropriately for the
28489 new template. */
28491 static tree
28492 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
28493 tree tsubst_args, tsubst_flags_t complain)
28495 if (olddecl == error_mark_node)
28496 return error_mark_node;
28498 tree oldidx = get_template_parm_index (olddecl);
28500 tree newtype;
28501 if (TREE_CODE (olddecl) == TYPE_DECL
28502 || TREE_CODE (olddecl) == TEMPLATE_DECL)
28504 tree oldtype = TREE_TYPE (olddecl);
28505 newtype = cxx_make_type (TREE_CODE (oldtype));
28506 TYPE_MAIN_VARIANT (newtype) = newtype;
28507 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
28508 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
28509 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
28511 else
28513 newtype = TREE_TYPE (olddecl);
28514 if (type_uses_auto (newtype))
28516 // Substitute once to fix references to other template parameters.
28517 newtype = tsubst (newtype, tsubst_args,
28518 complain|tf_partial, NULL_TREE);
28519 // Now substitute again to reduce the level of the auto.
28520 newtype = tsubst (newtype, current_template_args (),
28521 complain, NULL_TREE);
28523 else
28524 newtype = tsubst (newtype, tsubst_args,
28525 complain, NULL_TREE);
28528 tree newdecl
28529 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
28530 DECL_NAME (olddecl), newtype);
28531 SET_DECL_TEMPLATE_PARM_P (newdecl);
28533 tree newidx;
28534 if (TREE_CODE (olddecl) == TYPE_DECL
28535 || TREE_CODE (olddecl) == TEMPLATE_DECL)
28537 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
28538 = build_template_parm_index (index, level, level,
28539 newdecl, newtype);
28540 TEMPLATE_PARM_PARAMETER_PACK (newidx)
28541 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
28542 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
28543 if (TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (olddecl)))
28544 SET_TYPE_STRUCTURAL_EQUALITY (newtype);
28545 else
28546 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
28548 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
28550 DECL_TEMPLATE_RESULT (newdecl)
28551 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
28552 DECL_NAME (olddecl), newtype);
28553 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
28554 // First create a copy (ttargs) of tsubst_args with an
28555 // additional level for the template template parameter's own
28556 // template parameters (ttparms).
28557 tree ttparms = (INNERMOST_TEMPLATE_PARMS
28558 (DECL_TEMPLATE_PARMS (olddecl)));
28559 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
28560 tree ttargs = make_tree_vec (depth + 1);
28561 for (int i = 0; i < depth; ++i)
28562 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
28563 TREE_VEC_ELT (ttargs, depth)
28564 = template_parms_level_to_args (ttparms);
28565 // Substitute ttargs into ttparms to fix references to
28566 // other template parameters.
28567 ttparms = tsubst_template_parms_level (ttparms, ttargs,
28568 complain|tf_partial);
28569 // Now substitute again with args based on tparms, to reduce
28570 // the level of the ttparms.
28571 ttargs = current_template_args ();
28572 ttparms = tsubst_template_parms_level (ttparms, ttargs,
28573 complain);
28574 // Finally, tack the adjusted parms onto tparms.
28575 ttparms = tree_cons (size_int (depth), ttparms,
28576 current_template_parms);
28577 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
28580 else
28582 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
28583 tree newconst
28584 = build_decl (DECL_SOURCE_LOCATION (oldconst),
28585 TREE_CODE (oldconst),
28586 DECL_NAME (oldconst), newtype);
28587 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
28588 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
28589 SET_DECL_TEMPLATE_PARM_P (newconst);
28590 newidx = build_template_parm_index (index, level, level,
28591 newconst, newtype);
28592 TEMPLATE_PARM_PARAMETER_PACK (newidx)
28593 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
28594 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
28597 return newdecl;
28600 /* As rewrite_template_parm, but for the whole TREE_LIST representing a
28601 template parameter. */
28603 static tree
28604 rewrite_tparm_list (tree oldelt, unsigned index, unsigned level,
28605 tree targs, unsigned targs_index, tsubst_flags_t complain)
28607 tree olddecl = TREE_VALUE (oldelt);
28608 tree newdecl = rewrite_template_parm (olddecl, index, level,
28609 targs, complain);
28610 if (newdecl == error_mark_node)
28611 return error_mark_node;
28612 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
28613 targs, complain, NULL_TREE);
28614 tree list = build_tree_list (newdef, newdecl);
28615 TEMPLATE_PARM_CONSTRAINTS (list)
28616 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
28617 targs, complain, NULL_TREE);
28618 int depth = TMPL_ARGS_DEPTH (targs);
28619 TMPL_ARG (targs, depth, targs_index) = template_parm_to_arg (list);
28620 return list;
28623 /* Returns a C++17 class deduction guide template based on the constructor
28624 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
28625 guide, REFERENCE_TYPE for an implicit copy/move guide, or TREE_LIST for an
28626 aggregate initialization guide. OUTER_ARGS are the template arguments
28627 for the enclosing scope of the class. */
28629 static tree
28630 build_deduction_guide (tree type, tree ctor, tree outer_args, tsubst_flags_t complain)
28632 tree tparms, targs, fparms, fargs, ci;
28633 bool memtmpl = false;
28634 bool explicit_p;
28635 location_t loc;
28636 tree fn_tmpl = NULL_TREE;
28638 if (outer_args)
28640 ++processing_template_decl;
28641 type = tsubst (type, outer_args, complain, CLASSTYPE_TI_TEMPLATE (type));
28642 --processing_template_decl;
28645 if (!DECL_DECLARES_FUNCTION_P (ctor))
28647 if (TYPE_P (ctor))
28649 bool copy_p = TYPE_REF_P (ctor);
28650 if (copy_p)
28651 fparms = tree_cons (NULL_TREE, type, void_list_node);
28652 else
28653 fparms = void_list_node;
28655 else if (TREE_CODE (ctor) == TREE_LIST)
28656 fparms = ctor;
28657 else
28658 gcc_unreachable ();
28660 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
28661 tparms = DECL_TEMPLATE_PARMS (ctmpl);
28662 targs = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
28663 ci = NULL_TREE;
28664 fargs = NULL_TREE;
28665 loc = DECL_SOURCE_LOCATION (ctmpl);
28666 explicit_p = false;
28668 else
28670 ++processing_template_decl;
28671 bool ok = true;
28673 fn_tmpl
28674 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
28675 : DECL_TI_TEMPLATE (ctor));
28676 if (outer_args)
28677 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
28678 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
28680 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
28681 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
28682 fully specialized args for the enclosing class. Strip those off, as
28683 the deduction guide won't have those template parameters. */
28684 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
28685 TMPL_PARMS_DEPTH (tparms));
28686 /* Discard the 'this' parameter. */
28687 fparms = FUNCTION_ARG_CHAIN (ctor);
28688 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
28689 ci = get_constraints (ctor);
28690 loc = DECL_SOURCE_LOCATION (ctor);
28691 explicit_p = DECL_NONCONVERTING_P (ctor);
28693 if (PRIMARY_TEMPLATE_P (fn_tmpl))
28695 memtmpl = true;
28697 /* For a member template constructor, we need to flatten the two
28698 template parameter lists into one, and then adjust the function
28699 signature accordingly. This gets...complicated. */
28700 tree save_parms = current_template_parms;
28702 /* For a member template we should have two levels of parms/args, one
28703 for the class and one for the constructor. We stripped
28704 specialized args for further enclosing classes above. */
28705 const int depth = 2;
28706 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
28708 /* Template args for translating references to the two-level template
28709 parameters into references to the one-level template parameters we
28710 are creating. */
28711 tree tsubst_args = copy_node (targs);
28712 TMPL_ARGS_LEVEL (tsubst_args, depth)
28713 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
28715 /* Template parms for the constructor template. */
28716 tree ftparms = TREE_VALUE (tparms);
28717 unsigned flen = TREE_VEC_LENGTH (ftparms);
28718 /* Template parms for the class template. */
28719 tparms = TREE_CHAIN (tparms);
28720 tree ctparms = TREE_VALUE (tparms);
28721 unsigned clen = TREE_VEC_LENGTH (ctparms);
28722 /* Template parms for the deduction guide start as a copy of the
28723 template parms for the class. We set current_template_parms for
28724 lookup_template_class_1. */
28725 current_template_parms = tparms = copy_node (tparms);
28726 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
28727 for (unsigned i = 0; i < clen; ++i)
28728 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
28730 /* Now we need to rewrite the constructor parms to append them to the
28731 class parms. */
28732 for (unsigned i = 0; i < flen; ++i)
28734 unsigned index = i + clen;
28735 unsigned level = 1;
28736 tree oldelt = TREE_VEC_ELT (ftparms, i);
28737 tree newelt
28738 = rewrite_tparm_list (oldelt, index, level,
28739 tsubst_args, i, complain);
28740 if (newelt == error_mark_node)
28741 ok = false;
28742 TREE_VEC_ELT (new_vec, index) = newelt;
28745 /* Now we have a final set of template parms to substitute into the
28746 function signature. */
28747 targs = template_parms_to_args (tparms);
28748 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
28749 complain, ctor);
28750 if (fparms == error_mark_node)
28751 ok = false;
28752 if (ci)
28754 if (outer_args)
28755 /* FIXME: We'd like to avoid substituting outer template
28756 arguments into the constraint ahead of time, but the
28757 construction of tsubst_args assumes that outer arguments
28758 are already substituted in. */
28759 ci = tsubst_constraint_info (ci, outer_args, complain, ctor);
28760 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
28763 /* Parms are to have DECL_CHAIN tsubsted, which would be skipped if
28764 cp_unevaluated_operand. */
28765 cp_evaluated ev;
28766 fargs = tsubst (fargs, tsubst_args, complain, ctor);
28767 current_template_parms = save_parms;
28769 else
28771 /* Substitute in the same arguments to rewrite class members into
28772 references to members of an unknown specialization. */
28773 cp_evaluated ev;
28774 fparms = tsubst_arg_types (fparms, targs, NULL_TREE, complain, ctor);
28775 fargs = tsubst (fargs, targs, complain, ctor);
28776 if (ci)
28778 if (outer_args)
28779 /* FIXME: As above. */
28780 ci = tsubst_constraint_info (ci, outer_args, complain, ctor);
28781 ci = tsubst_constraint_info (ci, targs, complain, ctor);
28785 --processing_template_decl;
28786 if (!ok)
28787 return error_mark_node;
28790 if (!memtmpl)
28792 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
28793 tparms = copy_node (tparms);
28794 INNERMOST_TEMPLATE_PARMS (tparms)
28795 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
28798 tree fntype = build_function_type (type, fparms);
28799 tree ded_fn = build_lang_decl_loc (loc,
28800 FUNCTION_DECL,
28801 dguide_name (type), fntype);
28802 DECL_ARGUMENTS (ded_fn) = fargs;
28803 DECL_ARTIFICIAL (ded_fn) = true;
28804 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
28805 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
28806 DECL_ARTIFICIAL (ded_tmpl) = true;
28807 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
28808 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
28809 if (DECL_P (ctor))
28810 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
28811 if (ci)
28812 set_constraints (ded_tmpl, ci);
28814 return ded_tmpl;
28817 /* Add to LIST the member types for the reshaped initializer CTOR. */
28819 static tree
28820 collect_ctor_idx_types (tree ctor, tree list, tree elt = NULL_TREE)
28822 vec<constructor_elt, va_gc> *v = CONSTRUCTOR_ELTS (ctor);
28823 tree idx, val; unsigned i;
28824 FOR_EACH_CONSTRUCTOR_ELT (v, i, idx, val)
28826 tree ftype = elt ? elt : TREE_TYPE (idx);
28827 if (BRACE_ENCLOSED_INITIALIZER_P (val)
28828 && CONSTRUCTOR_NELTS (val)
28829 /* As in reshape_init_r, a non-aggregate or array-of-dependent-bound
28830 type gets a single initializer. */
28831 && CP_AGGREGATE_TYPE_P (ftype)
28832 && !(TREE_CODE (ftype) == ARRAY_TYPE
28833 && uses_template_parms (TYPE_DOMAIN (ftype))))
28835 tree subelt = NULL_TREE;
28836 if (TREE_CODE (ftype) == ARRAY_TYPE)
28837 subelt = TREE_TYPE (ftype);
28838 list = collect_ctor_idx_types (val, list, subelt);
28839 continue;
28841 tree arg = NULL_TREE;
28842 if (i == v->length() - 1
28843 && PACK_EXPANSION_P (ftype))
28844 /* Give the trailing pack expansion parameter a default argument to
28845 match aggregate initialization behavior, even if we deduce the
28846 length of the pack separately to more than we have initializers. */
28847 arg = build_constructor (init_list_type_node, NULL);
28848 /* if ei is of array type and xi is a braced-init-list or string literal,
28849 Ti is an rvalue reference to the declared type of ei */
28850 STRIP_ANY_LOCATION_WRAPPER (val);
28851 if (TREE_CODE (ftype) == ARRAY_TYPE
28852 && (BRACE_ENCLOSED_INITIALIZER_P (val)
28853 || TREE_CODE (val) == STRING_CST))
28855 if (TREE_CODE (val) == STRING_CST)
28856 ftype = cp_build_qualified_type
28857 (ftype, cp_type_quals (ftype) | TYPE_QUAL_CONST);
28858 ftype = (cp_build_reference_type
28859 (ftype, BRACE_ENCLOSED_INITIALIZER_P (val)));
28861 list = tree_cons (arg, ftype, list);
28864 return list;
28867 /* Return whether ETYPE is, or is derived from, a specialization of TMPL. */
28869 static bool
28870 is_spec_or_derived (tree etype, tree tmpl)
28872 if (!etype || !CLASS_TYPE_P (etype))
28873 return false;
28875 etype = cv_unqualified (etype);
28876 tree type = TREE_TYPE (tmpl);
28877 tree tparms = (INNERMOST_TEMPLATE_PARMS
28878 (DECL_TEMPLATE_PARMS (tmpl)));
28879 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
28880 int err = unify (tparms, targs, type, etype,
28881 UNIFY_ALLOW_DERIVED, /*explain*/false);
28882 ggc_free (targs);
28883 return !err;
28886 /* Return a C++20 aggregate deduction candidate for TYPE initialized from
28887 INIT. */
28889 static tree
28890 maybe_aggr_guide (tree tmpl, tree init, vec<tree,va_gc> *args)
28892 if (cxx_dialect < cxx20)
28893 return NULL_TREE;
28895 if (init == NULL_TREE)
28896 return NULL_TREE;
28898 /* We might be creating a guide for a class member template, e.g.,
28900 template<typename U> struct A {
28901 template<typename T> struct B { T t; };
28904 At this point, A will have been instantiated. Below, we need to
28905 use both A<U>::B<T> (TEMPLATE_TYPE) and A<int>::B<T> (TYPE) types. */
28906 const bool member_template_p
28907 = (DECL_TEMPLATE_INFO (tmpl)
28908 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (tmpl)));
28909 tree type = TREE_TYPE (tmpl);
28910 tree template_type = (member_template_p
28911 ? TREE_TYPE (DECL_TI_TEMPLATE (tmpl))
28912 : type);
28913 if (!CP_AGGREGATE_TYPE_P (template_type))
28914 return NULL_TREE;
28916 /* No aggregate candidate for copy-initialization. */
28917 if (args->length() == 1)
28919 tree val = (*args)[0];
28920 if (is_spec_or_derived (TREE_TYPE (val), tmpl))
28921 return NULL_TREE;
28924 /* If we encounter a problem, we just won't add the candidate. */
28925 tsubst_flags_t complain = tf_none;
28927 tree parms = NULL_TREE;
28928 if (BRACE_ENCLOSED_INITIALIZER_P (init))
28930 init = reshape_init (template_type, init, complain);
28931 if (init == error_mark_node)
28932 return NULL_TREE;
28933 parms = collect_ctor_idx_types (init, parms);
28934 /* If we're creating a deduction guide for a member class template,
28935 we've used the original template pattern type for the reshape_init
28936 above; this is done because we want PARMS to be a template parameter
28937 type, something that can be deduced when used as a function template
28938 parameter. At this point the outer class template has already been
28939 partially instantiated (we deferred the deduction until the enclosing
28940 scope is non-dependent). Therefore we have to partially instantiate
28941 PARMS, so that its template level is properly reduced and we don't get
28942 mismatches when deducing types using the guide with PARMS. */
28943 if (member_template_p)
28944 parms = tsubst (parms, DECL_TI_ARGS (tmpl), complain, init);
28946 else if (TREE_CODE (init) == TREE_LIST)
28948 int len = list_length (init);
28949 for (tree field = TYPE_FIELDS (type);
28950 len;
28951 --len, field = DECL_CHAIN (field))
28953 field = next_initializable_field (field);
28954 if (!field)
28955 return NULL_TREE;
28956 tree ftype = finish_decltype_type (field, true, complain);
28957 parms = tree_cons (NULL_TREE, ftype, parms);
28960 else
28961 /* Aggregate initialization doesn't apply to an initializer expression. */
28962 return NULL_TREE;
28964 if (parms)
28966 tree last = parms;
28967 parms = nreverse (parms);
28968 TREE_CHAIN (last) = void_list_node;
28969 tree guide = build_deduction_guide (type, parms, NULL_TREE, complain);
28970 return guide;
28973 return NULL_TREE;
28976 /* UGUIDES are the deduction guides for the underlying template of alias
28977 template TMPL; adjust them to be deduction guides for TMPL. */
28979 static tree
28980 alias_ctad_tweaks (tree tmpl, tree uguides)
28982 /* [over.match.class.deduct]: When resolving a placeholder for a deduced
28983 class type (9.2.8.2) where the template-name names an alias template A,
28984 the defining-type-id of A must be of the form
28986 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
28988 as specified in 9.2.8.2. The guides of A are the set of functions or
28989 function templates formed as follows. For each function or function
28990 template f in the guides of the template named by the simple-template-id
28991 of the defining-type-id, the template arguments of the return type of f
28992 are deduced from the defining-type-id of A according to the process in
28993 13.10.2.5 with the exception that deduction does not fail if not all
28994 template arguments are deduced. Let g denote the result of substituting
28995 these deductions into f. If substitution succeeds, form a function or
28996 function template f' with the following properties and add it to the set
28997 of guides of A:
28999 * The function type of f' is the function type of g.
29001 * If f is a function template, f' is a function template whose template
29002 parameter list consists of all the template parameters of A (including
29003 their default template arguments) that appear in the above deductions or
29004 (recursively) in their default template arguments, followed by the
29005 template parameters of f that were not deduced (including their default
29006 template arguments), otherwise f' is not a function template.
29008 * The associated constraints (13.5.2) are the conjunction of the
29009 associated constraints of g and a constraint that is satisfied if and only
29010 if the arguments of A are deducible (see below) from the return type.
29012 * If f is a copy deduction candidate (12.4.1.8), then f' is considered to
29013 be so as well.
29015 * If f was generated from a deduction-guide (12.4.1.8), then f' is
29016 considered to be so as well.
29018 * The explicit-specifier of f' is the explicit-specifier of g (if
29019 any). */
29021 /* This implementation differs from the above in two significant ways:
29023 1) We include all template parameters of A, not just some.
29024 2) The added constraint is same_type instead of deducible.
29026 I believe that while it's probably possible to construct a testcase that
29027 behaves differently with this simplification, it should have the same
29028 effect for real uses. Including all template parameters means that we
29029 deduce all parameters of A when resolving the call, so when we're in the
29030 constraint we don't need to deduce them again, we can just check whether
29031 the deduction produced the desired result. */
29033 tsubst_flags_t complain = tf_warning_or_error;
29034 tree atype = TREE_TYPE (tmpl);
29035 tree aguides = NULL_TREE;
29036 tree atparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
29037 unsigned natparms = TREE_VEC_LENGTH (atparms);
29038 tree utype = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
29039 for (ovl_iterator iter (uguides); iter; ++iter)
29041 tree f = *iter;
29042 tree in_decl = f;
29043 location_t loc = DECL_SOURCE_LOCATION (f);
29044 tree ret = TREE_TYPE (TREE_TYPE (f));
29045 tree fprime = f;
29046 if (TREE_CODE (f) == TEMPLATE_DECL)
29048 processing_template_decl_sentinel ptds (/*reset*/false);
29049 ++processing_template_decl;
29051 /* Deduce template arguments for f from the type-id of A. */
29052 tree ftparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (f));
29053 unsigned len = TREE_VEC_LENGTH (ftparms);
29054 tree targs = make_tree_vec (len);
29055 int err = unify (ftparms, targs, ret, utype, UNIFY_ALLOW_NONE, false);
29056 if (err)
29057 continue;
29059 /* The number of parms for f' is the number of parms for A plus
29060 non-deduced parms of f. */
29061 unsigned ndlen = 0;
29062 unsigned j;
29063 for (unsigned i = 0; i < len; ++i)
29064 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
29065 ++ndlen;
29066 tree gtparms = make_tree_vec (natparms + ndlen);
29068 /* First copy over the parms of A. */
29069 for (j = 0; j < natparms; ++j)
29070 TREE_VEC_ELT (gtparms, j) = TREE_VEC_ELT (atparms, j);
29071 /* Now rewrite the non-deduced parms of f. */
29072 for (unsigned i = 0; ndlen && i < len; ++i)
29073 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
29075 --ndlen;
29076 unsigned index = j++;
29077 unsigned level = 1;
29078 tree oldlist = TREE_VEC_ELT (ftparms, i);
29079 tree list = rewrite_tparm_list (oldlist, index, level,
29080 targs, i, complain);
29081 TREE_VEC_ELT (gtparms, index) = list;
29083 gtparms = build_tree_list (size_one_node, gtparms);
29085 /* Substitute the deduced arguments plus the rewritten template
29086 parameters into f to get g. This covers the type, copyness,
29087 guideness, and explicit-specifier. */
29088 tree g = tsubst_decl (DECL_TEMPLATE_RESULT (f), targs, complain);
29089 if (g == error_mark_node)
29090 continue;
29091 DECL_USE_TEMPLATE (g) = 0;
29092 fprime = build_template_decl (g, gtparms, false);
29093 DECL_TEMPLATE_RESULT (fprime) = g;
29094 TREE_TYPE (fprime) = TREE_TYPE (g);
29095 tree gtargs = template_parms_to_args (gtparms);
29096 DECL_TEMPLATE_INFO (g) = build_template_info (fprime, gtargs);
29097 DECL_PRIMARY_TEMPLATE (fprime) = fprime;
29099 /* Substitute the associated constraints. */
29100 tree ci = get_constraints (f);
29101 if (ci)
29102 ci = tsubst_constraint_info (ci, targs, complain, in_decl);
29103 if (ci == error_mark_node)
29104 continue;
29106 /* Add a constraint that the return type matches the instantiation of
29107 A with the same template arguments. */
29108 ret = TREE_TYPE (TREE_TYPE (fprime));
29109 if (!same_type_p (atype, ret)
29110 /* FIXME this should mean they don't compare as equivalent. */
29111 || dependent_alias_template_spec_p (atype, nt_opaque))
29113 tree same = finish_trait_expr (loc, CPTK_IS_SAME_AS, atype, ret);
29114 ci = append_constraint (ci, same);
29117 if (ci)
29119 remove_constraints (fprime);
29120 set_constraints (fprime, ci);
29123 else
29125 /* For a non-template deduction guide, if the arguments of A aren't
29126 deducible from the return type, don't add the candidate. */
29127 tree targs = make_tree_vec (natparms);
29128 int err = unify (atparms, targs, utype, ret, UNIFY_ALLOW_NONE, false);
29129 for (unsigned i = 0; !err && i < natparms; ++i)
29130 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
29131 err = true;
29132 if (err)
29133 continue;
29136 aguides = lookup_add (fprime, aguides);
29139 return aguides;
29142 /* Return artificial deduction guides built from the constructors of class
29143 template TMPL. */
29145 static tree
29146 ctor_deduction_guides_for (tree tmpl, tsubst_flags_t complain)
29148 tree type = TREE_TYPE (tmpl);
29149 tree outer_args = NULL_TREE;
29150 if (DECL_CLASS_SCOPE_P (tmpl)
29151 && CLASSTYPE_TEMPLATE_INSTANTIATION (DECL_CONTEXT (tmpl)))
29153 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
29154 type = TREE_TYPE (most_general_template (tmpl));
29157 tree cands = NULL_TREE;
29159 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
29161 /* Skip inherited constructors. */
29162 if (iter.using_p ())
29163 continue;
29165 tree guide = build_deduction_guide (type, *iter, outer_args, complain);
29166 cands = lookup_add (guide, cands);
29169 /* Add implicit default constructor deduction guide. */
29170 if (!TYPE_HAS_USER_CONSTRUCTOR (type))
29172 tree guide = build_deduction_guide (type, type, outer_args,
29173 complain);
29174 cands = lookup_add (guide, cands);
29177 /* Add copy guide. */
29179 tree gtype = build_reference_type (type);
29180 tree guide = build_deduction_guide (type, gtype, outer_args,
29181 complain);
29182 cands = lookup_add (guide, cands);
29185 return cands;
29188 static GTY((deletable)) hash_map<tree, tree_pair_p> *dguide_cache;
29190 /* Return the non-aggregate deduction guides for deducible template TMPL. The
29191 aggregate candidate is added separately because it depends on the
29192 initializer. Set ANY_DGUIDES_P if we find a non-implicit deduction
29193 guide. */
29195 static tree
29196 deduction_guides_for (tree tmpl, bool &any_dguides_p, tsubst_flags_t complain)
29198 tree guides = NULL_TREE;
29199 if (DECL_ALIAS_TEMPLATE_P (tmpl))
29201 tree under = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
29202 tree tinfo = get_template_info (under);
29203 guides = deduction_guides_for (TI_TEMPLATE (tinfo), any_dguides_p,
29204 complain);
29206 else
29208 guides = lookup_qualified_name (CP_DECL_CONTEXT (tmpl),
29209 dguide_name (tmpl),
29210 LOOK_want::NORMAL, /*complain*/false);
29211 if (guides == error_mark_node)
29212 guides = NULL_TREE;
29213 else
29214 any_dguides_p = true;
29217 /* Cache the deduction guides for a template. We also remember the result of
29218 lookup, and rebuild everything if it changes; should be very rare. */
29219 tree_pair_p cache = NULL;
29220 if (tree_pair_p &r
29221 = hash_map_safe_get_or_insert<hm_ggc> (dguide_cache, tmpl))
29223 cache = r;
29224 if (cache->purpose == guides)
29225 return cache->value;
29227 else
29229 r = cache = ggc_cleared_alloc<tree_pair_s> ();
29230 cache->purpose = guides;
29233 tree cands = NULL_TREE;
29234 if (DECL_ALIAS_TEMPLATE_P (tmpl))
29235 cands = alias_ctad_tweaks (tmpl, guides);
29236 else
29238 cands = ctor_deduction_guides_for (tmpl, complain);
29239 for (ovl_iterator it (guides); it; ++it)
29240 cands = lookup_add (*it, cands);
29243 cache->value = cands;
29244 return cands;
29247 /* Return whether TMPL is a (class template argument-) deducible template. */
29249 bool
29250 ctad_template_p (tree tmpl)
29252 /* A deducible template is either a class template or is an alias template
29253 whose defining-type-id is of the form
29255 typename(opt) nested-name-specifier(opt) template(opt) simple-template-id
29257 where the nested-name-specifier (if any) is non-dependent and the
29258 template-name of the simple-template-id names a deducible template. */
29260 if (DECL_CLASS_TEMPLATE_P (tmpl)
29261 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
29262 return true;
29263 if (!DECL_ALIAS_TEMPLATE_P (tmpl))
29264 return false;
29265 tree orig = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
29266 if (tree tinfo = get_template_info (orig))
29267 return ctad_template_p (TI_TEMPLATE (tinfo));
29268 return false;
29271 /* Deduce template arguments for the class template placeholder PTYPE for
29272 template TMPL based on the initializer INIT, and return the resulting
29273 type. */
29275 static tree
29276 do_class_deduction (tree ptype, tree tmpl, tree init,
29277 int flags, tsubst_flags_t complain)
29279 /* We should have handled this in the caller. */
29280 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
29281 return ptype;
29283 /* Wait until the enclosing scope is non-dependent. */
29284 if (DECL_CLASS_SCOPE_P (tmpl)
29285 && dependent_type_p (DECL_CONTEXT (tmpl)))
29286 return ptype;
29288 /* Initializing one placeholder from another. */
29289 if (init
29290 && (TREE_CODE (init) == TEMPLATE_PARM_INDEX
29291 || (TREE_CODE (init) == EXPR_PACK_EXPANSION
29292 && (TREE_CODE (PACK_EXPANSION_PATTERN (init))
29293 == TEMPLATE_PARM_INDEX)))
29294 && is_auto (TREE_TYPE (init))
29295 && CLASS_PLACEHOLDER_TEMPLATE (TREE_TYPE (init)) == tmpl)
29296 return cp_build_qualified_type (TREE_TYPE (init), cp_type_quals (ptype));
29298 /* Look through alias templates that just rename another template. */
29299 tmpl = get_underlying_template (tmpl);
29300 if (!ctad_template_p (tmpl))
29302 if (complain & tf_error)
29303 error ("non-deducible template %qT used without template arguments", tmpl);
29304 return error_mark_node;
29306 else if (cxx_dialect < cxx20 && DECL_ALIAS_TEMPLATE_P (tmpl))
29308 if (complain & tf_error)
29309 error ("alias template deduction only available "
29310 "with %<-std=c++20%> or %<-std=gnu++20%>");
29311 return error_mark_node;
29314 /* Wait until the initializer is non-dependent. */
29315 if (type_dependent_expression_p (init))
29316 return ptype;
29318 tree type = TREE_TYPE (tmpl);
29320 bool try_list_ctor = false;
29321 bool list_init_p = false;
29323 releasing_vec rv_args = NULL;
29324 vec<tree,va_gc> *&args = *&rv_args;
29325 if (init == NULL_TREE)
29326 args = make_tree_vector ();
29327 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
29329 list_init_p = true;
29330 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
29331 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1
29332 && !CONSTRUCTOR_IS_DESIGNATED_INIT (init))
29334 /* As an exception, the first phase in 16.3.1.7 (considering the
29335 initializer list as a single argument) is omitted if the
29336 initializer list consists of a single expression of type cv U,
29337 where U is a specialization of C or a class derived from a
29338 specialization of C. */
29339 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
29340 if (is_spec_or_derived (TREE_TYPE (elt), tmpl))
29341 try_list_ctor = false;
29343 if (try_list_ctor || is_std_init_list (type))
29344 args = make_tree_vector_single (init);
29345 else
29346 args = make_tree_vector_from_ctor (init);
29348 else if (TREE_CODE (init) == TREE_LIST)
29349 args = make_tree_vector_from_list (init);
29350 else
29351 args = make_tree_vector_single (init);
29353 /* Do this now to avoid problems with erroneous args later on. */
29354 args = resolve_args (args, complain);
29355 if (args == NULL)
29356 return error_mark_node;
29358 bool any_dguides_p = false;
29359 tree cands = deduction_guides_for (tmpl, any_dguides_p, complain);
29360 if (cands == error_mark_node)
29361 return error_mark_node;
29363 /* Prune explicit deduction guides in copy-initialization context (but
29364 not copy-list-initialization). */
29365 bool elided = false;
29366 if (!list_init_p && (flags & LOOKUP_ONLYCONVERTING))
29368 for (lkp_iterator iter (cands); !elided && iter; ++iter)
29369 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
29370 elided = true;
29372 if (elided)
29374 /* Found a nonconverting guide, prune the candidates. */
29375 tree pruned = NULL_TREE;
29376 for (lkp_iterator iter (cands); iter; ++iter)
29377 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
29378 pruned = lookup_add (*iter, pruned);
29380 cands = pruned;
29384 if (!any_dguides_p)
29385 if (tree guide = maybe_aggr_guide (tmpl, init, args))
29386 cands = lookup_add (guide, cands);
29388 tree fndecl = error_mark_node;
29390 /* If this is list-initialization and the class has a list constructor, first
29391 try deducing from the list as a single argument, as [over.match.list]. */
29392 tree list_cands = NULL_TREE;
29393 if (try_list_ctor && cands)
29394 for (lkp_iterator iter (cands); iter; ++iter)
29396 tree dg = *iter;
29397 if (is_list_ctor (dg))
29398 list_cands = lookup_add (dg, list_cands);
29400 if (list_cands)
29402 fndecl = perform_dguide_overload_resolution (list_cands, args, tf_none);
29404 if (fndecl == error_mark_node)
29406 /* That didn't work, now try treating the list as a sequence of
29407 arguments. */
29408 release_tree_vector (args);
29409 args = make_tree_vector_from_ctor (init);
29413 if (elided && !cands)
29415 error ("cannot deduce template arguments for copy-initialization"
29416 " of %qT, as it has no non-explicit deduction guides or "
29417 "user-declared constructors", type);
29418 return error_mark_node;
29420 else if (!cands && fndecl == error_mark_node)
29422 error ("cannot deduce template arguments of %qT, as it has no viable "
29423 "deduction guides", type);
29424 return error_mark_node;
29427 if (fndecl == error_mark_node)
29428 fndecl = perform_dguide_overload_resolution (cands, args, tf_none);
29430 if (fndecl == error_mark_node)
29432 if (complain & tf_warning_or_error)
29434 error ("class template argument deduction failed:");
29435 perform_dguide_overload_resolution (cands, args, complain);
29436 if (elided)
29437 inform (input_location, "explicit deduction guides not considered "
29438 "for copy-initialization");
29440 return error_mark_node;
29442 /* [over.match.list]/1: In copy-list-initialization, if an explicit
29443 constructor is chosen, the initialization is ill-formed. */
29444 else if (flags & LOOKUP_ONLYCONVERTING)
29446 if (DECL_NONCONVERTING_P (fndecl))
29448 if (complain & tf_warning_or_error)
29450 // TODO: Pass down location from cp_finish_decl.
29451 error ("class template argument deduction for %qT failed: "
29452 "explicit deduction guide selected in "
29453 "copy-list-initialization", type);
29454 inform (DECL_SOURCE_LOCATION (fndecl),
29455 "explicit deduction guide declared here");
29458 return error_mark_node;
29462 /* If CTAD succeeded but the type doesn't have any explicit deduction
29463 guides, this deduction might not be what the user intended. */
29464 if (fndecl != error_mark_node && !any_dguides_p)
29466 if ((!DECL_IN_SYSTEM_HEADER (fndecl)
29467 || global_dc->dc_warn_system_headers)
29468 && warning (OPT_Wctad_maybe_unsupported,
29469 "%qT may not intend to support class template argument "
29470 "deduction", type))
29471 inform (input_location, "add a deduction guide to suppress this "
29472 "warning");
29475 return cp_build_qualified_type (TREE_TYPE (TREE_TYPE (fndecl)),
29476 cp_type_quals (ptype));
29479 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
29480 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
29481 The CONTEXT determines the context in which auto deduction is performed
29482 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
29484 OUTER_TARGS is used during template argument deduction (context == adc_unify)
29485 to properly substitute the result. It's also used in the adc_unify and
29486 adc_requirement contexts to communicate the the necessary template arguments
29487 to satisfaction. OUTER_TARGS is ignored in other contexts.
29489 For partial-concept-ids, extra args may be appended to the list of deduced
29490 template arguments prior to determining constraint satisfaction. */
29492 tree
29493 do_auto_deduction (tree type, tree init, tree auto_node,
29494 tsubst_flags_t complain, auto_deduction_context context,
29495 tree outer_targs, int flags)
29497 if (init == error_mark_node)
29498 return error_mark_node;
29500 if (init && type_dependent_expression_p (init)
29501 && context != adc_unify)
29502 /* Defining a subset of type-dependent expressions that we can deduce
29503 from ahead of time isn't worth the trouble. */
29504 return type;
29506 /* Similarly, we can't deduce from another undeduced decl. */
29507 if (init && undeduced_auto_decl (init))
29508 return type;
29510 /* We may be doing a partial substitution, but we still want to replace
29511 auto_node. */
29512 complain &= ~tf_partial;
29514 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
29515 /* C++17 class template argument deduction. */
29516 return do_class_deduction (type, tmpl, init, flags, complain);
29518 if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
29519 /* Nothing we can do with this, even in deduction context. */
29520 return type;
29522 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
29523 with either a new invented type template parameter U or, if the
29524 initializer is a braced-init-list (8.5.4), with
29525 std::initializer_list<U>. */
29526 if (BRACE_ENCLOSED_INITIALIZER_P (init))
29528 if (!DIRECT_LIST_INIT_P (init))
29529 type = listify_autos (type, auto_node);
29530 else if (CONSTRUCTOR_NELTS (init) == 1)
29531 init = CONSTRUCTOR_ELT (init, 0)->value;
29532 else
29534 if (complain & tf_warning_or_error)
29536 if (permerror (input_location, "direct-list-initialization of "
29537 "%<auto%> requires exactly one element"))
29538 inform (input_location,
29539 "for deduction to %<std::initializer_list%>, use copy-"
29540 "list-initialization (i.e. add %<=%> before the %<{%>)");
29542 type = listify_autos (type, auto_node);
29546 if (type == error_mark_node)
29547 return error_mark_node;
29549 if (BRACE_ENCLOSED_INITIALIZER_P (init))
29551 /* We don't recurse here because we can't deduce from a nested
29552 initializer_list. */
29553 if (CONSTRUCTOR_ELTS (init))
29554 for (constructor_elt &elt : CONSTRUCTOR_ELTS (init))
29555 elt.value = resolve_nondeduced_context (elt.value, complain);
29557 else
29558 init = resolve_nondeduced_context (init, complain);
29560 tree targs;
29561 if (context == adc_decomp_type
29562 && auto_node == type
29563 && init != error_mark_node
29564 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
29566 /* [dcl.struct.bind]/1 - if decomposition declaration has no ref-qualifiers
29567 and initializer has array type, deduce cv-qualified array type. */
29568 targs = make_tree_vec (1);
29569 TREE_VEC_ELT (targs, 0) = TREE_TYPE (init);
29571 else if (AUTO_IS_DECLTYPE (auto_node))
29573 tree stripped_init = tree_strip_any_location_wrapper (init);
29574 if (REFERENCE_REF_P (stripped_init))
29575 stripped_init = TREE_OPERAND (stripped_init, 0);
29576 bool id = (DECL_P (stripped_init)
29577 || ((TREE_CODE (init) == COMPONENT_REF
29578 || TREE_CODE (init) == SCOPE_REF)
29579 && !REF_PARENTHESIZED_P (init)));
29580 tree deduced = finish_decltype_type (init, id, complain);
29581 deduced = canonicalize_type_argument (deduced, complain);
29582 if (deduced == error_mark_node)
29583 return error_mark_node;
29584 targs = make_tree_vec (1);
29585 TREE_VEC_ELT (targs, 0) = deduced;
29586 /* FIXME: These errors ought to be diagnosed at parse time. */
29587 if (type != auto_node)
29589 if (complain & tf_error)
29590 error ("%qT as type rather than plain %<decltype(auto)%>", type);
29591 return error_mark_node;
29593 else if (TYPE_QUALS (type) != TYPE_UNQUALIFIED)
29595 if (complain & tf_error)
29596 error ("%<decltype(auto)%> cannot be cv-qualified");
29597 return error_mark_node;
29600 else
29602 if (error_operand_p (init))
29603 return error_mark_node;
29605 tree parms = build_tree_list (NULL_TREE, type);
29606 tree tparms;
29608 if (flag_concepts)
29609 tparms = extract_autos (type);
29610 else
29612 tparms = make_tree_vec (1);
29613 TREE_VEC_ELT (tparms, 0)
29614 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
29617 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
29618 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
29619 DEDUCE_CALL,
29620 NULL, /*explain_p=*/false);
29621 if (val > 0)
29623 if (processing_template_decl)
29624 /* Try again at instantiation time. */
29625 return type;
29626 if (type && type != error_mark_node
29627 && (complain & tf_error))
29628 /* If type is error_mark_node a diagnostic must have been
29629 emitted by now. Also, having a mention to '<type error>'
29630 in the diagnostic is not really useful to the user. */
29632 if (cfun
29633 && FNDECL_USED_AUTO (current_function_decl)
29634 && (auto_node
29635 == DECL_SAVED_AUTO_RETURN_TYPE (current_function_decl))
29636 && LAMBDA_FUNCTION_P (current_function_decl))
29637 error ("unable to deduce lambda return type from %qE", init);
29638 else
29639 error ("unable to deduce %qT from %qE", type, init);
29640 type_unification_real (tparms, targs, parms, &init, 1, 0,
29641 DEDUCE_CALL,
29642 NULL, /*explain_p=*/true);
29644 return error_mark_node;
29648 /* Check any placeholder constraints against the deduced type. */
29649 if (processing_template_decl && context == adc_unify)
29650 /* Constraints will be checked after deduction. */;
29651 else if (tree constr = NON_ERROR (PLACEHOLDER_TYPE_CONSTRAINTS (auto_node)))
29653 if (processing_template_decl)
29655 gcc_checking_assert (context == adc_variable_type
29656 || context == adc_return_type
29657 || context == adc_decomp_type);
29658 gcc_checking_assert (!type_dependent_expression_p (init));
29659 /* If the constraint is dependent, we need to wait until
29660 instantiation time to resolve the placeholder. */
29661 if (placeholder_type_constraint_dependent_p (constr))
29662 return type;
29665 if ((context == adc_return_type
29666 || context == adc_variable_type
29667 || context == adc_decomp_type)
29668 && current_function_decl
29669 && DECL_TEMPLATE_INFO (current_function_decl))
29670 outer_targs = DECL_TI_ARGS (current_function_decl);
29672 tree full_targs = add_to_template_args (outer_targs, targs);
29674 /* HACK: Compensate for callers not always communicating all levels of
29675 outer template arguments by filling in the outermost missing levels
29676 with dummy levels before checking satisfaction. We'll still crash
29677 if the constraint depends on a template argument belonging to one of
29678 these missing levels, but this hack otherwise allows us to handle a
29679 large subset of possible constraints (including all non-dependent
29680 constraints). */
29681 if (int missing_levels = (TEMPLATE_TYPE_ORIG_LEVEL (auto_node)
29682 - TMPL_ARGS_DEPTH (full_targs)))
29684 tree dummy_levels = make_tree_vec (missing_levels);
29685 for (int i = 0; i < missing_levels; ++i)
29686 TREE_VEC_ELT (dummy_levels, i) = make_tree_vec (0);
29687 full_targs = add_to_template_args (dummy_levels, full_targs);
29690 if (!constraints_satisfied_p (auto_node, full_targs))
29692 if (complain & tf_warning_or_error)
29694 auto_diagnostic_group d;
29695 switch (context)
29697 case adc_unspecified:
29698 case adc_unify:
29699 error("placeholder constraints not satisfied");
29700 break;
29701 case adc_variable_type:
29702 case adc_decomp_type:
29703 error ("deduced initializer does not satisfy "
29704 "placeholder constraints");
29705 break;
29706 case adc_return_type:
29707 error ("deduced return type does not satisfy "
29708 "placeholder constraints");
29709 break;
29710 case adc_requirement:
29711 error ("deduced expression type does not satisfy "
29712 "placeholder constraints");
29713 break;
29715 diagnose_constraints (input_location, auto_node, full_targs);
29717 return error_mark_node;
29721 if (TEMPLATE_TYPE_LEVEL (auto_node) == 1)
29722 /* The outer template arguments are already substituted into type
29723 (but we still may have used them for constraint checking above). */;
29724 else if (context == adc_unify)
29725 targs = add_to_template_args (outer_targs, targs);
29726 else if (processing_template_decl)
29727 targs = add_to_template_args (current_template_args (), targs);
29728 return tsubst (type, targs, complain, NULL_TREE);
29731 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
29732 result. */
29734 tree
29735 splice_late_return_type (tree type, tree late_return_type)
29737 if (late_return_type)
29739 gcc_assert (is_auto (type) || seen_error ());
29740 return late_return_type;
29743 if (tree auto_node = find_type_usage (type, is_auto))
29744 if (TEMPLATE_TYPE_LEVEL (auto_node) <= processing_template_decl)
29746 /* In an abbreviated function template we didn't know we were dealing
29747 with a function template when we saw the auto return type, so rebuild
29748 the return type using an auto with the correct level. */
29749 tree new_auto = make_auto_1 (TYPE_IDENTIFIER (auto_node), false);
29750 tree auto_vec = make_tree_vec (1);
29751 TREE_VEC_ELT (auto_vec, 0) = new_auto;
29752 tree targs = add_outermost_template_args (current_template_args (),
29753 auto_vec);
29754 /* Also rebuild the constraint info in terms of the new auto. */
29755 if (tree ci = PLACEHOLDER_TYPE_CONSTRAINTS_INFO (auto_node))
29756 PLACEHOLDER_TYPE_CONSTRAINTS_INFO (new_auto)
29757 = build_tree_list (current_template_parms,
29758 tsubst_constraint (TREE_VALUE (ci), targs,
29759 tf_none, NULL_TREE));
29760 TYPE_CANONICAL (new_auto) = canonical_type_parameter (new_auto);
29761 return tsubst (type, targs, tf_none, NULL_TREE);
29763 return type;
29766 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
29767 'decltype(auto)' or a deduced class template. */
29769 bool
29770 is_auto (const_tree type)
29772 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
29773 && (TYPE_IDENTIFIER (type) == auto_identifier
29774 || TYPE_IDENTIFIER (type) == decltype_auto_identifier))
29775 return true;
29776 else
29777 return false;
29780 /* for_each_template_parm callback for type_uses_auto. */
29783 is_auto_r (tree tp, void */*data*/)
29785 return is_auto (tp);
29788 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
29789 a use of `auto'. Returns NULL_TREE otherwise. */
29791 tree
29792 type_uses_auto (tree type)
29794 if (type == NULL_TREE)
29795 return NULL_TREE;
29796 else if (flag_concepts)
29798 /* The Concepts TS allows multiple autos in one type-specifier; just
29799 return the first one we find, do_auto_deduction will collect all of
29800 them. */
29801 if (uses_template_parms (type))
29802 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
29803 /*visited*/NULL, /*nondeduced*/false);
29804 else
29805 return NULL_TREE;
29807 else
29808 return find_type_usage (type, is_auto);
29811 /* Report ill-formed occurrences of auto types in ARGUMENTS. If
29812 concepts are enabled, auto is acceptable in template arguments, but
29813 only when TEMPL identifies a template class. Return TRUE if any
29814 such errors were reported. */
29816 bool
29817 check_auto_in_tmpl_args (tree tmpl, tree args)
29819 /* If there were previous errors, nevermind. */
29820 if (!args || TREE_CODE (args) != TREE_VEC)
29821 return false;
29823 /* If TMPL is an identifier, we're parsing and we can't tell yet
29824 whether TMPL is supposed to be a type, a function or a variable.
29825 We'll only be able to tell during template substitution, so we
29826 expect to be called again then. If concepts are enabled and we
29827 know we have a type, we're ok. */
29828 if (flag_concepts
29829 && (identifier_p (tmpl)
29830 || (DECL_P (tmpl)
29831 && (DECL_TYPE_TEMPLATE_P (tmpl)
29832 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))))
29833 return false;
29835 /* Quickly search for any occurrences of auto; usually there won't
29836 be any, and then we'll avoid allocating the vector. */
29837 if (!type_uses_auto (args))
29838 return false;
29840 bool errors = false;
29842 tree vec = extract_autos (args);
29843 for (int i = 0; i < TREE_VEC_LENGTH (vec); i++)
29845 tree xauto = TREE_VALUE (TREE_VEC_ELT (vec, i));
29846 error_at (DECL_SOURCE_LOCATION (xauto),
29847 "invalid use of %qT in template argument", xauto);
29848 errors = true;
29851 return errors;
29854 /* Recursively walk over && expressions searching for EXPR. Return a reference
29855 to that expression. */
29857 static tree *find_template_requirement (tree *t, tree key)
29859 if (*t == key)
29860 return t;
29861 if (TREE_CODE (*t) == TRUTH_ANDIF_EXPR)
29863 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 0), key))
29864 return p;
29865 if (tree *p = find_template_requirement (&TREE_OPERAND (*t, 1), key))
29866 return p;
29868 return 0;
29871 /* Convert the generic type parameters in PARM that match the types given in the
29872 range [START_IDX, END_IDX) from the current_template_parms into generic type
29873 packs. */
29875 tree
29876 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
29878 tree current = current_template_parms;
29879 int depth = TMPL_PARMS_DEPTH (current);
29880 current = INNERMOST_TEMPLATE_PARMS (current);
29881 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
29883 for (int i = 0; i < start_idx; ++i)
29884 TREE_VEC_ELT (replacement, i)
29885 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
29887 for (int i = start_idx; i < end_idx; ++i)
29889 /* Create a distinct parameter pack type from the current parm and add it
29890 to the replacement args to tsubst below into the generic function
29891 parameter. */
29892 tree node = TREE_VEC_ELT (current, i);
29893 tree o = TREE_TYPE (TREE_VALUE (node));
29894 tree t = copy_type (o);
29895 TEMPLATE_TYPE_PARM_INDEX (t)
29896 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
29897 t, 0, 0, tf_none);
29898 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
29899 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
29900 TYPE_MAIN_VARIANT (t) = t;
29901 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
29902 TYPE_CANONICAL (t) = canonical_type_parameter (t);
29903 TREE_VEC_ELT (replacement, i) = t;
29905 /* Replace the current template parameter with new pack. */
29906 TREE_VALUE (node) = TREE_CHAIN (t);
29908 /* Surgically adjust the associated constraint of adjusted parameter
29909 and it's corresponding contribution to the current template
29910 requirements. */
29911 if (tree constr = TEMPLATE_PARM_CONSTRAINTS (node))
29913 tree id = unpack_concept_check (constr);
29914 TREE_VEC_ELT (TREE_OPERAND (id, 1), 0) = t;
29915 tree fold = finish_left_unary_fold_expr (constr, TRUTH_ANDIF_EXPR);
29916 TEMPLATE_PARM_CONSTRAINTS (node) = fold;
29918 /* If there was a constraint, we also need to replace that in
29919 the template requirements, which we've already built. */
29920 tree *reqs = &TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
29921 reqs = find_template_requirement (reqs, constr);
29922 *reqs = fold;
29926 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
29927 TREE_VEC_ELT (replacement, i)
29928 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
29930 /* If there are more levels then build up the replacement with the outer
29931 template parms. */
29932 if (depth > 1)
29933 replacement = add_to_template_args (template_parms_to_args
29934 (TREE_CHAIN (current_template_parms)),
29935 replacement);
29937 return tsubst (parm, replacement, tf_none, NULL_TREE);
29940 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
29941 0..N-1. */
29943 void
29944 declare_integer_pack (void)
29946 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
29947 build_function_type_list (integer_type_node,
29948 integer_type_node,
29949 NULL_TREE),
29950 NULL_TREE, ECF_CONST);
29951 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
29952 set_decl_built_in_function (ipfn, BUILT_IN_FRONTEND,
29953 CP_BUILT_IN_INTEGER_PACK);
29956 /* Walk the decl or type specialization table calling FN on each
29957 entry. */
29959 void
29960 walk_specializations (bool decls_p,
29961 void (*fn) (bool decls_p, spec_entry *entry, void *data),
29962 void *data)
29964 spec_hash_table *table = decls_p ? decl_specializations
29965 : type_specializations;
29966 spec_hash_table::iterator end (table->end ());
29967 for (spec_hash_table::iterator iter (table->begin ()); iter != end; ++iter)
29968 fn (decls_p, *iter, data);
29971 /* Lookup the specialization of *ELT, in the decl or type
29972 specialization table. Return the SPEC that's already there, or
29973 NULL if nothing. */
29975 tree
29976 match_mergeable_specialization (bool decl_p, spec_entry *elt)
29978 hash_table<spec_hasher> *specializations
29979 = decl_p ? decl_specializations : type_specializations;
29980 hashval_t hash = spec_hasher::hash (elt);
29981 auto *slot = specializations->find_slot_with_hash (elt, hash, NO_INSERT);
29983 if (slot)
29984 return (*slot)->spec;
29986 return NULL_TREE;
29989 /* Return flags encoding whether SPEC is on the instantiation and/or
29990 specialization lists of TMPL. */
29992 unsigned
29993 get_mergeable_specialization_flags (tree tmpl, tree decl)
29995 unsigned flags = 0;
29997 for (tree inst = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
29998 inst; inst = TREE_CHAIN (inst))
29999 if (TREE_VALUE (inst) == decl)
30001 flags |= 1;
30002 break;
30005 if (CLASS_TYPE_P (TREE_TYPE (decl))
30006 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl))
30007 && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)) == 2)
30008 /* Only need to search if DECL is a partial specialization. */
30009 for (tree part = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
30010 part; part = TREE_CHAIN (part))
30011 if (TREE_VALUE (part) == decl)
30013 flags |= 2;
30014 break;
30017 return flags;
30020 /* Add a new specialization described by SPEC. DECL is the
30021 maybe-template decl and FLAGS is as returned from
30022 get_mergeable_specialization_flags. */
30024 void
30025 add_mergeable_specialization (bool decl_p, bool alias_p, spec_entry *elt,
30026 tree decl, unsigned flags)
30028 hashval_t hash = spec_hasher::hash (elt);
30029 if (decl_p)
30031 auto *slot = decl_specializations->find_slot_with_hash (elt, hash, INSERT);
30033 gcc_checking_assert (!*slot);
30034 auto entry = ggc_alloc<spec_entry> ();
30035 *entry = *elt;
30036 *slot = entry;
30038 if (alias_p)
30040 elt->spec = TREE_TYPE (elt->spec);
30041 gcc_checking_assert (elt->spec);
30045 if (!decl_p || alias_p)
30047 auto *slot = type_specializations->find_slot_with_hash (elt, hash, INSERT);
30049 /* We don't distinguish different constrained partial type
30050 specializations, so there could be duplicates. Everything else
30051 must be new. */
30052 if (!(flags & 2 && *slot))
30054 gcc_checking_assert (!*slot);
30056 auto entry = ggc_alloc<spec_entry> ();
30057 *entry = *elt;
30058 *slot = entry;
30062 if (flags & 1)
30063 DECL_TEMPLATE_INSTANTIATIONS (elt->tmpl)
30064 = tree_cons (elt->args, decl, DECL_TEMPLATE_INSTANTIATIONS (elt->tmpl));
30066 if (flags & 2)
30068 /* A partial specialization. */
30069 tree cons = tree_cons (elt->args, decl,
30070 DECL_TEMPLATE_SPECIALIZATIONS (elt->tmpl));
30071 TREE_TYPE (cons) = elt->spec;
30072 DECL_TEMPLATE_SPECIALIZATIONS (elt->tmpl) = cons;
30076 /* Set up the hash tables for template instantiations. */
30078 void
30079 init_template_processing (void)
30081 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
30082 type_specializations = hash_table<spec_hasher>::create_ggc (37);
30084 if (cxx_dialect >= cxx11)
30085 declare_integer_pack ();
30088 /* Print stats about the template hash tables for -fstats. */
30090 void
30091 print_template_statistics (void)
30093 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
30094 "%f collisions\n", (long) decl_specializations->size (),
30095 (long) decl_specializations->elements (),
30096 decl_specializations->collisions ());
30097 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
30098 "%f collisions\n", (long) type_specializations->size (),
30099 (long) type_specializations->elements (),
30100 type_specializations->collisions ());
30103 #if CHECKING_P
30105 namespace selftest {
30107 /* Verify that build_non_dependent_expr () works, for various expressions,
30108 and that location wrappers don't affect the results. */
30110 static void
30111 test_build_non_dependent_expr ()
30113 location_t loc = BUILTINS_LOCATION;
30115 /* Verify constants, without and with location wrappers. */
30116 tree int_cst = build_int_cst (integer_type_node, 42);
30117 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
30119 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
30120 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
30121 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
30123 tree string_lit = build_string (4, "foo");
30124 TREE_TYPE (string_lit) = char_array_type_node;
30125 string_lit = fix_string_type (string_lit);
30126 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
30128 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
30129 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
30130 ASSERT_EQ (wrapped_string_lit,
30131 build_non_dependent_expr (wrapped_string_lit));
30134 /* Verify that type_dependent_expression_p () works correctly, even
30135 in the presence of location wrapper nodes. */
30137 static void
30138 test_type_dependent_expression_p ()
30140 location_t loc = BUILTINS_LOCATION;
30142 tree name = get_identifier ("foo");
30144 /* If no templates are involved, nothing is type-dependent. */
30145 gcc_assert (!processing_template_decl);
30146 ASSERT_FALSE (type_dependent_expression_p (name));
30148 ++processing_template_decl;
30150 /* Within a template, an unresolved name is always type-dependent. */
30151 ASSERT_TRUE (type_dependent_expression_p (name));
30153 /* Ensure it copes with NULL_TREE and errors. */
30154 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
30155 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
30157 /* A USING_DECL in a template should be type-dependent, even if wrapped
30158 with a location wrapper (PR c++/83799). */
30159 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
30160 TREE_TYPE (using_decl) = integer_type_node;
30161 ASSERT_TRUE (type_dependent_expression_p (using_decl));
30162 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
30163 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
30164 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
30166 --processing_template_decl;
30169 /* Run all of the selftests within this file. */
30171 void
30172 cp_pt_c_tests ()
30174 test_build_non_dependent_expr ();
30175 test_type_dependent_expression_p ();
30178 } // namespace selftest
30180 #endif /* #if CHECKING_P */
30182 #include "gt-cp-pt.h"