PR c++/83871
[official-gcc.git] / gcc / cp / pt.c
blob507675b4aae685a0eaf47d5451e2cddf00c94037
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 (template_count && DECL_CLASS_SCOPE_P (decl)
2064 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2066 gcc_assert (errorcount);
2067 return error_mark_node;
2070 fns = TREE_OPERAND (template_id, 0);
2071 explicit_targs = TREE_OPERAND (template_id, 1);
2073 if (fns == error_mark_node)
2074 return error_mark_node;
2076 /* Check for baselinks. */
2077 if (BASELINK_P (fns))
2078 fns = BASELINK_FUNCTIONS (fns);
2080 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2082 error ("%qD is not a function template", fns);
2083 return error_mark_node;
2085 else if (VAR_P (decl) && !variable_template_p (fns))
2087 error ("%qD is not a variable template", fns);
2088 return error_mark_node;
2091 /* Count the number of template headers specified for this
2092 specialization. */
2093 header_count = 0;
2094 for (b = current_binding_level;
2095 b->kind == sk_template_parms;
2096 b = b->level_chain)
2097 ++header_count;
2099 tree orig_fns = fns;
2101 if (variable_template_p (fns))
2103 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2104 targs = coerce_template_parms (parms, explicit_targs, fns,
2105 tf_warning_or_error,
2106 /*req_all*/true, /*use_defarg*/true);
2107 if (targs != error_mark_node)
2108 templates = tree_cons (targs, fns, templates);
2110 else for (lkp_iterator iter (fns); iter; ++iter)
2112 tree fn = *iter;
2114 if (TREE_CODE (fn) == TEMPLATE_DECL)
2116 tree decl_arg_types;
2117 tree fn_arg_types;
2118 tree insttype;
2120 /* In case of explicit specialization, we need to check if
2121 the number of template headers appearing in the specialization
2122 is correct. This is usually done in check_explicit_specialization,
2123 but the check done there cannot be exhaustive when specializing
2124 member functions. Consider the following code:
2126 template <> void A<int>::f(int);
2127 template <> template <> void A<int>::f(int);
2129 Assuming that A<int> is not itself an explicit specialization
2130 already, the first line specializes "f" which is a non-template
2131 member function, whilst the second line specializes "f" which
2132 is a template member function. So both lines are syntactically
2133 correct, and check_explicit_specialization does not reject
2134 them.
2136 Here, we can do better, as we are matching the specialization
2137 against the declarations. We count the number of template
2138 headers, and we check if they match TEMPLATE_COUNT + 1
2139 (TEMPLATE_COUNT is the number of qualifying template classes,
2140 plus there must be another header for the member template
2141 itself).
2143 Notice that if header_count is zero, this is not a
2144 specialization but rather a template instantiation, so there
2145 is no check we can perform here. */
2146 if (header_count && header_count != template_count + 1)
2147 continue;
2149 /* Check that the number of template arguments at the
2150 innermost level for DECL is the same as for FN. */
2151 if (current_binding_level->kind == sk_template_parms
2152 && !current_binding_level->explicit_spec_p
2153 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2154 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2155 (current_template_parms))))
2156 continue;
2158 /* DECL might be a specialization of FN. */
2159 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2160 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2162 /* For a non-static member function, we need to make sure
2163 that the const qualification is the same. Since
2164 get_bindings does not try to merge the "this" parameter,
2165 we must do the comparison explicitly. */
2166 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2167 && !same_type_p (TREE_VALUE (fn_arg_types),
2168 TREE_VALUE (decl_arg_types)))
2169 continue;
2171 /* Skip the "this" parameter and, for constructors of
2172 classes with virtual bases, the VTT parameter. A
2173 full specialization of a constructor will have a VTT
2174 parameter, but a template never will. */
2175 decl_arg_types
2176 = skip_artificial_parms_for (decl, decl_arg_types);
2177 fn_arg_types
2178 = skip_artificial_parms_for (fn, fn_arg_types);
2180 /* Function templates cannot be specializations; there are
2181 no partial specializations of functions. Therefore, if
2182 the type of DECL does not match FN, there is no
2183 match.
2185 Note that it should never be the case that we have both
2186 candidates added here, and for regular member functions
2187 below. */
2188 if (tsk == tsk_template)
2190 if (compparms (fn_arg_types, decl_arg_types))
2191 candidates = tree_cons (NULL_TREE, fn, candidates);
2192 continue;
2195 /* See whether this function might be a specialization of this
2196 template. Suppress access control because we might be trying
2197 to make this specialization a friend, and we have already done
2198 access control for the declaration of the specialization. */
2199 push_deferring_access_checks (dk_no_check);
2200 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2201 pop_deferring_access_checks ();
2203 if (!targs)
2204 /* We cannot deduce template arguments that when used to
2205 specialize TMPL will produce DECL. */
2206 continue;
2208 if (uses_template_parms (targs))
2209 /* We deduced something involving 'auto', which isn't a valid
2210 template argument. */
2211 continue;
2213 /* Remove, from the set of candidates, all those functions
2214 whose constraints are not satisfied. */
2215 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2216 continue;
2218 // Then, try to form the new function type.
2219 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2220 if (insttype == error_mark_node)
2221 continue;
2222 fn_arg_types
2223 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2224 if (!compparms (fn_arg_types, decl_arg_types))
2225 continue;
2227 /* Save this template, and the arguments deduced. */
2228 templates = tree_cons (targs, fn, templates);
2230 else if (need_member_template)
2231 /* FN is an ordinary member function, and we need a
2232 specialization of a member template. */
2234 else if (TREE_CODE (fn) != FUNCTION_DECL)
2235 /* We can get IDENTIFIER_NODEs here in certain erroneous
2236 cases. */
2238 else if (!DECL_FUNCTION_MEMBER_P (fn))
2239 /* This is just an ordinary non-member function. Nothing can
2240 be a specialization of that. */
2242 else if (DECL_ARTIFICIAL (fn))
2243 /* Cannot specialize functions that are created implicitly. */
2245 else
2247 tree decl_arg_types;
2249 /* This is an ordinary member function. However, since
2250 we're here, we can assume its enclosing class is a
2251 template class. For example,
2253 template <typename T> struct S { void f(); };
2254 template <> void S<int>::f() {}
2256 Here, S<int>::f is a non-template, but S<int> is a
2257 template class. If FN has the same type as DECL, we
2258 might be in business. */
2260 if (!DECL_TEMPLATE_INFO (fn))
2261 /* Its enclosing class is an explicit specialization
2262 of a template class. This is not a candidate. */
2263 continue;
2265 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2266 TREE_TYPE (TREE_TYPE (fn))))
2267 /* The return types differ. */
2268 continue;
2270 /* Adjust the type of DECL in case FN is a static member. */
2271 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2272 if (DECL_STATIC_FUNCTION_P (fn)
2273 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2274 decl_arg_types = TREE_CHAIN (decl_arg_types);
2276 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2277 decl_arg_types))
2278 continue;
2280 // If the deduced arguments do not satisfy the constraints,
2281 // this is not a candidate.
2282 if (flag_concepts && !constraints_satisfied_p (fn))
2283 continue;
2285 // Add the candidate.
2286 candidates = tree_cons (NULL_TREE, fn, candidates);
2290 if (templates && TREE_CHAIN (templates))
2292 /* We have:
2294 [temp.expl.spec]
2296 It is possible for a specialization with a given function
2297 signature to be instantiated from more than one function
2298 template. In such cases, explicit specification of the
2299 template arguments must be used to uniquely identify the
2300 function template specialization being specialized.
2302 Note that here, there's no suggestion that we're supposed to
2303 determine which of the candidate templates is most
2304 specialized. However, we, also have:
2306 [temp.func.order]
2308 Partial ordering of overloaded function template
2309 declarations is used in the following contexts to select
2310 the function template to which a function template
2311 specialization refers:
2313 -- when an explicit specialization refers to a function
2314 template.
2316 So, we do use the partial ordering rules, at least for now.
2317 This extension can only serve to make invalid programs valid,
2318 so it's safe. And, there is strong anecdotal evidence that
2319 the committee intended the partial ordering rules to apply;
2320 the EDG front end has that behavior, and John Spicer claims
2321 that the committee simply forgot to delete the wording in
2322 [temp.expl.spec]. */
2323 tree tmpl = most_specialized_instantiation (templates);
2324 if (tmpl != error_mark_node)
2326 templates = tmpl;
2327 TREE_CHAIN (templates) = NULL_TREE;
2331 // Concepts allows multiple declarations of member functions
2332 // with the same signature. Like above, we need to rely on
2333 // on the partial ordering of those candidates to determine which
2334 // is the best.
2335 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2337 if (tree cand = most_constrained_function (candidates))
2339 candidates = cand;
2340 TREE_CHAIN (cand) = NULL_TREE;
2344 if (templates == NULL_TREE && candidates == NULL_TREE)
2346 error ("template-id %qD for %q+D does not match any template "
2347 "declaration", template_id, decl);
2348 if (header_count && header_count != template_count + 1)
2349 inform (input_location, "saw %d %<template<>%>, need %d for "
2350 "specializing a member function template",
2351 header_count, template_count + 1);
2352 else
2353 print_candidates (orig_fns);
2354 return error_mark_node;
2356 else if ((templates && TREE_CHAIN (templates))
2357 || (candidates && TREE_CHAIN (candidates))
2358 || (templates && candidates))
2360 error ("ambiguous template specialization %qD for %q+D",
2361 template_id, decl);
2362 candidates = chainon (candidates, templates);
2363 print_candidates (candidates);
2364 return error_mark_node;
2367 /* We have one, and exactly one, match. */
2368 if (candidates)
2370 tree fn = TREE_VALUE (candidates);
2371 *targs_out = copy_node (DECL_TI_ARGS (fn));
2373 // Propagate the candidate's constraints to the declaration.
2374 set_constraints (decl, get_constraints (fn));
2376 /* DECL is a re-declaration or partial instantiation of a template
2377 function. */
2378 if (TREE_CODE (fn) == TEMPLATE_DECL)
2379 return fn;
2380 /* It was a specialization of an ordinary member function in a
2381 template class. */
2382 return DECL_TI_TEMPLATE (fn);
2385 /* It was a specialization of a template. */
2386 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2387 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2389 *targs_out = copy_node (targs);
2390 SET_TMPL_ARGS_LEVEL (*targs_out,
2391 TMPL_ARGS_DEPTH (*targs_out),
2392 TREE_PURPOSE (templates));
2394 else
2395 *targs_out = TREE_PURPOSE (templates);
2396 return TREE_VALUE (templates);
2399 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2400 but with the default argument values filled in from those in the
2401 TMPL_TYPES. */
2403 static tree
2404 copy_default_args_to_explicit_spec_1 (tree spec_types,
2405 tree tmpl_types)
2407 tree new_spec_types;
2409 if (!spec_types)
2410 return NULL_TREE;
2412 if (spec_types == void_list_node)
2413 return void_list_node;
2415 /* Substitute into the rest of the list. */
2416 new_spec_types =
2417 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2418 TREE_CHAIN (tmpl_types));
2420 /* Add the default argument for this parameter. */
2421 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2422 TREE_VALUE (spec_types),
2423 new_spec_types);
2426 /* DECL is an explicit specialization. Replicate default arguments
2427 from the template it specializes. (That way, code like:
2429 template <class T> void f(T = 3);
2430 template <> void f(double);
2431 void g () { f (); }
2433 works, as required.) An alternative approach would be to look up
2434 the correct default arguments at the call-site, but this approach
2435 is consistent with how implicit instantiations are handled. */
2437 static void
2438 copy_default_args_to_explicit_spec (tree decl)
2440 tree tmpl;
2441 tree spec_types;
2442 tree tmpl_types;
2443 tree new_spec_types;
2444 tree old_type;
2445 tree new_type;
2446 tree t;
2447 tree object_type = NULL_TREE;
2448 tree in_charge = NULL_TREE;
2449 tree vtt = NULL_TREE;
2451 /* See if there's anything we need to do. */
2452 tmpl = DECL_TI_TEMPLATE (decl);
2453 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2454 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2455 if (TREE_PURPOSE (t))
2456 break;
2457 if (!t)
2458 return;
2460 old_type = TREE_TYPE (decl);
2461 spec_types = TYPE_ARG_TYPES (old_type);
2463 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2465 /* Remove the this pointer, but remember the object's type for
2466 CV quals. */
2467 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2468 spec_types = TREE_CHAIN (spec_types);
2469 tmpl_types = TREE_CHAIN (tmpl_types);
2471 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2473 /* DECL may contain more parameters than TMPL due to the extra
2474 in-charge parameter in constructors and destructors. */
2475 in_charge = spec_types;
2476 spec_types = TREE_CHAIN (spec_types);
2478 if (DECL_HAS_VTT_PARM_P (decl))
2480 vtt = spec_types;
2481 spec_types = TREE_CHAIN (spec_types);
2485 /* Compute the merged default arguments. */
2486 new_spec_types =
2487 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2489 /* Compute the new FUNCTION_TYPE. */
2490 if (object_type)
2492 if (vtt)
2493 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2494 TREE_VALUE (vtt),
2495 new_spec_types);
2497 if (in_charge)
2498 /* Put the in-charge parameter back. */
2499 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2500 TREE_VALUE (in_charge),
2501 new_spec_types);
2503 new_type = build_method_type_directly (object_type,
2504 TREE_TYPE (old_type),
2505 new_spec_types);
2507 else
2508 new_type = build_function_type (TREE_TYPE (old_type),
2509 new_spec_types);
2510 new_type = cp_build_type_attribute_variant (new_type,
2511 TYPE_ATTRIBUTES (old_type));
2512 new_type = build_exception_variant (new_type,
2513 TYPE_RAISES_EXCEPTIONS (old_type));
2515 if (TYPE_HAS_LATE_RETURN_TYPE (old_type))
2516 TYPE_HAS_LATE_RETURN_TYPE (new_type) = 1;
2518 TREE_TYPE (decl) = new_type;
2521 /* Return the number of template headers we expect to see for a definition
2522 or specialization of CTYPE or one of its non-template members. */
2525 num_template_headers_for_class (tree ctype)
2527 int num_templates = 0;
2529 while (ctype && CLASS_TYPE_P (ctype))
2531 /* You're supposed to have one `template <...>' for every
2532 template class, but you don't need one for a full
2533 specialization. For example:
2535 template <class T> struct S{};
2536 template <> struct S<int> { void f(); };
2537 void S<int>::f () {}
2539 is correct; there shouldn't be a `template <>' for the
2540 definition of `S<int>::f'. */
2541 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2542 /* If CTYPE does not have template information of any
2543 kind, then it is not a template, nor is it nested
2544 within a template. */
2545 break;
2546 if (explicit_class_specialization_p (ctype))
2547 break;
2548 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2549 ++num_templates;
2551 ctype = TYPE_CONTEXT (ctype);
2554 return num_templates;
2557 /* Do a simple sanity check on the template headers that precede the
2558 variable declaration DECL. */
2560 void
2561 check_template_variable (tree decl)
2563 tree ctx = CP_DECL_CONTEXT (decl);
2564 int wanted = num_template_headers_for_class (ctx);
2565 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2566 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2568 if (cxx_dialect < cxx14)
2569 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2570 "variable templates only available with "
2571 "-std=c++14 or -std=gnu++14");
2573 // Namespace-scope variable templates should have a template header.
2574 ++wanted;
2576 if (template_header_count > wanted)
2578 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2579 "too many template headers for %qD "
2580 "(should be %d)",
2581 decl, wanted);
2582 if (warned && CLASS_TYPE_P (ctx)
2583 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2584 inform (DECL_SOURCE_LOCATION (decl),
2585 "members of an explicitly specialized class are defined "
2586 "without a template header");
2590 /* An explicit specialization whose declarator-id or class-head-name is not
2591 qualified shall be declared in the nearest enclosing namespace of the
2592 template, or, if the namespace is inline (7.3.1), any namespace from its
2593 enclosing namespace set.
2595 If the name declared in the explicit instantiation is an unqualified name,
2596 the explicit instantiation shall appear in the namespace where its template
2597 is declared or, if that namespace is inline (7.3.1), any namespace from its
2598 enclosing namespace set. */
2600 void
2601 check_unqualified_spec_or_inst (tree t, location_t loc)
2603 tree tmpl = most_general_template (t);
2604 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2605 && !is_nested_namespace (current_namespace,
2606 CP_DECL_CONTEXT (tmpl), true))
2608 if (processing_specialization)
2609 permerror (loc, "explicit specialization of %qD outside its "
2610 "namespace must use a nested-name-specifier", tmpl);
2611 else if (processing_explicit_instantiation
2612 && cxx_dialect >= cxx11)
2613 /* This was allowed in C++98, so only pedwarn. */
2614 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2615 "outside its namespace must use a nested-name-"
2616 "specifier", tmpl);
2620 /* Warn for a template specialization SPEC that is missing some of a set
2621 of function or type attributes that the template TEMPL is declared with.
2622 ATTRLIST is a list of additional attributes that SPEC should be taken
2623 to ultimately be declared with. */
2625 static void
2626 warn_spec_missing_attributes (tree tmpl, tree spec, tree attrlist)
2628 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
2629 tmpl = DECL_TEMPLATE_RESULT (tmpl);
2631 if (TREE_CODE (tmpl) != FUNCTION_DECL)
2632 return;
2634 /* Avoid warning if either declaration or its type is deprecated. */
2635 if (TREE_DEPRECATED (tmpl)
2636 || TREE_DEPRECATED (spec))
2637 return;
2639 tree tmpl_type = TREE_TYPE (tmpl);
2640 tree spec_type = TREE_TYPE (spec);
2642 if (TREE_DEPRECATED (tmpl_type)
2643 || TREE_DEPRECATED (spec_type)
2644 || TREE_DEPRECATED (TREE_TYPE (tmpl_type))
2645 || TREE_DEPRECATED (TREE_TYPE (spec_type)))
2646 return;
2648 tree tmpl_attrs[] = { DECL_ATTRIBUTES (tmpl), TYPE_ATTRIBUTES (tmpl_type) };
2649 tree spec_attrs[] = { DECL_ATTRIBUTES (spec), TYPE_ATTRIBUTES (spec_type) };
2651 if (!spec_attrs[0])
2652 spec_attrs[0] = attrlist;
2653 else if (!spec_attrs[1])
2654 spec_attrs[1] = attrlist;
2656 /* Avoid warning if the primary has no attributes. */
2657 if (!tmpl_attrs[0] && !tmpl_attrs[1])
2658 return;
2660 /* Avoid warning if either declaration contains an attribute on
2661 the white list below. */
2662 const char* const whitelist[] = {
2663 "error", "warning"
2666 for (unsigned i = 0; i != 2; ++i)
2667 for (unsigned j = 0; j != sizeof whitelist / sizeof *whitelist; ++j)
2668 if (lookup_attribute (whitelist[j], tmpl_attrs[i])
2669 || lookup_attribute (whitelist[j], spec_attrs[i]))
2670 return;
2672 /* Avoid warning if the difference between the primary and
2673 the specialization is not in one of the attributes below. */
2674 const char* const blacklist[] = {
2675 "alloc_align", "alloc_size", "assume_aligned", "format",
2676 "format_arg", "malloc", "nonnull"
2679 /* Put together a list of the black listed attributes that the primary
2680 template is declared with that the specialization is not, in case
2681 it's not apparent from the most recent declaration of the primary. */
2682 unsigned nattrs = 0;
2683 pretty_printer str;
2685 for (unsigned i = 0; i != sizeof blacklist / sizeof *blacklist; ++i)
2687 for (unsigned j = 0; j != 2; ++j)
2689 if (!lookup_attribute (blacklist[i], tmpl_attrs[j]))
2690 continue;
2692 for (unsigned k = 0; k != 1 + !!spec_attrs[1]; ++k)
2694 if (lookup_attribute (blacklist[i], spec_attrs[k]))
2695 break;
2697 if (nattrs)
2698 pp_string (&str, ", ");
2699 pp_begin_quote (&str, pp_show_color (global_dc->printer));
2700 pp_string (&str, blacklist[i]);
2701 pp_end_quote (&str, pp_show_color (global_dc->printer));
2702 ++nattrs;
2707 if (!nattrs)
2708 return;
2710 if (warning_at (DECL_SOURCE_LOCATION (spec), OPT_Wmissing_attributes,
2711 "explicit specialization %q#D may be missing attributes",
2712 spec))
2713 inform (DECL_SOURCE_LOCATION (tmpl),
2714 nattrs > 1
2715 ? G_("missing primary template attributes %s")
2716 : G_("missing primary template attribute %s"),
2717 pp_formatted_text (&str));
2720 /* Check to see if the function just declared, as indicated in
2721 DECLARATOR, and in DECL, is a specialization of a function
2722 template. We may also discover that the declaration is an explicit
2723 instantiation at this point.
2725 Returns DECL, or an equivalent declaration that should be used
2726 instead if all goes well. Issues an error message if something is
2727 amiss. Returns error_mark_node if the error is not easily
2728 recoverable.
2730 FLAGS is a bitmask consisting of the following flags:
2732 2: The function has a definition.
2733 4: The function is a friend.
2735 The TEMPLATE_COUNT is the number of references to qualifying
2736 template classes that appeared in the name of the function. For
2737 example, in
2739 template <class T> struct S { void f(); };
2740 void S<int>::f();
2742 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2743 classes are not counted in the TEMPLATE_COUNT, so that in
2745 template <class T> struct S {};
2746 template <> struct S<int> { void f(); }
2747 template <> void S<int>::f();
2749 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2750 invalid; there should be no template <>.)
2752 If the function is a specialization, it is marked as such via
2753 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2754 is set up correctly, and it is added to the list of specializations
2755 for that template. */
2757 tree
2758 check_explicit_specialization (tree declarator,
2759 tree decl,
2760 int template_count,
2761 int flags,
2762 tree attrlist)
2764 int have_def = flags & 2;
2765 int is_friend = flags & 4;
2766 bool is_concept = flags & 8;
2767 int specialization = 0;
2768 int explicit_instantiation = 0;
2769 int member_specialization = 0;
2770 tree ctype = DECL_CLASS_CONTEXT (decl);
2771 tree dname = DECL_NAME (decl);
2772 tmpl_spec_kind tsk;
2774 if (is_friend)
2776 if (!processing_specialization)
2777 tsk = tsk_none;
2778 else
2779 tsk = tsk_excessive_parms;
2781 else
2782 tsk = current_tmpl_spec_kind (template_count);
2784 switch (tsk)
2786 case tsk_none:
2787 if (processing_specialization && !VAR_P (decl))
2789 specialization = 1;
2790 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2792 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2794 if (is_friend)
2795 /* This could be something like:
2797 template <class T> void f(T);
2798 class S { friend void f<>(int); } */
2799 specialization = 1;
2800 else
2802 /* This case handles bogus declarations like template <>
2803 template <class T> void f<int>(); */
2805 error ("template-id %qD in declaration of primary template",
2806 declarator);
2807 return decl;
2810 break;
2812 case tsk_invalid_member_spec:
2813 /* The error has already been reported in
2814 check_specialization_scope. */
2815 return error_mark_node;
2817 case tsk_invalid_expl_inst:
2818 error ("template parameter list used in explicit instantiation");
2820 /* Fall through. */
2822 case tsk_expl_inst:
2823 if (have_def)
2824 error ("definition provided for explicit instantiation");
2826 explicit_instantiation = 1;
2827 break;
2829 case tsk_excessive_parms:
2830 case tsk_insufficient_parms:
2831 if (tsk == tsk_excessive_parms)
2832 error ("too many template parameter lists in declaration of %qD",
2833 decl);
2834 else if (template_header_count)
2835 error("too few template parameter lists in declaration of %qD", decl);
2836 else
2837 error("explicit specialization of %qD must be introduced by "
2838 "%<template <>%>", decl);
2840 /* Fall through. */
2841 case tsk_expl_spec:
2842 if (is_concept)
2843 error ("explicit specialization declared %<concept%>");
2845 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2846 /* In cases like template<> constexpr bool v = true;
2847 We'll give an error in check_template_variable. */
2848 break;
2850 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2851 if (ctype)
2852 member_specialization = 1;
2853 else
2854 specialization = 1;
2855 break;
2857 case tsk_template:
2858 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2860 /* This case handles bogus declarations like template <>
2861 template <class T> void f<int>(); */
2863 if (!uses_template_parms (declarator))
2864 error ("template-id %qD in declaration of primary template",
2865 declarator);
2866 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2868 /* Partial specialization of variable template. */
2869 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2870 specialization = 1;
2871 goto ok;
2873 else if (cxx_dialect < cxx14)
2874 error ("non-type partial specialization %qD "
2875 "is not allowed", declarator);
2876 else
2877 error ("non-class, non-variable partial specialization %qD "
2878 "is not allowed", declarator);
2879 return decl;
2880 ok:;
2883 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2884 /* This is a specialization of a member template, without
2885 specialization the containing class. Something like:
2887 template <class T> struct S {
2888 template <class U> void f (U);
2890 template <> template <class U> void S<int>::f(U) {}
2892 That's a specialization -- but of the entire template. */
2893 specialization = 1;
2894 break;
2896 default:
2897 gcc_unreachable ();
2900 if ((specialization || member_specialization)
2901 /* This doesn't apply to variable templates. */
2902 && (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE
2903 || TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE))
2905 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2906 for (; t; t = TREE_CHAIN (t))
2907 if (TREE_PURPOSE (t))
2909 permerror (input_location,
2910 "default argument specified in explicit specialization");
2911 break;
2915 if (specialization || member_specialization || explicit_instantiation)
2917 tree tmpl = NULL_TREE;
2918 tree targs = NULL_TREE;
2919 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2921 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2922 if (!was_template_id)
2924 tree fns;
2926 gcc_assert (identifier_p (declarator));
2927 if (ctype)
2928 fns = dname;
2929 else
2931 /* If there is no class context, the explicit instantiation
2932 must be at namespace scope. */
2933 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2935 /* Find the namespace binding, using the declaration
2936 context. */
2937 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2938 false, true);
2939 if (fns == error_mark_node)
2940 /* If lookup fails, look for a friend declaration so we can
2941 give a better diagnostic. */
2942 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2943 /*type*/false, /*complain*/true,
2944 /*hidden*/true);
2946 if (fns == error_mark_node || !is_overloaded_fn (fns))
2948 error ("%qD is not a template function", dname);
2949 fns = error_mark_node;
2953 declarator = lookup_template_function (fns, NULL_TREE);
2956 if (declarator == error_mark_node)
2957 return error_mark_node;
2959 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2961 if (!explicit_instantiation)
2962 /* A specialization in class scope. This is invalid,
2963 but the error will already have been flagged by
2964 check_specialization_scope. */
2965 return error_mark_node;
2966 else
2968 /* It's not valid to write an explicit instantiation in
2969 class scope, e.g.:
2971 class C { template void f(); }
2973 This case is caught by the parser. However, on
2974 something like:
2976 template class C { void f(); };
2978 (which is invalid) we can get here. The error will be
2979 issued later. */
2983 return decl;
2985 else if (ctype != NULL_TREE
2986 && (identifier_p (TREE_OPERAND (declarator, 0))))
2988 // We'll match variable templates in start_decl.
2989 if (VAR_P (decl))
2990 return decl;
2992 /* Find the list of functions in ctype that have the same
2993 name as the declared function. */
2994 tree name = TREE_OPERAND (declarator, 0);
2996 if (constructor_name_p (name, ctype))
2998 if (DECL_CONSTRUCTOR_P (decl)
2999 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
3000 : !CLASSTYPE_DESTRUCTOR (ctype))
3002 /* From [temp.expl.spec]:
3004 If such an explicit specialization for the member
3005 of a class template names an implicitly-declared
3006 special member function (clause _special_), the
3007 program is ill-formed.
3009 Similar language is found in [temp.explicit]. */
3010 error ("specialization of implicitly-declared special member function");
3011 return error_mark_node;
3014 name = DECL_NAME (decl);
3017 /* For a type-conversion operator, We might be looking for
3018 `operator int' which will be a specialization of
3019 `operator T'. Grab all the conversion operators, and
3020 then select from them. */
3021 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
3022 ? conv_op_identifier : name);
3024 if (fns == NULL_TREE)
3026 error ("no member function %qD declared in %qT", name, ctype);
3027 return error_mark_node;
3029 else
3030 TREE_OPERAND (declarator, 0) = fns;
3033 /* Figure out what exactly is being specialized at this point.
3034 Note that for an explicit instantiation, even one for a
3035 member function, we cannot tell a priori whether the
3036 instantiation is for a member template, or just a member
3037 function of a template class. Even if a member template is
3038 being instantiated, the member template arguments may be
3039 elided if they can be deduced from the rest of the
3040 declaration. */
3041 tmpl = determine_specialization (declarator, decl,
3042 &targs,
3043 member_specialization,
3044 template_count,
3045 tsk);
3047 if (!tmpl || tmpl == error_mark_node)
3048 /* We couldn't figure out what this declaration was
3049 specializing. */
3050 return error_mark_node;
3051 else
3053 if (TREE_CODE (decl) == FUNCTION_DECL
3054 && DECL_HIDDEN_FRIEND_P (tmpl))
3056 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3057 "friend declaration %qD is not visible to "
3058 "explicit specialization", tmpl))
3059 inform (DECL_SOURCE_LOCATION (tmpl),
3060 "friend declaration here");
3062 else if (!ctype && !is_friend
3063 && CP_DECL_CONTEXT (decl) == current_namespace)
3064 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
3066 tree gen_tmpl = most_general_template (tmpl);
3068 if (explicit_instantiation)
3070 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3071 is done by do_decl_instantiation later. */
3073 int arg_depth = TMPL_ARGS_DEPTH (targs);
3074 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3076 if (arg_depth > parm_depth)
3078 /* If TMPL is not the most general template (for
3079 example, if TMPL is a friend template that is
3080 injected into namespace scope), then there will
3081 be too many levels of TARGS. Remove some of them
3082 here. */
3083 int i;
3084 tree new_targs;
3086 new_targs = make_tree_vec (parm_depth);
3087 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3088 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3089 = TREE_VEC_ELT (targs, i);
3090 targs = new_targs;
3093 return instantiate_template (tmpl, targs, tf_error);
3096 /* If we thought that the DECL was a member function, but it
3097 turns out to be specializing a static member function,
3098 make DECL a static member function as well. */
3099 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3100 && DECL_STATIC_FUNCTION_P (tmpl)
3101 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3102 revert_static_member_fn (decl);
3104 /* If this is a specialization of a member template of a
3105 template class, we want to return the TEMPLATE_DECL, not
3106 the specialization of it. */
3107 if (tsk == tsk_template && !was_template_id)
3109 tree result = DECL_TEMPLATE_RESULT (tmpl);
3110 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3111 DECL_INITIAL (result) = NULL_TREE;
3112 if (have_def)
3114 tree parm;
3115 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3116 DECL_SOURCE_LOCATION (result)
3117 = DECL_SOURCE_LOCATION (decl);
3118 /* We want to use the argument list specified in the
3119 definition, not in the original declaration. */
3120 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3121 for (parm = DECL_ARGUMENTS (result); parm;
3122 parm = DECL_CHAIN (parm))
3123 DECL_CONTEXT (parm) = result;
3125 return register_specialization (tmpl, gen_tmpl, targs,
3126 is_friend, 0);
3129 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3130 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3132 if (was_template_id)
3133 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3135 /* Inherit default function arguments from the template
3136 DECL is specializing. */
3137 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3138 copy_default_args_to_explicit_spec (decl);
3140 /* This specialization has the same protection as the
3141 template it specializes. */
3142 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3143 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3145 /* 7.1.1-1 [dcl.stc]
3147 A storage-class-specifier shall not be specified in an
3148 explicit specialization...
3150 The parser rejects these, so unless action is taken here,
3151 explicit function specializations will always appear with
3152 global linkage.
3154 The action recommended by the C++ CWG in response to C++
3155 defect report 605 is to make the storage class and linkage
3156 of the explicit specialization match the templated function:
3158 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3160 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3162 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3163 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3165 /* A concept cannot be specialized. */
3166 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3168 error ("explicit specialization of function concept %qD",
3169 gen_tmpl);
3170 return error_mark_node;
3173 /* This specialization has the same linkage and visibility as
3174 the function template it specializes. */
3175 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3176 if (! TREE_PUBLIC (decl))
3178 DECL_INTERFACE_KNOWN (decl) = 1;
3179 DECL_NOT_REALLY_EXTERN (decl) = 1;
3181 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3182 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3184 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3185 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3189 /* If DECL is a friend declaration, declared using an
3190 unqualified name, the namespace associated with DECL may
3191 have been set incorrectly. For example, in:
3193 template <typename T> void f(T);
3194 namespace N {
3195 struct S { friend void f<int>(int); }
3198 we will have set the DECL_CONTEXT for the friend
3199 declaration to N, rather than to the global namespace. */
3200 if (DECL_NAMESPACE_SCOPE_P (decl))
3201 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3203 if (is_friend && !have_def)
3204 /* This is not really a declaration of a specialization.
3205 It's just the name of an instantiation. But, it's not
3206 a request for an instantiation, either. */
3207 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3208 else if (TREE_CODE (decl) == FUNCTION_DECL)
3209 /* A specialization is not necessarily COMDAT. */
3210 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3211 && DECL_DECLARED_INLINE_P (decl));
3212 else if (VAR_P (decl))
3213 DECL_COMDAT (decl) = false;
3215 /* If this is a full specialization, register it so that we can find
3216 it again. Partial specializations will be registered in
3217 process_partial_specialization. */
3218 if (!processing_template_decl)
3220 warn_spec_missing_attributes (gen_tmpl, decl, attrlist);
3222 decl = register_specialization (decl, gen_tmpl, targs,
3223 is_friend, 0);
3227 /* A 'structor should already have clones. */
3228 gcc_assert (decl == error_mark_node
3229 || variable_template_p (tmpl)
3230 || !(DECL_CONSTRUCTOR_P (decl)
3231 || DECL_DESTRUCTOR_P (decl))
3232 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3236 return decl;
3239 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3240 parameters. These are represented in the same format used for
3241 DECL_TEMPLATE_PARMS. */
3244 comp_template_parms (const_tree parms1, const_tree parms2)
3246 const_tree p1;
3247 const_tree p2;
3249 if (parms1 == parms2)
3250 return 1;
3252 for (p1 = parms1, p2 = parms2;
3253 p1 != NULL_TREE && p2 != NULL_TREE;
3254 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3256 tree t1 = TREE_VALUE (p1);
3257 tree t2 = TREE_VALUE (p2);
3258 int i;
3260 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3261 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3263 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3264 return 0;
3266 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3268 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3269 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3271 /* If either of the template parameters are invalid, assume
3272 they match for the sake of error recovery. */
3273 if (error_operand_p (parm1) || error_operand_p (parm2))
3274 return 1;
3276 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3277 return 0;
3279 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3280 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3281 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3282 continue;
3283 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3284 return 0;
3288 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3289 /* One set of parameters has more parameters lists than the
3290 other. */
3291 return 0;
3293 return 1;
3296 /* Determine whether PARM is a parameter pack. */
3298 bool
3299 template_parameter_pack_p (const_tree parm)
3301 /* Determine if we have a non-type template parameter pack. */
3302 if (TREE_CODE (parm) == PARM_DECL)
3303 return (DECL_TEMPLATE_PARM_P (parm)
3304 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3305 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3306 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3308 /* If this is a list of template parameters, we could get a
3309 TYPE_DECL or a TEMPLATE_DECL. */
3310 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3311 parm = TREE_TYPE (parm);
3313 /* Otherwise it must be a type template parameter. */
3314 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3315 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3316 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3319 /* Determine if T is a function parameter pack. */
3321 bool
3322 function_parameter_pack_p (const_tree t)
3324 if (t && TREE_CODE (t) == PARM_DECL)
3325 return DECL_PACK_P (t);
3326 return false;
3329 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3330 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3332 tree
3333 get_function_template_decl (const_tree primary_func_tmpl_inst)
3335 if (! primary_func_tmpl_inst
3336 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3337 || ! primary_template_specialization_p (primary_func_tmpl_inst))
3338 return NULL;
3340 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3343 /* Return true iff the function parameter PARAM_DECL was expanded
3344 from the function parameter pack PACK. */
3346 bool
3347 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3349 if (DECL_ARTIFICIAL (param_decl)
3350 || !function_parameter_pack_p (pack))
3351 return false;
3353 /* The parameter pack and its pack arguments have the same
3354 DECL_PARM_INDEX. */
3355 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3358 /* Determine whether ARGS describes a variadic template args list,
3359 i.e., one that is terminated by a template argument pack. */
3361 static bool
3362 template_args_variadic_p (tree args)
3364 int nargs;
3365 tree last_parm;
3367 if (args == NULL_TREE)
3368 return false;
3370 args = INNERMOST_TEMPLATE_ARGS (args);
3371 nargs = TREE_VEC_LENGTH (args);
3373 if (nargs == 0)
3374 return false;
3376 last_parm = TREE_VEC_ELT (args, nargs - 1);
3378 return ARGUMENT_PACK_P (last_parm);
3381 /* Generate a new name for the parameter pack name NAME (an
3382 IDENTIFIER_NODE) that incorporates its */
3384 static tree
3385 make_ith_pack_parameter_name (tree name, int i)
3387 /* Munge the name to include the parameter index. */
3388 #define NUMBUF_LEN 128
3389 char numbuf[NUMBUF_LEN];
3390 char* newname;
3391 int newname_len;
3393 if (name == NULL_TREE)
3394 return name;
3395 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3396 newname_len = IDENTIFIER_LENGTH (name)
3397 + strlen (numbuf) + 2;
3398 newname = (char*)alloca (newname_len);
3399 snprintf (newname, newname_len,
3400 "%s#%i", IDENTIFIER_POINTER (name), i);
3401 return get_identifier (newname);
3404 /* Return true if T is a primary function, class or alias template
3405 specialization, not including the template pattern. */
3407 bool
3408 primary_template_specialization_p (const_tree t)
3410 if (!t)
3411 return false;
3413 if (TREE_CODE (t) == FUNCTION_DECL || VAR_P (t))
3414 return (DECL_LANG_SPECIFIC (t)
3415 && DECL_USE_TEMPLATE (t)
3416 && DECL_TEMPLATE_INFO (t)
3417 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
3418 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3419 return (CLASSTYPE_TEMPLATE_INFO (t)
3420 && CLASSTYPE_USE_TEMPLATE (t)
3421 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
3422 else if (alias_template_specialization_p (t))
3423 return true;
3424 return false;
3427 /* Return true if PARM is a template template parameter. */
3429 bool
3430 template_template_parameter_p (const_tree parm)
3432 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3435 /* Return true iff PARM is a DECL representing a type template
3436 parameter. */
3438 bool
3439 template_type_parameter_p (const_tree parm)
3441 return (parm
3442 && (TREE_CODE (parm) == TYPE_DECL
3443 || TREE_CODE (parm) == TEMPLATE_DECL)
3444 && DECL_TEMPLATE_PARM_P (parm));
3447 /* Return the template parameters of T if T is a
3448 primary template instantiation, NULL otherwise. */
3450 tree
3451 get_primary_template_innermost_parameters (const_tree t)
3453 tree parms = NULL, template_info = NULL;
3455 if ((template_info = get_template_info (t))
3456 && primary_template_specialization_p (t))
3457 parms = INNERMOST_TEMPLATE_PARMS
3458 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3460 return parms;
3463 /* Return the template parameters of the LEVELth level from the full list
3464 of template parameters PARMS. */
3466 tree
3467 get_template_parms_at_level (tree parms, int level)
3469 tree p;
3470 if (!parms
3471 || TREE_CODE (parms) != TREE_LIST
3472 || level > TMPL_PARMS_DEPTH (parms))
3473 return NULL_TREE;
3475 for (p = parms; p; p = TREE_CHAIN (p))
3476 if (TMPL_PARMS_DEPTH (p) == level)
3477 return p;
3479 return NULL_TREE;
3482 /* Returns the template arguments of T if T is a template instantiation,
3483 NULL otherwise. */
3485 tree
3486 get_template_innermost_arguments (const_tree t)
3488 tree args = NULL, template_info = NULL;
3490 if ((template_info = get_template_info (t))
3491 && TI_ARGS (template_info))
3492 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3494 return args;
3497 /* Return the argument pack elements of T if T is a template argument pack,
3498 NULL otherwise. */
3500 tree
3501 get_template_argument_pack_elems (const_tree t)
3503 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3504 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3505 return NULL;
3507 return ARGUMENT_PACK_ARGS (t);
3510 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3511 ARGUMENT_PACK_SELECT represents. */
3513 static tree
3514 argument_pack_select_arg (tree t)
3516 tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
3517 tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
3519 /* If the selected argument is an expansion E, that most likely means we were
3520 called from gen_elem_of_pack_expansion_instantiation during the
3521 substituting of an argument pack (of which the Ith element is a pack
3522 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3523 In this case, the Ith element resulting from this substituting is going to
3524 be a pack expansion, which pattern is the pattern of E. Let's return the
3525 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3526 resulting pack expansion from it. */
3527 if (PACK_EXPANSION_P (arg))
3529 /* Make sure we aren't throwing away arg info. */
3530 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
3531 arg = PACK_EXPANSION_PATTERN (arg);
3534 return arg;
3538 /* True iff FN is a function representing a built-in variadic parameter
3539 pack. */
3541 bool
3542 builtin_pack_fn_p (tree fn)
3544 if (!fn
3545 || TREE_CODE (fn) != FUNCTION_DECL
3546 || !DECL_IS_BUILTIN (fn))
3547 return false;
3549 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3550 return true;
3552 return false;
3555 /* True iff CALL is a call to a function representing a built-in variadic
3556 parameter pack. */
3558 static bool
3559 builtin_pack_call_p (tree call)
3561 if (TREE_CODE (call) != CALL_EXPR)
3562 return false;
3563 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3566 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3568 static tree
3569 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3570 tree in_decl)
3572 tree ohi = CALL_EXPR_ARG (call, 0);
3573 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3574 false/*fn*/, true/*int_cst*/);
3576 if (value_dependent_expression_p (hi))
3578 if (hi != ohi)
3580 call = copy_node (call);
3581 CALL_EXPR_ARG (call, 0) = hi;
3583 tree ex = make_pack_expansion (call, complain);
3584 tree vec = make_tree_vec (1);
3585 TREE_VEC_ELT (vec, 0) = ex;
3586 return vec;
3588 else
3590 hi = cxx_constant_value (hi);
3591 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3593 /* Calculate the largest value of len that won't make the size of the vec
3594 overflow an int. The compiler will exceed resource limits long before
3595 this, but it seems a decent place to diagnose. */
3596 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3598 if (len < 0 || len > max)
3600 if ((complain & tf_error)
3601 && hi != error_mark_node)
3602 error ("argument to __integer_pack must be between 0 and %d", max);
3603 return error_mark_node;
3606 tree vec = make_tree_vec (len);
3608 for (int i = 0; i < len; ++i)
3609 TREE_VEC_ELT (vec, i) = size_int (i);
3611 return vec;
3615 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3616 CALL. */
3618 static tree
3619 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3620 tree in_decl)
3622 if (!builtin_pack_call_p (call))
3623 return NULL_TREE;
3625 tree fn = CALL_EXPR_FN (call);
3627 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3628 return expand_integer_pack (call, args, complain, in_decl);
3630 return NULL_TREE;
3633 /* Structure used to track the progress of find_parameter_packs_r. */
3634 struct find_parameter_pack_data
3636 /* TREE_LIST that will contain all of the parameter packs found by
3637 the traversal. */
3638 tree* parameter_packs;
3640 /* Set of AST nodes that have been visited by the traversal. */
3641 hash_set<tree> *visited;
3643 /* True iff we're making a type pack expansion. */
3644 bool type_pack_expansion_p;
3647 /* Identifies all of the argument packs that occur in a template
3648 argument and appends them to the TREE_LIST inside DATA, which is a
3649 find_parameter_pack_data structure. This is a subroutine of
3650 make_pack_expansion and uses_parameter_packs. */
3651 static tree
3652 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3654 tree t = *tp;
3655 struct find_parameter_pack_data* ppd =
3656 (struct find_parameter_pack_data*)data;
3657 bool parameter_pack_p = false;
3659 /* Handle type aliases/typedefs. */
3660 if (TYPE_ALIAS_P (t))
3662 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3663 cp_walk_tree (&TI_ARGS (tinfo),
3664 &find_parameter_packs_r,
3665 ppd, ppd->visited);
3666 *walk_subtrees = 0;
3667 return NULL_TREE;
3670 /* Identify whether this is a parameter pack or not. */
3671 switch (TREE_CODE (t))
3673 case TEMPLATE_PARM_INDEX:
3674 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3675 parameter_pack_p = true;
3676 break;
3678 case TEMPLATE_TYPE_PARM:
3679 t = TYPE_MAIN_VARIANT (t);
3680 /* FALLTHRU */
3681 case TEMPLATE_TEMPLATE_PARM:
3682 /* If the placeholder appears in the decl-specifier-seq of a function
3683 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3684 is a pack expansion, the invented template parameter is a template
3685 parameter pack. */
3686 if (ppd->type_pack_expansion_p && is_auto (t))
3687 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3688 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3689 parameter_pack_p = true;
3690 break;
3692 case FIELD_DECL:
3693 case PARM_DECL:
3694 if (DECL_PACK_P (t))
3696 /* We don't want to walk into the type of a PARM_DECL,
3697 because we don't want to see the type parameter pack. */
3698 *walk_subtrees = 0;
3699 parameter_pack_p = true;
3701 break;
3703 case VAR_DECL:
3704 if (DECL_PACK_P (t))
3706 /* We don't want to walk into the type of a variadic capture proxy,
3707 because we don't want to see the type parameter pack. */
3708 *walk_subtrees = 0;
3709 parameter_pack_p = true;
3711 else if (variable_template_specialization_p (t))
3713 cp_walk_tree (&DECL_TI_ARGS (t),
3714 find_parameter_packs_r,
3715 ppd, ppd->visited);
3716 *walk_subtrees = 0;
3718 break;
3720 case CALL_EXPR:
3721 if (builtin_pack_call_p (t))
3722 parameter_pack_p = true;
3723 break;
3725 case BASES:
3726 parameter_pack_p = true;
3727 break;
3728 default:
3729 /* Not a parameter pack. */
3730 break;
3733 if (parameter_pack_p)
3735 /* Add this parameter pack to the list. */
3736 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3739 if (TYPE_P (t))
3740 cp_walk_tree (&TYPE_CONTEXT (t),
3741 &find_parameter_packs_r, ppd, ppd->visited);
3743 /* This switch statement will return immediately if we don't find a
3744 parameter pack. */
3745 switch (TREE_CODE (t))
3747 case TEMPLATE_PARM_INDEX:
3748 return NULL_TREE;
3750 case BOUND_TEMPLATE_TEMPLATE_PARM:
3751 /* Check the template itself. */
3752 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3753 &find_parameter_packs_r, ppd, ppd->visited);
3754 /* Check the template arguments. */
3755 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3756 ppd->visited);
3757 *walk_subtrees = 0;
3758 return NULL_TREE;
3760 case TEMPLATE_TYPE_PARM:
3761 case TEMPLATE_TEMPLATE_PARM:
3762 return NULL_TREE;
3764 case PARM_DECL:
3765 return NULL_TREE;
3767 case DECL_EXPR:
3768 /* Ignore the declaration of a capture proxy for a parameter pack. */
3769 if (is_capture_proxy (DECL_EXPR_DECL (t)))
3770 *walk_subtrees = 0;
3771 return NULL_TREE;
3773 case RECORD_TYPE:
3774 if (TYPE_PTRMEMFUNC_P (t))
3775 return NULL_TREE;
3776 /* Fall through. */
3778 case UNION_TYPE:
3779 case ENUMERAL_TYPE:
3780 if (TYPE_TEMPLATE_INFO (t))
3781 cp_walk_tree (&TYPE_TI_ARGS (t),
3782 &find_parameter_packs_r, ppd, ppd->visited);
3784 *walk_subtrees = 0;
3785 return NULL_TREE;
3787 case TEMPLATE_DECL:
3788 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3789 return NULL_TREE;
3790 gcc_fallthrough();
3792 case CONSTRUCTOR:
3793 cp_walk_tree (&TREE_TYPE (t),
3794 &find_parameter_packs_r, ppd, ppd->visited);
3795 return NULL_TREE;
3797 case TYPENAME_TYPE:
3798 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3799 ppd, ppd->visited);
3800 *walk_subtrees = 0;
3801 return NULL_TREE;
3803 case TYPE_PACK_EXPANSION:
3804 case EXPR_PACK_EXPANSION:
3805 *walk_subtrees = 0;
3806 return NULL_TREE;
3808 case INTEGER_TYPE:
3809 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3810 ppd, ppd->visited);
3811 *walk_subtrees = 0;
3812 return NULL_TREE;
3814 case IDENTIFIER_NODE:
3815 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3816 ppd->visited);
3817 *walk_subtrees = 0;
3818 return NULL_TREE;
3820 case LAMBDA_EXPR:
3822 /* Look at explicit captures. */
3823 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t);
3824 cap; cap = TREE_CHAIN (cap))
3825 cp_walk_tree (&TREE_VALUE (cap), &find_parameter_packs_r, ppd,
3826 ppd->visited);
3827 /* Since we defer implicit capture, look in the body as well. */
3828 tree fn = lambda_function (t);
3829 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
3830 ppd->visited);
3831 *walk_subtrees = 0;
3832 return NULL_TREE;
3835 case DECLTYPE_TYPE:
3837 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3838 type_pack_expansion_p to false so that any placeholders
3839 within the expression don't get marked as parameter packs. */
3840 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
3841 ppd->type_pack_expansion_p = false;
3842 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
3843 ppd, ppd->visited);
3844 ppd->type_pack_expansion_p = type_pack_expansion_p;
3845 *walk_subtrees = 0;
3846 return NULL_TREE;
3849 default:
3850 return NULL_TREE;
3853 return NULL_TREE;
3856 /* Determines if the expression or type T uses any parameter packs. */
3857 bool
3858 uses_parameter_packs (tree t)
3860 tree parameter_packs = NULL_TREE;
3861 struct find_parameter_pack_data ppd;
3862 ppd.parameter_packs = &parameter_packs;
3863 ppd.visited = new hash_set<tree>;
3864 ppd.type_pack_expansion_p = false;
3865 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3866 delete ppd.visited;
3867 return parameter_packs != NULL_TREE;
3870 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3871 representation a base-class initializer into a parameter pack
3872 expansion. If all goes well, the resulting node will be an
3873 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3874 respectively. */
3875 tree
3876 make_pack_expansion (tree arg, tsubst_flags_t complain)
3878 tree result;
3879 tree parameter_packs = NULL_TREE;
3880 bool for_types = false;
3881 struct find_parameter_pack_data ppd;
3883 if (!arg || arg == error_mark_node)
3884 return arg;
3886 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
3888 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3889 class initializer. In this case, the TREE_PURPOSE will be a
3890 _TYPE node (representing the base class expansion we're
3891 initializing) and the TREE_VALUE will be a TREE_LIST
3892 containing the initialization arguments.
3894 The resulting expansion looks somewhat different from most
3895 expansions. Rather than returning just one _EXPANSION, we
3896 return a TREE_LIST whose TREE_PURPOSE is a
3897 TYPE_PACK_EXPANSION containing the bases that will be
3898 initialized. The TREE_VALUE will be identical to the
3899 original TREE_VALUE, which is a list of arguments that will
3900 be passed to each base. We do not introduce any new pack
3901 expansion nodes into the TREE_VALUE (although it is possible
3902 that some already exist), because the TREE_PURPOSE and
3903 TREE_VALUE all need to be expanded together with the same
3904 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3905 resulting TREE_PURPOSE will mention the parameter packs in
3906 both the bases and the arguments to the bases. */
3907 tree purpose;
3908 tree value;
3909 tree parameter_packs = NULL_TREE;
3911 /* Determine which parameter packs will be used by the base
3912 class expansion. */
3913 ppd.visited = new hash_set<tree>;
3914 ppd.parameter_packs = &parameter_packs;
3915 ppd.type_pack_expansion_p = true;
3916 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
3917 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3918 &ppd, ppd.visited);
3920 if (parameter_packs == NULL_TREE)
3922 if (complain & tf_error)
3923 error ("base initializer expansion %qT contains no parameter packs",
3924 arg);
3925 delete ppd.visited;
3926 return error_mark_node;
3929 if (TREE_VALUE (arg) != void_type_node)
3931 /* Collect the sets of parameter packs used in each of the
3932 initialization arguments. */
3933 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3935 /* Determine which parameter packs will be expanded in this
3936 argument. */
3937 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3938 &ppd, ppd.visited);
3942 delete ppd.visited;
3944 /* Create the pack expansion type for the base type. */
3945 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3946 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3947 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3948 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
3950 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3951 they will rarely be compared to anything. */
3952 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3954 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3957 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3958 for_types = true;
3960 /* Build the PACK_EXPANSION_* node. */
3961 result = for_types
3962 ? cxx_make_type (TYPE_PACK_EXPANSION)
3963 : make_node (EXPR_PACK_EXPANSION);
3964 SET_PACK_EXPANSION_PATTERN (result, arg);
3965 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3967 /* Propagate type and const-expression information. */
3968 TREE_TYPE (result) = TREE_TYPE (arg);
3969 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3970 /* Mark this read now, since the expansion might be length 0. */
3971 mark_exp_read (arg);
3973 else
3974 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3975 they will rarely be compared to anything. */
3976 SET_TYPE_STRUCTURAL_EQUALITY (result);
3978 /* Determine which parameter packs will be expanded. */
3979 ppd.parameter_packs = &parameter_packs;
3980 ppd.visited = new hash_set<tree>;
3981 ppd.type_pack_expansion_p = TYPE_P (arg);
3982 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3983 delete ppd.visited;
3985 /* Make sure we found some parameter packs. */
3986 if (parameter_packs == NULL_TREE)
3988 if (complain & tf_error)
3990 if (TYPE_P (arg))
3991 error ("expansion pattern %qT contains no argument packs", arg);
3992 else
3993 error ("expansion pattern %qE contains no argument packs", arg);
3995 return error_mark_node;
3997 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3999 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
4001 return result;
4004 /* Checks T for any "bare" parameter packs, which have not yet been
4005 expanded, and issues an error if any are found. This operation can
4006 only be done on full expressions or types (e.g., an expression
4007 statement, "if" condition, etc.), because we could have expressions like:
4009 foo(f(g(h(args)))...)
4011 where "args" is a parameter pack. check_for_bare_parameter_packs
4012 should not be called for the subexpressions args, h(args),
4013 g(h(args)), or f(g(h(args))), because we would produce erroneous
4014 error messages.
4016 Returns TRUE and emits an error if there were bare parameter packs,
4017 returns FALSE otherwise. */
4018 bool
4019 check_for_bare_parameter_packs (tree t)
4021 tree parameter_packs = NULL_TREE;
4022 struct find_parameter_pack_data ppd;
4024 if (!processing_template_decl || !t || t == error_mark_node)
4025 return false;
4027 /* A lambda might use a parameter pack from the containing context. */
4028 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
4029 return false;
4031 if (TREE_CODE (t) == TYPE_DECL)
4032 t = TREE_TYPE (t);
4034 ppd.parameter_packs = &parameter_packs;
4035 ppd.visited = new hash_set<tree>;
4036 ppd.type_pack_expansion_p = false;
4037 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4038 delete ppd.visited;
4040 if (parameter_packs)
4042 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
4043 error_at (loc, "parameter packs not expanded with %<...%>:");
4044 while (parameter_packs)
4046 tree pack = TREE_VALUE (parameter_packs);
4047 tree name = NULL_TREE;
4049 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
4050 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
4051 name = TYPE_NAME (pack);
4052 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
4053 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
4054 else if (TREE_CODE (pack) == CALL_EXPR)
4055 name = DECL_NAME (CALL_EXPR_FN (pack));
4056 else
4057 name = DECL_NAME (pack);
4059 if (name)
4060 inform (loc, " %qD", name);
4061 else
4062 inform (loc, " <anonymous>");
4064 parameter_packs = TREE_CHAIN (parameter_packs);
4067 return true;
4070 return false;
4073 /* Expand any parameter packs that occur in the template arguments in
4074 ARGS. */
4075 tree
4076 expand_template_argument_pack (tree args)
4078 if (args == error_mark_node)
4079 return error_mark_node;
4081 tree result_args = NULL_TREE;
4082 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
4083 int num_result_args = -1;
4084 int non_default_args_count = -1;
4086 /* First, determine if we need to expand anything, and the number of
4087 slots we'll need. */
4088 for (in_arg = 0; in_arg < nargs; ++in_arg)
4090 tree arg = TREE_VEC_ELT (args, in_arg);
4091 if (arg == NULL_TREE)
4092 return args;
4093 if (ARGUMENT_PACK_P (arg))
4095 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
4096 if (num_result_args < 0)
4097 num_result_args = in_arg + num_packed;
4098 else
4099 num_result_args += num_packed;
4101 else
4103 if (num_result_args >= 0)
4104 num_result_args++;
4108 /* If no expansion is necessary, we're done. */
4109 if (num_result_args < 0)
4110 return args;
4112 /* Expand arguments. */
4113 result_args = make_tree_vec (num_result_args);
4114 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
4115 non_default_args_count =
4116 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
4117 for (in_arg = 0; in_arg < nargs; ++in_arg)
4119 tree arg = TREE_VEC_ELT (args, in_arg);
4120 if (ARGUMENT_PACK_P (arg))
4122 tree packed = ARGUMENT_PACK_ARGS (arg);
4123 int i, num_packed = TREE_VEC_LENGTH (packed);
4124 for (i = 0; i < num_packed; ++i, ++out_arg)
4125 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
4126 if (non_default_args_count > 0)
4127 non_default_args_count += num_packed - 1;
4129 else
4131 TREE_VEC_ELT (result_args, out_arg) = arg;
4132 ++out_arg;
4135 if (non_default_args_count >= 0)
4136 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
4137 return result_args;
4140 /* Checks if DECL shadows a template parameter.
4142 [temp.local]: A template-parameter shall not be redeclared within its
4143 scope (including nested scopes).
4145 Emits an error and returns TRUE if the DECL shadows a parameter,
4146 returns FALSE otherwise. */
4148 bool
4149 check_template_shadow (tree decl)
4151 tree olddecl;
4153 /* If we're not in a template, we can't possibly shadow a template
4154 parameter. */
4155 if (!current_template_parms)
4156 return true;
4158 /* Figure out what we're shadowing. */
4159 decl = OVL_FIRST (decl);
4160 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4162 /* If there's no previous binding for this name, we're not shadowing
4163 anything, let alone a template parameter. */
4164 if (!olddecl)
4165 return true;
4167 /* If we're not shadowing a template parameter, we're done. Note
4168 that OLDDECL might be an OVERLOAD (or perhaps even an
4169 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4170 node. */
4171 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4172 return true;
4174 /* We check for decl != olddecl to avoid bogus errors for using a
4175 name inside a class. We check TPFI to avoid duplicate errors for
4176 inline member templates. */
4177 if (decl == olddecl
4178 || (DECL_TEMPLATE_PARM_P (decl)
4179 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4180 return true;
4182 /* Don't complain about the injected class name, as we've already
4183 complained about the class itself. */
4184 if (DECL_SELF_REFERENCE_P (decl))
4185 return false;
4187 if (DECL_TEMPLATE_PARM_P (decl))
4188 error ("declaration of template parameter %q+D shadows "
4189 "template parameter", decl);
4190 else
4191 error ("declaration of %q+#D shadows template parameter", decl);
4192 inform (DECL_SOURCE_LOCATION (olddecl),
4193 "template parameter %qD declared here", olddecl);
4194 return false;
4197 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4198 ORIG_LEVEL, DECL, and TYPE. */
4200 static tree
4201 build_template_parm_index (int index,
4202 int level,
4203 int orig_level,
4204 tree decl,
4205 tree type)
4207 tree t = make_node (TEMPLATE_PARM_INDEX);
4208 TEMPLATE_PARM_IDX (t) = index;
4209 TEMPLATE_PARM_LEVEL (t) = level;
4210 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4211 TEMPLATE_PARM_DECL (t) = decl;
4212 TREE_TYPE (t) = type;
4213 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4214 TREE_READONLY (t) = TREE_READONLY (decl);
4216 return t;
4219 /* Find the canonical type parameter for the given template type
4220 parameter. Returns the canonical type parameter, which may be TYPE
4221 if no such parameter existed. */
4223 static tree
4224 canonical_type_parameter (tree type)
4226 tree list;
4227 int idx = TEMPLATE_TYPE_IDX (type);
4228 if (!canonical_template_parms)
4229 vec_alloc (canonical_template_parms, idx + 1);
4231 if (canonical_template_parms->length () <= (unsigned) idx)
4232 vec_safe_grow_cleared (canonical_template_parms, idx + 1);
4234 list = (*canonical_template_parms)[idx];
4235 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4236 list = TREE_CHAIN (list);
4238 if (list)
4239 return TREE_VALUE (list);
4240 else
4242 (*canonical_template_parms)[idx]
4243 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4244 return type;
4248 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4249 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4250 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4251 new one is created. */
4253 static tree
4254 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4255 tsubst_flags_t complain)
4257 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4258 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4259 != TEMPLATE_PARM_LEVEL (index) - levels)
4260 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4262 tree orig_decl = TEMPLATE_PARM_DECL (index);
4263 tree decl, t;
4265 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4266 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
4267 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4268 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4269 DECL_ARTIFICIAL (decl) = 1;
4270 SET_DECL_TEMPLATE_PARM_P (decl);
4272 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4273 TEMPLATE_PARM_LEVEL (index) - levels,
4274 TEMPLATE_PARM_ORIG_LEVEL (index),
4275 decl, type);
4276 TEMPLATE_PARM_DESCENDANTS (index) = t;
4277 TEMPLATE_PARM_PARAMETER_PACK (t)
4278 = TEMPLATE_PARM_PARAMETER_PACK (index);
4280 /* Template template parameters need this. */
4281 if (TREE_CODE (decl) == TEMPLATE_DECL)
4283 DECL_TEMPLATE_RESULT (decl)
4284 = build_decl (DECL_SOURCE_LOCATION (decl),
4285 TYPE_DECL, DECL_NAME (decl), type);
4286 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl)) = true;
4287 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4288 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4292 return TEMPLATE_PARM_DESCENDANTS (index);
4295 /* Process information from new template parameter PARM and append it
4296 to the LIST being built. This new parameter is a non-type
4297 parameter iff IS_NON_TYPE is true. This new parameter is a
4298 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4299 is in PARM_LOC. */
4301 tree
4302 process_template_parm (tree list, location_t parm_loc, tree parm,
4303 bool is_non_type, bool is_parameter_pack)
4305 tree decl = 0;
4306 int idx = 0;
4308 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4309 tree defval = TREE_PURPOSE (parm);
4310 tree constr = TREE_TYPE (parm);
4312 if (list)
4314 tree p = tree_last (list);
4316 if (p && TREE_VALUE (p) != error_mark_node)
4318 p = TREE_VALUE (p);
4319 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4320 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4321 else
4322 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4325 ++idx;
4328 if (is_non_type)
4330 parm = TREE_VALUE (parm);
4332 SET_DECL_TEMPLATE_PARM_P (parm);
4334 if (TREE_TYPE (parm) != error_mark_node)
4336 /* [temp.param]
4338 The top-level cv-qualifiers on the template-parameter are
4339 ignored when determining its type. */
4340 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4341 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4342 TREE_TYPE (parm) = error_mark_node;
4343 else if (uses_parameter_packs (TREE_TYPE (parm))
4344 && !is_parameter_pack
4345 /* If we're in a nested template parameter list, the template
4346 template parameter could be a parameter pack. */
4347 && processing_template_parmlist == 1)
4349 /* This template parameter is not a parameter pack, but it
4350 should be. Complain about "bare" parameter packs. */
4351 check_for_bare_parameter_packs (TREE_TYPE (parm));
4353 /* Recover by calling this a parameter pack. */
4354 is_parameter_pack = true;
4358 /* A template parameter is not modifiable. */
4359 TREE_CONSTANT (parm) = 1;
4360 TREE_READONLY (parm) = 1;
4361 decl = build_decl (parm_loc,
4362 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4363 TREE_CONSTANT (decl) = 1;
4364 TREE_READONLY (decl) = 1;
4365 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4366 = build_template_parm_index (idx, processing_template_decl,
4367 processing_template_decl,
4368 decl, TREE_TYPE (parm));
4370 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4371 = is_parameter_pack;
4373 else
4375 tree t;
4376 parm = TREE_VALUE (TREE_VALUE (parm));
4378 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4380 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4381 /* This is for distinguishing between real templates and template
4382 template parameters */
4383 TREE_TYPE (parm) = t;
4384 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4385 decl = parm;
4387 else
4389 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4390 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4391 decl = build_decl (parm_loc,
4392 TYPE_DECL, parm, t);
4395 TYPE_NAME (t) = decl;
4396 TYPE_STUB_DECL (t) = decl;
4397 parm = decl;
4398 TEMPLATE_TYPE_PARM_INDEX (t)
4399 = build_template_parm_index (idx, processing_template_decl,
4400 processing_template_decl,
4401 decl, TREE_TYPE (parm));
4402 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4403 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4405 DECL_ARTIFICIAL (decl) = 1;
4406 SET_DECL_TEMPLATE_PARM_P (decl);
4408 /* Build requirements for the type/template parameter.
4409 This must be done after SET_DECL_TEMPLATE_PARM_P or
4410 process_template_parm could fail. */
4411 tree reqs = finish_shorthand_constraint (parm, constr);
4413 pushdecl (decl);
4415 /* Build the parameter node linking the parameter declaration,
4416 its default argument (if any), and its constraints (if any). */
4417 parm = build_tree_list (defval, parm);
4418 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4420 return chainon (list, parm);
4423 /* The end of a template parameter list has been reached. Process the
4424 tree list into a parameter vector, converting each parameter into a more
4425 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4426 as PARM_DECLs. */
4428 tree
4429 end_template_parm_list (tree parms)
4431 int nparms;
4432 tree parm, next;
4433 tree saved_parmlist = make_tree_vec (list_length (parms));
4435 /* Pop the dummy parameter level and add the real one. */
4436 current_template_parms = TREE_CHAIN (current_template_parms);
4438 current_template_parms
4439 = tree_cons (size_int (processing_template_decl),
4440 saved_parmlist, current_template_parms);
4442 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4444 next = TREE_CHAIN (parm);
4445 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4446 TREE_CHAIN (parm) = NULL_TREE;
4449 --processing_template_parmlist;
4451 return saved_parmlist;
4454 // Explicitly indicate the end of the template parameter list. We assume
4455 // that the current template parameters have been constructed and/or
4456 // managed explicitly, as when creating new template template parameters
4457 // from a shorthand constraint.
4458 void
4459 end_template_parm_list ()
4461 --processing_template_parmlist;
4464 /* end_template_decl is called after a template declaration is seen. */
4466 void
4467 end_template_decl (void)
4469 reset_specialization ();
4471 if (! processing_template_decl)
4472 return;
4474 /* This matches the pushlevel in begin_template_parm_list. */
4475 finish_scope ();
4477 --processing_template_decl;
4478 current_template_parms = TREE_CHAIN (current_template_parms);
4481 /* Takes a TREE_LIST representing a template parameter and convert it
4482 into an argument suitable to be passed to the type substitution
4483 functions. Note that If the TREE_LIST contains an error_mark
4484 node, the returned argument is error_mark_node. */
4486 tree
4487 template_parm_to_arg (tree t)
4490 if (t == NULL_TREE
4491 || TREE_CODE (t) != TREE_LIST)
4492 return t;
4494 if (error_operand_p (TREE_VALUE (t)))
4495 return error_mark_node;
4497 t = TREE_VALUE (t);
4499 if (TREE_CODE (t) == TYPE_DECL
4500 || TREE_CODE (t) == TEMPLATE_DECL)
4502 t = TREE_TYPE (t);
4504 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4506 /* Turn this argument into a TYPE_ARGUMENT_PACK
4507 with a single element, which expands T. */
4508 tree vec = make_tree_vec (1);
4509 if (CHECKING_P)
4510 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4512 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4514 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4515 SET_ARGUMENT_PACK_ARGS (t, vec);
4518 else
4520 t = DECL_INITIAL (t);
4522 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4524 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4525 with a single element, which expands T. */
4526 tree vec = make_tree_vec (1);
4527 if (CHECKING_P)
4528 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4530 t = convert_from_reference (t);
4531 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4533 t = make_node (NONTYPE_ARGUMENT_PACK);
4534 SET_ARGUMENT_PACK_ARGS (t, vec);
4536 else
4537 t = convert_from_reference (t);
4539 return t;
4542 /* Given a single level of template parameters (a TREE_VEC), return it
4543 as a set of template arguments. */
4545 static tree
4546 template_parms_level_to_args (tree parms)
4548 tree a = copy_node (parms);
4549 TREE_TYPE (a) = NULL_TREE;
4550 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4551 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4553 if (CHECKING_P)
4554 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4556 return a;
4559 /* Given a set of template parameters, return them as a set of template
4560 arguments. The template parameters are represented as a TREE_VEC, in
4561 the form documented in cp-tree.h for template arguments. */
4563 static tree
4564 template_parms_to_args (tree parms)
4566 tree header;
4567 tree args = NULL_TREE;
4568 int length = TMPL_PARMS_DEPTH (parms);
4569 int l = length;
4571 /* If there is only one level of template parameters, we do not
4572 create a TREE_VEC of TREE_VECs. Instead, we return a single
4573 TREE_VEC containing the arguments. */
4574 if (length > 1)
4575 args = make_tree_vec (length);
4577 for (header = parms; header; header = TREE_CHAIN (header))
4579 tree a = template_parms_level_to_args (TREE_VALUE (header));
4581 if (length > 1)
4582 TREE_VEC_ELT (args, --l) = a;
4583 else
4584 args = a;
4587 return args;
4590 /* Within the declaration of a template, return the currently active
4591 template parameters as an argument TREE_VEC. */
4593 static tree
4594 current_template_args (void)
4596 return template_parms_to_args (current_template_parms);
4599 /* Update the declared TYPE by doing any lookups which were thought to be
4600 dependent, but are not now that we know the SCOPE of the declarator. */
4602 tree
4603 maybe_update_decl_type (tree orig_type, tree scope)
4605 tree type = orig_type;
4607 if (type == NULL_TREE)
4608 return type;
4610 if (TREE_CODE (orig_type) == TYPE_DECL)
4611 type = TREE_TYPE (type);
4613 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4614 && dependent_type_p (type)
4615 /* Don't bother building up the args in this case. */
4616 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4618 /* tsubst in the args corresponding to the template parameters,
4619 including auto if present. Most things will be unchanged, but
4620 make_typename_type and tsubst_qualified_id will resolve
4621 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4622 tree args = current_template_args ();
4623 tree auto_node = type_uses_auto (type);
4624 tree pushed;
4625 if (auto_node)
4627 tree auto_vec = make_tree_vec (1);
4628 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4629 args = add_to_template_args (args, auto_vec);
4631 pushed = push_scope (scope);
4632 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4633 if (pushed)
4634 pop_scope (scope);
4637 if (type == error_mark_node)
4638 return orig_type;
4640 if (TREE_CODE (orig_type) == TYPE_DECL)
4642 if (same_type_p (type, TREE_TYPE (orig_type)))
4643 type = orig_type;
4644 else
4645 type = TYPE_NAME (type);
4647 return type;
4650 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4651 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4652 the new template is a member template. */
4654 tree
4655 build_template_decl (tree decl, tree parms, bool member_template_p)
4657 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4658 DECL_TEMPLATE_PARMS (tmpl) = parms;
4659 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4660 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4661 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4663 return tmpl;
4666 struct template_parm_data
4668 /* The level of the template parameters we are currently
4669 processing. */
4670 int level;
4672 /* The index of the specialization argument we are currently
4673 processing. */
4674 int current_arg;
4676 /* An array whose size is the number of template parameters. The
4677 elements are nonzero if the parameter has been used in any one
4678 of the arguments processed so far. */
4679 int* parms;
4681 /* An array whose size is the number of template arguments. The
4682 elements are nonzero if the argument makes use of template
4683 parameters of this level. */
4684 int* arg_uses_template_parms;
4687 /* Subroutine of push_template_decl used to see if each template
4688 parameter in a partial specialization is used in the explicit
4689 argument list. If T is of the LEVEL given in DATA (which is
4690 treated as a template_parm_data*), then DATA->PARMS is marked
4691 appropriately. */
4693 static int
4694 mark_template_parm (tree t, void* data)
4696 int level;
4697 int idx;
4698 struct template_parm_data* tpd = (struct template_parm_data*) data;
4700 template_parm_level_and_index (t, &level, &idx);
4702 if (level == tpd->level)
4704 tpd->parms[idx] = 1;
4705 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4708 /* In C++17 the type of a non-type argument is a deduced context. */
4709 if (cxx_dialect >= cxx17
4710 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4711 for_each_template_parm (TREE_TYPE (t),
4712 &mark_template_parm,
4713 data,
4714 NULL,
4715 /*include_nondeduced_p=*/false);
4717 /* Return zero so that for_each_template_parm will continue the
4718 traversal of the tree; we want to mark *every* template parm. */
4719 return 0;
4722 /* Process the partial specialization DECL. */
4724 static tree
4725 process_partial_specialization (tree decl)
4727 tree type = TREE_TYPE (decl);
4728 tree tinfo = get_template_info (decl);
4729 tree maintmpl = TI_TEMPLATE (tinfo);
4730 tree specargs = TI_ARGS (tinfo);
4731 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4732 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4733 tree inner_parms;
4734 tree inst;
4735 int nargs = TREE_VEC_LENGTH (inner_args);
4736 int ntparms;
4737 int i;
4738 bool did_error_intro = false;
4739 struct template_parm_data tpd;
4740 struct template_parm_data tpd2;
4742 gcc_assert (current_template_parms);
4744 /* A concept cannot be specialized. */
4745 if (flag_concepts && variable_concept_p (maintmpl))
4747 error ("specialization of variable concept %q#D", maintmpl);
4748 return error_mark_node;
4751 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4752 ntparms = TREE_VEC_LENGTH (inner_parms);
4754 /* We check that each of the template parameters given in the
4755 partial specialization is used in the argument list to the
4756 specialization. For example:
4758 template <class T> struct S;
4759 template <class T> struct S<T*>;
4761 The second declaration is OK because `T*' uses the template
4762 parameter T, whereas
4764 template <class T> struct S<int>;
4766 is no good. Even trickier is:
4768 template <class T>
4769 struct S1
4771 template <class U>
4772 struct S2;
4773 template <class U>
4774 struct S2<T>;
4777 The S2<T> declaration is actually invalid; it is a
4778 full-specialization. Of course,
4780 template <class U>
4781 struct S2<T (*)(U)>;
4783 or some such would have been OK. */
4784 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4785 tpd.parms = XALLOCAVEC (int, ntparms);
4786 memset (tpd.parms, 0, sizeof (int) * ntparms);
4788 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4789 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4790 for (i = 0; i < nargs; ++i)
4792 tpd.current_arg = i;
4793 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4794 &mark_template_parm,
4795 &tpd,
4796 NULL,
4797 /*include_nondeduced_p=*/false);
4799 for (i = 0; i < ntparms; ++i)
4800 if (tpd.parms[i] == 0)
4802 /* One of the template parms was not used in a deduced context in the
4803 specialization. */
4804 if (!did_error_intro)
4806 error ("template parameters not deducible in "
4807 "partial specialization:");
4808 did_error_intro = true;
4811 inform (input_location, " %qD",
4812 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4815 if (did_error_intro)
4816 return error_mark_node;
4818 /* [temp.class.spec]
4820 The argument list of the specialization shall not be identical to
4821 the implicit argument list of the primary template. */
4822 tree main_args
4823 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4824 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
4825 && (!flag_concepts
4826 || !strictly_subsumes (current_template_constraints (),
4827 get_constraints (maintmpl))))
4829 if (!flag_concepts)
4830 error ("partial specialization %q+D does not specialize "
4831 "any template arguments", decl);
4832 else
4833 error ("partial specialization %q+D does not specialize any "
4834 "template arguments and is not more constrained than", decl);
4835 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4838 /* A partial specialization that replaces multiple parameters of the
4839 primary template with a pack expansion is less specialized for those
4840 parameters. */
4841 if (nargs < DECL_NTPARMS (maintmpl))
4843 error ("partial specialization is not more specialized than the "
4844 "primary template because it replaces multiple parameters "
4845 "with a pack expansion");
4846 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4847 /* Avoid crash in process_partial_specialization. */
4848 return decl;
4851 /* If we aren't in a dependent class, we can actually try deduction. */
4852 else if (tpd.level == 1
4853 /* FIXME we should be able to handle a partial specialization of a
4854 partial instantiation, but currently we can't (c++/41727). */
4855 && TMPL_ARGS_DEPTH (specargs) == 1
4856 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
4858 if (permerror (input_location, "partial specialization %qD is not "
4859 "more specialized than", decl))
4860 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
4861 maintmpl);
4864 /* [temp.class.spec]
4866 A partially specialized non-type argument expression shall not
4867 involve template parameters of the partial specialization except
4868 when the argument expression is a simple identifier.
4870 The type of a template parameter corresponding to a specialized
4871 non-type argument shall not be dependent on a parameter of the
4872 specialization.
4874 Also, we verify that pack expansions only occur at the
4875 end of the argument list. */
4876 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4877 tpd2.parms = 0;
4878 for (i = 0; i < nargs; ++i)
4880 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4881 tree arg = TREE_VEC_ELT (inner_args, i);
4882 tree packed_args = NULL_TREE;
4883 int j, len = 1;
4885 if (ARGUMENT_PACK_P (arg))
4887 /* Extract the arguments from the argument pack. We'll be
4888 iterating over these in the following loop. */
4889 packed_args = ARGUMENT_PACK_ARGS (arg);
4890 len = TREE_VEC_LENGTH (packed_args);
4893 for (j = 0; j < len; j++)
4895 if (packed_args)
4896 /* Get the Jth argument in the parameter pack. */
4897 arg = TREE_VEC_ELT (packed_args, j);
4899 if (PACK_EXPANSION_P (arg))
4901 /* Pack expansions must come at the end of the
4902 argument list. */
4903 if ((packed_args && j < len - 1)
4904 || (!packed_args && i < nargs - 1))
4906 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4907 error ("parameter pack argument %qE must be at the "
4908 "end of the template argument list", arg);
4909 else
4910 error ("parameter pack argument %qT must be at the "
4911 "end of the template argument list", arg);
4915 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4916 /* We only care about the pattern. */
4917 arg = PACK_EXPANSION_PATTERN (arg);
4919 if (/* These first two lines are the `non-type' bit. */
4920 !TYPE_P (arg)
4921 && TREE_CODE (arg) != TEMPLATE_DECL
4922 /* This next two lines are the `argument expression is not just a
4923 simple identifier' condition and also the `specialized
4924 non-type argument' bit. */
4925 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4926 && !(REFERENCE_REF_P (arg)
4927 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4929 if ((!packed_args && tpd.arg_uses_template_parms[i])
4930 || (packed_args && uses_template_parms (arg)))
4931 error ("template argument %qE involves template parameter(s)",
4932 arg);
4933 else
4935 /* Look at the corresponding template parameter,
4936 marking which template parameters its type depends
4937 upon. */
4938 tree type = TREE_TYPE (parm);
4940 if (!tpd2.parms)
4942 /* We haven't yet initialized TPD2. Do so now. */
4943 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4944 /* The number of parameters here is the number in the
4945 main template, which, as checked in the assertion
4946 above, is NARGS. */
4947 tpd2.parms = XALLOCAVEC (int, nargs);
4948 tpd2.level =
4949 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4952 /* Mark the template parameters. But this time, we're
4953 looking for the template parameters of the main
4954 template, not in the specialization. */
4955 tpd2.current_arg = i;
4956 tpd2.arg_uses_template_parms[i] = 0;
4957 memset (tpd2.parms, 0, sizeof (int) * nargs);
4958 for_each_template_parm (type,
4959 &mark_template_parm,
4960 &tpd2,
4961 NULL,
4962 /*include_nondeduced_p=*/false);
4964 if (tpd2.arg_uses_template_parms [i])
4966 /* The type depended on some template parameters.
4967 If they are fully specialized in the
4968 specialization, that's OK. */
4969 int j;
4970 int count = 0;
4971 for (j = 0; j < nargs; ++j)
4972 if (tpd2.parms[j] != 0
4973 && tpd.arg_uses_template_parms [j])
4974 ++count;
4975 if (count != 0)
4976 error_n (input_location, count,
4977 "type %qT of template argument %qE depends "
4978 "on a template parameter",
4979 "type %qT of template argument %qE depends "
4980 "on template parameters",
4981 type,
4982 arg);
4989 /* We should only get here once. */
4990 if (TREE_CODE (decl) == TYPE_DECL)
4991 gcc_assert (!COMPLETE_TYPE_P (type));
4993 // Build the template decl.
4994 tree tmpl = build_template_decl (decl, current_template_parms,
4995 DECL_MEMBER_TEMPLATE_P (maintmpl));
4996 TREE_TYPE (tmpl) = type;
4997 DECL_TEMPLATE_RESULT (tmpl) = decl;
4998 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4999 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
5000 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
5002 /* Give template template parms a DECL_CONTEXT of the template
5003 for which they are a parameter. */
5004 for (i = 0; i < ntparms; ++i)
5006 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
5007 if (TREE_CODE (parm) == TEMPLATE_DECL)
5008 DECL_CONTEXT (parm) = tmpl;
5011 if (VAR_P (decl))
5012 /* We didn't register this in check_explicit_specialization so we could
5013 wait until the constraints were set. */
5014 decl = register_specialization (decl, maintmpl, specargs, false, 0);
5015 else
5016 associate_classtype_constraints (type);
5018 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
5019 = tree_cons (specargs, tmpl,
5020 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
5021 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
5023 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
5024 inst = TREE_CHAIN (inst))
5026 tree instance = TREE_VALUE (inst);
5027 if (TYPE_P (instance)
5028 ? (COMPLETE_TYPE_P (instance)
5029 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
5030 : DECL_TEMPLATE_INSTANTIATION (instance))
5032 tree spec = most_specialized_partial_spec (instance, tf_none);
5033 tree inst_decl = (DECL_P (instance)
5034 ? instance : TYPE_NAME (instance));
5035 if (!spec)
5036 /* OK */;
5037 else if (spec == error_mark_node)
5038 permerror (input_location,
5039 "declaration of %qD ambiguates earlier template "
5040 "instantiation for %qD", decl, inst_decl);
5041 else if (TREE_VALUE (spec) == tmpl)
5042 permerror (input_location,
5043 "partial specialization of %qD after instantiation "
5044 "of %qD", decl, inst_decl);
5048 return decl;
5051 /* PARM is a template parameter of some form; return the corresponding
5052 TEMPLATE_PARM_INDEX. */
5054 static tree
5055 get_template_parm_index (tree parm)
5057 if (TREE_CODE (parm) == PARM_DECL
5058 || TREE_CODE (parm) == CONST_DECL)
5059 parm = DECL_INITIAL (parm);
5060 else if (TREE_CODE (parm) == TYPE_DECL
5061 || TREE_CODE (parm) == TEMPLATE_DECL)
5062 parm = TREE_TYPE (parm);
5063 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
5064 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
5065 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
5066 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
5067 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
5068 return parm;
5071 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5072 parameter packs used by the template parameter PARM. */
5074 static void
5075 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
5077 /* A type parm can't refer to another parm. */
5078 if (TREE_CODE (parm) == TYPE_DECL)
5079 return;
5080 else if (TREE_CODE (parm) == PARM_DECL)
5082 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
5083 ppd, ppd->visited);
5084 return;
5087 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
5089 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
5090 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
5091 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
5094 /* PARM is a template parameter pack. Return any parameter packs used in
5095 its type or the type of any of its template parameters. If there are
5096 any such packs, it will be instantiated into a fixed template parameter
5097 list by partial instantiation rather than be fully deduced. */
5099 tree
5100 fixed_parameter_pack_p (tree parm)
5102 /* This can only be true in a member template. */
5103 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
5104 return NULL_TREE;
5105 /* This can only be true for a parameter pack. */
5106 if (!template_parameter_pack_p (parm))
5107 return NULL_TREE;
5108 /* A type parm can't refer to another parm. */
5109 if (TREE_CODE (parm) == TYPE_DECL)
5110 return NULL_TREE;
5112 tree parameter_packs = NULL_TREE;
5113 struct find_parameter_pack_data ppd;
5114 ppd.parameter_packs = &parameter_packs;
5115 ppd.visited = new hash_set<tree>;
5116 ppd.type_pack_expansion_p = false;
5118 fixed_parameter_pack_p_1 (parm, &ppd);
5120 delete ppd.visited;
5121 return parameter_packs;
5124 /* Check that a template declaration's use of default arguments and
5125 parameter packs is not invalid. Here, PARMS are the template
5126 parameters. IS_PRIMARY is true if DECL is the thing declared by
5127 a primary template. IS_PARTIAL is true if DECL is a partial
5128 specialization.
5130 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5131 function template declaration or a friend class template
5132 declaration. In the function case, 1 indicates a declaration, 2
5133 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5134 emitted for extraneous default arguments.
5136 Returns TRUE if there were no errors found, FALSE otherwise. */
5138 bool
5139 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
5140 bool is_partial, int is_friend_decl)
5142 const char *msg;
5143 int last_level_to_check;
5144 tree parm_level;
5145 bool no_errors = true;
5147 /* [temp.param]
5149 A default template-argument shall not be specified in a
5150 function template declaration or a function template definition, nor
5151 in the template-parameter-list of the definition of a member of a
5152 class template. */
5154 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5155 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
5156 /* You can't have a function template declaration in a local
5157 scope, nor you can you define a member of a class template in a
5158 local scope. */
5159 return true;
5161 if ((TREE_CODE (decl) == TYPE_DECL
5162 && TREE_TYPE (decl)
5163 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5164 || (TREE_CODE (decl) == FUNCTION_DECL
5165 && LAMBDA_FUNCTION_P (decl)))
5166 /* A lambda doesn't have an explicit declaration; don't complain
5167 about the parms of the enclosing class. */
5168 return true;
5170 if (current_class_type
5171 && !TYPE_BEING_DEFINED (current_class_type)
5172 && DECL_LANG_SPECIFIC (decl)
5173 && DECL_DECLARES_FUNCTION_P (decl)
5174 /* If this is either a friend defined in the scope of the class
5175 or a member function. */
5176 && (DECL_FUNCTION_MEMBER_P (decl)
5177 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5178 : DECL_FRIEND_CONTEXT (decl)
5179 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5180 : false)
5181 /* And, if it was a member function, it really was defined in
5182 the scope of the class. */
5183 && (!DECL_FUNCTION_MEMBER_P (decl)
5184 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5185 /* We already checked these parameters when the template was
5186 declared, so there's no need to do it again now. This function
5187 was defined in class scope, but we're processing its body now
5188 that the class is complete. */
5189 return true;
5191 /* Core issue 226 (C++0x only): the following only applies to class
5192 templates. */
5193 if (is_primary
5194 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5196 /* [temp.param]
5198 If a template-parameter has a default template-argument, all
5199 subsequent template-parameters shall have a default
5200 template-argument supplied. */
5201 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5203 tree inner_parms = TREE_VALUE (parm_level);
5204 int ntparms = TREE_VEC_LENGTH (inner_parms);
5205 int seen_def_arg_p = 0;
5206 int i;
5208 for (i = 0; i < ntparms; ++i)
5210 tree parm = TREE_VEC_ELT (inner_parms, i);
5212 if (parm == error_mark_node)
5213 continue;
5215 if (TREE_PURPOSE (parm))
5216 seen_def_arg_p = 1;
5217 else if (seen_def_arg_p
5218 && !template_parameter_pack_p (TREE_VALUE (parm)))
5220 error ("no default argument for %qD", TREE_VALUE (parm));
5221 /* For better subsequent error-recovery, we indicate that
5222 there should have been a default argument. */
5223 TREE_PURPOSE (parm) = error_mark_node;
5224 no_errors = false;
5226 else if (!is_partial
5227 && !is_friend_decl
5228 /* Don't complain about an enclosing partial
5229 specialization. */
5230 && parm_level == parms
5231 && TREE_CODE (decl) == TYPE_DECL
5232 && i < ntparms - 1
5233 && template_parameter_pack_p (TREE_VALUE (parm))
5234 /* A fixed parameter pack will be partially
5235 instantiated into a fixed length list. */
5236 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5238 /* A primary class template can only have one
5239 parameter pack, at the end of the template
5240 parameter list. */
5242 error ("parameter pack %q+D must be at the end of the"
5243 " template parameter list", TREE_VALUE (parm));
5245 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5246 = error_mark_node;
5247 no_errors = false;
5253 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5254 || is_partial
5255 || !is_primary
5256 || is_friend_decl)
5257 /* For an ordinary class template, default template arguments are
5258 allowed at the innermost level, e.g.:
5259 template <class T = int>
5260 struct S {};
5261 but, in a partial specialization, they're not allowed even
5262 there, as we have in [temp.class.spec]:
5264 The template parameter list of a specialization shall not
5265 contain default template argument values.
5267 So, for a partial specialization, or for a function template
5268 (in C++98/C++03), we look at all of them. */
5270 else
5271 /* But, for a primary class template that is not a partial
5272 specialization we look at all template parameters except the
5273 innermost ones. */
5274 parms = TREE_CHAIN (parms);
5276 /* Figure out what error message to issue. */
5277 if (is_friend_decl == 2)
5278 msg = G_("default template arguments may not be used in function template "
5279 "friend re-declaration");
5280 else if (is_friend_decl)
5281 msg = G_("default template arguments may not be used in template "
5282 "friend declarations");
5283 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5284 msg = G_("default template arguments may not be used in function templates "
5285 "without -std=c++11 or -std=gnu++11");
5286 else if (is_partial)
5287 msg = G_("default template arguments may not be used in "
5288 "partial specializations");
5289 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5290 msg = G_("default argument for template parameter for class enclosing %qD");
5291 else
5292 /* Per [temp.param]/9, "A default template-argument shall not be
5293 specified in the template-parameter-lists of the definition of
5294 a member of a class template that appears outside of the member's
5295 class.", thus if we aren't handling a member of a class template
5296 there is no need to examine the parameters. */
5297 return true;
5299 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5300 /* If we're inside a class definition, there's no need to
5301 examine the parameters to the class itself. On the one
5302 hand, they will be checked when the class is defined, and,
5303 on the other, default arguments are valid in things like:
5304 template <class T = double>
5305 struct S { template <class U> void f(U); };
5306 Here the default argument for `S' has no bearing on the
5307 declaration of `f'. */
5308 last_level_to_check = template_class_depth (current_class_type) + 1;
5309 else
5310 /* Check everything. */
5311 last_level_to_check = 0;
5313 for (parm_level = parms;
5314 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5315 parm_level = TREE_CHAIN (parm_level))
5317 tree inner_parms = TREE_VALUE (parm_level);
5318 int i;
5319 int ntparms;
5321 ntparms = TREE_VEC_LENGTH (inner_parms);
5322 for (i = 0; i < ntparms; ++i)
5324 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5325 continue;
5327 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5329 if (msg)
5331 no_errors = false;
5332 if (is_friend_decl == 2)
5333 return no_errors;
5335 error (msg, decl);
5336 msg = 0;
5339 /* Clear out the default argument so that we are not
5340 confused later. */
5341 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5345 /* At this point, if we're still interested in issuing messages,
5346 they must apply to classes surrounding the object declared. */
5347 if (msg)
5348 msg = G_("default argument for template parameter for class "
5349 "enclosing %qD");
5352 return no_errors;
5355 /* Worker for push_template_decl_real, called via
5356 for_each_template_parm. DATA is really an int, indicating the
5357 level of the parameters we are interested in. If T is a template
5358 parameter of that level, return nonzero. */
5360 static int
5361 template_parm_this_level_p (tree t, void* data)
5363 int this_level = *(int *)data;
5364 int level;
5366 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5367 level = TEMPLATE_PARM_LEVEL (t);
5368 else
5369 level = TEMPLATE_TYPE_LEVEL (t);
5370 return level == this_level;
5373 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5374 DATA is really an int, indicating the innermost outer level of parameters.
5375 If T is a template parameter of that level or further out, return
5376 nonzero. */
5378 static int
5379 template_parm_outer_level (tree t, void *data)
5381 int this_level = *(int *)data;
5382 int level;
5384 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5385 level = TEMPLATE_PARM_LEVEL (t);
5386 else
5387 level = TEMPLATE_TYPE_LEVEL (t);
5388 return level <= this_level;
5391 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5392 parameters given by current_template_args, or reuses a
5393 previously existing one, if appropriate. Returns the DECL, or an
5394 equivalent one, if it is replaced via a call to duplicate_decls.
5396 If IS_FRIEND is true, DECL is a friend declaration. */
5398 tree
5399 push_template_decl_real (tree decl, bool is_friend)
5401 tree tmpl;
5402 tree args;
5403 tree info;
5404 tree ctx;
5405 bool is_primary;
5406 bool is_partial;
5407 int new_template_p = 0;
5408 /* True if the template is a member template, in the sense of
5409 [temp.mem]. */
5410 bool member_template_p = false;
5412 if (decl == error_mark_node || !current_template_parms)
5413 return error_mark_node;
5415 /* See if this is a partial specialization. */
5416 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5417 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5418 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5419 || (VAR_P (decl)
5420 && DECL_LANG_SPECIFIC (decl)
5421 && DECL_TEMPLATE_SPECIALIZATION (decl)
5422 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5424 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5425 is_friend = true;
5427 if (is_friend)
5428 /* For a friend, we want the context of the friend, not
5429 the type of which it is a friend. */
5430 ctx = CP_DECL_CONTEXT (decl);
5431 else if (CP_DECL_CONTEXT (decl)
5432 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5433 /* In the case of a virtual function, we want the class in which
5434 it is defined. */
5435 ctx = CP_DECL_CONTEXT (decl);
5436 else
5437 /* Otherwise, if we're currently defining some class, the DECL
5438 is assumed to be a member of the class. */
5439 ctx = current_scope ();
5441 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5442 ctx = NULL_TREE;
5444 if (!DECL_CONTEXT (decl))
5445 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5447 /* See if this is a primary template. */
5448 if (is_friend && ctx
5449 && uses_template_parms_level (ctx, processing_template_decl))
5450 /* A friend template that specifies a class context, i.e.
5451 template <typename T> friend void A<T>::f();
5452 is not primary. */
5453 is_primary = false;
5454 else if (TREE_CODE (decl) == TYPE_DECL
5455 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5456 is_primary = false;
5457 else
5458 is_primary = template_parm_scope_p ();
5460 if (is_primary)
5462 warning (OPT_Wtemplates, "template %qD declared", decl);
5464 if (DECL_CLASS_SCOPE_P (decl))
5465 member_template_p = true;
5466 if (TREE_CODE (decl) == TYPE_DECL
5467 && anon_aggrname_p (DECL_NAME (decl)))
5469 error ("template class without a name");
5470 return error_mark_node;
5472 else if (TREE_CODE (decl) == FUNCTION_DECL)
5474 if (member_template_p)
5476 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5477 error ("member template %qD may not have virt-specifiers", decl);
5479 if (DECL_DESTRUCTOR_P (decl))
5481 /* [temp.mem]
5483 A destructor shall not be a member template. */
5484 error ("destructor %qD declared as member template", decl);
5485 return error_mark_node;
5487 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5488 && (!prototype_p (TREE_TYPE (decl))
5489 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5490 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5491 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5492 == void_list_node)))
5494 /* [basic.stc.dynamic.allocation]
5496 An allocation function can be a function
5497 template. ... Template allocation functions shall
5498 have two or more parameters. */
5499 error ("invalid template declaration of %qD", decl);
5500 return error_mark_node;
5503 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5504 && CLASS_TYPE_P (TREE_TYPE (decl)))
5506 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5507 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5508 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5510 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5511 if (TREE_CODE (t) == TYPE_DECL)
5512 t = TREE_TYPE (t);
5513 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5514 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5517 else if (TREE_CODE (decl) == TYPE_DECL
5518 && TYPE_DECL_ALIAS_P (decl))
5519 /* alias-declaration */
5520 gcc_assert (!DECL_ARTIFICIAL (decl));
5521 else if (VAR_P (decl))
5522 /* C++14 variable template. */;
5523 else
5525 error ("template declaration of %q#D", decl);
5526 return error_mark_node;
5530 /* Check to see that the rules regarding the use of default
5531 arguments are not being violated. We check args for a friend
5532 functions when we know whether it's a definition, introducing
5533 declaration or re-declaration. */
5534 if (!is_friend || TREE_CODE (decl) != FUNCTION_DECL)
5535 check_default_tmpl_args (decl, current_template_parms,
5536 is_primary, is_partial, is_friend);
5538 /* Ensure that there are no parameter packs in the type of this
5539 declaration that have not been expanded. */
5540 if (TREE_CODE (decl) == FUNCTION_DECL)
5542 /* Check each of the arguments individually to see if there are
5543 any bare parameter packs. */
5544 tree type = TREE_TYPE (decl);
5545 tree arg = DECL_ARGUMENTS (decl);
5546 tree argtype = TYPE_ARG_TYPES (type);
5548 while (arg && argtype)
5550 if (!DECL_PACK_P (arg)
5551 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5553 /* This is a PARM_DECL that contains unexpanded parameter
5554 packs. We have already complained about this in the
5555 check_for_bare_parameter_packs call, so just replace
5556 these types with ERROR_MARK_NODE. */
5557 TREE_TYPE (arg) = error_mark_node;
5558 TREE_VALUE (argtype) = error_mark_node;
5561 arg = DECL_CHAIN (arg);
5562 argtype = TREE_CHAIN (argtype);
5565 /* Check for bare parameter packs in the return type and the
5566 exception specifiers. */
5567 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5568 /* Errors were already issued, set return type to int
5569 as the frontend doesn't expect error_mark_node as
5570 the return type. */
5571 TREE_TYPE (type) = integer_type_node;
5572 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5573 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5575 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5576 && TYPE_DECL_ALIAS_P (decl))
5577 ? DECL_ORIGINAL_TYPE (decl)
5578 : TREE_TYPE (decl)))
5580 TREE_TYPE (decl) = error_mark_node;
5581 return error_mark_node;
5584 if (is_partial)
5585 return process_partial_specialization (decl);
5587 args = current_template_args ();
5589 if (!ctx
5590 || TREE_CODE (ctx) == FUNCTION_DECL
5591 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5592 || (TREE_CODE (decl) == TYPE_DECL
5593 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5594 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5596 if (DECL_LANG_SPECIFIC (decl)
5597 && DECL_TEMPLATE_INFO (decl)
5598 && DECL_TI_TEMPLATE (decl))
5599 tmpl = DECL_TI_TEMPLATE (decl);
5600 /* If DECL is a TYPE_DECL for a class-template, then there won't
5601 be DECL_LANG_SPECIFIC. The information equivalent to
5602 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5603 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5604 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5605 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5607 /* Since a template declaration already existed for this
5608 class-type, we must be redeclaring it here. Make sure
5609 that the redeclaration is valid. */
5610 redeclare_class_template (TREE_TYPE (decl),
5611 current_template_parms,
5612 current_template_constraints ());
5613 /* We don't need to create a new TEMPLATE_DECL; just use the
5614 one we already had. */
5615 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5617 else
5619 tmpl = build_template_decl (decl, current_template_parms,
5620 member_template_p);
5621 new_template_p = 1;
5623 if (DECL_LANG_SPECIFIC (decl)
5624 && DECL_TEMPLATE_SPECIALIZATION (decl))
5626 /* A specialization of a member template of a template
5627 class. */
5628 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5629 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5630 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5634 else
5636 tree a, t, current, parms;
5637 int i;
5638 tree tinfo = get_template_info (decl);
5640 if (!tinfo)
5642 error ("template definition of non-template %q#D", decl);
5643 return error_mark_node;
5646 tmpl = TI_TEMPLATE (tinfo);
5648 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5649 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5650 && DECL_TEMPLATE_SPECIALIZATION (decl)
5651 && DECL_MEMBER_TEMPLATE_P (tmpl))
5653 tree new_tmpl;
5655 /* The declaration is a specialization of a member
5656 template, declared outside the class. Therefore, the
5657 innermost template arguments will be NULL, so we
5658 replace them with the arguments determined by the
5659 earlier call to check_explicit_specialization. */
5660 args = DECL_TI_ARGS (decl);
5662 new_tmpl
5663 = build_template_decl (decl, current_template_parms,
5664 member_template_p);
5665 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5666 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5667 DECL_TI_TEMPLATE (decl) = new_tmpl;
5668 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5669 DECL_TEMPLATE_INFO (new_tmpl)
5670 = build_template_info (tmpl, args);
5672 register_specialization (new_tmpl,
5673 most_general_template (tmpl),
5674 args,
5675 is_friend, 0);
5676 return decl;
5679 /* Make sure the template headers we got make sense. */
5681 parms = DECL_TEMPLATE_PARMS (tmpl);
5682 i = TMPL_PARMS_DEPTH (parms);
5683 if (TMPL_ARGS_DEPTH (args) != i)
5685 error ("expected %d levels of template parms for %q#D, got %d",
5686 i, decl, TMPL_ARGS_DEPTH (args));
5687 DECL_INTERFACE_KNOWN (decl) = 1;
5688 return error_mark_node;
5690 else
5691 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5693 a = TMPL_ARGS_LEVEL (args, i);
5694 t = INNERMOST_TEMPLATE_PARMS (parms);
5696 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5698 if (current == decl)
5699 error ("got %d template parameters for %q#D",
5700 TREE_VEC_LENGTH (a), decl);
5701 else
5702 error ("got %d template parameters for %q#T",
5703 TREE_VEC_LENGTH (a), current);
5704 error (" but %d required", TREE_VEC_LENGTH (t));
5705 /* Avoid crash in import_export_decl. */
5706 DECL_INTERFACE_KNOWN (decl) = 1;
5707 return error_mark_node;
5710 if (current == decl)
5711 current = ctx;
5712 else if (current == NULL_TREE)
5713 /* Can happen in erroneous input. */
5714 break;
5715 else
5716 current = get_containing_scope (current);
5719 /* Check that the parms are used in the appropriate qualifying scopes
5720 in the declarator. */
5721 if (!comp_template_args
5722 (TI_ARGS (tinfo),
5723 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5725 error ("template arguments to %qD do not match original "
5726 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
5727 if (!uses_template_parms (TI_ARGS (tinfo)))
5728 inform (input_location, "use %<template<>%> for"
5729 " an explicit specialization");
5730 /* Avoid crash in import_export_decl. */
5731 DECL_INTERFACE_KNOWN (decl) = 1;
5732 return error_mark_node;
5736 DECL_TEMPLATE_RESULT (tmpl) = decl;
5737 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5739 /* Push template declarations for global functions and types. Note
5740 that we do not try to push a global template friend declared in a
5741 template class; such a thing may well depend on the template
5742 parameters of the class. */
5743 if (new_template_p && !ctx
5744 && !(is_friend && template_class_depth (current_class_type) > 0))
5746 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5747 if (tmpl == error_mark_node)
5748 return error_mark_node;
5750 /* Hide template friend classes that haven't been declared yet. */
5751 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5753 DECL_ANTICIPATED (tmpl) = 1;
5754 DECL_FRIEND_P (tmpl) = 1;
5758 if (is_primary)
5760 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5762 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5764 /* Give template template parms a DECL_CONTEXT of the template
5765 for which they are a parameter. */
5766 parms = INNERMOST_TEMPLATE_PARMS (parms);
5767 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5769 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5770 if (TREE_CODE (parm) == TEMPLATE_DECL)
5771 DECL_CONTEXT (parm) = tmpl;
5774 if (TREE_CODE (decl) == TYPE_DECL
5775 && TYPE_DECL_ALIAS_P (decl)
5776 && complex_alias_template_p (tmpl))
5777 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5780 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5781 back to its most general template. If TMPL is a specialization,
5782 ARGS may only have the innermost set of arguments. Add the missing
5783 argument levels if necessary. */
5784 if (DECL_TEMPLATE_INFO (tmpl))
5785 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5787 info = build_template_info (tmpl, args);
5789 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5790 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5791 else
5793 if (is_primary)
5794 retrofit_lang_decl (decl);
5795 if (DECL_LANG_SPECIFIC (decl))
5796 DECL_TEMPLATE_INFO (decl) = info;
5799 if (flag_implicit_templates
5800 && !is_friend
5801 && TREE_PUBLIC (decl)
5802 && VAR_OR_FUNCTION_DECL_P (decl))
5803 /* Set DECL_COMDAT on template instantiations; if we force
5804 them to be emitted by explicit instantiation or -frepo,
5805 mark_needed will tell cgraph to do the right thing. */
5806 DECL_COMDAT (decl) = true;
5808 return DECL_TEMPLATE_RESULT (tmpl);
5811 tree
5812 push_template_decl (tree decl)
5814 return push_template_decl_real (decl, false);
5817 /* FN is an inheriting constructor that inherits from the constructor
5818 template INHERITED; turn FN into a constructor template with a matching
5819 template header. */
5821 tree
5822 add_inherited_template_parms (tree fn, tree inherited)
5824 tree inner_parms
5825 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5826 inner_parms = copy_node (inner_parms);
5827 tree parms
5828 = tree_cons (size_int (processing_template_decl + 1),
5829 inner_parms, current_template_parms);
5830 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5831 tree args = template_parms_to_args (parms);
5832 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5833 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5834 DECL_TEMPLATE_RESULT (tmpl) = fn;
5835 DECL_ARTIFICIAL (tmpl) = true;
5836 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5837 return tmpl;
5840 /* Called when a class template TYPE is redeclared with the indicated
5841 template PARMS, e.g.:
5843 template <class T> struct S;
5844 template <class T> struct S {}; */
5846 bool
5847 redeclare_class_template (tree type, tree parms, tree cons)
5849 tree tmpl;
5850 tree tmpl_parms;
5851 int i;
5853 if (!TYPE_TEMPLATE_INFO (type))
5855 error ("%qT is not a template type", type);
5856 return false;
5859 tmpl = TYPE_TI_TEMPLATE (type);
5860 if (!PRIMARY_TEMPLATE_P (tmpl))
5861 /* The type is nested in some template class. Nothing to worry
5862 about here; there are no new template parameters for the nested
5863 type. */
5864 return true;
5866 if (!parms)
5868 error ("template specifiers not specified in declaration of %qD",
5869 tmpl);
5870 return false;
5873 parms = INNERMOST_TEMPLATE_PARMS (parms);
5874 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5876 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5878 error_n (input_location, TREE_VEC_LENGTH (parms),
5879 "redeclared with %d template parameter",
5880 "redeclared with %d template parameters",
5881 TREE_VEC_LENGTH (parms));
5882 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5883 "previous declaration %qD used %d template parameter",
5884 "previous declaration %qD used %d template parameters",
5885 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5886 return false;
5889 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5891 tree tmpl_parm;
5892 tree parm;
5893 tree tmpl_default;
5894 tree parm_default;
5896 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5897 || TREE_VEC_ELT (parms, i) == error_mark_node)
5898 continue;
5900 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5901 if (error_operand_p (tmpl_parm))
5902 return false;
5904 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5905 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5906 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5908 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5909 TEMPLATE_DECL. */
5910 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5911 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5912 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5913 || (TREE_CODE (tmpl_parm) != PARM_DECL
5914 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5915 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5916 || (TREE_CODE (tmpl_parm) == PARM_DECL
5917 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5918 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5920 error ("template parameter %q+#D", tmpl_parm);
5921 error ("redeclared here as %q#D", parm);
5922 return false;
5925 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5927 /* We have in [temp.param]:
5929 A template-parameter may not be given default arguments
5930 by two different declarations in the same scope. */
5931 error_at (input_location, "redefinition of default argument for %q#D", parm);
5932 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5933 "original definition appeared here");
5934 return false;
5937 if (parm_default != NULL_TREE)
5938 /* Update the previous template parameters (which are the ones
5939 that will really count) with the new default value. */
5940 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5941 else if (tmpl_default != NULL_TREE)
5942 /* Update the new parameters, too; they'll be used as the
5943 parameters for any members. */
5944 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5946 /* Give each template template parm in this redeclaration a
5947 DECL_CONTEXT of the template for which they are a parameter. */
5948 if (TREE_CODE (parm) == TEMPLATE_DECL)
5950 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5951 DECL_CONTEXT (parm) = tmpl;
5954 if (TREE_CODE (parm) == TYPE_DECL)
5955 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
5958 // Cannot redeclare a class template with a different set of constraints.
5959 if (!equivalent_constraints (get_constraints (tmpl), cons))
5961 error_at (input_location, "redeclaration %q#D with different "
5962 "constraints", tmpl);
5963 inform (DECL_SOURCE_LOCATION (tmpl),
5964 "original declaration appeared here");
5967 return true;
5970 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5971 to be used when the caller has already checked
5972 (processing_template_decl
5973 && !instantiation_dependent_expression_p (expr)
5974 && potential_constant_expression (expr))
5975 and cleared processing_template_decl. */
5977 tree
5978 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
5980 return tsubst_copy_and_build (expr,
5981 /*args=*/NULL_TREE,
5982 complain,
5983 /*in_decl=*/NULL_TREE,
5984 /*function_p=*/false,
5985 /*integral_constant_expression_p=*/true);
5988 /* Simplify EXPR if it is a non-dependent expression. Returns the
5989 (possibly simplified) expression. */
5991 tree
5992 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5994 if (expr == NULL_TREE)
5995 return NULL_TREE;
5997 /* If we're in a template, but EXPR isn't value dependent, simplify
5998 it. We're supposed to treat:
6000 template <typename T> void f(T[1 + 1]);
6001 template <typename T> void f(T[2]);
6003 as two declarations of the same function, for example. */
6004 if (processing_template_decl
6005 && is_nondependent_constant_expression (expr))
6007 processing_template_decl_sentinel s;
6008 expr = instantiate_non_dependent_expr_internal (expr, complain);
6010 return expr;
6013 tree
6014 instantiate_non_dependent_expr (tree expr)
6016 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
6019 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6020 an uninstantiated expression. */
6022 tree
6023 instantiate_non_dependent_or_null (tree expr)
6025 if (expr == NULL_TREE)
6026 return NULL_TREE;
6027 if (processing_template_decl)
6029 if (!is_nondependent_constant_expression (expr))
6030 expr = NULL_TREE;
6031 else
6033 processing_template_decl_sentinel s;
6034 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
6037 return expr;
6040 /* True iff T is a specialization of a variable template. */
6042 bool
6043 variable_template_specialization_p (tree t)
6045 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
6046 return false;
6047 tree tmpl = DECL_TI_TEMPLATE (t);
6048 return variable_template_p (tmpl);
6051 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6052 template declaration, or a TYPE_DECL for an alias declaration. */
6054 bool
6055 alias_type_or_template_p (tree t)
6057 if (t == NULL_TREE)
6058 return false;
6059 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
6060 || (TYPE_P (t)
6061 && TYPE_NAME (t)
6062 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
6063 || DECL_ALIAS_TEMPLATE_P (t));
6066 /* Return TRUE iff T is a specialization of an alias template. */
6068 bool
6069 alias_template_specialization_p (const_tree t)
6071 /* It's an alias template specialization if it's an alias and its
6072 TYPE_NAME is a specialization of a primary template. */
6073 if (TYPE_ALIAS_P (t))
6074 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
6075 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo));
6077 return false;
6080 /* An alias template is complex from a SFINAE perspective if a template-id
6081 using that alias can be ill-formed when the expansion is not, as with
6082 the void_t template. We determine this by checking whether the
6083 expansion for the alias template uses all its template parameters. */
6085 struct uses_all_template_parms_data
6087 int level;
6088 bool *seen;
6091 static int
6092 uses_all_template_parms_r (tree t, void *data_)
6094 struct uses_all_template_parms_data &data
6095 = *(struct uses_all_template_parms_data*)data_;
6096 tree idx = get_template_parm_index (t);
6098 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
6099 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
6100 return 0;
6103 static bool
6104 complex_alias_template_p (const_tree tmpl)
6106 struct uses_all_template_parms_data data;
6107 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6108 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6109 data.level = TMPL_PARMS_DEPTH (parms);
6110 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
6111 data.seen = XALLOCAVEC (bool, len);
6112 for (int i = 0; i < len; ++i)
6113 data.seen[i] = false;
6115 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
6116 for (int i = 0; i < len; ++i)
6117 if (!data.seen[i])
6118 return true;
6119 return false;
6122 /* Return TRUE iff T is a specialization of a complex alias template with
6123 dependent template-arguments. */
6125 bool
6126 dependent_alias_template_spec_p (const_tree t)
6128 if (!alias_template_specialization_p (t))
6129 return false;
6131 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
6132 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo)))
6133 return false;
6135 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
6136 if (!any_dependent_template_arguments_p (args))
6137 return false;
6139 return true;
6142 /* Return the number of innermost template parameters in TMPL. */
6144 static int
6145 num_innermost_template_parms (tree tmpl)
6147 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6148 return TREE_VEC_LENGTH (parms);
6151 /* Return either TMPL or another template that it is equivalent to under DR
6152 1286: An alias that just changes the name of a template is equivalent to
6153 the other template. */
6155 static tree
6156 get_underlying_template (tree tmpl)
6158 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6159 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6161 /* Determine if the alias is equivalent to an underlying template. */
6162 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6163 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6164 if (!tinfo)
6165 break;
6167 tree underlying = TI_TEMPLATE (tinfo);
6168 if (!PRIMARY_TEMPLATE_P (underlying)
6169 || (num_innermost_template_parms (tmpl)
6170 != num_innermost_template_parms (underlying)))
6171 break;
6173 tree alias_args = INNERMOST_TEMPLATE_ARGS
6174 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
6175 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6176 break;
6178 /* Alias is equivalent. Strip it and repeat. */
6179 tmpl = underlying;
6182 return tmpl;
6185 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6186 must be a reference-to-function or a pointer-to-function type, as specified
6187 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6188 and check that the resulting function has external linkage. */
6190 static tree
6191 convert_nontype_argument_function (tree type, tree expr,
6192 tsubst_flags_t complain)
6194 tree fns = expr;
6195 tree fn, fn_no_ptr;
6196 linkage_kind linkage;
6198 fn = instantiate_type (type, fns, tf_none);
6199 if (fn == error_mark_node)
6200 return error_mark_node;
6202 if (value_dependent_expression_p (fn))
6203 goto accept;
6205 fn_no_ptr = strip_fnptr_conv (fn);
6206 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6207 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6208 if (BASELINK_P (fn_no_ptr))
6209 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6211 /* [temp.arg.nontype]/1
6213 A template-argument for a non-type, non-template template-parameter
6214 shall be one of:
6215 [...]
6216 -- the address of an object or function with external [C++11: or
6217 internal] linkage. */
6219 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6221 if (complain & tf_error)
6223 error ("%qE is not a valid template argument for type %qT",
6224 expr, type);
6225 if (TYPE_PTR_P (type))
6226 inform (input_location, "it must be the address of a function "
6227 "with external linkage");
6228 else
6229 inform (input_location, "it must be the name of a function with "
6230 "external linkage");
6232 return NULL_TREE;
6235 linkage = decl_linkage (fn_no_ptr);
6236 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6238 if (complain & tf_error)
6240 if (cxx_dialect >= cxx11)
6241 error ("%qE is not a valid template argument for type %qT "
6242 "because %qD has no linkage",
6243 expr, type, fn_no_ptr);
6244 else
6245 error ("%qE is not a valid template argument for type %qT "
6246 "because %qD does not have external linkage",
6247 expr, type, fn_no_ptr);
6249 return NULL_TREE;
6252 accept:
6253 if (TREE_CODE (type) == REFERENCE_TYPE)
6255 if (REFERENCE_REF_P (fn))
6256 fn = TREE_OPERAND (fn, 0);
6257 else
6258 fn = build_address (fn);
6260 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6261 fn = build_nop (type, fn);
6263 return fn;
6266 /* Subroutine of convert_nontype_argument.
6267 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6268 Emit an error otherwise. */
6270 static bool
6271 check_valid_ptrmem_cst_expr (tree type, tree expr,
6272 tsubst_flags_t complain)
6274 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6275 tree orig_expr = expr;
6276 STRIP_NOPS (expr);
6277 if (null_ptr_cst_p (expr))
6278 return true;
6279 if (TREE_CODE (expr) == PTRMEM_CST
6280 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6281 PTRMEM_CST_CLASS (expr)))
6282 return true;
6283 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6284 return true;
6285 if (processing_template_decl
6286 && TREE_CODE (expr) == ADDR_EXPR
6287 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6288 return true;
6289 if (complain & tf_error)
6291 error_at (loc, "%qE is not a valid template argument for type %qT",
6292 orig_expr, type);
6293 if (TREE_CODE (expr) != PTRMEM_CST)
6294 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6295 else
6296 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6298 return false;
6301 /* Returns TRUE iff the address of OP is value-dependent.
6303 14.6.2.4 [temp.dep.temp]:
6304 A non-integral non-type template-argument is dependent if its type is
6305 dependent or it has either of the following forms
6306 qualified-id
6307 & qualified-id
6308 and contains a nested-name-specifier which specifies a class-name that
6309 names a dependent type.
6311 We generalize this to just say that the address of a member of a
6312 dependent class is value-dependent; the above doesn't cover the
6313 address of a static data member named with an unqualified-id. */
6315 static bool
6316 has_value_dependent_address (tree op)
6318 /* We could use get_inner_reference here, but there's no need;
6319 this is only relevant for template non-type arguments, which
6320 can only be expressed as &id-expression. */
6321 if (DECL_P (op))
6323 tree ctx = CP_DECL_CONTEXT (op);
6324 if (TYPE_P (ctx) && dependent_type_p (ctx))
6325 return true;
6328 return false;
6331 /* The next set of functions are used for providing helpful explanatory
6332 diagnostics for failed overload resolution. Their messages should be
6333 indented by two spaces for consistency with the messages in
6334 call.c */
6336 static int
6337 unify_success (bool /*explain_p*/)
6339 return 0;
6342 /* Other failure functions should call this one, to provide a single function
6343 for setting a breakpoint on. */
6345 static int
6346 unify_invalid (bool /*explain_p*/)
6348 return 1;
6351 static int
6352 unify_parameter_deduction_failure (bool explain_p, tree parm)
6354 if (explain_p)
6355 inform (input_location,
6356 " couldn't deduce template parameter %qD", parm);
6357 return unify_invalid (explain_p);
6360 static int
6361 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6363 if (explain_p)
6364 inform (input_location,
6365 " types %qT and %qT have incompatible cv-qualifiers",
6366 parm, arg);
6367 return unify_invalid (explain_p);
6370 static int
6371 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6373 if (explain_p)
6374 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6375 return unify_invalid (explain_p);
6378 static int
6379 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6381 if (explain_p)
6382 inform (input_location,
6383 " template parameter %qD is not a parameter pack, but "
6384 "argument %qD is",
6385 parm, arg);
6386 return unify_invalid (explain_p);
6389 static int
6390 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6392 if (explain_p)
6393 inform (input_location,
6394 " template argument %qE does not match "
6395 "pointer-to-member constant %qE",
6396 arg, parm);
6397 return unify_invalid (explain_p);
6400 static int
6401 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6403 if (explain_p)
6404 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6405 return unify_invalid (explain_p);
6408 static int
6409 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6411 if (explain_p)
6412 inform (input_location,
6413 " inconsistent parameter pack deduction with %qT and %qT",
6414 old_arg, new_arg);
6415 return unify_invalid (explain_p);
6418 static int
6419 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6421 if (explain_p)
6423 if (TYPE_P (parm))
6424 inform (input_location,
6425 " deduced conflicting types for parameter %qT (%qT and %qT)",
6426 parm, first, second);
6427 else
6428 inform (input_location,
6429 " deduced conflicting values for non-type parameter "
6430 "%qE (%qE and %qE)", parm, first, second);
6432 return unify_invalid (explain_p);
6435 static int
6436 unify_vla_arg (bool explain_p, tree arg)
6438 if (explain_p)
6439 inform (input_location,
6440 " variable-sized array type %qT is not "
6441 "a valid template argument",
6442 arg);
6443 return unify_invalid (explain_p);
6446 static int
6447 unify_method_type_error (bool explain_p, tree arg)
6449 if (explain_p)
6450 inform (input_location,
6451 " member function type %qT is not a valid template argument",
6452 arg);
6453 return unify_invalid (explain_p);
6456 static int
6457 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6459 if (explain_p)
6461 if (least_p)
6462 inform_n (input_location, wanted,
6463 " candidate expects at least %d argument, %d provided",
6464 " candidate expects at least %d arguments, %d provided",
6465 wanted, have);
6466 else
6467 inform_n (input_location, wanted,
6468 " candidate expects %d argument, %d provided",
6469 " candidate expects %d arguments, %d provided",
6470 wanted, have);
6472 return unify_invalid (explain_p);
6475 static int
6476 unify_too_many_arguments (bool explain_p, int have, int wanted)
6478 return unify_arity (explain_p, have, wanted);
6481 static int
6482 unify_too_few_arguments (bool explain_p, int have, int wanted,
6483 bool least_p = false)
6485 return unify_arity (explain_p, have, wanted, least_p);
6488 static int
6489 unify_arg_conversion (bool explain_p, tree to_type,
6490 tree from_type, tree arg)
6492 if (explain_p)
6493 inform (EXPR_LOC_OR_LOC (arg, input_location),
6494 " cannot convert %qE (type %qT) to type %qT",
6495 arg, from_type, to_type);
6496 return unify_invalid (explain_p);
6499 static int
6500 unify_no_common_base (bool explain_p, enum template_base_result r,
6501 tree parm, tree arg)
6503 if (explain_p)
6504 switch (r)
6506 case tbr_ambiguous_baseclass:
6507 inform (input_location, " %qT is an ambiguous base class of %qT",
6508 parm, arg);
6509 break;
6510 default:
6511 inform (input_location, " %qT is not derived from %qT", arg, parm);
6512 break;
6514 return unify_invalid (explain_p);
6517 static int
6518 unify_inconsistent_template_template_parameters (bool explain_p)
6520 if (explain_p)
6521 inform (input_location,
6522 " template parameters of a template template argument are "
6523 "inconsistent with other deduced template arguments");
6524 return unify_invalid (explain_p);
6527 static int
6528 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6530 if (explain_p)
6531 inform (input_location,
6532 " can't deduce a template for %qT from non-template type %qT",
6533 parm, arg);
6534 return unify_invalid (explain_p);
6537 static int
6538 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6540 if (explain_p)
6541 inform (input_location,
6542 " template argument %qE does not match %qE", arg, parm);
6543 return unify_invalid (explain_p);
6546 /* Attempt to convert the non-type template parameter EXPR to the
6547 indicated TYPE. If the conversion is successful, return the
6548 converted value. If the conversion is unsuccessful, return
6549 NULL_TREE if we issued an error message, or error_mark_node if we
6550 did not. We issue error messages for out-and-out bad template
6551 parameters, but not simply because the conversion failed, since we
6552 might be just trying to do argument deduction. Both TYPE and EXPR
6553 must be non-dependent.
6555 The conversion follows the special rules described in
6556 [temp.arg.nontype], and it is much more strict than an implicit
6557 conversion.
6559 This function is called twice for each template argument (see
6560 lookup_template_class for a more accurate description of this
6561 problem). This means that we need to handle expressions which
6562 are not valid in a C++ source, but can be created from the
6563 first call (for instance, casts to perform conversions). These
6564 hacks can go away after we fix the double coercion problem. */
6566 static tree
6567 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6569 tree expr_type;
6570 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6571 tree orig_expr = expr;
6573 /* Detect immediately string literals as invalid non-type argument.
6574 This special-case is not needed for correctness (we would easily
6575 catch this later), but only to provide better diagnostic for this
6576 common user mistake. As suggested by DR 100, we do not mention
6577 linkage issues in the diagnostic as this is not the point. */
6578 /* FIXME we're making this OK. */
6579 if (TREE_CODE (expr) == STRING_CST)
6581 if (complain & tf_error)
6582 error ("%qE is not a valid template argument for type %qT "
6583 "because string literals can never be used in this context",
6584 expr, type);
6585 return NULL_TREE;
6588 /* Add the ADDR_EXPR now for the benefit of
6589 value_dependent_expression_p. */
6590 if (TYPE_PTROBV_P (type)
6591 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6593 expr = decay_conversion (expr, complain);
6594 if (expr == error_mark_node)
6595 return error_mark_node;
6598 /* If we are in a template, EXPR may be non-dependent, but still
6599 have a syntactic, rather than semantic, form. For example, EXPR
6600 might be a SCOPE_REF, rather than the VAR_DECL to which the
6601 SCOPE_REF refers. Preserving the qualifying scope is necessary
6602 so that access checking can be performed when the template is
6603 instantiated -- but here we need the resolved form so that we can
6604 convert the argument. */
6605 bool non_dep = false;
6606 if (TYPE_REF_OBJ_P (type)
6607 && has_value_dependent_address (expr))
6608 /* If we want the address and it's value-dependent, don't fold. */;
6609 else if (processing_template_decl
6610 && is_nondependent_constant_expression (expr))
6611 non_dep = true;
6612 if (error_operand_p (expr))
6613 return error_mark_node;
6614 expr_type = TREE_TYPE (expr);
6616 /* If the argument is non-dependent, perform any conversions in
6617 non-dependent context as well. */
6618 processing_template_decl_sentinel s (non_dep);
6619 if (non_dep)
6620 expr = instantiate_non_dependent_expr_internal (expr, complain);
6622 if (value_dependent_expression_p (expr))
6623 expr = canonicalize_expr_argument (expr, complain);
6625 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6626 to a non-type argument of "nullptr". */
6627 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
6628 expr = fold_simple (convert (type, expr));
6630 /* In C++11, integral or enumeration non-type template arguments can be
6631 arbitrary constant expressions. Pointer and pointer to
6632 member arguments can be general constant expressions that evaluate
6633 to a null value, but otherwise still need to be of a specific form. */
6634 if (cxx_dialect >= cxx11)
6636 if (TREE_CODE (expr) == PTRMEM_CST)
6637 /* A PTRMEM_CST is already constant, and a valid template
6638 argument for a parameter of pointer to member type, we just want
6639 to leave it in that form rather than lower it to a
6640 CONSTRUCTOR. */;
6641 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6642 || cxx_dialect >= cxx17)
6644 /* C++17: A template-argument for a non-type template-parameter shall
6645 be a converted constant expression (8.20) of the type of the
6646 template-parameter. */
6647 expr = build_converted_constant_expr (type, expr, complain);
6648 if (expr == error_mark_node)
6649 return error_mark_node;
6650 expr = maybe_constant_value (expr);
6651 expr = convert_from_reference (expr);
6653 else if (TYPE_PTR_OR_PTRMEM_P (type))
6655 tree folded = maybe_constant_value (expr);
6656 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6657 : null_member_pointer_value_p (folded))
6658 expr = folded;
6662 if (TREE_CODE (type) == REFERENCE_TYPE)
6663 expr = mark_lvalue_use (expr);
6664 else
6665 expr = mark_rvalue_use (expr);
6667 /* HACK: Due to double coercion, we can get a
6668 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6669 which is the tree that we built on the first call (see
6670 below when coercing to reference to object or to reference to
6671 function). We just strip everything and get to the arg.
6672 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6673 for examples. */
6674 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6676 tree probe_type, probe = expr;
6677 if (REFERENCE_REF_P (probe))
6678 probe = TREE_OPERAND (probe, 0);
6679 probe_type = TREE_TYPE (probe);
6680 if (TREE_CODE (probe) == NOP_EXPR)
6682 /* ??? Maybe we could use convert_from_reference here, but we
6683 would need to relax its constraints because the NOP_EXPR
6684 could actually change the type to something more cv-qualified,
6685 and this is not folded by convert_from_reference. */
6686 tree addr = TREE_OPERAND (probe, 0);
6687 if (TREE_CODE (probe_type) == REFERENCE_TYPE
6688 && TREE_CODE (addr) == ADDR_EXPR
6689 && TYPE_PTR_P (TREE_TYPE (addr))
6690 && (same_type_ignoring_top_level_qualifiers_p
6691 (TREE_TYPE (probe_type),
6692 TREE_TYPE (TREE_TYPE (addr)))))
6694 expr = TREE_OPERAND (addr, 0);
6695 expr_type = TREE_TYPE (probe_type);
6700 /* [temp.arg.nontype]/5, bullet 1
6702 For a non-type template-parameter of integral or enumeration type,
6703 integral promotions (_conv.prom_) and integral conversions
6704 (_conv.integral_) are applied. */
6705 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6707 if (cxx_dialect < cxx11)
6709 tree t = build_converted_constant_expr (type, expr, complain);
6710 t = maybe_constant_value (t);
6711 if (t != error_mark_node)
6712 expr = t;
6715 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6716 return error_mark_node;
6718 /* Notice that there are constant expressions like '4 % 0' which
6719 do not fold into integer constants. */
6720 if (TREE_CODE (expr) != INTEGER_CST
6721 && !value_dependent_expression_p (expr))
6723 if (complain & tf_error)
6725 int errs = errorcount, warns = warningcount + werrorcount;
6726 if (!require_potential_constant_expression (expr))
6727 expr = error_mark_node;
6728 else
6729 expr = cxx_constant_value (expr);
6730 if (errorcount > errs || warningcount + werrorcount > warns)
6731 inform (loc, "in template argument for type %qT ", type);
6732 if (expr == error_mark_node)
6733 return NULL_TREE;
6734 /* else cxx_constant_value complained but gave us
6735 a real constant, so go ahead. */
6736 if (TREE_CODE (expr) != INTEGER_CST)
6738 /* Some assemble time constant expressions like
6739 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
6740 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
6741 as we can emit them into .rodata initializers of
6742 variables, yet they can't fold into an INTEGER_CST at
6743 compile time. Refuse them here. */
6744 gcc_checking_assert (reduced_constant_expression_p (expr));
6745 error_at (loc, "template argument %qE for type %qT not "
6746 "a constant integer", expr, type);
6747 return NULL_TREE;
6750 else
6751 return NULL_TREE;
6754 /* Avoid typedef problems. */
6755 if (TREE_TYPE (expr) != type)
6756 expr = fold_convert (type, expr);
6758 /* [temp.arg.nontype]/5, bullet 2
6760 For a non-type template-parameter of type pointer to object,
6761 qualification conversions (_conv.qual_) and the array-to-pointer
6762 conversion (_conv.array_) are applied. */
6763 else if (TYPE_PTROBV_P (type))
6765 tree decayed = expr;
6767 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6768 decay_conversion or an explicit cast. If it's a problematic cast,
6769 we'll complain about it below. */
6770 if (TREE_CODE (expr) == NOP_EXPR)
6772 tree probe = expr;
6773 STRIP_NOPS (probe);
6774 if (TREE_CODE (probe) == ADDR_EXPR
6775 && TYPE_PTR_P (TREE_TYPE (probe)))
6777 expr = probe;
6778 expr_type = TREE_TYPE (expr);
6782 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6784 A template-argument for a non-type, non-template template-parameter
6785 shall be one of: [...]
6787 -- the name of a non-type template-parameter;
6788 -- the address of an object or function with external linkage, [...]
6789 expressed as "& id-expression" where the & is optional if the name
6790 refers to a function or array, or if the corresponding
6791 template-parameter is a reference.
6793 Here, we do not care about functions, as they are invalid anyway
6794 for a parameter of type pointer-to-object. */
6796 if (value_dependent_expression_p (expr))
6797 /* Non-type template parameters are OK. */
6799 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6800 /* Null pointer values are OK in C++11. */;
6801 else if (TREE_CODE (expr) != ADDR_EXPR)
6803 if (VAR_P (expr))
6805 if (complain & tf_error)
6806 error ("%qD is not a valid template argument "
6807 "because %qD is a variable, not the address of "
6808 "a variable", orig_expr, expr);
6809 return NULL_TREE;
6811 if (POINTER_TYPE_P (expr_type))
6813 if (complain & tf_error)
6814 error ("%qE is not a valid template argument for %qT "
6815 "because it is not the address of a variable",
6816 orig_expr, type);
6817 return NULL_TREE;
6819 /* Other values, like integer constants, might be valid
6820 non-type arguments of some other type. */
6821 return error_mark_node;
6823 else
6825 tree decl = TREE_OPERAND (expr, 0);
6827 if (!VAR_P (decl))
6829 if (complain & tf_error)
6830 error ("%qE is not a valid template argument of type %qT "
6831 "because %qE is not a variable", orig_expr, type, decl);
6832 return NULL_TREE;
6834 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6836 if (complain & tf_error)
6837 error ("%qE is not a valid template argument of type %qT "
6838 "because %qD does not have external linkage",
6839 orig_expr, type, decl);
6840 return NULL_TREE;
6842 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
6843 && decl_linkage (decl) == lk_none)
6845 if (complain & tf_error)
6846 error ("%qE is not a valid template argument of type %qT "
6847 "because %qD has no linkage", orig_expr, type, decl);
6848 return NULL_TREE;
6850 /* C++17: For a non-type template-parameter of reference or pointer
6851 type, the value of the constant expression shall not refer to (or
6852 for a pointer type, shall not be the address of):
6853 * a subobject (4.5),
6854 * a temporary object (15.2),
6855 * a string literal (5.13.5),
6856 * the result of a typeid expression (8.2.8), or
6857 * a predefined __func__ variable (11.4.1). */
6858 else if (DECL_ARTIFICIAL (decl))
6860 if (complain & tf_error)
6861 error ("the address of %qD is not a valid template argument",
6862 decl);
6863 return NULL_TREE;
6865 else if (!same_type_ignoring_top_level_qualifiers_p
6866 (strip_array_types (TREE_TYPE (type)),
6867 strip_array_types (TREE_TYPE (decl))))
6869 if (complain & tf_error)
6870 error ("the address of the %qT subobject of %qD is not a "
6871 "valid template argument", TREE_TYPE (type), decl);
6872 return NULL_TREE;
6874 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
6876 if (complain & tf_error)
6877 error ("the address of %qD is not a valid template argument "
6878 "because it does not have static storage duration",
6879 decl);
6880 return NULL_TREE;
6884 expr = decayed;
6886 expr = perform_qualification_conversions (type, expr);
6887 if (expr == error_mark_node)
6888 return error_mark_node;
6890 /* [temp.arg.nontype]/5, bullet 3
6892 For a non-type template-parameter of type reference to object, no
6893 conversions apply. The type referred to by the reference may be more
6894 cv-qualified than the (otherwise identical) type of the
6895 template-argument. The template-parameter is bound directly to the
6896 template-argument, which must be an lvalue. */
6897 else if (TYPE_REF_OBJ_P (type))
6899 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6900 expr_type))
6901 return error_mark_node;
6903 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6905 if (complain & tf_error)
6906 error ("%qE is not a valid template argument for type %qT "
6907 "because of conflicts in cv-qualification", expr, type);
6908 return NULL_TREE;
6911 if (!lvalue_p (expr))
6913 if (complain & tf_error)
6914 error ("%qE is not a valid template argument for type %qT "
6915 "because it is not an lvalue", expr, type);
6916 return NULL_TREE;
6919 /* [temp.arg.nontype]/1
6921 A template-argument for a non-type, non-template template-parameter
6922 shall be one of: [...]
6924 -- the address of an object or function with external linkage. */
6925 if (INDIRECT_REF_P (expr)
6926 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6928 expr = TREE_OPERAND (expr, 0);
6929 if (DECL_P (expr))
6931 if (complain & tf_error)
6932 error ("%q#D is not a valid template argument for type %qT "
6933 "because a reference variable does not have a constant "
6934 "address", expr, type);
6935 return NULL_TREE;
6939 if (TYPE_REF_OBJ_P (TREE_TYPE (expr))
6940 && value_dependent_expression_p (expr))
6941 /* OK, dependent reference. We don't want to ask whether a DECL is
6942 itself value-dependent, since what we want here is its address. */;
6943 else
6945 if (!DECL_P (expr))
6947 if (complain & tf_error)
6948 error ("%qE is not a valid template argument for type %qT "
6949 "because it is not an object with linkage",
6950 expr, type);
6951 return NULL_TREE;
6954 /* DR 1155 allows internal linkage in C++11 and up. */
6955 linkage_kind linkage = decl_linkage (expr);
6956 if (linkage < (cxx_dialect >= cxx11 ? lk_internal : lk_external))
6958 if (complain & tf_error)
6959 error ("%qE is not a valid template argument for type %qT "
6960 "because object %qD does not have linkage",
6961 expr, type, expr);
6962 return NULL_TREE;
6965 expr = build_address (expr);
6968 if (!same_type_p (type, TREE_TYPE (expr)))
6969 expr = build_nop (type, expr);
6971 /* [temp.arg.nontype]/5, bullet 4
6973 For a non-type template-parameter of type pointer to function, only
6974 the function-to-pointer conversion (_conv.func_) is applied. If the
6975 template-argument represents a set of overloaded functions (or a
6976 pointer to such), the matching function is selected from the set
6977 (_over.over_). */
6978 else if (TYPE_PTRFN_P (type))
6980 /* If the argument is a template-id, we might not have enough
6981 context information to decay the pointer. */
6982 if (!type_unknown_p (expr_type))
6984 expr = decay_conversion (expr, complain);
6985 if (expr == error_mark_node)
6986 return error_mark_node;
6989 if (cxx_dialect >= cxx11 && integer_zerop (expr))
6990 /* Null pointer values are OK in C++11. */
6991 return perform_qualification_conversions (type, expr);
6993 expr = convert_nontype_argument_function (type, expr, complain);
6994 if (!expr || expr == error_mark_node)
6995 return expr;
6997 /* [temp.arg.nontype]/5, bullet 5
6999 For a non-type template-parameter of type reference to function, no
7000 conversions apply. If the template-argument represents a set of
7001 overloaded functions, the matching function is selected from the set
7002 (_over.over_). */
7003 else if (TYPE_REFFN_P (type))
7005 if (TREE_CODE (expr) == ADDR_EXPR)
7007 if (complain & tf_error)
7009 error ("%qE is not a valid template argument for type %qT "
7010 "because it is a pointer", expr, type);
7011 inform (input_location, "try using %qE instead",
7012 TREE_OPERAND (expr, 0));
7014 return NULL_TREE;
7017 expr = convert_nontype_argument_function (type, expr, complain);
7018 if (!expr || expr == error_mark_node)
7019 return expr;
7021 /* [temp.arg.nontype]/5, bullet 6
7023 For a non-type template-parameter of type pointer to member function,
7024 no conversions apply. If the template-argument represents a set of
7025 overloaded member functions, the matching member function is selected
7026 from the set (_over.over_). */
7027 else if (TYPE_PTRMEMFUNC_P (type))
7029 expr = instantiate_type (type, expr, tf_none);
7030 if (expr == error_mark_node)
7031 return error_mark_node;
7033 /* [temp.arg.nontype] bullet 1 says the pointer to member
7034 expression must be a pointer-to-member constant. */
7035 if (!value_dependent_expression_p (expr)
7036 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7037 return NULL_TREE;
7039 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7040 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7041 if (fnptr_conv_p (type, TREE_TYPE (expr)))
7042 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
7044 /* [temp.arg.nontype]/5, bullet 7
7046 For a non-type template-parameter of type pointer to data member,
7047 qualification conversions (_conv.qual_) are applied. */
7048 else if (TYPE_PTRDATAMEM_P (type))
7050 /* [temp.arg.nontype] bullet 1 says the pointer to member
7051 expression must be a pointer-to-member constant. */
7052 if (!value_dependent_expression_p (expr)
7053 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7054 return NULL_TREE;
7056 expr = perform_qualification_conversions (type, expr);
7057 if (expr == error_mark_node)
7058 return expr;
7060 else if (NULLPTR_TYPE_P (type))
7062 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
7064 if (complain & tf_error)
7065 error ("%qE is not a valid template argument for type %qT "
7066 "because it is of type %qT", expr, type, TREE_TYPE (expr));
7067 return NULL_TREE;
7069 return expr;
7071 /* A template non-type parameter must be one of the above. */
7072 else
7073 gcc_unreachable ();
7075 /* Sanity check: did we actually convert the argument to the
7076 right type? */
7077 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7078 (type, TREE_TYPE (expr)));
7079 return convert_from_reference (expr);
7082 /* Subroutine of coerce_template_template_parms, which returns 1 if
7083 PARM_PARM and ARG_PARM match using the rule for the template
7084 parameters of template template parameters. Both PARM and ARG are
7085 template parameters; the rest of the arguments are the same as for
7086 coerce_template_template_parms.
7088 static int
7089 coerce_template_template_parm (tree parm,
7090 tree arg,
7091 tsubst_flags_t complain,
7092 tree in_decl,
7093 tree outer_args)
7095 if (arg == NULL_TREE || error_operand_p (arg)
7096 || parm == NULL_TREE || error_operand_p (parm))
7097 return 0;
7099 if (TREE_CODE (arg) != TREE_CODE (parm))
7100 return 0;
7102 switch (TREE_CODE (parm))
7104 case TEMPLATE_DECL:
7105 /* We encounter instantiations of templates like
7106 template <template <template <class> class> class TT>
7107 class C; */
7109 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7110 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7112 if (!coerce_template_template_parms
7113 (parmparm, argparm, complain, in_decl, outer_args))
7114 return 0;
7116 /* Fall through. */
7118 case TYPE_DECL:
7119 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
7120 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7121 /* Argument is a parameter pack but parameter is not. */
7122 return 0;
7123 break;
7125 case PARM_DECL:
7126 /* The tsubst call is used to handle cases such as
7128 template <int> class C {};
7129 template <class T, template <T> class TT> class D {};
7130 D<int, C> d;
7132 i.e. the parameter list of TT depends on earlier parameters. */
7133 if (!uses_template_parms (TREE_TYPE (arg)))
7135 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
7136 if (!uses_template_parms (t)
7137 && !same_type_p (t, TREE_TYPE (arg)))
7138 return 0;
7141 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
7142 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7143 /* Argument is a parameter pack but parameter is not. */
7144 return 0;
7146 break;
7148 default:
7149 gcc_unreachable ();
7152 return 1;
7155 /* Coerce template argument list ARGLIST for use with template
7156 template-parameter TEMPL. */
7158 static tree
7159 coerce_template_args_for_ttp (tree templ, tree arglist,
7160 tsubst_flags_t complain)
7162 /* Consider an example where a template template parameter declared as
7164 template <class T, class U = std::allocator<T> > class TT
7166 The template parameter level of T and U are one level larger than
7167 of TT. To proper process the default argument of U, say when an
7168 instantiation `TT<int>' is seen, we need to build the full
7169 arguments containing {int} as the innermost level. Outer levels,
7170 available when not appearing as default template argument, can be
7171 obtained from the arguments of the enclosing template.
7173 Suppose that TT is later substituted with std::vector. The above
7174 instantiation is `TT<int, std::allocator<T> >' with TT at
7175 level 1, and T at level 2, while the template arguments at level 1
7176 becomes {std::vector} and the inner level 2 is {int}. */
7178 tree outer = DECL_CONTEXT (templ);
7179 if (outer)
7181 if (DECL_TEMPLATE_SPECIALIZATION (outer))
7182 /* We want arguments for the partial specialization, not arguments for
7183 the primary template. */
7184 outer = template_parms_to_args (DECL_TEMPLATE_PARMS (outer));
7185 else
7186 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7188 else if (current_template_parms)
7190 /* This is an argument of the current template, so we haven't set
7191 DECL_CONTEXT yet. */
7192 tree relevant_template_parms;
7194 /* Parameter levels that are greater than the level of the given
7195 template template parm are irrelevant. */
7196 relevant_template_parms = current_template_parms;
7197 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7198 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7199 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7201 outer = template_parms_to_args (relevant_template_parms);
7204 if (outer)
7205 arglist = add_to_template_args (outer, arglist);
7207 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7208 return coerce_template_parms (parmlist, arglist, templ,
7209 complain,
7210 /*require_all_args=*/true,
7211 /*use_default_args=*/true);
7214 /* A cache of template template parameters with match-all default
7215 arguments. */
7216 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7217 static void
7218 store_defaulted_ttp (tree v, tree t)
7220 if (!defaulted_ttp_cache)
7221 defaulted_ttp_cache = hash_map<tree,tree>::create_ggc (13);
7222 defaulted_ttp_cache->put (v, t);
7224 static tree
7225 lookup_defaulted_ttp (tree v)
7227 if (defaulted_ttp_cache)
7228 if (tree *p = defaulted_ttp_cache->get (v))
7229 return *p;
7230 return NULL_TREE;
7233 /* T is a bound template template-parameter. Copy its arguments into default
7234 arguments of the template template-parameter's template parameters. */
7236 static tree
7237 add_defaults_to_ttp (tree otmpl)
7239 if (tree c = lookup_defaulted_ttp (otmpl))
7240 return c;
7242 tree ntmpl = copy_node (otmpl);
7244 tree ntype = copy_node (TREE_TYPE (otmpl));
7245 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7246 TYPE_MAIN_VARIANT (ntype) = ntype;
7247 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7248 TYPE_NAME (ntype) = ntmpl;
7249 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7251 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7252 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7253 TEMPLATE_PARM_DECL (idx) = ntmpl;
7254 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7256 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7257 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7258 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7259 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7260 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7262 tree o = TREE_VEC_ELT (vec, i);
7263 if (!template_parameter_pack_p (TREE_VALUE (o)))
7265 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7266 TREE_PURPOSE (n) = any_targ_node;
7270 store_defaulted_ttp (otmpl, ntmpl);
7271 return ntmpl;
7274 /* ARG is a bound potential template template-argument, and PARGS is a list
7275 of arguments for the corresponding template template-parameter. Adjust
7276 PARGS as appropriate for application to ARG's template, and if ARG is a
7277 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7278 arguments to the template template parameter. */
7280 static tree
7281 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7283 ++processing_template_decl;
7284 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7285 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7287 /* When comparing two template template-parameters in partial ordering,
7288 rewrite the one currently being used as an argument to have default
7289 arguments for all parameters. */
7290 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7291 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7292 if (pargs != error_mark_node)
7293 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7294 TYPE_TI_ARGS (arg));
7296 else
7298 tree aparms
7299 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7300 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7301 /*require_all*/true,
7302 /*use_default*/true);
7304 --processing_template_decl;
7305 return pargs;
7308 /* Subroutine of unify for the case when PARM is a
7309 BOUND_TEMPLATE_TEMPLATE_PARM. */
7311 static int
7312 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7313 bool explain_p)
7315 tree parmvec = TYPE_TI_ARGS (parm);
7316 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7318 /* The template template parm might be variadic and the argument
7319 not, so flatten both argument lists. */
7320 parmvec = expand_template_argument_pack (parmvec);
7321 argvec = expand_template_argument_pack (argvec);
7323 if (flag_new_ttp)
7325 /* In keeping with P0522R0, adjust P's template arguments
7326 to apply to A's template; then flatten it again. */
7327 tree nparmvec = parmvec;
7328 nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7329 nparmvec = expand_template_argument_pack (nparmvec);
7331 if (unify (tparms, targs, nparmvec, argvec,
7332 UNIFY_ALLOW_NONE, explain_p))
7333 return 1;
7335 /* If the P0522 adjustment eliminated a pack expansion, deduce
7336 empty packs. */
7337 if (flag_new_ttp
7338 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7339 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7340 DEDUCE_EXACT, /*sub*/true, explain_p))
7341 return 1;
7343 else
7345 /* Deduce arguments T, i from TT<T> or TT<i>.
7346 We check each element of PARMVEC and ARGVEC individually
7347 rather than the whole TREE_VEC since they can have
7348 different number of elements, which is allowed under N2555. */
7350 int len = TREE_VEC_LENGTH (parmvec);
7352 /* Check if the parameters end in a pack, making them
7353 variadic. */
7354 int parm_variadic_p = 0;
7355 if (len > 0
7356 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7357 parm_variadic_p = 1;
7359 for (int i = 0; i < len - parm_variadic_p; ++i)
7360 /* If the template argument list of P contains a pack
7361 expansion that is not the last template argument, the
7362 entire template argument list is a non-deduced
7363 context. */
7364 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7365 return unify_success (explain_p);
7367 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7368 return unify_too_few_arguments (explain_p,
7369 TREE_VEC_LENGTH (argvec), len);
7371 for (int i = 0; i < len - parm_variadic_p; ++i)
7372 if (unify (tparms, targs,
7373 TREE_VEC_ELT (parmvec, i),
7374 TREE_VEC_ELT (argvec, i),
7375 UNIFY_ALLOW_NONE, explain_p))
7376 return 1;
7378 if (parm_variadic_p
7379 && unify_pack_expansion (tparms, targs,
7380 parmvec, argvec,
7381 DEDUCE_EXACT,
7382 /*subr=*/true, explain_p))
7383 return 1;
7386 return 0;
7389 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7390 template template parameters. Both PARM_PARMS and ARG_PARMS are
7391 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7392 or PARM_DECL.
7394 Consider the example:
7395 template <class T> class A;
7396 template<template <class U> class TT> class B;
7398 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7399 the parameters to A, and OUTER_ARGS contains A. */
7401 static int
7402 coerce_template_template_parms (tree parm_parms,
7403 tree arg_parms,
7404 tsubst_flags_t complain,
7405 tree in_decl,
7406 tree outer_args)
7408 int nparms, nargs, i;
7409 tree parm, arg;
7410 int variadic_p = 0;
7412 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7413 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7415 nparms = TREE_VEC_LENGTH (parm_parms);
7416 nargs = TREE_VEC_LENGTH (arg_parms);
7418 if (flag_new_ttp)
7420 /* P0522R0: A template template-parameter P is at least as specialized as
7421 a template template-argument A if, given the following rewrite to two
7422 function templates, the function template corresponding to P is at
7423 least as specialized as the function template corresponding to A
7424 according to the partial ordering rules for function templates
7425 ([temp.func.order]). Given an invented class template X with the
7426 template parameter list of A (including default arguments):
7428 * Each of the two function templates has the same template parameters,
7429 respectively, as P or A.
7431 * Each function template has a single function parameter whose type is
7432 a specialization of X with template arguments corresponding to the
7433 template parameters from the respective function template where, for
7434 each template parameter PP in the template parameter list of the
7435 function template, a corresponding template argument AA is formed. If
7436 PP declares a parameter pack, then AA is the pack expansion
7437 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7439 If the rewrite produces an invalid type, then P is not at least as
7440 specialized as A. */
7442 /* So coerce P's args to apply to A's parms, and then deduce between A's
7443 args and the converted args. If that succeeds, A is at least as
7444 specialized as P, so they match.*/
7445 tree pargs = template_parms_level_to_args (parm_parms);
7446 ++processing_template_decl;
7447 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7448 /*require_all*/true, /*use_default*/true);
7449 --processing_template_decl;
7450 if (pargs != error_mark_node)
7452 tree targs = make_tree_vec (nargs);
7453 tree aargs = template_parms_level_to_args (arg_parms);
7454 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7455 /*explain*/false))
7456 return 1;
7460 /* Determine whether we have a parameter pack at the end of the
7461 template template parameter's template parameter list. */
7462 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7464 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7466 if (error_operand_p (parm))
7467 return 0;
7469 switch (TREE_CODE (parm))
7471 case TEMPLATE_DECL:
7472 case TYPE_DECL:
7473 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7474 variadic_p = 1;
7475 break;
7477 case PARM_DECL:
7478 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7479 variadic_p = 1;
7480 break;
7482 default:
7483 gcc_unreachable ();
7487 if (nargs != nparms
7488 && !(variadic_p && nargs >= nparms - 1))
7489 return 0;
7491 /* Check all of the template parameters except the parameter pack at
7492 the end (if any). */
7493 for (i = 0; i < nparms - variadic_p; ++i)
7495 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7496 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7497 continue;
7499 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7500 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7502 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7503 outer_args))
7504 return 0;
7508 if (variadic_p)
7510 /* Check each of the template parameters in the template
7511 argument against the template parameter pack at the end of
7512 the template template parameter. */
7513 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7514 return 0;
7516 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7518 for (; i < nargs; ++i)
7520 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7521 continue;
7523 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7525 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7526 outer_args))
7527 return 0;
7531 return 1;
7534 /* Verifies that the deduced template arguments (in TARGS) for the
7535 template template parameters (in TPARMS) represent valid bindings,
7536 by comparing the template parameter list of each template argument
7537 to the template parameter list of its corresponding template
7538 template parameter, in accordance with DR150. This
7539 routine can only be called after all template arguments have been
7540 deduced. It will return TRUE if all of the template template
7541 parameter bindings are okay, FALSE otherwise. */
7542 bool
7543 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7545 int i, ntparms = TREE_VEC_LENGTH (tparms);
7546 bool ret = true;
7548 /* We're dealing with template parms in this process. */
7549 ++processing_template_decl;
7551 targs = INNERMOST_TEMPLATE_ARGS (targs);
7553 for (i = 0; i < ntparms; ++i)
7555 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7556 tree targ = TREE_VEC_ELT (targs, i);
7558 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7560 tree packed_args = NULL_TREE;
7561 int idx, len = 1;
7563 if (ARGUMENT_PACK_P (targ))
7565 /* Look inside the argument pack. */
7566 packed_args = ARGUMENT_PACK_ARGS (targ);
7567 len = TREE_VEC_LENGTH (packed_args);
7570 for (idx = 0; idx < len; ++idx)
7572 tree targ_parms = NULL_TREE;
7574 if (packed_args)
7575 /* Extract the next argument from the argument
7576 pack. */
7577 targ = TREE_VEC_ELT (packed_args, idx);
7579 if (PACK_EXPANSION_P (targ))
7580 /* Look at the pattern of the pack expansion. */
7581 targ = PACK_EXPANSION_PATTERN (targ);
7583 /* Extract the template parameters from the template
7584 argument. */
7585 if (TREE_CODE (targ) == TEMPLATE_DECL)
7586 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7587 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7588 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7590 /* Verify that we can coerce the template template
7591 parameters from the template argument to the template
7592 parameter. This requires an exact match. */
7593 if (targ_parms
7594 && !coerce_template_template_parms
7595 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7596 targ_parms,
7597 tf_none,
7598 tparm,
7599 targs))
7601 ret = false;
7602 goto out;
7608 out:
7610 --processing_template_decl;
7611 return ret;
7614 /* Since type attributes aren't mangled, we need to strip them from
7615 template type arguments. */
7617 static tree
7618 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7620 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7621 return arg;
7622 bool removed_attributes = false;
7623 tree canon = strip_typedefs (arg, &removed_attributes);
7624 if (removed_attributes
7625 && (complain & tf_warning))
7626 warning (OPT_Wignored_attributes,
7627 "ignoring attributes on template argument %qT", arg);
7628 return canon;
7631 /* And from inside dependent non-type arguments like sizeof(Type). */
7633 static tree
7634 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7636 if (!arg || arg == error_mark_node)
7637 return arg;
7638 bool removed_attributes = false;
7639 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7640 if (removed_attributes
7641 && (complain & tf_warning))
7642 warning (OPT_Wignored_attributes,
7643 "ignoring attributes in template argument %qE", arg);
7644 return canon;
7647 // A template declaration can be substituted for a constrained
7648 // template template parameter only when the argument is more
7649 // constrained than the parameter.
7650 static bool
7651 is_compatible_template_arg (tree parm, tree arg)
7653 tree parm_cons = get_constraints (parm);
7655 /* For now, allow constrained template template arguments
7656 and unconstrained template template parameters. */
7657 if (parm_cons == NULL_TREE)
7658 return true;
7660 tree arg_cons = get_constraints (arg);
7662 // If the template parameter is constrained, we need to rewrite its
7663 // constraints in terms of the ARG's template parameters. This ensures
7664 // that all of the template parameter types will have the same depth.
7666 // Note that this is only valid when coerce_template_template_parm is
7667 // true for the innermost template parameters of PARM and ARG. In other
7668 // words, because coercion is successful, this conversion will be valid.
7669 if (parm_cons)
7671 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7672 parm_cons = tsubst_constraint_info (parm_cons,
7673 INNERMOST_TEMPLATE_ARGS (args),
7674 tf_none, NULL_TREE);
7675 if (parm_cons == error_mark_node)
7676 return false;
7679 return subsumes (parm_cons, arg_cons);
7682 // Convert a placeholder argument into a binding to the original
7683 // parameter. The original parameter is saved as the TREE_TYPE of
7684 // ARG.
7685 static inline tree
7686 convert_wildcard_argument (tree parm, tree arg)
7688 TREE_TYPE (arg) = parm;
7689 return arg;
7692 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
7693 because one of them is dependent. But we need to represent the
7694 conversion for the benefit of cp_tree_equal. */
7696 static tree
7697 maybe_convert_nontype_argument (tree type, tree arg)
7699 /* Auto parms get no conversion. */
7700 if (type_uses_auto (type))
7701 return arg;
7702 /* We don't need or want to add this conversion now if we're going to use the
7703 argument for deduction. */
7704 if (value_dependent_expression_p (arg))
7705 return arg;
7707 type = cv_unqualified (type);
7708 tree argtype = TREE_TYPE (arg);
7709 if (same_type_p (type, argtype))
7710 return arg;
7712 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
7713 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
7714 return arg;
7717 /* Convert the indicated template ARG as necessary to match the
7718 indicated template PARM. Returns the converted ARG, or
7719 error_mark_node if the conversion was unsuccessful. Error and
7720 warning messages are issued under control of COMPLAIN. This
7721 conversion is for the Ith parameter in the parameter list. ARGS is
7722 the full set of template arguments deduced so far. */
7724 static tree
7725 convert_template_argument (tree parm,
7726 tree arg,
7727 tree args,
7728 tsubst_flags_t complain,
7729 int i,
7730 tree in_decl)
7732 tree orig_arg;
7733 tree val;
7734 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7736 if (parm == error_mark_node)
7737 return error_mark_node;
7739 /* Trivially convert placeholders. */
7740 if (TREE_CODE (arg) == WILDCARD_DECL)
7741 return convert_wildcard_argument (parm, arg);
7743 if (arg == any_targ_node)
7744 return arg;
7746 if (TREE_CODE (arg) == TREE_LIST
7747 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7749 /* The template argument was the name of some
7750 member function. That's usually
7751 invalid, but static members are OK. In any
7752 case, grab the underlying fields/functions
7753 and issue an error later if required. */
7754 orig_arg = TREE_VALUE (arg);
7755 TREE_TYPE (arg) = unknown_type_node;
7758 orig_arg = arg;
7760 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7761 requires_type = (TREE_CODE (parm) == TYPE_DECL
7762 || requires_tmpl_type);
7764 /* When determining whether an argument pack expansion is a template,
7765 look at the pattern. */
7766 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7767 arg = PACK_EXPANSION_PATTERN (arg);
7769 /* Deal with an injected-class-name used as a template template arg. */
7770 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7772 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7773 if (TREE_CODE (t) == TEMPLATE_DECL)
7775 if (cxx_dialect >= cxx11)
7776 /* OK under DR 1004. */;
7777 else if (complain & tf_warning_or_error)
7778 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7779 " used as template template argument", TYPE_NAME (arg));
7780 else if (flag_pedantic_errors)
7781 t = arg;
7783 arg = t;
7787 is_tmpl_type =
7788 ((TREE_CODE (arg) == TEMPLATE_DECL
7789 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7790 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7791 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7792 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7794 if (is_tmpl_type
7795 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7796 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7797 arg = TYPE_STUB_DECL (arg);
7799 is_type = TYPE_P (arg) || is_tmpl_type;
7801 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7802 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7804 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7806 if (complain & tf_error)
7807 error ("invalid use of destructor %qE as a type", orig_arg);
7808 return error_mark_node;
7811 permerror (input_location,
7812 "to refer to a type member of a template parameter, "
7813 "use %<typename %E%>", orig_arg);
7815 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7816 TREE_OPERAND (arg, 1),
7817 typename_type,
7818 complain);
7819 arg = orig_arg;
7820 is_type = 1;
7822 if (is_type != requires_type)
7824 if (in_decl)
7826 if (complain & tf_error)
7828 error ("type/value mismatch at argument %d in template "
7829 "parameter list for %qD",
7830 i + 1, in_decl);
7831 if (is_type)
7832 inform (input_location,
7833 " expected a constant of type %qT, got %qT",
7834 TREE_TYPE (parm),
7835 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7836 else if (requires_tmpl_type)
7837 inform (input_location,
7838 " expected a class template, got %qE", orig_arg);
7839 else
7840 inform (input_location,
7841 " expected a type, got %qE", orig_arg);
7844 return error_mark_node;
7846 if (is_tmpl_type ^ requires_tmpl_type)
7848 if (in_decl && (complain & tf_error))
7850 error ("type/value mismatch at argument %d in template "
7851 "parameter list for %qD",
7852 i + 1, in_decl);
7853 if (is_tmpl_type)
7854 inform (input_location,
7855 " expected a type, got %qT", DECL_NAME (arg));
7856 else
7857 inform (input_location,
7858 " expected a class template, got %qT", orig_arg);
7860 return error_mark_node;
7863 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7864 /* We already did the appropriate conversion when packing args. */
7865 val = orig_arg;
7866 else if (is_type)
7868 if (requires_tmpl_type)
7870 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7871 /* The number of argument required is not known yet.
7872 Just accept it for now. */
7873 val = orig_arg;
7874 else
7876 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7877 tree argparm;
7879 /* Strip alias templates that are equivalent to another
7880 template. */
7881 arg = get_underlying_template (arg);
7882 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7884 if (coerce_template_template_parms (parmparm, argparm,
7885 complain, in_decl,
7886 args))
7888 val = arg;
7890 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7891 TEMPLATE_DECL. */
7892 if (val != error_mark_node)
7894 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
7895 val = TREE_TYPE (val);
7896 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
7897 val = make_pack_expansion (val, complain);
7900 else
7902 if (in_decl && (complain & tf_error))
7904 error ("type/value mismatch at argument %d in "
7905 "template parameter list for %qD",
7906 i + 1, in_decl);
7907 inform (input_location,
7908 " expected a template of type %qD, got %qT",
7909 parm, orig_arg);
7912 val = error_mark_node;
7915 // Check that the constraints are compatible before allowing the
7916 // substitution.
7917 if (val != error_mark_node)
7918 if (!is_compatible_template_arg (parm, arg))
7920 if (in_decl && (complain & tf_error))
7922 error ("constraint mismatch at argument %d in "
7923 "template parameter list for %qD",
7924 i + 1, in_decl);
7925 inform (input_location, " expected %qD but got %qD",
7926 parm, arg);
7928 val = error_mark_node;
7932 else
7933 val = orig_arg;
7934 /* We only form one instance of each template specialization.
7935 Therefore, if we use a non-canonical variant (i.e., a
7936 typedef), any future messages referring to the type will use
7937 the typedef, which is confusing if those future uses do not
7938 themselves also use the typedef. */
7939 if (TYPE_P (val))
7940 val = canonicalize_type_argument (val, complain);
7942 else
7944 tree t = TREE_TYPE (parm);
7946 if (tree a = type_uses_auto (t))
7948 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
7949 if (t == error_mark_node)
7950 return error_mark_node;
7952 else
7953 t = tsubst (t, args, complain, in_decl);
7955 if (invalid_nontype_parm_type_p (t, complain))
7956 return error_mark_node;
7958 if (!type_dependent_expression_p (orig_arg)
7959 && !uses_template_parms (t))
7960 /* We used to call digest_init here. However, digest_init
7961 will report errors, which we don't want when complain
7962 is zero. More importantly, digest_init will try too
7963 hard to convert things: for example, `0' should not be
7964 converted to pointer type at this point according to
7965 the standard. Accepting this is not merely an
7966 extension, since deciding whether or not these
7967 conversions can occur is part of determining which
7968 function template to call, or whether a given explicit
7969 argument specification is valid. */
7970 val = convert_nontype_argument (t, orig_arg, complain);
7971 else
7973 val = canonicalize_expr_argument (orig_arg, complain);
7974 val = maybe_convert_nontype_argument (t, val);
7978 if (val == NULL_TREE)
7979 val = error_mark_node;
7980 else if (val == error_mark_node && (complain & tf_error))
7981 error ("could not convert template argument %qE from %qT to %qT",
7982 orig_arg, TREE_TYPE (orig_arg), t);
7984 if (INDIRECT_REF_P (val))
7986 /* Reject template arguments that are references to built-in
7987 functions with no library fallbacks. */
7988 const_tree inner = TREE_OPERAND (val, 0);
7989 const_tree innertype = TREE_TYPE (inner);
7990 if (innertype
7991 && TREE_CODE (innertype) == REFERENCE_TYPE
7992 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
7993 && TREE_OPERAND_LENGTH (inner) > 0
7994 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
7995 return error_mark_node;
7998 if (TREE_CODE (val) == SCOPE_REF)
8000 /* Strip typedefs from the SCOPE_REF. */
8001 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
8002 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
8003 complain);
8004 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
8005 QUALIFIED_NAME_IS_TEMPLATE (val));
8009 return val;
8012 /* Coerces the remaining template arguments in INNER_ARGS (from
8013 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8014 Returns the coerced argument pack. PARM_IDX is the position of this
8015 parameter in the template parameter list. ARGS is the original
8016 template argument list. */
8017 static tree
8018 coerce_template_parameter_pack (tree parms,
8019 int parm_idx,
8020 tree args,
8021 tree inner_args,
8022 int arg_idx,
8023 tree new_args,
8024 int* lost,
8025 tree in_decl,
8026 tsubst_flags_t complain)
8028 tree parm = TREE_VEC_ELT (parms, parm_idx);
8029 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8030 tree packed_args;
8031 tree argument_pack;
8032 tree packed_parms = NULL_TREE;
8034 if (arg_idx > nargs)
8035 arg_idx = nargs;
8037 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
8039 /* When the template parameter is a non-type template parameter pack
8040 or template template parameter pack whose type or template
8041 parameters use parameter packs, we know exactly how many arguments
8042 we are looking for. Build a vector of the instantiated decls for
8043 these template parameters in PACKED_PARMS. */
8044 /* We can't use make_pack_expansion here because it would interpret a
8045 _DECL as a use rather than a declaration. */
8046 tree decl = TREE_VALUE (parm);
8047 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
8048 SET_PACK_EXPANSION_PATTERN (exp, decl);
8049 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
8050 SET_TYPE_STRUCTURAL_EQUALITY (exp);
8052 TREE_VEC_LENGTH (args)--;
8053 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
8054 TREE_VEC_LENGTH (args)++;
8056 if (packed_parms == error_mark_node)
8057 return error_mark_node;
8059 /* If we're doing a partial instantiation of a member template,
8060 verify that all of the types used for the non-type
8061 template parameter pack are, in fact, valid for non-type
8062 template parameters. */
8063 if (arg_idx < nargs
8064 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
8066 int j, len = TREE_VEC_LENGTH (packed_parms);
8067 for (j = 0; j < len; ++j)
8069 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
8070 if (invalid_nontype_parm_type_p (t, complain))
8071 return error_mark_node;
8073 /* We don't know how many args we have yet, just
8074 use the unconverted ones for now. */
8075 return NULL_TREE;
8078 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
8080 /* Check if we have a placeholder pack, which indicates we're
8081 in the context of a introduction list. In that case we want
8082 to match this pack to the single placeholder. */
8083 else if (arg_idx < nargs
8084 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
8085 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
8087 nargs = arg_idx + 1;
8088 packed_args = make_tree_vec (1);
8090 else
8091 packed_args = make_tree_vec (nargs - arg_idx);
8093 /* Convert the remaining arguments, which will be a part of the
8094 parameter pack "parm". */
8095 int first_pack_arg = arg_idx;
8096 for (; arg_idx < nargs; ++arg_idx)
8098 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
8099 tree actual_parm = TREE_VALUE (parm);
8100 int pack_idx = arg_idx - first_pack_arg;
8102 if (packed_parms)
8104 /* Once we've packed as many args as we have types, stop. */
8105 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
8106 break;
8107 else if (PACK_EXPANSION_P (arg))
8108 /* We don't know how many args we have yet, just
8109 use the unconverted ones for now. */
8110 return NULL_TREE;
8111 else
8112 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
8115 if (arg == error_mark_node)
8117 if (complain & tf_error)
8118 error ("template argument %d is invalid", arg_idx + 1);
8120 else
8121 arg = convert_template_argument (actual_parm,
8122 arg, new_args, complain, parm_idx,
8123 in_decl);
8124 if (arg == error_mark_node)
8125 (*lost)++;
8126 TREE_VEC_ELT (packed_args, pack_idx) = arg;
8129 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
8130 && TREE_VEC_LENGTH (packed_args) > 0)
8132 if (complain & tf_error)
8133 error ("wrong number of template arguments (%d, should be %d)",
8134 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
8135 return error_mark_node;
8138 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
8139 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
8140 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
8141 else
8143 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
8144 TREE_CONSTANT (argument_pack) = 1;
8147 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
8148 if (CHECKING_P)
8149 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
8150 TREE_VEC_LENGTH (packed_args));
8151 return argument_pack;
8154 /* Returns the number of pack expansions in the template argument vector
8155 ARGS. */
8157 static int
8158 pack_expansion_args_count (tree args)
8160 int i;
8161 int count = 0;
8162 if (args)
8163 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8165 tree elt = TREE_VEC_ELT (args, i);
8166 if (elt && PACK_EXPANSION_P (elt))
8167 ++count;
8169 return count;
8172 /* Convert all template arguments to their appropriate types, and
8173 return a vector containing the innermost resulting template
8174 arguments. If any error occurs, return error_mark_node. Error and
8175 warning messages are issued under control of COMPLAIN.
8177 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8178 for arguments not specified in ARGS. Otherwise, if
8179 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8180 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8181 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8182 ARGS. */
8184 static tree
8185 coerce_template_parms (tree parms,
8186 tree args,
8187 tree in_decl,
8188 tsubst_flags_t complain,
8189 bool require_all_args,
8190 bool use_default_args)
8192 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8193 tree orig_inner_args;
8194 tree inner_args;
8195 tree new_args;
8196 tree new_inner_args;
8197 int saved_unevaluated_operand;
8198 int saved_inhibit_evaluation_warnings;
8200 /* When used as a boolean value, indicates whether this is a
8201 variadic template parameter list. Since it's an int, we can also
8202 subtract it from nparms to get the number of non-variadic
8203 parameters. */
8204 int variadic_p = 0;
8205 int variadic_args_p = 0;
8206 int post_variadic_parms = 0;
8208 /* Likewise for parameters with default arguments. */
8209 int default_p = 0;
8211 if (args == error_mark_node)
8212 return error_mark_node;
8214 nparms = TREE_VEC_LENGTH (parms);
8216 /* Determine if there are any parameter packs or default arguments. */
8217 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8219 tree parm = TREE_VEC_ELT (parms, parm_idx);
8220 if (variadic_p)
8221 ++post_variadic_parms;
8222 if (template_parameter_pack_p (TREE_VALUE (parm)))
8223 ++variadic_p;
8224 if (TREE_PURPOSE (parm))
8225 ++default_p;
8228 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8229 /* If there are no parameters that follow a parameter pack, we need to
8230 expand any argument packs so that we can deduce a parameter pack from
8231 some non-packed args followed by an argument pack, as in variadic85.C.
8232 If there are such parameters, we need to leave argument packs intact
8233 so the arguments are assigned properly. This can happen when dealing
8234 with a nested class inside a partial specialization of a class
8235 template, as in variadic92.C, or when deducing a template parameter pack
8236 from a sub-declarator, as in variadic114.C. */
8237 if (!post_variadic_parms)
8238 inner_args = expand_template_argument_pack (inner_args);
8240 /* Count any pack expansion args. */
8241 variadic_args_p = pack_expansion_args_count (inner_args);
8243 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8244 if ((nargs - variadic_args_p > nparms && !variadic_p)
8245 || (nargs < nparms - variadic_p
8246 && require_all_args
8247 && !variadic_args_p
8248 && (!use_default_args
8249 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8250 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8252 if (complain & tf_error)
8254 if (variadic_p || default_p)
8256 nparms -= variadic_p + default_p;
8257 error ("wrong number of template arguments "
8258 "(%d, should be at least %d)", nargs, nparms);
8260 else
8261 error ("wrong number of template arguments "
8262 "(%d, should be %d)", nargs, nparms);
8264 if (in_decl)
8265 inform (DECL_SOURCE_LOCATION (in_decl),
8266 "provided for %qD", in_decl);
8269 return error_mark_node;
8271 /* We can't pass a pack expansion to a non-pack parameter of an alias
8272 template (DR 1430). */
8273 else if (in_decl
8274 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8275 || concept_template_p (in_decl))
8276 && variadic_args_p
8277 && nargs - variadic_args_p < nparms - variadic_p)
8279 if (complain & tf_error)
8281 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8283 tree arg = TREE_VEC_ELT (inner_args, i);
8284 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8286 if (PACK_EXPANSION_P (arg)
8287 && !template_parameter_pack_p (parm))
8289 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8290 error_at (location_of (arg),
8291 "pack expansion argument for non-pack parameter "
8292 "%qD of alias template %qD", parm, in_decl);
8293 else
8294 error_at (location_of (arg),
8295 "pack expansion argument for non-pack parameter "
8296 "%qD of concept %qD", parm, in_decl);
8297 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8298 goto found;
8301 gcc_unreachable ();
8302 found:;
8304 return error_mark_node;
8307 /* We need to evaluate the template arguments, even though this
8308 template-id may be nested within a "sizeof". */
8309 saved_unevaluated_operand = cp_unevaluated_operand;
8310 cp_unevaluated_operand = 0;
8311 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8312 c_inhibit_evaluation_warnings = 0;
8313 new_inner_args = make_tree_vec (nparms);
8314 new_args = add_outermost_template_args (args, new_inner_args);
8315 int pack_adjust = 0;
8316 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8318 tree arg;
8319 tree parm;
8321 /* Get the Ith template parameter. */
8322 parm = TREE_VEC_ELT (parms, parm_idx);
8324 if (parm == error_mark_node)
8326 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8327 continue;
8330 /* Calculate the next argument. */
8331 if (arg_idx < nargs)
8332 arg = TREE_VEC_ELT (inner_args, arg_idx);
8333 else
8334 arg = NULL_TREE;
8336 if (template_parameter_pack_p (TREE_VALUE (parm))
8337 && !(arg && ARGUMENT_PACK_P (arg)))
8339 /* Some arguments will be placed in the
8340 template parameter pack PARM. */
8341 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8342 inner_args, arg_idx,
8343 new_args, &lost,
8344 in_decl, complain);
8346 if (arg == NULL_TREE)
8348 /* We don't know how many args we have yet, just use the
8349 unconverted (and still packed) ones for now. */
8350 new_inner_args = orig_inner_args;
8351 arg_idx = nargs;
8352 break;
8355 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8357 /* Store this argument. */
8358 if (arg == error_mark_node)
8360 lost++;
8361 /* We are done with all of the arguments. */
8362 arg_idx = nargs;
8364 else
8366 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8367 arg_idx += pack_adjust;
8370 continue;
8372 else if (arg)
8374 if (PACK_EXPANSION_P (arg))
8376 /* "If every valid specialization of a variadic template
8377 requires an empty template parameter pack, the template is
8378 ill-formed, no diagnostic required." So check that the
8379 pattern works with this parameter. */
8380 tree pattern = PACK_EXPANSION_PATTERN (arg);
8381 tree conv = convert_template_argument (TREE_VALUE (parm),
8382 pattern, new_args,
8383 complain, parm_idx,
8384 in_decl);
8385 if (conv == error_mark_node)
8387 if (complain & tf_error)
8388 inform (input_location, "so any instantiation with a "
8389 "non-empty parameter pack would be ill-formed");
8390 ++lost;
8392 else if (TYPE_P (conv) && !TYPE_P (pattern))
8393 /* Recover from missing typename. */
8394 TREE_VEC_ELT (inner_args, arg_idx)
8395 = make_pack_expansion (conv, complain);
8397 /* We don't know how many args we have yet, just
8398 use the unconverted ones for now. */
8399 new_inner_args = inner_args;
8400 arg_idx = nargs;
8401 break;
8404 else if (require_all_args)
8406 /* There must be a default arg in this case. */
8407 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8408 complain, in_decl);
8409 /* The position of the first default template argument,
8410 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8411 Record that. */
8412 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8413 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8414 arg_idx - pack_adjust);
8416 else
8417 break;
8419 if (arg == error_mark_node)
8421 if (complain & tf_error)
8422 error ("template argument %d is invalid", arg_idx + 1);
8424 else if (!arg)
8425 /* This only occurs if there was an error in the template
8426 parameter list itself (which we would already have
8427 reported) that we are trying to recover from, e.g., a class
8428 template with a parameter list such as
8429 template<typename..., typename>. */
8430 ++lost;
8431 else
8432 arg = convert_template_argument (TREE_VALUE (parm),
8433 arg, new_args, complain,
8434 parm_idx, in_decl);
8436 if (arg == error_mark_node)
8437 lost++;
8438 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8440 cp_unevaluated_operand = saved_unevaluated_operand;
8441 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8443 if (variadic_p && arg_idx < nargs)
8445 if (complain & tf_error)
8447 error ("wrong number of template arguments "
8448 "(%d, should be %d)", nargs, arg_idx);
8449 if (in_decl)
8450 error ("provided for %q+D", in_decl);
8452 return error_mark_node;
8455 if (lost)
8456 return error_mark_node;
8458 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8459 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8460 TREE_VEC_LENGTH (new_inner_args));
8462 return new_inner_args;
8465 /* Convert all template arguments to their appropriate types, and
8466 return a vector containing the innermost resulting template
8467 arguments. If any error occurs, return error_mark_node. Error and
8468 warning messages are not issued.
8470 Note that no function argument deduction is performed, and default
8471 arguments are used to fill in unspecified arguments. */
8472 tree
8473 coerce_template_parms (tree parms, tree args, tree in_decl)
8475 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8478 /* Convert all template arguments to their appropriate type, and
8479 instantiate default arguments as needed. This returns a vector
8480 containing the innermost resulting template arguments, or
8481 error_mark_node if unsuccessful. */
8482 tree
8483 coerce_template_parms (tree parms, tree args, tree in_decl,
8484 tsubst_flags_t complain)
8486 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8489 /* Like coerce_template_parms. If PARMS represents all template
8490 parameters levels, this function returns a vector of vectors
8491 representing all the resulting argument levels. Note that in this
8492 case, only the innermost arguments are coerced because the
8493 outermost ones are supposed to have been coerced already.
8495 Otherwise, if PARMS represents only (the innermost) vector of
8496 parameters, this function returns a vector containing just the
8497 innermost resulting arguments. */
8499 static tree
8500 coerce_innermost_template_parms (tree parms,
8501 tree args,
8502 tree in_decl,
8503 tsubst_flags_t complain,
8504 bool require_all_args,
8505 bool use_default_args)
8507 int parms_depth = TMPL_PARMS_DEPTH (parms);
8508 int args_depth = TMPL_ARGS_DEPTH (args);
8509 tree coerced_args;
8511 if (parms_depth > 1)
8513 coerced_args = make_tree_vec (parms_depth);
8514 tree level;
8515 int cur_depth;
8517 for (level = parms, cur_depth = parms_depth;
8518 parms_depth > 0 && level != NULL_TREE;
8519 level = TREE_CHAIN (level), --cur_depth)
8521 tree l;
8522 if (cur_depth == args_depth)
8523 l = coerce_template_parms (TREE_VALUE (level),
8524 args, in_decl, complain,
8525 require_all_args,
8526 use_default_args);
8527 else
8528 l = TMPL_ARGS_LEVEL (args, cur_depth);
8530 if (l == error_mark_node)
8531 return error_mark_node;
8533 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8536 else
8537 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8538 args, in_decl, complain,
8539 require_all_args,
8540 use_default_args);
8541 return coerced_args;
8544 /* Returns 1 if template args OT and NT are equivalent. */
8547 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
8549 if (nt == ot)
8550 return 1;
8551 if (nt == NULL_TREE || ot == NULL_TREE)
8552 return false;
8553 if (nt == any_targ_node || ot == any_targ_node)
8554 return true;
8556 if (TREE_CODE (nt) == TREE_VEC)
8557 /* For member templates */
8558 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8559 else if (PACK_EXPANSION_P (ot))
8560 return (PACK_EXPANSION_P (nt)
8561 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8562 PACK_EXPANSION_PATTERN (nt))
8563 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8564 PACK_EXPANSION_EXTRA_ARGS (nt)));
8565 else if (ARGUMENT_PACK_P (ot))
8567 int i, len;
8568 tree opack, npack;
8570 if (!ARGUMENT_PACK_P (nt))
8571 return 0;
8573 opack = ARGUMENT_PACK_ARGS (ot);
8574 npack = ARGUMENT_PACK_ARGS (nt);
8575 len = TREE_VEC_LENGTH (opack);
8576 if (TREE_VEC_LENGTH (npack) != len)
8577 return 0;
8578 for (i = 0; i < len; ++i)
8579 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8580 TREE_VEC_ELT (npack, i)))
8581 return 0;
8582 return 1;
8584 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8585 gcc_unreachable ();
8586 else if (TYPE_P (nt))
8588 if (!TYPE_P (ot))
8589 return false;
8590 /* Don't treat an alias template specialization with dependent
8591 arguments as equivalent to its underlying type when used as a
8592 template argument; we need them to be distinct so that we
8593 substitute into the specialization arguments at instantiation
8594 time. And aliases can't be equivalent without being ==, so
8595 we don't need to look any deeper.
8597 During partial ordering, however, we need to treat them normally so
8598 that we can order uses of the same alias with different
8599 cv-qualification (79960). */
8600 if (!partial_order
8601 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
8602 return false;
8603 else
8604 return same_type_p (ot, nt);
8606 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8607 return 0;
8608 else
8610 /* Try to treat a template non-type argument that has been converted
8611 to the parameter type as equivalent to one that hasn't yet. */
8612 for (enum tree_code code1 = TREE_CODE (ot);
8613 CONVERT_EXPR_CODE_P (code1)
8614 || code1 == NON_LVALUE_EXPR;
8615 code1 = TREE_CODE (ot))
8616 ot = TREE_OPERAND (ot, 0);
8617 for (enum tree_code code2 = TREE_CODE (nt);
8618 CONVERT_EXPR_CODE_P (code2)
8619 || code2 == NON_LVALUE_EXPR;
8620 code2 = TREE_CODE (nt))
8621 nt = TREE_OPERAND (nt, 0);
8623 return cp_tree_equal (ot, nt);
8627 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8628 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8629 NEWARG_PTR with the offending arguments if they are non-NULL. */
8632 comp_template_args (tree oldargs, tree newargs,
8633 tree *oldarg_ptr, tree *newarg_ptr,
8634 bool partial_order)
8636 int i;
8638 if (oldargs == newargs)
8639 return 1;
8641 if (!oldargs || !newargs)
8642 return 0;
8644 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
8645 return 0;
8647 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
8649 tree nt = TREE_VEC_ELT (newargs, i);
8650 tree ot = TREE_VEC_ELT (oldargs, i);
8652 if (! template_args_equal (ot, nt, partial_order))
8654 if (oldarg_ptr != NULL)
8655 *oldarg_ptr = ot;
8656 if (newarg_ptr != NULL)
8657 *newarg_ptr = nt;
8658 return 0;
8661 return 1;
8664 inline bool
8665 comp_template_args_porder (tree oargs, tree nargs)
8667 return comp_template_args (oargs, nargs, NULL, NULL, true);
8670 static void
8671 add_pending_template (tree d)
8673 tree ti = (TYPE_P (d)
8674 ? CLASSTYPE_TEMPLATE_INFO (d)
8675 : DECL_TEMPLATE_INFO (d));
8676 struct pending_template *pt;
8677 int level;
8679 if (TI_PENDING_TEMPLATE_FLAG (ti))
8680 return;
8682 /* We are called both from instantiate_decl, where we've already had a
8683 tinst_level pushed, and instantiate_template, where we haven't.
8684 Compensate. */
8685 level = !current_tinst_level || current_tinst_level->decl != d;
8687 if (level)
8688 push_tinst_level (d);
8690 pt = ggc_alloc<pending_template> ();
8691 pt->next = NULL;
8692 pt->tinst = current_tinst_level;
8693 if (last_pending_template)
8694 last_pending_template->next = pt;
8695 else
8696 pending_templates = pt;
8698 last_pending_template = pt;
8700 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
8702 if (level)
8703 pop_tinst_level ();
8707 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8708 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8709 documentation for TEMPLATE_ID_EXPR. */
8711 tree
8712 lookup_template_function (tree fns, tree arglist)
8714 tree type;
8716 if (fns == error_mark_node || arglist == error_mark_node)
8717 return error_mark_node;
8719 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
8721 if (!is_overloaded_fn (fns) && !identifier_p (fns))
8723 error ("%q#D is not a function template", fns);
8724 return error_mark_node;
8727 if (BASELINK_P (fns))
8729 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
8730 unknown_type_node,
8731 BASELINK_FUNCTIONS (fns),
8732 arglist);
8733 return fns;
8736 type = TREE_TYPE (fns);
8737 if (TREE_CODE (fns) == OVERLOAD || !type)
8738 type = unknown_type_node;
8740 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
8743 /* Within the scope of a template class S<T>, the name S gets bound
8744 (in build_self_reference) to a TYPE_DECL for the class, not a
8745 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8746 or one of its enclosing classes, and that type is a template,
8747 return the associated TEMPLATE_DECL. Otherwise, the original
8748 DECL is returned.
8750 Also handle the case when DECL is a TREE_LIST of ambiguous
8751 injected-class-names from different bases. */
8753 tree
8754 maybe_get_template_decl_from_type_decl (tree decl)
8756 if (decl == NULL_TREE)
8757 return decl;
8759 /* DR 176: A lookup that finds an injected-class-name (10.2
8760 [class.member.lookup]) can result in an ambiguity in certain cases
8761 (for example, if it is found in more than one base class). If all of
8762 the injected-class-names that are found refer to specializations of
8763 the same class template, and if the name is followed by a
8764 template-argument-list, the reference refers to the class template
8765 itself and not a specialization thereof, and is not ambiguous. */
8766 if (TREE_CODE (decl) == TREE_LIST)
8768 tree t, tmpl = NULL_TREE;
8769 for (t = decl; t; t = TREE_CHAIN (t))
8771 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
8772 if (!tmpl)
8773 tmpl = elt;
8774 else if (tmpl != elt)
8775 break;
8777 if (tmpl && t == NULL_TREE)
8778 return tmpl;
8779 else
8780 return decl;
8783 return (decl != NULL_TREE
8784 && DECL_SELF_REFERENCE_P (decl)
8785 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
8786 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
8789 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8790 parameters, find the desired type.
8792 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8794 IN_DECL, if non-NULL, is the template declaration we are trying to
8795 instantiate.
8797 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8798 the class we are looking up.
8800 Issue error and warning messages under control of COMPLAIN.
8802 If the template class is really a local class in a template
8803 function, then the FUNCTION_CONTEXT is the function in which it is
8804 being instantiated.
8806 ??? Note that this function is currently called *twice* for each
8807 template-id: the first time from the parser, while creating the
8808 incomplete type (finish_template_type), and the second type during the
8809 real instantiation (instantiate_template_class). This is surely something
8810 that we want to avoid. It also causes some problems with argument
8811 coercion (see convert_nontype_argument for more information on this). */
8813 static tree
8814 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
8815 int entering_scope, tsubst_flags_t complain)
8817 tree templ = NULL_TREE, parmlist;
8818 tree t;
8819 spec_entry **slot;
8820 spec_entry *entry;
8821 spec_entry elt;
8822 hashval_t hash;
8824 if (identifier_p (d1))
8826 tree value = innermost_non_namespace_value (d1);
8827 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
8828 templ = value;
8829 else
8831 if (context)
8832 push_decl_namespace (context);
8833 templ = lookup_name (d1);
8834 templ = maybe_get_template_decl_from_type_decl (templ);
8835 if (context)
8836 pop_decl_namespace ();
8838 if (templ)
8839 context = DECL_CONTEXT (templ);
8841 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
8843 tree type = TREE_TYPE (d1);
8845 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8846 an implicit typename for the second A. Deal with it. */
8847 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
8848 type = TREE_TYPE (type);
8850 if (CLASSTYPE_TEMPLATE_INFO (type))
8852 templ = CLASSTYPE_TI_TEMPLATE (type);
8853 d1 = DECL_NAME (templ);
8856 else if (TREE_CODE (d1) == ENUMERAL_TYPE
8857 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
8859 templ = TYPE_TI_TEMPLATE (d1);
8860 d1 = DECL_NAME (templ);
8862 else if (DECL_TYPE_TEMPLATE_P (d1))
8864 templ = d1;
8865 d1 = DECL_NAME (templ);
8866 context = DECL_CONTEXT (templ);
8868 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
8870 templ = d1;
8871 d1 = DECL_NAME (templ);
8874 /* Issue an error message if we didn't find a template. */
8875 if (! templ)
8877 if (complain & tf_error)
8878 error ("%qT is not a template", d1);
8879 return error_mark_node;
8882 if (TREE_CODE (templ) != TEMPLATE_DECL
8883 /* Make sure it's a user visible template, if it was named by
8884 the user. */
8885 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
8886 && !PRIMARY_TEMPLATE_P (templ)))
8888 if (complain & tf_error)
8890 error ("non-template type %qT used as a template", d1);
8891 if (in_decl)
8892 error ("for template declaration %q+D", in_decl);
8894 return error_mark_node;
8897 complain &= ~tf_user;
8899 /* An alias that just changes the name of a template is equivalent to the
8900 other template, so if any of the arguments are pack expansions, strip
8901 the alias to avoid problems with a pack expansion passed to a non-pack
8902 alias template parameter (DR 1430). */
8903 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
8904 templ = get_underlying_template (templ);
8906 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
8908 tree parm;
8909 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
8910 if (arglist2 == error_mark_node
8911 || (!uses_template_parms (arglist2)
8912 && check_instantiated_args (templ, arglist2, complain)))
8913 return error_mark_node;
8915 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
8916 return parm;
8918 else
8920 tree template_type = TREE_TYPE (templ);
8921 tree gen_tmpl;
8922 tree type_decl;
8923 tree found = NULL_TREE;
8924 int arg_depth;
8925 int parm_depth;
8926 int is_dependent_type;
8927 int use_partial_inst_tmpl = false;
8929 if (template_type == error_mark_node)
8930 /* An error occurred while building the template TEMPL, and a
8931 diagnostic has most certainly been emitted for that
8932 already. Let's propagate that error. */
8933 return error_mark_node;
8935 gen_tmpl = most_general_template (templ);
8936 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
8937 parm_depth = TMPL_PARMS_DEPTH (parmlist);
8938 arg_depth = TMPL_ARGS_DEPTH (arglist);
8940 if (arg_depth == 1 && parm_depth > 1)
8942 /* We've been given an incomplete set of template arguments.
8943 For example, given:
8945 template <class T> struct S1 {
8946 template <class U> struct S2 {};
8947 template <class U> struct S2<U*> {};
8950 we will be called with an ARGLIST of `U*', but the
8951 TEMPLATE will be `template <class T> template
8952 <class U> struct S1<T>::S2'. We must fill in the missing
8953 arguments. */
8954 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
8955 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
8956 arg_depth = TMPL_ARGS_DEPTH (arglist);
8959 /* Now we should have enough arguments. */
8960 gcc_assert (parm_depth == arg_depth);
8962 /* From here on, we're only interested in the most general
8963 template. */
8965 /* Calculate the BOUND_ARGS. These will be the args that are
8966 actually tsubst'd into the definition to create the
8967 instantiation. */
8968 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
8969 complain,
8970 /*require_all_args=*/true,
8971 /*use_default_args=*/true);
8973 if (arglist == error_mark_node)
8974 /* We were unable to bind the arguments. */
8975 return error_mark_node;
8977 /* In the scope of a template class, explicit references to the
8978 template class refer to the type of the template, not any
8979 instantiation of it. For example, in:
8981 template <class T> class C { void f(C<T>); }
8983 the `C<T>' is just the same as `C'. Outside of the
8984 class, however, such a reference is an instantiation. */
8985 if (entering_scope
8986 || !PRIMARY_TEMPLATE_P (gen_tmpl)
8987 || currently_open_class (template_type))
8989 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
8991 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
8992 return template_type;
8995 /* If we already have this specialization, return it. */
8996 elt.tmpl = gen_tmpl;
8997 elt.args = arglist;
8998 elt.spec = NULL_TREE;
8999 hash = spec_hasher::hash (&elt);
9000 entry = type_specializations->find_with_hash (&elt, hash);
9002 if (entry)
9003 return entry->spec;
9005 /* If the the template's constraints are not satisfied,
9006 then we cannot form a valid type.
9008 Note that the check is deferred until after the hash
9009 lookup. This prevents redundant checks on previously
9010 instantiated specializations. */
9011 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
9013 if (complain & tf_error)
9015 error ("template constraint failure");
9016 diagnose_constraints (input_location, gen_tmpl, arglist);
9018 return error_mark_node;
9021 is_dependent_type = uses_template_parms (arglist);
9023 /* If the deduced arguments are invalid, then the binding
9024 failed. */
9025 if (!is_dependent_type
9026 && check_instantiated_args (gen_tmpl,
9027 INNERMOST_TEMPLATE_ARGS (arglist),
9028 complain))
9029 return error_mark_node;
9031 if (!is_dependent_type
9032 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9033 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
9034 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
9036 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
9037 DECL_NAME (gen_tmpl),
9038 /*tag_scope=*/ts_global);
9039 return found;
9042 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
9043 complain, in_decl);
9044 if (context == error_mark_node)
9045 return error_mark_node;
9047 if (!context)
9048 context = global_namespace;
9050 /* Create the type. */
9051 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9053 /* The user referred to a specialization of an alias
9054 template represented by GEN_TMPL.
9056 [temp.alias]/2 says:
9058 When a template-id refers to the specialization of an
9059 alias template, it is equivalent to the associated
9060 type obtained by substitution of its
9061 template-arguments for the template-parameters in the
9062 type-id of the alias template. */
9064 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
9065 /* Note that the call above (by indirectly calling
9066 register_specialization in tsubst_decl) registers the
9067 TYPE_DECL representing the specialization of the alias
9068 template. So next time someone substitutes ARGLIST for
9069 the template parms into the alias template (GEN_TMPL),
9070 she'll get that TYPE_DECL back. */
9072 if (t == error_mark_node)
9073 return t;
9075 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
9077 if (!is_dependent_type)
9079 set_current_access_from_decl (TYPE_NAME (template_type));
9080 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
9081 tsubst (ENUM_UNDERLYING_TYPE (template_type),
9082 arglist, complain, in_decl),
9083 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
9084 arglist, complain, in_decl),
9085 SCOPED_ENUM_P (template_type), NULL);
9087 if (t == error_mark_node)
9088 return t;
9090 else
9092 /* We don't want to call start_enum for this type, since
9093 the values for the enumeration constants may involve
9094 template parameters. And, no one should be interested
9095 in the enumeration constants for such a type. */
9096 t = cxx_make_type (ENUMERAL_TYPE);
9097 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
9099 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
9100 ENUM_FIXED_UNDERLYING_TYPE_P (t)
9101 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
9103 else if (CLASS_TYPE_P (template_type))
9105 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9106 instantiated here. */
9107 gcc_assert (!LAMBDA_TYPE_P (template_type));
9109 t = make_class_type (TREE_CODE (template_type));
9110 CLASSTYPE_DECLARED_CLASS (t)
9111 = CLASSTYPE_DECLARED_CLASS (template_type);
9112 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
9114 /* A local class. Make sure the decl gets registered properly. */
9115 if (context == current_function_decl)
9116 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
9118 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
9119 /* This instantiation is another name for the primary
9120 template type. Set the TYPE_CANONICAL field
9121 appropriately. */
9122 TYPE_CANONICAL (t) = template_type;
9123 else if (any_template_arguments_need_structural_equality_p (arglist))
9124 /* Some of the template arguments require structural
9125 equality testing, so this template class requires
9126 structural equality testing. */
9127 SET_TYPE_STRUCTURAL_EQUALITY (t);
9129 else
9130 gcc_unreachable ();
9132 /* If we called start_enum or pushtag above, this information
9133 will already be set up. */
9134 if (!TYPE_NAME (t))
9136 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
9138 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
9139 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
9140 DECL_SOURCE_LOCATION (type_decl)
9141 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
9143 else
9144 type_decl = TYPE_NAME (t);
9146 if (CLASS_TYPE_P (template_type))
9148 TREE_PRIVATE (type_decl)
9149 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
9150 TREE_PROTECTED (type_decl)
9151 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
9152 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
9154 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
9155 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
9159 if (OVERLOAD_TYPE_P (t)
9160 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9162 static const char *tags[] = {"abi_tag", "may_alias"};
9164 for (unsigned ix = 0; ix != 2; ix++)
9166 tree attributes
9167 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
9169 if (attributes)
9170 TYPE_ATTRIBUTES (t)
9171 = tree_cons (TREE_PURPOSE (attributes),
9172 TREE_VALUE (attributes),
9173 TYPE_ATTRIBUTES (t));
9177 /* Let's consider the explicit specialization of a member
9178 of a class template specialization that is implicitly instantiated,
9179 e.g.:
9180 template<class T>
9181 struct S
9183 template<class U> struct M {}; //#0
9186 template<>
9187 template<>
9188 struct S<int>::M<char> //#1
9190 int i;
9192 [temp.expl.spec]/4 says this is valid.
9194 In this case, when we write:
9195 S<int>::M<char> m;
9197 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9198 the one of #0.
9200 When we encounter #1, we want to store the partial instantiation
9201 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9203 For all cases other than this "explicit specialization of member of a
9204 class template", we just want to store the most general template into
9205 the CLASSTYPE_TI_TEMPLATE of M.
9207 This case of "explicit specialization of member of a class template"
9208 only happens when:
9209 1/ the enclosing class is an instantiation of, and therefore not
9210 the same as, the context of the most general template, and
9211 2/ we aren't looking at the partial instantiation itself, i.e.
9212 the innermost arguments are not the same as the innermost parms of
9213 the most general template.
9215 So it's only when 1/ and 2/ happens that we want to use the partial
9216 instantiation of the member template in lieu of its most general
9217 template. */
9219 if (PRIMARY_TEMPLATE_P (gen_tmpl)
9220 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
9221 /* the enclosing class must be an instantiation... */
9222 && CLASS_TYPE_P (context)
9223 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
9225 TREE_VEC_LENGTH (arglist)--;
9226 ++processing_template_decl;
9227 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
9228 tree partial_inst_args =
9229 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
9230 arglist, complain, NULL_TREE);
9231 --processing_template_decl;
9232 TREE_VEC_LENGTH (arglist)++;
9233 if (partial_inst_args == error_mark_node)
9234 return error_mark_node;
9235 use_partial_inst_tmpl =
9236 /*...and we must not be looking at the partial instantiation
9237 itself. */
9238 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
9239 partial_inst_args);
9242 if (!use_partial_inst_tmpl)
9243 /* This case is easy; there are no member templates involved. */
9244 found = gen_tmpl;
9245 else
9247 /* This is a full instantiation of a member template. Find
9248 the partial instantiation of which this is an instance. */
9250 /* Temporarily reduce by one the number of levels in the ARGLIST
9251 so as to avoid comparing the last set of arguments. */
9252 TREE_VEC_LENGTH (arglist)--;
9253 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
9254 TREE_VEC_LENGTH (arglist)++;
9255 /* FOUND is either a proper class type, or an alias
9256 template specialization. In the later case, it's a
9257 TYPE_DECL, resulting from the substituting of arguments
9258 for parameters in the TYPE_DECL of the alias template
9259 done earlier. So be careful while getting the template
9260 of FOUND. */
9261 found = (TREE_CODE (found) == TEMPLATE_DECL
9262 ? found
9263 : (TREE_CODE (found) == TYPE_DECL
9264 ? DECL_TI_TEMPLATE (found)
9265 : CLASSTYPE_TI_TEMPLATE (found)));
9268 // Build template info for the new specialization.
9269 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
9271 elt.spec = t;
9272 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
9273 entry = ggc_alloc<spec_entry> ();
9274 *entry = elt;
9275 *slot = entry;
9277 /* Note this use of the partial instantiation so we can check it
9278 later in maybe_process_partial_specialization. */
9279 DECL_TEMPLATE_INSTANTIATIONS (found)
9280 = tree_cons (arglist, t,
9281 DECL_TEMPLATE_INSTANTIATIONS (found));
9283 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
9284 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9285 /* Now that the type has been registered on the instantiations
9286 list, we set up the enumerators. Because the enumeration
9287 constants may involve the enumeration type itself, we make
9288 sure to register the type first, and then create the
9289 constants. That way, doing tsubst_expr for the enumeration
9290 constants won't result in recursive calls here; we'll find
9291 the instantiation and exit above. */
9292 tsubst_enum (template_type, t, arglist);
9294 if (CLASS_TYPE_P (template_type) && is_dependent_type)
9295 /* If the type makes use of template parameters, the
9296 code that generates debugging information will crash. */
9297 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
9299 /* Possibly limit visibility based on template args. */
9300 TREE_PUBLIC (type_decl) = 1;
9301 determine_visibility (type_decl);
9303 inherit_targ_abi_tags (t);
9305 return t;
9309 /* Wrapper for lookup_template_class_1. */
9311 tree
9312 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
9313 int entering_scope, tsubst_flags_t complain)
9315 tree ret;
9316 timevar_push (TV_TEMPLATE_INST);
9317 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
9318 entering_scope, complain);
9319 timevar_pop (TV_TEMPLATE_INST);
9320 return ret;
9323 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9325 tree
9326 lookup_template_variable (tree templ, tree arglist)
9328 /* The type of the expression is NULL_TREE since the template-id could refer
9329 to an explicit or partial specialization. */
9330 tree type = NULL_TREE;
9331 if (flag_concepts && variable_concept_p (templ))
9332 /* Except that concepts are always bool. */
9333 type = boolean_type_node;
9334 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
9337 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9339 tree
9340 finish_template_variable (tree var, tsubst_flags_t complain)
9342 tree templ = TREE_OPERAND (var, 0);
9343 tree arglist = TREE_OPERAND (var, 1);
9345 /* We never want to return a VAR_DECL for a variable concept, since they
9346 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9347 bool concept_p = flag_concepts && variable_concept_p (templ);
9348 if (concept_p && processing_template_decl)
9349 return var;
9351 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
9352 arglist = add_outermost_template_args (tmpl_args, arglist);
9354 templ = most_general_template (templ);
9355 tree parms = DECL_TEMPLATE_PARMS (templ);
9356 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
9357 /*req_all*/true,
9358 /*use_default*/true);
9360 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
9362 if (complain & tf_error)
9364 error ("use of invalid variable template %qE", var);
9365 diagnose_constraints (location_of (var), templ, arglist);
9367 return error_mark_node;
9370 /* If a template-id refers to a specialization of a variable
9371 concept, then the expression is true if and only if the
9372 concept's constraints are satisfied by the given template
9373 arguments.
9375 NOTE: This is an extension of Concepts Lite TS that
9376 allows constraints to be used in expressions. */
9377 if (concept_p)
9379 tree decl = DECL_TEMPLATE_RESULT (templ);
9380 return evaluate_variable_concept (decl, arglist);
9383 return instantiate_template (templ, arglist, complain);
9386 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9387 TARGS template args, and instantiate it if it's not dependent. */
9389 tree
9390 lookup_and_finish_template_variable (tree templ, tree targs,
9391 tsubst_flags_t complain)
9393 templ = lookup_template_variable (templ, targs);
9394 if (!any_dependent_template_arguments_p (targs))
9396 templ = finish_template_variable (templ, complain);
9397 mark_used (templ);
9400 return convert_from_reference (templ);
9404 struct pair_fn_data
9406 tree_fn_t fn;
9407 tree_fn_t any_fn;
9408 void *data;
9409 /* True when we should also visit template parameters that occur in
9410 non-deduced contexts. */
9411 bool include_nondeduced_p;
9412 hash_set<tree> *visited;
9415 /* Called from for_each_template_parm via walk_tree. */
9417 static tree
9418 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
9420 tree t = *tp;
9421 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
9422 tree_fn_t fn = pfd->fn;
9423 void *data = pfd->data;
9424 tree result = NULL_TREE;
9426 #define WALK_SUBTREE(NODE) \
9427 do \
9429 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9430 pfd->include_nondeduced_p, \
9431 pfd->any_fn); \
9432 if (result) goto out; \
9434 while (0)
9436 if (pfd->any_fn && (*pfd->any_fn)(t, data))
9437 return t;
9439 if (TYPE_P (t)
9440 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
9441 WALK_SUBTREE (TYPE_CONTEXT (t));
9443 switch (TREE_CODE (t))
9445 case RECORD_TYPE:
9446 if (TYPE_PTRMEMFUNC_P (t))
9447 break;
9448 /* Fall through. */
9450 case UNION_TYPE:
9451 case ENUMERAL_TYPE:
9452 if (!TYPE_TEMPLATE_INFO (t))
9453 *walk_subtrees = 0;
9454 else
9455 WALK_SUBTREE (TYPE_TI_ARGS (t));
9456 break;
9458 case INTEGER_TYPE:
9459 WALK_SUBTREE (TYPE_MIN_VALUE (t));
9460 WALK_SUBTREE (TYPE_MAX_VALUE (t));
9461 break;
9463 case METHOD_TYPE:
9464 /* Since we're not going to walk subtrees, we have to do this
9465 explicitly here. */
9466 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
9467 /* Fall through. */
9469 case FUNCTION_TYPE:
9470 /* Check the return type. */
9471 WALK_SUBTREE (TREE_TYPE (t));
9473 /* Check the parameter types. Since default arguments are not
9474 instantiated until they are needed, the TYPE_ARG_TYPES may
9475 contain expressions that involve template parameters. But,
9476 no-one should be looking at them yet. And, once they're
9477 instantiated, they don't contain template parameters, so
9478 there's no point in looking at them then, either. */
9480 tree parm;
9482 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
9483 WALK_SUBTREE (TREE_VALUE (parm));
9485 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9486 want walk_tree walking into them itself. */
9487 *walk_subtrees = 0;
9490 if (flag_noexcept_type)
9492 tree spec = TYPE_RAISES_EXCEPTIONS (t);
9493 if (spec)
9494 WALK_SUBTREE (TREE_PURPOSE (spec));
9496 break;
9498 case TYPEOF_TYPE:
9499 case UNDERLYING_TYPE:
9500 if (pfd->include_nondeduced_p
9501 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
9502 pfd->visited,
9503 pfd->include_nondeduced_p,
9504 pfd->any_fn))
9505 return error_mark_node;
9506 break;
9508 case FUNCTION_DECL:
9509 case VAR_DECL:
9510 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9511 WALK_SUBTREE (DECL_TI_ARGS (t));
9512 /* Fall through. */
9514 case PARM_DECL:
9515 case CONST_DECL:
9516 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
9517 WALK_SUBTREE (DECL_INITIAL (t));
9518 if (DECL_CONTEXT (t)
9519 && pfd->include_nondeduced_p)
9520 WALK_SUBTREE (DECL_CONTEXT (t));
9521 break;
9523 case BOUND_TEMPLATE_TEMPLATE_PARM:
9524 /* Record template parameters such as `T' inside `TT<T>'. */
9525 WALK_SUBTREE (TYPE_TI_ARGS (t));
9526 /* Fall through. */
9528 case TEMPLATE_TEMPLATE_PARM:
9529 case TEMPLATE_TYPE_PARM:
9530 case TEMPLATE_PARM_INDEX:
9531 if (fn && (*fn)(t, data))
9532 return t;
9533 else if (!fn)
9534 return t;
9535 break;
9537 case TEMPLATE_DECL:
9538 /* A template template parameter is encountered. */
9539 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9540 WALK_SUBTREE (TREE_TYPE (t));
9542 /* Already substituted template template parameter */
9543 *walk_subtrees = 0;
9544 break;
9546 case TYPENAME_TYPE:
9547 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9548 partial instantiation. */
9549 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
9550 break;
9552 case CONSTRUCTOR:
9553 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
9554 && pfd->include_nondeduced_p)
9555 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
9556 break;
9558 case INDIRECT_REF:
9559 case COMPONENT_REF:
9560 /* If there's no type, then this thing must be some expression
9561 involving template parameters. */
9562 if (!fn && !TREE_TYPE (t))
9563 return error_mark_node;
9564 break;
9566 case MODOP_EXPR:
9567 case CAST_EXPR:
9568 case IMPLICIT_CONV_EXPR:
9569 case REINTERPRET_CAST_EXPR:
9570 case CONST_CAST_EXPR:
9571 case STATIC_CAST_EXPR:
9572 case DYNAMIC_CAST_EXPR:
9573 case ARROW_EXPR:
9574 case DOTSTAR_EXPR:
9575 case TYPEID_EXPR:
9576 case PSEUDO_DTOR_EXPR:
9577 if (!fn)
9578 return error_mark_node;
9579 break;
9581 default:
9582 break;
9585 #undef WALK_SUBTREE
9587 /* We didn't find any template parameters we liked. */
9588 out:
9589 return result;
9592 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9593 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9594 call FN with the parameter and the DATA.
9595 If FN returns nonzero, the iteration is terminated, and
9596 for_each_template_parm returns 1. Otherwise, the iteration
9597 continues. If FN never returns a nonzero value, the value
9598 returned by for_each_template_parm is 0. If FN is NULL, it is
9599 considered to be the function which always returns 1.
9601 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9602 parameters that occur in non-deduced contexts. When false, only
9603 visits those template parameters that can be deduced. */
9605 static tree
9606 for_each_template_parm (tree t, tree_fn_t fn, void* data,
9607 hash_set<tree> *visited,
9608 bool include_nondeduced_p,
9609 tree_fn_t any_fn)
9611 struct pair_fn_data pfd;
9612 tree result;
9614 /* Set up. */
9615 pfd.fn = fn;
9616 pfd.any_fn = any_fn;
9617 pfd.data = data;
9618 pfd.include_nondeduced_p = include_nondeduced_p;
9620 /* Walk the tree. (Conceptually, we would like to walk without
9621 duplicates, but for_each_template_parm_r recursively calls
9622 for_each_template_parm, so we would need to reorganize a fair
9623 bit to use walk_tree_without_duplicates, so we keep our own
9624 visited list.) */
9625 if (visited)
9626 pfd.visited = visited;
9627 else
9628 pfd.visited = new hash_set<tree>;
9629 result = cp_walk_tree (&t,
9630 for_each_template_parm_r,
9631 &pfd,
9632 pfd.visited);
9634 /* Clean up. */
9635 if (!visited)
9637 delete pfd.visited;
9638 pfd.visited = 0;
9641 return result;
9644 /* Returns true if T depends on any template parameter. */
9647 uses_template_parms (tree t)
9649 if (t == NULL_TREE)
9650 return false;
9652 bool dependent_p;
9653 int saved_processing_template_decl;
9655 saved_processing_template_decl = processing_template_decl;
9656 if (!saved_processing_template_decl)
9657 processing_template_decl = 1;
9658 if (TYPE_P (t))
9659 dependent_p = dependent_type_p (t);
9660 else if (TREE_CODE (t) == TREE_VEC)
9661 dependent_p = any_dependent_template_arguments_p (t);
9662 else if (TREE_CODE (t) == TREE_LIST)
9663 dependent_p = (uses_template_parms (TREE_VALUE (t))
9664 || uses_template_parms (TREE_CHAIN (t)));
9665 else if (TREE_CODE (t) == TYPE_DECL)
9666 dependent_p = dependent_type_p (TREE_TYPE (t));
9667 else if (DECL_P (t)
9668 || EXPR_P (t)
9669 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
9670 || TREE_CODE (t) == OVERLOAD
9671 || BASELINK_P (t)
9672 || identifier_p (t)
9673 || TREE_CODE (t) == TRAIT_EXPR
9674 || TREE_CODE (t) == CONSTRUCTOR
9675 || CONSTANT_CLASS_P (t))
9676 dependent_p = (type_dependent_expression_p (t)
9677 || value_dependent_expression_p (t));
9678 else
9680 gcc_assert (t == error_mark_node);
9681 dependent_p = false;
9684 processing_template_decl = saved_processing_template_decl;
9686 return dependent_p;
9689 /* Returns true iff current_function_decl is an incompletely instantiated
9690 template. Useful instead of processing_template_decl because the latter
9691 is set to 0 during instantiate_non_dependent_expr. */
9693 bool
9694 in_template_function (void)
9696 tree fn = current_function_decl;
9697 bool ret;
9698 ++processing_template_decl;
9699 ret = (fn && DECL_LANG_SPECIFIC (fn)
9700 && DECL_TEMPLATE_INFO (fn)
9701 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
9702 --processing_template_decl;
9703 return ret;
9706 /* Returns true if T depends on any template parameter with level LEVEL. */
9708 bool
9709 uses_template_parms_level (tree t, int level)
9711 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
9712 /*include_nondeduced_p=*/true);
9715 /* Returns true if the signature of DECL depends on any template parameter from
9716 its enclosing class. */
9718 bool
9719 uses_outer_template_parms (tree decl)
9721 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
9722 if (depth == 0)
9723 return false;
9724 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
9725 &depth, NULL, /*include_nondeduced_p=*/true))
9726 return true;
9727 if (PRIMARY_TEMPLATE_P (decl)
9728 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9729 (DECL_TEMPLATE_PARMS (decl)),
9730 template_parm_outer_level,
9731 &depth, NULL, /*include_nondeduced_p=*/true))
9732 return true;
9733 tree ci = get_constraints (decl);
9734 if (ci)
9735 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
9736 if (ci && for_each_template_parm (ci, template_parm_outer_level,
9737 &depth, NULL, /*nondeduced*/true))
9738 return true;
9739 return false;
9742 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9743 ill-formed translation unit, i.e. a variable or function that isn't
9744 usable in a constant expression. */
9746 static inline bool
9747 neglectable_inst_p (tree d)
9749 return (DECL_P (d)
9750 && !undeduced_auto_decl (d)
9751 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
9752 : decl_maybe_constant_var_p (d)));
9755 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9756 neglectable and instantiated from within an erroneous instantiation. */
9758 static bool
9759 limit_bad_template_recursion (tree decl)
9761 struct tinst_level *lev = current_tinst_level;
9762 int errs = errorcount + sorrycount;
9763 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
9764 return false;
9766 for (; lev; lev = lev->next)
9767 if (neglectable_inst_p (lev->decl))
9768 break;
9770 return (lev && errs > lev->errors);
9773 static int tinst_depth;
9774 extern int max_tinst_depth;
9775 int depth_reached;
9777 static GTY(()) struct tinst_level *last_error_tinst_level;
9779 /* We're starting to instantiate D; record the template instantiation context
9780 for diagnostics and to restore it later. */
9782 bool
9783 push_tinst_level (tree d)
9785 return push_tinst_level_loc (d, input_location);
9788 /* We're starting to instantiate D; record the template instantiation context
9789 at LOC for diagnostics and to restore it later. */
9791 bool
9792 push_tinst_level_loc (tree d, location_t loc)
9794 struct tinst_level *new_level;
9796 if (tinst_depth >= max_tinst_depth)
9798 /* Tell error.c not to try to instantiate any templates. */
9799 at_eof = 2;
9800 fatal_error (input_location,
9801 "template instantiation depth exceeds maximum of %d"
9802 " (use -ftemplate-depth= to increase the maximum)",
9803 max_tinst_depth);
9804 return false;
9807 /* If the current instantiation caused problems, don't let it instantiate
9808 anything else. Do allow deduction substitution and decls usable in
9809 constant expressions. */
9810 if (limit_bad_template_recursion (d))
9811 return false;
9813 /* When not -quiet, dump template instantiations other than functions, since
9814 announce_function will take care of those. */
9815 if (!quiet_flag
9816 && TREE_CODE (d) != TREE_LIST
9817 && TREE_CODE (d) != FUNCTION_DECL)
9818 fprintf (stderr, " %s", decl_as_string (d, TFF_DECL_SPECIFIERS));
9820 new_level = ggc_alloc<tinst_level> ();
9821 new_level->decl = d;
9822 new_level->locus = loc;
9823 new_level->errors = errorcount+sorrycount;
9824 new_level->in_system_header_p = in_system_header_at (input_location);
9825 new_level->next = current_tinst_level;
9826 current_tinst_level = new_level;
9828 ++tinst_depth;
9829 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
9830 depth_reached = tinst_depth;
9832 return true;
9835 /* We're done instantiating this template; return to the instantiation
9836 context. */
9838 void
9839 pop_tinst_level (void)
9841 /* Restore the filename and line number stashed away when we started
9842 this instantiation. */
9843 input_location = current_tinst_level->locus;
9844 current_tinst_level = current_tinst_level->next;
9845 --tinst_depth;
9848 /* We're instantiating a deferred template; restore the template
9849 instantiation context in which the instantiation was requested, which
9850 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9852 static tree
9853 reopen_tinst_level (struct tinst_level *level)
9855 struct tinst_level *t;
9857 tinst_depth = 0;
9858 for (t = level; t; t = t->next)
9859 ++tinst_depth;
9861 current_tinst_level = level;
9862 pop_tinst_level ();
9863 if (current_tinst_level)
9864 current_tinst_level->errors = errorcount+sorrycount;
9865 return level->decl;
9868 /* Returns the TINST_LEVEL which gives the original instantiation
9869 context. */
9871 struct tinst_level *
9872 outermost_tinst_level (void)
9874 struct tinst_level *level = current_tinst_level;
9875 if (level)
9876 while (level->next)
9877 level = level->next;
9878 return level;
9881 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9882 vector of template arguments, as for tsubst.
9884 Returns an appropriate tsubst'd friend declaration. */
9886 static tree
9887 tsubst_friend_function (tree decl, tree args)
9889 tree new_friend;
9891 if (TREE_CODE (decl) == FUNCTION_DECL
9892 && DECL_TEMPLATE_INSTANTIATION (decl)
9893 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
9894 /* This was a friend declared with an explicit template
9895 argument list, e.g.:
9897 friend void f<>(T);
9899 to indicate that f was a template instantiation, not a new
9900 function declaration. Now, we have to figure out what
9901 instantiation of what template. */
9903 tree template_id, arglist, fns;
9904 tree new_args;
9905 tree tmpl;
9906 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
9908 /* Friend functions are looked up in the containing namespace scope.
9909 We must enter that scope, to avoid finding member functions of the
9910 current class with same name. */
9911 push_nested_namespace (ns);
9912 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
9913 tf_warning_or_error, NULL_TREE,
9914 /*integral_constant_expression_p=*/false);
9915 pop_nested_namespace (ns);
9916 arglist = tsubst (DECL_TI_ARGS (decl), args,
9917 tf_warning_or_error, NULL_TREE);
9918 template_id = lookup_template_function (fns, arglist);
9920 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9921 tmpl = determine_specialization (template_id, new_friend,
9922 &new_args,
9923 /*need_member_template=*/0,
9924 TREE_VEC_LENGTH (args),
9925 tsk_none);
9926 return instantiate_template (tmpl, new_args, tf_error);
9929 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9931 /* The NEW_FRIEND will look like an instantiation, to the
9932 compiler, but is not an instantiation from the point of view of
9933 the language. For example, we might have had:
9935 template <class T> struct S {
9936 template <class U> friend void f(T, U);
9939 Then, in S<int>, template <class U> void f(int, U) is not an
9940 instantiation of anything. */
9941 if (new_friend == error_mark_node)
9942 return error_mark_node;
9944 DECL_USE_TEMPLATE (new_friend) = 0;
9945 if (TREE_CODE (decl) == TEMPLATE_DECL)
9947 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
9948 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
9949 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
9952 /* The mangled name for the NEW_FRIEND is incorrect. The function
9953 is not a template instantiation and should not be mangled like
9954 one. Therefore, we forget the mangling here; we'll recompute it
9955 later if we need it. */
9956 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
9958 SET_DECL_RTL (new_friend, NULL);
9959 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
9962 if (DECL_NAMESPACE_SCOPE_P (new_friend))
9964 tree old_decl;
9965 tree new_friend_template_info;
9966 tree new_friend_result_template_info;
9967 tree ns;
9968 int new_friend_is_defn;
9970 /* We must save some information from NEW_FRIEND before calling
9971 duplicate decls since that function will free NEW_FRIEND if
9972 possible. */
9973 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
9974 new_friend_is_defn =
9975 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9976 (template_for_substitution (new_friend)))
9977 != NULL_TREE);
9978 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
9980 /* This declaration is a `primary' template. */
9981 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
9983 new_friend_result_template_info
9984 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
9986 else
9987 new_friend_result_template_info = NULL_TREE;
9989 /* Inside pushdecl_namespace_level, we will push into the
9990 current namespace. However, the friend function should go
9991 into the namespace of the template. */
9992 ns = decl_namespace_context (new_friend);
9993 push_nested_namespace (ns);
9994 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
9995 pop_nested_namespace (ns);
9997 if (old_decl == error_mark_node)
9998 return error_mark_node;
10000 if (old_decl != new_friend)
10002 /* This new friend declaration matched an existing
10003 declaration. For example, given:
10005 template <class T> void f(T);
10006 template <class U> class C {
10007 template <class T> friend void f(T) {}
10010 the friend declaration actually provides the definition
10011 of `f', once C has been instantiated for some type. So,
10012 old_decl will be the out-of-class template declaration,
10013 while new_friend is the in-class definition.
10015 But, if `f' was called before this point, the
10016 instantiation of `f' will have DECL_TI_ARGS corresponding
10017 to `T' but not to `U', references to which might appear
10018 in the definition of `f'. Previously, the most general
10019 template for an instantiation of `f' was the out-of-class
10020 version; now it is the in-class version. Therefore, we
10021 run through all specialization of `f', adding to their
10022 DECL_TI_ARGS appropriately. In particular, they need a
10023 new set of outer arguments, corresponding to the
10024 arguments for this class instantiation.
10026 The same situation can arise with something like this:
10028 friend void f(int);
10029 template <class T> class C {
10030 friend void f(T) {}
10033 when `C<int>' is instantiated. Now, `f(int)' is defined
10034 in the class. */
10036 if (!new_friend_is_defn)
10037 /* On the other hand, if the in-class declaration does
10038 *not* provide a definition, then we don't want to alter
10039 existing definitions. We can just leave everything
10040 alone. */
10042 else
10044 tree new_template = TI_TEMPLATE (new_friend_template_info);
10045 tree new_args = TI_ARGS (new_friend_template_info);
10047 /* Overwrite whatever template info was there before, if
10048 any, with the new template information pertaining to
10049 the declaration. */
10050 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
10052 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
10054 /* We should have called reregister_specialization in
10055 duplicate_decls. */
10056 gcc_assert (retrieve_specialization (new_template,
10057 new_args, 0)
10058 == old_decl);
10060 /* Instantiate it if the global has already been used. */
10061 if (DECL_ODR_USED (old_decl))
10062 instantiate_decl (old_decl, /*defer_ok=*/true,
10063 /*expl_inst_class_mem_p=*/false);
10065 else
10067 tree t;
10069 /* Indicate that the old function template is a partial
10070 instantiation. */
10071 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
10072 = new_friend_result_template_info;
10074 gcc_assert (new_template
10075 == most_general_template (new_template));
10076 gcc_assert (new_template != old_decl);
10078 /* Reassign any specializations already in the hash table
10079 to the new more general template, and add the
10080 additional template args. */
10081 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
10082 t != NULL_TREE;
10083 t = TREE_CHAIN (t))
10085 tree spec = TREE_VALUE (t);
10086 spec_entry elt;
10088 elt.tmpl = old_decl;
10089 elt.args = DECL_TI_ARGS (spec);
10090 elt.spec = NULL_TREE;
10092 decl_specializations->remove_elt (&elt);
10094 DECL_TI_ARGS (spec)
10095 = add_outermost_template_args (new_args,
10096 DECL_TI_ARGS (spec));
10098 register_specialization
10099 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
10102 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
10106 /* The information from NEW_FRIEND has been merged into OLD_DECL
10107 by duplicate_decls. */
10108 new_friend = old_decl;
10111 else
10113 tree context = DECL_CONTEXT (new_friend);
10114 bool dependent_p;
10116 /* In the code
10117 template <class T> class C {
10118 template <class U> friend void C1<U>::f (); // case 1
10119 friend void C2<T>::f (); // case 2
10121 we only need to make sure CONTEXT is a complete type for
10122 case 2. To distinguish between the two cases, we note that
10123 CONTEXT of case 1 remains dependent type after tsubst while
10124 this isn't true for case 2. */
10125 ++processing_template_decl;
10126 dependent_p = dependent_type_p (context);
10127 --processing_template_decl;
10129 if (!dependent_p
10130 && !complete_type_or_else (context, NULL_TREE))
10131 return error_mark_node;
10133 if (COMPLETE_TYPE_P (context))
10135 tree fn = new_friend;
10136 /* do_friend adds the TEMPLATE_DECL for any member friend
10137 template even if it isn't a member template, i.e.
10138 template <class T> friend A<T>::f();
10139 Look through it in that case. */
10140 if (TREE_CODE (fn) == TEMPLATE_DECL
10141 && !PRIMARY_TEMPLATE_P (fn))
10142 fn = DECL_TEMPLATE_RESULT (fn);
10143 /* Check to see that the declaration is really present, and,
10144 possibly obtain an improved declaration. */
10145 fn = check_classfn (context, fn, NULL_TREE);
10147 if (fn)
10148 new_friend = fn;
10152 return new_friend;
10155 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
10156 template arguments, as for tsubst.
10158 Returns an appropriate tsubst'd friend type or error_mark_node on
10159 failure. */
10161 static tree
10162 tsubst_friend_class (tree friend_tmpl, tree args)
10164 tree tmpl;
10166 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
10168 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
10169 return TREE_TYPE (tmpl);
10172 tree context = CP_DECL_CONTEXT (friend_tmpl);
10173 if (TREE_CODE (context) == NAMESPACE_DECL)
10174 push_nested_namespace (context);
10175 else
10176 push_nested_class (context);
10178 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), /*prefer_type=*/false,
10179 /*non_class=*/false, /*block_p=*/false,
10180 /*namespaces_only=*/false, LOOKUP_HIDDEN);
10182 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
10184 /* The friend template has already been declared. Just
10185 check to see that the declarations match, and install any new
10186 default parameters. We must tsubst the default parameters,
10187 of course. We only need the innermost template parameters
10188 because that is all that redeclare_class_template will look
10189 at. */
10190 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
10191 > TMPL_ARGS_DEPTH (args))
10193 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
10194 args, tf_warning_or_error);
10195 location_t saved_input_location = input_location;
10196 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
10197 tree cons = get_constraints (tmpl);
10198 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
10199 input_location = saved_input_location;
10202 else
10204 /* The friend template has not already been declared. In this
10205 case, the instantiation of the template class will cause the
10206 injection of this template into the namespace scope. */
10207 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
10209 if (tmpl != error_mark_node)
10211 /* The new TMPL is not an instantiation of anything, so we
10212 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
10213 for the new type because that is supposed to be the
10214 corresponding template decl, i.e., TMPL. */
10215 DECL_USE_TEMPLATE (tmpl) = 0;
10216 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
10217 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
10218 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
10219 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
10221 /* It is hidden. */
10222 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl));
10223 DECL_ANTICIPATED (tmpl)
10224 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl)) = true;
10226 /* Inject this template into the enclosing namspace scope. */
10227 tmpl = pushdecl_namespace_level (tmpl, true);
10231 if (TREE_CODE (context) == NAMESPACE_DECL)
10232 pop_nested_namespace (context);
10233 else
10234 pop_nested_class ();
10236 return TREE_TYPE (tmpl);
10239 /* Returns zero if TYPE cannot be completed later due to circularity.
10240 Otherwise returns one. */
10242 static int
10243 can_complete_type_without_circularity (tree type)
10245 if (type == NULL_TREE || type == error_mark_node)
10246 return 0;
10247 else if (COMPLETE_TYPE_P (type))
10248 return 1;
10249 else if (TREE_CODE (type) == ARRAY_TYPE)
10250 return can_complete_type_without_circularity (TREE_TYPE (type));
10251 else if (CLASS_TYPE_P (type)
10252 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
10253 return 0;
10254 else
10255 return 1;
10258 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
10259 tsubst_flags_t, tree);
10261 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10262 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10264 static tree
10265 tsubst_attribute (tree t, tree *decl_p, tree args,
10266 tsubst_flags_t complain, tree in_decl)
10268 gcc_assert (ATTR_IS_DEPENDENT (t));
10270 tree val = TREE_VALUE (t);
10271 if (val == NULL_TREE)
10272 /* Nothing to do. */;
10273 else if ((flag_openmp || flag_openmp_simd)
10274 && is_attribute_p ("omp declare simd",
10275 get_attribute_name (t)))
10277 tree clauses = TREE_VALUE (val);
10278 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
10279 complain, in_decl);
10280 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
10281 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
10282 tree parms = DECL_ARGUMENTS (*decl_p);
10283 clauses
10284 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
10285 if (clauses)
10286 val = build_tree_list (NULL_TREE, clauses);
10287 else
10288 val = NULL_TREE;
10290 /* If the first attribute argument is an identifier, don't
10291 pass it through tsubst. Attributes like mode, format,
10292 cleanup and several target specific attributes expect it
10293 unmodified. */
10294 else if (attribute_takes_identifier_p (get_attribute_name (t)))
10296 tree chain
10297 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
10298 /*integral_constant_expression_p=*/false);
10299 if (chain != TREE_CHAIN (val))
10300 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
10302 else if (PACK_EXPANSION_P (val))
10304 /* An attribute pack expansion. */
10305 tree purp = TREE_PURPOSE (t);
10306 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
10307 if (pack == error_mark_node)
10308 return error_mark_node;
10309 int len = TREE_VEC_LENGTH (pack);
10310 tree list = NULL_TREE;
10311 tree *q = &list;
10312 for (int i = 0; i < len; ++i)
10314 tree elt = TREE_VEC_ELT (pack, i);
10315 *q = build_tree_list (purp, elt);
10316 q = &TREE_CHAIN (*q);
10318 return list;
10320 else
10321 val = tsubst_expr (val, args, complain, in_decl,
10322 /*integral_constant_expression_p=*/false);
10324 if (val != TREE_VALUE (t))
10325 return build_tree_list (TREE_PURPOSE (t), val);
10326 return t;
10329 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10330 unchanged or a new TREE_LIST chain. */
10332 static tree
10333 tsubst_attributes (tree attributes, tree args,
10334 tsubst_flags_t complain, tree in_decl)
10336 tree last_dep = NULL_TREE;
10338 for (tree t = attributes; t; t = TREE_CHAIN (t))
10339 if (ATTR_IS_DEPENDENT (t))
10341 last_dep = t;
10342 attributes = copy_list (attributes);
10343 break;
10346 if (last_dep)
10347 for (tree *p = &attributes; *p; )
10349 tree t = *p;
10350 if (ATTR_IS_DEPENDENT (t))
10352 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
10353 if (subst != t)
10355 *p = subst;
10356 while (*p)
10357 p = &TREE_CHAIN (*p);
10358 *p = TREE_CHAIN (t);
10359 continue;
10362 p = &TREE_CHAIN (*p);
10365 return attributes;
10368 /* Apply any attributes which had to be deferred until instantiation
10369 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10370 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10372 static void
10373 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
10374 tree args, tsubst_flags_t complain, tree in_decl)
10376 tree last_dep = NULL_TREE;
10377 tree t;
10378 tree *p;
10380 if (attributes == NULL_TREE)
10381 return;
10383 if (DECL_P (*decl_p))
10385 if (TREE_TYPE (*decl_p) == error_mark_node)
10386 return;
10387 p = &DECL_ATTRIBUTES (*decl_p);
10388 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10389 to our attributes parameter. */
10390 gcc_assert (*p == attributes);
10392 else
10394 p = &TYPE_ATTRIBUTES (*decl_p);
10395 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10396 lookup_template_class_1, and should be preserved. */
10397 gcc_assert (*p != attributes);
10398 while (*p)
10399 p = &TREE_CHAIN (*p);
10402 for (t = attributes; t; t = TREE_CHAIN (t))
10403 if (ATTR_IS_DEPENDENT (t))
10405 last_dep = t;
10406 attributes = copy_list (attributes);
10407 break;
10410 *p = attributes;
10411 if (last_dep)
10413 tree late_attrs = NULL_TREE;
10414 tree *q = &late_attrs;
10416 for (; *p; )
10418 t = *p;
10419 if (ATTR_IS_DEPENDENT (t))
10421 *p = TREE_CHAIN (t);
10422 TREE_CHAIN (t) = NULL_TREE;
10423 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
10424 while (*q)
10425 q = &TREE_CHAIN (*q);
10427 else
10428 p = &TREE_CHAIN (t);
10431 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
10435 /* Perform (or defer) access check for typedefs that were referenced
10436 from within the template TMPL code.
10437 This is a subroutine of instantiate_decl and instantiate_class_template.
10438 TMPL is the template to consider and TARGS is the list of arguments of
10439 that template. */
10441 static void
10442 perform_typedefs_access_check (tree tmpl, tree targs)
10444 location_t saved_location;
10445 unsigned i;
10446 qualified_typedef_usage_t *iter;
10448 if (!tmpl
10449 || (!CLASS_TYPE_P (tmpl)
10450 && TREE_CODE (tmpl) != FUNCTION_DECL))
10451 return;
10453 saved_location = input_location;
10454 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
10456 tree type_decl = iter->typedef_decl;
10457 tree type_scope = iter->context;
10459 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
10460 continue;
10462 if (uses_template_parms (type_decl))
10463 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
10464 if (uses_template_parms (type_scope))
10465 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
10467 /* Make access check error messages point to the location
10468 of the use of the typedef. */
10469 input_location = iter->locus;
10470 perform_or_defer_access_check (TYPE_BINFO (type_scope),
10471 type_decl, type_decl,
10472 tf_warning_or_error);
10474 input_location = saved_location;
10477 static tree
10478 instantiate_class_template_1 (tree type)
10480 tree templ, args, pattern, t, member;
10481 tree typedecl;
10482 tree pbinfo;
10483 tree base_list;
10484 unsigned int saved_maximum_field_alignment;
10485 tree fn_context;
10487 if (type == error_mark_node)
10488 return error_mark_node;
10490 if (COMPLETE_OR_OPEN_TYPE_P (type)
10491 || uses_template_parms (type))
10492 return type;
10494 /* Figure out which template is being instantiated. */
10495 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
10496 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
10498 /* Mark the type as in the process of being defined. */
10499 TYPE_BEING_DEFINED (type) = 1;
10501 /* Determine what specialization of the original template to
10502 instantiate. */
10503 t = most_specialized_partial_spec (type, tf_warning_or_error);
10504 if (t == error_mark_node)
10505 return error_mark_node;
10506 else if (t)
10508 /* This TYPE is actually an instantiation of a partial
10509 specialization. We replace the innermost set of ARGS with
10510 the arguments appropriate for substitution. For example,
10511 given:
10513 template <class T> struct S {};
10514 template <class T> struct S<T*> {};
10516 and supposing that we are instantiating S<int*>, ARGS will
10517 presently be {int*} -- but we need {int}. */
10518 pattern = TREE_TYPE (t);
10519 args = TREE_PURPOSE (t);
10521 else
10523 pattern = TREE_TYPE (templ);
10524 args = CLASSTYPE_TI_ARGS (type);
10527 /* If the template we're instantiating is incomplete, then clearly
10528 there's nothing we can do. */
10529 if (!COMPLETE_TYPE_P (pattern))
10531 /* We can try again later. */
10532 TYPE_BEING_DEFINED (type) = 0;
10533 return type;
10536 /* If we've recursively instantiated too many templates, stop. */
10537 if (! push_tinst_level (type))
10538 return type;
10540 /* We may be in the middle of deferred access check. Disable
10541 it now. */
10542 push_deferring_access_checks (dk_no_deferred);
10544 int saved_unevaluated_operand = cp_unevaluated_operand;
10545 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10547 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
10548 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10549 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
10550 fn_context = error_mark_node;
10551 if (!fn_context)
10552 push_to_top_level ();
10553 else
10555 cp_unevaluated_operand = 0;
10556 c_inhibit_evaluation_warnings = 0;
10558 /* Use #pragma pack from the template context. */
10559 saved_maximum_field_alignment = maximum_field_alignment;
10560 maximum_field_alignment = TYPE_PRECISION (pattern);
10562 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
10564 /* Set the input location to the most specialized template definition.
10565 This is needed if tsubsting causes an error. */
10566 typedecl = TYPE_MAIN_DECL (pattern);
10567 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
10568 DECL_SOURCE_LOCATION (typedecl);
10570 TYPE_PACKED (type) = TYPE_PACKED (pattern);
10571 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
10572 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
10573 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
10574 if (ANON_AGGR_TYPE_P (pattern))
10575 SET_ANON_AGGR_TYPE_P (type);
10576 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
10578 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
10579 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
10580 /* Adjust visibility for template arguments. */
10581 determine_visibility (TYPE_MAIN_DECL (type));
10583 if (CLASS_TYPE_P (type))
10584 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
10586 pbinfo = TYPE_BINFO (pattern);
10588 /* We should never instantiate a nested class before its enclosing
10589 class; we need to look up the nested class by name before we can
10590 instantiate it, and that lookup should instantiate the enclosing
10591 class. */
10592 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
10593 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
10595 base_list = NULL_TREE;
10596 if (BINFO_N_BASE_BINFOS (pbinfo))
10598 tree pbase_binfo;
10599 tree pushed_scope;
10600 int i;
10602 /* We must enter the scope containing the type, as that is where
10603 the accessibility of types named in dependent bases are
10604 looked up from. */
10605 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
10607 /* Substitute into each of the bases to determine the actual
10608 basetypes. */
10609 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
10611 tree base;
10612 tree access = BINFO_BASE_ACCESS (pbinfo, i);
10613 tree expanded_bases = NULL_TREE;
10614 int idx, len = 1;
10616 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
10618 expanded_bases =
10619 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
10620 args, tf_error, NULL_TREE);
10621 if (expanded_bases == error_mark_node)
10622 continue;
10624 len = TREE_VEC_LENGTH (expanded_bases);
10627 for (idx = 0; idx < len; idx++)
10629 if (expanded_bases)
10630 /* Extract the already-expanded base class. */
10631 base = TREE_VEC_ELT (expanded_bases, idx);
10632 else
10633 /* Substitute to figure out the base class. */
10634 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
10635 NULL_TREE);
10637 if (base == error_mark_node)
10638 continue;
10640 base_list = tree_cons (access, base, base_list);
10641 if (BINFO_VIRTUAL_P (pbase_binfo))
10642 TREE_TYPE (base_list) = integer_type_node;
10646 /* The list is now in reverse order; correct that. */
10647 base_list = nreverse (base_list);
10649 if (pushed_scope)
10650 pop_scope (pushed_scope);
10652 /* Now call xref_basetypes to set up all the base-class
10653 information. */
10654 xref_basetypes (type, base_list);
10656 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
10657 (int) ATTR_FLAG_TYPE_IN_PLACE,
10658 args, tf_error, NULL_TREE);
10659 fixup_attribute_variants (type);
10661 /* Now that our base classes are set up, enter the scope of the
10662 class, so that name lookups into base classes, etc. will work
10663 correctly. This is precisely analogous to what we do in
10664 begin_class_definition when defining an ordinary non-template
10665 class, except we also need to push the enclosing classes. */
10666 push_nested_class (type);
10668 /* Now members are processed in the order of declaration. */
10669 for (member = CLASSTYPE_DECL_LIST (pattern);
10670 member; member = TREE_CHAIN (member))
10672 tree t = TREE_VALUE (member);
10674 if (TREE_PURPOSE (member))
10676 if (TYPE_P (t))
10678 if (LAMBDA_TYPE_P (t))
10679 /* A closure type for a lambda in an NSDMI or default argument.
10680 Ignore it; it will be regenerated when needed. */
10681 continue;
10683 /* Build new CLASSTYPE_NESTED_UTDS. */
10685 tree newtag;
10686 bool class_template_p;
10688 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
10689 && TYPE_LANG_SPECIFIC (t)
10690 && CLASSTYPE_IS_TEMPLATE (t));
10691 /* If the member is a class template, then -- even after
10692 substitution -- there may be dependent types in the
10693 template argument list for the class. We increment
10694 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10695 that function will assume that no types are dependent
10696 when outside of a template. */
10697 if (class_template_p)
10698 ++processing_template_decl;
10699 newtag = tsubst (t, args, tf_error, NULL_TREE);
10700 if (class_template_p)
10701 --processing_template_decl;
10702 if (newtag == error_mark_node)
10703 continue;
10705 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
10707 tree name = TYPE_IDENTIFIER (t);
10709 if (class_template_p)
10710 /* Unfortunately, lookup_template_class sets
10711 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10712 instantiation (i.e., for the type of a member
10713 template class nested within a template class.)
10714 This behavior is required for
10715 maybe_process_partial_specialization to work
10716 correctly, but is not accurate in this case;
10717 the TAG is not an instantiation of anything.
10718 (The corresponding TEMPLATE_DECL is an
10719 instantiation, but the TYPE is not.) */
10720 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
10722 /* Now, we call pushtag to put this NEWTAG into the scope of
10723 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10724 pushtag calling push_template_decl. We don't have to do
10725 this for enums because it will already have been done in
10726 tsubst_enum. */
10727 if (name)
10728 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
10729 pushtag (name, newtag, /*tag_scope=*/ts_current);
10732 else if (DECL_DECLARES_FUNCTION_P (t))
10734 tree r;
10736 if (TREE_CODE (t) == TEMPLATE_DECL)
10737 ++processing_template_decl;
10738 r = tsubst (t, args, tf_error, NULL_TREE);
10739 if (TREE_CODE (t) == TEMPLATE_DECL)
10740 --processing_template_decl;
10741 set_current_access_from_decl (r);
10742 finish_member_declaration (r);
10743 /* Instantiate members marked with attribute used. */
10744 if (r != error_mark_node && DECL_PRESERVE_P (r))
10745 mark_used (r);
10746 if (TREE_CODE (r) == FUNCTION_DECL
10747 && DECL_OMP_DECLARE_REDUCTION_P (r))
10748 cp_check_omp_declare_reduction (r);
10750 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
10751 && LAMBDA_TYPE_P (TREE_TYPE (t)))
10752 /* A closure type for a lambda in an NSDMI or default argument.
10753 Ignore it; it will be regenerated when needed. */;
10754 else
10756 /* Build new TYPE_FIELDS. */
10757 if (TREE_CODE (t) == STATIC_ASSERT)
10759 tree condition;
10761 ++c_inhibit_evaluation_warnings;
10762 condition =
10763 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
10764 tf_warning_or_error, NULL_TREE,
10765 /*integral_constant_expression_p=*/true);
10766 --c_inhibit_evaluation_warnings;
10768 finish_static_assert (condition,
10769 STATIC_ASSERT_MESSAGE (t),
10770 STATIC_ASSERT_SOURCE_LOCATION (t),
10771 /*member_p=*/true);
10773 else if (TREE_CODE (t) != CONST_DECL)
10775 tree r;
10776 tree vec = NULL_TREE;
10777 int len = 1;
10779 /* The file and line for this declaration, to
10780 assist in error message reporting. Since we
10781 called push_tinst_level above, we don't need to
10782 restore these. */
10783 input_location = DECL_SOURCE_LOCATION (t);
10785 if (TREE_CODE (t) == TEMPLATE_DECL)
10786 ++processing_template_decl;
10787 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
10788 if (TREE_CODE (t) == TEMPLATE_DECL)
10789 --processing_template_decl;
10791 if (TREE_CODE (r) == TREE_VEC)
10793 /* A capture pack became multiple fields. */
10794 vec = r;
10795 len = TREE_VEC_LENGTH (vec);
10798 for (int i = 0; i < len; ++i)
10800 if (vec)
10801 r = TREE_VEC_ELT (vec, i);
10802 if (VAR_P (r))
10804 /* In [temp.inst]:
10806 [t]he initialization (and any associated
10807 side-effects) of a static data member does
10808 not occur unless the static data member is
10809 itself used in a way that requires the
10810 definition of the static data member to
10811 exist.
10813 Therefore, we do not substitute into the
10814 initialized for the static data member here. */
10815 finish_static_data_member_decl
10817 /*init=*/NULL_TREE,
10818 /*init_const_expr_p=*/false,
10819 /*asmspec_tree=*/NULL_TREE,
10820 /*flags=*/0);
10821 /* Instantiate members marked with attribute used. */
10822 if (r != error_mark_node && DECL_PRESERVE_P (r))
10823 mark_used (r);
10825 else if (TREE_CODE (r) == FIELD_DECL)
10827 /* Determine whether R has a valid type and can be
10828 completed later. If R is invalid, then its type
10829 is replaced by error_mark_node. */
10830 tree rtype = TREE_TYPE (r);
10831 if (can_complete_type_without_circularity (rtype))
10832 complete_type (rtype);
10834 if (!complete_or_array_type_p (rtype))
10836 /* If R's type couldn't be completed and
10837 it isn't a flexible array member (whose
10838 type is incomplete by definition) give
10839 an error. */
10840 cxx_incomplete_type_error (r, rtype);
10841 TREE_TYPE (r) = error_mark_node;
10845 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10846 such a thing will already have been added to the field
10847 list by tsubst_enum in finish_member_declaration in the
10848 CLASSTYPE_NESTED_UTDS case above. */
10849 if (!(TREE_CODE (r) == TYPE_DECL
10850 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
10851 && DECL_ARTIFICIAL (r)))
10853 set_current_access_from_decl (r);
10854 finish_member_declaration (r);
10860 else
10862 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
10863 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10865 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10867 tree friend_type = t;
10868 bool adjust_processing_template_decl = false;
10870 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10872 /* template <class T> friend class C; */
10873 friend_type = tsubst_friend_class (friend_type, args);
10874 adjust_processing_template_decl = true;
10876 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
10878 /* template <class T> friend class C::D; */
10879 friend_type = tsubst (friend_type, args,
10880 tf_warning_or_error, NULL_TREE);
10881 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10882 friend_type = TREE_TYPE (friend_type);
10883 adjust_processing_template_decl = true;
10885 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
10886 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
10888 /* This could be either
10890 friend class T::C;
10892 when dependent_type_p is false or
10894 template <class U> friend class T::C;
10896 otherwise. */
10897 /* Bump processing_template_decl in case this is something like
10898 template <class T> friend struct A<T>::B. */
10899 ++processing_template_decl;
10900 friend_type = tsubst (friend_type, args,
10901 tf_warning_or_error, NULL_TREE);
10902 if (dependent_type_p (friend_type))
10903 adjust_processing_template_decl = true;
10904 --processing_template_decl;
10906 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
10907 && !CLASSTYPE_USE_TEMPLATE (friend_type)
10908 && TYPE_HIDDEN_P (friend_type))
10910 /* friend class C;
10912 where C hasn't been declared yet. Let's lookup name
10913 from namespace scope directly, bypassing any name that
10914 come from dependent base class. */
10915 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
10917 /* The call to xref_tag_from_type does injection for friend
10918 classes. */
10919 push_nested_namespace (ns);
10920 friend_type =
10921 xref_tag_from_type (friend_type, NULL_TREE,
10922 /*tag_scope=*/ts_current);
10923 pop_nested_namespace (ns);
10925 else if (uses_template_parms (friend_type))
10926 /* friend class C<T>; */
10927 friend_type = tsubst (friend_type, args,
10928 tf_warning_or_error, NULL_TREE);
10929 /* Otherwise it's
10931 friend class C;
10933 where C is already declared or
10935 friend class C<int>;
10937 We don't have to do anything in these cases. */
10939 if (adjust_processing_template_decl)
10940 /* Trick make_friend_class into realizing that the friend
10941 we're adding is a template, not an ordinary class. It's
10942 important that we use make_friend_class since it will
10943 perform some error-checking and output cross-reference
10944 information. */
10945 ++processing_template_decl;
10947 if (friend_type != error_mark_node)
10948 make_friend_class (type, friend_type, /*complain=*/false);
10950 if (adjust_processing_template_decl)
10951 --processing_template_decl;
10953 else
10955 /* Build new DECL_FRIENDLIST. */
10956 tree r;
10958 /* The file and line for this declaration, to
10959 assist in error message reporting. Since we
10960 called push_tinst_level above, we don't need to
10961 restore these. */
10962 input_location = DECL_SOURCE_LOCATION (t);
10964 if (TREE_CODE (t) == TEMPLATE_DECL)
10966 ++processing_template_decl;
10967 push_deferring_access_checks (dk_no_check);
10970 r = tsubst_friend_function (t, args);
10971 add_friend (type, r, /*complain=*/false);
10972 if (TREE_CODE (t) == TEMPLATE_DECL)
10974 pop_deferring_access_checks ();
10975 --processing_template_decl;
10981 if (fn_context)
10983 /* Restore these before substituting into the lambda capture
10984 initializers. */
10985 cp_unevaluated_operand = saved_unevaluated_operand;
10986 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10989 /* Set the file and line number information to whatever is given for
10990 the class itself. This puts error messages involving generated
10991 implicit functions at a predictable point, and the same point
10992 that would be used for non-template classes. */
10993 input_location = DECL_SOURCE_LOCATION (typedecl);
10995 unreverse_member_declarations (type);
10996 finish_struct_1 (type);
10997 TYPE_BEING_DEFINED (type) = 0;
10999 /* We don't instantiate default arguments for member functions. 14.7.1:
11001 The implicit instantiation of a class template specialization causes
11002 the implicit instantiation of the declarations, but not of the
11003 definitions or default arguments, of the class member functions,
11004 member classes, static data members and member templates.... */
11006 /* Some typedefs referenced from within the template code need to be access
11007 checked at template instantiation time, i.e now. These types were
11008 added to the template at parsing time. Let's get those and perform
11009 the access checks then. */
11010 perform_typedefs_access_check (pattern, args);
11011 perform_deferred_access_checks (tf_warning_or_error);
11012 pop_nested_class ();
11013 maximum_field_alignment = saved_maximum_field_alignment;
11014 if (!fn_context)
11015 pop_from_top_level ();
11016 pop_deferring_access_checks ();
11017 pop_tinst_level ();
11019 /* The vtable for a template class can be emitted in any translation
11020 unit in which the class is instantiated. When there is no key
11021 method, however, finish_struct_1 will already have added TYPE to
11022 the keyed_classes. */
11023 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
11024 vec_safe_push (keyed_classes, type);
11026 return type;
11029 /* Wrapper for instantiate_class_template_1. */
11031 tree
11032 instantiate_class_template (tree type)
11034 tree ret;
11035 timevar_push (TV_TEMPLATE_INST);
11036 ret = instantiate_class_template_1 (type);
11037 timevar_pop (TV_TEMPLATE_INST);
11038 return ret;
11041 static tree
11042 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11044 tree r;
11046 if (!t)
11047 r = t;
11048 else if (TYPE_P (t))
11049 r = tsubst (t, args, complain, in_decl);
11050 else
11052 if (!(complain & tf_warning))
11053 ++c_inhibit_evaluation_warnings;
11054 r = tsubst_expr (t, args, complain, in_decl,
11055 /*integral_constant_expression_p=*/true);
11056 if (!(complain & tf_warning))
11057 --c_inhibit_evaluation_warnings;
11059 return r;
11062 /* Given a function parameter pack TMPL_PARM and some function parameters
11063 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
11064 and set *SPEC_P to point at the next point in the list. */
11066 tree
11067 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
11069 /* Collect all of the extra "packed" parameters into an
11070 argument pack. */
11071 tree parmvec;
11072 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
11073 tree spec_parm = *spec_p;
11074 int i, len;
11076 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
11077 if (tmpl_parm
11078 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
11079 break;
11081 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
11082 parmvec = make_tree_vec (len);
11083 spec_parm = *spec_p;
11084 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
11086 tree elt = spec_parm;
11087 if (DECL_PACK_P (elt))
11088 elt = make_pack_expansion (elt);
11089 TREE_VEC_ELT (parmvec, i) = elt;
11092 /* Build the argument packs. */
11093 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
11094 *spec_p = spec_parm;
11096 return argpack;
11099 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
11100 NONTYPE_ARGUMENT_PACK. */
11102 static tree
11103 make_fnparm_pack (tree spec_parm)
11105 return extract_fnparm_pack (NULL_TREE, &spec_parm);
11108 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
11109 pack expansion with no extra args, 2 if it has extra args, or 0
11110 if it is not a pack expansion. */
11112 static int
11113 argument_pack_element_is_expansion_p (tree arg_pack, int i)
11115 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
11116 if (i >= TREE_VEC_LENGTH (vec))
11117 return 0;
11118 tree elt = TREE_VEC_ELT (vec, i);
11119 if (DECL_P (elt))
11120 /* A decl pack is itself an expansion. */
11121 elt = TREE_TYPE (elt);
11122 if (!PACK_EXPANSION_P (elt))
11123 return 0;
11124 if (PACK_EXPANSION_EXTRA_ARGS (elt))
11125 return 2;
11126 return 1;
11130 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11132 static tree
11133 make_argument_pack_select (tree arg_pack, unsigned index)
11135 tree aps = make_node (ARGUMENT_PACK_SELECT);
11137 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
11138 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11140 return aps;
11143 /* This is a subroutine of tsubst_pack_expansion.
11145 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11146 mechanism to store the (non complete list of) arguments of the
11147 substitution and return a non substituted pack expansion, in order
11148 to wait for when we have enough arguments to really perform the
11149 substitution. */
11151 static bool
11152 use_pack_expansion_extra_args_p (tree parm_packs,
11153 int arg_pack_len,
11154 bool has_empty_arg)
11156 /* If one pack has an expansion and another pack has a normal
11157 argument or if one pack has an empty argument and an another
11158 one hasn't then tsubst_pack_expansion cannot perform the
11159 substitution and need to fall back on the
11160 PACK_EXPANSION_EXTRA mechanism. */
11161 if (parm_packs == NULL_TREE)
11162 return false;
11163 else if (has_empty_arg)
11164 return true;
11166 bool has_expansion_arg = false;
11167 for (int i = 0 ; i < arg_pack_len; ++i)
11169 bool has_non_expansion_arg = false;
11170 for (tree parm_pack = parm_packs;
11171 parm_pack;
11172 parm_pack = TREE_CHAIN (parm_pack))
11174 tree arg = TREE_VALUE (parm_pack);
11176 int exp = argument_pack_element_is_expansion_p (arg, i);
11177 if (exp == 2)
11178 /* We can't substitute a pack expansion with extra args into
11179 our pattern. */
11180 return true;
11181 else if (exp)
11182 has_expansion_arg = true;
11183 else
11184 has_non_expansion_arg = true;
11187 if (has_expansion_arg && has_non_expansion_arg)
11188 return true;
11190 return false;
11193 /* [temp.variadic]/6 says that:
11195 The instantiation of a pack expansion [...]
11196 produces a list E1,E2, ..., En, where N is the number of elements
11197 in the pack expansion parameters.
11199 This subroutine of tsubst_pack_expansion produces one of these Ei.
11201 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11202 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11203 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11204 INDEX is the index 'i' of the element Ei to produce. ARGS,
11205 COMPLAIN, and IN_DECL are the same parameters as for the
11206 tsubst_pack_expansion function.
11208 The function returns the resulting Ei upon successful completion,
11209 or error_mark_node.
11211 Note that this function possibly modifies the ARGS parameter, so
11212 it's the responsibility of the caller to restore it. */
11214 static tree
11215 gen_elem_of_pack_expansion_instantiation (tree pattern,
11216 tree parm_packs,
11217 unsigned index,
11218 tree args /* This parm gets
11219 modified. */,
11220 tsubst_flags_t complain,
11221 tree in_decl)
11223 tree t;
11224 bool ith_elem_is_expansion = false;
11226 /* For each parameter pack, change the substitution of the parameter
11227 pack to the ith argument in its argument pack, then expand the
11228 pattern. */
11229 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
11231 tree parm = TREE_PURPOSE (pack);
11232 tree arg_pack = TREE_VALUE (pack);
11233 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
11235 ith_elem_is_expansion |=
11236 argument_pack_element_is_expansion_p (arg_pack, index);
11238 /* Select the Ith argument from the pack. */
11239 if (TREE_CODE (parm) == PARM_DECL
11240 || VAR_P (parm)
11241 || TREE_CODE (parm) == FIELD_DECL)
11243 if (index == 0)
11245 aps = make_argument_pack_select (arg_pack, index);
11246 if (!mark_used (parm, complain) && !(complain & tf_error))
11247 return error_mark_node;
11248 register_local_specialization (aps, parm);
11250 else
11251 aps = retrieve_local_specialization (parm);
11253 else
11255 int idx, level;
11256 template_parm_level_and_index (parm, &level, &idx);
11258 if (index == 0)
11260 aps = make_argument_pack_select (arg_pack, index);
11261 /* Update the corresponding argument. */
11262 TMPL_ARG (args, level, idx) = aps;
11264 else
11265 /* Re-use the ARGUMENT_PACK_SELECT. */
11266 aps = TMPL_ARG (args, level, idx);
11268 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11271 /* Substitute into the PATTERN with the (possibly altered)
11272 arguments. */
11273 if (pattern == in_decl)
11274 /* Expanding a fixed parameter pack from
11275 coerce_template_parameter_pack. */
11276 t = tsubst_decl (pattern, args, complain);
11277 else if (pattern == error_mark_node)
11278 t = error_mark_node;
11279 else if (constraint_p (pattern))
11281 if (processing_template_decl)
11282 t = tsubst_constraint (pattern, args, complain, in_decl);
11283 else
11284 t = (constraints_satisfied_p (pattern, args)
11285 ? boolean_true_node : boolean_false_node);
11287 else if (!TYPE_P (pattern))
11288 t = tsubst_expr (pattern, args, complain, in_decl,
11289 /*integral_constant_expression_p=*/false);
11290 else
11291 t = tsubst (pattern, args, complain, in_decl);
11293 /* If the Ith argument pack element is a pack expansion, then
11294 the Ith element resulting from the substituting is going to
11295 be a pack expansion as well. */
11296 if (ith_elem_is_expansion)
11297 t = make_pack_expansion (t, complain);
11299 return t;
11302 /* When the unexpanded parameter pack in a fold expression expands to an empty
11303 sequence, the value of the expression is as follows; the program is
11304 ill-formed if the operator is not listed in this table.
11306 && true
11307 || false
11308 , void() */
11310 tree
11311 expand_empty_fold (tree t, tsubst_flags_t complain)
11313 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
11314 if (!FOLD_EXPR_MODIFY_P (t))
11315 switch (code)
11317 case TRUTH_ANDIF_EXPR:
11318 return boolean_true_node;
11319 case TRUTH_ORIF_EXPR:
11320 return boolean_false_node;
11321 case COMPOUND_EXPR:
11322 return void_node;
11323 default:
11324 break;
11327 if (complain & tf_error)
11328 error_at (location_of (t),
11329 "fold of empty expansion over %O", code);
11330 return error_mark_node;
11333 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11334 form an expression that combines the two terms using the
11335 operator of T. */
11337 static tree
11338 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
11340 tree op = FOLD_EXPR_OP (t);
11341 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
11343 // Handle compound assignment operators.
11344 if (FOLD_EXPR_MODIFY_P (t))
11345 return build_x_modify_expr (input_location, left, code, right, complain);
11347 switch (code)
11349 case COMPOUND_EXPR:
11350 return build_x_compound_expr (input_location, left, right, complain);
11351 case DOTSTAR_EXPR:
11352 return build_m_component_ref (left, right, complain);
11353 default:
11354 return build_x_binary_op (input_location, code,
11355 left, TREE_CODE (left),
11356 right, TREE_CODE (right),
11357 /*overload=*/NULL,
11358 complain);
11362 /* Substitute ARGS into the pack of a fold expression T. */
11364 static inline tree
11365 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11367 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
11370 /* Substitute ARGS into the pack of a fold expression T. */
11372 static inline tree
11373 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11375 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
11378 /* Expand a PACK of arguments into a grouped as left fold.
11379 Given a pack containing elements A0, A1, ..., An and an
11380 operator @, this builds the expression:
11382 ((A0 @ A1) @ A2) ... @ An
11384 Note that PACK must not be empty.
11386 The operator is defined by the original fold expression T. */
11388 static tree
11389 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
11391 tree left = TREE_VEC_ELT (pack, 0);
11392 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
11394 tree right = TREE_VEC_ELT (pack, i);
11395 left = fold_expression (t, left, right, complain);
11397 return left;
11400 /* Substitute into a unary left fold expression. */
11402 static tree
11403 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
11404 tree in_decl)
11406 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11407 if (pack == error_mark_node)
11408 return error_mark_node;
11409 if (PACK_EXPANSION_P (pack))
11411 tree r = copy_node (t);
11412 FOLD_EXPR_PACK (r) = pack;
11413 return r;
11415 if (TREE_VEC_LENGTH (pack) == 0)
11416 return expand_empty_fold (t, complain);
11417 else
11418 return expand_left_fold (t, pack, complain);
11421 /* Substitute into a binary left fold expression.
11423 Do ths by building a single (non-empty) vector of argumnts and
11424 building the expression from those elements. */
11426 static tree
11427 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
11428 tree in_decl)
11430 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11431 if (pack == error_mark_node)
11432 return error_mark_node;
11433 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11434 if (init == error_mark_node)
11435 return error_mark_node;
11437 if (PACK_EXPANSION_P (pack))
11439 tree r = copy_node (t);
11440 FOLD_EXPR_PACK (r) = pack;
11441 FOLD_EXPR_INIT (r) = init;
11442 return r;
11445 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
11446 TREE_VEC_ELT (vec, 0) = init;
11447 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
11448 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
11450 return expand_left_fold (t, vec, complain);
11453 /* Expand a PACK of arguments into a grouped as right fold.
11454 Given a pack containing elementns A0, A1, ..., and an
11455 operator @, this builds the expression:
11457 A0@ ... (An-2 @ (An-1 @ An))
11459 Note that PACK must not be empty.
11461 The operator is defined by the original fold expression T. */
11463 tree
11464 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
11466 // Build the expression.
11467 int n = TREE_VEC_LENGTH (pack);
11468 tree right = TREE_VEC_ELT (pack, n - 1);
11469 for (--n; n != 0; --n)
11471 tree left = TREE_VEC_ELT (pack, n - 1);
11472 right = fold_expression (t, left, right, complain);
11474 return right;
11477 /* Substitute into a unary right fold expression. */
11479 static tree
11480 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
11481 tree in_decl)
11483 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11484 if (pack == error_mark_node)
11485 return error_mark_node;
11486 if (PACK_EXPANSION_P (pack))
11488 tree r = copy_node (t);
11489 FOLD_EXPR_PACK (r) = pack;
11490 return r;
11492 if (TREE_VEC_LENGTH (pack) == 0)
11493 return expand_empty_fold (t, complain);
11494 else
11495 return expand_right_fold (t, pack, complain);
11498 /* Substitute into a binary right fold expression.
11500 Do ths by building a single (non-empty) vector of arguments and
11501 building the expression from those elements. */
11503 static tree
11504 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
11505 tree in_decl)
11507 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11508 if (pack == error_mark_node)
11509 return error_mark_node;
11510 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11511 if (init == error_mark_node)
11512 return error_mark_node;
11514 if (PACK_EXPANSION_P (pack))
11516 tree r = copy_node (t);
11517 FOLD_EXPR_PACK (r) = pack;
11518 FOLD_EXPR_INIT (r) = init;
11519 return r;
11522 int n = TREE_VEC_LENGTH (pack);
11523 tree vec = make_tree_vec (n + 1);
11524 for (int i = 0; i < n; ++i)
11525 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
11526 TREE_VEC_ELT (vec, n) = init;
11528 return expand_right_fold (t, vec, complain);
11531 /* Walk through the pattern of a pack expansion, adding everything in
11532 local_specializations to a list. */
11534 struct el_data
11536 tree extra;
11537 tsubst_flags_t complain;
11539 static tree
11540 extract_locals_r (tree *tp, int */*walk_subtrees*/, void *data_)
11542 el_data &data = *reinterpret_cast<el_data*>(data_);
11543 tree *extra = &data.extra;
11544 tsubst_flags_t complain = data.complain;
11545 if (tree spec = retrieve_local_specialization (*tp))
11547 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
11549 /* Maybe pull out the PARM_DECL for a partial instantiation. */
11550 tree args = ARGUMENT_PACK_ARGS (spec);
11551 if (TREE_VEC_LENGTH (args) == 1)
11553 tree elt = TREE_VEC_ELT (args, 0);
11554 if (PACK_EXPANSION_P (elt))
11555 elt = PACK_EXPANSION_PATTERN (elt);
11556 if (DECL_PACK_P (elt))
11557 spec = elt;
11559 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
11561 /* Handle lambda capture here, since we aren't doing any
11562 substitution now, and so tsubst_copy won't call
11563 process_outer_var_ref. */
11564 tree args = ARGUMENT_PACK_ARGS (spec);
11565 int len = TREE_VEC_LENGTH (args);
11566 for (int i = 0; i < len; ++i)
11568 tree arg = TREE_VEC_ELT (args, i);
11569 tree carg = arg;
11570 if (outer_automatic_var_p (arg))
11571 carg = process_outer_var_ref (arg, complain);
11572 if (carg != arg)
11574 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
11575 proxies. */
11576 if (i == 0)
11578 spec = copy_node (spec);
11579 args = copy_node (args);
11580 SET_ARGUMENT_PACK_ARGS (spec, args);
11581 register_local_specialization (spec, *tp);
11583 TREE_VEC_ELT (args, i) = carg;
11588 if (outer_automatic_var_p (spec))
11589 spec = process_outer_var_ref (spec, complain);
11590 *extra = tree_cons (*tp, spec, *extra);
11592 return NULL_TREE;
11594 static tree
11595 extract_local_specs (tree pattern, tsubst_flags_t complain)
11597 el_data data = { NULL_TREE, complain };
11598 cp_walk_tree_without_duplicates (&pattern, extract_locals_r, &data);
11599 return data.extra;
11602 /* Substitute ARGS into T, which is an pack expansion
11603 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11604 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11605 (if only a partial substitution could be performed) or
11606 ERROR_MARK_NODE if there was an error. */
11607 tree
11608 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
11609 tree in_decl)
11611 tree pattern;
11612 tree pack, packs = NULL_TREE;
11613 bool unsubstituted_packs = false;
11614 bool unsubstituted_fn_pack = false;
11615 int i, len = -1;
11616 tree result;
11617 hash_map<tree, tree> *saved_local_specializations = NULL;
11618 bool need_local_specializations = false;
11619 int levels;
11621 gcc_assert (PACK_EXPANSION_P (t));
11622 pattern = PACK_EXPANSION_PATTERN (t);
11624 /* Add in any args remembered from an earlier partial instantiation. */
11625 tree extra = PACK_EXPANSION_EXTRA_ARGS (t);
11626 if (extra && TREE_CODE (extra) == TREE_LIST)
11628 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
11630 /* The partial instantiation involved local declarations collected in
11631 extract_local_specs; map from the general template to our local
11632 context. */
11633 tree gen = TREE_PURPOSE (elt);
11634 tree inst = TREE_VALUE (elt);
11635 if (DECL_P (inst))
11636 if (tree local = retrieve_local_specialization (inst))
11637 inst = local;
11638 /* else inst is already a full instantiation of the pack. */
11639 register_local_specialization (inst, gen);
11641 gcc_assert (!TREE_PURPOSE (extra));
11642 extra = TREE_VALUE (extra);
11644 args = add_to_template_args (extra, args);
11646 levels = TMPL_ARGS_DEPTH (args);
11648 /* Determine the argument packs that will instantiate the parameter
11649 packs used in the expansion expression. While we're at it,
11650 compute the number of arguments to be expanded and make sure it
11651 is consistent. */
11652 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
11653 pack = TREE_CHAIN (pack))
11655 tree parm_pack = TREE_VALUE (pack);
11656 tree arg_pack = NULL_TREE;
11657 tree orig_arg = NULL_TREE;
11658 int level = 0;
11660 if (TREE_CODE (parm_pack) == BASES)
11662 gcc_assert (parm_pack == pattern);
11663 if (BASES_DIRECT (parm_pack))
11664 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
11665 args, complain, in_decl, false));
11666 else
11667 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
11668 args, complain, in_decl, false));
11670 else if (builtin_pack_call_p (parm_pack))
11672 /* ??? Support use in other patterns. */
11673 gcc_assert (parm_pack == pattern);
11674 return expand_builtin_pack_call (parm_pack, args,
11675 complain, in_decl);
11677 else if (TREE_CODE (parm_pack) == PARM_DECL)
11679 /* We know we have correct local_specializations if this
11680 expansion is at function scope, or if we're dealing with a
11681 local parameter in a requires expression; for the latter,
11682 tsubst_requires_expr set it up appropriately. */
11683 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
11684 arg_pack = retrieve_local_specialization (parm_pack);
11685 else
11686 /* We can't rely on local_specializations for a parameter
11687 name used later in a function declaration (such as in a
11688 late-specified return type). Even if it exists, it might
11689 have the wrong value for a recursive call. */
11690 need_local_specializations = true;
11692 if (!arg_pack)
11694 /* This parameter pack was used in an unevaluated context. Just
11695 make a dummy decl, since it's only used for its type. */
11696 arg_pack = tsubst_decl (parm_pack, args, complain);
11697 if (arg_pack && DECL_PACK_P (arg_pack))
11698 /* Partial instantiation of the parm_pack, we can't build
11699 up an argument pack yet. */
11700 arg_pack = NULL_TREE;
11701 else
11702 arg_pack = make_fnparm_pack (arg_pack);
11704 else if (argument_pack_element_is_expansion_p (arg_pack, 0))
11705 /* This argument pack isn't fully instantiated yet. We set this
11706 flag rather than clear arg_pack because we do want to do the
11707 optimization below, and we don't want to substitute directly
11708 into the pattern (as that would expose a NONTYPE_ARGUMENT_PACK
11709 where it isn't expected). */
11710 unsubstituted_fn_pack = true;
11712 else if (is_normal_capture_proxy (parm_pack))
11714 arg_pack = retrieve_local_specialization (parm_pack);
11715 if (argument_pack_element_is_expansion_p (arg_pack, 0))
11716 unsubstituted_fn_pack = true;
11718 else
11720 int idx;
11721 template_parm_level_and_index (parm_pack, &level, &idx);
11723 if (level <= levels)
11724 arg_pack = TMPL_ARG (args, level, idx);
11727 orig_arg = arg_pack;
11728 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
11729 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
11731 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
11732 /* This can only happen if we forget to expand an argument
11733 pack somewhere else. Just return an error, silently. */
11735 result = make_tree_vec (1);
11736 TREE_VEC_ELT (result, 0) = error_mark_node;
11737 return result;
11740 if (arg_pack)
11742 int my_len =
11743 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
11745 /* Don't bother trying to do a partial substitution with
11746 incomplete packs; we'll try again after deduction. */
11747 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
11748 return t;
11750 if (len < 0)
11751 len = my_len;
11752 else if (len != my_len
11753 && !unsubstituted_fn_pack)
11755 if (!(complain & tf_error))
11756 /* Fail quietly. */;
11757 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
11758 error ("mismatched argument pack lengths while expanding %qT",
11759 pattern);
11760 else
11761 error ("mismatched argument pack lengths while expanding %qE",
11762 pattern);
11763 return error_mark_node;
11766 /* Keep track of the parameter packs and their corresponding
11767 argument packs. */
11768 packs = tree_cons (parm_pack, arg_pack, packs);
11769 TREE_TYPE (packs) = orig_arg;
11771 else
11773 /* We can't substitute for this parameter pack. We use a flag as
11774 well as the missing_level counter because function parameter
11775 packs don't have a level. */
11776 gcc_assert (processing_template_decl);
11777 unsubstituted_packs = true;
11781 /* If the expansion is just T..., return the matching argument pack, unless
11782 we need to call convert_from_reference on all the elements. This is an
11783 important optimization; see c++/68422. */
11784 if (!unsubstituted_packs
11785 && TREE_PURPOSE (packs) == pattern)
11787 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
11789 /* If the argument pack is a single pack expansion, pull it out. */
11790 if (TREE_VEC_LENGTH (args) == 1
11791 && pack_expansion_args_count (args))
11792 return TREE_VEC_ELT (args, 0);
11794 /* Types need no adjustment, nor does sizeof..., and if we still have
11795 some pack expansion args we won't do anything yet. */
11796 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
11797 || PACK_EXPANSION_SIZEOF_P (t)
11798 || pack_expansion_args_count (args))
11799 return args;
11800 /* Also optimize expression pack expansions if we can tell that the
11801 elements won't have reference type. */
11802 tree type = TREE_TYPE (pattern);
11803 if (type && TREE_CODE (type) != REFERENCE_TYPE
11804 && !PACK_EXPANSION_P (type)
11805 && !WILDCARD_TYPE_P (type))
11806 return args;
11807 /* Otherwise use the normal path so we get convert_from_reference. */
11810 /* We cannot expand this expansion expression, because we don't have
11811 all of the argument packs we need. */
11812 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
11814 /* We got some full packs, but we can't substitute them in until we
11815 have values for all the packs. So remember these until then. */
11817 t = make_pack_expansion (pattern, complain);
11818 tree extra = args;
11819 if (local_specializations)
11820 if (tree locals = extract_local_specs (pattern, complain))
11821 extra = tree_cons (NULL_TREE, extra, locals);
11822 PACK_EXPANSION_EXTRA_ARGS (t) = extra;
11823 return t;
11825 else if (unsubstituted_packs)
11827 /* There were no real arguments, we're just replacing a parameter
11828 pack with another version of itself. Substitute into the
11829 pattern and return a PACK_EXPANSION_*. The caller will need to
11830 deal with that. */
11831 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
11832 t = tsubst_expr (pattern, args, complain, in_decl,
11833 /*integral_constant_expression_p=*/false);
11834 else
11835 t = tsubst (pattern, args, complain, in_decl);
11836 t = make_pack_expansion (t, complain);
11837 return t;
11840 gcc_assert (len >= 0);
11842 if (need_local_specializations)
11844 /* We're in a late-specified return type, so create our own local
11845 specializations map; the current map is either NULL or (in the
11846 case of recursive unification) might have bindings that we don't
11847 want to use or alter. */
11848 saved_local_specializations = local_specializations;
11849 local_specializations = new hash_map<tree, tree>;
11852 /* For each argument in each argument pack, substitute into the
11853 pattern. */
11854 result = make_tree_vec (len);
11855 tree elem_args = copy_template_args (args);
11856 for (i = 0; i < len; ++i)
11858 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
11860 elem_args, complain,
11861 in_decl);
11862 TREE_VEC_ELT (result, i) = t;
11863 if (t == error_mark_node)
11865 result = error_mark_node;
11866 break;
11870 /* Update ARGS to restore the substitution from parameter packs to
11871 their argument packs. */
11872 for (pack = packs; pack; pack = TREE_CHAIN (pack))
11874 tree parm = TREE_PURPOSE (pack);
11876 if (TREE_CODE (parm) == PARM_DECL
11877 || VAR_P (parm)
11878 || TREE_CODE (parm) == FIELD_DECL)
11879 register_local_specialization (TREE_TYPE (pack), parm);
11880 else
11882 int idx, level;
11884 if (TREE_VALUE (pack) == NULL_TREE)
11885 continue;
11887 template_parm_level_and_index (parm, &level, &idx);
11889 /* Update the corresponding argument. */
11890 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
11891 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
11892 TREE_TYPE (pack);
11893 else
11894 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
11898 if (need_local_specializations)
11900 delete local_specializations;
11901 local_specializations = saved_local_specializations;
11904 /* If the dependent pack arguments were such that we end up with only a
11905 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11906 if (len == 1 && TREE_CODE (result) == TREE_VEC
11907 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
11908 return TREE_VEC_ELT (result, 0);
11910 return result;
11913 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11914 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11915 parameter packs; all parms generated from a function parameter pack will
11916 have the same DECL_PARM_INDEX. */
11918 tree
11919 get_pattern_parm (tree parm, tree tmpl)
11921 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
11922 tree patparm;
11924 if (DECL_ARTIFICIAL (parm))
11926 for (patparm = DECL_ARGUMENTS (pattern);
11927 patparm; patparm = DECL_CHAIN (patparm))
11928 if (DECL_ARTIFICIAL (patparm)
11929 && DECL_NAME (parm) == DECL_NAME (patparm))
11930 break;
11932 else
11934 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
11935 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
11936 gcc_assert (DECL_PARM_INDEX (patparm)
11937 == DECL_PARM_INDEX (parm));
11940 return patparm;
11943 /* Make an argument pack out of the TREE_VEC VEC. */
11945 static tree
11946 make_argument_pack (tree vec)
11948 tree pack;
11949 tree elt = TREE_VEC_ELT (vec, 0);
11950 if (TYPE_P (elt))
11951 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
11952 else
11954 pack = make_node (NONTYPE_ARGUMENT_PACK);
11955 TREE_CONSTANT (pack) = 1;
11957 SET_ARGUMENT_PACK_ARGS (pack, vec);
11958 return pack;
11961 /* Return an exact copy of template args T that can be modified
11962 independently. */
11964 static tree
11965 copy_template_args (tree t)
11967 if (t == error_mark_node)
11968 return t;
11970 int len = TREE_VEC_LENGTH (t);
11971 tree new_vec = make_tree_vec (len);
11973 for (int i = 0; i < len; ++i)
11975 tree elt = TREE_VEC_ELT (t, i);
11976 if (elt && TREE_CODE (elt) == TREE_VEC)
11977 elt = copy_template_args (elt);
11978 TREE_VEC_ELT (new_vec, i) = elt;
11981 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
11982 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
11984 return new_vec;
11987 /* Substitute ARGS into the vector or list of template arguments T. */
11989 static tree
11990 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11992 tree orig_t = t;
11993 int len, need_new = 0, i, expanded_len_adjust = 0, out;
11994 tree *elts;
11996 if (t == error_mark_node)
11997 return error_mark_node;
11999 len = TREE_VEC_LENGTH (t);
12000 elts = XALLOCAVEC (tree, len);
12002 for (i = 0; i < len; i++)
12004 tree orig_arg = TREE_VEC_ELT (t, i);
12005 tree new_arg;
12007 if (TREE_CODE (orig_arg) == TREE_VEC)
12008 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
12009 else if (PACK_EXPANSION_P (orig_arg))
12011 /* Substitute into an expansion expression. */
12012 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
12014 if (TREE_CODE (new_arg) == TREE_VEC)
12015 /* Add to the expanded length adjustment the number of
12016 expanded arguments. We subtract one from this
12017 measurement, because the argument pack expression
12018 itself is already counted as 1 in
12019 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
12020 the argument pack is empty. */
12021 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
12023 else if (ARGUMENT_PACK_P (orig_arg))
12025 /* Substitute into each of the arguments. */
12026 new_arg = TYPE_P (orig_arg)
12027 ? cxx_make_type (TREE_CODE (orig_arg))
12028 : make_node (TREE_CODE (orig_arg));
12030 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
12031 args, complain, in_decl);
12032 if (pack_args == error_mark_node)
12033 new_arg = error_mark_node;
12034 else
12035 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
12037 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
12038 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
12040 else
12041 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
12043 if (new_arg == error_mark_node)
12044 return error_mark_node;
12046 elts[i] = new_arg;
12047 if (new_arg != orig_arg)
12048 need_new = 1;
12051 if (!need_new)
12052 return t;
12054 /* Make space for the expanded arguments coming from template
12055 argument packs. */
12056 t = make_tree_vec (len + expanded_len_adjust);
12057 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
12058 arguments for a member template.
12059 In that case each TREE_VEC in ORIG_T represents a level of template
12060 arguments, and ORIG_T won't carry any non defaulted argument count.
12061 It will rather be the nested TREE_VECs that will carry one.
12062 In other words, ORIG_T carries a non defaulted argument count only
12063 if it doesn't contain any nested TREE_VEC. */
12064 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
12066 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
12067 count += expanded_len_adjust;
12068 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
12070 for (i = 0, out = 0; i < len; i++)
12072 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
12073 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
12074 && TREE_CODE (elts[i]) == TREE_VEC)
12076 int idx;
12078 /* Now expand the template argument pack "in place". */
12079 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
12080 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
12082 else
12084 TREE_VEC_ELT (t, out) = elts[i];
12085 out++;
12089 return t;
12092 /* Substitute ARGS into one level PARMS of template parameters. */
12094 static tree
12095 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
12097 if (parms == error_mark_node)
12098 return error_mark_node;
12100 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
12102 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
12104 tree tuple = TREE_VEC_ELT (parms, i);
12106 if (tuple == error_mark_node)
12107 continue;
12109 TREE_VEC_ELT (new_vec, i) =
12110 tsubst_template_parm (tuple, args, complain);
12113 return new_vec;
12116 /* Return the result of substituting ARGS into the template parameters
12117 given by PARMS. If there are m levels of ARGS and m + n levels of
12118 PARMS, then the result will contain n levels of PARMS. For
12119 example, if PARMS is `template <class T> template <class U>
12120 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
12121 result will be `template <int*, double, class V>'. */
12123 static tree
12124 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
12126 tree r = NULL_TREE;
12127 tree* new_parms;
12129 /* When substituting into a template, we must set
12130 PROCESSING_TEMPLATE_DECL as the template parameters may be
12131 dependent if they are based on one-another, and the dependency
12132 predicates are short-circuit outside of templates. */
12133 ++processing_template_decl;
12135 for (new_parms = &r;
12136 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
12137 new_parms = &(TREE_CHAIN (*new_parms)),
12138 parms = TREE_CHAIN (parms))
12140 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
12141 args, complain);
12142 *new_parms =
12143 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
12144 - TMPL_ARGS_DEPTH (args)),
12145 new_vec, NULL_TREE);
12148 --processing_template_decl;
12150 return r;
12153 /* Return the result of substituting ARGS into one template parameter
12154 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
12155 parameter and which TREE_PURPOSE is the default argument of the
12156 template parameter. */
12158 static tree
12159 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
12161 tree default_value, parm_decl;
12163 if (args == NULL_TREE
12164 || t == NULL_TREE
12165 || t == error_mark_node)
12166 return t;
12168 gcc_assert (TREE_CODE (t) == TREE_LIST);
12170 default_value = TREE_PURPOSE (t);
12171 parm_decl = TREE_VALUE (t);
12173 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
12174 if (TREE_CODE (parm_decl) == PARM_DECL
12175 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
12176 parm_decl = error_mark_node;
12177 default_value = tsubst_template_arg (default_value, args,
12178 complain, NULL_TREE);
12180 return build_tree_list (default_value, parm_decl);
12183 /* Substitute the ARGS into the indicated aggregate (or enumeration)
12184 type T. If T is not an aggregate or enumeration type, it is
12185 handled as if by tsubst. IN_DECL is as for tsubst. If
12186 ENTERING_SCOPE is nonzero, T is the context for a template which
12187 we are presently tsubst'ing. Return the substituted value. */
12189 static tree
12190 tsubst_aggr_type (tree t,
12191 tree args,
12192 tsubst_flags_t complain,
12193 tree in_decl,
12194 int entering_scope)
12196 if (t == NULL_TREE)
12197 return NULL_TREE;
12199 switch (TREE_CODE (t))
12201 case RECORD_TYPE:
12202 if (TYPE_PTRMEMFUNC_P (t))
12203 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
12205 /* Fall through. */
12206 case ENUMERAL_TYPE:
12207 case UNION_TYPE:
12208 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
12210 tree argvec;
12211 tree context;
12212 tree r;
12213 int saved_unevaluated_operand;
12214 int saved_inhibit_evaluation_warnings;
12216 /* In "sizeof(X<I>)" we need to evaluate "I". */
12217 saved_unevaluated_operand = cp_unevaluated_operand;
12218 cp_unevaluated_operand = 0;
12219 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
12220 c_inhibit_evaluation_warnings = 0;
12222 /* First, determine the context for the type we are looking
12223 up. */
12224 context = TYPE_CONTEXT (t);
12225 if (context && TYPE_P (context))
12227 context = tsubst_aggr_type (context, args, complain,
12228 in_decl, /*entering_scope=*/1);
12229 /* If context is a nested class inside a class template,
12230 it may still need to be instantiated (c++/33959). */
12231 context = complete_type (context);
12234 /* Then, figure out what arguments are appropriate for the
12235 type we are trying to find. For example, given:
12237 template <class T> struct S;
12238 template <class T, class U> void f(T, U) { S<U> su; }
12240 and supposing that we are instantiating f<int, double>,
12241 then our ARGS will be {int, double}, but, when looking up
12242 S we only want {double}. */
12243 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
12244 complain, in_decl);
12245 if (argvec == error_mark_node)
12246 r = error_mark_node;
12247 else
12249 r = lookup_template_class (t, argvec, in_decl, context,
12250 entering_scope, complain);
12251 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
12254 cp_unevaluated_operand = saved_unevaluated_operand;
12255 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
12257 return r;
12259 else
12260 /* This is not a template type, so there's nothing to do. */
12261 return t;
12263 default:
12264 return tsubst (t, args, complain, in_decl);
12268 static GTY((cache)) tree_cache_map *defarg_inst;
12270 /* Substitute into the default argument ARG (a default argument for
12271 FN), which has the indicated TYPE. */
12273 tree
12274 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
12275 tsubst_flags_t complain)
12277 tree saved_class_ptr = NULL_TREE;
12278 tree saved_class_ref = NULL_TREE;
12279 int errs = errorcount + sorrycount;
12281 /* This can happen in invalid code. */
12282 if (TREE_CODE (arg) == DEFAULT_ARG)
12283 return arg;
12285 tree parm = FUNCTION_FIRST_USER_PARM (fn);
12286 parm = chain_index (parmnum, parm);
12287 tree parmtype = TREE_TYPE (parm);
12288 if (DECL_BY_REFERENCE (parm))
12289 parmtype = TREE_TYPE (parmtype);
12290 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
12292 tree *slot;
12293 if (defarg_inst && (slot = defarg_inst->get (parm)))
12294 return *slot;
12296 /* This default argument came from a template. Instantiate the
12297 default argument here, not in tsubst. In the case of
12298 something like:
12300 template <class T>
12301 struct S {
12302 static T t();
12303 void f(T = t());
12306 we must be careful to do name lookup in the scope of S<T>,
12307 rather than in the current class. */
12308 push_access_scope (fn);
12309 /* The "this" pointer is not valid in a default argument. */
12310 if (cfun)
12312 saved_class_ptr = current_class_ptr;
12313 cp_function_chain->x_current_class_ptr = NULL_TREE;
12314 saved_class_ref = current_class_ref;
12315 cp_function_chain->x_current_class_ref = NULL_TREE;
12318 start_lambda_scope (parm);
12320 push_deferring_access_checks(dk_no_deferred);
12321 /* The default argument expression may cause implicitly defined
12322 member functions to be synthesized, which will result in garbage
12323 collection. We must treat this situation as if we were within
12324 the body of function so as to avoid collecting live data on the
12325 stack. */
12326 ++function_depth;
12327 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
12328 complain, NULL_TREE,
12329 /*integral_constant_expression_p=*/false);
12330 --function_depth;
12331 pop_deferring_access_checks();
12333 finish_lambda_scope ();
12335 /* Restore the "this" pointer. */
12336 if (cfun)
12338 cp_function_chain->x_current_class_ptr = saved_class_ptr;
12339 cp_function_chain->x_current_class_ref = saved_class_ref;
12342 if (errorcount+sorrycount > errs
12343 && (complain & tf_warning_or_error))
12344 inform (input_location,
12345 " when instantiating default argument for call to %qD", fn);
12347 /* Make sure the default argument is reasonable. */
12348 arg = check_default_argument (type, arg, complain);
12350 pop_access_scope (fn);
12352 if (arg != error_mark_node && !cp_unevaluated_operand)
12354 if (!defarg_inst)
12355 defarg_inst = tree_cache_map::create_ggc (37);
12356 defarg_inst->put (parm, arg);
12359 return arg;
12362 /* Substitute into all the default arguments for FN. */
12364 static void
12365 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
12367 tree arg;
12368 tree tmpl_args;
12370 tmpl_args = DECL_TI_ARGS (fn);
12372 /* If this function is not yet instantiated, we certainly don't need
12373 its default arguments. */
12374 if (uses_template_parms (tmpl_args))
12375 return;
12376 /* Don't do this again for clones. */
12377 if (DECL_CLONED_FUNCTION_P (fn))
12378 return;
12380 int i = 0;
12381 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
12382 arg;
12383 arg = TREE_CHAIN (arg), ++i)
12384 if (TREE_PURPOSE (arg))
12385 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
12386 TREE_VALUE (arg),
12387 TREE_PURPOSE (arg),
12388 complain);
12391 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12393 static tree
12394 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
12395 tree lambda_fntype)
12397 tree gen_tmpl, argvec;
12398 hashval_t hash = 0;
12399 tree in_decl = t;
12401 /* Nobody should be tsubst'ing into non-template functions. */
12402 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
12404 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
12406 /* If T is not dependent, just return it. */
12407 if (!uses_template_parms (DECL_TI_ARGS (t)))
12408 return t;
12410 /* Calculate the most general template of which R is a
12411 specialization. */
12412 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
12414 /* We're substituting a lambda function under tsubst_lambda_expr but not
12415 directly from it; find the matching function we're already inside.
12416 But don't do this if T is a generic lambda with a single level of
12417 template parms, as in that case we're doing a normal instantiation. */
12418 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
12419 && (!generic_lambda_fn_p (t)
12420 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
12421 return enclosing_instantiation_of (t);
12423 /* Calculate the complete set of arguments used to
12424 specialize R. */
12425 argvec = tsubst_template_args (DECL_TI_ARGS
12426 (DECL_TEMPLATE_RESULT
12427 (DECL_TI_TEMPLATE (t))),
12428 args, complain, in_decl);
12429 if (argvec == error_mark_node)
12430 return error_mark_node;
12432 /* Check to see if we already have this specialization. */
12433 if (!lambda_fntype)
12435 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12436 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
12437 return spec;
12440 /* We can see more levels of arguments than parameters if
12441 there was a specialization of a member template, like
12442 this:
12444 template <class T> struct S { template <class U> void f(); }
12445 template <> template <class U> void S<int>::f(U);
12447 Here, we'll be substituting into the specialization,
12448 because that's where we can find the code we actually
12449 want to generate, but we'll have enough arguments for
12450 the most general template.
12452 We also deal with the peculiar case:
12454 template <class T> struct S {
12455 template <class U> friend void f();
12457 template <class U> void f() {}
12458 template S<int>;
12459 template void f<double>();
12461 Here, the ARGS for the instantiation of will be {int,
12462 double}. But, we only need as many ARGS as there are
12463 levels of template parameters in CODE_PATTERN. We are
12464 careful not to get fooled into reducing the ARGS in
12465 situations like:
12467 template <class T> struct S { template <class U> void f(U); }
12468 template <class T> template <> void S<T>::f(int) {}
12470 which we can spot because the pattern will be a
12471 specialization in this case. */
12472 int args_depth = TMPL_ARGS_DEPTH (args);
12473 int parms_depth =
12474 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
12476 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
12477 args = get_innermost_template_args (args, parms_depth);
12479 else
12481 /* This special case arises when we have something like this:
12483 template <class T> struct S {
12484 friend void f<int>(int, double);
12487 Here, the DECL_TI_TEMPLATE for the friend declaration
12488 will be an IDENTIFIER_NODE. We are being called from
12489 tsubst_friend_function, and we want only to create a
12490 new decl (R) with appropriate types so that we can call
12491 determine_specialization. */
12492 gen_tmpl = NULL_TREE;
12493 argvec = NULL_TREE;
12496 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
12497 : NULL_TREE);
12498 tree ctx = closure ? closure : DECL_CONTEXT (t);
12499 bool member = ctx && TYPE_P (ctx);
12501 if (member && !closure)
12502 ctx = tsubst_aggr_type (ctx, args,
12503 complain, t, /*entering_scope=*/1);
12505 tree type = (lambda_fntype ? lambda_fntype
12506 : tsubst (TREE_TYPE (t), args,
12507 complain | tf_fndecl_type, in_decl));
12508 if (type == error_mark_node)
12509 return error_mark_node;
12511 /* If we hit excessive deduction depth, the type is bogus even if
12512 it isn't error_mark_node, so don't build a decl. */
12513 if (excessive_deduction_depth)
12514 return error_mark_node;
12516 /* We do NOT check for matching decls pushed separately at this
12517 point, as they may not represent instantiations of this
12518 template, and in any case are considered separate under the
12519 discrete model. */
12520 tree r = copy_decl (t);
12521 DECL_USE_TEMPLATE (r) = 0;
12522 TREE_TYPE (r) = type;
12523 /* Clear out the mangled name and RTL for the instantiation. */
12524 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12525 SET_DECL_RTL (r, NULL);
12526 /* Leave DECL_INITIAL set on deleted instantiations. */
12527 if (!DECL_DELETED_FN (r))
12528 DECL_INITIAL (r) = NULL_TREE;
12529 DECL_CONTEXT (r) = ctx;
12531 /* OpenMP UDRs have the only argument a reference to the declared
12532 type. We want to diagnose if the declared type is a reference,
12533 which is invalid, but as references to references are usually
12534 quietly merged, diagnose it here. */
12535 if (DECL_OMP_DECLARE_REDUCTION_P (t))
12537 tree argtype
12538 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
12539 argtype = tsubst (argtype, args, complain, in_decl);
12540 if (TREE_CODE (argtype) == REFERENCE_TYPE)
12541 error_at (DECL_SOURCE_LOCATION (t),
12542 "reference type %qT in "
12543 "%<#pragma omp declare reduction%>", argtype);
12544 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
12545 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
12546 argtype);
12549 if (member && DECL_CONV_FN_P (r))
12550 /* Type-conversion operator. Reconstruct the name, in
12551 case it's the name of one of the template's parameters. */
12552 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
12554 tree parms = DECL_ARGUMENTS (t);
12555 if (closure)
12556 parms = DECL_CHAIN (parms);
12557 parms = tsubst (parms, args, complain, t);
12558 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
12559 DECL_CONTEXT (parm) = r;
12560 if (closure)
12562 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
12563 DECL_CHAIN (tparm) = parms;
12564 parms = tparm;
12566 DECL_ARGUMENTS (r) = parms;
12567 DECL_RESULT (r) = NULL_TREE;
12569 TREE_STATIC (r) = 0;
12570 TREE_PUBLIC (r) = TREE_PUBLIC (t);
12571 DECL_EXTERNAL (r) = 1;
12572 /* If this is an instantiation of a function with internal
12573 linkage, we already know what object file linkage will be
12574 assigned to the instantiation. */
12575 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
12576 DECL_DEFER_OUTPUT (r) = 0;
12577 DECL_CHAIN (r) = NULL_TREE;
12578 DECL_PENDING_INLINE_INFO (r) = 0;
12579 DECL_PENDING_INLINE_P (r) = 0;
12580 DECL_SAVED_TREE (r) = NULL_TREE;
12581 DECL_STRUCT_FUNCTION (r) = NULL;
12582 TREE_USED (r) = 0;
12583 /* We'll re-clone as appropriate in instantiate_template. */
12584 DECL_CLONED_FUNCTION (r) = NULL_TREE;
12586 /* If we aren't complaining now, return on error before we register
12587 the specialization so that we'll complain eventually. */
12588 if ((complain & tf_error) == 0
12589 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12590 && !grok_op_properties (r, /*complain=*/false))
12591 return error_mark_node;
12593 /* When instantiating a constrained member, substitute
12594 into the constraints to create a new constraint. */
12595 if (tree ci = get_constraints (t))
12596 if (member)
12598 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
12599 set_constraints (r, ci);
12602 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12603 this in the special friend case mentioned above where
12604 GEN_TMPL is NULL. */
12605 if (gen_tmpl && !closure)
12607 DECL_TEMPLATE_INFO (r)
12608 = build_template_info (gen_tmpl, argvec);
12609 SET_DECL_IMPLICIT_INSTANTIATION (r);
12611 tree new_r
12612 = register_specialization (r, gen_tmpl, argvec, false, hash);
12613 if (new_r != r)
12614 /* We instantiated this while substituting into
12615 the type earlier (template/friend54.C). */
12616 return new_r;
12618 /* We're not supposed to instantiate default arguments
12619 until they are called, for a template. But, for a
12620 declaration like:
12622 template <class T> void f ()
12623 { extern void g(int i = T()); }
12625 we should do the substitution when the template is
12626 instantiated. We handle the member function case in
12627 instantiate_class_template since the default arguments
12628 might refer to other members of the class. */
12629 if (!member
12630 && !PRIMARY_TEMPLATE_P (gen_tmpl)
12631 && !uses_template_parms (argvec))
12632 tsubst_default_arguments (r, complain);
12634 else
12635 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12637 /* Copy the list of befriending classes. */
12638 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
12639 *friends;
12640 friends = &TREE_CHAIN (*friends))
12642 *friends = copy_node (*friends);
12643 TREE_VALUE (*friends)
12644 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
12647 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
12649 maybe_retrofit_in_chrg (r);
12650 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
12651 return error_mark_node;
12652 /* If this is an instantiation of a member template, clone it.
12653 If it isn't, that'll be handled by
12654 clone_constructors_and_destructors. */
12655 if (PRIMARY_TEMPLATE_P (gen_tmpl))
12656 clone_function_decl (r, /*update_methods=*/false);
12658 else if ((complain & tf_error) != 0
12659 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12660 && !grok_op_properties (r, /*complain=*/true))
12661 return error_mark_node;
12663 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
12664 SET_DECL_FRIEND_CONTEXT (r,
12665 tsubst (DECL_FRIEND_CONTEXT (t),
12666 args, complain, in_decl));
12668 /* Possibly limit visibility based on template args. */
12669 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12670 if (DECL_VISIBILITY_SPECIFIED (t))
12672 DECL_VISIBILITY_SPECIFIED (r) = 0;
12673 DECL_ATTRIBUTES (r)
12674 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12676 determine_visibility (r);
12677 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
12678 && !processing_template_decl)
12679 defaulted_late_check (r);
12681 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12682 args, complain, in_decl);
12683 return r;
12686 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
12688 static tree
12689 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
12690 tree lambda_fntype)
12692 /* We can get here when processing a member function template,
12693 member class template, or template template parameter. */
12694 tree decl = DECL_TEMPLATE_RESULT (t);
12695 tree in_decl = t;
12696 tree spec;
12697 tree tmpl_args;
12698 tree full_args;
12699 tree r;
12700 hashval_t hash = 0;
12702 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12704 /* Template template parameter is treated here. */
12705 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12706 if (new_type == error_mark_node)
12707 r = error_mark_node;
12708 /* If we get a real template back, return it. This can happen in
12709 the context of most_specialized_partial_spec. */
12710 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
12711 r = new_type;
12712 else
12713 /* The new TEMPLATE_DECL was built in
12714 reduce_template_parm_level. */
12715 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
12716 return r;
12719 if (!lambda_fntype)
12721 /* We might already have an instance of this template.
12722 The ARGS are for the surrounding class type, so the
12723 full args contain the tsubst'd args for the context,
12724 plus the innermost args from the template decl. */
12725 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
12726 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
12727 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
12728 /* Because this is a template, the arguments will still be
12729 dependent, even after substitution. If
12730 PROCESSING_TEMPLATE_DECL is not set, the dependency
12731 predicates will short-circuit. */
12732 ++processing_template_decl;
12733 full_args = tsubst_template_args (tmpl_args, args,
12734 complain, in_decl);
12735 --processing_template_decl;
12736 if (full_args == error_mark_node)
12737 return error_mark_node;
12739 /* If this is a default template template argument,
12740 tsubst might not have changed anything. */
12741 if (full_args == tmpl_args)
12742 return t;
12744 hash = hash_tmpl_and_args (t, full_args);
12745 spec = retrieve_specialization (t, full_args, hash);
12746 if (spec != NULL_TREE)
12747 return spec;
12750 /* Make a new template decl. It will be similar to the
12751 original, but will record the current template arguments.
12752 We also create a new function declaration, which is just
12753 like the old one, but points to this new template, rather
12754 than the old one. */
12755 r = copy_decl (t);
12756 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
12757 DECL_CHAIN (r) = NULL_TREE;
12759 // Build new template info linking to the original template decl.
12760 if (!lambda_fntype)
12762 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12763 SET_DECL_IMPLICIT_INSTANTIATION (r);
12765 else
12766 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12768 /* The template parameters for this new template are all the
12769 template parameters for the old template, except the
12770 outermost level of parameters. */
12771 DECL_TEMPLATE_PARMS (r)
12772 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
12773 complain);
12775 if (TREE_CODE (decl) == TYPE_DECL
12776 && !TYPE_DECL_ALIAS_P (decl))
12778 tree new_type;
12779 ++processing_template_decl;
12780 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12781 --processing_template_decl;
12782 if (new_type == error_mark_node)
12783 return error_mark_node;
12785 TREE_TYPE (r) = new_type;
12786 /* For a partial specialization, we need to keep pointing to
12787 the primary template. */
12788 if (!DECL_TEMPLATE_SPECIALIZATION (t))
12789 CLASSTYPE_TI_TEMPLATE (new_type) = r;
12790 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
12791 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
12792 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
12794 else
12796 tree new_decl;
12797 ++processing_template_decl;
12798 if (TREE_CODE (decl) == FUNCTION_DECL)
12799 new_decl = tsubst_function_decl (decl, args, complain, lambda_fntype);
12800 else
12801 new_decl = tsubst (decl, args, complain, in_decl);
12802 --processing_template_decl;
12803 if (new_decl == error_mark_node)
12804 return error_mark_node;
12806 DECL_TEMPLATE_RESULT (r) = new_decl;
12807 TREE_TYPE (r) = TREE_TYPE (new_decl);
12808 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
12809 if (lambda_fntype)
12811 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
12812 DECL_TEMPLATE_INFO (new_decl) = build_template_info (r, args);
12814 else
12816 DECL_TI_TEMPLATE (new_decl) = r;
12817 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
12821 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
12822 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
12824 if (PRIMARY_TEMPLATE_P (t))
12825 DECL_PRIMARY_TEMPLATE (r) = r;
12827 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
12828 && !lambda_fntype)
12829 /* Record this non-type partial instantiation. */
12830 register_specialization (r, t,
12831 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
12832 false, hash);
12834 return r;
12837 /* True if FN is the op() for a lambda in an uninstantiated template. */
12839 bool
12840 lambda_fn_in_template_p (tree fn)
12842 if (!fn || !LAMBDA_FUNCTION_P (fn))
12843 return false;
12844 tree closure = DECL_CONTEXT (fn);
12845 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
12848 /* We're instantiating a variable from template function TCTX. Return the
12849 corresponding current enclosing scope. This gets complicated because lambda
12850 functions in templates are regenerated rather than instantiated, but generic
12851 lambda functions are subsequently instantiated. */
12853 static tree
12854 enclosing_instantiation_of (tree otctx)
12856 tree tctx = otctx;
12857 tree fn = current_function_decl;
12858 int lambda_count = 0;
12860 for (; tctx && lambda_fn_in_template_p (tctx);
12861 tctx = decl_function_context (tctx))
12862 ++lambda_count;
12863 for (; fn; fn = decl_function_context (fn))
12865 tree ofn = fn;
12866 int flambda_count = 0;
12867 for (; flambda_count < lambda_count && fn && LAMBDA_FUNCTION_P (fn);
12868 fn = decl_function_context (fn))
12869 ++flambda_count;
12870 if (DECL_TEMPLATE_INFO (fn)
12871 ? most_general_template (fn) != most_general_template (tctx)
12872 : fn != tctx)
12873 continue;
12874 gcc_assert (DECL_NAME (ofn) == DECL_NAME (otctx)
12875 || DECL_CONV_FN_P (ofn));
12876 return ofn;
12878 gcc_unreachable ();
12881 /* Substitute the ARGS into the T, which is a _DECL. Return the
12882 result of the substitution. Issue error and warning messages under
12883 control of COMPLAIN. */
12885 static tree
12886 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
12888 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12889 location_t saved_loc;
12890 tree r = NULL_TREE;
12891 tree in_decl = t;
12892 hashval_t hash = 0;
12894 /* Set the filename and linenumber to improve error-reporting. */
12895 saved_loc = input_location;
12896 input_location = DECL_SOURCE_LOCATION (t);
12898 switch (TREE_CODE (t))
12900 case TEMPLATE_DECL:
12901 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
12902 break;
12904 case FUNCTION_DECL:
12905 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
12906 break;
12908 case PARM_DECL:
12910 tree type = NULL_TREE;
12911 int i, len = 1;
12912 tree expanded_types = NULL_TREE;
12913 tree prev_r = NULL_TREE;
12914 tree first_r = NULL_TREE;
12916 if (DECL_PACK_P (t))
12918 /* If there is a local specialization that isn't a
12919 parameter pack, it means that we're doing a "simple"
12920 substitution from inside tsubst_pack_expansion. Just
12921 return the local specialization (which will be a single
12922 parm). */
12923 tree spec = retrieve_local_specialization (t);
12924 if (spec
12925 && TREE_CODE (spec) == PARM_DECL
12926 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
12927 RETURN (spec);
12929 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12930 the parameters in this function parameter pack. */
12931 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12932 complain, in_decl);
12933 if (TREE_CODE (expanded_types) == TREE_VEC)
12935 len = TREE_VEC_LENGTH (expanded_types);
12937 /* Zero-length parameter packs are boring. Just substitute
12938 into the chain. */
12939 if (len == 0)
12940 RETURN (tsubst (TREE_CHAIN (t), args, complain,
12941 TREE_CHAIN (t)));
12943 else
12945 /* All we did was update the type. Make a note of that. */
12946 type = expanded_types;
12947 expanded_types = NULL_TREE;
12951 /* Loop through all of the parameters we'll build. When T is
12952 a function parameter pack, LEN is the number of expanded
12953 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12954 r = NULL_TREE;
12955 for (i = 0; i < len; ++i)
12957 prev_r = r;
12958 r = copy_node (t);
12959 if (DECL_TEMPLATE_PARM_P (t))
12960 SET_DECL_TEMPLATE_PARM_P (r);
12962 if (expanded_types)
12963 /* We're on the Ith parameter of the function parameter
12964 pack. */
12966 /* Get the Ith type. */
12967 type = TREE_VEC_ELT (expanded_types, i);
12969 /* Rename the parameter to include the index. */
12970 DECL_NAME (r)
12971 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12973 else if (!type)
12974 /* We're dealing with a normal parameter. */
12975 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12977 type = type_decays_to (type);
12978 TREE_TYPE (r) = type;
12979 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12981 if (DECL_INITIAL (r))
12983 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
12984 DECL_INITIAL (r) = TREE_TYPE (r);
12985 else
12986 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
12987 complain, in_decl);
12990 DECL_CONTEXT (r) = NULL_TREE;
12992 if (!DECL_TEMPLATE_PARM_P (r))
12993 DECL_ARG_TYPE (r) = type_passed_as (type);
12995 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12996 args, complain, in_decl);
12998 /* Keep track of the first new parameter we
12999 generate. That's what will be returned to the
13000 caller. */
13001 if (!first_r)
13002 first_r = r;
13004 /* Build a proper chain of parameters when substituting
13005 into a function parameter pack. */
13006 if (prev_r)
13007 DECL_CHAIN (prev_r) = r;
13010 /* If cp_unevaluated_operand is set, we're just looking for a
13011 single dummy parameter, so don't keep going. */
13012 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
13013 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
13014 complain, DECL_CHAIN (t));
13016 /* FIRST_R contains the start of the chain we've built. */
13017 r = first_r;
13019 break;
13021 case FIELD_DECL:
13023 tree type = NULL_TREE;
13024 tree vec = NULL_TREE;
13025 tree expanded_types = NULL_TREE;
13026 int len = 1;
13028 if (PACK_EXPANSION_P (TREE_TYPE (t)))
13030 /* This field is a lambda capture pack. Return a TREE_VEC of
13031 the expanded fields to instantiate_class_template_1. */
13032 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
13033 complain, in_decl);
13034 if (TREE_CODE (expanded_types) == TREE_VEC)
13036 len = TREE_VEC_LENGTH (expanded_types);
13037 vec = make_tree_vec (len);
13039 else
13041 /* All we did was update the type. Make a note of that. */
13042 type = expanded_types;
13043 expanded_types = NULL_TREE;
13047 for (int i = 0; i < len; ++i)
13049 r = copy_decl (t);
13050 if (expanded_types)
13052 type = TREE_VEC_ELT (expanded_types, i);
13053 DECL_NAME (r)
13054 = make_ith_pack_parameter_name (DECL_NAME (r), i);
13056 else if (!type)
13057 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13059 if (type == error_mark_node)
13060 RETURN (error_mark_node);
13061 TREE_TYPE (r) = type;
13062 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13064 if (DECL_C_BIT_FIELD (r))
13065 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
13066 number of bits. */
13067 DECL_BIT_FIELD_REPRESENTATIVE (r)
13068 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
13069 complain, in_decl,
13070 /*integral_constant_expression_p=*/true);
13071 if (DECL_INITIAL (t))
13073 /* Set up DECL_TEMPLATE_INFO so that we can get at the
13074 NSDMI in perform_member_init. Still set DECL_INITIAL
13075 so that we know there is one. */
13076 DECL_INITIAL (r) = void_node;
13077 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
13078 retrofit_lang_decl (r);
13079 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
13081 /* We don't have to set DECL_CONTEXT here; it is set by
13082 finish_member_declaration. */
13083 DECL_CHAIN (r) = NULL_TREE;
13085 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13086 args, complain, in_decl);
13088 if (vec)
13089 TREE_VEC_ELT (vec, i) = r;
13092 if (vec)
13093 r = vec;
13095 break;
13097 case USING_DECL:
13098 /* We reach here only for member using decls. We also need to check
13099 uses_template_parms because DECL_DEPENDENT_P is not set for a
13100 using-declaration that designates a member of the current
13101 instantiation (c++/53549). */
13102 if (DECL_DEPENDENT_P (t)
13103 || uses_template_parms (USING_DECL_SCOPE (t)))
13105 tree scope = USING_DECL_SCOPE (t);
13106 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
13107 if (PACK_EXPANSION_P (scope))
13109 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
13110 int len = TREE_VEC_LENGTH (vec);
13111 r = make_tree_vec (len);
13112 for (int i = 0; i < len; ++i)
13114 tree escope = TREE_VEC_ELT (vec, i);
13115 tree elt = do_class_using_decl (escope, name);
13116 if (!elt)
13118 r = error_mark_node;
13119 break;
13121 else
13123 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
13124 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
13126 TREE_VEC_ELT (r, i) = elt;
13129 else
13131 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
13132 complain, in_decl);
13133 r = do_class_using_decl (inst_scope, name);
13134 if (!r)
13135 r = error_mark_node;
13136 else
13138 TREE_PROTECTED (r) = TREE_PROTECTED (t);
13139 TREE_PRIVATE (r) = TREE_PRIVATE (t);
13143 else
13145 r = copy_node (t);
13146 DECL_CHAIN (r) = NULL_TREE;
13148 break;
13150 case TYPE_DECL:
13151 case VAR_DECL:
13153 tree argvec = NULL_TREE;
13154 tree gen_tmpl = NULL_TREE;
13155 tree spec;
13156 tree tmpl = NULL_TREE;
13157 tree ctx;
13158 tree type = NULL_TREE;
13159 bool local_p;
13161 if (TREE_TYPE (t) == error_mark_node)
13162 RETURN (error_mark_node);
13164 if (TREE_CODE (t) == TYPE_DECL
13165 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
13167 /* If this is the canonical decl, we don't have to
13168 mess with instantiations, and often we can't (for
13169 typename, template type parms and such). Note that
13170 TYPE_NAME is not correct for the above test if
13171 we've copied the type for a typedef. */
13172 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13173 if (type == error_mark_node)
13174 RETURN (error_mark_node);
13175 r = TYPE_NAME (type);
13176 break;
13179 /* Check to see if we already have the specialization we
13180 need. */
13181 spec = NULL_TREE;
13182 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
13184 /* T is a static data member or namespace-scope entity.
13185 We have to substitute into namespace-scope variables
13186 (not just variable templates) because of cases like:
13188 template <class T> void f() { extern T t; }
13190 where the entity referenced is not known until
13191 instantiation time. */
13192 local_p = false;
13193 ctx = DECL_CONTEXT (t);
13194 if (DECL_CLASS_SCOPE_P (t))
13196 ctx = tsubst_aggr_type (ctx, args,
13197 complain,
13198 in_decl, /*entering_scope=*/1);
13199 /* If CTX is unchanged, then T is in fact the
13200 specialization we want. That situation occurs when
13201 referencing a static data member within in its own
13202 class. We can use pointer equality, rather than
13203 same_type_p, because DECL_CONTEXT is always
13204 canonical... */
13205 if (ctx == DECL_CONTEXT (t)
13206 /* ... unless T is a member template; in which
13207 case our caller can be willing to create a
13208 specialization of that template represented
13209 by T. */
13210 && !(DECL_TI_TEMPLATE (t)
13211 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
13212 spec = t;
13215 if (!spec)
13217 tmpl = DECL_TI_TEMPLATE (t);
13218 gen_tmpl = most_general_template (tmpl);
13219 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
13220 if (argvec != error_mark_node)
13221 argvec = (coerce_innermost_template_parms
13222 (DECL_TEMPLATE_PARMS (gen_tmpl),
13223 argvec, t, complain,
13224 /*all*/true, /*defarg*/true));
13225 if (argvec == error_mark_node)
13226 RETURN (error_mark_node);
13227 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13228 spec = retrieve_specialization (gen_tmpl, argvec, hash);
13231 else
13233 /* A local variable. */
13234 local_p = true;
13235 /* Subsequent calls to pushdecl will fill this in. */
13236 ctx = NULL_TREE;
13237 /* Unless this is a reference to a static variable from an
13238 enclosing function, in which case we need to fill it in now. */
13239 if (TREE_STATIC (t))
13241 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
13242 if (fn != current_function_decl)
13243 ctx = fn;
13245 spec = retrieve_local_specialization (t);
13247 /* If we already have the specialization we need, there is
13248 nothing more to do. */
13249 if (spec)
13251 r = spec;
13252 break;
13255 /* Create a new node for the specialization we need. */
13256 r = copy_decl (t);
13257 if (type == NULL_TREE)
13259 if (is_typedef_decl (t))
13260 type = DECL_ORIGINAL_TYPE (t);
13261 else
13262 type = TREE_TYPE (t);
13263 if (VAR_P (t)
13264 && VAR_HAD_UNKNOWN_BOUND (t)
13265 && type != error_mark_node)
13266 type = strip_array_domain (type);
13267 tree sub_args = args;
13268 if (tree auto_node = type_uses_auto (type))
13270 /* Mask off any template args past the variable's context so we
13271 don't replace the auto with an unrelated argument. */
13272 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
13273 int extra = TMPL_ARGS_DEPTH (args) - nouter;
13274 if (extra > 0)
13275 /* This should never happen with the new lambda instantiation
13276 model, but keep the handling just in case. */
13277 gcc_assert (!CHECKING_P),
13278 sub_args = strip_innermost_template_args (args, extra);
13280 type = tsubst (type, sub_args, complain, in_decl);
13282 if (VAR_P (r))
13284 /* Even if the original location is out of scope, the
13285 newly substituted one is not. */
13286 DECL_DEAD_FOR_LOCAL (r) = 0;
13287 DECL_INITIALIZED_P (r) = 0;
13288 DECL_TEMPLATE_INSTANTIATED (r) = 0;
13289 if (type == error_mark_node)
13290 RETURN (error_mark_node);
13291 if (TREE_CODE (type) == FUNCTION_TYPE)
13293 /* It may seem that this case cannot occur, since:
13295 typedef void f();
13296 void g() { f x; }
13298 declares a function, not a variable. However:
13300 typedef void f();
13301 template <typename T> void g() { T t; }
13302 template void g<f>();
13304 is an attempt to declare a variable with function
13305 type. */
13306 error ("variable %qD has function type",
13307 /* R is not yet sufficiently initialized, so we
13308 just use its name. */
13309 DECL_NAME (r));
13310 RETURN (error_mark_node);
13312 type = complete_type (type);
13313 /* Wait until cp_finish_decl to set this again, to handle
13314 circular dependency (template/instantiate6.C). */
13315 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
13316 type = check_var_type (DECL_NAME (r), type);
13318 if (DECL_HAS_VALUE_EXPR_P (t))
13320 tree ve = DECL_VALUE_EXPR (t);
13321 ve = tsubst_expr (ve, args, complain, in_decl,
13322 /*constant_expression_p=*/false);
13323 if (REFERENCE_REF_P (ve))
13325 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
13326 ve = TREE_OPERAND (ve, 0);
13328 SET_DECL_VALUE_EXPR (r, ve);
13330 if (CP_DECL_THREAD_LOCAL_P (r)
13331 && !processing_template_decl)
13332 set_decl_tls_model (r, decl_default_tls_model (r));
13334 else if (DECL_SELF_REFERENCE_P (t))
13335 SET_DECL_SELF_REFERENCE_P (r);
13336 TREE_TYPE (r) = type;
13337 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13338 DECL_CONTEXT (r) = ctx;
13339 /* Clear out the mangled name and RTL for the instantiation. */
13340 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13341 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
13342 SET_DECL_RTL (r, NULL);
13343 /* The initializer must not be expanded until it is required;
13344 see [temp.inst]. */
13345 DECL_INITIAL (r) = NULL_TREE;
13346 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
13347 if (VAR_P (r))
13349 if (DECL_LANG_SPECIFIC (r))
13350 SET_DECL_DEPENDENT_INIT_P (r, false);
13352 SET_DECL_MODE (r, VOIDmode);
13354 /* Possibly limit visibility based on template args. */
13355 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13356 if (DECL_VISIBILITY_SPECIFIED (t))
13358 DECL_VISIBILITY_SPECIFIED (r) = 0;
13359 DECL_ATTRIBUTES (r)
13360 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13362 determine_visibility (r);
13365 if (!local_p)
13367 /* A static data member declaration is always marked
13368 external when it is declared in-class, even if an
13369 initializer is present. We mimic the non-template
13370 processing here. */
13371 DECL_EXTERNAL (r) = 1;
13372 if (DECL_NAMESPACE_SCOPE_P (t))
13373 DECL_NOT_REALLY_EXTERN (r) = 1;
13375 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
13376 SET_DECL_IMPLICIT_INSTANTIATION (r);
13377 register_specialization (r, gen_tmpl, argvec, false, hash);
13379 else
13381 if (DECL_LANG_SPECIFIC (r))
13382 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13383 if (!cp_unevaluated_operand)
13384 register_local_specialization (r, t);
13387 DECL_CHAIN (r) = NULL_TREE;
13389 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
13390 /*flags=*/0,
13391 args, complain, in_decl);
13393 /* Preserve a typedef that names a type. */
13394 if (is_typedef_decl (r) && type != error_mark_node)
13396 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
13397 set_underlying_type (r);
13398 if (TYPE_DECL_ALIAS_P (r))
13399 /* An alias template specialization can be dependent
13400 even if its underlying type is not. */
13401 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
13404 layout_decl (r, 0);
13406 break;
13408 default:
13409 gcc_unreachable ();
13411 #undef RETURN
13413 out:
13414 /* Restore the file and line information. */
13415 input_location = saved_loc;
13417 return r;
13420 /* Substitute into the ARG_TYPES of a function type.
13421 If END is a TREE_CHAIN, leave it and any following types
13422 un-substituted. */
13424 static tree
13425 tsubst_arg_types (tree arg_types,
13426 tree args,
13427 tree end,
13428 tsubst_flags_t complain,
13429 tree in_decl)
13431 tree remaining_arg_types;
13432 tree type = NULL_TREE;
13433 int i = 1;
13434 tree expanded_args = NULL_TREE;
13435 tree default_arg;
13437 if (!arg_types || arg_types == void_list_node || arg_types == end)
13438 return arg_types;
13440 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
13441 args, end, complain, in_decl);
13442 if (remaining_arg_types == error_mark_node)
13443 return error_mark_node;
13445 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
13447 /* For a pack expansion, perform substitution on the
13448 entire expression. Later on, we'll handle the arguments
13449 one-by-one. */
13450 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
13451 args, complain, in_decl);
13453 if (TREE_CODE (expanded_args) == TREE_VEC)
13454 /* So that we'll spin through the parameters, one by one. */
13455 i = TREE_VEC_LENGTH (expanded_args);
13456 else
13458 /* We only partially substituted into the parameter
13459 pack. Our type is TYPE_PACK_EXPANSION. */
13460 type = expanded_args;
13461 expanded_args = NULL_TREE;
13465 while (i > 0) {
13466 --i;
13468 if (expanded_args)
13469 type = TREE_VEC_ELT (expanded_args, i);
13470 else if (!type)
13471 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
13473 if (type == error_mark_node)
13474 return error_mark_node;
13475 if (VOID_TYPE_P (type))
13477 if (complain & tf_error)
13479 error ("invalid parameter type %qT", type);
13480 if (in_decl)
13481 error ("in declaration %q+D", in_decl);
13483 return error_mark_node;
13485 /* DR 657. */
13486 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
13487 return error_mark_node;
13489 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13490 top-level qualifiers as required. */
13491 type = cv_unqualified (type_decays_to (type));
13493 /* We do not substitute into default arguments here. The standard
13494 mandates that they be instantiated only when needed, which is
13495 done in build_over_call. */
13496 default_arg = TREE_PURPOSE (arg_types);
13498 /* Except that we do substitute default arguments under tsubst_lambda_expr,
13499 since the new op() won't have any associated template arguments for us
13500 to refer to later. */
13501 if (lambda_fn_in_template_p (in_decl))
13502 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
13503 false/*fn*/, false/*constexpr*/);
13505 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
13507 /* We've instantiated a template before its default arguments
13508 have been parsed. This can happen for a nested template
13509 class, and is not an error unless we require the default
13510 argument in a call of this function. */
13511 remaining_arg_types =
13512 tree_cons (default_arg, type, remaining_arg_types);
13513 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
13515 else
13516 remaining_arg_types =
13517 hash_tree_cons (default_arg, type, remaining_arg_types);
13520 return remaining_arg_types;
13523 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13524 *not* handle the exception-specification for FNTYPE, because the
13525 initial substitution of explicitly provided template parameters
13526 during argument deduction forbids substitution into the
13527 exception-specification:
13529 [temp.deduct]
13531 All references in the function type of the function template to the
13532 corresponding template parameters are replaced by the specified tem-
13533 plate argument values. If a substitution in a template parameter or
13534 in the function type of the function template results in an invalid
13535 type, type deduction fails. [Note: The equivalent substitution in
13536 exception specifications is done only when the function is instanti-
13537 ated, at which point a program is ill-formed if the substitution
13538 results in an invalid type.] */
13540 static tree
13541 tsubst_function_type (tree t,
13542 tree args,
13543 tsubst_flags_t complain,
13544 tree in_decl)
13546 tree return_type;
13547 tree arg_types = NULL_TREE;
13548 tree fntype;
13550 /* The TYPE_CONTEXT is not used for function/method types. */
13551 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
13553 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13554 failure. */
13555 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13557 if (late_return_type_p)
13559 /* Substitute the argument types. */
13560 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13561 complain, in_decl);
13562 if (arg_types == error_mark_node)
13563 return error_mark_node;
13565 tree save_ccp = current_class_ptr;
13566 tree save_ccr = current_class_ref;
13567 tree this_type = (TREE_CODE (t) == METHOD_TYPE
13568 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
13569 bool do_inject = this_type && CLASS_TYPE_P (this_type);
13570 if (do_inject)
13572 /* DR 1207: 'this' is in scope in the trailing return type. */
13573 inject_this_parameter (this_type, cp_type_quals (this_type));
13576 /* Substitute the return type. */
13577 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13579 if (do_inject)
13581 current_class_ptr = save_ccp;
13582 current_class_ref = save_ccr;
13585 else
13586 /* Substitute the return type. */
13587 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13589 if (return_type == error_mark_node)
13590 return error_mark_node;
13591 /* DR 486 clarifies that creation of a function type with an
13592 invalid return type is a deduction failure. */
13593 if (TREE_CODE (return_type) == ARRAY_TYPE
13594 || TREE_CODE (return_type) == FUNCTION_TYPE)
13596 if (complain & tf_error)
13598 if (TREE_CODE (return_type) == ARRAY_TYPE)
13599 error ("function returning an array");
13600 else
13601 error ("function returning a function");
13603 return error_mark_node;
13605 /* And DR 657. */
13606 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
13607 return error_mark_node;
13609 if (!late_return_type_p)
13611 /* Substitute the argument types. */
13612 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13613 complain, in_decl);
13614 if (arg_types == error_mark_node)
13615 return error_mark_node;
13618 /* Construct a new type node and return it. */
13619 if (TREE_CODE (t) == FUNCTION_TYPE)
13621 fntype = build_function_type (return_type, arg_types);
13622 fntype = apply_memfn_quals (fntype,
13623 type_memfn_quals (t),
13624 type_memfn_rqual (t));
13626 else
13628 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13629 /* Don't pick up extra function qualifiers from the basetype. */
13630 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13631 if (! MAYBE_CLASS_TYPE_P (r))
13633 /* [temp.deduct]
13635 Type deduction may fail for any of the following
13636 reasons:
13638 -- Attempting to create "pointer to member of T" when T
13639 is not a class type. */
13640 if (complain & tf_error)
13641 error ("creating pointer to member function of non-class type %qT",
13643 return error_mark_node;
13646 fntype = build_method_type_directly (r, return_type,
13647 TREE_CHAIN (arg_types));
13648 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
13650 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
13652 if (late_return_type_p)
13653 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
13655 return fntype;
13658 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
13659 ARGS into that specification, and return the substituted
13660 specification. If there is no specification, return NULL_TREE. */
13662 static tree
13663 tsubst_exception_specification (tree fntype,
13664 tree args,
13665 tsubst_flags_t complain,
13666 tree in_decl,
13667 bool defer_ok)
13669 tree specs;
13670 tree new_specs;
13672 specs = TYPE_RAISES_EXCEPTIONS (fntype);
13673 new_specs = NULL_TREE;
13674 if (specs && TREE_PURPOSE (specs))
13676 /* A noexcept-specifier. */
13677 tree expr = TREE_PURPOSE (specs);
13678 if (TREE_CODE (expr) == INTEGER_CST)
13679 new_specs = expr;
13680 else if (defer_ok)
13682 /* Defer instantiation of noexcept-specifiers to avoid
13683 excessive instantiations (c++/49107). */
13684 new_specs = make_node (DEFERRED_NOEXCEPT);
13685 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
13687 /* We already partially instantiated this member template,
13688 so combine the new args with the old. */
13689 DEFERRED_NOEXCEPT_PATTERN (new_specs)
13690 = DEFERRED_NOEXCEPT_PATTERN (expr);
13691 DEFERRED_NOEXCEPT_ARGS (new_specs)
13692 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
13694 else
13696 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
13697 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
13700 else
13701 new_specs = tsubst_copy_and_build
13702 (expr, args, complain, in_decl, /*function_p=*/false,
13703 /*integral_constant_expression_p=*/true);
13704 new_specs = build_noexcept_spec (new_specs, complain);
13706 else if (specs)
13708 if (! TREE_VALUE (specs))
13709 new_specs = specs;
13710 else
13711 while (specs)
13713 tree spec;
13714 int i, len = 1;
13715 tree expanded_specs = NULL_TREE;
13717 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
13719 /* Expand the pack expansion type. */
13720 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
13721 args, complain,
13722 in_decl);
13724 if (expanded_specs == error_mark_node)
13725 return error_mark_node;
13726 else if (TREE_CODE (expanded_specs) == TREE_VEC)
13727 len = TREE_VEC_LENGTH (expanded_specs);
13728 else
13730 /* We're substituting into a member template, so
13731 we got a TYPE_PACK_EXPANSION back. Add that
13732 expansion and move on. */
13733 gcc_assert (TREE_CODE (expanded_specs)
13734 == TYPE_PACK_EXPANSION);
13735 new_specs = add_exception_specifier (new_specs,
13736 expanded_specs,
13737 complain);
13738 specs = TREE_CHAIN (specs);
13739 continue;
13743 for (i = 0; i < len; ++i)
13745 if (expanded_specs)
13746 spec = TREE_VEC_ELT (expanded_specs, i);
13747 else
13748 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
13749 if (spec == error_mark_node)
13750 return spec;
13751 new_specs = add_exception_specifier (new_specs, spec,
13752 complain);
13755 specs = TREE_CHAIN (specs);
13758 return new_specs;
13761 /* Take the tree structure T and replace template parameters used
13762 therein with the argument vector ARGS. IN_DECL is an associated
13763 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
13764 Issue error and warning messages under control of COMPLAIN. Note
13765 that we must be relatively non-tolerant of extensions here, in
13766 order to preserve conformance; if we allow substitutions that
13767 should not be allowed, we may allow argument deductions that should
13768 not succeed, and therefore report ambiguous overload situations
13769 where there are none. In theory, we could allow the substitution,
13770 but indicate that it should have failed, and allow our caller to
13771 make sure that the right thing happens, but we don't try to do this
13772 yet.
13774 This function is used for dealing with types, decls and the like;
13775 for expressions, use tsubst_expr or tsubst_copy. */
13777 tree
13778 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13780 enum tree_code code;
13781 tree type, r = NULL_TREE;
13783 if (t == NULL_TREE || t == error_mark_node
13784 || t == integer_type_node
13785 || t == void_type_node
13786 || t == char_type_node
13787 || t == unknown_type_node
13788 || TREE_CODE (t) == NAMESPACE_DECL
13789 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
13790 return t;
13792 if (DECL_P (t))
13793 return tsubst_decl (t, args, complain);
13795 if (args == NULL_TREE)
13796 return t;
13798 code = TREE_CODE (t);
13800 if (code == IDENTIFIER_NODE)
13801 type = IDENTIFIER_TYPE_VALUE (t);
13802 else
13803 type = TREE_TYPE (t);
13805 gcc_assert (type != unknown_type_node);
13807 /* Reuse typedefs. We need to do this to handle dependent attributes,
13808 such as attribute aligned. */
13809 if (TYPE_P (t)
13810 && typedef_variant_p (t))
13812 tree decl = TYPE_NAME (t);
13814 if (alias_template_specialization_p (t))
13816 /* DECL represents an alias template and we want to
13817 instantiate it. */
13818 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13819 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13820 r = instantiate_alias_template (tmpl, gen_args, complain);
13822 else if (DECL_CLASS_SCOPE_P (decl)
13823 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
13824 && uses_template_parms (DECL_CONTEXT (decl)))
13826 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13827 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13828 r = retrieve_specialization (tmpl, gen_args, 0);
13830 else if (DECL_FUNCTION_SCOPE_P (decl)
13831 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
13832 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
13833 r = retrieve_local_specialization (decl);
13834 else
13835 /* The typedef is from a non-template context. */
13836 return t;
13838 if (r)
13840 r = TREE_TYPE (r);
13841 r = cp_build_qualified_type_real
13842 (r, cp_type_quals (t) | cp_type_quals (r),
13843 complain | tf_ignore_bad_quals);
13844 return r;
13846 else
13848 /* We don't have an instantiation yet, so drop the typedef. */
13849 int quals = cp_type_quals (t);
13850 t = DECL_ORIGINAL_TYPE (decl);
13851 t = cp_build_qualified_type_real (t, quals,
13852 complain | tf_ignore_bad_quals);
13856 bool fndecl_type = (complain & tf_fndecl_type);
13857 complain &= ~tf_fndecl_type;
13859 if (type
13860 && code != TYPENAME_TYPE
13861 && code != TEMPLATE_TYPE_PARM
13862 && code != TEMPLATE_PARM_INDEX
13863 && code != IDENTIFIER_NODE
13864 && code != FUNCTION_TYPE
13865 && code != METHOD_TYPE)
13866 type = tsubst (type, args, complain, in_decl);
13867 if (type == error_mark_node)
13868 return error_mark_node;
13870 switch (code)
13872 case RECORD_TYPE:
13873 case UNION_TYPE:
13874 case ENUMERAL_TYPE:
13875 return tsubst_aggr_type (t, args, complain, in_decl,
13876 /*entering_scope=*/0);
13878 case ERROR_MARK:
13879 case IDENTIFIER_NODE:
13880 case VOID_TYPE:
13881 case REAL_TYPE:
13882 case COMPLEX_TYPE:
13883 case VECTOR_TYPE:
13884 case BOOLEAN_TYPE:
13885 case NULLPTR_TYPE:
13886 case LANG_TYPE:
13887 return t;
13889 case INTEGER_TYPE:
13890 if (t == integer_type_node)
13891 return t;
13893 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
13894 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
13895 return t;
13898 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
13900 max = tsubst_expr (omax, args, complain, in_decl,
13901 /*integral_constant_expression_p=*/false);
13903 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13904 needed. */
13905 if (TREE_CODE (max) == NOP_EXPR
13906 && TREE_SIDE_EFFECTS (omax)
13907 && !TREE_TYPE (max))
13908 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
13910 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13911 with TREE_SIDE_EFFECTS that indicates this is not an integral
13912 constant expression. */
13913 if (processing_template_decl
13914 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
13916 gcc_assert (TREE_CODE (max) == NOP_EXPR);
13917 TREE_SIDE_EFFECTS (max) = 1;
13920 return compute_array_index_type (NULL_TREE, max, complain);
13923 case TEMPLATE_TYPE_PARM:
13924 case TEMPLATE_TEMPLATE_PARM:
13925 case BOUND_TEMPLATE_TEMPLATE_PARM:
13926 case TEMPLATE_PARM_INDEX:
13928 int idx;
13929 int level;
13930 int levels;
13931 tree arg = NULL_TREE;
13933 /* Early in template argument deduction substitution, we don't
13934 want to reduce the level of 'auto', or it will be confused
13935 with a normal template parm in subsequent deduction. */
13936 if (is_auto (t) && (complain & tf_partial))
13937 return t;
13939 r = NULL_TREE;
13941 gcc_assert (TREE_VEC_LENGTH (args) > 0);
13942 template_parm_level_and_index (t, &level, &idx);
13944 levels = TMPL_ARGS_DEPTH (args);
13945 if (level <= levels
13946 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
13948 arg = TMPL_ARG (args, level, idx);
13950 /* See through ARGUMENT_PACK_SELECT arguments. */
13951 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
13952 arg = argument_pack_select_arg (arg);
13955 if (arg == error_mark_node)
13956 return error_mark_node;
13957 else if (arg != NULL_TREE)
13959 if (ARGUMENT_PACK_P (arg))
13960 /* If ARG is an argument pack, we don't actually want to
13961 perform a substitution here, because substitutions
13962 for argument packs are only done
13963 element-by-element. We can get to this point when
13964 substituting the type of a non-type template
13965 parameter pack, when that type actually contains
13966 template parameter packs from an outer template, e.g.,
13968 template<typename... Types> struct A {
13969 template<Types... Values> struct B { };
13970 }; */
13971 return t;
13973 if (code == TEMPLATE_TYPE_PARM)
13975 int quals;
13976 gcc_assert (TYPE_P (arg));
13978 quals = cp_type_quals (arg) | cp_type_quals (t);
13980 return cp_build_qualified_type_real
13981 (arg, quals, complain | tf_ignore_bad_quals);
13983 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13985 /* We are processing a type constructed from a
13986 template template parameter. */
13987 tree argvec = tsubst (TYPE_TI_ARGS (t),
13988 args, complain, in_decl);
13989 if (argvec == error_mark_node)
13990 return error_mark_node;
13992 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
13993 || TREE_CODE (arg) == TEMPLATE_DECL
13994 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
13996 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
13997 /* Consider this code:
13999 template <template <class> class Template>
14000 struct Internal {
14001 template <class Arg> using Bind = Template<Arg>;
14004 template <template <class> class Template, class Arg>
14005 using Instantiate = Template<Arg>; //#0
14007 template <template <class> class Template,
14008 class Argument>
14009 using Bind =
14010 Instantiate<Internal<Template>::template Bind,
14011 Argument>; //#1
14013 When #1 is parsed, the
14014 BOUND_TEMPLATE_TEMPLATE_PARM representing the
14015 parameter `Template' in #0 matches the
14016 UNBOUND_CLASS_TEMPLATE representing the argument
14017 `Internal<Template>::template Bind'; We then want
14018 to assemble the type `Bind<Argument>' that can't
14019 be fully created right now, because
14020 `Internal<Template>' not being complete, the Bind
14021 template cannot be looked up in that context. So
14022 we need to "store" `Bind<Argument>' for later
14023 when the context of Bind becomes complete. Let's
14024 store that in a TYPENAME_TYPE. */
14025 return make_typename_type (TYPE_CONTEXT (arg),
14026 build_nt (TEMPLATE_ID_EXPR,
14027 TYPE_IDENTIFIER (arg),
14028 argvec),
14029 typename_type,
14030 complain);
14032 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
14033 are resolving nested-types in the signature of a
14034 member function templates. Otherwise ARG is a
14035 TEMPLATE_DECL and is the real template to be
14036 instantiated. */
14037 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
14038 arg = TYPE_NAME (arg);
14040 r = lookup_template_class (arg,
14041 argvec, in_decl,
14042 DECL_CONTEXT (arg),
14043 /*entering_scope=*/0,
14044 complain);
14045 return cp_build_qualified_type_real
14046 (r, cp_type_quals (t) | cp_type_quals (r), complain);
14048 else if (code == TEMPLATE_TEMPLATE_PARM)
14049 return arg;
14050 else
14051 /* TEMPLATE_PARM_INDEX. */
14052 return convert_from_reference (unshare_expr (arg));
14055 if (level == 1)
14056 /* This can happen during the attempted tsubst'ing in
14057 unify. This means that we don't yet have any information
14058 about the template parameter in question. */
14059 return t;
14061 /* If we get here, we must have been looking at a parm for a
14062 more deeply nested template. Make a new version of this
14063 template parameter, but with a lower level. */
14064 switch (code)
14066 case TEMPLATE_TYPE_PARM:
14067 case TEMPLATE_TEMPLATE_PARM:
14068 case BOUND_TEMPLATE_TEMPLATE_PARM:
14069 if (cp_type_quals (t))
14071 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
14072 r = cp_build_qualified_type_real
14073 (r, cp_type_quals (t),
14074 complain | (code == TEMPLATE_TYPE_PARM
14075 ? tf_ignore_bad_quals : 0));
14077 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
14078 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
14079 && (r = (TEMPLATE_PARM_DESCENDANTS
14080 (TEMPLATE_TYPE_PARM_INDEX (t))))
14081 && (r = TREE_TYPE (r))
14082 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
14083 /* Break infinite recursion when substituting the constraints
14084 of a constrained placeholder. */;
14085 else
14087 r = copy_type (t);
14088 TEMPLATE_TYPE_PARM_INDEX (r)
14089 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
14090 r, levels, args, complain);
14091 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
14092 TYPE_MAIN_VARIANT (r) = r;
14093 TYPE_POINTER_TO (r) = NULL_TREE;
14094 TYPE_REFERENCE_TO (r) = NULL_TREE;
14096 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
14098 /* Propagate constraints on placeholders. */
14099 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
14100 PLACEHOLDER_TYPE_CONSTRAINTS (r)
14101 = tsubst_constraint (constr, args, complain, in_decl);
14102 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
14104 if (DECL_TEMPLATE_TEMPLATE_PARM_P (pl))
14105 pl = tsubst (pl, args, complain, in_decl);
14106 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
14110 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
14111 /* We have reduced the level of the template
14112 template parameter, but not the levels of its
14113 template parameters, so canonical_type_parameter
14114 will not be able to find the canonical template
14115 template parameter for this level. Thus, we
14116 require structural equality checking to compare
14117 TEMPLATE_TEMPLATE_PARMs. */
14118 SET_TYPE_STRUCTURAL_EQUALITY (r);
14119 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
14120 SET_TYPE_STRUCTURAL_EQUALITY (r);
14121 else
14122 TYPE_CANONICAL (r) = canonical_type_parameter (r);
14124 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
14126 tree tinfo = TYPE_TEMPLATE_INFO (t);
14127 /* We might need to substitute into the types of non-type
14128 template parameters. */
14129 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
14130 complain, in_decl);
14131 if (tmpl == error_mark_node)
14132 return error_mark_node;
14133 tree argvec = tsubst (TI_ARGS (tinfo), args,
14134 complain, in_decl);
14135 if (argvec == error_mark_node)
14136 return error_mark_node;
14138 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
14139 = build_template_info (tmpl, argvec);
14142 break;
14144 case TEMPLATE_PARM_INDEX:
14145 /* OK, now substitute the type of the non-type parameter. We
14146 couldn't do it earlier because it might be an auto parameter,
14147 and we wouldn't need to if we had an argument. */
14148 type = tsubst (type, args, complain, in_decl);
14149 if (type == error_mark_node)
14150 return error_mark_node;
14151 r = reduce_template_parm_level (t, type, levels, args, complain);
14152 break;
14154 default:
14155 gcc_unreachable ();
14158 return r;
14161 case TREE_LIST:
14163 tree purpose, value, chain;
14165 if (t == void_list_node)
14166 return t;
14168 purpose = TREE_PURPOSE (t);
14169 if (purpose)
14171 purpose = tsubst (purpose, args, complain, in_decl);
14172 if (purpose == error_mark_node)
14173 return error_mark_node;
14175 value = TREE_VALUE (t);
14176 if (value)
14178 value = tsubst (value, args, complain, in_decl);
14179 if (value == error_mark_node)
14180 return error_mark_node;
14182 chain = TREE_CHAIN (t);
14183 if (chain && chain != void_type_node)
14185 chain = tsubst (chain, args, complain, in_decl);
14186 if (chain == error_mark_node)
14187 return error_mark_node;
14189 if (purpose == TREE_PURPOSE (t)
14190 && value == TREE_VALUE (t)
14191 && chain == TREE_CHAIN (t))
14192 return t;
14193 return hash_tree_cons (purpose, value, chain);
14196 case TREE_BINFO:
14197 /* We should never be tsubsting a binfo. */
14198 gcc_unreachable ();
14200 case TREE_VEC:
14201 /* A vector of template arguments. */
14202 gcc_assert (!type);
14203 return tsubst_template_args (t, args, complain, in_decl);
14205 case POINTER_TYPE:
14206 case REFERENCE_TYPE:
14208 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
14209 return t;
14211 /* [temp.deduct]
14213 Type deduction may fail for any of the following
14214 reasons:
14216 -- Attempting to create a pointer to reference type.
14217 -- Attempting to create a reference to a reference type or
14218 a reference to void.
14220 Core issue 106 says that creating a reference to a reference
14221 during instantiation is no longer a cause for failure. We
14222 only enforce this check in strict C++98 mode. */
14223 if ((TREE_CODE (type) == REFERENCE_TYPE
14224 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
14225 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
14227 static location_t last_loc;
14229 /* We keep track of the last time we issued this error
14230 message to avoid spewing a ton of messages during a
14231 single bad template instantiation. */
14232 if (complain & tf_error
14233 && last_loc != input_location)
14235 if (VOID_TYPE_P (type))
14236 error ("forming reference to void");
14237 else if (code == POINTER_TYPE)
14238 error ("forming pointer to reference type %qT", type);
14239 else
14240 error ("forming reference to reference type %qT", type);
14241 last_loc = input_location;
14244 return error_mark_node;
14246 else if (TREE_CODE (type) == FUNCTION_TYPE
14247 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
14248 || type_memfn_rqual (type) != REF_QUAL_NONE))
14250 if (complain & tf_error)
14252 if (code == POINTER_TYPE)
14253 error ("forming pointer to qualified function type %qT",
14254 type);
14255 else
14256 error ("forming reference to qualified function type %qT",
14257 type);
14259 return error_mark_node;
14261 else if (code == POINTER_TYPE)
14263 r = build_pointer_type (type);
14264 if (TREE_CODE (type) == METHOD_TYPE)
14265 r = build_ptrmemfunc_type (r);
14267 else if (TREE_CODE (type) == REFERENCE_TYPE)
14268 /* In C++0x, during template argument substitution, when there is an
14269 attempt to create a reference to a reference type, reference
14270 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14272 "If a template-argument for a template-parameter T names a type
14273 that is a reference to a type A, an attempt to create the type
14274 'lvalue reference to cv T' creates the type 'lvalue reference to
14275 A,' while an attempt to create the type type rvalue reference to
14276 cv T' creates the type T"
14278 r = cp_build_reference_type
14279 (TREE_TYPE (type),
14280 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
14281 else
14282 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
14283 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
14285 if (r != error_mark_node)
14286 /* Will this ever be needed for TYPE_..._TO values? */
14287 layout_type (r);
14289 return r;
14291 case OFFSET_TYPE:
14293 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
14294 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
14296 /* [temp.deduct]
14298 Type deduction may fail for any of the following
14299 reasons:
14301 -- Attempting to create "pointer to member of T" when T
14302 is not a class type. */
14303 if (complain & tf_error)
14304 error ("creating pointer to member of non-class type %qT", r);
14305 return error_mark_node;
14307 if (TREE_CODE (type) == REFERENCE_TYPE)
14309 if (complain & tf_error)
14310 error ("creating pointer to member reference type %qT", type);
14311 return error_mark_node;
14313 if (VOID_TYPE_P (type))
14315 if (complain & tf_error)
14316 error ("creating pointer to member of type void");
14317 return error_mark_node;
14319 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
14320 if (TREE_CODE (type) == FUNCTION_TYPE)
14322 /* The type of the implicit object parameter gets its
14323 cv-qualifiers from the FUNCTION_TYPE. */
14324 tree memptr;
14325 tree method_type
14326 = build_memfn_type (type, r, type_memfn_quals (type),
14327 type_memfn_rqual (type));
14328 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
14329 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
14330 complain);
14332 else
14333 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
14334 cp_type_quals (t),
14335 complain);
14337 case FUNCTION_TYPE:
14338 case METHOD_TYPE:
14340 tree fntype;
14341 tree specs;
14342 fntype = tsubst_function_type (t, args, complain, in_decl);
14343 if (fntype == error_mark_node)
14344 return error_mark_node;
14346 /* Substitute the exception specification. */
14347 specs = tsubst_exception_specification (t, args, complain, in_decl,
14348 /*defer_ok*/fndecl_type);
14349 if (specs == error_mark_node)
14350 return error_mark_node;
14351 if (specs)
14352 fntype = build_exception_variant (fntype, specs);
14353 return fntype;
14355 case ARRAY_TYPE:
14357 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
14358 if (domain == error_mark_node)
14359 return error_mark_node;
14361 /* As an optimization, we avoid regenerating the array type if
14362 it will obviously be the same as T. */
14363 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
14364 return t;
14366 /* These checks should match the ones in create_array_type_for_decl.
14368 [temp.deduct]
14370 The deduction may fail for any of the following reasons:
14372 -- Attempting to create an array with an element type that
14373 is void, a function type, or a reference type, or [DR337]
14374 an abstract class type. */
14375 if (VOID_TYPE_P (type)
14376 || TREE_CODE (type) == FUNCTION_TYPE
14377 || (TREE_CODE (type) == ARRAY_TYPE
14378 && TYPE_DOMAIN (type) == NULL_TREE)
14379 || TREE_CODE (type) == REFERENCE_TYPE)
14381 if (complain & tf_error)
14382 error ("creating array of %qT", type);
14383 return error_mark_node;
14386 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
14387 return error_mark_node;
14389 r = build_cplus_array_type (type, domain);
14391 if (TYPE_USER_ALIGN (t))
14393 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
14394 TYPE_USER_ALIGN (r) = 1;
14397 return r;
14400 case TYPENAME_TYPE:
14402 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14403 in_decl, /*entering_scope=*/1);
14404 if (ctx == error_mark_node)
14405 return error_mark_node;
14407 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
14408 complain, in_decl);
14409 if (f == error_mark_node)
14410 return error_mark_node;
14412 if (!MAYBE_CLASS_TYPE_P (ctx))
14414 if (complain & tf_error)
14415 error ("%qT is not a class, struct, or union type", ctx);
14416 return error_mark_node;
14418 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
14420 /* Normally, make_typename_type does not require that the CTX
14421 have complete type in order to allow things like:
14423 template <class T> struct S { typename S<T>::X Y; };
14425 But, such constructs have already been resolved by this
14426 point, so here CTX really should have complete type, unless
14427 it's a partial instantiation. */
14428 ctx = complete_type (ctx);
14429 if (!COMPLETE_TYPE_P (ctx))
14431 if (complain & tf_error)
14432 cxx_incomplete_type_error (NULL_TREE, ctx);
14433 return error_mark_node;
14437 f = make_typename_type (ctx, f, typename_type,
14438 complain | tf_keep_type_decl);
14439 if (f == error_mark_node)
14440 return f;
14441 if (TREE_CODE (f) == TYPE_DECL)
14443 complain |= tf_ignore_bad_quals;
14444 f = TREE_TYPE (f);
14447 if (TREE_CODE (f) != TYPENAME_TYPE)
14449 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
14451 if (complain & tf_error)
14452 error ("%qT resolves to %qT, which is not an enumeration type",
14453 t, f);
14454 else
14455 return error_mark_node;
14457 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
14459 if (complain & tf_error)
14460 error ("%qT resolves to %qT, which is is not a class type",
14461 t, f);
14462 else
14463 return error_mark_node;
14467 return cp_build_qualified_type_real
14468 (f, cp_type_quals (f) | cp_type_quals (t), complain);
14471 case UNBOUND_CLASS_TEMPLATE:
14473 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14474 in_decl, /*entering_scope=*/1);
14475 tree name = TYPE_IDENTIFIER (t);
14476 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
14478 if (ctx == error_mark_node || name == error_mark_node)
14479 return error_mark_node;
14481 if (parm_list)
14482 parm_list = tsubst_template_parms (parm_list, args, complain);
14483 return make_unbound_class_template (ctx, name, parm_list, complain);
14486 case TYPEOF_TYPE:
14488 tree type;
14490 ++cp_unevaluated_operand;
14491 ++c_inhibit_evaluation_warnings;
14493 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
14494 complain, in_decl,
14495 /*integral_constant_expression_p=*/false);
14497 --cp_unevaluated_operand;
14498 --c_inhibit_evaluation_warnings;
14500 type = finish_typeof (type);
14501 return cp_build_qualified_type_real (type,
14502 cp_type_quals (t)
14503 | cp_type_quals (type),
14504 complain);
14507 case DECLTYPE_TYPE:
14509 tree type;
14511 ++cp_unevaluated_operand;
14512 ++c_inhibit_evaluation_warnings;
14514 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
14515 complain|tf_decltype, in_decl,
14516 /*function_p*/false,
14517 /*integral_constant_expression*/false);
14519 if (DECLTYPE_FOR_INIT_CAPTURE (t))
14521 if (type == NULL_TREE)
14523 if (complain & tf_error)
14524 error ("empty initializer in lambda init-capture");
14525 type = error_mark_node;
14527 else if (TREE_CODE (type) == TREE_LIST)
14528 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
14531 --cp_unevaluated_operand;
14532 --c_inhibit_evaluation_warnings;
14534 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
14535 type = lambda_capture_field_type (type,
14536 DECLTYPE_FOR_INIT_CAPTURE (t),
14537 DECLTYPE_FOR_REF_CAPTURE (t));
14538 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
14539 type = lambda_proxy_type (type);
14540 else
14542 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
14543 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
14544 && EXPR_P (type))
14545 /* In a template ~id could be either a complement expression
14546 or an unqualified-id naming a destructor; if instantiating
14547 it produces an expression, it's not an id-expression or
14548 member access. */
14549 id = false;
14550 type = finish_decltype_type (type, id, complain);
14552 return cp_build_qualified_type_real (type,
14553 cp_type_quals (t)
14554 | cp_type_quals (type),
14555 complain | tf_ignore_bad_quals);
14558 case UNDERLYING_TYPE:
14560 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
14561 complain, in_decl);
14562 return finish_underlying_type (type);
14565 case TYPE_ARGUMENT_PACK:
14566 case NONTYPE_ARGUMENT_PACK:
14568 tree r;
14570 if (code == NONTYPE_ARGUMENT_PACK)
14571 r = make_node (code);
14572 else
14573 r = cxx_make_type (code);
14575 tree pack_args = ARGUMENT_PACK_ARGS (t);
14576 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
14577 SET_ARGUMENT_PACK_ARGS (r, pack_args);
14579 return r;
14582 case VOID_CST:
14583 case INTEGER_CST:
14584 case REAL_CST:
14585 case STRING_CST:
14586 case PLUS_EXPR:
14587 case MINUS_EXPR:
14588 case NEGATE_EXPR:
14589 case NOP_EXPR:
14590 case INDIRECT_REF:
14591 case ADDR_EXPR:
14592 case CALL_EXPR:
14593 case ARRAY_REF:
14594 case SCOPE_REF:
14595 /* We should use one of the expression tsubsts for these codes. */
14596 gcc_unreachable ();
14598 default:
14599 sorry ("use of %qs in template", get_tree_code_name (code));
14600 return error_mark_node;
14604 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
14605 expression on the left-hand side of the "." or "->" operator. We
14606 only do the lookup if we had a dependent BASELINK. Otherwise we
14607 adjust it onto the instantiated heirarchy. */
14609 static tree
14610 tsubst_baselink (tree baselink, tree object_type,
14611 tree args, tsubst_flags_t complain, tree in_decl)
14613 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
14614 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
14615 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
14617 tree optype = BASELINK_OPTYPE (baselink);
14618 optype = tsubst (optype, args, complain, in_decl);
14620 tree template_args = NULL_TREE;
14621 bool template_id_p = false;
14622 tree fns = BASELINK_FUNCTIONS (baselink);
14623 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
14625 template_id_p = true;
14626 template_args = TREE_OPERAND (fns, 1);
14627 fns = TREE_OPERAND (fns, 0);
14628 if (template_args)
14629 template_args = tsubst_template_args (template_args, args,
14630 complain, in_decl);
14633 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
14634 binfo_type = tsubst (binfo_type, args, complain, in_decl);
14635 bool dependent_p = binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink));
14637 if (dependent_p)
14639 tree name = OVL_NAME (fns);
14640 if (IDENTIFIER_CONV_OP_P (name))
14641 name = make_conv_op_name (optype);
14643 if (name == complete_dtor_identifier)
14644 /* Treat as-if non-dependent below. */
14645 dependent_p = false;
14647 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
14648 if (!baselink)
14650 if ((complain & tf_error)
14651 && constructor_name_p (name, qualifying_scope))
14652 error ("cannot call constructor %<%T::%D%> directly",
14653 qualifying_scope, name);
14654 return error_mark_node;
14657 if (BASELINK_P (baselink))
14658 fns = BASELINK_FUNCTIONS (baselink);
14660 else
14661 /* We're going to overwrite pieces below, make a duplicate. */
14662 baselink = copy_node (baselink);
14664 /* If lookup found a single function, mark it as used at this point.
14665 (If lookup found multiple functions the one selected later by
14666 overload resolution will be marked as used at that point.) */
14667 if (!template_id_p && !really_overloaded_fn (fns)
14668 && !mark_used (OVL_FIRST (fns), complain) && !(complain & tf_error))
14669 return error_mark_node;
14671 if (BASELINK_P (baselink))
14673 /* Add back the template arguments, if present. */
14674 if (template_id_p)
14675 BASELINK_FUNCTIONS (baselink)
14676 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
14678 /* Update the conversion operator type. */
14679 BASELINK_OPTYPE (baselink) = optype;
14682 if (!object_type)
14683 object_type = current_class_type;
14685 if (qualified_p || !dependent_p)
14687 baselink = adjust_result_of_qualified_name_lookup (baselink,
14688 qualifying_scope,
14689 object_type);
14690 if (!qualified_p)
14691 /* We need to call adjust_result_of_qualified_name_lookup in case the
14692 destructor names a base class, but we unset BASELINK_QUALIFIED_P
14693 so that we still get virtual function binding. */
14694 BASELINK_QUALIFIED_P (baselink) = false;
14697 return baselink;
14700 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
14701 true if the qualified-id will be a postfix-expression in-and-of
14702 itself; false if more of the postfix-expression follows the
14703 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
14704 of "&". */
14706 static tree
14707 tsubst_qualified_id (tree qualified_id, tree args,
14708 tsubst_flags_t complain, tree in_decl,
14709 bool done, bool address_p)
14711 tree expr;
14712 tree scope;
14713 tree name;
14714 bool is_template;
14715 tree template_args;
14716 location_t loc = UNKNOWN_LOCATION;
14718 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
14720 /* Figure out what name to look up. */
14721 name = TREE_OPERAND (qualified_id, 1);
14722 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14724 is_template = true;
14725 loc = EXPR_LOCATION (name);
14726 template_args = TREE_OPERAND (name, 1);
14727 if (template_args)
14728 template_args = tsubst_template_args (template_args, args,
14729 complain, in_decl);
14730 if (template_args == error_mark_node)
14731 return error_mark_node;
14732 name = TREE_OPERAND (name, 0);
14734 else
14736 is_template = false;
14737 template_args = NULL_TREE;
14740 /* Substitute into the qualifying scope. When there are no ARGS, we
14741 are just trying to simplify a non-dependent expression. In that
14742 case the qualifying scope may be dependent, and, in any case,
14743 substituting will not help. */
14744 scope = TREE_OPERAND (qualified_id, 0);
14745 if (args)
14747 scope = tsubst (scope, args, complain, in_decl);
14748 expr = tsubst_copy (name, args, complain, in_decl);
14750 else
14751 expr = name;
14753 if (dependent_scope_p (scope))
14755 if (is_template)
14756 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
14757 tree r = build_qualified_name (NULL_TREE, scope, expr,
14758 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
14759 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
14760 return r;
14763 if (!BASELINK_P (name) && !DECL_P (expr))
14765 if (TREE_CODE (expr) == BIT_NOT_EXPR)
14767 /* A BIT_NOT_EXPR is used to represent a destructor. */
14768 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
14770 error ("qualifying type %qT does not match destructor name ~%qT",
14771 scope, TREE_OPERAND (expr, 0));
14772 expr = error_mark_node;
14774 else
14775 expr = lookup_qualified_name (scope, complete_dtor_identifier,
14776 /*is_type_p=*/0, false);
14778 else
14779 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
14780 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
14781 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
14783 if (complain & tf_error)
14785 error ("dependent-name %qE is parsed as a non-type, but "
14786 "instantiation yields a type", qualified_id);
14787 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
14789 return error_mark_node;
14793 if (DECL_P (expr))
14795 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
14796 scope);
14797 /* Remember that there was a reference to this entity. */
14798 if (!mark_used (expr, complain) && !(complain & tf_error))
14799 return error_mark_node;
14802 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
14804 if (complain & tf_error)
14805 qualified_name_lookup_error (scope,
14806 TREE_OPERAND (qualified_id, 1),
14807 expr, input_location);
14808 return error_mark_node;
14811 if (is_template)
14813 if (variable_template_p (expr))
14814 expr = lookup_and_finish_template_variable (expr, template_args,
14815 complain);
14816 else
14817 expr = lookup_template_function (expr, template_args);
14820 if (expr == error_mark_node && complain & tf_error)
14821 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
14822 expr, input_location);
14823 else if (TYPE_P (scope))
14825 expr = (adjust_result_of_qualified_name_lookup
14826 (expr, scope, current_nonlambda_class_type ()));
14827 expr = (finish_qualified_id_expr
14828 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
14829 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
14830 /*template_arg_p=*/false, complain));
14833 /* Expressions do not generally have reference type. */
14834 if (TREE_CODE (expr) != SCOPE_REF
14835 /* However, if we're about to form a pointer-to-member, we just
14836 want the referenced member referenced. */
14837 && TREE_CODE (expr) != OFFSET_REF)
14838 expr = convert_from_reference (expr);
14840 if (REF_PARENTHESIZED_P (qualified_id))
14841 expr = force_paren_expr (expr);
14843 return expr;
14846 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14847 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14848 for tsubst. */
14850 static tree
14851 tsubst_init (tree init, tree decl, tree args,
14852 tsubst_flags_t complain, tree in_decl)
14854 if (!init)
14855 return NULL_TREE;
14857 init = tsubst_expr (init, args, complain, in_decl, false);
14859 if (!init && TREE_TYPE (decl) != error_mark_node)
14861 /* If we had an initializer but it
14862 instantiated to nothing,
14863 value-initialize the object. This will
14864 only occur when the initializer was a
14865 pack expansion where the parameter packs
14866 used in that expansion were of length
14867 zero. */
14868 init = build_value_init (TREE_TYPE (decl),
14869 complain);
14870 if (TREE_CODE (init) == AGGR_INIT_EXPR)
14871 init = get_target_expr_sfinae (init, complain);
14872 if (TREE_CODE (init) == TARGET_EXPR)
14873 TARGET_EXPR_DIRECT_INIT_P (init) = true;
14876 return init;
14879 /* Like tsubst, but deals with expressions. This function just replaces
14880 template parms; to finish processing the resultant expression, use
14881 tsubst_copy_and_build or tsubst_expr. */
14883 static tree
14884 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14886 enum tree_code code;
14887 tree r;
14889 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
14890 return t;
14892 code = TREE_CODE (t);
14894 switch (code)
14896 case PARM_DECL:
14897 r = retrieve_local_specialization (t);
14899 if (r == NULL_TREE)
14901 /* We get here for a use of 'this' in an NSDMI. */
14902 if (DECL_NAME (t) == this_identifier && current_class_ptr)
14903 return current_class_ptr;
14905 /* This can happen for a parameter name used later in a function
14906 declaration (such as in a late-specified return type). Just
14907 make a dummy decl, since it's only used for its type. */
14908 gcc_assert (cp_unevaluated_operand != 0);
14909 r = tsubst_decl (t, args, complain);
14910 /* Give it the template pattern as its context; its true context
14911 hasn't been instantiated yet and this is good enough for
14912 mangling. */
14913 DECL_CONTEXT (r) = DECL_CONTEXT (t);
14916 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14917 r = argument_pack_select_arg (r);
14918 if (!mark_used (r, complain) && !(complain & tf_error))
14919 return error_mark_node;
14920 return r;
14922 case CONST_DECL:
14924 tree enum_type;
14925 tree v;
14927 if (DECL_TEMPLATE_PARM_P (t))
14928 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
14929 /* There is no need to substitute into namespace-scope
14930 enumerators. */
14931 if (DECL_NAMESPACE_SCOPE_P (t))
14932 return t;
14933 /* If ARGS is NULL, then T is known to be non-dependent. */
14934 if (args == NULL_TREE)
14935 return scalar_constant_value (t);
14937 /* Unfortunately, we cannot just call lookup_name here.
14938 Consider:
14940 template <int I> int f() {
14941 enum E { a = I };
14942 struct S { void g() { E e = a; } };
14945 When we instantiate f<7>::S::g(), say, lookup_name is not
14946 clever enough to find f<7>::a. */
14947 enum_type
14948 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14949 /*entering_scope=*/0);
14951 for (v = TYPE_VALUES (enum_type);
14952 v != NULL_TREE;
14953 v = TREE_CHAIN (v))
14954 if (TREE_PURPOSE (v) == DECL_NAME (t))
14955 return TREE_VALUE (v);
14957 /* We didn't find the name. That should never happen; if
14958 name-lookup found it during preliminary parsing, we
14959 should find it again here during instantiation. */
14960 gcc_unreachable ();
14962 return t;
14964 case FIELD_DECL:
14965 if (DECL_CONTEXT (t))
14967 tree ctx;
14969 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14970 /*entering_scope=*/1);
14971 if (ctx != DECL_CONTEXT (t))
14973 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
14974 if (!r)
14976 if (complain & tf_error)
14977 error ("using invalid field %qD", t);
14978 return error_mark_node;
14980 return r;
14984 return t;
14986 case VAR_DECL:
14987 case FUNCTION_DECL:
14988 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
14989 r = tsubst (t, args, complain, in_decl);
14990 else if (local_variable_p (t)
14991 && uses_template_parms (DECL_CONTEXT (t)))
14993 r = retrieve_local_specialization (t);
14994 if (r == NULL_TREE)
14996 /* First try name lookup to find the instantiation. */
14997 r = lookup_name (DECL_NAME (t));
14998 if (r && !is_capture_proxy (r))
15000 /* Make sure that the one we found is the one we want. */
15001 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
15002 if (ctx != DECL_CONTEXT (r))
15003 r = NULL_TREE;
15006 if (r)
15007 /* OK */;
15008 else
15010 /* This can happen for a variable used in a
15011 late-specified return type of a local lambda, or for a
15012 local static or constant. Building a new VAR_DECL
15013 should be OK in all those cases. */
15014 r = tsubst_decl (t, args, complain);
15015 if (local_specializations)
15016 /* Avoid infinite recursion (79640). */
15017 register_local_specialization (r, t);
15018 if (decl_maybe_constant_var_p (r))
15020 /* We can't call cp_finish_decl, so handle the
15021 initializer by hand. */
15022 tree init = tsubst_init (DECL_INITIAL (t), r, args,
15023 complain, in_decl);
15024 if (!processing_template_decl)
15025 init = maybe_constant_init (init);
15026 if (processing_template_decl
15027 ? potential_constant_expression (init)
15028 : reduced_constant_expression_p (init))
15029 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
15030 = TREE_CONSTANT (r) = true;
15031 DECL_INITIAL (r) = init;
15032 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
15033 TREE_TYPE (r)
15034 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
15035 complain, adc_variable_type);
15037 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
15038 || decl_constant_var_p (r)
15039 || errorcount || sorrycount);
15040 if (!processing_template_decl
15041 && !TREE_STATIC (r))
15042 r = process_outer_var_ref (r, complain);
15044 /* Remember this for subsequent uses. */
15045 if (local_specializations)
15046 register_local_specialization (r, t);
15048 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
15049 r = argument_pack_select_arg (r);
15051 else
15052 r = t;
15053 if (!mark_used (r, complain))
15054 return error_mark_node;
15055 return r;
15057 case NAMESPACE_DECL:
15058 return t;
15060 case OVERLOAD:
15061 /* An OVERLOAD will always be a non-dependent overload set; an
15062 overload set from function scope will just be represented with an
15063 IDENTIFIER_NODE, and from class scope with a BASELINK. */
15064 gcc_assert (!uses_template_parms (t));
15065 /* We must have marked any lookups as persistent. */
15066 gcc_assert (!OVL_LOOKUP_P (t) || OVL_USED_P (t));
15067 return t;
15069 case BASELINK:
15070 return tsubst_baselink (t, current_nonlambda_class_type (),
15071 args, complain, in_decl);
15073 case TEMPLATE_DECL:
15074 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
15075 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
15076 args, complain, in_decl);
15077 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
15078 return tsubst (t, args, complain, in_decl);
15079 else if (DECL_CLASS_SCOPE_P (t)
15080 && uses_template_parms (DECL_CONTEXT (t)))
15082 /* Template template argument like the following example need
15083 special treatment:
15085 template <template <class> class TT> struct C {};
15086 template <class T> struct D {
15087 template <class U> struct E {};
15088 C<E> c; // #1
15090 D<int> d; // #2
15092 We are processing the template argument `E' in #1 for
15093 the template instantiation #2. Originally, `E' is a
15094 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
15095 have to substitute this with one having context `D<int>'. */
15097 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
15098 if (dependent_scope_p (context))
15100 /* When rewriting a constructor into a deduction guide, a
15101 non-dependent name can become dependent, so memtmpl<args>
15102 becomes context::template memtmpl<args>. */
15103 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15104 return build_qualified_name (type, context, DECL_NAME (t),
15105 /*template*/true);
15107 return lookup_field (context, DECL_NAME(t), 0, false);
15109 else
15110 /* Ordinary template template argument. */
15111 return t;
15113 case NON_LVALUE_EXPR:
15114 case VIEW_CONVERT_EXPR:
15116 /* Handle location wrappers by substituting the wrapped node
15117 first, *then* reusing the resulting type. Doing the type
15118 first ensures that we handle template parameters and
15119 parameter pack expansions. */
15120 gcc_assert (location_wrapper_p (t));
15121 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15122 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
15125 case CAST_EXPR:
15126 case REINTERPRET_CAST_EXPR:
15127 case CONST_CAST_EXPR:
15128 case STATIC_CAST_EXPR:
15129 case DYNAMIC_CAST_EXPR:
15130 case IMPLICIT_CONV_EXPR:
15131 case CONVERT_EXPR:
15132 case NOP_EXPR:
15134 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15135 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15136 return build1 (code, type, op0);
15139 case SIZEOF_EXPR:
15140 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
15141 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
15143 tree expanded, op = TREE_OPERAND (t, 0);
15144 int len = 0;
15146 if (SIZEOF_EXPR_TYPE_P (t))
15147 op = TREE_TYPE (op);
15149 ++cp_unevaluated_operand;
15150 ++c_inhibit_evaluation_warnings;
15151 /* We only want to compute the number of arguments. */
15152 if (PACK_EXPANSION_P (op))
15153 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
15154 else
15155 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
15156 args, complain, in_decl);
15157 --cp_unevaluated_operand;
15158 --c_inhibit_evaluation_warnings;
15160 if (TREE_CODE (expanded) == TREE_VEC)
15162 len = TREE_VEC_LENGTH (expanded);
15163 /* Set TREE_USED for the benefit of -Wunused. */
15164 for (int i = 0; i < len; i++)
15165 if (DECL_P (TREE_VEC_ELT (expanded, i)))
15166 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
15169 if (expanded == error_mark_node)
15170 return error_mark_node;
15171 else if (PACK_EXPANSION_P (expanded)
15172 || (TREE_CODE (expanded) == TREE_VEC
15173 && pack_expansion_args_count (expanded)))
15176 if (PACK_EXPANSION_P (expanded))
15177 /* OK. */;
15178 else if (TREE_VEC_LENGTH (expanded) == 1)
15179 expanded = TREE_VEC_ELT (expanded, 0);
15180 else
15181 expanded = make_argument_pack (expanded);
15183 if (TYPE_P (expanded))
15184 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
15185 complain & tf_error);
15186 else
15187 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
15188 complain & tf_error);
15190 else
15191 return build_int_cst (size_type_node, len);
15193 if (SIZEOF_EXPR_TYPE_P (t))
15195 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
15196 args, complain, in_decl);
15197 r = build1 (NOP_EXPR, r, error_mark_node);
15198 r = build1 (SIZEOF_EXPR,
15199 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
15200 SIZEOF_EXPR_TYPE_P (r) = 1;
15201 return r;
15203 /* Fall through */
15205 case INDIRECT_REF:
15206 case NEGATE_EXPR:
15207 case TRUTH_NOT_EXPR:
15208 case BIT_NOT_EXPR:
15209 case ADDR_EXPR:
15210 case UNARY_PLUS_EXPR: /* Unary + */
15211 case ALIGNOF_EXPR:
15212 case AT_ENCODE_EXPR:
15213 case ARROW_EXPR:
15214 case THROW_EXPR:
15215 case TYPEID_EXPR:
15216 case REALPART_EXPR:
15217 case IMAGPART_EXPR:
15218 case PAREN_EXPR:
15220 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15221 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15222 return build1 (code, type, op0);
15225 case COMPONENT_REF:
15227 tree object;
15228 tree name;
15230 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15231 name = TREE_OPERAND (t, 1);
15232 if (TREE_CODE (name) == BIT_NOT_EXPR)
15234 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15235 complain, in_decl);
15236 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15238 else if (TREE_CODE (name) == SCOPE_REF
15239 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
15241 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
15242 complain, in_decl);
15243 name = TREE_OPERAND (name, 1);
15244 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15245 complain, in_decl);
15246 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15247 name = build_qualified_name (/*type=*/NULL_TREE,
15248 base, name,
15249 /*template_p=*/false);
15251 else if (BASELINK_P (name))
15252 name = tsubst_baselink (name,
15253 non_reference (TREE_TYPE (object)),
15254 args, complain,
15255 in_decl);
15256 else
15257 name = tsubst_copy (name, args, complain, in_decl);
15258 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
15261 case PLUS_EXPR:
15262 case MINUS_EXPR:
15263 case MULT_EXPR:
15264 case TRUNC_DIV_EXPR:
15265 case CEIL_DIV_EXPR:
15266 case FLOOR_DIV_EXPR:
15267 case ROUND_DIV_EXPR:
15268 case EXACT_DIV_EXPR:
15269 case BIT_AND_EXPR:
15270 case BIT_IOR_EXPR:
15271 case BIT_XOR_EXPR:
15272 case TRUNC_MOD_EXPR:
15273 case FLOOR_MOD_EXPR:
15274 case TRUTH_ANDIF_EXPR:
15275 case TRUTH_ORIF_EXPR:
15276 case TRUTH_AND_EXPR:
15277 case TRUTH_OR_EXPR:
15278 case RSHIFT_EXPR:
15279 case LSHIFT_EXPR:
15280 case RROTATE_EXPR:
15281 case LROTATE_EXPR:
15282 case EQ_EXPR:
15283 case NE_EXPR:
15284 case MAX_EXPR:
15285 case MIN_EXPR:
15286 case LE_EXPR:
15287 case GE_EXPR:
15288 case LT_EXPR:
15289 case GT_EXPR:
15290 case COMPOUND_EXPR:
15291 case DOTSTAR_EXPR:
15292 case MEMBER_REF:
15293 case PREDECREMENT_EXPR:
15294 case PREINCREMENT_EXPR:
15295 case POSTDECREMENT_EXPR:
15296 case POSTINCREMENT_EXPR:
15298 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15299 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15300 return build_nt (code, op0, op1);
15303 case SCOPE_REF:
15305 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15306 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15307 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
15308 QUALIFIED_NAME_IS_TEMPLATE (t));
15311 case ARRAY_REF:
15313 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15314 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15315 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
15318 case CALL_EXPR:
15320 int n = VL_EXP_OPERAND_LENGTH (t);
15321 tree result = build_vl_exp (CALL_EXPR, n);
15322 int i;
15323 for (i = 0; i < n; i++)
15324 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
15325 complain, in_decl);
15326 return result;
15329 case COND_EXPR:
15330 case MODOP_EXPR:
15331 case PSEUDO_DTOR_EXPR:
15332 case VEC_PERM_EXPR:
15334 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15335 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15336 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15337 r = build_nt (code, op0, op1, op2);
15338 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
15339 return r;
15342 case NEW_EXPR:
15344 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15345 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15346 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15347 r = build_nt (code, op0, op1, op2);
15348 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
15349 return r;
15352 case DELETE_EXPR:
15354 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15355 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15356 r = build_nt (code, op0, op1);
15357 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
15358 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
15359 return r;
15362 case TEMPLATE_ID_EXPR:
15364 /* Substituted template arguments */
15365 tree fn = TREE_OPERAND (t, 0);
15366 tree targs = TREE_OPERAND (t, 1);
15368 fn = tsubst_copy (fn, args, complain, in_decl);
15369 if (targs)
15370 targs = tsubst_template_args (targs, args, complain, in_decl);
15372 return lookup_template_function (fn, targs);
15375 case TREE_LIST:
15377 tree purpose, value, chain;
15379 if (t == void_list_node)
15380 return t;
15382 purpose = TREE_PURPOSE (t);
15383 if (purpose)
15384 purpose = tsubst_copy (purpose, args, complain, in_decl);
15385 value = TREE_VALUE (t);
15386 if (value)
15387 value = tsubst_copy (value, args, complain, in_decl);
15388 chain = TREE_CHAIN (t);
15389 if (chain && chain != void_type_node)
15390 chain = tsubst_copy (chain, args, complain, in_decl);
15391 if (purpose == TREE_PURPOSE (t)
15392 && value == TREE_VALUE (t)
15393 && chain == TREE_CHAIN (t))
15394 return t;
15395 return tree_cons (purpose, value, chain);
15398 case RECORD_TYPE:
15399 case UNION_TYPE:
15400 case ENUMERAL_TYPE:
15401 case INTEGER_TYPE:
15402 case TEMPLATE_TYPE_PARM:
15403 case TEMPLATE_TEMPLATE_PARM:
15404 case BOUND_TEMPLATE_TEMPLATE_PARM:
15405 case TEMPLATE_PARM_INDEX:
15406 case POINTER_TYPE:
15407 case REFERENCE_TYPE:
15408 case OFFSET_TYPE:
15409 case FUNCTION_TYPE:
15410 case METHOD_TYPE:
15411 case ARRAY_TYPE:
15412 case TYPENAME_TYPE:
15413 case UNBOUND_CLASS_TEMPLATE:
15414 case TYPEOF_TYPE:
15415 case DECLTYPE_TYPE:
15416 case TYPE_DECL:
15417 return tsubst (t, args, complain, in_decl);
15419 case USING_DECL:
15420 t = DECL_NAME (t);
15421 /* Fall through. */
15422 case IDENTIFIER_NODE:
15423 if (IDENTIFIER_CONV_OP_P (t))
15425 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15426 return make_conv_op_name (new_type);
15428 else
15429 return t;
15431 case CONSTRUCTOR:
15432 /* This is handled by tsubst_copy_and_build. */
15433 gcc_unreachable ();
15435 case VA_ARG_EXPR:
15437 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15438 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15439 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
15442 case CLEANUP_POINT_EXPR:
15443 /* We shouldn't have built any of these during initial template
15444 generation. Instead, they should be built during instantiation
15445 in response to the saved STMT_IS_FULL_EXPR_P setting. */
15446 gcc_unreachable ();
15448 case OFFSET_REF:
15450 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15451 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15452 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15453 r = build2 (code, type, op0, op1);
15454 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
15455 if (!mark_used (TREE_OPERAND (r, 1), complain)
15456 && !(complain & tf_error))
15457 return error_mark_node;
15458 return r;
15461 case EXPR_PACK_EXPANSION:
15462 error ("invalid use of pack expansion expression");
15463 return error_mark_node;
15465 case NONTYPE_ARGUMENT_PACK:
15466 error ("use %<...%> to expand argument pack");
15467 return error_mark_node;
15469 case VOID_CST:
15470 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
15471 return t;
15473 case INTEGER_CST:
15474 case REAL_CST:
15475 case STRING_CST:
15476 case COMPLEX_CST:
15478 /* Instantiate any typedefs in the type. */
15479 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15480 r = fold_convert (type, t);
15481 gcc_assert (TREE_CODE (r) == code);
15482 return r;
15485 case PTRMEM_CST:
15486 /* These can sometimes show up in a partial instantiation, but never
15487 involve template parms. */
15488 gcc_assert (!uses_template_parms (t));
15489 return t;
15491 case UNARY_LEFT_FOLD_EXPR:
15492 return tsubst_unary_left_fold (t, args, complain, in_decl);
15493 case UNARY_RIGHT_FOLD_EXPR:
15494 return tsubst_unary_right_fold (t, args, complain, in_decl);
15495 case BINARY_LEFT_FOLD_EXPR:
15496 return tsubst_binary_left_fold (t, args, complain, in_decl);
15497 case BINARY_RIGHT_FOLD_EXPR:
15498 return tsubst_binary_right_fold (t, args, complain, in_decl);
15499 case PREDICT_EXPR:
15500 return t;
15502 case DEBUG_BEGIN_STMT:
15503 /* ??? There's no point in copying it for now, but maybe some
15504 day it will contain more information, such as a pointer back
15505 to the containing function, inlined copy or so. */
15506 return t;
15508 default:
15509 /* We shouldn't get here, but keep going if !flag_checking. */
15510 if (flag_checking)
15511 gcc_unreachable ();
15512 return t;
15516 /* Helper function for tsubst_omp_clauses, used for instantiation of
15517 OMP_CLAUSE_DECL of clauses. */
15519 static tree
15520 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
15521 tree in_decl)
15523 if (decl == NULL_TREE)
15524 return NULL_TREE;
15526 /* Handle an OpenMP array section represented as a TREE_LIST (or
15527 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15528 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15529 TREE_LIST. We can handle it exactly the same as an array section
15530 (purpose, value, and a chain), even though the nomenclature
15531 (low_bound, length, etc) is different. */
15532 if (TREE_CODE (decl) == TREE_LIST)
15534 tree low_bound
15535 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
15536 /*integral_constant_expression_p=*/false);
15537 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
15538 /*integral_constant_expression_p=*/false);
15539 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
15540 in_decl);
15541 if (TREE_PURPOSE (decl) == low_bound
15542 && TREE_VALUE (decl) == length
15543 && TREE_CHAIN (decl) == chain)
15544 return decl;
15545 tree ret = tree_cons (low_bound, length, chain);
15546 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
15547 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
15548 return ret;
15550 tree ret = tsubst_expr (decl, args, complain, in_decl,
15551 /*integral_constant_expression_p=*/false);
15552 /* Undo convert_from_reference tsubst_expr could have called. */
15553 if (decl
15554 && REFERENCE_REF_P (ret)
15555 && !REFERENCE_REF_P (decl))
15556 ret = TREE_OPERAND (ret, 0);
15557 return ret;
15560 /* Like tsubst_copy, but specifically for OpenMP clauses. */
15562 static tree
15563 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
15564 tree args, tsubst_flags_t complain, tree in_decl)
15566 tree new_clauses = NULL_TREE, nc, oc;
15567 tree linear_no_step = NULL_TREE;
15569 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
15571 nc = copy_node (oc);
15572 OMP_CLAUSE_CHAIN (nc) = new_clauses;
15573 new_clauses = nc;
15575 switch (OMP_CLAUSE_CODE (nc))
15577 case OMP_CLAUSE_LASTPRIVATE:
15578 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
15580 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
15581 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
15582 in_decl, /*integral_constant_expression_p=*/false);
15583 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
15584 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
15586 /* FALLTHRU */
15587 case OMP_CLAUSE_PRIVATE:
15588 case OMP_CLAUSE_SHARED:
15589 case OMP_CLAUSE_FIRSTPRIVATE:
15590 case OMP_CLAUSE_COPYIN:
15591 case OMP_CLAUSE_COPYPRIVATE:
15592 case OMP_CLAUSE_UNIFORM:
15593 case OMP_CLAUSE_DEPEND:
15594 case OMP_CLAUSE_FROM:
15595 case OMP_CLAUSE_TO:
15596 case OMP_CLAUSE_MAP:
15597 case OMP_CLAUSE_USE_DEVICE_PTR:
15598 case OMP_CLAUSE_IS_DEVICE_PTR:
15599 OMP_CLAUSE_DECL (nc)
15600 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15601 in_decl);
15602 break;
15603 case OMP_CLAUSE_TILE:
15604 case OMP_CLAUSE_IF:
15605 case OMP_CLAUSE_NUM_THREADS:
15606 case OMP_CLAUSE_SCHEDULE:
15607 case OMP_CLAUSE_COLLAPSE:
15608 case OMP_CLAUSE_FINAL:
15609 case OMP_CLAUSE_DEVICE:
15610 case OMP_CLAUSE_DIST_SCHEDULE:
15611 case OMP_CLAUSE_NUM_TEAMS:
15612 case OMP_CLAUSE_THREAD_LIMIT:
15613 case OMP_CLAUSE_SAFELEN:
15614 case OMP_CLAUSE_SIMDLEN:
15615 case OMP_CLAUSE_NUM_TASKS:
15616 case OMP_CLAUSE_GRAINSIZE:
15617 case OMP_CLAUSE_PRIORITY:
15618 case OMP_CLAUSE_ORDERED:
15619 case OMP_CLAUSE_HINT:
15620 case OMP_CLAUSE_NUM_GANGS:
15621 case OMP_CLAUSE_NUM_WORKERS:
15622 case OMP_CLAUSE_VECTOR_LENGTH:
15623 case OMP_CLAUSE_WORKER:
15624 case OMP_CLAUSE_VECTOR:
15625 case OMP_CLAUSE_ASYNC:
15626 case OMP_CLAUSE_WAIT:
15627 OMP_CLAUSE_OPERAND (nc, 0)
15628 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
15629 in_decl, /*integral_constant_expression_p=*/false);
15630 break;
15631 case OMP_CLAUSE_REDUCTION:
15632 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
15634 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
15635 if (TREE_CODE (placeholder) == SCOPE_REF)
15637 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
15638 complain, in_decl);
15639 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
15640 = build_qualified_name (NULL_TREE, scope,
15641 TREE_OPERAND (placeholder, 1),
15642 false);
15644 else
15645 gcc_assert (identifier_p (placeholder));
15647 OMP_CLAUSE_DECL (nc)
15648 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15649 in_decl);
15650 break;
15651 case OMP_CLAUSE_GANG:
15652 case OMP_CLAUSE_ALIGNED:
15653 OMP_CLAUSE_DECL (nc)
15654 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15655 in_decl);
15656 OMP_CLAUSE_OPERAND (nc, 1)
15657 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
15658 in_decl, /*integral_constant_expression_p=*/false);
15659 break;
15660 case OMP_CLAUSE_LINEAR:
15661 OMP_CLAUSE_DECL (nc)
15662 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15663 in_decl);
15664 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
15666 gcc_assert (!linear_no_step);
15667 linear_no_step = nc;
15669 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
15670 OMP_CLAUSE_LINEAR_STEP (nc)
15671 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
15672 complain, in_decl);
15673 else
15674 OMP_CLAUSE_LINEAR_STEP (nc)
15675 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
15676 in_decl,
15677 /*integral_constant_expression_p=*/false);
15678 break;
15679 case OMP_CLAUSE_NOWAIT:
15680 case OMP_CLAUSE_DEFAULT:
15681 case OMP_CLAUSE_UNTIED:
15682 case OMP_CLAUSE_MERGEABLE:
15683 case OMP_CLAUSE_INBRANCH:
15684 case OMP_CLAUSE_NOTINBRANCH:
15685 case OMP_CLAUSE_PROC_BIND:
15686 case OMP_CLAUSE_FOR:
15687 case OMP_CLAUSE_PARALLEL:
15688 case OMP_CLAUSE_SECTIONS:
15689 case OMP_CLAUSE_TASKGROUP:
15690 case OMP_CLAUSE_NOGROUP:
15691 case OMP_CLAUSE_THREADS:
15692 case OMP_CLAUSE_SIMD:
15693 case OMP_CLAUSE_DEFAULTMAP:
15694 case OMP_CLAUSE_INDEPENDENT:
15695 case OMP_CLAUSE_AUTO:
15696 case OMP_CLAUSE_SEQ:
15697 break;
15698 default:
15699 gcc_unreachable ();
15701 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
15702 switch (OMP_CLAUSE_CODE (nc))
15704 case OMP_CLAUSE_SHARED:
15705 case OMP_CLAUSE_PRIVATE:
15706 case OMP_CLAUSE_FIRSTPRIVATE:
15707 case OMP_CLAUSE_LASTPRIVATE:
15708 case OMP_CLAUSE_COPYPRIVATE:
15709 case OMP_CLAUSE_LINEAR:
15710 case OMP_CLAUSE_REDUCTION:
15711 case OMP_CLAUSE_USE_DEVICE_PTR:
15712 case OMP_CLAUSE_IS_DEVICE_PTR:
15713 /* tsubst_expr on SCOPE_REF results in returning
15714 finish_non_static_data_member result. Undo that here. */
15715 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
15716 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
15717 == IDENTIFIER_NODE))
15719 tree t = OMP_CLAUSE_DECL (nc);
15720 tree v = t;
15721 while (v)
15722 switch (TREE_CODE (v))
15724 case COMPONENT_REF:
15725 case MEM_REF:
15726 case INDIRECT_REF:
15727 CASE_CONVERT:
15728 case POINTER_PLUS_EXPR:
15729 v = TREE_OPERAND (v, 0);
15730 continue;
15731 case PARM_DECL:
15732 if (DECL_CONTEXT (v) == current_function_decl
15733 && DECL_ARTIFICIAL (v)
15734 && DECL_NAME (v) == this_identifier)
15735 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
15736 /* FALLTHRU */
15737 default:
15738 v = NULL_TREE;
15739 break;
15742 else if (VAR_P (OMP_CLAUSE_DECL (oc))
15743 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
15744 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
15745 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
15746 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
15748 tree decl = OMP_CLAUSE_DECL (nc);
15749 if (VAR_P (decl))
15751 retrofit_lang_decl (decl);
15752 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
15755 break;
15756 default:
15757 break;
15761 new_clauses = nreverse (new_clauses);
15762 if (ort != C_ORT_OMP_DECLARE_SIMD)
15764 new_clauses = finish_omp_clauses (new_clauses, ort);
15765 if (linear_no_step)
15766 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
15767 if (nc == linear_no_step)
15769 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
15770 break;
15773 return new_clauses;
15776 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
15778 static tree
15779 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
15780 tree in_decl)
15782 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
15784 tree purpose, value, chain;
15786 if (t == NULL)
15787 return t;
15789 if (TREE_CODE (t) != TREE_LIST)
15790 return tsubst_copy_and_build (t, args, complain, in_decl,
15791 /*function_p=*/false,
15792 /*integral_constant_expression_p=*/false);
15794 if (t == void_list_node)
15795 return t;
15797 purpose = TREE_PURPOSE (t);
15798 if (purpose)
15799 purpose = RECUR (purpose);
15800 value = TREE_VALUE (t);
15801 if (value)
15803 if (TREE_CODE (value) != LABEL_DECL)
15804 value = RECUR (value);
15805 else
15807 value = lookup_label (DECL_NAME (value));
15808 gcc_assert (TREE_CODE (value) == LABEL_DECL);
15809 TREE_USED (value) = 1;
15812 chain = TREE_CHAIN (t);
15813 if (chain && chain != void_type_node)
15814 chain = RECUR (chain);
15815 return tree_cons (purpose, value, chain);
15816 #undef RECUR
15819 /* Used to temporarily communicate the list of #pragma omp parallel
15820 clauses to #pragma omp for instantiation if they are combined
15821 together. */
15823 static tree *omp_parallel_combined_clauses;
15825 /* Substitute one OMP_FOR iterator. */
15827 static void
15828 tsubst_omp_for_iterator (tree t, int i, tree declv, tree orig_declv,
15829 tree initv, tree condv, tree incrv, tree *clauses,
15830 tree args, tsubst_flags_t complain, tree in_decl,
15831 bool integral_constant_expression_p)
15833 #define RECUR(NODE) \
15834 tsubst_expr ((NODE), args, complain, in_decl, \
15835 integral_constant_expression_p)
15836 tree decl, init, cond, incr;
15838 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
15839 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
15841 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
15843 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
15844 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
15847 decl = TREE_OPERAND (init, 0);
15848 init = TREE_OPERAND (init, 1);
15849 tree decl_expr = NULL_TREE;
15850 if (init && TREE_CODE (init) == DECL_EXPR)
15852 /* We need to jump through some hoops to handle declarations in the
15853 init-statement, since we might need to handle auto deduction,
15854 but we need to keep control of initialization. */
15855 decl_expr = init;
15856 init = DECL_INITIAL (DECL_EXPR_DECL (init));
15857 decl = tsubst_decl (decl, args, complain);
15859 else
15861 if (TREE_CODE (decl) == SCOPE_REF)
15863 decl = RECUR (decl);
15864 if (TREE_CODE (decl) == COMPONENT_REF)
15866 tree v = decl;
15867 while (v)
15868 switch (TREE_CODE (v))
15870 case COMPONENT_REF:
15871 case MEM_REF:
15872 case INDIRECT_REF:
15873 CASE_CONVERT:
15874 case POINTER_PLUS_EXPR:
15875 v = TREE_OPERAND (v, 0);
15876 continue;
15877 case PARM_DECL:
15878 if (DECL_CONTEXT (v) == current_function_decl
15879 && DECL_ARTIFICIAL (v)
15880 && DECL_NAME (v) == this_identifier)
15882 decl = TREE_OPERAND (decl, 1);
15883 decl = omp_privatize_field (decl, false);
15885 /* FALLTHRU */
15886 default:
15887 v = NULL_TREE;
15888 break;
15892 else
15893 decl = RECUR (decl);
15895 init = RECUR (init);
15897 tree auto_node = type_uses_auto (TREE_TYPE (decl));
15898 if (auto_node && init)
15899 TREE_TYPE (decl)
15900 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
15902 gcc_assert (!type_dependent_expression_p (decl));
15904 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15906 if (decl_expr)
15908 /* Declare the variable, but don't let that initialize it. */
15909 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
15910 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
15911 RECUR (decl_expr);
15912 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
15915 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
15916 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15917 if (TREE_CODE (incr) == MODIFY_EXPR)
15919 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15920 tree rhs = RECUR (TREE_OPERAND (incr, 1));
15921 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
15922 NOP_EXPR, rhs, complain);
15924 else
15925 incr = RECUR (incr);
15926 TREE_VEC_ELT (declv, i) = decl;
15927 TREE_VEC_ELT (initv, i) = init;
15928 TREE_VEC_ELT (condv, i) = cond;
15929 TREE_VEC_ELT (incrv, i) = incr;
15930 return;
15933 if (decl_expr)
15935 /* Declare and initialize the variable. */
15936 RECUR (decl_expr);
15937 init = NULL_TREE;
15939 else if (init)
15941 tree *pc;
15942 int j;
15943 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
15945 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
15947 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
15948 && OMP_CLAUSE_DECL (*pc) == decl)
15949 break;
15950 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
15951 && OMP_CLAUSE_DECL (*pc) == decl)
15953 if (j)
15954 break;
15955 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15956 tree c = *pc;
15957 *pc = OMP_CLAUSE_CHAIN (c);
15958 OMP_CLAUSE_CHAIN (c) = *clauses;
15959 *clauses = c;
15961 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
15962 && OMP_CLAUSE_DECL (*pc) == decl)
15964 error ("iteration variable %qD should not be firstprivate",
15965 decl);
15966 *pc = OMP_CLAUSE_CHAIN (*pc);
15968 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
15969 && OMP_CLAUSE_DECL (*pc) == decl)
15971 error ("iteration variable %qD should not be reduction",
15972 decl);
15973 *pc = OMP_CLAUSE_CHAIN (*pc);
15975 else
15976 pc = &OMP_CLAUSE_CHAIN (*pc);
15978 if (*pc)
15979 break;
15981 if (*pc == NULL_TREE)
15983 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
15984 OMP_CLAUSE_DECL (c) = decl;
15985 c = finish_omp_clauses (c, C_ORT_OMP);
15986 if (c)
15988 OMP_CLAUSE_CHAIN (c) = *clauses;
15989 *clauses = c;
15993 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
15994 if (COMPARISON_CLASS_P (cond))
15996 tree op0 = RECUR (TREE_OPERAND (cond, 0));
15997 tree op1 = RECUR (TREE_OPERAND (cond, 1));
15998 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
16000 else
16001 cond = RECUR (cond);
16002 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
16003 switch (TREE_CODE (incr))
16005 case PREINCREMENT_EXPR:
16006 case PREDECREMENT_EXPR:
16007 case POSTINCREMENT_EXPR:
16008 case POSTDECREMENT_EXPR:
16009 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
16010 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
16011 break;
16012 case MODIFY_EXPR:
16013 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
16014 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
16016 tree rhs = TREE_OPERAND (incr, 1);
16017 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16018 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
16019 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
16020 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16021 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
16022 rhs0, rhs1));
16024 else
16025 incr = RECUR (incr);
16026 break;
16027 case MODOP_EXPR:
16028 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
16029 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
16031 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16032 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16033 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
16034 TREE_TYPE (decl), lhs,
16035 RECUR (TREE_OPERAND (incr, 2))));
16037 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
16038 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
16039 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
16041 tree rhs = TREE_OPERAND (incr, 2);
16042 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16043 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
16044 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
16045 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16046 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
16047 rhs0, rhs1));
16049 else
16050 incr = RECUR (incr);
16051 break;
16052 default:
16053 incr = RECUR (incr);
16054 break;
16057 TREE_VEC_ELT (declv, i) = decl;
16058 TREE_VEC_ELT (initv, i) = init;
16059 TREE_VEC_ELT (condv, i) = cond;
16060 TREE_VEC_ELT (incrv, i) = incr;
16061 #undef RECUR
16064 /* Helper function of tsubst_expr, find OMP_TEAMS inside
16065 of OMP_TARGET's body. */
16067 static tree
16068 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
16070 *walk_subtrees = 0;
16071 switch (TREE_CODE (*tp))
16073 case OMP_TEAMS:
16074 return *tp;
16075 case BIND_EXPR:
16076 case STATEMENT_LIST:
16077 *walk_subtrees = 1;
16078 break;
16079 default:
16080 break;
16082 return NULL_TREE;
16085 /* Helper function for tsubst_expr. For decomposition declaration
16086 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
16087 also the corresponding decls representing the identifiers
16088 of the decomposition declaration. Return DECL if successful
16089 or error_mark_node otherwise, set *FIRST to the first decl
16090 in the list chained through DECL_CHAIN and *CNT to the number
16091 of such decls. */
16093 static tree
16094 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
16095 tsubst_flags_t complain, tree in_decl, tree *first,
16096 unsigned int *cnt)
16098 tree decl2, decl3, prev = decl;
16099 *cnt = 0;
16100 gcc_assert (DECL_NAME (decl) == NULL_TREE);
16101 for (decl2 = DECL_CHAIN (pattern_decl);
16102 decl2
16103 && VAR_P (decl2)
16104 && DECL_DECOMPOSITION_P (decl2)
16105 && DECL_NAME (decl2);
16106 decl2 = DECL_CHAIN (decl2))
16108 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
16110 gcc_assert (errorcount);
16111 return error_mark_node;
16113 (*cnt)++;
16114 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
16115 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
16116 tree v = DECL_VALUE_EXPR (decl2);
16117 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
16118 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
16119 decl3 = tsubst (decl2, args, complain, in_decl);
16120 SET_DECL_VALUE_EXPR (decl2, v);
16121 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
16122 if (VAR_P (decl3))
16123 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
16124 maybe_push_decl (decl3);
16125 if (error_operand_p (decl3))
16126 decl = error_mark_node;
16127 else if (decl != error_mark_node
16128 && DECL_CHAIN (decl3) != prev)
16130 gcc_assert (errorcount);
16131 decl = error_mark_node;
16133 else
16134 prev = decl3;
16136 *first = prev;
16137 return decl;
16140 /* Like tsubst_copy for expressions, etc. but also does semantic
16141 processing. */
16143 tree
16144 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
16145 bool integral_constant_expression_p)
16147 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
16148 #define RECUR(NODE) \
16149 tsubst_expr ((NODE), args, complain, in_decl, \
16150 integral_constant_expression_p)
16152 tree stmt, tmp;
16153 tree r;
16154 location_t loc;
16156 if (t == NULL_TREE || t == error_mark_node)
16157 return t;
16159 loc = input_location;
16160 if (EXPR_HAS_LOCATION (t))
16161 input_location = EXPR_LOCATION (t);
16162 if (STATEMENT_CODE_P (TREE_CODE (t)))
16163 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
16165 switch (TREE_CODE (t))
16167 case STATEMENT_LIST:
16169 tree_stmt_iterator i;
16170 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
16171 RECUR (tsi_stmt (i));
16172 break;
16175 case CTOR_INITIALIZER:
16176 finish_mem_initializers (tsubst_initializer_list
16177 (TREE_OPERAND (t, 0), args));
16178 break;
16180 case RETURN_EXPR:
16181 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
16182 break;
16184 case EXPR_STMT:
16185 tmp = RECUR (EXPR_STMT_EXPR (t));
16186 if (EXPR_STMT_STMT_EXPR_RESULT (t))
16187 finish_stmt_expr_expr (tmp, cur_stmt_expr);
16188 else
16189 finish_expr_stmt (tmp);
16190 break;
16192 case USING_STMT:
16193 finish_local_using_directive (USING_STMT_NAMESPACE (t),
16194 /*attribs=*/NULL_TREE);
16195 break;
16197 case DECL_EXPR:
16199 tree decl, pattern_decl;
16200 tree init;
16202 pattern_decl = decl = DECL_EXPR_DECL (t);
16203 if (TREE_CODE (decl) == LABEL_DECL)
16204 finish_label_decl (DECL_NAME (decl));
16205 else if (TREE_CODE (decl) == USING_DECL)
16207 tree scope = USING_DECL_SCOPE (decl);
16208 tree name = DECL_NAME (decl);
16210 scope = tsubst (scope, args, complain, in_decl);
16211 decl = lookup_qualified_name (scope, name,
16212 /*is_type_p=*/false,
16213 /*complain=*/false);
16214 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
16215 qualified_name_lookup_error (scope, name, decl, input_location);
16216 else
16217 finish_local_using_decl (decl, scope, name);
16219 else if (is_capture_proxy (decl)
16220 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
16222 /* We're in tsubst_lambda_expr, we've already inserted a new
16223 capture proxy, so look it up and register it. */
16224 tree inst;
16225 if (DECL_PACK_P (decl))
16227 inst = (retrieve_local_specialization
16228 (DECL_CAPTURED_VARIABLE (decl)));
16229 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK);
16231 else
16233 inst = lookup_name_real (DECL_NAME (decl), 0, 0,
16234 /*block_p=*/true, 0, LOOKUP_HIDDEN);
16235 gcc_assert (inst != decl && is_capture_proxy (inst));
16237 register_local_specialization (inst, decl);
16238 break;
16240 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
16241 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
16242 /* Don't copy the old closure; we'll create a new one in
16243 tsubst_lambda_expr. */
16244 break;
16245 else
16247 init = DECL_INITIAL (decl);
16248 decl = tsubst (decl, args, complain, in_decl);
16249 if (decl != error_mark_node)
16251 /* By marking the declaration as instantiated, we avoid
16252 trying to instantiate it. Since instantiate_decl can't
16253 handle local variables, and since we've already done
16254 all that needs to be done, that's the right thing to
16255 do. */
16256 if (VAR_P (decl))
16257 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16258 if (VAR_P (decl)
16259 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
16260 /* Anonymous aggregates are a special case. */
16261 finish_anon_union (decl);
16262 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
16264 DECL_CONTEXT (decl) = current_function_decl;
16265 if (DECL_NAME (decl) == this_identifier)
16267 tree lam = DECL_CONTEXT (current_function_decl);
16268 lam = CLASSTYPE_LAMBDA_EXPR (lam);
16269 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
16271 insert_capture_proxy (decl);
16273 else if (DECL_IMPLICIT_TYPEDEF_P (t))
16274 /* We already did a pushtag. */;
16275 else if (TREE_CODE (decl) == FUNCTION_DECL
16276 && DECL_OMP_DECLARE_REDUCTION_P (decl)
16277 && DECL_FUNCTION_SCOPE_P (pattern_decl))
16279 DECL_CONTEXT (decl) = NULL_TREE;
16280 pushdecl (decl);
16281 DECL_CONTEXT (decl) = current_function_decl;
16282 cp_check_omp_declare_reduction (decl);
16284 else
16286 int const_init = false;
16287 maybe_push_decl (decl);
16288 if (VAR_P (decl)
16289 && DECL_PRETTY_FUNCTION_P (decl))
16291 /* For __PRETTY_FUNCTION__ we have to adjust the
16292 initializer. */
16293 const char *const name
16294 = cxx_printable_name (current_function_decl, 2);
16295 init = cp_fname_init (name, &TREE_TYPE (decl));
16297 else
16298 init = tsubst_init (init, decl, args, complain, in_decl);
16300 if (VAR_P (decl))
16301 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
16302 (pattern_decl));
16303 if (VAR_P (decl)
16304 && DECL_DECOMPOSITION_P (decl)
16305 && TREE_TYPE (pattern_decl) != error_mark_node)
16307 unsigned int cnt;
16308 tree first;
16309 tree ndecl
16310 = tsubst_decomp_names (decl, pattern_decl, args,
16311 complain, in_decl, &first, &cnt);
16312 if (ndecl != error_mark_node)
16313 cp_maybe_mangle_decomp (ndecl, first, cnt);
16314 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
16315 if (ndecl != error_mark_node)
16316 cp_finish_decomp (ndecl, first, cnt);
16318 else
16319 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
16324 break;
16327 case FOR_STMT:
16328 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
16329 RECUR (FOR_INIT_STMT (t));
16330 finish_init_stmt (stmt);
16331 tmp = RECUR (FOR_COND (t));
16332 finish_for_cond (tmp, stmt, false, 0);
16333 tmp = RECUR (FOR_EXPR (t));
16334 finish_for_expr (tmp, stmt);
16336 bool prev = note_iteration_stmt_body_start ();
16337 RECUR (FOR_BODY (t));
16338 note_iteration_stmt_body_end (prev);
16340 finish_for_stmt (stmt);
16341 break;
16343 case RANGE_FOR_STMT:
16345 /* Construct another range_for, if this is not a final
16346 substitution (for inside inside a generic lambda of a
16347 template). Otherwise convert to a regular for. */
16348 tree decl, expr;
16349 stmt = (processing_template_decl
16350 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
16351 : begin_for_stmt (NULL_TREE, NULL_TREE));
16352 decl = RANGE_FOR_DECL (t);
16353 decl = tsubst (decl, args, complain, in_decl);
16354 maybe_push_decl (decl);
16355 expr = RECUR (RANGE_FOR_EXPR (t));
16357 tree decomp_first = NULL_TREE;
16358 unsigned decomp_cnt = 0;
16359 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
16360 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
16361 complain, in_decl,
16362 &decomp_first, &decomp_cnt);
16364 if (processing_template_decl)
16366 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
16367 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
16368 finish_range_for_decl (stmt, decl, expr);
16370 else
16372 unsigned short unroll = (RANGE_FOR_UNROLL (t)
16373 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
16374 stmt = cp_convert_range_for (stmt, decl, expr,
16375 decomp_first, decomp_cnt,
16376 RANGE_FOR_IVDEP (t), unroll);
16379 bool prev = note_iteration_stmt_body_start ();
16380 RECUR (RANGE_FOR_BODY (t));
16381 note_iteration_stmt_body_end (prev);
16382 finish_for_stmt (stmt);
16384 break;
16386 case WHILE_STMT:
16387 stmt = begin_while_stmt ();
16388 tmp = RECUR (WHILE_COND (t));
16389 finish_while_stmt_cond (tmp, stmt, false, 0);
16391 bool prev = note_iteration_stmt_body_start ();
16392 RECUR (WHILE_BODY (t));
16393 note_iteration_stmt_body_end (prev);
16395 finish_while_stmt (stmt);
16396 break;
16398 case DO_STMT:
16399 stmt = begin_do_stmt ();
16401 bool prev = note_iteration_stmt_body_start ();
16402 RECUR (DO_BODY (t));
16403 note_iteration_stmt_body_end (prev);
16405 finish_do_body (stmt);
16406 tmp = RECUR (DO_COND (t));
16407 finish_do_stmt (tmp, stmt, false, 0);
16408 break;
16410 case IF_STMT:
16411 stmt = begin_if_stmt ();
16412 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
16413 tmp = RECUR (IF_COND (t));
16414 tmp = finish_if_stmt_cond (tmp, stmt);
16415 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
16416 /* Don't instantiate the THEN_CLAUSE. */;
16417 else
16419 bool inhibit = integer_zerop (fold_non_dependent_expr (tmp));
16420 if (inhibit)
16421 ++c_inhibit_evaluation_warnings;
16422 RECUR (THEN_CLAUSE (t));
16423 if (inhibit)
16424 --c_inhibit_evaluation_warnings;
16426 finish_then_clause (stmt);
16428 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
16429 /* Don't instantiate the ELSE_CLAUSE. */;
16430 else if (ELSE_CLAUSE (t))
16432 bool inhibit = integer_nonzerop (fold_non_dependent_expr (tmp));
16433 begin_else_clause (stmt);
16434 if (inhibit)
16435 ++c_inhibit_evaluation_warnings;
16436 RECUR (ELSE_CLAUSE (t));
16437 if (inhibit)
16438 --c_inhibit_evaluation_warnings;
16439 finish_else_clause (stmt);
16442 finish_if_stmt (stmt);
16443 break;
16445 case BIND_EXPR:
16446 if (BIND_EXPR_BODY_BLOCK (t))
16447 stmt = begin_function_body ();
16448 else
16449 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
16450 ? BCS_TRY_BLOCK : 0);
16452 RECUR (BIND_EXPR_BODY (t));
16454 if (BIND_EXPR_BODY_BLOCK (t))
16455 finish_function_body (stmt);
16456 else
16457 finish_compound_stmt (stmt);
16458 break;
16460 case BREAK_STMT:
16461 finish_break_stmt ();
16462 break;
16464 case CONTINUE_STMT:
16465 finish_continue_stmt ();
16466 break;
16468 case SWITCH_STMT:
16469 stmt = begin_switch_stmt ();
16470 tmp = RECUR (SWITCH_STMT_COND (t));
16471 finish_switch_cond (tmp, stmt);
16472 RECUR (SWITCH_STMT_BODY (t));
16473 finish_switch_stmt (stmt);
16474 break;
16476 case CASE_LABEL_EXPR:
16478 tree low = RECUR (CASE_LOW (t));
16479 tree high = RECUR (CASE_HIGH (t));
16480 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
16481 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
16482 FALLTHROUGH_LABEL_P (CASE_LABEL (l))
16483 = FALLTHROUGH_LABEL_P (CASE_LABEL (t));
16485 break;
16487 case LABEL_EXPR:
16489 tree decl = LABEL_EXPR_LABEL (t);
16490 tree label;
16492 label = finish_label_stmt (DECL_NAME (decl));
16493 if (TREE_CODE (label) == LABEL_DECL)
16494 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
16495 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
16496 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
16498 break;
16500 case GOTO_EXPR:
16501 tmp = GOTO_DESTINATION (t);
16502 if (TREE_CODE (tmp) != LABEL_DECL)
16503 /* Computed goto's must be tsubst'd into. On the other hand,
16504 non-computed gotos must not be; the identifier in question
16505 will have no binding. */
16506 tmp = RECUR (tmp);
16507 else
16508 tmp = DECL_NAME (tmp);
16509 finish_goto_stmt (tmp);
16510 break;
16512 case ASM_EXPR:
16514 tree string = RECUR (ASM_STRING (t));
16515 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
16516 complain, in_decl);
16517 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
16518 complain, in_decl);
16519 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
16520 complain, in_decl);
16521 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
16522 complain, in_decl);
16523 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
16524 clobbers, labels);
16525 tree asm_expr = tmp;
16526 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
16527 asm_expr = TREE_OPERAND (asm_expr, 0);
16528 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
16530 break;
16532 case TRY_BLOCK:
16533 if (CLEANUP_P (t))
16535 stmt = begin_try_block ();
16536 RECUR (TRY_STMTS (t));
16537 finish_cleanup_try_block (stmt);
16538 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
16540 else
16542 tree compound_stmt = NULL_TREE;
16544 if (FN_TRY_BLOCK_P (t))
16545 stmt = begin_function_try_block (&compound_stmt);
16546 else
16547 stmt = begin_try_block ();
16549 RECUR (TRY_STMTS (t));
16551 if (FN_TRY_BLOCK_P (t))
16552 finish_function_try_block (stmt);
16553 else
16554 finish_try_block (stmt);
16556 RECUR (TRY_HANDLERS (t));
16557 if (FN_TRY_BLOCK_P (t))
16558 finish_function_handler_sequence (stmt, compound_stmt);
16559 else
16560 finish_handler_sequence (stmt);
16562 break;
16564 case HANDLER:
16566 tree decl = HANDLER_PARMS (t);
16568 if (decl)
16570 decl = tsubst (decl, args, complain, in_decl);
16571 /* Prevent instantiate_decl from trying to instantiate
16572 this variable. We've already done all that needs to be
16573 done. */
16574 if (decl != error_mark_node)
16575 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16577 stmt = begin_handler ();
16578 finish_handler_parms (decl, stmt);
16579 RECUR (HANDLER_BODY (t));
16580 finish_handler (stmt);
16582 break;
16584 case TAG_DEFN:
16585 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
16586 if (CLASS_TYPE_P (tmp))
16588 /* Local classes are not independent templates; they are
16589 instantiated along with their containing function. And this
16590 way we don't have to deal with pushing out of one local class
16591 to instantiate a member of another local class. */
16592 /* Closures are handled by the LAMBDA_EXPR. */
16593 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
16594 complete_type (tmp);
16595 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
16596 if ((VAR_P (fld)
16597 || (TREE_CODE (fld) == FUNCTION_DECL
16598 && !DECL_ARTIFICIAL (fld)))
16599 && DECL_TEMPLATE_INSTANTIATION (fld))
16600 instantiate_decl (fld, /*defer_ok=*/false,
16601 /*expl_inst_class=*/false);
16603 break;
16605 case STATIC_ASSERT:
16607 tree condition;
16609 ++c_inhibit_evaluation_warnings;
16610 condition =
16611 tsubst_expr (STATIC_ASSERT_CONDITION (t),
16612 args,
16613 complain, in_decl,
16614 /*integral_constant_expression_p=*/true);
16615 --c_inhibit_evaluation_warnings;
16617 finish_static_assert (condition,
16618 STATIC_ASSERT_MESSAGE (t),
16619 STATIC_ASSERT_SOURCE_LOCATION (t),
16620 /*member_p=*/false);
16622 break;
16624 case OACC_KERNELS:
16625 case OACC_PARALLEL:
16626 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
16627 in_decl);
16628 stmt = begin_omp_parallel ();
16629 RECUR (OMP_BODY (t));
16630 finish_omp_construct (TREE_CODE (t), stmt, tmp);
16631 break;
16633 case OMP_PARALLEL:
16634 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
16635 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
16636 complain, in_decl);
16637 if (OMP_PARALLEL_COMBINED (t))
16638 omp_parallel_combined_clauses = &tmp;
16639 stmt = begin_omp_parallel ();
16640 RECUR (OMP_PARALLEL_BODY (t));
16641 gcc_assert (omp_parallel_combined_clauses == NULL);
16642 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
16643 = OMP_PARALLEL_COMBINED (t);
16644 pop_omp_privatization_clauses (r);
16645 break;
16647 case OMP_TASK:
16648 r = push_omp_privatization_clauses (false);
16649 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
16650 complain, in_decl);
16651 stmt = begin_omp_task ();
16652 RECUR (OMP_TASK_BODY (t));
16653 finish_omp_task (tmp, stmt);
16654 pop_omp_privatization_clauses (r);
16655 break;
16657 case OMP_FOR:
16658 case OMP_SIMD:
16659 case OMP_DISTRIBUTE:
16660 case OMP_TASKLOOP:
16661 case OACC_LOOP:
16663 tree clauses, body, pre_body;
16664 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
16665 tree orig_declv = NULL_TREE;
16666 tree incrv = NULL_TREE;
16667 enum c_omp_region_type ort = C_ORT_OMP;
16668 int i;
16670 if (TREE_CODE (t) == OACC_LOOP)
16671 ort = C_ORT_ACC;
16673 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
16674 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
16675 in_decl);
16676 if (OMP_FOR_INIT (t) != NULL_TREE)
16678 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16679 if (OMP_FOR_ORIG_DECLS (t))
16680 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16681 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16682 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16683 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16686 stmt = begin_omp_structured_block ();
16688 pre_body = push_stmt_list ();
16689 RECUR (OMP_FOR_PRE_BODY (t));
16690 pre_body = pop_stmt_list (pre_body);
16692 if (OMP_FOR_INIT (t) != NULL_TREE)
16693 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
16694 tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, condv,
16695 incrv, &clauses, args, complain, in_decl,
16696 integral_constant_expression_p);
16697 omp_parallel_combined_clauses = NULL;
16699 body = push_stmt_list ();
16700 RECUR (OMP_FOR_BODY (t));
16701 body = pop_stmt_list (body);
16703 if (OMP_FOR_INIT (t) != NULL_TREE)
16704 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
16705 orig_declv, initv, condv, incrv, body, pre_body,
16706 NULL, clauses);
16707 else
16709 t = make_node (TREE_CODE (t));
16710 TREE_TYPE (t) = void_type_node;
16711 OMP_FOR_BODY (t) = body;
16712 OMP_FOR_PRE_BODY (t) = pre_body;
16713 OMP_FOR_CLAUSES (t) = clauses;
16714 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
16715 add_stmt (t);
16718 add_stmt (finish_omp_structured_block (stmt));
16719 pop_omp_privatization_clauses (r);
16721 break;
16723 case OMP_SECTIONS:
16724 omp_parallel_combined_clauses = NULL;
16725 /* FALLTHRU */
16726 case OMP_SINGLE:
16727 case OMP_TEAMS:
16728 case OMP_CRITICAL:
16729 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
16730 && OMP_TEAMS_COMBINED (t));
16731 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
16732 in_decl);
16733 stmt = push_stmt_list ();
16734 RECUR (OMP_BODY (t));
16735 stmt = pop_stmt_list (stmt);
16737 t = copy_node (t);
16738 OMP_BODY (t) = stmt;
16739 OMP_CLAUSES (t) = tmp;
16740 add_stmt (t);
16741 pop_omp_privatization_clauses (r);
16742 break;
16744 case OACC_DATA:
16745 case OMP_TARGET_DATA:
16746 case OMP_TARGET:
16747 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
16748 ? C_ORT_ACC : C_ORT_OMP, args, complain,
16749 in_decl);
16750 keep_next_level (true);
16751 stmt = begin_omp_structured_block ();
16753 RECUR (OMP_BODY (t));
16754 stmt = finish_omp_structured_block (stmt);
16756 t = copy_node (t);
16757 OMP_BODY (t) = stmt;
16758 OMP_CLAUSES (t) = tmp;
16759 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
16761 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
16762 if (teams)
16764 /* For combined target teams, ensure the num_teams and
16765 thread_limit clause expressions are evaluated on the host,
16766 before entering the target construct. */
16767 tree c;
16768 for (c = OMP_TEAMS_CLAUSES (teams);
16769 c; c = OMP_CLAUSE_CHAIN (c))
16770 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
16771 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
16772 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
16774 tree expr = OMP_CLAUSE_OPERAND (c, 0);
16775 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
16776 if (expr == error_mark_node)
16777 continue;
16778 tmp = TARGET_EXPR_SLOT (expr);
16779 add_stmt (expr);
16780 OMP_CLAUSE_OPERAND (c, 0) = expr;
16781 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16782 OMP_CLAUSE_FIRSTPRIVATE);
16783 OMP_CLAUSE_DECL (tc) = tmp;
16784 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
16785 OMP_TARGET_CLAUSES (t) = tc;
16789 add_stmt (t);
16790 break;
16792 case OACC_DECLARE:
16793 t = copy_node (t);
16794 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
16795 complain, in_decl);
16796 OACC_DECLARE_CLAUSES (t) = tmp;
16797 add_stmt (t);
16798 break;
16800 case OMP_TARGET_UPDATE:
16801 case OMP_TARGET_ENTER_DATA:
16802 case OMP_TARGET_EXIT_DATA:
16803 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
16804 complain, in_decl);
16805 t = copy_node (t);
16806 OMP_STANDALONE_CLAUSES (t) = tmp;
16807 add_stmt (t);
16808 break;
16810 case OACC_ENTER_DATA:
16811 case OACC_EXIT_DATA:
16812 case OACC_UPDATE:
16813 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
16814 complain, in_decl);
16815 t = copy_node (t);
16816 OMP_STANDALONE_CLAUSES (t) = tmp;
16817 add_stmt (t);
16818 break;
16820 case OMP_ORDERED:
16821 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
16822 complain, in_decl);
16823 stmt = push_stmt_list ();
16824 RECUR (OMP_BODY (t));
16825 stmt = pop_stmt_list (stmt);
16827 t = copy_node (t);
16828 OMP_BODY (t) = stmt;
16829 OMP_ORDERED_CLAUSES (t) = tmp;
16830 add_stmt (t);
16831 break;
16833 case OMP_SECTION:
16834 case OMP_MASTER:
16835 case OMP_TASKGROUP:
16836 stmt = push_stmt_list ();
16837 RECUR (OMP_BODY (t));
16838 stmt = pop_stmt_list (stmt);
16840 t = copy_node (t);
16841 OMP_BODY (t) = stmt;
16842 add_stmt (t);
16843 break;
16845 case OMP_ATOMIC:
16846 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
16847 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
16849 tree op1 = TREE_OPERAND (t, 1);
16850 tree rhs1 = NULL_TREE;
16851 tree lhs, rhs;
16852 if (TREE_CODE (op1) == COMPOUND_EXPR)
16854 rhs1 = RECUR (TREE_OPERAND (op1, 0));
16855 op1 = TREE_OPERAND (op1, 1);
16857 lhs = RECUR (TREE_OPERAND (op1, 0));
16858 rhs = RECUR (TREE_OPERAND (op1, 1));
16859 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
16860 NULL_TREE, NULL_TREE, rhs1,
16861 OMP_ATOMIC_SEQ_CST (t));
16863 else
16865 tree op1 = TREE_OPERAND (t, 1);
16866 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
16867 tree rhs1 = NULL_TREE;
16868 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
16869 enum tree_code opcode = NOP_EXPR;
16870 if (code == OMP_ATOMIC_READ)
16872 v = RECUR (TREE_OPERAND (op1, 0));
16873 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16875 else if (code == OMP_ATOMIC_CAPTURE_OLD
16876 || code == OMP_ATOMIC_CAPTURE_NEW)
16878 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
16879 v = RECUR (TREE_OPERAND (op1, 0));
16880 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16881 if (TREE_CODE (op11) == COMPOUND_EXPR)
16883 rhs1 = RECUR (TREE_OPERAND (op11, 0));
16884 op11 = TREE_OPERAND (op11, 1);
16886 lhs = RECUR (TREE_OPERAND (op11, 0));
16887 rhs = RECUR (TREE_OPERAND (op11, 1));
16888 opcode = TREE_CODE (op11);
16889 if (opcode == MODIFY_EXPR)
16890 opcode = NOP_EXPR;
16892 else
16894 code = OMP_ATOMIC;
16895 lhs = RECUR (TREE_OPERAND (op1, 0));
16896 rhs = RECUR (TREE_OPERAND (op1, 1));
16898 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
16899 OMP_ATOMIC_SEQ_CST (t));
16901 break;
16903 case TRANSACTION_EXPR:
16905 int flags = 0;
16906 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
16907 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
16909 if (TRANSACTION_EXPR_IS_STMT (t))
16911 tree body = TRANSACTION_EXPR_BODY (t);
16912 tree noex = NULL_TREE;
16913 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
16915 noex = MUST_NOT_THROW_COND (body);
16916 if (noex == NULL_TREE)
16917 noex = boolean_true_node;
16918 body = TREE_OPERAND (body, 0);
16920 stmt = begin_transaction_stmt (input_location, NULL, flags);
16921 RECUR (body);
16922 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
16924 else
16926 stmt = build_transaction_expr (EXPR_LOCATION (t),
16927 RECUR (TRANSACTION_EXPR_BODY (t)),
16928 flags, NULL_TREE);
16929 RETURN (stmt);
16932 break;
16934 case MUST_NOT_THROW_EXPR:
16936 tree op0 = RECUR (TREE_OPERAND (t, 0));
16937 tree cond = RECUR (MUST_NOT_THROW_COND (t));
16938 RETURN (build_must_not_throw_expr (op0, cond));
16941 case EXPR_PACK_EXPANSION:
16942 error ("invalid use of pack expansion expression");
16943 RETURN (error_mark_node);
16945 case NONTYPE_ARGUMENT_PACK:
16946 error ("use %<...%> to expand argument pack");
16947 RETURN (error_mark_node);
16949 case COMPOUND_EXPR:
16950 tmp = RECUR (TREE_OPERAND (t, 0));
16951 if (tmp == NULL_TREE)
16952 /* If the first operand was a statement, we're done with it. */
16953 RETURN (RECUR (TREE_OPERAND (t, 1)));
16954 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
16955 RECUR (TREE_OPERAND (t, 1)),
16956 complain));
16958 case ANNOTATE_EXPR:
16959 tmp = RECUR (TREE_OPERAND (t, 0));
16960 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
16961 TREE_TYPE (tmp), tmp,
16962 RECUR (TREE_OPERAND (t, 1)),
16963 RECUR (TREE_OPERAND (t, 2))));
16965 default:
16966 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
16968 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
16969 /*function_p=*/false,
16970 integral_constant_expression_p));
16973 RETURN (NULL_TREE);
16974 out:
16975 input_location = loc;
16976 return r;
16977 #undef RECUR
16978 #undef RETURN
16981 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
16982 function. For description of the body see comment above
16983 cp_parser_omp_declare_reduction_exprs. */
16985 static void
16986 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16988 if (t == NULL_TREE || t == error_mark_node)
16989 return;
16991 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
16993 tree_stmt_iterator tsi;
16994 int i;
16995 tree stmts[7];
16996 memset (stmts, 0, sizeof stmts);
16997 for (i = 0, tsi = tsi_start (t);
16998 i < 7 && !tsi_end_p (tsi);
16999 i++, tsi_next (&tsi))
17000 stmts[i] = tsi_stmt (tsi);
17001 gcc_assert (tsi_end_p (tsi));
17003 if (i >= 3)
17005 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
17006 && TREE_CODE (stmts[1]) == DECL_EXPR);
17007 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
17008 args, complain, in_decl);
17009 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
17010 args, complain, in_decl);
17011 DECL_CONTEXT (omp_out) = current_function_decl;
17012 DECL_CONTEXT (omp_in) = current_function_decl;
17013 keep_next_level (true);
17014 tree block = begin_omp_structured_block ();
17015 tsubst_expr (stmts[2], args, complain, in_decl, false);
17016 block = finish_omp_structured_block (block);
17017 block = maybe_cleanup_point_expr_void (block);
17018 add_decl_expr (omp_out);
17019 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
17020 TREE_NO_WARNING (omp_out) = 1;
17021 add_decl_expr (omp_in);
17022 finish_expr_stmt (block);
17024 if (i >= 6)
17026 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
17027 && TREE_CODE (stmts[4]) == DECL_EXPR);
17028 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
17029 args, complain, in_decl);
17030 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
17031 args, complain, in_decl);
17032 DECL_CONTEXT (omp_priv) = current_function_decl;
17033 DECL_CONTEXT (omp_orig) = current_function_decl;
17034 keep_next_level (true);
17035 tree block = begin_omp_structured_block ();
17036 tsubst_expr (stmts[5], args, complain, in_decl, false);
17037 block = finish_omp_structured_block (block);
17038 block = maybe_cleanup_point_expr_void (block);
17039 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
17040 add_decl_expr (omp_priv);
17041 add_decl_expr (omp_orig);
17042 finish_expr_stmt (block);
17043 if (i == 7)
17044 add_decl_expr (omp_orig);
17048 /* T is a postfix-expression that is not being used in a function
17049 call. Return the substituted version of T. */
17051 static tree
17052 tsubst_non_call_postfix_expression (tree t, tree args,
17053 tsubst_flags_t complain,
17054 tree in_decl)
17056 if (TREE_CODE (t) == SCOPE_REF)
17057 t = tsubst_qualified_id (t, args, complain, in_decl,
17058 /*done=*/false, /*address_p=*/false);
17059 else
17060 t = tsubst_copy_and_build (t, args, complain, in_decl,
17061 /*function_p=*/false,
17062 /*integral_constant_expression_p=*/false);
17064 return t;
17067 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
17068 instantiation context. Instantiating a pack expansion containing a lambda
17069 might result in multiple lambdas all based on the same lambda in the
17070 template. */
17072 tree
17073 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
17075 tree oldfn = lambda_function (t);
17076 in_decl = oldfn;
17078 tree r = build_lambda_expr ();
17080 LAMBDA_EXPR_LOCATION (r)
17081 = LAMBDA_EXPR_LOCATION (t);
17082 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
17083 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
17084 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
17086 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
17087 LAMBDA_EXPR_EXTRA_SCOPE (r) = NULL_TREE;
17088 else
17089 record_lambda_scope (r);
17091 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
17092 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
17094 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
17095 cap = TREE_CHAIN (cap))
17097 tree field = TREE_PURPOSE (cap);
17098 if (PACK_EXPANSION_P (field))
17099 field = PACK_EXPANSION_PATTERN (field);
17100 field = tsubst_decl (field, args, complain);
17102 if (field == error_mark_node)
17103 return error_mark_node;
17105 tree init = TREE_VALUE (cap);
17106 if (PACK_EXPANSION_P (init))
17107 init = tsubst_pack_expansion (init, args, complain, in_decl);
17108 else
17109 init = tsubst_copy_and_build (init, args, complain, in_decl,
17110 /*fn*/false, /*constexpr*/false);
17112 if (TREE_CODE (field) == TREE_VEC)
17114 int len = TREE_VEC_LENGTH (field);
17115 gcc_assert (TREE_CODE (init) == TREE_VEC
17116 && TREE_VEC_LENGTH (init) == len);
17117 for (int i = 0; i < len; ++i)
17118 LAMBDA_EXPR_CAPTURE_LIST (r)
17119 = tree_cons (TREE_VEC_ELT (field, i),
17120 TREE_VEC_ELT (init, i),
17121 LAMBDA_EXPR_CAPTURE_LIST (r));
17123 else
17125 LAMBDA_EXPR_CAPTURE_LIST (r)
17126 = tree_cons (field, init, LAMBDA_EXPR_CAPTURE_LIST (r));
17128 if (id_equal (DECL_NAME (field), "__this"))
17129 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
17133 tree type = begin_lambda_type (r);
17135 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
17136 determine_visibility (TYPE_NAME (type));
17138 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
17140 tree oldtmpl = (generic_lambda_fn_p (oldfn)
17141 ? DECL_TI_TEMPLATE (oldfn)
17142 : NULL_TREE);
17144 tree fntype = static_fn_type (oldfn);
17145 if (oldtmpl)
17146 ++processing_template_decl;
17147 fntype = tsubst (fntype, args, complain, in_decl);
17148 if (oldtmpl)
17149 --processing_template_decl;
17151 if (fntype == error_mark_node)
17152 r = error_mark_node;
17153 else
17155 /* Fix the type of 'this'. */
17156 fntype = build_memfn_type (fntype, type,
17157 type_memfn_quals (fntype),
17158 type_memfn_rqual (fntype));
17159 tree fn, tmpl;
17160 if (oldtmpl)
17162 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
17163 fn = DECL_TEMPLATE_RESULT (tmpl);
17164 finish_member_declaration (tmpl);
17166 else
17168 tmpl = NULL_TREE;
17169 fn = tsubst_function_decl (oldfn, args, complain, fntype);
17170 finish_member_declaration (fn);
17173 /* Let finish_function set this. */
17174 DECL_DECLARED_CONSTEXPR_P (fn) = false;
17176 bool nested = cfun;
17177 if (nested)
17178 push_function_context ();
17179 else
17180 /* Still increment function_depth so that we don't GC in the
17181 middle of an expression. */
17182 ++function_depth;
17184 local_specialization_stack s (lss_copy);
17186 tree body = start_lambda_function (fn, r);
17188 register_parameter_specializations (oldfn, fn);
17190 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
17191 /*constexpr*/false);
17193 finish_lambda_function (body);
17195 if (nested)
17196 pop_function_context ();
17197 else
17198 --function_depth;
17200 /* The capture list was built up in reverse order; fix that now. */
17201 LAMBDA_EXPR_CAPTURE_LIST (r)
17202 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
17204 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
17206 maybe_add_lambda_conv_op (type);
17209 finish_struct (type, /*attr*/NULL_TREE);
17211 insert_pending_capture_proxies ();
17213 return r;
17216 /* Like tsubst but deals with expressions and performs semantic
17217 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
17219 tree
17220 tsubst_copy_and_build (tree t,
17221 tree args,
17222 tsubst_flags_t complain,
17223 tree in_decl,
17224 bool function_p,
17225 bool integral_constant_expression_p)
17227 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
17228 #define RECUR(NODE) \
17229 tsubst_copy_and_build (NODE, args, complain, in_decl, \
17230 /*function_p=*/false, \
17231 integral_constant_expression_p)
17233 tree retval, op1;
17234 location_t loc;
17236 if (t == NULL_TREE || t == error_mark_node)
17237 return t;
17239 loc = input_location;
17240 if (EXPR_HAS_LOCATION (t))
17241 input_location = EXPR_LOCATION (t);
17243 /* N3276 decltype magic only applies to calls at the top level or on the
17244 right side of a comma. */
17245 tsubst_flags_t decltype_flag = (complain & tf_decltype);
17246 complain &= ~tf_decltype;
17248 switch (TREE_CODE (t))
17250 case USING_DECL:
17251 t = DECL_NAME (t);
17252 /* Fall through. */
17253 case IDENTIFIER_NODE:
17255 tree decl;
17256 cp_id_kind idk;
17257 bool non_integral_constant_expression_p;
17258 const char *error_msg;
17260 if (IDENTIFIER_CONV_OP_P (t))
17262 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17263 t = make_conv_op_name (new_type);
17266 /* Look up the name. */
17267 decl = lookup_name (t);
17269 /* By convention, expressions use ERROR_MARK_NODE to indicate
17270 failure, not NULL_TREE. */
17271 if (decl == NULL_TREE)
17272 decl = error_mark_node;
17274 decl = finish_id_expression (t, decl, NULL_TREE,
17275 &idk,
17276 integral_constant_expression_p,
17277 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
17278 &non_integral_constant_expression_p,
17279 /*template_p=*/false,
17280 /*done=*/true,
17281 /*address_p=*/false,
17282 /*template_arg_p=*/false,
17283 &error_msg,
17284 input_location);
17285 if (error_msg)
17286 error (error_msg);
17287 if (!function_p && identifier_p (decl))
17289 if (complain & tf_error)
17290 unqualified_name_lookup_error (decl);
17291 decl = error_mark_node;
17293 RETURN (decl);
17296 case TEMPLATE_ID_EXPR:
17298 tree object;
17299 tree templ = RECUR (TREE_OPERAND (t, 0));
17300 tree targs = TREE_OPERAND (t, 1);
17302 if (targs)
17303 targs = tsubst_template_args (targs, args, complain, in_decl);
17304 if (targs == error_mark_node)
17305 return error_mark_node;
17307 if (TREE_CODE (templ) == SCOPE_REF)
17309 tree name = TREE_OPERAND (templ, 1);
17310 tree tid = lookup_template_function (name, targs);
17311 TREE_OPERAND (templ, 1) = tid;
17312 return templ;
17315 if (variable_template_p (templ))
17316 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
17318 if (TREE_CODE (templ) == COMPONENT_REF)
17320 object = TREE_OPERAND (templ, 0);
17321 templ = TREE_OPERAND (templ, 1);
17323 else
17324 object = NULL_TREE;
17325 templ = lookup_template_function (templ, targs);
17327 if (object)
17328 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
17329 object, templ, NULL_TREE));
17330 else
17331 RETURN (baselink_for_fns (templ));
17334 case INDIRECT_REF:
17336 tree r = RECUR (TREE_OPERAND (t, 0));
17338 if (REFERENCE_REF_P (t))
17340 /* A type conversion to reference type will be enclosed in
17341 such an indirect ref, but the substitution of the cast
17342 will have also added such an indirect ref. */
17343 r = convert_from_reference (r);
17345 else
17346 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
17347 complain|decltype_flag);
17349 if (REF_PARENTHESIZED_P (t))
17350 r = force_paren_expr (r);
17352 RETURN (r);
17355 case NOP_EXPR:
17357 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17358 tree op0 = RECUR (TREE_OPERAND (t, 0));
17359 RETURN (build_nop (type, op0));
17362 case IMPLICIT_CONV_EXPR:
17364 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17365 tree expr = RECUR (TREE_OPERAND (t, 0));
17366 if (dependent_type_p (type) || type_dependent_expression_p (expr))
17368 retval = copy_node (t);
17369 TREE_TYPE (retval) = type;
17370 TREE_OPERAND (retval, 0) = expr;
17371 RETURN (retval);
17373 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
17374 /* We'll pass this to convert_nontype_argument again, we don't need
17375 to actually perform any conversion here. */
17376 RETURN (expr);
17377 int flags = LOOKUP_IMPLICIT;
17378 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
17379 flags = LOOKUP_NORMAL;
17380 RETURN (perform_implicit_conversion_flags (type, expr, complain,
17381 flags));
17384 case CONVERT_EXPR:
17386 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17387 tree op0 = RECUR (TREE_OPERAND (t, 0));
17388 RETURN (build1 (CONVERT_EXPR, type, op0));
17391 case CAST_EXPR:
17392 case REINTERPRET_CAST_EXPR:
17393 case CONST_CAST_EXPR:
17394 case DYNAMIC_CAST_EXPR:
17395 case STATIC_CAST_EXPR:
17397 tree type;
17398 tree op, r = NULL_TREE;
17400 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17401 if (integral_constant_expression_p
17402 && !cast_valid_in_integral_constant_expression_p (type))
17404 if (complain & tf_error)
17405 error ("a cast to a type other than an integral or "
17406 "enumeration type cannot appear in a constant-expression");
17407 RETURN (error_mark_node);
17410 op = RECUR (TREE_OPERAND (t, 0));
17412 warning_sentinel s(warn_useless_cast);
17413 warning_sentinel s2(warn_ignored_qualifiers);
17414 switch (TREE_CODE (t))
17416 case CAST_EXPR:
17417 r = build_functional_cast (type, op, complain);
17418 break;
17419 case REINTERPRET_CAST_EXPR:
17420 r = build_reinterpret_cast (type, op, complain);
17421 break;
17422 case CONST_CAST_EXPR:
17423 r = build_const_cast (type, op, complain);
17424 break;
17425 case DYNAMIC_CAST_EXPR:
17426 r = build_dynamic_cast (type, op, complain);
17427 break;
17428 case STATIC_CAST_EXPR:
17429 r = build_static_cast (type, op, complain);
17430 break;
17431 default:
17432 gcc_unreachable ();
17435 RETURN (r);
17438 case POSTDECREMENT_EXPR:
17439 case POSTINCREMENT_EXPR:
17440 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17441 args, complain, in_decl);
17442 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
17443 complain|decltype_flag));
17445 case PREDECREMENT_EXPR:
17446 case PREINCREMENT_EXPR:
17447 case NEGATE_EXPR:
17448 case BIT_NOT_EXPR:
17449 case ABS_EXPR:
17450 case TRUTH_NOT_EXPR:
17451 case UNARY_PLUS_EXPR: /* Unary + */
17452 case REALPART_EXPR:
17453 case IMAGPART_EXPR:
17454 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
17455 RECUR (TREE_OPERAND (t, 0)),
17456 complain|decltype_flag));
17458 case FIX_TRUNC_EXPR:
17459 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
17460 false, complain));
17462 case ADDR_EXPR:
17463 op1 = TREE_OPERAND (t, 0);
17464 if (TREE_CODE (op1) == LABEL_DECL)
17465 RETURN (finish_label_address_expr (DECL_NAME (op1),
17466 EXPR_LOCATION (op1)));
17467 if (TREE_CODE (op1) == SCOPE_REF)
17468 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
17469 /*done=*/true, /*address_p=*/true);
17470 else
17471 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
17472 in_decl);
17473 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
17474 complain|decltype_flag));
17476 case PLUS_EXPR:
17477 case MINUS_EXPR:
17478 case MULT_EXPR:
17479 case TRUNC_DIV_EXPR:
17480 case CEIL_DIV_EXPR:
17481 case FLOOR_DIV_EXPR:
17482 case ROUND_DIV_EXPR:
17483 case EXACT_DIV_EXPR:
17484 case BIT_AND_EXPR:
17485 case BIT_IOR_EXPR:
17486 case BIT_XOR_EXPR:
17487 case TRUNC_MOD_EXPR:
17488 case FLOOR_MOD_EXPR:
17489 case TRUTH_ANDIF_EXPR:
17490 case TRUTH_ORIF_EXPR:
17491 case TRUTH_AND_EXPR:
17492 case TRUTH_OR_EXPR:
17493 case RSHIFT_EXPR:
17494 case LSHIFT_EXPR:
17495 case RROTATE_EXPR:
17496 case LROTATE_EXPR:
17497 case EQ_EXPR:
17498 case NE_EXPR:
17499 case MAX_EXPR:
17500 case MIN_EXPR:
17501 case LE_EXPR:
17502 case GE_EXPR:
17503 case LT_EXPR:
17504 case GT_EXPR:
17505 case MEMBER_REF:
17506 case DOTSTAR_EXPR:
17508 warning_sentinel s1(warn_type_limits);
17509 warning_sentinel s2(warn_div_by_zero);
17510 warning_sentinel s3(warn_logical_op);
17511 warning_sentinel s4(warn_tautological_compare);
17512 tree op0 = RECUR (TREE_OPERAND (t, 0));
17513 tree op1 = RECUR (TREE_OPERAND (t, 1));
17514 tree r = build_x_binary_op
17515 (input_location, TREE_CODE (t),
17516 op0,
17517 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
17518 ? ERROR_MARK
17519 : TREE_CODE (TREE_OPERAND (t, 0))),
17520 op1,
17521 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
17522 ? ERROR_MARK
17523 : TREE_CODE (TREE_OPERAND (t, 1))),
17524 /*overload=*/NULL,
17525 complain|decltype_flag);
17526 if (EXPR_P (r) && TREE_NO_WARNING (t))
17527 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17529 RETURN (r);
17532 case POINTER_PLUS_EXPR:
17534 tree op0 = RECUR (TREE_OPERAND (t, 0));
17535 tree op1 = RECUR (TREE_OPERAND (t, 1));
17536 return fold_build_pointer_plus (op0, op1);
17539 case SCOPE_REF:
17540 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
17541 /*address_p=*/false));
17542 case ARRAY_REF:
17543 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17544 args, complain, in_decl);
17545 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
17546 RECUR (TREE_OPERAND (t, 1)),
17547 complain|decltype_flag));
17549 case SIZEOF_EXPR:
17550 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
17551 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
17552 RETURN (tsubst_copy (t, args, complain, in_decl));
17553 /* Fall through */
17555 case ALIGNOF_EXPR:
17557 tree r;
17559 op1 = TREE_OPERAND (t, 0);
17560 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
17561 op1 = TREE_TYPE (op1);
17562 if (!args)
17564 /* When there are no ARGS, we are trying to evaluate a
17565 non-dependent expression from the parser. Trying to do
17566 the substitutions may not work. */
17567 if (!TYPE_P (op1))
17568 op1 = TREE_TYPE (op1);
17570 else
17572 ++cp_unevaluated_operand;
17573 ++c_inhibit_evaluation_warnings;
17574 if (TYPE_P (op1))
17575 op1 = tsubst (op1, args, complain, in_decl);
17576 else
17577 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17578 /*function_p=*/false,
17579 /*integral_constant_expression_p=*/
17580 false);
17581 --cp_unevaluated_operand;
17582 --c_inhibit_evaluation_warnings;
17584 if (TYPE_P (op1))
17585 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
17586 complain & tf_error);
17587 else
17588 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
17589 complain & tf_error);
17590 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
17592 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
17594 if (!processing_template_decl && TYPE_P (op1))
17596 r = build_min (SIZEOF_EXPR, size_type_node,
17597 build1 (NOP_EXPR, op1, error_mark_node));
17598 SIZEOF_EXPR_TYPE_P (r) = 1;
17600 else
17601 r = build_min (SIZEOF_EXPR, size_type_node, op1);
17602 TREE_SIDE_EFFECTS (r) = 0;
17603 TREE_READONLY (r) = 1;
17605 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
17607 RETURN (r);
17610 case AT_ENCODE_EXPR:
17612 op1 = TREE_OPERAND (t, 0);
17613 ++cp_unevaluated_operand;
17614 ++c_inhibit_evaluation_warnings;
17615 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17616 /*function_p=*/false,
17617 /*integral_constant_expression_p=*/false);
17618 --cp_unevaluated_operand;
17619 --c_inhibit_evaluation_warnings;
17620 RETURN (objc_build_encode_expr (op1));
17623 case NOEXCEPT_EXPR:
17624 op1 = TREE_OPERAND (t, 0);
17625 ++cp_unevaluated_operand;
17626 ++c_inhibit_evaluation_warnings;
17627 ++cp_noexcept_operand;
17628 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17629 /*function_p=*/false,
17630 /*integral_constant_expression_p=*/false);
17631 --cp_unevaluated_operand;
17632 --c_inhibit_evaluation_warnings;
17633 --cp_noexcept_operand;
17634 RETURN (finish_noexcept_expr (op1, complain));
17636 case MODOP_EXPR:
17638 warning_sentinel s(warn_div_by_zero);
17639 tree lhs = RECUR (TREE_OPERAND (t, 0));
17640 tree rhs = RECUR (TREE_OPERAND (t, 2));
17641 tree r = build_x_modify_expr
17642 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
17643 complain|decltype_flag);
17644 /* TREE_NO_WARNING must be set if either the expression was
17645 parenthesized or it uses an operator such as >>= rather
17646 than plain assignment. In the former case, it was already
17647 set and must be copied. In the latter case,
17648 build_x_modify_expr sets it and it must not be reset
17649 here. */
17650 if (TREE_NO_WARNING (t))
17651 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17653 RETURN (r);
17656 case ARROW_EXPR:
17657 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17658 args, complain, in_decl);
17659 /* Remember that there was a reference to this entity. */
17660 if (DECL_P (op1)
17661 && !mark_used (op1, complain) && !(complain & tf_error))
17662 RETURN (error_mark_node);
17663 RETURN (build_x_arrow (input_location, op1, complain));
17665 case NEW_EXPR:
17667 tree placement = RECUR (TREE_OPERAND (t, 0));
17668 tree init = RECUR (TREE_OPERAND (t, 3));
17669 vec<tree, va_gc> *placement_vec;
17670 vec<tree, va_gc> *init_vec;
17671 tree ret;
17673 if (placement == NULL_TREE)
17674 placement_vec = NULL;
17675 else
17677 placement_vec = make_tree_vector ();
17678 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
17679 vec_safe_push (placement_vec, TREE_VALUE (placement));
17682 /* If there was an initializer in the original tree, but it
17683 instantiated to an empty list, then we should pass a
17684 non-NULL empty vector to tell build_new that it was an
17685 empty initializer() rather than no initializer. This can
17686 only happen when the initializer is a pack expansion whose
17687 parameter packs are of length zero. */
17688 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
17689 init_vec = NULL;
17690 else
17692 init_vec = make_tree_vector ();
17693 if (init == void_node)
17694 gcc_assert (init_vec != NULL);
17695 else
17697 for (; init != NULL_TREE; init = TREE_CHAIN (init))
17698 vec_safe_push (init_vec, TREE_VALUE (init));
17702 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
17703 tree op2 = RECUR (TREE_OPERAND (t, 2));
17704 ret = build_new (&placement_vec, op1, op2, &init_vec,
17705 NEW_EXPR_USE_GLOBAL (t),
17706 complain);
17708 if (placement_vec != NULL)
17709 release_tree_vector (placement_vec);
17710 if (init_vec != NULL)
17711 release_tree_vector (init_vec);
17713 RETURN (ret);
17716 case DELETE_EXPR:
17718 tree op0 = RECUR (TREE_OPERAND (t, 0));
17719 tree op1 = RECUR (TREE_OPERAND (t, 1));
17720 RETURN (delete_sanity (op0, op1,
17721 DELETE_EXPR_USE_VEC (t),
17722 DELETE_EXPR_USE_GLOBAL (t),
17723 complain));
17726 case COMPOUND_EXPR:
17728 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
17729 complain & ~tf_decltype, in_decl,
17730 /*function_p=*/false,
17731 integral_constant_expression_p);
17732 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
17733 op0,
17734 RECUR (TREE_OPERAND (t, 1)),
17735 complain|decltype_flag));
17738 case CALL_EXPR:
17740 tree function;
17741 vec<tree, va_gc> *call_args;
17742 unsigned int nargs, i;
17743 bool qualified_p;
17744 bool koenig_p;
17745 tree ret;
17747 function = CALL_EXPR_FN (t);
17748 /* Internal function with no arguments. */
17749 if (function == NULL_TREE && call_expr_nargs (t) == 0)
17750 RETURN (t);
17752 /* When we parsed the expression, we determined whether or
17753 not Koenig lookup should be performed. */
17754 koenig_p = KOENIG_LOOKUP_P (t);
17755 if (function == NULL_TREE)
17757 koenig_p = false;
17758 qualified_p = false;
17760 else if (TREE_CODE (function) == SCOPE_REF)
17762 qualified_p = true;
17763 function = tsubst_qualified_id (function, args, complain, in_decl,
17764 /*done=*/false,
17765 /*address_p=*/false);
17767 else if (koenig_p && identifier_p (function))
17769 /* Do nothing; calling tsubst_copy_and_build on an identifier
17770 would incorrectly perform unqualified lookup again.
17772 Note that we can also have an IDENTIFIER_NODE if the earlier
17773 unqualified lookup found a member function; in that case
17774 koenig_p will be false and we do want to do the lookup
17775 again to find the instantiated member function.
17777 FIXME but doing that causes c++/15272, so we need to stop
17778 using IDENTIFIER_NODE in that situation. */
17779 qualified_p = false;
17781 else
17783 if (TREE_CODE (function) == COMPONENT_REF)
17785 tree op = TREE_OPERAND (function, 1);
17787 qualified_p = (TREE_CODE (op) == SCOPE_REF
17788 || (BASELINK_P (op)
17789 && BASELINK_QUALIFIED_P (op)));
17791 else
17792 qualified_p = false;
17794 if (TREE_CODE (function) == ADDR_EXPR
17795 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
17796 /* Avoid error about taking the address of a constructor. */
17797 function = TREE_OPERAND (function, 0);
17799 function = tsubst_copy_and_build (function, args, complain,
17800 in_decl,
17801 !qualified_p,
17802 integral_constant_expression_p);
17804 if (BASELINK_P (function))
17805 qualified_p = true;
17808 nargs = call_expr_nargs (t);
17809 call_args = make_tree_vector ();
17810 for (i = 0; i < nargs; ++i)
17812 tree arg = CALL_EXPR_ARG (t, i);
17814 if (!PACK_EXPANSION_P (arg))
17815 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
17816 else
17818 /* Expand the pack expansion and push each entry onto
17819 CALL_ARGS. */
17820 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
17821 if (TREE_CODE (arg) == TREE_VEC)
17823 unsigned int len, j;
17825 len = TREE_VEC_LENGTH (arg);
17826 for (j = 0; j < len; ++j)
17828 tree value = TREE_VEC_ELT (arg, j);
17829 if (value != NULL_TREE)
17830 value = convert_from_reference (value);
17831 vec_safe_push (call_args, value);
17834 else
17836 /* A partial substitution. Add one entry. */
17837 vec_safe_push (call_args, arg);
17842 /* We do not perform argument-dependent lookup if normal
17843 lookup finds a non-function, in accordance with the
17844 expected resolution of DR 218. */
17845 if (koenig_p
17846 && ((is_overloaded_fn (function)
17847 /* If lookup found a member function, the Koenig lookup is
17848 not appropriate, even if an unqualified-name was used
17849 to denote the function. */
17850 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
17851 || identifier_p (function))
17852 /* Only do this when substitution turns a dependent call
17853 into a non-dependent call. */
17854 && type_dependent_expression_p_push (t)
17855 && !any_type_dependent_arguments_p (call_args))
17856 function = perform_koenig_lookup (function, call_args, tf_none);
17858 if (function != NULL_TREE
17859 && identifier_p (function)
17860 && !any_type_dependent_arguments_p (call_args))
17862 if (koenig_p && (complain & tf_warning_or_error))
17864 /* For backwards compatibility and good diagnostics, try
17865 the unqualified lookup again if we aren't in SFINAE
17866 context. */
17867 tree unq = (tsubst_copy_and_build
17868 (function, args, complain, in_decl, true,
17869 integral_constant_expression_p));
17870 if (unq == error_mark_node)
17872 release_tree_vector (call_args);
17873 RETURN (error_mark_node);
17876 if (unq != function)
17878 /* In a lambda fn, we have to be careful to not
17879 introduce new this captures. Legacy code can't
17880 be using lambdas anyway, so it's ok to be
17881 stricter. */
17882 bool in_lambda = (current_class_type
17883 && LAMBDA_TYPE_P (current_class_type));
17884 char const *const msg
17885 = G_("%qD was not declared in this scope, "
17886 "and no declarations were found by "
17887 "argument-dependent lookup at the point "
17888 "of instantiation");
17890 bool diag = true;
17891 if (in_lambda)
17892 error_at (EXPR_LOC_OR_LOC (t, input_location),
17893 msg, function);
17894 else
17895 diag = permerror (EXPR_LOC_OR_LOC (t, input_location),
17896 msg, function);
17897 if (diag)
17899 tree fn = unq;
17901 if (INDIRECT_REF_P (fn))
17902 fn = TREE_OPERAND (fn, 0);
17903 if (is_overloaded_fn (fn))
17904 fn = get_first_fn (fn);
17906 if (!DECL_P (fn))
17907 /* Can't say anything more. */;
17908 else if (DECL_CLASS_SCOPE_P (fn))
17910 location_t loc = EXPR_LOC_OR_LOC (t,
17911 input_location);
17912 inform (loc,
17913 "declarations in dependent base %qT are "
17914 "not found by unqualified lookup",
17915 DECL_CLASS_CONTEXT (fn));
17916 if (current_class_ptr)
17917 inform (loc,
17918 "use %<this->%D%> instead", function);
17919 else
17920 inform (loc,
17921 "use %<%T::%D%> instead",
17922 current_class_name, function);
17924 else
17925 inform (DECL_SOURCE_LOCATION (fn),
17926 "%qD declared here, later in the "
17927 "translation unit", fn);
17928 if (in_lambda)
17930 release_tree_vector (call_args);
17931 RETURN (error_mark_node);
17935 function = unq;
17938 if (identifier_p (function))
17940 if (complain & tf_error)
17941 unqualified_name_lookup_error (function);
17942 release_tree_vector (call_args);
17943 RETURN (error_mark_node);
17947 /* Remember that there was a reference to this entity. */
17948 if (function != NULL_TREE
17949 && DECL_P (function)
17950 && !mark_used (function, complain) && !(complain & tf_error))
17952 release_tree_vector (call_args);
17953 RETURN (error_mark_node);
17956 /* Put back tf_decltype for the actual call. */
17957 complain |= decltype_flag;
17959 if (function == NULL_TREE)
17960 switch (CALL_EXPR_IFN (t))
17962 case IFN_LAUNDER:
17963 gcc_assert (nargs == 1);
17964 if (vec_safe_length (call_args) != 1)
17966 error_at (EXPR_LOC_OR_LOC (t, input_location),
17967 "wrong number of arguments to "
17968 "%<__builtin_launder%>");
17969 ret = error_mark_node;
17971 else
17972 ret = finish_builtin_launder (EXPR_LOC_OR_LOC (t,
17973 input_location),
17974 (*call_args)[0], complain);
17975 break;
17977 default:
17978 /* Unsupported internal function with arguments. */
17979 gcc_unreachable ();
17981 else if (TREE_CODE (function) == OFFSET_REF)
17982 ret = build_offset_ref_call_from_tree (function, &call_args,
17983 complain);
17984 else if (TREE_CODE (function) == COMPONENT_REF)
17986 tree instance = TREE_OPERAND (function, 0);
17987 tree fn = TREE_OPERAND (function, 1);
17989 if (processing_template_decl
17990 && (type_dependent_expression_p (instance)
17991 || (!BASELINK_P (fn)
17992 && TREE_CODE (fn) != FIELD_DECL)
17993 || type_dependent_expression_p (fn)
17994 || any_type_dependent_arguments_p (call_args)))
17995 ret = build_min_nt_call_vec (function, call_args);
17996 else if (!BASELINK_P (fn))
17997 ret = finish_call_expr (function, &call_args,
17998 /*disallow_virtual=*/false,
17999 /*koenig_p=*/false,
18000 complain);
18001 else
18002 ret = (build_new_method_call
18003 (instance, fn,
18004 &call_args, NULL_TREE,
18005 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
18006 /*fn_p=*/NULL,
18007 complain));
18009 else
18010 ret = finish_call_expr (function, &call_args,
18011 /*disallow_virtual=*/qualified_p,
18012 koenig_p,
18013 complain);
18015 release_tree_vector (call_args);
18017 if (ret != error_mark_node)
18019 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
18020 bool ord = CALL_EXPR_ORDERED_ARGS (t);
18021 bool rev = CALL_EXPR_REVERSE_ARGS (t);
18022 bool thk = CALL_FROM_THUNK_P (t);
18023 if (op || ord || rev || thk)
18025 function = extract_call_expr (ret);
18026 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
18027 CALL_EXPR_ORDERED_ARGS (function) = ord;
18028 CALL_EXPR_REVERSE_ARGS (function) = rev;
18029 if (thk)
18031 if (TREE_CODE (function) == CALL_EXPR)
18032 CALL_FROM_THUNK_P (function) = true;
18033 else
18034 AGGR_INIT_FROM_THUNK_P (function) = true;
18035 /* The thunk location is not interesting. */
18036 SET_EXPR_LOCATION (function, UNKNOWN_LOCATION);
18041 RETURN (ret);
18044 case COND_EXPR:
18046 tree cond = RECUR (TREE_OPERAND (t, 0));
18047 tree folded_cond = fold_non_dependent_expr (cond);
18048 tree exp1, exp2;
18050 if (TREE_CODE (folded_cond) == INTEGER_CST)
18052 if (integer_zerop (folded_cond))
18054 ++c_inhibit_evaluation_warnings;
18055 exp1 = RECUR (TREE_OPERAND (t, 1));
18056 --c_inhibit_evaluation_warnings;
18057 exp2 = RECUR (TREE_OPERAND (t, 2));
18059 else
18061 exp1 = RECUR (TREE_OPERAND (t, 1));
18062 ++c_inhibit_evaluation_warnings;
18063 exp2 = RECUR (TREE_OPERAND (t, 2));
18064 --c_inhibit_evaluation_warnings;
18066 cond = folded_cond;
18068 else
18070 exp1 = RECUR (TREE_OPERAND (t, 1));
18071 exp2 = RECUR (TREE_OPERAND (t, 2));
18074 warning_sentinel s(warn_duplicated_branches);
18075 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
18076 cond, exp1, exp2, complain));
18079 case PSEUDO_DTOR_EXPR:
18081 tree op0 = RECUR (TREE_OPERAND (t, 0));
18082 tree op1 = RECUR (TREE_OPERAND (t, 1));
18083 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
18084 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
18085 input_location));
18088 case TREE_LIST:
18090 tree purpose, value, chain;
18092 if (t == void_list_node)
18093 RETURN (t);
18095 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
18096 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
18098 /* We have pack expansions, so expand those and
18099 create a new list out of it. */
18100 tree purposevec = NULL_TREE;
18101 tree valuevec = NULL_TREE;
18102 tree chain;
18103 int i, len = -1;
18105 /* Expand the argument expressions. */
18106 if (TREE_PURPOSE (t))
18107 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
18108 complain, in_decl);
18109 if (TREE_VALUE (t))
18110 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
18111 complain, in_decl);
18113 /* Build the rest of the list. */
18114 chain = TREE_CHAIN (t);
18115 if (chain && chain != void_type_node)
18116 chain = RECUR (chain);
18118 /* Determine the number of arguments. */
18119 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
18121 len = TREE_VEC_LENGTH (purposevec);
18122 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
18124 else if (TREE_CODE (valuevec) == TREE_VEC)
18125 len = TREE_VEC_LENGTH (valuevec);
18126 else
18128 /* Since we only performed a partial substitution into
18129 the argument pack, we only RETURN (a single list
18130 node. */
18131 if (purposevec == TREE_PURPOSE (t)
18132 && valuevec == TREE_VALUE (t)
18133 && chain == TREE_CHAIN (t))
18134 RETURN (t);
18136 RETURN (tree_cons (purposevec, valuevec, chain));
18139 /* Convert the argument vectors into a TREE_LIST */
18140 i = len;
18141 while (i > 0)
18143 /* Grab the Ith values. */
18144 i--;
18145 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
18146 : NULL_TREE;
18147 value
18148 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
18149 : NULL_TREE;
18151 /* Build the list (backwards). */
18152 chain = tree_cons (purpose, value, chain);
18155 RETURN (chain);
18158 purpose = TREE_PURPOSE (t);
18159 if (purpose)
18160 purpose = RECUR (purpose);
18161 value = TREE_VALUE (t);
18162 if (value)
18163 value = RECUR (value);
18164 chain = TREE_CHAIN (t);
18165 if (chain && chain != void_type_node)
18166 chain = RECUR (chain);
18167 if (purpose == TREE_PURPOSE (t)
18168 && value == TREE_VALUE (t)
18169 && chain == TREE_CHAIN (t))
18170 RETURN (t);
18171 RETURN (tree_cons (purpose, value, chain));
18174 case COMPONENT_REF:
18176 tree object;
18177 tree object_type;
18178 tree member;
18179 tree r;
18181 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18182 args, complain, in_decl);
18183 /* Remember that there was a reference to this entity. */
18184 if (DECL_P (object)
18185 && !mark_used (object, complain) && !(complain & tf_error))
18186 RETURN (error_mark_node);
18187 object_type = TREE_TYPE (object);
18189 member = TREE_OPERAND (t, 1);
18190 if (BASELINK_P (member))
18191 member = tsubst_baselink (member,
18192 non_reference (TREE_TYPE (object)),
18193 args, complain, in_decl);
18194 else
18195 member = tsubst_copy (member, args, complain, in_decl);
18196 if (member == error_mark_node)
18197 RETURN (error_mark_node);
18199 if (TREE_CODE (member) == FIELD_DECL)
18201 r = finish_non_static_data_member (member, object, NULL_TREE);
18202 if (TREE_CODE (r) == COMPONENT_REF)
18203 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
18204 RETURN (r);
18206 else if (type_dependent_expression_p (object))
18207 /* We can't do much here. */;
18208 else if (!CLASS_TYPE_P (object_type))
18210 if (scalarish_type_p (object_type))
18212 tree s = NULL_TREE;
18213 tree dtor = member;
18215 if (TREE_CODE (dtor) == SCOPE_REF)
18217 s = TREE_OPERAND (dtor, 0);
18218 dtor = TREE_OPERAND (dtor, 1);
18220 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
18222 dtor = TREE_OPERAND (dtor, 0);
18223 if (TYPE_P (dtor))
18224 RETURN (finish_pseudo_destructor_expr
18225 (object, s, dtor, input_location));
18229 else if (TREE_CODE (member) == SCOPE_REF
18230 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
18232 /* Lookup the template functions now that we know what the
18233 scope is. */
18234 tree scope = TREE_OPERAND (member, 0);
18235 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
18236 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
18237 member = lookup_qualified_name (scope, tmpl,
18238 /*is_type_p=*/false,
18239 /*complain=*/false);
18240 if (BASELINK_P (member))
18242 BASELINK_FUNCTIONS (member)
18243 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
18244 args);
18245 member = (adjust_result_of_qualified_name_lookup
18246 (member, BINFO_TYPE (BASELINK_BINFO (member)),
18247 object_type));
18249 else
18251 qualified_name_lookup_error (scope, tmpl, member,
18252 input_location);
18253 RETURN (error_mark_node);
18256 else if (TREE_CODE (member) == SCOPE_REF
18257 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
18258 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
18260 if (complain & tf_error)
18262 if (TYPE_P (TREE_OPERAND (member, 0)))
18263 error ("%qT is not a class or namespace",
18264 TREE_OPERAND (member, 0));
18265 else
18266 error ("%qD is not a class or namespace",
18267 TREE_OPERAND (member, 0));
18269 RETURN (error_mark_node);
18272 r = finish_class_member_access_expr (object, member,
18273 /*template_p=*/false,
18274 complain);
18275 if (TREE_CODE (r) == COMPONENT_REF)
18276 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
18277 RETURN (r);
18280 case THROW_EXPR:
18281 RETURN (build_throw
18282 (RECUR (TREE_OPERAND (t, 0))));
18284 case CONSTRUCTOR:
18286 vec<constructor_elt, va_gc> *n;
18287 constructor_elt *ce;
18288 unsigned HOST_WIDE_INT idx;
18289 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18290 bool process_index_p;
18291 int newlen;
18292 bool need_copy_p = false;
18293 tree r;
18295 if (type == error_mark_node)
18296 RETURN (error_mark_node);
18298 /* We do not want to process the index of aggregate
18299 initializers as they are identifier nodes which will be
18300 looked up by digest_init. */
18301 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
18303 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
18304 newlen = vec_safe_length (n);
18305 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
18307 if (ce->index && process_index_p
18308 /* An identifier index is looked up in the type
18309 being initialized, not the current scope. */
18310 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
18311 ce->index = RECUR (ce->index);
18313 if (PACK_EXPANSION_P (ce->value))
18315 /* Substitute into the pack expansion. */
18316 ce->value = tsubst_pack_expansion (ce->value, args, complain,
18317 in_decl);
18319 if (ce->value == error_mark_node
18320 || PACK_EXPANSION_P (ce->value))
18322 else if (TREE_VEC_LENGTH (ce->value) == 1)
18323 /* Just move the argument into place. */
18324 ce->value = TREE_VEC_ELT (ce->value, 0);
18325 else
18327 /* Update the length of the final CONSTRUCTOR
18328 arguments vector, and note that we will need to
18329 copy.*/
18330 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
18331 need_copy_p = true;
18334 else
18335 ce->value = RECUR (ce->value);
18338 if (need_copy_p)
18340 vec<constructor_elt, va_gc> *old_n = n;
18342 vec_alloc (n, newlen);
18343 FOR_EACH_VEC_ELT (*old_n, idx, ce)
18345 if (TREE_CODE (ce->value) == TREE_VEC)
18347 int i, len = TREE_VEC_LENGTH (ce->value);
18348 for (i = 0; i < len; ++i)
18349 CONSTRUCTOR_APPEND_ELT (n, 0,
18350 TREE_VEC_ELT (ce->value, i));
18352 else
18353 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
18357 r = build_constructor (init_list_type_node, n);
18358 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
18360 if (TREE_HAS_CONSTRUCTOR (t))
18362 fcl_t cl = fcl_functional;
18363 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
18364 cl = fcl_c99;
18365 RETURN (finish_compound_literal (type, r, complain, cl));
18368 TREE_TYPE (r) = type;
18369 RETURN (r);
18372 case TYPEID_EXPR:
18374 tree operand_0 = TREE_OPERAND (t, 0);
18375 if (TYPE_P (operand_0))
18377 operand_0 = tsubst (operand_0, args, complain, in_decl);
18378 RETURN (get_typeid (operand_0, complain));
18380 else
18382 operand_0 = RECUR (operand_0);
18383 RETURN (build_typeid (operand_0, complain));
18387 case VAR_DECL:
18388 if (!args)
18389 RETURN (t);
18390 /* Fall through */
18392 case PARM_DECL:
18394 tree r = tsubst_copy (t, args, complain, in_decl);
18395 /* ??? We're doing a subset of finish_id_expression here. */
18396 if (VAR_P (r)
18397 && !processing_template_decl
18398 && !cp_unevaluated_operand
18399 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
18400 && CP_DECL_THREAD_LOCAL_P (r))
18402 if (tree wrap = get_tls_wrapper_fn (r))
18403 /* Replace an evaluated use of the thread_local variable with
18404 a call to its wrapper. */
18405 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
18407 else if (outer_automatic_var_p (r))
18408 r = process_outer_var_ref (r, complain);
18410 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
18411 /* If the original type was a reference, we'll be wrapped in
18412 the appropriate INDIRECT_REF. */
18413 r = convert_from_reference (r);
18414 RETURN (r);
18417 case VA_ARG_EXPR:
18419 tree op0 = RECUR (TREE_OPERAND (t, 0));
18420 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18421 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
18424 case OFFSETOF_EXPR:
18426 tree object_ptr
18427 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
18428 in_decl, /*function_p=*/false,
18429 /*integral_constant_expression_p=*/false);
18430 RETURN (finish_offsetof (object_ptr,
18431 RECUR (TREE_OPERAND (t, 0)),
18432 EXPR_LOCATION (t)));
18435 case ADDRESSOF_EXPR:
18436 RETURN (cp_build_addressof (EXPR_LOCATION (t),
18437 RECUR (TREE_OPERAND (t, 0)), complain));
18439 case TRAIT_EXPR:
18441 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
18442 complain, in_decl);
18444 tree type2 = TRAIT_EXPR_TYPE2 (t);
18445 if (type2 && TREE_CODE (type2) == TREE_LIST)
18446 type2 = RECUR (type2);
18447 else if (type2)
18448 type2 = tsubst (type2, args, complain, in_decl);
18450 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
18453 case STMT_EXPR:
18455 tree old_stmt_expr = cur_stmt_expr;
18456 tree stmt_expr = begin_stmt_expr ();
18458 cur_stmt_expr = stmt_expr;
18459 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
18460 integral_constant_expression_p);
18461 stmt_expr = finish_stmt_expr (stmt_expr, false);
18462 cur_stmt_expr = old_stmt_expr;
18464 /* If the resulting list of expression statement is empty,
18465 fold it further into void_node. */
18466 if (empty_expr_stmt_p (stmt_expr))
18467 stmt_expr = void_node;
18469 RETURN (stmt_expr);
18472 case LAMBDA_EXPR:
18474 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
18476 RETURN (build_lambda_object (r));
18479 case TARGET_EXPR:
18480 /* We can get here for a constant initializer of non-dependent type.
18481 FIXME stop folding in cp_parser_initializer_clause. */
18483 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
18484 complain);
18485 RETURN (r);
18488 case TRANSACTION_EXPR:
18489 RETURN (tsubst_expr(t, args, complain, in_decl,
18490 integral_constant_expression_p));
18492 case PAREN_EXPR:
18493 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
18495 case VEC_PERM_EXPR:
18497 tree op0 = RECUR (TREE_OPERAND (t, 0));
18498 tree op1 = RECUR (TREE_OPERAND (t, 1));
18499 tree op2 = RECUR (TREE_OPERAND (t, 2));
18500 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
18501 complain));
18504 case REQUIRES_EXPR:
18505 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
18507 case NON_LVALUE_EXPR:
18508 case VIEW_CONVERT_EXPR:
18509 /* We should only see these for location wrapper nodes, or within
18510 instantiate_non_dependent_expr (when args is NULL_TREE). */
18511 gcc_assert (location_wrapper_p (t) || args == NULL_TREE);
18512 if (location_wrapper_p (t))
18513 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)),
18514 EXPR_LOCATION (t)));
18515 /* fallthrough. */
18517 default:
18518 /* Handle Objective-C++ constructs, if appropriate. */
18520 tree subst
18521 = objcp_tsubst_copy_and_build (t, args, complain,
18522 in_decl, /*function_p=*/false);
18523 if (subst)
18524 RETURN (subst);
18526 RETURN (tsubst_copy (t, args, complain, in_decl));
18529 #undef RECUR
18530 #undef RETURN
18531 out:
18532 input_location = loc;
18533 return retval;
18536 /* Verify that the instantiated ARGS are valid. For type arguments,
18537 make sure that the type's linkage is ok. For non-type arguments,
18538 make sure they are constants if they are integral or enumerations.
18539 Emit an error under control of COMPLAIN, and return TRUE on error. */
18541 static bool
18542 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
18544 if (dependent_template_arg_p (t))
18545 return false;
18546 if (ARGUMENT_PACK_P (t))
18548 tree vec = ARGUMENT_PACK_ARGS (t);
18549 int len = TREE_VEC_LENGTH (vec);
18550 bool result = false;
18551 int i;
18553 for (i = 0; i < len; ++i)
18554 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
18555 result = true;
18556 return result;
18558 else if (TYPE_P (t))
18560 /* [basic.link]: A name with no linkage (notably, the name
18561 of a class or enumeration declared in a local scope)
18562 shall not be used to declare an entity with linkage.
18563 This implies that names with no linkage cannot be used as
18564 template arguments
18566 DR 757 relaxes this restriction for C++0x. */
18567 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
18568 : no_linkage_check (t, /*relaxed_p=*/false));
18570 if (nt)
18572 /* DR 488 makes use of a type with no linkage cause
18573 type deduction to fail. */
18574 if (complain & tf_error)
18576 if (TYPE_UNNAMED_P (nt))
18577 error ("%qT is/uses unnamed type", t);
18578 else
18579 error ("template argument for %qD uses local type %qT",
18580 tmpl, t);
18582 return true;
18584 /* In order to avoid all sorts of complications, we do not
18585 allow variably-modified types as template arguments. */
18586 else if (variably_modified_type_p (t, NULL_TREE))
18588 if (complain & tf_error)
18589 error ("%qT is a variably modified type", t);
18590 return true;
18593 /* Class template and alias template arguments should be OK. */
18594 else if (DECL_TYPE_TEMPLATE_P (t))
18596 /* A non-type argument of integral or enumerated type must be a
18597 constant. */
18598 else if (TREE_TYPE (t)
18599 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
18600 && !REFERENCE_REF_P (t)
18601 && !TREE_CONSTANT (t))
18603 if (complain & tf_error)
18604 error ("integral expression %qE is not constant", t);
18605 return true;
18607 return false;
18610 static bool
18611 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
18613 int ix, len = DECL_NTPARMS (tmpl);
18614 bool result = false;
18616 for (ix = 0; ix != len; ix++)
18618 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
18619 result = true;
18621 if (result && (complain & tf_error))
18622 error (" trying to instantiate %qD", tmpl);
18623 return result;
18626 /* We're out of SFINAE context now, so generate diagnostics for the access
18627 errors we saw earlier when instantiating D from TMPL and ARGS. */
18629 static void
18630 recheck_decl_substitution (tree d, tree tmpl, tree args)
18632 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
18633 tree type = TREE_TYPE (pattern);
18634 location_t loc = input_location;
18636 push_access_scope (d);
18637 push_deferring_access_checks (dk_no_deferred);
18638 input_location = DECL_SOURCE_LOCATION (pattern);
18639 tsubst (type, args, tf_warning_or_error, d);
18640 input_location = loc;
18641 pop_deferring_access_checks ();
18642 pop_access_scope (d);
18645 /* Instantiate the indicated variable, function, or alias template TMPL with
18646 the template arguments in TARG_PTR. */
18648 static tree
18649 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
18651 tree targ_ptr = orig_args;
18652 tree fndecl;
18653 tree gen_tmpl;
18654 tree spec;
18655 bool access_ok = true;
18657 if (tmpl == error_mark_node)
18658 return error_mark_node;
18660 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
18662 /* If this function is a clone, handle it specially. */
18663 if (DECL_CLONED_FUNCTION_P (tmpl))
18665 tree spec;
18666 tree clone;
18668 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
18669 DECL_CLONED_FUNCTION. */
18670 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
18671 targ_ptr, complain);
18672 if (spec == error_mark_node)
18673 return error_mark_node;
18675 /* Look for the clone. */
18676 FOR_EACH_CLONE (clone, spec)
18677 if (DECL_NAME (clone) == DECL_NAME (tmpl))
18678 return clone;
18679 /* We should always have found the clone by now. */
18680 gcc_unreachable ();
18681 return NULL_TREE;
18684 if (targ_ptr == error_mark_node)
18685 return error_mark_node;
18687 /* Check to see if we already have this specialization. */
18688 gen_tmpl = most_general_template (tmpl);
18689 if (TMPL_ARGS_DEPTH (targ_ptr)
18690 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
18691 /* targ_ptr only has the innermost template args, so add the outer ones
18692 from tmpl, which could be either a partial instantiation or gen_tmpl (in
18693 the case of a non-dependent call within a template definition). */
18694 targ_ptr = (add_outermost_template_args
18695 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
18696 targ_ptr));
18698 /* It would be nice to avoid hashing here and then again in tsubst_decl,
18699 but it doesn't seem to be on the hot path. */
18700 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
18702 gcc_assert (tmpl == gen_tmpl
18703 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
18704 == spec)
18705 || fndecl == NULL_TREE);
18707 if (spec != NULL_TREE)
18709 if (FNDECL_HAS_ACCESS_ERRORS (spec))
18711 if (complain & tf_error)
18712 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
18713 return error_mark_node;
18715 return spec;
18718 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
18719 complain))
18720 return error_mark_node;
18722 /* We are building a FUNCTION_DECL, during which the access of its
18723 parameters and return types have to be checked. However this
18724 FUNCTION_DECL which is the desired context for access checking
18725 is not built yet. We solve this chicken-and-egg problem by
18726 deferring all checks until we have the FUNCTION_DECL. */
18727 push_deferring_access_checks (dk_deferred);
18729 /* Instantiation of the function happens in the context of the function
18730 template, not the context of the overload resolution we're doing. */
18731 push_to_top_level ();
18732 /* If there are dependent arguments, e.g. because we're doing partial
18733 ordering, make sure processing_template_decl stays set. */
18734 if (uses_template_parms (targ_ptr))
18735 ++processing_template_decl;
18736 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18738 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
18739 complain, gen_tmpl, true);
18740 push_nested_class (ctx);
18743 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
18745 fndecl = NULL_TREE;
18746 if (VAR_P (pattern))
18748 /* We need to determine if we're using a partial or explicit
18749 specialization now, because the type of the variable could be
18750 different. */
18751 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
18752 tree elt = most_specialized_partial_spec (tid, complain);
18753 if (elt == error_mark_node)
18754 pattern = error_mark_node;
18755 else if (elt)
18757 tree partial_tmpl = TREE_VALUE (elt);
18758 tree partial_args = TREE_PURPOSE (elt);
18759 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
18760 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
18764 /* Substitute template parameters to obtain the specialization. */
18765 if (fndecl == NULL_TREE)
18766 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
18767 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18768 pop_nested_class ();
18769 pop_from_top_level ();
18771 if (fndecl == error_mark_node)
18773 pop_deferring_access_checks ();
18774 return error_mark_node;
18777 /* The DECL_TI_TEMPLATE should always be the immediate parent
18778 template, not the most general template. */
18779 DECL_TI_TEMPLATE (fndecl) = tmpl;
18780 DECL_TI_ARGS (fndecl) = targ_ptr;
18782 /* Now we know the specialization, compute access previously
18783 deferred. Do no access control for inheriting constructors,
18784 as we already checked access for the inherited constructor. */
18785 if (!(flag_new_inheriting_ctors
18786 && DECL_INHERITED_CTOR (fndecl)))
18788 push_access_scope (fndecl);
18789 if (!perform_deferred_access_checks (complain))
18790 access_ok = false;
18791 pop_access_scope (fndecl);
18793 pop_deferring_access_checks ();
18795 /* If we've just instantiated the main entry point for a function,
18796 instantiate all the alternate entry points as well. We do this
18797 by cloning the instantiation of the main entry point, not by
18798 instantiating the template clones. */
18799 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
18800 clone_function_decl (fndecl, /*update_methods=*/false);
18802 if (!access_ok)
18804 if (!(complain & tf_error))
18806 /* Remember to reinstantiate when we're out of SFINAE so the user
18807 can see the errors. */
18808 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
18810 return error_mark_node;
18812 return fndecl;
18815 /* Wrapper for instantiate_template_1. */
18817 tree
18818 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
18820 tree ret;
18821 timevar_push (TV_TEMPLATE_INST);
18822 ret = instantiate_template_1 (tmpl, orig_args, complain);
18823 timevar_pop (TV_TEMPLATE_INST);
18824 return ret;
18827 /* Instantiate the alias template TMPL with ARGS. Also push a template
18828 instantiation level, which instantiate_template doesn't do because
18829 functions and variables have sufficient context established by the
18830 callers. */
18832 static tree
18833 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
18835 struct pending_template *old_last_pend = last_pending_template;
18836 struct tinst_level *old_error_tinst = last_error_tinst_level;
18837 if (tmpl == error_mark_node || args == error_mark_node)
18838 return error_mark_node;
18839 tree tinst = build_tree_list (tmpl, args);
18840 if (!push_tinst_level (tinst))
18842 ggc_free (tinst);
18843 return error_mark_node;
18846 args =
18847 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
18848 args, tmpl, complain,
18849 /*require_all_args=*/true,
18850 /*use_default_args=*/true);
18852 tree r = instantiate_template (tmpl, args, complain);
18853 pop_tinst_level ();
18854 /* We can't free this if a pending_template entry or last_error_tinst_level
18855 is pointing at it. */
18856 if (last_pending_template == old_last_pend
18857 && last_error_tinst_level == old_error_tinst)
18858 ggc_free (tinst);
18860 return r;
18863 /* PARM is a template parameter pack for FN. Returns true iff
18864 PARM is used in a deducible way in the argument list of FN. */
18866 static bool
18867 pack_deducible_p (tree parm, tree fn)
18869 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
18870 for (; t; t = TREE_CHAIN (t))
18872 tree type = TREE_VALUE (t);
18873 tree packs;
18874 if (!PACK_EXPANSION_P (type))
18875 continue;
18876 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
18877 packs; packs = TREE_CHAIN (packs))
18878 if (template_args_equal (TREE_VALUE (packs), parm))
18880 /* The template parameter pack is used in a function parameter
18881 pack. If this is the end of the parameter list, the
18882 template parameter pack is deducible. */
18883 if (TREE_CHAIN (t) == void_list_node)
18884 return true;
18885 else
18886 /* Otherwise, not. Well, it could be deduced from
18887 a non-pack parameter, but doing so would end up with
18888 a deduction mismatch, so don't bother. */
18889 return false;
18892 /* The template parameter pack isn't used in any function parameter
18893 packs, but it might be used deeper, e.g. tuple<Args...>. */
18894 return true;
18897 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
18898 NARGS elements of the arguments that are being used when calling
18899 it. TARGS is a vector into which the deduced template arguments
18900 are placed.
18902 Returns either a FUNCTION_DECL for the matching specialization of FN or
18903 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
18904 true, diagnostics will be printed to explain why it failed.
18906 If FN is a conversion operator, or we are trying to produce a specific
18907 specialization, RETURN_TYPE is the return type desired.
18909 The EXPLICIT_TARGS are explicit template arguments provided via a
18910 template-id.
18912 The parameter STRICT is one of:
18914 DEDUCE_CALL:
18915 We are deducing arguments for a function call, as in
18916 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
18917 deducing arguments for a call to the result of a conversion
18918 function template, as in [over.call.object].
18920 DEDUCE_CONV:
18921 We are deducing arguments for a conversion function, as in
18922 [temp.deduct.conv].
18924 DEDUCE_EXACT:
18925 We are deducing arguments when doing an explicit instantiation
18926 as in [temp.explicit], when determining an explicit specialization
18927 as in [temp.expl.spec], or when taking the address of a function
18928 template, as in [temp.deduct.funcaddr]. */
18930 tree
18931 fn_type_unification (tree fn,
18932 tree explicit_targs,
18933 tree targs,
18934 const tree *args,
18935 unsigned int nargs,
18936 tree return_type,
18937 unification_kind_t strict,
18938 int flags,
18939 bool explain_p,
18940 bool decltype_p)
18942 tree parms;
18943 tree fntype;
18944 tree decl = NULL_TREE;
18945 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
18946 bool ok;
18947 static int deduction_depth;
18948 struct pending_template *old_last_pend = last_pending_template;
18949 struct tinst_level *old_error_tinst = last_error_tinst_level;
18951 tree orig_fn = fn;
18952 if (flag_new_inheriting_ctors)
18953 fn = strip_inheriting_ctors (fn);
18955 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
18956 tree tinst;
18957 tree r = error_mark_node;
18959 tree full_targs = targs;
18960 if (TMPL_ARGS_DEPTH (targs)
18961 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
18962 full_targs = (add_outermost_template_args
18963 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
18964 targs));
18966 if (decltype_p)
18967 complain |= tf_decltype;
18969 /* In C++0x, it's possible to have a function template whose type depends
18970 on itself recursively. This is most obvious with decltype, but can also
18971 occur with enumeration scope (c++/48969). So we need to catch infinite
18972 recursion and reject the substitution at deduction time; this function
18973 will return error_mark_node for any repeated substitution.
18975 This also catches excessive recursion such as when f<N> depends on
18976 f<N-1> across all integers, and returns error_mark_node for all the
18977 substitutions back up to the initial one.
18979 This is, of course, not reentrant. */
18980 if (excessive_deduction_depth)
18981 return error_mark_node;
18982 tinst = build_tree_list (fn, NULL_TREE);
18983 ++deduction_depth;
18985 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
18987 fntype = TREE_TYPE (fn);
18988 if (explicit_targs)
18990 /* [temp.deduct]
18992 The specified template arguments must match the template
18993 parameters in kind (i.e., type, nontype, template), and there
18994 must not be more arguments than there are parameters;
18995 otherwise type deduction fails.
18997 Nontype arguments must match the types of the corresponding
18998 nontype template parameters, or must be convertible to the
18999 types of the corresponding nontype parameters as specified in
19000 _temp.arg.nontype_, otherwise type deduction fails.
19002 All references in the function type of the function template
19003 to the corresponding template parameters are replaced by the
19004 specified template argument values. If a substitution in a
19005 template parameter or in the function type of the function
19006 template results in an invalid type, type deduction fails. */
19007 int i, len = TREE_VEC_LENGTH (tparms);
19008 location_t loc = input_location;
19009 bool incomplete = false;
19011 if (explicit_targs == error_mark_node)
19012 goto fail;
19014 if (TMPL_ARGS_DEPTH (explicit_targs)
19015 < TMPL_ARGS_DEPTH (full_targs))
19016 explicit_targs = add_outermost_template_args (full_targs,
19017 explicit_targs);
19019 /* Adjust any explicit template arguments before entering the
19020 substitution context. */
19021 explicit_targs
19022 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
19023 complain,
19024 /*require_all_args=*/false,
19025 /*use_default_args=*/false));
19026 if (explicit_targs == error_mark_node)
19027 goto fail;
19029 /* Substitute the explicit args into the function type. This is
19030 necessary so that, for instance, explicitly declared function
19031 arguments can match null pointed constants. If we were given
19032 an incomplete set of explicit args, we must not do semantic
19033 processing during substitution as we could create partial
19034 instantiations. */
19035 for (i = 0; i < len; i++)
19037 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
19038 bool parameter_pack = false;
19039 tree targ = TREE_VEC_ELT (explicit_targs, i);
19041 /* Dig out the actual parm. */
19042 if (TREE_CODE (parm) == TYPE_DECL
19043 || TREE_CODE (parm) == TEMPLATE_DECL)
19045 parm = TREE_TYPE (parm);
19046 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
19048 else if (TREE_CODE (parm) == PARM_DECL)
19050 parm = DECL_INITIAL (parm);
19051 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
19054 if (!parameter_pack && targ == NULL_TREE)
19055 /* No explicit argument for this template parameter. */
19056 incomplete = true;
19058 if (parameter_pack && pack_deducible_p (parm, fn))
19060 /* Mark the argument pack as "incomplete". We could
19061 still deduce more arguments during unification.
19062 We remove this mark in type_unification_real. */
19063 if (targ)
19065 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
19066 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
19067 = ARGUMENT_PACK_ARGS (targ);
19070 /* We have some incomplete argument packs. */
19071 incomplete = true;
19075 TREE_VALUE (tinst) = explicit_targs;
19076 if (!push_tinst_level (tinst))
19078 excessive_deduction_depth = true;
19079 goto fail;
19081 processing_template_decl += incomplete;
19082 input_location = DECL_SOURCE_LOCATION (fn);
19083 /* Ignore any access checks; we'll see them again in
19084 instantiate_template and they might have the wrong
19085 access path at this point. */
19086 push_deferring_access_checks (dk_deferred);
19087 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
19088 complain | tf_partial | tf_fndecl_type, NULL_TREE);
19089 pop_deferring_access_checks ();
19090 input_location = loc;
19091 processing_template_decl -= incomplete;
19092 pop_tinst_level ();
19094 if (fntype == error_mark_node)
19095 goto fail;
19097 /* Place the explicitly specified arguments in TARGS. */
19098 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
19099 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
19100 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
19103 /* Never do unification on the 'this' parameter. */
19104 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
19106 if (return_type && strict == DEDUCE_CALL)
19108 /* We're deducing for a call to the result of a template conversion
19109 function. The parms we really want are in return_type. */
19110 if (POINTER_TYPE_P (return_type))
19111 return_type = TREE_TYPE (return_type);
19112 parms = TYPE_ARG_TYPES (return_type);
19114 else if (return_type)
19116 tree *new_args;
19118 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
19119 new_args = XALLOCAVEC (tree, nargs + 1);
19120 new_args[0] = return_type;
19121 memcpy (new_args + 1, args, nargs * sizeof (tree));
19122 args = new_args;
19123 ++nargs;
19126 /* We allow incomplete unification without an error message here
19127 because the standard doesn't seem to explicitly prohibit it. Our
19128 callers must be ready to deal with unification failures in any
19129 event. */
19131 TREE_VALUE (tinst) = targs;
19132 /* If we aren't explaining yet, push tinst context so we can see where
19133 any errors (e.g. from class instantiations triggered by instantiation
19134 of default template arguments) come from. If we are explaining, this
19135 context is redundant. */
19136 if (!explain_p && !push_tinst_level (tinst))
19138 excessive_deduction_depth = true;
19139 goto fail;
19142 /* type_unification_real will pass back any access checks from default
19143 template argument substitution. */
19144 vec<deferred_access_check, va_gc> *checks;
19145 checks = NULL;
19147 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19148 full_targs, parms, args, nargs, /*subr=*/0,
19149 strict, flags, &checks, explain_p);
19150 if (!explain_p)
19151 pop_tinst_level ();
19152 if (!ok)
19153 goto fail;
19155 /* Now that we have bindings for all of the template arguments,
19156 ensure that the arguments deduced for the template template
19157 parameters have compatible template parameter lists. We cannot
19158 check this property before we have deduced all template
19159 arguments, because the template parameter types of a template
19160 template parameter might depend on prior template parameters
19161 deduced after the template template parameter. The following
19162 ill-formed example illustrates this issue:
19164 template<typename T, template<T> class C> void f(C<5>, T);
19166 template<int N> struct X {};
19168 void g() {
19169 f(X<5>(), 5l); // error: template argument deduction fails
19172 The template parameter list of 'C' depends on the template type
19173 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
19174 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
19175 time that we deduce 'C'. */
19176 if (!template_template_parm_bindings_ok_p
19177 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
19179 unify_inconsistent_template_template_parameters (explain_p);
19180 goto fail;
19183 /* All is well so far. Now, check:
19185 [temp.deduct]
19187 When all template arguments have been deduced, all uses of
19188 template parameters in nondeduced contexts are replaced with
19189 the corresponding deduced argument values. If the
19190 substitution results in an invalid type, as described above,
19191 type deduction fails. */
19192 TREE_VALUE (tinst) = targs;
19193 if (!push_tinst_level (tinst))
19195 excessive_deduction_depth = true;
19196 goto fail;
19199 /* Also collect access checks from the instantiation. */
19200 reopen_deferring_access_checks (checks);
19202 decl = instantiate_template (fn, targs, complain);
19204 checks = get_deferred_access_checks ();
19205 pop_deferring_access_checks ();
19207 pop_tinst_level ();
19209 if (decl == error_mark_node)
19210 goto fail;
19212 /* Now perform any access checks encountered during substitution. */
19213 push_access_scope (decl);
19214 ok = perform_access_checks (checks, complain);
19215 pop_access_scope (decl);
19216 if (!ok)
19217 goto fail;
19219 /* If we're looking for an exact match, check that what we got
19220 is indeed an exact match. It might not be if some template
19221 parameters are used in non-deduced contexts. But don't check
19222 for an exact match if we have dependent template arguments;
19223 in that case we're doing partial ordering, and we already know
19224 that we have two candidates that will provide the actual type. */
19225 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
19227 tree substed = TREE_TYPE (decl);
19228 unsigned int i;
19230 tree sarg
19231 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
19232 if (return_type)
19233 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
19234 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
19235 if (!same_type_p (args[i], TREE_VALUE (sarg)))
19237 unify_type_mismatch (explain_p, args[i],
19238 TREE_VALUE (sarg));
19239 goto fail;
19243 /* After doing deduction with the inherited constructor, actually return an
19244 instantiation of the inheriting constructor. */
19245 if (orig_fn != fn)
19246 decl = instantiate_template (orig_fn, targs, complain);
19248 r = decl;
19250 fail:
19251 --deduction_depth;
19252 if (excessive_deduction_depth)
19254 if (deduction_depth == 0)
19255 /* Reset once we're all the way out. */
19256 excessive_deduction_depth = false;
19259 /* We can't free this if a pending_template entry or last_error_tinst_level
19260 is pointing at it. */
19261 if (last_pending_template == old_last_pend
19262 && last_error_tinst_level == old_error_tinst)
19263 ggc_free (tinst);
19265 return r;
19268 /* Adjust types before performing type deduction, as described in
19269 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
19270 sections are symmetric. PARM is the type of a function parameter
19271 or the return type of the conversion function. ARG is the type of
19272 the argument passed to the call, or the type of the value
19273 initialized with the result of the conversion function.
19274 ARG_EXPR is the original argument expression, which may be null. */
19276 static int
19277 maybe_adjust_types_for_deduction (unification_kind_t strict,
19278 tree* parm,
19279 tree* arg,
19280 tree arg_expr)
19282 int result = 0;
19284 switch (strict)
19286 case DEDUCE_CALL:
19287 break;
19289 case DEDUCE_CONV:
19290 /* Swap PARM and ARG throughout the remainder of this
19291 function; the handling is precisely symmetric since PARM
19292 will initialize ARG rather than vice versa. */
19293 std::swap (parm, arg);
19294 break;
19296 case DEDUCE_EXACT:
19297 /* Core issue #873: Do the DR606 thing (see below) for these cases,
19298 too, but here handle it by stripping the reference from PARM
19299 rather than by adding it to ARG. */
19300 if (TREE_CODE (*parm) == REFERENCE_TYPE
19301 && TYPE_REF_IS_RVALUE (*parm)
19302 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19303 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19304 && TREE_CODE (*arg) == REFERENCE_TYPE
19305 && !TYPE_REF_IS_RVALUE (*arg))
19306 *parm = TREE_TYPE (*parm);
19307 /* Nothing else to do in this case. */
19308 return 0;
19310 default:
19311 gcc_unreachable ();
19314 if (TREE_CODE (*parm) != REFERENCE_TYPE)
19316 /* [temp.deduct.call]
19318 If P is not a reference type:
19320 --If A is an array type, the pointer type produced by the
19321 array-to-pointer standard conversion (_conv.array_) is
19322 used in place of A for type deduction; otherwise,
19324 --If A is a function type, the pointer type produced by
19325 the function-to-pointer standard conversion
19326 (_conv.func_) is used in place of A for type deduction;
19327 otherwise,
19329 --If A is a cv-qualified type, the top level
19330 cv-qualifiers of A's type are ignored for type
19331 deduction. */
19332 if (TREE_CODE (*arg) == ARRAY_TYPE)
19333 *arg = build_pointer_type (TREE_TYPE (*arg));
19334 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
19335 *arg = build_pointer_type (*arg);
19336 else
19337 *arg = TYPE_MAIN_VARIANT (*arg);
19340 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
19341 reference to a cv-unqualified template parameter that does not represent a
19342 template parameter of a class template (during class template argument
19343 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
19344 an lvalue, the type "lvalue reference to A" is used in place of A for type
19345 deduction. */
19346 if (TREE_CODE (*parm) == REFERENCE_TYPE
19347 && TYPE_REF_IS_RVALUE (*parm)
19348 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19349 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
19350 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19351 && (arg_expr ? lvalue_p (arg_expr)
19352 /* try_one_overload doesn't provide an arg_expr, but
19353 functions are always lvalues. */
19354 : TREE_CODE (*arg) == FUNCTION_TYPE))
19355 *arg = build_reference_type (*arg);
19357 /* [temp.deduct.call]
19359 If P is a cv-qualified type, the top level cv-qualifiers
19360 of P's type are ignored for type deduction. If P is a
19361 reference type, the type referred to by P is used for
19362 type deduction. */
19363 *parm = TYPE_MAIN_VARIANT (*parm);
19364 if (TREE_CODE (*parm) == REFERENCE_TYPE)
19366 *parm = TREE_TYPE (*parm);
19367 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
19370 /* DR 322. For conversion deduction, remove a reference type on parm
19371 too (which has been swapped into ARG). */
19372 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
19373 *arg = TREE_TYPE (*arg);
19375 return result;
19378 /* Subroutine of unify_one_argument. PARM is a function parameter of a
19379 template which does contain any deducible template parameters; check if
19380 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
19381 unify_one_argument. */
19383 static int
19384 check_non_deducible_conversion (tree parm, tree arg, int strict,
19385 int flags, bool explain_p)
19387 tree type;
19389 if (!TYPE_P (arg))
19390 type = TREE_TYPE (arg);
19391 else
19392 type = arg;
19394 if (same_type_p (parm, type))
19395 return unify_success (explain_p);
19397 if (strict == DEDUCE_CONV)
19399 if (can_convert_arg (type, parm, NULL_TREE, flags,
19400 explain_p ? tf_warning_or_error : tf_none))
19401 return unify_success (explain_p);
19403 else if (strict != DEDUCE_EXACT)
19405 if (can_convert_arg (parm, type,
19406 TYPE_P (arg) ? NULL_TREE : arg,
19407 flags, explain_p ? tf_warning_or_error : tf_none))
19408 return unify_success (explain_p);
19411 if (strict == DEDUCE_EXACT)
19412 return unify_type_mismatch (explain_p, parm, arg);
19413 else
19414 return unify_arg_conversion (explain_p, parm, type, arg);
19417 static bool uses_deducible_template_parms (tree type);
19419 /* Returns true iff the expression EXPR is one from which a template
19420 argument can be deduced. In other words, if it's an undecorated
19421 use of a template non-type parameter. */
19423 static bool
19424 deducible_expression (tree expr)
19426 /* Strip implicit conversions. */
19427 while (CONVERT_EXPR_P (expr))
19428 expr = TREE_OPERAND (expr, 0);
19429 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
19432 /* Returns true iff the array domain DOMAIN uses a template parameter in a
19433 deducible way; that is, if it has a max value of <PARM> - 1. */
19435 static bool
19436 deducible_array_bound (tree domain)
19438 if (domain == NULL_TREE)
19439 return false;
19441 tree max = TYPE_MAX_VALUE (domain);
19442 if (TREE_CODE (max) != MINUS_EXPR)
19443 return false;
19445 return deducible_expression (TREE_OPERAND (max, 0));
19448 /* Returns true iff the template arguments ARGS use a template parameter
19449 in a deducible way. */
19451 static bool
19452 deducible_template_args (tree args)
19454 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
19456 bool deducible;
19457 tree elt = TREE_VEC_ELT (args, i);
19458 if (ARGUMENT_PACK_P (elt))
19459 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
19460 else
19462 if (PACK_EXPANSION_P (elt))
19463 elt = PACK_EXPANSION_PATTERN (elt);
19464 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
19465 deducible = true;
19466 else if (TYPE_P (elt))
19467 deducible = uses_deducible_template_parms (elt);
19468 else
19469 deducible = deducible_expression (elt);
19471 if (deducible)
19472 return true;
19474 return false;
19477 /* Returns true iff TYPE contains any deducible references to template
19478 parameters, as per 14.8.2.5. */
19480 static bool
19481 uses_deducible_template_parms (tree type)
19483 if (PACK_EXPANSION_P (type))
19484 type = PACK_EXPANSION_PATTERN (type);
19486 /* T
19487 cv-list T
19488 TT<T>
19489 TT<i>
19490 TT<> */
19491 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
19492 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
19493 return true;
19495 /* T*
19497 T&& */
19498 if (POINTER_TYPE_P (type))
19499 return uses_deducible_template_parms (TREE_TYPE (type));
19501 /* T[integer-constant ]
19502 type [i] */
19503 if (TREE_CODE (type) == ARRAY_TYPE)
19504 return (uses_deducible_template_parms (TREE_TYPE (type))
19505 || deducible_array_bound (TYPE_DOMAIN (type)));
19507 /* T type ::*
19508 type T::*
19509 T T::*
19510 T (type ::*)()
19511 type (T::*)()
19512 type (type ::*)(T)
19513 type (T::*)(T)
19514 T (type ::*)(T)
19515 T (T::*)()
19516 T (T::*)(T) */
19517 if (TYPE_PTRMEM_P (type))
19518 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
19519 || (uses_deducible_template_parms
19520 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
19522 /* template-name <T> (where template-name refers to a class template)
19523 template-name <i> (where template-name refers to a class template) */
19524 if (CLASS_TYPE_P (type)
19525 && CLASSTYPE_TEMPLATE_INFO (type)
19526 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
19527 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
19528 (CLASSTYPE_TI_ARGS (type)));
19530 /* type (T)
19532 T(T) */
19533 if (TREE_CODE (type) == FUNCTION_TYPE
19534 || TREE_CODE (type) == METHOD_TYPE)
19536 if (uses_deducible_template_parms (TREE_TYPE (type)))
19537 return true;
19538 tree parm = TYPE_ARG_TYPES (type);
19539 if (TREE_CODE (type) == METHOD_TYPE)
19540 parm = TREE_CHAIN (parm);
19541 for (; parm; parm = TREE_CHAIN (parm))
19542 if (uses_deducible_template_parms (TREE_VALUE (parm)))
19543 return true;
19546 return false;
19549 /* Subroutine of type_unification_real and unify_pack_expansion to
19550 handle unification of a single P/A pair. Parameters are as
19551 for those functions. */
19553 static int
19554 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
19555 int subr, unification_kind_t strict,
19556 bool explain_p)
19558 tree arg_expr = NULL_TREE;
19559 int arg_strict;
19561 if (arg == error_mark_node || parm == error_mark_node)
19562 return unify_invalid (explain_p);
19563 if (arg == unknown_type_node)
19564 /* We can't deduce anything from this, but we might get all the
19565 template args from other function args. */
19566 return unify_success (explain_p);
19568 /* Implicit conversions (Clause 4) will be performed on a function
19569 argument to convert it to the type of the corresponding function
19570 parameter if the parameter type contains no template-parameters that
19571 participate in template argument deduction. */
19572 if (strict != DEDUCE_EXACT
19573 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
19574 /* For function parameters with no deducible template parameters,
19575 just return. We'll check non-dependent conversions later. */
19576 return unify_success (explain_p);
19578 switch (strict)
19580 case DEDUCE_CALL:
19581 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
19582 | UNIFY_ALLOW_MORE_CV_QUAL
19583 | UNIFY_ALLOW_DERIVED);
19584 break;
19586 case DEDUCE_CONV:
19587 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
19588 break;
19590 case DEDUCE_EXACT:
19591 arg_strict = UNIFY_ALLOW_NONE;
19592 break;
19594 default:
19595 gcc_unreachable ();
19598 /* We only do these transformations if this is the top-level
19599 parameter_type_list in a call or declaration matching; in other
19600 situations (nested function declarators, template argument lists) we
19601 won't be comparing a type to an expression, and we don't do any type
19602 adjustments. */
19603 if (!subr)
19605 if (!TYPE_P (arg))
19607 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
19608 if (type_unknown_p (arg))
19610 /* [temp.deduct.type] A template-argument can be
19611 deduced from a pointer to function or pointer
19612 to member function argument if the set of
19613 overloaded functions does not contain function
19614 templates and at most one of a set of
19615 overloaded functions provides a unique
19616 match. */
19617 resolve_overloaded_unification (tparms, targs, parm,
19618 arg, strict,
19619 arg_strict, explain_p);
19620 /* If a unique match was not found, this is a
19621 non-deduced context, so we still succeed. */
19622 return unify_success (explain_p);
19625 arg_expr = arg;
19626 arg = unlowered_expr_type (arg);
19627 if (arg == error_mark_node)
19628 return unify_invalid (explain_p);
19631 arg_strict |=
19632 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
19634 else
19635 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
19636 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
19637 return unify_template_argument_mismatch (explain_p, parm, arg);
19639 /* For deduction from an init-list we need the actual list. */
19640 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
19641 arg = arg_expr;
19642 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
19645 /* for_each_template_parm callback that always returns 0. */
19647 static int
19648 zero_r (tree, void *)
19650 return 0;
19653 /* for_each_template_parm any_fn callback to handle deduction of a template
19654 type argument from the type of an array bound. */
19656 static int
19657 array_deduction_r (tree t, void *data)
19659 tree_pair_p d = (tree_pair_p)data;
19660 tree &tparms = d->purpose;
19661 tree &targs = d->value;
19663 if (TREE_CODE (t) == ARRAY_TYPE)
19664 if (tree dom = TYPE_DOMAIN (t))
19665 if (tree max = TYPE_MAX_VALUE (dom))
19667 if (TREE_CODE (max) == MINUS_EXPR)
19668 max = TREE_OPERAND (max, 0);
19669 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
19670 unify (tparms, targs, TREE_TYPE (max), size_type_node,
19671 UNIFY_ALLOW_NONE, /*explain*/false);
19674 /* Keep walking. */
19675 return 0;
19678 /* Try to deduce any not-yet-deduced template type arguments from the type of
19679 an array bound. This is handled separately from unify because 14.8.2.5 says
19680 "The type of a type parameter is only deduced from an array bound if it is
19681 not otherwise deduced." */
19683 static void
19684 try_array_deduction (tree tparms, tree targs, tree parm)
19686 tree_pair_s data = { tparms, targs };
19687 hash_set<tree> visited;
19688 for_each_template_parm (parm, zero_r, &data, &visited,
19689 /*nondeduced*/false, array_deduction_r);
19692 /* Most parms like fn_type_unification.
19694 If SUBR is 1, we're being called recursively (to unify the
19695 arguments of a function or method parameter of a function
19696 template).
19698 CHECKS is a pointer to a vector of access checks encountered while
19699 substituting default template arguments. */
19701 static int
19702 type_unification_real (tree tparms,
19703 tree full_targs,
19704 tree xparms,
19705 const tree *xargs,
19706 unsigned int xnargs,
19707 int subr,
19708 unification_kind_t strict,
19709 int flags,
19710 vec<deferred_access_check, va_gc> **checks,
19711 bool explain_p)
19713 tree parm, arg;
19714 int i;
19715 int ntparms = TREE_VEC_LENGTH (tparms);
19716 int saw_undeduced = 0;
19717 tree parms;
19718 const tree *args;
19719 unsigned int nargs;
19720 unsigned int ia;
19722 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
19723 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
19724 gcc_assert (ntparms > 0);
19726 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
19728 /* Reset the number of non-defaulted template arguments contained
19729 in TARGS. */
19730 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
19732 again:
19733 parms = xparms;
19734 args = xargs;
19735 nargs = xnargs;
19737 ia = 0;
19738 while (parms && parms != void_list_node
19739 && ia < nargs)
19741 parm = TREE_VALUE (parms);
19743 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19744 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
19745 /* For a function parameter pack that occurs at the end of the
19746 parameter-declaration-list, the type A of each remaining
19747 argument of the call is compared with the type P of the
19748 declarator-id of the function parameter pack. */
19749 break;
19751 parms = TREE_CHAIN (parms);
19753 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19754 /* For a function parameter pack that does not occur at the
19755 end of the parameter-declaration-list, the type of the
19756 parameter pack is a non-deduced context. */
19757 continue;
19759 arg = args[ia];
19760 ++ia;
19762 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
19763 explain_p))
19764 return 1;
19767 if (parms
19768 && parms != void_list_node
19769 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
19771 /* Unify the remaining arguments with the pack expansion type. */
19772 tree argvec;
19773 tree parmvec = make_tree_vec (1);
19775 /* Allocate a TREE_VEC and copy in all of the arguments */
19776 argvec = make_tree_vec (nargs - ia);
19777 for (i = 0; ia < nargs; ++ia, ++i)
19778 TREE_VEC_ELT (argvec, i) = args[ia];
19780 /* Copy the parameter into parmvec. */
19781 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
19782 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
19783 /*subr=*/subr, explain_p))
19784 return 1;
19786 /* Advance to the end of the list of parameters. */
19787 parms = TREE_CHAIN (parms);
19790 /* Fail if we've reached the end of the parm list, and more args
19791 are present, and the parm list isn't variadic. */
19792 if (ia < nargs && parms == void_list_node)
19793 return unify_too_many_arguments (explain_p, nargs, ia);
19794 /* Fail if parms are left and they don't have default values and
19795 they aren't all deduced as empty packs (c++/57397). This is
19796 consistent with sufficient_parms_p. */
19797 if (parms && parms != void_list_node
19798 && TREE_PURPOSE (parms) == NULL_TREE)
19800 unsigned int count = nargs;
19801 tree p = parms;
19802 bool type_pack_p;
19805 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
19806 if (!type_pack_p)
19807 count++;
19808 p = TREE_CHAIN (p);
19810 while (p && p != void_list_node);
19811 if (count != nargs)
19812 return unify_too_few_arguments (explain_p, ia, count,
19813 type_pack_p);
19816 if (!subr)
19818 tsubst_flags_t complain = (explain_p
19819 ? tf_warning_or_error
19820 : tf_none);
19821 bool tried_array_deduction = (cxx_dialect < cxx17);
19823 for (i = 0; i < ntparms; i++)
19825 tree targ = TREE_VEC_ELT (targs, i);
19826 tree tparm = TREE_VEC_ELT (tparms, i);
19828 /* Clear the "incomplete" flags on all argument packs now so that
19829 substituting them into later default arguments works. */
19830 if (targ && ARGUMENT_PACK_P (targ))
19832 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
19833 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
19836 if (targ || tparm == error_mark_node)
19837 continue;
19838 tparm = TREE_VALUE (tparm);
19840 if (TREE_CODE (tparm) == TYPE_DECL
19841 && !tried_array_deduction)
19843 try_array_deduction (tparms, targs, xparms);
19844 tried_array_deduction = true;
19845 if (TREE_VEC_ELT (targs, i))
19846 continue;
19849 /* If this is an undeduced nontype parameter that depends on
19850 a type parameter, try another pass; its type may have been
19851 deduced from a later argument than the one from which
19852 this parameter can be deduced. */
19853 if (TREE_CODE (tparm) == PARM_DECL
19854 && uses_template_parms (TREE_TYPE (tparm))
19855 && saw_undeduced < 2)
19857 saw_undeduced = 1;
19858 continue;
19861 /* Core issue #226 (C++0x) [temp.deduct]:
19863 If a template argument has not been deduced, its
19864 default template argument, if any, is used.
19866 When we are in C++98 mode, TREE_PURPOSE will either
19867 be NULL_TREE or ERROR_MARK_NODE, so we do not need
19868 to explicitly check cxx_dialect here. */
19869 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
19870 /* OK, there is a default argument. Wait until after the
19871 conversion check to do substitution. */
19872 continue;
19874 /* If the type parameter is a parameter pack, then it will
19875 be deduced to an empty parameter pack. */
19876 if (template_parameter_pack_p (tparm))
19878 tree arg;
19880 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
19882 arg = make_node (NONTYPE_ARGUMENT_PACK);
19883 TREE_CONSTANT (arg) = 1;
19885 else
19886 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
19888 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
19890 TREE_VEC_ELT (targs, i) = arg;
19891 continue;
19894 return unify_parameter_deduction_failure (explain_p, tparm);
19897 /* DR 1391: All parameters have args, now check non-dependent parms for
19898 convertibility. */
19899 if (saw_undeduced < 2)
19900 for (ia = 0, parms = xparms, args = xargs, nargs = xnargs;
19901 parms && parms != void_list_node && ia < nargs; )
19903 parm = TREE_VALUE (parms);
19905 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19906 && (!TREE_CHAIN (parms)
19907 || TREE_CHAIN (parms) == void_list_node))
19908 /* For a function parameter pack that occurs at the end of the
19909 parameter-declaration-list, the type A of each remaining
19910 argument of the call is compared with the type P of the
19911 declarator-id of the function parameter pack. */
19912 break;
19914 parms = TREE_CHAIN (parms);
19916 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19917 /* For a function parameter pack that does not occur at the
19918 end of the parameter-declaration-list, the type of the
19919 parameter pack is a non-deduced context. */
19920 continue;
19922 arg = args[ia];
19923 ++ia;
19925 if (uses_template_parms (parm))
19926 continue;
19927 if (check_non_deducible_conversion (parm, arg, strict, flags,
19928 explain_p))
19929 return 1;
19932 /* Now substitute into the default template arguments. */
19933 for (i = 0; i < ntparms; i++)
19935 tree targ = TREE_VEC_ELT (targs, i);
19936 tree tparm = TREE_VEC_ELT (tparms, i);
19938 if (targ || tparm == error_mark_node)
19939 continue;
19940 tree parm = TREE_VALUE (tparm);
19942 tsubst_flags_t fcomplain = complain;
19943 if (saw_undeduced == 1)
19945 /* When saw_undeduced == 1, substitution into parm and arg might
19946 fail or not replace all template parameters, and that's
19947 fine. */
19948 fcomplain = tf_none;
19949 if (TREE_CODE (parm) == PARM_DECL
19950 && uses_template_parms (TREE_TYPE (parm)))
19951 continue;
19954 tree arg = TREE_PURPOSE (tparm);
19955 reopen_deferring_access_checks (*checks);
19956 location_t save_loc = input_location;
19957 if (DECL_P (parm))
19958 input_location = DECL_SOURCE_LOCATION (parm);
19959 arg = tsubst_template_arg (arg, full_targs, fcomplain, NULL_TREE);
19960 if (arg != error_mark_node && !uses_template_parms (arg))
19961 arg = convert_template_argument (parm, arg, full_targs, complain,
19962 i, NULL_TREE);
19963 else if (saw_undeduced == 1)
19964 arg = NULL_TREE;
19965 else
19966 arg = error_mark_node;
19967 input_location = save_loc;
19968 *checks = get_deferred_access_checks ();
19969 pop_deferring_access_checks ();
19970 if (arg == error_mark_node)
19971 return 1;
19972 else if (arg)
19974 TREE_VEC_ELT (targs, i) = arg;
19975 /* The position of the first default template argument,
19976 is also the number of non-defaulted arguments in TARGS.
19977 Record that. */
19978 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19979 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
19983 if (saw_undeduced++ == 1)
19984 goto again;
19987 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19988 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
19990 return unify_success (explain_p);
19993 /* Subroutine of type_unification_real. Args are like the variables
19994 at the call site. ARG is an overloaded function (or template-id);
19995 we try deducing template args from each of the overloads, and if
19996 only one succeeds, we go with that. Modifies TARGS and returns
19997 true on success. */
19999 static bool
20000 resolve_overloaded_unification (tree tparms,
20001 tree targs,
20002 tree parm,
20003 tree arg,
20004 unification_kind_t strict,
20005 int sub_strict,
20006 bool explain_p)
20008 tree tempargs = copy_node (targs);
20009 int good = 0;
20010 tree goodfn = NULL_TREE;
20011 bool addr_p;
20013 if (TREE_CODE (arg) == ADDR_EXPR)
20015 arg = TREE_OPERAND (arg, 0);
20016 addr_p = true;
20018 else
20019 addr_p = false;
20021 if (TREE_CODE (arg) == COMPONENT_REF)
20022 /* Handle `&x' where `x' is some static or non-static member
20023 function name. */
20024 arg = TREE_OPERAND (arg, 1);
20026 if (TREE_CODE (arg) == OFFSET_REF)
20027 arg = TREE_OPERAND (arg, 1);
20029 /* Strip baselink information. */
20030 if (BASELINK_P (arg))
20031 arg = BASELINK_FUNCTIONS (arg);
20033 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
20035 /* If we got some explicit template args, we need to plug them into
20036 the affected templates before we try to unify, in case the
20037 explicit args will completely resolve the templates in question. */
20039 int ok = 0;
20040 tree expl_subargs = TREE_OPERAND (arg, 1);
20041 arg = TREE_OPERAND (arg, 0);
20043 for (lkp_iterator iter (arg); iter; ++iter)
20045 tree fn = *iter;
20046 tree subargs, elem;
20048 if (TREE_CODE (fn) != TEMPLATE_DECL)
20049 continue;
20051 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
20052 expl_subargs, NULL_TREE, tf_none,
20053 /*require_all_args=*/true,
20054 /*use_default_args=*/true);
20055 if (subargs != error_mark_node
20056 && !any_dependent_template_arguments_p (subargs))
20058 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
20059 if (try_one_overload (tparms, targs, tempargs, parm,
20060 elem, strict, sub_strict, addr_p, explain_p)
20061 && (!goodfn || !same_type_p (goodfn, elem)))
20063 goodfn = elem;
20064 ++good;
20067 else if (subargs)
20068 ++ok;
20070 /* If no templates (or more than one) are fully resolved by the
20071 explicit arguments, this template-id is a non-deduced context; it
20072 could still be OK if we deduce all template arguments for the
20073 enclosing call through other arguments. */
20074 if (good != 1)
20075 good = ok;
20077 else if (TREE_CODE (arg) != OVERLOAD
20078 && TREE_CODE (arg) != FUNCTION_DECL)
20079 /* If ARG is, for example, "(0, &f)" then its type will be unknown
20080 -- but the deduction does not succeed because the expression is
20081 not just the function on its own. */
20082 return false;
20083 else
20084 for (lkp_iterator iter (arg); iter; ++iter)
20086 tree fn = *iter;
20087 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
20088 strict, sub_strict, addr_p, explain_p)
20089 && (!goodfn || !decls_match (goodfn, fn)))
20091 goodfn = fn;
20092 ++good;
20096 /* [temp.deduct.type] A template-argument can be deduced from a pointer
20097 to function or pointer to member function argument if the set of
20098 overloaded functions does not contain function templates and at most
20099 one of a set of overloaded functions provides a unique match.
20101 So if we found multiple possibilities, we return success but don't
20102 deduce anything. */
20104 if (good == 1)
20106 int i = TREE_VEC_LENGTH (targs);
20107 for (; i--; )
20108 if (TREE_VEC_ELT (tempargs, i))
20110 tree old = TREE_VEC_ELT (targs, i);
20111 tree new_ = TREE_VEC_ELT (tempargs, i);
20112 if (new_ && old && ARGUMENT_PACK_P (old)
20113 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
20114 /* Don't forget explicit template arguments in a pack. */
20115 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
20116 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
20117 TREE_VEC_ELT (targs, i) = new_;
20120 if (good)
20121 return true;
20123 return false;
20126 /* Core DR 115: In contexts where deduction is done and fails, or in
20127 contexts where deduction is not done, if a template argument list is
20128 specified and it, along with any default template arguments, identifies
20129 a single function template specialization, then the template-id is an
20130 lvalue for the function template specialization. */
20132 tree
20133 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
20135 tree expr, offset, baselink;
20136 bool addr;
20138 if (!type_unknown_p (orig_expr))
20139 return orig_expr;
20141 expr = orig_expr;
20142 addr = false;
20143 offset = NULL_TREE;
20144 baselink = NULL_TREE;
20146 if (TREE_CODE (expr) == ADDR_EXPR)
20148 expr = TREE_OPERAND (expr, 0);
20149 addr = true;
20151 if (TREE_CODE (expr) == OFFSET_REF)
20153 offset = expr;
20154 expr = TREE_OPERAND (expr, 1);
20156 if (BASELINK_P (expr))
20158 baselink = expr;
20159 expr = BASELINK_FUNCTIONS (expr);
20162 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
20164 int good = 0;
20165 tree goodfn = NULL_TREE;
20167 /* If we got some explicit template args, we need to plug them into
20168 the affected templates before we try to unify, in case the
20169 explicit args will completely resolve the templates in question. */
20171 tree expl_subargs = TREE_OPERAND (expr, 1);
20172 tree arg = TREE_OPERAND (expr, 0);
20173 tree badfn = NULL_TREE;
20174 tree badargs = NULL_TREE;
20176 for (lkp_iterator iter (arg); iter; ++iter)
20178 tree fn = *iter;
20179 tree subargs, elem;
20181 if (TREE_CODE (fn) != TEMPLATE_DECL)
20182 continue;
20184 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
20185 expl_subargs, NULL_TREE, tf_none,
20186 /*require_all_args=*/true,
20187 /*use_default_args=*/true);
20188 if (subargs != error_mark_node
20189 && !any_dependent_template_arguments_p (subargs))
20191 elem = instantiate_template (fn, subargs, tf_none);
20192 if (elem == error_mark_node)
20194 badfn = fn;
20195 badargs = subargs;
20197 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
20199 goodfn = elem;
20200 ++good;
20204 if (good == 1)
20206 mark_used (goodfn);
20207 expr = goodfn;
20208 if (baselink)
20209 expr = build_baselink (BASELINK_BINFO (baselink),
20210 BASELINK_ACCESS_BINFO (baselink),
20211 expr, BASELINK_OPTYPE (baselink));
20212 if (offset)
20214 tree base
20215 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
20216 expr = build_offset_ref (base, expr, addr, complain);
20218 if (addr)
20219 expr = cp_build_addr_expr (expr, complain);
20220 return expr;
20222 else if (good == 0 && badargs && (complain & tf_error))
20223 /* There were no good options and at least one bad one, so let the
20224 user know what the problem is. */
20225 instantiate_template (badfn, badargs, complain);
20227 return orig_expr;
20230 /* Subroutine of resolve_overloaded_unification; does deduction for a single
20231 overload. Fills TARGS with any deduced arguments, or error_mark_node if
20232 different overloads deduce different arguments for a given parm.
20233 ADDR_P is true if the expression for which deduction is being
20234 performed was of the form "& fn" rather than simply "fn".
20236 Returns 1 on success. */
20238 static int
20239 try_one_overload (tree tparms,
20240 tree orig_targs,
20241 tree targs,
20242 tree parm,
20243 tree arg,
20244 unification_kind_t strict,
20245 int sub_strict,
20246 bool addr_p,
20247 bool explain_p)
20249 int nargs;
20250 tree tempargs;
20251 int i;
20253 if (arg == error_mark_node)
20254 return 0;
20256 /* [temp.deduct.type] A template-argument can be deduced from a pointer
20257 to function or pointer to member function argument if the set of
20258 overloaded functions does not contain function templates and at most
20259 one of a set of overloaded functions provides a unique match.
20261 So if this is a template, just return success. */
20263 if (uses_template_parms (arg))
20264 return 1;
20266 if (TREE_CODE (arg) == METHOD_TYPE)
20267 arg = build_ptrmemfunc_type (build_pointer_type (arg));
20268 else if (addr_p)
20269 arg = build_pointer_type (arg);
20271 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
20273 /* We don't copy orig_targs for this because if we have already deduced
20274 some template args from previous args, unify would complain when we
20275 try to deduce a template parameter for the same argument, even though
20276 there isn't really a conflict. */
20277 nargs = TREE_VEC_LENGTH (targs);
20278 tempargs = make_tree_vec (nargs);
20280 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
20281 return 0;
20283 /* First make sure we didn't deduce anything that conflicts with
20284 explicitly specified args. */
20285 for (i = nargs; i--; )
20287 tree elt = TREE_VEC_ELT (tempargs, i);
20288 tree oldelt = TREE_VEC_ELT (orig_targs, i);
20290 if (!elt)
20291 /*NOP*/;
20292 else if (uses_template_parms (elt))
20293 /* Since we're unifying against ourselves, we will fill in
20294 template args used in the function parm list with our own
20295 template parms. Discard them. */
20296 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
20297 else if (oldelt && ARGUMENT_PACK_P (oldelt))
20299 /* Check that the argument at each index of the deduced argument pack
20300 is equivalent to the corresponding explicitly specified argument.
20301 We may have deduced more arguments than were explicitly specified,
20302 and that's OK. */
20304 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
20305 that's wrong if we deduce the same argument pack from multiple
20306 function arguments: it's only incomplete the first time. */
20308 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
20309 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
20311 if (TREE_VEC_LENGTH (deduced_pack)
20312 < TREE_VEC_LENGTH (explicit_pack))
20313 return 0;
20315 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
20316 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
20317 TREE_VEC_ELT (deduced_pack, j)))
20318 return 0;
20320 else if (oldelt && !template_args_equal (oldelt, elt))
20321 return 0;
20324 for (i = nargs; i--; )
20326 tree elt = TREE_VEC_ELT (tempargs, i);
20328 if (elt)
20329 TREE_VEC_ELT (targs, i) = elt;
20332 return 1;
20335 /* PARM is a template class (perhaps with unbound template
20336 parameters). ARG is a fully instantiated type. If ARG can be
20337 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
20338 TARGS are as for unify. */
20340 static tree
20341 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
20342 bool explain_p)
20344 tree copy_of_targs;
20346 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20347 return NULL_TREE;
20348 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20349 /* Matches anything. */;
20350 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
20351 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
20352 return NULL_TREE;
20354 /* We need to make a new template argument vector for the call to
20355 unify. If we used TARGS, we'd clutter it up with the result of
20356 the attempted unification, even if this class didn't work out.
20357 We also don't want to commit ourselves to all the unifications
20358 we've already done, since unification is supposed to be done on
20359 an argument-by-argument basis. In other words, consider the
20360 following pathological case:
20362 template <int I, int J, int K>
20363 struct S {};
20365 template <int I, int J>
20366 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
20368 template <int I, int J, int K>
20369 void f(S<I, J, K>, S<I, I, I>);
20371 void g() {
20372 S<0, 0, 0> s0;
20373 S<0, 1, 2> s2;
20375 f(s0, s2);
20378 Now, by the time we consider the unification involving `s2', we
20379 already know that we must have `f<0, 0, 0>'. But, even though
20380 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
20381 because there are two ways to unify base classes of S<0, 1, 2>
20382 with S<I, I, I>. If we kept the already deduced knowledge, we
20383 would reject the possibility I=1. */
20384 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
20386 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20388 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
20389 return NULL_TREE;
20390 return arg;
20393 /* If unification failed, we're done. */
20394 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
20395 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
20396 return NULL_TREE;
20398 return arg;
20401 /* Given a template type PARM and a class type ARG, find the unique
20402 base type in ARG that is an instance of PARM. We do not examine
20403 ARG itself; only its base-classes. If there is not exactly one
20404 appropriate base class, return NULL_TREE. PARM may be the type of
20405 a partial specialization, as well as a plain template type. Used
20406 by unify. */
20408 static enum template_base_result
20409 get_template_base (tree tparms, tree targs, tree parm, tree arg,
20410 bool explain_p, tree *result)
20412 tree rval = NULL_TREE;
20413 tree binfo;
20415 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
20417 binfo = TYPE_BINFO (complete_type (arg));
20418 if (!binfo)
20420 /* The type could not be completed. */
20421 *result = NULL_TREE;
20422 return tbr_incomplete_type;
20425 /* Walk in inheritance graph order. The search order is not
20426 important, and this avoids multiple walks of virtual bases. */
20427 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
20429 tree r = try_class_unification (tparms, targs, parm,
20430 BINFO_TYPE (binfo), explain_p);
20432 if (r)
20434 /* If there is more than one satisfactory baseclass, then:
20436 [temp.deduct.call]
20438 If they yield more than one possible deduced A, the type
20439 deduction fails.
20441 applies. */
20442 if (rval && !same_type_p (r, rval))
20444 *result = NULL_TREE;
20445 return tbr_ambiguous_baseclass;
20448 rval = r;
20452 *result = rval;
20453 return tbr_success;
20456 /* Returns the level of DECL, which declares a template parameter. */
20458 static int
20459 template_decl_level (tree decl)
20461 switch (TREE_CODE (decl))
20463 case TYPE_DECL:
20464 case TEMPLATE_DECL:
20465 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
20467 case PARM_DECL:
20468 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
20470 default:
20471 gcc_unreachable ();
20473 return 0;
20476 /* Decide whether ARG can be unified with PARM, considering only the
20477 cv-qualifiers of each type, given STRICT as documented for unify.
20478 Returns nonzero iff the unification is OK on that basis. */
20480 static int
20481 check_cv_quals_for_unify (int strict, tree arg, tree parm)
20483 int arg_quals = cp_type_quals (arg);
20484 int parm_quals = cp_type_quals (parm);
20486 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20487 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20489 /* Although a CVR qualifier is ignored when being applied to a
20490 substituted template parameter ([8.3.2]/1 for example), that
20491 does not allow us to unify "const T" with "int&" because both
20492 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
20493 It is ok when we're allowing additional CV qualifiers
20494 at the outer level [14.8.2.1]/3,1st bullet. */
20495 if ((TREE_CODE (arg) == REFERENCE_TYPE
20496 || TREE_CODE (arg) == FUNCTION_TYPE
20497 || TREE_CODE (arg) == METHOD_TYPE)
20498 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
20499 return 0;
20501 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
20502 && (parm_quals & TYPE_QUAL_RESTRICT))
20503 return 0;
20506 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20507 && (arg_quals & parm_quals) != parm_quals)
20508 return 0;
20510 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
20511 && (parm_quals & arg_quals) != arg_quals)
20512 return 0;
20514 return 1;
20517 /* Determines the LEVEL and INDEX for the template parameter PARM. */
20518 void
20519 template_parm_level_and_index (tree parm, int* level, int* index)
20521 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20522 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20523 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20525 *index = TEMPLATE_TYPE_IDX (parm);
20526 *level = TEMPLATE_TYPE_LEVEL (parm);
20528 else
20530 *index = TEMPLATE_PARM_IDX (parm);
20531 *level = TEMPLATE_PARM_LEVEL (parm);
20535 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
20536 do { \
20537 if (unify (TP, TA, P, A, S, EP)) \
20538 return 1; \
20539 } while (0)
20541 /* Unifies the remaining arguments in PACKED_ARGS with the pack
20542 expansion at the end of PACKED_PARMS. Returns 0 if the type
20543 deduction succeeds, 1 otherwise. STRICT is the same as in
20544 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
20545 function call argument list. We'll need to adjust the arguments to make them
20546 types. SUBR tells us if this is from a recursive call to
20547 type_unification_real, or for comparing two template argument
20548 lists. */
20550 static int
20551 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
20552 tree packed_args, unification_kind_t strict,
20553 bool subr, bool explain_p)
20555 tree parm
20556 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
20557 tree pattern = PACK_EXPANSION_PATTERN (parm);
20558 tree pack, packs = NULL_TREE;
20559 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
20561 /* Add in any args remembered from an earlier partial instantiation. */
20562 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
20563 int levels = TMPL_ARGS_DEPTH (targs);
20565 packed_args = expand_template_argument_pack (packed_args);
20567 int len = TREE_VEC_LENGTH (packed_args);
20569 /* Determine the parameter packs we will be deducing from the
20570 pattern, and record their current deductions. */
20571 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
20572 pack; pack = TREE_CHAIN (pack))
20574 tree parm_pack = TREE_VALUE (pack);
20575 int idx, level;
20577 /* Determine the index and level of this parameter pack. */
20578 template_parm_level_and_index (parm_pack, &level, &idx);
20579 if (level < levels)
20580 continue;
20582 /* Keep track of the parameter packs and their corresponding
20583 argument packs. */
20584 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
20585 TREE_TYPE (packs) = make_tree_vec (len - start);
20588 /* Loop through all of the arguments that have not yet been
20589 unified and unify each with the pattern. */
20590 for (i = start; i < len; i++)
20592 tree parm;
20593 bool any_explicit = false;
20594 tree arg = TREE_VEC_ELT (packed_args, i);
20596 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
20597 or the element of its argument pack at the current index if
20598 this argument was explicitly specified. */
20599 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20601 int idx, level;
20602 tree arg, pargs;
20603 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20605 arg = NULL_TREE;
20606 if (TREE_VALUE (pack)
20607 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
20608 && (i - start < TREE_VEC_LENGTH (pargs)))
20610 any_explicit = true;
20611 arg = TREE_VEC_ELT (pargs, i - start);
20613 TMPL_ARG (targs, level, idx) = arg;
20616 /* If we had explicit template arguments, substitute them into the
20617 pattern before deduction. */
20618 if (any_explicit)
20620 /* Some arguments might still be unspecified or dependent. */
20621 bool dependent;
20622 ++processing_template_decl;
20623 dependent = any_dependent_template_arguments_p (targs);
20624 if (!dependent)
20625 --processing_template_decl;
20626 parm = tsubst (pattern, targs,
20627 explain_p ? tf_warning_or_error : tf_none,
20628 NULL_TREE);
20629 if (dependent)
20630 --processing_template_decl;
20631 if (parm == error_mark_node)
20632 return 1;
20634 else
20635 parm = pattern;
20637 /* Unify the pattern with the current argument. */
20638 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
20639 explain_p))
20640 return 1;
20642 /* For each parameter pack, collect the deduced value. */
20643 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20645 int idx, level;
20646 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20648 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
20649 TMPL_ARG (targs, level, idx);
20653 /* Verify that the results of unification with the parameter packs
20654 produce results consistent with what we've seen before, and make
20655 the deduced argument packs available. */
20656 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20658 tree old_pack = TREE_VALUE (pack);
20659 tree new_args = TREE_TYPE (pack);
20660 int i, len = TREE_VEC_LENGTH (new_args);
20661 int idx, level;
20662 bool nondeduced_p = false;
20664 /* By default keep the original deduced argument pack.
20665 If necessary, more specific code is going to update the
20666 resulting deduced argument later down in this function. */
20667 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20668 TMPL_ARG (targs, level, idx) = old_pack;
20670 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
20671 actually deduce anything. */
20672 for (i = 0; i < len && !nondeduced_p; ++i)
20673 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
20674 nondeduced_p = true;
20675 if (nondeduced_p)
20676 continue;
20678 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
20680 /* If we had fewer function args than explicit template args,
20681 just use the explicits. */
20682 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20683 int explicit_len = TREE_VEC_LENGTH (explicit_args);
20684 if (len < explicit_len)
20685 new_args = explicit_args;
20688 if (!old_pack)
20690 tree result;
20691 /* Build the deduced *_ARGUMENT_PACK. */
20692 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
20694 result = make_node (NONTYPE_ARGUMENT_PACK);
20695 TREE_CONSTANT (result) = 1;
20697 else
20698 result = cxx_make_type (TYPE_ARGUMENT_PACK);
20700 SET_ARGUMENT_PACK_ARGS (result, new_args);
20702 /* Note the deduced argument packs for this parameter
20703 pack. */
20704 TMPL_ARG (targs, level, idx) = result;
20706 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
20707 && (ARGUMENT_PACK_ARGS (old_pack)
20708 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
20710 /* We only had the explicitly-provided arguments before, but
20711 now we have a complete set of arguments. */
20712 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20714 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
20715 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
20716 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
20718 else
20720 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
20721 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
20723 if (!comp_template_args (old_args, new_args,
20724 &bad_old_arg, &bad_new_arg))
20725 /* Inconsistent unification of this parameter pack. */
20726 return unify_parameter_pack_inconsistent (explain_p,
20727 bad_old_arg,
20728 bad_new_arg);
20732 return unify_success (explain_p);
20735 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
20736 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
20737 parameters and return value are as for unify. */
20739 static int
20740 unify_array_domain (tree tparms, tree targs,
20741 tree parm_dom, tree arg_dom,
20742 bool explain_p)
20744 tree parm_max;
20745 tree arg_max;
20746 bool parm_cst;
20747 bool arg_cst;
20749 /* Our representation of array types uses "N - 1" as the
20750 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
20751 not an integer constant. We cannot unify arbitrarily
20752 complex expressions, so we eliminate the MINUS_EXPRs
20753 here. */
20754 parm_max = TYPE_MAX_VALUE (parm_dom);
20755 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
20756 if (!parm_cst)
20758 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
20759 parm_max = TREE_OPERAND (parm_max, 0);
20761 arg_max = TYPE_MAX_VALUE (arg_dom);
20762 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
20763 if (!arg_cst)
20765 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
20766 trying to unify the type of a variable with the type
20767 of a template parameter. For example:
20769 template <unsigned int N>
20770 void f (char (&) [N]);
20771 int g();
20772 void h(int i) {
20773 char a[g(i)];
20774 f(a);
20777 Here, the type of the ARG will be "int [g(i)]", and
20778 may be a SAVE_EXPR, etc. */
20779 if (TREE_CODE (arg_max) != MINUS_EXPR)
20780 return unify_vla_arg (explain_p, arg_dom);
20781 arg_max = TREE_OPERAND (arg_max, 0);
20784 /* If only one of the bounds used a MINUS_EXPR, compensate
20785 by adding one to the other bound. */
20786 if (parm_cst && !arg_cst)
20787 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
20788 integer_type_node,
20789 parm_max,
20790 integer_one_node);
20791 else if (arg_cst && !parm_cst)
20792 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
20793 integer_type_node,
20794 arg_max,
20795 integer_one_node);
20797 return unify (tparms, targs, parm_max, arg_max,
20798 UNIFY_ALLOW_INTEGER, explain_p);
20801 /* Returns whether T, a P or A in unify, is a type, template or expression. */
20803 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
20805 static pa_kind_t
20806 pa_kind (tree t)
20808 if (PACK_EXPANSION_P (t))
20809 t = PACK_EXPANSION_PATTERN (t);
20810 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
20811 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
20812 || DECL_TYPE_TEMPLATE_P (t))
20813 return pa_tmpl;
20814 else if (TYPE_P (t))
20815 return pa_type;
20816 else
20817 return pa_expr;
20820 /* Deduce the value of template parameters. TPARMS is the (innermost)
20821 set of template parameters to a template. TARGS is the bindings
20822 for those template parameters, as determined thus far; TARGS may
20823 include template arguments for outer levels of template parameters
20824 as well. PARM is a parameter to a template function, or a
20825 subcomponent of that parameter; ARG is the corresponding argument.
20826 This function attempts to match PARM with ARG in a manner
20827 consistent with the existing assignments in TARGS. If more values
20828 are deduced, then TARGS is updated.
20830 Returns 0 if the type deduction succeeds, 1 otherwise. The
20831 parameter STRICT is a bitwise or of the following flags:
20833 UNIFY_ALLOW_NONE:
20834 Require an exact match between PARM and ARG.
20835 UNIFY_ALLOW_MORE_CV_QUAL:
20836 Allow the deduced ARG to be more cv-qualified (by qualification
20837 conversion) than ARG.
20838 UNIFY_ALLOW_LESS_CV_QUAL:
20839 Allow the deduced ARG to be less cv-qualified than ARG.
20840 UNIFY_ALLOW_DERIVED:
20841 Allow the deduced ARG to be a template base class of ARG,
20842 or a pointer to a template base class of the type pointed to by
20843 ARG.
20844 UNIFY_ALLOW_INTEGER:
20845 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
20846 case for more information.
20847 UNIFY_ALLOW_OUTER_LEVEL:
20848 This is the outermost level of a deduction. Used to determine validity
20849 of qualification conversions. A valid qualification conversion must
20850 have const qualified pointers leading up to the inner type which
20851 requires additional CV quals, except at the outer level, where const
20852 is not required [conv.qual]. It would be normal to set this flag in
20853 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
20854 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
20855 This is the outermost level of a deduction, and PARM can be more CV
20856 qualified at this point.
20857 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
20858 This is the outermost level of a deduction, and PARM can be less CV
20859 qualified at this point. */
20861 static int
20862 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
20863 bool explain_p)
20865 int idx;
20866 tree targ;
20867 tree tparm;
20868 int strict_in = strict;
20869 tsubst_flags_t complain = (explain_p
20870 ? tf_warning_or_error
20871 : tf_none);
20873 /* I don't think this will do the right thing with respect to types.
20874 But the only case I've seen it in so far has been array bounds, where
20875 signedness is the only information lost, and I think that will be
20876 okay. */
20877 while (CONVERT_EXPR_P (parm))
20878 parm = TREE_OPERAND (parm, 0);
20880 if (arg == error_mark_node)
20881 return unify_invalid (explain_p);
20882 if (arg == unknown_type_node
20883 || arg == init_list_type_node)
20884 /* We can't deduce anything from this, but we might get all the
20885 template args from other function args. */
20886 return unify_success (explain_p);
20888 if (parm == any_targ_node || arg == any_targ_node)
20889 return unify_success (explain_p);
20891 /* If PARM uses template parameters, then we can't bail out here,
20892 even if ARG == PARM, since we won't record unifications for the
20893 template parameters. We might need them if we're trying to
20894 figure out which of two things is more specialized. */
20895 if (arg == parm && !uses_template_parms (parm))
20896 return unify_success (explain_p);
20898 /* Handle init lists early, so the rest of the function can assume
20899 we're dealing with a type. */
20900 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
20902 tree elt, elttype;
20903 unsigned i;
20904 tree orig_parm = parm;
20906 /* Replace T with std::initializer_list<T> for deduction. */
20907 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20908 && flag_deduce_init_list)
20909 parm = listify (parm);
20911 if (!is_std_init_list (parm)
20912 && TREE_CODE (parm) != ARRAY_TYPE)
20913 /* We can only deduce from an initializer list argument if the
20914 parameter is std::initializer_list or an array; otherwise this
20915 is a non-deduced context. */
20916 return unify_success (explain_p);
20918 if (TREE_CODE (parm) == ARRAY_TYPE)
20919 elttype = TREE_TYPE (parm);
20920 else
20922 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
20923 /* Deduction is defined in terms of a single type, so just punt
20924 on the (bizarre) std::initializer_list<T...>. */
20925 if (PACK_EXPANSION_P (elttype))
20926 return unify_success (explain_p);
20929 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
20931 int elt_strict = strict;
20933 if (elt == error_mark_node)
20934 return unify_invalid (explain_p);
20936 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
20938 tree type = TREE_TYPE (elt);
20939 if (type == error_mark_node)
20940 return unify_invalid (explain_p);
20941 /* It should only be possible to get here for a call. */
20942 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
20943 elt_strict |= maybe_adjust_types_for_deduction
20944 (DEDUCE_CALL, &elttype, &type, elt);
20945 elt = type;
20948 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
20949 explain_p);
20952 if (TREE_CODE (parm) == ARRAY_TYPE
20953 && deducible_array_bound (TYPE_DOMAIN (parm)))
20955 /* Also deduce from the length of the initializer list. */
20956 tree max = size_int (CONSTRUCTOR_NELTS (arg));
20957 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
20958 if (idx == error_mark_node)
20959 return unify_invalid (explain_p);
20960 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20961 idx, explain_p);
20964 /* If the std::initializer_list<T> deduction worked, replace the
20965 deduced A with std::initializer_list<A>. */
20966 if (orig_parm != parm)
20968 idx = TEMPLATE_TYPE_IDX (orig_parm);
20969 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20970 targ = listify (targ);
20971 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
20973 return unify_success (explain_p);
20976 /* If parm and arg aren't the same kind of thing (template, type, or
20977 expression), fail early. */
20978 if (pa_kind (parm) != pa_kind (arg))
20979 return unify_invalid (explain_p);
20981 /* Immediately reject some pairs that won't unify because of
20982 cv-qualification mismatches. */
20983 if (TREE_CODE (arg) == TREE_CODE (parm)
20984 && TYPE_P (arg)
20985 /* It is the elements of the array which hold the cv quals of an array
20986 type, and the elements might be template type parms. We'll check
20987 when we recurse. */
20988 && TREE_CODE (arg) != ARRAY_TYPE
20989 /* We check the cv-qualifiers when unifying with template type
20990 parameters below. We want to allow ARG `const T' to unify with
20991 PARM `T' for example, when computing which of two templates
20992 is more specialized, for example. */
20993 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
20994 && !check_cv_quals_for_unify (strict_in, arg, parm))
20995 return unify_cv_qual_mismatch (explain_p, parm, arg);
20997 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
20998 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
20999 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
21000 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
21001 strict &= ~UNIFY_ALLOW_DERIVED;
21002 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
21003 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
21005 switch (TREE_CODE (parm))
21007 case TYPENAME_TYPE:
21008 case SCOPE_REF:
21009 case UNBOUND_CLASS_TEMPLATE:
21010 /* In a type which contains a nested-name-specifier, template
21011 argument values cannot be deduced for template parameters used
21012 within the nested-name-specifier. */
21013 return unify_success (explain_p);
21015 case TEMPLATE_TYPE_PARM:
21016 case TEMPLATE_TEMPLATE_PARM:
21017 case BOUND_TEMPLATE_TEMPLATE_PARM:
21018 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
21019 if (error_operand_p (tparm))
21020 return unify_invalid (explain_p);
21022 if (TEMPLATE_TYPE_LEVEL (parm)
21023 != template_decl_level (tparm))
21024 /* The PARM is not one we're trying to unify. Just check
21025 to see if it matches ARG. */
21027 if (TREE_CODE (arg) == TREE_CODE (parm)
21028 && (is_auto (parm) ? is_auto (arg)
21029 : same_type_p (parm, arg)))
21030 return unify_success (explain_p);
21031 else
21032 return unify_type_mismatch (explain_p, parm, arg);
21034 idx = TEMPLATE_TYPE_IDX (parm);
21035 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
21036 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
21037 if (error_operand_p (tparm))
21038 return unify_invalid (explain_p);
21040 /* Check for mixed types and values. */
21041 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
21042 && TREE_CODE (tparm) != TYPE_DECL)
21043 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
21044 && TREE_CODE (tparm) != TEMPLATE_DECL))
21045 gcc_unreachable ();
21047 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21049 if ((strict_in & UNIFY_ALLOW_DERIVED)
21050 && CLASS_TYPE_P (arg))
21052 /* First try to match ARG directly. */
21053 tree t = try_class_unification (tparms, targs, parm, arg,
21054 explain_p);
21055 if (!t)
21057 /* Otherwise, look for a suitable base of ARG, as below. */
21058 enum template_base_result r;
21059 r = get_template_base (tparms, targs, parm, arg,
21060 explain_p, &t);
21061 if (!t)
21062 return unify_no_common_base (explain_p, r, parm, arg);
21063 arg = t;
21066 /* ARG must be constructed from a template class or a template
21067 template parameter. */
21068 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
21069 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
21070 return unify_template_deduction_failure (explain_p, parm, arg);
21072 /* Deduce arguments T, i from TT<T> or TT<i>. */
21073 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
21074 return 1;
21076 arg = TYPE_TI_TEMPLATE (arg);
21078 /* Fall through to deduce template name. */
21081 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
21082 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21084 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
21086 /* Simple cases: Value already set, does match or doesn't. */
21087 if (targ != NULL_TREE && template_args_equal (targ, arg))
21088 return unify_success (explain_p);
21089 else if (targ)
21090 return unify_inconsistency (explain_p, parm, targ, arg);
21092 else
21094 /* If PARM is `const T' and ARG is only `int', we don't have
21095 a match unless we are allowing additional qualification.
21096 If ARG is `const int' and PARM is just `T' that's OK;
21097 that binds `const int' to `T'. */
21098 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
21099 arg, parm))
21100 return unify_cv_qual_mismatch (explain_p, parm, arg);
21102 /* Consider the case where ARG is `const volatile int' and
21103 PARM is `const T'. Then, T should be `volatile int'. */
21104 arg = cp_build_qualified_type_real
21105 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
21106 if (arg == error_mark_node)
21107 return unify_invalid (explain_p);
21109 /* Simple cases: Value already set, does match or doesn't. */
21110 if (targ != NULL_TREE && same_type_p (targ, arg))
21111 return unify_success (explain_p);
21112 else if (targ)
21113 return unify_inconsistency (explain_p, parm, targ, arg);
21115 /* Make sure that ARG is not a variable-sized array. (Note
21116 that were talking about variable-sized arrays (like
21117 `int[n]'), rather than arrays of unknown size (like
21118 `int[]').) We'll get very confused by such a type since
21119 the bound of the array is not constant, and therefore
21120 not mangleable. Besides, such types are not allowed in
21121 ISO C++, so we can do as we please here. We do allow
21122 them for 'auto' deduction, since that isn't ABI-exposed. */
21123 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
21124 return unify_vla_arg (explain_p, arg);
21126 /* Strip typedefs as in convert_template_argument. */
21127 arg = canonicalize_type_argument (arg, tf_none);
21130 /* If ARG is a parameter pack or an expansion, we cannot unify
21131 against it unless PARM is also a parameter pack. */
21132 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
21133 && !template_parameter_pack_p (parm))
21134 return unify_parameter_pack_mismatch (explain_p, parm, arg);
21136 /* If the argument deduction results is a METHOD_TYPE,
21137 then there is a problem.
21138 METHOD_TYPE doesn't map to any real C++ type the result of
21139 the deduction can not be of that type. */
21140 if (TREE_CODE (arg) == METHOD_TYPE)
21141 return unify_method_type_error (explain_p, arg);
21143 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
21144 return unify_success (explain_p);
21146 case TEMPLATE_PARM_INDEX:
21147 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
21148 if (error_operand_p (tparm))
21149 return unify_invalid (explain_p);
21151 if (TEMPLATE_PARM_LEVEL (parm)
21152 != template_decl_level (tparm))
21154 /* The PARM is not one we're trying to unify. Just check
21155 to see if it matches ARG. */
21156 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
21157 && cp_tree_equal (parm, arg));
21158 if (result)
21159 unify_expression_unequal (explain_p, parm, arg);
21160 return result;
21163 idx = TEMPLATE_PARM_IDX (parm);
21164 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
21166 if (targ)
21168 if ((strict & UNIFY_ALLOW_INTEGER)
21169 && TREE_TYPE (targ) && TREE_TYPE (arg)
21170 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
21171 /* We're deducing from an array bound, the type doesn't matter. */
21172 arg = fold_convert (TREE_TYPE (targ), arg);
21173 int x = !cp_tree_equal (targ, arg);
21174 if (x)
21175 unify_inconsistency (explain_p, parm, targ, arg);
21176 return x;
21179 /* [temp.deduct.type] If, in the declaration of a function template
21180 with a non-type template-parameter, the non-type
21181 template-parameter is used in an expression in the function
21182 parameter-list and, if the corresponding template-argument is
21183 deduced, the template-argument type shall match the type of the
21184 template-parameter exactly, except that a template-argument
21185 deduced from an array bound may be of any integral type.
21186 The non-type parameter might use already deduced type parameters. */
21187 ++processing_template_decl;
21188 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
21189 --processing_template_decl;
21190 if (tree a = type_uses_auto (tparm))
21192 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
21193 if (tparm == error_mark_node)
21194 return 1;
21197 if (!TREE_TYPE (arg))
21198 /* Template-parameter dependent expression. Just accept it for now.
21199 It will later be processed in convert_template_argument. */
21201 else if (same_type_p (non_reference (TREE_TYPE (arg)),
21202 non_reference (tparm)))
21203 /* OK */;
21204 else if ((strict & UNIFY_ALLOW_INTEGER)
21205 && CP_INTEGRAL_TYPE_P (tparm))
21206 /* Convert the ARG to the type of PARM; the deduced non-type
21207 template argument must exactly match the types of the
21208 corresponding parameter. */
21209 arg = fold (build_nop (tparm, arg));
21210 else if (uses_template_parms (tparm))
21212 /* We haven't deduced the type of this parameter yet. */
21213 if (cxx_dialect >= cxx17
21214 /* We deduce from array bounds in try_array_deduction. */
21215 && !(strict & UNIFY_ALLOW_INTEGER))
21217 /* Deduce it from the non-type argument. */
21218 tree atype = TREE_TYPE (arg);
21219 RECUR_AND_CHECK_FAILURE (tparms, targs,
21220 tparm, atype,
21221 UNIFY_ALLOW_NONE, explain_p);
21223 else
21224 /* Try again later. */
21225 return unify_success (explain_p);
21227 else
21228 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
21230 /* If ARG is a parameter pack or an expansion, we cannot unify
21231 against it unless PARM is also a parameter pack. */
21232 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
21233 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
21234 return unify_parameter_pack_mismatch (explain_p, parm, arg);
21237 bool removed_attr = false;
21238 arg = strip_typedefs_expr (arg, &removed_attr);
21240 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
21241 return unify_success (explain_p);
21243 case PTRMEM_CST:
21245 /* A pointer-to-member constant can be unified only with
21246 another constant. */
21247 if (TREE_CODE (arg) != PTRMEM_CST)
21248 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
21250 /* Just unify the class member. It would be useless (and possibly
21251 wrong, depending on the strict flags) to unify also
21252 PTRMEM_CST_CLASS, because we want to be sure that both parm and
21253 arg refer to the same variable, even if through different
21254 classes. For instance:
21256 struct A { int x; };
21257 struct B : A { };
21259 Unification of &A::x and &B::x must succeed. */
21260 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
21261 PTRMEM_CST_MEMBER (arg), strict, explain_p);
21264 case POINTER_TYPE:
21266 if (!TYPE_PTR_P (arg))
21267 return unify_type_mismatch (explain_p, parm, arg);
21269 /* [temp.deduct.call]
21271 A can be another pointer or pointer to member type that can
21272 be converted to the deduced A via a qualification
21273 conversion (_conv.qual_).
21275 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
21276 This will allow for additional cv-qualification of the
21277 pointed-to types if appropriate. */
21279 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
21280 /* The derived-to-base conversion only persists through one
21281 level of pointers. */
21282 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
21284 return unify (tparms, targs, TREE_TYPE (parm),
21285 TREE_TYPE (arg), strict, explain_p);
21288 case REFERENCE_TYPE:
21289 if (TREE_CODE (arg) != REFERENCE_TYPE)
21290 return unify_type_mismatch (explain_p, parm, arg);
21291 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21292 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21294 case ARRAY_TYPE:
21295 if (TREE_CODE (arg) != ARRAY_TYPE)
21296 return unify_type_mismatch (explain_p, parm, arg);
21297 if ((TYPE_DOMAIN (parm) == NULL_TREE)
21298 != (TYPE_DOMAIN (arg) == NULL_TREE))
21299 return unify_type_mismatch (explain_p, parm, arg);
21300 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21301 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21302 if (TYPE_DOMAIN (parm) != NULL_TREE)
21303 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
21304 TYPE_DOMAIN (arg), explain_p);
21305 return unify_success (explain_p);
21307 case REAL_TYPE:
21308 case COMPLEX_TYPE:
21309 case VECTOR_TYPE:
21310 case INTEGER_TYPE:
21311 case BOOLEAN_TYPE:
21312 case ENUMERAL_TYPE:
21313 case VOID_TYPE:
21314 case NULLPTR_TYPE:
21315 if (TREE_CODE (arg) != TREE_CODE (parm))
21316 return unify_type_mismatch (explain_p, parm, arg);
21318 /* We have already checked cv-qualification at the top of the
21319 function. */
21320 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
21321 return unify_type_mismatch (explain_p, parm, arg);
21323 /* As far as unification is concerned, this wins. Later checks
21324 will invalidate it if necessary. */
21325 return unify_success (explain_p);
21327 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
21328 /* Type INTEGER_CST can come from ordinary constant template args. */
21329 case INTEGER_CST:
21330 while (CONVERT_EXPR_P (arg))
21331 arg = TREE_OPERAND (arg, 0);
21333 if (TREE_CODE (arg) != INTEGER_CST)
21334 return unify_template_argument_mismatch (explain_p, parm, arg);
21335 return (tree_int_cst_equal (parm, arg)
21336 ? unify_success (explain_p)
21337 : unify_template_argument_mismatch (explain_p, parm, arg));
21339 case TREE_VEC:
21341 int i, len, argslen;
21342 int parm_variadic_p = 0;
21344 if (TREE_CODE (arg) != TREE_VEC)
21345 return unify_template_argument_mismatch (explain_p, parm, arg);
21347 len = TREE_VEC_LENGTH (parm);
21348 argslen = TREE_VEC_LENGTH (arg);
21350 /* Check for pack expansions in the parameters. */
21351 for (i = 0; i < len; ++i)
21353 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
21355 if (i == len - 1)
21356 /* We can unify against something with a trailing
21357 parameter pack. */
21358 parm_variadic_p = 1;
21359 else
21360 /* [temp.deduct.type]/9: If the template argument list of
21361 P contains a pack expansion that is not the last
21362 template argument, the entire template argument list
21363 is a non-deduced context. */
21364 return unify_success (explain_p);
21368 /* If we don't have enough arguments to satisfy the parameters
21369 (not counting the pack expression at the end), or we have
21370 too many arguments for a parameter list that doesn't end in
21371 a pack expression, we can't unify. */
21372 if (parm_variadic_p
21373 ? argslen < len - parm_variadic_p
21374 : argslen != len)
21375 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
21377 /* Unify all of the parameters that precede the (optional)
21378 pack expression. */
21379 for (i = 0; i < len - parm_variadic_p; ++i)
21381 RECUR_AND_CHECK_FAILURE (tparms, targs,
21382 TREE_VEC_ELT (parm, i),
21383 TREE_VEC_ELT (arg, i),
21384 UNIFY_ALLOW_NONE, explain_p);
21386 if (parm_variadic_p)
21387 return unify_pack_expansion (tparms, targs, parm, arg,
21388 DEDUCE_EXACT,
21389 /*subr=*/true, explain_p);
21390 return unify_success (explain_p);
21393 case RECORD_TYPE:
21394 case UNION_TYPE:
21395 if (TREE_CODE (arg) != TREE_CODE (parm))
21396 return unify_type_mismatch (explain_p, parm, arg);
21398 if (TYPE_PTRMEMFUNC_P (parm))
21400 if (!TYPE_PTRMEMFUNC_P (arg))
21401 return unify_type_mismatch (explain_p, parm, arg);
21403 return unify (tparms, targs,
21404 TYPE_PTRMEMFUNC_FN_TYPE (parm),
21405 TYPE_PTRMEMFUNC_FN_TYPE (arg),
21406 strict, explain_p);
21408 else if (TYPE_PTRMEMFUNC_P (arg))
21409 return unify_type_mismatch (explain_p, parm, arg);
21411 if (CLASSTYPE_TEMPLATE_INFO (parm))
21413 tree t = NULL_TREE;
21415 if (strict_in & UNIFY_ALLOW_DERIVED)
21417 /* First, we try to unify the PARM and ARG directly. */
21418 t = try_class_unification (tparms, targs,
21419 parm, arg, explain_p);
21421 if (!t)
21423 /* Fallback to the special case allowed in
21424 [temp.deduct.call]:
21426 If P is a class, and P has the form
21427 template-id, then A can be a derived class of
21428 the deduced A. Likewise, if P is a pointer to
21429 a class of the form template-id, A can be a
21430 pointer to a derived class pointed to by the
21431 deduced A. */
21432 enum template_base_result r;
21433 r = get_template_base (tparms, targs, parm, arg,
21434 explain_p, &t);
21436 if (!t)
21438 /* Don't give the derived diagnostic if we're
21439 already dealing with the same template. */
21440 bool same_template
21441 = (CLASSTYPE_TEMPLATE_INFO (arg)
21442 && (CLASSTYPE_TI_TEMPLATE (parm)
21443 == CLASSTYPE_TI_TEMPLATE (arg)));
21444 return unify_no_common_base (explain_p && !same_template,
21445 r, parm, arg);
21449 else if (CLASSTYPE_TEMPLATE_INFO (arg)
21450 && (CLASSTYPE_TI_TEMPLATE (parm)
21451 == CLASSTYPE_TI_TEMPLATE (arg)))
21452 /* Perhaps PARM is something like S<U> and ARG is S<int>.
21453 Then, we should unify `int' and `U'. */
21454 t = arg;
21455 else
21456 /* There's no chance of unification succeeding. */
21457 return unify_type_mismatch (explain_p, parm, arg);
21459 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
21460 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
21462 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
21463 return unify_type_mismatch (explain_p, parm, arg);
21464 return unify_success (explain_p);
21466 case METHOD_TYPE:
21467 case FUNCTION_TYPE:
21469 unsigned int nargs;
21470 tree *args;
21471 tree a;
21472 unsigned int i;
21474 if (TREE_CODE (arg) != TREE_CODE (parm))
21475 return unify_type_mismatch (explain_p, parm, arg);
21477 /* CV qualifications for methods can never be deduced, they must
21478 match exactly. We need to check them explicitly here,
21479 because type_unification_real treats them as any other
21480 cv-qualified parameter. */
21481 if (TREE_CODE (parm) == METHOD_TYPE
21482 && (!check_cv_quals_for_unify
21483 (UNIFY_ALLOW_NONE,
21484 class_of_this_parm (arg),
21485 class_of_this_parm (parm))))
21486 return unify_cv_qual_mismatch (explain_p, parm, arg);
21487 if (TREE_CODE (arg) == FUNCTION_TYPE
21488 && type_memfn_quals (parm) != type_memfn_quals (arg))
21489 return unify_cv_qual_mismatch (explain_p, parm, arg);
21490 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
21491 return unify_type_mismatch (explain_p, parm, arg);
21493 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
21494 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
21496 nargs = list_length (TYPE_ARG_TYPES (arg));
21497 args = XALLOCAVEC (tree, nargs);
21498 for (a = TYPE_ARG_TYPES (arg), i = 0;
21499 a != NULL_TREE && a != void_list_node;
21500 a = TREE_CHAIN (a), ++i)
21501 args[i] = TREE_VALUE (a);
21502 nargs = i;
21504 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
21505 args, nargs, 1, DEDUCE_EXACT,
21506 LOOKUP_NORMAL, NULL, explain_p))
21507 return 1;
21509 if (flag_noexcept_type)
21511 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
21512 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
21513 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
21514 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
21515 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
21516 && uses_template_parms (TREE_PURPOSE (pspec)))
21517 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
21518 TREE_PURPOSE (aspec),
21519 UNIFY_ALLOW_NONE, explain_p);
21520 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
21521 return unify_type_mismatch (explain_p, parm, arg);
21524 return 0;
21527 case OFFSET_TYPE:
21528 /* Unify a pointer to member with a pointer to member function, which
21529 deduces the type of the member as a function type. */
21530 if (TYPE_PTRMEMFUNC_P (arg))
21532 /* Check top-level cv qualifiers */
21533 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
21534 return unify_cv_qual_mismatch (explain_p, parm, arg);
21536 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21537 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
21538 UNIFY_ALLOW_NONE, explain_p);
21540 /* Determine the type of the function we are unifying against. */
21541 tree fntype = static_fn_type (arg);
21543 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
21546 if (TREE_CODE (arg) != OFFSET_TYPE)
21547 return unify_type_mismatch (explain_p, parm, arg);
21548 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21549 TYPE_OFFSET_BASETYPE (arg),
21550 UNIFY_ALLOW_NONE, explain_p);
21551 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21552 strict, explain_p);
21554 case CONST_DECL:
21555 if (DECL_TEMPLATE_PARM_P (parm))
21556 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
21557 if (arg != scalar_constant_value (parm))
21558 return unify_template_argument_mismatch (explain_p, parm, arg);
21559 return unify_success (explain_p);
21561 case FIELD_DECL:
21562 case TEMPLATE_DECL:
21563 /* Matched cases are handled by the ARG == PARM test above. */
21564 return unify_template_argument_mismatch (explain_p, parm, arg);
21566 case VAR_DECL:
21567 /* We might get a variable as a non-type template argument in parm if the
21568 corresponding parameter is type-dependent. Make any necessary
21569 adjustments based on whether arg is a reference. */
21570 if (CONSTANT_CLASS_P (arg))
21571 parm = fold_non_dependent_expr (parm);
21572 else if (REFERENCE_REF_P (arg))
21574 tree sub = TREE_OPERAND (arg, 0);
21575 STRIP_NOPS (sub);
21576 if (TREE_CODE (sub) == ADDR_EXPR)
21577 arg = TREE_OPERAND (sub, 0);
21579 /* Now use the normal expression code to check whether they match. */
21580 goto expr;
21582 case TYPE_ARGUMENT_PACK:
21583 case NONTYPE_ARGUMENT_PACK:
21584 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
21585 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
21587 case TYPEOF_TYPE:
21588 case DECLTYPE_TYPE:
21589 case UNDERLYING_TYPE:
21590 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
21591 or UNDERLYING_TYPE nodes. */
21592 return unify_success (explain_p);
21594 case ERROR_MARK:
21595 /* Unification fails if we hit an error node. */
21596 return unify_invalid (explain_p);
21598 case INDIRECT_REF:
21599 if (REFERENCE_REF_P (parm))
21601 bool pexp = PACK_EXPANSION_P (arg);
21602 if (pexp)
21603 arg = PACK_EXPANSION_PATTERN (arg);
21604 if (REFERENCE_REF_P (arg))
21605 arg = TREE_OPERAND (arg, 0);
21606 if (pexp)
21607 arg = make_pack_expansion (arg, complain);
21608 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
21609 strict, explain_p);
21611 /* FALLTHRU */
21613 default:
21614 /* An unresolved overload is a nondeduced context. */
21615 if (is_overloaded_fn (parm) || type_unknown_p (parm))
21616 return unify_success (explain_p);
21617 gcc_assert (EXPR_P (parm) || TREE_CODE (parm) == TRAIT_EXPR);
21618 expr:
21619 /* We must be looking at an expression. This can happen with
21620 something like:
21622 template <int I>
21623 void foo(S<I>, S<I + 2>);
21625 This is a "nondeduced context":
21627 [deduct.type]
21629 The nondeduced contexts are:
21631 --A type that is a template-id in which one or more of
21632 the template-arguments is an expression that references
21633 a template-parameter.
21635 In these cases, we assume deduction succeeded, but don't
21636 actually infer any unifications. */
21638 if (!uses_template_parms (parm)
21639 && !template_args_equal (parm, arg))
21640 return unify_expression_unequal (explain_p, parm, arg);
21641 else
21642 return unify_success (explain_p);
21645 #undef RECUR_AND_CHECK_FAILURE
21647 /* Note that DECL can be defined in this translation unit, if
21648 required. */
21650 static void
21651 mark_definable (tree decl)
21653 tree clone;
21654 DECL_NOT_REALLY_EXTERN (decl) = 1;
21655 FOR_EACH_CLONE (clone, decl)
21656 DECL_NOT_REALLY_EXTERN (clone) = 1;
21659 /* Called if RESULT is explicitly instantiated, or is a member of an
21660 explicitly instantiated class. */
21662 void
21663 mark_decl_instantiated (tree result, int extern_p)
21665 SET_DECL_EXPLICIT_INSTANTIATION (result);
21667 /* If this entity has already been written out, it's too late to
21668 make any modifications. */
21669 if (TREE_ASM_WRITTEN (result))
21670 return;
21672 /* For anonymous namespace we don't need to do anything. */
21673 if (decl_anon_ns_mem_p (result))
21675 gcc_assert (!TREE_PUBLIC (result));
21676 return;
21679 if (TREE_CODE (result) != FUNCTION_DECL)
21680 /* The TREE_PUBLIC flag for function declarations will have been
21681 set correctly by tsubst. */
21682 TREE_PUBLIC (result) = 1;
21684 /* This might have been set by an earlier implicit instantiation. */
21685 DECL_COMDAT (result) = 0;
21687 if (extern_p)
21688 DECL_NOT_REALLY_EXTERN (result) = 0;
21689 else
21691 mark_definable (result);
21692 mark_needed (result);
21693 /* Always make artificials weak. */
21694 if (DECL_ARTIFICIAL (result) && flag_weak)
21695 comdat_linkage (result);
21696 /* For WIN32 we also want to put explicit instantiations in
21697 linkonce sections. */
21698 else if (TREE_PUBLIC (result))
21699 maybe_make_one_only (result);
21702 /* If EXTERN_P, then this function will not be emitted -- unless
21703 followed by an explicit instantiation, at which point its linkage
21704 will be adjusted. If !EXTERN_P, then this function will be
21705 emitted here. In neither circumstance do we want
21706 import_export_decl to adjust the linkage. */
21707 DECL_INTERFACE_KNOWN (result) = 1;
21710 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
21711 important template arguments. If any are missing, we check whether
21712 they're important by using error_mark_node for substituting into any
21713 args that were used for partial ordering (the ones between ARGS and END)
21714 and seeing if it bubbles up. */
21716 static bool
21717 check_undeduced_parms (tree targs, tree args, tree end)
21719 bool found = false;
21720 int i;
21721 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
21722 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
21724 found = true;
21725 TREE_VEC_ELT (targs, i) = error_mark_node;
21727 if (found)
21729 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
21730 if (substed == error_mark_node)
21731 return true;
21733 return false;
21736 /* Given two function templates PAT1 and PAT2, return:
21738 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
21739 -1 if PAT2 is more specialized than PAT1.
21740 0 if neither is more specialized.
21742 LEN indicates the number of parameters we should consider
21743 (defaulted parameters should not be considered).
21745 The 1998 std underspecified function template partial ordering, and
21746 DR214 addresses the issue. We take pairs of arguments, one from
21747 each of the templates, and deduce them against each other. One of
21748 the templates will be more specialized if all the *other*
21749 template's arguments deduce against its arguments and at least one
21750 of its arguments *does* *not* deduce against the other template's
21751 corresponding argument. Deduction is done as for class templates.
21752 The arguments used in deduction have reference and top level cv
21753 qualifiers removed. Iff both arguments were originally reference
21754 types *and* deduction succeeds in both directions, an lvalue reference
21755 wins against an rvalue reference and otherwise the template
21756 with the more cv-qualified argument wins for that pairing (if
21757 neither is more cv-qualified, they both are equal). Unlike regular
21758 deduction, after all the arguments have been deduced in this way,
21759 we do *not* verify the deduced template argument values can be
21760 substituted into non-deduced contexts.
21762 The logic can be a bit confusing here, because we look at deduce1 and
21763 targs1 to see if pat2 is at least as specialized, and vice versa; if we
21764 can find template arguments for pat1 to make arg1 look like arg2, that
21765 means that arg2 is at least as specialized as arg1. */
21768 more_specialized_fn (tree pat1, tree pat2, int len)
21770 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
21771 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
21772 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
21773 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
21774 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
21775 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
21776 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
21777 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
21778 tree origs1, origs2;
21779 bool lose1 = false;
21780 bool lose2 = false;
21782 /* Remove the this parameter from non-static member functions. If
21783 one is a non-static member function and the other is not a static
21784 member function, remove the first parameter from that function
21785 also. This situation occurs for operator functions where we
21786 locate both a member function (with this pointer) and non-member
21787 operator (with explicit first operand). */
21788 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
21790 len--; /* LEN is the number of significant arguments for DECL1 */
21791 args1 = TREE_CHAIN (args1);
21792 if (!DECL_STATIC_FUNCTION_P (decl2))
21793 args2 = TREE_CHAIN (args2);
21795 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
21797 args2 = TREE_CHAIN (args2);
21798 if (!DECL_STATIC_FUNCTION_P (decl1))
21800 len--;
21801 args1 = TREE_CHAIN (args1);
21805 /* If only one is a conversion operator, they are unordered. */
21806 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
21807 return 0;
21809 /* Consider the return type for a conversion function */
21810 if (DECL_CONV_FN_P (decl1))
21812 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
21813 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
21814 len++;
21817 processing_template_decl++;
21819 origs1 = args1;
21820 origs2 = args2;
21822 while (len--
21823 /* Stop when an ellipsis is seen. */
21824 && args1 != NULL_TREE && args2 != NULL_TREE)
21826 tree arg1 = TREE_VALUE (args1);
21827 tree arg2 = TREE_VALUE (args2);
21828 int deduce1, deduce2;
21829 int quals1 = -1;
21830 int quals2 = -1;
21831 int ref1 = 0;
21832 int ref2 = 0;
21834 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21835 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21837 /* When both arguments are pack expansions, we need only
21838 unify the patterns themselves. */
21839 arg1 = PACK_EXPANSION_PATTERN (arg1);
21840 arg2 = PACK_EXPANSION_PATTERN (arg2);
21842 /* This is the last comparison we need to do. */
21843 len = 0;
21846 /* DR 1847: If a particular P contains no template-parameters that
21847 participate in template argument deduction, that P is not used to
21848 determine the ordering. */
21849 if (!uses_deducible_template_parms (arg1)
21850 && !uses_deducible_template_parms (arg2))
21851 goto next;
21853 if (TREE_CODE (arg1) == REFERENCE_TYPE)
21855 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
21856 arg1 = TREE_TYPE (arg1);
21857 quals1 = cp_type_quals (arg1);
21860 if (TREE_CODE (arg2) == REFERENCE_TYPE)
21862 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
21863 arg2 = TREE_TYPE (arg2);
21864 quals2 = cp_type_quals (arg2);
21867 arg1 = TYPE_MAIN_VARIANT (arg1);
21868 arg2 = TYPE_MAIN_VARIANT (arg2);
21870 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
21872 int i, len2 = remaining_arguments (args2);
21873 tree parmvec = make_tree_vec (1);
21874 tree argvec = make_tree_vec (len2);
21875 tree ta = args2;
21877 /* Setup the parameter vector, which contains only ARG1. */
21878 TREE_VEC_ELT (parmvec, 0) = arg1;
21880 /* Setup the argument vector, which contains the remaining
21881 arguments. */
21882 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
21883 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21885 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
21886 argvec, DEDUCE_EXACT,
21887 /*subr=*/true, /*explain_p=*/false)
21888 == 0);
21890 /* We cannot deduce in the other direction, because ARG1 is
21891 a pack expansion but ARG2 is not. */
21892 deduce2 = 0;
21894 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21896 int i, len1 = remaining_arguments (args1);
21897 tree parmvec = make_tree_vec (1);
21898 tree argvec = make_tree_vec (len1);
21899 tree ta = args1;
21901 /* Setup the parameter vector, which contains only ARG1. */
21902 TREE_VEC_ELT (parmvec, 0) = arg2;
21904 /* Setup the argument vector, which contains the remaining
21905 arguments. */
21906 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
21907 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21909 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
21910 argvec, DEDUCE_EXACT,
21911 /*subr=*/true, /*explain_p=*/false)
21912 == 0);
21914 /* We cannot deduce in the other direction, because ARG2 is
21915 a pack expansion but ARG1 is not.*/
21916 deduce1 = 0;
21919 else
21921 /* The normal case, where neither argument is a pack
21922 expansion. */
21923 deduce1 = (unify (tparms1, targs1, arg1, arg2,
21924 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21925 == 0);
21926 deduce2 = (unify (tparms2, targs2, arg2, arg1,
21927 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21928 == 0);
21931 /* If we couldn't deduce arguments for tparms1 to make arg1 match
21932 arg2, then arg2 is not as specialized as arg1. */
21933 if (!deduce1)
21934 lose2 = true;
21935 if (!deduce2)
21936 lose1 = true;
21938 /* "If, for a given type, deduction succeeds in both directions
21939 (i.e., the types are identical after the transformations above)
21940 and both P and A were reference types (before being replaced with
21941 the type referred to above):
21942 - if the type from the argument template was an lvalue reference and
21943 the type from the parameter template was not, the argument type is
21944 considered to be more specialized than the other; otherwise,
21945 - if the type from the argument template is more cv-qualified
21946 than the type from the parameter template (as described above),
21947 the argument type is considered to be more specialized than the other;
21948 otherwise,
21949 - neither type is more specialized than the other." */
21951 if (deduce1 && deduce2)
21953 if (ref1 && ref2 && ref1 != ref2)
21955 if (ref1 > ref2)
21956 lose1 = true;
21957 else
21958 lose2 = true;
21960 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
21962 if ((quals1 & quals2) == quals2)
21963 lose2 = true;
21964 if ((quals1 & quals2) == quals1)
21965 lose1 = true;
21969 if (lose1 && lose2)
21970 /* We've failed to deduce something in either direction.
21971 These must be unordered. */
21972 break;
21974 next:
21976 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21977 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21978 /* We have already processed all of the arguments in our
21979 handing of the pack expansion type. */
21980 len = 0;
21982 args1 = TREE_CHAIN (args1);
21983 args2 = TREE_CHAIN (args2);
21986 /* "In most cases, all template parameters must have values in order for
21987 deduction to succeed, but for partial ordering purposes a template
21988 parameter may remain without a value provided it is not used in the
21989 types being used for partial ordering."
21991 Thus, if we are missing any of the targs1 we need to substitute into
21992 origs1, then pat2 is not as specialized as pat1. This can happen when
21993 there is a nondeduced context. */
21994 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
21995 lose2 = true;
21996 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
21997 lose1 = true;
21999 processing_template_decl--;
22001 /* If both deductions succeed, the partial ordering selects the more
22002 constrained template. */
22003 if (!lose1 && !lose2)
22005 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
22006 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
22007 lose1 = !subsumes_constraints (c1, c2);
22008 lose2 = !subsumes_constraints (c2, c1);
22011 /* All things being equal, if the next argument is a pack expansion
22012 for one function but not for the other, prefer the
22013 non-variadic function. FIXME this is bogus; see c++/41958. */
22014 if (lose1 == lose2
22015 && args1 && TREE_VALUE (args1)
22016 && args2 && TREE_VALUE (args2))
22018 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
22019 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
22022 if (lose1 == lose2)
22023 return 0;
22024 else if (!lose1)
22025 return 1;
22026 else
22027 return -1;
22030 /* Determine which of two partial specializations of TMPL is more
22031 specialized.
22033 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
22034 to the first partial specialization. The TREE_PURPOSE is the
22035 innermost set of template parameters for the partial
22036 specialization. PAT2 is similar, but for the second template.
22038 Return 1 if the first partial specialization is more specialized;
22039 -1 if the second is more specialized; 0 if neither is more
22040 specialized.
22042 See [temp.class.order] for information about determining which of
22043 two templates is more specialized. */
22045 static int
22046 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
22048 tree targs;
22049 int winner = 0;
22050 bool any_deductions = false;
22052 tree tmpl1 = TREE_VALUE (pat1);
22053 tree tmpl2 = TREE_VALUE (pat2);
22054 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
22055 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
22057 /* Just like what happens for functions, if we are ordering between
22058 different template specializations, we may encounter dependent
22059 types in the arguments, and we need our dependency check functions
22060 to behave correctly. */
22061 ++processing_template_decl;
22062 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
22063 if (targs)
22065 --winner;
22066 any_deductions = true;
22069 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
22070 if (targs)
22072 ++winner;
22073 any_deductions = true;
22075 --processing_template_decl;
22077 /* If both deductions succeed, the partial ordering selects the more
22078 constrained template. */
22079 if (!winner && any_deductions)
22080 return more_constrained (tmpl1, tmpl2);
22082 /* In the case of a tie where at least one of the templates
22083 has a parameter pack at the end, the template with the most
22084 non-packed parameters wins. */
22085 if (winner == 0
22086 && any_deductions
22087 && (template_args_variadic_p (TREE_PURPOSE (pat1))
22088 || template_args_variadic_p (TREE_PURPOSE (pat2))))
22090 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
22091 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
22092 int len1 = TREE_VEC_LENGTH (args1);
22093 int len2 = TREE_VEC_LENGTH (args2);
22095 /* We don't count the pack expansion at the end. */
22096 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
22097 --len1;
22098 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
22099 --len2;
22101 if (len1 > len2)
22102 return 1;
22103 else if (len1 < len2)
22104 return -1;
22107 return winner;
22110 /* Return the template arguments that will produce the function signature
22111 DECL from the function template FN, with the explicit template
22112 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
22113 also match. Return NULL_TREE if no satisfactory arguments could be
22114 found. */
22116 static tree
22117 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
22119 int ntparms = DECL_NTPARMS (fn);
22120 tree targs = make_tree_vec (ntparms);
22121 tree decl_type = TREE_TYPE (decl);
22122 tree decl_arg_types;
22123 tree *args;
22124 unsigned int nargs, ix;
22125 tree arg;
22127 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
22129 /* Never do unification on the 'this' parameter. */
22130 decl_arg_types = skip_artificial_parms_for (decl,
22131 TYPE_ARG_TYPES (decl_type));
22133 nargs = list_length (decl_arg_types);
22134 args = XALLOCAVEC (tree, nargs);
22135 for (arg = decl_arg_types, ix = 0;
22136 arg != NULL_TREE && arg != void_list_node;
22137 arg = TREE_CHAIN (arg), ++ix)
22138 args[ix] = TREE_VALUE (arg);
22140 if (fn_type_unification (fn, explicit_args, targs,
22141 args, ix,
22142 (check_rettype || DECL_CONV_FN_P (fn)
22143 ? TREE_TYPE (decl_type) : NULL_TREE),
22144 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
22145 /*decltype*/false)
22146 == error_mark_node)
22147 return NULL_TREE;
22149 return targs;
22152 /* Return the innermost template arguments that, when applied to a partial
22153 specialization SPEC_TMPL of TMPL, yield the ARGS.
22155 For example, suppose we have:
22157 template <class T, class U> struct S {};
22158 template <class T> struct S<T*, int> {};
22160 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
22161 partial specialization and the ARGS will be {double*, int}. The resulting
22162 vector will be {double}, indicating that `T' is bound to `double'. */
22164 static tree
22165 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
22167 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
22168 tree spec_args
22169 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
22170 int i, ntparms = TREE_VEC_LENGTH (tparms);
22171 tree deduced_args;
22172 tree innermost_deduced_args;
22174 innermost_deduced_args = make_tree_vec (ntparms);
22175 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
22177 deduced_args = copy_node (args);
22178 SET_TMPL_ARGS_LEVEL (deduced_args,
22179 TMPL_ARGS_DEPTH (deduced_args),
22180 innermost_deduced_args);
22182 else
22183 deduced_args = innermost_deduced_args;
22185 bool tried_array_deduction = (cxx_dialect < cxx17);
22186 again:
22187 if (unify (tparms, deduced_args,
22188 INNERMOST_TEMPLATE_ARGS (spec_args),
22189 INNERMOST_TEMPLATE_ARGS (args),
22190 UNIFY_ALLOW_NONE, /*explain_p=*/false))
22191 return NULL_TREE;
22193 for (i = 0; i < ntparms; ++i)
22194 if (! TREE_VEC_ELT (innermost_deduced_args, i))
22196 if (!tried_array_deduction)
22198 try_array_deduction (tparms, innermost_deduced_args,
22199 INNERMOST_TEMPLATE_ARGS (spec_args));
22200 tried_array_deduction = true;
22201 if (TREE_VEC_ELT (innermost_deduced_args, i))
22202 goto again;
22204 return NULL_TREE;
22207 tree tinst = build_tree_list (spec_tmpl, deduced_args);
22208 if (!push_tinst_level (tinst))
22210 excessive_deduction_depth = true;
22211 return NULL_TREE;
22214 /* Verify that nondeduced template arguments agree with the type
22215 obtained from argument deduction.
22217 For example:
22219 struct A { typedef int X; };
22220 template <class T, class U> struct C {};
22221 template <class T> struct C<T, typename T::X> {};
22223 Then with the instantiation `C<A, int>', we can deduce that
22224 `T' is `A' but unify () does not check whether `typename T::X'
22225 is `int'. */
22226 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
22228 if (spec_args != error_mark_node)
22229 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
22230 INNERMOST_TEMPLATE_ARGS (spec_args),
22231 tmpl, tf_none, false, false);
22233 pop_tinst_level ();
22235 if (spec_args == error_mark_node
22236 /* We only need to check the innermost arguments; the other
22237 arguments will always agree. */
22238 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
22239 INNERMOST_TEMPLATE_ARGS (args)))
22240 return NULL_TREE;
22242 /* Now that we have bindings for all of the template arguments,
22243 ensure that the arguments deduced for the template template
22244 parameters have compatible template parameter lists. See the use
22245 of template_template_parm_bindings_ok_p in fn_type_unification
22246 for more information. */
22247 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
22248 return NULL_TREE;
22250 return deduced_args;
22253 // Compare two function templates T1 and T2 by deducing bindings
22254 // from one against the other. If both deductions succeed, compare
22255 // constraints to see which is more constrained.
22256 static int
22257 more_specialized_inst (tree t1, tree t2)
22259 int fate = 0;
22260 int count = 0;
22262 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
22264 --fate;
22265 ++count;
22268 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
22270 ++fate;
22271 ++count;
22274 // If both deductions succeed, then one may be more constrained.
22275 if (count == 2 && fate == 0)
22276 fate = more_constrained (t1, t2);
22278 return fate;
22281 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
22282 Return the TREE_LIST node with the most specialized template, if
22283 any. If there is no most specialized template, the error_mark_node
22284 is returned.
22286 Note that this function does not look at, or modify, the
22287 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
22288 returned is one of the elements of INSTANTIATIONS, callers may
22289 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
22290 and retrieve it from the value returned. */
22292 tree
22293 most_specialized_instantiation (tree templates)
22295 tree fn, champ;
22297 ++processing_template_decl;
22299 champ = templates;
22300 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
22302 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
22303 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
22304 if (fate == -1)
22305 champ = fn;
22306 else if (!fate)
22308 /* Equally specialized, move to next function. If there
22309 is no next function, nothing's most specialized. */
22310 fn = TREE_CHAIN (fn);
22311 champ = fn;
22312 if (!fn)
22313 break;
22317 if (champ)
22318 /* Now verify that champ is better than everything earlier in the
22319 instantiation list. */
22320 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
22321 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
22323 champ = NULL_TREE;
22324 break;
22328 processing_template_decl--;
22330 if (!champ)
22331 return error_mark_node;
22333 return champ;
22336 /* If DECL is a specialization of some template, return the most
22337 general such template. Otherwise, returns NULL_TREE.
22339 For example, given:
22341 template <class T> struct S { template <class U> void f(U); };
22343 if TMPL is `template <class U> void S<int>::f(U)' this will return
22344 the full template. This function will not trace past partial
22345 specializations, however. For example, given in addition:
22347 template <class T> struct S<T*> { template <class U> void f(U); };
22349 if TMPL is `template <class U> void S<int*>::f(U)' this will return
22350 `template <class T> template <class U> S<T*>::f(U)'. */
22352 tree
22353 most_general_template (tree decl)
22355 if (TREE_CODE (decl) != TEMPLATE_DECL)
22357 if (tree tinfo = get_template_info (decl))
22358 decl = TI_TEMPLATE (tinfo);
22359 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
22360 template friend, or a FIELD_DECL for a capture pack. */
22361 if (TREE_CODE (decl) != TEMPLATE_DECL)
22362 return NULL_TREE;
22365 /* Look for more and more general templates. */
22366 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
22368 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
22369 (See cp-tree.h for details.) */
22370 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
22371 break;
22373 if (CLASS_TYPE_P (TREE_TYPE (decl))
22374 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
22375 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
22376 break;
22378 /* Stop if we run into an explicitly specialized class template. */
22379 if (!DECL_NAMESPACE_SCOPE_P (decl)
22380 && DECL_CONTEXT (decl)
22381 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
22382 break;
22384 decl = DECL_TI_TEMPLATE (decl);
22387 return decl;
22390 /* Return the most specialized of the template partial specializations
22391 which can produce TARGET, a specialization of some class or variable
22392 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
22393 a TEMPLATE_DECL node corresponding to the partial specialization, while
22394 the TREE_PURPOSE is the set of template arguments that must be
22395 substituted into the template pattern in order to generate TARGET.
22397 If the choice of partial specialization is ambiguous, a diagnostic
22398 is issued, and the error_mark_node is returned. If there are no
22399 partial specializations matching TARGET, then NULL_TREE is
22400 returned, indicating that the primary template should be used. */
22402 static tree
22403 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
22405 tree list = NULL_TREE;
22406 tree t;
22407 tree champ;
22408 int fate;
22409 bool ambiguous_p;
22410 tree outer_args = NULL_TREE;
22411 tree tmpl, args;
22413 if (TYPE_P (target))
22415 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
22416 tmpl = TI_TEMPLATE (tinfo);
22417 args = TI_ARGS (tinfo);
22419 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
22421 tmpl = TREE_OPERAND (target, 0);
22422 args = TREE_OPERAND (target, 1);
22424 else if (VAR_P (target))
22426 tree tinfo = DECL_TEMPLATE_INFO (target);
22427 tmpl = TI_TEMPLATE (tinfo);
22428 args = TI_ARGS (tinfo);
22430 else
22431 gcc_unreachable ();
22433 tree main_tmpl = most_general_template (tmpl);
22435 /* For determining which partial specialization to use, only the
22436 innermost args are interesting. */
22437 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
22439 outer_args = strip_innermost_template_args (args, 1);
22440 args = INNERMOST_TEMPLATE_ARGS (args);
22443 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
22445 tree spec_args;
22446 tree spec_tmpl = TREE_VALUE (t);
22448 if (outer_args)
22450 /* Substitute in the template args from the enclosing class. */
22451 ++processing_template_decl;
22452 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
22453 --processing_template_decl;
22456 if (spec_tmpl == error_mark_node)
22457 return error_mark_node;
22459 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
22460 if (spec_args)
22462 if (outer_args)
22463 spec_args = add_to_template_args (outer_args, spec_args);
22465 /* Keep the candidate only if the constraints are satisfied,
22466 or if we're not compiling with concepts. */
22467 if (!flag_concepts
22468 || constraints_satisfied_p (spec_tmpl, spec_args))
22470 list = tree_cons (spec_args, TREE_VALUE (t), list);
22471 TREE_TYPE (list) = TREE_TYPE (t);
22476 if (! list)
22477 return NULL_TREE;
22479 ambiguous_p = false;
22480 t = list;
22481 champ = t;
22482 t = TREE_CHAIN (t);
22483 for (; t; t = TREE_CHAIN (t))
22485 fate = more_specialized_partial_spec (tmpl, champ, t);
22486 if (fate == 1)
22488 else
22490 if (fate == 0)
22492 t = TREE_CHAIN (t);
22493 if (! t)
22495 ambiguous_p = true;
22496 break;
22499 champ = t;
22503 if (!ambiguous_p)
22504 for (t = list; t && t != champ; t = TREE_CHAIN (t))
22506 fate = more_specialized_partial_spec (tmpl, champ, t);
22507 if (fate != 1)
22509 ambiguous_p = true;
22510 break;
22514 if (ambiguous_p)
22516 const char *str;
22517 char *spaces = NULL;
22518 if (!(complain & tf_error))
22519 return error_mark_node;
22520 if (TYPE_P (target))
22521 error ("ambiguous template instantiation for %q#T", target);
22522 else
22523 error ("ambiguous template instantiation for %q#D", target);
22524 str = ngettext ("candidate is:", "candidates are:", list_length (list));
22525 for (t = list; t; t = TREE_CHAIN (t))
22527 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
22528 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
22529 "%s %#qS", spaces ? spaces : str, subst);
22530 spaces = spaces ? spaces : get_spaces (str);
22532 free (spaces);
22533 return error_mark_node;
22536 return champ;
22539 /* Explicitly instantiate DECL. */
22541 void
22542 do_decl_instantiation (tree decl, tree storage)
22544 tree result = NULL_TREE;
22545 int extern_p = 0;
22547 if (!decl || decl == error_mark_node)
22548 /* An error occurred, for which grokdeclarator has already issued
22549 an appropriate message. */
22550 return;
22551 else if (! DECL_LANG_SPECIFIC (decl))
22553 error ("explicit instantiation of non-template %q#D", decl);
22554 return;
22557 bool var_templ = (DECL_TEMPLATE_INFO (decl)
22558 && variable_template_p (DECL_TI_TEMPLATE (decl)));
22560 if (VAR_P (decl) && !var_templ)
22562 /* There is an asymmetry here in the way VAR_DECLs and
22563 FUNCTION_DECLs are handled by grokdeclarator. In the case of
22564 the latter, the DECL we get back will be marked as a
22565 template instantiation, and the appropriate
22566 DECL_TEMPLATE_INFO will be set up. This does not happen for
22567 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
22568 should handle VAR_DECLs as it currently handles
22569 FUNCTION_DECLs. */
22570 if (!DECL_CLASS_SCOPE_P (decl))
22572 error ("%qD is not a static data member of a class template", decl);
22573 return;
22575 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
22576 if (!result || !VAR_P (result))
22578 error ("no matching template for %qD found", decl);
22579 return;
22581 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
22583 error ("type %qT for explicit instantiation %qD does not match "
22584 "declared type %qT", TREE_TYPE (result), decl,
22585 TREE_TYPE (decl));
22586 return;
22589 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
22591 error ("explicit instantiation of %q#D", decl);
22592 return;
22594 else
22595 result = decl;
22597 /* Check for various error cases. Note that if the explicit
22598 instantiation is valid the RESULT will currently be marked as an
22599 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
22600 until we get here. */
22602 if (DECL_TEMPLATE_SPECIALIZATION (result))
22604 /* DR 259 [temp.spec].
22606 Both an explicit instantiation and a declaration of an explicit
22607 specialization shall not appear in a program unless the explicit
22608 instantiation follows a declaration of the explicit specialization.
22610 For a given set of template parameters, if an explicit
22611 instantiation of a template appears after a declaration of an
22612 explicit specialization for that template, the explicit
22613 instantiation has no effect. */
22614 return;
22616 else if (DECL_EXPLICIT_INSTANTIATION (result))
22618 /* [temp.spec]
22620 No program shall explicitly instantiate any template more
22621 than once.
22623 We check DECL_NOT_REALLY_EXTERN so as not to complain when
22624 the first instantiation was `extern' and the second is not,
22625 and EXTERN_P for the opposite case. */
22626 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
22627 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
22628 /* If an "extern" explicit instantiation follows an ordinary
22629 explicit instantiation, the template is instantiated. */
22630 if (extern_p)
22631 return;
22633 else if (!DECL_IMPLICIT_INSTANTIATION (result))
22635 error ("no matching template for %qD found", result);
22636 return;
22638 else if (!DECL_TEMPLATE_INFO (result))
22640 permerror (input_location, "explicit instantiation of non-template %q#D", result);
22641 return;
22644 if (storage == NULL_TREE)
22646 else if (storage == ridpointers[(int) RID_EXTERN])
22648 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
22649 pedwarn (input_location, OPT_Wpedantic,
22650 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
22651 "instantiations");
22652 extern_p = 1;
22654 else
22655 error ("storage class %qD applied to template instantiation", storage);
22657 check_explicit_instantiation_namespace (result);
22658 mark_decl_instantiated (result, extern_p);
22659 if (! extern_p)
22660 instantiate_decl (result, /*defer_ok=*/true,
22661 /*expl_inst_class_mem_p=*/false);
22664 static void
22665 mark_class_instantiated (tree t, int extern_p)
22667 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
22668 SET_CLASSTYPE_INTERFACE_KNOWN (t);
22669 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
22670 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
22671 if (! extern_p)
22673 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
22674 rest_of_type_compilation (t, 1);
22678 /* Called from do_type_instantiation through binding_table_foreach to
22679 do recursive instantiation for the type bound in ENTRY. */
22680 static void
22681 bt_instantiate_type_proc (binding_entry entry, void *data)
22683 tree storage = *(tree *) data;
22685 if (MAYBE_CLASS_TYPE_P (entry->type)
22686 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
22687 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
22690 /* Perform an explicit instantiation of template class T. STORAGE, if
22691 non-null, is the RID for extern, inline or static. COMPLAIN is
22692 nonzero if this is called from the parser, zero if called recursively,
22693 since the standard is unclear (as detailed below). */
22695 void
22696 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
22698 int extern_p = 0;
22699 int nomem_p = 0;
22700 int static_p = 0;
22701 int previous_instantiation_extern_p = 0;
22703 if (TREE_CODE (t) == TYPE_DECL)
22704 t = TREE_TYPE (t);
22706 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
22708 tree tmpl =
22709 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
22710 if (tmpl)
22711 error ("explicit instantiation of non-class template %qD", tmpl);
22712 else
22713 error ("explicit instantiation of non-template type %qT", t);
22714 return;
22717 complete_type (t);
22719 if (!COMPLETE_TYPE_P (t))
22721 if (complain & tf_error)
22722 error ("explicit instantiation of %q#T before definition of template",
22724 return;
22727 if (storage != NULL_TREE)
22729 if (!in_system_header_at (input_location))
22731 if (storage == ridpointers[(int) RID_EXTERN])
22733 if (cxx_dialect == cxx98)
22734 pedwarn (input_location, OPT_Wpedantic,
22735 "ISO C++ 1998 forbids the use of %<extern%> on "
22736 "explicit instantiations");
22738 else
22739 pedwarn (input_location, OPT_Wpedantic,
22740 "ISO C++ forbids the use of %qE"
22741 " on explicit instantiations", storage);
22744 if (storage == ridpointers[(int) RID_INLINE])
22745 nomem_p = 1;
22746 else if (storage == ridpointers[(int) RID_EXTERN])
22747 extern_p = 1;
22748 else if (storage == ridpointers[(int) RID_STATIC])
22749 static_p = 1;
22750 else
22752 error ("storage class %qD applied to template instantiation",
22753 storage);
22754 extern_p = 0;
22758 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
22760 /* DR 259 [temp.spec].
22762 Both an explicit instantiation and a declaration of an explicit
22763 specialization shall not appear in a program unless the explicit
22764 instantiation follows a declaration of the explicit specialization.
22766 For a given set of template parameters, if an explicit
22767 instantiation of a template appears after a declaration of an
22768 explicit specialization for that template, the explicit
22769 instantiation has no effect. */
22770 return;
22772 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
22774 /* [temp.spec]
22776 No program shall explicitly instantiate any template more
22777 than once.
22779 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
22780 instantiation was `extern'. If EXTERN_P then the second is.
22781 These cases are OK. */
22782 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
22784 if (!previous_instantiation_extern_p && !extern_p
22785 && (complain & tf_error))
22786 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
22788 /* If we've already instantiated the template, just return now. */
22789 if (!CLASSTYPE_INTERFACE_ONLY (t))
22790 return;
22793 check_explicit_instantiation_namespace (TYPE_NAME (t));
22794 mark_class_instantiated (t, extern_p);
22796 if (nomem_p)
22797 return;
22799 /* In contrast to implicit instantiation, where only the
22800 declarations, and not the definitions, of members are
22801 instantiated, we have here:
22803 [temp.explicit]
22805 The explicit instantiation of a class template specialization
22806 implies the instantiation of all of its members not
22807 previously explicitly specialized in the translation unit
22808 containing the explicit instantiation.
22810 Of course, we can't instantiate member template classes, since we
22811 don't have any arguments for them. Note that the standard is
22812 unclear on whether the instantiation of the members are
22813 *explicit* instantiations or not. However, the most natural
22814 interpretation is that it should be an explicit
22815 instantiation. */
22816 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
22817 if ((VAR_P (fld)
22818 || (TREE_CODE (fld) == FUNCTION_DECL
22819 && !static_p
22820 && user_provided_p (fld)))
22821 && DECL_TEMPLATE_INSTANTIATION (fld))
22823 mark_decl_instantiated (fld, extern_p);
22824 if (! extern_p)
22825 instantiate_decl (fld, /*defer_ok=*/true,
22826 /*expl_inst_class_mem_p=*/true);
22829 if (CLASSTYPE_NESTED_UTDS (t))
22830 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
22831 bt_instantiate_type_proc, &storage);
22834 /* Given a function DECL, which is a specialization of TMPL, modify
22835 DECL to be a re-instantiation of TMPL with the same template
22836 arguments. TMPL should be the template into which tsubst'ing
22837 should occur for DECL, not the most general template.
22839 One reason for doing this is a scenario like this:
22841 template <class T>
22842 void f(const T&, int i);
22844 void g() { f(3, 7); }
22846 template <class T>
22847 void f(const T& t, const int i) { }
22849 Note that when the template is first instantiated, with
22850 instantiate_template, the resulting DECL will have no name for the
22851 first parameter, and the wrong type for the second. So, when we go
22852 to instantiate the DECL, we regenerate it. */
22854 static void
22855 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
22857 /* The arguments used to instantiate DECL, from the most general
22858 template. */
22859 tree code_pattern;
22861 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
22863 /* Make sure that we can see identifiers, and compute access
22864 correctly. */
22865 push_access_scope (decl);
22867 if (TREE_CODE (decl) == FUNCTION_DECL)
22869 tree decl_parm;
22870 tree pattern_parm;
22871 tree specs;
22872 int args_depth;
22873 int parms_depth;
22875 args_depth = TMPL_ARGS_DEPTH (args);
22876 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
22877 if (args_depth > parms_depth)
22878 args = get_innermost_template_args (args, parms_depth);
22880 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
22881 args, tf_error, NULL_TREE,
22882 /*defer_ok*/false);
22883 if (specs && specs != error_mark_node)
22884 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
22885 specs);
22887 /* Merge parameter declarations. */
22888 decl_parm = skip_artificial_parms_for (decl,
22889 DECL_ARGUMENTS (decl));
22890 pattern_parm
22891 = skip_artificial_parms_for (code_pattern,
22892 DECL_ARGUMENTS (code_pattern));
22893 while (decl_parm && !DECL_PACK_P (pattern_parm))
22895 tree parm_type;
22896 tree attributes;
22898 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22899 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
22900 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
22901 NULL_TREE);
22902 parm_type = type_decays_to (parm_type);
22903 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22904 TREE_TYPE (decl_parm) = parm_type;
22905 attributes = DECL_ATTRIBUTES (pattern_parm);
22906 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22908 DECL_ATTRIBUTES (decl_parm) = attributes;
22909 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22911 decl_parm = DECL_CHAIN (decl_parm);
22912 pattern_parm = DECL_CHAIN (pattern_parm);
22914 /* Merge any parameters that match with the function parameter
22915 pack. */
22916 if (pattern_parm && DECL_PACK_P (pattern_parm))
22918 int i, len;
22919 tree expanded_types;
22920 /* Expand the TYPE_PACK_EXPANSION that provides the types for
22921 the parameters in this function parameter pack. */
22922 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
22923 args, tf_error, NULL_TREE);
22924 len = TREE_VEC_LENGTH (expanded_types);
22925 for (i = 0; i < len; i++)
22927 tree parm_type;
22928 tree attributes;
22930 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22931 /* Rename the parameter to include the index. */
22932 DECL_NAME (decl_parm) =
22933 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
22934 parm_type = TREE_VEC_ELT (expanded_types, i);
22935 parm_type = type_decays_to (parm_type);
22936 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22937 TREE_TYPE (decl_parm) = parm_type;
22938 attributes = DECL_ATTRIBUTES (pattern_parm);
22939 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22941 DECL_ATTRIBUTES (decl_parm) = attributes;
22942 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22944 decl_parm = DECL_CHAIN (decl_parm);
22947 /* Merge additional specifiers from the CODE_PATTERN. */
22948 if (DECL_DECLARED_INLINE_P (code_pattern)
22949 && !DECL_DECLARED_INLINE_P (decl))
22950 DECL_DECLARED_INLINE_P (decl) = 1;
22952 else if (VAR_P (decl))
22954 start_lambda_scope (decl);
22955 DECL_INITIAL (decl) =
22956 tsubst_expr (DECL_INITIAL (code_pattern), args,
22957 tf_error, DECL_TI_TEMPLATE (decl),
22958 /*integral_constant_expression_p=*/false);
22959 finish_lambda_scope ();
22960 if (VAR_HAD_UNKNOWN_BOUND (decl))
22961 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
22962 tf_error, DECL_TI_TEMPLATE (decl));
22964 else
22965 gcc_unreachable ();
22967 pop_access_scope (decl);
22970 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
22971 substituted to get DECL. */
22973 tree
22974 template_for_substitution (tree decl)
22976 tree tmpl = DECL_TI_TEMPLATE (decl);
22978 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
22979 for the instantiation. This is not always the most general
22980 template. Consider, for example:
22982 template <class T>
22983 struct S { template <class U> void f();
22984 template <> void f<int>(); };
22986 and an instantiation of S<double>::f<int>. We want TD to be the
22987 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
22988 while (/* An instantiation cannot have a definition, so we need a
22989 more general template. */
22990 DECL_TEMPLATE_INSTANTIATION (tmpl)
22991 /* We must also deal with friend templates. Given:
22993 template <class T> struct S {
22994 template <class U> friend void f() {};
22997 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
22998 so far as the language is concerned, but that's still
22999 where we get the pattern for the instantiation from. On
23000 other hand, if the definition comes outside the class, say:
23002 template <class T> struct S {
23003 template <class U> friend void f();
23005 template <class U> friend void f() {}
23007 we don't need to look any further. That's what the check for
23008 DECL_INITIAL is for. */
23009 || (TREE_CODE (decl) == FUNCTION_DECL
23010 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
23011 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
23013 /* The present template, TD, should not be a definition. If it
23014 were a definition, we should be using it! Note that we
23015 cannot restructure the loop to just keep going until we find
23016 a template with a definition, since that might go too far if
23017 a specialization was declared, but not defined. */
23019 /* Fetch the more general template. */
23020 tmpl = DECL_TI_TEMPLATE (tmpl);
23023 return tmpl;
23026 /* Returns true if we need to instantiate this template instance even if we
23027 know we aren't going to emit it. */
23029 bool
23030 always_instantiate_p (tree decl)
23032 /* We always instantiate inline functions so that we can inline them. An
23033 explicit instantiation declaration prohibits implicit instantiation of
23034 non-inline functions. With high levels of optimization, we would
23035 normally inline non-inline functions -- but we're not allowed to do
23036 that for "extern template" functions. Therefore, we check
23037 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
23038 return ((TREE_CODE (decl) == FUNCTION_DECL
23039 && (DECL_DECLARED_INLINE_P (decl)
23040 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
23041 /* And we need to instantiate static data members so that
23042 their initializers are available in integral constant
23043 expressions. */
23044 || (VAR_P (decl)
23045 && decl_maybe_constant_var_p (decl)));
23048 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
23049 instantiate it now, modifying TREE_TYPE (fn). Returns false on
23050 error, true otherwise. */
23052 bool
23053 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
23055 tree fntype, spec, noex, clone;
23057 /* Don't instantiate a noexcept-specification from template context. */
23058 if (processing_template_decl)
23059 return true;
23061 if (DECL_CLONED_FUNCTION_P (fn))
23062 fn = DECL_CLONED_FUNCTION (fn);
23063 fntype = TREE_TYPE (fn);
23064 spec = TYPE_RAISES_EXCEPTIONS (fntype);
23066 if (!spec || !TREE_PURPOSE (spec))
23067 return true;
23069 noex = TREE_PURPOSE (spec);
23071 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
23073 static hash_set<tree>* fns = new hash_set<tree>;
23074 bool added = false;
23075 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
23076 spec = get_defaulted_eh_spec (fn, complain);
23077 else if (!(added = !fns->add (fn)))
23079 /* If hash_set::add returns true, the element was already there. */
23080 location_t loc = EXPR_LOC_OR_LOC (DEFERRED_NOEXCEPT_PATTERN (noex),
23081 DECL_SOURCE_LOCATION (fn));
23082 error_at (loc,
23083 "exception specification of %qD depends on itself",
23084 fn);
23085 spec = noexcept_false_spec;
23087 else if (push_tinst_level (fn))
23089 push_access_scope (fn);
23090 push_deferring_access_checks (dk_no_deferred);
23091 input_location = DECL_SOURCE_LOCATION (fn);
23092 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
23093 DEFERRED_NOEXCEPT_ARGS (noex),
23094 tf_warning_or_error, fn,
23095 /*function_p=*/false,
23096 /*integral_constant_expression_p=*/true);
23097 pop_deferring_access_checks ();
23098 pop_access_scope (fn);
23099 pop_tinst_level ();
23100 spec = build_noexcept_spec (noex, tf_warning_or_error);
23101 if (spec == error_mark_node)
23102 spec = noexcept_false_spec;
23104 else
23105 spec = noexcept_false_spec;
23107 if (added)
23108 fns->remove (fn);
23110 if (spec == error_mark_node)
23111 return false;
23113 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
23116 FOR_EACH_CLONE (clone, fn)
23118 if (TREE_TYPE (clone) == fntype)
23119 TREE_TYPE (clone) = TREE_TYPE (fn);
23120 else
23121 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
23124 return true;
23127 /* We're starting to process the function INST, an instantiation of PATTERN;
23128 add their parameters to local_specializations. */
23130 static void
23131 register_parameter_specializations (tree pattern, tree inst)
23133 tree tmpl_parm = DECL_ARGUMENTS (pattern);
23134 tree spec_parm = DECL_ARGUMENTS (inst);
23135 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
23137 register_local_specialization (spec_parm, tmpl_parm);
23138 spec_parm = skip_artificial_parms_for (inst, spec_parm);
23139 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
23141 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
23143 if (!DECL_PACK_P (tmpl_parm))
23145 register_local_specialization (spec_parm, tmpl_parm);
23146 spec_parm = DECL_CHAIN (spec_parm);
23148 else
23150 /* Register the (value) argument pack as a specialization of
23151 TMPL_PARM, then move on. */
23152 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
23153 register_local_specialization (argpack, tmpl_parm);
23156 gcc_assert (!spec_parm);
23159 /* Produce the definition of D, a _DECL generated from a template. If
23160 DEFER_OK is true, then we don't have to actually do the
23161 instantiation now; we just have to do it sometime. Normally it is
23162 an error if this is an explicit instantiation but D is undefined.
23163 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
23164 instantiated class template. */
23166 tree
23167 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
23169 tree tmpl = DECL_TI_TEMPLATE (d);
23170 tree gen_args;
23171 tree args;
23172 tree td;
23173 tree code_pattern;
23174 tree spec;
23175 tree gen_tmpl;
23176 bool pattern_defined;
23177 location_t saved_loc = input_location;
23178 int saved_unevaluated_operand = cp_unevaluated_operand;
23179 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
23180 bool external_p;
23181 bool deleted_p;
23183 /* This function should only be used to instantiate templates for
23184 functions and static member variables. */
23185 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
23187 /* A concept is never instantiated. */
23188 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
23190 /* Variables are never deferred; if instantiation is required, they
23191 are instantiated right away. That allows for better code in the
23192 case that an expression refers to the value of the variable --
23193 if the variable has a constant value the referring expression can
23194 take advantage of that fact. */
23195 if (VAR_P (d))
23196 defer_ok = false;
23198 /* Don't instantiate cloned functions. Instead, instantiate the
23199 functions they cloned. */
23200 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
23201 d = DECL_CLONED_FUNCTION (d);
23203 if (DECL_TEMPLATE_INSTANTIATED (d)
23204 || (TREE_CODE (d) == FUNCTION_DECL
23205 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
23206 || DECL_TEMPLATE_SPECIALIZATION (d))
23207 /* D has already been instantiated or explicitly specialized, so
23208 there's nothing for us to do here.
23210 It might seem reasonable to check whether or not D is an explicit
23211 instantiation, and, if so, stop here. But when an explicit
23212 instantiation is deferred until the end of the compilation,
23213 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
23214 the instantiation. */
23215 return d;
23217 /* Check to see whether we know that this template will be
23218 instantiated in some other file, as with "extern template"
23219 extension. */
23220 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
23222 /* In general, we do not instantiate such templates. */
23223 if (external_p && !always_instantiate_p (d))
23224 return d;
23226 gen_tmpl = most_general_template (tmpl);
23227 gen_args = DECL_TI_ARGS (d);
23229 if (tmpl != gen_tmpl)
23230 /* We should already have the extra args. */
23231 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
23232 == TMPL_ARGS_DEPTH (gen_args));
23233 /* And what's in the hash table should match D. */
23234 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
23235 || spec == NULL_TREE);
23237 /* This needs to happen before any tsubsting. */
23238 if (! push_tinst_level (d))
23239 return d;
23241 timevar_push (TV_TEMPLATE_INST);
23243 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
23244 for the instantiation. */
23245 td = template_for_substitution (d);
23246 args = gen_args;
23248 if (VAR_P (d))
23250 /* Look up an explicit specialization, if any. */
23251 tree tid = lookup_template_variable (gen_tmpl, gen_args);
23252 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
23253 if (elt && elt != error_mark_node)
23255 td = TREE_VALUE (elt);
23256 args = TREE_PURPOSE (elt);
23260 code_pattern = DECL_TEMPLATE_RESULT (td);
23262 /* We should never be trying to instantiate a member of a class
23263 template or partial specialization. */
23264 gcc_assert (d != code_pattern);
23266 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
23267 || DECL_TEMPLATE_SPECIALIZATION (td))
23268 /* In the case of a friend template whose definition is provided
23269 outside the class, we may have too many arguments. Drop the
23270 ones we don't need. The same is true for specializations. */
23271 args = get_innermost_template_args
23272 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
23274 if (TREE_CODE (d) == FUNCTION_DECL)
23276 deleted_p = DECL_DELETED_FN (code_pattern);
23277 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
23278 && DECL_INITIAL (code_pattern) != error_mark_node)
23279 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
23280 || deleted_p);
23282 else
23284 deleted_p = false;
23285 if (DECL_CLASS_SCOPE_P (code_pattern))
23286 pattern_defined = (! DECL_IN_AGGR_P (code_pattern)
23287 || DECL_INLINE_VAR_P (code_pattern));
23288 else
23289 pattern_defined = ! DECL_EXTERNAL (code_pattern);
23292 /* We may be in the middle of deferred access check. Disable it now. */
23293 push_deferring_access_checks (dk_no_deferred);
23295 /* Unless an explicit instantiation directive has already determined
23296 the linkage of D, remember that a definition is available for
23297 this entity. */
23298 if (pattern_defined
23299 && !DECL_INTERFACE_KNOWN (d)
23300 && !DECL_NOT_REALLY_EXTERN (d))
23301 mark_definable (d);
23303 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
23304 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
23305 input_location = DECL_SOURCE_LOCATION (d);
23307 /* If D is a member of an explicitly instantiated class template,
23308 and no definition is available, treat it like an implicit
23309 instantiation. */
23310 if (!pattern_defined && expl_inst_class_mem_p
23311 && DECL_EXPLICIT_INSTANTIATION (d))
23313 /* Leave linkage flags alone on instantiations with anonymous
23314 visibility. */
23315 if (TREE_PUBLIC (d))
23317 DECL_NOT_REALLY_EXTERN (d) = 0;
23318 DECL_INTERFACE_KNOWN (d) = 0;
23320 SET_DECL_IMPLICIT_INSTANTIATION (d);
23323 /* Defer all other templates, unless we have been explicitly
23324 forbidden from doing so. */
23325 if (/* If there is no definition, we cannot instantiate the
23326 template. */
23327 ! pattern_defined
23328 /* If it's OK to postpone instantiation, do so. */
23329 || defer_ok
23330 /* If this is a static data member that will be defined
23331 elsewhere, we don't want to instantiate the entire data
23332 member, but we do want to instantiate the initializer so that
23333 we can substitute that elsewhere. */
23334 || (external_p && VAR_P (d))
23335 /* Handle here a deleted function too, avoid generating
23336 its body (c++/61080). */
23337 || deleted_p)
23339 /* The definition of the static data member is now required so
23340 we must substitute the initializer. */
23341 if (VAR_P (d)
23342 && !DECL_INITIAL (d)
23343 && DECL_INITIAL (code_pattern))
23345 tree ns;
23346 tree init;
23347 bool const_init = false;
23348 bool enter_context = DECL_CLASS_SCOPE_P (d);
23350 ns = decl_namespace_context (d);
23351 push_nested_namespace (ns);
23352 if (enter_context)
23353 push_nested_class (DECL_CONTEXT (d));
23354 init = tsubst_expr (DECL_INITIAL (code_pattern),
23355 args,
23356 tf_warning_or_error, NULL_TREE,
23357 /*integral_constant_expression_p=*/false);
23358 /* If instantiating the initializer involved instantiating this
23359 again, don't call cp_finish_decl twice. */
23360 if (!DECL_INITIAL (d))
23362 /* Make sure the initializer is still constant, in case of
23363 circular dependency (template/instantiate6.C). */
23364 const_init
23365 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
23366 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
23367 /*asmspec_tree=*/NULL_TREE,
23368 LOOKUP_ONLYCONVERTING);
23370 if (enter_context)
23371 pop_nested_class ();
23372 pop_nested_namespace (ns);
23375 /* We restore the source position here because it's used by
23376 add_pending_template. */
23377 input_location = saved_loc;
23379 if (at_eof && !pattern_defined
23380 && DECL_EXPLICIT_INSTANTIATION (d)
23381 && DECL_NOT_REALLY_EXTERN (d))
23382 /* [temp.explicit]
23384 The definition of a non-exported function template, a
23385 non-exported member function template, or a non-exported
23386 member function or static data member of a class template
23387 shall be present in every translation unit in which it is
23388 explicitly instantiated. */
23389 permerror (input_location, "explicit instantiation of %qD "
23390 "but no definition available", d);
23392 /* If we're in unevaluated context, we just wanted to get the
23393 constant value; this isn't an odr use, so don't queue
23394 a full instantiation. */
23395 if (cp_unevaluated_operand != 0)
23396 goto out;
23397 /* ??? Historically, we have instantiated inline functions, even
23398 when marked as "extern template". */
23399 if (!(external_p && VAR_P (d)))
23400 add_pending_template (d);
23401 goto out;
23403 /* Tell the repository that D is available in this translation unit
23404 -- and see if it is supposed to be instantiated here. */
23405 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
23407 /* In a PCH file, despite the fact that the repository hasn't
23408 requested instantiation in the PCH it is still possible that
23409 an instantiation will be required in a file that includes the
23410 PCH. */
23411 if (pch_file)
23412 add_pending_template (d);
23413 /* Instantiate inline functions so that the inliner can do its
23414 job, even though we'll not be emitting a copy of this
23415 function. */
23416 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
23417 goto out;
23420 bool push_to_top, nested;
23421 tree fn_context;
23422 fn_context = decl_function_context (d);
23423 nested = current_function_decl != NULL_TREE;
23424 push_to_top = !(nested && fn_context == current_function_decl);
23426 vec<tree> omp_privatization_save;
23427 if (nested)
23428 save_omp_privatization_clauses (omp_privatization_save);
23430 if (push_to_top)
23431 push_to_top_level ();
23432 else
23434 push_function_context ();
23435 cp_unevaluated_operand = 0;
23436 c_inhibit_evaluation_warnings = 0;
23439 /* Mark D as instantiated so that recursive calls to
23440 instantiate_decl do not try to instantiate it again. */
23441 DECL_TEMPLATE_INSTANTIATED (d) = 1;
23443 /* Regenerate the declaration in case the template has been modified
23444 by a subsequent redeclaration. */
23445 regenerate_decl_from_template (d, td, args);
23447 /* We already set the file and line above. Reset them now in case
23448 they changed as a result of calling regenerate_decl_from_template. */
23449 input_location = DECL_SOURCE_LOCATION (d);
23451 if (VAR_P (d))
23453 tree init;
23454 bool const_init = false;
23456 /* Clear out DECL_RTL; whatever was there before may not be right
23457 since we've reset the type of the declaration. */
23458 SET_DECL_RTL (d, NULL);
23459 DECL_IN_AGGR_P (d) = 0;
23461 /* The initializer is placed in DECL_INITIAL by
23462 regenerate_decl_from_template so we don't need to
23463 push/pop_access_scope again here. Pull it out so that
23464 cp_finish_decl can process it. */
23465 init = DECL_INITIAL (d);
23466 DECL_INITIAL (d) = NULL_TREE;
23467 DECL_INITIALIZED_P (d) = 0;
23469 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
23470 initializer. That function will defer actual emission until
23471 we have a chance to determine linkage. */
23472 DECL_EXTERNAL (d) = 0;
23474 /* Enter the scope of D so that access-checking works correctly. */
23475 bool enter_context = DECL_CLASS_SCOPE_P (d);
23476 if (enter_context)
23477 push_nested_class (DECL_CONTEXT (d));
23479 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
23480 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
23482 if (enter_context)
23483 pop_nested_class ();
23485 if (variable_template_p (gen_tmpl))
23486 note_variable_template_instantiation (d);
23488 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
23489 synthesize_method (d);
23490 else if (TREE_CODE (d) == FUNCTION_DECL)
23492 /* Set up the list of local specializations. */
23493 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
23494 tree block = NULL_TREE;
23496 /* Set up context. */
23497 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23498 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23499 block = push_stmt_list ();
23500 else
23501 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
23503 /* Some typedefs referenced from within the template code need to be
23504 access checked at template instantiation time, i.e now. These
23505 types were added to the template at parsing time. Let's get those
23506 and perform the access checks then. */
23507 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
23508 args);
23510 /* Create substitution entries for the parameters. */
23511 register_parameter_specializations (code_pattern, d);
23513 /* Substitute into the body of the function. */
23514 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23515 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
23516 tf_warning_or_error, tmpl);
23517 else
23519 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
23520 tf_warning_or_error, tmpl,
23521 /*integral_constant_expression_p=*/false);
23523 /* Set the current input_location to the end of the function
23524 so that finish_function knows where we are. */
23525 input_location
23526 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
23528 /* Remember if we saw an infinite loop in the template. */
23529 current_function_infinite_loop
23530 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
23533 /* Finish the function. */
23534 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23535 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23536 DECL_SAVED_TREE (d) = pop_stmt_list (block);
23537 else
23539 d = finish_function (/*inline_p=*/false);
23540 expand_or_defer_fn (d);
23543 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23544 cp_check_omp_declare_reduction (d);
23547 /* We're not deferring instantiation any more. */
23548 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
23550 if (push_to_top)
23551 pop_from_top_level ();
23552 else
23553 pop_function_context ();
23555 if (nested)
23556 restore_omp_privatization_clauses (omp_privatization_save);
23558 out:
23559 pop_deferring_access_checks ();
23560 timevar_pop (TV_TEMPLATE_INST);
23561 pop_tinst_level ();
23562 input_location = saved_loc;
23563 cp_unevaluated_operand = saved_unevaluated_operand;
23564 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23566 return d;
23569 /* Run through the list of templates that we wish we could
23570 instantiate, and instantiate any we can. RETRIES is the
23571 number of times we retry pending template instantiation. */
23573 void
23574 instantiate_pending_templates (int retries)
23576 int reconsider;
23577 location_t saved_loc = input_location;
23579 /* Instantiating templates may trigger vtable generation. This in turn
23580 may require further template instantiations. We place a limit here
23581 to avoid infinite loop. */
23582 if (pending_templates && retries >= max_tinst_depth)
23584 tree decl = pending_templates->tinst->decl;
23586 fatal_error (input_location,
23587 "template instantiation depth exceeds maximum of %d"
23588 " instantiating %q+D, possibly from virtual table generation"
23589 " (use -ftemplate-depth= to increase the maximum)",
23590 max_tinst_depth, decl);
23591 if (TREE_CODE (decl) == FUNCTION_DECL)
23592 /* Pretend that we defined it. */
23593 DECL_INITIAL (decl) = error_mark_node;
23594 return;
23599 struct pending_template **t = &pending_templates;
23600 struct pending_template *last = NULL;
23601 reconsider = 0;
23602 while (*t)
23604 tree instantiation = reopen_tinst_level ((*t)->tinst);
23605 bool complete = false;
23607 if (TYPE_P (instantiation))
23609 if (!COMPLETE_TYPE_P (instantiation))
23611 instantiate_class_template (instantiation);
23612 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
23613 for (tree fld = TYPE_FIELDS (instantiation);
23614 fld; fld = TREE_CHAIN (fld))
23615 if ((VAR_P (fld)
23616 || (TREE_CODE (fld) == FUNCTION_DECL
23617 && !DECL_ARTIFICIAL (fld)))
23618 && DECL_TEMPLATE_INSTANTIATION (fld))
23619 instantiate_decl (fld,
23620 /*defer_ok=*/false,
23621 /*expl_inst_class_mem_p=*/false);
23623 if (COMPLETE_TYPE_P (instantiation))
23624 reconsider = 1;
23627 complete = COMPLETE_TYPE_P (instantiation);
23629 else
23631 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
23632 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
23634 instantiation
23635 = instantiate_decl (instantiation,
23636 /*defer_ok=*/false,
23637 /*expl_inst_class_mem_p=*/false);
23638 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
23639 reconsider = 1;
23642 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
23643 || DECL_TEMPLATE_INSTANTIATED (instantiation));
23646 if (complete)
23647 /* If INSTANTIATION has been instantiated, then we don't
23648 need to consider it again in the future. */
23649 *t = (*t)->next;
23650 else
23652 last = *t;
23653 t = &(*t)->next;
23655 tinst_depth = 0;
23656 current_tinst_level = NULL;
23658 last_pending_template = last;
23660 while (reconsider);
23662 input_location = saved_loc;
23665 /* Substitute ARGVEC into T, which is a list of initializers for
23666 either base class or a non-static data member. The TREE_PURPOSEs
23667 are DECLs, and the TREE_VALUEs are the initializer values. Used by
23668 instantiate_decl. */
23670 static tree
23671 tsubst_initializer_list (tree t, tree argvec)
23673 tree inits = NULL_TREE;
23674 tree target_ctor = error_mark_node;
23676 for (; t; t = TREE_CHAIN (t))
23678 tree decl;
23679 tree init;
23680 tree expanded_bases = NULL_TREE;
23681 tree expanded_arguments = NULL_TREE;
23682 int i, len = 1;
23684 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
23686 tree expr;
23687 tree arg;
23689 /* Expand the base class expansion type into separate base
23690 classes. */
23691 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
23692 tf_warning_or_error,
23693 NULL_TREE);
23694 if (expanded_bases == error_mark_node)
23695 continue;
23697 /* We'll be building separate TREE_LISTs of arguments for
23698 each base. */
23699 len = TREE_VEC_LENGTH (expanded_bases);
23700 expanded_arguments = make_tree_vec (len);
23701 for (i = 0; i < len; i++)
23702 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
23704 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
23705 expand each argument in the TREE_VALUE of t. */
23706 expr = make_node (EXPR_PACK_EXPANSION);
23707 PACK_EXPANSION_LOCAL_P (expr) = true;
23708 PACK_EXPANSION_PARAMETER_PACKS (expr) =
23709 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
23711 if (TREE_VALUE (t) == void_type_node)
23712 /* VOID_TYPE_NODE is used to indicate
23713 value-initialization. */
23715 for (i = 0; i < len; i++)
23716 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
23718 else
23720 /* Substitute parameter packs into each argument in the
23721 TREE_LIST. */
23722 in_base_initializer = 1;
23723 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
23725 tree expanded_exprs;
23727 /* Expand the argument. */
23728 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
23729 expanded_exprs
23730 = tsubst_pack_expansion (expr, argvec,
23731 tf_warning_or_error,
23732 NULL_TREE);
23733 if (expanded_exprs == error_mark_node)
23734 continue;
23736 /* Prepend each of the expanded expressions to the
23737 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
23738 for (i = 0; i < len; i++)
23740 TREE_VEC_ELT (expanded_arguments, i) =
23741 tree_cons (NULL_TREE,
23742 TREE_VEC_ELT (expanded_exprs, i),
23743 TREE_VEC_ELT (expanded_arguments, i));
23746 in_base_initializer = 0;
23748 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
23749 since we built them backwards. */
23750 for (i = 0; i < len; i++)
23752 TREE_VEC_ELT (expanded_arguments, i) =
23753 nreverse (TREE_VEC_ELT (expanded_arguments, i));
23758 for (i = 0; i < len; ++i)
23760 if (expanded_bases)
23762 decl = TREE_VEC_ELT (expanded_bases, i);
23763 decl = expand_member_init (decl);
23764 init = TREE_VEC_ELT (expanded_arguments, i);
23766 else
23768 tree tmp;
23769 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
23770 tf_warning_or_error, NULL_TREE);
23772 decl = expand_member_init (decl);
23773 if (decl && !DECL_P (decl))
23774 in_base_initializer = 1;
23776 init = TREE_VALUE (t);
23777 tmp = init;
23778 if (init != void_type_node)
23779 init = tsubst_expr (init, argvec,
23780 tf_warning_or_error, NULL_TREE,
23781 /*integral_constant_expression_p=*/false);
23782 if (init == NULL_TREE && tmp != NULL_TREE)
23783 /* If we had an initializer but it instantiated to nothing,
23784 value-initialize the object. This will only occur when
23785 the initializer was a pack expansion where the parameter
23786 packs used in that expansion were of length zero. */
23787 init = void_type_node;
23788 in_base_initializer = 0;
23791 if (target_ctor != error_mark_node
23792 && init != error_mark_node)
23794 error ("mem-initializer for %qD follows constructor delegation",
23795 decl);
23796 return inits;
23798 /* Look for a target constructor. */
23799 if (init != error_mark_node
23800 && decl && CLASS_TYPE_P (decl)
23801 && same_type_p (decl, current_class_type))
23803 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
23804 if (inits)
23806 error ("constructor delegation follows mem-initializer for %qD",
23807 TREE_PURPOSE (inits));
23808 continue;
23810 target_ctor = init;
23813 if (decl)
23815 init = build_tree_list (decl, init);
23816 TREE_CHAIN (init) = inits;
23817 inits = init;
23821 return inits;
23824 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
23826 static void
23827 set_current_access_from_decl (tree decl)
23829 if (TREE_PRIVATE (decl))
23830 current_access_specifier = access_private_node;
23831 else if (TREE_PROTECTED (decl))
23832 current_access_specifier = access_protected_node;
23833 else
23834 current_access_specifier = access_public_node;
23837 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
23838 is the instantiation (which should have been created with
23839 start_enum) and ARGS are the template arguments to use. */
23841 static void
23842 tsubst_enum (tree tag, tree newtag, tree args)
23844 tree e;
23846 if (SCOPED_ENUM_P (newtag))
23847 begin_scope (sk_scoped_enum, newtag);
23849 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
23851 tree value;
23852 tree decl;
23854 decl = TREE_VALUE (e);
23855 /* Note that in a template enum, the TREE_VALUE is the
23856 CONST_DECL, not the corresponding INTEGER_CST. */
23857 value = tsubst_expr (DECL_INITIAL (decl),
23858 args, tf_warning_or_error, NULL_TREE,
23859 /*integral_constant_expression_p=*/true);
23861 /* Give this enumeration constant the correct access. */
23862 set_current_access_from_decl (decl);
23864 /* Actually build the enumerator itself. Here we're assuming that
23865 enumerators can't have dependent attributes. */
23866 build_enumerator (DECL_NAME (decl), value, newtag,
23867 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
23870 if (SCOPED_ENUM_P (newtag))
23871 finish_scope ();
23873 finish_enum_value_list (newtag);
23874 finish_enum (newtag);
23876 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
23877 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
23880 /* DECL is a FUNCTION_DECL that is a template specialization. Return
23881 its type -- but without substituting the innermost set of template
23882 arguments. So, innermost set of template parameters will appear in
23883 the type. */
23885 tree
23886 get_mostly_instantiated_function_type (tree decl)
23888 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
23889 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
23892 /* Return truthvalue if we're processing a template different from
23893 the last one involved in diagnostics. */
23894 bool
23895 problematic_instantiation_changed (void)
23897 return current_tinst_level != last_error_tinst_level;
23900 /* Remember current template involved in diagnostics. */
23901 void
23902 record_last_problematic_instantiation (void)
23904 last_error_tinst_level = current_tinst_level;
23907 struct tinst_level *
23908 current_instantiation (void)
23910 return current_tinst_level;
23913 /* Return TRUE if current_function_decl is being instantiated, false
23914 otherwise. */
23916 bool
23917 instantiating_current_function_p (void)
23919 return (current_instantiation ()
23920 && current_instantiation ()->decl == current_function_decl);
23923 /* [temp.param] Check that template non-type parm TYPE is of an allowable
23924 type. Return false for ok, true for disallowed. Issue error and
23925 inform messages under control of COMPLAIN. */
23927 static bool
23928 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
23930 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
23931 return false;
23932 else if (POINTER_TYPE_P (type))
23933 return false;
23934 else if (TYPE_PTRMEM_P (type))
23935 return false;
23936 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
23937 return false;
23938 else if (TREE_CODE (type) == TYPENAME_TYPE)
23939 return false;
23940 else if (TREE_CODE (type) == DECLTYPE_TYPE)
23941 return false;
23942 else if (TREE_CODE (type) == NULLPTR_TYPE)
23943 return false;
23944 /* A bound template template parm could later be instantiated to have a valid
23945 nontype parm type via an alias template. */
23946 else if (cxx_dialect >= cxx11
23947 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23948 return false;
23950 if (complain & tf_error)
23952 if (type == error_mark_node)
23953 inform (input_location, "invalid template non-type parameter");
23954 else
23955 error ("%q#T is not a valid type for a template non-type parameter",
23956 type);
23958 return true;
23961 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
23962 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
23964 static bool
23965 dependent_type_p_r (tree type)
23967 tree scope;
23969 /* [temp.dep.type]
23971 A type is dependent if it is:
23973 -- a template parameter. Template template parameters are types
23974 for us (since TYPE_P holds true for them) so we handle
23975 them here. */
23976 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
23977 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
23978 return true;
23979 /* -- a qualified-id with a nested-name-specifier which contains a
23980 class-name that names a dependent type or whose unqualified-id
23981 names a dependent type. */
23982 if (TREE_CODE (type) == TYPENAME_TYPE)
23983 return true;
23985 /* An alias template specialization can be dependent even if the
23986 resulting type is not. */
23987 if (dependent_alias_template_spec_p (type))
23988 return true;
23990 /* -- a cv-qualified type where the cv-unqualified type is
23991 dependent.
23992 No code is necessary for this bullet; the code below handles
23993 cv-qualified types, and we don't want to strip aliases with
23994 TYPE_MAIN_VARIANT because of DR 1558. */
23995 /* -- a compound type constructed from any dependent type. */
23996 if (TYPE_PTRMEM_P (type))
23997 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
23998 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
23999 (type)));
24000 else if (TYPE_PTR_P (type)
24001 || TREE_CODE (type) == REFERENCE_TYPE)
24002 return dependent_type_p (TREE_TYPE (type));
24003 else if (TREE_CODE (type) == FUNCTION_TYPE
24004 || TREE_CODE (type) == METHOD_TYPE)
24006 tree arg_type;
24008 if (dependent_type_p (TREE_TYPE (type)))
24009 return true;
24010 for (arg_type = TYPE_ARG_TYPES (type);
24011 arg_type;
24012 arg_type = TREE_CHAIN (arg_type))
24013 if (dependent_type_p (TREE_VALUE (arg_type)))
24014 return true;
24015 if (cxx_dialect >= cxx17)
24016 /* A value-dependent noexcept-specifier makes the type dependent. */
24017 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
24018 if (tree noex = TREE_PURPOSE (spec))
24019 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
24020 affect overload resolution and treating it as dependent breaks
24021 things. */
24022 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
24023 && value_dependent_expression_p (noex))
24024 return true;
24025 return false;
24027 /* -- an array type constructed from any dependent type or whose
24028 size is specified by a constant expression that is
24029 value-dependent.
24031 We checked for type- and value-dependence of the bounds in
24032 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
24033 if (TREE_CODE (type) == ARRAY_TYPE)
24035 if (TYPE_DOMAIN (type)
24036 && dependent_type_p (TYPE_DOMAIN (type)))
24037 return true;
24038 return dependent_type_p (TREE_TYPE (type));
24041 /* -- a template-id in which either the template name is a template
24042 parameter ... */
24043 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
24044 return true;
24045 /* ... or any of the template arguments is a dependent type or
24046 an expression that is type-dependent or value-dependent. */
24047 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
24048 && (any_dependent_template_arguments_p
24049 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
24050 return true;
24052 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
24053 dependent; if the argument of the `typeof' expression is not
24054 type-dependent, then it should already been have resolved. */
24055 if (TREE_CODE (type) == TYPEOF_TYPE
24056 || TREE_CODE (type) == DECLTYPE_TYPE
24057 || TREE_CODE (type) == UNDERLYING_TYPE)
24058 return true;
24060 /* A template argument pack is dependent if any of its packed
24061 arguments are. */
24062 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
24064 tree args = ARGUMENT_PACK_ARGS (type);
24065 int i, len = TREE_VEC_LENGTH (args);
24066 for (i = 0; i < len; ++i)
24067 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
24068 return true;
24071 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
24072 be template parameters. */
24073 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
24074 return true;
24076 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
24077 return true;
24079 /* The standard does not specifically mention types that are local
24080 to template functions or local classes, but they should be
24081 considered dependent too. For example:
24083 template <int I> void f() {
24084 enum E { a = I };
24085 S<sizeof (E)> s;
24088 The size of `E' cannot be known until the value of `I' has been
24089 determined. Therefore, `E' must be considered dependent. */
24090 scope = TYPE_CONTEXT (type);
24091 if (scope && TYPE_P (scope))
24092 return dependent_type_p (scope);
24093 /* Don't use type_dependent_expression_p here, as it can lead
24094 to infinite recursion trying to determine whether a lambda
24095 nested in a lambda is dependent (c++/47687). */
24096 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
24097 && DECL_LANG_SPECIFIC (scope)
24098 && DECL_TEMPLATE_INFO (scope)
24099 && (any_dependent_template_arguments_p
24100 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
24101 return true;
24103 /* Other types are non-dependent. */
24104 return false;
24107 /* Returns TRUE if TYPE is dependent, in the sense of
24108 [temp.dep.type]. Note that a NULL type is considered dependent. */
24110 bool
24111 dependent_type_p (tree type)
24113 /* If there are no template parameters in scope, then there can't be
24114 any dependent types. */
24115 if (!processing_template_decl)
24117 /* If we are not processing a template, then nobody should be
24118 providing us with a dependent type. */
24119 gcc_assert (type);
24120 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
24121 return false;
24124 /* If the type is NULL, we have not computed a type for the entity
24125 in question; in that case, the type is dependent. */
24126 if (!type)
24127 return true;
24129 /* Erroneous types can be considered non-dependent. */
24130 if (type == error_mark_node)
24131 return false;
24133 /* Getting here with global_type_node means we improperly called this
24134 function on the TREE_TYPE of an IDENTIFIER_NODE. */
24135 gcc_checking_assert (type != global_type_node);
24137 /* If we have not already computed the appropriate value for TYPE,
24138 do so now. */
24139 if (!TYPE_DEPENDENT_P_VALID (type))
24141 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
24142 TYPE_DEPENDENT_P_VALID (type) = 1;
24145 return TYPE_DEPENDENT_P (type);
24148 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
24149 lookup. In other words, a dependent type that is not the current
24150 instantiation. */
24152 bool
24153 dependent_scope_p (tree scope)
24155 return (scope && TYPE_P (scope) && dependent_type_p (scope)
24156 && !currently_open_class (scope));
24159 /* T is a SCOPE_REF. Return whether it represents a non-static member of
24160 an unknown base of 'this' (and is therefore instantiation-dependent). */
24162 static bool
24163 unknown_base_ref_p (tree t)
24165 if (!current_class_ptr)
24166 return false;
24168 tree mem = TREE_OPERAND (t, 1);
24169 if (shared_member_p (mem))
24170 return false;
24172 tree cur = current_nonlambda_class_type ();
24173 if (!any_dependent_bases_p (cur))
24174 return false;
24176 tree ctx = TREE_OPERAND (t, 0);
24177 if (DERIVED_FROM_P (ctx, cur))
24178 return false;
24180 return true;
24183 /* T is a SCOPE_REF; return whether we need to consider it
24184 instantiation-dependent so that we can check access at instantiation
24185 time even though we know which member it resolves to. */
24187 static bool
24188 instantiation_dependent_scope_ref_p (tree t)
24190 if (DECL_P (TREE_OPERAND (t, 1))
24191 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
24192 && !unknown_base_ref_p (t)
24193 && accessible_in_template_p (TREE_OPERAND (t, 0),
24194 TREE_OPERAND (t, 1)))
24195 return false;
24196 else
24197 return true;
24200 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
24201 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
24202 expression. */
24204 /* Note that this predicate is not appropriate for general expressions;
24205 only constant expressions (that satisfy potential_constant_expression)
24206 can be tested for value dependence. */
24208 bool
24209 value_dependent_expression_p (tree expression)
24211 if (!processing_template_decl || expression == NULL_TREE)
24212 return false;
24214 /* A type-dependent expression is also value-dependent. */
24215 if (type_dependent_expression_p (expression))
24216 return true;
24218 switch (TREE_CODE (expression))
24220 case BASELINK:
24221 /* A dependent member function of the current instantiation. */
24222 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
24224 case FUNCTION_DECL:
24225 /* A dependent member function of the current instantiation. */
24226 if (DECL_CLASS_SCOPE_P (expression)
24227 && dependent_type_p (DECL_CONTEXT (expression)))
24228 return true;
24229 break;
24231 case IDENTIFIER_NODE:
24232 /* A name that has not been looked up -- must be dependent. */
24233 return true;
24235 case TEMPLATE_PARM_INDEX:
24236 /* A non-type template parm. */
24237 return true;
24239 case CONST_DECL:
24240 /* A non-type template parm. */
24241 if (DECL_TEMPLATE_PARM_P (expression))
24242 return true;
24243 return value_dependent_expression_p (DECL_INITIAL (expression));
24245 case VAR_DECL:
24246 /* A constant with literal type and is initialized
24247 with an expression that is value-dependent. */
24248 if (DECL_DEPENDENT_INIT_P (expression)
24249 /* FIXME cp_finish_decl doesn't fold reference initializers. */
24250 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE)
24251 return true;
24252 if (DECL_HAS_VALUE_EXPR_P (expression))
24254 tree value_expr = DECL_VALUE_EXPR (expression);
24255 if (value_dependent_expression_p (value_expr))
24256 return true;
24258 return false;
24260 case DYNAMIC_CAST_EXPR:
24261 case STATIC_CAST_EXPR:
24262 case CONST_CAST_EXPR:
24263 case REINTERPRET_CAST_EXPR:
24264 case CAST_EXPR:
24265 case IMPLICIT_CONV_EXPR:
24266 /* These expressions are value-dependent if the type to which
24267 the cast occurs is dependent or the expression being casted
24268 is value-dependent. */
24270 tree type = TREE_TYPE (expression);
24272 if (dependent_type_p (type))
24273 return true;
24275 /* A functional cast has a list of operands. */
24276 expression = TREE_OPERAND (expression, 0);
24277 if (!expression)
24279 /* If there are no operands, it must be an expression such
24280 as "int()". This should not happen for aggregate types
24281 because it would form non-constant expressions. */
24282 gcc_assert (cxx_dialect >= cxx11
24283 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
24285 return false;
24288 if (TREE_CODE (expression) == TREE_LIST)
24289 return any_value_dependent_elements_p (expression);
24291 return value_dependent_expression_p (expression);
24294 case SIZEOF_EXPR:
24295 if (SIZEOF_EXPR_TYPE_P (expression))
24296 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
24297 /* FALLTHRU */
24298 case ALIGNOF_EXPR:
24299 case TYPEID_EXPR:
24300 /* A `sizeof' expression is value-dependent if the operand is
24301 type-dependent or is a pack expansion. */
24302 expression = TREE_OPERAND (expression, 0);
24303 if (PACK_EXPANSION_P (expression))
24304 return true;
24305 else if (TYPE_P (expression))
24306 return dependent_type_p (expression);
24307 return instantiation_dependent_uneval_expression_p (expression);
24309 case AT_ENCODE_EXPR:
24310 /* An 'encode' expression is value-dependent if the operand is
24311 type-dependent. */
24312 expression = TREE_OPERAND (expression, 0);
24313 return dependent_type_p (expression);
24315 case NOEXCEPT_EXPR:
24316 expression = TREE_OPERAND (expression, 0);
24317 return instantiation_dependent_uneval_expression_p (expression);
24319 case SCOPE_REF:
24320 /* All instantiation-dependent expressions should also be considered
24321 value-dependent. */
24322 return instantiation_dependent_scope_ref_p (expression);
24324 case COMPONENT_REF:
24325 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
24326 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
24328 case NONTYPE_ARGUMENT_PACK:
24329 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
24330 is value-dependent. */
24332 tree values = ARGUMENT_PACK_ARGS (expression);
24333 int i, len = TREE_VEC_LENGTH (values);
24335 for (i = 0; i < len; ++i)
24336 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
24337 return true;
24339 return false;
24342 case TRAIT_EXPR:
24344 tree type2 = TRAIT_EXPR_TYPE2 (expression);
24346 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
24347 return true;
24349 if (!type2)
24350 return false;
24352 if (TREE_CODE (type2) != TREE_LIST)
24353 return dependent_type_p (type2);
24355 for (; type2; type2 = TREE_CHAIN (type2))
24356 if (dependent_type_p (TREE_VALUE (type2)))
24357 return true;
24359 return false;
24362 case MODOP_EXPR:
24363 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24364 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
24366 case ARRAY_REF:
24367 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24368 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
24370 case ADDR_EXPR:
24372 tree op = TREE_OPERAND (expression, 0);
24373 return (value_dependent_expression_p (op)
24374 || has_value_dependent_address (op));
24377 case REQUIRES_EXPR:
24378 /* Treat all requires-expressions as value-dependent so
24379 we don't try to fold them. */
24380 return true;
24382 case TYPE_REQ:
24383 return dependent_type_p (TREE_OPERAND (expression, 0));
24385 case CALL_EXPR:
24387 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
24388 return true;
24389 tree fn = get_callee_fndecl (expression);
24390 int i, nargs;
24391 nargs = call_expr_nargs (expression);
24392 for (i = 0; i < nargs; ++i)
24394 tree op = CALL_EXPR_ARG (expression, i);
24395 /* In a call to a constexpr member function, look through the
24396 implicit ADDR_EXPR on the object argument so that it doesn't
24397 cause the call to be considered value-dependent. We also
24398 look through it in potential_constant_expression. */
24399 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
24400 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
24401 && TREE_CODE (op) == ADDR_EXPR)
24402 op = TREE_OPERAND (op, 0);
24403 if (value_dependent_expression_p (op))
24404 return true;
24406 return false;
24409 case TEMPLATE_ID_EXPR:
24410 return variable_concept_p (TREE_OPERAND (expression, 0));
24412 case CONSTRUCTOR:
24414 unsigned ix;
24415 tree val;
24416 if (dependent_type_p (TREE_TYPE (expression)))
24417 return true;
24418 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
24419 if (value_dependent_expression_p (val))
24420 return true;
24421 return false;
24424 case STMT_EXPR:
24425 /* Treat a GNU statement expression as dependent to avoid crashing
24426 under instantiate_non_dependent_expr; it can't be constant. */
24427 return true;
24429 default:
24430 /* A constant expression is value-dependent if any subexpression is
24431 value-dependent. */
24432 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
24434 case tcc_reference:
24435 case tcc_unary:
24436 case tcc_comparison:
24437 case tcc_binary:
24438 case tcc_expression:
24439 case tcc_vl_exp:
24441 int i, len = cp_tree_operand_length (expression);
24443 for (i = 0; i < len; i++)
24445 tree t = TREE_OPERAND (expression, i);
24447 /* In some cases, some of the operands may be missing.
24448 (For example, in the case of PREDECREMENT_EXPR, the
24449 amount to increment by may be missing.) That doesn't
24450 make the expression dependent. */
24451 if (t && value_dependent_expression_p (t))
24452 return true;
24455 break;
24456 default:
24457 break;
24459 break;
24462 /* The expression is not value-dependent. */
24463 return false;
24466 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
24467 [temp.dep.expr]. Note that an expression with no type is
24468 considered dependent. Other parts of the compiler arrange for an
24469 expression with type-dependent subexpressions to have no type, so
24470 this function doesn't have to be fully recursive. */
24472 bool
24473 type_dependent_expression_p (tree expression)
24475 if (!processing_template_decl)
24476 return false;
24478 if (expression == NULL_TREE || expression == error_mark_node)
24479 return false;
24481 STRIP_ANY_LOCATION_WRAPPER (expression);
24483 /* An unresolved name is always dependent. */
24484 if (identifier_p (expression)
24485 || TREE_CODE (expression) == USING_DECL
24486 || TREE_CODE (expression) == WILDCARD_DECL)
24487 return true;
24489 /* A fold expression is type-dependent. */
24490 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
24491 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
24492 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
24493 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
24494 return true;
24496 /* Some expression forms are never type-dependent. */
24497 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
24498 || TREE_CODE (expression) == SIZEOF_EXPR
24499 || TREE_CODE (expression) == ALIGNOF_EXPR
24500 || TREE_CODE (expression) == AT_ENCODE_EXPR
24501 || TREE_CODE (expression) == NOEXCEPT_EXPR
24502 || TREE_CODE (expression) == TRAIT_EXPR
24503 || TREE_CODE (expression) == TYPEID_EXPR
24504 || TREE_CODE (expression) == DELETE_EXPR
24505 || TREE_CODE (expression) == VEC_DELETE_EXPR
24506 || TREE_CODE (expression) == THROW_EXPR
24507 || TREE_CODE (expression) == REQUIRES_EXPR)
24508 return false;
24510 /* The types of these expressions depends only on the type to which
24511 the cast occurs. */
24512 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
24513 || TREE_CODE (expression) == STATIC_CAST_EXPR
24514 || TREE_CODE (expression) == CONST_CAST_EXPR
24515 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
24516 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
24517 || TREE_CODE (expression) == CAST_EXPR)
24518 return dependent_type_p (TREE_TYPE (expression));
24520 /* The types of these expressions depends only on the type created
24521 by the expression. */
24522 if (TREE_CODE (expression) == NEW_EXPR
24523 || TREE_CODE (expression) == VEC_NEW_EXPR)
24525 /* For NEW_EXPR tree nodes created inside a template, either
24526 the object type itself or a TREE_LIST may appear as the
24527 operand 1. */
24528 tree type = TREE_OPERAND (expression, 1);
24529 if (TREE_CODE (type) == TREE_LIST)
24530 /* This is an array type. We need to check array dimensions
24531 as well. */
24532 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
24533 || value_dependent_expression_p
24534 (TREE_OPERAND (TREE_VALUE (type), 1));
24535 else
24536 return dependent_type_p (type);
24539 if (TREE_CODE (expression) == SCOPE_REF)
24541 tree scope = TREE_OPERAND (expression, 0);
24542 tree name = TREE_OPERAND (expression, 1);
24544 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
24545 contains an identifier associated by name lookup with one or more
24546 declarations declared with a dependent type, or...a
24547 nested-name-specifier or qualified-id that names a member of an
24548 unknown specialization. */
24549 return (type_dependent_expression_p (name)
24550 || dependent_scope_p (scope));
24553 if (TREE_CODE (expression) == TEMPLATE_DECL
24554 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
24555 return uses_outer_template_parms (expression);
24557 if (TREE_CODE (expression) == STMT_EXPR)
24558 expression = stmt_expr_value_expr (expression);
24560 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
24562 tree elt;
24563 unsigned i;
24565 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
24567 if (type_dependent_expression_p (elt))
24568 return true;
24570 return false;
24573 /* A static data member of the current instantiation with incomplete
24574 array type is type-dependent, as the definition and specializations
24575 can have different bounds. */
24576 if (VAR_P (expression)
24577 && DECL_CLASS_SCOPE_P (expression)
24578 && dependent_type_p (DECL_CONTEXT (expression))
24579 && VAR_HAD_UNKNOWN_BOUND (expression))
24580 return true;
24582 /* An array of unknown bound depending on a variadic parameter, eg:
24584 template<typename... Args>
24585 void foo (Args... args)
24587 int arr[] = { args... };
24590 template<int... vals>
24591 void bar ()
24593 int arr[] = { vals... };
24596 If the array has no length and has an initializer, it must be that
24597 we couldn't determine its length in cp_complete_array_type because
24598 it is dependent. */
24599 if (VAR_P (expression)
24600 && TREE_TYPE (expression) != NULL_TREE
24601 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
24602 && !TYPE_DOMAIN (TREE_TYPE (expression))
24603 && DECL_INITIAL (expression))
24604 return true;
24606 /* A function or variable template-id is type-dependent if it has any
24607 dependent template arguments. */
24608 if (VAR_OR_FUNCTION_DECL_P (expression)
24609 && DECL_LANG_SPECIFIC (expression)
24610 && DECL_TEMPLATE_INFO (expression))
24612 /* Consider the innermost template arguments, since those are the ones
24613 that come from the template-id; the template arguments for the
24614 enclosing class do not make it type-dependent unless they are used in
24615 the type of the decl. */
24616 if (PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression))
24617 && (any_dependent_template_arguments_p
24618 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
24619 return true;
24622 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
24623 type-dependent. Checking this is important for functions with auto return
24624 type, which looks like a dependent type. */
24625 if (TREE_CODE (expression) == FUNCTION_DECL
24626 && !(DECL_CLASS_SCOPE_P (expression)
24627 && dependent_type_p (DECL_CONTEXT (expression)))
24628 && !(DECL_FRIEND_P (expression)
24629 && (!DECL_FRIEND_CONTEXT (expression)
24630 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
24631 && !DECL_LOCAL_FUNCTION_P (expression))
24633 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
24634 || undeduced_auto_decl (expression));
24635 return false;
24638 /* Always dependent, on the number of arguments if nothing else. */
24639 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
24640 return true;
24642 if (TREE_TYPE (expression) == unknown_type_node)
24644 if (TREE_CODE (expression) == ADDR_EXPR)
24645 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
24646 if (TREE_CODE (expression) == COMPONENT_REF
24647 || TREE_CODE (expression) == OFFSET_REF)
24649 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
24650 return true;
24651 expression = TREE_OPERAND (expression, 1);
24652 if (identifier_p (expression))
24653 return false;
24655 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
24656 if (TREE_CODE (expression) == SCOPE_REF)
24657 return false;
24659 if (BASELINK_P (expression))
24661 if (BASELINK_OPTYPE (expression)
24662 && dependent_type_p (BASELINK_OPTYPE (expression)))
24663 return true;
24664 expression = BASELINK_FUNCTIONS (expression);
24667 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
24669 if (any_dependent_template_arguments_p
24670 (TREE_OPERAND (expression, 1)))
24671 return true;
24672 expression = TREE_OPERAND (expression, 0);
24673 if (identifier_p (expression))
24674 return true;
24677 gcc_assert (TREE_CODE (expression) == OVERLOAD
24678 || TREE_CODE (expression) == FUNCTION_DECL);
24680 for (lkp_iterator iter (expression); iter; ++iter)
24681 if (type_dependent_expression_p (*iter))
24682 return true;
24684 return false;
24687 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
24689 /* Dependent type attributes might not have made it from the decl to
24690 the type yet. */
24691 if (DECL_P (expression)
24692 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
24693 return true;
24695 return (dependent_type_p (TREE_TYPE (expression)));
24698 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
24699 type-dependent if the expression refers to a member of the current
24700 instantiation and the type of the referenced member is dependent, or the
24701 class member access expression refers to a member of an unknown
24702 specialization.
24704 This function returns true if the OBJECT in such a class member access
24705 expression is of an unknown specialization. */
24707 bool
24708 type_dependent_object_expression_p (tree object)
24710 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
24711 dependent. */
24712 if (TREE_CODE (object) == IDENTIFIER_NODE)
24713 return true;
24714 tree scope = TREE_TYPE (object);
24715 return (!scope || dependent_scope_p (scope));
24718 /* walk_tree callback function for instantiation_dependent_expression_p,
24719 below. Returns non-zero if a dependent subexpression is found. */
24721 static tree
24722 instantiation_dependent_r (tree *tp, int *walk_subtrees,
24723 void * /*data*/)
24725 if (TYPE_P (*tp))
24727 /* We don't have to worry about decltype currently because decltype
24728 of an instantiation-dependent expr is a dependent type. This
24729 might change depending on the resolution of DR 1172. */
24730 *walk_subtrees = false;
24731 return NULL_TREE;
24733 enum tree_code code = TREE_CODE (*tp);
24734 switch (code)
24736 /* Don't treat an argument list as dependent just because it has no
24737 TREE_TYPE. */
24738 case TREE_LIST:
24739 case TREE_VEC:
24740 return NULL_TREE;
24742 case TEMPLATE_PARM_INDEX:
24743 return *tp;
24745 /* Handle expressions with type operands. */
24746 case SIZEOF_EXPR:
24747 case ALIGNOF_EXPR:
24748 case TYPEID_EXPR:
24749 case AT_ENCODE_EXPR:
24751 tree op = TREE_OPERAND (*tp, 0);
24752 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
24753 op = TREE_TYPE (op);
24754 if (TYPE_P (op))
24756 if (dependent_type_p (op))
24757 return *tp;
24758 else
24760 *walk_subtrees = false;
24761 return NULL_TREE;
24764 break;
24767 case COMPONENT_REF:
24768 if (identifier_p (TREE_OPERAND (*tp, 1)))
24769 /* In a template, finish_class_member_access_expr creates a
24770 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
24771 type-dependent, so that we can check access control at
24772 instantiation time (PR 42277). See also Core issue 1273. */
24773 return *tp;
24774 break;
24776 case SCOPE_REF:
24777 if (instantiation_dependent_scope_ref_p (*tp))
24778 return *tp;
24779 else
24780 break;
24782 /* Treat statement-expressions as dependent. */
24783 case BIND_EXPR:
24784 return *tp;
24786 /* Treat requires-expressions as dependent. */
24787 case REQUIRES_EXPR:
24788 return *tp;
24790 case CALL_EXPR:
24791 /* Treat calls to function concepts as dependent. */
24792 if (function_concept_check_p (*tp))
24793 return *tp;
24794 break;
24796 case TEMPLATE_ID_EXPR:
24797 /* And variable concepts. */
24798 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
24799 return *tp;
24800 break;
24802 default:
24803 break;
24806 if (type_dependent_expression_p (*tp))
24807 return *tp;
24808 else
24809 return NULL_TREE;
24812 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
24813 sense defined by the ABI:
24815 "An expression is instantiation-dependent if it is type-dependent
24816 or value-dependent, or it has a subexpression that is type-dependent
24817 or value-dependent."
24819 Except don't actually check value-dependence for unevaluated expressions,
24820 because in sizeof(i) we don't care about the value of i. Checking
24821 type-dependence will in turn check value-dependence of array bounds/template
24822 arguments as needed. */
24824 bool
24825 instantiation_dependent_uneval_expression_p (tree expression)
24827 tree result;
24829 if (!processing_template_decl)
24830 return false;
24832 if (expression == error_mark_node)
24833 return false;
24835 result = cp_walk_tree_without_duplicates (&expression,
24836 instantiation_dependent_r, NULL);
24837 return result != NULL_TREE;
24840 /* As above, but also check value-dependence of the expression as a whole. */
24842 bool
24843 instantiation_dependent_expression_p (tree expression)
24845 return (instantiation_dependent_uneval_expression_p (expression)
24846 || value_dependent_expression_p (expression));
24849 /* Like type_dependent_expression_p, but it also works while not processing
24850 a template definition, i.e. during substitution or mangling. */
24852 bool
24853 type_dependent_expression_p_push (tree expr)
24855 bool b;
24856 ++processing_template_decl;
24857 b = type_dependent_expression_p (expr);
24858 --processing_template_decl;
24859 return b;
24862 /* Returns TRUE if ARGS contains a type-dependent expression. */
24864 bool
24865 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
24867 unsigned int i;
24868 tree arg;
24870 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
24872 if (type_dependent_expression_p (arg))
24873 return true;
24875 return false;
24878 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24879 expressions) contains any type-dependent expressions. */
24881 bool
24882 any_type_dependent_elements_p (const_tree list)
24884 for (; list; list = TREE_CHAIN (list))
24885 if (type_dependent_expression_p (TREE_VALUE (list)))
24886 return true;
24888 return false;
24891 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24892 expressions) contains any value-dependent expressions. */
24894 bool
24895 any_value_dependent_elements_p (const_tree list)
24897 for (; list; list = TREE_CHAIN (list))
24898 if (value_dependent_expression_p (TREE_VALUE (list)))
24899 return true;
24901 return false;
24904 /* Returns TRUE if the ARG (a template argument) is dependent. */
24906 bool
24907 dependent_template_arg_p (tree arg)
24909 if (!processing_template_decl)
24910 return false;
24912 /* Assume a template argument that was wrongly written by the user
24913 is dependent. This is consistent with what
24914 any_dependent_template_arguments_p [that calls this function]
24915 does. */
24916 if (!arg || arg == error_mark_node)
24917 return true;
24919 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
24920 arg = argument_pack_select_arg (arg);
24922 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
24923 return true;
24924 if (TREE_CODE (arg) == TEMPLATE_DECL)
24926 if (DECL_TEMPLATE_PARM_P (arg))
24927 return true;
24928 /* A member template of a dependent class is not necessarily
24929 type-dependent, but it is a dependent template argument because it
24930 will be a member of an unknown specialization to that template. */
24931 tree scope = CP_DECL_CONTEXT (arg);
24932 return TYPE_P (scope) && dependent_type_p (scope);
24934 else if (ARGUMENT_PACK_P (arg))
24936 tree args = ARGUMENT_PACK_ARGS (arg);
24937 int i, len = TREE_VEC_LENGTH (args);
24938 for (i = 0; i < len; ++i)
24940 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
24941 return true;
24944 return false;
24946 else if (TYPE_P (arg))
24947 return dependent_type_p (arg);
24948 else
24949 return (type_dependent_expression_p (arg)
24950 || value_dependent_expression_p (arg));
24953 /* Returns true if ARGS (a collection of template arguments) contains
24954 any types that require structural equality testing. */
24956 bool
24957 any_template_arguments_need_structural_equality_p (tree args)
24959 int i;
24960 int j;
24962 if (!args)
24963 return false;
24964 if (args == error_mark_node)
24965 return true;
24967 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24969 tree level = TMPL_ARGS_LEVEL (args, i + 1);
24970 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24972 tree arg = TREE_VEC_ELT (level, j);
24973 tree packed_args = NULL_TREE;
24974 int k, len = 1;
24976 if (ARGUMENT_PACK_P (arg))
24978 /* Look inside the argument pack. */
24979 packed_args = ARGUMENT_PACK_ARGS (arg);
24980 len = TREE_VEC_LENGTH (packed_args);
24983 for (k = 0; k < len; ++k)
24985 if (packed_args)
24986 arg = TREE_VEC_ELT (packed_args, k);
24988 if (error_operand_p (arg))
24989 return true;
24990 else if (TREE_CODE (arg) == TEMPLATE_DECL)
24991 continue;
24992 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
24993 return true;
24994 else if (!TYPE_P (arg) && TREE_TYPE (arg)
24995 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
24996 return true;
25001 return false;
25004 /* Returns true if ARGS (a collection of template arguments) contains
25005 any dependent arguments. */
25007 bool
25008 any_dependent_template_arguments_p (const_tree args)
25010 int i;
25011 int j;
25013 if (!args)
25014 return false;
25015 if (args == error_mark_node)
25016 return true;
25018 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
25020 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
25021 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
25022 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
25023 return true;
25026 return false;
25029 /* Returns TRUE if the template TMPL is type-dependent. */
25031 bool
25032 dependent_template_p (tree tmpl)
25034 if (TREE_CODE (tmpl) == OVERLOAD)
25036 for (lkp_iterator iter (tmpl); iter; ++iter)
25037 if (dependent_template_p (*iter))
25038 return true;
25039 return false;
25042 /* Template template parameters are dependent. */
25043 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
25044 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
25045 return true;
25046 /* So are names that have not been looked up. */
25047 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
25048 return true;
25049 return false;
25052 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
25054 bool
25055 dependent_template_id_p (tree tmpl, tree args)
25057 return (dependent_template_p (tmpl)
25058 || any_dependent_template_arguments_p (args));
25061 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
25062 are dependent. */
25064 bool
25065 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
25067 int i;
25069 if (!processing_template_decl)
25070 return false;
25072 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
25074 tree decl = TREE_VEC_ELT (declv, i);
25075 tree init = TREE_VEC_ELT (initv, i);
25076 tree cond = TREE_VEC_ELT (condv, i);
25077 tree incr = TREE_VEC_ELT (incrv, i);
25079 if (type_dependent_expression_p (decl)
25080 || TREE_CODE (decl) == SCOPE_REF)
25081 return true;
25083 if (init && type_dependent_expression_p (init))
25084 return true;
25086 if (type_dependent_expression_p (cond))
25087 return true;
25089 if (COMPARISON_CLASS_P (cond)
25090 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
25091 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
25092 return true;
25094 if (TREE_CODE (incr) == MODOP_EXPR)
25096 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
25097 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
25098 return true;
25100 else if (type_dependent_expression_p (incr))
25101 return true;
25102 else if (TREE_CODE (incr) == MODIFY_EXPR)
25104 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
25105 return true;
25106 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
25108 tree t = TREE_OPERAND (incr, 1);
25109 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
25110 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
25111 return true;
25116 return false;
25119 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
25120 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
25121 no such TYPE can be found. Note that this function peers inside
25122 uninstantiated templates and therefore should be used only in
25123 extremely limited situations. ONLY_CURRENT_P restricts this
25124 peering to the currently open classes hierarchy (which is required
25125 when comparing types). */
25127 tree
25128 resolve_typename_type (tree type, bool only_current_p)
25130 tree scope;
25131 tree name;
25132 tree decl;
25133 int quals;
25134 tree pushed_scope;
25135 tree result;
25137 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
25139 scope = TYPE_CONTEXT (type);
25140 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
25141 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
25142 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
25143 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
25144 identifier of the TYPENAME_TYPE anymore.
25145 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
25146 TYPENAME_TYPE instead, we avoid messing up with a possible
25147 typedef variant case. */
25148 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
25150 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
25151 it first before we can figure out what NAME refers to. */
25152 if (TREE_CODE (scope) == TYPENAME_TYPE)
25154 if (TYPENAME_IS_RESOLVING_P (scope))
25155 /* Given a class template A with a dependent base with nested type C,
25156 typedef typename A::C::C C will land us here, as trying to resolve
25157 the initial A::C leads to the local C typedef, which leads back to
25158 A::C::C. So we break the recursion now. */
25159 return type;
25160 else
25161 scope = resolve_typename_type (scope, only_current_p);
25163 /* If we don't know what SCOPE refers to, then we cannot resolve the
25164 TYPENAME_TYPE. */
25165 if (!CLASS_TYPE_P (scope))
25166 return type;
25167 /* If this is a typedef, we don't want to look inside (c++/11987). */
25168 if (typedef_variant_p (type))
25169 return type;
25170 /* If SCOPE isn't the template itself, it will not have a valid
25171 TYPE_FIELDS list. */
25172 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
25173 /* scope is either the template itself or a compatible instantiation
25174 like X<T>, so look up the name in the original template. */
25175 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
25176 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
25177 gcc_checking_assert (uses_template_parms (scope));
25178 /* If scope has no fields, it can't be a current instantiation. Check this
25179 before currently_open_class to avoid infinite recursion (71515). */
25180 if (!TYPE_FIELDS (scope))
25181 return type;
25182 /* If the SCOPE is not the current instantiation, there's no reason
25183 to look inside it. */
25184 if (only_current_p && !currently_open_class (scope))
25185 return type;
25186 /* Enter the SCOPE so that name lookup will be resolved as if we
25187 were in the class definition. In particular, SCOPE will no
25188 longer be considered a dependent type. */
25189 pushed_scope = push_scope (scope);
25190 /* Look up the declaration. */
25191 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
25192 tf_warning_or_error);
25194 result = NULL_TREE;
25196 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
25197 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
25198 tree fullname = TYPENAME_TYPE_FULLNAME (type);
25199 if (!decl)
25200 /*nop*/;
25201 else if (identifier_p (fullname)
25202 && TREE_CODE (decl) == TYPE_DECL)
25204 result = TREE_TYPE (decl);
25205 if (result == error_mark_node)
25206 result = NULL_TREE;
25208 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
25209 && DECL_CLASS_TEMPLATE_P (decl))
25211 /* Obtain the template and the arguments. */
25212 tree tmpl = TREE_OPERAND (fullname, 0);
25213 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
25215 /* We get here with a plain identifier because a previous tentative
25216 parse of the nested-name-specifier as part of a ptr-operator saw
25217 ::template X<A>. The use of ::template is necessary in a
25218 ptr-operator, but wrong in a declarator-id.
25220 [temp.names]: In a qualified-id of a declarator-id, the keyword
25221 template shall not appear at the top level. */
25222 pedwarn (EXPR_LOC_OR_LOC (fullname, input_location), OPT_Wpedantic,
25223 "keyword %<template%> not allowed in declarator-id");
25224 tmpl = decl;
25226 tree args = TREE_OPERAND (fullname, 1);
25227 /* Instantiate the template. */
25228 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
25229 /*entering_scope=*/true,
25230 tf_error | tf_user);
25231 if (result == error_mark_node)
25232 result = NULL_TREE;
25235 /* Leave the SCOPE. */
25236 if (pushed_scope)
25237 pop_scope (pushed_scope);
25239 /* If we failed to resolve it, return the original typename. */
25240 if (!result)
25241 return type;
25243 /* If lookup found a typename type, resolve that too. */
25244 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
25246 /* Ill-formed programs can cause infinite recursion here, so we
25247 must catch that. */
25248 TYPENAME_IS_RESOLVING_P (result) = 1;
25249 result = resolve_typename_type (result, only_current_p);
25250 TYPENAME_IS_RESOLVING_P (result) = 0;
25253 /* Qualify the resulting type. */
25254 quals = cp_type_quals (type);
25255 if (quals)
25256 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
25258 return result;
25261 /* EXPR is an expression which is not type-dependent. Return a proxy
25262 for EXPR that can be used to compute the types of larger
25263 expressions containing EXPR. */
25265 tree
25266 build_non_dependent_expr (tree expr)
25268 tree orig_expr = expr;
25269 tree inner_expr;
25271 /* When checking, try to get a constant value for all non-dependent
25272 expressions in order to expose bugs in *_dependent_expression_p
25273 and constexpr. This can affect code generation, see PR70704, so
25274 only do this for -fchecking=2. */
25275 if (flag_checking > 1
25276 && cxx_dialect >= cxx11
25277 /* Don't do this during nsdmi parsing as it can lead to
25278 unexpected recursive instantiations. */
25279 && !parsing_nsdmi ()
25280 /* Don't do this during concept expansion either and for
25281 the same reason. */
25282 && !expanding_concept ())
25283 fold_non_dependent_expr (expr);
25285 STRIP_ANY_LOCATION_WRAPPER (expr);
25287 /* Preserve OVERLOADs; the functions must be available to resolve
25288 types. */
25289 inner_expr = expr;
25290 if (TREE_CODE (inner_expr) == STMT_EXPR)
25291 inner_expr = stmt_expr_value_expr (inner_expr);
25292 if (TREE_CODE (inner_expr) == ADDR_EXPR)
25293 inner_expr = TREE_OPERAND (inner_expr, 0);
25294 if (TREE_CODE (inner_expr) == COMPONENT_REF)
25295 inner_expr = TREE_OPERAND (inner_expr, 1);
25296 if (is_overloaded_fn (inner_expr)
25297 || TREE_CODE (inner_expr) == OFFSET_REF)
25298 return orig_expr;
25299 /* There is no need to return a proxy for a variable. */
25300 if (VAR_P (expr))
25301 return orig_expr;
25302 /* Preserve string constants; conversions from string constants to
25303 "char *" are allowed, even though normally a "const char *"
25304 cannot be used to initialize a "char *". */
25305 if (TREE_CODE (expr) == STRING_CST)
25306 return orig_expr;
25307 /* Preserve void and arithmetic constants, as an optimization -- there is no
25308 reason to create a new node. */
25309 if (TREE_CODE (expr) == VOID_CST
25310 || TREE_CODE (expr) == INTEGER_CST
25311 || TREE_CODE (expr) == REAL_CST)
25312 return orig_expr;
25313 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
25314 There is at least one place where we want to know that a
25315 particular expression is a throw-expression: when checking a ?:
25316 expression, there are special rules if the second or third
25317 argument is a throw-expression. */
25318 if (TREE_CODE (expr) == THROW_EXPR)
25319 return orig_expr;
25321 /* Don't wrap an initializer list, we need to be able to look inside. */
25322 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
25323 return orig_expr;
25325 /* Don't wrap a dummy object, we need to be able to test for it. */
25326 if (is_dummy_object (expr))
25327 return orig_expr;
25329 if (TREE_CODE (expr) == COND_EXPR)
25330 return build3 (COND_EXPR,
25331 TREE_TYPE (expr),
25332 TREE_OPERAND (expr, 0),
25333 (TREE_OPERAND (expr, 1)
25334 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
25335 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
25336 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
25337 if (TREE_CODE (expr) == COMPOUND_EXPR
25338 && !COMPOUND_EXPR_OVERLOADED (expr))
25339 return build2 (COMPOUND_EXPR,
25340 TREE_TYPE (expr),
25341 TREE_OPERAND (expr, 0),
25342 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
25344 /* If the type is unknown, it can't really be non-dependent */
25345 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
25347 /* Otherwise, build a NON_DEPENDENT_EXPR. */
25348 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
25351 /* ARGS is a vector of expressions as arguments to a function call.
25352 Replace the arguments with equivalent non-dependent expressions.
25353 This modifies ARGS in place. */
25355 void
25356 make_args_non_dependent (vec<tree, va_gc> *args)
25358 unsigned int ix;
25359 tree arg;
25361 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
25363 tree newarg = build_non_dependent_expr (arg);
25364 if (newarg != arg)
25365 (*args)[ix] = newarg;
25369 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
25370 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
25371 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
25373 static tree
25374 make_auto_1 (tree name, bool set_canonical)
25376 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
25377 TYPE_NAME (au) = build_decl (input_location,
25378 TYPE_DECL, name, au);
25379 TYPE_STUB_DECL (au) = TYPE_NAME (au);
25380 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
25381 (0, processing_template_decl + 1, processing_template_decl + 1,
25382 TYPE_NAME (au), NULL_TREE);
25383 if (set_canonical)
25384 TYPE_CANONICAL (au) = canonical_type_parameter (au);
25385 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
25386 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
25388 return au;
25391 tree
25392 make_decltype_auto (void)
25394 return make_auto_1 (decltype_auto_identifier, true);
25397 tree
25398 make_auto (void)
25400 return make_auto_1 (auto_identifier, true);
25403 /* Return a C++17 deduction placeholder for class template TMPL. */
25405 tree
25406 make_template_placeholder (tree tmpl)
25408 tree t = make_auto_1 (DECL_NAME (tmpl), true);
25409 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
25410 return t;
25413 /* True iff T is a C++17 class template deduction placeholder. */
25415 bool
25416 template_placeholder_p (tree t)
25418 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
25421 /* Make a "constrained auto" type-specifier. This is an
25422 auto type with constraints that must be associated after
25423 deduction. The constraint is formed from the given
25424 CONC and its optional sequence of arguments, which are
25425 non-null if written as partial-concept-id. */
25427 tree
25428 make_constrained_auto (tree con, tree args)
25430 tree type = make_auto_1 (auto_identifier, false);
25432 /* Build the constraint. */
25433 tree tmpl = DECL_TI_TEMPLATE (con);
25434 tree expr = VAR_P (con) ? tmpl : ovl_make (tmpl);
25435 expr = build_concept_check (expr, type, args);
25437 tree constr = normalize_expression (expr);
25438 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
25440 /* Our canonical type depends on the constraint. */
25441 TYPE_CANONICAL (type) = canonical_type_parameter (type);
25443 /* Attach the constraint to the type declaration. */
25444 tree decl = TYPE_NAME (type);
25445 return decl;
25448 /* Given type ARG, return std::initializer_list<ARG>. */
25450 static tree
25451 listify (tree arg)
25453 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
25455 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
25457 gcc_rich_location richloc (input_location);
25458 maybe_add_include_fixit (&richloc, "<initializer_list>");
25459 error_at (&richloc,
25460 "deducing from brace-enclosed initializer list"
25461 " requires %<#include <initializer_list>%>");
25463 return error_mark_node;
25465 tree argvec = make_tree_vec (1);
25466 TREE_VEC_ELT (argvec, 0) = arg;
25468 return lookup_template_class (std_init_list, argvec, NULL_TREE,
25469 NULL_TREE, 0, tf_warning_or_error);
25472 /* Replace auto in TYPE with std::initializer_list<auto>. */
25474 static tree
25475 listify_autos (tree type, tree auto_node)
25477 tree init_auto = listify (auto_node);
25478 tree argvec = make_tree_vec (1);
25479 TREE_VEC_ELT (argvec, 0) = init_auto;
25480 if (processing_template_decl)
25481 argvec = add_to_template_args (current_template_args (), argvec);
25482 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
25485 /* Hash traits for hashing possibly constrained 'auto'
25486 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
25488 struct auto_hash : default_hash_traits<tree>
25490 static inline hashval_t hash (tree);
25491 static inline bool equal (tree, tree);
25494 /* Hash the 'auto' T. */
25496 inline hashval_t
25497 auto_hash::hash (tree t)
25499 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
25500 /* Matching constrained-type-specifiers denote the same template
25501 parameter, so hash the constraint. */
25502 return hash_placeholder_constraint (c);
25503 else
25504 /* But unconstrained autos are all separate, so just hash the pointer. */
25505 return iterative_hash_object (t, 0);
25508 /* Compare two 'auto's. */
25510 inline bool
25511 auto_hash::equal (tree t1, tree t2)
25513 if (t1 == t2)
25514 return true;
25516 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
25517 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
25519 /* Two unconstrained autos are distinct. */
25520 if (!c1 || !c2)
25521 return false;
25523 return equivalent_placeholder_constraints (c1, c2);
25526 /* for_each_template_parm callback for extract_autos: if t is a (possibly
25527 constrained) auto, add it to the vector. */
25529 static int
25530 extract_autos_r (tree t, void *data)
25532 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
25533 if (is_auto (t))
25535 /* All the autos were built with index 0; fix that up now. */
25536 tree *p = hash.find_slot (t, INSERT);
25537 unsigned idx;
25538 if (*p)
25539 /* If this is a repeated constrained-type-specifier, use the index we
25540 chose before. */
25541 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
25542 else
25544 /* Otherwise this is new, so use the current count. */
25545 *p = t;
25546 idx = hash.elements () - 1;
25548 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
25551 /* Always keep walking. */
25552 return 0;
25555 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
25556 says they can appear anywhere in the type. */
25558 static tree
25559 extract_autos (tree type)
25561 hash_set<tree> visited;
25562 hash_table<auto_hash> hash (2);
25564 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
25566 tree tree_vec = make_tree_vec (hash.elements());
25567 for (hash_table<auto_hash>::iterator iter = hash.begin();
25568 iter != hash.end(); ++iter)
25570 tree elt = *iter;
25571 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
25572 TREE_VEC_ELT (tree_vec, i)
25573 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
25576 return tree_vec;
25579 /* The stem for deduction guide names. */
25580 const char *const dguide_base = "__dguide_";
25582 /* Return the name for a deduction guide for class template TMPL. */
25584 tree
25585 dguide_name (tree tmpl)
25587 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
25588 tree tname = TYPE_IDENTIFIER (type);
25589 char *buf = (char *) alloca (1 + strlen (dguide_base)
25590 + IDENTIFIER_LENGTH (tname));
25591 memcpy (buf, dguide_base, strlen (dguide_base));
25592 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
25593 IDENTIFIER_LENGTH (tname) + 1);
25594 tree dname = get_identifier (buf);
25595 TREE_TYPE (dname) = type;
25596 return dname;
25599 /* True if NAME is the name of a deduction guide. */
25601 bool
25602 dguide_name_p (tree name)
25604 return (TREE_CODE (name) == IDENTIFIER_NODE
25605 && TREE_TYPE (name)
25606 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
25607 strlen (dguide_base)));
25610 /* True if FN is a deduction guide. */
25612 bool
25613 deduction_guide_p (const_tree fn)
25615 if (DECL_P (fn))
25616 if (tree name = DECL_NAME (fn))
25617 return dguide_name_p (name);
25618 return false;
25621 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
25623 bool
25624 copy_guide_p (const_tree fn)
25626 gcc_assert (deduction_guide_p (fn));
25627 if (!DECL_ARTIFICIAL (fn))
25628 return false;
25629 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
25630 return (TREE_CHAIN (parms) == void_list_node
25631 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
25634 /* True if FN is a guide generated from a constructor template. */
25636 bool
25637 template_guide_p (const_tree fn)
25639 gcc_assert (deduction_guide_p (fn));
25640 if (!DECL_ARTIFICIAL (fn))
25641 return false;
25642 tree tmpl = DECL_TI_TEMPLATE (fn);
25643 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
25644 return PRIMARY_TEMPLATE_P (org);
25645 return false;
25648 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
25649 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
25650 template parameter types. Note that the handling of template template
25651 parameters relies on current_template_parms being set appropriately for the
25652 new template. */
25654 static tree
25655 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
25656 tree tsubst_args, tsubst_flags_t complain)
25658 tree oldidx = get_template_parm_index (olddecl);
25660 tree newtype;
25661 if (TREE_CODE (olddecl) == TYPE_DECL
25662 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25664 tree oldtype = TREE_TYPE (olddecl);
25665 newtype = cxx_make_type (TREE_CODE (oldtype));
25666 TYPE_MAIN_VARIANT (newtype) = newtype;
25667 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
25668 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
25669 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
25671 else
25672 newtype = tsubst (TREE_TYPE (olddecl), tsubst_args,
25673 complain, NULL_TREE);
25675 tree newdecl
25676 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
25677 DECL_NAME (olddecl), newtype);
25678 SET_DECL_TEMPLATE_PARM_P (newdecl);
25680 tree newidx;
25681 if (TREE_CODE (olddecl) == TYPE_DECL
25682 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25684 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
25685 = build_template_parm_index (index, level, level,
25686 newdecl, newtype);
25687 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25688 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25689 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
25690 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
25692 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
25694 DECL_TEMPLATE_RESULT (newdecl)
25695 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
25696 DECL_NAME (olddecl), newtype);
25697 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
25698 // First create a copy (ttargs) of tsubst_args with an
25699 // additional level for the template template parameter's own
25700 // template parameters (ttparms).
25701 tree ttparms = (INNERMOST_TEMPLATE_PARMS
25702 (DECL_TEMPLATE_PARMS (olddecl)));
25703 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
25704 tree ttargs = make_tree_vec (depth + 1);
25705 for (int i = 0; i < depth; ++i)
25706 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
25707 TREE_VEC_ELT (ttargs, depth)
25708 = template_parms_level_to_args (ttparms);
25709 // Substitute ttargs into ttparms to fix references to
25710 // other template parameters.
25711 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25712 complain|tf_partial);
25713 // Now substitute again with args based on tparms, to reduce
25714 // the level of the ttparms.
25715 ttargs = current_template_args ();
25716 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25717 complain);
25718 // Finally, tack the adjusted parms onto tparms.
25719 ttparms = tree_cons (size_int (depth), ttparms,
25720 current_template_parms);
25721 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
25724 else
25726 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
25727 tree newconst
25728 = build_decl (DECL_SOURCE_LOCATION (oldconst),
25729 TREE_CODE (oldconst),
25730 DECL_NAME (oldconst), newtype);
25731 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
25732 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
25733 SET_DECL_TEMPLATE_PARM_P (newconst);
25734 newidx = build_template_parm_index (index, level, level,
25735 newconst, newtype);
25736 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25737 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25738 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
25741 return newdecl;
25744 /* Returns a C++17 class deduction guide template based on the constructor
25745 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
25746 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
25748 static tree
25749 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
25751 tree type, tparms, targs, fparms, fargs, ci;
25752 bool memtmpl = false;
25753 bool explicit_p;
25754 location_t loc;
25755 tree fn_tmpl = NULL_TREE;
25757 if (TYPE_P (ctor))
25759 type = ctor;
25760 bool copy_p = TREE_CODE (type) == REFERENCE_TYPE;
25761 if (copy_p)
25763 type = TREE_TYPE (type);
25764 fparms = tree_cons (NULL_TREE, type, void_list_node);
25766 else
25767 fparms = void_list_node;
25769 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
25770 tparms = DECL_TEMPLATE_PARMS (ctmpl);
25771 targs = CLASSTYPE_TI_ARGS (type);
25772 ci = NULL_TREE;
25773 fargs = NULL_TREE;
25774 loc = DECL_SOURCE_LOCATION (ctmpl);
25775 explicit_p = false;
25777 else
25779 ++processing_template_decl;
25781 fn_tmpl
25782 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
25783 : DECL_TI_TEMPLATE (ctor));
25784 if (outer_args)
25785 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
25786 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
25788 type = DECL_CONTEXT (ctor);
25790 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
25791 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
25792 fully specialized args for the enclosing class. Strip those off, as
25793 the deduction guide won't have those template parameters. */
25794 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
25795 TMPL_PARMS_DEPTH (tparms));
25796 /* Discard the 'this' parameter. */
25797 fparms = FUNCTION_ARG_CHAIN (ctor);
25798 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
25799 ci = get_constraints (ctor);
25800 loc = DECL_SOURCE_LOCATION (ctor);
25801 explicit_p = DECL_NONCONVERTING_P (ctor);
25803 if (PRIMARY_TEMPLATE_P (fn_tmpl))
25805 memtmpl = true;
25807 /* For a member template constructor, we need to flatten the two
25808 template parameter lists into one, and then adjust the function
25809 signature accordingly. This gets...complicated. */
25810 tree save_parms = current_template_parms;
25812 /* For a member template we should have two levels of parms/args, one
25813 for the class and one for the constructor. We stripped
25814 specialized args for further enclosing classes above. */
25815 const int depth = 2;
25816 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
25818 /* Template args for translating references to the two-level template
25819 parameters into references to the one-level template parameters we
25820 are creating. */
25821 tree tsubst_args = copy_node (targs);
25822 TMPL_ARGS_LEVEL (tsubst_args, depth)
25823 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
25825 /* Template parms for the constructor template. */
25826 tree ftparms = TREE_VALUE (tparms);
25827 unsigned flen = TREE_VEC_LENGTH (ftparms);
25828 /* Template parms for the class template. */
25829 tparms = TREE_CHAIN (tparms);
25830 tree ctparms = TREE_VALUE (tparms);
25831 unsigned clen = TREE_VEC_LENGTH (ctparms);
25832 /* Template parms for the deduction guide start as a copy of the
25833 template parms for the class. We set current_template_parms for
25834 lookup_template_class_1. */
25835 current_template_parms = tparms = copy_node (tparms);
25836 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
25837 for (unsigned i = 0; i < clen; ++i)
25838 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
25840 /* Now we need to rewrite the constructor parms to append them to the
25841 class parms. */
25842 for (unsigned i = 0; i < flen; ++i)
25844 unsigned index = i + clen;
25845 unsigned level = 1;
25846 tree oldelt = TREE_VEC_ELT (ftparms, i);
25847 tree olddecl = TREE_VALUE (oldelt);
25848 tree newdecl = rewrite_template_parm (olddecl, index, level,
25849 tsubst_args, complain);
25850 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
25851 tsubst_args, complain, ctor);
25852 tree list = build_tree_list (newdef, newdecl);
25853 TEMPLATE_PARM_CONSTRAINTS (list)
25854 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
25855 tsubst_args, complain, ctor);
25856 TREE_VEC_ELT (new_vec, index) = list;
25857 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
25860 /* Now we have a final set of template parms to substitute into the
25861 function signature. */
25862 targs = template_parms_to_args (tparms);
25863 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
25864 complain, ctor);
25865 fargs = tsubst (fargs, tsubst_args, complain, ctor);
25866 if (ci)
25867 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
25869 current_template_parms = save_parms;
25871 --processing_template_decl;
25874 if (!memtmpl)
25876 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
25877 tparms = copy_node (tparms);
25878 INNERMOST_TEMPLATE_PARMS (tparms)
25879 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
25882 tree fntype = build_function_type (type, fparms);
25883 tree ded_fn = build_lang_decl_loc (loc,
25884 FUNCTION_DECL,
25885 dguide_name (type), fntype);
25886 DECL_ARGUMENTS (ded_fn) = fargs;
25887 DECL_ARTIFICIAL (ded_fn) = true;
25888 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
25889 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
25890 DECL_ARTIFICIAL (ded_tmpl) = true;
25891 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
25892 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
25893 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
25894 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
25895 if (DECL_P (ctor))
25896 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
25897 if (ci)
25898 set_constraints (ded_tmpl, ci);
25900 return ded_tmpl;
25903 /* Deduce template arguments for the class template placeholder PTYPE for
25904 template TMPL based on the initializer INIT, and return the resulting
25905 type. */
25907 static tree
25908 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
25909 tsubst_flags_t complain)
25911 if (!DECL_CLASS_TEMPLATE_P (tmpl))
25913 /* We should have handled this in the caller. */
25914 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
25915 return ptype;
25916 if (complain & tf_error)
25917 error ("non-class template %qT used without template arguments", tmpl);
25918 return error_mark_node;
25921 tree type = TREE_TYPE (tmpl);
25923 bool try_list_ctor = false;
25925 vec<tree,va_gc> *args;
25926 if (init == NULL_TREE
25927 || TREE_CODE (init) == TREE_LIST)
25928 args = make_tree_vector_from_list (init);
25929 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
25931 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
25932 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
25934 /* As an exception, the first phase in 16.3.1.7 (considering the
25935 initializer list as a single argument) is omitted if the
25936 initializer list consists of a single expression of type cv U,
25937 where U is a specialization of C or a class derived from a
25938 specialization of C. */
25939 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
25940 tree etype = TREE_TYPE (elt);
25942 tree tparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
25943 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25944 int err = unify (tparms, targs, type, etype,
25945 UNIFY_ALLOW_DERIVED, /*explain*/false);
25946 if (err == 0)
25947 try_list_ctor = false;
25948 ggc_free (targs);
25950 if (try_list_ctor || is_std_init_list (type))
25951 args = make_tree_vector_single (init);
25952 else
25953 args = make_tree_vector_from_ctor (init);
25955 else
25956 args = make_tree_vector_single (init);
25958 tree dname = dguide_name (tmpl);
25959 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
25960 /*type*/false, /*complain*/false,
25961 /*hidden*/false);
25962 bool elided = false;
25963 if (cands == error_mark_node)
25964 cands = NULL_TREE;
25966 /* Prune explicit deduction guides in copy-initialization context. */
25967 if (flags & LOOKUP_ONLYCONVERTING)
25969 for (lkp_iterator iter (cands); !elided && iter; ++iter)
25970 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25971 elided = true;
25973 if (elided)
25975 /* Found a nonconverting guide, prune the candidates. */
25976 tree pruned = NULL_TREE;
25977 for (lkp_iterator iter (cands); iter; ++iter)
25978 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25979 pruned = lookup_add (*iter, pruned);
25981 cands = pruned;
25985 tree outer_args = NULL_TREE;
25986 if (DECL_CLASS_SCOPE_P (tmpl)
25987 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl)))
25989 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
25990 type = TREE_TYPE (most_general_template (tmpl));
25993 bool saw_ctor = false;
25994 // FIXME cache artificial deduction guides
25995 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
25997 tree guide = build_deduction_guide (*iter, outer_args, complain);
25998 if ((flags & LOOKUP_ONLYCONVERTING)
25999 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
26000 elided = true;
26001 else
26002 cands = lookup_add (guide, cands);
26004 saw_ctor = true;
26007 tree call = error_mark_node;
26009 /* If this is list-initialization and the class has a list constructor, first
26010 try deducing from the list as a single argument, as [over.match.list]. */
26011 tree list_cands = NULL_TREE;
26012 if (try_list_ctor && cands)
26013 for (lkp_iterator iter (cands); iter; ++iter)
26015 tree dg = *iter;
26016 if (is_list_ctor (dg))
26017 list_cands = lookup_add (dg, list_cands);
26019 if (list_cands)
26021 ++cp_unevaluated_operand;
26022 call = build_new_function_call (list_cands, &args, tf_decltype);
26023 --cp_unevaluated_operand;
26025 if (call == error_mark_node)
26027 /* That didn't work, now try treating the list as a sequence of
26028 arguments. */
26029 release_tree_vector (args);
26030 args = make_tree_vector_from_ctor (init);
26034 /* Maybe generate an implicit deduction guide. */
26035 if (call == error_mark_node && args->length () < 2)
26037 tree gtype = NULL_TREE;
26039 if (args->length () == 1)
26040 /* Generate a copy guide. */
26041 gtype = build_reference_type (type);
26042 else if (!saw_ctor)
26043 /* Generate a default guide. */
26044 gtype = type;
26046 if (gtype)
26048 tree guide = build_deduction_guide (gtype, outer_args, complain);
26049 cands = lookup_add (guide, cands);
26053 if (elided && !cands)
26055 error ("cannot deduce template arguments for copy-initialization"
26056 " of %qT, as it has no non-explicit deduction guides or "
26057 "user-declared constructors", type);
26058 return error_mark_node;
26060 else if (!cands && call == error_mark_node)
26062 error ("cannot deduce template arguments of %qT, as it has no viable "
26063 "deduction guides", type);
26064 return error_mark_node;
26067 if (call == error_mark_node)
26069 ++cp_unevaluated_operand;
26070 call = build_new_function_call (cands, &args, tf_decltype);
26071 --cp_unevaluated_operand;
26074 if (call == error_mark_node && (complain & tf_warning_or_error))
26076 error ("class template argument deduction failed:");
26078 ++cp_unevaluated_operand;
26079 call = build_new_function_call (cands, &args, complain | tf_decltype);
26080 --cp_unevaluated_operand;
26082 if (elided)
26083 inform (input_location, "explicit deduction guides not considered "
26084 "for copy-initialization");
26087 release_tree_vector (args);
26089 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
26092 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
26093 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
26094 The CONTEXT determines the context in which auto deduction is performed
26095 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
26096 OUTER_TARGS are used during template argument deduction
26097 (context == adc_unify) to properly substitute the result, and is ignored
26098 in other contexts.
26100 For partial-concept-ids, extra args may be appended to the list of deduced
26101 template arguments prior to determining constraint satisfaction. */
26103 tree
26104 do_auto_deduction (tree type, tree init, tree auto_node,
26105 tsubst_flags_t complain, auto_deduction_context context,
26106 tree outer_targs, int flags)
26108 tree targs;
26110 if (init == error_mark_node)
26111 return error_mark_node;
26113 if (init && type_dependent_expression_p (init)
26114 && context != adc_unify)
26115 /* Defining a subset of type-dependent expressions that we can deduce
26116 from ahead of time isn't worth the trouble. */
26117 return type;
26119 /* Similarly, we can't deduce from another undeduced decl. */
26120 if (init && undeduced_auto_decl (init))
26121 return type;
26123 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
26124 /* C++17 class template argument deduction. */
26125 return do_class_deduction (type, tmpl, init, flags, complain);
26127 if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
26128 /* Nothing we can do with this, even in deduction context. */
26129 return type;
26131 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
26132 with either a new invented type template parameter U or, if the
26133 initializer is a braced-init-list (8.5.4), with
26134 std::initializer_list<U>. */
26135 if (BRACE_ENCLOSED_INITIALIZER_P (init))
26137 if (!DIRECT_LIST_INIT_P (init))
26138 type = listify_autos (type, auto_node);
26139 else if (CONSTRUCTOR_NELTS (init) == 1)
26140 init = CONSTRUCTOR_ELT (init, 0)->value;
26141 else
26143 if (complain & tf_warning_or_error)
26145 if (permerror (input_location, "direct-list-initialization of "
26146 "%<auto%> requires exactly one element"))
26147 inform (input_location,
26148 "for deduction to %<std::initializer_list%>, use copy-"
26149 "list-initialization (i.e. add %<=%> before the %<{%>)");
26151 type = listify_autos (type, auto_node);
26155 if (type == error_mark_node)
26156 return error_mark_node;
26158 init = resolve_nondeduced_context (init, complain);
26160 if (context == adc_decomp_type
26161 && auto_node == type
26162 && init != error_mark_node
26163 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
26164 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
26165 and initializer has array type, deduce cv-qualified array type. */
26166 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
26167 complain);
26168 else if (AUTO_IS_DECLTYPE (auto_node))
26170 bool id = (DECL_P (init)
26171 || ((TREE_CODE (init) == COMPONENT_REF
26172 || TREE_CODE (init) == SCOPE_REF)
26173 && !REF_PARENTHESIZED_P (init)));
26174 targs = make_tree_vec (1);
26175 TREE_VEC_ELT (targs, 0)
26176 = finish_decltype_type (init, id, tf_warning_or_error);
26177 if (type != auto_node)
26179 if (complain & tf_error)
26180 error ("%qT as type rather than plain %<decltype(auto)%>", type);
26181 return error_mark_node;
26184 else
26186 tree parms = build_tree_list (NULL_TREE, type);
26187 tree tparms;
26189 if (flag_concepts)
26190 tparms = extract_autos (type);
26191 else
26193 tparms = make_tree_vec (1);
26194 TREE_VEC_ELT (tparms, 0)
26195 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
26198 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
26199 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
26200 DEDUCE_CALL, LOOKUP_NORMAL,
26201 NULL, /*explain_p=*/false);
26202 if (val > 0)
26204 if (processing_template_decl)
26205 /* Try again at instantiation time. */
26206 return type;
26207 if (type && type != error_mark_node
26208 && (complain & tf_error))
26209 /* If type is error_mark_node a diagnostic must have been
26210 emitted by now. Also, having a mention to '<type error>'
26211 in the diagnostic is not really useful to the user. */
26213 if (cfun && auto_node == current_function_auto_return_pattern
26214 && LAMBDA_FUNCTION_P (current_function_decl))
26215 error ("unable to deduce lambda return type from %qE", init);
26216 else
26217 error ("unable to deduce %qT from %qE", type, init);
26218 type_unification_real (tparms, targs, parms, &init, 1, 0,
26219 DEDUCE_CALL, LOOKUP_NORMAL,
26220 NULL, /*explain_p=*/true);
26222 return error_mark_node;
26226 /* Check any placeholder constraints against the deduced type. */
26227 if (flag_concepts && !processing_template_decl)
26228 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (auto_node))
26230 /* Use the deduced type to check the associated constraints. If we
26231 have a partial-concept-id, rebuild the argument list so that
26232 we check using the extra arguments. */
26233 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
26234 tree cargs = CHECK_CONSTR_ARGS (constr);
26235 if (TREE_VEC_LENGTH (cargs) > 1)
26237 cargs = copy_node (cargs);
26238 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
26240 else
26241 cargs = targs;
26242 if (!constraints_satisfied_p (constr, cargs))
26244 if (complain & tf_warning_or_error)
26246 switch (context)
26248 case adc_unspecified:
26249 case adc_unify:
26250 error("placeholder constraints not satisfied");
26251 break;
26252 case adc_variable_type:
26253 case adc_decomp_type:
26254 error ("deduced initializer does not satisfy "
26255 "placeholder constraints");
26256 break;
26257 case adc_return_type:
26258 error ("deduced return type does not satisfy "
26259 "placeholder constraints");
26260 break;
26261 case adc_requirement:
26262 error ("deduced expression type does not satisfy "
26263 "placeholder constraints");
26264 break;
26266 diagnose_constraints (input_location, constr, targs);
26268 return error_mark_node;
26272 if (processing_template_decl && context != adc_unify)
26273 outer_targs = current_template_args ();
26274 targs = add_to_template_args (outer_targs, targs);
26275 return tsubst (type, targs, complain, NULL_TREE);
26278 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
26279 result. */
26281 tree
26282 splice_late_return_type (tree type, tree late_return_type)
26284 if (is_auto (type))
26286 if (late_return_type)
26287 return late_return_type;
26289 tree idx = get_template_parm_index (type);
26290 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
26291 /* In an abbreviated function template we didn't know we were dealing
26292 with a function template when we saw the auto return type, so update
26293 it to have the correct level. */
26294 return make_auto_1 (TYPE_IDENTIFIER (type), true);
26296 return type;
26299 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
26300 'decltype(auto)' or a deduced class template. */
26302 bool
26303 is_auto (const_tree type)
26305 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
26306 && (TYPE_IDENTIFIER (type) == auto_identifier
26307 || TYPE_IDENTIFIER (type) == decltype_auto_identifier
26308 || CLASS_PLACEHOLDER_TEMPLATE (type)))
26309 return true;
26310 else
26311 return false;
26314 /* for_each_template_parm callback for type_uses_auto. */
26317 is_auto_r (tree tp, void */*data*/)
26319 return is_auto (tp);
26322 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
26323 a use of `auto'. Returns NULL_TREE otherwise. */
26325 tree
26326 type_uses_auto (tree type)
26328 if (type == NULL_TREE)
26329 return NULL_TREE;
26330 else if (flag_concepts)
26332 /* The Concepts TS allows multiple autos in one type-specifier; just
26333 return the first one we find, do_auto_deduction will collect all of
26334 them. */
26335 if (uses_template_parms (type))
26336 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
26337 /*visited*/NULL, /*nondeduced*/true);
26338 else
26339 return NULL_TREE;
26341 else
26342 return find_type_usage (type, is_auto);
26345 /* For a given template T, return the vector of typedefs referenced
26346 in T for which access check is needed at T instantiation time.
26347 T is either a FUNCTION_DECL or a RECORD_TYPE.
26348 Those typedefs were added to T by the function
26349 append_type_to_template_for_access_check. */
26351 vec<qualified_typedef_usage_t, va_gc> *
26352 get_types_needing_access_check (tree t)
26354 tree ti;
26355 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
26357 if (!t || t == error_mark_node)
26358 return NULL;
26360 if (!(ti = get_template_info (t)))
26361 return NULL;
26363 if (CLASS_TYPE_P (t)
26364 || TREE_CODE (t) == FUNCTION_DECL)
26366 if (!TI_TEMPLATE (ti))
26367 return NULL;
26369 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
26372 return result;
26375 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
26376 tied to T. That list of typedefs will be access checked at
26377 T instantiation time.
26378 T is either a FUNCTION_DECL or a RECORD_TYPE.
26379 TYPE_DECL is a TYPE_DECL node representing a typedef.
26380 SCOPE is the scope through which TYPE_DECL is accessed.
26381 LOCATION is the location of the usage point of TYPE_DECL.
26383 This function is a subroutine of
26384 append_type_to_template_for_access_check. */
26386 static void
26387 append_type_to_template_for_access_check_1 (tree t,
26388 tree type_decl,
26389 tree scope,
26390 location_t location)
26392 qualified_typedef_usage_t typedef_usage;
26393 tree ti;
26395 if (!t || t == error_mark_node)
26396 return;
26398 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
26399 || CLASS_TYPE_P (t))
26400 && type_decl
26401 && TREE_CODE (type_decl) == TYPE_DECL
26402 && scope);
26404 if (!(ti = get_template_info (t)))
26405 return;
26407 gcc_assert (TI_TEMPLATE (ti));
26409 typedef_usage.typedef_decl = type_decl;
26410 typedef_usage.context = scope;
26411 typedef_usage.locus = location;
26413 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
26416 /* Append TYPE_DECL to the template TEMPL.
26417 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
26418 At TEMPL instanciation time, TYPE_DECL will be checked to see
26419 if it can be accessed through SCOPE.
26420 LOCATION is the location of the usage point of TYPE_DECL.
26422 e.g. consider the following code snippet:
26424 class C
26426 typedef int myint;
26429 template<class U> struct S
26431 C::myint mi; // <-- usage point of the typedef C::myint
26434 S<char> s;
26436 At S<char> instantiation time, we need to check the access of C::myint
26437 In other words, we need to check the access of the myint typedef through
26438 the C scope. For that purpose, this function will add the myint typedef
26439 and the scope C through which its being accessed to a list of typedefs
26440 tied to the template S. That list will be walked at template instantiation
26441 time and access check performed on each typedefs it contains.
26442 Note that this particular code snippet should yield an error because
26443 myint is private to C. */
26445 void
26446 append_type_to_template_for_access_check (tree templ,
26447 tree type_decl,
26448 tree scope,
26449 location_t location)
26451 qualified_typedef_usage_t *iter;
26452 unsigned i;
26454 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
26456 /* Make sure we don't append the type to the template twice. */
26457 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
26458 if (iter->typedef_decl == type_decl && scope == iter->context)
26459 return;
26461 append_type_to_template_for_access_check_1 (templ, type_decl,
26462 scope, location);
26465 /* Convert the generic type parameters in PARM that match the types given in the
26466 range [START_IDX, END_IDX) from the current_template_parms into generic type
26467 packs. */
26469 tree
26470 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
26472 tree current = current_template_parms;
26473 int depth = TMPL_PARMS_DEPTH (current);
26474 current = INNERMOST_TEMPLATE_PARMS (current);
26475 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
26477 for (int i = 0; i < start_idx; ++i)
26478 TREE_VEC_ELT (replacement, i)
26479 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
26481 for (int i = start_idx; i < end_idx; ++i)
26483 /* Create a distinct parameter pack type from the current parm and add it
26484 to the replacement args to tsubst below into the generic function
26485 parameter. */
26487 tree o = TREE_TYPE (TREE_VALUE
26488 (TREE_VEC_ELT (current, i)));
26489 tree t = copy_type (o);
26490 TEMPLATE_TYPE_PARM_INDEX (t)
26491 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
26492 o, 0, 0, tf_none);
26493 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
26494 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
26495 TYPE_MAIN_VARIANT (t) = t;
26496 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
26497 TYPE_CANONICAL (t) = canonical_type_parameter (t);
26498 TREE_VEC_ELT (replacement, i) = t;
26499 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
26502 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
26503 TREE_VEC_ELT (replacement, i)
26504 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
26506 /* If there are more levels then build up the replacement with the outer
26507 template parms. */
26508 if (depth > 1)
26509 replacement = add_to_template_args (template_parms_to_args
26510 (TREE_CHAIN (current_template_parms)),
26511 replacement);
26513 return tsubst (parm, replacement, tf_none, NULL_TREE);
26516 /* Entries in the decl_constraint hash table. */
26517 struct GTY((for_user)) constr_entry
26519 tree decl;
26520 tree ci;
26523 /* Hashing function and equality for constraint entries. */
26524 struct constr_hasher : ggc_ptr_hash<constr_entry>
26526 static hashval_t hash (constr_entry *e)
26528 return (hashval_t)DECL_UID (e->decl);
26531 static bool equal (constr_entry *e1, constr_entry *e2)
26533 return e1->decl == e2->decl;
26537 /* A mapping from declarations to constraint information. Note that
26538 both templates and their underlying declarations are mapped to the
26539 same constraint information.
26541 FIXME: This is defined in pt.c because garbage collection
26542 code is not being generated for constraint.cc. */
26544 static GTY (()) hash_table<constr_hasher> *decl_constraints;
26546 /* Returns the template constraints of declaration T. If T is not
26547 constrained, return NULL_TREE. Note that T must be non-null. */
26549 tree
26550 get_constraints (tree t)
26552 if (!flag_concepts)
26553 return NULL_TREE;
26555 gcc_assert (DECL_P (t));
26556 if (TREE_CODE (t) == TEMPLATE_DECL)
26557 t = DECL_TEMPLATE_RESULT (t);
26558 constr_entry elt = { t, NULL_TREE };
26559 constr_entry* found = decl_constraints->find (&elt);
26560 if (found)
26561 return found->ci;
26562 else
26563 return NULL_TREE;
26566 /* Associate the given constraint information CI with the declaration
26567 T. If T is a template, then the constraints are associated with
26568 its underlying declaration. Don't build associations if CI is
26569 NULL_TREE. */
26571 void
26572 set_constraints (tree t, tree ci)
26574 if (!ci)
26575 return;
26576 gcc_assert (t && flag_concepts);
26577 if (TREE_CODE (t) == TEMPLATE_DECL)
26578 t = DECL_TEMPLATE_RESULT (t);
26579 gcc_assert (!get_constraints (t));
26580 constr_entry elt = {t, ci};
26581 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
26582 constr_entry* entry = ggc_alloc<constr_entry> ();
26583 *entry = elt;
26584 *slot = entry;
26587 /* Remove the associated constraints of the declaration T. */
26589 void
26590 remove_constraints (tree t)
26592 gcc_assert (DECL_P (t));
26593 if (TREE_CODE (t) == TEMPLATE_DECL)
26594 t = DECL_TEMPLATE_RESULT (t);
26596 constr_entry elt = {t, NULL_TREE};
26597 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
26598 if (slot)
26599 decl_constraints->clear_slot (slot);
26602 /* Memoized satisfaction results for declarations. This
26603 maps the pair (constraint_info, arguments) to the result computed
26604 by constraints_satisfied_p. */
26606 struct GTY((for_user)) constraint_sat_entry
26608 tree ci;
26609 tree args;
26610 tree result;
26613 /* Hashing function and equality for constraint entries. */
26615 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
26617 static hashval_t hash (constraint_sat_entry *e)
26619 hashval_t val = iterative_hash_object(e->ci, 0);
26620 return iterative_hash_template_arg (e->args, val);
26623 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
26625 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
26629 /* Memoized satisfaction results for concept checks. */
26631 struct GTY((for_user)) concept_spec_entry
26633 tree tmpl;
26634 tree args;
26635 tree result;
26638 /* Hashing function and equality for constraint entries. */
26640 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
26642 static hashval_t hash (concept_spec_entry *e)
26644 return hash_tmpl_and_args (e->tmpl, e->args);
26647 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
26649 ++comparing_specializations;
26650 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
26651 --comparing_specializations;
26652 return eq;
26656 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
26657 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
26659 /* Search for a memoized satisfaction result. Returns one of the
26660 truth value nodes if previously memoized, or NULL_TREE otherwise. */
26662 tree
26663 lookup_constraint_satisfaction (tree ci, tree args)
26665 constraint_sat_entry elt = { ci, args, NULL_TREE };
26666 constraint_sat_entry* found = constraint_memos->find (&elt);
26667 if (found)
26668 return found->result;
26669 else
26670 return NULL_TREE;
26673 /* Memoize the result of a satisfication test. Returns the saved result. */
26675 tree
26676 memoize_constraint_satisfaction (tree ci, tree args, tree result)
26678 constraint_sat_entry elt = {ci, args, result};
26679 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
26680 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
26681 *entry = elt;
26682 *slot = entry;
26683 return result;
26686 /* Search for a memoized satisfaction result for a concept. */
26688 tree
26689 lookup_concept_satisfaction (tree tmpl, tree args)
26691 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26692 concept_spec_entry* found = concept_memos->find (&elt);
26693 if (found)
26694 return found->result;
26695 else
26696 return NULL_TREE;
26699 /* Memoize the result of a concept check. Returns the saved result. */
26701 tree
26702 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
26704 concept_spec_entry elt = {tmpl, args, result};
26705 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
26706 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26707 *entry = elt;
26708 *slot = entry;
26709 return result;
26712 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
26714 /* Returns a prior concept specialization. This returns the substituted
26715 and normalized constraints defined by the concept. */
26717 tree
26718 get_concept_expansion (tree tmpl, tree args)
26720 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26721 concept_spec_entry* found = concept_expansions->find (&elt);
26722 if (found)
26723 return found->result;
26724 else
26725 return NULL_TREE;
26728 /* Save a concept expansion for later. */
26730 tree
26731 save_concept_expansion (tree tmpl, tree args, tree def)
26733 concept_spec_entry elt = {tmpl, args, def};
26734 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
26735 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26736 *entry = elt;
26737 *slot = entry;
26738 return def;
26741 static hashval_t
26742 hash_subsumption_args (tree t1, tree t2)
26744 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
26745 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
26746 int val = 0;
26747 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
26748 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
26749 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
26750 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
26751 return val;
26754 /* Compare the constraints of two subsumption entries. The LEFT1 and
26755 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
26756 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
26758 static bool
26759 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
26761 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
26762 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
26763 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
26764 CHECK_CONSTR_ARGS (right1)))
26765 return comp_template_args (CHECK_CONSTR_ARGS (left2),
26766 CHECK_CONSTR_ARGS (right2));
26767 return false;
26770 /* Key/value pair for learning and memoizing subsumption results. This
26771 associates a pair of check constraints (including arguments) with
26772 a boolean value indicating the result. */
26774 struct GTY((for_user)) subsumption_entry
26776 tree t1;
26777 tree t2;
26778 bool result;
26781 /* Hashing function and equality for constraint entries. */
26783 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
26785 static hashval_t hash (subsumption_entry *e)
26787 return hash_subsumption_args (e->t1, e->t2);
26790 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
26792 ++comparing_specializations;
26793 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
26794 --comparing_specializations;
26795 return eq;
26799 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
26801 /* Search for a previously cached subsumption result. */
26803 bool*
26804 lookup_subsumption_result (tree t1, tree t2)
26806 subsumption_entry elt = { t1, t2, false };
26807 subsumption_entry* found = subsumption_table->find (&elt);
26808 if (found)
26809 return &found->result;
26810 else
26811 return 0;
26814 /* Save a subsumption result. */
26816 bool
26817 save_subsumption_result (tree t1, tree t2, bool result)
26819 subsumption_entry elt = {t1, t2, result};
26820 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
26821 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
26822 *entry = elt;
26823 *slot = entry;
26824 return result;
26827 /* Set up the hash table for constraint association. */
26829 void
26830 init_constraint_processing (void)
26832 if (!flag_concepts)
26833 return;
26835 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
26836 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
26837 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
26838 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
26839 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
26842 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
26843 0..N-1. */
26845 void
26846 declare_integer_pack (void)
26848 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
26849 build_function_type_list (integer_type_node,
26850 integer_type_node,
26851 NULL_TREE),
26852 NULL_TREE, ECF_CONST);
26853 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
26854 DECL_BUILT_IN_CLASS (ipfn) = BUILT_IN_FRONTEND;
26857 /* Set up the hash tables for template instantiations. */
26859 void
26860 init_template_processing (void)
26862 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
26863 type_specializations = hash_table<spec_hasher>::create_ggc (37);
26865 if (cxx_dialect >= cxx11)
26866 declare_integer_pack ();
26869 /* Print stats about the template hash tables for -fstats. */
26871 void
26872 print_template_statistics (void)
26874 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
26875 "%f collisions\n", (long) decl_specializations->size (),
26876 (long) decl_specializations->elements (),
26877 decl_specializations->collisions ());
26878 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
26879 "%f collisions\n", (long) type_specializations->size (),
26880 (long) type_specializations->elements (),
26881 type_specializations->collisions ());
26884 #if CHECKING_P
26886 namespace selftest {
26888 /* Verify that build_non_dependent_expr () works, for various expressions,
26889 and that location wrappers don't affect the results. */
26891 static void
26892 test_build_non_dependent_expr ()
26894 location_t loc = BUILTINS_LOCATION;
26896 /* Verify constants, without and with location wrappers. */
26897 tree int_cst = build_int_cst (integer_type_node, 42);
26898 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
26900 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
26901 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
26902 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
26904 tree string_lit = build_string (4, "foo");
26905 TREE_TYPE (string_lit) = char_array_type_node;
26906 string_lit = fix_string_type (string_lit);
26907 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
26909 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
26910 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
26911 ASSERT_EQ (wrapped_string_lit,
26912 build_non_dependent_expr (wrapped_string_lit));
26915 /* Verify that type_dependent_expression_p () works correctly, even
26916 in the presence of location wrapper nodes. */
26918 static void
26919 test_type_dependent_expression_p ()
26921 location_t loc = BUILTINS_LOCATION;
26923 tree name = get_identifier ("foo");
26925 /* If no templates are involved, nothing is type-dependent. */
26926 gcc_assert (!processing_template_decl);
26927 ASSERT_FALSE (type_dependent_expression_p (name));
26929 ++processing_template_decl;
26931 /* Within a template, an unresolved name is always type-dependent. */
26932 ASSERT_TRUE (type_dependent_expression_p (name));
26934 /* Ensure it copes with NULL_TREE and errors. */
26935 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
26936 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
26938 /* A USING_DECL in a template should be type-dependent, even if wrapped
26939 with a location wrapper (PR c++/83799). */
26940 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
26941 TREE_TYPE (using_decl) = integer_type_node;
26942 ASSERT_TRUE (type_dependent_expression_p (using_decl));
26943 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
26944 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
26945 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
26947 --processing_template_decl;
26950 /* Run all of the selftests within this file. */
26952 void
26953 cp_pt_c_tests ()
26955 test_build_non_dependent_expr ();
26956 test_type_dependent_expression_p ();
26959 } // namespace selftest
26961 #endif /* #if CHECKING_P */
26963 #include "gt-cp-pt.h"