* pt.c (coerce_template_parms): Fix indentation.
[official-gcc.git] / gcc / cp / pt.c
blobe5238ad2fa5ced2d88b69077e6b7616431e04966
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 (NEW_DELETE_OPNAME_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 (processing_template_decl
6589 && !require_potential_constant_expression (expr))
6590 return NULL_TREE;
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 tree nparmvec = parmvec;
7174 if (flag_new_ttp)
7176 /* In keeping with P0522R0, adjust P's template arguments
7177 to apply to A's template; then flatten it again. */
7178 nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7179 nparmvec = expand_template_argument_pack (nparmvec);
7182 if (unify (tparms, targs, nparmvec, argvec,
7183 UNIFY_ALLOW_NONE, explain_p))
7184 return 1;
7186 /* If the P0522 adjustment eliminated a pack expansion, deduce
7187 empty packs. */
7188 if (flag_new_ttp
7189 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7190 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7191 DEDUCE_EXACT, /*sub*/true, explain_p))
7192 return 1;
7194 return 0;
7197 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7198 template template parameters. Both PARM_PARMS and ARG_PARMS are
7199 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7200 or PARM_DECL.
7202 Consider the example:
7203 template <class T> class A;
7204 template<template <class U> class TT> class B;
7206 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7207 the parameters to A, and OUTER_ARGS contains A. */
7209 static int
7210 coerce_template_template_parms (tree parm_parms,
7211 tree arg_parms,
7212 tsubst_flags_t complain,
7213 tree in_decl,
7214 tree outer_args)
7216 int nparms, nargs, i;
7217 tree parm, arg;
7218 int variadic_p = 0;
7220 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7221 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7223 nparms = TREE_VEC_LENGTH (parm_parms);
7224 nargs = TREE_VEC_LENGTH (arg_parms);
7226 if (flag_new_ttp)
7228 /* P0522R0: A template template-parameter P is at least as specialized as
7229 a template template-argument A if, given the following rewrite to two
7230 function templates, the function template corresponding to P is at
7231 least as specialized as the function template corresponding to A
7232 according to the partial ordering rules for function templates
7233 ([temp.func.order]). Given an invented class template X with the
7234 template parameter list of A (including default arguments):
7236 * Each of the two function templates has the same template parameters,
7237 respectively, as P or A.
7239 * Each function template has a single function parameter whose type is
7240 a specialization of X with template arguments corresponding to the
7241 template parameters from the respective function template where, for
7242 each template parameter PP in the template parameter list of the
7243 function template, a corresponding template argument AA is formed. If
7244 PP declares a parameter pack, then AA is the pack expansion
7245 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7247 If the rewrite produces an invalid type, then P is not at least as
7248 specialized as A. */
7250 /* So coerce P's args to apply to A's parms, and then deduce between A's
7251 args and the converted args. If that succeeds, A is at least as
7252 specialized as P, so they match.*/
7253 tree pargs = template_parms_level_to_args (parm_parms);
7254 ++processing_template_decl;
7255 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7256 /*require_all*/true, /*use_default*/true);
7257 --processing_template_decl;
7258 if (pargs != error_mark_node)
7260 tree targs = make_tree_vec (nargs);
7261 tree aargs = template_parms_level_to_args (arg_parms);
7262 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7263 /*explain*/false))
7264 return 1;
7268 /* Determine whether we have a parameter pack at the end of the
7269 template template parameter's template parameter list. */
7270 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7272 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7274 if (error_operand_p (parm))
7275 return 0;
7277 switch (TREE_CODE (parm))
7279 case TEMPLATE_DECL:
7280 case TYPE_DECL:
7281 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7282 variadic_p = 1;
7283 break;
7285 case PARM_DECL:
7286 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7287 variadic_p = 1;
7288 break;
7290 default:
7291 gcc_unreachable ();
7295 if (nargs != nparms
7296 && !(variadic_p && nargs >= nparms - 1))
7297 return 0;
7299 /* Check all of the template parameters except the parameter pack at
7300 the end (if any). */
7301 for (i = 0; i < nparms - variadic_p; ++i)
7303 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7304 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7305 continue;
7307 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7308 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7310 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7311 outer_args))
7312 return 0;
7316 if (variadic_p)
7318 /* Check each of the template parameters in the template
7319 argument against the template parameter pack at the end of
7320 the template template parameter. */
7321 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7322 return 0;
7324 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7326 for (; i < nargs; ++i)
7328 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7329 continue;
7331 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7333 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7334 outer_args))
7335 return 0;
7339 return 1;
7342 /* Verifies that the deduced template arguments (in TARGS) for the
7343 template template parameters (in TPARMS) represent valid bindings,
7344 by comparing the template parameter list of each template argument
7345 to the template parameter list of its corresponding template
7346 template parameter, in accordance with DR150. This
7347 routine can only be called after all template arguments have been
7348 deduced. It will return TRUE if all of the template template
7349 parameter bindings are okay, FALSE otherwise. */
7350 bool
7351 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7353 int i, ntparms = TREE_VEC_LENGTH (tparms);
7354 bool ret = true;
7356 /* We're dealing with template parms in this process. */
7357 ++processing_template_decl;
7359 targs = INNERMOST_TEMPLATE_ARGS (targs);
7361 for (i = 0; i < ntparms; ++i)
7363 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7364 tree targ = TREE_VEC_ELT (targs, i);
7366 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7368 tree packed_args = NULL_TREE;
7369 int idx, len = 1;
7371 if (ARGUMENT_PACK_P (targ))
7373 /* Look inside the argument pack. */
7374 packed_args = ARGUMENT_PACK_ARGS (targ);
7375 len = TREE_VEC_LENGTH (packed_args);
7378 for (idx = 0; idx < len; ++idx)
7380 tree targ_parms = NULL_TREE;
7382 if (packed_args)
7383 /* Extract the next argument from the argument
7384 pack. */
7385 targ = TREE_VEC_ELT (packed_args, idx);
7387 if (PACK_EXPANSION_P (targ))
7388 /* Look at the pattern of the pack expansion. */
7389 targ = PACK_EXPANSION_PATTERN (targ);
7391 /* Extract the template parameters from the template
7392 argument. */
7393 if (TREE_CODE (targ) == TEMPLATE_DECL)
7394 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7395 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7396 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7398 /* Verify that we can coerce the template template
7399 parameters from the template argument to the template
7400 parameter. This requires an exact match. */
7401 if (targ_parms
7402 && !coerce_template_template_parms
7403 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7404 targ_parms,
7405 tf_none,
7406 tparm,
7407 targs))
7409 ret = false;
7410 goto out;
7416 out:
7418 --processing_template_decl;
7419 return ret;
7422 /* Since type attributes aren't mangled, we need to strip them from
7423 template type arguments. */
7425 static tree
7426 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7428 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7429 return arg;
7430 bool removed_attributes = false;
7431 tree canon = strip_typedefs (arg, &removed_attributes);
7432 if (removed_attributes
7433 && (complain & tf_warning))
7434 warning (OPT_Wignored_attributes,
7435 "ignoring attributes on template argument %qT", arg);
7436 return canon;
7439 /* And from inside dependent non-type arguments like sizeof(Type). */
7441 static tree
7442 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7444 if (!arg || arg == error_mark_node)
7445 return arg;
7446 bool removed_attributes = false;
7447 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7448 if (removed_attributes
7449 && (complain & tf_warning))
7450 warning (OPT_Wignored_attributes,
7451 "ignoring attributes in template argument %qE", arg);
7452 return canon;
7455 // A template declaration can be substituted for a constrained
7456 // template template parameter only when the argument is more
7457 // constrained than the parameter.
7458 static bool
7459 is_compatible_template_arg (tree parm, tree arg)
7461 tree parm_cons = get_constraints (parm);
7463 /* For now, allow constrained template template arguments
7464 and unconstrained template template parameters. */
7465 if (parm_cons == NULL_TREE)
7466 return true;
7468 tree arg_cons = get_constraints (arg);
7470 // If the template parameter is constrained, we need to rewrite its
7471 // constraints in terms of the ARG's template parameters. This ensures
7472 // that all of the template parameter types will have the same depth.
7474 // Note that this is only valid when coerce_template_template_parm is
7475 // true for the innermost template parameters of PARM and ARG. In other
7476 // words, because coercion is successful, this conversion will be valid.
7477 if (parm_cons)
7479 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7480 parm_cons = tsubst_constraint_info (parm_cons,
7481 INNERMOST_TEMPLATE_ARGS (args),
7482 tf_none, NULL_TREE);
7483 if (parm_cons == error_mark_node)
7484 return false;
7487 return subsumes (parm_cons, arg_cons);
7490 // Convert a placeholder argument into a binding to the original
7491 // parameter. The original parameter is saved as the TREE_TYPE of
7492 // ARG.
7493 static inline tree
7494 convert_wildcard_argument (tree parm, tree arg)
7496 TREE_TYPE (arg) = parm;
7497 return arg;
7500 /* Convert the indicated template ARG as necessary to match the
7501 indicated template PARM. Returns the converted ARG, or
7502 error_mark_node if the conversion was unsuccessful. Error and
7503 warning messages are issued under control of COMPLAIN. This
7504 conversion is for the Ith parameter in the parameter list. ARGS is
7505 the full set of template arguments deduced so far. */
7507 static tree
7508 convert_template_argument (tree parm,
7509 tree arg,
7510 tree args,
7511 tsubst_flags_t complain,
7512 int i,
7513 tree in_decl)
7515 tree orig_arg;
7516 tree val;
7517 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7519 if (parm == error_mark_node)
7520 return error_mark_node;
7522 /* Trivially convert placeholders. */
7523 if (TREE_CODE (arg) == WILDCARD_DECL)
7524 return convert_wildcard_argument (parm, arg);
7526 if (arg == any_targ_node)
7527 return arg;
7529 if (TREE_CODE (arg) == TREE_LIST
7530 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7532 /* The template argument was the name of some
7533 member function. That's usually
7534 invalid, but static members are OK. In any
7535 case, grab the underlying fields/functions
7536 and issue an error later if required. */
7537 orig_arg = TREE_VALUE (arg);
7538 TREE_TYPE (arg) = unknown_type_node;
7541 orig_arg = arg;
7543 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7544 requires_type = (TREE_CODE (parm) == TYPE_DECL
7545 || requires_tmpl_type);
7547 /* When determining whether an argument pack expansion is a template,
7548 look at the pattern. */
7549 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7550 arg = PACK_EXPANSION_PATTERN (arg);
7552 /* Deal with an injected-class-name used as a template template arg. */
7553 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7555 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7556 if (TREE_CODE (t) == TEMPLATE_DECL)
7558 if (cxx_dialect >= cxx11)
7559 /* OK under DR 1004. */;
7560 else if (complain & tf_warning_or_error)
7561 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7562 " used as template template argument", TYPE_NAME (arg));
7563 else if (flag_pedantic_errors)
7564 t = arg;
7566 arg = t;
7570 is_tmpl_type =
7571 ((TREE_CODE (arg) == TEMPLATE_DECL
7572 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7573 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7574 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7575 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7577 if (is_tmpl_type
7578 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7579 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7580 arg = TYPE_STUB_DECL (arg);
7582 is_type = TYPE_P (arg) || is_tmpl_type;
7584 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7585 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7587 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7589 if (complain & tf_error)
7590 error ("invalid use of destructor %qE as a type", orig_arg);
7591 return error_mark_node;
7594 permerror (input_location,
7595 "to refer to a type member of a template parameter, "
7596 "use %<typename %E%>", orig_arg);
7598 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7599 TREE_OPERAND (arg, 1),
7600 typename_type,
7601 complain);
7602 arg = orig_arg;
7603 is_type = 1;
7605 if (is_type != requires_type)
7607 if (in_decl)
7609 if (complain & tf_error)
7611 error ("type/value mismatch at argument %d in template "
7612 "parameter list for %qD",
7613 i + 1, in_decl);
7614 if (is_type)
7615 inform (input_location,
7616 " expected a constant of type %qT, got %qT",
7617 TREE_TYPE (parm),
7618 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7619 else if (requires_tmpl_type)
7620 inform (input_location,
7621 " expected a class template, got %qE", orig_arg);
7622 else
7623 inform (input_location,
7624 " expected a type, got %qE", orig_arg);
7627 return error_mark_node;
7629 if (is_tmpl_type ^ requires_tmpl_type)
7631 if (in_decl && (complain & tf_error))
7633 error ("type/value mismatch at argument %d in template "
7634 "parameter list for %qD",
7635 i + 1, in_decl);
7636 if (is_tmpl_type)
7637 inform (input_location,
7638 " expected a type, got %qT", DECL_NAME (arg));
7639 else
7640 inform (input_location,
7641 " expected a class template, got %qT", orig_arg);
7643 return error_mark_node;
7646 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7647 /* We already did the appropriate conversion when packing args. */
7648 val = orig_arg;
7649 else if (is_type)
7651 if (requires_tmpl_type)
7653 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7654 /* The number of argument required is not known yet.
7655 Just accept it for now. */
7656 val = TREE_TYPE (arg);
7657 else
7659 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7660 tree argparm;
7662 /* Strip alias templates that are equivalent to another
7663 template. */
7664 arg = get_underlying_template (arg);
7665 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7667 if (coerce_template_template_parms (parmparm, argparm,
7668 complain, in_decl,
7669 args))
7671 val = arg;
7673 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7674 TEMPLATE_DECL. */
7675 if (val != error_mark_node)
7677 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
7678 val = TREE_TYPE (val);
7679 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
7680 val = make_pack_expansion (val);
7683 else
7685 if (in_decl && (complain & tf_error))
7687 error ("type/value mismatch at argument %d in "
7688 "template parameter list for %qD",
7689 i + 1, in_decl);
7690 inform (input_location,
7691 " expected a template of type %qD, got %qT",
7692 parm, orig_arg);
7695 val = error_mark_node;
7698 // Check that the constraints are compatible before allowing the
7699 // substitution.
7700 if (val != error_mark_node)
7701 if (!is_compatible_template_arg (parm, arg))
7703 if (in_decl && (complain & tf_error))
7705 error ("constraint mismatch at argument %d in "
7706 "template parameter list for %qD",
7707 i + 1, in_decl);
7708 inform (input_location, " expected %qD but got %qD",
7709 parm, arg);
7711 val = error_mark_node;
7715 else
7716 val = orig_arg;
7717 /* We only form one instance of each template specialization.
7718 Therefore, if we use a non-canonical variant (i.e., a
7719 typedef), any future messages referring to the type will use
7720 the typedef, which is confusing if those future uses do not
7721 themselves also use the typedef. */
7722 if (TYPE_P (val))
7723 val = canonicalize_type_argument (val, complain);
7725 else
7727 tree t = TREE_TYPE (parm);
7729 if (tree a = type_uses_auto (t))
7731 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
7732 if (t == error_mark_node)
7733 return error_mark_node;
7735 else
7736 t = tsubst (t, args, complain, in_decl);
7738 if (invalid_nontype_parm_type_p (t, complain))
7739 return error_mark_node;
7741 if (!type_dependent_expression_p (orig_arg)
7742 && !uses_template_parms (t))
7743 /* We used to call digest_init here. However, digest_init
7744 will report errors, which we don't want when complain
7745 is zero. More importantly, digest_init will try too
7746 hard to convert things: for example, `0' should not be
7747 converted to pointer type at this point according to
7748 the standard. Accepting this is not merely an
7749 extension, since deciding whether or not these
7750 conversions can occur is part of determining which
7751 function template to call, or whether a given explicit
7752 argument specification is valid. */
7753 val = convert_nontype_argument (t, orig_arg, complain);
7754 else
7755 val = canonicalize_expr_argument (orig_arg, complain);
7757 if (val == NULL_TREE)
7758 val = error_mark_node;
7759 else if (val == error_mark_node && (complain & tf_error))
7760 error ("could not convert template argument %qE from %qT to %qT",
7761 orig_arg, TREE_TYPE (orig_arg), t);
7763 if (INDIRECT_REF_P (val))
7765 /* Reject template arguments that are references to built-in
7766 functions with no library fallbacks. */
7767 const_tree inner = TREE_OPERAND (val, 0);
7768 const_tree innertype = TREE_TYPE (inner);
7769 if (innertype
7770 && TREE_CODE (innertype) == REFERENCE_TYPE
7771 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
7772 && 0 < TREE_OPERAND_LENGTH (inner)
7773 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
7774 return error_mark_node;
7777 if (TREE_CODE (val) == SCOPE_REF)
7779 /* Strip typedefs from the SCOPE_REF. */
7780 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
7781 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
7782 complain);
7783 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
7784 QUALIFIED_NAME_IS_TEMPLATE (val));
7788 return val;
7791 /* Coerces the remaining template arguments in INNER_ARGS (from
7792 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
7793 Returns the coerced argument pack. PARM_IDX is the position of this
7794 parameter in the template parameter list. ARGS is the original
7795 template argument list. */
7796 static tree
7797 coerce_template_parameter_pack (tree parms,
7798 int parm_idx,
7799 tree args,
7800 tree inner_args,
7801 int arg_idx,
7802 tree new_args,
7803 int* lost,
7804 tree in_decl,
7805 tsubst_flags_t complain)
7807 tree parm = TREE_VEC_ELT (parms, parm_idx);
7808 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7809 tree packed_args;
7810 tree argument_pack;
7811 tree packed_parms = NULL_TREE;
7813 if (arg_idx > nargs)
7814 arg_idx = nargs;
7816 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
7818 /* When the template parameter is a non-type template parameter pack
7819 or template template parameter pack whose type or template
7820 parameters use parameter packs, we know exactly how many arguments
7821 we are looking for. Build a vector of the instantiated decls for
7822 these template parameters in PACKED_PARMS. */
7823 /* We can't use make_pack_expansion here because it would interpret a
7824 _DECL as a use rather than a declaration. */
7825 tree decl = TREE_VALUE (parm);
7826 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
7827 SET_PACK_EXPANSION_PATTERN (exp, decl);
7828 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
7829 SET_TYPE_STRUCTURAL_EQUALITY (exp);
7831 TREE_VEC_LENGTH (args)--;
7832 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
7833 TREE_VEC_LENGTH (args)++;
7835 if (packed_parms == error_mark_node)
7836 return error_mark_node;
7838 /* If we're doing a partial instantiation of a member template,
7839 verify that all of the types used for the non-type
7840 template parameter pack are, in fact, valid for non-type
7841 template parameters. */
7842 if (arg_idx < nargs
7843 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
7845 int j, len = TREE_VEC_LENGTH (packed_parms);
7846 for (j = 0; j < len; ++j)
7848 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
7849 if (invalid_nontype_parm_type_p (t, complain))
7850 return error_mark_node;
7852 /* We don't know how many args we have yet, just
7853 use the unconverted ones for now. */
7854 return NULL_TREE;
7857 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
7859 /* Check if we have a placeholder pack, which indicates we're
7860 in the context of a introduction list. In that case we want
7861 to match this pack to the single placeholder. */
7862 else if (arg_idx < nargs
7863 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
7864 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
7866 nargs = arg_idx + 1;
7867 packed_args = make_tree_vec (1);
7869 else
7870 packed_args = make_tree_vec (nargs - arg_idx);
7872 /* Convert the remaining arguments, which will be a part of the
7873 parameter pack "parm". */
7874 int first_pack_arg = arg_idx;
7875 for (; arg_idx < nargs; ++arg_idx)
7877 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
7878 tree actual_parm = TREE_VALUE (parm);
7879 int pack_idx = arg_idx - first_pack_arg;
7881 if (packed_parms)
7883 /* Once we've packed as many args as we have types, stop. */
7884 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
7885 break;
7886 else if (PACK_EXPANSION_P (arg))
7887 /* We don't know how many args we have yet, just
7888 use the unconverted ones for now. */
7889 return NULL_TREE;
7890 else
7891 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
7894 if (arg == error_mark_node)
7896 if (complain & tf_error)
7897 error ("template argument %d is invalid", arg_idx + 1);
7899 else
7900 arg = convert_template_argument (actual_parm,
7901 arg, new_args, complain, parm_idx,
7902 in_decl);
7903 if (arg == error_mark_node)
7904 (*lost)++;
7905 TREE_VEC_ELT (packed_args, pack_idx) = arg;
7908 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
7909 && TREE_VEC_LENGTH (packed_args) > 0)
7911 if (complain & tf_error)
7912 error ("wrong number of template arguments (%d, should be %d)",
7913 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
7914 return error_mark_node;
7917 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
7918 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
7919 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
7920 else
7922 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
7923 TREE_CONSTANT (argument_pack) = 1;
7926 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
7927 if (CHECKING_P)
7928 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
7929 TREE_VEC_LENGTH (packed_args));
7930 return argument_pack;
7933 /* Returns the number of pack expansions in the template argument vector
7934 ARGS. */
7936 static int
7937 pack_expansion_args_count (tree args)
7939 int i;
7940 int count = 0;
7941 if (args)
7942 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
7944 tree elt = TREE_VEC_ELT (args, i);
7945 if (elt && PACK_EXPANSION_P (elt))
7946 ++count;
7948 return count;
7951 /* Convert all template arguments to their appropriate types, and
7952 return a vector containing the innermost resulting template
7953 arguments. If any error occurs, return error_mark_node. Error and
7954 warning messages are issued under control of COMPLAIN.
7956 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
7957 for arguments not specified in ARGS. Otherwise, if
7958 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
7959 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
7960 USE_DEFAULT_ARGS is false, then all arguments must be specified in
7961 ARGS. */
7963 static tree
7964 coerce_template_parms (tree parms,
7965 tree args,
7966 tree in_decl,
7967 tsubst_flags_t complain,
7968 bool require_all_args,
7969 bool use_default_args)
7971 int nparms, nargs, parm_idx, arg_idx, lost = 0;
7972 tree orig_inner_args;
7973 tree inner_args;
7974 tree new_args;
7975 tree new_inner_args;
7976 int saved_unevaluated_operand;
7977 int saved_inhibit_evaluation_warnings;
7979 /* When used as a boolean value, indicates whether this is a
7980 variadic template parameter list. Since it's an int, we can also
7981 subtract it from nparms to get the number of non-variadic
7982 parameters. */
7983 int variadic_p = 0;
7984 int variadic_args_p = 0;
7985 int post_variadic_parms = 0;
7987 /* Likewise for parameters with default arguments. */
7988 int default_p = 0;
7990 if (args == error_mark_node)
7991 return error_mark_node;
7993 nparms = TREE_VEC_LENGTH (parms);
7995 /* Determine if there are any parameter packs or default arguments. */
7996 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
7998 tree parm = TREE_VEC_ELT (parms, parm_idx);
7999 if (variadic_p)
8000 ++post_variadic_parms;
8001 if (template_parameter_pack_p (TREE_VALUE (parm)))
8002 ++variadic_p;
8003 if (TREE_PURPOSE (parm))
8004 ++default_p;
8007 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8008 /* If there are no parameters that follow a parameter pack, we need to
8009 expand any argument packs so that we can deduce a parameter pack from
8010 some non-packed args followed by an argument pack, as in variadic85.C.
8011 If there are such parameters, we need to leave argument packs intact
8012 so the arguments are assigned properly. This can happen when dealing
8013 with a nested class inside a partial specialization of a class
8014 template, as in variadic92.C, or when deducing a template parameter pack
8015 from a sub-declarator, as in variadic114.C. */
8016 if (!post_variadic_parms)
8017 inner_args = expand_template_argument_pack (inner_args);
8019 /* Count any pack expansion args. */
8020 variadic_args_p = pack_expansion_args_count (inner_args);
8022 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8023 if ((nargs - variadic_args_p > nparms && !variadic_p)
8024 || (nargs < nparms - variadic_p
8025 && require_all_args
8026 && !variadic_args_p
8027 && (!use_default_args
8028 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8029 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8031 if (complain & tf_error)
8033 if (variadic_p || default_p)
8035 nparms -= variadic_p + default_p;
8036 error ("wrong number of template arguments "
8037 "(%d, should be at least %d)", nargs, nparms);
8039 else
8040 error ("wrong number of template arguments "
8041 "(%d, should be %d)", nargs, nparms);
8043 if (in_decl)
8044 inform (DECL_SOURCE_LOCATION (in_decl),
8045 "provided for %qD", in_decl);
8048 return error_mark_node;
8050 /* We can't pass a pack expansion to a non-pack parameter of an alias
8051 template (DR 1430). */
8052 else if (in_decl
8053 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8054 || concept_template_p (in_decl))
8055 && variadic_args_p
8056 && nargs - variadic_args_p < nparms - variadic_p)
8058 if (complain & tf_error)
8060 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8062 tree arg = TREE_VEC_ELT (inner_args, i);
8063 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8065 if (PACK_EXPANSION_P (arg)
8066 && !template_parameter_pack_p (parm))
8068 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8069 error_at (location_of (arg),
8070 "pack expansion argument for non-pack parameter "
8071 "%qD of alias template %qD", parm, in_decl);
8072 else
8073 error_at (location_of (arg),
8074 "pack expansion argument for non-pack parameter "
8075 "%qD of concept %qD", parm, in_decl);
8076 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8077 goto found;
8080 gcc_unreachable ();
8081 found:;
8083 return error_mark_node;
8086 /* We need to evaluate the template arguments, even though this
8087 template-id may be nested within a "sizeof". */
8088 saved_unevaluated_operand = cp_unevaluated_operand;
8089 cp_unevaluated_operand = 0;
8090 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8091 c_inhibit_evaluation_warnings = 0;
8092 new_inner_args = make_tree_vec (nparms);
8093 new_args = add_outermost_template_args (args, new_inner_args);
8094 int pack_adjust = 0;
8095 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8097 tree arg;
8098 tree parm;
8100 /* Get the Ith template parameter. */
8101 parm = TREE_VEC_ELT (parms, parm_idx);
8103 if (parm == error_mark_node)
8105 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8106 continue;
8109 /* Calculate the next argument. */
8110 if (arg_idx < nargs)
8111 arg = TREE_VEC_ELT (inner_args, arg_idx);
8112 else
8113 arg = NULL_TREE;
8115 if (template_parameter_pack_p (TREE_VALUE (parm))
8116 && !(arg && ARGUMENT_PACK_P (arg)))
8118 /* Some arguments will be placed in the
8119 template parameter pack PARM. */
8120 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8121 inner_args, arg_idx,
8122 new_args, &lost,
8123 in_decl, complain);
8125 if (arg == NULL_TREE)
8127 /* We don't know how many args we have yet, just use the
8128 unconverted (and still packed) ones for now. */
8129 new_inner_args = orig_inner_args;
8130 arg_idx = nargs;
8131 break;
8134 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8136 /* Store this argument. */
8137 if (arg == error_mark_node)
8139 lost++;
8140 /* We are done with all of the arguments. */
8141 arg_idx = nargs;
8143 else
8145 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8146 arg_idx += pack_adjust;
8149 continue;
8151 else if (arg)
8153 if (PACK_EXPANSION_P (arg))
8155 /* "If every valid specialization of a variadic template
8156 requires an empty template parameter pack, the template is
8157 ill-formed, no diagnostic required." So check that the
8158 pattern works with this parameter. */
8159 tree pattern = PACK_EXPANSION_PATTERN (arg);
8160 tree conv = convert_template_argument (TREE_VALUE (parm),
8161 pattern, new_args,
8162 complain, parm_idx,
8163 in_decl);
8164 if (conv == error_mark_node)
8166 if (complain & tf_error)
8167 inform (input_location, "so any instantiation with a "
8168 "non-empty parameter pack would be ill-formed");
8169 ++lost;
8171 else if (TYPE_P (conv) && !TYPE_P (pattern))
8172 /* Recover from missing typename. */
8173 TREE_VEC_ELT (inner_args, arg_idx)
8174 = make_pack_expansion (conv);
8176 /* We don't know how many args we have yet, just
8177 use the unconverted ones for now. */
8178 new_inner_args = inner_args;
8179 arg_idx = nargs;
8180 break;
8183 else if (require_all_args)
8185 /* There must be a default arg in this case. */
8186 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8187 complain, in_decl);
8188 /* The position of the first default template argument,
8189 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8190 Record that. */
8191 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8192 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8193 arg_idx - pack_adjust);
8195 else
8196 break;
8198 if (arg == error_mark_node)
8200 if (complain & tf_error)
8201 error ("template argument %d is invalid", arg_idx + 1);
8203 else if (!arg)
8204 /* This only occurs if there was an error in the template
8205 parameter list itself (which we would already have
8206 reported) that we are trying to recover from, e.g., a class
8207 template with a parameter list such as
8208 template<typename..., typename>. */
8209 ++lost;
8210 else
8211 arg = convert_template_argument (TREE_VALUE (parm),
8212 arg, new_args, complain,
8213 parm_idx, in_decl);
8215 if (arg == error_mark_node)
8216 lost++;
8217 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8219 cp_unevaluated_operand = saved_unevaluated_operand;
8220 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8222 if (variadic_p && arg_idx < nargs)
8224 if (complain & tf_error)
8226 error ("wrong number of template arguments "
8227 "(%d, should be %d)", nargs, arg_idx);
8228 if (in_decl)
8229 error ("provided for %q+D", in_decl);
8231 return error_mark_node;
8234 if (lost)
8235 return error_mark_node;
8237 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8238 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8239 TREE_VEC_LENGTH (new_inner_args));
8241 return new_inner_args;
8244 /* Convert all template arguments to their appropriate types, and
8245 return a vector containing the innermost resulting template
8246 arguments. If any error occurs, return error_mark_node. Error and
8247 warning messages are not issued.
8249 Note that no function argument deduction is performed, and default
8250 arguments are used to fill in unspecified arguments. */
8251 tree
8252 coerce_template_parms (tree parms, tree args, tree in_decl)
8254 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8257 /* Convert all template arguments to their appropriate type, and
8258 instantiate default arguments as needed. This returns a vector
8259 containing the innermost resulting template arguments, or
8260 error_mark_node if unsuccessful. */
8261 tree
8262 coerce_template_parms (tree parms, tree args, tree in_decl,
8263 tsubst_flags_t complain)
8265 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8268 /* Like coerce_template_parms. If PARMS represents all template
8269 parameters levels, this function returns a vector of vectors
8270 representing all the resulting argument levels. Note that in this
8271 case, only the innermost arguments are coerced because the
8272 outermost ones are supposed to have been coerced already.
8274 Otherwise, if PARMS represents only (the innermost) vector of
8275 parameters, this function returns a vector containing just the
8276 innermost resulting arguments. */
8278 static tree
8279 coerce_innermost_template_parms (tree parms,
8280 tree args,
8281 tree in_decl,
8282 tsubst_flags_t complain,
8283 bool require_all_args,
8284 bool use_default_args)
8286 int parms_depth = TMPL_PARMS_DEPTH (parms);
8287 int args_depth = TMPL_ARGS_DEPTH (args);
8288 tree coerced_args;
8290 if (parms_depth > 1)
8292 coerced_args = make_tree_vec (parms_depth);
8293 tree level;
8294 int cur_depth;
8296 for (level = parms, cur_depth = parms_depth;
8297 parms_depth > 0 && level != NULL_TREE;
8298 level = TREE_CHAIN (level), --cur_depth)
8300 tree l;
8301 if (cur_depth == args_depth)
8302 l = coerce_template_parms (TREE_VALUE (level),
8303 args, in_decl, complain,
8304 require_all_args,
8305 use_default_args);
8306 else
8307 l = TMPL_ARGS_LEVEL (args, cur_depth);
8309 if (l == error_mark_node)
8310 return error_mark_node;
8312 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8315 else
8316 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8317 args, in_decl, complain,
8318 require_all_args,
8319 use_default_args);
8320 return coerced_args;
8323 /* Returns 1 if template args OT and NT are equivalent. */
8326 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
8328 if (nt == ot)
8329 return 1;
8330 if (nt == NULL_TREE || ot == NULL_TREE)
8331 return false;
8332 if (nt == any_targ_node || ot == any_targ_node)
8333 return true;
8335 if (TREE_CODE (nt) == TREE_VEC)
8336 /* For member templates */
8337 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8338 else if (PACK_EXPANSION_P (ot))
8339 return (PACK_EXPANSION_P (nt)
8340 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8341 PACK_EXPANSION_PATTERN (nt))
8342 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8343 PACK_EXPANSION_EXTRA_ARGS (nt)));
8344 else if (ARGUMENT_PACK_P (ot))
8346 int i, len;
8347 tree opack, npack;
8349 if (!ARGUMENT_PACK_P (nt))
8350 return 0;
8352 opack = ARGUMENT_PACK_ARGS (ot);
8353 npack = ARGUMENT_PACK_ARGS (nt);
8354 len = TREE_VEC_LENGTH (opack);
8355 if (TREE_VEC_LENGTH (npack) != len)
8356 return 0;
8357 for (i = 0; i < len; ++i)
8358 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8359 TREE_VEC_ELT (npack, i)))
8360 return 0;
8361 return 1;
8363 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8364 gcc_unreachable ();
8365 else if (TYPE_P (nt))
8367 if (!TYPE_P (ot))
8368 return false;
8369 /* Don't treat an alias template specialization with dependent
8370 arguments as equivalent to its underlying type when used as a
8371 template argument; we need them to be distinct so that we
8372 substitute into the specialization arguments at instantiation
8373 time. And aliases can't be equivalent without being ==, so
8374 we don't need to look any deeper.
8376 During partial ordering, however, we need to treat them normally so
8377 that we can order uses of the same alias with different
8378 cv-qualification (79960). */
8379 if (!partial_order
8380 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
8381 return false;
8382 else
8383 return same_type_p (ot, nt);
8385 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8386 return 0;
8387 else
8389 /* Try to treat a template non-type argument that has been converted
8390 to the parameter type as equivalent to one that hasn't yet. */
8391 for (enum tree_code code1 = TREE_CODE (ot);
8392 CONVERT_EXPR_CODE_P (code1)
8393 || code1 == NON_LVALUE_EXPR;
8394 code1 = TREE_CODE (ot))
8395 ot = TREE_OPERAND (ot, 0);
8396 for (enum tree_code code2 = TREE_CODE (nt);
8397 CONVERT_EXPR_CODE_P (code2)
8398 || code2 == NON_LVALUE_EXPR;
8399 code2 = TREE_CODE (nt))
8400 nt = TREE_OPERAND (nt, 0);
8402 return cp_tree_equal (ot, nt);
8406 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8407 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8408 NEWARG_PTR with the offending arguments if they are non-NULL. */
8411 comp_template_args (tree oldargs, tree newargs,
8412 tree *oldarg_ptr, tree *newarg_ptr,
8413 bool partial_order)
8415 int i;
8417 if (oldargs == newargs)
8418 return 1;
8420 if (!oldargs || !newargs)
8421 return 0;
8423 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
8424 return 0;
8426 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
8428 tree nt = TREE_VEC_ELT (newargs, i);
8429 tree ot = TREE_VEC_ELT (oldargs, i);
8431 if (! template_args_equal (ot, nt, partial_order))
8433 if (oldarg_ptr != NULL)
8434 *oldarg_ptr = ot;
8435 if (newarg_ptr != NULL)
8436 *newarg_ptr = nt;
8437 return 0;
8440 return 1;
8443 inline bool
8444 comp_template_args_porder (tree oargs, tree nargs)
8446 return comp_template_args (oargs, nargs, NULL, NULL, true);
8449 static void
8450 add_pending_template (tree d)
8452 tree ti = (TYPE_P (d)
8453 ? CLASSTYPE_TEMPLATE_INFO (d)
8454 : DECL_TEMPLATE_INFO (d));
8455 struct pending_template *pt;
8456 int level;
8458 if (TI_PENDING_TEMPLATE_FLAG (ti))
8459 return;
8461 /* We are called both from instantiate_decl, where we've already had a
8462 tinst_level pushed, and instantiate_template, where we haven't.
8463 Compensate. */
8464 level = !current_tinst_level || current_tinst_level->decl != d;
8466 if (level)
8467 push_tinst_level (d);
8469 pt = ggc_alloc<pending_template> ();
8470 pt->next = NULL;
8471 pt->tinst = current_tinst_level;
8472 if (last_pending_template)
8473 last_pending_template->next = pt;
8474 else
8475 pending_templates = pt;
8477 last_pending_template = pt;
8479 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
8481 if (level)
8482 pop_tinst_level ();
8486 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8487 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8488 documentation for TEMPLATE_ID_EXPR. */
8490 tree
8491 lookup_template_function (tree fns, tree arglist)
8493 tree type;
8495 if (fns == error_mark_node || arglist == error_mark_node)
8496 return error_mark_node;
8498 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
8500 if (!is_overloaded_fn (fns) && !identifier_p (fns))
8502 error ("%q#D is not a function template", fns);
8503 return error_mark_node;
8506 if (BASELINK_P (fns))
8508 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
8509 unknown_type_node,
8510 BASELINK_FUNCTIONS (fns),
8511 arglist);
8512 return fns;
8515 type = TREE_TYPE (fns);
8516 if (TREE_CODE (fns) == OVERLOAD || !type)
8517 type = unknown_type_node;
8519 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
8522 /* Within the scope of a template class S<T>, the name S gets bound
8523 (in build_self_reference) to a TYPE_DECL for the class, not a
8524 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8525 or one of its enclosing classes, and that type is a template,
8526 return the associated TEMPLATE_DECL. Otherwise, the original
8527 DECL is returned.
8529 Also handle the case when DECL is a TREE_LIST of ambiguous
8530 injected-class-names from different bases. */
8532 tree
8533 maybe_get_template_decl_from_type_decl (tree decl)
8535 if (decl == NULL_TREE)
8536 return decl;
8538 /* DR 176: A lookup that finds an injected-class-name (10.2
8539 [class.member.lookup]) can result in an ambiguity in certain cases
8540 (for example, if it is found in more than one base class). If all of
8541 the injected-class-names that are found refer to specializations of
8542 the same class template, and if the name is followed by a
8543 template-argument-list, the reference refers to the class template
8544 itself and not a specialization thereof, and is not ambiguous. */
8545 if (TREE_CODE (decl) == TREE_LIST)
8547 tree t, tmpl = NULL_TREE;
8548 for (t = decl; t; t = TREE_CHAIN (t))
8550 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
8551 if (!tmpl)
8552 tmpl = elt;
8553 else if (tmpl != elt)
8554 break;
8556 if (tmpl && t == NULL_TREE)
8557 return tmpl;
8558 else
8559 return decl;
8562 return (decl != NULL_TREE
8563 && DECL_SELF_REFERENCE_P (decl)
8564 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
8565 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
8568 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8569 parameters, find the desired type.
8571 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8573 IN_DECL, if non-NULL, is the template declaration we are trying to
8574 instantiate.
8576 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8577 the class we are looking up.
8579 Issue error and warning messages under control of COMPLAIN.
8581 If the template class is really a local class in a template
8582 function, then the FUNCTION_CONTEXT is the function in which it is
8583 being instantiated.
8585 ??? Note that this function is currently called *twice* for each
8586 template-id: the first time from the parser, while creating the
8587 incomplete type (finish_template_type), and the second type during the
8588 real instantiation (instantiate_template_class). This is surely something
8589 that we want to avoid. It also causes some problems with argument
8590 coercion (see convert_nontype_argument for more information on this). */
8592 static tree
8593 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
8594 int entering_scope, tsubst_flags_t complain)
8596 tree templ = NULL_TREE, parmlist;
8597 tree t;
8598 spec_entry **slot;
8599 spec_entry *entry;
8600 spec_entry elt;
8601 hashval_t hash;
8603 if (identifier_p (d1))
8605 tree value = innermost_non_namespace_value (d1);
8606 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
8607 templ = value;
8608 else
8610 if (context)
8611 push_decl_namespace (context);
8612 templ = lookup_name (d1);
8613 templ = maybe_get_template_decl_from_type_decl (templ);
8614 if (context)
8615 pop_decl_namespace ();
8617 if (templ)
8618 context = DECL_CONTEXT (templ);
8620 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
8622 tree type = TREE_TYPE (d1);
8624 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8625 an implicit typename for the second A. Deal with it. */
8626 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
8627 type = TREE_TYPE (type);
8629 if (CLASSTYPE_TEMPLATE_INFO (type))
8631 templ = CLASSTYPE_TI_TEMPLATE (type);
8632 d1 = DECL_NAME (templ);
8635 else if (TREE_CODE (d1) == ENUMERAL_TYPE
8636 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
8638 templ = TYPE_TI_TEMPLATE (d1);
8639 d1 = DECL_NAME (templ);
8641 else if (DECL_TYPE_TEMPLATE_P (d1))
8643 templ = d1;
8644 d1 = DECL_NAME (templ);
8645 context = DECL_CONTEXT (templ);
8647 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
8649 templ = d1;
8650 d1 = DECL_NAME (templ);
8653 /* Issue an error message if we didn't find a template. */
8654 if (! templ)
8656 if (complain & tf_error)
8657 error ("%qT is not a template", d1);
8658 return error_mark_node;
8661 if (TREE_CODE (templ) != TEMPLATE_DECL
8662 /* Make sure it's a user visible template, if it was named by
8663 the user. */
8664 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
8665 && !PRIMARY_TEMPLATE_P (templ)))
8667 if (complain & tf_error)
8669 error ("non-template type %qT used as a template", d1);
8670 if (in_decl)
8671 error ("for template declaration %q+D", in_decl);
8673 return error_mark_node;
8676 complain &= ~tf_user;
8678 /* An alias that just changes the name of a template is equivalent to the
8679 other template, so if any of the arguments are pack expansions, strip
8680 the alias to avoid problems with a pack expansion passed to a non-pack
8681 alias template parameter (DR 1430). */
8682 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
8683 templ = get_underlying_template (templ);
8685 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
8687 tree parm;
8688 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
8689 if (arglist2 == error_mark_node
8690 || (!uses_template_parms (arglist2)
8691 && check_instantiated_args (templ, arglist2, complain)))
8692 return error_mark_node;
8694 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
8695 return parm;
8697 else
8699 tree template_type = TREE_TYPE (templ);
8700 tree gen_tmpl;
8701 tree type_decl;
8702 tree found = NULL_TREE;
8703 int arg_depth;
8704 int parm_depth;
8705 int is_dependent_type;
8706 int use_partial_inst_tmpl = false;
8708 if (template_type == error_mark_node)
8709 /* An error occurred while building the template TEMPL, and a
8710 diagnostic has most certainly been emitted for that
8711 already. Let's propagate that error. */
8712 return error_mark_node;
8714 gen_tmpl = most_general_template (templ);
8715 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
8716 parm_depth = TMPL_PARMS_DEPTH (parmlist);
8717 arg_depth = TMPL_ARGS_DEPTH (arglist);
8719 if (arg_depth == 1 && parm_depth > 1)
8721 /* We've been given an incomplete set of template arguments.
8722 For example, given:
8724 template <class T> struct S1 {
8725 template <class U> struct S2 {};
8726 template <class U> struct S2<U*> {};
8729 we will be called with an ARGLIST of `U*', but the
8730 TEMPLATE will be `template <class T> template
8731 <class U> struct S1<T>::S2'. We must fill in the missing
8732 arguments. */
8733 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
8734 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
8735 arg_depth = TMPL_ARGS_DEPTH (arglist);
8738 /* Now we should have enough arguments. */
8739 gcc_assert (parm_depth == arg_depth);
8741 /* From here on, we're only interested in the most general
8742 template. */
8744 /* Calculate the BOUND_ARGS. These will be the args that are
8745 actually tsubst'd into the definition to create the
8746 instantiation. */
8747 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
8748 complain,
8749 /*require_all_args=*/true,
8750 /*use_default_args=*/true);
8752 if (arglist == error_mark_node)
8753 /* We were unable to bind the arguments. */
8754 return error_mark_node;
8756 /* In the scope of a template class, explicit references to the
8757 template class refer to the type of the template, not any
8758 instantiation of it. For example, in:
8760 template <class T> class C { void f(C<T>); }
8762 the `C<T>' is just the same as `C'. Outside of the
8763 class, however, such a reference is an instantiation. */
8764 if (entering_scope
8765 || !PRIMARY_TEMPLATE_P (gen_tmpl)
8766 || currently_open_class (template_type))
8768 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
8770 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
8771 return template_type;
8774 /* If we already have this specialization, return it. */
8775 elt.tmpl = gen_tmpl;
8776 elt.args = arglist;
8777 elt.spec = NULL_TREE;
8778 hash = spec_hasher::hash (&elt);
8779 entry = type_specializations->find_with_hash (&elt, hash);
8781 if (entry)
8782 return entry->spec;
8784 /* If the the template's constraints are not satisfied,
8785 then we cannot form a valid type.
8787 Note that the check is deferred until after the hash
8788 lookup. This prevents redundant checks on previously
8789 instantiated specializations. */
8790 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
8792 if (complain & tf_error)
8794 error ("template constraint failure");
8795 diagnose_constraints (input_location, gen_tmpl, arglist);
8797 return error_mark_node;
8800 is_dependent_type = uses_template_parms (arglist);
8802 /* If the deduced arguments are invalid, then the binding
8803 failed. */
8804 if (!is_dependent_type
8805 && check_instantiated_args (gen_tmpl,
8806 INNERMOST_TEMPLATE_ARGS (arglist),
8807 complain))
8808 return error_mark_node;
8810 if (!is_dependent_type
8811 && !PRIMARY_TEMPLATE_P (gen_tmpl)
8812 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
8813 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
8815 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
8816 DECL_NAME (gen_tmpl),
8817 /*tag_scope=*/ts_global);
8818 return found;
8821 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
8822 complain, in_decl);
8823 if (context == error_mark_node)
8824 return error_mark_node;
8826 if (!context)
8827 context = global_namespace;
8829 /* Create the type. */
8830 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8832 /* The user referred to a specialization of an alias
8833 template represented by GEN_TMPL.
8835 [temp.alias]/2 says:
8837 When a template-id refers to the specialization of an
8838 alias template, it is equivalent to the associated
8839 type obtained by substitution of its
8840 template-arguments for the template-parameters in the
8841 type-id of the alias template. */
8843 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
8844 /* Note that the call above (by indirectly calling
8845 register_specialization in tsubst_decl) registers the
8846 TYPE_DECL representing the specialization of the alias
8847 template. So next time someone substitutes ARGLIST for
8848 the template parms into the alias template (GEN_TMPL),
8849 she'll get that TYPE_DECL back. */
8851 if (t == error_mark_node)
8852 return t;
8854 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
8856 if (!is_dependent_type)
8858 set_current_access_from_decl (TYPE_NAME (template_type));
8859 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
8860 tsubst (ENUM_UNDERLYING_TYPE (template_type),
8861 arglist, complain, in_decl),
8862 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
8863 arglist, complain, in_decl),
8864 SCOPED_ENUM_P (template_type), NULL);
8866 if (t == error_mark_node)
8867 return t;
8869 else
8871 /* We don't want to call start_enum for this type, since
8872 the values for the enumeration constants may involve
8873 template parameters. And, no one should be interested
8874 in the enumeration constants for such a type. */
8875 t = cxx_make_type (ENUMERAL_TYPE);
8876 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
8878 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
8879 ENUM_FIXED_UNDERLYING_TYPE_P (t)
8880 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
8882 else if (CLASS_TYPE_P (template_type))
8884 t = make_class_type (TREE_CODE (template_type));
8885 CLASSTYPE_DECLARED_CLASS (t)
8886 = CLASSTYPE_DECLARED_CLASS (template_type);
8887 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
8889 /* A local class. Make sure the decl gets registered properly. */
8890 if (context == current_function_decl)
8891 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
8893 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
8894 /* This instantiation is another name for the primary
8895 template type. Set the TYPE_CANONICAL field
8896 appropriately. */
8897 TYPE_CANONICAL (t) = template_type;
8898 else if (any_template_arguments_need_structural_equality_p (arglist))
8899 /* Some of the template arguments require structural
8900 equality testing, so this template class requires
8901 structural equality testing. */
8902 SET_TYPE_STRUCTURAL_EQUALITY (t);
8904 else
8905 gcc_unreachable ();
8907 /* If we called start_enum or pushtag above, this information
8908 will already be set up. */
8909 if (!TYPE_NAME (t))
8911 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
8913 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
8914 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
8915 DECL_SOURCE_LOCATION (type_decl)
8916 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
8918 else
8919 type_decl = TYPE_NAME (t);
8921 if (CLASS_TYPE_P (template_type))
8923 TREE_PRIVATE (type_decl)
8924 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
8925 TREE_PROTECTED (type_decl)
8926 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
8927 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
8929 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
8930 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
8934 if (OVERLOAD_TYPE_P (t)
8935 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8937 static const char *tags[] = {"abi_tag", "may_alias"};
8939 for (unsigned ix = 0; ix != 2; ix++)
8941 tree attributes
8942 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
8944 if (attributes)
8945 TYPE_ATTRIBUTES (t)
8946 = tree_cons (TREE_PURPOSE (attributes),
8947 TREE_VALUE (attributes),
8948 TYPE_ATTRIBUTES (t));
8952 /* Let's consider the explicit specialization of a member
8953 of a class template specialization that is implicitly instantiated,
8954 e.g.:
8955 template<class T>
8956 struct S
8958 template<class U> struct M {}; //#0
8961 template<>
8962 template<>
8963 struct S<int>::M<char> //#1
8965 int i;
8967 [temp.expl.spec]/4 says this is valid.
8969 In this case, when we write:
8970 S<int>::M<char> m;
8972 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
8973 the one of #0.
8975 When we encounter #1, we want to store the partial instantiation
8976 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
8978 For all cases other than this "explicit specialization of member of a
8979 class template", we just want to store the most general template into
8980 the CLASSTYPE_TI_TEMPLATE of M.
8982 This case of "explicit specialization of member of a class template"
8983 only happens when:
8984 1/ the enclosing class is an instantiation of, and therefore not
8985 the same as, the context of the most general template, and
8986 2/ we aren't looking at the partial instantiation itself, i.e.
8987 the innermost arguments are not the same as the innermost parms of
8988 the most general template.
8990 So it's only when 1/ and 2/ happens that we want to use the partial
8991 instantiation of the member template in lieu of its most general
8992 template. */
8994 if (PRIMARY_TEMPLATE_P (gen_tmpl)
8995 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
8996 /* the enclosing class must be an instantiation... */
8997 && CLASS_TYPE_P (context)
8998 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
9000 TREE_VEC_LENGTH (arglist)--;
9001 ++processing_template_decl;
9002 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
9003 tree partial_inst_args =
9004 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
9005 arglist, complain, NULL_TREE);
9006 --processing_template_decl;
9007 TREE_VEC_LENGTH (arglist)++;
9008 if (partial_inst_args == error_mark_node)
9009 return error_mark_node;
9010 use_partial_inst_tmpl =
9011 /*...and we must not be looking at the partial instantiation
9012 itself. */
9013 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
9014 partial_inst_args);
9017 if (!use_partial_inst_tmpl)
9018 /* This case is easy; there are no member templates involved. */
9019 found = gen_tmpl;
9020 else
9022 /* This is a full instantiation of a member template. Find
9023 the partial instantiation of which this is an instance. */
9025 /* Temporarily reduce by one the number of levels in the ARGLIST
9026 so as to avoid comparing the last set of arguments. */
9027 TREE_VEC_LENGTH (arglist)--;
9028 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
9029 TREE_VEC_LENGTH (arglist)++;
9030 /* FOUND is either a proper class type, or an alias
9031 template specialization. In the later case, it's a
9032 TYPE_DECL, resulting from the substituting of arguments
9033 for parameters in the TYPE_DECL of the alias template
9034 done earlier. So be careful while getting the template
9035 of FOUND. */
9036 found = (TREE_CODE (found) == TEMPLATE_DECL
9037 ? found
9038 : (TREE_CODE (found) == TYPE_DECL
9039 ? DECL_TI_TEMPLATE (found)
9040 : CLASSTYPE_TI_TEMPLATE (found)));
9043 // Build template info for the new specialization.
9044 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
9046 elt.spec = t;
9047 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
9048 entry = ggc_alloc<spec_entry> ();
9049 *entry = elt;
9050 *slot = entry;
9052 /* Note this use of the partial instantiation so we can check it
9053 later in maybe_process_partial_specialization. */
9054 DECL_TEMPLATE_INSTANTIATIONS (found)
9055 = tree_cons (arglist, t,
9056 DECL_TEMPLATE_INSTANTIATIONS (found));
9058 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
9059 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9060 /* Now that the type has been registered on the instantiations
9061 list, we set up the enumerators. Because the enumeration
9062 constants may involve the enumeration type itself, we make
9063 sure to register the type first, and then create the
9064 constants. That way, doing tsubst_expr for the enumeration
9065 constants won't result in recursive calls here; we'll find
9066 the instantiation and exit above. */
9067 tsubst_enum (template_type, t, arglist);
9069 if (CLASS_TYPE_P (template_type) && is_dependent_type)
9070 /* If the type makes use of template parameters, the
9071 code that generates debugging information will crash. */
9072 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
9074 /* Possibly limit visibility based on template args. */
9075 TREE_PUBLIC (type_decl) = 1;
9076 determine_visibility (type_decl);
9078 inherit_targ_abi_tags (t);
9080 return t;
9084 /* Wrapper for lookup_template_class_1. */
9086 tree
9087 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
9088 int entering_scope, tsubst_flags_t complain)
9090 tree ret;
9091 timevar_push (TV_TEMPLATE_INST);
9092 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
9093 entering_scope, complain);
9094 timevar_pop (TV_TEMPLATE_INST);
9095 return ret;
9098 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9100 tree
9101 lookup_template_variable (tree templ, tree arglist)
9103 /* The type of the expression is NULL_TREE since the template-id could refer
9104 to an explicit or partial specialization. */
9105 tree type = NULL_TREE;
9106 if (flag_concepts && variable_concept_p (templ))
9107 /* Except that concepts are always bool. */
9108 type = boolean_type_node;
9109 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
9112 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9114 tree
9115 finish_template_variable (tree var, tsubst_flags_t complain)
9117 tree templ = TREE_OPERAND (var, 0);
9118 tree arglist = TREE_OPERAND (var, 1);
9120 /* We never want to return a VAR_DECL for a variable concept, since they
9121 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9122 bool concept_p = flag_concepts && variable_concept_p (templ);
9123 if (concept_p && processing_template_decl)
9124 return var;
9126 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
9127 arglist = add_outermost_template_args (tmpl_args, arglist);
9129 templ = most_general_template (templ);
9130 tree parms = DECL_TEMPLATE_PARMS (templ);
9131 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
9132 /*req_all*/true,
9133 /*use_default*/true);
9135 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
9137 if (complain & tf_error)
9139 error ("use of invalid variable template %qE", var);
9140 diagnose_constraints (location_of (var), templ, arglist);
9142 return error_mark_node;
9145 /* If a template-id refers to a specialization of a variable
9146 concept, then the expression is true if and only if the
9147 concept's constraints are satisfied by the given template
9148 arguments.
9150 NOTE: This is an extension of Concepts Lite TS that
9151 allows constraints to be used in expressions. */
9152 if (concept_p)
9154 tree decl = DECL_TEMPLATE_RESULT (templ);
9155 return evaluate_variable_concept (decl, arglist);
9158 return instantiate_template (templ, arglist, complain);
9161 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9162 TARGS template args, and instantiate it if it's not dependent. */
9164 tree
9165 lookup_and_finish_template_variable (tree templ, tree targs,
9166 tsubst_flags_t complain)
9168 templ = lookup_template_variable (templ, targs);
9169 if (!any_dependent_template_arguments_p (targs))
9171 templ = finish_template_variable (templ, complain);
9172 mark_used (templ);
9175 return convert_from_reference (templ);
9179 struct pair_fn_data
9181 tree_fn_t fn;
9182 tree_fn_t any_fn;
9183 void *data;
9184 /* True when we should also visit template parameters that occur in
9185 non-deduced contexts. */
9186 bool include_nondeduced_p;
9187 hash_set<tree> *visited;
9190 /* Called from for_each_template_parm via walk_tree. */
9192 static tree
9193 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
9195 tree t = *tp;
9196 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
9197 tree_fn_t fn = pfd->fn;
9198 void *data = pfd->data;
9199 tree result = NULL_TREE;
9201 #define WALK_SUBTREE(NODE) \
9202 do \
9204 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9205 pfd->include_nondeduced_p, \
9206 pfd->any_fn); \
9207 if (result) goto out; \
9209 while (0)
9211 if (pfd->any_fn && (*pfd->any_fn)(t, data))
9212 return t;
9214 if (TYPE_P (t)
9215 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
9216 WALK_SUBTREE (TYPE_CONTEXT (t));
9218 switch (TREE_CODE (t))
9220 case RECORD_TYPE:
9221 if (TYPE_PTRMEMFUNC_P (t))
9222 break;
9223 /* Fall through. */
9225 case UNION_TYPE:
9226 case ENUMERAL_TYPE:
9227 if (!TYPE_TEMPLATE_INFO (t))
9228 *walk_subtrees = 0;
9229 else
9230 WALK_SUBTREE (TYPE_TI_ARGS (t));
9231 break;
9233 case INTEGER_TYPE:
9234 WALK_SUBTREE (TYPE_MIN_VALUE (t));
9235 WALK_SUBTREE (TYPE_MAX_VALUE (t));
9236 break;
9238 case METHOD_TYPE:
9239 /* Since we're not going to walk subtrees, we have to do this
9240 explicitly here. */
9241 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
9242 /* Fall through. */
9244 case FUNCTION_TYPE:
9245 /* Check the return type. */
9246 WALK_SUBTREE (TREE_TYPE (t));
9248 /* Check the parameter types. Since default arguments are not
9249 instantiated until they are needed, the TYPE_ARG_TYPES may
9250 contain expressions that involve template parameters. But,
9251 no-one should be looking at them yet. And, once they're
9252 instantiated, they don't contain template parameters, so
9253 there's no point in looking at them then, either. */
9255 tree parm;
9257 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
9258 WALK_SUBTREE (TREE_VALUE (parm));
9260 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9261 want walk_tree walking into them itself. */
9262 *walk_subtrees = 0;
9265 if (flag_noexcept_type)
9267 tree spec = TYPE_RAISES_EXCEPTIONS (t);
9268 if (spec)
9269 WALK_SUBTREE (TREE_PURPOSE (spec));
9271 break;
9273 case TYPEOF_TYPE:
9274 case UNDERLYING_TYPE:
9275 if (pfd->include_nondeduced_p
9276 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
9277 pfd->visited,
9278 pfd->include_nondeduced_p,
9279 pfd->any_fn))
9280 return error_mark_node;
9281 break;
9283 case FUNCTION_DECL:
9284 case VAR_DECL:
9285 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9286 WALK_SUBTREE (DECL_TI_ARGS (t));
9287 /* Fall through. */
9289 case PARM_DECL:
9290 case CONST_DECL:
9291 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
9292 WALK_SUBTREE (DECL_INITIAL (t));
9293 if (DECL_CONTEXT (t)
9294 && pfd->include_nondeduced_p)
9295 WALK_SUBTREE (DECL_CONTEXT (t));
9296 break;
9298 case BOUND_TEMPLATE_TEMPLATE_PARM:
9299 /* Record template parameters such as `T' inside `TT<T>'. */
9300 WALK_SUBTREE (TYPE_TI_ARGS (t));
9301 /* Fall through. */
9303 case TEMPLATE_TEMPLATE_PARM:
9304 case TEMPLATE_TYPE_PARM:
9305 case TEMPLATE_PARM_INDEX:
9306 if (fn && (*fn)(t, data))
9307 return t;
9308 else if (!fn)
9309 return t;
9310 break;
9312 case TEMPLATE_DECL:
9313 /* A template template parameter is encountered. */
9314 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9315 WALK_SUBTREE (TREE_TYPE (t));
9317 /* Already substituted template template parameter */
9318 *walk_subtrees = 0;
9319 break;
9321 case TYPENAME_TYPE:
9322 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9323 partial instantiation. */
9324 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
9325 break;
9327 case CONSTRUCTOR:
9328 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
9329 && pfd->include_nondeduced_p)
9330 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
9331 break;
9333 case INDIRECT_REF:
9334 case COMPONENT_REF:
9335 /* If there's no type, then this thing must be some expression
9336 involving template parameters. */
9337 if (!fn && !TREE_TYPE (t))
9338 return error_mark_node;
9339 break;
9341 case MODOP_EXPR:
9342 case CAST_EXPR:
9343 case IMPLICIT_CONV_EXPR:
9344 case REINTERPRET_CAST_EXPR:
9345 case CONST_CAST_EXPR:
9346 case STATIC_CAST_EXPR:
9347 case DYNAMIC_CAST_EXPR:
9348 case ARROW_EXPR:
9349 case DOTSTAR_EXPR:
9350 case TYPEID_EXPR:
9351 case PSEUDO_DTOR_EXPR:
9352 if (!fn)
9353 return error_mark_node;
9354 break;
9356 default:
9357 break;
9360 #undef WALK_SUBTREE
9362 /* We didn't find any template parameters we liked. */
9363 out:
9364 return result;
9367 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9368 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9369 call FN with the parameter and the DATA.
9370 If FN returns nonzero, the iteration is terminated, and
9371 for_each_template_parm returns 1. Otherwise, the iteration
9372 continues. If FN never returns a nonzero value, the value
9373 returned by for_each_template_parm is 0. If FN is NULL, it is
9374 considered to be the function which always returns 1.
9376 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9377 parameters that occur in non-deduced contexts. When false, only
9378 visits those template parameters that can be deduced. */
9380 static tree
9381 for_each_template_parm (tree t, tree_fn_t fn, void* data,
9382 hash_set<tree> *visited,
9383 bool include_nondeduced_p,
9384 tree_fn_t any_fn)
9386 struct pair_fn_data pfd;
9387 tree result;
9389 /* Set up. */
9390 pfd.fn = fn;
9391 pfd.any_fn = any_fn;
9392 pfd.data = data;
9393 pfd.include_nondeduced_p = include_nondeduced_p;
9395 /* Walk the tree. (Conceptually, we would like to walk without
9396 duplicates, but for_each_template_parm_r recursively calls
9397 for_each_template_parm, so we would need to reorganize a fair
9398 bit to use walk_tree_without_duplicates, so we keep our own
9399 visited list.) */
9400 if (visited)
9401 pfd.visited = visited;
9402 else
9403 pfd.visited = new hash_set<tree>;
9404 result = cp_walk_tree (&t,
9405 for_each_template_parm_r,
9406 &pfd,
9407 pfd.visited);
9409 /* Clean up. */
9410 if (!visited)
9412 delete pfd.visited;
9413 pfd.visited = 0;
9416 return result;
9419 /* Returns true if T depends on any template parameter. */
9422 uses_template_parms (tree t)
9424 if (t == NULL_TREE)
9425 return false;
9427 bool dependent_p;
9428 int saved_processing_template_decl;
9430 saved_processing_template_decl = processing_template_decl;
9431 if (!saved_processing_template_decl)
9432 processing_template_decl = 1;
9433 if (TYPE_P (t))
9434 dependent_p = dependent_type_p (t);
9435 else if (TREE_CODE (t) == TREE_VEC)
9436 dependent_p = any_dependent_template_arguments_p (t);
9437 else if (TREE_CODE (t) == TREE_LIST)
9438 dependent_p = (uses_template_parms (TREE_VALUE (t))
9439 || uses_template_parms (TREE_CHAIN (t)));
9440 else if (TREE_CODE (t) == TYPE_DECL)
9441 dependent_p = dependent_type_p (TREE_TYPE (t));
9442 else if (DECL_P (t)
9443 || EXPR_P (t)
9444 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
9445 || TREE_CODE (t) == OVERLOAD
9446 || BASELINK_P (t)
9447 || identifier_p (t)
9448 || TREE_CODE (t) == TRAIT_EXPR
9449 || TREE_CODE (t) == CONSTRUCTOR
9450 || CONSTANT_CLASS_P (t))
9451 dependent_p = (type_dependent_expression_p (t)
9452 || value_dependent_expression_p (t));
9453 else
9455 gcc_assert (t == error_mark_node);
9456 dependent_p = false;
9459 processing_template_decl = saved_processing_template_decl;
9461 return dependent_p;
9464 /* Returns true iff current_function_decl is an incompletely instantiated
9465 template. Useful instead of processing_template_decl because the latter
9466 is set to 0 during instantiate_non_dependent_expr. */
9468 bool
9469 in_template_function (void)
9471 tree fn = current_function_decl;
9472 bool ret;
9473 ++processing_template_decl;
9474 ret = (fn && DECL_LANG_SPECIFIC (fn)
9475 && DECL_TEMPLATE_INFO (fn)
9476 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
9477 --processing_template_decl;
9478 return ret;
9481 /* Returns true if T depends on any template parameter with level LEVEL. */
9483 bool
9484 uses_template_parms_level (tree t, int level)
9486 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
9487 /*include_nondeduced_p=*/true);
9490 /* Returns true if the signature of DECL depends on any template parameter from
9491 its enclosing class. */
9493 bool
9494 uses_outer_template_parms (tree decl)
9496 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
9497 if (depth == 0)
9498 return false;
9499 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
9500 &depth, NULL, /*include_nondeduced_p=*/true))
9501 return true;
9502 if (PRIMARY_TEMPLATE_P (decl)
9503 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9504 (DECL_TEMPLATE_PARMS (decl)),
9505 template_parm_outer_level,
9506 &depth, NULL, /*include_nondeduced_p=*/true))
9507 return true;
9508 tree ci = get_constraints (decl);
9509 if (ci)
9510 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
9511 if (ci && for_each_template_parm (ci, template_parm_outer_level,
9512 &depth, NULL, /*nondeduced*/true))
9513 return true;
9514 return false;
9517 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9518 ill-formed translation unit, i.e. a variable or function that isn't
9519 usable in a constant expression. */
9521 static inline bool
9522 neglectable_inst_p (tree d)
9524 return (DECL_P (d)
9525 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
9526 : decl_maybe_constant_var_p (d)));
9529 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9530 neglectable and instantiated from within an erroneous instantiation. */
9532 static bool
9533 limit_bad_template_recursion (tree decl)
9535 struct tinst_level *lev = current_tinst_level;
9536 int errs = errorcount + sorrycount;
9537 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
9538 return false;
9540 for (; lev; lev = lev->next)
9541 if (neglectable_inst_p (lev->decl))
9542 break;
9544 return (lev && errs > lev->errors);
9547 static int tinst_depth;
9548 extern int max_tinst_depth;
9549 int depth_reached;
9551 static GTY(()) struct tinst_level *last_error_tinst_level;
9553 /* We're starting to instantiate D; record the template instantiation context
9554 for diagnostics and to restore it later. */
9556 bool
9557 push_tinst_level (tree d)
9559 return push_tinst_level_loc (d, input_location);
9562 /* We're starting to instantiate D; record the template instantiation context
9563 at LOC for diagnostics and to restore it later. */
9565 bool
9566 push_tinst_level_loc (tree d, location_t loc)
9568 struct tinst_level *new_level;
9570 if (tinst_depth >= max_tinst_depth)
9572 /* Tell error.c not to try to instantiate any templates. */
9573 at_eof = 2;
9574 fatal_error (input_location,
9575 "template instantiation depth exceeds maximum of %d"
9576 " (use -ftemplate-depth= to increase the maximum)",
9577 max_tinst_depth);
9578 return false;
9581 /* If the current instantiation caused problems, don't let it instantiate
9582 anything else. Do allow deduction substitution and decls usable in
9583 constant expressions. */
9584 if (limit_bad_template_recursion (d))
9585 return false;
9587 /* When not -quiet, dump template instantiations other than functions, since
9588 announce_function will take care of those. */
9589 if (!quiet_flag
9590 && TREE_CODE (d) != TREE_LIST
9591 && TREE_CODE (d) != FUNCTION_DECL)
9592 fprintf (stderr, " %s", decl_as_string (d, TFF_DECL_SPECIFIERS));
9594 new_level = ggc_alloc<tinst_level> ();
9595 new_level->decl = d;
9596 new_level->locus = loc;
9597 new_level->errors = errorcount+sorrycount;
9598 new_level->in_system_header_p = in_system_header_at (input_location);
9599 new_level->next = current_tinst_level;
9600 current_tinst_level = new_level;
9602 ++tinst_depth;
9603 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
9604 depth_reached = tinst_depth;
9606 return true;
9609 /* We're done instantiating this template; return to the instantiation
9610 context. */
9612 void
9613 pop_tinst_level (void)
9615 /* Restore the filename and line number stashed away when we started
9616 this instantiation. */
9617 input_location = current_tinst_level->locus;
9618 current_tinst_level = current_tinst_level->next;
9619 --tinst_depth;
9622 /* We're instantiating a deferred template; restore the template
9623 instantiation context in which the instantiation was requested, which
9624 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9626 static tree
9627 reopen_tinst_level (struct tinst_level *level)
9629 struct tinst_level *t;
9631 tinst_depth = 0;
9632 for (t = level; t; t = t->next)
9633 ++tinst_depth;
9635 current_tinst_level = level;
9636 pop_tinst_level ();
9637 if (current_tinst_level)
9638 current_tinst_level->errors = errorcount+sorrycount;
9639 return level->decl;
9642 /* Returns the TINST_LEVEL which gives the original instantiation
9643 context. */
9645 struct tinst_level *
9646 outermost_tinst_level (void)
9648 struct tinst_level *level = current_tinst_level;
9649 if (level)
9650 while (level->next)
9651 level = level->next;
9652 return level;
9655 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9656 vector of template arguments, as for tsubst.
9658 Returns an appropriate tsubst'd friend declaration. */
9660 static tree
9661 tsubst_friend_function (tree decl, tree args)
9663 tree new_friend;
9665 if (TREE_CODE (decl) == FUNCTION_DECL
9666 && DECL_TEMPLATE_INSTANTIATION (decl)
9667 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
9668 /* This was a friend declared with an explicit template
9669 argument list, e.g.:
9671 friend void f<>(T);
9673 to indicate that f was a template instantiation, not a new
9674 function declaration. Now, we have to figure out what
9675 instantiation of what template. */
9677 tree template_id, arglist, fns;
9678 tree new_args;
9679 tree tmpl;
9680 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
9682 /* Friend functions are looked up in the containing namespace scope.
9683 We must enter that scope, to avoid finding member functions of the
9684 current class with same name. */
9685 push_nested_namespace (ns);
9686 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
9687 tf_warning_or_error, NULL_TREE,
9688 /*integral_constant_expression_p=*/false);
9689 pop_nested_namespace (ns);
9690 arglist = tsubst (DECL_TI_ARGS (decl), args,
9691 tf_warning_or_error, NULL_TREE);
9692 template_id = lookup_template_function (fns, arglist);
9694 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9695 tmpl = determine_specialization (template_id, new_friend,
9696 &new_args,
9697 /*need_member_template=*/0,
9698 TREE_VEC_LENGTH (args),
9699 tsk_none);
9700 return instantiate_template (tmpl, new_args, tf_error);
9703 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9705 /* The NEW_FRIEND will look like an instantiation, to the
9706 compiler, but is not an instantiation from the point of view of
9707 the language. For example, we might have had:
9709 template <class T> struct S {
9710 template <class U> friend void f(T, U);
9713 Then, in S<int>, template <class U> void f(int, U) is not an
9714 instantiation of anything. */
9715 if (new_friend == error_mark_node)
9716 return error_mark_node;
9718 DECL_USE_TEMPLATE (new_friend) = 0;
9719 if (TREE_CODE (decl) == TEMPLATE_DECL)
9721 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
9722 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
9723 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
9726 /* The mangled name for the NEW_FRIEND is incorrect. The function
9727 is not a template instantiation and should not be mangled like
9728 one. Therefore, we forget the mangling here; we'll recompute it
9729 later if we need it. */
9730 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
9732 SET_DECL_RTL (new_friend, NULL);
9733 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
9736 if (DECL_NAMESPACE_SCOPE_P (new_friend))
9738 tree old_decl;
9739 tree new_friend_template_info;
9740 tree new_friend_result_template_info;
9741 tree ns;
9742 int new_friend_is_defn;
9744 /* We must save some information from NEW_FRIEND before calling
9745 duplicate decls since that function will free NEW_FRIEND if
9746 possible. */
9747 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
9748 new_friend_is_defn =
9749 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9750 (template_for_substitution (new_friend)))
9751 != NULL_TREE);
9752 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
9754 /* This declaration is a `primary' template. */
9755 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
9757 new_friend_result_template_info
9758 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
9760 else
9761 new_friend_result_template_info = NULL_TREE;
9763 /* Inside pushdecl_namespace_level, we will push into the
9764 current namespace. However, the friend function should go
9765 into the namespace of the template. */
9766 ns = decl_namespace_context (new_friend);
9767 push_nested_namespace (ns);
9768 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
9769 pop_nested_namespace (ns);
9771 if (old_decl == error_mark_node)
9772 return error_mark_node;
9774 if (old_decl != new_friend)
9776 /* This new friend declaration matched an existing
9777 declaration. For example, given:
9779 template <class T> void f(T);
9780 template <class U> class C {
9781 template <class T> friend void f(T) {}
9784 the friend declaration actually provides the definition
9785 of `f', once C has been instantiated for some type. So,
9786 old_decl will be the out-of-class template declaration,
9787 while new_friend is the in-class definition.
9789 But, if `f' was called before this point, the
9790 instantiation of `f' will have DECL_TI_ARGS corresponding
9791 to `T' but not to `U', references to which might appear
9792 in the definition of `f'. Previously, the most general
9793 template for an instantiation of `f' was the out-of-class
9794 version; now it is the in-class version. Therefore, we
9795 run through all specialization of `f', adding to their
9796 DECL_TI_ARGS appropriately. In particular, they need a
9797 new set of outer arguments, corresponding to the
9798 arguments for this class instantiation.
9800 The same situation can arise with something like this:
9802 friend void f(int);
9803 template <class T> class C {
9804 friend void f(T) {}
9807 when `C<int>' is instantiated. Now, `f(int)' is defined
9808 in the class. */
9810 if (!new_friend_is_defn)
9811 /* On the other hand, if the in-class declaration does
9812 *not* provide a definition, then we don't want to alter
9813 existing definitions. We can just leave everything
9814 alone. */
9816 else
9818 tree new_template = TI_TEMPLATE (new_friend_template_info);
9819 tree new_args = TI_ARGS (new_friend_template_info);
9821 /* Overwrite whatever template info was there before, if
9822 any, with the new template information pertaining to
9823 the declaration. */
9824 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
9826 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
9828 /* We should have called reregister_specialization in
9829 duplicate_decls. */
9830 gcc_assert (retrieve_specialization (new_template,
9831 new_args, 0)
9832 == old_decl);
9834 /* Instantiate it if the global has already been used. */
9835 if (DECL_ODR_USED (old_decl))
9836 instantiate_decl (old_decl, /*defer_ok=*/true,
9837 /*expl_inst_class_mem_p=*/false);
9839 else
9841 tree t;
9843 /* Indicate that the old function template is a partial
9844 instantiation. */
9845 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
9846 = new_friend_result_template_info;
9848 gcc_assert (new_template
9849 == most_general_template (new_template));
9850 gcc_assert (new_template != old_decl);
9852 /* Reassign any specializations already in the hash table
9853 to the new more general template, and add the
9854 additional template args. */
9855 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
9856 t != NULL_TREE;
9857 t = TREE_CHAIN (t))
9859 tree spec = TREE_VALUE (t);
9860 spec_entry elt;
9862 elt.tmpl = old_decl;
9863 elt.args = DECL_TI_ARGS (spec);
9864 elt.spec = NULL_TREE;
9866 decl_specializations->remove_elt (&elt);
9868 DECL_TI_ARGS (spec)
9869 = add_outermost_template_args (new_args,
9870 DECL_TI_ARGS (spec));
9872 register_specialization
9873 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
9876 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
9880 /* The information from NEW_FRIEND has been merged into OLD_DECL
9881 by duplicate_decls. */
9882 new_friend = old_decl;
9885 else
9887 tree context = DECL_CONTEXT (new_friend);
9888 bool dependent_p;
9890 /* In the code
9891 template <class T> class C {
9892 template <class U> friend void C1<U>::f (); // case 1
9893 friend void C2<T>::f (); // case 2
9895 we only need to make sure CONTEXT is a complete type for
9896 case 2. To distinguish between the two cases, we note that
9897 CONTEXT of case 1 remains dependent type after tsubst while
9898 this isn't true for case 2. */
9899 ++processing_template_decl;
9900 dependent_p = dependent_type_p (context);
9901 --processing_template_decl;
9903 if (!dependent_p
9904 && !complete_type_or_else (context, NULL_TREE))
9905 return error_mark_node;
9907 if (COMPLETE_TYPE_P (context))
9909 tree fn = new_friend;
9910 /* do_friend adds the TEMPLATE_DECL for any member friend
9911 template even if it isn't a member template, i.e.
9912 template <class T> friend A<T>::f();
9913 Look through it in that case. */
9914 if (TREE_CODE (fn) == TEMPLATE_DECL
9915 && !PRIMARY_TEMPLATE_P (fn))
9916 fn = DECL_TEMPLATE_RESULT (fn);
9917 /* Check to see that the declaration is really present, and,
9918 possibly obtain an improved declaration. */
9919 fn = check_classfn (context, fn, NULL_TREE);
9921 if (fn)
9922 new_friend = fn;
9926 return new_friend;
9929 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
9930 template arguments, as for tsubst.
9932 Returns an appropriate tsubst'd friend type or error_mark_node on
9933 failure. */
9935 static tree
9936 tsubst_friend_class (tree friend_tmpl, tree args)
9938 tree friend_type;
9939 tree tmpl;
9940 tree context;
9942 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
9944 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
9945 return TREE_TYPE (t);
9948 context = CP_DECL_CONTEXT (friend_tmpl);
9950 if (context != global_namespace)
9952 if (TREE_CODE (context) == NAMESPACE_DECL)
9953 push_nested_namespace (context);
9954 else
9955 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
9958 /* Look for a class template declaration. We look for hidden names
9959 because two friend declarations of the same template are the
9960 same. For example, in:
9962 struct A {
9963 template <typename> friend class F;
9965 template <typename> struct B {
9966 template <typename> friend class F;
9969 both F templates are the same. */
9970 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
9971 /*block_p=*/true, 0, LOOKUP_HIDDEN);
9973 /* But, if we don't find one, it might be because we're in a
9974 situation like this:
9976 template <class T>
9977 struct S {
9978 template <class U>
9979 friend struct S;
9982 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
9983 for `S<int>', not the TEMPLATE_DECL. */
9984 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
9986 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
9987 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
9990 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
9992 /* The friend template has already been declared. Just
9993 check to see that the declarations match, and install any new
9994 default parameters. We must tsubst the default parameters,
9995 of course. We only need the innermost template parameters
9996 because that is all that redeclare_class_template will look
9997 at. */
9998 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
9999 > TMPL_ARGS_DEPTH (args))
10001 tree parms;
10002 location_t saved_input_location;
10003 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
10004 args, tf_warning_or_error);
10006 saved_input_location = input_location;
10007 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
10008 tree cons = get_constraints (tmpl);
10009 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
10010 input_location = saved_input_location;
10014 friend_type = TREE_TYPE (tmpl);
10016 else
10018 /* The friend template has not already been declared. In this
10019 case, the instantiation of the template class will cause the
10020 injection of this template into the global scope. */
10021 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
10022 if (tmpl == error_mark_node)
10023 return error_mark_node;
10025 /* The new TMPL is not an instantiation of anything, so we
10026 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
10027 the new type because that is supposed to be the corresponding
10028 template decl, i.e., TMPL. */
10029 DECL_USE_TEMPLATE (tmpl) = 0;
10030 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
10031 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
10032 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
10033 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
10035 /* Inject this template into the global scope. */
10036 friend_type = TREE_TYPE (pushdecl_top_level (tmpl, true));
10039 if (context != global_namespace)
10041 if (TREE_CODE (context) == NAMESPACE_DECL)
10042 pop_nested_namespace (context);
10043 else
10044 pop_nested_class ();
10047 return friend_type;
10050 /* Returns zero if TYPE cannot be completed later due to circularity.
10051 Otherwise returns one. */
10053 static int
10054 can_complete_type_without_circularity (tree type)
10056 if (type == NULL_TREE || type == error_mark_node)
10057 return 0;
10058 else if (COMPLETE_TYPE_P (type))
10059 return 1;
10060 else if (TREE_CODE (type) == ARRAY_TYPE)
10061 return can_complete_type_without_circularity (TREE_TYPE (type));
10062 else if (CLASS_TYPE_P (type)
10063 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
10064 return 0;
10065 else
10066 return 1;
10069 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
10070 tsubst_flags_t, tree);
10072 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10073 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10075 static tree
10076 tsubst_attribute (tree t, tree *decl_p, tree args,
10077 tsubst_flags_t complain, tree in_decl)
10079 gcc_assert (ATTR_IS_DEPENDENT (t));
10081 tree val = TREE_VALUE (t);
10082 if (val == NULL_TREE)
10083 /* Nothing to do. */;
10084 else if ((flag_openmp || flag_openmp_simd || flag_cilkplus)
10085 && is_attribute_p ("omp declare simd",
10086 get_attribute_name (t)))
10088 tree clauses = TREE_VALUE (val);
10089 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
10090 complain, in_decl);
10091 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
10092 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
10093 tree parms = DECL_ARGUMENTS (*decl_p);
10094 clauses
10095 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
10096 if (clauses)
10097 val = build_tree_list (NULL_TREE, clauses);
10098 else
10099 val = NULL_TREE;
10101 /* If the first attribute argument is an identifier, don't
10102 pass it through tsubst. Attributes like mode, format,
10103 cleanup and several target specific attributes expect it
10104 unmodified. */
10105 else if (attribute_takes_identifier_p (get_attribute_name (t)))
10107 tree chain
10108 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
10109 /*integral_constant_expression_p=*/false);
10110 if (chain != TREE_CHAIN (val))
10111 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
10113 else if (PACK_EXPANSION_P (val))
10115 /* An attribute pack expansion. */
10116 tree purp = TREE_PURPOSE (t);
10117 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
10118 if (pack == error_mark_node)
10119 return error_mark_node;
10120 int len = TREE_VEC_LENGTH (pack);
10121 tree list = NULL_TREE;
10122 tree *q = &list;
10123 for (int i = 0; i < len; ++i)
10125 tree elt = TREE_VEC_ELT (pack, i);
10126 *q = build_tree_list (purp, elt);
10127 q = &TREE_CHAIN (*q);
10129 return list;
10131 else
10132 val = tsubst_expr (val, args, complain, in_decl,
10133 /*integral_constant_expression_p=*/false);
10135 if (val != TREE_VALUE (t))
10136 return build_tree_list (TREE_PURPOSE (t), val);
10137 return t;
10140 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10141 unchanged or a new TREE_LIST chain. */
10143 static tree
10144 tsubst_attributes (tree attributes, tree args,
10145 tsubst_flags_t complain, tree in_decl)
10147 tree last_dep = NULL_TREE;
10149 for (tree t = attributes; t; t = TREE_CHAIN (t))
10150 if (ATTR_IS_DEPENDENT (t))
10152 last_dep = t;
10153 attributes = copy_list (attributes);
10154 break;
10157 if (last_dep)
10158 for (tree *p = &attributes; *p; )
10160 tree t = *p;
10161 if (ATTR_IS_DEPENDENT (t))
10163 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
10164 if (subst != t)
10166 *p = subst;
10168 p = &TREE_CHAIN (*p);
10169 while (*p);
10170 *p = TREE_CHAIN (t);
10171 continue;
10174 p = &TREE_CHAIN (*p);
10177 return attributes;
10180 /* Apply any attributes which had to be deferred until instantiation
10181 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10182 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10184 static void
10185 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
10186 tree args, tsubst_flags_t complain, tree in_decl)
10188 tree last_dep = NULL_TREE;
10189 tree t;
10190 tree *p;
10192 if (attributes == NULL_TREE)
10193 return;
10195 if (DECL_P (*decl_p))
10197 if (TREE_TYPE (*decl_p) == error_mark_node)
10198 return;
10199 p = &DECL_ATTRIBUTES (*decl_p);
10200 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10201 to our attributes parameter. */
10202 gcc_assert (*p == attributes);
10204 else
10206 p = &TYPE_ATTRIBUTES (*decl_p);
10207 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10208 lookup_template_class_1, and should be preserved. */
10209 gcc_assert (*p != attributes);
10210 while (*p)
10211 p = &TREE_CHAIN (*p);
10214 for (t = attributes; t; t = TREE_CHAIN (t))
10215 if (ATTR_IS_DEPENDENT (t))
10217 last_dep = t;
10218 attributes = copy_list (attributes);
10219 break;
10222 *p = attributes;
10223 if (last_dep)
10225 tree late_attrs = NULL_TREE;
10226 tree *q = &late_attrs;
10228 for (; *p; )
10230 t = *p;
10231 if (ATTR_IS_DEPENDENT (t))
10233 *p = TREE_CHAIN (t);
10234 TREE_CHAIN (t) = NULL_TREE;
10235 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
10237 q = &TREE_CHAIN (*q);
10238 while (*q);
10240 else
10241 p = &TREE_CHAIN (t);
10244 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
10248 /* Perform (or defer) access check for typedefs that were referenced
10249 from within the template TMPL code.
10250 This is a subroutine of instantiate_decl and instantiate_class_template.
10251 TMPL is the template to consider and TARGS is the list of arguments of
10252 that template. */
10254 static void
10255 perform_typedefs_access_check (tree tmpl, tree targs)
10257 location_t saved_location;
10258 unsigned i;
10259 qualified_typedef_usage_t *iter;
10261 if (!tmpl
10262 || (!CLASS_TYPE_P (tmpl)
10263 && TREE_CODE (tmpl) != FUNCTION_DECL))
10264 return;
10266 saved_location = input_location;
10267 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
10269 tree type_decl = iter->typedef_decl;
10270 tree type_scope = iter->context;
10272 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
10273 continue;
10275 if (uses_template_parms (type_decl))
10276 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
10277 if (uses_template_parms (type_scope))
10278 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
10280 /* Make access check error messages point to the location
10281 of the use of the typedef. */
10282 input_location = iter->locus;
10283 perform_or_defer_access_check (TYPE_BINFO (type_scope),
10284 type_decl, type_decl,
10285 tf_warning_or_error);
10287 input_location = saved_location;
10290 static tree
10291 instantiate_class_template_1 (tree type)
10293 tree templ, args, pattern, t, member;
10294 tree typedecl;
10295 tree pbinfo;
10296 tree base_list;
10297 unsigned int saved_maximum_field_alignment;
10298 tree fn_context;
10300 if (type == error_mark_node)
10301 return error_mark_node;
10303 if (COMPLETE_OR_OPEN_TYPE_P (type)
10304 || uses_template_parms (type))
10305 return type;
10307 /* Figure out which template is being instantiated. */
10308 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
10309 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
10311 /* Determine what specialization of the original template to
10312 instantiate. */
10313 t = most_specialized_partial_spec (type, tf_warning_or_error);
10314 if (t == error_mark_node)
10316 TYPE_BEING_DEFINED (type) = 1;
10317 return error_mark_node;
10319 else if (t)
10321 /* This TYPE is actually an instantiation of a partial
10322 specialization. We replace the innermost set of ARGS with
10323 the arguments appropriate for substitution. For example,
10324 given:
10326 template <class T> struct S {};
10327 template <class T> struct S<T*> {};
10329 and supposing that we are instantiating S<int*>, ARGS will
10330 presently be {int*} -- but we need {int}. */
10331 pattern = TREE_TYPE (t);
10332 args = TREE_PURPOSE (t);
10334 else
10336 pattern = TREE_TYPE (templ);
10337 args = CLASSTYPE_TI_ARGS (type);
10340 /* If the template we're instantiating is incomplete, then clearly
10341 there's nothing we can do. */
10342 if (!COMPLETE_TYPE_P (pattern))
10343 return type;
10345 /* If we've recursively instantiated too many templates, stop. */
10346 if (! push_tinst_level (type))
10347 return type;
10349 /* Now we're really doing the instantiation. Mark the type as in
10350 the process of being defined. */
10351 TYPE_BEING_DEFINED (type) = 1;
10353 /* We may be in the middle of deferred access check. Disable
10354 it now. */
10355 push_deferring_access_checks (dk_no_deferred);
10357 int saved_unevaluated_operand = cp_unevaluated_operand;
10358 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10360 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
10361 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10362 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
10363 fn_context = error_mark_node;
10364 if (!fn_context)
10365 push_to_top_level ();
10366 else
10368 cp_unevaluated_operand = 0;
10369 c_inhibit_evaluation_warnings = 0;
10371 /* Use #pragma pack from the template context. */
10372 saved_maximum_field_alignment = maximum_field_alignment;
10373 maximum_field_alignment = TYPE_PRECISION (pattern);
10375 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
10377 /* Set the input location to the most specialized template definition.
10378 This is needed if tsubsting causes an error. */
10379 typedecl = TYPE_MAIN_DECL (pattern);
10380 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
10381 DECL_SOURCE_LOCATION (typedecl);
10383 TYPE_PACKED (type) = TYPE_PACKED (pattern);
10384 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
10385 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
10386 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
10387 if (ANON_AGGR_TYPE_P (pattern))
10388 SET_ANON_AGGR_TYPE_P (type);
10389 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
10391 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
10392 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
10393 /* Adjust visibility for template arguments. */
10394 determine_visibility (TYPE_MAIN_DECL (type));
10396 if (CLASS_TYPE_P (type))
10397 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
10399 pbinfo = TYPE_BINFO (pattern);
10401 /* We should never instantiate a nested class before its enclosing
10402 class; we need to look up the nested class by name before we can
10403 instantiate it, and that lookup should instantiate the enclosing
10404 class. */
10405 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
10406 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
10408 base_list = NULL_TREE;
10409 if (BINFO_N_BASE_BINFOS (pbinfo))
10411 tree pbase_binfo;
10412 tree pushed_scope;
10413 int i;
10415 /* We must enter the scope containing the type, as that is where
10416 the accessibility of types named in dependent bases are
10417 looked up from. */
10418 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
10420 /* Substitute into each of the bases to determine the actual
10421 basetypes. */
10422 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
10424 tree base;
10425 tree access = BINFO_BASE_ACCESS (pbinfo, i);
10426 tree expanded_bases = NULL_TREE;
10427 int idx, len = 1;
10429 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
10431 expanded_bases =
10432 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
10433 args, tf_error, NULL_TREE);
10434 if (expanded_bases == error_mark_node)
10435 continue;
10437 len = TREE_VEC_LENGTH (expanded_bases);
10440 for (idx = 0; idx < len; idx++)
10442 if (expanded_bases)
10443 /* Extract the already-expanded base class. */
10444 base = TREE_VEC_ELT (expanded_bases, idx);
10445 else
10446 /* Substitute to figure out the base class. */
10447 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
10448 NULL_TREE);
10450 if (base == error_mark_node)
10451 continue;
10453 base_list = tree_cons (access, base, base_list);
10454 if (BINFO_VIRTUAL_P (pbase_binfo))
10455 TREE_TYPE (base_list) = integer_type_node;
10459 /* The list is now in reverse order; correct that. */
10460 base_list = nreverse (base_list);
10462 if (pushed_scope)
10463 pop_scope (pushed_scope);
10465 /* Now call xref_basetypes to set up all the base-class
10466 information. */
10467 xref_basetypes (type, base_list);
10469 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
10470 (int) ATTR_FLAG_TYPE_IN_PLACE,
10471 args, tf_error, NULL_TREE);
10472 fixup_attribute_variants (type);
10474 /* Now that our base classes are set up, enter the scope of the
10475 class, so that name lookups into base classes, etc. will work
10476 correctly. This is precisely analogous to what we do in
10477 begin_class_definition when defining an ordinary non-template
10478 class, except we also need to push the enclosing classes. */
10479 push_nested_class (type);
10481 /* Now members are processed in the order of declaration. */
10482 for (member = CLASSTYPE_DECL_LIST (pattern);
10483 member; member = TREE_CHAIN (member))
10485 tree t = TREE_VALUE (member);
10487 if (TREE_PURPOSE (member))
10489 if (TYPE_P (t))
10491 /* Build new CLASSTYPE_NESTED_UTDS. */
10493 tree newtag;
10494 bool class_template_p;
10496 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
10497 && TYPE_LANG_SPECIFIC (t)
10498 && CLASSTYPE_IS_TEMPLATE (t));
10499 /* If the member is a class template, then -- even after
10500 substitution -- there may be dependent types in the
10501 template argument list for the class. We increment
10502 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10503 that function will assume that no types are dependent
10504 when outside of a template. */
10505 if (class_template_p)
10506 ++processing_template_decl;
10507 newtag = tsubst (t, args, tf_error, NULL_TREE);
10508 if (class_template_p)
10509 --processing_template_decl;
10510 if (newtag == error_mark_node)
10511 continue;
10513 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
10515 tree name = TYPE_IDENTIFIER (t);
10517 if (class_template_p)
10518 /* Unfortunately, lookup_template_class sets
10519 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10520 instantiation (i.e., for the type of a member
10521 template class nested within a template class.)
10522 This behavior is required for
10523 maybe_process_partial_specialization to work
10524 correctly, but is not accurate in this case;
10525 the TAG is not an instantiation of anything.
10526 (The corresponding TEMPLATE_DECL is an
10527 instantiation, but the TYPE is not.) */
10528 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
10530 /* Now, we call pushtag to put this NEWTAG into the scope of
10531 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10532 pushtag calling push_template_decl. We don't have to do
10533 this for enums because it will already have been done in
10534 tsubst_enum. */
10535 if (name)
10536 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
10537 pushtag (name, newtag, /*tag_scope=*/ts_current);
10540 else if (DECL_DECLARES_FUNCTION_P (t))
10542 /* Build new TYPE_METHODS. */
10543 tree r;
10545 if (TREE_CODE (t) == TEMPLATE_DECL)
10546 ++processing_template_decl;
10547 r = tsubst (t, args, tf_error, NULL_TREE);
10548 if (TREE_CODE (t) == TEMPLATE_DECL)
10549 --processing_template_decl;
10550 set_current_access_from_decl (r);
10551 finish_member_declaration (r);
10552 /* Instantiate members marked with attribute used. */
10553 if (r != error_mark_node && DECL_PRESERVE_P (r))
10554 mark_used (r);
10555 if (TREE_CODE (r) == FUNCTION_DECL
10556 && DECL_OMP_DECLARE_REDUCTION_P (r))
10557 cp_check_omp_declare_reduction (r);
10559 else if (DECL_CLASS_TEMPLATE_P (t)
10560 && LAMBDA_TYPE_P (TREE_TYPE (t)))
10561 /* A closure type for a lambda in a default argument for a
10562 member template. Ignore it; it will be instantiated with
10563 the default argument. */;
10564 else
10566 /* Build new TYPE_FIELDS. */
10567 if (TREE_CODE (t) == STATIC_ASSERT)
10569 tree condition;
10571 ++c_inhibit_evaluation_warnings;
10572 condition =
10573 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
10574 tf_warning_or_error, NULL_TREE,
10575 /*integral_constant_expression_p=*/true);
10576 --c_inhibit_evaluation_warnings;
10578 finish_static_assert (condition,
10579 STATIC_ASSERT_MESSAGE (t),
10580 STATIC_ASSERT_SOURCE_LOCATION (t),
10581 /*member_p=*/true);
10583 else if (TREE_CODE (t) != CONST_DECL)
10585 tree r;
10586 tree vec = NULL_TREE;
10587 int len = 1;
10589 /* The file and line for this declaration, to
10590 assist in error message reporting. Since we
10591 called push_tinst_level above, we don't need to
10592 restore these. */
10593 input_location = DECL_SOURCE_LOCATION (t);
10595 if (TREE_CODE (t) == TEMPLATE_DECL)
10596 ++processing_template_decl;
10597 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
10598 if (TREE_CODE (t) == TEMPLATE_DECL)
10599 --processing_template_decl;
10601 if (TREE_CODE (r) == TREE_VEC)
10603 /* A capture pack became multiple fields. */
10604 vec = r;
10605 len = TREE_VEC_LENGTH (vec);
10608 for (int i = 0; i < len; ++i)
10610 if (vec)
10611 r = TREE_VEC_ELT (vec, i);
10612 if (VAR_P (r))
10614 /* In [temp.inst]:
10616 [t]he initialization (and any associated
10617 side-effects) of a static data member does
10618 not occur unless the static data member is
10619 itself used in a way that requires the
10620 definition of the static data member to
10621 exist.
10623 Therefore, we do not substitute into the
10624 initialized for the static data member here. */
10625 finish_static_data_member_decl
10627 /*init=*/NULL_TREE,
10628 /*init_const_expr_p=*/false,
10629 /*asmspec_tree=*/NULL_TREE,
10630 /*flags=*/0);
10631 /* Instantiate members marked with attribute used. */
10632 if (r != error_mark_node && DECL_PRESERVE_P (r))
10633 mark_used (r);
10635 else if (TREE_CODE (r) == FIELD_DECL)
10637 /* Determine whether R has a valid type and can be
10638 completed later. If R is invalid, then its type
10639 is replaced by error_mark_node. */
10640 tree rtype = TREE_TYPE (r);
10641 if (can_complete_type_without_circularity (rtype))
10642 complete_type (rtype);
10644 if (!complete_or_array_type_p (rtype))
10646 /* If R's type couldn't be completed and
10647 it isn't a flexible array member (whose
10648 type is incomplete by definition) give
10649 an error. */
10650 cxx_incomplete_type_error (r, rtype);
10651 TREE_TYPE (r) = error_mark_node;
10655 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10656 such a thing will already have been added to the field
10657 list by tsubst_enum in finish_member_declaration in the
10658 CLASSTYPE_NESTED_UTDS case above. */
10659 if (!(TREE_CODE (r) == TYPE_DECL
10660 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
10661 && DECL_ARTIFICIAL (r)))
10663 set_current_access_from_decl (r);
10664 finish_member_declaration (r);
10670 else
10672 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
10673 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10675 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10677 tree friend_type = t;
10678 bool adjust_processing_template_decl = false;
10680 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10682 /* template <class T> friend class C; */
10683 friend_type = tsubst_friend_class (friend_type, args);
10684 adjust_processing_template_decl = true;
10686 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
10688 /* template <class T> friend class C::D; */
10689 friend_type = tsubst (friend_type, args,
10690 tf_warning_or_error, NULL_TREE);
10691 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10692 friend_type = TREE_TYPE (friend_type);
10693 adjust_processing_template_decl = true;
10695 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
10696 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
10698 /* This could be either
10700 friend class T::C;
10702 when dependent_type_p is false or
10704 template <class U> friend class T::C;
10706 otherwise. */
10707 /* Bump processing_template_decl in case this is something like
10708 template <class T> friend struct A<T>::B. */
10709 ++processing_template_decl;
10710 friend_type = tsubst (friend_type, args,
10711 tf_warning_or_error, NULL_TREE);
10712 if (dependent_type_p (friend_type))
10713 adjust_processing_template_decl = true;
10714 --processing_template_decl;
10716 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
10717 && TYPE_HIDDEN_P (friend_type))
10719 /* friend class C;
10721 where C hasn't been declared yet. Let's lookup name
10722 from namespace scope directly, bypassing any name that
10723 come from dependent base class. */
10724 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
10726 /* The call to xref_tag_from_type does injection for friend
10727 classes. */
10728 push_nested_namespace (ns);
10729 friend_type =
10730 xref_tag_from_type (friend_type, NULL_TREE,
10731 /*tag_scope=*/ts_current);
10732 pop_nested_namespace (ns);
10734 else if (uses_template_parms (friend_type))
10735 /* friend class C<T>; */
10736 friend_type = tsubst (friend_type, args,
10737 tf_warning_or_error, NULL_TREE);
10738 /* Otherwise it's
10740 friend class C;
10742 where C is already declared or
10744 friend class C<int>;
10746 We don't have to do anything in these cases. */
10748 if (adjust_processing_template_decl)
10749 /* Trick make_friend_class into realizing that the friend
10750 we're adding is a template, not an ordinary class. It's
10751 important that we use make_friend_class since it will
10752 perform some error-checking and output cross-reference
10753 information. */
10754 ++processing_template_decl;
10756 if (friend_type != error_mark_node)
10757 make_friend_class (type, friend_type, /*complain=*/false);
10759 if (adjust_processing_template_decl)
10760 --processing_template_decl;
10762 else
10764 /* Build new DECL_FRIENDLIST. */
10765 tree r;
10767 /* The file and line for this declaration, to
10768 assist in error message reporting. Since we
10769 called push_tinst_level above, we don't need to
10770 restore these. */
10771 input_location = DECL_SOURCE_LOCATION (t);
10773 if (TREE_CODE (t) == TEMPLATE_DECL)
10775 ++processing_template_decl;
10776 push_deferring_access_checks (dk_no_check);
10779 r = tsubst_friend_function (t, args);
10780 add_friend (type, r, /*complain=*/false);
10781 if (TREE_CODE (t) == TEMPLATE_DECL)
10783 pop_deferring_access_checks ();
10784 --processing_template_decl;
10790 if (fn_context)
10792 /* Restore these before substituting into the lambda capture
10793 initializers. */
10794 cp_unevaluated_operand = saved_unevaluated_operand;
10795 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10798 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
10800 tree decl = lambda_function (type);
10801 if (decl)
10803 if (cxx_dialect >= cxx1z)
10804 CLASSTYPE_LITERAL_P (type) = true;
10806 if (!DECL_TEMPLATE_INFO (decl)
10807 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
10809 /* Set function_depth to avoid garbage collection. */
10810 ++function_depth;
10811 instantiate_decl (decl, /*defer_ok=*/false, false);
10812 --function_depth;
10815 /* We need to instantiate the capture list from the template
10816 after we've instantiated the closure members, but before we
10817 consider adding the conversion op. Also keep any captures
10818 that may have been added during instantiation of the op(). */
10819 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
10820 tree tmpl_cap
10821 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
10822 args, tf_warning_or_error, NULL_TREE,
10823 false, false);
10825 LAMBDA_EXPR_CAPTURE_LIST (expr)
10826 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
10828 maybe_add_lambda_conv_op (type);
10830 else
10831 gcc_assert (errorcount);
10834 /* Set the file and line number information to whatever is given for
10835 the class itself. This puts error messages involving generated
10836 implicit functions at a predictable point, and the same point
10837 that would be used for non-template classes. */
10838 input_location = DECL_SOURCE_LOCATION (typedecl);
10840 unreverse_member_declarations (type);
10841 finish_struct_1 (type);
10842 TYPE_BEING_DEFINED (type) = 0;
10844 /* We don't instantiate default arguments for member functions. 14.7.1:
10846 The implicit instantiation of a class template specialization causes
10847 the implicit instantiation of the declarations, but not of the
10848 definitions or default arguments, of the class member functions,
10849 member classes, static data members and member templates.... */
10851 /* Some typedefs referenced from within the template code need to be access
10852 checked at template instantiation time, i.e now. These types were
10853 added to the template at parsing time. Let's get those and perform
10854 the access checks then. */
10855 perform_typedefs_access_check (pattern, args);
10856 perform_deferred_access_checks (tf_warning_or_error);
10857 pop_nested_class ();
10858 maximum_field_alignment = saved_maximum_field_alignment;
10859 if (!fn_context)
10860 pop_from_top_level ();
10861 pop_deferring_access_checks ();
10862 pop_tinst_level ();
10864 /* The vtable for a template class can be emitted in any translation
10865 unit in which the class is instantiated. When there is no key
10866 method, however, finish_struct_1 will already have added TYPE to
10867 the keyed_classes. */
10868 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
10869 vec_safe_push (keyed_classes, type);
10871 return type;
10874 /* Wrapper for instantiate_class_template_1. */
10876 tree
10877 instantiate_class_template (tree type)
10879 tree ret;
10880 timevar_push (TV_TEMPLATE_INST);
10881 ret = instantiate_class_template_1 (type);
10882 timevar_pop (TV_TEMPLATE_INST);
10883 return ret;
10886 static tree
10887 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10889 tree r;
10891 if (!t)
10892 r = t;
10893 else if (TYPE_P (t))
10894 r = tsubst (t, args, complain, in_decl);
10895 else
10897 if (!(complain & tf_warning))
10898 ++c_inhibit_evaluation_warnings;
10899 r = tsubst_expr (t, args, complain, in_decl,
10900 /*integral_constant_expression_p=*/true);
10901 if (!(complain & tf_warning))
10902 --c_inhibit_evaluation_warnings;
10904 return r;
10907 /* Given a function parameter pack TMPL_PARM and some function parameters
10908 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
10909 and set *SPEC_P to point at the next point in the list. */
10911 tree
10912 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
10914 /* Collect all of the extra "packed" parameters into an
10915 argument pack. */
10916 tree parmvec;
10917 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
10918 tree spec_parm = *spec_p;
10919 int i, len;
10921 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
10922 if (tmpl_parm
10923 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
10924 break;
10926 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
10927 parmvec = make_tree_vec (len);
10928 spec_parm = *spec_p;
10929 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
10930 TREE_VEC_ELT (parmvec, i) = spec_parm;
10932 /* Build the argument packs. */
10933 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
10934 *spec_p = spec_parm;
10936 return argpack;
10939 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
10940 NONTYPE_ARGUMENT_PACK. */
10942 static tree
10943 make_fnparm_pack (tree spec_parm)
10945 return extract_fnparm_pack (NULL_TREE, &spec_parm);
10948 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
10949 pack expansion with no extra args, 2 if it has extra args, or 0
10950 if it is not a pack expansion. */
10952 static int
10953 argument_pack_element_is_expansion_p (tree arg_pack, int i)
10955 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
10956 if (i >= TREE_VEC_LENGTH (vec))
10957 return 0;
10958 tree elt = TREE_VEC_ELT (vec, i);
10959 if (DECL_P (elt))
10960 /* A decl pack is itself an expansion. */
10961 elt = TREE_TYPE (elt);
10962 if (!PACK_EXPANSION_P (elt))
10963 return 0;
10964 if (PACK_EXPANSION_EXTRA_ARGS (elt))
10965 return 2;
10966 return 1;
10970 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
10972 static tree
10973 make_argument_pack_select (tree arg_pack, unsigned index)
10975 tree aps = make_node (ARGUMENT_PACK_SELECT);
10977 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
10978 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
10980 return aps;
10983 /* This is a subroutine of tsubst_pack_expansion.
10985 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
10986 mechanism to store the (non complete list of) arguments of the
10987 substitution and return a non substituted pack expansion, in order
10988 to wait for when we have enough arguments to really perform the
10989 substitution. */
10991 static bool
10992 use_pack_expansion_extra_args_p (tree parm_packs,
10993 int arg_pack_len,
10994 bool has_empty_arg)
10996 /* If one pack has an expansion and another pack has a normal
10997 argument or if one pack has an empty argument and an another
10998 one hasn't then tsubst_pack_expansion cannot perform the
10999 substitution and need to fall back on the
11000 PACK_EXPANSION_EXTRA mechanism. */
11001 if (parm_packs == NULL_TREE)
11002 return false;
11003 else if (has_empty_arg)
11004 return true;
11006 bool has_expansion_arg = false;
11007 for (int i = 0 ; i < arg_pack_len; ++i)
11009 bool has_non_expansion_arg = false;
11010 for (tree parm_pack = parm_packs;
11011 parm_pack;
11012 parm_pack = TREE_CHAIN (parm_pack))
11014 tree arg = TREE_VALUE (parm_pack);
11016 int exp = argument_pack_element_is_expansion_p (arg, i);
11017 if (exp == 2)
11018 /* We can't substitute a pack expansion with extra args into
11019 our pattern. */
11020 return true;
11021 else if (exp)
11022 has_expansion_arg = true;
11023 else
11024 has_non_expansion_arg = true;
11027 if (has_expansion_arg && has_non_expansion_arg)
11028 return true;
11030 return false;
11033 /* [temp.variadic]/6 says that:
11035 The instantiation of a pack expansion [...]
11036 produces a list E1,E2, ..., En, where N is the number of elements
11037 in the pack expansion parameters.
11039 This subroutine of tsubst_pack_expansion produces one of these Ei.
11041 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11042 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11043 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11044 INDEX is the index 'i' of the element Ei to produce. ARGS,
11045 COMPLAIN, and IN_DECL are the same parameters as for the
11046 tsubst_pack_expansion function.
11048 The function returns the resulting Ei upon successful completion,
11049 or error_mark_node.
11051 Note that this function possibly modifies the ARGS parameter, so
11052 it's the responsibility of the caller to restore it. */
11054 static tree
11055 gen_elem_of_pack_expansion_instantiation (tree pattern,
11056 tree parm_packs,
11057 unsigned index,
11058 tree args /* This parm gets
11059 modified. */,
11060 tsubst_flags_t complain,
11061 tree in_decl)
11063 tree t;
11064 bool ith_elem_is_expansion = false;
11066 /* For each parameter pack, change the substitution of the parameter
11067 pack to the ith argument in its argument pack, then expand the
11068 pattern. */
11069 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
11071 tree parm = TREE_PURPOSE (pack);
11072 tree arg_pack = TREE_VALUE (pack);
11073 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
11075 ith_elem_is_expansion |=
11076 argument_pack_element_is_expansion_p (arg_pack, index);
11078 /* Select the Ith argument from the pack. */
11079 if (TREE_CODE (parm) == PARM_DECL
11080 || TREE_CODE (parm) == FIELD_DECL)
11082 if (index == 0)
11084 aps = make_argument_pack_select (arg_pack, index);
11085 if (!mark_used (parm, complain) && !(complain & tf_error))
11086 return error_mark_node;
11087 register_local_specialization (aps, parm);
11089 else
11090 aps = retrieve_local_specialization (parm);
11092 else
11094 int idx, level;
11095 template_parm_level_and_index (parm, &level, &idx);
11097 if (index == 0)
11099 aps = make_argument_pack_select (arg_pack, index);
11100 /* Update the corresponding argument. */
11101 TMPL_ARG (args, level, idx) = aps;
11103 else
11104 /* Re-use the ARGUMENT_PACK_SELECT. */
11105 aps = TMPL_ARG (args, level, idx);
11107 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11110 /* Substitute into the PATTERN with the (possibly altered)
11111 arguments. */
11112 if (pattern == in_decl)
11113 /* Expanding a fixed parameter pack from
11114 coerce_template_parameter_pack. */
11115 t = tsubst_decl (pattern, args, complain);
11116 else if (pattern == error_mark_node)
11117 t = error_mark_node;
11118 else if (constraint_p (pattern))
11120 if (processing_template_decl)
11121 t = tsubst_constraint (pattern, args, complain, in_decl);
11122 else
11123 t = (constraints_satisfied_p (pattern, args)
11124 ? boolean_true_node : boolean_false_node);
11126 else if (!TYPE_P (pattern))
11127 t = tsubst_expr (pattern, args, complain, in_decl,
11128 /*integral_constant_expression_p=*/false);
11129 else
11130 t = tsubst (pattern, args, complain, in_decl);
11132 /* If the Ith argument pack element is a pack expansion, then
11133 the Ith element resulting from the substituting is going to
11134 be a pack expansion as well. */
11135 if (ith_elem_is_expansion)
11136 t = make_pack_expansion (t);
11138 return t;
11141 /* When the unexpanded parameter pack in a fold expression expands to an empty
11142 sequence, the value of the expression is as follows; the program is
11143 ill-formed if the operator is not listed in this table.
11145 && true
11146 || false
11147 , void() */
11149 tree
11150 expand_empty_fold (tree t, tsubst_flags_t complain)
11152 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
11153 if (!FOLD_EXPR_MODIFY_P (t))
11154 switch (code)
11156 case TRUTH_ANDIF_EXPR:
11157 return boolean_true_node;
11158 case TRUTH_ORIF_EXPR:
11159 return boolean_false_node;
11160 case COMPOUND_EXPR:
11161 return void_node;
11162 default:
11163 break;
11166 if (complain & tf_error)
11167 error_at (location_of (t),
11168 "fold of empty expansion over %O", code);
11169 return error_mark_node;
11172 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11173 form an expression that combines the two terms using the
11174 operator of T. */
11176 static tree
11177 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
11179 tree op = FOLD_EXPR_OP (t);
11180 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
11182 // Handle compound assignment operators.
11183 if (FOLD_EXPR_MODIFY_P (t))
11184 return build_x_modify_expr (input_location, left, code, right, complain);
11186 switch (code)
11188 case COMPOUND_EXPR:
11189 return build_x_compound_expr (input_location, left, right, complain);
11190 case DOTSTAR_EXPR:
11191 return build_m_component_ref (left, right, complain);
11192 default:
11193 return build_x_binary_op (input_location, code,
11194 left, TREE_CODE (left),
11195 right, TREE_CODE (right),
11196 /*overload=*/NULL,
11197 complain);
11201 /* Substitute ARGS into the pack of a fold expression T. */
11203 static inline tree
11204 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11206 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
11209 /* Substitute ARGS into the pack of a fold expression T. */
11211 static inline tree
11212 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11214 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
11217 /* Expand a PACK of arguments into a grouped as left fold.
11218 Given a pack containing elements A0, A1, ..., An and an
11219 operator @, this builds the expression:
11221 ((A0 @ A1) @ A2) ... @ An
11223 Note that PACK must not be empty.
11225 The operator is defined by the original fold expression T. */
11227 static tree
11228 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
11230 tree left = TREE_VEC_ELT (pack, 0);
11231 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
11233 tree right = TREE_VEC_ELT (pack, i);
11234 left = fold_expression (t, left, right, complain);
11236 return left;
11239 /* Substitute into a unary left fold expression. */
11241 static tree
11242 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
11243 tree in_decl)
11245 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11246 if (pack == error_mark_node)
11247 return error_mark_node;
11248 if (PACK_EXPANSION_P (pack))
11250 tree r = copy_node (t);
11251 FOLD_EXPR_PACK (r) = pack;
11252 return r;
11254 if (TREE_VEC_LENGTH (pack) == 0)
11255 return expand_empty_fold (t, complain);
11256 else
11257 return expand_left_fold (t, pack, complain);
11260 /* Substitute into a binary left fold expression.
11262 Do ths by building a single (non-empty) vector of argumnts and
11263 building the expression from those elements. */
11265 static tree
11266 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
11267 tree in_decl)
11269 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11270 if (pack == error_mark_node)
11271 return error_mark_node;
11272 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11273 if (init == error_mark_node)
11274 return error_mark_node;
11276 if (PACK_EXPANSION_P (pack))
11278 tree r = copy_node (t);
11279 FOLD_EXPR_PACK (r) = pack;
11280 FOLD_EXPR_INIT (r) = init;
11281 return r;
11284 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
11285 TREE_VEC_ELT (vec, 0) = init;
11286 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
11287 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
11289 return expand_left_fold (t, vec, complain);
11292 /* Expand a PACK of arguments into a grouped as right fold.
11293 Given a pack containing elementns A0, A1, ..., and an
11294 operator @, this builds the expression:
11296 A0@ ... (An-2 @ (An-1 @ An))
11298 Note that PACK must not be empty.
11300 The operator is defined by the original fold expression T. */
11302 tree
11303 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
11305 // Build the expression.
11306 int n = TREE_VEC_LENGTH (pack);
11307 tree right = TREE_VEC_ELT (pack, n - 1);
11308 for (--n; n != 0; --n)
11310 tree left = TREE_VEC_ELT (pack, n - 1);
11311 right = fold_expression (t, left, right, complain);
11313 return right;
11316 /* Substitute into a unary right fold expression. */
11318 static tree
11319 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
11320 tree in_decl)
11322 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11323 if (pack == error_mark_node)
11324 return error_mark_node;
11325 if (PACK_EXPANSION_P (pack))
11327 tree r = copy_node (t);
11328 FOLD_EXPR_PACK (r) = pack;
11329 return r;
11331 if (TREE_VEC_LENGTH (pack) == 0)
11332 return expand_empty_fold (t, complain);
11333 else
11334 return expand_right_fold (t, pack, complain);
11337 /* Substitute into a binary right fold expression.
11339 Do ths by building a single (non-empty) vector of arguments and
11340 building the expression from those elements. */
11342 static tree
11343 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
11344 tree in_decl)
11346 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11347 if (pack == error_mark_node)
11348 return error_mark_node;
11349 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11350 if (init == error_mark_node)
11351 return error_mark_node;
11353 if (PACK_EXPANSION_P (pack))
11355 tree r = copy_node (t);
11356 FOLD_EXPR_PACK (r) = pack;
11357 FOLD_EXPR_INIT (r) = init;
11358 return r;
11361 int n = TREE_VEC_LENGTH (pack);
11362 tree vec = make_tree_vec (n + 1);
11363 for (int i = 0; i < n; ++i)
11364 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
11365 TREE_VEC_ELT (vec, n) = init;
11367 return expand_right_fold (t, vec, complain);
11371 /* Substitute ARGS into T, which is an pack expansion
11372 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11373 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11374 (if only a partial substitution could be performed) or
11375 ERROR_MARK_NODE if there was an error. */
11376 tree
11377 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
11378 tree in_decl)
11380 tree pattern;
11381 tree pack, packs = NULL_TREE;
11382 bool unsubstituted_packs = false;
11383 int i, len = -1;
11384 tree result;
11385 hash_map<tree, tree> *saved_local_specializations = NULL;
11386 bool need_local_specializations = false;
11387 int levels;
11389 gcc_assert (PACK_EXPANSION_P (t));
11390 pattern = PACK_EXPANSION_PATTERN (t);
11392 /* Add in any args remembered from an earlier partial instantiation. */
11393 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
11395 levels = TMPL_ARGS_DEPTH (args);
11397 /* Determine the argument packs that will instantiate the parameter
11398 packs used in the expansion expression. While we're at it,
11399 compute the number of arguments to be expanded and make sure it
11400 is consistent. */
11401 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
11402 pack = TREE_CHAIN (pack))
11404 tree parm_pack = TREE_VALUE (pack);
11405 tree arg_pack = NULL_TREE;
11406 tree orig_arg = NULL_TREE;
11407 int level = 0;
11409 if (TREE_CODE (parm_pack) == BASES)
11411 gcc_assert (parm_pack == pattern);
11412 if (BASES_DIRECT (parm_pack))
11413 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
11414 args, complain, in_decl, false));
11415 else
11416 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
11417 args, complain, in_decl, false));
11419 else if (builtin_pack_call_p (parm_pack))
11421 /* ??? Support use in other patterns. */
11422 gcc_assert (parm_pack == pattern);
11423 return expand_builtin_pack_call (parm_pack, args,
11424 complain, in_decl);
11426 else if (TREE_CODE (parm_pack) == PARM_DECL)
11428 /* We know we have correct local_specializations if this
11429 expansion is at function scope, or if we're dealing with a
11430 local parameter in a requires expression; for the latter,
11431 tsubst_requires_expr set it up appropriately. */
11432 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
11433 arg_pack = retrieve_local_specialization (parm_pack);
11434 else
11435 /* We can't rely on local_specializations for a parameter
11436 name used later in a function declaration (such as in a
11437 late-specified return type). Even if it exists, it might
11438 have the wrong value for a recursive call. */
11439 need_local_specializations = true;
11441 if (!arg_pack)
11443 /* This parameter pack was used in an unevaluated context. Just
11444 make a dummy decl, since it's only used for its type. */
11445 arg_pack = tsubst_decl (parm_pack, args, complain);
11446 if (arg_pack && DECL_PACK_P (arg_pack))
11447 /* Partial instantiation of the parm_pack, we can't build
11448 up an argument pack yet. */
11449 arg_pack = NULL_TREE;
11450 else
11451 arg_pack = make_fnparm_pack (arg_pack);
11454 else if (TREE_CODE (parm_pack) == FIELD_DECL)
11455 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
11456 else
11458 int idx;
11459 template_parm_level_and_index (parm_pack, &level, &idx);
11461 if (level <= levels)
11462 arg_pack = TMPL_ARG (args, level, idx);
11465 orig_arg = arg_pack;
11466 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
11467 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
11469 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
11470 /* This can only happen if we forget to expand an argument
11471 pack somewhere else. Just return an error, silently. */
11473 result = make_tree_vec (1);
11474 TREE_VEC_ELT (result, 0) = error_mark_node;
11475 return result;
11478 if (arg_pack)
11480 int my_len =
11481 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
11483 /* Don't bother trying to do a partial substitution with
11484 incomplete packs; we'll try again after deduction. */
11485 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
11486 return t;
11488 if (len < 0)
11489 len = my_len;
11490 else if (len != my_len)
11492 if (!(complain & tf_error))
11493 /* Fail quietly. */;
11494 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
11495 error ("mismatched argument pack lengths while expanding %qT",
11496 pattern);
11497 else
11498 error ("mismatched argument pack lengths while expanding %qE",
11499 pattern);
11500 return error_mark_node;
11503 /* Keep track of the parameter packs and their corresponding
11504 argument packs. */
11505 packs = tree_cons (parm_pack, arg_pack, packs);
11506 TREE_TYPE (packs) = orig_arg;
11508 else
11510 /* We can't substitute for this parameter pack. We use a flag as
11511 well as the missing_level counter because function parameter
11512 packs don't have a level. */
11513 gcc_assert (processing_template_decl);
11514 unsubstituted_packs = true;
11518 /* If the expansion is just T..., return the matching argument pack, unless
11519 we need to call convert_from_reference on all the elements. This is an
11520 important optimization; see c++/68422. */
11521 if (!unsubstituted_packs
11522 && TREE_PURPOSE (packs) == pattern)
11524 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
11525 /* Types need no adjustment, nor does sizeof..., and if we still have
11526 some pack expansion args we won't do anything yet. */
11527 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
11528 || PACK_EXPANSION_SIZEOF_P (t)
11529 || pack_expansion_args_count (args))
11530 return args;
11531 /* Also optimize expression pack expansions if we can tell that the
11532 elements won't have reference type. */
11533 tree type = TREE_TYPE (pattern);
11534 if (type && TREE_CODE (type) != REFERENCE_TYPE
11535 && !PACK_EXPANSION_P (type)
11536 && !WILDCARD_TYPE_P (type))
11537 return args;
11538 /* Otherwise use the normal path so we get convert_from_reference. */
11541 /* We cannot expand this expansion expression, because we don't have
11542 all of the argument packs we need. */
11543 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
11545 /* We got some full packs, but we can't substitute them in until we
11546 have values for all the packs. So remember these until then. */
11548 t = make_pack_expansion (pattern);
11549 PACK_EXPANSION_EXTRA_ARGS (t) = args;
11550 return t;
11552 else if (unsubstituted_packs)
11554 /* There were no real arguments, we're just replacing a parameter
11555 pack with another version of itself. Substitute into the
11556 pattern and return a PACK_EXPANSION_*. The caller will need to
11557 deal with that. */
11558 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
11559 t = tsubst_expr (pattern, args, complain, in_decl,
11560 /*integral_constant_expression_p=*/false);
11561 else
11562 t = tsubst (pattern, args, complain, in_decl);
11563 t = make_pack_expansion (t);
11564 return t;
11567 gcc_assert (len >= 0);
11569 if (need_local_specializations)
11571 /* We're in a late-specified return type, so create our own local
11572 specializations map; the current map is either NULL or (in the
11573 case of recursive unification) might have bindings that we don't
11574 want to use or alter. */
11575 saved_local_specializations = local_specializations;
11576 local_specializations = new hash_map<tree, tree>;
11579 /* For each argument in each argument pack, substitute into the
11580 pattern. */
11581 result = make_tree_vec (len);
11582 tree elem_args = copy_template_args (args);
11583 for (i = 0; i < len; ++i)
11585 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
11587 elem_args, complain,
11588 in_decl);
11589 TREE_VEC_ELT (result, i) = t;
11590 if (t == error_mark_node)
11592 result = error_mark_node;
11593 break;
11597 /* Update ARGS to restore the substitution from parameter packs to
11598 their argument packs. */
11599 for (pack = packs; pack; pack = TREE_CHAIN (pack))
11601 tree parm = TREE_PURPOSE (pack);
11603 if (TREE_CODE (parm) == PARM_DECL
11604 || TREE_CODE (parm) == FIELD_DECL)
11605 register_local_specialization (TREE_TYPE (pack), parm);
11606 else
11608 int idx, level;
11610 if (TREE_VALUE (pack) == NULL_TREE)
11611 continue;
11613 template_parm_level_and_index (parm, &level, &idx);
11615 /* Update the corresponding argument. */
11616 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
11617 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
11618 TREE_TYPE (pack);
11619 else
11620 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
11624 if (need_local_specializations)
11626 delete local_specializations;
11627 local_specializations = saved_local_specializations;
11630 /* If the dependent pack arguments were such that we end up with only a
11631 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11632 if (len == 1 && TREE_CODE (result) == TREE_VEC
11633 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
11634 return TREE_VEC_ELT (result, 0);
11636 return result;
11639 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11640 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11641 parameter packs; all parms generated from a function parameter pack will
11642 have the same DECL_PARM_INDEX. */
11644 tree
11645 get_pattern_parm (tree parm, tree tmpl)
11647 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
11648 tree patparm;
11650 if (DECL_ARTIFICIAL (parm))
11652 for (patparm = DECL_ARGUMENTS (pattern);
11653 patparm; patparm = DECL_CHAIN (patparm))
11654 if (DECL_ARTIFICIAL (patparm)
11655 && DECL_NAME (parm) == DECL_NAME (patparm))
11656 break;
11658 else
11660 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
11661 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
11662 gcc_assert (DECL_PARM_INDEX (patparm)
11663 == DECL_PARM_INDEX (parm));
11666 return patparm;
11669 /* Make an argument pack out of the TREE_VEC VEC. */
11671 static tree
11672 make_argument_pack (tree vec)
11674 tree pack;
11675 tree elt = TREE_VEC_ELT (vec, 0);
11676 if (TYPE_P (elt))
11677 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
11678 else
11680 pack = make_node (NONTYPE_ARGUMENT_PACK);
11681 TREE_CONSTANT (pack) = 1;
11683 SET_ARGUMENT_PACK_ARGS (pack, vec);
11684 return pack;
11687 /* Return an exact copy of template args T that can be modified
11688 independently. */
11690 static tree
11691 copy_template_args (tree t)
11693 if (t == error_mark_node)
11694 return t;
11696 int len = TREE_VEC_LENGTH (t);
11697 tree new_vec = make_tree_vec (len);
11699 for (int i = 0; i < len; ++i)
11701 tree elt = TREE_VEC_ELT (t, i);
11702 if (elt && TREE_CODE (elt) == TREE_VEC)
11703 elt = copy_template_args (elt);
11704 TREE_VEC_ELT (new_vec, i) = elt;
11707 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
11708 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
11710 return new_vec;
11713 /* Substitute ARGS into the vector or list of template arguments T. */
11715 static tree
11716 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11718 tree orig_t = t;
11719 int len, need_new = 0, i, expanded_len_adjust = 0, out;
11720 tree *elts;
11722 if (t == error_mark_node)
11723 return error_mark_node;
11725 len = TREE_VEC_LENGTH (t);
11726 elts = XALLOCAVEC (tree, len);
11728 for (i = 0; i < len; i++)
11730 tree orig_arg = TREE_VEC_ELT (t, i);
11731 tree new_arg;
11733 if (TREE_CODE (orig_arg) == TREE_VEC)
11734 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
11735 else if (PACK_EXPANSION_P (orig_arg))
11737 /* Substitute into an expansion expression. */
11738 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
11740 if (TREE_CODE (new_arg) == TREE_VEC)
11741 /* Add to the expanded length adjustment the number of
11742 expanded arguments. We subtract one from this
11743 measurement, because the argument pack expression
11744 itself is already counted as 1 in
11745 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
11746 the argument pack is empty. */
11747 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
11749 else if (ARGUMENT_PACK_P (orig_arg))
11751 /* Substitute into each of the arguments. */
11752 new_arg = TYPE_P (orig_arg)
11753 ? cxx_make_type (TREE_CODE (orig_arg))
11754 : make_node (TREE_CODE (orig_arg));
11756 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
11757 args, complain, in_decl);
11758 if (pack_args == error_mark_node)
11759 new_arg = error_mark_node;
11760 else
11761 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
11763 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
11764 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
11766 else
11767 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
11769 if (new_arg == error_mark_node)
11770 return error_mark_node;
11772 elts[i] = new_arg;
11773 if (new_arg != orig_arg)
11774 need_new = 1;
11777 if (!need_new)
11778 return t;
11780 /* Make space for the expanded arguments coming from template
11781 argument packs. */
11782 t = make_tree_vec (len + expanded_len_adjust);
11783 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
11784 arguments for a member template.
11785 In that case each TREE_VEC in ORIG_T represents a level of template
11786 arguments, and ORIG_T won't carry any non defaulted argument count.
11787 It will rather be the nested TREE_VECs that will carry one.
11788 In other words, ORIG_T carries a non defaulted argument count only
11789 if it doesn't contain any nested TREE_VEC. */
11790 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
11792 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
11793 count += expanded_len_adjust;
11794 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
11796 for (i = 0, out = 0; i < len; i++)
11798 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
11799 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
11800 && TREE_CODE (elts[i]) == TREE_VEC)
11802 int idx;
11804 /* Now expand the template argument pack "in place". */
11805 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
11806 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
11808 else
11810 TREE_VEC_ELT (t, out) = elts[i];
11811 out++;
11815 return t;
11818 /* Substitute ARGS into one level PARMS of template parameters. */
11820 static tree
11821 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
11823 if (parms == error_mark_node)
11824 return error_mark_node;
11826 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
11828 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
11830 tree tuple = TREE_VEC_ELT (parms, i);
11832 if (tuple == error_mark_node)
11833 continue;
11835 TREE_VEC_ELT (new_vec, i) =
11836 tsubst_template_parm (tuple, args, complain);
11839 return new_vec;
11842 /* Return the result of substituting ARGS into the template parameters
11843 given by PARMS. If there are m levels of ARGS and m + n levels of
11844 PARMS, then the result will contain n levels of PARMS. For
11845 example, if PARMS is `template <class T> template <class U>
11846 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
11847 result will be `template <int*, double, class V>'. */
11849 static tree
11850 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
11852 tree r = NULL_TREE;
11853 tree* new_parms;
11855 /* When substituting into a template, we must set
11856 PROCESSING_TEMPLATE_DECL as the template parameters may be
11857 dependent if they are based on one-another, and the dependency
11858 predicates are short-circuit outside of templates. */
11859 ++processing_template_decl;
11861 for (new_parms = &r;
11862 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
11863 new_parms = &(TREE_CHAIN (*new_parms)),
11864 parms = TREE_CHAIN (parms))
11866 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
11867 args, complain);
11868 *new_parms =
11869 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
11870 - TMPL_ARGS_DEPTH (args)),
11871 new_vec, NULL_TREE);
11874 --processing_template_decl;
11876 return r;
11879 /* Return the result of substituting ARGS into one template parameter
11880 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
11881 parameter and which TREE_PURPOSE is the default argument of the
11882 template parameter. */
11884 static tree
11885 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
11887 tree default_value, parm_decl;
11889 if (args == NULL_TREE
11890 || t == NULL_TREE
11891 || t == error_mark_node)
11892 return t;
11894 gcc_assert (TREE_CODE (t) == TREE_LIST);
11896 default_value = TREE_PURPOSE (t);
11897 parm_decl = TREE_VALUE (t);
11899 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
11900 if (TREE_CODE (parm_decl) == PARM_DECL
11901 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
11902 parm_decl = error_mark_node;
11903 default_value = tsubst_template_arg (default_value, args,
11904 complain, NULL_TREE);
11906 return build_tree_list (default_value, parm_decl);
11909 /* Substitute the ARGS into the indicated aggregate (or enumeration)
11910 type T. If T is not an aggregate or enumeration type, it is
11911 handled as if by tsubst. IN_DECL is as for tsubst. If
11912 ENTERING_SCOPE is nonzero, T is the context for a template which
11913 we are presently tsubst'ing. Return the substituted value. */
11915 static tree
11916 tsubst_aggr_type (tree t,
11917 tree args,
11918 tsubst_flags_t complain,
11919 tree in_decl,
11920 int entering_scope)
11922 if (t == NULL_TREE)
11923 return NULL_TREE;
11925 switch (TREE_CODE (t))
11927 case RECORD_TYPE:
11928 if (TYPE_PTRMEMFUNC_P (t))
11929 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
11931 /* Fall through. */
11932 case ENUMERAL_TYPE:
11933 case UNION_TYPE:
11934 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
11936 tree argvec;
11937 tree context;
11938 tree r;
11939 int saved_unevaluated_operand;
11940 int saved_inhibit_evaluation_warnings;
11942 /* In "sizeof(X<I>)" we need to evaluate "I". */
11943 saved_unevaluated_operand = cp_unevaluated_operand;
11944 cp_unevaluated_operand = 0;
11945 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11946 c_inhibit_evaluation_warnings = 0;
11948 /* First, determine the context for the type we are looking
11949 up. */
11950 context = TYPE_CONTEXT (t);
11951 if (context && TYPE_P (context))
11953 context = tsubst_aggr_type (context, args, complain,
11954 in_decl, /*entering_scope=*/1);
11955 /* If context is a nested class inside a class template,
11956 it may still need to be instantiated (c++/33959). */
11957 context = complete_type (context);
11960 /* Then, figure out what arguments are appropriate for the
11961 type we are trying to find. For example, given:
11963 template <class T> struct S;
11964 template <class T, class U> void f(T, U) { S<U> su; }
11966 and supposing that we are instantiating f<int, double>,
11967 then our ARGS will be {int, double}, but, when looking up
11968 S we only want {double}. */
11969 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
11970 complain, in_decl);
11971 if (argvec == error_mark_node)
11972 r = error_mark_node;
11973 else
11975 r = lookup_template_class (t, argvec, in_decl, context,
11976 entering_scope, complain);
11977 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
11980 cp_unevaluated_operand = saved_unevaluated_operand;
11981 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
11983 return r;
11985 else
11986 /* This is not a template type, so there's nothing to do. */
11987 return t;
11989 default:
11990 return tsubst (t, args, complain, in_decl);
11994 /* Substitute into the default argument ARG (a default argument for
11995 FN), which has the indicated TYPE. */
11997 tree
11998 tsubst_default_argument (tree fn, tree type, tree arg, tsubst_flags_t complain)
12000 tree saved_class_ptr = NULL_TREE;
12001 tree saved_class_ref = NULL_TREE;
12002 int errs = errorcount + sorrycount;
12004 /* This can happen in invalid code. */
12005 if (TREE_CODE (arg) == DEFAULT_ARG)
12006 return arg;
12008 /* This default argument came from a template. Instantiate the
12009 default argument here, not in tsubst. In the case of
12010 something like:
12012 template <class T>
12013 struct S {
12014 static T t();
12015 void f(T = t());
12018 we must be careful to do name lookup in the scope of S<T>,
12019 rather than in the current class. */
12020 push_access_scope (fn);
12021 /* The "this" pointer is not valid in a default argument. */
12022 if (cfun)
12024 saved_class_ptr = current_class_ptr;
12025 cp_function_chain->x_current_class_ptr = NULL_TREE;
12026 saved_class_ref = current_class_ref;
12027 cp_function_chain->x_current_class_ref = NULL_TREE;
12030 push_deferring_access_checks(dk_no_deferred);
12031 /* The default argument expression may cause implicitly defined
12032 member functions to be synthesized, which will result in garbage
12033 collection. We must treat this situation as if we were within
12034 the body of function so as to avoid collecting live data on the
12035 stack. */
12036 ++function_depth;
12037 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
12038 complain, NULL_TREE,
12039 /*integral_constant_expression_p=*/false);
12040 --function_depth;
12041 pop_deferring_access_checks();
12043 /* Restore the "this" pointer. */
12044 if (cfun)
12046 cp_function_chain->x_current_class_ptr = saved_class_ptr;
12047 cp_function_chain->x_current_class_ref = saved_class_ref;
12050 if (errorcount+sorrycount > errs
12051 && (complain & tf_warning_or_error))
12052 inform (input_location,
12053 " when instantiating default argument for call to %qD", fn);
12055 /* Make sure the default argument is reasonable. */
12056 arg = check_default_argument (type, arg, complain);
12058 pop_access_scope (fn);
12060 return arg;
12063 /* Substitute into all the default arguments for FN. */
12065 static void
12066 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
12068 tree arg;
12069 tree tmpl_args;
12071 tmpl_args = DECL_TI_ARGS (fn);
12073 /* If this function is not yet instantiated, we certainly don't need
12074 its default arguments. */
12075 if (uses_template_parms (tmpl_args))
12076 return;
12077 /* Don't do this again for clones. */
12078 if (DECL_CLONED_FUNCTION_P (fn))
12079 return;
12081 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
12082 arg;
12083 arg = TREE_CHAIN (arg))
12084 if (TREE_PURPOSE (arg))
12085 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
12086 TREE_VALUE (arg),
12087 TREE_PURPOSE (arg),
12088 complain);
12091 /* Substitute the ARGS into the T, which is a _DECL. Return the
12092 result of the substitution. Issue error and warning messages under
12093 control of COMPLAIN. */
12095 static tree
12096 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
12098 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12099 location_t saved_loc;
12100 tree r = NULL_TREE;
12101 tree in_decl = t;
12102 hashval_t hash = 0;
12104 /* Set the filename and linenumber to improve error-reporting. */
12105 saved_loc = input_location;
12106 input_location = DECL_SOURCE_LOCATION (t);
12108 switch (TREE_CODE (t))
12110 case TEMPLATE_DECL:
12112 /* We can get here when processing a member function template,
12113 member class template, or template template parameter. */
12114 tree decl = DECL_TEMPLATE_RESULT (t);
12115 tree spec;
12116 tree tmpl_args;
12117 tree full_args;
12119 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12121 /* Template template parameter is treated here. */
12122 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12123 if (new_type == error_mark_node)
12124 r = error_mark_node;
12125 /* If we get a real template back, return it. This can happen in
12126 the context of most_specialized_partial_spec. */
12127 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
12128 r = new_type;
12129 else
12130 /* The new TEMPLATE_DECL was built in
12131 reduce_template_parm_level. */
12132 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
12133 break;
12136 /* We might already have an instance of this template.
12137 The ARGS are for the surrounding class type, so the
12138 full args contain the tsubst'd args for the context,
12139 plus the innermost args from the template decl. */
12140 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
12141 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
12142 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
12143 /* Because this is a template, the arguments will still be
12144 dependent, even after substitution. If
12145 PROCESSING_TEMPLATE_DECL is not set, the dependency
12146 predicates will short-circuit. */
12147 ++processing_template_decl;
12148 full_args = tsubst_template_args (tmpl_args, args,
12149 complain, in_decl);
12150 --processing_template_decl;
12151 if (full_args == error_mark_node)
12152 RETURN (error_mark_node);
12154 /* If this is a default template template argument,
12155 tsubst might not have changed anything. */
12156 if (full_args == tmpl_args)
12157 RETURN (t);
12159 hash = hash_tmpl_and_args (t, full_args);
12160 spec = retrieve_specialization (t, full_args, hash);
12161 if (spec != NULL_TREE)
12163 r = spec;
12164 break;
12167 /* Make a new template decl. It will be similar to the
12168 original, but will record the current template arguments.
12169 We also create a new function declaration, which is just
12170 like the old one, but points to this new template, rather
12171 than the old one. */
12172 r = copy_decl (t);
12173 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
12174 DECL_CHAIN (r) = NULL_TREE;
12176 // Build new template info linking to the original template decl.
12177 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12179 if (TREE_CODE (decl) == TYPE_DECL
12180 && !TYPE_DECL_ALIAS_P (decl))
12182 tree new_type;
12183 ++processing_template_decl;
12184 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12185 --processing_template_decl;
12186 if (new_type == error_mark_node)
12187 RETURN (error_mark_node);
12189 TREE_TYPE (r) = new_type;
12190 /* For a partial specialization, we need to keep pointing to
12191 the primary template. */
12192 if (!DECL_TEMPLATE_SPECIALIZATION (t))
12193 CLASSTYPE_TI_TEMPLATE (new_type) = r;
12194 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
12195 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
12196 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
12198 else
12200 tree new_decl;
12201 ++processing_template_decl;
12202 new_decl = tsubst (decl, args, complain, in_decl);
12203 --processing_template_decl;
12204 if (new_decl == error_mark_node)
12205 RETURN (error_mark_node);
12207 DECL_TEMPLATE_RESULT (r) = new_decl;
12208 DECL_TI_TEMPLATE (new_decl) = r;
12209 TREE_TYPE (r) = TREE_TYPE (new_decl);
12210 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
12211 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
12214 SET_DECL_IMPLICIT_INSTANTIATION (r);
12215 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
12216 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
12218 /* The template parameters for this new template are all the
12219 template parameters for the old template, except the
12220 outermost level of parameters. */
12221 DECL_TEMPLATE_PARMS (r)
12222 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
12223 complain);
12225 if (PRIMARY_TEMPLATE_P (t))
12226 DECL_PRIMARY_TEMPLATE (r) = r;
12228 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl))
12229 /* Record this non-type partial instantiation. */
12230 register_specialization (r, t,
12231 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
12232 false, hash);
12234 break;
12236 case FUNCTION_DECL:
12238 tree ctx;
12239 tree argvec = NULL_TREE;
12240 tree *friends;
12241 tree gen_tmpl;
12242 tree type;
12243 int member;
12244 int args_depth;
12245 int parms_depth;
12247 /* Nobody should be tsubst'ing into non-template functions. */
12248 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
12250 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
12252 tree spec;
12254 /* If T is not dependent, just return it. */
12255 if (!uses_template_parms (DECL_TI_ARGS (t)))
12256 RETURN (t);
12258 /* Calculate the most general template of which R is a
12259 specialization, and the complete set of arguments used to
12260 specialize R. */
12261 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
12262 argvec = tsubst_template_args (DECL_TI_ARGS
12263 (DECL_TEMPLATE_RESULT
12264 (DECL_TI_TEMPLATE (t))),
12265 args, complain, in_decl);
12266 if (argvec == error_mark_node)
12267 RETURN (error_mark_node);
12269 /* Check to see if we already have this specialization. */
12270 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12271 spec = retrieve_specialization (gen_tmpl, argvec, hash);
12273 if (spec)
12275 r = spec;
12276 break;
12279 /* We can see more levels of arguments than parameters if
12280 there was a specialization of a member template, like
12281 this:
12283 template <class T> struct S { template <class U> void f(); }
12284 template <> template <class U> void S<int>::f(U);
12286 Here, we'll be substituting into the specialization,
12287 because that's where we can find the code we actually
12288 want to generate, but we'll have enough arguments for
12289 the most general template.
12291 We also deal with the peculiar case:
12293 template <class T> struct S {
12294 template <class U> friend void f();
12296 template <class U> void f() {}
12297 template S<int>;
12298 template void f<double>();
12300 Here, the ARGS for the instantiation of will be {int,
12301 double}. But, we only need as many ARGS as there are
12302 levels of template parameters in CODE_PATTERN. We are
12303 careful not to get fooled into reducing the ARGS in
12304 situations like:
12306 template <class T> struct S { template <class U> void f(U); }
12307 template <class T> template <> void S<T>::f(int) {}
12309 which we can spot because the pattern will be a
12310 specialization in this case. */
12311 args_depth = TMPL_ARGS_DEPTH (args);
12312 parms_depth =
12313 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
12314 if (args_depth > parms_depth
12315 && !DECL_TEMPLATE_SPECIALIZATION (t))
12316 args = get_innermost_template_args (args, parms_depth);
12318 else
12320 /* This special case arises when we have something like this:
12322 template <class T> struct S {
12323 friend void f<int>(int, double);
12326 Here, the DECL_TI_TEMPLATE for the friend declaration
12327 will be an IDENTIFIER_NODE. We are being called from
12328 tsubst_friend_function, and we want only to create a
12329 new decl (R) with appropriate types so that we can call
12330 determine_specialization. */
12331 gen_tmpl = NULL_TREE;
12334 if (DECL_CLASS_SCOPE_P (t))
12336 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
12337 member = 2;
12338 else
12339 member = 1;
12340 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
12341 complain, t, /*entering_scope=*/1);
12343 else
12345 member = 0;
12346 ctx = DECL_CONTEXT (t);
12348 type = tsubst (TREE_TYPE (t), args, complain|tf_fndecl_type, in_decl);
12349 if (type == error_mark_node)
12350 RETURN (error_mark_node);
12352 /* If we hit excessive deduction depth, the type is bogus even if
12353 it isn't error_mark_node, so don't build a decl. */
12354 if (excessive_deduction_depth)
12355 RETURN (error_mark_node);
12357 /* We do NOT check for matching decls pushed separately at this
12358 point, as they may not represent instantiations of this
12359 template, and in any case are considered separate under the
12360 discrete model. */
12361 r = copy_decl (t);
12362 DECL_USE_TEMPLATE (r) = 0;
12363 TREE_TYPE (r) = type;
12364 /* Clear out the mangled name and RTL for the instantiation. */
12365 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12366 SET_DECL_RTL (r, NULL);
12367 /* Leave DECL_INITIAL set on deleted instantiations. */
12368 if (!DECL_DELETED_FN (r))
12369 DECL_INITIAL (r) = NULL_TREE;
12370 DECL_CONTEXT (r) = ctx;
12372 /* OpenMP UDRs have the only argument a reference to the declared
12373 type. We want to diagnose if the declared type is a reference,
12374 which is invalid, but as references to references are usually
12375 quietly merged, diagnose it here. */
12376 if (DECL_OMP_DECLARE_REDUCTION_P (t))
12378 tree argtype
12379 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
12380 argtype = tsubst (argtype, args, complain, in_decl);
12381 if (TREE_CODE (argtype) == REFERENCE_TYPE)
12382 error_at (DECL_SOURCE_LOCATION (t),
12383 "reference type %qT in "
12384 "%<#pragma omp declare reduction%>", argtype);
12385 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
12386 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
12387 argtype);
12390 if (member && DECL_CONV_FN_P (r))
12391 /* Type-conversion operator. Reconstruct the name, in
12392 case it's the name of one of the template's parameters. */
12393 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
12395 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
12396 complain, t);
12397 DECL_RESULT (r) = NULL_TREE;
12399 TREE_STATIC (r) = 0;
12400 TREE_PUBLIC (r) = TREE_PUBLIC (t);
12401 DECL_EXTERNAL (r) = 1;
12402 /* If this is an instantiation of a function with internal
12403 linkage, we already know what object file linkage will be
12404 assigned to the instantiation. */
12405 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
12406 DECL_DEFER_OUTPUT (r) = 0;
12407 DECL_CHAIN (r) = NULL_TREE;
12408 DECL_PENDING_INLINE_INFO (r) = 0;
12409 DECL_PENDING_INLINE_P (r) = 0;
12410 DECL_SAVED_TREE (r) = NULL_TREE;
12411 DECL_STRUCT_FUNCTION (r) = NULL;
12412 TREE_USED (r) = 0;
12413 /* We'll re-clone as appropriate in instantiate_template. */
12414 DECL_CLONED_FUNCTION (r) = NULL_TREE;
12416 /* If we aren't complaining now, return on error before we register
12417 the specialization so that we'll complain eventually. */
12418 if ((complain & tf_error) == 0
12419 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
12420 && !grok_op_properties (r, /*complain=*/false))
12421 RETURN (error_mark_node);
12423 /* When instantiating a constrained member, substitute
12424 into the constraints to create a new constraint. */
12425 if (tree ci = get_constraints (t))
12426 if (member)
12428 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
12429 set_constraints (r, ci);
12432 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12433 this in the special friend case mentioned above where
12434 GEN_TMPL is NULL. */
12435 if (gen_tmpl)
12437 DECL_TEMPLATE_INFO (r)
12438 = build_template_info (gen_tmpl, argvec);
12439 SET_DECL_IMPLICIT_INSTANTIATION (r);
12441 tree new_r
12442 = register_specialization (r, gen_tmpl, argvec, false, hash);
12443 if (new_r != r)
12444 /* We instantiated this while substituting into
12445 the type earlier (template/friend54.C). */
12446 RETURN (new_r);
12448 /* We're not supposed to instantiate default arguments
12449 until they are called, for a template. But, for a
12450 declaration like:
12452 template <class T> void f ()
12453 { extern void g(int i = T()); }
12455 we should do the substitution when the template is
12456 instantiated. We handle the member function case in
12457 instantiate_class_template since the default arguments
12458 might refer to other members of the class. */
12459 if (!member
12460 && !PRIMARY_TEMPLATE_P (gen_tmpl)
12461 && !uses_template_parms (argvec))
12462 tsubst_default_arguments (r, complain);
12464 else
12465 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12467 /* Copy the list of befriending classes. */
12468 for (friends = &DECL_BEFRIENDING_CLASSES (r);
12469 *friends;
12470 friends = &TREE_CHAIN (*friends))
12472 *friends = copy_node (*friends);
12473 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
12474 args, complain,
12475 in_decl);
12478 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
12480 maybe_retrofit_in_chrg (r);
12481 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
12482 RETURN (error_mark_node);
12483 /* If this is an instantiation of a member template, clone it.
12484 If it isn't, that'll be handled by
12485 clone_constructors_and_destructors. */
12486 if (PRIMARY_TEMPLATE_P (gen_tmpl))
12487 clone_function_decl (r, /*update_methods=*/false);
12489 else if ((complain & tf_error) != 0
12490 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
12491 && !grok_op_properties (r, /*complain=*/true))
12492 RETURN (error_mark_node);
12494 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
12495 SET_DECL_FRIEND_CONTEXT (r,
12496 tsubst (DECL_FRIEND_CONTEXT (t),
12497 args, complain, in_decl));
12499 /* Possibly limit visibility based on template args. */
12500 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12501 if (DECL_VISIBILITY_SPECIFIED (t))
12503 DECL_VISIBILITY_SPECIFIED (r) = 0;
12504 DECL_ATTRIBUTES (r)
12505 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12507 determine_visibility (r);
12508 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
12509 && !processing_template_decl)
12510 defaulted_late_check (r);
12512 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12513 args, complain, in_decl);
12515 break;
12517 case PARM_DECL:
12519 tree type = NULL_TREE;
12520 int i, len = 1;
12521 tree expanded_types = NULL_TREE;
12522 tree prev_r = NULL_TREE;
12523 tree first_r = NULL_TREE;
12525 if (DECL_PACK_P (t))
12527 /* If there is a local specialization that isn't a
12528 parameter pack, it means that we're doing a "simple"
12529 substitution from inside tsubst_pack_expansion. Just
12530 return the local specialization (which will be a single
12531 parm). */
12532 tree spec = retrieve_local_specialization (t);
12533 if (spec
12534 && TREE_CODE (spec) == PARM_DECL
12535 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
12536 RETURN (spec);
12538 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12539 the parameters in this function parameter pack. */
12540 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12541 complain, in_decl);
12542 if (TREE_CODE (expanded_types) == TREE_VEC)
12544 len = TREE_VEC_LENGTH (expanded_types);
12546 /* Zero-length parameter packs are boring. Just substitute
12547 into the chain. */
12548 if (len == 0)
12549 RETURN (tsubst (TREE_CHAIN (t), args, complain,
12550 TREE_CHAIN (t)));
12552 else
12554 /* All we did was update the type. Make a note of that. */
12555 type = expanded_types;
12556 expanded_types = NULL_TREE;
12560 /* Loop through all of the parameters we'll build. When T is
12561 a function parameter pack, LEN is the number of expanded
12562 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12563 r = NULL_TREE;
12564 for (i = 0; i < len; ++i)
12566 prev_r = r;
12567 r = copy_node (t);
12568 if (DECL_TEMPLATE_PARM_P (t))
12569 SET_DECL_TEMPLATE_PARM_P (r);
12571 if (expanded_types)
12572 /* We're on the Ith parameter of the function parameter
12573 pack. */
12575 /* Get the Ith type. */
12576 type = TREE_VEC_ELT (expanded_types, i);
12578 /* Rename the parameter to include the index. */
12579 DECL_NAME (r)
12580 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12582 else if (!type)
12583 /* We're dealing with a normal parameter. */
12584 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12586 type = type_decays_to (type);
12587 TREE_TYPE (r) = type;
12588 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12590 if (DECL_INITIAL (r))
12592 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
12593 DECL_INITIAL (r) = TREE_TYPE (r);
12594 else
12595 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
12596 complain, in_decl);
12599 DECL_CONTEXT (r) = NULL_TREE;
12601 if (!DECL_TEMPLATE_PARM_P (r))
12602 DECL_ARG_TYPE (r) = type_passed_as (type);
12604 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12605 args, complain, in_decl);
12607 /* Keep track of the first new parameter we
12608 generate. That's what will be returned to the
12609 caller. */
12610 if (!first_r)
12611 first_r = r;
12613 /* Build a proper chain of parameters when substituting
12614 into a function parameter pack. */
12615 if (prev_r)
12616 DECL_CHAIN (prev_r) = r;
12619 /* If cp_unevaluated_operand is set, we're just looking for a
12620 single dummy parameter, so don't keep going. */
12621 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
12622 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
12623 complain, DECL_CHAIN (t));
12625 /* FIRST_R contains the start of the chain we've built. */
12626 r = first_r;
12628 break;
12630 case FIELD_DECL:
12632 tree type = NULL_TREE;
12633 tree vec = NULL_TREE;
12634 tree expanded_types = NULL_TREE;
12635 int len = 1;
12637 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12639 /* This field is a lambda capture pack. Return a TREE_VEC of
12640 the expanded fields to instantiate_class_template_1 and
12641 store them in the specializations hash table as a
12642 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
12643 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12644 complain, in_decl);
12645 if (TREE_CODE (expanded_types) == TREE_VEC)
12647 len = TREE_VEC_LENGTH (expanded_types);
12648 vec = make_tree_vec (len);
12650 else
12652 /* All we did was update the type. Make a note of that. */
12653 type = expanded_types;
12654 expanded_types = NULL_TREE;
12658 for (int i = 0; i < len; ++i)
12660 r = copy_decl (t);
12661 if (expanded_types)
12663 type = TREE_VEC_ELT (expanded_types, i);
12664 DECL_NAME (r)
12665 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12667 else if (!type)
12668 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12670 if (type == error_mark_node)
12671 RETURN (error_mark_node);
12672 TREE_TYPE (r) = type;
12673 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12675 if (DECL_C_BIT_FIELD (r))
12676 /* For bit-fields, DECL_INITIAL gives the number of bits. For
12677 non-bit-fields DECL_INITIAL is a non-static data member
12678 initializer, which gets deferred instantiation. */
12679 DECL_INITIAL (r)
12680 = tsubst_expr (DECL_INITIAL (t), args,
12681 complain, in_decl,
12682 /*integral_constant_expression_p=*/true);
12683 else if (DECL_INITIAL (t))
12685 /* Set up DECL_TEMPLATE_INFO so that we can get at the
12686 NSDMI in perform_member_init. Still set DECL_INITIAL
12687 so that we know there is one. */
12688 DECL_INITIAL (r) = void_node;
12689 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
12690 retrofit_lang_decl (r);
12691 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12693 /* We don't have to set DECL_CONTEXT here; it is set by
12694 finish_member_declaration. */
12695 DECL_CHAIN (r) = NULL_TREE;
12697 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12698 args, complain, in_decl);
12700 if (vec)
12701 TREE_VEC_ELT (vec, i) = r;
12704 if (vec)
12706 r = vec;
12707 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
12708 SET_ARGUMENT_PACK_ARGS (pack, vec);
12709 register_specialization (pack, t, args, false, 0);
12712 break;
12714 case USING_DECL:
12715 /* We reach here only for member using decls. We also need to check
12716 uses_template_parms because DECL_DEPENDENT_P is not set for a
12717 using-declaration that designates a member of the current
12718 instantiation (c++/53549). */
12719 if (DECL_DEPENDENT_P (t)
12720 || uses_template_parms (USING_DECL_SCOPE (t)))
12722 tree scope = USING_DECL_SCOPE (t);
12723 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
12724 if (PACK_EXPANSION_P (scope))
12726 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
12727 int len = TREE_VEC_LENGTH (vec);
12728 r = make_tree_vec (len);
12729 for (int i = 0; i < len; ++i)
12731 tree escope = TREE_VEC_ELT (vec, i);
12732 tree elt = do_class_using_decl (escope, name);
12733 if (!elt)
12735 r = error_mark_node;
12736 break;
12738 else
12740 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
12741 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
12743 TREE_VEC_ELT (r, i) = elt;
12746 else
12748 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
12749 complain, in_decl);
12750 r = do_class_using_decl (inst_scope, name);
12751 if (!r)
12752 r = error_mark_node;
12753 else
12755 TREE_PROTECTED (r) = TREE_PROTECTED (t);
12756 TREE_PRIVATE (r) = TREE_PRIVATE (t);
12760 else
12762 r = copy_node (t);
12763 DECL_CHAIN (r) = NULL_TREE;
12765 break;
12767 case TYPE_DECL:
12768 case VAR_DECL:
12770 tree argvec = NULL_TREE;
12771 tree gen_tmpl = NULL_TREE;
12772 tree spec;
12773 tree tmpl = NULL_TREE;
12774 tree ctx;
12775 tree type = NULL_TREE;
12776 bool local_p;
12778 if (TREE_TYPE (t) == error_mark_node)
12779 RETURN (error_mark_node);
12781 if (TREE_CODE (t) == TYPE_DECL
12782 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
12784 /* If this is the canonical decl, we don't have to
12785 mess with instantiations, and often we can't (for
12786 typename, template type parms and such). Note that
12787 TYPE_NAME is not correct for the above test if
12788 we've copied the type for a typedef. */
12789 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12790 if (type == error_mark_node)
12791 RETURN (error_mark_node);
12792 r = TYPE_NAME (type);
12793 break;
12796 /* Check to see if we already have the specialization we
12797 need. */
12798 spec = NULL_TREE;
12799 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
12801 /* T is a static data member or namespace-scope entity.
12802 We have to substitute into namespace-scope variables
12803 (not just variable templates) because of cases like:
12805 template <class T> void f() { extern T t; }
12807 where the entity referenced is not known until
12808 instantiation time. */
12809 local_p = false;
12810 ctx = DECL_CONTEXT (t);
12811 if (DECL_CLASS_SCOPE_P (t))
12813 ctx = tsubst_aggr_type (ctx, args,
12814 complain,
12815 in_decl, /*entering_scope=*/1);
12816 /* If CTX is unchanged, then T is in fact the
12817 specialization we want. That situation occurs when
12818 referencing a static data member within in its own
12819 class. We can use pointer equality, rather than
12820 same_type_p, because DECL_CONTEXT is always
12821 canonical... */
12822 if (ctx == DECL_CONTEXT (t)
12823 /* ... unless T is a member template; in which
12824 case our caller can be willing to create a
12825 specialization of that template represented
12826 by T. */
12827 && !(DECL_TI_TEMPLATE (t)
12828 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
12829 spec = t;
12832 if (!spec)
12834 tmpl = DECL_TI_TEMPLATE (t);
12835 gen_tmpl = most_general_template (tmpl);
12836 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
12837 if (argvec != error_mark_node)
12838 argvec = (coerce_innermost_template_parms
12839 (DECL_TEMPLATE_PARMS (gen_tmpl),
12840 argvec, t, complain,
12841 /*all*/true, /*defarg*/true));
12842 if (argvec == error_mark_node)
12843 RETURN (error_mark_node);
12844 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12845 spec = retrieve_specialization (gen_tmpl, argvec, hash);
12848 else
12850 /* A local variable. */
12851 local_p = true;
12852 /* Subsequent calls to pushdecl will fill this in. */
12853 ctx = NULL_TREE;
12854 /* Unless this is a reference to a static variable from an
12855 enclosing function, in which case we need to fill it in now. */
12856 if (TREE_STATIC (t))
12858 tree fn = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
12859 if (fn != current_function_decl)
12860 ctx = fn;
12862 spec = retrieve_local_specialization (t);
12864 /* If we already have the specialization we need, there is
12865 nothing more to do. */
12866 if (spec)
12868 r = spec;
12869 break;
12872 /* Create a new node for the specialization we need. */
12873 r = copy_decl (t);
12874 if (type == NULL_TREE)
12876 if (is_typedef_decl (t))
12877 type = DECL_ORIGINAL_TYPE (t);
12878 else
12879 type = TREE_TYPE (t);
12880 if (VAR_P (t)
12881 && VAR_HAD_UNKNOWN_BOUND (t)
12882 && type != error_mark_node)
12883 type = strip_array_domain (type);
12884 type = tsubst (type, args, complain, in_decl);
12886 if (VAR_P (r))
12888 /* Even if the original location is out of scope, the
12889 newly substituted one is not. */
12890 DECL_DEAD_FOR_LOCAL (r) = 0;
12891 DECL_INITIALIZED_P (r) = 0;
12892 DECL_TEMPLATE_INSTANTIATED (r) = 0;
12893 if (type == error_mark_node)
12894 RETURN (error_mark_node);
12895 if (TREE_CODE (type) == FUNCTION_TYPE)
12897 /* It may seem that this case cannot occur, since:
12899 typedef void f();
12900 void g() { f x; }
12902 declares a function, not a variable. However:
12904 typedef void f();
12905 template <typename T> void g() { T t; }
12906 template void g<f>();
12908 is an attempt to declare a variable with function
12909 type. */
12910 error ("variable %qD has function type",
12911 /* R is not yet sufficiently initialized, so we
12912 just use its name. */
12913 DECL_NAME (r));
12914 RETURN (error_mark_node);
12916 type = complete_type (type);
12917 /* Wait until cp_finish_decl to set this again, to handle
12918 circular dependency (template/instantiate6.C). */
12919 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
12920 type = check_var_type (DECL_NAME (r), type);
12922 if (DECL_HAS_VALUE_EXPR_P (t))
12924 tree ve = DECL_VALUE_EXPR (t);
12925 ve = tsubst_expr (ve, args, complain, in_decl,
12926 /*constant_expression_p=*/false);
12927 if (REFERENCE_REF_P (ve))
12929 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
12930 ve = TREE_OPERAND (ve, 0);
12932 SET_DECL_VALUE_EXPR (r, ve);
12934 if (CP_DECL_THREAD_LOCAL_P (r)
12935 && !processing_template_decl)
12936 set_decl_tls_model (r, decl_default_tls_model (r));
12938 else if (DECL_SELF_REFERENCE_P (t))
12939 SET_DECL_SELF_REFERENCE_P (r);
12940 TREE_TYPE (r) = type;
12941 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12942 DECL_CONTEXT (r) = ctx;
12943 /* Clear out the mangled name and RTL for the instantiation. */
12944 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12945 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
12946 SET_DECL_RTL (r, NULL);
12947 /* The initializer must not be expanded until it is required;
12948 see [temp.inst]. */
12949 DECL_INITIAL (r) = NULL_TREE;
12950 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
12951 if (VAR_P (r))
12953 SET_DECL_MODE (r, VOIDmode);
12955 /* Possibly limit visibility based on template args. */
12956 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12957 if (DECL_VISIBILITY_SPECIFIED (t))
12959 DECL_VISIBILITY_SPECIFIED (r) = 0;
12960 DECL_ATTRIBUTES (r)
12961 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12963 determine_visibility (r);
12966 if (!local_p)
12968 /* A static data member declaration is always marked
12969 external when it is declared in-class, even if an
12970 initializer is present. We mimic the non-template
12971 processing here. */
12972 DECL_EXTERNAL (r) = 1;
12973 if (DECL_NAMESPACE_SCOPE_P (t))
12974 DECL_NOT_REALLY_EXTERN (r) = 1;
12976 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
12977 SET_DECL_IMPLICIT_INSTANTIATION (r);
12978 register_specialization (r, gen_tmpl, argvec, false, hash);
12980 else
12982 if (DECL_LANG_SPECIFIC (r))
12983 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12984 if (!cp_unevaluated_operand)
12985 register_local_specialization (r, t);
12988 DECL_CHAIN (r) = NULL_TREE;
12990 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
12991 /*flags=*/0,
12992 args, complain, in_decl);
12994 /* Preserve a typedef that names a type. */
12995 if (is_typedef_decl (r) && type != error_mark_node)
12997 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
12998 set_underlying_type (r);
12999 if (TYPE_DECL_ALIAS_P (r))
13000 /* An alias template specialization can be dependent
13001 even if its underlying type is not. */
13002 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
13005 layout_decl (r, 0);
13007 break;
13009 default:
13010 gcc_unreachable ();
13012 #undef RETURN
13014 out:
13015 /* Restore the file and line information. */
13016 input_location = saved_loc;
13018 return r;
13021 /* Substitute into the ARG_TYPES of a function type.
13022 If END is a TREE_CHAIN, leave it and any following types
13023 un-substituted. */
13025 static tree
13026 tsubst_arg_types (tree arg_types,
13027 tree args,
13028 tree end,
13029 tsubst_flags_t complain,
13030 tree in_decl)
13032 tree remaining_arg_types;
13033 tree type = NULL_TREE;
13034 int i = 1;
13035 tree expanded_args = NULL_TREE;
13036 tree default_arg;
13038 if (!arg_types || arg_types == void_list_node || arg_types == end)
13039 return arg_types;
13041 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
13042 args, end, complain, in_decl);
13043 if (remaining_arg_types == error_mark_node)
13044 return error_mark_node;
13046 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
13048 /* For a pack expansion, perform substitution on the
13049 entire expression. Later on, we'll handle the arguments
13050 one-by-one. */
13051 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
13052 args, complain, in_decl);
13054 if (TREE_CODE (expanded_args) == TREE_VEC)
13055 /* So that we'll spin through the parameters, one by one. */
13056 i = TREE_VEC_LENGTH (expanded_args);
13057 else
13059 /* We only partially substituted into the parameter
13060 pack. Our type is TYPE_PACK_EXPANSION. */
13061 type = expanded_args;
13062 expanded_args = NULL_TREE;
13066 while (i > 0) {
13067 --i;
13069 if (expanded_args)
13070 type = TREE_VEC_ELT (expanded_args, i);
13071 else if (!type)
13072 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
13074 if (type == error_mark_node)
13075 return error_mark_node;
13076 if (VOID_TYPE_P (type))
13078 if (complain & tf_error)
13080 error ("invalid parameter type %qT", type);
13081 if (in_decl)
13082 error ("in declaration %q+D", in_decl);
13084 return error_mark_node;
13086 /* DR 657. */
13087 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
13088 return error_mark_node;
13090 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13091 top-level qualifiers as required. */
13092 type = cv_unqualified (type_decays_to (type));
13094 /* We do not substitute into default arguments here. The standard
13095 mandates that they be instantiated only when needed, which is
13096 done in build_over_call. */
13097 default_arg = TREE_PURPOSE (arg_types);
13099 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
13101 /* We've instantiated a template before its default arguments
13102 have been parsed. This can happen for a nested template
13103 class, and is not an error unless we require the default
13104 argument in a call of this function. */
13105 remaining_arg_types =
13106 tree_cons (default_arg, type, remaining_arg_types);
13107 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
13109 else
13110 remaining_arg_types =
13111 hash_tree_cons (default_arg, type, remaining_arg_types);
13114 return remaining_arg_types;
13117 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13118 *not* handle the exception-specification for FNTYPE, because the
13119 initial substitution of explicitly provided template parameters
13120 during argument deduction forbids substitution into the
13121 exception-specification:
13123 [temp.deduct]
13125 All references in the function type of the function template to the
13126 corresponding template parameters are replaced by the specified tem-
13127 plate argument values. If a substitution in a template parameter or
13128 in the function type of the function template results in an invalid
13129 type, type deduction fails. [Note: The equivalent substitution in
13130 exception specifications is done only when the function is instanti-
13131 ated, at which point a program is ill-formed if the substitution
13132 results in an invalid type.] */
13134 static tree
13135 tsubst_function_type (tree t,
13136 tree args,
13137 tsubst_flags_t complain,
13138 tree in_decl)
13140 tree return_type;
13141 tree arg_types = NULL_TREE;
13142 tree fntype;
13144 /* The TYPE_CONTEXT is not used for function/method types. */
13145 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
13147 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13148 failure. */
13149 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13151 if (late_return_type_p)
13153 /* Substitute the argument types. */
13154 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13155 complain, in_decl);
13156 if (arg_types == error_mark_node)
13157 return error_mark_node;
13159 tree save_ccp = current_class_ptr;
13160 tree save_ccr = current_class_ref;
13161 tree this_type = (TREE_CODE (t) == METHOD_TYPE
13162 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
13163 bool do_inject = this_type && CLASS_TYPE_P (this_type);
13164 if (do_inject)
13166 /* DR 1207: 'this' is in scope in the trailing return type. */
13167 inject_this_parameter (this_type, cp_type_quals (this_type));
13170 /* Substitute the return type. */
13171 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13173 if (do_inject)
13175 current_class_ptr = save_ccp;
13176 current_class_ref = save_ccr;
13179 else
13180 /* Substitute the return type. */
13181 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13183 if (return_type == error_mark_node)
13184 return error_mark_node;
13185 /* DR 486 clarifies that creation of a function type with an
13186 invalid return type is a deduction failure. */
13187 if (TREE_CODE (return_type) == ARRAY_TYPE
13188 || TREE_CODE (return_type) == FUNCTION_TYPE)
13190 if (complain & tf_error)
13192 if (TREE_CODE (return_type) == ARRAY_TYPE)
13193 error ("function returning an array");
13194 else
13195 error ("function returning a function");
13197 return error_mark_node;
13199 /* And DR 657. */
13200 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
13201 return error_mark_node;
13203 if (!late_return_type_p)
13205 /* Substitute the argument types. */
13206 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13207 complain, in_decl);
13208 if (arg_types == error_mark_node)
13209 return error_mark_node;
13212 /* Construct a new type node and return it. */
13213 if (TREE_CODE (t) == FUNCTION_TYPE)
13215 fntype = build_function_type (return_type, arg_types);
13216 fntype = apply_memfn_quals (fntype,
13217 type_memfn_quals (t),
13218 type_memfn_rqual (t));
13220 else
13222 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13223 /* Don't pick up extra function qualifiers from the basetype. */
13224 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13225 if (! MAYBE_CLASS_TYPE_P (r))
13227 /* [temp.deduct]
13229 Type deduction may fail for any of the following
13230 reasons:
13232 -- Attempting to create "pointer to member of T" when T
13233 is not a class type. */
13234 if (complain & tf_error)
13235 error ("creating pointer to member function of non-class type %qT",
13237 return error_mark_node;
13240 fntype = build_method_type_directly (r, return_type,
13241 TREE_CHAIN (arg_types));
13242 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
13244 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
13246 if (late_return_type_p)
13247 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
13249 return fntype;
13252 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
13253 ARGS into that specification, and return the substituted
13254 specification. If there is no specification, return NULL_TREE. */
13256 static tree
13257 tsubst_exception_specification (tree fntype,
13258 tree args,
13259 tsubst_flags_t complain,
13260 tree in_decl,
13261 bool defer_ok)
13263 tree specs;
13264 tree new_specs;
13266 specs = TYPE_RAISES_EXCEPTIONS (fntype);
13267 new_specs = NULL_TREE;
13268 if (specs && TREE_PURPOSE (specs))
13270 /* A noexcept-specifier. */
13271 tree expr = TREE_PURPOSE (specs);
13272 if (TREE_CODE (expr) == INTEGER_CST)
13273 new_specs = expr;
13274 else if (defer_ok)
13276 /* Defer instantiation of noexcept-specifiers to avoid
13277 excessive instantiations (c++/49107). */
13278 new_specs = make_node (DEFERRED_NOEXCEPT);
13279 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
13281 /* We already partially instantiated this member template,
13282 so combine the new args with the old. */
13283 DEFERRED_NOEXCEPT_PATTERN (new_specs)
13284 = DEFERRED_NOEXCEPT_PATTERN (expr);
13285 DEFERRED_NOEXCEPT_ARGS (new_specs)
13286 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
13288 else
13290 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
13291 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
13294 else
13295 new_specs = tsubst_copy_and_build
13296 (expr, args, complain, in_decl, /*function_p=*/false,
13297 /*integral_constant_expression_p=*/true);
13298 new_specs = build_noexcept_spec (new_specs, complain);
13300 else if (specs)
13302 if (! TREE_VALUE (specs))
13303 new_specs = specs;
13304 else
13305 while (specs)
13307 tree spec;
13308 int i, len = 1;
13309 tree expanded_specs = NULL_TREE;
13311 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
13313 /* Expand the pack expansion type. */
13314 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
13315 args, complain,
13316 in_decl);
13318 if (expanded_specs == error_mark_node)
13319 return error_mark_node;
13320 else if (TREE_CODE (expanded_specs) == TREE_VEC)
13321 len = TREE_VEC_LENGTH (expanded_specs);
13322 else
13324 /* We're substituting into a member template, so
13325 we got a TYPE_PACK_EXPANSION back. Add that
13326 expansion and move on. */
13327 gcc_assert (TREE_CODE (expanded_specs)
13328 == TYPE_PACK_EXPANSION);
13329 new_specs = add_exception_specifier (new_specs,
13330 expanded_specs,
13331 complain);
13332 specs = TREE_CHAIN (specs);
13333 continue;
13337 for (i = 0; i < len; ++i)
13339 if (expanded_specs)
13340 spec = TREE_VEC_ELT (expanded_specs, i);
13341 else
13342 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
13343 if (spec == error_mark_node)
13344 return spec;
13345 new_specs = add_exception_specifier (new_specs, spec,
13346 complain);
13349 specs = TREE_CHAIN (specs);
13352 return new_specs;
13355 /* Take the tree structure T and replace template parameters used
13356 therein with the argument vector ARGS. IN_DECL is an associated
13357 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
13358 Issue error and warning messages under control of COMPLAIN. Note
13359 that we must be relatively non-tolerant of extensions here, in
13360 order to preserve conformance; if we allow substitutions that
13361 should not be allowed, we may allow argument deductions that should
13362 not succeed, and therefore report ambiguous overload situations
13363 where there are none. In theory, we could allow the substitution,
13364 but indicate that it should have failed, and allow our caller to
13365 make sure that the right thing happens, but we don't try to do this
13366 yet.
13368 This function is used for dealing with types, decls and the like;
13369 for expressions, use tsubst_expr or tsubst_copy. */
13371 tree
13372 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13374 enum tree_code code;
13375 tree type, r = NULL_TREE;
13377 if (t == NULL_TREE || t == error_mark_node
13378 || t == integer_type_node
13379 || t == void_type_node
13380 || t == char_type_node
13381 || t == unknown_type_node
13382 || TREE_CODE (t) == NAMESPACE_DECL
13383 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
13384 return t;
13386 if (DECL_P (t))
13387 return tsubst_decl (t, args, complain);
13389 if (args == NULL_TREE)
13390 return t;
13392 code = TREE_CODE (t);
13394 if (code == IDENTIFIER_NODE)
13395 type = IDENTIFIER_TYPE_VALUE (t);
13396 else
13397 type = TREE_TYPE (t);
13399 gcc_assert (type != unknown_type_node);
13401 /* Reuse typedefs. We need to do this to handle dependent attributes,
13402 such as attribute aligned. */
13403 if (TYPE_P (t)
13404 && typedef_variant_p (t))
13406 tree decl = TYPE_NAME (t);
13408 if (alias_template_specialization_p (t))
13410 /* DECL represents an alias template and we want to
13411 instantiate it. */
13412 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13413 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13414 r = instantiate_alias_template (tmpl, gen_args, complain);
13416 else if (DECL_CLASS_SCOPE_P (decl)
13417 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
13418 && uses_template_parms (DECL_CONTEXT (decl)))
13420 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13421 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13422 r = retrieve_specialization (tmpl, gen_args, 0);
13424 else if (DECL_FUNCTION_SCOPE_P (decl)
13425 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
13426 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
13427 r = retrieve_local_specialization (decl);
13428 else
13429 /* The typedef is from a non-template context. */
13430 return t;
13432 if (r)
13434 r = TREE_TYPE (r);
13435 r = cp_build_qualified_type_real
13436 (r, cp_type_quals (t) | cp_type_quals (r),
13437 complain | tf_ignore_bad_quals);
13438 return r;
13440 else
13442 /* We don't have an instantiation yet, so drop the typedef. */
13443 int quals = cp_type_quals (t);
13444 t = DECL_ORIGINAL_TYPE (decl);
13445 t = cp_build_qualified_type_real (t, quals,
13446 complain | tf_ignore_bad_quals);
13450 bool fndecl_type = (complain & tf_fndecl_type);
13451 complain &= ~tf_fndecl_type;
13453 if (type
13454 && code != TYPENAME_TYPE
13455 && code != TEMPLATE_TYPE_PARM
13456 && code != TEMPLATE_PARM_INDEX
13457 && code != IDENTIFIER_NODE
13458 && code != FUNCTION_TYPE
13459 && code != METHOD_TYPE)
13460 type = tsubst (type, args, complain, in_decl);
13461 if (type == error_mark_node)
13462 return error_mark_node;
13464 switch (code)
13466 case RECORD_TYPE:
13467 case UNION_TYPE:
13468 case ENUMERAL_TYPE:
13469 return tsubst_aggr_type (t, args, complain, in_decl,
13470 /*entering_scope=*/0);
13472 case ERROR_MARK:
13473 case IDENTIFIER_NODE:
13474 case VOID_TYPE:
13475 case REAL_TYPE:
13476 case COMPLEX_TYPE:
13477 case VECTOR_TYPE:
13478 case BOOLEAN_TYPE:
13479 case NULLPTR_TYPE:
13480 case LANG_TYPE:
13481 return t;
13483 case INTEGER_TYPE:
13484 if (t == integer_type_node)
13485 return t;
13487 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
13488 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
13489 return t;
13492 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
13494 max = tsubst_expr (omax, args, complain, in_decl,
13495 /*integral_constant_expression_p=*/false);
13497 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13498 needed. */
13499 if (TREE_CODE (max) == NOP_EXPR
13500 && TREE_SIDE_EFFECTS (omax)
13501 && !TREE_TYPE (max))
13502 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
13504 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13505 with TREE_SIDE_EFFECTS that indicates this is not an integral
13506 constant expression. */
13507 if (processing_template_decl
13508 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
13510 gcc_assert (TREE_CODE (max) == NOP_EXPR);
13511 TREE_SIDE_EFFECTS (max) = 1;
13514 return compute_array_index_type (NULL_TREE, max, complain);
13517 case TEMPLATE_TYPE_PARM:
13518 case TEMPLATE_TEMPLATE_PARM:
13519 case BOUND_TEMPLATE_TEMPLATE_PARM:
13520 case TEMPLATE_PARM_INDEX:
13522 int idx;
13523 int level;
13524 int levels;
13525 tree arg = NULL_TREE;
13527 /* Early in template argument deduction substitution, we don't
13528 want to reduce the level of 'auto', or it will be confused
13529 with a normal template parm in subsequent deduction. */
13530 if (is_auto (t) && (complain & tf_partial))
13531 return t;
13533 r = NULL_TREE;
13535 gcc_assert (TREE_VEC_LENGTH (args) > 0);
13536 template_parm_level_and_index (t, &level, &idx);
13538 levels = TMPL_ARGS_DEPTH (args);
13539 if (level <= levels
13540 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
13542 arg = TMPL_ARG (args, level, idx);
13544 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
13546 /* See through ARGUMENT_PACK_SELECT arguments. */
13547 arg = ARGUMENT_PACK_SELECT_ARG (arg);
13548 /* If the selected argument is an expansion E, that most
13549 likely means we were called from
13550 gen_elem_of_pack_expansion_instantiation during the
13551 substituting of pack an argument pack (which Ith
13552 element is a pack expansion, where I is
13553 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
13554 In this case, the Ith element resulting from this
13555 substituting is going to be a pack expansion, which
13556 pattern is the pattern of E. Let's return the
13557 pattern of E, and
13558 gen_elem_of_pack_expansion_instantiation will
13559 build the resulting pack expansion from it. */
13560 if (PACK_EXPANSION_P (arg))
13562 /* Make sure we aren't throwing away arg info. */
13563 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
13564 arg = PACK_EXPANSION_PATTERN (arg);
13569 if (arg == error_mark_node)
13570 return error_mark_node;
13571 else if (arg != NULL_TREE)
13573 if (ARGUMENT_PACK_P (arg))
13574 /* If ARG is an argument pack, we don't actually want to
13575 perform a substitution here, because substitutions
13576 for argument packs are only done
13577 element-by-element. We can get to this point when
13578 substituting the type of a non-type template
13579 parameter pack, when that type actually contains
13580 template parameter packs from an outer template, e.g.,
13582 template<typename... Types> struct A {
13583 template<Types... Values> struct B { };
13584 }; */
13585 return t;
13587 if (code == TEMPLATE_TYPE_PARM)
13589 int quals;
13590 gcc_assert (TYPE_P (arg));
13592 quals = cp_type_quals (arg) | cp_type_quals (t);
13594 return cp_build_qualified_type_real
13595 (arg, quals, complain | tf_ignore_bad_quals);
13597 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13599 /* We are processing a type constructed from a
13600 template template parameter. */
13601 tree argvec = tsubst (TYPE_TI_ARGS (t),
13602 args, complain, in_decl);
13603 if (argvec == error_mark_node)
13604 return error_mark_node;
13606 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
13607 || TREE_CODE (arg) == TEMPLATE_DECL
13608 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
13610 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
13611 /* Consider this code:
13613 template <template <class> class Template>
13614 struct Internal {
13615 template <class Arg> using Bind = Template<Arg>;
13618 template <template <class> class Template, class Arg>
13619 using Instantiate = Template<Arg>; //#0
13621 template <template <class> class Template,
13622 class Argument>
13623 using Bind =
13624 Instantiate<Internal<Template>::template Bind,
13625 Argument>; //#1
13627 When #1 is parsed, the
13628 BOUND_TEMPLATE_TEMPLATE_PARM representing the
13629 parameter `Template' in #0 matches the
13630 UNBOUND_CLASS_TEMPLATE representing the argument
13631 `Internal<Template>::template Bind'; We then want
13632 to assemble the type `Bind<Argument>' that can't
13633 be fully created right now, because
13634 `Internal<Template>' not being complete, the Bind
13635 template cannot be looked up in that context. So
13636 we need to "store" `Bind<Argument>' for later
13637 when the context of Bind becomes complete. Let's
13638 store that in a TYPENAME_TYPE. */
13639 return make_typename_type (TYPE_CONTEXT (arg),
13640 build_nt (TEMPLATE_ID_EXPR,
13641 TYPE_IDENTIFIER (arg),
13642 argvec),
13643 typename_type,
13644 complain);
13646 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
13647 are resolving nested-types in the signature of a
13648 member function templates. Otherwise ARG is a
13649 TEMPLATE_DECL and is the real template to be
13650 instantiated. */
13651 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
13652 arg = TYPE_NAME (arg);
13654 r = lookup_template_class (arg,
13655 argvec, in_decl,
13656 DECL_CONTEXT (arg),
13657 /*entering_scope=*/0,
13658 complain);
13659 return cp_build_qualified_type_real
13660 (r, cp_type_quals (t) | cp_type_quals (r), complain);
13662 else if (code == TEMPLATE_TEMPLATE_PARM)
13663 return arg;
13664 else
13665 /* TEMPLATE_PARM_INDEX. */
13666 return convert_from_reference (unshare_expr (arg));
13669 if (level == 1)
13670 /* This can happen during the attempted tsubst'ing in
13671 unify. This means that we don't yet have any information
13672 about the template parameter in question. */
13673 return t;
13675 /* If we get here, we must have been looking at a parm for a
13676 more deeply nested template. Make a new version of this
13677 template parameter, but with a lower level. */
13678 switch (code)
13680 case TEMPLATE_TYPE_PARM:
13681 case TEMPLATE_TEMPLATE_PARM:
13682 case BOUND_TEMPLATE_TEMPLATE_PARM:
13683 if (cp_type_quals (t))
13685 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
13686 r = cp_build_qualified_type_real
13687 (r, cp_type_quals (t),
13688 complain | (code == TEMPLATE_TYPE_PARM
13689 ? tf_ignore_bad_quals : 0));
13691 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
13692 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
13693 && (r = (TEMPLATE_PARM_DESCENDANTS
13694 (TEMPLATE_TYPE_PARM_INDEX (t))))
13695 && (r = TREE_TYPE (r))
13696 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
13697 /* Break infinite recursion when substituting the constraints
13698 of a constrained placeholder. */;
13699 else
13701 r = copy_type (t);
13702 TEMPLATE_TYPE_PARM_INDEX (r)
13703 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
13704 r, levels, args, complain);
13705 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
13706 TYPE_MAIN_VARIANT (r) = r;
13707 TYPE_POINTER_TO (r) = NULL_TREE;
13708 TYPE_REFERENCE_TO (r) = NULL_TREE;
13710 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
13712 /* Propagate constraints on placeholders. */
13713 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
13714 PLACEHOLDER_TYPE_CONSTRAINTS (r)
13715 = tsubst_constraint (constr, args, complain, in_decl);
13716 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
13718 if (DECL_TEMPLATE_TEMPLATE_PARM_P (pl))
13719 pl = tsubst (pl, args, complain, in_decl);
13720 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
13724 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
13725 /* We have reduced the level of the template
13726 template parameter, but not the levels of its
13727 template parameters, so canonical_type_parameter
13728 will not be able to find the canonical template
13729 template parameter for this level. Thus, we
13730 require structural equality checking to compare
13731 TEMPLATE_TEMPLATE_PARMs. */
13732 SET_TYPE_STRUCTURAL_EQUALITY (r);
13733 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
13734 SET_TYPE_STRUCTURAL_EQUALITY (r);
13735 else
13736 TYPE_CANONICAL (r) = canonical_type_parameter (r);
13738 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13740 tree tinfo = TYPE_TEMPLATE_INFO (t);
13741 /* We might need to substitute into the types of non-type
13742 template parameters. */
13743 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
13744 complain, in_decl);
13745 if (tmpl == error_mark_node)
13746 return error_mark_node;
13747 tree argvec = tsubst (TI_ARGS (tinfo), args,
13748 complain, in_decl);
13749 if (argvec == error_mark_node)
13750 return error_mark_node;
13752 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
13753 = build_template_info (tmpl, argvec);
13756 break;
13758 case TEMPLATE_PARM_INDEX:
13759 /* OK, now substitute the type of the non-type parameter. We
13760 couldn't do it earlier because it might be an auto parameter,
13761 and we wouldn't need to if we had an argument. */
13762 type = tsubst (type, args, complain, in_decl);
13763 r = reduce_template_parm_level (t, type, levels, args, complain);
13764 break;
13766 default:
13767 gcc_unreachable ();
13770 return r;
13773 case TREE_LIST:
13775 tree purpose, value, chain;
13777 if (t == void_list_node)
13778 return t;
13780 purpose = TREE_PURPOSE (t);
13781 if (purpose)
13783 purpose = tsubst (purpose, args, complain, in_decl);
13784 if (purpose == error_mark_node)
13785 return error_mark_node;
13787 value = TREE_VALUE (t);
13788 if (value)
13790 value = tsubst (value, args, complain, in_decl);
13791 if (value == error_mark_node)
13792 return error_mark_node;
13794 chain = TREE_CHAIN (t);
13795 if (chain && chain != void_type_node)
13797 chain = tsubst (chain, args, complain, in_decl);
13798 if (chain == error_mark_node)
13799 return error_mark_node;
13801 if (purpose == TREE_PURPOSE (t)
13802 && value == TREE_VALUE (t)
13803 && chain == TREE_CHAIN (t))
13804 return t;
13805 return hash_tree_cons (purpose, value, chain);
13808 case TREE_BINFO:
13809 /* We should never be tsubsting a binfo. */
13810 gcc_unreachable ();
13812 case TREE_VEC:
13813 /* A vector of template arguments. */
13814 gcc_assert (!type);
13815 return tsubst_template_args (t, args, complain, in_decl);
13817 case POINTER_TYPE:
13818 case REFERENCE_TYPE:
13820 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
13821 return t;
13823 /* [temp.deduct]
13825 Type deduction may fail for any of the following
13826 reasons:
13828 -- Attempting to create a pointer to reference type.
13829 -- Attempting to create a reference to a reference type or
13830 a reference to void.
13832 Core issue 106 says that creating a reference to a reference
13833 during instantiation is no longer a cause for failure. We
13834 only enforce this check in strict C++98 mode. */
13835 if ((TREE_CODE (type) == REFERENCE_TYPE
13836 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
13837 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
13839 static location_t last_loc;
13841 /* We keep track of the last time we issued this error
13842 message to avoid spewing a ton of messages during a
13843 single bad template instantiation. */
13844 if (complain & tf_error
13845 && last_loc != input_location)
13847 if (VOID_TYPE_P (type))
13848 error ("forming reference to void");
13849 else if (code == POINTER_TYPE)
13850 error ("forming pointer to reference type %qT", type);
13851 else
13852 error ("forming reference to reference type %qT", type);
13853 last_loc = input_location;
13856 return error_mark_node;
13858 else if (TREE_CODE (type) == FUNCTION_TYPE
13859 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
13860 || type_memfn_rqual (type) != REF_QUAL_NONE))
13862 if (complain & tf_error)
13864 if (code == POINTER_TYPE)
13865 error ("forming pointer to qualified function type %qT",
13866 type);
13867 else
13868 error ("forming reference to qualified function type %qT",
13869 type);
13871 return error_mark_node;
13873 else if (code == POINTER_TYPE)
13875 r = build_pointer_type (type);
13876 if (TREE_CODE (type) == METHOD_TYPE)
13877 r = build_ptrmemfunc_type (r);
13879 else if (TREE_CODE (type) == REFERENCE_TYPE)
13880 /* In C++0x, during template argument substitution, when there is an
13881 attempt to create a reference to a reference type, reference
13882 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
13884 "If a template-argument for a template-parameter T names a type
13885 that is a reference to a type A, an attempt to create the type
13886 'lvalue reference to cv T' creates the type 'lvalue reference to
13887 A,' while an attempt to create the type type rvalue reference to
13888 cv T' creates the type T"
13890 r = cp_build_reference_type
13891 (TREE_TYPE (type),
13892 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
13893 else
13894 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
13895 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
13897 if (r != error_mark_node)
13898 /* Will this ever be needed for TYPE_..._TO values? */
13899 layout_type (r);
13901 return r;
13903 case OFFSET_TYPE:
13905 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
13906 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
13908 /* [temp.deduct]
13910 Type deduction may fail for any of the following
13911 reasons:
13913 -- Attempting to create "pointer to member of T" when T
13914 is not a class type. */
13915 if (complain & tf_error)
13916 error ("creating pointer to member of non-class type %qT", r);
13917 return error_mark_node;
13919 if (TREE_CODE (type) == REFERENCE_TYPE)
13921 if (complain & tf_error)
13922 error ("creating pointer to member reference type %qT", type);
13923 return error_mark_node;
13925 if (VOID_TYPE_P (type))
13927 if (complain & tf_error)
13928 error ("creating pointer to member of type void");
13929 return error_mark_node;
13931 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
13932 if (TREE_CODE (type) == FUNCTION_TYPE)
13934 /* The type of the implicit object parameter gets its
13935 cv-qualifiers from the FUNCTION_TYPE. */
13936 tree memptr;
13937 tree method_type
13938 = build_memfn_type (type, r, type_memfn_quals (type),
13939 type_memfn_rqual (type));
13940 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
13941 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
13942 complain);
13944 else
13945 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
13946 cp_type_quals (t),
13947 complain);
13949 case FUNCTION_TYPE:
13950 case METHOD_TYPE:
13952 tree fntype;
13953 tree specs;
13954 fntype = tsubst_function_type (t, args, complain, in_decl);
13955 if (fntype == error_mark_node)
13956 return error_mark_node;
13958 /* Substitute the exception specification. */
13959 specs = tsubst_exception_specification (t, args, complain, in_decl,
13960 /*defer_ok*/fndecl_type);
13961 if (specs == error_mark_node)
13962 return error_mark_node;
13963 if (specs)
13964 fntype = build_exception_variant (fntype, specs);
13965 return fntype;
13967 case ARRAY_TYPE:
13969 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
13970 if (domain == error_mark_node)
13971 return error_mark_node;
13973 /* As an optimization, we avoid regenerating the array type if
13974 it will obviously be the same as T. */
13975 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
13976 return t;
13978 /* These checks should match the ones in create_array_type_for_decl.
13980 [temp.deduct]
13982 The deduction may fail for any of the following reasons:
13984 -- Attempting to create an array with an element type that
13985 is void, a function type, or a reference type, or [DR337]
13986 an abstract class type. */
13987 if (VOID_TYPE_P (type)
13988 || TREE_CODE (type) == FUNCTION_TYPE
13989 || (TREE_CODE (type) == ARRAY_TYPE
13990 && TYPE_DOMAIN (type) == NULL_TREE)
13991 || TREE_CODE (type) == REFERENCE_TYPE)
13993 if (complain & tf_error)
13994 error ("creating array of %qT", type);
13995 return error_mark_node;
13998 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
13999 return error_mark_node;
14001 r = build_cplus_array_type (type, domain);
14003 if (TYPE_USER_ALIGN (t))
14005 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
14006 TYPE_USER_ALIGN (r) = 1;
14009 return r;
14012 case TYPENAME_TYPE:
14014 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14015 in_decl, /*entering_scope=*/1);
14016 if (ctx == error_mark_node)
14017 return error_mark_node;
14019 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
14020 complain, in_decl);
14021 if (f == error_mark_node)
14022 return error_mark_node;
14024 if (!MAYBE_CLASS_TYPE_P (ctx))
14026 if (complain & tf_error)
14027 error ("%qT is not a class, struct, or union type", ctx);
14028 return error_mark_node;
14030 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
14032 /* Normally, make_typename_type does not require that the CTX
14033 have complete type in order to allow things like:
14035 template <class T> struct S { typename S<T>::X Y; };
14037 But, such constructs have already been resolved by this
14038 point, so here CTX really should have complete type, unless
14039 it's a partial instantiation. */
14040 ctx = complete_type (ctx);
14041 if (!COMPLETE_TYPE_P (ctx))
14043 if (complain & tf_error)
14044 cxx_incomplete_type_error (NULL_TREE, ctx);
14045 return error_mark_node;
14049 f = make_typename_type (ctx, f, typename_type,
14050 complain | tf_keep_type_decl);
14051 if (f == error_mark_node)
14052 return f;
14053 if (TREE_CODE (f) == TYPE_DECL)
14055 complain |= tf_ignore_bad_quals;
14056 f = TREE_TYPE (f);
14059 if (TREE_CODE (f) != TYPENAME_TYPE)
14061 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
14063 if (complain & tf_error)
14064 error ("%qT resolves to %qT, which is not an enumeration type",
14065 t, f);
14066 else
14067 return error_mark_node;
14069 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
14071 if (complain & tf_error)
14072 error ("%qT resolves to %qT, which is is not a class type",
14073 t, f);
14074 else
14075 return error_mark_node;
14079 return cp_build_qualified_type_real
14080 (f, cp_type_quals (f) | cp_type_quals (t), complain);
14083 case UNBOUND_CLASS_TEMPLATE:
14085 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14086 in_decl, /*entering_scope=*/1);
14087 tree name = TYPE_IDENTIFIER (t);
14088 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
14090 if (ctx == error_mark_node || name == error_mark_node)
14091 return error_mark_node;
14093 if (parm_list)
14094 parm_list = tsubst_template_parms (parm_list, args, complain);
14095 return make_unbound_class_template (ctx, name, parm_list, complain);
14098 case TYPEOF_TYPE:
14100 tree type;
14102 ++cp_unevaluated_operand;
14103 ++c_inhibit_evaluation_warnings;
14105 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
14106 complain, in_decl,
14107 /*integral_constant_expression_p=*/false);
14109 --cp_unevaluated_operand;
14110 --c_inhibit_evaluation_warnings;
14112 type = finish_typeof (type);
14113 return cp_build_qualified_type_real (type,
14114 cp_type_quals (t)
14115 | cp_type_quals (type),
14116 complain);
14119 case DECLTYPE_TYPE:
14121 tree type;
14123 ++cp_unevaluated_operand;
14124 ++c_inhibit_evaluation_warnings;
14126 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
14127 complain|tf_decltype, in_decl,
14128 /*function_p*/false,
14129 /*integral_constant_expression*/false);
14131 if (DECLTYPE_FOR_INIT_CAPTURE (t))
14133 if (type == NULL_TREE)
14135 if (complain & tf_error)
14136 error ("empty initializer in lambda init-capture");
14137 type = error_mark_node;
14139 else if (TREE_CODE (type) == TREE_LIST)
14140 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
14143 --cp_unevaluated_operand;
14144 --c_inhibit_evaluation_warnings;
14146 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
14147 type = lambda_capture_field_type (type,
14148 DECLTYPE_FOR_INIT_CAPTURE (t),
14149 DECLTYPE_FOR_REF_CAPTURE (t));
14150 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
14151 type = lambda_proxy_type (type);
14152 else
14154 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
14155 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
14156 && EXPR_P (type))
14157 /* In a template ~id could be either a complement expression
14158 or an unqualified-id naming a destructor; if instantiating
14159 it produces an expression, it's not an id-expression or
14160 member access. */
14161 id = false;
14162 type = finish_decltype_type (type, id, complain);
14164 return cp_build_qualified_type_real (type,
14165 cp_type_quals (t)
14166 | cp_type_quals (type),
14167 complain | tf_ignore_bad_quals);
14170 case UNDERLYING_TYPE:
14172 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
14173 complain, in_decl);
14174 return finish_underlying_type (type);
14177 case TYPE_ARGUMENT_PACK:
14178 case NONTYPE_ARGUMENT_PACK:
14180 tree r;
14182 if (code == NONTYPE_ARGUMENT_PACK)
14183 r = make_node (code);
14184 else
14185 r = cxx_make_type (code);
14187 tree pack_args = ARGUMENT_PACK_ARGS (t);
14188 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
14189 SET_ARGUMENT_PACK_ARGS (r, pack_args);
14191 return r;
14194 case VOID_CST:
14195 case INTEGER_CST:
14196 case REAL_CST:
14197 case STRING_CST:
14198 case PLUS_EXPR:
14199 case MINUS_EXPR:
14200 case NEGATE_EXPR:
14201 case NOP_EXPR:
14202 case INDIRECT_REF:
14203 case ADDR_EXPR:
14204 case CALL_EXPR:
14205 case ARRAY_REF:
14206 case SCOPE_REF:
14207 /* We should use one of the expression tsubsts for these codes. */
14208 gcc_unreachable ();
14210 default:
14211 sorry ("use of %qs in template", get_tree_code_name (code));
14212 return error_mark_node;
14216 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
14217 expression on the left-hand side of the "." or "->" operator. A
14218 baselink indicates a function from a base class. Both the
14219 BASELINK_ACCESS_BINFO and the base class referenced may indicate
14220 bases of the template class, rather than the instantiated class.
14221 In addition, lookups that were not ambiguous before may be
14222 ambiguous now. Therefore, we perform the lookup again. */
14224 static tree
14225 tsubst_baselink (tree baselink, tree object_type,
14226 tree args, tsubst_flags_t complain, tree in_decl)
14228 bool qualified = BASELINK_QUALIFIED_P (baselink);
14230 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
14231 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
14233 tree optype = BASELINK_OPTYPE (baselink);
14234 optype = tsubst (optype, args, complain, in_decl);
14236 tree template_args = NULL_TREE;
14237 bool template_id_p = false;
14238 tree fns = BASELINK_FUNCTIONS (baselink);
14239 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
14241 template_id_p = true;
14242 template_args = TREE_OPERAND (fns, 1);
14243 fns = TREE_OPERAND (fns, 0);
14244 if (template_args)
14245 template_args = tsubst_template_args (template_args, args,
14246 complain, in_decl);
14249 tree name = OVL_NAME (fns);
14250 if (IDENTIFIER_TYPENAME_P (name))
14251 name = mangle_conv_op_name_for_type (optype);
14253 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
14254 if (!baselink)
14256 if ((complain & tf_error) && constructor_name_p (name, qualifying_scope))
14257 error ("cannot call constructor %<%T::%D%> directly",
14258 qualifying_scope, name);
14259 return error_mark_node;
14262 /* If lookup found a single function, mark it as used at this point.
14263 (If it lookup found multiple functions the one selected later by
14264 overload resolution will be marked as used at that point.) */
14265 if (BASELINK_P (baselink))
14266 fns = BASELINK_FUNCTIONS (baselink);
14267 if (!template_id_p && !really_overloaded_fn (fns)
14268 && !mark_used (OVL_FIRST (fns), complain) && !(complain & tf_error))
14269 return error_mark_node;
14271 if (BASELINK_P (baselink))
14273 /* Add back the template arguments, if present. */
14274 if (template_id_p)
14275 BASELINK_FUNCTIONS (baselink)
14276 = build2 (TEMPLATE_ID_EXPR, unknown_type_node,
14277 BASELINK_FUNCTIONS (baselink), template_args);
14279 /* Update the conversion operator type. */
14280 BASELINK_OPTYPE (baselink) = optype;
14283 if (!object_type)
14284 object_type = current_class_type;
14286 if (qualified || name == complete_dtor_identifier)
14288 baselink = adjust_result_of_qualified_name_lookup (baselink,
14289 qualifying_scope,
14290 object_type);
14291 if (!qualified)
14292 /* We need to call adjust_result_of_qualified_name_lookup in case the
14293 destructor names a base class, but we unset BASELINK_QUALIFIED_P
14294 so that we still get virtual function binding. */
14295 BASELINK_QUALIFIED_P (baselink) = false;
14298 return baselink;
14301 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
14302 true if the qualified-id will be a postfix-expression in-and-of
14303 itself; false if more of the postfix-expression follows the
14304 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
14305 of "&". */
14307 static tree
14308 tsubst_qualified_id (tree qualified_id, tree args,
14309 tsubst_flags_t complain, tree in_decl,
14310 bool done, bool address_p)
14312 tree expr;
14313 tree scope;
14314 tree name;
14315 bool is_template;
14316 tree template_args;
14317 location_t loc = UNKNOWN_LOCATION;
14319 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
14321 /* Figure out what name to look up. */
14322 name = TREE_OPERAND (qualified_id, 1);
14323 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14325 is_template = true;
14326 loc = EXPR_LOCATION (name);
14327 template_args = TREE_OPERAND (name, 1);
14328 if (template_args)
14329 template_args = tsubst_template_args (template_args, args,
14330 complain, in_decl);
14331 if (template_args == error_mark_node)
14332 return error_mark_node;
14333 name = TREE_OPERAND (name, 0);
14335 else
14337 is_template = false;
14338 template_args = NULL_TREE;
14341 /* Substitute into the qualifying scope. When there are no ARGS, we
14342 are just trying to simplify a non-dependent expression. In that
14343 case the qualifying scope may be dependent, and, in any case,
14344 substituting will not help. */
14345 scope = TREE_OPERAND (qualified_id, 0);
14346 if (args)
14348 scope = tsubst (scope, args, complain, in_decl);
14349 expr = tsubst_copy (name, args, complain, in_decl);
14351 else
14352 expr = name;
14354 if (dependent_scope_p (scope))
14356 if (is_template)
14357 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
14358 tree r = build_qualified_name (NULL_TREE, scope, expr,
14359 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
14360 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
14361 return r;
14364 if (!BASELINK_P (name) && !DECL_P (expr))
14366 if (TREE_CODE (expr) == BIT_NOT_EXPR)
14368 /* A BIT_NOT_EXPR is used to represent a destructor. */
14369 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
14371 error ("qualifying type %qT does not match destructor name ~%qT",
14372 scope, TREE_OPERAND (expr, 0));
14373 expr = error_mark_node;
14375 else
14376 expr = lookup_qualified_name (scope, complete_dtor_identifier,
14377 /*is_type_p=*/0, false);
14379 else
14380 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
14381 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
14382 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
14384 if (complain & tf_error)
14386 error ("dependent-name %qE is parsed as a non-type, but "
14387 "instantiation yields a type", qualified_id);
14388 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
14390 return error_mark_node;
14394 if (DECL_P (expr))
14396 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
14397 scope);
14398 /* Remember that there was a reference to this entity. */
14399 if (!mark_used (expr, complain) && !(complain & tf_error))
14400 return error_mark_node;
14403 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
14405 if (complain & tf_error)
14406 qualified_name_lookup_error (scope,
14407 TREE_OPERAND (qualified_id, 1),
14408 expr, input_location);
14409 return error_mark_node;
14412 if (is_template)
14414 if (variable_template_p (expr))
14415 expr = lookup_and_finish_template_variable (expr, template_args,
14416 complain);
14417 else
14418 expr = lookup_template_function (expr, template_args);
14421 if (expr == error_mark_node && complain & tf_error)
14422 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
14423 expr, input_location);
14424 else if (TYPE_P (scope))
14426 expr = (adjust_result_of_qualified_name_lookup
14427 (expr, scope, current_nonlambda_class_type ()));
14428 expr = (finish_qualified_id_expr
14429 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
14430 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
14431 /*template_arg_p=*/false, complain));
14434 /* Expressions do not generally have reference type. */
14435 if (TREE_CODE (expr) != SCOPE_REF
14436 /* However, if we're about to form a pointer-to-member, we just
14437 want the referenced member referenced. */
14438 && TREE_CODE (expr) != OFFSET_REF)
14439 expr = convert_from_reference (expr);
14441 if (REF_PARENTHESIZED_P (qualified_id))
14442 expr = force_paren_expr (expr);
14444 return expr;
14447 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14448 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14449 for tsubst. */
14451 static tree
14452 tsubst_init (tree init, tree decl, tree args,
14453 tsubst_flags_t complain, tree in_decl)
14455 if (!init)
14456 return NULL_TREE;
14458 init = tsubst_expr (init, args, complain, in_decl, false);
14460 if (!init && TREE_TYPE (decl) != error_mark_node)
14462 /* If we had an initializer but it
14463 instantiated to nothing,
14464 value-initialize the object. This will
14465 only occur when the initializer was a
14466 pack expansion where the parameter packs
14467 used in that expansion were of length
14468 zero. */
14469 init = build_value_init (TREE_TYPE (decl),
14470 complain);
14471 if (TREE_CODE (init) == AGGR_INIT_EXPR)
14472 init = get_target_expr_sfinae (init, complain);
14473 if (TREE_CODE (init) == TARGET_EXPR)
14474 TARGET_EXPR_DIRECT_INIT_P (init) = true;
14477 return init;
14480 /* Like tsubst, but deals with expressions. This function just replaces
14481 template parms; to finish processing the resultant expression, use
14482 tsubst_copy_and_build or tsubst_expr. */
14484 static tree
14485 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14487 enum tree_code code;
14488 tree r;
14490 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
14491 return t;
14493 code = TREE_CODE (t);
14495 switch (code)
14497 case PARM_DECL:
14498 r = retrieve_local_specialization (t);
14500 if (r == NULL_TREE)
14502 /* We get here for a use of 'this' in an NSDMI as part of a
14503 constructor call or as part of an aggregate initialization. */
14504 if (DECL_NAME (t) == this_identifier
14505 && ((current_function_decl
14506 && DECL_CONSTRUCTOR_P (current_function_decl))
14507 || (current_class_ref
14508 && TREE_CODE (current_class_ref) == PLACEHOLDER_EXPR)))
14509 return current_class_ptr;
14511 /* This can happen for a parameter name used later in a function
14512 declaration (such as in a late-specified return type). Just
14513 make a dummy decl, since it's only used for its type. */
14514 gcc_assert (cp_unevaluated_operand != 0);
14515 r = tsubst_decl (t, args, complain);
14516 /* Give it the template pattern as its context; its true context
14517 hasn't been instantiated yet and this is good enough for
14518 mangling. */
14519 DECL_CONTEXT (r) = DECL_CONTEXT (t);
14522 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14523 r = ARGUMENT_PACK_SELECT_ARG (r);
14524 if (!mark_used (r, complain) && !(complain & tf_error))
14525 return error_mark_node;
14526 return r;
14528 case CONST_DECL:
14530 tree enum_type;
14531 tree v;
14533 if (DECL_TEMPLATE_PARM_P (t))
14534 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
14535 /* There is no need to substitute into namespace-scope
14536 enumerators. */
14537 if (DECL_NAMESPACE_SCOPE_P (t))
14538 return t;
14539 /* If ARGS is NULL, then T is known to be non-dependent. */
14540 if (args == NULL_TREE)
14541 return scalar_constant_value (t);
14543 /* Unfortunately, we cannot just call lookup_name here.
14544 Consider:
14546 template <int I> int f() {
14547 enum E { a = I };
14548 struct S { void g() { E e = a; } };
14551 When we instantiate f<7>::S::g(), say, lookup_name is not
14552 clever enough to find f<7>::a. */
14553 enum_type
14554 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14555 /*entering_scope=*/0);
14557 for (v = TYPE_VALUES (enum_type);
14558 v != NULL_TREE;
14559 v = TREE_CHAIN (v))
14560 if (TREE_PURPOSE (v) == DECL_NAME (t))
14561 return TREE_VALUE (v);
14563 /* We didn't find the name. That should never happen; if
14564 name-lookup found it during preliminary parsing, we
14565 should find it again here during instantiation. */
14566 gcc_unreachable ();
14568 return t;
14570 case FIELD_DECL:
14571 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14573 /* Check for a local specialization set up by
14574 tsubst_pack_expansion. */
14575 if (tree r = retrieve_local_specialization (t))
14577 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14578 r = ARGUMENT_PACK_SELECT_ARG (r);
14579 return r;
14582 /* When retrieving a capture pack from a generic lambda, remove the
14583 lambda call op's own template argument list from ARGS. Only the
14584 template arguments active for the closure type should be used to
14585 retrieve the pack specialization. */
14586 if (LAMBDA_FUNCTION_P (current_function_decl)
14587 && (template_class_depth (DECL_CONTEXT (t))
14588 != TMPL_ARGS_DEPTH (args)))
14589 args = strip_innermost_template_args (args, 1);
14591 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
14592 tsubst_decl put in the hash table. */
14593 return retrieve_specialization (t, args, 0);
14596 if (DECL_CONTEXT (t))
14598 tree ctx;
14600 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14601 /*entering_scope=*/1);
14602 if (ctx != DECL_CONTEXT (t))
14604 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
14605 if (!r)
14607 if (complain & tf_error)
14608 error ("using invalid field %qD", t);
14609 return error_mark_node;
14611 return r;
14615 return t;
14617 case VAR_DECL:
14618 case FUNCTION_DECL:
14619 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
14620 r = tsubst (t, args, complain, in_decl);
14621 else if (local_variable_p (t)
14622 && uses_template_parms (DECL_CONTEXT (t)))
14624 r = retrieve_local_specialization (t);
14625 if (r == NULL_TREE)
14627 /* First try name lookup to find the instantiation. */
14628 r = lookup_name (DECL_NAME (t));
14629 if (r && !is_capture_proxy (r))
14631 /* Make sure that the one we found is the one we want. */
14632 tree ctx = DECL_CONTEXT (t);
14633 if (DECL_LANG_SPECIFIC (ctx) && DECL_TEMPLATE_INFO (ctx))
14634 ctx = tsubst (ctx, args, complain, in_decl);
14635 if (ctx != DECL_CONTEXT (r))
14636 r = NULL_TREE;
14639 if (r)
14640 /* OK */;
14641 else
14643 /* This can happen for a variable used in a
14644 late-specified return type of a local lambda, or for a
14645 local static or constant. Building a new VAR_DECL
14646 should be OK in all those cases. */
14647 r = tsubst_decl (t, args, complain);
14648 if (local_specializations)
14649 /* Avoid infinite recursion (79640). */
14650 register_local_specialization (r, t);
14651 if (decl_maybe_constant_var_p (r))
14653 /* We can't call cp_finish_decl, so handle the
14654 initializer by hand. */
14655 tree init = tsubst_init (DECL_INITIAL (t), r, args,
14656 complain, in_decl);
14657 if (!processing_template_decl)
14658 init = maybe_constant_init (init);
14659 if (processing_template_decl
14660 ? potential_constant_expression (init)
14661 : reduced_constant_expression_p (init))
14662 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
14663 = TREE_CONSTANT (r) = true;
14664 DECL_INITIAL (r) = init;
14666 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
14667 || decl_constant_var_p (r)
14668 || errorcount || sorrycount);
14669 if (!processing_template_decl
14670 && !TREE_STATIC (r))
14671 r = process_outer_var_ref (r, complain);
14673 /* Remember this for subsequent uses. */
14674 if (local_specializations)
14675 register_local_specialization (r, t);
14678 else
14679 r = t;
14680 if (!mark_used (r, complain))
14681 return error_mark_node;
14682 return r;
14684 case NAMESPACE_DECL:
14685 return t;
14687 case OVERLOAD:
14688 /* An OVERLOAD will always be a non-dependent overload set; an
14689 overload set from function scope will just be represented with an
14690 IDENTIFIER_NODE, and from class scope with a BASELINK. */
14691 gcc_assert (!uses_template_parms (t));
14692 /* We must have marked any lookups as persistent. */
14693 gcc_assert (!OVL_LOOKUP_P (t) || OVL_USED_P (t));
14694 return t;
14696 case BASELINK:
14697 return tsubst_baselink (t, current_nonlambda_class_type (),
14698 args, complain, in_decl);
14700 case TEMPLATE_DECL:
14701 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
14702 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
14703 args, complain, in_decl);
14704 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
14705 return tsubst (t, args, complain, in_decl);
14706 else if (DECL_CLASS_SCOPE_P (t)
14707 && uses_template_parms (DECL_CONTEXT (t)))
14709 /* Template template argument like the following example need
14710 special treatment:
14712 template <template <class> class TT> struct C {};
14713 template <class T> struct D {
14714 template <class U> struct E {};
14715 C<E> c; // #1
14717 D<int> d; // #2
14719 We are processing the template argument `E' in #1 for
14720 the template instantiation #2. Originally, `E' is a
14721 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
14722 have to substitute this with one having context `D<int>'. */
14724 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
14725 if (dependent_scope_p (context))
14727 /* When rewriting a constructor into a deduction guide, a
14728 non-dependent name can become dependent, so memtmpl<args>
14729 becomes context::template memtmpl<args>. */
14730 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14731 return build_qualified_name (type, context, DECL_NAME (t),
14732 /*template*/true);
14734 return lookup_field (context, DECL_NAME(t), 0, false);
14736 else
14737 /* Ordinary template template argument. */
14738 return t;
14740 case CAST_EXPR:
14741 case REINTERPRET_CAST_EXPR:
14742 case CONST_CAST_EXPR:
14743 case STATIC_CAST_EXPR:
14744 case DYNAMIC_CAST_EXPR:
14745 case IMPLICIT_CONV_EXPR:
14746 case CONVERT_EXPR:
14747 case NOP_EXPR:
14749 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14750 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14751 return build1 (code, type, op0);
14754 case SIZEOF_EXPR:
14755 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
14756 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
14758 tree expanded, op = TREE_OPERAND (t, 0);
14759 int len = 0;
14761 if (SIZEOF_EXPR_TYPE_P (t))
14762 op = TREE_TYPE (op);
14764 ++cp_unevaluated_operand;
14765 ++c_inhibit_evaluation_warnings;
14766 /* We only want to compute the number of arguments. */
14767 if (PACK_EXPANSION_P (op))
14768 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
14769 else
14770 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
14771 args, complain, in_decl);
14772 --cp_unevaluated_operand;
14773 --c_inhibit_evaluation_warnings;
14775 if (TREE_CODE (expanded) == TREE_VEC)
14777 len = TREE_VEC_LENGTH (expanded);
14778 /* Set TREE_USED for the benefit of -Wunused. */
14779 for (int i = 0; i < len; i++)
14780 if (DECL_P (TREE_VEC_ELT (expanded, i)))
14781 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
14784 if (expanded == error_mark_node)
14785 return error_mark_node;
14786 else if (PACK_EXPANSION_P (expanded)
14787 || (TREE_CODE (expanded) == TREE_VEC
14788 && pack_expansion_args_count (expanded)))
14791 if (PACK_EXPANSION_P (expanded))
14792 /* OK. */;
14793 else if (TREE_VEC_LENGTH (expanded) == 1)
14794 expanded = TREE_VEC_ELT (expanded, 0);
14795 else
14796 expanded = make_argument_pack (expanded);
14798 if (TYPE_P (expanded))
14799 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
14800 complain & tf_error);
14801 else
14802 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
14803 complain & tf_error);
14805 else
14806 return build_int_cst (size_type_node, len);
14808 if (SIZEOF_EXPR_TYPE_P (t))
14810 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
14811 args, complain, in_decl);
14812 r = build1 (NOP_EXPR, r, error_mark_node);
14813 r = build1 (SIZEOF_EXPR,
14814 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
14815 SIZEOF_EXPR_TYPE_P (r) = 1;
14816 return r;
14818 /* Fall through */
14820 case INDIRECT_REF:
14821 case NEGATE_EXPR:
14822 case TRUTH_NOT_EXPR:
14823 case BIT_NOT_EXPR:
14824 case ADDR_EXPR:
14825 case UNARY_PLUS_EXPR: /* Unary + */
14826 case ALIGNOF_EXPR:
14827 case AT_ENCODE_EXPR:
14828 case ARROW_EXPR:
14829 case THROW_EXPR:
14830 case TYPEID_EXPR:
14831 case REALPART_EXPR:
14832 case IMAGPART_EXPR:
14833 case PAREN_EXPR:
14835 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14836 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14837 return build1 (code, type, op0);
14840 case COMPONENT_REF:
14842 tree object;
14843 tree name;
14845 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14846 name = TREE_OPERAND (t, 1);
14847 if (TREE_CODE (name) == BIT_NOT_EXPR)
14849 name = tsubst_copy (TREE_OPERAND (name, 0), args,
14850 complain, in_decl);
14851 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
14853 else if (TREE_CODE (name) == SCOPE_REF
14854 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
14856 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
14857 complain, in_decl);
14858 name = TREE_OPERAND (name, 1);
14859 name = tsubst_copy (TREE_OPERAND (name, 0), args,
14860 complain, in_decl);
14861 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
14862 name = build_qualified_name (/*type=*/NULL_TREE,
14863 base, name,
14864 /*template_p=*/false);
14866 else if (BASELINK_P (name))
14867 name = tsubst_baselink (name,
14868 non_reference (TREE_TYPE (object)),
14869 args, complain,
14870 in_decl);
14871 else
14872 name = tsubst_copy (name, args, complain, in_decl);
14873 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
14876 case PLUS_EXPR:
14877 case MINUS_EXPR:
14878 case MULT_EXPR:
14879 case TRUNC_DIV_EXPR:
14880 case CEIL_DIV_EXPR:
14881 case FLOOR_DIV_EXPR:
14882 case ROUND_DIV_EXPR:
14883 case EXACT_DIV_EXPR:
14884 case BIT_AND_EXPR:
14885 case BIT_IOR_EXPR:
14886 case BIT_XOR_EXPR:
14887 case TRUNC_MOD_EXPR:
14888 case FLOOR_MOD_EXPR:
14889 case TRUTH_ANDIF_EXPR:
14890 case TRUTH_ORIF_EXPR:
14891 case TRUTH_AND_EXPR:
14892 case TRUTH_OR_EXPR:
14893 case RSHIFT_EXPR:
14894 case LSHIFT_EXPR:
14895 case RROTATE_EXPR:
14896 case LROTATE_EXPR:
14897 case EQ_EXPR:
14898 case NE_EXPR:
14899 case MAX_EXPR:
14900 case MIN_EXPR:
14901 case LE_EXPR:
14902 case GE_EXPR:
14903 case LT_EXPR:
14904 case GT_EXPR:
14905 case COMPOUND_EXPR:
14906 case DOTSTAR_EXPR:
14907 case MEMBER_REF:
14908 case PREDECREMENT_EXPR:
14909 case PREINCREMENT_EXPR:
14910 case POSTDECREMENT_EXPR:
14911 case POSTINCREMENT_EXPR:
14913 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14914 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14915 return build_nt (code, op0, op1);
14918 case SCOPE_REF:
14920 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14921 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14922 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
14923 QUALIFIED_NAME_IS_TEMPLATE (t));
14926 case ARRAY_REF:
14928 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14929 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14930 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
14933 case CALL_EXPR:
14935 int n = VL_EXP_OPERAND_LENGTH (t);
14936 tree result = build_vl_exp (CALL_EXPR, n);
14937 int i;
14938 for (i = 0; i < n; i++)
14939 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
14940 complain, in_decl);
14941 return result;
14944 case COND_EXPR:
14945 case MODOP_EXPR:
14946 case PSEUDO_DTOR_EXPR:
14947 case VEC_PERM_EXPR:
14949 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14950 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14951 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
14952 r = build_nt (code, op0, op1, op2);
14953 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14954 return r;
14957 case NEW_EXPR:
14959 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14960 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14961 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
14962 r = build_nt (code, op0, op1, op2);
14963 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
14964 return r;
14967 case DELETE_EXPR:
14969 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14970 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14971 r = build_nt (code, op0, op1);
14972 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
14973 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
14974 return r;
14977 case TEMPLATE_ID_EXPR:
14979 /* Substituted template arguments */
14980 tree fn = TREE_OPERAND (t, 0);
14981 tree targs = TREE_OPERAND (t, 1);
14983 fn = tsubst_copy (fn, args, complain, in_decl);
14984 if (targs)
14985 targs = tsubst_template_args (targs, args, complain, in_decl);
14987 return lookup_template_function (fn, targs);
14990 case TREE_LIST:
14992 tree purpose, value, chain;
14994 if (t == void_list_node)
14995 return t;
14997 purpose = TREE_PURPOSE (t);
14998 if (purpose)
14999 purpose = tsubst_copy (purpose, args, complain, in_decl);
15000 value = TREE_VALUE (t);
15001 if (value)
15002 value = tsubst_copy (value, args, complain, in_decl);
15003 chain = TREE_CHAIN (t);
15004 if (chain && chain != void_type_node)
15005 chain = tsubst_copy (chain, args, complain, in_decl);
15006 if (purpose == TREE_PURPOSE (t)
15007 && value == TREE_VALUE (t)
15008 && chain == TREE_CHAIN (t))
15009 return t;
15010 return tree_cons (purpose, value, chain);
15013 case RECORD_TYPE:
15014 case UNION_TYPE:
15015 case ENUMERAL_TYPE:
15016 case INTEGER_TYPE:
15017 case TEMPLATE_TYPE_PARM:
15018 case TEMPLATE_TEMPLATE_PARM:
15019 case BOUND_TEMPLATE_TEMPLATE_PARM:
15020 case TEMPLATE_PARM_INDEX:
15021 case POINTER_TYPE:
15022 case REFERENCE_TYPE:
15023 case OFFSET_TYPE:
15024 case FUNCTION_TYPE:
15025 case METHOD_TYPE:
15026 case ARRAY_TYPE:
15027 case TYPENAME_TYPE:
15028 case UNBOUND_CLASS_TEMPLATE:
15029 case TYPEOF_TYPE:
15030 case DECLTYPE_TYPE:
15031 case TYPE_DECL:
15032 return tsubst (t, args, complain, in_decl);
15034 case USING_DECL:
15035 t = DECL_NAME (t);
15036 /* Fall through. */
15037 case IDENTIFIER_NODE:
15038 if (IDENTIFIER_TYPENAME_P (t))
15040 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15041 return mangle_conv_op_name_for_type (new_type);
15043 else
15044 return t;
15046 case CONSTRUCTOR:
15047 /* This is handled by tsubst_copy_and_build. */
15048 gcc_unreachable ();
15050 case VA_ARG_EXPR:
15052 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15053 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15054 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
15057 case CLEANUP_POINT_EXPR:
15058 /* We shouldn't have built any of these during initial template
15059 generation. Instead, they should be built during instantiation
15060 in response to the saved STMT_IS_FULL_EXPR_P setting. */
15061 gcc_unreachable ();
15063 case OFFSET_REF:
15065 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15066 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15067 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15068 r = build2 (code, type, op0, op1);
15069 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
15070 if (!mark_used (TREE_OPERAND (r, 1), complain)
15071 && !(complain & tf_error))
15072 return error_mark_node;
15073 return r;
15076 case EXPR_PACK_EXPANSION:
15077 error ("invalid use of pack expansion expression");
15078 return error_mark_node;
15080 case NONTYPE_ARGUMENT_PACK:
15081 error ("use %<...%> to expand argument pack");
15082 return error_mark_node;
15084 case VOID_CST:
15085 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
15086 return t;
15088 case INTEGER_CST:
15089 case REAL_CST:
15090 case STRING_CST:
15091 case COMPLEX_CST:
15093 /* Instantiate any typedefs in the type. */
15094 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15095 r = fold_convert (type, t);
15096 gcc_assert (TREE_CODE (r) == code);
15097 return r;
15100 case PTRMEM_CST:
15101 /* These can sometimes show up in a partial instantiation, but never
15102 involve template parms. */
15103 gcc_assert (!uses_template_parms (t));
15104 return t;
15106 case UNARY_LEFT_FOLD_EXPR:
15107 return tsubst_unary_left_fold (t, args, complain, in_decl);
15108 case UNARY_RIGHT_FOLD_EXPR:
15109 return tsubst_unary_right_fold (t, args, complain, in_decl);
15110 case BINARY_LEFT_FOLD_EXPR:
15111 return tsubst_binary_left_fold (t, args, complain, in_decl);
15112 case BINARY_RIGHT_FOLD_EXPR:
15113 return tsubst_binary_right_fold (t, args, complain, in_decl);
15115 default:
15116 /* We shouldn't get here, but keep going if !flag_checking. */
15117 if (flag_checking)
15118 gcc_unreachable ();
15119 return t;
15123 /* Helper function for tsubst_omp_clauses, used for instantiation of
15124 OMP_CLAUSE_DECL of clauses. */
15126 static tree
15127 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
15128 tree in_decl)
15130 if (decl == NULL_TREE)
15131 return NULL_TREE;
15133 /* Handle an OpenMP array section represented as a TREE_LIST (or
15134 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15135 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15136 TREE_LIST. We can handle it exactly the same as an array section
15137 (purpose, value, and a chain), even though the nomenclature
15138 (low_bound, length, etc) is different. */
15139 if (TREE_CODE (decl) == TREE_LIST)
15141 tree low_bound
15142 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
15143 /*integral_constant_expression_p=*/false);
15144 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
15145 /*integral_constant_expression_p=*/false);
15146 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
15147 in_decl);
15148 if (TREE_PURPOSE (decl) == low_bound
15149 && TREE_VALUE (decl) == length
15150 && TREE_CHAIN (decl) == chain)
15151 return decl;
15152 tree ret = tree_cons (low_bound, length, chain);
15153 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
15154 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
15155 return ret;
15157 tree ret = tsubst_expr (decl, args, complain, in_decl,
15158 /*integral_constant_expression_p=*/false);
15159 /* Undo convert_from_reference tsubst_expr could have called. */
15160 if (decl
15161 && REFERENCE_REF_P (ret)
15162 && !REFERENCE_REF_P (decl))
15163 ret = TREE_OPERAND (ret, 0);
15164 return ret;
15167 /* Like tsubst_copy, but specifically for OpenMP clauses. */
15169 static tree
15170 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
15171 tree args, tsubst_flags_t complain, tree in_decl)
15173 tree new_clauses = NULL_TREE, nc, oc;
15174 tree linear_no_step = NULL_TREE;
15176 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
15178 nc = copy_node (oc);
15179 OMP_CLAUSE_CHAIN (nc) = new_clauses;
15180 new_clauses = nc;
15182 switch (OMP_CLAUSE_CODE (nc))
15184 case OMP_CLAUSE_LASTPRIVATE:
15185 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
15187 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
15188 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
15189 in_decl, /*integral_constant_expression_p=*/false);
15190 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
15191 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
15193 /* FALLTHRU */
15194 case OMP_CLAUSE_PRIVATE:
15195 case OMP_CLAUSE_SHARED:
15196 case OMP_CLAUSE_FIRSTPRIVATE:
15197 case OMP_CLAUSE_COPYIN:
15198 case OMP_CLAUSE_COPYPRIVATE:
15199 case OMP_CLAUSE_UNIFORM:
15200 case OMP_CLAUSE_DEPEND:
15201 case OMP_CLAUSE_FROM:
15202 case OMP_CLAUSE_TO:
15203 case OMP_CLAUSE_MAP:
15204 case OMP_CLAUSE_USE_DEVICE_PTR:
15205 case OMP_CLAUSE_IS_DEVICE_PTR:
15206 OMP_CLAUSE_DECL (nc)
15207 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15208 in_decl);
15209 break;
15210 case OMP_CLAUSE_TILE:
15211 case OMP_CLAUSE_IF:
15212 case OMP_CLAUSE_NUM_THREADS:
15213 case OMP_CLAUSE_SCHEDULE:
15214 case OMP_CLAUSE_COLLAPSE:
15215 case OMP_CLAUSE_FINAL:
15216 case OMP_CLAUSE_DEVICE:
15217 case OMP_CLAUSE_DIST_SCHEDULE:
15218 case OMP_CLAUSE_NUM_TEAMS:
15219 case OMP_CLAUSE_THREAD_LIMIT:
15220 case OMP_CLAUSE_SAFELEN:
15221 case OMP_CLAUSE_SIMDLEN:
15222 case OMP_CLAUSE_NUM_TASKS:
15223 case OMP_CLAUSE_GRAINSIZE:
15224 case OMP_CLAUSE_PRIORITY:
15225 case OMP_CLAUSE_ORDERED:
15226 case OMP_CLAUSE_HINT:
15227 case OMP_CLAUSE_NUM_GANGS:
15228 case OMP_CLAUSE_NUM_WORKERS:
15229 case OMP_CLAUSE_VECTOR_LENGTH:
15230 case OMP_CLAUSE_WORKER:
15231 case OMP_CLAUSE_VECTOR:
15232 case OMP_CLAUSE_ASYNC:
15233 case OMP_CLAUSE_WAIT:
15234 OMP_CLAUSE_OPERAND (nc, 0)
15235 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
15236 in_decl, /*integral_constant_expression_p=*/false);
15237 break;
15238 case OMP_CLAUSE_REDUCTION:
15239 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
15241 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
15242 if (TREE_CODE (placeholder) == SCOPE_REF)
15244 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
15245 complain, in_decl);
15246 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
15247 = build_qualified_name (NULL_TREE, scope,
15248 TREE_OPERAND (placeholder, 1),
15249 false);
15251 else
15252 gcc_assert (identifier_p (placeholder));
15254 OMP_CLAUSE_DECL (nc)
15255 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15256 in_decl);
15257 break;
15258 case OMP_CLAUSE_GANG:
15259 case OMP_CLAUSE_ALIGNED:
15260 OMP_CLAUSE_DECL (nc)
15261 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15262 in_decl);
15263 OMP_CLAUSE_OPERAND (nc, 1)
15264 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
15265 in_decl, /*integral_constant_expression_p=*/false);
15266 break;
15267 case OMP_CLAUSE_LINEAR:
15268 OMP_CLAUSE_DECL (nc)
15269 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15270 in_decl);
15271 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
15273 gcc_assert (!linear_no_step);
15274 linear_no_step = nc;
15276 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
15277 OMP_CLAUSE_LINEAR_STEP (nc)
15278 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
15279 complain, in_decl);
15280 else
15281 OMP_CLAUSE_LINEAR_STEP (nc)
15282 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
15283 in_decl,
15284 /*integral_constant_expression_p=*/false);
15285 break;
15286 case OMP_CLAUSE_NOWAIT:
15287 case OMP_CLAUSE_DEFAULT:
15288 case OMP_CLAUSE_UNTIED:
15289 case OMP_CLAUSE_MERGEABLE:
15290 case OMP_CLAUSE_INBRANCH:
15291 case OMP_CLAUSE_NOTINBRANCH:
15292 case OMP_CLAUSE_PROC_BIND:
15293 case OMP_CLAUSE_FOR:
15294 case OMP_CLAUSE_PARALLEL:
15295 case OMP_CLAUSE_SECTIONS:
15296 case OMP_CLAUSE_TASKGROUP:
15297 case OMP_CLAUSE_NOGROUP:
15298 case OMP_CLAUSE_THREADS:
15299 case OMP_CLAUSE_SIMD:
15300 case OMP_CLAUSE_DEFAULTMAP:
15301 case OMP_CLAUSE_INDEPENDENT:
15302 case OMP_CLAUSE_AUTO:
15303 case OMP_CLAUSE_SEQ:
15304 break;
15305 default:
15306 gcc_unreachable ();
15308 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
15309 switch (OMP_CLAUSE_CODE (nc))
15311 case OMP_CLAUSE_SHARED:
15312 case OMP_CLAUSE_PRIVATE:
15313 case OMP_CLAUSE_FIRSTPRIVATE:
15314 case OMP_CLAUSE_LASTPRIVATE:
15315 case OMP_CLAUSE_COPYPRIVATE:
15316 case OMP_CLAUSE_LINEAR:
15317 case OMP_CLAUSE_REDUCTION:
15318 case OMP_CLAUSE_USE_DEVICE_PTR:
15319 case OMP_CLAUSE_IS_DEVICE_PTR:
15320 /* tsubst_expr on SCOPE_REF results in returning
15321 finish_non_static_data_member result. Undo that here. */
15322 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
15323 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
15324 == IDENTIFIER_NODE))
15326 tree t = OMP_CLAUSE_DECL (nc);
15327 tree v = t;
15328 while (v)
15329 switch (TREE_CODE (v))
15331 case COMPONENT_REF:
15332 case MEM_REF:
15333 case INDIRECT_REF:
15334 CASE_CONVERT:
15335 case POINTER_PLUS_EXPR:
15336 v = TREE_OPERAND (v, 0);
15337 continue;
15338 case PARM_DECL:
15339 if (DECL_CONTEXT (v) == current_function_decl
15340 && DECL_ARTIFICIAL (v)
15341 && DECL_NAME (v) == this_identifier)
15342 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
15343 /* FALLTHRU */
15344 default:
15345 v = NULL_TREE;
15346 break;
15349 else if (VAR_P (OMP_CLAUSE_DECL (oc))
15350 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
15351 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
15352 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
15353 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
15355 tree decl = OMP_CLAUSE_DECL (nc);
15356 if (VAR_P (decl))
15358 retrofit_lang_decl (decl);
15359 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
15362 break;
15363 default:
15364 break;
15368 new_clauses = nreverse (new_clauses);
15369 if (ort != C_ORT_OMP_DECLARE_SIMD)
15371 new_clauses = finish_omp_clauses (new_clauses, ort);
15372 if (linear_no_step)
15373 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
15374 if (nc == linear_no_step)
15376 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
15377 break;
15380 return new_clauses;
15383 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
15385 static tree
15386 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
15387 tree in_decl)
15389 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
15391 tree purpose, value, chain;
15393 if (t == NULL)
15394 return t;
15396 if (TREE_CODE (t) != TREE_LIST)
15397 return tsubst_copy_and_build (t, args, complain, in_decl,
15398 /*function_p=*/false,
15399 /*integral_constant_expression_p=*/false);
15401 if (t == void_list_node)
15402 return t;
15404 purpose = TREE_PURPOSE (t);
15405 if (purpose)
15406 purpose = RECUR (purpose);
15407 value = TREE_VALUE (t);
15408 if (value)
15410 if (TREE_CODE (value) != LABEL_DECL)
15411 value = RECUR (value);
15412 else
15414 value = lookup_label (DECL_NAME (value));
15415 gcc_assert (TREE_CODE (value) == LABEL_DECL);
15416 TREE_USED (value) = 1;
15419 chain = TREE_CHAIN (t);
15420 if (chain && chain != void_type_node)
15421 chain = RECUR (chain);
15422 return tree_cons (purpose, value, chain);
15423 #undef RECUR
15426 /* Used to temporarily communicate the list of #pragma omp parallel
15427 clauses to #pragma omp for instantiation if they are combined
15428 together. */
15430 static tree *omp_parallel_combined_clauses;
15432 /* Substitute one OMP_FOR iterator. */
15434 static void
15435 tsubst_omp_for_iterator (tree t, int i, tree declv, tree orig_declv,
15436 tree initv, tree condv, tree incrv, tree *clauses,
15437 tree args, tsubst_flags_t complain, tree in_decl,
15438 bool integral_constant_expression_p)
15440 #define RECUR(NODE) \
15441 tsubst_expr ((NODE), args, complain, in_decl, \
15442 integral_constant_expression_p)
15443 tree decl, init, cond, incr;
15445 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
15446 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
15448 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
15450 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
15451 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
15454 decl = TREE_OPERAND (init, 0);
15455 init = TREE_OPERAND (init, 1);
15456 tree decl_expr = NULL_TREE;
15457 if (init && TREE_CODE (init) == DECL_EXPR)
15459 /* We need to jump through some hoops to handle declarations in the
15460 init-statement, since we might need to handle auto deduction,
15461 but we need to keep control of initialization. */
15462 decl_expr = init;
15463 init = DECL_INITIAL (DECL_EXPR_DECL (init));
15464 decl = tsubst_decl (decl, args, complain);
15466 else
15468 if (TREE_CODE (decl) == SCOPE_REF)
15470 decl = RECUR (decl);
15471 if (TREE_CODE (decl) == COMPONENT_REF)
15473 tree v = decl;
15474 while (v)
15475 switch (TREE_CODE (v))
15477 case COMPONENT_REF:
15478 case MEM_REF:
15479 case INDIRECT_REF:
15480 CASE_CONVERT:
15481 case POINTER_PLUS_EXPR:
15482 v = TREE_OPERAND (v, 0);
15483 continue;
15484 case PARM_DECL:
15485 if (DECL_CONTEXT (v) == current_function_decl
15486 && DECL_ARTIFICIAL (v)
15487 && DECL_NAME (v) == this_identifier)
15489 decl = TREE_OPERAND (decl, 1);
15490 decl = omp_privatize_field (decl, false);
15492 /* FALLTHRU */
15493 default:
15494 v = NULL_TREE;
15495 break;
15499 else
15500 decl = RECUR (decl);
15502 init = RECUR (init);
15504 tree auto_node = type_uses_auto (TREE_TYPE (decl));
15505 if (auto_node && init)
15506 TREE_TYPE (decl)
15507 = do_auto_deduction (TREE_TYPE (decl), init, auto_node);
15509 gcc_assert (!type_dependent_expression_p (decl));
15511 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15513 if (decl_expr)
15515 /* Declare the variable, but don't let that initialize it. */
15516 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
15517 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
15518 RECUR (decl_expr);
15519 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
15522 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
15523 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15524 if (TREE_CODE (incr) == MODIFY_EXPR)
15526 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15527 tree rhs = RECUR (TREE_OPERAND (incr, 1));
15528 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
15529 NOP_EXPR, rhs, complain);
15531 else
15532 incr = RECUR (incr);
15533 TREE_VEC_ELT (declv, i) = decl;
15534 TREE_VEC_ELT (initv, i) = init;
15535 TREE_VEC_ELT (condv, i) = cond;
15536 TREE_VEC_ELT (incrv, i) = incr;
15537 return;
15540 if (decl_expr)
15542 /* Declare and initialize the variable. */
15543 RECUR (decl_expr);
15544 init = NULL_TREE;
15546 else if (init)
15548 tree *pc;
15549 int j;
15550 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
15552 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
15554 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
15555 && OMP_CLAUSE_DECL (*pc) == decl)
15556 break;
15557 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
15558 && OMP_CLAUSE_DECL (*pc) == decl)
15560 if (j)
15561 break;
15562 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15563 tree c = *pc;
15564 *pc = OMP_CLAUSE_CHAIN (c);
15565 OMP_CLAUSE_CHAIN (c) = *clauses;
15566 *clauses = c;
15568 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
15569 && OMP_CLAUSE_DECL (*pc) == decl)
15571 error ("iteration variable %qD should not be firstprivate",
15572 decl);
15573 *pc = OMP_CLAUSE_CHAIN (*pc);
15575 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
15576 && OMP_CLAUSE_DECL (*pc) == decl)
15578 error ("iteration variable %qD should not be reduction",
15579 decl);
15580 *pc = OMP_CLAUSE_CHAIN (*pc);
15582 else
15583 pc = &OMP_CLAUSE_CHAIN (*pc);
15585 if (*pc)
15586 break;
15588 if (*pc == NULL_TREE)
15590 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
15591 OMP_CLAUSE_DECL (c) = decl;
15592 c = finish_omp_clauses (c, C_ORT_OMP);
15593 if (c)
15595 OMP_CLAUSE_CHAIN (c) = *clauses;
15596 *clauses = c;
15600 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
15601 if (COMPARISON_CLASS_P (cond))
15603 tree op0 = RECUR (TREE_OPERAND (cond, 0));
15604 tree op1 = RECUR (TREE_OPERAND (cond, 1));
15605 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
15607 else
15608 cond = RECUR (cond);
15609 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15610 switch (TREE_CODE (incr))
15612 case PREINCREMENT_EXPR:
15613 case PREDECREMENT_EXPR:
15614 case POSTINCREMENT_EXPR:
15615 case POSTDECREMENT_EXPR:
15616 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
15617 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
15618 break;
15619 case MODIFY_EXPR:
15620 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15621 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15623 tree rhs = TREE_OPERAND (incr, 1);
15624 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15625 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15626 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15627 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15628 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15629 rhs0, rhs1));
15631 else
15632 incr = RECUR (incr);
15633 break;
15634 case MODOP_EXPR:
15635 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15636 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15638 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15639 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15640 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
15641 TREE_TYPE (decl), lhs,
15642 RECUR (TREE_OPERAND (incr, 2))));
15644 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
15645 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
15646 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
15648 tree rhs = TREE_OPERAND (incr, 2);
15649 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15650 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15651 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15652 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15653 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15654 rhs0, rhs1));
15656 else
15657 incr = RECUR (incr);
15658 break;
15659 default:
15660 incr = RECUR (incr);
15661 break;
15664 TREE_VEC_ELT (declv, i) = decl;
15665 TREE_VEC_ELT (initv, i) = init;
15666 TREE_VEC_ELT (condv, i) = cond;
15667 TREE_VEC_ELT (incrv, i) = incr;
15668 #undef RECUR
15671 /* Helper function of tsubst_expr, find OMP_TEAMS inside
15672 of OMP_TARGET's body. */
15674 static tree
15675 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
15677 *walk_subtrees = 0;
15678 switch (TREE_CODE (*tp))
15680 case OMP_TEAMS:
15681 return *tp;
15682 case BIND_EXPR:
15683 case STATEMENT_LIST:
15684 *walk_subtrees = 1;
15685 break;
15686 default:
15687 break;
15689 return NULL_TREE;
15692 /* Helper function for tsubst_expr. For decomposition declaration
15693 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
15694 also the corresponding decls representing the identifiers
15695 of the decomposition declaration. Return DECL if successful
15696 or error_mark_node otherwise, set *FIRST to the first decl
15697 in the list chained through DECL_CHAIN and *CNT to the number
15698 of such decls. */
15700 static tree
15701 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
15702 tsubst_flags_t complain, tree in_decl, tree *first,
15703 unsigned int *cnt)
15705 tree decl2, decl3, prev = decl;
15706 *cnt = 0;
15707 gcc_assert (DECL_NAME (decl) == NULL_TREE);
15708 for (decl2 = DECL_CHAIN (pattern_decl);
15709 decl2
15710 && VAR_P (decl2)
15711 && DECL_DECOMPOSITION_P (decl2)
15712 && DECL_NAME (decl2);
15713 decl2 = DECL_CHAIN (decl2))
15715 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
15717 gcc_assert (errorcount);
15718 return error_mark_node;
15720 (*cnt)++;
15721 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
15722 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
15723 tree v = DECL_VALUE_EXPR (decl2);
15724 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
15725 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
15726 decl3 = tsubst (decl2, args, complain, in_decl);
15727 SET_DECL_VALUE_EXPR (decl2, v);
15728 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
15729 if (VAR_P (decl3))
15730 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
15731 maybe_push_decl (decl3);
15732 if (error_operand_p (decl3))
15733 decl = error_mark_node;
15734 else if (decl != error_mark_node
15735 && DECL_CHAIN (decl3) != prev)
15737 gcc_assert (errorcount);
15738 decl = error_mark_node;
15740 else
15741 prev = decl3;
15743 *first = prev;
15744 return decl;
15747 /* Like tsubst_copy for expressions, etc. but also does semantic
15748 processing. */
15750 tree
15751 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
15752 bool integral_constant_expression_p)
15754 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
15755 #define RECUR(NODE) \
15756 tsubst_expr ((NODE), args, complain, in_decl, \
15757 integral_constant_expression_p)
15759 tree stmt, tmp;
15760 tree r;
15761 location_t loc;
15763 if (t == NULL_TREE || t == error_mark_node)
15764 return t;
15766 loc = input_location;
15767 if (EXPR_HAS_LOCATION (t))
15768 input_location = EXPR_LOCATION (t);
15769 if (STATEMENT_CODE_P (TREE_CODE (t)))
15770 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
15772 switch (TREE_CODE (t))
15774 case STATEMENT_LIST:
15776 tree_stmt_iterator i;
15777 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
15778 RECUR (tsi_stmt (i));
15779 break;
15782 case CTOR_INITIALIZER:
15783 finish_mem_initializers (tsubst_initializer_list
15784 (TREE_OPERAND (t, 0), args));
15785 break;
15787 case RETURN_EXPR:
15788 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
15789 break;
15791 case EXPR_STMT:
15792 tmp = RECUR (EXPR_STMT_EXPR (t));
15793 if (EXPR_STMT_STMT_EXPR_RESULT (t))
15794 finish_stmt_expr_expr (tmp, cur_stmt_expr);
15795 else
15796 finish_expr_stmt (tmp);
15797 break;
15799 case USING_STMT:
15800 finish_local_using_directive (USING_STMT_NAMESPACE (t),
15801 /*attribs=*/NULL_TREE);
15802 break;
15804 case DECL_EXPR:
15806 tree decl, pattern_decl;
15807 tree init;
15809 pattern_decl = decl = DECL_EXPR_DECL (t);
15810 if (TREE_CODE (decl) == LABEL_DECL)
15811 finish_label_decl (DECL_NAME (decl));
15812 else if (TREE_CODE (decl) == USING_DECL)
15814 tree scope = USING_DECL_SCOPE (decl);
15815 tree name = DECL_NAME (decl);
15817 scope = tsubst (scope, args, complain, in_decl);
15818 decl = lookup_qualified_name (scope, name,
15819 /*is_type_p=*/false,
15820 /*complain=*/false);
15821 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
15822 qualified_name_lookup_error (scope, name, decl, input_location);
15823 else
15824 finish_local_using_decl (decl, scope, name);
15826 else if (DECL_PACK_P (decl))
15828 /* Don't build up decls for a variadic capture proxy, we'll
15829 instantiate the elements directly as needed. */
15830 break;
15832 else
15834 init = DECL_INITIAL (decl);
15835 decl = tsubst (decl, args, complain, in_decl);
15836 if (decl != error_mark_node)
15838 /* By marking the declaration as instantiated, we avoid
15839 trying to instantiate it. Since instantiate_decl can't
15840 handle local variables, and since we've already done
15841 all that needs to be done, that's the right thing to
15842 do. */
15843 if (VAR_P (decl))
15844 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
15845 if (VAR_P (decl)
15846 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
15847 /* Anonymous aggregates are a special case. */
15848 finish_anon_union (decl);
15849 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
15851 DECL_CONTEXT (decl) = current_function_decl;
15852 if (DECL_NAME (decl) == this_identifier)
15854 tree lam = DECL_CONTEXT (current_function_decl);
15855 lam = CLASSTYPE_LAMBDA_EXPR (lam);
15856 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
15858 insert_capture_proxy (decl);
15860 else if (DECL_IMPLICIT_TYPEDEF_P (t))
15861 /* We already did a pushtag. */;
15862 else if (TREE_CODE (decl) == FUNCTION_DECL
15863 && DECL_OMP_DECLARE_REDUCTION_P (decl)
15864 && DECL_FUNCTION_SCOPE_P (pattern_decl))
15866 DECL_CONTEXT (decl) = NULL_TREE;
15867 pushdecl (decl);
15868 DECL_CONTEXT (decl) = current_function_decl;
15869 cp_check_omp_declare_reduction (decl);
15871 else
15873 int const_init = false;
15874 maybe_push_decl (decl);
15875 if (VAR_P (decl)
15876 && DECL_PRETTY_FUNCTION_P (decl))
15878 /* For __PRETTY_FUNCTION__ we have to adjust the
15879 initializer. */
15880 const char *const name
15881 = cxx_printable_name (current_function_decl, 2);
15882 init = cp_fname_init (name, &TREE_TYPE (decl));
15884 else
15885 init = tsubst_init (init, decl, args, complain, in_decl);
15887 if (VAR_P (decl))
15888 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
15889 (pattern_decl));
15890 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
15891 if (VAR_P (decl)
15892 && DECL_DECOMPOSITION_P (decl)
15893 && TREE_TYPE (pattern_decl) != error_mark_node)
15895 unsigned int cnt;
15896 tree first;
15897 decl = tsubst_decomp_names (decl, pattern_decl, args,
15898 complain, in_decl, &first,
15899 &cnt);
15900 if (decl != error_mark_node)
15901 cp_finish_decomp (decl, first, cnt);
15907 break;
15910 case FOR_STMT:
15911 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
15912 RECUR (FOR_INIT_STMT (t));
15913 finish_init_stmt (stmt);
15914 tmp = RECUR (FOR_COND (t));
15915 finish_for_cond (tmp, stmt, false);
15916 tmp = RECUR (FOR_EXPR (t));
15917 finish_for_expr (tmp, stmt);
15918 RECUR (FOR_BODY (t));
15919 finish_for_stmt (stmt);
15920 break;
15922 case RANGE_FOR_STMT:
15924 tree decl, expr;
15925 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
15926 decl = RANGE_FOR_DECL (t);
15927 decl = tsubst (decl, args, complain, in_decl);
15928 maybe_push_decl (decl);
15929 expr = RECUR (RANGE_FOR_EXPR (t));
15930 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
15932 unsigned int cnt;
15933 tree first;
15934 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
15935 complain, in_decl, &first, &cnt);
15936 stmt = cp_convert_range_for (stmt, decl, expr, first, cnt,
15937 RANGE_FOR_IVDEP (t));
15939 else
15940 stmt = cp_convert_range_for (stmt, decl, expr, NULL_TREE, 0,
15941 RANGE_FOR_IVDEP (t));
15942 RECUR (RANGE_FOR_BODY (t));
15943 finish_for_stmt (stmt);
15945 break;
15947 case WHILE_STMT:
15948 stmt = begin_while_stmt ();
15949 tmp = RECUR (WHILE_COND (t));
15950 finish_while_stmt_cond (tmp, stmt, false);
15951 RECUR (WHILE_BODY (t));
15952 finish_while_stmt (stmt);
15953 break;
15955 case DO_STMT:
15956 stmt = begin_do_stmt ();
15957 RECUR (DO_BODY (t));
15958 finish_do_body (stmt);
15959 tmp = RECUR (DO_COND (t));
15960 finish_do_stmt (tmp, stmt, false);
15961 break;
15963 case IF_STMT:
15964 stmt = begin_if_stmt ();
15965 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
15966 tmp = RECUR (IF_COND (t));
15967 tmp = finish_if_stmt_cond (tmp, stmt);
15968 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
15969 /* Don't instantiate the THEN_CLAUSE. */;
15970 else
15972 bool inhibit = integer_zerop (fold_non_dependent_expr (tmp));
15973 if (inhibit)
15974 ++c_inhibit_evaluation_warnings;
15975 RECUR (THEN_CLAUSE (t));
15976 if (inhibit)
15977 --c_inhibit_evaluation_warnings;
15979 finish_then_clause (stmt);
15981 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
15982 /* Don't instantiate the ELSE_CLAUSE. */;
15983 else if (ELSE_CLAUSE (t))
15985 bool inhibit = integer_nonzerop (fold_non_dependent_expr (tmp));
15986 begin_else_clause (stmt);
15987 if (inhibit)
15988 ++c_inhibit_evaluation_warnings;
15989 RECUR (ELSE_CLAUSE (t));
15990 if (inhibit)
15991 --c_inhibit_evaluation_warnings;
15992 finish_else_clause (stmt);
15995 finish_if_stmt (stmt);
15996 break;
15998 case BIND_EXPR:
15999 if (BIND_EXPR_BODY_BLOCK (t))
16000 stmt = begin_function_body ();
16001 else
16002 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
16003 ? BCS_TRY_BLOCK : 0);
16005 RECUR (BIND_EXPR_BODY (t));
16007 if (BIND_EXPR_BODY_BLOCK (t))
16008 finish_function_body (stmt);
16009 else
16010 finish_compound_stmt (stmt);
16011 break;
16013 case BREAK_STMT:
16014 finish_break_stmt ();
16015 break;
16017 case CONTINUE_STMT:
16018 finish_continue_stmt ();
16019 break;
16021 case SWITCH_STMT:
16022 stmt = begin_switch_stmt ();
16023 tmp = RECUR (SWITCH_STMT_COND (t));
16024 finish_switch_cond (tmp, stmt);
16025 RECUR (SWITCH_STMT_BODY (t));
16026 finish_switch_stmt (stmt);
16027 break;
16029 case CASE_LABEL_EXPR:
16031 tree low = RECUR (CASE_LOW (t));
16032 tree high = RECUR (CASE_HIGH (t));
16033 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
16034 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
16035 FALLTHROUGH_LABEL_P (CASE_LABEL (l))
16036 = FALLTHROUGH_LABEL_P (CASE_LABEL (t));
16038 break;
16040 case LABEL_EXPR:
16042 tree decl = LABEL_EXPR_LABEL (t);
16043 tree label;
16045 label = finish_label_stmt (DECL_NAME (decl));
16046 if (TREE_CODE (label) == LABEL_DECL)
16047 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
16048 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
16049 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
16051 break;
16053 case GOTO_EXPR:
16054 tmp = GOTO_DESTINATION (t);
16055 if (TREE_CODE (tmp) != LABEL_DECL)
16056 /* Computed goto's must be tsubst'd into. On the other hand,
16057 non-computed gotos must not be; the identifier in question
16058 will have no binding. */
16059 tmp = RECUR (tmp);
16060 else
16061 tmp = DECL_NAME (tmp);
16062 finish_goto_stmt (tmp);
16063 break;
16065 case ASM_EXPR:
16067 tree string = RECUR (ASM_STRING (t));
16068 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
16069 complain, in_decl);
16070 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
16071 complain, in_decl);
16072 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
16073 complain, in_decl);
16074 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
16075 complain, in_decl);
16076 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
16077 clobbers, labels);
16078 tree asm_expr = tmp;
16079 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
16080 asm_expr = TREE_OPERAND (asm_expr, 0);
16081 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
16083 break;
16085 case TRY_BLOCK:
16086 if (CLEANUP_P (t))
16088 stmt = begin_try_block ();
16089 RECUR (TRY_STMTS (t));
16090 finish_cleanup_try_block (stmt);
16091 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
16093 else
16095 tree compound_stmt = NULL_TREE;
16097 if (FN_TRY_BLOCK_P (t))
16098 stmt = begin_function_try_block (&compound_stmt);
16099 else
16100 stmt = begin_try_block ();
16102 RECUR (TRY_STMTS (t));
16104 if (FN_TRY_BLOCK_P (t))
16105 finish_function_try_block (stmt);
16106 else
16107 finish_try_block (stmt);
16109 RECUR (TRY_HANDLERS (t));
16110 if (FN_TRY_BLOCK_P (t))
16111 finish_function_handler_sequence (stmt, compound_stmt);
16112 else
16113 finish_handler_sequence (stmt);
16115 break;
16117 case HANDLER:
16119 tree decl = HANDLER_PARMS (t);
16121 if (decl)
16123 decl = tsubst (decl, args, complain, in_decl);
16124 /* Prevent instantiate_decl from trying to instantiate
16125 this variable. We've already done all that needs to be
16126 done. */
16127 if (decl != error_mark_node)
16128 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16130 stmt = begin_handler ();
16131 finish_handler_parms (decl, stmt);
16132 RECUR (HANDLER_BODY (t));
16133 finish_handler (stmt);
16135 break;
16137 case TAG_DEFN:
16138 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
16139 if (CLASS_TYPE_P (tmp))
16141 /* Local classes are not independent templates; they are
16142 instantiated along with their containing function. And this
16143 way we don't have to deal with pushing out of one local class
16144 to instantiate a member of another local class. */
16145 tree fn;
16146 /* Closures are handled by the LAMBDA_EXPR. */
16147 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
16148 complete_type (tmp);
16149 for (fn = TYPE_METHODS (tmp); fn; fn = DECL_CHAIN (fn))
16150 if (!DECL_ARTIFICIAL (fn))
16151 instantiate_decl (fn, /*defer_ok=*/false,
16152 /*expl_inst_class=*/false);
16154 break;
16156 case STATIC_ASSERT:
16158 tree condition;
16160 ++c_inhibit_evaluation_warnings;
16161 condition =
16162 tsubst_expr (STATIC_ASSERT_CONDITION (t),
16163 args,
16164 complain, in_decl,
16165 /*integral_constant_expression_p=*/true);
16166 --c_inhibit_evaluation_warnings;
16168 finish_static_assert (condition,
16169 STATIC_ASSERT_MESSAGE (t),
16170 STATIC_ASSERT_SOURCE_LOCATION (t),
16171 /*member_p=*/false);
16173 break;
16175 case OACC_KERNELS:
16176 case OACC_PARALLEL:
16177 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
16178 in_decl);
16179 stmt = begin_omp_parallel ();
16180 RECUR (OMP_BODY (t));
16181 finish_omp_construct (TREE_CODE (t), stmt, tmp);
16182 break;
16184 case OMP_PARALLEL:
16185 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
16186 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
16187 complain, in_decl);
16188 if (OMP_PARALLEL_COMBINED (t))
16189 omp_parallel_combined_clauses = &tmp;
16190 stmt = begin_omp_parallel ();
16191 RECUR (OMP_PARALLEL_BODY (t));
16192 gcc_assert (omp_parallel_combined_clauses == NULL);
16193 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
16194 = OMP_PARALLEL_COMBINED (t);
16195 pop_omp_privatization_clauses (r);
16196 break;
16198 case OMP_TASK:
16199 r = push_omp_privatization_clauses (false);
16200 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
16201 complain, in_decl);
16202 stmt = begin_omp_task ();
16203 RECUR (OMP_TASK_BODY (t));
16204 finish_omp_task (tmp, stmt);
16205 pop_omp_privatization_clauses (r);
16206 break;
16208 case OMP_FOR:
16209 case OMP_SIMD:
16210 case CILK_SIMD:
16211 case CILK_FOR:
16212 case OMP_DISTRIBUTE:
16213 case OMP_TASKLOOP:
16214 case OACC_LOOP:
16216 tree clauses, body, pre_body;
16217 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
16218 tree orig_declv = NULL_TREE;
16219 tree incrv = NULL_TREE;
16220 enum c_omp_region_type ort = C_ORT_OMP;
16221 int i;
16223 if (TREE_CODE (t) == CILK_SIMD || TREE_CODE (t) == CILK_FOR)
16224 ort = C_ORT_CILK;
16225 else if (TREE_CODE (t) == OACC_LOOP)
16226 ort = C_ORT_ACC;
16228 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
16229 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
16230 in_decl);
16231 if (OMP_FOR_INIT (t) != NULL_TREE)
16233 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16234 if (OMP_FOR_ORIG_DECLS (t))
16235 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16236 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16237 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16238 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16241 stmt = begin_omp_structured_block ();
16243 pre_body = push_stmt_list ();
16244 RECUR (OMP_FOR_PRE_BODY (t));
16245 pre_body = pop_stmt_list (pre_body);
16247 if (OMP_FOR_INIT (t) != NULL_TREE)
16248 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
16249 tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, condv,
16250 incrv, &clauses, args, complain, in_decl,
16251 integral_constant_expression_p);
16252 omp_parallel_combined_clauses = NULL;
16254 body = push_stmt_list ();
16255 RECUR (OMP_FOR_BODY (t));
16256 body = pop_stmt_list (body);
16258 if (OMP_FOR_INIT (t) != NULL_TREE)
16259 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
16260 orig_declv, initv, condv, incrv, body, pre_body,
16261 NULL, clauses);
16262 else
16264 t = make_node (TREE_CODE (t));
16265 TREE_TYPE (t) = void_type_node;
16266 OMP_FOR_BODY (t) = body;
16267 OMP_FOR_PRE_BODY (t) = pre_body;
16268 OMP_FOR_CLAUSES (t) = clauses;
16269 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
16270 add_stmt (t);
16273 add_stmt (finish_omp_structured_block (stmt));
16274 pop_omp_privatization_clauses (r);
16276 break;
16278 case OMP_SECTIONS:
16279 omp_parallel_combined_clauses = NULL;
16280 /* FALLTHRU */
16281 case OMP_SINGLE:
16282 case OMP_TEAMS:
16283 case OMP_CRITICAL:
16284 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
16285 && OMP_TEAMS_COMBINED (t));
16286 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
16287 in_decl);
16288 stmt = push_stmt_list ();
16289 RECUR (OMP_BODY (t));
16290 stmt = pop_stmt_list (stmt);
16292 t = copy_node (t);
16293 OMP_BODY (t) = stmt;
16294 OMP_CLAUSES (t) = tmp;
16295 add_stmt (t);
16296 pop_omp_privatization_clauses (r);
16297 break;
16299 case OACC_DATA:
16300 case OMP_TARGET_DATA:
16301 case OMP_TARGET:
16302 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
16303 ? C_ORT_ACC : C_ORT_OMP, args, complain,
16304 in_decl);
16305 keep_next_level (true);
16306 stmt = begin_omp_structured_block ();
16308 RECUR (OMP_BODY (t));
16309 stmt = finish_omp_structured_block (stmt);
16311 t = copy_node (t);
16312 OMP_BODY (t) = stmt;
16313 OMP_CLAUSES (t) = tmp;
16314 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
16316 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
16317 if (teams)
16319 /* For combined target teams, ensure the num_teams and
16320 thread_limit clause expressions are evaluated on the host,
16321 before entering the target construct. */
16322 tree c;
16323 for (c = OMP_TEAMS_CLAUSES (teams);
16324 c; c = OMP_CLAUSE_CHAIN (c))
16325 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
16326 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
16327 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
16329 tree expr = OMP_CLAUSE_OPERAND (c, 0);
16330 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
16331 if (expr == error_mark_node)
16332 continue;
16333 tmp = TARGET_EXPR_SLOT (expr);
16334 add_stmt (expr);
16335 OMP_CLAUSE_OPERAND (c, 0) = expr;
16336 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16337 OMP_CLAUSE_FIRSTPRIVATE);
16338 OMP_CLAUSE_DECL (tc) = tmp;
16339 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
16340 OMP_TARGET_CLAUSES (t) = tc;
16344 add_stmt (t);
16345 break;
16347 case OACC_DECLARE:
16348 t = copy_node (t);
16349 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
16350 complain, in_decl);
16351 OACC_DECLARE_CLAUSES (t) = tmp;
16352 add_stmt (t);
16353 break;
16355 case OMP_TARGET_UPDATE:
16356 case OMP_TARGET_ENTER_DATA:
16357 case OMP_TARGET_EXIT_DATA:
16358 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
16359 complain, in_decl);
16360 t = copy_node (t);
16361 OMP_STANDALONE_CLAUSES (t) = tmp;
16362 add_stmt (t);
16363 break;
16365 case OACC_ENTER_DATA:
16366 case OACC_EXIT_DATA:
16367 case OACC_UPDATE:
16368 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
16369 complain, in_decl);
16370 t = copy_node (t);
16371 OMP_STANDALONE_CLAUSES (t) = tmp;
16372 add_stmt (t);
16373 break;
16375 case OMP_ORDERED:
16376 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
16377 complain, in_decl);
16378 stmt = push_stmt_list ();
16379 RECUR (OMP_BODY (t));
16380 stmt = pop_stmt_list (stmt);
16382 t = copy_node (t);
16383 OMP_BODY (t) = stmt;
16384 OMP_ORDERED_CLAUSES (t) = tmp;
16385 add_stmt (t);
16386 break;
16388 case OMP_SECTION:
16389 case OMP_MASTER:
16390 case OMP_TASKGROUP:
16391 stmt = push_stmt_list ();
16392 RECUR (OMP_BODY (t));
16393 stmt = pop_stmt_list (stmt);
16395 t = copy_node (t);
16396 OMP_BODY (t) = stmt;
16397 add_stmt (t);
16398 break;
16400 case OMP_ATOMIC:
16401 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
16402 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
16404 tree op1 = TREE_OPERAND (t, 1);
16405 tree rhs1 = NULL_TREE;
16406 tree lhs, rhs;
16407 if (TREE_CODE (op1) == COMPOUND_EXPR)
16409 rhs1 = RECUR (TREE_OPERAND (op1, 0));
16410 op1 = TREE_OPERAND (op1, 1);
16412 lhs = RECUR (TREE_OPERAND (op1, 0));
16413 rhs = RECUR (TREE_OPERAND (op1, 1));
16414 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
16415 NULL_TREE, NULL_TREE, rhs1,
16416 OMP_ATOMIC_SEQ_CST (t));
16418 else
16420 tree op1 = TREE_OPERAND (t, 1);
16421 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
16422 tree rhs1 = NULL_TREE;
16423 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
16424 enum tree_code opcode = NOP_EXPR;
16425 if (code == OMP_ATOMIC_READ)
16427 v = RECUR (TREE_OPERAND (op1, 0));
16428 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16430 else if (code == OMP_ATOMIC_CAPTURE_OLD
16431 || code == OMP_ATOMIC_CAPTURE_NEW)
16433 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
16434 v = RECUR (TREE_OPERAND (op1, 0));
16435 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16436 if (TREE_CODE (op11) == COMPOUND_EXPR)
16438 rhs1 = RECUR (TREE_OPERAND (op11, 0));
16439 op11 = TREE_OPERAND (op11, 1);
16441 lhs = RECUR (TREE_OPERAND (op11, 0));
16442 rhs = RECUR (TREE_OPERAND (op11, 1));
16443 opcode = TREE_CODE (op11);
16444 if (opcode == MODIFY_EXPR)
16445 opcode = NOP_EXPR;
16447 else
16449 code = OMP_ATOMIC;
16450 lhs = RECUR (TREE_OPERAND (op1, 0));
16451 rhs = RECUR (TREE_OPERAND (op1, 1));
16453 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
16454 OMP_ATOMIC_SEQ_CST (t));
16456 break;
16458 case TRANSACTION_EXPR:
16460 int flags = 0;
16461 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
16462 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
16464 if (TRANSACTION_EXPR_IS_STMT (t))
16466 tree body = TRANSACTION_EXPR_BODY (t);
16467 tree noex = NULL_TREE;
16468 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
16470 noex = MUST_NOT_THROW_COND (body);
16471 if (noex == NULL_TREE)
16472 noex = boolean_true_node;
16473 body = TREE_OPERAND (body, 0);
16475 stmt = begin_transaction_stmt (input_location, NULL, flags);
16476 RECUR (body);
16477 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
16479 else
16481 stmt = build_transaction_expr (EXPR_LOCATION (t),
16482 RECUR (TRANSACTION_EXPR_BODY (t)),
16483 flags, NULL_TREE);
16484 RETURN (stmt);
16487 break;
16489 case MUST_NOT_THROW_EXPR:
16491 tree op0 = RECUR (TREE_OPERAND (t, 0));
16492 tree cond = RECUR (MUST_NOT_THROW_COND (t));
16493 RETURN (build_must_not_throw_expr (op0, cond));
16496 case EXPR_PACK_EXPANSION:
16497 error ("invalid use of pack expansion expression");
16498 RETURN (error_mark_node);
16500 case NONTYPE_ARGUMENT_PACK:
16501 error ("use %<...%> to expand argument pack");
16502 RETURN (error_mark_node);
16504 case CILK_SPAWN_STMT:
16505 cfun->calls_cilk_spawn = 1;
16506 RETURN (build_cilk_spawn (EXPR_LOCATION (t), RECUR (CILK_SPAWN_FN (t))));
16508 case CILK_SYNC_STMT:
16509 RETURN (build_cilk_sync ());
16511 case COMPOUND_EXPR:
16512 tmp = RECUR (TREE_OPERAND (t, 0));
16513 if (tmp == NULL_TREE)
16514 /* If the first operand was a statement, we're done with it. */
16515 RETURN (RECUR (TREE_OPERAND (t, 1)));
16516 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
16517 RECUR (TREE_OPERAND (t, 1)),
16518 complain));
16520 case ANNOTATE_EXPR:
16521 tmp = RECUR (TREE_OPERAND (t, 0));
16522 RETURN (build2_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
16523 TREE_TYPE (tmp), tmp, RECUR (TREE_OPERAND (t, 1))));
16525 default:
16526 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
16528 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
16529 /*function_p=*/false,
16530 integral_constant_expression_p));
16533 RETURN (NULL_TREE);
16534 out:
16535 input_location = loc;
16536 return r;
16537 #undef RECUR
16538 #undef RETURN
16541 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
16542 function. For description of the body see comment above
16543 cp_parser_omp_declare_reduction_exprs. */
16545 static void
16546 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16548 if (t == NULL_TREE || t == error_mark_node)
16549 return;
16551 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
16553 tree_stmt_iterator tsi;
16554 int i;
16555 tree stmts[7];
16556 memset (stmts, 0, sizeof stmts);
16557 for (i = 0, tsi = tsi_start (t);
16558 i < 7 && !tsi_end_p (tsi);
16559 i++, tsi_next (&tsi))
16560 stmts[i] = tsi_stmt (tsi);
16561 gcc_assert (tsi_end_p (tsi));
16563 if (i >= 3)
16565 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
16566 && TREE_CODE (stmts[1]) == DECL_EXPR);
16567 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
16568 args, complain, in_decl);
16569 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
16570 args, complain, in_decl);
16571 DECL_CONTEXT (omp_out) = current_function_decl;
16572 DECL_CONTEXT (omp_in) = current_function_decl;
16573 keep_next_level (true);
16574 tree block = begin_omp_structured_block ();
16575 tsubst_expr (stmts[2], args, complain, in_decl, false);
16576 block = finish_omp_structured_block (block);
16577 block = maybe_cleanup_point_expr_void (block);
16578 add_decl_expr (omp_out);
16579 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
16580 TREE_NO_WARNING (omp_out) = 1;
16581 add_decl_expr (omp_in);
16582 finish_expr_stmt (block);
16584 if (i >= 6)
16586 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
16587 && TREE_CODE (stmts[4]) == DECL_EXPR);
16588 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
16589 args, complain, in_decl);
16590 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
16591 args, complain, in_decl);
16592 DECL_CONTEXT (omp_priv) = current_function_decl;
16593 DECL_CONTEXT (omp_orig) = current_function_decl;
16594 keep_next_level (true);
16595 tree block = begin_omp_structured_block ();
16596 tsubst_expr (stmts[5], args, complain, in_decl, false);
16597 block = finish_omp_structured_block (block);
16598 block = maybe_cleanup_point_expr_void (block);
16599 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
16600 add_decl_expr (omp_priv);
16601 add_decl_expr (omp_orig);
16602 finish_expr_stmt (block);
16603 if (i == 7)
16604 add_decl_expr (omp_orig);
16608 /* T is a postfix-expression that is not being used in a function
16609 call. Return the substituted version of T. */
16611 static tree
16612 tsubst_non_call_postfix_expression (tree t, tree args,
16613 tsubst_flags_t complain,
16614 tree in_decl)
16616 if (TREE_CODE (t) == SCOPE_REF)
16617 t = tsubst_qualified_id (t, args, complain, in_decl,
16618 /*done=*/false, /*address_p=*/false);
16619 else
16620 t = tsubst_copy_and_build (t, args, complain, in_decl,
16621 /*function_p=*/false,
16622 /*integral_constant_expression_p=*/false);
16624 return t;
16627 /* Like tsubst but deals with expressions and performs semantic
16628 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
16630 tree
16631 tsubst_copy_and_build (tree t,
16632 tree args,
16633 tsubst_flags_t complain,
16634 tree in_decl,
16635 bool function_p,
16636 bool integral_constant_expression_p)
16638 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
16639 #define RECUR(NODE) \
16640 tsubst_copy_and_build (NODE, args, complain, in_decl, \
16641 /*function_p=*/false, \
16642 integral_constant_expression_p)
16644 tree retval, op1;
16645 location_t loc;
16647 if (t == NULL_TREE || t == error_mark_node)
16648 return t;
16650 loc = input_location;
16651 if (EXPR_HAS_LOCATION (t))
16652 input_location = EXPR_LOCATION (t);
16654 /* N3276 decltype magic only applies to calls at the top level or on the
16655 right side of a comma. */
16656 tsubst_flags_t decltype_flag = (complain & tf_decltype);
16657 complain &= ~tf_decltype;
16659 switch (TREE_CODE (t))
16661 case USING_DECL:
16662 t = DECL_NAME (t);
16663 /* Fall through. */
16664 case IDENTIFIER_NODE:
16666 tree decl;
16667 cp_id_kind idk;
16668 bool non_integral_constant_expression_p;
16669 const char *error_msg;
16671 if (IDENTIFIER_TYPENAME_P (t))
16673 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16674 t = mangle_conv_op_name_for_type (new_type);
16677 /* Look up the name. */
16678 decl = lookup_name (t);
16680 /* By convention, expressions use ERROR_MARK_NODE to indicate
16681 failure, not NULL_TREE. */
16682 if (decl == NULL_TREE)
16683 decl = error_mark_node;
16685 decl = finish_id_expression (t, decl, NULL_TREE,
16686 &idk,
16687 integral_constant_expression_p,
16688 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
16689 &non_integral_constant_expression_p,
16690 /*template_p=*/false,
16691 /*done=*/true,
16692 /*address_p=*/false,
16693 /*template_arg_p=*/false,
16694 &error_msg,
16695 input_location);
16696 if (error_msg)
16697 error (error_msg);
16698 if (!function_p && identifier_p (decl))
16700 if (complain & tf_error)
16701 unqualified_name_lookup_error (decl);
16702 decl = error_mark_node;
16704 RETURN (decl);
16707 case TEMPLATE_ID_EXPR:
16709 tree object;
16710 tree templ = RECUR (TREE_OPERAND (t, 0));
16711 tree targs = TREE_OPERAND (t, 1);
16713 if (targs)
16714 targs = tsubst_template_args (targs, args, complain, in_decl);
16715 if (targs == error_mark_node)
16716 return error_mark_node;
16718 if (TREE_CODE (templ) == SCOPE_REF)
16720 tree name = TREE_OPERAND (templ, 1);
16721 tree tid = lookup_template_function (name, targs);
16722 TREE_OPERAND (templ, 1) = tid;
16723 return templ;
16726 if (variable_template_p (templ))
16727 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
16729 if (TREE_CODE (templ) == COMPONENT_REF)
16731 object = TREE_OPERAND (templ, 0);
16732 templ = TREE_OPERAND (templ, 1);
16734 else
16735 object = NULL_TREE;
16736 templ = lookup_template_function (templ, targs);
16738 if (object)
16739 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
16740 object, templ, NULL_TREE));
16741 else
16742 RETURN (baselink_for_fns (templ));
16745 case INDIRECT_REF:
16747 tree r = RECUR (TREE_OPERAND (t, 0));
16749 if (REFERENCE_REF_P (t))
16751 /* A type conversion to reference type will be enclosed in
16752 such an indirect ref, but the substitution of the cast
16753 will have also added such an indirect ref. */
16754 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
16755 r = convert_from_reference (r);
16757 else
16758 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
16759 complain|decltype_flag);
16761 if (TREE_CODE (r) == INDIRECT_REF)
16762 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
16764 RETURN (r);
16767 case NOP_EXPR:
16769 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16770 tree op0 = RECUR (TREE_OPERAND (t, 0));
16771 RETURN (build_nop (type, op0));
16774 case IMPLICIT_CONV_EXPR:
16776 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16777 tree expr = RECUR (TREE_OPERAND (t, 0));
16778 int flags = LOOKUP_IMPLICIT;
16779 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
16780 flags = LOOKUP_NORMAL;
16781 RETURN (perform_implicit_conversion_flags (type, expr, complain,
16782 flags));
16785 case CONVERT_EXPR:
16787 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16788 tree op0 = RECUR (TREE_OPERAND (t, 0));
16789 RETURN (build1 (CONVERT_EXPR, type, op0));
16792 case CAST_EXPR:
16793 case REINTERPRET_CAST_EXPR:
16794 case CONST_CAST_EXPR:
16795 case DYNAMIC_CAST_EXPR:
16796 case STATIC_CAST_EXPR:
16798 tree type;
16799 tree op, r = NULL_TREE;
16801 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16802 if (integral_constant_expression_p
16803 && !cast_valid_in_integral_constant_expression_p (type))
16805 if (complain & tf_error)
16806 error ("a cast to a type other than an integral or "
16807 "enumeration type cannot appear in a constant-expression");
16808 RETURN (error_mark_node);
16811 op = RECUR (TREE_OPERAND (t, 0));
16813 warning_sentinel s(warn_useless_cast);
16814 switch (TREE_CODE (t))
16816 case CAST_EXPR:
16817 r = build_functional_cast (type, op, complain);
16818 break;
16819 case REINTERPRET_CAST_EXPR:
16820 r = build_reinterpret_cast (type, op, complain);
16821 break;
16822 case CONST_CAST_EXPR:
16823 r = build_const_cast (type, op, complain);
16824 break;
16825 case DYNAMIC_CAST_EXPR:
16826 r = build_dynamic_cast (type, op, complain);
16827 break;
16828 case STATIC_CAST_EXPR:
16829 r = build_static_cast (type, op, complain);
16830 break;
16831 default:
16832 gcc_unreachable ();
16835 RETURN (r);
16838 case POSTDECREMENT_EXPR:
16839 case POSTINCREMENT_EXPR:
16840 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16841 args, complain, in_decl);
16842 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
16843 complain|decltype_flag));
16845 case PREDECREMENT_EXPR:
16846 case PREINCREMENT_EXPR:
16847 case NEGATE_EXPR:
16848 case BIT_NOT_EXPR:
16849 case ABS_EXPR:
16850 case TRUTH_NOT_EXPR:
16851 case UNARY_PLUS_EXPR: /* Unary + */
16852 case REALPART_EXPR:
16853 case IMAGPART_EXPR:
16854 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
16855 RECUR (TREE_OPERAND (t, 0)),
16856 complain|decltype_flag));
16858 case FIX_TRUNC_EXPR:
16859 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
16860 false, complain));
16862 case ADDR_EXPR:
16863 op1 = TREE_OPERAND (t, 0);
16864 if (TREE_CODE (op1) == LABEL_DECL)
16865 RETURN (finish_label_address_expr (DECL_NAME (op1),
16866 EXPR_LOCATION (op1)));
16867 if (TREE_CODE (op1) == SCOPE_REF)
16868 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
16869 /*done=*/true, /*address_p=*/true);
16870 else
16871 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
16872 in_decl);
16873 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
16874 complain|decltype_flag));
16876 case PLUS_EXPR:
16877 case MINUS_EXPR:
16878 case MULT_EXPR:
16879 case TRUNC_DIV_EXPR:
16880 case CEIL_DIV_EXPR:
16881 case FLOOR_DIV_EXPR:
16882 case ROUND_DIV_EXPR:
16883 case EXACT_DIV_EXPR:
16884 case BIT_AND_EXPR:
16885 case BIT_IOR_EXPR:
16886 case BIT_XOR_EXPR:
16887 case TRUNC_MOD_EXPR:
16888 case FLOOR_MOD_EXPR:
16889 case TRUTH_ANDIF_EXPR:
16890 case TRUTH_ORIF_EXPR:
16891 case TRUTH_AND_EXPR:
16892 case TRUTH_OR_EXPR:
16893 case RSHIFT_EXPR:
16894 case LSHIFT_EXPR:
16895 case RROTATE_EXPR:
16896 case LROTATE_EXPR:
16897 case EQ_EXPR:
16898 case NE_EXPR:
16899 case MAX_EXPR:
16900 case MIN_EXPR:
16901 case LE_EXPR:
16902 case GE_EXPR:
16903 case LT_EXPR:
16904 case GT_EXPR:
16905 case MEMBER_REF:
16906 case DOTSTAR_EXPR:
16908 warning_sentinel s1(warn_type_limits);
16909 warning_sentinel s2(warn_div_by_zero);
16910 warning_sentinel s3(warn_logical_op);
16911 warning_sentinel s4(warn_tautological_compare);
16912 tree op0 = RECUR (TREE_OPERAND (t, 0));
16913 tree op1 = RECUR (TREE_OPERAND (t, 1));
16914 tree r = build_x_binary_op
16915 (input_location, TREE_CODE (t),
16916 op0,
16917 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
16918 ? ERROR_MARK
16919 : TREE_CODE (TREE_OPERAND (t, 0))),
16920 op1,
16921 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
16922 ? ERROR_MARK
16923 : TREE_CODE (TREE_OPERAND (t, 1))),
16924 /*overload=*/NULL,
16925 complain|decltype_flag);
16926 if (EXPR_P (r) && TREE_NO_WARNING (t))
16927 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16929 RETURN (r);
16932 case POINTER_PLUS_EXPR:
16934 tree op0 = RECUR (TREE_OPERAND (t, 0));
16935 tree op1 = RECUR (TREE_OPERAND (t, 1));
16936 return fold_build_pointer_plus (op0, op1);
16939 case SCOPE_REF:
16940 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
16941 /*address_p=*/false));
16942 case ARRAY_REF:
16943 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16944 args, complain, in_decl);
16945 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
16946 RECUR (TREE_OPERAND (t, 1)),
16947 complain|decltype_flag));
16949 case ARRAY_NOTATION_REF:
16951 tree start_index, length, stride;
16952 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
16953 args, complain, in_decl);
16954 start_index = RECUR (ARRAY_NOTATION_START (t));
16955 length = RECUR (ARRAY_NOTATION_LENGTH (t));
16956 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
16957 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
16958 length, stride, TREE_TYPE (op1)));
16960 case SIZEOF_EXPR:
16961 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
16962 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
16963 RETURN (tsubst_copy (t, args, complain, in_decl));
16964 /* Fall through */
16966 case ALIGNOF_EXPR:
16968 tree r;
16970 op1 = TREE_OPERAND (t, 0);
16971 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
16972 op1 = TREE_TYPE (op1);
16973 if (!args)
16975 /* When there are no ARGS, we are trying to evaluate a
16976 non-dependent expression from the parser. Trying to do
16977 the substitutions may not work. */
16978 if (!TYPE_P (op1))
16979 op1 = TREE_TYPE (op1);
16981 else
16983 ++cp_unevaluated_operand;
16984 ++c_inhibit_evaluation_warnings;
16985 if (TYPE_P (op1))
16986 op1 = tsubst (op1, args, complain, in_decl);
16987 else
16988 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
16989 /*function_p=*/false,
16990 /*integral_constant_expression_p=*/
16991 false);
16992 --cp_unevaluated_operand;
16993 --c_inhibit_evaluation_warnings;
16995 if (TYPE_P (op1))
16996 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
16997 complain & tf_error);
16998 else
16999 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
17000 complain & tf_error);
17001 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
17003 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
17005 if (!processing_template_decl && TYPE_P (op1))
17007 r = build_min (SIZEOF_EXPR, size_type_node,
17008 build1 (NOP_EXPR, op1, error_mark_node));
17009 SIZEOF_EXPR_TYPE_P (r) = 1;
17011 else
17012 r = build_min (SIZEOF_EXPR, size_type_node, op1);
17013 TREE_SIDE_EFFECTS (r) = 0;
17014 TREE_READONLY (r) = 1;
17016 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
17018 RETURN (r);
17021 case AT_ENCODE_EXPR:
17023 op1 = TREE_OPERAND (t, 0);
17024 ++cp_unevaluated_operand;
17025 ++c_inhibit_evaluation_warnings;
17026 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17027 /*function_p=*/false,
17028 /*integral_constant_expression_p=*/false);
17029 --cp_unevaluated_operand;
17030 --c_inhibit_evaluation_warnings;
17031 RETURN (objc_build_encode_expr (op1));
17034 case NOEXCEPT_EXPR:
17035 op1 = TREE_OPERAND (t, 0);
17036 ++cp_unevaluated_operand;
17037 ++c_inhibit_evaluation_warnings;
17038 ++cp_noexcept_operand;
17039 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17040 /*function_p=*/false,
17041 /*integral_constant_expression_p=*/false);
17042 --cp_unevaluated_operand;
17043 --c_inhibit_evaluation_warnings;
17044 --cp_noexcept_operand;
17045 RETURN (finish_noexcept_expr (op1, complain));
17047 case MODOP_EXPR:
17049 warning_sentinel s(warn_div_by_zero);
17050 tree lhs = RECUR (TREE_OPERAND (t, 0));
17051 tree rhs = RECUR (TREE_OPERAND (t, 2));
17052 tree r = build_x_modify_expr
17053 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
17054 complain|decltype_flag);
17055 /* TREE_NO_WARNING must be set if either the expression was
17056 parenthesized or it uses an operator such as >>= rather
17057 than plain assignment. In the former case, it was already
17058 set and must be copied. In the latter case,
17059 build_x_modify_expr sets it and it must not be reset
17060 here. */
17061 if (TREE_NO_WARNING (t))
17062 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17064 RETURN (r);
17067 case ARROW_EXPR:
17068 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17069 args, complain, in_decl);
17070 /* Remember that there was a reference to this entity. */
17071 if (DECL_P (op1)
17072 && !mark_used (op1, complain) && !(complain & tf_error))
17073 RETURN (error_mark_node);
17074 RETURN (build_x_arrow (input_location, op1, complain));
17076 case NEW_EXPR:
17078 tree placement = RECUR (TREE_OPERAND (t, 0));
17079 tree init = RECUR (TREE_OPERAND (t, 3));
17080 vec<tree, va_gc> *placement_vec;
17081 vec<tree, va_gc> *init_vec;
17082 tree ret;
17084 if (placement == NULL_TREE)
17085 placement_vec = NULL;
17086 else
17088 placement_vec = make_tree_vector ();
17089 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
17090 vec_safe_push (placement_vec, TREE_VALUE (placement));
17093 /* If there was an initializer in the original tree, but it
17094 instantiated to an empty list, then we should pass a
17095 non-NULL empty vector to tell build_new that it was an
17096 empty initializer() rather than no initializer. This can
17097 only happen when the initializer is a pack expansion whose
17098 parameter packs are of length zero. */
17099 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
17100 init_vec = NULL;
17101 else
17103 init_vec = make_tree_vector ();
17104 if (init == void_node)
17105 gcc_assert (init_vec != NULL);
17106 else
17108 for (; init != NULL_TREE; init = TREE_CHAIN (init))
17109 vec_safe_push (init_vec, TREE_VALUE (init));
17113 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
17114 tree op2 = RECUR (TREE_OPERAND (t, 2));
17115 ret = build_new (&placement_vec, op1, op2, &init_vec,
17116 NEW_EXPR_USE_GLOBAL (t),
17117 complain);
17119 if (placement_vec != NULL)
17120 release_tree_vector (placement_vec);
17121 if (init_vec != NULL)
17122 release_tree_vector (init_vec);
17124 RETURN (ret);
17127 case DELETE_EXPR:
17129 tree op0 = RECUR (TREE_OPERAND (t, 0));
17130 tree op1 = RECUR (TREE_OPERAND (t, 1));
17131 RETURN (delete_sanity (op0, op1,
17132 DELETE_EXPR_USE_VEC (t),
17133 DELETE_EXPR_USE_GLOBAL (t),
17134 complain));
17137 case COMPOUND_EXPR:
17139 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
17140 complain & ~tf_decltype, in_decl,
17141 /*function_p=*/false,
17142 integral_constant_expression_p);
17143 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
17144 op0,
17145 RECUR (TREE_OPERAND (t, 1)),
17146 complain|decltype_flag));
17149 case CALL_EXPR:
17151 tree function;
17152 vec<tree, va_gc> *call_args;
17153 unsigned int nargs, i;
17154 bool qualified_p;
17155 bool koenig_p;
17156 tree ret;
17158 function = CALL_EXPR_FN (t);
17159 /* Internal function with no arguments. */
17160 if (function == NULL_TREE && call_expr_nargs (t) == 0)
17161 RETURN (t);
17163 /* When we parsed the expression, we determined whether or
17164 not Koenig lookup should be performed. */
17165 koenig_p = KOENIG_LOOKUP_P (t);
17166 if (function == NULL_TREE)
17168 koenig_p = false;
17169 qualified_p = false;
17171 else if (TREE_CODE (function) == SCOPE_REF)
17173 qualified_p = true;
17174 function = tsubst_qualified_id (function, args, complain, in_decl,
17175 /*done=*/false,
17176 /*address_p=*/false);
17178 else if (koenig_p && identifier_p (function))
17180 /* Do nothing; calling tsubst_copy_and_build on an identifier
17181 would incorrectly perform unqualified lookup again.
17183 Note that we can also have an IDENTIFIER_NODE if the earlier
17184 unqualified lookup found a member function; in that case
17185 koenig_p will be false and we do want to do the lookup
17186 again to find the instantiated member function.
17188 FIXME but doing that causes c++/15272, so we need to stop
17189 using IDENTIFIER_NODE in that situation. */
17190 qualified_p = false;
17192 else
17194 if (TREE_CODE (function) == COMPONENT_REF)
17196 tree op = TREE_OPERAND (function, 1);
17198 qualified_p = (TREE_CODE (op) == SCOPE_REF
17199 || (BASELINK_P (op)
17200 && BASELINK_QUALIFIED_P (op)));
17202 else
17203 qualified_p = false;
17205 if (TREE_CODE (function) == ADDR_EXPR
17206 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
17207 /* Avoid error about taking the address of a constructor. */
17208 function = TREE_OPERAND (function, 0);
17210 function = tsubst_copy_and_build (function, args, complain,
17211 in_decl,
17212 !qualified_p,
17213 integral_constant_expression_p);
17215 if (BASELINK_P (function))
17216 qualified_p = true;
17219 nargs = call_expr_nargs (t);
17220 call_args = make_tree_vector ();
17221 for (i = 0; i < nargs; ++i)
17223 tree arg = CALL_EXPR_ARG (t, i);
17225 if (!PACK_EXPANSION_P (arg))
17226 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
17227 else
17229 /* Expand the pack expansion and push each entry onto
17230 CALL_ARGS. */
17231 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
17232 if (TREE_CODE (arg) == TREE_VEC)
17234 unsigned int len, j;
17236 len = TREE_VEC_LENGTH (arg);
17237 for (j = 0; j < len; ++j)
17239 tree value = TREE_VEC_ELT (arg, j);
17240 if (value != NULL_TREE)
17241 value = convert_from_reference (value);
17242 vec_safe_push (call_args, value);
17245 else
17247 /* A partial substitution. Add one entry. */
17248 vec_safe_push (call_args, arg);
17253 /* We do not perform argument-dependent lookup if normal
17254 lookup finds a non-function, in accordance with the
17255 expected resolution of DR 218. */
17256 if (koenig_p
17257 && ((is_overloaded_fn (function)
17258 /* If lookup found a member function, the Koenig lookup is
17259 not appropriate, even if an unqualified-name was used
17260 to denote the function. */
17261 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
17262 || identifier_p (function))
17263 /* Only do this when substitution turns a dependent call
17264 into a non-dependent call. */
17265 && type_dependent_expression_p_push (t)
17266 && !any_type_dependent_arguments_p (call_args))
17267 function = perform_koenig_lookup (function, call_args, tf_none);
17269 if (function != NULL_TREE
17270 && identifier_p (function)
17271 && !any_type_dependent_arguments_p (call_args))
17273 if (koenig_p && (complain & tf_warning_or_error))
17275 /* For backwards compatibility and good diagnostics, try
17276 the unqualified lookup again if we aren't in SFINAE
17277 context. */
17278 tree unq = (tsubst_copy_and_build
17279 (function, args, complain, in_decl, true,
17280 integral_constant_expression_p));
17281 if (unq == error_mark_node)
17283 release_tree_vector (call_args);
17284 RETURN (error_mark_node);
17287 if (unq != function)
17289 /* In a lambda fn, we have to be careful to not
17290 introduce new this captures. Legacy code can't
17291 be using lambdas anyway, so it's ok to be
17292 stricter. */
17293 bool in_lambda = (current_class_type
17294 && LAMBDA_TYPE_P (current_class_type));
17295 char const *const msg
17296 = G_("%qD was not declared in this scope, "
17297 "and no declarations were found by "
17298 "argument-dependent lookup at the point "
17299 "of instantiation");
17301 bool diag = true;
17302 if (in_lambda)
17303 error_at (EXPR_LOC_OR_LOC (t, input_location),
17304 msg, function);
17305 else
17306 diag = permerror (EXPR_LOC_OR_LOC (t, input_location),
17307 msg, function);
17308 if (diag)
17310 tree fn = unq;
17312 if (INDIRECT_REF_P (fn))
17313 fn = TREE_OPERAND (fn, 0);
17314 if (is_overloaded_fn (fn))
17315 fn = get_first_fn (fn);
17317 if (!DECL_P (fn))
17318 /* Can't say anything more. */;
17319 else if (DECL_CLASS_SCOPE_P (fn))
17321 location_t loc = EXPR_LOC_OR_LOC (t,
17322 input_location);
17323 inform (loc,
17324 "declarations in dependent base %qT are "
17325 "not found by unqualified lookup",
17326 DECL_CLASS_CONTEXT (fn));
17327 if (current_class_ptr)
17328 inform (loc,
17329 "use %<this->%D%> instead", function);
17330 else
17331 inform (loc,
17332 "use %<%T::%D%> instead",
17333 current_class_name, function);
17335 else
17336 inform (DECL_SOURCE_LOCATION (fn),
17337 "%qD declared here, later in the "
17338 "translation unit", fn);
17339 if (in_lambda)
17341 release_tree_vector (call_args);
17342 RETURN (error_mark_node);
17346 function = unq;
17349 if (identifier_p (function))
17351 if (complain & tf_error)
17352 unqualified_name_lookup_error (function);
17353 release_tree_vector (call_args);
17354 RETURN (error_mark_node);
17358 /* Remember that there was a reference to this entity. */
17359 if (function != NULL_TREE
17360 && DECL_P (function)
17361 && !mark_used (function, complain) && !(complain & tf_error))
17363 release_tree_vector (call_args);
17364 RETURN (error_mark_node);
17367 /* Put back tf_decltype for the actual call. */
17368 complain |= decltype_flag;
17370 if (function == NULL_TREE)
17371 switch (CALL_EXPR_IFN (t))
17373 case IFN_LAUNDER:
17374 gcc_assert (nargs == 1);
17375 if (vec_safe_length (call_args) != 1)
17377 error_at (EXPR_LOC_OR_LOC (t, input_location),
17378 "wrong number of arguments to "
17379 "%<__builtin_launder%>");
17380 ret = error_mark_node;
17382 else
17383 ret = finish_builtin_launder (EXPR_LOC_OR_LOC (t,
17384 input_location),
17385 (*call_args)[0], complain);
17386 break;
17388 default:
17389 /* Unsupported internal function with arguments. */
17390 gcc_unreachable ();
17392 else if (TREE_CODE (function) == OFFSET_REF)
17393 ret = build_offset_ref_call_from_tree (function, &call_args,
17394 complain);
17395 else if (TREE_CODE (function) == COMPONENT_REF)
17397 tree instance = TREE_OPERAND (function, 0);
17398 tree fn = TREE_OPERAND (function, 1);
17400 if (processing_template_decl
17401 && (type_dependent_expression_p (instance)
17402 || (!BASELINK_P (fn)
17403 && TREE_CODE (fn) != FIELD_DECL)
17404 || type_dependent_expression_p (fn)
17405 || any_type_dependent_arguments_p (call_args)))
17406 ret = build_min_nt_call_vec (function, call_args);
17407 else if (!BASELINK_P (fn))
17408 ret = finish_call_expr (function, &call_args,
17409 /*disallow_virtual=*/false,
17410 /*koenig_p=*/false,
17411 complain);
17412 else
17413 ret = (build_new_method_call
17414 (instance, fn,
17415 &call_args, NULL_TREE,
17416 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
17417 /*fn_p=*/NULL,
17418 complain));
17420 else
17421 ret = finish_call_expr (function, &call_args,
17422 /*disallow_virtual=*/qualified_p,
17423 koenig_p,
17424 complain);
17426 release_tree_vector (call_args);
17428 if (ret != error_mark_node)
17430 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
17431 bool ord = CALL_EXPR_ORDERED_ARGS (t);
17432 bool rev = CALL_EXPR_REVERSE_ARGS (t);
17433 bool thk = CALL_FROM_THUNK_P (t);
17434 if (op || ord || rev || thk)
17436 function = extract_call_expr (ret);
17437 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
17438 CALL_EXPR_ORDERED_ARGS (function) = ord;
17439 CALL_EXPR_REVERSE_ARGS (function) = rev;
17440 if (thk)
17442 CALL_FROM_THUNK_P (function) = true;
17443 /* The thunk location is not interesting. */
17444 SET_EXPR_LOCATION (function, UNKNOWN_LOCATION);
17449 RETURN (ret);
17452 case COND_EXPR:
17454 tree cond = RECUR (TREE_OPERAND (t, 0));
17455 tree folded_cond = fold_non_dependent_expr (cond);
17456 tree exp1, exp2;
17458 if (TREE_CODE (folded_cond) == INTEGER_CST)
17460 if (integer_zerop (folded_cond))
17462 ++c_inhibit_evaluation_warnings;
17463 exp1 = RECUR (TREE_OPERAND (t, 1));
17464 --c_inhibit_evaluation_warnings;
17465 exp2 = RECUR (TREE_OPERAND (t, 2));
17467 else
17469 exp1 = RECUR (TREE_OPERAND (t, 1));
17470 ++c_inhibit_evaluation_warnings;
17471 exp2 = RECUR (TREE_OPERAND (t, 2));
17472 --c_inhibit_evaluation_warnings;
17474 cond = folded_cond;
17476 else
17478 exp1 = RECUR (TREE_OPERAND (t, 1));
17479 exp2 = RECUR (TREE_OPERAND (t, 2));
17482 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
17483 cond, exp1, exp2, complain));
17486 case PSEUDO_DTOR_EXPR:
17488 tree op0 = RECUR (TREE_OPERAND (t, 0));
17489 tree op1 = RECUR (TREE_OPERAND (t, 1));
17490 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
17491 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
17492 input_location));
17495 case TREE_LIST:
17497 tree purpose, value, chain;
17499 if (t == void_list_node)
17500 RETURN (t);
17502 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
17503 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
17505 /* We have pack expansions, so expand those and
17506 create a new list out of it. */
17507 tree purposevec = NULL_TREE;
17508 tree valuevec = NULL_TREE;
17509 tree chain;
17510 int i, len = -1;
17512 /* Expand the argument expressions. */
17513 if (TREE_PURPOSE (t))
17514 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
17515 complain, in_decl);
17516 if (TREE_VALUE (t))
17517 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
17518 complain, in_decl);
17520 /* Build the rest of the list. */
17521 chain = TREE_CHAIN (t);
17522 if (chain && chain != void_type_node)
17523 chain = RECUR (chain);
17525 /* Determine the number of arguments. */
17526 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
17528 len = TREE_VEC_LENGTH (purposevec);
17529 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
17531 else if (TREE_CODE (valuevec) == TREE_VEC)
17532 len = TREE_VEC_LENGTH (valuevec);
17533 else
17535 /* Since we only performed a partial substitution into
17536 the argument pack, we only RETURN (a single list
17537 node. */
17538 if (purposevec == TREE_PURPOSE (t)
17539 && valuevec == TREE_VALUE (t)
17540 && chain == TREE_CHAIN (t))
17541 RETURN (t);
17543 RETURN (tree_cons (purposevec, valuevec, chain));
17546 /* Convert the argument vectors into a TREE_LIST */
17547 i = len;
17548 while (i > 0)
17550 /* Grab the Ith values. */
17551 i--;
17552 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
17553 : NULL_TREE;
17554 value
17555 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
17556 : NULL_TREE;
17558 /* Build the list (backwards). */
17559 chain = tree_cons (purpose, value, chain);
17562 RETURN (chain);
17565 purpose = TREE_PURPOSE (t);
17566 if (purpose)
17567 purpose = RECUR (purpose);
17568 value = TREE_VALUE (t);
17569 if (value)
17570 value = RECUR (value);
17571 chain = TREE_CHAIN (t);
17572 if (chain && chain != void_type_node)
17573 chain = RECUR (chain);
17574 if (purpose == TREE_PURPOSE (t)
17575 && value == TREE_VALUE (t)
17576 && chain == TREE_CHAIN (t))
17577 RETURN (t);
17578 RETURN (tree_cons (purpose, value, chain));
17581 case COMPONENT_REF:
17583 tree object;
17584 tree object_type;
17585 tree member;
17586 tree r;
17588 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17589 args, complain, in_decl);
17590 /* Remember that there was a reference to this entity. */
17591 if (DECL_P (object)
17592 && !mark_used (object, complain) && !(complain & tf_error))
17593 RETURN (error_mark_node);
17594 object_type = TREE_TYPE (object);
17596 member = TREE_OPERAND (t, 1);
17597 if (BASELINK_P (member))
17598 member = tsubst_baselink (member,
17599 non_reference (TREE_TYPE (object)),
17600 args, complain, in_decl);
17601 else
17602 member = tsubst_copy (member, args, complain, in_decl);
17603 if (member == error_mark_node)
17604 RETURN (error_mark_node);
17606 if (TREE_CODE (member) == FIELD_DECL)
17608 r = finish_non_static_data_member (member, object, NULL_TREE);
17609 if (TREE_CODE (r) == COMPONENT_REF)
17610 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17611 RETURN (r);
17613 else if (type_dependent_expression_p (object))
17614 /* We can't do much here. */;
17615 else if (!CLASS_TYPE_P (object_type))
17617 if (scalarish_type_p (object_type))
17619 tree s = NULL_TREE;
17620 tree dtor = member;
17622 if (TREE_CODE (dtor) == SCOPE_REF)
17624 s = TREE_OPERAND (dtor, 0);
17625 dtor = TREE_OPERAND (dtor, 1);
17627 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
17629 dtor = TREE_OPERAND (dtor, 0);
17630 if (TYPE_P (dtor))
17631 RETURN (finish_pseudo_destructor_expr
17632 (object, s, dtor, input_location));
17636 else if (TREE_CODE (member) == SCOPE_REF
17637 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
17639 /* Lookup the template functions now that we know what the
17640 scope is. */
17641 tree scope = TREE_OPERAND (member, 0);
17642 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
17643 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
17644 member = lookup_qualified_name (scope, tmpl,
17645 /*is_type_p=*/false,
17646 /*complain=*/false);
17647 if (BASELINK_P (member))
17649 BASELINK_FUNCTIONS (member)
17650 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
17651 args);
17652 member = (adjust_result_of_qualified_name_lookup
17653 (member, BINFO_TYPE (BASELINK_BINFO (member)),
17654 object_type));
17656 else
17658 qualified_name_lookup_error (scope, tmpl, member,
17659 input_location);
17660 RETURN (error_mark_node);
17663 else if (TREE_CODE (member) == SCOPE_REF
17664 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
17665 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
17667 if (complain & tf_error)
17669 if (TYPE_P (TREE_OPERAND (member, 0)))
17670 error ("%qT is not a class or namespace",
17671 TREE_OPERAND (member, 0));
17672 else
17673 error ("%qD is not a class or namespace",
17674 TREE_OPERAND (member, 0));
17676 RETURN (error_mark_node);
17679 r = finish_class_member_access_expr (object, member,
17680 /*template_p=*/false,
17681 complain);
17682 if (TREE_CODE (r) == COMPONENT_REF)
17683 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17684 RETURN (r);
17687 case THROW_EXPR:
17688 RETURN (build_throw
17689 (RECUR (TREE_OPERAND (t, 0))));
17691 case CONSTRUCTOR:
17693 vec<constructor_elt, va_gc> *n;
17694 constructor_elt *ce;
17695 unsigned HOST_WIDE_INT idx;
17696 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17697 bool process_index_p;
17698 int newlen;
17699 bool need_copy_p = false;
17700 tree r;
17702 if (type == error_mark_node)
17703 RETURN (error_mark_node);
17705 /* digest_init will do the wrong thing if we let it. */
17706 if (type && TYPE_PTRMEMFUNC_P (type))
17707 RETURN (t);
17709 /* We do not want to process the index of aggregate
17710 initializers as they are identifier nodes which will be
17711 looked up by digest_init. */
17712 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
17714 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
17715 newlen = vec_safe_length (n);
17716 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
17718 if (ce->index && process_index_p
17719 /* An identifier index is looked up in the type
17720 being initialized, not the current scope. */
17721 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
17722 ce->index = RECUR (ce->index);
17724 if (PACK_EXPANSION_P (ce->value))
17726 /* Substitute into the pack expansion. */
17727 ce->value = tsubst_pack_expansion (ce->value, args, complain,
17728 in_decl);
17730 if (ce->value == error_mark_node
17731 || PACK_EXPANSION_P (ce->value))
17733 else if (TREE_VEC_LENGTH (ce->value) == 1)
17734 /* Just move the argument into place. */
17735 ce->value = TREE_VEC_ELT (ce->value, 0);
17736 else
17738 /* Update the length of the final CONSTRUCTOR
17739 arguments vector, and note that we will need to
17740 copy.*/
17741 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
17742 need_copy_p = true;
17745 else
17746 ce->value = RECUR (ce->value);
17749 if (need_copy_p)
17751 vec<constructor_elt, va_gc> *old_n = n;
17753 vec_alloc (n, newlen);
17754 FOR_EACH_VEC_ELT (*old_n, idx, ce)
17756 if (TREE_CODE (ce->value) == TREE_VEC)
17758 int i, len = TREE_VEC_LENGTH (ce->value);
17759 for (i = 0; i < len; ++i)
17760 CONSTRUCTOR_APPEND_ELT (n, 0,
17761 TREE_VEC_ELT (ce->value, i));
17763 else
17764 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
17768 r = build_constructor (init_list_type_node, n);
17769 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
17771 if (TREE_HAS_CONSTRUCTOR (t))
17773 fcl_t cl = fcl_functional;
17774 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
17775 cl = fcl_c99;
17776 RETURN (finish_compound_literal (type, r, complain, cl));
17779 TREE_TYPE (r) = type;
17780 RETURN (r);
17783 case TYPEID_EXPR:
17785 tree operand_0 = TREE_OPERAND (t, 0);
17786 if (TYPE_P (operand_0))
17788 operand_0 = tsubst (operand_0, args, complain, in_decl);
17789 RETURN (get_typeid (operand_0, complain));
17791 else
17793 operand_0 = RECUR (operand_0);
17794 RETURN (build_typeid (operand_0, complain));
17798 case VAR_DECL:
17799 if (!args)
17800 RETURN (t);
17801 else if (DECL_PACK_P (t))
17803 /* We don't build decls for an instantiation of a
17804 variadic capture proxy, we instantiate the elements
17805 when needed. */
17806 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
17807 return RECUR (DECL_VALUE_EXPR (t));
17809 /* Fall through */
17811 case PARM_DECL:
17813 tree r = tsubst_copy (t, args, complain, in_decl);
17814 /* ??? We're doing a subset of finish_id_expression here. */
17815 if (VAR_P (r)
17816 && !processing_template_decl
17817 && !cp_unevaluated_operand
17818 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
17819 && CP_DECL_THREAD_LOCAL_P (r))
17821 if (tree wrap = get_tls_wrapper_fn (r))
17822 /* Replace an evaluated use of the thread_local variable with
17823 a call to its wrapper. */
17824 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
17826 else if (outer_automatic_var_p (r))
17828 r = process_outer_var_ref (r, complain);
17829 if (is_capture_proxy (r))
17830 register_local_specialization (r, t);
17833 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
17834 /* If the original type was a reference, we'll be wrapped in
17835 the appropriate INDIRECT_REF. */
17836 r = convert_from_reference (r);
17837 RETURN (r);
17840 case VA_ARG_EXPR:
17842 tree op0 = RECUR (TREE_OPERAND (t, 0));
17843 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17844 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
17847 case OFFSETOF_EXPR:
17849 tree object_ptr
17850 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
17851 in_decl, /*function_p=*/false,
17852 /*integral_constant_expression_p=*/false);
17853 RETURN (finish_offsetof (object_ptr,
17854 RECUR (TREE_OPERAND (t, 0)),
17855 EXPR_LOCATION (t)));
17858 case ADDRESSOF_EXPR:
17859 RETURN (cp_build_addressof (EXPR_LOCATION (t),
17860 RECUR (TREE_OPERAND (t, 0)), complain));
17862 case TRAIT_EXPR:
17864 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
17865 complain, in_decl);
17867 tree type2 = TRAIT_EXPR_TYPE2 (t);
17868 if (type2 && TREE_CODE (type2) == TREE_LIST)
17869 type2 = RECUR (type2);
17870 else if (type2)
17871 type2 = tsubst (type2, args, complain, in_decl);
17873 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
17876 case STMT_EXPR:
17878 tree old_stmt_expr = cur_stmt_expr;
17879 tree stmt_expr = begin_stmt_expr ();
17881 cur_stmt_expr = stmt_expr;
17882 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
17883 integral_constant_expression_p);
17884 stmt_expr = finish_stmt_expr (stmt_expr, false);
17885 cur_stmt_expr = old_stmt_expr;
17887 /* If the resulting list of expression statement is empty,
17888 fold it further into void_node. */
17889 if (empty_expr_stmt_p (stmt_expr))
17890 stmt_expr = void_node;
17892 RETURN (stmt_expr);
17895 case LAMBDA_EXPR:
17897 tree r = build_lambda_expr ();
17899 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
17900 LAMBDA_EXPR_CLOSURE (r) = type;
17901 CLASSTYPE_LAMBDA_EXPR (type) = r;
17903 LAMBDA_EXPR_LOCATION (r)
17904 = LAMBDA_EXPR_LOCATION (t);
17905 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
17906 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
17907 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
17908 LAMBDA_EXPR_DISCRIMINATOR (r)
17909 = (LAMBDA_EXPR_DISCRIMINATOR (t));
17910 tree scope = LAMBDA_EXPR_EXTRA_SCOPE (t);
17911 if (!scope)
17912 /* No substitution needed. */;
17913 else if (VAR_OR_FUNCTION_DECL_P (scope))
17914 /* For a function or variable scope, we want to use tsubst so that we
17915 don't complain about referring to an auto before deduction. */
17916 scope = tsubst (scope, args, complain, in_decl);
17917 else if (TREE_CODE (scope) == PARM_DECL)
17919 /* Look up the parameter we want directly, as tsubst_copy
17920 doesn't do what we need. */
17921 tree fn = tsubst (DECL_CONTEXT (scope), args, complain, in_decl);
17922 tree parm = FUNCTION_FIRST_USER_PARM (fn);
17923 while (DECL_PARM_INDEX (parm) != DECL_PARM_INDEX (scope))
17924 parm = DECL_CHAIN (parm);
17925 scope = parm;
17926 /* FIXME Work around the parm not having DECL_CONTEXT set. */
17927 if (DECL_CONTEXT (scope) == NULL_TREE)
17928 DECL_CONTEXT (scope) = fn;
17930 else if (TREE_CODE (scope) == FIELD_DECL)
17931 /* For a field, use tsubst_copy so that we look up the existing field
17932 rather than build a new one. */
17933 scope = RECUR (scope);
17934 else
17935 gcc_unreachable ();
17936 LAMBDA_EXPR_EXTRA_SCOPE (r) = scope;
17938 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
17939 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
17941 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
17942 determine_visibility (TYPE_NAME (type));
17943 /* Now that we know visibility, instantiate the type so we have a
17944 declaration of the op() for later calls to lambda_function. */
17945 complete_type (type);
17947 if (tree fn = lambda_function (type))
17948 LAMBDA_EXPR_RETURN_TYPE (r) = TREE_TYPE (TREE_TYPE (fn));
17950 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
17952 insert_pending_capture_proxies ();
17954 RETURN (build_lambda_object (r));
17957 case TARGET_EXPR:
17958 /* We can get here for a constant initializer of non-dependent type.
17959 FIXME stop folding in cp_parser_initializer_clause. */
17961 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
17962 complain);
17963 RETURN (r);
17966 case TRANSACTION_EXPR:
17967 RETURN (tsubst_expr(t, args, complain, in_decl,
17968 integral_constant_expression_p));
17970 case PAREN_EXPR:
17971 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
17973 case VEC_PERM_EXPR:
17975 tree op0 = RECUR (TREE_OPERAND (t, 0));
17976 tree op1 = RECUR (TREE_OPERAND (t, 1));
17977 tree op2 = RECUR (TREE_OPERAND (t, 2));
17978 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
17979 complain));
17982 case REQUIRES_EXPR:
17983 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
17985 default:
17986 /* Handle Objective-C++ constructs, if appropriate. */
17988 tree subst
17989 = objcp_tsubst_copy_and_build (t, args, complain,
17990 in_decl, /*function_p=*/false);
17991 if (subst)
17992 RETURN (subst);
17994 RETURN (tsubst_copy (t, args, complain, in_decl));
17997 #undef RECUR
17998 #undef RETURN
17999 out:
18000 input_location = loc;
18001 return retval;
18004 /* Verify that the instantiated ARGS are valid. For type arguments,
18005 make sure that the type's linkage is ok. For non-type arguments,
18006 make sure they are constants if they are integral or enumerations.
18007 Emit an error under control of COMPLAIN, and return TRUE on error. */
18009 static bool
18010 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
18012 if (dependent_template_arg_p (t))
18013 return false;
18014 if (ARGUMENT_PACK_P (t))
18016 tree vec = ARGUMENT_PACK_ARGS (t);
18017 int len = TREE_VEC_LENGTH (vec);
18018 bool result = false;
18019 int i;
18021 for (i = 0; i < len; ++i)
18022 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
18023 result = true;
18024 return result;
18026 else if (TYPE_P (t))
18028 /* [basic.link]: A name with no linkage (notably, the name
18029 of a class or enumeration declared in a local scope)
18030 shall not be used to declare an entity with linkage.
18031 This implies that names with no linkage cannot be used as
18032 template arguments
18034 DR 757 relaxes this restriction for C++0x. */
18035 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
18036 : no_linkage_check (t, /*relaxed_p=*/false));
18038 if (nt)
18040 /* DR 488 makes use of a type with no linkage cause
18041 type deduction to fail. */
18042 if (complain & tf_error)
18044 if (TYPE_UNNAMED_P (nt))
18045 error ("%qT is/uses unnamed type", t);
18046 else
18047 error ("template argument for %qD uses local type %qT",
18048 tmpl, t);
18050 return true;
18052 /* In order to avoid all sorts of complications, we do not
18053 allow variably-modified types as template arguments. */
18054 else if (variably_modified_type_p (t, NULL_TREE))
18056 if (complain & tf_error)
18057 error ("%qT is a variably modified type", t);
18058 return true;
18061 /* Class template and alias template arguments should be OK. */
18062 else if (DECL_TYPE_TEMPLATE_P (t))
18064 /* A non-type argument of integral or enumerated type must be a
18065 constant. */
18066 else if (TREE_TYPE (t)
18067 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
18068 && !REFERENCE_REF_P (t)
18069 && !TREE_CONSTANT (t))
18071 if (complain & tf_error)
18072 error ("integral expression %qE is not constant", t);
18073 return true;
18075 return false;
18078 static bool
18079 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
18081 int ix, len = DECL_NTPARMS (tmpl);
18082 bool result = false;
18084 for (ix = 0; ix != len; ix++)
18086 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
18087 result = true;
18089 if (result && (complain & tf_error))
18090 error (" trying to instantiate %qD", tmpl);
18091 return result;
18094 /* We're out of SFINAE context now, so generate diagnostics for the access
18095 errors we saw earlier when instantiating D from TMPL and ARGS. */
18097 static void
18098 recheck_decl_substitution (tree d, tree tmpl, tree args)
18100 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
18101 tree type = TREE_TYPE (pattern);
18102 location_t loc = input_location;
18104 push_access_scope (d);
18105 push_deferring_access_checks (dk_no_deferred);
18106 input_location = DECL_SOURCE_LOCATION (pattern);
18107 tsubst (type, args, tf_warning_or_error, d);
18108 input_location = loc;
18109 pop_deferring_access_checks ();
18110 pop_access_scope (d);
18113 /* Instantiate the indicated variable, function, or alias template TMPL with
18114 the template arguments in TARG_PTR. */
18116 static tree
18117 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
18119 tree targ_ptr = orig_args;
18120 tree fndecl;
18121 tree gen_tmpl;
18122 tree spec;
18123 bool access_ok = true;
18125 if (tmpl == error_mark_node)
18126 return error_mark_node;
18128 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
18130 /* If this function is a clone, handle it specially. */
18131 if (DECL_CLONED_FUNCTION_P (tmpl))
18133 tree spec;
18134 tree clone;
18136 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
18137 DECL_CLONED_FUNCTION. */
18138 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
18139 targ_ptr, complain);
18140 if (spec == error_mark_node)
18141 return error_mark_node;
18143 /* Look for the clone. */
18144 FOR_EACH_CLONE (clone, spec)
18145 if (DECL_NAME (clone) == DECL_NAME (tmpl))
18146 return clone;
18147 /* We should always have found the clone by now. */
18148 gcc_unreachable ();
18149 return NULL_TREE;
18152 if (targ_ptr == error_mark_node)
18153 return error_mark_node;
18155 /* Check to see if we already have this specialization. */
18156 gen_tmpl = most_general_template (tmpl);
18157 if (TMPL_ARGS_DEPTH (targ_ptr)
18158 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
18159 /* targ_ptr only has the innermost template args, so add the outer ones
18160 from tmpl, which could be either a partial instantiation or gen_tmpl (in
18161 the case of a non-dependent call within a template definition). */
18162 targ_ptr = (add_outermost_template_args
18163 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
18164 targ_ptr));
18166 /* It would be nice to avoid hashing here and then again in tsubst_decl,
18167 but it doesn't seem to be on the hot path. */
18168 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
18170 gcc_assert (tmpl == gen_tmpl
18171 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
18172 == spec)
18173 || fndecl == NULL_TREE);
18175 if (spec != NULL_TREE)
18177 if (FNDECL_HAS_ACCESS_ERRORS (spec))
18179 if (complain & tf_error)
18180 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
18181 return error_mark_node;
18183 return spec;
18186 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
18187 complain))
18188 return error_mark_node;
18190 /* We are building a FUNCTION_DECL, during which the access of its
18191 parameters and return types have to be checked. However this
18192 FUNCTION_DECL which is the desired context for access checking
18193 is not built yet. We solve this chicken-and-egg problem by
18194 deferring all checks until we have the FUNCTION_DECL. */
18195 push_deferring_access_checks (dk_deferred);
18197 /* Instantiation of the function happens in the context of the function
18198 template, not the context of the overload resolution we're doing. */
18199 push_to_top_level ();
18200 /* If there are dependent arguments, e.g. because we're doing partial
18201 ordering, make sure processing_template_decl stays set. */
18202 if (uses_template_parms (targ_ptr))
18203 ++processing_template_decl;
18204 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18206 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
18207 complain, gen_tmpl, true);
18208 push_nested_class (ctx);
18211 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
18213 fndecl = NULL_TREE;
18214 if (VAR_P (pattern))
18216 /* We need to determine if we're using a partial or explicit
18217 specialization now, because the type of the variable could be
18218 different. */
18219 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
18220 tree elt = most_specialized_partial_spec (tid, complain);
18221 if (elt == error_mark_node)
18222 pattern = error_mark_node;
18223 else if (elt)
18225 tree partial_tmpl = TREE_VALUE (elt);
18226 tree partial_args = TREE_PURPOSE (elt);
18227 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
18228 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
18232 /* Substitute template parameters to obtain the specialization. */
18233 if (fndecl == NULL_TREE)
18234 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
18235 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18236 pop_nested_class ();
18237 pop_from_top_level ();
18239 if (fndecl == error_mark_node)
18241 pop_deferring_access_checks ();
18242 return error_mark_node;
18245 /* The DECL_TI_TEMPLATE should always be the immediate parent
18246 template, not the most general template. */
18247 DECL_TI_TEMPLATE (fndecl) = tmpl;
18248 DECL_TI_ARGS (fndecl) = targ_ptr;
18250 /* Now we know the specialization, compute access previously
18251 deferred. Do no access control for inheriting constructors,
18252 as we already checked access for the inherited constructor. */
18253 if (!(flag_new_inheriting_ctors
18254 && DECL_INHERITED_CTOR (fndecl)))
18256 push_access_scope (fndecl);
18257 if (!perform_deferred_access_checks (complain))
18258 access_ok = false;
18259 pop_access_scope (fndecl);
18261 pop_deferring_access_checks ();
18263 /* If we've just instantiated the main entry point for a function,
18264 instantiate all the alternate entry points as well. We do this
18265 by cloning the instantiation of the main entry point, not by
18266 instantiating the template clones. */
18267 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
18268 clone_function_decl (fndecl, /*update_methods=*/false);
18270 if (!access_ok)
18272 if (!(complain & tf_error))
18274 /* Remember to reinstantiate when we're out of SFINAE so the user
18275 can see the errors. */
18276 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
18278 return error_mark_node;
18280 return fndecl;
18283 /* Wrapper for instantiate_template_1. */
18285 tree
18286 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
18288 tree ret;
18289 timevar_push (TV_TEMPLATE_INST);
18290 ret = instantiate_template_1 (tmpl, orig_args, complain);
18291 timevar_pop (TV_TEMPLATE_INST);
18292 return ret;
18295 /* Instantiate the alias template TMPL with ARGS. Also push a template
18296 instantiation level, which instantiate_template doesn't do because
18297 functions and variables have sufficient context established by the
18298 callers. */
18300 static tree
18301 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
18303 struct pending_template *old_last_pend = last_pending_template;
18304 struct tinst_level *old_error_tinst = last_error_tinst_level;
18305 if (tmpl == error_mark_node || args == error_mark_node)
18306 return error_mark_node;
18307 tree tinst = build_tree_list (tmpl, args);
18308 if (!push_tinst_level (tinst))
18310 ggc_free (tinst);
18311 return error_mark_node;
18314 args =
18315 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
18316 args, tmpl, complain,
18317 /*require_all_args=*/true,
18318 /*use_default_args=*/true);
18320 tree r = instantiate_template (tmpl, args, complain);
18321 pop_tinst_level ();
18322 /* We can't free this if a pending_template entry or last_error_tinst_level
18323 is pointing at it. */
18324 if (last_pending_template == old_last_pend
18325 && last_error_tinst_level == old_error_tinst)
18326 ggc_free (tinst);
18328 return r;
18331 /* PARM is a template parameter pack for FN. Returns true iff
18332 PARM is used in a deducible way in the argument list of FN. */
18334 static bool
18335 pack_deducible_p (tree parm, tree fn)
18337 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
18338 for (; t; t = TREE_CHAIN (t))
18340 tree type = TREE_VALUE (t);
18341 tree packs;
18342 if (!PACK_EXPANSION_P (type))
18343 continue;
18344 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
18345 packs; packs = TREE_CHAIN (packs))
18346 if (template_args_equal (TREE_VALUE (packs), parm))
18348 /* The template parameter pack is used in a function parameter
18349 pack. If this is the end of the parameter list, the
18350 template parameter pack is deducible. */
18351 if (TREE_CHAIN (t) == void_list_node)
18352 return true;
18353 else
18354 /* Otherwise, not. Well, it could be deduced from
18355 a non-pack parameter, but doing so would end up with
18356 a deduction mismatch, so don't bother. */
18357 return false;
18360 /* The template parameter pack isn't used in any function parameter
18361 packs, but it might be used deeper, e.g. tuple<Args...>. */
18362 return true;
18365 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
18366 NARGS elements of the arguments that are being used when calling
18367 it. TARGS is a vector into which the deduced template arguments
18368 are placed.
18370 Returns either a FUNCTION_DECL for the matching specialization of FN or
18371 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
18372 true, diagnostics will be printed to explain why it failed.
18374 If FN is a conversion operator, or we are trying to produce a specific
18375 specialization, RETURN_TYPE is the return type desired.
18377 The EXPLICIT_TARGS are explicit template arguments provided via a
18378 template-id.
18380 The parameter STRICT is one of:
18382 DEDUCE_CALL:
18383 We are deducing arguments for a function call, as in
18384 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
18385 deducing arguments for a call to the result of a conversion
18386 function template, as in [over.call.object].
18388 DEDUCE_CONV:
18389 We are deducing arguments for a conversion function, as in
18390 [temp.deduct.conv].
18392 DEDUCE_EXACT:
18393 We are deducing arguments when doing an explicit instantiation
18394 as in [temp.explicit], when determining an explicit specialization
18395 as in [temp.expl.spec], or when taking the address of a function
18396 template, as in [temp.deduct.funcaddr]. */
18398 tree
18399 fn_type_unification (tree fn,
18400 tree explicit_targs,
18401 tree targs,
18402 const tree *args,
18403 unsigned int nargs,
18404 tree return_type,
18405 unification_kind_t strict,
18406 int flags,
18407 bool explain_p,
18408 bool decltype_p)
18410 tree parms;
18411 tree fntype;
18412 tree decl = NULL_TREE;
18413 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
18414 bool ok;
18415 static int deduction_depth;
18416 struct pending_template *old_last_pend = last_pending_template;
18417 struct tinst_level *old_error_tinst = last_error_tinst_level;
18419 tree orig_fn = fn;
18420 if (flag_new_inheriting_ctors)
18421 fn = strip_inheriting_ctors (fn);
18423 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
18424 tree tinst;
18425 tree r = error_mark_node;
18427 tree full_targs = targs;
18428 if (TMPL_ARGS_DEPTH (targs)
18429 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
18430 full_targs = (add_outermost_template_args
18431 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
18432 targs));
18434 if (decltype_p)
18435 complain |= tf_decltype;
18437 /* In C++0x, it's possible to have a function template whose type depends
18438 on itself recursively. This is most obvious with decltype, but can also
18439 occur with enumeration scope (c++/48969). So we need to catch infinite
18440 recursion and reject the substitution at deduction time; this function
18441 will return error_mark_node for any repeated substitution.
18443 This also catches excessive recursion such as when f<N> depends on
18444 f<N-1> across all integers, and returns error_mark_node for all the
18445 substitutions back up to the initial one.
18447 This is, of course, not reentrant. */
18448 if (excessive_deduction_depth)
18449 return error_mark_node;
18450 tinst = build_tree_list (fn, NULL_TREE);
18451 ++deduction_depth;
18453 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
18455 fntype = TREE_TYPE (fn);
18456 if (explicit_targs)
18458 /* [temp.deduct]
18460 The specified template arguments must match the template
18461 parameters in kind (i.e., type, nontype, template), and there
18462 must not be more arguments than there are parameters;
18463 otherwise type deduction fails.
18465 Nontype arguments must match the types of the corresponding
18466 nontype template parameters, or must be convertible to the
18467 types of the corresponding nontype parameters as specified in
18468 _temp.arg.nontype_, otherwise type deduction fails.
18470 All references in the function type of the function template
18471 to the corresponding template parameters are replaced by the
18472 specified template argument values. If a substitution in a
18473 template parameter or in the function type of the function
18474 template results in an invalid type, type deduction fails. */
18475 int i, len = TREE_VEC_LENGTH (tparms);
18476 location_t loc = input_location;
18477 bool incomplete = false;
18479 if (explicit_targs == error_mark_node)
18480 goto fail;
18482 if (TMPL_ARGS_DEPTH (explicit_targs)
18483 < TMPL_ARGS_DEPTH (full_targs))
18484 explicit_targs = add_outermost_template_args (full_targs,
18485 explicit_targs);
18487 /* Adjust any explicit template arguments before entering the
18488 substitution context. */
18489 explicit_targs
18490 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
18491 complain,
18492 /*require_all_args=*/false,
18493 /*use_default_args=*/false));
18494 if (explicit_targs == error_mark_node)
18495 goto fail;
18497 /* Substitute the explicit args into the function type. This is
18498 necessary so that, for instance, explicitly declared function
18499 arguments can match null pointed constants. If we were given
18500 an incomplete set of explicit args, we must not do semantic
18501 processing during substitution as we could create partial
18502 instantiations. */
18503 for (i = 0; i < len; i++)
18505 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
18506 bool parameter_pack = false;
18507 tree targ = TREE_VEC_ELT (explicit_targs, i);
18509 /* Dig out the actual parm. */
18510 if (TREE_CODE (parm) == TYPE_DECL
18511 || TREE_CODE (parm) == TEMPLATE_DECL)
18513 parm = TREE_TYPE (parm);
18514 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
18516 else if (TREE_CODE (parm) == PARM_DECL)
18518 parm = DECL_INITIAL (parm);
18519 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
18522 if (!parameter_pack && targ == NULL_TREE)
18523 /* No explicit argument for this template parameter. */
18524 incomplete = true;
18526 if (parameter_pack && pack_deducible_p (parm, fn))
18528 /* Mark the argument pack as "incomplete". We could
18529 still deduce more arguments during unification.
18530 We remove this mark in type_unification_real. */
18531 if (targ)
18533 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
18534 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
18535 = ARGUMENT_PACK_ARGS (targ);
18538 /* We have some incomplete argument packs. */
18539 incomplete = true;
18543 TREE_VALUE (tinst) = explicit_targs;
18544 if (!push_tinst_level (tinst))
18546 excessive_deduction_depth = true;
18547 goto fail;
18549 processing_template_decl += incomplete;
18550 input_location = DECL_SOURCE_LOCATION (fn);
18551 /* Ignore any access checks; we'll see them again in
18552 instantiate_template and they might have the wrong
18553 access path at this point. */
18554 push_deferring_access_checks (dk_deferred);
18555 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
18556 complain | tf_partial | tf_fndecl_type, NULL_TREE);
18557 pop_deferring_access_checks ();
18558 input_location = loc;
18559 processing_template_decl -= incomplete;
18560 pop_tinst_level ();
18562 if (fntype == error_mark_node)
18563 goto fail;
18565 /* Place the explicitly specified arguments in TARGS. */
18566 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
18567 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
18568 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
18571 /* Never do unification on the 'this' parameter. */
18572 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
18574 if (return_type && strict == DEDUCE_CALL)
18576 /* We're deducing for a call to the result of a template conversion
18577 function. The parms we really want are in return_type. */
18578 if (POINTER_TYPE_P (return_type))
18579 return_type = TREE_TYPE (return_type);
18580 parms = TYPE_ARG_TYPES (return_type);
18582 else if (return_type)
18584 tree *new_args;
18586 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
18587 new_args = XALLOCAVEC (tree, nargs + 1);
18588 new_args[0] = return_type;
18589 memcpy (new_args + 1, args, nargs * sizeof (tree));
18590 args = new_args;
18591 ++nargs;
18594 /* We allow incomplete unification without an error message here
18595 because the standard doesn't seem to explicitly prohibit it. Our
18596 callers must be ready to deal with unification failures in any
18597 event. */
18599 TREE_VALUE (tinst) = targs;
18600 /* If we aren't explaining yet, push tinst context so we can see where
18601 any errors (e.g. from class instantiations triggered by instantiation
18602 of default template arguments) come from. If we are explaining, this
18603 context is redundant. */
18604 if (!explain_p && !push_tinst_level (tinst))
18606 excessive_deduction_depth = true;
18607 goto fail;
18610 /* type_unification_real will pass back any access checks from default
18611 template argument substitution. */
18612 vec<deferred_access_check, va_gc> *checks;
18613 checks = NULL;
18615 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
18616 full_targs, parms, args, nargs, /*subr=*/0,
18617 strict, flags, &checks, explain_p);
18618 if (!explain_p)
18619 pop_tinst_level ();
18620 if (!ok)
18621 goto fail;
18623 /* Now that we have bindings for all of the template arguments,
18624 ensure that the arguments deduced for the template template
18625 parameters have compatible template parameter lists. We cannot
18626 check this property before we have deduced all template
18627 arguments, because the template parameter types of a template
18628 template parameter might depend on prior template parameters
18629 deduced after the template template parameter. The following
18630 ill-formed example illustrates this issue:
18632 template<typename T, template<T> class C> void f(C<5>, T);
18634 template<int N> struct X {};
18636 void g() {
18637 f(X<5>(), 5l); // error: template argument deduction fails
18640 The template parameter list of 'C' depends on the template type
18641 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
18642 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
18643 time that we deduce 'C'. */
18644 if (!template_template_parm_bindings_ok_p
18645 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
18647 unify_inconsistent_template_template_parameters (explain_p);
18648 goto fail;
18651 /* All is well so far. Now, check:
18653 [temp.deduct]
18655 When all template arguments have been deduced, all uses of
18656 template parameters in nondeduced contexts are replaced with
18657 the corresponding deduced argument values. If the
18658 substitution results in an invalid type, as described above,
18659 type deduction fails. */
18660 TREE_VALUE (tinst) = targs;
18661 if (!push_tinst_level (tinst))
18663 excessive_deduction_depth = true;
18664 goto fail;
18667 /* Also collect access checks from the instantiation. */
18668 reopen_deferring_access_checks (checks);
18670 decl = instantiate_template (fn, targs, complain);
18672 checks = get_deferred_access_checks ();
18673 pop_deferring_access_checks ();
18675 pop_tinst_level ();
18677 if (decl == error_mark_node)
18678 goto fail;
18680 /* Now perform any access checks encountered during substitution. */
18681 push_access_scope (decl);
18682 ok = perform_access_checks (checks, complain);
18683 pop_access_scope (decl);
18684 if (!ok)
18685 goto fail;
18687 /* If we're looking for an exact match, check that what we got
18688 is indeed an exact match. It might not be if some template
18689 parameters are used in non-deduced contexts. But don't check
18690 for an exact match if we have dependent template arguments;
18691 in that case we're doing partial ordering, and we already know
18692 that we have two candidates that will provide the actual type. */
18693 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
18695 tree substed = TREE_TYPE (decl);
18696 unsigned int i;
18698 tree sarg
18699 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
18700 if (return_type)
18701 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
18702 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
18703 if (!same_type_p (args[i], TREE_VALUE (sarg)))
18705 unify_type_mismatch (explain_p, args[i],
18706 TREE_VALUE (sarg));
18707 goto fail;
18711 /* After doing deduction with the inherited constructor, actually return an
18712 instantiation of the inheriting constructor. */
18713 if (orig_fn != fn)
18714 decl = instantiate_template (orig_fn, targs, complain);
18716 r = decl;
18718 fail:
18719 --deduction_depth;
18720 if (excessive_deduction_depth)
18722 if (deduction_depth == 0)
18723 /* Reset once we're all the way out. */
18724 excessive_deduction_depth = false;
18727 /* We can't free this if a pending_template entry or last_error_tinst_level
18728 is pointing at it. */
18729 if (last_pending_template == old_last_pend
18730 && last_error_tinst_level == old_error_tinst)
18731 ggc_free (tinst);
18733 return r;
18736 /* Adjust types before performing type deduction, as described in
18737 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
18738 sections are symmetric. PARM is the type of a function parameter
18739 or the return type of the conversion function. ARG is the type of
18740 the argument passed to the call, or the type of the value
18741 initialized with the result of the conversion function.
18742 ARG_EXPR is the original argument expression, which may be null. */
18744 static int
18745 maybe_adjust_types_for_deduction (unification_kind_t strict,
18746 tree* parm,
18747 tree* arg,
18748 tree arg_expr)
18750 int result = 0;
18752 switch (strict)
18754 case DEDUCE_CALL:
18755 break;
18757 case DEDUCE_CONV:
18758 /* Swap PARM and ARG throughout the remainder of this
18759 function; the handling is precisely symmetric since PARM
18760 will initialize ARG rather than vice versa. */
18761 std::swap (parm, arg);
18762 break;
18764 case DEDUCE_EXACT:
18765 /* Core issue #873: Do the DR606 thing (see below) for these cases,
18766 too, but here handle it by stripping the reference from PARM
18767 rather than by adding it to ARG. */
18768 if (TREE_CODE (*parm) == REFERENCE_TYPE
18769 && TYPE_REF_IS_RVALUE (*parm)
18770 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
18771 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
18772 && TREE_CODE (*arg) == REFERENCE_TYPE
18773 && !TYPE_REF_IS_RVALUE (*arg))
18774 *parm = TREE_TYPE (*parm);
18775 /* Nothing else to do in this case. */
18776 return 0;
18778 default:
18779 gcc_unreachable ();
18782 if (TREE_CODE (*parm) != REFERENCE_TYPE)
18784 /* [temp.deduct.call]
18786 If P is not a reference type:
18788 --If A is an array type, the pointer type produced by the
18789 array-to-pointer standard conversion (_conv.array_) is
18790 used in place of A for type deduction; otherwise,
18792 --If A is a function type, the pointer type produced by
18793 the function-to-pointer standard conversion
18794 (_conv.func_) is used in place of A for type deduction;
18795 otherwise,
18797 --If A is a cv-qualified type, the top level
18798 cv-qualifiers of A's type are ignored for type
18799 deduction. */
18800 if (TREE_CODE (*arg) == ARRAY_TYPE)
18801 *arg = build_pointer_type (TREE_TYPE (*arg));
18802 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
18803 *arg = build_pointer_type (*arg);
18804 else
18805 *arg = TYPE_MAIN_VARIANT (*arg);
18808 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
18809 reference to a cv-unqualified template parameter that does not represent a
18810 template parameter of a class template (during class template argument
18811 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
18812 an lvalue, the type "lvalue reference to A" is used in place of A for type
18813 deduction. */
18814 if (TREE_CODE (*parm) == REFERENCE_TYPE
18815 && TYPE_REF_IS_RVALUE (*parm)
18816 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
18817 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
18818 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
18819 && (arg_expr ? lvalue_p (arg_expr)
18820 /* try_one_overload doesn't provide an arg_expr, but
18821 functions are always lvalues. */
18822 : TREE_CODE (*arg) == FUNCTION_TYPE))
18823 *arg = build_reference_type (*arg);
18825 /* [temp.deduct.call]
18827 If P is a cv-qualified type, the top level cv-qualifiers
18828 of P's type are ignored for type deduction. If P is a
18829 reference type, the type referred to by P is used for
18830 type deduction. */
18831 *parm = TYPE_MAIN_VARIANT (*parm);
18832 if (TREE_CODE (*parm) == REFERENCE_TYPE)
18834 *parm = TREE_TYPE (*parm);
18835 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
18838 /* DR 322. For conversion deduction, remove a reference type on parm
18839 too (which has been swapped into ARG). */
18840 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
18841 *arg = TREE_TYPE (*arg);
18843 return result;
18846 /* Subroutine of unify_one_argument. PARM is a function parameter of a
18847 template which does contain any deducible template parameters; check if
18848 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
18849 unify_one_argument. */
18851 static int
18852 check_non_deducible_conversion (tree parm, tree arg, int strict,
18853 int flags, bool explain_p)
18855 tree type;
18857 if (!TYPE_P (arg))
18858 type = TREE_TYPE (arg);
18859 else
18860 type = arg;
18862 if (same_type_p (parm, type))
18863 return unify_success (explain_p);
18865 if (strict == DEDUCE_CONV)
18867 if (can_convert_arg (type, parm, NULL_TREE, flags,
18868 explain_p ? tf_warning_or_error : tf_none))
18869 return unify_success (explain_p);
18871 else if (strict != DEDUCE_EXACT)
18873 if (can_convert_arg (parm, type,
18874 TYPE_P (arg) ? NULL_TREE : arg,
18875 flags, explain_p ? tf_warning_or_error : tf_none))
18876 return unify_success (explain_p);
18879 if (strict == DEDUCE_EXACT)
18880 return unify_type_mismatch (explain_p, parm, arg);
18881 else
18882 return unify_arg_conversion (explain_p, parm, type, arg);
18885 static bool uses_deducible_template_parms (tree type);
18887 /* Returns true iff the expression EXPR is one from which a template
18888 argument can be deduced. In other words, if it's an undecorated
18889 use of a template non-type parameter. */
18891 static bool
18892 deducible_expression (tree expr)
18894 /* Strip implicit conversions. */
18895 while (CONVERT_EXPR_P (expr))
18896 expr = TREE_OPERAND (expr, 0);
18897 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
18900 /* Returns true iff the array domain DOMAIN uses a template parameter in a
18901 deducible way; that is, if it has a max value of <PARM> - 1. */
18903 static bool
18904 deducible_array_bound (tree domain)
18906 if (domain == NULL_TREE)
18907 return false;
18909 tree max = TYPE_MAX_VALUE (domain);
18910 if (TREE_CODE (max) != MINUS_EXPR)
18911 return false;
18913 return deducible_expression (TREE_OPERAND (max, 0));
18916 /* Returns true iff the template arguments ARGS use a template parameter
18917 in a deducible way. */
18919 static bool
18920 deducible_template_args (tree args)
18922 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
18924 bool deducible;
18925 tree elt = TREE_VEC_ELT (args, i);
18926 if (ARGUMENT_PACK_P (elt))
18927 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
18928 else
18930 if (PACK_EXPANSION_P (elt))
18931 elt = PACK_EXPANSION_PATTERN (elt);
18932 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
18933 deducible = true;
18934 else if (TYPE_P (elt))
18935 deducible = uses_deducible_template_parms (elt);
18936 else
18937 deducible = deducible_expression (elt);
18939 if (deducible)
18940 return true;
18942 return false;
18945 /* Returns true iff TYPE contains any deducible references to template
18946 parameters, as per 14.8.2.5. */
18948 static bool
18949 uses_deducible_template_parms (tree type)
18951 if (PACK_EXPANSION_P (type))
18952 type = PACK_EXPANSION_PATTERN (type);
18954 /* T
18955 cv-list T
18956 TT<T>
18957 TT<i>
18958 TT<> */
18959 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
18960 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
18961 return true;
18963 /* T*
18965 T&& */
18966 if (POINTER_TYPE_P (type))
18967 return uses_deducible_template_parms (TREE_TYPE (type));
18969 /* T[integer-constant ]
18970 type [i] */
18971 if (TREE_CODE (type) == ARRAY_TYPE)
18972 return (uses_deducible_template_parms (TREE_TYPE (type))
18973 || deducible_array_bound (TYPE_DOMAIN (type)));
18975 /* T type ::*
18976 type T::*
18977 T T::*
18978 T (type ::*)()
18979 type (T::*)()
18980 type (type ::*)(T)
18981 type (T::*)(T)
18982 T (type ::*)(T)
18983 T (T::*)()
18984 T (T::*)(T) */
18985 if (TYPE_PTRMEM_P (type))
18986 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
18987 || (uses_deducible_template_parms
18988 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
18990 /* template-name <T> (where template-name refers to a class template)
18991 template-name <i> (where template-name refers to a class template) */
18992 if (CLASS_TYPE_P (type)
18993 && CLASSTYPE_TEMPLATE_INFO (type)
18994 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
18995 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
18996 (CLASSTYPE_TI_ARGS (type)));
18998 /* type (T)
19000 T(T) */
19001 if (TREE_CODE (type) == FUNCTION_TYPE
19002 || TREE_CODE (type) == METHOD_TYPE)
19004 if (uses_deducible_template_parms (TREE_TYPE (type)))
19005 return true;
19006 tree parm = TYPE_ARG_TYPES (type);
19007 if (TREE_CODE (type) == METHOD_TYPE)
19008 parm = TREE_CHAIN (parm);
19009 for (; parm; parm = TREE_CHAIN (parm))
19010 if (uses_deducible_template_parms (TREE_VALUE (parm)))
19011 return true;
19014 return false;
19017 /* Subroutine of type_unification_real and unify_pack_expansion to
19018 handle unification of a single P/A pair. Parameters are as
19019 for those functions. */
19021 static int
19022 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
19023 int subr, unification_kind_t strict,
19024 bool explain_p)
19026 tree arg_expr = NULL_TREE;
19027 int arg_strict;
19029 if (arg == error_mark_node || parm == error_mark_node)
19030 return unify_invalid (explain_p);
19031 if (arg == unknown_type_node)
19032 /* We can't deduce anything from this, but we might get all the
19033 template args from other function args. */
19034 return unify_success (explain_p);
19036 /* Implicit conversions (Clause 4) will be performed on a function
19037 argument to convert it to the type of the corresponding function
19038 parameter if the parameter type contains no template-parameters that
19039 participate in template argument deduction. */
19040 if (strict != DEDUCE_EXACT
19041 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
19042 /* For function parameters with no deducible template parameters,
19043 just return. We'll check non-dependent conversions later. */
19044 return unify_success (explain_p);
19046 switch (strict)
19048 case DEDUCE_CALL:
19049 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
19050 | UNIFY_ALLOW_MORE_CV_QUAL
19051 | UNIFY_ALLOW_DERIVED);
19052 break;
19054 case DEDUCE_CONV:
19055 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
19056 break;
19058 case DEDUCE_EXACT:
19059 arg_strict = UNIFY_ALLOW_NONE;
19060 break;
19062 default:
19063 gcc_unreachable ();
19066 /* We only do these transformations if this is the top-level
19067 parameter_type_list in a call or declaration matching; in other
19068 situations (nested function declarators, template argument lists) we
19069 won't be comparing a type to an expression, and we don't do any type
19070 adjustments. */
19071 if (!subr)
19073 if (!TYPE_P (arg))
19075 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
19076 if (type_unknown_p (arg))
19078 /* [temp.deduct.type] A template-argument can be
19079 deduced from a pointer to function or pointer
19080 to member function argument if the set of
19081 overloaded functions does not contain function
19082 templates and at most one of a set of
19083 overloaded functions provides a unique
19084 match. */
19086 if (resolve_overloaded_unification
19087 (tparms, targs, parm, arg, strict,
19088 arg_strict, explain_p))
19089 return unify_success (explain_p);
19090 return unify_overload_resolution_failure (explain_p, arg);
19093 arg_expr = arg;
19094 arg = unlowered_expr_type (arg);
19095 if (arg == error_mark_node)
19096 return unify_invalid (explain_p);
19099 arg_strict |=
19100 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
19102 else
19103 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
19104 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
19105 return unify_template_argument_mismatch (explain_p, parm, arg);
19107 /* For deduction from an init-list we need the actual list. */
19108 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
19109 arg = arg_expr;
19110 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
19113 /* for_each_template_parm callback that always returns 0. */
19115 static int
19116 zero_r (tree, void *)
19118 return 0;
19121 /* for_each_template_parm any_fn callback to handle deduction of a template
19122 type argument from the type of an array bound. */
19124 static int
19125 array_deduction_r (tree t, void *data)
19127 tree_pair_p d = (tree_pair_p)data;
19128 tree &tparms = d->purpose;
19129 tree &targs = d->value;
19131 if (TREE_CODE (t) == ARRAY_TYPE)
19132 if (tree dom = TYPE_DOMAIN (t))
19133 if (tree max = TYPE_MAX_VALUE (dom))
19135 if (TREE_CODE (max) == MINUS_EXPR)
19136 max = TREE_OPERAND (max, 0);
19137 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
19138 unify (tparms, targs, TREE_TYPE (max), size_type_node,
19139 UNIFY_ALLOW_NONE, /*explain*/false);
19142 /* Keep walking. */
19143 return 0;
19146 /* Try to deduce any not-yet-deduced template type arguments from the type of
19147 an array bound. This is handled separately from unify because 14.8.2.5 says
19148 "The type of a type parameter is only deduced from an array bound if it is
19149 not otherwise deduced." */
19151 static void
19152 try_array_deduction (tree tparms, tree targs, tree parm)
19154 tree_pair_s data = { tparms, targs };
19155 hash_set<tree> visited;
19156 for_each_template_parm (parm, zero_r, &data, &visited,
19157 /*nondeduced*/false, array_deduction_r);
19160 /* Most parms like fn_type_unification.
19162 If SUBR is 1, we're being called recursively (to unify the
19163 arguments of a function or method parameter of a function
19164 template).
19166 CHECKS is a pointer to a vector of access checks encountered while
19167 substituting default template arguments. */
19169 static int
19170 type_unification_real (tree tparms,
19171 tree full_targs,
19172 tree xparms,
19173 const tree *xargs,
19174 unsigned int xnargs,
19175 int subr,
19176 unification_kind_t strict,
19177 int flags,
19178 vec<deferred_access_check, va_gc> **checks,
19179 bool explain_p)
19181 tree parm, arg;
19182 int i;
19183 int ntparms = TREE_VEC_LENGTH (tparms);
19184 int saw_undeduced = 0;
19185 tree parms;
19186 const tree *args;
19187 unsigned int nargs;
19188 unsigned int ia;
19190 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
19191 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
19192 gcc_assert (ntparms > 0);
19194 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
19196 /* Reset the number of non-defaulted template arguments contained
19197 in TARGS. */
19198 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
19200 again:
19201 parms = xparms;
19202 args = xargs;
19203 nargs = xnargs;
19205 ia = 0;
19206 while (parms && parms != void_list_node
19207 && ia < nargs)
19209 parm = TREE_VALUE (parms);
19211 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19212 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
19213 /* For a function parameter pack that occurs at the end of the
19214 parameter-declaration-list, the type A of each remaining
19215 argument of the call is compared with the type P of the
19216 declarator-id of the function parameter pack. */
19217 break;
19219 parms = TREE_CHAIN (parms);
19221 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19222 /* For a function parameter pack that does not occur at the
19223 end of the parameter-declaration-list, the type of the
19224 parameter pack is a non-deduced context. */
19225 continue;
19227 arg = args[ia];
19228 ++ia;
19230 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
19231 explain_p))
19232 return 1;
19235 if (parms
19236 && parms != void_list_node
19237 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
19239 /* Unify the remaining arguments with the pack expansion type. */
19240 tree argvec;
19241 tree parmvec = make_tree_vec (1);
19243 /* Allocate a TREE_VEC and copy in all of the arguments */
19244 argvec = make_tree_vec (nargs - ia);
19245 for (i = 0; ia < nargs; ++ia, ++i)
19246 TREE_VEC_ELT (argvec, i) = args[ia];
19248 /* Copy the parameter into parmvec. */
19249 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
19250 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
19251 /*subr=*/subr, explain_p))
19252 return 1;
19254 /* Advance to the end of the list of parameters. */
19255 parms = TREE_CHAIN (parms);
19258 /* Fail if we've reached the end of the parm list, and more args
19259 are present, and the parm list isn't variadic. */
19260 if (ia < nargs && parms == void_list_node)
19261 return unify_too_many_arguments (explain_p, nargs, ia);
19262 /* Fail if parms are left and they don't have default values and
19263 they aren't all deduced as empty packs (c++/57397). This is
19264 consistent with sufficient_parms_p. */
19265 if (parms && parms != void_list_node
19266 && TREE_PURPOSE (parms) == NULL_TREE)
19268 unsigned int count = nargs;
19269 tree p = parms;
19270 bool type_pack_p;
19273 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
19274 if (!type_pack_p)
19275 count++;
19276 p = TREE_CHAIN (p);
19278 while (p && p != void_list_node);
19279 if (count != nargs)
19280 return unify_too_few_arguments (explain_p, ia, count,
19281 type_pack_p);
19284 if (!subr)
19286 tsubst_flags_t complain = (explain_p
19287 ? tf_warning_or_error
19288 : tf_none);
19289 bool tried_array_deduction = (cxx_dialect < cxx1z);
19291 for (i = 0; i < ntparms; i++)
19293 tree targ = TREE_VEC_ELT (targs, i);
19294 tree tparm = TREE_VEC_ELT (tparms, i);
19296 /* Clear the "incomplete" flags on all argument packs now so that
19297 substituting them into later default arguments works. */
19298 if (targ && ARGUMENT_PACK_P (targ))
19300 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
19301 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
19304 if (targ || tparm == error_mark_node)
19305 continue;
19306 tparm = TREE_VALUE (tparm);
19308 if (TREE_CODE (tparm) == TYPE_DECL
19309 && !tried_array_deduction)
19311 try_array_deduction (tparms, targs, xparms);
19312 tried_array_deduction = true;
19313 if (TREE_VEC_ELT (targs, i))
19314 continue;
19317 /* If this is an undeduced nontype parameter that depends on
19318 a type parameter, try another pass; its type may have been
19319 deduced from a later argument than the one from which
19320 this parameter can be deduced. */
19321 if (TREE_CODE (tparm) == PARM_DECL
19322 && uses_template_parms (TREE_TYPE (tparm))
19323 && saw_undeduced < 2)
19325 saw_undeduced = 1;
19326 continue;
19329 /* Core issue #226 (C++0x) [temp.deduct]:
19331 If a template argument has not been deduced, its
19332 default template argument, if any, is used.
19334 When we are in C++98 mode, TREE_PURPOSE will either
19335 be NULL_TREE or ERROR_MARK_NODE, so we do not need
19336 to explicitly check cxx_dialect here. */
19337 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
19338 /* OK, there is a default argument. Wait until after the
19339 conversion check to do substitution. */
19340 continue;
19342 /* If the type parameter is a parameter pack, then it will
19343 be deduced to an empty parameter pack. */
19344 if (template_parameter_pack_p (tparm))
19346 tree arg;
19348 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
19350 arg = make_node (NONTYPE_ARGUMENT_PACK);
19351 TREE_CONSTANT (arg) = 1;
19353 else
19354 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
19356 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
19358 TREE_VEC_ELT (targs, i) = arg;
19359 continue;
19362 return unify_parameter_deduction_failure (explain_p, tparm);
19365 /* DR 1391: All parameters have args, now check non-dependent parms for
19366 convertibility. */
19367 if (saw_undeduced < 2)
19368 for (ia = 0, parms = xparms, args = xargs, nargs = xnargs;
19369 parms && parms != void_list_node && ia < nargs; )
19371 parm = TREE_VALUE (parms);
19373 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19374 && (!TREE_CHAIN (parms)
19375 || TREE_CHAIN (parms) == void_list_node))
19376 /* For a function parameter pack that occurs at the end of the
19377 parameter-declaration-list, the type A of each remaining
19378 argument of the call is compared with the type P of the
19379 declarator-id of the function parameter pack. */
19380 break;
19382 parms = TREE_CHAIN (parms);
19384 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19385 /* For a function parameter pack that does not occur at the
19386 end of the parameter-declaration-list, the type of the
19387 parameter pack is a non-deduced context. */
19388 continue;
19390 arg = args[ia];
19391 ++ia;
19393 if (uses_template_parms (parm))
19394 continue;
19395 if (check_non_deducible_conversion (parm, arg, strict, flags,
19396 explain_p))
19397 return 1;
19400 /* Now substitute into the default template arguments. */
19401 for (i = 0; i < ntparms; i++)
19403 tree targ = TREE_VEC_ELT (targs, i);
19404 tree tparm = TREE_VEC_ELT (tparms, i);
19406 if (targ || tparm == error_mark_node)
19407 continue;
19408 tree parm = TREE_VALUE (tparm);
19410 if (TREE_CODE (parm) == PARM_DECL
19411 && uses_template_parms (TREE_TYPE (parm))
19412 && saw_undeduced < 2)
19413 continue;
19415 tree arg = TREE_PURPOSE (tparm);
19416 reopen_deferring_access_checks (*checks);
19417 location_t save_loc = input_location;
19418 if (DECL_P (parm))
19419 input_location = DECL_SOURCE_LOCATION (parm);
19420 arg = tsubst_template_arg (arg, full_targs, complain, NULL_TREE);
19421 if (!uses_template_parms (arg))
19422 arg = convert_template_argument (parm, arg, full_targs, complain,
19423 i, NULL_TREE);
19424 else if (saw_undeduced < 2)
19425 arg = NULL_TREE;
19426 else
19427 arg = error_mark_node;
19428 input_location = save_loc;
19429 *checks = get_deferred_access_checks ();
19430 pop_deferring_access_checks ();
19431 if (arg == error_mark_node)
19432 return 1;
19433 else if (arg)
19435 TREE_VEC_ELT (targs, i) = arg;
19436 /* The position of the first default template argument,
19437 is also the number of non-defaulted arguments in TARGS.
19438 Record that. */
19439 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19440 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
19444 if (saw_undeduced++ == 1)
19445 goto again;
19448 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19449 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
19451 return unify_success (explain_p);
19454 /* Subroutine of type_unification_real. Args are like the variables
19455 at the call site. ARG is an overloaded function (or template-id);
19456 we try deducing template args from each of the overloads, and if
19457 only one succeeds, we go with that. Modifies TARGS and returns
19458 true on success. */
19460 static bool
19461 resolve_overloaded_unification (tree tparms,
19462 tree targs,
19463 tree parm,
19464 tree arg,
19465 unification_kind_t strict,
19466 int sub_strict,
19467 bool explain_p)
19469 tree tempargs = copy_node (targs);
19470 int good = 0;
19471 tree goodfn = NULL_TREE;
19472 bool addr_p;
19474 if (TREE_CODE (arg) == ADDR_EXPR)
19476 arg = TREE_OPERAND (arg, 0);
19477 addr_p = true;
19479 else
19480 addr_p = false;
19482 if (TREE_CODE (arg) == COMPONENT_REF)
19483 /* Handle `&x' where `x' is some static or non-static member
19484 function name. */
19485 arg = TREE_OPERAND (arg, 1);
19487 if (TREE_CODE (arg) == OFFSET_REF)
19488 arg = TREE_OPERAND (arg, 1);
19490 /* Strip baselink information. */
19491 if (BASELINK_P (arg))
19492 arg = BASELINK_FUNCTIONS (arg);
19494 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
19496 /* If we got some explicit template args, we need to plug them into
19497 the affected templates before we try to unify, in case the
19498 explicit args will completely resolve the templates in question. */
19500 int ok = 0;
19501 tree expl_subargs = TREE_OPERAND (arg, 1);
19502 arg = TREE_OPERAND (arg, 0);
19504 for (lkp_iterator iter (arg); iter; ++iter)
19506 tree fn = *iter;
19507 tree subargs, elem;
19509 if (TREE_CODE (fn) != TEMPLATE_DECL)
19510 continue;
19512 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19513 expl_subargs, NULL_TREE, tf_none,
19514 /*require_all_args=*/true,
19515 /*use_default_args=*/true);
19516 if (subargs != error_mark_node
19517 && !any_dependent_template_arguments_p (subargs))
19519 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
19520 if (try_one_overload (tparms, targs, tempargs, parm,
19521 elem, strict, sub_strict, addr_p, explain_p)
19522 && (!goodfn || !same_type_p (goodfn, elem)))
19524 goodfn = elem;
19525 ++good;
19528 else if (subargs)
19529 ++ok;
19531 /* If no templates (or more than one) are fully resolved by the
19532 explicit arguments, this template-id is a non-deduced context; it
19533 could still be OK if we deduce all template arguments for the
19534 enclosing call through other arguments. */
19535 if (good != 1)
19536 good = ok;
19538 else if (TREE_CODE (arg) != OVERLOAD
19539 && TREE_CODE (arg) != FUNCTION_DECL)
19540 /* If ARG is, for example, "(0, &f)" then its type will be unknown
19541 -- but the deduction does not succeed because the expression is
19542 not just the function on its own. */
19543 return false;
19544 else
19545 for (lkp_iterator iter (arg); iter; ++iter)
19547 tree fn = *iter;
19548 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
19549 strict, sub_strict, addr_p, explain_p)
19550 && (!goodfn || !decls_match (goodfn, fn)))
19552 goodfn = fn;
19553 ++good;
19557 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19558 to function or pointer to member function argument if the set of
19559 overloaded functions does not contain function templates and at most
19560 one of a set of overloaded functions provides a unique match.
19562 So if we found multiple possibilities, we return success but don't
19563 deduce anything. */
19565 if (good == 1)
19567 int i = TREE_VEC_LENGTH (targs);
19568 for (; i--; )
19569 if (TREE_VEC_ELT (tempargs, i))
19571 tree old = TREE_VEC_ELT (targs, i);
19572 tree new_ = TREE_VEC_ELT (tempargs, i);
19573 if (new_ && old && ARGUMENT_PACK_P (old)
19574 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
19575 /* Don't forget explicit template arguments in a pack. */
19576 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
19577 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
19578 TREE_VEC_ELT (targs, i) = new_;
19581 if (good)
19582 return true;
19584 return false;
19587 /* Core DR 115: In contexts where deduction is done and fails, or in
19588 contexts where deduction is not done, if a template argument list is
19589 specified and it, along with any default template arguments, identifies
19590 a single function template specialization, then the template-id is an
19591 lvalue for the function template specialization. */
19593 tree
19594 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
19596 tree expr, offset, baselink;
19597 bool addr;
19599 if (!type_unknown_p (orig_expr))
19600 return orig_expr;
19602 expr = orig_expr;
19603 addr = false;
19604 offset = NULL_TREE;
19605 baselink = NULL_TREE;
19607 if (TREE_CODE (expr) == ADDR_EXPR)
19609 expr = TREE_OPERAND (expr, 0);
19610 addr = true;
19612 if (TREE_CODE (expr) == OFFSET_REF)
19614 offset = expr;
19615 expr = TREE_OPERAND (expr, 1);
19617 if (BASELINK_P (expr))
19619 baselink = expr;
19620 expr = BASELINK_FUNCTIONS (expr);
19623 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
19625 int good = 0;
19626 tree goodfn = NULL_TREE;
19628 /* If we got some explicit template args, we need to plug them into
19629 the affected templates before we try to unify, in case the
19630 explicit args will completely resolve the templates in question. */
19632 tree expl_subargs = TREE_OPERAND (expr, 1);
19633 tree arg = TREE_OPERAND (expr, 0);
19634 tree badfn = NULL_TREE;
19635 tree badargs = NULL_TREE;
19637 for (lkp_iterator iter (arg); iter; ++iter)
19639 tree fn = *iter;
19640 tree subargs, elem;
19642 if (TREE_CODE (fn) != TEMPLATE_DECL)
19643 continue;
19645 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19646 expl_subargs, NULL_TREE, tf_none,
19647 /*require_all_args=*/true,
19648 /*use_default_args=*/true);
19649 if (subargs != error_mark_node
19650 && !any_dependent_template_arguments_p (subargs))
19652 elem = instantiate_template (fn, subargs, tf_none);
19653 if (elem == error_mark_node)
19655 badfn = fn;
19656 badargs = subargs;
19658 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
19660 goodfn = elem;
19661 ++good;
19665 if (good == 1)
19667 mark_used (goodfn);
19668 expr = goodfn;
19669 if (baselink)
19670 expr = build_baselink (BASELINK_BINFO (baselink),
19671 BASELINK_ACCESS_BINFO (baselink),
19672 expr, BASELINK_OPTYPE (baselink));
19673 if (offset)
19675 tree base
19676 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
19677 expr = build_offset_ref (base, expr, addr, complain);
19679 if (addr)
19680 expr = cp_build_addr_expr (expr, complain);
19681 return expr;
19683 else if (good == 0 && badargs && (complain & tf_error))
19684 /* There were no good options and at least one bad one, so let the
19685 user know what the problem is. */
19686 instantiate_template (badfn, badargs, complain);
19688 return orig_expr;
19691 /* Subroutine of resolve_overloaded_unification; does deduction for a single
19692 overload. Fills TARGS with any deduced arguments, or error_mark_node if
19693 different overloads deduce different arguments for a given parm.
19694 ADDR_P is true if the expression for which deduction is being
19695 performed was of the form "& fn" rather than simply "fn".
19697 Returns 1 on success. */
19699 static int
19700 try_one_overload (tree tparms,
19701 tree orig_targs,
19702 tree targs,
19703 tree parm,
19704 tree arg,
19705 unification_kind_t strict,
19706 int sub_strict,
19707 bool addr_p,
19708 bool explain_p)
19710 int nargs;
19711 tree tempargs;
19712 int i;
19714 if (arg == error_mark_node)
19715 return 0;
19717 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19718 to function or pointer to member function argument if the set of
19719 overloaded functions does not contain function templates and at most
19720 one of a set of overloaded functions provides a unique match.
19722 So if this is a template, just return success. */
19724 if (uses_template_parms (arg))
19725 return 1;
19727 if (TREE_CODE (arg) == METHOD_TYPE)
19728 arg = build_ptrmemfunc_type (build_pointer_type (arg));
19729 else if (addr_p)
19730 arg = build_pointer_type (arg);
19732 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
19734 /* We don't copy orig_targs for this because if we have already deduced
19735 some template args from previous args, unify would complain when we
19736 try to deduce a template parameter for the same argument, even though
19737 there isn't really a conflict. */
19738 nargs = TREE_VEC_LENGTH (targs);
19739 tempargs = make_tree_vec (nargs);
19741 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
19742 return 0;
19744 /* First make sure we didn't deduce anything that conflicts with
19745 explicitly specified args. */
19746 for (i = nargs; i--; )
19748 tree elt = TREE_VEC_ELT (tempargs, i);
19749 tree oldelt = TREE_VEC_ELT (orig_targs, i);
19751 if (!elt)
19752 /*NOP*/;
19753 else if (uses_template_parms (elt))
19754 /* Since we're unifying against ourselves, we will fill in
19755 template args used in the function parm list with our own
19756 template parms. Discard them. */
19757 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
19758 else if (oldelt && ARGUMENT_PACK_P (oldelt))
19760 /* Check that the argument at each index of the deduced argument pack
19761 is equivalent to the corresponding explicitly specified argument.
19762 We may have deduced more arguments than were explicitly specified,
19763 and that's OK. */
19765 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
19766 that's wrong if we deduce the same argument pack from multiple
19767 function arguments: it's only incomplete the first time. */
19769 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
19770 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
19772 if (TREE_VEC_LENGTH (deduced_pack)
19773 < TREE_VEC_LENGTH (explicit_pack))
19774 return 0;
19776 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
19777 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
19778 TREE_VEC_ELT (deduced_pack, j)))
19779 return 0;
19781 else if (oldelt && !template_args_equal (oldelt, elt))
19782 return 0;
19785 for (i = nargs; i--; )
19787 tree elt = TREE_VEC_ELT (tempargs, i);
19789 if (elt)
19790 TREE_VEC_ELT (targs, i) = elt;
19793 return 1;
19796 /* PARM is a template class (perhaps with unbound template
19797 parameters). ARG is a fully instantiated type. If ARG can be
19798 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
19799 TARGS are as for unify. */
19801 static tree
19802 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
19803 bool explain_p)
19805 tree copy_of_targs;
19807 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
19808 return NULL_TREE;
19809 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19810 /* Matches anything. */;
19811 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
19812 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
19813 return NULL_TREE;
19815 /* We need to make a new template argument vector for the call to
19816 unify. If we used TARGS, we'd clutter it up with the result of
19817 the attempted unification, even if this class didn't work out.
19818 We also don't want to commit ourselves to all the unifications
19819 we've already done, since unification is supposed to be done on
19820 an argument-by-argument basis. In other words, consider the
19821 following pathological case:
19823 template <int I, int J, int K>
19824 struct S {};
19826 template <int I, int J>
19827 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
19829 template <int I, int J, int K>
19830 void f(S<I, J, K>, S<I, I, I>);
19832 void g() {
19833 S<0, 0, 0> s0;
19834 S<0, 1, 2> s2;
19836 f(s0, s2);
19839 Now, by the time we consider the unification involving `s2', we
19840 already know that we must have `f<0, 0, 0>'. But, even though
19841 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
19842 because there are two ways to unify base classes of S<0, 1, 2>
19843 with S<I, I, I>. If we kept the already deduced knowledge, we
19844 would reject the possibility I=1. */
19845 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
19847 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19849 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
19850 return NULL_TREE;
19851 return arg;
19854 /* If unification failed, we're done. */
19855 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
19856 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
19857 return NULL_TREE;
19859 return arg;
19862 /* Given a template type PARM and a class type ARG, find the unique
19863 base type in ARG that is an instance of PARM. We do not examine
19864 ARG itself; only its base-classes. If there is not exactly one
19865 appropriate base class, return NULL_TREE. PARM may be the type of
19866 a partial specialization, as well as a plain template type. Used
19867 by unify. */
19869 static enum template_base_result
19870 get_template_base (tree tparms, tree targs, tree parm, tree arg,
19871 bool explain_p, tree *result)
19873 tree rval = NULL_TREE;
19874 tree binfo;
19876 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
19878 binfo = TYPE_BINFO (complete_type (arg));
19879 if (!binfo)
19881 /* The type could not be completed. */
19882 *result = NULL_TREE;
19883 return tbr_incomplete_type;
19886 /* Walk in inheritance graph order. The search order is not
19887 important, and this avoids multiple walks of virtual bases. */
19888 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
19890 tree r = try_class_unification (tparms, targs, parm,
19891 BINFO_TYPE (binfo), explain_p);
19893 if (r)
19895 /* If there is more than one satisfactory baseclass, then:
19897 [temp.deduct.call]
19899 If they yield more than one possible deduced A, the type
19900 deduction fails.
19902 applies. */
19903 if (rval && !same_type_p (r, rval))
19905 *result = NULL_TREE;
19906 return tbr_ambiguous_baseclass;
19909 rval = r;
19913 *result = rval;
19914 return tbr_success;
19917 /* Returns the level of DECL, which declares a template parameter. */
19919 static int
19920 template_decl_level (tree decl)
19922 switch (TREE_CODE (decl))
19924 case TYPE_DECL:
19925 case TEMPLATE_DECL:
19926 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
19928 case PARM_DECL:
19929 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
19931 default:
19932 gcc_unreachable ();
19934 return 0;
19937 /* Decide whether ARG can be unified with PARM, considering only the
19938 cv-qualifiers of each type, given STRICT as documented for unify.
19939 Returns nonzero iff the unification is OK on that basis. */
19941 static int
19942 check_cv_quals_for_unify (int strict, tree arg, tree parm)
19944 int arg_quals = cp_type_quals (arg);
19945 int parm_quals = cp_type_quals (parm);
19947 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
19948 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
19950 /* Although a CVR qualifier is ignored when being applied to a
19951 substituted template parameter ([8.3.2]/1 for example), that
19952 does not allow us to unify "const T" with "int&" because both
19953 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
19954 It is ok when we're allowing additional CV qualifiers
19955 at the outer level [14.8.2.1]/3,1st bullet. */
19956 if ((TREE_CODE (arg) == REFERENCE_TYPE
19957 || TREE_CODE (arg) == FUNCTION_TYPE
19958 || TREE_CODE (arg) == METHOD_TYPE)
19959 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
19960 return 0;
19962 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
19963 && (parm_quals & TYPE_QUAL_RESTRICT))
19964 return 0;
19967 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
19968 && (arg_quals & parm_quals) != parm_quals)
19969 return 0;
19971 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
19972 && (parm_quals & arg_quals) != arg_quals)
19973 return 0;
19975 return 1;
19978 /* Determines the LEVEL and INDEX for the template parameter PARM. */
19979 void
19980 template_parm_level_and_index (tree parm, int* level, int* index)
19982 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
19983 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
19984 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19986 *index = TEMPLATE_TYPE_IDX (parm);
19987 *level = TEMPLATE_TYPE_LEVEL (parm);
19989 else
19991 *index = TEMPLATE_PARM_IDX (parm);
19992 *level = TEMPLATE_PARM_LEVEL (parm);
19996 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
19997 do { \
19998 if (unify (TP, TA, P, A, S, EP)) \
19999 return 1; \
20000 } while (0)
20002 /* Unifies the remaining arguments in PACKED_ARGS with the pack
20003 expansion at the end of PACKED_PARMS. Returns 0 if the type
20004 deduction succeeds, 1 otherwise. STRICT is the same as in
20005 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
20006 function call argument list. We'll need to adjust the arguments to make them
20007 types. SUBR tells us if this is from a recursive call to
20008 type_unification_real, or for comparing two template argument
20009 lists. */
20011 static int
20012 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
20013 tree packed_args, unification_kind_t strict,
20014 bool subr, bool explain_p)
20016 tree parm
20017 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
20018 tree pattern = PACK_EXPANSION_PATTERN (parm);
20019 tree pack, packs = NULL_TREE;
20020 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
20022 packed_args = expand_template_argument_pack (packed_args);
20024 int len = TREE_VEC_LENGTH (packed_args);
20026 /* Determine the parameter packs we will be deducing from the
20027 pattern, and record their current deductions. */
20028 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
20029 pack; pack = TREE_CHAIN (pack))
20031 tree parm_pack = TREE_VALUE (pack);
20032 int idx, level;
20034 /* Determine the index and level of this parameter pack. */
20035 template_parm_level_and_index (parm_pack, &level, &idx);
20037 /* Keep track of the parameter packs and their corresponding
20038 argument packs. */
20039 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
20040 TREE_TYPE (packs) = make_tree_vec (len - start);
20043 /* Loop through all of the arguments that have not yet been
20044 unified and unify each with the pattern. */
20045 for (i = start; i < len; i++)
20047 tree parm;
20048 bool any_explicit = false;
20049 tree arg = TREE_VEC_ELT (packed_args, i);
20051 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
20052 or the element of its argument pack at the current index if
20053 this argument was explicitly specified. */
20054 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20056 int idx, level;
20057 tree arg, pargs;
20058 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20060 arg = NULL_TREE;
20061 if (TREE_VALUE (pack)
20062 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
20063 && (i - start < TREE_VEC_LENGTH (pargs)))
20065 any_explicit = true;
20066 arg = TREE_VEC_ELT (pargs, i - start);
20068 TMPL_ARG (targs, level, idx) = arg;
20071 /* If we had explicit template arguments, substitute them into the
20072 pattern before deduction. */
20073 if (any_explicit)
20075 /* Some arguments might still be unspecified or dependent. */
20076 bool dependent;
20077 ++processing_template_decl;
20078 dependent = any_dependent_template_arguments_p (targs);
20079 if (!dependent)
20080 --processing_template_decl;
20081 parm = tsubst (pattern, targs,
20082 explain_p ? tf_warning_or_error : tf_none,
20083 NULL_TREE);
20084 if (dependent)
20085 --processing_template_decl;
20086 if (parm == error_mark_node)
20087 return 1;
20089 else
20090 parm = pattern;
20092 /* Unify the pattern with the current argument. */
20093 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
20094 explain_p))
20095 return 1;
20097 /* For each parameter pack, collect the deduced value. */
20098 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20100 int idx, level;
20101 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20103 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
20104 TMPL_ARG (targs, level, idx);
20108 /* Verify that the results of unification with the parameter packs
20109 produce results consistent with what we've seen before, and make
20110 the deduced argument packs available. */
20111 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20113 tree old_pack = TREE_VALUE (pack);
20114 tree new_args = TREE_TYPE (pack);
20115 int i, len = TREE_VEC_LENGTH (new_args);
20116 int idx, level;
20117 bool nondeduced_p = false;
20119 /* By default keep the original deduced argument pack.
20120 If necessary, more specific code is going to update the
20121 resulting deduced argument later down in this function. */
20122 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20123 TMPL_ARG (targs, level, idx) = old_pack;
20125 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
20126 actually deduce anything. */
20127 for (i = 0; i < len && !nondeduced_p; ++i)
20128 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
20129 nondeduced_p = true;
20130 if (nondeduced_p)
20131 continue;
20133 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
20135 /* If we had fewer function args than explicit template args,
20136 just use the explicits. */
20137 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20138 int explicit_len = TREE_VEC_LENGTH (explicit_args);
20139 if (len < explicit_len)
20140 new_args = explicit_args;
20143 if (!old_pack)
20145 tree result;
20146 /* Build the deduced *_ARGUMENT_PACK. */
20147 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
20149 result = make_node (NONTYPE_ARGUMENT_PACK);
20150 TREE_CONSTANT (result) = 1;
20152 else
20153 result = cxx_make_type (TYPE_ARGUMENT_PACK);
20155 SET_ARGUMENT_PACK_ARGS (result, new_args);
20157 /* Note the deduced argument packs for this parameter
20158 pack. */
20159 TMPL_ARG (targs, level, idx) = result;
20161 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
20162 && (ARGUMENT_PACK_ARGS (old_pack)
20163 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
20165 /* We only had the explicitly-provided arguments before, but
20166 now we have a complete set of arguments. */
20167 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20169 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
20170 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
20171 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
20173 else
20175 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
20176 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
20178 if (!comp_template_args (old_args, new_args,
20179 &bad_old_arg, &bad_new_arg))
20180 /* Inconsistent unification of this parameter pack. */
20181 return unify_parameter_pack_inconsistent (explain_p,
20182 bad_old_arg,
20183 bad_new_arg);
20187 return unify_success (explain_p);
20190 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
20191 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
20192 parameters and return value are as for unify. */
20194 static int
20195 unify_array_domain (tree tparms, tree targs,
20196 tree parm_dom, tree arg_dom,
20197 bool explain_p)
20199 tree parm_max;
20200 tree arg_max;
20201 bool parm_cst;
20202 bool arg_cst;
20204 /* Our representation of array types uses "N - 1" as the
20205 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
20206 not an integer constant. We cannot unify arbitrarily
20207 complex expressions, so we eliminate the MINUS_EXPRs
20208 here. */
20209 parm_max = TYPE_MAX_VALUE (parm_dom);
20210 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
20211 if (!parm_cst)
20213 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
20214 parm_max = TREE_OPERAND (parm_max, 0);
20216 arg_max = TYPE_MAX_VALUE (arg_dom);
20217 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
20218 if (!arg_cst)
20220 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
20221 trying to unify the type of a variable with the type
20222 of a template parameter. For example:
20224 template <unsigned int N>
20225 void f (char (&) [N]);
20226 int g();
20227 void h(int i) {
20228 char a[g(i)];
20229 f(a);
20232 Here, the type of the ARG will be "int [g(i)]", and
20233 may be a SAVE_EXPR, etc. */
20234 if (TREE_CODE (arg_max) != MINUS_EXPR)
20235 return unify_vla_arg (explain_p, arg_dom);
20236 arg_max = TREE_OPERAND (arg_max, 0);
20239 /* If only one of the bounds used a MINUS_EXPR, compensate
20240 by adding one to the other bound. */
20241 if (parm_cst && !arg_cst)
20242 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
20243 integer_type_node,
20244 parm_max,
20245 integer_one_node);
20246 else if (arg_cst && !parm_cst)
20247 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
20248 integer_type_node,
20249 arg_max,
20250 integer_one_node);
20252 return unify (tparms, targs, parm_max, arg_max,
20253 UNIFY_ALLOW_INTEGER, explain_p);
20256 /* Returns whether T, a P or A in unify, is a type, template or expression. */
20258 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
20260 static pa_kind_t
20261 pa_kind (tree t)
20263 if (PACK_EXPANSION_P (t))
20264 t = PACK_EXPANSION_PATTERN (t);
20265 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
20266 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
20267 || DECL_TYPE_TEMPLATE_P (t))
20268 return pa_tmpl;
20269 else if (TYPE_P (t))
20270 return pa_type;
20271 else
20272 return pa_expr;
20275 /* Deduce the value of template parameters. TPARMS is the (innermost)
20276 set of template parameters to a template. TARGS is the bindings
20277 for those template parameters, as determined thus far; TARGS may
20278 include template arguments for outer levels of template parameters
20279 as well. PARM is a parameter to a template function, or a
20280 subcomponent of that parameter; ARG is the corresponding argument.
20281 This function attempts to match PARM with ARG in a manner
20282 consistent with the existing assignments in TARGS. If more values
20283 are deduced, then TARGS is updated.
20285 Returns 0 if the type deduction succeeds, 1 otherwise. The
20286 parameter STRICT is a bitwise or of the following flags:
20288 UNIFY_ALLOW_NONE:
20289 Require an exact match between PARM and ARG.
20290 UNIFY_ALLOW_MORE_CV_QUAL:
20291 Allow the deduced ARG to be more cv-qualified (by qualification
20292 conversion) than ARG.
20293 UNIFY_ALLOW_LESS_CV_QUAL:
20294 Allow the deduced ARG to be less cv-qualified than ARG.
20295 UNIFY_ALLOW_DERIVED:
20296 Allow the deduced ARG to be a template base class of ARG,
20297 or a pointer to a template base class of the type pointed to by
20298 ARG.
20299 UNIFY_ALLOW_INTEGER:
20300 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
20301 case for more information.
20302 UNIFY_ALLOW_OUTER_LEVEL:
20303 This is the outermost level of a deduction. Used to determine validity
20304 of qualification conversions. A valid qualification conversion must
20305 have const qualified pointers leading up to the inner type which
20306 requires additional CV quals, except at the outer level, where const
20307 is not required [conv.qual]. It would be normal to set this flag in
20308 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
20309 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
20310 This is the outermost level of a deduction, and PARM can be more CV
20311 qualified at this point.
20312 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
20313 This is the outermost level of a deduction, and PARM can be less CV
20314 qualified at this point. */
20316 static int
20317 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
20318 bool explain_p)
20320 int idx;
20321 tree targ;
20322 tree tparm;
20323 int strict_in = strict;
20324 tsubst_flags_t complain = (explain_p
20325 ? tf_warning_or_error
20326 : tf_none);
20328 /* I don't think this will do the right thing with respect to types.
20329 But the only case I've seen it in so far has been array bounds, where
20330 signedness is the only information lost, and I think that will be
20331 okay. */
20332 while (CONVERT_EXPR_P (parm))
20333 parm = TREE_OPERAND (parm, 0);
20335 if (arg == error_mark_node)
20336 return unify_invalid (explain_p);
20337 if (arg == unknown_type_node
20338 || arg == init_list_type_node)
20339 /* We can't deduce anything from this, but we might get all the
20340 template args from other function args. */
20341 return unify_success (explain_p);
20343 if (parm == any_targ_node || arg == any_targ_node)
20344 return unify_success (explain_p);
20346 /* If PARM uses template parameters, then we can't bail out here,
20347 even if ARG == PARM, since we won't record unifications for the
20348 template parameters. We might need them if we're trying to
20349 figure out which of two things is more specialized. */
20350 if (arg == parm && !uses_template_parms (parm))
20351 return unify_success (explain_p);
20353 /* Handle init lists early, so the rest of the function can assume
20354 we're dealing with a type. */
20355 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
20357 tree elt, elttype;
20358 unsigned i;
20359 tree orig_parm = parm;
20361 /* Replace T with std::initializer_list<T> for deduction. */
20362 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20363 && flag_deduce_init_list)
20364 parm = listify (parm);
20366 if (!is_std_init_list (parm)
20367 && TREE_CODE (parm) != ARRAY_TYPE)
20368 /* We can only deduce from an initializer list argument if the
20369 parameter is std::initializer_list or an array; otherwise this
20370 is a non-deduced context. */
20371 return unify_success (explain_p);
20373 if (TREE_CODE (parm) == ARRAY_TYPE)
20374 elttype = TREE_TYPE (parm);
20375 else
20377 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
20378 /* Deduction is defined in terms of a single type, so just punt
20379 on the (bizarre) std::initializer_list<T...>. */
20380 if (PACK_EXPANSION_P (elttype))
20381 return unify_success (explain_p);
20384 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
20386 int elt_strict = strict;
20388 if (elt == error_mark_node)
20389 return unify_invalid (explain_p);
20391 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
20393 tree type = TREE_TYPE (elt);
20394 if (type == error_mark_node)
20395 return unify_invalid (explain_p);
20396 /* It should only be possible to get here for a call. */
20397 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
20398 elt_strict |= maybe_adjust_types_for_deduction
20399 (DEDUCE_CALL, &elttype, &type, elt);
20400 elt = type;
20403 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
20404 explain_p);
20407 if (TREE_CODE (parm) == ARRAY_TYPE
20408 && deducible_array_bound (TYPE_DOMAIN (parm)))
20410 /* Also deduce from the length of the initializer list. */
20411 tree max = size_int (CONSTRUCTOR_NELTS (arg));
20412 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
20413 if (idx == error_mark_node)
20414 return unify_invalid (explain_p);
20415 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20416 idx, explain_p);
20419 /* If the std::initializer_list<T> deduction worked, replace the
20420 deduced A with std::initializer_list<A>. */
20421 if (orig_parm != parm)
20423 idx = TEMPLATE_TYPE_IDX (orig_parm);
20424 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20425 targ = listify (targ);
20426 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
20428 return unify_success (explain_p);
20431 /* If parm and arg aren't the same kind of thing (template, type, or
20432 expression), fail early. */
20433 if (pa_kind (parm) != pa_kind (arg))
20434 return unify_invalid (explain_p);
20436 /* Immediately reject some pairs that won't unify because of
20437 cv-qualification mismatches. */
20438 if (TREE_CODE (arg) == TREE_CODE (parm)
20439 && TYPE_P (arg)
20440 /* It is the elements of the array which hold the cv quals of an array
20441 type, and the elements might be template type parms. We'll check
20442 when we recurse. */
20443 && TREE_CODE (arg) != ARRAY_TYPE
20444 /* We check the cv-qualifiers when unifying with template type
20445 parameters below. We want to allow ARG `const T' to unify with
20446 PARM `T' for example, when computing which of two templates
20447 is more specialized, for example. */
20448 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
20449 && !check_cv_quals_for_unify (strict_in, arg, parm))
20450 return unify_cv_qual_mismatch (explain_p, parm, arg);
20452 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
20453 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
20454 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
20455 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
20456 strict &= ~UNIFY_ALLOW_DERIVED;
20457 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
20458 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
20460 switch (TREE_CODE (parm))
20462 case TYPENAME_TYPE:
20463 case SCOPE_REF:
20464 case UNBOUND_CLASS_TEMPLATE:
20465 /* In a type which contains a nested-name-specifier, template
20466 argument values cannot be deduced for template parameters used
20467 within the nested-name-specifier. */
20468 return unify_success (explain_p);
20470 case TEMPLATE_TYPE_PARM:
20471 case TEMPLATE_TEMPLATE_PARM:
20472 case BOUND_TEMPLATE_TEMPLATE_PARM:
20473 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20474 if (error_operand_p (tparm))
20475 return unify_invalid (explain_p);
20477 if (TEMPLATE_TYPE_LEVEL (parm)
20478 != template_decl_level (tparm))
20479 /* The PARM is not one we're trying to unify. Just check
20480 to see if it matches ARG. */
20482 if (TREE_CODE (arg) == TREE_CODE (parm)
20483 && (is_auto (parm) ? is_auto (arg)
20484 : same_type_p (parm, arg)))
20485 return unify_success (explain_p);
20486 else
20487 return unify_type_mismatch (explain_p, parm, arg);
20489 idx = TEMPLATE_TYPE_IDX (parm);
20490 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20491 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
20492 if (error_operand_p (tparm))
20493 return unify_invalid (explain_p);
20495 /* Check for mixed types and values. */
20496 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20497 && TREE_CODE (tparm) != TYPE_DECL)
20498 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20499 && TREE_CODE (tparm) != TEMPLATE_DECL))
20500 gcc_unreachable ();
20502 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20504 if ((strict_in & UNIFY_ALLOW_DERIVED)
20505 && CLASS_TYPE_P (arg))
20507 /* First try to match ARG directly. */
20508 tree t = try_class_unification (tparms, targs, parm, arg,
20509 explain_p);
20510 if (!t)
20512 /* Otherwise, look for a suitable base of ARG, as below. */
20513 enum template_base_result r;
20514 r = get_template_base (tparms, targs, parm, arg,
20515 explain_p, &t);
20516 if (!t)
20517 return unify_no_common_base (explain_p, r, parm, arg);
20518 arg = t;
20521 /* ARG must be constructed from a template class or a template
20522 template parameter. */
20523 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
20524 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20525 return unify_template_deduction_failure (explain_p, parm, arg);
20527 /* Deduce arguments T, i from TT<T> or TT<i>. */
20528 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
20529 return 1;
20531 arg = TYPE_TI_TEMPLATE (arg);
20533 /* Fall through to deduce template name. */
20536 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20537 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20539 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
20541 /* Simple cases: Value already set, does match or doesn't. */
20542 if (targ != NULL_TREE && template_args_equal (targ, arg))
20543 return unify_success (explain_p);
20544 else if (targ)
20545 return unify_inconsistency (explain_p, parm, targ, arg);
20547 else
20549 /* If PARM is `const T' and ARG is only `int', we don't have
20550 a match unless we are allowing additional qualification.
20551 If ARG is `const int' and PARM is just `T' that's OK;
20552 that binds `const int' to `T'. */
20553 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
20554 arg, parm))
20555 return unify_cv_qual_mismatch (explain_p, parm, arg);
20557 /* Consider the case where ARG is `const volatile int' and
20558 PARM is `const T'. Then, T should be `volatile int'. */
20559 arg = cp_build_qualified_type_real
20560 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
20561 if (arg == error_mark_node)
20562 return unify_invalid (explain_p);
20564 /* Simple cases: Value already set, does match or doesn't. */
20565 if (targ != NULL_TREE && same_type_p (targ, arg))
20566 return unify_success (explain_p);
20567 else if (targ)
20568 return unify_inconsistency (explain_p, parm, targ, arg);
20570 /* Make sure that ARG is not a variable-sized array. (Note
20571 that were talking about variable-sized arrays (like
20572 `int[n]'), rather than arrays of unknown size (like
20573 `int[]').) We'll get very confused by such a type since
20574 the bound of the array is not constant, and therefore
20575 not mangleable. Besides, such types are not allowed in
20576 ISO C++, so we can do as we please here. We do allow
20577 them for 'auto' deduction, since that isn't ABI-exposed. */
20578 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
20579 return unify_vla_arg (explain_p, arg);
20581 /* Strip typedefs as in convert_template_argument. */
20582 arg = canonicalize_type_argument (arg, tf_none);
20585 /* If ARG is a parameter pack or an expansion, we cannot unify
20586 against it unless PARM is also a parameter pack. */
20587 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20588 && !template_parameter_pack_p (parm))
20589 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20591 /* If the argument deduction results is a METHOD_TYPE,
20592 then there is a problem.
20593 METHOD_TYPE doesn't map to any real C++ type the result of
20594 the deduction can not be of that type. */
20595 if (TREE_CODE (arg) == METHOD_TYPE)
20596 return unify_method_type_error (explain_p, arg);
20598 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20599 return unify_success (explain_p);
20601 case TEMPLATE_PARM_INDEX:
20602 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20603 if (error_operand_p (tparm))
20604 return unify_invalid (explain_p);
20606 if (TEMPLATE_PARM_LEVEL (parm)
20607 != template_decl_level (tparm))
20609 /* The PARM is not one we're trying to unify. Just check
20610 to see if it matches ARG. */
20611 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
20612 && cp_tree_equal (parm, arg));
20613 if (result)
20614 unify_expression_unequal (explain_p, parm, arg);
20615 return result;
20618 idx = TEMPLATE_PARM_IDX (parm);
20619 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20621 if (targ)
20623 int x = !cp_tree_equal (targ, arg);
20624 if (x)
20625 unify_inconsistency (explain_p, parm, targ, arg);
20626 return x;
20629 /* [temp.deduct.type] If, in the declaration of a function template
20630 with a non-type template-parameter, the non-type
20631 template-parameter is used in an expression in the function
20632 parameter-list and, if the corresponding template-argument is
20633 deduced, the template-argument type shall match the type of the
20634 template-parameter exactly, except that a template-argument
20635 deduced from an array bound may be of any integral type.
20636 The non-type parameter might use already deduced type parameters. */
20637 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
20638 if (tree a = type_uses_auto (tparm))
20640 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
20641 if (tparm == error_mark_node)
20642 return 1;
20645 if (!TREE_TYPE (arg))
20646 /* Template-parameter dependent expression. Just accept it for now.
20647 It will later be processed in convert_template_argument. */
20649 else if (same_type_p (non_reference (TREE_TYPE (arg)),
20650 non_reference (tparm)))
20651 /* OK */;
20652 else if ((strict & UNIFY_ALLOW_INTEGER)
20653 && CP_INTEGRAL_TYPE_P (tparm))
20654 /* Convert the ARG to the type of PARM; the deduced non-type
20655 template argument must exactly match the types of the
20656 corresponding parameter. */
20657 arg = fold (build_nop (tparm, arg));
20658 else if (uses_template_parms (tparm))
20660 /* We haven't deduced the type of this parameter yet. */
20661 if (cxx_dialect >= cxx1z
20662 /* We deduce from array bounds in try_array_deduction. */
20663 && !(strict & UNIFY_ALLOW_INTEGER))
20665 /* Deduce it from the non-type argument. */
20666 tree atype = TREE_TYPE (arg);
20667 RECUR_AND_CHECK_FAILURE (tparms, targs,
20668 tparm, atype,
20669 UNIFY_ALLOW_NONE, explain_p);
20671 else
20672 /* Try again later. */
20673 return unify_success (explain_p);
20675 else
20676 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
20678 /* If ARG is a parameter pack or an expansion, we cannot unify
20679 against it unless PARM is also a parameter pack. */
20680 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20681 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
20682 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20685 bool removed_attr = false;
20686 arg = strip_typedefs_expr (arg, &removed_attr);
20688 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20689 return unify_success (explain_p);
20691 case PTRMEM_CST:
20693 /* A pointer-to-member constant can be unified only with
20694 another constant. */
20695 if (TREE_CODE (arg) != PTRMEM_CST)
20696 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
20698 /* Just unify the class member. It would be useless (and possibly
20699 wrong, depending on the strict flags) to unify also
20700 PTRMEM_CST_CLASS, because we want to be sure that both parm and
20701 arg refer to the same variable, even if through different
20702 classes. For instance:
20704 struct A { int x; };
20705 struct B : A { };
20707 Unification of &A::x and &B::x must succeed. */
20708 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
20709 PTRMEM_CST_MEMBER (arg), strict, explain_p);
20712 case POINTER_TYPE:
20714 if (!TYPE_PTR_P (arg))
20715 return unify_type_mismatch (explain_p, parm, arg);
20717 /* [temp.deduct.call]
20719 A can be another pointer or pointer to member type that can
20720 be converted to the deduced A via a qualification
20721 conversion (_conv.qual_).
20723 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
20724 This will allow for additional cv-qualification of the
20725 pointed-to types if appropriate. */
20727 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
20728 /* The derived-to-base conversion only persists through one
20729 level of pointers. */
20730 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
20732 return unify (tparms, targs, TREE_TYPE (parm),
20733 TREE_TYPE (arg), strict, explain_p);
20736 case REFERENCE_TYPE:
20737 if (TREE_CODE (arg) != REFERENCE_TYPE)
20738 return unify_type_mismatch (explain_p, parm, arg);
20739 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
20740 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
20742 case ARRAY_TYPE:
20743 if (TREE_CODE (arg) != ARRAY_TYPE)
20744 return unify_type_mismatch (explain_p, parm, arg);
20745 if ((TYPE_DOMAIN (parm) == NULL_TREE)
20746 != (TYPE_DOMAIN (arg) == NULL_TREE))
20747 return unify_type_mismatch (explain_p, parm, arg);
20748 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
20749 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
20750 if (TYPE_DOMAIN (parm) != NULL_TREE)
20751 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20752 TYPE_DOMAIN (arg), explain_p);
20753 return unify_success (explain_p);
20755 case REAL_TYPE:
20756 case COMPLEX_TYPE:
20757 case VECTOR_TYPE:
20758 case INTEGER_TYPE:
20759 case BOOLEAN_TYPE:
20760 case ENUMERAL_TYPE:
20761 case VOID_TYPE:
20762 case NULLPTR_TYPE:
20763 if (TREE_CODE (arg) != TREE_CODE (parm))
20764 return unify_type_mismatch (explain_p, parm, arg);
20766 /* We have already checked cv-qualification at the top of the
20767 function. */
20768 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
20769 return unify_type_mismatch (explain_p, parm, arg);
20771 /* As far as unification is concerned, this wins. Later checks
20772 will invalidate it if necessary. */
20773 return unify_success (explain_p);
20775 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
20776 /* Type INTEGER_CST can come from ordinary constant template args. */
20777 case INTEGER_CST:
20778 while (CONVERT_EXPR_P (arg))
20779 arg = TREE_OPERAND (arg, 0);
20781 if (TREE_CODE (arg) != INTEGER_CST)
20782 return unify_template_argument_mismatch (explain_p, parm, arg);
20783 return (tree_int_cst_equal (parm, arg)
20784 ? unify_success (explain_p)
20785 : unify_template_argument_mismatch (explain_p, parm, arg));
20787 case TREE_VEC:
20789 int i, len, argslen;
20790 int parm_variadic_p = 0;
20792 if (TREE_CODE (arg) != TREE_VEC)
20793 return unify_template_argument_mismatch (explain_p, parm, arg);
20795 len = TREE_VEC_LENGTH (parm);
20796 argslen = TREE_VEC_LENGTH (arg);
20798 /* Check for pack expansions in the parameters. */
20799 for (i = 0; i < len; ++i)
20801 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
20803 if (i == len - 1)
20804 /* We can unify against something with a trailing
20805 parameter pack. */
20806 parm_variadic_p = 1;
20807 else
20808 /* [temp.deduct.type]/9: If the template argument list of
20809 P contains a pack expansion that is not the last
20810 template argument, the entire template argument list
20811 is a non-deduced context. */
20812 return unify_success (explain_p);
20816 /* If we don't have enough arguments to satisfy the parameters
20817 (not counting the pack expression at the end), or we have
20818 too many arguments for a parameter list that doesn't end in
20819 a pack expression, we can't unify. */
20820 if (parm_variadic_p
20821 ? argslen < len - parm_variadic_p
20822 : argslen != len)
20823 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
20825 /* Unify all of the parameters that precede the (optional)
20826 pack expression. */
20827 for (i = 0; i < len - parm_variadic_p; ++i)
20829 RECUR_AND_CHECK_FAILURE (tparms, targs,
20830 TREE_VEC_ELT (parm, i),
20831 TREE_VEC_ELT (arg, i),
20832 UNIFY_ALLOW_NONE, explain_p);
20834 if (parm_variadic_p)
20835 return unify_pack_expansion (tparms, targs, parm, arg,
20836 DEDUCE_EXACT,
20837 /*subr=*/true, explain_p);
20838 return unify_success (explain_p);
20841 case RECORD_TYPE:
20842 case UNION_TYPE:
20843 if (TREE_CODE (arg) != TREE_CODE (parm))
20844 return unify_type_mismatch (explain_p, parm, arg);
20846 if (TYPE_PTRMEMFUNC_P (parm))
20848 if (!TYPE_PTRMEMFUNC_P (arg))
20849 return unify_type_mismatch (explain_p, parm, arg);
20851 return unify (tparms, targs,
20852 TYPE_PTRMEMFUNC_FN_TYPE (parm),
20853 TYPE_PTRMEMFUNC_FN_TYPE (arg),
20854 strict, explain_p);
20856 else if (TYPE_PTRMEMFUNC_P (arg))
20857 return unify_type_mismatch (explain_p, parm, arg);
20859 if (CLASSTYPE_TEMPLATE_INFO (parm))
20861 tree t = NULL_TREE;
20863 if (strict_in & UNIFY_ALLOW_DERIVED)
20865 /* First, we try to unify the PARM and ARG directly. */
20866 t = try_class_unification (tparms, targs,
20867 parm, arg, explain_p);
20869 if (!t)
20871 /* Fallback to the special case allowed in
20872 [temp.deduct.call]:
20874 If P is a class, and P has the form
20875 template-id, then A can be a derived class of
20876 the deduced A. Likewise, if P is a pointer to
20877 a class of the form template-id, A can be a
20878 pointer to a derived class pointed to by the
20879 deduced A. */
20880 enum template_base_result r;
20881 r = get_template_base (tparms, targs, parm, arg,
20882 explain_p, &t);
20884 if (!t)
20886 /* Don't give the derived diagnostic if we're
20887 already dealing with the same template. */
20888 bool same_template
20889 = (CLASSTYPE_TEMPLATE_INFO (arg)
20890 && (CLASSTYPE_TI_TEMPLATE (parm)
20891 == CLASSTYPE_TI_TEMPLATE (arg)));
20892 return unify_no_common_base (explain_p && !same_template,
20893 r, parm, arg);
20897 else if (CLASSTYPE_TEMPLATE_INFO (arg)
20898 && (CLASSTYPE_TI_TEMPLATE (parm)
20899 == CLASSTYPE_TI_TEMPLATE (arg)))
20900 /* Perhaps PARM is something like S<U> and ARG is S<int>.
20901 Then, we should unify `int' and `U'. */
20902 t = arg;
20903 else
20904 /* There's no chance of unification succeeding. */
20905 return unify_type_mismatch (explain_p, parm, arg);
20907 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
20908 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
20910 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
20911 return unify_type_mismatch (explain_p, parm, arg);
20912 return unify_success (explain_p);
20914 case METHOD_TYPE:
20915 case FUNCTION_TYPE:
20917 unsigned int nargs;
20918 tree *args;
20919 tree a;
20920 unsigned int i;
20922 if (TREE_CODE (arg) != TREE_CODE (parm))
20923 return unify_type_mismatch (explain_p, parm, arg);
20925 /* CV qualifications for methods can never be deduced, they must
20926 match exactly. We need to check them explicitly here,
20927 because type_unification_real treats them as any other
20928 cv-qualified parameter. */
20929 if (TREE_CODE (parm) == METHOD_TYPE
20930 && (!check_cv_quals_for_unify
20931 (UNIFY_ALLOW_NONE,
20932 class_of_this_parm (arg),
20933 class_of_this_parm (parm))))
20934 return unify_cv_qual_mismatch (explain_p, parm, arg);
20935 if (TREE_CODE (arg) == FUNCTION_TYPE
20936 && type_memfn_quals (parm) != type_memfn_quals (arg))
20937 return unify_cv_qual_mismatch (explain_p, parm, arg);
20938 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
20939 return unify_type_mismatch (explain_p, parm, arg);
20941 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
20942 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
20944 nargs = list_length (TYPE_ARG_TYPES (arg));
20945 args = XALLOCAVEC (tree, nargs);
20946 for (a = TYPE_ARG_TYPES (arg), i = 0;
20947 a != NULL_TREE && a != void_list_node;
20948 a = TREE_CHAIN (a), ++i)
20949 args[i] = TREE_VALUE (a);
20950 nargs = i;
20952 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
20953 args, nargs, 1, DEDUCE_EXACT,
20954 LOOKUP_NORMAL, NULL, explain_p))
20955 return 1;
20957 if (flag_noexcept_type)
20959 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
20960 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
20961 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
20962 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
20963 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
20964 && uses_template_parms (TREE_PURPOSE (pspec)))
20965 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
20966 TREE_PURPOSE (aspec),
20967 UNIFY_ALLOW_NONE, explain_p);
20968 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
20969 return unify_type_mismatch (explain_p, parm, arg);
20972 return 0;
20975 case OFFSET_TYPE:
20976 /* Unify a pointer to member with a pointer to member function, which
20977 deduces the type of the member as a function type. */
20978 if (TYPE_PTRMEMFUNC_P (arg))
20980 /* Check top-level cv qualifiers */
20981 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
20982 return unify_cv_qual_mismatch (explain_p, parm, arg);
20984 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
20985 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
20986 UNIFY_ALLOW_NONE, explain_p);
20988 /* Determine the type of the function we are unifying against. */
20989 tree fntype = static_fn_type (arg);
20991 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
20994 if (TREE_CODE (arg) != OFFSET_TYPE)
20995 return unify_type_mismatch (explain_p, parm, arg);
20996 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
20997 TYPE_OFFSET_BASETYPE (arg),
20998 UNIFY_ALLOW_NONE, explain_p);
20999 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21000 strict, explain_p);
21002 case CONST_DECL:
21003 if (DECL_TEMPLATE_PARM_P (parm))
21004 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
21005 if (arg != scalar_constant_value (parm))
21006 return unify_template_argument_mismatch (explain_p, parm, arg);
21007 return unify_success (explain_p);
21009 case FIELD_DECL:
21010 case TEMPLATE_DECL:
21011 /* Matched cases are handled by the ARG == PARM test above. */
21012 return unify_template_argument_mismatch (explain_p, parm, arg);
21014 case VAR_DECL:
21015 /* We might get a variable as a non-type template argument in parm if the
21016 corresponding parameter is type-dependent. Make any necessary
21017 adjustments based on whether arg is a reference. */
21018 if (CONSTANT_CLASS_P (arg))
21019 parm = fold_non_dependent_expr (parm);
21020 else if (REFERENCE_REF_P (arg))
21022 tree sub = TREE_OPERAND (arg, 0);
21023 STRIP_NOPS (sub);
21024 if (TREE_CODE (sub) == ADDR_EXPR)
21025 arg = TREE_OPERAND (sub, 0);
21027 /* Now use the normal expression code to check whether they match. */
21028 goto expr;
21030 case TYPE_ARGUMENT_PACK:
21031 case NONTYPE_ARGUMENT_PACK:
21032 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
21033 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
21035 case TYPEOF_TYPE:
21036 case DECLTYPE_TYPE:
21037 case UNDERLYING_TYPE:
21038 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
21039 or UNDERLYING_TYPE nodes. */
21040 return unify_success (explain_p);
21042 case ERROR_MARK:
21043 /* Unification fails if we hit an error node. */
21044 return unify_invalid (explain_p);
21046 case INDIRECT_REF:
21047 if (REFERENCE_REF_P (parm))
21049 bool pexp = PACK_EXPANSION_P (arg);
21050 if (pexp)
21051 arg = PACK_EXPANSION_PATTERN (arg);
21052 if (REFERENCE_REF_P (arg))
21053 arg = TREE_OPERAND (arg, 0);
21054 if (pexp)
21055 arg = make_pack_expansion (arg);
21056 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
21057 strict, explain_p);
21059 /* FALLTHRU */
21061 default:
21062 /* An unresolved overload is a nondeduced context. */
21063 if (is_overloaded_fn (parm) || type_unknown_p (parm))
21064 return unify_success (explain_p);
21065 gcc_assert (EXPR_P (parm) || TREE_CODE (parm) == TRAIT_EXPR);
21066 expr:
21067 /* We must be looking at an expression. This can happen with
21068 something like:
21070 template <int I>
21071 void foo(S<I>, S<I + 2>);
21073 This is a "nondeduced context":
21075 [deduct.type]
21077 The nondeduced contexts are:
21079 --A type that is a template-id in which one or more of
21080 the template-arguments is an expression that references
21081 a template-parameter.
21083 In these cases, we assume deduction succeeded, but don't
21084 actually infer any unifications. */
21086 if (!uses_template_parms (parm)
21087 && !template_args_equal (parm, arg))
21088 return unify_expression_unequal (explain_p, parm, arg);
21089 else
21090 return unify_success (explain_p);
21093 #undef RECUR_AND_CHECK_FAILURE
21095 /* Note that DECL can be defined in this translation unit, if
21096 required. */
21098 static void
21099 mark_definable (tree decl)
21101 tree clone;
21102 DECL_NOT_REALLY_EXTERN (decl) = 1;
21103 FOR_EACH_CLONE (clone, decl)
21104 DECL_NOT_REALLY_EXTERN (clone) = 1;
21107 /* Called if RESULT is explicitly instantiated, or is a member of an
21108 explicitly instantiated class. */
21110 void
21111 mark_decl_instantiated (tree result, int extern_p)
21113 SET_DECL_EXPLICIT_INSTANTIATION (result);
21115 /* If this entity has already been written out, it's too late to
21116 make any modifications. */
21117 if (TREE_ASM_WRITTEN (result))
21118 return;
21120 /* For anonymous namespace we don't need to do anything. */
21121 if (decl_anon_ns_mem_p (result))
21123 gcc_assert (!TREE_PUBLIC (result));
21124 return;
21127 if (TREE_CODE (result) != FUNCTION_DECL)
21128 /* The TREE_PUBLIC flag for function declarations will have been
21129 set correctly by tsubst. */
21130 TREE_PUBLIC (result) = 1;
21132 /* This might have been set by an earlier implicit instantiation. */
21133 DECL_COMDAT (result) = 0;
21135 if (extern_p)
21136 DECL_NOT_REALLY_EXTERN (result) = 0;
21137 else
21139 mark_definable (result);
21140 mark_needed (result);
21141 /* Always make artificials weak. */
21142 if (DECL_ARTIFICIAL (result) && flag_weak)
21143 comdat_linkage (result);
21144 /* For WIN32 we also want to put explicit instantiations in
21145 linkonce sections. */
21146 else if (TREE_PUBLIC (result))
21147 maybe_make_one_only (result);
21150 /* If EXTERN_P, then this function will not be emitted -- unless
21151 followed by an explicit instantiation, at which point its linkage
21152 will be adjusted. If !EXTERN_P, then this function will be
21153 emitted here. In neither circumstance do we want
21154 import_export_decl to adjust the linkage. */
21155 DECL_INTERFACE_KNOWN (result) = 1;
21158 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
21159 important template arguments. If any are missing, we check whether
21160 they're important by using error_mark_node for substituting into any
21161 args that were used for partial ordering (the ones between ARGS and END)
21162 and seeing if it bubbles up. */
21164 static bool
21165 check_undeduced_parms (tree targs, tree args, tree end)
21167 bool found = false;
21168 int i;
21169 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
21170 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
21172 found = true;
21173 TREE_VEC_ELT (targs, i) = error_mark_node;
21175 if (found)
21177 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
21178 if (substed == error_mark_node)
21179 return true;
21181 return false;
21184 /* Given two function templates PAT1 and PAT2, return:
21186 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
21187 -1 if PAT2 is more specialized than PAT1.
21188 0 if neither is more specialized.
21190 LEN indicates the number of parameters we should consider
21191 (defaulted parameters should not be considered).
21193 The 1998 std underspecified function template partial ordering, and
21194 DR214 addresses the issue. We take pairs of arguments, one from
21195 each of the templates, and deduce them against each other. One of
21196 the templates will be more specialized if all the *other*
21197 template's arguments deduce against its arguments and at least one
21198 of its arguments *does* *not* deduce against the other template's
21199 corresponding argument. Deduction is done as for class templates.
21200 The arguments used in deduction have reference and top level cv
21201 qualifiers removed. Iff both arguments were originally reference
21202 types *and* deduction succeeds in both directions, an lvalue reference
21203 wins against an rvalue reference and otherwise the template
21204 with the more cv-qualified argument wins for that pairing (if
21205 neither is more cv-qualified, they both are equal). Unlike regular
21206 deduction, after all the arguments have been deduced in this way,
21207 we do *not* verify the deduced template argument values can be
21208 substituted into non-deduced contexts.
21210 The logic can be a bit confusing here, because we look at deduce1 and
21211 targs1 to see if pat2 is at least as specialized, and vice versa; if we
21212 can find template arguments for pat1 to make arg1 look like arg2, that
21213 means that arg2 is at least as specialized as arg1. */
21216 more_specialized_fn (tree pat1, tree pat2, int len)
21218 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
21219 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
21220 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
21221 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
21222 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
21223 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
21224 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
21225 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
21226 tree origs1, origs2;
21227 bool lose1 = false;
21228 bool lose2 = false;
21230 /* Remove the this parameter from non-static member functions. If
21231 one is a non-static member function and the other is not a static
21232 member function, remove the first parameter from that function
21233 also. This situation occurs for operator functions where we
21234 locate both a member function (with this pointer) and non-member
21235 operator (with explicit first operand). */
21236 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
21238 len--; /* LEN is the number of significant arguments for DECL1 */
21239 args1 = TREE_CHAIN (args1);
21240 if (!DECL_STATIC_FUNCTION_P (decl2))
21241 args2 = TREE_CHAIN (args2);
21243 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
21245 args2 = TREE_CHAIN (args2);
21246 if (!DECL_STATIC_FUNCTION_P (decl1))
21248 len--;
21249 args1 = TREE_CHAIN (args1);
21253 /* If only one is a conversion operator, they are unordered. */
21254 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
21255 return 0;
21257 /* Consider the return type for a conversion function */
21258 if (DECL_CONV_FN_P (decl1))
21260 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
21261 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
21262 len++;
21265 processing_template_decl++;
21267 origs1 = args1;
21268 origs2 = args2;
21270 while (len--
21271 /* Stop when an ellipsis is seen. */
21272 && args1 != NULL_TREE && args2 != NULL_TREE)
21274 tree arg1 = TREE_VALUE (args1);
21275 tree arg2 = TREE_VALUE (args2);
21276 int deduce1, deduce2;
21277 int quals1 = -1;
21278 int quals2 = -1;
21279 int ref1 = 0;
21280 int ref2 = 0;
21282 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21283 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21285 /* When both arguments are pack expansions, we need only
21286 unify the patterns themselves. */
21287 arg1 = PACK_EXPANSION_PATTERN (arg1);
21288 arg2 = PACK_EXPANSION_PATTERN (arg2);
21290 /* This is the last comparison we need to do. */
21291 len = 0;
21294 /* DR 1847: If a particular P contains no template-parameters that
21295 participate in template argument deduction, that P is not used to
21296 determine the ordering. */
21297 if (!uses_deducible_template_parms (arg1)
21298 && !uses_deducible_template_parms (arg2))
21299 goto next;
21301 if (TREE_CODE (arg1) == REFERENCE_TYPE)
21303 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
21304 arg1 = TREE_TYPE (arg1);
21305 quals1 = cp_type_quals (arg1);
21308 if (TREE_CODE (arg2) == REFERENCE_TYPE)
21310 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
21311 arg2 = TREE_TYPE (arg2);
21312 quals2 = cp_type_quals (arg2);
21315 arg1 = TYPE_MAIN_VARIANT (arg1);
21316 arg2 = TYPE_MAIN_VARIANT (arg2);
21318 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
21320 int i, len2 = remaining_arguments (args2);
21321 tree parmvec = make_tree_vec (1);
21322 tree argvec = make_tree_vec (len2);
21323 tree ta = args2;
21325 /* Setup the parameter vector, which contains only ARG1. */
21326 TREE_VEC_ELT (parmvec, 0) = arg1;
21328 /* Setup the argument vector, which contains the remaining
21329 arguments. */
21330 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
21331 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21333 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
21334 argvec, DEDUCE_EXACT,
21335 /*subr=*/true, /*explain_p=*/false)
21336 == 0);
21338 /* We cannot deduce in the other direction, because ARG1 is
21339 a pack expansion but ARG2 is not. */
21340 deduce2 = 0;
21342 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21344 int i, len1 = remaining_arguments (args1);
21345 tree parmvec = make_tree_vec (1);
21346 tree argvec = make_tree_vec (len1);
21347 tree ta = args1;
21349 /* Setup the parameter vector, which contains only ARG1. */
21350 TREE_VEC_ELT (parmvec, 0) = arg2;
21352 /* Setup the argument vector, which contains the remaining
21353 arguments. */
21354 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
21355 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21357 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
21358 argvec, DEDUCE_EXACT,
21359 /*subr=*/true, /*explain_p=*/false)
21360 == 0);
21362 /* We cannot deduce in the other direction, because ARG2 is
21363 a pack expansion but ARG1 is not.*/
21364 deduce1 = 0;
21367 else
21369 /* The normal case, where neither argument is a pack
21370 expansion. */
21371 deduce1 = (unify (tparms1, targs1, arg1, arg2,
21372 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21373 == 0);
21374 deduce2 = (unify (tparms2, targs2, arg2, arg1,
21375 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21376 == 0);
21379 /* If we couldn't deduce arguments for tparms1 to make arg1 match
21380 arg2, then arg2 is not as specialized as arg1. */
21381 if (!deduce1)
21382 lose2 = true;
21383 if (!deduce2)
21384 lose1 = true;
21386 /* "If, for a given type, deduction succeeds in both directions
21387 (i.e., the types are identical after the transformations above)
21388 and both P and A were reference types (before being replaced with
21389 the type referred to above):
21390 - if the type from the argument template was an lvalue reference and
21391 the type from the parameter template was not, the argument type is
21392 considered to be more specialized than the other; otherwise,
21393 - if the type from the argument template is more cv-qualified
21394 than the type from the parameter template (as described above),
21395 the argument type is considered to be more specialized than the other;
21396 otherwise,
21397 - neither type is more specialized than the other." */
21399 if (deduce1 && deduce2)
21401 if (ref1 && ref2 && ref1 != ref2)
21403 if (ref1 > ref2)
21404 lose1 = true;
21405 else
21406 lose2 = true;
21408 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
21410 if ((quals1 & quals2) == quals2)
21411 lose2 = true;
21412 if ((quals1 & quals2) == quals1)
21413 lose1 = true;
21417 if (lose1 && lose2)
21418 /* We've failed to deduce something in either direction.
21419 These must be unordered. */
21420 break;
21422 next:
21424 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21425 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21426 /* We have already processed all of the arguments in our
21427 handing of the pack expansion type. */
21428 len = 0;
21430 args1 = TREE_CHAIN (args1);
21431 args2 = TREE_CHAIN (args2);
21434 /* "In most cases, all template parameters must have values in order for
21435 deduction to succeed, but for partial ordering purposes a template
21436 parameter may remain without a value provided it is not used in the
21437 types being used for partial ordering."
21439 Thus, if we are missing any of the targs1 we need to substitute into
21440 origs1, then pat2 is not as specialized as pat1. This can happen when
21441 there is a nondeduced context. */
21442 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
21443 lose2 = true;
21444 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
21445 lose1 = true;
21447 processing_template_decl--;
21449 /* If both deductions succeed, the partial ordering selects the more
21450 constrained template. */
21451 if (!lose1 && !lose2)
21453 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
21454 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
21455 lose1 = !subsumes_constraints (c1, c2);
21456 lose2 = !subsumes_constraints (c2, c1);
21459 /* All things being equal, if the next argument is a pack expansion
21460 for one function but not for the other, prefer the
21461 non-variadic function. FIXME this is bogus; see c++/41958. */
21462 if (lose1 == lose2
21463 && args1 && TREE_VALUE (args1)
21464 && args2 && TREE_VALUE (args2))
21466 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
21467 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
21470 if (lose1 == lose2)
21471 return 0;
21472 else if (!lose1)
21473 return 1;
21474 else
21475 return -1;
21478 /* Determine which of two partial specializations of TMPL is more
21479 specialized.
21481 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
21482 to the first partial specialization. The TREE_PURPOSE is the
21483 innermost set of template parameters for the partial
21484 specialization. PAT2 is similar, but for the second template.
21486 Return 1 if the first partial specialization is more specialized;
21487 -1 if the second is more specialized; 0 if neither is more
21488 specialized.
21490 See [temp.class.order] for information about determining which of
21491 two templates is more specialized. */
21493 static int
21494 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
21496 tree targs;
21497 int winner = 0;
21498 bool any_deductions = false;
21500 tree tmpl1 = TREE_VALUE (pat1);
21501 tree tmpl2 = TREE_VALUE (pat2);
21502 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
21503 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
21505 /* Just like what happens for functions, if we are ordering between
21506 different template specializations, we may encounter dependent
21507 types in the arguments, and we need our dependency check functions
21508 to behave correctly. */
21509 ++processing_template_decl;
21510 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
21511 if (targs)
21513 --winner;
21514 any_deductions = true;
21517 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
21518 if (targs)
21520 ++winner;
21521 any_deductions = true;
21523 --processing_template_decl;
21525 /* If both deductions succeed, the partial ordering selects the more
21526 constrained template. */
21527 if (!winner && any_deductions)
21528 return more_constrained (tmpl1, tmpl2);
21530 /* In the case of a tie where at least one of the templates
21531 has a parameter pack at the end, the template with the most
21532 non-packed parameters wins. */
21533 if (winner == 0
21534 && any_deductions
21535 && (template_args_variadic_p (TREE_PURPOSE (pat1))
21536 || template_args_variadic_p (TREE_PURPOSE (pat2))))
21538 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
21539 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
21540 int len1 = TREE_VEC_LENGTH (args1);
21541 int len2 = TREE_VEC_LENGTH (args2);
21543 /* We don't count the pack expansion at the end. */
21544 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
21545 --len1;
21546 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
21547 --len2;
21549 if (len1 > len2)
21550 return 1;
21551 else if (len1 < len2)
21552 return -1;
21555 return winner;
21558 /* Return the template arguments that will produce the function signature
21559 DECL from the function template FN, with the explicit template
21560 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
21561 also match. Return NULL_TREE if no satisfactory arguments could be
21562 found. */
21564 static tree
21565 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
21567 int ntparms = DECL_NTPARMS (fn);
21568 tree targs = make_tree_vec (ntparms);
21569 tree decl_type = TREE_TYPE (decl);
21570 tree decl_arg_types;
21571 tree *args;
21572 unsigned int nargs, ix;
21573 tree arg;
21575 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
21577 /* Never do unification on the 'this' parameter. */
21578 decl_arg_types = skip_artificial_parms_for (decl,
21579 TYPE_ARG_TYPES (decl_type));
21581 nargs = list_length (decl_arg_types);
21582 args = XALLOCAVEC (tree, nargs);
21583 for (arg = decl_arg_types, ix = 0;
21584 arg != NULL_TREE && arg != void_list_node;
21585 arg = TREE_CHAIN (arg), ++ix)
21586 args[ix] = TREE_VALUE (arg);
21588 if (fn_type_unification (fn, explicit_args, targs,
21589 args, ix,
21590 (check_rettype || DECL_CONV_FN_P (fn)
21591 ? TREE_TYPE (decl_type) : NULL_TREE),
21592 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
21593 /*decltype*/false)
21594 == error_mark_node)
21595 return NULL_TREE;
21597 return targs;
21600 /* Return the innermost template arguments that, when applied to a partial
21601 specialization SPEC_TMPL of TMPL, yield the ARGS.
21603 For example, suppose we have:
21605 template <class T, class U> struct S {};
21606 template <class T> struct S<T*, int> {};
21608 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
21609 partial specialization and the ARGS will be {double*, int}. The resulting
21610 vector will be {double}, indicating that `T' is bound to `double'. */
21612 static tree
21613 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
21615 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
21616 tree spec_args
21617 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
21618 int i, ntparms = TREE_VEC_LENGTH (tparms);
21619 tree deduced_args;
21620 tree innermost_deduced_args;
21622 innermost_deduced_args = make_tree_vec (ntparms);
21623 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
21625 deduced_args = copy_node (args);
21626 SET_TMPL_ARGS_LEVEL (deduced_args,
21627 TMPL_ARGS_DEPTH (deduced_args),
21628 innermost_deduced_args);
21630 else
21631 deduced_args = innermost_deduced_args;
21633 bool tried_array_deduction = (cxx_dialect < cxx1z);
21634 again:
21635 if (unify (tparms, deduced_args,
21636 INNERMOST_TEMPLATE_ARGS (spec_args),
21637 INNERMOST_TEMPLATE_ARGS (args),
21638 UNIFY_ALLOW_NONE, /*explain_p=*/false))
21639 return NULL_TREE;
21641 for (i = 0; i < ntparms; ++i)
21642 if (! TREE_VEC_ELT (innermost_deduced_args, i))
21644 if (!tried_array_deduction)
21646 try_array_deduction (tparms, innermost_deduced_args,
21647 INNERMOST_TEMPLATE_ARGS (spec_args));
21648 tried_array_deduction = true;
21649 if (TREE_VEC_ELT (innermost_deduced_args, i))
21650 goto again;
21652 return NULL_TREE;
21655 tree tinst = build_tree_list (spec_tmpl, deduced_args);
21656 if (!push_tinst_level (tinst))
21658 excessive_deduction_depth = true;
21659 return NULL_TREE;
21662 /* Verify that nondeduced template arguments agree with the type
21663 obtained from argument deduction.
21665 For example:
21667 struct A { typedef int X; };
21668 template <class T, class U> struct C {};
21669 template <class T> struct C<T, typename T::X> {};
21671 Then with the instantiation `C<A, int>', we can deduce that
21672 `T' is `A' but unify () does not check whether `typename T::X'
21673 is `int'. */
21674 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
21676 if (spec_args != error_mark_node)
21677 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
21678 INNERMOST_TEMPLATE_ARGS (spec_args),
21679 tmpl, tf_none, false, false);
21681 pop_tinst_level ();
21683 if (spec_args == error_mark_node
21684 /* We only need to check the innermost arguments; the other
21685 arguments will always agree. */
21686 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
21687 INNERMOST_TEMPLATE_ARGS (args)))
21688 return NULL_TREE;
21690 /* Now that we have bindings for all of the template arguments,
21691 ensure that the arguments deduced for the template template
21692 parameters have compatible template parameter lists. See the use
21693 of template_template_parm_bindings_ok_p in fn_type_unification
21694 for more information. */
21695 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
21696 return NULL_TREE;
21698 return deduced_args;
21701 // Compare two function templates T1 and T2 by deducing bindings
21702 // from one against the other. If both deductions succeed, compare
21703 // constraints to see which is more constrained.
21704 static int
21705 more_specialized_inst (tree t1, tree t2)
21707 int fate = 0;
21708 int count = 0;
21710 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
21712 --fate;
21713 ++count;
21716 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
21718 ++fate;
21719 ++count;
21722 // If both deductions succeed, then one may be more constrained.
21723 if (count == 2 && fate == 0)
21724 fate = more_constrained (t1, t2);
21726 return fate;
21729 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
21730 Return the TREE_LIST node with the most specialized template, if
21731 any. If there is no most specialized template, the error_mark_node
21732 is returned.
21734 Note that this function does not look at, or modify, the
21735 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
21736 returned is one of the elements of INSTANTIATIONS, callers may
21737 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
21738 and retrieve it from the value returned. */
21740 tree
21741 most_specialized_instantiation (tree templates)
21743 tree fn, champ;
21745 ++processing_template_decl;
21747 champ = templates;
21748 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
21750 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
21751 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
21752 if (fate == -1)
21753 champ = fn;
21754 else if (!fate)
21756 /* Equally specialized, move to next function. If there
21757 is no next function, nothing's most specialized. */
21758 fn = TREE_CHAIN (fn);
21759 champ = fn;
21760 if (!fn)
21761 break;
21765 if (champ)
21766 /* Now verify that champ is better than everything earlier in the
21767 instantiation list. */
21768 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
21769 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
21771 champ = NULL_TREE;
21772 break;
21776 processing_template_decl--;
21778 if (!champ)
21779 return error_mark_node;
21781 return champ;
21784 /* If DECL is a specialization of some template, return the most
21785 general such template. Otherwise, returns NULL_TREE.
21787 For example, given:
21789 template <class T> struct S { template <class U> void f(U); };
21791 if TMPL is `template <class U> void S<int>::f(U)' this will return
21792 the full template. This function will not trace past partial
21793 specializations, however. For example, given in addition:
21795 template <class T> struct S<T*> { template <class U> void f(U); };
21797 if TMPL is `template <class U> void S<int*>::f(U)' this will return
21798 `template <class T> template <class U> S<T*>::f(U)'. */
21800 tree
21801 most_general_template (tree decl)
21803 if (TREE_CODE (decl) != TEMPLATE_DECL)
21805 if (tree tinfo = get_template_info (decl))
21806 decl = TI_TEMPLATE (tinfo);
21807 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
21808 template friend, or a FIELD_DECL for a capture pack. */
21809 if (TREE_CODE (decl) != TEMPLATE_DECL)
21810 return NULL_TREE;
21813 /* Look for more and more general templates. */
21814 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
21816 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
21817 (See cp-tree.h for details.) */
21818 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
21819 break;
21821 if (CLASS_TYPE_P (TREE_TYPE (decl))
21822 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
21823 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
21824 break;
21826 /* Stop if we run into an explicitly specialized class template. */
21827 if (!DECL_NAMESPACE_SCOPE_P (decl)
21828 && DECL_CONTEXT (decl)
21829 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
21830 break;
21832 decl = DECL_TI_TEMPLATE (decl);
21835 return decl;
21838 /* Return the most specialized of the template partial specializations
21839 which can produce TARGET, a specialization of some class or variable
21840 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
21841 a TEMPLATE_DECL node corresponding to the partial specialization, while
21842 the TREE_PURPOSE is the set of template arguments that must be
21843 substituted into the template pattern in order to generate TARGET.
21845 If the choice of partial specialization is ambiguous, a diagnostic
21846 is issued, and the error_mark_node is returned. If there are no
21847 partial specializations matching TARGET, then NULL_TREE is
21848 returned, indicating that the primary template should be used. */
21850 static tree
21851 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
21853 tree list = NULL_TREE;
21854 tree t;
21855 tree champ;
21856 int fate;
21857 bool ambiguous_p;
21858 tree outer_args = NULL_TREE;
21859 tree tmpl, args;
21861 if (TYPE_P (target))
21863 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
21864 tmpl = TI_TEMPLATE (tinfo);
21865 args = TI_ARGS (tinfo);
21867 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
21869 tmpl = TREE_OPERAND (target, 0);
21870 args = TREE_OPERAND (target, 1);
21872 else if (VAR_P (target))
21874 tree tinfo = DECL_TEMPLATE_INFO (target);
21875 tmpl = TI_TEMPLATE (tinfo);
21876 args = TI_ARGS (tinfo);
21878 else
21879 gcc_unreachable ();
21881 tree main_tmpl = most_general_template (tmpl);
21883 /* For determining which partial specialization to use, only the
21884 innermost args are interesting. */
21885 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
21887 outer_args = strip_innermost_template_args (args, 1);
21888 args = INNERMOST_TEMPLATE_ARGS (args);
21891 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
21893 tree spec_args;
21894 tree spec_tmpl = TREE_VALUE (t);
21896 if (outer_args)
21898 /* Substitute in the template args from the enclosing class. */
21899 ++processing_template_decl;
21900 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
21901 --processing_template_decl;
21904 if (spec_tmpl == error_mark_node)
21905 return error_mark_node;
21907 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
21908 if (spec_args)
21910 if (outer_args)
21911 spec_args = add_to_template_args (outer_args, spec_args);
21913 /* Keep the candidate only if the constraints are satisfied,
21914 or if we're not compiling with concepts. */
21915 if (!flag_concepts
21916 || constraints_satisfied_p (spec_tmpl, spec_args))
21918 list = tree_cons (spec_args, TREE_VALUE (t), list);
21919 TREE_TYPE (list) = TREE_TYPE (t);
21924 if (! list)
21925 return NULL_TREE;
21927 ambiguous_p = false;
21928 t = list;
21929 champ = t;
21930 t = TREE_CHAIN (t);
21931 for (; t; t = TREE_CHAIN (t))
21933 fate = more_specialized_partial_spec (tmpl, champ, t);
21934 if (fate == 1)
21936 else
21938 if (fate == 0)
21940 t = TREE_CHAIN (t);
21941 if (! t)
21943 ambiguous_p = true;
21944 break;
21947 champ = t;
21951 if (!ambiguous_p)
21952 for (t = list; t && t != champ; t = TREE_CHAIN (t))
21954 fate = more_specialized_partial_spec (tmpl, champ, t);
21955 if (fate != 1)
21957 ambiguous_p = true;
21958 break;
21962 if (ambiguous_p)
21964 const char *str;
21965 char *spaces = NULL;
21966 if (!(complain & tf_error))
21967 return error_mark_node;
21968 if (TYPE_P (target))
21969 error ("ambiguous template instantiation for %q#T", target);
21970 else
21971 error ("ambiguous template instantiation for %q#D", target);
21972 str = ngettext ("candidate is:", "candidates are:", list_length (list));
21973 for (t = list; t; t = TREE_CHAIN (t))
21975 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
21976 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
21977 "%s %#qS", spaces ? spaces : str, subst);
21978 spaces = spaces ? spaces : get_spaces (str);
21980 free (spaces);
21981 return error_mark_node;
21984 return champ;
21987 /* Explicitly instantiate DECL. */
21989 void
21990 do_decl_instantiation (tree decl, tree storage)
21992 tree result = NULL_TREE;
21993 int extern_p = 0;
21995 if (!decl || decl == error_mark_node)
21996 /* An error occurred, for which grokdeclarator has already issued
21997 an appropriate message. */
21998 return;
21999 else if (! DECL_LANG_SPECIFIC (decl))
22001 error ("explicit instantiation of non-template %q#D", decl);
22002 return;
22005 bool var_templ = (DECL_TEMPLATE_INFO (decl)
22006 && variable_template_p (DECL_TI_TEMPLATE (decl)));
22008 if (VAR_P (decl) && !var_templ)
22010 /* There is an asymmetry here in the way VAR_DECLs and
22011 FUNCTION_DECLs are handled by grokdeclarator. In the case of
22012 the latter, the DECL we get back will be marked as a
22013 template instantiation, and the appropriate
22014 DECL_TEMPLATE_INFO will be set up. This does not happen for
22015 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
22016 should handle VAR_DECLs as it currently handles
22017 FUNCTION_DECLs. */
22018 if (!DECL_CLASS_SCOPE_P (decl))
22020 error ("%qD is not a static data member of a class template", decl);
22021 return;
22023 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
22024 if (!result || !VAR_P (result))
22026 error ("no matching template for %qD found", decl);
22027 return;
22029 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
22031 error ("type %qT for explicit instantiation %qD does not match "
22032 "declared type %qT", TREE_TYPE (result), decl,
22033 TREE_TYPE (decl));
22034 return;
22037 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
22039 error ("explicit instantiation of %q#D", decl);
22040 return;
22042 else
22043 result = decl;
22045 /* Check for various error cases. Note that if the explicit
22046 instantiation is valid the RESULT will currently be marked as an
22047 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
22048 until we get here. */
22050 if (DECL_TEMPLATE_SPECIALIZATION (result))
22052 /* DR 259 [temp.spec].
22054 Both an explicit instantiation and a declaration of an explicit
22055 specialization shall not appear in a program unless the explicit
22056 instantiation follows a declaration of the explicit specialization.
22058 For a given set of template parameters, if an explicit
22059 instantiation of a template appears after a declaration of an
22060 explicit specialization for that template, the explicit
22061 instantiation has no effect. */
22062 return;
22064 else if (DECL_EXPLICIT_INSTANTIATION (result))
22066 /* [temp.spec]
22068 No program shall explicitly instantiate any template more
22069 than once.
22071 We check DECL_NOT_REALLY_EXTERN so as not to complain when
22072 the first instantiation was `extern' and the second is not,
22073 and EXTERN_P for the opposite case. */
22074 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
22075 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
22076 /* If an "extern" explicit instantiation follows an ordinary
22077 explicit instantiation, the template is instantiated. */
22078 if (extern_p)
22079 return;
22081 else if (!DECL_IMPLICIT_INSTANTIATION (result))
22083 error ("no matching template for %qD found", result);
22084 return;
22086 else if (!DECL_TEMPLATE_INFO (result))
22088 permerror (input_location, "explicit instantiation of non-template %q#D", result);
22089 return;
22092 if (storage == NULL_TREE)
22094 else if (storage == ridpointers[(int) RID_EXTERN])
22096 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
22097 pedwarn (input_location, OPT_Wpedantic,
22098 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
22099 "instantiations");
22100 extern_p = 1;
22102 else
22103 error ("storage class %qD applied to template instantiation", storage);
22105 check_explicit_instantiation_namespace (result);
22106 mark_decl_instantiated (result, extern_p);
22107 if (! extern_p)
22108 instantiate_decl (result, /*defer_ok=*/true,
22109 /*expl_inst_class_mem_p=*/false);
22112 static void
22113 mark_class_instantiated (tree t, int extern_p)
22115 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
22116 SET_CLASSTYPE_INTERFACE_KNOWN (t);
22117 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
22118 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
22119 if (! extern_p)
22121 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
22122 rest_of_type_compilation (t, 1);
22126 /* Called from do_type_instantiation through binding_table_foreach to
22127 do recursive instantiation for the type bound in ENTRY. */
22128 static void
22129 bt_instantiate_type_proc (binding_entry entry, void *data)
22131 tree storage = *(tree *) data;
22133 if (MAYBE_CLASS_TYPE_P (entry->type)
22134 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
22135 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
22138 /* Called from do_type_instantiation to instantiate a member
22139 (a member function or a static member variable) of an
22140 explicitly instantiated class template. */
22141 static void
22142 instantiate_class_member (tree decl, int extern_p)
22144 mark_decl_instantiated (decl, extern_p);
22145 if (! extern_p)
22146 instantiate_decl (decl, /*defer_ok=*/true,
22147 /*expl_inst_class_mem_p=*/true);
22150 /* Perform an explicit instantiation of template class T. STORAGE, if
22151 non-null, is the RID for extern, inline or static. COMPLAIN is
22152 nonzero if this is called from the parser, zero if called recursively,
22153 since the standard is unclear (as detailed below). */
22155 void
22156 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
22158 int extern_p = 0;
22159 int nomem_p = 0;
22160 int static_p = 0;
22161 int previous_instantiation_extern_p = 0;
22163 if (TREE_CODE (t) == TYPE_DECL)
22164 t = TREE_TYPE (t);
22166 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
22168 tree tmpl =
22169 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
22170 if (tmpl)
22171 error ("explicit instantiation of non-class template %qD", tmpl);
22172 else
22173 error ("explicit instantiation of non-template type %qT", t);
22174 return;
22177 complete_type (t);
22179 if (!COMPLETE_TYPE_P (t))
22181 if (complain & tf_error)
22182 error ("explicit instantiation of %q#T before definition of template",
22184 return;
22187 if (storage != NULL_TREE)
22189 if (!in_system_header_at (input_location))
22191 if (storage == ridpointers[(int) RID_EXTERN])
22193 if (cxx_dialect == cxx98)
22194 pedwarn (input_location, OPT_Wpedantic,
22195 "ISO C++ 1998 forbids the use of %<extern%> on "
22196 "explicit instantiations");
22198 else
22199 pedwarn (input_location, OPT_Wpedantic,
22200 "ISO C++ forbids the use of %qE"
22201 " on explicit instantiations", storage);
22204 if (storage == ridpointers[(int) RID_INLINE])
22205 nomem_p = 1;
22206 else if (storage == ridpointers[(int) RID_EXTERN])
22207 extern_p = 1;
22208 else if (storage == ridpointers[(int) RID_STATIC])
22209 static_p = 1;
22210 else
22212 error ("storage class %qD applied to template instantiation",
22213 storage);
22214 extern_p = 0;
22218 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
22220 /* DR 259 [temp.spec].
22222 Both an explicit instantiation and a declaration of an explicit
22223 specialization shall not appear in a program unless the explicit
22224 instantiation follows a declaration of the explicit specialization.
22226 For a given set of template parameters, if an explicit
22227 instantiation of a template appears after a declaration of an
22228 explicit specialization for that template, the explicit
22229 instantiation has no effect. */
22230 return;
22232 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
22234 /* [temp.spec]
22236 No program shall explicitly instantiate any template more
22237 than once.
22239 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
22240 instantiation was `extern'. If EXTERN_P then the second is.
22241 These cases are OK. */
22242 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
22244 if (!previous_instantiation_extern_p && !extern_p
22245 && (complain & tf_error))
22246 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
22248 /* If we've already instantiated the template, just return now. */
22249 if (!CLASSTYPE_INTERFACE_ONLY (t))
22250 return;
22253 check_explicit_instantiation_namespace (TYPE_NAME (t));
22254 mark_class_instantiated (t, extern_p);
22256 if (nomem_p)
22257 return;
22260 tree tmp;
22262 /* In contrast to implicit instantiation, where only the
22263 declarations, and not the definitions, of members are
22264 instantiated, we have here:
22266 [temp.explicit]
22268 The explicit instantiation of a class template specialization
22269 implies the instantiation of all of its members not
22270 previously explicitly specialized in the translation unit
22271 containing the explicit instantiation.
22273 Of course, we can't instantiate member template classes, since
22274 we don't have any arguments for them. Note that the standard
22275 is unclear on whether the instantiation of the members are
22276 *explicit* instantiations or not. However, the most natural
22277 interpretation is that it should be an explicit instantiation. */
22279 if (! static_p)
22280 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
22281 if (TREE_CODE (tmp) == FUNCTION_DECL
22282 && DECL_TEMPLATE_INSTANTIATION (tmp)
22283 && user_provided_p (tmp))
22284 instantiate_class_member (tmp, extern_p);
22286 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
22287 if (VAR_P (tmp) && DECL_TEMPLATE_INSTANTIATION (tmp))
22288 instantiate_class_member (tmp, extern_p);
22290 if (CLASSTYPE_NESTED_UTDS (t))
22291 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
22292 bt_instantiate_type_proc, &storage);
22296 /* Given a function DECL, which is a specialization of TMPL, modify
22297 DECL to be a re-instantiation of TMPL with the same template
22298 arguments. TMPL should be the template into which tsubst'ing
22299 should occur for DECL, not the most general template.
22301 One reason for doing this is a scenario like this:
22303 template <class T>
22304 void f(const T&, int i);
22306 void g() { f(3, 7); }
22308 template <class T>
22309 void f(const T& t, const int i) { }
22311 Note that when the template is first instantiated, with
22312 instantiate_template, the resulting DECL will have no name for the
22313 first parameter, and the wrong type for the second. So, when we go
22314 to instantiate the DECL, we regenerate it. */
22316 static void
22317 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
22319 /* The arguments used to instantiate DECL, from the most general
22320 template. */
22321 tree code_pattern;
22323 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
22325 /* Make sure that we can see identifiers, and compute access
22326 correctly. */
22327 push_access_scope (decl);
22329 if (TREE_CODE (decl) == FUNCTION_DECL)
22331 tree decl_parm;
22332 tree pattern_parm;
22333 tree specs;
22334 int args_depth;
22335 int parms_depth;
22337 args_depth = TMPL_ARGS_DEPTH (args);
22338 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
22339 if (args_depth > parms_depth)
22340 args = get_innermost_template_args (args, parms_depth);
22342 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
22343 args, tf_error, NULL_TREE,
22344 /*defer_ok*/false);
22345 if (specs && specs != error_mark_node)
22346 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
22347 specs);
22349 /* Merge parameter declarations. */
22350 decl_parm = skip_artificial_parms_for (decl,
22351 DECL_ARGUMENTS (decl));
22352 pattern_parm
22353 = skip_artificial_parms_for (code_pattern,
22354 DECL_ARGUMENTS (code_pattern));
22355 while (decl_parm && !DECL_PACK_P (pattern_parm))
22357 tree parm_type;
22358 tree attributes;
22360 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22361 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
22362 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
22363 NULL_TREE);
22364 parm_type = type_decays_to (parm_type);
22365 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22366 TREE_TYPE (decl_parm) = parm_type;
22367 attributes = DECL_ATTRIBUTES (pattern_parm);
22368 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22370 DECL_ATTRIBUTES (decl_parm) = attributes;
22371 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22373 decl_parm = DECL_CHAIN (decl_parm);
22374 pattern_parm = DECL_CHAIN (pattern_parm);
22376 /* Merge any parameters that match with the function parameter
22377 pack. */
22378 if (pattern_parm && DECL_PACK_P (pattern_parm))
22380 int i, len;
22381 tree expanded_types;
22382 /* Expand the TYPE_PACK_EXPANSION that provides the types for
22383 the parameters in this function parameter pack. */
22384 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
22385 args, tf_error, NULL_TREE);
22386 len = TREE_VEC_LENGTH (expanded_types);
22387 for (i = 0; i < len; i++)
22389 tree parm_type;
22390 tree attributes;
22392 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22393 /* Rename the parameter to include the index. */
22394 DECL_NAME (decl_parm) =
22395 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
22396 parm_type = TREE_VEC_ELT (expanded_types, i);
22397 parm_type = type_decays_to (parm_type);
22398 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22399 TREE_TYPE (decl_parm) = parm_type;
22400 attributes = DECL_ATTRIBUTES (pattern_parm);
22401 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22403 DECL_ATTRIBUTES (decl_parm) = attributes;
22404 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22406 decl_parm = DECL_CHAIN (decl_parm);
22409 /* Merge additional specifiers from the CODE_PATTERN. */
22410 if (DECL_DECLARED_INLINE_P (code_pattern)
22411 && !DECL_DECLARED_INLINE_P (decl))
22412 DECL_DECLARED_INLINE_P (decl) = 1;
22414 else if (VAR_P (decl))
22416 DECL_INITIAL (decl) =
22417 tsubst_expr (DECL_INITIAL (code_pattern), args,
22418 tf_error, DECL_TI_TEMPLATE (decl),
22419 /*integral_constant_expression_p=*/false);
22420 if (VAR_HAD_UNKNOWN_BOUND (decl))
22421 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
22422 tf_error, DECL_TI_TEMPLATE (decl));
22424 else
22425 gcc_unreachable ();
22427 pop_access_scope (decl);
22430 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
22431 substituted to get DECL. */
22433 tree
22434 template_for_substitution (tree decl)
22436 tree tmpl = DECL_TI_TEMPLATE (decl);
22438 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
22439 for the instantiation. This is not always the most general
22440 template. Consider, for example:
22442 template <class T>
22443 struct S { template <class U> void f();
22444 template <> void f<int>(); };
22446 and an instantiation of S<double>::f<int>. We want TD to be the
22447 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
22448 while (/* An instantiation cannot have a definition, so we need a
22449 more general template. */
22450 DECL_TEMPLATE_INSTANTIATION (tmpl)
22451 /* We must also deal with friend templates. Given:
22453 template <class T> struct S {
22454 template <class U> friend void f() {};
22457 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
22458 so far as the language is concerned, but that's still
22459 where we get the pattern for the instantiation from. On
22460 other hand, if the definition comes outside the class, say:
22462 template <class T> struct S {
22463 template <class U> friend void f();
22465 template <class U> friend void f() {}
22467 we don't need to look any further. That's what the check for
22468 DECL_INITIAL is for. */
22469 || (TREE_CODE (decl) == FUNCTION_DECL
22470 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
22471 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
22473 /* The present template, TD, should not be a definition. If it
22474 were a definition, we should be using it! Note that we
22475 cannot restructure the loop to just keep going until we find
22476 a template with a definition, since that might go too far if
22477 a specialization was declared, but not defined. */
22479 /* Fetch the more general template. */
22480 tmpl = DECL_TI_TEMPLATE (tmpl);
22483 return tmpl;
22486 /* Returns true if we need to instantiate this template instance even if we
22487 know we aren't going to emit it. */
22489 bool
22490 always_instantiate_p (tree decl)
22492 /* We always instantiate inline functions so that we can inline them. An
22493 explicit instantiation declaration prohibits implicit instantiation of
22494 non-inline functions. With high levels of optimization, we would
22495 normally inline non-inline functions -- but we're not allowed to do
22496 that for "extern template" functions. Therefore, we check
22497 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
22498 return ((TREE_CODE (decl) == FUNCTION_DECL
22499 && (DECL_DECLARED_INLINE_P (decl)
22500 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
22501 /* And we need to instantiate static data members so that
22502 their initializers are available in integral constant
22503 expressions. */
22504 || (VAR_P (decl)
22505 && decl_maybe_constant_var_p (decl)));
22508 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
22509 instantiate it now, modifying TREE_TYPE (fn). */
22511 void
22512 maybe_instantiate_noexcept (tree fn)
22514 tree fntype, spec, noex, clone;
22516 /* Don't instantiate a noexcept-specification from template context. */
22517 if (processing_template_decl)
22518 return;
22520 if (DECL_CLONED_FUNCTION_P (fn))
22521 fn = DECL_CLONED_FUNCTION (fn);
22522 fntype = TREE_TYPE (fn);
22523 spec = TYPE_RAISES_EXCEPTIONS (fntype);
22525 if (!spec || !TREE_PURPOSE (spec))
22526 return;
22528 noex = TREE_PURPOSE (spec);
22530 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
22532 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
22533 spec = get_defaulted_eh_spec (fn);
22534 else if (push_tinst_level (fn))
22536 push_access_scope (fn);
22537 push_deferring_access_checks (dk_no_deferred);
22538 input_location = DECL_SOURCE_LOCATION (fn);
22539 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
22540 DEFERRED_NOEXCEPT_ARGS (noex),
22541 tf_warning_or_error, fn,
22542 /*function_p=*/false,
22543 /*integral_constant_expression_p=*/true);
22544 pop_deferring_access_checks ();
22545 pop_access_scope (fn);
22546 pop_tinst_level ();
22547 spec = build_noexcept_spec (noex, tf_warning_or_error);
22548 if (spec == error_mark_node)
22549 spec = noexcept_false_spec;
22551 else
22552 spec = noexcept_false_spec;
22554 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
22557 FOR_EACH_CLONE (clone, fn)
22559 if (TREE_TYPE (clone) == fntype)
22560 TREE_TYPE (clone) = TREE_TYPE (fn);
22561 else
22562 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
22566 /* Produce the definition of D, a _DECL generated from a template. If
22567 DEFER_OK is true, then we don't have to actually do the
22568 instantiation now; we just have to do it sometime. Normally it is
22569 an error if this is an explicit instantiation but D is undefined.
22570 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
22571 instantiated class template. */
22573 tree
22574 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
22576 tree tmpl = DECL_TI_TEMPLATE (d);
22577 tree gen_args;
22578 tree args;
22579 tree td;
22580 tree code_pattern;
22581 tree spec;
22582 tree gen_tmpl;
22583 bool pattern_defined;
22584 location_t saved_loc = input_location;
22585 int saved_unevaluated_operand = cp_unevaluated_operand;
22586 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
22587 bool external_p;
22588 bool deleted_p;
22590 /* This function should only be used to instantiate templates for
22591 functions and static member variables. */
22592 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
22594 /* A concept is never instantiated. */
22595 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
22597 /* Variables are never deferred; if instantiation is required, they
22598 are instantiated right away. That allows for better code in the
22599 case that an expression refers to the value of the variable --
22600 if the variable has a constant value the referring expression can
22601 take advantage of that fact. */
22602 if (VAR_P (d))
22603 defer_ok = false;
22605 /* Don't instantiate cloned functions. Instead, instantiate the
22606 functions they cloned. */
22607 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
22608 d = DECL_CLONED_FUNCTION (d);
22610 if (DECL_TEMPLATE_INSTANTIATED (d)
22611 || (TREE_CODE (d) == FUNCTION_DECL
22612 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
22613 || DECL_TEMPLATE_SPECIALIZATION (d))
22614 /* D has already been instantiated or explicitly specialized, so
22615 there's nothing for us to do here.
22617 It might seem reasonable to check whether or not D is an explicit
22618 instantiation, and, if so, stop here. But when an explicit
22619 instantiation is deferred until the end of the compilation,
22620 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
22621 the instantiation. */
22622 return d;
22624 /* Check to see whether we know that this template will be
22625 instantiated in some other file, as with "extern template"
22626 extension. */
22627 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
22629 /* In general, we do not instantiate such templates. */
22630 if (external_p && !always_instantiate_p (d))
22631 return d;
22633 gen_tmpl = most_general_template (tmpl);
22634 gen_args = DECL_TI_ARGS (d);
22636 if (tmpl != gen_tmpl)
22637 /* We should already have the extra args. */
22638 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
22639 == TMPL_ARGS_DEPTH (gen_args));
22640 /* And what's in the hash table should match D. */
22641 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
22642 || spec == NULL_TREE);
22644 /* This needs to happen before any tsubsting. */
22645 if (! push_tinst_level (d))
22646 return d;
22648 timevar_push (TV_TEMPLATE_INST);
22650 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
22651 for the instantiation. */
22652 td = template_for_substitution (d);
22653 args = gen_args;
22655 if (VAR_P (d))
22657 /* Look up an explicit specialization, if any. */
22658 tree tid = lookup_template_variable (gen_tmpl, gen_args);
22659 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
22660 if (elt && elt != error_mark_node)
22662 td = TREE_VALUE (elt);
22663 args = TREE_PURPOSE (elt);
22667 code_pattern = DECL_TEMPLATE_RESULT (td);
22669 /* We should never be trying to instantiate a member of a class
22670 template or partial specialization. */
22671 gcc_assert (d != code_pattern);
22673 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
22674 || DECL_TEMPLATE_SPECIALIZATION (td))
22675 /* In the case of a friend template whose definition is provided
22676 outside the class, we may have too many arguments. Drop the
22677 ones we don't need. The same is true for specializations. */
22678 args = get_innermost_template_args
22679 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
22681 if (TREE_CODE (d) == FUNCTION_DECL)
22683 deleted_p = DECL_DELETED_FN (code_pattern);
22684 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
22685 && DECL_INITIAL (code_pattern) != error_mark_node)
22686 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
22687 || deleted_p);
22689 else
22691 deleted_p = false;
22692 if (DECL_CLASS_SCOPE_P (code_pattern))
22693 pattern_defined = (! DECL_IN_AGGR_P (code_pattern)
22694 || DECL_INLINE_VAR_P (code_pattern));
22695 else
22696 pattern_defined = ! DECL_EXTERNAL (code_pattern);
22699 /* We may be in the middle of deferred access check. Disable it now. */
22700 push_deferring_access_checks (dk_no_deferred);
22702 /* Unless an explicit instantiation directive has already determined
22703 the linkage of D, remember that a definition is available for
22704 this entity. */
22705 if (pattern_defined
22706 && !DECL_INTERFACE_KNOWN (d)
22707 && !DECL_NOT_REALLY_EXTERN (d))
22708 mark_definable (d);
22710 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
22711 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
22712 input_location = DECL_SOURCE_LOCATION (d);
22714 /* If D is a member of an explicitly instantiated class template,
22715 and no definition is available, treat it like an implicit
22716 instantiation. */
22717 if (!pattern_defined && expl_inst_class_mem_p
22718 && DECL_EXPLICIT_INSTANTIATION (d))
22720 /* Leave linkage flags alone on instantiations with anonymous
22721 visibility. */
22722 if (TREE_PUBLIC (d))
22724 DECL_NOT_REALLY_EXTERN (d) = 0;
22725 DECL_INTERFACE_KNOWN (d) = 0;
22727 SET_DECL_IMPLICIT_INSTANTIATION (d);
22730 /* Defer all other templates, unless we have been explicitly
22731 forbidden from doing so. */
22732 if (/* If there is no definition, we cannot instantiate the
22733 template. */
22734 ! pattern_defined
22735 /* If it's OK to postpone instantiation, do so. */
22736 || defer_ok
22737 /* If this is a static data member that will be defined
22738 elsewhere, we don't want to instantiate the entire data
22739 member, but we do want to instantiate the initializer so that
22740 we can substitute that elsewhere. */
22741 || (external_p && VAR_P (d))
22742 /* Handle here a deleted function too, avoid generating
22743 its body (c++/61080). */
22744 || deleted_p)
22746 /* The definition of the static data member is now required so
22747 we must substitute the initializer. */
22748 if (VAR_P (d)
22749 && !DECL_INITIAL (d)
22750 && DECL_INITIAL (code_pattern))
22752 tree ns;
22753 tree init;
22754 bool const_init = false;
22755 bool enter_context = DECL_CLASS_SCOPE_P (d);
22757 ns = decl_namespace_context (d);
22758 push_nested_namespace (ns);
22759 if (enter_context)
22760 push_nested_class (DECL_CONTEXT (d));
22761 init = tsubst_expr (DECL_INITIAL (code_pattern),
22762 args,
22763 tf_warning_or_error, NULL_TREE,
22764 /*integral_constant_expression_p=*/false);
22765 /* If instantiating the initializer involved instantiating this
22766 again, don't call cp_finish_decl twice. */
22767 if (!DECL_INITIAL (d))
22769 /* Make sure the initializer is still constant, in case of
22770 circular dependency (template/instantiate6.C). */
22771 const_init
22772 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
22773 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
22774 /*asmspec_tree=*/NULL_TREE,
22775 LOOKUP_ONLYCONVERTING);
22777 if (enter_context)
22778 pop_nested_class ();
22779 pop_nested_namespace (ns);
22782 /* We restore the source position here because it's used by
22783 add_pending_template. */
22784 input_location = saved_loc;
22786 if (at_eof && !pattern_defined
22787 && DECL_EXPLICIT_INSTANTIATION (d)
22788 && DECL_NOT_REALLY_EXTERN (d))
22789 /* [temp.explicit]
22791 The definition of a non-exported function template, a
22792 non-exported member function template, or a non-exported
22793 member function or static data member of a class template
22794 shall be present in every translation unit in which it is
22795 explicitly instantiated. */
22796 permerror (input_location, "explicit instantiation of %qD "
22797 "but no definition available", d);
22799 /* If we're in unevaluated context, we just wanted to get the
22800 constant value; this isn't an odr use, so don't queue
22801 a full instantiation. */
22802 if (cp_unevaluated_operand != 0)
22803 goto out;
22804 /* ??? Historically, we have instantiated inline functions, even
22805 when marked as "extern template". */
22806 if (!(external_p && VAR_P (d)))
22807 add_pending_template (d);
22808 goto out;
22810 /* Tell the repository that D is available in this translation unit
22811 -- and see if it is supposed to be instantiated here. */
22812 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
22814 /* In a PCH file, despite the fact that the repository hasn't
22815 requested instantiation in the PCH it is still possible that
22816 an instantiation will be required in a file that includes the
22817 PCH. */
22818 if (pch_file)
22819 add_pending_template (d);
22820 /* Instantiate inline functions so that the inliner can do its
22821 job, even though we'll not be emitting a copy of this
22822 function. */
22823 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
22824 goto out;
22827 bool push_to_top, nested;
22828 tree fn_context;
22829 fn_context = decl_function_context (d);
22830 nested = current_function_decl != NULL_TREE;
22831 push_to_top = !(nested && fn_context == current_function_decl);
22833 vec<tree> omp_privatization_save;
22834 if (nested)
22835 save_omp_privatization_clauses (omp_privatization_save);
22837 if (push_to_top)
22838 push_to_top_level ();
22839 else
22841 push_function_context ();
22842 cp_unevaluated_operand = 0;
22843 c_inhibit_evaluation_warnings = 0;
22846 /* Mark D as instantiated so that recursive calls to
22847 instantiate_decl do not try to instantiate it again. */
22848 DECL_TEMPLATE_INSTANTIATED (d) = 1;
22850 /* Regenerate the declaration in case the template has been modified
22851 by a subsequent redeclaration. */
22852 regenerate_decl_from_template (d, td, args);
22854 /* We already set the file and line above. Reset them now in case
22855 they changed as a result of calling regenerate_decl_from_template. */
22856 input_location = DECL_SOURCE_LOCATION (d);
22858 if (VAR_P (d))
22860 tree init;
22861 bool const_init = false;
22863 /* Clear out DECL_RTL; whatever was there before may not be right
22864 since we've reset the type of the declaration. */
22865 SET_DECL_RTL (d, NULL);
22866 DECL_IN_AGGR_P (d) = 0;
22868 /* The initializer is placed in DECL_INITIAL by
22869 regenerate_decl_from_template so we don't need to
22870 push/pop_access_scope again here. Pull it out so that
22871 cp_finish_decl can process it. */
22872 init = DECL_INITIAL (d);
22873 DECL_INITIAL (d) = NULL_TREE;
22874 DECL_INITIALIZED_P (d) = 0;
22876 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
22877 initializer. That function will defer actual emission until
22878 we have a chance to determine linkage. */
22879 DECL_EXTERNAL (d) = 0;
22881 /* Enter the scope of D so that access-checking works correctly. */
22882 bool enter_context = DECL_CLASS_SCOPE_P (d);
22883 if (enter_context)
22884 push_nested_class (DECL_CONTEXT (d));
22886 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
22887 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
22889 if (enter_context)
22890 pop_nested_class ();
22892 if (variable_template_p (gen_tmpl))
22893 note_variable_template_instantiation (d);
22895 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
22896 synthesize_method (d);
22897 else if (TREE_CODE (d) == FUNCTION_DECL)
22899 hash_map<tree, tree> *saved_local_specializations;
22900 tree tmpl_parm;
22901 tree spec_parm;
22902 tree block = NULL_TREE;
22903 tree lambda_ctx = NULL_TREE;
22905 /* Save away the current list, in case we are instantiating one
22906 template from within the body of another. */
22907 saved_local_specializations = local_specializations;
22909 /* Set up the list of local specializations. */
22910 local_specializations = new hash_map<tree, tree>;
22912 /* Set up context. */
22913 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
22914 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
22915 block = push_stmt_list ();
22916 else
22918 if (push_to_top && LAMBDA_FUNCTION_P (d))
22920 /* When instantiating a lambda's templated function
22921 operator, we need to push the non-lambda class scope
22922 of the lambda itself so that the nested function
22923 stack is sufficiently correct to deal with this
22924 capture. */
22925 lambda_ctx = DECL_CONTEXT (d);
22927 lambda_ctx = decl_type_context (TYPE_NAME (lambda_ctx));
22928 while (lambda_ctx && LAMBDA_TYPE_P (lambda_ctx));
22929 if (lambda_ctx)
22930 push_nested_class (lambda_ctx);
22932 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
22935 /* Some typedefs referenced from within the template code need to be
22936 access checked at template instantiation time, i.e now. These
22937 types were added to the template at parsing time. Let's get those
22938 and perform the access checks then. */
22939 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
22940 args);
22942 /* Create substitution entries for the parameters. */
22943 tmpl_parm = DECL_ARGUMENTS (code_pattern);
22944 spec_parm = DECL_ARGUMENTS (d);
22945 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
22947 register_local_specialization (spec_parm, tmpl_parm);
22948 spec_parm = skip_artificial_parms_for (d, spec_parm);
22949 tmpl_parm = skip_artificial_parms_for (code_pattern, tmpl_parm);
22951 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
22953 if (!DECL_PACK_P (tmpl_parm))
22955 register_local_specialization (spec_parm, tmpl_parm);
22956 spec_parm = DECL_CHAIN (spec_parm);
22958 else
22960 /* Register the (value) argument pack as a specialization of
22961 TMPL_PARM, then move on. */
22962 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
22963 register_local_specialization (argpack, tmpl_parm);
22966 gcc_assert (!spec_parm);
22968 /* Substitute into the body of the function. */
22969 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
22970 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
22971 tf_warning_or_error, tmpl);
22972 else
22974 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
22975 tf_warning_or_error, tmpl,
22976 /*integral_constant_expression_p=*/false);
22978 /* Set the current input_location to the end of the function
22979 so that finish_function knows where we are. */
22980 input_location
22981 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
22983 /* Remember if we saw an infinite loop in the template. */
22984 current_function_infinite_loop
22985 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
22988 /* We don't need the local specializations any more. */
22989 delete local_specializations;
22990 local_specializations = saved_local_specializations;
22992 /* Finish the function. */
22993 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
22994 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
22995 DECL_SAVED_TREE (d) = pop_stmt_list (block);
22996 else
22998 d = finish_function (0);
22999 expand_or_defer_fn (d);
23001 if (lambda_ctx)
23002 pop_nested_class ();
23004 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23005 cp_check_omp_declare_reduction (d);
23008 /* We're not deferring instantiation any more. */
23009 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
23011 if (push_to_top)
23012 pop_from_top_level ();
23013 else
23014 pop_function_context ();
23016 if (nested)
23017 restore_omp_privatization_clauses (omp_privatization_save);
23019 out:
23020 pop_deferring_access_checks ();
23021 timevar_pop (TV_TEMPLATE_INST);
23022 pop_tinst_level ();
23023 input_location = saved_loc;
23024 cp_unevaluated_operand = saved_unevaluated_operand;
23025 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23027 return d;
23030 /* Run through the list of templates that we wish we could
23031 instantiate, and instantiate any we can. RETRIES is the
23032 number of times we retry pending template instantiation. */
23034 void
23035 instantiate_pending_templates (int retries)
23037 int reconsider;
23038 location_t saved_loc = input_location;
23040 /* Instantiating templates may trigger vtable generation. This in turn
23041 may require further template instantiations. We place a limit here
23042 to avoid infinite loop. */
23043 if (pending_templates && retries >= max_tinst_depth)
23045 tree decl = pending_templates->tinst->decl;
23047 fatal_error (input_location,
23048 "template instantiation depth exceeds maximum of %d"
23049 " instantiating %q+D, possibly from virtual table generation"
23050 " (use -ftemplate-depth= to increase the maximum)",
23051 max_tinst_depth, decl);
23052 if (TREE_CODE (decl) == FUNCTION_DECL)
23053 /* Pretend that we defined it. */
23054 DECL_INITIAL (decl) = error_mark_node;
23055 return;
23060 struct pending_template **t = &pending_templates;
23061 struct pending_template *last = NULL;
23062 reconsider = 0;
23063 while (*t)
23065 tree instantiation = reopen_tinst_level ((*t)->tinst);
23066 bool complete = false;
23068 if (TYPE_P (instantiation))
23070 tree fn;
23072 if (!COMPLETE_TYPE_P (instantiation))
23074 instantiate_class_template (instantiation);
23075 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
23076 for (fn = TYPE_METHODS (instantiation);
23078 fn = TREE_CHAIN (fn))
23079 if (! DECL_ARTIFICIAL (fn))
23080 instantiate_decl (fn,
23081 /*defer_ok=*/false,
23082 /*expl_inst_class_mem_p=*/false);
23083 if (COMPLETE_TYPE_P (instantiation))
23084 reconsider = 1;
23087 complete = COMPLETE_TYPE_P (instantiation);
23089 else
23091 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
23092 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
23094 instantiation
23095 = instantiate_decl (instantiation,
23096 /*defer_ok=*/false,
23097 /*expl_inst_class_mem_p=*/false);
23098 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
23099 reconsider = 1;
23102 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
23103 || DECL_TEMPLATE_INSTANTIATED (instantiation));
23106 if (complete)
23107 /* If INSTANTIATION has been instantiated, then we don't
23108 need to consider it again in the future. */
23109 *t = (*t)->next;
23110 else
23112 last = *t;
23113 t = &(*t)->next;
23115 tinst_depth = 0;
23116 current_tinst_level = NULL;
23118 last_pending_template = last;
23120 while (reconsider);
23122 input_location = saved_loc;
23125 /* Substitute ARGVEC into T, which is a list of initializers for
23126 either base class or a non-static data member. The TREE_PURPOSEs
23127 are DECLs, and the TREE_VALUEs are the initializer values. Used by
23128 instantiate_decl. */
23130 static tree
23131 tsubst_initializer_list (tree t, tree argvec)
23133 tree inits = NULL_TREE;
23135 for (; t; t = TREE_CHAIN (t))
23137 tree decl;
23138 tree init;
23139 tree expanded_bases = NULL_TREE;
23140 tree expanded_arguments = NULL_TREE;
23141 int i, len = 1;
23143 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
23145 tree expr;
23146 tree arg;
23148 /* Expand the base class expansion type into separate base
23149 classes. */
23150 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
23151 tf_warning_or_error,
23152 NULL_TREE);
23153 if (expanded_bases == error_mark_node)
23154 continue;
23156 /* We'll be building separate TREE_LISTs of arguments for
23157 each base. */
23158 len = TREE_VEC_LENGTH (expanded_bases);
23159 expanded_arguments = make_tree_vec (len);
23160 for (i = 0; i < len; i++)
23161 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
23163 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
23164 expand each argument in the TREE_VALUE of t. */
23165 expr = make_node (EXPR_PACK_EXPANSION);
23166 PACK_EXPANSION_LOCAL_P (expr) = true;
23167 PACK_EXPANSION_PARAMETER_PACKS (expr) =
23168 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
23170 if (TREE_VALUE (t) == void_type_node)
23171 /* VOID_TYPE_NODE is used to indicate
23172 value-initialization. */
23174 for (i = 0; i < len; i++)
23175 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
23177 else
23179 /* Substitute parameter packs into each argument in the
23180 TREE_LIST. */
23181 in_base_initializer = 1;
23182 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
23184 tree expanded_exprs;
23186 /* Expand the argument. */
23187 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
23188 expanded_exprs
23189 = tsubst_pack_expansion (expr, argvec,
23190 tf_warning_or_error,
23191 NULL_TREE);
23192 if (expanded_exprs == error_mark_node)
23193 continue;
23195 /* Prepend each of the expanded expressions to the
23196 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
23197 for (i = 0; i < len; i++)
23199 TREE_VEC_ELT (expanded_arguments, i) =
23200 tree_cons (NULL_TREE,
23201 TREE_VEC_ELT (expanded_exprs, i),
23202 TREE_VEC_ELT (expanded_arguments, i));
23205 in_base_initializer = 0;
23207 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
23208 since we built them backwards. */
23209 for (i = 0; i < len; i++)
23211 TREE_VEC_ELT (expanded_arguments, i) =
23212 nreverse (TREE_VEC_ELT (expanded_arguments, i));
23217 for (i = 0; i < len; ++i)
23219 if (expanded_bases)
23221 decl = TREE_VEC_ELT (expanded_bases, i);
23222 decl = expand_member_init (decl);
23223 init = TREE_VEC_ELT (expanded_arguments, i);
23225 else
23227 tree tmp;
23228 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
23229 tf_warning_or_error, NULL_TREE);
23231 decl = expand_member_init (decl);
23232 if (decl && !DECL_P (decl))
23233 in_base_initializer = 1;
23235 init = TREE_VALUE (t);
23236 tmp = init;
23237 if (init != void_type_node)
23238 init = tsubst_expr (init, argvec,
23239 tf_warning_or_error, NULL_TREE,
23240 /*integral_constant_expression_p=*/false);
23241 if (init == NULL_TREE && tmp != NULL_TREE)
23242 /* If we had an initializer but it instantiated to nothing,
23243 value-initialize the object. This will only occur when
23244 the initializer was a pack expansion where the parameter
23245 packs used in that expansion were of length zero. */
23246 init = void_type_node;
23247 in_base_initializer = 0;
23250 if (decl)
23252 init = build_tree_list (decl, init);
23253 TREE_CHAIN (init) = inits;
23254 inits = init;
23258 return inits;
23261 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
23263 static void
23264 set_current_access_from_decl (tree decl)
23266 if (TREE_PRIVATE (decl))
23267 current_access_specifier = access_private_node;
23268 else if (TREE_PROTECTED (decl))
23269 current_access_specifier = access_protected_node;
23270 else
23271 current_access_specifier = access_public_node;
23274 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
23275 is the instantiation (which should have been created with
23276 start_enum) and ARGS are the template arguments to use. */
23278 static void
23279 tsubst_enum (tree tag, tree newtag, tree args)
23281 tree e;
23283 if (SCOPED_ENUM_P (newtag))
23284 begin_scope (sk_scoped_enum, newtag);
23286 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
23288 tree value;
23289 tree decl;
23291 decl = TREE_VALUE (e);
23292 /* Note that in a template enum, the TREE_VALUE is the
23293 CONST_DECL, not the corresponding INTEGER_CST. */
23294 value = tsubst_expr (DECL_INITIAL (decl),
23295 args, tf_warning_or_error, NULL_TREE,
23296 /*integral_constant_expression_p=*/true);
23298 /* Give this enumeration constant the correct access. */
23299 set_current_access_from_decl (decl);
23301 /* Actually build the enumerator itself. Here we're assuming that
23302 enumerators can't have dependent attributes. */
23303 build_enumerator (DECL_NAME (decl), value, newtag,
23304 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
23307 if (SCOPED_ENUM_P (newtag))
23308 finish_scope ();
23310 finish_enum_value_list (newtag);
23311 finish_enum (newtag);
23313 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
23314 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
23317 /* DECL is a FUNCTION_DECL that is a template specialization. Return
23318 its type -- but without substituting the innermost set of template
23319 arguments. So, innermost set of template parameters will appear in
23320 the type. */
23322 tree
23323 get_mostly_instantiated_function_type (tree decl)
23325 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
23326 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
23329 /* Return truthvalue if we're processing a template different from
23330 the last one involved in diagnostics. */
23331 bool
23332 problematic_instantiation_changed (void)
23334 return current_tinst_level != last_error_tinst_level;
23337 /* Remember current template involved in diagnostics. */
23338 void
23339 record_last_problematic_instantiation (void)
23341 last_error_tinst_level = current_tinst_level;
23344 struct tinst_level *
23345 current_instantiation (void)
23347 return current_tinst_level;
23350 /* Return TRUE if current_function_decl is being instantiated, false
23351 otherwise. */
23353 bool
23354 instantiating_current_function_p (void)
23356 return (current_instantiation ()
23357 && current_instantiation ()->decl == current_function_decl);
23360 /* [temp.param] Check that template non-type parm TYPE is of an allowable
23361 type. Return zero for ok, nonzero for disallowed. Issue error and
23362 warning messages under control of COMPLAIN. */
23364 static int
23365 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
23367 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
23368 return 0;
23369 else if (POINTER_TYPE_P (type))
23370 return 0;
23371 else if (TYPE_PTRMEM_P (type))
23372 return 0;
23373 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
23374 return 0;
23375 else if (TREE_CODE (type) == TYPENAME_TYPE)
23376 return 0;
23377 else if (TREE_CODE (type) == DECLTYPE_TYPE)
23378 return 0;
23379 else if (TREE_CODE (type) == NULLPTR_TYPE)
23380 return 0;
23381 /* A bound template template parm could later be instantiated to have a valid
23382 nontype parm type via an alias template. */
23383 else if (cxx_dialect >= cxx11
23384 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23385 return 0;
23387 if (complain & tf_error)
23389 if (type == error_mark_node)
23390 inform (input_location, "invalid template non-type parameter");
23391 else
23392 error ("%q#T is not a valid type for a template non-type parameter",
23393 type);
23395 return 1;
23398 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
23399 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
23401 static bool
23402 dependent_type_p_r (tree type)
23404 tree scope;
23406 /* [temp.dep.type]
23408 A type is dependent if it is:
23410 -- a template parameter. Template template parameters are types
23411 for us (since TYPE_P holds true for them) so we handle
23412 them here. */
23413 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
23414 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
23415 return true;
23416 /* -- a qualified-id with a nested-name-specifier which contains a
23417 class-name that names a dependent type or whose unqualified-id
23418 names a dependent type. */
23419 if (TREE_CODE (type) == TYPENAME_TYPE)
23420 return true;
23422 /* An alias template specialization can be dependent even if the
23423 resulting type is not. */
23424 if (dependent_alias_template_spec_p (type))
23425 return true;
23427 /* -- a cv-qualified type where the cv-unqualified type is
23428 dependent.
23429 No code is necessary for this bullet; the code below handles
23430 cv-qualified types, and we don't want to strip aliases with
23431 TYPE_MAIN_VARIANT because of DR 1558. */
23432 /* -- a compound type constructed from any dependent type. */
23433 if (TYPE_PTRMEM_P (type))
23434 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
23435 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
23436 (type)));
23437 else if (TYPE_PTR_P (type)
23438 || TREE_CODE (type) == REFERENCE_TYPE)
23439 return dependent_type_p (TREE_TYPE (type));
23440 else if (TREE_CODE (type) == FUNCTION_TYPE
23441 || TREE_CODE (type) == METHOD_TYPE)
23443 tree arg_type;
23445 if (dependent_type_p (TREE_TYPE (type)))
23446 return true;
23447 for (arg_type = TYPE_ARG_TYPES (type);
23448 arg_type;
23449 arg_type = TREE_CHAIN (arg_type))
23450 if (dependent_type_p (TREE_VALUE (arg_type)))
23451 return true;
23452 if (cxx_dialect >= cxx1z)
23454 /* A value-dependent noexcept-specifier makes the type dependent. */
23455 tree spec = TYPE_RAISES_EXCEPTIONS (type);
23456 if (spec && TREE_PURPOSE (spec)
23457 && value_dependent_expression_p (TREE_PURPOSE (spec)))
23458 return true;
23460 return false;
23462 /* -- an array type constructed from any dependent type or whose
23463 size is specified by a constant expression that is
23464 value-dependent.
23466 We checked for type- and value-dependence of the bounds in
23467 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
23468 if (TREE_CODE (type) == ARRAY_TYPE)
23470 if (TYPE_DOMAIN (type)
23471 && dependent_type_p (TYPE_DOMAIN (type)))
23472 return true;
23473 return dependent_type_p (TREE_TYPE (type));
23476 /* -- a template-id in which either the template name is a template
23477 parameter ... */
23478 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23479 return true;
23480 /* ... or any of the template arguments is a dependent type or
23481 an expression that is type-dependent or value-dependent. */
23482 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
23483 && (any_dependent_template_arguments_p
23484 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
23485 return true;
23487 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
23488 dependent; if the argument of the `typeof' expression is not
23489 type-dependent, then it should already been have resolved. */
23490 if (TREE_CODE (type) == TYPEOF_TYPE
23491 || TREE_CODE (type) == DECLTYPE_TYPE
23492 || TREE_CODE (type) == UNDERLYING_TYPE)
23493 return true;
23495 /* A template argument pack is dependent if any of its packed
23496 arguments are. */
23497 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
23499 tree args = ARGUMENT_PACK_ARGS (type);
23500 int i, len = TREE_VEC_LENGTH (args);
23501 for (i = 0; i < len; ++i)
23502 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
23503 return true;
23506 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
23507 be template parameters. */
23508 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
23509 return true;
23511 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
23512 return true;
23514 /* The standard does not specifically mention types that are local
23515 to template functions or local classes, but they should be
23516 considered dependent too. For example:
23518 template <int I> void f() {
23519 enum E { a = I };
23520 S<sizeof (E)> s;
23523 The size of `E' cannot be known until the value of `I' has been
23524 determined. Therefore, `E' must be considered dependent. */
23525 scope = TYPE_CONTEXT (type);
23526 if (scope && TYPE_P (scope))
23527 return dependent_type_p (scope);
23528 /* Don't use type_dependent_expression_p here, as it can lead
23529 to infinite recursion trying to determine whether a lambda
23530 nested in a lambda is dependent (c++/47687). */
23531 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
23532 && DECL_LANG_SPECIFIC (scope)
23533 && DECL_TEMPLATE_INFO (scope)
23534 && (any_dependent_template_arguments_p
23535 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
23536 return true;
23538 /* Other types are non-dependent. */
23539 return false;
23542 /* Returns TRUE if TYPE is dependent, in the sense of
23543 [temp.dep.type]. Note that a NULL type is considered dependent. */
23545 bool
23546 dependent_type_p (tree type)
23548 /* If there are no template parameters in scope, then there can't be
23549 any dependent types. */
23550 if (!processing_template_decl)
23552 /* If we are not processing a template, then nobody should be
23553 providing us with a dependent type. */
23554 gcc_assert (type);
23555 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
23556 return false;
23559 /* If the type is NULL, we have not computed a type for the entity
23560 in question; in that case, the type is dependent. */
23561 if (!type)
23562 return true;
23564 /* Erroneous types can be considered non-dependent. */
23565 if (type == error_mark_node)
23566 return false;
23568 /* Getting here with global_type_node means we improperly called this
23569 function on the TREE_TYPE of an IDENTIFIER_NODE. */
23570 gcc_checking_assert (type != global_type_node);
23572 /* If we have not already computed the appropriate value for TYPE,
23573 do so now. */
23574 if (!TYPE_DEPENDENT_P_VALID (type))
23576 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
23577 TYPE_DEPENDENT_P_VALID (type) = 1;
23580 return TYPE_DEPENDENT_P (type);
23583 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
23584 lookup. In other words, a dependent type that is not the current
23585 instantiation. */
23587 bool
23588 dependent_scope_p (tree scope)
23590 return (scope && TYPE_P (scope) && dependent_type_p (scope)
23591 && !currently_open_class (scope));
23594 /* T is a SCOPE_REF; return whether we need to consider it
23595 instantiation-dependent so that we can check access at instantiation
23596 time even though we know which member it resolves to. */
23598 static bool
23599 instantiation_dependent_scope_ref_p (tree t)
23601 if (DECL_P (TREE_OPERAND (t, 1))
23602 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
23603 && accessible_in_template_p (TREE_OPERAND (t, 0),
23604 TREE_OPERAND (t, 1)))
23605 return false;
23606 else
23607 return true;
23610 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
23611 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
23612 expression. */
23614 /* Note that this predicate is not appropriate for general expressions;
23615 only constant expressions (that satisfy potential_constant_expression)
23616 can be tested for value dependence. */
23618 bool
23619 value_dependent_expression_p (tree expression)
23621 if (!processing_template_decl || expression == NULL_TREE)
23622 return false;
23624 /* A name declared with a dependent type. */
23625 if (DECL_P (expression) && type_dependent_expression_p (expression))
23626 return true;
23628 switch (TREE_CODE (expression))
23630 case BASELINK:
23631 /* A dependent member function of the current instantiation. */
23632 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
23634 case FUNCTION_DECL:
23635 /* A dependent member function of the current instantiation. */
23636 if (DECL_CLASS_SCOPE_P (expression)
23637 && dependent_type_p (DECL_CONTEXT (expression)))
23638 return true;
23639 break;
23641 case IDENTIFIER_NODE:
23642 /* A name that has not been looked up -- must be dependent. */
23643 return true;
23645 case TEMPLATE_PARM_INDEX:
23646 /* A non-type template parm. */
23647 return true;
23649 case CONST_DECL:
23650 /* A non-type template parm. */
23651 if (DECL_TEMPLATE_PARM_P (expression))
23652 return true;
23653 return value_dependent_expression_p (DECL_INITIAL (expression));
23655 case VAR_DECL:
23656 /* A constant with literal type and is initialized
23657 with an expression that is value-dependent.
23659 Note that a non-dependent parenthesized initializer will have
23660 already been replaced with its constant value, so if we see
23661 a TREE_LIST it must be dependent. */
23662 if (DECL_INITIAL (expression)
23663 && decl_constant_var_p (expression)
23664 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
23665 /* cp_finish_decl doesn't fold reference initializers. */
23666 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE
23667 || type_dependent_expression_p (DECL_INITIAL (expression))
23668 || value_dependent_expression_p (DECL_INITIAL (expression))))
23669 return true;
23670 if (DECL_HAS_VALUE_EXPR_P (expression))
23672 tree value_expr = DECL_VALUE_EXPR (expression);
23673 if (type_dependent_expression_p (value_expr))
23674 return true;
23676 return false;
23678 case DYNAMIC_CAST_EXPR:
23679 case STATIC_CAST_EXPR:
23680 case CONST_CAST_EXPR:
23681 case REINTERPRET_CAST_EXPR:
23682 case CAST_EXPR:
23683 /* These expressions are value-dependent if the type to which
23684 the cast occurs is dependent or the expression being casted
23685 is value-dependent. */
23687 tree type = TREE_TYPE (expression);
23689 if (dependent_type_p (type))
23690 return true;
23692 /* A functional cast has a list of operands. */
23693 expression = TREE_OPERAND (expression, 0);
23694 if (!expression)
23696 /* If there are no operands, it must be an expression such
23697 as "int()". This should not happen for aggregate types
23698 because it would form non-constant expressions. */
23699 gcc_assert (cxx_dialect >= cxx11
23700 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
23702 return false;
23705 if (TREE_CODE (expression) == TREE_LIST)
23706 return any_value_dependent_elements_p (expression);
23708 return value_dependent_expression_p (expression);
23711 case SIZEOF_EXPR:
23712 if (SIZEOF_EXPR_TYPE_P (expression))
23713 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
23714 /* FALLTHRU */
23715 case ALIGNOF_EXPR:
23716 case TYPEID_EXPR:
23717 /* A `sizeof' expression is value-dependent if the operand is
23718 type-dependent or is a pack expansion. */
23719 expression = TREE_OPERAND (expression, 0);
23720 if (PACK_EXPANSION_P (expression))
23721 return true;
23722 else if (TYPE_P (expression))
23723 return dependent_type_p (expression);
23724 return instantiation_dependent_uneval_expression_p (expression);
23726 case AT_ENCODE_EXPR:
23727 /* An 'encode' expression is value-dependent if the operand is
23728 type-dependent. */
23729 expression = TREE_OPERAND (expression, 0);
23730 return dependent_type_p (expression);
23732 case NOEXCEPT_EXPR:
23733 expression = TREE_OPERAND (expression, 0);
23734 return instantiation_dependent_uneval_expression_p (expression);
23736 case SCOPE_REF:
23737 /* All instantiation-dependent expressions should also be considered
23738 value-dependent. */
23739 return instantiation_dependent_scope_ref_p (expression);
23741 case COMPONENT_REF:
23742 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
23743 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
23745 case NONTYPE_ARGUMENT_PACK:
23746 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
23747 is value-dependent. */
23749 tree values = ARGUMENT_PACK_ARGS (expression);
23750 int i, len = TREE_VEC_LENGTH (values);
23752 for (i = 0; i < len; ++i)
23753 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
23754 return true;
23756 return false;
23759 case TRAIT_EXPR:
23761 tree type2 = TRAIT_EXPR_TYPE2 (expression);
23762 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
23763 || (type2 ? dependent_type_p (type2) : false));
23766 case MODOP_EXPR:
23767 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
23768 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
23770 case ARRAY_REF:
23771 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
23772 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
23774 case ADDR_EXPR:
23776 tree op = TREE_OPERAND (expression, 0);
23777 return (value_dependent_expression_p (op)
23778 || has_value_dependent_address (op));
23781 case REQUIRES_EXPR:
23782 /* Treat all requires-expressions as value-dependent so
23783 we don't try to fold them. */
23784 return true;
23786 case TYPE_REQ:
23787 return dependent_type_p (TREE_OPERAND (expression, 0));
23789 case CALL_EXPR:
23791 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
23792 return true;
23793 tree fn = get_callee_fndecl (expression);
23794 int i, nargs;
23795 nargs = call_expr_nargs (expression);
23796 for (i = 0; i < nargs; ++i)
23798 tree op = CALL_EXPR_ARG (expression, i);
23799 /* In a call to a constexpr member function, look through the
23800 implicit ADDR_EXPR on the object argument so that it doesn't
23801 cause the call to be considered value-dependent. We also
23802 look through it in potential_constant_expression. */
23803 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
23804 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
23805 && TREE_CODE (op) == ADDR_EXPR)
23806 op = TREE_OPERAND (op, 0);
23807 if (value_dependent_expression_p (op))
23808 return true;
23810 return false;
23813 case TEMPLATE_ID_EXPR:
23814 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
23815 type-dependent. */
23816 return type_dependent_expression_p (expression)
23817 || variable_concept_p (TREE_OPERAND (expression, 0));
23819 case CONSTRUCTOR:
23821 unsigned ix;
23822 tree val;
23823 if (dependent_type_p (TREE_TYPE (expression)))
23824 return true;
23825 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
23826 if (value_dependent_expression_p (val))
23827 return true;
23828 return false;
23831 case STMT_EXPR:
23832 /* Treat a GNU statement expression as dependent to avoid crashing
23833 under instantiate_non_dependent_expr; it can't be constant. */
23834 return true;
23836 default:
23837 /* A constant expression is value-dependent if any subexpression is
23838 value-dependent. */
23839 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
23841 case tcc_reference:
23842 case tcc_unary:
23843 case tcc_comparison:
23844 case tcc_binary:
23845 case tcc_expression:
23846 case tcc_vl_exp:
23848 int i, len = cp_tree_operand_length (expression);
23850 for (i = 0; i < len; i++)
23852 tree t = TREE_OPERAND (expression, i);
23854 /* In some cases, some of the operands may be missing.
23855 (For example, in the case of PREDECREMENT_EXPR, the
23856 amount to increment by may be missing.) That doesn't
23857 make the expression dependent. */
23858 if (t && value_dependent_expression_p (t))
23859 return true;
23862 break;
23863 default:
23864 break;
23866 break;
23869 /* The expression is not value-dependent. */
23870 return false;
23873 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
23874 [temp.dep.expr]. Note that an expression with no type is
23875 considered dependent. Other parts of the compiler arrange for an
23876 expression with type-dependent subexpressions to have no type, so
23877 this function doesn't have to be fully recursive. */
23879 bool
23880 type_dependent_expression_p (tree expression)
23882 if (!processing_template_decl)
23883 return false;
23885 if (expression == NULL_TREE || expression == error_mark_node)
23886 return false;
23888 /* An unresolved name is always dependent. */
23889 if (identifier_p (expression)
23890 || TREE_CODE (expression) == USING_DECL
23891 || TREE_CODE (expression) == WILDCARD_DECL)
23892 return true;
23894 /* A fold expression is type-dependent. */
23895 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
23896 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
23897 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
23898 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
23899 return true;
23901 /* Some expression forms are never type-dependent. */
23902 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
23903 || TREE_CODE (expression) == SIZEOF_EXPR
23904 || TREE_CODE (expression) == ALIGNOF_EXPR
23905 || TREE_CODE (expression) == AT_ENCODE_EXPR
23906 || TREE_CODE (expression) == NOEXCEPT_EXPR
23907 || TREE_CODE (expression) == TRAIT_EXPR
23908 || TREE_CODE (expression) == TYPEID_EXPR
23909 || TREE_CODE (expression) == DELETE_EXPR
23910 || TREE_CODE (expression) == VEC_DELETE_EXPR
23911 || TREE_CODE (expression) == THROW_EXPR
23912 || TREE_CODE (expression) == REQUIRES_EXPR)
23913 return false;
23915 /* The types of these expressions depends only on the type to which
23916 the cast occurs. */
23917 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
23918 || TREE_CODE (expression) == STATIC_CAST_EXPR
23919 || TREE_CODE (expression) == CONST_CAST_EXPR
23920 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
23921 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
23922 || TREE_CODE (expression) == CAST_EXPR)
23923 return dependent_type_p (TREE_TYPE (expression));
23925 /* The types of these expressions depends only on the type created
23926 by the expression. */
23927 if (TREE_CODE (expression) == NEW_EXPR
23928 || TREE_CODE (expression) == VEC_NEW_EXPR)
23930 /* For NEW_EXPR tree nodes created inside a template, either
23931 the object type itself or a TREE_LIST may appear as the
23932 operand 1. */
23933 tree type = TREE_OPERAND (expression, 1);
23934 if (TREE_CODE (type) == TREE_LIST)
23935 /* This is an array type. We need to check array dimensions
23936 as well. */
23937 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
23938 || value_dependent_expression_p
23939 (TREE_OPERAND (TREE_VALUE (type), 1));
23940 else
23941 return dependent_type_p (type);
23944 if (TREE_CODE (expression) == SCOPE_REF)
23946 tree scope = TREE_OPERAND (expression, 0);
23947 tree name = TREE_OPERAND (expression, 1);
23949 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
23950 contains an identifier associated by name lookup with one or more
23951 declarations declared with a dependent type, or...a
23952 nested-name-specifier or qualified-id that names a member of an
23953 unknown specialization. */
23954 return (type_dependent_expression_p (name)
23955 || dependent_scope_p (scope));
23958 if (TREE_CODE (expression) == TEMPLATE_DECL
23959 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
23960 return uses_outer_template_parms (expression);
23962 if (TREE_CODE (expression) == STMT_EXPR)
23963 expression = stmt_expr_value_expr (expression);
23965 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
23967 tree elt;
23968 unsigned i;
23970 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
23972 if (type_dependent_expression_p (elt))
23973 return true;
23975 return false;
23978 /* A static data member of the current instantiation with incomplete
23979 array type is type-dependent, as the definition and specializations
23980 can have different bounds. */
23981 if (VAR_P (expression)
23982 && DECL_CLASS_SCOPE_P (expression)
23983 && dependent_type_p (DECL_CONTEXT (expression))
23984 && VAR_HAD_UNKNOWN_BOUND (expression))
23985 return true;
23987 /* An array of unknown bound depending on a variadic parameter, eg:
23989 template<typename... Args>
23990 void foo (Args... args)
23992 int arr[] = { args... };
23995 template<int... vals>
23996 void bar ()
23998 int arr[] = { vals... };
24001 If the array has no length and has an initializer, it must be that
24002 we couldn't determine its length in cp_complete_array_type because
24003 it is dependent. */
24004 if (VAR_P (expression)
24005 && TREE_TYPE (expression) != NULL_TREE
24006 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
24007 && !TYPE_DOMAIN (TREE_TYPE (expression))
24008 && DECL_INITIAL (expression))
24009 return true;
24011 /* A function or variable template-id is type-dependent if it has any
24012 dependent template arguments. */
24013 if (VAR_OR_FUNCTION_DECL_P (expression)
24014 && DECL_LANG_SPECIFIC (expression)
24015 && DECL_TEMPLATE_INFO (expression))
24017 /* Consider the innermost template arguments, since those are the ones
24018 that come from the template-id; the template arguments for the
24019 enclosing class do not make it type-dependent unless they are used in
24020 the type of the decl. */
24021 if (PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression))
24022 && (any_dependent_template_arguments_p
24023 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
24024 return true;
24026 /* Otherwise, if the decl isn't from a dependent scope, it can't be
24027 type-dependent. Checking this is important for functions with auto
24028 return type, which looks like a dependent type. */
24029 if (TREE_CODE (expression) == FUNCTION_DECL
24030 && (!DECL_CLASS_SCOPE_P (expression)
24031 || !dependent_type_p (DECL_CONTEXT (expression)))
24032 && (!DECL_FRIEND_CONTEXT (expression)
24033 || !dependent_type_p (DECL_FRIEND_CONTEXT (expression)))
24034 && !DECL_LOCAL_FUNCTION_P (expression))
24036 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
24037 || undeduced_auto_decl (expression));
24038 return false;
24042 /* Always dependent, on the number of arguments if nothing else. */
24043 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
24044 return true;
24046 if (TREE_TYPE (expression) == unknown_type_node)
24048 if (TREE_CODE (expression) == ADDR_EXPR)
24049 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
24050 if (TREE_CODE (expression) == COMPONENT_REF
24051 || TREE_CODE (expression) == OFFSET_REF)
24053 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
24054 return true;
24055 expression = TREE_OPERAND (expression, 1);
24056 if (identifier_p (expression))
24057 return false;
24059 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
24060 if (TREE_CODE (expression) == SCOPE_REF)
24061 return false;
24063 if (BASELINK_P (expression))
24065 if (BASELINK_OPTYPE (expression)
24066 && dependent_type_p (BASELINK_OPTYPE (expression)))
24067 return true;
24068 expression = BASELINK_FUNCTIONS (expression);
24071 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
24073 if (any_dependent_template_arguments_p
24074 (TREE_OPERAND (expression, 1)))
24075 return true;
24076 expression = TREE_OPERAND (expression, 0);
24077 if (identifier_p (expression))
24078 return true;
24081 gcc_assert (TREE_CODE (expression) == OVERLOAD
24082 || TREE_CODE (expression) == FUNCTION_DECL);
24084 for (lkp_iterator iter (expression); iter; ++iter)
24085 if (type_dependent_expression_p (*iter))
24086 return true;
24088 return false;
24091 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
24093 /* Dependent type attributes might not have made it from the decl to
24094 the type yet. */
24095 if (DECL_P (expression)
24096 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
24097 return true;
24099 return (dependent_type_p (TREE_TYPE (expression)));
24102 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
24103 type-dependent if the expression refers to a member of the current
24104 instantiation and the type of the referenced member is dependent, or the
24105 class member access expression refers to a member of an unknown
24106 specialization.
24108 This function returns true if the OBJECT in such a class member access
24109 expression is of an unknown specialization. */
24111 bool
24112 type_dependent_object_expression_p (tree object)
24114 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
24115 dependent. */
24116 if (TREE_CODE (object) == IDENTIFIER_NODE)
24117 return true;
24118 tree scope = TREE_TYPE (object);
24119 return (!scope || dependent_scope_p (scope));
24122 /* walk_tree callback function for instantiation_dependent_expression_p,
24123 below. Returns non-zero if a dependent subexpression is found. */
24125 static tree
24126 instantiation_dependent_r (tree *tp, int *walk_subtrees,
24127 void * /*data*/)
24129 if (TYPE_P (*tp))
24131 /* We don't have to worry about decltype currently because decltype
24132 of an instantiation-dependent expr is a dependent type. This
24133 might change depending on the resolution of DR 1172. */
24134 *walk_subtrees = false;
24135 return NULL_TREE;
24137 enum tree_code code = TREE_CODE (*tp);
24138 switch (code)
24140 /* Don't treat an argument list as dependent just because it has no
24141 TREE_TYPE. */
24142 case TREE_LIST:
24143 case TREE_VEC:
24144 return NULL_TREE;
24146 case TEMPLATE_PARM_INDEX:
24147 return *tp;
24149 /* Handle expressions with type operands. */
24150 case SIZEOF_EXPR:
24151 case ALIGNOF_EXPR:
24152 case TYPEID_EXPR:
24153 case AT_ENCODE_EXPR:
24155 tree op = TREE_OPERAND (*tp, 0);
24156 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
24157 op = TREE_TYPE (op);
24158 if (TYPE_P (op))
24160 if (dependent_type_p (op))
24161 return *tp;
24162 else
24164 *walk_subtrees = false;
24165 return NULL_TREE;
24168 break;
24171 case COMPONENT_REF:
24172 if (identifier_p (TREE_OPERAND (*tp, 1)))
24173 /* In a template, finish_class_member_access_expr creates a
24174 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
24175 type-dependent, so that we can check access control at
24176 instantiation time (PR 42277). See also Core issue 1273. */
24177 return *tp;
24178 break;
24180 case SCOPE_REF:
24181 if (instantiation_dependent_scope_ref_p (*tp))
24182 return *tp;
24183 else
24184 break;
24186 /* Treat statement-expressions as dependent. */
24187 case BIND_EXPR:
24188 return *tp;
24190 /* Treat requires-expressions as dependent. */
24191 case REQUIRES_EXPR:
24192 return *tp;
24194 case CALL_EXPR:
24195 /* Treat calls to function concepts as dependent. */
24196 if (function_concept_check_p (*tp))
24197 return *tp;
24198 break;
24200 case TEMPLATE_ID_EXPR:
24201 /* And variable concepts. */
24202 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
24203 return *tp;
24204 break;
24206 default:
24207 break;
24210 if (type_dependent_expression_p (*tp))
24211 return *tp;
24212 else
24213 return NULL_TREE;
24216 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
24217 sense defined by the ABI:
24219 "An expression is instantiation-dependent if it is type-dependent
24220 or value-dependent, or it has a subexpression that is type-dependent
24221 or value-dependent."
24223 Except don't actually check value-dependence for unevaluated expressions,
24224 because in sizeof(i) we don't care about the value of i. Checking
24225 type-dependence will in turn check value-dependence of array bounds/template
24226 arguments as needed. */
24228 bool
24229 instantiation_dependent_uneval_expression_p (tree expression)
24231 tree result;
24233 if (!processing_template_decl)
24234 return false;
24236 if (expression == error_mark_node)
24237 return false;
24239 result = cp_walk_tree_without_duplicates (&expression,
24240 instantiation_dependent_r, NULL);
24241 return result != NULL_TREE;
24244 /* As above, but also check value-dependence of the expression as a whole. */
24246 bool
24247 instantiation_dependent_expression_p (tree expression)
24249 return (instantiation_dependent_uneval_expression_p (expression)
24250 || value_dependent_expression_p (expression));
24253 /* Like type_dependent_expression_p, but it also works while not processing
24254 a template definition, i.e. during substitution or mangling. */
24256 bool
24257 type_dependent_expression_p_push (tree expr)
24259 bool b;
24260 ++processing_template_decl;
24261 b = type_dependent_expression_p (expr);
24262 --processing_template_decl;
24263 return b;
24266 /* Returns TRUE if ARGS contains a type-dependent expression. */
24268 bool
24269 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
24271 unsigned int i;
24272 tree arg;
24274 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
24276 if (type_dependent_expression_p (arg))
24277 return true;
24279 return false;
24282 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24283 expressions) contains any type-dependent expressions. */
24285 bool
24286 any_type_dependent_elements_p (const_tree list)
24288 for (; list; list = TREE_CHAIN (list))
24289 if (type_dependent_expression_p (TREE_VALUE (list)))
24290 return true;
24292 return false;
24295 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24296 expressions) contains any value-dependent expressions. */
24298 bool
24299 any_value_dependent_elements_p (const_tree list)
24301 for (; list; list = TREE_CHAIN (list))
24302 if (value_dependent_expression_p (TREE_VALUE (list)))
24303 return true;
24305 return false;
24308 /* Returns TRUE if the ARG (a template argument) is dependent. */
24310 bool
24311 dependent_template_arg_p (tree arg)
24313 if (!processing_template_decl)
24314 return false;
24316 /* Assume a template argument that was wrongly written by the user
24317 is dependent. This is consistent with what
24318 any_dependent_template_arguments_p [that calls this function]
24319 does. */
24320 if (!arg || arg == error_mark_node)
24321 return true;
24323 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
24324 arg = ARGUMENT_PACK_SELECT_ARG (arg);
24326 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
24327 return true;
24328 if (TREE_CODE (arg) == TEMPLATE_DECL)
24330 if (DECL_TEMPLATE_PARM_P (arg))
24331 return true;
24332 /* A member template of a dependent class is not necessarily
24333 type-dependent, but it is a dependent template argument because it
24334 will be a member of an unknown specialization to that template. */
24335 tree scope = CP_DECL_CONTEXT (arg);
24336 return TYPE_P (scope) && dependent_type_p (scope);
24338 else if (ARGUMENT_PACK_P (arg))
24340 tree args = ARGUMENT_PACK_ARGS (arg);
24341 int i, len = TREE_VEC_LENGTH (args);
24342 for (i = 0; i < len; ++i)
24344 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
24345 return true;
24348 return false;
24350 else if (TYPE_P (arg))
24351 return dependent_type_p (arg);
24352 else
24353 return (type_dependent_expression_p (arg)
24354 || value_dependent_expression_p (arg));
24357 /* Returns true if ARGS (a collection of template arguments) contains
24358 any types that require structural equality testing. */
24360 bool
24361 any_template_arguments_need_structural_equality_p (tree args)
24363 int i;
24364 int j;
24366 if (!args)
24367 return false;
24368 if (args == error_mark_node)
24369 return true;
24371 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24373 tree level = TMPL_ARGS_LEVEL (args, i + 1);
24374 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24376 tree arg = TREE_VEC_ELT (level, j);
24377 tree packed_args = NULL_TREE;
24378 int k, len = 1;
24380 if (ARGUMENT_PACK_P (arg))
24382 /* Look inside the argument pack. */
24383 packed_args = ARGUMENT_PACK_ARGS (arg);
24384 len = TREE_VEC_LENGTH (packed_args);
24387 for (k = 0; k < len; ++k)
24389 if (packed_args)
24390 arg = TREE_VEC_ELT (packed_args, k);
24392 if (error_operand_p (arg))
24393 return true;
24394 else if (TREE_CODE (arg) == TEMPLATE_DECL)
24395 continue;
24396 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
24397 return true;
24398 else if (!TYPE_P (arg) && TREE_TYPE (arg)
24399 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
24400 return true;
24405 return false;
24408 /* Returns true if ARGS (a collection of template arguments) contains
24409 any dependent arguments. */
24411 bool
24412 any_dependent_template_arguments_p (const_tree args)
24414 int i;
24415 int j;
24417 if (!args)
24418 return false;
24419 if (args == error_mark_node)
24420 return true;
24422 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24424 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
24425 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24426 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
24427 return true;
24430 return false;
24433 /* Returns TRUE if the template TMPL is type-dependent. */
24435 bool
24436 dependent_template_p (tree tmpl)
24438 if (TREE_CODE (tmpl) == OVERLOAD)
24440 for (lkp_iterator iter (tmpl); iter; ++iter)
24441 if (dependent_template_p (*iter))
24442 return true;
24443 return false;
24446 /* Template template parameters are dependent. */
24447 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
24448 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
24449 return true;
24450 /* So are names that have not been looked up. */
24451 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
24452 return true;
24453 return false;
24456 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
24458 bool
24459 dependent_template_id_p (tree tmpl, tree args)
24461 return (dependent_template_p (tmpl)
24462 || any_dependent_template_arguments_p (args));
24465 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
24466 are dependent. */
24468 bool
24469 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
24471 int i;
24473 if (!processing_template_decl)
24474 return false;
24476 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
24478 tree decl = TREE_VEC_ELT (declv, i);
24479 tree init = TREE_VEC_ELT (initv, i);
24480 tree cond = TREE_VEC_ELT (condv, i);
24481 tree incr = TREE_VEC_ELT (incrv, i);
24483 if (type_dependent_expression_p (decl)
24484 || TREE_CODE (decl) == SCOPE_REF)
24485 return true;
24487 if (init && type_dependent_expression_p (init))
24488 return true;
24490 if (type_dependent_expression_p (cond))
24491 return true;
24493 if (COMPARISON_CLASS_P (cond)
24494 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
24495 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
24496 return true;
24498 if (TREE_CODE (incr) == MODOP_EXPR)
24500 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
24501 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
24502 return true;
24504 else if (type_dependent_expression_p (incr))
24505 return true;
24506 else if (TREE_CODE (incr) == MODIFY_EXPR)
24508 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
24509 return true;
24510 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
24512 tree t = TREE_OPERAND (incr, 1);
24513 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
24514 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
24515 return true;
24520 return false;
24523 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
24524 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
24525 no such TYPE can be found. Note that this function peers inside
24526 uninstantiated templates and therefore should be used only in
24527 extremely limited situations. ONLY_CURRENT_P restricts this
24528 peering to the currently open classes hierarchy (which is required
24529 when comparing types). */
24531 tree
24532 resolve_typename_type (tree type, bool only_current_p)
24534 tree scope;
24535 tree name;
24536 tree decl;
24537 int quals;
24538 tree pushed_scope;
24539 tree result;
24541 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
24543 scope = TYPE_CONTEXT (type);
24544 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
24545 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
24546 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
24547 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
24548 identifier of the TYPENAME_TYPE anymore.
24549 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
24550 TYPENAME_TYPE instead, we avoid messing up with a possible
24551 typedef variant case. */
24552 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
24554 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
24555 it first before we can figure out what NAME refers to. */
24556 if (TREE_CODE (scope) == TYPENAME_TYPE)
24558 if (TYPENAME_IS_RESOLVING_P (scope))
24559 /* Given a class template A with a dependent base with nested type C,
24560 typedef typename A::C::C C will land us here, as trying to resolve
24561 the initial A::C leads to the local C typedef, which leads back to
24562 A::C::C. So we break the recursion now. */
24563 return type;
24564 else
24565 scope = resolve_typename_type (scope, only_current_p);
24567 /* If we don't know what SCOPE refers to, then we cannot resolve the
24568 TYPENAME_TYPE. */
24569 if (!CLASS_TYPE_P (scope))
24570 return type;
24571 /* If this is a typedef, we don't want to look inside (c++/11987). */
24572 if (typedef_variant_p (type))
24573 return type;
24574 /* If SCOPE isn't the template itself, it will not have a valid
24575 TYPE_FIELDS list. */
24576 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
24577 /* scope is either the template itself or a compatible instantiation
24578 like X<T>, so look up the name in the original template. */
24579 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
24580 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
24581 gcc_checking_assert (uses_template_parms (scope));
24582 /* If scope has no fields, it can't be a current instantiation. Check this
24583 before currently_open_class to avoid infinite recursion (71515). */
24584 if (!TYPE_FIELDS (scope))
24585 return type;
24586 /* If the SCOPE is not the current instantiation, there's no reason
24587 to look inside it. */
24588 if (only_current_p && !currently_open_class (scope))
24589 return type;
24590 /* Enter the SCOPE so that name lookup will be resolved as if we
24591 were in the class definition. In particular, SCOPE will no
24592 longer be considered a dependent type. */
24593 pushed_scope = push_scope (scope);
24594 /* Look up the declaration. */
24595 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
24596 tf_warning_or_error);
24598 result = NULL_TREE;
24600 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
24601 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
24602 if (!decl)
24603 /*nop*/;
24604 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type))
24605 && TREE_CODE (decl) == TYPE_DECL)
24607 result = TREE_TYPE (decl);
24608 if (result == error_mark_node)
24609 result = NULL_TREE;
24611 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
24612 && DECL_CLASS_TEMPLATE_P (decl))
24614 tree tmpl;
24615 tree args;
24616 /* Obtain the template and the arguments. */
24617 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
24618 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
24619 /* Instantiate the template. */
24620 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
24621 /*entering_scope=*/0,
24622 tf_error | tf_user);
24623 if (result == error_mark_node)
24624 result = NULL_TREE;
24627 /* Leave the SCOPE. */
24628 if (pushed_scope)
24629 pop_scope (pushed_scope);
24631 /* If we failed to resolve it, return the original typename. */
24632 if (!result)
24633 return type;
24635 /* If lookup found a typename type, resolve that too. */
24636 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
24638 /* Ill-formed programs can cause infinite recursion here, so we
24639 must catch that. */
24640 TYPENAME_IS_RESOLVING_P (result) = 1;
24641 result = resolve_typename_type (result, only_current_p);
24642 TYPENAME_IS_RESOLVING_P (result) = 0;
24645 /* Qualify the resulting type. */
24646 quals = cp_type_quals (type);
24647 if (quals)
24648 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
24650 return result;
24653 /* EXPR is an expression which is not type-dependent. Return a proxy
24654 for EXPR that can be used to compute the types of larger
24655 expressions containing EXPR. */
24657 tree
24658 build_non_dependent_expr (tree expr)
24660 tree inner_expr;
24662 /* When checking, try to get a constant value for all non-dependent
24663 expressions in order to expose bugs in *_dependent_expression_p
24664 and constexpr. This can affect code generation, see PR70704, so
24665 only do this for -fchecking=2. */
24666 if (flag_checking > 1
24667 && cxx_dialect >= cxx11
24668 /* Don't do this during nsdmi parsing as it can lead to
24669 unexpected recursive instantiations. */
24670 && !parsing_nsdmi ()
24671 /* Don't do this during concept expansion either and for
24672 the same reason. */
24673 && !expanding_concept ())
24674 fold_non_dependent_expr (expr);
24676 /* Preserve OVERLOADs; the functions must be available to resolve
24677 types. */
24678 inner_expr = expr;
24679 if (TREE_CODE (inner_expr) == STMT_EXPR)
24680 inner_expr = stmt_expr_value_expr (inner_expr);
24681 if (TREE_CODE (inner_expr) == ADDR_EXPR)
24682 inner_expr = TREE_OPERAND (inner_expr, 0);
24683 if (TREE_CODE (inner_expr) == COMPONENT_REF)
24684 inner_expr = TREE_OPERAND (inner_expr, 1);
24685 if (is_overloaded_fn (inner_expr)
24686 || TREE_CODE (inner_expr) == OFFSET_REF)
24687 return expr;
24688 /* There is no need to return a proxy for a variable. */
24689 if (VAR_P (expr))
24690 return expr;
24691 /* Preserve string constants; conversions from string constants to
24692 "char *" are allowed, even though normally a "const char *"
24693 cannot be used to initialize a "char *". */
24694 if (TREE_CODE (expr) == STRING_CST)
24695 return expr;
24696 /* Preserve void and arithmetic constants, as an optimization -- there is no
24697 reason to create a new node. */
24698 if (TREE_CODE (expr) == VOID_CST
24699 || TREE_CODE (expr) == INTEGER_CST
24700 || TREE_CODE (expr) == REAL_CST)
24701 return expr;
24702 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
24703 There is at least one place where we want to know that a
24704 particular expression is a throw-expression: when checking a ?:
24705 expression, there are special rules if the second or third
24706 argument is a throw-expression. */
24707 if (TREE_CODE (expr) == THROW_EXPR)
24708 return expr;
24710 /* Don't wrap an initializer list, we need to be able to look inside. */
24711 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
24712 return expr;
24714 /* Don't wrap a dummy object, we need to be able to test for it. */
24715 if (is_dummy_object (expr))
24716 return expr;
24718 if (TREE_CODE (expr) == COND_EXPR)
24719 return build3 (COND_EXPR,
24720 TREE_TYPE (expr),
24721 TREE_OPERAND (expr, 0),
24722 (TREE_OPERAND (expr, 1)
24723 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
24724 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
24725 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
24726 if (TREE_CODE (expr) == COMPOUND_EXPR
24727 && !COMPOUND_EXPR_OVERLOADED (expr))
24728 return build2 (COMPOUND_EXPR,
24729 TREE_TYPE (expr),
24730 TREE_OPERAND (expr, 0),
24731 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
24733 /* If the type is unknown, it can't really be non-dependent */
24734 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
24736 /* Otherwise, build a NON_DEPENDENT_EXPR. */
24737 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
24740 /* ARGS is a vector of expressions as arguments to a function call.
24741 Replace the arguments with equivalent non-dependent expressions.
24742 This modifies ARGS in place. */
24744 void
24745 make_args_non_dependent (vec<tree, va_gc> *args)
24747 unsigned int ix;
24748 tree arg;
24750 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
24752 tree newarg = build_non_dependent_expr (arg);
24753 if (newarg != arg)
24754 (*args)[ix] = newarg;
24758 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
24759 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
24760 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
24762 static tree
24763 make_auto_1 (tree name, bool set_canonical)
24765 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
24766 TYPE_NAME (au) = build_decl (input_location,
24767 TYPE_DECL, name, au);
24768 TYPE_STUB_DECL (au) = TYPE_NAME (au);
24769 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
24770 (0, processing_template_decl + 1, processing_template_decl + 1,
24771 TYPE_NAME (au), NULL_TREE);
24772 if (set_canonical)
24773 TYPE_CANONICAL (au) = canonical_type_parameter (au);
24774 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
24775 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
24777 return au;
24780 tree
24781 make_decltype_auto (void)
24783 return make_auto_1 (decltype_auto_identifier, true);
24786 tree
24787 make_auto (void)
24789 return make_auto_1 (auto_identifier, true);
24792 /* Return a C++17 deduction placeholder for class template TMPL. */
24794 tree
24795 make_template_placeholder (tree tmpl)
24797 tree t = make_auto_1 (DECL_NAME (tmpl), true);
24798 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
24799 return t;
24802 /* Make a "constrained auto" type-specifier. This is an
24803 auto type with constraints that must be associated after
24804 deduction. The constraint is formed from the given
24805 CONC and its optional sequence of arguments, which are
24806 non-null if written as partial-concept-id. */
24808 tree
24809 make_constrained_auto (tree con, tree args)
24811 tree type = make_auto_1 (auto_identifier, false);
24813 /* Build the constraint. */
24814 tree tmpl = DECL_TI_TEMPLATE (con);
24815 tree expr = VAR_P (con) ? tmpl : ovl_make (tmpl);
24816 expr = build_concept_check (expr, type, args);
24818 tree constr = normalize_expression (expr);
24819 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
24821 /* Our canonical type depends on the constraint. */
24822 TYPE_CANONICAL (type) = canonical_type_parameter (type);
24824 /* Attach the constraint to the type declaration. */
24825 tree decl = TYPE_NAME (type);
24826 return decl;
24829 /* Given type ARG, return std::initializer_list<ARG>. */
24831 static tree
24832 listify (tree arg)
24834 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
24836 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
24838 error ("deducing from brace-enclosed initializer list requires "
24839 "#include <initializer_list>");
24840 return error_mark_node;
24842 tree argvec = make_tree_vec (1);
24843 TREE_VEC_ELT (argvec, 0) = arg;
24845 return lookup_template_class (std_init_list, argvec, NULL_TREE,
24846 NULL_TREE, 0, tf_warning_or_error);
24849 /* Replace auto in TYPE with std::initializer_list<auto>. */
24851 static tree
24852 listify_autos (tree type, tree auto_node)
24854 tree init_auto = listify (auto_node);
24855 tree argvec = make_tree_vec (1);
24856 TREE_VEC_ELT (argvec, 0) = init_auto;
24857 if (processing_template_decl)
24858 argvec = add_to_template_args (current_template_args (), argvec);
24859 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
24862 /* Hash traits for hashing possibly constrained 'auto'
24863 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
24865 struct auto_hash : default_hash_traits<tree>
24867 static inline hashval_t hash (tree);
24868 static inline bool equal (tree, tree);
24871 /* Hash the 'auto' T. */
24873 inline hashval_t
24874 auto_hash::hash (tree t)
24876 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
24877 /* Matching constrained-type-specifiers denote the same template
24878 parameter, so hash the constraint. */
24879 return hash_placeholder_constraint (c);
24880 else
24881 /* But unconstrained autos are all separate, so just hash the pointer. */
24882 return iterative_hash_object (t, 0);
24885 /* Compare two 'auto's. */
24887 inline bool
24888 auto_hash::equal (tree t1, tree t2)
24890 if (t1 == t2)
24891 return true;
24893 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
24894 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
24896 /* Two unconstrained autos are distinct. */
24897 if (!c1 || !c2)
24898 return false;
24900 return equivalent_placeholder_constraints (c1, c2);
24903 /* for_each_template_parm callback for extract_autos: if t is a (possibly
24904 constrained) auto, add it to the vector. */
24906 static int
24907 extract_autos_r (tree t, void *data)
24909 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
24910 if (is_auto (t))
24912 /* All the autos were built with index 0; fix that up now. */
24913 tree *p = hash.find_slot (t, INSERT);
24914 unsigned idx;
24915 if (*p)
24916 /* If this is a repeated constrained-type-specifier, use the index we
24917 chose before. */
24918 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
24919 else
24921 /* Otherwise this is new, so use the current count. */
24922 *p = t;
24923 idx = hash.elements () - 1;
24925 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
24928 /* Always keep walking. */
24929 return 0;
24932 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
24933 says they can appear anywhere in the type. */
24935 static tree
24936 extract_autos (tree type)
24938 hash_set<tree> visited;
24939 hash_table<auto_hash> hash (2);
24941 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
24943 tree tree_vec = make_tree_vec (hash.elements());
24944 for (hash_table<auto_hash>::iterator iter = hash.begin();
24945 iter != hash.end(); ++iter)
24947 tree elt = *iter;
24948 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
24949 TREE_VEC_ELT (tree_vec, i)
24950 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
24953 return tree_vec;
24956 /* The stem for deduction guide names. */
24957 const char *const dguide_base = "__dguide_";
24959 /* Return the name for a deduction guide for class template TMPL. */
24961 tree
24962 dguide_name (tree tmpl)
24964 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
24965 tree tname = TYPE_IDENTIFIER (type);
24966 char *buf = (char *) alloca (1 + strlen (dguide_base)
24967 + IDENTIFIER_LENGTH (tname));
24968 memcpy (buf, dguide_base, strlen (dguide_base));
24969 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
24970 IDENTIFIER_LENGTH (tname) + 1);
24971 tree dname = get_identifier (buf);
24972 TREE_TYPE (dname) = type;
24973 return dname;
24976 /* True if NAME is the name of a deduction guide. */
24978 bool
24979 dguide_name_p (tree name)
24981 return (TREE_TYPE (name)
24982 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
24983 strlen (dguide_base)));
24986 /* True if FN is a deduction guide. */
24988 bool
24989 deduction_guide_p (const_tree fn)
24991 if (DECL_P (fn))
24992 if (tree name = DECL_NAME (fn))
24993 return dguide_name_p (name);
24994 return false;
24997 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
24999 bool
25000 copy_guide_p (const_tree fn)
25002 gcc_assert (deduction_guide_p (fn));
25003 if (!DECL_ARTIFICIAL (fn))
25004 return false;
25005 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
25006 return (TREE_CHAIN (parms) == void_list_node
25007 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
25010 /* True if FN is a guide generated from a constructor template. */
25012 bool
25013 template_guide_p (const_tree fn)
25015 gcc_assert (deduction_guide_p (fn));
25016 if (!DECL_ARTIFICIAL (fn))
25017 return false;
25018 if (tree ctor = DECL_ABSTRACT_ORIGIN (fn))
25020 tree tmpl = DECL_TI_TEMPLATE (ctor);
25021 return PRIMARY_TEMPLATE_P (tmpl);
25023 return false;
25026 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
25027 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
25028 template parameter types. Note that the handling of template template
25029 parameters relies on current_template_parms being set appropriately for the
25030 new template. */
25032 static tree
25033 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
25034 tree tsubst_args, tsubst_flags_t complain)
25036 tree oldidx = get_template_parm_index (olddecl);
25038 tree newtype;
25039 if (TREE_CODE (olddecl) == TYPE_DECL
25040 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25042 tree oldtype = TREE_TYPE (olddecl);
25043 newtype = cxx_make_type (TREE_CODE (oldtype));
25044 TYPE_MAIN_VARIANT (newtype) = newtype;
25045 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
25046 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
25047 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
25049 else
25050 newtype = tsubst (TREE_TYPE (olddecl), tsubst_args,
25051 complain, NULL_TREE);
25053 tree newdecl
25054 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
25055 DECL_NAME (olddecl), newtype);
25056 SET_DECL_TEMPLATE_PARM_P (newdecl);
25058 tree newidx;
25059 if (TREE_CODE (olddecl) == TYPE_DECL
25060 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25062 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
25063 = build_template_parm_index (index, level, level,
25064 newdecl, newtype);
25065 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25066 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25067 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
25068 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
25070 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
25072 DECL_TEMPLATE_RESULT (newdecl)
25073 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
25074 DECL_NAME (olddecl), newtype);
25075 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
25076 // First create a copy (ttargs) of tsubst_args with an
25077 // additional level for the template template parameter's own
25078 // template parameters (ttparms).
25079 tree ttparms = (INNERMOST_TEMPLATE_PARMS
25080 (DECL_TEMPLATE_PARMS (olddecl)));
25081 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
25082 tree ttargs = make_tree_vec (depth + 1);
25083 for (int i = 0; i < depth; ++i)
25084 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
25085 TREE_VEC_ELT (ttargs, depth)
25086 = template_parms_level_to_args (ttparms);
25087 // Substitute ttargs into ttparms to fix references to
25088 // other template parameters.
25089 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25090 complain);
25091 // Now substitute again with args based on tparms, to reduce
25092 // the level of the ttparms.
25093 ttargs = current_template_args ();
25094 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25095 complain);
25096 // Finally, tack the adjusted parms onto tparms.
25097 ttparms = tree_cons (size_int (depth), ttparms,
25098 current_template_parms);
25099 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
25102 else
25104 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
25105 tree newconst
25106 = build_decl (DECL_SOURCE_LOCATION (oldconst),
25107 TREE_CODE (oldconst),
25108 DECL_NAME (oldconst), newtype);
25109 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
25110 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
25111 SET_DECL_TEMPLATE_PARM_P (newconst);
25112 newidx = build_template_parm_index (index, level, level,
25113 newconst, newtype);
25114 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25115 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25116 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
25119 return newdecl;
25122 /* Returns a C++17 class deduction guide template based on the constructor
25123 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
25124 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
25126 static tree
25127 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
25129 tree type, tparms, targs, fparms, fargs, ci;
25130 bool memtmpl = false;
25131 bool explicit_p;
25132 location_t loc;
25134 if (TYPE_P (ctor))
25136 type = ctor;
25137 bool copy_p = TREE_CODE (type) == REFERENCE_TYPE;
25138 if (copy_p)
25140 type = TREE_TYPE (type);
25141 fparms = tree_cons (NULL_TREE, type, void_list_node);
25143 else
25144 fparms = void_list_node;
25146 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
25147 tparms = DECL_TEMPLATE_PARMS (ctmpl);
25148 targs = CLASSTYPE_TI_ARGS (type);
25149 ci = NULL_TREE;
25150 fargs = NULL_TREE;
25151 loc = DECL_SOURCE_LOCATION (ctmpl);
25152 explicit_p = false;
25154 else
25156 if (outer_args)
25157 ctor = tsubst (ctor, outer_args, complain, ctor);
25158 type = DECL_CONTEXT (ctor);
25159 tree fn_tmpl;
25160 if (TREE_CODE (ctor) == TEMPLATE_DECL)
25162 fn_tmpl = ctor;
25163 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
25165 else
25166 fn_tmpl = DECL_TI_TEMPLATE (ctor);
25168 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
25169 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
25170 fully specialized args for the enclosing class. Strip those off, as
25171 the deduction guide won't have those template parameters. */
25172 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
25173 TMPL_PARMS_DEPTH (tparms));
25174 /* Discard the 'this' parameter. */
25175 fparms = FUNCTION_ARG_CHAIN (ctor);
25176 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
25177 ci = get_constraints (ctor);
25178 loc = DECL_SOURCE_LOCATION (ctor);
25179 explicit_p = DECL_NONCONVERTING_P (ctor);
25181 if (PRIMARY_TEMPLATE_P (fn_tmpl))
25183 memtmpl = true;
25185 /* For a member template constructor, we need to flatten the two
25186 template parameter lists into one, and then adjust the function
25187 signature accordingly. This gets...complicated. */
25188 ++processing_template_decl;
25189 tree save_parms = current_template_parms;
25191 /* For a member template we should have two levels of parms/args, one
25192 for the class and one for the constructor. We stripped
25193 specialized args for further enclosing classes above. */
25194 const int depth = 2;
25195 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
25197 /* Template args for translating references to the two-level template
25198 parameters into references to the one-level template parameters we
25199 are creating. */
25200 tree tsubst_args = copy_node (targs);
25201 TMPL_ARGS_LEVEL (tsubst_args, depth)
25202 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
25204 /* Template parms for the constructor template. */
25205 tree ftparms = TREE_VALUE (tparms);
25206 unsigned flen = TREE_VEC_LENGTH (ftparms);
25207 /* Template parms for the class template. */
25208 tparms = TREE_CHAIN (tparms);
25209 tree ctparms = TREE_VALUE (tparms);
25210 unsigned clen = TREE_VEC_LENGTH (ctparms);
25211 /* Template parms for the deduction guide start as a copy of the
25212 template parms for the class. We set current_template_parms for
25213 lookup_template_class_1. */
25214 current_template_parms = tparms = copy_node (tparms);
25215 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
25216 for (unsigned i = 0; i < clen; ++i)
25217 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
25219 /* Now we need to rewrite the constructor parms to append them to the
25220 class parms. */
25221 for (unsigned i = 0; i < flen; ++i)
25223 unsigned index = i + clen;
25224 unsigned level = 1;
25225 tree oldelt = TREE_VEC_ELT (ftparms, i);
25226 tree olddecl = TREE_VALUE (oldelt);
25227 tree newdecl = rewrite_template_parm (olddecl, index, level,
25228 tsubst_args, complain);
25229 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
25230 tsubst_args, complain, ctor);
25231 tree list = build_tree_list (newdef, newdecl);
25232 TEMPLATE_PARM_CONSTRAINTS (list)
25233 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
25234 tsubst_args, complain, ctor);
25235 TREE_VEC_ELT (new_vec, index) = list;
25236 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
25239 /* Now we have a final set of template parms to substitute into the
25240 function signature. */
25241 targs = template_parms_to_args (tparms);
25242 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
25243 complain, ctor);
25244 fargs = tsubst (fargs, tsubst_args, complain, ctor);
25245 if (ci)
25246 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
25248 current_template_parms = save_parms;
25249 --processing_template_decl;
25253 if (!memtmpl)
25255 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
25256 tparms = copy_node (tparms);
25257 INNERMOST_TEMPLATE_PARMS (tparms)
25258 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
25261 tree fntype = build_function_type (type, fparms);
25262 tree ded_fn = build_lang_decl_loc (loc,
25263 FUNCTION_DECL,
25264 dguide_name (type), fntype);
25265 DECL_ARGUMENTS (ded_fn) = fargs;
25266 DECL_ARTIFICIAL (ded_fn) = true;
25267 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
25268 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
25269 DECL_ARTIFICIAL (ded_tmpl) = true;
25270 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
25271 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
25272 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
25273 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
25274 if (DECL_P (ctor))
25275 DECL_ABSTRACT_ORIGIN (ded_fn) = ctor;
25276 if (ci)
25277 set_constraints (ded_tmpl, ci);
25279 return ded_tmpl;
25282 /* Deduce template arguments for the class template placeholder PTYPE for
25283 template TMPL based on the initializer INIT, and return the resulting
25284 type. */
25286 static tree
25287 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
25288 tsubst_flags_t complain)
25290 if (!DECL_CLASS_TEMPLATE_P (tmpl))
25292 /* We should have handled this in the caller. */
25293 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
25294 return ptype;
25295 if (complain & tf_error)
25296 error ("non-class template %qT used without template arguments", tmpl);
25297 return error_mark_node;
25300 tree type = TREE_TYPE (tmpl);
25302 vec<tree,va_gc> *args;
25303 if (init == NULL_TREE
25304 || TREE_CODE (init) == TREE_LIST)
25305 args = make_tree_vector_from_list (init);
25306 else if (BRACE_ENCLOSED_INITIALIZER_P (init)
25307 && !TYPE_HAS_LIST_CTOR (type)
25308 && !is_std_init_list (type))
25309 args = make_tree_vector_from_ctor (init);
25310 else
25311 args = make_tree_vector_single (init);
25313 tree dname = dguide_name (tmpl);
25314 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
25315 /*type*/false, /*complain*/false,
25316 /*hidden*/false);
25317 bool elided = false;
25318 if (cands == error_mark_node)
25319 cands = NULL_TREE;
25321 /* Prune explicit deduction guides in copy-initialization context. */
25322 if (flags & LOOKUP_ONLYCONVERTING)
25324 for (lkp_iterator iter (cands); !elided && iter; ++iter)
25325 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25326 elided = true;
25328 if (elided)
25330 /* Found a nonconverting guide, prune the candidates. */
25331 tree pruned = NULL_TREE;
25332 for (lkp_iterator iter (cands); iter; ++iter)
25333 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25334 pruned = lookup_add (*iter, pruned);
25336 cands = pruned;
25340 tree outer_args = NULL_TREE;
25341 if (DECL_CLASS_SCOPE_P (tmpl)
25342 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl)))
25344 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
25345 type = TREE_TYPE (most_general_template (tmpl));
25348 bool saw_ctor = false;
25349 if (CLASSTYPE_METHOD_VEC (type))
25350 // FIXME cache artificial deduction guides
25351 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type));
25352 iter; ++iter)
25354 tree guide = build_deduction_guide (*iter, outer_args, complain);
25355 if ((flags & LOOKUP_ONLYCONVERTING)
25356 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
25357 elided = true;
25358 else
25359 cands = lookup_add (guide, cands);
25361 saw_ctor = true;
25364 if (args->length () < 2)
25366 tree gtype = NULL_TREE;
25368 if (args->length () == 1)
25369 gtype = build_reference_type (type);
25370 else if (!saw_ctor)
25371 gtype = type;
25373 if (gtype)
25375 tree guide = build_deduction_guide (gtype, outer_args, complain);
25376 if ((flags & LOOKUP_ONLYCONVERTING)
25377 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
25378 elided = true;
25379 else
25380 cands = lookup_add (guide, cands);
25384 if (elided && !cands)
25386 error ("cannot deduce template arguments for copy-initialization"
25387 " of %qT, as it has no non-explicit deduction guides or "
25388 "user-declared constructors", type);
25389 return error_mark_node;
25392 ++cp_unevaluated_operand;
25393 tree t = build_new_function_call (cands, &args, tf_decltype);
25395 if (t == error_mark_node && (complain & tf_warning_or_error))
25397 error ("class template argument deduction failed:");
25398 t = build_new_function_call (cands, &args, complain | tf_decltype);
25399 if (elided)
25400 inform (input_location, "explicit deduction guides not considered "
25401 "for copy-initialization");
25404 --cp_unevaluated_operand;
25405 release_tree_vector (args);
25407 return cp_build_qualified_type (TREE_TYPE (t), cp_type_quals (ptype));
25410 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25411 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
25413 tree
25414 do_auto_deduction (tree type, tree init, tree auto_node)
25416 return do_auto_deduction (type, init, auto_node,
25417 tf_warning_or_error,
25418 adc_unspecified);
25421 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25422 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
25423 The CONTEXT determines the context in which auto deduction is performed
25424 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
25425 OUTER_TARGS are used during template argument deduction
25426 (context == adc_unify) to properly substitute the result, and is ignored
25427 in other contexts.
25429 For partial-concept-ids, extra args may be appended to the list of deduced
25430 template arguments prior to determining constraint satisfaction. */
25432 tree
25433 do_auto_deduction (tree type, tree init, tree auto_node,
25434 tsubst_flags_t complain, auto_deduction_context context,
25435 tree outer_targs, int flags)
25437 tree targs;
25439 if (init == error_mark_node)
25440 return error_mark_node;
25442 if (init && type_dependent_expression_p (init)
25443 && context != adc_unify)
25444 /* Defining a subset of type-dependent expressions that we can deduce
25445 from ahead of time isn't worth the trouble. */
25446 return type;
25448 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
25449 /* C++17 class template argument deduction. */
25450 return do_class_deduction (type, tmpl, init, flags, complain);
25452 if (TREE_TYPE (init) == NULL_TREE)
25453 /* Nothing we can do with this, even in deduction context. */
25454 return type;
25456 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
25457 with either a new invented type template parameter U or, if the
25458 initializer is a braced-init-list (8.5.4), with
25459 std::initializer_list<U>. */
25460 if (BRACE_ENCLOSED_INITIALIZER_P (init))
25462 if (!DIRECT_LIST_INIT_P (init))
25463 type = listify_autos (type, auto_node);
25464 else if (CONSTRUCTOR_NELTS (init) == 1)
25465 init = CONSTRUCTOR_ELT (init, 0)->value;
25466 else
25468 if (complain & tf_warning_or_error)
25470 if (permerror (input_location, "direct-list-initialization of "
25471 "%<auto%> requires exactly one element"))
25472 inform (input_location,
25473 "for deduction to %<std::initializer_list%>, use copy-"
25474 "list-initialization (i.e. add %<=%> before the %<{%>)");
25476 type = listify_autos (type, auto_node);
25480 if (type == error_mark_node)
25481 return error_mark_node;
25483 init = resolve_nondeduced_context (init, complain);
25485 if (context == adc_decomp_type
25486 && auto_node == type
25487 && init != error_mark_node
25488 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
25489 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
25490 and initializer has array type, deduce cv-qualified array type. */
25491 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
25492 complain);
25493 else if (AUTO_IS_DECLTYPE (auto_node))
25495 bool id = (DECL_P (init)
25496 || ((TREE_CODE (init) == COMPONENT_REF
25497 || TREE_CODE (init) == SCOPE_REF)
25498 && !REF_PARENTHESIZED_P (init)));
25499 targs = make_tree_vec (1);
25500 TREE_VEC_ELT (targs, 0)
25501 = finish_decltype_type (init, id, tf_warning_or_error);
25502 if (type != auto_node)
25504 if (complain & tf_error)
25505 error ("%qT as type rather than plain %<decltype(auto)%>", type);
25506 return error_mark_node;
25509 else
25511 tree parms = build_tree_list (NULL_TREE, type);
25512 tree tparms;
25514 if (flag_concepts)
25515 tparms = extract_autos (type);
25516 else
25518 tparms = make_tree_vec (1);
25519 TREE_VEC_ELT (tparms, 0)
25520 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
25523 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25524 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
25525 DEDUCE_CALL, LOOKUP_NORMAL,
25526 NULL, /*explain_p=*/false);
25527 if (val > 0)
25529 if (processing_template_decl)
25530 /* Try again at instantiation time. */
25531 return type;
25532 if (type && type != error_mark_node
25533 && (complain & tf_error))
25534 /* If type is error_mark_node a diagnostic must have been
25535 emitted by now. Also, having a mention to '<type error>'
25536 in the diagnostic is not really useful to the user. */
25538 if (cfun && auto_node == current_function_auto_return_pattern
25539 && LAMBDA_FUNCTION_P (current_function_decl))
25540 error ("unable to deduce lambda return type from %qE", init);
25541 else
25542 error ("unable to deduce %qT from %qE", type, init);
25543 type_unification_real (tparms, targs, parms, &init, 1, 0,
25544 DEDUCE_CALL, LOOKUP_NORMAL,
25545 NULL, /*explain_p=*/true);
25547 return error_mark_node;
25551 /* Check any placeholder constraints against the deduced type. */
25552 if (flag_concepts && !processing_template_decl)
25553 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (auto_node))
25555 /* Use the deduced type to check the associated constraints. If we
25556 have a partial-concept-id, rebuild the argument list so that
25557 we check using the extra arguments. */
25558 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
25559 tree cargs = CHECK_CONSTR_ARGS (constr);
25560 if (TREE_VEC_LENGTH (cargs) > 1)
25562 cargs = copy_node (cargs);
25563 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
25565 else
25566 cargs = targs;
25567 if (!constraints_satisfied_p (constr, cargs))
25569 if (complain & tf_warning_or_error)
25571 switch (context)
25573 case adc_unspecified:
25574 case adc_unify:
25575 error("placeholder constraints not satisfied");
25576 break;
25577 case adc_variable_type:
25578 case adc_decomp_type:
25579 error ("deduced initializer does not satisfy "
25580 "placeholder constraints");
25581 break;
25582 case adc_return_type:
25583 error ("deduced return type does not satisfy "
25584 "placeholder constraints");
25585 break;
25586 case adc_requirement:
25587 error ("deduced expression type does not satisfy "
25588 "placeholder constraints");
25589 break;
25591 diagnose_constraints (input_location, constr, targs);
25593 return error_mark_node;
25597 if (processing_template_decl && context != adc_unify)
25598 outer_targs = current_template_args ();
25599 targs = add_to_template_args (outer_targs, targs);
25600 return tsubst (type, targs, complain, NULL_TREE);
25603 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
25604 result. */
25606 tree
25607 splice_late_return_type (tree type, tree late_return_type)
25609 if (is_auto (type))
25611 if (late_return_type)
25612 return late_return_type;
25614 tree idx = get_template_parm_index (type);
25615 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
25616 /* In an abbreviated function template we didn't know we were dealing
25617 with a function template when we saw the auto return type, so update
25618 it to have the correct level. */
25619 return make_auto_1 (TYPE_IDENTIFIER (type), true);
25621 return type;
25624 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
25625 'decltype(auto)' or a deduced class template. */
25627 bool
25628 is_auto (const_tree type)
25630 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
25631 && (TYPE_IDENTIFIER (type) == auto_identifier
25632 || TYPE_IDENTIFIER (type) == decltype_auto_identifier
25633 || CLASS_PLACEHOLDER_TEMPLATE (type)))
25634 return true;
25635 else
25636 return false;
25639 /* for_each_template_parm callback for type_uses_auto. */
25642 is_auto_r (tree tp, void */*data*/)
25644 return is_auto (tp);
25647 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
25648 a use of `auto'. Returns NULL_TREE otherwise. */
25650 tree
25651 type_uses_auto (tree type)
25653 if (type == NULL_TREE)
25654 return NULL_TREE;
25655 else if (flag_concepts)
25657 /* The Concepts TS allows multiple autos in one type-specifier; just
25658 return the first one we find, do_auto_deduction will collect all of
25659 them. */
25660 if (uses_template_parms (type))
25661 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
25662 /*visited*/NULL, /*nondeduced*/true);
25663 else
25664 return NULL_TREE;
25666 else
25667 return find_type_usage (type, is_auto);
25670 /* For a given template T, return the vector of typedefs referenced
25671 in T for which access check is needed at T instantiation time.
25672 T is either a FUNCTION_DECL or a RECORD_TYPE.
25673 Those typedefs were added to T by the function
25674 append_type_to_template_for_access_check. */
25676 vec<qualified_typedef_usage_t, va_gc> *
25677 get_types_needing_access_check (tree t)
25679 tree ti;
25680 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
25682 if (!t || t == error_mark_node)
25683 return NULL;
25685 if (!(ti = get_template_info (t)))
25686 return NULL;
25688 if (CLASS_TYPE_P (t)
25689 || TREE_CODE (t) == FUNCTION_DECL)
25691 if (!TI_TEMPLATE (ti))
25692 return NULL;
25694 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
25697 return result;
25700 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
25701 tied to T. That list of typedefs will be access checked at
25702 T instantiation time.
25703 T is either a FUNCTION_DECL or a RECORD_TYPE.
25704 TYPE_DECL is a TYPE_DECL node representing a typedef.
25705 SCOPE is the scope through which TYPE_DECL is accessed.
25706 LOCATION is the location of the usage point of TYPE_DECL.
25708 This function is a subroutine of
25709 append_type_to_template_for_access_check. */
25711 static void
25712 append_type_to_template_for_access_check_1 (tree t,
25713 tree type_decl,
25714 tree scope,
25715 location_t location)
25717 qualified_typedef_usage_t typedef_usage;
25718 tree ti;
25720 if (!t || t == error_mark_node)
25721 return;
25723 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
25724 || CLASS_TYPE_P (t))
25725 && type_decl
25726 && TREE_CODE (type_decl) == TYPE_DECL
25727 && scope);
25729 if (!(ti = get_template_info (t)))
25730 return;
25732 gcc_assert (TI_TEMPLATE (ti));
25734 typedef_usage.typedef_decl = type_decl;
25735 typedef_usage.context = scope;
25736 typedef_usage.locus = location;
25738 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
25741 /* Append TYPE_DECL to the template TEMPL.
25742 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
25743 At TEMPL instanciation time, TYPE_DECL will be checked to see
25744 if it can be accessed through SCOPE.
25745 LOCATION is the location of the usage point of TYPE_DECL.
25747 e.g. consider the following code snippet:
25749 class C
25751 typedef int myint;
25754 template<class U> struct S
25756 C::myint mi; // <-- usage point of the typedef C::myint
25759 S<char> s;
25761 At S<char> instantiation time, we need to check the access of C::myint
25762 In other words, we need to check the access of the myint typedef through
25763 the C scope. For that purpose, this function will add the myint typedef
25764 and the scope C through which its being accessed to a list of typedefs
25765 tied to the template S. That list will be walked at template instantiation
25766 time and access check performed on each typedefs it contains.
25767 Note that this particular code snippet should yield an error because
25768 myint is private to C. */
25770 void
25771 append_type_to_template_for_access_check (tree templ,
25772 tree type_decl,
25773 tree scope,
25774 location_t location)
25776 qualified_typedef_usage_t *iter;
25777 unsigned i;
25779 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
25781 /* Make sure we don't append the type to the template twice. */
25782 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
25783 if (iter->typedef_decl == type_decl && scope == iter->context)
25784 return;
25786 append_type_to_template_for_access_check_1 (templ, type_decl,
25787 scope, location);
25790 /* Convert the generic type parameters in PARM that match the types given in the
25791 range [START_IDX, END_IDX) from the current_template_parms into generic type
25792 packs. */
25794 tree
25795 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
25797 tree current = current_template_parms;
25798 int depth = TMPL_PARMS_DEPTH (current);
25799 current = INNERMOST_TEMPLATE_PARMS (current);
25800 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
25802 for (int i = 0; i < start_idx; ++i)
25803 TREE_VEC_ELT (replacement, i)
25804 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
25806 for (int i = start_idx; i < end_idx; ++i)
25808 /* Create a distinct parameter pack type from the current parm and add it
25809 to the replacement args to tsubst below into the generic function
25810 parameter. */
25812 tree o = TREE_TYPE (TREE_VALUE
25813 (TREE_VEC_ELT (current, i)));
25814 tree t = copy_type (o);
25815 TEMPLATE_TYPE_PARM_INDEX (t)
25816 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
25817 o, 0, 0, tf_none);
25818 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
25819 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
25820 TYPE_MAIN_VARIANT (t) = t;
25821 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
25822 TYPE_CANONICAL (t) = canonical_type_parameter (t);
25823 TREE_VEC_ELT (replacement, i) = t;
25824 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
25827 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
25828 TREE_VEC_ELT (replacement, i)
25829 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
25831 /* If there are more levels then build up the replacement with the outer
25832 template parms. */
25833 if (depth > 1)
25834 replacement = add_to_template_args (template_parms_to_args
25835 (TREE_CHAIN (current_template_parms)),
25836 replacement);
25838 return tsubst (parm, replacement, tf_none, NULL_TREE);
25841 /* Entries in the decl_constraint hash table. */
25842 struct GTY((for_user)) constr_entry
25844 tree decl;
25845 tree ci;
25848 /* Hashing function and equality for constraint entries. */
25849 struct constr_hasher : ggc_ptr_hash<constr_entry>
25851 static hashval_t hash (constr_entry *e)
25853 return (hashval_t)DECL_UID (e->decl);
25856 static bool equal (constr_entry *e1, constr_entry *e2)
25858 return e1->decl == e2->decl;
25862 /* A mapping from declarations to constraint information. Note that
25863 both templates and their underlying declarations are mapped to the
25864 same constraint information.
25866 FIXME: This is defined in pt.c because garbage collection
25867 code is not being generated for constraint.cc. */
25869 static GTY (()) hash_table<constr_hasher> *decl_constraints;
25871 /* Returns the template constraints of declaration T. If T is not
25872 constrained, return NULL_TREE. Note that T must be non-null. */
25874 tree
25875 get_constraints (tree t)
25877 if (!flag_concepts)
25878 return NULL_TREE;
25880 gcc_assert (DECL_P (t));
25881 if (TREE_CODE (t) == TEMPLATE_DECL)
25882 t = DECL_TEMPLATE_RESULT (t);
25883 constr_entry elt = { t, NULL_TREE };
25884 constr_entry* found = decl_constraints->find (&elt);
25885 if (found)
25886 return found->ci;
25887 else
25888 return NULL_TREE;
25891 /* Associate the given constraint information CI with the declaration
25892 T. If T is a template, then the constraints are associated with
25893 its underlying declaration. Don't build associations if CI is
25894 NULL_TREE. */
25896 void
25897 set_constraints (tree t, tree ci)
25899 if (!ci)
25900 return;
25901 gcc_assert (t && flag_concepts);
25902 if (TREE_CODE (t) == TEMPLATE_DECL)
25903 t = DECL_TEMPLATE_RESULT (t);
25904 gcc_assert (!get_constraints (t));
25905 constr_entry elt = {t, ci};
25906 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
25907 constr_entry* entry = ggc_alloc<constr_entry> ();
25908 *entry = elt;
25909 *slot = entry;
25912 /* Remove the associated constraints of the declaration T. */
25914 void
25915 remove_constraints (tree t)
25917 gcc_assert (DECL_P (t));
25918 if (TREE_CODE (t) == TEMPLATE_DECL)
25919 t = DECL_TEMPLATE_RESULT (t);
25921 constr_entry elt = {t, NULL_TREE};
25922 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
25923 if (slot)
25924 decl_constraints->clear_slot (slot);
25927 /* Memoized satisfaction results for declarations. This
25928 maps the pair (constraint_info, arguments) to the result computed
25929 by constraints_satisfied_p. */
25931 struct GTY((for_user)) constraint_sat_entry
25933 tree ci;
25934 tree args;
25935 tree result;
25938 /* Hashing function and equality for constraint entries. */
25940 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
25942 static hashval_t hash (constraint_sat_entry *e)
25944 hashval_t val = iterative_hash_object(e->ci, 0);
25945 return iterative_hash_template_arg (e->args, val);
25948 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
25950 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
25954 /* Memoized satisfaction results for concept checks. */
25956 struct GTY((for_user)) concept_spec_entry
25958 tree tmpl;
25959 tree args;
25960 tree result;
25963 /* Hashing function and equality for constraint entries. */
25965 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
25967 static hashval_t hash (concept_spec_entry *e)
25969 return hash_tmpl_and_args (e->tmpl, e->args);
25972 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
25974 ++comparing_specializations;
25975 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
25976 --comparing_specializations;
25977 return eq;
25981 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
25982 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
25984 /* Search for a memoized satisfaction result. Returns one of the
25985 truth value nodes if previously memoized, or NULL_TREE otherwise. */
25987 tree
25988 lookup_constraint_satisfaction (tree ci, tree args)
25990 constraint_sat_entry elt = { ci, args, NULL_TREE };
25991 constraint_sat_entry* found = constraint_memos->find (&elt);
25992 if (found)
25993 return found->result;
25994 else
25995 return NULL_TREE;
25998 /* Memoize the result of a satisfication test. Returns the saved result. */
26000 tree
26001 memoize_constraint_satisfaction (tree ci, tree args, tree result)
26003 constraint_sat_entry elt = {ci, args, result};
26004 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
26005 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
26006 *entry = elt;
26007 *slot = entry;
26008 return result;
26011 /* Search for a memoized satisfaction result for a concept. */
26013 tree
26014 lookup_concept_satisfaction (tree tmpl, tree args)
26016 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26017 concept_spec_entry* found = concept_memos->find (&elt);
26018 if (found)
26019 return found->result;
26020 else
26021 return NULL_TREE;
26024 /* Memoize the result of a concept check. Returns the saved result. */
26026 tree
26027 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
26029 concept_spec_entry elt = {tmpl, args, result};
26030 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
26031 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26032 *entry = elt;
26033 *slot = entry;
26034 return result;
26037 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
26039 /* Returns a prior concept specialization. This returns the substituted
26040 and normalized constraints defined by the concept. */
26042 tree
26043 get_concept_expansion (tree tmpl, tree args)
26045 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26046 concept_spec_entry* found = concept_expansions->find (&elt);
26047 if (found)
26048 return found->result;
26049 else
26050 return NULL_TREE;
26053 /* Save a concept expansion for later. */
26055 tree
26056 save_concept_expansion (tree tmpl, tree args, tree def)
26058 concept_spec_entry elt = {tmpl, args, def};
26059 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
26060 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26061 *entry = elt;
26062 *slot = entry;
26063 return def;
26066 static hashval_t
26067 hash_subsumption_args (tree t1, tree t2)
26069 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
26070 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
26071 int val = 0;
26072 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
26073 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
26074 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
26075 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
26076 return val;
26079 /* Compare the constraints of two subsumption entries. The LEFT1 and
26080 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
26081 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
26083 static bool
26084 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
26086 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
26087 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
26088 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
26089 CHECK_CONSTR_ARGS (right1)))
26090 return comp_template_args (CHECK_CONSTR_ARGS (left2),
26091 CHECK_CONSTR_ARGS (right2));
26092 return false;
26095 /* Key/value pair for learning and memoizing subsumption results. This
26096 associates a pair of check constraints (including arguments) with
26097 a boolean value indicating the result. */
26099 struct GTY((for_user)) subsumption_entry
26101 tree t1;
26102 tree t2;
26103 bool result;
26106 /* Hashing function and equality for constraint entries. */
26108 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
26110 static hashval_t hash (subsumption_entry *e)
26112 return hash_subsumption_args (e->t1, e->t2);
26115 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
26117 ++comparing_specializations;
26118 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
26119 --comparing_specializations;
26120 return eq;
26124 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
26126 /* Search for a previously cached subsumption result. */
26128 bool*
26129 lookup_subsumption_result (tree t1, tree t2)
26131 subsumption_entry elt = { t1, t2, false };
26132 subsumption_entry* found = subsumption_table->find (&elt);
26133 if (found)
26134 return &found->result;
26135 else
26136 return 0;
26139 /* Save a subsumption result. */
26141 bool
26142 save_subsumption_result (tree t1, tree t2, bool result)
26144 subsumption_entry elt = {t1, t2, result};
26145 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
26146 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
26147 *entry = elt;
26148 *slot = entry;
26149 return result;
26152 /* Set up the hash table for constraint association. */
26154 void
26155 init_constraint_processing (void)
26157 if (!flag_concepts)
26158 return;
26160 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
26161 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
26162 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
26163 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
26164 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
26167 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
26168 0..N-1. */
26170 void
26171 declare_integer_pack (void)
26173 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
26174 build_function_type_list (integer_type_node,
26175 integer_type_node,
26176 NULL_TREE),
26177 NULL_TREE, ECF_CONST);
26178 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
26179 DECL_BUILT_IN_CLASS (ipfn) = BUILT_IN_FRONTEND;
26182 /* Set up the hash tables for template instantiations. */
26184 void
26185 init_template_processing (void)
26187 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
26188 type_specializations = hash_table<spec_hasher>::create_ggc (37);
26190 if (cxx_dialect >= cxx11)
26191 declare_integer_pack ();
26194 /* Print stats about the template hash tables for -fstats. */
26196 void
26197 print_template_statistics (void)
26199 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
26200 "%f collisions\n", (long) decl_specializations->size (),
26201 (long) decl_specializations->elements (),
26202 decl_specializations->collisions ());
26203 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
26204 "%f collisions\n", (long) type_specializations->size (),
26205 (long) type_specializations->elements (),
26206 type_specializations->collisions ());
26209 #include "gt-cp-pt.h"