libgo: add misc/cgo files
[official-gcc.git] / gcc / cp / pt.c
blob43f9ca8b39e6b9ab4aab69bf27e3028814c78370
1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2017 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"
44 /* The type of functions taking a tree, and some additional data, and
45 returning an int. */
46 typedef int (*tree_fn_t) (tree, void*);
48 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
49 instantiations have been deferred, either because their definitions
50 were not yet available, or because we were putting off doing the work. */
51 struct GTY ((chain_next ("%h.next"))) pending_template {
52 struct pending_template *next;
53 struct tinst_level *tinst;
56 static GTY(()) struct pending_template *pending_templates;
57 static GTY(()) struct pending_template *last_pending_template;
59 int processing_template_parmlist;
60 static int template_header_count;
62 static GTY(()) tree saved_trees;
63 static vec<int> inline_parm_levels;
65 static GTY(()) struct tinst_level *current_tinst_level;
67 static GTY(()) tree saved_access_scope;
69 /* Live only within one (recursive) call to tsubst_expr. We use
70 this to pass the statement expression node from the STMT_EXPR
71 to the EXPR_STMT that is its result. */
72 static tree cur_stmt_expr;
74 // -------------------------------------------------------------------------- //
75 // Local Specialization Stack
77 // Implementation of the RAII helper for creating new local
78 // specializations.
79 local_specialization_stack::local_specialization_stack ()
80 : saved (local_specializations)
82 local_specializations = new hash_map<tree, tree>;
85 local_specialization_stack::~local_specialization_stack ()
87 delete local_specializations;
88 local_specializations = saved;
91 /* True if we've recursed into fn_type_unification too many times. */
92 static bool excessive_deduction_depth;
94 struct GTY((for_user)) spec_entry
96 tree tmpl;
97 tree args;
98 tree spec;
101 struct spec_hasher : ggc_ptr_hash<spec_entry>
103 static hashval_t hash (spec_entry *);
104 static bool equal (spec_entry *, spec_entry *);
107 static GTY (()) hash_table<spec_hasher> *decl_specializations;
109 static GTY (()) hash_table<spec_hasher> *type_specializations;
111 /* Contains canonical template parameter types. The vector is indexed by
112 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
113 TREE_LIST, whose TREE_VALUEs contain the canonical template
114 parameters of various types and levels. */
115 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
117 #define UNIFY_ALLOW_NONE 0
118 #define UNIFY_ALLOW_MORE_CV_QUAL 1
119 #define UNIFY_ALLOW_LESS_CV_QUAL 2
120 #define UNIFY_ALLOW_DERIVED 4
121 #define UNIFY_ALLOW_INTEGER 8
122 #define UNIFY_ALLOW_OUTER_LEVEL 16
123 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
124 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
126 enum template_base_result {
127 tbr_incomplete_type,
128 tbr_ambiguous_baseclass,
129 tbr_success
132 static void push_access_scope (tree);
133 static void pop_access_scope (tree);
134 static bool resolve_overloaded_unification (tree, tree, tree, tree,
135 unification_kind_t, int,
136 bool);
137 static int try_one_overload (tree, tree, tree, tree, tree,
138 unification_kind_t, int, bool, bool);
139 static int unify (tree, tree, tree, tree, int, bool);
140 static void add_pending_template (tree);
141 static tree reopen_tinst_level (struct tinst_level *);
142 static tree tsubst_initializer_list (tree, tree);
143 static tree get_partial_spec_bindings (tree, tree, tree);
144 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
145 bool, bool);
146 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
147 bool, bool);
148 static void tsubst_enum (tree, tree, tree);
149 static tree add_to_template_args (tree, tree);
150 static tree add_outermost_template_args (tree, tree);
151 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
152 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
153 tree);
154 static int type_unification_real (tree, tree, tree, const tree *,
155 unsigned int, int, unification_kind_t, int,
156 vec<deferred_access_check, va_gc> **,
157 bool);
158 static void note_template_header (int);
159 static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
160 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
161 static tree convert_template_argument (tree, tree, tree,
162 tsubst_flags_t, int, tree);
163 static tree for_each_template_parm (tree, tree_fn_t, void*,
164 hash_set<tree> *, bool, tree_fn_t = NULL);
165 static tree expand_template_argument_pack (tree);
166 static tree build_template_parm_index (int, int, int, tree, tree);
167 static bool inline_needs_template_parms (tree, bool);
168 static void push_inline_template_parms_recursive (tree, int);
169 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
170 static int mark_template_parm (tree, void *);
171 static int template_parm_this_level_p (tree, void *);
172 static tree tsubst_friend_function (tree, tree);
173 static tree tsubst_friend_class (tree, tree);
174 static int can_complete_type_without_circularity (tree);
175 static tree get_bindings (tree, tree, tree, bool);
176 static int template_decl_level (tree);
177 static int check_cv_quals_for_unify (int, tree, tree);
178 static void template_parm_level_and_index (tree, int*, int*);
179 static int unify_pack_expansion (tree, tree, tree,
180 tree, unification_kind_t, bool, bool);
181 static tree copy_template_args (tree);
182 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
183 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
184 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
185 static tree most_specialized_partial_spec (tree, tsubst_flags_t);
186 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
187 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
188 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
189 static bool check_specialization_scope (void);
190 static tree process_partial_specialization (tree);
191 static void set_current_access_from_decl (tree);
192 static enum template_base_result get_template_base (tree, tree, tree, tree,
193 bool , tree *);
194 static tree try_class_unification (tree, tree, tree, tree, bool);
195 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
196 tree, tree);
197 static bool template_template_parm_bindings_ok_p (tree, tree);
198 static void tsubst_default_arguments (tree, tsubst_flags_t);
199 static tree for_each_template_parm_r (tree *, int *, void *);
200 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
201 static void copy_default_args_to_explicit_spec (tree);
202 static int invalid_nontype_parm_type_p (tree, tsubst_flags_t);
203 static bool dependent_template_arg_p (tree);
204 static bool any_template_arguments_need_structural_equality_p (tree);
205 static bool dependent_type_p_r (tree);
206 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
207 static tree tsubst_decl (tree, tree, tsubst_flags_t);
208 static void perform_typedefs_access_check (tree tmpl, tree targs);
209 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
210 location_t);
211 static tree listify (tree);
212 static tree listify_autos (tree, tree);
213 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
214 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
215 static bool complex_alias_template_p (const_tree tmpl);
216 static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
217 static tree canonicalize_expr_argument (tree, tsubst_flags_t);
218 static tree make_argument_pack (tree);
220 /* Make the current scope suitable for access checking when we are
221 processing T. T can be FUNCTION_DECL for instantiated function
222 template, VAR_DECL for static member variable, or TYPE_DECL for
223 alias template (needed by instantiate_decl). */
225 static void
226 push_access_scope (tree t)
228 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
229 || TREE_CODE (t) == TYPE_DECL);
231 if (DECL_FRIEND_CONTEXT (t))
232 push_nested_class (DECL_FRIEND_CONTEXT (t));
233 else if (DECL_CLASS_SCOPE_P (t))
234 push_nested_class (DECL_CONTEXT (t));
235 else
236 push_to_top_level ();
238 if (TREE_CODE (t) == FUNCTION_DECL)
240 saved_access_scope = tree_cons
241 (NULL_TREE, current_function_decl, saved_access_scope);
242 current_function_decl = t;
246 /* Restore the scope set up by push_access_scope. T is the node we
247 are processing. */
249 static void
250 pop_access_scope (tree t)
252 if (TREE_CODE (t) == FUNCTION_DECL)
254 current_function_decl = TREE_VALUE (saved_access_scope);
255 saved_access_scope = TREE_CHAIN (saved_access_scope);
258 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
259 pop_nested_class ();
260 else
261 pop_from_top_level ();
264 /* Do any processing required when DECL (a member template
265 declaration) is finished. Returns the TEMPLATE_DECL corresponding
266 to DECL, unless it is a specialization, in which case the DECL
267 itself is returned. */
269 tree
270 finish_member_template_decl (tree decl)
272 if (decl == error_mark_node)
273 return error_mark_node;
275 gcc_assert (DECL_P (decl));
277 if (TREE_CODE (decl) == TYPE_DECL)
279 tree type;
281 type = TREE_TYPE (decl);
282 if (type == error_mark_node)
283 return error_mark_node;
284 if (MAYBE_CLASS_TYPE_P (type)
285 && CLASSTYPE_TEMPLATE_INFO (type)
286 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
288 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
289 check_member_template (tmpl);
290 return tmpl;
292 return NULL_TREE;
294 else if (TREE_CODE (decl) == FIELD_DECL)
295 error ("data member %qD cannot be a member template", decl);
296 else if (DECL_TEMPLATE_INFO (decl))
298 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
300 check_member_template (DECL_TI_TEMPLATE (decl));
301 return DECL_TI_TEMPLATE (decl);
303 else
304 return decl;
306 else
307 error ("invalid member template declaration %qD", decl);
309 return error_mark_node;
312 /* Create a template info node. */
314 tree
315 build_template_info (tree template_decl, tree template_args)
317 tree result = make_node (TEMPLATE_INFO);
318 TI_TEMPLATE (result) = template_decl;
319 TI_ARGS (result) = template_args;
320 return result;
323 /* Return the template info node corresponding to T, whatever T is. */
325 tree
326 get_template_info (const_tree t)
328 tree tinfo = NULL_TREE;
330 if (!t || t == error_mark_node)
331 return NULL;
333 if (TREE_CODE (t) == NAMESPACE_DECL
334 || TREE_CODE (t) == PARM_DECL)
335 return NULL;
337 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
338 tinfo = DECL_TEMPLATE_INFO (t);
340 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
341 t = TREE_TYPE (t);
343 if (OVERLOAD_TYPE_P (t))
344 tinfo = TYPE_TEMPLATE_INFO (t);
345 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
346 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
348 return tinfo;
351 /* Returns the template nesting level of the indicated class TYPE.
353 For example, in:
354 template <class T>
355 struct A
357 template <class U>
358 struct B {};
361 A<T>::B<U> has depth two, while A<T> has depth one.
362 Both A<T>::B<int> and A<int>::B<U> have depth one, if
363 they are instantiations, not specializations.
365 This function is guaranteed to return 0 if passed NULL_TREE so
366 that, for example, `template_class_depth (current_class_type)' is
367 always safe. */
370 template_class_depth (tree type)
372 int depth;
374 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
376 tree tinfo = get_template_info (type);
378 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
379 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
380 ++depth;
382 if (DECL_P (type))
383 type = CP_DECL_CONTEXT (type);
384 else if (LAMBDA_TYPE_P (type))
385 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
386 else
387 type = CP_TYPE_CONTEXT (type);
390 return depth;
393 /* Subroutine of maybe_begin_member_template_processing.
394 Returns true if processing DECL needs us to push template parms. */
396 static bool
397 inline_needs_template_parms (tree decl, bool nsdmi)
399 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
400 return false;
402 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
403 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
406 /* Subroutine of maybe_begin_member_template_processing.
407 Push the template parms in PARMS, starting from LEVELS steps into the
408 chain, and ending at the beginning, since template parms are listed
409 innermost first. */
411 static void
412 push_inline_template_parms_recursive (tree parmlist, int levels)
414 tree parms = TREE_VALUE (parmlist);
415 int i;
417 if (levels > 1)
418 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
420 ++processing_template_decl;
421 current_template_parms
422 = tree_cons (size_int (processing_template_decl),
423 parms, current_template_parms);
424 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
426 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
427 NULL);
428 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
430 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
432 if (error_operand_p (parm))
433 continue;
435 gcc_assert (DECL_P (parm));
437 switch (TREE_CODE (parm))
439 case TYPE_DECL:
440 case TEMPLATE_DECL:
441 pushdecl (parm);
442 break;
444 case PARM_DECL:
445 /* Push the CONST_DECL. */
446 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
447 break;
449 default:
450 gcc_unreachable ();
455 /* Restore the template parameter context for a member template, a
456 friend template defined in a class definition, or a non-template
457 member of template class. */
459 void
460 maybe_begin_member_template_processing (tree decl)
462 tree parms;
463 int levels = 0;
464 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
466 if (nsdmi)
468 tree ctx = DECL_CONTEXT (decl);
469 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
470 /* Disregard full specializations (c++/60999). */
471 && uses_template_parms (ctx)
472 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
475 if (inline_needs_template_parms (decl, nsdmi))
477 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
478 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
480 if (DECL_TEMPLATE_SPECIALIZATION (decl))
482 --levels;
483 parms = TREE_CHAIN (parms);
486 push_inline_template_parms_recursive (parms, levels);
489 /* Remember how many levels of template parameters we pushed so that
490 we can pop them later. */
491 inline_parm_levels.safe_push (levels);
494 /* Undo the effects of maybe_begin_member_template_processing. */
496 void
497 maybe_end_member_template_processing (void)
499 int i;
500 int last;
502 if (inline_parm_levels.length () == 0)
503 return;
505 last = inline_parm_levels.pop ();
506 for (i = 0; i < last; ++i)
508 --processing_template_decl;
509 current_template_parms = TREE_CHAIN (current_template_parms);
510 poplevel (0, 0, 0);
514 /* Return a new template argument vector which contains all of ARGS,
515 but has as its innermost set of arguments the EXTRA_ARGS. */
517 static tree
518 add_to_template_args (tree args, tree extra_args)
520 tree new_args;
521 int extra_depth;
522 int i;
523 int j;
525 if (args == NULL_TREE || extra_args == error_mark_node)
526 return extra_args;
528 extra_depth = TMPL_ARGS_DEPTH (extra_args);
529 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
531 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
532 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
534 for (j = 1; j <= extra_depth; ++j, ++i)
535 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
537 return new_args;
540 /* Like add_to_template_args, but only the outermost ARGS are added to
541 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
542 (EXTRA_ARGS) levels are added. This function is used to combine
543 the template arguments from a partial instantiation with the
544 template arguments used to attain the full instantiation from the
545 partial instantiation. */
547 static tree
548 add_outermost_template_args (tree args, tree extra_args)
550 tree new_args;
552 /* If there are more levels of EXTRA_ARGS than there are ARGS,
553 something very fishy is going on. */
554 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
556 /* If *all* the new arguments will be the EXTRA_ARGS, just return
557 them. */
558 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
559 return extra_args;
561 /* For the moment, we make ARGS look like it contains fewer levels. */
562 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
564 new_args = add_to_template_args (args, extra_args);
566 /* Now, we restore ARGS to its full dimensions. */
567 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
569 return new_args;
572 /* Return the N levels of innermost template arguments from the ARGS. */
574 tree
575 get_innermost_template_args (tree args, int n)
577 tree new_args;
578 int extra_levels;
579 int i;
581 gcc_assert (n >= 0);
583 /* If N is 1, just return the innermost set of template arguments. */
584 if (n == 1)
585 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
587 /* If we're not removing anything, just return the arguments we were
588 given. */
589 extra_levels = TMPL_ARGS_DEPTH (args) - n;
590 gcc_assert (extra_levels >= 0);
591 if (extra_levels == 0)
592 return args;
594 /* Make a new set of arguments, not containing the outer arguments. */
595 new_args = make_tree_vec (n);
596 for (i = 1; i <= n; ++i)
597 SET_TMPL_ARGS_LEVEL (new_args, i,
598 TMPL_ARGS_LEVEL (args, i + extra_levels));
600 return new_args;
603 /* The inverse of get_innermost_template_args: Return all but the innermost
604 EXTRA_LEVELS levels of template arguments from the ARGS. */
606 static tree
607 strip_innermost_template_args (tree args, int extra_levels)
609 tree new_args;
610 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
611 int i;
613 gcc_assert (n >= 0);
615 /* If N is 1, just return the outermost set of template arguments. */
616 if (n == 1)
617 return TMPL_ARGS_LEVEL (args, 1);
619 /* If we're not removing anything, just return the arguments we were
620 given. */
621 gcc_assert (extra_levels >= 0);
622 if (extra_levels == 0)
623 return args;
625 /* Make a new set of arguments, not containing the inner arguments. */
626 new_args = make_tree_vec (n);
627 for (i = 1; i <= n; ++i)
628 SET_TMPL_ARGS_LEVEL (new_args, i,
629 TMPL_ARGS_LEVEL (args, i));
631 return new_args;
634 /* We've got a template header coming up; push to a new level for storing
635 the parms. */
637 void
638 begin_template_parm_list (void)
640 /* We use a non-tag-transparent scope here, which causes pushtag to
641 put tags in this scope, rather than in the enclosing class or
642 namespace scope. This is the right thing, since we want
643 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
644 global template class, push_template_decl handles putting the
645 TEMPLATE_DECL into top-level scope. For a nested template class,
646 e.g.:
648 template <class T> struct S1 {
649 template <class T> struct S2 {};
652 pushtag contains special code to insert the TEMPLATE_DECL for S2
653 at the right scope. */
654 begin_scope (sk_template_parms, NULL);
655 ++processing_template_decl;
656 ++processing_template_parmlist;
657 note_template_header (0);
659 /* Add a dummy parameter level while we process the parameter list. */
660 current_template_parms
661 = tree_cons (size_int (processing_template_decl),
662 make_tree_vec (0),
663 current_template_parms);
666 /* This routine is called when a specialization is declared. If it is
667 invalid to declare a specialization here, an error is reported and
668 false is returned, otherwise this routine will return true. */
670 static bool
671 check_specialization_scope (void)
673 tree scope = current_scope ();
675 /* [temp.expl.spec]
677 An explicit specialization shall be declared in the namespace of
678 which the template is a member, or, for member templates, in the
679 namespace of which the enclosing class or enclosing class
680 template is a member. An explicit specialization of a member
681 function, member class or static data member of a class template
682 shall be declared in the namespace of which the class template
683 is a member. */
684 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
686 error ("explicit specialization in non-namespace scope %qD", scope);
687 return false;
690 /* [temp.expl.spec]
692 In an explicit specialization declaration for a member of a class
693 template or a member template that appears in namespace scope,
694 the member template and some of its enclosing class templates may
695 remain unspecialized, except that the declaration shall not
696 explicitly specialize a class member template if its enclosing
697 class templates are not explicitly specialized as well. */
698 if (current_template_parms)
700 error ("enclosing class templates are not explicitly specialized");
701 return false;
704 return true;
707 /* We've just seen template <>. */
709 bool
710 begin_specialization (void)
712 begin_scope (sk_template_spec, NULL);
713 note_template_header (1);
714 return check_specialization_scope ();
717 /* Called at then end of processing a declaration preceded by
718 template<>. */
720 void
721 end_specialization (void)
723 finish_scope ();
724 reset_specialization ();
727 /* Any template <>'s that we have seen thus far are not referring to a
728 function specialization. */
730 void
731 reset_specialization (void)
733 processing_specialization = 0;
734 template_header_count = 0;
737 /* We've just seen a template header. If SPECIALIZATION is nonzero,
738 it was of the form template <>. */
740 static void
741 note_template_header (int specialization)
743 processing_specialization = specialization;
744 template_header_count++;
747 /* We're beginning an explicit instantiation. */
749 void
750 begin_explicit_instantiation (void)
752 gcc_assert (!processing_explicit_instantiation);
753 processing_explicit_instantiation = true;
757 void
758 end_explicit_instantiation (void)
760 gcc_assert (processing_explicit_instantiation);
761 processing_explicit_instantiation = false;
764 /* An explicit specialization or partial specialization of TMPL is being
765 declared. Check that the namespace in which the specialization is
766 occurring is permissible. Returns false iff it is invalid to
767 specialize TMPL in the current namespace. */
769 static bool
770 check_specialization_namespace (tree tmpl)
772 tree tpl_ns = decl_namespace_context (tmpl);
774 /* [tmpl.expl.spec]
776 An explicit specialization shall be declared in a namespace enclosing the
777 specialized template. An explicit specialization whose declarator-id is
778 not qualified shall be declared in the nearest enclosing namespace of the
779 template, or, if the namespace is inline (7.3.1), any namespace from its
780 enclosing namespace set. */
781 if (current_scope() != DECL_CONTEXT (tmpl)
782 && !at_namespace_scope_p ())
784 error ("specialization of %qD must appear at namespace scope", tmpl);
785 return false;
788 if (is_nested_namespace (current_namespace, tpl_ns, cxx_dialect < cxx11))
789 /* Same or enclosing namespace. */
790 return true;
791 else
793 permerror (input_location,
794 "specialization of %qD in different namespace", tmpl);
795 inform (DECL_SOURCE_LOCATION (tmpl),
796 " from definition of %q#D", tmpl);
797 return false;
801 /* SPEC is an explicit instantiation. Check that it is valid to
802 perform this explicit instantiation in the current namespace. */
804 static void
805 check_explicit_instantiation_namespace (tree spec)
807 tree ns;
809 /* DR 275: An explicit instantiation shall appear in an enclosing
810 namespace of its template. */
811 ns = decl_namespace_context (spec);
812 if (!is_nested_namespace (current_namespace, ns))
813 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
814 "(which does not enclose namespace %qD)",
815 spec, current_namespace, ns);
818 // Returns the type of a template specialization only if that
819 // specialization needs to be defined. Otherwise (e.g., if the type has
820 // already been defined), the function returns NULL_TREE.
821 static tree
822 maybe_new_partial_specialization (tree type)
824 // An implicit instantiation of an incomplete type implies
825 // the definition of a new class template.
827 // template<typename T>
828 // struct S;
830 // template<typename T>
831 // struct S<T*>;
833 // Here, S<T*> is an implicit instantiation of S whose type
834 // is incomplete.
835 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
836 return type;
838 // It can also be the case that TYPE is a completed specialization.
839 // Continuing the previous example, suppose we also declare:
841 // template<typename T>
842 // requires Integral<T>
843 // struct S<T*>;
845 // Here, S<T*> refers to the specialization S<T*> defined
846 // above. However, we need to differentiate definitions because
847 // we intend to define a new partial specialization. In this case,
848 // we rely on the fact that the constraints are different for
849 // this declaration than that above.
851 // Note that we also get here for injected class names and
852 // late-parsed template definitions. We must ensure that we
853 // do not create new type declarations for those cases.
854 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
856 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
857 tree args = CLASSTYPE_TI_ARGS (type);
859 // If there are no template parameters, this cannot be a new
860 // partial template specializtion?
861 if (!current_template_parms)
862 return NULL_TREE;
864 // The injected-class-name is not a new partial specialization.
865 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
866 return NULL_TREE;
868 // If the constraints are not the same as those of the primary
869 // then, we can probably create a new specialization.
870 tree type_constr = current_template_constraints ();
872 if (type == TREE_TYPE (tmpl))
874 tree main_constr = get_constraints (tmpl);
875 if (equivalent_constraints (type_constr, main_constr))
876 return NULL_TREE;
879 // Also, if there's a pre-existing specialization with matching
880 // constraints, then this also isn't new.
881 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
882 while (specs)
884 tree spec_tmpl = TREE_VALUE (specs);
885 tree spec_args = TREE_PURPOSE (specs);
886 tree spec_constr = get_constraints (spec_tmpl);
887 if (comp_template_args (args, spec_args)
888 && equivalent_constraints (type_constr, spec_constr))
889 return NULL_TREE;
890 specs = TREE_CHAIN (specs);
893 // Create a new type node (and corresponding type decl)
894 // for the newly declared specialization.
895 tree t = make_class_type (TREE_CODE (type));
896 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
897 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
899 /* We only need a separate type node for storing the definition of this
900 partial specialization; uses of S<T*> are unconstrained, so all are
901 equivalent. So keep TYPE_CANONICAL the same. */
902 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
904 // Build the corresponding type decl.
905 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
906 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
907 DECL_SOURCE_LOCATION (d) = input_location;
909 return t;
912 return NULL_TREE;
915 /* The TYPE is being declared. If it is a template type, that means it
916 is a partial specialization. Do appropriate error-checking. */
918 tree
919 maybe_process_partial_specialization (tree type)
921 tree context;
923 if (type == error_mark_node)
924 return error_mark_node;
926 /* A lambda that appears in specialization context is not itself a
927 specialization. */
928 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
929 return type;
931 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
933 error ("name of class shadows template template parameter %qD",
934 TYPE_NAME (type));
935 return error_mark_node;
938 context = TYPE_CONTEXT (type);
940 if (TYPE_ALIAS_P (type))
942 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
944 if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
945 error ("specialization of alias template %qD",
946 TI_TEMPLATE (tinfo));
947 else
948 error ("explicit specialization of non-template %qT", type);
949 return error_mark_node;
951 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
953 /* This is for ordinary explicit specialization and partial
954 specialization of a template class such as:
956 template <> class C<int>;
960 template <class T> class C<T*>;
962 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
964 if (tree t = maybe_new_partial_specialization (type))
966 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
967 && !at_namespace_scope_p ())
968 return error_mark_node;
969 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
970 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
971 if (processing_template_decl)
973 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
974 if (decl == error_mark_node)
975 return error_mark_node;
976 return TREE_TYPE (decl);
979 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
980 error ("specialization of %qT after instantiation", type);
981 else if (errorcount && !processing_specialization
982 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
983 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
984 /* Trying to define a specialization either without a template<> header
985 or in an inappropriate place. We've already given an error, so just
986 bail now so we don't actually define the specialization. */
987 return error_mark_node;
989 else if (CLASS_TYPE_P (type)
990 && !CLASSTYPE_USE_TEMPLATE (type)
991 && CLASSTYPE_TEMPLATE_INFO (type)
992 && context && CLASS_TYPE_P (context)
993 && CLASSTYPE_TEMPLATE_INFO (context))
995 /* This is for an explicit specialization of member class
996 template according to [temp.expl.spec/18]:
998 template <> template <class U> class C<int>::D;
1000 The context `C<int>' must be an implicit instantiation.
1001 Otherwise this is just a member class template declared
1002 earlier like:
1004 template <> class C<int> { template <class U> class D; };
1005 template <> template <class U> class C<int>::D;
1007 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1008 while in the second case, `C<int>::D' is a primary template
1009 and `C<T>::D' may not exist. */
1011 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1012 && !COMPLETE_TYPE_P (type))
1014 tree t;
1015 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1017 if (current_namespace
1018 != decl_namespace_context (tmpl))
1020 permerror (input_location,
1021 "specializing %q#T in different namespace", type);
1022 permerror (DECL_SOURCE_LOCATION (tmpl),
1023 " from definition of %q#D", tmpl);
1026 /* Check for invalid specialization after instantiation:
1028 template <> template <> class C<int>::D<int>;
1029 template <> template <class U> class C<int>::D; */
1031 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1032 t; t = TREE_CHAIN (t))
1034 tree inst = TREE_VALUE (t);
1035 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1036 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1038 /* We already have a full specialization of this partial
1039 instantiation, or a full specialization has been
1040 looked up but not instantiated. Reassign it to the
1041 new member specialization template. */
1042 spec_entry elt;
1043 spec_entry *entry;
1045 elt.tmpl = most_general_template (tmpl);
1046 elt.args = CLASSTYPE_TI_ARGS (inst);
1047 elt.spec = inst;
1049 type_specializations->remove_elt (&elt);
1051 elt.tmpl = tmpl;
1052 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1054 spec_entry **slot
1055 = type_specializations->find_slot (&elt, INSERT);
1056 entry = ggc_alloc<spec_entry> ();
1057 *entry = elt;
1058 *slot = entry;
1060 else
1061 /* But if we've had an implicit instantiation, that's a
1062 problem ([temp.expl.spec]/6). */
1063 error ("specialization %qT after instantiation %qT",
1064 type, inst);
1067 /* Mark TYPE as a specialization. And as a result, we only
1068 have one level of template argument for the innermost
1069 class template. */
1070 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1071 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1072 CLASSTYPE_TI_ARGS (type)
1073 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1076 else if (processing_specialization)
1078 /* Someday C++0x may allow for enum template specialization. */
1079 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1080 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1081 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1082 "of %qD not allowed by ISO C++", type);
1083 else
1085 error ("explicit specialization of non-template %qT", type);
1086 return error_mark_node;
1090 return type;
1093 /* Returns nonzero if we can optimize the retrieval of specializations
1094 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1095 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1097 static inline bool
1098 optimize_specialization_lookup_p (tree tmpl)
1100 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1101 && DECL_CLASS_SCOPE_P (tmpl)
1102 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1103 parameter. */
1104 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1105 /* The optimized lookup depends on the fact that the
1106 template arguments for the member function template apply
1107 purely to the containing class, which is not true if the
1108 containing class is an explicit or partial
1109 specialization. */
1110 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1111 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1112 && !DECL_CONV_FN_P (tmpl)
1113 /* It is possible to have a template that is not a member
1114 template and is not a member of a template class:
1116 template <typename T>
1117 struct S { friend A::f(); };
1119 Here, the friend function is a template, but the context does
1120 not have template information. The optimized lookup relies
1121 on having ARGS be the template arguments for both the class
1122 and the function template. */
1123 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1126 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1127 gone through coerce_template_parms by now. */
1129 static void
1130 verify_unstripped_args (tree args)
1132 ++processing_template_decl;
1133 if (!any_dependent_template_arguments_p (args))
1135 tree inner = INNERMOST_TEMPLATE_ARGS (args);
1136 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1138 tree arg = TREE_VEC_ELT (inner, i);
1139 if (TREE_CODE (arg) == TEMPLATE_DECL)
1140 /* OK */;
1141 else if (TYPE_P (arg))
1142 gcc_assert (strip_typedefs (arg, NULL) == arg);
1143 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1144 /* Allow typedefs on the type of a non-type argument, since a
1145 parameter can have them. */;
1146 else
1147 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1150 --processing_template_decl;
1153 /* Retrieve the specialization (in the sense of [temp.spec] - a
1154 specialization is either an instantiation or an explicit
1155 specialization) of TMPL for the given template ARGS. If there is
1156 no such specialization, return NULL_TREE. The ARGS are a vector of
1157 arguments, or a vector of vectors of arguments, in the case of
1158 templates with more than one level of parameters.
1160 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1161 then we search for a partial specialization matching ARGS. This
1162 parameter is ignored if TMPL is not a class template.
1164 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1165 result is a NONTYPE_ARGUMENT_PACK. */
1167 static tree
1168 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1170 if (tmpl == NULL_TREE)
1171 return NULL_TREE;
1173 if (args == error_mark_node)
1174 return NULL_TREE;
1176 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1177 || TREE_CODE (tmpl) == FIELD_DECL);
1179 /* There should be as many levels of arguments as there are
1180 levels of parameters. */
1181 gcc_assert (TMPL_ARGS_DEPTH (args)
1182 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1183 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1184 : template_class_depth (DECL_CONTEXT (tmpl))));
1186 if (flag_checking)
1187 verify_unstripped_args (args);
1189 if (optimize_specialization_lookup_p (tmpl))
1191 /* The template arguments actually apply to the containing
1192 class. Find the class specialization with those
1193 arguments. */
1194 tree class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1195 tree class_specialization
1196 = retrieve_specialization (class_template, args, 0);
1197 if (!class_specialization)
1198 return NULL_TREE;
1199 /* Now, find the appropriate entry in the CLASSTYPE_METHOD_VEC
1200 for the specialization. */
1201 int idx = class_method_index_for_fn (class_specialization, tmpl);
1202 if (idx == -1)
1203 return NULL_TREE;
1204 /* Iterate through the methods with the indicated name, looking
1205 for the one that has an instance of TMPL. */
1206 vec<tree, va_gc> *methods = CLASSTYPE_METHOD_VEC (class_specialization);
1207 for (ovl_iterator iter ((*methods)[idx]); iter; ++iter)
1209 tree fn = *iter;
1210 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1211 /* using-declarations can add base methods to the method vec,
1212 and we don't want those here. */
1213 && DECL_CONTEXT (fn) == class_specialization)
1214 return fn;
1216 return NULL_TREE;
1218 else
1220 spec_entry *found;
1221 spec_entry elt;
1222 hash_table<spec_hasher> *specializations;
1224 elt.tmpl = tmpl;
1225 elt.args = args;
1226 elt.spec = NULL_TREE;
1228 if (DECL_CLASS_TEMPLATE_P (tmpl))
1229 specializations = type_specializations;
1230 else
1231 specializations = decl_specializations;
1233 if (hash == 0)
1234 hash = spec_hasher::hash (&elt);
1235 found = specializations->find_with_hash (&elt, hash);
1236 if (found)
1237 return found->spec;
1240 return NULL_TREE;
1243 /* Like retrieve_specialization, but for local declarations. */
1245 tree
1246 retrieve_local_specialization (tree tmpl)
1248 if (local_specializations == NULL)
1249 return NULL_TREE;
1251 tree *slot = local_specializations->get (tmpl);
1252 return slot ? *slot : NULL_TREE;
1255 /* Returns nonzero iff DECL is a specialization of TMPL. */
1258 is_specialization_of (tree decl, tree tmpl)
1260 tree t;
1262 if (TREE_CODE (decl) == FUNCTION_DECL)
1264 for (t = decl;
1265 t != NULL_TREE;
1266 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1267 if (t == tmpl)
1268 return 1;
1270 else
1272 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1274 for (t = TREE_TYPE (decl);
1275 t != NULL_TREE;
1276 t = CLASSTYPE_USE_TEMPLATE (t)
1277 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1278 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1279 return 1;
1282 return 0;
1285 /* Returns nonzero iff DECL is a specialization of friend declaration
1286 FRIEND_DECL according to [temp.friend]. */
1288 bool
1289 is_specialization_of_friend (tree decl, tree friend_decl)
1291 bool need_template = true;
1292 int template_depth;
1294 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1295 || TREE_CODE (decl) == TYPE_DECL);
1297 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1298 of a template class, we want to check if DECL is a specialization
1299 if this. */
1300 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1301 && DECL_TEMPLATE_INFO (friend_decl)
1302 && !DECL_USE_TEMPLATE (friend_decl))
1304 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1305 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1306 need_template = false;
1308 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1309 && !PRIMARY_TEMPLATE_P (friend_decl))
1310 need_template = false;
1312 /* There is nothing to do if this is not a template friend. */
1313 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1314 return false;
1316 if (is_specialization_of (decl, friend_decl))
1317 return true;
1319 /* [temp.friend/6]
1320 A member of a class template may be declared to be a friend of a
1321 non-template class. In this case, the corresponding member of
1322 every specialization of the class template is a friend of the
1323 class granting friendship.
1325 For example, given a template friend declaration
1327 template <class T> friend void A<T>::f();
1329 the member function below is considered a friend
1331 template <> struct A<int> {
1332 void f();
1335 For this type of template friend, TEMPLATE_DEPTH below will be
1336 nonzero. To determine if DECL is a friend of FRIEND, we first
1337 check if the enclosing class is a specialization of another. */
1339 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1340 if (template_depth
1341 && DECL_CLASS_SCOPE_P (decl)
1342 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1343 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1345 /* Next, we check the members themselves. In order to handle
1346 a few tricky cases, such as when FRIEND_DECL's are
1348 template <class T> friend void A<T>::g(T t);
1349 template <class T> template <T t> friend void A<T>::h();
1351 and DECL's are
1353 void A<int>::g(int);
1354 template <int> void A<int>::h();
1356 we need to figure out ARGS, the template arguments from
1357 the context of DECL. This is required for template substitution
1358 of `T' in the function parameter of `g' and template parameter
1359 of `h' in the above examples. Here ARGS corresponds to `int'. */
1361 tree context = DECL_CONTEXT (decl);
1362 tree args = NULL_TREE;
1363 int current_depth = 0;
1365 while (current_depth < template_depth)
1367 if (CLASSTYPE_TEMPLATE_INFO (context))
1369 if (current_depth == 0)
1370 args = TYPE_TI_ARGS (context);
1371 else
1372 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1373 current_depth++;
1375 context = TYPE_CONTEXT (context);
1378 if (TREE_CODE (decl) == FUNCTION_DECL)
1380 bool is_template;
1381 tree friend_type;
1382 tree decl_type;
1383 tree friend_args_type;
1384 tree decl_args_type;
1386 /* Make sure that both DECL and FRIEND_DECL are templates or
1387 non-templates. */
1388 is_template = DECL_TEMPLATE_INFO (decl)
1389 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1390 if (need_template ^ is_template)
1391 return false;
1392 else if (is_template)
1394 /* If both are templates, check template parameter list. */
1395 tree friend_parms
1396 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1397 args, tf_none);
1398 if (!comp_template_parms
1399 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1400 friend_parms))
1401 return false;
1403 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1405 else
1406 decl_type = TREE_TYPE (decl);
1408 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1409 tf_none, NULL_TREE);
1410 if (friend_type == error_mark_node)
1411 return false;
1413 /* Check if return types match. */
1414 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1415 return false;
1417 /* Check if function parameter types match, ignoring the
1418 `this' parameter. */
1419 friend_args_type = TYPE_ARG_TYPES (friend_type);
1420 decl_args_type = TYPE_ARG_TYPES (decl_type);
1421 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1422 friend_args_type = TREE_CHAIN (friend_args_type);
1423 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1424 decl_args_type = TREE_CHAIN (decl_args_type);
1426 return compparms (decl_args_type, friend_args_type);
1428 else
1430 /* DECL is a TYPE_DECL */
1431 bool is_template;
1432 tree decl_type = TREE_TYPE (decl);
1434 /* Make sure that both DECL and FRIEND_DECL are templates or
1435 non-templates. */
1436 is_template
1437 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1438 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1440 if (need_template ^ is_template)
1441 return false;
1442 else if (is_template)
1444 tree friend_parms;
1445 /* If both are templates, check the name of the two
1446 TEMPLATE_DECL's first because is_friend didn't. */
1447 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1448 != DECL_NAME (friend_decl))
1449 return false;
1451 /* Now check template parameter list. */
1452 friend_parms
1453 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1454 args, tf_none);
1455 return comp_template_parms
1456 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1457 friend_parms);
1459 else
1460 return (DECL_NAME (decl)
1461 == DECL_NAME (friend_decl));
1464 return false;
1467 /* Register the specialization SPEC as a specialization of TMPL with
1468 the indicated ARGS. IS_FRIEND indicates whether the specialization
1469 is actually just a friend declaration. Returns SPEC, or an
1470 equivalent prior declaration, if available.
1472 We also store instantiations of field packs in the hash table, even
1473 though they are not themselves templates, to make lookup easier. */
1475 static tree
1476 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1477 hashval_t hash)
1479 tree fn;
1480 spec_entry **slot = NULL;
1481 spec_entry elt;
1483 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1484 || (TREE_CODE (tmpl) == FIELD_DECL
1485 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1487 if (TREE_CODE (spec) == FUNCTION_DECL
1488 && uses_template_parms (DECL_TI_ARGS (spec)))
1489 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1490 register it; we want the corresponding TEMPLATE_DECL instead.
1491 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1492 the more obvious `uses_template_parms (spec)' to avoid problems
1493 with default function arguments. In particular, given
1494 something like this:
1496 template <class T> void f(T t1, T t = T())
1498 the default argument expression is not substituted for in an
1499 instantiation unless and until it is actually needed. */
1500 return spec;
1502 if (optimize_specialization_lookup_p (tmpl))
1503 /* We don't put these specializations in the hash table, but we might
1504 want to give an error about a mismatch. */
1505 fn = retrieve_specialization (tmpl, args, 0);
1506 else
1508 elt.tmpl = tmpl;
1509 elt.args = args;
1510 elt.spec = spec;
1512 if (hash == 0)
1513 hash = spec_hasher::hash (&elt);
1515 slot =
1516 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1517 if (*slot)
1518 fn = ((spec_entry *) *slot)->spec;
1519 else
1520 fn = NULL_TREE;
1523 /* We can sometimes try to re-register a specialization that we've
1524 already got. In particular, regenerate_decl_from_template calls
1525 duplicate_decls which will update the specialization list. But,
1526 we'll still get called again here anyhow. It's more convenient
1527 to simply allow this than to try to prevent it. */
1528 if (fn == spec)
1529 return spec;
1530 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1532 if (DECL_TEMPLATE_INSTANTIATION (fn))
1534 if (DECL_ODR_USED (fn)
1535 || DECL_EXPLICIT_INSTANTIATION (fn))
1537 error ("specialization of %qD after instantiation",
1538 fn);
1539 return error_mark_node;
1541 else
1543 tree clone;
1544 /* This situation should occur only if the first
1545 specialization is an implicit instantiation, the
1546 second is an explicit specialization, and the
1547 implicit instantiation has not yet been used. That
1548 situation can occur if we have implicitly
1549 instantiated a member function and then specialized
1550 it later.
1552 We can also wind up here if a friend declaration that
1553 looked like an instantiation turns out to be a
1554 specialization:
1556 template <class T> void foo(T);
1557 class S { friend void foo<>(int) };
1558 template <> void foo(int);
1560 We transform the existing DECL in place so that any
1561 pointers to it become pointers to the updated
1562 declaration.
1564 If there was a definition for the template, but not
1565 for the specialization, we want this to look as if
1566 there were no definition, and vice versa. */
1567 DECL_INITIAL (fn) = NULL_TREE;
1568 duplicate_decls (spec, fn, is_friend);
1569 /* The call to duplicate_decls will have applied
1570 [temp.expl.spec]:
1572 An explicit specialization of a function template
1573 is inline only if it is explicitly declared to be,
1574 and independently of whether its function template
1577 to the primary function; now copy the inline bits to
1578 the various clones. */
1579 FOR_EACH_CLONE (clone, fn)
1581 DECL_DECLARED_INLINE_P (clone)
1582 = DECL_DECLARED_INLINE_P (fn);
1583 DECL_SOURCE_LOCATION (clone)
1584 = DECL_SOURCE_LOCATION (fn);
1585 DECL_DELETED_FN (clone)
1586 = DECL_DELETED_FN (fn);
1588 check_specialization_namespace (tmpl);
1590 return fn;
1593 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1595 tree dd = duplicate_decls (spec, fn, is_friend);
1596 if (dd == error_mark_node)
1597 /* We've already complained in duplicate_decls. */
1598 return error_mark_node;
1600 if (dd == NULL_TREE && DECL_INITIAL (spec))
1601 /* Dup decl failed, but this is a new definition. Set the
1602 line number so any errors match this new
1603 definition. */
1604 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1606 return fn;
1609 else if (fn)
1610 return duplicate_decls (spec, fn, is_friend);
1612 /* A specialization must be declared in the same namespace as the
1613 template it is specializing. */
1614 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1615 && !check_specialization_namespace (tmpl))
1616 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1618 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1620 spec_entry *entry = ggc_alloc<spec_entry> ();
1621 gcc_assert (tmpl && args && spec);
1622 *entry = elt;
1623 *slot = entry;
1624 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1625 && PRIMARY_TEMPLATE_P (tmpl)
1626 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1627 || variable_template_p (tmpl))
1628 /* If TMPL is a forward declaration of a template function, keep a list
1629 of all specializations in case we need to reassign them to a friend
1630 template later in tsubst_friend_function.
1632 Also keep a list of all variable template instantiations so that
1633 process_partial_specialization can check whether a later partial
1634 specialization would have used it. */
1635 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1636 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1639 return spec;
1642 /* Returns true iff two spec_entry nodes are equivalent. */
1644 int comparing_specializations;
1646 bool
1647 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1649 int equal;
1651 ++comparing_specializations;
1652 equal = (e1->tmpl == e2->tmpl
1653 && comp_template_args (e1->args, e2->args));
1654 if (equal && flag_concepts
1655 /* tmpl could be a FIELD_DECL for a capture pack. */
1656 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1657 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1658 && uses_template_parms (e1->args))
1660 /* Partial specializations of a variable template can be distinguished by
1661 constraints. */
1662 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1663 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1664 equal = equivalent_constraints (c1, c2);
1666 --comparing_specializations;
1668 return equal;
1671 /* Returns a hash for a template TMPL and template arguments ARGS. */
1673 static hashval_t
1674 hash_tmpl_and_args (tree tmpl, tree args)
1676 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1677 return iterative_hash_template_arg (args, val);
1680 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1681 ignoring SPEC. */
1683 hashval_t
1684 spec_hasher::hash (spec_entry *e)
1686 return hash_tmpl_and_args (e->tmpl, e->args);
1689 /* Recursively calculate a hash value for a template argument ARG, for use
1690 in the hash tables of template specializations. */
1692 hashval_t
1693 iterative_hash_template_arg (tree arg, hashval_t val)
1695 unsigned HOST_WIDE_INT i;
1696 enum tree_code code;
1697 char tclass;
1699 if (arg == NULL_TREE)
1700 return iterative_hash_object (arg, val);
1702 if (!TYPE_P (arg))
1703 STRIP_NOPS (arg);
1705 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1706 gcc_unreachable ();
1708 code = TREE_CODE (arg);
1709 tclass = TREE_CODE_CLASS (code);
1711 val = iterative_hash_object (code, val);
1713 switch (code)
1715 case ERROR_MARK:
1716 return val;
1718 case IDENTIFIER_NODE:
1719 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1721 case TREE_VEC:
1723 int i, len = TREE_VEC_LENGTH (arg);
1724 for (i = 0; i < len; ++i)
1725 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1726 return val;
1729 case TYPE_PACK_EXPANSION:
1730 case EXPR_PACK_EXPANSION:
1731 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1732 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1734 case TYPE_ARGUMENT_PACK:
1735 case NONTYPE_ARGUMENT_PACK:
1736 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1738 case TREE_LIST:
1739 for (; arg; arg = TREE_CHAIN (arg))
1740 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1741 return val;
1743 case OVERLOAD:
1744 for (lkp_iterator iter (arg); iter; ++iter)
1745 val = iterative_hash_template_arg (*iter, val);
1746 return val;
1748 case CONSTRUCTOR:
1750 tree field, value;
1751 iterative_hash_template_arg (TREE_TYPE (arg), val);
1752 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1754 val = iterative_hash_template_arg (field, val);
1755 val = iterative_hash_template_arg (value, val);
1757 return val;
1760 case PARM_DECL:
1761 if (!DECL_ARTIFICIAL (arg))
1763 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1764 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1766 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1768 case TARGET_EXPR:
1769 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1771 case PTRMEM_CST:
1772 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1773 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1775 case TEMPLATE_PARM_INDEX:
1776 val = iterative_hash_template_arg
1777 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1778 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1779 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1781 case TRAIT_EXPR:
1782 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1783 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1784 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1786 case BASELINK:
1787 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1788 val);
1789 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1790 val);
1792 case MODOP_EXPR:
1793 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1794 code = TREE_CODE (TREE_OPERAND (arg, 1));
1795 val = iterative_hash_object (code, val);
1796 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1798 case LAMBDA_EXPR:
1799 /* A lambda can't appear in a template arg, but don't crash on
1800 erroneous input. */
1801 gcc_assert (seen_error ());
1802 return val;
1804 case CAST_EXPR:
1805 case IMPLICIT_CONV_EXPR:
1806 case STATIC_CAST_EXPR:
1807 case REINTERPRET_CAST_EXPR:
1808 case CONST_CAST_EXPR:
1809 case DYNAMIC_CAST_EXPR:
1810 case NEW_EXPR:
1811 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1812 /* Now hash operands as usual. */
1813 break;
1815 default:
1816 break;
1819 switch (tclass)
1821 case tcc_type:
1822 if (alias_template_specialization_p (arg))
1824 // We want an alias specialization that survived strip_typedefs
1825 // to hash differently from its TYPE_CANONICAL, to avoid hash
1826 // collisions that compare as different in template_args_equal.
1827 // These could be dependent specializations that strip_typedefs
1828 // left alone, or untouched specializations because
1829 // coerce_template_parms returns the unconverted template
1830 // arguments if it sees incomplete argument packs.
1831 tree ti = TYPE_ALIAS_TEMPLATE_INFO (arg);
1832 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1834 if (TYPE_CANONICAL (arg))
1835 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1836 val);
1837 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1838 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1839 /* Otherwise just compare the types during lookup. */
1840 return val;
1842 case tcc_declaration:
1843 case tcc_constant:
1844 return iterative_hash_expr (arg, val);
1846 default:
1847 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1849 unsigned n = cp_tree_operand_length (arg);
1850 for (i = 0; i < n; ++i)
1851 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1852 return val;
1855 gcc_unreachable ();
1856 return 0;
1859 /* Unregister the specialization SPEC as a specialization of TMPL.
1860 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1861 if the SPEC was listed as a specialization of TMPL.
1863 Note that SPEC has been ggc_freed, so we can't look inside it. */
1865 bool
1866 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1868 spec_entry *entry;
1869 spec_entry elt;
1871 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1872 elt.args = TI_ARGS (tinfo);
1873 elt.spec = NULL_TREE;
1875 entry = decl_specializations->find (&elt);
1876 if (entry != NULL)
1878 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1879 gcc_assert (new_spec != NULL_TREE);
1880 entry->spec = new_spec;
1881 return 1;
1884 return 0;
1887 /* Like register_specialization, but for local declarations. We are
1888 registering SPEC, an instantiation of TMPL. */
1890 void
1891 register_local_specialization (tree spec, tree tmpl)
1893 local_specializations->put (tmpl, spec);
1896 /* TYPE is a class type. Returns true if TYPE is an explicitly
1897 specialized class. */
1899 bool
1900 explicit_class_specialization_p (tree type)
1902 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1903 return false;
1904 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1907 /* Print the list of functions at FNS, going through all the overloads
1908 for each element of the list. Alternatively, FNS can not be a
1909 TREE_LIST, in which case it will be printed together with all the
1910 overloads.
1912 MORE and *STR should respectively be FALSE and NULL when the function
1913 is called from the outside. They are used internally on recursive
1914 calls. print_candidates manages the two parameters and leaves NULL
1915 in *STR when it ends. */
1917 static void
1918 print_candidates_1 (tree fns, char **str, bool more = false)
1920 if (TREE_CODE (fns) == TREE_LIST)
1921 for (; fns; fns = TREE_CHAIN (fns))
1922 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
1923 else
1924 for (lkp_iterator iter (fns); iter;)
1926 tree cand = *iter;
1927 ++iter;
1929 const char *pfx = *str;
1930 if (!pfx)
1932 if (more || iter)
1933 pfx = _("candidates are:");
1934 else
1935 pfx = _("candidate is:");
1936 *str = get_spaces (pfx);
1938 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
1942 /* Print the list of candidate FNS in an error message. FNS can also
1943 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1945 void
1946 print_candidates (tree fns)
1948 char *str = NULL;
1949 print_candidates_1 (fns, &str);
1950 free (str);
1953 /* Get a (possibly) constrained template declaration for the
1954 purpose of ordering candidates. */
1955 static tree
1956 get_template_for_ordering (tree list)
1958 gcc_assert (TREE_CODE (list) == TREE_LIST);
1959 tree f = TREE_VALUE (list);
1960 if (tree ti = DECL_TEMPLATE_INFO (f))
1961 return TI_TEMPLATE (ti);
1962 return f;
1965 /* Among candidates having the same signature, return the
1966 most constrained or NULL_TREE if there is no best candidate.
1967 If the signatures of candidates vary (e.g., template
1968 specialization vs. member function), then there can be no
1969 most constrained.
1971 Note that we don't compare constraints on the functions
1972 themselves, but rather those of their templates. */
1973 static tree
1974 most_constrained_function (tree candidates)
1976 // Try to find the best candidate in a first pass.
1977 tree champ = candidates;
1978 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
1980 int winner = more_constrained (get_template_for_ordering (champ),
1981 get_template_for_ordering (c));
1982 if (winner == -1)
1983 champ = c; // The candidate is more constrained
1984 else if (winner == 0)
1985 return NULL_TREE; // Neither is more constrained
1988 // Verify that the champ is better than previous candidates.
1989 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
1990 if (!more_constrained (get_template_for_ordering (champ),
1991 get_template_for_ordering (c)))
1992 return NULL_TREE;
1995 return champ;
1999 /* Returns the template (one of the functions given by TEMPLATE_ID)
2000 which can be specialized to match the indicated DECL with the
2001 explicit template args given in TEMPLATE_ID. The DECL may be
2002 NULL_TREE if none is available. In that case, the functions in
2003 TEMPLATE_ID are non-members.
2005 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2006 specialization of a member template.
2008 The TEMPLATE_COUNT is the number of references to qualifying
2009 template classes that appeared in the name of the function. See
2010 check_explicit_specialization for a more accurate description.
2012 TSK indicates what kind of template declaration (if any) is being
2013 declared. TSK_TEMPLATE indicates that the declaration given by
2014 DECL, though a FUNCTION_DECL, has template parameters, and is
2015 therefore a template function.
2017 The template args (those explicitly specified and those deduced)
2018 are output in a newly created vector *TARGS_OUT.
2020 If it is impossible to determine the result, an error message is
2021 issued. The error_mark_node is returned to indicate failure. */
2023 static tree
2024 determine_specialization (tree template_id,
2025 tree decl,
2026 tree* targs_out,
2027 int need_member_template,
2028 int template_count,
2029 tmpl_spec_kind tsk)
2031 tree fns;
2032 tree targs;
2033 tree explicit_targs;
2034 tree candidates = NULL_TREE;
2036 /* A TREE_LIST of templates of which DECL may be a specialization.
2037 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2038 corresponding TREE_PURPOSE is the set of template arguments that,
2039 when used to instantiate the template, would produce a function
2040 with the signature of DECL. */
2041 tree templates = NULL_TREE;
2042 int header_count;
2043 cp_binding_level *b;
2045 *targs_out = NULL_TREE;
2047 if (template_id == error_mark_node || decl == error_mark_node)
2048 return error_mark_node;
2050 /* We shouldn't be specializing a member template of an
2051 unspecialized class template; we already gave an error in
2052 check_specialization_scope, now avoid crashing. */
2053 if (template_count && DECL_CLASS_SCOPE_P (decl)
2054 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2056 gcc_assert (errorcount);
2057 return error_mark_node;
2060 fns = TREE_OPERAND (template_id, 0);
2061 explicit_targs = TREE_OPERAND (template_id, 1);
2063 if (fns == error_mark_node)
2064 return error_mark_node;
2066 /* Check for baselinks. */
2067 if (BASELINK_P (fns))
2068 fns = BASELINK_FUNCTIONS (fns);
2070 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2072 error ("%qD is not a function template", fns);
2073 return error_mark_node;
2075 else if (VAR_P (decl) && !variable_template_p (fns))
2077 error ("%qD is not a variable template", fns);
2078 return error_mark_node;
2081 /* Count the number of template headers specified for this
2082 specialization. */
2083 header_count = 0;
2084 for (b = current_binding_level;
2085 b->kind == sk_template_parms;
2086 b = b->level_chain)
2087 ++header_count;
2089 tree orig_fns = fns;
2091 if (variable_template_p (fns))
2093 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2094 targs = coerce_template_parms (parms, explicit_targs, fns,
2095 tf_warning_or_error,
2096 /*req_all*/true, /*use_defarg*/true);
2097 if (targs != error_mark_node)
2098 templates = tree_cons (targs, fns, templates);
2100 else for (lkp_iterator iter (fns); iter; ++iter)
2102 tree fn = *iter;
2104 if (TREE_CODE (fn) == TEMPLATE_DECL)
2106 tree decl_arg_types;
2107 tree fn_arg_types;
2108 tree insttype;
2110 /* In case of explicit specialization, we need to check if
2111 the number of template headers appearing in the specialization
2112 is correct. This is usually done in check_explicit_specialization,
2113 but the check done there cannot be exhaustive when specializing
2114 member functions. Consider the following code:
2116 template <> void A<int>::f(int);
2117 template <> template <> void A<int>::f(int);
2119 Assuming that A<int> is not itself an explicit specialization
2120 already, the first line specializes "f" which is a non-template
2121 member function, whilst the second line specializes "f" which
2122 is a template member function. So both lines are syntactically
2123 correct, and check_explicit_specialization does not reject
2124 them.
2126 Here, we can do better, as we are matching the specialization
2127 against the declarations. We count the number of template
2128 headers, and we check if they match TEMPLATE_COUNT + 1
2129 (TEMPLATE_COUNT is the number of qualifying template classes,
2130 plus there must be another header for the member template
2131 itself).
2133 Notice that if header_count is zero, this is not a
2134 specialization but rather a template instantiation, so there
2135 is no check we can perform here. */
2136 if (header_count && header_count != template_count + 1)
2137 continue;
2139 /* Check that the number of template arguments at the
2140 innermost level for DECL is the same as for FN. */
2141 if (current_binding_level->kind == sk_template_parms
2142 && !current_binding_level->explicit_spec_p
2143 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2144 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2145 (current_template_parms))))
2146 continue;
2148 /* DECL might be a specialization of FN. */
2149 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2150 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2152 /* For a non-static member function, we need to make sure
2153 that the const qualification is the same. Since
2154 get_bindings does not try to merge the "this" parameter,
2155 we must do the comparison explicitly. */
2156 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2157 && !same_type_p (TREE_VALUE (fn_arg_types),
2158 TREE_VALUE (decl_arg_types)))
2159 continue;
2161 /* Skip the "this" parameter and, for constructors of
2162 classes with virtual bases, the VTT parameter. A
2163 full specialization of a constructor will have a VTT
2164 parameter, but a template never will. */
2165 decl_arg_types
2166 = skip_artificial_parms_for (decl, decl_arg_types);
2167 fn_arg_types
2168 = skip_artificial_parms_for (fn, fn_arg_types);
2170 /* Function templates cannot be specializations; there are
2171 no partial specializations of functions. Therefore, if
2172 the type of DECL does not match FN, there is no
2173 match.
2175 Note that it should never be the case that we have both
2176 candidates added here, and for regular member functions
2177 below. */
2178 if (tsk == tsk_template)
2180 if (compparms (fn_arg_types, decl_arg_types))
2181 candidates = tree_cons (NULL_TREE, fn, candidates);
2182 continue;
2185 /* See whether this function might be a specialization of this
2186 template. Suppress access control because we might be trying
2187 to make this specialization a friend, and we have already done
2188 access control for the declaration of the specialization. */
2189 push_deferring_access_checks (dk_no_check);
2190 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2191 pop_deferring_access_checks ();
2193 if (!targs)
2194 /* We cannot deduce template arguments that when used to
2195 specialize TMPL will produce DECL. */
2196 continue;
2198 /* Remove, from the set of candidates, all those functions
2199 whose constraints are not satisfied. */
2200 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2201 continue;
2203 // Then, try to form the new function type.
2204 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2205 if (insttype == error_mark_node)
2206 continue;
2207 fn_arg_types
2208 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2209 if (!compparms (fn_arg_types, decl_arg_types))
2210 continue;
2212 /* Save this template, and the arguments deduced. */
2213 templates = tree_cons (targs, fn, templates);
2215 else if (need_member_template)
2216 /* FN is an ordinary member function, and we need a
2217 specialization of a member template. */
2219 else if (TREE_CODE (fn) != FUNCTION_DECL)
2220 /* We can get IDENTIFIER_NODEs here in certain erroneous
2221 cases. */
2223 else if (!DECL_FUNCTION_MEMBER_P (fn))
2224 /* This is just an ordinary non-member function. Nothing can
2225 be a specialization of that. */
2227 else if (DECL_ARTIFICIAL (fn))
2228 /* Cannot specialize functions that are created implicitly. */
2230 else
2232 tree decl_arg_types;
2234 /* This is an ordinary member function. However, since
2235 we're here, we can assume its enclosing class is a
2236 template class. For example,
2238 template <typename T> struct S { void f(); };
2239 template <> void S<int>::f() {}
2241 Here, S<int>::f is a non-template, but S<int> is a
2242 template class. If FN has the same type as DECL, we
2243 might be in business. */
2245 if (!DECL_TEMPLATE_INFO (fn))
2246 /* Its enclosing class is an explicit specialization
2247 of a template class. This is not a candidate. */
2248 continue;
2250 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2251 TREE_TYPE (TREE_TYPE (fn))))
2252 /* The return types differ. */
2253 continue;
2255 /* Adjust the type of DECL in case FN is a static member. */
2256 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2257 if (DECL_STATIC_FUNCTION_P (fn)
2258 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2259 decl_arg_types = TREE_CHAIN (decl_arg_types);
2261 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2262 decl_arg_types))
2263 continue;
2265 // If the deduced arguments do not satisfy the constraints,
2266 // this is not a candidate.
2267 if (flag_concepts && !constraints_satisfied_p (fn))
2268 continue;
2270 // Add the candidate.
2271 candidates = tree_cons (NULL_TREE, fn, candidates);
2275 if (templates && TREE_CHAIN (templates))
2277 /* We have:
2279 [temp.expl.spec]
2281 It is possible for a specialization with a given function
2282 signature to be instantiated from more than one function
2283 template. In such cases, explicit specification of the
2284 template arguments must be used to uniquely identify the
2285 function template specialization being specialized.
2287 Note that here, there's no suggestion that we're supposed to
2288 determine which of the candidate templates is most
2289 specialized. However, we, also have:
2291 [temp.func.order]
2293 Partial ordering of overloaded function template
2294 declarations is used in the following contexts to select
2295 the function template to which a function template
2296 specialization refers:
2298 -- when an explicit specialization refers to a function
2299 template.
2301 So, we do use the partial ordering rules, at least for now.
2302 This extension can only serve to make invalid programs valid,
2303 so it's safe. And, there is strong anecdotal evidence that
2304 the committee intended the partial ordering rules to apply;
2305 the EDG front end has that behavior, and John Spicer claims
2306 that the committee simply forgot to delete the wording in
2307 [temp.expl.spec]. */
2308 tree tmpl = most_specialized_instantiation (templates);
2309 if (tmpl != error_mark_node)
2311 templates = tmpl;
2312 TREE_CHAIN (templates) = NULL_TREE;
2316 // Concepts allows multiple declarations of member functions
2317 // with the same signature. Like above, we need to rely on
2318 // on the partial ordering of those candidates to determine which
2319 // is the best.
2320 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2322 if (tree cand = most_constrained_function (candidates))
2324 candidates = cand;
2325 TREE_CHAIN (cand) = NULL_TREE;
2329 if (templates == NULL_TREE && candidates == NULL_TREE)
2331 error ("template-id %qD for %q+D does not match any template "
2332 "declaration", template_id, decl);
2333 if (header_count && header_count != template_count + 1)
2334 inform (input_location, "saw %d %<template<>%>, need %d for "
2335 "specializing a member function template",
2336 header_count, template_count + 1);
2337 else
2338 print_candidates (orig_fns);
2339 return error_mark_node;
2341 else if ((templates && TREE_CHAIN (templates))
2342 || (candidates && TREE_CHAIN (candidates))
2343 || (templates && candidates))
2345 error ("ambiguous template specialization %qD for %q+D",
2346 template_id, decl);
2347 candidates = chainon (candidates, templates);
2348 print_candidates (candidates);
2349 return error_mark_node;
2352 /* We have one, and exactly one, match. */
2353 if (candidates)
2355 tree fn = TREE_VALUE (candidates);
2356 *targs_out = copy_node (DECL_TI_ARGS (fn));
2358 // Propagate the candidate's constraints to the declaration.
2359 set_constraints (decl, get_constraints (fn));
2361 /* DECL is a re-declaration or partial instantiation of a template
2362 function. */
2363 if (TREE_CODE (fn) == TEMPLATE_DECL)
2364 return fn;
2365 /* It was a specialization of an ordinary member function in a
2366 template class. */
2367 return DECL_TI_TEMPLATE (fn);
2370 /* It was a specialization of a template. */
2371 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2372 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2374 *targs_out = copy_node (targs);
2375 SET_TMPL_ARGS_LEVEL (*targs_out,
2376 TMPL_ARGS_DEPTH (*targs_out),
2377 TREE_PURPOSE (templates));
2379 else
2380 *targs_out = TREE_PURPOSE (templates);
2381 return TREE_VALUE (templates);
2384 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2385 but with the default argument values filled in from those in the
2386 TMPL_TYPES. */
2388 static tree
2389 copy_default_args_to_explicit_spec_1 (tree spec_types,
2390 tree tmpl_types)
2392 tree new_spec_types;
2394 if (!spec_types)
2395 return NULL_TREE;
2397 if (spec_types == void_list_node)
2398 return void_list_node;
2400 /* Substitute into the rest of the list. */
2401 new_spec_types =
2402 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2403 TREE_CHAIN (tmpl_types));
2405 /* Add the default argument for this parameter. */
2406 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2407 TREE_VALUE (spec_types),
2408 new_spec_types);
2411 /* DECL is an explicit specialization. Replicate default arguments
2412 from the template it specializes. (That way, code like:
2414 template <class T> void f(T = 3);
2415 template <> void f(double);
2416 void g () { f (); }
2418 works, as required.) An alternative approach would be to look up
2419 the correct default arguments at the call-site, but this approach
2420 is consistent with how implicit instantiations are handled. */
2422 static void
2423 copy_default_args_to_explicit_spec (tree decl)
2425 tree tmpl;
2426 tree spec_types;
2427 tree tmpl_types;
2428 tree new_spec_types;
2429 tree old_type;
2430 tree new_type;
2431 tree t;
2432 tree object_type = NULL_TREE;
2433 tree in_charge = NULL_TREE;
2434 tree vtt = NULL_TREE;
2436 /* See if there's anything we need to do. */
2437 tmpl = DECL_TI_TEMPLATE (decl);
2438 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2439 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2440 if (TREE_PURPOSE (t))
2441 break;
2442 if (!t)
2443 return;
2445 old_type = TREE_TYPE (decl);
2446 spec_types = TYPE_ARG_TYPES (old_type);
2448 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2450 /* Remove the this pointer, but remember the object's type for
2451 CV quals. */
2452 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2453 spec_types = TREE_CHAIN (spec_types);
2454 tmpl_types = TREE_CHAIN (tmpl_types);
2456 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2458 /* DECL may contain more parameters than TMPL due to the extra
2459 in-charge parameter in constructors and destructors. */
2460 in_charge = spec_types;
2461 spec_types = TREE_CHAIN (spec_types);
2463 if (DECL_HAS_VTT_PARM_P (decl))
2465 vtt = spec_types;
2466 spec_types = TREE_CHAIN (spec_types);
2470 /* Compute the merged default arguments. */
2471 new_spec_types =
2472 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2474 /* Compute the new FUNCTION_TYPE. */
2475 if (object_type)
2477 if (vtt)
2478 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2479 TREE_VALUE (vtt),
2480 new_spec_types);
2482 if (in_charge)
2483 /* Put the in-charge parameter back. */
2484 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2485 TREE_VALUE (in_charge),
2486 new_spec_types);
2488 new_type = build_method_type_directly (object_type,
2489 TREE_TYPE (old_type),
2490 new_spec_types);
2492 else
2493 new_type = build_function_type (TREE_TYPE (old_type),
2494 new_spec_types);
2495 new_type = cp_build_type_attribute_variant (new_type,
2496 TYPE_ATTRIBUTES (old_type));
2497 new_type = build_exception_variant (new_type,
2498 TYPE_RAISES_EXCEPTIONS (old_type));
2500 if (TYPE_HAS_LATE_RETURN_TYPE (old_type))
2501 TYPE_HAS_LATE_RETURN_TYPE (new_type) = 1;
2503 TREE_TYPE (decl) = new_type;
2506 /* Return the number of template headers we expect to see for a definition
2507 or specialization of CTYPE or one of its non-template members. */
2510 num_template_headers_for_class (tree ctype)
2512 int num_templates = 0;
2514 while (ctype && CLASS_TYPE_P (ctype))
2516 /* You're supposed to have one `template <...>' for every
2517 template class, but you don't need one for a full
2518 specialization. For example:
2520 template <class T> struct S{};
2521 template <> struct S<int> { void f(); };
2522 void S<int>::f () {}
2524 is correct; there shouldn't be a `template <>' for the
2525 definition of `S<int>::f'. */
2526 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2527 /* If CTYPE does not have template information of any
2528 kind, then it is not a template, nor is it nested
2529 within a template. */
2530 break;
2531 if (explicit_class_specialization_p (ctype))
2532 break;
2533 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2534 ++num_templates;
2536 ctype = TYPE_CONTEXT (ctype);
2539 return num_templates;
2542 /* Do a simple sanity check on the template headers that precede the
2543 variable declaration DECL. */
2545 void
2546 check_template_variable (tree decl)
2548 tree ctx = CP_DECL_CONTEXT (decl);
2549 int wanted = num_template_headers_for_class (ctx);
2550 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2551 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2553 if (cxx_dialect < cxx14)
2554 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2555 "variable templates only available with "
2556 "-std=c++14 or -std=gnu++14");
2558 // Namespace-scope variable templates should have a template header.
2559 ++wanted;
2561 if (template_header_count > wanted)
2563 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2564 "too many template headers for %qD "
2565 "(should be %d)",
2566 decl, wanted);
2567 if (warned && CLASS_TYPE_P (ctx)
2568 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2569 inform (DECL_SOURCE_LOCATION (decl),
2570 "members of an explicitly specialized class are defined "
2571 "without a template header");
2575 /* An explicit specialization whose declarator-id or class-head-name is not
2576 qualified shall be declared in the nearest enclosing namespace of the
2577 template, or, if the namespace is inline (7.3.1), any namespace from its
2578 enclosing namespace set.
2580 If the name declared in the explicit instantiation is an unqualified name,
2581 the explicit instantiation shall appear in the namespace where its template
2582 is declared or, if that namespace is inline (7.3.1), any namespace from its
2583 enclosing namespace set. */
2585 void
2586 check_unqualified_spec_or_inst (tree t, location_t loc)
2588 tree tmpl = most_general_template (t);
2589 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2590 && !is_nested_namespace (current_namespace,
2591 CP_DECL_CONTEXT (tmpl), true))
2593 if (processing_specialization)
2594 permerror (loc, "explicit specialization of %qD outside its "
2595 "namespace must use a nested-name-specifier", tmpl);
2596 else if (processing_explicit_instantiation
2597 && cxx_dialect >= cxx11)
2598 /* This was allowed in C++98, so only pedwarn. */
2599 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2600 "outside its namespace must use a nested-name-"
2601 "specifier", tmpl);
2605 /* Check to see if the function just declared, as indicated in
2606 DECLARATOR, and in DECL, is a specialization of a function
2607 template. We may also discover that the declaration is an explicit
2608 instantiation at this point.
2610 Returns DECL, or an equivalent declaration that should be used
2611 instead if all goes well. Issues an error message if something is
2612 amiss. Returns error_mark_node if the error is not easily
2613 recoverable.
2615 FLAGS is a bitmask consisting of the following flags:
2617 2: The function has a definition.
2618 4: The function is a friend.
2620 The TEMPLATE_COUNT is the number of references to qualifying
2621 template classes that appeared in the name of the function. For
2622 example, in
2624 template <class T> struct S { void f(); };
2625 void S<int>::f();
2627 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2628 classes are not counted in the TEMPLATE_COUNT, so that in
2630 template <class T> struct S {};
2631 template <> struct S<int> { void f(); }
2632 template <> void S<int>::f();
2634 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2635 invalid; there should be no template <>.)
2637 If the function is a specialization, it is marked as such via
2638 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2639 is set up correctly, and it is added to the list of specializations
2640 for that template. */
2642 tree
2643 check_explicit_specialization (tree declarator,
2644 tree decl,
2645 int template_count,
2646 int flags)
2648 int have_def = flags & 2;
2649 int is_friend = flags & 4;
2650 bool is_concept = flags & 8;
2651 int specialization = 0;
2652 int explicit_instantiation = 0;
2653 int member_specialization = 0;
2654 tree ctype = DECL_CLASS_CONTEXT (decl);
2655 tree dname = DECL_NAME (decl);
2656 tmpl_spec_kind tsk;
2658 if (is_friend)
2660 if (!processing_specialization)
2661 tsk = tsk_none;
2662 else
2663 tsk = tsk_excessive_parms;
2665 else
2666 tsk = current_tmpl_spec_kind (template_count);
2668 switch (tsk)
2670 case tsk_none:
2671 if (processing_specialization && !VAR_P (decl))
2673 specialization = 1;
2674 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2676 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2678 if (is_friend)
2679 /* This could be something like:
2681 template <class T> void f(T);
2682 class S { friend void f<>(int); } */
2683 specialization = 1;
2684 else
2686 /* This case handles bogus declarations like template <>
2687 template <class T> void f<int>(); */
2689 error ("template-id %qD in declaration of primary template",
2690 declarator);
2691 return decl;
2694 break;
2696 case tsk_invalid_member_spec:
2697 /* The error has already been reported in
2698 check_specialization_scope. */
2699 return error_mark_node;
2701 case tsk_invalid_expl_inst:
2702 error ("template parameter list used in explicit instantiation");
2704 /* Fall through. */
2706 case tsk_expl_inst:
2707 if (have_def)
2708 error ("definition provided for explicit instantiation");
2710 explicit_instantiation = 1;
2711 break;
2713 case tsk_excessive_parms:
2714 case tsk_insufficient_parms:
2715 if (tsk == tsk_excessive_parms)
2716 error ("too many template parameter lists in declaration of %qD",
2717 decl);
2718 else if (template_header_count)
2719 error("too few template parameter lists in declaration of %qD", decl);
2720 else
2721 error("explicit specialization of %qD must be introduced by "
2722 "%<template <>%>", decl);
2724 /* Fall through. */
2725 case tsk_expl_spec:
2726 if (is_concept)
2727 error ("explicit specialization declared %<concept%>");
2729 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2730 /* In cases like template<> constexpr bool v = true;
2731 We'll give an error in check_template_variable. */
2732 break;
2734 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2735 if (ctype)
2736 member_specialization = 1;
2737 else
2738 specialization = 1;
2739 break;
2741 case tsk_template:
2742 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2744 /* This case handles bogus declarations like template <>
2745 template <class T> void f<int>(); */
2747 if (!uses_template_parms (declarator))
2748 error ("template-id %qD in declaration of primary template",
2749 declarator);
2750 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2752 /* Partial specialization of variable template. */
2753 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2754 specialization = 1;
2755 goto ok;
2757 else if (cxx_dialect < cxx14)
2758 error ("non-type partial specialization %qD "
2759 "is not allowed", declarator);
2760 else
2761 error ("non-class, non-variable partial specialization %qD "
2762 "is not allowed", declarator);
2763 return decl;
2764 ok:;
2767 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2768 /* This is a specialization of a member template, without
2769 specialization the containing class. Something like:
2771 template <class T> struct S {
2772 template <class U> void f (U);
2774 template <> template <class U> void S<int>::f(U) {}
2776 That's a specialization -- but of the entire template. */
2777 specialization = 1;
2778 break;
2780 default:
2781 gcc_unreachable ();
2784 if ((specialization || member_specialization)
2785 /* This doesn't apply to variable templates. */
2786 && (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE
2787 || TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE))
2789 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2790 for (; t; t = TREE_CHAIN (t))
2791 if (TREE_PURPOSE (t))
2793 permerror (input_location,
2794 "default argument specified in explicit specialization");
2795 break;
2799 if (specialization || member_specialization || explicit_instantiation)
2801 tree tmpl = NULL_TREE;
2802 tree targs = NULL_TREE;
2803 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2805 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2806 if (!was_template_id)
2808 tree fns;
2810 gcc_assert (identifier_p (declarator));
2811 if (ctype)
2812 fns = dname;
2813 else
2815 /* If there is no class context, the explicit instantiation
2816 must be at namespace scope. */
2817 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2819 /* Find the namespace binding, using the declaration
2820 context. */
2821 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2822 false, true);
2823 if (fns == error_mark_node)
2824 /* If lookup fails, look for a friend declaration so we can
2825 give a better diagnostic. */
2826 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2827 /*type*/false, /*complain*/true,
2828 /*hidden*/true);
2830 if (fns == error_mark_node || !is_overloaded_fn (fns))
2832 error ("%qD is not a template function", dname);
2833 fns = error_mark_node;
2837 declarator = lookup_template_function (fns, NULL_TREE);
2840 if (declarator == error_mark_node)
2841 return error_mark_node;
2843 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2845 if (!explicit_instantiation)
2846 /* A specialization in class scope. This is invalid,
2847 but the error will already have been flagged by
2848 check_specialization_scope. */
2849 return error_mark_node;
2850 else
2852 /* It's not valid to write an explicit instantiation in
2853 class scope, e.g.:
2855 class C { template void f(); }
2857 This case is caught by the parser. However, on
2858 something like:
2860 template class C { void f(); };
2862 (which is invalid) we can get here. The error will be
2863 issued later. */
2867 return decl;
2869 else if (ctype != NULL_TREE
2870 && (identifier_p (TREE_OPERAND (declarator, 0))))
2872 // We'll match variable templates in start_decl.
2873 if (VAR_P (decl))
2874 return decl;
2876 /* Find the list of functions in ctype that have the same
2877 name as the declared function. */
2878 tree name = TREE_OPERAND (declarator, 0);
2879 tree fns = NULL_TREE;
2880 int idx;
2882 if (constructor_name_p (name, ctype))
2884 int is_constructor = DECL_CONSTRUCTOR_P (decl);
2886 if (is_constructor ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
2887 : !CLASSTYPE_DESTRUCTORS (ctype))
2889 /* From [temp.expl.spec]:
2891 If such an explicit specialization for the member
2892 of a class template names an implicitly-declared
2893 special member function (clause _special_), the
2894 program is ill-formed.
2896 Similar language is found in [temp.explicit]. */
2897 error ("specialization of implicitly-declared special member function");
2898 return error_mark_node;
2901 name = is_constructor ? ctor_identifier : dtor_identifier;
2904 if (!DECL_CONV_FN_P (decl))
2906 idx = lookup_fnfields_1 (ctype, name);
2907 if (idx >= 0)
2908 fns = (*CLASSTYPE_METHOD_VEC (ctype))[idx];
2910 else
2912 vec<tree, va_gc> *methods;
2913 tree ovl;
2915 /* For a type-conversion operator, we cannot do a
2916 name-based lookup. We might be looking for `operator
2917 int' which will be a specialization of `operator T'.
2918 So, we find *all* the conversion operators, and then
2919 select from them. */
2920 fns = NULL_TREE;
2922 methods = CLASSTYPE_METHOD_VEC (ctype);
2923 if (methods)
2924 for (idx = CLASSTYPE_FIRST_CONVERSION_SLOT;
2925 methods->iterate (idx, &ovl);
2926 ++idx)
2928 if (!DECL_CONV_FN_P (OVL_FIRST (ovl)))
2929 /* There are no more conversion functions. */
2930 break;
2932 /* Glue all these conversion functions together
2933 with those we already have. */
2934 fns = lookup_add (ovl, fns);
2938 if (fns == NULL_TREE)
2940 error ("no member function %qD declared in %qT", name, ctype);
2941 return error_mark_node;
2943 else
2944 TREE_OPERAND (declarator, 0) = fns;
2947 /* Figure out what exactly is being specialized at this point.
2948 Note that for an explicit instantiation, even one for a
2949 member function, we cannot tell a priori whether the
2950 instantiation is for a member template, or just a member
2951 function of a template class. Even if a member template is
2952 being instantiated, the member template arguments may be
2953 elided if they can be deduced from the rest of the
2954 declaration. */
2955 tmpl = determine_specialization (declarator, decl,
2956 &targs,
2957 member_specialization,
2958 template_count,
2959 tsk);
2961 if (!tmpl || tmpl == error_mark_node)
2962 /* We couldn't figure out what this declaration was
2963 specializing. */
2964 return error_mark_node;
2965 else
2967 if (TREE_CODE (decl) == FUNCTION_DECL
2968 && DECL_HIDDEN_FRIEND_P (tmpl))
2970 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2971 "friend declaration %qD is not visible to "
2972 "explicit specialization", tmpl))
2973 inform (DECL_SOURCE_LOCATION (tmpl),
2974 "friend declaration here");
2976 else if (!ctype && !is_friend
2977 && CP_DECL_CONTEXT (decl) == current_namespace)
2978 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
2980 tree gen_tmpl = most_general_template (tmpl);
2982 if (explicit_instantiation)
2984 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
2985 is done by do_decl_instantiation later. */
2987 int arg_depth = TMPL_ARGS_DEPTH (targs);
2988 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
2990 if (arg_depth > parm_depth)
2992 /* If TMPL is not the most general template (for
2993 example, if TMPL is a friend template that is
2994 injected into namespace scope), then there will
2995 be too many levels of TARGS. Remove some of them
2996 here. */
2997 int i;
2998 tree new_targs;
3000 new_targs = make_tree_vec (parm_depth);
3001 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3002 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3003 = TREE_VEC_ELT (targs, i);
3004 targs = new_targs;
3007 return instantiate_template (tmpl, targs, tf_error);
3010 /* If we thought that the DECL was a member function, but it
3011 turns out to be specializing a static member function,
3012 make DECL a static member function as well. */
3013 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3014 && DECL_STATIC_FUNCTION_P (tmpl)
3015 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3016 revert_static_member_fn (decl);
3018 /* If this is a specialization of a member template of a
3019 template class, we want to return the TEMPLATE_DECL, not
3020 the specialization of it. */
3021 if (tsk == tsk_template && !was_template_id)
3023 tree result = DECL_TEMPLATE_RESULT (tmpl);
3024 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3025 DECL_INITIAL (result) = NULL_TREE;
3026 if (have_def)
3028 tree parm;
3029 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3030 DECL_SOURCE_LOCATION (result)
3031 = DECL_SOURCE_LOCATION (decl);
3032 /* We want to use the argument list specified in the
3033 definition, not in the original declaration. */
3034 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3035 for (parm = DECL_ARGUMENTS (result); parm;
3036 parm = DECL_CHAIN (parm))
3037 DECL_CONTEXT (parm) = result;
3039 return register_specialization (tmpl, gen_tmpl, targs,
3040 is_friend, 0);
3043 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3044 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3046 if (was_template_id)
3047 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3049 /* Inherit default function arguments from the template
3050 DECL is specializing. */
3051 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3052 copy_default_args_to_explicit_spec (decl);
3054 /* This specialization has the same protection as the
3055 template it specializes. */
3056 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3057 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3059 /* 7.1.1-1 [dcl.stc]
3061 A storage-class-specifier shall not be specified in an
3062 explicit specialization...
3064 The parser rejects these, so unless action is taken here,
3065 explicit function specializations will always appear with
3066 global linkage.
3068 The action recommended by the C++ CWG in response to C++
3069 defect report 605 is to make the storage class and linkage
3070 of the explicit specialization match the templated function:
3072 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3074 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3076 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3077 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3079 /* A concept cannot be specialized. */
3080 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3082 error ("explicit specialization of function concept %qD",
3083 gen_tmpl);
3084 return error_mark_node;
3087 /* This specialization has the same linkage and visibility as
3088 the function template it specializes. */
3089 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3090 if (! TREE_PUBLIC (decl))
3092 DECL_INTERFACE_KNOWN (decl) = 1;
3093 DECL_NOT_REALLY_EXTERN (decl) = 1;
3095 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3096 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3098 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3099 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3103 /* If DECL is a friend declaration, declared using an
3104 unqualified name, the namespace associated with DECL may
3105 have been set incorrectly. For example, in:
3107 template <typename T> void f(T);
3108 namespace N {
3109 struct S { friend void f<int>(int); }
3112 we will have set the DECL_CONTEXT for the friend
3113 declaration to N, rather than to the global namespace. */
3114 if (DECL_NAMESPACE_SCOPE_P (decl))
3115 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3117 if (is_friend && !have_def)
3118 /* This is not really a declaration of a specialization.
3119 It's just the name of an instantiation. But, it's not
3120 a request for an instantiation, either. */
3121 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3122 else if (TREE_CODE (decl) == FUNCTION_DECL)
3123 /* A specialization is not necessarily COMDAT. */
3124 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3125 && DECL_DECLARED_INLINE_P (decl));
3126 else if (VAR_P (decl))
3127 DECL_COMDAT (decl) = false;
3129 /* If this is a full specialization, register it so that we can find
3130 it again. Partial specializations will be registered in
3131 process_partial_specialization. */
3132 if (!processing_template_decl)
3133 decl = register_specialization (decl, gen_tmpl, targs,
3134 is_friend, 0);
3136 /* A 'structor should already have clones. */
3137 gcc_assert (decl == error_mark_node
3138 || variable_template_p (tmpl)
3139 || !(DECL_CONSTRUCTOR_P (decl)
3140 || DECL_DESTRUCTOR_P (decl))
3141 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3145 return decl;
3148 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3149 parameters. These are represented in the same format used for
3150 DECL_TEMPLATE_PARMS. */
3153 comp_template_parms (const_tree parms1, const_tree parms2)
3155 const_tree p1;
3156 const_tree p2;
3158 if (parms1 == parms2)
3159 return 1;
3161 for (p1 = parms1, p2 = parms2;
3162 p1 != NULL_TREE && p2 != NULL_TREE;
3163 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3165 tree t1 = TREE_VALUE (p1);
3166 tree t2 = TREE_VALUE (p2);
3167 int i;
3169 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3170 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3172 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3173 return 0;
3175 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3177 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3178 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3180 /* If either of the template parameters are invalid, assume
3181 they match for the sake of error recovery. */
3182 if (error_operand_p (parm1) || error_operand_p (parm2))
3183 return 1;
3185 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3186 return 0;
3188 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3189 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3190 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3191 continue;
3192 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3193 return 0;
3197 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3198 /* One set of parameters has more parameters lists than the
3199 other. */
3200 return 0;
3202 return 1;
3205 /* Determine whether PARM is a parameter pack. */
3207 bool
3208 template_parameter_pack_p (const_tree parm)
3210 /* Determine if we have a non-type template parameter pack. */
3211 if (TREE_CODE (parm) == PARM_DECL)
3212 return (DECL_TEMPLATE_PARM_P (parm)
3213 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3214 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3215 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3217 /* If this is a list of template parameters, we could get a
3218 TYPE_DECL or a TEMPLATE_DECL. */
3219 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3220 parm = TREE_TYPE (parm);
3222 /* Otherwise it must be a type template parameter. */
3223 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3224 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3225 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3228 /* Determine if T is a function parameter pack. */
3230 bool
3231 function_parameter_pack_p (const_tree t)
3233 if (t && TREE_CODE (t) == PARM_DECL)
3234 return DECL_PACK_P (t);
3235 return false;
3238 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3239 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3241 tree
3242 get_function_template_decl (const_tree primary_func_tmpl_inst)
3244 if (! primary_func_tmpl_inst
3245 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3246 || ! primary_template_instantiation_p (primary_func_tmpl_inst))
3247 return NULL;
3249 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3252 /* Return true iff the function parameter PARAM_DECL was expanded
3253 from the function parameter pack PACK. */
3255 bool
3256 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3258 if (DECL_ARTIFICIAL (param_decl)
3259 || !function_parameter_pack_p (pack))
3260 return false;
3262 /* The parameter pack and its pack arguments have the same
3263 DECL_PARM_INDEX. */
3264 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3267 /* Determine whether ARGS describes a variadic template args list,
3268 i.e., one that is terminated by a template argument pack. */
3270 static bool
3271 template_args_variadic_p (tree args)
3273 int nargs;
3274 tree last_parm;
3276 if (args == NULL_TREE)
3277 return false;
3279 args = INNERMOST_TEMPLATE_ARGS (args);
3280 nargs = TREE_VEC_LENGTH (args);
3282 if (nargs == 0)
3283 return false;
3285 last_parm = TREE_VEC_ELT (args, nargs - 1);
3287 return ARGUMENT_PACK_P (last_parm);
3290 /* Generate a new name for the parameter pack name NAME (an
3291 IDENTIFIER_NODE) that incorporates its */
3293 static tree
3294 make_ith_pack_parameter_name (tree name, int i)
3296 /* Munge the name to include the parameter index. */
3297 #define NUMBUF_LEN 128
3298 char numbuf[NUMBUF_LEN];
3299 char* newname;
3300 int newname_len;
3302 if (name == NULL_TREE)
3303 return name;
3304 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3305 newname_len = IDENTIFIER_LENGTH (name)
3306 + strlen (numbuf) + 2;
3307 newname = (char*)alloca (newname_len);
3308 snprintf (newname, newname_len,
3309 "%s#%i", IDENTIFIER_POINTER (name), i);
3310 return get_identifier (newname);
3313 /* Return true if T is a primary function, class or alias template
3314 instantiation. */
3316 bool
3317 primary_template_instantiation_p (const_tree t)
3319 if (!t)
3320 return false;
3322 if (TREE_CODE (t) == FUNCTION_DECL)
3323 return DECL_LANG_SPECIFIC (t)
3324 && DECL_TEMPLATE_INSTANTIATION (t)
3325 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t));
3326 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3327 return CLASSTYPE_TEMPLATE_INSTANTIATION (t)
3328 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t));
3329 else if (alias_template_specialization_p (t))
3330 return true;
3331 return false;
3334 /* Return true if PARM is a template template parameter. */
3336 bool
3337 template_template_parameter_p (const_tree parm)
3339 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3342 /* Return true iff PARM is a DECL representing a type template
3343 parameter. */
3345 bool
3346 template_type_parameter_p (const_tree parm)
3348 return (parm
3349 && (TREE_CODE (parm) == TYPE_DECL
3350 || TREE_CODE (parm) == TEMPLATE_DECL)
3351 && DECL_TEMPLATE_PARM_P (parm));
3354 /* Return the template parameters of T if T is a
3355 primary template instantiation, NULL otherwise. */
3357 tree
3358 get_primary_template_innermost_parameters (const_tree t)
3360 tree parms = NULL, template_info = NULL;
3362 if ((template_info = get_template_info (t))
3363 && primary_template_instantiation_p (t))
3364 parms = INNERMOST_TEMPLATE_PARMS
3365 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3367 return parms;
3370 /* Return the template parameters of the LEVELth level from the full list
3371 of template parameters PARMS. */
3373 tree
3374 get_template_parms_at_level (tree parms, int level)
3376 tree p;
3377 if (!parms
3378 || TREE_CODE (parms) != TREE_LIST
3379 || level > TMPL_PARMS_DEPTH (parms))
3380 return NULL_TREE;
3382 for (p = parms; p; p = TREE_CHAIN (p))
3383 if (TMPL_PARMS_DEPTH (p) == level)
3384 return p;
3386 return NULL_TREE;
3389 /* Returns the template arguments of T if T is a template instantiation,
3390 NULL otherwise. */
3392 tree
3393 get_template_innermost_arguments (const_tree t)
3395 tree args = NULL, template_info = NULL;
3397 if ((template_info = get_template_info (t))
3398 && TI_ARGS (template_info))
3399 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3401 return args;
3404 /* Return the argument pack elements of T if T is a template argument pack,
3405 NULL otherwise. */
3407 tree
3408 get_template_argument_pack_elems (const_tree t)
3410 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3411 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3412 return NULL;
3414 return ARGUMENT_PACK_ARGS (t);
3417 /* True iff FN is a function representing a built-in variadic parameter
3418 pack. */
3420 bool
3421 builtin_pack_fn_p (tree fn)
3423 if (!fn
3424 || TREE_CODE (fn) != FUNCTION_DECL
3425 || !DECL_IS_BUILTIN (fn))
3426 return false;
3428 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3429 return true;
3431 return false;
3434 /* True iff CALL is a call to a function representing a built-in variadic
3435 parameter pack. */
3437 static bool
3438 builtin_pack_call_p (tree call)
3440 if (TREE_CODE (call) != CALL_EXPR)
3441 return false;
3442 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3445 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3447 static tree
3448 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3449 tree in_decl)
3451 tree ohi = CALL_EXPR_ARG (call, 0);
3452 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3453 false/*fn*/, true/*int_cst*/);
3455 if (value_dependent_expression_p (hi))
3457 if (hi != ohi)
3459 call = copy_node (call);
3460 CALL_EXPR_ARG (call, 0) = hi;
3462 tree ex = make_pack_expansion (call);
3463 tree vec = make_tree_vec (1);
3464 TREE_VEC_ELT (vec, 0) = ex;
3465 return vec;
3467 else
3469 hi = cxx_constant_value (hi);
3470 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3472 /* Calculate the largest value of len that won't make the size of the vec
3473 overflow an int. The compiler will exceed resource limits long before
3474 this, but it seems a decent place to diagnose. */
3475 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3477 if (len < 0 || len > max)
3479 if ((complain & tf_error)
3480 && hi != error_mark_node)
3481 error ("argument to __integer_pack must be between 0 and %d", max);
3482 return error_mark_node;
3485 tree vec = make_tree_vec (len);
3487 for (int i = 0; i < len; ++i)
3488 TREE_VEC_ELT (vec, i) = size_int (i);
3490 return vec;
3494 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3495 CALL. */
3497 static tree
3498 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3499 tree in_decl)
3501 if (!builtin_pack_call_p (call))
3502 return NULL_TREE;
3504 tree fn = CALL_EXPR_FN (call);
3506 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3507 return expand_integer_pack (call, args, complain, in_decl);
3509 return NULL_TREE;
3512 /* Structure used to track the progress of find_parameter_packs_r. */
3513 struct find_parameter_pack_data
3515 /* TREE_LIST that will contain all of the parameter packs found by
3516 the traversal. */
3517 tree* parameter_packs;
3519 /* Set of AST nodes that have been visited by the traversal. */
3520 hash_set<tree> *visited;
3522 /* True iff we're making a type pack expansion. */
3523 bool type_pack_expansion_p;
3526 /* Identifies all of the argument packs that occur in a template
3527 argument and appends them to the TREE_LIST inside DATA, which is a
3528 find_parameter_pack_data structure. This is a subroutine of
3529 make_pack_expansion and uses_parameter_packs. */
3530 static tree
3531 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3533 tree t = *tp;
3534 struct find_parameter_pack_data* ppd =
3535 (struct find_parameter_pack_data*)data;
3536 bool parameter_pack_p = false;
3538 /* Handle type aliases/typedefs. */
3539 if (TYPE_ALIAS_P (t))
3541 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3542 cp_walk_tree (&TI_ARGS (tinfo),
3543 &find_parameter_packs_r,
3544 ppd, ppd->visited);
3545 *walk_subtrees = 0;
3546 return NULL_TREE;
3549 /* Identify whether this is a parameter pack or not. */
3550 switch (TREE_CODE (t))
3552 case TEMPLATE_PARM_INDEX:
3553 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3554 parameter_pack_p = true;
3555 break;
3557 case TEMPLATE_TYPE_PARM:
3558 t = TYPE_MAIN_VARIANT (t);
3559 /* FALLTHRU */
3560 case TEMPLATE_TEMPLATE_PARM:
3561 /* If the placeholder appears in the decl-specifier-seq of a function
3562 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3563 is a pack expansion, the invented template parameter is a template
3564 parameter pack. */
3565 if (ppd->type_pack_expansion_p && is_auto (t))
3566 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3567 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3568 parameter_pack_p = true;
3569 break;
3571 case FIELD_DECL:
3572 case PARM_DECL:
3573 if (DECL_PACK_P (t))
3575 /* We don't want to walk into the type of a PARM_DECL,
3576 because we don't want to see the type parameter pack. */
3577 *walk_subtrees = 0;
3578 parameter_pack_p = true;
3580 break;
3582 /* Look through a lambda capture proxy to the field pack. */
3583 case VAR_DECL:
3584 if (DECL_HAS_VALUE_EXPR_P (t))
3586 tree v = DECL_VALUE_EXPR (t);
3587 cp_walk_tree (&v,
3588 &find_parameter_packs_r,
3589 ppd, ppd->visited);
3590 *walk_subtrees = 0;
3592 else if (variable_template_specialization_p (t))
3594 cp_walk_tree (&DECL_TI_ARGS (t),
3595 find_parameter_packs_r,
3596 ppd, ppd->visited);
3597 *walk_subtrees = 0;
3599 break;
3601 case CALL_EXPR:
3602 if (builtin_pack_call_p (t))
3603 parameter_pack_p = true;
3604 break;
3606 case BASES:
3607 parameter_pack_p = true;
3608 break;
3609 default:
3610 /* Not a parameter pack. */
3611 break;
3614 if (parameter_pack_p)
3616 /* Add this parameter pack to the list. */
3617 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3620 if (TYPE_P (t))
3621 cp_walk_tree (&TYPE_CONTEXT (t),
3622 &find_parameter_packs_r, ppd, ppd->visited);
3624 /* This switch statement will return immediately if we don't find a
3625 parameter pack. */
3626 switch (TREE_CODE (t))
3628 case TEMPLATE_PARM_INDEX:
3629 return NULL_TREE;
3631 case BOUND_TEMPLATE_TEMPLATE_PARM:
3632 /* Check the template itself. */
3633 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3634 &find_parameter_packs_r, ppd, ppd->visited);
3635 /* Check the template arguments. */
3636 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3637 ppd->visited);
3638 *walk_subtrees = 0;
3639 return NULL_TREE;
3641 case TEMPLATE_TYPE_PARM:
3642 case TEMPLATE_TEMPLATE_PARM:
3643 return NULL_TREE;
3645 case PARM_DECL:
3646 return NULL_TREE;
3648 case RECORD_TYPE:
3649 if (TYPE_PTRMEMFUNC_P (t))
3650 return NULL_TREE;
3651 /* Fall through. */
3653 case UNION_TYPE:
3654 case ENUMERAL_TYPE:
3655 if (TYPE_TEMPLATE_INFO (t))
3656 cp_walk_tree (&TYPE_TI_ARGS (t),
3657 &find_parameter_packs_r, ppd, ppd->visited);
3659 *walk_subtrees = 0;
3660 return NULL_TREE;
3662 case TEMPLATE_DECL:
3663 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3664 return NULL_TREE;
3665 gcc_fallthrough();
3667 case CONSTRUCTOR:
3668 cp_walk_tree (&TREE_TYPE (t),
3669 &find_parameter_packs_r, ppd, ppd->visited);
3670 return NULL_TREE;
3672 case TYPENAME_TYPE:
3673 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3674 ppd, ppd->visited);
3675 *walk_subtrees = 0;
3676 return NULL_TREE;
3678 case TYPE_PACK_EXPANSION:
3679 case EXPR_PACK_EXPANSION:
3680 *walk_subtrees = 0;
3681 return NULL_TREE;
3683 case INTEGER_TYPE:
3684 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3685 ppd, ppd->visited);
3686 *walk_subtrees = 0;
3687 return NULL_TREE;
3689 case IDENTIFIER_NODE:
3690 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3691 ppd->visited);
3692 *walk_subtrees = 0;
3693 return NULL_TREE;
3695 case DECLTYPE_TYPE:
3697 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3698 type_pack_expansion_p to false so that any placeholders
3699 within the expression don't get marked as parameter packs. */
3700 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
3701 ppd->type_pack_expansion_p = false;
3702 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
3703 ppd, ppd->visited);
3704 ppd->type_pack_expansion_p = type_pack_expansion_p;
3705 *walk_subtrees = 0;
3706 return NULL_TREE;
3709 default:
3710 return NULL_TREE;
3713 return NULL_TREE;
3716 /* Determines if the expression or type T uses any parameter packs. */
3717 bool
3718 uses_parameter_packs (tree t)
3720 tree parameter_packs = NULL_TREE;
3721 struct find_parameter_pack_data ppd;
3722 ppd.parameter_packs = &parameter_packs;
3723 ppd.visited = new hash_set<tree>;
3724 ppd.type_pack_expansion_p = false;
3725 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3726 delete ppd.visited;
3727 return parameter_packs != NULL_TREE;
3730 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3731 representation a base-class initializer into a parameter pack
3732 expansion. If all goes well, the resulting node will be an
3733 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3734 respectively. */
3735 tree
3736 make_pack_expansion (tree arg)
3738 tree result;
3739 tree parameter_packs = NULL_TREE;
3740 bool for_types = false;
3741 struct find_parameter_pack_data ppd;
3743 if (!arg || arg == error_mark_node)
3744 return arg;
3746 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
3748 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3749 class initializer. In this case, the TREE_PURPOSE will be a
3750 _TYPE node (representing the base class expansion we're
3751 initializing) and the TREE_VALUE will be a TREE_LIST
3752 containing the initialization arguments.
3754 The resulting expansion looks somewhat different from most
3755 expansions. Rather than returning just one _EXPANSION, we
3756 return a TREE_LIST whose TREE_PURPOSE is a
3757 TYPE_PACK_EXPANSION containing the bases that will be
3758 initialized. The TREE_VALUE will be identical to the
3759 original TREE_VALUE, which is a list of arguments that will
3760 be passed to each base. We do not introduce any new pack
3761 expansion nodes into the TREE_VALUE (although it is possible
3762 that some already exist), because the TREE_PURPOSE and
3763 TREE_VALUE all need to be expanded together with the same
3764 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3765 resulting TREE_PURPOSE will mention the parameter packs in
3766 both the bases and the arguments to the bases. */
3767 tree purpose;
3768 tree value;
3769 tree parameter_packs = NULL_TREE;
3771 /* Determine which parameter packs will be used by the base
3772 class expansion. */
3773 ppd.visited = new hash_set<tree>;
3774 ppd.parameter_packs = &parameter_packs;
3775 ppd.type_pack_expansion_p = true;
3776 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
3777 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3778 &ppd, ppd.visited);
3780 if (parameter_packs == NULL_TREE)
3782 error ("base initializer expansion %qT contains no parameter packs", arg);
3783 delete ppd.visited;
3784 return error_mark_node;
3787 if (TREE_VALUE (arg) != void_type_node)
3789 /* Collect the sets of parameter packs used in each of the
3790 initialization arguments. */
3791 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3793 /* Determine which parameter packs will be expanded in this
3794 argument. */
3795 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3796 &ppd, ppd.visited);
3800 delete ppd.visited;
3802 /* Create the pack expansion type for the base type. */
3803 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3804 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3805 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3807 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3808 they will rarely be compared to anything. */
3809 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3811 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3814 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3815 for_types = true;
3817 /* Build the PACK_EXPANSION_* node. */
3818 result = for_types
3819 ? cxx_make_type (TYPE_PACK_EXPANSION)
3820 : make_node (EXPR_PACK_EXPANSION);
3821 SET_PACK_EXPANSION_PATTERN (result, arg);
3822 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3824 /* Propagate type and const-expression information. */
3825 TREE_TYPE (result) = TREE_TYPE (arg);
3826 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3827 /* Mark this read now, since the expansion might be length 0. */
3828 mark_exp_read (arg);
3830 else
3831 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3832 they will rarely be compared to anything. */
3833 SET_TYPE_STRUCTURAL_EQUALITY (result);
3835 /* Determine which parameter packs will be expanded. */
3836 ppd.parameter_packs = &parameter_packs;
3837 ppd.visited = new hash_set<tree>;
3838 ppd.type_pack_expansion_p = TYPE_P (arg);
3839 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
3840 delete ppd.visited;
3842 /* Make sure we found some parameter packs. */
3843 if (parameter_packs == NULL_TREE)
3845 if (TYPE_P (arg))
3846 error ("expansion pattern %qT contains no argument packs", arg);
3847 else
3848 error ("expansion pattern %qE contains no argument packs", arg);
3849 return error_mark_node;
3851 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
3853 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
3855 return result;
3858 /* Checks T for any "bare" parameter packs, which have not yet been
3859 expanded, and issues an error if any are found. This operation can
3860 only be done on full expressions or types (e.g., an expression
3861 statement, "if" condition, etc.), because we could have expressions like:
3863 foo(f(g(h(args)))...)
3865 where "args" is a parameter pack. check_for_bare_parameter_packs
3866 should not be called for the subexpressions args, h(args),
3867 g(h(args)), or f(g(h(args))), because we would produce erroneous
3868 error messages.
3870 Returns TRUE and emits an error if there were bare parameter packs,
3871 returns FALSE otherwise. */
3872 bool
3873 check_for_bare_parameter_packs (tree t)
3875 tree parameter_packs = NULL_TREE;
3876 struct find_parameter_pack_data ppd;
3878 if (!processing_template_decl || !t || t == error_mark_node)
3879 return false;
3881 if (TREE_CODE (t) == TYPE_DECL)
3882 t = TREE_TYPE (t);
3884 ppd.parameter_packs = &parameter_packs;
3885 ppd.visited = new hash_set<tree>;
3886 ppd.type_pack_expansion_p = false;
3887 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3888 delete ppd.visited;
3890 if (parameter_packs)
3892 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
3893 error_at (loc, "parameter packs not expanded with %<...%>:");
3894 while (parameter_packs)
3896 tree pack = TREE_VALUE (parameter_packs);
3897 tree name = NULL_TREE;
3899 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
3900 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
3901 name = TYPE_NAME (pack);
3902 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
3903 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
3904 else if (TREE_CODE (pack) == CALL_EXPR)
3905 name = DECL_NAME (CALL_EXPR_FN (pack));
3906 else
3907 name = DECL_NAME (pack);
3909 if (name)
3910 inform (loc, " %qD", name);
3911 else
3912 inform (loc, " <anonymous>");
3914 parameter_packs = TREE_CHAIN (parameter_packs);
3917 return true;
3920 return false;
3923 /* Expand any parameter packs that occur in the template arguments in
3924 ARGS. */
3925 tree
3926 expand_template_argument_pack (tree args)
3928 if (args == error_mark_node)
3929 return error_mark_node;
3931 tree result_args = NULL_TREE;
3932 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
3933 int num_result_args = -1;
3934 int non_default_args_count = -1;
3936 /* First, determine if we need to expand anything, and the number of
3937 slots we'll need. */
3938 for (in_arg = 0; in_arg < nargs; ++in_arg)
3940 tree arg = TREE_VEC_ELT (args, in_arg);
3941 if (arg == NULL_TREE)
3942 return args;
3943 if (ARGUMENT_PACK_P (arg))
3945 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
3946 if (num_result_args < 0)
3947 num_result_args = in_arg + num_packed;
3948 else
3949 num_result_args += num_packed;
3951 else
3953 if (num_result_args >= 0)
3954 num_result_args++;
3958 /* If no expansion is necessary, we're done. */
3959 if (num_result_args < 0)
3960 return args;
3962 /* Expand arguments. */
3963 result_args = make_tree_vec (num_result_args);
3964 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
3965 non_default_args_count =
3966 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
3967 for (in_arg = 0; in_arg < nargs; ++in_arg)
3969 tree arg = TREE_VEC_ELT (args, in_arg);
3970 if (ARGUMENT_PACK_P (arg))
3972 tree packed = ARGUMENT_PACK_ARGS (arg);
3973 int i, num_packed = TREE_VEC_LENGTH (packed);
3974 for (i = 0; i < num_packed; ++i, ++out_arg)
3975 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
3976 if (non_default_args_count > 0)
3977 non_default_args_count += num_packed - 1;
3979 else
3981 TREE_VEC_ELT (result_args, out_arg) = arg;
3982 ++out_arg;
3985 if (non_default_args_count >= 0)
3986 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
3987 return result_args;
3990 /* Checks if DECL shadows a template parameter.
3992 [temp.local]: A template-parameter shall not be redeclared within its
3993 scope (including nested scopes).
3995 Emits an error and returns TRUE if the DECL shadows a parameter,
3996 returns FALSE otherwise. */
3998 bool
3999 check_template_shadow (tree decl)
4001 tree olddecl;
4003 /* If we're not in a template, we can't possibly shadow a template
4004 parameter. */
4005 if (!current_template_parms)
4006 return true;
4008 /* Figure out what we're shadowing. */
4009 decl = OVL_FIRST (decl);
4010 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4012 /* If there's no previous binding for this name, we're not shadowing
4013 anything, let alone a template parameter. */
4014 if (!olddecl)
4015 return true;
4017 /* If we're not shadowing a template parameter, we're done. Note
4018 that OLDDECL might be an OVERLOAD (or perhaps even an
4019 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4020 node. */
4021 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4022 return true;
4024 /* We check for decl != olddecl to avoid bogus errors for using a
4025 name inside a class. We check TPFI to avoid duplicate errors for
4026 inline member templates. */
4027 if (decl == olddecl
4028 || (DECL_TEMPLATE_PARM_P (decl)
4029 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4030 return true;
4032 /* Don't complain about the injected class name, as we've already
4033 complained about the class itself. */
4034 if (DECL_SELF_REFERENCE_P (decl))
4035 return false;
4037 if (DECL_TEMPLATE_PARM_P (decl))
4038 error ("declaration of template parameter %q+D shadows "
4039 "template parameter", decl);
4040 else
4041 error ("declaration of %q+#D shadows template parameter", decl);
4042 inform (DECL_SOURCE_LOCATION (olddecl),
4043 "template parameter %qD declared here", olddecl);
4044 return false;
4047 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4048 ORIG_LEVEL, DECL, and TYPE. */
4050 static tree
4051 build_template_parm_index (int index,
4052 int level,
4053 int orig_level,
4054 tree decl,
4055 tree type)
4057 tree t = make_node (TEMPLATE_PARM_INDEX);
4058 TEMPLATE_PARM_IDX (t) = index;
4059 TEMPLATE_PARM_LEVEL (t) = level;
4060 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4061 TEMPLATE_PARM_DECL (t) = decl;
4062 TREE_TYPE (t) = type;
4063 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4064 TREE_READONLY (t) = TREE_READONLY (decl);
4066 return t;
4069 /* Find the canonical type parameter for the given template type
4070 parameter. Returns the canonical type parameter, which may be TYPE
4071 if no such parameter existed. */
4073 static tree
4074 canonical_type_parameter (tree type)
4076 tree list;
4077 int idx = TEMPLATE_TYPE_IDX (type);
4078 if (!canonical_template_parms)
4079 vec_alloc (canonical_template_parms, idx + 1);
4081 if (canonical_template_parms->length () <= (unsigned) idx)
4082 vec_safe_grow_cleared (canonical_template_parms, idx + 1);
4084 list = (*canonical_template_parms)[idx];
4085 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4086 list = TREE_CHAIN (list);
4088 if (list)
4089 return TREE_VALUE (list);
4090 else
4092 (*canonical_template_parms)[idx]
4093 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4094 return type;
4098 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4099 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4100 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4101 new one is created. */
4103 static tree
4104 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4105 tsubst_flags_t complain)
4107 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4108 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4109 != TEMPLATE_PARM_LEVEL (index) - levels)
4110 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4112 tree orig_decl = TEMPLATE_PARM_DECL (index);
4113 tree decl, t;
4115 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4116 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
4117 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4118 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4119 DECL_ARTIFICIAL (decl) = 1;
4120 SET_DECL_TEMPLATE_PARM_P (decl);
4122 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4123 TEMPLATE_PARM_LEVEL (index) - levels,
4124 TEMPLATE_PARM_ORIG_LEVEL (index),
4125 decl, type);
4126 TEMPLATE_PARM_DESCENDANTS (index) = t;
4127 TEMPLATE_PARM_PARAMETER_PACK (t)
4128 = TEMPLATE_PARM_PARAMETER_PACK (index);
4130 /* Template template parameters need this. */
4131 if (TREE_CODE (decl) == TEMPLATE_DECL)
4133 DECL_TEMPLATE_RESULT (decl)
4134 = build_decl (DECL_SOURCE_LOCATION (decl),
4135 TYPE_DECL, DECL_NAME (decl), type);
4136 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl)) = true;
4137 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4138 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4142 return TEMPLATE_PARM_DESCENDANTS (index);
4145 /* Process information from new template parameter PARM and append it
4146 to the LIST being built. This new parameter is a non-type
4147 parameter iff IS_NON_TYPE is true. This new parameter is a
4148 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4149 is in PARM_LOC. */
4151 tree
4152 process_template_parm (tree list, location_t parm_loc, tree parm,
4153 bool is_non_type, bool is_parameter_pack)
4155 tree decl = 0;
4156 int idx = 0;
4158 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4159 tree defval = TREE_PURPOSE (parm);
4160 tree constr = TREE_TYPE (parm);
4162 if (list)
4164 tree p = tree_last (list);
4166 if (p && TREE_VALUE (p) != error_mark_node)
4168 p = TREE_VALUE (p);
4169 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4170 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4171 else
4172 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4175 ++idx;
4178 if (is_non_type)
4180 parm = TREE_VALUE (parm);
4182 SET_DECL_TEMPLATE_PARM_P (parm);
4184 if (TREE_TYPE (parm) != error_mark_node)
4186 /* [temp.param]
4188 The top-level cv-qualifiers on the template-parameter are
4189 ignored when determining its type. */
4190 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4191 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4192 TREE_TYPE (parm) = error_mark_node;
4193 else if (uses_parameter_packs (TREE_TYPE (parm))
4194 && !is_parameter_pack
4195 /* If we're in a nested template parameter list, the template
4196 template parameter could be a parameter pack. */
4197 && processing_template_parmlist == 1)
4199 /* This template parameter is not a parameter pack, but it
4200 should be. Complain about "bare" parameter packs. */
4201 check_for_bare_parameter_packs (TREE_TYPE (parm));
4203 /* Recover by calling this a parameter pack. */
4204 is_parameter_pack = true;
4208 /* A template parameter is not modifiable. */
4209 TREE_CONSTANT (parm) = 1;
4210 TREE_READONLY (parm) = 1;
4211 decl = build_decl (parm_loc,
4212 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4213 TREE_CONSTANT (decl) = 1;
4214 TREE_READONLY (decl) = 1;
4215 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4216 = build_template_parm_index (idx, processing_template_decl,
4217 processing_template_decl,
4218 decl, TREE_TYPE (parm));
4220 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4221 = is_parameter_pack;
4223 else
4225 tree t;
4226 parm = TREE_VALUE (TREE_VALUE (parm));
4228 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4230 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4231 /* This is for distinguishing between real templates and template
4232 template parameters */
4233 TREE_TYPE (parm) = t;
4234 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4235 decl = parm;
4237 else
4239 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4240 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4241 decl = build_decl (parm_loc,
4242 TYPE_DECL, parm, t);
4245 TYPE_NAME (t) = decl;
4246 TYPE_STUB_DECL (t) = decl;
4247 parm = decl;
4248 TEMPLATE_TYPE_PARM_INDEX (t)
4249 = build_template_parm_index (idx, processing_template_decl,
4250 processing_template_decl,
4251 decl, TREE_TYPE (parm));
4252 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4253 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4255 DECL_ARTIFICIAL (decl) = 1;
4256 SET_DECL_TEMPLATE_PARM_P (decl);
4258 /* Build requirements for the type/template parameter.
4259 This must be done after SET_DECL_TEMPLATE_PARM_P or
4260 process_template_parm could fail. */
4261 tree reqs = finish_shorthand_constraint (parm, constr);
4263 pushdecl (decl);
4265 /* Build the parameter node linking the parameter declaration,
4266 its default argument (if any), and its constraints (if any). */
4267 parm = build_tree_list (defval, parm);
4268 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4270 return chainon (list, parm);
4273 /* The end of a template parameter list has been reached. Process the
4274 tree list into a parameter vector, converting each parameter into a more
4275 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4276 as PARM_DECLs. */
4278 tree
4279 end_template_parm_list (tree parms)
4281 int nparms;
4282 tree parm, next;
4283 tree saved_parmlist = make_tree_vec (list_length (parms));
4285 /* Pop the dummy parameter level and add the real one. */
4286 current_template_parms = TREE_CHAIN (current_template_parms);
4288 current_template_parms
4289 = tree_cons (size_int (processing_template_decl),
4290 saved_parmlist, current_template_parms);
4292 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4294 next = TREE_CHAIN (parm);
4295 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4296 TREE_CHAIN (parm) = NULL_TREE;
4299 --processing_template_parmlist;
4301 return saved_parmlist;
4304 // Explicitly indicate the end of the template parameter list. We assume
4305 // that the current template parameters have been constructed and/or
4306 // managed explicitly, as when creating new template template parameters
4307 // from a shorthand constraint.
4308 void
4309 end_template_parm_list ()
4311 --processing_template_parmlist;
4314 /* end_template_decl is called after a template declaration is seen. */
4316 void
4317 end_template_decl (void)
4319 reset_specialization ();
4321 if (! processing_template_decl)
4322 return;
4324 /* This matches the pushlevel in begin_template_parm_list. */
4325 finish_scope ();
4327 --processing_template_decl;
4328 current_template_parms = TREE_CHAIN (current_template_parms);
4331 /* Takes a TREE_LIST representing a template parameter and convert it
4332 into an argument suitable to be passed to the type substitution
4333 functions. Note that If the TREE_LIST contains an error_mark
4334 node, the returned argument is error_mark_node. */
4336 tree
4337 template_parm_to_arg (tree t)
4340 if (t == NULL_TREE
4341 || TREE_CODE (t) != TREE_LIST)
4342 return t;
4344 if (error_operand_p (TREE_VALUE (t)))
4345 return error_mark_node;
4347 t = TREE_VALUE (t);
4349 if (TREE_CODE (t) == TYPE_DECL
4350 || TREE_CODE (t) == TEMPLATE_DECL)
4352 t = TREE_TYPE (t);
4354 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4356 /* Turn this argument into a TYPE_ARGUMENT_PACK
4357 with a single element, which expands T. */
4358 tree vec = make_tree_vec (1);
4359 if (CHECKING_P)
4360 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4362 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4364 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4365 SET_ARGUMENT_PACK_ARGS (t, vec);
4368 else
4370 t = DECL_INITIAL (t);
4372 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4374 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4375 with a single element, which expands T. */
4376 tree vec = make_tree_vec (1);
4377 if (CHECKING_P)
4378 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4380 t = convert_from_reference (t);
4381 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4383 t = make_node (NONTYPE_ARGUMENT_PACK);
4384 SET_ARGUMENT_PACK_ARGS (t, vec);
4386 else
4387 t = convert_from_reference (t);
4389 return t;
4392 /* Given a single level of template parameters (a TREE_VEC), return it
4393 as a set of template arguments. */
4395 static tree
4396 template_parms_level_to_args (tree parms)
4398 tree a = copy_node (parms);
4399 TREE_TYPE (a) = NULL_TREE;
4400 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4401 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4403 if (CHECKING_P)
4404 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4406 return a;
4409 /* Given a set of template parameters, return them as a set of template
4410 arguments. The template parameters are represented as a TREE_VEC, in
4411 the form documented in cp-tree.h for template arguments. */
4413 static tree
4414 template_parms_to_args (tree parms)
4416 tree header;
4417 tree args = NULL_TREE;
4418 int length = TMPL_PARMS_DEPTH (parms);
4419 int l = length;
4421 /* If there is only one level of template parameters, we do not
4422 create a TREE_VEC of TREE_VECs. Instead, we return a single
4423 TREE_VEC containing the arguments. */
4424 if (length > 1)
4425 args = make_tree_vec (length);
4427 for (header = parms; header; header = TREE_CHAIN (header))
4429 tree a = template_parms_level_to_args (TREE_VALUE (header));
4431 if (length > 1)
4432 TREE_VEC_ELT (args, --l) = a;
4433 else
4434 args = a;
4437 return args;
4440 /* Within the declaration of a template, return the currently active
4441 template parameters as an argument TREE_VEC. */
4443 static tree
4444 current_template_args (void)
4446 return template_parms_to_args (current_template_parms);
4449 /* Update the declared TYPE by doing any lookups which were thought to be
4450 dependent, but are not now that we know the SCOPE of the declarator. */
4452 tree
4453 maybe_update_decl_type (tree orig_type, tree scope)
4455 tree type = orig_type;
4457 if (type == NULL_TREE)
4458 return type;
4460 if (TREE_CODE (orig_type) == TYPE_DECL)
4461 type = TREE_TYPE (type);
4463 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4464 && dependent_type_p (type)
4465 /* Don't bother building up the args in this case. */
4466 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4468 /* tsubst in the args corresponding to the template parameters,
4469 including auto if present. Most things will be unchanged, but
4470 make_typename_type and tsubst_qualified_id will resolve
4471 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4472 tree args = current_template_args ();
4473 tree auto_node = type_uses_auto (type);
4474 tree pushed;
4475 if (auto_node)
4477 tree auto_vec = make_tree_vec (1);
4478 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4479 args = add_to_template_args (args, auto_vec);
4481 pushed = push_scope (scope);
4482 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4483 if (pushed)
4484 pop_scope (scope);
4487 if (type == error_mark_node)
4488 return orig_type;
4490 if (TREE_CODE (orig_type) == TYPE_DECL)
4492 if (same_type_p (type, TREE_TYPE (orig_type)))
4493 type = orig_type;
4494 else
4495 type = TYPE_NAME (type);
4497 return type;
4500 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4501 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4502 the new template is a member template. */
4504 tree
4505 build_template_decl (tree decl, tree parms, bool member_template_p)
4507 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4508 DECL_TEMPLATE_PARMS (tmpl) = parms;
4509 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4510 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4511 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4513 return tmpl;
4516 struct template_parm_data
4518 /* The level of the template parameters we are currently
4519 processing. */
4520 int level;
4522 /* The index of the specialization argument we are currently
4523 processing. */
4524 int current_arg;
4526 /* An array whose size is the number of template parameters. The
4527 elements are nonzero if the parameter has been used in any one
4528 of the arguments processed so far. */
4529 int* parms;
4531 /* An array whose size is the number of template arguments. The
4532 elements are nonzero if the argument makes use of template
4533 parameters of this level. */
4534 int* arg_uses_template_parms;
4537 /* Subroutine of push_template_decl used to see if each template
4538 parameter in a partial specialization is used in the explicit
4539 argument list. If T is of the LEVEL given in DATA (which is
4540 treated as a template_parm_data*), then DATA->PARMS is marked
4541 appropriately. */
4543 static int
4544 mark_template_parm (tree t, void* data)
4546 int level;
4547 int idx;
4548 struct template_parm_data* tpd = (struct template_parm_data*) data;
4550 template_parm_level_and_index (t, &level, &idx);
4552 if (level == tpd->level)
4554 tpd->parms[idx] = 1;
4555 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4558 /* In C++17 the type of a non-type argument is a deduced context. */
4559 if (cxx_dialect >= cxx1z
4560 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4561 for_each_template_parm (TREE_TYPE (t),
4562 &mark_template_parm,
4563 data,
4564 NULL,
4565 /*include_nondeduced_p=*/false);
4567 /* Return zero so that for_each_template_parm will continue the
4568 traversal of the tree; we want to mark *every* template parm. */
4569 return 0;
4572 /* Process the partial specialization DECL. */
4574 static tree
4575 process_partial_specialization (tree decl)
4577 tree type = TREE_TYPE (decl);
4578 tree tinfo = get_template_info (decl);
4579 tree maintmpl = TI_TEMPLATE (tinfo);
4580 tree specargs = TI_ARGS (tinfo);
4581 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4582 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4583 tree inner_parms;
4584 tree inst;
4585 int nargs = TREE_VEC_LENGTH (inner_args);
4586 int ntparms;
4587 int i;
4588 bool did_error_intro = false;
4589 struct template_parm_data tpd;
4590 struct template_parm_data tpd2;
4592 gcc_assert (current_template_parms);
4594 /* A concept cannot be specialized. */
4595 if (flag_concepts && variable_concept_p (maintmpl))
4597 error ("specialization of variable concept %q#D", maintmpl);
4598 return error_mark_node;
4601 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4602 ntparms = TREE_VEC_LENGTH (inner_parms);
4604 /* We check that each of the template parameters given in the
4605 partial specialization is used in the argument list to the
4606 specialization. For example:
4608 template <class T> struct S;
4609 template <class T> struct S<T*>;
4611 The second declaration is OK because `T*' uses the template
4612 parameter T, whereas
4614 template <class T> struct S<int>;
4616 is no good. Even trickier is:
4618 template <class T>
4619 struct S1
4621 template <class U>
4622 struct S2;
4623 template <class U>
4624 struct S2<T>;
4627 The S2<T> declaration is actually invalid; it is a
4628 full-specialization. Of course,
4630 template <class U>
4631 struct S2<T (*)(U)>;
4633 or some such would have been OK. */
4634 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4635 tpd.parms = XALLOCAVEC (int, ntparms);
4636 memset (tpd.parms, 0, sizeof (int) * ntparms);
4638 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4639 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4640 for (i = 0; i < nargs; ++i)
4642 tpd.current_arg = i;
4643 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4644 &mark_template_parm,
4645 &tpd,
4646 NULL,
4647 /*include_nondeduced_p=*/false);
4649 for (i = 0; i < ntparms; ++i)
4650 if (tpd.parms[i] == 0)
4652 /* One of the template parms was not used in a deduced context in the
4653 specialization. */
4654 if (!did_error_intro)
4656 error ("template parameters not deducible in "
4657 "partial specialization:");
4658 did_error_intro = true;
4661 inform (input_location, " %qD",
4662 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4665 if (did_error_intro)
4666 return error_mark_node;
4668 /* [temp.class.spec]
4670 The argument list of the specialization shall not be identical to
4671 the implicit argument list of the primary template. */
4672 tree main_args
4673 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4674 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
4675 && (!flag_concepts
4676 || !strictly_subsumes (current_template_constraints (),
4677 get_constraints (maintmpl))))
4679 if (!flag_concepts)
4680 error ("partial specialization %q+D does not specialize "
4681 "any template arguments", decl);
4682 else
4683 error ("partial specialization %q+D does not specialize any "
4684 "template arguments and is not more constrained than", decl);
4685 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4688 /* A partial specialization that replaces multiple parameters of the
4689 primary template with a pack expansion is less specialized for those
4690 parameters. */
4691 if (nargs < DECL_NTPARMS (maintmpl))
4693 error ("partial specialization is not more specialized than the "
4694 "primary template because it replaces multiple parameters "
4695 "with a pack expansion");
4696 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4697 /* Avoid crash in process_partial_specialization. */
4698 return decl;
4701 /* If we aren't in a dependent class, we can actually try deduction. */
4702 else if (tpd.level == 1
4703 /* FIXME we should be able to handle a partial specialization of a
4704 partial instantiation, but currently we can't (c++/41727). */
4705 && TMPL_ARGS_DEPTH (specargs) == 1
4706 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
4708 if (permerror (input_location, "partial specialization %qD is not "
4709 "more specialized than", decl))
4710 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
4711 maintmpl);
4714 /* [temp.class.spec]
4716 A partially specialized non-type argument expression shall not
4717 involve template parameters of the partial specialization except
4718 when the argument expression is a simple identifier.
4720 The type of a template parameter corresponding to a specialized
4721 non-type argument shall not be dependent on a parameter of the
4722 specialization.
4724 Also, we verify that pack expansions only occur at the
4725 end of the argument list. */
4726 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4727 tpd2.parms = 0;
4728 for (i = 0; i < nargs; ++i)
4730 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4731 tree arg = TREE_VEC_ELT (inner_args, i);
4732 tree packed_args = NULL_TREE;
4733 int j, len = 1;
4735 if (ARGUMENT_PACK_P (arg))
4737 /* Extract the arguments from the argument pack. We'll be
4738 iterating over these in the following loop. */
4739 packed_args = ARGUMENT_PACK_ARGS (arg);
4740 len = TREE_VEC_LENGTH (packed_args);
4743 for (j = 0; j < len; j++)
4745 if (packed_args)
4746 /* Get the Jth argument in the parameter pack. */
4747 arg = TREE_VEC_ELT (packed_args, j);
4749 if (PACK_EXPANSION_P (arg))
4751 /* Pack expansions must come at the end of the
4752 argument list. */
4753 if ((packed_args && j < len - 1)
4754 || (!packed_args && i < nargs - 1))
4756 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4757 error ("parameter pack argument %qE must be at the "
4758 "end of the template argument list", arg);
4759 else
4760 error ("parameter pack argument %qT must be at the "
4761 "end of the template argument list", arg);
4765 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4766 /* We only care about the pattern. */
4767 arg = PACK_EXPANSION_PATTERN (arg);
4769 if (/* These first two lines are the `non-type' bit. */
4770 !TYPE_P (arg)
4771 && TREE_CODE (arg) != TEMPLATE_DECL
4772 /* This next two lines are the `argument expression is not just a
4773 simple identifier' condition and also the `specialized
4774 non-type argument' bit. */
4775 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4776 && !(REFERENCE_REF_P (arg)
4777 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4779 if ((!packed_args && tpd.arg_uses_template_parms[i])
4780 || (packed_args && uses_template_parms (arg)))
4781 error ("template argument %qE involves template parameter(s)",
4782 arg);
4783 else
4785 /* Look at the corresponding template parameter,
4786 marking which template parameters its type depends
4787 upon. */
4788 tree type = TREE_TYPE (parm);
4790 if (!tpd2.parms)
4792 /* We haven't yet initialized TPD2. Do so now. */
4793 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4794 /* The number of parameters here is the number in the
4795 main template, which, as checked in the assertion
4796 above, is NARGS. */
4797 tpd2.parms = XALLOCAVEC (int, nargs);
4798 tpd2.level =
4799 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4802 /* Mark the template parameters. But this time, we're
4803 looking for the template parameters of the main
4804 template, not in the specialization. */
4805 tpd2.current_arg = i;
4806 tpd2.arg_uses_template_parms[i] = 0;
4807 memset (tpd2.parms, 0, sizeof (int) * nargs);
4808 for_each_template_parm (type,
4809 &mark_template_parm,
4810 &tpd2,
4811 NULL,
4812 /*include_nondeduced_p=*/false);
4814 if (tpd2.arg_uses_template_parms [i])
4816 /* The type depended on some template parameters.
4817 If they are fully specialized in the
4818 specialization, that's OK. */
4819 int j;
4820 int count = 0;
4821 for (j = 0; j < nargs; ++j)
4822 if (tpd2.parms[j] != 0
4823 && tpd.arg_uses_template_parms [j])
4824 ++count;
4825 if (count != 0)
4826 error_n (input_location, count,
4827 "type %qT of template argument %qE depends "
4828 "on a template parameter",
4829 "type %qT of template argument %qE depends "
4830 "on template parameters",
4831 type,
4832 arg);
4839 /* We should only get here once. */
4840 if (TREE_CODE (decl) == TYPE_DECL)
4841 gcc_assert (!COMPLETE_TYPE_P (type));
4843 // Build the template decl.
4844 tree tmpl = build_template_decl (decl, current_template_parms,
4845 DECL_MEMBER_TEMPLATE_P (maintmpl));
4846 TREE_TYPE (tmpl) = type;
4847 DECL_TEMPLATE_RESULT (tmpl) = decl;
4848 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
4849 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
4850 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
4852 /* Give template template parms a DECL_CONTEXT of the template
4853 for which they are a parameter. */
4854 for (i = 0; i < ntparms; ++i)
4856 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
4857 if (TREE_CODE (parm) == TEMPLATE_DECL)
4858 DECL_CONTEXT (parm) = tmpl;
4861 if (VAR_P (decl))
4862 /* We didn't register this in check_explicit_specialization so we could
4863 wait until the constraints were set. */
4864 decl = register_specialization (decl, maintmpl, specargs, false, 0);
4865 else
4866 associate_classtype_constraints (type);
4868 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
4869 = tree_cons (specargs, tmpl,
4870 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
4871 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
4873 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
4874 inst = TREE_CHAIN (inst))
4876 tree instance = TREE_VALUE (inst);
4877 if (TYPE_P (instance)
4878 ? (COMPLETE_TYPE_P (instance)
4879 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
4880 : DECL_TEMPLATE_INSTANTIATION (instance))
4882 tree spec = most_specialized_partial_spec (instance, tf_none);
4883 tree inst_decl = (DECL_P (instance)
4884 ? instance : TYPE_NAME (instance));
4885 if (!spec)
4886 /* OK */;
4887 else if (spec == error_mark_node)
4888 permerror (input_location,
4889 "declaration of %qD ambiguates earlier template "
4890 "instantiation for %qD", decl, inst_decl);
4891 else if (TREE_VALUE (spec) == tmpl)
4892 permerror (input_location,
4893 "partial specialization of %qD after instantiation "
4894 "of %qD", decl, inst_decl);
4898 return decl;
4901 /* PARM is a template parameter of some form; return the corresponding
4902 TEMPLATE_PARM_INDEX. */
4904 static tree
4905 get_template_parm_index (tree parm)
4907 if (TREE_CODE (parm) == PARM_DECL
4908 || TREE_CODE (parm) == CONST_DECL)
4909 parm = DECL_INITIAL (parm);
4910 else if (TREE_CODE (parm) == TYPE_DECL
4911 || TREE_CODE (parm) == TEMPLATE_DECL)
4912 parm = TREE_TYPE (parm);
4913 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
4914 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
4915 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
4916 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
4917 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
4918 return parm;
4921 /* Subroutine of fixed_parameter_pack_p below. Look for any template
4922 parameter packs used by the template parameter PARM. */
4924 static void
4925 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
4927 /* A type parm can't refer to another parm. */
4928 if (TREE_CODE (parm) == TYPE_DECL)
4929 return;
4930 else if (TREE_CODE (parm) == PARM_DECL)
4932 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
4933 ppd, ppd->visited);
4934 return;
4937 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
4939 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
4940 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
4941 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
4944 /* PARM is a template parameter pack. Return any parameter packs used in
4945 its type or the type of any of its template parameters. If there are
4946 any such packs, it will be instantiated into a fixed template parameter
4947 list by partial instantiation rather than be fully deduced. */
4949 tree
4950 fixed_parameter_pack_p (tree parm)
4952 /* This can only be true in a member template. */
4953 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
4954 return NULL_TREE;
4955 /* This can only be true for a parameter pack. */
4956 if (!template_parameter_pack_p (parm))
4957 return NULL_TREE;
4958 /* A type parm can't refer to another parm. */
4959 if (TREE_CODE (parm) == TYPE_DECL)
4960 return NULL_TREE;
4962 tree parameter_packs = NULL_TREE;
4963 struct find_parameter_pack_data ppd;
4964 ppd.parameter_packs = &parameter_packs;
4965 ppd.visited = new hash_set<tree>;
4966 ppd.type_pack_expansion_p = false;
4968 fixed_parameter_pack_p_1 (parm, &ppd);
4970 delete ppd.visited;
4971 return parameter_packs;
4974 /* Check that a template declaration's use of default arguments and
4975 parameter packs is not invalid. Here, PARMS are the template
4976 parameters. IS_PRIMARY is true if DECL is the thing declared by
4977 a primary template. IS_PARTIAL is true if DECL is a partial
4978 specialization.
4980 IS_FRIEND_DECL is nonzero if DECL is a friend function template
4981 declaration (but not a definition); 1 indicates a declaration, 2
4982 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
4983 emitted for extraneous default arguments.
4985 Returns TRUE if there were no errors found, FALSE otherwise. */
4987 bool
4988 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
4989 bool is_partial, int is_friend_decl)
4991 const char *msg;
4992 int last_level_to_check;
4993 tree parm_level;
4994 bool no_errors = true;
4996 /* [temp.param]
4998 A default template-argument shall not be specified in a
4999 function template declaration or a function template definition, nor
5000 in the template-parameter-list of the definition of a member of a
5001 class template. */
5003 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5004 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
5005 /* You can't have a function template declaration in a local
5006 scope, nor you can you define a member of a class template in a
5007 local scope. */
5008 return true;
5010 if ((TREE_CODE (decl) == TYPE_DECL
5011 && TREE_TYPE (decl)
5012 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5013 || (TREE_CODE (decl) == FUNCTION_DECL
5014 && LAMBDA_FUNCTION_P (decl)))
5015 /* A lambda doesn't have an explicit declaration; don't complain
5016 about the parms of the enclosing class. */
5017 return true;
5019 if (current_class_type
5020 && !TYPE_BEING_DEFINED (current_class_type)
5021 && DECL_LANG_SPECIFIC (decl)
5022 && DECL_DECLARES_FUNCTION_P (decl)
5023 /* If this is either a friend defined in the scope of the class
5024 or a member function. */
5025 && (DECL_FUNCTION_MEMBER_P (decl)
5026 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5027 : DECL_FRIEND_CONTEXT (decl)
5028 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5029 : false)
5030 /* And, if it was a member function, it really was defined in
5031 the scope of the class. */
5032 && (!DECL_FUNCTION_MEMBER_P (decl)
5033 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5034 /* We already checked these parameters when the template was
5035 declared, so there's no need to do it again now. This function
5036 was defined in class scope, but we're processing its body now
5037 that the class is complete. */
5038 return true;
5040 /* Core issue 226 (C++0x only): the following only applies to class
5041 templates. */
5042 if (is_primary
5043 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5045 /* [temp.param]
5047 If a template-parameter has a default template-argument, all
5048 subsequent template-parameters shall have a default
5049 template-argument supplied. */
5050 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5052 tree inner_parms = TREE_VALUE (parm_level);
5053 int ntparms = TREE_VEC_LENGTH (inner_parms);
5054 int seen_def_arg_p = 0;
5055 int i;
5057 for (i = 0; i < ntparms; ++i)
5059 tree parm = TREE_VEC_ELT (inner_parms, i);
5061 if (parm == error_mark_node)
5062 continue;
5064 if (TREE_PURPOSE (parm))
5065 seen_def_arg_p = 1;
5066 else if (seen_def_arg_p
5067 && !template_parameter_pack_p (TREE_VALUE (parm)))
5069 error ("no default argument for %qD", TREE_VALUE (parm));
5070 /* For better subsequent error-recovery, we indicate that
5071 there should have been a default argument. */
5072 TREE_PURPOSE (parm) = error_mark_node;
5073 no_errors = false;
5075 else if (!is_partial
5076 && !is_friend_decl
5077 /* Don't complain about an enclosing partial
5078 specialization. */
5079 && parm_level == parms
5080 && TREE_CODE (decl) == TYPE_DECL
5081 && i < ntparms - 1
5082 && template_parameter_pack_p (TREE_VALUE (parm))
5083 /* A fixed parameter pack will be partially
5084 instantiated into a fixed length list. */
5085 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5087 /* A primary class template can only have one
5088 parameter pack, at the end of the template
5089 parameter list. */
5091 error ("parameter pack %q+D must be at the end of the"
5092 " template parameter list", TREE_VALUE (parm));
5094 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5095 = error_mark_node;
5096 no_errors = false;
5102 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5103 || is_partial
5104 || !is_primary
5105 || is_friend_decl)
5106 /* For an ordinary class template, default template arguments are
5107 allowed at the innermost level, e.g.:
5108 template <class T = int>
5109 struct S {};
5110 but, in a partial specialization, they're not allowed even
5111 there, as we have in [temp.class.spec]:
5113 The template parameter list of a specialization shall not
5114 contain default template argument values.
5116 So, for a partial specialization, or for a function template
5117 (in C++98/C++03), we look at all of them. */
5119 else
5120 /* But, for a primary class template that is not a partial
5121 specialization we look at all template parameters except the
5122 innermost ones. */
5123 parms = TREE_CHAIN (parms);
5125 /* Figure out what error message to issue. */
5126 if (is_friend_decl == 2)
5127 msg = G_("default template arguments may not be used in function template "
5128 "friend re-declaration");
5129 else if (is_friend_decl)
5130 msg = G_("default template arguments may not be used in function template "
5131 "friend declarations");
5132 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5133 msg = G_("default template arguments may not be used in function templates "
5134 "without -std=c++11 or -std=gnu++11");
5135 else if (is_partial)
5136 msg = G_("default template arguments may not be used in "
5137 "partial specializations");
5138 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5139 msg = G_("default argument for template parameter for class enclosing %qD");
5140 else
5141 /* Per [temp.param]/9, "A default template-argument shall not be
5142 specified in the template-parameter-lists of the definition of
5143 a member of a class template that appears outside of the member's
5144 class.", thus if we aren't handling a member of a class template
5145 there is no need to examine the parameters. */
5146 return true;
5148 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5149 /* If we're inside a class definition, there's no need to
5150 examine the parameters to the class itself. On the one
5151 hand, they will be checked when the class is defined, and,
5152 on the other, default arguments are valid in things like:
5153 template <class T = double>
5154 struct S { template <class U> void f(U); };
5155 Here the default argument for `S' has no bearing on the
5156 declaration of `f'. */
5157 last_level_to_check = template_class_depth (current_class_type) + 1;
5158 else
5159 /* Check everything. */
5160 last_level_to_check = 0;
5162 for (parm_level = parms;
5163 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5164 parm_level = TREE_CHAIN (parm_level))
5166 tree inner_parms = TREE_VALUE (parm_level);
5167 int i;
5168 int ntparms;
5170 ntparms = TREE_VEC_LENGTH (inner_parms);
5171 for (i = 0; i < ntparms; ++i)
5173 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5174 continue;
5176 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5178 if (msg)
5180 no_errors = false;
5181 if (is_friend_decl == 2)
5182 return no_errors;
5184 error (msg, decl);
5185 msg = 0;
5188 /* Clear out the default argument so that we are not
5189 confused later. */
5190 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5194 /* At this point, if we're still interested in issuing messages,
5195 they must apply to classes surrounding the object declared. */
5196 if (msg)
5197 msg = G_("default argument for template parameter for class "
5198 "enclosing %qD");
5201 return no_errors;
5204 /* Worker for push_template_decl_real, called via
5205 for_each_template_parm. DATA is really an int, indicating the
5206 level of the parameters we are interested in. If T is a template
5207 parameter of that level, return nonzero. */
5209 static int
5210 template_parm_this_level_p (tree t, void* data)
5212 int this_level = *(int *)data;
5213 int level;
5215 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5216 level = TEMPLATE_PARM_LEVEL (t);
5217 else
5218 level = TEMPLATE_TYPE_LEVEL (t);
5219 return level == this_level;
5222 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5223 DATA is really an int, indicating the innermost outer level of parameters.
5224 If T is a template parameter of that level or further out, return
5225 nonzero. */
5227 static int
5228 template_parm_outer_level (tree t, void *data)
5230 int this_level = *(int *)data;
5231 int level;
5233 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5234 level = TEMPLATE_PARM_LEVEL (t);
5235 else
5236 level = TEMPLATE_TYPE_LEVEL (t);
5237 return level <= this_level;
5240 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5241 parameters given by current_template_args, or reuses a
5242 previously existing one, if appropriate. Returns the DECL, or an
5243 equivalent one, if it is replaced via a call to duplicate_decls.
5245 If IS_FRIEND is true, DECL is a friend declaration. */
5247 tree
5248 push_template_decl_real (tree decl, bool is_friend)
5250 tree tmpl;
5251 tree args;
5252 tree info;
5253 tree ctx;
5254 bool is_primary;
5255 bool is_partial;
5256 int new_template_p = 0;
5257 /* True if the template is a member template, in the sense of
5258 [temp.mem]. */
5259 bool member_template_p = false;
5261 if (decl == error_mark_node || !current_template_parms)
5262 return error_mark_node;
5264 /* See if this is a partial specialization. */
5265 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5266 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5267 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5268 || (VAR_P (decl)
5269 && DECL_LANG_SPECIFIC (decl)
5270 && DECL_TEMPLATE_SPECIALIZATION (decl)
5271 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5273 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5274 is_friend = true;
5276 if (is_friend)
5277 /* For a friend, we want the context of the friend function, not
5278 the type of which it is a friend. */
5279 ctx = CP_DECL_CONTEXT (decl);
5280 else if (CP_DECL_CONTEXT (decl)
5281 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5282 /* In the case of a virtual function, we want the class in which
5283 it is defined. */
5284 ctx = CP_DECL_CONTEXT (decl);
5285 else
5286 /* Otherwise, if we're currently defining some class, the DECL
5287 is assumed to be a member of the class. */
5288 ctx = current_scope ();
5290 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5291 ctx = NULL_TREE;
5293 if (!DECL_CONTEXT (decl))
5294 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5296 /* See if this is a primary template. */
5297 if (is_friend && ctx
5298 && uses_template_parms_level (ctx, processing_template_decl))
5299 /* A friend template that specifies a class context, i.e.
5300 template <typename T> friend void A<T>::f();
5301 is not primary. */
5302 is_primary = false;
5303 else if (TREE_CODE (decl) == TYPE_DECL
5304 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5305 is_primary = false;
5306 else
5307 is_primary = template_parm_scope_p ();
5309 if (is_primary)
5311 warning (OPT_Wtemplates, "template %qD declared", decl);
5313 if (DECL_CLASS_SCOPE_P (decl))
5314 member_template_p = true;
5315 if (TREE_CODE (decl) == TYPE_DECL
5316 && anon_aggrname_p (DECL_NAME (decl)))
5318 error ("template class without a name");
5319 return error_mark_node;
5321 else if (TREE_CODE (decl) == FUNCTION_DECL)
5323 if (member_template_p)
5325 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5326 error ("member template %qD may not have virt-specifiers", decl);
5328 if (DECL_DESTRUCTOR_P (decl))
5330 /* [temp.mem]
5332 A destructor shall not be a member template. */
5333 error ("destructor %qD declared as member template", decl);
5334 return error_mark_node;
5336 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5337 && (!prototype_p (TREE_TYPE (decl))
5338 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5339 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5340 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5341 == void_list_node)))
5343 /* [basic.stc.dynamic.allocation]
5345 An allocation function can be a function
5346 template. ... Template allocation functions shall
5347 have two or more parameters. */
5348 error ("invalid template declaration of %qD", decl);
5349 return error_mark_node;
5352 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5353 && CLASS_TYPE_P (TREE_TYPE (decl)))
5355 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5356 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5357 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5359 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5360 if (TREE_CODE (t) == TYPE_DECL)
5361 t = TREE_TYPE (t);
5362 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5363 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5366 else if (TREE_CODE (decl) == TYPE_DECL
5367 && TYPE_DECL_ALIAS_P (decl))
5368 /* alias-declaration */
5369 gcc_assert (!DECL_ARTIFICIAL (decl));
5370 else if (VAR_P (decl))
5371 /* C++14 variable template. */;
5372 else
5374 error ("template declaration of %q#D", decl);
5375 return error_mark_node;
5379 /* Check to see that the rules regarding the use of default
5380 arguments are not being violated. */
5381 check_default_tmpl_args (decl, current_template_parms,
5382 is_primary, is_partial, /*is_friend_decl=*/0);
5384 /* Ensure that there are no parameter packs in the type of this
5385 declaration that have not been expanded. */
5386 if (TREE_CODE (decl) == FUNCTION_DECL)
5388 /* Check each of the arguments individually to see if there are
5389 any bare parameter packs. */
5390 tree type = TREE_TYPE (decl);
5391 tree arg = DECL_ARGUMENTS (decl);
5392 tree argtype = TYPE_ARG_TYPES (type);
5394 while (arg && argtype)
5396 if (!DECL_PACK_P (arg)
5397 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5399 /* This is a PARM_DECL that contains unexpanded parameter
5400 packs. We have already complained about this in the
5401 check_for_bare_parameter_packs call, so just replace
5402 these types with ERROR_MARK_NODE. */
5403 TREE_TYPE (arg) = error_mark_node;
5404 TREE_VALUE (argtype) = error_mark_node;
5407 arg = DECL_CHAIN (arg);
5408 argtype = TREE_CHAIN (argtype);
5411 /* Check for bare parameter packs in the return type and the
5412 exception specifiers. */
5413 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5414 /* Errors were already issued, set return type to int
5415 as the frontend doesn't expect error_mark_node as
5416 the return type. */
5417 TREE_TYPE (type) = integer_type_node;
5418 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5419 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5421 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5422 && TYPE_DECL_ALIAS_P (decl))
5423 ? DECL_ORIGINAL_TYPE (decl)
5424 : TREE_TYPE (decl)))
5426 TREE_TYPE (decl) = error_mark_node;
5427 return error_mark_node;
5430 if (is_partial)
5431 return process_partial_specialization (decl);
5433 args = current_template_args ();
5435 if (!ctx
5436 || TREE_CODE (ctx) == FUNCTION_DECL
5437 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5438 || (TREE_CODE (decl) == TYPE_DECL
5439 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5440 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5442 if (DECL_LANG_SPECIFIC (decl)
5443 && DECL_TEMPLATE_INFO (decl)
5444 && DECL_TI_TEMPLATE (decl))
5445 tmpl = DECL_TI_TEMPLATE (decl);
5446 /* If DECL is a TYPE_DECL for a class-template, then there won't
5447 be DECL_LANG_SPECIFIC. The information equivalent to
5448 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5449 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5450 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5451 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5453 /* Since a template declaration already existed for this
5454 class-type, we must be redeclaring it here. Make sure
5455 that the redeclaration is valid. */
5456 redeclare_class_template (TREE_TYPE (decl),
5457 current_template_parms,
5458 current_template_constraints ());
5459 /* We don't need to create a new TEMPLATE_DECL; just use the
5460 one we already had. */
5461 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5463 else
5465 tmpl = build_template_decl (decl, current_template_parms,
5466 member_template_p);
5467 new_template_p = 1;
5469 if (DECL_LANG_SPECIFIC (decl)
5470 && DECL_TEMPLATE_SPECIALIZATION (decl))
5472 /* A specialization of a member template of a template
5473 class. */
5474 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5475 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5476 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5480 else
5482 tree a, t, current, parms;
5483 int i;
5484 tree tinfo = get_template_info (decl);
5486 if (!tinfo)
5488 error ("template definition of non-template %q#D", decl);
5489 return error_mark_node;
5492 tmpl = TI_TEMPLATE (tinfo);
5494 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5495 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5496 && DECL_TEMPLATE_SPECIALIZATION (decl)
5497 && DECL_MEMBER_TEMPLATE_P (tmpl))
5499 tree new_tmpl;
5501 /* The declaration is a specialization of a member
5502 template, declared outside the class. Therefore, the
5503 innermost template arguments will be NULL, so we
5504 replace them with the arguments determined by the
5505 earlier call to check_explicit_specialization. */
5506 args = DECL_TI_ARGS (decl);
5508 new_tmpl
5509 = build_template_decl (decl, current_template_parms,
5510 member_template_p);
5511 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5512 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5513 DECL_TI_TEMPLATE (decl) = new_tmpl;
5514 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5515 DECL_TEMPLATE_INFO (new_tmpl)
5516 = build_template_info (tmpl, args);
5518 register_specialization (new_tmpl,
5519 most_general_template (tmpl),
5520 args,
5521 is_friend, 0);
5522 return decl;
5525 /* Make sure the template headers we got make sense. */
5527 parms = DECL_TEMPLATE_PARMS (tmpl);
5528 i = TMPL_PARMS_DEPTH (parms);
5529 if (TMPL_ARGS_DEPTH (args) != i)
5531 error ("expected %d levels of template parms for %q#D, got %d",
5532 i, decl, TMPL_ARGS_DEPTH (args));
5533 DECL_INTERFACE_KNOWN (decl) = 1;
5534 return error_mark_node;
5536 else
5537 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5539 a = TMPL_ARGS_LEVEL (args, i);
5540 t = INNERMOST_TEMPLATE_PARMS (parms);
5542 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5544 if (current == decl)
5545 error ("got %d template parameters for %q#D",
5546 TREE_VEC_LENGTH (a), decl);
5547 else
5548 error ("got %d template parameters for %q#T",
5549 TREE_VEC_LENGTH (a), current);
5550 error (" but %d required", TREE_VEC_LENGTH (t));
5551 /* Avoid crash in import_export_decl. */
5552 DECL_INTERFACE_KNOWN (decl) = 1;
5553 return error_mark_node;
5556 if (current == decl)
5557 current = ctx;
5558 else if (current == NULL_TREE)
5559 /* Can happen in erroneous input. */
5560 break;
5561 else
5562 current = get_containing_scope (current);
5565 /* Check that the parms are used in the appropriate qualifying scopes
5566 in the declarator. */
5567 if (!comp_template_args
5568 (TI_ARGS (tinfo),
5569 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5571 error ("\
5572 template arguments to %qD do not match original template %qD",
5573 decl, DECL_TEMPLATE_RESULT (tmpl));
5574 if (!uses_template_parms (TI_ARGS (tinfo)))
5575 inform (input_location, "use template<> for an explicit specialization");
5576 /* Avoid crash in import_export_decl. */
5577 DECL_INTERFACE_KNOWN (decl) = 1;
5578 return error_mark_node;
5582 DECL_TEMPLATE_RESULT (tmpl) = decl;
5583 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5585 /* Push template declarations for global functions and types. Note
5586 that we do not try to push a global template friend declared in a
5587 template class; such a thing may well depend on the template
5588 parameters of the class. */
5589 if (new_template_p && !ctx
5590 && !(is_friend && template_class_depth (current_class_type) > 0))
5592 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5593 if (tmpl == error_mark_node)
5594 return error_mark_node;
5596 /* Hide template friend classes that haven't been declared yet. */
5597 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5599 DECL_ANTICIPATED (tmpl) = 1;
5600 DECL_FRIEND_P (tmpl) = 1;
5604 if (is_primary)
5606 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5607 int i;
5609 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5610 if (DECL_CONV_FN_P (tmpl))
5612 int depth = TMPL_PARMS_DEPTH (parms);
5614 /* It is a conversion operator. See if the type converted to
5615 depends on innermost template operands. */
5617 if (uses_template_parms_level (TREE_TYPE (TREE_TYPE (tmpl)),
5618 depth))
5619 DECL_TEMPLATE_CONV_FN_P (tmpl) = 1;
5622 /* Give template template parms a DECL_CONTEXT of the template
5623 for which they are a parameter. */
5624 parms = INNERMOST_TEMPLATE_PARMS (parms);
5625 for (i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5627 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5628 if (TREE_CODE (parm) == TEMPLATE_DECL)
5629 DECL_CONTEXT (parm) = tmpl;
5632 if (TREE_CODE (decl) == TYPE_DECL
5633 && TYPE_DECL_ALIAS_P (decl)
5634 && complex_alias_template_p (tmpl))
5635 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5638 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5639 back to its most general template. If TMPL is a specialization,
5640 ARGS may only have the innermost set of arguments. Add the missing
5641 argument levels if necessary. */
5642 if (DECL_TEMPLATE_INFO (tmpl))
5643 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5645 info = build_template_info (tmpl, args);
5647 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5648 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5649 else
5651 if (is_primary)
5652 retrofit_lang_decl (decl);
5653 if (DECL_LANG_SPECIFIC (decl))
5654 DECL_TEMPLATE_INFO (decl) = info;
5657 if (flag_implicit_templates
5658 && !is_friend
5659 && TREE_PUBLIC (decl)
5660 && VAR_OR_FUNCTION_DECL_P (decl))
5661 /* Set DECL_COMDAT on template instantiations; if we force
5662 them to be emitted by explicit instantiation or -frepo,
5663 mark_needed will tell cgraph to do the right thing. */
5664 DECL_COMDAT (decl) = true;
5666 return DECL_TEMPLATE_RESULT (tmpl);
5669 tree
5670 push_template_decl (tree decl)
5672 return push_template_decl_real (decl, false);
5675 /* FN is an inheriting constructor that inherits from the constructor
5676 template INHERITED; turn FN into a constructor template with a matching
5677 template header. */
5679 tree
5680 add_inherited_template_parms (tree fn, tree inherited)
5682 tree inner_parms
5683 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5684 inner_parms = copy_node (inner_parms);
5685 tree parms
5686 = tree_cons (size_int (processing_template_decl + 1),
5687 inner_parms, current_template_parms);
5688 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5689 tree args = template_parms_to_args (parms);
5690 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5691 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5692 DECL_TEMPLATE_RESULT (tmpl) = fn;
5693 DECL_ARTIFICIAL (tmpl) = true;
5694 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5695 return tmpl;
5698 /* Called when a class template TYPE is redeclared with the indicated
5699 template PARMS, e.g.:
5701 template <class T> struct S;
5702 template <class T> struct S {}; */
5704 bool
5705 redeclare_class_template (tree type, tree parms, tree cons)
5707 tree tmpl;
5708 tree tmpl_parms;
5709 int i;
5711 if (!TYPE_TEMPLATE_INFO (type))
5713 error ("%qT is not a template type", type);
5714 return false;
5717 tmpl = TYPE_TI_TEMPLATE (type);
5718 if (!PRIMARY_TEMPLATE_P (tmpl))
5719 /* The type is nested in some template class. Nothing to worry
5720 about here; there are no new template parameters for the nested
5721 type. */
5722 return true;
5724 if (!parms)
5726 error ("template specifiers not specified in declaration of %qD",
5727 tmpl);
5728 return false;
5731 parms = INNERMOST_TEMPLATE_PARMS (parms);
5732 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5734 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5736 error_n (input_location, TREE_VEC_LENGTH (parms),
5737 "redeclared with %d template parameter",
5738 "redeclared with %d template parameters",
5739 TREE_VEC_LENGTH (parms));
5740 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5741 "previous declaration %qD used %d template parameter",
5742 "previous declaration %qD used %d template parameters",
5743 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5744 return false;
5747 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5749 tree tmpl_parm;
5750 tree parm;
5751 tree tmpl_default;
5752 tree parm_default;
5754 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5755 || TREE_VEC_ELT (parms, i) == error_mark_node)
5756 continue;
5758 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5759 if (error_operand_p (tmpl_parm))
5760 return false;
5762 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5763 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5764 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5766 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5767 TEMPLATE_DECL. */
5768 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5769 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5770 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5771 || (TREE_CODE (tmpl_parm) != PARM_DECL
5772 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5773 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5774 || (TREE_CODE (tmpl_parm) == PARM_DECL
5775 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5776 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5778 error ("template parameter %q+#D", tmpl_parm);
5779 error ("redeclared here as %q#D", parm);
5780 return false;
5783 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5785 /* We have in [temp.param]:
5787 A template-parameter may not be given default arguments
5788 by two different declarations in the same scope. */
5789 error_at (input_location, "redefinition of default argument for %q#D", parm);
5790 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5791 "original definition appeared here");
5792 return false;
5795 if (parm_default != NULL_TREE)
5796 /* Update the previous template parameters (which are the ones
5797 that will really count) with the new default value. */
5798 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5799 else if (tmpl_default != NULL_TREE)
5800 /* Update the new parameters, too; they'll be used as the
5801 parameters for any members. */
5802 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5804 /* Give each template template parm in this redeclaration a
5805 DECL_CONTEXT of the template for which they are a parameter. */
5806 if (TREE_CODE (parm) == TEMPLATE_DECL)
5808 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5809 DECL_CONTEXT (parm) = tmpl;
5812 if (TREE_CODE (parm) == TYPE_DECL)
5813 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
5816 // Cannot redeclare a class template with a different set of constraints.
5817 if (!equivalent_constraints (get_constraints (tmpl), cons))
5819 error_at (input_location, "redeclaration %q#D with different "
5820 "constraints", tmpl);
5821 inform (DECL_SOURCE_LOCATION (tmpl),
5822 "original declaration appeared here");
5825 return true;
5828 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5829 to be used when the caller has already checked
5830 (processing_template_decl
5831 && !instantiation_dependent_expression_p (expr)
5832 && potential_constant_expression (expr))
5833 and cleared processing_template_decl. */
5835 tree
5836 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
5838 return tsubst_copy_and_build (expr,
5839 /*args=*/NULL_TREE,
5840 complain,
5841 /*in_decl=*/NULL_TREE,
5842 /*function_p=*/false,
5843 /*integral_constant_expression_p=*/true);
5846 /* Simplify EXPR if it is a non-dependent expression. Returns the
5847 (possibly simplified) expression. */
5849 tree
5850 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
5852 if (expr == NULL_TREE)
5853 return NULL_TREE;
5855 /* If we're in a template, but EXPR isn't value dependent, simplify
5856 it. We're supposed to treat:
5858 template <typename T> void f(T[1 + 1]);
5859 template <typename T> void f(T[2]);
5861 as two declarations of the same function, for example. */
5862 if (processing_template_decl
5863 && potential_nondependent_constant_expression (expr))
5865 processing_template_decl_sentinel s;
5866 expr = instantiate_non_dependent_expr_internal (expr, complain);
5868 return expr;
5871 tree
5872 instantiate_non_dependent_expr (tree expr)
5874 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
5877 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
5878 an uninstantiated expression. */
5880 tree
5881 instantiate_non_dependent_or_null (tree expr)
5883 if (expr == NULL_TREE)
5884 return NULL_TREE;
5885 if (processing_template_decl)
5887 if (!potential_nondependent_constant_expression (expr))
5888 expr = NULL_TREE;
5889 else
5891 processing_template_decl_sentinel s;
5892 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
5895 return expr;
5898 /* True iff T is a specialization of a variable template. */
5900 bool
5901 variable_template_specialization_p (tree t)
5903 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
5904 return false;
5905 tree tmpl = DECL_TI_TEMPLATE (t);
5906 return variable_template_p (tmpl);
5909 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
5910 template declaration, or a TYPE_DECL for an alias declaration. */
5912 bool
5913 alias_type_or_template_p (tree t)
5915 if (t == NULL_TREE)
5916 return false;
5917 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
5918 || (TYPE_P (t)
5919 && TYPE_NAME (t)
5920 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
5921 || DECL_ALIAS_TEMPLATE_P (t));
5924 /* Return TRUE iff T is a specialization of an alias template. */
5926 bool
5927 alias_template_specialization_p (const_tree t)
5929 /* It's an alias template specialization if it's an alias and its
5930 TYPE_NAME is a specialization of a primary template. */
5931 if (TYPE_ALIAS_P (t))
5932 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
5933 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo));
5935 return false;
5938 /* An alias template is complex from a SFINAE perspective if a template-id
5939 using that alias can be ill-formed when the expansion is not, as with
5940 the void_t template. We determine this by checking whether the
5941 expansion for the alias template uses all its template parameters. */
5943 struct uses_all_template_parms_data
5945 int level;
5946 bool *seen;
5949 static int
5950 uses_all_template_parms_r (tree t, void *data_)
5952 struct uses_all_template_parms_data &data
5953 = *(struct uses_all_template_parms_data*)data_;
5954 tree idx = get_template_parm_index (t);
5956 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
5957 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
5958 return 0;
5961 static bool
5962 complex_alias_template_p (const_tree tmpl)
5964 struct uses_all_template_parms_data data;
5965 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
5966 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5967 data.level = TMPL_PARMS_DEPTH (parms);
5968 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
5969 data.seen = XALLOCAVEC (bool, len);
5970 for (int i = 0; i < len; ++i)
5971 data.seen[i] = false;
5973 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
5974 for (int i = 0; i < len; ++i)
5975 if (!data.seen[i])
5976 return true;
5977 return false;
5980 /* Return TRUE iff T is a specialization of a complex alias template with
5981 dependent template-arguments. */
5983 bool
5984 dependent_alias_template_spec_p (const_tree t)
5986 if (!alias_template_specialization_p (t))
5987 return false;
5989 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
5990 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo)))
5991 return false;
5993 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
5994 if (!any_dependent_template_arguments_p (args))
5995 return false;
5997 return true;
6000 /* Return the number of innermost template parameters in TMPL. */
6002 static int
6003 num_innermost_template_parms (tree tmpl)
6005 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6006 return TREE_VEC_LENGTH (parms);
6009 /* Return either TMPL or another template that it is equivalent to under DR
6010 1286: An alias that just changes the name of a template is equivalent to
6011 the other template. */
6013 static tree
6014 get_underlying_template (tree tmpl)
6016 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6017 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6019 /* Determine if the alias is equivalent to an underlying template. */
6020 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6021 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6022 if (!tinfo)
6023 break;
6025 tree underlying = TI_TEMPLATE (tinfo);
6026 if (!PRIMARY_TEMPLATE_P (underlying)
6027 || (num_innermost_template_parms (tmpl)
6028 != num_innermost_template_parms (underlying)))
6029 break;
6031 tree alias_args = INNERMOST_TEMPLATE_ARGS
6032 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
6033 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6034 break;
6036 /* Alias is equivalent. Strip it and repeat. */
6037 tmpl = underlying;
6040 return tmpl;
6043 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6044 must be a reference-to-function or a pointer-to-function type, as specified
6045 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6046 and check that the resulting function has external linkage. */
6048 static tree
6049 convert_nontype_argument_function (tree type, tree expr,
6050 tsubst_flags_t complain)
6052 tree fns = expr;
6053 tree fn, fn_no_ptr;
6054 linkage_kind linkage;
6056 fn = instantiate_type (type, fns, tf_none);
6057 if (fn == error_mark_node)
6058 return error_mark_node;
6060 if (value_dependent_expression_p (fn))
6061 goto accept;
6063 fn_no_ptr = strip_fnptr_conv (fn);
6064 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6065 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6066 if (BASELINK_P (fn_no_ptr))
6067 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6069 /* [temp.arg.nontype]/1
6071 A template-argument for a non-type, non-template template-parameter
6072 shall be one of:
6073 [...]
6074 -- the address of an object or function with external [C++11: or
6075 internal] linkage. */
6077 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6079 if (complain & tf_error)
6081 error ("%qE is not a valid template argument for type %qT",
6082 expr, type);
6083 if (TYPE_PTR_P (type))
6084 inform (input_location, "it must be the address of a function "
6085 "with external linkage");
6086 else
6087 inform (input_location, "it must be the name of a function with "
6088 "external linkage");
6090 return NULL_TREE;
6093 linkage = decl_linkage (fn_no_ptr);
6094 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6096 if (complain & tf_error)
6098 if (cxx_dialect >= cxx11)
6099 error ("%qE is not a valid template argument for type %qT "
6100 "because %qD has no linkage",
6101 expr, type, fn_no_ptr);
6102 else
6103 error ("%qE is not a valid template argument for type %qT "
6104 "because %qD does not have external linkage",
6105 expr, type, fn_no_ptr);
6107 return NULL_TREE;
6110 accept:
6111 if (TREE_CODE (type) == REFERENCE_TYPE)
6112 fn = build_address (fn);
6113 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6114 fn = build_nop (type, fn);
6116 return fn;
6119 /* Subroutine of convert_nontype_argument.
6120 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6121 Emit an error otherwise. */
6123 static bool
6124 check_valid_ptrmem_cst_expr (tree type, tree expr,
6125 tsubst_flags_t complain)
6127 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6128 tree orig_expr = expr;
6129 STRIP_NOPS (expr);
6130 if (null_ptr_cst_p (expr))
6131 return true;
6132 if (TREE_CODE (expr) == PTRMEM_CST
6133 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6134 PTRMEM_CST_CLASS (expr)))
6135 return true;
6136 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6137 return true;
6138 if (processing_template_decl
6139 && TREE_CODE (expr) == ADDR_EXPR
6140 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6141 return true;
6142 if (complain & tf_error)
6144 error_at (loc, "%qE is not a valid template argument for type %qT",
6145 orig_expr, type);
6146 if (TREE_CODE (expr) != PTRMEM_CST)
6147 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6148 else
6149 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6151 return false;
6154 /* Returns TRUE iff the address of OP is value-dependent.
6156 14.6.2.4 [temp.dep.temp]:
6157 A non-integral non-type template-argument is dependent if its type is
6158 dependent or it has either of the following forms
6159 qualified-id
6160 & qualified-id
6161 and contains a nested-name-specifier which specifies a class-name that
6162 names a dependent type.
6164 We generalize this to just say that the address of a member of a
6165 dependent class is value-dependent; the above doesn't cover the
6166 address of a static data member named with an unqualified-id. */
6168 static bool
6169 has_value_dependent_address (tree op)
6171 /* We could use get_inner_reference here, but there's no need;
6172 this is only relevant for template non-type arguments, which
6173 can only be expressed as &id-expression. */
6174 if (DECL_P (op))
6176 tree ctx = CP_DECL_CONTEXT (op);
6177 if (TYPE_P (ctx) && dependent_type_p (ctx))
6178 return true;
6181 return false;
6184 /* The next set of functions are used for providing helpful explanatory
6185 diagnostics for failed overload resolution. Their messages should be
6186 indented by two spaces for consistency with the messages in
6187 call.c */
6189 static int
6190 unify_success (bool /*explain_p*/)
6192 return 0;
6195 /* Other failure functions should call this one, to provide a single function
6196 for setting a breakpoint on. */
6198 static int
6199 unify_invalid (bool /*explain_p*/)
6201 return 1;
6204 static int
6205 unify_parameter_deduction_failure (bool explain_p, tree parm)
6207 if (explain_p)
6208 inform (input_location,
6209 " couldn't deduce template parameter %qD", parm);
6210 return unify_invalid (explain_p);
6213 static int
6214 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6216 if (explain_p)
6217 inform (input_location,
6218 " types %qT and %qT have incompatible cv-qualifiers",
6219 parm, arg);
6220 return unify_invalid (explain_p);
6223 static int
6224 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6226 if (explain_p)
6227 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6228 return unify_invalid (explain_p);
6231 static int
6232 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6234 if (explain_p)
6235 inform (input_location,
6236 " template parameter %qD is not a parameter pack, but "
6237 "argument %qD is",
6238 parm, arg);
6239 return unify_invalid (explain_p);
6242 static int
6243 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6245 if (explain_p)
6246 inform (input_location,
6247 " template argument %qE does not match "
6248 "pointer-to-member constant %qE",
6249 arg, parm);
6250 return unify_invalid (explain_p);
6253 static int
6254 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6256 if (explain_p)
6257 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6258 return unify_invalid (explain_p);
6261 static int
6262 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6264 if (explain_p)
6265 inform (input_location,
6266 " inconsistent parameter pack deduction with %qT and %qT",
6267 old_arg, new_arg);
6268 return unify_invalid (explain_p);
6271 static int
6272 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6274 if (explain_p)
6276 if (TYPE_P (parm))
6277 inform (input_location,
6278 " deduced conflicting types for parameter %qT (%qT and %qT)",
6279 parm, first, second);
6280 else
6281 inform (input_location,
6282 " deduced conflicting values for non-type parameter "
6283 "%qE (%qE and %qE)", parm, first, second);
6285 return unify_invalid (explain_p);
6288 static int
6289 unify_vla_arg (bool explain_p, tree arg)
6291 if (explain_p)
6292 inform (input_location,
6293 " variable-sized array type %qT is not "
6294 "a valid template argument",
6295 arg);
6296 return unify_invalid (explain_p);
6299 static int
6300 unify_method_type_error (bool explain_p, tree arg)
6302 if (explain_p)
6303 inform (input_location,
6304 " member function type %qT is not a valid template argument",
6305 arg);
6306 return unify_invalid (explain_p);
6309 static int
6310 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6312 if (explain_p)
6314 if (least_p)
6315 inform_n (input_location, wanted,
6316 " candidate expects at least %d argument, %d provided",
6317 " candidate expects at least %d arguments, %d provided",
6318 wanted, have);
6319 else
6320 inform_n (input_location, wanted,
6321 " candidate expects %d argument, %d provided",
6322 " candidate expects %d arguments, %d provided",
6323 wanted, have);
6325 return unify_invalid (explain_p);
6328 static int
6329 unify_too_many_arguments (bool explain_p, int have, int wanted)
6331 return unify_arity (explain_p, have, wanted);
6334 static int
6335 unify_too_few_arguments (bool explain_p, int have, int wanted,
6336 bool least_p = false)
6338 return unify_arity (explain_p, have, wanted, least_p);
6341 static int
6342 unify_arg_conversion (bool explain_p, tree to_type,
6343 tree from_type, tree arg)
6345 if (explain_p)
6346 inform (EXPR_LOC_OR_LOC (arg, input_location),
6347 " cannot convert %qE (type %qT) to type %qT",
6348 arg, from_type, to_type);
6349 return unify_invalid (explain_p);
6352 static int
6353 unify_no_common_base (bool explain_p, enum template_base_result r,
6354 tree parm, tree arg)
6356 if (explain_p)
6357 switch (r)
6359 case tbr_ambiguous_baseclass:
6360 inform (input_location, " %qT is an ambiguous base class of %qT",
6361 parm, arg);
6362 break;
6363 default:
6364 inform (input_location, " %qT is not derived from %qT", arg, parm);
6365 break;
6367 return unify_invalid (explain_p);
6370 static int
6371 unify_inconsistent_template_template_parameters (bool explain_p)
6373 if (explain_p)
6374 inform (input_location,
6375 " template parameters of a template template argument are "
6376 "inconsistent with other deduced template arguments");
6377 return unify_invalid (explain_p);
6380 static int
6381 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6383 if (explain_p)
6384 inform (input_location,
6385 " can't deduce a template for %qT from non-template type %qT",
6386 parm, arg);
6387 return unify_invalid (explain_p);
6390 static int
6391 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6393 if (explain_p)
6394 inform (input_location,
6395 " template argument %qE does not match %qE", arg, parm);
6396 return unify_invalid (explain_p);
6399 static int
6400 unify_overload_resolution_failure (bool explain_p, tree arg)
6402 if (explain_p)
6403 inform (input_location,
6404 " could not resolve address from overloaded function %qE",
6405 arg);
6406 return unify_invalid (explain_p);
6409 /* Attempt to convert the non-type template parameter EXPR to the
6410 indicated TYPE. If the conversion is successful, return the
6411 converted value. If the conversion is unsuccessful, return
6412 NULL_TREE if we issued an error message, or error_mark_node if we
6413 did not. We issue error messages for out-and-out bad template
6414 parameters, but not simply because the conversion failed, since we
6415 might be just trying to do argument deduction. Both TYPE and EXPR
6416 must be non-dependent.
6418 The conversion follows the special rules described in
6419 [temp.arg.nontype], and it is much more strict than an implicit
6420 conversion.
6422 This function is called twice for each template argument (see
6423 lookup_template_class for a more accurate description of this
6424 problem). This means that we need to handle expressions which
6425 are not valid in a C++ source, but can be created from the
6426 first call (for instance, casts to perform conversions). These
6427 hacks can go away after we fix the double coercion problem. */
6429 static tree
6430 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6432 tree expr_type;
6433 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6434 tree orig_expr = expr;
6436 /* Detect immediately string literals as invalid non-type argument.
6437 This special-case is not needed for correctness (we would easily
6438 catch this later), but only to provide better diagnostic for this
6439 common user mistake. As suggested by DR 100, we do not mention
6440 linkage issues in the diagnostic as this is not the point. */
6441 /* FIXME we're making this OK. */
6442 if (TREE_CODE (expr) == STRING_CST)
6444 if (complain & tf_error)
6445 error ("%qE is not a valid template argument for type %qT "
6446 "because string literals can never be used in this context",
6447 expr, type);
6448 return NULL_TREE;
6451 /* Add the ADDR_EXPR now for the benefit of
6452 value_dependent_expression_p. */
6453 if (TYPE_PTROBV_P (type)
6454 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6456 expr = decay_conversion (expr, complain);
6457 if (expr == error_mark_node)
6458 return error_mark_node;
6461 /* If we are in a template, EXPR may be non-dependent, but still
6462 have a syntactic, rather than semantic, form. For example, EXPR
6463 might be a SCOPE_REF, rather than the VAR_DECL to which the
6464 SCOPE_REF refers. Preserving the qualifying scope is necessary
6465 so that access checking can be performed when the template is
6466 instantiated -- but here we need the resolved form so that we can
6467 convert the argument. */
6468 bool non_dep = false;
6469 if (TYPE_REF_OBJ_P (type)
6470 && has_value_dependent_address (expr))
6471 /* If we want the address and it's value-dependent, don't fold. */;
6472 else if (processing_template_decl
6473 && potential_nondependent_constant_expression (expr))
6474 non_dep = true;
6475 if (error_operand_p (expr))
6476 return error_mark_node;
6477 expr_type = TREE_TYPE (expr);
6478 if (TREE_CODE (type) == REFERENCE_TYPE)
6479 expr = mark_lvalue_use (expr);
6480 else
6481 expr = mark_rvalue_use (expr);
6483 /* If the argument is non-dependent, perform any conversions in
6484 non-dependent context as well. */
6485 processing_template_decl_sentinel s (non_dep);
6486 if (non_dep)
6487 expr = instantiate_non_dependent_expr_internal (expr, complain);
6489 if (value_dependent_expression_p (expr))
6490 expr = canonicalize_expr_argument (expr, complain);
6492 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6493 to a non-type argument of "nullptr". */
6494 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
6495 expr = fold_simple (convert (type, expr));
6497 /* In C++11, integral or enumeration non-type template arguments can be
6498 arbitrary constant expressions. Pointer and pointer to
6499 member arguments can be general constant expressions that evaluate
6500 to a null value, but otherwise still need to be of a specific form. */
6501 if (cxx_dialect >= cxx11)
6503 if (TREE_CODE (expr) == PTRMEM_CST)
6504 /* A PTRMEM_CST is already constant, and a valid template
6505 argument for a parameter of pointer to member type, we just want
6506 to leave it in that form rather than lower it to a
6507 CONSTRUCTOR. */;
6508 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6509 || cxx_dialect >= cxx1z)
6511 /* C++17: A template-argument for a non-type template-parameter shall
6512 be a converted constant expression (8.20) of the type of the
6513 template-parameter. */
6514 expr = build_converted_constant_expr (type, expr, complain);
6515 if (expr == error_mark_node)
6516 return error_mark_node;
6517 expr = maybe_constant_value (expr);
6518 expr = convert_from_reference (expr);
6520 else if (TYPE_PTR_OR_PTRMEM_P (type))
6522 tree folded = maybe_constant_value (expr);
6523 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6524 : null_member_pointer_value_p (folded))
6525 expr = folded;
6529 /* HACK: Due to double coercion, we can get a
6530 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6531 which is the tree that we built on the first call (see
6532 below when coercing to reference to object or to reference to
6533 function). We just strip everything and get to the arg.
6534 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6535 for examples. */
6536 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6538 tree probe_type, probe = expr;
6539 if (REFERENCE_REF_P (probe))
6540 probe = TREE_OPERAND (probe, 0);
6541 probe_type = TREE_TYPE (probe);
6542 if (TREE_CODE (probe) == NOP_EXPR)
6544 /* ??? Maybe we could use convert_from_reference here, but we
6545 would need to relax its constraints because the NOP_EXPR
6546 could actually change the type to something more cv-qualified,
6547 and this is not folded by convert_from_reference. */
6548 tree addr = TREE_OPERAND (probe, 0);
6549 if (TREE_CODE (probe_type) == REFERENCE_TYPE
6550 && TREE_CODE (addr) == ADDR_EXPR
6551 && TYPE_PTR_P (TREE_TYPE (addr))
6552 && (same_type_ignoring_top_level_qualifiers_p
6553 (TREE_TYPE (probe_type),
6554 TREE_TYPE (TREE_TYPE (addr)))))
6556 expr = TREE_OPERAND (addr, 0);
6557 expr_type = TREE_TYPE (probe_type);
6562 /* [temp.arg.nontype]/5, bullet 1
6564 For a non-type template-parameter of integral or enumeration type,
6565 integral promotions (_conv.prom_) and integral conversions
6566 (_conv.integral_) are applied. */
6567 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6569 if (cxx_dialect < cxx11)
6571 tree t = build_converted_constant_expr (type, expr, complain);
6572 t = maybe_constant_value (t);
6573 if (t != error_mark_node)
6574 expr = t;
6577 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6578 return error_mark_node;
6580 /* Notice that there are constant expressions like '4 % 0' which
6581 do not fold into integer constants. */
6582 if (TREE_CODE (expr) != INTEGER_CST
6583 && !value_dependent_expression_p (expr))
6585 if (complain & tf_error)
6587 int errs = errorcount, warns = warningcount + werrorcount;
6588 if (!require_potential_constant_expression (expr))
6589 expr = error_mark_node;
6590 else
6591 expr = cxx_constant_value (expr);
6592 if (errorcount > errs || warningcount + werrorcount > warns)
6593 inform (loc, "in template argument for type %qT ", type);
6594 if (expr == error_mark_node)
6595 return NULL_TREE;
6596 /* else cxx_constant_value complained but gave us
6597 a real constant, so go ahead. */
6598 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
6600 else
6601 return NULL_TREE;
6604 /* Avoid typedef problems. */
6605 if (TREE_TYPE (expr) != type)
6606 expr = fold_convert (type, expr);
6608 /* [temp.arg.nontype]/5, bullet 2
6610 For a non-type template-parameter of type pointer to object,
6611 qualification conversions (_conv.qual_) and the array-to-pointer
6612 conversion (_conv.array_) are applied. */
6613 else if (TYPE_PTROBV_P (type))
6615 tree decayed = expr;
6617 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6618 decay_conversion or an explicit cast. If it's a problematic cast,
6619 we'll complain about it below. */
6620 if (TREE_CODE (expr) == NOP_EXPR)
6622 tree probe = expr;
6623 STRIP_NOPS (probe);
6624 if (TREE_CODE (probe) == ADDR_EXPR
6625 && TYPE_PTR_P (TREE_TYPE (probe)))
6627 expr = probe;
6628 expr_type = TREE_TYPE (expr);
6632 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6634 A template-argument for a non-type, non-template template-parameter
6635 shall be one of: [...]
6637 -- the name of a non-type template-parameter;
6638 -- the address of an object or function with external linkage, [...]
6639 expressed as "& id-expression" where the & is optional if the name
6640 refers to a function or array, or if the corresponding
6641 template-parameter is a reference.
6643 Here, we do not care about functions, as they are invalid anyway
6644 for a parameter of type pointer-to-object. */
6646 if (value_dependent_expression_p (expr))
6647 /* Non-type template parameters are OK. */
6649 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6650 /* Null pointer values are OK in C++11. */;
6651 else if (TREE_CODE (expr) != ADDR_EXPR)
6653 if (VAR_P (expr))
6655 if (complain & tf_error)
6656 error ("%qD is not a valid template argument "
6657 "because %qD is a variable, not the address of "
6658 "a variable", orig_expr, expr);
6659 return NULL_TREE;
6661 if (POINTER_TYPE_P (expr_type))
6663 if (complain & tf_error)
6664 error ("%qE is not a valid template argument for %qT "
6665 "because it is not the address of a variable",
6666 orig_expr, type);
6667 return NULL_TREE;
6669 /* Other values, like integer constants, might be valid
6670 non-type arguments of some other type. */
6671 return error_mark_node;
6673 else
6675 tree decl = TREE_OPERAND (expr, 0);
6677 if (!VAR_P (decl))
6679 if (complain & tf_error)
6680 error ("%qE is not a valid template argument of type %qT "
6681 "because %qE is not a variable", orig_expr, type, decl);
6682 return NULL_TREE;
6684 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6686 if (complain & tf_error)
6687 error ("%qE is not a valid template argument of type %qT "
6688 "because %qD does not have external linkage",
6689 orig_expr, type, decl);
6690 return NULL_TREE;
6692 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx1z)
6693 && decl_linkage (decl) == lk_none)
6695 if (complain & tf_error)
6696 error ("%qE is not a valid template argument of type %qT "
6697 "because %qD has no linkage", orig_expr, type, decl);
6698 return NULL_TREE;
6700 /* C++17: For a non-type template-parameter of reference or pointer
6701 type, the value of the constant expression shall not refer to (or
6702 for a pointer type, shall not be the address of):
6703 * a subobject (4.5),
6704 * a temporary object (15.2),
6705 * a string literal (5.13.5),
6706 * the result of a typeid expression (8.2.8), or
6707 * a predefined __func__ variable (11.4.1). */
6708 else if (DECL_ARTIFICIAL (decl))
6710 if (complain & tf_error)
6711 error ("the address of %qD is not a valid template argument",
6712 decl);
6713 return NULL_TREE;
6715 else if (!same_type_ignoring_top_level_qualifiers_p
6716 (strip_array_types (TREE_TYPE (type)),
6717 strip_array_types (TREE_TYPE (decl))))
6719 if (complain & tf_error)
6720 error ("the address of the %qT subobject of %qD is not a "
6721 "valid template argument", TREE_TYPE (type), decl);
6722 return NULL_TREE;
6724 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
6726 if (complain & tf_error)
6727 error ("the address of %qD is not a valid template argument "
6728 "because it does not have static storage duration",
6729 decl);
6730 return NULL_TREE;
6734 expr = decayed;
6736 expr = perform_qualification_conversions (type, expr);
6737 if (expr == error_mark_node)
6738 return error_mark_node;
6740 /* [temp.arg.nontype]/5, bullet 3
6742 For a non-type template-parameter of type reference to object, no
6743 conversions apply. The type referred to by the reference may be more
6744 cv-qualified than the (otherwise identical) type of the
6745 template-argument. The template-parameter is bound directly to the
6746 template-argument, which must be an lvalue. */
6747 else if (TYPE_REF_OBJ_P (type))
6749 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6750 expr_type))
6751 return error_mark_node;
6753 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6755 if (complain & tf_error)
6756 error ("%qE is not a valid template argument for type %qT "
6757 "because of conflicts in cv-qualification", expr, type);
6758 return NULL_TREE;
6761 if (!lvalue_p (expr))
6763 if (complain & tf_error)
6764 error ("%qE is not a valid template argument for type %qT "
6765 "because it is not an lvalue", expr, type);
6766 return NULL_TREE;
6769 /* [temp.arg.nontype]/1
6771 A template-argument for a non-type, non-template template-parameter
6772 shall be one of: [...]
6774 -- the address of an object or function with external linkage. */
6775 if (INDIRECT_REF_P (expr)
6776 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6778 expr = TREE_OPERAND (expr, 0);
6779 if (DECL_P (expr))
6781 if (complain & tf_error)
6782 error ("%q#D is not a valid template argument for type %qT "
6783 "because a reference variable does not have a constant "
6784 "address", expr, type);
6785 return NULL_TREE;
6789 if (TYPE_REF_OBJ_P (TREE_TYPE (expr))
6790 && value_dependent_expression_p (expr))
6791 /* OK, dependent reference. We don't want to ask whether a DECL is
6792 itself value-dependent, since what we want here is its address. */;
6793 else
6795 if (!DECL_P (expr))
6797 if (complain & tf_error)
6798 error ("%qE is not a valid template argument for type %qT "
6799 "because it is not an object with linkage",
6800 expr, type);
6801 return NULL_TREE;
6804 /* DR 1155 allows internal linkage in C++11 and up. */
6805 linkage_kind linkage = decl_linkage (expr);
6806 if (linkage < (cxx_dialect >= cxx11 ? lk_internal : lk_external))
6808 if (complain & tf_error)
6809 error ("%qE is not a valid template argument for type %qT "
6810 "because object %qD does not have linkage",
6811 expr, type, expr);
6812 return NULL_TREE;
6815 expr = build_address (expr);
6818 if (!same_type_p (type, TREE_TYPE (expr)))
6819 expr = build_nop (type, expr);
6821 /* [temp.arg.nontype]/5, bullet 4
6823 For a non-type template-parameter of type pointer to function, only
6824 the function-to-pointer conversion (_conv.func_) is applied. If the
6825 template-argument represents a set of overloaded functions (or a
6826 pointer to such), the matching function is selected from the set
6827 (_over.over_). */
6828 else if (TYPE_PTRFN_P (type))
6830 /* If the argument is a template-id, we might not have enough
6831 context information to decay the pointer. */
6832 if (!type_unknown_p (expr_type))
6834 expr = decay_conversion (expr, complain);
6835 if (expr == error_mark_node)
6836 return error_mark_node;
6839 if (cxx_dialect >= cxx11 && integer_zerop (expr))
6840 /* Null pointer values are OK in C++11. */
6841 return perform_qualification_conversions (type, expr);
6843 expr = convert_nontype_argument_function (type, expr, complain);
6844 if (!expr || expr == error_mark_node)
6845 return expr;
6847 /* [temp.arg.nontype]/5, bullet 5
6849 For a non-type template-parameter of type reference to function, no
6850 conversions apply. If the template-argument represents a set of
6851 overloaded functions, the matching function is selected from the set
6852 (_over.over_). */
6853 else if (TYPE_REFFN_P (type))
6855 if (TREE_CODE (expr) == ADDR_EXPR)
6857 if (complain & tf_error)
6859 error ("%qE is not a valid template argument for type %qT "
6860 "because it is a pointer", expr, type);
6861 inform (input_location, "try using %qE instead",
6862 TREE_OPERAND (expr, 0));
6864 return NULL_TREE;
6867 expr = convert_nontype_argument_function (type, expr, complain);
6868 if (!expr || expr == error_mark_node)
6869 return expr;
6871 /* [temp.arg.nontype]/5, bullet 6
6873 For a non-type template-parameter of type pointer to member function,
6874 no conversions apply. If the template-argument represents a set of
6875 overloaded member functions, the matching member function is selected
6876 from the set (_over.over_). */
6877 else if (TYPE_PTRMEMFUNC_P (type))
6879 expr = instantiate_type (type, expr, tf_none);
6880 if (expr == error_mark_node)
6881 return error_mark_node;
6883 /* [temp.arg.nontype] bullet 1 says the pointer to member
6884 expression must be a pointer-to-member constant. */
6885 if (!value_dependent_expression_p (expr)
6886 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6887 return NULL_TREE;
6889 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
6890 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
6891 if (fnptr_conv_p (type, TREE_TYPE (expr)))
6892 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
6894 /* [temp.arg.nontype]/5, bullet 7
6896 For a non-type template-parameter of type pointer to data member,
6897 qualification conversions (_conv.qual_) are applied. */
6898 else if (TYPE_PTRDATAMEM_P (type))
6900 /* [temp.arg.nontype] bullet 1 says the pointer to member
6901 expression must be a pointer-to-member constant. */
6902 if (!value_dependent_expression_p (expr)
6903 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6904 return NULL_TREE;
6906 expr = perform_qualification_conversions (type, expr);
6907 if (expr == error_mark_node)
6908 return expr;
6910 else if (NULLPTR_TYPE_P (type))
6912 if (expr != nullptr_node)
6914 if (complain & tf_error)
6915 error ("%qE is not a valid template argument for type %qT "
6916 "because it is of type %qT", expr, type, TREE_TYPE (expr));
6917 return NULL_TREE;
6919 return expr;
6921 /* A template non-type parameter must be one of the above. */
6922 else
6923 gcc_unreachable ();
6925 /* Sanity check: did we actually convert the argument to the
6926 right type? */
6927 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6928 (type, TREE_TYPE (expr)));
6929 return convert_from_reference (expr);
6932 /* Subroutine of coerce_template_template_parms, which returns 1 if
6933 PARM_PARM and ARG_PARM match using the rule for the template
6934 parameters of template template parameters. Both PARM and ARG are
6935 template parameters; the rest of the arguments are the same as for
6936 coerce_template_template_parms.
6938 static int
6939 coerce_template_template_parm (tree parm,
6940 tree arg,
6941 tsubst_flags_t complain,
6942 tree in_decl,
6943 tree outer_args)
6945 if (arg == NULL_TREE || error_operand_p (arg)
6946 || parm == NULL_TREE || error_operand_p (parm))
6947 return 0;
6949 if (TREE_CODE (arg) != TREE_CODE (parm))
6950 return 0;
6952 switch (TREE_CODE (parm))
6954 case TEMPLATE_DECL:
6955 /* We encounter instantiations of templates like
6956 template <template <template <class> class> class TT>
6957 class C; */
6959 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6960 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6962 if (!coerce_template_template_parms
6963 (parmparm, argparm, complain, in_decl, outer_args))
6964 return 0;
6966 /* Fall through. */
6968 case TYPE_DECL:
6969 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
6970 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6971 /* Argument is a parameter pack but parameter is not. */
6972 return 0;
6973 break;
6975 case PARM_DECL:
6976 /* The tsubst call is used to handle cases such as
6978 template <int> class C {};
6979 template <class T, template <T> class TT> class D {};
6980 D<int, C> d;
6982 i.e. the parameter list of TT depends on earlier parameters. */
6983 if (!uses_template_parms (TREE_TYPE (arg)))
6985 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
6986 if (!uses_template_parms (t)
6987 && !same_type_p (t, TREE_TYPE (arg)))
6988 return 0;
6991 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6992 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6993 /* Argument is a parameter pack but parameter is not. */
6994 return 0;
6996 break;
6998 default:
6999 gcc_unreachable ();
7002 return 1;
7005 /* Coerce template argument list ARGLIST for use with template
7006 template-parameter TEMPL. */
7008 static tree
7009 coerce_template_args_for_ttp (tree templ, tree arglist,
7010 tsubst_flags_t complain)
7012 /* Consider an example where a template template parameter declared as
7014 template <class T, class U = std::allocator<T> > class TT
7016 The template parameter level of T and U are one level larger than
7017 of TT. To proper process the default argument of U, say when an
7018 instantiation `TT<int>' is seen, we need to build the full
7019 arguments containing {int} as the innermost level. Outer levels,
7020 available when not appearing as default template argument, can be
7021 obtained from the arguments of the enclosing template.
7023 Suppose that TT is later substituted with std::vector. The above
7024 instantiation is `TT<int, std::allocator<T> >' with TT at
7025 level 1, and T at level 2, while the template arguments at level 1
7026 becomes {std::vector} and the inner level 2 is {int}. */
7028 tree outer = DECL_CONTEXT (templ);
7029 if (outer)
7031 if (DECL_TEMPLATE_SPECIALIZATION (outer))
7032 /* We want arguments for the partial specialization, not arguments for
7033 the primary template. */
7034 outer = template_parms_to_args (DECL_TEMPLATE_PARMS (outer));
7035 else
7036 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7038 else if (current_template_parms)
7040 /* This is an argument of the current template, so we haven't set
7041 DECL_CONTEXT yet. */
7042 tree relevant_template_parms;
7044 /* Parameter levels that are greater than the level of the given
7045 template template parm are irrelevant. */
7046 relevant_template_parms = current_template_parms;
7047 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7048 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7049 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7051 outer = template_parms_to_args (relevant_template_parms);
7054 if (outer)
7055 arglist = add_to_template_args (outer, arglist);
7057 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7058 return coerce_template_parms (parmlist, arglist, templ,
7059 complain,
7060 /*require_all_args=*/true,
7061 /*use_default_args=*/true);
7064 /* A cache of template template parameters with match-all default
7065 arguments. */
7066 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7067 static void
7068 store_defaulted_ttp (tree v, tree t)
7070 if (!defaulted_ttp_cache)
7071 defaulted_ttp_cache = hash_map<tree,tree>::create_ggc (13);
7072 defaulted_ttp_cache->put (v, t);
7074 static tree
7075 lookup_defaulted_ttp (tree v)
7077 if (defaulted_ttp_cache)
7078 if (tree *p = defaulted_ttp_cache->get (v))
7079 return *p;
7080 return NULL_TREE;
7083 /* T is a bound template template-parameter. Copy its arguments into default
7084 arguments of the template template-parameter's template parameters. */
7086 static tree
7087 add_defaults_to_ttp (tree otmpl)
7089 if (tree c = lookup_defaulted_ttp (otmpl))
7090 return c;
7092 tree ntmpl = copy_node (otmpl);
7094 tree ntype = copy_node (TREE_TYPE (otmpl));
7095 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7096 TYPE_MAIN_VARIANT (ntype) = ntype;
7097 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7098 TYPE_NAME (ntype) = ntmpl;
7099 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7101 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7102 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7103 TEMPLATE_PARM_DECL (idx) = ntmpl;
7104 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7106 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7107 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7108 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7109 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7110 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7112 tree o = TREE_VEC_ELT (vec, i);
7113 if (!template_parameter_pack_p (TREE_VALUE (o)))
7115 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7116 TREE_PURPOSE (n) = any_targ_node;
7120 store_defaulted_ttp (otmpl, ntmpl);
7121 return ntmpl;
7124 /* ARG is a bound potential template template-argument, and PARGS is a list
7125 of arguments for the corresponding template template-parameter. Adjust
7126 PARGS as appropriate for application to ARG's template, and if ARG is a
7127 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7128 arguments to the template template parameter. */
7130 static tree
7131 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7133 ++processing_template_decl;
7134 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7135 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7137 /* When comparing two template template-parameters in partial ordering,
7138 rewrite the one currently being used as an argument to have default
7139 arguments for all parameters. */
7140 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7141 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7142 if (pargs != error_mark_node)
7143 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7144 TYPE_TI_ARGS (arg));
7146 else
7148 tree aparms
7149 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7150 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7151 /*require_all*/true,
7152 /*use_default*/true);
7154 --processing_template_decl;
7155 return pargs;
7158 /* Subroutine of unify for the case when PARM is a
7159 BOUND_TEMPLATE_TEMPLATE_PARM. */
7161 static int
7162 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7163 bool explain_p)
7165 tree parmvec = TYPE_TI_ARGS (parm);
7166 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7168 /* The template template parm might be variadic and the argument
7169 not, so flatten both argument lists. */
7170 parmvec = expand_template_argument_pack (parmvec);
7171 argvec = expand_template_argument_pack (argvec);
7173 if (flag_new_ttp)
7175 /* In keeping with P0522R0, adjust P's template arguments
7176 to apply to A's template; then flatten it again. */
7177 tree nparmvec = parmvec;
7178 nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7179 nparmvec = expand_template_argument_pack (nparmvec);
7181 if (unify (tparms, targs, nparmvec, argvec,
7182 UNIFY_ALLOW_NONE, explain_p))
7183 return 1;
7185 /* If the P0522 adjustment eliminated a pack expansion, deduce
7186 empty packs. */
7187 if (flag_new_ttp
7188 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7189 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7190 DEDUCE_EXACT, /*sub*/true, explain_p))
7191 return 1;
7193 else
7195 /* Deduce arguments T, i from TT<T> or TT<i>.
7196 We check each element of PARMVEC and ARGVEC individually
7197 rather than the whole TREE_VEC since they can have
7198 different number of elements, which is allowed under N2555. */
7200 int len = TREE_VEC_LENGTH (parmvec);
7202 /* Check if the parameters end in a pack, making them
7203 variadic. */
7204 int parm_variadic_p = 0;
7205 if (len > 0
7206 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7207 parm_variadic_p = 1;
7209 for (int i = 0; i < len - parm_variadic_p; ++i)
7210 /* If the template argument list of P contains a pack
7211 expansion that is not the last template argument, the
7212 entire template argument list is a non-deduced
7213 context. */
7214 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7215 return unify_success (explain_p);
7217 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7218 return unify_too_few_arguments (explain_p,
7219 TREE_VEC_LENGTH (argvec), len);
7221 for (int i = 0; i < len - parm_variadic_p; ++i)
7222 if (unify (tparms, targs,
7223 TREE_VEC_ELT (parmvec, i),
7224 TREE_VEC_ELT (argvec, i),
7225 UNIFY_ALLOW_NONE, explain_p))
7226 return 1;
7228 if (parm_variadic_p
7229 && unify_pack_expansion (tparms, targs,
7230 parmvec, argvec,
7231 DEDUCE_EXACT,
7232 /*subr=*/true, explain_p))
7233 return 1;
7236 return 0;
7239 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7240 template template parameters. Both PARM_PARMS and ARG_PARMS are
7241 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7242 or PARM_DECL.
7244 Consider the example:
7245 template <class T> class A;
7246 template<template <class U> class TT> class B;
7248 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7249 the parameters to A, and OUTER_ARGS contains A. */
7251 static int
7252 coerce_template_template_parms (tree parm_parms,
7253 tree arg_parms,
7254 tsubst_flags_t complain,
7255 tree in_decl,
7256 tree outer_args)
7258 int nparms, nargs, i;
7259 tree parm, arg;
7260 int variadic_p = 0;
7262 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7263 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7265 nparms = TREE_VEC_LENGTH (parm_parms);
7266 nargs = TREE_VEC_LENGTH (arg_parms);
7268 if (flag_new_ttp)
7270 /* P0522R0: A template template-parameter P is at least as specialized as
7271 a template template-argument A if, given the following rewrite to two
7272 function templates, the function template corresponding to P is at
7273 least as specialized as the function template corresponding to A
7274 according to the partial ordering rules for function templates
7275 ([temp.func.order]). Given an invented class template X with the
7276 template parameter list of A (including default arguments):
7278 * Each of the two function templates has the same template parameters,
7279 respectively, as P or A.
7281 * Each function template has a single function parameter whose type is
7282 a specialization of X with template arguments corresponding to the
7283 template parameters from the respective function template where, for
7284 each template parameter PP in the template parameter list of the
7285 function template, a corresponding template argument AA is formed. If
7286 PP declares a parameter pack, then AA is the pack expansion
7287 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7289 If the rewrite produces an invalid type, then P is not at least as
7290 specialized as A. */
7292 /* So coerce P's args to apply to A's parms, and then deduce between A's
7293 args and the converted args. If that succeeds, A is at least as
7294 specialized as P, so they match.*/
7295 tree pargs = template_parms_level_to_args (parm_parms);
7296 ++processing_template_decl;
7297 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7298 /*require_all*/true, /*use_default*/true);
7299 --processing_template_decl;
7300 if (pargs != error_mark_node)
7302 tree targs = make_tree_vec (nargs);
7303 tree aargs = template_parms_level_to_args (arg_parms);
7304 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7305 /*explain*/false))
7306 return 1;
7310 /* Determine whether we have a parameter pack at the end of the
7311 template template parameter's template parameter list. */
7312 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7314 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7316 if (error_operand_p (parm))
7317 return 0;
7319 switch (TREE_CODE (parm))
7321 case TEMPLATE_DECL:
7322 case TYPE_DECL:
7323 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7324 variadic_p = 1;
7325 break;
7327 case PARM_DECL:
7328 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7329 variadic_p = 1;
7330 break;
7332 default:
7333 gcc_unreachable ();
7337 if (nargs != nparms
7338 && !(variadic_p && nargs >= nparms - 1))
7339 return 0;
7341 /* Check all of the template parameters except the parameter pack at
7342 the end (if any). */
7343 for (i = 0; i < nparms - variadic_p; ++i)
7345 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7346 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7347 continue;
7349 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7350 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7352 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7353 outer_args))
7354 return 0;
7358 if (variadic_p)
7360 /* Check each of the template parameters in the template
7361 argument against the template parameter pack at the end of
7362 the template template parameter. */
7363 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7364 return 0;
7366 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7368 for (; i < nargs; ++i)
7370 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7371 continue;
7373 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7375 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7376 outer_args))
7377 return 0;
7381 return 1;
7384 /* Verifies that the deduced template arguments (in TARGS) for the
7385 template template parameters (in TPARMS) represent valid bindings,
7386 by comparing the template parameter list of each template argument
7387 to the template parameter list of its corresponding template
7388 template parameter, in accordance with DR150. This
7389 routine can only be called after all template arguments have been
7390 deduced. It will return TRUE if all of the template template
7391 parameter bindings are okay, FALSE otherwise. */
7392 bool
7393 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7395 int i, ntparms = TREE_VEC_LENGTH (tparms);
7396 bool ret = true;
7398 /* We're dealing with template parms in this process. */
7399 ++processing_template_decl;
7401 targs = INNERMOST_TEMPLATE_ARGS (targs);
7403 for (i = 0; i < ntparms; ++i)
7405 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7406 tree targ = TREE_VEC_ELT (targs, i);
7408 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7410 tree packed_args = NULL_TREE;
7411 int idx, len = 1;
7413 if (ARGUMENT_PACK_P (targ))
7415 /* Look inside the argument pack. */
7416 packed_args = ARGUMENT_PACK_ARGS (targ);
7417 len = TREE_VEC_LENGTH (packed_args);
7420 for (idx = 0; idx < len; ++idx)
7422 tree targ_parms = NULL_TREE;
7424 if (packed_args)
7425 /* Extract the next argument from the argument
7426 pack. */
7427 targ = TREE_VEC_ELT (packed_args, idx);
7429 if (PACK_EXPANSION_P (targ))
7430 /* Look at the pattern of the pack expansion. */
7431 targ = PACK_EXPANSION_PATTERN (targ);
7433 /* Extract the template parameters from the template
7434 argument. */
7435 if (TREE_CODE (targ) == TEMPLATE_DECL)
7436 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7437 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7438 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7440 /* Verify that we can coerce the template template
7441 parameters from the template argument to the template
7442 parameter. This requires an exact match. */
7443 if (targ_parms
7444 && !coerce_template_template_parms
7445 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7446 targ_parms,
7447 tf_none,
7448 tparm,
7449 targs))
7451 ret = false;
7452 goto out;
7458 out:
7460 --processing_template_decl;
7461 return ret;
7464 /* Since type attributes aren't mangled, we need to strip them from
7465 template type arguments. */
7467 static tree
7468 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7470 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7471 return arg;
7472 bool removed_attributes = false;
7473 tree canon = strip_typedefs (arg, &removed_attributes);
7474 if (removed_attributes
7475 && (complain & tf_warning))
7476 warning (OPT_Wignored_attributes,
7477 "ignoring attributes on template argument %qT", arg);
7478 return canon;
7481 /* And from inside dependent non-type arguments like sizeof(Type). */
7483 static tree
7484 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7486 if (!arg || arg == error_mark_node)
7487 return arg;
7488 bool removed_attributes = false;
7489 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7490 if (removed_attributes
7491 && (complain & tf_warning))
7492 warning (OPT_Wignored_attributes,
7493 "ignoring attributes in template argument %qE", arg);
7494 return canon;
7497 // A template declaration can be substituted for a constrained
7498 // template template parameter only when the argument is more
7499 // constrained than the parameter.
7500 static bool
7501 is_compatible_template_arg (tree parm, tree arg)
7503 tree parm_cons = get_constraints (parm);
7505 /* For now, allow constrained template template arguments
7506 and unconstrained template template parameters. */
7507 if (parm_cons == NULL_TREE)
7508 return true;
7510 tree arg_cons = get_constraints (arg);
7512 // If the template parameter is constrained, we need to rewrite its
7513 // constraints in terms of the ARG's template parameters. This ensures
7514 // that all of the template parameter types will have the same depth.
7516 // Note that this is only valid when coerce_template_template_parm is
7517 // true for the innermost template parameters of PARM and ARG. In other
7518 // words, because coercion is successful, this conversion will be valid.
7519 if (parm_cons)
7521 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7522 parm_cons = tsubst_constraint_info (parm_cons,
7523 INNERMOST_TEMPLATE_ARGS (args),
7524 tf_none, NULL_TREE);
7525 if (parm_cons == error_mark_node)
7526 return false;
7529 return subsumes (parm_cons, arg_cons);
7532 // Convert a placeholder argument into a binding to the original
7533 // parameter. The original parameter is saved as the TREE_TYPE of
7534 // ARG.
7535 static inline tree
7536 convert_wildcard_argument (tree parm, tree arg)
7538 TREE_TYPE (arg) = parm;
7539 return arg;
7542 /* Convert the indicated template ARG as necessary to match the
7543 indicated template PARM. Returns the converted ARG, or
7544 error_mark_node if the conversion was unsuccessful. Error and
7545 warning messages are issued under control of COMPLAIN. This
7546 conversion is for the Ith parameter in the parameter list. ARGS is
7547 the full set of template arguments deduced so far. */
7549 static tree
7550 convert_template_argument (tree parm,
7551 tree arg,
7552 tree args,
7553 tsubst_flags_t complain,
7554 int i,
7555 tree in_decl)
7557 tree orig_arg;
7558 tree val;
7559 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7561 if (parm == error_mark_node)
7562 return error_mark_node;
7564 /* Trivially convert placeholders. */
7565 if (TREE_CODE (arg) == WILDCARD_DECL)
7566 return convert_wildcard_argument (parm, arg);
7568 if (arg == any_targ_node)
7569 return arg;
7571 if (TREE_CODE (arg) == TREE_LIST
7572 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7574 /* The template argument was the name of some
7575 member function. That's usually
7576 invalid, but static members are OK. In any
7577 case, grab the underlying fields/functions
7578 and issue an error later if required. */
7579 orig_arg = TREE_VALUE (arg);
7580 TREE_TYPE (arg) = unknown_type_node;
7583 orig_arg = arg;
7585 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7586 requires_type = (TREE_CODE (parm) == TYPE_DECL
7587 || requires_tmpl_type);
7589 /* When determining whether an argument pack expansion is a template,
7590 look at the pattern. */
7591 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7592 arg = PACK_EXPANSION_PATTERN (arg);
7594 /* Deal with an injected-class-name used as a template template arg. */
7595 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7597 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7598 if (TREE_CODE (t) == TEMPLATE_DECL)
7600 if (cxx_dialect >= cxx11)
7601 /* OK under DR 1004. */;
7602 else if (complain & tf_warning_or_error)
7603 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7604 " used as template template argument", TYPE_NAME (arg));
7605 else if (flag_pedantic_errors)
7606 t = arg;
7608 arg = t;
7612 is_tmpl_type =
7613 ((TREE_CODE (arg) == TEMPLATE_DECL
7614 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7615 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7616 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7617 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7619 if (is_tmpl_type
7620 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7621 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7622 arg = TYPE_STUB_DECL (arg);
7624 is_type = TYPE_P (arg) || is_tmpl_type;
7626 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7627 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7629 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7631 if (complain & tf_error)
7632 error ("invalid use of destructor %qE as a type", orig_arg);
7633 return error_mark_node;
7636 permerror (input_location,
7637 "to refer to a type member of a template parameter, "
7638 "use %<typename %E%>", orig_arg);
7640 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7641 TREE_OPERAND (arg, 1),
7642 typename_type,
7643 complain);
7644 arg = orig_arg;
7645 is_type = 1;
7647 if (is_type != requires_type)
7649 if (in_decl)
7651 if (complain & tf_error)
7653 error ("type/value mismatch at argument %d in template "
7654 "parameter list for %qD",
7655 i + 1, in_decl);
7656 if (is_type)
7657 inform (input_location,
7658 " expected a constant of type %qT, got %qT",
7659 TREE_TYPE (parm),
7660 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7661 else if (requires_tmpl_type)
7662 inform (input_location,
7663 " expected a class template, got %qE", orig_arg);
7664 else
7665 inform (input_location,
7666 " expected a type, got %qE", orig_arg);
7669 return error_mark_node;
7671 if (is_tmpl_type ^ requires_tmpl_type)
7673 if (in_decl && (complain & tf_error))
7675 error ("type/value mismatch at argument %d in template "
7676 "parameter list for %qD",
7677 i + 1, in_decl);
7678 if (is_tmpl_type)
7679 inform (input_location,
7680 " expected a type, got %qT", DECL_NAME (arg));
7681 else
7682 inform (input_location,
7683 " expected a class template, got %qT", orig_arg);
7685 return error_mark_node;
7688 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7689 /* We already did the appropriate conversion when packing args. */
7690 val = orig_arg;
7691 else if (is_type)
7693 if (requires_tmpl_type)
7695 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7696 /* The number of argument required is not known yet.
7697 Just accept it for now. */
7698 val = TREE_TYPE (arg);
7699 else
7701 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7702 tree argparm;
7704 /* Strip alias templates that are equivalent to another
7705 template. */
7706 arg = get_underlying_template (arg);
7707 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7709 if (coerce_template_template_parms (parmparm, argparm,
7710 complain, in_decl,
7711 args))
7713 val = arg;
7715 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7716 TEMPLATE_DECL. */
7717 if (val != error_mark_node)
7719 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
7720 val = TREE_TYPE (val);
7721 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
7722 val = make_pack_expansion (val);
7725 else
7727 if (in_decl && (complain & tf_error))
7729 error ("type/value mismatch at argument %d in "
7730 "template parameter list for %qD",
7731 i + 1, in_decl);
7732 inform (input_location,
7733 " expected a template of type %qD, got %qT",
7734 parm, orig_arg);
7737 val = error_mark_node;
7740 // Check that the constraints are compatible before allowing the
7741 // substitution.
7742 if (val != error_mark_node)
7743 if (!is_compatible_template_arg (parm, arg))
7745 if (in_decl && (complain & tf_error))
7747 error ("constraint mismatch at argument %d in "
7748 "template parameter list for %qD",
7749 i + 1, in_decl);
7750 inform (input_location, " expected %qD but got %qD",
7751 parm, arg);
7753 val = error_mark_node;
7757 else
7758 val = orig_arg;
7759 /* We only form one instance of each template specialization.
7760 Therefore, if we use a non-canonical variant (i.e., a
7761 typedef), any future messages referring to the type will use
7762 the typedef, which is confusing if those future uses do not
7763 themselves also use the typedef. */
7764 if (TYPE_P (val))
7765 val = canonicalize_type_argument (val, complain);
7767 else
7769 tree t = TREE_TYPE (parm);
7771 if (tree a = type_uses_auto (t))
7773 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
7774 if (t == error_mark_node)
7775 return error_mark_node;
7777 else
7778 t = tsubst (t, args, complain, in_decl);
7780 if (invalid_nontype_parm_type_p (t, complain))
7781 return error_mark_node;
7783 if (!type_dependent_expression_p (orig_arg)
7784 && !uses_template_parms (t))
7785 /* We used to call digest_init here. However, digest_init
7786 will report errors, which we don't want when complain
7787 is zero. More importantly, digest_init will try too
7788 hard to convert things: for example, `0' should not be
7789 converted to pointer type at this point according to
7790 the standard. Accepting this is not merely an
7791 extension, since deciding whether or not these
7792 conversions can occur is part of determining which
7793 function template to call, or whether a given explicit
7794 argument specification is valid. */
7795 val = convert_nontype_argument (t, orig_arg, complain);
7796 else
7797 val = canonicalize_expr_argument (orig_arg, complain);
7799 if (val == NULL_TREE)
7800 val = error_mark_node;
7801 else if (val == error_mark_node && (complain & tf_error))
7802 error ("could not convert template argument %qE from %qT to %qT",
7803 orig_arg, TREE_TYPE (orig_arg), t);
7805 if (INDIRECT_REF_P (val))
7807 /* Reject template arguments that are references to built-in
7808 functions with no library fallbacks. */
7809 const_tree inner = TREE_OPERAND (val, 0);
7810 const_tree innertype = TREE_TYPE (inner);
7811 if (innertype
7812 && TREE_CODE (innertype) == REFERENCE_TYPE
7813 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
7814 && 0 < TREE_OPERAND_LENGTH (inner)
7815 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
7816 return error_mark_node;
7819 if (TREE_CODE (val) == SCOPE_REF)
7821 /* Strip typedefs from the SCOPE_REF. */
7822 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
7823 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
7824 complain);
7825 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
7826 QUALIFIED_NAME_IS_TEMPLATE (val));
7830 return val;
7833 /* Coerces the remaining template arguments in INNER_ARGS (from
7834 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
7835 Returns the coerced argument pack. PARM_IDX is the position of this
7836 parameter in the template parameter list. ARGS is the original
7837 template argument list. */
7838 static tree
7839 coerce_template_parameter_pack (tree parms,
7840 int parm_idx,
7841 tree args,
7842 tree inner_args,
7843 int arg_idx,
7844 tree new_args,
7845 int* lost,
7846 tree in_decl,
7847 tsubst_flags_t complain)
7849 tree parm = TREE_VEC_ELT (parms, parm_idx);
7850 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7851 tree packed_args;
7852 tree argument_pack;
7853 tree packed_parms = NULL_TREE;
7855 if (arg_idx > nargs)
7856 arg_idx = nargs;
7858 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
7860 /* When the template parameter is a non-type template parameter pack
7861 or template template parameter pack whose type or template
7862 parameters use parameter packs, we know exactly how many arguments
7863 we are looking for. Build a vector of the instantiated decls for
7864 these template parameters in PACKED_PARMS. */
7865 /* We can't use make_pack_expansion here because it would interpret a
7866 _DECL as a use rather than a declaration. */
7867 tree decl = TREE_VALUE (parm);
7868 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
7869 SET_PACK_EXPANSION_PATTERN (exp, decl);
7870 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
7871 SET_TYPE_STRUCTURAL_EQUALITY (exp);
7873 TREE_VEC_LENGTH (args)--;
7874 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
7875 TREE_VEC_LENGTH (args)++;
7877 if (packed_parms == error_mark_node)
7878 return error_mark_node;
7880 /* If we're doing a partial instantiation of a member template,
7881 verify that all of the types used for the non-type
7882 template parameter pack are, in fact, valid for non-type
7883 template parameters. */
7884 if (arg_idx < nargs
7885 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
7887 int j, len = TREE_VEC_LENGTH (packed_parms);
7888 for (j = 0; j < len; ++j)
7890 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
7891 if (invalid_nontype_parm_type_p (t, complain))
7892 return error_mark_node;
7894 /* We don't know how many args we have yet, just
7895 use the unconverted ones for now. */
7896 return NULL_TREE;
7899 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
7901 /* Check if we have a placeholder pack, which indicates we're
7902 in the context of a introduction list. In that case we want
7903 to match this pack to the single placeholder. */
7904 else if (arg_idx < nargs
7905 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
7906 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
7908 nargs = arg_idx + 1;
7909 packed_args = make_tree_vec (1);
7911 else
7912 packed_args = make_tree_vec (nargs - arg_idx);
7914 /* Convert the remaining arguments, which will be a part of the
7915 parameter pack "parm". */
7916 int first_pack_arg = arg_idx;
7917 for (; arg_idx < nargs; ++arg_idx)
7919 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
7920 tree actual_parm = TREE_VALUE (parm);
7921 int pack_idx = arg_idx - first_pack_arg;
7923 if (packed_parms)
7925 /* Once we've packed as many args as we have types, stop. */
7926 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
7927 break;
7928 else if (PACK_EXPANSION_P (arg))
7929 /* We don't know how many args we have yet, just
7930 use the unconverted ones for now. */
7931 return NULL_TREE;
7932 else
7933 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
7936 if (arg == error_mark_node)
7938 if (complain & tf_error)
7939 error ("template argument %d is invalid", arg_idx + 1);
7941 else
7942 arg = convert_template_argument (actual_parm,
7943 arg, new_args, complain, parm_idx,
7944 in_decl);
7945 if (arg == error_mark_node)
7946 (*lost)++;
7947 TREE_VEC_ELT (packed_args, pack_idx) = arg;
7950 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
7951 && TREE_VEC_LENGTH (packed_args) > 0)
7953 if (complain & tf_error)
7954 error ("wrong number of template arguments (%d, should be %d)",
7955 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
7956 return error_mark_node;
7959 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
7960 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
7961 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
7962 else
7964 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
7965 TREE_CONSTANT (argument_pack) = 1;
7968 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
7969 if (CHECKING_P)
7970 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
7971 TREE_VEC_LENGTH (packed_args));
7972 return argument_pack;
7975 /* Returns the number of pack expansions in the template argument vector
7976 ARGS. */
7978 static int
7979 pack_expansion_args_count (tree args)
7981 int i;
7982 int count = 0;
7983 if (args)
7984 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
7986 tree elt = TREE_VEC_ELT (args, i);
7987 if (elt && PACK_EXPANSION_P (elt))
7988 ++count;
7990 return count;
7993 /* Convert all template arguments to their appropriate types, and
7994 return a vector containing the innermost resulting template
7995 arguments. If any error occurs, return error_mark_node. Error and
7996 warning messages are issued under control of COMPLAIN.
7998 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
7999 for arguments not specified in ARGS. Otherwise, if
8000 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8001 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8002 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8003 ARGS. */
8005 static tree
8006 coerce_template_parms (tree parms,
8007 tree args,
8008 tree in_decl,
8009 tsubst_flags_t complain,
8010 bool require_all_args,
8011 bool use_default_args)
8013 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8014 tree orig_inner_args;
8015 tree inner_args;
8016 tree new_args;
8017 tree new_inner_args;
8018 int saved_unevaluated_operand;
8019 int saved_inhibit_evaluation_warnings;
8021 /* When used as a boolean value, indicates whether this is a
8022 variadic template parameter list. Since it's an int, we can also
8023 subtract it from nparms to get the number of non-variadic
8024 parameters. */
8025 int variadic_p = 0;
8026 int variadic_args_p = 0;
8027 int post_variadic_parms = 0;
8029 /* Likewise for parameters with default arguments. */
8030 int default_p = 0;
8032 if (args == error_mark_node)
8033 return error_mark_node;
8035 nparms = TREE_VEC_LENGTH (parms);
8037 /* Determine if there are any parameter packs or default arguments. */
8038 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8040 tree parm = TREE_VEC_ELT (parms, parm_idx);
8041 if (variadic_p)
8042 ++post_variadic_parms;
8043 if (template_parameter_pack_p (TREE_VALUE (parm)))
8044 ++variadic_p;
8045 if (TREE_PURPOSE (parm))
8046 ++default_p;
8049 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8050 /* If there are no parameters that follow a parameter pack, we need to
8051 expand any argument packs so that we can deduce a parameter pack from
8052 some non-packed args followed by an argument pack, as in variadic85.C.
8053 If there are such parameters, we need to leave argument packs intact
8054 so the arguments are assigned properly. This can happen when dealing
8055 with a nested class inside a partial specialization of a class
8056 template, as in variadic92.C, or when deducing a template parameter pack
8057 from a sub-declarator, as in variadic114.C. */
8058 if (!post_variadic_parms)
8059 inner_args = expand_template_argument_pack (inner_args);
8061 /* Count any pack expansion args. */
8062 variadic_args_p = pack_expansion_args_count (inner_args);
8064 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8065 if ((nargs - variadic_args_p > nparms && !variadic_p)
8066 || (nargs < nparms - variadic_p
8067 && require_all_args
8068 && !variadic_args_p
8069 && (!use_default_args
8070 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8071 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8073 if (complain & tf_error)
8075 if (variadic_p || default_p)
8077 nparms -= variadic_p + default_p;
8078 error ("wrong number of template arguments "
8079 "(%d, should be at least %d)", nargs, nparms);
8081 else
8082 error ("wrong number of template arguments "
8083 "(%d, should be %d)", nargs, nparms);
8085 if (in_decl)
8086 inform (DECL_SOURCE_LOCATION (in_decl),
8087 "provided for %qD", in_decl);
8090 return error_mark_node;
8092 /* We can't pass a pack expansion to a non-pack parameter of an alias
8093 template (DR 1430). */
8094 else if (in_decl
8095 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8096 || concept_template_p (in_decl))
8097 && variadic_args_p
8098 && nargs - variadic_args_p < nparms - variadic_p)
8100 if (complain & tf_error)
8102 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8104 tree arg = TREE_VEC_ELT (inner_args, i);
8105 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8107 if (PACK_EXPANSION_P (arg)
8108 && !template_parameter_pack_p (parm))
8110 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8111 error_at (location_of (arg),
8112 "pack expansion argument for non-pack parameter "
8113 "%qD of alias template %qD", parm, in_decl);
8114 else
8115 error_at (location_of (arg),
8116 "pack expansion argument for non-pack parameter "
8117 "%qD of concept %qD", parm, in_decl);
8118 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8119 goto found;
8122 gcc_unreachable ();
8123 found:;
8125 return error_mark_node;
8128 /* We need to evaluate the template arguments, even though this
8129 template-id may be nested within a "sizeof". */
8130 saved_unevaluated_operand = cp_unevaluated_operand;
8131 cp_unevaluated_operand = 0;
8132 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8133 c_inhibit_evaluation_warnings = 0;
8134 new_inner_args = make_tree_vec (nparms);
8135 new_args = add_outermost_template_args (args, new_inner_args);
8136 int pack_adjust = 0;
8137 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8139 tree arg;
8140 tree parm;
8142 /* Get the Ith template parameter. */
8143 parm = TREE_VEC_ELT (parms, parm_idx);
8145 if (parm == error_mark_node)
8147 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8148 continue;
8151 /* Calculate the next argument. */
8152 if (arg_idx < nargs)
8153 arg = TREE_VEC_ELT (inner_args, arg_idx);
8154 else
8155 arg = NULL_TREE;
8157 if (template_parameter_pack_p (TREE_VALUE (parm))
8158 && !(arg && ARGUMENT_PACK_P (arg)))
8160 /* Some arguments will be placed in the
8161 template parameter pack PARM. */
8162 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8163 inner_args, arg_idx,
8164 new_args, &lost,
8165 in_decl, complain);
8167 if (arg == NULL_TREE)
8169 /* We don't know how many args we have yet, just use the
8170 unconverted (and still packed) ones for now. */
8171 new_inner_args = orig_inner_args;
8172 arg_idx = nargs;
8173 break;
8176 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8178 /* Store this argument. */
8179 if (arg == error_mark_node)
8181 lost++;
8182 /* We are done with all of the arguments. */
8183 arg_idx = nargs;
8185 else
8187 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8188 arg_idx += pack_adjust;
8191 continue;
8193 else if (arg)
8195 if (PACK_EXPANSION_P (arg))
8197 /* "If every valid specialization of a variadic template
8198 requires an empty template parameter pack, the template is
8199 ill-formed, no diagnostic required." So check that the
8200 pattern works with this parameter. */
8201 tree pattern = PACK_EXPANSION_PATTERN (arg);
8202 tree conv = convert_template_argument (TREE_VALUE (parm),
8203 pattern, new_args,
8204 complain, parm_idx,
8205 in_decl);
8206 if (conv == error_mark_node)
8208 if (complain & tf_error)
8209 inform (input_location, "so any instantiation with a "
8210 "non-empty parameter pack would be ill-formed");
8211 ++lost;
8213 else if (TYPE_P (conv) && !TYPE_P (pattern))
8214 /* Recover from missing typename. */
8215 TREE_VEC_ELT (inner_args, arg_idx)
8216 = make_pack_expansion (conv);
8218 /* We don't know how many args we have yet, just
8219 use the unconverted ones for now. */
8220 new_inner_args = inner_args;
8221 arg_idx = nargs;
8222 break;
8225 else if (require_all_args)
8227 /* There must be a default arg in this case. */
8228 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8229 complain, in_decl);
8230 /* The position of the first default template argument,
8231 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8232 Record that. */
8233 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8234 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8235 arg_idx - pack_adjust);
8237 else
8238 break;
8240 if (arg == error_mark_node)
8242 if (complain & tf_error)
8243 error ("template argument %d is invalid", arg_idx + 1);
8245 else if (!arg)
8246 /* This only occurs if there was an error in the template
8247 parameter list itself (which we would already have
8248 reported) that we are trying to recover from, e.g., a class
8249 template with a parameter list such as
8250 template<typename..., typename>. */
8251 ++lost;
8252 else
8253 arg = convert_template_argument (TREE_VALUE (parm),
8254 arg, new_args, complain,
8255 parm_idx, in_decl);
8257 if (arg == error_mark_node)
8258 lost++;
8259 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8261 cp_unevaluated_operand = saved_unevaluated_operand;
8262 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8264 if (variadic_p && arg_idx < nargs)
8266 if (complain & tf_error)
8268 error ("wrong number of template arguments "
8269 "(%d, should be %d)", nargs, arg_idx);
8270 if (in_decl)
8271 error ("provided for %q+D", in_decl);
8273 return error_mark_node;
8276 if (lost)
8277 return error_mark_node;
8279 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8280 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8281 TREE_VEC_LENGTH (new_inner_args));
8283 return new_inner_args;
8286 /* Convert all template arguments to their appropriate types, and
8287 return a vector containing the innermost resulting template
8288 arguments. If any error occurs, return error_mark_node. Error and
8289 warning messages are not issued.
8291 Note that no function argument deduction is performed, and default
8292 arguments are used to fill in unspecified arguments. */
8293 tree
8294 coerce_template_parms (tree parms, tree args, tree in_decl)
8296 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8299 /* Convert all template arguments to their appropriate type, and
8300 instantiate default arguments as needed. This returns a vector
8301 containing the innermost resulting template arguments, or
8302 error_mark_node if unsuccessful. */
8303 tree
8304 coerce_template_parms (tree parms, tree args, tree in_decl,
8305 tsubst_flags_t complain)
8307 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8310 /* Like coerce_template_parms. If PARMS represents all template
8311 parameters levels, this function returns a vector of vectors
8312 representing all the resulting argument levels. Note that in this
8313 case, only the innermost arguments are coerced because the
8314 outermost ones are supposed to have been coerced already.
8316 Otherwise, if PARMS represents only (the innermost) vector of
8317 parameters, this function returns a vector containing just the
8318 innermost resulting arguments. */
8320 static tree
8321 coerce_innermost_template_parms (tree parms,
8322 tree args,
8323 tree in_decl,
8324 tsubst_flags_t complain,
8325 bool require_all_args,
8326 bool use_default_args)
8328 int parms_depth = TMPL_PARMS_DEPTH (parms);
8329 int args_depth = TMPL_ARGS_DEPTH (args);
8330 tree coerced_args;
8332 if (parms_depth > 1)
8334 coerced_args = make_tree_vec (parms_depth);
8335 tree level;
8336 int cur_depth;
8338 for (level = parms, cur_depth = parms_depth;
8339 parms_depth > 0 && level != NULL_TREE;
8340 level = TREE_CHAIN (level), --cur_depth)
8342 tree l;
8343 if (cur_depth == args_depth)
8344 l = coerce_template_parms (TREE_VALUE (level),
8345 args, in_decl, complain,
8346 require_all_args,
8347 use_default_args);
8348 else
8349 l = TMPL_ARGS_LEVEL (args, cur_depth);
8351 if (l == error_mark_node)
8352 return error_mark_node;
8354 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8357 else
8358 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8359 args, in_decl, complain,
8360 require_all_args,
8361 use_default_args);
8362 return coerced_args;
8365 /* Returns 1 if template args OT and NT are equivalent. */
8368 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
8370 if (nt == ot)
8371 return 1;
8372 if (nt == NULL_TREE || ot == NULL_TREE)
8373 return false;
8374 if (nt == any_targ_node || ot == any_targ_node)
8375 return true;
8377 if (TREE_CODE (nt) == TREE_VEC)
8378 /* For member templates */
8379 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8380 else if (PACK_EXPANSION_P (ot))
8381 return (PACK_EXPANSION_P (nt)
8382 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8383 PACK_EXPANSION_PATTERN (nt))
8384 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8385 PACK_EXPANSION_EXTRA_ARGS (nt)));
8386 else if (ARGUMENT_PACK_P (ot))
8388 int i, len;
8389 tree opack, npack;
8391 if (!ARGUMENT_PACK_P (nt))
8392 return 0;
8394 opack = ARGUMENT_PACK_ARGS (ot);
8395 npack = ARGUMENT_PACK_ARGS (nt);
8396 len = TREE_VEC_LENGTH (opack);
8397 if (TREE_VEC_LENGTH (npack) != len)
8398 return 0;
8399 for (i = 0; i < len; ++i)
8400 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8401 TREE_VEC_ELT (npack, i)))
8402 return 0;
8403 return 1;
8405 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8406 gcc_unreachable ();
8407 else if (TYPE_P (nt))
8409 if (!TYPE_P (ot))
8410 return false;
8411 /* Don't treat an alias template specialization with dependent
8412 arguments as equivalent to its underlying type when used as a
8413 template argument; we need them to be distinct so that we
8414 substitute into the specialization arguments at instantiation
8415 time. And aliases can't be equivalent without being ==, so
8416 we don't need to look any deeper.
8418 During partial ordering, however, we need to treat them normally so
8419 that we can order uses of the same alias with different
8420 cv-qualification (79960). */
8421 if (!partial_order
8422 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
8423 return false;
8424 else
8425 return same_type_p (ot, nt);
8427 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8428 return 0;
8429 else
8431 /* Try to treat a template non-type argument that has been converted
8432 to the parameter type as equivalent to one that hasn't yet. */
8433 for (enum tree_code code1 = TREE_CODE (ot);
8434 CONVERT_EXPR_CODE_P (code1)
8435 || code1 == NON_LVALUE_EXPR;
8436 code1 = TREE_CODE (ot))
8437 ot = TREE_OPERAND (ot, 0);
8438 for (enum tree_code code2 = TREE_CODE (nt);
8439 CONVERT_EXPR_CODE_P (code2)
8440 || code2 == NON_LVALUE_EXPR;
8441 code2 = TREE_CODE (nt))
8442 nt = TREE_OPERAND (nt, 0);
8444 return cp_tree_equal (ot, nt);
8448 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8449 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8450 NEWARG_PTR with the offending arguments if they are non-NULL. */
8453 comp_template_args (tree oldargs, tree newargs,
8454 tree *oldarg_ptr, tree *newarg_ptr,
8455 bool partial_order)
8457 int i;
8459 if (oldargs == newargs)
8460 return 1;
8462 if (!oldargs || !newargs)
8463 return 0;
8465 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
8466 return 0;
8468 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
8470 tree nt = TREE_VEC_ELT (newargs, i);
8471 tree ot = TREE_VEC_ELT (oldargs, i);
8473 if (! template_args_equal (ot, nt, partial_order))
8475 if (oldarg_ptr != NULL)
8476 *oldarg_ptr = ot;
8477 if (newarg_ptr != NULL)
8478 *newarg_ptr = nt;
8479 return 0;
8482 return 1;
8485 inline bool
8486 comp_template_args_porder (tree oargs, tree nargs)
8488 return comp_template_args (oargs, nargs, NULL, NULL, true);
8491 static void
8492 add_pending_template (tree d)
8494 tree ti = (TYPE_P (d)
8495 ? CLASSTYPE_TEMPLATE_INFO (d)
8496 : DECL_TEMPLATE_INFO (d));
8497 struct pending_template *pt;
8498 int level;
8500 if (TI_PENDING_TEMPLATE_FLAG (ti))
8501 return;
8503 /* We are called both from instantiate_decl, where we've already had a
8504 tinst_level pushed, and instantiate_template, where we haven't.
8505 Compensate. */
8506 level = !current_tinst_level || current_tinst_level->decl != d;
8508 if (level)
8509 push_tinst_level (d);
8511 pt = ggc_alloc<pending_template> ();
8512 pt->next = NULL;
8513 pt->tinst = current_tinst_level;
8514 if (last_pending_template)
8515 last_pending_template->next = pt;
8516 else
8517 pending_templates = pt;
8519 last_pending_template = pt;
8521 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
8523 if (level)
8524 pop_tinst_level ();
8528 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8529 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8530 documentation for TEMPLATE_ID_EXPR. */
8532 tree
8533 lookup_template_function (tree fns, tree arglist)
8535 tree type;
8537 if (fns == error_mark_node || arglist == error_mark_node)
8538 return error_mark_node;
8540 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
8542 if (!is_overloaded_fn (fns) && !identifier_p (fns))
8544 error ("%q#D is not a function template", fns);
8545 return error_mark_node;
8548 if (BASELINK_P (fns))
8550 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
8551 unknown_type_node,
8552 BASELINK_FUNCTIONS (fns),
8553 arglist);
8554 return fns;
8557 type = TREE_TYPE (fns);
8558 if (TREE_CODE (fns) == OVERLOAD || !type)
8559 type = unknown_type_node;
8561 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
8564 /* Within the scope of a template class S<T>, the name S gets bound
8565 (in build_self_reference) to a TYPE_DECL for the class, not a
8566 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8567 or one of its enclosing classes, and that type is a template,
8568 return the associated TEMPLATE_DECL. Otherwise, the original
8569 DECL is returned.
8571 Also handle the case when DECL is a TREE_LIST of ambiguous
8572 injected-class-names from different bases. */
8574 tree
8575 maybe_get_template_decl_from_type_decl (tree decl)
8577 if (decl == NULL_TREE)
8578 return decl;
8580 /* DR 176: A lookup that finds an injected-class-name (10.2
8581 [class.member.lookup]) can result in an ambiguity in certain cases
8582 (for example, if it is found in more than one base class). If all of
8583 the injected-class-names that are found refer to specializations of
8584 the same class template, and if the name is followed by a
8585 template-argument-list, the reference refers to the class template
8586 itself and not a specialization thereof, and is not ambiguous. */
8587 if (TREE_CODE (decl) == TREE_LIST)
8589 tree t, tmpl = NULL_TREE;
8590 for (t = decl; t; t = TREE_CHAIN (t))
8592 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
8593 if (!tmpl)
8594 tmpl = elt;
8595 else if (tmpl != elt)
8596 break;
8598 if (tmpl && t == NULL_TREE)
8599 return tmpl;
8600 else
8601 return decl;
8604 return (decl != NULL_TREE
8605 && DECL_SELF_REFERENCE_P (decl)
8606 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
8607 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
8610 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8611 parameters, find the desired type.
8613 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8615 IN_DECL, if non-NULL, is the template declaration we are trying to
8616 instantiate.
8618 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8619 the class we are looking up.
8621 Issue error and warning messages under control of COMPLAIN.
8623 If the template class is really a local class in a template
8624 function, then the FUNCTION_CONTEXT is the function in which it is
8625 being instantiated.
8627 ??? Note that this function is currently called *twice* for each
8628 template-id: the first time from the parser, while creating the
8629 incomplete type (finish_template_type), and the second type during the
8630 real instantiation (instantiate_template_class). This is surely something
8631 that we want to avoid. It also causes some problems with argument
8632 coercion (see convert_nontype_argument for more information on this). */
8634 static tree
8635 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
8636 int entering_scope, tsubst_flags_t complain)
8638 tree templ = NULL_TREE, parmlist;
8639 tree t;
8640 spec_entry **slot;
8641 spec_entry *entry;
8642 spec_entry elt;
8643 hashval_t hash;
8645 if (identifier_p (d1))
8647 tree value = innermost_non_namespace_value (d1);
8648 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
8649 templ = value;
8650 else
8652 if (context)
8653 push_decl_namespace (context);
8654 templ = lookup_name (d1);
8655 templ = maybe_get_template_decl_from_type_decl (templ);
8656 if (context)
8657 pop_decl_namespace ();
8659 if (templ)
8660 context = DECL_CONTEXT (templ);
8662 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
8664 tree type = TREE_TYPE (d1);
8666 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8667 an implicit typename for the second A. Deal with it. */
8668 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
8669 type = TREE_TYPE (type);
8671 if (CLASSTYPE_TEMPLATE_INFO (type))
8673 templ = CLASSTYPE_TI_TEMPLATE (type);
8674 d1 = DECL_NAME (templ);
8677 else if (TREE_CODE (d1) == ENUMERAL_TYPE
8678 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
8680 templ = TYPE_TI_TEMPLATE (d1);
8681 d1 = DECL_NAME (templ);
8683 else if (DECL_TYPE_TEMPLATE_P (d1))
8685 templ = d1;
8686 d1 = DECL_NAME (templ);
8687 context = DECL_CONTEXT (templ);
8689 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
8691 templ = d1;
8692 d1 = DECL_NAME (templ);
8695 /* Issue an error message if we didn't find a template. */
8696 if (! templ)
8698 if (complain & tf_error)
8699 error ("%qT is not a template", d1);
8700 return error_mark_node;
8703 if (TREE_CODE (templ) != TEMPLATE_DECL
8704 /* Make sure it's a user visible template, if it was named by
8705 the user. */
8706 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
8707 && !PRIMARY_TEMPLATE_P (templ)))
8709 if (complain & tf_error)
8711 error ("non-template type %qT used as a template", d1);
8712 if (in_decl)
8713 error ("for template declaration %q+D", in_decl);
8715 return error_mark_node;
8718 complain &= ~tf_user;
8720 /* An alias that just changes the name of a template is equivalent to the
8721 other template, so if any of the arguments are pack expansions, strip
8722 the alias to avoid problems with a pack expansion passed to a non-pack
8723 alias template parameter (DR 1430). */
8724 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
8725 templ = get_underlying_template (templ);
8727 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
8729 tree parm;
8730 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
8731 if (arglist2 == error_mark_node
8732 || (!uses_template_parms (arglist2)
8733 && check_instantiated_args (templ, arglist2, complain)))
8734 return error_mark_node;
8736 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
8737 return parm;
8739 else
8741 tree template_type = TREE_TYPE (templ);
8742 tree gen_tmpl;
8743 tree type_decl;
8744 tree found = NULL_TREE;
8745 int arg_depth;
8746 int parm_depth;
8747 int is_dependent_type;
8748 int use_partial_inst_tmpl = false;
8750 if (template_type == error_mark_node)
8751 /* An error occurred while building the template TEMPL, and a
8752 diagnostic has most certainly been emitted for that
8753 already. Let's propagate that error. */
8754 return error_mark_node;
8756 gen_tmpl = most_general_template (templ);
8757 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
8758 parm_depth = TMPL_PARMS_DEPTH (parmlist);
8759 arg_depth = TMPL_ARGS_DEPTH (arglist);
8761 if (arg_depth == 1 && parm_depth > 1)
8763 /* We've been given an incomplete set of template arguments.
8764 For example, given:
8766 template <class T> struct S1 {
8767 template <class U> struct S2 {};
8768 template <class U> struct S2<U*> {};
8771 we will be called with an ARGLIST of `U*', but the
8772 TEMPLATE will be `template <class T> template
8773 <class U> struct S1<T>::S2'. We must fill in the missing
8774 arguments. */
8775 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
8776 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
8777 arg_depth = TMPL_ARGS_DEPTH (arglist);
8780 /* Now we should have enough arguments. */
8781 gcc_assert (parm_depth == arg_depth);
8783 /* From here on, we're only interested in the most general
8784 template. */
8786 /* Calculate the BOUND_ARGS. These will be the args that are
8787 actually tsubst'd into the definition to create the
8788 instantiation. */
8789 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
8790 complain,
8791 /*require_all_args=*/true,
8792 /*use_default_args=*/true);
8794 if (arglist == error_mark_node)
8795 /* We were unable to bind the arguments. */
8796 return error_mark_node;
8798 /* In the scope of a template class, explicit references to the
8799 template class refer to the type of the template, not any
8800 instantiation of it. For example, in:
8802 template <class T> class C { void f(C<T>); }
8804 the `C<T>' is just the same as `C'. Outside of the
8805 class, however, such a reference is an instantiation. */
8806 if (entering_scope
8807 || !PRIMARY_TEMPLATE_P (gen_tmpl)
8808 || currently_open_class (template_type))
8810 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
8812 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
8813 return template_type;
8816 /* If we already have this specialization, return it. */
8817 elt.tmpl = gen_tmpl;
8818 elt.args = arglist;
8819 elt.spec = NULL_TREE;
8820 hash = spec_hasher::hash (&elt);
8821 entry = type_specializations->find_with_hash (&elt, hash);
8823 if (entry)
8824 return entry->spec;
8826 /* If the the template's constraints are not satisfied,
8827 then we cannot form a valid type.
8829 Note that the check is deferred until after the hash
8830 lookup. This prevents redundant checks on previously
8831 instantiated specializations. */
8832 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
8834 if (complain & tf_error)
8836 error ("template constraint failure");
8837 diagnose_constraints (input_location, gen_tmpl, arglist);
8839 return error_mark_node;
8842 is_dependent_type = uses_template_parms (arglist);
8844 /* If the deduced arguments are invalid, then the binding
8845 failed. */
8846 if (!is_dependent_type
8847 && check_instantiated_args (gen_tmpl,
8848 INNERMOST_TEMPLATE_ARGS (arglist),
8849 complain))
8850 return error_mark_node;
8852 if (!is_dependent_type
8853 && !PRIMARY_TEMPLATE_P (gen_tmpl)
8854 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
8855 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
8857 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
8858 DECL_NAME (gen_tmpl),
8859 /*tag_scope=*/ts_global);
8860 return found;
8863 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
8864 complain, in_decl);
8865 if (context == error_mark_node)
8866 return error_mark_node;
8868 if (!context)
8869 context = global_namespace;
8871 /* Create the type. */
8872 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8874 /* The user referred to a specialization of an alias
8875 template represented by GEN_TMPL.
8877 [temp.alias]/2 says:
8879 When a template-id refers to the specialization of an
8880 alias template, it is equivalent to the associated
8881 type obtained by substitution of its
8882 template-arguments for the template-parameters in the
8883 type-id of the alias template. */
8885 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
8886 /* Note that the call above (by indirectly calling
8887 register_specialization in tsubst_decl) registers the
8888 TYPE_DECL representing the specialization of the alias
8889 template. So next time someone substitutes ARGLIST for
8890 the template parms into the alias template (GEN_TMPL),
8891 she'll get that TYPE_DECL back. */
8893 if (t == error_mark_node)
8894 return t;
8896 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
8898 if (!is_dependent_type)
8900 set_current_access_from_decl (TYPE_NAME (template_type));
8901 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
8902 tsubst (ENUM_UNDERLYING_TYPE (template_type),
8903 arglist, complain, in_decl),
8904 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
8905 arglist, complain, in_decl),
8906 SCOPED_ENUM_P (template_type), NULL);
8908 if (t == error_mark_node)
8909 return t;
8911 else
8913 /* We don't want to call start_enum for this type, since
8914 the values for the enumeration constants may involve
8915 template parameters. And, no one should be interested
8916 in the enumeration constants for such a type. */
8917 t = cxx_make_type (ENUMERAL_TYPE);
8918 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
8920 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
8921 ENUM_FIXED_UNDERLYING_TYPE_P (t)
8922 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
8924 else if (CLASS_TYPE_P (template_type))
8926 t = make_class_type (TREE_CODE (template_type));
8927 CLASSTYPE_DECLARED_CLASS (t)
8928 = CLASSTYPE_DECLARED_CLASS (template_type);
8929 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
8931 /* A local class. Make sure the decl gets registered properly. */
8932 if (context == current_function_decl)
8933 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
8935 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
8936 /* This instantiation is another name for the primary
8937 template type. Set the TYPE_CANONICAL field
8938 appropriately. */
8939 TYPE_CANONICAL (t) = template_type;
8940 else if (any_template_arguments_need_structural_equality_p (arglist))
8941 /* Some of the template arguments require structural
8942 equality testing, so this template class requires
8943 structural equality testing. */
8944 SET_TYPE_STRUCTURAL_EQUALITY (t);
8946 else
8947 gcc_unreachable ();
8949 /* If we called start_enum or pushtag above, this information
8950 will already be set up. */
8951 if (!TYPE_NAME (t))
8953 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
8955 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
8956 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
8957 DECL_SOURCE_LOCATION (type_decl)
8958 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
8960 else
8961 type_decl = TYPE_NAME (t);
8963 if (CLASS_TYPE_P (template_type))
8965 TREE_PRIVATE (type_decl)
8966 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
8967 TREE_PROTECTED (type_decl)
8968 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
8969 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
8971 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
8972 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
8976 if (OVERLOAD_TYPE_P (t)
8977 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8979 static const char *tags[] = {"abi_tag", "may_alias"};
8981 for (unsigned ix = 0; ix != 2; ix++)
8983 tree attributes
8984 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
8986 if (attributes)
8987 TYPE_ATTRIBUTES (t)
8988 = tree_cons (TREE_PURPOSE (attributes),
8989 TREE_VALUE (attributes),
8990 TYPE_ATTRIBUTES (t));
8994 /* Let's consider the explicit specialization of a member
8995 of a class template specialization that is implicitly instantiated,
8996 e.g.:
8997 template<class T>
8998 struct S
9000 template<class U> struct M {}; //#0
9003 template<>
9004 template<>
9005 struct S<int>::M<char> //#1
9007 int i;
9009 [temp.expl.spec]/4 says this is valid.
9011 In this case, when we write:
9012 S<int>::M<char> m;
9014 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9015 the one of #0.
9017 When we encounter #1, we want to store the partial instantiation
9018 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9020 For all cases other than this "explicit specialization of member of a
9021 class template", we just want to store the most general template into
9022 the CLASSTYPE_TI_TEMPLATE of M.
9024 This case of "explicit specialization of member of a class template"
9025 only happens when:
9026 1/ the enclosing class is an instantiation of, and therefore not
9027 the same as, the context of the most general template, and
9028 2/ we aren't looking at the partial instantiation itself, i.e.
9029 the innermost arguments are not the same as the innermost parms of
9030 the most general template.
9032 So it's only when 1/ and 2/ happens that we want to use the partial
9033 instantiation of the member template in lieu of its most general
9034 template. */
9036 if (PRIMARY_TEMPLATE_P (gen_tmpl)
9037 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
9038 /* the enclosing class must be an instantiation... */
9039 && CLASS_TYPE_P (context)
9040 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
9042 TREE_VEC_LENGTH (arglist)--;
9043 ++processing_template_decl;
9044 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
9045 tree partial_inst_args =
9046 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
9047 arglist, complain, NULL_TREE);
9048 --processing_template_decl;
9049 TREE_VEC_LENGTH (arglist)++;
9050 if (partial_inst_args == error_mark_node)
9051 return error_mark_node;
9052 use_partial_inst_tmpl =
9053 /*...and we must not be looking at the partial instantiation
9054 itself. */
9055 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
9056 partial_inst_args);
9059 if (!use_partial_inst_tmpl)
9060 /* This case is easy; there are no member templates involved. */
9061 found = gen_tmpl;
9062 else
9064 /* This is a full instantiation of a member template. Find
9065 the partial instantiation of which this is an instance. */
9067 /* Temporarily reduce by one the number of levels in the ARGLIST
9068 so as to avoid comparing the last set of arguments. */
9069 TREE_VEC_LENGTH (arglist)--;
9070 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
9071 TREE_VEC_LENGTH (arglist)++;
9072 /* FOUND is either a proper class type, or an alias
9073 template specialization. In the later case, it's a
9074 TYPE_DECL, resulting from the substituting of arguments
9075 for parameters in the TYPE_DECL of the alias template
9076 done earlier. So be careful while getting the template
9077 of FOUND. */
9078 found = (TREE_CODE (found) == TEMPLATE_DECL
9079 ? found
9080 : (TREE_CODE (found) == TYPE_DECL
9081 ? DECL_TI_TEMPLATE (found)
9082 : CLASSTYPE_TI_TEMPLATE (found)));
9085 // Build template info for the new specialization.
9086 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
9088 elt.spec = t;
9089 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
9090 entry = ggc_alloc<spec_entry> ();
9091 *entry = elt;
9092 *slot = entry;
9094 /* Note this use of the partial instantiation so we can check it
9095 later in maybe_process_partial_specialization. */
9096 DECL_TEMPLATE_INSTANTIATIONS (found)
9097 = tree_cons (arglist, t,
9098 DECL_TEMPLATE_INSTANTIATIONS (found));
9100 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
9101 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9102 /* Now that the type has been registered on the instantiations
9103 list, we set up the enumerators. Because the enumeration
9104 constants may involve the enumeration type itself, we make
9105 sure to register the type first, and then create the
9106 constants. That way, doing tsubst_expr for the enumeration
9107 constants won't result in recursive calls here; we'll find
9108 the instantiation and exit above. */
9109 tsubst_enum (template_type, t, arglist);
9111 if (CLASS_TYPE_P (template_type) && is_dependent_type)
9112 /* If the type makes use of template parameters, the
9113 code that generates debugging information will crash. */
9114 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
9116 /* Possibly limit visibility based on template args. */
9117 TREE_PUBLIC (type_decl) = 1;
9118 determine_visibility (type_decl);
9120 inherit_targ_abi_tags (t);
9122 return t;
9126 /* Wrapper for lookup_template_class_1. */
9128 tree
9129 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
9130 int entering_scope, tsubst_flags_t complain)
9132 tree ret;
9133 timevar_push (TV_TEMPLATE_INST);
9134 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
9135 entering_scope, complain);
9136 timevar_pop (TV_TEMPLATE_INST);
9137 return ret;
9140 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9142 tree
9143 lookup_template_variable (tree templ, tree arglist)
9145 /* The type of the expression is NULL_TREE since the template-id could refer
9146 to an explicit or partial specialization. */
9147 tree type = NULL_TREE;
9148 if (flag_concepts && variable_concept_p (templ))
9149 /* Except that concepts are always bool. */
9150 type = boolean_type_node;
9151 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
9154 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9156 tree
9157 finish_template_variable (tree var, tsubst_flags_t complain)
9159 tree templ = TREE_OPERAND (var, 0);
9160 tree arglist = TREE_OPERAND (var, 1);
9162 /* We never want to return a VAR_DECL for a variable concept, since they
9163 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9164 bool concept_p = flag_concepts && variable_concept_p (templ);
9165 if (concept_p && processing_template_decl)
9166 return var;
9168 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
9169 arglist = add_outermost_template_args (tmpl_args, arglist);
9171 templ = most_general_template (templ);
9172 tree parms = DECL_TEMPLATE_PARMS (templ);
9173 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
9174 /*req_all*/true,
9175 /*use_default*/true);
9177 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
9179 if (complain & tf_error)
9181 error ("use of invalid variable template %qE", var);
9182 diagnose_constraints (location_of (var), templ, arglist);
9184 return error_mark_node;
9187 /* If a template-id refers to a specialization of a variable
9188 concept, then the expression is true if and only if the
9189 concept's constraints are satisfied by the given template
9190 arguments.
9192 NOTE: This is an extension of Concepts Lite TS that
9193 allows constraints to be used in expressions. */
9194 if (concept_p)
9196 tree decl = DECL_TEMPLATE_RESULT (templ);
9197 return evaluate_variable_concept (decl, arglist);
9200 return instantiate_template (templ, arglist, complain);
9203 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9204 TARGS template args, and instantiate it if it's not dependent. */
9206 tree
9207 lookup_and_finish_template_variable (tree templ, tree targs,
9208 tsubst_flags_t complain)
9210 templ = lookup_template_variable (templ, targs);
9211 if (!any_dependent_template_arguments_p (targs))
9213 templ = finish_template_variable (templ, complain);
9214 mark_used (templ);
9217 return convert_from_reference (templ);
9221 struct pair_fn_data
9223 tree_fn_t fn;
9224 tree_fn_t any_fn;
9225 void *data;
9226 /* True when we should also visit template parameters that occur in
9227 non-deduced contexts. */
9228 bool include_nondeduced_p;
9229 hash_set<tree> *visited;
9232 /* Called from for_each_template_parm via walk_tree. */
9234 static tree
9235 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
9237 tree t = *tp;
9238 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
9239 tree_fn_t fn = pfd->fn;
9240 void *data = pfd->data;
9241 tree result = NULL_TREE;
9243 #define WALK_SUBTREE(NODE) \
9244 do \
9246 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9247 pfd->include_nondeduced_p, \
9248 pfd->any_fn); \
9249 if (result) goto out; \
9251 while (0)
9253 if (pfd->any_fn && (*pfd->any_fn)(t, data))
9254 return t;
9256 if (TYPE_P (t)
9257 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
9258 WALK_SUBTREE (TYPE_CONTEXT (t));
9260 switch (TREE_CODE (t))
9262 case RECORD_TYPE:
9263 if (TYPE_PTRMEMFUNC_P (t))
9264 break;
9265 /* Fall through. */
9267 case UNION_TYPE:
9268 case ENUMERAL_TYPE:
9269 if (!TYPE_TEMPLATE_INFO (t))
9270 *walk_subtrees = 0;
9271 else
9272 WALK_SUBTREE (TYPE_TI_ARGS (t));
9273 break;
9275 case INTEGER_TYPE:
9276 WALK_SUBTREE (TYPE_MIN_VALUE (t));
9277 WALK_SUBTREE (TYPE_MAX_VALUE (t));
9278 break;
9280 case METHOD_TYPE:
9281 /* Since we're not going to walk subtrees, we have to do this
9282 explicitly here. */
9283 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
9284 /* Fall through. */
9286 case FUNCTION_TYPE:
9287 /* Check the return type. */
9288 WALK_SUBTREE (TREE_TYPE (t));
9290 /* Check the parameter types. Since default arguments are not
9291 instantiated until they are needed, the TYPE_ARG_TYPES may
9292 contain expressions that involve template parameters. But,
9293 no-one should be looking at them yet. And, once they're
9294 instantiated, they don't contain template parameters, so
9295 there's no point in looking at them then, either. */
9297 tree parm;
9299 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
9300 WALK_SUBTREE (TREE_VALUE (parm));
9302 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9303 want walk_tree walking into them itself. */
9304 *walk_subtrees = 0;
9307 if (flag_noexcept_type)
9309 tree spec = TYPE_RAISES_EXCEPTIONS (t);
9310 if (spec)
9311 WALK_SUBTREE (TREE_PURPOSE (spec));
9313 break;
9315 case TYPEOF_TYPE:
9316 case UNDERLYING_TYPE:
9317 if (pfd->include_nondeduced_p
9318 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
9319 pfd->visited,
9320 pfd->include_nondeduced_p,
9321 pfd->any_fn))
9322 return error_mark_node;
9323 break;
9325 case FUNCTION_DECL:
9326 case VAR_DECL:
9327 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9328 WALK_SUBTREE (DECL_TI_ARGS (t));
9329 /* Fall through. */
9331 case PARM_DECL:
9332 case CONST_DECL:
9333 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
9334 WALK_SUBTREE (DECL_INITIAL (t));
9335 if (DECL_CONTEXT (t)
9336 && pfd->include_nondeduced_p)
9337 WALK_SUBTREE (DECL_CONTEXT (t));
9338 break;
9340 case BOUND_TEMPLATE_TEMPLATE_PARM:
9341 /* Record template parameters such as `T' inside `TT<T>'. */
9342 WALK_SUBTREE (TYPE_TI_ARGS (t));
9343 /* Fall through. */
9345 case TEMPLATE_TEMPLATE_PARM:
9346 case TEMPLATE_TYPE_PARM:
9347 case TEMPLATE_PARM_INDEX:
9348 if (fn && (*fn)(t, data))
9349 return t;
9350 else if (!fn)
9351 return t;
9352 break;
9354 case TEMPLATE_DECL:
9355 /* A template template parameter is encountered. */
9356 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9357 WALK_SUBTREE (TREE_TYPE (t));
9359 /* Already substituted template template parameter */
9360 *walk_subtrees = 0;
9361 break;
9363 case TYPENAME_TYPE:
9364 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9365 partial instantiation. */
9366 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
9367 break;
9369 case CONSTRUCTOR:
9370 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
9371 && pfd->include_nondeduced_p)
9372 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
9373 break;
9375 case INDIRECT_REF:
9376 case COMPONENT_REF:
9377 /* If there's no type, then this thing must be some expression
9378 involving template parameters. */
9379 if (!fn && !TREE_TYPE (t))
9380 return error_mark_node;
9381 break;
9383 case MODOP_EXPR:
9384 case CAST_EXPR:
9385 case IMPLICIT_CONV_EXPR:
9386 case REINTERPRET_CAST_EXPR:
9387 case CONST_CAST_EXPR:
9388 case STATIC_CAST_EXPR:
9389 case DYNAMIC_CAST_EXPR:
9390 case ARROW_EXPR:
9391 case DOTSTAR_EXPR:
9392 case TYPEID_EXPR:
9393 case PSEUDO_DTOR_EXPR:
9394 if (!fn)
9395 return error_mark_node;
9396 break;
9398 default:
9399 break;
9402 #undef WALK_SUBTREE
9404 /* We didn't find any template parameters we liked. */
9405 out:
9406 return result;
9409 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9410 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9411 call FN with the parameter and the DATA.
9412 If FN returns nonzero, the iteration is terminated, and
9413 for_each_template_parm returns 1. Otherwise, the iteration
9414 continues. If FN never returns a nonzero value, the value
9415 returned by for_each_template_parm is 0. If FN is NULL, it is
9416 considered to be the function which always returns 1.
9418 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9419 parameters that occur in non-deduced contexts. When false, only
9420 visits those template parameters that can be deduced. */
9422 static tree
9423 for_each_template_parm (tree t, tree_fn_t fn, void* data,
9424 hash_set<tree> *visited,
9425 bool include_nondeduced_p,
9426 tree_fn_t any_fn)
9428 struct pair_fn_data pfd;
9429 tree result;
9431 /* Set up. */
9432 pfd.fn = fn;
9433 pfd.any_fn = any_fn;
9434 pfd.data = data;
9435 pfd.include_nondeduced_p = include_nondeduced_p;
9437 /* Walk the tree. (Conceptually, we would like to walk without
9438 duplicates, but for_each_template_parm_r recursively calls
9439 for_each_template_parm, so we would need to reorganize a fair
9440 bit to use walk_tree_without_duplicates, so we keep our own
9441 visited list.) */
9442 if (visited)
9443 pfd.visited = visited;
9444 else
9445 pfd.visited = new hash_set<tree>;
9446 result = cp_walk_tree (&t,
9447 for_each_template_parm_r,
9448 &pfd,
9449 pfd.visited);
9451 /* Clean up. */
9452 if (!visited)
9454 delete pfd.visited;
9455 pfd.visited = 0;
9458 return result;
9461 /* Returns true if T depends on any template parameter. */
9464 uses_template_parms (tree t)
9466 if (t == NULL_TREE)
9467 return false;
9469 bool dependent_p;
9470 int saved_processing_template_decl;
9472 saved_processing_template_decl = processing_template_decl;
9473 if (!saved_processing_template_decl)
9474 processing_template_decl = 1;
9475 if (TYPE_P (t))
9476 dependent_p = dependent_type_p (t);
9477 else if (TREE_CODE (t) == TREE_VEC)
9478 dependent_p = any_dependent_template_arguments_p (t);
9479 else if (TREE_CODE (t) == TREE_LIST)
9480 dependent_p = (uses_template_parms (TREE_VALUE (t))
9481 || uses_template_parms (TREE_CHAIN (t)));
9482 else if (TREE_CODE (t) == TYPE_DECL)
9483 dependent_p = dependent_type_p (TREE_TYPE (t));
9484 else if (DECL_P (t)
9485 || EXPR_P (t)
9486 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
9487 || TREE_CODE (t) == OVERLOAD
9488 || BASELINK_P (t)
9489 || identifier_p (t)
9490 || TREE_CODE (t) == TRAIT_EXPR
9491 || TREE_CODE (t) == CONSTRUCTOR
9492 || CONSTANT_CLASS_P (t))
9493 dependent_p = (type_dependent_expression_p (t)
9494 || value_dependent_expression_p (t));
9495 else
9497 gcc_assert (t == error_mark_node);
9498 dependent_p = false;
9501 processing_template_decl = saved_processing_template_decl;
9503 return dependent_p;
9506 /* Returns true iff current_function_decl is an incompletely instantiated
9507 template. Useful instead of processing_template_decl because the latter
9508 is set to 0 during instantiate_non_dependent_expr. */
9510 bool
9511 in_template_function (void)
9513 tree fn = current_function_decl;
9514 bool ret;
9515 ++processing_template_decl;
9516 ret = (fn && DECL_LANG_SPECIFIC (fn)
9517 && DECL_TEMPLATE_INFO (fn)
9518 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
9519 --processing_template_decl;
9520 return ret;
9523 /* Returns true if T depends on any template parameter with level LEVEL. */
9525 bool
9526 uses_template_parms_level (tree t, int level)
9528 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
9529 /*include_nondeduced_p=*/true);
9532 /* Returns true if the signature of DECL depends on any template parameter from
9533 its enclosing class. */
9535 bool
9536 uses_outer_template_parms (tree decl)
9538 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
9539 if (depth == 0)
9540 return false;
9541 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
9542 &depth, NULL, /*include_nondeduced_p=*/true))
9543 return true;
9544 if (PRIMARY_TEMPLATE_P (decl)
9545 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9546 (DECL_TEMPLATE_PARMS (decl)),
9547 template_parm_outer_level,
9548 &depth, NULL, /*include_nondeduced_p=*/true))
9549 return true;
9550 tree ci = get_constraints (decl);
9551 if (ci)
9552 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
9553 if (ci && for_each_template_parm (ci, template_parm_outer_level,
9554 &depth, NULL, /*nondeduced*/true))
9555 return true;
9556 return false;
9559 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9560 ill-formed translation unit, i.e. a variable or function that isn't
9561 usable in a constant expression. */
9563 static inline bool
9564 neglectable_inst_p (tree d)
9566 return (DECL_P (d)
9567 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
9568 : decl_maybe_constant_var_p (d)));
9571 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9572 neglectable and instantiated from within an erroneous instantiation. */
9574 static bool
9575 limit_bad_template_recursion (tree decl)
9577 struct tinst_level *lev = current_tinst_level;
9578 int errs = errorcount + sorrycount;
9579 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
9580 return false;
9582 for (; lev; lev = lev->next)
9583 if (neglectable_inst_p (lev->decl))
9584 break;
9586 return (lev && errs > lev->errors);
9589 static int tinst_depth;
9590 extern int max_tinst_depth;
9591 int depth_reached;
9593 static GTY(()) struct tinst_level *last_error_tinst_level;
9595 /* We're starting to instantiate D; record the template instantiation context
9596 for diagnostics and to restore it later. */
9598 bool
9599 push_tinst_level (tree d)
9601 return push_tinst_level_loc (d, input_location);
9604 /* We're starting to instantiate D; record the template instantiation context
9605 at LOC for diagnostics and to restore it later. */
9607 bool
9608 push_tinst_level_loc (tree d, location_t loc)
9610 struct tinst_level *new_level;
9612 if (tinst_depth >= max_tinst_depth)
9614 /* Tell error.c not to try to instantiate any templates. */
9615 at_eof = 2;
9616 fatal_error (input_location,
9617 "template instantiation depth exceeds maximum of %d"
9618 " (use -ftemplate-depth= to increase the maximum)",
9619 max_tinst_depth);
9620 return false;
9623 /* If the current instantiation caused problems, don't let it instantiate
9624 anything else. Do allow deduction substitution and decls usable in
9625 constant expressions. */
9626 if (limit_bad_template_recursion (d))
9627 return false;
9629 /* When not -quiet, dump template instantiations other than functions, since
9630 announce_function will take care of those. */
9631 if (!quiet_flag
9632 && TREE_CODE (d) != TREE_LIST
9633 && TREE_CODE (d) != FUNCTION_DECL)
9634 fprintf (stderr, " %s", decl_as_string (d, TFF_DECL_SPECIFIERS));
9636 new_level = ggc_alloc<tinst_level> ();
9637 new_level->decl = d;
9638 new_level->locus = loc;
9639 new_level->errors = errorcount+sorrycount;
9640 new_level->in_system_header_p = in_system_header_at (input_location);
9641 new_level->next = current_tinst_level;
9642 current_tinst_level = new_level;
9644 ++tinst_depth;
9645 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
9646 depth_reached = tinst_depth;
9648 return true;
9651 /* We're done instantiating this template; return to the instantiation
9652 context. */
9654 void
9655 pop_tinst_level (void)
9657 /* Restore the filename and line number stashed away when we started
9658 this instantiation. */
9659 input_location = current_tinst_level->locus;
9660 current_tinst_level = current_tinst_level->next;
9661 --tinst_depth;
9664 /* We're instantiating a deferred template; restore the template
9665 instantiation context in which the instantiation was requested, which
9666 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9668 static tree
9669 reopen_tinst_level (struct tinst_level *level)
9671 struct tinst_level *t;
9673 tinst_depth = 0;
9674 for (t = level; t; t = t->next)
9675 ++tinst_depth;
9677 current_tinst_level = level;
9678 pop_tinst_level ();
9679 if (current_tinst_level)
9680 current_tinst_level->errors = errorcount+sorrycount;
9681 return level->decl;
9684 /* Returns the TINST_LEVEL which gives the original instantiation
9685 context. */
9687 struct tinst_level *
9688 outermost_tinst_level (void)
9690 struct tinst_level *level = current_tinst_level;
9691 if (level)
9692 while (level->next)
9693 level = level->next;
9694 return level;
9697 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9698 vector of template arguments, as for tsubst.
9700 Returns an appropriate tsubst'd friend declaration. */
9702 static tree
9703 tsubst_friend_function (tree decl, tree args)
9705 tree new_friend;
9707 if (TREE_CODE (decl) == FUNCTION_DECL
9708 && DECL_TEMPLATE_INSTANTIATION (decl)
9709 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
9710 /* This was a friend declared with an explicit template
9711 argument list, e.g.:
9713 friend void f<>(T);
9715 to indicate that f was a template instantiation, not a new
9716 function declaration. Now, we have to figure out what
9717 instantiation of what template. */
9719 tree template_id, arglist, fns;
9720 tree new_args;
9721 tree tmpl;
9722 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
9724 /* Friend functions are looked up in the containing namespace scope.
9725 We must enter that scope, to avoid finding member functions of the
9726 current class with same name. */
9727 push_nested_namespace (ns);
9728 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
9729 tf_warning_or_error, NULL_TREE,
9730 /*integral_constant_expression_p=*/false);
9731 pop_nested_namespace (ns);
9732 arglist = tsubst (DECL_TI_ARGS (decl), args,
9733 tf_warning_or_error, NULL_TREE);
9734 template_id = lookup_template_function (fns, arglist);
9736 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9737 tmpl = determine_specialization (template_id, new_friend,
9738 &new_args,
9739 /*need_member_template=*/0,
9740 TREE_VEC_LENGTH (args),
9741 tsk_none);
9742 return instantiate_template (tmpl, new_args, tf_error);
9745 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9747 /* The NEW_FRIEND will look like an instantiation, to the
9748 compiler, but is not an instantiation from the point of view of
9749 the language. For example, we might have had:
9751 template <class T> struct S {
9752 template <class U> friend void f(T, U);
9755 Then, in S<int>, template <class U> void f(int, U) is not an
9756 instantiation of anything. */
9757 if (new_friend == error_mark_node)
9758 return error_mark_node;
9760 DECL_USE_TEMPLATE (new_friend) = 0;
9761 if (TREE_CODE (decl) == TEMPLATE_DECL)
9763 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
9764 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
9765 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
9768 /* The mangled name for the NEW_FRIEND is incorrect. The function
9769 is not a template instantiation and should not be mangled like
9770 one. Therefore, we forget the mangling here; we'll recompute it
9771 later if we need it. */
9772 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
9774 SET_DECL_RTL (new_friend, NULL);
9775 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
9778 if (DECL_NAMESPACE_SCOPE_P (new_friend))
9780 tree old_decl;
9781 tree new_friend_template_info;
9782 tree new_friend_result_template_info;
9783 tree ns;
9784 int new_friend_is_defn;
9786 /* We must save some information from NEW_FRIEND before calling
9787 duplicate decls since that function will free NEW_FRIEND if
9788 possible. */
9789 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
9790 new_friend_is_defn =
9791 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9792 (template_for_substitution (new_friend)))
9793 != NULL_TREE);
9794 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
9796 /* This declaration is a `primary' template. */
9797 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
9799 new_friend_result_template_info
9800 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
9802 else
9803 new_friend_result_template_info = NULL_TREE;
9805 /* Inside pushdecl_namespace_level, we will push into the
9806 current namespace. However, the friend function should go
9807 into the namespace of the template. */
9808 ns = decl_namespace_context (new_friend);
9809 push_nested_namespace (ns);
9810 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
9811 pop_nested_namespace (ns);
9813 if (old_decl == error_mark_node)
9814 return error_mark_node;
9816 if (old_decl != new_friend)
9818 /* This new friend declaration matched an existing
9819 declaration. For example, given:
9821 template <class T> void f(T);
9822 template <class U> class C {
9823 template <class T> friend void f(T) {}
9826 the friend declaration actually provides the definition
9827 of `f', once C has been instantiated for some type. So,
9828 old_decl will be the out-of-class template declaration,
9829 while new_friend is the in-class definition.
9831 But, if `f' was called before this point, the
9832 instantiation of `f' will have DECL_TI_ARGS corresponding
9833 to `T' but not to `U', references to which might appear
9834 in the definition of `f'. Previously, the most general
9835 template for an instantiation of `f' was the out-of-class
9836 version; now it is the in-class version. Therefore, we
9837 run through all specialization of `f', adding to their
9838 DECL_TI_ARGS appropriately. In particular, they need a
9839 new set of outer arguments, corresponding to the
9840 arguments for this class instantiation.
9842 The same situation can arise with something like this:
9844 friend void f(int);
9845 template <class T> class C {
9846 friend void f(T) {}
9849 when `C<int>' is instantiated. Now, `f(int)' is defined
9850 in the class. */
9852 if (!new_friend_is_defn)
9853 /* On the other hand, if the in-class declaration does
9854 *not* provide a definition, then we don't want to alter
9855 existing definitions. We can just leave everything
9856 alone. */
9858 else
9860 tree new_template = TI_TEMPLATE (new_friend_template_info);
9861 tree new_args = TI_ARGS (new_friend_template_info);
9863 /* Overwrite whatever template info was there before, if
9864 any, with the new template information pertaining to
9865 the declaration. */
9866 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
9868 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
9870 /* We should have called reregister_specialization in
9871 duplicate_decls. */
9872 gcc_assert (retrieve_specialization (new_template,
9873 new_args, 0)
9874 == old_decl);
9876 /* Instantiate it if the global has already been used. */
9877 if (DECL_ODR_USED (old_decl))
9878 instantiate_decl (old_decl, /*defer_ok=*/true,
9879 /*expl_inst_class_mem_p=*/false);
9881 else
9883 tree t;
9885 /* Indicate that the old function template is a partial
9886 instantiation. */
9887 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
9888 = new_friend_result_template_info;
9890 gcc_assert (new_template
9891 == most_general_template (new_template));
9892 gcc_assert (new_template != old_decl);
9894 /* Reassign any specializations already in the hash table
9895 to the new more general template, and add the
9896 additional template args. */
9897 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
9898 t != NULL_TREE;
9899 t = TREE_CHAIN (t))
9901 tree spec = TREE_VALUE (t);
9902 spec_entry elt;
9904 elt.tmpl = old_decl;
9905 elt.args = DECL_TI_ARGS (spec);
9906 elt.spec = NULL_TREE;
9908 decl_specializations->remove_elt (&elt);
9910 DECL_TI_ARGS (spec)
9911 = add_outermost_template_args (new_args,
9912 DECL_TI_ARGS (spec));
9914 register_specialization
9915 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
9918 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
9922 /* The information from NEW_FRIEND has been merged into OLD_DECL
9923 by duplicate_decls. */
9924 new_friend = old_decl;
9927 else
9929 tree context = DECL_CONTEXT (new_friend);
9930 bool dependent_p;
9932 /* In the code
9933 template <class T> class C {
9934 template <class U> friend void C1<U>::f (); // case 1
9935 friend void C2<T>::f (); // case 2
9937 we only need to make sure CONTEXT is a complete type for
9938 case 2. To distinguish between the two cases, we note that
9939 CONTEXT of case 1 remains dependent type after tsubst while
9940 this isn't true for case 2. */
9941 ++processing_template_decl;
9942 dependent_p = dependent_type_p (context);
9943 --processing_template_decl;
9945 if (!dependent_p
9946 && !complete_type_or_else (context, NULL_TREE))
9947 return error_mark_node;
9949 if (COMPLETE_TYPE_P (context))
9951 tree fn = new_friend;
9952 /* do_friend adds the TEMPLATE_DECL for any member friend
9953 template even if it isn't a member template, i.e.
9954 template <class T> friend A<T>::f();
9955 Look through it in that case. */
9956 if (TREE_CODE (fn) == TEMPLATE_DECL
9957 && !PRIMARY_TEMPLATE_P (fn))
9958 fn = DECL_TEMPLATE_RESULT (fn);
9959 /* Check to see that the declaration is really present, and,
9960 possibly obtain an improved declaration. */
9961 fn = check_classfn (context, fn, NULL_TREE);
9963 if (fn)
9964 new_friend = fn;
9968 return new_friend;
9971 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
9972 template arguments, as for tsubst.
9974 Returns an appropriate tsubst'd friend type or error_mark_node on
9975 failure. */
9977 static tree
9978 tsubst_friend_class (tree friend_tmpl, tree args)
9980 tree friend_type;
9981 tree tmpl;
9982 tree context;
9984 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
9986 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
9987 return TREE_TYPE (t);
9990 context = CP_DECL_CONTEXT (friend_tmpl);
9992 if (context != global_namespace)
9994 if (TREE_CODE (context) == NAMESPACE_DECL)
9995 push_nested_namespace (context);
9996 else
9997 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
10000 /* Look for a class template declaration. We look for hidden names
10001 because two friend declarations of the same template are the
10002 same. For example, in:
10004 struct A {
10005 template <typename> friend class F;
10007 template <typename> struct B {
10008 template <typename> friend class F;
10011 both F templates are the same. */
10012 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
10013 /*block_p=*/true, 0, LOOKUP_HIDDEN);
10015 /* But, if we don't find one, it might be because we're in a
10016 situation like this:
10018 template <class T>
10019 struct S {
10020 template <class U>
10021 friend struct S;
10024 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
10025 for `S<int>', not the TEMPLATE_DECL. */
10026 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
10028 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
10029 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
10032 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
10034 /* The friend template has already been declared. Just
10035 check to see that the declarations match, and install any new
10036 default parameters. We must tsubst the default parameters,
10037 of course. We only need the innermost template parameters
10038 because that is all that redeclare_class_template will look
10039 at. */
10040 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
10041 > TMPL_ARGS_DEPTH (args))
10043 tree parms;
10044 location_t saved_input_location;
10045 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
10046 args, tf_warning_or_error);
10048 saved_input_location = input_location;
10049 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
10050 tree cons = get_constraints (tmpl);
10051 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
10052 input_location = saved_input_location;
10056 friend_type = TREE_TYPE (tmpl);
10058 else
10060 /* The friend template has not already been declared. In this
10061 case, the instantiation of the template class will cause the
10062 injection of this template into the global scope. */
10063 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
10064 if (tmpl == error_mark_node)
10065 return error_mark_node;
10067 /* The new TMPL is not an instantiation of anything, so we
10068 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
10069 the new type because that is supposed to be the corresponding
10070 template decl, i.e., TMPL. */
10071 DECL_USE_TEMPLATE (tmpl) = 0;
10072 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
10073 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
10074 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
10075 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
10077 /* Inject this template into the global scope. */
10078 friend_type = TREE_TYPE (pushdecl_top_level (tmpl, true));
10081 if (context != global_namespace)
10083 if (TREE_CODE (context) == NAMESPACE_DECL)
10084 pop_nested_namespace (context);
10085 else
10086 pop_nested_class ();
10089 return friend_type;
10092 /* Returns zero if TYPE cannot be completed later due to circularity.
10093 Otherwise returns one. */
10095 static int
10096 can_complete_type_without_circularity (tree type)
10098 if (type == NULL_TREE || type == error_mark_node)
10099 return 0;
10100 else if (COMPLETE_TYPE_P (type))
10101 return 1;
10102 else if (TREE_CODE (type) == ARRAY_TYPE)
10103 return can_complete_type_without_circularity (TREE_TYPE (type));
10104 else if (CLASS_TYPE_P (type)
10105 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
10106 return 0;
10107 else
10108 return 1;
10111 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
10112 tsubst_flags_t, tree);
10114 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10115 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10117 static tree
10118 tsubst_attribute (tree t, tree *decl_p, tree args,
10119 tsubst_flags_t complain, tree in_decl)
10121 gcc_assert (ATTR_IS_DEPENDENT (t));
10123 tree val = TREE_VALUE (t);
10124 if (val == NULL_TREE)
10125 /* Nothing to do. */;
10126 else if ((flag_openmp || flag_openmp_simd || flag_cilkplus)
10127 && is_attribute_p ("omp declare simd",
10128 get_attribute_name (t)))
10130 tree clauses = TREE_VALUE (val);
10131 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
10132 complain, in_decl);
10133 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
10134 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
10135 tree parms = DECL_ARGUMENTS (*decl_p);
10136 clauses
10137 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
10138 if (clauses)
10139 val = build_tree_list (NULL_TREE, clauses);
10140 else
10141 val = NULL_TREE;
10143 /* If the first attribute argument is an identifier, don't
10144 pass it through tsubst. Attributes like mode, format,
10145 cleanup and several target specific attributes expect it
10146 unmodified. */
10147 else if (attribute_takes_identifier_p (get_attribute_name (t)))
10149 tree chain
10150 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
10151 /*integral_constant_expression_p=*/false);
10152 if (chain != TREE_CHAIN (val))
10153 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
10155 else if (PACK_EXPANSION_P (val))
10157 /* An attribute pack expansion. */
10158 tree purp = TREE_PURPOSE (t);
10159 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
10160 if (pack == error_mark_node)
10161 return error_mark_node;
10162 int len = TREE_VEC_LENGTH (pack);
10163 tree list = NULL_TREE;
10164 tree *q = &list;
10165 for (int i = 0; i < len; ++i)
10167 tree elt = TREE_VEC_ELT (pack, i);
10168 *q = build_tree_list (purp, elt);
10169 q = &TREE_CHAIN (*q);
10171 return list;
10173 else
10174 val = tsubst_expr (val, args, complain, in_decl,
10175 /*integral_constant_expression_p=*/false);
10177 if (val != TREE_VALUE (t))
10178 return build_tree_list (TREE_PURPOSE (t), val);
10179 return t;
10182 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10183 unchanged or a new TREE_LIST chain. */
10185 static tree
10186 tsubst_attributes (tree attributes, tree args,
10187 tsubst_flags_t complain, tree in_decl)
10189 tree last_dep = NULL_TREE;
10191 for (tree t = attributes; t; t = TREE_CHAIN (t))
10192 if (ATTR_IS_DEPENDENT (t))
10194 last_dep = t;
10195 attributes = copy_list (attributes);
10196 break;
10199 if (last_dep)
10200 for (tree *p = &attributes; *p; )
10202 tree t = *p;
10203 if (ATTR_IS_DEPENDENT (t))
10205 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
10206 if (subst != t)
10208 *p = subst;
10210 p = &TREE_CHAIN (*p);
10211 while (*p);
10212 *p = TREE_CHAIN (t);
10213 continue;
10216 p = &TREE_CHAIN (*p);
10219 return attributes;
10222 /* Apply any attributes which had to be deferred until instantiation
10223 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10224 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10226 static void
10227 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
10228 tree args, tsubst_flags_t complain, tree in_decl)
10230 tree last_dep = NULL_TREE;
10231 tree t;
10232 tree *p;
10234 if (attributes == NULL_TREE)
10235 return;
10237 if (DECL_P (*decl_p))
10239 if (TREE_TYPE (*decl_p) == error_mark_node)
10240 return;
10241 p = &DECL_ATTRIBUTES (*decl_p);
10242 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10243 to our attributes parameter. */
10244 gcc_assert (*p == attributes);
10246 else
10248 p = &TYPE_ATTRIBUTES (*decl_p);
10249 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10250 lookup_template_class_1, and should be preserved. */
10251 gcc_assert (*p != attributes);
10252 while (*p)
10253 p = &TREE_CHAIN (*p);
10256 for (t = attributes; t; t = TREE_CHAIN (t))
10257 if (ATTR_IS_DEPENDENT (t))
10259 last_dep = t;
10260 attributes = copy_list (attributes);
10261 break;
10264 *p = attributes;
10265 if (last_dep)
10267 tree late_attrs = NULL_TREE;
10268 tree *q = &late_attrs;
10270 for (; *p; )
10272 t = *p;
10273 if (ATTR_IS_DEPENDENT (t))
10275 *p = TREE_CHAIN (t);
10276 TREE_CHAIN (t) = NULL_TREE;
10277 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
10279 q = &TREE_CHAIN (*q);
10280 while (*q);
10282 else
10283 p = &TREE_CHAIN (t);
10286 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
10290 /* Perform (or defer) access check for typedefs that were referenced
10291 from within the template TMPL code.
10292 This is a subroutine of instantiate_decl and instantiate_class_template.
10293 TMPL is the template to consider and TARGS is the list of arguments of
10294 that template. */
10296 static void
10297 perform_typedefs_access_check (tree tmpl, tree targs)
10299 location_t saved_location;
10300 unsigned i;
10301 qualified_typedef_usage_t *iter;
10303 if (!tmpl
10304 || (!CLASS_TYPE_P (tmpl)
10305 && TREE_CODE (tmpl) != FUNCTION_DECL))
10306 return;
10308 saved_location = input_location;
10309 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
10311 tree type_decl = iter->typedef_decl;
10312 tree type_scope = iter->context;
10314 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
10315 continue;
10317 if (uses_template_parms (type_decl))
10318 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
10319 if (uses_template_parms (type_scope))
10320 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
10322 /* Make access check error messages point to the location
10323 of the use of the typedef. */
10324 input_location = iter->locus;
10325 perform_or_defer_access_check (TYPE_BINFO (type_scope),
10326 type_decl, type_decl,
10327 tf_warning_or_error);
10329 input_location = saved_location;
10332 static tree
10333 instantiate_class_template_1 (tree type)
10335 tree templ, args, pattern, t, member;
10336 tree typedecl;
10337 tree pbinfo;
10338 tree base_list;
10339 unsigned int saved_maximum_field_alignment;
10340 tree fn_context;
10342 if (type == error_mark_node)
10343 return error_mark_node;
10345 if (COMPLETE_OR_OPEN_TYPE_P (type)
10346 || uses_template_parms (type))
10347 return type;
10349 /* Figure out which template is being instantiated. */
10350 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
10351 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
10353 /* Determine what specialization of the original template to
10354 instantiate. */
10355 t = most_specialized_partial_spec (type, tf_warning_or_error);
10356 if (t == error_mark_node)
10358 TYPE_BEING_DEFINED (type) = 1;
10359 return error_mark_node;
10361 else if (t)
10363 /* This TYPE is actually an instantiation of a partial
10364 specialization. We replace the innermost set of ARGS with
10365 the arguments appropriate for substitution. For example,
10366 given:
10368 template <class T> struct S {};
10369 template <class T> struct S<T*> {};
10371 and supposing that we are instantiating S<int*>, ARGS will
10372 presently be {int*} -- but we need {int}. */
10373 pattern = TREE_TYPE (t);
10374 args = TREE_PURPOSE (t);
10376 else
10378 pattern = TREE_TYPE (templ);
10379 args = CLASSTYPE_TI_ARGS (type);
10382 /* If the template we're instantiating is incomplete, then clearly
10383 there's nothing we can do. */
10384 if (!COMPLETE_TYPE_P (pattern))
10385 return type;
10387 /* If we've recursively instantiated too many templates, stop. */
10388 if (! push_tinst_level (type))
10389 return type;
10391 /* Now we're really doing the instantiation. Mark the type as in
10392 the process of being defined. */
10393 TYPE_BEING_DEFINED (type) = 1;
10395 /* We may be in the middle of deferred access check. Disable
10396 it now. */
10397 push_deferring_access_checks (dk_no_deferred);
10399 int saved_unevaluated_operand = cp_unevaluated_operand;
10400 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10402 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
10403 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10404 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
10405 fn_context = error_mark_node;
10406 if (!fn_context)
10407 push_to_top_level ();
10408 else
10410 cp_unevaluated_operand = 0;
10411 c_inhibit_evaluation_warnings = 0;
10413 /* Use #pragma pack from the template context. */
10414 saved_maximum_field_alignment = maximum_field_alignment;
10415 maximum_field_alignment = TYPE_PRECISION (pattern);
10417 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
10419 /* Set the input location to the most specialized template definition.
10420 This is needed if tsubsting causes an error. */
10421 typedecl = TYPE_MAIN_DECL (pattern);
10422 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
10423 DECL_SOURCE_LOCATION (typedecl);
10425 TYPE_PACKED (type) = TYPE_PACKED (pattern);
10426 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
10427 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
10428 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
10429 if (ANON_AGGR_TYPE_P (pattern))
10430 SET_ANON_AGGR_TYPE_P (type);
10431 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
10433 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
10434 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
10435 /* Adjust visibility for template arguments. */
10436 determine_visibility (TYPE_MAIN_DECL (type));
10438 if (CLASS_TYPE_P (type))
10439 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
10441 pbinfo = TYPE_BINFO (pattern);
10443 /* We should never instantiate a nested class before its enclosing
10444 class; we need to look up the nested class by name before we can
10445 instantiate it, and that lookup should instantiate the enclosing
10446 class. */
10447 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
10448 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
10450 base_list = NULL_TREE;
10451 if (BINFO_N_BASE_BINFOS (pbinfo))
10453 tree pbase_binfo;
10454 tree pushed_scope;
10455 int i;
10457 /* We must enter the scope containing the type, as that is where
10458 the accessibility of types named in dependent bases are
10459 looked up from. */
10460 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
10462 /* Substitute into each of the bases to determine the actual
10463 basetypes. */
10464 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
10466 tree base;
10467 tree access = BINFO_BASE_ACCESS (pbinfo, i);
10468 tree expanded_bases = NULL_TREE;
10469 int idx, len = 1;
10471 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
10473 expanded_bases =
10474 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
10475 args, tf_error, NULL_TREE);
10476 if (expanded_bases == error_mark_node)
10477 continue;
10479 len = TREE_VEC_LENGTH (expanded_bases);
10482 for (idx = 0; idx < len; idx++)
10484 if (expanded_bases)
10485 /* Extract the already-expanded base class. */
10486 base = TREE_VEC_ELT (expanded_bases, idx);
10487 else
10488 /* Substitute to figure out the base class. */
10489 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
10490 NULL_TREE);
10492 if (base == error_mark_node)
10493 continue;
10495 base_list = tree_cons (access, base, base_list);
10496 if (BINFO_VIRTUAL_P (pbase_binfo))
10497 TREE_TYPE (base_list) = integer_type_node;
10501 /* The list is now in reverse order; correct that. */
10502 base_list = nreverse (base_list);
10504 if (pushed_scope)
10505 pop_scope (pushed_scope);
10507 /* Now call xref_basetypes to set up all the base-class
10508 information. */
10509 xref_basetypes (type, base_list);
10511 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
10512 (int) ATTR_FLAG_TYPE_IN_PLACE,
10513 args, tf_error, NULL_TREE);
10514 fixup_attribute_variants (type);
10516 /* Now that our base classes are set up, enter the scope of the
10517 class, so that name lookups into base classes, etc. will work
10518 correctly. This is precisely analogous to what we do in
10519 begin_class_definition when defining an ordinary non-template
10520 class, except we also need to push the enclosing classes. */
10521 push_nested_class (type);
10523 /* Now members are processed in the order of declaration. */
10524 for (member = CLASSTYPE_DECL_LIST (pattern);
10525 member; member = TREE_CHAIN (member))
10527 tree t = TREE_VALUE (member);
10529 if (TREE_PURPOSE (member))
10531 if (TYPE_P (t))
10533 /* Build new CLASSTYPE_NESTED_UTDS. */
10535 tree newtag;
10536 bool class_template_p;
10538 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
10539 && TYPE_LANG_SPECIFIC (t)
10540 && CLASSTYPE_IS_TEMPLATE (t));
10541 /* If the member is a class template, then -- even after
10542 substitution -- there may be dependent types in the
10543 template argument list for the class. We increment
10544 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10545 that function will assume that no types are dependent
10546 when outside of a template. */
10547 if (class_template_p)
10548 ++processing_template_decl;
10549 newtag = tsubst (t, args, tf_error, NULL_TREE);
10550 if (class_template_p)
10551 --processing_template_decl;
10552 if (newtag == error_mark_node)
10553 continue;
10555 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
10557 tree name = TYPE_IDENTIFIER (t);
10559 if (class_template_p)
10560 /* Unfortunately, lookup_template_class sets
10561 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10562 instantiation (i.e., for the type of a member
10563 template class nested within a template class.)
10564 This behavior is required for
10565 maybe_process_partial_specialization to work
10566 correctly, but is not accurate in this case;
10567 the TAG is not an instantiation of anything.
10568 (The corresponding TEMPLATE_DECL is an
10569 instantiation, but the TYPE is not.) */
10570 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
10572 /* Now, we call pushtag to put this NEWTAG into the scope of
10573 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10574 pushtag calling push_template_decl. We don't have to do
10575 this for enums because it will already have been done in
10576 tsubst_enum. */
10577 if (name)
10578 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
10579 pushtag (name, newtag, /*tag_scope=*/ts_current);
10582 else if (DECL_DECLARES_FUNCTION_P (t))
10584 /* Build new TYPE_METHODS. */
10585 tree r;
10587 if (TREE_CODE (t) == TEMPLATE_DECL)
10588 ++processing_template_decl;
10589 r = tsubst (t, args, tf_error, NULL_TREE);
10590 if (TREE_CODE (t) == TEMPLATE_DECL)
10591 --processing_template_decl;
10592 set_current_access_from_decl (r);
10593 finish_member_declaration (r);
10594 /* Instantiate members marked with attribute used. */
10595 if (r != error_mark_node && DECL_PRESERVE_P (r))
10596 mark_used (r);
10597 if (TREE_CODE (r) == FUNCTION_DECL
10598 && DECL_OMP_DECLARE_REDUCTION_P (r))
10599 cp_check_omp_declare_reduction (r);
10601 else if (DECL_CLASS_TEMPLATE_P (t)
10602 && LAMBDA_TYPE_P (TREE_TYPE (t)))
10603 /* A closure type for a lambda in a default argument for a
10604 member template. Ignore it; it will be instantiated with
10605 the default argument. */;
10606 else
10608 /* Build new TYPE_FIELDS. */
10609 if (TREE_CODE (t) == STATIC_ASSERT)
10611 tree condition;
10613 ++c_inhibit_evaluation_warnings;
10614 condition =
10615 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
10616 tf_warning_or_error, NULL_TREE,
10617 /*integral_constant_expression_p=*/true);
10618 --c_inhibit_evaluation_warnings;
10620 finish_static_assert (condition,
10621 STATIC_ASSERT_MESSAGE (t),
10622 STATIC_ASSERT_SOURCE_LOCATION (t),
10623 /*member_p=*/true);
10625 else if (TREE_CODE (t) != CONST_DECL)
10627 tree r;
10628 tree vec = NULL_TREE;
10629 int len = 1;
10631 /* The file and line for this declaration, to
10632 assist in error message reporting. Since we
10633 called push_tinst_level above, we don't need to
10634 restore these. */
10635 input_location = DECL_SOURCE_LOCATION (t);
10637 if (TREE_CODE (t) == TEMPLATE_DECL)
10638 ++processing_template_decl;
10639 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
10640 if (TREE_CODE (t) == TEMPLATE_DECL)
10641 --processing_template_decl;
10643 if (TREE_CODE (r) == TREE_VEC)
10645 /* A capture pack became multiple fields. */
10646 vec = r;
10647 len = TREE_VEC_LENGTH (vec);
10650 for (int i = 0; i < len; ++i)
10652 if (vec)
10653 r = TREE_VEC_ELT (vec, i);
10654 if (VAR_P (r))
10656 /* In [temp.inst]:
10658 [t]he initialization (and any associated
10659 side-effects) of a static data member does
10660 not occur unless the static data member is
10661 itself used in a way that requires the
10662 definition of the static data member to
10663 exist.
10665 Therefore, we do not substitute into the
10666 initialized for the static data member here. */
10667 finish_static_data_member_decl
10669 /*init=*/NULL_TREE,
10670 /*init_const_expr_p=*/false,
10671 /*asmspec_tree=*/NULL_TREE,
10672 /*flags=*/0);
10673 /* Instantiate members marked with attribute used. */
10674 if (r != error_mark_node && DECL_PRESERVE_P (r))
10675 mark_used (r);
10677 else if (TREE_CODE (r) == FIELD_DECL)
10679 /* Determine whether R has a valid type and can be
10680 completed later. If R is invalid, then its type
10681 is replaced by error_mark_node. */
10682 tree rtype = TREE_TYPE (r);
10683 if (can_complete_type_without_circularity (rtype))
10684 complete_type (rtype);
10686 if (!complete_or_array_type_p (rtype))
10688 /* If R's type couldn't be completed and
10689 it isn't a flexible array member (whose
10690 type is incomplete by definition) give
10691 an error. */
10692 cxx_incomplete_type_error (r, rtype);
10693 TREE_TYPE (r) = error_mark_node;
10697 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10698 such a thing will already have been added to the field
10699 list by tsubst_enum in finish_member_declaration in the
10700 CLASSTYPE_NESTED_UTDS case above. */
10701 if (!(TREE_CODE (r) == TYPE_DECL
10702 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
10703 && DECL_ARTIFICIAL (r)))
10705 set_current_access_from_decl (r);
10706 finish_member_declaration (r);
10712 else
10714 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
10715 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10717 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10719 tree friend_type = t;
10720 bool adjust_processing_template_decl = false;
10722 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10724 /* template <class T> friend class C; */
10725 friend_type = tsubst_friend_class (friend_type, args);
10726 adjust_processing_template_decl = true;
10728 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
10730 /* template <class T> friend class C::D; */
10731 friend_type = tsubst (friend_type, args,
10732 tf_warning_or_error, NULL_TREE);
10733 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10734 friend_type = TREE_TYPE (friend_type);
10735 adjust_processing_template_decl = true;
10737 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
10738 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
10740 /* This could be either
10742 friend class T::C;
10744 when dependent_type_p is false or
10746 template <class U> friend class T::C;
10748 otherwise. */
10749 /* Bump processing_template_decl in case this is something like
10750 template <class T> friend struct A<T>::B. */
10751 ++processing_template_decl;
10752 friend_type = tsubst (friend_type, args,
10753 tf_warning_or_error, NULL_TREE);
10754 if (dependent_type_p (friend_type))
10755 adjust_processing_template_decl = true;
10756 --processing_template_decl;
10758 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
10759 && TYPE_HIDDEN_P (friend_type))
10761 /* friend class C;
10763 where C hasn't been declared yet. Let's lookup name
10764 from namespace scope directly, bypassing any name that
10765 come from dependent base class. */
10766 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
10768 /* The call to xref_tag_from_type does injection for friend
10769 classes. */
10770 push_nested_namespace (ns);
10771 friend_type =
10772 xref_tag_from_type (friend_type, NULL_TREE,
10773 /*tag_scope=*/ts_current);
10774 pop_nested_namespace (ns);
10776 else if (uses_template_parms (friend_type))
10777 /* friend class C<T>; */
10778 friend_type = tsubst (friend_type, args,
10779 tf_warning_or_error, NULL_TREE);
10780 /* Otherwise it's
10782 friend class C;
10784 where C is already declared or
10786 friend class C<int>;
10788 We don't have to do anything in these cases. */
10790 if (adjust_processing_template_decl)
10791 /* Trick make_friend_class into realizing that the friend
10792 we're adding is a template, not an ordinary class. It's
10793 important that we use make_friend_class since it will
10794 perform some error-checking and output cross-reference
10795 information. */
10796 ++processing_template_decl;
10798 if (friend_type != error_mark_node)
10799 make_friend_class (type, friend_type, /*complain=*/false);
10801 if (adjust_processing_template_decl)
10802 --processing_template_decl;
10804 else
10806 /* Build new DECL_FRIENDLIST. */
10807 tree r;
10809 /* The file and line for this declaration, to
10810 assist in error message reporting. Since we
10811 called push_tinst_level above, we don't need to
10812 restore these. */
10813 input_location = DECL_SOURCE_LOCATION (t);
10815 if (TREE_CODE (t) == TEMPLATE_DECL)
10817 ++processing_template_decl;
10818 push_deferring_access_checks (dk_no_check);
10821 r = tsubst_friend_function (t, args);
10822 add_friend (type, r, /*complain=*/false);
10823 if (TREE_CODE (t) == TEMPLATE_DECL)
10825 pop_deferring_access_checks ();
10826 --processing_template_decl;
10832 if (fn_context)
10834 /* Restore these before substituting into the lambda capture
10835 initializers. */
10836 cp_unevaluated_operand = saved_unevaluated_operand;
10837 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10840 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
10842 tree decl = lambda_function (type);
10843 if (decl)
10845 if (cxx_dialect >= cxx1z)
10846 CLASSTYPE_LITERAL_P (type) = true;
10848 if (!DECL_TEMPLATE_INFO (decl)
10849 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
10851 /* Set function_depth to avoid garbage collection. */
10852 ++function_depth;
10853 instantiate_decl (decl, /*defer_ok=*/false, false);
10854 --function_depth;
10857 /* We need to instantiate the capture list from the template
10858 after we've instantiated the closure members, but before we
10859 consider adding the conversion op. Also keep any captures
10860 that may have been added during instantiation of the op(). */
10861 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
10862 tree tmpl_cap
10863 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
10864 args, tf_warning_or_error, NULL_TREE,
10865 false, false);
10867 LAMBDA_EXPR_CAPTURE_LIST (expr)
10868 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
10870 maybe_add_lambda_conv_op (type);
10872 else
10873 gcc_assert (errorcount);
10876 /* Set the file and line number information to whatever is given for
10877 the class itself. This puts error messages involving generated
10878 implicit functions at a predictable point, and the same point
10879 that would be used for non-template classes. */
10880 input_location = DECL_SOURCE_LOCATION (typedecl);
10882 unreverse_member_declarations (type);
10883 finish_struct_1 (type);
10884 TYPE_BEING_DEFINED (type) = 0;
10886 /* We don't instantiate default arguments for member functions. 14.7.1:
10888 The implicit instantiation of a class template specialization causes
10889 the implicit instantiation of the declarations, but not of the
10890 definitions or default arguments, of the class member functions,
10891 member classes, static data members and member templates.... */
10893 /* Some typedefs referenced from within the template code need to be access
10894 checked at template instantiation time, i.e now. These types were
10895 added to the template at parsing time. Let's get those and perform
10896 the access checks then. */
10897 perform_typedefs_access_check (pattern, args);
10898 perform_deferred_access_checks (tf_warning_or_error);
10899 pop_nested_class ();
10900 maximum_field_alignment = saved_maximum_field_alignment;
10901 if (!fn_context)
10902 pop_from_top_level ();
10903 pop_deferring_access_checks ();
10904 pop_tinst_level ();
10906 /* The vtable for a template class can be emitted in any translation
10907 unit in which the class is instantiated. When there is no key
10908 method, however, finish_struct_1 will already have added TYPE to
10909 the keyed_classes. */
10910 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
10911 vec_safe_push (keyed_classes, type);
10913 return type;
10916 /* Wrapper for instantiate_class_template_1. */
10918 tree
10919 instantiate_class_template (tree type)
10921 tree ret;
10922 timevar_push (TV_TEMPLATE_INST);
10923 ret = instantiate_class_template_1 (type);
10924 timevar_pop (TV_TEMPLATE_INST);
10925 return ret;
10928 static tree
10929 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10931 tree r;
10933 if (!t)
10934 r = t;
10935 else if (TYPE_P (t))
10936 r = tsubst (t, args, complain, in_decl);
10937 else
10939 if (!(complain & tf_warning))
10940 ++c_inhibit_evaluation_warnings;
10941 r = tsubst_expr (t, args, complain, in_decl,
10942 /*integral_constant_expression_p=*/true);
10943 if (!(complain & tf_warning))
10944 --c_inhibit_evaluation_warnings;
10946 return r;
10949 /* Given a function parameter pack TMPL_PARM and some function parameters
10950 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
10951 and set *SPEC_P to point at the next point in the list. */
10953 tree
10954 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
10956 /* Collect all of the extra "packed" parameters into an
10957 argument pack. */
10958 tree parmvec;
10959 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
10960 tree spec_parm = *spec_p;
10961 int i, len;
10963 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
10964 if (tmpl_parm
10965 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
10966 break;
10968 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
10969 parmvec = make_tree_vec (len);
10970 spec_parm = *spec_p;
10971 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
10972 TREE_VEC_ELT (parmvec, i) = spec_parm;
10974 /* Build the argument packs. */
10975 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
10976 *spec_p = spec_parm;
10978 return argpack;
10981 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
10982 NONTYPE_ARGUMENT_PACK. */
10984 static tree
10985 make_fnparm_pack (tree spec_parm)
10987 return extract_fnparm_pack (NULL_TREE, &spec_parm);
10990 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
10991 pack expansion with no extra args, 2 if it has extra args, or 0
10992 if it is not a pack expansion. */
10994 static int
10995 argument_pack_element_is_expansion_p (tree arg_pack, int i)
10997 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
10998 if (i >= TREE_VEC_LENGTH (vec))
10999 return 0;
11000 tree elt = TREE_VEC_ELT (vec, i);
11001 if (DECL_P (elt))
11002 /* A decl pack is itself an expansion. */
11003 elt = TREE_TYPE (elt);
11004 if (!PACK_EXPANSION_P (elt))
11005 return 0;
11006 if (PACK_EXPANSION_EXTRA_ARGS (elt))
11007 return 2;
11008 return 1;
11012 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11014 static tree
11015 make_argument_pack_select (tree arg_pack, unsigned index)
11017 tree aps = make_node (ARGUMENT_PACK_SELECT);
11019 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
11020 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11022 return aps;
11025 /* This is a subroutine of tsubst_pack_expansion.
11027 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11028 mechanism to store the (non complete list of) arguments of the
11029 substitution and return a non substituted pack expansion, in order
11030 to wait for when we have enough arguments to really perform the
11031 substitution. */
11033 static bool
11034 use_pack_expansion_extra_args_p (tree parm_packs,
11035 int arg_pack_len,
11036 bool has_empty_arg)
11038 /* If one pack has an expansion and another pack has a normal
11039 argument or if one pack has an empty argument and an another
11040 one hasn't then tsubst_pack_expansion cannot perform the
11041 substitution and need to fall back on the
11042 PACK_EXPANSION_EXTRA mechanism. */
11043 if (parm_packs == NULL_TREE)
11044 return false;
11045 else if (has_empty_arg)
11046 return true;
11048 bool has_expansion_arg = false;
11049 for (int i = 0 ; i < arg_pack_len; ++i)
11051 bool has_non_expansion_arg = false;
11052 for (tree parm_pack = parm_packs;
11053 parm_pack;
11054 parm_pack = TREE_CHAIN (parm_pack))
11056 tree arg = TREE_VALUE (parm_pack);
11058 int exp = argument_pack_element_is_expansion_p (arg, i);
11059 if (exp == 2)
11060 /* We can't substitute a pack expansion with extra args into
11061 our pattern. */
11062 return true;
11063 else if (exp)
11064 has_expansion_arg = true;
11065 else
11066 has_non_expansion_arg = true;
11069 if (has_expansion_arg && has_non_expansion_arg)
11070 return true;
11072 return false;
11075 /* [temp.variadic]/6 says that:
11077 The instantiation of a pack expansion [...]
11078 produces a list E1,E2, ..., En, where N is the number of elements
11079 in the pack expansion parameters.
11081 This subroutine of tsubst_pack_expansion produces one of these Ei.
11083 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11084 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11085 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11086 INDEX is the index 'i' of the element Ei to produce. ARGS,
11087 COMPLAIN, and IN_DECL are the same parameters as for the
11088 tsubst_pack_expansion function.
11090 The function returns the resulting Ei upon successful completion,
11091 or error_mark_node.
11093 Note that this function possibly modifies the ARGS parameter, so
11094 it's the responsibility of the caller to restore it. */
11096 static tree
11097 gen_elem_of_pack_expansion_instantiation (tree pattern,
11098 tree parm_packs,
11099 unsigned index,
11100 tree args /* This parm gets
11101 modified. */,
11102 tsubst_flags_t complain,
11103 tree in_decl)
11105 tree t;
11106 bool ith_elem_is_expansion = false;
11108 /* For each parameter pack, change the substitution of the parameter
11109 pack to the ith argument in its argument pack, then expand the
11110 pattern. */
11111 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
11113 tree parm = TREE_PURPOSE (pack);
11114 tree arg_pack = TREE_VALUE (pack);
11115 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
11117 ith_elem_is_expansion |=
11118 argument_pack_element_is_expansion_p (arg_pack, index);
11120 /* Select the Ith argument from the pack. */
11121 if (TREE_CODE (parm) == PARM_DECL
11122 || TREE_CODE (parm) == FIELD_DECL)
11124 if (index == 0)
11126 aps = make_argument_pack_select (arg_pack, index);
11127 if (!mark_used (parm, complain) && !(complain & tf_error))
11128 return error_mark_node;
11129 register_local_specialization (aps, parm);
11131 else
11132 aps = retrieve_local_specialization (parm);
11134 else
11136 int idx, level;
11137 template_parm_level_and_index (parm, &level, &idx);
11139 if (index == 0)
11141 aps = make_argument_pack_select (arg_pack, index);
11142 /* Update the corresponding argument. */
11143 TMPL_ARG (args, level, idx) = aps;
11145 else
11146 /* Re-use the ARGUMENT_PACK_SELECT. */
11147 aps = TMPL_ARG (args, level, idx);
11149 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11152 /* Substitute into the PATTERN with the (possibly altered)
11153 arguments. */
11154 if (pattern == in_decl)
11155 /* Expanding a fixed parameter pack from
11156 coerce_template_parameter_pack. */
11157 t = tsubst_decl (pattern, args, complain);
11158 else if (pattern == error_mark_node)
11159 t = error_mark_node;
11160 else if (constraint_p (pattern))
11162 if (processing_template_decl)
11163 t = tsubst_constraint (pattern, args, complain, in_decl);
11164 else
11165 t = (constraints_satisfied_p (pattern, args)
11166 ? boolean_true_node : boolean_false_node);
11168 else if (!TYPE_P (pattern))
11169 t = tsubst_expr (pattern, args, complain, in_decl,
11170 /*integral_constant_expression_p=*/false);
11171 else
11172 t = tsubst (pattern, args, complain, in_decl);
11174 /* If the Ith argument pack element is a pack expansion, then
11175 the Ith element resulting from the substituting is going to
11176 be a pack expansion as well. */
11177 if (ith_elem_is_expansion)
11178 t = make_pack_expansion (t);
11180 return t;
11183 /* When the unexpanded parameter pack in a fold expression expands to an empty
11184 sequence, the value of the expression is as follows; the program is
11185 ill-formed if the operator is not listed in this table.
11187 && true
11188 || false
11189 , void() */
11191 tree
11192 expand_empty_fold (tree t, tsubst_flags_t complain)
11194 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
11195 if (!FOLD_EXPR_MODIFY_P (t))
11196 switch (code)
11198 case TRUTH_ANDIF_EXPR:
11199 return boolean_true_node;
11200 case TRUTH_ORIF_EXPR:
11201 return boolean_false_node;
11202 case COMPOUND_EXPR:
11203 return void_node;
11204 default:
11205 break;
11208 if (complain & tf_error)
11209 error_at (location_of (t),
11210 "fold of empty expansion over %O", code);
11211 return error_mark_node;
11214 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11215 form an expression that combines the two terms using the
11216 operator of T. */
11218 static tree
11219 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
11221 tree op = FOLD_EXPR_OP (t);
11222 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
11224 // Handle compound assignment operators.
11225 if (FOLD_EXPR_MODIFY_P (t))
11226 return build_x_modify_expr (input_location, left, code, right, complain);
11228 switch (code)
11230 case COMPOUND_EXPR:
11231 return build_x_compound_expr (input_location, left, right, complain);
11232 case DOTSTAR_EXPR:
11233 return build_m_component_ref (left, right, complain);
11234 default:
11235 return build_x_binary_op (input_location, code,
11236 left, TREE_CODE (left),
11237 right, TREE_CODE (right),
11238 /*overload=*/NULL,
11239 complain);
11243 /* Substitute ARGS into the pack of a fold expression T. */
11245 static inline tree
11246 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11248 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
11251 /* Substitute ARGS into the pack of a fold expression T. */
11253 static inline tree
11254 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11256 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
11259 /* Expand a PACK of arguments into a grouped as left fold.
11260 Given a pack containing elements A0, A1, ..., An and an
11261 operator @, this builds the expression:
11263 ((A0 @ A1) @ A2) ... @ An
11265 Note that PACK must not be empty.
11267 The operator is defined by the original fold expression T. */
11269 static tree
11270 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
11272 tree left = TREE_VEC_ELT (pack, 0);
11273 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
11275 tree right = TREE_VEC_ELT (pack, i);
11276 left = fold_expression (t, left, right, complain);
11278 return left;
11281 /* Substitute into a unary left fold expression. */
11283 static tree
11284 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
11285 tree in_decl)
11287 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11288 if (pack == error_mark_node)
11289 return error_mark_node;
11290 if (PACK_EXPANSION_P (pack))
11292 tree r = copy_node (t);
11293 FOLD_EXPR_PACK (r) = pack;
11294 return r;
11296 if (TREE_VEC_LENGTH (pack) == 0)
11297 return expand_empty_fold (t, complain);
11298 else
11299 return expand_left_fold (t, pack, complain);
11302 /* Substitute into a binary left fold expression.
11304 Do ths by building a single (non-empty) vector of argumnts and
11305 building the expression from those elements. */
11307 static tree
11308 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
11309 tree in_decl)
11311 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11312 if (pack == error_mark_node)
11313 return error_mark_node;
11314 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11315 if (init == error_mark_node)
11316 return error_mark_node;
11318 if (PACK_EXPANSION_P (pack))
11320 tree r = copy_node (t);
11321 FOLD_EXPR_PACK (r) = pack;
11322 FOLD_EXPR_INIT (r) = init;
11323 return r;
11326 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
11327 TREE_VEC_ELT (vec, 0) = init;
11328 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
11329 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
11331 return expand_left_fold (t, vec, complain);
11334 /* Expand a PACK of arguments into a grouped as right fold.
11335 Given a pack containing elementns A0, A1, ..., and an
11336 operator @, this builds the expression:
11338 A0@ ... (An-2 @ (An-1 @ An))
11340 Note that PACK must not be empty.
11342 The operator is defined by the original fold expression T. */
11344 tree
11345 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
11347 // Build the expression.
11348 int n = TREE_VEC_LENGTH (pack);
11349 tree right = TREE_VEC_ELT (pack, n - 1);
11350 for (--n; n != 0; --n)
11352 tree left = TREE_VEC_ELT (pack, n - 1);
11353 right = fold_expression (t, left, right, complain);
11355 return right;
11358 /* Substitute into a unary right fold expression. */
11360 static tree
11361 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
11362 tree in_decl)
11364 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11365 if (pack == error_mark_node)
11366 return error_mark_node;
11367 if (PACK_EXPANSION_P (pack))
11369 tree r = copy_node (t);
11370 FOLD_EXPR_PACK (r) = pack;
11371 return r;
11373 if (TREE_VEC_LENGTH (pack) == 0)
11374 return expand_empty_fold (t, complain);
11375 else
11376 return expand_right_fold (t, pack, complain);
11379 /* Substitute into a binary right fold expression.
11381 Do ths by building a single (non-empty) vector of arguments and
11382 building the expression from those elements. */
11384 static tree
11385 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
11386 tree in_decl)
11388 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11389 if (pack == error_mark_node)
11390 return error_mark_node;
11391 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11392 if (init == error_mark_node)
11393 return error_mark_node;
11395 if (PACK_EXPANSION_P (pack))
11397 tree r = copy_node (t);
11398 FOLD_EXPR_PACK (r) = pack;
11399 FOLD_EXPR_INIT (r) = init;
11400 return r;
11403 int n = TREE_VEC_LENGTH (pack);
11404 tree vec = make_tree_vec (n + 1);
11405 for (int i = 0; i < n; ++i)
11406 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
11407 TREE_VEC_ELT (vec, n) = init;
11409 return expand_right_fold (t, vec, complain);
11413 /* Substitute ARGS into T, which is an pack expansion
11414 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11415 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11416 (if only a partial substitution could be performed) or
11417 ERROR_MARK_NODE if there was an error. */
11418 tree
11419 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
11420 tree in_decl)
11422 tree pattern;
11423 tree pack, packs = NULL_TREE;
11424 bool unsubstituted_packs = false;
11425 int i, len = -1;
11426 tree result;
11427 hash_map<tree, tree> *saved_local_specializations = NULL;
11428 bool need_local_specializations = false;
11429 int levels;
11431 gcc_assert (PACK_EXPANSION_P (t));
11432 pattern = PACK_EXPANSION_PATTERN (t);
11434 /* Add in any args remembered from an earlier partial instantiation. */
11435 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
11437 levels = TMPL_ARGS_DEPTH (args);
11439 /* Determine the argument packs that will instantiate the parameter
11440 packs used in the expansion expression. While we're at it,
11441 compute the number of arguments to be expanded and make sure it
11442 is consistent. */
11443 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
11444 pack = TREE_CHAIN (pack))
11446 tree parm_pack = TREE_VALUE (pack);
11447 tree arg_pack = NULL_TREE;
11448 tree orig_arg = NULL_TREE;
11449 int level = 0;
11451 if (TREE_CODE (parm_pack) == BASES)
11453 gcc_assert (parm_pack == pattern);
11454 if (BASES_DIRECT (parm_pack))
11455 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
11456 args, complain, in_decl, false));
11457 else
11458 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
11459 args, complain, in_decl, false));
11461 else if (builtin_pack_call_p (parm_pack))
11463 /* ??? Support use in other patterns. */
11464 gcc_assert (parm_pack == pattern);
11465 return expand_builtin_pack_call (parm_pack, args,
11466 complain, in_decl);
11468 else if (TREE_CODE (parm_pack) == PARM_DECL)
11470 /* We know we have correct local_specializations if this
11471 expansion is at function scope, or if we're dealing with a
11472 local parameter in a requires expression; for the latter,
11473 tsubst_requires_expr set it up appropriately. */
11474 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
11475 arg_pack = retrieve_local_specialization (parm_pack);
11476 else
11477 /* We can't rely on local_specializations for a parameter
11478 name used later in a function declaration (such as in a
11479 late-specified return type). Even if it exists, it might
11480 have the wrong value for a recursive call. */
11481 need_local_specializations = true;
11483 if (!arg_pack)
11485 /* This parameter pack was used in an unevaluated context. Just
11486 make a dummy decl, since it's only used for its type. */
11487 arg_pack = tsubst_decl (parm_pack, args, complain);
11488 if (arg_pack && DECL_PACK_P (arg_pack))
11489 /* Partial instantiation of the parm_pack, we can't build
11490 up an argument pack yet. */
11491 arg_pack = NULL_TREE;
11492 else
11493 arg_pack = make_fnparm_pack (arg_pack);
11496 else if (TREE_CODE (parm_pack) == FIELD_DECL)
11497 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
11498 else
11500 int idx;
11501 template_parm_level_and_index (parm_pack, &level, &idx);
11503 if (level <= levels)
11504 arg_pack = TMPL_ARG (args, level, idx);
11507 orig_arg = arg_pack;
11508 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
11509 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
11511 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
11512 /* This can only happen if we forget to expand an argument
11513 pack somewhere else. Just return an error, silently. */
11515 result = make_tree_vec (1);
11516 TREE_VEC_ELT (result, 0) = error_mark_node;
11517 return result;
11520 if (arg_pack)
11522 int my_len =
11523 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
11525 /* Don't bother trying to do a partial substitution with
11526 incomplete packs; we'll try again after deduction. */
11527 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
11528 return t;
11530 if (len < 0)
11531 len = my_len;
11532 else if (len != my_len)
11534 if (!(complain & tf_error))
11535 /* Fail quietly. */;
11536 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
11537 error ("mismatched argument pack lengths while expanding %qT",
11538 pattern);
11539 else
11540 error ("mismatched argument pack lengths while expanding %qE",
11541 pattern);
11542 return error_mark_node;
11545 /* Keep track of the parameter packs and their corresponding
11546 argument packs. */
11547 packs = tree_cons (parm_pack, arg_pack, packs);
11548 TREE_TYPE (packs) = orig_arg;
11550 else
11552 /* We can't substitute for this parameter pack. We use a flag as
11553 well as the missing_level counter because function parameter
11554 packs don't have a level. */
11555 gcc_assert (processing_template_decl);
11556 unsubstituted_packs = true;
11560 /* If the expansion is just T..., return the matching argument pack, unless
11561 we need to call convert_from_reference on all the elements. This is an
11562 important optimization; see c++/68422. */
11563 if (!unsubstituted_packs
11564 && TREE_PURPOSE (packs) == pattern)
11566 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
11567 /* Types need no adjustment, nor does sizeof..., and if we still have
11568 some pack expansion args we won't do anything yet. */
11569 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
11570 || PACK_EXPANSION_SIZEOF_P (t)
11571 || pack_expansion_args_count (args))
11572 return args;
11573 /* Also optimize expression pack expansions if we can tell that the
11574 elements won't have reference type. */
11575 tree type = TREE_TYPE (pattern);
11576 if (type && TREE_CODE (type) != REFERENCE_TYPE
11577 && !PACK_EXPANSION_P (type)
11578 && !WILDCARD_TYPE_P (type))
11579 return args;
11580 /* Otherwise use the normal path so we get convert_from_reference. */
11583 /* We cannot expand this expansion expression, because we don't have
11584 all of the argument packs we need. */
11585 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
11587 /* We got some full packs, but we can't substitute them in until we
11588 have values for all the packs. So remember these until then. */
11590 t = make_pack_expansion (pattern);
11591 PACK_EXPANSION_EXTRA_ARGS (t) = args;
11592 return t;
11594 else if (unsubstituted_packs)
11596 /* There were no real arguments, we're just replacing a parameter
11597 pack with another version of itself. Substitute into the
11598 pattern and return a PACK_EXPANSION_*. The caller will need to
11599 deal with that. */
11600 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
11601 t = tsubst_expr (pattern, args, complain, in_decl,
11602 /*integral_constant_expression_p=*/false);
11603 else
11604 t = tsubst (pattern, args, complain, in_decl);
11605 t = make_pack_expansion (t);
11606 return t;
11609 gcc_assert (len >= 0);
11611 if (need_local_specializations)
11613 /* We're in a late-specified return type, so create our own local
11614 specializations map; the current map is either NULL or (in the
11615 case of recursive unification) might have bindings that we don't
11616 want to use or alter. */
11617 saved_local_specializations = local_specializations;
11618 local_specializations = new hash_map<tree, tree>;
11621 /* For each argument in each argument pack, substitute into the
11622 pattern. */
11623 result = make_tree_vec (len);
11624 tree elem_args = copy_template_args (args);
11625 for (i = 0; i < len; ++i)
11627 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
11629 elem_args, complain,
11630 in_decl);
11631 TREE_VEC_ELT (result, i) = t;
11632 if (t == error_mark_node)
11634 result = error_mark_node;
11635 break;
11639 /* Update ARGS to restore the substitution from parameter packs to
11640 their argument packs. */
11641 for (pack = packs; pack; pack = TREE_CHAIN (pack))
11643 tree parm = TREE_PURPOSE (pack);
11645 if (TREE_CODE (parm) == PARM_DECL
11646 || TREE_CODE (parm) == FIELD_DECL)
11647 register_local_specialization (TREE_TYPE (pack), parm);
11648 else
11650 int idx, level;
11652 if (TREE_VALUE (pack) == NULL_TREE)
11653 continue;
11655 template_parm_level_and_index (parm, &level, &idx);
11657 /* Update the corresponding argument. */
11658 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
11659 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
11660 TREE_TYPE (pack);
11661 else
11662 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
11666 if (need_local_specializations)
11668 delete local_specializations;
11669 local_specializations = saved_local_specializations;
11672 /* If the dependent pack arguments were such that we end up with only a
11673 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11674 if (len == 1 && TREE_CODE (result) == TREE_VEC
11675 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
11676 return TREE_VEC_ELT (result, 0);
11678 return result;
11681 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11682 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11683 parameter packs; all parms generated from a function parameter pack will
11684 have the same DECL_PARM_INDEX. */
11686 tree
11687 get_pattern_parm (tree parm, tree tmpl)
11689 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
11690 tree patparm;
11692 if (DECL_ARTIFICIAL (parm))
11694 for (patparm = DECL_ARGUMENTS (pattern);
11695 patparm; patparm = DECL_CHAIN (patparm))
11696 if (DECL_ARTIFICIAL (patparm)
11697 && DECL_NAME (parm) == DECL_NAME (patparm))
11698 break;
11700 else
11702 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
11703 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
11704 gcc_assert (DECL_PARM_INDEX (patparm)
11705 == DECL_PARM_INDEX (parm));
11708 return patparm;
11711 /* Make an argument pack out of the TREE_VEC VEC. */
11713 static tree
11714 make_argument_pack (tree vec)
11716 tree pack;
11717 tree elt = TREE_VEC_ELT (vec, 0);
11718 if (TYPE_P (elt))
11719 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
11720 else
11722 pack = make_node (NONTYPE_ARGUMENT_PACK);
11723 TREE_CONSTANT (pack) = 1;
11725 SET_ARGUMENT_PACK_ARGS (pack, vec);
11726 return pack;
11729 /* Return an exact copy of template args T that can be modified
11730 independently. */
11732 static tree
11733 copy_template_args (tree t)
11735 if (t == error_mark_node)
11736 return t;
11738 int len = TREE_VEC_LENGTH (t);
11739 tree new_vec = make_tree_vec (len);
11741 for (int i = 0; i < len; ++i)
11743 tree elt = TREE_VEC_ELT (t, i);
11744 if (elt && TREE_CODE (elt) == TREE_VEC)
11745 elt = copy_template_args (elt);
11746 TREE_VEC_ELT (new_vec, i) = elt;
11749 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
11750 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
11752 return new_vec;
11755 /* Substitute ARGS into the vector or list of template arguments T. */
11757 static tree
11758 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11760 tree orig_t = t;
11761 int len, need_new = 0, i, expanded_len_adjust = 0, out;
11762 tree *elts;
11764 if (t == error_mark_node)
11765 return error_mark_node;
11767 len = TREE_VEC_LENGTH (t);
11768 elts = XALLOCAVEC (tree, len);
11770 for (i = 0; i < len; i++)
11772 tree orig_arg = TREE_VEC_ELT (t, i);
11773 tree new_arg;
11775 if (TREE_CODE (orig_arg) == TREE_VEC)
11776 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
11777 else if (PACK_EXPANSION_P (orig_arg))
11779 /* Substitute into an expansion expression. */
11780 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
11782 if (TREE_CODE (new_arg) == TREE_VEC)
11783 /* Add to the expanded length adjustment the number of
11784 expanded arguments. We subtract one from this
11785 measurement, because the argument pack expression
11786 itself is already counted as 1 in
11787 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
11788 the argument pack is empty. */
11789 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
11791 else if (ARGUMENT_PACK_P (orig_arg))
11793 /* Substitute into each of the arguments. */
11794 new_arg = TYPE_P (orig_arg)
11795 ? cxx_make_type (TREE_CODE (orig_arg))
11796 : make_node (TREE_CODE (orig_arg));
11798 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
11799 args, complain, in_decl);
11800 if (pack_args == error_mark_node)
11801 new_arg = error_mark_node;
11802 else
11803 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
11805 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
11806 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
11808 else
11809 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
11811 if (new_arg == error_mark_node)
11812 return error_mark_node;
11814 elts[i] = new_arg;
11815 if (new_arg != orig_arg)
11816 need_new = 1;
11819 if (!need_new)
11820 return t;
11822 /* Make space for the expanded arguments coming from template
11823 argument packs. */
11824 t = make_tree_vec (len + expanded_len_adjust);
11825 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
11826 arguments for a member template.
11827 In that case each TREE_VEC in ORIG_T represents a level of template
11828 arguments, and ORIG_T won't carry any non defaulted argument count.
11829 It will rather be the nested TREE_VECs that will carry one.
11830 In other words, ORIG_T carries a non defaulted argument count only
11831 if it doesn't contain any nested TREE_VEC. */
11832 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
11834 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
11835 count += expanded_len_adjust;
11836 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
11838 for (i = 0, out = 0; i < len; i++)
11840 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
11841 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
11842 && TREE_CODE (elts[i]) == TREE_VEC)
11844 int idx;
11846 /* Now expand the template argument pack "in place". */
11847 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
11848 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
11850 else
11852 TREE_VEC_ELT (t, out) = elts[i];
11853 out++;
11857 return t;
11860 /* Substitute ARGS into one level PARMS of template parameters. */
11862 static tree
11863 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
11865 if (parms == error_mark_node)
11866 return error_mark_node;
11868 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
11870 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
11872 tree tuple = TREE_VEC_ELT (parms, i);
11874 if (tuple == error_mark_node)
11875 continue;
11877 TREE_VEC_ELT (new_vec, i) =
11878 tsubst_template_parm (tuple, args, complain);
11881 return new_vec;
11884 /* Return the result of substituting ARGS into the template parameters
11885 given by PARMS. If there are m levels of ARGS and m + n levels of
11886 PARMS, then the result will contain n levels of PARMS. For
11887 example, if PARMS is `template <class T> template <class U>
11888 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
11889 result will be `template <int*, double, class V>'. */
11891 static tree
11892 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
11894 tree r = NULL_TREE;
11895 tree* new_parms;
11897 /* When substituting into a template, we must set
11898 PROCESSING_TEMPLATE_DECL as the template parameters may be
11899 dependent if they are based on one-another, and the dependency
11900 predicates are short-circuit outside of templates. */
11901 ++processing_template_decl;
11903 for (new_parms = &r;
11904 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
11905 new_parms = &(TREE_CHAIN (*new_parms)),
11906 parms = TREE_CHAIN (parms))
11908 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
11909 args, complain);
11910 *new_parms =
11911 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
11912 - TMPL_ARGS_DEPTH (args)),
11913 new_vec, NULL_TREE);
11916 --processing_template_decl;
11918 return r;
11921 /* Return the result of substituting ARGS into one template parameter
11922 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
11923 parameter and which TREE_PURPOSE is the default argument of the
11924 template parameter. */
11926 static tree
11927 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
11929 tree default_value, parm_decl;
11931 if (args == NULL_TREE
11932 || t == NULL_TREE
11933 || t == error_mark_node)
11934 return t;
11936 gcc_assert (TREE_CODE (t) == TREE_LIST);
11938 default_value = TREE_PURPOSE (t);
11939 parm_decl = TREE_VALUE (t);
11941 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
11942 if (TREE_CODE (parm_decl) == PARM_DECL
11943 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
11944 parm_decl = error_mark_node;
11945 default_value = tsubst_template_arg (default_value, args,
11946 complain, NULL_TREE);
11948 return build_tree_list (default_value, parm_decl);
11951 /* Substitute the ARGS into the indicated aggregate (or enumeration)
11952 type T. If T is not an aggregate or enumeration type, it is
11953 handled as if by tsubst. IN_DECL is as for tsubst. If
11954 ENTERING_SCOPE is nonzero, T is the context for a template which
11955 we are presently tsubst'ing. Return the substituted value. */
11957 static tree
11958 tsubst_aggr_type (tree t,
11959 tree args,
11960 tsubst_flags_t complain,
11961 tree in_decl,
11962 int entering_scope)
11964 if (t == NULL_TREE)
11965 return NULL_TREE;
11967 switch (TREE_CODE (t))
11969 case RECORD_TYPE:
11970 if (TYPE_PTRMEMFUNC_P (t))
11971 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
11973 /* Fall through. */
11974 case ENUMERAL_TYPE:
11975 case UNION_TYPE:
11976 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
11978 tree argvec;
11979 tree context;
11980 tree r;
11981 int saved_unevaluated_operand;
11982 int saved_inhibit_evaluation_warnings;
11984 /* In "sizeof(X<I>)" we need to evaluate "I". */
11985 saved_unevaluated_operand = cp_unevaluated_operand;
11986 cp_unevaluated_operand = 0;
11987 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11988 c_inhibit_evaluation_warnings = 0;
11990 /* First, determine the context for the type we are looking
11991 up. */
11992 context = TYPE_CONTEXT (t);
11993 if (context && TYPE_P (context))
11995 context = tsubst_aggr_type (context, args, complain,
11996 in_decl, /*entering_scope=*/1);
11997 /* If context is a nested class inside a class template,
11998 it may still need to be instantiated (c++/33959). */
11999 context = complete_type (context);
12002 /* Then, figure out what arguments are appropriate for the
12003 type we are trying to find. For example, given:
12005 template <class T> struct S;
12006 template <class T, class U> void f(T, U) { S<U> su; }
12008 and supposing that we are instantiating f<int, double>,
12009 then our ARGS will be {int, double}, but, when looking up
12010 S we only want {double}. */
12011 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
12012 complain, in_decl);
12013 if (argvec == error_mark_node)
12014 r = error_mark_node;
12015 else
12017 r = lookup_template_class (t, argvec, in_decl, context,
12018 entering_scope, complain);
12019 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
12022 cp_unevaluated_operand = saved_unevaluated_operand;
12023 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
12025 return r;
12027 else
12028 /* This is not a template type, so there's nothing to do. */
12029 return t;
12031 default:
12032 return tsubst (t, args, complain, in_decl);
12036 /* Substitute into the default argument ARG (a default argument for
12037 FN), which has the indicated TYPE. */
12039 tree
12040 tsubst_default_argument (tree fn, tree type, tree arg, tsubst_flags_t complain)
12042 tree saved_class_ptr = NULL_TREE;
12043 tree saved_class_ref = NULL_TREE;
12044 int errs = errorcount + sorrycount;
12046 /* This can happen in invalid code. */
12047 if (TREE_CODE (arg) == DEFAULT_ARG)
12048 return arg;
12050 /* This default argument came from a template. Instantiate the
12051 default argument here, not in tsubst. In the case of
12052 something like:
12054 template <class T>
12055 struct S {
12056 static T t();
12057 void f(T = t());
12060 we must be careful to do name lookup in the scope of S<T>,
12061 rather than in the current class. */
12062 push_access_scope (fn);
12063 /* The "this" pointer is not valid in a default argument. */
12064 if (cfun)
12066 saved_class_ptr = current_class_ptr;
12067 cp_function_chain->x_current_class_ptr = NULL_TREE;
12068 saved_class_ref = current_class_ref;
12069 cp_function_chain->x_current_class_ref = NULL_TREE;
12072 push_deferring_access_checks(dk_no_deferred);
12073 /* The default argument expression may cause implicitly defined
12074 member functions to be synthesized, which will result in garbage
12075 collection. We must treat this situation as if we were within
12076 the body of function so as to avoid collecting live data on the
12077 stack. */
12078 ++function_depth;
12079 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
12080 complain, NULL_TREE,
12081 /*integral_constant_expression_p=*/false);
12082 --function_depth;
12083 pop_deferring_access_checks();
12085 /* Restore the "this" pointer. */
12086 if (cfun)
12088 cp_function_chain->x_current_class_ptr = saved_class_ptr;
12089 cp_function_chain->x_current_class_ref = saved_class_ref;
12092 if (errorcount+sorrycount > errs
12093 && (complain & tf_warning_or_error))
12094 inform (input_location,
12095 " when instantiating default argument for call to %qD", fn);
12097 /* Make sure the default argument is reasonable. */
12098 arg = check_default_argument (type, arg, complain);
12100 pop_access_scope (fn);
12102 return arg;
12105 /* Substitute into all the default arguments for FN. */
12107 static void
12108 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
12110 tree arg;
12111 tree tmpl_args;
12113 tmpl_args = DECL_TI_ARGS (fn);
12115 /* If this function is not yet instantiated, we certainly don't need
12116 its default arguments. */
12117 if (uses_template_parms (tmpl_args))
12118 return;
12119 /* Don't do this again for clones. */
12120 if (DECL_CLONED_FUNCTION_P (fn))
12121 return;
12123 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
12124 arg;
12125 arg = TREE_CHAIN (arg))
12126 if (TREE_PURPOSE (arg))
12127 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
12128 TREE_VALUE (arg),
12129 TREE_PURPOSE (arg),
12130 complain);
12133 /* Substitute the ARGS into the T, which is a _DECL. Return the
12134 result of the substitution. Issue error and warning messages under
12135 control of COMPLAIN. */
12137 static tree
12138 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
12140 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12141 location_t saved_loc;
12142 tree r = NULL_TREE;
12143 tree in_decl = t;
12144 hashval_t hash = 0;
12146 /* Set the filename and linenumber to improve error-reporting. */
12147 saved_loc = input_location;
12148 input_location = DECL_SOURCE_LOCATION (t);
12150 switch (TREE_CODE (t))
12152 case TEMPLATE_DECL:
12154 /* We can get here when processing a member function template,
12155 member class template, or template template parameter. */
12156 tree decl = DECL_TEMPLATE_RESULT (t);
12157 tree spec;
12158 tree tmpl_args;
12159 tree full_args;
12161 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12163 /* Template template parameter is treated here. */
12164 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12165 if (new_type == error_mark_node)
12166 r = error_mark_node;
12167 /* If we get a real template back, return it. This can happen in
12168 the context of most_specialized_partial_spec. */
12169 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
12170 r = new_type;
12171 else
12172 /* The new TEMPLATE_DECL was built in
12173 reduce_template_parm_level. */
12174 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
12175 break;
12178 /* We might already have an instance of this template.
12179 The ARGS are for the surrounding class type, so the
12180 full args contain the tsubst'd args for the context,
12181 plus the innermost args from the template decl. */
12182 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
12183 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
12184 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
12185 /* Because this is a template, the arguments will still be
12186 dependent, even after substitution. If
12187 PROCESSING_TEMPLATE_DECL is not set, the dependency
12188 predicates will short-circuit. */
12189 ++processing_template_decl;
12190 full_args = tsubst_template_args (tmpl_args, args,
12191 complain, in_decl);
12192 --processing_template_decl;
12193 if (full_args == error_mark_node)
12194 RETURN (error_mark_node);
12196 /* If this is a default template template argument,
12197 tsubst might not have changed anything. */
12198 if (full_args == tmpl_args)
12199 RETURN (t);
12201 hash = hash_tmpl_and_args (t, full_args);
12202 spec = retrieve_specialization (t, full_args, hash);
12203 if (spec != NULL_TREE)
12205 r = spec;
12206 break;
12209 /* Make a new template decl. It will be similar to the
12210 original, but will record the current template arguments.
12211 We also create a new function declaration, which is just
12212 like the old one, but points to this new template, rather
12213 than the old one. */
12214 r = copy_decl (t);
12215 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
12216 DECL_CHAIN (r) = NULL_TREE;
12218 // Build new template info linking to the original template decl.
12219 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12221 if (TREE_CODE (decl) == TYPE_DECL
12222 && !TYPE_DECL_ALIAS_P (decl))
12224 tree new_type;
12225 ++processing_template_decl;
12226 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12227 --processing_template_decl;
12228 if (new_type == error_mark_node)
12229 RETURN (error_mark_node);
12231 TREE_TYPE (r) = new_type;
12232 /* For a partial specialization, we need to keep pointing to
12233 the primary template. */
12234 if (!DECL_TEMPLATE_SPECIALIZATION (t))
12235 CLASSTYPE_TI_TEMPLATE (new_type) = r;
12236 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
12237 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
12238 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
12240 else
12242 tree new_decl;
12243 ++processing_template_decl;
12244 new_decl = tsubst (decl, args, complain, in_decl);
12245 --processing_template_decl;
12246 if (new_decl == error_mark_node)
12247 RETURN (error_mark_node);
12249 DECL_TEMPLATE_RESULT (r) = new_decl;
12250 DECL_TI_TEMPLATE (new_decl) = r;
12251 TREE_TYPE (r) = TREE_TYPE (new_decl);
12252 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
12253 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
12256 SET_DECL_IMPLICIT_INSTANTIATION (r);
12257 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
12258 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
12260 /* The template parameters for this new template are all the
12261 template parameters for the old template, except the
12262 outermost level of parameters. */
12263 DECL_TEMPLATE_PARMS (r)
12264 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
12265 complain);
12267 if (PRIMARY_TEMPLATE_P (t))
12268 DECL_PRIMARY_TEMPLATE (r) = r;
12270 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl))
12271 /* Record this non-type partial instantiation. */
12272 register_specialization (r, t,
12273 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
12274 false, hash);
12276 break;
12278 case FUNCTION_DECL:
12280 tree ctx;
12281 tree argvec = NULL_TREE;
12282 tree *friends;
12283 tree gen_tmpl;
12284 tree type;
12285 int member;
12286 int args_depth;
12287 int parms_depth;
12289 /* Nobody should be tsubst'ing into non-template functions. */
12290 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
12292 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
12294 tree spec;
12296 /* If T is not dependent, just return it. */
12297 if (!uses_template_parms (DECL_TI_ARGS (t)))
12298 RETURN (t);
12300 /* Calculate the most general template of which R is a
12301 specialization, and the complete set of arguments used to
12302 specialize R. */
12303 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
12304 argvec = tsubst_template_args (DECL_TI_ARGS
12305 (DECL_TEMPLATE_RESULT
12306 (DECL_TI_TEMPLATE (t))),
12307 args, complain, in_decl);
12308 if (argvec == error_mark_node)
12309 RETURN (error_mark_node);
12311 /* Check to see if we already have this specialization. */
12312 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12313 spec = retrieve_specialization (gen_tmpl, argvec, hash);
12315 if (spec)
12317 r = spec;
12318 break;
12321 /* We can see more levels of arguments than parameters if
12322 there was a specialization of a member template, like
12323 this:
12325 template <class T> struct S { template <class U> void f(); }
12326 template <> template <class U> void S<int>::f(U);
12328 Here, we'll be substituting into the specialization,
12329 because that's where we can find the code we actually
12330 want to generate, but we'll have enough arguments for
12331 the most general template.
12333 We also deal with the peculiar case:
12335 template <class T> struct S {
12336 template <class U> friend void f();
12338 template <class U> void f() {}
12339 template S<int>;
12340 template void f<double>();
12342 Here, the ARGS for the instantiation of will be {int,
12343 double}. But, we only need as many ARGS as there are
12344 levels of template parameters in CODE_PATTERN. We are
12345 careful not to get fooled into reducing the ARGS in
12346 situations like:
12348 template <class T> struct S { template <class U> void f(U); }
12349 template <class T> template <> void S<T>::f(int) {}
12351 which we can spot because the pattern will be a
12352 specialization in this case. */
12353 args_depth = TMPL_ARGS_DEPTH (args);
12354 parms_depth =
12355 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
12356 if (args_depth > parms_depth
12357 && !DECL_TEMPLATE_SPECIALIZATION (t))
12358 args = get_innermost_template_args (args, parms_depth);
12360 else
12362 /* This special case arises when we have something like this:
12364 template <class T> struct S {
12365 friend void f<int>(int, double);
12368 Here, the DECL_TI_TEMPLATE for the friend declaration
12369 will be an IDENTIFIER_NODE. We are being called from
12370 tsubst_friend_function, and we want only to create a
12371 new decl (R) with appropriate types so that we can call
12372 determine_specialization. */
12373 gen_tmpl = NULL_TREE;
12376 if (DECL_CLASS_SCOPE_P (t))
12378 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
12379 member = 2;
12380 else
12381 member = 1;
12382 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
12383 complain, t, /*entering_scope=*/1);
12385 else
12387 member = 0;
12388 ctx = DECL_CONTEXT (t);
12390 type = tsubst (TREE_TYPE (t), args, complain|tf_fndecl_type, in_decl);
12391 if (type == error_mark_node)
12392 RETURN (error_mark_node);
12394 /* If we hit excessive deduction depth, the type is bogus even if
12395 it isn't error_mark_node, so don't build a decl. */
12396 if (excessive_deduction_depth)
12397 RETURN (error_mark_node);
12399 /* We do NOT check for matching decls pushed separately at this
12400 point, as they may not represent instantiations of this
12401 template, and in any case are considered separate under the
12402 discrete model. */
12403 r = copy_decl (t);
12404 DECL_USE_TEMPLATE (r) = 0;
12405 TREE_TYPE (r) = type;
12406 /* Clear out the mangled name and RTL for the instantiation. */
12407 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12408 SET_DECL_RTL (r, NULL);
12409 /* Leave DECL_INITIAL set on deleted instantiations. */
12410 if (!DECL_DELETED_FN (r))
12411 DECL_INITIAL (r) = NULL_TREE;
12412 DECL_CONTEXT (r) = ctx;
12414 /* OpenMP UDRs have the only argument a reference to the declared
12415 type. We want to diagnose if the declared type is a reference,
12416 which is invalid, but as references to references are usually
12417 quietly merged, diagnose it here. */
12418 if (DECL_OMP_DECLARE_REDUCTION_P (t))
12420 tree argtype
12421 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
12422 argtype = tsubst (argtype, args, complain, in_decl);
12423 if (TREE_CODE (argtype) == REFERENCE_TYPE)
12424 error_at (DECL_SOURCE_LOCATION (t),
12425 "reference type %qT in "
12426 "%<#pragma omp declare reduction%>", argtype);
12427 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
12428 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
12429 argtype);
12432 if (member && DECL_CONV_FN_P (r))
12433 /* Type-conversion operator. Reconstruct the name, in
12434 case it's the name of one of the template's parameters. */
12435 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
12437 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
12438 complain, t);
12439 DECL_RESULT (r) = NULL_TREE;
12441 TREE_STATIC (r) = 0;
12442 TREE_PUBLIC (r) = TREE_PUBLIC (t);
12443 DECL_EXTERNAL (r) = 1;
12444 /* If this is an instantiation of a function with internal
12445 linkage, we already know what object file linkage will be
12446 assigned to the instantiation. */
12447 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
12448 DECL_DEFER_OUTPUT (r) = 0;
12449 DECL_CHAIN (r) = NULL_TREE;
12450 DECL_PENDING_INLINE_INFO (r) = 0;
12451 DECL_PENDING_INLINE_P (r) = 0;
12452 DECL_SAVED_TREE (r) = NULL_TREE;
12453 DECL_STRUCT_FUNCTION (r) = NULL;
12454 TREE_USED (r) = 0;
12455 /* We'll re-clone as appropriate in instantiate_template. */
12456 DECL_CLONED_FUNCTION (r) = NULL_TREE;
12458 /* If we aren't complaining now, return on error before we register
12459 the specialization so that we'll complain eventually. */
12460 if ((complain & tf_error) == 0
12461 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12462 && !grok_op_properties (r, /*complain=*/false))
12463 RETURN (error_mark_node);
12465 /* When instantiating a constrained member, substitute
12466 into the constraints to create a new constraint. */
12467 if (tree ci = get_constraints (t))
12468 if (member)
12470 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
12471 set_constraints (r, ci);
12474 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12475 this in the special friend case mentioned above where
12476 GEN_TMPL is NULL. */
12477 if (gen_tmpl)
12479 DECL_TEMPLATE_INFO (r)
12480 = build_template_info (gen_tmpl, argvec);
12481 SET_DECL_IMPLICIT_INSTANTIATION (r);
12483 tree new_r
12484 = register_specialization (r, gen_tmpl, argvec, false, hash);
12485 if (new_r != r)
12486 /* We instantiated this while substituting into
12487 the type earlier (template/friend54.C). */
12488 RETURN (new_r);
12490 /* We're not supposed to instantiate default arguments
12491 until they are called, for a template. But, for a
12492 declaration like:
12494 template <class T> void f ()
12495 { extern void g(int i = T()); }
12497 we should do the substitution when the template is
12498 instantiated. We handle the member function case in
12499 instantiate_class_template since the default arguments
12500 might refer to other members of the class. */
12501 if (!member
12502 && !PRIMARY_TEMPLATE_P (gen_tmpl)
12503 && !uses_template_parms (argvec))
12504 tsubst_default_arguments (r, complain);
12506 else
12507 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12509 /* Copy the list of befriending classes. */
12510 for (friends = &DECL_BEFRIENDING_CLASSES (r);
12511 *friends;
12512 friends = &TREE_CHAIN (*friends))
12514 *friends = copy_node (*friends);
12515 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
12516 args, complain,
12517 in_decl);
12520 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
12522 maybe_retrofit_in_chrg (r);
12523 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
12524 RETURN (error_mark_node);
12525 /* If this is an instantiation of a member template, clone it.
12526 If it isn't, that'll be handled by
12527 clone_constructors_and_destructors. */
12528 if (PRIMARY_TEMPLATE_P (gen_tmpl))
12529 clone_function_decl (r, /*update_methods=*/false);
12531 else if ((complain & tf_error) != 0
12532 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12533 && !grok_op_properties (r, /*complain=*/true))
12534 RETURN (error_mark_node);
12536 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
12537 SET_DECL_FRIEND_CONTEXT (r,
12538 tsubst (DECL_FRIEND_CONTEXT (t),
12539 args, complain, in_decl));
12541 /* Possibly limit visibility based on template args. */
12542 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12543 if (DECL_VISIBILITY_SPECIFIED (t))
12545 DECL_VISIBILITY_SPECIFIED (r) = 0;
12546 DECL_ATTRIBUTES (r)
12547 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12549 determine_visibility (r);
12550 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
12551 && !processing_template_decl)
12552 defaulted_late_check (r);
12554 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12555 args, complain, in_decl);
12557 break;
12559 case PARM_DECL:
12561 tree type = NULL_TREE;
12562 int i, len = 1;
12563 tree expanded_types = NULL_TREE;
12564 tree prev_r = NULL_TREE;
12565 tree first_r = NULL_TREE;
12567 if (DECL_PACK_P (t))
12569 /* If there is a local specialization that isn't a
12570 parameter pack, it means that we're doing a "simple"
12571 substitution from inside tsubst_pack_expansion. Just
12572 return the local specialization (which will be a single
12573 parm). */
12574 tree spec = retrieve_local_specialization (t);
12575 if (spec
12576 && TREE_CODE (spec) == PARM_DECL
12577 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
12578 RETURN (spec);
12580 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12581 the parameters in this function parameter pack. */
12582 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12583 complain, in_decl);
12584 if (TREE_CODE (expanded_types) == TREE_VEC)
12586 len = TREE_VEC_LENGTH (expanded_types);
12588 /* Zero-length parameter packs are boring. Just substitute
12589 into the chain. */
12590 if (len == 0)
12591 RETURN (tsubst (TREE_CHAIN (t), args, complain,
12592 TREE_CHAIN (t)));
12594 else
12596 /* All we did was update the type. Make a note of that. */
12597 type = expanded_types;
12598 expanded_types = NULL_TREE;
12602 /* Loop through all of the parameters we'll build. When T is
12603 a function parameter pack, LEN is the number of expanded
12604 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12605 r = NULL_TREE;
12606 for (i = 0; i < len; ++i)
12608 prev_r = r;
12609 r = copy_node (t);
12610 if (DECL_TEMPLATE_PARM_P (t))
12611 SET_DECL_TEMPLATE_PARM_P (r);
12613 if (expanded_types)
12614 /* We're on the Ith parameter of the function parameter
12615 pack. */
12617 /* Get the Ith type. */
12618 type = TREE_VEC_ELT (expanded_types, i);
12620 /* Rename the parameter to include the index. */
12621 DECL_NAME (r)
12622 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12624 else if (!type)
12625 /* We're dealing with a normal parameter. */
12626 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12628 type = type_decays_to (type);
12629 TREE_TYPE (r) = type;
12630 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12632 if (DECL_INITIAL (r))
12634 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
12635 DECL_INITIAL (r) = TREE_TYPE (r);
12636 else
12637 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
12638 complain, in_decl);
12641 DECL_CONTEXT (r) = NULL_TREE;
12643 if (!DECL_TEMPLATE_PARM_P (r))
12644 DECL_ARG_TYPE (r) = type_passed_as (type);
12646 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12647 args, complain, in_decl);
12649 /* Keep track of the first new parameter we
12650 generate. That's what will be returned to the
12651 caller. */
12652 if (!first_r)
12653 first_r = r;
12655 /* Build a proper chain of parameters when substituting
12656 into a function parameter pack. */
12657 if (prev_r)
12658 DECL_CHAIN (prev_r) = r;
12661 /* If cp_unevaluated_operand is set, we're just looking for a
12662 single dummy parameter, so don't keep going. */
12663 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
12664 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
12665 complain, DECL_CHAIN (t));
12667 /* FIRST_R contains the start of the chain we've built. */
12668 r = first_r;
12670 break;
12672 case FIELD_DECL:
12674 tree type = NULL_TREE;
12675 tree vec = NULL_TREE;
12676 tree expanded_types = NULL_TREE;
12677 int len = 1;
12679 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12681 /* This field is a lambda capture pack. Return a TREE_VEC of
12682 the expanded fields to instantiate_class_template_1 and
12683 store them in the specializations hash table as a
12684 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
12685 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12686 complain, in_decl);
12687 if (TREE_CODE (expanded_types) == TREE_VEC)
12689 len = TREE_VEC_LENGTH (expanded_types);
12690 vec = make_tree_vec (len);
12692 else
12694 /* All we did was update the type. Make a note of that. */
12695 type = expanded_types;
12696 expanded_types = NULL_TREE;
12700 for (int i = 0; i < len; ++i)
12702 r = copy_decl (t);
12703 if (expanded_types)
12705 type = TREE_VEC_ELT (expanded_types, i);
12706 DECL_NAME (r)
12707 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12709 else if (!type)
12710 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12712 if (type == error_mark_node)
12713 RETURN (error_mark_node);
12714 TREE_TYPE (r) = type;
12715 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12717 if (DECL_C_BIT_FIELD (r))
12718 /* For bit-fields, DECL_INITIAL gives the number of bits. For
12719 non-bit-fields DECL_INITIAL is a non-static data member
12720 initializer, which gets deferred instantiation. */
12721 DECL_INITIAL (r)
12722 = tsubst_expr (DECL_INITIAL (t), args,
12723 complain, in_decl,
12724 /*integral_constant_expression_p=*/true);
12725 else if (DECL_INITIAL (t))
12727 /* Set up DECL_TEMPLATE_INFO so that we can get at the
12728 NSDMI in perform_member_init. Still set DECL_INITIAL
12729 so that we know there is one. */
12730 DECL_INITIAL (r) = void_node;
12731 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
12732 retrofit_lang_decl (r);
12733 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12735 /* We don't have to set DECL_CONTEXT here; it is set by
12736 finish_member_declaration. */
12737 DECL_CHAIN (r) = NULL_TREE;
12739 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12740 args, complain, in_decl);
12742 if (vec)
12743 TREE_VEC_ELT (vec, i) = r;
12746 if (vec)
12748 r = vec;
12749 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
12750 SET_ARGUMENT_PACK_ARGS (pack, vec);
12751 register_specialization (pack, t, args, false, 0);
12754 break;
12756 case USING_DECL:
12757 /* We reach here only for member using decls. We also need to check
12758 uses_template_parms because DECL_DEPENDENT_P is not set for a
12759 using-declaration that designates a member of the current
12760 instantiation (c++/53549). */
12761 if (DECL_DEPENDENT_P (t)
12762 || uses_template_parms (USING_DECL_SCOPE (t)))
12764 tree scope = USING_DECL_SCOPE (t);
12765 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
12766 if (PACK_EXPANSION_P (scope))
12768 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
12769 int len = TREE_VEC_LENGTH (vec);
12770 r = make_tree_vec (len);
12771 for (int i = 0; i < len; ++i)
12773 tree escope = TREE_VEC_ELT (vec, i);
12774 tree elt = do_class_using_decl (escope, name);
12775 if (!elt)
12777 r = error_mark_node;
12778 break;
12780 else
12782 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
12783 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
12785 TREE_VEC_ELT (r, i) = elt;
12788 else
12790 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
12791 complain, in_decl);
12792 r = do_class_using_decl (inst_scope, name);
12793 if (!r)
12794 r = error_mark_node;
12795 else
12797 TREE_PROTECTED (r) = TREE_PROTECTED (t);
12798 TREE_PRIVATE (r) = TREE_PRIVATE (t);
12802 else
12804 r = copy_node (t);
12805 DECL_CHAIN (r) = NULL_TREE;
12807 break;
12809 case TYPE_DECL:
12810 case VAR_DECL:
12812 tree argvec = NULL_TREE;
12813 tree gen_tmpl = NULL_TREE;
12814 tree spec;
12815 tree tmpl = NULL_TREE;
12816 tree ctx;
12817 tree type = NULL_TREE;
12818 bool local_p;
12820 if (TREE_TYPE (t) == error_mark_node)
12821 RETURN (error_mark_node);
12823 if (TREE_CODE (t) == TYPE_DECL
12824 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
12826 /* If this is the canonical decl, we don't have to
12827 mess with instantiations, and often we can't (for
12828 typename, template type parms and such). Note that
12829 TYPE_NAME is not correct for the above test if
12830 we've copied the type for a typedef. */
12831 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12832 if (type == error_mark_node)
12833 RETURN (error_mark_node);
12834 r = TYPE_NAME (type);
12835 break;
12838 /* Check to see if we already have the specialization we
12839 need. */
12840 spec = NULL_TREE;
12841 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
12843 /* T is a static data member or namespace-scope entity.
12844 We have to substitute into namespace-scope variables
12845 (not just variable templates) because of cases like:
12847 template <class T> void f() { extern T t; }
12849 where the entity referenced is not known until
12850 instantiation time. */
12851 local_p = false;
12852 ctx = DECL_CONTEXT (t);
12853 if (DECL_CLASS_SCOPE_P (t))
12855 ctx = tsubst_aggr_type (ctx, args,
12856 complain,
12857 in_decl, /*entering_scope=*/1);
12858 /* If CTX is unchanged, then T is in fact the
12859 specialization we want. That situation occurs when
12860 referencing a static data member within in its own
12861 class. We can use pointer equality, rather than
12862 same_type_p, because DECL_CONTEXT is always
12863 canonical... */
12864 if (ctx == DECL_CONTEXT (t)
12865 /* ... unless T is a member template; in which
12866 case our caller can be willing to create a
12867 specialization of that template represented
12868 by T. */
12869 && !(DECL_TI_TEMPLATE (t)
12870 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
12871 spec = t;
12874 if (!spec)
12876 tmpl = DECL_TI_TEMPLATE (t);
12877 gen_tmpl = most_general_template (tmpl);
12878 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
12879 if (argvec != error_mark_node)
12880 argvec = (coerce_innermost_template_parms
12881 (DECL_TEMPLATE_PARMS (gen_tmpl),
12882 argvec, t, complain,
12883 /*all*/true, /*defarg*/true));
12884 if (argvec == error_mark_node)
12885 RETURN (error_mark_node);
12886 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12887 spec = retrieve_specialization (gen_tmpl, argvec, hash);
12890 else
12892 /* A local variable. */
12893 local_p = true;
12894 /* Subsequent calls to pushdecl will fill this in. */
12895 ctx = NULL_TREE;
12896 /* Unless this is a reference to a static variable from an
12897 enclosing function, in which case we need to fill it in now. */
12898 if (TREE_STATIC (t))
12900 tree fn = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
12901 if (fn != current_function_decl)
12902 ctx = fn;
12904 spec = retrieve_local_specialization (t);
12906 /* If we already have the specialization we need, there is
12907 nothing more to do. */
12908 if (spec)
12910 r = spec;
12911 break;
12914 /* Create a new node for the specialization we need. */
12915 r = copy_decl (t);
12916 if (type == NULL_TREE)
12918 if (is_typedef_decl (t))
12919 type = DECL_ORIGINAL_TYPE (t);
12920 else
12921 type = TREE_TYPE (t);
12922 if (VAR_P (t)
12923 && VAR_HAD_UNKNOWN_BOUND (t)
12924 && type != error_mark_node)
12925 type = strip_array_domain (type);
12926 type = tsubst (type, args, complain, in_decl);
12928 if (VAR_P (r))
12930 /* Even if the original location is out of scope, the
12931 newly substituted one is not. */
12932 DECL_DEAD_FOR_LOCAL (r) = 0;
12933 DECL_INITIALIZED_P (r) = 0;
12934 DECL_TEMPLATE_INSTANTIATED (r) = 0;
12935 if (type == error_mark_node)
12936 RETURN (error_mark_node);
12937 if (TREE_CODE (type) == FUNCTION_TYPE)
12939 /* It may seem that this case cannot occur, since:
12941 typedef void f();
12942 void g() { f x; }
12944 declares a function, not a variable. However:
12946 typedef void f();
12947 template <typename T> void g() { T t; }
12948 template void g<f>();
12950 is an attempt to declare a variable with function
12951 type. */
12952 error ("variable %qD has function type",
12953 /* R is not yet sufficiently initialized, so we
12954 just use its name. */
12955 DECL_NAME (r));
12956 RETURN (error_mark_node);
12958 type = complete_type (type);
12959 /* Wait until cp_finish_decl to set this again, to handle
12960 circular dependency (template/instantiate6.C). */
12961 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
12962 type = check_var_type (DECL_NAME (r), type);
12964 if (DECL_HAS_VALUE_EXPR_P (t))
12966 tree ve = DECL_VALUE_EXPR (t);
12967 ve = tsubst_expr (ve, args, complain, in_decl,
12968 /*constant_expression_p=*/false);
12969 if (REFERENCE_REF_P (ve))
12971 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
12972 ve = TREE_OPERAND (ve, 0);
12974 SET_DECL_VALUE_EXPR (r, ve);
12976 if (CP_DECL_THREAD_LOCAL_P (r)
12977 && !processing_template_decl)
12978 set_decl_tls_model (r, decl_default_tls_model (r));
12980 else if (DECL_SELF_REFERENCE_P (t))
12981 SET_DECL_SELF_REFERENCE_P (r);
12982 TREE_TYPE (r) = type;
12983 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12984 DECL_CONTEXT (r) = ctx;
12985 /* Clear out the mangled name and RTL for the instantiation. */
12986 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12987 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
12988 SET_DECL_RTL (r, NULL);
12989 /* The initializer must not be expanded until it is required;
12990 see [temp.inst]. */
12991 DECL_INITIAL (r) = NULL_TREE;
12992 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
12993 if (VAR_P (r))
12995 SET_DECL_MODE (r, VOIDmode);
12997 /* Possibly limit visibility based on template args. */
12998 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12999 if (DECL_VISIBILITY_SPECIFIED (t))
13001 DECL_VISIBILITY_SPECIFIED (r) = 0;
13002 DECL_ATTRIBUTES (r)
13003 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13005 determine_visibility (r);
13008 if (!local_p)
13010 /* A static data member declaration is always marked
13011 external when it is declared in-class, even if an
13012 initializer is present. We mimic the non-template
13013 processing here. */
13014 DECL_EXTERNAL (r) = 1;
13015 if (DECL_NAMESPACE_SCOPE_P (t))
13016 DECL_NOT_REALLY_EXTERN (r) = 1;
13018 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
13019 SET_DECL_IMPLICIT_INSTANTIATION (r);
13020 register_specialization (r, gen_tmpl, argvec, false, hash);
13022 else
13024 if (DECL_LANG_SPECIFIC (r))
13025 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13026 if (!cp_unevaluated_operand)
13027 register_local_specialization (r, t);
13030 DECL_CHAIN (r) = NULL_TREE;
13032 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
13033 /*flags=*/0,
13034 args, complain, in_decl);
13036 /* Preserve a typedef that names a type. */
13037 if (is_typedef_decl (r) && type != error_mark_node)
13039 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
13040 set_underlying_type (r);
13041 if (TYPE_DECL_ALIAS_P (r))
13042 /* An alias template specialization can be dependent
13043 even if its underlying type is not. */
13044 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
13047 layout_decl (r, 0);
13049 break;
13051 default:
13052 gcc_unreachable ();
13054 #undef RETURN
13056 out:
13057 /* Restore the file and line information. */
13058 input_location = saved_loc;
13060 return r;
13063 /* Substitute into the ARG_TYPES of a function type.
13064 If END is a TREE_CHAIN, leave it and any following types
13065 un-substituted. */
13067 static tree
13068 tsubst_arg_types (tree arg_types,
13069 tree args,
13070 tree end,
13071 tsubst_flags_t complain,
13072 tree in_decl)
13074 tree remaining_arg_types;
13075 tree type = NULL_TREE;
13076 int i = 1;
13077 tree expanded_args = NULL_TREE;
13078 tree default_arg;
13080 if (!arg_types || arg_types == void_list_node || arg_types == end)
13081 return arg_types;
13083 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
13084 args, end, complain, in_decl);
13085 if (remaining_arg_types == error_mark_node)
13086 return error_mark_node;
13088 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
13090 /* For a pack expansion, perform substitution on the
13091 entire expression. Later on, we'll handle the arguments
13092 one-by-one. */
13093 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
13094 args, complain, in_decl);
13096 if (TREE_CODE (expanded_args) == TREE_VEC)
13097 /* So that we'll spin through the parameters, one by one. */
13098 i = TREE_VEC_LENGTH (expanded_args);
13099 else
13101 /* We only partially substituted into the parameter
13102 pack. Our type is TYPE_PACK_EXPANSION. */
13103 type = expanded_args;
13104 expanded_args = NULL_TREE;
13108 while (i > 0) {
13109 --i;
13111 if (expanded_args)
13112 type = TREE_VEC_ELT (expanded_args, i);
13113 else if (!type)
13114 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
13116 if (type == error_mark_node)
13117 return error_mark_node;
13118 if (VOID_TYPE_P (type))
13120 if (complain & tf_error)
13122 error ("invalid parameter type %qT", type);
13123 if (in_decl)
13124 error ("in declaration %q+D", in_decl);
13126 return error_mark_node;
13128 /* DR 657. */
13129 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
13130 return error_mark_node;
13132 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13133 top-level qualifiers as required. */
13134 type = cv_unqualified (type_decays_to (type));
13136 /* We do not substitute into default arguments here. The standard
13137 mandates that they be instantiated only when needed, which is
13138 done in build_over_call. */
13139 default_arg = TREE_PURPOSE (arg_types);
13141 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
13143 /* We've instantiated a template before its default arguments
13144 have been parsed. This can happen for a nested template
13145 class, and is not an error unless we require the default
13146 argument in a call of this function. */
13147 remaining_arg_types =
13148 tree_cons (default_arg, type, remaining_arg_types);
13149 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
13151 else
13152 remaining_arg_types =
13153 hash_tree_cons (default_arg, type, remaining_arg_types);
13156 return remaining_arg_types;
13159 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13160 *not* handle the exception-specification for FNTYPE, because the
13161 initial substitution of explicitly provided template parameters
13162 during argument deduction forbids substitution into the
13163 exception-specification:
13165 [temp.deduct]
13167 All references in the function type of the function template to the
13168 corresponding template parameters are replaced by the specified tem-
13169 plate argument values. If a substitution in a template parameter or
13170 in the function type of the function template results in an invalid
13171 type, type deduction fails. [Note: The equivalent substitution in
13172 exception specifications is done only when the function is instanti-
13173 ated, at which point a program is ill-formed if the substitution
13174 results in an invalid type.] */
13176 static tree
13177 tsubst_function_type (tree t,
13178 tree args,
13179 tsubst_flags_t complain,
13180 tree in_decl)
13182 tree return_type;
13183 tree arg_types = NULL_TREE;
13184 tree fntype;
13186 /* The TYPE_CONTEXT is not used for function/method types. */
13187 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
13189 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13190 failure. */
13191 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13193 if (late_return_type_p)
13195 /* Substitute the argument types. */
13196 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13197 complain, in_decl);
13198 if (arg_types == error_mark_node)
13199 return error_mark_node;
13201 tree save_ccp = current_class_ptr;
13202 tree save_ccr = current_class_ref;
13203 tree this_type = (TREE_CODE (t) == METHOD_TYPE
13204 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
13205 bool do_inject = this_type && CLASS_TYPE_P (this_type);
13206 if (do_inject)
13208 /* DR 1207: 'this' is in scope in the trailing return type. */
13209 inject_this_parameter (this_type, cp_type_quals (this_type));
13212 /* Substitute the return type. */
13213 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13215 if (do_inject)
13217 current_class_ptr = save_ccp;
13218 current_class_ref = save_ccr;
13221 else
13222 /* Substitute the return type. */
13223 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13225 if (return_type == error_mark_node)
13226 return error_mark_node;
13227 /* DR 486 clarifies that creation of a function type with an
13228 invalid return type is a deduction failure. */
13229 if (TREE_CODE (return_type) == ARRAY_TYPE
13230 || TREE_CODE (return_type) == FUNCTION_TYPE)
13232 if (complain & tf_error)
13234 if (TREE_CODE (return_type) == ARRAY_TYPE)
13235 error ("function returning an array");
13236 else
13237 error ("function returning a function");
13239 return error_mark_node;
13241 /* And DR 657. */
13242 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
13243 return error_mark_node;
13245 if (!late_return_type_p)
13247 /* Substitute the argument types. */
13248 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13249 complain, in_decl);
13250 if (arg_types == error_mark_node)
13251 return error_mark_node;
13254 /* Construct a new type node and return it. */
13255 if (TREE_CODE (t) == FUNCTION_TYPE)
13257 fntype = build_function_type (return_type, arg_types);
13258 fntype = apply_memfn_quals (fntype,
13259 type_memfn_quals (t),
13260 type_memfn_rqual (t));
13262 else
13264 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13265 /* Don't pick up extra function qualifiers from the basetype. */
13266 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13267 if (! MAYBE_CLASS_TYPE_P (r))
13269 /* [temp.deduct]
13271 Type deduction may fail for any of the following
13272 reasons:
13274 -- Attempting to create "pointer to member of T" when T
13275 is not a class type. */
13276 if (complain & tf_error)
13277 error ("creating pointer to member function of non-class type %qT",
13279 return error_mark_node;
13282 fntype = build_method_type_directly (r, return_type,
13283 TREE_CHAIN (arg_types));
13284 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
13286 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
13288 if (late_return_type_p)
13289 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
13291 return fntype;
13294 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
13295 ARGS into that specification, and return the substituted
13296 specification. If there is no specification, return NULL_TREE. */
13298 static tree
13299 tsubst_exception_specification (tree fntype,
13300 tree args,
13301 tsubst_flags_t complain,
13302 tree in_decl,
13303 bool defer_ok)
13305 tree specs;
13306 tree new_specs;
13308 specs = TYPE_RAISES_EXCEPTIONS (fntype);
13309 new_specs = NULL_TREE;
13310 if (specs && TREE_PURPOSE (specs))
13312 /* A noexcept-specifier. */
13313 tree expr = TREE_PURPOSE (specs);
13314 if (TREE_CODE (expr) == INTEGER_CST)
13315 new_specs = expr;
13316 else if (defer_ok)
13318 /* Defer instantiation of noexcept-specifiers to avoid
13319 excessive instantiations (c++/49107). */
13320 new_specs = make_node (DEFERRED_NOEXCEPT);
13321 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
13323 /* We already partially instantiated this member template,
13324 so combine the new args with the old. */
13325 DEFERRED_NOEXCEPT_PATTERN (new_specs)
13326 = DEFERRED_NOEXCEPT_PATTERN (expr);
13327 DEFERRED_NOEXCEPT_ARGS (new_specs)
13328 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
13330 else
13332 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
13333 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
13336 else
13337 new_specs = tsubst_copy_and_build
13338 (expr, args, complain, in_decl, /*function_p=*/false,
13339 /*integral_constant_expression_p=*/true);
13340 new_specs = build_noexcept_spec (new_specs, complain);
13342 else if (specs)
13344 if (! TREE_VALUE (specs))
13345 new_specs = specs;
13346 else
13347 while (specs)
13349 tree spec;
13350 int i, len = 1;
13351 tree expanded_specs = NULL_TREE;
13353 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
13355 /* Expand the pack expansion type. */
13356 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
13357 args, complain,
13358 in_decl);
13360 if (expanded_specs == error_mark_node)
13361 return error_mark_node;
13362 else if (TREE_CODE (expanded_specs) == TREE_VEC)
13363 len = TREE_VEC_LENGTH (expanded_specs);
13364 else
13366 /* We're substituting into a member template, so
13367 we got a TYPE_PACK_EXPANSION back. Add that
13368 expansion and move on. */
13369 gcc_assert (TREE_CODE (expanded_specs)
13370 == TYPE_PACK_EXPANSION);
13371 new_specs = add_exception_specifier (new_specs,
13372 expanded_specs,
13373 complain);
13374 specs = TREE_CHAIN (specs);
13375 continue;
13379 for (i = 0; i < len; ++i)
13381 if (expanded_specs)
13382 spec = TREE_VEC_ELT (expanded_specs, i);
13383 else
13384 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
13385 if (spec == error_mark_node)
13386 return spec;
13387 new_specs = add_exception_specifier (new_specs, spec,
13388 complain);
13391 specs = TREE_CHAIN (specs);
13394 return new_specs;
13397 /* Take the tree structure T and replace template parameters used
13398 therein with the argument vector ARGS. IN_DECL is an associated
13399 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
13400 Issue error and warning messages under control of COMPLAIN. Note
13401 that we must be relatively non-tolerant of extensions here, in
13402 order to preserve conformance; if we allow substitutions that
13403 should not be allowed, we may allow argument deductions that should
13404 not succeed, and therefore report ambiguous overload situations
13405 where there are none. In theory, we could allow the substitution,
13406 but indicate that it should have failed, and allow our caller to
13407 make sure that the right thing happens, but we don't try to do this
13408 yet.
13410 This function is used for dealing with types, decls and the like;
13411 for expressions, use tsubst_expr or tsubst_copy. */
13413 tree
13414 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13416 enum tree_code code;
13417 tree type, r = NULL_TREE;
13419 if (t == NULL_TREE || t == error_mark_node
13420 || t == integer_type_node
13421 || t == void_type_node
13422 || t == char_type_node
13423 || t == unknown_type_node
13424 || TREE_CODE (t) == NAMESPACE_DECL
13425 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
13426 return t;
13428 if (DECL_P (t))
13429 return tsubst_decl (t, args, complain);
13431 if (args == NULL_TREE)
13432 return t;
13434 code = TREE_CODE (t);
13436 if (code == IDENTIFIER_NODE)
13437 type = IDENTIFIER_TYPE_VALUE (t);
13438 else
13439 type = TREE_TYPE (t);
13441 gcc_assert (type != unknown_type_node);
13443 /* Reuse typedefs. We need to do this to handle dependent attributes,
13444 such as attribute aligned. */
13445 if (TYPE_P (t)
13446 && typedef_variant_p (t))
13448 tree decl = TYPE_NAME (t);
13450 if (alias_template_specialization_p (t))
13452 /* DECL represents an alias template and we want to
13453 instantiate it. */
13454 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13455 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13456 r = instantiate_alias_template (tmpl, gen_args, complain);
13458 else if (DECL_CLASS_SCOPE_P (decl)
13459 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
13460 && uses_template_parms (DECL_CONTEXT (decl)))
13462 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13463 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13464 r = retrieve_specialization (tmpl, gen_args, 0);
13466 else if (DECL_FUNCTION_SCOPE_P (decl)
13467 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
13468 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
13469 r = retrieve_local_specialization (decl);
13470 else
13471 /* The typedef is from a non-template context. */
13472 return t;
13474 if (r)
13476 r = TREE_TYPE (r);
13477 r = cp_build_qualified_type_real
13478 (r, cp_type_quals (t) | cp_type_quals (r),
13479 complain | tf_ignore_bad_quals);
13480 return r;
13482 else
13484 /* We don't have an instantiation yet, so drop the typedef. */
13485 int quals = cp_type_quals (t);
13486 t = DECL_ORIGINAL_TYPE (decl);
13487 t = cp_build_qualified_type_real (t, quals,
13488 complain | tf_ignore_bad_quals);
13492 bool fndecl_type = (complain & tf_fndecl_type);
13493 complain &= ~tf_fndecl_type;
13495 if (type
13496 && code != TYPENAME_TYPE
13497 && code != TEMPLATE_TYPE_PARM
13498 && code != TEMPLATE_PARM_INDEX
13499 && code != IDENTIFIER_NODE
13500 && code != FUNCTION_TYPE
13501 && code != METHOD_TYPE)
13502 type = tsubst (type, args, complain, in_decl);
13503 if (type == error_mark_node)
13504 return error_mark_node;
13506 switch (code)
13508 case RECORD_TYPE:
13509 case UNION_TYPE:
13510 case ENUMERAL_TYPE:
13511 return tsubst_aggr_type (t, args, complain, in_decl,
13512 /*entering_scope=*/0);
13514 case ERROR_MARK:
13515 case IDENTIFIER_NODE:
13516 case VOID_TYPE:
13517 case REAL_TYPE:
13518 case COMPLEX_TYPE:
13519 case VECTOR_TYPE:
13520 case BOOLEAN_TYPE:
13521 case NULLPTR_TYPE:
13522 case LANG_TYPE:
13523 return t;
13525 case INTEGER_TYPE:
13526 if (t == integer_type_node)
13527 return t;
13529 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
13530 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
13531 return t;
13534 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
13536 max = tsubst_expr (omax, args, complain, in_decl,
13537 /*integral_constant_expression_p=*/false);
13539 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13540 needed. */
13541 if (TREE_CODE (max) == NOP_EXPR
13542 && TREE_SIDE_EFFECTS (omax)
13543 && !TREE_TYPE (max))
13544 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
13546 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13547 with TREE_SIDE_EFFECTS that indicates this is not an integral
13548 constant expression. */
13549 if (processing_template_decl
13550 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
13552 gcc_assert (TREE_CODE (max) == NOP_EXPR);
13553 TREE_SIDE_EFFECTS (max) = 1;
13556 return compute_array_index_type (NULL_TREE, max, complain);
13559 case TEMPLATE_TYPE_PARM:
13560 case TEMPLATE_TEMPLATE_PARM:
13561 case BOUND_TEMPLATE_TEMPLATE_PARM:
13562 case TEMPLATE_PARM_INDEX:
13564 int idx;
13565 int level;
13566 int levels;
13567 tree arg = NULL_TREE;
13569 /* Early in template argument deduction substitution, we don't
13570 want to reduce the level of 'auto', or it will be confused
13571 with a normal template parm in subsequent deduction. */
13572 if (is_auto (t) && (complain & tf_partial))
13573 return t;
13575 r = NULL_TREE;
13577 gcc_assert (TREE_VEC_LENGTH (args) > 0);
13578 template_parm_level_and_index (t, &level, &idx);
13580 levels = TMPL_ARGS_DEPTH (args);
13581 if (level <= levels
13582 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
13584 arg = TMPL_ARG (args, level, idx);
13586 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
13588 /* See through ARGUMENT_PACK_SELECT arguments. */
13589 arg = ARGUMENT_PACK_SELECT_ARG (arg);
13590 /* If the selected argument is an expansion E, that most
13591 likely means we were called from
13592 gen_elem_of_pack_expansion_instantiation during the
13593 substituting of pack an argument pack (which Ith
13594 element is a pack expansion, where I is
13595 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
13596 In this case, the Ith element resulting from this
13597 substituting is going to be a pack expansion, which
13598 pattern is the pattern of E. Let's return the
13599 pattern of E, and
13600 gen_elem_of_pack_expansion_instantiation will
13601 build the resulting pack expansion from it. */
13602 if (PACK_EXPANSION_P (arg))
13604 /* Make sure we aren't throwing away arg info. */
13605 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
13606 arg = PACK_EXPANSION_PATTERN (arg);
13611 if (arg == error_mark_node)
13612 return error_mark_node;
13613 else if (arg != NULL_TREE)
13615 if (ARGUMENT_PACK_P (arg))
13616 /* If ARG is an argument pack, we don't actually want to
13617 perform a substitution here, because substitutions
13618 for argument packs are only done
13619 element-by-element. We can get to this point when
13620 substituting the type of a non-type template
13621 parameter pack, when that type actually contains
13622 template parameter packs from an outer template, e.g.,
13624 template<typename... Types> struct A {
13625 template<Types... Values> struct B { };
13626 }; */
13627 return t;
13629 if (code == TEMPLATE_TYPE_PARM)
13631 int quals;
13632 gcc_assert (TYPE_P (arg));
13634 quals = cp_type_quals (arg) | cp_type_quals (t);
13636 return cp_build_qualified_type_real
13637 (arg, quals, complain | tf_ignore_bad_quals);
13639 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13641 /* We are processing a type constructed from a
13642 template template parameter. */
13643 tree argvec = tsubst (TYPE_TI_ARGS (t),
13644 args, complain, in_decl);
13645 if (argvec == error_mark_node)
13646 return error_mark_node;
13648 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
13649 || TREE_CODE (arg) == TEMPLATE_DECL
13650 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
13652 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
13653 /* Consider this code:
13655 template <template <class> class Template>
13656 struct Internal {
13657 template <class Arg> using Bind = Template<Arg>;
13660 template <template <class> class Template, class Arg>
13661 using Instantiate = Template<Arg>; //#0
13663 template <template <class> class Template,
13664 class Argument>
13665 using Bind =
13666 Instantiate<Internal<Template>::template Bind,
13667 Argument>; //#1
13669 When #1 is parsed, the
13670 BOUND_TEMPLATE_TEMPLATE_PARM representing the
13671 parameter `Template' in #0 matches the
13672 UNBOUND_CLASS_TEMPLATE representing the argument
13673 `Internal<Template>::template Bind'; We then want
13674 to assemble the type `Bind<Argument>' that can't
13675 be fully created right now, because
13676 `Internal<Template>' not being complete, the Bind
13677 template cannot be looked up in that context. So
13678 we need to "store" `Bind<Argument>' for later
13679 when the context of Bind becomes complete. Let's
13680 store that in a TYPENAME_TYPE. */
13681 return make_typename_type (TYPE_CONTEXT (arg),
13682 build_nt (TEMPLATE_ID_EXPR,
13683 TYPE_IDENTIFIER (arg),
13684 argvec),
13685 typename_type,
13686 complain);
13688 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
13689 are resolving nested-types in the signature of a
13690 member function templates. Otherwise ARG is a
13691 TEMPLATE_DECL and is the real template to be
13692 instantiated. */
13693 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
13694 arg = TYPE_NAME (arg);
13696 r = lookup_template_class (arg,
13697 argvec, in_decl,
13698 DECL_CONTEXT (arg),
13699 /*entering_scope=*/0,
13700 complain);
13701 return cp_build_qualified_type_real
13702 (r, cp_type_quals (t) | cp_type_quals (r), complain);
13704 else if (code == TEMPLATE_TEMPLATE_PARM)
13705 return arg;
13706 else
13707 /* TEMPLATE_PARM_INDEX. */
13708 return convert_from_reference (unshare_expr (arg));
13711 if (level == 1)
13712 /* This can happen during the attempted tsubst'ing in
13713 unify. This means that we don't yet have any information
13714 about the template parameter in question. */
13715 return t;
13717 /* If we get here, we must have been looking at a parm for a
13718 more deeply nested template. Make a new version of this
13719 template parameter, but with a lower level. */
13720 switch (code)
13722 case TEMPLATE_TYPE_PARM:
13723 case TEMPLATE_TEMPLATE_PARM:
13724 case BOUND_TEMPLATE_TEMPLATE_PARM:
13725 if (cp_type_quals (t))
13727 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
13728 r = cp_build_qualified_type_real
13729 (r, cp_type_quals (t),
13730 complain | (code == TEMPLATE_TYPE_PARM
13731 ? tf_ignore_bad_quals : 0));
13733 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
13734 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
13735 && (r = (TEMPLATE_PARM_DESCENDANTS
13736 (TEMPLATE_TYPE_PARM_INDEX (t))))
13737 && (r = TREE_TYPE (r))
13738 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
13739 /* Break infinite recursion when substituting the constraints
13740 of a constrained placeholder. */;
13741 else
13743 r = copy_type (t);
13744 TEMPLATE_TYPE_PARM_INDEX (r)
13745 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
13746 r, levels, args, complain);
13747 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
13748 TYPE_MAIN_VARIANT (r) = r;
13749 TYPE_POINTER_TO (r) = NULL_TREE;
13750 TYPE_REFERENCE_TO (r) = NULL_TREE;
13752 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
13754 /* Propagate constraints on placeholders. */
13755 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
13756 PLACEHOLDER_TYPE_CONSTRAINTS (r)
13757 = tsubst_constraint (constr, args, complain, in_decl);
13758 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
13760 if (DECL_TEMPLATE_TEMPLATE_PARM_P (pl))
13761 pl = tsubst (pl, args, complain, in_decl);
13762 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
13766 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
13767 /* We have reduced the level of the template
13768 template parameter, but not the levels of its
13769 template parameters, so canonical_type_parameter
13770 will not be able to find the canonical template
13771 template parameter for this level. Thus, we
13772 require structural equality checking to compare
13773 TEMPLATE_TEMPLATE_PARMs. */
13774 SET_TYPE_STRUCTURAL_EQUALITY (r);
13775 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
13776 SET_TYPE_STRUCTURAL_EQUALITY (r);
13777 else
13778 TYPE_CANONICAL (r) = canonical_type_parameter (r);
13780 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13782 tree tinfo = TYPE_TEMPLATE_INFO (t);
13783 /* We might need to substitute into the types of non-type
13784 template parameters. */
13785 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
13786 complain, in_decl);
13787 if (tmpl == error_mark_node)
13788 return error_mark_node;
13789 tree argvec = tsubst (TI_ARGS (tinfo), args,
13790 complain, in_decl);
13791 if (argvec == error_mark_node)
13792 return error_mark_node;
13794 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
13795 = build_template_info (tmpl, argvec);
13798 break;
13800 case TEMPLATE_PARM_INDEX:
13801 /* OK, now substitute the type of the non-type parameter. We
13802 couldn't do it earlier because it might be an auto parameter,
13803 and we wouldn't need to if we had an argument. */
13804 type = tsubst (type, args, complain, in_decl);
13805 r = reduce_template_parm_level (t, type, levels, args, complain);
13806 break;
13808 default:
13809 gcc_unreachable ();
13812 return r;
13815 case TREE_LIST:
13817 tree purpose, value, chain;
13819 if (t == void_list_node)
13820 return t;
13822 purpose = TREE_PURPOSE (t);
13823 if (purpose)
13825 purpose = tsubst (purpose, args, complain, in_decl);
13826 if (purpose == error_mark_node)
13827 return error_mark_node;
13829 value = TREE_VALUE (t);
13830 if (value)
13832 value = tsubst (value, args, complain, in_decl);
13833 if (value == error_mark_node)
13834 return error_mark_node;
13836 chain = TREE_CHAIN (t);
13837 if (chain && chain != void_type_node)
13839 chain = tsubst (chain, args, complain, in_decl);
13840 if (chain == error_mark_node)
13841 return error_mark_node;
13843 if (purpose == TREE_PURPOSE (t)
13844 && value == TREE_VALUE (t)
13845 && chain == TREE_CHAIN (t))
13846 return t;
13847 return hash_tree_cons (purpose, value, chain);
13850 case TREE_BINFO:
13851 /* We should never be tsubsting a binfo. */
13852 gcc_unreachable ();
13854 case TREE_VEC:
13855 /* A vector of template arguments. */
13856 gcc_assert (!type);
13857 return tsubst_template_args (t, args, complain, in_decl);
13859 case POINTER_TYPE:
13860 case REFERENCE_TYPE:
13862 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
13863 return t;
13865 /* [temp.deduct]
13867 Type deduction may fail for any of the following
13868 reasons:
13870 -- Attempting to create a pointer to reference type.
13871 -- Attempting to create a reference to a reference type or
13872 a reference to void.
13874 Core issue 106 says that creating a reference to a reference
13875 during instantiation is no longer a cause for failure. We
13876 only enforce this check in strict C++98 mode. */
13877 if ((TREE_CODE (type) == REFERENCE_TYPE
13878 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
13879 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
13881 static location_t last_loc;
13883 /* We keep track of the last time we issued this error
13884 message to avoid spewing a ton of messages during a
13885 single bad template instantiation. */
13886 if (complain & tf_error
13887 && last_loc != input_location)
13889 if (VOID_TYPE_P (type))
13890 error ("forming reference to void");
13891 else if (code == POINTER_TYPE)
13892 error ("forming pointer to reference type %qT", type);
13893 else
13894 error ("forming reference to reference type %qT", type);
13895 last_loc = input_location;
13898 return error_mark_node;
13900 else if (TREE_CODE (type) == FUNCTION_TYPE
13901 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
13902 || type_memfn_rqual (type) != REF_QUAL_NONE))
13904 if (complain & tf_error)
13906 if (code == POINTER_TYPE)
13907 error ("forming pointer to qualified function type %qT",
13908 type);
13909 else
13910 error ("forming reference to qualified function type %qT",
13911 type);
13913 return error_mark_node;
13915 else if (code == POINTER_TYPE)
13917 r = build_pointer_type (type);
13918 if (TREE_CODE (type) == METHOD_TYPE)
13919 r = build_ptrmemfunc_type (r);
13921 else if (TREE_CODE (type) == REFERENCE_TYPE)
13922 /* In C++0x, during template argument substitution, when there is an
13923 attempt to create a reference to a reference type, reference
13924 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
13926 "If a template-argument for a template-parameter T names a type
13927 that is a reference to a type A, an attempt to create the type
13928 'lvalue reference to cv T' creates the type 'lvalue reference to
13929 A,' while an attempt to create the type type rvalue reference to
13930 cv T' creates the type T"
13932 r = cp_build_reference_type
13933 (TREE_TYPE (type),
13934 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
13935 else
13936 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
13937 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
13939 if (r != error_mark_node)
13940 /* Will this ever be needed for TYPE_..._TO values? */
13941 layout_type (r);
13943 return r;
13945 case OFFSET_TYPE:
13947 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
13948 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
13950 /* [temp.deduct]
13952 Type deduction may fail for any of the following
13953 reasons:
13955 -- Attempting to create "pointer to member of T" when T
13956 is not a class type. */
13957 if (complain & tf_error)
13958 error ("creating pointer to member of non-class type %qT", r);
13959 return error_mark_node;
13961 if (TREE_CODE (type) == REFERENCE_TYPE)
13963 if (complain & tf_error)
13964 error ("creating pointer to member reference type %qT", type);
13965 return error_mark_node;
13967 if (VOID_TYPE_P (type))
13969 if (complain & tf_error)
13970 error ("creating pointer to member of type void");
13971 return error_mark_node;
13973 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
13974 if (TREE_CODE (type) == FUNCTION_TYPE)
13976 /* The type of the implicit object parameter gets its
13977 cv-qualifiers from the FUNCTION_TYPE. */
13978 tree memptr;
13979 tree method_type
13980 = build_memfn_type (type, r, type_memfn_quals (type),
13981 type_memfn_rqual (type));
13982 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
13983 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
13984 complain);
13986 else
13987 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
13988 cp_type_quals (t),
13989 complain);
13991 case FUNCTION_TYPE:
13992 case METHOD_TYPE:
13994 tree fntype;
13995 tree specs;
13996 fntype = tsubst_function_type (t, args, complain, in_decl);
13997 if (fntype == error_mark_node)
13998 return error_mark_node;
14000 /* Substitute the exception specification. */
14001 specs = tsubst_exception_specification (t, args, complain, in_decl,
14002 /*defer_ok*/fndecl_type);
14003 if (specs == error_mark_node)
14004 return error_mark_node;
14005 if (specs)
14006 fntype = build_exception_variant (fntype, specs);
14007 return fntype;
14009 case ARRAY_TYPE:
14011 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
14012 if (domain == error_mark_node)
14013 return error_mark_node;
14015 /* As an optimization, we avoid regenerating the array type if
14016 it will obviously be the same as T. */
14017 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
14018 return t;
14020 /* These checks should match the ones in create_array_type_for_decl.
14022 [temp.deduct]
14024 The deduction may fail for any of the following reasons:
14026 -- Attempting to create an array with an element type that
14027 is void, a function type, or a reference type, or [DR337]
14028 an abstract class type. */
14029 if (VOID_TYPE_P (type)
14030 || TREE_CODE (type) == FUNCTION_TYPE
14031 || (TREE_CODE (type) == ARRAY_TYPE
14032 && TYPE_DOMAIN (type) == NULL_TREE)
14033 || TREE_CODE (type) == REFERENCE_TYPE)
14035 if (complain & tf_error)
14036 error ("creating array of %qT", type);
14037 return error_mark_node;
14040 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
14041 return error_mark_node;
14043 r = build_cplus_array_type (type, domain);
14045 if (TYPE_USER_ALIGN (t))
14047 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
14048 TYPE_USER_ALIGN (r) = 1;
14051 return r;
14054 case TYPENAME_TYPE:
14056 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14057 in_decl, /*entering_scope=*/1);
14058 if (ctx == error_mark_node)
14059 return error_mark_node;
14061 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
14062 complain, in_decl);
14063 if (f == error_mark_node)
14064 return error_mark_node;
14066 if (!MAYBE_CLASS_TYPE_P (ctx))
14068 if (complain & tf_error)
14069 error ("%qT is not a class, struct, or union type", ctx);
14070 return error_mark_node;
14072 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
14074 /* Normally, make_typename_type does not require that the CTX
14075 have complete type in order to allow things like:
14077 template <class T> struct S { typename S<T>::X Y; };
14079 But, such constructs have already been resolved by this
14080 point, so here CTX really should have complete type, unless
14081 it's a partial instantiation. */
14082 ctx = complete_type (ctx);
14083 if (!COMPLETE_TYPE_P (ctx))
14085 if (complain & tf_error)
14086 cxx_incomplete_type_error (NULL_TREE, ctx);
14087 return error_mark_node;
14091 f = make_typename_type (ctx, f, typename_type,
14092 complain | tf_keep_type_decl);
14093 if (f == error_mark_node)
14094 return f;
14095 if (TREE_CODE (f) == TYPE_DECL)
14097 complain |= tf_ignore_bad_quals;
14098 f = TREE_TYPE (f);
14101 if (TREE_CODE (f) != TYPENAME_TYPE)
14103 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
14105 if (complain & tf_error)
14106 error ("%qT resolves to %qT, which is not an enumeration type",
14107 t, f);
14108 else
14109 return error_mark_node;
14111 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
14113 if (complain & tf_error)
14114 error ("%qT resolves to %qT, which is is not a class type",
14115 t, f);
14116 else
14117 return error_mark_node;
14121 return cp_build_qualified_type_real
14122 (f, cp_type_quals (f) | cp_type_quals (t), complain);
14125 case UNBOUND_CLASS_TEMPLATE:
14127 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14128 in_decl, /*entering_scope=*/1);
14129 tree name = TYPE_IDENTIFIER (t);
14130 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
14132 if (ctx == error_mark_node || name == error_mark_node)
14133 return error_mark_node;
14135 if (parm_list)
14136 parm_list = tsubst_template_parms (parm_list, args, complain);
14137 return make_unbound_class_template (ctx, name, parm_list, complain);
14140 case TYPEOF_TYPE:
14142 tree type;
14144 ++cp_unevaluated_operand;
14145 ++c_inhibit_evaluation_warnings;
14147 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
14148 complain, in_decl,
14149 /*integral_constant_expression_p=*/false);
14151 --cp_unevaluated_operand;
14152 --c_inhibit_evaluation_warnings;
14154 type = finish_typeof (type);
14155 return cp_build_qualified_type_real (type,
14156 cp_type_quals (t)
14157 | cp_type_quals (type),
14158 complain);
14161 case DECLTYPE_TYPE:
14163 tree type;
14165 ++cp_unevaluated_operand;
14166 ++c_inhibit_evaluation_warnings;
14168 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
14169 complain|tf_decltype, in_decl,
14170 /*function_p*/false,
14171 /*integral_constant_expression*/false);
14173 if (DECLTYPE_FOR_INIT_CAPTURE (t))
14175 if (type == NULL_TREE)
14177 if (complain & tf_error)
14178 error ("empty initializer in lambda init-capture");
14179 type = error_mark_node;
14181 else if (TREE_CODE (type) == TREE_LIST)
14182 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
14185 --cp_unevaluated_operand;
14186 --c_inhibit_evaluation_warnings;
14188 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
14189 type = lambda_capture_field_type (type,
14190 DECLTYPE_FOR_INIT_CAPTURE (t),
14191 DECLTYPE_FOR_REF_CAPTURE (t));
14192 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
14193 type = lambda_proxy_type (type);
14194 else
14196 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
14197 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
14198 && EXPR_P (type))
14199 /* In a template ~id could be either a complement expression
14200 or an unqualified-id naming a destructor; if instantiating
14201 it produces an expression, it's not an id-expression or
14202 member access. */
14203 id = false;
14204 type = finish_decltype_type (type, id, complain);
14206 return cp_build_qualified_type_real (type,
14207 cp_type_quals (t)
14208 | cp_type_quals (type),
14209 complain | tf_ignore_bad_quals);
14212 case UNDERLYING_TYPE:
14214 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
14215 complain, in_decl);
14216 return finish_underlying_type (type);
14219 case TYPE_ARGUMENT_PACK:
14220 case NONTYPE_ARGUMENT_PACK:
14222 tree r;
14224 if (code == NONTYPE_ARGUMENT_PACK)
14225 r = make_node (code);
14226 else
14227 r = cxx_make_type (code);
14229 tree pack_args = ARGUMENT_PACK_ARGS (t);
14230 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
14231 SET_ARGUMENT_PACK_ARGS (r, pack_args);
14233 return r;
14236 case VOID_CST:
14237 case INTEGER_CST:
14238 case REAL_CST:
14239 case STRING_CST:
14240 case PLUS_EXPR:
14241 case MINUS_EXPR:
14242 case NEGATE_EXPR:
14243 case NOP_EXPR:
14244 case INDIRECT_REF:
14245 case ADDR_EXPR:
14246 case CALL_EXPR:
14247 case ARRAY_REF:
14248 case SCOPE_REF:
14249 /* We should use one of the expression tsubsts for these codes. */
14250 gcc_unreachable ();
14252 default:
14253 sorry ("use of %qs in template", get_tree_code_name (code));
14254 return error_mark_node;
14258 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
14259 expression on the left-hand side of the "." or "->" operator. A
14260 baselink indicates a function from a base class. Both the
14261 BASELINK_ACCESS_BINFO and the base class referenced may indicate
14262 bases of the template class, rather than the instantiated class.
14263 In addition, lookups that were not ambiguous before may be
14264 ambiguous now. Therefore, we perform the lookup again. */
14266 static tree
14267 tsubst_baselink (tree baselink, tree object_type,
14268 tree args, tsubst_flags_t complain, tree in_decl)
14270 bool qualified = BASELINK_QUALIFIED_P (baselink);
14272 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
14273 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
14275 tree optype = BASELINK_OPTYPE (baselink);
14276 optype = tsubst (optype, args, complain, in_decl);
14278 tree template_args = NULL_TREE;
14279 bool template_id_p = false;
14280 tree fns = BASELINK_FUNCTIONS (baselink);
14281 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
14283 template_id_p = true;
14284 template_args = TREE_OPERAND (fns, 1);
14285 fns = TREE_OPERAND (fns, 0);
14286 if (template_args)
14287 template_args = tsubst_template_args (template_args, args,
14288 complain, in_decl);
14291 tree name = OVL_NAME (fns);
14292 if (IDENTIFIER_CONV_OP_P (name))
14293 name = mangle_conv_op_name_for_type (optype);
14295 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
14296 if (!baselink)
14298 if ((complain & tf_error) && constructor_name_p (name, qualifying_scope))
14299 error ("cannot call constructor %<%T::%D%> directly",
14300 qualifying_scope, name);
14301 return error_mark_node;
14304 /* If lookup found a single function, mark it as used at this point.
14305 (If it lookup found multiple functions the one selected later by
14306 overload resolution will be marked as used at that point.) */
14307 if (BASELINK_P (baselink))
14308 fns = BASELINK_FUNCTIONS (baselink);
14309 if (!template_id_p && !really_overloaded_fn (fns)
14310 && !mark_used (OVL_FIRST (fns), complain) && !(complain & tf_error))
14311 return error_mark_node;
14313 if (BASELINK_P (baselink))
14315 /* Add back the template arguments, if present. */
14316 if (template_id_p)
14317 BASELINK_FUNCTIONS (baselink)
14318 = build2 (TEMPLATE_ID_EXPR, unknown_type_node,
14319 BASELINK_FUNCTIONS (baselink), template_args);
14321 /* Update the conversion operator type. */
14322 BASELINK_OPTYPE (baselink) = optype;
14325 if (!object_type)
14326 object_type = current_class_type;
14328 if (qualified || name == complete_dtor_identifier)
14330 baselink = adjust_result_of_qualified_name_lookup (baselink,
14331 qualifying_scope,
14332 object_type);
14333 if (!qualified)
14334 /* We need to call adjust_result_of_qualified_name_lookup in case the
14335 destructor names a base class, but we unset BASELINK_QUALIFIED_P
14336 so that we still get virtual function binding. */
14337 BASELINK_QUALIFIED_P (baselink) = false;
14340 return baselink;
14343 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
14344 true if the qualified-id will be a postfix-expression in-and-of
14345 itself; false if more of the postfix-expression follows the
14346 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
14347 of "&". */
14349 static tree
14350 tsubst_qualified_id (tree qualified_id, tree args,
14351 tsubst_flags_t complain, tree in_decl,
14352 bool done, bool address_p)
14354 tree expr;
14355 tree scope;
14356 tree name;
14357 bool is_template;
14358 tree template_args;
14359 location_t loc = UNKNOWN_LOCATION;
14361 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
14363 /* Figure out what name to look up. */
14364 name = TREE_OPERAND (qualified_id, 1);
14365 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14367 is_template = true;
14368 loc = EXPR_LOCATION (name);
14369 template_args = TREE_OPERAND (name, 1);
14370 if (template_args)
14371 template_args = tsubst_template_args (template_args, args,
14372 complain, in_decl);
14373 if (template_args == error_mark_node)
14374 return error_mark_node;
14375 name = TREE_OPERAND (name, 0);
14377 else
14379 is_template = false;
14380 template_args = NULL_TREE;
14383 /* Substitute into the qualifying scope. When there are no ARGS, we
14384 are just trying to simplify a non-dependent expression. In that
14385 case the qualifying scope may be dependent, and, in any case,
14386 substituting will not help. */
14387 scope = TREE_OPERAND (qualified_id, 0);
14388 if (args)
14390 scope = tsubst (scope, args, complain, in_decl);
14391 expr = tsubst_copy (name, args, complain, in_decl);
14393 else
14394 expr = name;
14396 if (dependent_scope_p (scope))
14398 if (is_template)
14399 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
14400 tree r = build_qualified_name (NULL_TREE, scope, expr,
14401 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
14402 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
14403 return r;
14406 if (!BASELINK_P (name) && !DECL_P (expr))
14408 if (TREE_CODE (expr) == BIT_NOT_EXPR)
14410 /* A BIT_NOT_EXPR is used to represent a destructor. */
14411 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
14413 error ("qualifying type %qT does not match destructor name ~%qT",
14414 scope, TREE_OPERAND (expr, 0));
14415 expr = error_mark_node;
14417 else
14418 expr = lookup_qualified_name (scope, complete_dtor_identifier,
14419 /*is_type_p=*/0, false);
14421 else
14422 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
14423 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
14424 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
14426 if (complain & tf_error)
14428 error ("dependent-name %qE is parsed as a non-type, but "
14429 "instantiation yields a type", qualified_id);
14430 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
14432 return error_mark_node;
14436 if (DECL_P (expr))
14438 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
14439 scope);
14440 /* Remember that there was a reference to this entity. */
14441 if (!mark_used (expr, complain) && !(complain & tf_error))
14442 return error_mark_node;
14445 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
14447 if (complain & tf_error)
14448 qualified_name_lookup_error (scope,
14449 TREE_OPERAND (qualified_id, 1),
14450 expr, input_location);
14451 return error_mark_node;
14454 if (is_template)
14456 if (variable_template_p (expr))
14457 expr = lookup_and_finish_template_variable (expr, template_args,
14458 complain);
14459 else
14460 expr = lookup_template_function (expr, template_args);
14463 if (expr == error_mark_node && complain & tf_error)
14464 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
14465 expr, input_location);
14466 else if (TYPE_P (scope))
14468 expr = (adjust_result_of_qualified_name_lookup
14469 (expr, scope, current_nonlambda_class_type ()));
14470 expr = (finish_qualified_id_expr
14471 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
14472 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
14473 /*template_arg_p=*/false, complain));
14476 /* Expressions do not generally have reference type. */
14477 if (TREE_CODE (expr) != SCOPE_REF
14478 /* However, if we're about to form a pointer-to-member, we just
14479 want the referenced member referenced. */
14480 && TREE_CODE (expr) != OFFSET_REF)
14481 expr = convert_from_reference (expr);
14483 if (REF_PARENTHESIZED_P (qualified_id))
14484 expr = force_paren_expr (expr);
14486 return expr;
14489 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14490 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14491 for tsubst. */
14493 static tree
14494 tsubst_init (tree init, tree decl, tree args,
14495 tsubst_flags_t complain, tree in_decl)
14497 if (!init)
14498 return NULL_TREE;
14500 init = tsubst_expr (init, args, complain, in_decl, false);
14502 if (!init && TREE_TYPE (decl) != error_mark_node)
14504 /* If we had an initializer but it
14505 instantiated to nothing,
14506 value-initialize the object. This will
14507 only occur when the initializer was a
14508 pack expansion where the parameter packs
14509 used in that expansion were of length
14510 zero. */
14511 init = build_value_init (TREE_TYPE (decl),
14512 complain);
14513 if (TREE_CODE (init) == AGGR_INIT_EXPR)
14514 init = get_target_expr_sfinae (init, complain);
14515 if (TREE_CODE (init) == TARGET_EXPR)
14516 TARGET_EXPR_DIRECT_INIT_P (init) = true;
14519 return init;
14522 /* Like tsubst, but deals with expressions. This function just replaces
14523 template parms; to finish processing the resultant expression, use
14524 tsubst_copy_and_build or tsubst_expr. */
14526 static tree
14527 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14529 enum tree_code code;
14530 tree r;
14532 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
14533 return t;
14535 code = TREE_CODE (t);
14537 switch (code)
14539 case PARM_DECL:
14540 r = retrieve_local_specialization (t);
14542 if (r == NULL_TREE)
14544 /* We get here for a use of 'this' in an NSDMI as part of a
14545 constructor call or as part of an aggregate initialization. */
14546 if (DECL_NAME (t) == this_identifier
14547 && ((current_function_decl
14548 && DECL_CONSTRUCTOR_P (current_function_decl))
14549 || (current_class_ref
14550 && TREE_CODE (current_class_ref) == PLACEHOLDER_EXPR)))
14551 return current_class_ptr;
14553 /* This can happen for a parameter name used later in a function
14554 declaration (such as in a late-specified return type). Just
14555 make a dummy decl, since it's only used for its type. */
14556 gcc_assert (cp_unevaluated_operand != 0);
14557 r = tsubst_decl (t, args, complain);
14558 /* Give it the template pattern as its context; its true context
14559 hasn't been instantiated yet and this is good enough for
14560 mangling. */
14561 DECL_CONTEXT (r) = DECL_CONTEXT (t);
14564 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14565 r = ARGUMENT_PACK_SELECT_ARG (r);
14566 if (!mark_used (r, complain) && !(complain & tf_error))
14567 return error_mark_node;
14568 return r;
14570 case CONST_DECL:
14572 tree enum_type;
14573 tree v;
14575 if (DECL_TEMPLATE_PARM_P (t))
14576 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
14577 /* There is no need to substitute into namespace-scope
14578 enumerators. */
14579 if (DECL_NAMESPACE_SCOPE_P (t))
14580 return t;
14581 /* If ARGS is NULL, then T is known to be non-dependent. */
14582 if (args == NULL_TREE)
14583 return scalar_constant_value (t);
14585 /* Unfortunately, we cannot just call lookup_name here.
14586 Consider:
14588 template <int I> int f() {
14589 enum E { a = I };
14590 struct S { void g() { E e = a; } };
14593 When we instantiate f<7>::S::g(), say, lookup_name is not
14594 clever enough to find f<7>::a. */
14595 enum_type
14596 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14597 /*entering_scope=*/0);
14599 for (v = TYPE_VALUES (enum_type);
14600 v != NULL_TREE;
14601 v = TREE_CHAIN (v))
14602 if (TREE_PURPOSE (v) == DECL_NAME (t))
14603 return TREE_VALUE (v);
14605 /* We didn't find the name. That should never happen; if
14606 name-lookup found it during preliminary parsing, we
14607 should find it again here during instantiation. */
14608 gcc_unreachable ();
14610 return t;
14612 case FIELD_DECL:
14613 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14615 /* Check for a local specialization set up by
14616 tsubst_pack_expansion. */
14617 if (tree r = retrieve_local_specialization (t))
14619 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14620 r = ARGUMENT_PACK_SELECT_ARG (r);
14621 return r;
14624 /* When retrieving a capture pack from a generic lambda, remove the
14625 lambda call op's own template argument list from ARGS. Only the
14626 template arguments active for the closure type should be used to
14627 retrieve the pack specialization. */
14628 if (LAMBDA_FUNCTION_P (current_function_decl)
14629 && (template_class_depth (DECL_CONTEXT (t))
14630 != TMPL_ARGS_DEPTH (args)))
14631 args = strip_innermost_template_args (args, 1);
14633 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
14634 tsubst_decl put in the hash table. */
14635 return retrieve_specialization (t, args, 0);
14638 if (DECL_CONTEXT (t))
14640 tree ctx;
14642 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14643 /*entering_scope=*/1);
14644 if (ctx != DECL_CONTEXT (t))
14646 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
14647 if (!r)
14649 if (complain & tf_error)
14650 error ("using invalid field %qD", t);
14651 return error_mark_node;
14653 return r;
14657 return t;
14659 case VAR_DECL:
14660 case FUNCTION_DECL:
14661 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
14662 r = tsubst (t, args, complain, in_decl);
14663 else if (local_variable_p (t)
14664 && uses_template_parms (DECL_CONTEXT (t)))
14666 r = retrieve_local_specialization (t);
14667 if (r == NULL_TREE)
14669 /* First try name lookup to find the instantiation. */
14670 r = lookup_name (DECL_NAME (t));
14671 if (r && !is_capture_proxy (r))
14673 /* Make sure that the one we found is the one we want. */
14674 tree ctx = DECL_CONTEXT (t);
14675 if (DECL_LANG_SPECIFIC (ctx) && DECL_TEMPLATE_INFO (ctx))
14676 ctx = tsubst (ctx, args, complain, in_decl);
14677 if (ctx != DECL_CONTEXT (r))
14678 r = NULL_TREE;
14681 if (r)
14682 /* OK */;
14683 else
14685 /* This can happen for a variable used in a
14686 late-specified return type of a local lambda, or for a
14687 local static or constant. Building a new VAR_DECL
14688 should be OK in all those cases. */
14689 r = tsubst_decl (t, args, complain);
14690 if (local_specializations)
14691 /* Avoid infinite recursion (79640). */
14692 register_local_specialization (r, t);
14693 if (decl_maybe_constant_var_p (r))
14695 /* We can't call cp_finish_decl, so handle the
14696 initializer by hand. */
14697 tree init = tsubst_init (DECL_INITIAL (t), r, args,
14698 complain, in_decl);
14699 if (!processing_template_decl)
14700 init = maybe_constant_init (init);
14701 if (processing_template_decl
14702 ? potential_constant_expression (init)
14703 : reduced_constant_expression_p (init))
14704 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
14705 = TREE_CONSTANT (r) = true;
14706 DECL_INITIAL (r) = init;
14708 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
14709 || decl_constant_var_p (r)
14710 || errorcount || sorrycount);
14711 if (!processing_template_decl
14712 && !TREE_STATIC (r))
14713 r = process_outer_var_ref (r, complain);
14715 /* Remember this for subsequent uses. */
14716 if (local_specializations)
14717 register_local_specialization (r, t);
14720 else
14721 r = t;
14722 if (!mark_used (r, complain))
14723 return error_mark_node;
14724 return r;
14726 case NAMESPACE_DECL:
14727 return t;
14729 case OVERLOAD:
14730 /* An OVERLOAD will always be a non-dependent overload set; an
14731 overload set from function scope will just be represented with an
14732 IDENTIFIER_NODE, and from class scope with a BASELINK. */
14733 gcc_assert (!uses_template_parms (t));
14734 /* We must have marked any lookups as persistent. */
14735 gcc_assert (!OVL_LOOKUP_P (t) || OVL_USED_P (t));
14736 return t;
14738 case BASELINK:
14739 return tsubst_baselink (t, current_nonlambda_class_type (),
14740 args, complain, in_decl);
14742 case TEMPLATE_DECL:
14743 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
14744 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
14745 args, complain, in_decl);
14746 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
14747 return tsubst (t, args, complain, in_decl);
14748 else if (DECL_CLASS_SCOPE_P (t)
14749 && uses_template_parms (DECL_CONTEXT (t)))
14751 /* Template template argument like the following example need
14752 special treatment:
14754 template <template <class> class TT> struct C {};
14755 template <class T> struct D {
14756 template <class U> struct E {};
14757 C<E> c; // #1
14759 D<int> d; // #2
14761 We are processing the template argument `E' in #1 for
14762 the template instantiation #2. Originally, `E' is a
14763 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
14764 have to substitute this with one having context `D<int>'. */
14766 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
14767 if (dependent_scope_p (context))
14769 /* When rewriting a constructor into a deduction guide, a
14770 non-dependent name can become dependent, so memtmpl<args>
14771 becomes context::template memtmpl<args>. */
14772 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14773 return build_qualified_name (type, context, DECL_NAME (t),
14774 /*template*/true);
14776 return lookup_field (context, DECL_NAME(t), 0, false);
14778 else
14779 /* Ordinary template template argument. */
14780 return t;
14782 case CAST_EXPR:
14783 case REINTERPRET_CAST_EXPR:
14784 case CONST_CAST_EXPR:
14785 case STATIC_CAST_EXPR:
14786 case DYNAMIC_CAST_EXPR:
14787 case IMPLICIT_CONV_EXPR:
14788 case CONVERT_EXPR:
14789 case NOP_EXPR:
14791 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14792 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14793 return build1 (code, type, op0);
14796 case SIZEOF_EXPR:
14797 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
14798 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
14800 tree expanded, op = TREE_OPERAND (t, 0);
14801 int len = 0;
14803 if (SIZEOF_EXPR_TYPE_P (t))
14804 op = TREE_TYPE (op);
14806 ++cp_unevaluated_operand;
14807 ++c_inhibit_evaluation_warnings;
14808 /* We only want to compute the number of arguments. */
14809 if (PACK_EXPANSION_P (op))
14810 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
14811 else
14812 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
14813 args, complain, in_decl);
14814 --cp_unevaluated_operand;
14815 --c_inhibit_evaluation_warnings;
14817 if (TREE_CODE (expanded) == TREE_VEC)
14819 len = TREE_VEC_LENGTH (expanded);
14820 /* Set TREE_USED for the benefit of -Wunused. */
14821 for (int i = 0; i < len; i++)
14822 if (DECL_P (TREE_VEC_ELT (expanded, i)))
14823 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
14826 if (expanded == error_mark_node)
14827 return error_mark_node;
14828 else if (PACK_EXPANSION_P (expanded)
14829 || (TREE_CODE (expanded) == TREE_VEC
14830 && pack_expansion_args_count (expanded)))
14833 if (PACK_EXPANSION_P (expanded))
14834 /* OK. */;
14835 else if (TREE_VEC_LENGTH (expanded) == 1)
14836 expanded = TREE_VEC_ELT (expanded, 0);
14837 else
14838 expanded = make_argument_pack (expanded);
14840 if (TYPE_P (expanded))
14841 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
14842 complain & tf_error);
14843 else
14844 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
14845 complain & tf_error);
14847 else
14848 return build_int_cst (size_type_node, len);
14850 if (SIZEOF_EXPR_TYPE_P (t))
14852 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
14853 args, complain, in_decl);
14854 r = build1 (NOP_EXPR, r, error_mark_node);
14855 r = build1 (SIZEOF_EXPR,
14856 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
14857 SIZEOF_EXPR_TYPE_P (r) = 1;
14858 return r;
14860 /* Fall through */
14862 case INDIRECT_REF:
14863 case NEGATE_EXPR:
14864 case TRUTH_NOT_EXPR:
14865 case BIT_NOT_EXPR:
14866 case ADDR_EXPR:
14867 case UNARY_PLUS_EXPR: /* Unary + */
14868 case ALIGNOF_EXPR:
14869 case AT_ENCODE_EXPR:
14870 case ARROW_EXPR:
14871 case THROW_EXPR:
14872 case TYPEID_EXPR:
14873 case REALPART_EXPR:
14874 case IMAGPART_EXPR:
14875 case PAREN_EXPR:
14877 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14878 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14879 return build1 (code, type, op0);
14882 case COMPONENT_REF:
14884 tree object;
14885 tree name;
14887 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14888 name = TREE_OPERAND (t, 1);
14889 if (TREE_CODE (name) == BIT_NOT_EXPR)
14891 name = tsubst_copy (TREE_OPERAND (name, 0), args,
14892 complain, in_decl);
14893 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
14895 else if (TREE_CODE (name) == SCOPE_REF
14896 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
14898 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
14899 complain, in_decl);
14900 name = TREE_OPERAND (name, 1);
14901 name = tsubst_copy (TREE_OPERAND (name, 0), args,
14902 complain, in_decl);
14903 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
14904 name = build_qualified_name (/*type=*/NULL_TREE,
14905 base, name,
14906 /*template_p=*/false);
14908 else if (BASELINK_P (name))
14909 name = tsubst_baselink (name,
14910 non_reference (TREE_TYPE (object)),
14911 args, complain,
14912 in_decl);
14913 else
14914 name = tsubst_copy (name, args, complain, in_decl);
14915 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
14918 case PLUS_EXPR:
14919 case MINUS_EXPR:
14920 case MULT_EXPR:
14921 case TRUNC_DIV_EXPR:
14922 case CEIL_DIV_EXPR:
14923 case FLOOR_DIV_EXPR:
14924 case ROUND_DIV_EXPR:
14925 case EXACT_DIV_EXPR:
14926 case BIT_AND_EXPR:
14927 case BIT_IOR_EXPR:
14928 case BIT_XOR_EXPR:
14929 case TRUNC_MOD_EXPR:
14930 case FLOOR_MOD_EXPR:
14931 case TRUTH_ANDIF_EXPR:
14932 case TRUTH_ORIF_EXPR:
14933 case TRUTH_AND_EXPR:
14934 case TRUTH_OR_EXPR:
14935 case RSHIFT_EXPR:
14936 case LSHIFT_EXPR:
14937 case RROTATE_EXPR:
14938 case LROTATE_EXPR:
14939 case EQ_EXPR:
14940 case NE_EXPR:
14941 case MAX_EXPR:
14942 case MIN_EXPR:
14943 case LE_EXPR:
14944 case GE_EXPR:
14945 case LT_EXPR:
14946 case GT_EXPR:
14947 case COMPOUND_EXPR:
14948 case DOTSTAR_EXPR:
14949 case MEMBER_REF:
14950 case PREDECREMENT_EXPR:
14951 case PREINCREMENT_EXPR:
14952 case POSTDECREMENT_EXPR:
14953 case POSTINCREMENT_EXPR:
14955 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14956 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14957 return build_nt (code, op0, op1);
14960 case SCOPE_REF:
14962 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14963 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14964 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
14965 QUALIFIED_NAME_IS_TEMPLATE (t));
14968 case ARRAY_REF:
14970 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14971 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14972 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
14975 case CALL_EXPR:
14977 int n = VL_EXP_OPERAND_LENGTH (t);
14978 tree result = build_vl_exp (CALL_EXPR, n);
14979 int i;
14980 for (i = 0; i < n; i++)
14981 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
14982 complain, in_decl);
14983 return result;
14986 case COND_EXPR:
14987 case MODOP_EXPR:
14988 case PSEUDO_DTOR_EXPR:
14989 case VEC_PERM_EXPR:
14991 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14992 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14993 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
14994 r = build_nt (code, op0, op1, op2);
14995 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14996 return r;
14999 case NEW_EXPR:
15001 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15002 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15003 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15004 r = build_nt (code, op0, op1, op2);
15005 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
15006 return r;
15009 case DELETE_EXPR:
15011 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15012 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15013 r = build_nt (code, op0, op1);
15014 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
15015 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
15016 return r;
15019 case TEMPLATE_ID_EXPR:
15021 /* Substituted template arguments */
15022 tree fn = TREE_OPERAND (t, 0);
15023 tree targs = TREE_OPERAND (t, 1);
15025 fn = tsubst_copy (fn, args, complain, in_decl);
15026 if (targs)
15027 targs = tsubst_template_args (targs, args, complain, in_decl);
15029 return lookup_template_function (fn, targs);
15032 case TREE_LIST:
15034 tree purpose, value, chain;
15036 if (t == void_list_node)
15037 return t;
15039 purpose = TREE_PURPOSE (t);
15040 if (purpose)
15041 purpose = tsubst_copy (purpose, args, complain, in_decl);
15042 value = TREE_VALUE (t);
15043 if (value)
15044 value = tsubst_copy (value, args, complain, in_decl);
15045 chain = TREE_CHAIN (t);
15046 if (chain && chain != void_type_node)
15047 chain = tsubst_copy (chain, args, complain, in_decl);
15048 if (purpose == TREE_PURPOSE (t)
15049 && value == TREE_VALUE (t)
15050 && chain == TREE_CHAIN (t))
15051 return t;
15052 return tree_cons (purpose, value, chain);
15055 case RECORD_TYPE:
15056 case UNION_TYPE:
15057 case ENUMERAL_TYPE:
15058 case INTEGER_TYPE:
15059 case TEMPLATE_TYPE_PARM:
15060 case TEMPLATE_TEMPLATE_PARM:
15061 case BOUND_TEMPLATE_TEMPLATE_PARM:
15062 case TEMPLATE_PARM_INDEX:
15063 case POINTER_TYPE:
15064 case REFERENCE_TYPE:
15065 case OFFSET_TYPE:
15066 case FUNCTION_TYPE:
15067 case METHOD_TYPE:
15068 case ARRAY_TYPE:
15069 case TYPENAME_TYPE:
15070 case UNBOUND_CLASS_TEMPLATE:
15071 case TYPEOF_TYPE:
15072 case DECLTYPE_TYPE:
15073 case TYPE_DECL:
15074 return tsubst (t, args, complain, in_decl);
15076 case USING_DECL:
15077 t = DECL_NAME (t);
15078 /* Fall through. */
15079 case IDENTIFIER_NODE:
15080 if (IDENTIFIER_CONV_OP_P (t))
15082 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15083 return mangle_conv_op_name_for_type (new_type);
15085 else
15086 return t;
15088 case CONSTRUCTOR:
15089 /* This is handled by tsubst_copy_and_build. */
15090 gcc_unreachable ();
15092 case VA_ARG_EXPR:
15094 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15095 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15096 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
15099 case CLEANUP_POINT_EXPR:
15100 /* We shouldn't have built any of these during initial template
15101 generation. Instead, they should be built during instantiation
15102 in response to the saved STMT_IS_FULL_EXPR_P setting. */
15103 gcc_unreachable ();
15105 case OFFSET_REF:
15107 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15108 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15109 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15110 r = build2 (code, type, op0, op1);
15111 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
15112 if (!mark_used (TREE_OPERAND (r, 1), complain)
15113 && !(complain & tf_error))
15114 return error_mark_node;
15115 return r;
15118 case EXPR_PACK_EXPANSION:
15119 error ("invalid use of pack expansion expression");
15120 return error_mark_node;
15122 case NONTYPE_ARGUMENT_PACK:
15123 error ("use %<...%> to expand argument pack");
15124 return error_mark_node;
15126 case VOID_CST:
15127 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
15128 return t;
15130 case INTEGER_CST:
15131 case REAL_CST:
15132 case STRING_CST:
15133 case COMPLEX_CST:
15135 /* Instantiate any typedefs in the type. */
15136 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15137 r = fold_convert (type, t);
15138 gcc_assert (TREE_CODE (r) == code);
15139 return r;
15142 case PTRMEM_CST:
15143 /* These can sometimes show up in a partial instantiation, but never
15144 involve template parms. */
15145 gcc_assert (!uses_template_parms (t));
15146 return t;
15148 case UNARY_LEFT_FOLD_EXPR:
15149 return tsubst_unary_left_fold (t, args, complain, in_decl);
15150 case UNARY_RIGHT_FOLD_EXPR:
15151 return tsubst_unary_right_fold (t, args, complain, in_decl);
15152 case BINARY_LEFT_FOLD_EXPR:
15153 return tsubst_binary_left_fold (t, args, complain, in_decl);
15154 case BINARY_RIGHT_FOLD_EXPR:
15155 return tsubst_binary_right_fold (t, args, complain, in_decl);
15157 default:
15158 /* We shouldn't get here, but keep going if !flag_checking. */
15159 if (flag_checking)
15160 gcc_unreachable ();
15161 return t;
15165 /* Helper function for tsubst_omp_clauses, used for instantiation of
15166 OMP_CLAUSE_DECL of clauses. */
15168 static tree
15169 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
15170 tree in_decl)
15172 if (decl == NULL_TREE)
15173 return NULL_TREE;
15175 /* Handle an OpenMP array section represented as a TREE_LIST (or
15176 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15177 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15178 TREE_LIST. We can handle it exactly the same as an array section
15179 (purpose, value, and a chain), even though the nomenclature
15180 (low_bound, length, etc) is different. */
15181 if (TREE_CODE (decl) == TREE_LIST)
15183 tree low_bound
15184 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
15185 /*integral_constant_expression_p=*/false);
15186 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
15187 /*integral_constant_expression_p=*/false);
15188 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
15189 in_decl);
15190 if (TREE_PURPOSE (decl) == low_bound
15191 && TREE_VALUE (decl) == length
15192 && TREE_CHAIN (decl) == chain)
15193 return decl;
15194 tree ret = tree_cons (low_bound, length, chain);
15195 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
15196 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
15197 return ret;
15199 tree ret = tsubst_expr (decl, args, complain, in_decl,
15200 /*integral_constant_expression_p=*/false);
15201 /* Undo convert_from_reference tsubst_expr could have called. */
15202 if (decl
15203 && REFERENCE_REF_P (ret)
15204 && !REFERENCE_REF_P (decl))
15205 ret = TREE_OPERAND (ret, 0);
15206 return ret;
15209 /* Like tsubst_copy, but specifically for OpenMP clauses. */
15211 static tree
15212 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
15213 tree args, tsubst_flags_t complain, tree in_decl)
15215 tree new_clauses = NULL_TREE, nc, oc;
15216 tree linear_no_step = NULL_TREE;
15218 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
15220 nc = copy_node (oc);
15221 OMP_CLAUSE_CHAIN (nc) = new_clauses;
15222 new_clauses = nc;
15224 switch (OMP_CLAUSE_CODE (nc))
15226 case OMP_CLAUSE_LASTPRIVATE:
15227 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
15229 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
15230 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
15231 in_decl, /*integral_constant_expression_p=*/false);
15232 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
15233 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
15235 /* FALLTHRU */
15236 case OMP_CLAUSE_PRIVATE:
15237 case OMP_CLAUSE_SHARED:
15238 case OMP_CLAUSE_FIRSTPRIVATE:
15239 case OMP_CLAUSE_COPYIN:
15240 case OMP_CLAUSE_COPYPRIVATE:
15241 case OMP_CLAUSE_UNIFORM:
15242 case OMP_CLAUSE_DEPEND:
15243 case OMP_CLAUSE_FROM:
15244 case OMP_CLAUSE_TO:
15245 case OMP_CLAUSE_MAP:
15246 case OMP_CLAUSE_USE_DEVICE_PTR:
15247 case OMP_CLAUSE_IS_DEVICE_PTR:
15248 OMP_CLAUSE_DECL (nc)
15249 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15250 in_decl);
15251 break;
15252 case OMP_CLAUSE_TILE:
15253 case OMP_CLAUSE_IF:
15254 case OMP_CLAUSE_NUM_THREADS:
15255 case OMP_CLAUSE_SCHEDULE:
15256 case OMP_CLAUSE_COLLAPSE:
15257 case OMP_CLAUSE_FINAL:
15258 case OMP_CLAUSE_DEVICE:
15259 case OMP_CLAUSE_DIST_SCHEDULE:
15260 case OMP_CLAUSE_NUM_TEAMS:
15261 case OMP_CLAUSE_THREAD_LIMIT:
15262 case OMP_CLAUSE_SAFELEN:
15263 case OMP_CLAUSE_SIMDLEN:
15264 case OMP_CLAUSE_NUM_TASKS:
15265 case OMP_CLAUSE_GRAINSIZE:
15266 case OMP_CLAUSE_PRIORITY:
15267 case OMP_CLAUSE_ORDERED:
15268 case OMP_CLAUSE_HINT:
15269 case OMP_CLAUSE_NUM_GANGS:
15270 case OMP_CLAUSE_NUM_WORKERS:
15271 case OMP_CLAUSE_VECTOR_LENGTH:
15272 case OMP_CLAUSE_WORKER:
15273 case OMP_CLAUSE_VECTOR:
15274 case OMP_CLAUSE_ASYNC:
15275 case OMP_CLAUSE_WAIT:
15276 OMP_CLAUSE_OPERAND (nc, 0)
15277 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
15278 in_decl, /*integral_constant_expression_p=*/false);
15279 break;
15280 case OMP_CLAUSE_REDUCTION:
15281 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
15283 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
15284 if (TREE_CODE (placeholder) == SCOPE_REF)
15286 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
15287 complain, in_decl);
15288 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
15289 = build_qualified_name (NULL_TREE, scope,
15290 TREE_OPERAND (placeholder, 1),
15291 false);
15293 else
15294 gcc_assert (identifier_p (placeholder));
15296 OMP_CLAUSE_DECL (nc)
15297 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15298 in_decl);
15299 break;
15300 case OMP_CLAUSE_GANG:
15301 case OMP_CLAUSE_ALIGNED:
15302 OMP_CLAUSE_DECL (nc)
15303 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15304 in_decl);
15305 OMP_CLAUSE_OPERAND (nc, 1)
15306 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
15307 in_decl, /*integral_constant_expression_p=*/false);
15308 break;
15309 case OMP_CLAUSE_LINEAR:
15310 OMP_CLAUSE_DECL (nc)
15311 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15312 in_decl);
15313 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
15315 gcc_assert (!linear_no_step);
15316 linear_no_step = nc;
15318 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
15319 OMP_CLAUSE_LINEAR_STEP (nc)
15320 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
15321 complain, in_decl);
15322 else
15323 OMP_CLAUSE_LINEAR_STEP (nc)
15324 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
15325 in_decl,
15326 /*integral_constant_expression_p=*/false);
15327 break;
15328 case OMP_CLAUSE_NOWAIT:
15329 case OMP_CLAUSE_DEFAULT:
15330 case OMP_CLAUSE_UNTIED:
15331 case OMP_CLAUSE_MERGEABLE:
15332 case OMP_CLAUSE_INBRANCH:
15333 case OMP_CLAUSE_NOTINBRANCH:
15334 case OMP_CLAUSE_PROC_BIND:
15335 case OMP_CLAUSE_FOR:
15336 case OMP_CLAUSE_PARALLEL:
15337 case OMP_CLAUSE_SECTIONS:
15338 case OMP_CLAUSE_TASKGROUP:
15339 case OMP_CLAUSE_NOGROUP:
15340 case OMP_CLAUSE_THREADS:
15341 case OMP_CLAUSE_SIMD:
15342 case OMP_CLAUSE_DEFAULTMAP:
15343 case OMP_CLAUSE_INDEPENDENT:
15344 case OMP_CLAUSE_AUTO:
15345 case OMP_CLAUSE_SEQ:
15346 break;
15347 default:
15348 gcc_unreachable ();
15350 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
15351 switch (OMP_CLAUSE_CODE (nc))
15353 case OMP_CLAUSE_SHARED:
15354 case OMP_CLAUSE_PRIVATE:
15355 case OMP_CLAUSE_FIRSTPRIVATE:
15356 case OMP_CLAUSE_LASTPRIVATE:
15357 case OMP_CLAUSE_COPYPRIVATE:
15358 case OMP_CLAUSE_LINEAR:
15359 case OMP_CLAUSE_REDUCTION:
15360 case OMP_CLAUSE_USE_DEVICE_PTR:
15361 case OMP_CLAUSE_IS_DEVICE_PTR:
15362 /* tsubst_expr on SCOPE_REF results in returning
15363 finish_non_static_data_member result. Undo that here. */
15364 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
15365 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
15366 == IDENTIFIER_NODE))
15368 tree t = OMP_CLAUSE_DECL (nc);
15369 tree v = t;
15370 while (v)
15371 switch (TREE_CODE (v))
15373 case COMPONENT_REF:
15374 case MEM_REF:
15375 case INDIRECT_REF:
15376 CASE_CONVERT:
15377 case POINTER_PLUS_EXPR:
15378 v = TREE_OPERAND (v, 0);
15379 continue;
15380 case PARM_DECL:
15381 if (DECL_CONTEXT (v) == current_function_decl
15382 && DECL_ARTIFICIAL (v)
15383 && DECL_NAME (v) == this_identifier)
15384 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
15385 /* FALLTHRU */
15386 default:
15387 v = NULL_TREE;
15388 break;
15391 else if (VAR_P (OMP_CLAUSE_DECL (oc))
15392 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
15393 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
15394 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
15395 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
15397 tree decl = OMP_CLAUSE_DECL (nc);
15398 if (VAR_P (decl))
15400 retrofit_lang_decl (decl);
15401 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
15404 break;
15405 default:
15406 break;
15410 new_clauses = nreverse (new_clauses);
15411 if (ort != C_ORT_OMP_DECLARE_SIMD)
15413 new_clauses = finish_omp_clauses (new_clauses, ort);
15414 if (linear_no_step)
15415 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
15416 if (nc == linear_no_step)
15418 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
15419 break;
15422 return new_clauses;
15425 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
15427 static tree
15428 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
15429 tree in_decl)
15431 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
15433 tree purpose, value, chain;
15435 if (t == NULL)
15436 return t;
15438 if (TREE_CODE (t) != TREE_LIST)
15439 return tsubst_copy_and_build (t, args, complain, in_decl,
15440 /*function_p=*/false,
15441 /*integral_constant_expression_p=*/false);
15443 if (t == void_list_node)
15444 return t;
15446 purpose = TREE_PURPOSE (t);
15447 if (purpose)
15448 purpose = RECUR (purpose);
15449 value = TREE_VALUE (t);
15450 if (value)
15452 if (TREE_CODE (value) != LABEL_DECL)
15453 value = RECUR (value);
15454 else
15456 value = lookup_label (DECL_NAME (value));
15457 gcc_assert (TREE_CODE (value) == LABEL_DECL);
15458 TREE_USED (value) = 1;
15461 chain = TREE_CHAIN (t);
15462 if (chain && chain != void_type_node)
15463 chain = RECUR (chain);
15464 return tree_cons (purpose, value, chain);
15465 #undef RECUR
15468 /* Used to temporarily communicate the list of #pragma omp parallel
15469 clauses to #pragma omp for instantiation if they are combined
15470 together. */
15472 static tree *omp_parallel_combined_clauses;
15474 /* Substitute one OMP_FOR iterator. */
15476 static void
15477 tsubst_omp_for_iterator (tree t, int i, tree declv, tree orig_declv,
15478 tree initv, tree condv, tree incrv, tree *clauses,
15479 tree args, tsubst_flags_t complain, tree in_decl,
15480 bool integral_constant_expression_p)
15482 #define RECUR(NODE) \
15483 tsubst_expr ((NODE), args, complain, in_decl, \
15484 integral_constant_expression_p)
15485 tree decl, init, cond, incr;
15487 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
15488 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
15490 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
15492 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
15493 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
15496 decl = TREE_OPERAND (init, 0);
15497 init = TREE_OPERAND (init, 1);
15498 tree decl_expr = NULL_TREE;
15499 if (init && TREE_CODE (init) == DECL_EXPR)
15501 /* We need to jump through some hoops to handle declarations in the
15502 init-statement, since we might need to handle auto deduction,
15503 but we need to keep control of initialization. */
15504 decl_expr = init;
15505 init = DECL_INITIAL (DECL_EXPR_DECL (init));
15506 decl = tsubst_decl (decl, args, complain);
15508 else
15510 if (TREE_CODE (decl) == SCOPE_REF)
15512 decl = RECUR (decl);
15513 if (TREE_CODE (decl) == COMPONENT_REF)
15515 tree v = decl;
15516 while (v)
15517 switch (TREE_CODE (v))
15519 case COMPONENT_REF:
15520 case MEM_REF:
15521 case INDIRECT_REF:
15522 CASE_CONVERT:
15523 case POINTER_PLUS_EXPR:
15524 v = TREE_OPERAND (v, 0);
15525 continue;
15526 case PARM_DECL:
15527 if (DECL_CONTEXT (v) == current_function_decl
15528 && DECL_ARTIFICIAL (v)
15529 && DECL_NAME (v) == this_identifier)
15531 decl = TREE_OPERAND (decl, 1);
15532 decl = omp_privatize_field (decl, false);
15534 /* FALLTHRU */
15535 default:
15536 v = NULL_TREE;
15537 break;
15541 else
15542 decl = RECUR (decl);
15544 init = RECUR (init);
15546 tree auto_node = type_uses_auto (TREE_TYPE (decl));
15547 if (auto_node && init)
15548 TREE_TYPE (decl)
15549 = do_auto_deduction (TREE_TYPE (decl), init, auto_node);
15551 gcc_assert (!type_dependent_expression_p (decl));
15553 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15555 if (decl_expr)
15557 /* Declare the variable, but don't let that initialize it. */
15558 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
15559 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
15560 RECUR (decl_expr);
15561 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
15564 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
15565 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15566 if (TREE_CODE (incr) == MODIFY_EXPR)
15568 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15569 tree rhs = RECUR (TREE_OPERAND (incr, 1));
15570 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
15571 NOP_EXPR, rhs, complain);
15573 else
15574 incr = RECUR (incr);
15575 TREE_VEC_ELT (declv, i) = decl;
15576 TREE_VEC_ELT (initv, i) = init;
15577 TREE_VEC_ELT (condv, i) = cond;
15578 TREE_VEC_ELT (incrv, i) = incr;
15579 return;
15582 if (decl_expr)
15584 /* Declare and initialize the variable. */
15585 RECUR (decl_expr);
15586 init = NULL_TREE;
15588 else if (init)
15590 tree *pc;
15591 int j;
15592 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
15594 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
15596 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
15597 && OMP_CLAUSE_DECL (*pc) == decl)
15598 break;
15599 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
15600 && OMP_CLAUSE_DECL (*pc) == decl)
15602 if (j)
15603 break;
15604 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15605 tree c = *pc;
15606 *pc = OMP_CLAUSE_CHAIN (c);
15607 OMP_CLAUSE_CHAIN (c) = *clauses;
15608 *clauses = c;
15610 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
15611 && OMP_CLAUSE_DECL (*pc) == decl)
15613 error ("iteration variable %qD should not be firstprivate",
15614 decl);
15615 *pc = OMP_CLAUSE_CHAIN (*pc);
15617 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
15618 && OMP_CLAUSE_DECL (*pc) == decl)
15620 error ("iteration variable %qD should not be reduction",
15621 decl);
15622 *pc = OMP_CLAUSE_CHAIN (*pc);
15624 else
15625 pc = &OMP_CLAUSE_CHAIN (*pc);
15627 if (*pc)
15628 break;
15630 if (*pc == NULL_TREE)
15632 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
15633 OMP_CLAUSE_DECL (c) = decl;
15634 c = finish_omp_clauses (c, C_ORT_OMP);
15635 if (c)
15637 OMP_CLAUSE_CHAIN (c) = *clauses;
15638 *clauses = c;
15642 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
15643 if (COMPARISON_CLASS_P (cond))
15645 tree op0 = RECUR (TREE_OPERAND (cond, 0));
15646 tree op1 = RECUR (TREE_OPERAND (cond, 1));
15647 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
15649 else
15650 cond = RECUR (cond);
15651 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15652 switch (TREE_CODE (incr))
15654 case PREINCREMENT_EXPR:
15655 case PREDECREMENT_EXPR:
15656 case POSTINCREMENT_EXPR:
15657 case POSTDECREMENT_EXPR:
15658 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
15659 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
15660 break;
15661 case MODIFY_EXPR:
15662 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15663 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15665 tree rhs = TREE_OPERAND (incr, 1);
15666 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15667 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15668 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15669 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15670 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15671 rhs0, rhs1));
15673 else
15674 incr = RECUR (incr);
15675 break;
15676 case MODOP_EXPR:
15677 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15678 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15680 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15681 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15682 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
15683 TREE_TYPE (decl), lhs,
15684 RECUR (TREE_OPERAND (incr, 2))));
15686 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
15687 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
15688 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
15690 tree rhs = TREE_OPERAND (incr, 2);
15691 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15692 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15693 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15694 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15695 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15696 rhs0, rhs1));
15698 else
15699 incr = RECUR (incr);
15700 break;
15701 default:
15702 incr = RECUR (incr);
15703 break;
15706 TREE_VEC_ELT (declv, i) = decl;
15707 TREE_VEC_ELT (initv, i) = init;
15708 TREE_VEC_ELT (condv, i) = cond;
15709 TREE_VEC_ELT (incrv, i) = incr;
15710 #undef RECUR
15713 /* Helper function of tsubst_expr, find OMP_TEAMS inside
15714 of OMP_TARGET's body. */
15716 static tree
15717 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
15719 *walk_subtrees = 0;
15720 switch (TREE_CODE (*tp))
15722 case OMP_TEAMS:
15723 return *tp;
15724 case BIND_EXPR:
15725 case STATEMENT_LIST:
15726 *walk_subtrees = 1;
15727 break;
15728 default:
15729 break;
15731 return NULL_TREE;
15734 /* Helper function for tsubst_expr. For decomposition declaration
15735 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
15736 also the corresponding decls representing the identifiers
15737 of the decomposition declaration. Return DECL if successful
15738 or error_mark_node otherwise, set *FIRST to the first decl
15739 in the list chained through DECL_CHAIN and *CNT to the number
15740 of such decls. */
15742 static tree
15743 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
15744 tsubst_flags_t complain, tree in_decl, tree *first,
15745 unsigned int *cnt)
15747 tree decl2, decl3, prev = decl;
15748 *cnt = 0;
15749 gcc_assert (DECL_NAME (decl) == NULL_TREE);
15750 for (decl2 = DECL_CHAIN (pattern_decl);
15751 decl2
15752 && VAR_P (decl2)
15753 && DECL_DECOMPOSITION_P (decl2)
15754 && DECL_NAME (decl2);
15755 decl2 = DECL_CHAIN (decl2))
15757 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
15759 gcc_assert (errorcount);
15760 return error_mark_node;
15762 (*cnt)++;
15763 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
15764 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
15765 tree v = DECL_VALUE_EXPR (decl2);
15766 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
15767 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
15768 decl3 = tsubst (decl2, args, complain, in_decl);
15769 SET_DECL_VALUE_EXPR (decl2, v);
15770 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
15771 if (VAR_P (decl3))
15772 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
15773 maybe_push_decl (decl3);
15774 if (error_operand_p (decl3))
15775 decl = error_mark_node;
15776 else if (decl != error_mark_node
15777 && DECL_CHAIN (decl3) != prev)
15779 gcc_assert (errorcount);
15780 decl = error_mark_node;
15782 else
15783 prev = decl3;
15785 *first = prev;
15786 return decl;
15789 /* Like tsubst_copy for expressions, etc. but also does semantic
15790 processing. */
15792 tree
15793 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
15794 bool integral_constant_expression_p)
15796 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
15797 #define RECUR(NODE) \
15798 tsubst_expr ((NODE), args, complain, in_decl, \
15799 integral_constant_expression_p)
15801 tree stmt, tmp;
15802 tree r;
15803 location_t loc;
15805 if (t == NULL_TREE || t == error_mark_node)
15806 return t;
15808 loc = input_location;
15809 if (EXPR_HAS_LOCATION (t))
15810 input_location = EXPR_LOCATION (t);
15811 if (STATEMENT_CODE_P (TREE_CODE (t)))
15812 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
15814 switch (TREE_CODE (t))
15816 case STATEMENT_LIST:
15818 tree_stmt_iterator i;
15819 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
15820 RECUR (tsi_stmt (i));
15821 break;
15824 case CTOR_INITIALIZER:
15825 finish_mem_initializers (tsubst_initializer_list
15826 (TREE_OPERAND (t, 0), args));
15827 break;
15829 case RETURN_EXPR:
15830 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
15831 break;
15833 case EXPR_STMT:
15834 tmp = RECUR (EXPR_STMT_EXPR (t));
15835 if (EXPR_STMT_STMT_EXPR_RESULT (t))
15836 finish_stmt_expr_expr (tmp, cur_stmt_expr);
15837 else
15838 finish_expr_stmt (tmp);
15839 break;
15841 case USING_STMT:
15842 finish_local_using_directive (USING_STMT_NAMESPACE (t),
15843 /*attribs=*/NULL_TREE);
15844 break;
15846 case DECL_EXPR:
15848 tree decl, pattern_decl;
15849 tree init;
15851 pattern_decl = decl = DECL_EXPR_DECL (t);
15852 if (TREE_CODE (decl) == LABEL_DECL)
15853 finish_label_decl (DECL_NAME (decl));
15854 else if (TREE_CODE (decl) == USING_DECL)
15856 tree scope = USING_DECL_SCOPE (decl);
15857 tree name = DECL_NAME (decl);
15859 scope = tsubst (scope, args, complain, in_decl);
15860 decl = lookup_qualified_name (scope, name,
15861 /*is_type_p=*/false,
15862 /*complain=*/false);
15863 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
15864 qualified_name_lookup_error (scope, name, decl, input_location);
15865 else
15866 finish_local_using_decl (decl, scope, name);
15868 else if (DECL_PACK_P (decl))
15870 /* Don't build up decls for a variadic capture proxy, we'll
15871 instantiate the elements directly as needed. */
15872 break;
15874 else
15876 init = DECL_INITIAL (decl);
15877 decl = tsubst (decl, args, complain, in_decl);
15878 if (decl != error_mark_node)
15880 /* By marking the declaration as instantiated, we avoid
15881 trying to instantiate it. Since instantiate_decl can't
15882 handle local variables, and since we've already done
15883 all that needs to be done, that's the right thing to
15884 do. */
15885 if (VAR_P (decl))
15886 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
15887 if (VAR_P (decl)
15888 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
15889 /* Anonymous aggregates are a special case. */
15890 finish_anon_union (decl);
15891 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
15893 DECL_CONTEXT (decl) = current_function_decl;
15894 if (DECL_NAME (decl) == this_identifier)
15896 tree lam = DECL_CONTEXT (current_function_decl);
15897 lam = CLASSTYPE_LAMBDA_EXPR (lam);
15898 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
15900 insert_capture_proxy (decl);
15902 else if (DECL_IMPLICIT_TYPEDEF_P (t))
15903 /* We already did a pushtag. */;
15904 else if (TREE_CODE (decl) == FUNCTION_DECL
15905 && DECL_OMP_DECLARE_REDUCTION_P (decl)
15906 && DECL_FUNCTION_SCOPE_P (pattern_decl))
15908 DECL_CONTEXT (decl) = NULL_TREE;
15909 pushdecl (decl);
15910 DECL_CONTEXT (decl) = current_function_decl;
15911 cp_check_omp_declare_reduction (decl);
15913 else
15915 int const_init = false;
15916 maybe_push_decl (decl);
15917 if (VAR_P (decl)
15918 && DECL_PRETTY_FUNCTION_P (decl))
15920 /* For __PRETTY_FUNCTION__ we have to adjust the
15921 initializer. */
15922 const char *const name
15923 = cxx_printable_name (current_function_decl, 2);
15924 init = cp_fname_init (name, &TREE_TYPE (decl));
15926 else
15927 init = tsubst_init (init, decl, args, complain, in_decl);
15929 if (VAR_P (decl))
15930 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
15931 (pattern_decl));
15932 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
15933 if (VAR_P (decl)
15934 && DECL_DECOMPOSITION_P (decl)
15935 && TREE_TYPE (pattern_decl) != error_mark_node)
15937 unsigned int cnt;
15938 tree first;
15939 decl = tsubst_decomp_names (decl, pattern_decl, args,
15940 complain, in_decl, &first,
15941 &cnt);
15942 if (decl != error_mark_node)
15943 cp_finish_decomp (decl, first, cnt);
15949 break;
15952 case FOR_STMT:
15953 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
15954 RECUR (FOR_INIT_STMT (t));
15955 finish_init_stmt (stmt);
15956 tmp = RECUR (FOR_COND (t));
15957 finish_for_cond (tmp, stmt, false);
15958 tmp = RECUR (FOR_EXPR (t));
15959 finish_for_expr (tmp, stmt);
15960 RECUR (FOR_BODY (t));
15961 finish_for_stmt (stmt);
15962 break;
15964 case RANGE_FOR_STMT:
15966 tree decl, expr;
15967 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
15968 decl = RANGE_FOR_DECL (t);
15969 decl = tsubst (decl, args, complain, in_decl);
15970 maybe_push_decl (decl);
15971 expr = RECUR (RANGE_FOR_EXPR (t));
15972 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
15974 unsigned int cnt;
15975 tree first;
15976 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
15977 complain, in_decl, &first, &cnt);
15978 stmt = cp_convert_range_for (stmt, decl, expr, first, cnt,
15979 RANGE_FOR_IVDEP (t));
15981 else
15982 stmt = cp_convert_range_for (stmt, decl, expr, NULL_TREE, 0,
15983 RANGE_FOR_IVDEP (t));
15984 RECUR (RANGE_FOR_BODY (t));
15985 finish_for_stmt (stmt);
15987 break;
15989 case WHILE_STMT:
15990 stmt = begin_while_stmt ();
15991 tmp = RECUR (WHILE_COND (t));
15992 finish_while_stmt_cond (tmp, stmt, false);
15993 RECUR (WHILE_BODY (t));
15994 finish_while_stmt (stmt);
15995 break;
15997 case DO_STMT:
15998 stmt = begin_do_stmt ();
15999 RECUR (DO_BODY (t));
16000 finish_do_body (stmt);
16001 tmp = RECUR (DO_COND (t));
16002 finish_do_stmt (tmp, stmt, false);
16003 break;
16005 case IF_STMT:
16006 stmt = begin_if_stmt ();
16007 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
16008 tmp = RECUR (IF_COND (t));
16009 tmp = finish_if_stmt_cond (tmp, stmt);
16010 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
16011 /* Don't instantiate the THEN_CLAUSE. */;
16012 else
16014 bool inhibit = integer_zerop (fold_non_dependent_expr (tmp));
16015 if (inhibit)
16016 ++c_inhibit_evaluation_warnings;
16017 RECUR (THEN_CLAUSE (t));
16018 if (inhibit)
16019 --c_inhibit_evaluation_warnings;
16021 finish_then_clause (stmt);
16023 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
16024 /* Don't instantiate the ELSE_CLAUSE. */;
16025 else if (ELSE_CLAUSE (t))
16027 bool inhibit = integer_nonzerop (fold_non_dependent_expr (tmp));
16028 begin_else_clause (stmt);
16029 if (inhibit)
16030 ++c_inhibit_evaluation_warnings;
16031 RECUR (ELSE_CLAUSE (t));
16032 if (inhibit)
16033 --c_inhibit_evaluation_warnings;
16034 finish_else_clause (stmt);
16037 finish_if_stmt (stmt);
16038 break;
16040 case BIND_EXPR:
16041 if (BIND_EXPR_BODY_BLOCK (t))
16042 stmt = begin_function_body ();
16043 else
16044 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
16045 ? BCS_TRY_BLOCK : 0);
16047 RECUR (BIND_EXPR_BODY (t));
16049 if (BIND_EXPR_BODY_BLOCK (t))
16050 finish_function_body (stmt);
16051 else
16052 finish_compound_stmt (stmt);
16053 break;
16055 case BREAK_STMT:
16056 finish_break_stmt ();
16057 break;
16059 case CONTINUE_STMT:
16060 finish_continue_stmt ();
16061 break;
16063 case SWITCH_STMT:
16064 stmt = begin_switch_stmt ();
16065 tmp = RECUR (SWITCH_STMT_COND (t));
16066 finish_switch_cond (tmp, stmt);
16067 RECUR (SWITCH_STMT_BODY (t));
16068 finish_switch_stmt (stmt);
16069 break;
16071 case CASE_LABEL_EXPR:
16073 tree low = RECUR (CASE_LOW (t));
16074 tree high = RECUR (CASE_HIGH (t));
16075 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
16076 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
16077 FALLTHROUGH_LABEL_P (CASE_LABEL (l))
16078 = FALLTHROUGH_LABEL_P (CASE_LABEL (t));
16080 break;
16082 case LABEL_EXPR:
16084 tree decl = LABEL_EXPR_LABEL (t);
16085 tree label;
16087 label = finish_label_stmt (DECL_NAME (decl));
16088 if (TREE_CODE (label) == LABEL_DECL)
16089 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
16090 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
16091 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
16093 break;
16095 case GOTO_EXPR:
16096 tmp = GOTO_DESTINATION (t);
16097 if (TREE_CODE (tmp) != LABEL_DECL)
16098 /* Computed goto's must be tsubst'd into. On the other hand,
16099 non-computed gotos must not be; the identifier in question
16100 will have no binding. */
16101 tmp = RECUR (tmp);
16102 else
16103 tmp = DECL_NAME (tmp);
16104 finish_goto_stmt (tmp);
16105 break;
16107 case ASM_EXPR:
16109 tree string = RECUR (ASM_STRING (t));
16110 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
16111 complain, in_decl);
16112 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
16113 complain, in_decl);
16114 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
16115 complain, in_decl);
16116 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
16117 complain, in_decl);
16118 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
16119 clobbers, labels);
16120 tree asm_expr = tmp;
16121 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
16122 asm_expr = TREE_OPERAND (asm_expr, 0);
16123 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
16125 break;
16127 case TRY_BLOCK:
16128 if (CLEANUP_P (t))
16130 stmt = begin_try_block ();
16131 RECUR (TRY_STMTS (t));
16132 finish_cleanup_try_block (stmt);
16133 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
16135 else
16137 tree compound_stmt = NULL_TREE;
16139 if (FN_TRY_BLOCK_P (t))
16140 stmt = begin_function_try_block (&compound_stmt);
16141 else
16142 stmt = begin_try_block ();
16144 RECUR (TRY_STMTS (t));
16146 if (FN_TRY_BLOCK_P (t))
16147 finish_function_try_block (stmt);
16148 else
16149 finish_try_block (stmt);
16151 RECUR (TRY_HANDLERS (t));
16152 if (FN_TRY_BLOCK_P (t))
16153 finish_function_handler_sequence (stmt, compound_stmt);
16154 else
16155 finish_handler_sequence (stmt);
16157 break;
16159 case HANDLER:
16161 tree decl = HANDLER_PARMS (t);
16163 if (decl)
16165 decl = tsubst (decl, args, complain, in_decl);
16166 /* Prevent instantiate_decl from trying to instantiate
16167 this variable. We've already done all that needs to be
16168 done. */
16169 if (decl != error_mark_node)
16170 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16172 stmt = begin_handler ();
16173 finish_handler_parms (decl, stmt);
16174 RECUR (HANDLER_BODY (t));
16175 finish_handler (stmt);
16177 break;
16179 case TAG_DEFN:
16180 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
16181 if (CLASS_TYPE_P (tmp))
16183 /* Local classes are not independent templates; they are
16184 instantiated along with their containing function. And this
16185 way we don't have to deal with pushing out of one local class
16186 to instantiate a member of another local class. */
16187 tree fn;
16188 /* Closures are handled by the LAMBDA_EXPR. */
16189 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
16190 complete_type (tmp);
16191 for (fn = TYPE_METHODS (tmp); fn; fn = DECL_CHAIN (fn))
16192 if (!DECL_ARTIFICIAL (fn))
16193 instantiate_decl (fn, /*defer_ok=*/false,
16194 /*expl_inst_class=*/false);
16196 break;
16198 case STATIC_ASSERT:
16200 tree condition;
16202 ++c_inhibit_evaluation_warnings;
16203 condition =
16204 tsubst_expr (STATIC_ASSERT_CONDITION (t),
16205 args,
16206 complain, in_decl,
16207 /*integral_constant_expression_p=*/true);
16208 --c_inhibit_evaluation_warnings;
16210 finish_static_assert (condition,
16211 STATIC_ASSERT_MESSAGE (t),
16212 STATIC_ASSERT_SOURCE_LOCATION (t),
16213 /*member_p=*/false);
16215 break;
16217 case OACC_KERNELS:
16218 case OACC_PARALLEL:
16219 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
16220 in_decl);
16221 stmt = begin_omp_parallel ();
16222 RECUR (OMP_BODY (t));
16223 finish_omp_construct (TREE_CODE (t), stmt, tmp);
16224 break;
16226 case OMP_PARALLEL:
16227 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
16228 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
16229 complain, in_decl);
16230 if (OMP_PARALLEL_COMBINED (t))
16231 omp_parallel_combined_clauses = &tmp;
16232 stmt = begin_omp_parallel ();
16233 RECUR (OMP_PARALLEL_BODY (t));
16234 gcc_assert (omp_parallel_combined_clauses == NULL);
16235 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
16236 = OMP_PARALLEL_COMBINED (t);
16237 pop_omp_privatization_clauses (r);
16238 break;
16240 case OMP_TASK:
16241 r = push_omp_privatization_clauses (false);
16242 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
16243 complain, in_decl);
16244 stmt = begin_omp_task ();
16245 RECUR (OMP_TASK_BODY (t));
16246 finish_omp_task (tmp, stmt);
16247 pop_omp_privatization_clauses (r);
16248 break;
16250 case OMP_FOR:
16251 case OMP_SIMD:
16252 case CILK_SIMD:
16253 case CILK_FOR:
16254 case OMP_DISTRIBUTE:
16255 case OMP_TASKLOOP:
16256 case OACC_LOOP:
16258 tree clauses, body, pre_body;
16259 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
16260 tree orig_declv = NULL_TREE;
16261 tree incrv = NULL_TREE;
16262 enum c_omp_region_type ort = C_ORT_OMP;
16263 int i;
16265 if (TREE_CODE (t) == CILK_SIMD || TREE_CODE (t) == CILK_FOR)
16266 ort = C_ORT_CILK;
16267 else if (TREE_CODE (t) == OACC_LOOP)
16268 ort = C_ORT_ACC;
16270 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
16271 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
16272 in_decl);
16273 if (OMP_FOR_INIT (t) != NULL_TREE)
16275 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16276 if (OMP_FOR_ORIG_DECLS (t))
16277 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16278 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16279 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16280 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16283 stmt = begin_omp_structured_block ();
16285 pre_body = push_stmt_list ();
16286 RECUR (OMP_FOR_PRE_BODY (t));
16287 pre_body = pop_stmt_list (pre_body);
16289 if (OMP_FOR_INIT (t) != NULL_TREE)
16290 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
16291 tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, condv,
16292 incrv, &clauses, args, complain, in_decl,
16293 integral_constant_expression_p);
16294 omp_parallel_combined_clauses = NULL;
16296 body = push_stmt_list ();
16297 RECUR (OMP_FOR_BODY (t));
16298 body = pop_stmt_list (body);
16300 if (OMP_FOR_INIT (t) != NULL_TREE)
16301 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
16302 orig_declv, initv, condv, incrv, body, pre_body,
16303 NULL, clauses);
16304 else
16306 t = make_node (TREE_CODE (t));
16307 TREE_TYPE (t) = void_type_node;
16308 OMP_FOR_BODY (t) = body;
16309 OMP_FOR_PRE_BODY (t) = pre_body;
16310 OMP_FOR_CLAUSES (t) = clauses;
16311 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
16312 add_stmt (t);
16315 add_stmt (finish_omp_structured_block (stmt));
16316 pop_omp_privatization_clauses (r);
16318 break;
16320 case OMP_SECTIONS:
16321 omp_parallel_combined_clauses = NULL;
16322 /* FALLTHRU */
16323 case OMP_SINGLE:
16324 case OMP_TEAMS:
16325 case OMP_CRITICAL:
16326 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
16327 && OMP_TEAMS_COMBINED (t));
16328 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
16329 in_decl);
16330 stmt = push_stmt_list ();
16331 RECUR (OMP_BODY (t));
16332 stmt = pop_stmt_list (stmt);
16334 t = copy_node (t);
16335 OMP_BODY (t) = stmt;
16336 OMP_CLAUSES (t) = tmp;
16337 add_stmt (t);
16338 pop_omp_privatization_clauses (r);
16339 break;
16341 case OACC_DATA:
16342 case OMP_TARGET_DATA:
16343 case OMP_TARGET:
16344 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
16345 ? C_ORT_ACC : C_ORT_OMP, args, complain,
16346 in_decl);
16347 keep_next_level (true);
16348 stmt = begin_omp_structured_block ();
16350 RECUR (OMP_BODY (t));
16351 stmt = finish_omp_structured_block (stmt);
16353 t = copy_node (t);
16354 OMP_BODY (t) = stmt;
16355 OMP_CLAUSES (t) = tmp;
16356 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
16358 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
16359 if (teams)
16361 /* For combined target teams, ensure the num_teams and
16362 thread_limit clause expressions are evaluated on the host,
16363 before entering the target construct. */
16364 tree c;
16365 for (c = OMP_TEAMS_CLAUSES (teams);
16366 c; c = OMP_CLAUSE_CHAIN (c))
16367 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
16368 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
16369 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
16371 tree expr = OMP_CLAUSE_OPERAND (c, 0);
16372 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
16373 if (expr == error_mark_node)
16374 continue;
16375 tmp = TARGET_EXPR_SLOT (expr);
16376 add_stmt (expr);
16377 OMP_CLAUSE_OPERAND (c, 0) = expr;
16378 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16379 OMP_CLAUSE_FIRSTPRIVATE);
16380 OMP_CLAUSE_DECL (tc) = tmp;
16381 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
16382 OMP_TARGET_CLAUSES (t) = tc;
16386 add_stmt (t);
16387 break;
16389 case OACC_DECLARE:
16390 t = copy_node (t);
16391 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
16392 complain, in_decl);
16393 OACC_DECLARE_CLAUSES (t) = tmp;
16394 add_stmt (t);
16395 break;
16397 case OMP_TARGET_UPDATE:
16398 case OMP_TARGET_ENTER_DATA:
16399 case OMP_TARGET_EXIT_DATA:
16400 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
16401 complain, in_decl);
16402 t = copy_node (t);
16403 OMP_STANDALONE_CLAUSES (t) = tmp;
16404 add_stmt (t);
16405 break;
16407 case OACC_ENTER_DATA:
16408 case OACC_EXIT_DATA:
16409 case OACC_UPDATE:
16410 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
16411 complain, in_decl);
16412 t = copy_node (t);
16413 OMP_STANDALONE_CLAUSES (t) = tmp;
16414 add_stmt (t);
16415 break;
16417 case OMP_ORDERED:
16418 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
16419 complain, in_decl);
16420 stmt = push_stmt_list ();
16421 RECUR (OMP_BODY (t));
16422 stmt = pop_stmt_list (stmt);
16424 t = copy_node (t);
16425 OMP_BODY (t) = stmt;
16426 OMP_ORDERED_CLAUSES (t) = tmp;
16427 add_stmt (t);
16428 break;
16430 case OMP_SECTION:
16431 case OMP_MASTER:
16432 case OMP_TASKGROUP:
16433 stmt = push_stmt_list ();
16434 RECUR (OMP_BODY (t));
16435 stmt = pop_stmt_list (stmt);
16437 t = copy_node (t);
16438 OMP_BODY (t) = stmt;
16439 add_stmt (t);
16440 break;
16442 case OMP_ATOMIC:
16443 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
16444 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
16446 tree op1 = TREE_OPERAND (t, 1);
16447 tree rhs1 = NULL_TREE;
16448 tree lhs, rhs;
16449 if (TREE_CODE (op1) == COMPOUND_EXPR)
16451 rhs1 = RECUR (TREE_OPERAND (op1, 0));
16452 op1 = TREE_OPERAND (op1, 1);
16454 lhs = RECUR (TREE_OPERAND (op1, 0));
16455 rhs = RECUR (TREE_OPERAND (op1, 1));
16456 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
16457 NULL_TREE, NULL_TREE, rhs1,
16458 OMP_ATOMIC_SEQ_CST (t));
16460 else
16462 tree op1 = TREE_OPERAND (t, 1);
16463 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
16464 tree rhs1 = NULL_TREE;
16465 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
16466 enum tree_code opcode = NOP_EXPR;
16467 if (code == OMP_ATOMIC_READ)
16469 v = RECUR (TREE_OPERAND (op1, 0));
16470 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16472 else if (code == OMP_ATOMIC_CAPTURE_OLD
16473 || code == OMP_ATOMIC_CAPTURE_NEW)
16475 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
16476 v = RECUR (TREE_OPERAND (op1, 0));
16477 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16478 if (TREE_CODE (op11) == COMPOUND_EXPR)
16480 rhs1 = RECUR (TREE_OPERAND (op11, 0));
16481 op11 = TREE_OPERAND (op11, 1);
16483 lhs = RECUR (TREE_OPERAND (op11, 0));
16484 rhs = RECUR (TREE_OPERAND (op11, 1));
16485 opcode = TREE_CODE (op11);
16486 if (opcode == MODIFY_EXPR)
16487 opcode = NOP_EXPR;
16489 else
16491 code = OMP_ATOMIC;
16492 lhs = RECUR (TREE_OPERAND (op1, 0));
16493 rhs = RECUR (TREE_OPERAND (op1, 1));
16495 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
16496 OMP_ATOMIC_SEQ_CST (t));
16498 break;
16500 case TRANSACTION_EXPR:
16502 int flags = 0;
16503 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
16504 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
16506 if (TRANSACTION_EXPR_IS_STMT (t))
16508 tree body = TRANSACTION_EXPR_BODY (t);
16509 tree noex = NULL_TREE;
16510 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
16512 noex = MUST_NOT_THROW_COND (body);
16513 if (noex == NULL_TREE)
16514 noex = boolean_true_node;
16515 body = TREE_OPERAND (body, 0);
16517 stmt = begin_transaction_stmt (input_location, NULL, flags);
16518 RECUR (body);
16519 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
16521 else
16523 stmt = build_transaction_expr (EXPR_LOCATION (t),
16524 RECUR (TRANSACTION_EXPR_BODY (t)),
16525 flags, NULL_TREE);
16526 RETURN (stmt);
16529 break;
16531 case MUST_NOT_THROW_EXPR:
16533 tree op0 = RECUR (TREE_OPERAND (t, 0));
16534 tree cond = RECUR (MUST_NOT_THROW_COND (t));
16535 RETURN (build_must_not_throw_expr (op0, cond));
16538 case EXPR_PACK_EXPANSION:
16539 error ("invalid use of pack expansion expression");
16540 RETURN (error_mark_node);
16542 case NONTYPE_ARGUMENT_PACK:
16543 error ("use %<...%> to expand argument pack");
16544 RETURN (error_mark_node);
16546 case CILK_SPAWN_STMT:
16547 cfun->calls_cilk_spawn = 1;
16548 RETURN (build_cilk_spawn (EXPR_LOCATION (t), RECUR (CILK_SPAWN_FN (t))));
16550 case CILK_SYNC_STMT:
16551 RETURN (build_cilk_sync ());
16553 case COMPOUND_EXPR:
16554 tmp = RECUR (TREE_OPERAND (t, 0));
16555 if (tmp == NULL_TREE)
16556 /* If the first operand was a statement, we're done with it. */
16557 RETURN (RECUR (TREE_OPERAND (t, 1)));
16558 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
16559 RECUR (TREE_OPERAND (t, 1)),
16560 complain));
16562 case ANNOTATE_EXPR:
16563 tmp = RECUR (TREE_OPERAND (t, 0));
16564 RETURN (build2_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
16565 TREE_TYPE (tmp), tmp, RECUR (TREE_OPERAND (t, 1))));
16567 default:
16568 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
16570 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
16571 /*function_p=*/false,
16572 integral_constant_expression_p));
16575 RETURN (NULL_TREE);
16576 out:
16577 input_location = loc;
16578 return r;
16579 #undef RECUR
16580 #undef RETURN
16583 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
16584 function. For description of the body see comment above
16585 cp_parser_omp_declare_reduction_exprs. */
16587 static void
16588 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16590 if (t == NULL_TREE || t == error_mark_node)
16591 return;
16593 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
16595 tree_stmt_iterator tsi;
16596 int i;
16597 tree stmts[7];
16598 memset (stmts, 0, sizeof stmts);
16599 for (i = 0, tsi = tsi_start (t);
16600 i < 7 && !tsi_end_p (tsi);
16601 i++, tsi_next (&tsi))
16602 stmts[i] = tsi_stmt (tsi);
16603 gcc_assert (tsi_end_p (tsi));
16605 if (i >= 3)
16607 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
16608 && TREE_CODE (stmts[1]) == DECL_EXPR);
16609 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
16610 args, complain, in_decl);
16611 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
16612 args, complain, in_decl);
16613 DECL_CONTEXT (omp_out) = current_function_decl;
16614 DECL_CONTEXT (omp_in) = current_function_decl;
16615 keep_next_level (true);
16616 tree block = begin_omp_structured_block ();
16617 tsubst_expr (stmts[2], args, complain, in_decl, false);
16618 block = finish_omp_structured_block (block);
16619 block = maybe_cleanup_point_expr_void (block);
16620 add_decl_expr (omp_out);
16621 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
16622 TREE_NO_WARNING (omp_out) = 1;
16623 add_decl_expr (omp_in);
16624 finish_expr_stmt (block);
16626 if (i >= 6)
16628 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
16629 && TREE_CODE (stmts[4]) == DECL_EXPR);
16630 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
16631 args, complain, in_decl);
16632 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
16633 args, complain, in_decl);
16634 DECL_CONTEXT (omp_priv) = current_function_decl;
16635 DECL_CONTEXT (omp_orig) = current_function_decl;
16636 keep_next_level (true);
16637 tree block = begin_omp_structured_block ();
16638 tsubst_expr (stmts[5], args, complain, in_decl, false);
16639 block = finish_omp_structured_block (block);
16640 block = maybe_cleanup_point_expr_void (block);
16641 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
16642 add_decl_expr (omp_priv);
16643 add_decl_expr (omp_orig);
16644 finish_expr_stmt (block);
16645 if (i == 7)
16646 add_decl_expr (omp_orig);
16650 /* T is a postfix-expression that is not being used in a function
16651 call. Return the substituted version of T. */
16653 static tree
16654 tsubst_non_call_postfix_expression (tree t, tree args,
16655 tsubst_flags_t complain,
16656 tree in_decl)
16658 if (TREE_CODE (t) == SCOPE_REF)
16659 t = tsubst_qualified_id (t, args, complain, in_decl,
16660 /*done=*/false, /*address_p=*/false);
16661 else
16662 t = tsubst_copy_and_build (t, args, complain, in_decl,
16663 /*function_p=*/false,
16664 /*integral_constant_expression_p=*/false);
16666 return t;
16669 /* Like tsubst but deals with expressions and performs semantic
16670 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
16672 tree
16673 tsubst_copy_and_build (tree t,
16674 tree args,
16675 tsubst_flags_t complain,
16676 tree in_decl,
16677 bool function_p,
16678 bool integral_constant_expression_p)
16680 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
16681 #define RECUR(NODE) \
16682 tsubst_copy_and_build (NODE, args, complain, in_decl, \
16683 /*function_p=*/false, \
16684 integral_constant_expression_p)
16686 tree retval, op1;
16687 location_t loc;
16689 if (t == NULL_TREE || t == error_mark_node)
16690 return t;
16692 loc = input_location;
16693 if (EXPR_HAS_LOCATION (t))
16694 input_location = EXPR_LOCATION (t);
16696 /* N3276 decltype magic only applies to calls at the top level or on the
16697 right side of a comma. */
16698 tsubst_flags_t decltype_flag = (complain & tf_decltype);
16699 complain &= ~tf_decltype;
16701 switch (TREE_CODE (t))
16703 case USING_DECL:
16704 t = DECL_NAME (t);
16705 /* Fall through. */
16706 case IDENTIFIER_NODE:
16708 tree decl;
16709 cp_id_kind idk;
16710 bool non_integral_constant_expression_p;
16711 const char *error_msg;
16713 if (IDENTIFIER_CONV_OP_P (t))
16715 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16716 t = mangle_conv_op_name_for_type (new_type);
16719 /* Look up the name. */
16720 decl = lookup_name (t);
16722 /* By convention, expressions use ERROR_MARK_NODE to indicate
16723 failure, not NULL_TREE. */
16724 if (decl == NULL_TREE)
16725 decl = error_mark_node;
16727 decl = finish_id_expression (t, decl, NULL_TREE,
16728 &idk,
16729 integral_constant_expression_p,
16730 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
16731 &non_integral_constant_expression_p,
16732 /*template_p=*/false,
16733 /*done=*/true,
16734 /*address_p=*/false,
16735 /*template_arg_p=*/false,
16736 &error_msg,
16737 input_location);
16738 if (error_msg)
16739 error (error_msg);
16740 if (!function_p && identifier_p (decl))
16742 if (complain & tf_error)
16743 unqualified_name_lookup_error (decl);
16744 decl = error_mark_node;
16746 RETURN (decl);
16749 case TEMPLATE_ID_EXPR:
16751 tree object;
16752 tree templ = RECUR (TREE_OPERAND (t, 0));
16753 tree targs = TREE_OPERAND (t, 1);
16755 if (targs)
16756 targs = tsubst_template_args (targs, args, complain, in_decl);
16757 if (targs == error_mark_node)
16758 return error_mark_node;
16760 if (TREE_CODE (templ) == SCOPE_REF)
16762 tree name = TREE_OPERAND (templ, 1);
16763 tree tid = lookup_template_function (name, targs);
16764 TREE_OPERAND (templ, 1) = tid;
16765 return templ;
16768 if (variable_template_p (templ))
16769 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
16771 if (TREE_CODE (templ) == COMPONENT_REF)
16773 object = TREE_OPERAND (templ, 0);
16774 templ = TREE_OPERAND (templ, 1);
16776 else
16777 object = NULL_TREE;
16778 templ = lookup_template_function (templ, targs);
16780 if (object)
16781 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
16782 object, templ, NULL_TREE));
16783 else
16784 RETURN (baselink_for_fns (templ));
16787 case INDIRECT_REF:
16789 tree r = RECUR (TREE_OPERAND (t, 0));
16791 if (REFERENCE_REF_P (t))
16793 /* A type conversion to reference type will be enclosed in
16794 such an indirect ref, but the substitution of the cast
16795 will have also added such an indirect ref. */
16796 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
16797 r = convert_from_reference (r);
16799 else
16800 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
16801 complain|decltype_flag);
16803 if (TREE_CODE (r) == INDIRECT_REF)
16804 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
16806 RETURN (r);
16809 case NOP_EXPR:
16811 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16812 tree op0 = RECUR (TREE_OPERAND (t, 0));
16813 RETURN (build_nop (type, op0));
16816 case IMPLICIT_CONV_EXPR:
16818 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16819 tree expr = RECUR (TREE_OPERAND (t, 0));
16820 int flags = LOOKUP_IMPLICIT;
16821 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
16822 flags = LOOKUP_NORMAL;
16823 RETURN (perform_implicit_conversion_flags (type, expr, complain,
16824 flags));
16827 case CONVERT_EXPR:
16829 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16830 tree op0 = RECUR (TREE_OPERAND (t, 0));
16831 RETURN (build1 (CONVERT_EXPR, type, op0));
16834 case CAST_EXPR:
16835 case REINTERPRET_CAST_EXPR:
16836 case CONST_CAST_EXPR:
16837 case DYNAMIC_CAST_EXPR:
16838 case STATIC_CAST_EXPR:
16840 tree type;
16841 tree op, r = NULL_TREE;
16843 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16844 if (integral_constant_expression_p
16845 && !cast_valid_in_integral_constant_expression_p (type))
16847 if (complain & tf_error)
16848 error ("a cast to a type other than an integral or "
16849 "enumeration type cannot appear in a constant-expression");
16850 RETURN (error_mark_node);
16853 op = RECUR (TREE_OPERAND (t, 0));
16855 warning_sentinel s(warn_useless_cast);
16856 switch (TREE_CODE (t))
16858 case CAST_EXPR:
16859 r = build_functional_cast (type, op, complain);
16860 break;
16861 case REINTERPRET_CAST_EXPR:
16862 r = build_reinterpret_cast (type, op, complain);
16863 break;
16864 case CONST_CAST_EXPR:
16865 r = build_const_cast (type, op, complain);
16866 break;
16867 case DYNAMIC_CAST_EXPR:
16868 r = build_dynamic_cast (type, op, complain);
16869 break;
16870 case STATIC_CAST_EXPR:
16871 r = build_static_cast (type, op, complain);
16872 break;
16873 default:
16874 gcc_unreachable ();
16877 RETURN (r);
16880 case POSTDECREMENT_EXPR:
16881 case POSTINCREMENT_EXPR:
16882 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16883 args, complain, in_decl);
16884 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
16885 complain|decltype_flag));
16887 case PREDECREMENT_EXPR:
16888 case PREINCREMENT_EXPR:
16889 case NEGATE_EXPR:
16890 case BIT_NOT_EXPR:
16891 case ABS_EXPR:
16892 case TRUTH_NOT_EXPR:
16893 case UNARY_PLUS_EXPR: /* Unary + */
16894 case REALPART_EXPR:
16895 case IMAGPART_EXPR:
16896 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
16897 RECUR (TREE_OPERAND (t, 0)),
16898 complain|decltype_flag));
16900 case FIX_TRUNC_EXPR:
16901 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
16902 false, complain));
16904 case ADDR_EXPR:
16905 op1 = TREE_OPERAND (t, 0);
16906 if (TREE_CODE (op1) == LABEL_DECL)
16907 RETURN (finish_label_address_expr (DECL_NAME (op1),
16908 EXPR_LOCATION (op1)));
16909 if (TREE_CODE (op1) == SCOPE_REF)
16910 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
16911 /*done=*/true, /*address_p=*/true);
16912 else
16913 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
16914 in_decl);
16915 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
16916 complain|decltype_flag));
16918 case PLUS_EXPR:
16919 case MINUS_EXPR:
16920 case MULT_EXPR:
16921 case TRUNC_DIV_EXPR:
16922 case CEIL_DIV_EXPR:
16923 case FLOOR_DIV_EXPR:
16924 case ROUND_DIV_EXPR:
16925 case EXACT_DIV_EXPR:
16926 case BIT_AND_EXPR:
16927 case BIT_IOR_EXPR:
16928 case BIT_XOR_EXPR:
16929 case TRUNC_MOD_EXPR:
16930 case FLOOR_MOD_EXPR:
16931 case TRUTH_ANDIF_EXPR:
16932 case TRUTH_ORIF_EXPR:
16933 case TRUTH_AND_EXPR:
16934 case TRUTH_OR_EXPR:
16935 case RSHIFT_EXPR:
16936 case LSHIFT_EXPR:
16937 case RROTATE_EXPR:
16938 case LROTATE_EXPR:
16939 case EQ_EXPR:
16940 case NE_EXPR:
16941 case MAX_EXPR:
16942 case MIN_EXPR:
16943 case LE_EXPR:
16944 case GE_EXPR:
16945 case LT_EXPR:
16946 case GT_EXPR:
16947 case MEMBER_REF:
16948 case DOTSTAR_EXPR:
16950 warning_sentinel s1(warn_type_limits);
16951 warning_sentinel s2(warn_div_by_zero);
16952 warning_sentinel s3(warn_logical_op);
16953 warning_sentinel s4(warn_tautological_compare);
16954 tree op0 = RECUR (TREE_OPERAND (t, 0));
16955 tree op1 = RECUR (TREE_OPERAND (t, 1));
16956 tree r = build_x_binary_op
16957 (input_location, TREE_CODE (t),
16958 op0,
16959 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
16960 ? ERROR_MARK
16961 : TREE_CODE (TREE_OPERAND (t, 0))),
16962 op1,
16963 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
16964 ? ERROR_MARK
16965 : TREE_CODE (TREE_OPERAND (t, 1))),
16966 /*overload=*/NULL,
16967 complain|decltype_flag);
16968 if (EXPR_P (r) && TREE_NO_WARNING (t))
16969 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16971 RETURN (r);
16974 case POINTER_PLUS_EXPR:
16976 tree op0 = RECUR (TREE_OPERAND (t, 0));
16977 tree op1 = RECUR (TREE_OPERAND (t, 1));
16978 return fold_build_pointer_plus (op0, op1);
16981 case SCOPE_REF:
16982 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
16983 /*address_p=*/false));
16984 case ARRAY_REF:
16985 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16986 args, complain, in_decl);
16987 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
16988 RECUR (TREE_OPERAND (t, 1)),
16989 complain|decltype_flag));
16991 case ARRAY_NOTATION_REF:
16993 tree start_index, length, stride;
16994 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
16995 args, complain, in_decl);
16996 start_index = RECUR (ARRAY_NOTATION_START (t));
16997 length = RECUR (ARRAY_NOTATION_LENGTH (t));
16998 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
16999 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
17000 length, stride, TREE_TYPE (op1)));
17002 case SIZEOF_EXPR:
17003 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
17004 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
17005 RETURN (tsubst_copy (t, args, complain, in_decl));
17006 /* Fall through */
17008 case ALIGNOF_EXPR:
17010 tree r;
17012 op1 = TREE_OPERAND (t, 0);
17013 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
17014 op1 = TREE_TYPE (op1);
17015 if (!args)
17017 /* When there are no ARGS, we are trying to evaluate a
17018 non-dependent expression from the parser. Trying to do
17019 the substitutions may not work. */
17020 if (!TYPE_P (op1))
17021 op1 = TREE_TYPE (op1);
17023 else
17025 ++cp_unevaluated_operand;
17026 ++c_inhibit_evaluation_warnings;
17027 if (TYPE_P (op1))
17028 op1 = tsubst (op1, args, complain, in_decl);
17029 else
17030 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17031 /*function_p=*/false,
17032 /*integral_constant_expression_p=*/
17033 false);
17034 --cp_unevaluated_operand;
17035 --c_inhibit_evaluation_warnings;
17037 if (TYPE_P (op1))
17038 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
17039 complain & tf_error);
17040 else
17041 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
17042 complain & tf_error);
17043 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
17045 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
17047 if (!processing_template_decl && TYPE_P (op1))
17049 r = build_min (SIZEOF_EXPR, size_type_node,
17050 build1 (NOP_EXPR, op1, error_mark_node));
17051 SIZEOF_EXPR_TYPE_P (r) = 1;
17053 else
17054 r = build_min (SIZEOF_EXPR, size_type_node, op1);
17055 TREE_SIDE_EFFECTS (r) = 0;
17056 TREE_READONLY (r) = 1;
17058 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
17060 RETURN (r);
17063 case AT_ENCODE_EXPR:
17065 op1 = TREE_OPERAND (t, 0);
17066 ++cp_unevaluated_operand;
17067 ++c_inhibit_evaluation_warnings;
17068 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17069 /*function_p=*/false,
17070 /*integral_constant_expression_p=*/false);
17071 --cp_unevaluated_operand;
17072 --c_inhibit_evaluation_warnings;
17073 RETURN (objc_build_encode_expr (op1));
17076 case NOEXCEPT_EXPR:
17077 op1 = TREE_OPERAND (t, 0);
17078 ++cp_unevaluated_operand;
17079 ++c_inhibit_evaluation_warnings;
17080 ++cp_noexcept_operand;
17081 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17082 /*function_p=*/false,
17083 /*integral_constant_expression_p=*/false);
17084 --cp_unevaluated_operand;
17085 --c_inhibit_evaluation_warnings;
17086 --cp_noexcept_operand;
17087 RETURN (finish_noexcept_expr (op1, complain));
17089 case MODOP_EXPR:
17091 warning_sentinel s(warn_div_by_zero);
17092 tree lhs = RECUR (TREE_OPERAND (t, 0));
17093 tree rhs = RECUR (TREE_OPERAND (t, 2));
17094 tree r = build_x_modify_expr
17095 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
17096 complain|decltype_flag);
17097 /* TREE_NO_WARNING must be set if either the expression was
17098 parenthesized or it uses an operator such as >>= rather
17099 than plain assignment. In the former case, it was already
17100 set and must be copied. In the latter case,
17101 build_x_modify_expr sets it and it must not be reset
17102 here. */
17103 if (TREE_NO_WARNING (t))
17104 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17106 RETURN (r);
17109 case ARROW_EXPR:
17110 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17111 args, complain, in_decl);
17112 /* Remember that there was a reference to this entity. */
17113 if (DECL_P (op1)
17114 && !mark_used (op1, complain) && !(complain & tf_error))
17115 RETURN (error_mark_node);
17116 RETURN (build_x_arrow (input_location, op1, complain));
17118 case NEW_EXPR:
17120 tree placement = RECUR (TREE_OPERAND (t, 0));
17121 tree init = RECUR (TREE_OPERAND (t, 3));
17122 vec<tree, va_gc> *placement_vec;
17123 vec<tree, va_gc> *init_vec;
17124 tree ret;
17126 if (placement == NULL_TREE)
17127 placement_vec = NULL;
17128 else
17130 placement_vec = make_tree_vector ();
17131 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
17132 vec_safe_push (placement_vec, TREE_VALUE (placement));
17135 /* If there was an initializer in the original tree, but it
17136 instantiated to an empty list, then we should pass a
17137 non-NULL empty vector to tell build_new that it was an
17138 empty initializer() rather than no initializer. This can
17139 only happen when the initializer is a pack expansion whose
17140 parameter packs are of length zero. */
17141 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
17142 init_vec = NULL;
17143 else
17145 init_vec = make_tree_vector ();
17146 if (init == void_node)
17147 gcc_assert (init_vec != NULL);
17148 else
17150 for (; init != NULL_TREE; init = TREE_CHAIN (init))
17151 vec_safe_push (init_vec, TREE_VALUE (init));
17155 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
17156 tree op2 = RECUR (TREE_OPERAND (t, 2));
17157 ret = build_new (&placement_vec, op1, op2, &init_vec,
17158 NEW_EXPR_USE_GLOBAL (t),
17159 complain);
17161 if (placement_vec != NULL)
17162 release_tree_vector (placement_vec);
17163 if (init_vec != NULL)
17164 release_tree_vector (init_vec);
17166 RETURN (ret);
17169 case DELETE_EXPR:
17171 tree op0 = RECUR (TREE_OPERAND (t, 0));
17172 tree op1 = RECUR (TREE_OPERAND (t, 1));
17173 RETURN (delete_sanity (op0, op1,
17174 DELETE_EXPR_USE_VEC (t),
17175 DELETE_EXPR_USE_GLOBAL (t),
17176 complain));
17179 case COMPOUND_EXPR:
17181 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
17182 complain & ~tf_decltype, in_decl,
17183 /*function_p=*/false,
17184 integral_constant_expression_p);
17185 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
17186 op0,
17187 RECUR (TREE_OPERAND (t, 1)),
17188 complain|decltype_flag));
17191 case CALL_EXPR:
17193 tree function;
17194 vec<tree, va_gc> *call_args;
17195 unsigned int nargs, i;
17196 bool qualified_p;
17197 bool koenig_p;
17198 tree ret;
17200 function = CALL_EXPR_FN (t);
17201 /* Internal function with no arguments. */
17202 if (function == NULL_TREE && call_expr_nargs (t) == 0)
17203 RETURN (t);
17205 /* When we parsed the expression, we determined whether or
17206 not Koenig lookup should be performed. */
17207 koenig_p = KOENIG_LOOKUP_P (t);
17208 if (function == NULL_TREE)
17210 koenig_p = false;
17211 qualified_p = false;
17213 else if (TREE_CODE (function) == SCOPE_REF)
17215 qualified_p = true;
17216 function = tsubst_qualified_id (function, args, complain, in_decl,
17217 /*done=*/false,
17218 /*address_p=*/false);
17220 else if (koenig_p && identifier_p (function))
17222 /* Do nothing; calling tsubst_copy_and_build on an identifier
17223 would incorrectly perform unqualified lookup again.
17225 Note that we can also have an IDENTIFIER_NODE if the earlier
17226 unqualified lookup found a member function; in that case
17227 koenig_p will be false and we do want to do the lookup
17228 again to find the instantiated member function.
17230 FIXME but doing that causes c++/15272, so we need to stop
17231 using IDENTIFIER_NODE in that situation. */
17232 qualified_p = false;
17234 else
17236 if (TREE_CODE (function) == COMPONENT_REF)
17238 tree op = TREE_OPERAND (function, 1);
17240 qualified_p = (TREE_CODE (op) == SCOPE_REF
17241 || (BASELINK_P (op)
17242 && BASELINK_QUALIFIED_P (op)));
17244 else
17245 qualified_p = false;
17247 if (TREE_CODE (function) == ADDR_EXPR
17248 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
17249 /* Avoid error about taking the address of a constructor. */
17250 function = TREE_OPERAND (function, 0);
17252 function = tsubst_copy_and_build (function, args, complain,
17253 in_decl,
17254 !qualified_p,
17255 integral_constant_expression_p);
17257 if (BASELINK_P (function))
17258 qualified_p = true;
17261 nargs = call_expr_nargs (t);
17262 call_args = make_tree_vector ();
17263 for (i = 0; i < nargs; ++i)
17265 tree arg = CALL_EXPR_ARG (t, i);
17267 if (!PACK_EXPANSION_P (arg))
17268 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
17269 else
17271 /* Expand the pack expansion and push each entry onto
17272 CALL_ARGS. */
17273 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
17274 if (TREE_CODE (arg) == TREE_VEC)
17276 unsigned int len, j;
17278 len = TREE_VEC_LENGTH (arg);
17279 for (j = 0; j < len; ++j)
17281 tree value = TREE_VEC_ELT (arg, j);
17282 if (value != NULL_TREE)
17283 value = convert_from_reference (value);
17284 vec_safe_push (call_args, value);
17287 else
17289 /* A partial substitution. Add one entry. */
17290 vec_safe_push (call_args, arg);
17295 /* We do not perform argument-dependent lookup if normal
17296 lookup finds a non-function, in accordance with the
17297 expected resolution of DR 218. */
17298 if (koenig_p
17299 && ((is_overloaded_fn (function)
17300 /* If lookup found a member function, the Koenig lookup is
17301 not appropriate, even if an unqualified-name was used
17302 to denote the function. */
17303 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
17304 || identifier_p (function))
17305 /* Only do this when substitution turns a dependent call
17306 into a non-dependent call. */
17307 && type_dependent_expression_p_push (t)
17308 && !any_type_dependent_arguments_p (call_args))
17309 function = perform_koenig_lookup (function, call_args, tf_none);
17311 if (function != NULL_TREE
17312 && identifier_p (function)
17313 && !any_type_dependent_arguments_p (call_args))
17315 if (koenig_p && (complain & tf_warning_or_error))
17317 /* For backwards compatibility and good diagnostics, try
17318 the unqualified lookup again if we aren't in SFINAE
17319 context. */
17320 tree unq = (tsubst_copy_and_build
17321 (function, args, complain, in_decl, true,
17322 integral_constant_expression_p));
17323 if (unq == error_mark_node)
17325 release_tree_vector (call_args);
17326 RETURN (error_mark_node);
17329 if (unq != function)
17331 /* In a lambda fn, we have to be careful to not
17332 introduce new this captures. Legacy code can't
17333 be using lambdas anyway, so it's ok to be
17334 stricter. */
17335 bool in_lambda = (current_class_type
17336 && LAMBDA_TYPE_P (current_class_type));
17337 char const *const msg
17338 = G_("%qD was not declared in this scope, "
17339 "and no declarations were found by "
17340 "argument-dependent lookup at the point "
17341 "of instantiation");
17343 bool diag = true;
17344 if (in_lambda)
17345 error_at (EXPR_LOC_OR_LOC (t, input_location),
17346 msg, function);
17347 else
17348 diag = permerror (EXPR_LOC_OR_LOC (t, input_location),
17349 msg, function);
17350 if (diag)
17352 tree fn = unq;
17354 if (INDIRECT_REF_P (fn))
17355 fn = TREE_OPERAND (fn, 0);
17356 if (is_overloaded_fn (fn))
17357 fn = get_first_fn (fn);
17359 if (!DECL_P (fn))
17360 /* Can't say anything more. */;
17361 else if (DECL_CLASS_SCOPE_P (fn))
17363 location_t loc = EXPR_LOC_OR_LOC (t,
17364 input_location);
17365 inform (loc,
17366 "declarations in dependent base %qT are "
17367 "not found by unqualified lookup",
17368 DECL_CLASS_CONTEXT (fn));
17369 if (current_class_ptr)
17370 inform (loc,
17371 "use %<this->%D%> instead", function);
17372 else
17373 inform (loc,
17374 "use %<%T::%D%> instead",
17375 current_class_name, function);
17377 else
17378 inform (DECL_SOURCE_LOCATION (fn),
17379 "%qD declared here, later in the "
17380 "translation unit", fn);
17381 if (in_lambda)
17383 release_tree_vector (call_args);
17384 RETURN (error_mark_node);
17388 function = unq;
17391 if (identifier_p (function))
17393 if (complain & tf_error)
17394 unqualified_name_lookup_error (function);
17395 release_tree_vector (call_args);
17396 RETURN (error_mark_node);
17400 /* Remember that there was a reference to this entity. */
17401 if (function != NULL_TREE
17402 && DECL_P (function)
17403 && !mark_used (function, complain) && !(complain & tf_error))
17405 release_tree_vector (call_args);
17406 RETURN (error_mark_node);
17409 /* Put back tf_decltype for the actual call. */
17410 complain |= decltype_flag;
17412 if (function == NULL_TREE)
17413 switch (CALL_EXPR_IFN (t))
17415 case IFN_LAUNDER:
17416 gcc_assert (nargs == 1);
17417 if (vec_safe_length (call_args) != 1)
17419 error_at (EXPR_LOC_OR_LOC (t, input_location),
17420 "wrong number of arguments to "
17421 "%<__builtin_launder%>");
17422 ret = error_mark_node;
17424 else
17425 ret = finish_builtin_launder (EXPR_LOC_OR_LOC (t,
17426 input_location),
17427 (*call_args)[0], complain);
17428 break;
17430 default:
17431 /* Unsupported internal function with arguments. */
17432 gcc_unreachable ();
17434 else if (TREE_CODE (function) == OFFSET_REF)
17435 ret = build_offset_ref_call_from_tree (function, &call_args,
17436 complain);
17437 else if (TREE_CODE (function) == COMPONENT_REF)
17439 tree instance = TREE_OPERAND (function, 0);
17440 tree fn = TREE_OPERAND (function, 1);
17442 if (processing_template_decl
17443 && (type_dependent_expression_p (instance)
17444 || (!BASELINK_P (fn)
17445 && TREE_CODE (fn) != FIELD_DECL)
17446 || type_dependent_expression_p (fn)
17447 || any_type_dependent_arguments_p (call_args)))
17448 ret = build_min_nt_call_vec (function, call_args);
17449 else if (!BASELINK_P (fn))
17450 ret = finish_call_expr (function, &call_args,
17451 /*disallow_virtual=*/false,
17452 /*koenig_p=*/false,
17453 complain);
17454 else
17455 ret = (build_new_method_call
17456 (instance, fn,
17457 &call_args, NULL_TREE,
17458 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
17459 /*fn_p=*/NULL,
17460 complain));
17462 else
17463 ret = finish_call_expr (function, &call_args,
17464 /*disallow_virtual=*/qualified_p,
17465 koenig_p,
17466 complain);
17468 release_tree_vector (call_args);
17470 if (ret != error_mark_node)
17472 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
17473 bool ord = CALL_EXPR_ORDERED_ARGS (t);
17474 bool rev = CALL_EXPR_REVERSE_ARGS (t);
17475 bool thk = CALL_FROM_THUNK_P (t);
17476 if (op || ord || rev || thk)
17478 function = extract_call_expr (ret);
17479 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
17480 CALL_EXPR_ORDERED_ARGS (function) = ord;
17481 CALL_EXPR_REVERSE_ARGS (function) = rev;
17482 if (thk)
17484 CALL_FROM_THUNK_P (function) = true;
17485 /* The thunk location is not interesting. */
17486 SET_EXPR_LOCATION (function, UNKNOWN_LOCATION);
17491 RETURN (ret);
17494 case COND_EXPR:
17496 tree cond = RECUR (TREE_OPERAND (t, 0));
17497 tree folded_cond = fold_non_dependent_expr (cond);
17498 tree exp1, exp2;
17500 if (TREE_CODE (folded_cond) == INTEGER_CST)
17502 if (integer_zerop (folded_cond))
17504 ++c_inhibit_evaluation_warnings;
17505 exp1 = RECUR (TREE_OPERAND (t, 1));
17506 --c_inhibit_evaluation_warnings;
17507 exp2 = RECUR (TREE_OPERAND (t, 2));
17509 else
17511 exp1 = RECUR (TREE_OPERAND (t, 1));
17512 ++c_inhibit_evaluation_warnings;
17513 exp2 = RECUR (TREE_OPERAND (t, 2));
17514 --c_inhibit_evaluation_warnings;
17516 cond = folded_cond;
17518 else
17520 exp1 = RECUR (TREE_OPERAND (t, 1));
17521 exp2 = RECUR (TREE_OPERAND (t, 2));
17524 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
17525 cond, exp1, exp2, complain));
17528 case PSEUDO_DTOR_EXPR:
17530 tree op0 = RECUR (TREE_OPERAND (t, 0));
17531 tree op1 = RECUR (TREE_OPERAND (t, 1));
17532 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
17533 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
17534 input_location));
17537 case TREE_LIST:
17539 tree purpose, value, chain;
17541 if (t == void_list_node)
17542 RETURN (t);
17544 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
17545 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
17547 /* We have pack expansions, so expand those and
17548 create a new list out of it. */
17549 tree purposevec = NULL_TREE;
17550 tree valuevec = NULL_TREE;
17551 tree chain;
17552 int i, len = -1;
17554 /* Expand the argument expressions. */
17555 if (TREE_PURPOSE (t))
17556 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
17557 complain, in_decl);
17558 if (TREE_VALUE (t))
17559 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
17560 complain, in_decl);
17562 /* Build the rest of the list. */
17563 chain = TREE_CHAIN (t);
17564 if (chain && chain != void_type_node)
17565 chain = RECUR (chain);
17567 /* Determine the number of arguments. */
17568 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
17570 len = TREE_VEC_LENGTH (purposevec);
17571 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
17573 else if (TREE_CODE (valuevec) == TREE_VEC)
17574 len = TREE_VEC_LENGTH (valuevec);
17575 else
17577 /* Since we only performed a partial substitution into
17578 the argument pack, we only RETURN (a single list
17579 node. */
17580 if (purposevec == TREE_PURPOSE (t)
17581 && valuevec == TREE_VALUE (t)
17582 && chain == TREE_CHAIN (t))
17583 RETURN (t);
17585 RETURN (tree_cons (purposevec, valuevec, chain));
17588 /* Convert the argument vectors into a TREE_LIST */
17589 i = len;
17590 while (i > 0)
17592 /* Grab the Ith values. */
17593 i--;
17594 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
17595 : NULL_TREE;
17596 value
17597 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
17598 : NULL_TREE;
17600 /* Build the list (backwards). */
17601 chain = tree_cons (purpose, value, chain);
17604 RETURN (chain);
17607 purpose = TREE_PURPOSE (t);
17608 if (purpose)
17609 purpose = RECUR (purpose);
17610 value = TREE_VALUE (t);
17611 if (value)
17612 value = RECUR (value);
17613 chain = TREE_CHAIN (t);
17614 if (chain && chain != void_type_node)
17615 chain = RECUR (chain);
17616 if (purpose == TREE_PURPOSE (t)
17617 && value == TREE_VALUE (t)
17618 && chain == TREE_CHAIN (t))
17619 RETURN (t);
17620 RETURN (tree_cons (purpose, value, chain));
17623 case COMPONENT_REF:
17625 tree object;
17626 tree object_type;
17627 tree member;
17628 tree r;
17630 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17631 args, complain, in_decl);
17632 /* Remember that there was a reference to this entity. */
17633 if (DECL_P (object)
17634 && !mark_used (object, complain) && !(complain & tf_error))
17635 RETURN (error_mark_node);
17636 object_type = TREE_TYPE (object);
17638 member = TREE_OPERAND (t, 1);
17639 if (BASELINK_P (member))
17640 member = tsubst_baselink (member,
17641 non_reference (TREE_TYPE (object)),
17642 args, complain, in_decl);
17643 else
17644 member = tsubst_copy (member, args, complain, in_decl);
17645 if (member == error_mark_node)
17646 RETURN (error_mark_node);
17648 if (TREE_CODE (member) == FIELD_DECL)
17650 r = finish_non_static_data_member (member, object, NULL_TREE);
17651 if (TREE_CODE (r) == COMPONENT_REF)
17652 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17653 RETURN (r);
17655 else if (type_dependent_expression_p (object))
17656 /* We can't do much here. */;
17657 else if (!CLASS_TYPE_P (object_type))
17659 if (scalarish_type_p (object_type))
17661 tree s = NULL_TREE;
17662 tree dtor = member;
17664 if (TREE_CODE (dtor) == SCOPE_REF)
17666 s = TREE_OPERAND (dtor, 0);
17667 dtor = TREE_OPERAND (dtor, 1);
17669 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
17671 dtor = TREE_OPERAND (dtor, 0);
17672 if (TYPE_P (dtor))
17673 RETURN (finish_pseudo_destructor_expr
17674 (object, s, dtor, input_location));
17678 else if (TREE_CODE (member) == SCOPE_REF
17679 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
17681 /* Lookup the template functions now that we know what the
17682 scope is. */
17683 tree scope = TREE_OPERAND (member, 0);
17684 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
17685 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
17686 member = lookup_qualified_name (scope, tmpl,
17687 /*is_type_p=*/false,
17688 /*complain=*/false);
17689 if (BASELINK_P (member))
17691 BASELINK_FUNCTIONS (member)
17692 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
17693 args);
17694 member = (adjust_result_of_qualified_name_lookup
17695 (member, BINFO_TYPE (BASELINK_BINFO (member)),
17696 object_type));
17698 else
17700 qualified_name_lookup_error (scope, tmpl, member,
17701 input_location);
17702 RETURN (error_mark_node);
17705 else if (TREE_CODE (member) == SCOPE_REF
17706 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
17707 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
17709 if (complain & tf_error)
17711 if (TYPE_P (TREE_OPERAND (member, 0)))
17712 error ("%qT is not a class or namespace",
17713 TREE_OPERAND (member, 0));
17714 else
17715 error ("%qD is not a class or namespace",
17716 TREE_OPERAND (member, 0));
17718 RETURN (error_mark_node);
17721 r = finish_class_member_access_expr (object, member,
17722 /*template_p=*/false,
17723 complain);
17724 if (TREE_CODE (r) == COMPONENT_REF)
17725 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17726 RETURN (r);
17729 case THROW_EXPR:
17730 RETURN (build_throw
17731 (RECUR (TREE_OPERAND (t, 0))));
17733 case CONSTRUCTOR:
17735 vec<constructor_elt, va_gc> *n;
17736 constructor_elt *ce;
17737 unsigned HOST_WIDE_INT idx;
17738 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17739 bool process_index_p;
17740 int newlen;
17741 bool need_copy_p = false;
17742 tree r;
17744 if (type == error_mark_node)
17745 RETURN (error_mark_node);
17747 /* digest_init will do the wrong thing if we let it. */
17748 if (type && TYPE_PTRMEMFUNC_P (type))
17749 RETURN (t);
17751 /* We do not want to process the index of aggregate
17752 initializers as they are identifier nodes which will be
17753 looked up by digest_init. */
17754 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
17756 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
17757 newlen = vec_safe_length (n);
17758 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
17760 if (ce->index && process_index_p
17761 /* An identifier index is looked up in the type
17762 being initialized, not the current scope. */
17763 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
17764 ce->index = RECUR (ce->index);
17766 if (PACK_EXPANSION_P (ce->value))
17768 /* Substitute into the pack expansion. */
17769 ce->value = tsubst_pack_expansion (ce->value, args, complain,
17770 in_decl);
17772 if (ce->value == error_mark_node
17773 || PACK_EXPANSION_P (ce->value))
17775 else if (TREE_VEC_LENGTH (ce->value) == 1)
17776 /* Just move the argument into place. */
17777 ce->value = TREE_VEC_ELT (ce->value, 0);
17778 else
17780 /* Update the length of the final CONSTRUCTOR
17781 arguments vector, and note that we will need to
17782 copy.*/
17783 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
17784 need_copy_p = true;
17787 else
17788 ce->value = RECUR (ce->value);
17791 if (need_copy_p)
17793 vec<constructor_elt, va_gc> *old_n = n;
17795 vec_alloc (n, newlen);
17796 FOR_EACH_VEC_ELT (*old_n, idx, ce)
17798 if (TREE_CODE (ce->value) == TREE_VEC)
17800 int i, len = TREE_VEC_LENGTH (ce->value);
17801 for (i = 0; i < len; ++i)
17802 CONSTRUCTOR_APPEND_ELT (n, 0,
17803 TREE_VEC_ELT (ce->value, i));
17805 else
17806 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
17810 r = build_constructor (init_list_type_node, n);
17811 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
17813 if (TREE_HAS_CONSTRUCTOR (t))
17815 fcl_t cl = fcl_functional;
17816 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
17817 cl = fcl_c99;
17818 RETURN (finish_compound_literal (type, r, complain, cl));
17821 TREE_TYPE (r) = type;
17822 RETURN (r);
17825 case TYPEID_EXPR:
17827 tree operand_0 = TREE_OPERAND (t, 0);
17828 if (TYPE_P (operand_0))
17830 operand_0 = tsubst (operand_0, args, complain, in_decl);
17831 RETURN (get_typeid (operand_0, complain));
17833 else
17835 operand_0 = RECUR (operand_0);
17836 RETURN (build_typeid (operand_0, complain));
17840 case VAR_DECL:
17841 if (!args)
17842 RETURN (t);
17843 else if (DECL_PACK_P (t))
17845 /* We don't build decls for an instantiation of a
17846 variadic capture proxy, we instantiate the elements
17847 when needed. */
17848 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
17849 return RECUR (DECL_VALUE_EXPR (t));
17851 /* Fall through */
17853 case PARM_DECL:
17855 tree r = tsubst_copy (t, args, complain, in_decl);
17856 /* ??? We're doing a subset of finish_id_expression here. */
17857 if (VAR_P (r)
17858 && !processing_template_decl
17859 && !cp_unevaluated_operand
17860 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
17861 && CP_DECL_THREAD_LOCAL_P (r))
17863 if (tree wrap = get_tls_wrapper_fn (r))
17864 /* Replace an evaluated use of the thread_local variable with
17865 a call to its wrapper. */
17866 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
17868 else if (outer_automatic_var_p (r))
17870 r = process_outer_var_ref (r, complain);
17871 if (is_capture_proxy (r))
17872 register_local_specialization (r, t);
17875 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
17876 /* If the original type was a reference, we'll be wrapped in
17877 the appropriate INDIRECT_REF. */
17878 r = convert_from_reference (r);
17879 RETURN (r);
17882 case VA_ARG_EXPR:
17884 tree op0 = RECUR (TREE_OPERAND (t, 0));
17885 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17886 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
17889 case OFFSETOF_EXPR:
17891 tree object_ptr
17892 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
17893 in_decl, /*function_p=*/false,
17894 /*integral_constant_expression_p=*/false);
17895 RETURN (finish_offsetof (object_ptr,
17896 RECUR (TREE_OPERAND (t, 0)),
17897 EXPR_LOCATION (t)));
17900 case ADDRESSOF_EXPR:
17901 RETURN (cp_build_addressof (EXPR_LOCATION (t),
17902 RECUR (TREE_OPERAND (t, 0)), complain));
17904 case TRAIT_EXPR:
17906 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
17907 complain, in_decl);
17909 tree type2 = TRAIT_EXPR_TYPE2 (t);
17910 if (type2 && TREE_CODE (type2) == TREE_LIST)
17911 type2 = RECUR (type2);
17912 else if (type2)
17913 type2 = tsubst (type2, args, complain, in_decl);
17915 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
17918 case STMT_EXPR:
17920 tree old_stmt_expr = cur_stmt_expr;
17921 tree stmt_expr = begin_stmt_expr ();
17923 cur_stmt_expr = stmt_expr;
17924 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
17925 integral_constant_expression_p);
17926 stmt_expr = finish_stmt_expr (stmt_expr, false);
17927 cur_stmt_expr = old_stmt_expr;
17929 /* If the resulting list of expression statement is empty,
17930 fold it further into void_node. */
17931 if (empty_expr_stmt_p (stmt_expr))
17932 stmt_expr = void_node;
17934 RETURN (stmt_expr);
17937 case LAMBDA_EXPR:
17939 tree r = build_lambda_expr ();
17941 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
17942 LAMBDA_EXPR_CLOSURE (r) = type;
17943 CLASSTYPE_LAMBDA_EXPR (type) = r;
17945 LAMBDA_EXPR_LOCATION (r)
17946 = LAMBDA_EXPR_LOCATION (t);
17947 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
17948 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
17949 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
17950 LAMBDA_EXPR_DISCRIMINATOR (r)
17951 = (LAMBDA_EXPR_DISCRIMINATOR (t));
17952 tree scope = LAMBDA_EXPR_EXTRA_SCOPE (t);
17953 if (!scope)
17954 /* No substitution needed. */;
17955 else if (VAR_OR_FUNCTION_DECL_P (scope))
17956 /* For a function or variable scope, we want to use tsubst so that we
17957 don't complain about referring to an auto before deduction. */
17958 scope = tsubst (scope, args, complain, in_decl);
17959 else if (TREE_CODE (scope) == PARM_DECL)
17961 /* Look up the parameter we want directly, as tsubst_copy
17962 doesn't do what we need. */
17963 tree fn = tsubst (DECL_CONTEXT (scope), args, complain, in_decl);
17964 tree parm = FUNCTION_FIRST_USER_PARM (fn);
17965 while (DECL_PARM_INDEX (parm) != DECL_PARM_INDEX (scope))
17966 parm = DECL_CHAIN (parm);
17967 scope = parm;
17968 /* FIXME Work around the parm not having DECL_CONTEXT set. */
17969 if (DECL_CONTEXT (scope) == NULL_TREE)
17970 DECL_CONTEXT (scope) = fn;
17972 else if (TREE_CODE (scope) == FIELD_DECL)
17973 /* For a field, use tsubst_copy so that we look up the existing field
17974 rather than build a new one. */
17975 scope = RECUR (scope);
17976 else
17977 gcc_unreachable ();
17978 LAMBDA_EXPR_EXTRA_SCOPE (r) = scope;
17980 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
17981 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
17983 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
17984 determine_visibility (TYPE_NAME (type));
17985 /* Now that we know visibility, instantiate the type so we have a
17986 declaration of the op() for later calls to lambda_function. */
17987 complete_type (type);
17989 if (tree fn = lambda_function (type))
17990 LAMBDA_EXPR_RETURN_TYPE (r) = TREE_TYPE (TREE_TYPE (fn));
17992 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
17994 insert_pending_capture_proxies ();
17996 RETURN (build_lambda_object (r));
17999 case TARGET_EXPR:
18000 /* We can get here for a constant initializer of non-dependent type.
18001 FIXME stop folding in cp_parser_initializer_clause. */
18003 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
18004 complain);
18005 RETURN (r);
18008 case TRANSACTION_EXPR:
18009 RETURN (tsubst_expr(t, args, complain, in_decl,
18010 integral_constant_expression_p));
18012 case PAREN_EXPR:
18013 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
18015 case VEC_PERM_EXPR:
18017 tree op0 = RECUR (TREE_OPERAND (t, 0));
18018 tree op1 = RECUR (TREE_OPERAND (t, 1));
18019 tree op2 = RECUR (TREE_OPERAND (t, 2));
18020 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
18021 complain));
18024 case REQUIRES_EXPR:
18025 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
18027 default:
18028 /* Handle Objective-C++ constructs, if appropriate. */
18030 tree subst
18031 = objcp_tsubst_copy_and_build (t, args, complain,
18032 in_decl, /*function_p=*/false);
18033 if (subst)
18034 RETURN (subst);
18036 RETURN (tsubst_copy (t, args, complain, in_decl));
18039 #undef RECUR
18040 #undef RETURN
18041 out:
18042 input_location = loc;
18043 return retval;
18046 /* Verify that the instantiated ARGS are valid. For type arguments,
18047 make sure that the type's linkage is ok. For non-type arguments,
18048 make sure they are constants if they are integral or enumerations.
18049 Emit an error under control of COMPLAIN, and return TRUE on error. */
18051 static bool
18052 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
18054 if (dependent_template_arg_p (t))
18055 return false;
18056 if (ARGUMENT_PACK_P (t))
18058 tree vec = ARGUMENT_PACK_ARGS (t);
18059 int len = TREE_VEC_LENGTH (vec);
18060 bool result = false;
18061 int i;
18063 for (i = 0; i < len; ++i)
18064 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
18065 result = true;
18066 return result;
18068 else if (TYPE_P (t))
18070 /* [basic.link]: A name with no linkage (notably, the name
18071 of a class or enumeration declared in a local scope)
18072 shall not be used to declare an entity with linkage.
18073 This implies that names with no linkage cannot be used as
18074 template arguments
18076 DR 757 relaxes this restriction for C++0x. */
18077 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
18078 : no_linkage_check (t, /*relaxed_p=*/false));
18080 if (nt)
18082 /* DR 488 makes use of a type with no linkage cause
18083 type deduction to fail. */
18084 if (complain & tf_error)
18086 if (TYPE_UNNAMED_P (nt))
18087 error ("%qT is/uses unnamed type", t);
18088 else
18089 error ("template argument for %qD uses local type %qT",
18090 tmpl, t);
18092 return true;
18094 /* In order to avoid all sorts of complications, we do not
18095 allow variably-modified types as template arguments. */
18096 else if (variably_modified_type_p (t, NULL_TREE))
18098 if (complain & tf_error)
18099 error ("%qT is a variably modified type", t);
18100 return true;
18103 /* Class template and alias template arguments should be OK. */
18104 else if (DECL_TYPE_TEMPLATE_P (t))
18106 /* A non-type argument of integral or enumerated type must be a
18107 constant. */
18108 else if (TREE_TYPE (t)
18109 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
18110 && !REFERENCE_REF_P (t)
18111 && !TREE_CONSTANT (t))
18113 if (complain & tf_error)
18114 error ("integral expression %qE is not constant", t);
18115 return true;
18117 return false;
18120 static bool
18121 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
18123 int ix, len = DECL_NTPARMS (tmpl);
18124 bool result = false;
18126 for (ix = 0; ix != len; ix++)
18128 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
18129 result = true;
18131 if (result && (complain & tf_error))
18132 error (" trying to instantiate %qD", tmpl);
18133 return result;
18136 /* We're out of SFINAE context now, so generate diagnostics for the access
18137 errors we saw earlier when instantiating D from TMPL and ARGS. */
18139 static void
18140 recheck_decl_substitution (tree d, tree tmpl, tree args)
18142 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
18143 tree type = TREE_TYPE (pattern);
18144 location_t loc = input_location;
18146 push_access_scope (d);
18147 push_deferring_access_checks (dk_no_deferred);
18148 input_location = DECL_SOURCE_LOCATION (pattern);
18149 tsubst (type, args, tf_warning_or_error, d);
18150 input_location = loc;
18151 pop_deferring_access_checks ();
18152 pop_access_scope (d);
18155 /* Instantiate the indicated variable, function, or alias template TMPL with
18156 the template arguments in TARG_PTR. */
18158 static tree
18159 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
18161 tree targ_ptr = orig_args;
18162 tree fndecl;
18163 tree gen_tmpl;
18164 tree spec;
18165 bool access_ok = true;
18167 if (tmpl == error_mark_node)
18168 return error_mark_node;
18170 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
18172 /* If this function is a clone, handle it specially. */
18173 if (DECL_CLONED_FUNCTION_P (tmpl))
18175 tree spec;
18176 tree clone;
18178 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
18179 DECL_CLONED_FUNCTION. */
18180 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
18181 targ_ptr, complain);
18182 if (spec == error_mark_node)
18183 return error_mark_node;
18185 /* Look for the clone. */
18186 FOR_EACH_CLONE (clone, spec)
18187 if (DECL_NAME (clone) == DECL_NAME (tmpl))
18188 return clone;
18189 /* We should always have found the clone by now. */
18190 gcc_unreachable ();
18191 return NULL_TREE;
18194 if (targ_ptr == error_mark_node)
18195 return error_mark_node;
18197 /* Check to see if we already have this specialization. */
18198 gen_tmpl = most_general_template (tmpl);
18199 if (TMPL_ARGS_DEPTH (targ_ptr)
18200 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
18201 /* targ_ptr only has the innermost template args, so add the outer ones
18202 from tmpl, which could be either a partial instantiation or gen_tmpl (in
18203 the case of a non-dependent call within a template definition). */
18204 targ_ptr = (add_outermost_template_args
18205 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
18206 targ_ptr));
18208 /* It would be nice to avoid hashing here and then again in tsubst_decl,
18209 but it doesn't seem to be on the hot path. */
18210 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
18212 gcc_assert (tmpl == gen_tmpl
18213 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
18214 == spec)
18215 || fndecl == NULL_TREE);
18217 if (spec != NULL_TREE)
18219 if (FNDECL_HAS_ACCESS_ERRORS (spec))
18221 if (complain & tf_error)
18222 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
18223 return error_mark_node;
18225 return spec;
18228 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
18229 complain))
18230 return error_mark_node;
18232 /* We are building a FUNCTION_DECL, during which the access of its
18233 parameters and return types have to be checked. However this
18234 FUNCTION_DECL which is the desired context for access checking
18235 is not built yet. We solve this chicken-and-egg problem by
18236 deferring all checks until we have the FUNCTION_DECL. */
18237 push_deferring_access_checks (dk_deferred);
18239 /* Instantiation of the function happens in the context of the function
18240 template, not the context of the overload resolution we're doing. */
18241 push_to_top_level ();
18242 /* If there are dependent arguments, e.g. because we're doing partial
18243 ordering, make sure processing_template_decl stays set. */
18244 if (uses_template_parms (targ_ptr))
18245 ++processing_template_decl;
18246 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18248 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
18249 complain, gen_tmpl, true);
18250 push_nested_class (ctx);
18253 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
18255 fndecl = NULL_TREE;
18256 if (VAR_P (pattern))
18258 /* We need to determine if we're using a partial or explicit
18259 specialization now, because the type of the variable could be
18260 different. */
18261 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
18262 tree elt = most_specialized_partial_spec (tid, complain);
18263 if (elt == error_mark_node)
18264 pattern = error_mark_node;
18265 else if (elt)
18267 tree partial_tmpl = TREE_VALUE (elt);
18268 tree partial_args = TREE_PURPOSE (elt);
18269 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
18270 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
18274 /* Substitute template parameters to obtain the specialization. */
18275 if (fndecl == NULL_TREE)
18276 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
18277 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18278 pop_nested_class ();
18279 pop_from_top_level ();
18281 if (fndecl == error_mark_node)
18283 pop_deferring_access_checks ();
18284 return error_mark_node;
18287 /* The DECL_TI_TEMPLATE should always be the immediate parent
18288 template, not the most general template. */
18289 DECL_TI_TEMPLATE (fndecl) = tmpl;
18290 DECL_TI_ARGS (fndecl) = targ_ptr;
18292 /* Now we know the specialization, compute access previously
18293 deferred. Do no access control for inheriting constructors,
18294 as we already checked access for the inherited constructor. */
18295 if (!(flag_new_inheriting_ctors
18296 && DECL_INHERITED_CTOR (fndecl)))
18298 push_access_scope (fndecl);
18299 if (!perform_deferred_access_checks (complain))
18300 access_ok = false;
18301 pop_access_scope (fndecl);
18303 pop_deferring_access_checks ();
18305 /* If we've just instantiated the main entry point for a function,
18306 instantiate all the alternate entry points as well. We do this
18307 by cloning the instantiation of the main entry point, not by
18308 instantiating the template clones. */
18309 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
18310 clone_function_decl (fndecl, /*update_methods=*/false);
18312 if (!access_ok)
18314 if (!(complain & tf_error))
18316 /* Remember to reinstantiate when we're out of SFINAE so the user
18317 can see the errors. */
18318 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
18320 return error_mark_node;
18322 return fndecl;
18325 /* Wrapper for instantiate_template_1. */
18327 tree
18328 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
18330 tree ret;
18331 timevar_push (TV_TEMPLATE_INST);
18332 ret = instantiate_template_1 (tmpl, orig_args, complain);
18333 timevar_pop (TV_TEMPLATE_INST);
18334 return ret;
18337 /* Instantiate the alias template TMPL with ARGS. Also push a template
18338 instantiation level, which instantiate_template doesn't do because
18339 functions and variables have sufficient context established by the
18340 callers. */
18342 static tree
18343 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
18345 struct pending_template *old_last_pend = last_pending_template;
18346 struct tinst_level *old_error_tinst = last_error_tinst_level;
18347 if (tmpl == error_mark_node || args == error_mark_node)
18348 return error_mark_node;
18349 tree tinst = build_tree_list (tmpl, args);
18350 if (!push_tinst_level (tinst))
18352 ggc_free (tinst);
18353 return error_mark_node;
18356 args =
18357 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
18358 args, tmpl, complain,
18359 /*require_all_args=*/true,
18360 /*use_default_args=*/true);
18362 tree r = instantiate_template (tmpl, args, complain);
18363 pop_tinst_level ();
18364 /* We can't free this if a pending_template entry or last_error_tinst_level
18365 is pointing at it. */
18366 if (last_pending_template == old_last_pend
18367 && last_error_tinst_level == old_error_tinst)
18368 ggc_free (tinst);
18370 return r;
18373 /* PARM is a template parameter pack for FN. Returns true iff
18374 PARM is used in a deducible way in the argument list of FN. */
18376 static bool
18377 pack_deducible_p (tree parm, tree fn)
18379 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
18380 for (; t; t = TREE_CHAIN (t))
18382 tree type = TREE_VALUE (t);
18383 tree packs;
18384 if (!PACK_EXPANSION_P (type))
18385 continue;
18386 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
18387 packs; packs = TREE_CHAIN (packs))
18388 if (template_args_equal (TREE_VALUE (packs), parm))
18390 /* The template parameter pack is used in a function parameter
18391 pack. If this is the end of the parameter list, the
18392 template parameter pack is deducible. */
18393 if (TREE_CHAIN (t) == void_list_node)
18394 return true;
18395 else
18396 /* Otherwise, not. Well, it could be deduced from
18397 a non-pack parameter, but doing so would end up with
18398 a deduction mismatch, so don't bother. */
18399 return false;
18402 /* The template parameter pack isn't used in any function parameter
18403 packs, but it might be used deeper, e.g. tuple<Args...>. */
18404 return true;
18407 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
18408 NARGS elements of the arguments that are being used when calling
18409 it. TARGS is a vector into which the deduced template arguments
18410 are placed.
18412 Returns either a FUNCTION_DECL for the matching specialization of FN or
18413 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
18414 true, diagnostics will be printed to explain why it failed.
18416 If FN is a conversion operator, or we are trying to produce a specific
18417 specialization, RETURN_TYPE is the return type desired.
18419 The EXPLICIT_TARGS are explicit template arguments provided via a
18420 template-id.
18422 The parameter STRICT is one of:
18424 DEDUCE_CALL:
18425 We are deducing arguments for a function call, as in
18426 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
18427 deducing arguments for a call to the result of a conversion
18428 function template, as in [over.call.object].
18430 DEDUCE_CONV:
18431 We are deducing arguments for a conversion function, as in
18432 [temp.deduct.conv].
18434 DEDUCE_EXACT:
18435 We are deducing arguments when doing an explicit instantiation
18436 as in [temp.explicit], when determining an explicit specialization
18437 as in [temp.expl.spec], or when taking the address of a function
18438 template, as in [temp.deduct.funcaddr]. */
18440 tree
18441 fn_type_unification (tree fn,
18442 tree explicit_targs,
18443 tree targs,
18444 const tree *args,
18445 unsigned int nargs,
18446 tree return_type,
18447 unification_kind_t strict,
18448 int flags,
18449 bool explain_p,
18450 bool decltype_p)
18452 tree parms;
18453 tree fntype;
18454 tree decl = NULL_TREE;
18455 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
18456 bool ok;
18457 static int deduction_depth;
18458 struct pending_template *old_last_pend = last_pending_template;
18459 struct tinst_level *old_error_tinst = last_error_tinst_level;
18461 tree orig_fn = fn;
18462 if (flag_new_inheriting_ctors)
18463 fn = strip_inheriting_ctors (fn);
18465 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
18466 tree tinst;
18467 tree r = error_mark_node;
18469 tree full_targs = targs;
18470 if (TMPL_ARGS_DEPTH (targs)
18471 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
18472 full_targs = (add_outermost_template_args
18473 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
18474 targs));
18476 if (decltype_p)
18477 complain |= tf_decltype;
18479 /* In C++0x, it's possible to have a function template whose type depends
18480 on itself recursively. This is most obvious with decltype, but can also
18481 occur with enumeration scope (c++/48969). So we need to catch infinite
18482 recursion and reject the substitution at deduction time; this function
18483 will return error_mark_node for any repeated substitution.
18485 This also catches excessive recursion such as when f<N> depends on
18486 f<N-1> across all integers, and returns error_mark_node for all the
18487 substitutions back up to the initial one.
18489 This is, of course, not reentrant. */
18490 if (excessive_deduction_depth)
18491 return error_mark_node;
18492 tinst = build_tree_list (fn, NULL_TREE);
18493 ++deduction_depth;
18495 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
18497 fntype = TREE_TYPE (fn);
18498 if (explicit_targs)
18500 /* [temp.deduct]
18502 The specified template arguments must match the template
18503 parameters in kind (i.e., type, nontype, template), and there
18504 must not be more arguments than there are parameters;
18505 otherwise type deduction fails.
18507 Nontype arguments must match the types of the corresponding
18508 nontype template parameters, or must be convertible to the
18509 types of the corresponding nontype parameters as specified in
18510 _temp.arg.nontype_, otherwise type deduction fails.
18512 All references in the function type of the function template
18513 to the corresponding template parameters are replaced by the
18514 specified template argument values. If a substitution in a
18515 template parameter or in the function type of the function
18516 template results in an invalid type, type deduction fails. */
18517 int i, len = TREE_VEC_LENGTH (tparms);
18518 location_t loc = input_location;
18519 bool incomplete = false;
18521 if (explicit_targs == error_mark_node)
18522 goto fail;
18524 if (TMPL_ARGS_DEPTH (explicit_targs)
18525 < TMPL_ARGS_DEPTH (full_targs))
18526 explicit_targs = add_outermost_template_args (full_targs,
18527 explicit_targs);
18529 /* Adjust any explicit template arguments before entering the
18530 substitution context. */
18531 explicit_targs
18532 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
18533 complain,
18534 /*require_all_args=*/false,
18535 /*use_default_args=*/false));
18536 if (explicit_targs == error_mark_node)
18537 goto fail;
18539 /* Substitute the explicit args into the function type. This is
18540 necessary so that, for instance, explicitly declared function
18541 arguments can match null pointed constants. If we were given
18542 an incomplete set of explicit args, we must not do semantic
18543 processing during substitution as we could create partial
18544 instantiations. */
18545 for (i = 0; i < len; i++)
18547 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
18548 bool parameter_pack = false;
18549 tree targ = TREE_VEC_ELT (explicit_targs, i);
18551 /* Dig out the actual parm. */
18552 if (TREE_CODE (parm) == TYPE_DECL
18553 || TREE_CODE (parm) == TEMPLATE_DECL)
18555 parm = TREE_TYPE (parm);
18556 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
18558 else if (TREE_CODE (parm) == PARM_DECL)
18560 parm = DECL_INITIAL (parm);
18561 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
18564 if (!parameter_pack && targ == NULL_TREE)
18565 /* No explicit argument for this template parameter. */
18566 incomplete = true;
18568 if (parameter_pack && pack_deducible_p (parm, fn))
18570 /* Mark the argument pack as "incomplete". We could
18571 still deduce more arguments during unification.
18572 We remove this mark in type_unification_real. */
18573 if (targ)
18575 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
18576 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
18577 = ARGUMENT_PACK_ARGS (targ);
18580 /* We have some incomplete argument packs. */
18581 incomplete = true;
18585 TREE_VALUE (tinst) = explicit_targs;
18586 if (!push_tinst_level (tinst))
18588 excessive_deduction_depth = true;
18589 goto fail;
18591 processing_template_decl += incomplete;
18592 input_location = DECL_SOURCE_LOCATION (fn);
18593 /* Ignore any access checks; we'll see them again in
18594 instantiate_template and they might have the wrong
18595 access path at this point. */
18596 push_deferring_access_checks (dk_deferred);
18597 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
18598 complain | tf_partial | tf_fndecl_type, NULL_TREE);
18599 pop_deferring_access_checks ();
18600 input_location = loc;
18601 processing_template_decl -= incomplete;
18602 pop_tinst_level ();
18604 if (fntype == error_mark_node)
18605 goto fail;
18607 /* Place the explicitly specified arguments in TARGS. */
18608 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
18609 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
18610 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
18613 /* Never do unification on the 'this' parameter. */
18614 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
18616 if (return_type && strict == DEDUCE_CALL)
18618 /* We're deducing for a call to the result of a template conversion
18619 function. The parms we really want are in return_type. */
18620 if (POINTER_TYPE_P (return_type))
18621 return_type = TREE_TYPE (return_type);
18622 parms = TYPE_ARG_TYPES (return_type);
18624 else if (return_type)
18626 tree *new_args;
18628 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
18629 new_args = XALLOCAVEC (tree, nargs + 1);
18630 new_args[0] = return_type;
18631 memcpy (new_args + 1, args, nargs * sizeof (tree));
18632 args = new_args;
18633 ++nargs;
18636 /* We allow incomplete unification without an error message here
18637 because the standard doesn't seem to explicitly prohibit it. Our
18638 callers must be ready to deal with unification failures in any
18639 event. */
18641 TREE_VALUE (tinst) = targs;
18642 /* If we aren't explaining yet, push tinst context so we can see where
18643 any errors (e.g. from class instantiations triggered by instantiation
18644 of default template arguments) come from. If we are explaining, this
18645 context is redundant. */
18646 if (!explain_p && !push_tinst_level (tinst))
18648 excessive_deduction_depth = true;
18649 goto fail;
18652 /* type_unification_real will pass back any access checks from default
18653 template argument substitution. */
18654 vec<deferred_access_check, va_gc> *checks;
18655 checks = NULL;
18657 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
18658 full_targs, parms, args, nargs, /*subr=*/0,
18659 strict, flags, &checks, explain_p);
18660 if (!explain_p)
18661 pop_tinst_level ();
18662 if (!ok)
18663 goto fail;
18665 /* Now that we have bindings for all of the template arguments,
18666 ensure that the arguments deduced for the template template
18667 parameters have compatible template parameter lists. We cannot
18668 check this property before we have deduced all template
18669 arguments, because the template parameter types of a template
18670 template parameter might depend on prior template parameters
18671 deduced after the template template parameter. The following
18672 ill-formed example illustrates this issue:
18674 template<typename T, template<T> class C> void f(C<5>, T);
18676 template<int N> struct X {};
18678 void g() {
18679 f(X<5>(), 5l); // error: template argument deduction fails
18682 The template parameter list of 'C' depends on the template type
18683 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
18684 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
18685 time that we deduce 'C'. */
18686 if (!template_template_parm_bindings_ok_p
18687 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
18689 unify_inconsistent_template_template_parameters (explain_p);
18690 goto fail;
18693 /* All is well so far. Now, check:
18695 [temp.deduct]
18697 When all template arguments have been deduced, all uses of
18698 template parameters in nondeduced contexts are replaced with
18699 the corresponding deduced argument values. If the
18700 substitution results in an invalid type, as described above,
18701 type deduction fails. */
18702 TREE_VALUE (tinst) = targs;
18703 if (!push_tinst_level (tinst))
18705 excessive_deduction_depth = true;
18706 goto fail;
18709 /* Also collect access checks from the instantiation. */
18710 reopen_deferring_access_checks (checks);
18712 decl = instantiate_template (fn, targs, complain);
18714 checks = get_deferred_access_checks ();
18715 pop_deferring_access_checks ();
18717 pop_tinst_level ();
18719 if (decl == error_mark_node)
18720 goto fail;
18722 /* Now perform any access checks encountered during substitution. */
18723 push_access_scope (decl);
18724 ok = perform_access_checks (checks, complain);
18725 pop_access_scope (decl);
18726 if (!ok)
18727 goto fail;
18729 /* If we're looking for an exact match, check that what we got
18730 is indeed an exact match. It might not be if some template
18731 parameters are used in non-deduced contexts. But don't check
18732 for an exact match if we have dependent template arguments;
18733 in that case we're doing partial ordering, and we already know
18734 that we have two candidates that will provide the actual type. */
18735 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
18737 tree substed = TREE_TYPE (decl);
18738 unsigned int i;
18740 tree sarg
18741 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
18742 if (return_type)
18743 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
18744 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
18745 if (!same_type_p (args[i], TREE_VALUE (sarg)))
18747 unify_type_mismatch (explain_p, args[i],
18748 TREE_VALUE (sarg));
18749 goto fail;
18753 /* After doing deduction with the inherited constructor, actually return an
18754 instantiation of the inheriting constructor. */
18755 if (orig_fn != fn)
18756 decl = instantiate_template (orig_fn, targs, complain);
18758 r = decl;
18760 fail:
18761 --deduction_depth;
18762 if (excessive_deduction_depth)
18764 if (deduction_depth == 0)
18765 /* Reset once we're all the way out. */
18766 excessive_deduction_depth = false;
18769 /* We can't free this if a pending_template entry or last_error_tinst_level
18770 is pointing at it. */
18771 if (last_pending_template == old_last_pend
18772 && last_error_tinst_level == old_error_tinst)
18773 ggc_free (tinst);
18775 return r;
18778 /* Adjust types before performing type deduction, as described in
18779 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
18780 sections are symmetric. PARM is the type of a function parameter
18781 or the return type of the conversion function. ARG is the type of
18782 the argument passed to the call, or the type of the value
18783 initialized with the result of the conversion function.
18784 ARG_EXPR is the original argument expression, which may be null. */
18786 static int
18787 maybe_adjust_types_for_deduction (unification_kind_t strict,
18788 tree* parm,
18789 tree* arg,
18790 tree arg_expr)
18792 int result = 0;
18794 switch (strict)
18796 case DEDUCE_CALL:
18797 break;
18799 case DEDUCE_CONV:
18800 /* Swap PARM and ARG throughout the remainder of this
18801 function; the handling is precisely symmetric since PARM
18802 will initialize ARG rather than vice versa. */
18803 std::swap (parm, arg);
18804 break;
18806 case DEDUCE_EXACT:
18807 /* Core issue #873: Do the DR606 thing (see below) for these cases,
18808 too, but here handle it by stripping the reference from PARM
18809 rather than by adding it to ARG. */
18810 if (TREE_CODE (*parm) == REFERENCE_TYPE
18811 && TYPE_REF_IS_RVALUE (*parm)
18812 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
18813 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
18814 && TREE_CODE (*arg) == REFERENCE_TYPE
18815 && !TYPE_REF_IS_RVALUE (*arg))
18816 *parm = TREE_TYPE (*parm);
18817 /* Nothing else to do in this case. */
18818 return 0;
18820 default:
18821 gcc_unreachable ();
18824 if (TREE_CODE (*parm) != REFERENCE_TYPE)
18826 /* [temp.deduct.call]
18828 If P is not a reference type:
18830 --If A is an array type, the pointer type produced by the
18831 array-to-pointer standard conversion (_conv.array_) is
18832 used in place of A for type deduction; otherwise,
18834 --If A is a function type, the pointer type produced by
18835 the function-to-pointer standard conversion
18836 (_conv.func_) is used in place of A for type deduction;
18837 otherwise,
18839 --If A is a cv-qualified type, the top level
18840 cv-qualifiers of A's type are ignored for type
18841 deduction. */
18842 if (TREE_CODE (*arg) == ARRAY_TYPE)
18843 *arg = build_pointer_type (TREE_TYPE (*arg));
18844 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
18845 *arg = build_pointer_type (*arg);
18846 else
18847 *arg = TYPE_MAIN_VARIANT (*arg);
18850 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
18851 reference to a cv-unqualified template parameter that does not represent a
18852 template parameter of a class template (during class template argument
18853 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
18854 an lvalue, the type "lvalue reference to A" is used in place of A for type
18855 deduction. */
18856 if (TREE_CODE (*parm) == REFERENCE_TYPE
18857 && TYPE_REF_IS_RVALUE (*parm)
18858 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
18859 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
18860 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
18861 && (arg_expr ? lvalue_p (arg_expr)
18862 /* try_one_overload doesn't provide an arg_expr, but
18863 functions are always lvalues. */
18864 : TREE_CODE (*arg) == FUNCTION_TYPE))
18865 *arg = build_reference_type (*arg);
18867 /* [temp.deduct.call]
18869 If P is a cv-qualified type, the top level cv-qualifiers
18870 of P's type are ignored for type deduction. If P is a
18871 reference type, the type referred to by P is used for
18872 type deduction. */
18873 *parm = TYPE_MAIN_VARIANT (*parm);
18874 if (TREE_CODE (*parm) == REFERENCE_TYPE)
18876 *parm = TREE_TYPE (*parm);
18877 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
18880 /* DR 322. For conversion deduction, remove a reference type on parm
18881 too (which has been swapped into ARG). */
18882 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
18883 *arg = TREE_TYPE (*arg);
18885 return result;
18888 /* Subroutine of unify_one_argument. PARM is a function parameter of a
18889 template which does contain any deducible template parameters; check if
18890 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
18891 unify_one_argument. */
18893 static int
18894 check_non_deducible_conversion (tree parm, tree arg, int strict,
18895 int flags, bool explain_p)
18897 tree type;
18899 if (!TYPE_P (arg))
18900 type = TREE_TYPE (arg);
18901 else
18902 type = arg;
18904 if (same_type_p (parm, type))
18905 return unify_success (explain_p);
18907 if (strict == DEDUCE_CONV)
18909 if (can_convert_arg (type, parm, NULL_TREE, flags,
18910 explain_p ? tf_warning_or_error : tf_none))
18911 return unify_success (explain_p);
18913 else if (strict != DEDUCE_EXACT)
18915 if (can_convert_arg (parm, type,
18916 TYPE_P (arg) ? NULL_TREE : arg,
18917 flags, explain_p ? tf_warning_or_error : tf_none))
18918 return unify_success (explain_p);
18921 if (strict == DEDUCE_EXACT)
18922 return unify_type_mismatch (explain_p, parm, arg);
18923 else
18924 return unify_arg_conversion (explain_p, parm, type, arg);
18927 static bool uses_deducible_template_parms (tree type);
18929 /* Returns true iff the expression EXPR is one from which a template
18930 argument can be deduced. In other words, if it's an undecorated
18931 use of a template non-type parameter. */
18933 static bool
18934 deducible_expression (tree expr)
18936 /* Strip implicit conversions. */
18937 while (CONVERT_EXPR_P (expr))
18938 expr = TREE_OPERAND (expr, 0);
18939 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
18942 /* Returns true iff the array domain DOMAIN uses a template parameter in a
18943 deducible way; that is, if it has a max value of <PARM> - 1. */
18945 static bool
18946 deducible_array_bound (tree domain)
18948 if (domain == NULL_TREE)
18949 return false;
18951 tree max = TYPE_MAX_VALUE (domain);
18952 if (TREE_CODE (max) != MINUS_EXPR)
18953 return false;
18955 return deducible_expression (TREE_OPERAND (max, 0));
18958 /* Returns true iff the template arguments ARGS use a template parameter
18959 in a deducible way. */
18961 static bool
18962 deducible_template_args (tree args)
18964 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
18966 bool deducible;
18967 tree elt = TREE_VEC_ELT (args, i);
18968 if (ARGUMENT_PACK_P (elt))
18969 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
18970 else
18972 if (PACK_EXPANSION_P (elt))
18973 elt = PACK_EXPANSION_PATTERN (elt);
18974 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
18975 deducible = true;
18976 else if (TYPE_P (elt))
18977 deducible = uses_deducible_template_parms (elt);
18978 else
18979 deducible = deducible_expression (elt);
18981 if (deducible)
18982 return true;
18984 return false;
18987 /* Returns true iff TYPE contains any deducible references to template
18988 parameters, as per 14.8.2.5. */
18990 static bool
18991 uses_deducible_template_parms (tree type)
18993 if (PACK_EXPANSION_P (type))
18994 type = PACK_EXPANSION_PATTERN (type);
18996 /* T
18997 cv-list T
18998 TT<T>
18999 TT<i>
19000 TT<> */
19001 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
19002 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
19003 return true;
19005 /* T*
19007 T&& */
19008 if (POINTER_TYPE_P (type))
19009 return uses_deducible_template_parms (TREE_TYPE (type));
19011 /* T[integer-constant ]
19012 type [i] */
19013 if (TREE_CODE (type) == ARRAY_TYPE)
19014 return (uses_deducible_template_parms (TREE_TYPE (type))
19015 || deducible_array_bound (TYPE_DOMAIN (type)));
19017 /* T type ::*
19018 type T::*
19019 T T::*
19020 T (type ::*)()
19021 type (T::*)()
19022 type (type ::*)(T)
19023 type (T::*)(T)
19024 T (type ::*)(T)
19025 T (T::*)()
19026 T (T::*)(T) */
19027 if (TYPE_PTRMEM_P (type))
19028 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
19029 || (uses_deducible_template_parms
19030 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
19032 /* template-name <T> (where template-name refers to a class template)
19033 template-name <i> (where template-name refers to a class template) */
19034 if (CLASS_TYPE_P (type)
19035 && CLASSTYPE_TEMPLATE_INFO (type)
19036 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
19037 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
19038 (CLASSTYPE_TI_ARGS (type)));
19040 /* type (T)
19042 T(T) */
19043 if (TREE_CODE (type) == FUNCTION_TYPE
19044 || TREE_CODE (type) == METHOD_TYPE)
19046 if (uses_deducible_template_parms (TREE_TYPE (type)))
19047 return true;
19048 tree parm = TYPE_ARG_TYPES (type);
19049 if (TREE_CODE (type) == METHOD_TYPE)
19050 parm = TREE_CHAIN (parm);
19051 for (; parm; parm = TREE_CHAIN (parm))
19052 if (uses_deducible_template_parms (TREE_VALUE (parm)))
19053 return true;
19056 return false;
19059 /* Subroutine of type_unification_real and unify_pack_expansion to
19060 handle unification of a single P/A pair. Parameters are as
19061 for those functions. */
19063 static int
19064 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
19065 int subr, unification_kind_t strict,
19066 bool explain_p)
19068 tree arg_expr = NULL_TREE;
19069 int arg_strict;
19071 if (arg == error_mark_node || parm == error_mark_node)
19072 return unify_invalid (explain_p);
19073 if (arg == unknown_type_node)
19074 /* We can't deduce anything from this, but we might get all the
19075 template args from other function args. */
19076 return unify_success (explain_p);
19078 /* Implicit conversions (Clause 4) will be performed on a function
19079 argument to convert it to the type of the corresponding function
19080 parameter if the parameter type contains no template-parameters that
19081 participate in template argument deduction. */
19082 if (strict != DEDUCE_EXACT
19083 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
19084 /* For function parameters with no deducible template parameters,
19085 just return. We'll check non-dependent conversions later. */
19086 return unify_success (explain_p);
19088 switch (strict)
19090 case DEDUCE_CALL:
19091 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
19092 | UNIFY_ALLOW_MORE_CV_QUAL
19093 | UNIFY_ALLOW_DERIVED);
19094 break;
19096 case DEDUCE_CONV:
19097 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
19098 break;
19100 case DEDUCE_EXACT:
19101 arg_strict = UNIFY_ALLOW_NONE;
19102 break;
19104 default:
19105 gcc_unreachable ();
19108 /* We only do these transformations if this is the top-level
19109 parameter_type_list in a call or declaration matching; in other
19110 situations (nested function declarators, template argument lists) we
19111 won't be comparing a type to an expression, and we don't do any type
19112 adjustments. */
19113 if (!subr)
19115 if (!TYPE_P (arg))
19117 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
19118 if (type_unknown_p (arg))
19120 /* [temp.deduct.type] A template-argument can be
19121 deduced from a pointer to function or pointer
19122 to member function argument if the set of
19123 overloaded functions does not contain function
19124 templates and at most one of a set of
19125 overloaded functions provides a unique
19126 match. */
19128 if (resolve_overloaded_unification
19129 (tparms, targs, parm, arg, strict,
19130 arg_strict, explain_p))
19131 return unify_success (explain_p);
19132 return unify_overload_resolution_failure (explain_p, arg);
19135 arg_expr = arg;
19136 arg = unlowered_expr_type (arg);
19137 if (arg == error_mark_node)
19138 return unify_invalid (explain_p);
19141 arg_strict |=
19142 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
19144 else
19145 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
19146 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
19147 return unify_template_argument_mismatch (explain_p, parm, arg);
19149 /* For deduction from an init-list we need the actual list. */
19150 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
19151 arg = arg_expr;
19152 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
19155 /* for_each_template_parm callback that always returns 0. */
19157 static int
19158 zero_r (tree, void *)
19160 return 0;
19163 /* for_each_template_parm any_fn callback to handle deduction of a template
19164 type argument from the type of an array bound. */
19166 static int
19167 array_deduction_r (tree t, void *data)
19169 tree_pair_p d = (tree_pair_p)data;
19170 tree &tparms = d->purpose;
19171 tree &targs = d->value;
19173 if (TREE_CODE (t) == ARRAY_TYPE)
19174 if (tree dom = TYPE_DOMAIN (t))
19175 if (tree max = TYPE_MAX_VALUE (dom))
19177 if (TREE_CODE (max) == MINUS_EXPR)
19178 max = TREE_OPERAND (max, 0);
19179 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
19180 unify (tparms, targs, TREE_TYPE (max), size_type_node,
19181 UNIFY_ALLOW_NONE, /*explain*/false);
19184 /* Keep walking. */
19185 return 0;
19188 /* Try to deduce any not-yet-deduced template type arguments from the type of
19189 an array bound. This is handled separately from unify because 14.8.2.5 says
19190 "The type of a type parameter is only deduced from an array bound if it is
19191 not otherwise deduced." */
19193 static void
19194 try_array_deduction (tree tparms, tree targs, tree parm)
19196 tree_pair_s data = { tparms, targs };
19197 hash_set<tree> visited;
19198 for_each_template_parm (parm, zero_r, &data, &visited,
19199 /*nondeduced*/false, array_deduction_r);
19202 /* Most parms like fn_type_unification.
19204 If SUBR is 1, we're being called recursively (to unify the
19205 arguments of a function or method parameter of a function
19206 template).
19208 CHECKS is a pointer to a vector of access checks encountered while
19209 substituting default template arguments. */
19211 static int
19212 type_unification_real (tree tparms,
19213 tree full_targs,
19214 tree xparms,
19215 const tree *xargs,
19216 unsigned int xnargs,
19217 int subr,
19218 unification_kind_t strict,
19219 int flags,
19220 vec<deferred_access_check, va_gc> **checks,
19221 bool explain_p)
19223 tree parm, arg;
19224 int i;
19225 int ntparms = TREE_VEC_LENGTH (tparms);
19226 int saw_undeduced = 0;
19227 tree parms;
19228 const tree *args;
19229 unsigned int nargs;
19230 unsigned int ia;
19232 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
19233 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
19234 gcc_assert (ntparms > 0);
19236 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
19238 /* Reset the number of non-defaulted template arguments contained
19239 in TARGS. */
19240 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
19242 again:
19243 parms = xparms;
19244 args = xargs;
19245 nargs = xnargs;
19247 ia = 0;
19248 while (parms && parms != void_list_node
19249 && ia < nargs)
19251 parm = TREE_VALUE (parms);
19253 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19254 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
19255 /* For a function parameter pack that occurs at the end of the
19256 parameter-declaration-list, the type A of each remaining
19257 argument of the call is compared with the type P of the
19258 declarator-id of the function parameter pack. */
19259 break;
19261 parms = TREE_CHAIN (parms);
19263 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19264 /* For a function parameter pack that does not occur at the
19265 end of the parameter-declaration-list, the type of the
19266 parameter pack is a non-deduced context. */
19267 continue;
19269 arg = args[ia];
19270 ++ia;
19272 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
19273 explain_p))
19274 return 1;
19277 if (parms
19278 && parms != void_list_node
19279 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
19281 /* Unify the remaining arguments with the pack expansion type. */
19282 tree argvec;
19283 tree parmvec = make_tree_vec (1);
19285 /* Allocate a TREE_VEC and copy in all of the arguments */
19286 argvec = make_tree_vec (nargs - ia);
19287 for (i = 0; ia < nargs; ++ia, ++i)
19288 TREE_VEC_ELT (argvec, i) = args[ia];
19290 /* Copy the parameter into parmvec. */
19291 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
19292 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
19293 /*subr=*/subr, explain_p))
19294 return 1;
19296 /* Advance to the end of the list of parameters. */
19297 parms = TREE_CHAIN (parms);
19300 /* Fail if we've reached the end of the parm list, and more args
19301 are present, and the parm list isn't variadic. */
19302 if (ia < nargs && parms == void_list_node)
19303 return unify_too_many_arguments (explain_p, nargs, ia);
19304 /* Fail if parms are left and they don't have default values and
19305 they aren't all deduced as empty packs (c++/57397). This is
19306 consistent with sufficient_parms_p. */
19307 if (parms && parms != void_list_node
19308 && TREE_PURPOSE (parms) == NULL_TREE)
19310 unsigned int count = nargs;
19311 tree p = parms;
19312 bool type_pack_p;
19315 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
19316 if (!type_pack_p)
19317 count++;
19318 p = TREE_CHAIN (p);
19320 while (p && p != void_list_node);
19321 if (count != nargs)
19322 return unify_too_few_arguments (explain_p, ia, count,
19323 type_pack_p);
19326 if (!subr)
19328 tsubst_flags_t complain = (explain_p
19329 ? tf_warning_or_error
19330 : tf_none);
19331 bool tried_array_deduction = (cxx_dialect < cxx1z);
19333 for (i = 0; i < ntparms; i++)
19335 tree targ = TREE_VEC_ELT (targs, i);
19336 tree tparm = TREE_VEC_ELT (tparms, i);
19338 /* Clear the "incomplete" flags on all argument packs now so that
19339 substituting them into later default arguments works. */
19340 if (targ && ARGUMENT_PACK_P (targ))
19342 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
19343 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
19346 if (targ || tparm == error_mark_node)
19347 continue;
19348 tparm = TREE_VALUE (tparm);
19350 if (TREE_CODE (tparm) == TYPE_DECL
19351 && !tried_array_deduction)
19353 try_array_deduction (tparms, targs, xparms);
19354 tried_array_deduction = true;
19355 if (TREE_VEC_ELT (targs, i))
19356 continue;
19359 /* If this is an undeduced nontype parameter that depends on
19360 a type parameter, try another pass; its type may have been
19361 deduced from a later argument than the one from which
19362 this parameter can be deduced. */
19363 if (TREE_CODE (tparm) == PARM_DECL
19364 && uses_template_parms (TREE_TYPE (tparm))
19365 && saw_undeduced < 2)
19367 saw_undeduced = 1;
19368 continue;
19371 /* Core issue #226 (C++0x) [temp.deduct]:
19373 If a template argument has not been deduced, its
19374 default template argument, if any, is used.
19376 When we are in C++98 mode, TREE_PURPOSE will either
19377 be NULL_TREE or ERROR_MARK_NODE, so we do not need
19378 to explicitly check cxx_dialect here. */
19379 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
19380 /* OK, there is a default argument. Wait until after the
19381 conversion check to do substitution. */
19382 continue;
19384 /* If the type parameter is a parameter pack, then it will
19385 be deduced to an empty parameter pack. */
19386 if (template_parameter_pack_p (tparm))
19388 tree arg;
19390 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
19392 arg = make_node (NONTYPE_ARGUMENT_PACK);
19393 TREE_CONSTANT (arg) = 1;
19395 else
19396 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
19398 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
19400 TREE_VEC_ELT (targs, i) = arg;
19401 continue;
19404 return unify_parameter_deduction_failure (explain_p, tparm);
19407 /* DR 1391: All parameters have args, now check non-dependent parms for
19408 convertibility. */
19409 if (saw_undeduced < 2)
19410 for (ia = 0, parms = xparms, args = xargs, nargs = xnargs;
19411 parms && parms != void_list_node && ia < nargs; )
19413 parm = TREE_VALUE (parms);
19415 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19416 && (!TREE_CHAIN (parms)
19417 || TREE_CHAIN (parms) == void_list_node))
19418 /* For a function parameter pack that occurs at the end of the
19419 parameter-declaration-list, the type A of each remaining
19420 argument of the call is compared with the type P of the
19421 declarator-id of the function parameter pack. */
19422 break;
19424 parms = TREE_CHAIN (parms);
19426 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19427 /* For a function parameter pack that does not occur at the
19428 end of the parameter-declaration-list, the type of the
19429 parameter pack is a non-deduced context. */
19430 continue;
19432 arg = args[ia];
19433 ++ia;
19435 if (uses_template_parms (parm))
19436 continue;
19437 if (check_non_deducible_conversion (parm, arg, strict, flags,
19438 explain_p))
19439 return 1;
19442 /* Now substitute into the default template arguments. */
19443 for (i = 0; i < ntparms; i++)
19445 tree targ = TREE_VEC_ELT (targs, i);
19446 tree tparm = TREE_VEC_ELT (tparms, i);
19448 if (targ || tparm == error_mark_node)
19449 continue;
19450 tree parm = TREE_VALUE (tparm);
19452 if (TREE_CODE (parm) == PARM_DECL
19453 && uses_template_parms (TREE_TYPE (parm))
19454 && saw_undeduced < 2)
19455 continue;
19457 tree arg = TREE_PURPOSE (tparm);
19458 reopen_deferring_access_checks (*checks);
19459 location_t save_loc = input_location;
19460 if (DECL_P (parm))
19461 input_location = DECL_SOURCE_LOCATION (parm);
19462 arg = tsubst_template_arg (arg, full_targs, complain, NULL_TREE);
19463 if (!uses_template_parms (arg))
19464 arg = convert_template_argument (parm, arg, full_targs, complain,
19465 i, NULL_TREE);
19466 else if (saw_undeduced < 2)
19467 arg = NULL_TREE;
19468 else
19469 arg = error_mark_node;
19470 input_location = save_loc;
19471 *checks = get_deferred_access_checks ();
19472 pop_deferring_access_checks ();
19473 if (arg == error_mark_node)
19474 return 1;
19475 else if (arg)
19477 TREE_VEC_ELT (targs, i) = arg;
19478 /* The position of the first default template argument,
19479 is also the number of non-defaulted arguments in TARGS.
19480 Record that. */
19481 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19482 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
19486 if (saw_undeduced++ == 1)
19487 goto again;
19490 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19491 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
19493 return unify_success (explain_p);
19496 /* Subroutine of type_unification_real. Args are like the variables
19497 at the call site. ARG is an overloaded function (or template-id);
19498 we try deducing template args from each of the overloads, and if
19499 only one succeeds, we go with that. Modifies TARGS and returns
19500 true on success. */
19502 static bool
19503 resolve_overloaded_unification (tree tparms,
19504 tree targs,
19505 tree parm,
19506 tree arg,
19507 unification_kind_t strict,
19508 int sub_strict,
19509 bool explain_p)
19511 tree tempargs = copy_node (targs);
19512 int good = 0;
19513 tree goodfn = NULL_TREE;
19514 bool addr_p;
19516 if (TREE_CODE (arg) == ADDR_EXPR)
19518 arg = TREE_OPERAND (arg, 0);
19519 addr_p = true;
19521 else
19522 addr_p = false;
19524 if (TREE_CODE (arg) == COMPONENT_REF)
19525 /* Handle `&x' where `x' is some static or non-static member
19526 function name. */
19527 arg = TREE_OPERAND (arg, 1);
19529 if (TREE_CODE (arg) == OFFSET_REF)
19530 arg = TREE_OPERAND (arg, 1);
19532 /* Strip baselink information. */
19533 if (BASELINK_P (arg))
19534 arg = BASELINK_FUNCTIONS (arg);
19536 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
19538 /* If we got some explicit template args, we need to plug them into
19539 the affected templates before we try to unify, in case the
19540 explicit args will completely resolve the templates in question. */
19542 int ok = 0;
19543 tree expl_subargs = TREE_OPERAND (arg, 1);
19544 arg = TREE_OPERAND (arg, 0);
19546 for (lkp_iterator iter (arg); iter; ++iter)
19548 tree fn = *iter;
19549 tree subargs, elem;
19551 if (TREE_CODE (fn) != TEMPLATE_DECL)
19552 continue;
19554 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19555 expl_subargs, NULL_TREE, tf_none,
19556 /*require_all_args=*/true,
19557 /*use_default_args=*/true);
19558 if (subargs != error_mark_node
19559 && !any_dependent_template_arguments_p (subargs))
19561 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
19562 if (try_one_overload (tparms, targs, tempargs, parm,
19563 elem, strict, sub_strict, addr_p, explain_p)
19564 && (!goodfn || !same_type_p (goodfn, elem)))
19566 goodfn = elem;
19567 ++good;
19570 else if (subargs)
19571 ++ok;
19573 /* If no templates (or more than one) are fully resolved by the
19574 explicit arguments, this template-id is a non-deduced context; it
19575 could still be OK if we deduce all template arguments for the
19576 enclosing call through other arguments. */
19577 if (good != 1)
19578 good = ok;
19580 else if (TREE_CODE (arg) != OVERLOAD
19581 && TREE_CODE (arg) != FUNCTION_DECL)
19582 /* If ARG is, for example, "(0, &f)" then its type will be unknown
19583 -- but the deduction does not succeed because the expression is
19584 not just the function on its own. */
19585 return false;
19586 else
19587 for (lkp_iterator iter (arg); iter; ++iter)
19589 tree fn = *iter;
19590 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
19591 strict, sub_strict, addr_p, explain_p)
19592 && (!goodfn || !decls_match (goodfn, fn)))
19594 goodfn = fn;
19595 ++good;
19599 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19600 to function or pointer to member function argument if the set of
19601 overloaded functions does not contain function templates and at most
19602 one of a set of overloaded functions provides a unique match.
19604 So if we found multiple possibilities, we return success but don't
19605 deduce anything. */
19607 if (good == 1)
19609 int i = TREE_VEC_LENGTH (targs);
19610 for (; i--; )
19611 if (TREE_VEC_ELT (tempargs, i))
19613 tree old = TREE_VEC_ELT (targs, i);
19614 tree new_ = TREE_VEC_ELT (tempargs, i);
19615 if (new_ && old && ARGUMENT_PACK_P (old)
19616 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
19617 /* Don't forget explicit template arguments in a pack. */
19618 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
19619 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
19620 TREE_VEC_ELT (targs, i) = new_;
19623 if (good)
19624 return true;
19626 return false;
19629 /* Core DR 115: In contexts where deduction is done and fails, or in
19630 contexts where deduction is not done, if a template argument list is
19631 specified and it, along with any default template arguments, identifies
19632 a single function template specialization, then the template-id is an
19633 lvalue for the function template specialization. */
19635 tree
19636 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
19638 tree expr, offset, baselink;
19639 bool addr;
19641 if (!type_unknown_p (orig_expr))
19642 return orig_expr;
19644 expr = orig_expr;
19645 addr = false;
19646 offset = NULL_TREE;
19647 baselink = NULL_TREE;
19649 if (TREE_CODE (expr) == ADDR_EXPR)
19651 expr = TREE_OPERAND (expr, 0);
19652 addr = true;
19654 if (TREE_CODE (expr) == OFFSET_REF)
19656 offset = expr;
19657 expr = TREE_OPERAND (expr, 1);
19659 if (BASELINK_P (expr))
19661 baselink = expr;
19662 expr = BASELINK_FUNCTIONS (expr);
19665 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
19667 int good = 0;
19668 tree goodfn = NULL_TREE;
19670 /* If we got some explicit template args, we need to plug them into
19671 the affected templates before we try to unify, in case the
19672 explicit args will completely resolve the templates in question. */
19674 tree expl_subargs = TREE_OPERAND (expr, 1);
19675 tree arg = TREE_OPERAND (expr, 0);
19676 tree badfn = NULL_TREE;
19677 tree badargs = NULL_TREE;
19679 for (lkp_iterator iter (arg); iter; ++iter)
19681 tree fn = *iter;
19682 tree subargs, elem;
19684 if (TREE_CODE (fn) != TEMPLATE_DECL)
19685 continue;
19687 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19688 expl_subargs, NULL_TREE, tf_none,
19689 /*require_all_args=*/true,
19690 /*use_default_args=*/true);
19691 if (subargs != error_mark_node
19692 && !any_dependent_template_arguments_p (subargs))
19694 elem = instantiate_template (fn, subargs, tf_none);
19695 if (elem == error_mark_node)
19697 badfn = fn;
19698 badargs = subargs;
19700 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
19702 goodfn = elem;
19703 ++good;
19707 if (good == 1)
19709 mark_used (goodfn);
19710 expr = goodfn;
19711 if (baselink)
19712 expr = build_baselink (BASELINK_BINFO (baselink),
19713 BASELINK_ACCESS_BINFO (baselink),
19714 expr, BASELINK_OPTYPE (baselink));
19715 if (offset)
19717 tree base
19718 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
19719 expr = build_offset_ref (base, expr, addr, complain);
19721 if (addr)
19722 expr = cp_build_addr_expr (expr, complain);
19723 return expr;
19725 else if (good == 0 && badargs && (complain & tf_error))
19726 /* There were no good options and at least one bad one, so let the
19727 user know what the problem is. */
19728 instantiate_template (badfn, badargs, complain);
19730 return orig_expr;
19733 /* Subroutine of resolve_overloaded_unification; does deduction for a single
19734 overload. Fills TARGS with any deduced arguments, or error_mark_node if
19735 different overloads deduce different arguments for a given parm.
19736 ADDR_P is true if the expression for which deduction is being
19737 performed was of the form "& fn" rather than simply "fn".
19739 Returns 1 on success. */
19741 static int
19742 try_one_overload (tree tparms,
19743 tree orig_targs,
19744 tree targs,
19745 tree parm,
19746 tree arg,
19747 unification_kind_t strict,
19748 int sub_strict,
19749 bool addr_p,
19750 bool explain_p)
19752 int nargs;
19753 tree tempargs;
19754 int i;
19756 if (arg == error_mark_node)
19757 return 0;
19759 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19760 to function or pointer to member function argument if the set of
19761 overloaded functions does not contain function templates and at most
19762 one of a set of overloaded functions provides a unique match.
19764 So if this is a template, just return success. */
19766 if (uses_template_parms (arg))
19767 return 1;
19769 if (TREE_CODE (arg) == METHOD_TYPE)
19770 arg = build_ptrmemfunc_type (build_pointer_type (arg));
19771 else if (addr_p)
19772 arg = build_pointer_type (arg);
19774 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
19776 /* We don't copy orig_targs for this because if we have already deduced
19777 some template args from previous args, unify would complain when we
19778 try to deduce a template parameter for the same argument, even though
19779 there isn't really a conflict. */
19780 nargs = TREE_VEC_LENGTH (targs);
19781 tempargs = make_tree_vec (nargs);
19783 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
19784 return 0;
19786 /* First make sure we didn't deduce anything that conflicts with
19787 explicitly specified args. */
19788 for (i = nargs; i--; )
19790 tree elt = TREE_VEC_ELT (tempargs, i);
19791 tree oldelt = TREE_VEC_ELT (orig_targs, i);
19793 if (!elt)
19794 /*NOP*/;
19795 else if (uses_template_parms (elt))
19796 /* Since we're unifying against ourselves, we will fill in
19797 template args used in the function parm list with our own
19798 template parms. Discard them. */
19799 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
19800 else if (oldelt && ARGUMENT_PACK_P (oldelt))
19802 /* Check that the argument at each index of the deduced argument pack
19803 is equivalent to the corresponding explicitly specified argument.
19804 We may have deduced more arguments than were explicitly specified,
19805 and that's OK. */
19807 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
19808 that's wrong if we deduce the same argument pack from multiple
19809 function arguments: it's only incomplete the first time. */
19811 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
19812 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
19814 if (TREE_VEC_LENGTH (deduced_pack)
19815 < TREE_VEC_LENGTH (explicit_pack))
19816 return 0;
19818 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
19819 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
19820 TREE_VEC_ELT (deduced_pack, j)))
19821 return 0;
19823 else if (oldelt && !template_args_equal (oldelt, elt))
19824 return 0;
19827 for (i = nargs; i--; )
19829 tree elt = TREE_VEC_ELT (tempargs, i);
19831 if (elt)
19832 TREE_VEC_ELT (targs, i) = elt;
19835 return 1;
19838 /* PARM is a template class (perhaps with unbound template
19839 parameters). ARG is a fully instantiated type. If ARG can be
19840 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
19841 TARGS are as for unify. */
19843 static tree
19844 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
19845 bool explain_p)
19847 tree copy_of_targs;
19849 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
19850 return NULL_TREE;
19851 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19852 /* Matches anything. */;
19853 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
19854 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
19855 return NULL_TREE;
19857 /* We need to make a new template argument vector for the call to
19858 unify. If we used TARGS, we'd clutter it up with the result of
19859 the attempted unification, even if this class didn't work out.
19860 We also don't want to commit ourselves to all the unifications
19861 we've already done, since unification is supposed to be done on
19862 an argument-by-argument basis. In other words, consider the
19863 following pathological case:
19865 template <int I, int J, int K>
19866 struct S {};
19868 template <int I, int J>
19869 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
19871 template <int I, int J, int K>
19872 void f(S<I, J, K>, S<I, I, I>);
19874 void g() {
19875 S<0, 0, 0> s0;
19876 S<0, 1, 2> s2;
19878 f(s0, s2);
19881 Now, by the time we consider the unification involving `s2', we
19882 already know that we must have `f<0, 0, 0>'. But, even though
19883 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
19884 because there are two ways to unify base classes of S<0, 1, 2>
19885 with S<I, I, I>. If we kept the already deduced knowledge, we
19886 would reject the possibility I=1. */
19887 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
19889 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19891 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
19892 return NULL_TREE;
19893 return arg;
19896 /* If unification failed, we're done. */
19897 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
19898 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
19899 return NULL_TREE;
19901 return arg;
19904 /* Given a template type PARM and a class type ARG, find the unique
19905 base type in ARG that is an instance of PARM. We do not examine
19906 ARG itself; only its base-classes. If there is not exactly one
19907 appropriate base class, return NULL_TREE. PARM may be the type of
19908 a partial specialization, as well as a plain template type. Used
19909 by unify. */
19911 static enum template_base_result
19912 get_template_base (tree tparms, tree targs, tree parm, tree arg,
19913 bool explain_p, tree *result)
19915 tree rval = NULL_TREE;
19916 tree binfo;
19918 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
19920 binfo = TYPE_BINFO (complete_type (arg));
19921 if (!binfo)
19923 /* The type could not be completed. */
19924 *result = NULL_TREE;
19925 return tbr_incomplete_type;
19928 /* Walk in inheritance graph order. The search order is not
19929 important, and this avoids multiple walks of virtual bases. */
19930 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
19932 tree r = try_class_unification (tparms, targs, parm,
19933 BINFO_TYPE (binfo), explain_p);
19935 if (r)
19937 /* If there is more than one satisfactory baseclass, then:
19939 [temp.deduct.call]
19941 If they yield more than one possible deduced A, the type
19942 deduction fails.
19944 applies. */
19945 if (rval && !same_type_p (r, rval))
19947 *result = NULL_TREE;
19948 return tbr_ambiguous_baseclass;
19951 rval = r;
19955 *result = rval;
19956 return tbr_success;
19959 /* Returns the level of DECL, which declares a template parameter. */
19961 static int
19962 template_decl_level (tree decl)
19964 switch (TREE_CODE (decl))
19966 case TYPE_DECL:
19967 case TEMPLATE_DECL:
19968 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
19970 case PARM_DECL:
19971 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
19973 default:
19974 gcc_unreachable ();
19976 return 0;
19979 /* Decide whether ARG can be unified with PARM, considering only the
19980 cv-qualifiers of each type, given STRICT as documented for unify.
19981 Returns nonzero iff the unification is OK on that basis. */
19983 static int
19984 check_cv_quals_for_unify (int strict, tree arg, tree parm)
19986 int arg_quals = cp_type_quals (arg);
19987 int parm_quals = cp_type_quals (parm);
19989 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
19990 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
19992 /* Although a CVR qualifier is ignored when being applied to a
19993 substituted template parameter ([8.3.2]/1 for example), that
19994 does not allow us to unify "const T" with "int&" because both
19995 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
19996 It is ok when we're allowing additional CV qualifiers
19997 at the outer level [14.8.2.1]/3,1st bullet. */
19998 if ((TREE_CODE (arg) == REFERENCE_TYPE
19999 || TREE_CODE (arg) == FUNCTION_TYPE
20000 || TREE_CODE (arg) == METHOD_TYPE)
20001 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
20002 return 0;
20004 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
20005 && (parm_quals & TYPE_QUAL_RESTRICT))
20006 return 0;
20009 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20010 && (arg_quals & parm_quals) != parm_quals)
20011 return 0;
20013 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
20014 && (parm_quals & arg_quals) != arg_quals)
20015 return 0;
20017 return 1;
20020 /* Determines the LEVEL and INDEX for the template parameter PARM. */
20021 void
20022 template_parm_level_and_index (tree parm, int* level, int* index)
20024 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20025 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20026 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20028 *index = TEMPLATE_TYPE_IDX (parm);
20029 *level = TEMPLATE_TYPE_LEVEL (parm);
20031 else
20033 *index = TEMPLATE_PARM_IDX (parm);
20034 *level = TEMPLATE_PARM_LEVEL (parm);
20038 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
20039 do { \
20040 if (unify (TP, TA, P, A, S, EP)) \
20041 return 1; \
20042 } while (0)
20044 /* Unifies the remaining arguments in PACKED_ARGS with the pack
20045 expansion at the end of PACKED_PARMS. Returns 0 if the type
20046 deduction succeeds, 1 otherwise. STRICT is the same as in
20047 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
20048 function call argument list. We'll need to adjust the arguments to make them
20049 types. SUBR tells us if this is from a recursive call to
20050 type_unification_real, or for comparing two template argument
20051 lists. */
20053 static int
20054 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
20055 tree packed_args, unification_kind_t strict,
20056 bool subr, bool explain_p)
20058 tree parm
20059 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
20060 tree pattern = PACK_EXPANSION_PATTERN (parm);
20061 tree pack, packs = NULL_TREE;
20062 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
20064 packed_args = expand_template_argument_pack (packed_args);
20066 int len = TREE_VEC_LENGTH (packed_args);
20068 /* Determine the parameter packs we will be deducing from the
20069 pattern, and record their current deductions. */
20070 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
20071 pack; pack = TREE_CHAIN (pack))
20073 tree parm_pack = TREE_VALUE (pack);
20074 int idx, level;
20076 /* Determine the index and level of this parameter pack. */
20077 template_parm_level_and_index (parm_pack, &level, &idx);
20079 /* Keep track of the parameter packs and their corresponding
20080 argument packs. */
20081 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
20082 TREE_TYPE (packs) = make_tree_vec (len - start);
20085 /* Loop through all of the arguments that have not yet been
20086 unified and unify each with the pattern. */
20087 for (i = start; i < len; i++)
20089 tree parm;
20090 bool any_explicit = false;
20091 tree arg = TREE_VEC_ELT (packed_args, i);
20093 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
20094 or the element of its argument pack at the current index if
20095 this argument was explicitly specified. */
20096 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20098 int idx, level;
20099 tree arg, pargs;
20100 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20102 arg = NULL_TREE;
20103 if (TREE_VALUE (pack)
20104 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
20105 && (i - start < TREE_VEC_LENGTH (pargs)))
20107 any_explicit = true;
20108 arg = TREE_VEC_ELT (pargs, i - start);
20110 TMPL_ARG (targs, level, idx) = arg;
20113 /* If we had explicit template arguments, substitute them into the
20114 pattern before deduction. */
20115 if (any_explicit)
20117 /* Some arguments might still be unspecified or dependent. */
20118 bool dependent;
20119 ++processing_template_decl;
20120 dependent = any_dependent_template_arguments_p (targs);
20121 if (!dependent)
20122 --processing_template_decl;
20123 parm = tsubst (pattern, targs,
20124 explain_p ? tf_warning_or_error : tf_none,
20125 NULL_TREE);
20126 if (dependent)
20127 --processing_template_decl;
20128 if (parm == error_mark_node)
20129 return 1;
20131 else
20132 parm = pattern;
20134 /* Unify the pattern with the current argument. */
20135 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
20136 explain_p))
20137 return 1;
20139 /* For each parameter pack, collect the deduced value. */
20140 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20142 int idx, level;
20143 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20145 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
20146 TMPL_ARG (targs, level, idx);
20150 /* Verify that the results of unification with the parameter packs
20151 produce results consistent with what we've seen before, and make
20152 the deduced argument packs available. */
20153 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20155 tree old_pack = TREE_VALUE (pack);
20156 tree new_args = TREE_TYPE (pack);
20157 int i, len = TREE_VEC_LENGTH (new_args);
20158 int idx, level;
20159 bool nondeduced_p = false;
20161 /* By default keep the original deduced argument pack.
20162 If necessary, more specific code is going to update the
20163 resulting deduced argument later down in this function. */
20164 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20165 TMPL_ARG (targs, level, idx) = old_pack;
20167 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
20168 actually deduce anything. */
20169 for (i = 0; i < len && !nondeduced_p; ++i)
20170 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
20171 nondeduced_p = true;
20172 if (nondeduced_p)
20173 continue;
20175 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
20177 /* If we had fewer function args than explicit template args,
20178 just use the explicits. */
20179 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20180 int explicit_len = TREE_VEC_LENGTH (explicit_args);
20181 if (len < explicit_len)
20182 new_args = explicit_args;
20185 if (!old_pack)
20187 tree result;
20188 /* Build the deduced *_ARGUMENT_PACK. */
20189 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
20191 result = make_node (NONTYPE_ARGUMENT_PACK);
20192 TREE_CONSTANT (result) = 1;
20194 else
20195 result = cxx_make_type (TYPE_ARGUMENT_PACK);
20197 SET_ARGUMENT_PACK_ARGS (result, new_args);
20199 /* Note the deduced argument packs for this parameter
20200 pack. */
20201 TMPL_ARG (targs, level, idx) = result;
20203 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
20204 && (ARGUMENT_PACK_ARGS (old_pack)
20205 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
20207 /* We only had the explicitly-provided arguments before, but
20208 now we have a complete set of arguments. */
20209 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20211 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
20212 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
20213 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
20215 else
20217 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
20218 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
20220 if (!comp_template_args (old_args, new_args,
20221 &bad_old_arg, &bad_new_arg))
20222 /* Inconsistent unification of this parameter pack. */
20223 return unify_parameter_pack_inconsistent (explain_p,
20224 bad_old_arg,
20225 bad_new_arg);
20229 return unify_success (explain_p);
20232 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
20233 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
20234 parameters and return value are as for unify. */
20236 static int
20237 unify_array_domain (tree tparms, tree targs,
20238 tree parm_dom, tree arg_dom,
20239 bool explain_p)
20241 tree parm_max;
20242 tree arg_max;
20243 bool parm_cst;
20244 bool arg_cst;
20246 /* Our representation of array types uses "N - 1" as the
20247 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
20248 not an integer constant. We cannot unify arbitrarily
20249 complex expressions, so we eliminate the MINUS_EXPRs
20250 here. */
20251 parm_max = TYPE_MAX_VALUE (parm_dom);
20252 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
20253 if (!parm_cst)
20255 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
20256 parm_max = TREE_OPERAND (parm_max, 0);
20258 arg_max = TYPE_MAX_VALUE (arg_dom);
20259 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
20260 if (!arg_cst)
20262 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
20263 trying to unify the type of a variable with the type
20264 of a template parameter. For example:
20266 template <unsigned int N>
20267 void f (char (&) [N]);
20268 int g();
20269 void h(int i) {
20270 char a[g(i)];
20271 f(a);
20274 Here, the type of the ARG will be "int [g(i)]", and
20275 may be a SAVE_EXPR, etc. */
20276 if (TREE_CODE (arg_max) != MINUS_EXPR)
20277 return unify_vla_arg (explain_p, arg_dom);
20278 arg_max = TREE_OPERAND (arg_max, 0);
20281 /* If only one of the bounds used a MINUS_EXPR, compensate
20282 by adding one to the other bound. */
20283 if (parm_cst && !arg_cst)
20284 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
20285 integer_type_node,
20286 parm_max,
20287 integer_one_node);
20288 else if (arg_cst && !parm_cst)
20289 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
20290 integer_type_node,
20291 arg_max,
20292 integer_one_node);
20294 return unify (tparms, targs, parm_max, arg_max,
20295 UNIFY_ALLOW_INTEGER, explain_p);
20298 /* Returns whether T, a P or A in unify, is a type, template or expression. */
20300 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
20302 static pa_kind_t
20303 pa_kind (tree t)
20305 if (PACK_EXPANSION_P (t))
20306 t = PACK_EXPANSION_PATTERN (t);
20307 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
20308 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
20309 || DECL_TYPE_TEMPLATE_P (t))
20310 return pa_tmpl;
20311 else if (TYPE_P (t))
20312 return pa_type;
20313 else
20314 return pa_expr;
20317 /* Deduce the value of template parameters. TPARMS is the (innermost)
20318 set of template parameters to a template. TARGS is the bindings
20319 for those template parameters, as determined thus far; TARGS may
20320 include template arguments for outer levels of template parameters
20321 as well. PARM is a parameter to a template function, or a
20322 subcomponent of that parameter; ARG is the corresponding argument.
20323 This function attempts to match PARM with ARG in a manner
20324 consistent with the existing assignments in TARGS. If more values
20325 are deduced, then TARGS is updated.
20327 Returns 0 if the type deduction succeeds, 1 otherwise. The
20328 parameter STRICT is a bitwise or of the following flags:
20330 UNIFY_ALLOW_NONE:
20331 Require an exact match between PARM and ARG.
20332 UNIFY_ALLOW_MORE_CV_QUAL:
20333 Allow the deduced ARG to be more cv-qualified (by qualification
20334 conversion) than ARG.
20335 UNIFY_ALLOW_LESS_CV_QUAL:
20336 Allow the deduced ARG to be less cv-qualified than ARG.
20337 UNIFY_ALLOW_DERIVED:
20338 Allow the deduced ARG to be a template base class of ARG,
20339 or a pointer to a template base class of the type pointed to by
20340 ARG.
20341 UNIFY_ALLOW_INTEGER:
20342 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
20343 case for more information.
20344 UNIFY_ALLOW_OUTER_LEVEL:
20345 This is the outermost level of a deduction. Used to determine validity
20346 of qualification conversions. A valid qualification conversion must
20347 have const qualified pointers leading up to the inner type which
20348 requires additional CV quals, except at the outer level, where const
20349 is not required [conv.qual]. It would be normal to set this flag in
20350 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
20351 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
20352 This is the outermost level of a deduction, and PARM can be more CV
20353 qualified at this point.
20354 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
20355 This is the outermost level of a deduction, and PARM can be less CV
20356 qualified at this point. */
20358 static int
20359 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
20360 bool explain_p)
20362 int idx;
20363 tree targ;
20364 tree tparm;
20365 int strict_in = strict;
20366 tsubst_flags_t complain = (explain_p
20367 ? tf_warning_or_error
20368 : tf_none);
20370 /* I don't think this will do the right thing with respect to types.
20371 But the only case I've seen it in so far has been array bounds, where
20372 signedness is the only information lost, and I think that will be
20373 okay. */
20374 while (CONVERT_EXPR_P (parm))
20375 parm = TREE_OPERAND (parm, 0);
20377 if (arg == error_mark_node)
20378 return unify_invalid (explain_p);
20379 if (arg == unknown_type_node
20380 || arg == init_list_type_node)
20381 /* We can't deduce anything from this, but we might get all the
20382 template args from other function args. */
20383 return unify_success (explain_p);
20385 if (parm == any_targ_node || arg == any_targ_node)
20386 return unify_success (explain_p);
20388 /* If PARM uses template parameters, then we can't bail out here,
20389 even if ARG == PARM, since we won't record unifications for the
20390 template parameters. We might need them if we're trying to
20391 figure out which of two things is more specialized. */
20392 if (arg == parm && !uses_template_parms (parm))
20393 return unify_success (explain_p);
20395 /* Handle init lists early, so the rest of the function can assume
20396 we're dealing with a type. */
20397 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
20399 tree elt, elttype;
20400 unsigned i;
20401 tree orig_parm = parm;
20403 /* Replace T with std::initializer_list<T> for deduction. */
20404 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20405 && flag_deduce_init_list)
20406 parm = listify (parm);
20408 if (!is_std_init_list (parm)
20409 && TREE_CODE (parm) != ARRAY_TYPE)
20410 /* We can only deduce from an initializer list argument if the
20411 parameter is std::initializer_list or an array; otherwise this
20412 is a non-deduced context. */
20413 return unify_success (explain_p);
20415 if (TREE_CODE (parm) == ARRAY_TYPE)
20416 elttype = TREE_TYPE (parm);
20417 else
20419 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
20420 /* Deduction is defined in terms of a single type, so just punt
20421 on the (bizarre) std::initializer_list<T...>. */
20422 if (PACK_EXPANSION_P (elttype))
20423 return unify_success (explain_p);
20426 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
20428 int elt_strict = strict;
20430 if (elt == error_mark_node)
20431 return unify_invalid (explain_p);
20433 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
20435 tree type = TREE_TYPE (elt);
20436 if (type == error_mark_node)
20437 return unify_invalid (explain_p);
20438 /* It should only be possible to get here for a call. */
20439 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
20440 elt_strict |= maybe_adjust_types_for_deduction
20441 (DEDUCE_CALL, &elttype, &type, elt);
20442 elt = type;
20445 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
20446 explain_p);
20449 if (TREE_CODE (parm) == ARRAY_TYPE
20450 && deducible_array_bound (TYPE_DOMAIN (parm)))
20452 /* Also deduce from the length of the initializer list. */
20453 tree max = size_int (CONSTRUCTOR_NELTS (arg));
20454 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
20455 if (idx == error_mark_node)
20456 return unify_invalid (explain_p);
20457 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20458 idx, explain_p);
20461 /* If the std::initializer_list<T> deduction worked, replace the
20462 deduced A with std::initializer_list<A>. */
20463 if (orig_parm != parm)
20465 idx = TEMPLATE_TYPE_IDX (orig_parm);
20466 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20467 targ = listify (targ);
20468 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
20470 return unify_success (explain_p);
20473 /* If parm and arg aren't the same kind of thing (template, type, or
20474 expression), fail early. */
20475 if (pa_kind (parm) != pa_kind (arg))
20476 return unify_invalid (explain_p);
20478 /* Immediately reject some pairs that won't unify because of
20479 cv-qualification mismatches. */
20480 if (TREE_CODE (arg) == TREE_CODE (parm)
20481 && TYPE_P (arg)
20482 /* It is the elements of the array which hold the cv quals of an array
20483 type, and the elements might be template type parms. We'll check
20484 when we recurse. */
20485 && TREE_CODE (arg) != ARRAY_TYPE
20486 /* We check the cv-qualifiers when unifying with template type
20487 parameters below. We want to allow ARG `const T' to unify with
20488 PARM `T' for example, when computing which of two templates
20489 is more specialized, for example. */
20490 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
20491 && !check_cv_quals_for_unify (strict_in, arg, parm))
20492 return unify_cv_qual_mismatch (explain_p, parm, arg);
20494 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
20495 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
20496 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
20497 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
20498 strict &= ~UNIFY_ALLOW_DERIVED;
20499 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
20500 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
20502 switch (TREE_CODE (parm))
20504 case TYPENAME_TYPE:
20505 case SCOPE_REF:
20506 case UNBOUND_CLASS_TEMPLATE:
20507 /* In a type which contains a nested-name-specifier, template
20508 argument values cannot be deduced for template parameters used
20509 within the nested-name-specifier. */
20510 return unify_success (explain_p);
20512 case TEMPLATE_TYPE_PARM:
20513 case TEMPLATE_TEMPLATE_PARM:
20514 case BOUND_TEMPLATE_TEMPLATE_PARM:
20515 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20516 if (error_operand_p (tparm))
20517 return unify_invalid (explain_p);
20519 if (TEMPLATE_TYPE_LEVEL (parm)
20520 != template_decl_level (tparm))
20521 /* The PARM is not one we're trying to unify. Just check
20522 to see if it matches ARG. */
20524 if (TREE_CODE (arg) == TREE_CODE (parm)
20525 && (is_auto (parm) ? is_auto (arg)
20526 : same_type_p (parm, arg)))
20527 return unify_success (explain_p);
20528 else
20529 return unify_type_mismatch (explain_p, parm, arg);
20531 idx = TEMPLATE_TYPE_IDX (parm);
20532 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20533 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
20534 if (error_operand_p (tparm))
20535 return unify_invalid (explain_p);
20537 /* Check for mixed types and values. */
20538 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20539 && TREE_CODE (tparm) != TYPE_DECL)
20540 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20541 && TREE_CODE (tparm) != TEMPLATE_DECL))
20542 gcc_unreachable ();
20544 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20546 if ((strict_in & UNIFY_ALLOW_DERIVED)
20547 && CLASS_TYPE_P (arg))
20549 /* First try to match ARG directly. */
20550 tree t = try_class_unification (tparms, targs, parm, arg,
20551 explain_p);
20552 if (!t)
20554 /* Otherwise, look for a suitable base of ARG, as below. */
20555 enum template_base_result r;
20556 r = get_template_base (tparms, targs, parm, arg,
20557 explain_p, &t);
20558 if (!t)
20559 return unify_no_common_base (explain_p, r, parm, arg);
20560 arg = t;
20563 /* ARG must be constructed from a template class or a template
20564 template parameter. */
20565 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
20566 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20567 return unify_template_deduction_failure (explain_p, parm, arg);
20569 /* Deduce arguments T, i from TT<T> or TT<i>. */
20570 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
20571 return 1;
20573 arg = TYPE_TI_TEMPLATE (arg);
20575 /* Fall through to deduce template name. */
20578 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20579 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20581 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
20583 /* Simple cases: Value already set, does match or doesn't. */
20584 if (targ != NULL_TREE && template_args_equal (targ, arg))
20585 return unify_success (explain_p);
20586 else if (targ)
20587 return unify_inconsistency (explain_p, parm, targ, arg);
20589 else
20591 /* If PARM is `const T' and ARG is only `int', we don't have
20592 a match unless we are allowing additional qualification.
20593 If ARG is `const int' and PARM is just `T' that's OK;
20594 that binds `const int' to `T'. */
20595 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
20596 arg, parm))
20597 return unify_cv_qual_mismatch (explain_p, parm, arg);
20599 /* Consider the case where ARG is `const volatile int' and
20600 PARM is `const T'. Then, T should be `volatile int'. */
20601 arg = cp_build_qualified_type_real
20602 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
20603 if (arg == error_mark_node)
20604 return unify_invalid (explain_p);
20606 /* Simple cases: Value already set, does match or doesn't. */
20607 if (targ != NULL_TREE && same_type_p (targ, arg))
20608 return unify_success (explain_p);
20609 else if (targ)
20610 return unify_inconsistency (explain_p, parm, targ, arg);
20612 /* Make sure that ARG is not a variable-sized array. (Note
20613 that were talking about variable-sized arrays (like
20614 `int[n]'), rather than arrays of unknown size (like
20615 `int[]').) We'll get very confused by such a type since
20616 the bound of the array is not constant, and therefore
20617 not mangleable. Besides, such types are not allowed in
20618 ISO C++, so we can do as we please here. We do allow
20619 them for 'auto' deduction, since that isn't ABI-exposed. */
20620 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
20621 return unify_vla_arg (explain_p, arg);
20623 /* Strip typedefs as in convert_template_argument. */
20624 arg = canonicalize_type_argument (arg, tf_none);
20627 /* If ARG is a parameter pack or an expansion, we cannot unify
20628 against it unless PARM is also a parameter pack. */
20629 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20630 && !template_parameter_pack_p (parm))
20631 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20633 /* If the argument deduction results is a METHOD_TYPE,
20634 then there is a problem.
20635 METHOD_TYPE doesn't map to any real C++ type the result of
20636 the deduction can not be of that type. */
20637 if (TREE_CODE (arg) == METHOD_TYPE)
20638 return unify_method_type_error (explain_p, arg);
20640 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20641 return unify_success (explain_p);
20643 case TEMPLATE_PARM_INDEX:
20644 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20645 if (error_operand_p (tparm))
20646 return unify_invalid (explain_p);
20648 if (TEMPLATE_PARM_LEVEL (parm)
20649 != template_decl_level (tparm))
20651 /* The PARM is not one we're trying to unify. Just check
20652 to see if it matches ARG. */
20653 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
20654 && cp_tree_equal (parm, arg));
20655 if (result)
20656 unify_expression_unequal (explain_p, parm, arg);
20657 return result;
20660 idx = TEMPLATE_PARM_IDX (parm);
20661 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20663 if (targ)
20665 int x = !cp_tree_equal (targ, arg);
20666 if (x)
20667 unify_inconsistency (explain_p, parm, targ, arg);
20668 return x;
20671 /* [temp.deduct.type] If, in the declaration of a function template
20672 with a non-type template-parameter, the non-type
20673 template-parameter is used in an expression in the function
20674 parameter-list and, if the corresponding template-argument is
20675 deduced, the template-argument type shall match the type of the
20676 template-parameter exactly, except that a template-argument
20677 deduced from an array bound may be of any integral type.
20678 The non-type parameter might use already deduced type parameters. */
20679 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
20680 if (tree a = type_uses_auto (tparm))
20682 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
20683 if (tparm == error_mark_node)
20684 return 1;
20687 if (!TREE_TYPE (arg))
20688 /* Template-parameter dependent expression. Just accept it for now.
20689 It will later be processed in convert_template_argument. */
20691 else if (same_type_p (non_reference (TREE_TYPE (arg)),
20692 non_reference (tparm)))
20693 /* OK */;
20694 else if ((strict & UNIFY_ALLOW_INTEGER)
20695 && CP_INTEGRAL_TYPE_P (tparm))
20696 /* Convert the ARG to the type of PARM; the deduced non-type
20697 template argument must exactly match the types of the
20698 corresponding parameter. */
20699 arg = fold (build_nop (tparm, arg));
20700 else if (uses_template_parms (tparm))
20702 /* We haven't deduced the type of this parameter yet. */
20703 if (cxx_dialect >= cxx1z
20704 /* We deduce from array bounds in try_array_deduction. */
20705 && !(strict & UNIFY_ALLOW_INTEGER))
20707 /* Deduce it from the non-type argument. */
20708 tree atype = TREE_TYPE (arg);
20709 RECUR_AND_CHECK_FAILURE (tparms, targs,
20710 tparm, atype,
20711 UNIFY_ALLOW_NONE, explain_p);
20713 else
20714 /* Try again later. */
20715 return unify_success (explain_p);
20717 else
20718 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
20720 /* If ARG is a parameter pack or an expansion, we cannot unify
20721 against it unless PARM is also a parameter pack. */
20722 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20723 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
20724 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20727 bool removed_attr = false;
20728 arg = strip_typedefs_expr (arg, &removed_attr);
20730 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20731 return unify_success (explain_p);
20733 case PTRMEM_CST:
20735 /* A pointer-to-member constant can be unified only with
20736 another constant. */
20737 if (TREE_CODE (arg) != PTRMEM_CST)
20738 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
20740 /* Just unify the class member. It would be useless (and possibly
20741 wrong, depending on the strict flags) to unify also
20742 PTRMEM_CST_CLASS, because we want to be sure that both parm and
20743 arg refer to the same variable, even if through different
20744 classes. For instance:
20746 struct A { int x; };
20747 struct B : A { };
20749 Unification of &A::x and &B::x must succeed. */
20750 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
20751 PTRMEM_CST_MEMBER (arg), strict, explain_p);
20754 case POINTER_TYPE:
20756 if (!TYPE_PTR_P (arg))
20757 return unify_type_mismatch (explain_p, parm, arg);
20759 /* [temp.deduct.call]
20761 A can be another pointer or pointer to member type that can
20762 be converted to the deduced A via a qualification
20763 conversion (_conv.qual_).
20765 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
20766 This will allow for additional cv-qualification of the
20767 pointed-to types if appropriate. */
20769 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
20770 /* The derived-to-base conversion only persists through one
20771 level of pointers. */
20772 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
20774 return unify (tparms, targs, TREE_TYPE (parm),
20775 TREE_TYPE (arg), strict, explain_p);
20778 case REFERENCE_TYPE:
20779 if (TREE_CODE (arg) != REFERENCE_TYPE)
20780 return unify_type_mismatch (explain_p, parm, arg);
20781 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
20782 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
20784 case ARRAY_TYPE:
20785 if (TREE_CODE (arg) != ARRAY_TYPE)
20786 return unify_type_mismatch (explain_p, parm, arg);
20787 if ((TYPE_DOMAIN (parm) == NULL_TREE)
20788 != (TYPE_DOMAIN (arg) == NULL_TREE))
20789 return unify_type_mismatch (explain_p, parm, arg);
20790 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
20791 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
20792 if (TYPE_DOMAIN (parm) != NULL_TREE)
20793 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20794 TYPE_DOMAIN (arg), explain_p);
20795 return unify_success (explain_p);
20797 case REAL_TYPE:
20798 case COMPLEX_TYPE:
20799 case VECTOR_TYPE:
20800 case INTEGER_TYPE:
20801 case BOOLEAN_TYPE:
20802 case ENUMERAL_TYPE:
20803 case VOID_TYPE:
20804 case NULLPTR_TYPE:
20805 if (TREE_CODE (arg) != TREE_CODE (parm))
20806 return unify_type_mismatch (explain_p, parm, arg);
20808 /* We have already checked cv-qualification at the top of the
20809 function. */
20810 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
20811 return unify_type_mismatch (explain_p, parm, arg);
20813 /* As far as unification is concerned, this wins. Later checks
20814 will invalidate it if necessary. */
20815 return unify_success (explain_p);
20817 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
20818 /* Type INTEGER_CST can come from ordinary constant template args. */
20819 case INTEGER_CST:
20820 while (CONVERT_EXPR_P (arg))
20821 arg = TREE_OPERAND (arg, 0);
20823 if (TREE_CODE (arg) != INTEGER_CST)
20824 return unify_template_argument_mismatch (explain_p, parm, arg);
20825 return (tree_int_cst_equal (parm, arg)
20826 ? unify_success (explain_p)
20827 : unify_template_argument_mismatch (explain_p, parm, arg));
20829 case TREE_VEC:
20831 int i, len, argslen;
20832 int parm_variadic_p = 0;
20834 if (TREE_CODE (arg) != TREE_VEC)
20835 return unify_template_argument_mismatch (explain_p, parm, arg);
20837 len = TREE_VEC_LENGTH (parm);
20838 argslen = TREE_VEC_LENGTH (arg);
20840 /* Check for pack expansions in the parameters. */
20841 for (i = 0; i < len; ++i)
20843 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
20845 if (i == len - 1)
20846 /* We can unify against something with a trailing
20847 parameter pack. */
20848 parm_variadic_p = 1;
20849 else
20850 /* [temp.deduct.type]/9: If the template argument list of
20851 P contains a pack expansion that is not the last
20852 template argument, the entire template argument list
20853 is a non-deduced context. */
20854 return unify_success (explain_p);
20858 /* If we don't have enough arguments to satisfy the parameters
20859 (not counting the pack expression at the end), or we have
20860 too many arguments for a parameter list that doesn't end in
20861 a pack expression, we can't unify. */
20862 if (parm_variadic_p
20863 ? argslen < len - parm_variadic_p
20864 : argslen != len)
20865 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
20867 /* Unify all of the parameters that precede the (optional)
20868 pack expression. */
20869 for (i = 0; i < len - parm_variadic_p; ++i)
20871 RECUR_AND_CHECK_FAILURE (tparms, targs,
20872 TREE_VEC_ELT (parm, i),
20873 TREE_VEC_ELT (arg, i),
20874 UNIFY_ALLOW_NONE, explain_p);
20876 if (parm_variadic_p)
20877 return unify_pack_expansion (tparms, targs, parm, arg,
20878 DEDUCE_EXACT,
20879 /*subr=*/true, explain_p);
20880 return unify_success (explain_p);
20883 case RECORD_TYPE:
20884 case UNION_TYPE:
20885 if (TREE_CODE (arg) != TREE_CODE (parm))
20886 return unify_type_mismatch (explain_p, parm, arg);
20888 if (TYPE_PTRMEMFUNC_P (parm))
20890 if (!TYPE_PTRMEMFUNC_P (arg))
20891 return unify_type_mismatch (explain_p, parm, arg);
20893 return unify (tparms, targs,
20894 TYPE_PTRMEMFUNC_FN_TYPE (parm),
20895 TYPE_PTRMEMFUNC_FN_TYPE (arg),
20896 strict, explain_p);
20898 else if (TYPE_PTRMEMFUNC_P (arg))
20899 return unify_type_mismatch (explain_p, parm, arg);
20901 if (CLASSTYPE_TEMPLATE_INFO (parm))
20903 tree t = NULL_TREE;
20905 if (strict_in & UNIFY_ALLOW_DERIVED)
20907 /* First, we try to unify the PARM and ARG directly. */
20908 t = try_class_unification (tparms, targs,
20909 parm, arg, explain_p);
20911 if (!t)
20913 /* Fallback to the special case allowed in
20914 [temp.deduct.call]:
20916 If P is a class, and P has the form
20917 template-id, then A can be a derived class of
20918 the deduced A. Likewise, if P is a pointer to
20919 a class of the form template-id, A can be a
20920 pointer to a derived class pointed to by the
20921 deduced A. */
20922 enum template_base_result r;
20923 r = get_template_base (tparms, targs, parm, arg,
20924 explain_p, &t);
20926 if (!t)
20928 /* Don't give the derived diagnostic if we're
20929 already dealing with the same template. */
20930 bool same_template
20931 = (CLASSTYPE_TEMPLATE_INFO (arg)
20932 && (CLASSTYPE_TI_TEMPLATE (parm)
20933 == CLASSTYPE_TI_TEMPLATE (arg)));
20934 return unify_no_common_base (explain_p && !same_template,
20935 r, parm, arg);
20939 else if (CLASSTYPE_TEMPLATE_INFO (arg)
20940 && (CLASSTYPE_TI_TEMPLATE (parm)
20941 == CLASSTYPE_TI_TEMPLATE (arg)))
20942 /* Perhaps PARM is something like S<U> and ARG is S<int>.
20943 Then, we should unify `int' and `U'. */
20944 t = arg;
20945 else
20946 /* There's no chance of unification succeeding. */
20947 return unify_type_mismatch (explain_p, parm, arg);
20949 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
20950 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
20952 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
20953 return unify_type_mismatch (explain_p, parm, arg);
20954 return unify_success (explain_p);
20956 case METHOD_TYPE:
20957 case FUNCTION_TYPE:
20959 unsigned int nargs;
20960 tree *args;
20961 tree a;
20962 unsigned int i;
20964 if (TREE_CODE (arg) != TREE_CODE (parm))
20965 return unify_type_mismatch (explain_p, parm, arg);
20967 /* CV qualifications for methods can never be deduced, they must
20968 match exactly. We need to check them explicitly here,
20969 because type_unification_real treats them as any other
20970 cv-qualified parameter. */
20971 if (TREE_CODE (parm) == METHOD_TYPE
20972 && (!check_cv_quals_for_unify
20973 (UNIFY_ALLOW_NONE,
20974 class_of_this_parm (arg),
20975 class_of_this_parm (parm))))
20976 return unify_cv_qual_mismatch (explain_p, parm, arg);
20977 if (TREE_CODE (arg) == FUNCTION_TYPE
20978 && type_memfn_quals (parm) != type_memfn_quals (arg))
20979 return unify_cv_qual_mismatch (explain_p, parm, arg);
20980 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
20981 return unify_type_mismatch (explain_p, parm, arg);
20983 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
20984 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
20986 nargs = list_length (TYPE_ARG_TYPES (arg));
20987 args = XALLOCAVEC (tree, nargs);
20988 for (a = TYPE_ARG_TYPES (arg), i = 0;
20989 a != NULL_TREE && a != void_list_node;
20990 a = TREE_CHAIN (a), ++i)
20991 args[i] = TREE_VALUE (a);
20992 nargs = i;
20994 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
20995 args, nargs, 1, DEDUCE_EXACT,
20996 LOOKUP_NORMAL, NULL, explain_p))
20997 return 1;
20999 if (flag_noexcept_type)
21001 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
21002 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
21003 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
21004 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
21005 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
21006 && uses_template_parms (TREE_PURPOSE (pspec)))
21007 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
21008 TREE_PURPOSE (aspec),
21009 UNIFY_ALLOW_NONE, explain_p);
21010 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
21011 return unify_type_mismatch (explain_p, parm, arg);
21014 return 0;
21017 case OFFSET_TYPE:
21018 /* Unify a pointer to member with a pointer to member function, which
21019 deduces the type of the member as a function type. */
21020 if (TYPE_PTRMEMFUNC_P (arg))
21022 /* Check top-level cv qualifiers */
21023 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
21024 return unify_cv_qual_mismatch (explain_p, parm, arg);
21026 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21027 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
21028 UNIFY_ALLOW_NONE, explain_p);
21030 /* Determine the type of the function we are unifying against. */
21031 tree fntype = static_fn_type (arg);
21033 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
21036 if (TREE_CODE (arg) != OFFSET_TYPE)
21037 return unify_type_mismatch (explain_p, parm, arg);
21038 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21039 TYPE_OFFSET_BASETYPE (arg),
21040 UNIFY_ALLOW_NONE, explain_p);
21041 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21042 strict, explain_p);
21044 case CONST_DECL:
21045 if (DECL_TEMPLATE_PARM_P (parm))
21046 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
21047 if (arg != scalar_constant_value (parm))
21048 return unify_template_argument_mismatch (explain_p, parm, arg);
21049 return unify_success (explain_p);
21051 case FIELD_DECL:
21052 case TEMPLATE_DECL:
21053 /* Matched cases are handled by the ARG == PARM test above. */
21054 return unify_template_argument_mismatch (explain_p, parm, arg);
21056 case VAR_DECL:
21057 /* We might get a variable as a non-type template argument in parm if the
21058 corresponding parameter is type-dependent. Make any necessary
21059 adjustments based on whether arg is a reference. */
21060 if (CONSTANT_CLASS_P (arg))
21061 parm = fold_non_dependent_expr (parm);
21062 else if (REFERENCE_REF_P (arg))
21064 tree sub = TREE_OPERAND (arg, 0);
21065 STRIP_NOPS (sub);
21066 if (TREE_CODE (sub) == ADDR_EXPR)
21067 arg = TREE_OPERAND (sub, 0);
21069 /* Now use the normal expression code to check whether they match. */
21070 goto expr;
21072 case TYPE_ARGUMENT_PACK:
21073 case NONTYPE_ARGUMENT_PACK:
21074 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
21075 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
21077 case TYPEOF_TYPE:
21078 case DECLTYPE_TYPE:
21079 case UNDERLYING_TYPE:
21080 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
21081 or UNDERLYING_TYPE nodes. */
21082 return unify_success (explain_p);
21084 case ERROR_MARK:
21085 /* Unification fails if we hit an error node. */
21086 return unify_invalid (explain_p);
21088 case INDIRECT_REF:
21089 if (REFERENCE_REF_P (parm))
21091 bool pexp = PACK_EXPANSION_P (arg);
21092 if (pexp)
21093 arg = PACK_EXPANSION_PATTERN (arg);
21094 if (REFERENCE_REF_P (arg))
21095 arg = TREE_OPERAND (arg, 0);
21096 if (pexp)
21097 arg = make_pack_expansion (arg);
21098 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
21099 strict, explain_p);
21101 /* FALLTHRU */
21103 default:
21104 /* An unresolved overload is a nondeduced context. */
21105 if (is_overloaded_fn (parm) || type_unknown_p (parm))
21106 return unify_success (explain_p);
21107 gcc_assert (EXPR_P (parm) || TREE_CODE (parm) == TRAIT_EXPR);
21108 expr:
21109 /* We must be looking at an expression. This can happen with
21110 something like:
21112 template <int I>
21113 void foo(S<I>, S<I + 2>);
21115 This is a "nondeduced context":
21117 [deduct.type]
21119 The nondeduced contexts are:
21121 --A type that is a template-id in which one or more of
21122 the template-arguments is an expression that references
21123 a template-parameter.
21125 In these cases, we assume deduction succeeded, but don't
21126 actually infer any unifications. */
21128 if (!uses_template_parms (parm)
21129 && !template_args_equal (parm, arg))
21130 return unify_expression_unequal (explain_p, parm, arg);
21131 else
21132 return unify_success (explain_p);
21135 #undef RECUR_AND_CHECK_FAILURE
21137 /* Note that DECL can be defined in this translation unit, if
21138 required. */
21140 static void
21141 mark_definable (tree decl)
21143 tree clone;
21144 DECL_NOT_REALLY_EXTERN (decl) = 1;
21145 FOR_EACH_CLONE (clone, decl)
21146 DECL_NOT_REALLY_EXTERN (clone) = 1;
21149 /* Called if RESULT is explicitly instantiated, or is a member of an
21150 explicitly instantiated class. */
21152 void
21153 mark_decl_instantiated (tree result, int extern_p)
21155 SET_DECL_EXPLICIT_INSTANTIATION (result);
21157 /* If this entity has already been written out, it's too late to
21158 make any modifications. */
21159 if (TREE_ASM_WRITTEN (result))
21160 return;
21162 /* For anonymous namespace we don't need to do anything. */
21163 if (decl_anon_ns_mem_p (result))
21165 gcc_assert (!TREE_PUBLIC (result));
21166 return;
21169 if (TREE_CODE (result) != FUNCTION_DECL)
21170 /* The TREE_PUBLIC flag for function declarations will have been
21171 set correctly by tsubst. */
21172 TREE_PUBLIC (result) = 1;
21174 /* This might have been set by an earlier implicit instantiation. */
21175 DECL_COMDAT (result) = 0;
21177 if (extern_p)
21178 DECL_NOT_REALLY_EXTERN (result) = 0;
21179 else
21181 mark_definable (result);
21182 mark_needed (result);
21183 /* Always make artificials weak. */
21184 if (DECL_ARTIFICIAL (result) && flag_weak)
21185 comdat_linkage (result);
21186 /* For WIN32 we also want to put explicit instantiations in
21187 linkonce sections. */
21188 else if (TREE_PUBLIC (result))
21189 maybe_make_one_only (result);
21192 /* If EXTERN_P, then this function will not be emitted -- unless
21193 followed by an explicit instantiation, at which point its linkage
21194 will be adjusted. If !EXTERN_P, then this function will be
21195 emitted here. In neither circumstance do we want
21196 import_export_decl to adjust the linkage. */
21197 DECL_INTERFACE_KNOWN (result) = 1;
21200 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
21201 important template arguments. If any are missing, we check whether
21202 they're important by using error_mark_node for substituting into any
21203 args that were used for partial ordering (the ones between ARGS and END)
21204 and seeing if it bubbles up. */
21206 static bool
21207 check_undeduced_parms (tree targs, tree args, tree end)
21209 bool found = false;
21210 int i;
21211 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
21212 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
21214 found = true;
21215 TREE_VEC_ELT (targs, i) = error_mark_node;
21217 if (found)
21219 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
21220 if (substed == error_mark_node)
21221 return true;
21223 return false;
21226 /* Given two function templates PAT1 and PAT2, return:
21228 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
21229 -1 if PAT2 is more specialized than PAT1.
21230 0 if neither is more specialized.
21232 LEN indicates the number of parameters we should consider
21233 (defaulted parameters should not be considered).
21235 The 1998 std underspecified function template partial ordering, and
21236 DR214 addresses the issue. We take pairs of arguments, one from
21237 each of the templates, and deduce them against each other. One of
21238 the templates will be more specialized if all the *other*
21239 template's arguments deduce against its arguments and at least one
21240 of its arguments *does* *not* deduce against the other template's
21241 corresponding argument. Deduction is done as for class templates.
21242 The arguments used in deduction have reference and top level cv
21243 qualifiers removed. Iff both arguments were originally reference
21244 types *and* deduction succeeds in both directions, an lvalue reference
21245 wins against an rvalue reference and otherwise the template
21246 with the more cv-qualified argument wins for that pairing (if
21247 neither is more cv-qualified, they both are equal). Unlike regular
21248 deduction, after all the arguments have been deduced in this way,
21249 we do *not* verify the deduced template argument values can be
21250 substituted into non-deduced contexts.
21252 The logic can be a bit confusing here, because we look at deduce1 and
21253 targs1 to see if pat2 is at least as specialized, and vice versa; if we
21254 can find template arguments for pat1 to make arg1 look like arg2, that
21255 means that arg2 is at least as specialized as arg1. */
21258 more_specialized_fn (tree pat1, tree pat2, int len)
21260 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
21261 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
21262 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
21263 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
21264 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
21265 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
21266 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
21267 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
21268 tree origs1, origs2;
21269 bool lose1 = false;
21270 bool lose2 = false;
21272 /* Remove the this parameter from non-static member functions. If
21273 one is a non-static member function and the other is not a static
21274 member function, remove the first parameter from that function
21275 also. This situation occurs for operator functions where we
21276 locate both a member function (with this pointer) and non-member
21277 operator (with explicit first operand). */
21278 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
21280 len--; /* LEN is the number of significant arguments for DECL1 */
21281 args1 = TREE_CHAIN (args1);
21282 if (!DECL_STATIC_FUNCTION_P (decl2))
21283 args2 = TREE_CHAIN (args2);
21285 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
21287 args2 = TREE_CHAIN (args2);
21288 if (!DECL_STATIC_FUNCTION_P (decl1))
21290 len--;
21291 args1 = TREE_CHAIN (args1);
21295 /* If only one is a conversion operator, they are unordered. */
21296 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
21297 return 0;
21299 /* Consider the return type for a conversion function */
21300 if (DECL_CONV_FN_P (decl1))
21302 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
21303 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
21304 len++;
21307 processing_template_decl++;
21309 origs1 = args1;
21310 origs2 = args2;
21312 while (len--
21313 /* Stop when an ellipsis is seen. */
21314 && args1 != NULL_TREE && args2 != NULL_TREE)
21316 tree arg1 = TREE_VALUE (args1);
21317 tree arg2 = TREE_VALUE (args2);
21318 int deduce1, deduce2;
21319 int quals1 = -1;
21320 int quals2 = -1;
21321 int ref1 = 0;
21322 int ref2 = 0;
21324 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21325 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21327 /* When both arguments are pack expansions, we need only
21328 unify the patterns themselves. */
21329 arg1 = PACK_EXPANSION_PATTERN (arg1);
21330 arg2 = PACK_EXPANSION_PATTERN (arg2);
21332 /* This is the last comparison we need to do. */
21333 len = 0;
21336 /* DR 1847: If a particular P contains no template-parameters that
21337 participate in template argument deduction, that P is not used to
21338 determine the ordering. */
21339 if (!uses_deducible_template_parms (arg1)
21340 && !uses_deducible_template_parms (arg2))
21341 goto next;
21343 if (TREE_CODE (arg1) == REFERENCE_TYPE)
21345 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
21346 arg1 = TREE_TYPE (arg1);
21347 quals1 = cp_type_quals (arg1);
21350 if (TREE_CODE (arg2) == REFERENCE_TYPE)
21352 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
21353 arg2 = TREE_TYPE (arg2);
21354 quals2 = cp_type_quals (arg2);
21357 arg1 = TYPE_MAIN_VARIANT (arg1);
21358 arg2 = TYPE_MAIN_VARIANT (arg2);
21360 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
21362 int i, len2 = remaining_arguments (args2);
21363 tree parmvec = make_tree_vec (1);
21364 tree argvec = make_tree_vec (len2);
21365 tree ta = args2;
21367 /* Setup the parameter vector, which contains only ARG1. */
21368 TREE_VEC_ELT (parmvec, 0) = arg1;
21370 /* Setup the argument vector, which contains the remaining
21371 arguments. */
21372 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
21373 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21375 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
21376 argvec, DEDUCE_EXACT,
21377 /*subr=*/true, /*explain_p=*/false)
21378 == 0);
21380 /* We cannot deduce in the other direction, because ARG1 is
21381 a pack expansion but ARG2 is not. */
21382 deduce2 = 0;
21384 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21386 int i, len1 = remaining_arguments (args1);
21387 tree parmvec = make_tree_vec (1);
21388 tree argvec = make_tree_vec (len1);
21389 tree ta = args1;
21391 /* Setup the parameter vector, which contains only ARG1. */
21392 TREE_VEC_ELT (parmvec, 0) = arg2;
21394 /* Setup the argument vector, which contains the remaining
21395 arguments. */
21396 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
21397 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21399 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
21400 argvec, DEDUCE_EXACT,
21401 /*subr=*/true, /*explain_p=*/false)
21402 == 0);
21404 /* We cannot deduce in the other direction, because ARG2 is
21405 a pack expansion but ARG1 is not.*/
21406 deduce1 = 0;
21409 else
21411 /* The normal case, where neither argument is a pack
21412 expansion. */
21413 deduce1 = (unify (tparms1, targs1, arg1, arg2,
21414 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21415 == 0);
21416 deduce2 = (unify (tparms2, targs2, arg2, arg1,
21417 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21418 == 0);
21421 /* If we couldn't deduce arguments for tparms1 to make arg1 match
21422 arg2, then arg2 is not as specialized as arg1. */
21423 if (!deduce1)
21424 lose2 = true;
21425 if (!deduce2)
21426 lose1 = true;
21428 /* "If, for a given type, deduction succeeds in both directions
21429 (i.e., the types are identical after the transformations above)
21430 and both P and A were reference types (before being replaced with
21431 the type referred to above):
21432 - if the type from the argument template was an lvalue reference and
21433 the type from the parameter template was not, the argument type is
21434 considered to be more specialized than the other; otherwise,
21435 - if the type from the argument template is more cv-qualified
21436 than the type from the parameter template (as described above),
21437 the argument type is considered to be more specialized than the other;
21438 otherwise,
21439 - neither type is more specialized than the other." */
21441 if (deduce1 && deduce2)
21443 if (ref1 && ref2 && ref1 != ref2)
21445 if (ref1 > ref2)
21446 lose1 = true;
21447 else
21448 lose2 = true;
21450 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
21452 if ((quals1 & quals2) == quals2)
21453 lose2 = true;
21454 if ((quals1 & quals2) == quals1)
21455 lose1 = true;
21459 if (lose1 && lose2)
21460 /* We've failed to deduce something in either direction.
21461 These must be unordered. */
21462 break;
21464 next:
21466 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21467 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21468 /* We have already processed all of the arguments in our
21469 handing of the pack expansion type. */
21470 len = 0;
21472 args1 = TREE_CHAIN (args1);
21473 args2 = TREE_CHAIN (args2);
21476 /* "In most cases, all template parameters must have values in order for
21477 deduction to succeed, but for partial ordering purposes a template
21478 parameter may remain without a value provided it is not used in the
21479 types being used for partial ordering."
21481 Thus, if we are missing any of the targs1 we need to substitute into
21482 origs1, then pat2 is not as specialized as pat1. This can happen when
21483 there is a nondeduced context. */
21484 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
21485 lose2 = true;
21486 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
21487 lose1 = true;
21489 processing_template_decl--;
21491 /* If both deductions succeed, the partial ordering selects the more
21492 constrained template. */
21493 if (!lose1 && !lose2)
21495 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
21496 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
21497 lose1 = !subsumes_constraints (c1, c2);
21498 lose2 = !subsumes_constraints (c2, c1);
21501 /* All things being equal, if the next argument is a pack expansion
21502 for one function but not for the other, prefer the
21503 non-variadic function. FIXME this is bogus; see c++/41958. */
21504 if (lose1 == lose2
21505 && args1 && TREE_VALUE (args1)
21506 && args2 && TREE_VALUE (args2))
21508 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
21509 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
21512 if (lose1 == lose2)
21513 return 0;
21514 else if (!lose1)
21515 return 1;
21516 else
21517 return -1;
21520 /* Determine which of two partial specializations of TMPL is more
21521 specialized.
21523 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
21524 to the first partial specialization. The TREE_PURPOSE is the
21525 innermost set of template parameters for the partial
21526 specialization. PAT2 is similar, but for the second template.
21528 Return 1 if the first partial specialization is more specialized;
21529 -1 if the second is more specialized; 0 if neither is more
21530 specialized.
21532 See [temp.class.order] for information about determining which of
21533 two templates is more specialized. */
21535 static int
21536 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
21538 tree targs;
21539 int winner = 0;
21540 bool any_deductions = false;
21542 tree tmpl1 = TREE_VALUE (pat1);
21543 tree tmpl2 = TREE_VALUE (pat2);
21544 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
21545 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
21547 /* Just like what happens for functions, if we are ordering between
21548 different template specializations, we may encounter dependent
21549 types in the arguments, and we need our dependency check functions
21550 to behave correctly. */
21551 ++processing_template_decl;
21552 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
21553 if (targs)
21555 --winner;
21556 any_deductions = true;
21559 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
21560 if (targs)
21562 ++winner;
21563 any_deductions = true;
21565 --processing_template_decl;
21567 /* If both deductions succeed, the partial ordering selects the more
21568 constrained template. */
21569 if (!winner && any_deductions)
21570 return more_constrained (tmpl1, tmpl2);
21572 /* In the case of a tie where at least one of the templates
21573 has a parameter pack at the end, the template with the most
21574 non-packed parameters wins. */
21575 if (winner == 0
21576 && any_deductions
21577 && (template_args_variadic_p (TREE_PURPOSE (pat1))
21578 || template_args_variadic_p (TREE_PURPOSE (pat2))))
21580 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
21581 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
21582 int len1 = TREE_VEC_LENGTH (args1);
21583 int len2 = TREE_VEC_LENGTH (args2);
21585 /* We don't count the pack expansion at the end. */
21586 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
21587 --len1;
21588 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
21589 --len2;
21591 if (len1 > len2)
21592 return 1;
21593 else if (len1 < len2)
21594 return -1;
21597 return winner;
21600 /* Return the template arguments that will produce the function signature
21601 DECL from the function template FN, with the explicit template
21602 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
21603 also match. Return NULL_TREE if no satisfactory arguments could be
21604 found. */
21606 static tree
21607 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
21609 int ntparms = DECL_NTPARMS (fn);
21610 tree targs = make_tree_vec (ntparms);
21611 tree decl_type = TREE_TYPE (decl);
21612 tree decl_arg_types;
21613 tree *args;
21614 unsigned int nargs, ix;
21615 tree arg;
21617 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
21619 /* Never do unification on the 'this' parameter. */
21620 decl_arg_types = skip_artificial_parms_for (decl,
21621 TYPE_ARG_TYPES (decl_type));
21623 nargs = list_length (decl_arg_types);
21624 args = XALLOCAVEC (tree, nargs);
21625 for (arg = decl_arg_types, ix = 0;
21626 arg != NULL_TREE && arg != void_list_node;
21627 arg = TREE_CHAIN (arg), ++ix)
21628 args[ix] = TREE_VALUE (arg);
21630 if (fn_type_unification (fn, explicit_args, targs,
21631 args, ix,
21632 (check_rettype || DECL_CONV_FN_P (fn)
21633 ? TREE_TYPE (decl_type) : NULL_TREE),
21634 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
21635 /*decltype*/false)
21636 == error_mark_node)
21637 return NULL_TREE;
21639 return targs;
21642 /* Return the innermost template arguments that, when applied to a partial
21643 specialization SPEC_TMPL of TMPL, yield the ARGS.
21645 For example, suppose we have:
21647 template <class T, class U> struct S {};
21648 template <class T> struct S<T*, int> {};
21650 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
21651 partial specialization and the ARGS will be {double*, int}. The resulting
21652 vector will be {double}, indicating that `T' is bound to `double'. */
21654 static tree
21655 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
21657 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
21658 tree spec_args
21659 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
21660 int i, ntparms = TREE_VEC_LENGTH (tparms);
21661 tree deduced_args;
21662 tree innermost_deduced_args;
21664 innermost_deduced_args = make_tree_vec (ntparms);
21665 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
21667 deduced_args = copy_node (args);
21668 SET_TMPL_ARGS_LEVEL (deduced_args,
21669 TMPL_ARGS_DEPTH (deduced_args),
21670 innermost_deduced_args);
21672 else
21673 deduced_args = innermost_deduced_args;
21675 bool tried_array_deduction = (cxx_dialect < cxx1z);
21676 again:
21677 if (unify (tparms, deduced_args,
21678 INNERMOST_TEMPLATE_ARGS (spec_args),
21679 INNERMOST_TEMPLATE_ARGS (args),
21680 UNIFY_ALLOW_NONE, /*explain_p=*/false))
21681 return NULL_TREE;
21683 for (i = 0; i < ntparms; ++i)
21684 if (! TREE_VEC_ELT (innermost_deduced_args, i))
21686 if (!tried_array_deduction)
21688 try_array_deduction (tparms, innermost_deduced_args,
21689 INNERMOST_TEMPLATE_ARGS (spec_args));
21690 tried_array_deduction = true;
21691 if (TREE_VEC_ELT (innermost_deduced_args, i))
21692 goto again;
21694 return NULL_TREE;
21697 tree tinst = build_tree_list (spec_tmpl, deduced_args);
21698 if (!push_tinst_level (tinst))
21700 excessive_deduction_depth = true;
21701 return NULL_TREE;
21704 /* Verify that nondeduced template arguments agree with the type
21705 obtained from argument deduction.
21707 For example:
21709 struct A { typedef int X; };
21710 template <class T, class U> struct C {};
21711 template <class T> struct C<T, typename T::X> {};
21713 Then with the instantiation `C<A, int>', we can deduce that
21714 `T' is `A' but unify () does not check whether `typename T::X'
21715 is `int'. */
21716 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
21718 if (spec_args != error_mark_node)
21719 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
21720 INNERMOST_TEMPLATE_ARGS (spec_args),
21721 tmpl, tf_none, false, false);
21723 pop_tinst_level ();
21725 if (spec_args == error_mark_node
21726 /* We only need to check the innermost arguments; the other
21727 arguments will always agree. */
21728 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
21729 INNERMOST_TEMPLATE_ARGS (args)))
21730 return NULL_TREE;
21732 /* Now that we have bindings for all of the template arguments,
21733 ensure that the arguments deduced for the template template
21734 parameters have compatible template parameter lists. See the use
21735 of template_template_parm_bindings_ok_p in fn_type_unification
21736 for more information. */
21737 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
21738 return NULL_TREE;
21740 return deduced_args;
21743 // Compare two function templates T1 and T2 by deducing bindings
21744 // from one against the other. If both deductions succeed, compare
21745 // constraints to see which is more constrained.
21746 static int
21747 more_specialized_inst (tree t1, tree t2)
21749 int fate = 0;
21750 int count = 0;
21752 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
21754 --fate;
21755 ++count;
21758 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
21760 ++fate;
21761 ++count;
21764 // If both deductions succeed, then one may be more constrained.
21765 if (count == 2 && fate == 0)
21766 fate = more_constrained (t1, t2);
21768 return fate;
21771 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
21772 Return the TREE_LIST node with the most specialized template, if
21773 any. If there is no most specialized template, the error_mark_node
21774 is returned.
21776 Note that this function does not look at, or modify, the
21777 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
21778 returned is one of the elements of INSTANTIATIONS, callers may
21779 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
21780 and retrieve it from the value returned. */
21782 tree
21783 most_specialized_instantiation (tree templates)
21785 tree fn, champ;
21787 ++processing_template_decl;
21789 champ = templates;
21790 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
21792 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
21793 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
21794 if (fate == -1)
21795 champ = fn;
21796 else if (!fate)
21798 /* Equally specialized, move to next function. If there
21799 is no next function, nothing's most specialized. */
21800 fn = TREE_CHAIN (fn);
21801 champ = fn;
21802 if (!fn)
21803 break;
21807 if (champ)
21808 /* Now verify that champ is better than everything earlier in the
21809 instantiation list. */
21810 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
21811 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
21813 champ = NULL_TREE;
21814 break;
21818 processing_template_decl--;
21820 if (!champ)
21821 return error_mark_node;
21823 return champ;
21826 /* If DECL is a specialization of some template, return the most
21827 general such template. Otherwise, returns NULL_TREE.
21829 For example, given:
21831 template <class T> struct S { template <class U> void f(U); };
21833 if TMPL is `template <class U> void S<int>::f(U)' this will return
21834 the full template. This function will not trace past partial
21835 specializations, however. For example, given in addition:
21837 template <class T> struct S<T*> { template <class U> void f(U); };
21839 if TMPL is `template <class U> void S<int*>::f(U)' this will return
21840 `template <class T> template <class U> S<T*>::f(U)'. */
21842 tree
21843 most_general_template (tree decl)
21845 if (TREE_CODE (decl) != TEMPLATE_DECL)
21847 if (tree tinfo = get_template_info (decl))
21848 decl = TI_TEMPLATE (tinfo);
21849 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
21850 template friend, or a FIELD_DECL for a capture pack. */
21851 if (TREE_CODE (decl) != TEMPLATE_DECL)
21852 return NULL_TREE;
21855 /* Look for more and more general templates. */
21856 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
21858 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
21859 (See cp-tree.h for details.) */
21860 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
21861 break;
21863 if (CLASS_TYPE_P (TREE_TYPE (decl))
21864 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
21865 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
21866 break;
21868 /* Stop if we run into an explicitly specialized class template. */
21869 if (!DECL_NAMESPACE_SCOPE_P (decl)
21870 && DECL_CONTEXT (decl)
21871 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
21872 break;
21874 decl = DECL_TI_TEMPLATE (decl);
21877 return decl;
21880 /* Return the most specialized of the template partial specializations
21881 which can produce TARGET, a specialization of some class or variable
21882 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
21883 a TEMPLATE_DECL node corresponding to the partial specialization, while
21884 the TREE_PURPOSE is the set of template arguments that must be
21885 substituted into the template pattern in order to generate TARGET.
21887 If the choice of partial specialization is ambiguous, a diagnostic
21888 is issued, and the error_mark_node is returned. If there are no
21889 partial specializations matching TARGET, then NULL_TREE is
21890 returned, indicating that the primary template should be used. */
21892 static tree
21893 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
21895 tree list = NULL_TREE;
21896 tree t;
21897 tree champ;
21898 int fate;
21899 bool ambiguous_p;
21900 tree outer_args = NULL_TREE;
21901 tree tmpl, args;
21903 if (TYPE_P (target))
21905 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
21906 tmpl = TI_TEMPLATE (tinfo);
21907 args = TI_ARGS (tinfo);
21909 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
21911 tmpl = TREE_OPERAND (target, 0);
21912 args = TREE_OPERAND (target, 1);
21914 else if (VAR_P (target))
21916 tree tinfo = DECL_TEMPLATE_INFO (target);
21917 tmpl = TI_TEMPLATE (tinfo);
21918 args = TI_ARGS (tinfo);
21920 else
21921 gcc_unreachable ();
21923 tree main_tmpl = most_general_template (tmpl);
21925 /* For determining which partial specialization to use, only the
21926 innermost args are interesting. */
21927 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
21929 outer_args = strip_innermost_template_args (args, 1);
21930 args = INNERMOST_TEMPLATE_ARGS (args);
21933 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
21935 tree spec_args;
21936 tree spec_tmpl = TREE_VALUE (t);
21938 if (outer_args)
21940 /* Substitute in the template args from the enclosing class. */
21941 ++processing_template_decl;
21942 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
21943 --processing_template_decl;
21946 if (spec_tmpl == error_mark_node)
21947 return error_mark_node;
21949 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
21950 if (spec_args)
21952 if (outer_args)
21953 spec_args = add_to_template_args (outer_args, spec_args);
21955 /* Keep the candidate only if the constraints are satisfied,
21956 or if we're not compiling with concepts. */
21957 if (!flag_concepts
21958 || constraints_satisfied_p (spec_tmpl, spec_args))
21960 list = tree_cons (spec_args, TREE_VALUE (t), list);
21961 TREE_TYPE (list) = TREE_TYPE (t);
21966 if (! list)
21967 return NULL_TREE;
21969 ambiguous_p = false;
21970 t = list;
21971 champ = t;
21972 t = TREE_CHAIN (t);
21973 for (; t; t = TREE_CHAIN (t))
21975 fate = more_specialized_partial_spec (tmpl, champ, t);
21976 if (fate == 1)
21978 else
21980 if (fate == 0)
21982 t = TREE_CHAIN (t);
21983 if (! t)
21985 ambiguous_p = true;
21986 break;
21989 champ = t;
21993 if (!ambiguous_p)
21994 for (t = list; t && t != champ; t = TREE_CHAIN (t))
21996 fate = more_specialized_partial_spec (tmpl, champ, t);
21997 if (fate != 1)
21999 ambiguous_p = true;
22000 break;
22004 if (ambiguous_p)
22006 const char *str;
22007 char *spaces = NULL;
22008 if (!(complain & tf_error))
22009 return error_mark_node;
22010 if (TYPE_P (target))
22011 error ("ambiguous template instantiation for %q#T", target);
22012 else
22013 error ("ambiguous template instantiation for %q#D", target);
22014 str = ngettext ("candidate is:", "candidates are:", list_length (list));
22015 for (t = list; t; t = TREE_CHAIN (t))
22017 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
22018 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
22019 "%s %#qS", spaces ? spaces : str, subst);
22020 spaces = spaces ? spaces : get_spaces (str);
22022 free (spaces);
22023 return error_mark_node;
22026 return champ;
22029 /* Explicitly instantiate DECL. */
22031 void
22032 do_decl_instantiation (tree decl, tree storage)
22034 tree result = NULL_TREE;
22035 int extern_p = 0;
22037 if (!decl || decl == error_mark_node)
22038 /* An error occurred, for which grokdeclarator has already issued
22039 an appropriate message. */
22040 return;
22041 else if (! DECL_LANG_SPECIFIC (decl))
22043 error ("explicit instantiation of non-template %q#D", decl);
22044 return;
22047 bool var_templ = (DECL_TEMPLATE_INFO (decl)
22048 && variable_template_p (DECL_TI_TEMPLATE (decl)));
22050 if (VAR_P (decl) && !var_templ)
22052 /* There is an asymmetry here in the way VAR_DECLs and
22053 FUNCTION_DECLs are handled by grokdeclarator. In the case of
22054 the latter, the DECL we get back will be marked as a
22055 template instantiation, and the appropriate
22056 DECL_TEMPLATE_INFO will be set up. This does not happen for
22057 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
22058 should handle VAR_DECLs as it currently handles
22059 FUNCTION_DECLs. */
22060 if (!DECL_CLASS_SCOPE_P (decl))
22062 error ("%qD is not a static data member of a class template", decl);
22063 return;
22065 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
22066 if (!result || !VAR_P (result))
22068 error ("no matching template for %qD found", decl);
22069 return;
22071 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
22073 error ("type %qT for explicit instantiation %qD does not match "
22074 "declared type %qT", TREE_TYPE (result), decl,
22075 TREE_TYPE (decl));
22076 return;
22079 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
22081 error ("explicit instantiation of %q#D", decl);
22082 return;
22084 else
22085 result = decl;
22087 /* Check for various error cases. Note that if the explicit
22088 instantiation is valid the RESULT will currently be marked as an
22089 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
22090 until we get here. */
22092 if (DECL_TEMPLATE_SPECIALIZATION (result))
22094 /* DR 259 [temp.spec].
22096 Both an explicit instantiation and a declaration of an explicit
22097 specialization shall not appear in a program unless the explicit
22098 instantiation follows a declaration of the explicit specialization.
22100 For a given set of template parameters, if an explicit
22101 instantiation of a template appears after a declaration of an
22102 explicit specialization for that template, the explicit
22103 instantiation has no effect. */
22104 return;
22106 else if (DECL_EXPLICIT_INSTANTIATION (result))
22108 /* [temp.spec]
22110 No program shall explicitly instantiate any template more
22111 than once.
22113 We check DECL_NOT_REALLY_EXTERN so as not to complain when
22114 the first instantiation was `extern' and the second is not,
22115 and EXTERN_P for the opposite case. */
22116 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
22117 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
22118 /* If an "extern" explicit instantiation follows an ordinary
22119 explicit instantiation, the template is instantiated. */
22120 if (extern_p)
22121 return;
22123 else if (!DECL_IMPLICIT_INSTANTIATION (result))
22125 error ("no matching template for %qD found", result);
22126 return;
22128 else if (!DECL_TEMPLATE_INFO (result))
22130 permerror (input_location, "explicit instantiation of non-template %q#D", result);
22131 return;
22134 if (storage == NULL_TREE)
22136 else if (storage == ridpointers[(int) RID_EXTERN])
22138 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
22139 pedwarn (input_location, OPT_Wpedantic,
22140 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
22141 "instantiations");
22142 extern_p = 1;
22144 else
22145 error ("storage class %qD applied to template instantiation", storage);
22147 check_explicit_instantiation_namespace (result);
22148 mark_decl_instantiated (result, extern_p);
22149 if (! extern_p)
22150 instantiate_decl (result, /*defer_ok=*/true,
22151 /*expl_inst_class_mem_p=*/false);
22154 static void
22155 mark_class_instantiated (tree t, int extern_p)
22157 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
22158 SET_CLASSTYPE_INTERFACE_KNOWN (t);
22159 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
22160 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
22161 if (! extern_p)
22163 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
22164 rest_of_type_compilation (t, 1);
22168 /* Called from do_type_instantiation through binding_table_foreach to
22169 do recursive instantiation for the type bound in ENTRY. */
22170 static void
22171 bt_instantiate_type_proc (binding_entry entry, void *data)
22173 tree storage = *(tree *) data;
22175 if (MAYBE_CLASS_TYPE_P (entry->type)
22176 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
22177 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
22180 /* Called from do_type_instantiation to instantiate a member
22181 (a member function or a static member variable) of an
22182 explicitly instantiated class template. */
22183 static void
22184 instantiate_class_member (tree decl, int extern_p)
22186 mark_decl_instantiated (decl, extern_p);
22187 if (! extern_p)
22188 instantiate_decl (decl, /*defer_ok=*/true,
22189 /*expl_inst_class_mem_p=*/true);
22192 /* Perform an explicit instantiation of template class T. STORAGE, if
22193 non-null, is the RID for extern, inline or static. COMPLAIN is
22194 nonzero if this is called from the parser, zero if called recursively,
22195 since the standard is unclear (as detailed below). */
22197 void
22198 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
22200 int extern_p = 0;
22201 int nomem_p = 0;
22202 int static_p = 0;
22203 int previous_instantiation_extern_p = 0;
22205 if (TREE_CODE (t) == TYPE_DECL)
22206 t = TREE_TYPE (t);
22208 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
22210 tree tmpl =
22211 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
22212 if (tmpl)
22213 error ("explicit instantiation of non-class template %qD", tmpl);
22214 else
22215 error ("explicit instantiation of non-template type %qT", t);
22216 return;
22219 complete_type (t);
22221 if (!COMPLETE_TYPE_P (t))
22223 if (complain & tf_error)
22224 error ("explicit instantiation of %q#T before definition of template",
22226 return;
22229 if (storage != NULL_TREE)
22231 if (!in_system_header_at (input_location))
22233 if (storage == ridpointers[(int) RID_EXTERN])
22235 if (cxx_dialect == cxx98)
22236 pedwarn (input_location, OPT_Wpedantic,
22237 "ISO C++ 1998 forbids the use of %<extern%> on "
22238 "explicit instantiations");
22240 else
22241 pedwarn (input_location, OPT_Wpedantic,
22242 "ISO C++ forbids the use of %qE"
22243 " on explicit instantiations", storage);
22246 if (storage == ridpointers[(int) RID_INLINE])
22247 nomem_p = 1;
22248 else if (storage == ridpointers[(int) RID_EXTERN])
22249 extern_p = 1;
22250 else if (storage == ridpointers[(int) RID_STATIC])
22251 static_p = 1;
22252 else
22254 error ("storage class %qD applied to template instantiation",
22255 storage);
22256 extern_p = 0;
22260 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
22262 /* DR 259 [temp.spec].
22264 Both an explicit instantiation and a declaration of an explicit
22265 specialization shall not appear in a program unless the explicit
22266 instantiation follows a declaration of the explicit specialization.
22268 For a given set of template parameters, if an explicit
22269 instantiation of a template appears after a declaration of an
22270 explicit specialization for that template, the explicit
22271 instantiation has no effect. */
22272 return;
22274 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
22276 /* [temp.spec]
22278 No program shall explicitly instantiate any template more
22279 than once.
22281 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
22282 instantiation was `extern'. If EXTERN_P then the second is.
22283 These cases are OK. */
22284 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
22286 if (!previous_instantiation_extern_p && !extern_p
22287 && (complain & tf_error))
22288 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
22290 /* If we've already instantiated the template, just return now. */
22291 if (!CLASSTYPE_INTERFACE_ONLY (t))
22292 return;
22295 check_explicit_instantiation_namespace (TYPE_NAME (t));
22296 mark_class_instantiated (t, extern_p);
22298 if (nomem_p)
22299 return;
22302 tree tmp;
22304 /* In contrast to implicit instantiation, where only the
22305 declarations, and not the definitions, of members are
22306 instantiated, we have here:
22308 [temp.explicit]
22310 The explicit instantiation of a class template specialization
22311 implies the instantiation of all of its members not
22312 previously explicitly specialized in the translation unit
22313 containing the explicit instantiation.
22315 Of course, we can't instantiate member template classes, since
22316 we don't have any arguments for them. Note that the standard
22317 is unclear on whether the instantiation of the members are
22318 *explicit* instantiations or not. However, the most natural
22319 interpretation is that it should be an explicit instantiation. */
22321 if (! static_p)
22322 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
22323 if (TREE_CODE (tmp) == FUNCTION_DECL
22324 && DECL_TEMPLATE_INSTANTIATION (tmp)
22325 && user_provided_p (tmp))
22326 instantiate_class_member (tmp, extern_p);
22328 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
22329 if (VAR_P (tmp) && DECL_TEMPLATE_INSTANTIATION (tmp))
22330 instantiate_class_member (tmp, extern_p);
22332 if (CLASSTYPE_NESTED_UTDS (t))
22333 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
22334 bt_instantiate_type_proc, &storage);
22338 /* Given a function DECL, which is a specialization of TMPL, modify
22339 DECL to be a re-instantiation of TMPL with the same template
22340 arguments. TMPL should be the template into which tsubst'ing
22341 should occur for DECL, not the most general template.
22343 One reason for doing this is a scenario like this:
22345 template <class T>
22346 void f(const T&, int i);
22348 void g() { f(3, 7); }
22350 template <class T>
22351 void f(const T& t, const int i) { }
22353 Note that when the template is first instantiated, with
22354 instantiate_template, the resulting DECL will have no name for the
22355 first parameter, and the wrong type for the second. So, when we go
22356 to instantiate the DECL, we regenerate it. */
22358 static void
22359 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
22361 /* The arguments used to instantiate DECL, from the most general
22362 template. */
22363 tree code_pattern;
22365 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
22367 /* Make sure that we can see identifiers, and compute access
22368 correctly. */
22369 push_access_scope (decl);
22371 if (TREE_CODE (decl) == FUNCTION_DECL)
22373 tree decl_parm;
22374 tree pattern_parm;
22375 tree specs;
22376 int args_depth;
22377 int parms_depth;
22379 args_depth = TMPL_ARGS_DEPTH (args);
22380 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
22381 if (args_depth > parms_depth)
22382 args = get_innermost_template_args (args, parms_depth);
22384 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
22385 args, tf_error, NULL_TREE,
22386 /*defer_ok*/false);
22387 if (specs && specs != error_mark_node)
22388 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
22389 specs);
22391 /* Merge parameter declarations. */
22392 decl_parm = skip_artificial_parms_for (decl,
22393 DECL_ARGUMENTS (decl));
22394 pattern_parm
22395 = skip_artificial_parms_for (code_pattern,
22396 DECL_ARGUMENTS (code_pattern));
22397 while (decl_parm && !DECL_PACK_P (pattern_parm))
22399 tree parm_type;
22400 tree attributes;
22402 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22403 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
22404 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
22405 NULL_TREE);
22406 parm_type = type_decays_to (parm_type);
22407 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22408 TREE_TYPE (decl_parm) = parm_type;
22409 attributes = DECL_ATTRIBUTES (pattern_parm);
22410 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22412 DECL_ATTRIBUTES (decl_parm) = attributes;
22413 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22415 decl_parm = DECL_CHAIN (decl_parm);
22416 pattern_parm = DECL_CHAIN (pattern_parm);
22418 /* Merge any parameters that match with the function parameter
22419 pack. */
22420 if (pattern_parm && DECL_PACK_P (pattern_parm))
22422 int i, len;
22423 tree expanded_types;
22424 /* Expand the TYPE_PACK_EXPANSION that provides the types for
22425 the parameters in this function parameter pack. */
22426 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
22427 args, tf_error, NULL_TREE);
22428 len = TREE_VEC_LENGTH (expanded_types);
22429 for (i = 0; i < len; i++)
22431 tree parm_type;
22432 tree attributes;
22434 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22435 /* Rename the parameter to include the index. */
22436 DECL_NAME (decl_parm) =
22437 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
22438 parm_type = TREE_VEC_ELT (expanded_types, i);
22439 parm_type = type_decays_to (parm_type);
22440 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22441 TREE_TYPE (decl_parm) = parm_type;
22442 attributes = DECL_ATTRIBUTES (pattern_parm);
22443 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22445 DECL_ATTRIBUTES (decl_parm) = attributes;
22446 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22448 decl_parm = DECL_CHAIN (decl_parm);
22451 /* Merge additional specifiers from the CODE_PATTERN. */
22452 if (DECL_DECLARED_INLINE_P (code_pattern)
22453 && !DECL_DECLARED_INLINE_P (decl))
22454 DECL_DECLARED_INLINE_P (decl) = 1;
22456 else if (VAR_P (decl))
22458 DECL_INITIAL (decl) =
22459 tsubst_expr (DECL_INITIAL (code_pattern), args,
22460 tf_error, DECL_TI_TEMPLATE (decl),
22461 /*integral_constant_expression_p=*/false);
22462 if (VAR_HAD_UNKNOWN_BOUND (decl))
22463 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
22464 tf_error, DECL_TI_TEMPLATE (decl));
22466 else
22467 gcc_unreachable ();
22469 pop_access_scope (decl);
22472 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
22473 substituted to get DECL. */
22475 tree
22476 template_for_substitution (tree decl)
22478 tree tmpl = DECL_TI_TEMPLATE (decl);
22480 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
22481 for the instantiation. This is not always the most general
22482 template. Consider, for example:
22484 template <class T>
22485 struct S { template <class U> void f();
22486 template <> void f<int>(); };
22488 and an instantiation of S<double>::f<int>. We want TD to be the
22489 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
22490 while (/* An instantiation cannot have a definition, so we need a
22491 more general template. */
22492 DECL_TEMPLATE_INSTANTIATION (tmpl)
22493 /* We must also deal with friend templates. Given:
22495 template <class T> struct S {
22496 template <class U> friend void f() {};
22499 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
22500 so far as the language is concerned, but that's still
22501 where we get the pattern for the instantiation from. On
22502 other hand, if the definition comes outside the class, say:
22504 template <class T> struct S {
22505 template <class U> friend void f();
22507 template <class U> friend void f() {}
22509 we don't need to look any further. That's what the check for
22510 DECL_INITIAL is for. */
22511 || (TREE_CODE (decl) == FUNCTION_DECL
22512 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
22513 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
22515 /* The present template, TD, should not be a definition. If it
22516 were a definition, we should be using it! Note that we
22517 cannot restructure the loop to just keep going until we find
22518 a template with a definition, since that might go too far if
22519 a specialization was declared, but not defined. */
22521 /* Fetch the more general template. */
22522 tmpl = DECL_TI_TEMPLATE (tmpl);
22525 return tmpl;
22528 /* Returns true if we need to instantiate this template instance even if we
22529 know we aren't going to emit it. */
22531 bool
22532 always_instantiate_p (tree decl)
22534 /* We always instantiate inline functions so that we can inline them. An
22535 explicit instantiation declaration prohibits implicit instantiation of
22536 non-inline functions. With high levels of optimization, we would
22537 normally inline non-inline functions -- but we're not allowed to do
22538 that for "extern template" functions. Therefore, we check
22539 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
22540 return ((TREE_CODE (decl) == FUNCTION_DECL
22541 && (DECL_DECLARED_INLINE_P (decl)
22542 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
22543 /* And we need to instantiate static data members so that
22544 their initializers are available in integral constant
22545 expressions. */
22546 || (VAR_P (decl)
22547 && decl_maybe_constant_var_p (decl)));
22550 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
22551 instantiate it now, modifying TREE_TYPE (fn). */
22553 void
22554 maybe_instantiate_noexcept (tree fn)
22556 tree fntype, spec, noex, clone;
22558 /* Don't instantiate a noexcept-specification from template context. */
22559 if (processing_template_decl)
22560 return;
22562 if (DECL_CLONED_FUNCTION_P (fn))
22563 fn = DECL_CLONED_FUNCTION (fn);
22564 fntype = TREE_TYPE (fn);
22565 spec = TYPE_RAISES_EXCEPTIONS (fntype);
22567 if (!spec || !TREE_PURPOSE (spec))
22568 return;
22570 noex = TREE_PURPOSE (spec);
22572 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
22574 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
22575 spec = get_defaulted_eh_spec (fn);
22576 else if (push_tinst_level (fn))
22578 push_access_scope (fn);
22579 push_deferring_access_checks (dk_no_deferred);
22580 input_location = DECL_SOURCE_LOCATION (fn);
22581 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
22582 DEFERRED_NOEXCEPT_ARGS (noex),
22583 tf_warning_or_error, fn,
22584 /*function_p=*/false,
22585 /*integral_constant_expression_p=*/true);
22586 pop_deferring_access_checks ();
22587 pop_access_scope (fn);
22588 pop_tinst_level ();
22589 spec = build_noexcept_spec (noex, tf_warning_or_error);
22590 if (spec == error_mark_node)
22591 spec = noexcept_false_spec;
22593 else
22594 spec = noexcept_false_spec;
22596 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
22599 FOR_EACH_CLONE (clone, fn)
22601 if (TREE_TYPE (clone) == fntype)
22602 TREE_TYPE (clone) = TREE_TYPE (fn);
22603 else
22604 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
22608 /* Produce the definition of D, a _DECL generated from a template. If
22609 DEFER_OK is true, then we don't have to actually do the
22610 instantiation now; we just have to do it sometime. Normally it is
22611 an error if this is an explicit instantiation but D is undefined.
22612 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
22613 instantiated class template. */
22615 tree
22616 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
22618 tree tmpl = DECL_TI_TEMPLATE (d);
22619 tree gen_args;
22620 tree args;
22621 tree td;
22622 tree code_pattern;
22623 tree spec;
22624 tree gen_tmpl;
22625 bool pattern_defined;
22626 location_t saved_loc = input_location;
22627 int saved_unevaluated_operand = cp_unevaluated_operand;
22628 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
22629 bool external_p;
22630 bool deleted_p;
22632 /* This function should only be used to instantiate templates for
22633 functions and static member variables. */
22634 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
22636 /* A concept is never instantiated. */
22637 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
22639 /* Variables are never deferred; if instantiation is required, they
22640 are instantiated right away. That allows for better code in the
22641 case that an expression refers to the value of the variable --
22642 if the variable has a constant value the referring expression can
22643 take advantage of that fact. */
22644 if (VAR_P (d))
22645 defer_ok = false;
22647 /* Don't instantiate cloned functions. Instead, instantiate the
22648 functions they cloned. */
22649 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
22650 d = DECL_CLONED_FUNCTION (d);
22652 if (DECL_TEMPLATE_INSTANTIATED (d)
22653 || (TREE_CODE (d) == FUNCTION_DECL
22654 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
22655 || DECL_TEMPLATE_SPECIALIZATION (d))
22656 /* D has already been instantiated or explicitly specialized, so
22657 there's nothing for us to do here.
22659 It might seem reasonable to check whether or not D is an explicit
22660 instantiation, and, if so, stop here. But when an explicit
22661 instantiation is deferred until the end of the compilation,
22662 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
22663 the instantiation. */
22664 return d;
22666 /* Check to see whether we know that this template will be
22667 instantiated in some other file, as with "extern template"
22668 extension. */
22669 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
22671 /* In general, we do not instantiate such templates. */
22672 if (external_p && !always_instantiate_p (d))
22673 return d;
22675 gen_tmpl = most_general_template (tmpl);
22676 gen_args = DECL_TI_ARGS (d);
22678 if (tmpl != gen_tmpl)
22679 /* We should already have the extra args. */
22680 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
22681 == TMPL_ARGS_DEPTH (gen_args));
22682 /* And what's in the hash table should match D. */
22683 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
22684 || spec == NULL_TREE);
22686 /* This needs to happen before any tsubsting. */
22687 if (! push_tinst_level (d))
22688 return d;
22690 timevar_push (TV_TEMPLATE_INST);
22692 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
22693 for the instantiation. */
22694 td = template_for_substitution (d);
22695 args = gen_args;
22697 if (VAR_P (d))
22699 /* Look up an explicit specialization, if any. */
22700 tree tid = lookup_template_variable (gen_tmpl, gen_args);
22701 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
22702 if (elt && elt != error_mark_node)
22704 td = TREE_VALUE (elt);
22705 args = TREE_PURPOSE (elt);
22709 code_pattern = DECL_TEMPLATE_RESULT (td);
22711 /* We should never be trying to instantiate a member of a class
22712 template or partial specialization. */
22713 gcc_assert (d != code_pattern);
22715 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
22716 || DECL_TEMPLATE_SPECIALIZATION (td))
22717 /* In the case of a friend template whose definition is provided
22718 outside the class, we may have too many arguments. Drop the
22719 ones we don't need. The same is true for specializations. */
22720 args = get_innermost_template_args
22721 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
22723 if (TREE_CODE (d) == FUNCTION_DECL)
22725 deleted_p = DECL_DELETED_FN (code_pattern);
22726 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
22727 && DECL_INITIAL (code_pattern) != error_mark_node)
22728 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
22729 || deleted_p);
22731 else
22733 deleted_p = false;
22734 if (DECL_CLASS_SCOPE_P (code_pattern))
22735 pattern_defined = (! DECL_IN_AGGR_P (code_pattern)
22736 || DECL_INLINE_VAR_P (code_pattern));
22737 else
22738 pattern_defined = ! DECL_EXTERNAL (code_pattern);
22741 /* We may be in the middle of deferred access check. Disable it now. */
22742 push_deferring_access_checks (dk_no_deferred);
22744 /* Unless an explicit instantiation directive has already determined
22745 the linkage of D, remember that a definition is available for
22746 this entity. */
22747 if (pattern_defined
22748 && !DECL_INTERFACE_KNOWN (d)
22749 && !DECL_NOT_REALLY_EXTERN (d))
22750 mark_definable (d);
22752 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
22753 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
22754 input_location = DECL_SOURCE_LOCATION (d);
22756 /* If D is a member of an explicitly instantiated class template,
22757 and no definition is available, treat it like an implicit
22758 instantiation. */
22759 if (!pattern_defined && expl_inst_class_mem_p
22760 && DECL_EXPLICIT_INSTANTIATION (d))
22762 /* Leave linkage flags alone on instantiations with anonymous
22763 visibility. */
22764 if (TREE_PUBLIC (d))
22766 DECL_NOT_REALLY_EXTERN (d) = 0;
22767 DECL_INTERFACE_KNOWN (d) = 0;
22769 SET_DECL_IMPLICIT_INSTANTIATION (d);
22772 /* Defer all other templates, unless we have been explicitly
22773 forbidden from doing so. */
22774 if (/* If there is no definition, we cannot instantiate the
22775 template. */
22776 ! pattern_defined
22777 /* If it's OK to postpone instantiation, do so. */
22778 || defer_ok
22779 /* If this is a static data member that will be defined
22780 elsewhere, we don't want to instantiate the entire data
22781 member, but we do want to instantiate the initializer so that
22782 we can substitute that elsewhere. */
22783 || (external_p && VAR_P (d))
22784 /* Handle here a deleted function too, avoid generating
22785 its body (c++/61080). */
22786 || deleted_p)
22788 /* The definition of the static data member is now required so
22789 we must substitute the initializer. */
22790 if (VAR_P (d)
22791 && !DECL_INITIAL (d)
22792 && DECL_INITIAL (code_pattern))
22794 tree ns;
22795 tree init;
22796 bool const_init = false;
22797 bool enter_context = DECL_CLASS_SCOPE_P (d);
22799 ns = decl_namespace_context (d);
22800 push_nested_namespace (ns);
22801 if (enter_context)
22802 push_nested_class (DECL_CONTEXT (d));
22803 init = tsubst_expr (DECL_INITIAL (code_pattern),
22804 args,
22805 tf_warning_or_error, NULL_TREE,
22806 /*integral_constant_expression_p=*/false);
22807 /* If instantiating the initializer involved instantiating this
22808 again, don't call cp_finish_decl twice. */
22809 if (!DECL_INITIAL (d))
22811 /* Make sure the initializer is still constant, in case of
22812 circular dependency (template/instantiate6.C). */
22813 const_init
22814 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
22815 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
22816 /*asmspec_tree=*/NULL_TREE,
22817 LOOKUP_ONLYCONVERTING);
22819 if (enter_context)
22820 pop_nested_class ();
22821 pop_nested_namespace (ns);
22824 /* We restore the source position here because it's used by
22825 add_pending_template. */
22826 input_location = saved_loc;
22828 if (at_eof && !pattern_defined
22829 && DECL_EXPLICIT_INSTANTIATION (d)
22830 && DECL_NOT_REALLY_EXTERN (d))
22831 /* [temp.explicit]
22833 The definition of a non-exported function template, a
22834 non-exported member function template, or a non-exported
22835 member function or static data member of a class template
22836 shall be present in every translation unit in which it is
22837 explicitly instantiated. */
22838 permerror (input_location, "explicit instantiation of %qD "
22839 "but no definition available", d);
22841 /* If we're in unevaluated context, we just wanted to get the
22842 constant value; this isn't an odr use, so don't queue
22843 a full instantiation. */
22844 if (cp_unevaluated_operand != 0)
22845 goto out;
22846 /* ??? Historically, we have instantiated inline functions, even
22847 when marked as "extern template". */
22848 if (!(external_p && VAR_P (d)))
22849 add_pending_template (d);
22850 goto out;
22852 /* Tell the repository that D is available in this translation unit
22853 -- and see if it is supposed to be instantiated here. */
22854 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
22856 /* In a PCH file, despite the fact that the repository hasn't
22857 requested instantiation in the PCH it is still possible that
22858 an instantiation will be required in a file that includes the
22859 PCH. */
22860 if (pch_file)
22861 add_pending_template (d);
22862 /* Instantiate inline functions so that the inliner can do its
22863 job, even though we'll not be emitting a copy of this
22864 function. */
22865 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
22866 goto out;
22869 bool push_to_top, nested;
22870 tree fn_context;
22871 fn_context = decl_function_context (d);
22872 nested = current_function_decl != NULL_TREE;
22873 push_to_top = !(nested && fn_context == current_function_decl);
22875 vec<tree> omp_privatization_save;
22876 if (nested)
22877 save_omp_privatization_clauses (omp_privatization_save);
22879 if (push_to_top)
22880 push_to_top_level ();
22881 else
22883 push_function_context ();
22884 cp_unevaluated_operand = 0;
22885 c_inhibit_evaluation_warnings = 0;
22888 /* Mark D as instantiated so that recursive calls to
22889 instantiate_decl do not try to instantiate it again. */
22890 DECL_TEMPLATE_INSTANTIATED (d) = 1;
22892 /* Regenerate the declaration in case the template has been modified
22893 by a subsequent redeclaration. */
22894 regenerate_decl_from_template (d, td, args);
22896 /* We already set the file and line above. Reset them now in case
22897 they changed as a result of calling regenerate_decl_from_template. */
22898 input_location = DECL_SOURCE_LOCATION (d);
22900 if (VAR_P (d))
22902 tree init;
22903 bool const_init = false;
22905 /* Clear out DECL_RTL; whatever was there before may not be right
22906 since we've reset the type of the declaration. */
22907 SET_DECL_RTL (d, NULL);
22908 DECL_IN_AGGR_P (d) = 0;
22910 /* The initializer is placed in DECL_INITIAL by
22911 regenerate_decl_from_template so we don't need to
22912 push/pop_access_scope again here. Pull it out so that
22913 cp_finish_decl can process it. */
22914 init = DECL_INITIAL (d);
22915 DECL_INITIAL (d) = NULL_TREE;
22916 DECL_INITIALIZED_P (d) = 0;
22918 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
22919 initializer. That function will defer actual emission until
22920 we have a chance to determine linkage. */
22921 DECL_EXTERNAL (d) = 0;
22923 /* Enter the scope of D so that access-checking works correctly. */
22924 bool enter_context = DECL_CLASS_SCOPE_P (d);
22925 if (enter_context)
22926 push_nested_class (DECL_CONTEXT (d));
22928 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
22929 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
22931 if (enter_context)
22932 pop_nested_class ();
22934 if (variable_template_p (gen_tmpl))
22935 note_variable_template_instantiation (d);
22937 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
22938 synthesize_method (d);
22939 else if (TREE_CODE (d) == FUNCTION_DECL)
22941 hash_map<tree, tree> *saved_local_specializations;
22942 tree tmpl_parm;
22943 tree spec_parm;
22944 tree block = NULL_TREE;
22945 tree lambda_ctx = NULL_TREE;
22947 /* Save away the current list, in case we are instantiating one
22948 template from within the body of another. */
22949 saved_local_specializations = local_specializations;
22951 /* Set up the list of local specializations. */
22952 local_specializations = new hash_map<tree, tree>;
22954 /* Set up context. */
22955 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
22956 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
22957 block = push_stmt_list ();
22958 else
22960 if (push_to_top && LAMBDA_FUNCTION_P (d))
22962 /* When instantiating a lambda's templated function
22963 operator, we need to push the non-lambda class scope
22964 of the lambda itself so that the nested function
22965 stack is sufficiently correct to deal with this
22966 capture. */
22967 lambda_ctx = DECL_CONTEXT (d);
22969 lambda_ctx = decl_type_context (TYPE_NAME (lambda_ctx));
22970 while (lambda_ctx && LAMBDA_TYPE_P (lambda_ctx));
22971 if (lambda_ctx)
22972 push_nested_class (lambda_ctx);
22974 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
22977 /* Some typedefs referenced from within the template code need to be
22978 access checked at template instantiation time, i.e now. These
22979 types were added to the template at parsing time. Let's get those
22980 and perform the access checks then. */
22981 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
22982 args);
22984 /* Create substitution entries for the parameters. */
22985 tmpl_parm = DECL_ARGUMENTS (code_pattern);
22986 spec_parm = DECL_ARGUMENTS (d);
22987 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
22989 register_local_specialization (spec_parm, tmpl_parm);
22990 spec_parm = skip_artificial_parms_for (d, spec_parm);
22991 tmpl_parm = skip_artificial_parms_for (code_pattern, tmpl_parm);
22993 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
22995 if (!DECL_PACK_P (tmpl_parm))
22997 register_local_specialization (spec_parm, tmpl_parm);
22998 spec_parm = DECL_CHAIN (spec_parm);
23000 else
23002 /* Register the (value) argument pack as a specialization of
23003 TMPL_PARM, then move on. */
23004 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
23005 register_local_specialization (argpack, tmpl_parm);
23008 gcc_assert (!spec_parm);
23010 /* Substitute into the body of the function. */
23011 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23012 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
23013 tf_warning_or_error, tmpl);
23014 else
23016 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
23017 tf_warning_or_error, tmpl,
23018 /*integral_constant_expression_p=*/false);
23020 /* Set the current input_location to the end of the function
23021 so that finish_function knows where we are. */
23022 input_location
23023 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
23025 /* Remember if we saw an infinite loop in the template. */
23026 current_function_infinite_loop
23027 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
23030 /* We don't need the local specializations any more. */
23031 delete local_specializations;
23032 local_specializations = saved_local_specializations;
23034 /* Finish the function. */
23035 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23036 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23037 DECL_SAVED_TREE (d) = pop_stmt_list (block);
23038 else
23040 d = finish_function (0);
23041 expand_or_defer_fn (d);
23043 if (lambda_ctx)
23044 pop_nested_class ();
23046 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23047 cp_check_omp_declare_reduction (d);
23050 /* We're not deferring instantiation any more. */
23051 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
23053 if (push_to_top)
23054 pop_from_top_level ();
23055 else
23056 pop_function_context ();
23058 if (nested)
23059 restore_omp_privatization_clauses (omp_privatization_save);
23061 out:
23062 pop_deferring_access_checks ();
23063 timevar_pop (TV_TEMPLATE_INST);
23064 pop_tinst_level ();
23065 input_location = saved_loc;
23066 cp_unevaluated_operand = saved_unevaluated_operand;
23067 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23069 return d;
23072 /* Run through the list of templates that we wish we could
23073 instantiate, and instantiate any we can. RETRIES is the
23074 number of times we retry pending template instantiation. */
23076 void
23077 instantiate_pending_templates (int retries)
23079 int reconsider;
23080 location_t saved_loc = input_location;
23082 /* Instantiating templates may trigger vtable generation. This in turn
23083 may require further template instantiations. We place a limit here
23084 to avoid infinite loop. */
23085 if (pending_templates && retries >= max_tinst_depth)
23087 tree decl = pending_templates->tinst->decl;
23089 fatal_error (input_location,
23090 "template instantiation depth exceeds maximum of %d"
23091 " instantiating %q+D, possibly from virtual table generation"
23092 " (use -ftemplate-depth= to increase the maximum)",
23093 max_tinst_depth, decl);
23094 if (TREE_CODE (decl) == FUNCTION_DECL)
23095 /* Pretend that we defined it. */
23096 DECL_INITIAL (decl) = error_mark_node;
23097 return;
23102 struct pending_template **t = &pending_templates;
23103 struct pending_template *last = NULL;
23104 reconsider = 0;
23105 while (*t)
23107 tree instantiation = reopen_tinst_level ((*t)->tinst);
23108 bool complete = false;
23110 if (TYPE_P (instantiation))
23112 tree fn;
23114 if (!COMPLETE_TYPE_P (instantiation))
23116 instantiate_class_template (instantiation);
23117 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
23118 for (fn = TYPE_METHODS (instantiation);
23120 fn = TREE_CHAIN (fn))
23121 if (! DECL_ARTIFICIAL (fn))
23122 instantiate_decl (fn,
23123 /*defer_ok=*/false,
23124 /*expl_inst_class_mem_p=*/false);
23125 if (COMPLETE_TYPE_P (instantiation))
23126 reconsider = 1;
23129 complete = COMPLETE_TYPE_P (instantiation);
23131 else
23133 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
23134 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
23136 instantiation
23137 = instantiate_decl (instantiation,
23138 /*defer_ok=*/false,
23139 /*expl_inst_class_mem_p=*/false);
23140 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
23141 reconsider = 1;
23144 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
23145 || DECL_TEMPLATE_INSTANTIATED (instantiation));
23148 if (complete)
23149 /* If INSTANTIATION has been instantiated, then we don't
23150 need to consider it again in the future. */
23151 *t = (*t)->next;
23152 else
23154 last = *t;
23155 t = &(*t)->next;
23157 tinst_depth = 0;
23158 current_tinst_level = NULL;
23160 last_pending_template = last;
23162 while (reconsider);
23164 input_location = saved_loc;
23167 /* Substitute ARGVEC into T, which is a list of initializers for
23168 either base class or a non-static data member. The TREE_PURPOSEs
23169 are DECLs, and the TREE_VALUEs are the initializer values. Used by
23170 instantiate_decl. */
23172 static tree
23173 tsubst_initializer_list (tree t, tree argvec)
23175 tree inits = NULL_TREE;
23177 for (; t; t = TREE_CHAIN (t))
23179 tree decl;
23180 tree init;
23181 tree expanded_bases = NULL_TREE;
23182 tree expanded_arguments = NULL_TREE;
23183 int i, len = 1;
23185 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
23187 tree expr;
23188 tree arg;
23190 /* Expand the base class expansion type into separate base
23191 classes. */
23192 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
23193 tf_warning_or_error,
23194 NULL_TREE);
23195 if (expanded_bases == error_mark_node)
23196 continue;
23198 /* We'll be building separate TREE_LISTs of arguments for
23199 each base. */
23200 len = TREE_VEC_LENGTH (expanded_bases);
23201 expanded_arguments = make_tree_vec (len);
23202 for (i = 0; i < len; i++)
23203 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
23205 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
23206 expand each argument in the TREE_VALUE of t. */
23207 expr = make_node (EXPR_PACK_EXPANSION);
23208 PACK_EXPANSION_LOCAL_P (expr) = true;
23209 PACK_EXPANSION_PARAMETER_PACKS (expr) =
23210 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
23212 if (TREE_VALUE (t) == void_type_node)
23213 /* VOID_TYPE_NODE is used to indicate
23214 value-initialization. */
23216 for (i = 0; i < len; i++)
23217 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
23219 else
23221 /* Substitute parameter packs into each argument in the
23222 TREE_LIST. */
23223 in_base_initializer = 1;
23224 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
23226 tree expanded_exprs;
23228 /* Expand the argument. */
23229 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
23230 expanded_exprs
23231 = tsubst_pack_expansion (expr, argvec,
23232 tf_warning_or_error,
23233 NULL_TREE);
23234 if (expanded_exprs == error_mark_node)
23235 continue;
23237 /* Prepend each of the expanded expressions to the
23238 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
23239 for (i = 0; i < len; i++)
23241 TREE_VEC_ELT (expanded_arguments, i) =
23242 tree_cons (NULL_TREE,
23243 TREE_VEC_ELT (expanded_exprs, i),
23244 TREE_VEC_ELT (expanded_arguments, i));
23247 in_base_initializer = 0;
23249 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
23250 since we built them backwards. */
23251 for (i = 0; i < len; i++)
23253 TREE_VEC_ELT (expanded_arguments, i) =
23254 nreverse (TREE_VEC_ELT (expanded_arguments, i));
23259 for (i = 0; i < len; ++i)
23261 if (expanded_bases)
23263 decl = TREE_VEC_ELT (expanded_bases, i);
23264 decl = expand_member_init (decl);
23265 init = TREE_VEC_ELT (expanded_arguments, i);
23267 else
23269 tree tmp;
23270 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
23271 tf_warning_or_error, NULL_TREE);
23273 decl = expand_member_init (decl);
23274 if (decl && !DECL_P (decl))
23275 in_base_initializer = 1;
23277 init = TREE_VALUE (t);
23278 tmp = init;
23279 if (init != void_type_node)
23280 init = tsubst_expr (init, argvec,
23281 tf_warning_or_error, NULL_TREE,
23282 /*integral_constant_expression_p=*/false);
23283 if (init == NULL_TREE && tmp != NULL_TREE)
23284 /* If we had an initializer but it instantiated to nothing,
23285 value-initialize the object. This will only occur when
23286 the initializer was a pack expansion where the parameter
23287 packs used in that expansion were of length zero. */
23288 init = void_type_node;
23289 in_base_initializer = 0;
23292 if (decl)
23294 init = build_tree_list (decl, init);
23295 TREE_CHAIN (init) = inits;
23296 inits = init;
23300 return inits;
23303 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
23305 static void
23306 set_current_access_from_decl (tree decl)
23308 if (TREE_PRIVATE (decl))
23309 current_access_specifier = access_private_node;
23310 else if (TREE_PROTECTED (decl))
23311 current_access_specifier = access_protected_node;
23312 else
23313 current_access_specifier = access_public_node;
23316 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
23317 is the instantiation (which should have been created with
23318 start_enum) and ARGS are the template arguments to use. */
23320 static void
23321 tsubst_enum (tree tag, tree newtag, tree args)
23323 tree e;
23325 if (SCOPED_ENUM_P (newtag))
23326 begin_scope (sk_scoped_enum, newtag);
23328 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
23330 tree value;
23331 tree decl;
23333 decl = TREE_VALUE (e);
23334 /* Note that in a template enum, the TREE_VALUE is the
23335 CONST_DECL, not the corresponding INTEGER_CST. */
23336 value = tsubst_expr (DECL_INITIAL (decl),
23337 args, tf_warning_or_error, NULL_TREE,
23338 /*integral_constant_expression_p=*/true);
23340 /* Give this enumeration constant the correct access. */
23341 set_current_access_from_decl (decl);
23343 /* Actually build the enumerator itself. Here we're assuming that
23344 enumerators can't have dependent attributes. */
23345 build_enumerator (DECL_NAME (decl), value, newtag,
23346 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
23349 if (SCOPED_ENUM_P (newtag))
23350 finish_scope ();
23352 finish_enum_value_list (newtag);
23353 finish_enum (newtag);
23355 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
23356 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
23359 /* DECL is a FUNCTION_DECL that is a template specialization. Return
23360 its type -- but without substituting the innermost set of template
23361 arguments. So, innermost set of template parameters will appear in
23362 the type. */
23364 tree
23365 get_mostly_instantiated_function_type (tree decl)
23367 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
23368 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
23371 /* Return truthvalue if we're processing a template different from
23372 the last one involved in diagnostics. */
23373 bool
23374 problematic_instantiation_changed (void)
23376 return current_tinst_level != last_error_tinst_level;
23379 /* Remember current template involved in diagnostics. */
23380 void
23381 record_last_problematic_instantiation (void)
23383 last_error_tinst_level = current_tinst_level;
23386 struct tinst_level *
23387 current_instantiation (void)
23389 return current_tinst_level;
23392 /* Return TRUE if current_function_decl is being instantiated, false
23393 otherwise. */
23395 bool
23396 instantiating_current_function_p (void)
23398 return (current_instantiation ()
23399 && current_instantiation ()->decl == current_function_decl);
23402 /* [temp.param] Check that template non-type parm TYPE is of an allowable
23403 type. Return zero for ok, nonzero for disallowed. Issue error and
23404 warning messages under control of COMPLAIN. */
23406 static int
23407 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
23409 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
23410 return 0;
23411 else if (POINTER_TYPE_P (type))
23412 return 0;
23413 else if (TYPE_PTRMEM_P (type))
23414 return 0;
23415 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
23416 return 0;
23417 else if (TREE_CODE (type) == TYPENAME_TYPE)
23418 return 0;
23419 else if (TREE_CODE (type) == DECLTYPE_TYPE)
23420 return 0;
23421 else if (TREE_CODE (type) == NULLPTR_TYPE)
23422 return 0;
23423 /* A bound template template parm could later be instantiated to have a valid
23424 nontype parm type via an alias template. */
23425 else if (cxx_dialect >= cxx11
23426 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23427 return 0;
23429 if (complain & tf_error)
23431 if (type == error_mark_node)
23432 inform (input_location, "invalid template non-type parameter");
23433 else
23434 error ("%q#T is not a valid type for a template non-type parameter",
23435 type);
23437 return 1;
23440 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
23441 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
23443 static bool
23444 dependent_type_p_r (tree type)
23446 tree scope;
23448 /* [temp.dep.type]
23450 A type is dependent if it is:
23452 -- a template parameter. Template template parameters are types
23453 for us (since TYPE_P holds true for them) so we handle
23454 them here. */
23455 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
23456 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
23457 return true;
23458 /* -- a qualified-id with a nested-name-specifier which contains a
23459 class-name that names a dependent type or whose unqualified-id
23460 names a dependent type. */
23461 if (TREE_CODE (type) == TYPENAME_TYPE)
23462 return true;
23464 /* An alias template specialization can be dependent even if the
23465 resulting type is not. */
23466 if (dependent_alias_template_spec_p (type))
23467 return true;
23469 /* -- a cv-qualified type where the cv-unqualified type is
23470 dependent.
23471 No code is necessary for this bullet; the code below handles
23472 cv-qualified types, and we don't want to strip aliases with
23473 TYPE_MAIN_VARIANT because of DR 1558. */
23474 /* -- a compound type constructed from any dependent type. */
23475 if (TYPE_PTRMEM_P (type))
23476 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
23477 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
23478 (type)));
23479 else if (TYPE_PTR_P (type)
23480 || TREE_CODE (type) == REFERENCE_TYPE)
23481 return dependent_type_p (TREE_TYPE (type));
23482 else if (TREE_CODE (type) == FUNCTION_TYPE
23483 || TREE_CODE (type) == METHOD_TYPE)
23485 tree arg_type;
23487 if (dependent_type_p (TREE_TYPE (type)))
23488 return true;
23489 for (arg_type = TYPE_ARG_TYPES (type);
23490 arg_type;
23491 arg_type = TREE_CHAIN (arg_type))
23492 if (dependent_type_p (TREE_VALUE (arg_type)))
23493 return true;
23494 if (cxx_dialect >= cxx1z)
23496 /* A value-dependent noexcept-specifier makes the type dependent. */
23497 tree spec = TYPE_RAISES_EXCEPTIONS (type);
23498 if (spec && TREE_PURPOSE (spec)
23499 && value_dependent_expression_p (TREE_PURPOSE (spec)))
23500 return true;
23502 return false;
23504 /* -- an array type constructed from any dependent type or whose
23505 size is specified by a constant expression that is
23506 value-dependent.
23508 We checked for type- and value-dependence of the bounds in
23509 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
23510 if (TREE_CODE (type) == ARRAY_TYPE)
23512 if (TYPE_DOMAIN (type)
23513 && dependent_type_p (TYPE_DOMAIN (type)))
23514 return true;
23515 return dependent_type_p (TREE_TYPE (type));
23518 /* -- a template-id in which either the template name is a template
23519 parameter ... */
23520 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23521 return true;
23522 /* ... or any of the template arguments is a dependent type or
23523 an expression that is type-dependent or value-dependent. */
23524 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
23525 && (any_dependent_template_arguments_p
23526 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
23527 return true;
23529 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
23530 dependent; if the argument of the `typeof' expression is not
23531 type-dependent, then it should already been have resolved. */
23532 if (TREE_CODE (type) == TYPEOF_TYPE
23533 || TREE_CODE (type) == DECLTYPE_TYPE
23534 || TREE_CODE (type) == UNDERLYING_TYPE)
23535 return true;
23537 /* A template argument pack is dependent if any of its packed
23538 arguments are. */
23539 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
23541 tree args = ARGUMENT_PACK_ARGS (type);
23542 int i, len = TREE_VEC_LENGTH (args);
23543 for (i = 0; i < len; ++i)
23544 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
23545 return true;
23548 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
23549 be template parameters. */
23550 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
23551 return true;
23553 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
23554 return true;
23556 /* The standard does not specifically mention types that are local
23557 to template functions or local classes, but they should be
23558 considered dependent too. For example:
23560 template <int I> void f() {
23561 enum E { a = I };
23562 S<sizeof (E)> s;
23565 The size of `E' cannot be known until the value of `I' has been
23566 determined. Therefore, `E' must be considered dependent. */
23567 scope = TYPE_CONTEXT (type);
23568 if (scope && TYPE_P (scope))
23569 return dependent_type_p (scope);
23570 /* Don't use type_dependent_expression_p here, as it can lead
23571 to infinite recursion trying to determine whether a lambda
23572 nested in a lambda is dependent (c++/47687). */
23573 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
23574 && DECL_LANG_SPECIFIC (scope)
23575 && DECL_TEMPLATE_INFO (scope)
23576 && (any_dependent_template_arguments_p
23577 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
23578 return true;
23580 /* Other types are non-dependent. */
23581 return false;
23584 /* Returns TRUE if TYPE is dependent, in the sense of
23585 [temp.dep.type]. Note that a NULL type is considered dependent. */
23587 bool
23588 dependent_type_p (tree type)
23590 /* If there are no template parameters in scope, then there can't be
23591 any dependent types. */
23592 if (!processing_template_decl)
23594 /* If we are not processing a template, then nobody should be
23595 providing us with a dependent type. */
23596 gcc_assert (type);
23597 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
23598 return false;
23601 /* If the type is NULL, we have not computed a type for the entity
23602 in question; in that case, the type is dependent. */
23603 if (!type)
23604 return true;
23606 /* Erroneous types can be considered non-dependent. */
23607 if (type == error_mark_node)
23608 return false;
23610 /* Getting here with global_type_node means we improperly called this
23611 function on the TREE_TYPE of an IDENTIFIER_NODE. */
23612 gcc_checking_assert (type != global_type_node);
23614 /* If we have not already computed the appropriate value for TYPE,
23615 do so now. */
23616 if (!TYPE_DEPENDENT_P_VALID (type))
23618 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
23619 TYPE_DEPENDENT_P_VALID (type) = 1;
23622 return TYPE_DEPENDENT_P (type);
23625 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
23626 lookup. In other words, a dependent type that is not the current
23627 instantiation. */
23629 bool
23630 dependent_scope_p (tree scope)
23632 return (scope && TYPE_P (scope) && dependent_type_p (scope)
23633 && !currently_open_class (scope));
23636 /* T is a SCOPE_REF; return whether we need to consider it
23637 instantiation-dependent so that we can check access at instantiation
23638 time even though we know which member it resolves to. */
23640 static bool
23641 instantiation_dependent_scope_ref_p (tree t)
23643 if (DECL_P (TREE_OPERAND (t, 1))
23644 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
23645 && accessible_in_template_p (TREE_OPERAND (t, 0),
23646 TREE_OPERAND (t, 1)))
23647 return false;
23648 else
23649 return true;
23652 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
23653 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
23654 expression. */
23656 /* Note that this predicate is not appropriate for general expressions;
23657 only constant expressions (that satisfy potential_constant_expression)
23658 can be tested for value dependence. */
23660 bool
23661 value_dependent_expression_p (tree expression)
23663 if (!processing_template_decl || expression == NULL_TREE)
23664 return false;
23666 /* A name declared with a dependent type. */
23667 if (DECL_P (expression) && type_dependent_expression_p (expression))
23668 return true;
23670 switch (TREE_CODE (expression))
23672 case BASELINK:
23673 /* A dependent member function of the current instantiation. */
23674 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
23676 case FUNCTION_DECL:
23677 /* A dependent member function of the current instantiation. */
23678 if (DECL_CLASS_SCOPE_P (expression)
23679 && dependent_type_p (DECL_CONTEXT (expression)))
23680 return true;
23681 break;
23683 case IDENTIFIER_NODE:
23684 /* A name that has not been looked up -- must be dependent. */
23685 return true;
23687 case TEMPLATE_PARM_INDEX:
23688 /* A non-type template parm. */
23689 return true;
23691 case CONST_DECL:
23692 /* A non-type template parm. */
23693 if (DECL_TEMPLATE_PARM_P (expression))
23694 return true;
23695 return value_dependent_expression_p (DECL_INITIAL (expression));
23697 case VAR_DECL:
23698 /* A constant with literal type and is initialized
23699 with an expression that is value-dependent.
23701 Note that a non-dependent parenthesized initializer will have
23702 already been replaced with its constant value, so if we see
23703 a TREE_LIST it must be dependent. */
23704 if (DECL_INITIAL (expression)
23705 && decl_constant_var_p (expression)
23706 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
23707 /* cp_finish_decl doesn't fold reference initializers. */
23708 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE
23709 || type_dependent_expression_p (DECL_INITIAL (expression))
23710 || value_dependent_expression_p (DECL_INITIAL (expression))))
23711 return true;
23712 if (DECL_HAS_VALUE_EXPR_P (expression))
23714 tree value_expr = DECL_VALUE_EXPR (expression);
23715 if (type_dependent_expression_p (value_expr))
23716 return true;
23718 return false;
23720 case DYNAMIC_CAST_EXPR:
23721 case STATIC_CAST_EXPR:
23722 case CONST_CAST_EXPR:
23723 case REINTERPRET_CAST_EXPR:
23724 case CAST_EXPR:
23725 /* These expressions are value-dependent if the type to which
23726 the cast occurs is dependent or the expression being casted
23727 is value-dependent. */
23729 tree type = TREE_TYPE (expression);
23731 if (dependent_type_p (type))
23732 return true;
23734 /* A functional cast has a list of operands. */
23735 expression = TREE_OPERAND (expression, 0);
23736 if (!expression)
23738 /* If there are no operands, it must be an expression such
23739 as "int()". This should not happen for aggregate types
23740 because it would form non-constant expressions. */
23741 gcc_assert (cxx_dialect >= cxx11
23742 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
23744 return false;
23747 if (TREE_CODE (expression) == TREE_LIST)
23748 return any_value_dependent_elements_p (expression);
23750 return value_dependent_expression_p (expression);
23753 case SIZEOF_EXPR:
23754 if (SIZEOF_EXPR_TYPE_P (expression))
23755 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
23756 /* FALLTHRU */
23757 case ALIGNOF_EXPR:
23758 case TYPEID_EXPR:
23759 /* A `sizeof' expression is value-dependent if the operand is
23760 type-dependent or is a pack expansion. */
23761 expression = TREE_OPERAND (expression, 0);
23762 if (PACK_EXPANSION_P (expression))
23763 return true;
23764 else if (TYPE_P (expression))
23765 return dependent_type_p (expression);
23766 return instantiation_dependent_uneval_expression_p (expression);
23768 case AT_ENCODE_EXPR:
23769 /* An 'encode' expression is value-dependent if the operand is
23770 type-dependent. */
23771 expression = TREE_OPERAND (expression, 0);
23772 return dependent_type_p (expression);
23774 case NOEXCEPT_EXPR:
23775 expression = TREE_OPERAND (expression, 0);
23776 return instantiation_dependent_uneval_expression_p (expression);
23778 case SCOPE_REF:
23779 /* All instantiation-dependent expressions should also be considered
23780 value-dependent. */
23781 return instantiation_dependent_scope_ref_p (expression);
23783 case COMPONENT_REF:
23784 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
23785 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
23787 case NONTYPE_ARGUMENT_PACK:
23788 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
23789 is value-dependent. */
23791 tree values = ARGUMENT_PACK_ARGS (expression);
23792 int i, len = TREE_VEC_LENGTH (values);
23794 for (i = 0; i < len; ++i)
23795 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
23796 return true;
23798 return false;
23801 case TRAIT_EXPR:
23803 tree type2 = TRAIT_EXPR_TYPE2 (expression);
23804 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
23805 || (type2 ? dependent_type_p (type2) : false));
23808 case MODOP_EXPR:
23809 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
23810 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
23812 case ARRAY_REF:
23813 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
23814 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
23816 case ADDR_EXPR:
23818 tree op = TREE_OPERAND (expression, 0);
23819 return (value_dependent_expression_p (op)
23820 || has_value_dependent_address (op));
23823 case REQUIRES_EXPR:
23824 /* Treat all requires-expressions as value-dependent so
23825 we don't try to fold them. */
23826 return true;
23828 case TYPE_REQ:
23829 return dependent_type_p (TREE_OPERAND (expression, 0));
23831 case CALL_EXPR:
23833 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
23834 return true;
23835 tree fn = get_callee_fndecl (expression);
23836 int i, nargs;
23837 nargs = call_expr_nargs (expression);
23838 for (i = 0; i < nargs; ++i)
23840 tree op = CALL_EXPR_ARG (expression, i);
23841 /* In a call to a constexpr member function, look through the
23842 implicit ADDR_EXPR on the object argument so that it doesn't
23843 cause the call to be considered value-dependent. We also
23844 look through it in potential_constant_expression. */
23845 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
23846 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
23847 && TREE_CODE (op) == ADDR_EXPR)
23848 op = TREE_OPERAND (op, 0);
23849 if (value_dependent_expression_p (op))
23850 return true;
23852 return false;
23855 case TEMPLATE_ID_EXPR:
23856 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
23857 type-dependent. */
23858 return type_dependent_expression_p (expression)
23859 || variable_concept_p (TREE_OPERAND (expression, 0));
23861 case CONSTRUCTOR:
23863 unsigned ix;
23864 tree val;
23865 if (dependent_type_p (TREE_TYPE (expression)))
23866 return true;
23867 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
23868 if (value_dependent_expression_p (val))
23869 return true;
23870 return false;
23873 case STMT_EXPR:
23874 /* Treat a GNU statement expression as dependent to avoid crashing
23875 under instantiate_non_dependent_expr; it can't be constant. */
23876 return true;
23878 default:
23879 /* A constant expression is value-dependent if any subexpression is
23880 value-dependent. */
23881 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
23883 case tcc_reference:
23884 case tcc_unary:
23885 case tcc_comparison:
23886 case tcc_binary:
23887 case tcc_expression:
23888 case tcc_vl_exp:
23890 int i, len = cp_tree_operand_length (expression);
23892 for (i = 0; i < len; i++)
23894 tree t = TREE_OPERAND (expression, i);
23896 /* In some cases, some of the operands may be missing.
23897 (For example, in the case of PREDECREMENT_EXPR, the
23898 amount to increment by may be missing.) That doesn't
23899 make the expression dependent. */
23900 if (t && value_dependent_expression_p (t))
23901 return true;
23904 break;
23905 default:
23906 break;
23908 break;
23911 /* The expression is not value-dependent. */
23912 return false;
23915 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
23916 [temp.dep.expr]. Note that an expression with no type is
23917 considered dependent. Other parts of the compiler arrange for an
23918 expression with type-dependent subexpressions to have no type, so
23919 this function doesn't have to be fully recursive. */
23921 bool
23922 type_dependent_expression_p (tree expression)
23924 if (!processing_template_decl)
23925 return false;
23927 if (expression == NULL_TREE || expression == error_mark_node)
23928 return false;
23930 /* An unresolved name is always dependent. */
23931 if (identifier_p (expression)
23932 || TREE_CODE (expression) == USING_DECL
23933 || TREE_CODE (expression) == WILDCARD_DECL)
23934 return true;
23936 /* A fold expression is type-dependent. */
23937 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
23938 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
23939 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
23940 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
23941 return true;
23943 /* Some expression forms are never type-dependent. */
23944 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
23945 || TREE_CODE (expression) == SIZEOF_EXPR
23946 || TREE_CODE (expression) == ALIGNOF_EXPR
23947 || TREE_CODE (expression) == AT_ENCODE_EXPR
23948 || TREE_CODE (expression) == NOEXCEPT_EXPR
23949 || TREE_CODE (expression) == TRAIT_EXPR
23950 || TREE_CODE (expression) == TYPEID_EXPR
23951 || TREE_CODE (expression) == DELETE_EXPR
23952 || TREE_CODE (expression) == VEC_DELETE_EXPR
23953 || TREE_CODE (expression) == THROW_EXPR
23954 || TREE_CODE (expression) == REQUIRES_EXPR)
23955 return false;
23957 /* The types of these expressions depends only on the type to which
23958 the cast occurs. */
23959 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
23960 || TREE_CODE (expression) == STATIC_CAST_EXPR
23961 || TREE_CODE (expression) == CONST_CAST_EXPR
23962 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
23963 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
23964 || TREE_CODE (expression) == CAST_EXPR)
23965 return dependent_type_p (TREE_TYPE (expression));
23967 /* The types of these expressions depends only on the type created
23968 by the expression. */
23969 if (TREE_CODE (expression) == NEW_EXPR
23970 || TREE_CODE (expression) == VEC_NEW_EXPR)
23972 /* For NEW_EXPR tree nodes created inside a template, either
23973 the object type itself or a TREE_LIST may appear as the
23974 operand 1. */
23975 tree type = TREE_OPERAND (expression, 1);
23976 if (TREE_CODE (type) == TREE_LIST)
23977 /* This is an array type. We need to check array dimensions
23978 as well. */
23979 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
23980 || value_dependent_expression_p
23981 (TREE_OPERAND (TREE_VALUE (type), 1));
23982 else
23983 return dependent_type_p (type);
23986 if (TREE_CODE (expression) == SCOPE_REF)
23988 tree scope = TREE_OPERAND (expression, 0);
23989 tree name = TREE_OPERAND (expression, 1);
23991 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
23992 contains an identifier associated by name lookup with one or more
23993 declarations declared with a dependent type, or...a
23994 nested-name-specifier or qualified-id that names a member of an
23995 unknown specialization. */
23996 return (type_dependent_expression_p (name)
23997 || dependent_scope_p (scope));
24000 if (TREE_CODE (expression) == TEMPLATE_DECL
24001 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
24002 return uses_outer_template_parms (expression);
24004 if (TREE_CODE (expression) == STMT_EXPR)
24005 expression = stmt_expr_value_expr (expression);
24007 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
24009 tree elt;
24010 unsigned i;
24012 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
24014 if (type_dependent_expression_p (elt))
24015 return true;
24017 return false;
24020 /* A static data member of the current instantiation with incomplete
24021 array type is type-dependent, as the definition and specializations
24022 can have different bounds. */
24023 if (VAR_P (expression)
24024 && DECL_CLASS_SCOPE_P (expression)
24025 && dependent_type_p (DECL_CONTEXT (expression))
24026 && VAR_HAD_UNKNOWN_BOUND (expression))
24027 return true;
24029 /* An array of unknown bound depending on a variadic parameter, eg:
24031 template<typename... Args>
24032 void foo (Args... args)
24034 int arr[] = { args... };
24037 template<int... vals>
24038 void bar ()
24040 int arr[] = { vals... };
24043 If the array has no length and has an initializer, it must be that
24044 we couldn't determine its length in cp_complete_array_type because
24045 it is dependent. */
24046 if (VAR_P (expression)
24047 && TREE_TYPE (expression) != NULL_TREE
24048 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
24049 && !TYPE_DOMAIN (TREE_TYPE (expression))
24050 && DECL_INITIAL (expression))
24051 return true;
24053 /* A function or variable template-id is type-dependent if it has any
24054 dependent template arguments. */
24055 if (VAR_OR_FUNCTION_DECL_P (expression)
24056 && DECL_LANG_SPECIFIC (expression)
24057 && DECL_TEMPLATE_INFO (expression))
24059 /* Consider the innermost template arguments, since those are the ones
24060 that come from the template-id; the template arguments for the
24061 enclosing class do not make it type-dependent unless they are used in
24062 the type of the decl. */
24063 if (PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression))
24064 && (any_dependent_template_arguments_p
24065 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
24066 return true;
24068 /* Otherwise, if the decl isn't from a dependent scope, it can't be
24069 type-dependent. Checking this is important for functions with auto
24070 return type, which looks like a dependent type. */
24071 if (TREE_CODE (expression) == FUNCTION_DECL
24072 && (!DECL_CLASS_SCOPE_P (expression)
24073 || !dependent_type_p (DECL_CONTEXT (expression)))
24074 && (!DECL_FRIEND_CONTEXT (expression)
24075 || !dependent_type_p (DECL_FRIEND_CONTEXT (expression)))
24076 && !DECL_LOCAL_FUNCTION_P (expression))
24078 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
24079 || undeduced_auto_decl (expression));
24080 return false;
24084 /* Always dependent, on the number of arguments if nothing else. */
24085 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
24086 return true;
24088 if (TREE_TYPE (expression) == unknown_type_node)
24090 if (TREE_CODE (expression) == ADDR_EXPR)
24091 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
24092 if (TREE_CODE (expression) == COMPONENT_REF
24093 || TREE_CODE (expression) == OFFSET_REF)
24095 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
24096 return true;
24097 expression = TREE_OPERAND (expression, 1);
24098 if (identifier_p (expression))
24099 return false;
24101 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
24102 if (TREE_CODE (expression) == SCOPE_REF)
24103 return false;
24105 if (BASELINK_P (expression))
24107 if (BASELINK_OPTYPE (expression)
24108 && dependent_type_p (BASELINK_OPTYPE (expression)))
24109 return true;
24110 expression = BASELINK_FUNCTIONS (expression);
24113 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
24115 if (any_dependent_template_arguments_p
24116 (TREE_OPERAND (expression, 1)))
24117 return true;
24118 expression = TREE_OPERAND (expression, 0);
24119 if (identifier_p (expression))
24120 return true;
24123 gcc_assert (TREE_CODE (expression) == OVERLOAD
24124 || TREE_CODE (expression) == FUNCTION_DECL);
24126 for (lkp_iterator iter (expression); iter; ++iter)
24127 if (type_dependent_expression_p (*iter))
24128 return true;
24130 return false;
24133 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
24135 /* Dependent type attributes might not have made it from the decl to
24136 the type yet. */
24137 if (DECL_P (expression)
24138 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
24139 return true;
24141 return (dependent_type_p (TREE_TYPE (expression)));
24144 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
24145 type-dependent if the expression refers to a member of the current
24146 instantiation and the type of the referenced member is dependent, or the
24147 class member access expression refers to a member of an unknown
24148 specialization.
24150 This function returns true if the OBJECT in such a class member access
24151 expression is of an unknown specialization. */
24153 bool
24154 type_dependent_object_expression_p (tree object)
24156 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
24157 dependent. */
24158 if (TREE_CODE (object) == IDENTIFIER_NODE)
24159 return true;
24160 tree scope = TREE_TYPE (object);
24161 return (!scope || dependent_scope_p (scope));
24164 /* walk_tree callback function for instantiation_dependent_expression_p,
24165 below. Returns non-zero if a dependent subexpression is found. */
24167 static tree
24168 instantiation_dependent_r (tree *tp, int *walk_subtrees,
24169 void * /*data*/)
24171 if (TYPE_P (*tp))
24173 /* We don't have to worry about decltype currently because decltype
24174 of an instantiation-dependent expr is a dependent type. This
24175 might change depending on the resolution of DR 1172. */
24176 *walk_subtrees = false;
24177 return NULL_TREE;
24179 enum tree_code code = TREE_CODE (*tp);
24180 switch (code)
24182 /* Don't treat an argument list as dependent just because it has no
24183 TREE_TYPE. */
24184 case TREE_LIST:
24185 case TREE_VEC:
24186 return NULL_TREE;
24188 case TEMPLATE_PARM_INDEX:
24189 return *tp;
24191 /* Handle expressions with type operands. */
24192 case SIZEOF_EXPR:
24193 case ALIGNOF_EXPR:
24194 case TYPEID_EXPR:
24195 case AT_ENCODE_EXPR:
24197 tree op = TREE_OPERAND (*tp, 0);
24198 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
24199 op = TREE_TYPE (op);
24200 if (TYPE_P (op))
24202 if (dependent_type_p (op))
24203 return *tp;
24204 else
24206 *walk_subtrees = false;
24207 return NULL_TREE;
24210 break;
24213 case COMPONENT_REF:
24214 if (identifier_p (TREE_OPERAND (*tp, 1)))
24215 /* In a template, finish_class_member_access_expr creates a
24216 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
24217 type-dependent, so that we can check access control at
24218 instantiation time (PR 42277). See also Core issue 1273. */
24219 return *tp;
24220 break;
24222 case SCOPE_REF:
24223 if (instantiation_dependent_scope_ref_p (*tp))
24224 return *tp;
24225 else
24226 break;
24228 /* Treat statement-expressions as dependent. */
24229 case BIND_EXPR:
24230 return *tp;
24232 /* Treat requires-expressions as dependent. */
24233 case REQUIRES_EXPR:
24234 return *tp;
24236 case CALL_EXPR:
24237 /* Treat calls to function concepts as dependent. */
24238 if (function_concept_check_p (*tp))
24239 return *tp;
24240 break;
24242 case TEMPLATE_ID_EXPR:
24243 /* And variable concepts. */
24244 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
24245 return *tp;
24246 break;
24248 default:
24249 break;
24252 if (type_dependent_expression_p (*tp))
24253 return *tp;
24254 else
24255 return NULL_TREE;
24258 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
24259 sense defined by the ABI:
24261 "An expression is instantiation-dependent if it is type-dependent
24262 or value-dependent, or it has a subexpression that is type-dependent
24263 or value-dependent."
24265 Except don't actually check value-dependence for unevaluated expressions,
24266 because in sizeof(i) we don't care about the value of i. Checking
24267 type-dependence will in turn check value-dependence of array bounds/template
24268 arguments as needed. */
24270 bool
24271 instantiation_dependent_uneval_expression_p (tree expression)
24273 tree result;
24275 if (!processing_template_decl)
24276 return false;
24278 if (expression == error_mark_node)
24279 return false;
24281 result = cp_walk_tree_without_duplicates (&expression,
24282 instantiation_dependent_r, NULL);
24283 return result != NULL_TREE;
24286 /* As above, but also check value-dependence of the expression as a whole. */
24288 bool
24289 instantiation_dependent_expression_p (tree expression)
24291 return (instantiation_dependent_uneval_expression_p (expression)
24292 || value_dependent_expression_p (expression));
24295 /* Like type_dependent_expression_p, but it also works while not processing
24296 a template definition, i.e. during substitution or mangling. */
24298 bool
24299 type_dependent_expression_p_push (tree expr)
24301 bool b;
24302 ++processing_template_decl;
24303 b = type_dependent_expression_p (expr);
24304 --processing_template_decl;
24305 return b;
24308 /* Returns TRUE if ARGS contains a type-dependent expression. */
24310 bool
24311 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
24313 unsigned int i;
24314 tree arg;
24316 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
24318 if (type_dependent_expression_p (arg))
24319 return true;
24321 return false;
24324 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24325 expressions) contains any type-dependent expressions. */
24327 bool
24328 any_type_dependent_elements_p (const_tree list)
24330 for (; list; list = TREE_CHAIN (list))
24331 if (type_dependent_expression_p (TREE_VALUE (list)))
24332 return true;
24334 return false;
24337 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24338 expressions) contains any value-dependent expressions. */
24340 bool
24341 any_value_dependent_elements_p (const_tree list)
24343 for (; list; list = TREE_CHAIN (list))
24344 if (value_dependent_expression_p (TREE_VALUE (list)))
24345 return true;
24347 return false;
24350 /* Returns TRUE if the ARG (a template argument) is dependent. */
24352 bool
24353 dependent_template_arg_p (tree arg)
24355 if (!processing_template_decl)
24356 return false;
24358 /* Assume a template argument that was wrongly written by the user
24359 is dependent. This is consistent with what
24360 any_dependent_template_arguments_p [that calls this function]
24361 does. */
24362 if (!arg || arg == error_mark_node)
24363 return true;
24365 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
24366 arg = ARGUMENT_PACK_SELECT_ARG (arg);
24368 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
24369 return true;
24370 if (TREE_CODE (arg) == TEMPLATE_DECL)
24372 if (DECL_TEMPLATE_PARM_P (arg))
24373 return true;
24374 /* A member template of a dependent class is not necessarily
24375 type-dependent, but it is a dependent template argument because it
24376 will be a member of an unknown specialization to that template. */
24377 tree scope = CP_DECL_CONTEXT (arg);
24378 return TYPE_P (scope) && dependent_type_p (scope);
24380 else if (ARGUMENT_PACK_P (arg))
24382 tree args = ARGUMENT_PACK_ARGS (arg);
24383 int i, len = TREE_VEC_LENGTH (args);
24384 for (i = 0; i < len; ++i)
24386 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
24387 return true;
24390 return false;
24392 else if (TYPE_P (arg))
24393 return dependent_type_p (arg);
24394 else
24395 return (type_dependent_expression_p (arg)
24396 || value_dependent_expression_p (arg));
24399 /* Returns true if ARGS (a collection of template arguments) contains
24400 any types that require structural equality testing. */
24402 bool
24403 any_template_arguments_need_structural_equality_p (tree args)
24405 int i;
24406 int j;
24408 if (!args)
24409 return false;
24410 if (args == error_mark_node)
24411 return true;
24413 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24415 tree level = TMPL_ARGS_LEVEL (args, i + 1);
24416 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24418 tree arg = TREE_VEC_ELT (level, j);
24419 tree packed_args = NULL_TREE;
24420 int k, len = 1;
24422 if (ARGUMENT_PACK_P (arg))
24424 /* Look inside the argument pack. */
24425 packed_args = ARGUMENT_PACK_ARGS (arg);
24426 len = TREE_VEC_LENGTH (packed_args);
24429 for (k = 0; k < len; ++k)
24431 if (packed_args)
24432 arg = TREE_VEC_ELT (packed_args, k);
24434 if (error_operand_p (arg))
24435 return true;
24436 else if (TREE_CODE (arg) == TEMPLATE_DECL)
24437 continue;
24438 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
24439 return true;
24440 else if (!TYPE_P (arg) && TREE_TYPE (arg)
24441 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
24442 return true;
24447 return false;
24450 /* Returns true if ARGS (a collection of template arguments) contains
24451 any dependent arguments. */
24453 bool
24454 any_dependent_template_arguments_p (const_tree args)
24456 int i;
24457 int j;
24459 if (!args)
24460 return false;
24461 if (args == error_mark_node)
24462 return true;
24464 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24466 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
24467 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24468 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
24469 return true;
24472 return false;
24475 /* Returns TRUE if the template TMPL is type-dependent. */
24477 bool
24478 dependent_template_p (tree tmpl)
24480 if (TREE_CODE (tmpl) == OVERLOAD)
24482 for (lkp_iterator iter (tmpl); iter; ++iter)
24483 if (dependent_template_p (*iter))
24484 return true;
24485 return false;
24488 /* Template template parameters are dependent. */
24489 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
24490 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
24491 return true;
24492 /* So are names that have not been looked up. */
24493 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
24494 return true;
24495 return false;
24498 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
24500 bool
24501 dependent_template_id_p (tree tmpl, tree args)
24503 return (dependent_template_p (tmpl)
24504 || any_dependent_template_arguments_p (args));
24507 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
24508 are dependent. */
24510 bool
24511 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
24513 int i;
24515 if (!processing_template_decl)
24516 return false;
24518 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
24520 tree decl = TREE_VEC_ELT (declv, i);
24521 tree init = TREE_VEC_ELT (initv, i);
24522 tree cond = TREE_VEC_ELT (condv, i);
24523 tree incr = TREE_VEC_ELT (incrv, i);
24525 if (type_dependent_expression_p (decl)
24526 || TREE_CODE (decl) == SCOPE_REF)
24527 return true;
24529 if (init && type_dependent_expression_p (init))
24530 return true;
24532 if (type_dependent_expression_p (cond))
24533 return true;
24535 if (COMPARISON_CLASS_P (cond)
24536 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
24537 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
24538 return true;
24540 if (TREE_CODE (incr) == MODOP_EXPR)
24542 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
24543 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
24544 return true;
24546 else if (type_dependent_expression_p (incr))
24547 return true;
24548 else if (TREE_CODE (incr) == MODIFY_EXPR)
24550 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
24551 return true;
24552 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
24554 tree t = TREE_OPERAND (incr, 1);
24555 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
24556 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
24557 return true;
24562 return false;
24565 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
24566 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
24567 no such TYPE can be found. Note that this function peers inside
24568 uninstantiated templates and therefore should be used only in
24569 extremely limited situations. ONLY_CURRENT_P restricts this
24570 peering to the currently open classes hierarchy (which is required
24571 when comparing types). */
24573 tree
24574 resolve_typename_type (tree type, bool only_current_p)
24576 tree scope;
24577 tree name;
24578 tree decl;
24579 int quals;
24580 tree pushed_scope;
24581 tree result;
24583 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
24585 scope = TYPE_CONTEXT (type);
24586 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
24587 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
24588 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
24589 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
24590 identifier of the TYPENAME_TYPE anymore.
24591 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
24592 TYPENAME_TYPE instead, we avoid messing up with a possible
24593 typedef variant case. */
24594 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
24596 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
24597 it first before we can figure out what NAME refers to. */
24598 if (TREE_CODE (scope) == TYPENAME_TYPE)
24600 if (TYPENAME_IS_RESOLVING_P (scope))
24601 /* Given a class template A with a dependent base with nested type C,
24602 typedef typename A::C::C C will land us here, as trying to resolve
24603 the initial A::C leads to the local C typedef, which leads back to
24604 A::C::C. So we break the recursion now. */
24605 return type;
24606 else
24607 scope = resolve_typename_type (scope, only_current_p);
24609 /* If we don't know what SCOPE refers to, then we cannot resolve the
24610 TYPENAME_TYPE. */
24611 if (!CLASS_TYPE_P (scope))
24612 return type;
24613 /* If this is a typedef, we don't want to look inside (c++/11987). */
24614 if (typedef_variant_p (type))
24615 return type;
24616 /* If SCOPE isn't the template itself, it will not have a valid
24617 TYPE_FIELDS list. */
24618 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
24619 /* scope is either the template itself or a compatible instantiation
24620 like X<T>, so look up the name in the original template. */
24621 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
24622 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
24623 gcc_checking_assert (uses_template_parms (scope));
24624 /* If scope has no fields, it can't be a current instantiation. Check this
24625 before currently_open_class to avoid infinite recursion (71515). */
24626 if (!TYPE_FIELDS (scope))
24627 return type;
24628 /* If the SCOPE is not the current instantiation, there's no reason
24629 to look inside it. */
24630 if (only_current_p && !currently_open_class (scope))
24631 return type;
24632 /* Enter the SCOPE so that name lookup will be resolved as if we
24633 were in the class definition. In particular, SCOPE will no
24634 longer be considered a dependent type. */
24635 pushed_scope = push_scope (scope);
24636 /* Look up the declaration. */
24637 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
24638 tf_warning_or_error);
24640 result = NULL_TREE;
24642 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
24643 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
24644 if (!decl)
24645 /*nop*/;
24646 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type))
24647 && TREE_CODE (decl) == TYPE_DECL)
24649 result = TREE_TYPE (decl);
24650 if (result == error_mark_node)
24651 result = NULL_TREE;
24653 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
24654 && DECL_CLASS_TEMPLATE_P (decl))
24656 tree tmpl;
24657 tree args;
24658 /* Obtain the template and the arguments. */
24659 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
24660 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
24661 /* Instantiate the template. */
24662 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
24663 /*entering_scope=*/0,
24664 tf_error | tf_user);
24665 if (result == error_mark_node)
24666 result = NULL_TREE;
24669 /* Leave the SCOPE. */
24670 if (pushed_scope)
24671 pop_scope (pushed_scope);
24673 /* If we failed to resolve it, return the original typename. */
24674 if (!result)
24675 return type;
24677 /* If lookup found a typename type, resolve that too. */
24678 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
24680 /* Ill-formed programs can cause infinite recursion here, so we
24681 must catch that. */
24682 TYPENAME_IS_RESOLVING_P (result) = 1;
24683 result = resolve_typename_type (result, only_current_p);
24684 TYPENAME_IS_RESOLVING_P (result) = 0;
24687 /* Qualify the resulting type. */
24688 quals = cp_type_quals (type);
24689 if (quals)
24690 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
24692 return result;
24695 /* EXPR is an expression which is not type-dependent. Return a proxy
24696 for EXPR that can be used to compute the types of larger
24697 expressions containing EXPR. */
24699 tree
24700 build_non_dependent_expr (tree expr)
24702 tree inner_expr;
24704 /* When checking, try to get a constant value for all non-dependent
24705 expressions in order to expose bugs in *_dependent_expression_p
24706 and constexpr. This can affect code generation, see PR70704, so
24707 only do this for -fchecking=2. */
24708 if (flag_checking > 1
24709 && cxx_dialect >= cxx11
24710 /* Don't do this during nsdmi parsing as it can lead to
24711 unexpected recursive instantiations. */
24712 && !parsing_nsdmi ()
24713 /* Don't do this during concept expansion either and for
24714 the same reason. */
24715 && !expanding_concept ())
24716 fold_non_dependent_expr (expr);
24718 /* Preserve OVERLOADs; the functions must be available to resolve
24719 types. */
24720 inner_expr = expr;
24721 if (TREE_CODE (inner_expr) == STMT_EXPR)
24722 inner_expr = stmt_expr_value_expr (inner_expr);
24723 if (TREE_CODE (inner_expr) == ADDR_EXPR)
24724 inner_expr = TREE_OPERAND (inner_expr, 0);
24725 if (TREE_CODE (inner_expr) == COMPONENT_REF)
24726 inner_expr = TREE_OPERAND (inner_expr, 1);
24727 if (is_overloaded_fn (inner_expr)
24728 || TREE_CODE (inner_expr) == OFFSET_REF)
24729 return expr;
24730 /* There is no need to return a proxy for a variable. */
24731 if (VAR_P (expr))
24732 return expr;
24733 /* Preserve string constants; conversions from string constants to
24734 "char *" are allowed, even though normally a "const char *"
24735 cannot be used to initialize a "char *". */
24736 if (TREE_CODE (expr) == STRING_CST)
24737 return expr;
24738 /* Preserve void and arithmetic constants, as an optimization -- there is no
24739 reason to create a new node. */
24740 if (TREE_CODE (expr) == VOID_CST
24741 || TREE_CODE (expr) == INTEGER_CST
24742 || TREE_CODE (expr) == REAL_CST)
24743 return expr;
24744 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
24745 There is at least one place where we want to know that a
24746 particular expression is a throw-expression: when checking a ?:
24747 expression, there are special rules if the second or third
24748 argument is a throw-expression. */
24749 if (TREE_CODE (expr) == THROW_EXPR)
24750 return expr;
24752 /* Don't wrap an initializer list, we need to be able to look inside. */
24753 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
24754 return expr;
24756 /* Don't wrap a dummy object, we need to be able to test for it. */
24757 if (is_dummy_object (expr))
24758 return expr;
24760 if (TREE_CODE (expr) == COND_EXPR)
24761 return build3 (COND_EXPR,
24762 TREE_TYPE (expr),
24763 TREE_OPERAND (expr, 0),
24764 (TREE_OPERAND (expr, 1)
24765 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
24766 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
24767 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
24768 if (TREE_CODE (expr) == COMPOUND_EXPR
24769 && !COMPOUND_EXPR_OVERLOADED (expr))
24770 return build2 (COMPOUND_EXPR,
24771 TREE_TYPE (expr),
24772 TREE_OPERAND (expr, 0),
24773 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
24775 /* If the type is unknown, it can't really be non-dependent */
24776 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
24778 /* Otherwise, build a NON_DEPENDENT_EXPR. */
24779 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
24782 /* ARGS is a vector of expressions as arguments to a function call.
24783 Replace the arguments with equivalent non-dependent expressions.
24784 This modifies ARGS in place. */
24786 void
24787 make_args_non_dependent (vec<tree, va_gc> *args)
24789 unsigned int ix;
24790 tree arg;
24792 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
24794 tree newarg = build_non_dependent_expr (arg);
24795 if (newarg != arg)
24796 (*args)[ix] = newarg;
24800 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
24801 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
24802 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
24804 static tree
24805 make_auto_1 (tree name, bool set_canonical)
24807 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
24808 TYPE_NAME (au) = build_decl (input_location,
24809 TYPE_DECL, name, au);
24810 TYPE_STUB_DECL (au) = TYPE_NAME (au);
24811 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
24812 (0, processing_template_decl + 1, processing_template_decl + 1,
24813 TYPE_NAME (au), NULL_TREE);
24814 if (set_canonical)
24815 TYPE_CANONICAL (au) = canonical_type_parameter (au);
24816 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
24817 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
24819 return au;
24822 tree
24823 make_decltype_auto (void)
24825 return make_auto_1 (decltype_auto_identifier, true);
24828 tree
24829 make_auto (void)
24831 return make_auto_1 (auto_identifier, true);
24834 /* Return a C++17 deduction placeholder for class template TMPL. */
24836 tree
24837 make_template_placeholder (tree tmpl)
24839 tree t = make_auto_1 (DECL_NAME (tmpl), true);
24840 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
24841 return t;
24844 /* True iff T is a C++17 class template deduction placeholder. */
24846 bool
24847 template_placeholder_p (tree t)
24849 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
24852 /* Make a "constrained auto" type-specifier. This is an
24853 auto type with constraints that must be associated after
24854 deduction. The constraint is formed from the given
24855 CONC and its optional sequence of arguments, which are
24856 non-null if written as partial-concept-id. */
24858 tree
24859 make_constrained_auto (tree con, tree args)
24861 tree type = make_auto_1 (auto_identifier, false);
24863 /* Build the constraint. */
24864 tree tmpl = DECL_TI_TEMPLATE (con);
24865 tree expr = VAR_P (con) ? tmpl : ovl_make (tmpl);
24866 expr = build_concept_check (expr, type, args);
24868 tree constr = normalize_expression (expr);
24869 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
24871 /* Our canonical type depends on the constraint. */
24872 TYPE_CANONICAL (type) = canonical_type_parameter (type);
24874 /* Attach the constraint to the type declaration. */
24875 tree decl = TYPE_NAME (type);
24876 return decl;
24879 /* Given type ARG, return std::initializer_list<ARG>. */
24881 static tree
24882 listify (tree arg)
24884 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
24886 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
24888 error ("deducing from brace-enclosed initializer list requires "
24889 "#include <initializer_list>");
24890 return error_mark_node;
24892 tree argvec = make_tree_vec (1);
24893 TREE_VEC_ELT (argvec, 0) = arg;
24895 return lookup_template_class (std_init_list, argvec, NULL_TREE,
24896 NULL_TREE, 0, tf_warning_or_error);
24899 /* Replace auto in TYPE with std::initializer_list<auto>. */
24901 static tree
24902 listify_autos (tree type, tree auto_node)
24904 tree init_auto = listify (auto_node);
24905 tree argvec = make_tree_vec (1);
24906 TREE_VEC_ELT (argvec, 0) = init_auto;
24907 if (processing_template_decl)
24908 argvec = add_to_template_args (current_template_args (), argvec);
24909 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
24912 /* Hash traits for hashing possibly constrained 'auto'
24913 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
24915 struct auto_hash : default_hash_traits<tree>
24917 static inline hashval_t hash (tree);
24918 static inline bool equal (tree, tree);
24921 /* Hash the 'auto' T. */
24923 inline hashval_t
24924 auto_hash::hash (tree t)
24926 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
24927 /* Matching constrained-type-specifiers denote the same template
24928 parameter, so hash the constraint. */
24929 return hash_placeholder_constraint (c);
24930 else
24931 /* But unconstrained autos are all separate, so just hash the pointer. */
24932 return iterative_hash_object (t, 0);
24935 /* Compare two 'auto's. */
24937 inline bool
24938 auto_hash::equal (tree t1, tree t2)
24940 if (t1 == t2)
24941 return true;
24943 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
24944 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
24946 /* Two unconstrained autos are distinct. */
24947 if (!c1 || !c2)
24948 return false;
24950 return equivalent_placeholder_constraints (c1, c2);
24953 /* for_each_template_parm callback for extract_autos: if t is a (possibly
24954 constrained) auto, add it to the vector. */
24956 static int
24957 extract_autos_r (tree t, void *data)
24959 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
24960 if (is_auto (t))
24962 /* All the autos were built with index 0; fix that up now. */
24963 tree *p = hash.find_slot (t, INSERT);
24964 unsigned idx;
24965 if (*p)
24966 /* If this is a repeated constrained-type-specifier, use the index we
24967 chose before. */
24968 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
24969 else
24971 /* Otherwise this is new, so use the current count. */
24972 *p = t;
24973 idx = hash.elements () - 1;
24975 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
24978 /* Always keep walking. */
24979 return 0;
24982 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
24983 says they can appear anywhere in the type. */
24985 static tree
24986 extract_autos (tree type)
24988 hash_set<tree> visited;
24989 hash_table<auto_hash> hash (2);
24991 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
24993 tree tree_vec = make_tree_vec (hash.elements());
24994 for (hash_table<auto_hash>::iterator iter = hash.begin();
24995 iter != hash.end(); ++iter)
24997 tree elt = *iter;
24998 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
24999 TREE_VEC_ELT (tree_vec, i)
25000 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
25003 return tree_vec;
25006 /* The stem for deduction guide names. */
25007 const char *const dguide_base = "__dguide_";
25009 /* Return the name for a deduction guide for class template TMPL. */
25011 tree
25012 dguide_name (tree tmpl)
25014 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
25015 tree tname = TYPE_IDENTIFIER (type);
25016 char *buf = (char *) alloca (1 + strlen (dguide_base)
25017 + IDENTIFIER_LENGTH (tname));
25018 memcpy (buf, dguide_base, strlen (dguide_base));
25019 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
25020 IDENTIFIER_LENGTH (tname) + 1);
25021 tree dname = get_identifier (buf);
25022 TREE_TYPE (dname) = type;
25023 return dname;
25026 /* True if NAME is the name of a deduction guide. */
25028 bool
25029 dguide_name_p (tree name)
25031 return (TREE_TYPE (name)
25032 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
25033 strlen (dguide_base)));
25036 /* True if FN is a deduction guide. */
25038 bool
25039 deduction_guide_p (const_tree fn)
25041 if (DECL_P (fn))
25042 if (tree name = DECL_NAME (fn))
25043 return dguide_name_p (name);
25044 return false;
25047 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
25049 bool
25050 copy_guide_p (const_tree fn)
25052 gcc_assert (deduction_guide_p (fn));
25053 if (!DECL_ARTIFICIAL (fn))
25054 return false;
25055 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
25056 return (TREE_CHAIN (parms) == void_list_node
25057 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
25060 /* True if FN is a guide generated from a constructor template. */
25062 bool
25063 template_guide_p (const_tree fn)
25065 gcc_assert (deduction_guide_p (fn));
25066 if (!DECL_ARTIFICIAL (fn))
25067 return false;
25068 if (tree ctor = DECL_ABSTRACT_ORIGIN (fn))
25070 tree tmpl = DECL_TI_TEMPLATE (ctor);
25071 return PRIMARY_TEMPLATE_P (tmpl);
25073 return false;
25076 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
25077 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
25078 template parameter types. Note that the handling of template template
25079 parameters relies on current_template_parms being set appropriately for the
25080 new template. */
25082 static tree
25083 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
25084 tree tsubst_args, tsubst_flags_t complain)
25086 tree oldidx = get_template_parm_index (olddecl);
25088 tree newtype;
25089 if (TREE_CODE (olddecl) == TYPE_DECL
25090 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25092 tree oldtype = TREE_TYPE (olddecl);
25093 newtype = cxx_make_type (TREE_CODE (oldtype));
25094 TYPE_MAIN_VARIANT (newtype) = newtype;
25095 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
25096 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
25097 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
25099 else
25100 newtype = tsubst (TREE_TYPE (olddecl), tsubst_args,
25101 complain, NULL_TREE);
25103 tree newdecl
25104 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
25105 DECL_NAME (olddecl), newtype);
25106 SET_DECL_TEMPLATE_PARM_P (newdecl);
25108 tree newidx;
25109 if (TREE_CODE (olddecl) == TYPE_DECL
25110 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25112 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
25113 = build_template_parm_index (index, level, level,
25114 newdecl, newtype);
25115 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25116 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25117 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
25118 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
25120 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
25122 DECL_TEMPLATE_RESULT (newdecl)
25123 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
25124 DECL_NAME (olddecl), newtype);
25125 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
25126 // First create a copy (ttargs) of tsubst_args with an
25127 // additional level for the template template parameter's own
25128 // template parameters (ttparms).
25129 tree ttparms = (INNERMOST_TEMPLATE_PARMS
25130 (DECL_TEMPLATE_PARMS (olddecl)));
25131 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
25132 tree ttargs = make_tree_vec (depth + 1);
25133 for (int i = 0; i < depth; ++i)
25134 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
25135 TREE_VEC_ELT (ttargs, depth)
25136 = template_parms_level_to_args (ttparms);
25137 // Substitute ttargs into ttparms to fix references to
25138 // other template parameters.
25139 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25140 complain);
25141 // Now substitute again with args based on tparms, to reduce
25142 // the level of the ttparms.
25143 ttargs = current_template_args ();
25144 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25145 complain);
25146 // Finally, tack the adjusted parms onto tparms.
25147 ttparms = tree_cons (size_int (depth), ttparms,
25148 current_template_parms);
25149 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
25152 else
25154 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
25155 tree newconst
25156 = build_decl (DECL_SOURCE_LOCATION (oldconst),
25157 TREE_CODE (oldconst),
25158 DECL_NAME (oldconst), newtype);
25159 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
25160 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
25161 SET_DECL_TEMPLATE_PARM_P (newconst);
25162 newidx = build_template_parm_index (index, level, level,
25163 newconst, newtype);
25164 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25165 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25166 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
25169 return newdecl;
25172 /* Returns a C++17 class deduction guide template based on the constructor
25173 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
25174 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
25176 static tree
25177 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
25179 tree type, tparms, targs, fparms, fargs, ci;
25180 bool memtmpl = false;
25181 bool explicit_p;
25182 location_t loc;
25184 if (TYPE_P (ctor))
25186 type = ctor;
25187 bool copy_p = TREE_CODE (type) == REFERENCE_TYPE;
25188 if (copy_p)
25190 type = TREE_TYPE (type);
25191 fparms = tree_cons (NULL_TREE, type, void_list_node);
25193 else
25194 fparms = void_list_node;
25196 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
25197 tparms = DECL_TEMPLATE_PARMS (ctmpl);
25198 targs = CLASSTYPE_TI_ARGS (type);
25199 ci = NULL_TREE;
25200 fargs = NULL_TREE;
25201 loc = DECL_SOURCE_LOCATION (ctmpl);
25202 explicit_p = false;
25204 else
25206 if (outer_args)
25207 ctor = tsubst (ctor, outer_args, complain, ctor);
25208 type = DECL_CONTEXT (ctor);
25209 tree fn_tmpl;
25210 if (TREE_CODE (ctor) == TEMPLATE_DECL)
25212 fn_tmpl = ctor;
25213 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
25215 else
25216 fn_tmpl = DECL_TI_TEMPLATE (ctor);
25218 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
25219 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
25220 fully specialized args for the enclosing class. Strip those off, as
25221 the deduction guide won't have those template parameters. */
25222 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
25223 TMPL_PARMS_DEPTH (tparms));
25224 /* Discard the 'this' parameter. */
25225 fparms = FUNCTION_ARG_CHAIN (ctor);
25226 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
25227 ci = get_constraints (ctor);
25228 loc = DECL_SOURCE_LOCATION (ctor);
25229 explicit_p = DECL_NONCONVERTING_P (ctor);
25231 if (PRIMARY_TEMPLATE_P (fn_tmpl))
25233 memtmpl = true;
25235 /* For a member template constructor, we need to flatten the two
25236 template parameter lists into one, and then adjust the function
25237 signature accordingly. This gets...complicated. */
25238 ++processing_template_decl;
25239 tree save_parms = current_template_parms;
25241 /* For a member template we should have two levels of parms/args, one
25242 for the class and one for the constructor. We stripped
25243 specialized args for further enclosing classes above. */
25244 const int depth = 2;
25245 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
25247 /* Template args for translating references to the two-level template
25248 parameters into references to the one-level template parameters we
25249 are creating. */
25250 tree tsubst_args = copy_node (targs);
25251 TMPL_ARGS_LEVEL (tsubst_args, depth)
25252 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
25254 /* Template parms for the constructor template. */
25255 tree ftparms = TREE_VALUE (tparms);
25256 unsigned flen = TREE_VEC_LENGTH (ftparms);
25257 /* Template parms for the class template. */
25258 tparms = TREE_CHAIN (tparms);
25259 tree ctparms = TREE_VALUE (tparms);
25260 unsigned clen = TREE_VEC_LENGTH (ctparms);
25261 /* Template parms for the deduction guide start as a copy of the
25262 template parms for the class. We set current_template_parms for
25263 lookup_template_class_1. */
25264 current_template_parms = tparms = copy_node (tparms);
25265 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
25266 for (unsigned i = 0; i < clen; ++i)
25267 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
25269 /* Now we need to rewrite the constructor parms to append them to the
25270 class parms. */
25271 for (unsigned i = 0; i < flen; ++i)
25273 unsigned index = i + clen;
25274 unsigned level = 1;
25275 tree oldelt = TREE_VEC_ELT (ftparms, i);
25276 tree olddecl = TREE_VALUE (oldelt);
25277 tree newdecl = rewrite_template_parm (olddecl, index, level,
25278 tsubst_args, complain);
25279 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
25280 tsubst_args, complain, ctor);
25281 tree list = build_tree_list (newdef, newdecl);
25282 TEMPLATE_PARM_CONSTRAINTS (list)
25283 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
25284 tsubst_args, complain, ctor);
25285 TREE_VEC_ELT (new_vec, index) = list;
25286 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
25289 /* Now we have a final set of template parms to substitute into the
25290 function signature. */
25291 targs = template_parms_to_args (tparms);
25292 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
25293 complain, ctor);
25294 fargs = tsubst (fargs, tsubst_args, complain, ctor);
25295 if (ci)
25296 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
25298 current_template_parms = save_parms;
25299 --processing_template_decl;
25303 if (!memtmpl)
25305 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
25306 tparms = copy_node (tparms);
25307 INNERMOST_TEMPLATE_PARMS (tparms)
25308 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
25311 tree fntype = build_function_type (type, fparms);
25312 tree ded_fn = build_lang_decl_loc (loc,
25313 FUNCTION_DECL,
25314 dguide_name (type), fntype);
25315 DECL_ARGUMENTS (ded_fn) = fargs;
25316 DECL_ARTIFICIAL (ded_fn) = true;
25317 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
25318 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
25319 DECL_ARTIFICIAL (ded_tmpl) = true;
25320 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
25321 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
25322 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
25323 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
25324 if (DECL_P (ctor))
25325 DECL_ABSTRACT_ORIGIN (ded_fn) = ctor;
25326 if (ci)
25327 set_constraints (ded_tmpl, ci);
25329 return ded_tmpl;
25332 /* Deduce template arguments for the class template placeholder PTYPE for
25333 template TMPL based on the initializer INIT, and return the resulting
25334 type. */
25336 static tree
25337 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
25338 tsubst_flags_t complain)
25340 if (!DECL_CLASS_TEMPLATE_P (tmpl))
25342 /* We should have handled this in the caller. */
25343 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
25344 return ptype;
25345 if (complain & tf_error)
25346 error ("non-class template %qT used without template arguments", tmpl);
25347 return error_mark_node;
25350 tree type = TREE_TYPE (tmpl);
25352 vec<tree,va_gc> *args;
25353 if (init == NULL_TREE
25354 || TREE_CODE (init) == TREE_LIST)
25355 args = make_tree_vector_from_list (init);
25356 else if (BRACE_ENCLOSED_INITIALIZER_P (init)
25357 && !TYPE_HAS_LIST_CTOR (type)
25358 && !is_std_init_list (type))
25359 args = make_tree_vector_from_ctor (init);
25360 else
25361 args = make_tree_vector_single (init);
25363 tree dname = dguide_name (tmpl);
25364 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
25365 /*type*/false, /*complain*/false,
25366 /*hidden*/false);
25367 bool elided = false;
25368 if (cands == error_mark_node)
25369 cands = NULL_TREE;
25371 /* Prune explicit deduction guides in copy-initialization context. */
25372 if (flags & LOOKUP_ONLYCONVERTING)
25374 for (lkp_iterator iter (cands); !elided && iter; ++iter)
25375 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25376 elided = true;
25378 if (elided)
25380 /* Found a nonconverting guide, prune the candidates. */
25381 tree pruned = NULL_TREE;
25382 for (lkp_iterator iter (cands); iter; ++iter)
25383 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25384 pruned = lookup_add (*iter, pruned);
25386 cands = pruned;
25390 tree outer_args = NULL_TREE;
25391 if (DECL_CLASS_SCOPE_P (tmpl)
25392 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl)))
25394 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
25395 type = TREE_TYPE (most_general_template (tmpl));
25398 bool saw_ctor = false;
25399 if (CLASSTYPE_METHOD_VEC (type))
25400 // FIXME cache artificial deduction guides
25401 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type));
25402 iter; ++iter)
25404 tree guide = build_deduction_guide (*iter, outer_args, complain);
25405 if ((flags & LOOKUP_ONLYCONVERTING)
25406 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
25407 elided = true;
25408 else
25409 cands = lookup_add (guide, cands);
25411 saw_ctor = true;
25414 if (args->length () < 2)
25416 tree gtype = NULL_TREE;
25418 if (args->length () == 1)
25419 gtype = build_reference_type (type);
25420 else if (!saw_ctor)
25421 gtype = type;
25423 if (gtype)
25425 tree guide = build_deduction_guide (gtype, outer_args, complain);
25426 if ((flags & LOOKUP_ONLYCONVERTING)
25427 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
25428 elided = true;
25429 else
25430 cands = lookup_add (guide, cands);
25434 if (elided && !cands)
25436 error ("cannot deduce template arguments for copy-initialization"
25437 " of %qT, as it has no non-explicit deduction guides or "
25438 "user-declared constructors", type);
25439 return error_mark_node;
25442 ++cp_unevaluated_operand;
25443 tree t = build_new_function_call (cands, &args, tf_decltype);
25445 if (t == error_mark_node && (complain & tf_warning_or_error))
25447 error ("class template argument deduction failed:");
25448 t = build_new_function_call (cands, &args, complain | tf_decltype);
25449 if (elided)
25450 inform (input_location, "explicit deduction guides not considered "
25451 "for copy-initialization");
25454 --cp_unevaluated_operand;
25455 release_tree_vector (args);
25457 return cp_build_qualified_type (TREE_TYPE (t), cp_type_quals (ptype));
25460 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25461 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
25463 tree
25464 do_auto_deduction (tree type, tree init, tree auto_node)
25466 return do_auto_deduction (type, init, auto_node,
25467 tf_warning_or_error,
25468 adc_unspecified);
25471 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25472 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
25473 The CONTEXT determines the context in which auto deduction is performed
25474 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
25475 OUTER_TARGS are used during template argument deduction
25476 (context == adc_unify) to properly substitute the result, and is ignored
25477 in other contexts.
25479 For partial-concept-ids, extra args may be appended to the list of deduced
25480 template arguments prior to determining constraint satisfaction. */
25482 tree
25483 do_auto_deduction (tree type, tree init, tree auto_node,
25484 tsubst_flags_t complain, auto_deduction_context context,
25485 tree outer_targs, int flags)
25487 tree targs;
25489 if (init == error_mark_node)
25490 return error_mark_node;
25492 if (init && type_dependent_expression_p (init)
25493 && context != adc_unify)
25494 /* Defining a subset of type-dependent expressions that we can deduce
25495 from ahead of time isn't worth the trouble. */
25496 return type;
25498 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
25499 /* C++17 class template argument deduction. */
25500 return do_class_deduction (type, tmpl, init, flags, complain);
25502 if (TREE_TYPE (init) == NULL_TREE)
25503 /* Nothing we can do with this, even in deduction context. */
25504 return type;
25506 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
25507 with either a new invented type template parameter U or, if the
25508 initializer is a braced-init-list (8.5.4), with
25509 std::initializer_list<U>. */
25510 if (BRACE_ENCLOSED_INITIALIZER_P (init))
25512 if (!DIRECT_LIST_INIT_P (init))
25513 type = listify_autos (type, auto_node);
25514 else if (CONSTRUCTOR_NELTS (init) == 1)
25515 init = CONSTRUCTOR_ELT (init, 0)->value;
25516 else
25518 if (complain & tf_warning_or_error)
25520 if (permerror (input_location, "direct-list-initialization of "
25521 "%<auto%> requires exactly one element"))
25522 inform (input_location,
25523 "for deduction to %<std::initializer_list%>, use copy-"
25524 "list-initialization (i.e. add %<=%> before the %<{%>)");
25526 type = listify_autos (type, auto_node);
25530 if (type == error_mark_node)
25531 return error_mark_node;
25533 init = resolve_nondeduced_context (init, complain);
25535 if (context == adc_decomp_type
25536 && auto_node == type
25537 && init != error_mark_node
25538 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
25539 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
25540 and initializer has array type, deduce cv-qualified array type. */
25541 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
25542 complain);
25543 else if (AUTO_IS_DECLTYPE (auto_node))
25545 bool id = (DECL_P (init)
25546 || ((TREE_CODE (init) == COMPONENT_REF
25547 || TREE_CODE (init) == SCOPE_REF)
25548 && !REF_PARENTHESIZED_P (init)));
25549 targs = make_tree_vec (1);
25550 TREE_VEC_ELT (targs, 0)
25551 = finish_decltype_type (init, id, tf_warning_or_error);
25552 if (type != auto_node)
25554 if (complain & tf_error)
25555 error ("%qT as type rather than plain %<decltype(auto)%>", type);
25556 return error_mark_node;
25559 else
25561 tree parms = build_tree_list (NULL_TREE, type);
25562 tree tparms;
25564 if (flag_concepts)
25565 tparms = extract_autos (type);
25566 else
25568 tparms = make_tree_vec (1);
25569 TREE_VEC_ELT (tparms, 0)
25570 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
25573 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25574 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
25575 DEDUCE_CALL, LOOKUP_NORMAL,
25576 NULL, /*explain_p=*/false);
25577 if (val > 0)
25579 if (processing_template_decl)
25580 /* Try again at instantiation time. */
25581 return type;
25582 if (type && type != error_mark_node
25583 && (complain & tf_error))
25584 /* If type is error_mark_node a diagnostic must have been
25585 emitted by now. Also, having a mention to '<type error>'
25586 in the diagnostic is not really useful to the user. */
25588 if (cfun && auto_node == current_function_auto_return_pattern
25589 && LAMBDA_FUNCTION_P (current_function_decl))
25590 error ("unable to deduce lambda return type from %qE", init);
25591 else
25592 error ("unable to deduce %qT from %qE", type, init);
25593 type_unification_real (tparms, targs, parms, &init, 1, 0,
25594 DEDUCE_CALL, LOOKUP_NORMAL,
25595 NULL, /*explain_p=*/true);
25597 return error_mark_node;
25601 /* Check any placeholder constraints against the deduced type. */
25602 if (flag_concepts && !processing_template_decl)
25603 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (auto_node))
25605 /* Use the deduced type to check the associated constraints. If we
25606 have a partial-concept-id, rebuild the argument list so that
25607 we check using the extra arguments. */
25608 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
25609 tree cargs = CHECK_CONSTR_ARGS (constr);
25610 if (TREE_VEC_LENGTH (cargs) > 1)
25612 cargs = copy_node (cargs);
25613 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
25615 else
25616 cargs = targs;
25617 if (!constraints_satisfied_p (constr, cargs))
25619 if (complain & tf_warning_or_error)
25621 switch (context)
25623 case adc_unspecified:
25624 case adc_unify:
25625 error("placeholder constraints not satisfied");
25626 break;
25627 case adc_variable_type:
25628 case adc_decomp_type:
25629 error ("deduced initializer does not satisfy "
25630 "placeholder constraints");
25631 break;
25632 case adc_return_type:
25633 error ("deduced return type does not satisfy "
25634 "placeholder constraints");
25635 break;
25636 case adc_requirement:
25637 error ("deduced expression type does not satisfy "
25638 "placeholder constraints");
25639 break;
25641 diagnose_constraints (input_location, constr, targs);
25643 return error_mark_node;
25647 if (processing_template_decl && context != adc_unify)
25648 outer_targs = current_template_args ();
25649 targs = add_to_template_args (outer_targs, targs);
25650 return tsubst (type, targs, complain, NULL_TREE);
25653 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
25654 result. */
25656 tree
25657 splice_late_return_type (tree type, tree late_return_type)
25659 if (is_auto (type))
25661 if (late_return_type)
25662 return late_return_type;
25664 tree idx = get_template_parm_index (type);
25665 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
25666 /* In an abbreviated function template we didn't know we were dealing
25667 with a function template when we saw the auto return type, so update
25668 it to have the correct level. */
25669 return make_auto_1 (TYPE_IDENTIFIER (type), true);
25671 return type;
25674 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
25675 'decltype(auto)' or a deduced class template. */
25677 bool
25678 is_auto (const_tree type)
25680 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
25681 && (TYPE_IDENTIFIER (type) == auto_identifier
25682 || TYPE_IDENTIFIER (type) == decltype_auto_identifier
25683 || CLASS_PLACEHOLDER_TEMPLATE (type)))
25684 return true;
25685 else
25686 return false;
25689 /* for_each_template_parm callback for type_uses_auto. */
25692 is_auto_r (tree tp, void */*data*/)
25694 return is_auto (tp);
25697 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
25698 a use of `auto'. Returns NULL_TREE otherwise. */
25700 tree
25701 type_uses_auto (tree type)
25703 if (type == NULL_TREE)
25704 return NULL_TREE;
25705 else if (flag_concepts)
25707 /* The Concepts TS allows multiple autos in one type-specifier; just
25708 return the first one we find, do_auto_deduction will collect all of
25709 them. */
25710 if (uses_template_parms (type))
25711 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
25712 /*visited*/NULL, /*nondeduced*/true);
25713 else
25714 return NULL_TREE;
25716 else
25717 return find_type_usage (type, is_auto);
25720 /* For a given template T, return the vector of typedefs referenced
25721 in T for which access check is needed at T instantiation time.
25722 T is either a FUNCTION_DECL or a RECORD_TYPE.
25723 Those typedefs were added to T by the function
25724 append_type_to_template_for_access_check. */
25726 vec<qualified_typedef_usage_t, va_gc> *
25727 get_types_needing_access_check (tree t)
25729 tree ti;
25730 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
25732 if (!t || t == error_mark_node)
25733 return NULL;
25735 if (!(ti = get_template_info (t)))
25736 return NULL;
25738 if (CLASS_TYPE_P (t)
25739 || TREE_CODE (t) == FUNCTION_DECL)
25741 if (!TI_TEMPLATE (ti))
25742 return NULL;
25744 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
25747 return result;
25750 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
25751 tied to T. That list of typedefs will be access checked at
25752 T instantiation time.
25753 T is either a FUNCTION_DECL or a RECORD_TYPE.
25754 TYPE_DECL is a TYPE_DECL node representing a typedef.
25755 SCOPE is the scope through which TYPE_DECL is accessed.
25756 LOCATION is the location of the usage point of TYPE_DECL.
25758 This function is a subroutine of
25759 append_type_to_template_for_access_check. */
25761 static void
25762 append_type_to_template_for_access_check_1 (tree t,
25763 tree type_decl,
25764 tree scope,
25765 location_t location)
25767 qualified_typedef_usage_t typedef_usage;
25768 tree ti;
25770 if (!t || t == error_mark_node)
25771 return;
25773 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
25774 || CLASS_TYPE_P (t))
25775 && type_decl
25776 && TREE_CODE (type_decl) == TYPE_DECL
25777 && scope);
25779 if (!(ti = get_template_info (t)))
25780 return;
25782 gcc_assert (TI_TEMPLATE (ti));
25784 typedef_usage.typedef_decl = type_decl;
25785 typedef_usage.context = scope;
25786 typedef_usage.locus = location;
25788 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
25791 /* Append TYPE_DECL to the template TEMPL.
25792 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
25793 At TEMPL instanciation time, TYPE_DECL will be checked to see
25794 if it can be accessed through SCOPE.
25795 LOCATION is the location of the usage point of TYPE_DECL.
25797 e.g. consider the following code snippet:
25799 class C
25801 typedef int myint;
25804 template<class U> struct S
25806 C::myint mi; // <-- usage point of the typedef C::myint
25809 S<char> s;
25811 At S<char> instantiation time, we need to check the access of C::myint
25812 In other words, we need to check the access of the myint typedef through
25813 the C scope. For that purpose, this function will add the myint typedef
25814 and the scope C through which its being accessed to a list of typedefs
25815 tied to the template S. That list will be walked at template instantiation
25816 time and access check performed on each typedefs it contains.
25817 Note that this particular code snippet should yield an error because
25818 myint is private to C. */
25820 void
25821 append_type_to_template_for_access_check (tree templ,
25822 tree type_decl,
25823 tree scope,
25824 location_t location)
25826 qualified_typedef_usage_t *iter;
25827 unsigned i;
25829 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
25831 /* Make sure we don't append the type to the template twice. */
25832 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
25833 if (iter->typedef_decl == type_decl && scope == iter->context)
25834 return;
25836 append_type_to_template_for_access_check_1 (templ, type_decl,
25837 scope, location);
25840 /* Convert the generic type parameters in PARM that match the types given in the
25841 range [START_IDX, END_IDX) from the current_template_parms into generic type
25842 packs. */
25844 tree
25845 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
25847 tree current = current_template_parms;
25848 int depth = TMPL_PARMS_DEPTH (current);
25849 current = INNERMOST_TEMPLATE_PARMS (current);
25850 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
25852 for (int i = 0; i < start_idx; ++i)
25853 TREE_VEC_ELT (replacement, i)
25854 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
25856 for (int i = start_idx; i < end_idx; ++i)
25858 /* Create a distinct parameter pack type from the current parm and add it
25859 to the replacement args to tsubst below into the generic function
25860 parameter. */
25862 tree o = TREE_TYPE (TREE_VALUE
25863 (TREE_VEC_ELT (current, i)));
25864 tree t = copy_type (o);
25865 TEMPLATE_TYPE_PARM_INDEX (t)
25866 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
25867 o, 0, 0, tf_none);
25868 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
25869 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
25870 TYPE_MAIN_VARIANT (t) = t;
25871 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
25872 TYPE_CANONICAL (t) = canonical_type_parameter (t);
25873 TREE_VEC_ELT (replacement, i) = t;
25874 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
25877 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
25878 TREE_VEC_ELT (replacement, i)
25879 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
25881 /* If there are more levels then build up the replacement with the outer
25882 template parms. */
25883 if (depth > 1)
25884 replacement = add_to_template_args (template_parms_to_args
25885 (TREE_CHAIN (current_template_parms)),
25886 replacement);
25888 return tsubst (parm, replacement, tf_none, NULL_TREE);
25891 /* Entries in the decl_constraint hash table. */
25892 struct GTY((for_user)) constr_entry
25894 tree decl;
25895 tree ci;
25898 /* Hashing function and equality for constraint entries. */
25899 struct constr_hasher : ggc_ptr_hash<constr_entry>
25901 static hashval_t hash (constr_entry *e)
25903 return (hashval_t)DECL_UID (e->decl);
25906 static bool equal (constr_entry *e1, constr_entry *e2)
25908 return e1->decl == e2->decl;
25912 /* A mapping from declarations to constraint information. Note that
25913 both templates and their underlying declarations are mapped to the
25914 same constraint information.
25916 FIXME: This is defined in pt.c because garbage collection
25917 code is not being generated for constraint.cc. */
25919 static GTY (()) hash_table<constr_hasher> *decl_constraints;
25921 /* Returns the template constraints of declaration T. If T is not
25922 constrained, return NULL_TREE. Note that T must be non-null. */
25924 tree
25925 get_constraints (tree t)
25927 if (!flag_concepts)
25928 return NULL_TREE;
25930 gcc_assert (DECL_P (t));
25931 if (TREE_CODE (t) == TEMPLATE_DECL)
25932 t = DECL_TEMPLATE_RESULT (t);
25933 constr_entry elt = { t, NULL_TREE };
25934 constr_entry* found = decl_constraints->find (&elt);
25935 if (found)
25936 return found->ci;
25937 else
25938 return NULL_TREE;
25941 /* Associate the given constraint information CI with the declaration
25942 T. If T is a template, then the constraints are associated with
25943 its underlying declaration. Don't build associations if CI is
25944 NULL_TREE. */
25946 void
25947 set_constraints (tree t, tree ci)
25949 if (!ci)
25950 return;
25951 gcc_assert (t && flag_concepts);
25952 if (TREE_CODE (t) == TEMPLATE_DECL)
25953 t = DECL_TEMPLATE_RESULT (t);
25954 gcc_assert (!get_constraints (t));
25955 constr_entry elt = {t, ci};
25956 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
25957 constr_entry* entry = ggc_alloc<constr_entry> ();
25958 *entry = elt;
25959 *slot = entry;
25962 /* Remove the associated constraints of the declaration T. */
25964 void
25965 remove_constraints (tree t)
25967 gcc_assert (DECL_P (t));
25968 if (TREE_CODE (t) == TEMPLATE_DECL)
25969 t = DECL_TEMPLATE_RESULT (t);
25971 constr_entry elt = {t, NULL_TREE};
25972 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
25973 if (slot)
25974 decl_constraints->clear_slot (slot);
25977 /* Memoized satisfaction results for declarations. This
25978 maps the pair (constraint_info, arguments) to the result computed
25979 by constraints_satisfied_p. */
25981 struct GTY((for_user)) constraint_sat_entry
25983 tree ci;
25984 tree args;
25985 tree result;
25988 /* Hashing function and equality for constraint entries. */
25990 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
25992 static hashval_t hash (constraint_sat_entry *e)
25994 hashval_t val = iterative_hash_object(e->ci, 0);
25995 return iterative_hash_template_arg (e->args, val);
25998 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
26000 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
26004 /* Memoized satisfaction results for concept checks. */
26006 struct GTY((for_user)) concept_spec_entry
26008 tree tmpl;
26009 tree args;
26010 tree result;
26013 /* Hashing function and equality for constraint entries. */
26015 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
26017 static hashval_t hash (concept_spec_entry *e)
26019 return hash_tmpl_and_args (e->tmpl, e->args);
26022 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
26024 ++comparing_specializations;
26025 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
26026 --comparing_specializations;
26027 return eq;
26031 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
26032 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
26034 /* Search for a memoized satisfaction result. Returns one of the
26035 truth value nodes if previously memoized, or NULL_TREE otherwise. */
26037 tree
26038 lookup_constraint_satisfaction (tree ci, tree args)
26040 constraint_sat_entry elt = { ci, args, NULL_TREE };
26041 constraint_sat_entry* found = constraint_memos->find (&elt);
26042 if (found)
26043 return found->result;
26044 else
26045 return NULL_TREE;
26048 /* Memoize the result of a satisfication test. Returns the saved result. */
26050 tree
26051 memoize_constraint_satisfaction (tree ci, tree args, tree result)
26053 constraint_sat_entry elt = {ci, args, result};
26054 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
26055 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
26056 *entry = elt;
26057 *slot = entry;
26058 return result;
26061 /* Search for a memoized satisfaction result for a concept. */
26063 tree
26064 lookup_concept_satisfaction (tree tmpl, tree args)
26066 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26067 concept_spec_entry* found = concept_memos->find (&elt);
26068 if (found)
26069 return found->result;
26070 else
26071 return NULL_TREE;
26074 /* Memoize the result of a concept check. Returns the saved result. */
26076 tree
26077 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
26079 concept_spec_entry elt = {tmpl, args, result};
26080 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
26081 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26082 *entry = elt;
26083 *slot = entry;
26084 return result;
26087 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
26089 /* Returns a prior concept specialization. This returns the substituted
26090 and normalized constraints defined by the concept. */
26092 tree
26093 get_concept_expansion (tree tmpl, tree args)
26095 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26096 concept_spec_entry* found = concept_expansions->find (&elt);
26097 if (found)
26098 return found->result;
26099 else
26100 return NULL_TREE;
26103 /* Save a concept expansion for later. */
26105 tree
26106 save_concept_expansion (tree tmpl, tree args, tree def)
26108 concept_spec_entry elt = {tmpl, args, def};
26109 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
26110 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26111 *entry = elt;
26112 *slot = entry;
26113 return def;
26116 static hashval_t
26117 hash_subsumption_args (tree t1, tree t2)
26119 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
26120 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
26121 int val = 0;
26122 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
26123 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
26124 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
26125 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
26126 return val;
26129 /* Compare the constraints of two subsumption entries. The LEFT1 and
26130 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
26131 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
26133 static bool
26134 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
26136 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
26137 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
26138 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
26139 CHECK_CONSTR_ARGS (right1)))
26140 return comp_template_args (CHECK_CONSTR_ARGS (left2),
26141 CHECK_CONSTR_ARGS (right2));
26142 return false;
26145 /* Key/value pair for learning and memoizing subsumption results. This
26146 associates a pair of check constraints (including arguments) with
26147 a boolean value indicating the result. */
26149 struct GTY((for_user)) subsumption_entry
26151 tree t1;
26152 tree t2;
26153 bool result;
26156 /* Hashing function and equality for constraint entries. */
26158 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
26160 static hashval_t hash (subsumption_entry *e)
26162 return hash_subsumption_args (e->t1, e->t2);
26165 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
26167 ++comparing_specializations;
26168 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
26169 --comparing_specializations;
26170 return eq;
26174 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
26176 /* Search for a previously cached subsumption result. */
26178 bool*
26179 lookup_subsumption_result (tree t1, tree t2)
26181 subsumption_entry elt = { t1, t2, false };
26182 subsumption_entry* found = subsumption_table->find (&elt);
26183 if (found)
26184 return &found->result;
26185 else
26186 return 0;
26189 /* Save a subsumption result. */
26191 bool
26192 save_subsumption_result (tree t1, tree t2, bool result)
26194 subsumption_entry elt = {t1, t2, result};
26195 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
26196 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
26197 *entry = elt;
26198 *slot = entry;
26199 return result;
26202 /* Set up the hash table for constraint association. */
26204 void
26205 init_constraint_processing (void)
26207 if (!flag_concepts)
26208 return;
26210 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
26211 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
26212 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
26213 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
26214 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
26217 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
26218 0..N-1. */
26220 void
26221 declare_integer_pack (void)
26223 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
26224 build_function_type_list (integer_type_node,
26225 integer_type_node,
26226 NULL_TREE),
26227 NULL_TREE, ECF_CONST);
26228 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
26229 DECL_BUILT_IN_CLASS (ipfn) = BUILT_IN_FRONTEND;
26232 /* Set up the hash tables for template instantiations. */
26234 void
26235 init_template_processing (void)
26237 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
26238 type_specializations = hash_table<spec_hasher>::create_ggc (37);
26240 if (cxx_dialect >= cxx11)
26241 declare_integer_pack ();
26244 /* Print stats about the template hash tables for -fstats. */
26246 void
26247 print_template_statistics (void)
26249 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
26250 "%f collisions\n", (long) decl_specializations->size (),
26251 (long) decl_specializations->elements (),
26252 decl_specializations->collisions ());
26253 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
26254 "%f collisions\n", (long) type_specializations->size (),
26255 (long) type_specializations->elements (),
26256 type_specializations->collisions ());
26259 #include "gt-cp-pt.h"