PR c++/84662
[official-gcc.git] / gcc / cp / pt.c
blobc81bd5094d95dd693a74bb8ddacb8bec8b2df6f6
1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2018 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
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 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "cp-tree.h"
31 #include "timevar.h"
32 #include "stringpool.h"
33 #include "varasm.h"
34 #include "attribs.h"
35 #include "stor-layout.h"
36 #include "intl.h"
37 #include "c-family/c-objc.h"
38 #include "cp-objcp-common.h"
39 #include "toplev.h"
40 #include "tree-iterator.h"
41 #include "type-utils.h"
42 #include "gimplify.h"
43 #include "gcc-rich-location.h"
44 #include "selftest.h"
46 /* The type of functions taking a tree, and some additional data, and
47 returning an int. */
48 typedef int (*tree_fn_t) (tree, void*);
50 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
51 instantiations have been deferred, either because their definitions
52 were not yet available, or because we were putting off doing the work. */
53 struct GTY ((chain_next ("%h.next"))) pending_template {
54 struct pending_template *next;
55 struct tinst_level *tinst;
58 static GTY(()) struct pending_template *pending_templates;
59 static GTY(()) struct pending_template *last_pending_template;
61 int processing_template_parmlist;
62 static int template_header_count;
64 static GTY(()) tree saved_trees;
65 static vec<int> inline_parm_levels;
67 static GTY(()) struct tinst_level *current_tinst_level;
69 static GTY(()) tree saved_access_scope;
71 /* Live only within one (recursive) call to tsubst_expr. We use
72 this to pass the statement expression node from the STMT_EXPR
73 to the EXPR_STMT that is its result. */
74 static tree cur_stmt_expr;
76 // -------------------------------------------------------------------------- //
77 // Local Specialization Stack
79 // Implementation of the RAII helper for creating new local
80 // specializations.
81 local_specialization_stack::local_specialization_stack (lss_policy policy)
82 : saved (local_specializations)
84 if (policy == lss_blank || !saved)
85 local_specializations = new hash_map<tree, tree>;
86 else
87 local_specializations = new hash_map<tree, tree>(*saved);
90 local_specialization_stack::~local_specialization_stack ()
92 delete local_specializations;
93 local_specializations = saved;
96 /* True if we've recursed into fn_type_unification too many times. */
97 static bool excessive_deduction_depth;
99 struct GTY((for_user)) spec_entry
101 tree tmpl;
102 tree args;
103 tree spec;
106 struct spec_hasher : ggc_ptr_hash<spec_entry>
108 static hashval_t hash (spec_entry *);
109 static bool equal (spec_entry *, spec_entry *);
112 static GTY (()) hash_table<spec_hasher> *decl_specializations;
114 static GTY (()) hash_table<spec_hasher> *type_specializations;
116 /* Contains canonical template parameter types. The vector is indexed by
117 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
118 TREE_LIST, whose TREE_VALUEs contain the canonical template
119 parameters of various types and levels. */
120 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
122 #define UNIFY_ALLOW_NONE 0
123 #define UNIFY_ALLOW_MORE_CV_QUAL 1
124 #define UNIFY_ALLOW_LESS_CV_QUAL 2
125 #define UNIFY_ALLOW_DERIVED 4
126 #define UNIFY_ALLOW_INTEGER 8
127 #define UNIFY_ALLOW_OUTER_LEVEL 16
128 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
129 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
131 enum template_base_result {
132 tbr_incomplete_type,
133 tbr_ambiguous_baseclass,
134 tbr_success
137 static void push_access_scope (tree);
138 static void pop_access_scope (tree);
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 tree add_to_template_args (tree, tree);
155 static tree add_outermost_template_args (tree, tree);
156 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
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, int,
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 void template_parm_level_and_index (tree, int*, int*);
184 static int unify_pack_expansion (tree, tree, tree,
185 tree, unification_kind_t, bool, bool);
186 static tree copy_template_args (tree);
187 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
188 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
189 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
190 static tree most_specialized_partial_spec (tree, tsubst_flags_t);
191 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
192 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
193 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
194 static bool check_specialization_scope (void);
195 static tree process_partial_specialization (tree);
196 static void set_current_access_from_decl (tree);
197 static enum template_base_result get_template_base (tree, tree, tree, tree,
198 bool , tree *);
199 static tree try_class_unification (tree, tree, tree, tree, bool);
200 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
201 tree, tree);
202 static bool template_template_parm_bindings_ok_p (tree, tree);
203 static void tsubst_default_arguments (tree, tsubst_flags_t);
204 static tree for_each_template_parm_r (tree *, int *, void *);
205 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
206 static void copy_default_args_to_explicit_spec (tree);
207 static bool invalid_nontype_parm_type_p (tree, tsubst_flags_t);
208 static bool dependent_template_arg_p (tree);
209 static bool any_template_arguments_need_structural_equality_p (tree);
210 static bool dependent_type_p_r (tree);
211 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
212 static tree tsubst_decl (tree, tree, tsubst_flags_t);
213 static void perform_typedefs_access_check (tree tmpl, tree targs);
214 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
215 location_t);
216 static tree listify (tree);
217 static tree listify_autos (tree, tree);
218 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
219 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
220 static bool complex_alias_template_p (const_tree tmpl);
221 static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
222 static tree canonicalize_expr_argument (tree, tsubst_flags_t);
223 static tree make_argument_pack (tree);
224 static void register_parameter_specializations (tree, tree);
225 static tree enclosing_instantiation_of (tree tctx);
227 /* Make the current scope suitable for access checking when we are
228 processing T. T can be FUNCTION_DECL for instantiated function
229 template, VAR_DECL for static member variable, or TYPE_DECL for
230 alias template (needed by instantiate_decl). */
232 static void
233 push_access_scope (tree t)
235 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
236 || TREE_CODE (t) == TYPE_DECL);
238 if (DECL_FRIEND_CONTEXT (t))
239 push_nested_class (DECL_FRIEND_CONTEXT (t));
240 else if (DECL_CLASS_SCOPE_P (t))
241 push_nested_class (DECL_CONTEXT (t));
242 else
243 push_to_top_level ();
245 if (TREE_CODE (t) == FUNCTION_DECL)
247 saved_access_scope = tree_cons
248 (NULL_TREE, current_function_decl, saved_access_scope);
249 current_function_decl = t;
253 /* Restore the scope set up by push_access_scope. T is the node we
254 are processing. */
256 static void
257 pop_access_scope (tree t)
259 if (TREE_CODE (t) == FUNCTION_DECL)
261 current_function_decl = TREE_VALUE (saved_access_scope);
262 saved_access_scope = TREE_CHAIN (saved_access_scope);
265 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
266 pop_nested_class ();
267 else
268 pop_from_top_level ();
271 /* Do any processing required when DECL (a member template
272 declaration) is finished. Returns the TEMPLATE_DECL corresponding
273 to DECL, unless it is a specialization, in which case the DECL
274 itself is returned. */
276 tree
277 finish_member_template_decl (tree decl)
279 if (decl == error_mark_node)
280 return error_mark_node;
282 gcc_assert (DECL_P (decl));
284 if (TREE_CODE (decl) == TYPE_DECL)
286 tree type;
288 type = TREE_TYPE (decl);
289 if (type == error_mark_node)
290 return error_mark_node;
291 if (MAYBE_CLASS_TYPE_P (type)
292 && CLASSTYPE_TEMPLATE_INFO (type)
293 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
295 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
296 check_member_template (tmpl);
297 return tmpl;
299 return NULL_TREE;
301 else if (TREE_CODE (decl) == FIELD_DECL)
302 error ("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 ("invalid member template declaration %qD", decl);
316 return error_mark_node;
319 /* Create a template info node. */
321 tree
322 build_template_info (tree template_decl, tree template_args)
324 tree result = make_node (TEMPLATE_INFO);
325 TI_TEMPLATE (result) = template_decl;
326 TI_ARGS (result) = template_args;
327 return result;
330 /* Return the template info node corresponding to T, whatever T is. */
332 tree
333 get_template_info (const_tree t)
335 tree tinfo = NULL_TREE;
337 if (!t || t == error_mark_node)
338 return NULL;
340 if (TREE_CODE (t) == NAMESPACE_DECL
341 || TREE_CODE (t) == PARM_DECL)
342 return NULL;
344 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
345 tinfo = DECL_TEMPLATE_INFO (t);
347 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
348 t = TREE_TYPE (t);
350 if (OVERLOAD_TYPE_P (t))
351 tinfo = TYPE_TEMPLATE_INFO (t);
352 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
353 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
355 return tinfo;
358 /* Returns the template nesting level of the indicated class TYPE.
360 For example, in:
361 template <class T>
362 struct A
364 template <class U>
365 struct B {};
368 A<T>::B<U> has depth two, while A<T> has depth one.
369 Both A<T>::B<int> and A<int>::B<U> have depth one, if
370 they are instantiations, not specializations.
372 This function is guaranteed to return 0 if passed NULL_TREE so
373 that, for example, `template_class_depth (current_class_type)' is
374 always safe. */
377 template_class_depth (tree type)
379 int depth;
381 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
383 tree tinfo = get_template_info (type);
385 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
386 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
387 ++depth;
389 if (DECL_P (type))
390 type = CP_DECL_CONTEXT (type);
391 else if (LAMBDA_TYPE_P (type))
392 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
393 else
394 type = CP_TYPE_CONTEXT (type);
397 return depth;
400 /* Subroutine of maybe_begin_member_template_processing.
401 Returns true if processing DECL needs us to push template parms. */
403 static bool
404 inline_needs_template_parms (tree decl, bool nsdmi)
406 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
407 return false;
409 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
410 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
413 /* Subroutine of maybe_begin_member_template_processing.
414 Push the template parms in PARMS, starting from LEVELS steps into the
415 chain, and ending at the beginning, since template parms are listed
416 innermost first. */
418 static void
419 push_inline_template_parms_recursive (tree parmlist, int levels)
421 tree parms = TREE_VALUE (parmlist);
422 int i;
424 if (levels > 1)
425 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
427 ++processing_template_decl;
428 current_template_parms
429 = tree_cons (size_int (processing_template_decl),
430 parms, current_template_parms);
431 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
433 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
434 NULL);
435 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
437 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
439 if (error_operand_p (parm))
440 continue;
442 gcc_assert (DECL_P (parm));
444 switch (TREE_CODE (parm))
446 case TYPE_DECL:
447 case TEMPLATE_DECL:
448 pushdecl (parm);
449 break;
451 case PARM_DECL:
452 /* Push the CONST_DECL. */
453 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
454 break;
456 default:
457 gcc_unreachable ();
462 /* Restore the template parameter context for a member template, a
463 friend template defined in a class definition, or a non-template
464 member of template class. */
466 void
467 maybe_begin_member_template_processing (tree decl)
469 tree parms;
470 int levels = 0;
471 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
473 if (nsdmi)
475 tree ctx = DECL_CONTEXT (decl);
476 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
477 /* Disregard full specializations (c++/60999). */
478 && uses_template_parms (ctx)
479 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
482 if (inline_needs_template_parms (decl, nsdmi))
484 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
485 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
487 if (DECL_TEMPLATE_SPECIALIZATION (decl))
489 --levels;
490 parms = TREE_CHAIN (parms);
493 push_inline_template_parms_recursive (parms, levels);
496 /* Remember how many levels of template parameters we pushed so that
497 we can pop them later. */
498 inline_parm_levels.safe_push (levels);
501 /* Undo the effects of maybe_begin_member_template_processing. */
503 void
504 maybe_end_member_template_processing (void)
506 int i;
507 int last;
509 if (inline_parm_levels.length () == 0)
510 return;
512 last = inline_parm_levels.pop ();
513 for (i = 0; i < last; ++i)
515 --processing_template_decl;
516 current_template_parms = TREE_CHAIN (current_template_parms);
517 poplevel (0, 0, 0);
521 /* Return a new template argument vector which contains all of ARGS,
522 but has as its innermost set of arguments the EXTRA_ARGS. */
524 static tree
525 add_to_template_args (tree args, tree extra_args)
527 tree new_args;
528 int extra_depth;
529 int i;
530 int j;
532 if (args == NULL_TREE || extra_args == error_mark_node)
533 return extra_args;
535 extra_depth = TMPL_ARGS_DEPTH (extra_args);
536 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
538 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
539 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
541 for (j = 1; j <= extra_depth; ++j, ++i)
542 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
544 return new_args;
547 /* Like add_to_template_args, but only the outermost ARGS are added to
548 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
549 (EXTRA_ARGS) levels are added. This function is used to combine
550 the template arguments from a partial instantiation with the
551 template arguments used to attain the full instantiation from the
552 partial instantiation. */
554 static tree
555 add_outermost_template_args (tree args, tree extra_args)
557 tree new_args;
559 /* If there are more levels of EXTRA_ARGS than there are ARGS,
560 something very fishy is going on. */
561 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
563 /* If *all* the new arguments will be the EXTRA_ARGS, just return
564 them. */
565 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
566 return extra_args;
568 /* For the moment, we make ARGS look like it contains fewer levels. */
569 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
571 new_args = add_to_template_args (args, extra_args);
573 /* Now, we restore ARGS to its full dimensions. */
574 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
576 return new_args;
579 /* Return the N levels of innermost template arguments from the ARGS. */
581 tree
582 get_innermost_template_args (tree args, int n)
584 tree new_args;
585 int extra_levels;
586 int i;
588 gcc_assert (n >= 0);
590 /* If N is 1, just return the innermost set of template arguments. */
591 if (n == 1)
592 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
594 /* If we're not removing anything, just return the arguments we were
595 given. */
596 extra_levels = TMPL_ARGS_DEPTH (args) - n;
597 gcc_assert (extra_levels >= 0);
598 if (extra_levels == 0)
599 return args;
601 /* Make a new set of arguments, not containing the outer arguments. */
602 new_args = make_tree_vec (n);
603 for (i = 1; i <= n; ++i)
604 SET_TMPL_ARGS_LEVEL (new_args, i,
605 TMPL_ARGS_LEVEL (args, i + extra_levels));
607 return new_args;
610 /* The inverse of get_innermost_template_args: Return all but the innermost
611 EXTRA_LEVELS levels of template arguments from the ARGS. */
613 static tree
614 strip_innermost_template_args (tree args, int extra_levels)
616 tree new_args;
617 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
618 int i;
620 gcc_assert (n >= 0);
622 /* If N is 1, just return the outermost set of template arguments. */
623 if (n == 1)
624 return TMPL_ARGS_LEVEL (args, 1);
626 /* If we're not removing anything, just return the arguments we were
627 given. */
628 gcc_assert (extra_levels >= 0);
629 if (extra_levels == 0)
630 return args;
632 /* Make a new set of arguments, not containing the inner arguments. */
633 new_args = make_tree_vec (n);
634 for (i = 1; i <= n; ++i)
635 SET_TMPL_ARGS_LEVEL (new_args, i,
636 TMPL_ARGS_LEVEL (args, i));
638 return new_args;
641 /* We've got a template header coming up; push to a new level for storing
642 the parms. */
644 void
645 begin_template_parm_list (void)
647 /* We use a non-tag-transparent scope here, which causes pushtag to
648 put tags in this scope, rather than in the enclosing class or
649 namespace scope. This is the right thing, since we want
650 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
651 global template class, push_template_decl handles putting the
652 TEMPLATE_DECL into top-level scope. For a nested template class,
653 e.g.:
655 template <class T> struct S1 {
656 template <class T> struct S2 {};
659 pushtag contains special code to insert the TEMPLATE_DECL for S2
660 at the right scope. */
661 begin_scope (sk_template_parms, NULL);
662 ++processing_template_decl;
663 ++processing_template_parmlist;
664 note_template_header (0);
666 /* Add a dummy parameter level while we process the parameter list. */
667 current_template_parms
668 = tree_cons (size_int (processing_template_decl),
669 make_tree_vec (0),
670 current_template_parms);
673 /* This routine is called when a specialization is declared. If it is
674 invalid to declare a specialization here, an error is reported and
675 false is returned, otherwise this routine will return true. */
677 static bool
678 check_specialization_scope (void)
680 tree scope = current_scope ();
682 /* [temp.expl.spec]
684 An explicit specialization shall be declared in the namespace of
685 which the template is a member, or, for member templates, in the
686 namespace of which the enclosing class or enclosing class
687 template is a member. An explicit specialization of a member
688 function, member class or static data member of a class template
689 shall be declared in the namespace of which the class template
690 is a member. */
691 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
693 error ("explicit specialization in non-namespace scope %qD", scope);
694 return false;
697 /* [temp.expl.spec]
699 In an explicit specialization declaration for a member of a class
700 template or a member template that appears in namespace scope,
701 the member template and some of its enclosing class templates may
702 remain unspecialized, except that the declaration shall not
703 explicitly specialize a class member template if its enclosing
704 class templates are not explicitly specialized as well. */
705 if (current_template_parms)
707 error ("enclosing class templates are not explicitly specialized");
708 return false;
711 return true;
714 /* We've just seen template <>. */
716 bool
717 begin_specialization (void)
719 begin_scope (sk_template_spec, NULL);
720 note_template_header (1);
721 return check_specialization_scope ();
724 /* Called at then end of processing a declaration preceded by
725 template<>. */
727 void
728 end_specialization (void)
730 finish_scope ();
731 reset_specialization ();
734 /* Any template <>'s that we have seen thus far are not referring to a
735 function specialization. */
737 void
738 reset_specialization (void)
740 processing_specialization = 0;
741 template_header_count = 0;
744 /* We've just seen a template header. If SPECIALIZATION is nonzero,
745 it was of the form template <>. */
747 static void
748 note_template_header (int specialization)
750 processing_specialization = specialization;
751 template_header_count++;
754 /* We're beginning an explicit instantiation. */
756 void
757 begin_explicit_instantiation (void)
759 gcc_assert (!processing_explicit_instantiation);
760 processing_explicit_instantiation = true;
764 void
765 end_explicit_instantiation (void)
767 gcc_assert (processing_explicit_instantiation);
768 processing_explicit_instantiation = false;
771 /* An explicit specialization or partial specialization of TMPL is being
772 declared. Check that the namespace in which the specialization is
773 occurring is permissible. Returns false iff it is invalid to
774 specialize TMPL in the current namespace. */
776 static bool
777 check_specialization_namespace (tree tmpl)
779 tree tpl_ns = decl_namespace_context (tmpl);
781 /* [tmpl.expl.spec]
783 An explicit specialization shall be declared in a namespace enclosing the
784 specialized template. An explicit specialization whose declarator-id is
785 not qualified shall be declared in the nearest enclosing namespace of the
786 template, or, if the namespace is inline (7.3.1), any namespace from its
787 enclosing namespace set. */
788 if (current_scope() != DECL_CONTEXT (tmpl)
789 && !at_namespace_scope_p ())
791 error ("specialization of %qD must appear at namespace scope", tmpl);
792 return false;
795 if (is_nested_namespace (current_namespace, tpl_ns, cxx_dialect < cxx11))
796 /* Same or enclosing namespace. */
797 return true;
798 else
800 permerror (input_location,
801 "specialization of %qD in different namespace", tmpl);
802 inform (DECL_SOURCE_LOCATION (tmpl),
803 " from definition of %q#D", tmpl);
804 return false;
808 /* SPEC is an explicit instantiation. Check that it is valid to
809 perform this explicit instantiation in the current namespace. */
811 static void
812 check_explicit_instantiation_namespace (tree spec)
814 tree ns;
816 /* DR 275: An explicit instantiation shall appear in an enclosing
817 namespace of its template. */
818 ns = decl_namespace_context (spec);
819 if (!is_nested_namespace (current_namespace, ns))
820 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
821 "(which does not enclose namespace %qD)",
822 spec, current_namespace, ns);
825 // Returns the type of a template specialization only if that
826 // specialization needs to be defined. Otherwise (e.g., if the type has
827 // already been defined), the function returns NULL_TREE.
828 static tree
829 maybe_new_partial_specialization (tree type)
831 // An implicit instantiation of an incomplete type implies
832 // the definition of a new class template.
834 // template<typename T>
835 // struct S;
837 // template<typename T>
838 // struct S<T*>;
840 // Here, S<T*> is an implicit instantiation of S whose type
841 // is incomplete.
842 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
843 return type;
845 // It can also be the case that TYPE is a completed specialization.
846 // Continuing the previous example, suppose we also declare:
848 // template<typename T>
849 // requires Integral<T>
850 // struct S<T*>;
852 // Here, S<T*> refers to the specialization S<T*> defined
853 // above. However, we need to differentiate definitions because
854 // we intend to define a new partial specialization. In this case,
855 // we rely on the fact that the constraints are different for
856 // this declaration than that above.
858 // Note that we also get here for injected class names and
859 // late-parsed template definitions. We must ensure that we
860 // do not create new type declarations for those cases.
861 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
863 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
864 tree args = CLASSTYPE_TI_ARGS (type);
866 // If there are no template parameters, this cannot be a new
867 // partial template specializtion?
868 if (!current_template_parms)
869 return NULL_TREE;
871 // The injected-class-name is not a new partial specialization.
872 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
873 return NULL_TREE;
875 // If the constraints are not the same as those of the primary
876 // then, we can probably create a new specialization.
877 tree type_constr = current_template_constraints ();
879 if (type == TREE_TYPE (tmpl))
881 tree main_constr = get_constraints (tmpl);
882 if (equivalent_constraints (type_constr, main_constr))
883 return NULL_TREE;
886 // Also, if there's a pre-existing specialization with matching
887 // constraints, then this also isn't new.
888 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
889 while (specs)
891 tree spec_tmpl = TREE_VALUE (specs);
892 tree spec_args = TREE_PURPOSE (specs);
893 tree spec_constr = get_constraints (spec_tmpl);
894 if (comp_template_args (args, spec_args)
895 && equivalent_constraints (type_constr, spec_constr))
896 return NULL_TREE;
897 specs = TREE_CHAIN (specs);
900 // Create a new type node (and corresponding type decl)
901 // for the newly declared specialization.
902 tree t = make_class_type (TREE_CODE (type));
903 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
904 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
906 /* We only need a separate type node for storing the definition of this
907 partial specialization; uses of S<T*> are unconstrained, so all are
908 equivalent. So keep TYPE_CANONICAL the same. */
909 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
911 // Build the corresponding type decl.
912 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
913 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
914 DECL_SOURCE_LOCATION (d) = input_location;
916 return t;
919 return NULL_TREE;
922 /* The TYPE is being declared. If it is a template type, that means it
923 is a partial specialization. Do appropriate error-checking. */
925 tree
926 maybe_process_partial_specialization (tree type)
928 tree context;
930 if (type == error_mark_node)
931 return error_mark_node;
933 /* A lambda that appears in specialization context is not itself a
934 specialization. */
935 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
936 return type;
938 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
940 error ("name of class shadows template template parameter %qD",
941 TYPE_NAME (type));
942 return error_mark_node;
945 context = TYPE_CONTEXT (type);
947 if (TYPE_ALIAS_P (type))
949 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
951 if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
952 error ("specialization of alias template %qD",
953 TI_TEMPLATE (tinfo));
954 else
955 error ("explicit specialization of non-template %qT", type);
956 return error_mark_node;
958 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
960 /* This is for ordinary explicit specialization and partial
961 specialization of a template class such as:
963 template <> class C<int>;
967 template <class T> class C<T*>;
969 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
971 if (tree t = maybe_new_partial_specialization (type))
973 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
974 && !at_namespace_scope_p ())
975 return error_mark_node;
976 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
977 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
978 if (processing_template_decl)
980 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
981 if (decl == error_mark_node)
982 return error_mark_node;
983 return TREE_TYPE (decl);
986 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
987 error ("specialization of %qT after instantiation", type);
988 else if (errorcount && !processing_specialization
989 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
990 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
991 /* Trying to define a specialization either without a template<> header
992 or in an inappropriate place. We've already given an error, so just
993 bail now so we don't actually define the specialization. */
994 return error_mark_node;
996 else if (CLASS_TYPE_P (type)
997 && !CLASSTYPE_USE_TEMPLATE (type)
998 && CLASSTYPE_TEMPLATE_INFO (type)
999 && context && CLASS_TYPE_P (context)
1000 && CLASSTYPE_TEMPLATE_INFO (context))
1002 /* This is for an explicit specialization of member class
1003 template according to [temp.expl.spec/18]:
1005 template <> template <class U> class C<int>::D;
1007 The context `C<int>' must be an implicit instantiation.
1008 Otherwise this is just a member class template declared
1009 earlier like:
1011 template <> class C<int> { template <class U> class D; };
1012 template <> template <class U> class C<int>::D;
1014 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1015 while in the second case, `C<int>::D' is a primary template
1016 and `C<T>::D' may not exist. */
1018 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1019 && !COMPLETE_TYPE_P (type))
1021 tree t;
1022 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1024 if (current_namespace
1025 != decl_namespace_context (tmpl))
1027 permerror (input_location,
1028 "specializing %q#T in different namespace", type);
1029 permerror (DECL_SOURCE_LOCATION (tmpl),
1030 " from definition of %q#D", tmpl);
1033 /* Check for invalid specialization after instantiation:
1035 template <> template <> class C<int>::D<int>;
1036 template <> template <class U> class C<int>::D; */
1038 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1039 t; t = TREE_CHAIN (t))
1041 tree inst = TREE_VALUE (t);
1042 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1043 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1045 /* We already have a full specialization of this partial
1046 instantiation, or a full specialization has been
1047 looked up but not instantiated. Reassign it to the
1048 new member specialization template. */
1049 spec_entry elt;
1050 spec_entry *entry;
1052 elt.tmpl = most_general_template (tmpl);
1053 elt.args = CLASSTYPE_TI_ARGS (inst);
1054 elt.spec = inst;
1056 type_specializations->remove_elt (&elt);
1058 elt.tmpl = tmpl;
1059 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1061 spec_entry **slot
1062 = type_specializations->find_slot (&elt, INSERT);
1063 entry = ggc_alloc<spec_entry> ();
1064 *entry = elt;
1065 *slot = entry;
1067 else
1068 /* But if we've had an implicit instantiation, that's a
1069 problem ([temp.expl.spec]/6). */
1070 error ("specialization %qT after instantiation %qT",
1071 type, inst);
1074 /* Mark TYPE as a specialization. And as a result, we only
1075 have one level of template argument for the innermost
1076 class template. */
1077 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1078 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1079 CLASSTYPE_TI_ARGS (type)
1080 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1083 else if (processing_specialization)
1085 /* Someday C++0x may allow for enum template specialization. */
1086 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1087 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1088 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1089 "of %qD not allowed by ISO C++", type);
1090 else
1092 error ("explicit specialization of non-template %qT", type);
1093 return error_mark_node;
1097 return type;
1100 /* Returns nonzero if we can optimize the retrieval of specializations
1101 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1102 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1104 static inline bool
1105 optimize_specialization_lookup_p (tree tmpl)
1107 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1108 && DECL_CLASS_SCOPE_P (tmpl)
1109 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1110 parameter. */
1111 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1112 /* The optimized lookup depends on the fact that the
1113 template arguments for the member function template apply
1114 purely to the containing class, which is not true if the
1115 containing class is an explicit or partial
1116 specialization. */
1117 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1118 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1119 && !DECL_CONV_FN_P (tmpl)
1120 /* It is possible to have a template that is not a member
1121 template and is not a member of a template class:
1123 template <typename T>
1124 struct S { friend A::f(); };
1126 Here, the friend function is a template, but the context does
1127 not have template information. The optimized lookup relies
1128 on having ARGS be the template arguments for both the class
1129 and the function template. */
1130 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1133 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1134 gone through coerce_template_parms by now. */
1136 static void
1137 verify_unstripped_args (tree args)
1139 ++processing_template_decl;
1140 if (!any_dependent_template_arguments_p (args))
1142 tree inner = INNERMOST_TEMPLATE_ARGS (args);
1143 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1145 tree arg = TREE_VEC_ELT (inner, i);
1146 if (TREE_CODE (arg) == TEMPLATE_DECL)
1147 /* OK */;
1148 else if (TYPE_P (arg))
1149 gcc_assert (strip_typedefs (arg, NULL) == arg);
1150 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1151 /* Allow typedefs on the type of a non-type argument, since a
1152 parameter can have them. */;
1153 else
1154 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1157 --processing_template_decl;
1160 /* Retrieve the specialization (in the sense of [temp.spec] - a
1161 specialization is either an instantiation or an explicit
1162 specialization) of TMPL for the given template ARGS. If there is
1163 no such specialization, return NULL_TREE. The ARGS are a vector of
1164 arguments, or a vector of vectors of arguments, in the case of
1165 templates with more than one level of parameters.
1167 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1168 then we search for a partial specialization matching ARGS. This
1169 parameter is ignored if TMPL is not a class template.
1171 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1172 result is a NONTYPE_ARGUMENT_PACK. */
1174 static tree
1175 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1177 if (tmpl == NULL_TREE)
1178 return NULL_TREE;
1180 if (args == error_mark_node)
1181 return NULL_TREE;
1183 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1184 || TREE_CODE (tmpl) == FIELD_DECL);
1186 /* There should be as many levels of arguments as there are
1187 levels of parameters. */
1188 gcc_assert (TMPL_ARGS_DEPTH (args)
1189 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1190 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1191 : template_class_depth (DECL_CONTEXT (tmpl))));
1193 if (flag_checking)
1194 verify_unstripped_args (args);
1196 /* Lambda functions in templates aren't instantiated normally, but through
1197 tsubst_lambda_expr. */
1198 if (lambda_fn_in_template_p (tmpl))
1199 return NULL_TREE;
1201 if (optimize_specialization_lookup_p (tmpl))
1203 /* The template arguments actually apply to the containing
1204 class. Find the class specialization with those
1205 arguments. */
1206 tree class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1207 tree class_specialization
1208 = retrieve_specialization (class_template, args, 0);
1209 if (!class_specialization)
1210 return NULL_TREE;
1212 /* Find the instance of TMPL. */
1213 tree fns = get_class_binding (class_specialization, DECL_NAME (tmpl));
1214 for (ovl_iterator iter (fns); iter; ++iter)
1216 tree fn = *iter;
1217 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1218 /* using-declarations can add base methods to the method vec,
1219 and we don't want those here. */
1220 && DECL_CONTEXT (fn) == class_specialization)
1221 return fn;
1223 return NULL_TREE;
1225 else
1227 spec_entry *found;
1228 spec_entry elt;
1229 hash_table<spec_hasher> *specializations;
1231 elt.tmpl = tmpl;
1232 elt.args = args;
1233 elt.spec = NULL_TREE;
1235 if (DECL_CLASS_TEMPLATE_P (tmpl))
1236 specializations = type_specializations;
1237 else
1238 specializations = decl_specializations;
1240 if (hash == 0)
1241 hash = spec_hasher::hash (&elt);
1242 found = specializations->find_with_hash (&elt, hash);
1243 if (found)
1244 return found->spec;
1247 return NULL_TREE;
1250 /* Like retrieve_specialization, but for local declarations. */
1252 tree
1253 retrieve_local_specialization (tree tmpl)
1255 if (local_specializations == NULL)
1256 return NULL_TREE;
1258 tree *slot = local_specializations->get (tmpl);
1259 return slot ? *slot : NULL_TREE;
1262 /* Returns nonzero iff DECL is a specialization of TMPL. */
1265 is_specialization_of (tree decl, tree tmpl)
1267 tree t;
1269 if (TREE_CODE (decl) == FUNCTION_DECL)
1271 for (t = decl;
1272 t != NULL_TREE;
1273 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1274 if (t == tmpl)
1275 return 1;
1277 else
1279 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1281 for (t = TREE_TYPE (decl);
1282 t != NULL_TREE;
1283 t = CLASSTYPE_USE_TEMPLATE (t)
1284 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1285 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1286 return 1;
1289 return 0;
1292 /* Returns nonzero iff DECL is a specialization of friend declaration
1293 FRIEND_DECL according to [temp.friend]. */
1295 bool
1296 is_specialization_of_friend (tree decl, tree friend_decl)
1298 bool need_template = true;
1299 int template_depth;
1301 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1302 || TREE_CODE (decl) == TYPE_DECL);
1304 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1305 of a template class, we want to check if DECL is a specialization
1306 if this. */
1307 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1308 && DECL_TEMPLATE_INFO (friend_decl)
1309 && !DECL_USE_TEMPLATE (friend_decl))
1311 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1312 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1313 need_template = false;
1315 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1316 && !PRIMARY_TEMPLATE_P (friend_decl))
1317 need_template = false;
1319 /* There is nothing to do if this is not a template friend. */
1320 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1321 return false;
1323 if (is_specialization_of (decl, friend_decl))
1324 return true;
1326 /* [temp.friend/6]
1327 A member of a class template may be declared to be a friend of a
1328 non-template class. In this case, the corresponding member of
1329 every specialization of the class template is a friend of the
1330 class granting friendship.
1332 For example, given a template friend declaration
1334 template <class T> friend void A<T>::f();
1336 the member function below is considered a friend
1338 template <> struct A<int> {
1339 void f();
1342 For this type of template friend, TEMPLATE_DEPTH below will be
1343 nonzero. To determine if DECL is a friend of FRIEND, we first
1344 check if the enclosing class is a specialization of another. */
1346 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1347 if (template_depth
1348 && DECL_CLASS_SCOPE_P (decl)
1349 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1350 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1352 /* Next, we check the members themselves. In order to handle
1353 a few tricky cases, such as when FRIEND_DECL's are
1355 template <class T> friend void A<T>::g(T t);
1356 template <class T> template <T t> friend void A<T>::h();
1358 and DECL's are
1360 void A<int>::g(int);
1361 template <int> void A<int>::h();
1363 we need to figure out ARGS, the template arguments from
1364 the context of DECL. This is required for template substitution
1365 of `T' in the function parameter of `g' and template parameter
1366 of `h' in the above examples. Here ARGS corresponds to `int'. */
1368 tree context = DECL_CONTEXT (decl);
1369 tree args = NULL_TREE;
1370 int current_depth = 0;
1372 while (current_depth < template_depth)
1374 if (CLASSTYPE_TEMPLATE_INFO (context))
1376 if (current_depth == 0)
1377 args = TYPE_TI_ARGS (context);
1378 else
1379 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1380 current_depth++;
1382 context = TYPE_CONTEXT (context);
1385 if (TREE_CODE (decl) == FUNCTION_DECL)
1387 bool is_template;
1388 tree friend_type;
1389 tree decl_type;
1390 tree friend_args_type;
1391 tree decl_args_type;
1393 /* Make sure that both DECL and FRIEND_DECL are templates or
1394 non-templates. */
1395 is_template = DECL_TEMPLATE_INFO (decl)
1396 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1397 if (need_template ^ is_template)
1398 return false;
1399 else if (is_template)
1401 /* If both are templates, check template parameter list. */
1402 tree friend_parms
1403 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1404 args, tf_none);
1405 if (!comp_template_parms
1406 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1407 friend_parms))
1408 return false;
1410 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1412 else
1413 decl_type = TREE_TYPE (decl);
1415 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1416 tf_none, NULL_TREE);
1417 if (friend_type == error_mark_node)
1418 return false;
1420 /* Check if return types match. */
1421 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1422 return false;
1424 /* Check if function parameter types match, ignoring the
1425 `this' parameter. */
1426 friend_args_type = TYPE_ARG_TYPES (friend_type);
1427 decl_args_type = TYPE_ARG_TYPES (decl_type);
1428 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1429 friend_args_type = TREE_CHAIN (friend_args_type);
1430 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1431 decl_args_type = TREE_CHAIN (decl_args_type);
1433 return compparms (decl_args_type, friend_args_type);
1435 else
1437 /* DECL is a TYPE_DECL */
1438 bool is_template;
1439 tree decl_type = TREE_TYPE (decl);
1441 /* Make sure that both DECL and FRIEND_DECL are templates or
1442 non-templates. */
1443 is_template
1444 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1445 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1447 if (need_template ^ is_template)
1448 return false;
1449 else if (is_template)
1451 tree friend_parms;
1452 /* If both are templates, check the name of the two
1453 TEMPLATE_DECL's first because is_friend didn't. */
1454 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1455 != DECL_NAME (friend_decl))
1456 return false;
1458 /* Now check template parameter list. */
1459 friend_parms
1460 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1461 args, tf_none);
1462 return comp_template_parms
1463 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1464 friend_parms);
1466 else
1467 return (DECL_NAME (decl)
1468 == DECL_NAME (friend_decl));
1471 return false;
1474 /* Register the specialization SPEC as a specialization of TMPL with
1475 the indicated ARGS. IS_FRIEND indicates whether the specialization
1476 is actually just a friend declaration. ATTRLIST is the list of
1477 attributes that the specialization is declared with or NULL when
1478 it isn't. Returns SPEC, or an equivalent prior declaration, if
1479 available.
1481 We also store instantiations of field packs in the hash table, even
1482 though they are not themselves templates, to make lookup easier. */
1484 static tree
1485 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1486 hashval_t hash)
1488 tree fn;
1489 spec_entry **slot = NULL;
1490 spec_entry elt;
1492 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1493 || (TREE_CODE (tmpl) == FIELD_DECL
1494 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1496 if (TREE_CODE (spec) == FUNCTION_DECL
1497 && uses_template_parms (DECL_TI_ARGS (spec)))
1498 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1499 register it; we want the corresponding TEMPLATE_DECL instead.
1500 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1501 the more obvious `uses_template_parms (spec)' to avoid problems
1502 with default function arguments. In particular, given
1503 something like this:
1505 template <class T> void f(T t1, T t = T())
1507 the default argument expression is not substituted for in an
1508 instantiation unless and until it is actually needed. */
1509 return spec;
1511 if (optimize_specialization_lookup_p (tmpl))
1512 /* We don't put these specializations in the hash table, but we might
1513 want to give an error about a mismatch. */
1514 fn = retrieve_specialization (tmpl, args, 0);
1515 else
1517 elt.tmpl = tmpl;
1518 elt.args = args;
1519 elt.spec = spec;
1521 if (hash == 0)
1522 hash = spec_hasher::hash (&elt);
1524 slot =
1525 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1526 if (*slot)
1527 fn = ((spec_entry *) *slot)->spec;
1528 else
1529 fn = NULL_TREE;
1532 /* We can sometimes try to re-register a specialization that we've
1533 already got. In particular, regenerate_decl_from_template calls
1534 duplicate_decls which will update the specialization list. But,
1535 we'll still get called again here anyhow. It's more convenient
1536 to simply allow this than to try to prevent it. */
1537 if (fn == spec)
1538 return spec;
1539 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1541 if (DECL_TEMPLATE_INSTANTIATION (fn))
1543 if (DECL_ODR_USED (fn)
1544 || DECL_EXPLICIT_INSTANTIATION (fn))
1546 error ("specialization of %qD after instantiation",
1547 fn);
1548 return error_mark_node;
1550 else
1552 tree clone;
1553 /* This situation should occur only if the first
1554 specialization is an implicit instantiation, the
1555 second is an explicit specialization, and the
1556 implicit instantiation has not yet been used. That
1557 situation can occur if we have implicitly
1558 instantiated a member function and then specialized
1559 it later.
1561 We can also wind up here if a friend declaration that
1562 looked like an instantiation turns out to be a
1563 specialization:
1565 template <class T> void foo(T);
1566 class S { friend void foo<>(int) };
1567 template <> void foo(int);
1569 We transform the existing DECL in place so that any
1570 pointers to it become pointers to the updated
1571 declaration.
1573 If there was a definition for the template, but not
1574 for the specialization, we want this to look as if
1575 there were no definition, and vice versa. */
1576 DECL_INITIAL (fn) = NULL_TREE;
1577 duplicate_decls (spec, fn, is_friend);
1578 /* The call to duplicate_decls will have applied
1579 [temp.expl.spec]:
1581 An explicit specialization of a function template
1582 is inline only if it is explicitly declared to be,
1583 and independently of whether its function template
1586 to the primary function; now copy the inline bits to
1587 the various clones. */
1588 FOR_EACH_CLONE (clone, fn)
1590 DECL_DECLARED_INLINE_P (clone)
1591 = DECL_DECLARED_INLINE_P (fn);
1592 DECL_SOURCE_LOCATION (clone)
1593 = DECL_SOURCE_LOCATION (fn);
1594 DECL_DELETED_FN (clone)
1595 = DECL_DELETED_FN (fn);
1597 check_specialization_namespace (tmpl);
1599 return fn;
1602 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1604 tree dd = duplicate_decls (spec, fn, is_friend);
1605 if (dd == error_mark_node)
1606 /* We've already complained in duplicate_decls. */
1607 return error_mark_node;
1609 if (dd == NULL_TREE && DECL_INITIAL (spec))
1610 /* Dup decl failed, but this is a new definition. Set the
1611 line number so any errors match this new
1612 definition. */
1613 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1615 return fn;
1618 else if (fn)
1619 return duplicate_decls (spec, fn, is_friend);
1621 /* A specialization must be declared in the same namespace as the
1622 template it is specializing. */
1623 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1624 && !check_specialization_namespace (tmpl))
1625 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1627 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1629 spec_entry *entry = ggc_alloc<spec_entry> ();
1630 gcc_assert (tmpl && args && spec);
1631 *entry = elt;
1632 *slot = entry;
1633 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1634 && PRIMARY_TEMPLATE_P (tmpl)
1635 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1636 || variable_template_p (tmpl))
1637 /* If TMPL is a forward declaration of a template function, keep a list
1638 of all specializations in case we need to reassign them to a friend
1639 template later in tsubst_friend_function.
1641 Also keep a list of all variable template instantiations so that
1642 process_partial_specialization can check whether a later partial
1643 specialization would have used it. */
1644 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1645 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1648 return spec;
1651 /* Returns true iff two spec_entry nodes are equivalent. */
1653 int comparing_specializations;
1655 bool
1656 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1658 int equal;
1660 ++comparing_specializations;
1661 equal = (e1->tmpl == e2->tmpl
1662 && comp_template_args (e1->args, e2->args));
1663 if (equal && flag_concepts
1664 /* tmpl could be a FIELD_DECL for a capture pack. */
1665 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1666 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1667 && uses_template_parms (e1->args))
1669 /* Partial specializations of a variable template can be distinguished by
1670 constraints. */
1671 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1672 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1673 equal = equivalent_constraints (c1, c2);
1675 --comparing_specializations;
1677 return equal;
1680 /* Returns a hash for a template TMPL and template arguments ARGS. */
1682 static hashval_t
1683 hash_tmpl_and_args (tree tmpl, tree args)
1685 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1686 return iterative_hash_template_arg (args, val);
1689 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1690 ignoring SPEC. */
1692 hashval_t
1693 spec_hasher::hash (spec_entry *e)
1695 return hash_tmpl_and_args (e->tmpl, e->args);
1698 /* Recursively calculate a hash value for a template argument ARG, for use
1699 in the hash tables of template specializations. */
1701 hashval_t
1702 iterative_hash_template_arg (tree arg, hashval_t val)
1704 unsigned HOST_WIDE_INT i;
1705 enum tree_code code;
1706 char tclass;
1708 if (arg == NULL_TREE)
1709 return iterative_hash_object (arg, val);
1711 if (!TYPE_P (arg))
1712 STRIP_NOPS (arg);
1714 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1715 gcc_unreachable ();
1717 code = TREE_CODE (arg);
1718 tclass = TREE_CODE_CLASS (code);
1720 val = iterative_hash_object (code, val);
1722 switch (code)
1724 case ERROR_MARK:
1725 return val;
1727 case IDENTIFIER_NODE:
1728 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1730 case TREE_VEC:
1732 int i, len = TREE_VEC_LENGTH (arg);
1733 for (i = 0; i < len; ++i)
1734 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1735 return val;
1738 case TYPE_PACK_EXPANSION:
1739 case EXPR_PACK_EXPANSION:
1740 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1741 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1743 case TYPE_ARGUMENT_PACK:
1744 case NONTYPE_ARGUMENT_PACK:
1745 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1747 case TREE_LIST:
1748 for (; arg; arg = TREE_CHAIN (arg))
1749 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1750 return val;
1752 case OVERLOAD:
1753 for (lkp_iterator iter (arg); iter; ++iter)
1754 val = iterative_hash_template_arg (*iter, val);
1755 return val;
1757 case CONSTRUCTOR:
1759 tree field, value;
1760 iterative_hash_template_arg (TREE_TYPE (arg), val);
1761 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1763 val = iterative_hash_template_arg (field, val);
1764 val = iterative_hash_template_arg (value, val);
1766 return val;
1769 case PARM_DECL:
1770 if (!DECL_ARTIFICIAL (arg))
1772 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1773 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1775 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1777 case TARGET_EXPR:
1778 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1780 case PTRMEM_CST:
1781 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1782 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1784 case TEMPLATE_PARM_INDEX:
1785 val = iterative_hash_template_arg
1786 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1787 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1788 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1790 case TRAIT_EXPR:
1791 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1792 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1793 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1795 case BASELINK:
1796 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1797 val);
1798 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1799 val);
1801 case MODOP_EXPR:
1802 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1803 code = TREE_CODE (TREE_OPERAND (arg, 1));
1804 val = iterative_hash_object (code, val);
1805 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1807 case LAMBDA_EXPR:
1808 /* A lambda can't appear in a template arg, but don't crash on
1809 erroneous input. */
1810 gcc_assert (seen_error ());
1811 return val;
1813 case CAST_EXPR:
1814 case IMPLICIT_CONV_EXPR:
1815 case STATIC_CAST_EXPR:
1816 case REINTERPRET_CAST_EXPR:
1817 case CONST_CAST_EXPR:
1818 case DYNAMIC_CAST_EXPR:
1819 case NEW_EXPR:
1820 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1821 /* Now hash operands as usual. */
1822 break;
1824 default:
1825 break;
1828 switch (tclass)
1830 case tcc_type:
1831 if (alias_template_specialization_p (arg))
1833 // We want an alias specialization that survived strip_typedefs
1834 // to hash differently from its TYPE_CANONICAL, to avoid hash
1835 // collisions that compare as different in template_args_equal.
1836 // These could be dependent specializations that strip_typedefs
1837 // left alone, or untouched specializations because
1838 // coerce_template_parms returns the unconverted template
1839 // arguments if it sees incomplete argument packs.
1840 tree ti = TYPE_ALIAS_TEMPLATE_INFO (arg);
1841 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1843 if (TYPE_CANONICAL (arg))
1844 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1845 val);
1846 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1847 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1848 /* Otherwise just compare the types during lookup. */
1849 return val;
1851 case tcc_declaration:
1852 case tcc_constant:
1853 return iterative_hash_expr (arg, val);
1855 default:
1856 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1858 unsigned n = cp_tree_operand_length (arg);
1859 for (i = 0; i < n; ++i)
1860 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1861 return val;
1864 gcc_unreachable ();
1865 return 0;
1868 /* Unregister the specialization SPEC as a specialization of TMPL.
1869 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1870 if the SPEC was listed as a specialization of TMPL.
1872 Note that SPEC has been ggc_freed, so we can't look inside it. */
1874 bool
1875 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1877 spec_entry *entry;
1878 spec_entry elt;
1880 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1881 elt.args = TI_ARGS (tinfo);
1882 elt.spec = NULL_TREE;
1884 entry = decl_specializations->find (&elt);
1885 if (entry != NULL)
1887 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1888 gcc_assert (new_spec != NULL_TREE);
1889 entry->spec = new_spec;
1890 return 1;
1893 return 0;
1896 /* Like register_specialization, but for local declarations. We are
1897 registering SPEC, an instantiation of TMPL. */
1899 void
1900 register_local_specialization (tree spec, tree tmpl)
1902 gcc_assert (tmpl != spec);
1903 local_specializations->put (tmpl, spec);
1906 /* TYPE is a class type. Returns true if TYPE is an explicitly
1907 specialized class. */
1909 bool
1910 explicit_class_specialization_p (tree type)
1912 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1913 return false;
1914 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1917 /* Print the list of functions at FNS, going through all the overloads
1918 for each element of the list. Alternatively, FNS can not be a
1919 TREE_LIST, in which case it will be printed together with all the
1920 overloads.
1922 MORE and *STR should respectively be FALSE and NULL when the function
1923 is called from the outside. They are used internally on recursive
1924 calls. print_candidates manages the two parameters and leaves NULL
1925 in *STR when it ends. */
1927 static void
1928 print_candidates_1 (tree fns, char **str, bool more = false)
1930 if (TREE_CODE (fns) == TREE_LIST)
1931 for (; fns; fns = TREE_CHAIN (fns))
1932 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
1933 else
1934 for (lkp_iterator iter (fns); iter;)
1936 tree cand = *iter;
1937 ++iter;
1939 const char *pfx = *str;
1940 if (!pfx)
1942 if (more || iter)
1943 pfx = _("candidates are:");
1944 else
1945 pfx = _("candidate is:");
1946 *str = get_spaces (pfx);
1948 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
1952 /* Print the list of candidate FNS in an error message. FNS can also
1953 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1955 void
1956 print_candidates (tree fns)
1958 char *str = NULL;
1959 print_candidates_1 (fns, &str);
1960 free (str);
1963 /* Get a (possibly) constrained template declaration for the
1964 purpose of ordering candidates. */
1965 static tree
1966 get_template_for_ordering (tree list)
1968 gcc_assert (TREE_CODE (list) == TREE_LIST);
1969 tree f = TREE_VALUE (list);
1970 if (tree ti = DECL_TEMPLATE_INFO (f))
1971 return TI_TEMPLATE (ti);
1972 return f;
1975 /* Among candidates having the same signature, return the
1976 most constrained or NULL_TREE if there is no best candidate.
1977 If the signatures of candidates vary (e.g., template
1978 specialization vs. member function), then there can be no
1979 most constrained.
1981 Note that we don't compare constraints on the functions
1982 themselves, but rather those of their templates. */
1983 static tree
1984 most_constrained_function (tree candidates)
1986 // Try to find the best candidate in a first pass.
1987 tree champ = candidates;
1988 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
1990 int winner = more_constrained (get_template_for_ordering (champ),
1991 get_template_for_ordering (c));
1992 if (winner == -1)
1993 champ = c; // The candidate is more constrained
1994 else if (winner == 0)
1995 return NULL_TREE; // Neither is more constrained
1998 // Verify that the champ is better than previous candidates.
1999 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2000 if (!more_constrained (get_template_for_ordering (champ),
2001 get_template_for_ordering (c)))
2002 return NULL_TREE;
2005 return champ;
2009 /* Returns the template (one of the functions given by TEMPLATE_ID)
2010 which can be specialized to match the indicated DECL with the
2011 explicit template args given in TEMPLATE_ID. The DECL may be
2012 NULL_TREE if none is available. In that case, the functions in
2013 TEMPLATE_ID are non-members.
2015 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2016 specialization of a member template.
2018 The TEMPLATE_COUNT is the number of references to qualifying
2019 template classes that appeared in the name of the function. See
2020 check_explicit_specialization for a more accurate description.
2022 TSK indicates what kind of template declaration (if any) is being
2023 declared. TSK_TEMPLATE indicates that the declaration given by
2024 DECL, though a FUNCTION_DECL, has template parameters, and is
2025 therefore a template function.
2027 The template args (those explicitly specified and those deduced)
2028 are output in a newly created vector *TARGS_OUT.
2030 If it is impossible to determine the result, an error message is
2031 issued. The error_mark_node is returned to indicate failure. */
2033 static tree
2034 determine_specialization (tree template_id,
2035 tree decl,
2036 tree* targs_out,
2037 int need_member_template,
2038 int template_count,
2039 tmpl_spec_kind tsk)
2041 tree fns;
2042 tree targs;
2043 tree explicit_targs;
2044 tree candidates = NULL_TREE;
2046 /* A TREE_LIST of templates of which DECL may be a specialization.
2047 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2048 corresponding TREE_PURPOSE is the set of template arguments that,
2049 when used to instantiate the template, would produce a function
2050 with the signature of DECL. */
2051 tree templates = NULL_TREE;
2052 int header_count;
2053 cp_binding_level *b;
2055 *targs_out = NULL_TREE;
2057 if (template_id == error_mark_node || decl == error_mark_node)
2058 return error_mark_node;
2060 /* We shouldn't be specializing a member template of an
2061 unspecialized class template; we already gave an error in
2062 check_specialization_scope, now avoid crashing. */
2063 if (!VAR_P (decl)
2064 && template_count && DECL_CLASS_SCOPE_P (decl)
2065 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2067 gcc_assert (errorcount);
2068 return error_mark_node;
2071 fns = TREE_OPERAND (template_id, 0);
2072 explicit_targs = TREE_OPERAND (template_id, 1);
2074 if (fns == error_mark_node)
2075 return error_mark_node;
2077 /* Check for baselinks. */
2078 if (BASELINK_P (fns))
2079 fns = BASELINK_FUNCTIONS (fns);
2081 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2083 error ("%qD is not a function template", fns);
2084 return error_mark_node;
2086 else if (VAR_P (decl) && !variable_template_p (fns))
2088 error ("%qD is not a variable template", fns);
2089 return error_mark_node;
2092 /* Count the number of template headers specified for this
2093 specialization. */
2094 header_count = 0;
2095 for (b = current_binding_level;
2096 b->kind == sk_template_parms;
2097 b = b->level_chain)
2098 ++header_count;
2100 tree orig_fns = fns;
2102 if (variable_template_p (fns))
2104 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2105 targs = coerce_template_parms (parms, explicit_targs, fns,
2106 tf_warning_or_error,
2107 /*req_all*/true, /*use_defarg*/true);
2108 if (targs != error_mark_node)
2109 templates = tree_cons (targs, fns, templates);
2111 else for (lkp_iterator iter (fns); iter; ++iter)
2113 tree fn = *iter;
2115 if (TREE_CODE (fn) == TEMPLATE_DECL)
2117 tree decl_arg_types;
2118 tree fn_arg_types;
2119 tree insttype;
2121 /* In case of explicit specialization, we need to check if
2122 the number of template headers appearing in the specialization
2123 is correct. This is usually done in check_explicit_specialization,
2124 but the check done there cannot be exhaustive when specializing
2125 member functions. Consider the following code:
2127 template <> void A<int>::f(int);
2128 template <> template <> void A<int>::f(int);
2130 Assuming that A<int> is not itself an explicit specialization
2131 already, the first line specializes "f" which is a non-template
2132 member function, whilst the second line specializes "f" which
2133 is a template member function. So both lines are syntactically
2134 correct, and check_explicit_specialization does not reject
2135 them.
2137 Here, we can do better, as we are matching the specialization
2138 against the declarations. We count the number of template
2139 headers, and we check if they match TEMPLATE_COUNT + 1
2140 (TEMPLATE_COUNT is the number of qualifying template classes,
2141 plus there must be another header for the member template
2142 itself).
2144 Notice that if header_count is zero, this is not a
2145 specialization but rather a template instantiation, so there
2146 is no check we can perform here. */
2147 if (header_count && header_count != template_count + 1)
2148 continue;
2150 /* Check that the number of template arguments at the
2151 innermost level for DECL is the same as for FN. */
2152 if (current_binding_level->kind == sk_template_parms
2153 && !current_binding_level->explicit_spec_p
2154 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2155 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2156 (current_template_parms))))
2157 continue;
2159 /* DECL might be a specialization of FN. */
2160 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2161 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2163 /* For a non-static member function, we need to make sure
2164 that the const qualification is the same. Since
2165 get_bindings does not try to merge the "this" parameter,
2166 we must do the comparison explicitly. */
2167 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2169 if (!same_type_p (TREE_VALUE (fn_arg_types),
2170 TREE_VALUE (decl_arg_types)))
2171 continue;
2173 /* And the ref-qualification. */
2174 if (type_memfn_rqual (TREE_TYPE (decl))
2175 != type_memfn_rqual (TREE_TYPE (fn)))
2176 continue;
2179 /* Skip the "this" parameter and, for constructors of
2180 classes with virtual bases, the VTT parameter. A
2181 full specialization of a constructor will have a VTT
2182 parameter, but a template never will. */
2183 decl_arg_types
2184 = skip_artificial_parms_for (decl, decl_arg_types);
2185 fn_arg_types
2186 = skip_artificial_parms_for (fn, fn_arg_types);
2188 /* Function templates cannot be specializations; there are
2189 no partial specializations of functions. Therefore, if
2190 the type of DECL does not match FN, there is no
2191 match.
2193 Note that it should never be the case that we have both
2194 candidates added here, and for regular member functions
2195 below. */
2196 if (tsk == tsk_template)
2198 if (compparms (fn_arg_types, decl_arg_types))
2199 candidates = tree_cons (NULL_TREE, fn, candidates);
2200 continue;
2203 /* See whether this function might be a specialization of this
2204 template. Suppress access control because we might be trying
2205 to make this specialization a friend, and we have already done
2206 access control for the declaration of the specialization. */
2207 push_deferring_access_checks (dk_no_check);
2208 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2209 pop_deferring_access_checks ();
2211 if (!targs)
2212 /* We cannot deduce template arguments that when used to
2213 specialize TMPL will produce DECL. */
2214 continue;
2216 if (uses_template_parms (targs))
2217 /* We deduced something involving 'auto', which isn't a valid
2218 template argument. */
2219 continue;
2221 /* Remove, from the set of candidates, all those functions
2222 whose constraints are not satisfied. */
2223 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2224 continue;
2226 // Then, try to form the new function type.
2227 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2228 if (insttype == error_mark_node)
2229 continue;
2230 fn_arg_types
2231 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2232 if (!compparms (fn_arg_types, decl_arg_types))
2233 continue;
2235 /* Save this template, and the arguments deduced. */
2236 templates = tree_cons (targs, fn, templates);
2238 else if (need_member_template)
2239 /* FN is an ordinary member function, and we need a
2240 specialization of a member template. */
2242 else if (TREE_CODE (fn) != FUNCTION_DECL)
2243 /* We can get IDENTIFIER_NODEs here in certain erroneous
2244 cases. */
2246 else if (!DECL_FUNCTION_MEMBER_P (fn))
2247 /* This is just an ordinary non-member function. Nothing can
2248 be a specialization of that. */
2250 else if (DECL_ARTIFICIAL (fn))
2251 /* Cannot specialize functions that are created implicitly. */
2253 else
2255 tree decl_arg_types;
2257 /* This is an ordinary member function. However, since
2258 we're here, we can assume its enclosing class is a
2259 template class. For example,
2261 template <typename T> struct S { void f(); };
2262 template <> void S<int>::f() {}
2264 Here, S<int>::f is a non-template, but S<int> is a
2265 template class. If FN has the same type as DECL, we
2266 might be in business. */
2268 if (!DECL_TEMPLATE_INFO (fn))
2269 /* Its enclosing class is an explicit specialization
2270 of a template class. This is not a candidate. */
2271 continue;
2273 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2274 TREE_TYPE (TREE_TYPE (fn))))
2275 /* The return types differ. */
2276 continue;
2278 /* Adjust the type of DECL in case FN is a static member. */
2279 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2280 if (DECL_STATIC_FUNCTION_P (fn)
2281 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2282 decl_arg_types = TREE_CHAIN (decl_arg_types);
2284 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2285 decl_arg_types))
2286 continue;
2288 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2289 && (type_memfn_rqual (TREE_TYPE (decl))
2290 != type_memfn_rqual (TREE_TYPE (fn))))
2291 continue;
2293 // If the deduced arguments do not satisfy the constraints,
2294 // this is not a candidate.
2295 if (flag_concepts && !constraints_satisfied_p (fn))
2296 continue;
2298 // Add the candidate.
2299 candidates = tree_cons (NULL_TREE, fn, candidates);
2303 if (templates && TREE_CHAIN (templates))
2305 /* We have:
2307 [temp.expl.spec]
2309 It is possible for a specialization with a given function
2310 signature to be instantiated from more than one function
2311 template. In such cases, explicit specification of the
2312 template arguments must be used to uniquely identify the
2313 function template specialization being specialized.
2315 Note that here, there's no suggestion that we're supposed to
2316 determine which of the candidate templates is most
2317 specialized. However, we, also have:
2319 [temp.func.order]
2321 Partial ordering of overloaded function template
2322 declarations is used in the following contexts to select
2323 the function template to which a function template
2324 specialization refers:
2326 -- when an explicit specialization refers to a function
2327 template.
2329 So, we do use the partial ordering rules, at least for now.
2330 This extension can only serve to make invalid programs valid,
2331 so it's safe. And, there is strong anecdotal evidence that
2332 the committee intended the partial ordering rules to apply;
2333 the EDG front end has that behavior, and John Spicer claims
2334 that the committee simply forgot to delete the wording in
2335 [temp.expl.spec]. */
2336 tree tmpl = most_specialized_instantiation (templates);
2337 if (tmpl != error_mark_node)
2339 templates = tmpl;
2340 TREE_CHAIN (templates) = NULL_TREE;
2344 // Concepts allows multiple declarations of member functions
2345 // with the same signature. Like above, we need to rely on
2346 // on the partial ordering of those candidates to determine which
2347 // is the best.
2348 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2350 if (tree cand = most_constrained_function (candidates))
2352 candidates = cand;
2353 TREE_CHAIN (cand) = NULL_TREE;
2357 if (templates == NULL_TREE && candidates == NULL_TREE)
2359 error ("template-id %qD for %q+D does not match any template "
2360 "declaration", template_id, decl);
2361 if (header_count && header_count != template_count + 1)
2362 inform (input_location, "saw %d %<template<>%>, need %d for "
2363 "specializing a member function template",
2364 header_count, template_count + 1);
2365 else
2366 print_candidates (orig_fns);
2367 return error_mark_node;
2369 else if ((templates && TREE_CHAIN (templates))
2370 || (candidates && TREE_CHAIN (candidates))
2371 || (templates && candidates))
2373 error ("ambiguous template specialization %qD for %q+D",
2374 template_id, decl);
2375 candidates = chainon (candidates, templates);
2376 print_candidates (candidates);
2377 return error_mark_node;
2380 /* We have one, and exactly one, match. */
2381 if (candidates)
2383 tree fn = TREE_VALUE (candidates);
2384 *targs_out = copy_node (DECL_TI_ARGS (fn));
2386 // Propagate the candidate's constraints to the declaration.
2387 set_constraints (decl, get_constraints (fn));
2389 /* DECL is a re-declaration or partial instantiation of a template
2390 function. */
2391 if (TREE_CODE (fn) == TEMPLATE_DECL)
2392 return fn;
2393 /* It was a specialization of an ordinary member function in a
2394 template class. */
2395 return DECL_TI_TEMPLATE (fn);
2398 /* It was a specialization of a template. */
2399 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2400 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2402 *targs_out = copy_node (targs);
2403 SET_TMPL_ARGS_LEVEL (*targs_out,
2404 TMPL_ARGS_DEPTH (*targs_out),
2405 TREE_PURPOSE (templates));
2407 else
2408 *targs_out = TREE_PURPOSE (templates);
2409 return TREE_VALUE (templates);
2412 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2413 but with the default argument values filled in from those in the
2414 TMPL_TYPES. */
2416 static tree
2417 copy_default_args_to_explicit_spec_1 (tree spec_types,
2418 tree tmpl_types)
2420 tree new_spec_types;
2422 if (!spec_types)
2423 return NULL_TREE;
2425 if (spec_types == void_list_node)
2426 return void_list_node;
2428 /* Substitute into the rest of the list. */
2429 new_spec_types =
2430 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2431 TREE_CHAIN (tmpl_types));
2433 /* Add the default argument for this parameter. */
2434 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2435 TREE_VALUE (spec_types),
2436 new_spec_types);
2439 /* DECL is an explicit specialization. Replicate default arguments
2440 from the template it specializes. (That way, code like:
2442 template <class T> void f(T = 3);
2443 template <> void f(double);
2444 void g () { f (); }
2446 works, as required.) An alternative approach would be to look up
2447 the correct default arguments at the call-site, but this approach
2448 is consistent with how implicit instantiations are handled. */
2450 static void
2451 copy_default_args_to_explicit_spec (tree decl)
2453 tree tmpl;
2454 tree spec_types;
2455 tree tmpl_types;
2456 tree new_spec_types;
2457 tree old_type;
2458 tree new_type;
2459 tree t;
2460 tree object_type = NULL_TREE;
2461 tree in_charge = NULL_TREE;
2462 tree vtt = NULL_TREE;
2464 /* See if there's anything we need to do. */
2465 tmpl = DECL_TI_TEMPLATE (decl);
2466 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2467 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2468 if (TREE_PURPOSE (t))
2469 break;
2470 if (!t)
2471 return;
2473 old_type = TREE_TYPE (decl);
2474 spec_types = TYPE_ARG_TYPES (old_type);
2476 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2478 /* Remove the this pointer, but remember the object's type for
2479 CV quals. */
2480 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2481 spec_types = TREE_CHAIN (spec_types);
2482 tmpl_types = TREE_CHAIN (tmpl_types);
2484 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2486 /* DECL may contain more parameters than TMPL due to the extra
2487 in-charge parameter in constructors and destructors. */
2488 in_charge = spec_types;
2489 spec_types = TREE_CHAIN (spec_types);
2491 if (DECL_HAS_VTT_PARM_P (decl))
2493 vtt = spec_types;
2494 spec_types = TREE_CHAIN (spec_types);
2498 /* Compute the merged default arguments. */
2499 new_spec_types =
2500 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2502 /* Compute the new FUNCTION_TYPE. */
2503 if (object_type)
2505 if (vtt)
2506 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2507 TREE_VALUE (vtt),
2508 new_spec_types);
2510 if (in_charge)
2511 /* Put the in-charge parameter back. */
2512 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2513 TREE_VALUE (in_charge),
2514 new_spec_types);
2516 new_type = build_method_type_directly (object_type,
2517 TREE_TYPE (old_type),
2518 new_spec_types);
2520 else
2521 new_type = build_function_type (TREE_TYPE (old_type),
2522 new_spec_types);
2523 new_type = cp_build_type_attribute_variant (new_type,
2524 TYPE_ATTRIBUTES (old_type));
2525 new_type = build_exception_variant (new_type,
2526 TYPE_RAISES_EXCEPTIONS (old_type));
2528 if (TYPE_HAS_LATE_RETURN_TYPE (old_type))
2529 TYPE_HAS_LATE_RETURN_TYPE (new_type) = 1;
2531 TREE_TYPE (decl) = new_type;
2534 /* Return the number of template headers we expect to see for a definition
2535 or specialization of CTYPE or one of its non-template members. */
2538 num_template_headers_for_class (tree ctype)
2540 int num_templates = 0;
2542 while (ctype && CLASS_TYPE_P (ctype))
2544 /* You're supposed to have one `template <...>' for every
2545 template class, but you don't need one for a full
2546 specialization. For example:
2548 template <class T> struct S{};
2549 template <> struct S<int> { void f(); };
2550 void S<int>::f () {}
2552 is correct; there shouldn't be a `template <>' for the
2553 definition of `S<int>::f'. */
2554 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2555 /* If CTYPE does not have template information of any
2556 kind, then it is not a template, nor is it nested
2557 within a template. */
2558 break;
2559 if (explicit_class_specialization_p (ctype))
2560 break;
2561 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2562 ++num_templates;
2564 ctype = TYPE_CONTEXT (ctype);
2567 return num_templates;
2570 /* Do a simple sanity check on the template headers that precede the
2571 variable declaration DECL. */
2573 void
2574 check_template_variable (tree decl)
2576 tree ctx = CP_DECL_CONTEXT (decl);
2577 int wanted = num_template_headers_for_class (ctx);
2578 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2579 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2581 if (cxx_dialect < cxx14)
2582 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2583 "variable templates only available with "
2584 "-std=c++14 or -std=gnu++14");
2586 // Namespace-scope variable templates should have a template header.
2587 ++wanted;
2589 if (template_header_count > wanted)
2591 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2592 "too many template headers for %qD "
2593 "(should be %d)",
2594 decl, wanted);
2595 if (warned && CLASS_TYPE_P (ctx)
2596 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2597 inform (DECL_SOURCE_LOCATION (decl),
2598 "members of an explicitly specialized class are defined "
2599 "without a template header");
2603 /* An explicit specialization whose declarator-id or class-head-name is not
2604 qualified shall be declared in the nearest enclosing namespace of the
2605 template, or, if the namespace is inline (7.3.1), any namespace from its
2606 enclosing namespace set.
2608 If the name declared in the explicit instantiation is an unqualified name,
2609 the explicit instantiation shall appear in the namespace where its template
2610 is declared or, if that namespace is inline (7.3.1), any namespace from its
2611 enclosing namespace set. */
2613 void
2614 check_unqualified_spec_or_inst (tree t, location_t loc)
2616 tree tmpl = most_general_template (t);
2617 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2618 && !is_nested_namespace (current_namespace,
2619 CP_DECL_CONTEXT (tmpl), true))
2621 if (processing_specialization)
2622 permerror (loc, "explicit specialization of %qD outside its "
2623 "namespace must use a nested-name-specifier", tmpl);
2624 else if (processing_explicit_instantiation
2625 && cxx_dialect >= cxx11)
2626 /* This was allowed in C++98, so only pedwarn. */
2627 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2628 "outside its namespace must use a nested-name-"
2629 "specifier", tmpl);
2633 /* Warn for a template specialization SPEC that is missing some of a set
2634 of function or type attributes that the template TEMPL is declared with.
2635 ATTRLIST is a list of additional attributes that SPEC should be taken
2636 to ultimately be declared with. */
2638 static void
2639 warn_spec_missing_attributes (tree tmpl, tree spec, tree attrlist)
2641 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
2642 tmpl = DECL_TEMPLATE_RESULT (tmpl);
2644 if (TREE_CODE (tmpl) != FUNCTION_DECL)
2645 return;
2647 /* Avoid warning if either declaration or its type is deprecated. */
2648 if (TREE_DEPRECATED (tmpl)
2649 || TREE_DEPRECATED (spec))
2650 return;
2652 tree tmpl_type = TREE_TYPE (tmpl);
2653 tree spec_type = TREE_TYPE (spec);
2655 if (TREE_DEPRECATED (tmpl_type)
2656 || TREE_DEPRECATED (spec_type)
2657 || TREE_DEPRECATED (TREE_TYPE (tmpl_type))
2658 || TREE_DEPRECATED (TREE_TYPE (spec_type)))
2659 return;
2661 tree tmpl_attrs[] = { DECL_ATTRIBUTES (tmpl), TYPE_ATTRIBUTES (tmpl_type) };
2662 tree spec_attrs[] = { DECL_ATTRIBUTES (spec), TYPE_ATTRIBUTES (spec_type) };
2664 if (!spec_attrs[0])
2665 spec_attrs[0] = attrlist;
2666 else if (!spec_attrs[1])
2667 spec_attrs[1] = attrlist;
2669 /* Avoid warning if the primary has no attributes. */
2670 if (!tmpl_attrs[0] && !tmpl_attrs[1])
2671 return;
2673 /* Avoid warning if either declaration contains an attribute on
2674 the white list below. */
2675 const char* const whitelist[] = {
2676 "error", "warning"
2679 for (unsigned i = 0; i != 2; ++i)
2680 for (unsigned j = 0; j != sizeof whitelist / sizeof *whitelist; ++j)
2681 if (lookup_attribute (whitelist[j], tmpl_attrs[i])
2682 || lookup_attribute (whitelist[j], spec_attrs[i]))
2683 return;
2685 /* Avoid warning if the difference between the primary and
2686 the specialization is not in one of the attributes below. */
2687 const char* const blacklist[] = {
2688 "alloc_align", "alloc_size", "assume_aligned", "format",
2689 "format_arg", "malloc", "nonnull"
2692 /* Put together a list of the black listed attributes that the primary
2693 template is declared with that the specialization is not, in case
2694 it's not apparent from the most recent declaration of the primary. */
2695 unsigned nattrs = 0;
2696 pretty_printer str;
2698 for (unsigned i = 0; i != sizeof blacklist / sizeof *blacklist; ++i)
2700 for (unsigned j = 0; j != 2; ++j)
2702 if (!lookup_attribute (blacklist[i], tmpl_attrs[j]))
2703 continue;
2705 for (unsigned k = 0; k != 1 + !!spec_attrs[1]; ++k)
2707 if (lookup_attribute (blacklist[i], spec_attrs[k]))
2708 break;
2710 if (nattrs)
2711 pp_string (&str, ", ");
2712 pp_begin_quote (&str, pp_show_color (global_dc->printer));
2713 pp_string (&str, blacklist[i]);
2714 pp_end_quote (&str, pp_show_color (global_dc->printer));
2715 ++nattrs;
2720 if (!nattrs)
2721 return;
2723 if (warning_at (DECL_SOURCE_LOCATION (spec), OPT_Wmissing_attributes,
2724 "explicit specialization %q#D may be missing attributes",
2725 spec))
2726 inform (DECL_SOURCE_LOCATION (tmpl),
2727 nattrs > 1
2728 ? G_("missing primary template attributes %s")
2729 : G_("missing primary template attribute %s"),
2730 pp_formatted_text (&str));
2733 /* Check to see if the function just declared, as indicated in
2734 DECLARATOR, and in DECL, is a specialization of a function
2735 template. We may also discover that the declaration is an explicit
2736 instantiation at this point.
2738 Returns DECL, or an equivalent declaration that should be used
2739 instead if all goes well. Issues an error message if something is
2740 amiss. Returns error_mark_node if the error is not easily
2741 recoverable.
2743 FLAGS is a bitmask consisting of the following flags:
2745 2: The function has a definition.
2746 4: The function is a friend.
2748 The TEMPLATE_COUNT is the number of references to qualifying
2749 template classes that appeared in the name of the function. For
2750 example, in
2752 template <class T> struct S { void f(); };
2753 void S<int>::f();
2755 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2756 classes are not counted in the TEMPLATE_COUNT, so that in
2758 template <class T> struct S {};
2759 template <> struct S<int> { void f(); }
2760 template <> void S<int>::f();
2762 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2763 invalid; there should be no template <>.)
2765 If the function is a specialization, it is marked as such via
2766 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2767 is set up correctly, and it is added to the list of specializations
2768 for that template. */
2770 tree
2771 check_explicit_specialization (tree declarator,
2772 tree decl,
2773 int template_count,
2774 int flags,
2775 tree attrlist)
2777 int have_def = flags & 2;
2778 int is_friend = flags & 4;
2779 bool is_concept = flags & 8;
2780 int specialization = 0;
2781 int explicit_instantiation = 0;
2782 int member_specialization = 0;
2783 tree ctype = DECL_CLASS_CONTEXT (decl);
2784 tree dname = DECL_NAME (decl);
2785 tmpl_spec_kind tsk;
2787 if (is_friend)
2789 if (!processing_specialization)
2790 tsk = tsk_none;
2791 else
2792 tsk = tsk_excessive_parms;
2794 else
2795 tsk = current_tmpl_spec_kind (template_count);
2797 switch (tsk)
2799 case tsk_none:
2800 if (processing_specialization && !VAR_P (decl))
2802 specialization = 1;
2803 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2805 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2807 if (is_friend)
2808 /* This could be something like:
2810 template <class T> void f(T);
2811 class S { friend void f<>(int); } */
2812 specialization = 1;
2813 else
2815 /* This case handles bogus declarations like template <>
2816 template <class T> void f<int>(); */
2818 error ("template-id %qD in declaration of primary template",
2819 declarator);
2820 return decl;
2823 break;
2825 case tsk_invalid_member_spec:
2826 /* The error has already been reported in
2827 check_specialization_scope. */
2828 return error_mark_node;
2830 case tsk_invalid_expl_inst:
2831 error ("template parameter list used in explicit instantiation");
2833 /* Fall through. */
2835 case tsk_expl_inst:
2836 if (have_def)
2837 error ("definition provided for explicit instantiation");
2839 explicit_instantiation = 1;
2840 break;
2842 case tsk_excessive_parms:
2843 case tsk_insufficient_parms:
2844 if (tsk == tsk_excessive_parms)
2845 error ("too many template parameter lists in declaration of %qD",
2846 decl);
2847 else if (template_header_count)
2848 error("too few template parameter lists in declaration of %qD", decl);
2849 else
2850 error("explicit specialization of %qD must be introduced by "
2851 "%<template <>%>", decl);
2853 /* Fall through. */
2854 case tsk_expl_spec:
2855 if (is_concept)
2856 error ("explicit specialization declared %<concept%>");
2858 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2859 /* In cases like template<> constexpr bool v = true;
2860 We'll give an error in check_template_variable. */
2861 break;
2863 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2864 if (ctype)
2865 member_specialization = 1;
2866 else
2867 specialization = 1;
2868 break;
2870 case tsk_template:
2871 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2873 /* This case handles bogus declarations like template <>
2874 template <class T> void f<int>(); */
2876 if (!uses_template_parms (declarator))
2877 error ("template-id %qD in declaration of primary template",
2878 declarator);
2879 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2881 /* Partial specialization of variable template. */
2882 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2883 specialization = 1;
2884 goto ok;
2886 else if (cxx_dialect < cxx14)
2887 error ("non-type partial specialization %qD "
2888 "is not allowed", declarator);
2889 else
2890 error ("non-class, non-variable partial specialization %qD "
2891 "is not allowed", declarator);
2892 return decl;
2893 ok:;
2896 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2897 /* This is a specialization of a member template, without
2898 specialization the containing class. Something like:
2900 template <class T> struct S {
2901 template <class U> void f (U);
2903 template <> template <class U> void S<int>::f(U) {}
2905 That's a specialization -- but of the entire template. */
2906 specialization = 1;
2907 break;
2909 default:
2910 gcc_unreachable ();
2913 if ((specialization || member_specialization)
2914 /* This doesn't apply to variable templates. */
2915 && (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE
2916 || TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE))
2918 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2919 for (; t; t = TREE_CHAIN (t))
2920 if (TREE_PURPOSE (t))
2922 permerror (input_location,
2923 "default argument specified in explicit specialization");
2924 break;
2928 if (specialization || member_specialization || explicit_instantiation)
2930 tree tmpl = NULL_TREE;
2931 tree targs = NULL_TREE;
2932 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2934 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2935 if (!was_template_id)
2937 tree fns;
2939 gcc_assert (identifier_p (declarator));
2940 if (ctype)
2941 fns = dname;
2942 else
2944 /* If there is no class context, the explicit instantiation
2945 must be at namespace scope. */
2946 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2948 /* Find the namespace binding, using the declaration
2949 context. */
2950 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2951 false, true);
2952 if (fns == error_mark_node)
2953 /* If lookup fails, look for a friend declaration so we can
2954 give a better diagnostic. */
2955 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2956 /*type*/false, /*complain*/true,
2957 /*hidden*/true);
2959 if (fns == error_mark_node || !is_overloaded_fn (fns))
2961 error ("%qD is not a template function", dname);
2962 fns = error_mark_node;
2966 declarator = lookup_template_function (fns, NULL_TREE);
2969 if (declarator == error_mark_node)
2970 return error_mark_node;
2972 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2974 if (!explicit_instantiation)
2975 /* A specialization in class scope. This is invalid,
2976 but the error will already have been flagged by
2977 check_specialization_scope. */
2978 return error_mark_node;
2979 else
2981 /* It's not valid to write an explicit instantiation in
2982 class scope, e.g.:
2984 class C { template void f(); }
2986 This case is caught by the parser. However, on
2987 something like:
2989 template class C { void f(); };
2991 (which is invalid) we can get here. The error will be
2992 issued later. */
2996 return decl;
2998 else if (ctype != NULL_TREE
2999 && (identifier_p (TREE_OPERAND (declarator, 0))))
3001 // We'll match variable templates in start_decl.
3002 if (VAR_P (decl))
3003 return decl;
3005 /* Find the list of functions in ctype that have the same
3006 name as the declared function. */
3007 tree name = TREE_OPERAND (declarator, 0);
3009 if (constructor_name_p (name, ctype))
3011 if (DECL_CONSTRUCTOR_P (decl)
3012 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
3013 : !CLASSTYPE_DESTRUCTOR (ctype))
3015 /* From [temp.expl.spec]:
3017 If such an explicit specialization for the member
3018 of a class template names an implicitly-declared
3019 special member function (clause _special_), the
3020 program is ill-formed.
3022 Similar language is found in [temp.explicit]. */
3023 error ("specialization of implicitly-declared special member function");
3024 return error_mark_node;
3027 name = DECL_NAME (decl);
3030 /* For a type-conversion operator, We might be looking for
3031 `operator int' which will be a specialization of
3032 `operator T'. Grab all the conversion operators, and
3033 then select from them. */
3034 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
3035 ? conv_op_identifier : name);
3037 if (fns == NULL_TREE)
3039 error ("no member function %qD declared in %qT", name, ctype);
3040 return error_mark_node;
3042 else
3043 TREE_OPERAND (declarator, 0) = fns;
3046 /* Figure out what exactly is being specialized at this point.
3047 Note that for an explicit instantiation, even one for a
3048 member function, we cannot tell a priori whether the
3049 instantiation is for a member template, or just a member
3050 function of a template class. Even if a member template is
3051 being instantiated, the member template arguments may be
3052 elided if they can be deduced from the rest of the
3053 declaration. */
3054 tmpl = determine_specialization (declarator, decl,
3055 &targs,
3056 member_specialization,
3057 template_count,
3058 tsk);
3060 if (!tmpl || tmpl == error_mark_node)
3061 /* We couldn't figure out what this declaration was
3062 specializing. */
3063 return error_mark_node;
3064 else
3066 if (TREE_CODE (decl) == FUNCTION_DECL
3067 && DECL_HIDDEN_FRIEND_P (tmpl))
3069 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3070 "friend declaration %qD is not visible to "
3071 "explicit specialization", tmpl))
3072 inform (DECL_SOURCE_LOCATION (tmpl),
3073 "friend declaration here");
3075 else if (!ctype && !is_friend
3076 && CP_DECL_CONTEXT (decl) == current_namespace)
3077 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
3079 tree gen_tmpl = most_general_template (tmpl);
3081 if (explicit_instantiation)
3083 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3084 is done by do_decl_instantiation later. */
3086 int arg_depth = TMPL_ARGS_DEPTH (targs);
3087 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3089 if (arg_depth > parm_depth)
3091 /* If TMPL is not the most general template (for
3092 example, if TMPL is a friend template that is
3093 injected into namespace scope), then there will
3094 be too many levels of TARGS. Remove some of them
3095 here. */
3096 int i;
3097 tree new_targs;
3099 new_targs = make_tree_vec (parm_depth);
3100 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3101 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3102 = TREE_VEC_ELT (targs, i);
3103 targs = new_targs;
3106 return instantiate_template (tmpl, targs, tf_error);
3109 /* If we thought that the DECL was a member function, but it
3110 turns out to be specializing a static member function,
3111 make DECL a static member function as well. */
3112 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3113 && DECL_STATIC_FUNCTION_P (tmpl)
3114 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3115 revert_static_member_fn (decl);
3117 /* If this is a specialization of a member template of a
3118 template class, we want to return the TEMPLATE_DECL, not
3119 the specialization of it. */
3120 if (tsk == tsk_template && !was_template_id)
3122 tree result = DECL_TEMPLATE_RESULT (tmpl);
3123 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3124 DECL_INITIAL (result) = NULL_TREE;
3125 if (have_def)
3127 tree parm;
3128 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3129 DECL_SOURCE_LOCATION (result)
3130 = DECL_SOURCE_LOCATION (decl);
3131 /* We want to use the argument list specified in the
3132 definition, not in the original declaration. */
3133 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3134 for (parm = DECL_ARGUMENTS (result); parm;
3135 parm = DECL_CHAIN (parm))
3136 DECL_CONTEXT (parm) = result;
3138 return register_specialization (tmpl, gen_tmpl, targs,
3139 is_friend, 0);
3142 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3143 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3145 if (was_template_id)
3146 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3148 /* Inherit default function arguments from the template
3149 DECL is specializing. */
3150 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3151 copy_default_args_to_explicit_spec (decl);
3153 /* This specialization has the same protection as the
3154 template it specializes. */
3155 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3156 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3158 /* 7.1.1-1 [dcl.stc]
3160 A storage-class-specifier shall not be specified in an
3161 explicit specialization...
3163 The parser rejects these, so unless action is taken here,
3164 explicit function specializations will always appear with
3165 global linkage.
3167 The action recommended by the C++ CWG in response to C++
3168 defect report 605 is to make the storage class and linkage
3169 of the explicit specialization match the templated function:
3171 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3173 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3175 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3176 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3178 /* A concept cannot be specialized. */
3179 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3181 error ("explicit specialization of function concept %qD",
3182 gen_tmpl);
3183 return error_mark_node;
3186 /* This specialization has the same linkage and visibility as
3187 the function template it specializes. */
3188 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3189 if (! TREE_PUBLIC (decl))
3191 DECL_INTERFACE_KNOWN (decl) = 1;
3192 DECL_NOT_REALLY_EXTERN (decl) = 1;
3194 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3195 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3197 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3198 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3202 /* If DECL is a friend declaration, declared using an
3203 unqualified name, the namespace associated with DECL may
3204 have been set incorrectly. For example, in:
3206 template <typename T> void f(T);
3207 namespace N {
3208 struct S { friend void f<int>(int); }
3211 we will have set the DECL_CONTEXT for the friend
3212 declaration to N, rather than to the global namespace. */
3213 if (DECL_NAMESPACE_SCOPE_P (decl))
3214 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3216 if (is_friend && !have_def)
3217 /* This is not really a declaration of a specialization.
3218 It's just the name of an instantiation. But, it's not
3219 a request for an instantiation, either. */
3220 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3221 else if (TREE_CODE (decl) == FUNCTION_DECL)
3222 /* A specialization is not necessarily COMDAT. */
3223 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3224 && DECL_DECLARED_INLINE_P (decl));
3225 else if (VAR_P (decl))
3226 DECL_COMDAT (decl) = false;
3228 /* If this is a full specialization, register it so that we can find
3229 it again. Partial specializations will be registered in
3230 process_partial_specialization. */
3231 if (!processing_template_decl)
3233 warn_spec_missing_attributes (gen_tmpl, decl, attrlist);
3235 decl = register_specialization (decl, gen_tmpl, targs,
3236 is_friend, 0);
3240 /* A 'structor should already have clones. */
3241 gcc_assert (decl == error_mark_node
3242 || variable_template_p (tmpl)
3243 || !(DECL_CONSTRUCTOR_P (decl)
3244 || DECL_DESTRUCTOR_P (decl))
3245 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3249 return decl;
3252 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3253 parameters. These are represented in the same format used for
3254 DECL_TEMPLATE_PARMS. */
3257 comp_template_parms (const_tree parms1, const_tree parms2)
3259 const_tree p1;
3260 const_tree p2;
3262 if (parms1 == parms2)
3263 return 1;
3265 for (p1 = parms1, p2 = parms2;
3266 p1 != NULL_TREE && p2 != NULL_TREE;
3267 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3269 tree t1 = TREE_VALUE (p1);
3270 tree t2 = TREE_VALUE (p2);
3271 int i;
3273 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3274 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3276 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3277 return 0;
3279 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3281 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3282 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3284 /* If either of the template parameters are invalid, assume
3285 they match for the sake of error recovery. */
3286 if (error_operand_p (parm1) || error_operand_p (parm2))
3287 return 1;
3289 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3290 return 0;
3292 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3293 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3294 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3295 continue;
3296 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3297 return 0;
3301 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3302 /* One set of parameters has more parameters lists than the
3303 other. */
3304 return 0;
3306 return 1;
3309 /* Determine whether PARM is a parameter pack. */
3311 bool
3312 template_parameter_pack_p (const_tree parm)
3314 /* Determine if we have a non-type template parameter pack. */
3315 if (TREE_CODE (parm) == PARM_DECL)
3316 return (DECL_TEMPLATE_PARM_P (parm)
3317 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3318 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3319 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3321 /* If this is a list of template parameters, we could get a
3322 TYPE_DECL or a TEMPLATE_DECL. */
3323 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3324 parm = TREE_TYPE (parm);
3326 /* Otherwise it must be a type template parameter. */
3327 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3328 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3329 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3332 /* Determine if T is a function parameter pack. */
3334 bool
3335 function_parameter_pack_p (const_tree t)
3337 if (t && TREE_CODE (t) == PARM_DECL)
3338 return DECL_PACK_P (t);
3339 return false;
3342 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3343 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3345 tree
3346 get_function_template_decl (const_tree primary_func_tmpl_inst)
3348 if (! primary_func_tmpl_inst
3349 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3350 || ! primary_template_specialization_p (primary_func_tmpl_inst))
3351 return NULL;
3353 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3356 /* Return true iff the function parameter PARAM_DECL was expanded
3357 from the function parameter pack PACK. */
3359 bool
3360 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3362 if (DECL_ARTIFICIAL (param_decl)
3363 || !function_parameter_pack_p (pack))
3364 return false;
3366 /* The parameter pack and its pack arguments have the same
3367 DECL_PARM_INDEX. */
3368 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3371 /* Determine whether ARGS describes a variadic template args list,
3372 i.e., one that is terminated by a template argument pack. */
3374 static bool
3375 template_args_variadic_p (tree args)
3377 int nargs;
3378 tree last_parm;
3380 if (args == NULL_TREE)
3381 return false;
3383 args = INNERMOST_TEMPLATE_ARGS (args);
3384 nargs = TREE_VEC_LENGTH (args);
3386 if (nargs == 0)
3387 return false;
3389 last_parm = TREE_VEC_ELT (args, nargs - 1);
3391 return ARGUMENT_PACK_P (last_parm);
3394 /* Generate a new name for the parameter pack name NAME (an
3395 IDENTIFIER_NODE) that incorporates its */
3397 static tree
3398 make_ith_pack_parameter_name (tree name, int i)
3400 /* Munge the name to include the parameter index. */
3401 #define NUMBUF_LEN 128
3402 char numbuf[NUMBUF_LEN];
3403 char* newname;
3404 int newname_len;
3406 if (name == NULL_TREE)
3407 return name;
3408 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3409 newname_len = IDENTIFIER_LENGTH (name)
3410 + strlen (numbuf) + 2;
3411 newname = (char*)alloca (newname_len);
3412 snprintf (newname, newname_len,
3413 "%s#%i", IDENTIFIER_POINTER (name), i);
3414 return get_identifier (newname);
3417 /* Return true if T is a primary function, class or alias template
3418 specialization, not including the template pattern. */
3420 bool
3421 primary_template_specialization_p (const_tree t)
3423 if (!t)
3424 return false;
3426 if (TREE_CODE (t) == FUNCTION_DECL || VAR_P (t))
3427 return (DECL_LANG_SPECIFIC (t)
3428 && DECL_USE_TEMPLATE (t)
3429 && DECL_TEMPLATE_INFO (t)
3430 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
3431 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3432 return (CLASSTYPE_TEMPLATE_INFO (t)
3433 && CLASSTYPE_USE_TEMPLATE (t)
3434 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
3435 else if (alias_template_specialization_p (t))
3436 return true;
3437 return false;
3440 /* Return true if PARM is a template template parameter. */
3442 bool
3443 template_template_parameter_p (const_tree parm)
3445 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3448 /* Return true iff PARM is a DECL representing a type template
3449 parameter. */
3451 bool
3452 template_type_parameter_p (const_tree parm)
3454 return (parm
3455 && (TREE_CODE (parm) == TYPE_DECL
3456 || TREE_CODE (parm) == TEMPLATE_DECL)
3457 && DECL_TEMPLATE_PARM_P (parm));
3460 /* Return the template parameters of T if T is a
3461 primary template instantiation, NULL otherwise. */
3463 tree
3464 get_primary_template_innermost_parameters (const_tree t)
3466 tree parms = NULL, template_info = NULL;
3468 if ((template_info = get_template_info (t))
3469 && primary_template_specialization_p (t))
3470 parms = INNERMOST_TEMPLATE_PARMS
3471 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3473 return parms;
3476 /* Return the template parameters of the LEVELth level from the full list
3477 of template parameters PARMS. */
3479 tree
3480 get_template_parms_at_level (tree parms, int level)
3482 tree p;
3483 if (!parms
3484 || TREE_CODE (parms) != TREE_LIST
3485 || level > TMPL_PARMS_DEPTH (parms))
3486 return NULL_TREE;
3488 for (p = parms; p; p = TREE_CHAIN (p))
3489 if (TMPL_PARMS_DEPTH (p) == level)
3490 return p;
3492 return NULL_TREE;
3495 /* Returns the template arguments of T if T is a template instantiation,
3496 NULL otherwise. */
3498 tree
3499 get_template_innermost_arguments (const_tree t)
3501 tree args = NULL, template_info = NULL;
3503 if ((template_info = get_template_info (t))
3504 && TI_ARGS (template_info))
3505 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3507 return args;
3510 /* Return the argument pack elements of T if T is a template argument pack,
3511 NULL otherwise. */
3513 tree
3514 get_template_argument_pack_elems (const_tree t)
3516 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3517 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3518 return NULL;
3520 return ARGUMENT_PACK_ARGS (t);
3523 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3524 ARGUMENT_PACK_SELECT represents. */
3526 static tree
3527 argument_pack_select_arg (tree t)
3529 tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
3530 tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
3532 /* If the selected argument is an expansion E, that most likely means we were
3533 called from gen_elem_of_pack_expansion_instantiation during the
3534 substituting of an argument pack (of which the Ith element is a pack
3535 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3536 In this case, the Ith element resulting from this substituting is going to
3537 be a pack expansion, which pattern is the pattern of E. Let's return the
3538 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3539 resulting pack expansion from it. */
3540 if (PACK_EXPANSION_P (arg))
3542 /* Make sure we aren't throwing away arg info. */
3543 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
3544 arg = PACK_EXPANSION_PATTERN (arg);
3547 return arg;
3551 /* True iff FN is a function representing a built-in variadic parameter
3552 pack. */
3554 bool
3555 builtin_pack_fn_p (tree fn)
3557 if (!fn
3558 || TREE_CODE (fn) != FUNCTION_DECL
3559 || !DECL_IS_BUILTIN (fn))
3560 return false;
3562 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3563 return true;
3565 return false;
3568 /* True iff CALL is a call to a function representing a built-in variadic
3569 parameter pack. */
3571 static bool
3572 builtin_pack_call_p (tree call)
3574 if (TREE_CODE (call) != CALL_EXPR)
3575 return false;
3576 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3579 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3581 static tree
3582 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3583 tree in_decl)
3585 tree ohi = CALL_EXPR_ARG (call, 0);
3586 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3587 false/*fn*/, true/*int_cst*/);
3589 if (value_dependent_expression_p (hi))
3591 if (hi != ohi)
3593 call = copy_node (call);
3594 CALL_EXPR_ARG (call, 0) = hi;
3596 tree ex = make_pack_expansion (call, complain);
3597 tree vec = make_tree_vec (1);
3598 TREE_VEC_ELT (vec, 0) = ex;
3599 return vec;
3601 else
3603 hi = cxx_constant_value (hi);
3604 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3606 /* Calculate the largest value of len that won't make the size of the vec
3607 overflow an int. The compiler will exceed resource limits long before
3608 this, but it seems a decent place to diagnose. */
3609 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3611 if (len < 0 || len > max)
3613 if ((complain & tf_error)
3614 && hi != error_mark_node)
3615 error ("argument to __integer_pack must be between 0 and %d", max);
3616 return error_mark_node;
3619 tree vec = make_tree_vec (len);
3621 for (int i = 0; i < len; ++i)
3622 TREE_VEC_ELT (vec, i) = size_int (i);
3624 return vec;
3628 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3629 CALL. */
3631 static tree
3632 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3633 tree in_decl)
3635 if (!builtin_pack_call_p (call))
3636 return NULL_TREE;
3638 tree fn = CALL_EXPR_FN (call);
3640 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3641 return expand_integer_pack (call, args, complain, in_decl);
3643 return NULL_TREE;
3646 /* Structure used to track the progress of find_parameter_packs_r. */
3647 struct find_parameter_pack_data
3649 /* TREE_LIST that will contain all of the parameter packs found by
3650 the traversal. */
3651 tree* parameter_packs;
3653 /* Set of AST nodes that have been visited by the traversal. */
3654 hash_set<tree> *visited;
3656 /* True iff we're making a type pack expansion. */
3657 bool type_pack_expansion_p;
3660 /* Identifies all of the argument packs that occur in a template
3661 argument and appends them to the TREE_LIST inside DATA, which is a
3662 find_parameter_pack_data structure. This is a subroutine of
3663 make_pack_expansion and uses_parameter_packs. */
3664 static tree
3665 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3667 tree t = *tp;
3668 struct find_parameter_pack_data* ppd =
3669 (struct find_parameter_pack_data*)data;
3670 bool parameter_pack_p = false;
3672 /* Handle type aliases/typedefs. */
3673 if (TYPE_ALIAS_P (t))
3675 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3676 cp_walk_tree (&TI_ARGS (tinfo),
3677 &find_parameter_packs_r,
3678 ppd, ppd->visited);
3679 *walk_subtrees = 0;
3680 return NULL_TREE;
3683 /* Identify whether this is a parameter pack or not. */
3684 switch (TREE_CODE (t))
3686 case TEMPLATE_PARM_INDEX:
3687 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3688 parameter_pack_p = true;
3689 break;
3691 case TEMPLATE_TYPE_PARM:
3692 t = TYPE_MAIN_VARIANT (t);
3693 /* FALLTHRU */
3694 case TEMPLATE_TEMPLATE_PARM:
3695 /* If the placeholder appears in the decl-specifier-seq of a function
3696 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3697 is a pack expansion, the invented template parameter is a template
3698 parameter pack. */
3699 if (ppd->type_pack_expansion_p && is_auto (t))
3700 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3701 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3702 parameter_pack_p = true;
3703 break;
3705 case FIELD_DECL:
3706 case PARM_DECL:
3707 if (DECL_PACK_P (t))
3709 /* We don't want to walk into the type of a PARM_DECL,
3710 because we don't want to see the type parameter pack. */
3711 *walk_subtrees = 0;
3712 parameter_pack_p = true;
3714 break;
3716 case VAR_DECL:
3717 if (DECL_PACK_P (t))
3719 /* We don't want to walk into the type of a variadic capture proxy,
3720 because we don't want to see the type parameter pack. */
3721 *walk_subtrees = 0;
3722 parameter_pack_p = true;
3724 else if (variable_template_specialization_p (t))
3726 cp_walk_tree (&DECL_TI_ARGS (t),
3727 find_parameter_packs_r,
3728 ppd, ppd->visited);
3729 *walk_subtrees = 0;
3731 break;
3733 case CALL_EXPR:
3734 if (builtin_pack_call_p (t))
3735 parameter_pack_p = true;
3736 break;
3738 case BASES:
3739 parameter_pack_p = true;
3740 break;
3741 default:
3742 /* Not a parameter pack. */
3743 break;
3746 if (parameter_pack_p)
3748 /* Add this parameter pack to the list. */
3749 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3752 if (TYPE_P (t))
3753 cp_walk_tree (&TYPE_CONTEXT (t),
3754 &find_parameter_packs_r, ppd, ppd->visited);
3756 /* This switch statement will return immediately if we don't find a
3757 parameter pack. */
3758 switch (TREE_CODE (t))
3760 case TEMPLATE_PARM_INDEX:
3761 return NULL_TREE;
3763 case BOUND_TEMPLATE_TEMPLATE_PARM:
3764 /* Check the template itself. */
3765 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3766 &find_parameter_packs_r, ppd, ppd->visited);
3767 /* Check the template arguments. */
3768 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3769 ppd->visited);
3770 *walk_subtrees = 0;
3771 return NULL_TREE;
3773 case TEMPLATE_TYPE_PARM:
3774 case TEMPLATE_TEMPLATE_PARM:
3775 return NULL_TREE;
3777 case PARM_DECL:
3778 return NULL_TREE;
3780 case DECL_EXPR:
3781 /* Ignore the declaration of a capture proxy for a parameter pack. */
3782 if (is_capture_proxy (DECL_EXPR_DECL (t)))
3783 *walk_subtrees = 0;
3784 return NULL_TREE;
3786 case RECORD_TYPE:
3787 if (TYPE_PTRMEMFUNC_P (t))
3788 return NULL_TREE;
3789 /* Fall through. */
3791 case UNION_TYPE:
3792 case ENUMERAL_TYPE:
3793 if (TYPE_TEMPLATE_INFO (t))
3794 cp_walk_tree (&TYPE_TI_ARGS (t),
3795 &find_parameter_packs_r, ppd, ppd->visited);
3797 *walk_subtrees = 0;
3798 return NULL_TREE;
3800 case TEMPLATE_DECL:
3801 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3802 return NULL_TREE;
3803 gcc_fallthrough();
3805 case CONSTRUCTOR:
3806 cp_walk_tree (&TREE_TYPE (t),
3807 &find_parameter_packs_r, ppd, ppd->visited);
3808 return NULL_TREE;
3810 case TYPENAME_TYPE:
3811 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3812 ppd, ppd->visited);
3813 *walk_subtrees = 0;
3814 return NULL_TREE;
3816 case TYPE_PACK_EXPANSION:
3817 case EXPR_PACK_EXPANSION:
3818 *walk_subtrees = 0;
3819 return NULL_TREE;
3821 case INTEGER_TYPE:
3822 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3823 ppd, ppd->visited);
3824 *walk_subtrees = 0;
3825 return NULL_TREE;
3827 case IDENTIFIER_NODE:
3828 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3829 ppd->visited);
3830 *walk_subtrees = 0;
3831 return NULL_TREE;
3833 case LAMBDA_EXPR:
3835 /* Look at explicit captures. */
3836 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t);
3837 cap; cap = TREE_CHAIN (cap))
3838 cp_walk_tree (&TREE_VALUE (cap), &find_parameter_packs_r, ppd,
3839 ppd->visited);
3840 /* Since we defer implicit capture, look in the body as well. */
3841 tree fn = lambda_function (t);
3842 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
3843 ppd->visited);
3844 *walk_subtrees = 0;
3845 return NULL_TREE;
3848 case DECLTYPE_TYPE:
3850 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3851 type_pack_expansion_p to false so that any placeholders
3852 within the expression don't get marked as parameter packs. */
3853 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
3854 ppd->type_pack_expansion_p = false;
3855 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
3856 ppd, ppd->visited);
3857 ppd->type_pack_expansion_p = type_pack_expansion_p;
3858 *walk_subtrees = 0;
3859 return NULL_TREE;
3862 default:
3863 return NULL_TREE;
3866 return NULL_TREE;
3869 /* Determines if the expression or type T uses any parameter packs. */
3870 bool
3871 uses_parameter_packs (tree t)
3873 tree parameter_packs = NULL_TREE;
3874 struct find_parameter_pack_data ppd;
3875 ppd.parameter_packs = &parameter_packs;
3876 ppd.visited = new hash_set<tree>;
3877 ppd.type_pack_expansion_p = false;
3878 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3879 delete ppd.visited;
3880 return parameter_packs != NULL_TREE;
3883 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3884 representation a base-class initializer into a parameter pack
3885 expansion. If all goes well, the resulting node will be an
3886 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3887 respectively. */
3888 tree
3889 make_pack_expansion (tree arg, tsubst_flags_t complain)
3891 tree result;
3892 tree parameter_packs = NULL_TREE;
3893 bool for_types = false;
3894 struct find_parameter_pack_data ppd;
3896 if (!arg || arg == error_mark_node)
3897 return arg;
3899 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
3901 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3902 class initializer. In this case, the TREE_PURPOSE will be a
3903 _TYPE node (representing the base class expansion we're
3904 initializing) and the TREE_VALUE will be a TREE_LIST
3905 containing the initialization arguments.
3907 The resulting expansion looks somewhat different from most
3908 expansions. Rather than returning just one _EXPANSION, we
3909 return a TREE_LIST whose TREE_PURPOSE is a
3910 TYPE_PACK_EXPANSION containing the bases that will be
3911 initialized. The TREE_VALUE will be identical to the
3912 original TREE_VALUE, which is a list of arguments that will
3913 be passed to each base. We do not introduce any new pack
3914 expansion nodes into the TREE_VALUE (although it is possible
3915 that some already exist), because the TREE_PURPOSE and
3916 TREE_VALUE all need to be expanded together with the same
3917 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3918 resulting TREE_PURPOSE will mention the parameter packs in
3919 both the bases and the arguments to the bases. */
3920 tree purpose;
3921 tree value;
3922 tree parameter_packs = NULL_TREE;
3924 /* Determine which parameter packs will be used by the base
3925 class expansion. */
3926 ppd.visited = new hash_set<tree>;
3927 ppd.parameter_packs = &parameter_packs;
3928 ppd.type_pack_expansion_p = true;
3929 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
3930 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3931 &ppd, ppd.visited);
3933 if (parameter_packs == NULL_TREE)
3935 if (complain & tf_error)
3936 error ("base initializer expansion %qT contains no parameter packs",
3937 arg);
3938 delete ppd.visited;
3939 return error_mark_node;
3942 if (TREE_VALUE (arg) != void_type_node)
3944 /* Collect the sets of parameter packs used in each of the
3945 initialization arguments. */
3946 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3948 /* Determine which parameter packs will be expanded in this
3949 argument. */
3950 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3951 &ppd, ppd.visited);
3955 delete ppd.visited;
3957 /* Create the pack expansion type for the base type. */
3958 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3959 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3960 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3961 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
3963 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3964 they will rarely be compared to anything. */
3965 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3967 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3970 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3971 for_types = true;
3973 /* Build the PACK_EXPANSION_* node. */
3974 result = for_types
3975 ? cxx_make_type (TYPE_PACK_EXPANSION)
3976 : make_node (EXPR_PACK_EXPANSION);
3977 SET_PACK_EXPANSION_PATTERN (result, arg);
3978 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3980 /* Propagate type and const-expression information. */
3981 TREE_TYPE (result) = TREE_TYPE (arg);
3982 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3983 /* Mark this read now, since the expansion might be length 0. */
3984 mark_exp_read (arg);
3986 else
3987 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3988 they will rarely be compared to anything. */
3989 SET_TYPE_STRUCTURAL_EQUALITY (result);
3991 /* Determine which parameter packs will be expanded. */
3992 ppd.parameter_packs = &parameter_packs;
3993 ppd.visited = new hash_set<tree>;
3994 ppd.type_pack_expansion_p = TYPE_P (arg);
3995 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3996 delete ppd.visited;
3998 /* Make sure we found some parameter packs. */
3999 if (parameter_packs == NULL_TREE)
4001 if (complain & tf_error)
4003 if (TYPE_P (arg))
4004 error ("expansion pattern %qT contains no argument packs", arg);
4005 else
4006 error ("expansion pattern %qE contains no argument packs", arg);
4008 return error_mark_node;
4010 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
4012 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
4014 return result;
4017 /* Checks T for any "bare" parameter packs, which have not yet been
4018 expanded, and issues an error if any are found. This operation can
4019 only be done on full expressions or types (e.g., an expression
4020 statement, "if" condition, etc.), because we could have expressions like:
4022 foo(f(g(h(args)))...)
4024 where "args" is a parameter pack. check_for_bare_parameter_packs
4025 should not be called for the subexpressions args, h(args),
4026 g(h(args)), or f(g(h(args))), because we would produce erroneous
4027 error messages.
4029 Returns TRUE and emits an error if there were bare parameter packs,
4030 returns FALSE otherwise. */
4031 bool
4032 check_for_bare_parameter_packs (tree t)
4034 tree parameter_packs = NULL_TREE;
4035 struct find_parameter_pack_data ppd;
4037 if (!processing_template_decl || !t || t == error_mark_node)
4038 return false;
4040 /* A lambda might use a parameter pack from the containing context. */
4041 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
4042 return false;
4044 if (TREE_CODE (t) == TYPE_DECL)
4045 t = TREE_TYPE (t);
4047 ppd.parameter_packs = &parameter_packs;
4048 ppd.visited = new hash_set<tree>;
4049 ppd.type_pack_expansion_p = false;
4050 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4051 delete ppd.visited;
4053 if (parameter_packs)
4055 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
4056 error_at (loc, "parameter packs not expanded with %<...%>:");
4057 while (parameter_packs)
4059 tree pack = TREE_VALUE (parameter_packs);
4060 tree name = NULL_TREE;
4062 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
4063 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
4064 name = TYPE_NAME (pack);
4065 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
4066 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
4067 else if (TREE_CODE (pack) == CALL_EXPR)
4068 name = DECL_NAME (CALL_EXPR_FN (pack));
4069 else
4070 name = DECL_NAME (pack);
4072 if (name)
4073 inform (loc, " %qD", name);
4074 else
4075 inform (loc, " <anonymous>");
4077 parameter_packs = TREE_CHAIN (parameter_packs);
4080 return true;
4083 return false;
4086 /* Expand any parameter packs that occur in the template arguments in
4087 ARGS. */
4088 tree
4089 expand_template_argument_pack (tree args)
4091 if (args == error_mark_node)
4092 return error_mark_node;
4094 tree result_args = NULL_TREE;
4095 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
4096 int num_result_args = -1;
4097 int non_default_args_count = -1;
4099 /* First, determine if we need to expand anything, and the number of
4100 slots we'll need. */
4101 for (in_arg = 0; in_arg < nargs; ++in_arg)
4103 tree arg = TREE_VEC_ELT (args, in_arg);
4104 if (arg == NULL_TREE)
4105 return args;
4106 if (ARGUMENT_PACK_P (arg))
4108 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
4109 if (num_result_args < 0)
4110 num_result_args = in_arg + num_packed;
4111 else
4112 num_result_args += num_packed;
4114 else
4116 if (num_result_args >= 0)
4117 num_result_args++;
4121 /* If no expansion is necessary, we're done. */
4122 if (num_result_args < 0)
4123 return args;
4125 /* Expand arguments. */
4126 result_args = make_tree_vec (num_result_args);
4127 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
4128 non_default_args_count =
4129 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
4130 for (in_arg = 0; in_arg < nargs; ++in_arg)
4132 tree arg = TREE_VEC_ELT (args, in_arg);
4133 if (ARGUMENT_PACK_P (arg))
4135 tree packed = ARGUMENT_PACK_ARGS (arg);
4136 int i, num_packed = TREE_VEC_LENGTH (packed);
4137 for (i = 0; i < num_packed; ++i, ++out_arg)
4138 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
4139 if (non_default_args_count > 0)
4140 non_default_args_count += num_packed - 1;
4142 else
4144 TREE_VEC_ELT (result_args, out_arg) = arg;
4145 ++out_arg;
4148 if (non_default_args_count >= 0)
4149 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
4150 return result_args;
4153 /* Checks if DECL shadows a template parameter.
4155 [temp.local]: A template-parameter shall not be redeclared within its
4156 scope (including nested scopes).
4158 Emits an error and returns TRUE if the DECL shadows a parameter,
4159 returns FALSE otherwise. */
4161 bool
4162 check_template_shadow (tree decl)
4164 tree olddecl;
4166 /* If we're not in a template, we can't possibly shadow a template
4167 parameter. */
4168 if (!current_template_parms)
4169 return true;
4171 /* Figure out what we're shadowing. */
4172 decl = OVL_FIRST (decl);
4173 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4175 /* If there's no previous binding for this name, we're not shadowing
4176 anything, let alone a template parameter. */
4177 if (!olddecl)
4178 return true;
4180 /* If we're not shadowing a template parameter, we're done. Note
4181 that OLDDECL might be an OVERLOAD (or perhaps even an
4182 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4183 node. */
4184 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4185 return true;
4187 /* We check for decl != olddecl to avoid bogus errors for using a
4188 name inside a class. We check TPFI to avoid duplicate errors for
4189 inline member templates. */
4190 if (decl == olddecl
4191 || (DECL_TEMPLATE_PARM_P (decl)
4192 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4193 return true;
4195 /* Don't complain about the injected class name, as we've already
4196 complained about the class itself. */
4197 if (DECL_SELF_REFERENCE_P (decl))
4198 return false;
4200 if (DECL_TEMPLATE_PARM_P (decl))
4201 error ("declaration of template parameter %q+D shadows "
4202 "template parameter", decl);
4203 else
4204 error ("declaration of %q+#D shadows template parameter", decl);
4205 inform (DECL_SOURCE_LOCATION (olddecl),
4206 "template parameter %qD declared here", olddecl);
4207 return false;
4210 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4211 ORIG_LEVEL, DECL, and TYPE. */
4213 static tree
4214 build_template_parm_index (int index,
4215 int level,
4216 int orig_level,
4217 tree decl,
4218 tree type)
4220 tree t = make_node (TEMPLATE_PARM_INDEX);
4221 TEMPLATE_PARM_IDX (t) = index;
4222 TEMPLATE_PARM_LEVEL (t) = level;
4223 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4224 TEMPLATE_PARM_DECL (t) = decl;
4225 TREE_TYPE (t) = type;
4226 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4227 TREE_READONLY (t) = TREE_READONLY (decl);
4229 return t;
4232 /* Find the canonical type parameter for the given template type
4233 parameter. Returns the canonical type parameter, which may be TYPE
4234 if no such parameter existed. */
4236 static tree
4237 canonical_type_parameter (tree type)
4239 tree list;
4240 int idx = TEMPLATE_TYPE_IDX (type);
4241 if (!canonical_template_parms)
4242 vec_alloc (canonical_template_parms, idx + 1);
4244 if (canonical_template_parms->length () <= (unsigned) idx)
4245 vec_safe_grow_cleared (canonical_template_parms, idx + 1);
4247 list = (*canonical_template_parms)[idx];
4248 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4249 list = TREE_CHAIN (list);
4251 if (list)
4252 return TREE_VALUE (list);
4253 else
4255 (*canonical_template_parms)[idx]
4256 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4257 return type;
4261 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4262 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4263 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4264 new one is created. */
4266 static tree
4267 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4268 tsubst_flags_t complain)
4270 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4271 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4272 != TEMPLATE_PARM_LEVEL (index) - levels)
4273 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4275 tree orig_decl = TEMPLATE_PARM_DECL (index);
4276 tree decl, t;
4278 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4279 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
4280 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4281 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4282 DECL_ARTIFICIAL (decl) = 1;
4283 SET_DECL_TEMPLATE_PARM_P (decl);
4285 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4286 TEMPLATE_PARM_LEVEL (index) - levels,
4287 TEMPLATE_PARM_ORIG_LEVEL (index),
4288 decl, type);
4289 TEMPLATE_PARM_DESCENDANTS (index) = t;
4290 TEMPLATE_PARM_PARAMETER_PACK (t)
4291 = TEMPLATE_PARM_PARAMETER_PACK (index);
4293 /* Template template parameters need this. */
4294 if (TREE_CODE (decl) == TEMPLATE_DECL)
4296 DECL_TEMPLATE_RESULT (decl)
4297 = build_decl (DECL_SOURCE_LOCATION (decl),
4298 TYPE_DECL, DECL_NAME (decl), type);
4299 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl)) = true;
4300 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4301 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4305 return TEMPLATE_PARM_DESCENDANTS (index);
4308 /* Process information from new template parameter PARM and append it
4309 to the LIST being built. This new parameter is a non-type
4310 parameter iff IS_NON_TYPE is true. This new parameter is a
4311 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4312 is in PARM_LOC. */
4314 tree
4315 process_template_parm (tree list, location_t parm_loc, tree parm,
4316 bool is_non_type, bool is_parameter_pack)
4318 tree decl = 0;
4319 int idx = 0;
4321 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4322 tree defval = TREE_PURPOSE (parm);
4323 tree constr = TREE_TYPE (parm);
4325 if (list)
4327 tree p = tree_last (list);
4329 if (p && TREE_VALUE (p) != error_mark_node)
4331 p = TREE_VALUE (p);
4332 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4333 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4334 else
4335 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4338 ++idx;
4341 if (is_non_type)
4343 parm = TREE_VALUE (parm);
4345 SET_DECL_TEMPLATE_PARM_P (parm);
4347 if (TREE_TYPE (parm) != error_mark_node)
4349 /* [temp.param]
4351 The top-level cv-qualifiers on the template-parameter are
4352 ignored when determining its type. */
4353 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4354 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4355 TREE_TYPE (parm) = error_mark_node;
4356 else if (uses_parameter_packs (TREE_TYPE (parm))
4357 && !is_parameter_pack
4358 /* If we're in a nested template parameter list, the template
4359 template parameter could be a parameter pack. */
4360 && processing_template_parmlist == 1)
4362 /* This template parameter is not a parameter pack, but it
4363 should be. Complain about "bare" parameter packs. */
4364 check_for_bare_parameter_packs (TREE_TYPE (parm));
4366 /* Recover by calling this a parameter pack. */
4367 is_parameter_pack = true;
4371 /* A template parameter is not modifiable. */
4372 TREE_CONSTANT (parm) = 1;
4373 TREE_READONLY (parm) = 1;
4374 decl = build_decl (parm_loc,
4375 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4376 TREE_CONSTANT (decl) = 1;
4377 TREE_READONLY (decl) = 1;
4378 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4379 = build_template_parm_index (idx, processing_template_decl,
4380 processing_template_decl,
4381 decl, TREE_TYPE (parm));
4383 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4384 = is_parameter_pack;
4386 else
4388 tree t;
4389 parm = TREE_VALUE (TREE_VALUE (parm));
4391 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4393 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4394 /* This is for distinguishing between real templates and template
4395 template parameters */
4396 TREE_TYPE (parm) = t;
4397 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4398 decl = parm;
4400 else
4402 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4403 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4404 decl = build_decl (parm_loc,
4405 TYPE_DECL, parm, t);
4408 TYPE_NAME (t) = decl;
4409 TYPE_STUB_DECL (t) = decl;
4410 parm = decl;
4411 TEMPLATE_TYPE_PARM_INDEX (t)
4412 = build_template_parm_index (idx, processing_template_decl,
4413 processing_template_decl,
4414 decl, TREE_TYPE (parm));
4415 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4416 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4418 DECL_ARTIFICIAL (decl) = 1;
4419 SET_DECL_TEMPLATE_PARM_P (decl);
4421 /* Build requirements for the type/template parameter.
4422 This must be done after SET_DECL_TEMPLATE_PARM_P or
4423 process_template_parm could fail. */
4424 tree reqs = finish_shorthand_constraint (parm, constr);
4426 pushdecl (decl);
4428 /* Build the parameter node linking the parameter declaration,
4429 its default argument (if any), and its constraints (if any). */
4430 parm = build_tree_list (defval, parm);
4431 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4433 return chainon (list, parm);
4436 /* The end of a template parameter list has been reached. Process the
4437 tree list into a parameter vector, converting each parameter into a more
4438 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4439 as PARM_DECLs. */
4441 tree
4442 end_template_parm_list (tree parms)
4444 int nparms;
4445 tree parm, next;
4446 tree saved_parmlist = make_tree_vec (list_length (parms));
4448 /* Pop the dummy parameter level and add the real one. */
4449 current_template_parms = TREE_CHAIN (current_template_parms);
4451 current_template_parms
4452 = tree_cons (size_int (processing_template_decl),
4453 saved_parmlist, current_template_parms);
4455 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4457 next = TREE_CHAIN (parm);
4458 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4459 TREE_CHAIN (parm) = NULL_TREE;
4462 --processing_template_parmlist;
4464 return saved_parmlist;
4467 // Explicitly indicate the end of the template parameter list. We assume
4468 // that the current template parameters have been constructed and/or
4469 // managed explicitly, as when creating new template template parameters
4470 // from a shorthand constraint.
4471 void
4472 end_template_parm_list ()
4474 --processing_template_parmlist;
4477 /* end_template_decl is called after a template declaration is seen. */
4479 void
4480 end_template_decl (void)
4482 reset_specialization ();
4484 if (! processing_template_decl)
4485 return;
4487 /* This matches the pushlevel in begin_template_parm_list. */
4488 finish_scope ();
4490 --processing_template_decl;
4491 current_template_parms = TREE_CHAIN (current_template_parms);
4494 /* Takes a TREE_LIST representing a template parameter and convert it
4495 into an argument suitable to be passed to the type substitution
4496 functions. Note that If the TREE_LIST contains an error_mark
4497 node, the returned argument is error_mark_node. */
4499 tree
4500 template_parm_to_arg (tree t)
4503 if (t == NULL_TREE
4504 || TREE_CODE (t) != TREE_LIST)
4505 return t;
4507 if (error_operand_p (TREE_VALUE (t)))
4508 return error_mark_node;
4510 t = TREE_VALUE (t);
4512 if (TREE_CODE (t) == TYPE_DECL
4513 || TREE_CODE (t) == TEMPLATE_DECL)
4515 t = TREE_TYPE (t);
4517 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4519 /* Turn this argument into a TYPE_ARGUMENT_PACK
4520 with a single element, which expands T. */
4521 tree vec = make_tree_vec (1);
4522 if (CHECKING_P)
4523 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4525 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4527 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4528 SET_ARGUMENT_PACK_ARGS (t, vec);
4531 else
4533 t = DECL_INITIAL (t);
4535 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4537 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4538 with a single element, which expands T. */
4539 tree vec = make_tree_vec (1);
4540 if (CHECKING_P)
4541 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4543 t = convert_from_reference (t);
4544 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4546 t = make_node (NONTYPE_ARGUMENT_PACK);
4547 SET_ARGUMENT_PACK_ARGS (t, vec);
4549 else
4550 t = convert_from_reference (t);
4552 return t;
4555 /* Given a single level of template parameters (a TREE_VEC), return it
4556 as a set of template arguments. */
4558 static tree
4559 template_parms_level_to_args (tree parms)
4561 tree a = copy_node (parms);
4562 TREE_TYPE (a) = NULL_TREE;
4563 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4564 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4566 if (CHECKING_P)
4567 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4569 return a;
4572 /* Given a set of template parameters, return them as a set of template
4573 arguments. The template parameters are represented as a TREE_VEC, in
4574 the form documented in cp-tree.h for template arguments. */
4576 static tree
4577 template_parms_to_args (tree parms)
4579 tree header;
4580 tree args = NULL_TREE;
4581 int length = TMPL_PARMS_DEPTH (parms);
4582 int l = length;
4584 /* If there is only one level of template parameters, we do not
4585 create a TREE_VEC of TREE_VECs. Instead, we return a single
4586 TREE_VEC containing the arguments. */
4587 if (length > 1)
4588 args = make_tree_vec (length);
4590 for (header = parms; header; header = TREE_CHAIN (header))
4592 tree a = template_parms_level_to_args (TREE_VALUE (header));
4594 if (length > 1)
4595 TREE_VEC_ELT (args, --l) = a;
4596 else
4597 args = a;
4600 return args;
4603 /* Within the declaration of a template, return the currently active
4604 template parameters as an argument TREE_VEC. */
4606 static tree
4607 current_template_args (void)
4609 return template_parms_to_args (current_template_parms);
4612 /* Update the declared TYPE by doing any lookups which were thought to be
4613 dependent, but are not now that we know the SCOPE of the declarator. */
4615 tree
4616 maybe_update_decl_type (tree orig_type, tree scope)
4618 tree type = orig_type;
4620 if (type == NULL_TREE)
4621 return type;
4623 if (TREE_CODE (orig_type) == TYPE_DECL)
4624 type = TREE_TYPE (type);
4626 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4627 && dependent_type_p (type)
4628 /* Don't bother building up the args in this case. */
4629 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4631 /* tsubst in the args corresponding to the template parameters,
4632 including auto if present. Most things will be unchanged, but
4633 make_typename_type and tsubst_qualified_id will resolve
4634 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4635 tree args = current_template_args ();
4636 tree auto_node = type_uses_auto (type);
4637 tree pushed;
4638 if (auto_node)
4640 tree auto_vec = make_tree_vec (1);
4641 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4642 args = add_to_template_args (args, auto_vec);
4644 pushed = push_scope (scope);
4645 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4646 if (pushed)
4647 pop_scope (scope);
4650 if (type == error_mark_node)
4651 return orig_type;
4653 if (TREE_CODE (orig_type) == TYPE_DECL)
4655 if (same_type_p (type, TREE_TYPE (orig_type)))
4656 type = orig_type;
4657 else
4658 type = TYPE_NAME (type);
4660 return type;
4663 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4664 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4665 the new template is a member template. */
4667 tree
4668 build_template_decl (tree decl, tree parms, bool member_template_p)
4670 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4671 DECL_TEMPLATE_PARMS (tmpl) = parms;
4672 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4673 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4674 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4676 return tmpl;
4679 struct template_parm_data
4681 /* The level of the template parameters we are currently
4682 processing. */
4683 int level;
4685 /* The index of the specialization argument we are currently
4686 processing. */
4687 int current_arg;
4689 /* An array whose size is the number of template parameters. The
4690 elements are nonzero if the parameter has been used in any one
4691 of the arguments processed so far. */
4692 int* parms;
4694 /* An array whose size is the number of template arguments. The
4695 elements are nonzero if the argument makes use of template
4696 parameters of this level. */
4697 int* arg_uses_template_parms;
4700 /* Subroutine of push_template_decl used to see if each template
4701 parameter in a partial specialization is used in the explicit
4702 argument list. If T is of the LEVEL given in DATA (which is
4703 treated as a template_parm_data*), then DATA->PARMS is marked
4704 appropriately. */
4706 static int
4707 mark_template_parm (tree t, void* data)
4709 int level;
4710 int idx;
4711 struct template_parm_data* tpd = (struct template_parm_data*) data;
4713 template_parm_level_and_index (t, &level, &idx);
4715 if (level == tpd->level)
4717 tpd->parms[idx] = 1;
4718 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4721 /* In C++17 the type of a non-type argument is a deduced context. */
4722 if (cxx_dialect >= cxx17
4723 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4724 for_each_template_parm (TREE_TYPE (t),
4725 &mark_template_parm,
4726 data,
4727 NULL,
4728 /*include_nondeduced_p=*/false);
4730 /* Return zero so that for_each_template_parm will continue the
4731 traversal of the tree; we want to mark *every* template parm. */
4732 return 0;
4735 /* Process the partial specialization DECL. */
4737 static tree
4738 process_partial_specialization (tree decl)
4740 tree type = TREE_TYPE (decl);
4741 tree tinfo = get_template_info (decl);
4742 tree maintmpl = TI_TEMPLATE (tinfo);
4743 tree specargs = TI_ARGS (tinfo);
4744 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4745 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4746 tree inner_parms;
4747 tree inst;
4748 int nargs = TREE_VEC_LENGTH (inner_args);
4749 int ntparms;
4750 int i;
4751 bool did_error_intro = false;
4752 struct template_parm_data tpd;
4753 struct template_parm_data tpd2;
4755 gcc_assert (current_template_parms);
4757 /* A concept cannot be specialized. */
4758 if (flag_concepts && variable_concept_p (maintmpl))
4760 error ("specialization of variable concept %q#D", maintmpl);
4761 return error_mark_node;
4764 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4765 ntparms = TREE_VEC_LENGTH (inner_parms);
4767 /* We check that each of the template parameters given in the
4768 partial specialization is used in the argument list to the
4769 specialization. For example:
4771 template <class T> struct S;
4772 template <class T> struct S<T*>;
4774 The second declaration is OK because `T*' uses the template
4775 parameter T, whereas
4777 template <class T> struct S<int>;
4779 is no good. Even trickier is:
4781 template <class T>
4782 struct S1
4784 template <class U>
4785 struct S2;
4786 template <class U>
4787 struct S2<T>;
4790 The S2<T> declaration is actually invalid; it is a
4791 full-specialization. Of course,
4793 template <class U>
4794 struct S2<T (*)(U)>;
4796 or some such would have been OK. */
4797 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4798 tpd.parms = XALLOCAVEC (int, ntparms);
4799 memset (tpd.parms, 0, sizeof (int) * ntparms);
4801 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4802 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4803 for (i = 0; i < nargs; ++i)
4805 tpd.current_arg = i;
4806 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4807 &mark_template_parm,
4808 &tpd,
4809 NULL,
4810 /*include_nondeduced_p=*/false);
4812 for (i = 0; i < ntparms; ++i)
4813 if (tpd.parms[i] == 0)
4815 /* One of the template parms was not used in a deduced context in the
4816 specialization. */
4817 if (!did_error_intro)
4819 error ("template parameters not deducible in "
4820 "partial specialization:");
4821 did_error_intro = true;
4824 inform (input_location, " %qD",
4825 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4828 if (did_error_intro)
4829 return error_mark_node;
4831 /* [temp.class.spec]
4833 The argument list of the specialization shall not be identical to
4834 the implicit argument list of the primary template. */
4835 tree main_args
4836 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4837 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
4838 && (!flag_concepts
4839 || !strictly_subsumes (current_template_constraints (),
4840 get_constraints (maintmpl))))
4842 if (!flag_concepts)
4843 error ("partial specialization %q+D does not specialize "
4844 "any template arguments; to define the primary template, "
4845 "remove the template argument list", decl);
4846 else
4847 error ("partial specialization %q+D does not specialize any "
4848 "template arguments and is not more constrained than "
4849 "the primary template; to define the primary template, "
4850 "remove the template argument list", decl);
4851 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4854 /* A partial specialization that replaces multiple parameters of the
4855 primary template with a pack expansion is less specialized for those
4856 parameters. */
4857 if (nargs < DECL_NTPARMS (maintmpl))
4859 error ("partial specialization is not more specialized than the "
4860 "primary template because it replaces multiple parameters "
4861 "with a pack expansion");
4862 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4863 /* Avoid crash in process_partial_specialization. */
4864 return decl;
4867 /* If we aren't in a dependent class, we can actually try deduction. */
4868 else if (tpd.level == 1
4869 /* FIXME we should be able to handle a partial specialization of a
4870 partial instantiation, but currently we can't (c++/41727). */
4871 && TMPL_ARGS_DEPTH (specargs) == 1
4872 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
4874 if (permerror (input_location, "partial specialization %qD is not "
4875 "more specialized than", decl))
4876 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
4877 maintmpl);
4880 /* [temp.class.spec]
4882 A partially specialized non-type argument expression shall not
4883 involve template parameters of the partial specialization except
4884 when the argument expression is a simple identifier.
4886 The type of a template parameter corresponding to a specialized
4887 non-type argument shall not be dependent on a parameter of the
4888 specialization.
4890 Also, we verify that pack expansions only occur at the
4891 end of the argument list. */
4892 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4893 tpd2.parms = 0;
4894 for (i = 0; i < nargs; ++i)
4896 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4897 tree arg = TREE_VEC_ELT (inner_args, i);
4898 tree packed_args = NULL_TREE;
4899 int j, len = 1;
4901 if (ARGUMENT_PACK_P (arg))
4903 /* Extract the arguments from the argument pack. We'll be
4904 iterating over these in the following loop. */
4905 packed_args = ARGUMENT_PACK_ARGS (arg);
4906 len = TREE_VEC_LENGTH (packed_args);
4909 for (j = 0; j < len; j++)
4911 if (packed_args)
4912 /* Get the Jth argument in the parameter pack. */
4913 arg = TREE_VEC_ELT (packed_args, j);
4915 if (PACK_EXPANSION_P (arg))
4917 /* Pack expansions must come at the end of the
4918 argument list. */
4919 if ((packed_args && j < len - 1)
4920 || (!packed_args && i < nargs - 1))
4922 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4923 error ("parameter pack argument %qE must be at the "
4924 "end of the template argument list", arg);
4925 else
4926 error ("parameter pack argument %qT must be at the "
4927 "end of the template argument list", arg);
4931 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4932 /* We only care about the pattern. */
4933 arg = PACK_EXPANSION_PATTERN (arg);
4935 if (/* These first two lines are the `non-type' bit. */
4936 !TYPE_P (arg)
4937 && TREE_CODE (arg) != TEMPLATE_DECL
4938 /* This next two lines are the `argument expression is not just a
4939 simple identifier' condition and also the `specialized
4940 non-type argument' bit. */
4941 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4942 && !(REFERENCE_REF_P (arg)
4943 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4945 if ((!packed_args && tpd.arg_uses_template_parms[i])
4946 || (packed_args && uses_template_parms (arg)))
4947 error ("template argument %qE involves template parameter(s)",
4948 arg);
4949 else
4951 /* Look at the corresponding template parameter,
4952 marking which template parameters its type depends
4953 upon. */
4954 tree type = TREE_TYPE (parm);
4956 if (!tpd2.parms)
4958 /* We haven't yet initialized TPD2. Do so now. */
4959 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4960 /* The number of parameters here is the number in the
4961 main template, which, as checked in the assertion
4962 above, is NARGS. */
4963 tpd2.parms = XALLOCAVEC (int, nargs);
4964 tpd2.level =
4965 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4968 /* Mark the template parameters. But this time, we're
4969 looking for the template parameters of the main
4970 template, not in the specialization. */
4971 tpd2.current_arg = i;
4972 tpd2.arg_uses_template_parms[i] = 0;
4973 memset (tpd2.parms, 0, sizeof (int) * nargs);
4974 for_each_template_parm (type,
4975 &mark_template_parm,
4976 &tpd2,
4977 NULL,
4978 /*include_nondeduced_p=*/false);
4980 if (tpd2.arg_uses_template_parms [i])
4982 /* The type depended on some template parameters.
4983 If they are fully specialized in the
4984 specialization, that's OK. */
4985 int j;
4986 int count = 0;
4987 for (j = 0; j < nargs; ++j)
4988 if (tpd2.parms[j] != 0
4989 && tpd.arg_uses_template_parms [j])
4990 ++count;
4991 if (count != 0)
4992 error_n (input_location, count,
4993 "type %qT of template argument %qE depends "
4994 "on a template parameter",
4995 "type %qT of template argument %qE depends "
4996 "on template parameters",
4997 type,
4998 arg);
5005 /* We should only get here once. */
5006 if (TREE_CODE (decl) == TYPE_DECL)
5007 gcc_assert (!COMPLETE_TYPE_P (type));
5009 // Build the template decl.
5010 tree tmpl = build_template_decl (decl, current_template_parms,
5011 DECL_MEMBER_TEMPLATE_P (maintmpl));
5012 TREE_TYPE (tmpl) = type;
5013 DECL_TEMPLATE_RESULT (tmpl) = decl;
5014 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5015 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
5016 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
5018 /* Give template template parms a DECL_CONTEXT of the template
5019 for which they are a parameter. */
5020 for (i = 0; i < ntparms; ++i)
5022 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
5023 if (TREE_CODE (parm) == TEMPLATE_DECL)
5024 DECL_CONTEXT (parm) = tmpl;
5027 if (VAR_P (decl))
5028 /* We didn't register this in check_explicit_specialization so we could
5029 wait until the constraints were set. */
5030 decl = register_specialization (decl, maintmpl, specargs, false, 0);
5031 else
5032 associate_classtype_constraints (type);
5034 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
5035 = tree_cons (specargs, tmpl,
5036 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
5037 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
5039 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
5040 inst = TREE_CHAIN (inst))
5042 tree instance = TREE_VALUE (inst);
5043 if (TYPE_P (instance)
5044 ? (COMPLETE_TYPE_P (instance)
5045 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
5046 : DECL_TEMPLATE_INSTANTIATION (instance))
5048 tree spec = most_specialized_partial_spec (instance, tf_none);
5049 tree inst_decl = (DECL_P (instance)
5050 ? instance : TYPE_NAME (instance));
5051 if (!spec)
5052 /* OK */;
5053 else if (spec == error_mark_node)
5054 permerror (input_location,
5055 "declaration of %qD ambiguates earlier template "
5056 "instantiation for %qD", decl, inst_decl);
5057 else if (TREE_VALUE (spec) == tmpl)
5058 permerror (input_location,
5059 "partial specialization of %qD after instantiation "
5060 "of %qD", decl, inst_decl);
5064 return decl;
5067 /* PARM is a template parameter of some form; return the corresponding
5068 TEMPLATE_PARM_INDEX. */
5070 static tree
5071 get_template_parm_index (tree parm)
5073 if (TREE_CODE (parm) == PARM_DECL
5074 || TREE_CODE (parm) == CONST_DECL)
5075 parm = DECL_INITIAL (parm);
5076 else if (TREE_CODE (parm) == TYPE_DECL
5077 || TREE_CODE (parm) == TEMPLATE_DECL)
5078 parm = TREE_TYPE (parm);
5079 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
5080 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
5081 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
5082 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
5083 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
5084 return parm;
5087 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5088 parameter packs used by the template parameter PARM. */
5090 static void
5091 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
5093 /* A type parm can't refer to another parm. */
5094 if (TREE_CODE (parm) == TYPE_DECL)
5095 return;
5096 else if (TREE_CODE (parm) == PARM_DECL)
5098 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
5099 ppd, ppd->visited);
5100 return;
5103 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
5105 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
5106 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
5107 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
5110 /* PARM is a template parameter pack. Return any parameter packs used in
5111 its type or the type of any of its template parameters. If there are
5112 any such packs, it will be instantiated into a fixed template parameter
5113 list by partial instantiation rather than be fully deduced. */
5115 tree
5116 fixed_parameter_pack_p (tree parm)
5118 /* This can only be true in a member template. */
5119 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
5120 return NULL_TREE;
5121 /* This can only be true for a parameter pack. */
5122 if (!template_parameter_pack_p (parm))
5123 return NULL_TREE;
5124 /* A type parm can't refer to another parm. */
5125 if (TREE_CODE (parm) == TYPE_DECL)
5126 return NULL_TREE;
5128 tree parameter_packs = NULL_TREE;
5129 struct find_parameter_pack_data ppd;
5130 ppd.parameter_packs = &parameter_packs;
5131 ppd.visited = new hash_set<tree>;
5132 ppd.type_pack_expansion_p = false;
5134 fixed_parameter_pack_p_1 (parm, &ppd);
5136 delete ppd.visited;
5137 return parameter_packs;
5140 /* Check that a template declaration's use of default arguments and
5141 parameter packs is not invalid. Here, PARMS are the template
5142 parameters. IS_PRIMARY is true if DECL is the thing declared by
5143 a primary template. IS_PARTIAL is true if DECL is a partial
5144 specialization.
5146 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5147 function template declaration or a friend class template
5148 declaration. In the function case, 1 indicates a declaration, 2
5149 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5150 emitted for extraneous default arguments.
5152 Returns TRUE if there were no errors found, FALSE otherwise. */
5154 bool
5155 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
5156 bool is_partial, int is_friend_decl)
5158 const char *msg;
5159 int last_level_to_check;
5160 tree parm_level;
5161 bool no_errors = true;
5163 /* [temp.param]
5165 A default template-argument shall not be specified in a
5166 function template declaration or a function template definition, nor
5167 in the template-parameter-list of the definition of a member of a
5168 class template. */
5170 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5171 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
5172 /* You can't have a function template declaration in a local
5173 scope, nor you can you define a member of a class template in a
5174 local scope. */
5175 return true;
5177 if ((TREE_CODE (decl) == TYPE_DECL
5178 && TREE_TYPE (decl)
5179 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5180 || (TREE_CODE (decl) == FUNCTION_DECL
5181 && LAMBDA_FUNCTION_P (decl)))
5182 /* A lambda doesn't have an explicit declaration; don't complain
5183 about the parms of the enclosing class. */
5184 return true;
5186 if (current_class_type
5187 && !TYPE_BEING_DEFINED (current_class_type)
5188 && DECL_LANG_SPECIFIC (decl)
5189 && DECL_DECLARES_FUNCTION_P (decl)
5190 /* If this is either a friend defined in the scope of the class
5191 or a member function. */
5192 && (DECL_FUNCTION_MEMBER_P (decl)
5193 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5194 : DECL_FRIEND_CONTEXT (decl)
5195 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5196 : false)
5197 /* And, if it was a member function, it really was defined in
5198 the scope of the class. */
5199 && (!DECL_FUNCTION_MEMBER_P (decl)
5200 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5201 /* We already checked these parameters when the template was
5202 declared, so there's no need to do it again now. This function
5203 was defined in class scope, but we're processing its body now
5204 that the class is complete. */
5205 return true;
5207 /* Core issue 226 (C++0x only): the following only applies to class
5208 templates. */
5209 if (is_primary
5210 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5212 /* [temp.param]
5214 If a template-parameter has a default template-argument, all
5215 subsequent template-parameters shall have a default
5216 template-argument supplied. */
5217 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5219 tree inner_parms = TREE_VALUE (parm_level);
5220 int ntparms = TREE_VEC_LENGTH (inner_parms);
5221 int seen_def_arg_p = 0;
5222 int i;
5224 for (i = 0; i < ntparms; ++i)
5226 tree parm = TREE_VEC_ELT (inner_parms, i);
5228 if (parm == error_mark_node)
5229 continue;
5231 if (TREE_PURPOSE (parm))
5232 seen_def_arg_p = 1;
5233 else if (seen_def_arg_p
5234 && !template_parameter_pack_p (TREE_VALUE (parm)))
5236 error ("no default argument for %qD", TREE_VALUE (parm));
5237 /* For better subsequent error-recovery, we indicate that
5238 there should have been a default argument. */
5239 TREE_PURPOSE (parm) = error_mark_node;
5240 no_errors = false;
5242 else if (!is_partial
5243 && !is_friend_decl
5244 /* Don't complain about an enclosing partial
5245 specialization. */
5246 && parm_level == parms
5247 && TREE_CODE (decl) == TYPE_DECL
5248 && i < ntparms - 1
5249 && template_parameter_pack_p (TREE_VALUE (parm))
5250 /* A fixed parameter pack will be partially
5251 instantiated into a fixed length list. */
5252 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5254 /* A primary class template can only have one
5255 parameter pack, at the end of the template
5256 parameter list. */
5258 error ("parameter pack %q+D must be at the end of the"
5259 " template parameter list", TREE_VALUE (parm));
5261 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5262 = error_mark_node;
5263 no_errors = false;
5269 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5270 || is_partial
5271 || !is_primary
5272 || is_friend_decl)
5273 /* For an ordinary class template, default template arguments are
5274 allowed at the innermost level, e.g.:
5275 template <class T = int>
5276 struct S {};
5277 but, in a partial specialization, they're not allowed even
5278 there, as we have in [temp.class.spec]:
5280 The template parameter list of a specialization shall not
5281 contain default template argument values.
5283 So, for a partial specialization, or for a function template
5284 (in C++98/C++03), we look at all of them. */
5286 else
5287 /* But, for a primary class template that is not a partial
5288 specialization we look at all template parameters except the
5289 innermost ones. */
5290 parms = TREE_CHAIN (parms);
5292 /* Figure out what error message to issue. */
5293 if (is_friend_decl == 2)
5294 msg = G_("default template arguments may not be used in function template "
5295 "friend re-declaration");
5296 else if (is_friend_decl)
5297 msg = G_("default template arguments may not be used in template "
5298 "friend declarations");
5299 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5300 msg = G_("default template arguments may not be used in function templates "
5301 "without -std=c++11 or -std=gnu++11");
5302 else if (is_partial)
5303 msg = G_("default template arguments may not be used in "
5304 "partial specializations");
5305 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5306 msg = G_("default argument for template parameter for class enclosing %qD");
5307 else
5308 /* Per [temp.param]/9, "A default template-argument shall not be
5309 specified in the template-parameter-lists of the definition of
5310 a member of a class template that appears outside of the member's
5311 class.", thus if we aren't handling a member of a class template
5312 there is no need to examine the parameters. */
5313 return true;
5315 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5316 /* If we're inside a class definition, there's no need to
5317 examine the parameters to the class itself. On the one
5318 hand, they will be checked when the class is defined, and,
5319 on the other, default arguments are valid in things like:
5320 template <class T = double>
5321 struct S { template <class U> void f(U); };
5322 Here the default argument for `S' has no bearing on the
5323 declaration of `f'. */
5324 last_level_to_check = template_class_depth (current_class_type) + 1;
5325 else
5326 /* Check everything. */
5327 last_level_to_check = 0;
5329 for (parm_level = parms;
5330 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5331 parm_level = TREE_CHAIN (parm_level))
5333 tree inner_parms = TREE_VALUE (parm_level);
5334 int i;
5335 int ntparms;
5337 ntparms = TREE_VEC_LENGTH (inner_parms);
5338 for (i = 0; i < ntparms; ++i)
5340 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5341 continue;
5343 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5345 if (msg)
5347 no_errors = false;
5348 if (is_friend_decl == 2)
5349 return no_errors;
5351 error (msg, decl);
5352 msg = 0;
5355 /* Clear out the default argument so that we are not
5356 confused later. */
5357 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5361 /* At this point, if we're still interested in issuing messages,
5362 they must apply to classes surrounding the object declared. */
5363 if (msg)
5364 msg = G_("default argument for template parameter for class "
5365 "enclosing %qD");
5368 return no_errors;
5371 /* Worker for push_template_decl_real, called via
5372 for_each_template_parm. DATA is really an int, indicating the
5373 level of the parameters we are interested in. If T is a template
5374 parameter of that level, return nonzero. */
5376 static int
5377 template_parm_this_level_p (tree t, void* data)
5379 int this_level = *(int *)data;
5380 int level;
5382 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5383 level = TEMPLATE_PARM_LEVEL (t);
5384 else
5385 level = TEMPLATE_TYPE_LEVEL (t);
5386 return level == this_level;
5389 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5390 DATA is really an int, indicating the innermost outer level of parameters.
5391 If T is a template parameter of that level or further out, return
5392 nonzero. */
5394 static int
5395 template_parm_outer_level (tree t, void *data)
5397 int this_level = *(int *)data;
5398 int level;
5400 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5401 level = TEMPLATE_PARM_LEVEL (t);
5402 else
5403 level = TEMPLATE_TYPE_LEVEL (t);
5404 return level <= this_level;
5407 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5408 parameters given by current_template_args, or reuses a
5409 previously existing one, if appropriate. Returns the DECL, or an
5410 equivalent one, if it is replaced via a call to duplicate_decls.
5412 If IS_FRIEND is true, DECL is a friend declaration. */
5414 tree
5415 push_template_decl_real (tree decl, bool is_friend)
5417 tree tmpl;
5418 tree args;
5419 tree info;
5420 tree ctx;
5421 bool is_primary;
5422 bool is_partial;
5423 int new_template_p = 0;
5424 /* True if the template is a member template, in the sense of
5425 [temp.mem]. */
5426 bool member_template_p = false;
5428 if (decl == error_mark_node || !current_template_parms)
5429 return error_mark_node;
5431 /* See if this is a partial specialization. */
5432 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5433 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5434 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5435 || (VAR_P (decl)
5436 && DECL_LANG_SPECIFIC (decl)
5437 && DECL_TEMPLATE_SPECIALIZATION (decl)
5438 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5440 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5441 is_friend = true;
5443 if (is_friend)
5444 /* For a friend, we want the context of the friend, not
5445 the type of which it is a friend. */
5446 ctx = CP_DECL_CONTEXT (decl);
5447 else if (CP_DECL_CONTEXT (decl)
5448 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5449 /* In the case of a virtual function, we want the class in which
5450 it is defined. */
5451 ctx = CP_DECL_CONTEXT (decl);
5452 else
5453 /* Otherwise, if we're currently defining some class, the DECL
5454 is assumed to be a member of the class. */
5455 ctx = current_scope ();
5457 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5458 ctx = NULL_TREE;
5460 if (!DECL_CONTEXT (decl))
5461 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5463 /* See if this is a primary template. */
5464 if (is_friend && ctx
5465 && uses_template_parms_level (ctx, processing_template_decl))
5466 /* A friend template that specifies a class context, i.e.
5467 template <typename T> friend void A<T>::f();
5468 is not primary. */
5469 is_primary = false;
5470 else if (TREE_CODE (decl) == TYPE_DECL
5471 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5472 is_primary = false;
5473 else
5474 is_primary = template_parm_scope_p ();
5476 if (is_primary)
5478 warning (OPT_Wtemplates, "template %qD declared", decl);
5480 if (DECL_CLASS_SCOPE_P (decl))
5481 member_template_p = true;
5482 if (TREE_CODE (decl) == TYPE_DECL
5483 && anon_aggrname_p (DECL_NAME (decl)))
5485 error ("template class without a name");
5486 return error_mark_node;
5488 else if (TREE_CODE (decl) == FUNCTION_DECL)
5490 if (member_template_p)
5492 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5493 error ("member template %qD may not have virt-specifiers", decl);
5495 if (DECL_DESTRUCTOR_P (decl))
5497 /* [temp.mem]
5499 A destructor shall not be a member template. */
5500 error ("destructor %qD declared as member template", decl);
5501 return error_mark_node;
5503 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5504 && (!prototype_p (TREE_TYPE (decl))
5505 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5506 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5507 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5508 == void_list_node)))
5510 /* [basic.stc.dynamic.allocation]
5512 An allocation function can be a function
5513 template. ... Template allocation functions shall
5514 have two or more parameters. */
5515 error ("invalid template declaration of %qD", decl);
5516 return error_mark_node;
5519 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5520 && CLASS_TYPE_P (TREE_TYPE (decl)))
5522 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5523 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5524 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5526 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5527 if (TREE_CODE (t) == TYPE_DECL)
5528 t = TREE_TYPE (t);
5529 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5530 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5533 else if (TREE_CODE (decl) == TYPE_DECL
5534 && TYPE_DECL_ALIAS_P (decl))
5535 /* alias-declaration */
5536 gcc_assert (!DECL_ARTIFICIAL (decl));
5537 else if (VAR_P (decl))
5538 /* C++14 variable template. */;
5539 else
5541 error ("template declaration of %q#D", decl);
5542 return error_mark_node;
5546 /* Check to see that the rules regarding the use of default
5547 arguments are not being violated. We check args for a friend
5548 functions when we know whether it's a definition, introducing
5549 declaration or re-declaration. */
5550 if (!is_friend || TREE_CODE (decl) != FUNCTION_DECL)
5551 check_default_tmpl_args (decl, current_template_parms,
5552 is_primary, is_partial, is_friend);
5554 /* Ensure that there are no parameter packs in the type of this
5555 declaration that have not been expanded. */
5556 if (TREE_CODE (decl) == FUNCTION_DECL)
5558 /* Check each of the arguments individually to see if there are
5559 any bare parameter packs. */
5560 tree type = TREE_TYPE (decl);
5561 tree arg = DECL_ARGUMENTS (decl);
5562 tree argtype = TYPE_ARG_TYPES (type);
5564 while (arg && argtype)
5566 if (!DECL_PACK_P (arg)
5567 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5569 /* This is a PARM_DECL that contains unexpanded parameter
5570 packs. We have already complained about this in the
5571 check_for_bare_parameter_packs call, so just replace
5572 these types with ERROR_MARK_NODE. */
5573 TREE_TYPE (arg) = error_mark_node;
5574 TREE_VALUE (argtype) = error_mark_node;
5577 arg = DECL_CHAIN (arg);
5578 argtype = TREE_CHAIN (argtype);
5581 /* Check for bare parameter packs in the return type and the
5582 exception specifiers. */
5583 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5584 /* Errors were already issued, set return type to int
5585 as the frontend doesn't expect error_mark_node as
5586 the return type. */
5587 TREE_TYPE (type) = integer_type_node;
5588 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5589 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5591 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5592 && TYPE_DECL_ALIAS_P (decl))
5593 ? DECL_ORIGINAL_TYPE (decl)
5594 : TREE_TYPE (decl)))
5596 TREE_TYPE (decl) = error_mark_node;
5597 return error_mark_node;
5600 if (is_partial)
5601 return process_partial_specialization (decl);
5603 args = current_template_args ();
5605 if (!ctx
5606 || TREE_CODE (ctx) == FUNCTION_DECL
5607 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5608 || (TREE_CODE (decl) == TYPE_DECL
5609 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5610 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5612 if (DECL_LANG_SPECIFIC (decl)
5613 && DECL_TEMPLATE_INFO (decl)
5614 && DECL_TI_TEMPLATE (decl))
5615 tmpl = DECL_TI_TEMPLATE (decl);
5616 /* If DECL is a TYPE_DECL for a class-template, then there won't
5617 be DECL_LANG_SPECIFIC. The information equivalent to
5618 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5619 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5620 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5621 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5623 /* Since a template declaration already existed for this
5624 class-type, we must be redeclaring it here. Make sure
5625 that the redeclaration is valid. */
5626 redeclare_class_template (TREE_TYPE (decl),
5627 current_template_parms,
5628 current_template_constraints ());
5629 /* We don't need to create a new TEMPLATE_DECL; just use the
5630 one we already had. */
5631 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5633 else
5635 tmpl = build_template_decl (decl, current_template_parms,
5636 member_template_p);
5637 new_template_p = 1;
5639 if (DECL_LANG_SPECIFIC (decl)
5640 && DECL_TEMPLATE_SPECIALIZATION (decl))
5642 /* A specialization of a member template of a template
5643 class. */
5644 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5645 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5646 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5650 else
5652 tree a, t, current, parms;
5653 int i;
5654 tree tinfo = get_template_info (decl);
5656 if (!tinfo)
5658 error ("template definition of non-template %q#D", decl);
5659 return error_mark_node;
5662 tmpl = TI_TEMPLATE (tinfo);
5664 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5665 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5666 && DECL_TEMPLATE_SPECIALIZATION (decl)
5667 && DECL_MEMBER_TEMPLATE_P (tmpl))
5669 tree new_tmpl;
5671 /* The declaration is a specialization of a member
5672 template, declared outside the class. Therefore, the
5673 innermost template arguments will be NULL, so we
5674 replace them with the arguments determined by the
5675 earlier call to check_explicit_specialization. */
5676 args = DECL_TI_ARGS (decl);
5678 new_tmpl
5679 = build_template_decl (decl, current_template_parms,
5680 member_template_p);
5681 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5682 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5683 DECL_TI_TEMPLATE (decl) = new_tmpl;
5684 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5685 DECL_TEMPLATE_INFO (new_tmpl)
5686 = build_template_info (tmpl, args);
5688 register_specialization (new_tmpl,
5689 most_general_template (tmpl),
5690 args,
5691 is_friend, 0);
5692 return decl;
5695 /* Make sure the template headers we got make sense. */
5697 parms = DECL_TEMPLATE_PARMS (tmpl);
5698 i = TMPL_PARMS_DEPTH (parms);
5699 if (TMPL_ARGS_DEPTH (args) != i)
5701 error ("expected %d levels of template parms for %q#D, got %d",
5702 i, decl, TMPL_ARGS_DEPTH (args));
5703 DECL_INTERFACE_KNOWN (decl) = 1;
5704 return error_mark_node;
5706 else
5707 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5709 a = TMPL_ARGS_LEVEL (args, i);
5710 t = INNERMOST_TEMPLATE_PARMS (parms);
5712 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5714 if (current == decl)
5715 error ("got %d template parameters for %q#D",
5716 TREE_VEC_LENGTH (a), decl);
5717 else
5718 error ("got %d template parameters for %q#T",
5719 TREE_VEC_LENGTH (a), current);
5720 error (" but %d required", TREE_VEC_LENGTH (t));
5721 /* Avoid crash in import_export_decl. */
5722 DECL_INTERFACE_KNOWN (decl) = 1;
5723 return error_mark_node;
5726 if (current == decl)
5727 current = ctx;
5728 else if (current == NULL_TREE)
5729 /* Can happen in erroneous input. */
5730 break;
5731 else
5732 current = get_containing_scope (current);
5735 /* Check that the parms are used in the appropriate qualifying scopes
5736 in the declarator. */
5737 if (!comp_template_args
5738 (TI_ARGS (tinfo),
5739 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5741 error ("template arguments to %qD do not match original "
5742 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
5743 if (!uses_template_parms (TI_ARGS (tinfo)))
5744 inform (input_location, "use %<template<>%> for"
5745 " an explicit specialization");
5746 /* Avoid crash in import_export_decl. */
5747 DECL_INTERFACE_KNOWN (decl) = 1;
5748 return error_mark_node;
5752 DECL_TEMPLATE_RESULT (tmpl) = decl;
5753 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5755 /* Push template declarations for global functions and types. Note
5756 that we do not try to push a global template friend declared in a
5757 template class; such a thing may well depend on the template
5758 parameters of the class. */
5759 if (new_template_p && !ctx
5760 && !(is_friend && template_class_depth (current_class_type) > 0))
5762 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5763 if (tmpl == error_mark_node)
5764 return error_mark_node;
5766 /* Hide template friend classes that haven't been declared yet. */
5767 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5769 DECL_ANTICIPATED (tmpl) = 1;
5770 DECL_FRIEND_P (tmpl) = 1;
5774 if (is_primary)
5776 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5778 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5780 /* Give template template parms a DECL_CONTEXT of the template
5781 for which they are a parameter. */
5782 parms = INNERMOST_TEMPLATE_PARMS (parms);
5783 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5785 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5786 if (TREE_CODE (parm) == TEMPLATE_DECL)
5787 DECL_CONTEXT (parm) = tmpl;
5790 if (TREE_CODE (decl) == TYPE_DECL
5791 && TYPE_DECL_ALIAS_P (decl)
5792 && complex_alias_template_p (tmpl))
5793 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5796 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5797 back to its most general template. If TMPL is a specialization,
5798 ARGS may only have the innermost set of arguments. Add the missing
5799 argument levels if necessary. */
5800 if (DECL_TEMPLATE_INFO (tmpl))
5801 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5803 info = build_template_info (tmpl, args);
5805 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5806 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5807 else
5809 if (is_primary)
5810 retrofit_lang_decl (decl);
5811 if (DECL_LANG_SPECIFIC (decl))
5812 DECL_TEMPLATE_INFO (decl) = info;
5815 if (flag_implicit_templates
5816 && !is_friend
5817 && TREE_PUBLIC (decl)
5818 && VAR_OR_FUNCTION_DECL_P (decl))
5819 /* Set DECL_COMDAT on template instantiations; if we force
5820 them to be emitted by explicit instantiation or -frepo,
5821 mark_needed will tell cgraph to do the right thing. */
5822 DECL_COMDAT (decl) = true;
5824 return DECL_TEMPLATE_RESULT (tmpl);
5827 tree
5828 push_template_decl (tree decl)
5830 return push_template_decl_real (decl, false);
5833 /* FN is an inheriting constructor that inherits from the constructor
5834 template INHERITED; turn FN into a constructor template with a matching
5835 template header. */
5837 tree
5838 add_inherited_template_parms (tree fn, tree inherited)
5840 tree inner_parms
5841 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5842 inner_parms = copy_node (inner_parms);
5843 tree parms
5844 = tree_cons (size_int (processing_template_decl + 1),
5845 inner_parms, current_template_parms);
5846 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5847 tree args = template_parms_to_args (parms);
5848 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5849 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5850 DECL_TEMPLATE_RESULT (tmpl) = fn;
5851 DECL_ARTIFICIAL (tmpl) = true;
5852 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5853 return tmpl;
5856 /* Called when a class template TYPE is redeclared with the indicated
5857 template PARMS, e.g.:
5859 template <class T> struct S;
5860 template <class T> struct S {}; */
5862 bool
5863 redeclare_class_template (tree type, tree parms, tree cons)
5865 tree tmpl;
5866 tree tmpl_parms;
5867 int i;
5869 if (!TYPE_TEMPLATE_INFO (type))
5871 error ("%qT is not a template type", type);
5872 return false;
5875 tmpl = TYPE_TI_TEMPLATE (type);
5876 if (!PRIMARY_TEMPLATE_P (tmpl))
5877 /* The type is nested in some template class. Nothing to worry
5878 about here; there are no new template parameters for the nested
5879 type. */
5880 return true;
5882 if (!parms)
5884 error ("template specifiers not specified in declaration of %qD",
5885 tmpl);
5886 return false;
5889 parms = INNERMOST_TEMPLATE_PARMS (parms);
5890 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5892 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5894 error_n (input_location, TREE_VEC_LENGTH (parms),
5895 "redeclared with %d template parameter",
5896 "redeclared with %d template parameters",
5897 TREE_VEC_LENGTH (parms));
5898 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5899 "previous declaration %qD used %d template parameter",
5900 "previous declaration %qD used %d template parameters",
5901 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5902 return false;
5905 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5907 tree tmpl_parm;
5908 tree parm;
5909 tree tmpl_default;
5910 tree parm_default;
5912 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5913 || TREE_VEC_ELT (parms, i) == error_mark_node)
5914 continue;
5916 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5917 if (error_operand_p (tmpl_parm))
5918 return false;
5920 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5921 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5922 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5924 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5925 TEMPLATE_DECL. */
5926 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5927 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5928 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5929 || (TREE_CODE (tmpl_parm) != PARM_DECL
5930 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5931 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5932 || (TREE_CODE (tmpl_parm) == PARM_DECL
5933 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5934 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5936 error ("template parameter %q+#D", tmpl_parm);
5937 error ("redeclared here as %q#D", parm);
5938 return false;
5941 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5943 /* We have in [temp.param]:
5945 A template-parameter may not be given default arguments
5946 by two different declarations in the same scope. */
5947 error_at (input_location, "redefinition of default argument for %q#D", parm);
5948 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5949 "original definition appeared here");
5950 return false;
5953 if (parm_default != NULL_TREE)
5954 /* Update the previous template parameters (which are the ones
5955 that will really count) with the new default value. */
5956 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5957 else if (tmpl_default != NULL_TREE)
5958 /* Update the new parameters, too; they'll be used as the
5959 parameters for any members. */
5960 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5962 /* Give each template template parm in this redeclaration a
5963 DECL_CONTEXT of the template for which they are a parameter. */
5964 if (TREE_CODE (parm) == TEMPLATE_DECL)
5966 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5967 DECL_CONTEXT (parm) = tmpl;
5970 if (TREE_CODE (parm) == TYPE_DECL)
5971 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
5974 // Cannot redeclare a class template with a different set of constraints.
5975 if (!equivalent_constraints (get_constraints (tmpl), cons))
5977 error_at (input_location, "redeclaration %q#D with different "
5978 "constraints", tmpl);
5979 inform (DECL_SOURCE_LOCATION (tmpl),
5980 "original declaration appeared here");
5983 return true;
5986 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5987 to be used when the caller has already checked
5988 (processing_template_decl
5989 && !instantiation_dependent_expression_p (expr)
5990 && potential_constant_expression (expr))
5991 and cleared processing_template_decl. */
5993 tree
5994 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
5996 return tsubst_copy_and_build (expr,
5997 /*args=*/NULL_TREE,
5998 complain,
5999 /*in_decl=*/NULL_TREE,
6000 /*function_p=*/false,
6001 /*integral_constant_expression_p=*/true);
6004 /* Simplify EXPR if it is a non-dependent expression. Returns the
6005 (possibly simplified) expression. */
6007 tree
6008 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
6010 if (expr == NULL_TREE)
6011 return NULL_TREE;
6013 /* If we're in a template, but EXPR isn't value dependent, simplify
6014 it. We're supposed to treat:
6016 template <typename T> void f(T[1 + 1]);
6017 template <typename T> void f(T[2]);
6019 as two declarations of the same function, for example. */
6020 if (processing_template_decl
6021 && is_nondependent_constant_expression (expr))
6023 processing_template_decl_sentinel s;
6024 expr = instantiate_non_dependent_expr_internal (expr, complain);
6026 return expr;
6029 tree
6030 instantiate_non_dependent_expr (tree expr)
6032 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
6035 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6036 an uninstantiated expression. */
6038 tree
6039 instantiate_non_dependent_or_null (tree expr)
6041 if (expr == NULL_TREE)
6042 return NULL_TREE;
6043 if (processing_template_decl)
6045 if (!is_nondependent_constant_expression (expr))
6046 expr = NULL_TREE;
6047 else
6049 processing_template_decl_sentinel s;
6050 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
6053 return expr;
6056 /* True iff T is a specialization of a variable template. */
6058 bool
6059 variable_template_specialization_p (tree t)
6061 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
6062 return false;
6063 tree tmpl = DECL_TI_TEMPLATE (t);
6064 return variable_template_p (tmpl);
6067 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6068 template declaration, or a TYPE_DECL for an alias declaration. */
6070 bool
6071 alias_type_or_template_p (tree t)
6073 if (t == NULL_TREE)
6074 return false;
6075 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
6076 || (TYPE_P (t)
6077 && TYPE_NAME (t)
6078 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
6079 || DECL_ALIAS_TEMPLATE_P (t));
6082 /* Return TRUE iff T is a specialization of an alias template. */
6084 bool
6085 alias_template_specialization_p (const_tree t)
6087 /* It's an alias template specialization if it's an alias and its
6088 TYPE_NAME is a specialization of a primary template. */
6089 if (TYPE_ALIAS_P (t))
6090 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
6091 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo));
6093 return false;
6096 /* An alias template is complex from a SFINAE perspective if a template-id
6097 using that alias can be ill-formed when the expansion is not, as with
6098 the void_t template. We determine this by checking whether the
6099 expansion for the alias template uses all its template parameters. */
6101 struct uses_all_template_parms_data
6103 int level;
6104 bool *seen;
6107 static int
6108 uses_all_template_parms_r (tree t, void *data_)
6110 struct uses_all_template_parms_data &data
6111 = *(struct uses_all_template_parms_data*)data_;
6112 tree idx = get_template_parm_index (t);
6114 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
6115 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
6116 return 0;
6119 static bool
6120 complex_alias_template_p (const_tree tmpl)
6122 struct uses_all_template_parms_data data;
6123 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6124 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6125 data.level = TMPL_PARMS_DEPTH (parms);
6126 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
6127 data.seen = XALLOCAVEC (bool, len);
6128 for (int i = 0; i < len; ++i)
6129 data.seen[i] = false;
6131 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
6132 for (int i = 0; i < len; ++i)
6133 if (!data.seen[i])
6134 return true;
6135 return false;
6138 /* Return TRUE iff T is a specialization of a complex alias template with
6139 dependent template-arguments. */
6141 bool
6142 dependent_alias_template_spec_p (const_tree t)
6144 if (!alias_template_specialization_p (t))
6145 return false;
6147 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
6148 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo)))
6149 return false;
6151 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
6152 if (!any_dependent_template_arguments_p (args))
6153 return false;
6155 return true;
6158 /* Return the number of innermost template parameters in TMPL. */
6160 static int
6161 num_innermost_template_parms (tree tmpl)
6163 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6164 return TREE_VEC_LENGTH (parms);
6167 /* Return either TMPL or another template that it is equivalent to under DR
6168 1286: An alias that just changes the name of a template is equivalent to
6169 the other template. */
6171 static tree
6172 get_underlying_template (tree tmpl)
6174 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6175 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6177 /* Determine if the alias is equivalent to an underlying template. */
6178 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6179 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6180 if (!tinfo)
6181 break;
6183 tree underlying = TI_TEMPLATE (tinfo);
6184 if (!PRIMARY_TEMPLATE_P (underlying)
6185 || (num_innermost_template_parms (tmpl)
6186 != num_innermost_template_parms (underlying)))
6187 break;
6189 tree alias_args = INNERMOST_TEMPLATE_ARGS
6190 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
6191 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6192 break;
6194 /* Alias is equivalent. Strip it and repeat. */
6195 tmpl = underlying;
6198 return tmpl;
6201 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6202 must be a reference-to-function or a pointer-to-function type, as specified
6203 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6204 and check that the resulting function has external linkage. */
6206 static tree
6207 convert_nontype_argument_function (tree type, tree expr,
6208 tsubst_flags_t complain)
6210 tree fns = expr;
6211 tree fn, fn_no_ptr;
6212 linkage_kind linkage;
6214 fn = instantiate_type (type, fns, tf_none);
6215 if (fn == error_mark_node)
6216 return error_mark_node;
6218 if (value_dependent_expression_p (fn))
6219 goto accept;
6221 fn_no_ptr = strip_fnptr_conv (fn);
6222 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6223 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6224 if (BASELINK_P (fn_no_ptr))
6225 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6227 /* [temp.arg.nontype]/1
6229 A template-argument for a non-type, non-template template-parameter
6230 shall be one of:
6231 [...]
6232 -- the address of an object or function with external [C++11: or
6233 internal] linkage. */
6235 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6237 if (complain & tf_error)
6239 error ("%qE is not a valid template argument for type %qT",
6240 expr, type);
6241 if (TYPE_PTR_P (type))
6242 inform (input_location, "it must be the address of a function "
6243 "with external linkage");
6244 else
6245 inform (input_location, "it must be the name of a function with "
6246 "external linkage");
6248 return NULL_TREE;
6251 linkage = decl_linkage (fn_no_ptr);
6252 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6254 if (complain & tf_error)
6256 if (cxx_dialect >= cxx11)
6257 error ("%qE is not a valid template argument for type %qT "
6258 "because %qD has no linkage",
6259 expr, type, fn_no_ptr);
6260 else
6261 error ("%qE is not a valid template argument for type %qT "
6262 "because %qD does not have external linkage",
6263 expr, type, fn_no_ptr);
6265 return NULL_TREE;
6268 accept:
6269 if (TREE_CODE (type) == REFERENCE_TYPE)
6271 if (REFERENCE_REF_P (fn))
6272 fn = TREE_OPERAND (fn, 0);
6273 else
6274 fn = build_address (fn);
6276 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6277 fn = build_nop (type, fn);
6279 return fn;
6282 /* Subroutine of convert_nontype_argument.
6283 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6284 Emit an error otherwise. */
6286 static bool
6287 check_valid_ptrmem_cst_expr (tree type, tree expr,
6288 tsubst_flags_t complain)
6290 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6291 tree orig_expr = expr;
6292 STRIP_NOPS (expr);
6293 if (null_ptr_cst_p (expr))
6294 return true;
6295 if (TREE_CODE (expr) == PTRMEM_CST
6296 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6297 PTRMEM_CST_CLASS (expr)))
6298 return true;
6299 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6300 return true;
6301 if (processing_template_decl
6302 && TREE_CODE (expr) == ADDR_EXPR
6303 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6304 return true;
6305 if (complain & tf_error)
6307 error_at (loc, "%qE is not a valid template argument for type %qT",
6308 orig_expr, type);
6309 if (TREE_CODE (expr) != PTRMEM_CST)
6310 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6311 else
6312 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6314 return false;
6317 /* Returns TRUE iff the address of OP is value-dependent.
6319 14.6.2.4 [temp.dep.temp]:
6320 A non-integral non-type template-argument is dependent if its type is
6321 dependent or it has either of the following forms
6322 qualified-id
6323 & qualified-id
6324 and contains a nested-name-specifier which specifies a class-name that
6325 names a dependent type.
6327 We generalize this to just say that the address of a member of a
6328 dependent class is value-dependent; the above doesn't cover the
6329 address of a static data member named with an unqualified-id. */
6331 static bool
6332 has_value_dependent_address (tree op)
6334 /* We could use get_inner_reference here, but there's no need;
6335 this is only relevant for template non-type arguments, which
6336 can only be expressed as &id-expression. */
6337 if (DECL_P (op))
6339 tree ctx = CP_DECL_CONTEXT (op);
6340 if (TYPE_P (ctx) && dependent_type_p (ctx))
6341 return true;
6344 return false;
6347 /* The next set of functions are used for providing helpful explanatory
6348 diagnostics for failed overload resolution. Their messages should be
6349 indented by two spaces for consistency with the messages in
6350 call.c */
6352 static int
6353 unify_success (bool /*explain_p*/)
6355 return 0;
6358 /* Other failure functions should call this one, to provide a single function
6359 for setting a breakpoint on. */
6361 static int
6362 unify_invalid (bool /*explain_p*/)
6364 return 1;
6367 static int
6368 unify_parameter_deduction_failure (bool explain_p, tree parm)
6370 if (explain_p)
6371 inform (input_location,
6372 " couldn't deduce template parameter %qD", parm);
6373 return unify_invalid (explain_p);
6376 static int
6377 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6379 if (explain_p)
6380 inform (input_location,
6381 " types %qT and %qT have incompatible cv-qualifiers",
6382 parm, arg);
6383 return unify_invalid (explain_p);
6386 static int
6387 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6389 if (explain_p)
6390 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6391 return unify_invalid (explain_p);
6394 static int
6395 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6397 if (explain_p)
6398 inform (input_location,
6399 " template parameter %qD is not a parameter pack, but "
6400 "argument %qD is",
6401 parm, arg);
6402 return unify_invalid (explain_p);
6405 static int
6406 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6408 if (explain_p)
6409 inform (input_location,
6410 " template argument %qE does not match "
6411 "pointer-to-member constant %qE",
6412 arg, parm);
6413 return unify_invalid (explain_p);
6416 static int
6417 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6419 if (explain_p)
6420 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6421 return unify_invalid (explain_p);
6424 static int
6425 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6427 if (explain_p)
6428 inform (input_location,
6429 " inconsistent parameter pack deduction with %qT and %qT",
6430 old_arg, new_arg);
6431 return unify_invalid (explain_p);
6434 static int
6435 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6437 if (explain_p)
6439 if (TYPE_P (parm))
6440 inform (input_location,
6441 " deduced conflicting types for parameter %qT (%qT and %qT)",
6442 parm, first, second);
6443 else
6444 inform (input_location,
6445 " deduced conflicting values for non-type parameter "
6446 "%qE (%qE and %qE)", parm, first, second);
6448 return unify_invalid (explain_p);
6451 static int
6452 unify_vla_arg (bool explain_p, tree arg)
6454 if (explain_p)
6455 inform (input_location,
6456 " variable-sized array type %qT is not "
6457 "a valid template argument",
6458 arg);
6459 return unify_invalid (explain_p);
6462 static int
6463 unify_method_type_error (bool explain_p, tree arg)
6465 if (explain_p)
6466 inform (input_location,
6467 " member function type %qT is not a valid template argument",
6468 arg);
6469 return unify_invalid (explain_p);
6472 static int
6473 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6475 if (explain_p)
6477 if (least_p)
6478 inform_n (input_location, wanted,
6479 " candidate expects at least %d argument, %d provided",
6480 " candidate expects at least %d arguments, %d provided",
6481 wanted, have);
6482 else
6483 inform_n (input_location, wanted,
6484 " candidate expects %d argument, %d provided",
6485 " candidate expects %d arguments, %d provided",
6486 wanted, have);
6488 return unify_invalid (explain_p);
6491 static int
6492 unify_too_many_arguments (bool explain_p, int have, int wanted)
6494 return unify_arity (explain_p, have, wanted);
6497 static int
6498 unify_too_few_arguments (bool explain_p, int have, int wanted,
6499 bool least_p = false)
6501 return unify_arity (explain_p, have, wanted, least_p);
6504 static int
6505 unify_arg_conversion (bool explain_p, tree to_type,
6506 tree from_type, tree arg)
6508 if (explain_p)
6509 inform (EXPR_LOC_OR_LOC (arg, input_location),
6510 " cannot convert %qE (type %qT) to type %qT",
6511 arg, from_type, to_type);
6512 return unify_invalid (explain_p);
6515 static int
6516 unify_no_common_base (bool explain_p, enum template_base_result r,
6517 tree parm, tree arg)
6519 if (explain_p)
6520 switch (r)
6522 case tbr_ambiguous_baseclass:
6523 inform (input_location, " %qT is an ambiguous base class of %qT",
6524 parm, arg);
6525 break;
6526 default:
6527 inform (input_location, " %qT is not derived from %qT", arg, parm);
6528 break;
6530 return unify_invalid (explain_p);
6533 static int
6534 unify_inconsistent_template_template_parameters (bool explain_p)
6536 if (explain_p)
6537 inform (input_location,
6538 " template parameters of a template template argument are "
6539 "inconsistent with other deduced template arguments");
6540 return unify_invalid (explain_p);
6543 static int
6544 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6546 if (explain_p)
6547 inform (input_location,
6548 " can't deduce a template for %qT from non-template type %qT",
6549 parm, arg);
6550 return unify_invalid (explain_p);
6553 static int
6554 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6556 if (explain_p)
6557 inform (input_location,
6558 " template argument %qE does not match %qE", arg, parm);
6559 return unify_invalid (explain_p);
6562 /* Attempt to convert the non-type template parameter EXPR to the
6563 indicated TYPE. If the conversion is successful, return the
6564 converted value. If the conversion is unsuccessful, return
6565 NULL_TREE if we issued an error message, or error_mark_node if we
6566 did not. We issue error messages for out-and-out bad template
6567 parameters, but not simply because the conversion failed, since we
6568 might be just trying to do argument deduction. Both TYPE and EXPR
6569 must be non-dependent.
6571 The conversion follows the special rules described in
6572 [temp.arg.nontype], and it is much more strict than an implicit
6573 conversion.
6575 This function is called twice for each template argument (see
6576 lookup_template_class for a more accurate description of this
6577 problem). This means that we need to handle expressions which
6578 are not valid in a C++ source, but can be created from the
6579 first call (for instance, casts to perform conversions). These
6580 hacks can go away after we fix the double coercion problem. */
6582 static tree
6583 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6585 tree expr_type;
6586 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6587 tree orig_expr = expr;
6589 /* Detect immediately string literals as invalid non-type argument.
6590 This special-case is not needed for correctness (we would easily
6591 catch this later), but only to provide better diagnostic for this
6592 common user mistake. As suggested by DR 100, we do not mention
6593 linkage issues in the diagnostic as this is not the point. */
6594 /* FIXME we're making this OK. */
6595 if (TREE_CODE (expr) == STRING_CST)
6597 if (complain & tf_error)
6598 error ("%qE is not a valid template argument for type %qT "
6599 "because string literals can never be used in this context",
6600 expr, type);
6601 return NULL_TREE;
6604 /* Add the ADDR_EXPR now for the benefit of
6605 value_dependent_expression_p. */
6606 if (TYPE_PTROBV_P (type)
6607 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6609 expr = decay_conversion (expr, complain);
6610 if (expr == error_mark_node)
6611 return error_mark_node;
6614 /* If we are in a template, EXPR may be non-dependent, but still
6615 have a syntactic, rather than semantic, form. For example, EXPR
6616 might be a SCOPE_REF, rather than the VAR_DECL to which the
6617 SCOPE_REF refers. Preserving the qualifying scope is necessary
6618 so that access checking can be performed when the template is
6619 instantiated -- but here we need the resolved form so that we can
6620 convert the argument. */
6621 bool non_dep = false;
6622 if (TYPE_REF_OBJ_P (type)
6623 && has_value_dependent_address (expr))
6624 /* If we want the address and it's value-dependent, don't fold. */;
6625 else if (processing_template_decl
6626 && is_nondependent_constant_expression (expr))
6627 non_dep = true;
6628 if (error_operand_p (expr))
6629 return error_mark_node;
6630 expr_type = TREE_TYPE (expr);
6632 /* If the argument is non-dependent, perform any conversions in
6633 non-dependent context as well. */
6634 processing_template_decl_sentinel s (non_dep);
6635 if (non_dep)
6636 expr = instantiate_non_dependent_expr_internal (expr, complain);
6638 if (value_dependent_expression_p (expr))
6639 expr = canonicalize_expr_argument (expr, complain);
6641 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6642 to a non-type argument of "nullptr". */
6643 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
6644 expr = fold_simple (convert (type, expr));
6646 /* In C++11, integral or enumeration non-type template arguments can be
6647 arbitrary constant expressions. Pointer and pointer to
6648 member arguments can be general constant expressions that evaluate
6649 to a null value, but otherwise still need to be of a specific form. */
6650 if (cxx_dialect >= cxx11)
6652 if (TREE_CODE (expr) == PTRMEM_CST)
6653 /* A PTRMEM_CST is already constant, and a valid template
6654 argument for a parameter of pointer to member type, we just want
6655 to leave it in that form rather than lower it to a
6656 CONSTRUCTOR. */;
6657 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6658 || cxx_dialect >= cxx17)
6660 /* C++17: A template-argument for a non-type template-parameter shall
6661 be a converted constant expression (8.20) of the type of the
6662 template-parameter. */
6663 expr = build_converted_constant_expr (type, expr, complain);
6664 if (expr == error_mark_node)
6665 return error_mark_node;
6666 expr = maybe_constant_value (expr);
6667 expr = convert_from_reference (expr);
6669 else if (TYPE_PTR_OR_PTRMEM_P (type))
6671 tree folded = maybe_constant_value (expr);
6672 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6673 : null_member_pointer_value_p (folded))
6674 expr = folded;
6678 if (TREE_CODE (type) == REFERENCE_TYPE)
6679 expr = mark_lvalue_use (expr);
6680 else
6681 expr = mark_rvalue_use (expr);
6683 /* HACK: Due to double coercion, we can get a
6684 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6685 which is the tree that we built on the first call (see
6686 below when coercing to reference to object or to reference to
6687 function). We just strip everything and get to the arg.
6688 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6689 for examples. */
6690 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6692 tree probe_type, probe = expr;
6693 if (REFERENCE_REF_P (probe))
6694 probe = TREE_OPERAND (probe, 0);
6695 probe_type = TREE_TYPE (probe);
6696 if (TREE_CODE (probe) == NOP_EXPR)
6698 /* ??? Maybe we could use convert_from_reference here, but we
6699 would need to relax its constraints because the NOP_EXPR
6700 could actually change the type to something more cv-qualified,
6701 and this is not folded by convert_from_reference. */
6702 tree addr = TREE_OPERAND (probe, 0);
6703 if (TREE_CODE (probe_type) == REFERENCE_TYPE
6704 && TREE_CODE (addr) == ADDR_EXPR
6705 && TYPE_PTR_P (TREE_TYPE (addr))
6706 && (same_type_ignoring_top_level_qualifiers_p
6707 (TREE_TYPE (probe_type),
6708 TREE_TYPE (TREE_TYPE (addr)))))
6710 expr = TREE_OPERAND (addr, 0);
6711 expr_type = TREE_TYPE (probe_type);
6716 /* [temp.arg.nontype]/5, bullet 1
6718 For a non-type template-parameter of integral or enumeration type,
6719 integral promotions (_conv.prom_) and integral conversions
6720 (_conv.integral_) are applied. */
6721 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6723 if (cxx_dialect < cxx11)
6725 tree t = build_converted_constant_expr (type, expr, complain);
6726 t = maybe_constant_value (t);
6727 if (t != error_mark_node)
6728 expr = t;
6731 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6732 return error_mark_node;
6734 /* Notice that there are constant expressions like '4 % 0' which
6735 do not fold into integer constants. */
6736 if (TREE_CODE (expr) != INTEGER_CST
6737 && !value_dependent_expression_p (expr))
6739 if (complain & tf_error)
6741 int errs = errorcount, warns = warningcount + werrorcount;
6742 if (!require_potential_constant_expression (expr))
6743 expr = error_mark_node;
6744 else
6745 expr = cxx_constant_value (expr);
6746 if (errorcount > errs || warningcount + werrorcount > warns)
6747 inform (loc, "in template argument for type %qT ", type);
6748 if (expr == error_mark_node)
6749 return NULL_TREE;
6750 /* else cxx_constant_value complained but gave us
6751 a real constant, so go ahead. */
6752 if (TREE_CODE (expr) != INTEGER_CST)
6754 /* Some assemble time constant expressions like
6755 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
6756 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
6757 as we can emit them into .rodata initializers of
6758 variables, yet they can't fold into an INTEGER_CST at
6759 compile time. Refuse them here. */
6760 gcc_checking_assert (reduced_constant_expression_p (expr));
6761 error_at (loc, "template argument %qE for type %qT not "
6762 "a constant integer", expr, type);
6763 return NULL_TREE;
6766 else
6767 return NULL_TREE;
6770 /* Avoid typedef problems. */
6771 if (TREE_TYPE (expr) != type)
6772 expr = fold_convert (type, expr);
6774 /* [temp.arg.nontype]/5, bullet 2
6776 For a non-type template-parameter of type pointer to object,
6777 qualification conversions (_conv.qual_) and the array-to-pointer
6778 conversion (_conv.array_) are applied. */
6779 else if (TYPE_PTROBV_P (type))
6781 tree decayed = expr;
6783 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6784 decay_conversion or an explicit cast. If it's a problematic cast,
6785 we'll complain about it below. */
6786 if (TREE_CODE (expr) == NOP_EXPR)
6788 tree probe = expr;
6789 STRIP_NOPS (probe);
6790 if (TREE_CODE (probe) == ADDR_EXPR
6791 && TYPE_PTR_P (TREE_TYPE (probe)))
6793 expr = probe;
6794 expr_type = TREE_TYPE (expr);
6798 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6800 A template-argument for a non-type, non-template template-parameter
6801 shall be one of: [...]
6803 -- the name of a non-type template-parameter;
6804 -- the address of an object or function with external linkage, [...]
6805 expressed as "& id-expression" where the & is optional if the name
6806 refers to a function or array, or if the corresponding
6807 template-parameter is a reference.
6809 Here, we do not care about functions, as they are invalid anyway
6810 for a parameter of type pointer-to-object. */
6812 if (value_dependent_expression_p (expr))
6813 /* Non-type template parameters are OK. */
6815 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6816 /* Null pointer values are OK in C++11. */;
6817 else if (TREE_CODE (expr) != ADDR_EXPR)
6819 if (VAR_P (expr))
6821 if (complain & tf_error)
6822 error ("%qD is not a valid template argument "
6823 "because %qD is a variable, not the address of "
6824 "a variable", orig_expr, expr);
6825 return NULL_TREE;
6827 if (POINTER_TYPE_P (expr_type))
6829 if (complain & tf_error)
6830 error ("%qE is not a valid template argument for %qT "
6831 "because it is not the address of a variable",
6832 orig_expr, type);
6833 return NULL_TREE;
6835 /* Other values, like integer constants, might be valid
6836 non-type arguments of some other type. */
6837 return error_mark_node;
6839 else
6841 tree decl = TREE_OPERAND (expr, 0);
6843 if (!VAR_P (decl))
6845 if (complain & tf_error)
6846 error ("%qE is not a valid template argument of type %qT "
6847 "because %qE is not a variable", orig_expr, type, decl);
6848 return NULL_TREE;
6850 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6852 if (complain & tf_error)
6853 error ("%qE is not a valid template argument of type %qT "
6854 "because %qD does not have external linkage",
6855 orig_expr, type, decl);
6856 return NULL_TREE;
6858 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
6859 && decl_linkage (decl) == lk_none)
6861 if (complain & tf_error)
6862 error ("%qE is not a valid template argument of type %qT "
6863 "because %qD has no linkage", orig_expr, type, decl);
6864 return NULL_TREE;
6866 /* C++17: For a non-type template-parameter of reference or pointer
6867 type, the value of the constant expression shall not refer to (or
6868 for a pointer type, shall not be the address of):
6869 * a subobject (4.5),
6870 * a temporary object (15.2),
6871 * a string literal (5.13.5),
6872 * the result of a typeid expression (8.2.8), or
6873 * a predefined __func__ variable (11.4.1). */
6874 else if (DECL_ARTIFICIAL (decl))
6876 if (complain & tf_error)
6877 error ("the address of %qD is not a valid template argument",
6878 decl);
6879 return NULL_TREE;
6881 else if (!same_type_ignoring_top_level_qualifiers_p
6882 (strip_array_types (TREE_TYPE (type)),
6883 strip_array_types (TREE_TYPE (decl))))
6885 if (complain & tf_error)
6886 error ("the address of the %qT subobject of %qD is not a "
6887 "valid template argument", TREE_TYPE (type), decl);
6888 return NULL_TREE;
6890 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
6892 if (complain & tf_error)
6893 error ("the address of %qD is not a valid template argument "
6894 "because it does not have static storage duration",
6895 decl);
6896 return NULL_TREE;
6900 expr = decayed;
6902 expr = perform_qualification_conversions (type, expr);
6903 if (expr == error_mark_node)
6904 return error_mark_node;
6906 /* [temp.arg.nontype]/5, bullet 3
6908 For a non-type template-parameter of type reference to object, no
6909 conversions apply. The type referred to by the reference may be more
6910 cv-qualified than the (otherwise identical) type of the
6911 template-argument. The template-parameter is bound directly to the
6912 template-argument, which must be an lvalue. */
6913 else if (TYPE_REF_OBJ_P (type))
6915 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6916 expr_type))
6917 return error_mark_node;
6919 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6921 if (complain & tf_error)
6922 error ("%qE is not a valid template argument for type %qT "
6923 "because of conflicts in cv-qualification", expr, type);
6924 return NULL_TREE;
6927 if (!lvalue_p (expr))
6929 if (complain & tf_error)
6930 error ("%qE is not a valid template argument for type %qT "
6931 "because it is not an lvalue", expr, type);
6932 return NULL_TREE;
6935 /* [temp.arg.nontype]/1
6937 A template-argument for a non-type, non-template template-parameter
6938 shall be one of: [...]
6940 -- the address of an object or function with external linkage. */
6941 if (INDIRECT_REF_P (expr)
6942 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6944 expr = TREE_OPERAND (expr, 0);
6945 if (DECL_P (expr))
6947 if (complain & tf_error)
6948 error ("%q#D is not a valid template argument for type %qT "
6949 "because a reference variable does not have a constant "
6950 "address", expr, type);
6951 return NULL_TREE;
6955 if (TYPE_REF_OBJ_P (TREE_TYPE (expr))
6956 && value_dependent_expression_p (expr))
6957 /* OK, dependent reference. We don't want to ask whether a DECL is
6958 itself value-dependent, since what we want here is its address. */;
6959 else
6961 if (!DECL_P (expr))
6963 if (complain & tf_error)
6964 error ("%qE is not a valid template argument for type %qT "
6965 "because it is not an object with linkage",
6966 expr, type);
6967 return NULL_TREE;
6970 /* DR 1155 allows internal linkage in C++11 and up. */
6971 linkage_kind linkage = decl_linkage (expr);
6972 if (linkage < (cxx_dialect >= cxx11 ? lk_internal : lk_external))
6974 if (complain & tf_error)
6975 error ("%qE is not a valid template argument for type %qT "
6976 "because object %qD does not have linkage",
6977 expr, type, expr);
6978 return NULL_TREE;
6981 expr = build_address (expr);
6984 if (!same_type_p (type, TREE_TYPE (expr)))
6985 expr = build_nop (type, expr);
6987 /* [temp.arg.nontype]/5, bullet 4
6989 For a non-type template-parameter of type pointer to function, only
6990 the function-to-pointer conversion (_conv.func_) is applied. If the
6991 template-argument represents a set of overloaded functions (or a
6992 pointer to such), the matching function is selected from the set
6993 (_over.over_). */
6994 else if (TYPE_PTRFN_P (type))
6996 /* If the argument is a template-id, we might not have enough
6997 context information to decay the pointer. */
6998 if (!type_unknown_p (expr_type))
7000 expr = decay_conversion (expr, complain);
7001 if (expr == error_mark_node)
7002 return error_mark_node;
7005 if (cxx_dialect >= cxx11 && integer_zerop (expr))
7006 /* Null pointer values are OK in C++11. */
7007 return perform_qualification_conversions (type, expr);
7009 expr = convert_nontype_argument_function (type, expr, complain);
7010 if (!expr || expr == error_mark_node)
7011 return expr;
7013 /* [temp.arg.nontype]/5, bullet 5
7015 For a non-type template-parameter of type reference to function, no
7016 conversions apply. If the template-argument represents a set of
7017 overloaded functions, the matching function is selected from the set
7018 (_over.over_). */
7019 else if (TYPE_REFFN_P (type))
7021 if (TREE_CODE (expr) == ADDR_EXPR)
7023 if (complain & tf_error)
7025 error ("%qE is not a valid template argument for type %qT "
7026 "because it is a pointer", expr, type);
7027 inform (input_location, "try using %qE instead",
7028 TREE_OPERAND (expr, 0));
7030 return NULL_TREE;
7033 expr = convert_nontype_argument_function (type, expr, complain);
7034 if (!expr || expr == error_mark_node)
7035 return expr;
7037 /* [temp.arg.nontype]/5, bullet 6
7039 For a non-type template-parameter of type pointer to member function,
7040 no conversions apply. If the template-argument represents a set of
7041 overloaded member functions, the matching member function is selected
7042 from the set (_over.over_). */
7043 else if (TYPE_PTRMEMFUNC_P (type))
7045 expr = instantiate_type (type, expr, tf_none);
7046 if (expr == error_mark_node)
7047 return error_mark_node;
7049 /* [temp.arg.nontype] bullet 1 says the pointer to member
7050 expression must be a pointer-to-member constant. */
7051 if (!value_dependent_expression_p (expr)
7052 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7053 return NULL_TREE;
7055 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7056 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7057 if (fnptr_conv_p (type, TREE_TYPE (expr)))
7058 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
7060 /* [temp.arg.nontype]/5, bullet 7
7062 For a non-type template-parameter of type pointer to data member,
7063 qualification conversions (_conv.qual_) are applied. */
7064 else if (TYPE_PTRDATAMEM_P (type))
7066 /* [temp.arg.nontype] bullet 1 says the pointer to member
7067 expression must be a pointer-to-member constant. */
7068 if (!value_dependent_expression_p (expr)
7069 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7070 return NULL_TREE;
7072 expr = perform_qualification_conversions (type, expr);
7073 if (expr == error_mark_node)
7074 return expr;
7076 else if (NULLPTR_TYPE_P (type))
7078 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
7080 if (complain & tf_error)
7081 error ("%qE is not a valid template argument for type %qT "
7082 "because it is of type %qT", expr, type, TREE_TYPE (expr));
7083 return NULL_TREE;
7085 return expr;
7087 /* A template non-type parameter must be one of the above. */
7088 else
7089 gcc_unreachable ();
7091 /* Sanity check: did we actually convert the argument to the
7092 right type? */
7093 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7094 (type, TREE_TYPE (expr)));
7095 return convert_from_reference (expr);
7098 /* Subroutine of coerce_template_template_parms, which returns 1 if
7099 PARM_PARM and ARG_PARM match using the rule for the template
7100 parameters of template template parameters. Both PARM and ARG are
7101 template parameters; the rest of the arguments are the same as for
7102 coerce_template_template_parms.
7104 static int
7105 coerce_template_template_parm (tree parm,
7106 tree arg,
7107 tsubst_flags_t complain,
7108 tree in_decl,
7109 tree outer_args)
7111 if (arg == NULL_TREE || error_operand_p (arg)
7112 || parm == NULL_TREE || error_operand_p (parm))
7113 return 0;
7115 if (TREE_CODE (arg) != TREE_CODE (parm))
7116 return 0;
7118 switch (TREE_CODE (parm))
7120 case TEMPLATE_DECL:
7121 /* We encounter instantiations of templates like
7122 template <template <template <class> class> class TT>
7123 class C; */
7125 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7126 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7128 if (!coerce_template_template_parms
7129 (parmparm, argparm, complain, in_decl, outer_args))
7130 return 0;
7132 /* Fall through. */
7134 case TYPE_DECL:
7135 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
7136 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7137 /* Argument is a parameter pack but parameter is not. */
7138 return 0;
7139 break;
7141 case PARM_DECL:
7142 /* The tsubst call is used to handle cases such as
7144 template <int> class C {};
7145 template <class T, template <T> class TT> class D {};
7146 D<int, C> d;
7148 i.e. the parameter list of TT depends on earlier parameters. */
7149 if (!uses_template_parms (TREE_TYPE (arg)))
7151 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
7152 if (!uses_template_parms (t)
7153 && !same_type_p (t, TREE_TYPE (arg)))
7154 return 0;
7157 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
7158 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7159 /* Argument is a parameter pack but parameter is not. */
7160 return 0;
7162 break;
7164 default:
7165 gcc_unreachable ();
7168 return 1;
7171 /* Coerce template argument list ARGLIST for use with template
7172 template-parameter TEMPL. */
7174 static tree
7175 coerce_template_args_for_ttp (tree templ, tree arglist,
7176 tsubst_flags_t complain)
7178 /* Consider an example where a template template parameter declared as
7180 template <class T, class U = std::allocator<T> > class TT
7182 The template parameter level of T and U are one level larger than
7183 of TT. To proper process the default argument of U, say when an
7184 instantiation `TT<int>' is seen, we need to build the full
7185 arguments containing {int} as the innermost level. Outer levels,
7186 available when not appearing as default template argument, can be
7187 obtained from the arguments of the enclosing template.
7189 Suppose that TT is later substituted with std::vector. The above
7190 instantiation is `TT<int, std::allocator<T> >' with TT at
7191 level 1, and T at level 2, while the template arguments at level 1
7192 becomes {std::vector} and the inner level 2 is {int}. */
7194 tree outer = DECL_CONTEXT (templ);
7195 if (outer)
7197 if (DECL_TEMPLATE_SPECIALIZATION (outer))
7198 /* We want arguments for the partial specialization, not arguments for
7199 the primary template. */
7200 outer = template_parms_to_args (DECL_TEMPLATE_PARMS (outer));
7201 else
7202 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7204 else if (current_template_parms)
7206 /* This is an argument of the current template, so we haven't set
7207 DECL_CONTEXT yet. */
7208 tree relevant_template_parms;
7210 /* Parameter levels that are greater than the level of the given
7211 template template parm are irrelevant. */
7212 relevant_template_parms = current_template_parms;
7213 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7214 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7215 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7217 outer = template_parms_to_args (relevant_template_parms);
7220 if (outer)
7221 arglist = add_to_template_args (outer, arglist);
7223 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7224 return coerce_template_parms (parmlist, arglist, templ,
7225 complain,
7226 /*require_all_args=*/true,
7227 /*use_default_args=*/true);
7230 /* A cache of template template parameters with match-all default
7231 arguments. */
7232 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7233 static void
7234 store_defaulted_ttp (tree v, tree t)
7236 if (!defaulted_ttp_cache)
7237 defaulted_ttp_cache = hash_map<tree,tree>::create_ggc (13);
7238 defaulted_ttp_cache->put (v, t);
7240 static tree
7241 lookup_defaulted_ttp (tree v)
7243 if (defaulted_ttp_cache)
7244 if (tree *p = defaulted_ttp_cache->get (v))
7245 return *p;
7246 return NULL_TREE;
7249 /* T is a bound template template-parameter. Copy its arguments into default
7250 arguments of the template template-parameter's template parameters. */
7252 static tree
7253 add_defaults_to_ttp (tree otmpl)
7255 if (tree c = lookup_defaulted_ttp (otmpl))
7256 return c;
7258 tree ntmpl = copy_node (otmpl);
7260 tree ntype = copy_node (TREE_TYPE (otmpl));
7261 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7262 TYPE_MAIN_VARIANT (ntype) = ntype;
7263 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7264 TYPE_NAME (ntype) = ntmpl;
7265 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7267 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7268 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7269 TEMPLATE_PARM_DECL (idx) = ntmpl;
7270 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7272 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7273 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7274 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7275 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7276 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7278 tree o = TREE_VEC_ELT (vec, i);
7279 if (!template_parameter_pack_p (TREE_VALUE (o)))
7281 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7282 TREE_PURPOSE (n) = any_targ_node;
7286 store_defaulted_ttp (otmpl, ntmpl);
7287 return ntmpl;
7290 /* ARG is a bound potential template template-argument, and PARGS is a list
7291 of arguments for the corresponding template template-parameter. Adjust
7292 PARGS as appropriate for application to ARG's template, and if ARG is a
7293 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7294 arguments to the template template parameter. */
7296 static tree
7297 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7299 ++processing_template_decl;
7300 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7301 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7303 /* When comparing two template template-parameters in partial ordering,
7304 rewrite the one currently being used as an argument to have default
7305 arguments for all parameters. */
7306 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7307 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7308 if (pargs != error_mark_node)
7309 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7310 TYPE_TI_ARGS (arg));
7312 else
7314 tree aparms
7315 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7316 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7317 /*require_all*/true,
7318 /*use_default*/true);
7320 --processing_template_decl;
7321 return pargs;
7324 /* Subroutine of unify for the case when PARM is a
7325 BOUND_TEMPLATE_TEMPLATE_PARM. */
7327 static int
7328 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7329 bool explain_p)
7331 tree parmvec = TYPE_TI_ARGS (parm);
7332 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7334 /* The template template parm might be variadic and the argument
7335 not, so flatten both argument lists. */
7336 parmvec = expand_template_argument_pack (parmvec);
7337 argvec = expand_template_argument_pack (argvec);
7339 if (flag_new_ttp)
7341 /* In keeping with P0522R0, adjust P's template arguments
7342 to apply to A's template; then flatten it again. */
7343 tree nparmvec = parmvec;
7344 nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7345 nparmvec = expand_template_argument_pack (nparmvec);
7347 if (unify (tparms, targs, nparmvec, argvec,
7348 UNIFY_ALLOW_NONE, explain_p))
7349 return 1;
7351 /* If the P0522 adjustment eliminated a pack expansion, deduce
7352 empty packs. */
7353 if (flag_new_ttp
7354 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7355 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7356 DEDUCE_EXACT, /*sub*/true, explain_p))
7357 return 1;
7359 else
7361 /* Deduce arguments T, i from TT<T> or TT<i>.
7362 We check each element of PARMVEC and ARGVEC individually
7363 rather than the whole TREE_VEC since they can have
7364 different number of elements, which is allowed under N2555. */
7366 int len = TREE_VEC_LENGTH (parmvec);
7368 /* Check if the parameters end in a pack, making them
7369 variadic. */
7370 int parm_variadic_p = 0;
7371 if (len > 0
7372 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7373 parm_variadic_p = 1;
7375 for (int i = 0; i < len - parm_variadic_p; ++i)
7376 /* If the template argument list of P contains a pack
7377 expansion that is not the last template argument, the
7378 entire template argument list is a non-deduced
7379 context. */
7380 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7381 return unify_success (explain_p);
7383 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7384 return unify_too_few_arguments (explain_p,
7385 TREE_VEC_LENGTH (argvec), len);
7387 for (int i = 0; i < len - parm_variadic_p; ++i)
7388 if (unify (tparms, targs,
7389 TREE_VEC_ELT (parmvec, i),
7390 TREE_VEC_ELT (argvec, i),
7391 UNIFY_ALLOW_NONE, explain_p))
7392 return 1;
7394 if (parm_variadic_p
7395 && unify_pack_expansion (tparms, targs,
7396 parmvec, argvec,
7397 DEDUCE_EXACT,
7398 /*subr=*/true, explain_p))
7399 return 1;
7402 return 0;
7405 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7406 template template parameters. Both PARM_PARMS and ARG_PARMS are
7407 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7408 or PARM_DECL.
7410 Consider the example:
7411 template <class T> class A;
7412 template<template <class U> class TT> class B;
7414 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7415 the parameters to A, and OUTER_ARGS contains A. */
7417 static int
7418 coerce_template_template_parms (tree parm_parms,
7419 tree arg_parms,
7420 tsubst_flags_t complain,
7421 tree in_decl,
7422 tree outer_args)
7424 int nparms, nargs, i;
7425 tree parm, arg;
7426 int variadic_p = 0;
7428 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7429 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7431 nparms = TREE_VEC_LENGTH (parm_parms);
7432 nargs = TREE_VEC_LENGTH (arg_parms);
7434 if (flag_new_ttp)
7436 /* P0522R0: A template template-parameter P is at least as specialized as
7437 a template template-argument A if, given the following rewrite to two
7438 function templates, the function template corresponding to P is at
7439 least as specialized as the function template corresponding to A
7440 according to the partial ordering rules for function templates
7441 ([temp.func.order]). Given an invented class template X with the
7442 template parameter list of A (including default arguments):
7444 * Each of the two function templates has the same template parameters,
7445 respectively, as P or A.
7447 * Each function template has a single function parameter whose type is
7448 a specialization of X with template arguments corresponding to the
7449 template parameters from the respective function template where, for
7450 each template parameter PP in the template parameter list of the
7451 function template, a corresponding template argument AA is formed. If
7452 PP declares a parameter pack, then AA is the pack expansion
7453 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7455 If the rewrite produces an invalid type, then P is not at least as
7456 specialized as A. */
7458 /* So coerce P's args to apply to A's parms, and then deduce between A's
7459 args and the converted args. If that succeeds, A is at least as
7460 specialized as P, so they match.*/
7461 tree pargs = template_parms_level_to_args (parm_parms);
7462 ++processing_template_decl;
7463 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7464 /*require_all*/true, /*use_default*/true);
7465 --processing_template_decl;
7466 if (pargs != error_mark_node)
7468 tree targs = make_tree_vec (nargs);
7469 tree aargs = template_parms_level_to_args (arg_parms);
7470 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7471 /*explain*/false))
7472 return 1;
7476 /* Determine whether we have a parameter pack at the end of the
7477 template template parameter's template parameter list. */
7478 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7480 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7482 if (error_operand_p (parm))
7483 return 0;
7485 switch (TREE_CODE (parm))
7487 case TEMPLATE_DECL:
7488 case TYPE_DECL:
7489 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7490 variadic_p = 1;
7491 break;
7493 case PARM_DECL:
7494 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7495 variadic_p = 1;
7496 break;
7498 default:
7499 gcc_unreachable ();
7503 if (nargs != nparms
7504 && !(variadic_p && nargs >= nparms - 1))
7505 return 0;
7507 /* Check all of the template parameters except the parameter pack at
7508 the end (if any). */
7509 for (i = 0; i < nparms - variadic_p; ++i)
7511 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7512 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7513 continue;
7515 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7516 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7518 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7519 outer_args))
7520 return 0;
7524 if (variadic_p)
7526 /* Check each of the template parameters in the template
7527 argument against the template parameter pack at the end of
7528 the template template parameter. */
7529 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7530 return 0;
7532 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7534 for (; i < nargs; ++i)
7536 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7537 continue;
7539 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7541 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7542 outer_args))
7543 return 0;
7547 return 1;
7550 /* Verifies that the deduced template arguments (in TARGS) for the
7551 template template parameters (in TPARMS) represent valid bindings,
7552 by comparing the template parameter list of each template argument
7553 to the template parameter list of its corresponding template
7554 template parameter, in accordance with DR150. This
7555 routine can only be called after all template arguments have been
7556 deduced. It will return TRUE if all of the template template
7557 parameter bindings are okay, FALSE otherwise. */
7558 bool
7559 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7561 int i, ntparms = TREE_VEC_LENGTH (tparms);
7562 bool ret = true;
7564 /* We're dealing with template parms in this process. */
7565 ++processing_template_decl;
7567 targs = INNERMOST_TEMPLATE_ARGS (targs);
7569 for (i = 0; i < ntparms; ++i)
7571 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7572 tree targ = TREE_VEC_ELT (targs, i);
7574 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7576 tree packed_args = NULL_TREE;
7577 int idx, len = 1;
7579 if (ARGUMENT_PACK_P (targ))
7581 /* Look inside the argument pack. */
7582 packed_args = ARGUMENT_PACK_ARGS (targ);
7583 len = TREE_VEC_LENGTH (packed_args);
7586 for (idx = 0; idx < len; ++idx)
7588 tree targ_parms = NULL_TREE;
7590 if (packed_args)
7591 /* Extract the next argument from the argument
7592 pack. */
7593 targ = TREE_VEC_ELT (packed_args, idx);
7595 if (PACK_EXPANSION_P (targ))
7596 /* Look at the pattern of the pack expansion. */
7597 targ = PACK_EXPANSION_PATTERN (targ);
7599 /* Extract the template parameters from the template
7600 argument. */
7601 if (TREE_CODE (targ) == TEMPLATE_DECL)
7602 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7603 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7604 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7606 /* Verify that we can coerce the template template
7607 parameters from the template argument to the template
7608 parameter. This requires an exact match. */
7609 if (targ_parms
7610 && !coerce_template_template_parms
7611 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7612 targ_parms,
7613 tf_none,
7614 tparm,
7615 targs))
7617 ret = false;
7618 goto out;
7624 out:
7626 --processing_template_decl;
7627 return ret;
7630 /* Since type attributes aren't mangled, we need to strip them from
7631 template type arguments. */
7633 static tree
7634 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7636 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7637 return arg;
7638 bool removed_attributes = false;
7639 tree canon = strip_typedefs (arg, &removed_attributes);
7640 if (removed_attributes
7641 && (complain & tf_warning))
7642 warning (OPT_Wignored_attributes,
7643 "ignoring attributes on template argument %qT", arg);
7644 return canon;
7647 /* And from inside dependent non-type arguments like sizeof(Type). */
7649 static tree
7650 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7652 if (!arg || arg == error_mark_node)
7653 return arg;
7654 bool removed_attributes = false;
7655 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7656 if (removed_attributes
7657 && (complain & tf_warning))
7658 warning (OPT_Wignored_attributes,
7659 "ignoring attributes in template argument %qE", arg);
7660 return canon;
7663 // A template declaration can be substituted for a constrained
7664 // template template parameter only when the argument is more
7665 // constrained than the parameter.
7666 static bool
7667 is_compatible_template_arg (tree parm, tree arg)
7669 tree parm_cons = get_constraints (parm);
7671 /* For now, allow constrained template template arguments
7672 and unconstrained template template parameters. */
7673 if (parm_cons == NULL_TREE)
7674 return true;
7676 tree arg_cons = get_constraints (arg);
7678 // If the template parameter is constrained, we need to rewrite its
7679 // constraints in terms of the ARG's template parameters. This ensures
7680 // that all of the template parameter types will have the same depth.
7682 // Note that this is only valid when coerce_template_template_parm is
7683 // true for the innermost template parameters of PARM and ARG. In other
7684 // words, because coercion is successful, this conversion will be valid.
7685 if (parm_cons)
7687 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7688 parm_cons = tsubst_constraint_info (parm_cons,
7689 INNERMOST_TEMPLATE_ARGS (args),
7690 tf_none, NULL_TREE);
7691 if (parm_cons == error_mark_node)
7692 return false;
7695 return subsumes (parm_cons, arg_cons);
7698 // Convert a placeholder argument into a binding to the original
7699 // parameter. The original parameter is saved as the TREE_TYPE of
7700 // ARG.
7701 static inline tree
7702 convert_wildcard_argument (tree parm, tree arg)
7704 TREE_TYPE (arg) = parm;
7705 return arg;
7708 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
7709 because one of them is dependent. But we need to represent the
7710 conversion for the benefit of cp_tree_equal. */
7712 static tree
7713 maybe_convert_nontype_argument (tree type, tree arg)
7715 /* Auto parms get no conversion. */
7716 if (type_uses_auto (type))
7717 return arg;
7718 /* We don't need or want to add this conversion now if we're going to use the
7719 argument for deduction. */
7720 if (value_dependent_expression_p (arg))
7721 return arg;
7723 type = cv_unqualified (type);
7724 tree argtype = TREE_TYPE (arg);
7725 if (same_type_p (type, argtype))
7726 return arg;
7728 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
7729 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
7730 return arg;
7733 /* Convert the indicated template ARG as necessary to match the
7734 indicated template PARM. Returns the converted ARG, or
7735 error_mark_node if the conversion was unsuccessful. Error and
7736 warning messages are issued under control of COMPLAIN. This
7737 conversion is for the Ith parameter in the parameter list. ARGS is
7738 the full set of template arguments deduced so far. */
7740 static tree
7741 convert_template_argument (tree parm,
7742 tree arg,
7743 tree args,
7744 tsubst_flags_t complain,
7745 int i,
7746 tree in_decl)
7748 tree orig_arg;
7749 tree val;
7750 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7752 if (parm == error_mark_node)
7753 return error_mark_node;
7755 /* Trivially convert placeholders. */
7756 if (TREE_CODE (arg) == WILDCARD_DECL)
7757 return convert_wildcard_argument (parm, arg);
7759 if (arg == any_targ_node)
7760 return arg;
7762 if (TREE_CODE (arg) == TREE_LIST
7763 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7765 /* The template argument was the name of some
7766 member function. That's usually
7767 invalid, but static members are OK. In any
7768 case, grab the underlying fields/functions
7769 and issue an error later if required. */
7770 orig_arg = TREE_VALUE (arg);
7771 TREE_TYPE (arg) = unknown_type_node;
7774 orig_arg = arg;
7776 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7777 requires_type = (TREE_CODE (parm) == TYPE_DECL
7778 || requires_tmpl_type);
7780 /* When determining whether an argument pack expansion is a template,
7781 look at the pattern. */
7782 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7783 arg = PACK_EXPANSION_PATTERN (arg);
7785 /* Deal with an injected-class-name used as a template template arg. */
7786 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7788 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7789 if (TREE_CODE (t) == TEMPLATE_DECL)
7791 if (cxx_dialect >= cxx11)
7792 /* OK under DR 1004. */;
7793 else if (complain & tf_warning_or_error)
7794 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7795 " used as template template argument", TYPE_NAME (arg));
7796 else if (flag_pedantic_errors)
7797 t = arg;
7799 arg = t;
7803 is_tmpl_type =
7804 ((TREE_CODE (arg) == TEMPLATE_DECL
7805 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7806 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7807 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7808 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7810 if (is_tmpl_type
7811 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7812 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7813 arg = TYPE_STUB_DECL (arg);
7815 is_type = TYPE_P (arg) || is_tmpl_type;
7817 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7818 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7820 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7822 if (complain & tf_error)
7823 error ("invalid use of destructor %qE as a type", orig_arg);
7824 return error_mark_node;
7827 permerror (input_location,
7828 "to refer to a type member of a template parameter, "
7829 "use %<typename %E%>", orig_arg);
7831 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7832 TREE_OPERAND (arg, 1),
7833 typename_type,
7834 complain);
7835 arg = orig_arg;
7836 is_type = 1;
7838 if (is_type != requires_type)
7840 if (in_decl)
7842 if (complain & tf_error)
7844 error ("type/value mismatch at argument %d in template "
7845 "parameter list for %qD",
7846 i + 1, in_decl);
7847 if (is_type)
7848 inform (input_location,
7849 " expected a constant of type %qT, got %qT",
7850 TREE_TYPE (parm),
7851 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7852 else if (requires_tmpl_type)
7853 inform (input_location,
7854 " expected a class template, got %qE", orig_arg);
7855 else
7856 inform (input_location,
7857 " expected a type, got %qE", orig_arg);
7860 return error_mark_node;
7862 if (is_tmpl_type ^ requires_tmpl_type)
7864 if (in_decl && (complain & tf_error))
7866 error ("type/value mismatch at argument %d in template "
7867 "parameter list for %qD",
7868 i + 1, in_decl);
7869 if (is_tmpl_type)
7870 inform (input_location,
7871 " expected a type, got %qT", DECL_NAME (arg));
7872 else
7873 inform (input_location,
7874 " expected a class template, got %qT", orig_arg);
7876 return error_mark_node;
7879 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7880 /* We already did the appropriate conversion when packing args. */
7881 val = orig_arg;
7882 else if (is_type)
7884 if (requires_tmpl_type)
7886 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7887 /* The number of argument required is not known yet.
7888 Just accept it for now. */
7889 val = orig_arg;
7890 else
7892 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7893 tree argparm;
7895 /* Strip alias templates that are equivalent to another
7896 template. */
7897 arg = get_underlying_template (arg);
7898 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7900 if (coerce_template_template_parms (parmparm, argparm,
7901 complain, in_decl,
7902 args))
7904 val = arg;
7906 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7907 TEMPLATE_DECL. */
7908 if (val != error_mark_node)
7910 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
7911 val = TREE_TYPE (val);
7912 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
7913 val = make_pack_expansion (val, complain);
7916 else
7918 if (in_decl && (complain & tf_error))
7920 error ("type/value mismatch at argument %d in "
7921 "template parameter list for %qD",
7922 i + 1, in_decl);
7923 inform (input_location,
7924 " expected a template of type %qD, got %qT",
7925 parm, orig_arg);
7928 val = error_mark_node;
7931 // Check that the constraints are compatible before allowing the
7932 // substitution.
7933 if (val != error_mark_node)
7934 if (!is_compatible_template_arg (parm, arg))
7936 if (in_decl && (complain & tf_error))
7938 error ("constraint mismatch at argument %d in "
7939 "template parameter list for %qD",
7940 i + 1, in_decl);
7941 inform (input_location, " expected %qD but got %qD",
7942 parm, arg);
7944 val = error_mark_node;
7948 else
7949 val = orig_arg;
7950 /* We only form one instance of each template specialization.
7951 Therefore, if we use a non-canonical variant (i.e., a
7952 typedef), any future messages referring to the type will use
7953 the typedef, which is confusing if those future uses do not
7954 themselves also use the typedef. */
7955 if (TYPE_P (val))
7956 val = canonicalize_type_argument (val, complain);
7958 else
7960 tree t = TREE_TYPE (parm);
7962 if (tree a = type_uses_auto (t))
7964 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
7965 if (t == error_mark_node)
7966 return error_mark_node;
7968 else
7969 t = tsubst (t, args, complain, in_decl);
7971 if (invalid_nontype_parm_type_p (t, complain))
7972 return error_mark_node;
7974 if (!type_dependent_expression_p (orig_arg)
7975 && !uses_template_parms (t))
7976 /* We used to call digest_init here. However, digest_init
7977 will report errors, which we don't want when complain
7978 is zero. More importantly, digest_init will try too
7979 hard to convert things: for example, `0' should not be
7980 converted to pointer type at this point according to
7981 the standard. Accepting this is not merely an
7982 extension, since deciding whether or not these
7983 conversions can occur is part of determining which
7984 function template to call, or whether a given explicit
7985 argument specification is valid. */
7986 val = convert_nontype_argument (t, orig_arg, complain);
7987 else
7989 val = canonicalize_expr_argument (orig_arg, complain);
7990 val = maybe_convert_nontype_argument (t, val);
7994 if (val == NULL_TREE)
7995 val = error_mark_node;
7996 else if (val == error_mark_node && (complain & tf_error))
7997 error ("could not convert template argument %qE from %qT to %qT",
7998 orig_arg, TREE_TYPE (orig_arg), t);
8000 if (INDIRECT_REF_P (val))
8002 /* Reject template arguments that are references to built-in
8003 functions with no library fallbacks. */
8004 const_tree inner = TREE_OPERAND (val, 0);
8005 const_tree innertype = TREE_TYPE (inner);
8006 if (innertype
8007 && TREE_CODE (innertype) == REFERENCE_TYPE
8008 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
8009 && TREE_OPERAND_LENGTH (inner) > 0
8010 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
8011 return error_mark_node;
8014 if (TREE_CODE (val) == SCOPE_REF)
8016 /* Strip typedefs from the SCOPE_REF. */
8017 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
8018 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
8019 complain);
8020 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
8021 QUALIFIED_NAME_IS_TEMPLATE (val));
8025 return val;
8028 /* Coerces the remaining template arguments in INNER_ARGS (from
8029 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8030 Returns the coerced argument pack. PARM_IDX is the position of this
8031 parameter in the template parameter list. ARGS is the original
8032 template argument list. */
8033 static tree
8034 coerce_template_parameter_pack (tree parms,
8035 int parm_idx,
8036 tree args,
8037 tree inner_args,
8038 int arg_idx,
8039 tree new_args,
8040 int* lost,
8041 tree in_decl,
8042 tsubst_flags_t complain)
8044 tree parm = TREE_VEC_ELT (parms, parm_idx);
8045 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8046 tree packed_args;
8047 tree argument_pack;
8048 tree packed_parms = NULL_TREE;
8050 if (arg_idx > nargs)
8051 arg_idx = nargs;
8053 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
8055 /* When the template parameter is a non-type template parameter pack
8056 or template template parameter pack whose type or template
8057 parameters use parameter packs, we know exactly how many arguments
8058 we are looking for. Build a vector of the instantiated decls for
8059 these template parameters in PACKED_PARMS. */
8060 /* We can't use make_pack_expansion here because it would interpret a
8061 _DECL as a use rather than a declaration. */
8062 tree decl = TREE_VALUE (parm);
8063 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
8064 SET_PACK_EXPANSION_PATTERN (exp, decl);
8065 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
8066 SET_TYPE_STRUCTURAL_EQUALITY (exp);
8068 TREE_VEC_LENGTH (args)--;
8069 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
8070 TREE_VEC_LENGTH (args)++;
8072 if (packed_parms == error_mark_node)
8073 return error_mark_node;
8075 /* If we're doing a partial instantiation of a member template,
8076 verify that all of the types used for the non-type
8077 template parameter pack are, in fact, valid for non-type
8078 template parameters. */
8079 if (arg_idx < nargs
8080 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
8082 int j, len = TREE_VEC_LENGTH (packed_parms);
8083 for (j = 0; j < len; ++j)
8085 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
8086 if (invalid_nontype_parm_type_p (t, complain))
8087 return error_mark_node;
8089 /* We don't know how many args we have yet, just
8090 use the unconverted ones for now. */
8091 return NULL_TREE;
8094 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
8096 /* Check if we have a placeholder pack, which indicates we're
8097 in the context of a introduction list. In that case we want
8098 to match this pack to the single placeholder. */
8099 else if (arg_idx < nargs
8100 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
8101 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
8103 nargs = arg_idx + 1;
8104 packed_args = make_tree_vec (1);
8106 else
8107 packed_args = make_tree_vec (nargs - arg_idx);
8109 /* Convert the remaining arguments, which will be a part of the
8110 parameter pack "parm". */
8111 int first_pack_arg = arg_idx;
8112 for (; arg_idx < nargs; ++arg_idx)
8114 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
8115 tree actual_parm = TREE_VALUE (parm);
8116 int pack_idx = arg_idx - first_pack_arg;
8118 if (packed_parms)
8120 /* Once we've packed as many args as we have types, stop. */
8121 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
8122 break;
8123 else if (PACK_EXPANSION_P (arg))
8124 /* We don't know how many args we have yet, just
8125 use the unconverted ones for now. */
8126 return NULL_TREE;
8127 else
8128 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
8131 if (arg == error_mark_node)
8133 if (complain & tf_error)
8134 error ("template argument %d is invalid", arg_idx + 1);
8136 else
8137 arg = convert_template_argument (actual_parm,
8138 arg, new_args, complain, parm_idx,
8139 in_decl);
8140 if (arg == error_mark_node)
8141 (*lost)++;
8142 TREE_VEC_ELT (packed_args, pack_idx) = arg;
8145 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
8146 && TREE_VEC_LENGTH (packed_args) > 0)
8148 if (complain & tf_error)
8149 error ("wrong number of template arguments (%d, should be %d)",
8150 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
8151 return error_mark_node;
8154 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
8155 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
8156 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
8157 else
8159 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
8160 TREE_CONSTANT (argument_pack) = 1;
8163 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
8164 if (CHECKING_P)
8165 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
8166 TREE_VEC_LENGTH (packed_args));
8167 return argument_pack;
8170 /* Returns the number of pack expansions in the template argument vector
8171 ARGS. */
8173 static int
8174 pack_expansion_args_count (tree args)
8176 int i;
8177 int count = 0;
8178 if (args)
8179 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8181 tree elt = TREE_VEC_ELT (args, i);
8182 if (elt && PACK_EXPANSION_P (elt))
8183 ++count;
8185 return count;
8188 /* Convert all template arguments to their appropriate types, and
8189 return a vector containing the innermost resulting template
8190 arguments. If any error occurs, return error_mark_node. Error and
8191 warning messages are issued under control of COMPLAIN.
8193 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8194 for arguments not specified in ARGS. Otherwise, if
8195 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8196 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8197 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8198 ARGS. */
8200 static tree
8201 coerce_template_parms (tree parms,
8202 tree args,
8203 tree in_decl,
8204 tsubst_flags_t complain,
8205 bool require_all_args,
8206 bool use_default_args)
8208 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8209 tree orig_inner_args;
8210 tree inner_args;
8211 tree new_args;
8212 tree new_inner_args;
8213 int saved_unevaluated_operand;
8214 int saved_inhibit_evaluation_warnings;
8216 /* When used as a boolean value, indicates whether this is a
8217 variadic template parameter list. Since it's an int, we can also
8218 subtract it from nparms to get the number of non-variadic
8219 parameters. */
8220 int variadic_p = 0;
8221 int variadic_args_p = 0;
8222 int post_variadic_parms = 0;
8224 /* Likewise for parameters with default arguments. */
8225 int default_p = 0;
8227 if (args == error_mark_node)
8228 return error_mark_node;
8230 nparms = TREE_VEC_LENGTH (parms);
8232 /* Determine if there are any parameter packs or default arguments. */
8233 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8235 tree parm = TREE_VEC_ELT (parms, parm_idx);
8236 if (variadic_p)
8237 ++post_variadic_parms;
8238 if (template_parameter_pack_p (TREE_VALUE (parm)))
8239 ++variadic_p;
8240 if (TREE_PURPOSE (parm))
8241 ++default_p;
8244 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8245 /* If there are no parameters that follow a parameter pack, we need to
8246 expand any argument packs so that we can deduce a parameter pack from
8247 some non-packed args followed by an argument pack, as in variadic85.C.
8248 If there are such parameters, we need to leave argument packs intact
8249 so the arguments are assigned properly. This can happen when dealing
8250 with a nested class inside a partial specialization of a class
8251 template, as in variadic92.C, or when deducing a template parameter pack
8252 from a sub-declarator, as in variadic114.C. */
8253 if (!post_variadic_parms)
8254 inner_args = expand_template_argument_pack (inner_args);
8256 /* Count any pack expansion args. */
8257 variadic_args_p = pack_expansion_args_count (inner_args);
8259 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8260 if ((nargs - variadic_args_p > nparms && !variadic_p)
8261 || (nargs < nparms - variadic_p
8262 && require_all_args
8263 && !variadic_args_p
8264 && (!use_default_args
8265 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8266 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8268 if (complain & tf_error)
8270 if (variadic_p || default_p)
8272 nparms -= variadic_p + default_p;
8273 error ("wrong number of template arguments "
8274 "(%d, should be at least %d)", nargs, nparms);
8276 else
8277 error ("wrong number of template arguments "
8278 "(%d, should be %d)", nargs, nparms);
8280 if (in_decl)
8281 inform (DECL_SOURCE_LOCATION (in_decl),
8282 "provided for %qD", in_decl);
8285 return error_mark_node;
8287 /* We can't pass a pack expansion to a non-pack parameter of an alias
8288 template (DR 1430). */
8289 else if (in_decl
8290 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8291 || concept_template_p (in_decl))
8292 && variadic_args_p
8293 && nargs - variadic_args_p < nparms - variadic_p)
8295 if (complain & tf_error)
8297 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8299 tree arg = TREE_VEC_ELT (inner_args, i);
8300 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8302 if (PACK_EXPANSION_P (arg)
8303 && !template_parameter_pack_p (parm))
8305 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8306 error_at (location_of (arg),
8307 "pack expansion argument for non-pack parameter "
8308 "%qD of alias template %qD", parm, in_decl);
8309 else
8310 error_at (location_of (arg),
8311 "pack expansion argument for non-pack parameter "
8312 "%qD of concept %qD", parm, in_decl);
8313 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8314 goto found;
8317 gcc_unreachable ();
8318 found:;
8320 return error_mark_node;
8323 /* We need to evaluate the template arguments, even though this
8324 template-id may be nested within a "sizeof". */
8325 saved_unevaluated_operand = cp_unevaluated_operand;
8326 cp_unevaluated_operand = 0;
8327 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8328 c_inhibit_evaluation_warnings = 0;
8329 new_inner_args = make_tree_vec (nparms);
8330 new_args = add_outermost_template_args (args, new_inner_args);
8331 int pack_adjust = 0;
8332 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8334 tree arg;
8335 tree parm;
8337 /* Get the Ith template parameter. */
8338 parm = TREE_VEC_ELT (parms, parm_idx);
8340 if (parm == error_mark_node)
8342 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8343 continue;
8346 /* Calculate the next argument. */
8347 if (arg_idx < nargs)
8348 arg = TREE_VEC_ELT (inner_args, arg_idx);
8349 else
8350 arg = NULL_TREE;
8352 if (template_parameter_pack_p (TREE_VALUE (parm))
8353 && !(arg && ARGUMENT_PACK_P (arg)))
8355 /* Some arguments will be placed in the
8356 template parameter pack PARM. */
8357 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8358 inner_args, arg_idx,
8359 new_args, &lost,
8360 in_decl, complain);
8362 if (arg == NULL_TREE)
8364 /* We don't know how many args we have yet, just use the
8365 unconverted (and still packed) ones for now. */
8366 new_inner_args = orig_inner_args;
8367 arg_idx = nargs;
8368 break;
8371 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8373 /* Store this argument. */
8374 if (arg == error_mark_node)
8376 lost++;
8377 /* We are done with all of the arguments. */
8378 arg_idx = nargs;
8380 else
8382 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8383 arg_idx += pack_adjust;
8386 continue;
8388 else if (arg)
8390 if (PACK_EXPANSION_P (arg))
8392 /* "If every valid specialization of a variadic template
8393 requires an empty template parameter pack, the template is
8394 ill-formed, no diagnostic required." So check that the
8395 pattern works with this parameter. */
8396 tree pattern = PACK_EXPANSION_PATTERN (arg);
8397 tree conv = convert_template_argument (TREE_VALUE (parm),
8398 pattern, new_args,
8399 complain, parm_idx,
8400 in_decl);
8401 if (conv == error_mark_node)
8403 if (complain & tf_error)
8404 inform (input_location, "so any instantiation with a "
8405 "non-empty parameter pack would be ill-formed");
8406 ++lost;
8408 else if (TYPE_P (conv) && !TYPE_P (pattern))
8409 /* Recover from missing typename. */
8410 TREE_VEC_ELT (inner_args, arg_idx)
8411 = make_pack_expansion (conv, complain);
8413 /* We don't know how many args we have yet, just
8414 use the unconverted ones for now. */
8415 new_inner_args = inner_args;
8416 arg_idx = nargs;
8417 break;
8420 else if (require_all_args)
8422 /* There must be a default arg in this case. */
8423 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8424 complain, in_decl);
8425 /* The position of the first default template argument,
8426 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8427 Record that. */
8428 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8429 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8430 arg_idx - pack_adjust);
8432 else
8433 break;
8435 if (arg == error_mark_node)
8437 if (complain & tf_error)
8438 error ("template argument %d is invalid", arg_idx + 1);
8440 else if (!arg)
8441 /* This only occurs if there was an error in the template
8442 parameter list itself (which we would already have
8443 reported) that we are trying to recover from, e.g., a class
8444 template with a parameter list such as
8445 template<typename..., typename>. */
8446 ++lost;
8447 else
8448 arg = convert_template_argument (TREE_VALUE (parm),
8449 arg, new_args, complain,
8450 parm_idx, in_decl);
8452 if (arg == error_mark_node)
8453 lost++;
8454 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8456 cp_unevaluated_operand = saved_unevaluated_operand;
8457 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8459 if (variadic_p && arg_idx < nargs)
8461 if (complain & tf_error)
8463 error ("wrong number of template arguments "
8464 "(%d, should be %d)", nargs, arg_idx);
8465 if (in_decl)
8466 error ("provided for %q+D", in_decl);
8468 return error_mark_node;
8471 if (lost)
8472 return error_mark_node;
8474 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8475 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8476 TREE_VEC_LENGTH (new_inner_args));
8478 return new_inner_args;
8481 /* Convert all template arguments to their appropriate types, and
8482 return a vector containing the innermost resulting template
8483 arguments. If any error occurs, return error_mark_node. Error and
8484 warning messages are not issued.
8486 Note that no function argument deduction is performed, and default
8487 arguments are used to fill in unspecified arguments. */
8488 tree
8489 coerce_template_parms (tree parms, tree args, tree in_decl)
8491 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8494 /* Convert all template arguments to their appropriate type, and
8495 instantiate default arguments as needed. This returns a vector
8496 containing the innermost resulting template arguments, or
8497 error_mark_node if unsuccessful. */
8498 tree
8499 coerce_template_parms (tree parms, tree args, tree in_decl,
8500 tsubst_flags_t complain)
8502 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8505 /* Like coerce_template_parms. If PARMS represents all template
8506 parameters levels, this function returns a vector of vectors
8507 representing all the resulting argument levels. Note that in this
8508 case, only the innermost arguments are coerced because the
8509 outermost ones are supposed to have been coerced already.
8511 Otherwise, if PARMS represents only (the innermost) vector of
8512 parameters, this function returns a vector containing just the
8513 innermost resulting arguments. */
8515 static tree
8516 coerce_innermost_template_parms (tree parms,
8517 tree args,
8518 tree in_decl,
8519 tsubst_flags_t complain,
8520 bool require_all_args,
8521 bool use_default_args)
8523 int parms_depth = TMPL_PARMS_DEPTH (parms);
8524 int args_depth = TMPL_ARGS_DEPTH (args);
8525 tree coerced_args;
8527 if (parms_depth > 1)
8529 coerced_args = make_tree_vec (parms_depth);
8530 tree level;
8531 int cur_depth;
8533 for (level = parms, cur_depth = parms_depth;
8534 parms_depth > 0 && level != NULL_TREE;
8535 level = TREE_CHAIN (level), --cur_depth)
8537 tree l;
8538 if (cur_depth == args_depth)
8539 l = coerce_template_parms (TREE_VALUE (level),
8540 args, in_decl, complain,
8541 require_all_args,
8542 use_default_args);
8543 else
8544 l = TMPL_ARGS_LEVEL (args, cur_depth);
8546 if (l == error_mark_node)
8547 return error_mark_node;
8549 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8552 else
8553 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8554 args, in_decl, complain,
8555 require_all_args,
8556 use_default_args);
8557 return coerced_args;
8560 /* Returns 1 if template args OT and NT are equivalent. */
8563 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
8565 if (nt == ot)
8566 return 1;
8567 if (nt == NULL_TREE || ot == NULL_TREE)
8568 return false;
8569 if (nt == any_targ_node || ot == any_targ_node)
8570 return true;
8572 if (TREE_CODE (nt) == TREE_VEC)
8573 /* For member templates */
8574 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8575 else if (PACK_EXPANSION_P (ot))
8576 return (PACK_EXPANSION_P (nt)
8577 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8578 PACK_EXPANSION_PATTERN (nt))
8579 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8580 PACK_EXPANSION_EXTRA_ARGS (nt)));
8581 else if (ARGUMENT_PACK_P (ot))
8583 int i, len;
8584 tree opack, npack;
8586 if (!ARGUMENT_PACK_P (nt))
8587 return 0;
8589 opack = ARGUMENT_PACK_ARGS (ot);
8590 npack = ARGUMENT_PACK_ARGS (nt);
8591 len = TREE_VEC_LENGTH (opack);
8592 if (TREE_VEC_LENGTH (npack) != len)
8593 return 0;
8594 for (i = 0; i < len; ++i)
8595 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8596 TREE_VEC_ELT (npack, i)))
8597 return 0;
8598 return 1;
8600 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8601 gcc_unreachable ();
8602 else if (TYPE_P (nt))
8604 if (!TYPE_P (ot))
8605 return false;
8606 /* Don't treat an alias template specialization with dependent
8607 arguments as equivalent to its underlying type when used as a
8608 template argument; we need them to be distinct so that we
8609 substitute into the specialization arguments at instantiation
8610 time. And aliases can't be equivalent without being ==, so
8611 we don't need to look any deeper.
8613 During partial ordering, however, we need to treat them normally so
8614 that we can order uses of the same alias with different
8615 cv-qualification (79960). */
8616 if (!partial_order
8617 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
8618 return false;
8619 else
8620 return same_type_p (ot, nt);
8622 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8623 return 0;
8624 else
8626 /* Try to treat a template non-type argument that has been converted
8627 to the parameter type as equivalent to one that hasn't yet. */
8628 for (enum tree_code code1 = TREE_CODE (ot);
8629 CONVERT_EXPR_CODE_P (code1)
8630 || code1 == NON_LVALUE_EXPR;
8631 code1 = TREE_CODE (ot))
8632 ot = TREE_OPERAND (ot, 0);
8633 for (enum tree_code code2 = TREE_CODE (nt);
8634 CONVERT_EXPR_CODE_P (code2)
8635 || code2 == NON_LVALUE_EXPR;
8636 code2 = TREE_CODE (nt))
8637 nt = TREE_OPERAND (nt, 0);
8639 return cp_tree_equal (ot, nt);
8643 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8644 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8645 NEWARG_PTR with the offending arguments if they are non-NULL. */
8648 comp_template_args (tree oldargs, tree newargs,
8649 tree *oldarg_ptr, tree *newarg_ptr,
8650 bool partial_order)
8652 int i;
8654 if (oldargs == newargs)
8655 return 1;
8657 if (!oldargs || !newargs)
8658 return 0;
8660 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
8661 return 0;
8663 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
8665 tree nt = TREE_VEC_ELT (newargs, i);
8666 tree ot = TREE_VEC_ELT (oldargs, i);
8668 if (! template_args_equal (ot, nt, partial_order))
8670 if (oldarg_ptr != NULL)
8671 *oldarg_ptr = ot;
8672 if (newarg_ptr != NULL)
8673 *newarg_ptr = nt;
8674 return 0;
8677 return 1;
8680 inline bool
8681 comp_template_args_porder (tree oargs, tree nargs)
8683 return comp_template_args (oargs, nargs, NULL, NULL, true);
8686 static void
8687 add_pending_template (tree d)
8689 tree ti = (TYPE_P (d)
8690 ? CLASSTYPE_TEMPLATE_INFO (d)
8691 : DECL_TEMPLATE_INFO (d));
8692 struct pending_template *pt;
8693 int level;
8695 if (TI_PENDING_TEMPLATE_FLAG (ti))
8696 return;
8698 /* We are called both from instantiate_decl, where we've already had a
8699 tinst_level pushed, and instantiate_template, where we haven't.
8700 Compensate. */
8701 level = !current_tinst_level || current_tinst_level->decl != d;
8703 if (level)
8704 push_tinst_level (d);
8706 pt = ggc_alloc<pending_template> ();
8707 pt->next = NULL;
8708 pt->tinst = current_tinst_level;
8709 if (last_pending_template)
8710 last_pending_template->next = pt;
8711 else
8712 pending_templates = pt;
8714 last_pending_template = pt;
8716 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
8718 if (level)
8719 pop_tinst_level ();
8723 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8724 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8725 documentation for TEMPLATE_ID_EXPR. */
8727 tree
8728 lookup_template_function (tree fns, tree arglist)
8730 tree type;
8732 if (fns == error_mark_node || arglist == error_mark_node)
8733 return error_mark_node;
8735 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
8737 if (!is_overloaded_fn (fns) && !identifier_p (fns))
8739 error ("%q#D is not a function template", fns);
8740 return error_mark_node;
8743 if (BASELINK_P (fns))
8745 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
8746 unknown_type_node,
8747 BASELINK_FUNCTIONS (fns),
8748 arglist);
8749 return fns;
8752 type = TREE_TYPE (fns);
8753 if (TREE_CODE (fns) == OVERLOAD || !type)
8754 type = unknown_type_node;
8756 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
8759 /* Within the scope of a template class S<T>, the name S gets bound
8760 (in build_self_reference) to a TYPE_DECL for the class, not a
8761 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8762 or one of its enclosing classes, and that type is a template,
8763 return the associated TEMPLATE_DECL. Otherwise, the original
8764 DECL is returned.
8766 Also handle the case when DECL is a TREE_LIST of ambiguous
8767 injected-class-names from different bases. */
8769 tree
8770 maybe_get_template_decl_from_type_decl (tree decl)
8772 if (decl == NULL_TREE)
8773 return decl;
8775 /* DR 176: A lookup that finds an injected-class-name (10.2
8776 [class.member.lookup]) can result in an ambiguity in certain cases
8777 (for example, if it is found in more than one base class). If all of
8778 the injected-class-names that are found refer to specializations of
8779 the same class template, and if the name is followed by a
8780 template-argument-list, the reference refers to the class template
8781 itself and not a specialization thereof, and is not ambiguous. */
8782 if (TREE_CODE (decl) == TREE_LIST)
8784 tree t, tmpl = NULL_TREE;
8785 for (t = decl; t; t = TREE_CHAIN (t))
8787 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
8788 if (!tmpl)
8789 tmpl = elt;
8790 else if (tmpl != elt)
8791 break;
8793 if (tmpl && t == NULL_TREE)
8794 return tmpl;
8795 else
8796 return decl;
8799 return (decl != NULL_TREE
8800 && DECL_SELF_REFERENCE_P (decl)
8801 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
8802 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
8805 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8806 parameters, find the desired type.
8808 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8810 IN_DECL, if non-NULL, is the template declaration we are trying to
8811 instantiate.
8813 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8814 the class we are looking up.
8816 Issue error and warning messages under control of COMPLAIN.
8818 If the template class is really a local class in a template
8819 function, then the FUNCTION_CONTEXT is the function in which it is
8820 being instantiated.
8822 ??? Note that this function is currently called *twice* for each
8823 template-id: the first time from the parser, while creating the
8824 incomplete type (finish_template_type), and the second type during the
8825 real instantiation (instantiate_template_class). This is surely something
8826 that we want to avoid. It also causes some problems with argument
8827 coercion (see convert_nontype_argument for more information on this). */
8829 static tree
8830 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
8831 int entering_scope, tsubst_flags_t complain)
8833 tree templ = NULL_TREE, parmlist;
8834 tree t;
8835 spec_entry **slot;
8836 spec_entry *entry;
8837 spec_entry elt;
8838 hashval_t hash;
8840 if (identifier_p (d1))
8842 tree value = innermost_non_namespace_value (d1);
8843 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
8844 templ = value;
8845 else
8847 if (context)
8848 push_decl_namespace (context);
8849 templ = lookup_name (d1);
8850 templ = maybe_get_template_decl_from_type_decl (templ);
8851 if (context)
8852 pop_decl_namespace ();
8854 if (templ)
8855 context = DECL_CONTEXT (templ);
8857 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
8859 tree type = TREE_TYPE (d1);
8861 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8862 an implicit typename for the second A. Deal with it. */
8863 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
8864 type = TREE_TYPE (type);
8866 if (CLASSTYPE_TEMPLATE_INFO (type))
8868 templ = CLASSTYPE_TI_TEMPLATE (type);
8869 d1 = DECL_NAME (templ);
8872 else if (TREE_CODE (d1) == ENUMERAL_TYPE
8873 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
8875 templ = TYPE_TI_TEMPLATE (d1);
8876 d1 = DECL_NAME (templ);
8878 else if (DECL_TYPE_TEMPLATE_P (d1))
8880 templ = d1;
8881 d1 = DECL_NAME (templ);
8882 context = DECL_CONTEXT (templ);
8884 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
8886 templ = d1;
8887 d1 = DECL_NAME (templ);
8890 /* Issue an error message if we didn't find a template. */
8891 if (! templ)
8893 if (complain & tf_error)
8894 error ("%qT is not a template", d1);
8895 return error_mark_node;
8898 if (TREE_CODE (templ) != TEMPLATE_DECL
8899 /* Make sure it's a user visible template, if it was named by
8900 the user. */
8901 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
8902 && !PRIMARY_TEMPLATE_P (templ)))
8904 if (complain & tf_error)
8906 error ("non-template type %qT used as a template", d1);
8907 if (in_decl)
8908 error ("for template declaration %q+D", in_decl);
8910 return error_mark_node;
8913 complain &= ~tf_user;
8915 /* An alias that just changes the name of a template is equivalent to the
8916 other template, so if any of the arguments are pack expansions, strip
8917 the alias to avoid problems with a pack expansion passed to a non-pack
8918 alias template parameter (DR 1430). */
8919 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
8920 templ = get_underlying_template (templ);
8922 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
8924 tree parm;
8925 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
8926 if (arglist2 == error_mark_node
8927 || (!uses_template_parms (arglist2)
8928 && check_instantiated_args (templ, arglist2, complain)))
8929 return error_mark_node;
8931 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
8932 return parm;
8934 else
8936 tree template_type = TREE_TYPE (templ);
8937 tree gen_tmpl;
8938 tree type_decl;
8939 tree found = NULL_TREE;
8940 int arg_depth;
8941 int parm_depth;
8942 int is_dependent_type;
8943 int use_partial_inst_tmpl = false;
8945 if (template_type == error_mark_node)
8946 /* An error occurred while building the template TEMPL, and a
8947 diagnostic has most certainly been emitted for that
8948 already. Let's propagate that error. */
8949 return error_mark_node;
8951 gen_tmpl = most_general_template (templ);
8952 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
8953 parm_depth = TMPL_PARMS_DEPTH (parmlist);
8954 arg_depth = TMPL_ARGS_DEPTH (arglist);
8956 if (arg_depth == 1 && parm_depth > 1)
8958 /* We've been given an incomplete set of template arguments.
8959 For example, given:
8961 template <class T> struct S1 {
8962 template <class U> struct S2 {};
8963 template <class U> struct S2<U*> {};
8966 we will be called with an ARGLIST of `U*', but the
8967 TEMPLATE will be `template <class T> template
8968 <class U> struct S1<T>::S2'. We must fill in the missing
8969 arguments. */
8970 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
8971 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
8972 arg_depth = TMPL_ARGS_DEPTH (arglist);
8975 /* Now we should have enough arguments. */
8976 gcc_assert (parm_depth == arg_depth);
8978 /* From here on, we're only interested in the most general
8979 template. */
8981 /* Calculate the BOUND_ARGS. These will be the args that are
8982 actually tsubst'd into the definition to create the
8983 instantiation. */
8984 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
8985 complain,
8986 /*require_all_args=*/true,
8987 /*use_default_args=*/true);
8989 if (arglist == error_mark_node)
8990 /* We were unable to bind the arguments. */
8991 return error_mark_node;
8993 /* In the scope of a template class, explicit references to the
8994 template class refer to the type of the template, not any
8995 instantiation of it. For example, in:
8997 template <class T> class C { void f(C<T>); }
8999 the `C<T>' is just the same as `C'. Outside of the
9000 class, however, such a reference is an instantiation. */
9001 if (entering_scope
9002 || !PRIMARY_TEMPLATE_P (gen_tmpl)
9003 || currently_open_class (template_type))
9005 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
9007 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
9008 return template_type;
9011 /* If we already have this specialization, return it. */
9012 elt.tmpl = gen_tmpl;
9013 elt.args = arglist;
9014 elt.spec = NULL_TREE;
9015 hash = spec_hasher::hash (&elt);
9016 entry = type_specializations->find_with_hash (&elt, hash);
9018 if (entry)
9019 return entry->spec;
9021 /* If the the template's constraints are not satisfied,
9022 then we cannot form a valid type.
9024 Note that the check is deferred until after the hash
9025 lookup. This prevents redundant checks on previously
9026 instantiated specializations. */
9027 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
9029 if (complain & tf_error)
9031 error ("template constraint failure");
9032 diagnose_constraints (input_location, gen_tmpl, arglist);
9034 return error_mark_node;
9037 is_dependent_type = uses_template_parms (arglist);
9039 /* If the deduced arguments are invalid, then the binding
9040 failed. */
9041 if (!is_dependent_type
9042 && check_instantiated_args (gen_tmpl,
9043 INNERMOST_TEMPLATE_ARGS (arglist),
9044 complain))
9045 return error_mark_node;
9047 if (!is_dependent_type
9048 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9049 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
9050 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
9052 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
9053 DECL_NAME (gen_tmpl),
9054 /*tag_scope=*/ts_global);
9055 return found;
9058 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
9059 complain, in_decl);
9060 if (context == error_mark_node)
9061 return error_mark_node;
9063 if (!context)
9064 context = global_namespace;
9066 /* Create the type. */
9067 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9069 /* The user referred to a specialization of an alias
9070 template represented by GEN_TMPL.
9072 [temp.alias]/2 says:
9074 When a template-id refers to the specialization of an
9075 alias template, it is equivalent to the associated
9076 type obtained by substitution of its
9077 template-arguments for the template-parameters in the
9078 type-id of the alias template. */
9080 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
9081 /* Note that the call above (by indirectly calling
9082 register_specialization in tsubst_decl) registers the
9083 TYPE_DECL representing the specialization of the alias
9084 template. So next time someone substitutes ARGLIST for
9085 the template parms into the alias template (GEN_TMPL),
9086 she'll get that TYPE_DECL back. */
9088 if (t == error_mark_node)
9089 return t;
9091 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
9093 if (!is_dependent_type)
9095 set_current_access_from_decl (TYPE_NAME (template_type));
9096 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
9097 tsubst (ENUM_UNDERLYING_TYPE (template_type),
9098 arglist, complain, in_decl),
9099 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
9100 arglist, complain, in_decl),
9101 SCOPED_ENUM_P (template_type), NULL);
9103 if (t == error_mark_node)
9104 return t;
9106 else
9108 /* We don't want to call start_enum for this type, since
9109 the values for the enumeration constants may involve
9110 template parameters. And, no one should be interested
9111 in the enumeration constants for such a type. */
9112 t = cxx_make_type (ENUMERAL_TYPE);
9113 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
9115 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
9116 ENUM_FIXED_UNDERLYING_TYPE_P (t)
9117 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
9119 else if (CLASS_TYPE_P (template_type))
9121 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9122 instantiated here. */
9123 gcc_assert (!LAMBDA_TYPE_P (template_type));
9125 t = make_class_type (TREE_CODE (template_type));
9126 CLASSTYPE_DECLARED_CLASS (t)
9127 = CLASSTYPE_DECLARED_CLASS (template_type);
9128 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
9130 /* A local class. Make sure the decl gets registered properly. */
9131 if (context == current_function_decl)
9132 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
9134 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
9135 /* This instantiation is another name for the primary
9136 template type. Set the TYPE_CANONICAL field
9137 appropriately. */
9138 TYPE_CANONICAL (t) = template_type;
9139 else if (any_template_arguments_need_structural_equality_p (arglist))
9140 /* Some of the template arguments require structural
9141 equality testing, so this template class requires
9142 structural equality testing. */
9143 SET_TYPE_STRUCTURAL_EQUALITY (t);
9145 else
9146 gcc_unreachable ();
9148 /* If we called start_enum or pushtag above, this information
9149 will already be set up. */
9150 if (!TYPE_NAME (t))
9152 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
9154 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
9155 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
9156 DECL_SOURCE_LOCATION (type_decl)
9157 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
9159 else
9160 type_decl = TYPE_NAME (t);
9162 if (CLASS_TYPE_P (template_type))
9164 TREE_PRIVATE (type_decl)
9165 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
9166 TREE_PROTECTED (type_decl)
9167 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
9168 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
9170 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
9171 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
9175 if (OVERLOAD_TYPE_P (t)
9176 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9178 static const char *tags[] = {"abi_tag", "may_alias"};
9180 for (unsigned ix = 0; ix != 2; ix++)
9182 tree attributes
9183 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
9185 if (attributes)
9186 TYPE_ATTRIBUTES (t)
9187 = tree_cons (TREE_PURPOSE (attributes),
9188 TREE_VALUE (attributes),
9189 TYPE_ATTRIBUTES (t));
9193 /* Let's consider the explicit specialization of a member
9194 of a class template specialization that is implicitly instantiated,
9195 e.g.:
9196 template<class T>
9197 struct S
9199 template<class U> struct M {}; //#0
9202 template<>
9203 template<>
9204 struct S<int>::M<char> //#1
9206 int i;
9208 [temp.expl.spec]/4 says this is valid.
9210 In this case, when we write:
9211 S<int>::M<char> m;
9213 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9214 the one of #0.
9216 When we encounter #1, we want to store the partial instantiation
9217 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9219 For all cases other than this "explicit specialization of member of a
9220 class template", we just want to store the most general template into
9221 the CLASSTYPE_TI_TEMPLATE of M.
9223 This case of "explicit specialization of member of a class template"
9224 only happens when:
9225 1/ the enclosing class is an instantiation of, and therefore not
9226 the same as, the context of the most general template, and
9227 2/ we aren't looking at the partial instantiation itself, i.e.
9228 the innermost arguments are not the same as the innermost parms of
9229 the most general template.
9231 So it's only when 1/ and 2/ happens that we want to use the partial
9232 instantiation of the member template in lieu of its most general
9233 template. */
9235 if (PRIMARY_TEMPLATE_P (gen_tmpl)
9236 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
9237 /* the enclosing class must be an instantiation... */
9238 && CLASS_TYPE_P (context)
9239 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
9241 TREE_VEC_LENGTH (arglist)--;
9242 ++processing_template_decl;
9243 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
9244 tree partial_inst_args =
9245 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
9246 arglist, complain, NULL_TREE);
9247 --processing_template_decl;
9248 TREE_VEC_LENGTH (arglist)++;
9249 if (partial_inst_args == error_mark_node)
9250 return error_mark_node;
9251 use_partial_inst_tmpl =
9252 /*...and we must not be looking at the partial instantiation
9253 itself. */
9254 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
9255 partial_inst_args);
9258 if (!use_partial_inst_tmpl)
9259 /* This case is easy; there are no member templates involved. */
9260 found = gen_tmpl;
9261 else
9263 /* This is a full instantiation of a member template. Find
9264 the partial instantiation of which this is an instance. */
9266 /* Temporarily reduce by one the number of levels in the ARGLIST
9267 so as to avoid comparing the last set of arguments. */
9268 TREE_VEC_LENGTH (arglist)--;
9269 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
9270 TREE_VEC_LENGTH (arglist)++;
9271 /* FOUND is either a proper class type, or an alias
9272 template specialization. In the later case, it's a
9273 TYPE_DECL, resulting from the substituting of arguments
9274 for parameters in the TYPE_DECL of the alias template
9275 done earlier. So be careful while getting the template
9276 of FOUND. */
9277 found = (TREE_CODE (found) == TEMPLATE_DECL
9278 ? found
9279 : (TREE_CODE (found) == TYPE_DECL
9280 ? DECL_TI_TEMPLATE (found)
9281 : CLASSTYPE_TI_TEMPLATE (found)));
9284 // Build template info for the new specialization.
9285 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
9287 elt.spec = t;
9288 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
9289 entry = ggc_alloc<spec_entry> ();
9290 *entry = elt;
9291 *slot = entry;
9293 /* Note this use of the partial instantiation so we can check it
9294 later in maybe_process_partial_specialization. */
9295 DECL_TEMPLATE_INSTANTIATIONS (found)
9296 = tree_cons (arglist, t,
9297 DECL_TEMPLATE_INSTANTIATIONS (found));
9299 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
9300 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9301 /* Now that the type has been registered on the instantiations
9302 list, we set up the enumerators. Because the enumeration
9303 constants may involve the enumeration type itself, we make
9304 sure to register the type first, and then create the
9305 constants. That way, doing tsubst_expr for the enumeration
9306 constants won't result in recursive calls here; we'll find
9307 the instantiation and exit above. */
9308 tsubst_enum (template_type, t, arglist);
9310 if (CLASS_TYPE_P (template_type) && is_dependent_type)
9311 /* If the type makes use of template parameters, the
9312 code that generates debugging information will crash. */
9313 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
9315 /* Possibly limit visibility based on template args. */
9316 TREE_PUBLIC (type_decl) = 1;
9317 determine_visibility (type_decl);
9319 inherit_targ_abi_tags (t);
9321 return t;
9325 /* Wrapper for lookup_template_class_1. */
9327 tree
9328 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
9329 int entering_scope, tsubst_flags_t complain)
9331 tree ret;
9332 timevar_push (TV_TEMPLATE_INST);
9333 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
9334 entering_scope, complain);
9335 timevar_pop (TV_TEMPLATE_INST);
9336 return ret;
9339 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9341 tree
9342 lookup_template_variable (tree templ, tree arglist)
9344 /* The type of the expression is NULL_TREE since the template-id could refer
9345 to an explicit or partial specialization. */
9346 tree type = NULL_TREE;
9347 if (flag_concepts && variable_concept_p (templ))
9348 /* Except that concepts are always bool. */
9349 type = boolean_type_node;
9350 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
9353 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9355 tree
9356 finish_template_variable (tree var, tsubst_flags_t complain)
9358 tree templ = TREE_OPERAND (var, 0);
9359 tree arglist = TREE_OPERAND (var, 1);
9361 /* We never want to return a VAR_DECL for a variable concept, since they
9362 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9363 bool concept_p = flag_concepts && variable_concept_p (templ);
9364 if (concept_p && processing_template_decl)
9365 return var;
9367 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
9368 arglist = add_outermost_template_args (tmpl_args, arglist);
9370 templ = most_general_template (templ);
9371 tree parms = DECL_TEMPLATE_PARMS (templ);
9372 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
9373 /*req_all*/true,
9374 /*use_default*/true);
9376 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
9378 if (complain & tf_error)
9380 error ("use of invalid variable template %qE", var);
9381 diagnose_constraints (location_of (var), templ, arglist);
9383 return error_mark_node;
9386 /* If a template-id refers to a specialization of a variable
9387 concept, then the expression is true if and only if the
9388 concept's constraints are satisfied by the given template
9389 arguments.
9391 NOTE: This is an extension of Concepts Lite TS that
9392 allows constraints to be used in expressions. */
9393 if (concept_p)
9395 tree decl = DECL_TEMPLATE_RESULT (templ);
9396 return evaluate_variable_concept (decl, arglist);
9399 return instantiate_template (templ, arglist, complain);
9402 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9403 TARGS template args, and instantiate it if it's not dependent. */
9405 tree
9406 lookup_and_finish_template_variable (tree templ, tree targs,
9407 tsubst_flags_t complain)
9409 templ = lookup_template_variable (templ, targs);
9410 if (!any_dependent_template_arguments_p (targs))
9412 templ = finish_template_variable (templ, complain);
9413 mark_used (templ);
9416 return convert_from_reference (templ);
9420 struct pair_fn_data
9422 tree_fn_t fn;
9423 tree_fn_t any_fn;
9424 void *data;
9425 /* True when we should also visit template parameters that occur in
9426 non-deduced contexts. */
9427 bool include_nondeduced_p;
9428 hash_set<tree> *visited;
9431 /* Called from for_each_template_parm via walk_tree. */
9433 static tree
9434 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
9436 tree t = *tp;
9437 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
9438 tree_fn_t fn = pfd->fn;
9439 void *data = pfd->data;
9440 tree result = NULL_TREE;
9442 #define WALK_SUBTREE(NODE) \
9443 do \
9445 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9446 pfd->include_nondeduced_p, \
9447 pfd->any_fn); \
9448 if (result) goto out; \
9450 while (0)
9452 if (pfd->any_fn && (*pfd->any_fn)(t, data))
9453 return t;
9455 if (TYPE_P (t)
9456 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
9457 WALK_SUBTREE (TYPE_CONTEXT (t));
9459 switch (TREE_CODE (t))
9461 case RECORD_TYPE:
9462 if (TYPE_PTRMEMFUNC_P (t))
9463 break;
9464 /* Fall through. */
9466 case UNION_TYPE:
9467 case ENUMERAL_TYPE:
9468 if (!TYPE_TEMPLATE_INFO (t))
9469 *walk_subtrees = 0;
9470 else
9471 WALK_SUBTREE (TYPE_TI_ARGS (t));
9472 break;
9474 case INTEGER_TYPE:
9475 WALK_SUBTREE (TYPE_MIN_VALUE (t));
9476 WALK_SUBTREE (TYPE_MAX_VALUE (t));
9477 break;
9479 case METHOD_TYPE:
9480 /* Since we're not going to walk subtrees, we have to do this
9481 explicitly here. */
9482 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
9483 /* Fall through. */
9485 case FUNCTION_TYPE:
9486 /* Check the return type. */
9487 WALK_SUBTREE (TREE_TYPE (t));
9489 /* Check the parameter types. Since default arguments are not
9490 instantiated until they are needed, the TYPE_ARG_TYPES may
9491 contain expressions that involve template parameters. But,
9492 no-one should be looking at them yet. And, once they're
9493 instantiated, they don't contain template parameters, so
9494 there's no point in looking at them then, either. */
9496 tree parm;
9498 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
9499 WALK_SUBTREE (TREE_VALUE (parm));
9501 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9502 want walk_tree walking into them itself. */
9503 *walk_subtrees = 0;
9506 if (flag_noexcept_type)
9508 tree spec = TYPE_RAISES_EXCEPTIONS (t);
9509 if (spec)
9510 WALK_SUBTREE (TREE_PURPOSE (spec));
9512 break;
9514 case TYPEOF_TYPE:
9515 case UNDERLYING_TYPE:
9516 if (pfd->include_nondeduced_p
9517 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
9518 pfd->visited,
9519 pfd->include_nondeduced_p,
9520 pfd->any_fn))
9521 return error_mark_node;
9522 break;
9524 case FUNCTION_DECL:
9525 case VAR_DECL:
9526 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9527 WALK_SUBTREE (DECL_TI_ARGS (t));
9528 /* Fall through. */
9530 case PARM_DECL:
9531 case CONST_DECL:
9532 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
9533 WALK_SUBTREE (DECL_INITIAL (t));
9534 if (DECL_CONTEXT (t)
9535 && pfd->include_nondeduced_p)
9536 WALK_SUBTREE (DECL_CONTEXT (t));
9537 break;
9539 case BOUND_TEMPLATE_TEMPLATE_PARM:
9540 /* Record template parameters such as `T' inside `TT<T>'. */
9541 WALK_SUBTREE (TYPE_TI_ARGS (t));
9542 /* Fall through. */
9544 case TEMPLATE_TEMPLATE_PARM:
9545 case TEMPLATE_TYPE_PARM:
9546 case TEMPLATE_PARM_INDEX:
9547 if (fn && (*fn)(t, data))
9548 return t;
9549 else if (!fn)
9550 return t;
9551 break;
9553 case TEMPLATE_DECL:
9554 /* A template template parameter is encountered. */
9555 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9556 WALK_SUBTREE (TREE_TYPE (t));
9558 /* Already substituted template template parameter */
9559 *walk_subtrees = 0;
9560 break;
9562 case TYPENAME_TYPE:
9563 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9564 partial instantiation. */
9565 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
9566 break;
9568 case CONSTRUCTOR:
9569 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
9570 && pfd->include_nondeduced_p)
9571 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
9572 break;
9574 case INDIRECT_REF:
9575 case COMPONENT_REF:
9576 /* If there's no type, then this thing must be some expression
9577 involving template parameters. */
9578 if (!fn && !TREE_TYPE (t))
9579 return error_mark_node;
9580 break;
9582 case MODOP_EXPR:
9583 case CAST_EXPR:
9584 case IMPLICIT_CONV_EXPR:
9585 case REINTERPRET_CAST_EXPR:
9586 case CONST_CAST_EXPR:
9587 case STATIC_CAST_EXPR:
9588 case DYNAMIC_CAST_EXPR:
9589 case ARROW_EXPR:
9590 case DOTSTAR_EXPR:
9591 case TYPEID_EXPR:
9592 case PSEUDO_DTOR_EXPR:
9593 if (!fn)
9594 return error_mark_node;
9595 break;
9597 default:
9598 break;
9601 #undef WALK_SUBTREE
9603 /* We didn't find any template parameters we liked. */
9604 out:
9605 return result;
9608 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9609 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9610 call FN with the parameter and the DATA.
9611 If FN returns nonzero, the iteration is terminated, and
9612 for_each_template_parm returns 1. Otherwise, the iteration
9613 continues. If FN never returns a nonzero value, the value
9614 returned by for_each_template_parm is 0. If FN is NULL, it is
9615 considered to be the function which always returns 1.
9617 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9618 parameters that occur in non-deduced contexts. When false, only
9619 visits those template parameters that can be deduced. */
9621 static tree
9622 for_each_template_parm (tree t, tree_fn_t fn, void* data,
9623 hash_set<tree> *visited,
9624 bool include_nondeduced_p,
9625 tree_fn_t any_fn)
9627 struct pair_fn_data pfd;
9628 tree result;
9630 /* Set up. */
9631 pfd.fn = fn;
9632 pfd.any_fn = any_fn;
9633 pfd.data = data;
9634 pfd.include_nondeduced_p = include_nondeduced_p;
9636 /* Walk the tree. (Conceptually, we would like to walk without
9637 duplicates, but for_each_template_parm_r recursively calls
9638 for_each_template_parm, so we would need to reorganize a fair
9639 bit to use walk_tree_without_duplicates, so we keep our own
9640 visited list.) */
9641 if (visited)
9642 pfd.visited = visited;
9643 else
9644 pfd.visited = new hash_set<tree>;
9645 result = cp_walk_tree (&t,
9646 for_each_template_parm_r,
9647 &pfd,
9648 pfd.visited);
9650 /* Clean up. */
9651 if (!visited)
9653 delete pfd.visited;
9654 pfd.visited = 0;
9657 return result;
9660 /* Returns true if T depends on any template parameter. */
9663 uses_template_parms (tree t)
9665 if (t == NULL_TREE)
9666 return false;
9668 bool dependent_p;
9669 int saved_processing_template_decl;
9671 saved_processing_template_decl = processing_template_decl;
9672 if (!saved_processing_template_decl)
9673 processing_template_decl = 1;
9674 if (TYPE_P (t))
9675 dependent_p = dependent_type_p (t);
9676 else if (TREE_CODE (t) == TREE_VEC)
9677 dependent_p = any_dependent_template_arguments_p (t);
9678 else if (TREE_CODE (t) == TREE_LIST)
9679 dependent_p = (uses_template_parms (TREE_VALUE (t))
9680 || uses_template_parms (TREE_CHAIN (t)));
9681 else if (TREE_CODE (t) == TYPE_DECL)
9682 dependent_p = dependent_type_p (TREE_TYPE (t));
9683 else if (DECL_P (t)
9684 || EXPR_P (t)
9685 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
9686 || TREE_CODE (t) == OVERLOAD
9687 || BASELINK_P (t)
9688 || identifier_p (t)
9689 || TREE_CODE (t) == TRAIT_EXPR
9690 || TREE_CODE (t) == CONSTRUCTOR
9691 || CONSTANT_CLASS_P (t))
9692 dependent_p = (type_dependent_expression_p (t)
9693 || value_dependent_expression_p (t));
9694 else
9696 gcc_assert (t == error_mark_node);
9697 dependent_p = false;
9700 processing_template_decl = saved_processing_template_decl;
9702 return dependent_p;
9705 /* Returns true iff current_function_decl is an incompletely instantiated
9706 template. Useful instead of processing_template_decl because the latter
9707 is set to 0 during instantiate_non_dependent_expr. */
9709 bool
9710 in_template_function (void)
9712 tree fn = current_function_decl;
9713 bool ret;
9714 ++processing_template_decl;
9715 ret = (fn && DECL_LANG_SPECIFIC (fn)
9716 && DECL_TEMPLATE_INFO (fn)
9717 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
9718 --processing_template_decl;
9719 return ret;
9722 /* Returns true if T depends on any template parameter with level LEVEL. */
9724 bool
9725 uses_template_parms_level (tree t, int level)
9727 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
9728 /*include_nondeduced_p=*/true);
9731 /* Returns true if the signature of DECL depends on any template parameter from
9732 its enclosing class. */
9734 bool
9735 uses_outer_template_parms (tree decl)
9737 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
9738 if (depth == 0)
9739 return false;
9740 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
9741 &depth, NULL, /*include_nondeduced_p=*/true))
9742 return true;
9743 if (PRIMARY_TEMPLATE_P (decl)
9744 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9745 (DECL_TEMPLATE_PARMS (decl)),
9746 template_parm_outer_level,
9747 &depth, NULL, /*include_nondeduced_p=*/true))
9748 return true;
9749 tree ci = get_constraints (decl);
9750 if (ci)
9751 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
9752 if (ci && for_each_template_parm (ci, template_parm_outer_level,
9753 &depth, NULL, /*nondeduced*/true))
9754 return true;
9755 return false;
9758 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9759 ill-formed translation unit, i.e. a variable or function that isn't
9760 usable in a constant expression. */
9762 static inline bool
9763 neglectable_inst_p (tree d)
9765 return (DECL_P (d)
9766 && !undeduced_auto_decl (d)
9767 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
9768 : decl_maybe_constant_var_p (d)));
9771 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9772 neglectable and instantiated from within an erroneous instantiation. */
9774 static bool
9775 limit_bad_template_recursion (tree decl)
9777 struct tinst_level *lev = current_tinst_level;
9778 int errs = errorcount + sorrycount;
9779 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
9780 return false;
9782 for (; lev; lev = lev->next)
9783 if (neglectable_inst_p (lev->decl))
9784 break;
9786 return (lev && errs > lev->errors);
9789 static int tinst_depth;
9790 extern int max_tinst_depth;
9791 int depth_reached;
9793 static GTY(()) struct tinst_level *last_error_tinst_level;
9795 /* We're starting to instantiate D; record the template instantiation context
9796 for diagnostics and to restore it later. */
9798 bool
9799 push_tinst_level (tree d)
9801 return push_tinst_level_loc (d, input_location);
9804 /* We're starting to instantiate D; record the template instantiation context
9805 at LOC for diagnostics and to restore it later. */
9807 bool
9808 push_tinst_level_loc (tree d, location_t loc)
9810 struct tinst_level *new_level;
9812 if (tinst_depth >= max_tinst_depth)
9814 /* Tell error.c not to try to instantiate any templates. */
9815 at_eof = 2;
9816 fatal_error (input_location,
9817 "template instantiation depth exceeds maximum of %d"
9818 " (use -ftemplate-depth= to increase the maximum)",
9819 max_tinst_depth);
9820 return false;
9823 /* If the current instantiation caused problems, don't let it instantiate
9824 anything else. Do allow deduction substitution and decls usable in
9825 constant expressions. */
9826 if (limit_bad_template_recursion (d))
9827 return false;
9829 /* When not -quiet, dump template instantiations other than functions, since
9830 announce_function will take care of those. */
9831 if (!quiet_flag
9832 && TREE_CODE (d) != TREE_LIST
9833 && TREE_CODE (d) != FUNCTION_DECL)
9834 fprintf (stderr, " %s", decl_as_string (d, TFF_DECL_SPECIFIERS));
9836 new_level = ggc_alloc<tinst_level> ();
9837 new_level->decl = d;
9838 new_level->locus = loc;
9839 new_level->errors = errorcount+sorrycount;
9840 new_level->in_system_header_p = in_system_header_at (input_location);
9841 new_level->next = current_tinst_level;
9842 current_tinst_level = new_level;
9844 ++tinst_depth;
9845 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
9846 depth_reached = tinst_depth;
9848 return true;
9851 /* We're done instantiating this template; return to the instantiation
9852 context. */
9854 void
9855 pop_tinst_level (void)
9857 /* Restore the filename and line number stashed away when we started
9858 this instantiation. */
9859 input_location = current_tinst_level->locus;
9860 current_tinst_level = current_tinst_level->next;
9861 --tinst_depth;
9864 /* We're instantiating a deferred template; restore the template
9865 instantiation context in which the instantiation was requested, which
9866 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9868 static tree
9869 reopen_tinst_level (struct tinst_level *level)
9871 struct tinst_level *t;
9873 tinst_depth = 0;
9874 for (t = level; t; t = t->next)
9875 ++tinst_depth;
9877 current_tinst_level = level;
9878 pop_tinst_level ();
9879 if (current_tinst_level)
9880 current_tinst_level->errors = errorcount+sorrycount;
9881 return level->decl;
9884 /* Returns the TINST_LEVEL which gives the original instantiation
9885 context. */
9887 struct tinst_level *
9888 outermost_tinst_level (void)
9890 struct tinst_level *level = current_tinst_level;
9891 if (level)
9892 while (level->next)
9893 level = level->next;
9894 return level;
9897 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9898 vector of template arguments, as for tsubst.
9900 Returns an appropriate tsubst'd friend declaration. */
9902 static tree
9903 tsubst_friend_function (tree decl, tree args)
9905 tree new_friend;
9907 if (TREE_CODE (decl) == FUNCTION_DECL
9908 && DECL_TEMPLATE_INSTANTIATION (decl)
9909 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
9910 /* This was a friend declared with an explicit template
9911 argument list, e.g.:
9913 friend void f<>(T);
9915 to indicate that f was a template instantiation, not a new
9916 function declaration. Now, we have to figure out what
9917 instantiation of what template. */
9919 tree template_id, arglist, fns;
9920 tree new_args;
9921 tree tmpl;
9922 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
9924 /* Friend functions are looked up in the containing namespace scope.
9925 We must enter that scope, to avoid finding member functions of the
9926 current class with same name. */
9927 push_nested_namespace (ns);
9928 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
9929 tf_warning_or_error, NULL_TREE,
9930 /*integral_constant_expression_p=*/false);
9931 pop_nested_namespace (ns);
9932 arglist = tsubst (DECL_TI_ARGS (decl), args,
9933 tf_warning_or_error, NULL_TREE);
9934 template_id = lookup_template_function (fns, arglist);
9936 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9937 tmpl = determine_specialization (template_id, new_friend,
9938 &new_args,
9939 /*need_member_template=*/0,
9940 TREE_VEC_LENGTH (args),
9941 tsk_none);
9942 return instantiate_template (tmpl, new_args, tf_error);
9945 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9947 /* The NEW_FRIEND will look like an instantiation, to the
9948 compiler, but is not an instantiation from the point of view of
9949 the language. For example, we might have had:
9951 template <class T> struct S {
9952 template <class U> friend void f(T, U);
9955 Then, in S<int>, template <class U> void f(int, U) is not an
9956 instantiation of anything. */
9957 if (new_friend == error_mark_node)
9958 return error_mark_node;
9960 DECL_USE_TEMPLATE (new_friend) = 0;
9961 if (TREE_CODE (decl) == TEMPLATE_DECL)
9963 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
9964 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
9965 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
9968 /* The mangled name for the NEW_FRIEND is incorrect. The function
9969 is not a template instantiation and should not be mangled like
9970 one. Therefore, we forget the mangling here; we'll recompute it
9971 later if we need it. */
9972 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
9974 SET_DECL_RTL (new_friend, NULL);
9975 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
9978 if (DECL_NAMESPACE_SCOPE_P (new_friend))
9980 tree old_decl;
9981 tree new_friend_template_info;
9982 tree new_friend_result_template_info;
9983 tree ns;
9984 int new_friend_is_defn;
9986 /* We must save some information from NEW_FRIEND before calling
9987 duplicate decls since that function will free NEW_FRIEND if
9988 possible. */
9989 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
9990 new_friend_is_defn =
9991 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9992 (template_for_substitution (new_friend)))
9993 != NULL_TREE);
9994 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
9996 /* This declaration is a `primary' template. */
9997 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
9999 new_friend_result_template_info
10000 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
10002 else
10003 new_friend_result_template_info = NULL_TREE;
10005 /* Inside pushdecl_namespace_level, we will push into the
10006 current namespace. However, the friend function should go
10007 into the namespace of the template. */
10008 ns = decl_namespace_context (new_friend);
10009 push_nested_namespace (ns);
10010 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
10011 pop_nested_namespace (ns);
10013 if (old_decl == error_mark_node)
10014 return error_mark_node;
10016 if (old_decl != new_friend)
10018 /* This new friend declaration matched an existing
10019 declaration. For example, given:
10021 template <class T> void f(T);
10022 template <class U> class C {
10023 template <class T> friend void f(T) {}
10026 the friend declaration actually provides the definition
10027 of `f', once C has been instantiated for some type. So,
10028 old_decl will be the out-of-class template declaration,
10029 while new_friend is the in-class definition.
10031 But, if `f' was called before this point, the
10032 instantiation of `f' will have DECL_TI_ARGS corresponding
10033 to `T' but not to `U', references to which might appear
10034 in the definition of `f'. Previously, the most general
10035 template for an instantiation of `f' was the out-of-class
10036 version; now it is the in-class version. Therefore, we
10037 run through all specialization of `f', adding to their
10038 DECL_TI_ARGS appropriately. In particular, they need a
10039 new set of outer arguments, corresponding to the
10040 arguments for this class instantiation.
10042 The same situation can arise with something like this:
10044 friend void f(int);
10045 template <class T> class C {
10046 friend void f(T) {}
10049 when `C<int>' is instantiated. Now, `f(int)' is defined
10050 in the class. */
10052 if (!new_friend_is_defn)
10053 /* On the other hand, if the in-class declaration does
10054 *not* provide a definition, then we don't want to alter
10055 existing definitions. We can just leave everything
10056 alone. */
10058 else
10060 tree new_template = TI_TEMPLATE (new_friend_template_info);
10061 tree new_args = TI_ARGS (new_friend_template_info);
10063 /* Overwrite whatever template info was there before, if
10064 any, with the new template information pertaining to
10065 the declaration. */
10066 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
10068 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
10070 /* We should have called reregister_specialization in
10071 duplicate_decls. */
10072 gcc_assert (retrieve_specialization (new_template,
10073 new_args, 0)
10074 == old_decl);
10076 /* Instantiate it if the global has already been used. */
10077 if (DECL_ODR_USED (old_decl))
10078 instantiate_decl (old_decl, /*defer_ok=*/true,
10079 /*expl_inst_class_mem_p=*/false);
10081 else
10083 tree t;
10085 /* Indicate that the old function template is a partial
10086 instantiation. */
10087 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
10088 = new_friend_result_template_info;
10090 gcc_assert (new_template
10091 == most_general_template (new_template));
10092 gcc_assert (new_template != old_decl);
10094 /* Reassign any specializations already in the hash table
10095 to the new more general template, and add the
10096 additional template args. */
10097 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
10098 t != NULL_TREE;
10099 t = TREE_CHAIN (t))
10101 tree spec = TREE_VALUE (t);
10102 spec_entry elt;
10104 elt.tmpl = old_decl;
10105 elt.args = DECL_TI_ARGS (spec);
10106 elt.spec = NULL_TREE;
10108 decl_specializations->remove_elt (&elt);
10110 DECL_TI_ARGS (spec)
10111 = add_outermost_template_args (new_args,
10112 DECL_TI_ARGS (spec));
10114 register_specialization
10115 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
10118 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
10122 /* The information from NEW_FRIEND has been merged into OLD_DECL
10123 by duplicate_decls. */
10124 new_friend = old_decl;
10127 else
10129 tree context = DECL_CONTEXT (new_friend);
10130 bool dependent_p;
10132 /* In the code
10133 template <class T> class C {
10134 template <class U> friend void C1<U>::f (); // case 1
10135 friend void C2<T>::f (); // case 2
10137 we only need to make sure CONTEXT is a complete type for
10138 case 2. To distinguish between the two cases, we note that
10139 CONTEXT of case 1 remains dependent type after tsubst while
10140 this isn't true for case 2. */
10141 ++processing_template_decl;
10142 dependent_p = dependent_type_p (context);
10143 --processing_template_decl;
10145 if (!dependent_p
10146 && !complete_type_or_else (context, NULL_TREE))
10147 return error_mark_node;
10149 if (COMPLETE_TYPE_P (context))
10151 tree fn = new_friend;
10152 /* do_friend adds the TEMPLATE_DECL for any member friend
10153 template even if it isn't a member template, i.e.
10154 template <class T> friend A<T>::f();
10155 Look through it in that case. */
10156 if (TREE_CODE (fn) == TEMPLATE_DECL
10157 && !PRIMARY_TEMPLATE_P (fn))
10158 fn = DECL_TEMPLATE_RESULT (fn);
10159 /* Check to see that the declaration is really present, and,
10160 possibly obtain an improved declaration. */
10161 fn = check_classfn (context, fn, NULL_TREE);
10163 if (fn)
10164 new_friend = fn;
10168 return new_friend;
10171 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
10172 template arguments, as for tsubst.
10174 Returns an appropriate tsubst'd friend type or error_mark_node on
10175 failure. */
10177 static tree
10178 tsubst_friend_class (tree friend_tmpl, tree args)
10180 tree tmpl;
10182 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
10184 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
10185 return TREE_TYPE (tmpl);
10188 tree context = CP_DECL_CONTEXT (friend_tmpl);
10189 if (TREE_CODE (context) == NAMESPACE_DECL)
10190 push_nested_namespace (context);
10191 else
10192 push_nested_class (context);
10194 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), /*prefer_type=*/false,
10195 /*non_class=*/false, /*block_p=*/false,
10196 /*namespaces_only=*/false, LOOKUP_HIDDEN);
10198 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
10200 /* The friend template has already been declared. Just
10201 check to see that the declarations match, and install any new
10202 default parameters. We must tsubst the default parameters,
10203 of course. We only need the innermost template parameters
10204 because that is all that redeclare_class_template will look
10205 at. */
10206 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
10207 > TMPL_ARGS_DEPTH (args))
10209 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
10210 args, tf_warning_or_error);
10211 location_t saved_input_location = input_location;
10212 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
10213 tree cons = get_constraints (tmpl);
10214 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
10215 input_location = saved_input_location;
10218 else
10220 /* The friend template has not already been declared. In this
10221 case, the instantiation of the template class will cause the
10222 injection of this template into the namespace scope. */
10223 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
10225 if (tmpl != error_mark_node)
10227 /* The new TMPL is not an instantiation of anything, so we
10228 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
10229 for the new type because that is supposed to be the
10230 corresponding template decl, i.e., TMPL. */
10231 DECL_USE_TEMPLATE (tmpl) = 0;
10232 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
10233 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
10234 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
10235 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
10237 /* It is hidden. */
10238 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl));
10239 DECL_ANTICIPATED (tmpl)
10240 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl)) = true;
10242 /* Inject this template into the enclosing namspace scope. */
10243 tmpl = pushdecl_namespace_level (tmpl, true);
10247 if (TREE_CODE (context) == NAMESPACE_DECL)
10248 pop_nested_namespace (context);
10249 else
10250 pop_nested_class ();
10252 return TREE_TYPE (tmpl);
10255 /* Returns zero if TYPE cannot be completed later due to circularity.
10256 Otherwise returns one. */
10258 static int
10259 can_complete_type_without_circularity (tree type)
10261 if (type == NULL_TREE || type == error_mark_node)
10262 return 0;
10263 else if (COMPLETE_TYPE_P (type))
10264 return 1;
10265 else if (TREE_CODE (type) == ARRAY_TYPE)
10266 return can_complete_type_without_circularity (TREE_TYPE (type));
10267 else if (CLASS_TYPE_P (type)
10268 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
10269 return 0;
10270 else
10271 return 1;
10274 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
10275 tsubst_flags_t, tree);
10277 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10278 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10280 static tree
10281 tsubst_attribute (tree t, tree *decl_p, tree args,
10282 tsubst_flags_t complain, tree in_decl)
10284 gcc_assert (ATTR_IS_DEPENDENT (t));
10286 tree val = TREE_VALUE (t);
10287 if (val == NULL_TREE)
10288 /* Nothing to do. */;
10289 else if ((flag_openmp || flag_openmp_simd)
10290 && is_attribute_p ("omp declare simd",
10291 get_attribute_name (t)))
10293 tree clauses = TREE_VALUE (val);
10294 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
10295 complain, in_decl);
10296 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
10297 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
10298 tree parms = DECL_ARGUMENTS (*decl_p);
10299 clauses
10300 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
10301 if (clauses)
10302 val = build_tree_list (NULL_TREE, clauses);
10303 else
10304 val = NULL_TREE;
10306 /* If the first attribute argument is an identifier, don't
10307 pass it through tsubst. Attributes like mode, format,
10308 cleanup and several target specific attributes expect it
10309 unmodified. */
10310 else if (attribute_takes_identifier_p (get_attribute_name (t)))
10312 tree chain
10313 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
10314 /*integral_constant_expression_p=*/false);
10315 if (chain != TREE_CHAIN (val))
10316 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
10318 else if (PACK_EXPANSION_P (val))
10320 /* An attribute pack expansion. */
10321 tree purp = TREE_PURPOSE (t);
10322 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
10323 if (pack == error_mark_node)
10324 return error_mark_node;
10325 int len = TREE_VEC_LENGTH (pack);
10326 tree list = NULL_TREE;
10327 tree *q = &list;
10328 for (int i = 0; i < len; ++i)
10330 tree elt = TREE_VEC_ELT (pack, i);
10331 *q = build_tree_list (purp, elt);
10332 q = &TREE_CHAIN (*q);
10334 return list;
10336 else
10337 val = tsubst_expr (val, args, complain, in_decl,
10338 /*integral_constant_expression_p=*/false);
10340 if (val != TREE_VALUE (t))
10341 return build_tree_list (TREE_PURPOSE (t), val);
10342 return t;
10345 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10346 unchanged or a new TREE_LIST chain. */
10348 static tree
10349 tsubst_attributes (tree attributes, tree args,
10350 tsubst_flags_t complain, tree in_decl)
10352 tree last_dep = NULL_TREE;
10354 for (tree t = attributes; t; t = TREE_CHAIN (t))
10355 if (ATTR_IS_DEPENDENT (t))
10357 last_dep = t;
10358 attributes = copy_list (attributes);
10359 break;
10362 if (last_dep)
10363 for (tree *p = &attributes; *p; )
10365 tree t = *p;
10366 if (ATTR_IS_DEPENDENT (t))
10368 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
10369 if (subst != t)
10371 *p = subst;
10372 while (*p)
10373 p = &TREE_CHAIN (*p);
10374 *p = TREE_CHAIN (t);
10375 continue;
10378 p = &TREE_CHAIN (*p);
10381 return attributes;
10384 /* Apply any attributes which had to be deferred until instantiation
10385 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10386 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10388 static void
10389 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
10390 tree args, tsubst_flags_t complain, tree in_decl)
10392 tree last_dep = NULL_TREE;
10393 tree t;
10394 tree *p;
10396 if (attributes == NULL_TREE)
10397 return;
10399 if (DECL_P (*decl_p))
10401 if (TREE_TYPE (*decl_p) == error_mark_node)
10402 return;
10403 p = &DECL_ATTRIBUTES (*decl_p);
10404 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10405 to our attributes parameter. */
10406 gcc_assert (*p == attributes);
10408 else
10410 p = &TYPE_ATTRIBUTES (*decl_p);
10411 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10412 lookup_template_class_1, and should be preserved. */
10413 gcc_assert (*p != attributes);
10414 while (*p)
10415 p = &TREE_CHAIN (*p);
10418 for (t = attributes; t; t = TREE_CHAIN (t))
10419 if (ATTR_IS_DEPENDENT (t))
10421 last_dep = t;
10422 attributes = copy_list (attributes);
10423 break;
10426 *p = attributes;
10427 if (last_dep)
10429 tree late_attrs = NULL_TREE;
10430 tree *q = &late_attrs;
10432 for (; *p; )
10434 t = *p;
10435 if (ATTR_IS_DEPENDENT (t))
10437 *p = TREE_CHAIN (t);
10438 TREE_CHAIN (t) = NULL_TREE;
10439 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
10440 while (*q)
10441 q = &TREE_CHAIN (*q);
10443 else
10444 p = &TREE_CHAIN (t);
10447 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
10451 /* Perform (or defer) access check for typedefs that were referenced
10452 from within the template TMPL code.
10453 This is a subroutine of instantiate_decl and instantiate_class_template.
10454 TMPL is the template to consider and TARGS is the list of arguments of
10455 that template. */
10457 static void
10458 perform_typedefs_access_check (tree tmpl, tree targs)
10460 location_t saved_location;
10461 unsigned i;
10462 qualified_typedef_usage_t *iter;
10464 if (!tmpl
10465 || (!CLASS_TYPE_P (tmpl)
10466 && TREE_CODE (tmpl) != FUNCTION_DECL))
10467 return;
10469 saved_location = input_location;
10470 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
10472 tree type_decl = iter->typedef_decl;
10473 tree type_scope = iter->context;
10475 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
10476 continue;
10478 if (uses_template_parms (type_decl))
10479 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
10480 if (uses_template_parms (type_scope))
10481 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
10483 /* Make access check error messages point to the location
10484 of the use of the typedef. */
10485 input_location = iter->locus;
10486 perform_or_defer_access_check (TYPE_BINFO (type_scope),
10487 type_decl, type_decl,
10488 tf_warning_or_error);
10490 input_location = saved_location;
10493 static tree
10494 instantiate_class_template_1 (tree type)
10496 tree templ, args, pattern, t, member;
10497 tree typedecl;
10498 tree pbinfo;
10499 tree base_list;
10500 unsigned int saved_maximum_field_alignment;
10501 tree fn_context;
10503 if (type == error_mark_node)
10504 return error_mark_node;
10506 if (COMPLETE_OR_OPEN_TYPE_P (type)
10507 || uses_template_parms (type))
10508 return type;
10510 /* Figure out which template is being instantiated. */
10511 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
10512 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
10514 /* Mark the type as in the process of being defined. */
10515 TYPE_BEING_DEFINED (type) = 1;
10517 /* Determine what specialization of the original template to
10518 instantiate. */
10519 t = most_specialized_partial_spec (type, tf_warning_or_error);
10520 if (t == error_mark_node)
10521 return error_mark_node;
10522 else if (t)
10524 /* This TYPE is actually an instantiation of a partial
10525 specialization. We replace the innermost set of ARGS with
10526 the arguments appropriate for substitution. For example,
10527 given:
10529 template <class T> struct S {};
10530 template <class T> struct S<T*> {};
10532 and supposing that we are instantiating S<int*>, ARGS will
10533 presently be {int*} -- but we need {int}. */
10534 pattern = TREE_TYPE (t);
10535 args = TREE_PURPOSE (t);
10537 else
10539 pattern = TREE_TYPE (templ);
10540 args = CLASSTYPE_TI_ARGS (type);
10543 /* If the template we're instantiating is incomplete, then clearly
10544 there's nothing we can do. */
10545 if (!COMPLETE_TYPE_P (pattern))
10547 /* We can try again later. */
10548 TYPE_BEING_DEFINED (type) = 0;
10549 return type;
10552 /* If we've recursively instantiated too many templates, stop. */
10553 if (! push_tinst_level (type))
10554 return type;
10556 /* We may be in the middle of deferred access check. Disable
10557 it now. */
10558 push_deferring_access_checks (dk_no_deferred);
10560 int saved_unevaluated_operand = cp_unevaluated_operand;
10561 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10563 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
10564 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10565 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
10566 fn_context = error_mark_node;
10567 if (!fn_context)
10568 push_to_top_level ();
10569 else
10571 cp_unevaluated_operand = 0;
10572 c_inhibit_evaluation_warnings = 0;
10574 /* Use #pragma pack from the template context. */
10575 saved_maximum_field_alignment = maximum_field_alignment;
10576 maximum_field_alignment = TYPE_PRECISION (pattern);
10578 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
10580 /* Set the input location to the most specialized template definition.
10581 This is needed if tsubsting causes an error. */
10582 typedecl = TYPE_MAIN_DECL (pattern);
10583 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
10584 DECL_SOURCE_LOCATION (typedecl);
10586 TYPE_PACKED (type) = TYPE_PACKED (pattern);
10587 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
10588 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
10589 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
10590 if (ANON_AGGR_TYPE_P (pattern))
10591 SET_ANON_AGGR_TYPE_P (type);
10592 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
10594 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
10595 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
10596 /* Adjust visibility for template arguments. */
10597 determine_visibility (TYPE_MAIN_DECL (type));
10599 if (CLASS_TYPE_P (type))
10600 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
10602 pbinfo = TYPE_BINFO (pattern);
10604 /* We should never instantiate a nested class before its enclosing
10605 class; we need to look up the nested class by name before we can
10606 instantiate it, and that lookup should instantiate the enclosing
10607 class. */
10608 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
10609 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
10611 base_list = NULL_TREE;
10612 if (BINFO_N_BASE_BINFOS (pbinfo))
10614 tree pbase_binfo;
10615 tree pushed_scope;
10616 int i;
10618 /* We must enter the scope containing the type, as that is where
10619 the accessibility of types named in dependent bases are
10620 looked up from. */
10621 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
10623 /* Substitute into each of the bases to determine the actual
10624 basetypes. */
10625 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
10627 tree base;
10628 tree access = BINFO_BASE_ACCESS (pbinfo, i);
10629 tree expanded_bases = NULL_TREE;
10630 int idx, len = 1;
10632 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
10634 expanded_bases =
10635 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
10636 args, tf_error, NULL_TREE);
10637 if (expanded_bases == error_mark_node)
10638 continue;
10640 len = TREE_VEC_LENGTH (expanded_bases);
10643 for (idx = 0; idx < len; idx++)
10645 if (expanded_bases)
10646 /* Extract the already-expanded base class. */
10647 base = TREE_VEC_ELT (expanded_bases, idx);
10648 else
10649 /* Substitute to figure out the base class. */
10650 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
10651 NULL_TREE);
10653 if (base == error_mark_node)
10654 continue;
10656 base_list = tree_cons (access, base, base_list);
10657 if (BINFO_VIRTUAL_P (pbase_binfo))
10658 TREE_TYPE (base_list) = integer_type_node;
10662 /* The list is now in reverse order; correct that. */
10663 base_list = nreverse (base_list);
10665 if (pushed_scope)
10666 pop_scope (pushed_scope);
10668 /* Now call xref_basetypes to set up all the base-class
10669 information. */
10670 xref_basetypes (type, base_list);
10672 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
10673 (int) ATTR_FLAG_TYPE_IN_PLACE,
10674 args, tf_error, NULL_TREE);
10675 fixup_attribute_variants (type);
10677 /* Now that our base classes are set up, enter the scope of the
10678 class, so that name lookups into base classes, etc. will work
10679 correctly. This is precisely analogous to what we do in
10680 begin_class_definition when defining an ordinary non-template
10681 class, except we also need to push the enclosing classes. */
10682 push_nested_class (type);
10684 /* Now members are processed in the order of declaration. */
10685 for (member = CLASSTYPE_DECL_LIST (pattern);
10686 member; member = TREE_CHAIN (member))
10688 tree t = TREE_VALUE (member);
10690 if (TREE_PURPOSE (member))
10692 if (TYPE_P (t))
10694 if (LAMBDA_TYPE_P (t))
10695 /* A closure type for a lambda in an NSDMI or default argument.
10696 Ignore it; it will be regenerated when needed. */
10697 continue;
10699 /* Build new CLASSTYPE_NESTED_UTDS. */
10701 tree newtag;
10702 bool class_template_p;
10704 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
10705 && TYPE_LANG_SPECIFIC (t)
10706 && CLASSTYPE_IS_TEMPLATE (t));
10707 /* If the member is a class template, then -- even after
10708 substitution -- there may be dependent types in the
10709 template argument list for the class. We increment
10710 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10711 that function will assume that no types are dependent
10712 when outside of a template. */
10713 if (class_template_p)
10714 ++processing_template_decl;
10715 newtag = tsubst (t, args, tf_error, NULL_TREE);
10716 if (class_template_p)
10717 --processing_template_decl;
10718 if (newtag == error_mark_node)
10719 continue;
10721 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
10723 tree name = TYPE_IDENTIFIER (t);
10725 if (class_template_p)
10726 /* Unfortunately, lookup_template_class sets
10727 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10728 instantiation (i.e., for the type of a member
10729 template class nested within a template class.)
10730 This behavior is required for
10731 maybe_process_partial_specialization to work
10732 correctly, but is not accurate in this case;
10733 the TAG is not an instantiation of anything.
10734 (The corresponding TEMPLATE_DECL is an
10735 instantiation, but the TYPE is not.) */
10736 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
10738 /* Now, we call pushtag to put this NEWTAG into the scope of
10739 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10740 pushtag calling push_template_decl. We don't have to do
10741 this for enums because it will already have been done in
10742 tsubst_enum. */
10743 if (name)
10744 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
10745 pushtag (name, newtag, /*tag_scope=*/ts_current);
10748 else if (DECL_DECLARES_FUNCTION_P (t))
10750 tree r;
10752 if (TREE_CODE (t) == TEMPLATE_DECL)
10753 ++processing_template_decl;
10754 r = tsubst (t, args, tf_error, NULL_TREE);
10755 if (TREE_CODE (t) == TEMPLATE_DECL)
10756 --processing_template_decl;
10757 set_current_access_from_decl (r);
10758 finish_member_declaration (r);
10759 /* Instantiate members marked with attribute used. */
10760 if (r != error_mark_node && DECL_PRESERVE_P (r))
10761 mark_used (r);
10762 if (TREE_CODE (r) == FUNCTION_DECL
10763 && DECL_OMP_DECLARE_REDUCTION_P (r))
10764 cp_check_omp_declare_reduction (r);
10766 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
10767 && LAMBDA_TYPE_P (TREE_TYPE (t)))
10768 /* A closure type for a lambda in an NSDMI or default argument.
10769 Ignore it; it will be regenerated when needed. */;
10770 else
10772 /* Build new TYPE_FIELDS. */
10773 if (TREE_CODE (t) == STATIC_ASSERT)
10775 tree condition;
10777 ++c_inhibit_evaluation_warnings;
10778 condition =
10779 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
10780 tf_warning_or_error, NULL_TREE,
10781 /*integral_constant_expression_p=*/true);
10782 --c_inhibit_evaluation_warnings;
10784 finish_static_assert (condition,
10785 STATIC_ASSERT_MESSAGE (t),
10786 STATIC_ASSERT_SOURCE_LOCATION (t),
10787 /*member_p=*/true);
10789 else if (TREE_CODE (t) != CONST_DECL)
10791 tree r;
10792 tree vec = NULL_TREE;
10793 int len = 1;
10795 /* The file and line for this declaration, to
10796 assist in error message reporting. Since we
10797 called push_tinst_level above, we don't need to
10798 restore these. */
10799 input_location = DECL_SOURCE_LOCATION (t);
10801 if (TREE_CODE (t) == TEMPLATE_DECL)
10802 ++processing_template_decl;
10803 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
10804 if (TREE_CODE (t) == TEMPLATE_DECL)
10805 --processing_template_decl;
10807 if (TREE_CODE (r) == TREE_VEC)
10809 /* A capture pack became multiple fields. */
10810 vec = r;
10811 len = TREE_VEC_LENGTH (vec);
10814 for (int i = 0; i < len; ++i)
10816 if (vec)
10817 r = TREE_VEC_ELT (vec, i);
10818 if (VAR_P (r))
10820 /* In [temp.inst]:
10822 [t]he initialization (and any associated
10823 side-effects) of a static data member does
10824 not occur unless the static data member is
10825 itself used in a way that requires the
10826 definition of the static data member to
10827 exist.
10829 Therefore, we do not substitute into the
10830 initialized for the static data member here. */
10831 finish_static_data_member_decl
10833 /*init=*/NULL_TREE,
10834 /*init_const_expr_p=*/false,
10835 /*asmspec_tree=*/NULL_TREE,
10836 /*flags=*/0);
10837 /* Instantiate members marked with attribute used. */
10838 if (r != error_mark_node && DECL_PRESERVE_P (r))
10839 mark_used (r);
10841 else if (TREE_CODE (r) == FIELD_DECL)
10843 /* Determine whether R has a valid type and can be
10844 completed later. If R is invalid, then its type
10845 is replaced by error_mark_node. */
10846 tree rtype = TREE_TYPE (r);
10847 if (can_complete_type_without_circularity (rtype))
10848 complete_type (rtype);
10850 if (!complete_or_array_type_p (rtype))
10852 /* If R's type couldn't be completed and
10853 it isn't a flexible array member (whose
10854 type is incomplete by definition) give
10855 an error. */
10856 cxx_incomplete_type_error (r, rtype);
10857 TREE_TYPE (r) = error_mark_node;
10861 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10862 such a thing will already have been added to the field
10863 list by tsubst_enum in finish_member_declaration in the
10864 CLASSTYPE_NESTED_UTDS case above. */
10865 if (!(TREE_CODE (r) == TYPE_DECL
10866 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
10867 && DECL_ARTIFICIAL (r)))
10869 set_current_access_from_decl (r);
10870 finish_member_declaration (r);
10876 else
10878 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
10879 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10881 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10883 tree friend_type = t;
10884 bool adjust_processing_template_decl = false;
10886 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10888 /* template <class T> friend class C; */
10889 friend_type = tsubst_friend_class (friend_type, args);
10890 adjust_processing_template_decl = true;
10892 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
10894 /* template <class T> friend class C::D; */
10895 friend_type = tsubst (friend_type, args,
10896 tf_warning_or_error, NULL_TREE);
10897 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10898 friend_type = TREE_TYPE (friend_type);
10899 adjust_processing_template_decl = true;
10901 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
10902 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
10904 /* This could be either
10906 friend class T::C;
10908 when dependent_type_p is false or
10910 template <class U> friend class T::C;
10912 otherwise. */
10913 /* Bump processing_template_decl in case this is something like
10914 template <class T> friend struct A<T>::B. */
10915 ++processing_template_decl;
10916 friend_type = tsubst (friend_type, args,
10917 tf_warning_or_error, NULL_TREE);
10918 if (dependent_type_p (friend_type))
10919 adjust_processing_template_decl = true;
10920 --processing_template_decl;
10922 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
10923 && !CLASSTYPE_USE_TEMPLATE (friend_type)
10924 && TYPE_HIDDEN_P (friend_type))
10926 /* friend class C;
10928 where C hasn't been declared yet. Let's lookup name
10929 from namespace scope directly, bypassing any name that
10930 come from dependent base class. */
10931 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
10933 /* The call to xref_tag_from_type does injection for friend
10934 classes. */
10935 push_nested_namespace (ns);
10936 friend_type =
10937 xref_tag_from_type (friend_type, NULL_TREE,
10938 /*tag_scope=*/ts_current);
10939 pop_nested_namespace (ns);
10941 else if (uses_template_parms (friend_type))
10942 /* friend class C<T>; */
10943 friend_type = tsubst (friend_type, args,
10944 tf_warning_or_error, NULL_TREE);
10945 /* Otherwise it's
10947 friend class C;
10949 where C is already declared or
10951 friend class C<int>;
10953 We don't have to do anything in these cases. */
10955 if (adjust_processing_template_decl)
10956 /* Trick make_friend_class into realizing that the friend
10957 we're adding is a template, not an ordinary class. It's
10958 important that we use make_friend_class since it will
10959 perform some error-checking and output cross-reference
10960 information. */
10961 ++processing_template_decl;
10963 if (friend_type != error_mark_node)
10964 make_friend_class (type, friend_type, /*complain=*/false);
10966 if (adjust_processing_template_decl)
10967 --processing_template_decl;
10969 else
10971 /* Build new DECL_FRIENDLIST. */
10972 tree r;
10974 /* The file and line for this declaration, to
10975 assist in error message reporting. Since we
10976 called push_tinst_level above, we don't need to
10977 restore these. */
10978 input_location = DECL_SOURCE_LOCATION (t);
10980 if (TREE_CODE (t) == TEMPLATE_DECL)
10982 ++processing_template_decl;
10983 push_deferring_access_checks (dk_no_check);
10986 r = tsubst_friend_function (t, args);
10987 add_friend (type, r, /*complain=*/false);
10988 if (TREE_CODE (t) == TEMPLATE_DECL)
10990 pop_deferring_access_checks ();
10991 --processing_template_decl;
10997 if (fn_context)
10999 /* Restore these before substituting into the lambda capture
11000 initializers. */
11001 cp_unevaluated_operand = saved_unevaluated_operand;
11002 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
11005 /* Set the file and line number information to whatever is given for
11006 the class itself. This puts error messages involving generated
11007 implicit functions at a predictable point, and the same point
11008 that would be used for non-template classes. */
11009 input_location = DECL_SOURCE_LOCATION (typedecl);
11011 unreverse_member_declarations (type);
11012 finish_struct_1 (type);
11013 TYPE_BEING_DEFINED (type) = 0;
11015 /* We don't instantiate default arguments for member functions. 14.7.1:
11017 The implicit instantiation of a class template specialization causes
11018 the implicit instantiation of the declarations, but not of the
11019 definitions or default arguments, of the class member functions,
11020 member classes, static data members and member templates.... */
11022 /* Some typedefs referenced from within the template code need to be access
11023 checked at template instantiation time, i.e now. These types were
11024 added to the template at parsing time. Let's get those and perform
11025 the access checks then. */
11026 perform_typedefs_access_check (pattern, args);
11027 perform_deferred_access_checks (tf_warning_or_error);
11028 pop_nested_class ();
11029 maximum_field_alignment = saved_maximum_field_alignment;
11030 if (!fn_context)
11031 pop_from_top_level ();
11032 pop_deferring_access_checks ();
11033 pop_tinst_level ();
11035 /* The vtable for a template class can be emitted in any translation
11036 unit in which the class is instantiated. When there is no key
11037 method, however, finish_struct_1 will already have added TYPE to
11038 the keyed_classes. */
11039 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
11040 vec_safe_push (keyed_classes, type);
11042 return type;
11045 /* Wrapper for instantiate_class_template_1. */
11047 tree
11048 instantiate_class_template (tree type)
11050 tree ret;
11051 timevar_push (TV_TEMPLATE_INST);
11052 ret = instantiate_class_template_1 (type);
11053 timevar_pop (TV_TEMPLATE_INST);
11054 return ret;
11057 static tree
11058 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11060 tree r;
11062 if (!t)
11063 r = t;
11064 else if (TYPE_P (t))
11065 r = tsubst (t, args, complain, in_decl);
11066 else
11068 if (!(complain & tf_warning))
11069 ++c_inhibit_evaluation_warnings;
11070 r = tsubst_expr (t, args, complain, in_decl,
11071 /*integral_constant_expression_p=*/true);
11072 if (!(complain & tf_warning))
11073 --c_inhibit_evaluation_warnings;
11075 return r;
11078 /* Given a function parameter pack TMPL_PARM and some function parameters
11079 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
11080 and set *SPEC_P to point at the next point in the list. */
11082 tree
11083 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
11085 /* Collect all of the extra "packed" parameters into an
11086 argument pack. */
11087 tree parmvec;
11088 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
11089 tree spec_parm = *spec_p;
11090 int i, len;
11092 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
11093 if (tmpl_parm
11094 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
11095 break;
11097 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
11098 parmvec = make_tree_vec (len);
11099 spec_parm = *spec_p;
11100 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
11102 tree elt = spec_parm;
11103 if (DECL_PACK_P (elt))
11104 elt = make_pack_expansion (elt);
11105 TREE_VEC_ELT (parmvec, i) = elt;
11108 /* Build the argument packs. */
11109 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
11110 *spec_p = spec_parm;
11112 return argpack;
11115 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
11116 NONTYPE_ARGUMENT_PACK. */
11118 static tree
11119 make_fnparm_pack (tree spec_parm)
11121 return extract_fnparm_pack (NULL_TREE, &spec_parm);
11124 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
11125 pack expansion with no extra args, 2 if it has extra args, or 0
11126 if it is not a pack expansion. */
11128 static int
11129 argument_pack_element_is_expansion_p (tree arg_pack, int i)
11131 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
11132 if (i >= TREE_VEC_LENGTH (vec))
11133 return 0;
11134 tree elt = TREE_VEC_ELT (vec, i);
11135 if (DECL_P (elt))
11136 /* A decl pack is itself an expansion. */
11137 elt = TREE_TYPE (elt);
11138 if (!PACK_EXPANSION_P (elt))
11139 return 0;
11140 if (PACK_EXPANSION_EXTRA_ARGS (elt))
11141 return 2;
11142 return 1;
11146 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11148 static tree
11149 make_argument_pack_select (tree arg_pack, unsigned index)
11151 tree aps = make_node (ARGUMENT_PACK_SELECT);
11153 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
11154 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11156 return aps;
11159 /* This is a subroutine of tsubst_pack_expansion.
11161 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11162 mechanism to store the (non complete list of) arguments of the
11163 substitution and return a non substituted pack expansion, in order
11164 to wait for when we have enough arguments to really perform the
11165 substitution. */
11167 static bool
11168 use_pack_expansion_extra_args_p (tree parm_packs,
11169 int arg_pack_len,
11170 bool has_empty_arg)
11172 /* If one pack has an expansion and another pack has a normal
11173 argument or if one pack has an empty argument and an another
11174 one hasn't then tsubst_pack_expansion cannot perform the
11175 substitution and need to fall back on the
11176 PACK_EXPANSION_EXTRA mechanism. */
11177 if (parm_packs == NULL_TREE)
11178 return false;
11179 else if (has_empty_arg)
11180 return true;
11182 bool has_expansion_arg = false;
11183 for (int i = 0 ; i < arg_pack_len; ++i)
11185 bool has_non_expansion_arg = false;
11186 for (tree parm_pack = parm_packs;
11187 parm_pack;
11188 parm_pack = TREE_CHAIN (parm_pack))
11190 tree arg = TREE_VALUE (parm_pack);
11192 int exp = argument_pack_element_is_expansion_p (arg, i);
11193 if (exp == 2)
11194 /* We can't substitute a pack expansion with extra args into
11195 our pattern. */
11196 return true;
11197 else if (exp)
11198 has_expansion_arg = true;
11199 else
11200 has_non_expansion_arg = true;
11203 if (has_expansion_arg && has_non_expansion_arg)
11204 return true;
11206 return false;
11209 /* [temp.variadic]/6 says that:
11211 The instantiation of a pack expansion [...]
11212 produces a list E1,E2, ..., En, where N is the number of elements
11213 in the pack expansion parameters.
11215 This subroutine of tsubst_pack_expansion produces one of these Ei.
11217 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11218 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11219 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11220 INDEX is the index 'i' of the element Ei to produce. ARGS,
11221 COMPLAIN, and IN_DECL are the same parameters as for the
11222 tsubst_pack_expansion function.
11224 The function returns the resulting Ei upon successful completion,
11225 or error_mark_node.
11227 Note that this function possibly modifies the ARGS parameter, so
11228 it's the responsibility of the caller to restore it. */
11230 static tree
11231 gen_elem_of_pack_expansion_instantiation (tree pattern,
11232 tree parm_packs,
11233 unsigned index,
11234 tree args /* This parm gets
11235 modified. */,
11236 tsubst_flags_t complain,
11237 tree in_decl)
11239 tree t;
11240 bool ith_elem_is_expansion = false;
11242 /* For each parameter pack, change the substitution of the parameter
11243 pack to the ith argument in its argument pack, then expand the
11244 pattern. */
11245 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
11247 tree parm = TREE_PURPOSE (pack);
11248 tree arg_pack = TREE_VALUE (pack);
11249 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
11251 ith_elem_is_expansion |=
11252 argument_pack_element_is_expansion_p (arg_pack, index);
11254 /* Select the Ith argument from the pack. */
11255 if (TREE_CODE (parm) == PARM_DECL
11256 || VAR_P (parm)
11257 || TREE_CODE (parm) == FIELD_DECL)
11259 if (index == 0)
11261 aps = make_argument_pack_select (arg_pack, index);
11262 if (!mark_used (parm, complain) && !(complain & tf_error))
11263 return error_mark_node;
11264 register_local_specialization (aps, parm);
11266 else
11267 aps = retrieve_local_specialization (parm);
11269 else
11271 int idx, level;
11272 template_parm_level_and_index (parm, &level, &idx);
11274 if (index == 0)
11276 aps = make_argument_pack_select (arg_pack, index);
11277 /* Update the corresponding argument. */
11278 TMPL_ARG (args, level, idx) = aps;
11280 else
11281 /* Re-use the ARGUMENT_PACK_SELECT. */
11282 aps = TMPL_ARG (args, level, idx);
11284 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11287 /* Substitute into the PATTERN with the (possibly altered)
11288 arguments. */
11289 if (pattern == in_decl)
11290 /* Expanding a fixed parameter pack from
11291 coerce_template_parameter_pack. */
11292 t = tsubst_decl (pattern, args, complain);
11293 else if (pattern == error_mark_node)
11294 t = error_mark_node;
11295 else if (constraint_p (pattern))
11297 if (processing_template_decl)
11298 t = tsubst_constraint (pattern, args, complain, in_decl);
11299 else
11300 t = (constraints_satisfied_p (pattern, args)
11301 ? boolean_true_node : boolean_false_node);
11303 else if (!TYPE_P (pattern))
11304 t = tsubst_expr (pattern, args, complain, in_decl,
11305 /*integral_constant_expression_p=*/false);
11306 else
11307 t = tsubst (pattern, args, complain, in_decl);
11309 /* If the Ith argument pack element is a pack expansion, then
11310 the Ith element resulting from the substituting is going to
11311 be a pack expansion as well. */
11312 if (ith_elem_is_expansion)
11313 t = make_pack_expansion (t, complain);
11315 return t;
11318 /* When the unexpanded parameter pack in a fold expression expands to an empty
11319 sequence, the value of the expression is as follows; the program is
11320 ill-formed if the operator is not listed in this table.
11322 && true
11323 || false
11324 , void() */
11326 tree
11327 expand_empty_fold (tree t, tsubst_flags_t complain)
11329 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
11330 if (!FOLD_EXPR_MODIFY_P (t))
11331 switch (code)
11333 case TRUTH_ANDIF_EXPR:
11334 return boolean_true_node;
11335 case TRUTH_ORIF_EXPR:
11336 return boolean_false_node;
11337 case COMPOUND_EXPR:
11338 return void_node;
11339 default:
11340 break;
11343 if (complain & tf_error)
11344 error_at (location_of (t),
11345 "fold of empty expansion over %O", code);
11346 return error_mark_node;
11349 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11350 form an expression that combines the two terms using the
11351 operator of T. */
11353 static tree
11354 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
11356 tree op = FOLD_EXPR_OP (t);
11357 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
11359 // Handle compound assignment operators.
11360 if (FOLD_EXPR_MODIFY_P (t))
11361 return build_x_modify_expr (input_location, left, code, right, complain);
11363 switch (code)
11365 case COMPOUND_EXPR:
11366 return build_x_compound_expr (input_location, left, right, complain);
11367 case DOTSTAR_EXPR:
11368 return build_m_component_ref (left, right, complain);
11369 default:
11370 return build_x_binary_op (input_location, code,
11371 left, TREE_CODE (left),
11372 right, TREE_CODE (right),
11373 /*overload=*/NULL,
11374 complain);
11378 /* Substitute ARGS into the pack of a fold expression T. */
11380 static inline tree
11381 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11383 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
11386 /* Substitute ARGS into the pack of a fold expression T. */
11388 static inline tree
11389 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11391 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
11394 /* Expand a PACK of arguments into a grouped as left fold.
11395 Given a pack containing elements A0, A1, ..., An and an
11396 operator @, this builds the expression:
11398 ((A0 @ A1) @ A2) ... @ An
11400 Note that PACK must not be empty.
11402 The operator is defined by the original fold expression T. */
11404 static tree
11405 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
11407 tree left = TREE_VEC_ELT (pack, 0);
11408 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
11410 tree right = TREE_VEC_ELT (pack, i);
11411 left = fold_expression (t, left, right, complain);
11413 return left;
11416 /* Substitute into a unary left fold expression. */
11418 static tree
11419 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
11420 tree in_decl)
11422 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11423 if (pack == error_mark_node)
11424 return error_mark_node;
11425 if (PACK_EXPANSION_P (pack))
11427 tree r = copy_node (t);
11428 FOLD_EXPR_PACK (r) = pack;
11429 return r;
11431 if (TREE_VEC_LENGTH (pack) == 0)
11432 return expand_empty_fold (t, complain);
11433 else
11434 return expand_left_fold (t, pack, complain);
11437 /* Substitute into a binary left fold expression.
11439 Do ths by building a single (non-empty) vector of argumnts and
11440 building the expression from those elements. */
11442 static tree
11443 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
11444 tree in_decl)
11446 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11447 if (pack == error_mark_node)
11448 return error_mark_node;
11449 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11450 if (init == error_mark_node)
11451 return error_mark_node;
11453 if (PACK_EXPANSION_P (pack))
11455 tree r = copy_node (t);
11456 FOLD_EXPR_PACK (r) = pack;
11457 FOLD_EXPR_INIT (r) = init;
11458 return r;
11461 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
11462 TREE_VEC_ELT (vec, 0) = init;
11463 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
11464 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
11466 return expand_left_fold (t, vec, complain);
11469 /* Expand a PACK of arguments into a grouped as right fold.
11470 Given a pack containing elementns A0, A1, ..., and an
11471 operator @, this builds the expression:
11473 A0@ ... (An-2 @ (An-1 @ An))
11475 Note that PACK must not be empty.
11477 The operator is defined by the original fold expression T. */
11479 tree
11480 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
11482 // Build the expression.
11483 int n = TREE_VEC_LENGTH (pack);
11484 tree right = TREE_VEC_ELT (pack, n - 1);
11485 for (--n; n != 0; --n)
11487 tree left = TREE_VEC_ELT (pack, n - 1);
11488 right = fold_expression (t, left, right, complain);
11490 return right;
11493 /* Substitute into a unary right fold expression. */
11495 static tree
11496 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
11497 tree in_decl)
11499 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11500 if (pack == error_mark_node)
11501 return error_mark_node;
11502 if (PACK_EXPANSION_P (pack))
11504 tree r = copy_node (t);
11505 FOLD_EXPR_PACK (r) = pack;
11506 return r;
11508 if (TREE_VEC_LENGTH (pack) == 0)
11509 return expand_empty_fold (t, complain);
11510 else
11511 return expand_right_fold (t, pack, complain);
11514 /* Substitute into a binary right fold expression.
11516 Do ths by building a single (non-empty) vector of arguments and
11517 building the expression from those elements. */
11519 static tree
11520 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
11521 tree in_decl)
11523 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11524 if (pack == error_mark_node)
11525 return error_mark_node;
11526 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11527 if (init == error_mark_node)
11528 return error_mark_node;
11530 if (PACK_EXPANSION_P (pack))
11532 tree r = copy_node (t);
11533 FOLD_EXPR_PACK (r) = pack;
11534 FOLD_EXPR_INIT (r) = init;
11535 return r;
11538 int n = TREE_VEC_LENGTH (pack);
11539 tree vec = make_tree_vec (n + 1);
11540 for (int i = 0; i < n; ++i)
11541 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
11542 TREE_VEC_ELT (vec, n) = init;
11544 return expand_right_fold (t, vec, complain);
11547 /* Walk through the pattern of a pack expansion, adding everything in
11548 local_specializations to a list. */
11550 struct el_data
11552 tree extra;
11553 tsubst_flags_t complain;
11555 static tree
11556 extract_locals_r (tree *tp, int */*walk_subtrees*/, void *data_)
11558 el_data &data = *reinterpret_cast<el_data*>(data_);
11559 tree *extra = &data.extra;
11560 tsubst_flags_t complain = data.complain;
11561 if (tree spec = retrieve_local_specialization (*tp))
11563 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
11565 /* Maybe pull out the PARM_DECL for a partial instantiation. */
11566 tree args = ARGUMENT_PACK_ARGS (spec);
11567 if (TREE_VEC_LENGTH (args) == 1)
11569 tree elt = TREE_VEC_ELT (args, 0);
11570 if (PACK_EXPANSION_P (elt))
11571 elt = PACK_EXPANSION_PATTERN (elt);
11572 if (DECL_PACK_P (elt))
11573 spec = elt;
11575 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
11577 /* Handle lambda capture here, since we aren't doing any
11578 substitution now, and so tsubst_copy won't call
11579 process_outer_var_ref. */
11580 tree args = ARGUMENT_PACK_ARGS (spec);
11581 int len = TREE_VEC_LENGTH (args);
11582 for (int i = 0; i < len; ++i)
11584 tree arg = TREE_VEC_ELT (args, i);
11585 tree carg = arg;
11586 if (outer_automatic_var_p (arg))
11587 carg = process_outer_var_ref (arg, complain);
11588 if (carg != arg)
11590 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
11591 proxies. */
11592 if (i == 0)
11594 spec = copy_node (spec);
11595 args = copy_node (args);
11596 SET_ARGUMENT_PACK_ARGS (spec, args);
11597 register_local_specialization (spec, *tp);
11599 TREE_VEC_ELT (args, i) = carg;
11604 if (outer_automatic_var_p (spec))
11605 spec = process_outer_var_ref (spec, complain);
11606 *extra = tree_cons (*tp, spec, *extra);
11608 return NULL_TREE;
11610 static tree
11611 extract_local_specs (tree pattern, tsubst_flags_t complain)
11613 el_data data = { NULL_TREE, complain };
11614 cp_walk_tree_without_duplicates (&pattern, extract_locals_r, &data);
11615 return data.extra;
11618 /* Substitute ARGS into T, which is an pack expansion
11619 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11620 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11621 (if only a partial substitution could be performed) or
11622 ERROR_MARK_NODE if there was an error. */
11623 tree
11624 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
11625 tree in_decl)
11627 tree pattern;
11628 tree pack, packs = NULL_TREE;
11629 bool unsubstituted_packs = false;
11630 bool unsubstituted_fn_pack = false;
11631 int i, len = -1;
11632 tree result;
11633 hash_map<tree, tree> *saved_local_specializations = NULL;
11634 bool need_local_specializations = false;
11635 int levels;
11637 gcc_assert (PACK_EXPANSION_P (t));
11638 pattern = PACK_EXPANSION_PATTERN (t);
11640 /* Add in any args remembered from an earlier partial instantiation. */
11641 tree extra = PACK_EXPANSION_EXTRA_ARGS (t);
11642 if (extra && TREE_CODE (extra) == TREE_LIST)
11644 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
11646 /* The partial instantiation involved local declarations collected in
11647 extract_local_specs; map from the general template to our local
11648 context. */
11649 tree gen = TREE_PURPOSE (elt);
11650 tree inst = TREE_VALUE (elt);
11651 if (DECL_P (inst))
11652 if (tree local = retrieve_local_specialization (inst))
11653 inst = local;
11654 /* else inst is already a full instantiation of the pack. */
11655 register_local_specialization (inst, gen);
11657 gcc_assert (!TREE_PURPOSE (extra));
11658 extra = TREE_VALUE (extra);
11660 args = add_to_template_args (extra, args);
11662 levels = TMPL_ARGS_DEPTH (args);
11664 /* Determine the argument packs that will instantiate the parameter
11665 packs used in the expansion expression. While we're at it,
11666 compute the number of arguments to be expanded and make sure it
11667 is consistent. */
11668 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
11669 pack = TREE_CHAIN (pack))
11671 tree parm_pack = TREE_VALUE (pack);
11672 tree arg_pack = NULL_TREE;
11673 tree orig_arg = NULL_TREE;
11674 int level = 0;
11676 if (TREE_CODE (parm_pack) == BASES)
11678 gcc_assert (parm_pack == pattern);
11679 if (BASES_DIRECT (parm_pack))
11680 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
11681 args, complain, in_decl, false));
11682 else
11683 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
11684 args, complain, in_decl, false));
11686 else if (builtin_pack_call_p (parm_pack))
11688 /* ??? Support use in other patterns. */
11689 gcc_assert (parm_pack == pattern);
11690 return expand_builtin_pack_call (parm_pack, args,
11691 complain, in_decl);
11693 else if (TREE_CODE (parm_pack) == PARM_DECL)
11695 /* We know we have correct local_specializations if this
11696 expansion is at function scope, or if we're dealing with a
11697 local parameter in a requires expression; for the latter,
11698 tsubst_requires_expr set it up appropriately. */
11699 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
11700 arg_pack = retrieve_local_specialization (parm_pack);
11701 else
11702 /* We can't rely on local_specializations for a parameter
11703 name used later in a function declaration (such as in a
11704 late-specified return type). Even if it exists, it might
11705 have the wrong value for a recursive call. */
11706 need_local_specializations = true;
11708 if (!arg_pack)
11710 /* This parameter pack was used in an unevaluated context. Just
11711 make a dummy decl, since it's only used for its type. */
11712 arg_pack = tsubst_decl (parm_pack, args, complain);
11713 if (arg_pack && DECL_PACK_P (arg_pack))
11714 /* Partial instantiation of the parm_pack, we can't build
11715 up an argument pack yet. */
11716 arg_pack = NULL_TREE;
11717 else
11718 arg_pack = make_fnparm_pack (arg_pack);
11720 else if (argument_pack_element_is_expansion_p (arg_pack, 0))
11721 /* This argument pack isn't fully instantiated yet. We set this
11722 flag rather than clear arg_pack because we do want to do the
11723 optimization below, and we don't want to substitute directly
11724 into the pattern (as that would expose a NONTYPE_ARGUMENT_PACK
11725 where it isn't expected). */
11726 unsubstituted_fn_pack = true;
11728 else if (is_normal_capture_proxy (parm_pack))
11730 arg_pack = retrieve_local_specialization (parm_pack);
11731 if (argument_pack_element_is_expansion_p (arg_pack, 0))
11732 unsubstituted_fn_pack = true;
11734 else
11736 int idx;
11737 template_parm_level_and_index (parm_pack, &level, &idx);
11739 if (level <= levels)
11740 arg_pack = TMPL_ARG (args, level, idx);
11743 orig_arg = arg_pack;
11744 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
11745 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
11747 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
11748 /* This can only happen if we forget to expand an argument
11749 pack somewhere else. Just return an error, silently. */
11751 result = make_tree_vec (1);
11752 TREE_VEC_ELT (result, 0) = error_mark_node;
11753 return result;
11756 if (arg_pack)
11758 int my_len =
11759 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
11761 /* Don't bother trying to do a partial substitution with
11762 incomplete packs; we'll try again after deduction. */
11763 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
11764 return t;
11766 if (len < 0)
11767 len = my_len;
11768 else if (len != my_len
11769 && !unsubstituted_fn_pack)
11771 if (!(complain & tf_error))
11772 /* Fail quietly. */;
11773 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
11774 error ("mismatched argument pack lengths while expanding %qT",
11775 pattern);
11776 else
11777 error ("mismatched argument pack lengths while expanding %qE",
11778 pattern);
11779 return error_mark_node;
11782 /* Keep track of the parameter packs and their corresponding
11783 argument packs. */
11784 packs = tree_cons (parm_pack, arg_pack, packs);
11785 TREE_TYPE (packs) = orig_arg;
11787 else
11789 /* We can't substitute for this parameter pack. We use a flag as
11790 well as the missing_level counter because function parameter
11791 packs don't have a level. */
11792 gcc_assert (processing_template_decl);
11793 unsubstituted_packs = true;
11797 /* If the expansion is just T..., return the matching argument pack, unless
11798 we need to call convert_from_reference on all the elements. This is an
11799 important optimization; see c++/68422. */
11800 if (!unsubstituted_packs
11801 && TREE_PURPOSE (packs) == pattern)
11803 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
11805 /* If the argument pack is a single pack expansion, pull it out. */
11806 if (TREE_VEC_LENGTH (args) == 1
11807 && pack_expansion_args_count (args))
11808 return TREE_VEC_ELT (args, 0);
11810 /* Types need no adjustment, nor does sizeof..., and if we still have
11811 some pack expansion args we won't do anything yet. */
11812 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
11813 || PACK_EXPANSION_SIZEOF_P (t)
11814 || pack_expansion_args_count (args))
11815 return args;
11816 /* Also optimize expression pack expansions if we can tell that the
11817 elements won't have reference type. */
11818 tree type = TREE_TYPE (pattern);
11819 if (type && TREE_CODE (type) != REFERENCE_TYPE
11820 && !PACK_EXPANSION_P (type)
11821 && !WILDCARD_TYPE_P (type))
11822 return args;
11823 /* Otherwise use the normal path so we get convert_from_reference. */
11826 /* We cannot expand this expansion expression, because we don't have
11827 all of the argument packs we need. */
11828 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
11830 /* We got some full packs, but we can't substitute them in until we
11831 have values for all the packs. So remember these until then. */
11833 t = make_pack_expansion (pattern, complain);
11834 tree extra = args;
11835 if (local_specializations)
11836 if (tree locals = extract_local_specs (pattern, complain))
11837 extra = tree_cons (NULL_TREE, extra, locals);
11838 PACK_EXPANSION_EXTRA_ARGS (t) = extra;
11839 return t;
11841 else if (unsubstituted_packs)
11843 /* There were no real arguments, we're just replacing a parameter
11844 pack with another version of itself. Substitute into the
11845 pattern and return a PACK_EXPANSION_*. The caller will need to
11846 deal with that. */
11847 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
11848 t = tsubst_expr (pattern, args, complain, in_decl,
11849 /*integral_constant_expression_p=*/false);
11850 else
11851 t = tsubst (pattern, args, complain, in_decl);
11852 t = make_pack_expansion (t, complain);
11853 return t;
11856 gcc_assert (len >= 0);
11858 if (need_local_specializations)
11860 /* We're in a late-specified return type, so create our own local
11861 specializations map; the current map is either NULL or (in the
11862 case of recursive unification) might have bindings that we don't
11863 want to use or alter. */
11864 saved_local_specializations = local_specializations;
11865 local_specializations = new hash_map<tree, tree>;
11868 /* For each argument in each argument pack, substitute into the
11869 pattern. */
11870 result = make_tree_vec (len);
11871 tree elem_args = copy_template_args (args);
11872 for (i = 0; i < len; ++i)
11874 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
11876 elem_args, complain,
11877 in_decl);
11878 TREE_VEC_ELT (result, i) = t;
11879 if (t == error_mark_node)
11881 result = error_mark_node;
11882 break;
11886 /* Update ARGS to restore the substitution from parameter packs to
11887 their argument packs. */
11888 for (pack = packs; pack; pack = TREE_CHAIN (pack))
11890 tree parm = TREE_PURPOSE (pack);
11892 if (TREE_CODE (parm) == PARM_DECL
11893 || VAR_P (parm)
11894 || TREE_CODE (parm) == FIELD_DECL)
11895 register_local_specialization (TREE_TYPE (pack), parm);
11896 else
11898 int idx, level;
11900 if (TREE_VALUE (pack) == NULL_TREE)
11901 continue;
11903 template_parm_level_and_index (parm, &level, &idx);
11905 /* Update the corresponding argument. */
11906 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
11907 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
11908 TREE_TYPE (pack);
11909 else
11910 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
11914 if (need_local_specializations)
11916 delete local_specializations;
11917 local_specializations = saved_local_specializations;
11920 /* If the dependent pack arguments were such that we end up with only a
11921 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11922 if (len == 1 && TREE_CODE (result) == TREE_VEC
11923 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
11924 return TREE_VEC_ELT (result, 0);
11926 return result;
11929 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11930 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11931 parameter packs; all parms generated from a function parameter pack will
11932 have the same DECL_PARM_INDEX. */
11934 tree
11935 get_pattern_parm (tree parm, tree tmpl)
11937 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
11938 tree patparm;
11940 if (DECL_ARTIFICIAL (parm))
11942 for (patparm = DECL_ARGUMENTS (pattern);
11943 patparm; patparm = DECL_CHAIN (patparm))
11944 if (DECL_ARTIFICIAL (patparm)
11945 && DECL_NAME (parm) == DECL_NAME (patparm))
11946 break;
11948 else
11950 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
11951 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
11952 gcc_assert (DECL_PARM_INDEX (patparm)
11953 == DECL_PARM_INDEX (parm));
11956 return patparm;
11959 /* Make an argument pack out of the TREE_VEC VEC. */
11961 static tree
11962 make_argument_pack (tree vec)
11964 tree pack;
11965 tree elt = TREE_VEC_ELT (vec, 0);
11966 if (TYPE_P (elt))
11967 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
11968 else
11970 pack = make_node (NONTYPE_ARGUMENT_PACK);
11971 TREE_CONSTANT (pack) = 1;
11973 SET_ARGUMENT_PACK_ARGS (pack, vec);
11974 return pack;
11977 /* Return an exact copy of template args T that can be modified
11978 independently. */
11980 static tree
11981 copy_template_args (tree t)
11983 if (t == error_mark_node)
11984 return t;
11986 int len = TREE_VEC_LENGTH (t);
11987 tree new_vec = make_tree_vec (len);
11989 for (int i = 0; i < len; ++i)
11991 tree elt = TREE_VEC_ELT (t, i);
11992 if (elt && TREE_CODE (elt) == TREE_VEC)
11993 elt = copy_template_args (elt);
11994 TREE_VEC_ELT (new_vec, i) = elt;
11997 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
11998 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
12000 return new_vec;
12003 /* Substitute ARGS into the vector or list of template arguments T. */
12005 static tree
12006 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12008 tree orig_t = t;
12009 int len, need_new = 0, i, expanded_len_adjust = 0, out;
12010 tree *elts;
12012 if (t == error_mark_node)
12013 return error_mark_node;
12015 len = TREE_VEC_LENGTH (t);
12016 elts = XALLOCAVEC (tree, len);
12018 for (i = 0; i < len; i++)
12020 tree orig_arg = TREE_VEC_ELT (t, i);
12021 tree new_arg;
12023 if (TREE_CODE (orig_arg) == TREE_VEC)
12024 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
12025 else if (PACK_EXPANSION_P (orig_arg))
12027 /* Substitute into an expansion expression. */
12028 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
12030 if (TREE_CODE (new_arg) == TREE_VEC)
12031 /* Add to the expanded length adjustment the number of
12032 expanded arguments. We subtract one from this
12033 measurement, because the argument pack expression
12034 itself is already counted as 1 in
12035 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
12036 the argument pack is empty. */
12037 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
12039 else if (ARGUMENT_PACK_P (orig_arg))
12041 /* Substitute into each of the arguments. */
12042 new_arg = TYPE_P (orig_arg)
12043 ? cxx_make_type (TREE_CODE (orig_arg))
12044 : make_node (TREE_CODE (orig_arg));
12046 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
12047 args, complain, in_decl);
12048 if (pack_args == error_mark_node)
12049 new_arg = error_mark_node;
12050 else
12051 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
12053 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
12054 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
12056 else
12057 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
12059 if (new_arg == error_mark_node)
12060 return error_mark_node;
12062 elts[i] = new_arg;
12063 if (new_arg != orig_arg)
12064 need_new = 1;
12067 if (!need_new)
12068 return t;
12070 /* Make space for the expanded arguments coming from template
12071 argument packs. */
12072 t = make_tree_vec (len + expanded_len_adjust);
12073 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
12074 arguments for a member template.
12075 In that case each TREE_VEC in ORIG_T represents a level of template
12076 arguments, and ORIG_T won't carry any non defaulted argument count.
12077 It will rather be the nested TREE_VECs that will carry one.
12078 In other words, ORIG_T carries a non defaulted argument count only
12079 if it doesn't contain any nested TREE_VEC. */
12080 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
12082 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
12083 count += expanded_len_adjust;
12084 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
12086 for (i = 0, out = 0; i < len; i++)
12088 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
12089 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
12090 && TREE_CODE (elts[i]) == TREE_VEC)
12092 int idx;
12094 /* Now expand the template argument pack "in place". */
12095 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
12096 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
12098 else
12100 TREE_VEC_ELT (t, out) = elts[i];
12101 out++;
12105 return t;
12108 /* Substitute ARGS into one level PARMS of template parameters. */
12110 static tree
12111 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
12113 if (parms == error_mark_node)
12114 return error_mark_node;
12116 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
12118 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
12120 tree tuple = TREE_VEC_ELT (parms, i);
12122 if (tuple == error_mark_node)
12123 continue;
12125 TREE_VEC_ELT (new_vec, i) =
12126 tsubst_template_parm (tuple, args, complain);
12129 return new_vec;
12132 /* Return the result of substituting ARGS into the template parameters
12133 given by PARMS. If there are m levels of ARGS and m + n levels of
12134 PARMS, then the result will contain n levels of PARMS. For
12135 example, if PARMS is `template <class T> template <class U>
12136 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
12137 result will be `template <int*, double, class V>'. */
12139 static tree
12140 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
12142 tree r = NULL_TREE;
12143 tree* new_parms;
12145 /* When substituting into a template, we must set
12146 PROCESSING_TEMPLATE_DECL as the template parameters may be
12147 dependent if they are based on one-another, and the dependency
12148 predicates are short-circuit outside of templates. */
12149 ++processing_template_decl;
12151 for (new_parms = &r;
12152 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
12153 new_parms = &(TREE_CHAIN (*new_parms)),
12154 parms = TREE_CHAIN (parms))
12156 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
12157 args, complain);
12158 *new_parms =
12159 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
12160 - TMPL_ARGS_DEPTH (args)),
12161 new_vec, NULL_TREE);
12164 --processing_template_decl;
12166 return r;
12169 /* Return the result of substituting ARGS into one template parameter
12170 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
12171 parameter and which TREE_PURPOSE is the default argument of the
12172 template parameter. */
12174 static tree
12175 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
12177 tree default_value, parm_decl;
12179 if (args == NULL_TREE
12180 || t == NULL_TREE
12181 || t == error_mark_node)
12182 return t;
12184 gcc_assert (TREE_CODE (t) == TREE_LIST);
12186 default_value = TREE_PURPOSE (t);
12187 parm_decl = TREE_VALUE (t);
12189 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
12190 if (TREE_CODE (parm_decl) == PARM_DECL
12191 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
12192 parm_decl = error_mark_node;
12193 default_value = tsubst_template_arg (default_value, args,
12194 complain, NULL_TREE);
12196 return build_tree_list (default_value, parm_decl);
12199 /* Substitute the ARGS into the indicated aggregate (or enumeration)
12200 type T. If T is not an aggregate or enumeration type, it is
12201 handled as if by tsubst. IN_DECL is as for tsubst. If
12202 ENTERING_SCOPE is nonzero, T is the context for a template which
12203 we are presently tsubst'ing. Return the substituted value. */
12205 static tree
12206 tsubst_aggr_type (tree t,
12207 tree args,
12208 tsubst_flags_t complain,
12209 tree in_decl,
12210 int entering_scope)
12212 if (t == NULL_TREE)
12213 return NULL_TREE;
12215 switch (TREE_CODE (t))
12217 case RECORD_TYPE:
12218 if (TYPE_PTRMEMFUNC_P (t))
12219 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
12221 /* Fall through. */
12222 case ENUMERAL_TYPE:
12223 case UNION_TYPE:
12224 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
12226 tree argvec;
12227 tree context;
12228 tree r;
12229 int saved_unevaluated_operand;
12230 int saved_inhibit_evaluation_warnings;
12232 /* In "sizeof(X<I>)" we need to evaluate "I". */
12233 saved_unevaluated_operand = cp_unevaluated_operand;
12234 cp_unevaluated_operand = 0;
12235 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
12236 c_inhibit_evaluation_warnings = 0;
12238 /* First, determine the context for the type we are looking
12239 up. */
12240 context = TYPE_CONTEXT (t);
12241 if (context && TYPE_P (context))
12243 context = tsubst_aggr_type (context, args, complain,
12244 in_decl, /*entering_scope=*/1);
12245 /* If context is a nested class inside a class template,
12246 it may still need to be instantiated (c++/33959). */
12247 context = complete_type (context);
12250 /* Then, figure out what arguments are appropriate for the
12251 type we are trying to find. For example, given:
12253 template <class T> struct S;
12254 template <class T, class U> void f(T, U) { S<U> su; }
12256 and supposing that we are instantiating f<int, double>,
12257 then our ARGS will be {int, double}, but, when looking up
12258 S we only want {double}. */
12259 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
12260 complain, in_decl);
12261 if (argvec == error_mark_node)
12262 r = error_mark_node;
12263 else
12265 r = lookup_template_class (t, argvec, in_decl, context,
12266 entering_scope, complain);
12267 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
12270 cp_unevaluated_operand = saved_unevaluated_operand;
12271 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
12273 return r;
12275 else
12276 /* This is not a template type, so there's nothing to do. */
12277 return t;
12279 default:
12280 return tsubst (t, args, complain, in_decl);
12284 static GTY((cache)) tree_cache_map *defarg_inst;
12286 /* Substitute into the default argument ARG (a default argument for
12287 FN), which has the indicated TYPE. */
12289 tree
12290 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
12291 tsubst_flags_t complain)
12293 tree saved_class_ptr = NULL_TREE;
12294 tree saved_class_ref = NULL_TREE;
12295 int errs = errorcount + sorrycount;
12297 /* This can happen in invalid code. */
12298 if (TREE_CODE (arg) == DEFAULT_ARG)
12299 return arg;
12301 tree parm = FUNCTION_FIRST_USER_PARM (fn);
12302 parm = chain_index (parmnum, parm);
12303 tree parmtype = TREE_TYPE (parm);
12304 if (DECL_BY_REFERENCE (parm))
12305 parmtype = TREE_TYPE (parmtype);
12306 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
12308 tree *slot;
12309 if (defarg_inst && (slot = defarg_inst->get (parm)))
12310 return *slot;
12312 /* This default argument came from a template. Instantiate the
12313 default argument here, not in tsubst. In the case of
12314 something like:
12316 template <class T>
12317 struct S {
12318 static T t();
12319 void f(T = t());
12322 we must be careful to do name lookup in the scope of S<T>,
12323 rather than in the current class. */
12324 push_access_scope (fn);
12325 /* The "this" pointer is not valid in a default argument. */
12326 if (cfun)
12328 saved_class_ptr = current_class_ptr;
12329 cp_function_chain->x_current_class_ptr = NULL_TREE;
12330 saved_class_ref = current_class_ref;
12331 cp_function_chain->x_current_class_ref = NULL_TREE;
12334 start_lambda_scope (parm);
12336 push_deferring_access_checks(dk_no_deferred);
12337 /* The default argument expression may cause implicitly defined
12338 member functions to be synthesized, which will result in garbage
12339 collection. We must treat this situation as if we were within
12340 the body of function so as to avoid collecting live data on the
12341 stack. */
12342 ++function_depth;
12343 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
12344 complain, NULL_TREE,
12345 /*integral_constant_expression_p=*/false);
12346 --function_depth;
12347 pop_deferring_access_checks();
12349 finish_lambda_scope ();
12351 /* Restore the "this" pointer. */
12352 if (cfun)
12354 cp_function_chain->x_current_class_ptr = saved_class_ptr;
12355 cp_function_chain->x_current_class_ref = saved_class_ref;
12358 if (errorcount+sorrycount > errs
12359 && (complain & tf_warning_or_error))
12360 inform (input_location,
12361 " when instantiating default argument for call to %qD", fn);
12363 /* Make sure the default argument is reasonable. */
12364 arg = check_default_argument (type, arg, complain);
12366 pop_access_scope (fn);
12368 if (arg != error_mark_node && !cp_unevaluated_operand)
12370 if (!defarg_inst)
12371 defarg_inst = tree_cache_map::create_ggc (37);
12372 defarg_inst->put (parm, arg);
12375 return arg;
12378 /* Substitute into all the default arguments for FN. */
12380 static void
12381 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
12383 tree arg;
12384 tree tmpl_args;
12386 tmpl_args = DECL_TI_ARGS (fn);
12388 /* If this function is not yet instantiated, we certainly don't need
12389 its default arguments. */
12390 if (uses_template_parms (tmpl_args))
12391 return;
12392 /* Don't do this again for clones. */
12393 if (DECL_CLONED_FUNCTION_P (fn))
12394 return;
12396 int i = 0;
12397 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
12398 arg;
12399 arg = TREE_CHAIN (arg), ++i)
12400 if (TREE_PURPOSE (arg))
12401 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
12402 TREE_VALUE (arg),
12403 TREE_PURPOSE (arg),
12404 complain);
12407 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12409 static tree
12410 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
12411 tree lambda_fntype)
12413 tree gen_tmpl, argvec;
12414 hashval_t hash = 0;
12415 tree in_decl = t;
12417 /* Nobody should be tsubst'ing into non-template functions. */
12418 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
12420 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
12422 /* If T is not dependent, just return it. */
12423 if (!uses_template_parms (DECL_TI_ARGS (t)))
12424 return t;
12426 /* Calculate the most general template of which R is a
12427 specialization. */
12428 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
12430 /* We're substituting a lambda function under tsubst_lambda_expr but not
12431 directly from it; find the matching function we're already inside.
12432 But don't do this if T is a generic lambda with a single level of
12433 template parms, as in that case we're doing a normal instantiation. */
12434 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
12435 && (!generic_lambda_fn_p (t)
12436 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
12437 return enclosing_instantiation_of (t);
12439 /* Calculate the complete set of arguments used to
12440 specialize R. */
12441 argvec = tsubst_template_args (DECL_TI_ARGS
12442 (DECL_TEMPLATE_RESULT
12443 (DECL_TI_TEMPLATE (t))),
12444 args, complain, in_decl);
12445 if (argvec == error_mark_node)
12446 return error_mark_node;
12448 /* Check to see if we already have this specialization. */
12449 if (!lambda_fntype)
12451 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12452 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
12453 return spec;
12456 /* We can see more levels of arguments than parameters if
12457 there was a specialization of a member template, like
12458 this:
12460 template <class T> struct S { template <class U> void f(); }
12461 template <> template <class U> void S<int>::f(U);
12463 Here, we'll be substituting into the specialization,
12464 because that's where we can find the code we actually
12465 want to generate, but we'll have enough arguments for
12466 the most general template.
12468 We also deal with the peculiar case:
12470 template <class T> struct S {
12471 template <class U> friend void f();
12473 template <class U> void f() {}
12474 template S<int>;
12475 template void f<double>();
12477 Here, the ARGS for the instantiation of will be {int,
12478 double}. But, we only need as many ARGS as there are
12479 levels of template parameters in CODE_PATTERN. We are
12480 careful not to get fooled into reducing the ARGS in
12481 situations like:
12483 template <class T> struct S { template <class U> void f(U); }
12484 template <class T> template <> void S<T>::f(int) {}
12486 which we can spot because the pattern will be a
12487 specialization in this case. */
12488 int args_depth = TMPL_ARGS_DEPTH (args);
12489 int parms_depth =
12490 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
12492 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
12493 args = get_innermost_template_args (args, parms_depth);
12495 else
12497 /* This special case arises when we have something like this:
12499 template <class T> struct S {
12500 friend void f<int>(int, double);
12503 Here, the DECL_TI_TEMPLATE for the friend declaration
12504 will be an IDENTIFIER_NODE. We are being called from
12505 tsubst_friend_function, and we want only to create a
12506 new decl (R) with appropriate types so that we can call
12507 determine_specialization. */
12508 gen_tmpl = NULL_TREE;
12509 argvec = NULL_TREE;
12512 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
12513 : NULL_TREE);
12514 tree ctx = closure ? closure : DECL_CONTEXT (t);
12515 bool member = ctx && TYPE_P (ctx);
12517 if (member && !closure)
12518 ctx = tsubst_aggr_type (ctx, args,
12519 complain, t, /*entering_scope=*/1);
12521 tree type = (lambda_fntype ? lambda_fntype
12522 : tsubst (TREE_TYPE (t), args,
12523 complain | tf_fndecl_type, in_decl));
12524 if (type == error_mark_node)
12525 return error_mark_node;
12527 /* If we hit excessive deduction depth, the type is bogus even if
12528 it isn't error_mark_node, so don't build a decl. */
12529 if (excessive_deduction_depth)
12530 return error_mark_node;
12532 /* We do NOT check for matching decls pushed separately at this
12533 point, as they may not represent instantiations of this
12534 template, and in any case are considered separate under the
12535 discrete model. */
12536 tree r = copy_decl (t);
12537 DECL_USE_TEMPLATE (r) = 0;
12538 TREE_TYPE (r) = type;
12539 /* Clear out the mangled name and RTL for the instantiation. */
12540 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12541 SET_DECL_RTL (r, NULL);
12542 /* Leave DECL_INITIAL set on deleted instantiations. */
12543 if (!DECL_DELETED_FN (r))
12544 DECL_INITIAL (r) = NULL_TREE;
12545 DECL_CONTEXT (r) = ctx;
12547 /* OpenMP UDRs have the only argument a reference to the declared
12548 type. We want to diagnose if the declared type is a reference,
12549 which is invalid, but as references to references are usually
12550 quietly merged, diagnose it here. */
12551 if (DECL_OMP_DECLARE_REDUCTION_P (t))
12553 tree argtype
12554 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
12555 argtype = tsubst (argtype, args, complain, in_decl);
12556 if (TREE_CODE (argtype) == REFERENCE_TYPE)
12557 error_at (DECL_SOURCE_LOCATION (t),
12558 "reference type %qT in "
12559 "%<#pragma omp declare reduction%>", argtype);
12560 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
12561 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
12562 argtype);
12565 if (member && DECL_CONV_FN_P (r))
12566 /* Type-conversion operator. Reconstruct the name, in
12567 case it's the name of one of the template's parameters. */
12568 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
12570 tree parms = DECL_ARGUMENTS (t);
12571 if (closure)
12572 parms = DECL_CHAIN (parms);
12573 parms = tsubst (parms, args, complain, t);
12574 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
12575 DECL_CONTEXT (parm) = r;
12576 if (closure)
12578 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
12579 DECL_CHAIN (tparm) = parms;
12580 parms = tparm;
12582 DECL_ARGUMENTS (r) = parms;
12583 DECL_RESULT (r) = NULL_TREE;
12585 TREE_STATIC (r) = 0;
12586 TREE_PUBLIC (r) = TREE_PUBLIC (t);
12587 DECL_EXTERNAL (r) = 1;
12588 /* If this is an instantiation of a function with internal
12589 linkage, we already know what object file linkage will be
12590 assigned to the instantiation. */
12591 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
12592 DECL_DEFER_OUTPUT (r) = 0;
12593 DECL_CHAIN (r) = NULL_TREE;
12594 DECL_PENDING_INLINE_INFO (r) = 0;
12595 DECL_PENDING_INLINE_P (r) = 0;
12596 DECL_SAVED_TREE (r) = NULL_TREE;
12597 DECL_STRUCT_FUNCTION (r) = NULL;
12598 TREE_USED (r) = 0;
12599 /* We'll re-clone as appropriate in instantiate_template. */
12600 DECL_CLONED_FUNCTION (r) = NULL_TREE;
12602 /* If we aren't complaining now, return on error before we register
12603 the specialization so that we'll complain eventually. */
12604 if ((complain & tf_error) == 0
12605 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12606 && !grok_op_properties (r, /*complain=*/false))
12607 return error_mark_node;
12609 /* When instantiating a constrained member, substitute
12610 into the constraints to create a new constraint. */
12611 if (tree ci = get_constraints (t))
12612 if (member)
12614 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
12615 set_constraints (r, ci);
12618 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12619 this in the special friend case mentioned above where
12620 GEN_TMPL is NULL. */
12621 if (gen_tmpl && !closure)
12623 DECL_TEMPLATE_INFO (r)
12624 = build_template_info (gen_tmpl, argvec);
12625 SET_DECL_IMPLICIT_INSTANTIATION (r);
12627 tree new_r
12628 = register_specialization (r, gen_tmpl, argvec, false, hash);
12629 if (new_r != r)
12630 /* We instantiated this while substituting into
12631 the type earlier (template/friend54.C). */
12632 return new_r;
12634 /* We're not supposed to instantiate default arguments
12635 until they are called, for a template. But, for a
12636 declaration like:
12638 template <class T> void f ()
12639 { extern void g(int i = T()); }
12641 we should do the substitution when the template is
12642 instantiated. We handle the member function case in
12643 instantiate_class_template since the default arguments
12644 might refer to other members of the class. */
12645 if (!member
12646 && !PRIMARY_TEMPLATE_P (gen_tmpl)
12647 && !uses_template_parms (argvec))
12648 tsubst_default_arguments (r, complain);
12650 else
12651 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12653 /* Copy the list of befriending classes. */
12654 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
12655 *friends;
12656 friends = &TREE_CHAIN (*friends))
12658 *friends = copy_node (*friends);
12659 TREE_VALUE (*friends)
12660 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
12663 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
12665 maybe_retrofit_in_chrg (r);
12666 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
12667 return error_mark_node;
12668 /* If this is an instantiation of a member template, clone it.
12669 If it isn't, that'll be handled by
12670 clone_constructors_and_destructors. */
12671 if (PRIMARY_TEMPLATE_P (gen_tmpl))
12672 clone_function_decl (r, /*update_methods=*/false);
12674 else if ((complain & tf_error) != 0
12675 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12676 && !grok_op_properties (r, /*complain=*/true))
12677 return error_mark_node;
12679 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
12680 SET_DECL_FRIEND_CONTEXT (r,
12681 tsubst (DECL_FRIEND_CONTEXT (t),
12682 args, complain, in_decl));
12684 /* Possibly limit visibility based on template args. */
12685 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12686 if (DECL_VISIBILITY_SPECIFIED (t))
12688 DECL_VISIBILITY_SPECIFIED (r) = 0;
12689 DECL_ATTRIBUTES (r)
12690 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12692 determine_visibility (r);
12693 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
12694 && !processing_template_decl)
12695 defaulted_late_check (r);
12697 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12698 args, complain, in_decl);
12699 return r;
12702 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
12704 static tree
12705 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
12706 tree lambda_fntype)
12708 /* We can get here when processing a member function template,
12709 member class template, or template template parameter. */
12710 tree decl = DECL_TEMPLATE_RESULT (t);
12711 tree in_decl = t;
12712 tree spec;
12713 tree tmpl_args;
12714 tree full_args;
12715 tree r;
12716 hashval_t hash = 0;
12718 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12720 /* Template template parameter is treated here. */
12721 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12722 if (new_type == error_mark_node)
12723 r = error_mark_node;
12724 /* If we get a real template back, return it. This can happen in
12725 the context of most_specialized_partial_spec. */
12726 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
12727 r = new_type;
12728 else
12729 /* The new TEMPLATE_DECL was built in
12730 reduce_template_parm_level. */
12731 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
12732 return r;
12735 if (!lambda_fntype)
12737 /* We might already have an instance of this template.
12738 The ARGS are for the surrounding class type, so the
12739 full args contain the tsubst'd args for the context,
12740 plus the innermost args from the template decl. */
12741 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
12742 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
12743 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
12744 /* Because this is a template, the arguments will still be
12745 dependent, even after substitution. If
12746 PROCESSING_TEMPLATE_DECL is not set, the dependency
12747 predicates will short-circuit. */
12748 ++processing_template_decl;
12749 full_args = tsubst_template_args (tmpl_args, args,
12750 complain, in_decl);
12751 --processing_template_decl;
12752 if (full_args == error_mark_node)
12753 return error_mark_node;
12755 /* If this is a default template template argument,
12756 tsubst might not have changed anything. */
12757 if (full_args == tmpl_args)
12758 return t;
12760 hash = hash_tmpl_and_args (t, full_args);
12761 spec = retrieve_specialization (t, full_args, hash);
12762 if (spec != NULL_TREE)
12763 return spec;
12766 /* Make a new template decl. It will be similar to the
12767 original, but will record the current template arguments.
12768 We also create a new function declaration, which is just
12769 like the old one, but points to this new template, rather
12770 than the old one. */
12771 r = copy_decl (t);
12772 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
12773 DECL_CHAIN (r) = NULL_TREE;
12775 // Build new template info linking to the original template decl.
12776 if (!lambda_fntype)
12778 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12779 SET_DECL_IMPLICIT_INSTANTIATION (r);
12781 else
12782 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12784 /* The template parameters for this new template are all the
12785 template parameters for the old template, except the
12786 outermost level of parameters. */
12787 DECL_TEMPLATE_PARMS (r)
12788 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
12789 complain);
12791 if (TREE_CODE (decl) == TYPE_DECL
12792 && !TYPE_DECL_ALIAS_P (decl))
12794 tree new_type;
12795 ++processing_template_decl;
12796 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12797 --processing_template_decl;
12798 if (new_type == error_mark_node)
12799 return error_mark_node;
12801 TREE_TYPE (r) = new_type;
12802 /* For a partial specialization, we need to keep pointing to
12803 the primary template. */
12804 if (!DECL_TEMPLATE_SPECIALIZATION (t))
12805 CLASSTYPE_TI_TEMPLATE (new_type) = r;
12806 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
12807 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
12808 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
12810 else
12812 tree new_decl;
12813 ++processing_template_decl;
12814 if (TREE_CODE (decl) == FUNCTION_DECL)
12815 new_decl = tsubst_function_decl (decl, args, complain, lambda_fntype);
12816 else
12817 new_decl = tsubst (decl, args, complain, in_decl);
12818 --processing_template_decl;
12819 if (new_decl == error_mark_node)
12820 return error_mark_node;
12822 DECL_TEMPLATE_RESULT (r) = new_decl;
12823 TREE_TYPE (r) = TREE_TYPE (new_decl);
12824 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
12825 if (lambda_fntype)
12827 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
12828 DECL_TEMPLATE_INFO (new_decl) = build_template_info (r, args);
12830 else
12832 DECL_TI_TEMPLATE (new_decl) = r;
12833 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
12837 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
12838 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
12840 if (PRIMARY_TEMPLATE_P (t))
12841 DECL_PRIMARY_TEMPLATE (r) = r;
12843 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
12844 && !lambda_fntype)
12845 /* Record this non-type partial instantiation. */
12846 register_specialization (r, t,
12847 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
12848 false, hash);
12850 return r;
12853 /* True if FN is the op() for a lambda in an uninstantiated template. */
12855 bool
12856 lambda_fn_in_template_p (tree fn)
12858 if (!fn || !LAMBDA_FUNCTION_P (fn))
12859 return false;
12860 tree closure = DECL_CONTEXT (fn);
12861 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
12864 /* We're instantiating a variable from template function TCTX. Return the
12865 corresponding current enclosing scope. This gets complicated because lambda
12866 functions in templates are regenerated rather than instantiated, but generic
12867 lambda functions are subsequently instantiated. */
12869 static tree
12870 enclosing_instantiation_of (tree otctx)
12872 tree tctx = otctx;
12873 tree fn = current_function_decl;
12874 int lambda_count = 0;
12876 for (; tctx && lambda_fn_in_template_p (tctx);
12877 tctx = decl_function_context (tctx))
12878 ++lambda_count;
12879 for (; fn; fn = decl_function_context (fn))
12881 tree ofn = fn;
12882 int flambda_count = 0;
12883 for (; flambda_count < lambda_count && fn && LAMBDA_FUNCTION_P (fn);
12884 fn = decl_function_context (fn))
12885 ++flambda_count;
12886 if (DECL_TEMPLATE_INFO (fn)
12887 ? most_general_template (fn) != most_general_template (tctx)
12888 : fn != tctx)
12889 continue;
12890 gcc_assert (DECL_NAME (ofn) == DECL_NAME (otctx)
12891 || DECL_CONV_FN_P (ofn));
12892 return ofn;
12894 gcc_unreachable ();
12897 /* Substitute the ARGS into the T, which is a _DECL. Return the
12898 result of the substitution. Issue error and warning messages under
12899 control of COMPLAIN. */
12901 static tree
12902 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
12904 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12905 location_t saved_loc;
12906 tree r = NULL_TREE;
12907 tree in_decl = t;
12908 hashval_t hash = 0;
12910 /* Set the filename and linenumber to improve error-reporting. */
12911 saved_loc = input_location;
12912 input_location = DECL_SOURCE_LOCATION (t);
12914 switch (TREE_CODE (t))
12916 case TEMPLATE_DECL:
12917 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
12918 break;
12920 case FUNCTION_DECL:
12921 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
12922 break;
12924 case PARM_DECL:
12926 tree type = NULL_TREE;
12927 int i, len = 1;
12928 tree expanded_types = NULL_TREE;
12929 tree prev_r = NULL_TREE;
12930 tree first_r = NULL_TREE;
12932 if (DECL_PACK_P (t))
12934 /* If there is a local specialization that isn't a
12935 parameter pack, it means that we're doing a "simple"
12936 substitution from inside tsubst_pack_expansion. Just
12937 return the local specialization (which will be a single
12938 parm). */
12939 tree spec = retrieve_local_specialization (t);
12940 if (spec
12941 && TREE_CODE (spec) == PARM_DECL
12942 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
12943 RETURN (spec);
12945 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12946 the parameters in this function parameter pack. */
12947 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12948 complain, in_decl);
12949 if (TREE_CODE (expanded_types) == TREE_VEC)
12951 len = TREE_VEC_LENGTH (expanded_types);
12953 /* Zero-length parameter packs are boring. Just substitute
12954 into the chain. */
12955 if (len == 0)
12956 RETURN (tsubst (TREE_CHAIN (t), args, complain,
12957 TREE_CHAIN (t)));
12959 else
12961 /* All we did was update the type. Make a note of that. */
12962 type = expanded_types;
12963 expanded_types = NULL_TREE;
12967 /* Loop through all of the parameters we'll build. When T is
12968 a function parameter pack, LEN is the number of expanded
12969 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12970 r = NULL_TREE;
12971 for (i = 0; i < len; ++i)
12973 prev_r = r;
12974 r = copy_node (t);
12975 if (DECL_TEMPLATE_PARM_P (t))
12976 SET_DECL_TEMPLATE_PARM_P (r);
12978 if (expanded_types)
12979 /* We're on the Ith parameter of the function parameter
12980 pack. */
12982 /* Get the Ith type. */
12983 type = TREE_VEC_ELT (expanded_types, i);
12985 /* Rename the parameter to include the index. */
12986 DECL_NAME (r)
12987 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12989 else if (!type)
12990 /* We're dealing with a normal parameter. */
12991 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12993 type = type_decays_to (type);
12994 TREE_TYPE (r) = type;
12995 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12997 if (DECL_INITIAL (r))
12999 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
13000 DECL_INITIAL (r) = TREE_TYPE (r);
13001 else
13002 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
13003 complain, in_decl);
13006 DECL_CONTEXT (r) = NULL_TREE;
13008 if (!DECL_TEMPLATE_PARM_P (r))
13009 DECL_ARG_TYPE (r) = type_passed_as (type);
13011 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13012 args, complain, in_decl);
13014 /* Keep track of the first new parameter we
13015 generate. That's what will be returned to the
13016 caller. */
13017 if (!first_r)
13018 first_r = r;
13020 /* Build a proper chain of parameters when substituting
13021 into a function parameter pack. */
13022 if (prev_r)
13023 DECL_CHAIN (prev_r) = r;
13026 /* If cp_unevaluated_operand is set, we're just looking for a
13027 single dummy parameter, so don't keep going. */
13028 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
13029 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
13030 complain, DECL_CHAIN (t));
13032 /* FIRST_R contains the start of the chain we've built. */
13033 r = first_r;
13035 break;
13037 case FIELD_DECL:
13039 tree type = NULL_TREE;
13040 tree vec = NULL_TREE;
13041 tree expanded_types = NULL_TREE;
13042 int len = 1;
13044 if (PACK_EXPANSION_P (TREE_TYPE (t)))
13046 /* This field is a lambda capture pack. Return a TREE_VEC of
13047 the expanded fields to instantiate_class_template_1. */
13048 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
13049 complain, in_decl);
13050 if (TREE_CODE (expanded_types) == TREE_VEC)
13052 len = TREE_VEC_LENGTH (expanded_types);
13053 vec = make_tree_vec (len);
13055 else
13057 /* All we did was update the type. Make a note of that. */
13058 type = expanded_types;
13059 expanded_types = NULL_TREE;
13063 for (int i = 0; i < len; ++i)
13065 r = copy_decl (t);
13066 if (expanded_types)
13068 type = TREE_VEC_ELT (expanded_types, i);
13069 DECL_NAME (r)
13070 = make_ith_pack_parameter_name (DECL_NAME (r), i);
13072 else if (!type)
13073 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13075 if (type == error_mark_node)
13076 RETURN (error_mark_node);
13077 TREE_TYPE (r) = type;
13078 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13080 if (DECL_C_BIT_FIELD (r))
13081 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
13082 number of bits. */
13083 DECL_BIT_FIELD_REPRESENTATIVE (r)
13084 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
13085 complain, in_decl,
13086 /*integral_constant_expression_p=*/true);
13087 if (DECL_INITIAL (t))
13089 /* Set up DECL_TEMPLATE_INFO so that we can get at the
13090 NSDMI in perform_member_init. Still set DECL_INITIAL
13091 so that we know there is one. */
13092 DECL_INITIAL (r) = void_node;
13093 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
13094 retrofit_lang_decl (r);
13095 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
13097 /* We don't have to set DECL_CONTEXT here; it is set by
13098 finish_member_declaration. */
13099 DECL_CHAIN (r) = NULL_TREE;
13101 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13102 args, complain, in_decl);
13104 if (vec)
13105 TREE_VEC_ELT (vec, i) = r;
13108 if (vec)
13109 r = vec;
13111 break;
13113 case USING_DECL:
13114 /* We reach here only for member using decls. We also need to check
13115 uses_template_parms because DECL_DEPENDENT_P is not set for a
13116 using-declaration that designates a member of the current
13117 instantiation (c++/53549). */
13118 if (DECL_DEPENDENT_P (t)
13119 || uses_template_parms (USING_DECL_SCOPE (t)))
13121 tree scope = USING_DECL_SCOPE (t);
13122 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
13123 if (PACK_EXPANSION_P (scope))
13125 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
13126 int len = TREE_VEC_LENGTH (vec);
13127 r = make_tree_vec (len);
13128 for (int i = 0; i < len; ++i)
13130 tree escope = TREE_VEC_ELT (vec, i);
13131 tree elt = do_class_using_decl (escope, name);
13132 if (!elt)
13134 r = error_mark_node;
13135 break;
13137 else
13139 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
13140 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
13142 TREE_VEC_ELT (r, i) = elt;
13145 else
13147 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
13148 complain, in_decl);
13149 r = do_class_using_decl (inst_scope, name);
13150 if (!r)
13151 r = error_mark_node;
13152 else
13154 TREE_PROTECTED (r) = TREE_PROTECTED (t);
13155 TREE_PRIVATE (r) = TREE_PRIVATE (t);
13159 else
13161 r = copy_node (t);
13162 DECL_CHAIN (r) = NULL_TREE;
13164 break;
13166 case TYPE_DECL:
13167 case VAR_DECL:
13169 tree argvec = NULL_TREE;
13170 tree gen_tmpl = NULL_TREE;
13171 tree spec;
13172 tree tmpl = NULL_TREE;
13173 tree ctx;
13174 tree type = NULL_TREE;
13175 bool local_p;
13177 if (TREE_TYPE (t) == error_mark_node)
13178 RETURN (error_mark_node);
13180 if (TREE_CODE (t) == TYPE_DECL
13181 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
13183 /* If this is the canonical decl, we don't have to
13184 mess with instantiations, and often we can't (for
13185 typename, template type parms and such). Note that
13186 TYPE_NAME is not correct for the above test if
13187 we've copied the type for a typedef. */
13188 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13189 if (type == error_mark_node)
13190 RETURN (error_mark_node);
13191 r = TYPE_NAME (type);
13192 break;
13195 /* Check to see if we already have the specialization we
13196 need. */
13197 spec = NULL_TREE;
13198 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
13200 /* T is a static data member or namespace-scope entity.
13201 We have to substitute into namespace-scope variables
13202 (not just variable templates) because of cases like:
13204 template <class T> void f() { extern T t; }
13206 where the entity referenced is not known until
13207 instantiation time. */
13208 local_p = false;
13209 ctx = DECL_CONTEXT (t);
13210 if (DECL_CLASS_SCOPE_P (t))
13212 ctx = tsubst_aggr_type (ctx, args,
13213 complain,
13214 in_decl, /*entering_scope=*/1);
13215 /* If CTX is unchanged, then T is in fact the
13216 specialization we want. That situation occurs when
13217 referencing a static data member within in its own
13218 class. We can use pointer equality, rather than
13219 same_type_p, because DECL_CONTEXT is always
13220 canonical... */
13221 if (ctx == DECL_CONTEXT (t)
13222 /* ... unless T is a member template; in which
13223 case our caller can be willing to create a
13224 specialization of that template represented
13225 by T. */
13226 && !(DECL_TI_TEMPLATE (t)
13227 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
13228 spec = t;
13231 if (!spec)
13233 tmpl = DECL_TI_TEMPLATE (t);
13234 gen_tmpl = most_general_template (tmpl);
13235 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
13236 if (argvec != error_mark_node)
13237 argvec = (coerce_innermost_template_parms
13238 (DECL_TEMPLATE_PARMS (gen_tmpl),
13239 argvec, t, complain,
13240 /*all*/true, /*defarg*/true));
13241 if (argvec == error_mark_node)
13242 RETURN (error_mark_node);
13243 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13244 spec = retrieve_specialization (gen_tmpl, argvec, hash);
13247 else
13249 /* A local variable. */
13250 local_p = true;
13251 /* Subsequent calls to pushdecl will fill this in. */
13252 ctx = NULL_TREE;
13253 /* Unless this is a reference to a static variable from an
13254 enclosing function, in which case we need to fill it in now. */
13255 if (TREE_STATIC (t))
13257 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
13258 if (fn != current_function_decl)
13259 ctx = fn;
13261 spec = retrieve_local_specialization (t);
13263 /* If we already have the specialization we need, there is
13264 nothing more to do. */
13265 if (spec)
13267 r = spec;
13268 break;
13271 /* Create a new node for the specialization we need. */
13272 r = copy_decl (t);
13273 if (type == NULL_TREE)
13275 if (is_typedef_decl (t))
13276 type = DECL_ORIGINAL_TYPE (t);
13277 else
13278 type = TREE_TYPE (t);
13279 if (VAR_P (t)
13280 && VAR_HAD_UNKNOWN_BOUND (t)
13281 && type != error_mark_node)
13282 type = strip_array_domain (type);
13283 tree sub_args = args;
13284 if (tree auto_node = type_uses_auto (type))
13286 /* Mask off any template args past the variable's context so we
13287 don't replace the auto with an unrelated argument. */
13288 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
13289 int extra = TMPL_ARGS_DEPTH (args) - nouter;
13290 if (extra > 0)
13291 /* This should never happen with the new lambda instantiation
13292 model, but keep the handling just in case. */
13293 gcc_assert (!CHECKING_P),
13294 sub_args = strip_innermost_template_args (args, extra);
13296 type = tsubst (type, sub_args, complain, in_decl);
13298 if (VAR_P (r))
13300 /* Even if the original location is out of scope, the
13301 newly substituted one is not. */
13302 DECL_DEAD_FOR_LOCAL (r) = 0;
13303 DECL_INITIALIZED_P (r) = 0;
13304 DECL_TEMPLATE_INSTANTIATED (r) = 0;
13305 if (type == error_mark_node)
13306 RETURN (error_mark_node);
13307 if (TREE_CODE (type) == FUNCTION_TYPE)
13309 /* It may seem that this case cannot occur, since:
13311 typedef void f();
13312 void g() { f x; }
13314 declares a function, not a variable. However:
13316 typedef void f();
13317 template <typename T> void g() { T t; }
13318 template void g<f>();
13320 is an attempt to declare a variable with function
13321 type. */
13322 error ("variable %qD has function type",
13323 /* R is not yet sufficiently initialized, so we
13324 just use its name. */
13325 DECL_NAME (r));
13326 RETURN (error_mark_node);
13328 type = complete_type (type);
13329 /* Wait until cp_finish_decl to set this again, to handle
13330 circular dependency (template/instantiate6.C). */
13331 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
13332 type = check_var_type (DECL_NAME (r), type);
13334 if (DECL_HAS_VALUE_EXPR_P (t))
13336 tree ve = DECL_VALUE_EXPR (t);
13337 ve = tsubst_expr (ve, args, complain, in_decl,
13338 /*constant_expression_p=*/false);
13339 if (REFERENCE_REF_P (ve))
13341 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
13342 ve = TREE_OPERAND (ve, 0);
13344 SET_DECL_VALUE_EXPR (r, ve);
13346 if (CP_DECL_THREAD_LOCAL_P (r)
13347 && !processing_template_decl)
13348 set_decl_tls_model (r, decl_default_tls_model (r));
13350 else if (DECL_SELF_REFERENCE_P (t))
13351 SET_DECL_SELF_REFERENCE_P (r);
13352 TREE_TYPE (r) = type;
13353 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13354 DECL_CONTEXT (r) = ctx;
13355 /* Clear out the mangled name and RTL for the instantiation. */
13356 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13357 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
13358 SET_DECL_RTL (r, NULL);
13359 /* The initializer must not be expanded until it is required;
13360 see [temp.inst]. */
13361 DECL_INITIAL (r) = NULL_TREE;
13362 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
13363 if (VAR_P (r))
13365 if (DECL_LANG_SPECIFIC (r))
13366 SET_DECL_DEPENDENT_INIT_P (r, false);
13368 SET_DECL_MODE (r, VOIDmode);
13370 /* Possibly limit visibility based on template args. */
13371 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13372 if (DECL_VISIBILITY_SPECIFIED (t))
13374 DECL_VISIBILITY_SPECIFIED (r) = 0;
13375 DECL_ATTRIBUTES (r)
13376 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13378 determine_visibility (r);
13381 if (!local_p)
13383 /* A static data member declaration is always marked
13384 external when it is declared in-class, even if an
13385 initializer is present. We mimic the non-template
13386 processing here. */
13387 DECL_EXTERNAL (r) = 1;
13388 if (DECL_NAMESPACE_SCOPE_P (t))
13389 DECL_NOT_REALLY_EXTERN (r) = 1;
13391 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
13392 SET_DECL_IMPLICIT_INSTANTIATION (r);
13393 register_specialization (r, gen_tmpl, argvec, false, hash);
13395 else
13397 if (DECL_LANG_SPECIFIC (r))
13398 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13399 if (!cp_unevaluated_operand)
13400 register_local_specialization (r, t);
13403 DECL_CHAIN (r) = NULL_TREE;
13405 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
13406 /*flags=*/0,
13407 args, complain, in_decl);
13409 /* Preserve a typedef that names a type. */
13410 if (is_typedef_decl (r) && type != error_mark_node)
13412 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
13413 set_underlying_type (r);
13414 if (TYPE_DECL_ALIAS_P (r))
13415 /* An alias template specialization can be dependent
13416 even if its underlying type is not. */
13417 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
13420 layout_decl (r, 0);
13422 break;
13424 default:
13425 gcc_unreachable ();
13427 #undef RETURN
13429 out:
13430 /* Restore the file and line information. */
13431 input_location = saved_loc;
13433 return r;
13436 /* Substitute into the ARG_TYPES of a function type.
13437 If END is a TREE_CHAIN, leave it and any following types
13438 un-substituted. */
13440 static tree
13441 tsubst_arg_types (tree arg_types,
13442 tree args,
13443 tree end,
13444 tsubst_flags_t complain,
13445 tree in_decl)
13447 tree remaining_arg_types;
13448 tree type = NULL_TREE;
13449 int i = 1;
13450 tree expanded_args = NULL_TREE;
13451 tree default_arg;
13453 if (!arg_types || arg_types == void_list_node || arg_types == end)
13454 return arg_types;
13456 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
13457 args, end, complain, in_decl);
13458 if (remaining_arg_types == error_mark_node)
13459 return error_mark_node;
13461 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
13463 /* For a pack expansion, perform substitution on the
13464 entire expression. Later on, we'll handle the arguments
13465 one-by-one. */
13466 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
13467 args, complain, in_decl);
13469 if (TREE_CODE (expanded_args) == TREE_VEC)
13470 /* So that we'll spin through the parameters, one by one. */
13471 i = TREE_VEC_LENGTH (expanded_args);
13472 else
13474 /* We only partially substituted into the parameter
13475 pack. Our type is TYPE_PACK_EXPANSION. */
13476 type = expanded_args;
13477 expanded_args = NULL_TREE;
13481 while (i > 0) {
13482 --i;
13484 if (expanded_args)
13485 type = TREE_VEC_ELT (expanded_args, i);
13486 else if (!type)
13487 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
13489 if (type == error_mark_node)
13490 return error_mark_node;
13491 if (VOID_TYPE_P (type))
13493 if (complain & tf_error)
13495 error ("invalid parameter type %qT", type);
13496 if (in_decl)
13497 error ("in declaration %q+D", in_decl);
13499 return error_mark_node;
13501 /* DR 657. */
13502 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
13503 return error_mark_node;
13505 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13506 top-level qualifiers as required. */
13507 type = cv_unqualified (type_decays_to (type));
13509 /* We do not substitute into default arguments here. The standard
13510 mandates that they be instantiated only when needed, which is
13511 done in build_over_call. */
13512 default_arg = TREE_PURPOSE (arg_types);
13514 /* Except that we do substitute default arguments under tsubst_lambda_expr,
13515 since the new op() won't have any associated template arguments for us
13516 to refer to later. */
13517 if (lambda_fn_in_template_p (in_decl))
13518 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
13519 false/*fn*/, false/*constexpr*/);
13521 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
13523 /* We've instantiated a template before its default arguments
13524 have been parsed. This can happen for a nested template
13525 class, and is not an error unless we require the default
13526 argument in a call of this function. */
13527 remaining_arg_types =
13528 tree_cons (default_arg, type, remaining_arg_types);
13529 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
13531 else
13532 remaining_arg_types =
13533 hash_tree_cons (default_arg, type, remaining_arg_types);
13536 return remaining_arg_types;
13539 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13540 *not* handle the exception-specification for FNTYPE, because the
13541 initial substitution of explicitly provided template parameters
13542 during argument deduction forbids substitution into the
13543 exception-specification:
13545 [temp.deduct]
13547 All references in the function type of the function template to the
13548 corresponding template parameters are replaced by the specified tem-
13549 plate argument values. If a substitution in a template parameter or
13550 in the function type of the function template results in an invalid
13551 type, type deduction fails. [Note: The equivalent substitution in
13552 exception specifications is done only when the function is instanti-
13553 ated, at which point a program is ill-formed if the substitution
13554 results in an invalid type.] */
13556 static tree
13557 tsubst_function_type (tree t,
13558 tree args,
13559 tsubst_flags_t complain,
13560 tree in_decl)
13562 tree return_type;
13563 tree arg_types = NULL_TREE;
13564 tree fntype;
13566 /* The TYPE_CONTEXT is not used for function/method types. */
13567 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
13569 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13570 failure. */
13571 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13573 if (late_return_type_p)
13575 /* Substitute the argument types. */
13576 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13577 complain, in_decl);
13578 if (arg_types == error_mark_node)
13579 return error_mark_node;
13581 tree save_ccp = current_class_ptr;
13582 tree save_ccr = current_class_ref;
13583 tree this_type = (TREE_CODE (t) == METHOD_TYPE
13584 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
13585 bool do_inject = this_type && CLASS_TYPE_P (this_type);
13586 if (do_inject)
13588 /* DR 1207: 'this' is in scope in the trailing return type. */
13589 inject_this_parameter (this_type, cp_type_quals (this_type));
13592 /* Substitute the return type. */
13593 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13595 if (do_inject)
13597 current_class_ptr = save_ccp;
13598 current_class_ref = save_ccr;
13601 else
13602 /* Substitute the return type. */
13603 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13605 if (return_type == error_mark_node)
13606 return error_mark_node;
13607 /* DR 486 clarifies that creation of a function type with an
13608 invalid return type is a deduction failure. */
13609 if (TREE_CODE (return_type) == ARRAY_TYPE
13610 || TREE_CODE (return_type) == FUNCTION_TYPE)
13612 if (complain & tf_error)
13614 if (TREE_CODE (return_type) == ARRAY_TYPE)
13615 error ("function returning an array");
13616 else
13617 error ("function returning a function");
13619 return error_mark_node;
13621 /* And DR 657. */
13622 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
13623 return error_mark_node;
13625 if (!late_return_type_p)
13627 /* Substitute the argument types. */
13628 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13629 complain, in_decl);
13630 if (arg_types == error_mark_node)
13631 return error_mark_node;
13634 /* Construct a new type node and return it. */
13635 if (TREE_CODE (t) == FUNCTION_TYPE)
13637 fntype = build_function_type (return_type, arg_types);
13638 fntype = apply_memfn_quals (fntype,
13639 type_memfn_quals (t),
13640 type_memfn_rqual (t));
13642 else
13644 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13645 /* Don't pick up extra function qualifiers from the basetype. */
13646 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13647 if (! MAYBE_CLASS_TYPE_P (r))
13649 /* [temp.deduct]
13651 Type deduction may fail for any of the following
13652 reasons:
13654 -- Attempting to create "pointer to member of T" when T
13655 is not a class type. */
13656 if (complain & tf_error)
13657 error ("creating pointer to member function of non-class type %qT",
13659 return error_mark_node;
13662 fntype = build_method_type_directly (r, return_type,
13663 TREE_CHAIN (arg_types));
13664 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
13666 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
13668 if (late_return_type_p)
13669 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
13671 return fntype;
13674 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
13675 ARGS into that specification, and return the substituted
13676 specification. If there is no specification, return NULL_TREE. */
13678 static tree
13679 tsubst_exception_specification (tree fntype,
13680 tree args,
13681 tsubst_flags_t complain,
13682 tree in_decl,
13683 bool defer_ok)
13685 tree specs;
13686 tree new_specs;
13688 specs = TYPE_RAISES_EXCEPTIONS (fntype);
13689 new_specs = NULL_TREE;
13690 if (specs && TREE_PURPOSE (specs))
13692 /* A noexcept-specifier. */
13693 tree expr = TREE_PURPOSE (specs);
13694 if (TREE_CODE (expr) == INTEGER_CST)
13695 new_specs = expr;
13696 else if (defer_ok)
13698 /* Defer instantiation of noexcept-specifiers to avoid
13699 excessive instantiations (c++/49107). */
13700 new_specs = make_node (DEFERRED_NOEXCEPT);
13701 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
13703 /* We already partially instantiated this member template,
13704 so combine the new args with the old. */
13705 DEFERRED_NOEXCEPT_PATTERN (new_specs)
13706 = DEFERRED_NOEXCEPT_PATTERN (expr);
13707 DEFERRED_NOEXCEPT_ARGS (new_specs)
13708 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
13710 else
13712 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
13713 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
13716 else
13717 new_specs = tsubst_copy_and_build
13718 (expr, args, complain, in_decl, /*function_p=*/false,
13719 /*integral_constant_expression_p=*/true);
13720 new_specs = build_noexcept_spec (new_specs, complain);
13722 else if (specs)
13724 if (! TREE_VALUE (specs))
13725 new_specs = specs;
13726 else
13727 while (specs)
13729 tree spec;
13730 int i, len = 1;
13731 tree expanded_specs = NULL_TREE;
13733 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
13735 /* Expand the pack expansion type. */
13736 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
13737 args, complain,
13738 in_decl);
13740 if (expanded_specs == error_mark_node)
13741 return error_mark_node;
13742 else if (TREE_CODE (expanded_specs) == TREE_VEC)
13743 len = TREE_VEC_LENGTH (expanded_specs);
13744 else
13746 /* We're substituting into a member template, so
13747 we got a TYPE_PACK_EXPANSION back. Add that
13748 expansion and move on. */
13749 gcc_assert (TREE_CODE (expanded_specs)
13750 == TYPE_PACK_EXPANSION);
13751 new_specs = add_exception_specifier (new_specs,
13752 expanded_specs,
13753 complain);
13754 specs = TREE_CHAIN (specs);
13755 continue;
13759 for (i = 0; i < len; ++i)
13761 if (expanded_specs)
13762 spec = TREE_VEC_ELT (expanded_specs, i);
13763 else
13764 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
13765 if (spec == error_mark_node)
13766 return spec;
13767 new_specs = add_exception_specifier (new_specs, spec,
13768 complain);
13771 specs = TREE_CHAIN (specs);
13774 return new_specs;
13777 /* Take the tree structure T and replace template parameters used
13778 therein with the argument vector ARGS. IN_DECL is an associated
13779 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
13780 Issue error and warning messages under control of COMPLAIN. Note
13781 that we must be relatively non-tolerant of extensions here, in
13782 order to preserve conformance; if we allow substitutions that
13783 should not be allowed, we may allow argument deductions that should
13784 not succeed, and therefore report ambiguous overload situations
13785 where there are none. In theory, we could allow the substitution,
13786 but indicate that it should have failed, and allow our caller to
13787 make sure that the right thing happens, but we don't try to do this
13788 yet.
13790 This function is used for dealing with types, decls and the like;
13791 for expressions, use tsubst_expr or tsubst_copy. */
13793 tree
13794 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13796 enum tree_code code;
13797 tree type, r = NULL_TREE;
13799 if (t == NULL_TREE || t == error_mark_node
13800 || t == integer_type_node
13801 || t == void_type_node
13802 || t == char_type_node
13803 || t == unknown_type_node
13804 || TREE_CODE (t) == NAMESPACE_DECL
13805 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
13806 return t;
13808 if (DECL_P (t))
13809 return tsubst_decl (t, args, complain);
13811 if (args == NULL_TREE)
13812 return t;
13814 code = TREE_CODE (t);
13816 if (code == IDENTIFIER_NODE)
13817 type = IDENTIFIER_TYPE_VALUE (t);
13818 else
13819 type = TREE_TYPE (t);
13821 gcc_assert (type != unknown_type_node);
13823 /* Reuse typedefs. We need to do this to handle dependent attributes,
13824 such as attribute aligned. */
13825 if (TYPE_P (t)
13826 && typedef_variant_p (t))
13828 tree decl = TYPE_NAME (t);
13830 if (alias_template_specialization_p (t))
13832 /* DECL represents an alias template and we want to
13833 instantiate it. */
13834 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13835 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13836 r = instantiate_alias_template (tmpl, gen_args, complain);
13838 else if (DECL_CLASS_SCOPE_P (decl)
13839 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
13840 && uses_template_parms (DECL_CONTEXT (decl)))
13842 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13843 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13844 r = retrieve_specialization (tmpl, gen_args, 0);
13846 else if (DECL_FUNCTION_SCOPE_P (decl)
13847 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
13848 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
13849 r = retrieve_local_specialization (decl);
13850 else
13851 /* The typedef is from a non-template context. */
13852 return t;
13854 if (r)
13856 r = TREE_TYPE (r);
13857 r = cp_build_qualified_type_real
13858 (r, cp_type_quals (t) | cp_type_quals (r),
13859 complain | tf_ignore_bad_quals);
13860 return r;
13862 else
13864 /* We don't have an instantiation yet, so drop the typedef. */
13865 int quals = cp_type_quals (t);
13866 t = DECL_ORIGINAL_TYPE (decl);
13867 t = cp_build_qualified_type_real (t, quals,
13868 complain | tf_ignore_bad_quals);
13872 bool fndecl_type = (complain & tf_fndecl_type);
13873 complain &= ~tf_fndecl_type;
13875 if (type
13876 && code != TYPENAME_TYPE
13877 && code != TEMPLATE_TYPE_PARM
13878 && code != TEMPLATE_PARM_INDEX
13879 && code != IDENTIFIER_NODE
13880 && code != FUNCTION_TYPE
13881 && code != METHOD_TYPE)
13882 type = tsubst (type, args, complain, in_decl);
13883 if (type == error_mark_node)
13884 return error_mark_node;
13886 switch (code)
13888 case RECORD_TYPE:
13889 case UNION_TYPE:
13890 case ENUMERAL_TYPE:
13891 return tsubst_aggr_type (t, args, complain, in_decl,
13892 /*entering_scope=*/0);
13894 case ERROR_MARK:
13895 case IDENTIFIER_NODE:
13896 case VOID_TYPE:
13897 case REAL_TYPE:
13898 case COMPLEX_TYPE:
13899 case VECTOR_TYPE:
13900 case BOOLEAN_TYPE:
13901 case NULLPTR_TYPE:
13902 case LANG_TYPE:
13903 return t;
13905 case INTEGER_TYPE:
13906 if (t == integer_type_node)
13907 return t;
13909 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
13910 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
13911 return t;
13914 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
13916 max = tsubst_expr (omax, args, complain, in_decl,
13917 /*integral_constant_expression_p=*/false);
13919 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13920 needed. */
13921 if (TREE_CODE (max) == NOP_EXPR
13922 && TREE_SIDE_EFFECTS (omax)
13923 && !TREE_TYPE (max))
13924 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
13926 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13927 with TREE_SIDE_EFFECTS that indicates this is not an integral
13928 constant expression. */
13929 if (processing_template_decl
13930 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
13932 gcc_assert (TREE_CODE (max) == NOP_EXPR);
13933 TREE_SIDE_EFFECTS (max) = 1;
13936 return compute_array_index_type (NULL_TREE, max, complain);
13939 case TEMPLATE_TYPE_PARM:
13940 case TEMPLATE_TEMPLATE_PARM:
13941 case BOUND_TEMPLATE_TEMPLATE_PARM:
13942 case TEMPLATE_PARM_INDEX:
13944 int idx;
13945 int level;
13946 int levels;
13947 tree arg = NULL_TREE;
13949 /* Early in template argument deduction substitution, we don't
13950 want to reduce the level of 'auto', or it will be confused
13951 with a normal template parm in subsequent deduction. */
13952 if (is_auto (t) && (complain & tf_partial))
13953 return t;
13955 r = NULL_TREE;
13957 gcc_assert (TREE_VEC_LENGTH (args) > 0);
13958 template_parm_level_and_index (t, &level, &idx);
13960 levels = TMPL_ARGS_DEPTH (args);
13961 if (level <= levels
13962 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
13964 arg = TMPL_ARG (args, level, idx);
13966 /* See through ARGUMENT_PACK_SELECT arguments. */
13967 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
13968 arg = argument_pack_select_arg (arg);
13971 if (arg == error_mark_node)
13972 return error_mark_node;
13973 else if (arg != NULL_TREE)
13975 if (ARGUMENT_PACK_P (arg))
13976 /* If ARG is an argument pack, we don't actually want to
13977 perform a substitution here, because substitutions
13978 for argument packs are only done
13979 element-by-element. We can get to this point when
13980 substituting the type of a non-type template
13981 parameter pack, when that type actually contains
13982 template parameter packs from an outer template, e.g.,
13984 template<typename... Types> struct A {
13985 template<Types... Values> struct B { };
13986 }; */
13987 return t;
13989 if (code == TEMPLATE_TYPE_PARM)
13991 int quals;
13992 gcc_assert (TYPE_P (arg));
13994 quals = cp_type_quals (arg) | cp_type_quals (t);
13996 return cp_build_qualified_type_real
13997 (arg, quals, complain | tf_ignore_bad_quals);
13999 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
14001 /* We are processing a type constructed from a
14002 template template parameter. */
14003 tree argvec = tsubst (TYPE_TI_ARGS (t),
14004 args, complain, in_decl);
14005 if (argvec == error_mark_node)
14006 return error_mark_node;
14008 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
14009 || TREE_CODE (arg) == TEMPLATE_DECL
14010 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
14012 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
14013 /* Consider this code:
14015 template <template <class> class Template>
14016 struct Internal {
14017 template <class Arg> using Bind = Template<Arg>;
14020 template <template <class> class Template, class Arg>
14021 using Instantiate = Template<Arg>; //#0
14023 template <template <class> class Template,
14024 class Argument>
14025 using Bind =
14026 Instantiate<Internal<Template>::template Bind,
14027 Argument>; //#1
14029 When #1 is parsed, the
14030 BOUND_TEMPLATE_TEMPLATE_PARM representing the
14031 parameter `Template' in #0 matches the
14032 UNBOUND_CLASS_TEMPLATE representing the argument
14033 `Internal<Template>::template Bind'; We then want
14034 to assemble the type `Bind<Argument>' that can't
14035 be fully created right now, because
14036 `Internal<Template>' not being complete, the Bind
14037 template cannot be looked up in that context. So
14038 we need to "store" `Bind<Argument>' for later
14039 when the context of Bind becomes complete. Let's
14040 store that in a TYPENAME_TYPE. */
14041 return make_typename_type (TYPE_CONTEXT (arg),
14042 build_nt (TEMPLATE_ID_EXPR,
14043 TYPE_IDENTIFIER (arg),
14044 argvec),
14045 typename_type,
14046 complain);
14048 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
14049 are resolving nested-types in the signature of a
14050 member function templates. Otherwise ARG is a
14051 TEMPLATE_DECL and is the real template to be
14052 instantiated. */
14053 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
14054 arg = TYPE_NAME (arg);
14056 r = lookup_template_class (arg,
14057 argvec, in_decl,
14058 DECL_CONTEXT (arg),
14059 /*entering_scope=*/0,
14060 complain);
14061 return cp_build_qualified_type_real
14062 (r, cp_type_quals (t) | cp_type_quals (r), complain);
14064 else if (code == TEMPLATE_TEMPLATE_PARM)
14065 return arg;
14066 else
14067 /* TEMPLATE_PARM_INDEX. */
14068 return convert_from_reference (unshare_expr (arg));
14071 if (level == 1)
14072 /* This can happen during the attempted tsubst'ing in
14073 unify. This means that we don't yet have any information
14074 about the template parameter in question. */
14075 return t;
14077 /* If we get here, we must have been looking at a parm for a
14078 more deeply nested template. Make a new version of this
14079 template parameter, but with a lower level. */
14080 switch (code)
14082 case TEMPLATE_TYPE_PARM:
14083 case TEMPLATE_TEMPLATE_PARM:
14084 case BOUND_TEMPLATE_TEMPLATE_PARM:
14085 if (cp_type_quals (t))
14087 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
14088 r = cp_build_qualified_type_real
14089 (r, cp_type_quals (t),
14090 complain | (code == TEMPLATE_TYPE_PARM
14091 ? tf_ignore_bad_quals : 0));
14093 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
14094 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
14095 && (r = (TEMPLATE_PARM_DESCENDANTS
14096 (TEMPLATE_TYPE_PARM_INDEX (t))))
14097 && (r = TREE_TYPE (r))
14098 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
14099 /* Break infinite recursion when substituting the constraints
14100 of a constrained placeholder. */;
14101 else
14103 r = copy_type (t);
14104 TEMPLATE_TYPE_PARM_INDEX (r)
14105 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
14106 r, levels, args, complain);
14107 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
14108 TYPE_MAIN_VARIANT (r) = r;
14109 TYPE_POINTER_TO (r) = NULL_TREE;
14110 TYPE_REFERENCE_TO (r) = NULL_TREE;
14112 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
14114 /* Propagate constraints on placeholders. */
14115 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
14116 PLACEHOLDER_TYPE_CONSTRAINTS (r)
14117 = tsubst_constraint (constr, args, complain, in_decl);
14118 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
14120 if (DECL_TEMPLATE_TEMPLATE_PARM_P (pl))
14121 pl = tsubst (pl, args, complain, in_decl);
14122 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
14126 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
14127 /* We have reduced the level of the template
14128 template parameter, but not the levels of its
14129 template parameters, so canonical_type_parameter
14130 will not be able to find the canonical template
14131 template parameter for this level. Thus, we
14132 require structural equality checking to compare
14133 TEMPLATE_TEMPLATE_PARMs. */
14134 SET_TYPE_STRUCTURAL_EQUALITY (r);
14135 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
14136 SET_TYPE_STRUCTURAL_EQUALITY (r);
14137 else
14138 TYPE_CANONICAL (r) = canonical_type_parameter (r);
14140 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
14142 tree tinfo = TYPE_TEMPLATE_INFO (t);
14143 /* We might need to substitute into the types of non-type
14144 template parameters. */
14145 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
14146 complain, in_decl);
14147 if (tmpl == error_mark_node)
14148 return error_mark_node;
14149 tree argvec = tsubst (TI_ARGS (tinfo), args,
14150 complain, in_decl);
14151 if (argvec == error_mark_node)
14152 return error_mark_node;
14154 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
14155 = build_template_info (tmpl, argvec);
14158 break;
14160 case TEMPLATE_PARM_INDEX:
14161 /* OK, now substitute the type of the non-type parameter. We
14162 couldn't do it earlier because it might be an auto parameter,
14163 and we wouldn't need to if we had an argument. */
14164 type = tsubst (type, args, complain, in_decl);
14165 if (type == error_mark_node)
14166 return error_mark_node;
14167 r = reduce_template_parm_level (t, type, levels, args, complain);
14168 break;
14170 default:
14171 gcc_unreachable ();
14174 return r;
14177 case TREE_LIST:
14179 tree purpose, value, chain;
14181 if (t == void_list_node)
14182 return t;
14184 purpose = TREE_PURPOSE (t);
14185 if (purpose)
14187 purpose = tsubst (purpose, args, complain, in_decl);
14188 if (purpose == error_mark_node)
14189 return error_mark_node;
14191 value = TREE_VALUE (t);
14192 if (value)
14194 value = tsubst (value, args, complain, in_decl);
14195 if (value == error_mark_node)
14196 return error_mark_node;
14198 chain = TREE_CHAIN (t);
14199 if (chain && chain != void_type_node)
14201 chain = tsubst (chain, args, complain, in_decl);
14202 if (chain == error_mark_node)
14203 return error_mark_node;
14205 if (purpose == TREE_PURPOSE (t)
14206 && value == TREE_VALUE (t)
14207 && chain == TREE_CHAIN (t))
14208 return t;
14209 return hash_tree_cons (purpose, value, chain);
14212 case TREE_BINFO:
14213 /* We should never be tsubsting a binfo. */
14214 gcc_unreachable ();
14216 case TREE_VEC:
14217 /* A vector of template arguments. */
14218 gcc_assert (!type);
14219 return tsubst_template_args (t, args, complain, in_decl);
14221 case POINTER_TYPE:
14222 case REFERENCE_TYPE:
14224 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
14225 return t;
14227 /* [temp.deduct]
14229 Type deduction may fail for any of the following
14230 reasons:
14232 -- Attempting to create a pointer to reference type.
14233 -- Attempting to create a reference to a reference type or
14234 a reference to void.
14236 Core issue 106 says that creating a reference to a reference
14237 during instantiation is no longer a cause for failure. We
14238 only enforce this check in strict C++98 mode. */
14239 if ((TREE_CODE (type) == REFERENCE_TYPE
14240 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
14241 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
14243 static location_t last_loc;
14245 /* We keep track of the last time we issued this error
14246 message to avoid spewing a ton of messages during a
14247 single bad template instantiation. */
14248 if (complain & tf_error
14249 && last_loc != input_location)
14251 if (VOID_TYPE_P (type))
14252 error ("forming reference to void");
14253 else if (code == POINTER_TYPE)
14254 error ("forming pointer to reference type %qT", type);
14255 else
14256 error ("forming reference to reference type %qT", type);
14257 last_loc = input_location;
14260 return error_mark_node;
14262 else if (TREE_CODE (type) == FUNCTION_TYPE
14263 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
14264 || type_memfn_rqual (type) != REF_QUAL_NONE))
14266 if (complain & tf_error)
14268 if (code == POINTER_TYPE)
14269 error ("forming pointer to qualified function type %qT",
14270 type);
14271 else
14272 error ("forming reference to qualified function type %qT",
14273 type);
14275 return error_mark_node;
14277 else if (code == POINTER_TYPE)
14279 r = build_pointer_type (type);
14280 if (TREE_CODE (type) == METHOD_TYPE)
14281 r = build_ptrmemfunc_type (r);
14283 else if (TREE_CODE (type) == REFERENCE_TYPE)
14284 /* In C++0x, during template argument substitution, when there is an
14285 attempt to create a reference to a reference type, reference
14286 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14288 "If a template-argument for a template-parameter T names a type
14289 that is a reference to a type A, an attempt to create the type
14290 'lvalue reference to cv T' creates the type 'lvalue reference to
14291 A,' while an attempt to create the type type rvalue reference to
14292 cv T' creates the type T"
14294 r = cp_build_reference_type
14295 (TREE_TYPE (type),
14296 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
14297 else
14298 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
14299 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
14301 if (r != error_mark_node)
14302 /* Will this ever be needed for TYPE_..._TO values? */
14303 layout_type (r);
14305 return r;
14307 case OFFSET_TYPE:
14309 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
14310 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
14312 /* [temp.deduct]
14314 Type deduction may fail for any of the following
14315 reasons:
14317 -- Attempting to create "pointer to member of T" when T
14318 is not a class type. */
14319 if (complain & tf_error)
14320 error ("creating pointer to member of non-class type %qT", r);
14321 return error_mark_node;
14323 if (TREE_CODE (type) == REFERENCE_TYPE)
14325 if (complain & tf_error)
14326 error ("creating pointer to member reference type %qT", type);
14327 return error_mark_node;
14329 if (VOID_TYPE_P (type))
14331 if (complain & tf_error)
14332 error ("creating pointer to member of type void");
14333 return error_mark_node;
14335 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
14336 if (TREE_CODE (type) == FUNCTION_TYPE)
14338 /* The type of the implicit object parameter gets its
14339 cv-qualifiers from the FUNCTION_TYPE. */
14340 tree memptr;
14341 tree method_type
14342 = build_memfn_type (type, r, type_memfn_quals (type),
14343 type_memfn_rqual (type));
14344 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
14345 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
14346 complain);
14348 else
14349 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
14350 cp_type_quals (t),
14351 complain);
14353 case FUNCTION_TYPE:
14354 case METHOD_TYPE:
14356 tree fntype;
14357 tree specs;
14358 fntype = tsubst_function_type (t, args, complain, in_decl);
14359 if (fntype == error_mark_node)
14360 return error_mark_node;
14362 /* Substitute the exception specification. */
14363 specs = tsubst_exception_specification (t, args, complain, in_decl,
14364 /*defer_ok*/fndecl_type);
14365 if (specs == error_mark_node)
14366 return error_mark_node;
14367 if (specs)
14368 fntype = build_exception_variant (fntype, specs);
14369 return fntype;
14371 case ARRAY_TYPE:
14373 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
14374 if (domain == error_mark_node)
14375 return error_mark_node;
14377 /* As an optimization, we avoid regenerating the array type if
14378 it will obviously be the same as T. */
14379 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
14380 return t;
14382 /* These checks should match the ones in create_array_type_for_decl.
14384 [temp.deduct]
14386 The deduction may fail for any of the following reasons:
14388 -- Attempting to create an array with an element type that
14389 is void, a function type, or a reference type, or [DR337]
14390 an abstract class type. */
14391 if (VOID_TYPE_P (type)
14392 || TREE_CODE (type) == FUNCTION_TYPE
14393 || (TREE_CODE (type) == ARRAY_TYPE
14394 && TYPE_DOMAIN (type) == NULL_TREE)
14395 || TREE_CODE (type) == REFERENCE_TYPE)
14397 if (complain & tf_error)
14398 error ("creating array of %qT", type);
14399 return error_mark_node;
14402 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
14403 return error_mark_node;
14405 r = build_cplus_array_type (type, domain);
14407 if (TYPE_USER_ALIGN (t))
14409 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
14410 TYPE_USER_ALIGN (r) = 1;
14413 return r;
14416 case TYPENAME_TYPE:
14418 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14419 in_decl, /*entering_scope=*/1);
14420 if (ctx == error_mark_node)
14421 return error_mark_node;
14423 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
14424 complain, in_decl);
14425 if (f == error_mark_node)
14426 return error_mark_node;
14428 if (!MAYBE_CLASS_TYPE_P (ctx))
14430 if (complain & tf_error)
14431 error ("%qT is not a class, struct, or union type", ctx);
14432 return error_mark_node;
14434 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
14436 /* Normally, make_typename_type does not require that the CTX
14437 have complete type in order to allow things like:
14439 template <class T> struct S { typename S<T>::X Y; };
14441 But, such constructs have already been resolved by this
14442 point, so here CTX really should have complete type, unless
14443 it's a partial instantiation. */
14444 ctx = complete_type (ctx);
14445 if (!COMPLETE_TYPE_P (ctx))
14447 if (complain & tf_error)
14448 cxx_incomplete_type_error (NULL_TREE, ctx);
14449 return error_mark_node;
14453 f = make_typename_type (ctx, f, typename_type,
14454 complain | tf_keep_type_decl);
14455 if (f == error_mark_node)
14456 return f;
14457 if (TREE_CODE (f) == TYPE_DECL)
14459 complain |= tf_ignore_bad_quals;
14460 f = TREE_TYPE (f);
14463 if (TREE_CODE (f) != TYPENAME_TYPE)
14465 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
14467 if (complain & tf_error)
14468 error ("%qT resolves to %qT, which is not an enumeration type",
14469 t, f);
14470 else
14471 return error_mark_node;
14473 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
14475 if (complain & tf_error)
14476 error ("%qT resolves to %qT, which is is not a class type",
14477 t, f);
14478 else
14479 return error_mark_node;
14483 return cp_build_qualified_type_real
14484 (f, cp_type_quals (f) | cp_type_quals (t), complain);
14487 case UNBOUND_CLASS_TEMPLATE:
14489 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14490 in_decl, /*entering_scope=*/1);
14491 tree name = TYPE_IDENTIFIER (t);
14492 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
14494 if (ctx == error_mark_node || name == error_mark_node)
14495 return error_mark_node;
14497 if (parm_list)
14498 parm_list = tsubst_template_parms (parm_list, args, complain);
14499 return make_unbound_class_template (ctx, name, parm_list, complain);
14502 case TYPEOF_TYPE:
14504 tree type;
14506 ++cp_unevaluated_operand;
14507 ++c_inhibit_evaluation_warnings;
14509 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
14510 complain, in_decl,
14511 /*integral_constant_expression_p=*/false);
14513 --cp_unevaluated_operand;
14514 --c_inhibit_evaluation_warnings;
14516 type = finish_typeof (type);
14517 return cp_build_qualified_type_real (type,
14518 cp_type_quals (t)
14519 | cp_type_quals (type),
14520 complain);
14523 case DECLTYPE_TYPE:
14525 tree type;
14527 ++cp_unevaluated_operand;
14528 ++c_inhibit_evaluation_warnings;
14530 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
14531 complain|tf_decltype, in_decl,
14532 /*function_p*/false,
14533 /*integral_constant_expression*/false);
14535 if (DECLTYPE_FOR_INIT_CAPTURE (t))
14537 if (type == NULL_TREE)
14539 if (complain & tf_error)
14540 error ("empty initializer in lambda init-capture");
14541 type = error_mark_node;
14543 else if (TREE_CODE (type) == TREE_LIST)
14544 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
14547 --cp_unevaluated_operand;
14548 --c_inhibit_evaluation_warnings;
14550 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
14551 type = lambda_capture_field_type (type,
14552 DECLTYPE_FOR_INIT_CAPTURE (t),
14553 DECLTYPE_FOR_REF_CAPTURE (t));
14554 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
14555 type = lambda_proxy_type (type);
14556 else
14558 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
14559 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
14560 && EXPR_P (type))
14561 /* In a template ~id could be either a complement expression
14562 or an unqualified-id naming a destructor; if instantiating
14563 it produces an expression, it's not an id-expression or
14564 member access. */
14565 id = false;
14566 type = finish_decltype_type (type, id, complain);
14568 return cp_build_qualified_type_real (type,
14569 cp_type_quals (t)
14570 | cp_type_quals (type),
14571 complain | tf_ignore_bad_quals);
14574 case UNDERLYING_TYPE:
14576 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
14577 complain, in_decl);
14578 return finish_underlying_type (type);
14581 case TYPE_ARGUMENT_PACK:
14582 case NONTYPE_ARGUMENT_PACK:
14584 tree r;
14586 if (code == NONTYPE_ARGUMENT_PACK)
14587 r = make_node (code);
14588 else
14589 r = cxx_make_type (code);
14591 tree pack_args = ARGUMENT_PACK_ARGS (t);
14592 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
14593 SET_ARGUMENT_PACK_ARGS (r, pack_args);
14595 return r;
14598 case VOID_CST:
14599 case INTEGER_CST:
14600 case REAL_CST:
14601 case STRING_CST:
14602 case PLUS_EXPR:
14603 case MINUS_EXPR:
14604 case NEGATE_EXPR:
14605 case NOP_EXPR:
14606 case INDIRECT_REF:
14607 case ADDR_EXPR:
14608 case CALL_EXPR:
14609 case ARRAY_REF:
14610 case SCOPE_REF:
14611 /* We should use one of the expression tsubsts for these codes. */
14612 gcc_unreachable ();
14614 default:
14615 sorry ("use of %qs in template", get_tree_code_name (code));
14616 return error_mark_node;
14620 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
14621 expression on the left-hand side of the "." or "->" operator. We
14622 only do the lookup if we had a dependent BASELINK. Otherwise we
14623 adjust it onto the instantiated heirarchy. */
14625 static tree
14626 tsubst_baselink (tree baselink, tree object_type,
14627 tree args, tsubst_flags_t complain, tree in_decl)
14629 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
14630 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
14631 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
14633 tree optype = BASELINK_OPTYPE (baselink);
14634 optype = tsubst (optype, args, complain, in_decl);
14636 tree template_args = NULL_TREE;
14637 bool template_id_p = false;
14638 tree fns = BASELINK_FUNCTIONS (baselink);
14639 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
14641 template_id_p = true;
14642 template_args = TREE_OPERAND (fns, 1);
14643 fns = TREE_OPERAND (fns, 0);
14644 if (template_args)
14645 template_args = tsubst_template_args (template_args, args,
14646 complain, in_decl);
14649 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
14650 binfo_type = tsubst (binfo_type, args, complain, in_decl);
14651 bool dependent_p = binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink));
14653 if (dependent_p)
14655 tree name = OVL_NAME (fns);
14656 if (IDENTIFIER_CONV_OP_P (name))
14657 name = make_conv_op_name (optype);
14659 if (name == complete_dtor_identifier)
14660 /* Treat as-if non-dependent below. */
14661 dependent_p = false;
14663 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
14664 if (!baselink)
14666 if ((complain & tf_error)
14667 && constructor_name_p (name, qualifying_scope))
14668 error ("cannot call constructor %<%T::%D%> directly",
14669 qualifying_scope, name);
14670 return error_mark_node;
14673 if (BASELINK_P (baselink))
14674 fns = BASELINK_FUNCTIONS (baselink);
14676 else
14677 /* We're going to overwrite pieces below, make a duplicate. */
14678 baselink = copy_node (baselink);
14680 /* If lookup found a single function, mark it as used at this point.
14681 (If lookup found multiple functions the one selected later by
14682 overload resolution will be marked as used at that point.) */
14683 if (!template_id_p && !really_overloaded_fn (fns)
14684 && !mark_used (OVL_FIRST (fns), complain) && !(complain & tf_error))
14685 return error_mark_node;
14687 if (BASELINK_P (baselink))
14689 /* Add back the template arguments, if present. */
14690 if (template_id_p)
14691 BASELINK_FUNCTIONS (baselink)
14692 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
14694 /* Update the conversion operator type. */
14695 BASELINK_OPTYPE (baselink) = optype;
14698 if (!object_type)
14699 object_type = current_class_type;
14701 if (qualified_p || !dependent_p)
14703 baselink = adjust_result_of_qualified_name_lookup (baselink,
14704 qualifying_scope,
14705 object_type);
14706 if (!qualified_p)
14707 /* We need to call adjust_result_of_qualified_name_lookup in case the
14708 destructor names a base class, but we unset BASELINK_QUALIFIED_P
14709 so that we still get virtual function binding. */
14710 BASELINK_QUALIFIED_P (baselink) = false;
14713 return baselink;
14716 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
14717 true if the qualified-id will be a postfix-expression in-and-of
14718 itself; false if more of the postfix-expression follows the
14719 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
14720 of "&". */
14722 static tree
14723 tsubst_qualified_id (tree qualified_id, tree args,
14724 tsubst_flags_t complain, tree in_decl,
14725 bool done, bool address_p)
14727 tree expr;
14728 tree scope;
14729 tree name;
14730 bool is_template;
14731 tree template_args;
14732 location_t loc = UNKNOWN_LOCATION;
14734 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
14736 /* Figure out what name to look up. */
14737 name = TREE_OPERAND (qualified_id, 1);
14738 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14740 is_template = true;
14741 loc = EXPR_LOCATION (name);
14742 template_args = TREE_OPERAND (name, 1);
14743 if (template_args)
14744 template_args = tsubst_template_args (template_args, args,
14745 complain, in_decl);
14746 if (template_args == error_mark_node)
14747 return error_mark_node;
14748 name = TREE_OPERAND (name, 0);
14750 else
14752 is_template = false;
14753 template_args = NULL_TREE;
14756 /* Substitute into the qualifying scope. When there are no ARGS, we
14757 are just trying to simplify a non-dependent expression. In that
14758 case the qualifying scope may be dependent, and, in any case,
14759 substituting will not help. */
14760 scope = TREE_OPERAND (qualified_id, 0);
14761 if (args)
14763 scope = tsubst (scope, args, complain, in_decl);
14764 expr = tsubst_copy (name, args, complain, in_decl);
14766 else
14767 expr = name;
14769 if (dependent_scope_p (scope))
14771 if (is_template)
14772 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
14773 tree r = build_qualified_name (NULL_TREE, scope, expr,
14774 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
14775 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
14776 return r;
14779 if (!BASELINK_P (name) && !DECL_P (expr))
14781 if (TREE_CODE (expr) == BIT_NOT_EXPR)
14783 /* A BIT_NOT_EXPR is used to represent a destructor. */
14784 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
14786 error ("qualifying type %qT does not match destructor name ~%qT",
14787 scope, TREE_OPERAND (expr, 0));
14788 expr = error_mark_node;
14790 else
14791 expr = lookup_qualified_name (scope, complete_dtor_identifier,
14792 /*is_type_p=*/0, false);
14794 else
14795 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
14796 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
14797 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
14799 if (complain & tf_error)
14801 error ("dependent-name %qE is parsed as a non-type, but "
14802 "instantiation yields a type", qualified_id);
14803 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
14805 return error_mark_node;
14809 if (DECL_P (expr))
14811 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
14812 scope);
14813 /* Remember that there was a reference to this entity. */
14814 if (!mark_used (expr, complain) && !(complain & tf_error))
14815 return error_mark_node;
14818 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
14820 if (complain & tf_error)
14821 qualified_name_lookup_error (scope,
14822 TREE_OPERAND (qualified_id, 1),
14823 expr, input_location);
14824 return error_mark_node;
14827 if (is_template)
14829 if (variable_template_p (expr))
14830 expr = lookup_and_finish_template_variable (expr, template_args,
14831 complain);
14832 else
14833 expr = lookup_template_function (expr, template_args);
14836 if (expr == error_mark_node && complain & tf_error)
14837 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
14838 expr, input_location);
14839 else if (TYPE_P (scope))
14841 expr = (adjust_result_of_qualified_name_lookup
14842 (expr, scope, current_nonlambda_class_type ()));
14843 expr = (finish_qualified_id_expr
14844 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
14845 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
14846 /*template_arg_p=*/false, complain));
14849 /* Expressions do not generally have reference type. */
14850 if (TREE_CODE (expr) != SCOPE_REF
14851 /* However, if we're about to form a pointer-to-member, we just
14852 want the referenced member referenced. */
14853 && TREE_CODE (expr) != OFFSET_REF)
14854 expr = convert_from_reference (expr);
14856 if (REF_PARENTHESIZED_P (qualified_id))
14857 expr = force_paren_expr (expr);
14859 return expr;
14862 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14863 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14864 for tsubst. */
14866 static tree
14867 tsubst_init (tree init, tree decl, tree args,
14868 tsubst_flags_t complain, tree in_decl)
14870 if (!init)
14871 return NULL_TREE;
14873 init = tsubst_expr (init, args, complain, in_decl, false);
14875 if (!init && TREE_TYPE (decl) != error_mark_node)
14877 /* If we had an initializer but it
14878 instantiated to nothing,
14879 value-initialize the object. This will
14880 only occur when the initializer was a
14881 pack expansion where the parameter packs
14882 used in that expansion were of length
14883 zero. */
14884 init = build_value_init (TREE_TYPE (decl),
14885 complain);
14886 if (TREE_CODE (init) == AGGR_INIT_EXPR)
14887 init = get_target_expr_sfinae (init, complain);
14888 if (TREE_CODE (init) == TARGET_EXPR)
14889 TARGET_EXPR_DIRECT_INIT_P (init) = true;
14892 return init;
14895 /* Like tsubst, but deals with expressions. This function just replaces
14896 template parms; to finish processing the resultant expression, use
14897 tsubst_copy_and_build or tsubst_expr. */
14899 static tree
14900 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14902 enum tree_code code;
14903 tree r;
14905 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
14906 return t;
14908 code = TREE_CODE (t);
14910 switch (code)
14912 case PARM_DECL:
14913 r = retrieve_local_specialization (t);
14915 if (r == NULL_TREE)
14917 /* We get here for a use of 'this' in an NSDMI. */
14918 if (DECL_NAME (t) == this_identifier && current_class_ptr)
14919 return current_class_ptr;
14921 /* This can happen for a parameter name used later in a function
14922 declaration (such as in a late-specified return type). Just
14923 make a dummy decl, since it's only used for its type. */
14924 gcc_assert (cp_unevaluated_operand != 0);
14925 r = tsubst_decl (t, args, complain);
14926 /* Give it the template pattern as its context; its true context
14927 hasn't been instantiated yet and this is good enough for
14928 mangling. */
14929 DECL_CONTEXT (r) = DECL_CONTEXT (t);
14932 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14933 r = argument_pack_select_arg (r);
14934 if (!mark_used (r, complain) && !(complain & tf_error))
14935 return error_mark_node;
14936 return r;
14938 case CONST_DECL:
14940 tree enum_type;
14941 tree v;
14943 if (DECL_TEMPLATE_PARM_P (t))
14944 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
14945 /* There is no need to substitute into namespace-scope
14946 enumerators. */
14947 if (DECL_NAMESPACE_SCOPE_P (t))
14948 return t;
14949 /* If ARGS is NULL, then T is known to be non-dependent. */
14950 if (args == NULL_TREE)
14951 return scalar_constant_value (t);
14953 /* Unfortunately, we cannot just call lookup_name here.
14954 Consider:
14956 template <int I> int f() {
14957 enum E { a = I };
14958 struct S { void g() { E e = a; } };
14961 When we instantiate f<7>::S::g(), say, lookup_name is not
14962 clever enough to find f<7>::a. */
14963 enum_type
14964 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14965 /*entering_scope=*/0);
14967 for (v = TYPE_VALUES (enum_type);
14968 v != NULL_TREE;
14969 v = TREE_CHAIN (v))
14970 if (TREE_PURPOSE (v) == DECL_NAME (t))
14971 return TREE_VALUE (v);
14973 /* We didn't find the name. That should never happen; if
14974 name-lookup found it during preliminary parsing, we
14975 should find it again here during instantiation. */
14976 gcc_unreachable ();
14978 return t;
14980 case FIELD_DECL:
14981 if (DECL_CONTEXT (t))
14983 tree ctx;
14985 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14986 /*entering_scope=*/1);
14987 if (ctx != DECL_CONTEXT (t))
14989 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
14990 if (!r)
14992 if (complain & tf_error)
14993 error ("using invalid field %qD", t);
14994 return error_mark_node;
14996 return r;
15000 return t;
15002 case VAR_DECL:
15003 case FUNCTION_DECL:
15004 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
15005 r = tsubst (t, args, complain, in_decl);
15006 else if (local_variable_p (t)
15007 && uses_template_parms (DECL_CONTEXT (t)))
15009 r = retrieve_local_specialization (t);
15010 if (r == NULL_TREE)
15012 /* First try name lookup to find the instantiation. */
15013 r = lookup_name (DECL_NAME (t));
15014 if (r && !is_capture_proxy (r))
15016 /* Make sure that the one we found is the one we want. */
15017 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
15018 if (ctx != DECL_CONTEXT (r))
15019 r = NULL_TREE;
15022 if (r)
15023 /* OK */;
15024 else
15026 /* This can happen for a variable used in a
15027 late-specified return type of a local lambda, or for a
15028 local static or constant. Building a new VAR_DECL
15029 should be OK in all those cases. */
15030 r = tsubst_decl (t, args, complain);
15031 if (local_specializations)
15032 /* Avoid infinite recursion (79640). */
15033 register_local_specialization (r, t);
15034 if (decl_maybe_constant_var_p (r))
15036 /* We can't call cp_finish_decl, so handle the
15037 initializer by hand. */
15038 tree init = tsubst_init (DECL_INITIAL (t), r, args,
15039 complain, in_decl);
15040 if (!processing_template_decl)
15041 init = maybe_constant_init (init);
15042 if (processing_template_decl
15043 ? potential_constant_expression (init)
15044 : reduced_constant_expression_p (init))
15045 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
15046 = TREE_CONSTANT (r) = true;
15047 DECL_INITIAL (r) = init;
15048 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
15049 TREE_TYPE (r)
15050 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
15051 complain, adc_variable_type);
15053 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
15054 || decl_constant_var_p (r)
15055 || errorcount || sorrycount);
15056 if (!processing_template_decl
15057 && !TREE_STATIC (r))
15058 r = process_outer_var_ref (r, complain);
15060 /* Remember this for subsequent uses. */
15061 if (local_specializations)
15062 register_local_specialization (r, t);
15064 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
15065 r = argument_pack_select_arg (r);
15067 else
15068 r = t;
15069 if (!mark_used (r, complain))
15070 return error_mark_node;
15071 return r;
15073 case NAMESPACE_DECL:
15074 return t;
15076 case OVERLOAD:
15077 /* An OVERLOAD will always be a non-dependent overload set; an
15078 overload set from function scope will just be represented with an
15079 IDENTIFIER_NODE, and from class scope with a BASELINK. */
15080 gcc_assert (!uses_template_parms (t));
15081 /* We must have marked any lookups as persistent. */
15082 gcc_assert (!OVL_LOOKUP_P (t) || OVL_USED_P (t));
15083 return t;
15085 case BASELINK:
15086 return tsubst_baselink (t, current_nonlambda_class_type (),
15087 args, complain, in_decl);
15089 case TEMPLATE_DECL:
15090 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
15091 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
15092 args, complain, in_decl);
15093 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
15094 return tsubst (t, args, complain, in_decl);
15095 else if (DECL_CLASS_SCOPE_P (t)
15096 && uses_template_parms (DECL_CONTEXT (t)))
15098 /* Template template argument like the following example need
15099 special treatment:
15101 template <template <class> class TT> struct C {};
15102 template <class T> struct D {
15103 template <class U> struct E {};
15104 C<E> c; // #1
15106 D<int> d; // #2
15108 We are processing the template argument `E' in #1 for
15109 the template instantiation #2. Originally, `E' is a
15110 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
15111 have to substitute this with one having context `D<int>'. */
15113 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
15114 if (dependent_scope_p (context))
15116 /* When rewriting a constructor into a deduction guide, a
15117 non-dependent name can become dependent, so memtmpl<args>
15118 becomes context::template memtmpl<args>. */
15119 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15120 return build_qualified_name (type, context, DECL_NAME (t),
15121 /*template*/true);
15123 return lookup_field (context, DECL_NAME(t), 0, false);
15125 else
15126 /* Ordinary template template argument. */
15127 return t;
15129 case NON_LVALUE_EXPR:
15130 case VIEW_CONVERT_EXPR:
15132 /* Handle location wrappers by substituting the wrapped node
15133 first, *then* reusing the resulting type. Doing the type
15134 first ensures that we handle template parameters and
15135 parameter pack expansions. */
15136 gcc_assert (location_wrapper_p (t));
15137 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15138 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
15141 case CAST_EXPR:
15142 case REINTERPRET_CAST_EXPR:
15143 case CONST_CAST_EXPR:
15144 case STATIC_CAST_EXPR:
15145 case DYNAMIC_CAST_EXPR:
15146 case IMPLICIT_CONV_EXPR:
15147 case CONVERT_EXPR:
15148 case NOP_EXPR:
15150 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15151 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15152 return build1 (code, type, op0);
15155 case SIZEOF_EXPR:
15156 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
15157 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
15159 tree expanded, op = TREE_OPERAND (t, 0);
15160 int len = 0;
15162 if (SIZEOF_EXPR_TYPE_P (t))
15163 op = TREE_TYPE (op);
15165 ++cp_unevaluated_operand;
15166 ++c_inhibit_evaluation_warnings;
15167 /* We only want to compute the number of arguments. */
15168 if (PACK_EXPANSION_P (op))
15169 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
15170 else
15171 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
15172 args, complain, in_decl);
15173 --cp_unevaluated_operand;
15174 --c_inhibit_evaluation_warnings;
15176 if (TREE_CODE (expanded) == TREE_VEC)
15178 len = TREE_VEC_LENGTH (expanded);
15179 /* Set TREE_USED for the benefit of -Wunused. */
15180 for (int i = 0; i < len; i++)
15181 if (DECL_P (TREE_VEC_ELT (expanded, i)))
15182 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
15185 if (expanded == error_mark_node)
15186 return error_mark_node;
15187 else if (PACK_EXPANSION_P (expanded)
15188 || (TREE_CODE (expanded) == TREE_VEC
15189 && pack_expansion_args_count (expanded)))
15192 if (PACK_EXPANSION_P (expanded))
15193 /* OK. */;
15194 else if (TREE_VEC_LENGTH (expanded) == 1)
15195 expanded = TREE_VEC_ELT (expanded, 0);
15196 else
15197 expanded = make_argument_pack (expanded);
15199 if (TYPE_P (expanded))
15200 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
15201 complain & tf_error);
15202 else
15203 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
15204 complain & tf_error);
15206 else
15207 return build_int_cst (size_type_node, len);
15209 if (SIZEOF_EXPR_TYPE_P (t))
15211 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
15212 args, complain, in_decl);
15213 r = build1 (NOP_EXPR, r, error_mark_node);
15214 r = build1 (SIZEOF_EXPR,
15215 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
15216 SIZEOF_EXPR_TYPE_P (r) = 1;
15217 return r;
15219 /* Fall through */
15221 case INDIRECT_REF:
15222 case NEGATE_EXPR:
15223 case TRUTH_NOT_EXPR:
15224 case BIT_NOT_EXPR:
15225 case ADDR_EXPR:
15226 case UNARY_PLUS_EXPR: /* Unary + */
15227 case ALIGNOF_EXPR:
15228 case AT_ENCODE_EXPR:
15229 case ARROW_EXPR:
15230 case THROW_EXPR:
15231 case TYPEID_EXPR:
15232 case REALPART_EXPR:
15233 case IMAGPART_EXPR:
15234 case PAREN_EXPR:
15236 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15237 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15238 return build1 (code, type, op0);
15241 case COMPONENT_REF:
15243 tree object;
15244 tree name;
15246 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15247 name = TREE_OPERAND (t, 1);
15248 if (TREE_CODE (name) == BIT_NOT_EXPR)
15250 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15251 complain, in_decl);
15252 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15254 else if (TREE_CODE (name) == SCOPE_REF
15255 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
15257 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
15258 complain, in_decl);
15259 name = TREE_OPERAND (name, 1);
15260 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15261 complain, in_decl);
15262 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15263 name = build_qualified_name (/*type=*/NULL_TREE,
15264 base, name,
15265 /*template_p=*/false);
15267 else if (BASELINK_P (name))
15268 name = tsubst_baselink (name,
15269 non_reference (TREE_TYPE (object)),
15270 args, complain,
15271 in_decl);
15272 else
15273 name = tsubst_copy (name, args, complain, in_decl);
15274 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
15277 case PLUS_EXPR:
15278 case MINUS_EXPR:
15279 case MULT_EXPR:
15280 case TRUNC_DIV_EXPR:
15281 case CEIL_DIV_EXPR:
15282 case FLOOR_DIV_EXPR:
15283 case ROUND_DIV_EXPR:
15284 case EXACT_DIV_EXPR:
15285 case BIT_AND_EXPR:
15286 case BIT_IOR_EXPR:
15287 case BIT_XOR_EXPR:
15288 case TRUNC_MOD_EXPR:
15289 case FLOOR_MOD_EXPR:
15290 case TRUTH_ANDIF_EXPR:
15291 case TRUTH_ORIF_EXPR:
15292 case TRUTH_AND_EXPR:
15293 case TRUTH_OR_EXPR:
15294 case RSHIFT_EXPR:
15295 case LSHIFT_EXPR:
15296 case RROTATE_EXPR:
15297 case LROTATE_EXPR:
15298 case EQ_EXPR:
15299 case NE_EXPR:
15300 case MAX_EXPR:
15301 case MIN_EXPR:
15302 case LE_EXPR:
15303 case GE_EXPR:
15304 case LT_EXPR:
15305 case GT_EXPR:
15306 case COMPOUND_EXPR:
15307 case DOTSTAR_EXPR:
15308 case MEMBER_REF:
15309 case PREDECREMENT_EXPR:
15310 case PREINCREMENT_EXPR:
15311 case POSTDECREMENT_EXPR:
15312 case POSTINCREMENT_EXPR:
15314 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15315 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15316 return build_nt (code, op0, op1);
15319 case SCOPE_REF:
15321 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15322 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15323 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
15324 QUALIFIED_NAME_IS_TEMPLATE (t));
15327 case ARRAY_REF:
15329 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15330 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15331 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
15334 case CALL_EXPR:
15336 int n = VL_EXP_OPERAND_LENGTH (t);
15337 tree result = build_vl_exp (CALL_EXPR, n);
15338 int i;
15339 for (i = 0; i < n; i++)
15340 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
15341 complain, in_decl);
15342 return result;
15345 case COND_EXPR:
15346 case MODOP_EXPR:
15347 case PSEUDO_DTOR_EXPR:
15348 case VEC_PERM_EXPR:
15350 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15351 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15352 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15353 r = build_nt (code, op0, op1, op2);
15354 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
15355 return r;
15358 case NEW_EXPR:
15360 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15361 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15362 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15363 r = build_nt (code, op0, op1, op2);
15364 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
15365 return r;
15368 case DELETE_EXPR:
15370 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15371 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15372 r = build_nt (code, op0, op1);
15373 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
15374 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
15375 return r;
15378 case TEMPLATE_ID_EXPR:
15380 /* Substituted template arguments */
15381 tree fn = TREE_OPERAND (t, 0);
15382 tree targs = TREE_OPERAND (t, 1);
15384 fn = tsubst_copy (fn, args, complain, in_decl);
15385 if (targs)
15386 targs = tsubst_template_args (targs, args, complain, in_decl);
15388 return lookup_template_function (fn, targs);
15391 case TREE_LIST:
15393 tree purpose, value, chain;
15395 if (t == void_list_node)
15396 return t;
15398 purpose = TREE_PURPOSE (t);
15399 if (purpose)
15400 purpose = tsubst_copy (purpose, args, complain, in_decl);
15401 value = TREE_VALUE (t);
15402 if (value)
15403 value = tsubst_copy (value, args, complain, in_decl);
15404 chain = TREE_CHAIN (t);
15405 if (chain && chain != void_type_node)
15406 chain = tsubst_copy (chain, args, complain, in_decl);
15407 if (purpose == TREE_PURPOSE (t)
15408 && value == TREE_VALUE (t)
15409 && chain == TREE_CHAIN (t))
15410 return t;
15411 return tree_cons (purpose, value, chain);
15414 case RECORD_TYPE:
15415 case UNION_TYPE:
15416 case ENUMERAL_TYPE:
15417 case INTEGER_TYPE:
15418 case TEMPLATE_TYPE_PARM:
15419 case TEMPLATE_TEMPLATE_PARM:
15420 case BOUND_TEMPLATE_TEMPLATE_PARM:
15421 case TEMPLATE_PARM_INDEX:
15422 case POINTER_TYPE:
15423 case REFERENCE_TYPE:
15424 case OFFSET_TYPE:
15425 case FUNCTION_TYPE:
15426 case METHOD_TYPE:
15427 case ARRAY_TYPE:
15428 case TYPENAME_TYPE:
15429 case UNBOUND_CLASS_TEMPLATE:
15430 case TYPEOF_TYPE:
15431 case DECLTYPE_TYPE:
15432 case TYPE_DECL:
15433 return tsubst (t, args, complain, in_decl);
15435 case USING_DECL:
15436 t = DECL_NAME (t);
15437 /* Fall through. */
15438 case IDENTIFIER_NODE:
15439 if (IDENTIFIER_CONV_OP_P (t))
15441 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15442 return make_conv_op_name (new_type);
15444 else
15445 return t;
15447 case CONSTRUCTOR:
15448 /* This is handled by tsubst_copy_and_build. */
15449 gcc_unreachable ();
15451 case VA_ARG_EXPR:
15453 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15454 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15455 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
15458 case CLEANUP_POINT_EXPR:
15459 /* We shouldn't have built any of these during initial template
15460 generation. Instead, they should be built during instantiation
15461 in response to the saved STMT_IS_FULL_EXPR_P setting. */
15462 gcc_unreachable ();
15464 case OFFSET_REF:
15466 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15467 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15468 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15469 r = build2 (code, type, op0, op1);
15470 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
15471 if (!mark_used (TREE_OPERAND (r, 1), complain)
15472 && !(complain & tf_error))
15473 return error_mark_node;
15474 return r;
15477 case EXPR_PACK_EXPANSION:
15478 error ("invalid use of pack expansion expression");
15479 return error_mark_node;
15481 case NONTYPE_ARGUMENT_PACK:
15482 error ("use %<...%> to expand argument pack");
15483 return error_mark_node;
15485 case VOID_CST:
15486 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
15487 return t;
15489 case INTEGER_CST:
15490 case REAL_CST:
15491 case STRING_CST:
15492 case COMPLEX_CST:
15494 /* Instantiate any typedefs in the type. */
15495 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15496 r = fold_convert (type, t);
15497 gcc_assert (TREE_CODE (r) == code);
15498 return r;
15501 case PTRMEM_CST:
15502 /* These can sometimes show up in a partial instantiation, but never
15503 involve template parms. */
15504 gcc_assert (!uses_template_parms (t));
15505 return t;
15507 case UNARY_LEFT_FOLD_EXPR:
15508 return tsubst_unary_left_fold (t, args, complain, in_decl);
15509 case UNARY_RIGHT_FOLD_EXPR:
15510 return tsubst_unary_right_fold (t, args, complain, in_decl);
15511 case BINARY_LEFT_FOLD_EXPR:
15512 return tsubst_binary_left_fold (t, args, complain, in_decl);
15513 case BINARY_RIGHT_FOLD_EXPR:
15514 return tsubst_binary_right_fold (t, args, complain, in_decl);
15515 case PREDICT_EXPR:
15516 return t;
15518 case DEBUG_BEGIN_STMT:
15519 /* ??? There's no point in copying it for now, but maybe some
15520 day it will contain more information, such as a pointer back
15521 to the containing function, inlined copy or so. */
15522 return t;
15524 default:
15525 /* We shouldn't get here, but keep going if !flag_checking. */
15526 if (flag_checking)
15527 gcc_unreachable ();
15528 return t;
15532 /* Helper function for tsubst_omp_clauses, used for instantiation of
15533 OMP_CLAUSE_DECL of clauses. */
15535 static tree
15536 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
15537 tree in_decl)
15539 if (decl == NULL_TREE)
15540 return NULL_TREE;
15542 /* Handle an OpenMP array section represented as a TREE_LIST (or
15543 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15544 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15545 TREE_LIST. We can handle it exactly the same as an array section
15546 (purpose, value, and a chain), even though the nomenclature
15547 (low_bound, length, etc) is different. */
15548 if (TREE_CODE (decl) == TREE_LIST)
15550 tree low_bound
15551 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
15552 /*integral_constant_expression_p=*/false);
15553 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
15554 /*integral_constant_expression_p=*/false);
15555 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
15556 in_decl);
15557 if (TREE_PURPOSE (decl) == low_bound
15558 && TREE_VALUE (decl) == length
15559 && TREE_CHAIN (decl) == chain)
15560 return decl;
15561 tree ret = tree_cons (low_bound, length, chain);
15562 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
15563 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
15564 return ret;
15566 tree ret = tsubst_expr (decl, args, complain, in_decl,
15567 /*integral_constant_expression_p=*/false);
15568 /* Undo convert_from_reference tsubst_expr could have called. */
15569 if (decl
15570 && REFERENCE_REF_P (ret)
15571 && !REFERENCE_REF_P (decl))
15572 ret = TREE_OPERAND (ret, 0);
15573 return ret;
15576 /* Like tsubst_copy, but specifically for OpenMP clauses. */
15578 static tree
15579 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
15580 tree args, tsubst_flags_t complain, tree in_decl)
15582 tree new_clauses = NULL_TREE, nc, oc;
15583 tree linear_no_step = NULL_TREE;
15585 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
15587 nc = copy_node (oc);
15588 OMP_CLAUSE_CHAIN (nc) = new_clauses;
15589 new_clauses = nc;
15591 switch (OMP_CLAUSE_CODE (nc))
15593 case OMP_CLAUSE_LASTPRIVATE:
15594 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
15596 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
15597 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
15598 in_decl, /*integral_constant_expression_p=*/false);
15599 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
15600 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
15602 /* FALLTHRU */
15603 case OMP_CLAUSE_PRIVATE:
15604 case OMP_CLAUSE_SHARED:
15605 case OMP_CLAUSE_FIRSTPRIVATE:
15606 case OMP_CLAUSE_COPYIN:
15607 case OMP_CLAUSE_COPYPRIVATE:
15608 case OMP_CLAUSE_UNIFORM:
15609 case OMP_CLAUSE_DEPEND:
15610 case OMP_CLAUSE_FROM:
15611 case OMP_CLAUSE_TO:
15612 case OMP_CLAUSE_MAP:
15613 case OMP_CLAUSE_USE_DEVICE_PTR:
15614 case OMP_CLAUSE_IS_DEVICE_PTR:
15615 OMP_CLAUSE_DECL (nc)
15616 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15617 in_decl);
15618 break;
15619 case OMP_CLAUSE_TILE:
15620 case OMP_CLAUSE_IF:
15621 case OMP_CLAUSE_NUM_THREADS:
15622 case OMP_CLAUSE_SCHEDULE:
15623 case OMP_CLAUSE_COLLAPSE:
15624 case OMP_CLAUSE_FINAL:
15625 case OMP_CLAUSE_DEVICE:
15626 case OMP_CLAUSE_DIST_SCHEDULE:
15627 case OMP_CLAUSE_NUM_TEAMS:
15628 case OMP_CLAUSE_THREAD_LIMIT:
15629 case OMP_CLAUSE_SAFELEN:
15630 case OMP_CLAUSE_SIMDLEN:
15631 case OMP_CLAUSE_NUM_TASKS:
15632 case OMP_CLAUSE_GRAINSIZE:
15633 case OMP_CLAUSE_PRIORITY:
15634 case OMP_CLAUSE_ORDERED:
15635 case OMP_CLAUSE_HINT:
15636 case OMP_CLAUSE_NUM_GANGS:
15637 case OMP_CLAUSE_NUM_WORKERS:
15638 case OMP_CLAUSE_VECTOR_LENGTH:
15639 case OMP_CLAUSE_WORKER:
15640 case OMP_CLAUSE_VECTOR:
15641 case OMP_CLAUSE_ASYNC:
15642 case OMP_CLAUSE_WAIT:
15643 OMP_CLAUSE_OPERAND (nc, 0)
15644 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
15645 in_decl, /*integral_constant_expression_p=*/false);
15646 break;
15647 case OMP_CLAUSE_REDUCTION:
15648 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
15650 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
15651 if (TREE_CODE (placeholder) == SCOPE_REF)
15653 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
15654 complain, in_decl);
15655 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
15656 = build_qualified_name (NULL_TREE, scope,
15657 TREE_OPERAND (placeholder, 1),
15658 false);
15660 else
15661 gcc_assert (identifier_p (placeholder));
15663 OMP_CLAUSE_DECL (nc)
15664 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15665 in_decl);
15666 break;
15667 case OMP_CLAUSE_GANG:
15668 case OMP_CLAUSE_ALIGNED:
15669 OMP_CLAUSE_DECL (nc)
15670 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15671 in_decl);
15672 OMP_CLAUSE_OPERAND (nc, 1)
15673 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
15674 in_decl, /*integral_constant_expression_p=*/false);
15675 break;
15676 case OMP_CLAUSE_LINEAR:
15677 OMP_CLAUSE_DECL (nc)
15678 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15679 in_decl);
15680 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
15682 gcc_assert (!linear_no_step);
15683 linear_no_step = nc;
15685 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
15686 OMP_CLAUSE_LINEAR_STEP (nc)
15687 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
15688 complain, in_decl);
15689 else
15690 OMP_CLAUSE_LINEAR_STEP (nc)
15691 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
15692 in_decl,
15693 /*integral_constant_expression_p=*/false);
15694 break;
15695 case OMP_CLAUSE_NOWAIT:
15696 case OMP_CLAUSE_DEFAULT:
15697 case OMP_CLAUSE_UNTIED:
15698 case OMP_CLAUSE_MERGEABLE:
15699 case OMP_CLAUSE_INBRANCH:
15700 case OMP_CLAUSE_NOTINBRANCH:
15701 case OMP_CLAUSE_PROC_BIND:
15702 case OMP_CLAUSE_FOR:
15703 case OMP_CLAUSE_PARALLEL:
15704 case OMP_CLAUSE_SECTIONS:
15705 case OMP_CLAUSE_TASKGROUP:
15706 case OMP_CLAUSE_NOGROUP:
15707 case OMP_CLAUSE_THREADS:
15708 case OMP_CLAUSE_SIMD:
15709 case OMP_CLAUSE_DEFAULTMAP:
15710 case OMP_CLAUSE_INDEPENDENT:
15711 case OMP_CLAUSE_AUTO:
15712 case OMP_CLAUSE_SEQ:
15713 break;
15714 default:
15715 gcc_unreachable ();
15717 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
15718 switch (OMP_CLAUSE_CODE (nc))
15720 case OMP_CLAUSE_SHARED:
15721 case OMP_CLAUSE_PRIVATE:
15722 case OMP_CLAUSE_FIRSTPRIVATE:
15723 case OMP_CLAUSE_LASTPRIVATE:
15724 case OMP_CLAUSE_COPYPRIVATE:
15725 case OMP_CLAUSE_LINEAR:
15726 case OMP_CLAUSE_REDUCTION:
15727 case OMP_CLAUSE_USE_DEVICE_PTR:
15728 case OMP_CLAUSE_IS_DEVICE_PTR:
15729 /* tsubst_expr on SCOPE_REF results in returning
15730 finish_non_static_data_member result. Undo that here. */
15731 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
15732 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
15733 == IDENTIFIER_NODE))
15735 tree t = OMP_CLAUSE_DECL (nc);
15736 tree v = t;
15737 while (v)
15738 switch (TREE_CODE (v))
15740 case COMPONENT_REF:
15741 case MEM_REF:
15742 case INDIRECT_REF:
15743 CASE_CONVERT:
15744 case POINTER_PLUS_EXPR:
15745 v = TREE_OPERAND (v, 0);
15746 continue;
15747 case PARM_DECL:
15748 if (DECL_CONTEXT (v) == current_function_decl
15749 && DECL_ARTIFICIAL (v)
15750 && DECL_NAME (v) == this_identifier)
15751 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
15752 /* FALLTHRU */
15753 default:
15754 v = NULL_TREE;
15755 break;
15758 else if (VAR_P (OMP_CLAUSE_DECL (oc))
15759 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
15760 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
15761 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
15762 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
15764 tree decl = OMP_CLAUSE_DECL (nc);
15765 if (VAR_P (decl))
15767 retrofit_lang_decl (decl);
15768 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
15771 break;
15772 default:
15773 break;
15777 new_clauses = nreverse (new_clauses);
15778 if (ort != C_ORT_OMP_DECLARE_SIMD)
15780 new_clauses = finish_omp_clauses (new_clauses, ort);
15781 if (linear_no_step)
15782 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
15783 if (nc == linear_no_step)
15785 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
15786 break;
15789 return new_clauses;
15792 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
15794 static tree
15795 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
15796 tree in_decl)
15798 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
15800 tree purpose, value, chain;
15802 if (t == NULL)
15803 return t;
15805 if (TREE_CODE (t) != TREE_LIST)
15806 return tsubst_copy_and_build (t, args, complain, in_decl,
15807 /*function_p=*/false,
15808 /*integral_constant_expression_p=*/false);
15810 if (t == void_list_node)
15811 return t;
15813 purpose = TREE_PURPOSE (t);
15814 if (purpose)
15815 purpose = RECUR (purpose);
15816 value = TREE_VALUE (t);
15817 if (value)
15819 if (TREE_CODE (value) != LABEL_DECL)
15820 value = RECUR (value);
15821 else
15823 value = lookup_label (DECL_NAME (value));
15824 gcc_assert (TREE_CODE (value) == LABEL_DECL);
15825 TREE_USED (value) = 1;
15828 chain = TREE_CHAIN (t);
15829 if (chain && chain != void_type_node)
15830 chain = RECUR (chain);
15831 return tree_cons (purpose, value, chain);
15832 #undef RECUR
15835 /* Used to temporarily communicate the list of #pragma omp parallel
15836 clauses to #pragma omp for instantiation if they are combined
15837 together. */
15839 static tree *omp_parallel_combined_clauses;
15841 /* Substitute one OMP_FOR iterator. */
15843 static void
15844 tsubst_omp_for_iterator (tree t, int i, tree declv, tree orig_declv,
15845 tree initv, tree condv, tree incrv, tree *clauses,
15846 tree args, tsubst_flags_t complain, tree in_decl,
15847 bool integral_constant_expression_p)
15849 #define RECUR(NODE) \
15850 tsubst_expr ((NODE), args, complain, in_decl, \
15851 integral_constant_expression_p)
15852 tree decl, init, cond, incr;
15854 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
15855 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
15857 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
15859 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
15860 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
15863 decl = TREE_OPERAND (init, 0);
15864 init = TREE_OPERAND (init, 1);
15865 tree decl_expr = NULL_TREE;
15866 if (init && TREE_CODE (init) == DECL_EXPR)
15868 /* We need to jump through some hoops to handle declarations in the
15869 init-statement, since we might need to handle auto deduction,
15870 but we need to keep control of initialization. */
15871 decl_expr = init;
15872 init = DECL_INITIAL (DECL_EXPR_DECL (init));
15873 decl = tsubst_decl (decl, args, complain);
15875 else
15877 if (TREE_CODE (decl) == SCOPE_REF)
15879 decl = RECUR (decl);
15880 if (TREE_CODE (decl) == COMPONENT_REF)
15882 tree v = decl;
15883 while (v)
15884 switch (TREE_CODE (v))
15886 case COMPONENT_REF:
15887 case MEM_REF:
15888 case INDIRECT_REF:
15889 CASE_CONVERT:
15890 case POINTER_PLUS_EXPR:
15891 v = TREE_OPERAND (v, 0);
15892 continue;
15893 case PARM_DECL:
15894 if (DECL_CONTEXT (v) == current_function_decl
15895 && DECL_ARTIFICIAL (v)
15896 && DECL_NAME (v) == this_identifier)
15898 decl = TREE_OPERAND (decl, 1);
15899 decl = omp_privatize_field (decl, false);
15901 /* FALLTHRU */
15902 default:
15903 v = NULL_TREE;
15904 break;
15908 else
15909 decl = RECUR (decl);
15911 init = RECUR (init);
15913 tree auto_node = type_uses_auto (TREE_TYPE (decl));
15914 if (auto_node && init)
15915 TREE_TYPE (decl)
15916 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
15918 gcc_assert (!type_dependent_expression_p (decl));
15920 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15922 if (decl_expr)
15924 /* Declare the variable, but don't let that initialize it. */
15925 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
15926 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
15927 RECUR (decl_expr);
15928 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
15931 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
15932 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15933 if (TREE_CODE (incr) == MODIFY_EXPR)
15935 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15936 tree rhs = RECUR (TREE_OPERAND (incr, 1));
15937 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
15938 NOP_EXPR, rhs, complain);
15940 else
15941 incr = RECUR (incr);
15942 TREE_VEC_ELT (declv, i) = decl;
15943 TREE_VEC_ELT (initv, i) = init;
15944 TREE_VEC_ELT (condv, i) = cond;
15945 TREE_VEC_ELT (incrv, i) = incr;
15946 return;
15949 if (decl_expr)
15951 /* Declare and initialize the variable. */
15952 RECUR (decl_expr);
15953 init = NULL_TREE;
15955 else if (init)
15957 tree *pc;
15958 int j;
15959 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
15961 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
15963 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
15964 && OMP_CLAUSE_DECL (*pc) == decl)
15965 break;
15966 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
15967 && OMP_CLAUSE_DECL (*pc) == decl)
15969 if (j)
15970 break;
15971 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15972 tree c = *pc;
15973 *pc = OMP_CLAUSE_CHAIN (c);
15974 OMP_CLAUSE_CHAIN (c) = *clauses;
15975 *clauses = c;
15977 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
15978 && OMP_CLAUSE_DECL (*pc) == decl)
15980 error ("iteration variable %qD should not be firstprivate",
15981 decl);
15982 *pc = OMP_CLAUSE_CHAIN (*pc);
15984 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
15985 && OMP_CLAUSE_DECL (*pc) == decl)
15987 error ("iteration variable %qD should not be reduction",
15988 decl);
15989 *pc = OMP_CLAUSE_CHAIN (*pc);
15991 else
15992 pc = &OMP_CLAUSE_CHAIN (*pc);
15994 if (*pc)
15995 break;
15997 if (*pc == NULL_TREE)
15999 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
16000 OMP_CLAUSE_DECL (c) = decl;
16001 c = finish_omp_clauses (c, C_ORT_OMP);
16002 if (c)
16004 OMP_CLAUSE_CHAIN (c) = *clauses;
16005 *clauses = c;
16009 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
16010 if (COMPARISON_CLASS_P (cond))
16012 tree op0 = RECUR (TREE_OPERAND (cond, 0));
16013 tree op1 = RECUR (TREE_OPERAND (cond, 1));
16014 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
16016 else
16017 cond = RECUR (cond);
16018 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
16019 switch (TREE_CODE (incr))
16021 case PREINCREMENT_EXPR:
16022 case PREDECREMENT_EXPR:
16023 case POSTINCREMENT_EXPR:
16024 case POSTDECREMENT_EXPR:
16025 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
16026 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
16027 break;
16028 case MODIFY_EXPR:
16029 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
16030 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
16032 tree rhs = TREE_OPERAND (incr, 1);
16033 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16034 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
16035 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
16036 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16037 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
16038 rhs0, rhs1));
16040 else
16041 incr = RECUR (incr);
16042 break;
16043 case MODOP_EXPR:
16044 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
16045 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
16047 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16048 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16049 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
16050 TREE_TYPE (decl), lhs,
16051 RECUR (TREE_OPERAND (incr, 2))));
16053 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
16054 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
16055 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
16057 tree rhs = TREE_OPERAND (incr, 2);
16058 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16059 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
16060 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
16061 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16062 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
16063 rhs0, rhs1));
16065 else
16066 incr = RECUR (incr);
16067 break;
16068 default:
16069 incr = RECUR (incr);
16070 break;
16073 TREE_VEC_ELT (declv, i) = decl;
16074 TREE_VEC_ELT (initv, i) = init;
16075 TREE_VEC_ELT (condv, i) = cond;
16076 TREE_VEC_ELT (incrv, i) = incr;
16077 #undef RECUR
16080 /* Helper function of tsubst_expr, find OMP_TEAMS inside
16081 of OMP_TARGET's body. */
16083 static tree
16084 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
16086 *walk_subtrees = 0;
16087 switch (TREE_CODE (*tp))
16089 case OMP_TEAMS:
16090 return *tp;
16091 case BIND_EXPR:
16092 case STATEMENT_LIST:
16093 *walk_subtrees = 1;
16094 break;
16095 default:
16096 break;
16098 return NULL_TREE;
16101 /* Helper function for tsubst_expr. For decomposition declaration
16102 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
16103 also the corresponding decls representing the identifiers
16104 of the decomposition declaration. Return DECL if successful
16105 or error_mark_node otherwise, set *FIRST to the first decl
16106 in the list chained through DECL_CHAIN and *CNT to the number
16107 of such decls. */
16109 static tree
16110 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
16111 tsubst_flags_t complain, tree in_decl, tree *first,
16112 unsigned int *cnt)
16114 tree decl2, decl3, prev = decl;
16115 *cnt = 0;
16116 gcc_assert (DECL_NAME (decl) == NULL_TREE);
16117 for (decl2 = DECL_CHAIN (pattern_decl);
16118 decl2
16119 && VAR_P (decl2)
16120 && DECL_DECOMPOSITION_P (decl2)
16121 && DECL_NAME (decl2);
16122 decl2 = DECL_CHAIN (decl2))
16124 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
16126 gcc_assert (errorcount);
16127 return error_mark_node;
16129 (*cnt)++;
16130 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
16131 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
16132 tree v = DECL_VALUE_EXPR (decl2);
16133 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
16134 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
16135 decl3 = tsubst (decl2, args, complain, in_decl);
16136 SET_DECL_VALUE_EXPR (decl2, v);
16137 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
16138 if (VAR_P (decl3))
16139 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
16140 maybe_push_decl (decl3);
16141 if (error_operand_p (decl3))
16142 decl = error_mark_node;
16143 else if (decl != error_mark_node
16144 && DECL_CHAIN (decl3) != prev)
16146 gcc_assert (errorcount);
16147 decl = error_mark_node;
16149 else
16150 prev = decl3;
16152 *first = prev;
16153 return decl;
16156 /* Like tsubst_copy for expressions, etc. but also does semantic
16157 processing. */
16159 tree
16160 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
16161 bool integral_constant_expression_p)
16163 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
16164 #define RECUR(NODE) \
16165 tsubst_expr ((NODE), args, complain, in_decl, \
16166 integral_constant_expression_p)
16168 tree stmt, tmp;
16169 tree r;
16170 location_t loc;
16172 if (t == NULL_TREE || t == error_mark_node)
16173 return t;
16175 loc = input_location;
16176 if (EXPR_HAS_LOCATION (t))
16177 input_location = EXPR_LOCATION (t);
16178 if (STATEMENT_CODE_P (TREE_CODE (t)))
16179 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
16181 switch (TREE_CODE (t))
16183 case STATEMENT_LIST:
16185 tree_stmt_iterator i;
16186 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
16187 RECUR (tsi_stmt (i));
16188 break;
16191 case CTOR_INITIALIZER:
16192 finish_mem_initializers (tsubst_initializer_list
16193 (TREE_OPERAND (t, 0), args));
16194 break;
16196 case RETURN_EXPR:
16197 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
16198 break;
16200 case EXPR_STMT:
16201 tmp = RECUR (EXPR_STMT_EXPR (t));
16202 if (EXPR_STMT_STMT_EXPR_RESULT (t))
16203 finish_stmt_expr_expr (tmp, cur_stmt_expr);
16204 else
16205 finish_expr_stmt (tmp);
16206 break;
16208 case USING_STMT:
16209 finish_local_using_directive (USING_STMT_NAMESPACE (t),
16210 /*attribs=*/NULL_TREE);
16211 break;
16213 case DECL_EXPR:
16215 tree decl, pattern_decl;
16216 tree init;
16218 pattern_decl = decl = DECL_EXPR_DECL (t);
16219 if (TREE_CODE (decl) == LABEL_DECL)
16220 finish_label_decl (DECL_NAME (decl));
16221 else if (TREE_CODE (decl) == USING_DECL)
16223 tree scope = USING_DECL_SCOPE (decl);
16224 tree name = DECL_NAME (decl);
16226 scope = tsubst (scope, args, complain, in_decl);
16227 decl = lookup_qualified_name (scope, name,
16228 /*is_type_p=*/false,
16229 /*complain=*/false);
16230 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
16231 qualified_name_lookup_error (scope, name, decl, input_location);
16232 else
16233 finish_local_using_decl (decl, scope, name);
16235 else if (is_capture_proxy (decl)
16236 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
16238 /* We're in tsubst_lambda_expr, we've already inserted a new
16239 capture proxy, so look it up and register it. */
16240 tree inst;
16241 if (DECL_PACK_P (decl))
16243 inst = (retrieve_local_specialization
16244 (DECL_CAPTURED_VARIABLE (decl)));
16245 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK);
16247 else
16249 inst = lookup_name_real (DECL_NAME (decl), 0, 0,
16250 /*block_p=*/true, 0, LOOKUP_HIDDEN);
16251 gcc_assert (inst != decl && is_capture_proxy (inst));
16253 register_local_specialization (inst, decl);
16254 break;
16256 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
16257 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
16258 /* Don't copy the old closure; we'll create a new one in
16259 tsubst_lambda_expr. */
16260 break;
16261 else
16263 init = DECL_INITIAL (decl);
16264 decl = tsubst (decl, args, complain, in_decl);
16265 if (decl != error_mark_node)
16267 /* By marking the declaration as instantiated, we avoid
16268 trying to instantiate it. Since instantiate_decl can't
16269 handle local variables, and since we've already done
16270 all that needs to be done, that's the right thing to
16271 do. */
16272 if (VAR_P (decl))
16273 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16274 if (VAR_P (decl)
16275 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
16276 /* Anonymous aggregates are a special case. */
16277 finish_anon_union (decl);
16278 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
16280 DECL_CONTEXT (decl) = current_function_decl;
16281 if (DECL_NAME (decl) == this_identifier)
16283 tree lam = DECL_CONTEXT (current_function_decl);
16284 lam = CLASSTYPE_LAMBDA_EXPR (lam);
16285 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
16287 insert_capture_proxy (decl);
16289 else if (DECL_IMPLICIT_TYPEDEF_P (t))
16290 /* We already did a pushtag. */;
16291 else if (TREE_CODE (decl) == FUNCTION_DECL
16292 && DECL_OMP_DECLARE_REDUCTION_P (decl)
16293 && DECL_FUNCTION_SCOPE_P (pattern_decl))
16295 DECL_CONTEXT (decl) = NULL_TREE;
16296 pushdecl (decl);
16297 DECL_CONTEXT (decl) = current_function_decl;
16298 cp_check_omp_declare_reduction (decl);
16300 else
16302 int const_init = false;
16303 maybe_push_decl (decl);
16304 if (VAR_P (decl)
16305 && DECL_PRETTY_FUNCTION_P (decl))
16307 /* For __PRETTY_FUNCTION__ we have to adjust the
16308 initializer. */
16309 const char *const name
16310 = cxx_printable_name (current_function_decl, 2);
16311 init = cp_fname_init (name, &TREE_TYPE (decl));
16313 else
16314 init = tsubst_init (init, decl, args, complain, in_decl);
16316 if (VAR_P (decl))
16317 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
16318 (pattern_decl));
16319 if (VAR_P (decl)
16320 && DECL_DECOMPOSITION_P (decl)
16321 && TREE_TYPE (pattern_decl) != error_mark_node)
16323 unsigned int cnt;
16324 tree first;
16325 tree ndecl
16326 = tsubst_decomp_names (decl, pattern_decl, args,
16327 complain, in_decl, &first, &cnt);
16328 if (ndecl != error_mark_node)
16329 cp_maybe_mangle_decomp (ndecl, first, cnt);
16330 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
16331 if (ndecl != error_mark_node)
16332 cp_finish_decomp (ndecl, first, cnt);
16334 else
16335 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
16340 break;
16343 case FOR_STMT:
16344 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
16345 RECUR (FOR_INIT_STMT (t));
16346 finish_init_stmt (stmt);
16347 tmp = RECUR (FOR_COND (t));
16348 finish_for_cond (tmp, stmt, false, 0);
16349 tmp = RECUR (FOR_EXPR (t));
16350 finish_for_expr (tmp, stmt);
16352 bool prev = note_iteration_stmt_body_start ();
16353 RECUR (FOR_BODY (t));
16354 note_iteration_stmt_body_end (prev);
16356 finish_for_stmt (stmt);
16357 break;
16359 case RANGE_FOR_STMT:
16361 /* Construct another range_for, if this is not a final
16362 substitution (for inside inside a generic lambda of a
16363 template). Otherwise convert to a regular for. */
16364 tree decl, expr;
16365 stmt = (processing_template_decl
16366 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
16367 : begin_for_stmt (NULL_TREE, NULL_TREE));
16368 decl = RANGE_FOR_DECL (t);
16369 decl = tsubst (decl, args, complain, in_decl);
16370 maybe_push_decl (decl);
16371 expr = RECUR (RANGE_FOR_EXPR (t));
16373 tree decomp_first = NULL_TREE;
16374 unsigned decomp_cnt = 0;
16375 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
16376 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
16377 complain, in_decl,
16378 &decomp_first, &decomp_cnt);
16380 if (processing_template_decl)
16382 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
16383 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
16384 finish_range_for_decl (stmt, decl, expr);
16386 else
16388 unsigned short unroll = (RANGE_FOR_UNROLL (t)
16389 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
16390 stmt = cp_convert_range_for (stmt, decl, expr,
16391 decomp_first, decomp_cnt,
16392 RANGE_FOR_IVDEP (t), unroll);
16395 bool prev = note_iteration_stmt_body_start ();
16396 RECUR (RANGE_FOR_BODY (t));
16397 note_iteration_stmt_body_end (prev);
16398 finish_for_stmt (stmt);
16400 break;
16402 case WHILE_STMT:
16403 stmt = begin_while_stmt ();
16404 tmp = RECUR (WHILE_COND (t));
16405 finish_while_stmt_cond (tmp, stmt, false, 0);
16407 bool prev = note_iteration_stmt_body_start ();
16408 RECUR (WHILE_BODY (t));
16409 note_iteration_stmt_body_end (prev);
16411 finish_while_stmt (stmt);
16412 break;
16414 case DO_STMT:
16415 stmt = begin_do_stmt ();
16417 bool prev = note_iteration_stmt_body_start ();
16418 RECUR (DO_BODY (t));
16419 note_iteration_stmt_body_end (prev);
16421 finish_do_body (stmt);
16422 tmp = RECUR (DO_COND (t));
16423 finish_do_stmt (tmp, stmt, false, 0);
16424 break;
16426 case IF_STMT:
16427 stmt = begin_if_stmt ();
16428 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
16429 tmp = RECUR (IF_COND (t));
16430 tmp = finish_if_stmt_cond (tmp, stmt);
16431 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
16432 /* Don't instantiate the THEN_CLAUSE. */;
16433 else
16435 bool inhibit = integer_zerop (fold_non_dependent_expr (tmp));
16436 if (inhibit)
16437 ++c_inhibit_evaluation_warnings;
16438 RECUR (THEN_CLAUSE (t));
16439 if (inhibit)
16440 --c_inhibit_evaluation_warnings;
16442 finish_then_clause (stmt);
16444 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
16445 /* Don't instantiate the ELSE_CLAUSE. */;
16446 else if (ELSE_CLAUSE (t))
16448 bool inhibit = integer_nonzerop (fold_non_dependent_expr (tmp));
16449 begin_else_clause (stmt);
16450 if (inhibit)
16451 ++c_inhibit_evaluation_warnings;
16452 RECUR (ELSE_CLAUSE (t));
16453 if (inhibit)
16454 --c_inhibit_evaluation_warnings;
16455 finish_else_clause (stmt);
16458 finish_if_stmt (stmt);
16459 break;
16461 case BIND_EXPR:
16462 if (BIND_EXPR_BODY_BLOCK (t))
16463 stmt = begin_function_body ();
16464 else
16465 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
16466 ? BCS_TRY_BLOCK : 0);
16468 RECUR (BIND_EXPR_BODY (t));
16470 if (BIND_EXPR_BODY_BLOCK (t))
16471 finish_function_body (stmt);
16472 else
16473 finish_compound_stmt (stmt);
16474 break;
16476 case BREAK_STMT:
16477 finish_break_stmt ();
16478 break;
16480 case CONTINUE_STMT:
16481 finish_continue_stmt ();
16482 break;
16484 case SWITCH_STMT:
16485 stmt = begin_switch_stmt ();
16486 tmp = RECUR (SWITCH_STMT_COND (t));
16487 finish_switch_cond (tmp, stmt);
16488 RECUR (SWITCH_STMT_BODY (t));
16489 finish_switch_stmt (stmt);
16490 break;
16492 case CASE_LABEL_EXPR:
16494 tree low = RECUR (CASE_LOW (t));
16495 tree high = RECUR (CASE_HIGH (t));
16496 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
16497 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
16498 FALLTHROUGH_LABEL_P (CASE_LABEL (l))
16499 = FALLTHROUGH_LABEL_P (CASE_LABEL (t));
16501 break;
16503 case LABEL_EXPR:
16505 tree decl = LABEL_EXPR_LABEL (t);
16506 tree label;
16508 label = finish_label_stmt (DECL_NAME (decl));
16509 if (TREE_CODE (label) == LABEL_DECL)
16510 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
16511 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
16512 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
16514 break;
16516 case GOTO_EXPR:
16517 tmp = GOTO_DESTINATION (t);
16518 if (TREE_CODE (tmp) != LABEL_DECL)
16519 /* Computed goto's must be tsubst'd into. On the other hand,
16520 non-computed gotos must not be; the identifier in question
16521 will have no binding. */
16522 tmp = RECUR (tmp);
16523 else
16524 tmp = DECL_NAME (tmp);
16525 finish_goto_stmt (tmp);
16526 break;
16528 case ASM_EXPR:
16530 tree string = RECUR (ASM_STRING (t));
16531 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
16532 complain, in_decl);
16533 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
16534 complain, in_decl);
16535 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
16536 complain, in_decl);
16537 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
16538 complain, in_decl);
16539 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
16540 clobbers, labels);
16541 tree asm_expr = tmp;
16542 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
16543 asm_expr = TREE_OPERAND (asm_expr, 0);
16544 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
16546 break;
16548 case TRY_BLOCK:
16549 if (CLEANUP_P (t))
16551 stmt = begin_try_block ();
16552 RECUR (TRY_STMTS (t));
16553 finish_cleanup_try_block (stmt);
16554 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
16556 else
16558 tree compound_stmt = NULL_TREE;
16560 if (FN_TRY_BLOCK_P (t))
16561 stmt = begin_function_try_block (&compound_stmt);
16562 else
16563 stmt = begin_try_block ();
16565 RECUR (TRY_STMTS (t));
16567 if (FN_TRY_BLOCK_P (t))
16568 finish_function_try_block (stmt);
16569 else
16570 finish_try_block (stmt);
16572 RECUR (TRY_HANDLERS (t));
16573 if (FN_TRY_BLOCK_P (t))
16574 finish_function_handler_sequence (stmt, compound_stmt);
16575 else
16576 finish_handler_sequence (stmt);
16578 break;
16580 case HANDLER:
16582 tree decl = HANDLER_PARMS (t);
16584 if (decl)
16586 decl = tsubst (decl, args, complain, in_decl);
16587 /* Prevent instantiate_decl from trying to instantiate
16588 this variable. We've already done all that needs to be
16589 done. */
16590 if (decl != error_mark_node)
16591 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16593 stmt = begin_handler ();
16594 finish_handler_parms (decl, stmt);
16595 RECUR (HANDLER_BODY (t));
16596 finish_handler (stmt);
16598 break;
16600 case TAG_DEFN:
16601 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
16602 if (CLASS_TYPE_P (tmp))
16604 /* Local classes are not independent templates; they are
16605 instantiated along with their containing function. And this
16606 way we don't have to deal with pushing out of one local class
16607 to instantiate a member of another local class. */
16608 /* Closures are handled by the LAMBDA_EXPR. */
16609 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
16610 complete_type (tmp);
16611 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
16612 if ((VAR_P (fld)
16613 || (TREE_CODE (fld) == FUNCTION_DECL
16614 && !DECL_ARTIFICIAL (fld)))
16615 && DECL_TEMPLATE_INSTANTIATION (fld))
16616 instantiate_decl (fld, /*defer_ok=*/false,
16617 /*expl_inst_class=*/false);
16619 break;
16621 case STATIC_ASSERT:
16623 tree condition;
16625 ++c_inhibit_evaluation_warnings;
16626 condition =
16627 tsubst_expr (STATIC_ASSERT_CONDITION (t),
16628 args,
16629 complain, in_decl,
16630 /*integral_constant_expression_p=*/true);
16631 --c_inhibit_evaluation_warnings;
16633 finish_static_assert (condition,
16634 STATIC_ASSERT_MESSAGE (t),
16635 STATIC_ASSERT_SOURCE_LOCATION (t),
16636 /*member_p=*/false);
16638 break;
16640 case OACC_KERNELS:
16641 case OACC_PARALLEL:
16642 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
16643 in_decl);
16644 stmt = begin_omp_parallel ();
16645 RECUR (OMP_BODY (t));
16646 finish_omp_construct (TREE_CODE (t), stmt, tmp);
16647 break;
16649 case OMP_PARALLEL:
16650 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
16651 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
16652 complain, in_decl);
16653 if (OMP_PARALLEL_COMBINED (t))
16654 omp_parallel_combined_clauses = &tmp;
16655 stmt = begin_omp_parallel ();
16656 RECUR (OMP_PARALLEL_BODY (t));
16657 gcc_assert (omp_parallel_combined_clauses == NULL);
16658 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
16659 = OMP_PARALLEL_COMBINED (t);
16660 pop_omp_privatization_clauses (r);
16661 break;
16663 case OMP_TASK:
16664 r = push_omp_privatization_clauses (false);
16665 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
16666 complain, in_decl);
16667 stmt = begin_omp_task ();
16668 RECUR (OMP_TASK_BODY (t));
16669 finish_omp_task (tmp, stmt);
16670 pop_omp_privatization_clauses (r);
16671 break;
16673 case OMP_FOR:
16674 case OMP_SIMD:
16675 case OMP_DISTRIBUTE:
16676 case OMP_TASKLOOP:
16677 case OACC_LOOP:
16679 tree clauses, body, pre_body;
16680 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
16681 tree orig_declv = NULL_TREE;
16682 tree incrv = NULL_TREE;
16683 enum c_omp_region_type ort = C_ORT_OMP;
16684 int i;
16686 if (TREE_CODE (t) == OACC_LOOP)
16687 ort = C_ORT_ACC;
16689 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
16690 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
16691 in_decl);
16692 if (OMP_FOR_INIT (t) != NULL_TREE)
16694 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16695 if (OMP_FOR_ORIG_DECLS (t))
16696 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16697 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16698 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16699 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16702 stmt = begin_omp_structured_block ();
16704 pre_body = push_stmt_list ();
16705 RECUR (OMP_FOR_PRE_BODY (t));
16706 pre_body = pop_stmt_list (pre_body);
16708 if (OMP_FOR_INIT (t) != NULL_TREE)
16709 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
16710 tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, condv,
16711 incrv, &clauses, args, complain, in_decl,
16712 integral_constant_expression_p);
16713 omp_parallel_combined_clauses = NULL;
16715 body = push_stmt_list ();
16716 RECUR (OMP_FOR_BODY (t));
16717 body = pop_stmt_list (body);
16719 if (OMP_FOR_INIT (t) != NULL_TREE)
16720 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
16721 orig_declv, initv, condv, incrv, body, pre_body,
16722 NULL, clauses);
16723 else
16725 t = make_node (TREE_CODE (t));
16726 TREE_TYPE (t) = void_type_node;
16727 OMP_FOR_BODY (t) = body;
16728 OMP_FOR_PRE_BODY (t) = pre_body;
16729 OMP_FOR_CLAUSES (t) = clauses;
16730 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
16731 add_stmt (t);
16734 add_stmt (finish_omp_structured_block (stmt));
16735 pop_omp_privatization_clauses (r);
16737 break;
16739 case OMP_SECTIONS:
16740 omp_parallel_combined_clauses = NULL;
16741 /* FALLTHRU */
16742 case OMP_SINGLE:
16743 case OMP_TEAMS:
16744 case OMP_CRITICAL:
16745 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
16746 && OMP_TEAMS_COMBINED (t));
16747 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
16748 in_decl);
16749 stmt = push_stmt_list ();
16750 RECUR (OMP_BODY (t));
16751 stmt = pop_stmt_list (stmt);
16753 t = copy_node (t);
16754 OMP_BODY (t) = stmt;
16755 OMP_CLAUSES (t) = tmp;
16756 add_stmt (t);
16757 pop_omp_privatization_clauses (r);
16758 break;
16760 case OACC_DATA:
16761 case OMP_TARGET_DATA:
16762 case OMP_TARGET:
16763 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
16764 ? C_ORT_ACC : C_ORT_OMP, args, complain,
16765 in_decl);
16766 keep_next_level (true);
16767 stmt = begin_omp_structured_block ();
16769 RECUR (OMP_BODY (t));
16770 stmt = finish_omp_structured_block (stmt);
16772 t = copy_node (t);
16773 OMP_BODY (t) = stmt;
16774 OMP_CLAUSES (t) = tmp;
16775 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
16777 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
16778 if (teams)
16780 /* For combined target teams, ensure the num_teams and
16781 thread_limit clause expressions are evaluated on the host,
16782 before entering the target construct. */
16783 tree c;
16784 for (c = OMP_TEAMS_CLAUSES (teams);
16785 c; c = OMP_CLAUSE_CHAIN (c))
16786 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
16787 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
16788 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
16790 tree expr = OMP_CLAUSE_OPERAND (c, 0);
16791 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
16792 if (expr == error_mark_node)
16793 continue;
16794 tmp = TARGET_EXPR_SLOT (expr);
16795 add_stmt (expr);
16796 OMP_CLAUSE_OPERAND (c, 0) = expr;
16797 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16798 OMP_CLAUSE_FIRSTPRIVATE);
16799 OMP_CLAUSE_DECL (tc) = tmp;
16800 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
16801 OMP_TARGET_CLAUSES (t) = tc;
16805 add_stmt (t);
16806 break;
16808 case OACC_DECLARE:
16809 t = copy_node (t);
16810 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
16811 complain, in_decl);
16812 OACC_DECLARE_CLAUSES (t) = tmp;
16813 add_stmt (t);
16814 break;
16816 case OMP_TARGET_UPDATE:
16817 case OMP_TARGET_ENTER_DATA:
16818 case OMP_TARGET_EXIT_DATA:
16819 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
16820 complain, in_decl);
16821 t = copy_node (t);
16822 OMP_STANDALONE_CLAUSES (t) = tmp;
16823 add_stmt (t);
16824 break;
16826 case OACC_ENTER_DATA:
16827 case OACC_EXIT_DATA:
16828 case OACC_UPDATE:
16829 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
16830 complain, in_decl);
16831 t = copy_node (t);
16832 OMP_STANDALONE_CLAUSES (t) = tmp;
16833 add_stmt (t);
16834 break;
16836 case OMP_ORDERED:
16837 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
16838 complain, in_decl);
16839 stmt = push_stmt_list ();
16840 RECUR (OMP_BODY (t));
16841 stmt = pop_stmt_list (stmt);
16843 t = copy_node (t);
16844 OMP_BODY (t) = stmt;
16845 OMP_ORDERED_CLAUSES (t) = tmp;
16846 add_stmt (t);
16847 break;
16849 case OMP_SECTION:
16850 case OMP_MASTER:
16851 case OMP_TASKGROUP:
16852 stmt = push_stmt_list ();
16853 RECUR (OMP_BODY (t));
16854 stmt = pop_stmt_list (stmt);
16856 t = copy_node (t);
16857 OMP_BODY (t) = stmt;
16858 add_stmt (t);
16859 break;
16861 case OMP_ATOMIC:
16862 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
16863 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
16865 tree op1 = TREE_OPERAND (t, 1);
16866 tree rhs1 = NULL_TREE;
16867 tree lhs, rhs;
16868 if (TREE_CODE (op1) == COMPOUND_EXPR)
16870 rhs1 = RECUR (TREE_OPERAND (op1, 0));
16871 op1 = TREE_OPERAND (op1, 1);
16873 lhs = RECUR (TREE_OPERAND (op1, 0));
16874 rhs = RECUR (TREE_OPERAND (op1, 1));
16875 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
16876 NULL_TREE, NULL_TREE, rhs1,
16877 OMP_ATOMIC_SEQ_CST (t));
16879 else
16881 tree op1 = TREE_OPERAND (t, 1);
16882 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
16883 tree rhs1 = NULL_TREE;
16884 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
16885 enum tree_code opcode = NOP_EXPR;
16886 if (code == OMP_ATOMIC_READ)
16888 v = RECUR (TREE_OPERAND (op1, 0));
16889 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16891 else if (code == OMP_ATOMIC_CAPTURE_OLD
16892 || code == OMP_ATOMIC_CAPTURE_NEW)
16894 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
16895 v = RECUR (TREE_OPERAND (op1, 0));
16896 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16897 if (TREE_CODE (op11) == COMPOUND_EXPR)
16899 rhs1 = RECUR (TREE_OPERAND (op11, 0));
16900 op11 = TREE_OPERAND (op11, 1);
16902 lhs = RECUR (TREE_OPERAND (op11, 0));
16903 rhs = RECUR (TREE_OPERAND (op11, 1));
16904 opcode = TREE_CODE (op11);
16905 if (opcode == MODIFY_EXPR)
16906 opcode = NOP_EXPR;
16908 else
16910 code = OMP_ATOMIC;
16911 lhs = RECUR (TREE_OPERAND (op1, 0));
16912 rhs = RECUR (TREE_OPERAND (op1, 1));
16914 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
16915 OMP_ATOMIC_SEQ_CST (t));
16917 break;
16919 case TRANSACTION_EXPR:
16921 int flags = 0;
16922 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
16923 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
16925 if (TRANSACTION_EXPR_IS_STMT (t))
16927 tree body = TRANSACTION_EXPR_BODY (t);
16928 tree noex = NULL_TREE;
16929 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
16931 noex = MUST_NOT_THROW_COND (body);
16932 if (noex == NULL_TREE)
16933 noex = boolean_true_node;
16934 body = TREE_OPERAND (body, 0);
16936 stmt = begin_transaction_stmt (input_location, NULL, flags);
16937 RECUR (body);
16938 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
16940 else
16942 stmt = build_transaction_expr (EXPR_LOCATION (t),
16943 RECUR (TRANSACTION_EXPR_BODY (t)),
16944 flags, NULL_TREE);
16945 RETURN (stmt);
16948 break;
16950 case MUST_NOT_THROW_EXPR:
16952 tree op0 = RECUR (TREE_OPERAND (t, 0));
16953 tree cond = RECUR (MUST_NOT_THROW_COND (t));
16954 RETURN (build_must_not_throw_expr (op0, cond));
16957 case EXPR_PACK_EXPANSION:
16958 error ("invalid use of pack expansion expression");
16959 RETURN (error_mark_node);
16961 case NONTYPE_ARGUMENT_PACK:
16962 error ("use %<...%> to expand argument pack");
16963 RETURN (error_mark_node);
16965 case COMPOUND_EXPR:
16966 tmp = RECUR (TREE_OPERAND (t, 0));
16967 if (tmp == NULL_TREE)
16968 /* If the first operand was a statement, we're done with it. */
16969 RETURN (RECUR (TREE_OPERAND (t, 1)));
16970 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
16971 RECUR (TREE_OPERAND (t, 1)),
16972 complain));
16974 case ANNOTATE_EXPR:
16975 tmp = RECUR (TREE_OPERAND (t, 0));
16976 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
16977 TREE_TYPE (tmp), tmp,
16978 RECUR (TREE_OPERAND (t, 1)),
16979 RECUR (TREE_OPERAND (t, 2))));
16981 default:
16982 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
16984 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
16985 /*function_p=*/false,
16986 integral_constant_expression_p));
16989 RETURN (NULL_TREE);
16990 out:
16991 input_location = loc;
16992 return r;
16993 #undef RECUR
16994 #undef RETURN
16997 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
16998 function. For description of the body see comment above
16999 cp_parser_omp_declare_reduction_exprs. */
17001 static void
17002 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
17004 if (t == NULL_TREE || t == error_mark_node)
17005 return;
17007 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
17009 tree_stmt_iterator tsi;
17010 int i;
17011 tree stmts[7];
17012 memset (stmts, 0, sizeof stmts);
17013 for (i = 0, tsi = tsi_start (t);
17014 i < 7 && !tsi_end_p (tsi);
17015 i++, tsi_next (&tsi))
17016 stmts[i] = tsi_stmt (tsi);
17017 gcc_assert (tsi_end_p (tsi));
17019 if (i >= 3)
17021 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
17022 && TREE_CODE (stmts[1]) == DECL_EXPR);
17023 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
17024 args, complain, in_decl);
17025 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
17026 args, complain, in_decl);
17027 DECL_CONTEXT (omp_out) = current_function_decl;
17028 DECL_CONTEXT (omp_in) = current_function_decl;
17029 keep_next_level (true);
17030 tree block = begin_omp_structured_block ();
17031 tsubst_expr (stmts[2], args, complain, in_decl, false);
17032 block = finish_omp_structured_block (block);
17033 block = maybe_cleanup_point_expr_void (block);
17034 add_decl_expr (omp_out);
17035 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
17036 TREE_NO_WARNING (omp_out) = 1;
17037 add_decl_expr (omp_in);
17038 finish_expr_stmt (block);
17040 if (i >= 6)
17042 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
17043 && TREE_CODE (stmts[4]) == DECL_EXPR);
17044 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
17045 args, complain, in_decl);
17046 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
17047 args, complain, in_decl);
17048 DECL_CONTEXT (omp_priv) = current_function_decl;
17049 DECL_CONTEXT (omp_orig) = current_function_decl;
17050 keep_next_level (true);
17051 tree block = begin_omp_structured_block ();
17052 tsubst_expr (stmts[5], args, complain, in_decl, false);
17053 block = finish_omp_structured_block (block);
17054 block = maybe_cleanup_point_expr_void (block);
17055 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
17056 add_decl_expr (omp_priv);
17057 add_decl_expr (omp_orig);
17058 finish_expr_stmt (block);
17059 if (i == 7)
17060 add_decl_expr (omp_orig);
17064 /* T is a postfix-expression that is not being used in a function
17065 call. Return the substituted version of T. */
17067 static tree
17068 tsubst_non_call_postfix_expression (tree t, tree args,
17069 tsubst_flags_t complain,
17070 tree in_decl)
17072 if (TREE_CODE (t) == SCOPE_REF)
17073 t = tsubst_qualified_id (t, args, complain, in_decl,
17074 /*done=*/false, /*address_p=*/false);
17075 else
17076 t = tsubst_copy_and_build (t, args, complain, in_decl,
17077 /*function_p=*/false,
17078 /*integral_constant_expression_p=*/false);
17080 return t;
17083 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
17084 instantiation context. Instantiating a pack expansion containing a lambda
17085 might result in multiple lambdas all based on the same lambda in the
17086 template. */
17088 tree
17089 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
17091 tree oldfn = lambda_function (t);
17092 in_decl = oldfn;
17094 tree r = build_lambda_expr ();
17096 LAMBDA_EXPR_LOCATION (r)
17097 = LAMBDA_EXPR_LOCATION (t);
17098 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
17099 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
17100 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
17102 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
17103 LAMBDA_EXPR_EXTRA_SCOPE (r) = NULL_TREE;
17104 else
17105 record_lambda_scope (r);
17107 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
17108 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
17110 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
17111 cap = TREE_CHAIN (cap))
17113 tree field = TREE_PURPOSE (cap);
17114 if (PACK_EXPANSION_P (field))
17115 field = PACK_EXPANSION_PATTERN (field);
17116 field = tsubst_decl (field, args, complain);
17118 if (field == error_mark_node)
17119 return error_mark_node;
17121 tree init = TREE_VALUE (cap);
17122 if (PACK_EXPANSION_P (init))
17123 init = tsubst_pack_expansion (init, args, complain, in_decl);
17124 else
17125 init = tsubst_copy_and_build (init, args, complain, in_decl,
17126 /*fn*/false, /*constexpr*/false);
17128 if (TREE_CODE (field) == TREE_VEC)
17130 int len = TREE_VEC_LENGTH (field);
17131 gcc_assert (TREE_CODE (init) == TREE_VEC
17132 && TREE_VEC_LENGTH (init) == len);
17133 for (int i = 0; i < len; ++i)
17134 LAMBDA_EXPR_CAPTURE_LIST (r)
17135 = tree_cons (TREE_VEC_ELT (field, i),
17136 TREE_VEC_ELT (init, i),
17137 LAMBDA_EXPR_CAPTURE_LIST (r));
17139 else
17141 LAMBDA_EXPR_CAPTURE_LIST (r)
17142 = tree_cons (field, init, LAMBDA_EXPR_CAPTURE_LIST (r));
17144 if (id_equal (DECL_NAME (field), "__this"))
17145 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
17149 tree type = begin_lambda_type (r);
17151 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
17152 determine_visibility (TYPE_NAME (type));
17154 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
17156 tree oldtmpl = (generic_lambda_fn_p (oldfn)
17157 ? DECL_TI_TEMPLATE (oldfn)
17158 : NULL_TREE);
17160 tree fntype = static_fn_type (oldfn);
17161 if (oldtmpl)
17162 ++processing_template_decl;
17163 fntype = tsubst (fntype, args, complain, in_decl);
17164 if (oldtmpl)
17165 --processing_template_decl;
17167 if (fntype == error_mark_node)
17168 r = error_mark_node;
17169 else
17171 /* Fix the type of 'this'. */
17172 fntype = build_memfn_type (fntype, type,
17173 type_memfn_quals (fntype),
17174 type_memfn_rqual (fntype));
17175 tree fn, tmpl;
17176 if (oldtmpl)
17178 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
17179 fn = DECL_TEMPLATE_RESULT (tmpl);
17180 finish_member_declaration (tmpl);
17182 else
17184 tmpl = NULL_TREE;
17185 fn = tsubst_function_decl (oldfn, args, complain, fntype);
17186 finish_member_declaration (fn);
17189 /* Let finish_function set this. */
17190 DECL_DECLARED_CONSTEXPR_P (fn) = false;
17192 bool nested = cfun;
17193 if (nested)
17194 push_function_context ();
17195 else
17196 /* Still increment function_depth so that we don't GC in the
17197 middle of an expression. */
17198 ++function_depth;
17200 local_specialization_stack s (lss_copy);
17202 tree body = start_lambda_function (fn, r);
17204 register_parameter_specializations (oldfn, fn);
17206 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
17207 /*constexpr*/false);
17209 finish_lambda_function (body);
17211 if (nested)
17212 pop_function_context ();
17213 else
17214 --function_depth;
17216 /* The capture list was built up in reverse order; fix that now. */
17217 LAMBDA_EXPR_CAPTURE_LIST (r)
17218 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
17220 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
17222 maybe_add_lambda_conv_op (type);
17225 finish_struct (type, /*attr*/NULL_TREE);
17227 insert_pending_capture_proxies ();
17229 return r;
17232 /* Like tsubst but deals with expressions and performs semantic
17233 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
17235 tree
17236 tsubst_copy_and_build (tree t,
17237 tree args,
17238 tsubst_flags_t complain,
17239 tree in_decl,
17240 bool function_p,
17241 bool integral_constant_expression_p)
17243 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
17244 #define RECUR(NODE) \
17245 tsubst_copy_and_build (NODE, args, complain, in_decl, \
17246 /*function_p=*/false, \
17247 integral_constant_expression_p)
17249 tree retval, op1;
17250 location_t loc;
17252 if (t == NULL_TREE || t == error_mark_node)
17253 return t;
17255 loc = input_location;
17256 if (EXPR_HAS_LOCATION (t))
17257 input_location = EXPR_LOCATION (t);
17259 /* N3276 decltype magic only applies to calls at the top level or on the
17260 right side of a comma. */
17261 tsubst_flags_t decltype_flag = (complain & tf_decltype);
17262 complain &= ~tf_decltype;
17264 switch (TREE_CODE (t))
17266 case USING_DECL:
17267 t = DECL_NAME (t);
17268 /* Fall through. */
17269 case IDENTIFIER_NODE:
17271 tree decl;
17272 cp_id_kind idk;
17273 bool non_integral_constant_expression_p;
17274 const char *error_msg;
17276 if (IDENTIFIER_CONV_OP_P (t))
17278 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17279 t = make_conv_op_name (new_type);
17282 /* Look up the name. */
17283 decl = lookup_name (t);
17285 /* By convention, expressions use ERROR_MARK_NODE to indicate
17286 failure, not NULL_TREE. */
17287 if (decl == NULL_TREE)
17288 decl = error_mark_node;
17290 decl = finish_id_expression (t, decl, NULL_TREE,
17291 &idk,
17292 integral_constant_expression_p,
17293 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
17294 &non_integral_constant_expression_p,
17295 /*template_p=*/false,
17296 /*done=*/true,
17297 /*address_p=*/false,
17298 /*template_arg_p=*/false,
17299 &error_msg,
17300 input_location);
17301 if (error_msg)
17302 error (error_msg);
17303 if (!function_p && identifier_p (decl))
17305 if (complain & tf_error)
17306 unqualified_name_lookup_error (decl);
17307 decl = error_mark_node;
17309 RETURN (decl);
17312 case TEMPLATE_ID_EXPR:
17314 tree object;
17315 tree templ = RECUR (TREE_OPERAND (t, 0));
17316 tree targs = TREE_OPERAND (t, 1);
17318 if (targs)
17319 targs = tsubst_template_args (targs, args, complain, in_decl);
17320 if (targs == error_mark_node)
17321 RETURN (error_mark_node);
17323 if (TREE_CODE (templ) == SCOPE_REF)
17325 tree name = TREE_OPERAND (templ, 1);
17326 tree tid = lookup_template_function (name, targs);
17327 TREE_OPERAND (templ, 1) = tid;
17328 RETURN (templ);
17331 if (variable_template_p (templ))
17332 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
17334 if (TREE_CODE (templ) == COMPONENT_REF)
17336 object = TREE_OPERAND (templ, 0);
17337 templ = TREE_OPERAND (templ, 1);
17339 else
17340 object = NULL_TREE;
17341 templ = lookup_template_function (templ, targs);
17343 if (object)
17344 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
17345 object, templ, NULL_TREE));
17346 else
17347 RETURN (baselink_for_fns (templ));
17350 case INDIRECT_REF:
17352 tree r = RECUR (TREE_OPERAND (t, 0));
17354 if (REFERENCE_REF_P (t))
17356 /* A type conversion to reference type will be enclosed in
17357 such an indirect ref, but the substitution of the cast
17358 will have also added such an indirect ref. */
17359 r = convert_from_reference (r);
17361 else
17362 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
17363 complain|decltype_flag);
17365 if (REF_PARENTHESIZED_P (t))
17366 r = force_paren_expr (r);
17368 RETURN (r);
17371 case NOP_EXPR:
17373 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17374 tree op0 = RECUR (TREE_OPERAND (t, 0));
17375 RETURN (build_nop (type, op0));
17378 case IMPLICIT_CONV_EXPR:
17380 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17381 tree expr = RECUR (TREE_OPERAND (t, 0));
17382 if (dependent_type_p (type) || type_dependent_expression_p (expr))
17384 retval = copy_node (t);
17385 TREE_TYPE (retval) = type;
17386 TREE_OPERAND (retval, 0) = expr;
17387 RETURN (retval);
17389 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
17390 /* We'll pass this to convert_nontype_argument again, we don't need
17391 to actually perform any conversion here. */
17392 RETURN (expr);
17393 int flags = LOOKUP_IMPLICIT;
17394 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
17395 flags = LOOKUP_NORMAL;
17396 RETURN (perform_implicit_conversion_flags (type, expr, complain,
17397 flags));
17400 case CONVERT_EXPR:
17402 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17403 tree op0 = RECUR (TREE_OPERAND (t, 0));
17404 if (op0 == error_mark_node)
17405 RETURN (error_mark_node);
17406 RETURN (build1 (CONVERT_EXPR, type, op0));
17409 case CAST_EXPR:
17410 case REINTERPRET_CAST_EXPR:
17411 case CONST_CAST_EXPR:
17412 case DYNAMIC_CAST_EXPR:
17413 case STATIC_CAST_EXPR:
17415 tree type;
17416 tree op, r = NULL_TREE;
17418 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17419 if (integral_constant_expression_p
17420 && !cast_valid_in_integral_constant_expression_p (type))
17422 if (complain & tf_error)
17423 error ("a cast to a type other than an integral or "
17424 "enumeration type cannot appear in a constant-expression");
17425 RETURN (error_mark_node);
17428 op = RECUR (TREE_OPERAND (t, 0));
17430 warning_sentinel s(warn_useless_cast);
17431 warning_sentinel s2(warn_ignored_qualifiers);
17432 switch (TREE_CODE (t))
17434 case CAST_EXPR:
17435 r = build_functional_cast (type, op, complain);
17436 break;
17437 case REINTERPRET_CAST_EXPR:
17438 r = build_reinterpret_cast (type, op, complain);
17439 break;
17440 case CONST_CAST_EXPR:
17441 r = build_const_cast (type, op, complain);
17442 break;
17443 case DYNAMIC_CAST_EXPR:
17444 r = build_dynamic_cast (type, op, complain);
17445 break;
17446 case STATIC_CAST_EXPR:
17447 r = build_static_cast (type, op, complain);
17448 break;
17449 default:
17450 gcc_unreachable ();
17453 RETURN (r);
17456 case POSTDECREMENT_EXPR:
17457 case POSTINCREMENT_EXPR:
17458 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17459 args, complain, in_decl);
17460 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
17461 complain|decltype_flag));
17463 case PREDECREMENT_EXPR:
17464 case PREINCREMENT_EXPR:
17465 case NEGATE_EXPR:
17466 case BIT_NOT_EXPR:
17467 case ABS_EXPR:
17468 case TRUTH_NOT_EXPR:
17469 case UNARY_PLUS_EXPR: /* Unary + */
17470 case REALPART_EXPR:
17471 case IMAGPART_EXPR:
17472 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
17473 RECUR (TREE_OPERAND (t, 0)),
17474 complain|decltype_flag));
17476 case FIX_TRUNC_EXPR:
17477 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
17478 false, complain));
17480 case ADDR_EXPR:
17481 op1 = TREE_OPERAND (t, 0);
17482 if (TREE_CODE (op1) == LABEL_DECL)
17483 RETURN (finish_label_address_expr (DECL_NAME (op1),
17484 EXPR_LOCATION (op1)));
17485 if (TREE_CODE (op1) == SCOPE_REF)
17486 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
17487 /*done=*/true, /*address_p=*/true);
17488 else
17489 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
17490 in_decl);
17491 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
17492 complain|decltype_flag));
17494 case PLUS_EXPR:
17495 case MINUS_EXPR:
17496 case MULT_EXPR:
17497 case TRUNC_DIV_EXPR:
17498 case CEIL_DIV_EXPR:
17499 case FLOOR_DIV_EXPR:
17500 case ROUND_DIV_EXPR:
17501 case EXACT_DIV_EXPR:
17502 case BIT_AND_EXPR:
17503 case BIT_IOR_EXPR:
17504 case BIT_XOR_EXPR:
17505 case TRUNC_MOD_EXPR:
17506 case FLOOR_MOD_EXPR:
17507 case TRUTH_ANDIF_EXPR:
17508 case TRUTH_ORIF_EXPR:
17509 case TRUTH_AND_EXPR:
17510 case TRUTH_OR_EXPR:
17511 case RSHIFT_EXPR:
17512 case LSHIFT_EXPR:
17513 case RROTATE_EXPR:
17514 case LROTATE_EXPR:
17515 case EQ_EXPR:
17516 case NE_EXPR:
17517 case MAX_EXPR:
17518 case MIN_EXPR:
17519 case LE_EXPR:
17520 case GE_EXPR:
17521 case LT_EXPR:
17522 case GT_EXPR:
17523 case MEMBER_REF:
17524 case DOTSTAR_EXPR:
17526 warning_sentinel s1(warn_type_limits);
17527 warning_sentinel s2(warn_div_by_zero);
17528 warning_sentinel s3(warn_logical_op);
17529 warning_sentinel s4(warn_tautological_compare);
17530 tree op0 = RECUR (TREE_OPERAND (t, 0));
17531 tree op1 = RECUR (TREE_OPERAND (t, 1));
17532 tree r = build_x_binary_op
17533 (input_location, TREE_CODE (t),
17534 op0,
17535 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
17536 ? ERROR_MARK
17537 : TREE_CODE (TREE_OPERAND (t, 0))),
17538 op1,
17539 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
17540 ? ERROR_MARK
17541 : TREE_CODE (TREE_OPERAND (t, 1))),
17542 /*overload=*/NULL,
17543 complain|decltype_flag);
17544 if (EXPR_P (r) && TREE_NO_WARNING (t))
17545 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17547 RETURN (r);
17550 case POINTER_PLUS_EXPR:
17552 tree op0 = RECUR (TREE_OPERAND (t, 0));
17553 tree op1 = RECUR (TREE_OPERAND (t, 1));
17554 RETURN (fold_build_pointer_plus (op0, op1));
17557 case SCOPE_REF:
17558 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
17559 /*address_p=*/false));
17560 case ARRAY_REF:
17561 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17562 args, complain, in_decl);
17563 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
17564 RECUR (TREE_OPERAND (t, 1)),
17565 complain|decltype_flag));
17567 case SIZEOF_EXPR:
17568 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
17569 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
17570 RETURN (tsubst_copy (t, args, complain, in_decl));
17571 /* Fall through */
17573 case ALIGNOF_EXPR:
17575 tree r;
17577 op1 = TREE_OPERAND (t, 0);
17578 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
17579 op1 = TREE_TYPE (op1);
17580 if (!args)
17582 /* When there are no ARGS, we are trying to evaluate a
17583 non-dependent expression from the parser. Trying to do
17584 the substitutions may not work. */
17585 if (!TYPE_P (op1))
17586 op1 = TREE_TYPE (op1);
17588 else
17590 ++cp_unevaluated_operand;
17591 ++c_inhibit_evaluation_warnings;
17592 if (TYPE_P (op1))
17593 op1 = tsubst (op1, args, complain, in_decl);
17594 else
17595 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17596 /*function_p=*/false,
17597 /*integral_constant_expression_p=*/
17598 false);
17599 --cp_unevaluated_operand;
17600 --c_inhibit_evaluation_warnings;
17602 if (TYPE_P (op1))
17603 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
17604 complain & tf_error);
17605 else
17606 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
17607 complain & tf_error);
17608 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
17610 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
17612 if (!processing_template_decl && TYPE_P (op1))
17614 r = build_min (SIZEOF_EXPR, size_type_node,
17615 build1 (NOP_EXPR, op1, error_mark_node));
17616 SIZEOF_EXPR_TYPE_P (r) = 1;
17618 else
17619 r = build_min (SIZEOF_EXPR, size_type_node, op1);
17620 TREE_SIDE_EFFECTS (r) = 0;
17621 TREE_READONLY (r) = 1;
17623 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
17625 RETURN (r);
17628 case AT_ENCODE_EXPR:
17630 op1 = TREE_OPERAND (t, 0);
17631 ++cp_unevaluated_operand;
17632 ++c_inhibit_evaluation_warnings;
17633 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17634 /*function_p=*/false,
17635 /*integral_constant_expression_p=*/false);
17636 --cp_unevaluated_operand;
17637 --c_inhibit_evaluation_warnings;
17638 RETURN (objc_build_encode_expr (op1));
17641 case NOEXCEPT_EXPR:
17642 op1 = TREE_OPERAND (t, 0);
17643 ++cp_unevaluated_operand;
17644 ++c_inhibit_evaluation_warnings;
17645 ++cp_noexcept_operand;
17646 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17647 /*function_p=*/false,
17648 /*integral_constant_expression_p=*/false);
17649 --cp_unevaluated_operand;
17650 --c_inhibit_evaluation_warnings;
17651 --cp_noexcept_operand;
17652 RETURN (finish_noexcept_expr (op1, complain));
17654 case MODOP_EXPR:
17656 warning_sentinel s(warn_div_by_zero);
17657 tree lhs = RECUR (TREE_OPERAND (t, 0));
17658 tree rhs = RECUR (TREE_OPERAND (t, 2));
17659 tree r = build_x_modify_expr
17660 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
17661 complain|decltype_flag);
17662 /* TREE_NO_WARNING must be set if either the expression was
17663 parenthesized or it uses an operator such as >>= rather
17664 than plain assignment. In the former case, it was already
17665 set and must be copied. In the latter case,
17666 build_x_modify_expr sets it and it must not be reset
17667 here. */
17668 if (TREE_NO_WARNING (t))
17669 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17671 RETURN (r);
17674 case ARROW_EXPR:
17675 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17676 args, complain, in_decl);
17677 /* Remember that there was a reference to this entity. */
17678 if (DECL_P (op1)
17679 && !mark_used (op1, complain) && !(complain & tf_error))
17680 RETURN (error_mark_node);
17681 RETURN (build_x_arrow (input_location, op1, complain));
17683 case NEW_EXPR:
17685 tree placement = RECUR (TREE_OPERAND (t, 0));
17686 tree init = RECUR (TREE_OPERAND (t, 3));
17687 vec<tree, va_gc> *placement_vec;
17688 vec<tree, va_gc> *init_vec;
17689 tree ret;
17691 if (placement == NULL_TREE)
17692 placement_vec = NULL;
17693 else
17695 placement_vec = make_tree_vector ();
17696 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
17697 vec_safe_push (placement_vec, TREE_VALUE (placement));
17700 /* If there was an initializer in the original tree, but it
17701 instantiated to an empty list, then we should pass a
17702 non-NULL empty vector to tell build_new that it was an
17703 empty initializer() rather than no initializer. This can
17704 only happen when the initializer is a pack expansion whose
17705 parameter packs are of length zero. */
17706 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
17707 init_vec = NULL;
17708 else
17710 init_vec = make_tree_vector ();
17711 if (init == void_node)
17712 gcc_assert (init_vec != NULL);
17713 else
17715 for (; init != NULL_TREE; init = TREE_CHAIN (init))
17716 vec_safe_push (init_vec, TREE_VALUE (init));
17720 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
17721 tree op2 = RECUR (TREE_OPERAND (t, 2));
17722 ret = build_new (&placement_vec, op1, op2, &init_vec,
17723 NEW_EXPR_USE_GLOBAL (t),
17724 complain);
17726 if (placement_vec != NULL)
17727 release_tree_vector (placement_vec);
17728 if (init_vec != NULL)
17729 release_tree_vector (init_vec);
17731 RETURN (ret);
17734 case DELETE_EXPR:
17736 tree op0 = RECUR (TREE_OPERAND (t, 0));
17737 tree op1 = RECUR (TREE_OPERAND (t, 1));
17738 RETURN (delete_sanity (op0, op1,
17739 DELETE_EXPR_USE_VEC (t),
17740 DELETE_EXPR_USE_GLOBAL (t),
17741 complain));
17744 case COMPOUND_EXPR:
17746 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
17747 complain & ~tf_decltype, in_decl,
17748 /*function_p=*/false,
17749 integral_constant_expression_p);
17750 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
17751 op0,
17752 RECUR (TREE_OPERAND (t, 1)),
17753 complain|decltype_flag));
17756 case CALL_EXPR:
17758 tree function;
17759 vec<tree, va_gc> *call_args;
17760 unsigned int nargs, i;
17761 bool qualified_p;
17762 bool koenig_p;
17763 tree ret;
17765 function = CALL_EXPR_FN (t);
17766 /* Internal function with no arguments. */
17767 if (function == NULL_TREE && call_expr_nargs (t) == 0)
17768 RETURN (t);
17770 /* When we parsed the expression, we determined whether or
17771 not Koenig lookup should be performed. */
17772 koenig_p = KOENIG_LOOKUP_P (t);
17773 if (function == NULL_TREE)
17775 koenig_p = false;
17776 qualified_p = false;
17778 else if (TREE_CODE (function) == SCOPE_REF)
17780 qualified_p = true;
17781 function = tsubst_qualified_id (function, args, complain, in_decl,
17782 /*done=*/false,
17783 /*address_p=*/false);
17785 else if (koenig_p && identifier_p (function))
17787 /* Do nothing; calling tsubst_copy_and_build on an identifier
17788 would incorrectly perform unqualified lookup again.
17790 Note that we can also have an IDENTIFIER_NODE if the earlier
17791 unqualified lookup found a member function; in that case
17792 koenig_p will be false and we do want to do the lookup
17793 again to find the instantiated member function.
17795 FIXME but doing that causes c++/15272, so we need to stop
17796 using IDENTIFIER_NODE in that situation. */
17797 qualified_p = false;
17799 else
17801 if (TREE_CODE (function) == COMPONENT_REF)
17803 tree op = TREE_OPERAND (function, 1);
17805 qualified_p = (TREE_CODE (op) == SCOPE_REF
17806 || (BASELINK_P (op)
17807 && BASELINK_QUALIFIED_P (op)));
17809 else
17810 qualified_p = false;
17812 if (TREE_CODE (function) == ADDR_EXPR
17813 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
17814 /* Avoid error about taking the address of a constructor. */
17815 function = TREE_OPERAND (function, 0);
17817 function = tsubst_copy_and_build (function, args, complain,
17818 in_decl,
17819 !qualified_p,
17820 integral_constant_expression_p);
17822 if (BASELINK_P (function))
17823 qualified_p = true;
17826 nargs = call_expr_nargs (t);
17827 call_args = make_tree_vector ();
17828 for (i = 0; i < nargs; ++i)
17830 tree arg = CALL_EXPR_ARG (t, i);
17832 if (!PACK_EXPANSION_P (arg))
17833 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
17834 else
17836 /* Expand the pack expansion and push each entry onto
17837 CALL_ARGS. */
17838 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
17839 if (TREE_CODE (arg) == TREE_VEC)
17841 unsigned int len, j;
17843 len = TREE_VEC_LENGTH (arg);
17844 for (j = 0; j < len; ++j)
17846 tree value = TREE_VEC_ELT (arg, j);
17847 if (value != NULL_TREE)
17848 value = convert_from_reference (value);
17849 vec_safe_push (call_args, value);
17852 else
17854 /* A partial substitution. Add one entry. */
17855 vec_safe_push (call_args, arg);
17860 /* We do not perform argument-dependent lookup if normal
17861 lookup finds a non-function, in accordance with the
17862 expected resolution of DR 218. */
17863 if (koenig_p
17864 && ((is_overloaded_fn (function)
17865 /* If lookup found a member function, the Koenig lookup is
17866 not appropriate, even if an unqualified-name was used
17867 to denote the function. */
17868 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
17869 || identifier_p (function))
17870 /* Only do this when substitution turns a dependent call
17871 into a non-dependent call. */
17872 && type_dependent_expression_p_push (t)
17873 && !any_type_dependent_arguments_p (call_args))
17874 function = perform_koenig_lookup (function, call_args, tf_none);
17876 if (function != NULL_TREE
17877 && identifier_p (function)
17878 && !any_type_dependent_arguments_p (call_args))
17880 if (koenig_p && (complain & tf_warning_or_error))
17882 /* For backwards compatibility and good diagnostics, try
17883 the unqualified lookup again if we aren't in SFINAE
17884 context. */
17885 tree unq = (tsubst_copy_and_build
17886 (function, args, complain, in_decl, true,
17887 integral_constant_expression_p));
17888 if (unq == error_mark_node)
17890 release_tree_vector (call_args);
17891 RETURN (error_mark_node);
17894 if (unq != function)
17896 /* In a lambda fn, we have to be careful to not
17897 introduce new this captures. Legacy code can't
17898 be using lambdas anyway, so it's ok to be
17899 stricter. */
17900 bool in_lambda = (current_class_type
17901 && LAMBDA_TYPE_P (current_class_type));
17902 char const *const msg
17903 = G_("%qD was not declared in this scope, "
17904 "and no declarations were found by "
17905 "argument-dependent lookup at the point "
17906 "of instantiation");
17908 bool diag = true;
17909 if (in_lambda)
17910 error_at (EXPR_LOC_OR_LOC (t, input_location),
17911 msg, function);
17912 else
17913 diag = permerror (EXPR_LOC_OR_LOC (t, input_location),
17914 msg, function);
17915 if (diag)
17917 tree fn = unq;
17919 if (INDIRECT_REF_P (fn))
17920 fn = TREE_OPERAND (fn, 0);
17921 if (is_overloaded_fn (fn))
17922 fn = get_first_fn (fn);
17924 if (!DECL_P (fn))
17925 /* Can't say anything more. */;
17926 else if (DECL_CLASS_SCOPE_P (fn))
17928 location_t loc = EXPR_LOC_OR_LOC (t,
17929 input_location);
17930 inform (loc,
17931 "declarations in dependent base %qT are "
17932 "not found by unqualified lookup",
17933 DECL_CLASS_CONTEXT (fn));
17934 if (current_class_ptr)
17935 inform (loc,
17936 "use %<this->%D%> instead", function);
17937 else
17938 inform (loc,
17939 "use %<%T::%D%> instead",
17940 current_class_name, function);
17942 else
17943 inform (DECL_SOURCE_LOCATION (fn),
17944 "%qD declared here, later in the "
17945 "translation unit", fn);
17946 if (in_lambda)
17948 release_tree_vector (call_args);
17949 RETURN (error_mark_node);
17953 function = unq;
17956 if (identifier_p (function))
17958 if (complain & tf_error)
17959 unqualified_name_lookup_error (function);
17960 release_tree_vector (call_args);
17961 RETURN (error_mark_node);
17965 /* Remember that there was a reference to this entity. */
17966 if (function != NULL_TREE
17967 && DECL_P (function)
17968 && !mark_used (function, complain) && !(complain & tf_error))
17970 release_tree_vector (call_args);
17971 RETURN (error_mark_node);
17974 /* Put back tf_decltype for the actual call. */
17975 complain |= decltype_flag;
17977 if (function == NULL_TREE)
17978 switch (CALL_EXPR_IFN (t))
17980 case IFN_LAUNDER:
17981 gcc_assert (nargs == 1);
17982 if (vec_safe_length (call_args) != 1)
17984 error_at (EXPR_LOC_OR_LOC (t, input_location),
17985 "wrong number of arguments to "
17986 "%<__builtin_launder%>");
17987 ret = error_mark_node;
17989 else
17990 ret = finish_builtin_launder (EXPR_LOC_OR_LOC (t,
17991 input_location),
17992 (*call_args)[0], complain);
17993 break;
17995 default:
17996 /* Unsupported internal function with arguments. */
17997 gcc_unreachable ();
17999 else if (TREE_CODE (function) == OFFSET_REF)
18000 ret = build_offset_ref_call_from_tree (function, &call_args,
18001 complain);
18002 else if (TREE_CODE (function) == COMPONENT_REF)
18004 tree instance = TREE_OPERAND (function, 0);
18005 tree fn = TREE_OPERAND (function, 1);
18007 if (processing_template_decl
18008 && (type_dependent_expression_p (instance)
18009 || (!BASELINK_P (fn)
18010 && TREE_CODE (fn) != FIELD_DECL)
18011 || type_dependent_expression_p (fn)
18012 || any_type_dependent_arguments_p (call_args)))
18013 ret = build_min_nt_call_vec (function, call_args);
18014 else if (!BASELINK_P (fn))
18015 ret = finish_call_expr (function, &call_args,
18016 /*disallow_virtual=*/false,
18017 /*koenig_p=*/false,
18018 complain);
18019 else
18020 ret = (build_new_method_call
18021 (instance, fn,
18022 &call_args, NULL_TREE,
18023 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
18024 /*fn_p=*/NULL,
18025 complain));
18027 else
18028 ret = finish_call_expr (function, &call_args,
18029 /*disallow_virtual=*/qualified_p,
18030 koenig_p,
18031 complain);
18033 release_tree_vector (call_args);
18035 if (ret != error_mark_node)
18037 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
18038 bool ord = CALL_EXPR_ORDERED_ARGS (t);
18039 bool rev = CALL_EXPR_REVERSE_ARGS (t);
18040 bool thk = CALL_FROM_THUNK_P (t);
18041 if (op || ord || rev || thk)
18043 function = extract_call_expr (ret);
18044 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
18045 CALL_EXPR_ORDERED_ARGS (function) = ord;
18046 CALL_EXPR_REVERSE_ARGS (function) = rev;
18047 if (thk)
18049 if (TREE_CODE (function) == CALL_EXPR)
18050 CALL_FROM_THUNK_P (function) = true;
18051 else
18052 AGGR_INIT_FROM_THUNK_P (function) = true;
18053 /* The thunk location is not interesting. */
18054 SET_EXPR_LOCATION (function, UNKNOWN_LOCATION);
18059 RETURN (ret);
18062 case COND_EXPR:
18064 tree cond = RECUR (TREE_OPERAND (t, 0));
18065 tree folded_cond = fold_non_dependent_expr (cond);
18066 tree exp1, exp2;
18068 if (TREE_CODE (folded_cond) == INTEGER_CST)
18070 if (integer_zerop (folded_cond))
18072 ++c_inhibit_evaluation_warnings;
18073 exp1 = RECUR (TREE_OPERAND (t, 1));
18074 --c_inhibit_evaluation_warnings;
18075 exp2 = RECUR (TREE_OPERAND (t, 2));
18077 else
18079 exp1 = RECUR (TREE_OPERAND (t, 1));
18080 ++c_inhibit_evaluation_warnings;
18081 exp2 = RECUR (TREE_OPERAND (t, 2));
18082 --c_inhibit_evaluation_warnings;
18084 cond = folded_cond;
18086 else
18088 exp1 = RECUR (TREE_OPERAND (t, 1));
18089 exp2 = RECUR (TREE_OPERAND (t, 2));
18092 warning_sentinel s(warn_duplicated_branches);
18093 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
18094 cond, exp1, exp2, complain));
18097 case PSEUDO_DTOR_EXPR:
18099 tree op0 = RECUR (TREE_OPERAND (t, 0));
18100 tree op1 = RECUR (TREE_OPERAND (t, 1));
18101 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
18102 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
18103 input_location));
18106 case TREE_LIST:
18108 tree purpose, value, chain;
18110 if (t == void_list_node)
18111 RETURN (t);
18113 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
18114 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
18116 /* We have pack expansions, so expand those and
18117 create a new list out of it. */
18118 tree purposevec = NULL_TREE;
18119 tree valuevec = NULL_TREE;
18120 tree chain;
18121 int i, len = -1;
18123 /* Expand the argument expressions. */
18124 if (TREE_PURPOSE (t))
18125 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
18126 complain, in_decl);
18127 if (TREE_VALUE (t))
18128 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
18129 complain, in_decl);
18131 /* Build the rest of the list. */
18132 chain = TREE_CHAIN (t);
18133 if (chain && chain != void_type_node)
18134 chain = RECUR (chain);
18136 /* Determine the number of arguments. */
18137 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
18139 len = TREE_VEC_LENGTH (purposevec);
18140 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
18142 else if (TREE_CODE (valuevec) == TREE_VEC)
18143 len = TREE_VEC_LENGTH (valuevec);
18144 else
18146 /* Since we only performed a partial substitution into
18147 the argument pack, we only RETURN (a single list
18148 node. */
18149 if (purposevec == TREE_PURPOSE (t)
18150 && valuevec == TREE_VALUE (t)
18151 && chain == TREE_CHAIN (t))
18152 RETURN (t);
18154 RETURN (tree_cons (purposevec, valuevec, chain));
18157 /* Convert the argument vectors into a TREE_LIST */
18158 i = len;
18159 while (i > 0)
18161 /* Grab the Ith values. */
18162 i--;
18163 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
18164 : NULL_TREE;
18165 value
18166 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
18167 : NULL_TREE;
18169 /* Build the list (backwards). */
18170 chain = tree_cons (purpose, value, chain);
18173 RETURN (chain);
18176 purpose = TREE_PURPOSE (t);
18177 if (purpose)
18178 purpose = RECUR (purpose);
18179 value = TREE_VALUE (t);
18180 if (value)
18181 value = RECUR (value);
18182 chain = TREE_CHAIN (t);
18183 if (chain && chain != void_type_node)
18184 chain = RECUR (chain);
18185 if (purpose == TREE_PURPOSE (t)
18186 && value == TREE_VALUE (t)
18187 && chain == TREE_CHAIN (t))
18188 RETURN (t);
18189 RETURN (tree_cons (purpose, value, chain));
18192 case COMPONENT_REF:
18194 tree object;
18195 tree object_type;
18196 tree member;
18197 tree r;
18199 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18200 args, complain, in_decl);
18201 /* Remember that there was a reference to this entity. */
18202 if (DECL_P (object)
18203 && !mark_used (object, complain) && !(complain & tf_error))
18204 RETURN (error_mark_node);
18205 object_type = TREE_TYPE (object);
18207 member = TREE_OPERAND (t, 1);
18208 if (BASELINK_P (member))
18209 member = tsubst_baselink (member,
18210 non_reference (TREE_TYPE (object)),
18211 args, complain, in_decl);
18212 else
18213 member = tsubst_copy (member, args, complain, in_decl);
18214 if (member == error_mark_node)
18215 RETURN (error_mark_node);
18217 if (TREE_CODE (member) == FIELD_DECL)
18219 r = finish_non_static_data_member (member, object, NULL_TREE);
18220 if (TREE_CODE (r) == COMPONENT_REF)
18221 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
18222 RETURN (r);
18224 else if (type_dependent_expression_p (object))
18225 /* We can't do much here. */;
18226 else if (!CLASS_TYPE_P (object_type))
18228 if (scalarish_type_p (object_type))
18230 tree s = NULL_TREE;
18231 tree dtor = member;
18233 if (TREE_CODE (dtor) == SCOPE_REF)
18235 s = TREE_OPERAND (dtor, 0);
18236 dtor = TREE_OPERAND (dtor, 1);
18238 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
18240 dtor = TREE_OPERAND (dtor, 0);
18241 if (TYPE_P (dtor))
18242 RETURN (finish_pseudo_destructor_expr
18243 (object, s, dtor, input_location));
18247 else if (TREE_CODE (member) == SCOPE_REF
18248 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
18250 /* Lookup the template functions now that we know what the
18251 scope is. */
18252 tree scope = TREE_OPERAND (member, 0);
18253 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
18254 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
18255 member = lookup_qualified_name (scope, tmpl,
18256 /*is_type_p=*/false,
18257 /*complain=*/false);
18258 if (BASELINK_P (member))
18260 BASELINK_FUNCTIONS (member)
18261 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
18262 args);
18263 member = (adjust_result_of_qualified_name_lookup
18264 (member, BINFO_TYPE (BASELINK_BINFO (member)),
18265 object_type));
18267 else
18269 qualified_name_lookup_error (scope, tmpl, member,
18270 input_location);
18271 RETURN (error_mark_node);
18274 else if (TREE_CODE (member) == SCOPE_REF
18275 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
18276 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
18278 if (complain & tf_error)
18280 if (TYPE_P (TREE_OPERAND (member, 0)))
18281 error ("%qT is not a class or namespace",
18282 TREE_OPERAND (member, 0));
18283 else
18284 error ("%qD is not a class or namespace",
18285 TREE_OPERAND (member, 0));
18287 RETURN (error_mark_node);
18290 r = finish_class_member_access_expr (object, member,
18291 /*template_p=*/false,
18292 complain);
18293 if (TREE_CODE (r) == COMPONENT_REF)
18294 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
18295 RETURN (r);
18298 case THROW_EXPR:
18299 RETURN (build_throw
18300 (RECUR (TREE_OPERAND (t, 0))));
18302 case CONSTRUCTOR:
18304 vec<constructor_elt, va_gc> *n;
18305 constructor_elt *ce;
18306 unsigned HOST_WIDE_INT idx;
18307 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18308 bool process_index_p;
18309 int newlen;
18310 bool need_copy_p = false;
18311 tree r;
18313 if (type == error_mark_node)
18314 RETURN (error_mark_node);
18316 /* We do not want to process the index of aggregate
18317 initializers as they are identifier nodes which will be
18318 looked up by digest_init. */
18319 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
18321 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
18322 newlen = vec_safe_length (n);
18323 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
18325 if (ce->index && process_index_p
18326 /* An identifier index is looked up in the type
18327 being initialized, not the current scope. */
18328 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
18329 ce->index = RECUR (ce->index);
18331 if (PACK_EXPANSION_P (ce->value))
18333 /* Substitute into the pack expansion. */
18334 ce->value = tsubst_pack_expansion (ce->value, args, complain,
18335 in_decl);
18337 if (ce->value == error_mark_node
18338 || PACK_EXPANSION_P (ce->value))
18340 else if (TREE_VEC_LENGTH (ce->value) == 1)
18341 /* Just move the argument into place. */
18342 ce->value = TREE_VEC_ELT (ce->value, 0);
18343 else
18345 /* Update the length of the final CONSTRUCTOR
18346 arguments vector, and note that we will need to
18347 copy.*/
18348 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
18349 need_copy_p = true;
18352 else
18353 ce->value = RECUR (ce->value);
18356 if (need_copy_p)
18358 vec<constructor_elt, va_gc> *old_n = n;
18360 vec_alloc (n, newlen);
18361 FOR_EACH_VEC_ELT (*old_n, idx, ce)
18363 if (TREE_CODE (ce->value) == TREE_VEC)
18365 int i, len = TREE_VEC_LENGTH (ce->value);
18366 for (i = 0; i < len; ++i)
18367 CONSTRUCTOR_APPEND_ELT (n, 0,
18368 TREE_VEC_ELT (ce->value, i));
18370 else
18371 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
18375 r = build_constructor (init_list_type_node, n);
18376 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
18378 if (TREE_HAS_CONSTRUCTOR (t))
18380 fcl_t cl = fcl_functional;
18381 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
18382 cl = fcl_c99;
18383 RETURN (finish_compound_literal (type, r, complain, cl));
18386 TREE_TYPE (r) = type;
18387 RETURN (r);
18390 case TYPEID_EXPR:
18392 tree operand_0 = TREE_OPERAND (t, 0);
18393 if (TYPE_P (operand_0))
18395 operand_0 = tsubst (operand_0, args, complain, in_decl);
18396 RETURN (get_typeid (operand_0, complain));
18398 else
18400 operand_0 = RECUR (operand_0);
18401 RETURN (build_typeid (operand_0, complain));
18405 case VAR_DECL:
18406 if (!args)
18407 RETURN (t);
18408 /* Fall through */
18410 case PARM_DECL:
18412 tree r = tsubst_copy (t, args, complain, in_decl);
18413 /* ??? We're doing a subset of finish_id_expression here. */
18414 if (VAR_P (r)
18415 && !processing_template_decl
18416 && !cp_unevaluated_operand
18417 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
18418 && CP_DECL_THREAD_LOCAL_P (r))
18420 if (tree wrap = get_tls_wrapper_fn (r))
18421 /* Replace an evaluated use of the thread_local variable with
18422 a call to its wrapper. */
18423 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
18425 else if (outer_automatic_var_p (r))
18426 r = process_outer_var_ref (r, complain);
18428 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
18429 /* If the original type was a reference, we'll be wrapped in
18430 the appropriate INDIRECT_REF. */
18431 r = convert_from_reference (r);
18432 RETURN (r);
18435 case VA_ARG_EXPR:
18437 tree op0 = RECUR (TREE_OPERAND (t, 0));
18438 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18439 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
18442 case OFFSETOF_EXPR:
18444 tree object_ptr
18445 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
18446 in_decl, /*function_p=*/false,
18447 /*integral_constant_expression_p=*/false);
18448 RETURN (finish_offsetof (object_ptr,
18449 RECUR (TREE_OPERAND (t, 0)),
18450 EXPR_LOCATION (t)));
18453 case ADDRESSOF_EXPR:
18454 RETURN (cp_build_addressof (EXPR_LOCATION (t),
18455 RECUR (TREE_OPERAND (t, 0)), complain));
18457 case TRAIT_EXPR:
18459 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
18460 complain, in_decl);
18462 tree type2 = TRAIT_EXPR_TYPE2 (t);
18463 if (type2 && TREE_CODE (type2) == TREE_LIST)
18464 type2 = RECUR (type2);
18465 else if (type2)
18466 type2 = tsubst (type2, args, complain, in_decl);
18468 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
18471 case STMT_EXPR:
18473 tree old_stmt_expr = cur_stmt_expr;
18474 tree stmt_expr = begin_stmt_expr ();
18476 cur_stmt_expr = stmt_expr;
18477 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
18478 integral_constant_expression_p);
18479 stmt_expr = finish_stmt_expr (stmt_expr, false);
18480 cur_stmt_expr = old_stmt_expr;
18482 /* If the resulting list of expression statement is empty,
18483 fold it further into void_node. */
18484 if (empty_expr_stmt_p (stmt_expr))
18485 stmt_expr = void_node;
18487 RETURN (stmt_expr);
18490 case LAMBDA_EXPR:
18492 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
18494 RETURN (build_lambda_object (r));
18497 case TARGET_EXPR:
18498 /* We can get here for a constant initializer of non-dependent type.
18499 FIXME stop folding in cp_parser_initializer_clause. */
18501 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
18502 complain);
18503 RETURN (r);
18506 case TRANSACTION_EXPR:
18507 RETURN (tsubst_expr(t, args, complain, in_decl,
18508 integral_constant_expression_p));
18510 case PAREN_EXPR:
18511 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
18513 case VEC_PERM_EXPR:
18515 tree op0 = RECUR (TREE_OPERAND (t, 0));
18516 tree op1 = RECUR (TREE_OPERAND (t, 1));
18517 tree op2 = RECUR (TREE_OPERAND (t, 2));
18518 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
18519 complain));
18522 case REQUIRES_EXPR:
18523 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
18525 case NON_LVALUE_EXPR:
18526 case VIEW_CONVERT_EXPR:
18527 /* We should only see these for location wrapper nodes, or within
18528 instantiate_non_dependent_expr (when args is NULL_TREE). */
18529 gcc_assert (location_wrapper_p (t) || args == NULL_TREE);
18530 if (location_wrapper_p (t))
18531 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)),
18532 EXPR_LOCATION (t)));
18533 /* fallthrough. */
18535 default:
18536 /* Handle Objective-C++ constructs, if appropriate. */
18538 tree subst
18539 = objcp_tsubst_copy_and_build (t, args, complain,
18540 in_decl, /*function_p=*/false);
18541 if (subst)
18542 RETURN (subst);
18544 RETURN (tsubst_copy (t, args, complain, in_decl));
18547 #undef RECUR
18548 #undef RETURN
18549 out:
18550 input_location = loc;
18551 return retval;
18554 /* Verify that the instantiated ARGS are valid. For type arguments,
18555 make sure that the type's linkage is ok. For non-type arguments,
18556 make sure they are constants if they are integral or enumerations.
18557 Emit an error under control of COMPLAIN, and return TRUE on error. */
18559 static bool
18560 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
18562 if (dependent_template_arg_p (t))
18563 return false;
18564 if (ARGUMENT_PACK_P (t))
18566 tree vec = ARGUMENT_PACK_ARGS (t);
18567 int len = TREE_VEC_LENGTH (vec);
18568 bool result = false;
18569 int i;
18571 for (i = 0; i < len; ++i)
18572 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
18573 result = true;
18574 return result;
18576 else if (TYPE_P (t))
18578 /* [basic.link]: A name with no linkage (notably, the name
18579 of a class or enumeration declared in a local scope)
18580 shall not be used to declare an entity with linkage.
18581 This implies that names with no linkage cannot be used as
18582 template arguments
18584 DR 757 relaxes this restriction for C++0x. */
18585 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
18586 : no_linkage_check (t, /*relaxed_p=*/false));
18588 if (nt)
18590 /* DR 488 makes use of a type with no linkage cause
18591 type deduction to fail. */
18592 if (complain & tf_error)
18594 if (TYPE_UNNAMED_P (nt))
18595 error ("%qT is/uses unnamed type", t);
18596 else
18597 error ("template argument for %qD uses local type %qT",
18598 tmpl, t);
18600 return true;
18602 /* In order to avoid all sorts of complications, we do not
18603 allow variably-modified types as template arguments. */
18604 else if (variably_modified_type_p (t, NULL_TREE))
18606 if (complain & tf_error)
18607 error ("%qT is a variably modified type", t);
18608 return true;
18611 /* Class template and alias template arguments should be OK. */
18612 else if (DECL_TYPE_TEMPLATE_P (t))
18614 /* A non-type argument of integral or enumerated type must be a
18615 constant. */
18616 else if (TREE_TYPE (t)
18617 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
18618 && !REFERENCE_REF_P (t)
18619 && !TREE_CONSTANT (t))
18621 if (complain & tf_error)
18622 error ("integral expression %qE is not constant", t);
18623 return true;
18625 return false;
18628 static bool
18629 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
18631 int ix, len = DECL_NTPARMS (tmpl);
18632 bool result = false;
18634 for (ix = 0; ix != len; ix++)
18636 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
18637 result = true;
18639 if (result && (complain & tf_error))
18640 error (" trying to instantiate %qD", tmpl);
18641 return result;
18644 /* We're out of SFINAE context now, so generate diagnostics for the access
18645 errors we saw earlier when instantiating D from TMPL and ARGS. */
18647 static void
18648 recheck_decl_substitution (tree d, tree tmpl, tree args)
18650 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
18651 tree type = TREE_TYPE (pattern);
18652 location_t loc = input_location;
18654 push_access_scope (d);
18655 push_deferring_access_checks (dk_no_deferred);
18656 input_location = DECL_SOURCE_LOCATION (pattern);
18657 tsubst (type, args, tf_warning_or_error, d);
18658 input_location = loc;
18659 pop_deferring_access_checks ();
18660 pop_access_scope (d);
18663 /* Instantiate the indicated variable, function, or alias template TMPL with
18664 the template arguments in TARG_PTR. */
18666 static tree
18667 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
18669 tree targ_ptr = orig_args;
18670 tree fndecl;
18671 tree gen_tmpl;
18672 tree spec;
18673 bool access_ok = true;
18675 if (tmpl == error_mark_node)
18676 return error_mark_node;
18678 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
18680 /* If this function is a clone, handle it specially. */
18681 if (DECL_CLONED_FUNCTION_P (tmpl))
18683 tree spec;
18684 tree clone;
18686 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
18687 DECL_CLONED_FUNCTION. */
18688 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
18689 targ_ptr, complain);
18690 if (spec == error_mark_node)
18691 return error_mark_node;
18693 /* Look for the clone. */
18694 FOR_EACH_CLONE (clone, spec)
18695 if (DECL_NAME (clone) == DECL_NAME (tmpl))
18696 return clone;
18697 /* We should always have found the clone by now. */
18698 gcc_unreachable ();
18699 return NULL_TREE;
18702 if (targ_ptr == error_mark_node)
18703 return error_mark_node;
18705 /* Check to see if we already have this specialization. */
18706 gen_tmpl = most_general_template (tmpl);
18707 if (TMPL_ARGS_DEPTH (targ_ptr)
18708 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
18709 /* targ_ptr only has the innermost template args, so add the outer ones
18710 from tmpl, which could be either a partial instantiation or gen_tmpl (in
18711 the case of a non-dependent call within a template definition). */
18712 targ_ptr = (add_outermost_template_args
18713 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
18714 targ_ptr));
18716 /* It would be nice to avoid hashing here and then again in tsubst_decl,
18717 but it doesn't seem to be on the hot path. */
18718 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
18720 gcc_assert (tmpl == gen_tmpl
18721 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
18722 == spec)
18723 || fndecl == NULL_TREE);
18725 if (spec != NULL_TREE)
18727 if (FNDECL_HAS_ACCESS_ERRORS (spec))
18729 if (complain & tf_error)
18730 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
18731 return error_mark_node;
18733 return spec;
18736 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
18737 complain))
18738 return error_mark_node;
18740 /* We are building a FUNCTION_DECL, during which the access of its
18741 parameters and return types have to be checked. However this
18742 FUNCTION_DECL which is the desired context for access checking
18743 is not built yet. We solve this chicken-and-egg problem by
18744 deferring all checks until we have the FUNCTION_DECL. */
18745 push_deferring_access_checks (dk_deferred);
18747 /* Instantiation of the function happens in the context of the function
18748 template, not the context of the overload resolution we're doing. */
18749 push_to_top_level ();
18750 /* If there are dependent arguments, e.g. because we're doing partial
18751 ordering, make sure processing_template_decl stays set. */
18752 if (uses_template_parms (targ_ptr))
18753 ++processing_template_decl;
18754 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18756 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
18757 complain, gen_tmpl, true);
18758 push_nested_class (ctx);
18761 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
18763 fndecl = NULL_TREE;
18764 if (VAR_P (pattern))
18766 /* We need to determine if we're using a partial or explicit
18767 specialization now, because the type of the variable could be
18768 different. */
18769 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
18770 tree elt = most_specialized_partial_spec (tid, complain);
18771 if (elt == error_mark_node)
18772 pattern = error_mark_node;
18773 else if (elt)
18775 tree partial_tmpl = TREE_VALUE (elt);
18776 tree partial_args = TREE_PURPOSE (elt);
18777 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
18778 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
18782 /* Substitute template parameters to obtain the specialization. */
18783 if (fndecl == NULL_TREE)
18784 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
18785 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18786 pop_nested_class ();
18787 pop_from_top_level ();
18789 if (fndecl == error_mark_node)
18791 pop_deferring_access_checks ();
18792 return error_mark_node;
18795 /* The DECL_TI_TEMPLATE should always be the immediate parent
18796 template, not the most general template. */
18797 DECL_TI_TEMPLATE (fndecl) = tmpl;
18798 DECL_TI_ARGS (fndecl) = targ_ptr;
18800 /* Now we know the specialization, compute access previously
18801 deferred. Do no access control for inheriting constructors,
18802 as we already checked access for the inherited constructor. */
18803 if (!(flag_new_inheriting_ctors
18804 && DECL_INHERITED_CTOR (fndecl)))
18806 push_access_scope (fndecl);
18807 if (!perform_deferred_access_checks (complain))
18808 access_ok = false;
18809 pop_access_scope (fndecl);
18811 pop_deferring_access_checks ();
18813 /* If we've just instantiated the main entry point for a function,
18814 instantiate all the alternate entry points as well. We do this
18815 by cloning the instantiation of the main entry point, not by
18816 instantiating the template clones. */
18817 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
18818 clone_function_decl (fndecl, /*update_methods=*/false);
18820 if (!access_ok)
18822 if (!(complain & tf_error))
18824 /* Remember to reinstantiate when we're out of SFINAE so the user
18825 can see the errors. */
18826 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
18828 return error_mark_node;
18830 return fndecl;
18833 /* Wrapper for instantiate_template_1. */
18835 tree
18836 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
18838 tree ret;
18839 timevar_push (TV_TEMPLATE_INST);
18840 ret = instantiate_template_1 (tmpl, orig_args, complain);
18841 timevar_pop (TV_TEMPLATE_INST);
18842 return ret;
18845 /* Instantiate the alias template TMPL with ARGS. Also push a template
18846 instantiation level, which instantiate_template doesn't do because
18847 functions and variables have sufficient context established by the
18848 callers. */
18850 static tree
18851 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
18853 struct pending_template *old_last_pend = last_pending_template;
18854 struct tinst_level *old_error_tinst = last_error_tinst_level;
18855 if (tmpl == error_mark_node || args == error_mark_node)
18856 return error_mark_node;
18857 tree tinst = build_tree_list (tmpl, args);
18858 if (!push_tinst_level (tinst))
18860 ggc_free (tinst);
18861 return error_mark_node;
18864 args =
18865 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
18866 args, tmpl, complain,
18867 /*require_all_args=*/true,
18868 /*use_default_args=*/true);
18870 tree r = instantiate_template (tmpl, args, complain);
18871 pop_tinst_level ();
18872 /* We can't free this if a pending_template entry or last_error_tinst_level
18873 is pointing at it. */
18874 if (last_pending_template == old_last_pend
18875 && last_error_tinst_level == old_error_tinst)
18876 ggc_free (tinst);
18878 return r;
18881 /* PARM is a template parameter pack for FN. Returns true iff
18882 PARM is used in a deducible way in the argument list of FN. */
18884 static bool
18885 pack_deducible_p (tree parm, tree fn)
18887 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
18888 for (; t; t = TREE_CHAIN (t))
18890 tree type = TREE_VALUE (t);
18891 tree packs;
18892 if (!PACK_EXPANSION_P (type))
18893 continue;
18894 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
18895 packs; packs = TREE_CHAIN (packs))
18896 if (template_args_equal (TREE_VALUE (packs), parm))
18898 /* The template parameter pack is used in a function parameter
18899 pack. If this is the end of the parameter list, the
18900 template parameter pack is deducible. */
18901 if (TREE_CHAIN (t) == void_list_node)
18902 return true;
18903 else
18904 /* Otherwise, not. Well, it could be deduced from
18905 a non-pack parameter, but doing so would end up with
18906 a deduction mismatch, so don't bother. */
18907 return false;
18910 /* The template parameter pack isn't used in any function parameter
18911 packs, but it might be used deeper, e.g. tuple<Args...>. */
18912 return true;
18915 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
18916 NARGS elements of the arguments that are being used when calling
18917 it. TARGS is a vector into which the deduced template arguments
18918 are placed.
18920 Returns either a FUNCTION_DECL for the matching specialization of FN or
18921 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
18922 true, diagnostics will be printed to explain why it failed.
18924 If FN is a conversion operator, or we are trying to produce a specific
18925 specialization, RETURN_TYPE is the return type desired.
18927 The EXPLICIT_TARGS are explicit template arguments provided via a
18928 template-id.
18930 The parameter STRICT is one of:
18932 DEDUCE_CALL:
18933 We are deducing arguments for a function call, as in
18934 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
18935 deducing arguments for a call to the result of a conversion
18936 function template, as in [over.call.object].
18938 DEDUCE_CONV:
18939 We are deducing arguments for a conversion function, as in
18940 [temp.deduct.conv].
18942 DEDUCE_EXACT:
18943 We are deducing arguments when doing an explicit instantiation
18944 as in [temp.explicit], when determining an explicit specialization
18945 as in [temp.expl.spec], or when taking the address of a function
18946 template, as in [temp.deduct.funcaddr]. */
18948 tree
18949 fn_type_unification (tree fn,
18950 tree explicit_targs,
18951 tree targs,
18952 const tree *args,
18953 unsigned int nargs,
18954 tree return_type,
18955 unification_kind_t strict,
18956 int flags,
18957 bool explain_p,
18958 bool decltype_p)
18960 tree parms;
18961 tree fntype;
18962 tree decl = NULL_TREE;
18963 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
18964 bool ok;
18965 static int deduction_depth;
18966 struct pending_template *old_last_pend = last_pending_template;
18967 struct tinst_level *old_error_tinst = last_error_tinst_level;
18969 tree orig_fn = fn;
18970 if (flag_new_inheriting_ctors)
18971 fn = strip_inheriting_ctors (fn);
18973 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
18974 tree tinst;
18975 tree r = error_mark_node;
18977 tree full_targs = targs;
18978 if (TMPL_ARGS_DEPTH (targs)
18979 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
18980 full_targs = (add_outermost_template_args
18981 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
18982 targs));
18984 if (decltype_p)
18985 complain |= tf_decltype;
18987 /* In C++0x, it's possible to have a function template whose type depends
18988 on itself recursively. This is most obvious with decltype, but can also
18989 occur with enumeration scope (c++/48969). So we need to catch infinite
18990 recursion and reject the substitution at deduction time; this function
18991 will return error_mark_node for any repeated substitution.
18993 This also catches excessive recursion such as when f<N> depends on
18994 f<N-1> across all integers, and returns error_mark_node for all the
18995 substitutions back up to the initial one.
18997 This is, of course, not reentrant. */
18998 if (excessive_deduction_depth)
18999 return error_mark_node;
19000 tinst = build_tree_list (fn, NULL_TREE);
19001 ++deduction_depth;
19003 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
19005 fntype = TREE_TYPE (fn);
19006 if (explicit_targs)
19008 /* [temp.deduct]
19010 The specified template arguments must match the template
19011 parameters in kind (i.e., type, nontype, template), and there
19012 must not be more arguments than there are parameters;
19013 otherwise type deduction fails.
19015 Nontype arguments must match the types of the corresponding
19016 nontype template parameters, or must be convertible to the
19017 types of the corresponding nontype parameters as specified in
19018 _temp.arg.nontype_, otherwise type deduction fails.
19020 All references in the function type of the function template
19021 to the corresponding template parameters are replaced by the
19022 specified template argument values. If a substitution in a
19023 template parameter or in the function type of the function
19024 template results in an invalid type, type deduction fails. */
19025 int i, len = TREE_VEC_LENGTH (tparms);
19026 location_t loc = input_location;
19027 bool incomplete = false;
19029 if (explicit_targs == error_mark_node)
19030 goto fail;
19032 if (TMPL_ARGS_DEPTH (explicit_targs)
19033 < TMPL_ARGS_DEPTH (full_targs))
19034 explicit_targs = add_outermost_template_args (full_targs,
19035 explicit_targs);
19037 /* Adjust any explicit template arguments before entering the
19038 substitution context. */
19039 explicit_targs
19040 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
19041 complain,
19042 /*require_all_args=*/false,
19043 /*use_default_args=*/false));
19044 if (explicit_targs == error_mark_node)
19045 goto fail;
19047 /* Substitute the explicit args into the function type. This is
19048 necessary so that, for instance, explicitly declared function
19049 arguments can match null pointed constants. If we were given
19050 an incomplete set of explicit args, we must not do semantic
19051 processing during substitution as we could create partial
19052 instantiations. */
19053 for (i = 0; i < len; i++)
19055 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
19056 bool parameter_pack = false;
19057 tree targ = TREE_VEC_ELT (explicit_targs, i);
19059 /* Dig out the actual parm. */
19060 if (TREE_CODE (parm) == TYPE_DECL
19061 || TREE_CODE (parm) == TEMPLATE_DECL)
19063 parm = TREE_TYPE (parm);
19064 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
19066 else if (TREE_CODE (parm) == PARM_DECL)
19068 parm = DECL_INITIAL (parm);
19069 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
19072 if (!parameter_pack && targ == NULL_TREE)
19073 /* No explicit argument for this template parameter. */
19074 incomplete = true;
19076 if (parameter_pack && pack_deducible_p (parm, fn))
19078 /* Mark the argument pack as "incomplete". We could
19079 still deduce more arguments during unification.
19080 We remove this mark in type_unification_real. */
19081 if (targ)
19083 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
19084 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
19085 = ARGUMENT_PACK_ARGS (targ);
19088 /* We have some incomplete argument packs. */
19089 incomplete = true;
19093 TREE_VALUE (tinst) = explicit_targs;
19094 if (!push_tinst_level (tinst))
19096 excessive_deduction_depth = true;
19097 goto fail;
19099 processing_template_decl += incomplete;
19100 input_location = DECL_SOURCE_LOCATION (fn);
19101 /* Ignore any access checks; we'll see them again in
19102 instantiate_template and they might have the wrong
19103 access path at this point. */
19104 push_deferring_access_checks (dk_deferred);
19105 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
19106 complain | tf_partial | tf_fndecl_type, NULL_TREE);
19107 pop_deferring_access_checks ();
19108 input_location = loc;
19109 processing_template_decl -= incomplete;
19110 pop_tinst_level ();
19112 if (fntype == error_mark_node)
19113 goto fail;
19115 /* Place the explicitly specified arguments in TARGS. */
19116 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
19117 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
19118 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
19121 /* Never do unification on the 'this' parameter. */
19122 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
19124 if (return_type && strict == DEDUCE_CALL)
19126 /* We're deducing for a call to the result of a template conversion
19127 function. The parms we really want are in return_type. */
19128 if (POINTER_TYPE_P (return_type))
19129 return_type = TREE_TYPE (return_type);
19130 parms = TYPE_ARG_TYPES (return_type);
19132 else if (return_type)
19134 tree *new_args;
19136 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
19137 new_args = XALLOCAVEC (tree, nargs + 1);
19138 new_args[0] = return_type;
19139 memcpy (new_args + 1, args, nargs * sizeof (tree));
19140 args = new_args;
19141 ++nargs;
19144 /* We allow incomplete unification without an error message here
19145 because the standard doesn't seem to explicitly prohibit it. Our
19146 callers must be ready to deal with unification failures in any
19147 event. */
19149 TREE_VALUE (tinst) = targs;
19150 /* If we aren't explaining yet, push tinst context so we can see where
19151 any errors (e.g. from class instantiations triggered by instantiation
19152 of default template arguments) come from. If we are explaining, this
19153 context is redundant. */
19154 if (!explain_p && !push_tinst_level (tinst))
19156 excessive_deduction_depth = true;
19157 goto fail;
19160 /* type_unification_real will pass back any access checks from default
19161 template argument substitution. */
19162 vec<deferred_access_check, va_gc> *checks;
19163 checks = NULL;
19165 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19166 full_targs, parms, args, nargs, /*subr=*/0,
19167 strict, flags, &checks, explain_p);
19168 if (!explain_p)
19169 pop_tinst_level ();
19170 if (!ok)
19171 goto fail;
19173 /* Now that we have bindings for all of the template arguments,
19174 ensure that the arguments deduced for the template template
19175 parameters have compatible template parameter lists. We cannot
19176 check this property before we have deduced all template
19177 arguments, because the template parameter types of a template
19178 template parameter might depend on prior template parameters
19179 deduced after the template template parameter. The following
19180 ill-formed example illustrates this issue:
19182 template<typename T, template<T> class C> void f(C<5>, T);
19184 template<int N> struct X {};
19186 void g() {
19187 f(X<5>(), 5l); // error: template argument deduction fails
19190 The template parameter list of 'C' depends on the template type
19191 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
19192 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
19193 time that we deduce 'C'. */
19194 if (!template_template_parm_bindings_ok_p
19195 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
19197 unify_inconsistent_template_template_parameters (explain_p);
19198 goto fail;
19201 /* All is well so far. Now, check:
19203 [temp.deduct]
19205 When all template arguments have been deduced, all uses of
19206 template parameters in nondeduced contexts are replaced with
19207 the corresponding deduced argument values. If the
19208 substitution results in an invalid type, as described above,
19209 type deduction fails. */
19210 TREE_VALUE (tinst) = targs;
19211 if (!push_tinst_level (tinst))
19213 excessive_deduction_depth = true;
19214 goto fail;
19217 /* Also collect access checks from the instantiation. */
19218 reopen_deferring_access_checks (checks);
19220 decl = instantiate_template (fn, targs, complain);
19222 checks = get_deferred_access_checks ();
19223 pop_deferring_access_checks ();
19225 pop_tinst_level ();
19227 if (decl == error_mark_node)
19228 goto fail;
19230 /* Now perform any access checks encountered during substitution. */
19231 push_access_scope (decl);
19232 ok = perform_access_checks (checks, complain);
19233 pop_access_scope (decl);
19234 if (!ok)
19235 goto fail;
19237 /* If we're looking for an exact match, check that what we got
19238 is indeed an exact match. It might not be if some template
19239 parameters are used in non-deduced contexts. But don't check
19240 for an exact match if we have dependent template arguments;
19241 in that case we're doing partial ordering, and we already know
19242 that we have two candidates that will provide the actual type. */
19243 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
19245 tree substed = TREE_TYPE (decl);
19246 unsigned int i;
19248 tree sarg
19249 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
19250 if (return_type)
19251 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
19252 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
19253 if (!same_type_p (args[i], TREE_VALUE (sarg)))
19255 unify_type_mismatch (explain_p, args[i],
19256 TREE_VALUE (sarg));
19257 goto fail;
19261 /* After doing deduction with the inherited constructor, actually return an
19262 instantiation of the inheriting constructor. */
19263 if (orig_fn != fn)
19264 decl = instantiate_template (orig_fn, targs, complain);
19266 r = decl;
19268 fail:
19269 --deduction_depth;
19270 if (excessive_deduction_depth)
19272 if (deduction_depth == 0)
19273 /* Reset once we're all the way out. */
19274 excessive_deduction_depth = false;
19277 /* We can't free this if a pending_template entry or last_error_tinst_level
19278 is pointing at it. */
19279 if (last_pending_template == old_last_pend
19280 && last_error_tinst_level == old_error_tinst)
19281 ggc_free (tinst);
19283 return r;
19286 /* Adjust types before performing type deduction, as described in
19287 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
19288 sections are symmetric. PARM is the type of a function parameter
19289 or the return type of the conversion function. ARG is the type of
19290 the argument passed to the call, or the type of the value
19291 initialized with the result of the conversion function.
19292 ARG_EXPR is the original argument expression, which may be null. */
19294 static int
19295 maybe_adjust_types_for_deduction (unification_kind_t strict,
19296 tree* parm,
19297 tree* arg,
19298 tree arg_expr)
19300 int result = 0;
19302 switch (strict)
19304 case DEDUCE_CALL:
19305 break;
19307 case DEDUCE_CONV:
19308 /* Swap PARM and ARG throughout the remainder of this
19309 function; the handling is precisely symmetric since PARM
19310 will initialize ARG rather than vice versa. */
19311 std::swap (parm, arg);
19312 break;
19314 case DEDUCE_EXACT:
19315 /* Core issue #873: Do the DR606 thing (see below) for these cases,
19316 too, but here handle it by stripping the reference from PARM
19317 rather than by adding it to ARG. */
19318 if (TREE_CODE (*parm) == REFERENCE_TYPE
19319 && TYPE_REF_IS_RVALUE (*parm)
19320 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19321 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19322 && TREE_CODE (*arg) == REFERENCE_TYPE
19323 && !TYPE_REF_IS_RVALUE (*arg))
19324 *parm = TREE_TYPE (*parm);
19325 /* Nothing else to do in this case. */
19326 return 0;
19328 default:
19329 gcc_unreachable ();
19332 if (TREE_CODE (*parm) != REFERENCE_TYPE)
19334 /* [temp.deduct.call]
19336 If P is not a reference type:
19338 --If A is an array type, the pointer type produced by the
19339 array-to-pointer standard conversion (_conv.array_) is
19340 used in place of A for type deduction; otherwise,
19342 --If A is a function type, the pointer type produced by
19343 the function-to-pointer standard conversion
19344 (_conv.func_) is used in place of A for type deduction;
19345 otherwise,
19347 --If A is a cv-qualified type, the top level
19348 cv-qualifiers of A's type are ignored for type
19349 deduction. */
19350 if (TREE_CODE (*arg) == ARRAY_TYPE)
19351 *arg = build_pointer_type (TREE_TYPE (*arg));
19352 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
19353 *arg = build_pointer_type (*arg);
19354 else
19355 *arg = TYPE_MAIN_VARIANT (*arg);
19358 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
19359 reference to a cv-unqualified template parameter that does not represent a
19360 template parameter of a class template (during class template argument
19361 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
19362 an lvalue, the type "lvalue reference to A" is used in place of A for type
19363 deduction. */
19364 if (TREE_CODE (*parm) == REFERENCE_TYPE
19365 && TYPE_REF_IS_RVALUE (*parm)
19366 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19367 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
19368 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19369 && (arg_expr ? lvalue_p (arg_expr)
19370 /* try_one_overload doesn't provide an arg_expr, but
19371 functions are always lvalues. */
19372 : TREE_CODE (*arg) == FUNCTION_TYPE))
19373 *arg = build_reference_type (*arg);
19375 /* [temp.deduct.call]
19377 If P is a cv-qualified type, the top level cv-qualifiers
19378 of P's type are ignored for type deduction. If P is a
19379 reference type, the type referred to by P is used for
19380 type deduction. */
19381 *parm = TYPE_MAIN_VARIANT (*parm);
19382 if (TREE_CODE (*parm) == REFERENCE_TYPE)
19384 *parm = TREE_TYPE (*parm);
19385 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
19388 /* DR 322. For conversion deduction, remove a reference type on parm
19389 too (which has been swapped into ARG). */
19390 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
19391 *arg = TREE_TYPE (*arg);
19393 return result;
19396 /* Subroutine of unify_one_argument. PARM is a function parameter of a
19397 template which does contain any deducible template parameters; check if
19398 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
19399 unify_one_argument. */
19401 static int
19402 check_non_deducible_conversion (tree parm, tree arg, int strict,
19403 int flags, bool explain_p)
19405 tree type;
19407 if (!TYPE_P (arg))
19408 type = TREE_TYPE (arg);
19409 else
19410 type = arg;
19412 if (same_type_p (parm, type))
19413 return unify_success (explain_p);
19415 if (strict == DEDUCE_CONV)
19417 if (can_convert_arg (type, parm, NULL_TREE, flags,
19418 explain_p ? tf_warning_or_error : tf_none))
19419 return unify_success (explain_p);
19421 else if (strict != DEDUCE_EXACT)
19423 if (can_convert_arg (parm, type,
19424 TYPE_P (arg) ? NULL_TREE : arg,
19425 flags, explain_p ? tf_warning_or_error : tf_none))
19426 return unify_success (explain_p);
19429 if (strict == DEDUCE_EXACT)
19430 return unify_type_mismatch (explain_p, parm, arg);
19431 else
19432 return unify_arg_conversion (explain_p, parm, type, arg);
19435 static bool uses_deducible_template_parms (tree type);
19437 /* Returns true iff the expression EXPR is one from which a template
19438 argument can be deduced. In other words, if it's an undecorated
19439 use of a template non-type parameter. */
19441 static bool
19442 deducible_expression (tree expr)
19444 /* Strip implicit conversions. */
19445 while (CONVERT_EXPR_P (expr))
19446 expr = TREE_OPERAND (expr, 0);
19447 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
19450 /* Returns true iff the array domain DOMAIN uses a template parameter in a
19451 deducible way; that is, if it has a max value of <PARM> - 1. */
19453 static bool
19454 deducible_array_bound (tree domain)
19456 if (domain == NULL_TREE)
19457 return false;
19459 tree max = TYPE_MAX_VALUE (domain);
19460 if (TREE_CODE (max) != MINUS_EXPR)
19461 return false;
19463 return deducible_expression (TREE_OPERAND (max, 0));
19466 /* Returns true iff the template arguments ARGS use a template parameter
19467 in a deducible way. */
19469 static bool
19470 deducible_template_args (tree args)
19472 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
19474 bool deducible;
19475 tree elt = TREE_VEC_ELT (args, i);
19476 if (ARGUMENT_PACK_P (elt))
19477 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
19478 else
19480 if (PACK_EXPANSION_P (elt))
19481 elt = PACK_EXPANSION_PATTERN (elt);
19482 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
19483 deducible = true;
19484 else if (TYPE_P (elt))
19485 deducible = uses_deducible_template_parms (elt);
19486 else
19487 deducible = deducible_expression (elt);
19489 if (deducible)
19490 return true;
19492 return false;
19495 /* Returns true iff TYPE contains any deducible references to template
19496 parameters, as per 14.8.2.5. */
19498 static bool
19499 uses_deducible_template_parms (tree type)
19501 if (PACK_EXPANSION_P (type))
19502 type = PACK_EXPANSION_PATTERN (type);
19504 /* T
19505 cv-list T
19506 TT<T>
19507 TT<i>
19508 TT<> */
19509 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
19510 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
19511 return true;
19513 /* T*
19515 T&& */
19516 if (POINTER_TYPE_P (type))
19517 return uses_deducible_template_parms (TREE_TYPE (type));
19519 /* T[integer-constant ]
19520 type [i] */
19521 if (TREE_CODE (type) == ARRAY_TYPE)
19522 return (uses_deducible_template_parms (TREE_TYPE (type))
19523 || deducible_array_bound (TYPE_DOMAIN (type)));
19525 /* T type ::*
19526 type T::*
19527 T T::*
19528 T (type ::*)()
19529 type (T::*)()
19530 type (type ::*)(T)
19531 type (T::*)(T)
19532 T (type ::*)(T)
19533 T (T::*)()
19534 T (T::*)(T) */
19535 if (TYPE_PTRMEM_P (type))
19536 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
19537 || (uses_deducible_template_parms
19538 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
19540 /* template-name <T> (where template-name refers to a class template)
19541 template-name <i> (where template-name refers to a class template) */
19542 if (CLASS_TYPE_P (type)
19543 && CLASSTYPE_TEMPLATE_INFO (type)
19544 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
19545 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
19546 (CLASSTYPE_TI_ARGS (type)));
19548 /* type (T)
19550 T(T) */
19551 if (TREE_CODE (type) == FUNCTION_TYPE
19552 || TREE_CODE (type) == METHOD_TYPE)
19554 if (uses_deducible_template_parms (TREE_TYPE (type)))
19555 return true;
19556 tree parm = TYPE_ARG_TYPES (type);
19557 if (TREE_CODE (type) == METHOD_TYPE)
19558 parm = TREE_CHAIN (parm);
19559 for (; parm; parm = TREE_CHAIN (parm))
19560 if (uses_deducible_template_parms (TREE_VALUE (parm)))
19561 return true;
19564 return false;
19567 /* Subroutine of type_unification_real and unify_pack_expansion to
19568 handle unification of a single P/A pair. Parameters are as
19569 for those functions. */
19571 static int
19572 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
19573 int subr, unification_kind_t strict,
19574 bool explain_p)
19576 tree arg_expr = NULL_TREE;
19577 int arg_strict;
19579 if (arg == error_mark_node || parm == error_mark_node)
19580 return unify_invalid (explain_p);
19581 if (arg == unknown_type_node)
19582 /* We can't deduce anything from this, but we might get all the
19583 template args from other function args. */
19584 return unify_success (explain_p);
19586 /* Implicit conversions (Clause 4) will be performed on a function
19587 argument to convert it to the type of the corresponding function
19588 parameter if the parameter type contains no template-parameters that
19589 participate in template argument deduction. */
19590 if (strict != DEDUCE_EXACT
19591 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
19592 /* For function parameters with no deducible template parameters,
19593 just return. We'll check non-dependent conversions later. */
19594 return unify_success (explain_p);
19596 switch (strict)
19598 case DEDUCE_CALL:
19599 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
19600 | UNIFY_ALLOW_MORE_CV_QUAL
19601 | UNIFY_ALLOW_DERIVED);
19602 break;
19604 case DEDUCE_CONV:
19605 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
19606 break;
19608 case DEDUCE_EXACT:
19609 arg_strict = UNIFY_ALLOW_NONE;
19610 break;
19612 default:
19613 gcc_unreachable ();
19616 /* We only do these transformations if this is the top-level
19617 parameter_type_list in a call or declaration matching; in other
19618 situations (nested function declarators, template argument lists) we
19619 won't be comparing a type to an expression, and we don't do any type
19620 adjustments. */
19621 if (!subr)
19623 if (!TYPE_P (arg))
19625 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
19626 if (type_unknown_p (arg))
19628 /* [temp.deduct.type] A template-argument can be
19629 deduced from a pointer to function or pointer
19630 to member function argument if the set of
19631 overloaded functions does not contain function
19632 templates and at most one of a set of
19633 overloaded functions provides a unique
19634 match. */
19635 resolve_overloaded_unification (tparms, targs, parm,
19636 arg, strict,
19637 arg_strict, explain_p);
19638 /* If a unique match was not found, this is a
19639 non-deduced context, so we still succeed. */
19640 return unify_success (explain_p);
19643 arg_expr = arg;
19644 arg = unlowered_expr_type (arg);
19645 if (arg == error_mark_node)
19646 return unify_invalid (explain_p);
19649 arg_strict |=
19650 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
19652 else
19653 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
19654 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
19655 return unify_template_argument_mismatch (explain_p, parm, arg);
19657 /* For deduction from an init-list we need the actual list. */
19658 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
19659 arg = arg_expr;
19660 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
19663 /* for_each_template_parm callback that always returns 0. */
19665 static int
19666 zero_r (tree, void *)
19668 return 0;
19671 /* for_each_template_parm any_fn callback to handle deduction of a template
19672 type argument from the type of an array bound. */
19674 static int
19675 array_deduction_r (tree t, void *data)
19677 tree_pair_p d = (tree_pair_p)data;
19678 tree &tparms = d->purpose;
19679 tree &targs = d->value;
19681 if (TREE_CODE (t) == ARRAY_TYPE)
19682 if (tree dom = TYPE_DOMAIN (t))
19683 if (tree max = TYPE_MAX_VALUE (dom))
19685 if (TREE_CODE (max) == MINUS_EXPR)
19686 max = TREE_OPERAND (max, 0);
19687 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
19688 unify (tparms, targs, TREE_TYPE (max), size_type_node,
19689 UNIFY_ALLOW_NONE, /*explain*/false);
19692 /* Keep walking. */
19693 return 0;
19696 /* Try to deduce any not-yet-deduced template type arguments from the type of
19697 an array bound. This is handled separately from unify because 14.8.2.5 says
19698 "The type of a type parameter is only deduced from an array bound if it is
19699 not otherwise deduced." */
19701 static void
19702 try_array_deduction (tree tparms, tree targs, tree parm)
19704 tree_pair_s data = { tparms, targs };
19705 hash_set<tree> visited;
19706 for_each_template_parm (parm, zero_r, &data, &visited,
19707 /*nondeduced*/false, array_deduction_r);
19710 /* Most parms like fn_type_unification.
19712 If SUBR is 1, we're being called recursively (to unify the
19713 arguments of a function or method parameter of a function
19714 template).
19716 CHECKS is a pointer to a vector of access checks encountered while
19717 substituting default template arguments. */
19719 static int
19720 type_unification_real (tree tparms,
19721 tree full_targs,
19722 tree xparms,
19723 const tree *xargs,
19724 unsigned int xnargs,
19725 int subr,
19726 unification_kind_t strict,
19727 int flags,
19728 vec<deferred_access_check, va_gc> **checks,
19729 bool explain_p)
19731 tree parm, arg;
19732 int i;
19733 int ntparms = TREE_VEC_LENGTH (tparms);
19734 int saw_undeduced = 0;
19735 tree parms;
19736 const tree *args;
19737 unsigned int nargs;
19738 unsigned int ia;
19740 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
19741 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
19742 gcc_assert (ntparms > 0);
19744 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
19746 /* Reset the number of non-defaulted template arguments contained
19747 in TARGS. */
19748 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
19750 again:
19751 parms = xparms;
19752 args = xargs;
19753 nargs = xnargs;
19755 ia = 0;
19756 while (parms && parms != void_list_node
19757 && ia < nargs)
19759 parm = TREE_VALUE (parms);
19761 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19762 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
19763 /* For a function parameter pack that occurs at the end of the
19764 parameter-declaration-list, the type A of each remaining
19765 argument of the call is compared with the type P of the
19766 declarator-id of the function parameter pack. */
19767 break;
19769 parms = TREE_CHAIN (parms);
19771 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19772 /* For a function parameter pack that does not occur at the
19773 end of the parameter-declaration-list, the type of the
19774 parameter pack is a non-deduced context. */
19775 continue;
19777 arg = args[ia];
19778 ++ia;
19780 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
19781 explain_p))
19782 return 1;
19785 if (parms
19786 && parms != void_list_node
19787 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
19789 /* Unify the remaining arguments with the pack expansion type. */
19790 tree argvec;
19791 tree parmvec = make_tree_vec (1);
19793 /* Allocate a TREE_VEC and copy in all of the arguments */
19794 argvec = make_tree_vec (nargs - ia);
19795 for (i = 0; ia < nargs; ++ia, ++i)
19796 TREE_VEC_ELT (argvec, i) = args[ia];
19798 /* Copy the parameter into parmvec. */
19799 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
19800 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
19801 /*subr=*/subr, explain_p))
19802 return 1;
19804 /* Advance to the end of the list of parameters. */
19805 parms = TREE_CHAIN (parms);
19808 /* Fail if we've reached the end of the parm list, and more args
19809 are present, and the parm list isn't variadic. */
19810 if (ia < nargs && parms == void_list_node)
19811 return unify_too_many_arguments (explain_p, nargs, ia);
19812 /* Fail if parms are left and they don't have default values and
19813 they aren't all deduced as empty packs (c++/57397). This is
19814 consistent with sufficient_parms_p. */
19815 if (parms && parms != void_list_node
19816 && TREE_PURPOSE (parms) == NULL_TREE)
19818 unsigned int count = nargs;
19819 tree p = parms;
19820 bool type_pack_p;
19823 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
19824 if (!type_pack_p)
19825 count++;
19826 p = TREE_CHAIN (p);
19828 while (p && p != void_list_node);
19829 if (count != nargs)
19830 return unify_too_few_arguments (explain_p, ia, count,
19831 type_pack_p);
19834 if (!subr)
19836 tsubst_flags_t complain = (explain_p
19837 ? tf_warning_or_error
19838 : tf_none);
19839 bool tried_array_deduction = (cxx_dialect < cxx17);
19841 for (i = 0; i < ntparms; i++)
19843 tree targ = TREE_VEC_ELT (targs, i);
19844 tree tparm = TREE_VEC_ELT (tparms, i);
19846 /* Clear the "incomplete" flags on all argument packs now so that
19847 substituting them into later default arguments works. */
19848 if (targ && ARGUMENT_PACK_P (targ))
19850 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
19851 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
19854 if (targ || tparm == error_mark_node)
19855 continue;
19856 tparm = TREE_VALUE (tparm);
19858 if (TREE_CODE (tparm) == TYPE_DECL
19859 && !tried_array_deduction)
19861 try_array_deduction (tparms, targs, xparms);
19862 tried_array_deduction = true;
19863 if (TREE_VEC_ELT (targs, i))
19864 continue;
19867 /* If this is an undeduced nontype parameter that depends on
19868 a type parameter, try another pass; its type may have been
19869 deduced from a later argument than the one from which
19870 this parameter can be deduced. */
19871 if (TREE_CODE (tparm) == PARM_DECL
19872 && uses_template_parms (TREE_TYPE (tparm))
19873 && saw_undeduced < 2)
19875 saw_undeduced = 1;
19876 continue;
19879 /* Core issue #226 (C++0x) [temp.deduct]:
19881 If a template argument has not been deduced, its
19882 default template argument, if any, is used.
19884 When we are in C++98 mode, TREE_PURPOSE will either
19885 be NULL_TREE or ERROR_MARK_NODE, so we do not need
19886 to explicitly check cxx_dialect here. */
19887 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
19888 /* OK, there is a default argument. Wait until after the
19889 conversion check to do substitution. */
19890 continue;
19892 /* If the type parameter is a parameter pack, then it will
19893 be deduced to an empty parameter pack. */
19894 if (template_parameter_pack_p (tparm))
19896 tree arg;
19898 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
19900 arg = make_node (NONTYPE_ARGUMENT_PACK);
19901 TREE_CONSTANT (arg) = 1;
19903 else
19904 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
19906 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
19908 TREE_VEC_ELT (targs, i) = arg;
19909 continue;
19912 return unify_parameter_deduction_failure (explain_p, tparm);
19915 /* DR 1391: All parameters have args, now check non-dependent parms for
19916 convertibility. */
19917 if (saw_undeduced < 2)
19918 for (ia = 0, parms = xparms, args = xargs, nargs = xnargs;
19919 parms && parms != void_list_node && ia < nargs; )
19921 parm = TREE_VALUE (parms);
19923 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19924 && (!TREE_CHAIN (parms)
19925 || TREE_CHAIN (parms) == void_list_node))
19926 /* For a function parameter pack that occurs at the end of the
19927 parameter-declaration-list, the type A of each remaining
19928 argument of the call is compared with the type P of the
19929 declarator-id of the function parameter pack. */
19930 break;
19932 parms = TREE_CHAIN (parms);
19934 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19935 /* For a function parameter pack that does not occur at the
19936 end of the parameter-declaration-list, the type of the
19937 parameter pack is a non-deduced context. */
19938 continue;
19940 arg = args[ia];
19941 ++ia;
19943 if (uses_template_parms (parm))
19944 continue;
19945 if (check_non_deducible_conversion (parm, arg, strict, flags,
19946 explain_p))
19947 return 1;
19950 /* Now substitute into the default template arguments. */
19951 for (i = 0; i < ntparms; i++)
19953 tree targ = TREE_VEC_ELT (targs, i);
19954 tree tparm = TREE_VEC_ELT (tparms, i);
19956 if (targ || tparm == error_mark_node)
19957 continue;
19958 tree parm = TREE_VALUE (tparm);
19960 tsubst_flags_t fcomplain = complain;
19961 if (saw_undeduced == 1)
19963 /* When saw_undeduced == 1, substitution into parm and arg might
19964 fail or not replace all template parameters, and that's
19965 fine. */
19966 fcomplain = tf_none;
19967 if (TREE_CODE (parm) == PARM_DECL
19968 && uses_template_parms (TREE_TYPE (parm)))
19969 continue;
19972 tree arg = TREE_PURPOSE (tparm);
19973 reopen_deferring_access_checks (*checks);
19974 location_t save_loc = input_location;
19975 if (DECL_P (parm))
19976 input_location = DECL_SOURCE_LOCATION (parm);
19977 arg = tsubst_template_arg (arg, full_targs, fcomplain, NULL_TREE);
19978 if (arg != error_mark_node && !uses_template_parms (arg))
19979 arg = convert_template_argument (parm, arg, full_targs, complain,
19980 i, NULL_TREE);
19981 else if (saw_undeduced == 1)
19982 arg = NULL_TREE;
19983 else
19984 arg = error_mark_node;
19985 input_location = save_loc;
19986 *checks = get_deferred_access_checks ();
19987 pop_deferring_access_checks ();
19988 if (arg == error_mark_node)
19989 return 1;
19990 else if (arg)
19992 TREE_VEC_ELT (targs, i) = arg;
19993 /* The position of the first default template argument,
19994 is also the number of non-defaulted arguments in TARGS.
19995 Record that. */
19996 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19997 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
20001 if (saw_undeduced++ == 1)
20002 goto again;
20005 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
20006 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
20008 return unify_success (explain_p);
20011 /* Subroutine of type_unification_real. Args are like the variables
20012 at the call site. ARG is an overloaded function (or template-id);
20013 we try deducing template args from each of the overloads, and if
20014 only one succeeds, we go with that. Modifies TARGS and returns
20015 true on success. */
20017 static bool
20018 resolve_overloaded_unification (tree tparms,
20019 tree targs,
20020 tree parm,
20021 tree arg,
20022 unification_kind_t strict,
20023 int sub_strict,
20024 bool explain_p)
20026 tree tempargs = copy_node (targs);
20027 int good = 0;
20028 tree goodfn = NULL_TREE;
20029 bool addr_p;
20031 if (TREE_CODE (arg) == ADDR_EXPR)
20033 arg = TREE_OPERAND (arg, 0);
20034 addr_p = true;
20036 else
20037 addr_p = false;
20039 if (TREE_CODE (arg) == COMPONENT_REF)
20040 /* Handle `&x' where `x' is some static or non-static member
20041 function name. */
20042 arg = TREE_OPERAND (arg, 1);
20044 if (TREE_CODE (arg) == OFFSET_REF)
20045 arg = TREE_OPERAND (arg, 1);
20047 /* Strip baselink information. */
20048 if (BASELINK_P (arg))
20049 arg = BASELINK_FUNCTIONS (arg);
20051 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
20053 /* If we got some explicit template args, we need to plug them into
20054 the affected templates before we try to unify, in case the
20055 explicit args will completely resolve the templates in question. */
20057 int ok = 0;
20058 tree expl_subargs = TREE_OPERAND (arg, 1);
20059 arg = TREE_OPERAND (arg, 0);
20061 for (lkp_iterator iter (arg); iter; ++iter)
20063 tree fn = *iter;
20064 tree subargs, elem;
20066 if (TREE_CODE (fn) != TEMPLATE_DECL)
20067 continue;
20069 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
20070 expl_subargs, NULL_TREE, tf_none,
20071 /*require_all_args=*/true,
20072 /*use_default_args=*/true);
20073 if (subargs != error_mark_node
20074 && !any_dependent_template_arguments_p (subargs))
20076 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
20077 if (try_one_overload (tparms, targs, tempargs, parm,
20078 elem, strict, sub_strict, addr_p, explain_p)
20079 && (!goodfn || !same_type_p (goodfn, elem)))
20081 goodfn = elem;
20082 ++good;
20085 else if (subargs)
20086 ++ok;
20088 /* If no templates (or more than one) are fully resolved by the
20089 explicit arguments, this template-id is a non-deduced context; it
20090 could still be OK if we deduce all template arguments for the
20091 enclosing call through other arguments. */
20092 if (good != 1)
20093 good = ok;
20095 else if (TREE_CODE (arg) != OVERLOAD
20096 && TREE_CODE (arg) != FUNCTION_DECL)
20097 /* If ARG is, for example, "(0, &f)" then its type will be unknown
20098 -- but the deduction does not succeed because the expression is
20099 not just the function on its own. */
20100 return false;
20101 else
20102 for (lkp_iterator iter (arg); iter; ++iter)
20104 tree fn = *iter;
20105 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
20106 strict, sub_strict, addr_p, explain_p)
20107 && (!goodfn || !decls_match (goodfn, fn)))
20109 goodfn = fn;
20110 ++good;
20114 /* [temp.deduct.type] A template-argument can be deduced from a pointer
20115 to function or pointer to member function argument if the set of
20116 overloaded functions does not contain function templates and at most
20117 one of a set of overloaded functions provides a unique match.
20119 So if we found multiple possibilities, we return success but don't
20120 deduce anything. */
20122 if (good == 1)
20124 int i = TREE_VEC_LENGTH (targs);
20125 for (; i--; )
20126 if (TREE_VEC_ELT (tempargs, i))
20128 tree old = TREE_VEC_ELT (targs, i);
20129 tree new_ = TREE_VEC_ELT (tempargs, i);
20130 if (new_ && old && ARGUMENT_PACK_P (old)
20131 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
20132 /* Don't forget explicit template arguments in a pack. */
20133 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
20134 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
20135 TREE_VEC_ELT (targs, i) = new_;
20138 if (good)
20139 return true;
20141 return false;
20144 /* Core DR 115: In contexts where deduction is done and fails, or in
20145 contexts where deduction is not done, if a template argument list is
20146 specified and it, along with any default template arguments, identifies
20147 a single function template specialization, then the template-id is an
20148 lvalue for the function template specialization. */
20150 tree
20151 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
20153 tree expr, offset, baselink;
20154 bool addr;
20156 if (!type_unknown_p (orig_expr))
20157 return orig_expr;
20159 expr = orig_expr;
20160 addr = false;
20161 offset = NULL_TREE;
20162 baselink = NULL_TREE;
20164 if (TREE_CODE (expr) == ADDR_EXPR)
20166 expr = TREE_OPERAND (expr, 0);
20167 addr = true;
20169 if (TREE_CODE (expr) == OFFSET_REF)
20171 offset = expr;
20172 expr = TREE_OPERAND (expr, 1);
20174 if (BASELINK_P (expr))
20176 baselink = expr;
20177 expr = BASELINK_FUNCTIONS (expr);
20180 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
20182 int good = 0;
20183 tree goodfn = NULL_TREE;
20185 /* If we got some explicit template args, we need to plug them into
20186 the affected templates before we try to unify, in case the
20187 explicit args will completely resolve the templates in question. */
20189 tree expl_subargs = TREE_OPERAND (expr, 1);
20190 tree arg = TREE_OPERAND (expr, 0);
20191 tree badfn = NULL_TREE;
20192 tree badargs = NULL_TREE;
20194 for (lkp_iterator iter (arg); iter; ++iter)
20196 tree fn = *iter;
20197 tree subargs, elem;
20199 if (TREE_CODE (fn) != TEMPLATE_DECL)
20200 continue;
20202 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
20203 expl_subargs, NULL_TREE, tf_none,
20204 /*require_all_args=*/true,
20205 /*use_default_args=*/true);
20206 if (subargs != error_mark_node
20207 && !any_dependent_template_arguments_p (subargs))
20209 elem = instantiate_template (fn, subargs, tf_none);
20210 if (elem == error_mark_node)
20212 badfn = fn;
20213 badargs = subargs;
20215 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
20217 goodfn = elem;
20218 ++good;
20222 if (good == 1)
20224 mark_used (goodfn);
20225 expr = goodfn;
20226 if (baselink)
20227 expr = build_baselink (BASELINK_BINFO (baselink),
20228 BASELINK_ACCESS_BINFO (baselink),
20229 expr, BASELINK_OPTYPE (baselink));
20230 if (offset)
20232 tree base
20233 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
20234 expr = build_offset_ref (base, expr, addr, complain);
20236 if (addr)
20237 expr = cp_build_addr_expr (expr, complain);
20238 return expr;
20240 else if (good == 0 && badargs && (complain & tf_error))
20241 /* There were no good options and at least one bad one, so let the
20242 user know what the problem is. */
20243 instantiate_template (badfn, badargs, complain);
20245 return orig_expr;
20248 /* Subroutine of resolve_overloaded_unification; does deduction for a single
20249 overload. Fills TARGS with any deduced arguments, or error_mark_node if
20250 different overloads deduce different arguments for a given parm.
20251 ADDR_P is true if the expression for which deduction is being
20252 performed was of the form "& fn" rather than simply "fn".
20254 Returns 1 on success. */
20256 static int
20257 try_one_overload (tree tparms,
20258 tree orig_targs,
20259 tree targs,
20260 tree parm,
20261 tree arg,
20262 unification_kind_t strict,
20263 int sub_strict,
20264 bool addr_p,
20265 bool explain_p)
20267 int nargs;
20268 tree tempargs;
20269 int i;
20271 if (arg == error_mark_node)
20272 return 0;
20274 /* [temp.deduct.type] A template-argument can be deduced from a pointer
20275 to function or pointer to member function argument if the set of
20276 overloaded functions does not contain function templates and at most
20277 one of a set of overloaded functions provides a unique match.
20279 So if this is a template, just return success. */
20281 if (uses_template_parms (arg))
20282 return 1;
20284 if (TREE_CODE (arg) == METHOD_TYPE)
20285 arg = build_ptrmemfunc_type (build_pointer_type (arg));
20286 else if (addr_p)
20287 arg = build_pointer_type (arg);
20289 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
20291 /* We don't copy orig_targs for this because if we have already deduced
20292 some template args from previous args, unify would complain when we
20293 try to deduce a template parameter for the same argument, even though
20294 there isn't really a conflict. */
20295 nargs = TREE_VEC_LENGTH (targs);
20296 tempargs = make_tree_vec (nargs);
20298 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
20299 return 0;
20301 /* First make sure we didn't deduce anything that conflicts with
20302 explicitly specified args. */
20303 for (i = nargs; i--; )
20305 tree elt = TREE_VEC_ELT (tempargs, i);
20306 tree oldelt = TREE_VEC_ELT (orig_targs, i);
20308 if (!elt)
20309 /*NOP*/;
20310 else if (uses_template_parms (elt))
20311 /* Since we're unifying against ourselves, we will fill in
20312 template args used in the function parm list with our own
20313 template parms. Discard them. */
20314 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
20315 else if (oldelt && ARGUMENT_PACK_P (oldelt))
20317 /* Check that the argument at each index of the deduced argument pack
20318 is equivalent to the corresponding explicitly specified argument.
20319 We may have deduced more arguments than were explicitly specified,
20320 and that's OK. */
20322 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
20323 that's wrong if we deduce the same argument pack from multiple
20324 function arguments: it's only incomplete the first time. */
20326 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
20327 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
20329 if (TREE_VEC_LENGTH (deduced_pack)
20330 < TREE_VEC_LENGTH (explicit_pack))
20331 return 0;
20333 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
20334 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
20335 TREE_VEC_ELT (deduced_pack, j)))
20336 return 0;
20338 else if (oldelt && !template_args_equal (oldelt, elt))
20339 return 0;
20342 for (i = nargs; i--; )
20344 tree elt = TREE_VEC_ELT (tempargs, i);
20346 if (elt)
20347 TREE_VEC_ELT (targs, i) = elt;
20350 return 1;
20353 /* PARM is a template class (perhaps with unbound template
20354 parameters). ARG is a fully instantiated type. If ARG can be
20355 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
20356 TARGS are as for unify. */
20358 static tree
20359 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
20360 bool explain_p)
20362 tree copy_of_targs;
20364 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20365 return NULL_TREE;
20366 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20367 /* Matches anything. */;
20368 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
20369 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
20370 return NULL_TREE;
20372 /* We need to make a new template argument vector for the call to
20373 unify. If we used TARGS, we'd clutter it up with the result of
20374 the attempted unification, even if this class didn't work out.
20375 We also don't want to commit ourselves to all the unifications
20376 we've already done, since unification is supposed to be done on
20377 an argument-by-argument basis. In other words, consider the
20378 following pathological case:
20380 template <int I, int J, int K>
20381 struct S {};
20383 template <int I, int J>
20384 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
20386 template <int I, int J, int K>
20387 void f(S<I, J, K>, S<I, I, I>);
20389 void g() {
20390 S<0, 0, 0> s0;
20391 S<0, 1, 2> s2;
20393 f(s0, s2);
20396 Now, by the time we consider the unification involving `s2', we
20397 already know that we must have `f<0, 0, 0>'. But, even though
20398 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
20399 because there are two ways to unify base classes of S<0, 1, 2>
20400 with S<I, I, I>. If we kept the already deduced knowledge, we
20401 would reject the possibility I=1. */
20402 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
20404 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20406 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
20407 return NULL_TREE;
20408 return arg;
20411 /* If unification failed, we're done. */
20412 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
20413 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
20414 return NULL_TREE;
20416 return arg;
20419 /* Given a template type PARM and a class type ARG, find the unique
20420 base type in ARG that is an instance of PARM. We do not examine
20421 ARG itself; only its base-classes. If there is not exactly one
20422 appropriate base class, return NULL_TREE. PARM may be the type of
20423 a partial specialization, as well as a plain template type. Used
20424 by unify. */
20426 static enum template_base_result
20427 get_template_base (tree tparms, tree targs, tree parm, tree arg,
20428 bool explain_p, tree *result)
20430 tree rval = NULL_TREE;
20431 tree binfo;
20433 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
20435 binfo = TYPE_BINFO (complete_type (arg));
20436 if (!binfo)
20438 /* The type could not be completed. */
20439 *result = NULL_TREE;
20440 return tbr_incomplete_type;
20443 /* Walk in inheritance graph order. The search order is not
20444 important, and this avoids multiple walks of virtual bases. */
20445 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
20447 tree r = try_class_unification (tparms, targs, parm,
20448 BINFO_TYPE (binfo), explain_p);
20450 if (r)
20452 /* If there is more than one satisfactory baseclass, then:
20454 [temp.deduct.call]
20456 If they yield more than one possible deduced A, the type
20457 deduction fails.
20459 applies. */
20460 if (rval && !same_type_p (r, rval))
20462 *result = NULL_TREE;
20463 return tbr_ambiguous_baseclass;
20466 rval = r;
20470 *result = rval;
20471 return tbr_success;
20474 /* Returns the level of DECL, which declares a template parameter. */
20476 static int
20477 template_decl_level (tree decl)
20479 switch (TREE_CODE (decl))
20481 case TYPE_DECL:
20482 case TEMPLATE_DECL:
20483 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
20485 case PARM_DECL:
20486 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
20488 default:
20489 gcc_unreachable ();
20491 return 0;
20494 /* Decide whether ARG can be unified with PARM, considering only the
20495 cv-qualifiers of each type, given STRICT as documented for unify.
20496 Returns nonzero iff the unification is OK on that basis. */
20498 static int
20499 check_cv_quals_for_unify (int strict, tree arg, tree parm)
20501 int arg_quals = cp_type_quals (arg);
20502 int parm_quals = cp_type_quals (parm);
20504 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20505 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20507 /* Although a CVR qualifier is ignored when being applied to a
20508 substituted template parameter ([8.3.2]/1 for example), that
20509 does not allow us to unify "const T" with "int&" because both
20510 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
20511 It is ok when we're allowing additional CV qualifiers
20512 at the outer level [14.8.2.1]/3,1st bullet. */
20513 if ((TREE_CODE (arg) == REFERENCE_TYPE
20514 || TREE_CODE (arg) == FUNCTION_TYPE
20515 || TREE_CODE (arg) == METHOD_TYPE)
20516 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
20517 return 0;
20519 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
20520 && (parm_quals & TYPE_QUAL_RESTRICT))
20521 return 0;
20524 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20525 && (arg_quals & parm_quals) != parm_quals)
20526 return 0;
20528 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
20529 && (parm_quals & arg_quals) != arg_quals)
20530 return 0;
20532 return 1;
20535 /* Determines the LEVEL and INDEX for the template parameter PARM. */
20536 void
20537 template_parm_level_and_index (tree parm, int* level, int* index)
20539 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20540 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20541 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20543 *index = TEMPLATE_TYPE_IDX (parm);
20544 *level = TEMPLATE_TYPE_LEVEL (parm);
20546 else
20548 *index = TEMPLATE_PARM_IDX (parm);
20549 *level = TEMPLATE_PARM_LEVEL (parm);
20553 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
20554 do { \
20555 if (unify (TP, TA, P, A, S, EP)) \
20556 return 1; \
20557 } while (0)
20559 /* Unifies the remaining arguments in PACKED_ARGS with the pack
20560 expansion at the end of PACKED_PARMS. Returns 0 if the type
20561 deduction succeeds, 1 otherwise. STRICT is the same as in
20562 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
20563 function call argument list. We'll need to adjust the arguments to make them
20564 types. SUBR tells us if this is from a recursive call to
20565 type_unification_real, or for comparing two template argument
20566 lists. */
20568 static int
20569 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
20570 tree packed_args, unification_kind_t strict,
20571 bool subr, bool explain_p)
20573 tree parm
20574 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
20575 tree pattern = PACK_EXPANSION_PATTERN (parm);
20576 tree pack, packs = NULL_TREE;
20577 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
20579 /* Add in any args remembered from an earlier partial instantiation. */
20580 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
20581 int levels = TMPL_ARGS_DEPTH (targs);
20583 packed_args = expand_template_argument_pack (packed_args);
20585 int len = TREE_VEC_LENGTH (packed_args);
20587 /* Determine the parameter packs we will be deducing from the
20588 pattern, and record their current deductions. */
20589 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
20590 pack; pack = TREE_CHAIN (pack))
20592 tree parm_pack = TREE_VALUE (pack);
20593 int idx, level;
20595 /* Determine the index and level of this parameter pack. */
20596 template_parm_level_and_index (parm_pack, &level, &idx);
20597 if (level < levels)
20598 continue;
20600 /* Keep track of the parameter packs and their corresponding
20601 argument packs. */
20602 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
20603 TREE_TYPE (packs) = make_tree_vec (len - start);
20606 /* Loop through all of the arguments that have not yet been
20607 unified and unify each with the pattern. */
20608 for (i = start; i < len; i++)
20610 tree parm;
20611 bool any_explicit = false;
20612 tree arg = TREE_VEC_ELT (packed_args, i);
20614 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
20615 or the element of its argument pack at the current index if
20616 this argument was explicitly specified. */
20617 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20619 int idx, level;
20620 tree arg, pargs;
20621 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20623 arg = NULL_TREE;
20624 if (TREE_VALUE (pack)
20625 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
20626 && (i - start < TREE_VEC_LENGTH (pargs)))
20628 any_explicit = true;
20629 arg = TREE_VEC_ELT (pargs, i - start);
20631 TMPL_ARG (targs, level, idx) = arg;
20634 /* If we had explicit template arguments, substitute them into the
20635 pattern before deduction. */
20636 if (any_explicit)
20638 /* Some arguments might still be unspecified or dependent. */
20639 bool dependent;
20640 ++processing_template_decl;
20641 dependent = any_dependent_template_arguments_p (targs);
20642 if (!dependent)
20643 --processing_template_decl;
20644 parm = tsubst (pattern, targs,
20645 explain_p ? tf_warning_or_error : tf_none,
20646 NULL_TREE);
20647 if (dependent)
20648 --processing_template_decl;
20649 if (parm == error_mark_node)
20650 return 1;
20652 else
20653 parm = pattern;
20655 /* Unify the pattern with the current argument. */
20656 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
20657 explain_p))
20658 return 1;
20660 /* For each parameter pack, collect the deduced value. */
20661 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20663 int idx, level;
20664 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20666 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
20667 TMPL_ARG (targs, level, idx);
20671 /* Verify that the results of unification with the parameter packs
20672 produce results consistent with what we've seen before, and make
20673 the deduced argument packs available. */
20674 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20676 tree old_pack = TREE_VALUE (pack);
20677 tree new_args = TREE_TYPE (pack);
20678 int i, len = TREE_VEC_LENGTH (new_args);
20679 int idx, level;
20680 bool nondeduced_p = false;
20682 /* By default keep the original deduced argument pack.
20683 If necessary, more specific code is going to update the
20684 resulting deduced argument later down in this function. */
20685 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20686 TMPL_ARG (targs, level, idx) = old_pack;
20688 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
20689 actually deduce anything. */
20690 for (i = 0; i < len && !nondeduced_p; ++i)
20691 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
20692 nondeduced_p = true;
20693 if (nondeduced_p)
20694 continue;
20696 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
20698 /* If we had fewer function args than explicit template args,
20699 just use the explicits. */
20700 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20701 int explicit_len = TREE_VEC_LENGTH (explicit_args);
20702 if (len < explicit_len)
20703 new_args = explicit_args;
20706 if (!old_pack)
20708 tree result;
20709 /* Build the deduced *_ARGUMENT_PACK. */
20710 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
20712 result = make_node (NONTYPE_ARGUMENT_PACK);
20713 TREE_CONSTANT (result) = 1;
20715 else
20716 result = cxx_make_type (TYPE_ARGUMENT_PACK);
20718 SET_ARGUMENT_PACK_ARGS (result, new_args);
20720 /* Note the deduced argument packs for this parameter
20721 pack. */
20722 TMPL_ARG (targs, level, idx) = result;
20724 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
20725 && (ARGUMENT_PACK_ARGS (old_pack)
20726 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
20728 /* We only had the explicitly-provided arguments before, but
20729 now we have a complete set of arguments. */
20730 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20732 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
20733 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
20734 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
20736 else
20738 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
20739 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
20741 if (!comp_template_args (old_args, new_args,
20742 &bad_old_arg, &bad_new_arg))
20743 /* Inconsistent unification of this parameter pack. */
20744 return unify_parameter_pack_inconsistent (explain_p,
20745 bad_old_arg,
20746 bad_new_arg);
20750 return unify_success (explain_p);
20753 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
20754 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
20755 parameters and return value are as for unify. */
20757 static int
20758 unify_array_domain (tree tparms, tree targs,
20759 tree parm_dom, tree arg_dom,
20760 bool explain_p)
20762 tree parm_max;
20763 tree arg_max;
20764 bool parm_cst;
20765 bool arg_cst;
20767 /* Our representation of array types uses "N - 1" as the
20768 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
20769 not an integer constant. We cannot unify arbitrarily
20770 complex expressions, so we eliminate the MINUS_EXPRs
20771 here. */
20772 parm_max = TYPE_MAX_VALUE (parm_dom);
20773 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
20774 if (!parm_cst)
20776 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
20777 parm_max = TREE_OPERAND (parm_max, 0);
20779 arg_max = TYPE_MAX_VALUE (arg_dom);
20780 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
20781 if (!arg_cst)
20783 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
20784 trying to unify the type of a variable with the type
20785 of a template parameter. For example:
20787 template <unsigned int N>
20788 void f (char (&) [N]);
20789 int g();
20790 void h(int i) {
20791 char a[g(i)];
20792 f(a);
20795 Here, the type of the ARG will be "int [g(i)]", and
20796 may be a SAVE_EXPR, etc. */
20797 if (TREE_CODE (arg_max) != MINUS_EXPR)
20798 return unify_vla_arg (explain_p, arg_dom);
20799 arg_max = TREE_OPERAND (arg_max, 0);
20802 /* If only one of the bounds used a MINUS_EXPR, compensate
20803 by adding one to the other bound. */
20804 if (parm_cst && !arg_cst)
20805 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
20806 integer_type_node,
20807 parm_max,
20808 integer_one_node);
20809 else if (arg_cst && !parm_cst)
20810 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
20811 integer_type_node,
20812 arg_max,
20813 integer_one_node);
20815 return unify (tparms, targs, parm_max, arg_max,
20816 UNIFY_ALLOW_INTEGER, explain_p);
20819 /* Returns whether T, a P or A in unify, is a type, template or expression. */
20821 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
20823 static pa_kind_t
20824 pa_kind (tree t)
20826 if (PACK_EXPANSION_P (t))
20827 t = PACK_EXPANSION_PATTERN (t);
20828 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
20829 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
20830 || DECL_TYPE_TEMPLATE_P (t))
20831 return pa_tmpl;
20832 else if (TYPE_P (t))
20833 return pa_type;
20834 else
20835 return pa_expr;
20838 /* Deduce the value of template parameters. TPARMS is the (innermost)
20839 set of template parameters to a template. TARGS is the bindings
20840 for those template parameters, as determined thus far; TARGS may
20841 include template arguments for outer levels of template parameters
20842 as well. PARM is a parameter to a template function, or a
20843 subcomponent of that parameter; ARG is the corresponding argument.
20844 This function attempts to match PARM with ARG in a manner
20845 consistent with the existing assignments in TARGS. If more values
20846 are deduced, then TARGS is updated.
20848 Returns 0 if the type deduction succeeds, 1 otherwise. The
20849 parameter STRICT is a bitwise or of the following flags:
20851 UNIFY_ALLOW_NONE:
20852 Require an exact match between PARM and ARG.
20853 UNIFY_ALLOW_MORE_CV_QUAL:
20854 Allow the deduced ARG to be more cv-qualified (by qualification
20855 conversion) than ARG.
20856 UNIFY_ALLOW_LESS_CV_QUAL:
20857 Allow the deduced ARG to be less cv-qualified than ARG.
20858 UNIFY_ALLOW_DERIVED:
20859 Allow the deduced ARG to be a template base class of ARG,
20860 or a pointer to a template base class of the type pointed to by
20861 ARG.
20862 UNIFY_ALLOW_INTEGER:
20863 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
20864 case for more information.
20865 UNIFY_ALLOW_OUTER_LEVEL:
20866 This is the outermost level of a deduction. Used to determine validity
20867 of qualification conversions. A valid qualification conversion must
20868 have const qualified pointers leading up to the inner type which
20869 requires additional CV quals, except at the outer level, where const
20870 is not required [conv.qual]. It would be normal to set this flag in
20871 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
20872 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
20873 This is the outermost level of a deduction, and PARM can be more CV
20874 qualified at this point.
20875 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
20876 This is the outermost level of a deduction, and PARM can be less CV
20877 qualified at this point. */
20879 static int
20880 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
20881 bool explain_p)
20883 int idx;
20884 tree targ;
20885 tree tparm;
20886 int strict_in = strict;
20887 tsubst_flags_t complain = (explain_p
20888 ? tf_warning_or_error
20889 : tf_none);
20891 /* I don't think this will do the right thing with respect to types.
20892 But the only case I've seen it in so far has been array bounds, where
20893 signedness is the only information lost, and I think that will be
20894 okay. */
20895 while (CONVERT_EXPR_P (parm))
20896 parm = TREE_OPERAND (parm, 0);
20898 if (arg == error_mark_node)
20899 return unify_invalid (explain_p);
20900 if (arg == unknown_type_node
20901 || arg == init_list_type_node)
20902 /* We can't deduce anything from this, but we might get all the
20903 template args from other function args. */
20904 return unify_success (explain_p);
20906 if (parm == any_targ_node || arg == any_targ_node)
20907 return unify_success (explain_p);
20909 /* If PARM uses template parameters, then we can't bail out here,
20910 even if ARG == PARM, since we won't record unifications for the
20911 template parameters. We might need them if we're trying to
20912 figure out which of two things is more specialized. */
20913 if (arg == parm && !uses_template_parms (parm))
20914 return unify_success (explain_p);
20916 /* Handle init lists early, so the rest of the function can assume
20917 we're dealing with a type. */
20918 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
20920 tree elt, elttype;
20921 unsigned i;
20922 tree orig_parm = parm;
20924 /* Replace T with std::initializer_list<T> for deduction. */
20925 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20926 && flag_deduce_init_list)
20927 parm = listify (parm);
20929 if (!is_std_init_list (parm)
20930 && TREE_CODE (parm) != ARRAY_TYPE)
20931 /* We can only deduce from an initializer list argument if the
20932 parameter is std::initializer_list or an array; otherwise this
20933 is a non-deduced context. */
20934 return unify_success (explain_p);
20936 if (TREE_CODE (parm) == ARRAY_TYPE)
20937 elttype = TREE_TYPE (parm);
20938 else
20940 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
20941 /* Deduction is defined in terms of a single type, so just punt
20942 on the (bizarre) std::initializer_list<T...>. */
20943 if (PACK_EXPANSION_P (elttype))
20944 return unify_success (explain_p);
20947 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
20949 int elt_strict = strict;
20951 if (elt == error_mark_node)
20952 return unify_invalid (explain_p);
20954 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
20956 tree type = TREE_TYPE (elt);
20957 if (type == error_mark_node)
20958 return unify_invalid (explain_p);
20959 /* It should only be possible to get here for a call. */
20960 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
20961 elt_strict |= maybe_adjust_types_for_deduction
20962 (DEDUCE_CALL, &elttype, &type, elt);
20963 elt = type;
20966 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
20967 explain_p);
20970 if (TREE_CODE (parm) == ARRAY_TYPE
20971 && deducible_array_bound (TYPE_DOMAIN (parm)))
20973 /* Also deduce from the length of the initializer list. */
20974 tree max = size_int (CONSTRUCTOR_NELTS (arg));
20975 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
20976 if (idx == error_mark_node)
20977 return unify_invalid (explain_p);
20978 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20979 idx, explain_p);
20982 /* If the std::initializer_list<T> deduction worked, replace the
20983 deduced A with std::initializer_list<A>. */
20984 if (orig_parm != parm)
20986 idx = TEMPLATE_TYPE_IDX (orig_parm);
20987 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20988 targ = listify (targ);
20989 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
20991 return unify_success (explain_p);
20994 /* If parm and arg aren't the same kind of thing (template, type, or
20995 expression), fail early. */
20996 if (pa_kind (parm) != pa_kind (arg))
20997 return unify_invalid (explain_p);
20999 /* Immediately reject some pairs that won't unify because of
21000 cv-qualification mismatches. */
21001 if (TREE_CODE (arg) == TREE_CODE (parm)
21002 && TYPE_P (arg)
21003 /* It is the elements of the array which hold the cv quals of an array
21004 type, and the elements might be template type parms. We'll check
21005 when we recurse. */
21006 && TREE_CODE (arg) != ARRAY_TYPE
21007 /* We check the cv-qualifiers when unifying with template type
21008 parameters below. We want to allow ARG `const T' to unify with
21009 PARM `T' for example, when computing which of two templates
21010 is more specialized, for example. */
21011 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
21012 && !check_cv_quals_for_unify (strict_in, arg, parm))
21013 return unify_cv_qual_mismatch (explain_p, parm, arg);
21015 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
21016 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
21017 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
21018 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
21019 strict &= ~UNIFY_ALLOW_DERIVED;
21020 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
21021 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
21023 switch (TREE_CODE (parm))
21025 case TYPENAME_TYPE:
21026 case SCOPE_REF:
21027 case UNBOUND_CLASS_TEMPLATE:
21028 /* In a type which contains a nested-name-specifier, template
21029 argument values cannot be deduced for template parameters used
21030 within the nested-name-specifier. */
21031 return unify_success (explain_p);
21033 case TEMPLATE_TYPE_PARM:
21034 case TEMPLATE_TEMPLATE_PARM:
21035 case BOUND_TEMPLATE_TEMPLATE_PARM:
21036 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
21037 if (error_operand_p (tparm))
21038 return unify_invalid (explain_p);
21040 if (TEMPLATE_TYPE_LEVEL (parm)
21041 != template_decl_level (tparm))
21042 /* The PARM is not one we're trying to unify. Just check
21043 to see if it matches ARG. */
21045 if (TREE_CODE (arg) == TREE_CODE (parm)
21046 && (is_auto (parm) ? is_auto (arg)
21047 : same_type_p (parm, arg)))
21048 return unify_success (explain_p);
21049 else
21050 return unify_type_mismatch (explain_p, parm, arg);
21052 idx = TEMPLATE_TYPE_IDX (parm);
21053 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
21054 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
21055 if (error_operand_p (tparm))
21056 return unify_invalid (explain_p);
21058 /* Check for mixed types and values. */
21059 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
21060 && TREE_CODE (tparm) != TYPE_DECL)
21061 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
21062 && TREE_CODE (tparm) != TEMPLATE_DECL))
21063 gcc_unreachable ();
21065 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21067 if ((strict_in & UNIFY_ALLOW_DERIVED)
21068 && CLASS_TYPE_P (arg))
21070 /* First try to match ARG directly. */
21071 tree t = try_class_unification (tparms, targs, parm, arg,
21072 explain_p);
21073 if (!t)
21075 /* Otherwise, look for a suitable base of ARG, as below. */
21076 enum template_base_result r;
21077 r = get_template_base (tparms, targs, parm, arg,
21078 explain_p, &t);
21079 if (!t)
21080 return unify_no_common_base (explain_p, r, parm, arg);
21081 arg = t;
21084 /* ARG must be constructed from a template class or a template
21085 template parameter. */
21086 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
21087 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
21088 return unify_template_deduction_failure (explain_p, parm, arg);
21090 /* Deduce arguments T, i from TT<T> or TT<i>. */
21091 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
21092 return 1;
21094 arg = TYPE_TI_TEMPLATE (arg);
21096 /* Fall through to deduce template name. */
21099 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
21100 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21102 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
21104 /* Simple cases: Value already set, does match or doesn't. */
21105 if (targ != NULL_TREE && template_args_equal (targ, arg))
21106 return unify_success (explain_p);
21107 else if (targ)
21108 return unify_inconsistency (explain_p, parm, targ, arg);
21110 else
21112 /* If PARM is `const T' and ARG is only `int', we don't have
21113 a match unless we are allowing additional qualification.
21114 If ARG is `const int' and PARM is just `T' that's OK;
21115 that binds `const int' to `T'. */
21116 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
21117 arg, parm))
21118 return unify_cv_qual_mismatch (explain_p, parm, arg);
21120 /* Consider the case where ARG is `const volatile int' and
21121 PARM is `const T'. Then, T should be `volatile int'. */
21122 arg = cp_build_qualified_type_real
21123 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
21124 if (arg == error_mark_node)
21125 return unify_invalid (explain_p);
21127 /* Simple cases: Value already set, does match or doesn't. */
21128 if (targ != NULL_TREE && same_type_p (targ, arg))
21129 return unify_success (explain_p);
21130 else if (targ)
21131 return unify_inconsistency (explain_p, parm, targ, arg);
21133 /* Make sure that ARG is not a variable-sized array. (Note
21134 that were talking about variable-sized arrays (like
21135 `int[n]'), rather than arrays of unknown size (like
21136 `int[]').) We'll get very confused by such a type since
21137 the bound of the array is not constant, and therefore
21138 not mangleable. Besides, such types are not allowed in
21139 ISO C++, so we can do as we please here. We do allow
21140 them for 'auto' deduction, since that isn't ABI-exposed. */
21141 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
21142 return unify_vla_arg (explain_p, arg);
21144 /* Strip typedefs as in convert_template_argument. */
21145 arg = canonicalize_type_argument (arg, tf_none);
21148 /* If ARG is a parameter pack or an expansion, we cannot unify
21149 against it unless PARM is also a parameter pack. */
21150 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
21151 && !template_parameter_pack_p (parm))
21152 return unify_parameter_pack_mismatch (explain_p, parm, arg);
21154 /* If the argument deduction results is a METHOD_TYPE,
21155 then there is a problem.
21156 METHOD_TYPE doesn't map to any real C++ type the result of
21157 the deduction can not be of that type. */
21158 if (TREE_CODE (arg) == METHOD_TYPE)
21159 return unify_method_type_error (explain_p, arg);
21161 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
21162 return unify_success (explain_p);
21164 case TEMPLATE_PARM_INDEX:
21165 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
21166 if (error_operand_p (tparm))
21167 return unify_invalid (explain_p);
21169 if (TEMPLATE_PARM_LEVEL (parm)
21170 != template_decl_level (tparm))
21172 /* The PARM is not one we're trying to unify. Just check
21173 to see if it matches ARG. */
21174 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
21175 && cp_tree_equal (parm, arg));
21176 if (result)
21177 unify_expression_unequal (explain_p, parm, arg);
21178 return result;
21181 idx = TEMPLATE_PARM_IDX (parm);
21182 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
21184 if (targ)
21186 if ((strict & UNIFY_ALLOW_INTEGER)
21187 && TREE_TYPE (targ) && TREE_TYPE (arg)
21188 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
21189 /* We're deducing from an array bound, the type doesn't matter. */
21190 arg = fold_convert (TREE_TYPE (targ), arg);
21191 int x = !cp_tree_equal (targ, arg);
21192 if (x)
21193 unify_inconsistency (explain_p, parm, targ, arg);
21194 return x;
21197 /* [temp.deduct.type] If, in the declaration of a function template
21198 with a non-type template-parameter, the non-type
21199 template-parameter is used in an expression in the function
21200 parameter-list and, if the corresponding template-argument is
21201 deduced, the template-argument type shall match the type of the
21202 template-parameter exactly, except that a template-argument
21203 deduced from an array bound may be of any integral type.
21204 The non-type parameter might use already deduced type parameters. */
21205 ++processing_template_decl;
21206 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
21207 --processing_template_decl;
21208 if (tree a = type_uses_auto (tparm))
21210 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
21211 if (tparm == error_mark_node)
21212 return 1;
21215 if (!TREE_TYPE (arg))
21216 /* Template-parameter dependent expression. Just accept it for now.
21217 It will later be processed in convert_template_argument. */
21219 else if (same_type_p (non_reference (TREE_TYPE (arg)),
21220 non_reference (tparm)))
21221 /* OK */;
21222 else if ((strict & UNIFY_ALLOW_INTEGER)
21223 && CP_INTEGRAL_TYPE_P (tparm))
21224 /* Convert the ARG to the type of PARM; the deduced non-type
21225 template argument must exactly match the types of the
21226 corresponding parameter. */
21227 arg = fold (build_nop (tparm, arg));
21228 else if (uses_template_parms (tparm))
21230 /* We haven't deduced the type of this parameter yet. */
21231 if (cxx_dialect >= cxx17
21232 /* We deduce from array bounds in try_array_deduction. */
21233 && !(strict & UNIFY_ALLOW_INTEGER))
21235 /* Deduce it from the non-type argument. */
21236 tree atype = TREE_TYPE (arg);
21237 RECUR_AND_CHECK_FAILURE (tparms, targs,
21238 tparm, atype,
21239 UNIFY_ALLOW_NONE, explain_p);
21241 else
21242 /* Try again later. */
21243 return unify_success (explain_p);
21245 else
21246 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
21248 /* If ARG is a parameter pack or an expansion, we cannot unify
21249 against it unless PARM is also a parameter pack. */
21250 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
21251 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
21252 return unify_parameter_pack_mismatch (explain_p, parm, arg);
21255 bool removed_attr = false;
21256 arg = strip_typedefs_expr (arg, &removed_attr);
21258 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
21259 return unify_success (explain_p);
21261 case PTRMEM_CST:
21263 /* A pointer-to-member constant can be unified only with
21264 another constant. */
21265 if (TREE_CODE (arg) != PTRMEM_CST)
21266 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
21268 /* Just unify the class member. It would be useless (and possibly
21269 wrong, depending on the strict flags) to unify also
21270 PTRMEM_CST_CLASS, because we want to be sure that both parm and
21271 arg refer to the same variable, even if through different
21272 classes. For instance:
21274 struct A { int x; };
21275 struct B : A { };
21277 Unification of &A::x and &B::x must succeed. */
21278 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
21279 PTRMEM_CST_MEMBER (arg), strict, explain_p);
21282 case POINTER_TYPE:
21284 if (!TYPE_PTR_P (arg))
21285 return unify_type_mismatch (explain_p, parm, arg);
21287 /* [temp.deduct.call]
21289 A can be another pointer or pointer to member type that can
21290 be converted to the deduced A via a qualification
21291 conversion (_conv.qual_).
21293 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
21294 This will allow for additional cv-qualification of the
21295 pointed-to types if appropriate. */
21297 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
21298 /* The derived-to-base conversion only persists through one
21299 level of pointers. */
21300 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
21302 return unify (tparms, targs, TREE_TYPE (parm),
21303 TREE_TYPE (arg), strict, explain_p);
21306 case REFERENCE_TYPE:
21307 if (TREE_CODE (arg) != REFERENCE_TYPE)
21308 return unify_type_mismatch (explain_p, parm, arg);
21309 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21310 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21312 case ARRAY_TYPE:
21313 if (TREE_CODE (arg) != ARRAY_TYPE)
21314 return unify_type_mismatch (explain_p, parm, arg);
21315 if ((TYPE_DOMAIN (parm) == NULL_TREE)
21316 != (TYPE_DOMAIN (arg) == NULL_TREE))
21317 return unify_type_mismatch (explain_p, parm, arg);
21318 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21319 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21320 if (TYPE_DOMAIN (parm) != NULL_TREE)
21321 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
21322 TYPE_DOMAIN (arg), explain_p);
21323 return unify_success (explain_p);
21325 case REAL_TYPE:
21326 case COMPLEX_TYPE:
21327 case VECTOR_TYPE:
21328 case INTEGER_TYPE:
21329 case BOOLEAN_TYPE:
21330 case ENUMERAL_TYPE:
21331 case VOID_TYPE:
21332 case NULLPTR_TYPE:
21333 if (TREE_CODE (arg) != TREE_CODE (parm))
21334 return unify_type_mismatch (explain_p, parm, arg);
21336 /* We have already checked cv-qualification at the top of the
21337 function. */
21338 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
21339 return unify_type_mismatch (explain_p, parm, arg);
21341 /* As far as unification is concerned, this wins. Later checks
21342 will invalidate it if necessary. */
21343 return unify_success (explain_p);
21345 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
21346 /* Type INTEGER_CST can come from ordinary constant template args. */
21347 case INTEGER_CST:
21348 while (CONVERT_EXPR_P (arg))
21349 arg = TREE_OPERAND (arg, 0);
21351 if (TREE_CODE (arg) != INTEGER_CST)
21352 return unify_template_argument_mismatch (explain_p, parm, arg);
21353 return (tree_int_cst_equal (parm, arg)
21354 ? unify_success (explain_p)
21355 : unify_template_argument_mismatch (explain_p, parm, arg));
21357 case TREE_VEC:
21359 int i, len, argslen;
21360 int parm_variadic_p = 0;
21362 if (TREE_CODE (arg) != TREE_VEC)
21363 return unify_template_argument_mismatch (explain_p, parm, arg);
21365 len = TREE_VEC_LENGTH (parm);
21366 argslen = TREE_VEC_LENGTH (arg);
21368 /* Check for pack expansions in the parameters. */
21369 for (i = 0; i < len; ++i)
21371 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
21373 if (i == len - 1)
21374 /* We can unify against something with a trailing
21375 parameter pack. */
21376 parm_variadic_p = 1;
21377 else
21378 /* [temp.deduct.type]/9: If the template argument list of
21379 P contains a pack expansion that is not the last
21380 template argument, the entire template argument list
21381 is a non-deduced context. */
21382 return unify_success (explain_p);
21386 /* If we don't have enough arguments to satisfy the parameters
21387 (not counting the pack expression at the end), or we have
21388 too many arguments for a parameter list that doesn't end in
21389 a pack expression, we can't unify. */
21390 if (parm_variadic_p
21391 ? argslen < len - parm_variadic_p
21392 : argslen != len)
21393 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
21395 /* Unify all of the parameters that precede the (optional)
21396 pack expression. */
21397 for (i = 0; i < len - parm_variadic_p; ++i)
21399 RECUR_AND_CHECK_FAILURE (tparms, targs,
21400 TREE_VEC_ELT (parm, i),
21401 TREE_VEC_ELT (arg, i),
21402 UNIFY_ALLOW_NONE, explain_p);
21404 if (parm_variadic_p)
21405 return unify_pack_expansion (tparms, targs, parm, arg,
21406 DEDUCE_EXACT,
21407 /*subr=*/true, explain_p);
21408 return unify_success (explain_p);
21411 case RECORD_TYPE:
21412 case UNION_TYPE:
21413 if (TREE_CODE (arg) != TREE_CODE (parm))
21414 return unify_type_mismatch (explain_p, parm, arg);
21416 if (TYPE_PTRMEMFUNC_P (parm))
21418 if (!TYPE_PTRMEMFUNC_P (arg))
21419 return unify_type_mismatch (explain_p, parm, arg);
21421 return unify (tparms, targs,
21422 TYPE_PTRMEMFUNC_FN_TYPE (parm),
21423 TYPE_PTRMEMFUNC_FN_TYPE (arg),
21424 strict, explain_p);
21426 else if (TYPE_PTRMEMFUNC_P (arg))
21427 return unify_type_mismatch (explain_p, parm, arg);
21429 if (CLASSTYPE_TEMPLATE_INFO (parm))
21431 tree t = NULL_TREE;
21433 if (strict_in & UNIFY_ALLOW_DERIVED)
21435 /* First, we try to unify the PARM and ARG directly. */
21436 t = try_class_unification (tparms, targs,
21437 parm, arg, explain_p);
21439 if (!t)
21441 /* Fallback to the special case allowed in
21442 [temp.deduct.call]:
21444 If P is a class, and P has the form
21445 template-id, then A can be a derived class of
21446 the deduced A. Likewise, if P is a pointer to
21447 a class of the form template-id, A can be a
21448 pointer to a derived class pointed to by the
21449 deduced A. */
21450 enum template_base_result r;
21451 r = get_template_base (tparms, targs, parm, arg,
21452 explain_p, &t);
21454 if (!t)
21456 /* Don't give the derived diagnostic if we're
21457 already dealing with the same template. */
21458 bool same_template
21459 = (CLASSTYPE_TEMPLATE_INFO (arg)
21460 && (CLASSTYPE_TI_TEMPLATE (parm)
21461 == CLASSTYPE_TI_TEMPLATE (arg)));
21462 return unify_no_common_base (explain_p && !same_template,
21463 r, parm, arg);
21467 else if (CLASSTYPE_TEMPLATE_INFO (arg)
21468 && (CLASSTYPE_TI_TEMPLATE (parm)
21469 == CLASSTYPE_TI_TEMPLATE (arg)))
21470 /* Perhaps PARM is something like S<U> and ARG is S<int>.
21471 Then, we should unify `int' and `U'. */
21472 t = arg;
21473 else
21474 /* There's no chance of unification succeeding. */
21475 return unify_type_mismatch (explain_p, parm, arg);
21477 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
21478 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
21480 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
21481 return unify_type_mismatch (explain_p, parm, arg);
21482 return unify_success (explain_p);
21484 case METHOD_TYPE:
21485 case FUNCTION_TYPE:
21487 unsigned int nargs;
21488 tree *args;
21489 tree a;
21490 unsigned int i;
21492 if (TREE_CODE (arg) != TREE_CODE (parm))
21493 return unify_type_mismatch (explain_p, parm, arg);
21495 /* CV qualifications for methods can never be deduced, they must
21496 match exactly. We need to check them explicitly here,
21497 because type_unification_real treats them as any other
21498 cv-qualified parameter. */
21499 if (TREE_CODE (parm) == METHOD_TYPE
21500 && (!check_cv_quals_for_unify
21501 (UNIFY_ALLOW_NONE,
21502 class_of_this_parm (arg),
21503 class_of_this_parm (parm))))
21504 return unify_cv_qual_mismatch (explain_p, parm, arg);
21505 if (TREE_CODE (arg) == FUNCTION_TYPE
21506 && type_memfn_quals (parm) != type_memfn_quals (arg))
21507 return unify_cv_qual_mismatch (explain_p, parm, arg);
21508 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
21509 return unify_type_mismatch (explain_p, parm, arg);
21511 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
21512 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
21514 nargs = list_length (TYPE_ARG_TYPES (arg));
21515 args = XALLOCAVEC (tree, nargs);
21516 for (a = TYPE_ARG_TYPES (arg), i = 0;
21517 a != NULL_TREE && a != void_list_node;
21518 a = TREE_CHAIN (a), ++i)
21519 args[i] = TREE_VALUE (a);
21520 nargs = i;
21522 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
21523 args, nargs, 1, DEDUCE_EXACT,
21524 LOOKUP_NORMAL, NULL, explain_p))
21525 return 1;
21527 if (flag_noexcept_type)
21529 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
21530 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
21531 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
21532 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
21533 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
21534 && uses_template_parms (TREE_PURPOSE (pspec)))
21535 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
21536 TREE_PURPOSE (aspec),
21537 UNIFY_ALLOW_NONE, explain_p);
21538 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
21539 return unify_type_mismatch (explain_p, parm, arg);
21542 return 0;
21545 case OFFSET_TYPE:
21546 /* Unify a pointer to member with a pointer to member function, which
21547 deduces the type of the member as a function type. */
21548 if (TYPE_PTRMEMFUNC_P (arg))
21550 /* Check top-level cv qualifiers */
21551 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
21552 return unify_cv_qual_mismatch (explain_p, parm, arg);
21554 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21555 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
21556 UNIFY_ALLOW_NONE, explain_p);
21558 /* Determine the type of the function we are unifying against. */
21559 tree fntype = static_fn_type (arg);
21561 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
21564 if (TREE_CODE (arg) != OFFSET_TYPE)
21565 return unify_type_mismatch (explain_p, parm, arg);
21566 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21567 TYPE_OFFSET_BASETYPE (arg),
21568 UNIFY_ALLOW_NONE, explain_p);
21569 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21570 strict, explain_p);
21572 case CONST_DECL:
21573 if (DECL_TEMPLATE_PARM_P (parm))
21574 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
21575 if (arg != scalar_constant_value (parm))
21576 return unify_template_argument_mismatch (explain_p, parm, arg);
21577 return unify_success (explain_p);
21579 case FIELD_DECL:
21580 case TEMPLATE_DECL:
21581 /* Matched cases are handled by the ARG == PARM test above. */
21582 return unify_template_argument_mismatch (explain_p, parm, arg);
21584 case VAR_DECL:
21585 /* We might get a variable as a non-type template argument in parm if the
21586 corresponding parameter is type-dependent. Make any necessary
21587 adjustments based on whether arg is a reference. */
21588 if (CONSTANT_CLASS_P (arg))
21589 parm = fold_non_dependent_expr (parm);
21590 else if (REFERENCE_REF_P (arg))
21592 tree sub = TREE_OPERAND (arg, 0);
21593 STRIP_NOPS (sub);
21594 if (TREE_CODE (sub) == ADDR_EXPR)
21595 arg = TREE_OPERAND (sub, 0);
21597 /* Now use the normal expression code to check whether they match. */
21598 goto expr;
21600 case TYPE_ARGUMENT_PACK:
21601 case NONTYPE_ARGUMENT_PACK:
21602 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
21603 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
21605 case TYPEOF_TYPE:
21606 case DECLTYPE_TYPE:
21607 case UNDERLYING_TYPE:
21608 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
21609 or UNDERLYING_TYPE nodes. */
21610 return unify_success (explain_p);
21612 case ERROR_MARK:
21613 /* Unification fails if we hit an error node. */
21614 return unify_invalid (explain_p);
21616 case INDIRECT_REF:
21617 if (REFERENCE_REF_P (parm))
21619 bool pexp = PACK_EXPANSION_P (arg);
21620 if (pexp)
21621 arg = PACK_EXPANSION_PATTERN (arg);
21622 if (REFERENCE_REF_P (arg))
21623 arg = TREE_OPERAND (arg, 0);
21624 if (pexp)
21625 arg = make_pack_expansion (arg, complain);
21626 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
21627 strict, explain_p);
21629 /* FALLTHRU */
21631 default:
21632 /* An unresolved overload is a nondeduced context. */
21633 if (is_overloaded_fn (parm) || type_unknown_p (parm))
21634 return unify_success (explain_p);
21635 gcc_assert (EXPR_P (parm) || TREE_CODE (parm) == TRAIT_EXPR);
21636 expr:
21637 /* We must be looking at an expression. This can happen with
21638 something like:
21640 template <int I>
21641 void foo(S<I>, S<I + 2>);
21643 This is a "nondeduced context":
21645 [deduct.type]
21647 The nondeduced contexts are:
21649 --A type that is a template-id in which one or more of
21650 the template-arguments is an expression that references
21651 a template-parameter.
21653 In these cases, we assume deduction succeeded, but don't
21654 actually infer any unifications. */
21656 if (!uses_template_parms (parm)
21657 && !template_args_equal (parm, arg))
21658 return unify_expression_unequal (explain_p, parm, arg);
21659 else
21660 return unify_success (explain_p);
21663 #undef RECUR_AND_CHECK_FAILURE
21665 /* Note that DECL can be defined in this translation unit, if
21666 required. */
21668 static void
21669 mark_definable (tree decl)
21671 tree clone;
21672 DECL_NOT_REALLY_EXTERN (decl) = 1;
21673 FOR_EACH_CLONE (clone, decl)
21674 DECL_NOT_REALLY_EXTERN (clone) = 1;
21677 /* Called if RESULT is explicitly instantiated, or is a member of an
21678 explicitly instantiated class. */
21680 void
21681 mark_decl_instantiated (tree result, int extern_p)
21683 SET_DECL_EXPLICIT_INSTANTIATION (result);
21685 /* If this entity has already been written out, it's too late to
21686 make any modifications. */
21687 if (TREE_ASM_WRITTEN (result))
21688 return;
21690 /* For anonymous namespace we don't need to do anything. */
21691 if (decl_anon_ns_mem_p (result))
21693 gcc_assert (!TREE_PUBLIC (result));
21694 return;
21697 if (TREE_CODE (result) != FUNCTION_DECL)
21698 /* The TREE_PUBLIC flag for function declarations will have been
21699 set correctly by tsubst. */
21700 TREE_PUBLIC (result) = 1;
21702 /* This might have been set by an earlier implicit instantiation. */
21703 DECL_COMDAT (result) = 0;
21705 if (extern_p)
21706 DECL_NOT_REALLY_EXTERN (result) = 0;
21707 else
21709 mark_definable (result);
21710 mark_needed (result);
21711 /* Always make artificials weak. */
21712 if (DECL_ARTIFICIAL (result) && flag_weak)
21713 comdat_linkage (result);
21714 /* For WIN32 we also want to put explicit instantiations in
21715 linkonce sections. */
21716 else if (TREE_PUBLIC (result))
21717 maybe_make_one_only (result);
21720 /* If EXTERN_P, then this function will not be emitted -- unless
21721 followed by an explicit instantiation, at which point its linkage
21722 will be adjusted. If !EXTERN_P, then this function will be
21723 emitted here. In neither circumstance do we want
21724 import_export_decl to adjust the linkage. */
21725 DECL_INTERFACE_KNOWN (result) = 1;
21728 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
21729 important template arguments. If any are missing, we check whether
21730 they're important by using error_mark_node for substituting into any
21731 args that were used for partial ordering (the ones between ARGS and END)
21732 and seeing if it bubbles up. */
21734 static bool
21735 check_undeduced_parms (tree targs, tree args, tree end)
21737 bool found = false;
21738 int i;
21739 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
21740 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
21742 found = true;
21743 TREE_VEC_ELT (targs, i) = error_mark_node;
21745 if (found)
21747 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
21748 if (substed == error_mark_node)
21749 return true;
21751 return false;
21754 /* Given two function templates PAT1 and PAT2, return:
21756 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
21757 -1 if PAT2 is more specialized than PAT1.
21758 0 if neither is more specialized.
21760 LEN indicates the number of parameters we should consider
21761 (defaulted parameters should not be considered).
21763 The 1998 std underspecified function template partial ordering, and
21764 DR214 addresses the issue. We take pairs of arguments, one from
21765 each of the templates, and deduce them against each other. One of
21766 the templates will be more specialized if all the *other*
21767 template's arguments deduce against its arguments and at least one
21768 of its arguments *does* *not* deduce against the other template's
21769 corresponding argument. Deduction is done as for class templates.
21770 The arguments used in deduction have reference and top level cv
21771 qualifiers removed. Iff both arguments were originally reference
21772 types *and* deduction succeeds in both directions, an lvalue reference
21773 wins against an rvalue reference and otherwise the template
21774 with the more cv-qualified argument wins for that pairing (if
21775 neither is more cv-qualified, they both are equal). Unlike regular
21776 deduction, after all the arguments have been deduced in this way,
21777 we do *not* verify the deduced template argument values can be
21778 substituted into non-deduced contexts.
21780 The logic can be a bit confusing here, because we look at deduce1 and
21781 targs1 to see if pat2 is at least as specialized, and vice versa; if we
21782 can find template arguments for pat1 to make arg1 look like arg2, that
21783 means that arg2 is at least as specialized as arg1. */
21786 more_specialized_fn (tree pat1, tree pat2, int len)
21788 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
21789 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
21790 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
21791 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
21792 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
21793 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
21794 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
21795 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
21796 tree origs1, origs2;
21797 bool lose1 = false;
21798 bool lose2 = false;
21800 /* Remove the this parameter from non-static member functions. If
21801 one is a non-static member function and the other is not a static
21802 member function, remove the first parameter from that function
21803 also. This situation occurs for operator functions where we
21804 locate both a member function (with this pointer) and non-member
21805 operator (with explicit first operand). */
21806 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
21808 len--; /* LEN is the number of significant arguments for DECL1 */
21809 args1 = TREE_CHAIN (args1);
21810 if (!DECL_STATIC_FUNCTION_P (decl2))
21811 args2 = TREE_CHAIN (args2);
21813 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
21815 args2 = TREE_CHAIN (args2);
21816 if (!DECL_STATIC_FUNCTION_P (decl1))
21818 len--;
21819 args1 = TREE_CHAIN (args1);
21823 /* If only one is a conversion operator, they are unordered. */
21824 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
21825 return 0;
21827 /* Consider the return type for a conversion function */
21828 if (DECL_CONV_FN_P (decl1))
21830 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
21831 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
21832 len++;
21835 processing_template_decl++;
21837 origs1 = args1;
21838 origs2 = args2;
21840 while (len--
21841 /* Stop when an ellipsis is seen. */
21842 && args1 != NULL_TREE && args2 != NULL_TREE)
21844 tree arg1 = TREE_VALUE (args1);
21845 tree arg2 = TREE_VALUE (args2);
21846 int deduce1, deduce2;
21847 int quals1 = -1;
21848 int quals2 = -1;
21849 int ref1 = 0;
21850 int ref2 = 0;
21852 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21853 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21855 /* When both arguments are pack expansions, we need only
21856 unify the patterns themselves. */
21857 arg1 = PACK_EXPANSION_PATTERN (arg1);
21858 arg2 = PACK_EXPANSION_PATTERN (arg2);
21860 /* This is the last comparison we need to do. */
21861 len = 0;
21864 /* DR 1847: If a particular P contains no template-parameters that
21865 participate in template argument deduction, that P is not used to
21866 determine the ordering. */
21867 if (!uses_deducible_template_parms (arg1)
21868 && !uses_deducible_template_parms (arg2))
21869 goto next;
21871 if (TREE_CODE (arg1) == REFERENCE_TYPE)
21873 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
21874 arg1 = TREE_TYPE (arg1);
21875 quals1 = cp_type_quals (arg1);
21878 if (TREE_CODE (arg2) == REFERENCE_TYPE)
21880 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
21881 arg2 = TREE_TYPE (arg2);
21882 quals2 = cp_type_quals (arg2);
21885 arg1 = TYPE_MAIN_VARIANT (arg1);
21886 arg2 = TYPE_MAIN_VARIANT (arg2);
21888 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
21890 int i, len2 = remaining_arguments (args2);
21891 tree parmvec = make_tree_vec (1);
21892 tree argvec = make_tree_vec (len2);
21893 tree ta = args2;
21895 /* Setup the parameter vector, which contains only ARG1. */
21896 TREE_VEC_ELT (parmvec, 0) = arg1;
21898 /* Setup the argument vector, which contains the remaining
21899 arguments. */
21900 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
21901 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21903 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
21904 argvec, DEDUCE_EXACT,
21905 /*subr=*/true, /*explain_p=*/false)
21906 == 0);
21908 /* We cannot deduce in the other direction, because ARG1 is
21909 a pack expansion but ARG2 is not. */
21910 deduce2 = 0;
21912 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21914 int i, len1 = remaining_arguments (args1);
21915 tree parmvec = make_tree_vec (1);
21916 tree argvec = make_tree_vec (len1);
21917 tree ta = args1;
21919 /* Setup the parameter vector, which contains only ARG1. */
21920 TREE_VEC_ELT (parmvec, 0) = arg2;
21922 /* Setup the argument vector, which contains the remaining
21923 arguments. */
21924 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
21925 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21927 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
21928 argvec, DEDUCE_EXACT,
21929 /*subr=*/true, /*explain_p=*/false)
21930 == 0);
21932 /* We cannot deduce in the other direction, because ARG2 is
21933 a pack expansion but ARG1 is not.*/
21934 deduce1 = 0;
21937 else
21939 /* The normal case, where neither argument is a pack
21940 expansion. */
21941 deduce1 = (unify (tparms1, targs1, arg1, arg2,
21942 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21943 == 0);
21944 deduce2 = (unify (tparms2, targs2, arg2, arg1,
21945 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21946 == 0);
21949 /* If we couldn't deduce arguments for tparms1 to make arg1 match
21950 arg2, then arg2 is not as specialized as arg1. */
21951 if (!deduce1)
21952 lose2 = true;
21953 if (!deduce2)
21954 lose1 = true;
21956 /* "If, for a given type, deduction succeeds in both directions
21957 (i.e., the types are identical after the transformations above)
21958 and both P and A were reference types (before being replaced with
21959 the type referred to above):
21960 - if the type from the argument template was an lvalue reference and
21961 the type from the parameter template was not, the argument type is
21962 considered to be more specialized than the other; otherwise,
21963 - if the type from the argument template is more cv-qualified
21964 than the type from the parameter template (as described above),
21965 the argument type is considered to be more specialized than the other;
21966 otherwise,
21967 - neither type is more specialized than the other." */
21969 if (deduce1 && deduce2)
21971 if (ref1 && ref2 && ref1 != ref2)
21973 if (ref1 > ref2)
21974 lose1 = true;
21975 else
21976 lose2 = true;
21978 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
21980 if ((quals1 & quals2) == quals2)
21981 lose2 = true;
21982 if ((quals1 & quals2) == quals1)
21983 lose1 = true;
21987 if (lose1 && lose2)
21988 /* We've failed to deduce something in either direction.
21989 These must be unordered. */
21990 break;
21992 next:
21994 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21995 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21996 /* We have already processed all of the arguments in our
21997 handing of the pack expansion type. */
21998 len = 0;
22000 args1 = TREE_CHAIN (args1);
22001 args2 = TREE_CHAIN (args2);
22004 /* "In most cases, all template parameters must have values in order for
22005 deduction to succeed, but for partial ordering purposes a template
22006 parameter may remain without a value provided it is not used in the
22007 types being used for partial ordering."
22009 Thus, if we are missing any of the targs1 we need to substitute into
22010 origs1, then pat2 is not as specialized as pat1. This can happen when
22011 there is a nondeduced context. */
22012 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
22013 lose2 = true;
22014 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
22015 lose1 = true;
22017 processing_template_decl--;
22019 /* If both deductions succeed, the partial ordering selects the more
22020 constrained template. */
22021 if (!lose1 && !lose2)
22023 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
22024 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
22025 lose1 = !subsumes_constraints (c1, c2);
22026 lose2 = !subsumes_constraints (c2, c1);
22029 /* All things being equal, if the next argument is a pack expansion
22030 for one function but not for the other, prefer the
22031 non-variadic function. FIXME this is bogus; see c++/41958. */
22032 if (lose1 == lose2
22033 && args1 && TREE_VALUE (args1)
22034 && args2 && TREE_VALUE (args2))
22036 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
22037 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
22040 if (lose1 == lose2)
22041 return 0;
22042 else if (!lose1)
22043 return 1;
22044 else
22045 return -1;
22048 /* Determine which of two partial specializations of TMPL is more
22049 specialized.
22051 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
22052 to the first partial specialization. The TREE_PURPOSE is the
22053 innermost set of template parameters for the partial
22054 specialization. PAT2 is similar, but for the second template.
22056 Return 1 if the first partial specialization is more specialized;
22057 -1 if the second is more specialized; 0 if neither is more
22058 specialized.
22060 See [temp.class.order] for information about determining which of
22061 two templates is more specialized. */
22063 static int
22064 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
22066 tree targs;
22067 int winner = 0;
22068 bool any_deductions = false;
22070 tree tmpl1 = TREE_VALUE (pat1);
22071 tree tmpl2 = TREE_VALUE (pat2);
22072 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
22073 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
22075 /* Just like what happens for functions, if we are ordering between
22076 different template specializations, we may encounter dependent
22077 types in the arguments, and we need our dependency check functions
22078 to behave correctly. */
22079 ++processing_template_decl;
22080 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
22081 if (targs)
22083 --winner;
22084 any_deductions = true;
22087 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
22088 if (targs)
22090 ++winner;
22091 any_deductions = true;
22093 --processing_template_decl;
22095 /* If both deductions succeed, the partial ordering selects the more
22096 constrained template. */
22097 if (!winner && any_deductions)
22098 return more_constrained (tmpl1, tmpl2);
22100 /* In the case of a tie where at least one of the templates
22101 has a parameter pack at the end, the template with the most
22102 non-packed parameters wins. */
22103 if (winner == 0
22104 && any_deductions
22105 && (template_args_variadic_p (TREE_PURPOSE (pat1))
22106 || template_args_variadic_p (TREE_PURPOSE (pat2))))
22108 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
22109 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
22110 int len1 = TREE_VEC_LENGTH (args1);
22111 int len2 = TREE_VEC_LENGTH (args2);
22113 /* We don't count the pack expansion at the end. */
22114 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
22115 --len1;
22116 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
22117 --len2;
22119 if (len1 > len2)
22120 return 1;
22121 else if (len1 < len2)
22122 return -1;
22125 return winner;
22128 /* Return the template arguments that will produce the function signature
22129 DECL from the function template FN, with the explicit template
22130 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
22131 also match. Return NULL_TREE if no satisfactory arguments could be
22132 found. */
22134 static tree
22135 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
22137 int ntparms = DECL_NTPARMS (fn);
22138 tree targs = make_tree_vec (ntparms);
22139 tree decl_type = TREE_TYPE (decl);
22140 tree decl_arg_types;
22141 tree *args;
22142 unsigned int nargs, ix;
22143 tree arg;
22145 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
22147 /* Never do unification on the 'this' parameter. */
22148 decl_arg_types = skip_artificial_parms_for (decl,
22149 TYPE_ARG_TYPES (decl_type));
22151 nargs = list_length (decl_arg_types);
22152 args = XALLOCAVEC (tree, nargs);
22153 for (arg = decl_arg_types, ix = 0;
22154 arg != NULL_TREE && arg != void_list_node;
22155 arg = TREE_CHAIN (arg), ++ix)
22156 args[ix] = TREE_VALUE (arg);
22158 if (fn_type_unification (fn, explicit_args, targs,
22159 args, ix,
22160 (check_rettype || DECL_CONV_FN_P (fn)
22161 ? TREE_TYPE (decl_type) : NULL_TREE),
22162 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
22163 /*decltype*/false)
22164 == error_mark_node)
22165 return NULL_TREE;
22167 return targs;
22170 /* Return the innermost template arguments that, when applied to a partial
22171 specialization SPEC_TMPL of TMPL, yield the ARGS.
22173 For example, suppose we have:
22175 template <class T, class U> struct S {};
22176 template <class T> struct S<T*, int> {};
22178 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
22179 partial specialization and the ARGS will be {double*, int}. The resulting
22180 vector will be {double}, indicating that `T' is bound to `double'. */
22182 static tree
22183 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
22185 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
22186 tree spec_args
22187 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
22188 int i, ntparms = TREE_VEC_LENGTH (tparms);
22189 tree deduced_args;
22190 tree innermost_deduced_args;
22192 innermost_deduced_args = make_tree_vec (ntparms);
22193 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
22195 deduced_args = copy_node (args);
22196 SET_TMPL_ARGS_LEVEL (deduced_args,
22197 TMPL_ARGS_DEPTH (deduced_args),
22198 innermost_deduced_args);
22200 else
22201 deduced_args = innermost_deduced_args;
22203 bool tried_array_deduction = (cxx_dialect < cxx17);
22204 again:
22205 if (unify (tparms, deduced_args,
22206 INNERMOST_TEMPLATE_ARGS (spec_args),
22207 INNERMOST_TEMPLATE_ARGS (args),
22208 UNIFY_ALLOW_NONE, /*explain_p=*/false))
22209 return NULL_TREE;
22211 for (i = 0; i < ntparms; ++i)
22212 if (! TREE_VEC_ELT (innermost_deduced_args, i))
22214 if (!tried_array_deduction)
22216 try_array_deduction (tparms, innermost_deduced_args,
22217 INNERMOST_TEMPLATE_ARGS (spec_args));
22218 tried_array_deduction = true;
22219 if (TREE_VEC_ELT (innermost_deduced_args, i))
22220 goto again;
22222 return NULL_TREE;
22225 tree tinst = build_tree_list (spec_tmpl, deduced_args);
22226 if (!push_tinst_level (tinst))
22228 excessive_deduction_depth = true;
22229 return NULL_TREE;
22232 /* Verify that nondeduced template arguments agree with the type
22233 obtained from argument deduction.
22235 For example:
22237 struct A { typedef int X; };
22238 template <class T, class U> struct C {};
22239 template <class T> struct C<T, typename T::X> {};
22241 Then with the instantiation `C<A, int>', we can deduce that
22242 `T' is `A' but unify () does not check whether `typename T::X'
22243 is `int'. */
22244 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
22246 if (spec_args != error_mark_node)
22247 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
22248 INNERMOST_TEMPLATE_ARGS (spec_args),
22249 tmpl, tf_none, false, false);
22251 pop_tinst_level ();
22253 if (spec_args == error_mark_node
22254 /* We only need to check the innermost arguments; the other
22255 arguments will always agree. */
22256 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
22257 INNERMOST_TEMPLATE_ARGS (args)))
22258 return NULL_TREE;
22260 /* Now that we have bindings for all of the template arguments,
22261 ensure that the arguments deduced for the template template
22262 parameters have compatible template parameter lists. See the use
22263 of template_template_parm_bindings_ok_p in fn_type_unification
22264 for more information. */
22265 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
22266 return NULL_TREE;
22268 return deduced_args;
22271 // Compare two function templates T1 and T2 by deducing bindings
22272 // from one against the other. If both deductions succeed, compare
22273 // constraints to see which is more constrained.
22274 static int
22275 more_specialized_inst (tree t1, tree t2)
22277 int fate = 0;
22278 int count = 0;
22280 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
22282 --fate;
22283 ++count;
22286 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
22288 ++fate;
22289 ++count;
22292 // If both deductions succeed, then one may be more constrained.
22293 if (count == 2 && fate == 0)
22294 fate = more_constrained (t1, t2);
22296 return fate;
22299 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
22300 Return the TREE_LIST node with the most specialized template, if
22301 any. If there is no most specialized template, the error_mark_node
22302 is returned.
22304 Note that this function does not look at, or modify, the
22305 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
22306 returned is one of the elements of INSTANTIATIONS, callers may
22307 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
22308 and retrieve it from the value returned. */
22310 tree
22311 most_specialized_instantiation (tree templates)
22313 tree fn, champ;
22315 ++processing_template_decl;
22317 champ = templates;
22318 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
22320 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
22321 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
22322 if (fate == -1)
22323 champ = fn;
22324 else if (!fate)
22326 /* Equally specialized, move to next function. If there
22327 is no next function, nothing's most specialized. */
22328 fn = TREE_CHAIN (fn);
22329 champ = fn;
22330 if (!fn)
22331 break;
22335 if (champ)
22336 /* Now verify that champ is better than everything earlier in the
22337 instantiation list. */
22338 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
22339 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
22341 champ = NULL_TREE;
22342 break;
22346 processing_template_decl--;
22348 if (!champ)
22349 return error_mark_node;
22351 return champ;
22354 /* If DECL is a specialization of some template, return the most
22355 general such template. Otherwise, returns NULL_TREE.
22357 For example, given:
22359 template <class T> struct S { template <class U> void f(U); };
22361 if TMPL is `template <class U> void S<int>::f(U)' this will return
22362 the full template. This function will not trace past partial
22363 specializations, however. For example, given in addition:
22365 template <class T> struct S<T*> { template <class U> void f(U); };
22367 if TMPL is `template <class U> void S<int*>::f(U)' this will return
22368 `template <class T> template <class U> S<T*>::f(U)'. */
22370 tree
22371 most_general_template (tree decl)
22373 if (TREE_CODE (decl) != TEMPLATE_DECL)
22375 if (tree tinfo = get_template_info (decl))
22376 decl = TI_TEMPLATE (tinfo);
22377 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
22378 template friend, or a FIELD_DECL for a capture pack. */
22379 if (TREE_CODE (decl) != TEMPLATE_DECL)
22380 return NULL_TREE;
22383 /* Look for more and more general templates. */
22384 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
22386 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
22387 (See cp-tree.h for details.) */
22388 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
22389 break;
22391 if (CLASS_TYPE_P (TREE_TYPE (decl))
22392 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
22393 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
22394 break;
22396 /* Stop if we run into an explicitly specialized class template. */
22397 if (!DECL_NAMESPACE_SCOPE_P (decl)
22398 && DECL_CONTEXT (decl)
22399 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
22400 break;
22402 decl = DECL_TI_TEMPLATE (decl);
22405 return decl;
22408 /* Return the most specialized of the template partial specializations
22409 which can produce TARGET, a specialization of some class or variable
22410 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
22411 a TEMPLATE_DECL node corresponding to the partial specialization, while
22412 the TREE_PURPOSE is the set of template arguments that must be
22413 substituted into the template pattern in order to generate TARGET.
22415 If the choice of partial specialization is ambiguous, a diagnostic
22416 is issued, and the error_mark_node is returned. If there are no
22417 partial specializations matching TARGET, then NULL_TREE is
22418 returned, indicating that the primary template should be used. */
22420 static tree
22421 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
22423 tree list = NULL_TREE;
22424 tree t;
22425 tree champ;
22426 int fate;
22427 bool ambiguous_p;
22428 tree outer_args = NULL_TREE;
22429 tree tmpl, args;
22431 if (TYPE_P (target))
22433 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
22434 tmpl = TI_TEMPLATE (tinfo);
22435 args = TI_ARGS (tinfo);
22437 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
22439 tmpl = TREE_OPERAND (target, 0);
22440 args = TREE_OPERAND (target, 1);
22442 else if (VAR_P (target))
22444 tree tinfo = DECL_TEMPLATE_INFO (target);
22445 tmpl = TI_TEMPLATE (tinfo);
22446 args = TI_ARGS (tinfo);
22448 else
22449 gcc_unreachable ();
22451 tree main_tmpl = most_general_template (tmpl);
22453 /* For determining which partial specialization to use, only the
22454 innermost args are interesting. */
22455 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
22457 outer_args = strip_innermost_template_args (args, 1);
22458 args = INNERMOST_TEMPLATE_ARGS (args);
22461 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
22463 tree spec_args;
22464 tree spec_tmpl = TREE_VALUE (t);
22466 if (outer_args)
22468 /* Substitute in the template args from the enclosing class. */
22469 ++processing_template_decl;
22470 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
22471 --processing_template_decl;
22474 if (spec_tmpl == error_mark_node)
22475 return error_mark_node;
22477 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
22478 if (spec_args)
22480 if (outer_args)
22481 spec_args = add_to_template_args (outer_args, spec_args);
22483 /* Keep the candidate only if the constraints are satisfied,
22484 or if we're not compiling with concepts. */
22485 if (!flag_concepts
22486 || constraints_satisfied_p (spec_tmpl, spec_args))
22488 list = tree_cons (spec_args, TREE_VALUE (t), list);
22489 TREE_TYPE (list) = TREE_TYPE (t);
22494 if (! list)
22495 return NULL_TREE;
22497 ambiguous_p = false;
22498 t = list;
22499 champ = t;
22500 t = TREE_CHAIN (t);
22501 for (; t; t = TREE_CHAIN (t))
22503 fate = more_specialized_partial_spec (tmpl, champ, t);
22504 if (fate == 1)
22506 else
22508 if (fate == 0)
22510 t = TREE_CHAIN (t);
22511 if (! t)
22513 ambiguous_p = true;
22514 break;
22517 champ = t;
22521 if (!ambiguous_p)
22522 for (t = list; t && t != champ; t = TREE_CHAIN (t))
22524 fate = more_specialized_partial_spec (tmpl, champ, t);
22525 if (fate != 1)
22527 ambiguous_p = true;
22528 break;
22532 if (ambiguous_p)
22534 const char *str;
22535 char *spaces = NULL;
22536 if (!(complain & tf_error))
22537 return error_mark_node;
22538 if (TYPE_P (target))
22539 error ("ambiguous template instantiation for %q#T", target);
22540 else
22541 error ("ambiguous template instantiation for %q#D", target);
22542 str = ngettext ("candidate is:", "candidates are:", list_length (list));
22543 for (t = list; t; t = TREE_CHAIN (t))
22545 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
22546 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
22547 "%s %#qS", spaces ? spaces : str, subst);
22548 spaces = spaces ? spaces : get_spaces (str);
22550 free (spaces);
22551 return error_mark_node;
22554 return champ;
22557 /* Explicitly instantiate DECL. */
22559 void
22560 do_decl_instantiation (tree decl, tree storage)
22562 tree result = NULL_TREE;
22563 int extern_p = 0;
22565 if (!decl || decl == error_mark_node)
22566 /* An error occurred, for which grokdeclarator has already issued
22567 an appropriate message. */
22568 return;
22569 else if (! DECL_LANG_SPECIFIC (decl))
22571 error ("explicit instantiation of non-template %q#D", decl);
22572 return;
22575 bool var_templ = (DECL_TEMPLATE_INFO (decl)
22576 && variable_template_p (DECL_TI_TEMPLATE (decl)));
22578 if (VAR_P (decl) && !var_templ)
22580 /* There is an asymmetry here in the way VAR_DECLs and
22581 FUNCTION_DECLs are handled by grokdeclarator. In the case of
22582 the latter, the DECL we get back will be marked as a
22583 template instantiation, and the appropriate
22584 DECL_TEMPLATE_INFO will be set up. This does not happen for
22585 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
22586 should handle VAR_DECLs as it currently handles
22587 FUNCTION_DECLs. */
22588 if (!DECL_CLASS_SCOPE_P (decl))
22590 error ("%qD is not a static data member of a class template", decl);
22591 return;
22593 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
22594 if (!result || !VAR_P (result))
22596 error ("no matching template for %qD found", decl);
22597 return;
22599 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
22601 error ("type %qT for explicit instantiation %qD does not match "
22602 "declared type %qT", TREE_TYPE (result), decl,
22603 TREE_TYPE (decl));
22604 return;
22607 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
22609 error ("explicit instantiation of %q#D", decl);
22610 return;
22612 else
22613 result = decl;
22615 /* Check for various error cases. Note that if the explicit
22616 instantiation is valid the RESULT will currently be marked as an
22617 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
22618 until we get here. */
22620 if (DECL_TEMPLATE_SPECIALIZATION (result))
22622 /* DR 259 [temp.spec].
22624 Both an explicit instantiation and a declaration of an explicit
22625 specialization shall not appear in a program unless the explicit
22626 instantiation follows a declaration of the explicit specialization.
22628 For a given set of template parameters, if an explicit
22629 instantiation of a template appears after a declaration of an
22630 explicit specialization for that template, the explicit
22631 instantiation has no effect. */
22632 return;
22634 else if (DECL_EXPLICIT_INSTANTIATION (result))
22636 /* [temp.spec]
22638 No program shall explicitly instantiate any template more
22639 than once.
22641 We check DECL_NOT_REALLY_EXTERN so as not to complain when
22642 the first instantiation was `extern' and the second is not,
22643 and EXTERN_P for the opposite case. */
22644 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
22645 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
22646 /* If an "extern" explicit instantiation follows an ordinary
22647 explicit instantiation, the template is instantiated. */
22648 if (extern_p)
22649 return;
22651 else if (!DECL_IMPLICIT_INSTANTIATION (result))
22653 error ("no matching template for %qD found", result);
22654 return;
22656 else if (!DECL_TEMPLATE_INFO (result))
22658 permerror (input_location, "explicit instantiation of non-template %q#D", result);
22659 return;
22662 if (storage == NULL_TREE)
22664 else if (storage == ridpointers[(int) RID_EXTERN])
22666 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
22667 pedwarn (input_location, OPT_Wpedantic,
22668 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
22669 "instantiations");
22670 extern_p = 1;
22672 else
22673 error ("storage class %qD applied to template instantiation", storage);
22675 check_explicit_instantiation_namespace (result);
22676 mark_decl_instantiated (result, extern_p);
22677 if (! extern_p)
22678 instantiate_decl (result, /*defer_ok=*/true,
22679 /*expl_inst_class_mem_p=*/false);
22682 static void
22683 mark_class_instantiated (tree t, int extern_p)
22685 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
22686 SET_CLASSTYPE_INTERFACE_KNOWN (t);
22687 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
22688 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
22689 if (! extern_p)
22691 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
22692 rest_of_type_compilation (t, 1);
22696 /* Called from do_type_instantiation through binding_table_foreach to
22697 do recursive instantiation for the type bound in ENTRY. */
22698 static void
22699 bt_instantiate_type_proc (binding_entry entry, void *data)
22701 tree storage = *(tree *) data;
22703 if (MAYBE_CLASS_TYPE_P (entry->type)
22704 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
22705 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
22708 /* Perform an explicit instantiation of template class T. STORAGE, if
22709 non-null, is the RID for extern, inline or static. COMPLAIN is
22710 nonzero if this is called from the parser, zero if called recursively,
22711 since the standard is unclear (as detailed below). */
22713 void
22714 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
22716 int extern_p = 0;
22717 int nomem_p = 0;
22718 int static_p = 0;
22719 int previous_instantiation_extern_p = 0;
22721 if (TREE_CODE (t) == TYPE_DECL)
22722 t = TREE_TYPE (t);
22724 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
22726 tree tmpl =
22727 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
22728 if (tmpl)
22729 error ("explicit instantiation of non-class template %qD", tmpl);
22730 else
22731 error ("explicit instantiation of non-template type %qT", t);
22732 return;
22735 complete_type (t);
22737 if (!COMPLETE_TYPE_P (t))
22739 if (complain & tf_error)
22740 error ("explicit instantiation of %q#T before definition of template",
22742 return;
22745 if (storage != NULL_TREE)
22747 if (!in_system_header_at (input_location))
22749 if (storage == ridpointers[(int) RID_EXTERN])
22751 if (cxx_dialect == cxx98)
22752 pedwarn (input_location, OPT_Wpedantic,
22753 "ISO C++ 1998 forbids the use of %<extern%> on "
22754 "explicit instantiations");
22756 else
22757 pedwarn (input_location, OPT_Wpedantic,
22758 "ISO C++ forbids the use of %qE"
22759 " on explicit instantiations", storage);
22762 if (storage == ridpointers[(int) RID_INLINE])
22763 nomem_p = 1;
22764 else if (storage == ridpointers[(int) RID_EXTERN])
22765 extern_p = 1;
22766 else if (storage == ridpointers[(int) RID_STATIC])
22767 static_p = 1;
22768 else
22770 error ("storage class %qD applied to template instantiation",
22771 storage);
22772 extern_p = 0;
22776 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
22778 /* DR 259 [temp.spec].
22780 Both an explicit instantiation and a declaration of an explicit
22781 specialization shall not appear in a program unless the explicit
22782 instantiation follows a declaration of the explicit specialization.
22784 For a given set of template parameters, if an explicit
22785 instantiation of a template appears after a declaration of an
22786 explicit specialization for that template, the explicit
22787 instantiation has no effect. */
22788 return;
22790 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
22792 /* [temp.spec]
22794 No program shall explicitly instantiate any template more
22795 than once.
22797 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
22798 instantiation was `extern'. If EXTERN_P then the second is.
22799 These cases are OK. */
22800 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
22802 if (!previous_instantiation_extern_p && !extern_p
22803 && (complain & tf_error))
22804 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
22806 /* If we've already instantiated the template, just return now. */
22807 if (!CLASSTYPE_INTERFACE_ONLY (t))
22808 return;
22811 check_explicit_instantiation_namespace (TYPE_NAME (t));
22812 mark_class_instantiated (t, extern_p);
22814 if (nomem_p)
22815 return;
22817 /* In contrast to implicit instantiation, where only the
22818 declarations, and not the definitions, of members are
22819 instantiated, we have here:
22821 [temp.explicit]
22823 The explicit instantiation of a class template specialization
22824 implies the instantiation of all of its members not
22825 previously explicitly specialized in the translation unit
22826 containing the explicit instantiation.
22828 Of course, we can't instantiate member template classes, since we
22829 don't have any arguments for them. Note that the standard is
22830 unclear on whether the instantiation of the members are
22831 *explicit* instantiations or not. However, the most natural
22832 interpretation is that it should be an explicit
22833 instantiation. */
22834 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
22835 if ((VAR_P (fld)
22836 || (TREE_CODE (fld) == FUNCTION_DECL
22837 && !static_p
22838 && user_provided_p (fld)))
22839 && DECL_TEMPLATE_INSTANTIATION (fld))
22841 mark_decl_instantiated (fld, extern_p);
22842 if (! extern_p)
22843 instantiate_decl (fld, /*defer_ok=*/true,
22844 /*expl_inst_class_mem_p=*/true);
22847 if (CLASSTYPE_NESTED_UTDS (t))
22848 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
22849 bt_instantiate_type_proc, &storage);
22852 /* Given a function DECL, which is a specialization of TMPL, modify
22853 DECL to be a re-instantiation of TMPL with the same template
22854 arguments. TMPL should be the template into which tsubst'ing
22855 should occur for DECL, not the most general template.
22857 One reason for doing this is a scenario like this:
22859 template <class T>
22860 void f(const T&, int i);
22862 void g() { f(3, 7); }
22864 template <class T>
22865 void f(const T& t, const int i) { }
22867 Note that when the template is first instantiated, with
22868 instantiate_template, the resulting DECL will have no name for the
22869 first parameter, and the wrong type for the second. So, when we go
22870 to instantiate the DECL, we regenerate it. */
22872 static void
22873 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
22875 /* The arguments used to instantiate DECL, from the most general
22876 template. */
22877 tree code_pattern;
22879 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
22881 /* Make sure that we can see identifiers, and compute access
22882 correctly. */
22883 push_access_scope (decl);
22885 if (TREE_CODE (decl) == FUNCTION_DECL)
22887 tree decl_parm;
22888 tree pattern_parm;
22889 tree specs;
22890 int args_depth;
22891 int parms_depth;
22893 args_depth = TMPL_ARGS_DEPTH (args);
22894 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
22895 if (args_depth > parms_depth)
22896 args = get_innermost_template_args (args, parms_depth);
22898 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
22899 args, tf_error, NULL_TREE,
22900 /*defer_ok*/false);
22901 if (specs && specs != error_mark_node)
22902 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
22903 specs);
22905 /* Merge parameter declarations. */
22906 decl_parm = skip_artificial_parms_for (decl,
22907 DECL_ARGUMENTS (decl));
22908 pattern_parm
22909 = skip_artificial_parms_for (code_pattern,
22910 DECL_ARGUMENTS (code_pattern));
22911 while (decl_parm && !DECL_PACK_P (pattern_parm))
22913 tree parm_type;
22914 tree attributes;
22916 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22917 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
22918 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
22919 NULL_TREE);
22920 parm_type = type_decays_to (parm_type);
22921 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22922 TREE_TYPE (decl_parm) = parm_type;
22923 attributes = DECL_ATTRIBUTES (pattern_parm);
22924 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22926 DECL_ATTRIBUTES (decl_parm) = attributes;
22927 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22929 decl_parm = DECL_CHAIN (decl_parm);
22930 pattern_parm = DECL_CHAIN (pattern_parm);
22932 /* Merge any parameters that match with the function parameter
22933 pack. */
22934 if (pattern_parm && DECL_PACK_P (pattern_parm))
22936 int i, len;
22937 tree expanded_types;
22938 /* Expand the TYPE_PACK_EXPANSION that provides the types for
22939 the parameters in this function parameter pack. */
22940 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
22941 args, tf_error, NULL_TREE);
22942 len = TREE_VEC_LENGTH (expanded_types);
22943 for (i = 0; i < len; i++)
22945 tree parm_type;
22946 tree attributes;
22948 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22949 /* Rename the parameter to include the index. */
22950 DECL_NAME (decl_parm) =
22951 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
22952 parm_type = TREE_VEC_ELT (expanded_types, i);
22953 parm_type = type_decays_to (parm_type);
22954 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22955 TREE_TYPE (decl_parm) = parm_type;
22956 attributes = DECL_ATTRIBUTES (pattern_parm);
22957 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22959 DECL_ATTRIBUTES (decl_parm) = attributes;
22960 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22962 decl_parm = DECL_CHAIN (decl_parm);
22965 /* Merge additional specifiers from the CODE_PATTERN. */
22966 if (DECL_DECLARED_INLINE_P (code_pattern)
22967 && !DECL_DECLARED_INLINE_P (decl))
22968 DECL_DECLARED_INLINE_P (decl) = 1;
22970 else if (VAR_P (decl))
22972 start_lambda_scope (decl);
22973 DECL_INITIAL (decl) =
22974 tsubst_expr (DECL_INITIAL (code_pattern), args,
22975 tf_error, DECL_TI_TEMPLATE (decl),
22976 /*integral_constant_expression_p=*/false);
22977 finish_lambda_scope ();
22978 if (VAR_HAD_UNKNOWN_BOUND (decl))
22979 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
22980 tf_error, DECL_TI_TEMPLATE (decl));
22982 else
22983 gcc_unreachable ();
22985 pop_access_scope (decl);
22988 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
22989 substituted to get DECL. */
22991 tree
22992 template_for_substitution (tree decl)
22994 tree tmpl = DECL_TI_TEMPLATE (decl);
22996 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
22997 for the instantiation. This is not always the most general
22998 template. Consider, for example:
23000 template <class T>
23001 struct S { template <class U> void f();
23002 template <> void f<int>(); };
23004 and an instantiation of S<double>::f<int>. We want TD to be the
23005 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
23006 while (/* An instantiation cannot have a definition, so we need a
23007 more general template. */
23008 DECL_TEMPLATE_INSTANTIATION (tmpl)
23009 /* We must also deal with friend templates. Given:
23011 template <class T> struct S {
23012 template <class U> friend void f() {};
23015 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
23016 so far as the language is concerned, but that's still
23017 where we get the pattern for the instantiation from. On
23018 other hand, if the definition comes outside the class, say:
23020 template <class T> struct S {
23021 template <class U> friend void f();
23023 template <class U> friend void f() {}
23025 we don't need to look any further. That's what the check for
23026 DECL_INITIAL is for. */
23027 || (TREE_CODE (decl) == FUNCTION_DECL
23028 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
23029 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
23031 /* The present template, TD, should not be a definition. If it
23032 were a definition, we should be using it! Note that we
23033 cannot restructure the loop to just keep going until we find
23034 a template with a definition, since that might go too far if
23035 a specialization was declared, but not defined. */
23037 /* Fetch the more general template. */
23038 tmpl = DECL_TI_TEMPLATE (tmpl);
23041 return tmpl;
23044 /* Returns true if we need to instantiate this template instance even if we
23045 know we aren't going to emit it. */
23047 bool
23048 always_instantiate_p (tree decl)
23050 /* We always instantiate inline functions so that we can inline them. An
23051 explicit instantiation declaration prohibits implicit instantiation of
23052 non-inline functions. With high levels of optimization, we would
23053 normally inline non-inline functions -- but we're not allowed to do
23054 that for "extern template" functions. Therefore, we check
23055 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
23056 return ((TREE_CODE (decl) == FUNCTION_DECL
23057 && (DECL_DECLARED_INLINE_P (decl)
23058 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
23059 /* And we need to instantiate static data members so that
23060 their initializers are available in integral constant
23061 expressions. */
23062 || (VAR_P (decl)
23063 && decl_maybe_constant_var_p (decl)));
23066 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
23067 instantiate it now, modifying TREE_TYPE (fn). Returns false on
23068 error, true otherwise. */
23070 bool
23071 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
23073 tree fntype, spec, noex, clone;
23075 /* Don't instantiate a noexcept-specification from template context. */
23076 if (processing_template_decl)
23077 return true;
23079 if (DECL_CLONED_FUNCTION_P (fn))
23080 fn = DECL_CLONED_FUNCTION (fn);
23081 fntype = TREE_TYPE (fn);
23082 spec = TYPE_RAISES_EXCEPTIONS (fntype);
23084 if (!spec || !TREE_PURPOSE (spec))
23085 return true;
23087 noex = TREE_PURPOSE (spec);
23089 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
23091 static hash_set<tree>* fns = new hash_set<tree>;
23092 bool added = false;
23093 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
23094 spec = get_defaulted_eh_spec (fn, complain);
23095 else if (!(added = !fns->add (fn)))
23097 /* If hash_set::add returns true, the element was already there. */
23098 location_t loc = EXPR_LOC_OR_LOC (DEFERRED_NOEXCEPT_PATTERN (noex),
23099 DECL_SOURCE_LOCATION (fn));
23100 error_at (loc,
23101 "exception specification of %qD depends on itself",
23102 fn);
23103 spec = noexcept_false_spec;
23105 else if (push_tinst_level (fn))
23107 push_access_scope (fn);
23108 push_deferring_access_checks (dk_no_deferred);
23109 input_location = DECL_SOURCE_LOCATION (fn);
23110 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
23111 DEFERRED_NOEXCEPT_ARGS (noex),
23112 tf_warning_or_error, fn,
23113 /*function_p=*/false,
23114 /*integral_constant_expression_p=*/true);
23115 pop_deferring_access_checks ();
23116 pop_access_scope (fn);
23117 pop_tinst_level ();
23118 spec = build_noexcept_spec (noex, tf_warning_or_error);
23119 if (spec == error_mark_node)
23120 spec = noexcept_false_spec;
23122 else
23123 spec = noexcept_false_spec;
23125 if (added)
23126 fns->remove (fn);
23128 if (spec == error_mark_node)
23129 return false;
23131 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
23134 FOR_EACH_CLONE (clone, fn)
23136 if (TREE_TYPE (clone) == fntype)
23137 TREE_TYPE (clone) = TREE_TYPE (fn);
23138 else
23139 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
23142 return true;
23145 /* We're starting to process the function INST, an instantiation of PATTERN;
23146 add their parameters to local_specializations. */
23148 static void
23149 register_parameter_specializations (tree pattern, tree inst)
23151 tree tmpl_parm = DECL_ARGUMENTS (pattern);
23152 tree spec_parm = DECL_ARGUMENTS (inst);
23153 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
23155 register_local_specialization (spec_parm, tmpl_parm);
23156 spec_parm = skip_artificial_parms_for (inst, spec_parm);
23157 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
23159 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
23161 if (!DECL_PACK_P (tmpl_parm))
23163 register_local_specialization (spec_parm, tmpl_parm);
23164 spec_parm = DECL_CHAIN (spec_parm);
23166 else
23168 /* Register the (value) argument pack as a specialization of
23169 TMPL_PARM, then move on. */
23170 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
23171 register_local_specialization (argpack, tmpl_parm);
23174 gcc_assert (!spec_parm);
23177 /* Produce the definition of D, a _DECL generated from a template. If
23178 DEFER_OK is true, then we don't have to actually do the
23179 instantiation now; we just have to do it sometime. Normally it is
23180 an error if this is an explicit instantiation but D is undefined.
23181 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
23182 instantiated class template. */
23184 tree
23185 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
23187 tree tmpl = DECL_TI_TEMPLATE (d);
23188 tree gen_args;
23189 tree args;
23190 tree td;
23191 tree code_pattern;
23192 tree spec;
23193 tree gen_tmpl;
23194 bool pattern_defined;
23195 location_t saved_loc = input_location;
23196 int saved_unevaluated_operand = cp_unevaluated_operand;
23197 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
23198 bool external_p;
23199 bool deleted_p;
23201 /* This function should only be used to instantiate templates for
23202 functions and static member variables. */
23203 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
23205 /* A concept is never instantiated. */
23206 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
23208 /* Variables are never deferred; if instantiation is required, they
23209 are instantiated right away. That allows for better code in the
23210 case that an expression refers to the value of the variable --
23211 if the variable has a constant value the referring expression can
23212 take advantage of that fact. */
23213 if (VAR_P (d))
23214 defer_ok = false;
23216 /* Don't instantiate cloned functions. Instead, instantiate the
23217 functions they cloned. */
23218 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
23219 d = DECL_CLONED_FUNCTION (d);
23221 if (DECL_TEMPLATE_INSTANTIATED (d)
23222 || (TREE_CODE (d) == FUNCTION_DECL
23223 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
23224 || DECL_TEMPLATE_SPECIALIZATION (d))
23225 /* D has already been instantiated or explicitly specialized, so
23226 there's nothing for us to do here.
23228 It might seem reasonable to check whether or not D is an explicit
23229 instantiation, and, if so, stop here. But when an explicit
23230 instantiation is deferred until the end of the compilation,
23231 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
23232 the instantiation. */
23233 return d;
23235 /* Check to see whether we know that this template will be
23236 instantiated in some other file, as with "extern template"
23237 extension. */
23238 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
23240 /* In general, we do not instantiate such templates. */
23241 if (external_p && !always_instantiate_p (d))
23242 return d;
23244 gen_tmpl = most_general_template (tmpl);
23245 gen_args = DECL_TI_ARGS (d);
23247 if (tmpl != gen_tmpl)
23248 /* We should already have the extra args. */
23249 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
23250 == TMPL_ARGS_DEPTH (gen_args));
23251 /* And what's in the hash table should match D. */
23252 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
23253 || spec == NULL_TREE);
23255 /* This needs to happen before any tsubsting. */
23256 if (! push_tinst_level (d))
23257 return d;
23259 timevar_push (TV_TEMPLATE_INST);
23261 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
23262 for the instantiation. */
23263 td = template_for_substitution (d);
23264 args = gen_args;
23266 if (VAR_P (d))
23268 /* Look up an explicit specialization, if any. */
23269 tree tid = lookup_template_variable (gen_tmpl, gen_args);
23270 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
23271 if (elt && elt != error_mark_node)
23273 td = TREE_VALUE (elt);
23274 args = TREE_PURPOSE (elt);
23278 code_pattern = DECL_TEMPLATE_RESULT (td);
23280 /* We should never be trying to instantiate a member of a class
23281 template or partial specialization. */
23282 gcc_assert (d != code_pattern);
23284 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
23285 || DECL_TEMPLATE_SPECIALIZATION (td))
23286 /* In the case of a friend template whose definition is provided
23287 outside the class, we may have too many arguments. Drop the
23288 ones we don't need. The same is true for specializations. */
23289 args = get_innermost_template_args
23290 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
23292 if (TREE_CODE (d) == FUNCTION_DECL)
23294 deleted_p = DECL_DELETED_FN (code_pattern);
23295 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
23296 && DECL_INITIAL (code_pattern) != error_mark_node)
23297 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
23298 || deleted_p);
23300 else
23302 deleted_p = false;
23303 if (DECL_CLASS_SCOPE_P (code_pattern))
23304 pattern_defined = (! DECL_IN_AGGR_P (code_pattern)
23305 || DECL_INLINE_VAR_P (code_pattern));
23306 else
23307 pattern_defined = ! DECL_EXTERNAL (code_pattern);
23310 /* We may be in the middle of deferred access check. Disable it now. */
23311 push_deferring_access_checks (dk_no_deferred);
23313 /* Unless an explicit instantiation directive has already determined
23314 the linkage of D, remember that a definition is available for
23315 this entity. */
23316 if (pattern_defined
23317 && !DECL_INTERFACE_KNOWN (d)
23318 && !DECL_NOT_REALLY_EXTERN (d))
23319 mark_definable (d);
23321 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
23322 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
23323 input_location = DECL_SOURCE_LOCATION (d);
23325 /* If D is a member of an explicitly instantiated class template,
23326 and no definition is available, treat it like an implicit
23327 instantiation. */
23328 if (!pattern_defined && expl_inst_class_mem_p
23329 && DECL_EXPLICIT_INSTANTIATION (d))
23331 /* Leave linkage flags alone on instantiations with anonymous
23332 visibility. */
23333 if (TREE_PUBLIC (d))
23335 DECL_NOT_REALLY_EXTERN (d) = 0;
23336 DECL_INTERFACE_KNOWN (d) = 0;
23338 SET_DECL_IMPLICIT_INSTANTIATION (d);
23341 /* Defer all other templates, unless we have been explicitly
23342 forbidden from doing so. */
23343 if (/* If there is no definition, we cannot instantiate the
23344 template. */
23345 ! pattern_defined
23346 /* If it's OK to postpone instantiation, do so. */
23347 || defer_ok
23348 /* If this is a static data member that will be defined
23349 elsewhere, we don't want to instantiate the entire data
23350 member, but we do want to instantiate the initializer so that
23351 we can substitute that elsewhere. */
23352 || (external_p && VAR_P (d))
23353 /* Handle here a deleted function too, avoid generating
23354 its body (c++/61080). */
23355 || deleted_p)
23357 /* The definition of the static data member is now required so
23358 we must substitute the initializer. */
23359 if (VAR_P (d)
23360 && !DECL_INITIAL (d)
23361 && DECL_INITIAL (code_pattern))
23363 tree ns;
23364 tree init;
23365 bool const_init = false;
23366 bool enter_context = DECL_CLASS_SCOPE_P (d);
23368 ns = decl_namespace_context (d);
23369 push_nested_namespace (ns);
23370 if (enter_context)
23371 push_nested_class (DECL_CONTEXT (d));
23372 init = tsubst_expr (DECL_INITIAL (code_pattern),
23373 args,
23374 tf_warning_or_error, NULL_TREE,
23375 /*integral_constant_expression_p=*/false);
23376 /* If instantiating the initializer involved instantiating this
23377 again, don't call cp_finish_decl twice. */
23378 if (!DECL_INITIAL (d))
23380 /* Make sure the initializer is still constant, in case of
23381 circular dependency (template/instantiate6.C). */
23382 const_init
23383 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
23384 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
23385 /*asmspec_tree=*/NULL_TREE,
23386 LOOKUP_ONLYCONVERTING);
23388 if (enter_context)
23389 pop_nested_class ();
23390 pop_nested_namespace (ns);
23393 /* We restore the source position here because it's used by
23394 add_pending_template. */
23395 input_location = saved_loc;
23397 if (at_eof && !pattern_defined
23398 && DECL_EXPLICIT_INSTANTIATION (d)
23399 && DECL_NOT_REALLY_EXTERN (d))
23400 /* [temp.explicit]
23402 The definition of a non-exported function template, a
23403 non-exported member function template, or a non-exported
23404 member function or static data member of a class template
23405 shall be present in every translation unit in which it is
23406 explicitly instantiated. */
23407 permerror (input_location, "explicit instantiation of %qD "
23408 "but no definition available", d);
23410 /* If we're in unevaluated context, we just wanted to get the
23411 constant value; this isn't an odr use, so don't queue
23412 a full instantiation. */
23413 if (cp_unevaluated_operand != 0)
23414 goto out;
23415 /* ??? Historically, we have instantiated inline functions, even
23416 when marked as "extern template". */
23417 if (!(external_p && VAR_P (d)))
23418 add_pending_template (d);
23419 goto out;
23421 /* Tell the repository that D is available in this translation unit
23422 -- and see if it is supposed to be instantiated here. */
23423 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
23425 /* In a PCH file, despite the fact that the repository hasn't
23426 requested instantiation in the PCH it is still possible that
23427 an instantiation will be required in a file that includes the
23428 PCH. */
23429 if (pch_file)
23430 add_pending_template (d);
23431 /* Instantiate inline functions so that the inliner can do its
23432 job, even though we'll not be emitting a copy of this
23433 function. */
23434 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
23435 goto out;
23438 bool push_to_top, nested;
23439 tree fn_context;
23440 fn_context = decl_function_context (d);
23441 nested = current_function_decl != NULL_TREE;
23442 push_to_top = !(nested && fn_context == current_function_decl);
23444 vec<tree> omp_privatization_save;
23445 if (nested)
23446 save_omp_privatization_clauses (omp_privatization_save);
23448 if (push_to_top)
23449 push_to_top_level ();
23450 else
23452 push_function_context ();
23453 cp_unevaluated_operand = 0;
23454 c_inhibit_evaluation_warnings = 0;
23457 /* Mark D as instantiated so that recursive calls to
23458 instantiate_decl do not try to instantiate it again. */
23459 DECL_TEMPLATE_INSTANTIATED (d) = 1;
23461 /* Regenerate the declaration in case the template has been modified
23462 by a subsequent redeclaration. */
23463 regenerate_decl_from_template (d, td, args);
23465 /* We already set the file and line above. Reset them now in case
23466 they changed as a result of calling regenerate_decl_from_template. */
23467 input_location = DECL_SOURCE_LOCATION (d);
23469 if (VAR_P (d))
23471 tree init;
23472 bool const_init = false;
23474 /* Clear out DECL_RTL; whatever was there before may not be right
23475 since we've reset the type of the declaration. */
23476 SET_DECL_RTL (d, NULL);
23477 DECL_IN_AGGR_P (d) = 0;
23479 /* The initializer is placed in DECL_INITIAL by
23480 regenerate_decl_from_template so we don't need to
23481 push/pop_access_scope again here. Pull it out so that
23482 cp_finish_decl can process it. */
23483 init = DECL_INITIAL (d);
23484 DECL_INITIAL (d) = NULL_TREE;
23485 DECL_INITIALIZED_P (d) = 0;
23487 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
23488 initializer. That function will defer actual emission until
23489 we have a chance to determine linkage. */
23490 DECL_EXTERNAL (d) = 0;
23492 /* Enter the scope of D so that access-checking works correctly. */
23493 bool enter_context = DECL_CLASS_SCOPE_P (d);
23494 if (enter_context)
23495 push_nested_class (DECL_CONTEXT (d));
23497 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
23498 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
23500 if (enter_context)
23501 pop_nested_class ();
23503 if (variable_template_p (gen_tmpl))
23504 note_variable_template_instantiation (d);
23506 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
23507 synthesize_method (d);
23508 else if (TREE_CODE (d) == FUNCTION_DECL)
23510 /* Set up the list of local specializations. */
23511 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
23512 tree block = NULL_TREE;
23514 /* Set up context. */
23515 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23516 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23517 block = push_stmt_list ();
23518 else
23519 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
23521 /* Some typedefs referenced from within the template code need to be
23522 access checked at template instantiation time, i.e now. These
23523 types were added to the template at parsing time. Let's get those
23524 and perform the access checks then. */
23525 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
23526 args);
23528 /* Create substitution entries for the parameters. */
23529 register_parameter_specializations (code_pattern, d);
23531 /* Substitute into the body of the function. */
23532 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23533 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
23534 tf_warning_or_error, tmpl);
23535 else
23537 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
23538 tf_warning_or_error, tmpl,
23539 /*integral_constant_expression_p=*/false);
23541 /* Set the current input_location to the end of the function
23542 so that finish_function knows where we are. */
23543 input_location
23544 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
23546 /* Remember if we saw an infinite loop in the template. */
23547 current_function_infinite_loop
23548 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
23551 /* Finish the function. */
23552 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23553 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23554 DECL_SAVED_TREE (d) = pop_stmt_list (block);
23555 else
23557 d = finish_function (/*inline_p=*/false);
23558 expand_or_defer_fn (d);
23561 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23562 cp_check_omp_declare_reduction (d);
23565 /* We're not deferring instantiation any more. */
23566 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
23568 if (push_to_top)
23569 pop_from_top_level ();
23570 else
23571 pop_function_context ();
23573 if (nested)
23574 restore_omp_privatization_clauses (omp_privatization_save);
23576 out:
23577 pop_deferring_access_checks ();
23578 timevar_pop (TV_TEMPLATE_INST);
23579 pop_tinst_level ();
23580 input_location = saved_loc;
23581 cp_unevaluated_operand = saved_unevaluated_operand;
23582 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23584 return d;
23587 /* Run through the list of templates that we wish we could
23588 instantiate, and instantiate any we can. RETRIES is the
23589 number of times we retry pending template instantiation. */
23591 void
23592 instantiate_pending_templates (int retries)
23594 int reconsider;
23595 location_t saved_loc = input_location;
23597 /* Instantiating templates may trigger vtable generation. This in turn
23598 may require further template instantiations. We place a limit here
23599 to avoid infinite loop. */
23600 if (pending_templates && retries >= max_tinst_depth)
23602 tree decl = pending_templates->tinst->decl;
23604 fatal_error (input_location,
23605 "template instantiation depth exceeds maximum of %d"
23606 " instantiating %q+D, possibly from virtual table generation"
23607 " (use -ftemplate-depth= to increase the maximum)",
23608 max_tinst_depth, decl);
23609 if (TREE_CODE (decl) == FUNCTION_DECL)
23610 /* Pretend that we defined it. */
23611 DECL_INITIAL (decl) = error_mark_node;
23612 return;
23617 struct pending_template **t = &pending_templates;
23618 struct pending_template *last = NULL;
23619 reconsider = 0;
23620 while (*t)
23622 tree instantiation = reopen_tinst_level ((*t)->tinst);
23623 bool complete = false;
23625 if (TYPE_P (instantiation))
23627 if (!COMPLETE_TYPE_P (instantiation))
23629 instantiate_class_template (instantiation);
23630 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
23631 for (tree fld = TYPE_FIELDS (instantiation);
23632 fld; fld = TREE_CHAIN (fld))
23633 if ((VAR_P (fld)
23634 || (TREE_CODE (fld) == FUNCTION_DECL
23635 && !DECL_ARTIFICIAL (fld)))
23636 && DECL_TEMPLATE_INSTANTIATION (fld))
23637 instantiate_decl (fld,
23638 /*defer_ok=*/false,
23639 /*expl_inst_class_mem_p=*/false);
23641 if (COMPLETE_TYPE_P (instantiation))
23642 reconsider = 1;
23645 complete = COMPLETE_TYPE_P (instantiation);
23647 else
23649 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
23650 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
23652 instantiation
23653 = instantiate_decl (instantiation,
23654 /*defer_ok=*/false,
23655 /*expl_inst_class_mem_p=*/false);
23656 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
23657 reconsider = 1;
23660 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
23661 || DECL_TEMPLATE_INSTANTIATED (instantiation));
23664 if (complete)
23665 /* If INSTANTIATION has been instantiated, then we don't
23666 need to consider it again in the future. */
23667 *t = (*t)->next;
23668 else
23670 last = *t;
23671 t = &(*t)->next;
23673 tinst_depth = 0;
23674 current_tinst_level = NULL;
23676 last_pending_template = last;
23678 while (reconsider);
23680 input_location = saved_loc;
23683 /* Substitute ARGVEC into T, which is a list of initializers for
23684 either base class or a non-static data member. The TREE_PURPOSEs
23685 are DECLs, and the TREE_VALUEs are the initializer values. Used by
23686 instantiate_decl. */
23688 static tree
23689 tsubst_initializer_list (tree t, tree argvec)
23691 tree inits = NULL_TREE;
23692 tree target_ctor = error_mark_node;
23694 for (; t; t = TREE_CHAIN (t))
23696 tree decl;
23697 tree init;
23698 tree expanded_bases = NULL_TREE;
23699 tree expanded_arguments = NULL_TREE;
23700 int i, len = 1;
23702 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
23704 tree expr;
23705 tree arg;
23707 /* Expand the base class expansion type into separate base
23708 classes. */
23709 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
23710 tf_warning_or_error,
23711 NULL_TREE);
23712 if (expanded_bases == error_mark_node)
23713 continue;
23715 /* We'll be building separate TREE_LISTs of arguments for
23716 each base. */
23717 len = TREE_VEC_LENGTH (expanded_bases);
23718 expanded_arguments = make_tree_vec (len);
23719 for (i = 0; i < len; i++)
23720 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
23722 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
23723 expand each argument in the TREE_VALUE of t. */
23724 expr = make_node (EXPR_PACK_EXPANSION);
23725 PACK_EXPANSION_LOCAL_P (expr) = true;
23726 PACK_EXPANSION_PARAMETER_PACKS (expr) =
23727 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
23729 if (TREE_VALUE (t) == void_type_node)
23730 /* VOID_TYPE_NODE is used to indicate
23731 value-initialization. */
23733 for (i = 0; i < len; i++)
23734 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
23736 else
23738 /* Substitute parameter packs into each argument in the
23739 TREE_LIST. */
23740 in_base_initializer = 1;
23741 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
23743 tree expanded_exprs;
23745 /* Expand the argument. */
23746 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
23747 expanded_exprs
23748 = tsubst_pack_expansion (expr, argvec,
23749 tf_warning_or_error,
23750 NULL_TREE);
23751 if (expanded_exprs == error_mark_node)
23752 continue;
23754 /* Prepend each of the expanded expressions to the
23755 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
23756 for (i = 0; i < len; i++)
23758 TREE_VEC_ELT (expanded_arguments, i) =
23759 tree_cons (NULL_TREE,
23760 TREE_VEC_ELT (expanded_exprs, i),
23761 TREE_VEC_ELT (expanded_arguments, i));
23764 in_base_initializer = 0;
23766 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
23767 since we built them backwards. */
23768 for (i = 0; i < len; i++)
23770 TREE_VEC_ELT (expanded_arguments, i) =
23771 nreverse (TREE_VEC_ELT (expanded_arguments, i));
23776 for (i = 0; i < len; ++i)
23778 if (expanded_bases)
23780 decl = TREE_VEC_ELT (expanded_bases, i);
23781 decl = expand_member_init (decl);
23782 init = TREE_VEC_ELT (expanded_arguments, i);
23784 else
23786 tree tmp;
23787 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
23788 tf_warning_or_error, NULL_TREE);
23790 decl = expand_member_init (decl);
23791 if (decl && !DECL_P (decl))
23792 in_base_initializer = 1;
23794 init = TREE_VALUE (t);
23795 tmp = init;
23796 if (init != void_type_node)
23797 init = tsubst_expr (init, argvec,
23798 tf_warning_or_error, NULL_TREE,
23799 /*integral_constant_expression_p=*/false);
23800 if (init == NULL_TREE && tmp != NULL_TREE)
23801 /* If we had an initializer but it instantiated to nothing,
23802 value-initialize the object. This will only occur when
23803 the initializer was a pack expansion where the parameter
23804 packs used in that expansion were of length zero. */
23805 init = void_type_node;
23806 in_base_initializer = 0;
23809 if (target_ctor != error_mark_node
23810 && init != error_mark_node)
23812 error ("mem-initializer for %qD follows constructor delegation",
23813 decl);
23814 return inits;
23816 /* Look for a target constructor. */
23817 if (init != error_mark_node
23818 && decl && CLASS_TYPE_P (decl)
23819 && same_type_p (decl, current_class_type))
23821 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
23822 if (inits)
23824 error ("constructor delegation follows mem-initializer for %qD",
23825 TREE_PURPOSE (inits));
23826 continue;
23828 target_ctor = init;
23831 if (decl)
23833 init = build_tree_list (decl, init);
23834 TREE_CHAIN (init) = inits;
23835 inits = init;
23839 return inits;
23842 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
23844 static void
23845 set_current_access_from_decl (tree decl)
23847 if (TREE_PRIVATE (decl))
23848 current_access_specifier = access_private_node;
23849 else if (TREE_PROTECTED (decl))
23850 current_access_specifier = access_protected_node;
23851 else
23852 current_access_specifier = access_public_node;
23855 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
23856 is the instantiation (which should have been created with
23857 start_enum) and ARGS are the template arguments to use. */
23859 static void
23860 tsubst_enum (tree tag, tree newtag, tree args)
23862 tree e;
23864 if (SCOPED_ENUM_P (newtag))
23865 begin_scope (sk_scoped_enum, newtag);
23867 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
23869 tree value;
23870 tree decl;
23872 decl = TREE_VALUE (e);
23873 /* Note that in a template enum, the TREE_VALUE is the
23874 CONST_DECL, not the corresponding INTEGER_CST. */
23875 value = tsubst_expr (DECL_INITIAL (decl),
23876 args, tf_warning_or_error, NULL_TREE,
23877 /*integral_constant_expression_p=*/true);
23879 /* Give this enumeration constant the correct access. */
23880 set_current_access_from_decl (decl);
23882 /* Actually build the enumerator itself. Here we're assuming that
23883 enumerators can't have dependent attributes. */
23884 build_enumerator (DECL_NAME (decl), value, newtag,
23885 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
23888 if (SCOPED_ENUM_P (newtag))
23889 finish_scope ();
23891 finish_enum_value_list (newtag);
23892 finish_enum (newtag);
23894 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
23895 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
23898 /* DECL is a FUNCTION_DECL that is a template specialization. Return
23899 its type -- but without substituting the innermost set of template
23900 arguments. So, innermost set of template parameters will appear in
23901 the type. */
23903 tree
23904 get_mostly_instantiated_function_type (tree decl)
23906 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
23907 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
23910 /* Return truthvalue if we're processing a template different from
23911 the last one involved in diagnostics. */
23912 bool
23913 problematic_instantiation_changed (void)
23915 return current_tinst_level != last_error_tinst_level;
23918 /* Remember current template involved in diagnostics. */
23919 void
23920 record_last_problematic_instantiation (void)
23922 last_error_tinst_level = current_tinst_level;
23925 struct tinst_level *
23926 current_instantiation (void)
23928 return current_tinst_level;
23931 /* Return TRUE if current_function_decl is being instantiated, false
23932 otherwise. */
23934 bool
23935 instantiating_current_function_p (void)
23937 return (current_instantiation ()
23938 && current_instantiation ()->decl == current_function_decl);
23941 /* [temp.param] Check that template non-type parm TYPE is of an allowable
23942 type. Return false for ok, true for disallowed. Issue error and
23943 inform messages under control of COMPLAIN. */
23945 static bool
23946 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
23948 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
23949 return false;
23950 else if (POINTER_TYPE_P (type))
23951 return false;
23952 else if (TYPE_PTRMEM_P (type))
23953 return false;
23954 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
23955 return false;
23956 else if (TREE_CODE (type) == TYPENAME_TYPE)
23957 return false;
23958 else if (TREE_CODE (type) == DECLTYPE_TYPE)
23959 return false;
23960 else if (TREE_CODE (type) == NULLPTR_TYPE)
23961 return false;
23962 /* A bound template template parm could later be instantiated to have a valid
23963 nontype parm type via an alias template. */
23964 else if (cxx_dialect >= cxx11
23965 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23966 return false;
23968 if (complain & tf_error)
23970 if (type == error_mark_node)
23971 inform (input_location, "invalid template non-type parameter");
23972 else
23973 error ("%q#T is not a valid type for a template non-type parameter",
23974 type);
23976 return true;
23979 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
23980 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
23982 static bool
23983 dependent_type_p_r (tree type)
23985 tree scope;
23987 /* [temp.dep.type]
23989 A type is dependent if it is:
23991 -- a template parameter. Template template parameters are types
23992 for us (since TYPE_P holds true for them) so we handle
23993 them here. */
23994 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
23995 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
23996 return true;
23997 /* -- a qualified-id with a nested-name-specifier which contains a
23998 class-name that names a dependent type or whose unqualified-id
23999 names a dependent type. */
24000 if (TREE_CODE (type) == TYPENAME_TYPE)
24001 return true;
24003 /* An alias template specialization can be dependent even if the
24004 resulting type is not. */
24005 if (dependent_alias_template_spec_p (type))
24006 return true;
24008 /* -- a cv-qualified type where the cv-unqualified type is
24009 dependent.
24010 No code is necessary for this bullet; the code below handles
24011 cv-qualified types, and we don't want to strip aliases with
24012 TYPE_MAIN_VARIANT because of DR 1558. */
24013 /* -- a compound type constructed from any dependent type. */
24014 if (TYPE_PTRMEM_P (type))
24015 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
24016 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
24017 (type)));
24018 else if (TYPE_PTR_P (type)
24019 || TREE_CODE (type) == REFERENCE_TYPE)
24020 return dependent_type_p (TREE_TYPE (type));
24021 else if (TREE_CODE (type) == FUNCTION_TYPE
24022 || TREE_CODE (type) == METHOD_TYPE)
24024 tree arg_type;
24026 if (dependent_type_p (TREE_TYPE (type)))
24027 return true;
24028 for (arg_type = TYPE_ARG_TYPES (type);
24029 arg_type;
24030 arg_type = TREE_CHAIN (arg_type))
24031 if (dependent_type_p (TREE_VALUE (arg_type)))
24032 return true;
24033 if (cxx_dialect >= cxx17)
24034 /* A value-dependent noexcept-specifier makes the type dependent. */
24035 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
24036 if (tree noex = TREE_PURPOSE (spec))
24037 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
24038 affect overload resolution and treating it as dependent breaks
24039 things. */
24040 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
24041 && value_dependent_expression_p (noex))
24042 return true;
24043 return false;
24045 /* -- an array type constructed from any dependent type or whose
24046 size is specified by a constant expression that is
24047 value-dependent.
24049 We checked for type- and value-dependence of the bounds in
24050 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
24051 if (TREE_CODE (type) == ARRAY_TYPE)
24053 if (TYPE_DOMAIN (type)
24054 && dependent_type_p (TYPE_DOMAIN (type)))
24055 return true;
24056 return dependent_type_p (TREE_TYPE (type));
24059 /* -- a template-id in which either the template name is a template
24060 parameter ... */
24061 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
24062 return true;
24063 /* ... or any of the template arguments is a dependent type or
24064 an expression that is type-dependent or value-dependent. */
24065 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
24066 && (any_dependent_template_arguments_p
24067 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
24068 return true;
24070 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
24071 dependent; if the argument of the `typeof' expression is not
24072 type-dependent, then it should already been have resolved. */
24073 if (TREE_CODE (type) == TYPEOF_TYPE
24074 || TREE_CODE (type) == DECLTYPE_TYPE
24075 || TREE_CODE (type) == UNDERLYING_TYPE)
24076 return true;
24078 /* A template argument pack is dependent if any of its packed
24079 arguments are. */
24080 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
24082 tree args = ARGUMENT_PACK_ARGS (type);
24083 int i, len = TREE_VEC_LENGTH (args);
24084 for (i = 0; i < len; ++i)
24085 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
24086 return true;
24089 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
24090 be template parameters. */
24091 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
24092 return true;
24094 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
24095 return true;
24097 /* The standard does not specifically mention types that are local
24098 to template functions or local classes, but they should be
24099 considered dependent too. For example:
24101 template <int I> void f() {
24102 enum E { a = I };
24103 S<sizeof (E)> s;
24106 The size of `E' cannot be known until the value of `I' has been
24107 determined. Therefore, `E' must be considered dependent. */
24108 scope = TYPE_CONTEXT (type);
24109 if (scope && TYPE_P (scope))
24110 return dependent_type_p (scope);
24111 /* Don't use type_dependent_expression_p here, as it can lead
24112 to infinite recursion trying to determine whether a lambda
24113 nested in a lambda is dependent (c++/47687). */
24114 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
24115 && DECL_LANG_SPECIFIC (scope)
24116 && DECL_TEMPLATE_INFO (scope)
24117 && (any_dependent_template_arguments_p
24118 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
24119 return true;
24121 /* Other types are non-dependent. */
24122 return false;
24125 /* Returns TRUE if TYPE is dependent, in the sense of
24126 [temp.dep.type]. Note that a NULL type is considered dependent. */
24128 bool
24129 dependent_type_p (tree type)
24131 /* If there are no template parameters in scope, then there can't be
24132 any dependent types. */
24133 if (!processing_template_decl)
24135 /* If we are not processing a template, then nobody should be
24136 providing us with a dependent type. */
24137 gcc_assert (type);
24138 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
24139 return false;
24142 /* If the type is NULL, we have not computed a type for the entity
24143 in question; in that case, the type is dependent. */
24144 if (!type)
24145 return true;
24147 /* Erroneous types can be considered non-dependent. */
24148 if (type == error_mark_node)
24149 return false;
24151 /* Getting here with global_type_node means we improperly called this
24152 function on the TREE_TYPE of an IDENTIFIER_NODE. */
24153 gcc_checking_assert (type != global_type_node);
24155 /* If we have not already computed the appropriate value for TYPE,
24156 do so now. */
24157 if (!TYPE_DEPENDENT_P_VALID (type))
24159 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
24160 TYPE_DEPENDENT_P_VALID (type) = 1;
24163 return TYPE_DEPENDENT_P (type);
24166 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
24167 lookup. In other words, a dependent type that is not the current
24168 instantiation. */
24170 bool
24171 dependent_scope_p (tree scope)
24173 return (scope && TYPE_P (scope) && dependent_type_p (scope)
24174 && !currently_open_class (scope));
24177 /* T is a SCOPE_REF. Return whether it represents a non-static member of
24178 an unknown base of 'this' (and is therefore instantiation-dependent). */
24180 static bool
24181 unknown_base_ref_p (tree t)
24183 if (!current_class_ptr)
24184 return false;
24186 tree mem = TREE_OPERAND (t, 1);
24187 if (shared_member_p (mem))
24188 return false;
24190 tree cur = current_nonlambda_class_type ();
24191 if (!any_dependent_bases_p (cur))
24192 return false;
24194 tree ctx = TREE_OPERAND (t, 0);
24195 if (DERIVED_FROM_P (ctx, cur))
24196 return false;
24198 return true;
24201 /* T is a SCOPE_REF; return whether we need to consider it
24202 instantiation-dependent so that we can check access at instantiation
24203 time even though we know which member it resolves to. */
24205 static bool
24206 instantiation_dependent_scope_ref_p (tree t)
24208 if (DECL_P (TREE_OPERAND (t, 1))
24209 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
24210 && !unknown_base_ref_p (t)
24211 && accessible_in_template_p (TREE_OPERAND (t, 0),
24212 TREE_OPERAND (t, 1)))
24213 return false;
24214 else
24215 return true;
24218 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
24219 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
24220 expression. */
24222 /* Note that this predicate is not appropriate for general expressions;
24223 only constant expressions (that satisfy potential_constant_expression)
24224 can be tested for value dependence. */
24226 bool
24227 value_dependent_expression_p (tree expression)
24229 if (!processing_template_decl || expression == NULL_TREE)
24230 return false;
24232 /* A type-dependent expression is also value-dependent. */
24233 if (type_dependent_expression_p (expression))
24234 return true;
24236 switch (TREE_CODE (expression))
24238 case BASELINK:
24239 /* A dependent member function of the current instantiation. */
24240 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
24242 case FUNCTION_DECL:
24243 /* A dependent member function of the current instantiation. */
24244 if (DECL_CLASS_SCOPE_P (expression)
24245 && dependent_type_p (DECL_CONTEXT (expression)))
24246 return true;
24247 break;
24249 case IDENTIFIER_NODE:
24250 /* A name that has not been looked up -- must be dependent. */
24251 return true;
24253 case TEMPLATE_PARM_INDEX:
24254 /* A non-type template parm. */
24255 return true;
24257 case CONST_DECL:
24258 /* A non-type template parm. */
24259 if (DECL_TEMPLATE_PARM_P (expression))
24260 return true;
24261 return value_dependent_expression_p (DECL_INITIAL (expression));
24263 case VAR_DECL:
24264 /* A constant with literal type and is initialized
24265 with an expression that is value-dependent. */
24266 if (DECL_DEPENDENT_INIT_P (expression)
24267 /* FIXME cp_finish_decl doesn't fold reference initializers. */
24268 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE)
24269 return true;
24270 if (DECL_HAS_VALUE_EXPR_P (expression))
24272 tree value_expr = DECL_VALUE_EXPR (expression);
24273 if (value_dependent_expression_p (value_expr))
24274 return true;
24276 return false;
24278 case DYNAMIC_CAST_EXPR:
24279 case STATIC_CAST_EXPR:
24280 case CONST_CAST_EXPR:
24281 case REINTERPRET_CAST_EXPR:
24282 case CAST_EXPR:
24283 case IMPLICIT_CONV_EXPR:
24284 /* These expressions are value-dependent if the type to which
24285 the cast occurs is dependent or the expression being casted
24286 is value-dependent. */
24288 tree type = TREE_TYPE (expression);
24290 if (dependent_type_p (type))
24291 return true;
24293 /* A functional cast has a list of operands. */
24294 expression = TREE_OPERAND (expression, 0);
24295 if (!expression)
24297 /* If there are no operands, it must be an expression such
24298 as "int()". This should not happen for aggregate types
24299 because it would form non-constant expressions. */
24300 gcc_assert (cxx_dialect >= cxx11
24301 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
24303 return false;
24306 if (TREE_CODE (expression) == TREE_LIST)
24307 return any_value_dependent_elements_p (expression);
24309 return value_dependent_expression_p (expression);
24312 case SIZEOF_EXPR:
24313 if (SIZEOF_EXPR_TYPE_P (expression))
24314 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
24315 /* FALLTHRU */
24316 case ALIGNOF_EXPR:
24317 case TYPEID_EXPR:
24318 /* A `sizeof' expression is value-dependent if the operand is
24319 type-dependent or is a pack expansion. */
24320 expression = TREE_OPERAND (expression, 0);
24321 if (PACK_EXPANSION_P (expression))
24322 return true;
24323 else if (TYPE_P (expression))
24324 return dependent_type_p (expression);
24325 return instantiation_dependent_uneval_expression_p (expression);
24327 case AT_ENCODE_EXPR:
24328 /* An 'encode' expression is value-dependent if the operand is
24329 type-dependent. */
24330 expression = TREE_OPERAND (expression, 0);
24331 return dependent_type_p (expression);
24333 case NOEXCEPT_EXPR:
24334 expression = TREE_OPERAND (expression, 0);
24335 return instantiation_dependent_uneval_expression_p (expression);
24337 case SCOPE_REF:
24338 /* All instantiation-dependent expressions should also be considered
24339 value-dependent. */
24340 return instantiation_dependent_scope_ref_p (expression);
24342 case COMPONENT_REF:
24343 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
24344 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
24346 case NONTYPE_ARGUMENT_PACK:
24347 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
24348 is value-dependent. */
24350 tree values = ARGUMENT_PACK_ARGS (expression);
24351 int i, len = TREE_VEC_LENGTH (values);
24353 for (i = 0; i < len; ++i)
24354 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
24355 return true;
24357 return false;
24360 case TRAIT_EXPR:
24362 tree type2 = TRAIT_EXPR_TYPE2 (expression);
24364 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
24365 return true;
24367 if (!type2)
24368 return false;
24370 if (TREE_CODE (type2) != TREE_LIST)
24371 return dependent_type_p (type2);
24373 for (; type2; type2 = TREE_CHAIN (type2))
24374 if (dependent_type_p (TREE_VALUE (type2)))
24375 return true;
24377 return false;
24380 case MODOP_EXPR:
24381 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24382 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
24384 case ARRAY_REF:
24385 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24386 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
24388 case ADDR_EXPR:
24390 tree op = TREE_OPERAND (expression, 0);
24391 return (value_dependent_expression_p (op)
24392 || has_value_dependent_address (op));
24395 case REQUIRES_EXPR:
24396 /* Treat all requires-expressions as value-dependent so
24397 we don't try to fold them. */
24398 return true;
24400 case TYPE_REQ:
24401 return dependent_type_p (TREE_OPERAND (expression, 0));
24403 case CALL_EXPR:
24405 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
24406 return true;
24407 tree fn = get_callee_fndecl (expression);
24408 int i, nargs;
24409 nargs = call_expr_nargs (expression);
24410 for (i = 0; i < nargs; ++i)
24412 tree op = CALL_EXPR_ARG (expression, i);
24413 /* In a call to a constexpr member function, look through the
24414 implicit ADDR_EXPR on the object argument so that it doesn't
24415 cause the call to be considered value-dependent. We also
24416 look through it in potential_constant_expression. */
24417 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
24418 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
24419 && TREE_CODE (op) == ADDR_EXPR)
24420 op = TREE_OPERAND (op, 0);
24421 if (value_dependent_expression_p (op))
24422 return true;
24424 return false;
24427 case TEMPLATE_ID_EXPR:
24428 return variable_concept_p (TREE_OPERAND (expression, 0));
24430 case CONSTRUCTOR:
24432 unsigned ix;
24433 tree val;
24434 if (dependent_type_p (TREE_TYPE (expression)))
24435 return true;
24436 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
24437 if (value_dependent_expression_p (val))
24438 return true;
24439 return false;
24442 case STMT_EXPR:
24443 /* Treat a GNU statement expression as dependent to avoid crashing
24444 under instantiate_non_dependent_expr; it can't be constant. */
24445 return true;
24447 default:
24448 /* A constant expression is value-dependent if any subexpression is
24449 value-dependent. */
24450 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
24452 case tcc_reference:
24453 case tcc_unary:
24454 case tcc_comparison:
24455 case tcc_binary:
24456 case tcc_expression:
24457 case tcc_vl_exp:
24459 int i, len = cp_tree_operand_length (expression);
24461 for (i = 0; i < len; i++)
24463 tree t = TREE_OPERAND (expression, i);
24465 /* In some cases, some of the operands may be missing.
24466 (For example, in the case of PREDECREMENT_EXPR, the
24467 amount to increment by may be missing.) That doesn't
24468 make the expression dependent. */
24469 if (t && value_dependent_expression_p (t))
24470 return true;
24473 break;
24474 default:
24475 break;
24477 break;
24480 /* The expression is not value-dependent. */
24481 return false;
24484 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
24485 [temp.dep.expr]. Note that an expression with no type is
24486 considered dependent. Other parts of the compiler arrange for an
24487 expression with type-dependent subexpressions to have no type, so
24488 this function doesn't have to be fully recursive. */
24490 bool
24491 type_dependent_expression_p (tree expression)
24493 if (!processing_template_decl)
24494 return false;
24496 if (expression == NULL_TREE || expression == error_mark_node)
24497 return false;
24499 STRIP_ANY_LOCATION_WRAPPER (expression);
24501 /* An unresolved name is always dependent. */
24502 if (identifier_p (expression)
24503 || TREE_CODE (expression) == USING_DECL
24504 || TREE_CODE (expression) == WILDCARD_DECL)
24505 return true;
24507 /* A fold expression is type-dependent. */
24508 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
24509 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
24510 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
24511 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
24512 return true;
24514 /* Some expression forms are never type-dependent. */
24515 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
24516 || TREE_CODE (expression) == SIZEOF_EXPR
24517 || TREE_CODE (expression) == ALIGNOF_EXPR
24518 || TREE_CODE (expression) == AT_ENCODE_EXPR
24519 || TREE_CODE (expression) == NOEXCEPT_EXPR
24520 || TREE_CODE (expression) == TRAIT_EXPR
24521 || TREE_CODE (expression) == TYPEID_EXPR
24522 || TREE_CODE (expression) == DELETE_EXPR
24523 || TREE_CODE (expression) == VEC_DELETE_EXPR
24524 || TREE_CODE (expression) == THROW_EXPR
24525 || TREE_CODE (expression) == REQUIRES_EXPR)
24526 return false;
24528 /* The types of these expressions depends only on the type to which
24529 the cast occurs. */
24530 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
24531 || TREE_CODE (expression) == STATIC_CAST_EXPR
24532 || TREE_CODE (expression) == CONST_CAST_EXPR
24533 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
24534 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
24535 || TREE_CODE (expression) == CAST_EXPR)
24536 return dependent_type_p (TREE_TYPE (expression));
24538 /* The types of these expressions depends only on the type created
24539 by the expression. */
24540 if (TREE_CODE (expression) == NEW_EXPR
24541 || TREE_CODE (expression) == VEC_NEW_EXPR)
24543 /* For NEW_EXPR tree nodes created inside a template, either
24544 the object type itself or a TREE_LIST may appear as the
24545 operand 1. */
24546 tree type = TREE_OPERAND (expression, 1);
24547 if (TREE_CODE (type) == TREE_LIST)
24548 /* This is an array type. We need to check array dimensions
24549 as well. */
24550 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
24551 || value_dependent_expression_p
24552 (TREE_OPERAND (TREE_VALUE (type), 1));
24553 else
24554 return dependent_type_p (type);
24557 if (TREE_CODE (expression) == SCOPE_REF)
24559 tree scope = TREE_OPERAND (expression, 0);
24560 tree name = TREE_OPERAND (expression, 1);
24562 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
24563 contains an identifier associated by name lookup with one or more
24564 declarations declared with a dependent type, or...a
24565 nested-name-specifier or qualified-id that names a member of an
24566 unknown specialization. */
24567 return (type_dependent_expression_p (name)
24568 || dependent_scope_p (scope));
24571 if (TREE_CODE (expression) == TEMPLATE_DECL
24572 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
24573 return uses_outer_template_parms (expression);
24575 if (TREE_CODE (expression) == STMT_EXPR)
24576 expression = stmt_expr_value_expr (expression);
24578 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
24580 tree elt;
24581 unsigned i;
24583 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
24585 if (type_dependent_expression_p (elt))
24586 return true;
24588 return false;
24591 /* A static data member of the current instantiation with incomplete
24592 array type is type-dependent, as the definition and specializations
24593 can have different bounds. */
24594 if (VAR_P (expression)
24595 && DECL_CLASS_SCOPE_P (expression)
24596 && dependent_type_p (DECL_CONTEXT (expression))
24597 && VAR_HAD_UNKNOWN_BOUND (expression))
24598 return true;
24600 /* An array of unknown bound depending on a variadic parameter, eg:
24602 template<typename... Args>
24603 void foo (Args... args)
24605 int arr[] = { args... };
24608 template<int... vals>
24609 void bar ()
24611 int arr[] = { vals... };
24614 If the array has no length and has an initializer, it must be that
24615 we couldn't determine its length in cp_complete_array_type because
24616 it is dependent. */
24617 if (VAR_P (expression)
24618 && TREE_TYPE (expression) != NULL_TREE
24619 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
24620 && !TYPE_DOMAIN (TREE_TYPE (expression))
24621 && DECL_INITIAL (expression))
24622 return true;
24624 /* A function or variable template-id is type-dependent if it has any
24625 dependent template arguments. */
24626 if (VAR_OR_FUNCTION_DECL_P (expression)
24627 && DECL_LANG_SPECIFIC (expression)
24628 && DECL_TEMPLATE_INFO (expression))
24630 /* Consider the innermost template arguments, since those are the ones
24631 that come from the template-id; the template arguments for the
24632 enclosing class do not make it type-dependent unless they are used in
24633 the type of the decl. */
24634 if (PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression))
24635 && (any_dependent_template_arguments_p
24636 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
24637 return true;
24640 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
24641 type-dependent. Checking this is important for functions with auto return
24642 type, which looks like a dependent type. */
24643 if (TREE_CODE (expression) == FUNCTION_DECL
24644 && !(DECL_CLASS_SCOPE_P (expression)
24645 && dependent_type_p (DECL_CONTEXT (expression)))
24646 && !(DECL_LANG_SPECIFIC (expression)
24647 && DECL_FRIEND_P (expression)
24648 && (!DECL_FRIEND_CONTEXT (expression)
24649 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
24650 && !DECL_LOCAL_FUNCTION_P (expression))
24652 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
24653 || undeduced_auto_decl (expression));
24654 return false;
24657 /* Always dependent, on the number of arguments if nothing else. */
24658 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
24659 return true;
24661 if (TREE_TYPE (expression) == unknown_type_node)
24663 if (TREE_CODE (expression) == ADDR_EXPR)
24664 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
24665 if (TREE_CODE (expression) == COMPONENT_REF
24666 || TREE_CODE (expression) == OFFSET_REF)
24668 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
24669 return true;
24670 expression = TREE_OPERAND (expression, 1);
24671 if (identifier_p (expression))
24672 return false;
24674 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
24675 if (TREE_CODE (expression) == SCOPE_REF)
24676 return false;
24678 if (BASELINK_P (expression))
24680 if (BASELINK_OPTYPE (expression)
24681 && dependent_type_p (BASELINK_OPTYPE (expression)))
24682 return true;
24683 expression = BASELINK_FUNCTIONS (expression);
24686 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
24688 if (any_dependent_template_arguments_p
24689 (TREE_OPERAND (expression, 1)))
24690 return true;
24691 expression = TREE_OPERAND (expression, 0);
24692 if (identifier_p (expression))
24693 return true;
24696 gcc_assert (TREE_CODE (expression) == OVERLOAD
24697 || TREE_CODE (expression) == FUNCTION_DECL);
24699 for (lkp_iterator iter (expression); iter; ++iter)
24700 if (type_dependent_expression_p (*iter))
24701 return true;
24703 return false;
24706 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
24708 /* Dependent type attributes might not have made it from the decl to
24709 the type yet. */
24710 if (DECL_P (expression)
24711 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
24712 return true;
24714 return (dependent_type_p (TREE_TYPE (expression)));
24717 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
24718 type-dependent if the expression refers to a member of the current
24719 instantiation and the type of the referenced member is dependent, or the
24720 class member access expression refers to a member of an unknown
24721 specialization.
24723 This function returns true if the OBJECT in such a class member access
24724 expression is of an unknown specialization. */
24726 bool
24727 type_dependent_object_expression_p (tree object)
24729 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
24730 dependent. */
24731 if (TREE_CODE (object) == IDENTIFIER_NODE)
24732 return true;
24733 tree scope = TREE_TYPE (object);
24734 return (!scope || dependent_scope_p (scope));
24737 /* walk_tree callback function for instantiation_dependent_expression_p,
24738 below. Returns non-zero if a dependent subexpression is found. */
24740 static tree
24741 instantiation_dependent_r (tree *tp, int *walk_subtrees,
24742 void * /*data*/)
24744 if (TYPE_P (*tp))
24746 /* We don't have to worry about decltype currently because decltype
24747 of an instantiation-dependent expr is a dependent type. This
24748 might change depending on the resolution of DR 1172. */
24749 *walk_subtrees = false;
24750 return NULL_TREE;
24752 enum tree_code code = TREE_CODE (*tp);
24753 switch (code)
24755 /* Don't treat an argument list as dependent just because it has no
24756 TREE_TYPE. */
24757 case TREE_LIST:
24758 case TREE_VEC:
24759 return NULL_TREE;
24761 case TEMPLATE_PARM_INDEX:
24762 return *tp;
24764 /* Handle expressions with type operands. */
24765 case SIZEOF_EXPR:
24766 case ALIGNOF_EXPR:
24767 case TYPEID_EXPR:
24768 case AT_ENCODE_EXPR:
24770 tree op = TREE_OPERAND (*tp, 0);
24771 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
24772 op = TREE_TYPE (op);
24773 if (TYPE_P (op))
24775 if (dependent_type_p (op))
24776 return *tp;
24777 else
24779 *walk_subtrees = false;
24780 return NULL_TREE;
24783 break;
24786 case COMPONENT_REF:
24787 if (identifier_p (TREE_OPERAND (*tp, 1)))
24788 /* In a template, finish_class_member_access_expr creates a
24789 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
24790 type-dependent, so that we can check access control at
24791 instantiation time (PR 42277). See also Core issue 1273. */
24792 return *tp;
24793 break;
24795 case SCOPE_REF:
24796 if (instantiation_dependent_scope_ref_p (*tp))
24797 return *tp;
24798 else
24799 break;
24801 /* Treat statement-expressions as dependent. */
24802 case BIND_EXPR:
24803 return *tp;
24805 /* Treat requires-expressions as dependent. */
24806 case REQUIRES_EXPR:
24807 return *tp;
24809 case CALL_EXPR:
24810 /* Treat calls to function concepts as dependent. */
24811 if (function_concept_check_p (*tp))
24812 return *tp;
24813 break;
24815 case TEMPLATE_ID_EXPR:
24816 /* And variable concepts. */
24817 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
24818 return *tp;
24819 break;
24821 default:
24822 break;
24825 if (type_dependent_expression_p (*tp))
24826 return *tp;
24827 else
24828 return NULL_TREE;
24831 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
24832 sense defined by the ABI:
24834 "An expression is instantiation-dependent if it is type-dependent
24835 or value-dependent, or it has a subexpression that is type-dependent
24836 or value-dependent."
24838 Except don't actually check value-dependence for unevaluated expressions,
24839 because in sizeof(i) we don't care about the value of i. Checking
24840 type-dependence will in turn check value-dependence of array bounds/template
24841 arguments as needed. */
24843 bool
24844 instantiation_dependent_uneval_expression_p (tree expression)
24846 tree result;
24848 if (!processing_template_decl)
24849 return false;
24851 if (expression == error_mark_node)
24852 return false;
24854 result = cp_walk_tree_without_duplicates (&expression,
24855 instantiation_dependent_r, NULL);
24856 return result != NULL_TREE;
24859 /* As above, but also check value-dependence of the expression as a whole. */
24861 bool
24862 instantiation_dependent_expression_p (tree expression)
24864 return (instantiation_dependent_uneval_expression_p (expression)
24865 || value_dependent_expression_p (expression));
24868 /* Like type_dependent_expression_p, but it also works while not processing
24869 a template definition, i.e. during substitution or mangling. */
24871 bool
24872 type_dependent_expression_p_push (tree expr)
24874 bool b;
24875 ++processing_template_decl;
24876 b = type_dependent_expression_p (expr);
24877 --processing_template_decl;
24878 return b;
24881 /* Returns TRUE if ARGS contains a type-dependent expression. */
24883 bool
24884 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
24886 unsigned int i;
24887 tree arg;
24889 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
24891 if (type_dependent_expression_p (arg))
24892 return true;
24894 return false;
24897 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24898 expressions) contains any type-dependent expressions. */
24900 bool
24901 any_type_dependent_elements_p (const_tree list)
24903 for (; list; list = TREE_CHAIN (list))
24904 if (type_dependent_expression_p (TREE_VALUE (list)))
24905 return true;
24907 return false;
24910 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24911 expressions) contains any value-dependent expressions. */
24913 bool
24914 any_value_dependent_elements_p (const_tree list)
24916 for (; list; list = TREE_CHAIN (list))
24917 if (value_dependent_expression_p (TREE_VALUE (list)))
24918 return true;
24920 return false;
24923 /* Returns TRUE if the ARG (a template argument) is dependent. */
24925 bool
24926 dependent_template_arg_p (tree arg)
24928 if (!processing_template_decl)
24929 return false;
24931 /* Assume a template argument that was wrongly written by the user
24932 is dependent. This is consistent with what
24933 any_dependent_template_arguments_p [that calls this function]
24934 does. */
24935 if (!arg || arg == error_mark_node)
24936 return true;
24938 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
24939 arg = argument_pack_select_arg (arg);
24941 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
24942 return true;
24943 if (TREE_CODE (arg) == TEMPLATE_DECL)
24945 if (DECL_TEMPLATE_PARM_P (arg))
24946 return true;
24947 /* A member template of a dependent class is not necessarily
24948 type-dependent, but it is a dependent template argument because it
24949 will be a member of an unknown specialization to that template. */
24950 tree scope = CP_DECL_CONTEXT (arg);
24951 return TYPE_P (scope) && dependent_type_p (scope);
24953 else if (ARGUMENT_PACK_P (arg))
24955 tree args = ARGUMENT_PACK_ARGS (arg);
24956 int i, len = TREE_VEC_LENGTH (args);
24957 for (i = 0; i < len; ++i)
24959 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
24960 return true;
24963 return false;
24965 else if (TYPE_P (arg))
24966 return dependent_type_p (arg);
24967 else
24968 return (type_dependent_expression_p (arg)
24969 || value_dependent_expression_p (arg));
24972 /* Returns true if ARGS (a collection of template arguments) contains
24973 any types that require structural equality testing. */
24975 bool
24976 any_template_arguments_need_structural_equality_p (tree args)
24978 int i;
24979 int j;
24981 if (!args)
24982 return false;
24983 if (args == error_mark_node)
24984 return true;
24986 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24988 tree level = TMPL_ARGS_LEVEL (args, i + 1);
24989 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24991 tree arg = TREE_VEC_ELT (level, j);
24992 tree packed_args = NULL_TREE;
24993 int k, len = 1;
24995 if (ARGUMENT_PACK_P (arg))
24997 /* Look inside the argument pack. */
24998 packed_args = ARGUMENT_PACK_ARGS (arg);
24999 len = TREE_VEC_LENGTH (packed_args);
25002 for (k = 0; k < len; ++k)
25004 if (packed_args)
25005 arg = TREE_VEC_ELT (packed_args, k);
25007 if (error_operand_p (arg))
25008 return true;
25009 else if (TREE_CODE (arg) == TEMPLATE_DECL)
25010 continue;
25011 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
25012 return true;
25013 else if (!TYPE_P (arg) && TREE_TYPE (arg)
25014 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
25015 return true;
25020 return false;
25023 /* Returns true if ARGS (a collection of template arguments) contains
25024 any dependent arguments. */
25026 bool
25027 any_dependent_template_arguments_p (const_tree args)
25029 int i;
25030 int j;
25032 if (!args)
25033 return false;
25034 if (args == error_mark_node)
25035 return true;
25037 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
25039 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
25040 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
25041 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
25042 return true;
25045 return false;
25048 /* Returns TRUE if the template TMPL is type-dependent. */
25050 bool
25051 dependent_template_p (tree tmpl)
25053 if (TREE_CODE (tmpl) == OVERLOAD)
25055 for (lkp_iterator iter (tmpl); iter; ++iter)
25056 if (dependent_template_p (*iter))
25057 return true;
25058 return false;
25061 /* Template template parameters are dependent. */
25062 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
25063 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
25064 return true;
25065 /* So are names that have not been looked up. */
25066 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
25067 return true;
25068 return false;
25071 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
25073 bool
25074 dependent_template_id_p (tree tmpl, tree args)
25076 return (dependent_template_p (tmpl)
25077 || any_dependent_template_arguments_p (args));
25080 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
25081 are dependent. */
25083 bool
25084 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
25086 int i;
25088 if (!processing_template_decl)
25089 return false;
25091 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
25093 tree decl = TREE_VEC_ELT (declv, i);
25094 tree init = TREE_VEC_ELT (initv, i);
25095 tree cond = TREE_VEC_ELT (condv, i);
25096 tree incr = TREE_VEC_ELT (incrv, i);
25098 if (type_dependent_expression_p (decl)
25099 || TREE_CODE (decl) == SCOPE_REF)
25100 return true;
25102 if (init && type_dependent_expression_p (init))
25103 return true;
25105 if (type_dependent_expression_p (cond))
25106 return true;
25108 if (COMPARISON_CLASS_P (cond)
25109 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
25110 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
25111 return true;
25113 if (TREE_CODE (incr) == MODOP_EXPR)
25115 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
25116 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
25117 return true;
25119 else if (type_dependent_expression_p (incr))
25120 return true;
25121 else if (TREE_CODE (incr) == MODIFY_EXPR)
25123 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
25124 return true;
25125 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
25127 tree t = TREE_OPERAND (incr, 1);
25128 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
25129 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
25130 return true;
25135 return false;
25138 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
25139 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
25140 no such TYPE can be found. Note that this function peers inside
25141 uninstantiated templates and therefore should be used only in
25142 extremely limited situations. ONLY_CURRENT_P restricts this
25143 peering to the currently open classes hierarchy (which is required
25144 when comparing types). */
25146 tree
25147 resolve_typename_type (tree type, bool only_current_p)
25149 tree scope;
25150 tree name;
25151 tree decl;
25152 int quals;
25153 tree pushed_scope;
25154 tree result;
25156 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
25158 scope = TYPE_CONTEXT (type);
25159 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
25160 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
25161 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
25162 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
25163 identifier of the TYPENAME_TYPE anymore.
25164 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
25165 TYPENAME_TYPE instead, we avoid messing up with a possible
25166 typedef variant case. */
25167 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
25169 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
25170 it first before we can figure out what NAME refers to. */
25171 if (TREE_CODE (scope) == TYPENAME_TYPE)
25173 if (TYPENAME_IS_RESOLVING_P (scope))
25174 /* Given a class template A with a dependent base with nested type C,
25175 typedef typename A::C::C C will land us here, as trying to resolve
25176 the initial A::C leads to the local C typedef, which leads back to
25177 A::C::C. So we break the recursion now. */
25178 return type;
25179 else
25180 scope = resolve_typename_type (scope, only_current_p);
25182 /* If we don't know what SCOPE refers to, then we cannot resolve the
25183 TYPENAME_TYPE. */
25184 if (!CLASS_TYPE_P (scope))
25185 return type;
25186 /* If this is a typedef, we don't want to look inside (c++/11987). */
25187 if (typedef_variant_p (type))
25188 return type;
25189 /* If SCOPE isn't the template itself, it will not have a valid
25190 TYPE_FIELDS list. */
25191 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
25192 /* scope is either the template itself or a compatible instantiation
25193 like X<T>, so look up the name in the original template. */
25194 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
25195 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
25196 gcc_checking_assert (uses_template_parms (scope));
25197 /* If scope has no fields, it can't be a current instantiation. Check this
25198 before currently_open_class to avoid infinite recursion (71515). */
25199 if (!TYPE_FIELDS (scope))
25200 return type;
25201 /* If the SCOPE is not the current instantiation, there's no reason
25202 to look inside it. */
25203 if (only_current_p && !currently_open_class (scope))
25204 return type;
25205 /* Enter the SCOPE so that name lookup will be resolved as if we
25206 were in the class definition. In particular, SCOPE will no
25207 longer be considered a dependent type. */
25208 pushed_scope = push_scope (scope);
25209 /* Look up the declaration. */
25210 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
25211 tf_warning_or_error);
25213 result = NULL_TREE;
25215 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
25216 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
25217 tree fullname = TYPENAME_TYPE_FULLNAME (type);
25218 if (!decl)
25219 /*nop*/;
25220 else if (identifier_p (fullname)
25221 && TREE_CODE (decl) == TYPE_DECL)
25223 result = TREE_TYPE (decl);
25224 if (result == error_mark_node)
25225 result = NULL_TREE;
25227 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
25228 && DECL_CLASS_TEMPLATE_P (decl))
25230 /* Obtain the template and the arguments. */
25231 tree tmpl = TREE_OPERAND (fullname, 0);
25232 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
25234 /* We get here with a plain identifier because a previous tentative
25235 parse of the nested-name-specifier as part of a ptr-operator saw
25236 ::template X<A>. The use of ::template is necessary in a
25237 ptr-operator, but wrong in a declarator-id.
25239 [temp.names]: In a qualified-id of a declarator-id, the keyword
25240 template shall not appear at the top level. */
25241 pedwarn (EXPR_LOC_OR_LOC (fullname, input_location), OPT_Wpedantic,
25242 "keyword %<template%> not allowed in declarator-id");
25243 tmpl = decl;
25245 tree args = TREE_OPERAND (fullname, 1);
25246 /* Instantiate the template. */
25247 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
25248 /*entering_scope=*/true,
25249 tf_error | tf_user);
25250 if (result == error_mark_node)
25251 result = NULL_TREE;
25254 /* Leave the SCOPE. */
25255 if (pushed_scope)
25256 pop_scope (pushed_scope);
25258 /* If we failed to resolve it, return the original typename. */
25259 if (!result)
25260 return type;
25262 /* If lookup found a typename type, resolve that too. */
25263 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
25265 /* Ill-formed programs can cause infinite recursion here, so we
25266 must catch that. */
25267 TYPENAME_IS_RESOLVING_P (result) = 1;
25268 result = resolve_typename_type (result, only_current_p);
25269 TYPENAME_IS_RESOLVING_P (result) = 0;
25272 /* Qualify the resulting type. */
25273 quals = cp_type_quals (type);
25274 if (quals)
25275 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
25277 return result;
25280 /* EXPR is an expression which is not type-dependent. Return a proxy
25281 for EXPR that can be used to compute the types of larger
25282 expressions containing EXPR. */
25284 tree
25285 build_non_dependent_expr (tree expr)
25287 tree orig_expr = expr;
25288 tree inner_expr;
25290 /* When checking, try to get a constant value for all non-dependent
25291 expressions in order to expose bugs in *_dependent_expression_p
25292 and constexpr. This can affect code generation, see PR70704, so
25293 only do this for -fchecking=2. */
25294 if (flag_checking > 1
25295 && cxx_dialect >= cxx11
25296 /* Don't do this during nsdmi parsing as it can lead to
25297 unexpected recursive instantiations. */
25298 && !parsing_nsdmi ()
25299 /* Don't do this during concept expansion either and for
25300 the same reason. */
25301 && !expanding_concept ())
25302 fold_non_dependent_expr (expr);
25304 STRIP_ANY_LOCATION_WRAPPER (expr);
25306 /* Preserve OVERLOADs; the functions must be available to resolve
25307 types. */
25308 inner_expr = expr;
25309 if (TREE_CODE (inner_expr) == STMT_EXPR)
25310 inner_expr = stmt_expr_value_expr (inner_expr);
25311 if (TREE_CODE (inner_expr) == ADDR_EXPR)
25312 inner_expr = TREE_OPERAND (inner_expr, 0);
25313 if (TREE_CODE (inner_expr) == COMPONENT_REF)
25314 inner_expr = TREE_OPERAND (inner_expr, 1);
25315 if (is_overloaded_fn (inner_expr)
25316 || TREE_CODE (inner_expr) == OFFSET_REF)
25317 return orig_expr;
25318 /* There is no need to return a proxy for a variable. */
25319 if (VAR_P (expr))
25320 return orig_expr;
25321 /* Preserve string constants; conversions from string constants to
25322 "char *" are allowed, even though normally a "const char *"
25323 cannot be used to initialize a "char *". */
25324 if (TREE_CODE (expr) == STRING_CST)
25325 return orig_expr;
25326 /* Preserve void and arithmetic constants, as an optimization -- there is no
25327 reason to create a new node. */
25328 if (TREE_CODE (expr) == VOID_CST
25329 || TREE_CODE (expr) == INTEGER_CST
25330 || TREE_CODE (expr) == REAL_CST)
25331 return orig_expr;
25332 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
25333 There is at least one place where we want to know that a
25334 particular expression is a throw-expression: when checking a ?:
25335 expression, there are special rules if the second or third
25336 argument is a throw-expression. */
25337 if (TREE_CODE (expr) == THROW_EXPR)
25338 return orig_expr;
25340 /* Don't wrap an initializer list, we need to be able to look inside. */
25341 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
25342 return orig_expr;
25344 /* Don't wrap a dummy object, we need to be able to test for it. */
25345 if (is_dummy_object (expr))
25346 return orig_expr;
25348 if (TREE_CODE (expr) == COND_EXPR)
25349 return build3 (COND_EXPR,
25350 TREE_TYPE (expr),
25351 TREE_OPERAND (expr, 0),
25352 (TREE_OPERAND (expr, 1)
25353 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
25354 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
25355 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
25356 if (TREE_CODE (expr) == COMPOUND_EXPR
25357 && !COMPOUND_EXPR_OVERLOADED (expr))
25358 return build2 (COMPOUND_EXPR,
25359 TREE_TYPE (expr),
25360 TREE_OPERAND (expr, 0),
25361 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
25363 /* If the type is unknown, it can't really be non-dependent */
25364 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
25366 /* Otherwise, build a NON_DEPENDENT_EXPR. */
25367 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
25370 /* ARGS is a vector of expressions as arguments to a function call.
25371 Replace the arguments with equivalent non-dependent expressions.
25372 This modifies ARGS in place. */
25374 void
25375 make_args_non_dependent (vec<tree, va_gc> *args)
25377 unsigned int ix;
25378 tree arg;
25380 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
25382 tree newarg = build_non_dependent_expr (arg);
25383 if (newarg != arg)
25384 (*args)[ix] = newarg;
25388 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
25389 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
25390 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
25392 static tree
25393 make_auto_1 (tree name, bool set_canonical)
25395 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
25396 TYPE_NAME (au) = build_decl (input_location,
25397 TYPE_DECL, name, au);
25398 TYPE_STUB_DECL (au) = TYPE_NAME (au);
25399 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
25400 (0, processing_template_decl + 1, processing_template_decl + 1,
25401 TYPE_NAME (au), NULL_TREE);
25402 if (set_canonical)
25403 TYPE_CANONICAL (au) = canonical_type_parameter (au);
25404 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
25405 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
25407 return au;
25410 tree
25411 make_decltype_auto (void)
25413 return make_auto_1 (decltype_auto_identifier, true);
25416 tree
25417 make_auto (void)
25419 return make_auto_1 (auto_identifier, true);
25422 /* Return a C++17 deduction placeholder for class template TMPL. */
25424 tree
25425 make_template_placeholder (tree tmpl)
25427 tree t = make_auto_1 (DECL_NAME (tmpl), true);
25428 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
25429 return t;
25432 /* True iff T is a C++17 class template deduction placeholder. */
25434 bool
25435 template_placeholder_p (tree t)
25437 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
25440 /* Make a "constrained auto" type-specifier. This is an
25441 auto type with constraints that must be associated after
25442 deduction. The constraint is formed from the given
25443 CONC and its optional sequence of arguments, which are
25444 non-null if written as partial-concept-id. */
25446 tree
25447 make_constrained_auto (tree con, tree args)
25449 tree type = make_auto_1 (auto_identifier, false);
25451 /* Build the constraint. */
25452 tree tmpl = DECL_TI_TEMPLATE (con);
25453 tree expr = VAR_P (con) ? tmpl : ovl_make (tmpl);
25454 expr = build_concept_check (expr, type, args);
25456 tree constr = normalize_expression (expr);
25457 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
25459 /* Our canonical type depends on the constraint. */
25460 TYPE_CANONICAL (type) = canonical_type_parameter (type);
25462 /* Attach the constraint to the type declaration. */
25463 tree decl = TYPE_NAME (type);
25464 return decl;
25467 /* Given type ARG, return std::initializer_list<ARG>. */
25469 static tree
25470 listify (tree arg)
25472 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
25474 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
25476 gcc_rich_location richloc (input_location);
25477 maybe_add_include_fixit (&richloc, "<initializer_list>");
25478 error_at (&richloc,
25479 "deducing from brace-enclosed initializer list"
25480 " requires %<#include <initializer_list>%>");
25482 return error_mark_node;
25484 tree argvec = make_tree_vec (1);
25485 TREE_VEC_ELT (argvec, 0) = arg;
25487 return lookup_template_class (std_init_list, argvec, NULL_TREE,
25488 NULL_TREE, 0, tf_warning_or_error);
25491 /* Replace auto in TYPE with std::initializer_list<auto>. */
25493 static tree
25494 listify_autos (tree type, tree auto_node)
25496 tree init_auto = listify (auto_node);
25497 tree argvec = make_tree_vec (1);
25498 TREE_VEC_ELT (argvec, 0) = init_auto;
25499 if (processing_template_decl)
25500 argvec = add_to_template_args (current_template_args (), argvec);
25501 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
25504 /* Hash traits for hashing possibly constrained 'auto'
25505 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
25507 struct auto_hash : default_hash_traits<tree>
25509 static inline hashval_t hash (tree);
25510 static inline bool equal (tree, tree);
25513 /* Hash the 'auto' T. */
25515 inline hashval_t
25516 auto_hash::hash (tree t)
25518 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
25519 /* Matching constrained-type-specifiers denote the same template
25520 parameter, so hash the constraint. */
25521 return hash_placeholder_constraint (c);
25522 else
25523 /* But unconstrained autos are all separate, so just hash the pointer. */
25524 return iterative_hash_object (t, 0);
25527 /* Compare two 'auto's. */
25529 inline bool
25530 auto_hash::equal (tree t1, tree t2)
25532 if (t1 == t2)
25533 return true;
25535 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
25536 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
25538 /* Two unconstrained autos are distinct. */
25539 if (!c1 || !c2)
25540 return false;
25542 return equivalent_placeholder_constraints (c1, c2);
25545 /* for_each_template_parm callback for extract_autos: if t is a (possibly
25546 constrained) auto, add it to the vector. */
25548 static int
25549 extract_autos_r (tree t, void *data)
25551 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
25552 if (is_auto (t))
25554 /* All the autos were built with index 0; fix that up now. */
25555 tree *p = hash.find_slot (t, INSERT);
25556 unsigned idx;
25557 if (*p)
25558 /* If this is a repeated constrained-type-specifier, use the index we
25559 chose before. */
25560 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
25561 else
25563 /* Otherwise this is new, so use the current count. */
25564 *p = t;
25565 idx = hash.elements () - 1;
25567 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
25570 /* Always keep walking. */
25571 return 0;
25574 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
25575 says they can appear anywhere in the type. */
25577 static tree
25578 extract_autos (tree type)
25580 hash_set<tree> visited;
25581 hash_table<auto_hash> hash (2);
25583 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
25585 tree tree_vec = make_tree_vec (hash.elements());
25586 for (hash_table<auto_hash>::iterator iter = hash.begin();
25587 iter != hash.end(); ++iter)
25589 tree elt = *iter;
25590 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
25591 TREE_VEC_ELT (tree_vec, i)
25592 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
25595 return tree_vec;
25598 /* The stem for deduction guide names. */
25599 const char *const dguide_base = "__dguide_";
25601 /* Return the name for a deduction guide for class template TMPL. */
25603 tree
25604 dguide_name (tree tmpl)
25606 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
25607 tree tname = TYPE_IDENTIFIER (type);
25608 char *buf = (char *) alloca (1 + strlen (dguide_base)
25609 + IDENTIFIER_LENGTH (tname));
25610 memcpy (buf, dguide_base, strlen (dguide_base));
25611 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
25612 IDENTIFIER_LENGTH (tname) + 1);
25613 tree dname = get_identifier (buf);
25614 TREE_TYPE (dname) = type;
25615 return dname;
25618 /* True if NAME is the name of a deduction guide. */
25620 bool
25621 dguide_name_p (tree name)
25623 return (TREE_CODE (name) == IDENTIFIER_NODE
25624 && TREE_TYPE (name)
25625 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
25626 strlen (dguide_base)));
25629 /* True if FN is a deduction guide. */
25631 bool
25632 deduction_guide_p (const_tree fn)
25634 if (DECL_P (fn))
25635 if (tree name = DECL_NAME (fn))
25636 return dguide_name_p (name);
25637 return false;
25640 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
25642 bool
25643 copy_guide_p (const_tree fn)
25645 gcc_assert (deduction_guide_p (fn));
25646 if (!DECL_ARTIFICIAL (fn))
25647 return false;
25648 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
25649 return (TREE_CHAIN (parms) == void_list_node
25650 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
25653 /* True if FN is a guide generated from a constructor template. */
25655 bool
25656 template_guide_p (const_tree fn)
25658 gcc_assert (deduction_guide_p (fn));
25659 if (!DECL_ARTIFICIAL (fn))
25660 return false;
25661 tree tmpl = DECL_TI_TEMPLATE (fn);
25662 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
25663 return PRIMARY_TEMPLATE_P (org);
25664 return false;
25667 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
25668 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
25669 template parameter types. Note that the handling of template template
25670 parameters relies on current_template_parms being set appropriately for the
25671 new template. */
25673 static tree
25674 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
25675 tree tsubst_args, tsubst_flags_t complain)
25677 tree oldidx = get_template_parm_index (olddecl);
25679 tree newtype;
25680 if (TREE_CODE (olddecl) == TYPE_DECL
25681 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25683 tree oldtype = TREE_TYPE (olddecl);
25684 newtype = cxx_make_type (TREE_CODE (oldtype));
25685 TYPE_MAIN_VARIANT (newtype) = newtype;
25686 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
25687 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
25688 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
25690 else
25691 newtype = tsubst (TREE_TYPE (olddecl), tsubst_args,
25692 complain, NULL_TREE);
25694 tree newdecl
25695 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
25696 DECL_NAME (olddecl), newtype);
25697 SET_DECL_TEMPLATE_PARM_P (newdecl);
25699 tree newidx;
25700 if (TREE_CODE (olddecl) == TYPE_DECL
25701 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25703 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
25704 = build_template_parm_index (index, level, level,
25705 newdecl, newtype);
25706 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25707 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25708 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
25709 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
25711 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
25713 DECL_TEMPLATE_RESULT (newdecl)
25714 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
25715 DECL_NAME (olddecl), newtype);
25716 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
25717 // First create a copy (ttargs) of tsubst_args with an
25718 // additional level for the template template parameter's own
25719 // template parameters (ttparms).
25720 tree ttparms = (INNERMOST_TEMPLATE_PARMS
25721 (DECL_TEMPLATE_PARMS (olddecl)));
25722 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
25723 tree ttargs = make_tree_vec (depth + 1);
25724 for (int i = 0; i < depth; ++i)
25725 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
25726 TREE_VEC_ELT (ttargs, depth)
25727 = template_parms_level_to_args (ttparms);
25728 // Substitute ttargs into ttparms to fix references to
25729 // other template parameters.
25730 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25731 complain|tf_partial);
25732 // Now substitute again with args based on tparms, to reduce
25733 // the level of the ttparms.
25734 ttargs = current_template_args ();
25735 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25736 complain);
25737 // Finally, tack the adjusted parms onto tparms.
25738 ttparms = tree_cons (size_int (depth), ttparms,
25739 current_template_parms);
25740 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
25743 else
25745 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
25746 tree newconst
25747 = build_decl (DECL_SOURCE_LOCATION (oldconst),
25748 TREE_CODE (oldconst),
25749 DECL_NAME (oldconst), newtype);
25750 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
25751 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
25752 SET_DECL_TEMPLATE_PARM_P (newconst);
25753 newidx = build_template_parm_index (index, level, level,
25754 newconst, newtype);
25755 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25756 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25757 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
25760 return newdecl;
25763 /* Returns a C++17 class deduction guide template based on the constructor
25764 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
25765 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
25767 static tree
25768 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
25770 tree type, tparms, targs, fparms, fargs, ci;
25771 bool memtmpl = false;
25772 bool explicit_p;
25773 location_t loc;
25774 tree fn_tmpl = NULL_TREE;
25776 if (TYPE_P (ctor))
25778 type = ctor;
25779 bool copy_p = TREE_CODE (type) == REFERENCE_TYPE;
25780 if (copy_p)
25782 type = TREE_TYPE (type);
25783 fparms = tree_cons (NULL_TREE, type, void_list_node);
25785 else
25786 fparms = void_list_node;
25788 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
25789 tparms = DECL_TEMPLATE_PARMS (ctmpl);
25790 targs = CLASSTYPE_TI_ARGS (type);
25791 ci = NULL_TREE;
25792 fargs = NULL_TREE;
25793 loc = DECL_SOURCE_LOCATION (ctmpl);
25794 explicit_p = false;
25796 else
25798 ++processing_template_decl;
25800 fn_tmpl
25801 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
25802 : DECL_TI_TEMPLATE (ctor));
25803 if (outer_args)
25804 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
25805 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
25807 type = DECL_CONTEXT (ctor);
25809 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
25810 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
25811 fully specialized args for the enclosing class. Strip those off, as
25812 the deduction guide won't have those template parameters. */
25813 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
25814 TMPL_PARMS_DEPTH (tparms));
25815 /* Discard the 'this' parameter. */
25816 fparms = FUNCTION_ARG_CHAIN (ctor);
25817 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
25818 ci = get_constraints (ctor);
25819 loc = DECL_SOURCE_LOCATION (ctor);
25820 explicit_p = DECL_NONCONVERTING_P (ctor);
25822 if (PRIMARY_TEMPLATE_P (fn_tmpl))
25824 memtmpl = true;
25826 /* For a member template constructor, we need to flatten the two
25827 template parameter lists into one, and then adjust the function
25828 signature accordingly. This gets...complicated. */
25829 tree save_parms = current_template_parms;
25831 /* For a member template we should have two levels of parms/args, one
25832 for the class and one for the constructor. We stripped
25833 specialized args for further enclosing classes above. */
25834 const int depth = 2;
25835 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
25837 /* Template args for translating references to the two-level template
25838 parameters into references to the one-level template parameters we
25839 are creating. */
25840 tree tsubst_args = copy_node (targs);
25841 TMPL_ARGS_LEVEL (tsubst_args, depth)
25842 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
25844 /* Template parms for the constructor template. */
25845 tree ftparms = TREE_VALUE (tparms);
25846 unsigned flen = TREE_VEC_LENGTH (ftparms);
25847 /* Template parms for the class template. */
25848 tparms = TREE_CHAIN (tparms);
25849 tree ctparms = TREE_VALUE (tparms);
25850 unsigned clen = TREE_VEC_LENGTH (ctparms);
25851 /* Template parms for the deduction guide start as a copy of the
25852 template parms for the class. We set current_template_parms for
25853 lookup_template_class_1. */
25854 current_template_parms = tparms = copy_node (tparms);
25855 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
25856 for (unsigned i = 0; i < clen; ++i)
25857 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
25859 /* Now we need to rewrite the constructor parms to append them to the
25860 class parms. */
25861 for (unsigned i = 0; i < flen; ++i)
25863 unsigned index = i + clen;
25864 unsigned level = 1;
25865 tree oldelt = TREE_VEC_ELT (ftparms, i);
25866 tree olddecl = TREE_VALUE (oldelt);
25867 tree newdecl = rewrite_template_parm (olddecl, index, level,
25868 tsubst_args, complain);
25869 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
25870 tsubst_args, complain, ctor);
25871 tree list = build_tree_list (newdef, newdecl);
25872 TEMPLATE_PARM_CONSTRAINTS (list)
25873 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
25874 tsubst_args, complain, ctor);
25875 TREE_VEC_ELT (new_vec, index) = list;
25876 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
25879 /* Now we have a final set of template parms to substitute into the
25880 function signature. */
25881 targs = template_parms_to_args (tparms);
25882 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
25883 complain, ctor);
25884 fargs = tsubst (fargs, tsubst_args, complain, ctor);
25885 if (ci)
25886 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
25888 current_template_parms = save_parms;
25890 --processing_template_decl;
25893 if (!memtmpl)
25895 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
25896 tparms = copy_node (tparms);
25897 INNERMOST_TEMPLATE_PARMS (tparms)
25898 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
25901 tree fntype = build_function_type (type, fparms);
25902 tree ded_fn = build_lang_decl_loc (loc,
25903 FUNCTION_DECL,
25904 dguide_name (type), fntype);
25905 DECL_ARGUMENTS (ded_fn) = fargs;
25906 DECL_ARTIFICIAL (ded_fn) = true;
25907 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
25908 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
25909 DECL_ARTIFICIAL (ded_tmpl) = true;
25910 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
25911 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
25912 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
25913 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
25914 if (DECL_P (ctor))
25915 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
25916 if (ci)
25917 set_constraints (ded_tmpl, ci);
25919 return ded_tmpl;
25922 /* Deduce template arguments for the class template placeholder PTYPE for
25923 template TMPL based on the initializer INIT, and return the resulting
25924 type. */
25926 static tree
25927 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
25928 tsubst_flags_t complain)
25930 if (!DECL_CLASS_TEMPLATE_P (tmpl))
25932 /* We should have handled this in the caller. */
25933 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
25934 return ptype;
25935 if (complain & tf_error)
25936 error ("non-class template %qT used without template arguments", tmpl);
25937 return error_mark_node;
25940 tree type = TREE_TYPE (tmpl);
25942 bool try_list_ctor = false;
25944 vec<tree,va_gc> *args;
25945 if (init == NULL_TREE
25946 || TREE_CODE (init) == TREE_LIST)
25947 args = make_tree_vector_from_list (init);
25948 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
25950 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
25951 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
25953 /* As an exception, the first phase in 16.3.1.7 (considering the
25954 initializer list as a single argument) is omitted if the
25955 initializer list consists of a single expression of type cv U,
25956 where U is a specialization of C or a class derived from a
25957 specialization of C. */
25958 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
25959 tree etype = TREE_TYPE (elt);
25961 tree tparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
25962 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25963 int err = unify (tparms, targs, type, etype,
25964 UNIFY_ALLOW_DERIVED, /*explain*/false);
25965 if (err == 0)
25966 try_list_ctor = false;
25967 ggc_free (targs);
25969 if (try_list_ctor || is_std_init_list (type))
25970 args = make_tree_vector_single (init);
25971 else
25972 args = make_tree_vector_from_ctor (init);
25974 else
25975 args = make_tree_vector_single (init);
25977 tree dname = dguide_name (tmpl);
25978 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
25979 /*type*/false, /*complain*/false,
25980 /*hidden*/false);
25981 bool elided = false;
25982 if (cands == error_mark_node)
25983 cands = NULL_TREE;
25985 /* Prune explicit deduction guides in copy-initialization context. */
25986 if (flags & LOOKUP_ONLYCONVERTING)
25988 for (lkp_iterator iter (cands); !elided && iter; ++iter)
25989 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25990 elided = true;
25992 if (elided)
25994 /* Found a nonconverting guide, prune the candidates. */
25995 tree pruned = NULL_TREE;
25996 for (lkp_iterator iter (cands); iter; ++iter)
25997 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25998 pruned = lookup_add (*iter, pruned);
26000 cands = pruned;
26004 tree outer_args = NULL_TREE;
26005 if (DECL_CLASS_SCOPE_P (tmpl)
26006 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl)))
26008 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
26009 type = TREE_TYPE (most_general_template (tmpl));
26012 bool saw_ctor = false;
26013 // FIXME cache artificial deduction guides
26014 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
26016 tree guide = build_deduction_guide (*iter, outer_args, complain);
26017 if ((flags & LOOKUP_ONLYCONVERTING)
26018 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
26019 elided = true;
26020 else
26021 cands = lookup_add (guide, cands);
26023 saw_ctor = true;
26026 tree call = error_mark_node;
26028 /* If this is list-initialization and the class has a list constructor, first
26029 try deducing from the list as a single argument, as [over.match.list]. */
26030 tree list_cands = NULL_TREE;
26031 if (try_list_ctor && cands)
26032 for (lkp_iterator iter (cands); iter; ++iter)
26034 tree dg = *iter;
26035 if (is_list_ctor (dg))
26036 list_cands = lookup_add (dg, list_cands);
26038 if (list_cands)
26040 ++cp_unevaluated_operand;
26041 call = build_new_function_call (list_cands, &args, tf_decltype);
26042 --cp_unevaluated_operand;
26044 if (call == error_mark_node)
26046 /* That didn't work, now try treating the list as a sequence of
26047 arguments. */
26048 release_tree_vector (args);
26049 args = make_tree_vector_from_ctor (init);
26053 /* Maybe generate an implicit deduction guide. */
26054 if (call == error_mark_node && args->length () < 2)
26056 tree gtype = NULL_TREE;
26058 if (args->length () == 1)
26059 /* Generate a copy guide. */
26060 gtype = build_reference_type (type);
26061 else if (!saw_ctor)
26062 /* Generate a default guide. */
26063 gtype = type;
26065 if (gtype)
26067 tree guide = build_deduction_guide (gtype, outer_args, complain);
26068 cands = lookup_add (guide, cands);
26072 if (elided && !cands)
26074 error ("cannot deduce template arguments for copy-initialization"
26075 " of %qT, as it has no non-explicit deduction guides or "
26076 "user-declared constructors", type);
26077 return error_mark_node;
26079 else if (!cands && call == error_mark_node)
26081 error ("cannot deduce template arguments of %qT, as it has no viable "
26082 "deduction guides", type);
26083 return error_mark_node;
26086 if (call == error_mark_node)
26088 ++cp_unevaluated_operand;
26089 call = build_new_function_call (cands, &args, tf_decltype);
26090 --cp_unevaluated_operand;
26093 if (call == error_mark_node && (complain & tf_warning_or_error))
26095 error ("class template argument deduction failed:");
26097 ++cp_unevaluated_operand;
26098 call = build_new_function_call (cands, &args, complain | tf_decltype);
26099 --cp_unevaluated_operand;
26101 if (elided)
26102 inform (input_location, "explicit deduction guides not considered "
26103 "for copy-initialization");
26106 release_tree_vector (args);
26108 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
26111 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
26112 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
26113 The CONTEXT determines the context in which auto deduction is performed
26114 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
26115 OUTER_TARGS are used during template argument deduction
26116 (context == adc_unify) to properly substitute the result, and is ignored
26117 in other contexts.
26119 For partial-concept-ids, extra args may be appended to the list of deduced
26120 template arguments prior to determining constraint satisfaction. */
26122 tree
26123 do_auto_deduction (tree type, tree init, tree auto_node,
26124 tsubst_flags_t complain, auto_deduction_context context,
26125 tree outer_targs, int flags)
26127 tree targs;
26129 if (init == error_mark_node)
26130 return error_mark_node;
26132 if (init && type_dependent_expression_p (init)
26133 && context != adc_unify)
26134 /* Defining a subset of type-dependent expressions that we can deduce
26135 from ahead of time isn't worth the trouble. */
26136 return type;
26138 /* Similarly, we can't deduce from another undeduced decl. */
26139 if (init && undeduced_auto_decl (init))
26140 return type;
26142 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
26143 /* C++17 class template argument deduction. */
26144 return do_class_deduction (type, tmpl, init, flags, complain);
26146 if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
26147 /* Nothing we can do with this, even in deduction context. */
26148 return type;
26150 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
26151 with either a new invented type template parameter U or, if the
26152 initializer is a braced-init-list (8.5.4), with
26153 std::initializer_list<U>. */
26154 if (BRACE_ENCLOSED_INITIALIZER_P (init))
26156 if (!DIRECT_LIST_INIT_P (init))
26157 type = listify_autos (type, auto_node);
26158 else if (CONSTRUCTOR_NELTS (init) == 1)
26159 init = CONSTRUCTOR_ELT (init, 0)->value;
26160 else
26162 if (complain & tf_warning_or_error)
26164 if (permerror (input_location, "direct-list-initialization of "
26165 "%<auto%> requires exactly one element"))
26166 inform (input_location,
26167 "for deduction to %<std::initializer_list%>, use copy-"
26168 "list-initialization (i.e. add %<=%> before the %<{%>)");
26170 type = listify_autos (type, auto_node);
26174 if (type == error_mark_node)
26175 return error_mark_node;
26177 init = resolve_nondeduced_context (init, complain);
26179 if (context == adc_decomp_type
26180 && auto_node == type
26181 && init != error_mark_node
26182 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
26183 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
26184 and initializer has array type, deduce cv-qualified array type. */
26185 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
26186 complain);
26187 else if (AUTO_IS_DECLTYPE (auto_node))
26189 bool id = (DECL_P (init)
26190 || ((TREE_CODE (init) == COMPONENT_REF
26191 || TREE_CODE (init) == SCOPE_REF)
26192 && !REF_PARENTHESIZED_P (init)));
26193 targs = make_tree_vec (1);
26194 TREE_VEC_ELT (targs, 0)
26195 = finish_decltype_type (init, id, tf_warning_or_error);
26196 if (type != auto_node)
26198 if (complain & tf_error)
26199 error ("%qT as type rather than plain %<decltype(auto)%>", type);
26200 return error_mark_node;
26203 else
26205 tree parms = build_tree_list (NULL_TREE, type);
26206 tree tparms;
26208 if (flag_concepts)
26209 tparms = extract_autos (type);
26210 else
26212 tparms = make_tree_vec (1);
26213 TREE_VEC_ELT (tparms, 0)
26214 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
26217 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
26218 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
26219 DEDUCE_CALL, LOOKUP_NORMAL,
26220 NULL, /*explain_p=*/false);
26221 if (val > 0)
26223 if (processing_template_decl)
26224 /* Try again at instantiation time. */
26225 return type;
26226 if (type && type != error_mark_node
26227 && (complain & tf_error))
26228 /* If type is error_mark_node a diagnostic must have been
26229 emitted by now. Also, having a mention to '<type error>'
26230 in the diagnostic is not really useful to the user. */
26232 if (cfun && auto_node == current_function_auto_return_pattern
26233 && LAMBDA_FUNCTION_P (current_function_decl))
26234 error ("unable to deduce lambda return type from %qE", init);
26235 else
26236 error ("unable to deduce %qT from %qE", type, init);
26237 type_unification_real (tparms, targs, parms, &init, 1, 0,
26238 DEDUCE_CALL, LOOKUP_NORMAL,
26239 NULL, /*explain_p=*/true);
26241 return error_mark_node;
26245 /* Check any placeholder constraints against the deduced type. */
26246 if (flag_concepts && !processing_template_decl)
26247 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (auto_node))
26249 /* Use the deduced type to check the associated constraints. If we
26250 have a partial-concept-id, rebuild the argument list so that
26251 we check using the extra arguments. */
26252 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
26253 tree cargs = CHECK_CONSTR_ARGS (constr);
26254 if (TREE_VEC_LENGTH (cargs) > 1)
26256 cargs = copy_node (cargs);
26257 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
26259 else
26260 cargs = targs;
26261 if (!constraints_satisfied_p (constr, cargs))
26263 if (complain & tf_warning_or_error)
26265 switch (context)
26267 case adc_unspecified:
26268 case adc_unify:
26269 error("placeholder constraints not satisfied");
26270 break;
26271 case adc_variable_type:
26272 case adc_decomp_type:
26273 error ("deduced initializer does not satisfy "
26274 "placeholder constraints");
26275 break;
26276 case adc_return_type:
26277 error ("deduced return type does not satisfy "
26278 "placeholder constraints");
26279 break;
26280 case adc_requirement:
26281 error ("deduced expression type does not satisfy "
26282 "placeholder constraints");
26283 break;
26285 diagnose_constraints (input_location, constr, targs);
26287 return error_mark_node;
26291 if (processing_template_decl && context != adc_unify)
26292 outer_targs = current_template_args ();
26293 targs = add_to_template_args (outer_targs, targs);
26294 return tsubst (type, targs, complain, NULL_TREE);
26297 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
26298 result. */
26300 tree
26301 splice_late_return_type (tree type, tree late_return_type)
26303 if (is_auto (type))
26305 if (late_return_type)
26306 return late_return_type;
26308 tree idx = get_template_parm_index (type);
26309 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
26310 /* In an abbreviated function template we didn't know we were dealing
26311 with a function template when we saw the auto return type, so update
26312 it to have the correct level. */
26313 return make_auto_1 (TYPE_IDENTIFIER (type), true);
26315 return type;
26318 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
26319 'decltype(auto)' or a deduced class template. */
26321 bool
26322 is_auto (const_tree type)
26324 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
26325 && (TYPE_IDENTIFIER (type) == auto_identifier
26326 || TYPE_IDENTIFIER (type) == decltype_auto_identifier
26327 || CLASS_PLACEHOLDER_TEMPLATE (type)))
26328 return true;
26329 else
26330 return false;
26333 /* for_each_template_parm callback for type_uses_auto. */
26336 is_auto_r (tree tp, void */*data*/)
26338 return is_auto (tp);
26341 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
26342 a use of `auto'. Returns NULL_TREE otherwise. */
26344 tree
26345 type_uses_auto (tree type)
26347 if (type == NULL_TREE)
26348 return NULL_TREE;
26349 else if (flag_concepts)
26351 /* The Concepts TS allows multiple autos in one type-specifier; just
26352 return the first one we find, do_auto_deduction will collect all of
26353 them. */
26354 if (uses_template_parms (type))
26355 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
26356 /*visited*/NULL, /*nondeduced*/true);
26357 else
26358 return NULL_TREE;
26360 else
26361 return find_type_usage (type, is_auto);
26364 /* For a given template T, return the vector of typedefs referenced
26365 in T for which access check is needed at T instantiation time.
26366 T is either a FUNCTION_DECL or a RECORD_TYPE.
26367 Those typedefs were added to T by the function
26368 append_type_to_template_for_access_check. */
26370 vec<qualified_typedef_usage_t, va_gc> *
26371 get_types_needing_access_check (tree t)
26373 tree ti;
26374 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
26376 if (!t || t == error_mark_node)
26377 return NULL;
26379 if (!(ti = get_template_info (t)))
26380 return NULL;
26382 if (CLASS_TYPE_P (t)
26383 || TREE_CODE (t) == FUNCTION_DECL)
26385 if (!TI_TEMPLATE (ti))
26386 return NULL;
26388 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
26391 return result;
26394 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
26395 tied to T. That list of typedefs will be access checked at
26396 T instantiation time.
26397 T is either a FUNCTION_DECL or a RECORD_TYPE.
26398 TYPE_DECL is a TYPE_DECL node representing a typedef.
26399 SCOPE is the scope through which TYPE_DECL is accessed.
26400 LOCATION is the location of the usage point of TYPE_DECL.
26402 This function is a subroutine of
26403 append_type_to_template_for_access_check. */
26405 static void
26406 append_type_to_template_for_access_check_1 (tree t,
26407 tree type_decl,
26408 tree scope,
26409 location_t location)
26411 qualified_typedef_usage_t typedef_usage;
26412 tree ti;
26414 if (!t || t == error_mark_node)
26415 return;
26417 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
26418 || CLASS_TYPE_P (t))
26419 && type_decl
26420 && TREE_CODE (type_decl) == TYPE_DECL
26421 && scope);
26423 if (!(ti = get_template_info (t)))
26424 return;
26426 gcc_assert (TI_TEMPLATE (ti));
26428 typedef_usage.typedef_decl = type_decl;
26429 typedef_usage.context = scope;
26430 typedef_usage.locus = location;
26432 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
26435 /* Append TYPE_DECL to the template TEMPL.
26436 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
26437 At TEMPL instanciation time, TYPE_DECL will be checked to see
26438 if it can be accessed through SCOPE.
26439 LOCATION is the location of the usage point of TYPE_DECL.
26441 e.g. consider the following code snippet:
26443 class C
26445 typedef int myint;
26448 template<class U> struct S
26450 C::myint mi; // <-- usage point of the typedef C::myint
26453 S<char> s;
26455 At S<char> instantiation time, we need to check the access of C::myint
26456 In other words, we need to check the access of the myint typedef through
26457 the C scope. For that purpose, this function will add the myint typedef
26458 and the scope C through which its being accessed to a list of typedefs
26459 tied to the template S. That list will be walked at template instantiation
26460 time and access check performed on each typedefs it contains.
26461 Note that this particular code snippet should yield an error because
26462 myint is private to C. */
26464 void
26465 append_type_to_template_for_access_check (tree templ,
26466 tree type_decl,
26467 tree scope,
26468 location_t location)
26470 qualified_typedef_usage_t *iter;
26471 unsigned i;
26473 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
26475 /* Make sure we don't append the type to the template twice. */
26476 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
26477 if (iter->typedef_decl == type_decl && scope == iter->context)
26478 return;
26480 append_type_to_template_for_access_check_1 (templ, type_decl,
26481 scope, location);
26484 /* Convert the generic type parameters in PARM that match the types given in the
26485 range [START_IDX, END_IDX) from the current_template_parms into generic type
26486 packs. */
26488 tree
26489 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
26491 tree current = current_template_parms;
26492 int depth = TMPL_PARMS_DEPTH (current);
26493 current = INNERMOST_TEMPLATE_PARMS (current);
26494 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
26496 for (int i = 0; i < start_idx; ++i)
26497 TREE_VEC_ELT (replacement, i)
26498 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
26500 for (int i = start_idx; i < end_idx; ++i)
26502 /* Create a distinct parameter pack type from the current parm and add it
26503 to the replacement args to tsubst below into the generic function
26504 parameter. */
26506 tree o = TREE_TYPE (TREE_VALUE
26507 (TREE_VEC_ELT (current, i)));
26508 tree t = copy_type (o);
26509 TEMPLATE_TYPE_PARM_INDEX (t)
26510 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
26511 o, 0, 0, tf_none);
26512 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
26513 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
26514 TYPE_MAIN_VARIANT (t) = t;
26515 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
26516 TYPE_CANONICAL (t) = canonical_type_parameter (t);
26517 TREE_VEC_ELT (replacement, i) = t;
26518 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
26521 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
26522 TREE_VEC_ELT (replacement, i)
26523 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
26525 /* If there are more levels then build up the replacement with the outer
26526 template parms. */
26527 if (depth > 1)
26528 replacement = add_to_template_args (template_parms_to_args
26529 (TREE_CHAIN (current_template_parms)),
26530 replacement);
26532 return tsubst (parm, replacement, tf_none, NULL_TREE);
26535 /* Entries in the decl_constraint hash table. */
26536 struct GTY((for_user)) constr_entry
26538 tree decl;
26539 tree ci;
26542 /* Hashing function and equality for constraint entries. */
26543 struct constr_hasher : ggc_ptr_hash<constr_entry>
26545 static hashval_t hash (constr_entry *e)
26547 return (hashval_t)DECL_UID (e->decl);
26550 static bool equal (constr_entry *e1, constr_entry *e2)
26552 return e1->decl == e2->decl;
26556 /* A mapping from declarations to constraint information. Note that
26557 both templates and their underlying declarations are mapped to the
26558 same constraint information.
26560 FIXME: This is defined in pt.c because garbage collection
26561 code is not being generated for constraint.cc. */
26563 static GTY (()) hash_table<constr_hasher> *decl_constraints;
26565 /* Returns the template constraints of declaration T. If T is not
26566 constrained, return NULL_TREE. Note that T must be non-null. */
26568 tree
26569 get_constraints (tree t)
26571 if (!flag_concepts)
26572 return NULL_TREE;
26574 gcc_assert (DECL_P (t));
26575 if (TREE_CODE (t) == TEMPLATE_DECL)
26576 t = DECL_TEMPLATE_RESULT (t);
26577 constr_entry elt = { t, NULL_TREE };
26578 constr_entry* found = decl_constraints->find (&elt);
26579 if (found)
26580 return found->ci;
26581 else
26582 return NULL_TREE;
26585 /* Associate the given constraint information CI with the declaration
26586 T. If T is a template, then the constraints are associated with
26587 its underlying declaration. Don't build associations if CI is
26588 NULL_TREE. */
26590 void
26591 set_constraints (tree t, tree ci)
26593 if (!ci)
26594 return;
26595 gcc_assert (t && flag_concepts);
26596 if (TREE_CODE (t) == TEMPLATE_DECL)
26597 t = DECL_TEMPLATE_RESULT (t);
26598 gcc_assert (!get_constraints (t));
26599 constr_entry elt = {t, ci};
26600 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
26601 constr_entry* entry = ggc_alloc<constr_entry> ();
26602 *entry = elt;
26603 *slot = entry;
26606 /* Remove the associated constraints of the declaration T. */
26608 void
26609 remove_constraints (tree t)
26611 gcc_assert (DECL_P (t));
26612 if (TREE_CODE (t) == TEMPLATE_DECL)
26613 t = DECL_TEMPLATE_RESULT (t);
26615 constr_entry elt = {t, NULL_TREE};
26616 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
26617 if (slot)
26618 decl_constraints->clear_slot (slot);
26621 /* Memoized satisfaction results for declarations. This
26622 maps the pair (constraint_info, arguments) to the result computed
26623 by constraints_satisfied_p. */
26625 struct GTY((for_user)) constraint_sat_entry
26627 tree ci;
26628 tree args;
26629 tree result;
26632 /* Hashing function and equality for constraint entries. */
26634 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
26636 static hashval_t hash (constraint_sat_entry *e)
26638 hashval_t val = iterative_hash_object(e->ci, 0);
26639 return iterative_hash_template_arg (e->args, val);
26642 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
26644 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
26648 /* Memoized satisfaction results for concept checks. */
26650 struct GTY((for_user)) concept_spec_entry
26652 tree tmpl;
26653 tree args;
26654 tree result;
26657 /* Hashing function and equality for constraint entries. */
26659 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
26661 static hashval_t hash (concept_spec_entry *e)
26663 return hash_tmpl_and_args (e->tmpl, e->args);
26666 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
26668 ++comparing_specializations;
26669 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
26670 --comparing_specializations;
26671 return eq;
26675 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
26676 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
26678 /* Search for a memoized satisfaction result. Returns one of the
26679 truth value nodes if previously memoized, or NULL_TREE otherwise. */
26681 tree
26682 lookup_constraint_satisfaction (tree ci, tree args)
26684 constraint_sat_entry elt = { ci, args, NULL_TREE };
26685 constraint_sat_entry* found = constraint_memos->find (&elt);
26686 if (found)
26687 return found->result;
26688 else
26689 return NULL_TREE;
26692 /* Memoize the result of a satisfication test. Returns the saved result. */
26694 tree
26695 memoize_constraint_satisfaction (tree ci, tree args, tree result)
26697 constraint_sat_entry elt = {ci, args, result};
26698 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
26699 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
26700 *entry = elt;
26701 *slot = entry;
26702 return result;
26705 /* Search for a memoized satisfaction result for a concept. */
26707 tree
26708 lookup_concept_satisfaction (tree tmpl, tree args)
26710 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26711 concept_spec_entry* found = concept_memos->find (&elt);
26712 if (found)
26713 return found->result;
26714 else
26715 return NULL_TREE;
26718 /* Memoize the result of a concept check. Returns the saved result. */
26720 tree
26721 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
26723 concept_spec_entry elt = {tmpl, args, result};
26724 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
26725 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26726 *entry = elt;
26727 *slot = entry;
26728 return result;
26731 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
26733 /* Returns a prior concept specialization. This returns the substituted
26734 and normalized constraints defined by the concept. */
26736 tree
26737 get_concept_expansion (tree tmpl, tree args)
26739 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26740 concept_spec_entry* found = concept_expansions->find (&elt);
26741 if (found)
26742 return found->result;
26743 else
26744 return NULL_TREE;
26747 /* Save a concept expansion for later. */
26749 tree
26750 save_concept_expansion (tree tmpl, tree args, tree def)
26752 concept_spec_entry elt = {tmpl, args, def};
26753 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
26754 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26755 *entry = elt;
26756 *slot = entry;
26757 return def;
26760 static hashval_t
26761 hash_subsumption_args (tree t1, tree t2)
26763 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
26764 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
26765 int val = 0;
26766 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
26767 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
26768 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
26769 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
26770 return val;
26773 /* Compare the constraints of two subsumption entries. The LEFT1 and
26774 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
26775 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
26777 static bool
26778 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
26780 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
26781 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
26782 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
26783 CHECK_CONSTR_ARGS (right1)))
26784 return comp_template_args (CHECK_CONSTR_ARGS (left2),
26785 CHECK_CONSTR_ARGS (right2));
26786 return false;
26789 /* Key/value pair for learning and memoizing subsumption results. This
26790 associates a pair of check constraints (including arguments) with
26791 a boolean value indicating the result. */
26793 struct GTY((for_user)) subsumption_entry
26795 tree t1;
26796 tree t2;
26797 bool result;
26800 /* Hashing function and equality for constraint entries. */
26802 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
26804 static hashval_t hash (subsumption_entry *e)
26806 return hash_subsumption_args (e->t1, e->t2);
26809 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
26811 ++comparing_specializations;
26812 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
26813 --comparing_specializations;
26814 return eq;
26818 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
26820 /* Search for a previously cached subsumption result. */
26822 bool*
26823 lookup_subsumption_result (tree t1, tree t2)
26825 subsumption_entry elt = { t1, t2, false };
26826 subsumption_entry* found = subsumption_table->find (&elt);
26827 if (found)
26828 return &found->result;
26829 else
26830 return 0;
26833 /* Save a subsumption result. */
26835 bool
26836 save_subsumption_result (tree t1, tree t2, bool result)
26838 subsumption_entry elt = {t1, t2, result};
26839 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
26840 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
26841 *entry = elt;
26842 *slot = entry;
26843 return result;
26846 /* Set up the hash table for constraint association. */
26848 void
26849 init_constraint_processing (void)
26851 if (!flag_concepts)
26852 return;
26854 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
26855 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
26856 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
26857 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
26858 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
26861 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
26862 0..N-1. */
26864 void
26865 declare_integer_pack (void)
26867 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
26868 build_function_type_list (integer_type_node,
26869 integer_type_node,
26870 NULL_TREE),
26871 NULL_TREE, ECF_CONST);
26872 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
26873 DECL_BUILT_IN_CLASS (ipfn) = BUILT_IN_FRONTEND;
26876 /* Set up the hash tables for template instantiations. */
26878 void
26879 init_template_processing (void)
26881 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
26882 type_specializations = hash_table<spec_hasher>::create_ggc (37);
26884 if (cxx_dialect >= cxx11)
26885 declare_integer_pack ();
26888 /* Print stats about the template hash tables for -fstats. */
26890 void
26891 print_template_statistics (void)
26893 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
26894 "%f collisions\n", (long) decl_specializations->size (),
26895 (long) decl_specializations->elements (),
26896 decl_specializations->collisions ());
26897 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
26898 "%f collisions\n", (long) type_specializations->size (),
26899 (long) type_specializations->elements (),
26900 type_specializations->collisions ());
26903 #if CHECKING_P
26905 namespace selftest {
26907 /* Verify that build_non_dependent_expr () works, for various expressions,
26908 and that location wrappers don't affect the results. */
26910 static void
26911 test_build_non_dependent_expr ()
26913 location_t loc = BUILTINS_LOCATION;
26915 /* Verify constants, without and with location wrappers. */
26916 tree int_cst = build_int_cst (integer_type_node, 42);
26917 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
26919 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
26920 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
26921 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
26923 tree string_lit = build_string (4, "foo");
26924 TREE_TYPE (string_lit) = char_array_type_node;
26925 string_lit = fix_string_type (string_lit);
26926 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
26928 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
26929 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
26930 ASSERT_EQ (wrapped_string_lit,
26931 build_non_dependent_expr (wrapped_string_lit));
26934 /* Verify that type_dependent_expression_p () works correctly, even
26935 in the presence of location wrapper nodes. */
26937 static void
26938 test_type_dependent_expression_p ()
26940 location_t loc = BUILTINS_LOCATION;
26942 tree name = get_identifier ("foo");
26944 /* If no templates are involved, nothing is type-dependent. */
26945 gcc_assert (!processing_template_decl);
26946 ASSERT_FALSE (type_dependent_expression_p (name));
26948 ++processing_template_decl;
26950 /* Within a template, an unresolved name is always type-dependent. */
26951 ASSERT_TRUE (type_dependent_expression_p (name));
26953 /* Ensure it copes with NULL_TREE and errors. */
26954 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
26955 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
26957 /* A USING_DECL in a template should be type-dependent, even if wrapped
26958 with a location wrapper (PR c++/83799). */
26959 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
26960 TREE_TYPE (using_decl) = integer_type_node;
26961 ASSERT_TRUE (type_dependent_expression_p (using_decl));
26962 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
26963 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
26964 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
26966 --processing_template_decl;
26969 /* Run all of the selftests within this file. */
26971 void
26972 cp_pt_c_tests ()
26974 test_build_non_dependent_expr ();
26975 test_type_dependent_expression_p ();
26978 } // namespace selftest
26980 #endif /* #if CHECKING_P */
26982 #include "gt-cp-pt.h"