PR c++/80384 - ICE with dependent noexcept-specifier
[official-gcc.git] / gcc / cp / pt.c
blob99f5b123a84ef0e6a266d55ce7b482d66e999b39
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 (strcmp (IDENTIFIER_POINTER (DECL_NAME (fn)), "__integer_pack") == 0)
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 (strcmp (IDENTIFIER_POINTER (DECL_NAME (fn)), "__integer_pack") == 0)
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 STRIP_NOPS (expr);
6128 if (expr && (null_ptr_cst_p (expr) || TREE_CODE (expr) == PTRMEM_CST))
6129 return true;
6130 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6131 return true;
6132 if (processing_template_decl
6133 && TREE_CODE (expr) == ADDR_EXPR
6134 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6135 return true;
6136 if (complain & tf_error)
6138 error ("%qE is not a valid template argument for type %qT",
6139 expr, type);
6140 error ("it must be a pointer-to-member of the form %<&X::Y%>");
6142 return false;
6145 /* Returns TRUE iff the address of OP is value-dependent.
6147 14.6.2.4 [temp.dep.temp]:
6148 A non-integral non-type template-argument is dependent if its type is
6149 dependent or it has either of the following forms
6150 qualified-id
6151 & qualified-id
6152 and contains a nested-name-specifier which specifies a class-name that
6153 names a dependent type.
6155 We generalize this to just say that the address of a member of a
6156 dependent class is value-dependent; the above doesn't cover the
6157 address of a static data member named with an unqualified-id. */
6159 static bool
6160 has_value_dependent_address (tree op)
6162 /* We could use get_inner_reference here, but there's no need;
6163 this is only relevant for template non-type arguments, which
6164 can only be expressed as &id-expression. */
6165 if (DECL_P (op))
6167 tree ctx = CP_DECL_CONTEXT (op);
6168 if (TYPE_P (ctx) && dependent_type_p (ctx))
6169 return true;
6172 return false;
6175 /* The next set of functions are used for providing helpful explanatory
6176 diagnostics for failed overload resolution. Their messages should be
6177 indented by two spaces for consistency with the messages in
6178 call.c */
6180 static int
6181 unify_success (bool /*explain_p*/)
6183 return 0;
6186 /* Other failure functions should call this one, to provide a single function
6187 for setting a breakpoint on. */
6189 static int
6190 unify_invalid (bool /*explain_p*/)
6192 return 1;
6195 static int
6196 unify_parameter_deduction_failure (bool explain_p, tree parm)
6198 if (explain_p)
6199 inform (input_location,
6200 " couldn't deduce template parameter %qD", parm);
6201 return unify_invalid (explain_p);
6204 static int
6205 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6207 if (explain_p)
6208 inform (input_location,
6209 " types %qT and %qT have incompatible cv-qualifiers",
6210 parm, arg);
6211 return unify_invalid (explain_p);
6214 static int
6215 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6217 if (explain_p)
6218 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6219 return unify_invalid (explain_p);
6222 static int
6223 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6225 if (explain_p)
6226 inform (input_location,
6227 " template parameter %qD is not a parameter pack, but "
6228 "argument %qD is",
6229 parm, arg);
6230 return unify_invalid (explain_p);
6233 static int
6234 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6236 if (explain_p)
6237 inform (input_location,
6238 " template argument %qE does not match "
6239 "pointer-to-member constant %qE",
6240 arg, parm);
6241 return unify_invalid (explain_p);
6244 static int
6245 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6247 if (explain_p)
6248 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6249 return unify_invalid (explain_p);
6252 static int
6253 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6255 if (explain_p)
6256 inform (input_location,
6257 " inconsistent parameter pack deduction with %qT and %qT",
6258 old_arg, new_arg);
6259 return unify_invalid (explain_p);
6262 static int
6263 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6265 if (explain_p)
6267 if (TYPE_P (parm))
6268 inform (input_location,
6269 " deduced conflicting types for parameter %qT (%qT and %qT)",
6270 parm, first, second);
6271 else
6272 inform (input_location,
6273 " deduced conflicting values for non-type parameter "
6274 "%qE (%qE and %qE)", parm, first, second);
6276 return unify_invalid (explain_p);
6279 static int
6280 unify_vla_arg (bool explain_p, tree arg)
6282 if (explain_p)
6283 inform (input_location,
6284 " variable-sized array type %qT is not "
6285 "a valid template argument",
6286 arg);
6287 return unify_invalid (explain_p);
6290 static int
6291 unify_method_type_error (bool explain_p, tree arg)
6293 if (explain_p)
6294 inform (input_location,
6295 " member function type %qT is not a valid template argument",
6296 arg);
6297 return unify_invalid (explain_p);
6300 static int
6301 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6303 if (explain_p)
6305 if (least_p)
6306 inform_n (input_location, wanted,
6307 " candidate expects at least %d argument, %d provided",
6308 " candidate expects at least %d arguments, %d provided",
6309 wanted, have);
6310 else
6311 inform_n (input_location, wanted,
6312 " candidate expects %d argument, %d provided",
6313 " candidate expects %d arguments, %d provided",
6314 wanted, have);
6316 return unify_invalid (explain_p);
6319 static int
6320 unify_too_many_arguments (bool explain_p, int have, int wanted)
6322 return unify_arity (explain_p, have, wanted);
6325 static int
6326 unify_too_few_arguments (bool explain_p, int have, int wanted,
6327 bool least_p = false)
6329 return unify_arity (explain_p, have, wanted, least_p);
6332 static int
6333 unify_arg_conversion (bool explain_p, tree to_type,
6334 tree from_type, tree arg)
6336 if (explain_p)
6337 inform (EXPR_LOC_OR_LOC (arg, input_location),
6338 " cannot convert %qE (type %qT) to type %qT",
6339 arg, from_type, to_type);
6340 return unify_invalid (explain_p);
6343 static int
6344 unify_no_common_base (bool explain_p, enum template_base_result r,
6345 tree parm, tree arg)
6347 if (explain_p)
6348 switch (r)
6350 case tbr_ambiguous_baseclass:
6351 inform (input_location, " %qT is an ambiguous base class of %qT",
6352 parm, arg);
6353 break;
6354 default:
6355 inform (input_location, " %qT is not derived from %qT", arg, parm);
6356 break;
6358 return unify_invalid (explain_p);
6361 static int
6362 unify_inconsistent_template_template_parameters (bool explain_p)
6364 if (explain_p)
6365 inform (input_location,
6366 " template parameters of a template template argument are "
6367 "inconsistent with other deduced template arguments");
6368 return unify_invalid (explain_p);
6371 static int
6372 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6374 if (explain_p)
6375 inform (input_location,
6376 " can't deduce a template for %qT from non-template type %qT",
6377 parm, arg);
6378 return unify_invalid (explain_p);
6381 static int
6382 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6384 if (explain_p)
6385 inform (input_location,
6386 " template argument %qE does not match %qE", arg, parm);
6387 return unify_invalid (explain_p);
6390 static int
6391 unify_overload_resolution_failure (bool explain_p, tree arg)
6393 if (explain_p)
6394 inform (input_location,
6395 " could not resolve address from overloaded function %qE",
6396 arg);
6397 return unify_invalid (explain_p);
6400 /* Attempt to convert the non-type template parameter EXPR to the
6401 indicated TYPE. If the conversion is successful, return the
6402 converted value. If the conversion is unsuccessful, return
6403 NULL_TREE if we issued an error message, or error_mark_node if we
6404 did not. We issue error messages for out-and-out bad template
6405 parameters, but not simply because the conversion failed, since we
6406 might be just trying to do argument deduction. Both TYPE and EXPR
6407 must be non-dependent.
6409 The conversion follows the special rules described in
6410 [temp.arg.nontype], and it is much more strict than an implicit
6411 conversion.
6413 This function is called twice for each template argument (see
6414 lookup_template_class for a more accurate description of this
6415 problem). This means that we need to handle expressions which
6416 are not valid in a C++ source, but can be created from the
6417 first call (for instance, casts to perform conversions). These
6418 hacks can go away after we fix the double coercion problem. */
6420 static tree
6421 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6423 tree expr_type;
6425 /* Detect immediately string literals as invalid non-type argument.
6426 This special-case is not needed for correctness (we would easily
6427 catch this later), but only to provide better diagnostic for this
6428 common user mistake. As suggested by DR 100, we do not mention
6429 linkage issues in the diagnostic as this is not the point. */
6430 /* FIXME we're making this OK. */
6431 if (TREE_CODE (expr) == STRING_CST)
6433 if (complain & tf_error)
6434 error ("%qE is not a valid template argument for type %qT "
6435 "because string literals can never be used in this context",
6436 expr, type);
6437 return NULL_TREE;
6440 /* Add the ADDR_EXPR now for the benefit of
6441 value_dependent_expression_p. */
6442 if (TYPE_PTROBV_P (type)
6443 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6445 expr = decay_conversion (expr, complain);
6446 if (expr == error_mark_node)
6447 return error_mark_node;
6450 /* If we are in a template, EXPR may be non-dependent, but still
6451 have a syntactic, rather than semantic, form. For example, EXPR
6452 might be a SCOPE_REF, rather than the VAR_DECL to which the
6453 SCOPE_REF refers. Preserving the qualifying scope is necessary
6454 so that access checking can be performed when the template is
6455 instantiated -- but here we need the resolved form so that we can
6456 convert the argument. */
6457 bool non_dep = false;
6458 if (TYPE_REF_OBJ_P (type)
6459 && has_value_dependent_address (expr))
6460 /* If we want the address and it's value-dependent, don't fold. */;
6461 else if (processing_template_decl
6462 && potential_nondependent_constant_expression (expr))
6463 non_dep = true;
6464 if (error_operand_p (expr))
6465 return error_mark_node;
6466 expr_type = TREE_TYPE (expr);
6467 if (TREE_CODE (type) == REFERENCE_TYPE)
6468 expr = mark_lvalue_use (expr);
6469 else
6470 expr = mark_rvalue_use (expr);
6472 /* If the argument is non-dependent, perform any conversions in
6473 non-dependent context as well. */
6474 processing_template_decl_sentinel s (non_dep);
6475 if (non_dep)
6476 expr = instantiate_non_dependent_expr_internal (expr, complain);
6478 if (value_dependent_expression_p (expr))
6479 expr = canonicalize_expr_argument (expr, complain);
6481 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6482 to a non-type argument of "nullptr". */
6483 if (expr == nullptr_node && TYPE_PTR_OR_PTRMEM_P (type))
6484 expr = fold_simple (convert (type, expr));
6486 /* In C++11, integral or enumeration non-type template arguments can be
6487 arbitrary constant expressions. Pointer and pointer to
6488 member arguments can be general constant expressions that evaluate
6489 to a null value, but otherwise still need to be of a specific form. */
6490 if (cxx_dialect >= cxx11)
6492 if (TREE_CODE (expr) == PTRMEM_CST)
6493 /* A PTRMEM_CST is already constant, and a valid template
6494 argument for a parameter of pointer to member type, we just want
6495 to leave it in that form rather than lower it to a
6496 CONSTRUCTOR. */;
6497 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6498 /* Constant value checking is done later with type conversion. */;
6499 else if (cxx_dialect >= cxx1z)
6501 if (TREE_CODE (type) != REFERENCE_TYPE)
6502 expr = maybe_constant_value (expr);
6503 else if (REFERENCE_REF_P (expr))
6505 expr = TREE_OPERAND (expr, 0);
6506 expr = maybe_constant_value (expr);
6507 expr = convert_from_reference (expr);
6510 else if (TYPE_PTR_OR_PTRMEM_P (type))
6512 tree folded = maybe_constant_value (expr);
6513 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6514 : null_member_pointer_value_p (folded))
6515 expr = folded;
6519 /* HACK: Due to double coercion, we can get a
6520 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6521 which is the tree that we built on the first call (see
6522 below when coercing to reference to object or to reference to
6523 function). We just strip everything and get to the arg.
6524 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6525 for examples. */
6526 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6528 tree probe_type, probe = expr;
6529 if (REFERENCE_REF_P (probe))
6530 probe = TREE_OPERAND (probe, 0);
6531 probe_type = TREE_TYPE (probe);
6532 if (TREE_CODE (probe) == NOP_EXPR)
6534 /* ??? Maybe we could use convert_from_reference here, but we
6535 would need to relax its constraints because the NOP_EXPR
6536 could actually change the type to something more cv-qualified,
6537 and this is not folded by convert_from_reference. */
6538 tree addr = TREE_OPERAND (probe, 0);
6539 if (TREE_CODE (probe_type) == REFERENCE_TYPE
6540 && TREE_CODE (addr) == ADDR_EXPR
6541 && TYPE_PTR_P (TREE_TYPE (addr))
6542 && (same_type_ignoring_top_level_qualifiers_p
6543 (TREE_TYPE (probe_type),
6544 TREE_TYPE (TREE_TYPE (addr)))))
6546 expr = TREE_OPERAND (addr, 0);
6547 expr_type = TREE_TYPE (probe_type);
6552 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
6553 parameter is a pointer to object, through decay and
6554 qualification conversion. Let's strip everything. */
6555 else if (TREE_CODE (expr) == NOP_EXPR && TYPE_PTROBV_P (type))
6557 tree probe = expr;
6558 STRIP_NOPS (probe);
6559 if (TREE_CODE (probe) == ADDR_EXPR
6560 && TYPE_PTR_P (TREE_TYPE (probe)))
6562 /* Skip the ADDR_EXPR only if it is part of the decay for
6563 an array. Otherwise, it is part of the original argument
6564 in the source code. */
6565 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe, 0))) == ARRAY_TYPE)
6566 probe = TREE_OPERAND (probe, 0);
6567 expr = probe;
6568 expr_type = TREE_TYPE (expr);
6572 /* [temp.arg.nontype]/5, bullet 1
6574 For a non-type template-parameter of integral or enumeration type,
6575 integral promotions (_conv.prom_) and integral conversions
6576 (_conv.integral_) are applied. */
6577 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6579 tree t = build_integral_nontype_arg_conv (type, expr, complain);
6580 t = maybe_constant_value (t);
6581 if (t != error_mark_node)
6582 expr = t;
6584 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6585 return error_mark_node;
6587 /* Notice that there are constant expressions like '4 % 0' which
6588 do not fold into integer constants. */
6589 if (TREE_CODE (expr) != INTEGER_CST
6590 && !value_dependent_expression_p (expr))
6592 if (complain & tf_error)
6594 int errs = errorcount, warns = warningcount + werrorcount;
6595 if (processing_template_decl
6596 && !require_potential_constant_expression (expr))
6597 return NULL_TREE;
6598 expr = cxx_constant_value (expr);
6599 if (errorcount > errs || warningcount + werrorcount > warns)
6600 inform (EXPR_LOC_OR_LOC (expr, input_location),
6601 "in template argument for type %qT ", type);
6602 if (expr == error_mark_node)
6603 return NULL_TREE;
6604 /* else cxx_constant_value complained but gave us
6605 a real constant, so go ahead. */
6606 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
6608 else
6609 return NULL_TREE;
6612 /* Avoid typedef problems. */
6613 if (TREE_TYPE (expr) != type)
6614 expr = fold_convert (type, expr);
6616 /* [temp.arg.nontype]/5, bullet 2
6618 For a non-type template-parameter of type pointer to object,
6619 qualification conversions (_conv.qual_) and the array-to-pointer
6620 conversion (_conv.array_) are applied. */
6621 else if (TYPE_PTROBV_P (type))
6623 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6625 A template-argument for a non-type, non-template template-parameter
6626 shall be one of: [...]
6628 -- the name of a non-type template-parameter;
6629 -- the address of an object or function with external linkage, [...]
6630 expressed as "& id-expression" where the & is optional if the name
6631 refers to a function or array, or if the corresponding
6632 template-parameter is a reference.
6634 Here, we do not care about functions, as they are invalid anyway
6635 for a parameter of type pointer-to-object. */
6637 if (value_dependent_expression_p (expr))
6638 /* Non-type template parameters are OK. */
6640 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6641 /* Null pointer values are OK in C++11. */;
6642 else if (TREE_CODE (expr) != ADDR_EXPR
6643 && TREE_CODE (expr_type) != ARRAY_TYPE)
6645 if (VAR_P (expr))
6647 if (complain & tf_error)
6648 error ("%qD is not a valid template argument "
6649 "because %qD is a variable, not the address of "
6650 "a variable", expr, expr);
6651 return NULL_TREE;
6653 if (POINTER_TYPE_P (expr_type))
6655 if (complain & tf_error)
6656 error ("%qE is not a valid template argument for %qT "
6657 "because it is not the address of a variable",
6658 expr, type);
6659 return NULL_TREE;
6661 /* Other values, like integer constants, might be valid
6662 non-type arguments of some other type. */
6663 return error_mark_node;
6665 else
6667 tree decl;
6669 decl = ((TREE_CODE (expr) == ADDR_EXPR)
6670 ? TREE_OPERAND (expr, 0) : expr);
6671 if (!VAR_P (decl))
6673 if (complain & tf_error)
6674 error ("%qE is not a valid template argument of type %qT "
6675 "because %qE is not a variable", expr, type, decl);
6676 return NULL_TREE;
6678 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6680 if (complain & tf_error)
6681 error ("%qE is not a valid template argument of type %qT "
6682 "because %qD does not have external linkage",
6683 expr, type, decl);
6684 return NULL_TREE;
6686 else if (cxx_dialect >= cxx11 && decl_linkage (decl) == lk_none)
6688 if (complain & tf_error)
6689 error ("%qE is not a valid template argument of type %qT "
6690 "because %qD has no linkage", expr, type, decl);
6691 return NULL_TREE;
6695 expr = decay_conversion (expr, complain);
6696 if (expr == error_mark_node)
6697 return error_mark_node;
6699 expr = perform_qualification_conversions (type, expr);
6700 if (expr == error_mark_node)
6701 return error_mark_node;
6703 /* [temp.arg.nontype]/5, bullet 3
6705 For a non-type template-parameter of type reference to object, no
6706 conversions apply. The type referred to by the reference may be more
6707 cv-qualified than the (otherwise identical) type of the
6708 template-argument. The template-parameter is bound directly to the
6709 template-argument, which must be an lvalue. */
6710 else if (TYPE_REF_OBJ_P (type))
6712 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6713 expr_type))
6714 return error_mark_node;
6716 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6718 if (complain & tf_error)
6719 error ("%qE is not a valid template argument for type %qT "
6720 "because of conflicts in cv-qualification", expr, type);
6721 return NULL_TREE;
6724 if (!lvalue_p (expr))
6726 if (complain & tf_error)
6727 error ("%qE is not a valid template argument for type %qT "
6728 "because it is not an lvalue", expr, type);
6729 return NULL_TREE;
6732 /* [temp.arg.nontype]/1
6734 A template-argument for a non-type, non-template template-parameter
6735 shall be one of: [...]
6737 -- the address of an object or function with external linkage. */
6738 if (INDIRECT_REF_P (expr)
6739 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6741 expr = TREE_OPERAND (expr, 0);
6742 if (DECL_P (expr))
6744 if (complain & tf_error)
6745 error ("%q#D is not a valid template argument for type %qT "
6746 "because a reference variable does not have a constant "
6747 "address", expr, type);
6748 return NULL_TREE;
6752 if (TYPE_REF_OBJ_P (TREE_TYPE (expr))
6753 && value_dependent_expression_p (expr))
6754 /* OK, dependent reference. We don't want to ask whether a DECL is
6755 itself value-dependent, since what we want here is its address. */;
6756 else
6758 if (!DECL_P (expr))
6760 if (complain & tf_error)
6761 error ("%qE is not a valid template argument for type %qT "
6762 "because it is not an object with linkage",
6763 expr, type);
6764 return NULL_TREE;
6767 /* DR 1155 allows internal linkage in C++11 and up. */
6768 linkage_kind linkage = decl_linkage (expr);
6769 if (linkage < (cxx_dialect >= cxx11 ? lk_internal : lk_external))
6771 if (complain & tf_error)
6772 error ("%qE is not a valid template argument for type %qT "
6773 "because object %qD does not have linkage",
6774 expr, type, expr);
6775 return NULL_TREE;
6778 expr = build_address (expr);
6781 if (!same_type_p (type, TREE_TYPE (expr)))
6782 expr = build_nop (type, expr);
6784 /* [temp.arg.nontype]/5, bullet 4
6786 For a non-type template-parameter of type pointer to function, only
6787 the function-to-pointer conversion (_conv.func_) is applied. If the
6788 template-argument represents a set of overloaded functions (or a
6789 pointer to such), the matching function is selected from the set
6790 (_over.over_). */
6791 else if (TYPE_PTRFN_P (type))
6793 /* If the argument is a template-id, we might not have enough
6794 context information to decay the pointer. */
6795 if (!type_unknown_p (expr_type))
6797 expr = decay_conversion (expr, complain);
6798 if (expr == error_mark_node)
6799 return error_mark_node;
6802 if (cxx_dialect >= cxx11 && integer_zerop (expr))
6803 /* Null pointer values are OK in C++11. */
6804 return perform_qualification_conversions (type, expr);
6806 expr = convert_nontype_argument_function (type, expr, complain);
6807 if (!expr || expr == error_mark_node)
6808 return expr;
6810 /* [temp.arg.nontype]/5, bullet 5
6812 For a non-type template-parameter of type reference to function, no
6813 conversions apply. If the template-argument represents a set of
6814 overloaded functions, the matching function is selected from the set
6815 (_over.over_). */
6816 else if (TYPE_REFFN_P (type))
6818 if (TREE_CODE (expr) == ADDR_EXPR)
6820 if (complain & tf_error)
6822 error ("%qE is not a valid template argument for type %qT "
6823 "because it is a pointer", expr, type);
6824 inform (input_location, "try using %qE instead",
6825 TREE_OPERAND (expr, 0));
6827 return NULL_TREE;
6830 expr = convert_nontype_argument_function (type, expr, complain);
6831 if (!expr || expr == error_mark_node)
6832 return expr;
6834 /* [temp.arg.nontype]/5, bullet 6
6836 For a non-type template-parameter of type pointer to member function,
6837 no conversions apply. If the template-argument represents a set of
6838 overloaded member functions, the matching member function is selected
6839 from the set (_over.over_). */
6840 else if (TYPE_PTRMEMFUNC_P (type))
6842 expr = instantiate_type (type, expr, tf_none);
6843 if (expr == error_mark_node)
6844 return error_mark_node;
6846 /* [temp.arg.nontype] bullet 1 says the pointer to member
6847 expression must be a pointer-to-member constant. */
6848 if (!value_dependent_expression_p (expr)
6849 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6850 return error_mark_node;
6852 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
6853 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
6854 if (fnptr_conv_p (type, TREE_TYPE (expr)))
6855 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
6857 /* There is no way to disable standard conversions in
6858 resolve_address_of_overloaded_function (called by
6859 instantiate_type). It is possible that the call succeeded by
6860 converting &B::I to &D::I (where B is a base of D), so we need
6861 to reject this conversion here.
6863 Actually, even if there was a way to disable standard conversions,
6864 it would still be better to reject them here so that we can
6865 provide a superior diagnostic. */
6866 if (!same_type_p (TREE_TYPE (expr), type))
6868 if (complain & tf_error)
6870 error ("%qE is not a valid template argument for type %qT "
6871 "because it is of type %qT", expr, type,
6872 TREE_TYPE (expr));
6873 /* If we are just one standard conversion off, explain. */
6874 if (can_convert_standard (type, TREE_TYPE (expr), complain))
6875 inform (input_location,
6876 "standard conversions are not allowed in this context");
6878 return NULL_TREE;
6881 /* [temp.arg.nontype]/5, bullet 7
6883 For a non-type template-parameter of type pointer to data member,
6884 qualification conversions (_conv.qual_) are applied. */
6885 else if (TYPE_PTRDATAMEM_P (type))
6887 /* [temp.arg.nontype] bullet 1 says the pointer to member
6888 expression must be a pointer-to-member constant. */
6889 if (!value_dependent_expression_p (expr)
6890 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6891 return error_mark_node;
6893 expr = perform_qualification_conversions (type, expr);
6894 if (expr == error_mark_node)
6895 return expr;
6897 else if (NULLPTR_TYPE_P (type))
6899 if (expr != nullptr_node)
6901 if (complain & tf_error)
6902 error ("%qE is not a valid template argument for type %qT "
6903 "because it is of type %qT", expr, type, TREE_TYPE (expr));
6904 return NULL_TREE;
6906 return expr;
6908 /* A template non-type parameter must be one of the above. */
6909 else
6910 gcc_unreachable ();
6912 /* Sanity check: did we actually convert the argument to the
6913 right type? */
6914 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6915 (type, TREE_TYPE (expr)));
6916 return convert_from_reference (expr);
6919 /* Subroutine of coerce_template_template_parms, which returns 1 if
6920 PARM_PARM and ARG_PARM match using the rule for the template
6921 parameters of template template parameters. Both PARM and ARG are
6922 template parameters; the rest of the arguments are the same as for
6923 coerce_template_template_parms.
6925 static int
6926 coerce_template_template_parm (tree parm,
6927 tree arg,
6928 tsubst_flags_t complain,
6929 tree in_decl,
6930 tree outer_args)
6932 if (arg == NULL_TREE || error_operand_p (arg)
6933 || parm == NULL_TREE || error_operand_p (parm))
6934 return 0;
6936 if (TREE_CODE (arg) != TREE_CODE (parm))
6937 return 0;
6939 switch (TREE_CODE (parm))
6941 case TEMPLATE_DECL:
6942 /* We encounter instantiations of templates like
6943 template <template <template <class> class> class TT>
6944 class C; */
6946 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6947 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6949 if (!coerce_template_template_parms
6950 (parmparm, argparm, complain, in_decl, outer_args))
6951 return 0;
6953 /* Fall through. */
6955 case TYPE_DECL:
6956 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
6957 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6958 /* Argument is a parameter pack but parameter is not. */
6959 return 0;
6960 break;
6962 case PARM_DECL:
6963 /* The tsubst call is used to handle cases such as
6965 template <int> class C {};
6966 template <class T, template <T> class TT> class D {};
6967 D<int, C> d;
6969 i.e. the parameter list of TT depends on earlier parameters. */
6970 if (!uses_template_parms (TREE_TYPE (arg)))
6972 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
6973 if (!uses_template_parms (t)
6974 && !same_type_p (t, TREE_TYPE (arg)))
6975 return 0;
6978 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6979 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6980 /* Argument is a parameter pack but parameter is not. */
6981 return 0;
6983 break;
6985 default:
6986 gcc_unreachable ();
6989 return 1;
6992 /* Coerce template argument list ARGLIST for use with template
6993 template-parameter TEMPL. */
6995 static tree
6996 coerce_template_args_for_ttp (tree templ, tree arglist,
6997 tsubst_flags_t complain)
6999 /* Consider an example where a template template parameter declared as
7001 template <class T, class U = std::allocator<T> > class TT
7003 The template parameter level of T and U are one level larger than
7004 of TT. To proper process the default argument of U, say when an
7005 instantiation `TT<int>' is seen, we need to build the full
7006 arguments containing {int} as the innermost level. Outer levels,
7007 available when not appearing as default template argument, can be
7008 obtained from the arguments of the enclosing template.
7010 Suppose that TT is later substituted with std::vector. The above
7011 instantiation is `TT<int, std::allocator<T> >' with TT at
7012 level 1, and T at level 2, while the template arguments at level 1
7013 becomes {std::vector} and the inner level 2 is {int}. */
7015 tree outer = DECL_CONTEXT (templ);
7016 if (outer)
7018 if (DECL_TEMPLATE_SPECIALIZATION (outer))
7019 /* We want arguments for the partial specialization, not arguments for
7020 the primary template. */
7021 outer = template_parms_to_args (DECL_TEMPLATE_PARMS (outer));
7022 else
7023 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7025 else if (current_template_parms)
7027 /* This is an argument of the current template, so we haven't set
7028 DECL_CONTEXT yet. */
7029 tree relevant_template_parms;
7031 /* Parameter levels that are greater than the level of the given
7032 template template parm are irrelevant. */
7033 relevant_template_parms = current_template_parms;
7034 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7035 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7036 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7038 outer = template_parms_to_args (relevant_template_parms);
7041 if (outer)
7042 arglist = add_to_template_args (outer, arglist);
7044 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7045 return coerce_template_parms (parmlist, arglist, templ,
7046 complain,
7047 /*require_all_args=*/true,
7048 /*use_default_args=*/true);
7051 /* A cache of template template parameters with match-all default
7052 arguments. */
7053 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7054 static void
7055 store_defaulted_ttp (tree v, tree t)
7057 if (!defaulted_ttp_cache)
7058 defaulted_ttp_cache = hash_map<tree,tree>::create_ggc (13);
7059 defaulted_ttp_cache->put (v, t);
7061 static tree
7062 lookup_defaulted_ttp (tree v)
7064 if (defaulted_ttp_cache)
7065 if (tree *p = defaulted_ttp_cache->get (v))
7066 return *p;
7067 return NULL_TREE;
7070 /* T is a bound template template-parameter. Copy its arguments into default
7071 arguments of the template template-parameter's template parameters. */
7073 static tree
7074 add_defaults_to_ttp (tree otmpl)
7076 if (tree c = lookup_defaulted_ttp (otmpl))
7077 return c;
7079 tree ntmpl = copy_node (otmpl);
7081 tree ntype = copy_node (TREE_TYPE (otmpl));
7082 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7083 TYPE_MAIN_VARIANT (ntype) = ntype;
7084 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7085 TYPE_NAME (ntype) = ntmpl;
7086 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7088 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7089 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7090 TEMPLATE_PARM_DECL (idx) = ntmpl;
7091 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7093 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7094 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7095 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7096 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7097 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7099 tree o = TREE_VEC_ELT (vec, i);
7100 if (!template_parameter_pack_p (TREE_VALUE (o)))
7102 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7103 TREE_PURPOSE (n) = any_targ_node;
7107 store_defaulted_ttp (otmpl, ntmpl);
7108 return ntmpl;
7111 /* ARG is a bound potential template template-argument, and PARGS is a list
7112 of arguments for the corresponding template template-parameter. Adjust
7113 PARGS as appropriate for application to ARG's template, and if ARG is a
7114 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7115 arguments to the template template parameter. */
7117 static tree
7118 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7120 ++processing_template_decl;
7121 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7122 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7124 /* When comparing two template template-parameters in partial ordering,
7125 rewrite the one currently being used as an argument to have default
7126 arguments for all parameters. */
7127 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7128 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7129 if (pargs != error_mark_node)
7130 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7131 TYPE_TI_ARGS (arg));
7133 else
7135 tree aparms
7136 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7137 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7138 /*require_all*/true,
7139 /*use_default*/true);
7141 --processing_template_decl;
7142 return pargs;
7145 /* Subroutine of unify for the case when PARM is a
7146 BOUND_TEMPLATE_TEMPLATE_PARM. */
7148 static int
7149 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7150 bool explain_p)
7152 tree parmvec = TYPE_TI_ARGS (parm);
7153 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7155 /* The template template parm might be variadic and the argument
7156 not, so flatten both argument lists. */
7157 parmvec = expand_template_argument_pack (parmvec);
7158 argvec = expand_template_argument_pack (argvec);
7160 tree nparmvec = parmvec;
7161 if (flag_new_ttp)
7163 /* In keeping with P0522R0, adjust P's template arguments
7164 to apply to A's template; then flatten it again. */
7165 nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7166 nparmvec = expand_template_argument_pack (nparmvec);
7169 if (unify (tparms, targs, nparmvec, argvec,
7170 UNIFY_ALLOW_NONE, explain_p))
7171 return 1;
7173 /* If the P0522 adjustment eliminated a pack expansion, deduce
7174 empty packs. */
7175 if (flag_new_ttp
7176 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7177 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7178 DEDUCE_EXACT, /*sub*/true, explain_p))
7179 return 1;
7181 return 0;
7184 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7185 template template parameters. Both PARM_PARMS and ARG_PARMS are
7186 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7187 or PARM_DECL.
7189 Consider the example:
7190 template <class T> class A;
7191 template<template <class U> class TT> class B;
7193 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7194 the parameters to A, and OUTER_ARGS contains A. */
7196 static int
7197 coerce_template_template_parms (tree parm_parms,
7198 tree arg_parms,
7199 tsubst_flags_t complain,
7200 tree in_decl,
7201 tree outer_args)
7203 int nparms, nargs, i;
7204 tree parm, arg;
7205 int variadic_p = 0;
7207 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7208 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7210 nparms = TREE_VEC_LENGTH (parm_parms);
7211 nargs = TREE_VEC_LENGTH (arg_parms);
7213 if (flag_new_ttp)
7215 /* P0522R0: A template template-parameter P is at least as specialized as
7216 a template template-argument A if, given the following rewrite to two
7217 function templates, the function template corresponding to P is at
7218 least as specialized as the function template corresponding to A
7219 according to the partial ordering rules for function templates
7220 ([temp.func.order]). Given an invented class template X with the
7221 template parameter list of A (including default arguments):
7223 * Each of the two function templates has the same template parameters,
7224 respectively, as P or A.
7226 * Each function template has a single function parameter whose type is
7227 a specialization of X with template arguments corresponding to the
7228 template parameters from the respective function template where, for
7229 each template parameter PP in the template parameter list of the
7230 function template, a corresponding template argument AA is formed. If
7231 PP declares a parameter pack, then AA is the pack expansion
7232 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7234 If the rewrite produces an invalid type, then P is not at least as
7235 specialized as A. */
7237 /* So coerce P's args to apply to A's parms, and then deduce between A's
7238 args and the converted args. If that succeeds, A is at least as
7239 specialized as P, so they match.*/
7240 tree pargs = template_parms_level_to_args (parm_parms);
7241 ++processing_template_decl;
7242 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7243 /*require_all*/true, /*use_default*/true);
7244 --processing_template_decl;
7245 if (pargs != error_mark_node)
7247 tree targs = make_tree_vec (nargs);
7248 tree aargs = template_parms_level_to_args (arg_parms);
7249 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7250 /*explain*/false))
7251 return 1;
7255 /* Determine whether we have a parameter pack at the end of the
7256 template template parameter's template parameter list. */
7257 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7259 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7261 if (error_operand_p (parm))
7262 return 0;
7264 switch (TREE_CODE (parm))
7266 case TEMPLATE_DECL:
7267 case TYPE_DECL:
7268 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7269 variadic_p = 1;
7270 break;
7272 case PARM_DECL:
7273 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7274 variadic_p = 1;
7275 break;
7277 default:
7278 gcc_unreachable ();
7282 if (nargs != nparms
7283 && !(variadic_p && nargs >= nparms - 1))
7284 return 0;
7286 /* Check all of the template parameters except the parameter pack at
7287 the end (if any). */
7288 for (i = 0; i < nparms - variadic_p; ++i)
7290 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7291 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7292 continue;
7294 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7295 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7297 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7298 outer_args))
7299 return 0;
7303 if (variadic_p)
7305 /* Check each of the template parameters in the template
7306 argument against the template parameter pack at the end of
7307 the template template parameter. */
7308 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7309 return 0;
7311 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7313 for (; i < nargs; ++i)
7315 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7316 continue;
7318 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7320 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7321 outer_args))
7322 return 0;
7326 return 1;
7329 /* Verifies that the deduced template arguments (in TARGS) for the
7330 template template parameters (in TPARMS) represent valid bindings,
7331 by comparing the template parameter list of each template argument
7332 to the template parameter list of its corresponding template
7333 template parameter, in accordance with DR150. This
7334 routine can only be called after all template arguments have been
7335 deduced. It will return TRUE if all of the template template
7336 parameter bindings are okay, FALSE otherwise. */
7337 bool
7338 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7340 int i, ntparms = TREE_VEC_LENGTH (tparms);
7341 bool ret = true;
7343 /* We're dealing with template parms in this process. */
7344 ++processing_template_decl;
7346 targs = INNERMOST_TEMPLATE_ARGS (targs);
7348 for (i = 0; i < ntparms; ++i)
7350 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7351 tree targ = TREE_VEC_ELT (targs, i);
7353 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7355 tree packed_args = NULL_TREE;
7356 int idx, len = 1;
7358 if (ARGUMENT_PACK_P (targ))
7360 /* Look inside the argument pack. */
7361 packed_args = ARGUMENT_PACK_ARGS (targ);
7362 len = TREE_VEC_LENGTH (packed_args);
7365 for (idx = 0; idx < len; ++idx)
7367 tree targ_parms = NULL_TREE;
7369 if (packed_args)
7370 /* Extract the next argument from the argument
7371 pack. */
7372 targ = TREE_VEC_ELT (packed_args, idx);
7374 if (PACK_EXPANSION_P (targ))
7375 /* Look at the pattern of the pack expansion. */
7376 targ = PACK_EXPANSION_PATTERN (targ);
7378 /* Extract the template parameters from the template
7379 argument. */
7380 if (TREE_CODE (targ) == TEMPLATE_DECL)
7381 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7382 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7383 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7385 /* Verify that we can coerce the template template
7386 parameters from the template argument to the template
7387 parameter. This requires an exact match. */
7388 if (targ_parms
7389 && !coerce_template_template_parms
7390 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7391 targ_parms,
7392 tf_none,
7393 tparm,
7394 targs))
7396 ret = false;
7397 goto out;
7403 out:
7405 --processing_template_decl;
7406 return ret;
7409 /* Since type attributes aren't mangled, we need to strip them from
7410 template type arguments. */
7412 static tree
7413 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7415 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7416 return arg;
7417 bool removed_attributes = false;
7418 tree canon = strip_typedefs (arg, &removed_attributes);
7419 if (removed_attributes
7420 && (complain & tf_warning))
7421 warning (OPT_Wignored_attributes,
7422 "ignoring attributes on template argument %qT", arg);
7423 return canon;
7426 /* And from inside dependent non-type arguments like sizeof(Type). */
7428 static tree
7429 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7431 if (!arg || arg == error_mark_node)
7432 return arg;
7433 bool removed_attributes = false;
7434 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7435 if (removed_attributes
7436 && (complain & tf_warning))
7437 warning (OPT_Wignored_attributes,
7438 "ignoring attributes in template argument %qE", arg);
7439 return canon;
7442 // A template declaration can be substituted for a constrained
7443 // template template parameter only when the argument is more
7444 // constrained than the parameter.
7445 static bool
7446 is_compatible_template_arg (tree parm, tree arg)
7448 tree parm_cons = get_constraints (parm);
7450 /* For now, allow constrained template template arguments
7451 and unconstrained template template parameters. */
7452 if (parm_cons == NULL_TREE)
7453 return true;
7455 tree arg_cons = get_constraints (arg);
7457 // If the template parameter is constrained, we need to rewrite its
7458 // constraints in terms of the ARG's template parameters. This ensures
7459 // that all of the template parameter types will have the same depth.
7461 // Note that this is only valid when coerce_template_template_parm is
7462 // true for the innermost template parameters of PARM and ARG. In other
7463 // words, because coercion is successful, this conversion will be valid.
7464 if (parm_cons)
7466 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7467 parm_cons = tsubst_constraint_info (parm_cons,
7468 INNERMOST_TEMPLATE_ARGS (args),
7469 tf_none, NULL_TREE);
7470 if (parm_cons == error_mark_node)
7471 return false;
7474 return subsumes (parm_cons, arg_cons);
7477 // Convert a placeholder argument into a binding to the original
7478 // parameter. The original parameter is saved as the TREE_TYPE of
7479 // ARG.
7480 static inline tree
7481 convert_wildcard_argument (tree parm, tree arg)
7483 TREE_TYPE (arg) = parm;
7484 return arg;
7487 /* Convert the indicated template ARG as necessary to match the
7488 indicated template PARM. Returns the converted ARG, or
7489 error_mark_node if the conversion was unsuccessful. Error and
7490 warning messages are issued under control of COMPLAIN. This
7491 conversion is for the Ith parameter in the parameter list. ARGS is
7492 the full set of template arguments deduced so far. */
7494 static tree
7495 convert_template_argument (tree parm,
7496 tree arg,
7497 tree args,
7498 tsubst_flags_t complain,
7499 int i,
7500 tree in_decl)
7502 tree orig_arg;
7503 tree val;
7504 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7506 if (parm == error_mark_node)
7507 return error_mark_node;
7509 /* Trivially convert placeholders. */
7510 if (TREE_CODE (arg) == WILDCARD_DECL)
7511 return convert_wildcard_argument (parm, arg);
7513 if (arg == any_targ_node)
7514 return arg;
7516 if (TREE_CODE (arg) == TREE_LIST
7517 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7519 /* The template argument was the name of some
7520 member function. That's usually
7521 invalid, but static members are OK. In any
7522 case, grab the underlying fields/functions
7523 and issue an error later if required. */
7524 orig_arg = TREE_VALUE (arg);
7525 TREE_TYPE (arg) = unknown_type_node;
7528 orig_arg = arg;
7530 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7531 requires_type = (TREE_CODE (parm) == TYPE_DECL
7532 || requires_tmpl_type);
7534 /* When determining whether an argument pack expansion is a template,
7535 look at the pattern. */
7536 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7537 arg = PACK_EXPANSION_PATTERN (arg);
7539 /* Deal with an injected-class-name used as a template template arg. */
7540 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7542 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7543 if (TREE_CODE (t) == TEMPLATE_DECL)
7545 if (cxx_dialect >= cxx11)
7546 /* OK under DR 1004. */;
7547 else if (complain & tf_warning_or_error)
7548 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7549 " used as template template argument", TYPE_NAME (arg));
7550 else if (flag_pedantic_errors)
7551 t = arg;
7553 arg = t;
7557 is_tmpl_type =
7558 ((TREE_CODE (arg) == TEMPLATE_DECL
7559 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7560 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7561 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7562 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7564 if (is_tmpl_type
7565 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7566 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7567 arg = TYPE_STUB_DECL (arg);
7569 is_type = TYPE_P (arg) || is_tmpl_type;
7571 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7572 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7574 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7576 if (complain & tf_error)
7577 error ("invalid use of destructor %qE as a type", orig_arg);
7578 return error_mark_node;
7581 permerror (input_location,
7582 "to refer to a type member of a template parameter, "
7583 "use %<typename %E%>", orig_arg);
7585 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7586 TREE_OPERAND (arg, 1),
7587 typename_type,
7588 complain);
7589 arg = orig_arg;
7590 is_type = 1;
7592 if (is_type != requires_type)
7594 if (in_decl)
7596 if (complain & tf_error)
7598 error ("type/value mismatch at argument %d in template "
7599 "parameter list for %qD",
7600 i + 1, in_decl);
7601 if (is_type)
7602 inform (input_location,
7603 " expected a constant of type %qT, got %qT",
7604 TREE_TYPE (parm),
7605 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7606 else if (requires_tmpl_type)
7607 inform (input_location,
7608 " expected a class template, got %qE", orig_arg);
7609 else
7610 inform (input_location,
7611 " expected a type, got %qE", orig_arg);
7614 return error_mark_node;
7616 if (is_tmpl_type ^ requires_tmpl_type)
7618 if (in_decl && (complain & tf_error))
7620 error ("type/value mismatch at argument %d in template "
7621 "parameter list for %qD",
7622 i + 1, in_decl);
7623 if (is_tmpl_type)
7624 inform (input_location,
7625 " expected a type, got %qT", DECL_NAME (arg));
7626 else
7627 inform (input_location,
7628 " expected a class template, got %qT", orig_arg);
7630 return error_mark_node;
7633 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7634 /* We already did the appropriate conversion when packing args. */
7635 val = orig_arg;
7636 else if (is_type)
7638 if (requires_tmpl_type)
7640 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7641 /* The number of argument required is not known yet.
7642 Just accept it for now. */
7643 val = TREE_TYPE (arg);
7644 else
7646 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7647 tree argparm;
7649 /* Strip alias templates that are equivalent to another
7650 template. */
7651 arg = get_underlying_template (arg);
7652 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7654 if (coerce_template_template_parms (parmparm, argparm,
7655 complain, in_decl,
7656 args))
7658 val = arg;
7660 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7661 TEMPLATE_DECL. */
7662 if (val != error_mark_node)
7664 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
7665 val = TREE_TYPE (val);
7666 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
7667 val = make_pack_expansion (val);
7670 else
7672 if (in_decl && (complain & tf_error))
7674 error ("type/value mismatch at argument %d in "
7675 "template parameter list for %qD",
7676 i + 1, in_decl);
7677 inform (input_location,
7678 " expected a template of type %qD, got %qT",
7679 parm, orig_arg);
7682 val = error_mark_node;
7685 // Check that the constraints are compatible before allowing the
7686 // substitution.
7687 if (val != error_mark_node)
7688 if (!is_compatible_template_arg (parm, arg))
7690 if (in_decl && (complain & tf_error))
7692 error ("constraint mismatch at argument %d in "
7693 "template parameter list for %qD",
7694 i + 1, in_decl);
7695 inform (input_location, " expected %qD but got %qD",
7696 parm, arg);
7698 val = error_mark_node;
7702 else
7703 val = orig_arg;
7704 /* We only form one instance of each template specialization.
7705 Therefore, if we use a non-canonical variant (i.e., a
7706 typedef), any future messages referring to the type will use
7707 the typedef, which is confusing if those future uses do not
7708 themselves also use the typedef. */
7709 if (TYPE_P (val))
7710 val = canonicalize_type_argument (val, complain);
7712 else
7714 tree t = TREE_TYPE (parm);
7716 if (tree a = type_uses_auto (t))
7718 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
7719 if (t == error_mark_node)
7720 return error_mark_node;
7722 else
7723 t = tsubst (t, args, complain, in_decl);
7725 if (invalid_nontype_parm_type_p (t, complain))
7726 return error_mark_node;
7728 if (!type_dependent_expression_p (orig_arg)
7729 && !uses_template_parms (t))
7730 /* We used to call digest_init here. However, digest_init
7731 will report errors, which we don't want when complain
7732 is zero. More importantly, digest_init will try too
7733 hard to convert things: for example, `0' should not be
7734 converted to pointer type at this point according to
7735 the standard. Accepting this is not merely an
7736 extension, since deciding whether or not these
7737 conversions can occur is part of determining which
7738 function template to call, or whether a given explicit
7739 argument specification is valid. */
7740 val = convert_nontype_argument (t, orig_arg, complain);
7741 else
7742 val = canonicalize_expr_argument (orig_arg, complain);
7744 if (val == NULL_TREE)
7745 val = error_mark_node;
7746 else if (val == error_mark_node && (complain & tf_error))
7747 error ("could not convert template argument %qE from %qT to %qT",
7748 orig_arg, TREE_TYPE (orig_arg), t);
7750 if (INDIRECT_REF_P (val))
7752 /* Reject template arguments that are references to built-in
7753 functions with no library fallbacks. */
7754 const_tree inner = TREE_OPERAND (val, 0);
7755 const_tree innertype = TREE_TYPE (inner);
7756 if (innertype
7757 && TREE_CODE (innertype) == REFERENCE_TYPE
7758 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
7759 && 0 < TREE_OPERAND_LENGTH (inner)
7760 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
7761 return error_mark_node;
7764 if (TREE_CODE (val) == SCOPE_REF)
7766 /* Strip typedefs from the SCOPE_REF. */
7767 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
7768 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
7769 complain);
7770 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
7771 QUALIFIED_NAME_IS_TEMPLATE (val));
7775 return val;
7778 /* Coerces the remaining template arguments in INNER_ARGS (from
7779 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
7780 Returns the coerced argument pack. PARM_IDX is the position of this
7781 parameter in the template parameter list. ARGS is the original
7782 template argument list. */
7783 static tree
7784 coerce_template_parameter_pack (tree parms,
7785 int parm_idx,
7786 tree args,
7787 tree inner_args,
7788 int arg_idx,
7789 tree new_args,
7790 int* lost,
7791 tree in_decl,
7792 tsubst_flags_t complain)
7794 tree parm = TREE_VEC_ELT (parms, parm_idx);
7795 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7796 tree packed_args;
7797 tree argument_pack;
7798 tree packed_parms = NULL_TREE;
7800 if (arg_idx > nargs)
7801 arg_idx = nargs;
7803 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
7805 /* When the template parameter is a non-type template parameter pack
7806 or template template parameter pack whose type or template
7807 parameters use parameter packs, we know exactly how many arguments
7808 we are looking for. Build a vector of the instantiated decls for
7809 these template parameters in PACKED_PARMS. */
7810 /* We can't use make_pack_expansion here because it would interpret a
7811 _DECL as a use rather than a declaration. */
7812 tree decl = TREE_VALUE (parm);
7813 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
7814 SET_PACK_EXPANSION_PATTERN (exp, decl);
7815 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
7816 SET_TYPE_STRUCTURAL_EQUALITY (exp);
7818 TREE_VEC_LENGTH (args)--;
7819 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
7820 TREE_VEC_LENGTH (args)++;
7822 if (packed_parms == error_mark_node)
7823 return error_mark_node;
7825 /* If we're doing a partial instantiation of a member template,
7826 verify that all of the types used for the non-type
7827 template parameter pack are, in fact, valid for non-type
7828 template parameters. */
7829 if (arg_idx < nargs
7830 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
7832 int j, len = TREE_VEC_LENGTH (packed_parms);
7833 for (j = 0; j < len; ++j)
7835 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
7836 if (invalid_nontype_parm_type_p (t, complain))
7837 return error_mark_node;
7839 /* We don't know how many args we have yet, just
7840 use the unconverted ones for now. */
7841 return NULL_TREE;
7844 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
7846 /* Check if we have a placeholder pack, which indicates we're
7847 in the context of a introduction list. In that case we want
7848 to match this pack to the single placeholder. */
7849 else if (arg_idx < nargs
7850 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
7851 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
7853 nargs = arg_idx + 1;
7854 packed_args = make_tree_vec (1);
7856 else
7857 packed_args = make_tree_vec (nargs - arg_idx);
7859 /* Convert the remaining arguments, which will be a part of the
7860 parameter pack "parm". */
7861 int first_pack_arg = arg_idx;
7862 for (; arg_idx < nargs; ++arg_idx)
7864 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
7865 tree actual_parm = TREE_VALUE (parm);
7866 int pack_idx = arg_idx - first_pack_arg;
7868 if (packed_parms)
7870 /* Once we've packed as many args as we have types, stop. */
7871 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
7872 break;
7873 else if (PACK_EXPANSION_P (arg))
7874 /* We don't know how many args we have yet, just
7875 use the unconverted ones for now. */
7876 return NULL_TREE;
7877 else
7878 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
7881 if (arg == error_mark_node)
7883 if (complain & tf_error)
7884 error ("template argument %d is invalid", arg_idx + 1);
7886 else
7887 arg = convert_template_argument (actual_parm,
7888 arg, new_args, complain, parm_idx,
7889 in_decl);
7890 if (arg == error_mark_node)
7891 (*lost)++;
7892 TREE_VEC_ELT (packed_args, pack_idx) = arg;
7895 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
7896 && TREE_VEC_LENGTH (packed_args) > 0)
7898 if (complain & tf_error)
7899 error ("wrong number of template arguments (%d, should be %d)",
7900 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
7901 return error_mark_node;
7904 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
7905 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
7906 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
7907 else
7909 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
7910 TREE_CONSTANT (argument_pack) = 1;
7913 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
7914 if (CHECKING_P)
7915 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
7916 TREE_VEC_LENGTH (packed_args));
7917 return argument_pack;
7920 /* Returns the number of pack expansions in the template argument vector
7921 ARGS. */
7923 static int
7924 pack_expansion_args_count (tree args)
7926 int i;
7927 int count = 0;
7928 if (args)
7929 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
7931 tree elt = TREE_VEC_ELT (args, i);
7932 if (elt && PACK_EXPANSION_P (elt))
7933 ++count;
7935 return count;
7938 /* Convert all template arguments to their appropriate types, and
7939 return a vector containing the innermost resulting template
7940 arguments. If any error occurs, return error_mark_node. Error and
7941 warning messages are issued under control of COMPLAIN.
7943 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
7944 for arguments not specified in ARGS. Otherwise, if
7945 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
7946 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
7947 USE_DEFAULT_ARGS is false, then all arguments must be specified in
7948 ARGS. */
7950 static tree
7951 coerce_template_parms (tree parms,
7952 tree args,
7953 tree in_decl,
7954 tsubst_flags_t complain,
7955 bool require_all_args,
7956 bool use_default_args)
7958 int nparms, nargs, parm_idx, arg_idx, lost = 0;
7959 tree orig_inner_args;
7960 tree inner_args;
7961 tree new_args;
7962 tree new_inner_args;
7963 int saved_unevaluated_operand;
7964 int saved_inhibit_evaluation_warnings;
7966 /* When used as a boolean value, indicates whether this is a
7967 variadic template parameter list. Since it's an int, we can also
7968 subtract it from nparms to get the number of non-variadic
7969 parameters. */
7970 int variadic_p = 0;
7971 int variadic_args_p = 0;
7972 int post_variadic_parms = 0;
7974 /* Likewise for parameters with default arguments. */
7975 int default_p = 0;
7977 if (args == error_mark_node)
7978 return error_mark_node;
7980 nparms = TREE_VEC_LENGTH (parms);
7982 /* Determine if there are any parameter packs or default arguments. */
7983 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
7985 tree parm = TREE_VEC_ELT (parms, parm_idx);
7986 if (variadic_p)
7987 ++post_variadic_parms;
7988 if (template_parameter_pack_p (TREE_VALUE (parm)))
7989 ++variadic_p;
7990 if (TREE_PURPOSE (parm))
7991 ++default_p;
7994 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
7995 /* If there are no parameters that follow a parameter pack, we need to
7996 expand any argument packs so that we can deduce a parameter pack from
7997 some non-packed args followed by an argument pack, as in variadic85.C.
7998 If there are such parameters, we need to leave argument packs intact
7999 so the arguments are assigned properly. This can happen when dealing
8000 with a nested class inside a partial specialization of a class
8001 template, as in variadic92.C, or when deducing a template parameter pack
8002 from a sub-declarator, as in variadic114.C. */
8003 if (!post_variadic_parms)
8004 inner_args = expand_template_argument_pack (inner_args);
8006 /* Count any pack expansion args. */
8007 variadic_args_p = pack_expansion_args_count (inner_args);
8009 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8010 if ((nargs - variadic_args_p > nparms && !variadic_p)
8011 || (nargs < nparms - variadic_p
8012 && require_all_args
8013 && !variadic_args_p
8014 && (!use_default_args
8015 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8016 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8018 if (complain & tf_error)
8020 if (variadic_p || default_p)
8022 nparms -= variadic_p + default_p;
8023 error ("wrong number of template arguments "
8024 "(%d, should be at least %d)", nargs, nparms);
8026 else
8027 error ("wrong number of template arguments "
8028 "(%d, should be %d)", nargs, nparms);
8030 if (in_decl)
8031 inform (DECL_SOURCE_LOCATION (in_decl),
8032 "provided for %qD", in_decl);
8035 return error_mark_node;
8037 /* We can't pass a pack expansion to a non-pack parameter of an alias
8038 template (DR 1430). */
8039 else if (in_decl
8040 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8041 || concept_template_p (in_decl))
8042 && variadic_args_p
8043 && nargs - variadic_args_p < nparms - variadic_p)
8045 if (complain & tf_error)
8047 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8049 tree arg = TREE_VEC_ELT (inner_args, i);
8050 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8052 if (PACK_EXPANSION_P (arg)
8053 && !template_parameter_pack_p (parm))
8055 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8056 error_at (location_of (arg),
8057 "pack expansion argument for non-pack parameter "
8058 "%qD of alias template %qD", parm, in_decl);
8059 else
8060 error_at (location_of (arg),
8061 "pack expansion argument for non-pack parameter "
8062 "%qD of concept %qD", parm, in_decl);
8063 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8064 goto found;
8067 gcc_unreachable ();
8068 found:;
8070 return error_mark_node;
8073 /* We need to evaluate the template arguments, even though this
8074 template-id may be nested within a "sizeof". */
8075 saved_unevaluated_operand = cp_unevaluated_operand;
8076 cp_unevaluated_operand = 0;
8077 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8078 c_inhibit_evaluation_warnings = 0;
8079 new_inner_args = make_tree_vec (nparms);
8080 new_args = add_outermost_template_args (args, new_inner_args);
8081 int pack_adjust = 0;
8082 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8084 tree arg;
8085 tree parm;
8087 /* Get the Ith template parameter. */
8088 parm = TREE_VEC_ELT (parms, parm_idx);
8090 if (parm == error_mark_node)
8092 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8093 continue;
8096 /* Calculate the next argument. */
8097 if (arg_idx < nargs)
8098 arg = TREE_VEC_ELT (inner_args, arg_idx);
8099 else
8100 arg = NULL_TREE;
8102 if (template_parameter_pack_p (TREE_VALUE (parm))
8103 && !(arg && ARGUMENT_PACK_P (arg)))
8105 /* Some arguments will be placed in the
8106 template parameter pack PARM. */
8107 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8108 inner_args, arg_idx,
8109 new_args, &lost,
8110 in_decl, complain);
8112 if (arg == NULL_TREE)
8114 /* We don't know how many args we have yet, just use the
8115 unconverted (and still packed) ones for now. */
8116 new_inner_args = orig_inner_args;
8117 arg_idx = nargs;
8118 break;
8121 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8123 /* Store this argument. */
8124 if (arg == error_mark_node)
8126 lost++;
8127 /* We are done with all of the arguments. */
8128 arg_idx = nargs;
8130 else
8132 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8133 arg_idx += pack_adjust;
8136 continue;
8138 else if (arg)
8140 if (PACK_EXPANSION_P (arg))
8142 /* "If every valid specialization of a variadic template
8143 requires an empty template parameter pack, the template is
8144 ill-formed, no diagnostic required." So check that the
8145 pattern works with this parameter. */
8146 tree pattern = PACK_EXPANSION_PATTERN (arg);
8147 tree conv = convert_template_argument (TREE_VALUE (parm),
8148 pattern, new_args,
8149 complain, parm_idx,
8150 in_decl);
8151 if (conv == error_mark_node)
8153 if (complain & tf_error)
8154 inform (input_location, "so any instantiation with a "
8155 "non-empty parameter pack would be ill-formed");
8156 ++lost;
8158 else if (TYPE_P (conv) && !TYPE_P (pattern))
8159 /* Recover from missing typename. */
8160 TREE_VEC_ELT (inner_args, arg_idx)
8161 = make_pack_expansion (conv);
8163 /* We don't know how many args we have yet, just
8164 use the unconverted ones for now. */
8165 new_inner_args = inner_args;
8166 arg_idx = nargs;
8167 break;
8170 else if (require_all_args)
8172 /* There must be a default arg in this case. */
8173 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8174 complain, in_decl);
8175 /* The position of the first default template argument,
8176 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8177 Record that. */
8178 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8179 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8180 arg_idx - pack_adjust);
8182 else
8183 break;
8185 if (arg == error_mark_node)
8187 if (complain & tf_error)
8188 error ("template argument %d is invalid", arg_idx + 1);
8190 else if (!arg)
8191 /* This only occurs if there was an error in the template
8192 parameter list itself (which we would already have
8193 reported) that we are trying to recover from, e.g., a class
8194 template with a parameter list such as
8195 template<typename..., typename>. */
8196 ++lost;
8197 else
8198 arg = convert_template_argument (TREE_VALUE (parm),
8199 arg, new_args, complain,
8200 parm_idx, in_decl);
8202 if (arg == error_mark_node)
8203 lost++;
8204 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8206 cp_unevaluated_operand = saved_unevaluated_operand;
8207 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8209 if (variadic_p && arg_idx < nargs)
8211 if (complain & tf_error)
8213 error ("wrong number of template arguments "
8214 "(%d, should be %d)", nargs, arg_idx);
8215 if (in_decl)
8216 error ("provided for %q+D", in_decl);
8218 return error_mark_node;
8221 if (lost)
8222 return error_mark_node;
8224 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8225 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8226 TREE_VEC_LENGTH (new_inner_args));
8228 return new_inner_args;
8231 /* Convert all template arguments to their appropriate types, and
8232 return a vector containing the innermost resulting template
8233 arguments. If any error occurs, return error_mark_node. Error and
8234 warning messages are not issued.
8236 Note that no function argument deduction is performed, and default
8237 arguments are used to fill in unspecified arguments. */
8238 tree
8239 coerce_template_parms (tree parms, tree args, tree in_decl)
8241 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8244 /* Convert all template arguments to their appropriate type, and
8245 instantiate default arguments as needed. This returns a vector
8246 containing the innermost resulting template arguments, or
8247 error_mark_node if unsuccessful. */
8248 tree
8249 coerce_template_parms (tree parms, tree args, tree in_decl,
8250 tsubst_flags_t complain)
8252 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8255 /* Like coerce_template_parms. If PARMS represents all template
8256 parameters levels, this function returns a vector of vectors
8257 representing all the resulting argument levels. Note that in this
8258 case, only the innermost arguments are coerced because the
8259 outermost ones are supposed to have been coerced already.
8261 Otherwise, if PARMS represents only (the innermost) vector of
8262 parameters, this function returns a vector containing just the
8263 innermost resulting arguments. */
8265 static tree
8266 coerce_innermost_template_parms (tree parms,
8267 tree args,
8268 tree in_decl,
8269 tsubst_flags_t complain,
8270 bool require_all_args,
8271 bool use_default_args)
8273 int parms_depth = TMPL_PARMS_DEPTH (parms);
8274 int args_depth = TMPL_ARGS_DEPTH (args);
8275 tree coerced_args;
8277 if (parms_depth > 1)
8279 coerced_args = make_tree_vec (parms_depth);
8280 tree level;
8281 int cur_depth;
8283 for (level = parms, cur_depth = parms_depth;
8284 parms_depth > 0 && level != NULL_TREE;
8285 level = TREE_CHAIN (level), --cur_depth)
8287 tree l;
8288 if (cur_depth == args_depth)
8289 l = coerce_template_parms (TREE_VALUE (level),
8290 args, in_decl, complain,
8291 require_all_args,
8292 use_default_args);
8293 else
8294 l = TMPL_ARGS_LEVEL (args, cur_depth);
8296 if (l == error_mark_node)
8297 return error_mark_node;
8299 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8302 else
8303 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8304 args, in_decl, complain,
8305 require_all_args,
8306 use_default_args);
8307 return coerced_args;
8310 /* Returns 1 if template args OT and NT are equivalent. */
8313 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
8315 if (nt == ot)
8316 return 1;
8317 if (nt == NULL_TREE || ot == NULL_TREE)
8318 return false;
8319 if (nt == any_targ_node || ot == any_targ_node)
8320 return true;
8322 if (TREE_CODE (nt) == TREE_VEC)
8323 /* For member templates */
8324 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8325 else if (PACK_EXPANSION_P (ot))
8326 return (PACK_EXPANSION_P (nt)
8327 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8328 PACK_EXPANSION_PATTERN (nt))
8329 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8330 PACK_EXPANSION_EXTRA_ARGS (nt)));
8331 else if (ARGUMENT_PACK_P (ot))
8333 int i, len;
8334 tree opack, npack;
8336 if (!ARGUMENT_PACK_P (nt))
8337 return 0;
8339 opack = ARGUMENT_PACK_ARGS (ot);
8340 npack = ARGUMENT_PACK_ARGS (nt);
8341 len = TREE_VEC_LENGTH (opack);
8342 if (TREE_VEC_LENGTH (npack) != len)
8343 return 0;
8344 for (i = 0; i < len; ++i)
8345 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8346 TREE_VEC_ELT (npack, i)))
8347 return 0;
8348 return 1;
8350 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8351 gcc_unreachable ();
8352 else if (TYPE_P (nt))
8354 if (!TYPE_P (ot))
8355 return false;
8356 /* Don't treat an alias template specialization with dependent
8357 arguments as equivalent to its underlying type when used as a
8358 template argument; we need them to be distinct so that we
8359 substitute into the specialization arguments at instantiation
8360 time. And aliases can't be equivalent without being ==, so
8361 we don't need to look any deeper.
8363 During partial ordering, however, we need to treat them normally so
8364 that we can order uses of the same alias with different
8365 cv-qualification (79960). */
8366 if (!partial_order
8367 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
8368 return false;
8369 else
8370 return same_type_p (ot, nt);
8372 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8373 return 0;
8374 else
8376 /* Try to treat a template non-type argument that has been converted
8377 to the parameter type as equivalent to one that hasn't yet. */
8378 for (enum tree_code code1 = TREE_CODE (ot);
8379 CONVERT_EXPR_CODE_P (code1)
8380 || code1 == NON_LVALUE_EXPR;
8381 code1 = TREE_CODE (ot))
8382 ot = TREE_OPERAND (ot, 0);
8383 for (enum tree_code code2 = TREE_CODE (nt);
8384 CONVERT_EXPR_CODE_P (code2)
8385 || code2 == NON_LVALUE_EXPR;
8386 code2 = TREE_CODE (nt))
8387 nt = TREE_OPERAND (nt, 0);
8389 return cp_tree_equal (ot, nt);
8393 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8394 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8395 NEWARG_PTR with the offending arguments if they are non-NULL. */
8398 comp_template_args (tree oldargs, tree newargs,
8399 tree *oldarg_ptr, tree *newarg_ptr,
8400 bool partial_order)
8402 int i;
8404 if (oldargs == newargs)
8405 return 1;
8407 if (!oldargs || !newargs)
8408 return 0;
8410 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
8411 return 0;
8413 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
8415 tree nt = TREE_VEC_ELT (newargs, i);
8416 tree ot = TREE_VEC_ELT (oldargs, i);
8418 if (! template_args_equal (ot, nt, partial_order))
8420 if (oldarg_ptr != NULL)
8421 *oldarg_ptr = ot;
8422 if (newarg_ptr != NULL)
8423 *newarg_ptr = nt;
8424 return 0;
8427 return 1;
8430 inline bool
8431 comp_template_args_porder (tree oargs, tree nargs)
8433 return comp_template_args (oargs, nargs, NULL, NULL, true);
8436 static void
8437 add_pending_template (tree d)
8439 tree ti = (TYPE_P (d)
8440 ? CLASSTYPE_TEMPLATE_INFO (d)
8441 : DECL_TEMPLATE_INFO (d));
8442 struct pending_template *pt;
8443 int level;
8445 if (TI_PENDING_TEMPLATE_FLAG (ti))
8446 return;
8448 /* We are called both from instantiate_decl, where we've already had a
8449 tinst_level pushed, and instantiate_template, where we haven't.
8450 Compensate. */
8451 level = !current_tinst_level || current_tinst_level->decl != d;
8453 if (level)
8454 push_tinst_level (d);
8456 pt = ggc_alloc<pending_template> ();
8457 pt->next = NULL;
8458 pt->tinst = current_tinst_level;
8459 if (last_pending_template)
8460 last_pending_template->next = pt;
8461 else
8462 pending_templates = pt;
8464 last_pending_template = pt;
8466 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
8468 if (level)
8469 pop_tinst_level ();
8473 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8474 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8475 documentation for TEMPLATE_ID_EXPR. */
8477 tree
8478 lookup_template_function (tree fns, tree arglist)
8480 tree type;
8482 if (fns == error_mark_node || arglist == error_mark_node)
8483 return error_mark_node;
8485 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
8487 if (!is_overloaded_fn (fns) && !identifier_p (fns))
8489 error ("%q#D is not a function template", fns);
8490 return error_mark_node;
8493 if (BASELINK_P (fns))
8495 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
8496 unknown_type_node,
8497 BASELINK_FUNCTIONS (fns),
8498 arglist);
8499 return fns;
8502 type = TREE_TYPE (fns);
8503 if (TREE_CODE (fns) == OVERLOAD || !type)
8504 type = unknown_type_node;
8506 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
8509 /* Within the scope of a template class S<T>, the name S gets bound
8510 (in build_self_reference) to a TYPE_DECL for the class, not a
8511 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8512 or one of its enclosing classes, and that type is a template,
8513 return the associated TEMPLATE_DECL. Otherwise, the original
8514 DECL is returned.
8516 Also handle the case when DECL is a TREE_LIST of ambiguous
8517 injected-class-names from different bases. */
8519 tree
8520 maybe_get_template_decl_from_type_decl (tree decl)
8522 if (decl == NULL_TREE)
8523 return decl;
8525 /* DR 176: A lookup that finds an injected-class-name (10.2
8526 [class.member.lookup]) can result in an ambiguity in certain cases
8527 (for example, if it is found in more than one base class). If all of
8528 the injected-class-names that are found refer to specializations of
8529 the same class template, and if the name is followed by a
8530 template-argument-list, the reference refers to the class template
8531 itself and not a specialization thereof, and is not ambiguous. */
8532 if (TREE_CODE (decl) == TREE_LIST)
8534 tree t, tmpl = NULL_TREE;
8535 for (t = decl; t; t = TREE_CHAIN (t))
8537 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
8538 if (!tmpl)
8539 tmpl = elt;
8540 else if (tmpl != elt)
8541 break;
8543 if (tmpl && t == NULL_TREE)
8544 return tmpl;
8545 else
8546 return decl;
8549 return (decl != NULL_TREE
8550 && DECL_SELF_REFERENCE_P (decl)
8551 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
8552 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
8555 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8556 parameters, find the desired type.
8558 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8560 IN_DECL, if non-NULL, is the template declaration we are trying to
8561 instantiate.
8563 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8564 the class we are looking up.
8566 Issue error and warning messages under control of COMPLAIN.
8568 If the template class is really a local class in a template
8569 function, then the FUNCTION_CONTEXT is the function in which it is
8570 being instantiated.
8572 ??? Note that this function is currently called *twice* for each
8573 template-id: the first time from the parser, while creating the
8574 incomplete type (finish_template_type), and the second type during the
8575 real instantiation (instantiate_template_class). This is surely something
8576 that we want to avoid. It also causes some problems with argument
8577 coercion (see convert_nontype_argument for more information on this). */
8579 static tree
8580 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
8581 int entering_scope, tsubst_flags_t complain)
8583 tree templ = NULL_TREE, parmlist;
8584 tree t;
8585 spec_entry **slot;
8586 spec_entry *entry;
8587 spec_entry elt;
8588 hashval_t hash;
8590 if (identifier_p (d1))
8592 tree value = innermost_non_namespace_value (d1);
8593 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
8594 templ = value;
8595 else
8597 if (context)
8598 push_decl_namespace (context);
8599 templ = lookup_name (d1);
8600 templ = maybe_get_template_decl_from_type_decl (templ);
8601 if (context)
8602 pop_decl_namespace ();
8604 if (templ)
8605 context = DECL_CONTEXT (templ);
8607 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
8609 tree type = TREE_TYPE (d1);
8611 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8612 an implicit typename for the second A. Deal with it. */
8613 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
8614 type = TREE_TYPE (type);
8616 if (CLASSTYPE_TEMPLATE_INFO (type))
8618 templ = CLASSTYPE_TI_TEMPLATE (type);
8619 d1 = DECL_NAME (templ);
8622 else if (TREE_CODE (d1) == ENUMERAL_TYPE
8623 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
8625 templ = TYPE_TI_TEMPLATE (d1);
8626 d1 = DECL_NAME (templ);
8628 else if (DECL_TYPE_TEMPLATE_P (d1))
8630 templ = d1;
8631 d1 = DECL_NAME (templ);
8632 context = DECL_CONTEXT (templ);
8634 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
8636 templ = d1;
8637 d1 = DECL_NAME (templ);
8640 /* Issue an error message if we didn't find a template. */
8641 if (! templ)
8643 if (complain & tf_error)
8644 error ("%qT is not a template", d1);
8645 return error_mark_node;
8648 if (TREE_CODE (templ) != TEMPLATE_DECL
8649 /* Make sure it's a user visible template, if it was named by
8650 the user. */
8651 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
8652 && !PRIMARY_TEMPLATE_P (templ)))
8654 if (complain & tf_error)
8656 error ("non-template type %qT used as a template", d1);
8657 if (in_decl)
8658 error ("for template declaration %q+D", in_decl);
8660 return error_mark_node;
8663 complain &= ~tf_user;
8665 /* An alias that just changes the name of a template is equivalent to the
8666 other template, so if any of the arguments are pack expansions, strip
8667 the alias to avoid problems with a pack expansion passed to a non-pack
8668 alias template parameter (DR 1430). */
8669 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
8670 templ = get_underlying_template (templ);
8672 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
8674 tree parm;
8675 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
8676 if (arglist2 == error_mark_node
8677 || (!uses_template_parms (arglist2)
8678 && check_instantiated_args (templ, arglist2, complain)))
8679 return error_mark_node;
8681 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
8682 return parm;
8684 else
8686 tree template_type = TREE_TYPE (templ);
8687 tree gen_tmpl;
8688 tree type_decl;
8689 tree found = NULL_TREE;
8690 int arg_depth;
8691 int parm_depth;
8692 int is_dependent_type;
8693 int use_partial_inst_tmpl = false;
8695 if (template_type == error_mark_node)
8696 /* An error occurred while building the template TEMPL, and a
8697 diagnostic has most certainly been emitted for that
8698 already. Let's propagate that error. */
8699 return error_mark_node;
8701 gen_tmpl = most_general_template (templ);
8702 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
8703 parm_depth = TMPL_PARMS_DEPTH (parmlist);
8704 arg_depth = TMPL_ARGS_DEPTH (arglist);
8706 if (arg_depth == 1 && parm_depth > 1)
8708 /* We've been given an incomplete set of template arguments.
8709 For example, given:
8711 template <class T> struct S1 {
8712 template <class U> struct S2 {};
8713 template <class U> struct S2<U*> {};
8716 we will be called with an ARGLIST of `U*', but the
8717 TEMPLATE will be `template <class T> template
8718 <class U> struct S1<T>::S2'. We must fill in the missing
8719 arguments. */
8720 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
8721 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
8722 arg_depth = TMPL_ARGS_DEPTH (arglist);
8725 /* Now we should have enough arguments. */
8726 gcc_assert (parm_depth == arg_depth);
8728 /* From here on, we're only interested in the most general
8729 template. */
8731 /* Calculate the BOUND_ARGS. These will be the args that are
8732 actually tsubst'd into the definition to create the
8733 instantiation. */
8734 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
8735 complain,
8736 /*require_all_args=*/true,
8737 /*use_default_args=*/true);
8739 if (arglist == error_mark_node)
8740 /* We were unable to bind the arguments. */
8741 return error_mark_node;
8743 /* In the scope of a template class, explicit references to the
8744 template class refer to the type of the template, not any
8745 instantiation of it. For example, in:
8747 template <class T> class C { void f(C<T>); }
8749 the `C<T>' is just the same as `C'. Outside of the
8750 class, however, such a reference is an instantiation. */
8751 if (entering_scope
8752 || !PRIMARY_TEMPLATE_P (gen_tmpl)
8753 || currently_open_class (template_type))
8755 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
8757 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
8758 return template_type;
8761 /* If we already have this specialization, return it. */
8762 elt.tmpl = gen_tmpl;
8763 elt.args = arglist;
8764 elt.spec = NULL_TREE;
8765 hash = spec_hasher::hash (&elt);
8766 entry = type_specializations->find_with_hash (&elt, hash);
8768 if (entry)
8769 return entry->spec;
8771 /* If the the template's constraints are not satisfied,
8772 then we cannot form a valid type.
8774 Note that the check is deferred until after the hash
8775 lookup. This prevents redundant checks on previously
8776 instantiated specializations. */
8777 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
8779 if (complain & tf_error)
8781 error ("template constraint failure");
8782 diagnose_constraints (input_location, gen_tmpl, arglist);
8784 return error_mark_node;
8787 is_dependent_type = uses_template_parms (arglist);
8789 /* If the deduced arguments are invalid, then the binding
8790 failed. */
8791 if (!is_dependent_type
8792 && check_instantiated_args (gen_tmpl,
8793 INNERMOST_TEMPLATE_ARGS (arglist),
8794 complain))
8795 return error_mark_node;
8797 if (!is_dependent_type
8798 && !PRIMARY_TEMPLATE_P (gen_tmpl)
8799 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
8800 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
8802 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
8803 DECL_NAME (gen_tmpl),
8804 /*tag_scope=*/ts_global);
8805 return found;
8808 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
8809 complain, in_decl);
8810 if (context == error_mark_node)
8811 return error_mark_node;
8813 if (!context)
8814 context = global_namespace;
8816 /* Create the type. */
8817 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8819 /* The user referred to a specialization of an alias
8820 template represented by GEN_TMPL.
8822 [temp.alias]/2 says:
8824 When a template-id refers to the specialization of an
8825 alias template, it is equivalent to the associated
8826 type obtained by substitution of its
8827 template-arguments for the template-parameters in the
8828 type-id of the alias template. */
8830 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
8831 /* Note that the call above (by indirectly calling
8832 register_specialization in tsubst_decl) registers the
8833 TYPE_DECL representing the specialization of the alias
8834 template. So next time someone substitutes ARGLIST for
8835 the template parms into the alias template (GEN_TMPL),
8836 she'll get that TYPE_DECL back. */
8838 if (t == error_mark_node)
8839 return t;
8841 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
8843 if (!is_dependent_type)
8845 set_current_access_from_decl (TYPE_NAME (template_type));
8846 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
8847 tsubst (ENUM_UNDERLYING_TYPE (template_type),
8848 arglist, complain, in_decl),
8849 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
8850 arglist, complain, in_decl),
8851 SCOPED_ENUM_P (template_type), NULL);
8853 if (t == error_mark_node)
8854 return t;
8856 else
8858 /* We don't want to call start_enum for this type, since
8859 the values for the enumeration constants may involve
8860 template parameters. And, no one should be interested
8861 in the enumeration constants for such a type. */
8862 t = cxx_make_type (ENUMERAL_TYPE);
8863 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
8865 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
8866 ENUM_FIXED_UNDERLYING_TYPE_P (t)
8867 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
8869 else if (CLASS_TYPE_P (template_type))
8871 t = make_class_type (TREE_CODE (template_type));
8872 CLASSTYPE_DECLARED_CLASS (t)
8873 = CLASSTYPE_DECLARED_CLASS (template_type);
8874 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
8876 /* A local class. Make sure the decl gets registered properly. */
8877 if (context == current_function_decl)
8878 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
8880 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
8881 /* This instantiation is another name for the primary
8882 template type. Set the TYPE_CANONICAL field
8883 appropriately. */
8884 TYPE_CANONICAL (t) = template_type;
8885 else if (any_template_arguments_need_structural_equality_p (arglist))
8886 /* Some of the template arguments require structural
8887 equality testing, so this template class requires
8888 structural equality testing. */
8889 SET_TYPE_STRUCTURAL_EQUALITY (t);
8891 else
8892 gcc_unreachable ();
8894 /* If we called start_enum or pushtag above, this information
8895 will already be set up. */
8896 if (!TYPE_NAME (t))
8898 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
8900 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
8901 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
8902 DECL_SOURCE_LOCATION (type_decl)
8903 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
8905 else
8906 type_decl = TYPE_NAME (t);
8908 if (CLASS_TYPE_P (template_type))
8910 TREE_PRIVATE (type_decl)
8911 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
8912 TREE_PROTECTED (type_decl)
8913 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
8914 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
8916 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
8917 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
8921 if (OVERLOAD_TYPE_P (t)
8922 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8924 static const char *tags[] = {"abi_tag", "may_alias"};
8926 for (unsigned ix = 0; ix != 2; ix++)
8928 tree attributes
8929 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
8931 if (attributes)
8932 TYPE_ATTRIBUTES (t)
8933 = tree_cons (TREE_PURPOSE (attributes),
8934 TREE_VALUE (attributes),
8935 TYPE_ATTRIBUTES (t));
8939 /* Let's consider the explicit specialization of a member
8940 of a class template specialization that is implicitly instantiated,
8941 e.g.:
8942 template<class T>
8943 struct S
8945 template<class U> struct M {}; //#0
8948 template<>
8949 template<>
8950 struct S<int>::M<char> //#1
8952 int i;
8954 [temp.expl.spec]/4 says this is valid.
8956 In this case, when we write:
8957 S<int>::M<char> m;
8959 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
8960 the one of #0.
8962 When we encounter #1, we want to store the partial instantiation
8963 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
8965 For all cases other than this "explicit specialization of member of a
8966 class template", we just want to store the most general template into
8967 the CLASSTYPE_TI_TEMPLATE of M.
8969 This case of "explicit specialization of member of a class template"
8970 only happens when:
8971 1/ the enclosing class is an instantiation of, and therefore not
8972 the same as, the context of the most general template, and
8973 2/ we aren't looking at the partial instantiation itself, i.e.
8974 the innermost arguments are not the same as the innermost parms of
8975 the most general template.
8977 So it's only when 1/ and 2/ happens that we want to use the partial
8978 instantiation of the member template in lieu of its most general
8979 template. */
8981 if (PRIMARY_TEMPLATE_P (gen_tmpl)
8982 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
8983 /* the enclosing class must be an instantiation... */
8984 && CLASS_TYPE_P (context)
8985 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
8987 TREE_VEC_LENGTH (arglist)--;
8988 ++processing_template_decl;
8989 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
8990 tree partial_inst_args =
8991 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
8992 arglist, complain, NULL_TREE);
8993 --processing_template_decl;
8994 TREE_VEC_LENGTH (arglist)++;
8995 if (partial_inst_args == error_mark_node)
8996 return error_mark_node;
8997 use_partial_inst_tmpl =
8998 /*...and we must not be looking at the partial instantiation
8999 itself. */
9000 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
9001 partial_inst_args);
9004 if (!use_partial_inst_tmpl)
9005 /* This case is easy; there are no member templates involved. */
9006 found = gen_tmpl;
9007 else
9009 /* This is a full instantiation of a member template. Find
9010 the partial instantiation of which this is an instance. */
9012 /* Temporarily reduce by one the number of levels in the ARGLIST
9013 so as to avoid comparing the last set of arguments. */
9014 TREE_VEC_LENGTH (arglist)--;
9015 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
9016 TREE_VEC_LENGTH (arglist)++;
9017 /* FOUND is either a proper class type, or an alias
9018 template specialization. In the later case, it's a
9019 TYPE_DECL, resulting from the substituting of arguments
9020 for parameters in the TYPE_DECL of the alias template
9021 done earlier. So be careful while getting the template
9022 of FOUND. */
9023 found = (TREE_CODE (found) == TEMPLATE_DECL
9024 ? found
9025 : (TREE_CODE (found) == TYPE_DECL
9026 ? DECL_TI_TEMPLATE (found)
9027 : CLASSTYPE_TI_TEMPLATE (found)));
9030 // Build template info for the new specialization.
9031 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
9033 elt.spec = t;
9034 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
9035 entry = ggc_alloc<spec_entry> ();
9036 *entry = elt;
9037 *slot = entry;
9039 /* Note this use of the partial instantiation so we can check it
9040 later in maybe_process_partial_specialization. */
9041 DECL_TEMPLATE_INSTANTIATIONS (found)
9042 = tree_cons (arglist, t,
9043 DECL_TEMPLATE_INSTANTIATIONS (found));
9045 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
9046 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9047 /* Now that the type has been registered on the instantiations
9048 list, we set up the enumerators. Because the enumeration
9049 constants may involve the enumeration type itself, we make
9050 sure to register the type first, and then create the
9051 constants. That way, doing tsubst_expr for the enumeration
9052 constants won't result in recursive calls here; we'll find
9053 the instantiation and exit above. */
9054 tsubst_enum (template_type, t, arglist);
9056 if (CLASS_TYPE_P (template_type) && is_dependent_type)
9057 /* If the type makes use of template parameters, the
9058 code that generates debugging information will crash. */
9059 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
9061 /* Possibly limit visibility based on template args. */
9062 TREE_PUBLIC (type_decl) = 1;
9063 determine_visibility (type_decl);
9065 inherit_targ_abi_tags (t);
9067 return t;
9071 /* Wrapper for lookup_template_class_1. */
9073 tree
9074 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
9075 int entering_scope, tsubst_flags_t complain)
9077 tree ret;
9078 timevar_push (TV_TEMPLATE_INST);
9079 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
9080 entering_scope, complain);
9081 timevar_pop (TV_TEMPLATE_INST);
9082 return ret;
9085 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9087 tree
9088 lookup_template_variable (tree templ, tree arglist)
9090 /* The type of the expression is NULL_TREE since the template-id could refer
9091 to an explicit or partial specialization. */
9092 tree type = NULL_TREE;
9093 if (flag_concepts && variable_concept_p (templ))
9094 /* Except that concepts are always bool. */
9095 type = boolean_type_node;
9096 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
9099 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9101 tree
9102 finish_template_variable (tree var, tsubst_flags_t complain)
9104 tree templ = TREE_OPERAND (var, 0);
9105 tree arglist = TREE_OPERAND (var, 1);
9107 /* We never want to return a VAR_DECL for a variable concept, since they
9108 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9109 bool concept_p = flag_concepts && variable_concept_p (templ);
9110 if (concept_p && processing_template_decl)
9111 return var;
9113 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
9114 arglist = add_outermost_template_args (tmpl_args, arglist);
9116 templ = most_general_template (templ);
9117 tree parms = DECL_TEMPLATE_PARMS (templ);
9118 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
9119 /*req_all*/true,
9120 /*use_default*/true);
9122 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
9124 if (complain & tf_error)
9126 error ("use of invalid variable template %qE", var);
9127 diagnose_constraints (location_of (var), templ, arglist);
9129 return error_mark_node;
9132 /* If a template-id refers to a specialization of a variable
9133 concept, then the expression is true if and only if the
9134 concept's constraints are satisfied by the given template
9135 arguments.
9137 NOTE: This is an extension of Concepts Lite TS that
9138 allows constraints to be used in expressions. */
9139 if (concept_p)
9141 tree decl = DECL_TEMPLATE_RESULT (templ);
9142 return evaluate_variable_concept (decl, arglist);
9145 return instantiate_template (templ, arglist, complain);
9148 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9149 TARGS template args, and instantiate it if it's not dependent. */
9151 tree
9152 lookup_and_finish_template_variable (tree templ, tree targs,
9153 tsubst_flags_t complain)
9155 templ = lookup_template_variable (templ, targs);
9156 if (!any_dependent_template_arguments_p (targs))
9158 templ = finish_template_variable (templ, complain);
9159 mark_used (templ);
9162 return convert_from_reference (templ);
9166 struct pair_fn_data
9168 tree_fn_t fn;
9169 tree_fn_t any_fn;
9170 void *data;
9171 /* True when we should also visit template parameters that occur in
9172 non-deduced contexts. */
9173 bool include_nondeduced_p;
9174 hash_set<tree> *visited;
9177 /* Called from for_each_template_parm via walk_tree. */
9179 static tree
9180 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
9182 tree t = *tp;
9183 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
9184 tree_fn_t fn = pfd->fn;
9185 void *data = pfd->data;
9186 tree result = NULL_TREE;
9188 #define WALK_SUBTREE(NODE) \
9189 do \
9191 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9192 pfd->include_nondeduced_p, \
9193 pfd->any_fn); \
9194 if (result) goto out; \
9196 while (0)
9198 if (pfd->any_fn && (*pfd->any_fn)(t, data))
9199 return t;
9201 if (TYPE_P (t)
9202 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
9203 WALK_SUBTREE (TYPE_CONTEXT (t));
9205 switch (TREE_CODE (t))
9207 case RECORD_TYPE:
9208 if (TYPE_PTRMEMFUNC_P (t))
9209 break;
9210 /* Fall through. */
9212 case UNION_TYPE:
9213 case ENUMERAL_TYPE:
9214 if (!TYPE_TEMPLATE_INFO (t))
9215 *walk_subtrees = 0;
9216 else
9217 WALK_SUBTREE (TYPE_TI_ARGS (t));
9218 break;
9220 case INTEGER_TYPE:
9221 WALK_SUBTREE (TYPE_MIN_VALUE (t));
9222 WALK_SUBTREE (TYPE_MAX_VALUE (t));
9223 break;
9225 case METHOD_TYPE:
9226 /* Since we're not going to walk subtrees, we have to do this
9227 explicitly here. */
9228 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
9229 /* Fall through. */
9231 case FUNCTION_TYPE:
9232 /* Check the return type. */
9233 WALK_SUBTREE (TREE_TYPE (t));
9235 /* Check the parameter types. Since default arguments are not
9236 instantiated until they are needed, the TYPE_ARG_TYPES may
9237 contain expressions that involve template parameters. But,
9238 no-one should be looking at them yet. And, once they're
9239 instantiated, they don't contain template parameters, so
9240 there's no point in looking at them then, either. */
9242 tree parm;
9244 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
9245 WALK_SUBTREE (TREE_VALUE (parm));
9247 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9248 want walk_tree walking into them itself. */
9249 *walk_subtrees = 0;
9252 if (flag_noexcept_type)
9254 tree spec = TYPE_RAISES_EXCEPTIONS (t);
9255 if (spec)
9256 WALK_SUBTREE (TREE_PURPOSE (spec));
9258 break;
9260 case TYPEOF_TYPE:
9261 case UNDERLYING_TYPE:
9262 if (pfd->include_nondeduced_p
9263 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
9264 pfd->visited,
9265 pfd->include_nondeduced_p,
9266 pfd->any_fn))
9267 return error_mark_node;
9268 break;
9270 case FUNCTION_DECL:
9271 case VAR_DECL:
9272 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9273 WALK_SUBTREE (DECL_TI_ARGS (t));
9274 /* Fall through. */
9276 case PARM_DECL:
9277 case CONST_DECL:
9278 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
9279 WALK_SUBTREE (DECL_INITIAL (t));
9280 if (DECL_CONTEXT (t)
9281 && pfd->include_nondeduced_p)
9282 WALK_SUBTREE (DECL_CONTEXT (t));
9283 break;
9285 case BOUND_TEMPLATE_TEMPLATE_PARM:
9286 /* Record template parameters such as `T' inside `TT<T>'. */
9287 WALK_SUBTREE (TYPE_TI_ARGS (t));
9288 /* Fall through. */
9290 case TEMPLATE_TEMPLATE_PARM:
9291 case TEMPLATE_TYPE_PARM:
9292 case TEMPLATE_PARM_INDEX:
9293 if (fn && (*fn)(t, data))
9294 return t;
9295 else if (!fn)
9296 return t;
9297 break;
9299 case TEMPLATE_DECL:
9300 /* A template template parameter is encountered. */
9301 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9302 WALK_SUBTREE (TREE_TYPE (t));
9304 /* Already substituted template template parameter */
9305 *walk_subtrees = 0;
9306 break;
9308 case TYPENAME_TYPE:
9309 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9310 partial instantiation. */
9311 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
9312 break;
9314 case CONSTRUCTOR:
9315 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
9316 && pfd->include_nondeduced_p)
9317 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
9318 break;
9320 case INDIRECT_REF:
9321 case COMPONENT_REF:
9322 /* If there's no type, then this thing must be some expression
9323 involving template parameters. */
9324 if (!fn && !TREE_TYPE (t))
9325 return error_mark_node;
9326 break;
9328 case MODOP_EXPR:
9329 case CAST_EXPR:
9330 case IMPLICIT_CONV_EXPR:
9331 case REINTERPRET_CAST_EXPR:
9332 case CONST_CAST_EXPR:
9333 case STATIC_CAST_EXPR:
9334 case DYNAMIC_CAST_EXPR:
9335 case ARROW_EXPR:
9336 case DOTSTAR_EXPR:
9337 case TYPEID_EXPR:
9338 case PSEUDO_DTOR_EXPR:
9339 if (!fn)
9340 return error_mark_node;
9341 break;
9343 default:
9344 break;
9347 #undef WALK_SUBTREE
9349 /* We didn't find any template parameters we liked. */
9350 out:
9351 return result;
9354 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9355 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9356 call FN with the parameter and the DATA.
9357 If FN returns nonzero, the iteration is terminated, and
9358 for_each_template_parm returns 1. Otherwise, the iteration
9359 continues. If FN never returns a nonzero value, the value
9360 returned by for_each_template_parm is 0. If FN is NULL, it is
9361 considered to be the function which always returns 1.
9363 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9364 parameters that occur in non-deduced contexts. When false, only
9365 visits those template parameters that can be deduced. */
9367 static tree
9368 for_each_template_parm (tree t, tree_fn_t fn, void* data,
9369 hash_set<tree> *visited,
9370 bool include_nondeduced_p,
9371 tree_fn_t any_fn)
9373 struct pair_fn_data pfd;
9374 tree result;
9376 /* Set up. */
9377 pfd.fn = fn;
9378 pfd.any_fn = any_fn;
9379 pfd.data = data;
9380 pfd.include_nondeduced_p = include_nondeduced_p;
9382 /* Walk the tree. (Conceptually, we would like to walk without
9383 duplicates, but for_each_template_parm_r recursively calls
9384 for_each_template_parm, so we would need to reorganize a fair
9385 bit to use walk_tree_without_duplicates, so we keep our own
9386 visited list.) */
9387 if (visited)
9388 pfd.visited = visited;
9389 else
9390 pfd.visited = new hash_set<tree>;
9391 result = cp_walk_tree (&t,
9392 for_each_template_parm_r,
9393 &pfd,
9394 pfd.visited);
9396 /* Clean up. */
9397 if (!visited)
9399 delete pfd.visited;
9400 pfd.visited = 0;
9403 return result;
9406 /* Returns true if T depends on any template parameter. */
9409 uses_template_parms (tree t)
9411 if (t == NULL_TREE)
9412 return false;
9414 bool dependent_p;
9415 int saved_processing_template_decl;
9417 saved_processing_template_decl = processing_template_decl;
9418 if (!saved_processing_template_decl)
9419 processing_template_decl = 1;
9420 if (TYPE_P (t))
9421 dependent_p = dependent_type_p (t);
9422 else if (TREE_CODE (t) == TREE_VEC)
9423 dependent_p = any_dependent_template_arguments_p (t);
9424 else if (TREE_CODE (t) == TREE_LIST)
9425 dependent_p = (uses_template_parms (TREE_VALUE (t))
9426 || uses_template_parms (TREE_CHAIN (t)));
9427 else if (TREE_CODE (t) == TYPE_DECL)
9428 dependent_p = dependent_type_p (TREE_TYPE (t));
9429 else if (DECL_P (t)
9430 || EXPR_P (t)
9431 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
9432 || TREE_CODE (t) == OVERLOAD
9433 || BASELINK_P (t)
9434 || identifier_p (t)
9435 || TREE_CODE (t) == TRAIT_EXPR
9436 || TREE_CODE (t) == CONSTRUCTOR
9437 || CONSTANT_CLASS_P (t))
9438 dependent_p = (type_dependent_expression_p (t)
9439 || value_dependent_expression_p (t));
9440 else
9442 gcc_assert (t == error_mark_node);
9443 dependent_p = false;
9446 processing_template_decl = saved_processing_template_decl;
9448 return dependent_p;
9451 /* Returns true iff current_function_decl is an incompletely instantiated
9452 template. Useful instead of processing_template_decl because the latter
9453 is set to 0 during instantiate_non_dependent_expr. */
9455 bool
9456 in_template_function (void)
9458 tree fn = current_function_decl;
9459 bool ret;
9460 ++processing_template_decl;
9461 ret = (fn && DECL_LANG_SPECIFIC (fn)
9462 && DECL_TEMPLATE_INFO (fn)
9463 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
9464 --processing_template_decl;
9465 return ret;
9468 /* Returns true if T depends on any template parameter with level LEVEL. */
9470 bool
9471 uses_template_parms_level (tree t, int level)
9473 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
9474 /*include_nondeduced_p=*/true);
9477 /* Returns true if the signature of DECL depends on any template parameter from
9478 its enclosing class. */
9480 bool
9481 uses_outer_template_parms (tree decl)
9483 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
9484 if (depth == 0)
9485 return false;
9486 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
9487 &depth, NULL, /*include_nondeduced_p=*/true))
9488 return true;
9489 if (PRIMARY_TEMPLATE_P (decl)
9490 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9491 (DECL_TEMPLATE_PARMS (decl)),
9492 template_parm_outer_level,
9493 &depth, NULL, /*include_nondeduced_p=*/true))
9494 return true;
9495 tree ci = get_constraints (decl);
9496 if (ci)
9497 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
9498 if (ci && for_each_template_parm (ci, template_parm_outer_level,
9499 &depth, NULL, /*nondeduced*/true))
9500 return true;
9501 return false;
9504 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9505 ill-formed translation unit, i.e. a variable or function that isn't
9506 usable in a constant expression. */
9508 static inline bool
9509 neglectable_inst_p (tree d)
9511 return (DECL_P (d)
9512 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
9513 : decl_maybe_constant_var_p (d)));
9516 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9517 neglectable and instantiated from within an erroneous instantiation. */
9519 static bool
9520 limit_bad_template_recursion (tree decl)
9522 struct tinst_level *lev = current_tinst_level;
9523 int errs = errorcount + sorrycount;
9524 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
9525 return false;
9527 for (; lev; lev = lev->next)
9528 if (neglectable_inst_p (lev->decl))
9529 break;
9531 return (lev && errs > lev->errors);
9534 static int tinst_depth;
9535 extern int max_tinst_depth;
9536 int depth_reached;
9538 static GTY(()) struct tinst_level *last_error_tinst_level;
9540 /* We're starting to instantiate D; record the template instantiation context
9541 for diagnostics and to restore it later. */
9543 bool
9544 push_tinst_level (tree d)
9546 return push_tinst_level_loc (d, input_location);
9549 /* We're starting to instantiate D; record the template instantiation context
9550 at LOC for diagnostics and to restore it later. */
9552 bool
9553 push_tinst_level_loc (tree d, location_t loc)
9555 struct tinst_level *new_level;
9557 if (tinst_depth >= max_tinst_depth)
9559 /* Tell error.c not to try to instantiate any templates. */
9560 at_eof = 2;
9561 fatal_error (input_location,
9562 "template instantiation depth exceeds maximum of %d"
9563 " (use -ftemplate-depth= to increase the maximum)",
9564 max_tinst_depth);
9565 return false;
9568 /* If the current instantiation caused problems, don't let it instantiate
9569 anything else. Do allow deduction substitution and decls usable in
9570 constant expressions. */
9571 if (limit_bad_template_recursion (d))
9572 return false;
9574 /* When not -quiet, dump template instantiations other than functions, since
9575 announce_function will take care of those. */
9576 if (!quiet_flag
9577 && TREE_CODE (d) != TREE_LIST
9578 && TREE_CODE (d) != FUNCTION_DECL)
9579 fprintf (stderr, " %s", decl_as_string (d, TFF_DECL_SPECIFIERS));
9581 new_level = ggc_alloc<tinst_level> ();
9582 new_level->decl = d;
9583 new_level->locus = loc;
9584 new_level->errors = errorcount+sorrycount;
9585 new_level->in_system_header_p = in_system_header_at (input_location);
9586 new_level->next = current_tinst_level;
9587 current_tinst_level = new_level;
9589 ++tinst_depth;
9590 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
9591 depth_reached = tinst_depth;
9593 return true;
9596 /* We're done instantiating this template; return to the instantiation
9597 context. */
9599 void
9600 pop_tinst_level (void)
9602 /* Restore the filename and line number stashed away when we started
9603 this instantiation. */
9604 input_location = current_tinst_level->locus;
9605 current_tinst_level = current_tinst_level->next;
9606 --tinst_depth;
9609 /* We're instantiating a deferred template; restore the template
9610 instantiation context in which the instantiation was requested, which
9611 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9613 static tree
9614 reopen_tinst_level (struct tinst_level *level)
9616 struct tinst_level *t;
9618 tinst_depth = 0;
9619 for (t = level; t; t = t->next)
9620 ++tinst_depth;
9622 current_tinst_level = level;
9623 pop_tinst_level ();
9624 if (current_tinst_level)
9625 current_tinst_level->errors = errorcount+sorrycount;
9626 return level->decl;
9629 /* Returns the TINST_LEVEL which gives the original instantiation
9630 context. */
9632 struct tinst_level *
9633 outermost_tinst_level (void)
9635 struct tinst_level *level = current_tinst_level;
9636 if (level)
9637 while (level->next)
9638 level = level->next;
9639 return level;
9642 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9643 vector of template arguments, as for tsubst.
9645 Returns an appropriate tsubst'd friend declaration. */
9647 static tree
9648 tsubst_friend_function (tree decl, tree args)
9650 tree new_friend;
9652 if (TREE_CODE (decl) == FUNCTION_DECL
9653 && DECL_TEMPLATE_INSTANTIATION (decl)
9654 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
9655 /* This was a friend declared with an explicit template
9656 argument list, e.g.:
9658 friend void f<>(T);
9660 to indicate that f was a template instantiation, not a new
9661 function declaration. Now, we have to figure out what
9662 instantiation of what template. */
9664 tree template_id, arglist, fns;
9665 tree new_args;
9666 tree tmpl;
9667 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
9669 /* Friend functions are looked up in the containing namespace scope.
9670 We must enter that scope, to avoid finding member functions of the
9671 current class with same name. */
9672 push_nested_namespace (ns);
9673 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
9674 tf_warning_or_error, NULL_TREE,
9675 /*integral_constant_expression_p=*/false);
9676 pop_nested_namespace (ns);
9677 arglist = tsubst (DECL_TI_ARGS (decl), args,
9678 tf_warning_or_error, NULL_TREE);
9679 template_id = lookup_template_function (fns, arglist);
9681 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9682 tmpl = determine_specialization (template_id, new_friend,
9683 &new_args,
9684 /*need_member_template=*/0,
9685 TREE_VEC_LENGTH (args),
9686 tsk_none);
9687 return instantiate_template (tmpl, new_args, tf_error);
9690 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9692 /* The NEW_FRIEND will look like an instantiation, to the
9693 compiler, but is not an instantiation from the point of view of
9694 the language. For example, we might have had:
9696 template <class T> struct S {
9697 template <class U> friend void f(T, U);
9700 Then, in S<int>, template <class U> void f(int, U) is not an
9701 instantiation of anything. */
9702 if (new_friend == error_mark_node)
9703 return error_mark_node;
9705 DECL_USE_TEMPLATE (new_friend) = 0;
9706 if (TREE_CODE (decl) == TEMPLATE_DECL)
9708 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
9709 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
9710 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
9713 /* The mangled name for the NEW_FRIEND is incorrect. The function
9714 is not a template instantiation and should not be mangled like
9715 one. Therefore, we forget the mangling here; we'll recompute it
9716 later if we need it. */
9717 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
9719 SET_DECL_RTL (new_friend, NULL);
9720 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
9723 if (DECL_NAMESPACE_SCOPE_P (new_friend))
9725 tree old_decl;
9726 tree new_friend_template_info;
9727 tree new_friend_result_template_info;
9728 tree ns;
9729 int new_friend_is_defn;
9731 /* We must save some information from NEW_FRIEND before calling
9732 duplicate decls since that function will free NEW_FRIEND if
9733 possible. */
9734 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
9735 new_friend_is_defn =
9736 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9737 (template_for_substitution (new_friend)))
9738 != NULL_TREE);
9739 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
9741 /* This declaration is a `primary' template. */
9742 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
9744 new_friend_result_template_info
9745 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
9747 else
9748 new_friend_result_template_info = NULL_TREE;
9750 /* Inside pushdecl_namespace_level, we will push into the
9751 current namespace. However, the friend function should go
9752 into the namespace of the template. */
9753 ns = decl_namespace_context (new_friend);
9754 push_nested_namespace (ns);
9755 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
9756 pop_nested_namespace (ns);
9758 if (old_decl == error_mark_node)
9759 return error_mark_node;
9761 if (old_decl != new_friend)
9763 /* This new friend declaration matched an existing
9764 declaration. For example, given:
9766 template <class T> void f(T);
9767 template <class U> class C {
9768 template <class T> friend void f(T) {}
9771 the friend declaration actually provides the definition
9772 of `f', once C has been instantiated for some type. So,
9773 old_decl will be the out-of-class template declaration,
9774 while new_friend is the in-class definition.
9776 But, if `f' was called before this point, the
9777 instantiation of `f' will have DECL_TI_ARGS corresponding
9778 to `T' but not to `U', references to which might appear
9779 in the definition of `f'. Previously, the most general
9780 template for an instantiation of `f' was the out-of-class
9781 version; now it is the in-class version. Therefore, we
9782 run through all specialization of `f', adding to their
9783 DECL_TI_ARGS appropriately. In particular, they need a
9784 new set of outer arguments, corresponding to the
9785 arguments for this class instantiation.
9787 The same situation can arise with something like this:
9789 friend void f(int);
9790 template <class T> class C {
9791 friend void f(T) {}
9794 when `C<int>' is instantiated. Now, `f(int)' is defined
9795 in the class. */
9797 if (!new_friend_is_defn)
9798 /* On the other hand, if the in-class declaration does
9799 *not* provide a definition, then we don't want to alter
9800 existing definitions. We can just leave everything
9801 alone. */
9803 else
9805 tree new_template = TI_TEMPLATE (new_friend_template_info);
9806 tree new_args = TI_ARGS (new_friend_template_info);
9808 /* Overwrite whatever template info was there before, if
9809 any, with the new template information pertaining to
9810 the declaration. */
9811 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
9813 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
9815 /* We should have called reregister_specialization in
9816 duplicate_decls. */
9817 gcc_assert (retrieve_specialization (new_template,
9818 new_args, 0)
9819 == old_decl);
9821 /* Instantiate it if the global has already been used. */
9822 if (DECL_ODR_USED (old_decl))
9823 instantiate_decl (old_decl, /*defer_ok=*/true,
9824 /*expl_inst_class_mem_p=*/false);
9826 else
9828 tree t;
9830 /* Indicate that the old function template is a partial
9831 instantiation. */
9832 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
9833 = new_friend_result_template_info;
9835 gcc_assert (new_template
9836 == most_general_template (new_template));
9837 gcc_assert (new_template != old_decl);
9839 /* Reassign any specializations already in the hash table
9840 to the new more general template, and add the
9841 additional template args. */
9842 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
9843 t != NULL_TREE;
9844 t = TREE_CHAIN (t))
9846 tree spec = TREE_VALUE (t);
9847 spec_entry elt;
9849 elt.tmpl = old_decl;
9850 elt.args = DECL_TI_ARGS (spec);
9851 elt.spec = NULL_TREE;
9853 decl_specializations->remove_elt (&elt);
9855 DECL_TI_ARGS (spec)
9856 = add_outermost_template_args (new_args,
9857 DECL_TI_ARGS (spec));
9859 register_specialization
9860 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
9863 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
9867 /* The information from NEW_FRIEND has been merged into OLD_DECL
9868 by duplicate_decls. */
9869 new_friend = old_decl;
9872 else
9874 tree context = DECL_CONTEXT (new_friend);
9875 bool dependent_p;
9877 /* In the code
9878 template <class T> class C {
9879 template <class U> friend void C1<U>::f (); // case 1
9880 friend void C2<T>::f (); // case 2
9882 we only need to make sure CONTEXT is a complete type for
9883 case 2. To distinguish between the two cases, we note that
9884 CONTEXT of case 1 remains dependent type after tsubst while
9885 this isn't true for case 2. */
9886 ++processing_template_decl;
9887 dependent_p = dependent_type_p (context);
9888 --processing_template_decl;
9890 if (!dependent_p
9891 && !complete_type_or_else (context, NULL_TREE))
9892 return error_mark_node;
9894 if (COMPLETE_TYPE_P (context))
9896 tree fn = new_friend;
9897 /* do_friend adds the TEMPLATE_DECL for any member friend
9898 template even if it isn't a member template, i.e.
9899 template <class T> friend A<T>::f();
9900 Look through it in that case. */
9901 if (TREE_CODE (fn) == TEMPLATE_DECL
9902 && !PRIMARY_TEMPLATE_P (fn))
9903 fn = DECL_TEMPLATE_RESULT (fn);
9904 /* Check to see that the declaration is really present, and,
9905 possibly obtain an improved declaration. */
9906 fn = check_classfn (context, fn, NULL_TREE);
9908 if (fn)
9909 new_friend = fn;
9913 return new_friend;
9916 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
9917 template arguments, as for tsubst.
9919 Returns an appropriate tsubst'd friend type or error_mark_node on
9920 failure. */
9922 static tree
9923 tsubst_friend_class (tree friend_tmpl, tree args)
9925 tree friend_type;
9926 tree tmpl;
9927 tree context;
9929 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
9931 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
9932 return TREE_TYPE (t);
9935 context = CP_DECL_CONTEXT (friend_tmpl);
9937 if (context != global_namespace)
9939 if (TREE_CODE (context) == NAMESPACE_DECL)
9940 push_nested_namespace (context);
9941 else
9942 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
9945 /* Look for a class template declaration. We look for hidden names
9946 because two friend declarations of the same template are the
9947 same. For example, in:
9949 struct A {
9950 template <typename> friend class F;
9952 template <typename> struct B {
9953 template <typename> friend class F;
9956 both F templates are the same. */
9957 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
9958 /*block_p=*/true, 0, LOOKUP_HIDDEN);
9960 /* But, if we don't find one, it might be because we're in a
9961 situation like this:
9963 template <class T>
9964 struct S {
9965 template <class U>
9966 friend struct S;
9969 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
9970 for `S<int>', not the TEMPLATE_DECL. */
9971 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
9973 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
9974 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
9977 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
9979 /* The friend template has already been declared. Just
9980 check to see that the declarations match, and install any new
9981 default parameters. We must tsubst the default parameters,
9982 of course. We only need the innermost template parameters
9983 because that is all that redeclare_class_template will look
9984 at. */
9985 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
9986 > TMPL_ARGS_DEPTH (args))
9988 tree parms;
9989 location_t saved_input_location;
9990 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
9991 args, tf_warning_or_error);
9993 saved_input_location = input_location;
9994 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
9995 tree cons = get_constraints (tmpl);
9996 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
9997 input_location = saved_input_location;
10001 friend_type = TREE_TYPE (tmpl);
10003 else
10005 /* The friend template has not already been declared. In this
10006 case, the instantiation of the template class will cause the
10007 injection of this template into the global scope. */
10008 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
10009 if (tmpl == error_mark_node)
10010 return error_mark_node;
10012 /* The new TMPL is not an instantiation of anything, so we
10013 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
10014 the new type because that is supposed to be the corresponding
10015 template decl, i.e., TMPL. */
10016 DECL_USE_TEMPLATE (tmpl) = 0;
10017 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
10018 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
10019 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
10020 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
10022 /* Inject this template into the global scope. */
10023 friend_type = TREE_TYPE (pushdecl_top_level (tmpl, true));
10026 if (context != global_namespace)
10028 if (TREE_CODE (context) == NAMESPACE_DECL)
10029 pop_nested_namespace (context);
10030 else
10031 pop_nested_class ();
10034 return friend_type;
10037 /* Returns zero if TYPE cannot be completed later due to circularity.
10038 Otherwise returns one. */
10040 static int
10041 can_complete_type_without_circularity (tree type)
10043 if (type == NULL_TREE || type == error_mark_node)
10044 return 0;
10045 else if (COMPLETE_TYPE_P (type))
10046 return 1;
10047 else if (TREE_CODE (type) == ARRAY_TYPE)
10048 return can_complete_type_without_circularity (TREE_TYPE (type));
10049 else if (CLASS_TYPE_P (type)
10050 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
10051 return 0;
10052 else
10053 return 1;
10056 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
10057 tsubst_flags_t, tree);
10059 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10060 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10062 static tree
10063 tsubst_attribute (tree t, tree *decl_p, tree args,
10064 tsubst_flags_t complain, tree in_decl)
10066 gcc_assert (ATTR_IS_DEPENDENT (t));
10068 tree val = TREE_VALUE (t);
10069 if (val == NULL_TREE)
10070 /* Nothing to do. */;
10071 else if ((flag_openmp || flag_openmp_simd || flag_cilkplus)
10072 && is_attribute_p ("omp declare simd",
10073 get_attribute_name (t)))
10075 tree clauses = TREE_VALUE (val);
10076 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
10077 complain, in_decl);
10078 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
10079 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
10080 tree parms = DECL_ARGUMENTS (*decl_p);
10081 clauses
10082 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
10083 if (clauses)
10084 val = build_tree_list (NULL_TREE, clauses);
10085 else
10086 val = NULL_TREE;
10088 /* If the first attribute argument is an identifier, don't
10089 pass it through tsubst. Attributes like mode, format,
10090 cleanup and several target specific attributes expect it
10091 unmodified. */
10092 else if (attribute_takes_identifier_p (get_attribute_name (t)))
10094 tree chain
10095 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
10096 /*integral_constant_expression_p=*/false);
10097 if (chain != TREE_CHAIN (val))
10098 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
10100 else if (PACK_EXPANSION_P (val))
10102 /* An attribute pack expansion. */
10103 tree purp = TREE_PURPOSE (t);
10104 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
10105 if (pack == error_mark_node)
10106 return error_mark_node;
10107 int len = TREE_VEC_LENGTH (pack);
10108 tree list = NULL_TREE;
10109 tree *q = &list;
10110 for (int i = 0; i < len; ++i)
10112 tree elt = TREE_VEC_ELT (pack, i);
10113 *q = build_tree_list (purp, elt);
10114 q = &TREE_CHAIN (*q);
10116 return list;
10118 else
10119 val = tsubst_expr (val, args, complain, in_decl,
10120 /*integral_constant_expression_p=*/false);
10122 if (val != TREE_VALUE (t))
10123 return build_tree_list (TREE_PURPOSE (t), val);
10124 return t;
10127 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10128 unchanged or a new TREE_LIST chain. */
10130 static tree
10131 tsubst_attributes (tree attributes, tree args,
10132 tsubst_flags_t complain, tree in_decl)
10134 tree last_dep = NULL_TREE;
10136 for (tree t = attributes; t; t = TREE_CHAIN (t))
10137 if (ATTR_IS_DEPENDENT (t))
10139 last_dep = t;
10140 attributes = copy_list (attributes);
10141 break;
10144 if (last_dep)
10145 for (tree *p = &attributes; *p; )
10147 tree t = *p;
10148 if (ATTR_IS_DEPENDENT (t))
10150 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
10151 if (subst != t)
10153 *p = subst;
10155 p = &TREE_CHAIN (*p);
10156 while (*p);
10157 *p = TREE_CHAIN (t);
10158 continue;
10161 p = &TREE_CHAIN (*p);
10164 return attributes;
10167 /* Apply any attributes which had to be deferred until instantiation
10168 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10169 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10171 static void
10172 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
10173 tree args, tsubst_flags_t complain, tree in_decl)
10175 tree last_dep = NULL_TREE;
10176 tree t;
10177 tree *p;
10179 if (attributes == NULL_TREE)
10180 return;
10182 if (DECL_P (*decl_p))
10184 if (TREE_TYPE (*decl_p) == error_mark_node)
10185 return;
10186 p = &DECL_ATTRIBUTES (*decl_p);
10187 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10188 to our attributes parameter. */
10189 gcc_assert (*p == attributes);
10191 else
10193 p = &TYPE_ATTRIBUTES (*decl_p);
10194 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10195 lookup_template_class_1, and should be preserved. */
10196 gcc_assert (*p != attributes);
10197 while (*p)
10198 p = &TREE_CHAIN (*p);
10201 for (t = attributes; t; t = TREE_CHAIN (t))
10202 if (ATTR_IS_DEPENDENT (t))
10204 last_dep = t;
10205 attributes = copy_list (attributes);
10206 break;
10209 *p = attributes;
10210 if (last_dep)
10212 tree late_attrs = NULL_TREE;
10213 tree *q = &late_attrs;
10215 for (; *p; )
10217 t = *p;
10218 if (ATTR_IS_DEPENDENT (t))
10220 *p = TREE_CHAIN (t);
10221 TREE_CHAIN (t) = NULL_TREE;
10222 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
10224 q = &TREE_CHAIN (*q);
10225 while (*q);
10227 else
10228 p = &TREE_CHAIN (t);
10231 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
10235 /* Perform (or defer) access check for typedefs that were referenced
10236 from within the template TMPL code.
10237 This is a subroutine of instantiate_decl and instantiate_class_template.
10238 TMPL is the template to consider and TARGS is the list of arguments of
10239 that template. */
10241 static void
10242 perform_typedefs_access_check (tree tmpl, tree targs)
10244 location_t saved_location;
10245 unsigned i;
10246 qualified_typedef_usage_t *iter;
10248 if (!tmpl
10249 || (!CLASS_TYPE_P (tmpl)
10250 && TREE_CODE (tmpl) != FUNCTION_DECL))
10251 return;
10253 saved_location = input_location;
10254 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
10256 tree type_decl = iter->typedef_decl;
10257 tree type_scope = iter->context;
10259 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
10260 continue;
10262 if (uses_template_parms (type_decl))
10263 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
10264 if (uses_template_parms (type_scope))
10265 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
10267 /* Make access check error messages point to the location
10268 of the use of the typedef. */
10269 input_location = iter->locus;
10270 perform_or_defer_access_check (TYPE_BINFO (type_scope),
10271 type_decl, type_decl,
10272 tf_warning_or_error);
10274 input_location = saved_location;
10277 static tree
10278 instantiate_class_template_1 (tree type)
10280 tree templ, args, pattern, t, member;
10281 tree typedecl;
10282 tree pbinfo;
10283 tree base_list;
10284 unsigned int saved_maximum_field_alignment;
10285 tree fn_context;
10287 if (type == error_mark_node)
10288 return error_mark_node;
10290 if (COMPLETE_OR_OPEN_TYPE_P (type)
10291 || uses_template_parms (type))
10292 return type;
10294 /* Figure out which template is being instantiated. */
10295 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
10296 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
10298 /* Determine what specialization of the original template to
10299 instantiate. */
10300 t = most_specialized_partial_spec (type, tf_warning_or_error);
10301 if (t == error_mark_node)
10303 TYPE_BEING_DEFINED (type) = 1;
10304 return error_mark_node;
10306 else if (t)
10308 /* This TYPE is actually an instantiation of a partial
10309 specialization. We replace the innermost set of ARGS with
10310 the arguments appropriate for substitution. For example,
10311 given:
10313 template <class T> struct S {};
10314 template <class T> struct S<T*> {};
10316 and supposing that we are instantiating S<int*>, ARGS will
10317 presently be {int*} -- but we need {int}. */
10318 pattern = TREE_TYPE (t);
10319 args = TREE_PURPOSE (t);
10321 else
10323 pattern = TREE_TYPE (templ);
10324 args = CLASSTYPE_TI_ARGS (type);
10327 /* If the template we're instantiating is incomplete, then clearly
10328 there's nothing we can do. */
10329 if (!COMPLETE_TYPE_P (pattern))
10330 return type;
10332 /* If we've recursively instantiated too many templates, stop. */
10333 if (! push_tinst_level (type))
10334 return type;
10336 /* Now we're really doing the instantiation. Mark the type as in
10337 the process of being defined. */
10338 TYPE_BEING_DEFINED (type) = 1;
10340 /* We may be in the middle of deferred access check. Disable
10341 it now. */
10342 push_deferring_access_checks (dk_no_deferred);
10344 int saved_unevaluated_operand = cp_unevaluated_operand;
10345 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10347 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
10348 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10349 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
10350 fn_context = error_mark_node;
10351 if (!fn_context)
10352 push_to_top_level ();
10353 else
10355 cp_unevaluated_operand = 0;
10356 c_inhibit_evaluation_warnings = 0;
10358 /* Use #pragma pack from the template context. */
10359 saved_maximum_field_alignment = maximum_field_alignment;
10360 maximum_field_alignment = TYPE_PRECISION (pattern);
10362 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
10364 /* Set the input location to the most specialized template definition.
10365 This is needed if tsubsting causes an error. */
10366 typedecl = TYPE_MAIN_DECL (pattern);
10367 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
10368 DECL_SOURCE_LOCATION (typedecl);
10370 TYPE_PACKED (type) = TYPE_PACKED (pattern);
10371 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
10372 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
10373 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
10374 if (ANON_AGGR_TYPE_P (pattern))
10375 SET_ANON_AGGR_TYPE_P (type);
10376 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
10378 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
10379 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
10380 /* Adjust visibility for template arguments. */
10381 determine_visibility (TYPE_MAIN_DECL (type));
10383 if (CLASS_TYPE_P (type))
10384 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
10386 pbinfo = TYPE_BINFO (pattern);
10388 /* We should never instantiate a nested class before its enclosing
10389 class; we need to look up the nested class by name before we can
10390 instantiate it, and that lookup should instantiate the enclosing
10391 class. */
10392 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
10393 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
10395 base_list = NULL_TREE;
10396 if (BINFO_N_BASE_BINFOS (pbinfo))
10398 tree pbase_binfo;
10399 tree pushed_scope;
10400 int i;
10402 /* We must enter the scope containing the type, as that is where
10403 the accessibility of types named in dependent bases are
10404 looked up from. */
10405 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
10407 /* Substitute into each of the bases to determine the actual
10408 basetypes. */
10409 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
10411 tree base;
10412 tree access = BINFO_BASE_ACCESS (pbinfo, i);
10413 tree expanded_bases = NULL_TREE;
10414 int idx, len = 1;
10416 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
10418 expanded_bases =
10419 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
10420 args, tf_error, NULL_TREE);
10421 if (expanded_bases == error_mark_node)
10422 continue;
10424 len = TREE_VEC_LENGTH (expanded_bases);
10427 for (idx = 0; idx < len; idx++)
10429 if (expanded_bases)
10430 /* Extract the already-expanded base class. */
10431 base = TREE_VEC_ELT (expanded_bases, idx);
10432 else
10433 /* Substitute to figure out the base class. */
10434 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
10435 NULL_TREE);
10437 if (base == error_mark_node)
10438 continue;
10440 base_list = tree_cons (access, base, base_list);
10441 if (BINFO_VIRTUAL_P (pbase_binfo))
10442 TREE_TYPE (base_list) = integer_type_node;
10446 /* The list is now in reverse order; correct that. */
10447 base_list = nreverse (base_list);
10449 if (pushed_scope)
10450 pop_scope (pushed_scope);
10452 /* Now call xref_basetypes to set up all the base-class
10453 information. */
10454 xref_basetypes (type, base_list);
10456 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
10457 (int) ATTR_FLAG_TYPE_IN_PLACE,
10458 args, tf_error, NULL_TREE);
10459 fixup_attribute_variants (type);
10461 /* Now that our base classes are set up, enter the scope of the
10462 class, so that name lookups into base classes, etc. will work
10463 correctly. This is precisely analogous to what we do in
10464 begin_class_definition when defining an ordinary non-template
10465 class, except we also need to push the enclosing classes. */
10466 push_nested_class (type);
10468 /* Now members are processed in the order of declaration. */
10469 for (member = CLASSTYPE_DECL_LIST (pattern);
10470 member; member = TREE_CHAIN (member))
10472 tree t = TREE_VALUE (member);
10474 if (TREE_PURPOSE (member))
10476 if (TYPE_P (t))
10478 /* Build new CLASSTYPE_NESTED_UTDS. */
10480 tree newtag;
10481 bool class_template_p;
10483 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
10484 && TYPE_LANG_SPECIFIC (t)
10485 && CLASSTYPE_IS_TEMPLATE (t));
10486 /* If the member is a class template, then -- even after
10487 substitution -- there may be dependent types in the
10488 template argument list for the class. We increment
10489 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10490 that function will assume that no types are dependent
10491 when outside of a template. */
10492 if (class_template_p)
10493 ++processing_template_decl;
10494 newtag = tsubst (t, args, tf_error, NULL_TREE);
10495 if (class_template_p)
10496 --processing_template_decl;
10497 if (newtag == error_mark_node)
10498 continue;
10500 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
10502 tree name = TYPE_IDENTIFIER (t);
10504 if (class_template_p)
10505 /* Unfortunately, lookup_template_class sets
10506 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10507 instantiation (i.e., for the type of a member
10508 template class nested within a template class.)
10509 This behavior is required for
10510 maybe_process_partial_specialization to work
10511 correctly, but is not accurate in this case;
10512 the TAG is not an instantiation of anything.
10513 (The corresponding TEMPLATE_DECL is an
10514 instantiation, but the TYPE is not.) */
10515 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
10517 /* Now, we call pushtag to put this NEWTAG into the scope of
10518 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10519 pushtag calling push_template_decl. We don't have to do
10520 this for enums because it will already have been done in
10521 tsubst_enum. */
10522 if (name)
10523 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
10524 pushtag (name, newtag, /*tag_scope=*/ts_current);
10527 else if (DECL_DECLARES_FUNCTION_P (t))
10529 /* Build new TYPE_METHODS. */
10530 tree r;
10532 if (TREE_CODE (t) == TEMPLATE_DECL)
10533 ++processing_template_decl;
10534 r = tsubst (t, args, tf_error, NULL_TREE);
10535 if (TREE_CODE (t) == TEMPLATE_DECL)
10536 --processing_template_decl;
10537 set_current_access_from_decl (r);
10538 finish_member_declaration (r);
10539 /* Instantiate members marked with attribute used. */
10540 if (r != error_mark_node && DECL_PRESERVE_P (r))
10541 mark_used (r);
10542 if (TREE_CODE (r) == FUNCTION_DECL
10543 && DECL_OMP_DECLARE_REDUCTION_P (r))
10544 cp_check_omp_declare_reduction (r);
10546 else if (DECL_CLASS_TEMPLATE_P (t)
10547 && LAMBDA_TYPE_P (TREE_TYPE (t)))
10548 /* A closure type for a lambda in a default argument for a
10549 member template. Ignore it; it will be instantiated with
10550 the default argument. */;
10551 else
10553 /* Build new TYPE_FIELDS. */
10554 if (TREE_CODE (t) == STATIC_ASSERT)
10556 tree condition;
10558 ++c_inhibit_evaluation_warnings;
10559 condition =
10560 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
10561 tf_warning_or_error, NULL_TREE,
10562 /*integral_constant_expression_p=*/true);
10563 --c_inhibit_evaluation_warnings;
10565 finish_static_assert (condition,
10566 STATIC_ASSERT_MESSAGE (t),
10567 STATIC_ASSERT_SOURCE_LOCATION (t),
10568 /*member_p=*/true);
10570 else if (TREE_CODE (t) != CONST_DECL)
10572 tree r;
10573 tree vec = NULL_TREE;
10574 int len = 1;
10576 /* The file and line for this declaration, to
10577 assist in error message reporting. Since we
10578 called push_tinst_level above, we don't need to
10579 restore these. */
10580 input_location = DECL_SOURCE_LOCATION (t);
10582 if (TREE_CODE (t) == TEMPLATE_DECL)
10583 ++processing_template_decl;
10584 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
10585 if (TREE_CODE (t) == TEMPLATE_DECL)
10586 --processing_template_decl;
10588 if (TREE_CODE (r) == TREE_VEC)
10590 /* A capture pack became multiple fields. */
10591 vec = r;
10592 len = TREE_VEC_LENGTH (vec);
10595 for (int i = 0; i < len; ++i)
10597 if (vec)
10598 r = TREE_VEC_ELT (vec, i);
10599 if (VAR_P (r))
10601 /* In [temp.inst]:
10603 [t]he initialization (and any associated
10604 side-effects) of a static data member does
10605 not occur unless the static data member is
10606 itself used in a way that requires the
10607 definition of the static data member to
10608 exist.
10610 Therefore, we do not substitute into the
10611 initialized for the static data member here. */
10612 finish_static_data_member_decl
10614 /*init=*/NULL_TREE,
10615 /*init_const_expr_p=*/false,
10616 /*asmspec_tree=*/NULL_TREE,
10617 /*flags=*/0);
10618 /* Instantiate members marked with attribute used. */
10619 if (r != error_mark_node && DECL_PRESERVE_P (r))
10620 mark_used (r);
10622 else if (TREE_CODE (r) == FIELD_DECL)
10624 /* Determine whether R has a valid type and can be
10625 completed later. If R is invalid, then its type
10626 is replaced by error_mark_node. */
10627 tree rtype = TREE_TYPE (r);
10628 if (can_complete_type_without_circularity (rtype))
10629 complete_type (rtype);
10631 if (!complete_or_array_type_p (rtype))
10633 /* If R's type couldn't be completed and
10634 it isn't a flexible array member (whose
10635 type is incomplete by definition) give
10636 an error. */
10637 cxx_incomplete_type_error (r, rtype);
10638 TREE_TYPE (r) = error_mark_node;
10642 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10643 such a thing will already have been added to the field
10644 list by tsubst_enum in finish_member_declaration in the
10645 CLASSTYPE_NESTED_UTDS case above. */
10646 if (!(TREE_CODE (r) == TYPE_DECL
10647 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
10648 && DECL_ARTIFICIAL (r)))
10650 set_current_access_from_decl (r);
10651 finish_member_declaration (r);
10657 else
10659 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
10660 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10662 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10664 tree friend_type = t;
10665 bool adjust_processing_template_decl = false;
10667 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10669 /* template <class T> friend class C; */
10670 friend_type = tsubst_friend_class (friend_type, args);
10671 adjust_processing_template_decl = true;
10673 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
10675 /* template <class T> friend class C::D; */
10676 friend_type = tsubst (friend_type, args,
10677 tf_warning_or_error, NULL_TREE);
10678 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10679 friend_type = TREE_TYPE (friend_type);
10680 adjust_processing_template_decl = true;
10682 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
10683 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
10685 /* This could be either
10687 friend class T::C;
10689 when dependent_type_p is false or
10691 template <class U> friend class T::C;
10693 otherwise. */
10694 /* Bump processing_template_decl in case this is something like
10695 template <class T> friend struct A<T>::B. */
10696 ++processing_template_decl;
10697 friend_type = tsubst (friend_type, args,
10698 tf_warning_or_error, NULL_TREE);
10699 if (dependent_type_p (friend_type))
10700 adjust_processing_template_decl = true;
10701 --processing_template_decl;
10703 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
10704 && TYPE_HIDDEN_P (friend_type))
10706 /* friend class C;
10708 where C hasn't been declared yet. Let's lookup name
10709 from namespace scope directly, bypassing any name that
10710 come from dependent base class. */
10711 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
10713 /* The call to xref_tag_from_type does injection for friend
10714 classes. */
10715 push_nested_namespace (ns);
10716 friend_type =
10717 xref_tag_from_type (friend_type, NULL_TREE,
10718 /*tag_scope=*/ts_current);
10719 pop_nested_namespace (ns);
10721 else if (uses_template_parms (friend_type))
10722 /* friend class C<T>; */
10723 friend_type = tsubst (friend_type, args,
10724 tf_warning_or_error, NULL_TREE);
10725 /* Otherwise it's
10727 friend class C;
10729 where C is already declared or
10731 friend class C<int>;
10733 We don't have to do anything in these cases. */
10735 if (adjust_processing_template_decl)
10736 /* Trick make_friend_class into realizing that the friend
10737 we're adding is a template, not an ordinary class. It's
10738 important that we use make_friend_class since it will
10739 perform some error-checking and output cross-reference
10740 information. */
10741 ++processing_template_decl;
10743 if (friend_type != error_mark_node)
10744 make_friend_class (type, friend_type, /*complain=*/false);
10746 if (adjust_processing_template_decl)
10747 --processing_template_decl;
10749 else
10751 /* Build new DECL_FRIENDLIST. */
10752 tree r;
10754 /* The file and line for this declaration, to
10755 assist in error message reporting. Since we
10756 called push_tinst_level above, we don't need to
10757 restore these. */
10758 input_location = DECL_SOURCE_LOCATION (t);
10760 if (TREE_CODE (t) == TEMPLATE_DECL)
10762 ++processing_template_decl;
10763 push_deferring_access_checks (dk_no_check);
10766 r = tsubst_friend_function (t, args);
10767 add_friend (type, r, /*complain=*/false);
10768 if (TREE_CODE (t) == TEMPLATE_DECL)
10770 pop_deferring_access_checks ();
10771 --processing_template_decl;
10777 if (fn_context)
10779 /* Restore these before substituting into the lambda capture
10780 initializers. */
10781 cp_unevaluated_operand = saved_unevaluated_operand;
10782 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10785 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
10787 tree decl = lambda_function (type);
10788 if (decl)
10790 if (cxx_dialect >= cxx1z)
10791 CLASSTYPE_LITERAL_P (type) = true;
10793 if (!DECL_TEMPLATE_INFO (decl)
10794 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
10796 /* Set function_depth to avoid garbage collection. */
10797 ++function_depth;
10798 instantiate_decl (decl, /*defer_ok=*/false, false);
10799 --function_depth;
10802 /* We need to instantiate the capture list from the template
10803 after we've instantiated the closure members, but before we
10804 consider adding the conversion op. Also keep any captures
10805 that may have been added during instantiation of the op(). */
10806 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
10807 tree tmpl_cap
10808 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
10809 args, tf_warning_or_error, NULL_TREE,
10810 false, false);
10812 LAMBDA_EXPR_CAPTURE_LIST (expr)
10813 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
10815 maybe_add_lambda_conv_op (type);
10817 else
10818 gcc_assert (errorcount);
10821 /* Set the file and line number information to whatever is given for
10822 the class itself. This puts error messages involving generated
10823 implicit functions at a predictable point, and the same point
10824 that would be used for non-template classes. */
10825 input_location = DECL_SOURCE_LOCATION (typedecl);
10827 unreverse_member_declarations (type);
10828 finish_struct_1 (type);
10829 TYPE_BEING_DEFINED (type) = 0;
10831 /* We don't instantiate default arguments for member functions. 14.7.1:
10833 The implicit instantiation of a class template specialization causes
10834 the implicit instantiation of the declarations, but not of the
10835 definitions or default arguments, of the class member functions,
10836 member classes, static data members and member templates.... */
10838 /* Some typedefs referenced from within the template code need to be access
10839 checked at template instantiation time, i.e now. These types were
10840 added to the template at parsing time. Let's get those and perform
10841 the access checks then. */
10842 perform_typedefs_access_check (pattern, args);
10843 perform_deferred_access_checks (tf_warning_or_error);
10844 pop_nested_class ();
10845 maximum_field_alignment = saved_maximum_field_alignment;
10846 if (!fn_context)
10847 pop_from_top_level ();
10848 pop_deferring_access_checks ();
10849 pop_tinst_level ();
10851 /* The vtable for a template class can be emitted in any translation
10852 unit in which the class is instantiated. When there is no key
10853 method, however, finish_struct_1 will already have added TYPE to
10854 the keyed_classes list. */
10855 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
10856 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
10858 return type;
10861 /* Wrapper for instantiate_class_template_1. */
10863 tree
10864 instantiate_class_template (tree type)
10866 tree ret;
10867 timevar_push (TV_TEMPLATE_INST);
10868 ret = instantiate_class_template_1 (type);
10869 timevar_pop (TV_TEMPLATE_INST);
10870 return ret;
10873 static tree
10874 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10876 tree r;
10878 if (!t)
10879 r = t;
10880 else if (TYPE_P (t))
10881 r = tsubst (t, args, complain, in_decl);
10882 else
10884 if (!(complain & tf_warning))
10885 ++c_inhibit_evaluation_warnings;
10886 r = tsubst_expr (t, args, complain, in_decl,
10887 /*integral_constant_expression_p=*/true);
10888 if (!(complain & tf_warning))
10889 --c_inhibit_evaluation_warnings;
10891 return r;
10894 /* Given a function parameter pack TMPL_PARM and some function parameters
10895 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
10896 and set *SPEC_P to point at the next point in the list. */
10898 tree
10899 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
10901 /* Collect all of the extra "packed" parameters into an
10902 argument pack. */
10903 tree parmvec;
10904 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
10905 tree spec_parm = *spec_p;
10906 int i, len;
10908 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
10909 if (tmpl_parm
10910 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
10911 break;
10913 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
10914 parmvec = make_tree_vec (len);
10915 spec_parm = *spec_p;
10916 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
10917 TREE_VEC_ELT (parmvec, i) = spec_parm;
10919 /* Build the argument packs. */
10920 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
10921 *spec_p = spec_parm;
10923 return argpack;
10926 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
10927 NONTYPE_ARGUMENT_PACK. */
10929 static tree
10930 make_fnparm_pack (tree spec_parm)
10932 return extract_fnparm_pack (NULL_TREE, &spec_parm);
10935 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
10936 pack expansion with no extra args, 2 if it has extra args, or 0
10937 if it is not a pack expansion. */
10939 static int
10940 argument_pack_element_is_expansion_p (tree arg_pack, int i)
10942 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
10943 if (i >= TREE_VEC_LENGTH (vec))
10944 return 0;
10945 tree elt = TREE_VEC_ELT (vec, i);
10946 if (DECL_P (elt))
10947 /* A decl pack is itself an expansion. */
10948 elt = TREE_TYPE (elt);
10949 if (!PACK_EXPANSION_P (elt))
10950 return 0;
10951 if (PACK_EXPANSION_EXTRA_ARGS (elt))
10952 return 2;
10953 return 1;
10957 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
10959 static tree
10960 make_argument_pack_select (tree arg_pack, unsigned index)
10962 tree aps = make_node (ARGUMENT_PACK_SELECT);
10964 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
10965 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
10967 return aps;
10970 /* This is a subroutine of tsubst_pack_expansion.
10972 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
10973 mechanism to store the (non complete list of) arguments of the
10974 substitution and return a non substituted pack expansion, in order
10975 to wait for when we have enough arguments to really perform the
10976 substitution. */
10978 static bool
10979 use_pack_expansion_extra_args_p (tree parm_packs,
10980 int arg_pack_len,
10981 bool has_empty_arg)
10983 /* If one pack has an expansion and another pack has a normal
10984 argument or if one pack has an empty argument and an another
10985 one hasn't then tsubst_pack_expansion cannot perform the
10986 substitution and need to fall back on the
10987 PACK_EXPANSION_EXTRA mechanism. */
10988 if (parm_packs == NULL_TREE)
10989 return false;
10990 else if (has_empty_arg)
10991 return true;
10993 bool has_expansion_arg = false;
10994 for (int i = 0 ; i < arg_pack_len; ++i)
10996 bool has_non_expansion_arg = false;
10997 for (tree parm_pack = parm_packs;
10998 parm_pack;
10999 parm_pack = TREE_CHAIN (parm_pack))
11001 tree arg = TREE_VALUE (parm_pack);
11003 int exp = argument_pack_element_is_expansion_p (arg, i);
11004 if (exp == 2)
11005 /* We can't substitute a pack expansion with extra args into
11006 our pattern. */
11007 return true;
11008 else if (exp)
11009 has_expansion_arg = true;
11010 else
11011 has_non_expansion_arg = true;
11014 if (has_expansion_arg && has_non_expansion_arg)
11015 return true;
11017 return false;
11020 /* [temp.variadic]/6 says that:
11022 The instantiation of a pack expansion [...]
11023 produces a list E1,E2, ..., En, where N is the number of elements
11024 in the pack expansion parameters.
11026 This subroutine of tsubst_pack_expansion produces one of these Ei.
11028 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11029 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11030 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11031 INDEX is the index 'i' of the element Ei to produce. ARGS,
11032 COMPLAIN, and IN_DECL are the same parameters as for the
11033 tsubst_pack_expansion function.
11035 The function returns the resulting Ei upon successful completion,
11036 or error_mark_node.
11038 Note that this function possibly modifies the ARGS parameter, so
11039 it's the responsibility of the caller to restore it. */
11041 static tree
11042 gen_elem_of_pack_expansion_instantiation (tree pattern,
11043 tree parm_packs,
11044 unsigned index,
11045 tree args /* This parm gets
11046 modified. */,
11047 tsubst_flags_t complain,
11048 tree in_decl)
11050 tree t;
11051 bool ith_elem_is_expansion = false;
11053 /* For each parameter pack, change the substitution of the parameter
11054 pack to the ith argument in its argument pack, then expand the
11055 pattern. */
11056 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
11058 tree parm = TREE_PURPOSE (pack);
11059 tree arg_pack = TREE_VALUE (pack);
11060 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
11062 ith_elem_is_expansion |=
11063 argument_pack_element_is_expansion_p (arg_pack, index);
11065 /* Select the Ith argument from the pack. */
11066 if (TREE_CODE (parm) == PARM_DECL
11067 || TREE_CODE (parm) == FIELD_DECL)
11069 if (index == 0)
11071 aps = make_argument_pack_select (arg_pack, index);
11072 if (!mark_used (parm, complain) && !(complain & tf_error))
11073 return error_mark_node;
11074 register_local_specialization (aps, parm);
11076 else
11077 aps = retrieve_local_specialization (parm);
11079 else
11081 int idx, level;
11082 template_parm_level_and_index (parm, &level, &idx);
11084 if (index == 0)
11086 aps = make_argument_pack_select (arg_pack, index);
11087 /* Update the corresponding argument. */
11088 TMPL_ARG (args, level, idx) = aps;
11090 else
11091 /* Re-use the ARGUMENT_PACK_SELECT. */
11092 aps = TMPL_ARG (args, level, idx);
11094 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11097 /* Substitute into the PATTERN with the (possibly altered)
11098 arguments. */
11099 if (pattern == in_decl)
11100 /* Expanding a fixed parameter pack from
11101 coerce_template_parameter_pack. */
11102 t = tsubst_decl (pattern, args, complain);
11103 else if (pattern == error_mark_node)
11104 t = error_mark_node;
11105 else if (constraint_p (pattern))
11107 if (processing_template_decl)
11108 t = tsubst_constraint (pattern, args, complain, in_decl);
11109 else
11110 t = (constraints_satisfied_p (pattern, args)
11111 ? boolean_true_node : boolean_false_node);
11113 else if (!TYPE_P (pattern))
11114 t = tsubst_expr (pattern, args, complain, in_decl,
11115 /*integral_constant_expression_p=*/false);
11116 else
11117 t = tsubst (pattern, args, complain, in_decl);
11119 /* If the Ith argument pack element is a pack expansion, then
11120 the Ith element resulting from the substituting is going to
11121 be a pack expansion as well. */
11122 if (ith_elem_is_expansion)
11123 t = make_pack_expansion (t);
11125 return t;
11128 /* When the unexpanded parameter pack in a fold expression expands to an empty
11129 sequence, the value of the expression is as follows; the program is
11130 ill-formed if the operator is not listed in this table.
11132 && true
11133 || false
11134 , void() */
11136 tree
11137 expand_empty_fold (tree t, tsubst_flags_t complain)
11139 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
11140 if (!FOLD_EXPR_MODIFY_P (t))
11141 switch (code)
11143 case TRUTH_ANDIF_EXPR:
11144 return boolean_true_node;
11145 case TRUTH_ORIF_EXPR:
11146 return boolean_false_node;
11147 case COMPOUND_EXPR:
11148 return void_node;
11149 default:
11150 break;
11153 if (complain & tf_error)
11154 error_at (location_of (t),
11155 "fold of empty expansion over %O", code);
11156 return error_mark_node;
11159 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11160 form an expression that combines the two terms using the
11161 operator of T. */
11163 static tree
11164 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
11166 tree op = FOLD_EXPR_OP (t);
11167 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
11169 // Handle compound assignment operators.
11170 if (FOLD_EXPR_MODIFY_P (t))
11171 return build_x_modify_expr (input_location, left, code, right, complain);
11173 switch (code)
11175 case COMPOUND_EXPR:
11176 return build_x_compound_expr (input_location, left, right, complain);
11177 case DOTSTAR_EXPR:
11178 return build_m_component_ref (left, right, complain);
11179 default:
11180 return build_x_binary_op (input_location, code,
11181 left, TREE_CODE (left),
11182 right, TREE_CODE (right),
11183 /*overload=*/NULL,
11184 complain);
11188 /* Substitute ARGS into the pack of a fold expression T. */
11190 static inline tree
11191 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11193 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
11196 /* Substitute ARGS into the pack of a fold expression T. */
11198 static inline tree
11199 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11201 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
11204 /* Expand a PACK of arguments into a grouped as left fold.
11205 Given a pack containing elements A0, A1, ..., An and an
11206 operator @, this builds the expression:
11208 ((A0 @ A1) @ A2) ... @ An
11210 Note that PACK must not be empty.
11212 The operator is defined by the original fold expression T. */
11214 static tree
11215 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
11217 tree left = TREE_VEC_ELT (pack, 0);
11218 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
11220 tree right = TREE_VEC_ELT (pack, i);
11221 left = fold_expression (t, left, right, complain);
11223 return left;
11226 /* Substitute into a unary left fold expression. */
11228 static tree
11229 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
11230 tree in_decl)
11232 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11233 if (pack == error_mark_node)
11234 return error_mark_node;
11235 if (PACK_EXPANSION_P (pack))
11237 tree r = copy_node (t);
11238 FOLD_EXPR_PACK (r) = pack;
11239 return r;
11241 if (TREE_VEC_LENGTH (pack) == 0)
11242 return expand_empty_fold (t, complain);
11243 else
11244 return expand_left_fold (t, pack, complain);
11247 /* Substitute into a binary left fold expression.
11249 Do ths by building a single (non-empty) vector of argumnts and
11250 building the expression from those elements. */
11252 static tree
11253 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
11254 tree in_decl)
11256 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11257 if (pack == error_mark_node)
11258 return error_mark_node;
11259 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11260 if (init == error_mark_node)
11261 return error_mark_node;
11263 if (PACK_EXPANSION_P (pack))
11265 tree r = copy_node (t);
11266 FOLD_EXPR_PACK (r) = pack;
11267 FOLD_EXPR_INIT (r) = init;
11268 return r;
11271 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
11272 TREE_VEC_ELT (vec, 0) = init;
11273 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
11274 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
11276 return expand_left_fold (t, vec, complain);
11279 /* Expand a PACK of arguments into a grouped as right fold.
11280 Given a pack containing elementns A0, A1, ..., and an
11281 operator @, this builds the expression:
11283 A0@ ... (An-2 @ (An-1 @ An))
11285 Note that PACK must not be empty.
11287 The operator is defined by the original fold expression T. */
11289 tree
11290 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
11292 // Build the expression.
11293 int n = TREE_VEC_LENGTH (pack);
11294 tree right = TREE_VEC_ELT (pack, n - 1);
11295 for (--n; n != 0; --n)
11297 tree left = TREE_VEC_ELT (pack, n - 1);
11298 right = fold_expression (t, left, right, complain);
11300 return right;
11303 /* Substitute into a unary right fold expression. */
11305 static tree
11306 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
11307 tree in_decl)
11309 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11310 if (pack == error_mark_node)
11311 return error_mark_node;
11312 if (PACK_EXPANSION_P (pack))
11314 tree r = copy_node (t);
11315 FOLD_EXPR_PACK (r) = pack;
11316 return r;
11318 if (TREE_VEC_LENGTH (pack) == 0)
11319 return expand_empty_fold (t, complain);
11320 else
11321 return expand_right_fold (t, pack, complain);
11324 /* Substitute into a binary right fold expression.
11326 Do ths by building a single (non-empty) vector of arguments and
11327 building the expression from those elements. */
11329 static tree
11330 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
11331 tree in_decl)
11333 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11334 if (pack == error_mark_node)
11335 return error_mark_node;
11336 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11337 if (init == error_mark_node)
11338 return error_mark_node;
11340 if (PACK_EXPANSION_P (pack))
11342 tree r = copy_node (t);
11343 FOLD_EXPR_PACK (r) = pack;
11344 FOLD_EXPR_INIT (r) = init;
11345 return r;
11348 int n = TREE_VEC_LENGTH (pack);
11349 tree vec = make_tree_vec (n + 1);
11350 for (int i = 0; i < n; ++i)
11351 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
11352 TREE_VEC_ELT (vec, n) = init;
11354 return expand_right_fold (t, vec, complain);
11358 /* Substitute ARGS into T, which is an pack expansion
11359 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11360 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11361 (if only a partial substitution could be performed) or
11362 ERROR_MARK_NODE if there was an error. */
11363 tree
11364 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
11365 tree in_decl)
11367 tree pattern;
11368 tree pack, packs = NULL_TREE;
11369 bool unsubstituted_packs = false;
11370 int i, len = -1;
11371 tree result;
11372 hash_map<tree, tree> *saved_local_specializations = NULL;
11373 bool need_local_specializations = false;
11374 int levels;
11376 gcc_assert (PACK_EXPANSION_P (t));
11377 pattern = PACK_EXPANSION_PATTERN (t);
11379 /* Add in any args remembered from an earlier partial instantiation. */
11380 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
11382 levels = TMPL_ARGS_DEPTH (args);
11384 /* Determine the argument packs that will instantiate the parameter
11385 packs used in the expansion expression. While we're at it,
11386 compute the number of arguments to be expanded and make sure it
11387 is consistent. */
11388 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
11389 pack = TREE_CHAIN (pack))
11391 tree parm_pack = TREE_VALUE (pack);
11392 tree arg_pack = NULL_TREE;
11393 tree orig_arg = NULL_TREE;
11394 int level = 0;
11396 if (TREE_CODE (parm_pack) == BASES)
11398 gcc_assert (parm_pack == pattern);
11399 if (BASES_DIRECT (parm_pack))
11400 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
11401 args, complain, in_decl, false));
11402 else
11403 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
11404 args, complain, in_decl, false));
11406 else if (builtin_pack_call_p (parm_pack))
11408 /* ??? Support use in other patterns. */
11409 gcc_assert (parm_pack == pattern);
11410 return expand_builtin_pack_call (parm_pack, args,
11411 complain, in_decl);
11413 else if (TREE_CODE (parm_pack) == PARM_DECL)
11415 /* We know we have correct local_specializations if this
11416 expansion is at function scope, or if we're dealing with a
11417 local parameter in a requires expression; for the latter,
11418 tsubst_requires_expr set it up appropriately. */
11419 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
11420 arg_pack = retrieve_local_specialization (parm_pack);
11421 else
11422 /* We can't rely on local_specializations for a parameter
11423 name used later in a function declaration (such as in a
11424 late-specified return type). Even if it exists, it might
11425 have the wrong value for a recursive call. */
11426 need_local_specializations = true;
11428 if (!arg_pack)
11430 /* This parameter pack was used in an unevaluated context. Just
11431 make a dummy decl, since it's only used for its type. */
11432 arg_pack = tsubst_decl (parm_pack, args, complain);
11433 if (arg_pack && DECL_PACK_P (arg_pack))
11434 /* Partial instantiation of the parm_pack, we can't build
11435 up an argument pack yet. */
11436 arg_pack = NULL_TREE;
11437 else
11438 arg_pack = make_fnparm_pack (arg_pack);
11441 else if (TREE_CODE (parm_pack) == FIELD_DECL)
11442 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
11443 else
11445 int idx;
11446 template_parm_level_and_index (parm_pack, &level, &idx);
11448 if (level <= levels)
11449 arg_pack = TMPL_ARG (args, level, idx);
11452 orig_arg = arg_pack;
11453 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
11454 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
11456 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
11457 /* This can only happen if we forget to expand an argument
11458 pack somewhere else. Just return an error, silently. */
11460 result = make_tree_vec (1);
11461 TREE_VEC_ELT (result, 0) = error_mark_node;
11462 return result;
11465 if (arg_pack)
11467 int my_len =
11468 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
11470 /* Don't bother trying to do a partial substitution with
11471 incomplete packs; we'll try again after deduction. */
11472 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
11473 return t;
11475 if (len < 0)
11476 len = my_len;
11477 else if (len != my_len)
11479 if (!(complain & tf_error))
11480 /* Fail quietly. */;
11481 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
11482 error ("mismatched argument pack lengths while expanding %qT",
11483 pattern);
11484 else
11485 error ("mismatched argument pack lengths while expanding %qE",
11486 pattern);
11487 return error_mark_node;
11490 /* Keep track of the parameter packs and their corresponding
11491 argument packs. */
11492 packs = tree_cons (parm_pack, arg_pack, packs);
11493 TREE_TYPE (packs) = orig_arg;
11495 else
11497 /* We can't substitute for this parameter pack. We use a flag as
11498 well as the missing_level counter because function parameter
11499 packs don't have a level. */
11500 gcc_assert (processing_template_decl);
11501 unsubstituted_packs = true;
11505 /* If the expansion is just T..., return the matching argument pack, unless
11506 we need to call convert_from_reference on all the elements. This is an
11507 important optimization; see c++/68422. */
11508 if (!unsubstituted_packs
11509 && TREE_PURPOSE (packs) == pattern)
11511 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
11512 /* Types need no adjustment, nor does sizeof..., and if we still have
11513 some pack expansion args we won't do anything yet. */
11514 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
11515 || PACK_EXPANSION_SIZEOF_P (t)
11516 || pack_expansion_args_count (args))
11517 return args;
11518 /* Also optimize expression pack expansions if we can tell that the
11519 elements won't have reference type. */
11520 tree type = TREE_TYPE (pattern);
11521 if (type && TREE_CODE (type) != REFERENCE_TYPE
11522 && !PACK_EXPANSION_P (type)
11523 && !WILDCARD_TYPE_P (type))
11524 return args;
11525 /* Otherwise use the normal path so we get convert_from_reference. */
11528 /* We cannot expand this expansion expression, because we don't have
11529 all of the argument packs we need. */
11530 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
11532 /* We got some full packs, but we can't substitute them in until we
11533 have values for all the packs. So remember these until then. */
11535 t = make_pack_expansion (pattern);
11536 PACK_EXPANSION_EXTRA_ARGS (t) = args;
11537 return t;
11539 else if (unsubstituted_packs)
11541 /* There were no real arguments, we're just replacing a parameter
11542 pack with another version of itself. Substitute into the
11543 pattern and return a PACK_EXPANSION_*. The caller will need to
11544 deal with that. */
11545 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
11546 t = tsubst_expr (pattern, args, complain, in_decl,
11547 /*integral_constant_expression_p=*/false);
11548 else
11549 t = tsubst (pattern, args, complain, in_decl);
11550 t = make_pack_expansion (t);
11551 return t;
11554 gcc_assert (len >= 0);
11556 if (need_local_specializations)
11558 /* We're in a late-specified return type, so create our own local
11559 specializations map; the current map is either NULL or (in the
11560 case of recursive unification) might have bindings that we don't
11561 want to use or alter. */
11562 saved_local_specializations = local_specializations;
11563 local_specializations = new hash_map<tree, tree>;
11566 /* For each argument in each argument pack, substitute into the
11567 pattern. */
11568 result = make_tree_vec (len);
11569 tree elem_args = copy_template_args (args);
11570 for (i = 0; i < len; ++i)
11572 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
11574 elem_args, complain,
11575 in_decl);
11576 TREE_VEC_ELT (result, i) = t;
11577 if (t == error_mark_node)
11579 result = error_mark_node;
11580 break;
11584 /* Update ARGS to restore the substitution from parameter packs to
11585 their argument packs. */
11586 for (pack = packs; pack; pack = TREE_CHAIN (pack))
11588 tree parm = TREE_PURPOSE (pack);
11590 if (TREE_CODE (parm) == PARM_DECL
11591 || TREE_CODE (parm) == FIELD_DECL)
11592 register_local_specialization (TREE_TYPE (pack), parm);
11593 else
11595 int idx, level;
11597 if (TREE_VALUE (pack) == NULL_TREE)
11598 continue;
11600 template_parm_level_and_index (parm, &level, &idx);
11602 /* Update the corresponding argument. */
11603 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
11604 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
11605 TREE_TYPE (pack);
11606 else
11607 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
11611 if (need_local_specializations)
11613 delete local_specializations;
11614 local_specializations = saved_local_specializations;
11617 /* If the dependent pack arguments were such that we end up with only a
11618 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11619 if (len == 1 && TREE_CODE (result) == TREE_VEC
11620 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
11621 return TREE_VEC_ELT (result, 0);
11623 return result;
11626 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11627 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11628 parameter packs; all parms generated from a function parameter pack will
11629 have the same DECL_PARM_INDEX. */
11631 tree
11632 get_pattern_parm (tree parm, tree tmpl)
11634 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
11635 tree patparm;
11637 if (DECL_ARTIFICIAL (parm))
11639 for (patparm = DECL_ARGUMENTS (pattern);
11640 patparm; patparm = DECL_CHAIN (patparm))
11641 if (DECL_ARTIFICIAL (patparm)
11642 && DECL_NAME (parm) == DECL_NAME (patparm))
11643 break;
11645 else
11647 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
11648 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
11649 gcc_assert (DECL_PARM_INDEX (patparm)
11650 == DECL_PARM_INDEX (parm));
11653 return patparm;
11656 /* Make an argument pack out of the TREE_VEC VEC. */
11658 static tree
11659 make_argument_pack (tree vec)
11661 tree pack;
11662 tree elt = TREE_VEC_ELT (vec, 0);
11663 if (TYPE_P (elt))
11664 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
11665 else
11667 pack = make_node (NONTYPE_ARGUMENT_PACK);
11668 TREE_CONSTANT (pack) = 1;
11670 SET_ARGUMENT_PACK_ARGS (pack, vec);
11671 return pack;
11674 /* Return an exact copy of template args T that can be modified
11675 independently. */
11677 static tree
11678 copy_template_args (tree t)
11680 if (t == error_mark_node)
11681 return t;
11683 int len = TREE_VEC_LENGTH (t);
11684 tree new_vec = make_tree_vec (len);
11686 for (int i = 0; i < len; ++i)
11688 tree elt = TREE_VEC_ELT (t, i);
11689 if (elt && TREE_CODE (elt) == TREE_VEC)
11690 elt = copy_template_args (elt);
11691 TREE_VEC_ELT (new_vec, i) = elt;
11694 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
11695 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
11697 return new_vec;
11700 /* Substitute ARGS into the vector or list of template arguments T. */
11702 static tree
11703 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11705 tree orig_t = t;
11706 int len, need_new = 0, i, expanded_len_adjust = 0, out;
11707 tree *elts;
11709 if (t == error_mark_node)
11710 return error_mark_node;
11712 len = TREE_VEC_LENGTH (t);
11713 elts = XALLOCAVEC (tree, len);
11715 for (i = 0; i < len; i++)
11717 tree orig_arg = TREE_VEC_ELT (t, i);
11718 tree new_arg;
11720 if (TREE_CODE (orig_arg) == TREE_VEC)
11721 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
11722 else if (PACK_EXPANSION_P (orig_arg))
11724 /* Substitute into an expansion expression. */
11725 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
11727 if (TREE_CODE (new_arg) == TREE_VEC)
11728 /* Add to the expanded length adjustment the number of
11729 expanded arguments. We subtract one from this
11730 measurement, because the argument pack expression
11731 itself is already counted as 1 in
11732 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
11733 the argument pack is empty. */
11734 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
11736 else if (ARGUMENT_PACK_P (orig_arg))
11738 /* Substitute into each of the arguments. */
11739 new_arg = TYPE_P (orig_arg)
11740 ? cxx_make_type (TREE_CODE (orig_arg))
11741 : make_node (TREE_CODE (orig_arg));
11743 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
11744 args, complain, in_decl);
11745 if (pack_args == error_mark_node)
11746 new_arg = error_mark_node;
11747 else
11748 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
11750 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
11751 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
11753 else
11754 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
11756 if (new_arg == error_mark_node)
11757 return error_mark_node;
11759 elts[i] = new_arg;
11760 if (new_arg != orig_arg)
11761 need_new = 1;
11764 if (!need_new)
11765 return t;
11767 /* Make space for the expanded arguments coming from template
11768 argument packs. */
11769 t = make_tree_vec (len + expanded_len_adjust);
11770 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
11771 arguments for a member template.
11772 In that case each TREE_VEC in ORIG_T represents a level of template
11773 arguments, and ORIG_T won't carry any non defaulted argument count.
11774 It will rather be the nested TREE_VECs that will carry one.
11775 In other words, ORIG_T carries a non defaulted argument count only
11776 if it doesn't contain any nested TREE_VEC. */
11777 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
11779 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
11780 count += expanded_len_adjust;
11781 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
11783 for (i = 0, out = 0; i < len; i++)
11785 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
11786 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
11787 && TREE_CODE (elts[i]) == TREE_VEC)
11789 int idx;
11791 /* Now expand the template argument pack "in place". */
11792 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
11793 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
11795 else
11797 TREE_VEC_ELT (t, out) = elts[i];
11798 out++;
11802 return t;
11805 /* Substitute ARGS into one level PARMS of template parameters. */
11807 static tree
11808 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
11810 if (parms == error_mark_node)
11811 return error_mark_node;
11813 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
11815 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
11817 tree tuple = TREE_VEC_ELT (parms, i);
11819 if (tuple == error_mark_node)
11820 continue;
11822 TREE_VEC_ELT (new_vec, i) =
11823 tsubst_template_parm (tuple, args, complain);
11826 return new_vec;
11829 /* Return the result of substituting ARGS into the template parameters
11830 given by PARMS. If there are m levels of ARGS and m + n levels of
11831 PARMS, then the result will contain n levels of PARMS. For
11832 example, if PARMS is `template <class T> template <class U>
11833 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
11834 result will be `template <int*, double, class V>'. */
11836 static tree
11837 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
11839 tree r = NULL_TREE;
11840 tree* new_parms;
11842 /* When substituting into a template, we must set
11843 PROCESSING_TEMPLATE_DECL as the template parameters may be
11844 dependent if they are based on one-another, and the dependency
11845 predicates are short-circuit outside of templates. */
11846 ++processing_template_decl;
11848 for (new_parms = &r;
11849 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
11850 new_parms = &(TREE_CHAIN (*new_parms)),
11851 parms = TREE_CHAIN (parms))
11853 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
11854 args, complain);
11855 *new_parms =
11856 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
11857 - TMPL_ARGS_DEPTH (args)),
11858 new_vec, NULL_TREE);
11861 --processing_template_decl;
11863 return r;
11866 /* Return the result of substituting ARGS into one template parameter
11867 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
11868 parameter and which TREE_PURPOSE is the default argument of the
11869 template parameter. */
11871 static tree
11872 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
11874 tree default_value, parm_decl;
11876 if (args == NULL_TREE
11877 || t == NULL_TREE
11878 || t == error_mark_node)
11879 return t;
11881 gcc_assert (TREE_CODE (t) == TREE_LIST);
11883 default_value = TREE_PURPOSE (t);
11884 parm_decl = TREE_VALUE (t);
11886 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
11887 if (TREE_CODE (parm_decl) == PARM_DECL
11888 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
11889 parm_decl = error_mark_node;
11890 default_value = tsubst_template_arg (default_value, args,
11891 complain, NULL_TREE);
11893 return build_tree_list (default_value, parm_decl);
11896 /* Substitute the ARGS into the indicated aggregate (or enumeration)
11897 type T. If T is not an aggregate or enumeration type, it is
11898 handled as if by tsubst. IN_DECL is as for tsubst. If
11899 ENTERING_SCOPE is nonzero, T is the context for a template which
11900 we are presently tsubst'ing. Return the substituted value. */
11902 static tree
11903 tsubst_aggr_type (tree t,
11904 tree args,
11905 tsubst_flags_t complain,
11906 tree in_decl,
11907 int entering_scope)
11909 if (t == NULL_TREE)
11910 return NULL_TREE;
11912 switch (TREE_CODE (t))
11914 case RECORD_TYPE:
11915 if (TYPE_PTRMEMFUNC_P (t))
11916 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
11918 /* Fall through. */
11919 case ENUMERAL_TYPE:
11920 case UNION_TYPE:
11921 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
11923 tree argvec;
11924 tree context;
11925 tree r;
11926 int saved_unevaluated_operand;
11927 int saved_inhibit_evaluation_warnings;
11929 /* In "sizeof(X<I>)" we need to evaluate "I". */
11930 saved_unevaluated_operand = cp_unevaluated_operand;
11931 cp_unevaluated_operand = 0;
11932 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11933 c_inhibit_evaluation_warnings = 0;
11935 /* First, determine the context for the type we are looking
11936 up. */
11937 context = TYPE_CONTEXT (t);
11938 if (context && TYPE_P (context))
11940 context = tsubst_aggr_type (context, args, complain,
11941 in_decl, /*entering_scope=*/1);
11942 /* If context is a nested class inside a class template,
11943 it may still need to be instantiated (c++/33959). */
11944 context = complete_type (context);
11947 /* Then, figure out what arguments are appropriate for the
11948 type we are trying to find. For example, given:
11950 template <class T> struct S;
11951 template <class T, class U> void f(T, U) { S<U> su; }
11953 and supposing that we are instantiating f<int, double>,
11954 then our ARGS will be {int, double}, but, when looking up
11955 S we only want {double}. */
11956 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
11957 complain, in_decl);
11958 if (argvec == error_mark_node)
11959 r = error_mark_node;
11960 else
11962 r = lookup_template_class (t, argvec, in_decl, context,
11963 entering_scope, complain);
11964 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
11967 cp_unevaluated_operand = saved_unevaluated_operand;
11968 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
11970 return r;
11972 else
11973 /* This is not a template type, so there's nothing to do. */
11974 return t;
11976 default:
11977 return tsubst (t, args, complain, in_decl);
11981 /* Substitute into the default argument ARG (a default argument for
11982 FN), which has the indicated TYPE. */
11984 tree
11985 tsubst_default_argument (tree fn, tree type, tree arg, tsubst_flags_t complain)
11987 tree saved_class_ptr = NULL_TREE;
11988 tree saved_class_ref = NULL_TREE;
11989 int errs = errorcount + sorrycount;
11991 /* This can happen in invalid code. */
11992 if (TREE_CODE (arg) == DEFAULT_ARG)
11993 return arg;
11995 /* This default argument came from a template. Instantiate the
11996 default argument here, not in tsubst. In the case of
11997 something like:
11999 template <class T>
12000 struct S {
12001 static T t();
12002 void f(T = t());
12005 we must be careful to do name lookup in the scope of S<T>,
12006 rather than in the current class. */
12007 push_access_scope (fn);
12008 /* The "this" pointer is not valid in a default argument. */
12009 if (cfun)
12011 saved_class_ptr = current_class_ptr;
12012 cp_function_chain->x_current_class_ptr = NULL_TREE;
12013 saved_class_ref = current_class_ref;
12014 cp_function_chain->x_current_class_ref = NULL_TREE;
12017 push_deferring_access_checks(dk_no_deferred);
12018 /* The default argument expression may cause implicitly defined
12019 member functions to be synthesized, which will result in garbage
12020 collection. We must treat this situation as if we were within
12021 the body of function so as to avoid collecting live data on the
12022 stack. */
12023 ++function_depth;
12024 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
12025 complain, NULL_TREE,
12026 /*integral_constant_expression_p=*/false);
12027 --function_depth;
12028 pop_deferring_access_checks();
12030 /* Restore the "this" pointer. */
12031 if (cfun)
12033 cp_function_chain->x_current_class_ptr = saved_class_ptr;
12034 cp_function_chain->x_current_class_ref = saved_class_ref;
12037 if (errorcount+sorrycount > errs
12038 && (complain & tf_warning_or_error))
12039 inform (input_location,
12040 " when instantiating default argument for call to %qD", fn);
12042 /* Make sure the default argument is reasonable. */
12043 arg = check_default_argument (type, arg, complain);
12045 pop_access_scope (fn);
12047 return arg;
12050 /* Substitute into all the default arguments for FN. */
12052 static void
12053 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
12055 tree arg;
12056 tree tmpl_args;
12058 tmpl_args = DECL_TI_ARGS (fn);
12060 /* If this function is not yet instantiated, we certainly don't need
12061 its default arguments. */
12062 if (uses_template_parms (tmpl_args))
12063 return;
12064 /* Don't do this again for clones. */
12065 if (DECL_CLONED_FUNCTION_P (fn))
12066 return;
12068 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
12069 arg;
12070 arg = TREE_CHAIN (arg))
12071 if (TREE_PURPOSE (arg))
12072 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
12073 TREE_VALUE (arg),
12074 TREE_PURPOSE (arg),
12075 complain);
12078 /* Substitute the ARGS into the T, which is a _DECL. Return the
12079 result of the substitution. Issue error and warning messages under
12080 control of COMPLAIN. */
12082 static tree
12083 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
12085 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12086 location_t saved_loc;
12087 tree r = NULL_TREE;
12088 tree in_decl = t;
12089 hashval_t hash = 0;
12091 /* Set the filename and linenumber to improve error-reporting. */
12092 saved_loc = input_location;
12093 input_location = DECL_SOURCE_LOCATION (t);
12095 switch (TREE_CODE (t))
12097 case TEMPLATE_DECL:
12099 /* We can get here when processing a member function template,
12100 member class template, or template template parameter. */
12101 tree decl = DECL_TEMPLATE_RESULT (t);
12102 tree spec;
12103 tree tmpl_args;
12104 tree full_args;
12106 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12108 /* Template template parameter is treated here. */
12109 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12110 if (new_type == error_mark_node)
12111 r = error_mark_node;
12112 /* If we get a real template back, return it. This can happen in
12113 the context of most_specialized_partial_spec. */
12114 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
12115 r = new_type;
12116 else
12117 /* The new TEMPLATE_DECL was built in
12118 reduce_template_parm_level. */
12119 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
12120 break;
12123 /* We might already have an instance of this template.
12124 The ARGS are for the surrounding class type, so the
12125 full args contain the tsubst'd args for the context,
12126 plus the innermost args from the template decl. */
12127 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
12128 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
12129 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
12130 /* Because this is a template, the arguments will still be
12131 dependent, even after substitution. If
12132 PROCESSING_TEMPLATE_DECL is not set, the dependency
12133 predicates will short-circuit. */
12134 ++processing_template_decl;
12135 full_args = tsubst_template_args (tmpl_args, args,
12136 complain, in_decl);
12137 --processing_template_decl;
12138 if (full_args == error_mark_node)
12139 RETURN (error_mark_node);
12141 /* If this is a default template template argument,
12142 tsubst might not have changed anything. */
12143 if (full_args == tmpl_args)
12144 RETURN (t);
12146 hash = hash_tmpl_and_args (t, full_args);
12147 spec = retrieve_specialization (t, full_args, hash);
12148 if (spec != NULL_TREE)
12150 r = spec;
12151 break;
12154 /* Make a new template decl. It will be similar to the
12155 original, but will record the current template arguments.
12156 We also create a new function declaration, which is just
12157 like the old one, but points to this new template, rather
12158 than the old one. */
12159 r = copy_decl (t);
12160 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
12161 DECL_CHAIN (r) = NULL_TREE;
12163 // Build new template info linking to the original template decl.
12164 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12166 if (TREE_CODE (decl) == TYPE_DECL
12167 && !TYPE_DECL_ALIAS_P (decl))
12169 tree new_type;
12170 ++processing_template_decl;
12171 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12172 --processing_template_decl;
12173 if (new_type == error_mark_node)
12174 RETURN (error_mark_node);
12176 TREE_TYPE (r) = new_type;
12177 /* For a partial specialization, we need to keep pointing to
12178 the primary template. */
12179 if (!DECL_TEMPLATE_SPECIALIZATION (t))
12180 CLASSTYPE_TI_TEMPLATE (new_type) = r;
12181 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
12182 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
12183 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
12185 else
12187 tree new_decl;
12188 ++processing_template_decl;
12189 new_decl = tsubst (decl, args, complain, in_decl);
12190 --processing_template_decl;
12191 if (new_decl == error_mark_node)
12192 RETURN (error_mark_node);
12194 DECL_TEMPLATE_RESULT (r) = new_decl;
12195 DECL_TI_TEMPLATE (new_decl) = r;
12196 TREE_TYPE (r) = TREE_TYPE (new_decl);
12197 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
12198 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
12201 SET_DECL_IMPLICIT_INSTANTIATION (r);
12202 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
12203 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
12205 /* The template parameters for this new template are all the
12206 template parameters for the old template, except the
12207 outermost level of parameters. */
12208 DECL_TEMPLATE_PARMS (r)
12209 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
12210 complain);
12212 if (PRIMARY_TEMPLATE_P (t))
12213 DECL_PRIMARY_TEMPLATE (r) = r;
12215 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl))
12216 /* Record this non-type partial instantiation. */
12217 register_specialization (r, t,
12218 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
12219 false, hash);
12221 break;
12223 case FUNCTION_DECL:
12225 tree ctx;
12226 tree argvec = NULL_TREE;
12227 tree *friends;
12228 tree gen_tmpl;
12229 tree type;
12230 int member;
12231 int args_depth;
12232 int parms_depth;
12234 /* Nobody should be tsubst'ing into non-template functions. */
12235 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
12237 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
12239 tree spec;
12241 /* If T is not dependent, just return it. */
12242 if (!uses_template_parms (DECL_TI_ARGS (t)))
12243 RETURN (t);
12245 /* Calculate the most general template of which R is a
12246 specialization, and the complete set of arguments used to
12247 specialize R. */
12248 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
12249 argvec = tsubst_template_args (DECL_TI_ARGS
12250 (DECL_TEMPLATE_RESULT
12251 (DECL_TI_TEMPLATE (t))),
12252 args, complain, in_decl);
12253 if (argvec == error_mark_node)
12254 RETURN (error_mark_node);
12256 /* Check to see if we already have this specialization. */
12257 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12258 spec = retrieve_specialization (gen_tmpl, argvec, hash);
12260 if (spec)
12262 r = spec;
12263 break;
12266 /* We can see more levels of arguments than parameters if
12267 there was a specialization of a member template, like
12268 this:
12270 template <class T> struct S { template <class U> void f(); }
12271 template <> template <class U> void S<int>::f(U);
12273 Here, we'll be substituting into the specialization,
12274 because that's where we can find the code we actually
12275 want to generate, but we'll have enough arguments for
12276 the most general template.
12278 We also deal with the peculiar case:
12280 template <class T> struct S {
12281 template <class U> friend void f();
12283 template <class U> void f() {}
12284 template S<int>;
12285 template void f<double>();
12287 Here, the ARGS for the instantiation of will be {int,
12288 double}. But, we only need as many ARGS as there are
12289 levels of template parameters in CODE_PATTERN. We are
12290 careful not to get fooled into reducing the ARGS in
12291 situations like:
12293 template <class T> struct S { template <class U> void f(U); }
12294 template <class T> template <> void S<T>::f(int) {}
12296 which we can spot because the pattern will be a
12297 specialization in this case. */
12298 args_depth = TMPL_ARGS_DEPTH (args);
12299 parms_depth =
12300 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
12301 if (args_depth > parms_depth
12302 && !DECL_TEMPLATE_SPECIALIZATION (t))
12303 args = get_innermost_template_args (args, parms_depth);
12305 else
12307 /* This special case arises when we have something like this:
12309 template <class T> struct S {
12310 friend void f<int>(int, double);
12313 Here, the DECL_TI_TEMPLATE for the friend declaration
12314 will be an IDENTIFIER_NODE. We are being called from
12315 tsubst_friend_function, and we want only to create a
12316 new decl (R) with appropriate types so that we can call
12317 determine_specialization. */
12318 gen_tmpl = NULL_TREE;
12321 if (DECL_CLASS_SCOPE_P (t))
12323 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
12324 member = 2;
12325 else
12326 member = 1;
12327 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
12328 complain, t, /*entering_scope=*/1);
12330 else
12332 member = 0;
12333 ctx = DECL_CONTEXT (t);
12335 type = tsubst (TREE_TYPE (t), args, complain|tf_fndecl_type, in_decl);
12336 if (type == error_mark_node)
12337 RETURN (error_mark_node);
12339 /* If we hit excessive deduction depth, the type is bogus even if
12340 it isn't error_mark_node, so don't build a decl. */
12341 if (excessive_deduction_depth)
12342 RETURN (error_mark_node);
12344 /* We do NOT check for matching decls pushed separately at this
12345 point, as they may not represent instantiations of this
12346 template, and in any case are considered separate under the
12347 discrete model. */
12348 r = copy_decl (t);
12349 DECL_USE_TEMPLATE (r) = 0;
12350 TREE_TYPE (r) = type;
12351 /* Clear out the mangled name and RTL for the instantiation. */
12352 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12353 SET_DECL_RTL (r, NULL);
12354 /* Leave DECL_INITIAL set on deleted instantiations. */
12355 if (!DECL_DELETED_FN (r))
12356 DECL_INITIAL (r) = NULL_TREE;
12357 DECL_CONTEXT (r) = ctx;
12359 /* OpenMP UDRs have the only argument a reference to the declared
12360 type. We want to diagnose if the declared type is a reference,
12361 which is invalid, but as references to references are usually
12362 quietly merged, diagnose it here. */
12363 if (DECL_OMP_DECLARE_REDUCTION_P (t))
12365 tree argtype
12366 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
12367 argtype = tsubst (argtype, args, complain, in_decl);
12368 if (TREE_CODE (argtype) == REFERENCE_TYPE)
12369 error_at (DECL_SOURCE_LOCATION (t),
12370 "reference type %qT in "
12371 "%<#pragma omp declare reduction%>", argtype);
12372 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
12373 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
12374 argtype);
12377 if (member && DECL_CONV_FN_P (r))
12378 /* Type-conversion operator. Reconstruct the name, in
12379 case it's the name of one of the template's parameters. */
12380 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
12382 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
12383 complain, t);
12384 DECL_RESULT (r) = NULL_TREE;
12386 TREE_STATIC (r) = 0;
12387 TREE_PUBLIC (r) = TREE_PUBLIC (t);
12388 DECL_EXTERNAL (r) = 1;
12389 /* If this is an instantiation of a function with internal
12390 linkage, we already know what object file linkage will be
12391 assigned to the instantiation. */
12392 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
12393 DECL_DEFER_OUTPUT (r) = 0;
12394 DECL_CHAIN (r) = NULL_TREE;
12395 DECL_PENDING_INLINE_INFO (r) = 0;
12396 DECL_PENDING_INLINE_P (r) = 0;
12397 DECL_SAVED_TREE (r) = NULL_TREE;
12398 DECL_STRUCT_FUNCTION (r) = NULL;
12399 TREE_USED (r) = 0;
12400 /* We'll re-clone as appropriate in instantiate_template. */
12401 DECL_CLONED_FUNCTION (r) = NULL_TREE;
12403 /* If we aren't complaining now, return on error before we register
12404 the specialization so that we'll complain eventually. */
12405 if ((complain & tf_error) == 0
12406 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
12407 && !grok_op_properties (r, /*complain=*/false))
12408 RETURN (error_mark_node);
12410 /* When instantiating a constrained member, substitute
12411 into the constraints to create a new constraint. */
12412 if (tree ci = get_constraints (t))
12413 if (member)
12415 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
12416 set_constraints (r, ci);
12419 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12420 this in the special friend case mentioned above where
12421 GEN_TMPL is NULL. */
12422 if (gen_tmpl)
12424 DECL_TEMPLATE_INFO (r)
12425 = build_template_info (gen_tmpl, argvec);
12426 SET_DECL_IMPLICIT_INSTANTIATION (r);
12428 tree new_r
12429 = register_specialization (r, gen_tmpl, argvec, false, hash);
12430 if (new_r != r)
12431 /* We instantiated this while substituting into
12432 the type earlier (template/friend54.C). */
12433 RETURN (new_r);
12435 /* We're not supposed to instantiate default arguments
12436 until they are called, for a template. But, for a
12437 declaration like:
12439 template <class T> void f ()
12440 { extern void g(int i = T()); }
12442 we should do the substitution when the template is
12443 instantiated. We handle the member function case in
12444 instantiate_class_template since the default arguments
12445 might refer to other members of the class. */
12446 if (!member
12447 && !PRIMARY_TEMPLATE_P (gen_tmpl)
12448 && !uses_template_parms (argvec))
12449 tsubst_default_arguments (r, complain);
12451 else
12452 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12454 /* Copy the list of befriending classes. */
12455 for (friends = &DECL_BEFRIENDING_CLASSES (r);
12456 *friends;
12457 friends = &TREE_CHAIN (*friends))
12459 *friends = copy_node (*friends);
12460 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
12461 args, complain,
12462 in_decl);
12465 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
12467 maybe_retrofit_in_chrg (r);
12468 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
12469 RETURN (error_mark_node);
12470 /* If this is an instantiation of a member template, clone it.
12471 If it isn't, that'll be handled by
12472 clone_constructors_and_destructors. */
12473 if (PRIMARY_TEMPLATE_P (gen_tmpl))
12474 clone_function_decl (r, /*update_methods=*/false);
12476 else if ((complain & tf_error) != 0
12477 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
12478 && !grok_op_properties (r, /*complain=*/true))
12479 RETURN (error_mark_node);
12481 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
12482 SET_DECL_FRIEND_CONTEXT (r,
12483 tsubst (DECL_FRIEND_CONTEXT (t),
12484 args, complain, in_decl));
12486 /* Possibly limit visibility based on template args. */
12487 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12488 if (DECL_VISIBILITY_SPECIFIED (t))
12490 DECL_VISIBILITY_SPECIFIED (r) = 0;
12491 DECL_ATTRIBUTES (r)
12492 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12494 determine_visibility (r);
12495 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
12496 && !processing_template_decl)
12497 defaulted_late_check (r);
12499 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12500 args, complain, in_decl);
12502 break;
12504 case PARM_DECL:
12506 tree type = NULL_TREE;
12507 int i, len = 1;
12508 tree expanded_types = NULL_TREE;
12509 tree prev_r = NULL_TREE;
12510 tree first_r = NULL_TREE;
12512 if (DECL_PACK_P (t))
12514 /* If there is a local specialization that isn't a
12515 parameter pack, it means that we're doing a "simple"
12516 substitution from inside tsubst_pack_expansion. Just
12517 return the local specialization (which will be a single
12518 parm). */
12519 tree spec = retrieve_local_specialization (t);
12520 if (spec
12521 && TREE_CODE (spec) == PARM_DECL
12522 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
12523 RETURN (spec);
12525 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12526 the parameters in this function parameter pack. */
12527 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12528 complain, in_decl);
12529 if (TREE_CODE (expanded_types) == TREE_VEC)
12531 len = TREE_VEC_LENGTH (expanded_types);
12533 /* Zero-length parameter packs are boring. Just substitute
12534 into the chain. */
12535 if (len == 0)
12536 RETURN (tsubst (TREE_CHAIN (t), args, complain,
12537 TREE_CHAIN (t)));
12539 else
12541 /* All we did was update the type. Make a note of that. */
12542 type = expanded_types;
12543 expanded_types = NULL_TREE;
12547 /* Loop through all of the parameters we'll build. When T is
12548 a function parameter pack, LEN is the number of expanded
12549 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12550 r = NULL_TREE;
12551 for (i = 0; i < len; ++i)
12553 prev_r = r;
12554 r = copy_node (t);
12555 if (DECL_TEMPLATE_PARM_P (t))
12556 SET_DECL_TEMPLATE_PARM_P (r);
12558 if (expanded_types)
12559 /* We're on the Ith parameter of the function parameter
12560 pack. */
12562 /* Get the Ith type. */
12563 type = TREE_VEC_ELT (expanded_types, i);
12565 /* Rename the parameter to include the index. */
12566 DECL_NAME (r)
12567 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12569 else if (!type)
12570 /* We're dealing with a normal parameter. */
12571 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12573 type = type_decays_to (type);
12574 TREE_TYPE (r) = type;
12575 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12577 if (DECL_INITIAL (r))
12579 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
12580 DECL_INITIAL (r) = TREE_TYPE (r);
12581 else
12582 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
12583 complain, in_decl);
12586 DECL_CONTEXT (r) = NULL_TREE;
12588 if (!DECL_TEMPLATE_PARM_P (r))
12589 DECL_ARG_TYPE (r) = type_passed_as (type);
12591 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12592 args, complain, in_decl);
12594 /* Keep track of the first new parameter we
12595 generate. That's what will be returned to the
12596 caller. */
12597 if (!first_r)
12598 first_r = r;
12600 /* Build a proper chain of parameters when substituting
12601 into a function parameter pack. */
12602 if (prev_r)
12603 DECL_CHAIN (prev_r) = r;
12606 /* If cp_unevaluated_operand is set, we're just looking for a
12607 single dummy parameter, so don't keep going. */
12608 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
12609 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
12610 complain, DECL_CHAIN (t));
12612 /* FIRST_R contains the start of the chain we've built. */
12613 r = first_r;
12615 break;
12617 case FIELD_DECL:
12619 tree type = NULL_TREE;
12620 tree vec = NULL_TREE;
12621 tree expanded_types = NULL_TREE;
12622 int len = 1;
12624 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12626 /* This field is a lambda capture pack. Return a TREE_VEC of
12627 the expanded fields to instantiate_class_template_1 and
12628 store them in the specializations hash table as a
12629 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
12630 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12631 complain, in_decl);
12632 if (TREE_CODE (expanded_types) == TREE_VEC)
12634 len = TREE_VEC_LENGTH (expanded_types);
12635 vec = make_tree_vec (len);
12637 else
12639 /* All we did was update the type. Make a note of that. */
12640 type = expanded_types;
12641 expanded_types = NULL_TREE;
12645 for (int i = 0; i < len; ++i)
12647 r = copy_decl (t);
12648 if (expanded_types)
12650 type = TREE_VEC_ELT (expanded_types, i);
12651 DECL_NAME (r)
12652 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12654 else if (!type)
12655 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12657 if (type == error_mark_node)
12658 RETURN (error_mark_node);
12659 TREE_TYPE (r) = type;
12660 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12662 if (DECL_C_BIT_FIELD (r))
12663 /* For bit-fields, DECL_INITIAL gives the number of bits. For
12664 non-bit-fields DECL_INITIAL is a non-static data member
12665 initializer, which gets deferred instantiation. */
12666 DECL_INITIAL (r)
12667 = tsubst_expr (DECL_INITIAL (t), args,
12668 complain, in_decl,
12669 /*integral_constant_expression_p=*/true);
12670 else if (DECL_INITIAL (t))
12672 /* Set up DECL_TEMPLATE_INFO so that we can get at the
12673 NSDMI in perform_member_init. Still set DECL_INITIAL
12674 so that we know there is one. */
12675 DECL_INITIAL (r) = void_node;
12676 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
12677 retrofit_lang_decl (r);
12678 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12680 /* We don't have to set DECL_CONTEXT here; it is set by
12681 finish_member_declaration. */
12682 DECL_CHAIN (r) = NULL_TREE;
12684 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12685 args, complain, in_decl);
12687 if (vec)
12688 TREE_VEC_ELT (vec, i) = r;
12691 if (vec)
12693 r = vec;
12694 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
12695 SET_ARGUMENT_PACK_ARGS (pack, vec);
12696 register_specialization (pack, t, args, false, 0);
12699 break;
12701 case USING_DECL:
12702 /* We reach here only for member using decls. We also need to check
12703 uses_template_parms because DECL_DEPENDENT_P is not set for a
12704 using-declaration that designates a member of the current
12705 instantiation (c++/53549). */
12706 if (DECL_DEPENDENT_P (t)
12707 || uses_template_parms (USING_DECL_SCOPE (t)))
12709 tree scope = USING_DECL_SCOPE (t);
12710 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
12711 if (PACK_EXPANSION_P (scope))
12713 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
12714 int len = TREE_VEC_LENGTH (vec);
12715 r = make_tree_vec (len);
12716 for (int i = 0; i < len; ++i)
12718 tree escope = TREE_VEC_ELT (vec, i);
12719 tree elt = do_class_using_decl (escope, name);
12720 if (!elt)
12722 r = error_mark_node;
12723 break;
12725 else
12727 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
12728 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
12730 TREE_VEC_ELT (r, i) = elt;
12733 else
12735 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
12736 complain, in_decl);
12737 r = do_class_using_decl (inst_scope, name);
12738 if (!r)
12739 r = error_mark_node;
12740 else
12742 TREE_PROTECTED (r) = TREE_PROTECTED (t);
12743 TREE_PRIVATE (r) = TREE_PRIVATE (t);
12747 else
12749 r = copy_node (t);
12750 DECL_CHAIN (r) = NULL_TREE;
12752 break;
12754 case TYPE_DECL:
12755 case VAR_DECL:
12757 tree argvec = NULL_TREE;
12758 tree gen_tmpl = NULL_TREE;
12759 tree spec;
12760 tree tmpl = NULL_TREE;
12761 tree ctx;
12762 tree type = NULL_TREE;
12763 bool local_p;
12765 if (TREE_TYPE (t) == error_mark_node)
12766 RETURN (error_mark_node);
12768 if (TREE_CODE (t) == TYPE_DECL
12769 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
12771 /* If this is the canonical decl, we don't have to
12772 mess with instantiations, and often we can't (for
12773 typename, template type parms and such). Note that
12774 TYPE_NAME is not correct for the above test if
12775 we've copied the type for a typedef. */
12776 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12777 if (type == error_mark_node)
12778 RETURN (error_mark_node);
12779 r = TYPE_NAME (type);
12780 break;
12783 /* Check to see if we already have the specialization we
12784 need. */
12785 spec = NULL_TREE;
12786 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
12788 /* T is a static data member or namespace-scope entity.
12789 We have to substitute into namespace-scope variables
12790 (not just variable templates) because of cases like:
12792 template <class T> void f() { extern T t; }
12794 where the entity referenced is not known until
12795 instantiation time. */
12796 local_p = false;
12797 ctx = DECL_CONTEXT (t);
12798 if (DECL_CLASS_SCOPE_P (t))
12800 ctx = tsubst_aggr_type (ctx, args,
12801 complain,
12802 in_decl, /*entering_scope=*/1);
12803 /* If CTX is unchanged, then T is in fact the
12804 specialization we want. That situation occurs when
12805 referencing a static data member within in its own
12806 class. We can use pointer equality, rather than
12807 same_type_p, because DECL_CONTEXT is always
12808 canonical... */
12809 if (ctx == DECL_CONTEXT (t)
12810 /* ... unless T is a member template; in which
12811 case our caller can be willing to create a
12812 specialization of that template represented
12813 by T. */
12814 && !(DECL_TI_TEMPLATE (t)
12815 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
12816 spec = t;
12819 if (!spec)
12821 tmpl = DECL_TI_TEMPLATE (t);
12822 gen_tmpl = most_general_template (tmpl);
12823 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
12824 if (argvec != error_mark_node)
12825 argvec = (coerce_innermost_template_parms
12826 (DECL_TEMPLATE_PARMS (gen_tmpl),
12827 argvec, t, complain,
12828 /*all*/true, /*defarg*/true));
12829 if (argvec == error_mark_node)
12830 RETURN (error_mark_node);
12831 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12832 spec = retrieve_specialization (gen_tmpl, argvec, hash);
12835 else
12837 /* A local variable. */
12838 local_p = true;
12839 /* Subsequent calls to pushdecl will fill this in. */
12840 ctx = NULL_TREE;
12841 /* Unless this is a reference to a static variable from an
12842 enclosing function, in which case we need to fill it in now. */
12843 if (TREE_STATIC (t))
12845 tree fn = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
12846 if (fn != current_function_decl)
12847 ctx = fn;
12849 spec = retrieve_local_specialization (t);
12851 /* If we already have the specialization we need, there is
12852 nothing more to do. */
12853 if (spec)
12855 r = spec;
12856 break;
12859 /* Create a new node for the specialization we need. */
12860 r = copy_decl (t);
12861 if (type == NULL_TREE)
12863 if (is_typedef_decl (t))
12864 type = DECL_ORIGINAL_TYPE (t);
12865 else
12866 type = TREE_TYPE (t);
12867 if (VAR_P (t)
12868 && VAR_HAD_UNKNOWN_BOUND (t)
12869 && type != error_mark_node)
12870 type = strip_array_domain (type);
12871 type = tsubst (type, args, complain, in_decl);
12873 if (VAR_P (r))
12875 /* Even if the original location is out of scope, the
12876 newly substituted one is not. */
12877 DECL_DEAD_FOR_LOCAL (r) = 0;
12878 DECL_INITIALIZED_P (r) = 0;
12879 DECL_TEMPLATE_INSTANTIATED (r) = 0;
12880 if (type == error_mark_node)
12881 RETURN (error_mark_node);
12882 if (TREE_CODE (type) == FUNCTION_TYPE)
12884 /* It may seem that this case cannot occur, since:
12886 typedef void f();
12887 void g() { f x; }
12889 declares a function, not a variable. However:
12891 typedef void f();
12892 template <typename T> void g() { T t; }
12893 template void g<f>();
12895 is an attempt to declare a variable with function
12896 type. */
12897 error ("variable %qD has function type",
12898 /* R is not yet sufficiently initialized, so we
12899 just use its name. */
12900 DECL_NAME (r));
12901 RETURN (error_mark_node);
12903 type = complete_type (type);
12904 /* Wait until cp_finish_decl to set this again, to handle
12905 circular dependency (template/instantiate6.C). */
12906 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
12907 type = check_var_type (DECL_NAME (r), type);
12909 if (DECL_HAS_VALUE_EXPR_P (t))
12911 tree ve = DECL_VALUE_EXPR (t);
12912 ve = tsubst_expr (ve, args, complain, in_decl,
12913 /*constant_expression_p=*/false);
12914 if (REFERENCE_REF_P (ve))
12916 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
12917 ve = TREE_OPERAND (ve, 0);
12919 SET_DECL_VALUE_EXPR (r, ve);
12921 if (CP_DECL_THREAD_LOCAL_P (r)
12922 && !processing_template_decl)
12923 set_decl_tls_model (r, decl_default_tls_model (r));
12925 else if (DECL_SELF_REFERENCE_P (t))
12926 SET_DECL_SELF_REFERENCE_P (r);
12927 TREE_TYPE (r) = type;
12928 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12929 DECL_CONTEXT (r) = ctx;
12930 /* Clear out the mangled name and RTL for the instantiation. */
12931 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12932 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
12933 SET_DECL_RTL (r, NULL);
12934 /* The initializer must not be expanded until it is required;
12935 see [temp.inst]. */
12936 DECL_INITIAL (r) = NULL_TREE;
12937 if (VAR_P (r))
12938 SET_DECL_MODE (r, VOIDmode);
12939 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
12940 SET_DECL_RTL (r, NULL);
12941 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
12942 if (VAR_P (r))
12944 /* Possibly limit visibility based on template args. */
12945 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12946 if (DECL_VISIBILITY_SPECIFIED (t))
12948 DECL_VISIBILITY_SPECIFIED (r) = 0;
12949 DECL_ATTRIBUTES (r)
12950 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12952 determine_visibility (r);
12955 if (!local_p)
12957 /* A static data member declaration is always marked
12958 external when it is declared in-class, even if an
12959 initializer is present. We mimic the non-template
12960 processing here. */
12961 DECL_EXTERNAL (r) = 1;
12962 if (DECL_NAMESPACE_SCOPE_P (t))
12963 DECL_NOT_REALLY_EXTERN (r) = 1;
12965 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
12966 SET_DECL_IMPLICIT_INSTANTIATION (r);
12967 register_specialization (r, gen_tmpl, argvec, false, hash);
12969 else
12971 if (DECL_LANG_SPECIFIC (r))
12972 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12973 if (!cp_unevaluated_operand)
12974 register_local_specialization (r, t);
12977 DECL_CHAIN (r) = NULL_TREE;
12979 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
12980 /*flags=*/0,
12981 args, complain, in_decl);
12983 /* Preserve a typedef that names a type. */
12984 if (is_typedef_decl (r) && type != error_mark_node)
12986 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
12987 set_underlying_type (r);
12988 if (TYPE_DECL_ALIAS_P (r))
12989 /* An alias template specialization can be dependent
12990 even if its underlying type is not. */
12991 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
12994 layout_decl (r, 0);
12996 break;
12998 default:
12999 gcc_unreachable ();
13001 #undef RETURN
13003 out:
13004 /* Restore the file and line information. */
13005 input_location = saved_loc;
13007 return r;
13010 /* Substitute into the ARG_TYPES of a function type.
13011 If END is a TREE_CHAIN, leave it and any following types
13012 un-substituted. */
13014 static tree
13015 tsubst_arg_types (tree arg_types,
13016 tree args,
13017 tree end,
13018 tsubst_flags_t complain,
13019 tree in_decl)
13021 tree remaining_arg_types;
13022 tree type = NULL_TREE;
13023 int i = 1;
13024 tree expanded_args = NULL_TREE;
13025 tree default_arg;
13027 if (!arg_types || arg_types == void_list_node || arg_types == end)
13028 return arg_types;
13030 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
13031 args, end, complain, in_decl);
13032 if (remaining_arg_types == error_mark_node)
13033 return error_mark_node;
13035 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
13037 /* For a pack expansion, perform substitution on the
13038 entire expression. Later on, we'll handle the arguments
13039 one-by-one. */
13040 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
13041 args, complain, in_decl);
13043 if (TREE_CODE (expanded_args) == TREE_VEC)
13044 /* So that we'll spin through the parameters, one by one. */
13045 i = TREE_VEC_LENGTH (expanded_args);
13046 else
13048 /* We only partially substituted into the parameter
13049 pack. Our type is TYPE_PACK_EXPANSION. */
13050 type = expanded_args;
13051 expanded_args = NULL_TREE;
13055 while (i > 0) {
13056 --i;
13058 if (expanded_args)
13059 type = TREE_VEC_ELT (expanded_args, i);
13060 else if (!type)
13061 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
13063 if (type == error_mark_node)
13064 return error_mark_node;
13065 if (VOID_TYPE_P (type))
13067 if (complain & tf_error)
13069 error ("invalid parameter type %qT", type);
13070 if (in_decl)
13071 error ("in declaration %q+D", in_decl);
13073 return error_mark_node;
13075 /* DR 657. */
13076 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
13077 return error_mark_node;
13079 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13080 top-level qualifiers as required. */
13081 type = cv_unqualified (type_decays_to (type));
13083 /* We do not substitute into default arguments here. The standard
13084 mandates that they be instantiated only when needed, which is
13085 done in build_over_call. */
13086 default_arg = TREE_PURPOSE (arg_types);
13088 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
13090 /* We've instantiated a template before its default arguments
13091 have been parsed. This can happen for a nested template
13092 class, and is not an error unless we require the default
13093 argument in a call of this function. */
13094 remaining_arg_types =
13095 tree_cons (default_arg, type, remaining_arg_types);
13096 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
13098 else
13099 remaining_arg_types =
13100 hash_tree_cons (default_arg, type, remaining_arg_types);
13103 return remaining_arg_types;
13106 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13107 *not* handle the exception-specification for FNTYPE, because the
13108 initial substitution of explicitly provided template parameters
13109 during argument deduction forbids substitution into the
13110 exception-specification:
13112 [temp.deduct]
13114 All references in the function type of the function template to the
13115 corresponding template parameters are replaced by the specified tem-
13116 plate argument values. If a substitution in a template parameter or
13117 in the function type of the function template results in an invalid
13118 type, type deduction fails. [Note: The equivalent substitution in
13119 exception specifications is done only when the function is instanti-
13120 ated, at which point a program is ill-formed if the substitution
13121 results in an invalid type.] */
13123 static tree
13124 tsubst_function_type (tree t,
13125 tree args,
13126 tsubst_flags_t complain,
13127 tree in_decl)
13129 tree return_type;
13130 tree arg_types = NULL_TREE;
13131 tree fntype;
13133 /* The TYPE_CONTEXT is not used for function/method types. */
13134 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
13136 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13137 failure. */
13138 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13140 if (late_return_type_p)
13142 /* Substitute the argument types. */
13143 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13144 complain, in_decl);
13145 if (arg_types == error_mark_node)
13146 return error_mark_node;
13148 tree save_ccp = current_class_ptr;
13149 tree save_ccr = current_class_ref;
13150 tree this_type = (TREE_CODE (t) == METHOD_TYPE
13151 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
13152 bool do_inject = this_type && CLASS_TYPE_P (this_type);
13153 if (do_inject)
13155 /* DR 1207: 'this' is in scope in the trailing return type. */
13156 inject_this_parameter (this_type, cp_type_quals (this_type));
13159 /* Substitute the return type. */
13160 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13162 if (do_inject)
13164 current_class_ptr = save_ccp;
13165 current_class_ref = save_ccr;
13168 else
13169 /* Substitute the return type. */
13170 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13172 if (return_type == error_mark_node)
13173 return error_mark_node;
13174 /* DR 486 clarifies that creation of a function type with an
13175 invalid return type is a deduction failure. */
13176 if (TREE_CODE (return_type) == ARRAY_TYPE
13177 || TREE_CODE (return_type) == FUNCTION_TYPE)
13179 if (complain & tf_error)
13181 if (TREE_CODE (return_type) == ARRAY_TYPE)
13182 error ("function returning an array");
13183 else
13184 error ("function returning a function");
13186 return error_mark_node;
13188 /* And DR 657. */
13189 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
13190 return error_mark_node;
13192 if (!late_return_type_p)
13194 /* Substitute the argument types. */
13195 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13196 complain, in_decl);
13197 if (arg_types == error_mark_node)
13198 return error_mark_node;
13201 /* Construct a new type node and return it. */
13202 if (TREE_CODE (t) == FUNCTION_TYPE)
13204 fntype = build_function_type (return_type, arg_types);
13205 fntype = apply_memfn_quals (fntype,
13206 type_memfn_quals (t),
13207 type_memfn_rqual (t));
13209 else
13211 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13212 /* Don't pick up extra function qualifiers from the basetype. */
13213 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13214 if (! MAYBE_CLASS_TYPE_P (r))
13216 /* [temp.deduct]
13218 Type deduction may fail for any of the following
13219 reasons:
13221 -- Attempting to create "pointer to member of T" when T
13222 is not a class type. */
13223 if (complain & tf_error)
13224 error ("creating pointer to member function of non-class type %qT",
13226 return error_mark_node;
13229 fntype = build_method_type_directly (r, return_type,
13230 TREE_CHAIN (arg_types));
13231 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
13233 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
13235 if (late_return_type_p)
13236 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
13238 return fntype;
13241 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
13242 ARGS into that specification, and return the substituted
13243 specification. If there is no specification, return NULL_TREE. */
13245 static tree
13246 tsubst_exception_specification (tree fntype,
13247 tree args,
13248 tsubst_flags_t complain,
13249 tree in_decl,
13250 bool defer_ok)
13252 tree specs;
13253 tree new_specs;
13255 specs = TYPE_RAISES_EXCEPTIONS (fntype);
13256 new_specs = NULL_TREE;
13257 if (specs && TREE_PURPOSE (specs))
13259 /* A noexcept-specifier. */
13260 tree expr = TREE_PURPOSE (specs);
13261 if (TREE_CODE (expr) == INTEGER_CST)
13262 new_specs = expr;
13263 else if (defer_ok)
13265 /* Defer instantiation of noexcept-specifiers to avoid
13266 excessive instantiations (c++/49107). */
13267 new_specs = make_node (DEFERRED_NOEXCEPT);
13268 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
13270 /* We already partially instantiated this member template,
13271 so combine the new args with the old. */
13272 DEFERRED_NOEXCEPT_PATTERN (new_specs)
13273 = DEFERRED_NOEXCEPT_PATTERN (expr);
13274 DEFERRED_NOEXCEPT_ARGS (new_specs)
13275 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
13277 else
13279 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
13280 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
13283 else
13284 new_specs = tsubst_copy_and_build
13285 (expr, args, complain, in_decl, /*function_p=*/false,
13286 /*integral_constant_expression_p=*/true);
13287 new_specs = build_noexcept_spec (new_specs, complain);
13289 else if (specs)
13291 if (! TREE_VALUE (specs))
13292 new_specs = specs;
13293 else
13294 while (specs)
13296 tree spec;
13297 int i, len = 1;
13298 tree expanded_specs = NULL_TREE;
13300 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
13302 /* Expand the pack expansion type. */
13303 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
13304 args, complain,
13305 in_decl);
13307 if (expanded_specs == error_mark_node)
13308 return error_mark_node;
13309 else if (TREE_CODE (expanded_specs) == TREE_VEC)
13310 len = TREE_VEC_LENGTH (expanded_specs);
13311 else
13313 /* We're substituting into a member template, so
13314 we got a TYPE_PACK_EXPANSION back. Add that
13315 expansion and move on. */
13316 gcc_assert (TREE_CODE (expanded_specs)
13317 == TYPE_PACK_EXPANSION);
13318 new_specs = add_exception_specifier (new_specs,
13319 expanded_specs,
13320 complain);
13321 specs = TREE_CHAIN (specs);
13322 continue;
13326 for (i = 0; i < len; ++i)
13328 if (expanded_specs)
13329 spec = TREE_VEC_ELT (expanded_specs, i);
13330 else
13331 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
13332 if (spec == error_mark_node)
13333 return spec;
13334 new_specs = add_exception_specifier (new_specs, spec,
13335 complain);
13338 specs = TREE_CHAIN (specs);
13341 return new_specs;
13344 /* Take the tree structure T and replace template parameters used
13345 therein with the argument vector ARGS. IN_DECL is an associated
13346 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
13347 Issue error and warning messages under control of COMPLAIN. Note
13348 that we must be relatively non-tolerant of extensions here, in
13349 order to preserve conformance; if we allow substitutions that
13350 should not be allowed, we may allow argument deductions that should
13351 not succeed, and therefore report ambiguous overload situations
13352 where there are none. In theory, we could allow the substitution,
13353 but indicate that it should have failed, and allow our caller to
13354 make sure that the right thing happens, but we don't try to do this
13355 yet.
13357 This function is used for dealing with types, decls and the like;
13358 for expressions, use tsubst_expr or tsubst_copy. */
13360 tree
13361 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13363 enum tree_code code;
13364 tree type, r = NULL_TREE;
13366 if (t == NULL_TREE || t == error_mark_node
13367 || t == integer_type_node
13368 || t == void_type_node
13369 || t == char_type_node
13370 || t == unknown_type_node
13371 || TREE_CODE (t) == NAMESPACE_DECL
13372 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
13373 return t;
13375 if (DECL_P (t))
13376 return tsubst_decl (t, args, complain);
13378 if (args == NULL_TREE)
13379 return t;
13381 code = TREE_CODE (t);
13383 if (code == IDENTIFIER_NODE)
13384 type = IDENTIFIER_TYPE_VALUE (t);
13385 else
13386 type = TREE_TYPE (t);
13388 gcc_assert (type != unknown_type_node);
13390 /* Reuse typedefs. We need to do this to handle dependent attributes,
13391 such as attribute aligned. */
13392 if (TYPE_P (t)
13393 && typedef_variant_p (t))
13395 tree decl = TYPE_NAME (t);
13397 if (alias_template_specialization_p (t))
13399 /* DECL represents an alias template and we want to
13400 instantiate it. */
13401 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13402 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13403 r = instantiate_alias_template (tmpl, gen_args, complain);
13405 else if (DECL_CLASS_SCOPE_P (decl)
13406 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
13407 && uses_template_parms (DECL_CONTEXT (decl)))
13409 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13410 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13411 r = retrieve_specialization (tmpl, gen_args, 0);
13413 else if (DECL_FUNCTION_SCOPE_P (decl)
13414 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
13415 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
13416 r = retrieve_local_specialization (decl);
13417 else
13418 /* The typedef is from a non-template context. */
13419 return t;
13421 if (r)
13423 r = TREE_TYPE (r);
13424 r = cp_build_qualified_type_real
13425 (r, cp_type_quals (t) | cp_type_quals (r),
13426 complain | tf_ignore_bad_quals);
13427 return r;
13429 else
13431 /* We don't have an instantiation yet, so drop the typedef. */
13432 int quals = cp_type_quals (t);
13433 t = DECL_ORIGINAL_TYPE (decl);
13434 t = cp_build_qualified_type_real (t, quals,
13435 complain | tf_ignore_bad_quals);
13439 bool fndecl_type = (complain & tf_fndecl_type);
13440 complain &= ~tf_fndecl_type;
13442 if (type
13443 && code != TYPENAME_TYPE
13444 && code != TEMPLATE_TYPE_PARM
13445 && code != TEMPLATE_PARM_INDEX
13446 && code != IDENTIFIER_NODE
13447 && code != FUNCTION_TYPE
13448 && code != METHOD_TYPE)
13449 type = tsubst (type, args, complain, in_decl);
13450 if (type == error_mark_node)
13451 return error_mark_node;
13453 switch (code)
13455 case RECORD_TYPE:
13456 case UNION_TYPE:
13457 case ENUMERAL_TYPE:
13458 return tsubst_aggr_type (t, args, complain, in_decl,
13459 /*entering_scope=*/0);
13461 case ERROR_MARK:
13462 case IDENTIFIER_NODE:
13463 case VOID_TYPE:
13464 case REAL_TYPE:
13465 case COMPLEX_TYPE:
13466 case VECTOR_TYPE:
13467 case BOOLEAN_TYPE:
13468 case NULLPTR_TYPE:
13469 case LANG_TYPE:
13470 return t;
13472 case INTEGER_TYPE:
13473 if (t == integer_type_node)
13474 return t;
13476 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
13477 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
13478 return t;
13481 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
13483 max = tsubst_expr (omax, args, complain, in_decl,
13484 /*integral_constant_expression_p=*/false);
13486 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13487 needed. */
13488 if (TREE_CODE (max) == NOP_EXPR
13489 && TREE_SIDE_EFFECTS (omax)
13490 && !TREE_TYPE (max))
13491 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
13493 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13494 with TREE_SIDE_EFFECTS that indicates this is not an integral
13495 constant expression. */
13496 if (processing_template_decl
13497 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
13499 gcc_assert (TREE_CODE (max) == NOP_EXPR);
13500 TREE_SIDE_EFFECTS (max) = 1;
13503 return compute_array_index_type (NULL_TREE, max, complain);
13506 case TEMPLATE_TYPE_PARM:
13507 case TEMPLATE_TEMPLATE_PARM:
13508 case BOUND_TEMPLATE_TEMPLATE_PARM:
13509 case TEMPLATE_PARM_INDEX:
13511 int idx;
13512 int level;
13513 int levels;
13514 tree arg = NULL_TREE;
13516 /* Early in template argument deduction substitution, we don't
13517 want to reduce the level of 'auto', or it will be confused
13518 with a normal template parm in subsequent deduction. */
13519 if (is_auto (t) && (complain & tf_partial))
13520 return t;
13522 r = NULL_TREE;
13524 gcc_assert (TREE_VEC_LENGTH (args) > 0);
13525 template_parm_level_and_index (t, &level, &idx);
13527 levels = TMPL_ARGS_DEPTH (args);
13528 if (level <= levels
13529 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
13531 arg = TMPL_ARG (args, level, idx);
13533 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
13535 /* See through ARGUMENT_PACK_SELECT arguments. */
13536 arg = ARGUMENT_PACK_SELECT_ARG (arg);
13537 /* If the selected argument is an expansion E, that most
13538 likely means we were called from
13539 gen_elem_of_pack_expansion_instantiation during the
13540 substituting of pack an argument pack (which Ith
13541 element is a pack expansion, where I is
13542 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
13543 In this case, the Ith element resulting from this
13544 substituting is going to be a pack expansion, which
13545 pattern is the pattern of E. Let's return the
13546 pattern of E, and
13547 gen_elem_of_pack_expansion_instantiation will
13548 build the resulting pack expansion from it. */
13549 if (PACK_EXPANSION_P (arg))
13551 /* Make sure we aren't throwing away arg info. */
13552 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
13553 arg = PACK_EXPANSION_PATTERN (arg);
13558 if (arg == error_mark_node)
13559 return error_mark_node;
13560 else if (arg != NULL_TREE)
13562 if (ARGUMENT_PACK_P (arg))
13563 /* If ARG is an argument pack, we don't actually want to
13564 perform a substitution here, because substitutions
13565 for argument packs are only done
13566 element-by-element. We can get to this point when
13567 substituting the type of a non-type template
13568 parameter pack, when that type actually contains
13569 template parameter packs from an outer template, e.g.,
13571 template<typename... Types> struct A {
13572 template<Types... Values> struct B { };
13573 }; */
13574 return t;
13576 if (code == TEMPLATE_TYPE_PARM)
13578 int quals;
13579 gcc_assert (TYPE_P (arg));
13581 quals = cp_type_quals (arg) | cp_type_quals (t);
13583 return cp_build_qualified_type_real
13584 (arg, quals, complain | tf_ignore_bad_quals);
13586 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13588 /* We are processing a type constructed from a
13589 template template parameter. */
13590 tree argvec = tsubst (TYPE_TI_ARGS (t),
13591 args, complain, in_decl);
13592 if (argvec == error_mark_node)
13593 return error_mark_node;
13595 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
13596 || TREE_CODE (arg) == TEMPLATE_DECL
13597 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
13599 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
13600 /* Consider this code:
13602 template <template <class> class Template>
13603 struct Internal {
13604 template <class Arg> using Bind = Template<Arg>;
13607 template <template <class> class Template, class Arg>
13608 using Instantiate = Template<Arg>; //#0
13610 template <template <class> class Template,
13611 class Argument>
13612 using Bind =
13613 Instantiate<Internal<Template>::template Bind,
13614 Argument>; //#1
13616 When #1 is parsed, the
13617 BOUND_TEMPLATE_TEMPLATE_PARM representing the
13618 parameter `Template' in #0 matches the
13619 UNBOUND_CLASS_TEMPLATE representing the argument
13620 `Internal<Template>::template Bind'; We then want
13621 to assemble the type `Bind<Argument>' that can't
13622 be fully created right now, because
13623 `Internal<Template>' not being complete, the Bind
13624 template cannot be looked up in that context. So
13625 we need to "store" `Bind<Argument>' for later
13626 when the context of Bind becomes complete. Let's
13627 store that in a TYPENAME_TYPE. */
13628 return make_typename_type (TYPE_CONTEXT (arg),
13629 build_nt (TEMPLATE_ID_EXPR,
13630 TYPE_IDENTIFIER (arg),
13631 argvec),
13632 typename_type,
13633 complain);
13635 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
13636 are resolving nested-types in the signature of a
13637 member function templates. Otherwise ARG is a
13638 TEMPLATE_DECL and is the real template to be
13639 instantiated. */
13640 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
13641 arg = TYPE_NAME (arg);
13643 r = lookup_template_class (arg,
13644 argvec, in_decl,
13645 DECL_CONTEXT (arg),
13646 /*entering_scope=*/0,
13647 complain);
13648 return cp_build_qualified_type_real
13649 (r, cp_type_quals (t) | cp_type_quals (r), complain);
13651 else if (code == TEMPLATE_TEMPLATE_PARM)
13652 return arg;
13653 else
13654 /* TEMPLATE_PARM_INDEX. */
13655 return convert_from_reference (unshare_expr (arg));
13658 if (level == 1)
13659 /* This can happen during the attempted tsubst'ing in
13660 unify. This means that we don't yet have any information
13661 about the template parameter in question. */
13662 return t;
13664 /* If we get here, we must have been looking at a parm for a
13665 more deeply nested template. Make a new version of this
13666 template parameter, but with a lower level. */
13667 switch (code)
13669 case TEMPLATE_TYPE_PARM:
13670 case TEMPLATE_TEMPLATE_PARM:
13671 case BOUND_TEMPLATE_TEMPLATE_PARM:
13672 if (cp_type_quals (t))
13674 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
13675 r = cp_build_qualified_type_real
13676 (r, cp_type_quals (t),
13677 complain | (code == TEMPLATE_TYPE_PARM
13678 ? tf_ignore_bad_quals : 0));
13680 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
13681 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
13682 && (r = (TEMPLATE_PARM_DESCENDANTS
13683 (TEMPLATE_TYPE_PARM_INDEX (t))))
13684 && (r = TREE_TYPE (r))
13685 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
13686 /* Break infinite recursion when substituting the constraints
13687 of a constrained placeholder. */;
13688 else
13690 r = copy_type (t);
13691 TEMPLATE_TYPE_PARM_INDEX (r)
13692 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
13693 r, levels, args, complain);
13694 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
13695 TYPE_MAIN_VARIANT (r) = r;
13696 TYPE_POINTER_TO (r) = NULL_TREE;
13697 TYPE_REFERENCE_TO (r) = NULL_TREE;
13699 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
13701 /* Propagate constraints on placeholders. */
13702 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
13703 PLACEHOLDER_TYPE_CONSTRAINTS (r)
13704 = tsubst_constraint (constr, args, complain, in_decl);
13705 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
13707 if (DECL_TEMPLATE_TEMPLATE_PARM_P (pl))
13708 pl = tsubst (pl, args, complain, in_decl);
13709 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
13713 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
13714 /* We have reduced the level of the template
13715 template parameter, but not the levels of its
13716 template parameters, so canonical_type_parameter
13717 will not be able to find the canonical template
13718 template parameter for this level. Thus, we
13719 require structural equality checking to compare
13720 TEMPLATE_TEMPLATE_PARMs. */
13721 SET_TYPE_STRUCTURAL_EQUALITY (r);
13722 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
13723 SET_TYPE_STRUCTURAL_EQUALITY (r);
13724 else
13725 TYPE_CANONICAL (r) = canonical_type_parameter (r);
13727 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13729 tree tinfo = TYPE_TEMPLATE_INFO (t);
13730 /* We might need to substitute into the types of non-type
13731 template parameters. */
13732 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
13733 complain, in_decl);
13734 if (tmpl == error_mark_node)
13735 return error_mark_node;
13736 tree argvec = tsubst (TI_ARGS (tinfo), args,
13737 complain, in_decl);
13738 if (argvec == error_mark_node)
13739 return error_mark_node;
13741 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
13742 = build_template_info (tmpl, argvec);
13745 break;
13747 case TEMPLATE_PARM_INDEX:
13748 /* OK, now substitute the type of the non-type parameter. We
13749 couldn't do it earlier because it might be an auto parameter,
13750 and we wouldn't need to if we had an argument. */
13751 type = tsubst (type, args, complain, in_decl);
13752 r = reduce_template_parm_level (t, type, levels, args, complain);
13753 break;
13755 default:
13756 gcc_unreachable ();
13759 return r;
13762 case TREE_LIST:
13764 tree purpose, value, chain;
13766 if (t == void_list_node)
13767 return t;
13769 purpose = TREE_PURPOSE (t);
13770 if (purpose)
13772 purpose = tsubst (purpose, args, complain, in_decl);
13773 if (purpose == error_mark_node)
13774 return error_mark_node;
13776 value = TREE_VALUE (t);
13777 if (value)
13779 value = tsubst (value, args, complain, in_decl);
13780 if (value == error_mark_node)
13781 return error_mark_node;
13783 chain = TREE_CHAIN (t);
13784 if (chain && chain != void_type_node)
13786 chain = tsubst (chain, args, complain, in_decl);
13787 if (chain == error_mark_node)
13788 return error_mark_node;
13790 if (purpose == TREE_PURPOSE (t)
13791 && value == TREE_VALUE (t)
13792 && chain == TREE_CHAIN (t))
13793 return t;
13794 return hash_tree_cons (purpose, value, chain);
13797 case TREE_BINFO:
13798 /* We should never be tsubsting a binfo. */
13799 gcc_unreachable ();
13801 case TREE_VEC:
13802 /* A vector of template arguments. */
13803 gcc_assert (!type);
13804 return tsubst_template_args (t, args, complain, in_decl);
13806 case POINTER_TYPE:
13807 case REFERENCE_TYPE:
13809 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
13810 return t;
13812 /* [temp.deduct]
13814 Type deduction may fail for any of the following
13815 reasons:
13817 -- Attempting to create a pointer to reference type.
13818 -- Attempting to create a reference to a reference type or
13819 a reference to void.
13821 Core issue 106 says that creating a reference to a reference
13822 during instantiation is no longer a cause for failure. We
13823 only enforce this check in strict C++98 mode. */
13824 if ((TREE_CODE (type) == REFERENCE_TYPE
13825 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
13826 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
13828 static location_t last_loc;
13830 /* We keep track of the last time we issued this error
13831 message to avoid spewing a ton of messages during a
13832 single bad template instantiation. */
13833 if (complain & tf_error
13834 && last_loc != input_location)
13836 if (VOID_TYPE_P (type))
13837 error ("forming reference to void");
13838 else if (code == POINTER_TYPE)
13839 error ("forming pointer to reference type %qT", type);
13840 else
13841 error ("forming reference to reference type %qT", type);
13842 last_loc = input_location;
13845 return error_mark_node;
13847 else if (TREE_CODE (type) == FUNCTION_TYPE
13848 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
13849 || type_memfn_rqual (type) != REF_QUAL_NONE))
13851 if (complain & tf_error)
13853 if (code == POINTER_TYPE)
13854 error ("forming pointer to qualified function type %qT",
13855 type);
13856 else
13857 error ("forming reference to qualified function type %qT",
13858 type);
13860 return error_mark_node;
13862 else if (code == POINTER_TYPE)
13864 r = build_pointer_type (type);
13865 if (TREE_CODE (type) == METHOD_TYPE)
13866 r = build_ptrmemfunc_type (r);
13868 else if (TREE_CODE (type) == REFERENCE_TYPE)
13869 /* In C++0x, during template argument substitution, when there is an
13870 attempt to create a reference to a reference type, reference
13871 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
13873 "If a template-argument for a template-parameter T names a type
13874 that is a reference to a type A, an attempt to create the type
13875 'lvalue reference to cv T' creates the type 'lvalue reference to
13876 A,' while an attempt to create the type type rvalue reference to
13877 cv T' creates the type T"
13879 r = cp_build_reference_type
13880 (TREE_TYPE (type),
13881 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
13882 else
13883 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
13884 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
13886 if (r != error_mark_node)
13887 /* Will this ever be needed for TYPE_..._TO values? */
13888 layout_type (r);
13890 return r;
13892 case OFFSET_TYPE:
13894 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
13895 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
13897 /* [temp.deduct]
13899 Type deduction may fail for any of the following
13900 reasons:
13902 -- Attempting to create "pointer to member of T" when T
13903 is not a class type. */
13904 if (complain & tf_error)
13905 error ("creating pointer to member of non-class type %qT", r);
13906 return error_mark_node;
13908 if (TREE_CODE (type) == REFERENCE_TYPE)
13910 if (complain & tf_error)
13911 error ("creating pointer to member reference type %qT", type);
13912 return error_mark_node;
13914 if (VOID_TYPE_P (type))
13916 if (complain & tf_error)
13917 error ("creating pointer to member of type void");
13918 return error_mark_node;
13920 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
13921 if (TREE_CODE (type) == FUNCTION_TYPE)
13923 /* The type of the implicit object parameter gets its
13924 cv-qualifiers from the FUNCTION_TYPE. */
13925 tree memptr;
13926 tree method_type
13927 = build_memfn_type (type, r, type_memfn_quals (type),
13928 type_memfn_rqual (type));
13929 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
13930 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
13931 complain);
13933 else
13934 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
13935 cp_type_quals (t),
13936 complain);
13938 case FUNCTION_TYPE:
13939 case METHOD_TYPE:
13941 tree fntype;
13942 tree specs;
13943 fntype = tsubst_function_type (t, args, complain, in_decl);
13944 if (fntype == error_mark_node)
13945 return error_mark_node;
13947 /* Substitute the exception specification. */
13948 specs = tsubst_exception_specification (t, args, complain, in_decl,
13949 /*defer_ok*/fndecl_type);
13950 if (specs == error_mark_node)
13951 return error_mark_node;
13952 if (specs)
13953 fntype = build_exception_variant (fntype, specs);
13954 return fntype;
13956 case ARRAY_TYPE:
13958 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
13959 if (domain == error_mark_node)
13960 return error_mark_node;
13962 /* As an optimization, we avoid regenerating the array type if
13963 it will obviously be the same as T. */
13964 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
13965 return t;
13967 /* These checks should match the ones in create_array_type_for_decl.
13969 [temp.deduct]
13971 The deduction may fail for any of the following reasons:
13973 -- Attempting to create an array with an element type that
13974 is void, a function type, or a reference type, or [DR337]
13975 an abstract class type. */
13976 if (VOID_TYPE_P (type)
13977 || TREE_CODE (type) == FUNCTION_TYPE
13978 || (TREE_CODE (type) == ARRAY_TYPE
13979 && TYPE_DOMAIN (type) == NULL_TREE)
13980 || TREE_CODE (type) == REFERENCE_TYPE)
13982 if (complain & tf_error)
13983 error ("creating array of %qT", type);
13984 return error_mark_node;
13987 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
13988 return error_mark_node;
13990 r = build_cplus_array_type (type, domain);
13992 if (TYPE_USER_ALIGN (t))
13994 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
13995 TYPE_USER_ALIGN (r) = 1;
13998 return r;
14001 case TYPENAME_TYPE:
14003 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14004 in_decl, /*entering_scope=*/1);
14005 if (ctx == error_mark_node)
14006 return error_mark_node;
14008 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
14009 complain, in_decl);
14010 if (f == error_mark_node)
14011 return error_mark_node;
14013 if (!MAYBE_CLASS_TYPE_P (ctx))
14015 if (complain & tf_error)
14016 error ("%qT is not a class, struct, or union type", ctx);
14017 return error_mark_node;
14019 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
14021 /* Normally, make_typename_type does not require that the CTX
14022 have complete type in order to allow things like:
14024 template <class T> struct S { typename S<T>::X Y; };
14026 But, such constructs have already been resolved by this
14027 point, so here CTX really should have complete type, unless
14028 it's a partial instantiation. */
14029 ctx = complete_type (ctx);
14030 if (!COMPLETE_TYPE_P (ctx))
14032 if (complain & tf_error)
14033 cxx_incomplete_type_error (NULL_TREE, ctx);
14034 return error_mark_node;
14038 f = make_typename_type (ctx, f, typename_type,
14039 complain | tf_keep_type_decl);
14040 if (f == error_mark_node)
14041 return f;
14042 if (TREE_CODE (f) == TYPE_DECL)
14044 complain |= tf_ignore_bad_quals;
14045 f = TREE_TYPE (f);
14048 if (TREE_CODE (f) != TYPENAME_TYPE)
14050 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
14052 if (complain & tf_error)
14053 error ("%qT resolves to %qT, which is not an enumeration type",
14054 t, f);
14055 else
14056 return error_mark_node;
14058 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
14060 if (complain & tf_error)
14061 error ("%qT resolves to %qT, which is is not a class type",
14062 t, f);
14063 else
14064 return error_mark_node;
14068 return cp_build_qualified_type_real
14069 (f, cp_type_quals (f) | cp_type_quals (t), complain);
14072 case UNBOUND_CLASS_TEMPLATE:
14074 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14075 in_decl, /*entering_scope=*/1);
14076 tree name = TYPE_IDENTIFIER (t);
14077 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
14079 if (ctx == error_mark_node || name == error_mark_node)
14080 return error_mark_node;
14082 if (parm_list)
14083 parm_list = tsubst_template_parms (parm_list, args, complain);
14084 return make_unbound_class_template (ctx, name, parm_list, complain);
14087 case TYPEOF_TYPE:
14089 tree type;
14091 ++cp_unevaluated_operand;
14092 ++c_inhibit_evaluation_warnings;
14094 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
14095 complain, in_decl,
14096 /*integral_constant_expression_p=*/false);
14098 --cp_unevaluated_operand;
14099 --c_inhibit_evaluation_warnings;
14101 type = finish_typeof (type);
14102 return cp_build_qualified_type_real (type,
14103 cp_type_quals (t)
14104 | cp_type_quals (type),
14105 complain);
14108 case DECLTYPE_TYPE:
14110 tree type;
14112 ++cp_unevaluated_operand;
14113 ++c_inhibit_evaluation_warnings;
14115 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
14116 complain|tf_decltype, in_decl,
14117 /*function_p*/false,
14118 /*integral_constant_expression*/false);
14120 if (DECLTYPE_FOR_INIT_CAPTURE (t))
14122 if (type == NULL_TREE)
14124 if (complain & tf_error)
14125 error ("empty initializer in lambda init-capture");
14126 type = error_mark_node;
14128 else if (TREE_CODE (type) == TREE_LIST)
14129 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
14132 --cp_unevaluated_operand;
14133 --c_inhibit_evaluation_warnings;
14135 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
14136 type = lambda_capture_field_type (type,
14137 DECLTYPE_FOR_INIT_CAPTURE (t),
14138 DECLTYPE_FOR_REF_CAPTURE (t));
14139 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
14140 type = lambda_proxy_type (type);
14141 else
14143 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
14144 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
14145 && EXPR_P (type))
14146 /* In a template ~id could be either a complement expression
14147 or an unqualified-id naming a destructor; if instantiating
14148 it produces an expression, it's not an id-expression or
14149 member access. */
14150 id = false;
14151 type = finish_decltype_type (type, id, complain);
14153 return cp_build_qualified_type_real (type,
14154 cp_type_quals (t)
14155 | cp_type_quals (type),
14156 complain | tf_ignore_bad_quals);
14159 case UNDERLYING_TYPE:
14161 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
14162 complain, in_decl);
14163 return finish_underlying_type (type);
14166 case TYPE_ARGUMENT_PACK:
14167 case NONTYPE_ARGUMENT_PACK:
14169 tree r;
14171 if (code == NONTYPE_ARGUMENT_PACK)
14172 r = make_node (code);
14173 else
14174 r = cxx_make_type (code);
14176 tree pack_args = ARGUMENT_PACK_ARGS (t);
14177 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
14178 SET_ARGUMENT_PACK_ARGS (r, pack_args);
14180 return r;
14183 case VOID_CST:
14184 case INTEGER_CST:
14185 case REAL_CST:
14186 case STRING_CST:
14187 case PLUS_EXPR:
14188 case MINUS_EXPR:
14189 case NEGATE_EXPR:
14190 case NOP_EXPR:
14191 case INDIRECT_REF:
14192 case ADDR_EXPR:
14193 case CALL_EXPR:
14194 case ARRAY_REF:
14195 case SCOPE_REF:
14196 /* We should use one of the expression tsubsts for these codes. */
14197 gcc_unreachable ();
14199 default:
14200 sorry ("use of %qs in template", get_tree_code_name (code));
14201 return error_mark_node;
14205 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
14206 type of the expression on the left-hand side of the "." or "->"
14207 operator. */
14209 static tree
14210 tsubst_baselink (tree baselink, tree object_type,
14211 tree args, tsubst_flags_t complain, tree in_decl)
14213 tree name;
14214 tree qualifying_scope;
14215 tree fns;
14216 tree optype;
14217 tree template_args = 0;
14218 bool template_id_p = false;
14219 bool qualified = BASELINK_QUALIFIED_P (baselink);
14221 /* A baselink indicates a function from a base class. Both the
14222 BASELINK_ACCESS_BINFO and the base class referenced may
14223 indicate bases of the template class, rather than the
14224 instantiated class. In addition, lookups that were not
14225 ambiguous before may be ambiguous now. Therefore, we perform
14226 the lookup again. */
14227 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
14228 qualifying_scope = tsubst (qualifying_scope, args,
14229 complain, in_decl);
14230 fns = BASELINK_FUNCTIONS (baselink);
14231 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
14232 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
14234 template_id_p = true;
14235 template_args = TREE_OPERAND (fns, 1);
14236 fns = TREE_OPERAND (fns, 0);
14237 if (template_args)
14238 template_args = tsubst_template_args (template_args, args,
14239 complain, in_decl);
14241 name = OVL_NAME (fns);
14242 if (IDENTIFIER_TYPENAME_P (name))
14243 name = mangle_conv_op_name_for_type (optype);
14244 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
14245 if (!baselink)
14247 if (constructor_name_p (name, qualifying_scope))
14249 if (complain & tf_error)
14250 error ("cannot call constructor %<%T::%D%> directly",
14251 qualifying_scope, name);
14253 return error_mark_node;
14256 /* If lookup found a single function, mark it as used at this
14257 point. (If it lookup found multiple functions the one selected
14258 later by overload resolution will be marked as used at that
14259 point.) */
14260 if (BASELINK_P (baselink))
14261 fns = BASELINK_FUNCTIONS (baselink);
14262 if (!template_id_p && !really_overloaded_fn (fns)
14263 && !mark_used (OVL_FIRST (fns), complain) && !(complain & tf_error))
14264 return error_mark_node;
14266 /* Add back the template arguments, if present. */
14267 if (BASELINK_P (baselink) && template_id_p)
14268 BASELINK_FUNCTIONS (baselink)
14269 = build2 (TEMPLATE_ID_EXPR,
14270 unknown_type_node,
14271 BASELINK_FUNCTIONS (baselink),
14272 template_args);
14273 /* Update the conversion operator type. */
14274 if (BASELINK_P (baselink))
14275 BASELINK_OPTYPE (baselink) = optype;
14277 if (!object_type)
14278 object_type = current_class_type;
14280 if (qualified || name == complete_dtor_identifier)
14282 baselink = adjust_result_of_qualified_name_lookup (baselink,
14283 qualifying_scope,
14284 object_type);
14285 if (!qualified)
14286 /* We need to call adjust_result_of_qualified_name_lookup in case the
14287 destructor names a base class, but we unset BASELINK_QUALIFIED_P
14288 so that we still get virtual function binding. */
14289 BASELINK_QUALIFIED_P (baselink) = false;
14291 return baselink;
14294 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
14295 true if the qualified-id will be a postfix-expression in-and-of
14296 itself; false if more of the postfix-expression follows the
14297 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
14298 of "&". */
14300 static tree
14301 tsubst_qualified_id (tree qualified_id, tree args,
14302 tsubst_flags_t complain, tree in_decl,
14303 bool done, bool address_p)
14305 tree expr;
14306 tree scope;
14307 tree name;
14308 bool is_template;
14309 tree template_args;
14310 location_t loc = UNKNOWN_LOCATION;
14312 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
14314 /* Figure out what name to look up. */
14315 name = TREE_OPERAND (qualified_id, 1);
14316 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14318 is_template = true;
14319 loc = EXPR_LOCATION (name);
14320 template_args = TREE_OPERAND (name, 1);
14321 if (template_args)
14322 template_args = tsubst_template_args (template_args, args,
14323 complain, in_decl);
14324 if (template_args == error_mark_node)
14325 return error_mark_node;
14326 name = TREE_OPERAND (name, 0);
14328 else
14330 is_template = false;
14331 template_args = NULL_TREE;
14334 /* Substitute into the qualifying scope. When there are no ARGS, we
14335 are just trying to simplify a non-dependent expression. In that
14336 case the qualifying scope may be dependent, and, in any case,
14337 substituting will not help. */
14338 scope = TREE_OPERAND (qualified_id, 0);
14339 if (args)
14341 scope = tsubst (scope, args, complain, in_decl);
14342 expr = tsubst_copy (name, args, complain, in_decl);
14344 else
14345 expr = name;
14347 if (dependent_scope_p (scope))
14349 if (is_template)
14350 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
14351 tree r = build_qualified_name (NULL_TREE, scope, expr,
14352 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
14353 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
14354 return r;
14357 if (!BASELINK_P (name) && !DECL_P (expr))
14359 if (TREE_CODE (expr) == BIT_NOT_EXPR)
14361 /* A BIT_NOT_EXPR is used to represent a destructor. */
14362 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
14364 error ("qualifying type %qT does not match destructor name ~%qT",
14365 scope, TREE_OPERAND (expr, 0));
14366 expr = error_mark_node;
14368 else
14369 expr = lookup_qualified_name (scope, complete_dtor_identifier,
14370 /*is_type_p=*/0, false);
14372 else
14373 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
14374 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
14375 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
14377 if (complain & tf_error)
14379 error ("dependent-name %qE is parsed as a non-type, but "
14380 "instantiation yields a type", qualified_id);
14381 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
14383 return error_mark_node;
14387 if (DECL_P (expr))
14389 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
14390 scope);
14391 /* Remember that there was a reference to this entity. */
14392 if (!mark_used (expr, complain) && !(complain & tf_error))
14393 return error_mark_node;
14396 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
14398 if (complain & tf_error)
14399 qualified_name_lookup_error (scope,
14400 TREE_OPERAND (qualified_id, 1),
14401 expr, input_location);
14402 return error_mark_node;
14405 if (is_template)
14407 if (variable_template_p (expr))
14408 expr = lookup_and_finish_template_variable (expr, template_args,
14409 complain);
14410 else
14411 expr = lookup_template_function (expr, template_args);
14414 if (expr == error_mark_node && complain & tf_error)
14415 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
14416 expr, input_location);
14417 else if (TYPE_P (scope))
14419 expr = (adjust_result_of_qualified_name_lookup
14420 (expr, scope, current_nonlambda_class_type ()));
14421 expr = (finish_qualified_id_expr
14422 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
14423 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
14424 /*template_arg_p=*/false, complain));
14427 /* Expressions do not generally have reference type. */
14428 if (TREE_CODE (expr) != SCOPE_REF
14429 /* However, if we're about to form a pointer-to-member, we just
14430 want the referenced member referenced. */
14431 && TREE_CODE (expr) != OFFSET_REF)
14432 expr = convert_from_reference (expr);
14434 if (REF_PARENTHESIZED_P (qualified_id))
14435 expr = force_paren_expr (expr);
14437 return expr;
14440 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14441 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14442 for tsubst. */
14444 static tree
14445 tsubst_init (tree init, tree decl, tree args,
14446 tsubst_flags_t complain, tree in_decl)
14448 if (!init)
14449 return NULL_TREE;
14451 init = tsubst_expr (init, args, complain, in_decl, false);
14453 if (!init && TREE_TYPE (decl) != error_mark_node)
14455 /* If we had an initializer but it
14456 instantiated to nothing,
14457 value-initialize the object. This will
14458 only occur when the initializer was a
14459 pack expansion where the parameter packs
14460 used in that expansion were of length
14461 zero. */
14462 init = build_value_init (TREE_TYPE (decl),
14463 complain);
14464 if (TREE_CODE (init) == AGGR_INIT_EXPR)
14465 init = get_target_expr_sfinae (init, complain);
14466 if (TREE_CODE (init) == TARGET_EXPR)
14467 TARGET_EXPR_DIRECT_INIT_P (init) = true;
14470 return init;
14473 /* Like tsubst, but deals with expressions. This function just replaces
14474 template parms; to finish processing the resultant expression, use
14475 tsubst_copy_and_build or tsubst_expr. */
14477 static tree
14478 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14480 enum tree_code code;
14481 tree r;
14483 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
14484 return t;
14486 code = TREE_CODE (t);
14488 switch (code)
14490 case PARM_DECL:
14491 r = retrieve_local_specialization (t);
14493 if (r == NULL_TREE)
14495 /* We get here for a use of 'this' in an NSDMI as part of a
14496 constructor call or as part of an aggregate initialization. */
14497 if (DECL_NAME (t) == this_identifier
14498 && ((current_function_decl
14499 && DECL_CONSTRUCTOR_P (current_function_decl))
14500 || (current_class_ref
14501 && TREE_CODE (current_class_ref) == PLACEHOLDER_EXPR)))
14502 return current_class_ptr;
14504 /* This can happen for a parameter name used later in a function
14505 declaration (such as in a late-specified return type). Just
14506 make a dummy decl, since it's only used for its type. */
14507 gcc_assert (cp_unevaluated_operand != 0);
14508 r = tsubst_decl (t, args, complain);
14509 /* Give it the template pattern as its context; its true context
14510 hasn't been instantiated yet and this is good enough for
14511 mangling. */
14512 DECL_CONTEXT (r) = DECL_CONTEXT (t);
14515 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14516 r = ARGUMENT_PACK_SELECT_ARG (r);
14517 if (!mark_used (r, complain) && !(complain & tf_error))
14518 return error_mark_node;
14519 return r;
14521 case CONST_DECL:
14523 tree enum_type;
14524 tree v;
14526 if (DECL_TEMPLATE_PARM_P (t))
14527 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
14528 /* There is no need to substitute into namespace-scope
14529 enumerators. */
14530 if (DECL_NAMESPACE_SCOPE_P (t))
14531 return t;
14532 /* If ARGS is NULL, then T is known to be non-dependent. */
14533 if (args == NULL_TREE)
14534 return scalar_constant_value (t);
14536 /* Unfortunately, we cannot just call lookup_name here.
14537 Consider:
14539 template <int I> int f() {
14540 enum E { a = I };
14541 struct S { void g() { E e = a; } };
14544 When we instantiate f<7>::S::g(), say, lookup_name is not
14545 clever enough to find f<7>::a. */
14546 enum_type
14547 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14548 /*entering_scope=*/0);
14550 for (v = TYPE_VALUES (enum_type);
14551 v != NULL_TREE;
14552 v = TREE_CHAIN (v))
14553 if (TREE_PURPOSE (v) == DECL_NAME (t))
14554 return TREE_VALUE (v);
14556 /* We didn't find the name. That should never happen; if
14557 name-lookup found it during preliminary parsing, we
14558 should find it again here during instantiation. */
14559 gcc_unreachable ();
14561 return t;
14563 case FIELD_DECL:
14564 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14566 /* Check for a local specialization set up by
14567 tsubst_pack_expansion. */
14568 if (tree r = retrieve_local_specialization (t))
14570 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14571 r = ARGUMENT_PACK_SELECT_ARG (r);
14572 return r;
14575 /* When retrieving a capture pack from a generic lambda, remove the
14576 lambda call op's own template argument list from ARGS. Only the
14577 template arguments active for the closure type should be used to
14578 retrieve the pack specialization. */
14579 if (LAMBDA_FUNCTION_P (current_function_decl)
14580 && (template_class_depth (DECL_CONTEXT (t))
14581 != TMPL_ARGS_DEPTH (args)))
14582 args = strip_innermost_template_args (args, 1);
14584 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
14585 tsubst_decl put in the hash table. */
14586 return retrieve_specialization (t, args, 0);
14589 if (DECL_CONTEXT (t))
14591 tree ctx;
14593 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14594 /*entering_scope=*/1);
14595 if (ctx != DECL_CONTEXT (t))
14597 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
14598 if (!r)
14600 if (complain & tf_error)
14601 error ("using invalid field %qD", t);
14602 return error_mark_node;
14604 return r;
14608 return t;
14610 case VAR_DECL:
14611 case FUNCTION_DECL:
14612 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
14613 r = tsubst (t, args, complain, in_decl);
14614 else if (local_variable_p (t)
14615 && uses_template_parms (DECL_CONTEXT (t)))
14617 r = retrieve_local_specialization (t);
14618 if (r == NULL_TREE)
14620 /* First try name lookup to find the instantiation. */
14621 r = lookup_name (DECL_NAME (t));
14622 if (r && !is_capture_proxy (r))
14624 /* Make sure that the one we found is the one we want. */
14625 tree ctx = DECL_CONTEXT (t);
14626 if (DECL_LANG_SPECIFIC (ctx) && DECL_TEMPLATE_INFO (ctx))
14627 ctx = tsubst (ctx, args, complain, in_decl);
14628 if (ctx != DECL_CONTEXT (r))
14629 r = NULL_TREE;
14632 if (r)
14633 /* OK */;
14634 else
14636 /* This can happen for a variable used in a
14637 late-specified return type of a local lambda, or for a
14638 local static or constant. Building a new VAR_DECL
14639 should be OK in all those cases. */
14640 r = tsubst_decl (t, args, complain);
14641 if (local_specializations)
14642 /* Avoid infinite recursion (79640). */
14643 register_local_specialization (r, t);
14644 if (decl_maybe_constant_var_p (r))
14646 /* We can't call cp_finish_decl, so handle the
14647 initializer by hand. */
14648 tree init = tsubst_init (DECL_INITIAL (t), r, args,
14649 complain, in_decl);
14650 if (!processing_template_decl)
14651 init = maybe_constant_init (init);
14652 if (processing_template_decl
14653 ? potential_constant_expression (init)
14654 : reduced_constant_expression_p (init))
14655 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
14656 = TREE_CONSTANT (r) = true;
14657 DECL_INITIAL (r) = init;
14659 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
14660 || decl_constant_var_p (r)
14661 || errorcount || sorrycount);
14662 if (!processing_template_decl
14663 && !TREE_STATIC (r))
14664 r = process_outer_var_ref (r, complain);
14666 /* Remember this for subsequent uses. */
14667 if (local_specializations)
14668 register_local_specialization (r, t);
14671 else
14672 r = t;
14673 if (!mark_used (r, complain))
14674 return error_mark_node;
14675 return r;
14677 case NAMESPACE_DECL:
14678 return t;
14680 case OVERLOAD:
14681 /* An OVERLOAD will always be a non-dependent overload set; an
14682 overload set from function scope will just be represented with an
14683 IDENTIFIER_NODE, and from class scope with a BASELINK. */
14684 gcc_assert (!uses_template_parms (t));
14685 /* We must have marked any lookups as persistent. */
14686 gcc_assert (!OVL_LOOKUP_P (t) || OVL_USED_P (t));
14687 return t;
14689 case BASELINK:
14690 return tsubst_baselink (t, current_nonlambda_class_type (),
14691 args, complain, in_decl);
14693 case TEMPLATE_DECL:
14694 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
14695 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
14696 args, complain, in_decl);
14697 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
14698 return tsubst (t, args, complain, in_decl);
14699 else if (DECL_CLASS_SCOPE_P (t)
14700 && uses_template_parms (DECL_CONTEXT (t)))
14702 /* Template template argument like the following example need
14703 special treatment:
14705 template <template <class> class TT> struct C {};
14706 template <class T> struct D {
14707 template <class U> struct E {};
14708 C<E> c; // #1
14710 D<int> d; // #2
14712 We are processing the template argument `E' in #1 for
14713 the template instantiation #2. Originally, `E' is a
14714 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
14715 have to substitute this with one having context `D<int>'. */
14717 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
14718 if (dependent_scope_p (context))
14720 /* When rewriting a constructor into a deduction guide, a
14721 non-dependent name can become dependent, so memtmpl<args>
14722 becomes context::template memtmpl<args>. */
14723 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14724 return build_qualified_name (type, context, DECL_NAME (t),
14725 /*template*/true);
14727 return lookup_field (context, DECL_NAME(t), 0, false);
14729 else
14730 /* Ordinary template template argument. */
14731 return t;
14733 case CAST_EXPR:
14734 case REINTERPRET_CAST_EXPR:
14735 case CONST_CAST_EXPR:
14736 case STATIC_CAST_EXPR:
14737 case DYNAMIC_CAST_EXPR:
14738 case IMPLICIT_CONV_EXPR:
14739 case CONVERT_EXPR:
14740 case NOP_EXPR:
14742 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14743 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14744 return build1 (code, type, op0);
14747 case SIZEOF_EXPR:
14748 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
14749 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
14751 tree expanded, op = TREE_OPERAND (t, 0);
14752 int len = 0;
14754 if (SIZEOF_EXPR_TYPE_P (t))
14755 op = TREE_TYPE (op);
14757 ++cp_unevaluated_operand;
14758 ++c_inhibit_evaluation_warnings;
14759 /* We only want to compute the number of arguments. */
14760 if (PACK_EXPANSION_P (op))
14761 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
14762 else
14763 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
14764 args, complain, in_decl);
14765 --cp_unevaluated_operand;
14766 --c_inhibit_evaluation_warnings;
14768 if (TREE_CODE (expanded) == TREE_VEC)
14770 len = TREE_VEC_LENGTH (expanded);
14771 /* Set TREE_USED for the benefit of -Wunused. */
14772 for (int i = 0; i < len; i++)
14773 if (DECL_P (TREE_VEC_ELT (expanded, i)))
14774 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
14777 if (expanded == error_mark_node)
14778 return error_mark_node;
14779 else if (PACK_EXPANSION_P (expanded)
14780 || (TREE_CODE (expanded) == TREE_VEC
14781 && pack_expansion_args_count (expanded)))
14784 if (PACK_EXPANSION_P (expanded))
14785 /* OK. */;
14786 else if (TREE_VEC_LENGTH (expanded) == 1)
14787 expanded = TREE_VEC_ELT (expanded, 0);
14788 else
14789 expanded = make_argument_pack (expanded);
14791 if (TYPE_P (expanded))
14792 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
14793 complain & tf_error);
14794 else
14795 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
14796 complain & tf_error);
14798 else
14799 return build_int_cst (size_type_node, len);
14801 if (SIZEOF_EXPR_TYPE_P (t))
14803 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
14804 args, complain, in_decl);
14805 r = build1 (NOP_EXPR, r, error_mark_node);
14806 r = build1 (SIZEOF_EXPR,
14807 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
14808 SIZEOF_EXPR_TYPE_P (r) = 1;
14809 return r;
14811 /* Fall through */
14813 case INDIRECT_REF:
14814 case NEGATE_EXPR:
14815 case TRUTH_NOT_EXPR:
14816 case BIT_NOT_EXPR:
14817 case ADDR_EXPR:
14818 case UNARY_PLUS_EXPR: /* Unary + */
14819 case ALIGNOF_EXPR:
14820 case AT_ENCODE_EXPR:
14821 case ARROW_EXPR:
14822 case THROW_EXPR:
14823 case TYPEID_EXPR:
14824 case REALPART_EXPR:
14825 case IMAGPART_EXPR:
14826 case PAREN_EXPR:
14828 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14829 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14830 return build1 (code, type, op0);
14833 case COMPONENT_REF:
14835 tree object;
14836 tree name;
14838 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14839 name = TREE_OPERAND (t, 1);
14840 if (TREE_CODE (name) == BIT_NOT_EXPR)
14842 name = tsubst_copy (TREE_OPERAND (name, 0), args,
14843 complain, in_decl);
14844 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
14846 else if (TREE_CODE (name) == SCOPE_REF
14847 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
14849 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
14850 complain, in_decl);
14851 name = TREE_OPERAND (name, 1);
14852 name = tsubst_copy (TREE_OPERAND (name, 0), args,
14853 complain, in_decl);
14854 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
14855 name = build_qualified_name (/*type=*/NULL_TREE,
14856 base, name,
14857 /*template_p=*/false);
14859 else if (BASELINK_P (name))
14860 name = tsubst_baselink (name,
14861 non_reference (TREE_TYPE (object)),
14862 args, complain,
14863 in_decl);
14864 else
14865 name = tsubst_copy (name, args, complain, in_decl);
14866 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
14869 case PLUS_EXPR:
14870 case MINUS_EXPR:
14871 case MULT_EXPR:
14872 case TRUNC_DIV_EXPR:
14873 case CEIL_DIV_EXPR:
14874 case FLOOR_DIV_EXPR:
14875 case ROUND_DIV_EXPR:
14876 case EXACT_DIV_EXPR:
14877 case BIT_AND_EXPR:
14878 case BIT_IOR_EXPR:
14879 case BIT_XOR_EXPR:
14880 case TRUNC_MOD_EXPR:
14881 case FLOOR_MOD_EXPR:
14882 case TRUTH_ANDIF_EXPR:
14883 case TRUTH_ORIF_EXPR:
14884 case TRUTH_AND_EXPR:
14885 case TRUTH_OR_EXPR:
14886 case RSHIFT_EXPR:
14887 case LSHIFT_EXPR:
14888 case RROTATE_EXPR:
14889 case LROTATE_EXPR:
14890 case EQ_EXPR:
14891 case NE_EXPR:
14892 case MAX_EXPR:
14893 case MIN_EXPR:
14894 case LE_EXPR:
14895 case GE_EXPR:
14896 case LT_EXPR:
14897 case GT_EXPR:
14898 case COMPOUND_EXPR:
14899 case DOTSTAR_EXPR:
14900 case MEMBER_REF:
14901 case PREDECREMENT_EXPR:
14902 case PREINCREMENT_EXPR:
14903 case POSTDECREMENT_EXPR:
14904 case POSTINCREMENT_EXPR:
14906 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14907 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14908 return build_nt (code, op0, op1);
14911 case SCOPE_REF:
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_qualified_name (/*type=*/NULL_TREE, op0, op1,
14916 QUALIFIED_NAME_IS_TEMPLATE (t));
14919 case ARRAY_REF:
14921 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14922 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14923 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
14926 case CALL_EXPR:
14928 int n = VL_EXP_OPERAND_LENGTH (t);
14929 tree result = build_vl_exp (CALL_EXPR, n);
14930 int i;
14931 for (i = 0; i < n; i++)
14932 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
14933 complain, in_decl);
14934 return result;
14937 case COND_EXPR:
14938 case MODOP_EXPR:
14939 case PSEUDO_DTOR_EXPR:
14940 case VEC_PERM_EXPR:
14942 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14943 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14944 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
14945 r = build_nt (code, op0, op1, op2);
14946 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14947 return r;
14950 case NEW_EXPR:
14952 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14953 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14954 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
14955 r = build_nt (code, op0, op1, op2);
14956 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
14957 return r;
14960 case DELETE_EXPR:
14962 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14963 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14964 r = build_nt (code, op0, op1);
14965 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
14966 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
14967 return r;
14970 case TEMPLATE_ID_EXPR:
14972 /* Substituted template arguments */
14973 tree fn = TREE_OPERAND (t, 0);
14974 tree targs = TREE_OPERAND (t, 1);
14976 fn = tsubst_copy (fn, args, complain, in_decl);
14977 if (targs)
14978 targs = tsubst_template_args (targs, args, complain, in_decl);
14980 return lookup_template_function (fn, targs);
14983 case TREE_LIST:
14985 tree purpose, value, chain;
14987 if (t == void_list_node)
14988 return t;
14990 purpose = TREE_PURPOSE (t);
14991 if (purpose)
14992 purpose = tsubst_copy (purpose, args, complain, in_decl);
14993 value = TREE_VALUE (t);
14994 if (value)
14995 value = tsubst_copy (value, args, complain, in_decl);
14996 chain = TREE_CHAIN (t);
14997 if (chain && chain != void_type_node)
14998 chain = tsubst_copy (chain, args, complain, in_decl);
14999 if (purpose == TREE_PURPOSE (t)
15000 && value == TREE_VALUE (t)
15001 && chain == TREE_CHAIN (t))
15002 return t;
15003 return tree_cons (purpose, value, chain);
15006 case RECORD_TYPE:
15007 case UNION_TYPE:
15008 case ENUMERAL_TYPE:
15009 case INTEGER_TYPE:
15010 case TEMPLATE_TYPE_PARM:
15011 case TEMPLATE_TEMPLATE_PARM:
15012 case BOUND_TEMPLATE_TEMPLATE_PARM:
15013 case TEMPLATE_PARM_INDEX:
15014 case POINTER_TYPE:
15015 case REFERENCE_TYPE:
15016 case OFFSET_TYPE:
15017 case FUNCTION_TYPE:
15018 case METHOD_TYPE:
15019 case ARRAY_TYPE:
15020 case TYPENAME_TYPE:
15021 case UNBOUND_CLASS_TEMPLATE:
15022 case TYPEOF_TYPE:
15023 case DECLTYPE_TYPE:
15024 case TYPE_DECL:
15025 return tsubst (t, args, complain, in_decl);
15027 case USING_DECL:
15028 t = DECL_NAME (t);
15029 /* Fall through. */
15030 case IDENTIFIER_NODE:
15031 if (IDENTIFIER_TYPENAME_P (t))
15033 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15034 return mangle_conv_op_name_for_type (new_type);
15036 else
15037 return t;
15039 case CONSTRUCTOR:
15040 /* This is handled by tsubst_copy_and_build. */
15041 gcc_unreachable ();
15043 case VA_ARG_EXPR:
15045 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15046 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15047 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
15050 case CLEANUP_POINT_EXPR:
15051 /* We shouldn't have built any of these during initial template
15052 generation. Instead, they should be built during instantiation
15053 in response to the saved STMT_IS_FULL_EXPR_P setting. */
15054 gcc_unreachable ();
15056 case OFFSET_REF:
15058 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15059 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15060 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15061 r = build2 (code, type, op0, op1);
15062 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
15063 if (!mark_used (TREE_OPERAND (r, 1), complain)
15064 && !(complain & tf_error))
15065 return error_mark_node;
15066 return r;
15069 case EXPR_PACK_EXPANSION:
15070 error ("invalid use of pack expansion expression");
15071 return error_mark_node;
15073 case NONTYPE_ARGUMENT_PACK:
15074 error ("use %<...%> to expand argument pack");
15075 return error_mark_node;
15077 case VOID_CST:
15078 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
15079 return t;
15081 case INTEGER_CST:
15082 case REAL_CST:
15083 case STRING_CST:
15084 case COMPLEX_CST:
15086 /* Instantiate any typedefs in the type. */
15087 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15088 r = fold_convert (type, t);
15089 gcc_assert (TREE_CODE (r) == code);
15090 return r;
15093 case PTRMEM_CST:
15094 /* These can sometimes show up in a partial instantiation, but never
15095 involve template parms. */
15096 gcc_assert (!uses_template_parms (t));
15097 return t;
15099 case UNARY_LEFT_FOLD_EXPR:
15100 return tsubst_unary_left_fold (t, args, complain, in_decl);
15101 case UNARY_RIGHT_FOLD_EXPR:
15102 return tsubst_unary_right_fold (t, args, complain, in_decl);
15103 case BINARY_LEFT_FOLD_EXPR:
15104 return tsubst_binary_left_fold (t, args, complain, in_decl);
15105 case BINARY_RIGHT_FOLD_EXPR:
15106 return tsubst_binary_right_fold (t, args, complain, in_decl);
15108 default:
15109 /* We shouldn't get here, but keep going if !flag_checking. */
15110 if (flag_checking)
15111 gcc_unreachable ();
15112 return t;
15116 /* Helper function for tsubst_omp_clauses, used for instantiation of
15117 OMP_CLAUSE_DECL of clauses. */
15119 static tree
15120 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
15121 tree in_decl)
15123 if (decl == NULL_TREE)
15124 return NULL_TREE;
15126 /* Handle an OpenMP array section represented as a TREE_LIST (or
15127 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15128 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15129 TREE_LIST. We can handle it exactly the same as an array section
15130 (purpose, value, and a chain), even though the nomenclature
15131 (low_bound, length, etc) is different. */
15132 if (TREE_CODE (decl) == TREE_LIST)
15134 tree low_bound
15135 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
15136 /*integral_constant_expression_p=*/false);
15137 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
15138 /*integral_constant_expression_p=*/false);
15139 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
15140 in_decl);
15141 if (TREE_PURPOSE (decl) == low_bound
15142 && TREE_VALUE (decl) == length
15143 && TREE_CHAIN (decl) == chain)
15144 return decl;
15145 tree ret = tree_cons (low_bound, length, chain);
15146 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
15147 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
15148 return ret;
15150 tree ret = tsubst_expr (decl, args, complain, in_decl,
15151 /*integral_constant_expression_p=*/false);
15152 /* Undo convert_from_reference tsubst_expr could have called. */
15153 if (decl
15154 && REFERENCE_REF_P (ret)
15155 && !REFERENCE_REF_P (decl))
15156 ret = TREE_OPERAND (ret, 0);
15157 return ret;
15160 /* Like tsubst_copy, but specifically for OpenMP clauses. */
15162 static tree
15163 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
15164 tree args, tsubst_flags_t complain, tree in_decl)
15166 tree new_clauses = NULL_TREE, nc, oc;
15167 tree linear_no_step = NULL_TREE;
15169 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
15171 nc = copy_node (oc);
15172 OMP_CLAUSE_CHAIN (nc) = new_clauses;
15173 new_clauses = nc;
15175 switch (OMP_CLAUSE_CODE (nc))
15177 case OMP_CLAUSE_LASTPRIVATE:
15178 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
15180 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
15181 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
15182 in_decl, /*integral_constant_expression_p=*/false);
15183 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
15184 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
15186 /* FALLTHRU */
15187 case OMP_CLAUSE_PRIVATE:
15188 case OMP_CLAUSE_SHARED:
15189 case OMP_CLAUSE_FIRSTPRIVATE:
15190 case OMP_CLAUSE_COPYIN:
15191 case OMP_CLAUSE_COPYPRIVATE:
15192 case OMP_CLAUSE_UNIFORM:
15193 case OMP_CLAUSE_DEPEND:
15194 case OMP_CLAUSE_FROM:
15195 case OMP_CLAUSE_TO:
15196 case OMP_CLAUSE_MAP:
15197 case OMP_CLAUSE_USE_DEVICE_PTR:
15198 case OMP_CLAUSE_IS_DEVICE_PTR:
15199 OMP_CLAUSE_DECL (nc)
15200 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15201 in_decl);
15202 break;
15203 case OMP_CLAUSE_TILE:
15204 case OMP_CLAUSE_IF:
15205 case OMP_CLAUSE_NUM_THREADS:
15206 case OMP_CLAUSE_SCHEDULE:
15207 case OMP_CLAUSE_COLLAPSE:
15208 case OMP_CLAUSE_FINAL:
15209 case OMP_CLAUSE_DEVICE:
15210 case OMP_CLAUSE_DIST_SCHEDULE:
15211 case OMP_CLAUSE_NUM_TEAMS:
15212 case OMP_CLAUSE_THREAD_LIMIT:
15213 case OMP_CLAUSE_SAFELEN:
15214 case OMP_CLAUSE_SIMDLEN:
15215 case OMP_CLAUSE_NUM_TASKS:
15216 case OMP_CLAUSE_GRAINSIZE:
15217 case OMP_CLAUSE_PRIORITY:
15218 case OMP_CLAUSE_ORDERED:
15219 case OMP_CLAUSE_HINT:
15220 case OMP_CLAUSE_NUM_GANGS:
15221 case OMP_CLAUSE_NUM_WORKERS:
15222 case OMP_CLAUSE_VECTOR_LENGTH:
15223 case OMP_CLAUSE_WORKER:
15224 case OMP_CLAUSE_VECTOR:
15225 case OMP_CLAUSE_ASYNC:
15226 case OMP_CLAUSE_WAIT:
15227 OMP_CLAUSE_OPERAND (nc, 0)
15228 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
15229 in_decl, /*integral_constant_expression_p=*/false);
15230 break;
15231 case OMP_CLAUSE_REDUCTION:
15232 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
15234 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
15235 if (TREE_CODE (placeholder) == SCOPE_REF)
15237 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
15238 complain, in_decl);
15239 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
15240 = build_qualified_name (NULL_TREE, scope,
15241 TREE_OPERAND (placeholder, 1),
15242 false);
15244 else
15245 gcc_assert (identifier_p (placeholder));
15247 OMP_CLAUSE_DECL (nc)
15248 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15249 in_decl);
15250 break;
15251 case OMP_CLAUSE_GANG:
15252 case OMP_CLAUSE_ALIGNED:
15253 OMP_CLAUSE_DECL (nc)
15254 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15255 in_decl);
15256 OMP_CLAUSE_OPERAND (nc, 1)
15257 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
15258 in_decl, /*integral_constant_expression_p=*/false);
15259 break;
15260 case OMP_CLAUSE_LINEAR:
15261 OMP_CLAUSE_DECL (nc)
15262 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15263 in_decl);
15264 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
15266 gcc_assert (!linear_no_step);
15267 linear_no_step = nc;
15269 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
15270 OMP_CLAUSE_LINEAR_STEP (nc)
15271 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
15272 complain, in_decl);
15273 else
15274 OMP_CLAUSE_LINEAR_STEP (nc)
15275 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
15276 in_decl,
15277 /*integral_constant_expression_p=*/false);
15278 break;
15279 case OMP_CLAUSE_NOWAIT:
15280 case OMP_CLAUSE_DEFAULT:
15281 case OMP_CLAUSE_UNTIED:
15282 case OMP_CLAUSE_MERGEABLE:
15283 case OMP_CLAUSE_INBRANCH:
15284 case OMP_CLAUSE_NOTINBRANCH:
15285 case OMP_CLAUSE_PROC_BIND:
15286 case OMP_CLAUSE_FOR:
15287 case OMP_CLAUSE_PARALLEL:
15288 case OMP_CLAUSE_SECTIONS:
15289 case OMP_CLAUSE_TASKGROUP:
15290 case OMP_CLAUSE_NOGROUP:
15291 case OMP_CLAUSE_THREADS:
15292 case OMP_CLAUSE_SIMD:
15293 case OMP_CLAUSE_DEFAULTMAP:
15294 case OMP_CLAUSE_INDEPENDENT:
15295 case OMP_CLAUSE_AUTO:
15296 case OMP_CLAUSE_SEQ:
15297 break;
15298 default:
15299 gcc_unreachable ();
15301 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
15302 switch (OMP_CLAUSE_CODE (nc))
15304 case OMP_CLAUSE_SHARED:
15305 case OMP_CLAUSE_PRIVATE:
15306 case OMP_CLAUSE_FIRSTPRIVATE:
15307 case OMP_CLAUSE_LASTPRIVATE:
15308 case OMP_CLAUSE_COPYPRIVATE:
15309 case OMP_CLAUSE_LINEAR:
15310 case OMP_CLAUSE_REDUCTION:
15311 case OMP_CLAUSE_USE_DEVICE_PTR:
15312 case OMP_CLAUSE_IS_DEVICE_PTR:
15313 /* tsubst_expr on SCOPE_REF results in returning
15314 finish_non_static_data_member result. Undo that here. */
15315 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
15316 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
15317 == IDENTIFIER_NODE))
15319 tree t = OMP_CLAUSE_DECL (nc);
15320 tree v = t;
15321 while (v)
15322 switch (TREE_CODE (v))
15324 case COMPONENT_REF:
15325 case MEM_REF:
15326 case INDIRECT_REF:
15327 CASE_CONVERT:
15328 case POINTER_PLUS_EXPR:
15329 v = TREE_OPERAND (v, 0);
15330 continue;
15331 case PARM_DECL:
15332 if (DECL_CONTEXT (v) == current_function_decl
15333 && DECL_ARTIFICIAL (v)
15334 && DECL_NAME (v) == this_identifier)
15335 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
15336 /* FALLTHRU */
15337 default:
15338 v = NULL_TREE;
15339 break;
15342 else if (VAR_P (OMP_CLAUSE_DECL (oc))
15343 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
15344 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
15345 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
15346 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
15348 tree decl = OMP_CLAUSE_DECL (nc);
15349 if (VAR_P (decl))
15351 retrofit_lang_decl (decl);
15352 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
15355 break;
15356 default:
15357 break;
15361 new_clauses = nreverse (new_clauses);
15362 if (ort != C_ORT_OMP_DECLARE_SIMD)
15364 new_clauses = finish_omp_clauses (new_clauses, ort);
15365 if (linear_no_step)
15366 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
15367 if (nc == linear_no_step)
15369 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
15370 break;
15373 return new_clauses;
15376 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
15378 static tree
15379 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
15380 tree in_decl)
15382 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
15384 tree purpose, value, chain;
15386 if (t == NULL)
15387 return t;
15389 if (TREE_CODE (t) != TREE_LIST)
15390 return tsubst_copy_and_build (t, args, complain, in_decl,
15391 /*function_p=*/false,
15392 /*integral_constant_expression_p=*/false);
15394 if (t == void_list_node)
15395 return t;
15397 purpose = TREE_PURPOSE (t);
15398 if (purpose)
15399 purpose = RECUR (purpose);
15400 value = TREE_VALUE (t);
15401 if (value)
15403 if (TREE_CODE (value) != LABEL_DECL)
15404 value = RECUR (value);
15405 else
15407 value = lookup_label (DECL_NAME (value));
15408 gcc_assert (TREE_CODE (value) == LABEL_DECL);
15409 TREE_USED (value) = 1;
15412 chain = TREE_CHAIN (t);
15413 if (chain && chain != void_type_node)
15414 chain = RECUR (chain);
15415 return tree_cons (purpose, value, chain);
15416 #undef RECUR
15419 /* Used to temporarily communicate the list of #pragma omp parallel
15420 clauses to #pragma omp for instantiation if they are combined
15421 together. */
15423 static tree *omp_parallel_combined_clauses;
15425 /* Substitute one OMP_FOR iterator. */
15427 static void
15428 tsubst_omp_for_iterator (tree t, int i, tree declv, tree orig_declv,
15429 tree initv, tree condv, tree incrv, tree *clauses,
15430 tree args, tsubst_flags_t complain, tree in_decl,
15431 bool integral_constant_expression_p)
15433 #define RECUR(NODE) \
15434 tsubst_expr ((NODE), args, complain, in_decl, \
15435 integral_constant_expression_p)
15436 tree decl, init, cond, incr;
15438 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
15439 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
15441 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
15443 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
15444 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
15447 decl = TREE_OPERAND (init, 0);
15448 init = TREE_OPERAND (init, 1);
15449 tree decl_expr = NULL_TREE;
15450 if (init && TREE_CODE (init) == DECL_EXPR)
15452 /* We need to jump through some hoops to handle declarations in the
15453 init-statement, since we might need to handle auto deduction,
15454 but we need to keep control of initialization. */
15455 decl_expr = init;
15456 init = DECL_INITIAL (DECL_EXPR_DECL (init));
15457 decl = tsubst_decl (decl, args, complain);
15459 else
15461 if (TREE_CODE (decl) == SCOPE_REF)
15463 decl = RECUR (decl);
15464 if (TREE_CODE (decl) == COMPONENT_REF)
15466 tree v = decl;
15467 while (v)
15468 switch (TREE_CODE (v))
15470 case COMPONENT_REF:
15471 case MEM_REF:
15472 case INDIRECT_REF:
15473 CASE_CONVERT:
15474 case POINTER_PLUS_EXPR:
15475 v = TREE_OPERAND (v, 0);
15476 continue;
15477 case PARM_DECL:
15478 if (DECL_CONTEXT (v) == current_function_decl
15479 && DECL_ARTIFICIAL (v)
15480 && DECL_NAME (v) == this_identifier)
15482 decl = TREE_OPERAND (decl, 1);
15483 decl = omp_privatize_field (decl, false);
15485 /* FALLTHRU */
15486 default:
15487 v = NULL_TREE;
15488 break;
15492 else
15493 decl = RECUR (decl);
15495 init = RECUR (init);
15497 tree auto_node = type_uses_auto (TREE_TYPE (decl));
15498 if (auto_node && init)
15499 TREE_TYPE (decl)
15500 = do_auto_deduction (TREE_TYPE (decl), init, auto_node);
15502 gcc_assert (!type_dependent_expression_p (decl));
15504 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15506 if (decl_expr)
15508 /* Declare the variable, but don't let that initialize it. */
15509 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
15510 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
15511 RECUR (decl_expr);
15512 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
15515 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
15516 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15517 if (TREE_CODE (incr) == MODIFY_EXPR)
15519 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15520 tree rhs = RECUR (TREE_OPERAND (incr, 1));
15521 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
15522 NOP_EXPR, rhs, complain);
15524 else
15525 incr = RECUR (incr);
15526 TREE_VEC_ELT (declv, i) = decl;
15527 TREE_VEC_ELT (initv, i) = init;
15528 TREE_VEC_ELT (condv, i) = cond;
15529 TREE_VEC_ELT (incrv, i) = incr;
15530 return;
15533 if (decl_expr)
15535 /* Declare and initialize the variable. */
15536 RECUR (decl_expr);
15537 init = NULL_TREE;
15539 else if (init)
15541 tree *pc;
15542 int j;
15543 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
15545 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
15547 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
15548 && OMP_CLAUSE_DECL (*pc) == decl)
15549 break;
15550 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
15551 && OMP_CLAUSE_DECL (*pc) == decl)
15553 if (j)
15554 break;
15555 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15556 tree c = *pc;
15557 *pc = OMP_CLAUSE_CHAIN (c);
15558 OMP_CLAUSE_CHAIN (c) = *clauses;
15559 *clauses = c;
15561 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
15562 && OMP_CLAUSE_DECL (*pc) == decl)
15564 error ("iteration variable %qD should not be firstprivate",
15565 decl);
15566 *pc = OMP_CLAUSE_CHAIN (*pc);
15568 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
15569 && OMP_CLAUSE_DECL (*pc) == decl)
15571 error ("iteration variable %qD should not be reduction",
15572 decl);
15573 *pc = OMP_CLAUSE_CHAIN (*pc);
15575 else
15576 pc = &OMP_CLAUSE_CHAIN (*pc);
15578 if (*pc)
15579 break;
15581 if (*pc == NULL_TREE)
15583 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
15584 OMP_CLAUSE_DECL (c) = decl;
15585 c = finish_omp_clauses (c, C_ORT_OMP);
15586 if (c)
15588 OMP_CLAUSE_CHAIN (c) = *clauses;
15589 *clauses = c;
15593 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
15594 if (COMPARISON_CLASS_P (cond))
15596 tree op0 = RECUR (TREE_OPERAND (cond, 0));
15597 tree op1 = RECUR (TREE_OPERAND (cond, 1));
15598 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
15600 else
15601 cond = RECUR (cond);
15602 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15603 switch (TREE_CODE (incr))
15605 case PREINCREMENT_EXPR:
15606 case PREDECREMENT_EXPR:
15607 case POSTINCREMENT_EXPR:
15608 case POSTDECREMENT_EXPR:
15609 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
15610 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
15611 break;
15612 case MODIFY_EXPR:
15613 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15614 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15616 tree rhs = TREE_OPERAND (incr, 1);
15617 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15618 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15619 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15620 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15621 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15622 rhs0, rhs1));
15624 else
15625 incr = RECUR (incr);
15626 break;
15627 case MODOP_EXPR:
15628 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15629 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15631 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15632 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15633 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
15634 TREE_TYPE (decl), lhs,
15635 RECUR (TREE_OPERAND (incr, 2))));
15637 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
15638 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
15639 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
15641 tree rhs = TREE_OPERAND (incr, 2);
15642 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15643 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15644 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15645 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15646 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15647 rhs0, rhs1));
15649 else
15650 incr = RECUR (incr);
15651 break;
15652 default:
15653 incr = RECUR (incr);
15654 break;
15657 TREE_VEC_ELT (declv, i) = decl;
15658 TREE_VEC_ELT (initv, i) = init;
15659 TREE_VEC_ELT (condv, i) = cond;
15660 TREE_VEC_ELT (incrv, i) = incr;
15661 #undef RECUR
15664 /* Helper function of tsubst_expr, find OMP_TEAMS inside
15665 of OMP_TARGET's body. */
15667 static tree
15668 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
15670 *walk_subtrees = 0;
15671 switch (TREE_CODE (*tp))
15673 case OMP_TEAMS:
15674 return *tp;
15675 case BIND_EXPR:
15676 case STATEMENT_LIST:
15677 *walk_subtrees = 1;
15678 break;
15679 default:
15680 break;
15682 return NULL_TREE;
15685 /* Helper function for tsubst_expr. For decomposition declaration
15686 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
15687 also the corresponding decls representing the identifiers
15688 of the decomposition declaration. Return DECL if successful
15689 or error_mark_node otherwise, set *FIRST to the first decl
15690 in the list chained through DECL_CHAIN and *CNT to the number
15691 of such decls. */
15693 static tree
15694 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
15695 tsubst_flags_t complain, tree in_decl, tree *first,
15696 unsigned int *cnt)
15698 tree decl2, decl3, prev = decl;
15699 *cnt = 0;
15700 gcc_assert (DECL_NAME (decl) == NULL_TREE);
15701 for (decl2 = DECL_CHAIN (pattern_decl);
15702 decl2
15703 && VAR_P (decl2)
15704 && DECL_DECOMPOSITION_P (decl2)
15705 && DECL_NAME (decl2);
15706 decl2 = DECL_CHAIN (decl2))
15708 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
15710 gcc_assert (errorcount);
15711 return error_mark_node;
15713 (*cnt)++;
15714 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
15715 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
15716 tree v = DECL_VALUE_EXPR (decl2);
15717 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
15718 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
15719 decl3 = tsubst (decl2, args, complain, in_decl);
15720 SET_DECL_VALUE_EXPR (decl2, v);
15721 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
15722 if (VAR_P (decl3))
15723 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
15724 maybe_push_decl (decl3);
15725 if (error_operand_p (decl3))
15726 decl = error_mark_node;
15727 else if (decl != error_mark_node
15728 && DECL_CHAIN (decl3) != prev)
15730 gcc_assert (errorcount);
15731 decl = error_mark_node;
15733 else
15734 prev = decl3;
15736 *first = prev;
15737 return decl;
15740 /* Like tsubst_copy for expressions, etc. but also does semantic
15741 processing. */
15743 tree
15744 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
15745 bool integral_constant_expression_p)
15747 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
15748 #define RECUR(NODE) \
15749 tsubst_expr ((NODE), args, complain, in_decl, \
15750 integral_constant_expression_p)
15752 tree stmt, tmp;
15753 tree r;
15754 location_t loc;
15756 if (t == NULL_TREE || t == error_mark_node)
15757 return t;
15759 loc = input_location;
15760 if (EXPR_HAS_LOCATION (t))
15761 input_location = EXPR_LOCATION (t);
15762 if (STATEMENT_CODE_P (TREE_CODE (t)))
15763 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
15765 switch (TREE_CODE (t))
15767 case STATEMENT_LIST:
15769 tree_stmt_iterator i;
15770 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
15771 RECUR (tsi_stmt (i));
15772 break;
15775 case CTOR_INITIALIZER:
15776 finish_mem_initializers (tsubst_initializer_list
15777 (TREE_OPERAND (t, 0), args));
15778 break;
15780 case RETURN_EXPR:
15781 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
15782 break;
15784 case EXPR_STMT:
15785 tmp = RECUR (EXPR_STMT_EXPR (t));
15786 if (EXPR_STMT_STMT_EXPR_RESULT (t))
15787 finish_stmt_expr_expr (tmp, cur_stmt_expr);
15788 else
15789 finish_expr_stmt (tmp);
15790 break;
15792 case USING_STMT:
15793 finish_local_using_directive (USING_STMT_NAMESPACE (t),
15794 /*attribs=*/NULL_TREE);
15795 break;
15797 case DECL_EXPR:
15799 tree decl, pattern_decl;
15800 tree init;
15802 pattern_decl = decl = DECL_EXPR_DECL (t);
15803 if (TREE_CODE (decl) == LABEL_DECL)
15804 finish_label_decl (DECL_NAME (decl));
15805 else if (TREE_CODE (decl) == USING_DECL)
15807 tree scope = USING_DECL_SCOPE (decl);
15808 tree name = DECL_NAME (decl);
15810 scope = tsubst (scope, args, complain, in_decl);
15811 decl = lookup_qualified_name (scope, name,
15812 /*is_type_p=*/false,
15813 /*complain=*/false);
15814 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
15815 qualified_name_lookup_error (scope, name, decl, input_location);
15816 else
15817 finish_local_using_decl (decl, scope, name);
15819 else if (DECL_PACK_P (decl))
15821 /* Don't build up decls for a variadic capture proxy, we'll
15822 instantiate the elements directly as needed. */
15823 break;
15825 else
15827 init = DECL_INITIAL (decl);
15828 decl = tsubst (decl, args, complain, in_decl);
15829 if (decl != error_mark_node)
15831 /* By marking the declaration as instantiated, we avoid
15832 trying to instantiate it. Since instantiate_decl can't
15833 handle local variables, and since we've already done
15834 all that needs to be done, that's the right thing to
15835 do. */
15836 if (VAR_P (decl))
15837 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
15838 if (VAR_P (decl)
15839 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
15840 /* Anonymous aggregates are a special case. */
15841 finish_anon_union (decl);
15842 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
15844 DECL_CONTEXT (decl) = current_function_decl;
15845 if (DECL_NAME (decl) == this_identifier)
15847 tree lam = DECL_CONTEXT (current_function_decl);
15848 lam = CLASSTYPE_LAMBDA_EXPR (lam);
15849 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
15851 insert_capture_proxy (decl);
15853 else if (DECL_IMPLICIT_TYPEDEF_P (t))
15854 /* We already did a pushtag. */;
15855 else if (TREE_CODE (decl) == FUNCTION_DECL
15856 && DECL_OMP_DECLARE_REDUCTION_P (decl)
15857 && DECL_FUNCTION_SCOPE_P (pattern_decl))
15859 DECL_CONTEXT (decl) = NULL_TREE;
15860 pushdecl (decl);
15861 DECL_CONTEXT (decl) = current_function_decl;
15862 cp_check_omp_declare_reduction (decl);
15864 else
15866 int const_init = false;
15867 maybe_push_decl (decl);
15868 if (VAR_P (decl)
15869 && DECL_PRETTY_FUNCTION_P (decl))
15871 /* For __PRETTY_FUNCTION__ we have to adjust the
15872 initializer. */
15873 const char *const name
15874 = cxx_printable_name (current_function_decl, 2);
15875 init = cp_fname_init (name, &TREE_TYPE (decl));
15877 else
15878 init = tsubst_init (init, decl, args, complain, in_decl);
15880 if (VAR_P (decl))
15881 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
15882 (pattern_decl));
15883 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
15884 if (VAR_P (decl)
15885 && DECL_DECOMPOSITION_P (decl)
15886 && TREE_TYPE (pattern_decl) != error_mark_node)
15888 unsigned int cnt;
15889 tree first;
15890 decl = tsubst_decomp_names (decl, pattern_decl, args,
15891 complain, in_decl, &first,
15892 &cnt);
15893 if (decl != error_mark_node)
15894 cp_finish_decomp (decl, first, cnt);
15900 break;
15903 case FOR_STMT:
15904 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
15905 RECUR (FOR_INIT_STMT (t));
15906 finish_init_stmt (stmt);
15907 tmp = RECUR (FOR_COND (t));
15908 finish_for_cond (tmp, stmt, false);
15909 tmp = RECUR (FOR_EXPR (t));
15910 finish_for_expr (tmp, stmt);
15911 RECUR (FOR_BODY (t));
15912 finish_for_stmt (stmt);
15913 break;
15915 case RANGE_FOR_STMT:
15917 tree decl, expr;
15918 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
15919 decl = RANGE_FOR_DECL (t);
15920 decl = tsubst (decl, args, complain, in_decl);
15921 maybe_push_decl (decl);
15922 expr = RECUR (RANGE_FOR_EXPR (t));
15923 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
15925 unsigned int cnt;
15926 tree first;
15927 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
15928 complain, in_decl, &first, &cnt);
15929 stmt = cp_convert_range_for (stmt, decl, expr, first, cnt,
15930 RANGE_FOR_IVDEP (t));
15932 else
15933 stmt = cp_convert_range_for (stmt, decl, expr, NULL_TREE, 0,
15934 RANGE_FOR_IVDEP (t));
15935 RECUR (RANGE_FOR_BODY (t));
15936 finish_for_stmt (stmt);
15938 break;
15940 case WHILE_STMT:
15941 stmt = begin_while_stmt ();
15942 tmp = RECUR (WHILE_COND (t));
15943 finish_while_stmt_cond (tmp, stmt, false);
15944 RECUR (WHILE_BODY (t));
15945 finish_while_stmt (stmt);
15946 break;
15948 case DO_STMT:
15949 stmt = begin_do_stmt ();
15950 RECUR (DO_BODY (t));
15951 finish_do_body (stmt);
15952 tmp = RECUR (DO_COND (t));
15953 finish_do_stmt (tmp, stmt, false);
15954 break;
15956 case IF_STMT:
15957 stmt = begin_if_stmt ();
15958 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
15959 tmp = RECUR (IF_COND (t));
15960 tmp = finish_if_stmt_cond (tmp, stmt);
15961 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
15962 /* Don't instantiate the THEN_CLAUSE. */;
15963 else
15965 bool inhibit = integer_zerop (fold_non_dependent_expr (tmp));
15966 if (inhibit)
15967 ++c_inhibit_evaluation_warnings;
15968 RECUR (THEN_CLAUSE (t));
15969 if (inhibit)
15970 --c_inhibit_evaluation_warnings;
15972 finish_then_clause (stmt);
15974 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
15975 /* Don't instantiate the ELSE_CLAUSE. */;
15976 else if (ELSE_CLAUSE (t))
15978 bool inhibit = integer_nonzerop (fold_non_dependent_expr (tmp));
15979 begin_else_clause (stmt);
15980 if (inhibit)
15981 ++c_inhibit_evaluation_warnings;
15982 RECUR (ELSE_CLAUSE (t));
15983 if (inhibit)
15984 --c_inhibit_evaluation_warnings;
15985 finish_else_clause (stmt);
15988 finish_if_stmt (stmt);
15989 break;
15991 case BIND_EXPR:
15992 if (BIND_EXPR_BODY_BLOCK (t))
15993 stmt = begin_function_body ();
15994 else
15995 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
15996 ? BCS_TRY_BLOCK : 0);
15998 RECUR (BIND_EXPR_BODY (t));
16000 if (BIND_EXPR_BODY_BLOCK (t))
16001 finish_function_body (stmt);
16002 else
16003 finish_compound_stmt (stmt);
16004 break;
16006 case BREAK_STMT:
16007 finish_break_stmt ();
16008 break;
16010 case CONTINUE_STMT:
16011 finish_continue_stmt ();
16012 break;
16014 case SWITCH_STMT:
16015 stmt = begin_switch_stmt ();
16016 tmp = RECUR (SWITCH_STMT_COND (t));
16017 finish_switch_cond (tmp, stmt);
16018 RECUR (SWITCH_STMT_BODY (t));
16019 finish_switch_stmt (stmt);
16020 break;
16022 case CASE_LABEL_EXPR:
16024 tree low = RECUR (CASE_LOW (t));
16025 tree high = RECUR (CASE_HIGH (t));
16026 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
16027 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
16028 FALLTHROUGH_LABEL_P (CASE_LABEL (l))
16029 = FALLTHROUGH_LABEL_P (CASE_LABEL (t));
16031 break;
16033 case LABEL_EXPR:
16035 tree decl = LABEL_EXPR_LABEL (t);
16036 tree label;
16038 label = finish_label_stmt (DECL_NAME (decl));
16039 if (TREE_CODE (label) == LABEL_DECL)
16040 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
16041 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
16042 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
16044 break;
16046 case GOTO_EXPR:
16047 tmp = GOTO_DESTINATION (t);
16048 if (TREE_CODE (tmp) != LABEL_DECL)
16049 /* Computed goto's must be tsubst'd into. On the other hand,
16050 non-computed gotos must not be; the identifier in question
16051 will have no binding. */
16052 tmp = RECUR (tmp);
16053 else
16054 tmp = DECL_NAME (tmp);
16055 finish_goto_stmt (tmp);
16056 break;
16058 case ASM_EXPR:
16060 tree string = RECUR (ASM_STRING (t));
16061 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
16062 complain, in_decl);
16063 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
16064 complain, in_decl);
16065 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
16066 complain, in_decl);
16067 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
16068 complain, in_decl);
16069 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
16070 clobbers, labels);
16071 tree asm_expr = tmp;
16072 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
16073 asm_expr = TREE_OPERAND (asm_expr, 0);
16074 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
16076 break;
16078 case TRY_BLOCK:
16079 if (CLEANUP_P (t))
16081 stmt = begin_try_block ();
16082 RECUR (TRY_STMTS (t));
16083 finish_cleanup_try_block (stmt);
16084 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
16086 else
16088 tree compound_stmt = NULL_TREE;
16090 if (FN_TRY_BLOCK_P (t))
16091 stmt = begin_function_try_block (&compound_stmt);
16092 else
16093 stmt = begin_try_block ();
16095 RECUR (TRY_STMTS (t));
16097 if (FN_TRY_BLOCK_P (t))
16098 finish_function_try_block (stmt);
16099 else
16100 finish_try_block (stmt);
16102 RECUR (TRY_HANDLERS (t));
16103 if (FN_TRY_BLOCK_P (t))
16104 finish_function_handler_sequence (stmt, compound_stmt);
16105 else
16106 finish_handler_sequence (stmt);
16108 break;
16110 case HANDLER:
16112 tree decl = HANDLER_PARMS (t);
16114 if (decl)
16116 decl = tsubst (decl, args, complain, in_decl);
16117 /* Prevent instantiate_decl from trying to instantiate
16118 this variable. We've already done all that needs to be
16119 done. */
16120 if (decl != error_mark_node)
16121 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16123 stmt = begin_handler ();
16124 finish_handler_parms (decl, stmt);
16125 RECUR (HANDLER_BODY (t));
16126 finish_handler (stmt);
16128 break;
16130 case TAG_DEFN:
16131 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
16132 if (CLASS_TYPE_P (tmp))
16134 /* Local classes are not independent templates; they are
16135 instantiated along with their containing function. And this
16136 way we don't have to deal with pushing out of one local class
16137 to instantiate a member of another local class. */
16138 tree fn;
16139 /* Closures are handled by the LAMBDA_EXPR. */
16140 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
16141 complete_type (tmp);
16142 for (fn = TYPE_METHODS (tmp); fn; fn = DECL_CHAIN (fn))
16143 if (!DECL_ARTIFICIAL (fn))
16144 instantiate_decl (fn, /*defer_ok=*/false,
16145 /*expl_inst_class=*/false);
16147 break;
16149 case STATIC_ASSERT:
16151 tree condition;
16153 ++c_inhibit_evaluation_warnings;
16154 condition =
16155 tsubst_expr (STATIC_ASSERT_CONDITION (t),
16156 args,
16157 complain, in_decl,
16158 /*integral_constant_expression_p=*/true);
16159 --c_inhibit_evaluation_warnings;
16161 finish_static_assert (condition,
16162 STATIC_ASSERT_MESSAGE (t),
16163 STATIC_ASSERT_SOURCE_LOCATION (t),
16164 /*member_p=*/false);
16166 break;
16168 case OACC_KERNELS:
16169 case OACC_PARALLEL:
16170 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
16171 in_decl);
16172 stmt = begin_omp_parallel ();
16173 RECUR (OMP_BODY (t));
16174 finish_omp_construct (TREE_CODE (t), stmt, tmp);
16175 break;
16177 case OMP_PARALLEL:
16178 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
16179 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
16180 complain, in_decl);
16181 if (OMP_PARALLEL_COMBINED (t))
16182 omp_parallel_combined_clauses = &tmp;
16183 stmt = begin_omp_parallel ();
16184 RECUR (OMP_PARALLEL_BODY (t));
16185 gcc_assert (omp_parallel_combined_clauses == NULL);
16186 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
16187 = OMP_PARALLEL_COMBINED (t);
16188 pop_omp_privatization_clauses (r);
16189 break;
16191 case OMP_TASK:
16192 r = push_omp_privatization_clauses (false);
16193 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
16194 complain, in_decl);
16195 stmt = begin_omp_task ();
16196 RECUR (OMP_TASK_BODY (t));
16197 finish_omp_task (tmp, stmt);
16198 pop_omp_privatization_clauses (r);
16199 break;
16201 case OMP_FOR:
16202 case OMP_SIMD:
16203 case CILK_SIMD:
16204 case CILK_FOR:
16205 case OMP_DISTRIBUTE:
16206 case OMP_TASKLOOP:
16207 case OACC_LOOP:
16209 tree clauses, body, pre_body;
16210 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
16211 tree orig_declv = NULL_TREE;
16212 tree incrv = NULL_TREE;
16213 enum c_omp_region_type ort = C_ORT_OMP;
16214 int i;
16216 if (TREE_CODE (t) == CILK_SIMD || TREE_CODE (t) == CILK_FOR)
16217 ort = C_ORT_CILK;
16218 else if (TREE_CODE (t) == OACC_LOOP)
16219 ort = C_ORT_ACC;
16221 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
16222 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
16223 in_decl);
16224 if (OMP_FOR_INIT (t) != NULL_TREE)
16226 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16227 if (OMP_FOR_ORIG_DECLS (t))
16228 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16229 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16230 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16231 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16234 stmt = begin_omp_structured_block ();
16236 pre_body = push_stmt_list ();
16237 RECUR (OMP_FOR_PRE_BODY (t));
16238 pre_body = pop_stmt_list (pre_body);
16240 if (OMP_FOR_INIT (t) != NULL_TREE)
16241 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
16242 tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, condv,
16243 incrv, &clauses, args, complain, in_decl,
16244 integral_constant_expression_p);
16245 omp_parallel_combined_clauses = NULL;
16247 body = push_stmt_list ();
16248 RECUR (OMP_FOR_BODY (t));
16249 body = pop_stmt_list (body);
16251 if (OMP_FOR_INIT (t) != NULL_TREE)
16252 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
16253 orig_declv, initv, condv, incrv, body, pre_body,
16254 NULL, clauses);
16255 else
16257 t = make_node (TREE_CODE (t));
16258 TREE_TYPE (t) = void_type_node;
16259 OMP_FOR_BODY (t) = body;
16260 OMP_FOR_PRE_BODY (t) = pre_body;
16261 OMP_FOR_CLAUSES (t) = clauses;
16262 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
16263 add_stmt (t);
16266 add_stmt (finish_omp_structured_block (stmt));
16267 pop_omp_privatization_clauses (r);
16269 break;
16271 case OMP_SECTIONS:
16272 omp_parallel_combined_clauses = NULL;
16273 /* FALLTHRU */
16274 case OMP_SINGLE:
16275 case OMP_TEAMS:
16276 case OMP_CRITICAL:
16277 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
16278 && OMP_TEAMS_COMBINED (t));
16279 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
16280 in_decl);
16281 stmt = push_stmt_list ();
16282 RECUR (OMP_BODY (t));
16283 stmt = pop_stmt_list (stmt);
16285 t = copy_node (t);
16286 OMP_BODY (t) = stmt;
16287 OMP_CLAUSES (t) = tmp;
16288 add_stmt (t);
16289 pop_omp_privatization_clauses (r);
16290 break;
16292 case OACC_DATA:
16293 case OMP_TARGET_DATA:
16294 case OMP_TARGET:
16295 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
16296 ? C_ORT_ACC : C_ORT_OMP, args, complain,
16297 in_decl);
16298 keep_next_level (true);
16299 stmt = begin_omp_structured_block ();
16301 RECUR (OMP_BODY (t));
16302 stmt = finish_omp_structured_block (stmt);
16304 t = copy_node (t);
16305 OMP_BODY (t) = stmt;
16306 OMP_CLAUSES (t) = tmp;
16307 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
16309 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
16310 if (teams)
16312 /* For combined target teams, ensure the num_teams and
16313 thread_limit clause expressions are evaluated on the host,
16314 before entering the target construct. */
16315 tree c;
16316 for (c = OMP_TEAMS_CLAUSES (teams);
16317 c; c = OMP_CLAUSE_CHAIN (c))
16318 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
16319 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
16320 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
16322 tree expr = OMP_CLAUSE_OPERAND (c, 0);
16323 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
16324 if (expr == error_mark_node)
16325 continue;
16326 tmp = TARGET_EXPR_SLOT (expr);
16327 add_stmt (expr);
16328 OMP_CLAUSE_OPERAND (c, 0) = expr;
16329 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16330 OMP_CLAUSE_FIRSTPRIVATE);
16331 OMP_CLAUSE_DECL (tc) = tmp;
16332 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
16333 OMP_TARGET_CLAUSES (t) = tc;
16337 add_stmt (t);
16338 break;
16340 case OACC_DECLARE:
16341 t = copy_node (t);
16342 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
16343 complain, in_decl);
16344 OACC_DECLARE_CLAUSES (t) = tmp;
16345 add_stmt (t);
16346 break;
16348 case OMP_TARGET_UPDATE:
16349 case OMP_TARGET_ENTER_DATA:
16350 case OMP_TARGET_EXIT_DATA:
16351 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
16352 complain, in_decl);
16353 t = copy_node (t);
16354 OMP_STANDALONE_CLAUSES (t) = tmp;
16355 add_stmt (t);
16356 break;
16358 case OACC_ENTER_DATA:
16359 case OACC_EXIT_DATA:
16360 case OACC_UPDATE:
16361 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
16362 complain, in_decl);
16363 t = copy_node (t);
16364 OMP_STANDALONE_CLAUSES (t) = tmp;
16365 add_stmt (t);
16366 break;
16368 case OMP_ORDERED:
16369 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
16370 complain, in_decl);
16371 stmt = push_stmt_list ();
16372 RECUR (OMP_BODY (t));
16373 stmt = pop_stmt_list (stmt);
16375 t = copy_node (t);
16376 OMP_BODY (t) = stmt;
16377 OMP_ORDERED_CLAUSES (t) = tmp;
16378 add_stmt (t);
16379 break;
16381 case OMP_SECTION:
16382 case OMP_MASTER:
16383 case OMP_TASKGROUP:
16384 stmt = push_stmt_list ();
16385 RECUR (OMP_BODY (t));
16386 stmt = pop_stmt_list (stmt);
16388 t = copy_node (t);
16389 OMP_BODY (t) = stmt;
16390 add_stmt (t);
16391 break;
16393 case OMP_ATOMIC:
16394 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
16395 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
16397 tree op1 = TREE_OPERAND (t, 1);
16398 tree rhs1 = NULL_TREE;
16399 tree lhs, rhs;
16400 if (TREE_CODE (op1) == COMPOUND_EXPR)
16402 rhs1 = RECUR (TREE_OPERAND (op1, 0));
16403 op1 = TREE_OPERAND (op1, 1);
16405 lhs = RECUR (TREE_OPERAND (op1, 0));
16406 rhs = RECUR (TREE_OPERAND (op1, 1));
16407 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
16408 NULL_TREE, NULL_TREE, rhs1,
16409 OMP_ATOMIC_SEQ_CST (t));
16411 else
16413 tree op1 = TREE_OPERAND (t, 1);
16414 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
16415 tree rhs1 = NULL_TREE;
16416 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
16417 enum tree_code opcode = NOP_EXPR;
16418 if (code == OMP_ATOMIC_READ)
16420 v = RECUR (TREE_OPERAND (op1, 0));
16421 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16423 else if (code == OMP_ATOMIC_CAPTURE_OLD
16424 || code == OMP_ATOMIC_CAPTURE_NEW)
16426 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
16427 v = RECUR (TREE_OPERAND (op1, 0));
16428 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16429 if (TREE_CODE (op11) == COMPOUND_EXPR)
16431 rhs1 = RECUR (TREE_OPERAND (op11, 0));
16432 op11 = TREE_OPERAND (op11, 1);
16434 lhs = RECUR (TREE_OPERAND (op11, 0));
16435 rhs = RECUR (TREE_OPERAND (op11, 1));
16436 opcode = TREE_CODE (op11);
16437 if (opcode == MODIFY_EXPR)
16438 opcode = NOP_EXPR;
16440 else
16442 code = OMP_ATOMIC;
16443 lhs = RECUR (TREE_OPERAND (op1, 0));
16444 rhs = RECUR (TREE_OPERAND (op1, 1));
16446 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
16447 OMP_ATOMIC_SEQ_CST (t));
16449 break;
16451 case TRANSACTION_EXPR:
16453 int flags = 0;
16454 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
16455 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
16457 if (TRANSACTION_EXPR_IS_STMT (t))
16459 tree body = TRANSACTION_EXPR_BODY (t);
16460 tree noex = NULL_TREE;
16461 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
16463 noex = MUST_NOT_THROW_COND (body);
16464 if (noex == NULL_TREE)
16465 noex = boolean_true_node;
16466 body = TREE_OPERAND (body, 0);
16468 stmt = begin_transaction_stmt (input_location, NULL, flags);
16469 RECUR (body);
16470 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
16472 else
16474 stmt = build_transaction_expr (EXPR_LOCATION (t),
16475 RECUR (TRANSACTION_EXPR_BODY (t)),
16476 flags, NULL_TREE);
16477 RETURN (stmt);
16480 break;
16482 case MUST_NOT_THROW_EXPR:
16484 tree op0 = RECUR (TREE_OPERAND (t, 0));
16485 tree cond = RECUR (MUST_NOT_THROW_COND (t));
16486 RETURN (build_must_not_throw_expr (op0, cond));
16489 case EXPR_PACK_EXPANSION:
16490 error ("invalid use of pack expansion expression");
16491 RETURN (error_mark_node);
16493 case NONTYPE_ARGUMENT_PACK:
16494 error ("use %<...%> to expand argument pack");
16495 RETURN (error_mark_node);
16497 case CILK_SPAWN_STMT:
16498 cfun->calls_cilk_spawn = 1;
16499 RETURN (build_cilk_spawn (EXPR_LOCATION (t), RECUR (CILK_SPAWN_FN (t))));
16501 case CILK_SYNC_STMT:
16502 RETURN (build_cilk_sync ());
16504 case COMPOUND_EXPR:
16505 tmp = RECUR (TREE_OPERAND (t, 0));
16506 if (tmp == NULL_TREE)
16507 /* If the first operand was a statement, we're done with it. */
16508 RETURN (RECUR (TREE_OPERAND (t, 1)));
16509 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
16510 RECUR (TREE_OPERAND (t, 1)),
16511 complain));
16513 case ANNOTATE_EXPR:
16514 tmp = RECUR (TREE_OPERAND (t, 0));
16515 RETURN (build2_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
16516 TREE_TYPE (tmp), tmp, RECUR (TREE_OPERAND (t, 1))));
16518 default:
16519 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
16521 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
16522 /*function_p=*/false,
16523 integral_constant_expression_p));
16526 RETURN (NULL_TREE);
16527 out:
16528 input_location = loc;
16529 return r;
16530 #undef RECUR
16531 #undef RETURN
16534 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
16535 function. For description of the body see comment above
16536 cp_parser_omp_declare_reduction_exprs. */
16538 static void
16539 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16541 if (t == NULL_TREE || t == error_mark_node)
16542 return;
16544 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
16546 tree_stmt_iterator tsi;
16547 int i;
16548 tree stmts[7];
16549 memset (stmts, 0, sizeof stmts);
16550 for (i = 0, tsi = tsi_start (t);
16551 i < 7 && !tsi_end_p (tsi);
16552 i++, tsi_next (&tsi))
16553 stmts[i] = tsi_stmt (tsi);
16554 gcc_assert (tsi_end_p (tsi));
16556 if (i >= 3)
16558 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
16559 && TREE_CODE (stmts[1]) == DECL_EXPR);
16560 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
16561 args, complain, in_decl);
16562 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
16563 args, complain, in_decl);
16564 DECL_CONTEXT (omp_out) = current_function_decl;
16565 DECL_CONTEXT (omp_in) = current_function_decl;
16566 keep_next_level (true);
16567 tree block = begin_omp_structured_block ();
16568 tsubst_expr (stmts[2], args, complain, in_decl, false);
16569 block = finish_omp_structured_block (block);
16570 block = maybe_cleanup_point_expr_void (block);
16571 add_decl_expr (omp_out);
16572 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
16573 TREE_NO_WARNING (omp_out) = 1;
16574 add_decl_expr (omp_in);
16575 finish_expr_stmt (block);
16577 if (i >= 6)
16579 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
16580 && TREE_CODE (stmts[4]) == DECL_EXPR);
16581 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
16582 args, complain, in_decl);
16583 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
16584 args, complain, in_decl);
16585 DECL_CONTEXT (omp_priv) = current_function_decl;
16586 DECL_CONTEXT (omp_orig) = current_function_decl;
16587 keep_next_level (true);
16588 tree block = begin_omp_structured_block ();
16589 tsubst_expr (stmts[5], args, complain, in_decl, false);
16590 block = finish_omp_structured_block (block);
16591 block = maybe_cleanup_point_expr_void (block);
16592 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
16593 add_decl_expr (omp_priv);
16594 add_decl_expr (omp_orig);
16595 finish_expr_stmt (block);
16596 if (i == 7)
16597 add_decl_expr (omp_orig);
16601 /* T is a postfix-expression that is not being used in a function
16602 call. Return the substituted version of T. */
16604 static tree
16605 tsubst_non_call_postfix_expression (tree t, tree args,
16606 tsubst_flags_t complain,
16607 tree in_decl)
16609 if (TREE_CODE (t) == SCOPE_REF)
16610 t = tsubst_qualified_id (t, args, complain, in_decl,
16611 /*done=*/false, /*address_p=*/false);
16612 else
16613 t = tsubst_copy_and_build (t, args, complain, in_decl,
16614 /*function_p=*/false,
16615 /*integral_constant_expression_p=*/false);
16617 return t;
16620 /* Like tsubst but deals with expressions and performs semantic
16621 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
16623 tree
16624 tsubst_copy_and_build (tree t,
16625 tree args,
16626 tsubst_flags_t complain,
16627 tree in_decl,
16628 bool function_p,
16629 bool integral_constant_expression_p)
16631 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
16632 #define RECUR(NODE) \
16633 tsubst_copy_and_build (NODE, args, complain, in_decl, \
16634 /*function_p=*/false, \
16635 integral_constant_expression_p)
16637 tree retval, op1;
16638 location_t loc;
16640 if (t == NULL_TREE || t == error_mark_node)
16641 return t;
16643 loc = input_location;
16644 if (EXPR_HAS_LOCATION (t))
16645 input_location = EXPR_LOCATION (t);
16647 /* N3276 decltype magic only applies to calls at the top level or on the
16648 right side of a comma. */
16649 tsubst_flags_t decltype_flag = (complain & tf_decltype);
16650 complain &= ~tf_decltype;
16652 switch (TREE_CODE (t))
16654 case USING_DECL:
16655 t = DECL_NAME (t);
16656 /* Fall through. */
16657 case IDENTIFIER_NODE:
16659 tree decl;
16660 cp_id_kind idk;
16661 bool non_integral_constant_expression_p;
16662 const char *error_msg;
16664 if (IDENTIFIER_TYPENAME_P (t))
16666 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16667 t = mangle_conv_op_name_for_type (new_type);
16670 /* Look up the name. */
16671 decl = lookup_name (t);
16673 /* By convention, expressions use ERROR_MARK_NODE to indicate
16674 failure, not NULL_TREE. */
16675 if (decl == NULL_TREE)
16676 decl = error_mark_node;
16678 decl = finish_id_expression (t, decl, NULL_TREE,
16679 &idk,
16680 integral_constant_expression_p,
16681 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
16682 &non_integral_constant_expression_p,
16683 /*template_p=*/false,
16684 /*done=*/true,
16685 /*address_p=*/false,
16686 /*template_arg_p=*/false,
16687 &error_msg,
16688 input_location);
16689 if (error_msg)
16690 error (error_msg);
16691 if (!function_p && identifier_p (decl))
16693 if (complain & tf_error)
16694 unqualified_name_lookup_error (decl);
16695 decl = error_mark_node;
16697 RETURN (decl);
16700 case TEMPLATE_ID_EXPR:
16702 tree object;
16703 tree templ = RECUR (TREE_OPERAND (t, 0));
16704 tree targs = TREE_OPERAND (t, 1);
16706 if (targs)
16707 targs = tsubst_template_args (targs, args, complain, in_decl);
16708 if (targs == error_mark_node)
16709 return error_mark_node;
16711 if (TREE_CODE (templ) == SCOPE_REF)
16713 tree name = TREE_OPERAND (templ, 1);
16714 tree tid = lookup_template_function (name, targs);
16715 TREE_OPERAND (templ, 1) = tid;
16716 return templ;
16719 if (variable_template_p (templ))
16720 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
16722 if (TREE_CODE (templ) == COMPONENT_REF)
16724 object = TREE_OPERAND (templ, 0);
16725 templ = TREE_OPERAND (templ, 1);
16727 else
16728 object = NULL_TREE;
16729 templ = lookup_template_function (templ, targs);
16731 if (object)
16732 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
16733 object, templ, NULL_TREE));
16734 else
16735 RETURN (baselink_for_fns (templ));
16738 case INDIRECT_REF:
16740 tree r = RECUR (TREE_OPERAND (t, 0));
16742 if (REFERENCE_REF_P (t))
16744 /* A type conversion to reference type will be enclosed in
16745 such an indirect ref, but the substitution of the cast
16746 will have also added such an indirect ref. */
16747 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
16748 r = convert_from_reference (r);
16750 else
16751 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
16752 complain|decltype_flag);
16754 if (TREE_CODE (r) == INDIRECT_REF)
16755 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
16757 RETURN (r);
16760 case NOP_EXPR:
16762 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16763 tree op0 = RECUR (TREE_OPERAND (t, 0));
16764 RETURN (build_nop (type, op0));
16767 case IMPLICIT_CONV_EXPR:
16769 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16770 tree expr = RECUR (TREE_OPERAND (t, 0));
16771 int flags = LOOKUP_IMPLICIT;
16772 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
16773 flags = LOOKUP_NORMAL;
16774 RETURN (perform_implicit_conversion_flags (type, expr, complain,
16775 flags));
16778 case CONVERT_EXPR:
16780 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16781 tree op0 = RECUR (TREE_OPERAND (t, 0));
16782 RETURN (build1 (CONVERT_EXPR, type, op0));
16785 case CAST_EXPR:
16786 case REINTERPRET_CAST_EXPR:
16787 case CONST_CAST_EXPR:
16788 case DYNAMIC_CAST_EXPR:
16789 case STATIC_CAST_EXPR:
16791 tree type;
16792 tree op, r = NULL_TREE;
16794 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16795 if (integral_constant_expression_p
16796 && !cast_valid_in_integral_constant_expression_p (type))
16798 if (complain & tf_error)
16799 error ("a cast to a type other than an integral or "
16800 "enumeration type cannot appear in a constant-expression");
16801 RETURN (error_mark_node);
16804 op = RECUR (TREE_OPERAND (t, 0));
16806 warning_sentinel s(warn_useless_cast);
16807 switch (TREE_CODE (t))
16809 case CAST_EXPR:
16810 r = build_functional_cast (type, op, complain);
16811 break;
16812 case REINTERPRET_CAST_EXPR:
16813 r = build_reinterpret_cast (type, op, complain);
16814 break;
16815 case CONST_CAST_EXPR:
16816 r = build_const_cast (type, op, complain);
16817 break;
16818 case DYNAMIC_CAST_EXPR:
16819 r = build_dynamic_cast (type, op, complain);
16820 break;
16821 case STATIC_CAST_EXPR:
16822 r = build_static_cast (type, op, complain);
16823 break;
16824 default:
16825 gcc_unreachable ();
16828 RETURN (r);
16831 case POSTDECREMENT_EXPR:
16832 case POSTINCREMENT_EXPR:
16833 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16834 args, complain, in_decl);
16835 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
16836 complain|decltype_flag));
16838 case PREDECREMENT_EXPR:
16839 case PREINCREMENT_EXPR:
16840 case NEGATE_EXPR:
16841 case BIT_NOT_EXPR:
16842 case ABS_EXPR:
16843 case TRUTH_NOT_EXPR:
16844 case UNARY_PLUS_EXPR: /* Unary + */
16845 case REALPART_EXPR:
16846 case IMAGPART_EXPR:
16847 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
16848 RECUR (TREE_OPERAND (t, 0)),
16849 complain|decltype_flag));
16851 case FIX_TRUNC_EXPR:
16852 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
16853 false, complain));
16855 case ADDR_EXPR:
16856 op1 = TREE_OPERAND (t, 0);
16857 if (TREE_CODE (op1) == LABEL_DECL)
16858 RETURN (finish_label_address_expr (DECL_NAME (op1),
16859 EXPR_LOCATION (op1)));
16860 if (TREE_CODE (op1) == SCOPE_REF)
16861 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
16862 /*done=*/true, /*address_p=*/true);
16863 else
16864 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
16865 in_decl);
16866 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
16867 complain|decltype_flag));
16869 case PLUS_EXPR:
16870 case MINUS_EXPR:
16871 case MULT_EXPR:
16872 case TRUNC_DIV_EXPR:
16873 case CEIL_DIV_EXPR:
16874 case FLOOR_DIV_EXPR:
16875 case ROUND_DIV_EXPR:
16876 case EXACT_DIV_EXPR:
16877 case BIT_AND_EXPR:
16878 case BIT_IOR_EXPR:
16879 case BIT_XOR_EXPR:
16880 case TRUNC_MOD_EXPR:
16881 case FLOOR_MOD_EXPR:
16882 case TRUTH_ANDIF_EXPR:
16883 case TRUTH_ORIF_EXPR:
16884 case TRUTH_AND_EXPR:
16885 case TRUTH_OR_EXPR:
16886 case RSHIFT_EXPR:
16887 case LSHIFT_EXPR:
16888 case RROTATE_EXPR:
16889 case LROTATE_EXPR:
16890 case EQ_EXPR:
16891 case NE_EXPR:
16892 case MAX_EXPR:
16893 case MIN_EXPR:
16894 case LE_EXPR:
16895 case GE_EXPR:
16896 case LT_EXPR:
16897 case GT_EXPR:
16898 case MEMBER_REF:
16899 case DOTSTAR_EXPR:
16901 warning_sentinel s1(warn_type_limits);
16902 warning_sentinel s2(warn_div_by_zero);
16903 warning_sentinel s3(warn_logical_op);
16904 warning_sentinel s4(warn_tautological_compare);
16905 tree op0 = RECUR (TREE_OPERAND (t, 0));
16906 tree op1 = RECUR (TREE_OPERAND (t, 1));
16907 tree r = build_x_binary_op
16908 (input_location, TREE_CODE (t),
16909 op0,
16910 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
16911 ? ERROR_MARK
16912 : TREE_CODE (TREE_OPERAND (t, 0))),
16913 op1,
16914 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
16915 ? ERROR_MARK
16916 : TREE_CODE (TREE_OPERAND (t, 1))),
16917 /*overload=*/NULL,
16918 complain|decltype_flag);
16919 if (EXPR_P (r) && TREE_NO_WARNING (t))
16920 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16922 RETURN (r);
16925 case POINTER_PLUS_EXPR:
16927 tree op0 = RECUR (TREE_OPERAND (t, 0));
16928 tree op1 = RECUR (TREE_OPERAND (t, 1));
16929 return fold_build_pointer_plus (op0, op1);
16932 case SCOPE_REF:
16933 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
16934 /*address_p=*/false));
16935 case ARRAY_REF:
16936 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16937 args, complain, in_decl);
16938 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
16939 RECUR (TREE_OPERAND (t, 1)),
16940 complain|decltype_flag));
16942 case ARRAY_NOTATION_REF:
16944 tree start_index, length, stride;
16945 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
16946 args, complain, in_decl);
16947 start_index = RECUR (ARRAY_NOTATION_START (t));
16948 length = RECUR (ARRAY_NOTATION_LENGTH (t));
16949 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
16950 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
16951 length, stride, TREE_TYPE (op1)));
16953 case SIZEOF_EXPR:
16954 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
16955 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
16956 RETURN (tsubst_copy (t, args, complain, in_decl));
16957 /* Fall through */
16959 case ALIGNOF_EXPR:
16961 tree r;
16963 op1 = TREE_OPERAND (t, 0);
16964 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
16965 op1 = TREE_TYPE (op1);
16966 if (!args)
16968 /* When there are no ARGS, we are trying to evaluate a
16969 non-dependent expression from the parser. Trying to do
16970 the substitutions may not work. */
16971 if (!TYPE_P (op1))
16972 op1 = TREE_TYPE (op1);
16974 else
16976 ++cp_unevaluated_operand;
16977 ++c_inhibit_evaluation_warnings;
16978 if (TYPE_P (op1))
16979 op1 = tsubst (op1, args, complain, in_decl);
16980 else
16981 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
16982 /*function_p=*/false,
16983 /*integral_constant_expression_p=*/
16984 false);
16985 --cp_unevaluated_operand;
16986 --c_inhibit_evaluation_warnings;
16988 if (TYPE_P (op1))
16989 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
16990 complain & tf_error);
16991 else
16992 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
16993 complain & tf_error);
16994 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
16996 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
16998 if (!processing_template_decl && TYPE_P (op1))
17000 r = build_min (SIZEOF_EXPR, size_type_node,
17001 build1 (NOP_EXPR, op1, error_mark_node));
17002 SIZEOF_EXPR_TYPE_P (r) = 1;
17004 else
17005 r = build_min (SIZEOF_EXPR, size_type_node, op1);
17006 TREE_SIDE_EFFECTS (r) = 0;
17007 TREE_READONLY (r) = 1;
17009 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
17011 RETURN (r);
17014 case AT_ENCODE_EXPR:
17016 op1 = TREE_OPERAND (t, 0);
17017 ++cp_unevaluated_operand;
17018 ++c_inhibit_evaluation_warnings;
17019 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17020 /*function_p=*/false,
17021 /*integral_constant_expression_p=*/false);
17022 --cp_unevaluated_operand;
17023 --c_inhibit_evaluation_warnings;
17024 RETURN (objc_build_encode_expr (op1));
17027 case NOEXCEPT_EXPR:
17028 op1 = TREE_OPERAND (t, 0);
17029 ++cp_unevaluated_operand;
17030 ++c_inhibit_evaluation_warnings;
17031 ++cp_noexcept_operand;
17032 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17033 /*function_p=*/false,
17034 /*integral_constant_expression_p=*/false);
17035 --cp_unevaluated_operand;
17036 --c_inhibit_evaluation_warnings;
17037 --cp_noexcept_operand;
17038 RETURN (finish_noexcept_expr (op1, complain));
17040 case MODOP_EXPR:
17042 warning_sentinel s(warn_div_by_zero);
17043 tree lhs = RECUR (TREE_OPERAND (t, 0));
17044 tree rhs = RECUR (TREE_OPERAND (t, 2));
17045 tree r = build_x_modify_expr
17046 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
17047 complain|decltype_flag);
17048 /* TREE_NO_WARNING must be set if either the expression was
17049 parenthesized or it uses an operator such as >>= rather
17050 than plain assignment. In the former case, it was already
17051 set and must be copied. In the latter case,
17052 build_x_modify_expr sets it and it must not be reset
17053 here. */
17054 if (TREE_NO_WARNING (t))
17055 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17057 RETURN (r);
17060 case ARROW_EXPR:
17061 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17062 args, complain, in_decl);
17063 /* Remember that there was a reference to this entity. */
17064 if (DECL_P (op1)
17065 && !mark_used (op1, complain) && !(complain & tf_error))
17066 RETURN (error_mark_node);
17067 RETURN (build_x_arrow (input_location, op1, complain));
17069 case NEW_EXPR:
17071 tree placement = RECUR (TREE_OPERAND (t, 0));
17072 tree init = RECUR (TREE_OPERAND (t, 3));
17073 vec<tree, va_gc> *placement_vec;
17074 vec<tree, va_gc> *init_vec;
17075 tree ret;
17077 if (placement == NULL_TREE)
17078 placement_vec = NULL;
17079 else
17081 placement_vec = make_tree_vector ();
17082 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
17083 vec_safe_push (placement_vec, TREE_VALUE (placement));
17086 /* If there was an initializer in the original tree, but it
17087 instantiated to an empty list, then we should pass a
17088 non-NULL empty vector to tell build_new that it was an
17089 empty initializer() rather than no initializer. This can
17090 only happen when the initializer is a pack expansion whose
17091 parameter packs are of length zero. */
17092 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
17093 init_vec = NULL;
17094 else
17096 init_vec = make_tree_vector ();
17097 if (init == void_node)
17098 gcc_assert (init_vec != NULL);
17099 else
17101 for (; init != NULL_TREE; init = TREE_CHAIN (init))
17102 vec_safe_push (init_vec, TREE_VALUE (init));
17106 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
17107 tree op2 = RECUR (TREE_OPERAND (t, 2));
17108 ret = build_new (&placement_vec, op1, op2, &init_vec,
17109 NEW_EXPR_USE_GLOBAL (t),
17110 complain);
17112 if (placement_vec != NULL)
17113 release_tree_vector (placement_vec);
17114 if (init_vec != NULL)
17115 release_tree_vector (init_vec);
17117 RETURN (ret);
17120 case DELETE_EXPR:
17122 tree op0 = RECUR (TREE_OPERAND (t, 0));
17123 tree op1 = RECUR (TREE_OPERAND (t, 1));
17124 RETURN (delete_sanity (op0, op1,
17125 DELETE_EXPR_USE_VEC (t),
17126 DELETE_EXPR_USE_GLOBAL (t),
17127 complain));
17130 case COMPOUND_EXPR:
17132 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
17133 complain & ~tf_decltype, in_decl,
17134 /*function_p=*/false,
17135 integral_constant_expression_p);
17136 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
17137 op0,
17138 RECUR (TREE_OPERAND (t, 1)),
17139 complain|decltype_flag));
17142 case CALL_EXPR:
17144 tree function;
17145 vec<tree, va_gc> *call_args;
17146 unsigned int nargs, i;
17147 bool qualified_p;
17148 bool koenig_p;
17149 tree ret;
17151 function = CALL_EXPR_FN (t);
17152 /* Internal function with no arguments. */
17153 if (function == NULL_TREE && call_expr_nargs (t) == 0)
17154 RETURN (t);
17156 /* When we parsed the expression, we determined whether or
17157 not Koenig lookup should be performed. */
17158 koenig_p = KOENIG_LOOKUP_P (t);
17159 if (function == NULL_TREE)
17161 koenig_p = false;
17162 qualified_p = false;
17164 else if (TREE_CODE (function) == SCOPE_REF)
17166 qualified_p = true;
17167 function = tsubst_qualified_id (function, args, complain, in_decl,
17168 /*done=*/false,
17169 /*address_p=*/false);
17171 else if (koenig_p && identifier_p (function))
17173 /* Do nothing; calling tsubst_copy_and_build on an identifier
17174 would incorrectly perform unqualified lookup again.
17176 Note that we can also have an IDENTIFIER_NODE if the earlier
17177 unqualified lookup found a member function; in that case
17178 koenig_p will be false and we do want to do the lookup
17179 again to find the instantiated member function.
17181 FIXME but doing that causes c++/15272, so we need to stop
17182 using IDENTIFIER_NODE in that situation. */
17183 qualified_p = false;
17185 else
17187 if (TREE_CODE (function) == COMPONENT_REF)
17189 tree op = TREE_OPERAND (function, 1);
17191 qualified_p = (TREE_CODE (op) == SCOPE_REF
17192 || (BASELINK_P (op)
17193 && BASELINK_QUALIFIED_P (op)));
17195 else
17196 qualified_p = false;
17198 if (TREE_CODE (function) == ADDR_EXPR
17199 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
17200 /* Avoid error about taking the address of a constructor. */
17201 function = TREE_OPERAND (function, 0);
17203 function = tsubst_copy_and_build (function, args, complain,
17204 in_decl,
17205 !qualified_p,
17206 integral_constant_expression_p);
17208 if (BASELINK_P (function))
17209 qualified_p = true;
17212 nargs = call_expr_nargs (t);
17213 call_args = make_tree_vector ();
17214 for (i = 0; i < nargs; ++i)
17216 tree arg = CALL_EXPR_ARG (t, i);
17218 if (!PACK_EXPANSION_P (arg))
17219 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
17220 else
17222 /* Expand the pack expansion and push each entry onto
17223 CALL_ARGS. */
17224 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
17225 if (TREE_CODE (arg) == TREE_VEC)
17227 unsigned int len, j;
17229 len = TREE_VEC_LENGTH (arg);
17230 for (j = 0; j < len; ++j)
17232 tree value = TREE_VEC_ELT (arg, j);
17233 if (value != NULL_TREE)
17234 value = convert_from_reference (value);
17235 vec_safe_push (call_args, value);
17238 else
17240 /* A partial substitution. Add one entry. */
17241 vec_safe_push (call_args, arg);
17246 /* We do not perform argument-dependent lookup if normal
17247 lookup finds a non-function, in accordance with the
17248 expected resolution of DR 218. */
17249 if (koenig_p
17250 && ((is_overloaded_fn (function)
17251 /* If lookup found a member function, the Koenig lookup is
17252 not appropriate, even if an unqualified-name was used
17253 to denote the function. */
17254 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
17255 || identifier_p (function))
17256 /* Only do this when substitution turns a dependent call
17257 into a non-dependent call. */
17258 && type_dependent_expression_p_push (t)
17259 && !any_type_dependent_arguments_p (call_args))
17260 function = perform_koenig_lookup (function, call_args, tf_none);
17262 if (function != NULL_TREE
17263 && identifier_p (function)
17264 && !any_type_dependent_arguments_p (call_args))
17266 if (koenig_p && (complain & tf_warning_or_error))
17268 /* For backwards compatibility and good diagnostics, try
17269 the unqualified lookup again if we aren't in SFINAE
17270 context. */
17271 tree unq = (tsubst_copy_and_build
17272 (function, args, complain, in_decl, true,
17273 integral_constant_expression_p));
17274 if (unq == error_mark_node)
17276 release_tree_vector (call_args);
17277 RETURN (error_mark_node);
17280 if (unq != function)
17282 /* In a lambda fn, we have to be careful to not
17283 introduce new this captures. Legacy code can't
17284 be using lambdas anyway, so it's ok to be
17285 stricter. */
17286 bool in_lambda = (current_class_type
17287 && LAMBDA_TYPE_P (current_class_type));
17288 char const *const msg
17289 = G_("%qD was not declared in this scope, "
17290 "and no declarations were found by "
17291 "argument-dependent lookup at the point "
17292 "of instantiation");
17294 bool diag = true;
17295 if (in_lambda)
17296 error_at (EXPR_LOC_OR_LOC (t, input_location),
17297 msg, function);
17298 else
17299 diag = permerror (EXPR_LOC_OR_LOC (t, input_location),
17300 msg, function);
17301 if (diag)
17303 tree fn = unq;
17305 if (INDIRECT_REF_P (fn))
17306 fn = TREE_OPERAND (fn, 0);
17307 if (is_overloaded_fn (fn))
17308 fn = get_first_fn (fn);
17310 if (!DECL_P (fn))
17311 /* Can't say anything more. */;
17312 else if (DECL_CLASS_SCOPE_P (fn))
17314 location_t loc = EXPR_LOC_OR_LOC (t,
17315 input_location);
17316 inform (loc,
17317 "declarations in dependent base %qT are "
17318 "not found by unqualified lookup",
17319 DECL_CLASS_CONTEXT (fn));
17320 if (current_class_ptr)
17321 inform (loc,
17322 "use %<this->%D%> instead", function);
17323 else
17324 inform (loc,
17325 "use %<%T::%D%> instead",
17326 current_class_name, function);
17328 else
17329 inform (DECL_SOURCE_LOCATION (fn),
17330 "%qD declared here, later in the "
17331 "translation unit", fn);
17332 if (in_lambda)
17334 release_tree_vector (call_args);
17335 RETURN (error_mark_node);
17339 function = unq;
17342 if (identifier_p (function))
17344 if (complain & tf_error)
17345 unqualified_name_lookup_error (function);
17346 release_tree_vector (call_args);
17347 RETURN (error_mark_node);
17351 /* Remember that there was a reference to this entity. */
17352 if (function != NULL_TREE
17353 && DECL_P (function)
17354 && !mark_used (function, complain) && !(complain & tf_error))
17356 release_tree_vector (call_args);
17357 RETURN (error_mark_node);
17360 /* Put back tf_decltype for the actual call. */
17361 complain |= decltype_flag;
17363 if (function == NULL_TREE)
17364 switch (CALL_EXPR_IFN (t))
17366 case IFN_LAUNDER:
17367 gcc_assert (nargs == 1);
17368 if (vec_safe_length (call_args) != 1)
17370 error_at (EXPR_LOC_OR_LOC (t, input_location),
17371 "wrong number of arguments to "
17372 "%<__builtin_launder%>");
17373 ret = error_mark_node;
17375 else
17376 ret = finish_builtin_launder (EXPR_LOC_OR_LOC (t,
17377 input_location),
17378 (*call_args)[0], complain);
17379 break;
17381 default:
17382 /* Unsupported internal function with arguments. */
17383 gcc_unreachable ();
17385 else if (TREE_CODE (function) == OFFSET_REF)
17386 ret = build_offset_ref_call_from_tree (function, &call_args,
17387 complain);
17388 else if (TREE_CODE (function) == COMPONENT_REF)
17390 tree instance = TREE_OPERAND (function, 0);
17391 tree fn = TREE_OPERAND (function, 1);
17393 if (processing_template_decl
17394 && (type_dependent_expression_p (instance)
17395 || (!BASELINK_P (fn)
17396 && TREE_CODE (fn) != FIELD_DECL)
17397 || type_dependent_expression_p (fn)
17398 || any_type_dependent_arguments_p (call_args)))
17399 ret = build_min_nt_call_vec (function, call_args);
17400 else if (!BASELINK_P (fn))
17401 ret = finish_call_expr (function, &call_args,
17402 /*disallow_virtual=*/false,
17403 /*koenig_p=*/false,
17404 complain);
17405 else
17406 ret = (build_new_method_call
17407 (instance, fn,
17408 &call_args, NULL_TREE,
17409 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
17410 /*fn_p=*/NULL,
17411 complain));
17413 else
17414 ret = finish_call_expr (function, &call_args,
17415 /*disallow_virtual=*/qualified_p,
17416 koenig_p,
17417 complain);
17419 release_tree_vector (call_args);
17421 if (ret != error_mark_node)
17423 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
17424 bool ord = CALL_EXPR_ORDERED_ARGS (t);
17425 bool rev = CALL_EXPR_REVERSE_ARGS (t);
17426 bool thk = CALL_FROM_THUNK_P (t);
17427 if (op || ord || rev || thk)
17429 function = extract_call_expr (ret);
17430 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
17431 CALL_EXPR_ORDERED_ARGS (function) = ord;
17432 CALL_EXPR_REVERSE_ARGS (function) = rev;
17433 if (thk)
17435 CALL_FROM_THUNK_P (function) = true;
17436 /* The thunk location is not interesting. */
17437 SET_EXPR_LOCATION (function, UNKNOWN_LOCATION);
17442 RETURN (ret);
17445 case COND_EXPR:
17447 tree cond = RECUR (TREE_OPERAND (t, 0));
17448 tree folded_cond = fold_non_dependent_expr (cond);
17449 tree exp1, exp2;
17451 if (TREE_CODE (folded_cond) == INTEGER_CST)
17453 if (integer_zerop (folded_cond))
17455 ++c_inhibit_evaluation_warnings;
17456 exp1 = RECUR (TREE_OPERAND (t, 1));
17457 --c_inhibit_evaluation_warnings;
17458 exp2 = RECUR (TREE_OPERAND (t, 2));
17460 else
17462 exp1 = RECUR (TREE_OPERAND (t, 1));
17463 ++c_inhibit_evaluation_warnings;
17464 exp2 = RECUR (TREE_OPERAND (t, 2));
17465 --c_inhibit_evaluation_warnings;
17467 cond = folded_cond;
17469 else
17471 exp1 = RECUR (TREE_OPERAND (t, 1));
17472 exp2 = RECUR (TREE_OPERAND (t, 2));
17475 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
17476 cond, exp1, exp2, complain));
17479 case PSEUDO_DTOR_EXPR:
17481 tree op0 = RECUR (TREE_OPERAND (t, 0));
17482 tree op1 = RECUR (TREE_OPERAND (t, 1));
17483 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
17484 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
17485 input_location));
17488 case TREE_LIST:
17490 tree purpose, value, chain;
17492 if (t == void_list_node)
17493 RETURN (t);
17495 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
17496 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
17498 /* We have pack expansions, so expand those and
17499 create a new list out of it. */
17500 tree purposevec = NULL_TREE;
17501 tree valuevec = NULL_TREE;
17502 tree chain;
17503 int i, len = -1;
17505 /* Expand the argument expressions. */
17506 if (TREE_PURPOSE (t))
17507 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
17508 complain, in_decl);
17509 if (TREE_VALUE (t))
17510 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
17511 complain, in_decl);
17513 /* Build the rest of the list. */
17514 chain = TREE_CHAIN (t);
17515 if (chain && chain != void_type_node)
17516 chain = RECUR (chain);
17518 /* Determine the number of arguments. */
17519 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
17521 len = TREE_VEC_LENGTH (purposevec);
17522 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
17524 else if (TREE_CODE (valuevec) == TREE_VEC)
17525 len = TREE_VEC_LENGTH (valuevec);
17526 else
17528 /* Since we only performed a partial substitution into
17529 the argument pack, we only RETURN (a single list
17530 node. */
17531 if (purposevec == TREE_PURPOSE (t)
17532 && valuevec == TREE_VALUE (t)
17533 && chain == TREE_CHAIN (t))
17534 RETURN (t);
17536 RETURN (tree_cons (purposevec, valuevec, chain));
17539 /* Convert the argument vectors into a TREE_LIST */
17540 i = len;
17541 while (i > 0)
17543 /* Grab the Ith values. */
17544 i--;
17545 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
17546 : NULL_TREE;
17547 value
17548 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
17549 : NULL_TREE;
17551 /* Build the list (backwards). */
17552 chain = tree_cons (purpose, value, chain);
17555 RETURN (chain);
17558 purpose = TREE_PURPOSE (t);
17559 if (purpose)
17560 purpose = RECUR (purpose);
17561 value = TREE_VALUE (t);
17562 if (value)
17563 value = RECUR (value);
17564 chain = TREE_CHAIN (t);
17565 if (chain && chain != void_type_node)
17566 chain = RECUR (chain);
17567 if (purpose == TREE_PURPOSE (t)
17568 && value == TREE_VALUE (t)
17569 && chain == TREE_CHAIN (t))
17570 RETURN (t);
17571 RETURN (tree_cons (purpose, value, chain));
17574 case COMPONENT_REF:
17576 tree object;
17577 tree object_type;
17578 tree member;
17579 tree r;
17581 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17582 args, complain, in_decl);
17583 /* Remember that there was a reference to this entity. */
17584 if (DECL_P (object)
17585 && !mark_used (object, complain) && !(complain & tf_error))
17586 RETURN (error_mark_node);
17587 object_type = TREE_TYPE (object);
17589 member = TREE_OPERAND (t, 1);
17590 if (BASELINK_P (member))
17591 member = tsubst_baselink (member,
17592 non_reference (TREE_TYPE (object)),
17593 args, complain, in_decl);
17594 else
17595 member = tsubst_copy (member, args, complain, in_decl);
17596 if (member == error_mark_node)
17597 RETURN (error_mark_node);
17599 if (TREE_CODE (member) == FIELD_DECL)
17601 r = finish_non_static_data_member (member, object, NULL_TREE);
17602 if (TREE_CODE (r) == COMPONENT_REF)
17603 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17604 RETURN (r);
17606 else if (type_dependent_expression_p (object))
17607 /* We can't do much here. */;
17608 else if (!CLASS_TYPE_P (object_type))
17610 if (scalarish_type_p (object_type))
17612 tree s = NULL_TREE;
17613 tree dtor = member;
17615 if (TREE_CODE (dtor) == SCOPE_REF)
17617 s = TREE_OPERAND (dtor, 0);
17618 dtor = TREE_OPERAND (dtor, 1);
17620 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
17622 dtor = TREE_OPERAND (dtor, 0);
17623 if (TYPE_P (dtor))
17624 RETURN (finish_pseudo_destructor_expr
17625 (object, s, dtor, input_location));
17629 else if (TREE_CODE (member) == SCOPE_REF
17630 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
17632 /* Lookup the template functions now that we know what the
17633 scope is. */
17634 tree scope = TREE_OPERAND (member, 0);
17635 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
17636 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
17637 member = lookup_qualified_name (scope, tmpl,
17638 /*is_type_p=*/false,
17639 /*complain=*/false);
17640 if (BASELINK_P (member))
17642 BASELINK_FUNCTIONS (member)
17643 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
17644 args);
17645 member = (adjust_result_of_qualified_name_lookup
17646 (member, BINFO_TYPE (BASELINK_BINFO (member)),
17647 object_type));
17649 else
17651 qualified_name_lookup_error (scope, tmpl, member,
17652 input_location);
17653 RETURN (error_mark_node);
17656 else if (TREE_CODE (member) == SCOPE_REF
17657 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
17658 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
17660 if (complain & tf_error)
17662 if (TYPE_P (TREE_OPERAND (member, 0)))
17663 error ("%qT is not a class or namespace",
17664 TREE_OPERAND (member, 0));
17665 else
17666 error ("%qD is not a class or namespace",
17667 TREE_OPERAND (member, 0));
17669 RETURN (error_mark_node);
17672 r = finish_class_member_access_expr (object, member,
17673 /*template_p=*/false,
17674 complain);
17675 if (TREE_CODE (r) == COMPONENT_REF)
17676 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17677 RETURN (r);
17680 case THROW_EXPR:
17681 RETURN (build_throw
17682 (RECUR (TREE_OPERAND (t, 0))));
17684 case CONSTRUCTOR:
17686 vec<constructor_elt, va_gc> *n;
17687 constructor_elt *ce;
17688 unsigned HOST_WIDE_INT idx;
17689 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17690 bool process_index_p;
17691 int newlen;
17692 bool need_copy_p = false;
17693 tree r;
17695 if (type == error_mark_node)
17696 RETURN (error_mark_node);
17698 /* digest_init will do the wrong thing if we let it. */
17699 if (type && TYPE_PTRMEMFUNC_P (type))
17700 RETURN (t);
17702 /* We do not want to process the index of aggregate
17703 initializers as they are identifier nodes which will be
17704 looked up by digest_init. */
17705 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
17707 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
17708 newlen = vec_safe_length (n);
17709 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
17711 if (ce->index && process_index_p
17712 /* An identifier index is looked up in the type
17713 being initialized, not the current scope. */
17714 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
17715 ce->index = RECUR (ce->index);
17717 if (PACK_EXPANSION_P (ce->value))
17719 /* Substitute into the pack expansion. */
17720 ce->value = tsubst_pack_expansion (ce->value, args, complain,
17721 in_decl);
17723 if (ce->value == error_mark_node
17724 || PACK_EXPANSION_P (ce->value))
17726 else if (TREE_VEC_LENGTH (ce->value) == 1)
17727 /* Just move the argument into place. */
17728 ce->value = TREE_VEC_ELT (ce->value, 0);
17729 else
17731 /* Update the length of the final CONSTRUCTOR
17732 arguments vector, and note that we will need to
17733 copy.*/
17734 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
17735 need_copy_p = true;
17738 else
17739 ce->value = RECUR (ce->value);
17742 if (need_copy_p)
17744 vec<constructor_elt, va_gc> *old_n = n;
17746 vec_alloc (n, newlen);
17747 FOR_EACH_VEC_ELT (*old_n, idx, ce)
17749 if (TREE_CODE (ce->value) == TREE_VEC)
17751 int i, len = TREE_VEC_LENGTH (ce->value);
17752 for (i = 0; i < len; ++i)
17753 CONSTRUCTOR_APPEND_ELT (n, 0,
17754 TREE_VEC_ELT (ce->value, i));
17756 else
17757 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
17761 r = build_constructor (init_list_type_node, n);
17762 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
17764 if (TREE_HAS_CONSTRUCTOR (t))
17766 fcl_t cl = fcl_functional;
17767 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
17768 cl = fcl_c99;
17769 RETURN (finish_compound_literal (type, r, complain, cl));
17772 TREE_TYPE (r) = type;
17773 RETURN (r);
17776 case TYPEID_EXPR:
17778 tree operand_0 = TREE_OPERAND (t, 0);
17779 if (TYPE_P (operand_0))
17781 operand_0 = tsubst (operand_0, args, complain, in_decl);
17782 RETURN (get_typeid (operand_0, complain));
17784 else
17786 operand_0 = RECUR (operand_0);
17787 RETURN (build_typeid (operand_0, complain));
17791 case VAR_DECL:
17792 if (!args)
17793 RETURN (t);
17794 else if (DECL_PACK_P (t))
17796 /* We don't build decls for an instantiation of a
17797 variadic capture proxy, we instantiate the elements
17798 when needed. */
17799 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
17800 return RECUR (DECL_VALUE_EXPR (t));
17802 /* Fall through */
17804 case PARM_DECL:
17806 tree r = tsubst_copy (t, args, complain, in_decl);
17807 /* ??? We're doing a subset of finish_id_expression here. */
17808 if (VAR_P (r)
17809 && !processing_template_decl
17810 && !cp_unevaluated_operand
17811 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
17812 && CP_DECL_THREAD_LOCAL_P (r))
17814 if (tree wrap = get_tls_wrapper_fn (r))
17815 /* Replace an evaluated use of the thread_local variable with
17816 a call to its wrapper. */
17817 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
17819 else if (outer_automatic_var_p (r))
17821 r = process_outer_var_ref (r, complain);
17822 if (is_capture_proxy (r))
17823 register_local_specialization (r, t);
17826 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
17827 /* If the original type was a reference, we'll be wrapped in
17828 the appropriate INDIRECT_REF. */
17829 r = convert_from_reference (r);
17830 RETURN (r);
17833 case VA_ARG_EXPR:
17835 tree op0 = RECUR (TREE_OPERAND (t, 0));
17836 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17837 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
17840 case OFFSETOF_EXPR:
17842 tree object_ptr
17843 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
17844 in_decl, /*function_p=*/false,
17845 /*integral_constant_expression_p=*/false);
17846 RETURN (finish_offsetof (object_ptr,
17847 RECUR (TREE_OPERAND (t, 0)),
17848 EXPR_LOCATION (t)));
17851 case ADDRESSOF_EXPR:
17852 RETURN (cp_build_addressof (EXPR_LOCATION (t),
17853 RECUR (TREE_OPERAND (t, 0)), complain));
17855 case TRAIT_EXPR:
17857 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
17858 complain, in_decl);
17860 tree type2 = TRAIT_EXPR_TYPE2 (t);
17861 if (type2 && TREE_CODE (type2) == TREE_LIST)
17862 type2 = RECUR (type2);
17863 else if (type2)
17864 type2 = tsubst (type2, args, complain, in_decl);
17866 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
17869 case STMT_EXPR:
17871 tree old_stmt_expr = cur_stmt_expr;
17872 tree stmt_expr = begin_stmt_expr ();
17874 cur_stmt_expr = stmt_expr;
17875 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
17876 integral_constant_expression_p);
17877 stmt_expr = finish_stmt_expr (stmt_expr, false);
17878 cur_stmt_expr = old_stmt_expr;
17880 /* If the resulting list of expression statement is empty,
17881 fold it further into void_node. */
17882 if (empty_expr_stmt_p (stmt_expr))
17883 stmt_expr = void_node;
17885 RETURN (stmt_expr);
17888 case LAMBDA_EXPR:
17890 tree r = build_lambda_expr ();
17892 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
17893 LAMBDA_EXPR_CLOSURE (r) = type;
17894 CLASSTYPE_LAMBDA_EXPR (type) = r;
17896 LAMBDA_EXPR_LOCATION (r)
17897 = LAMBDA_EXPR_LOCATION (t);
17898 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
17899 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
17900 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
17901 LAMBDA_EXPR_DISCRIMINATOR (r)
17902 = (LAMBDA_EXPR_DISCRIMINATOR (t));
17903 tree scope = LAMBDA_EXPR_EXTRA_SCOPE (t);
17904 if (!scope)
17905 /* No substitution needed. */;
17906 else if (VAR_OR_FUNCTION_DECL_P (scope))
17907 /* For a function or variable scope, we want to use tsubst so that we
17908 don't complain about referring to an auto before deduction. */
17909 scope = tsubst (scope, args, complain, in_decl);
17910 else if (TREE_CODE (scope) == PARM_DECL)
17912 /* Look up the parameter we want directly, as tsubst_copy
17913 doesn't do what we need. */
17914 tree fn = tsubst (DECL_CONTEXT (scope), args, complain, in_decl);
17915 tree parm = FUNCTION_FIRST_USER_PARM (fn);
17916 while (DECL_PARM_INDEX (parm) != DECL_PARM_INDEX (scope))
17917 parm = DECL_CHAIN (parm);
17918 scope = parm;
17919 /* FIXME Work around the parm not having DECL_CONTEXT set. */
17920 if (DECL_CONTEXT (scope) == NULL_TREE)
17921 DECL_CONTEXT (scope) = fn;
17923 else if (TREE_CODE (scope) == FIELD_DECL)
17924 /* For a field, use tsubst_copy so that we look up the existing field
17925 rather than build a new one. */
17926 scope = RECUR (scope);
17927 else
17928 gcc_unreachable ();
17929 LAMBDA_EXPR_EXTRA_SCOPE (r) = scope;
17931 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
17932 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
17934 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
17935 determine_visibility (TYPE_NAME (type));
17936 /* Now that we know visibility, instantiate the type so we have a
17937 declaration of the op() for later calls to lambda_function. */
17938 complete_type (type);
17940 if (tree fn = lambda_function (type))
17941 LAMBDA_EXPR_RETURN_TYPE (r) = TREE_TYPE (TREE_TYPE (fn));
17943 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
17945 insert_pending_capture_proxies ();
17947 RETURN (build_lambda_object (r));
17950 case TARGET_EXPR:
17951 /* We can get here for a constant initializer of non-dependent type.
17952 FIXME stop folding in cp_parser_initializer_clause. */
17954 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
17955 complain);
17956 RETURN (r);
17959 case TRANSACTION_EXPR:
17960 RETURN (tsubst_expr(t, args, complain, in_decl,
17961 integral_constant_expression_p));
17963 case PAREN_EXPR:
17964 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
17966 case VEC_PERM_EXPR:
17968 tree op0 = RECUR (TREE_OPERAND (t, 0));
17969 tree op1 = RECUR (TREE_OPERAND (t, 1));
17970 tree op2 = RECUR (TREE_OPERAND (t, 2));
17971 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
17972 complain));
17975 case REQUIRES_EXPR:
17976 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
17978 default:
17979 /* Handle Objective-C++ constructs, if appropriate. */
17981 tree subst
17982 = objcp_tsubst_copy_and_build (t, args, complain,
17983 in_decl, /*function_p=*/false);
17984 if (subst)
17985 RETURN (subst);
17987 RETURN (tsubst_copy (t, args, complain, in_decl));
17990 #undef RECUR
17991 #undef RETURN
17992 out:
17993 input_location = loc;
17994 return retval;
17997 /* Verify that the instantiated ARGS are valid. For type arguments,
17998 make sure that the type's linkage is ok. For non-type arguments,
17999 make sure they are constants if they are integral or enumerations.
18000 Emit an error under control of COMPLAIN, and return TRUE on error. */
18002 static bool
18003 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
18005 if (dependent_template_arg_p (t))
18006 return false;
18007 if (ARGUMENT_PACK_P (t))
18009 tree vec = ARGUMENT_PACK_ARGS (t);
18010 int len = TREE_VEC_LENGTH (vec);
18011 bool result = false;
18012 int i;
18014 for (i = 0; i < len; ++i)
18015 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
18016 result = true;
18017 return result;
18019 else if (TYPE_P (t))
18021 /* [basic.link]: A name with no linkage (notably, the name
18022 of a class or enumeration declared in a local scope)
18023 shall not be used to declare an entity with linkage.
18024 This implies that names with no linkage cannot be used as
18025 template arguments
18027 DR 757 relaxes this restriction for C++0x. */
18028 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
18029 : no_linkage_check (t, /*relaxed_p=*/false));
18031 if (nt)
18033 /* DR 488 makes use of a type with no linkage cause
18034 type deduction to fail. */
18035 if (complain & tf_error)
18037 if (TYPE_UNNAMED_P (nt))
18038 error ("%qT is/uses unnamed type", t);
18039 else
18040 error ("template argument for %qD uses local type %qT",
18041 tmpl, t);
18043 return true;
18045 /* In order to avoid all sorts of complications, we do not
18046 allow variably-modified types as template arguments. */
18047 else if (variably_modified_type_p (t, NULL_TREE))
18049 if (complain & tf_error)
18050 error ("%qT is a variably modified type", t);
18051 return true;
18054 /* Class template and alias template arguments should be OK. */
18055 else if (DECL_TYPE_TEMPLATE_P (t))
18057 /* A non-type argument of integral or enumerated type must be a
18058 constant. */
18059 else if (TREE_TYPE (t)
18060 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
18061 && !REFERENCE_REF_P (t)
18062 && !TREE_CONSTANT (t))
18064 if (complain & tf_error)
18065 error ("integral expression %qE is not constant", t);
18066 return true;
18068 return false;
18071 static bool
18072 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
18074 int ix, len = DECL_NTPARMS (tmpl);
18075 bool result = false;
18077 for (ix = 0; ix != len; ix++)
18079 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
18080 result = true;
18082 if (result && (complain & tf_error))
18083 error (" trying to instantiate %qD", tmpl);
18084 return result;
18087 /* We're out of SFINAE context now, so generate diagnostics for the access
18088 errors we saw earlier when instantiating D from TMPL and ARGS. */
18090 static void
18091 recheck_decl_substitution (tree d, tree tmpl, tree args)
18093 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
18094 tree type = TREE_TYPE (pattern);
18095 location_t loc = input_location;
18097 push_access_scope (d);
18098 push_deferring_access_checks (dk_no_deferred);
18099 input_location = DECL_SOURCE_LOCATION (pattern);
18100 tsubst (type, args, tf_warning_or_error, d);
18101 input_location = loc;
18102 pop_deferring_access_checks ();
18103 pop_access_scope (d);
18106 /* Instantiate the indicated variable, function, or alias template TMPL with
18107 the template arguments in TARG_PTR. */
18109 static tree
18110 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
18112 tree targ_ptr = orig_args;
18113 tree fndecl;
18114 tree gen_tmpl;
18115 tree spec;
18116 bool access_ok = true;
18118 if (tmpl == error_mark_node)
18119 return error_mark_node;
18121 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
18123 /* If this function is a clone, handle it specially. */
18124 if (DECL_CLONED_FUNCTION_P (tmpl))
18126 tree spec;
18127 tree clone;
18129 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
18130 DECL_CLONED_FUNCTION. */
18131 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
18132 targ_ptr, complain);
18133 if (spec == error_mark_node)
18134 return error_mark_node;
18136 /* Look for the clone. */
18137 FOR_EACH_CLONE (clone, spec)
18138 if (DECL_NAME (clone) == DECL_NAME (tmpl))
18139 return clone;
18140 /* We should always have found the clone by now. */
18141 gcc_unreachable ();
18142 return NULL_TREE;
18145 if (targ_ptr == error_mark_node)
18146 return error_mark_node;
18148 /* Check to see if we already have this specialization. */
18149 gen_tmpl = most_general_template (tmpl);
18150 if (TMPL_ARGS_DEPTH (targ_ptr)
18151 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
18152 /* targ_ptr only has the innermost template args, so add the outer ones
18153 from tmpl, which could be either a partial instantiation or gen_tmpl (in
18154 the case of a non-dependent call within a template definition). */
18155 targ_ptr = (add_outermost_template_args
18156 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
18157 targ_ptr));
18159 /* It would be nice to avoid hashing here and then again in tsubst_decl,
18160 but it doesn't seem to be on the hot path. */
18161 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
18163 gcc_assert (tmpl == gen_tmpl
18164 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
18165 == spec)
18166 || fndecl == NULL_TREE);
18168 if (spec != NULL_TREE)
18170 if (FNDECL_HAS_ACCESS_ERRORS (spec))
18172 if (complain & tf_error)
18173 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
18174 return error_mark_node;
18176 return spec;
18179 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
18180 complain))
18181 return error_mark_node;
18183 /* We are building a FUNCTION_DECL, during which the access of its
18184 parameters and return types have to be checked. However this
18185 FUNCTION_DECL which is the desired context for access checking
18186 is not built yet. We solve this chicken-and-egg problem by
18187 deferring all checks until we have the FUNCTION_DECL. */
18188 push_deferring_access_checks (dk_deferred);
18190 /* Instantiation of the function happens in the context of the function
18191 template, not the context of the overload resolution we're doing. */
18192 push_to_top_level ();
18193 /* If there are dependent arguments, e.g. because we're doing partial
18194 ordering, make sure processing_template_decl stays set. */
18195 if (uses_template_parms (targ_ptr))
18196 ++processing_template_decl;
18197 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18199 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
18200 complain, gen_tmpl, true);
18201 push_nested_class (ctx);
18204 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
18206 fndecl = NULL_TREE;
18207 if (VAR_P (pattern))
18209 /* We need to determine if we're using a partial or explicit
18210 specialization now, because the type of the variable could be
18211 different. */
18212 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
18213 tree elt = most_specialized_partial_spec (tid, complain);
18214 if (elt == error_mark_node)
18215 pattern = error_mark_node;
18216 else if (elt)
18218 tree partial_tmpl = TREE_VALUE (elt);
18219 tree partial_args = TREE_PURPOSE (elt);
18220 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
18221 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
18225 /* Substitute template parameters to obtain the specialization. */
18226 if (fndecl == NULL_TREE)
18227 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
18228 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18229 pop_nested_class ();
18230 pop_from_top_level ();
18232 if (fndecl == error_mark_node)
18234 pop_deferring_access_checks ();
18235 return error_mark_node;
18238 /* The DECL_TI_TEMPLATE should always be the immediate parent
18239 template, not the most general template. */
18240 DECL_TI_TEMPLATE (fndecl) = tmpl;
18241 DECL_TI_ARGS (fndecl) = targ_ptr;
18243 /* Now we know the specialization, compute access previously
18244 deferred. Do no access control for inheriting constructors,
18245 as we already checked access for the inherited constructor. */
18246 if (!(flag_new_inheriting_ctors
18247 && DECL_INHERITED_CTOR (fndecl)))
18249 push_access_scope (fndecl);
18250 if (!perform_deferred_access_checks (complain))
18251 access_ok = false;
18252 pop_access_scope (fndecl);
18254 pop_deferring_access_checks ();
18256 /* If we've just instantiated the main entry point for a function,
18257 instantiate all the alternate entry points as well. We do this
18258 by cloning the instantiation of the main entry point, not by
18259 instantiating the template clones. */
18260 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
18261 clone_function_decl (fndecl, /*update_methods=*/false);
18263 if (!access_ok)
18265 if (!(complain & tf_error))
18267 /* Remember to reinstantiate when we're out of SFINAE so the user
18268 can see the errors. */
18269 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
18271 return error_mark_node;
18273 return fndecl;
18276 /* Wrapper for instantiate_template_1. */
18278 tree
18279 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
18281 tree ret;
18282 timevar_push (TV_TEMPLATE_INST);
18283 ret = instantiate_template_1 (tmpl, orig_args, complain);
18284 timevar_pop (TV_TEMPLATE_INST);
18285 return ret;
18288 /* Instantiate the alias template TMPL with ARGS. Also push a template
18289 instantiation level, which instantiate_template doesn't do because
18290 functions and variables have sufficient context established by the
18291 callers. */
18293 static tree
18294 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
18296 struct pending_template *old_last_pend = last_pending_template;
18297 struct tinst_level *old_error_tinst = last_error_tinst_level;
18298 if (tmpl == error_mark_node || args == error_mark_node)
18299 return error_mark_node;
18300 tree tinst = build_tree_list (tmpl, args);
18301 if (!push_tinst_level (tinst))
18303 ggc_free (tinst);
18304 return error_mark_node;
18307 args =
18308 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
18309 args, tmpl, complain,
18310 /*require_all_args=*/true,
18311 /*use_default_args=*/true);
18313 tree r = instantiate_template (tmpl, args, complain);
18314 pop_tinst_level ();
18315 /* We can't free this if a pending_template entry or last_error_tinst_level
18316 is pointing at it. */
18317 if (last_pending_template == old_last_pend
18318 && last_error_tinst_level == old_error_tinst)
18319 ggc_free (tinst);
18321 return r;
18324 /* PARM is a template parameter pack for FN. Returns true iff
18325 PARM is used in a deducible way in the argument list of FN. */
18327 static bool
18328 pack_deducible_p (tree parm, tree fn)
18330 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
18331 for (; t; t = TREE_CHAIN (t))
18333 tree type = TREE_VALUE (t);
18334 tree packs;
18335 if (!PACK_EXPANSION_P (type))
18336 continue;
18337 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
18338 packs; packs = TREE_CHAIN (packs))
18339 if (template_args_equal (TREE_VALUE (packs), parm))
18341 /* The template parameter pack is used in a function parameter
18342 pack. If this is the end of the parameter list, the
18343 template parameter pack is deducible. */
18344 if (TREE_CHAIN (t) == void_list_node)
18345 return true;
18346 else
18347 /* Otherwise, not. Well, it could be deduced from
18348 a non-pack parameter, but doing so would end up with
18349 a deduction mismatch, so don't bother. */
18350 return false;
18353 /* The template parameter pack isn't used in any function parameter
18354 packs, but it might be used deeper, e.g. tuple<Args...>. */
18355 return true;
18358 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
18359 NARGS elements of the arguments that are being used when calling
18360 it. TARGS is a vector into which the deduced template arguments
18361 are placed.
18363 Returns either a FUNCTION_DECL for the matching specialization of FN or
18364 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
18365 true, diagnostics will be printed to explain why it failed.
18367 If FN is a conversion operator, or we are trying to produce a specific
18368 specialization, RETURN_TYPE is the return type desired.
18370 The EXPLICIT_TARGS are explicit template arguments provided via a
18371 template-id.
18373 The parameter STRICT is one of:
18375 DEDUCE_CALL:
18376 We are deducing arguments for a function call, as in
18377 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
18378 deducing arguments for a call to the result of a conversion
18379 function template, as in [over.call.object].
18381 DEDUCE_CONV:
18382 We are deducing arguments for a conversion function, as in
18383 [temp.deduct.conv].
18385 DEDUCE_EXACT:
18386 We are deducing arguments when doing an explicit instantiation
18387 as in [temp.explicit], when determining an explicit specialization
18388 as in [temp.expl.spec], or when taking the address of a function
18389 template, as in [temp.deduct.funcaddr]. */
18391 tree
18392 fn_type_unification (tree fn,
18393 tree explicit_targs,
18394 tree targs,
18395 const tree *args,
18396 unsigned int nargs,
18397 tree return_type,
18398 unification_kind_t strict,
18399 int flags,
18400 bool explain_p,
18401 bool decltype_p)
18403 tree parms;
18404 tree fntype;
18405 tree decl = NULL_TREE;
18406 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
18407 bool ok;
18408 static int deduction_depth;
18409 struct pending_template *old_last_pend = last_pending_template;
18410 struct tinst_level *old_error_tinst = last_error_tinst_level;
18412 tree orig_fn = fn;
18413 if (flag_new_inheriting_ctors)
18414 fn = strip_inheriting_ctors (fn);
18416 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
18417 tree tinst;
18418 tree r = error_mark_node;
18420 tree full_targs = targs;
18421 if (TMPL_ARGS_DEPTH (targs)
18422 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
18423 full_targs = (add_outermost_template_args
18424 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
18425 targs));
18427 if (decltype_p)
18428 complain |= tf_decltype;
18430 /* In C++0x, it's possible to have a function template whose type depends
18431 on itself recursively. This is most obvious with decltype, but can also
18432 occur with enumeration scope (c++/48969). So we need to catch infinite
18433 recursion and reject the substitution at deduction time; this function
18434 will return error_mark_node for any repeated substitution.
18436 This also catches excessive recursion such as when f<N> depends on
18437 f<N-1> across all integers, and returns error_mark_node for all the
18438 substitutions back up to the initial one.
18440 This is, of course, not reentrant. */
18441 if (excessive_deduction_depth)
18442 return error_mark_node;
18443 tinst = build_tree_list (fn, NULL_TREE);
18444 ++deduction_depth;
18446 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
18448 fntype = TREE_TYPE (fn);
18449 if (explicit_targs)
18451 /* [temp.deduct]
18453 The specified template arguments must match the template
18454 parameters in kind (i.e., type, nontype, template), and there
18455 must not be more arguments than there are parameters;
18456 otherwise type deduction fails.
18458 Nontype arguments must match the types of the corresponding
18459 nontype template parameters, or must be convertible to the
18460 types of the corresponding nontype parameters as specified in
18461 _temp.arg.nontype_, otherwise type deduction fails.
18463 All references in the function type of the function template
18464 to the corresponding template parameters are replaced by the
18465 specified template argument values. If a substitution in a
18466 template parameter or in the function type of the function
18467 template results in an invalid type, type deduction fails. */
18468 int i, len = TREE_VEC_LENGTH (tparms);
18469 location_t loc = input_location;
18470 bool incomplete = false;
18472 if (explicit_targs == error_mark_node)
18473 goto fail;
18475 if (TMPL_ARGS_DEPTH (explicit_targs)
18476 < TMPL_ARGS_DEPTH (full_targs))
18477 explicit_targs = add_outermost_template_args (full_targs,
18478 explicit_targs);
18480 /* Adjust any explicit template arguments before entering the
18481 substitution context. */
18482 explicit_targs
18483 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
18484 complain,
18485 /*require_all_args=*/false,
18486 /*use_default_args=*/false));
18487 if (explicit_targs == error_mark_node)
18488 goto fail;
18490 /* Substitute the explicit args into the function type. This is
18491 necessary so that, for instance, explicitly declared function
18492 arguments can match null pointed constants. If we were given
18493 an incomplete set of explicit args, we must not do semantic
18494 processing during substitution as we could create partial
18495 instantiations. */
18496 for (i = 0; i < len; i++)
18498 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
18499 bool parameter_pack = false;
18500 tree targ = TREE_VEC_ELT (explicit_targs, i);
18502 /* Dig out the actual parm. */
18503 if (TREE_CODE (parm) == TYPE_DECL
18504 || TREE_CODE (parm) == TEMPLATE_DECL)
18506 parm = TREE_TYPE (parm);
18507 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
18509 else if (TREE_CODE (parm) == PARM_DECL)
18511 parm = DECL_INITIAL (parm);
18512 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
18515 if (!parameter_pack && targ == NULL_TREE)
18516 /* No explicit argument for this template parameter. */
18517 incomplete = true;
18519 if (parameter_pack && pack_deducible_p (parm, fn))
18521 /* Mark the argument pack as "incomplete". We could
18522 still deduce more arguments during unification.
18523 We remove this mark in type_unification_real. */
18524 if (targ)
18526 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
18527 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
18528 = ARGUMENT_PACK_ARGS (targ);
18531 /* We have some incomplete argument packs. */
18532 incomplete = true;
18536 TREE_VALUE (tinst) = explicit_targs;
18537 if (!push_tinst_level (tinst))
18539 excessive_deduction_depth = true;
18540 goto fail;
18542 processing_template_decl += incomplete;
18543 input_location = DECL_SOURCE_LOCATION (fn);
18544 /* Ignore any access checks; we'll see them again in
18545 instantiate_template and they might have the wrong
18546 access path at this point. */
18547 push_deferring_access_checks (dk_deferred);
18548 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
18549 complain | tf_partial | tf_fndecl_type, NULL_TREE);
18550 pop_deferring_access_checks ();
18551 input_location = loc;
18552 processing_template_decl -= incomplete;
18553 pop_tinst_level ();
18555 if (fntype == error_mark_node)
18556 goto fail;
18558 /* Place the explicitly specified arguments in TARGS. */
18559 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
18560 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
18561 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
18564 /* Never do unification on the 'this' parameter. */
18565 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
18567 if (return_type && strict == DEDUCE_CALL)
18569 /* We're deducing for a call to the result of a template conversion
18570 function. The parms we really want are in return_type. */
18571 if (POINTER_TYPE_P (return_type))
18572 return_type = TREE_TYPE (return_type);
18573 parms = TYPE_ARG_TYPES (return_type);
18575 else if (return_type)
18577 tree *new_args;
18579 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
18580 new_args = XALLOCAVEC (tree, nargs + 1);
18581 new_args[0] = return_type;
18582 memcpy (new_args + 1, args, nargs * sizeof (tree));
18583 args = new_args;
18584 ++nargs;
18587 /* We allow incomplete unification without an error message here
18588 because the standard doesn't seem to explicitly prohibit it. Our
18589 callers must be ready to deal with unification failures in any
18590 event. */
18592 TREE_VALUE (tinst) = targs;
18593 /* If we aren't explaining yet, push tinst context so we can see where
18594 any errors (e.g. from class instantiations triggered by instantiation
18595 of default template arguments) come from. If we are explaining, this
18596 context is redundant. */
18597 if (!explain_p && !push_tinst_level (tinst))
18599 excessive_deduction_depth = true;
18600 goto fail;
18603 /* type_unification_real will pass back any access checks from default
18604 template argument substitution. */
18605 vec<deferred_access_check, va_gc> *checks;
18606 checks = NULL;
18608 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
18609 full_targs, parms, args, nargs, /*subr=*/0,
18610 strict, flags, &checks, explain_p);
18611 if (!explain_p)
18612 pop_tinst_level ();
18613 if (!ok)
18614 goto fail;
18616 /* Now that we have bindings for all of the template arguments,
18617 ensure that the arguments deduced for the template template
18618 parameters have compatible template parameter lists. We cannot
18619 check this property before we have deduced all template
18620 arguments, because the template parameter types of a template
18621 template parameter might depend on prior template parameters
18622 deduced after the template template parameter. The following
18623 ill-formed example illustrates this issue:
18625 template<typename T, template<T> class C> void f(C<5>, T);
18627 template<int N> struct X {};
18629 void g() {
18630 f(X<5>(), 5l); // error: template argument deduction fails
18633 The template parameter list of 'C' depends on the template type
18634 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
18635 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
18636 time that we deduce 'C'. */
18637 if (!template_template_parm_bindings_ok_p
18638 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
18640 unify_inconsistent_template_template_parameters (explain_p);
18641 goto fail;
18644 /* All is well so far. Now, check:
18646 [temp.deduct]
18648 When all template arguments have been deduced, all uses of
18649 template parameters in nondeduced contexts are replaced with
18650 the corresponding deduced argument values. If the
18651 substitution results in an invalid type, as described above,
18652 type deduction fails. */
18653 TREE_VALUE (tinst) = targs;
18654 if (!push_tinst_level (tinst))
18656 excessive_deduction_depth = true;
18657 goto fail;
18660 /* Also collect access checks from the instantiation. */
18661 reopen_deferring_access_checks (checks);
18663 decl = instantiate_template (fn, targs, complain);
18665 checks = get_deferred_access_checks ();
18666 pop_deferring_access_checks ();
18668 pop_tinst_level ();
18670 if (decl == error_mark_node)
18671 goto fail;
18673 /* Now perform any access checks encountered during substitution. */
18674 push_access_scope (decl);
18675 ok = perform_access_checks (checks, complain);
18676 pop_access_scope (decl);
18677 if (!ok)
18678 goto fail;
18680 /* If we're looking for an exact match, check that what we got
18681 is indeed an exact match. It might not be if some template
18682 parameters are used in non-deduced contexts. But don't check
18683 for an exact match if we have dependent template arguments;
18684 in that case we're doing partial ordering, and we already know
18685 that we have two candidates that will provide the actual type. */
18686 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
18688 tree substed = TREE_TYPE (decl);
18689 unsigned int i;
18691 tree sarg
18692 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
18693 if (return_type)
18694 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
18695 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
18696 if (!same_type_p (args[i], TREE_VALUE (sarg)))
18698 unify_type_mismatch (explain_p, args[i],
18699 TREE_VALUE (sarg));
18700 goto fail;
18704 /* After doing deduction with the inherited constructor, actually return an
18705 instantiation of the inheriting constructor. */
18706 if (orig_fn != fn)
18707 decl = instantiate_template (orig_fn, targs, complain);
18709 r = decl;
18711 fail:
18712 --deduction_depth;
18713 if (excessive_deduction_depth)
18715 if (deduction_depth == 0)
18716 /* Reset once we're all the way out. */
18717 excessive_deduction_depth = false;
18720 /* We can't free this if a pending_template entry or last_error_tinst_level
18721 is pointing at it. */
18722 if (last_pending_template == old_last_pend
18723 && last_error_tinst_level == old_error_tinst)
18724 ggc_free (tinst);
18726 return r;
18729 /* Adjust types before performing type deduction, as described in
18730 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
18731 sections are symmetric. PARM is the type of a function parameter
18732 or the return type of the conversion function. ARG is the type of
18733 the argument passed to the call, or the type of the value
18734 initialized with the result of the conversion function.
18735 ARG_EXPR is the original argument expression, which may be null. */
18737 static int
18738 maybe_adjust_types_for_deduction (unification_kind_t strict,
18739 tree* parm,
18740 tree* arg,
18741 tree arg_expr)
18743 int result = 0;
18745 switch (strict)
18747 case DEDUCE_CALL:
18748 break;
18750 case DEDUCE_CONV:
18751 /* Swap PARM and ARG throughout the remainder of this
18752 function; the handling is precisely symmetric since PARM
18753 will initialize ARG rather than vice versa. */
18754 std::swap (parm, arg);
18755 break;
18757 case DEDUCE_EXACT:
18758 /* Core issue #873: Do the DR606 thing (see below) for these cases,
18759 too, but here handle it by stripping the reference from PARM
18760 rather than by adding it to ARG. */
18761 if (TREE_CODE (*parm) == REFERENCE_TYPE
18762 && TYPE_REF_IS_RVALUE (*parm)
18763 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
18764 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
18765 && TREE_CODE (*arg) == REFERENCE_TYPE
18766 && !TYPE_REF_IS_RVALUE (*arg))
18767 *parm = TREE_TYPE (*parm);
18768 /* Nothing else to do in this case. */
18769 return 0;
18771 default:
18772 gcc_unreachable ();
18775 if (TREE_CODE (*parm) != REFERENCE_TYPE)
18777 /* [temp.deduct.call]
18779 If P is not a reference type:
18781 --If A is an array type, the pointer type produced by the
18782 array-to-pointer standard conversion (_conv.array_) is
18783 used in place of A for type deduction; otherwise,
18785 --If A is a function type, the pointer type produced by
18786 the function-to-pointer standard conversion
18787 (_conv.func_) is used in place of A for type deduction;
18788 otherwise,
18790 --If A is a cv-qualified type, the top level
18791 cv-qualifiers of A's type are ignored for type
18792 deduction. */
18793 if (TREE_CODE (*arg) == ARRAY_TYPE)
18794 *arg = build_pointer_type (TREE_TYPE (*arg));
18795 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
18796 *arg = build_pointer_type (*arg);
18797 else
18798 *arg = TYPE_MAIN_VARIANT (*arg);
18801 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
18802 reference to a cv-unqualified template parameter that does not represent a
18803 template parameter of a class template (during class template argument
18804 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
18805 an lvalue, the type "lvalue reference to A" is used in place of A for type
18806 deduction. */
18807 if (TREE_CODE (*parm) == REFERENCE_TYPE
18808 && TYPE_REF_IS_RVALUE (*parm)
18809 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
18810 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
18811 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
18812 && (arg_expr ? lvalue_p (arg_expr)
18813 /* try_one_overload doesn't provide an arg_expr, but
18814 functions are always lvalues. */
18815 : TREE_CODE (*arg) == FUNCTION_TYPE))
18816 *arg = build_reference_type (*arg);
18818 /* [temp.deduct.call]
18820 If P is a cv-qualified type, the top level cv-qualifiers
18821 of P's type are ignored for type deduction. If P is a
18822 reference type, the type referred to by P is used for
18823 type deduction. */
18824 *parm = TYPE_MAIN_VARIANT (*parm);
18825 if (TREE_CODE (*parm) == REFERENCE_TYPE)
18827 *parm = TREE_TYPE (*parm);
18828 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
18831 /* DR 322. For conversion deduction, remove a reference type on parm
18832 too (which has been swapped into ARG). */
18833 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
18834 *arg = TREE_TYPE (*arg);
18836 return result;
18839 /* Subroutine of unify_one_argument. PARM is a function parameter of a
18840 template which does contain any deducible template parameters; check if
18841 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
18842 unify_one_argument. */
18844 static int
18845 check_non_deducible_conversion (tree parm, tree arg, int strict,
18846 int flags, bool explain_p)
18848 tree type;
18850 if (!TYPE_P (arg))
18851 type = TREE_TYPE (arg);
18852 else
18853 type = arg;
18855 if (same_type_p (parm, type))
18856 return unify_success (explain_p);
18858 if (strict == DEDUCE_CONV)
18860 if (can_convert_arg (type, parm, NULL_TREE, flags,
18861 explain_p ? tf_warning_or_error : tf_none))
18862 return unify_success (explain_p);
18864 else if (strict != DEDUCE_EXACT)
18866 if (can_convert_arg (parm, type,
18867 TYPE_P (arg) ? NULL_TREE : arg,
18868 flags, explain_p ? tf_warning_or_error : tf_none))
18869 return unify_success (explain_p);
18872 if (strict == DEDUCE_EXACT)
18873 return unify_type_mismatch (explain_p, parm, arg);
18874 else
18875 return unify_arg_conversion (explain_p, parm, type, arg);
18878 static bool uses_deducible_template_parms (tree type);
18880 /* Returns true iff the expression EXPR is one from which a template
18881 argument can be deduced. In other words, if it's an undecorated
18882 use of a template non-type parameter. */
18884 static bool
18885 deducible_expression (tree expr)
18887 /* Strip implicit conversions. */
18888 while (CONVERT_EXPR_P (expr))
18889 expr = TREE_OPERAND (expr, 0);
18890 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
18893 /* Returns true iff the array domain DOMAIN uses a template parameter in a
18894 deducible way; that is, if it has a max value of <PARM> - 1. */
18896 static bool
18897 deducible_array_bound (tree domain)
18899 if (domain == NULL_TREE)
18900 return false;
18902 tree max = TYPE_MAX_VALUE (domain);
18903 if (TREE_CODE (max) != MINUS_EXPR)
18904 return false;
18906 return deducible_expression (TREE_OPERAND (max, 0));
18909 /* Returns true iff the template arguments ARGS use a template parameter
18910 in a deducible way. */
18912 static bool
18913 deducible_template_args (tree args)
18915 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
18917 bool deducible;
18918 tree elt = TREE_VEC_ELT (args, i);
18919 if (ARGUMENT_PACK_P (elt))
18920 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
18921 else
18923 if (PACK_EXPANSION_P (elt))
18924 elt = PACK_EXPANSION_PATTERN (elt);
18925 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
18926 deducible = true;
18927 else if (TYPE_P (elt))
18928 deducible = uses_deducible_template_parms (elt);
18929 else
18930 deducible = deducible_expression (elt);
18932 if (deducible)
18933 return true;
18935 return false;
18938 /* Returns true iff TYPE contains any deducible references to template
18939 parameters, as per 14.8.2.5. */
18941 static bool
18942 uses_deducible_template_parms (tree type)
18944 if (PACK_EXPANSION_P (type))
18945 type = PACK_EXPANSION_PATTERN (type);
18947 /* T
18948 cv-list T
18949 TT<T>
18950 TT<i>
18951 TT<> */
18952 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
18953 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
18954 return true;
18956 /* T*
18958 T&& */
18959 if (POINTER_TYPE_P (type))
18960 return uses_deducible_template_parms (TREE_TYPE (type));
18962 /* T[integer-constant ]
18963 type [i] */
18964 if (TREE_CODE (type) == ARRAY_TYPE)
18965 return (uses_deducible_template_parms (TREE_TYPE (type))
18966 || deducible_array_bound (TYPE_DOMAIN (type)));
18968 /* T type ::*
18969 type T::*
18970 T T::*
18971 T (type ::*)()
18972 type (T::*)()
18973 type (type ::*)(T)
18974 type (T::*)(T)
18975 T (type ::*)(T)
18976 T (T::*)()
18977 T (T::*)(T) */
18978 if (TYPE_PTRMEM_P (type))
18979 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
18980 || (uses_deducible_template_parms
18981 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
18983 /* template-name <T> (where template-name refers to a class template)
18984 template-name <i> (where template-name refers to a class template) */
18985 if (CLASS_TYPE_P (type)
18986 && CLASSTYPE_TEMPLATE_INFO (type)
18987 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
18988 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
18989 (CLASSTYPE_TI_ARGS (type)));
18991 /* type (T)
18993 T(T) */
18994 if (TREE_CODE (type) == FUNCTION_TYPE
18995 || TREE_CODE (type) == METHOD_TYPE)
18997 if (uses_deducible_template_parms (TREE_TYPE (type)))
18998 return true;
18999 tree parm = TYPE_ARG_TYPES (type);
19000 if (TREE_CODE (type) == METHOD_TYPE)
19001 parm = TREE_CHAIN (parm);
19002 for (; parm; parm = TREE_CHAIN (parm))
19003 if (uses_deducible_template_parms (TREE_VALUE (parm)))
19004 return true;
19007 return false;
19010 /* Subroutine of type_unification_real and unify_pack_expansion to
19011 handle unification of a single P/A pair. Parameters are as
19012 for those functions. */
19014 static int
19015 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
19016 int subr, unification_kind_t strict,
19017 bool explain_p)
19019 tree arg_expr = NULL_TREE;
19020 int arg_strict;
19022 if (arg == error_mark_node || parm == error_mark_node)
19023 return unify_invalid (explain_p);
19024 if (arg == unknown_type_node)
19025 /* We can't deduce anything from this, but we might get all the
19026 template args from other function args. */
19027 return unify_success (explain_p);
19029 /* Implicit conversions (Clause 4) will be performed on a function
19030 argument to convert it to the type of the corresponding function
19031 parameter if the parameter type contains no template-parameters that
19032 participate in template argument deduction. */
19033 if (strict != DEDUCE_EXACT
19034 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
19035 /* For function parameters with no deducible template parameters,
19036 just return. We'll check non-dependent conversions later. */
19037 return unify_success (explain_p);
19039 switch (strict)
19041 case DEDUCE_CALL:
19042 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
19043 | UNIFY_ALLOW_MORE_CV_QUAL
19044 | UNIFY_ALLOW_DERIVED);
19045 break;
19047 case DEDUCE_CONV:
19048 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
19049 break;
19051 case DEDUCE_EXACT:
19052 arg_strict = UNIFY_ALLOW_NONE;
19053 break;
19055 default:
19056 gcc_unreachable ();
19059 /* We only do these transformations if this is the top-level
19060 parameter_type_list in a call or declaration matching; in other
19061 situations (nested function declarators, template argument lists) we
19062 won't be comparing a type to an expression, and we don't do any type
19063 adjustments. */
19064 if (!subr)
19066 if (!TYPE_P (arg))
19068 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
19069 if (type_unknown_p (arg))
19071 /* [temp.deduct.type] A template-argument can be
19072 deduced from a pointer to function or pointer
19073 to member function argument if the set of
19074 overloaded functions does not contain function
19075 templates and at most one of a set of
19076 overloaded functions provides a unique
19077 match. */
19079 if (resolve_overloaded_unification
19080 (tparms, targs, parm, arg, strict,
19081 arg_strict, explain_p))
19082 return unify_success (explain_p);
19083 return unify_overload_resolution_failure (explain_p, arg);
19086 arg_expr = arg;
19087 arg = unlowered_expr_type (arg);
19088 if (arg == error_mark_node)
19089 return unify_invalid (explain_p);
19092 arg_strict |=
19093 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
19095 else
19096 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
19097 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
19098 return unify_template_argument_mismatch (explain_p, parm, arg);
19100 /* For deduction from an init-list we need the actual list. */
19101 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
19102 arg = arg_expr;
19103 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
19106 /* for_each_template_parm callback that always returns 0. */
19108 static int
19109 zero_r (tree, void *)
19111 return 0;
19114 /* for_each_template_parm any_fn callback to handle deduction of a template
19115 type argument from the type of an array bound. */
19117 static int
19118 array_deduction_r (tree t, void *data)
19120 tree_pair_p d = (tree_pair_p)data;
19121 tree &tparms = d->purpose;
19122 tree &targs = d->value;
19124 if (TREE_CODE (t) == ARRAY_TYPE)
19125 if (tree dom = TYPE_DOMAIN (t))
19126 if (tree max = TYPE_MAX_VALUE (dom))
19128 if (TREE_CODE (max) == MINUS_EXPR)
19129 max = TREE_OPERAND (max, 0);
19130 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
19131 unify (tparms, targs, TREE_TYPE (max), size_type_node,
19132 UNIFY_ALLOW_NONE, /*explain*/false);
19135 /* Keep walking. */
19136 return 0;
19139 /* Try to deduce any not-yet-deduced template type arguments from the type of
19140 an array bound. This is handled separately from unify because 14.8.2.5 says
19141 "The type of a type parameter is only deduced from an array bound if it is
19142 not otherwise deduced." */
19144 static void
19145 try_array_deduction (tree tparms, tree targs, tree parm)
19147 tree_pair_s data = { tparms, targs };
19148 hash_set<tree> visited;
19149 for_each_template_parm (parm, zero_r, &data, &visited,
19150 /*nondeduced*/false, array_deduction_r);
19153 /* Most parms like fn_type_unification.
19155 If SUBR is 1, we're being called recursively (to unify the
19156 arguments of a function or method parameter of a function
19157 template).
19159 CHECKS is a pointer to a vector of access checks encountered while
19160 substituting default template arguments. */
19162 static int
19163 type_unification_real (tree tparms,
19164 tree full_targs,
19165 tree xparms,
19166 const tree *xargs,
19167 unsigned int xnargs,
19168 int subr,
19169 unification_kind_t strict,
19170 int flags,
19171 vec<deferred_access_check, va_gc> **checks,
19172 bool explain_p)
19174 tree parm, arg;
19175 int i;
19176 int ntparms = TREE_VEC_LENGTH (tparms);
19177 int saw_undeduced = 0;
19178 tree parms;
19179 const tree *args;
19180 unsigned int nargs;
19181 unsigned int ia;
19183 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
19184 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
19185 gcc_assert (ntparms > 0);
19187 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
19189 /* Reset the number of non-defaulted template arguments contained
19190 in TARGS. */
19191 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
19193 again:
19194 parms = xparms;
19195 args = xargs;
19196 nargs = xnargs;
19198 ia = 0;
19199 while (parms && parms != void_list_node
19200 && ia < nargs)
19202 parm = TREE_VALUE (parms);
19204 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19205 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
19206 /* For a function parameter pack that occurs at the end of the
19207 parameter-declaration-list, the type A of each remaining
19208 argument of the call is compared with the type P of the
19209 declarator-id of the function parameter pack. */
19210 break;
19212 parms = TREE_CHAIN (parms);
19214 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19215 /* For a function parameter pack that does not occur at the
19216 end of the parameter-declaration-list, the type of the
19217 parameter pack is a non-deduced context. */
19218 continue;
19220 arg = args[ia];
19221 ++ia;
19223 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
19224 explain_p))
19225 return 1;
19228 if (parms
19229 && parms != void_list_node
19230 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
19232 /* Unify the remaining arguments with the pack expansion type. */
19233 tree argvec;
19234 tree parmvec = make_tree_vec (1);
19236 /* Allocate a TREE_VEC and copy in all of the arguments */
19237 argvec = make_tree_vec (nargs - ia);
19238 for (i = 0; ia < nargs; ++ia, ++i)
19239 TREE_VEC_ELT (argvec, i) = args[ia];
19241 /* Copy the parameter into parmvec. */
19242 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
19243 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
19244 /*subr=*/subr, explain_p))
19245 return 1;
19247 /* Advance to the end of the list of parameters. */
19248 parms = TREE_CHAIN (parms);
19251 /* Fail if we've reached the end of the parm list, and more args
19252 are present, and the parm list isn't variadic. */
19253 if (ia < nargs && parms == void_list_node)
19254 return unify_too_many_arguments (explain_p, nargs, ia);
19255 /* Fail if parms are left and they don't have default values and
19256 they aren't all deduced as empty packs (c++/57397). This is
19257 consistent with sufficient_parms_p. */
19258 if (parms && parms != void_list_node
19259 && TREE_PURPOSE (parms) == NULL_TREE)
19261 unsigned int count = nargs;
19262 tree p = parms;
19263 bool type_pack_p;
19266 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
19267 if (!type_pack_p)
19268 count++;
19269 p = TREE_CHAIN (p);
19271 while (p && p != void_list_node);
19272 if (count != nargs)
19273 return unify_too_few_arguments (explain_p, ia, count,
19274 type_pack_p);
19277 if (!subr)
19279 tsubst_flags_t complain = (explain_p
19280 ? tf_warning_or_error
19281 : tf_none);
19282 bool tried_array_deduction = (cxx_dialect < cxx1z);
19284 for (i = 0; i < ntparms; i++)
19286 tree targ = TREE_VEC_ELT (targs, i);
19287 tree tparm = TREE_VEC_ELT (tparms, i);
19289 /* Clear the "incomplete" flags on all argument packs now so that
19290 substituting them into later default arguments works. */
19291 if (targ && ARGUMENT_PACK_P (targ))
19293 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
19294 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
19297 if (targ || tparm == error_mark_node)
19298 continue;
19299 tparm = TREE_VALUE (tparm);
19301 if (TREE_CODE (tparm) == TYPE_DECL
19302 && !tried_array_deduction)
19304 try_array_deduction (tparms, targs, xparms);
19305 tried_array_deduction = true;
19306 if (TREE_VEC_ELT (targs, i))
19307 continue;
19310 /* If this is an undeduced nontype parameter that depends on
19311 a type parameter, try another pass; its type may have been
19312 deduced from a later argument than the one from which
19313 this parameter can be deduced. */
19314 if (TREE_CODE (tparm) == PARM_DECL
19315 && uses_template_parms (TREE_TYPE (tparm))
19316 && saw_undeduced < 2)
19318 saw_undeduced = 1;
19319 continue;
19322 /* Core issue #226 (C++0x) [temp.deduct]:
19324 If a template argument has not been deduced, its
19325 default template argument, if any, is used.
19327 When we are in C++98 mode, TREE_PURPOSE will either
19328 be NULL_TREE or ERROR_MARK_NODE, so we do not need
19329 to explicitly check cxx_dialect here. */
19330 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
19331 /* OK, there is a default argument. Wait until after the
19332 conversion check to do substitution. */
19333 continue;
19335 /* If the type parameter is a parameter pack, then it will
19336 be deduced to an empty parameter pack. */
19337 if (template_parameter_pack_p (tparm))
19339 tree arg;
19341 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
19343 arg = make_node (NONTYPE_ARGUMENT_PACK);
19344 TREE_CONSTANT (arg) = 1;
19346 else
19347 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
19349 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
19351 TREE_VEC_ELT (targs, i) = arg;
19352 continue;
19355 return unify_parameter_deduction_failure (explain_p, tparm);
19358 /* DR 1391: All parameters have args, now check non-dependent parms for
19359 convertibility. */
19360 if (saw_undeduced < 2)
19361 for (ia = 0, parms = xparms, args = xargs, nargs = xnargs;
19362 parms && parms != void_list_node && ia < nargs; )
19364 parm = TREE_VALUE (parms);
19366 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19367 && (!TREE_CHAIN (parms)
19368 || TREE_CHAIN (parms) == void_list_node))
19369 /* For a function parameter pack that occurs at the end of the
19370 parameter-declaration-list, the type A of each remaining
19371 argument of the call is compared with the type P of the
19372 declarator-id of the function parameter pack. */
19373 break;
19375 parms = TREE_CHAIN (parms);
19377 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19378 /* For a function parameter pack that does not occur at the
19379 end of the parameter-declaration-list, the type of the
19380 parameter pack is a non-deduced context. */
19381 continue;
19383 arg = args[ia];
19384 ++ia;
19386 if (uses_template_parms (parm))
19387 continue;
19388 if (check_non_deducible_conversion (parm, arg, strict, flags,
19389 explain_p))
19390 return 1;
19393 /* Now substitute into the default template arguments. */
19394 for (i = 0; i < ntparms; i++)
19396 tree targ = TREE_VEC_ELT (targs, i);
19397 tree tparm = TREE_VEC_ELT (tparms, i);
19399 if (targ || tparm == error_mark_node)
19400 continue;
19401 tree parm = TREE_VALUE (tparm);
19403 if (TREE_CODE (parm) == PARM_DECL
19404 && uses_template_parms (TREE_TYPE (parm))
19405 && saw_undeduced < 2)
19406 continue;
19408 tree arg = TREE_PURPOSE (tparm);
19409 reopen_deferring_access_checks (*checks);
19410 location_t save_loc = input_location;
19411 if (DECL_P (parm))
19412 input_location = DECL_SOURCE_LOCATION (parm);
19413 arg = tsubst_template_arg (arg, full_targs, complain, NULL_TREE);
19414 if (!uses_template_parms (arg))
19415 arg = convert_template_argument (parm, arg, full_targs, complain,
19416 i, NULL_TREE);
19417 else if (saw_undeduced < 2)
19418 arg = NULL_TREE;
19419 else
19420 arg = error_mark_node;
19421 input_location = save_loc;
19422 *checks = get_deferred_access_checks ();
19423 pop_deferring_access_checks ();
19424 if (arg == error_mark_node)
19425 return 1;
19426 else if (arg)
19428 TREE_VEC_ELT (targs, i) = arg;
19429 /* The position of the first default template argument,
19430 is also the number of non-defaulted arguments in TARGS.
19431 Record that. */
19432 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19433 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
19437 if (saw_undeduced++ == 1)
19438 goto again;
19441 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19442 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
19444 return unify_success (explain_p);
19447 /* Subroutine of type_unification_real. Args are like the variables
19448 at the call site. ARG is an overloaded function (or template-id);
19449 we try deducing template args from each of the overloads, and if
19450 only one succeeds, we go with that. Modifies TARGS and returns
19451 true on success. */
19453 static bool
19454 resolve_overloaded_unification (tree tparms,
19455 tree targs,
19456 tree parm,
19457 tree arg,
19458 unification_kind_t strict,
19459 int sub_strict,
19460 bool explain_p)
19462 tree tempargs = copy_node (targs);
19463 int good = 0;
19464 tree goodfn = NULL_TREE;
19465 bool addr_p;
19467 if (TREE_CODE (arg) == ADDR_EXPR)
19469 arg = TREE_OPERAND (arg, 0);
19470 addr_p = true;
19472 else
19473 addr_p = false;
19475 if (TREE_CODE (arg) == COMPONENT_REF)
19476 /* Handle `&x' where `x' is some static or non-static member
19477 function name. */
19478 arg = TREE_OPERAND (arg, 1);
19480 if (TREE_CODE (arg) == OFFSET_REF)
19481 arg = TREE_OPERAND (arg, 1);
19483 /* Strip baselink information. */
19484 if (BASELINK_P (arg))
19485 arg = BASELINK_FUNCTIONS (arg);
19487 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
19489 /* If we got some explicit template args, we need to plug them into
19490 the affected templates before we try to unify, in case the
19491 explicit args will completely resolve the templates in question. */
19493 int ok = 0;
19494 tree expl_subargs = TREE_OPERAND (arg, 1);
19495 arg = TREE_OPERAND (arg, 0);
19497 for (lkp_iterator iter (arg); iter; ++iter)
19499 tree fn = *iter;
19500 tree subargs, elem;
19502 if (TREE_CODE (fn) != TEMPLATE_DECL)
19503 continue;
19505 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19506 expl_subargs, NULL_TREE, tf_none,
19507 /*require_all_args=*/true,
19508 /*use_default_args=*/true);
19509 if (subargs != error_mark_node
19510 && !any_dependent_template_arguments_p (subargs))
19512 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
19513 if (try_one_overload (tparms, targs, tempargs, parm,
19514 elem, strict, sub_strict, addr_p, explain_p)
19515 && (!goodfn || !same_type_p (goodfn, elem)))
19517 goodfn = elem;
19518 ++good;
19521 else if (subargs)
19522 ++ok;
19524 /* If no templates (or more than one) are fully resolved by the
19525 explicit arguments, this template-id is a non-deduced context; it
19526 could still be OK if we deduce all template arguments for the
19527 enclosing call through other arguments. */
19528 if (good != 1)
19529 good = ok;
19531 else if (TREE_CODE (arg) != OVERLOAD
19532 && TREE_CODE (arg) != FUNCTION_DECL)
19533 /* If ARG is, for example, "(0, &f)" then its type will be unknown
19534 -- but the deduction does not succeed because the expression is
19535 not just the function on its own. */
19536 return false;
19537 else
19538 for (lkp_iterator iter (arg); iter; ++iter)
19540 tree fn = *iter;
19541 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
19542 strict, sub_strict, addr_p, explain_p)
19543 && (!goodfn || !decls_match (goodfn, fn)))
19545 goodfn = fn;
19546 ++good;
19550 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19551 to function or pointer to member function argument if the set of
19552 overloaded functions does not contain function templates and at most
19553 one of a set of overloaded functions provides a unique match.
19555 So if we found multiple possibilities, we return success but don't
19556 deduce anything. */
19558 if (good == 1)
19560 int i = TREE_VEC_LENGTH (targs);
19561 for (; i--; )
19562 if (TREE_VEC_ELT (tempargs, i))
19564 tree old = TREE_VEC_ELT (targs, i);
19565 tree new_ = TREE_VEC_ELT (tempargs, i);
19566 if (new_ && old && ARGUMENT_PACK_P (old)
19567 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
19568 /* Don't forget explicit template arguments in a pack. */
19569 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
19570 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
19571 TREE_VEC_ELT (targs, i) = new_;
19574 if (good)
19575 return true;
19577 return false;
19580 /* Core DR 115: In contexts where deduction is done and fails, or in
19581 contexts where deduction is not done, if a template argument list is
19582 specified and it, along with any default template arguments, identifies
19583 a single function template specialization, then the template-id is an
19584 lvalue for the function template specialization. */
19586 tree
19587 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
19589 tree expr, offset, baselink;
19590 bool addr;
19592 if (!type_unknown_p (orig_expr))
19593 return orig_expr;
19595 expr = orig_expr;
19596 addr = false;
19597 offset = NULL_TREE;
19598 baselink = NULL_TREE;
19600 if (TREE_CODE (expr) == ADDR_EXPR)
19602 expr = TREE_OPERAND (expr, 0);
19603 addr = true;
19605 if (TREE_CODE (expr) == OFFSET_REF)
19607 offset = expr;
19608 expr = TREE_OPERAND (expr, 1);
19610 if (BASELINK_P (expr))
19612 baselink = expr;
19613 expr = BASELINK_FUNCTIONS (expr);
19616 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
19618 int good = 0;
19619 tree goodfn = NULL_TREE;
19621 /* If we got some explicit template args, we need to plug them into
19622 the affected templates before we try to unify, in case the
19623 explicit args will completely resolve the templates in question. */
19625 tree expl_subargs = TREE_OPERAND (expr, 1);
19626 tree arg = TREE_OPERAND (expr, 0);
19627 tree badfn = NULL_TREE;
19628 tree badargs = NULL_TREE;
19630 for (lkp_iterator iter (arg); iter; ++iter)
19632 tree fn = *iter;
19633 tree subargs, elem;
19635 if (TREE_CODE (fn) != TEMPLATE_DECL)
19636 continue;
19638 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19639 expl_subargs, NULL_TREE, tf_none,
19640 /*require_all_args=*/true,
19641 /*use_default_args=*/true);
19642 if (subargs != error_mark_node
19643 && !any_dependent_template_arguments_p (subargs))
19645 elem = instantiate_template (fn, subargs, tf_none);
19646 if (elem == error_mark_node)
19648 badfn = fn;
19649 badargs = subargs;
19651 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
19653 goodfn = elem;
19654 ++good;
19658 if (good == 1)
19660 mark_used (goodfn);
19661 expr = goodfn;
19662 if (baselink)
19663 expr = build_baselink (BASELINK_BINFO (baselink),
19664 BASELINK_ACCESS_BINFO (baselink),
19665 expr, BASELINK_OPTYPE (baselink));
19666 if (offset)
19668 tree base
19669 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
19670 expr = build_offset_ref (base, expr, addr, complain);
19672 if (addr)
19673 expr = cp_build_addr_expr (expr, complain);
19674 return expr;
19676 else if (good == 0 && badargs && (complain & tf_error))
19677 /* There were no good options and at least one bad one, so let the
19678 user know what the problem is. */
19679 instantiate_template (badfn, badargs, complain);
19681 return orig_expr;
19684 /* Subroutine of resolve_overloaded_unification; does deduction for a single
19685 overload. Fills TARGS with any deduced arguments, or error_mark_node if
19686 different overloads deduce different arguments for a given parm.
19687 ADDR_P is true if the expression for which deduction is being
19688 performed was of the form "& fn" rather than simply "fn".
19690 Returns 1 on success. */
19692 static int
19693 try_one_overload (tree tparms,
19694 tree orig_targs,
19695 tree targs,
19696 tree parm,
19697 tree arg,
19698 unification_kind_t strict,
19699 int sub_strict,
19700 bool addr_p,
19701 bool explain_p)
19703 int nargs;
19704 tree tempargs;
19705 int i;
19707 if (arg == error_mark_node)
19708 return 0;
19710 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19711 to function or pointer to member function argument if the set of
19712 overloaded functions does not contain function templates and at most
19713 one of a set of overloaded functions provides a unique match.
19715 So if this is a template, just return success. */
19717 if (uses_template_parms (arg))
19718 return 1;
19720 if (TREE_CODE (arg) == METHOD_TYPE)
19721 arg = build_ptrmemfunc_type (build_pointer_type (arg));
19722 else if (addr_p)
19723 arg = build_pointer_type (arg);
19725 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
19727 /* We don't copy orig_targs for this because if we have already deduced
19728 some template args from previous args, unify would complain when we
19729 try to deduce a template parameter for the same argument, even though
19730 there isn't really a conflict. */
19731 nargs = TREE_VEC_LENGTH (targs);
19732 tempargs = make_tree_vec (nargs);
19734 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
19735 return 0;
19737 /* First make sure we didn't deduce anything that conflicts with
19738 explicitly specified args. */
19739 for (i = nargs; i--; )
19741 tree elt = TREE_VEC_ELT (tempargs, i);
19742 tree oldelt = TREE_VEC_ELT (orig_targs, i);
19744 if (!elt)
19745 /*NOP*/;
19746 else if (uses_template_parms (elt))
19747 /* Since we're unifying against ourselves, we will fill in
19748 template args used in the function parm list with our own
19749 template parms. Discard them. */
19750 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
19751 else if (oldelt && ARGUMENT_PACK_P (oldelt))
19753 /* Check that the argument at each index of the deduced argument pack
19754 is equivalent to the corresponding explicitly specified argument.
19755 We may have deduced more arguments than were explicitly specified,
19756 and that's OK. */
19758 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
19759 that's wrong if we deduce the same argument pack from multiple
19760 function arguments: it's only incomplete the first time. */
19762 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
19763 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
19765 if (TREE_VEC_LENGTH (deduced_pack)
19766 < TREE_VEC_LENGTH (explicit_pack))
19767 return 0;
19769 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
19770 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
19771 TREE_VEC_ELT (deduced_pack, j)))
19772 return 0;
19774 else if (oldelt && !template_args_equal (oldelt, elt))
19775 return 0;
19778 for (i = nargs; i--; )
19780 tree elt = TREE_VEC_ELT (tempargs, i);
19782 if (elt)
19783 TREE_VEC_ELT (targs, i) = elt;
19786 return 1;
19789 /* PARM is a template class (perhaps with unbound template
19790 parameters). ARG is a fully instantiated type. If ARG can be
19791 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
19792 TARGS are as for unify. */
19794 static tree
19795 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
19796 bool explain_p)
19798 tree copy_of_targs;
19800 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
19801 return NULL_TREE;
19802 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19803 /* Matches anything. */;
19804 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
19805 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
19806 return NULL_TREE;
19808 /* We need to make a new template argument vector for the call to
19809 unify. If we used TARGS, we'd clutter it up with the result of
19810 the attempted unification, even if this class didn't work out.
19811 We also don't want to commit ourselves to all the unifications
19812 we've already done, since unification is supposed to be done on
19813 an argument-by-argument basis. In other words, consider the
19814 following pathological case:
19816 template <int I, int J, int K>
19817 struct S {};
19819 template <int I, int J>
19820 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
19822 template <int I, int J, int K>
19823 void f(S<I, J, K>, S<I, I, I>);
19825 void g() {
19826 S<0, 0, 0> s0;
19827 S<0, 1, 2> s2;
19829 f(s0, s2);
19832 Now, by the time we consider the unification involving `s2', we
19833 already know that we must have `f<0, 0, 0>'. But, even though
19834 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
19835 because there are two ways to unify base classes of S<0, 1, 2>
19836 with S<I, I, I>. If we kept the already deduced knowledge, we
19837 would reject the possibility I=1. */
19838 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
19840 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19842 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
19843 return NULL_TREE;
19844 return arg;
19847 /* If unification failed, we're done. */
19848 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
19849 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
19850 return NULL_TREE;
19852 return arg;
19855 /* Given a template type PARM and a class type ARG, find the unique
19856 base type in ARG that is an instance of PARM. We do not examine
19857 ARG itself; only its base-classes. If there is not exactly one
19858 appropriate base class, return NULL_TREE. PARM may be the type of
19859 a partial specialization, as well as a plain template type. Used
19860 by unify. */
19862 static enum template_base_result
19863 get_template_base (tree tparms, tree targs, tree parm, tree arg,
19864 bool explain_p, tree *result)
19866 tree rval = NULL_TREE;
19867 tree binfo;
19869 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
19871 binfo = TYPE_BINFO (complete_type (arg));
19872 if (!binfo)
19874 /* The type could not be completed. */
19875 *result = NULL_TREE;
19876 return tbr_incomplete_type;
19879 /* Walk in inheritance graph order. The search order is not
19880 important, and this avoids multiple walks of virtual bases. */
19881 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
19883 tree r = try_class_unification (tparms, targs, parm,
19884 BINFO_TYPE (binfo), explain_p);
19886 if (r)
19888 /* If there is more than one satisfactory baseclass, then:
19890 [temp.deduct.call]
19892 If they yield more than one possible deduced A, the type
19893 deduction fails.
19895 applies. */
19896 if (rval && !same_type_p (r, rval))
19898 *result = NULL_TREE;
19899 return tbr_ambiguous_baseclass;
19902 rval = r;
19906 *result = rval;
19907 return tbr_success;
19910 /* Returns the level of DECL, which declares a template parameter. */
19912 static int
19913 template_decl_level (tree decl)
19915 switch (TREE_CODE (decl))
19917 case TYPE_DECL:
19918 case TEMPLATE_DECL:
19919 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
19921 case PARM_DECL:
19922 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
19924 default:
19925 gcc_unreachable ();
19927 return 0;
19930 /* Decide whether ARG can be unified with PARM, considering only the
19931 cv-qualifiers of each type, given STRICT as documented for unify.
19932 Returns nonzero iff the unification is OK on that basis. */
19934 static int
19935 check_cv_quals_for_unify (int strict, tree arg, tree parm)
19937 int arg_quals = cp_type_quals (arg);
19938 int parm_quals = cp_type_quals (parm);
19940 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
19941 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
19943 /* Although a CVR qualifier is ignored when being applied to a
19944 substituted template parameter ([8.3.2]/1 for example), that
19945 does not allow us to unify "const T" with "int&" because both
19946 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
19947 It is ok when we're allowing additional CV qualifiers
19948 at the outer level [14.8.2.1]/3,1st bullet. */
19949 if ((TREE_CODE (arg) == REFERENCE_TYPE
19950 || TREE_CODE (arg) == FUNCTION_TYPE
19951 || TREE_CODE (arg) == METHOD_TYPE)
19952 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
19953 return 0;
19955 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
19956 && (parm_quals & TYPE_QUAL_RESTRICT))
19957 return 0;
19960 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
19961 && (arg_quals & parm_quals) != parm_quals)
19962 return 0;
19964 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
19965 && (parm_quals & arg_quals) != arg_quals)
19966 return 0;
19968 return 1;
19971 /* Determines the LEVEL and INDEX for the template parameter PARM. */
19972 void
19973 template_parm_level_and_index (tree parm, int* level, int* index)
19975 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
19976 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
19977 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19979 *index = TEMPLATE_TYPE_IDX (parm);
19980 *level = TEMPLATE_TYPE_LEVEL (parm);
19982 else
19984 *index = TEMPLATE_PARM_IDX (parm);
19985 *level = TEMPLATE_PARM_LEVEL (parm);
19989 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
19990 do { \
19991 if (unify (TP, TA, P, A, S, EP)) \
19992 return 1; \
19993 } while (0)
19995 /* Unifies the remaining arguments in PACKED_ARGS with the pack
19996 expansion at the end of PACKED_PARMS. Returns 0 if the type
19997 deduction succeeds, 1 otherwise. STRICT is the same as in
19998 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
19999 function call argument list. We'll need to adjust the arguments to make them
20000 types. SUBR tells us if this is from a recursive call to
20001 type_unification_real, or for comparing two template argument
20002 lists. */
20004 static int
20005 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
20006 tree packed_args, unification_kind_t strict,
20007 bool subr, bool explain_p)
20009 tree parm
20010 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
20011 tree pattern = PACK_EXPANSION_PATTERN (parm);
20012 tree pack, packs = NULL_TREE;
20013 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
20015 packed_args = expand_template_argument_pack (packed_args);
20017 int len = TREE_VEC_LENGTH (packed_args);
20019 /* Determine the parameter packs we will be deducing from the
20020 pattern, and record their current deductions. */
20021 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
20022 pack; pack = TREE_CHAIN (pack))
20024 tree parm_pack = TREE_VALUE (pack);
20025 int idx, level;
20027 /* Determine the index and level of this parameter pack. */
20028 template_parm_level_and_index (parm_pack, &level, &idx);
20030 /* Keep track of the parameter packs and their corresponding
20031 argument packs. */
20032 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
20033 TREE_TYPE (packs) = make_tree_vec (len - start);
20036 /* Loop through all of the arguments that have not yet been
20037 unified and unify each with the pattern. */
20038 for (i = start; i < len; i++)
20040 tree parm;
20041 bool any_explicit = false;
20042 tree arg = TREE_VEC_ELT (packed_args, i);
20044 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
20045 or the element of its argument pack at the current index if
20046 this argument was explicitly specified. */
20047 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20049 int idx, level;
20050 tree arg, pargs;
20051 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20053 arg = NULL_TREE;
20054 if (TREE_VALUE (pack)
20055 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
20056 && (i - start < TREE_VEC_LENGTH (pargs)))
20058 any_explicit = true;
20059 arg = TREE_VEC_ELT (pargs, i - start);
20061 TMPL_ARG (targs, level, idx) = arg;
20064 /* If we had explicit template arguments, substitute them into the
20065 pattern before deduction. */
20066 if (any_explicit)
20068 /* Some arguments might still be unspecified or dependent. */
20069 bool dependent;
20070 ++processing_template_decl;
20071 dependent = any_dependent_template_arguments_p (targs);
20072 if (!dependent)
20073 --processing_template_decl;
20074 parm = tsubst (pattern, targs,
20075 explain_p ? tf_warning_or_error : tf_none,
20076 NULL_TREE);
20077 if (dependent)
20078 --processing_template_decl;
20079 if (parm == error_mark_node)
20080 return 1;
20082 else
20083 parm = pattern;
20085 /* Unify the pattern with the current argument. */
20086 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
20087 explain_p))
20088 return 1;
20090 /* For each parameter pack, collect the deduced value. */
20091 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20093 int idx, level;
20094 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20096 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
20097 TMPL_ARG (targs, level, idx);
20101 /* Verify that the results of unification with the parameter packs
20102 produce results consistent with what we've seen before, and make
20103 the deduced argument packs available. */
20104 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20106 tree old_pack = TREE_VALUE (pack);
20107 tree new_args = TREE_TYPE (pack);
20108 int i, len = TREE_VEC_LENGTH (new_args);
20109 int idx, level;
20110 bool nondeduced_p = false;
20112 /* By default keep the original deduced argument pack.
20113 If necessary, more specific code is going to update the
20114 resulting deduced argument later down in this function. */
20115 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20116 TMPL_ARG (targs, level, idx) = old_pack;
20118 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
20119 actually deduce anything. */
20120 for (i = 0; i < len && !nondeduced_p; ++i)
20121 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
20122 nondeduced_p = true;
20123 if (nondeduced_p)
20124 continue;
20126 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
20128 /* If we had fewer function args than explicit template args,
20129 just use the explicits. */
20130 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20131 int explicit_len = TREE_VEC_LENGTH (explicit_args);
20132 if (len < explicit_len)
20133 new_args = explicit_args;
20136 if (!old_pack)
20138 tree result;
20139 /* Build the deduced *_ARGUMENT_PACK. */
20140 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
20142 result = make_node (NONTYPE_ARGUMENT_PACK);
20143 TREE_CONSTANT (result) = 1;
20145 else
20146 result = cxx_make_type (TYPE_ARGUMENT_PACK);
20148 SET_ARGUMENT_PACK_ARGS (result, new_args);
20150 /* Note the deduced argument packs for this parameter
20151 pack. */
20152 TMPL_ARG (targs, level, idx) = result;
20154 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
20155 && (ARGUMENT_PACK_ARGS (old_pack)
20156 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
20158 /* We only had the explicitly-provided arguments before, but
20159 now we have a complete set of arguments. */
20160 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20162 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
20163 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
20164 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
20166 else
20168 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
20169 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
20171 if (!comp_template_args (old_args, new_args,
20172 &bad_old_arg, &bad_new_arg))
20173 /* Inconsistent unification of this parameter pack. */
20174 return unify_parameter_pack_inconsistent (explain_p,
20175 bad_old_arg,
20176 bad_new_arg);
20180 return unify_success (explain_p);
20183 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
20184 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
20185 parameters and return value are as for unify. */
20187 static int
20188 unify_array_domain (tree tparms, tree targs,
20189 tree parm_dom, tree arg_dom,
20190 bool explain_p)
20192 tree parm_max;
20193 tree arg_max;
20194 bool parm_cst;
20195 bool arg_cst;
20197 /* Our representation of array types uses "N - 1" as the
20198 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
20199 not an integer constant. We cannot unify arbitrarily
20200 complex expressions, so we eliminate the MINUS_EXPRs
20201 here. */
20202 parm_max = TYPE_MAX_VALUE (parm_dom);
20203 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
20204 if (!parm_cst)
20206 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
20207 parm_max = TREE_OPERAND (parm_max, 0);
20209 arg_max = TYPE_MAX_VALUE (arg_dom);
20210 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
20211 if (!arg_cst)
20213 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
20214 trying to unify the type of a variable with the type
20215 of a template parameter. For example:
20217 template <unsigned int N>
20218 void f (char (&) [N]);
20219 int g();
20220 void h(int i) {
20221 char a[g(i)];
20222 f(a);
20225 Here, the type of the ARG will be "int [g(i)]", and
20226 may be a SAVE_EXPR, etc. */
20227 if (TREE_CODE (arg_max) != MINUS_EXPR)
20228 return unify_vla_arg (explain_p, arg_dom);
20229 arg_max = TREE_OPERAND (arg_max, 0);
20232 /* If only one of the bounds used a MINUS_EXPR, compensate
20233 by adding one to the other bound. */
20234 if (parm_cst && !arg_cst)
20235 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
20236 integer_type_node,
20237 parm_max,
20238 integer_one_node);
20239 else if (arg_cst && !parm_cst)
20240 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
20241 integer_type_node,
20242 arg_max,
20243 integer_one_node);
20245 return unify (tparms, targs, parm_max, arg_max,
20246 UNIFY_ALLOW_INTEGER, explain_p);
20249 /* Returns whether T, a P or A in unify, is a type, template or expression. */
20251 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
20253 static pa_kind_t
20254 pa_kind (tree t)
20256 if (PACK_EXPANSION_P (t))
20257 t = PACK_EXPANSION_PATTERN (t);
20258 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
20259 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
20260 || DECL_TYPE_TEMPLATE_P (t))
20261 return pa_tmpl;
20262 else if (TYPE_P (t))
20263 return pa_type;
20264 else
20265 return pa_expr;
20268 /* Deduce the value of template parameters. TPARMS is the (innermost)
20269 set of template parameters to a template. TARGS is the bindings
20270 for those template parameters, as determined thus far; TARGS may
20271 include template arguments for outer levels of template parameters
20272 as well. PARM is a parameter to a template function, or a
20273 subcomponent of that parameter; ARG is the corresponding argument.
20274 This function attempts to match PARM with ARG in a manner
20275 consistent with the existing assignments in TARGS. If more values
20276 are deduced, then TARGS is updated.
20278 Returns 0 if the type deduction succeeds, 1 otherwise. The
20279 parameter STRICT is a bitwise or of the following flags:
20281 UNIFY_ALLOW_NONE:
20282 Require an exact match between PARM and ARG.
20283 UNIFY_ALLOW_MORE_CV_QUAL:
20284 Allow the deduced ARG to be more cv-qualified (by qualification
20285 conversion) than ARG.
20286 UNIFY_ALLOW_LESS_CV_QUAL:
20287 Allow the deduced ARG to be less cv-qualified than ARG.
20288 UNIFY_ALLOW_DERIVED:
20289 Allow the deduced ARG to be a template base class of ARG,
20290 or a pointer to a template base class of the type pointed to by
20291 ARG.
20292 UNIFY_ALLOW_INTEGER:
20293 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
20294 case for more information.
20295 UNIFY_ALLOW_OUTER_LEVEL:
20296 This is the outermost level of a deduction. Used to determine validity
20297 of qualification conversions. A valid qualification conversion must
20298 have const qualified pointers leading up to the inner type which
20299 requires additional CV quals, except at the outer level, where const
20300 is not required [conv.qual]. It would be normal to set this flag in
20301 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
20302 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
20303 This is the outermost level of a deduction, and PARM can be more CV
20304 qualified at this point.
20305 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
20306 This is the outermost level of a deduction, and PARM can be less CV
20307 qualified at this point. */
20309 static int
20310 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
20311 bool explain_p)
20313 int idx;
20314 tree targ;
20315 tree tparm;
20316 int strict_in = strict;
20317 tsubst_flags_t complain = (explain_p
20318 ? tf_warning_or_error
20319 : tf_none);
20321 /* I don't think this will do the right thing with respect to types.
20322 But the only case I've seen it in so far has been array bounds, where
20323 signedness is the only information lost, and I think that will be
20324 okay. */
20325 while (CONVERT_EXPR_P (parm))
20326 parm = TREE_OPERAND (parm, 0);
20328 if (arg == error_mark_node)
20329 return unify_invalid (explain_p);
20330 if (arg == unknown_type_node
20331 || arg == init_list_type_node)
20332 /* We can't deduce anything from this, but we might get all the
20333 template args from other function args. */
20334 return unify_success (explain_p);
20336 if (parm == any_targ_node || arg == any_targ_node)
20337 return unify_success (explain_p);
20339 /* If PARM uses template parameters, then we can't bail out here,
20340 even if ARG == PARM, since we won't record unifications for the
20341 template parameters. We might need them if we're trying to
20342 figure out which of two things is more specialized. */
20343 if (arg == parm && !uses_template_parms (parm))
20344 return unify_success (explain_p);
20346 /* Handle init lists early, so the rest of the function can assume
20347 we're dealing with a type. */
20348 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
20350 tree elt, elttype;
20351 unsigned i;
20352 tree orig_parm = parm;
20354 /* Replace T with std::initializer_list<T> for deduction. */
20355 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20356 && flag_deduce_init_list)
20357 parm = listify (parm);
20359 if (!is_std_init_list (parm)
20360 && TREE_CODE (parm) != ARRAY_TYPE)
20361 /* We can only deduce from an initializer list argument if the
20362 parameter is std::initializer_list or an array; otherwise this
20363 is a non-deduced context. */
20364 return unify_success (explain_p);
20366 if (TREE_CODE (parm) == ARRAY_TYPE)
20367 elttype = TREE_TYPE (parm);
20368 else
20370 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
20371 /* Deduction is defined in terms of a single type, so just punt
20372 on the (bizarre) std::initializer_list<T...>. */
20373 if (PACK_EXPANSION_P (elttype))
20374 return unify_success (explain_p);
20377 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
20379 int elt_strict = strict;
20381 if (elt == error_mark_node)
20382 return unify_invalid (explain_p);
20384 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
20386 tree type = TREE_TYPE (elt);
20387 if (type == error_mark_node)
20388 return unify_invalid (explain_p);
20389 /* It should only be possible to get here for a call. */
20390 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
20391 elt_strict |= maybe_adjust_types_for_deduction
20392 (DEDUCE_CALL, &elttype, &type, elt);
20393 elt = type;
20396 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
20397 explain_p);
20400 if (TREE_CODE (parm) == ARRAY_TYPE
20401 && deducible_array_bound (TYPE_DOMAIN (parm)))
20403 /* Also deduce from the length of the initializer list. */
20404 tree max = size_int (CONSTRUCTOR_NELTS (arg));
20405 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
20406 if (idx == error_mark_node)
20407 return unify_invalid (explain_p);
20408 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20409 idx, explain_p);
20412 /* If the std::initializer_list<T> deduction worked, replace the
20413 deduced A with std::initializer_list<A>. */
20414 if (orig_parm != parm)
20416 idx = TEMPLATE_TYPE_IDX (orig_parm);
20417 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20418 targ = listify (targ);
20419 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
20421 return unify_success (explain_p);
20424 /* If parm and arg aren't the same kind of thing (template, type, or
20425 expression), fail early. */
20426 if (pa_kind (parm) != pa_kind (arg))
20427 return unify_invalid (explain_p);
20429 /* Immediately reject some pairs that won't unify because of
20430 cv-qualification mismatches. */
20431 if (TREE_CODE (arg) == TREE_CODE (parm)
20432 && TYPE_P (arg)
20433 /* It is the elements of the array which hold the cv quals of an array
20434 type, and the elements might be template type parms. We'll check
20435 when we recurse. */
20436 && TREE_CODE (arg) != ARRAY_TYPE
20437 /* We check the cv-qualifiers when unifying with template type
20438 parameters below. We want to allow ARG `const T' to unify with
20439 PARM `T' for example, when computing which of two templates
20440 is more specialized, for example. */
20441 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
20442 && !check_cv_quals_for_unify (strict_in, arg, parm))
20443 return unify_cv_qual_mismatch (explain_p, parm, arg);
20445 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
20446 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
20447 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
20448 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
20449 strict &= ~UNIFY_ALLOW_DERIVED;
20450 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
20451 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
20453 switch (TREE_CODE (parm))
20455 case TYPENAME_TYPE:
20456 case SCOPE_REF:
20457 case UNBOUND_CLASS_TEMPLATE:
20458 /* In a type which contains a nested-name-specifier, template
20459 argument values cannot be deduced for template parameters used
20460 within the nested-name-specifier. */
20461 return unify_success (explain_p);
20463 case TEMPLATE_TYPE_PARM:
20464 case TEMPLATE_TEMPLATE_PARM:
20465 case BOUND_TEMPLATE_TEMPLATE_PARM:
20466 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20467 if (error_operand_p (tparm))
20468 return unify_invalid (explain_p);
20470 if (TEMPLATE_TYPE_LEVEL (parm)
20471 != template_decl_level (tparm))
20472 /* The PARM is not one we're trying to unify. Just check
20473 to see if it matches ARG. */
20475 if (TREE_CODE (arg) == TREE_CODE (parm)
20476 && (is_auto (parm) ? is_auto (arg)
20477 : same_type_p (parm, arg)))
20478 return unify_success (explain_p);
20479 else
20480 return unify_type_mismatch (explain_p, parm, arg);
20482 idx = TEMPLATE_TYPE_IDX (parm);
20483 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20484 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
20485 if (error_operand_p (tparm))
20486 return unify_invalid (explain_p);
20488 /* Check for mixed types and values. */
20489 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20490 && TREE_CODE (tparm) != TYPE_DECL)
20491 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20492 && TREE_CODE (tparm) != TEMPLATE_DECL))
20493 gcc_unreachable ();
20495 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20497 if ((strict_in & UNIFY_ALLOW_DERIVED)
20498 && CLASS_TYPE_P (arg))
20500 /* First try to match ARG directly. */
20501 tree t = try_class_unification (tparms, targs, parm, arg,
20502 explain_p);
20503 if (!t)
20505 /* Otherwise, look for a suitable base of ARG, as below. */
20506 enum template_base_result r;
20507 r = get_template_base (tparms, targs, parm, arg,
20508 explain_p, &t);
20509 if (!t)
20510 return unify_no_common_base (explain_p, r, parm, arg);
20511 arg = t;
20514 /* ARG must be constructed from a template class or a template
20515 template parameter. */
20516 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
20517 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20518 return unify_template_deduction_failure (explain_p, parm, arg);
20520 /* Deduce arguments T, i from TT<T> or TT<i>. */
20521 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
20522 return 1;
20524 arg = TYPE_TI_TEMPLATE (arg);
20526 /* Fall through to deduce template name. */
20529 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20530 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20532 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
20534 /* Simple cases: Value already set, does match or doesn't. */
20535 if (targ != NULL_TREE && template_args_equal (targ, arg))
20536 return unify_success (explain_p);
20537 else if (targ)
20538 return unify_inconsistency (explain_p, parm, targ, arg);
20540 else
20542 /* If PARM is `const T' and ARG is only `int', we don't have
20543 a match unless we are allowing additional qualification.
20544 If ARG is `const int' and PARM is just `T' that's OK;
20545 that binds `const int' to `T'. */
20546 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
20547 arg, parm))
20548 return unify_cv_qual_mismatch (explain_p, parm, arg);
20550 /* Consider the case where ARG is `const volatile int' and
20551 PARM is `const T'. Then, T should be `volatile int'. */
20552 arg = cp_build_qualified_type_real
20553 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
20554 if (arg == error_mark_node)
20555 return unify_invalid (explain_p);
20557 /* Simple cases: Value already set, does match or doesn't. */
20558 if (targ != NULL_TREE && same_type_p (targ, arg))
20559 return unify_success (explain_p);
20560 else if (targ)
20561 return unify_inconsistency (explain_p, parm, targ, arg);
20563 /* Make sure that ARG is not a variable-sized array. (Note
20564 that were talking about variable-sized arrays (like
20565 `int[n]'), rather than arrays of unknown size (like
20566 `int[]').) We'll get very confused by such a type since
20567 the bound of the array is not constant, and therefore
20568 not mangleable. Besides, such types are not allowed in
20569 ISO C++, so we can do as we please here. We do allow
20570 them for 'auto' deduction, since that isn't ABI-exposed. */
20571 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
20572 return unify_vla_arg (explain_p, arg);
20574 /* Strip typedefs as in convert_template_argument. */
20575 arg = canonicalize_type_argument (arg, tf_none);
20578 /* If ARG is a parameter pack or an expansion, we cannot unify
20579 against it unless PARM is also a parameter pack. */
20580 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20581 && !template_parameter_pack_p (parm))
20582 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20584 /* If the argument deduction results is a METHOD_TYPE,
20585 then there is a problem.
20586 METHOD_TYPE doesn't map to any real C++ type the result of
20587 the deduction can not be of that type. */
20588 if (TREE_CODE (arg) == METHOD_TYPE)
20589 return unify_method_type_error (explain_p, arg);
20591 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20592 return unify_success (explain_p);
20594 case TEMPLATE_PARM_INDEX:
20595 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20596 if (error_operand_p (tparm))
20597 return unify_invalid (explain_p);
20599 if (TEMPLATE_PARM_LEVEL (parm)
20600 != template_decl_level (tparm))
20602 /* The PARM is not one we're trying to unify. Just check
20603 to see if it matches ARG. */
20604 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
20605 && cp_tree_equal (parm, arg));
20606 if (result)
20607 unify_expression_unequal (explain_p, parm, arg);
20608 return result;
20611 idx = TEMPLATE_PARM_IDX (parm);
20612 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20614 if (targ)
20616 int x = !cp_tree_equal (targ, arg);
20617 if (x)
20618 unify_inconsistency (explain_p, parm, targ, arg);
20619 return x;
20622 if (cxx_dialect >= cxx1z
20623 /* We deduce from array bounds in try_array_deduction. */
20624 && !(strict & UNIFY_ALLOW_INTEGER)
20625 && uses_template_parms (TREE_TYPE (parm))
20626 && !type_uses_auto (TREE_TYPE (parm)))
20628 tree atype = TREE_TYPE (arg);
20629 RECUR_AND_CHECK_FAILURE (tparms, targs,
20630 TREE_TYPE (parm), atype,
20631 UNIFY_ALLOW_NONE, explain_p);
20634 /* [temp.deduct.type] If, in the declaration of a function template
20635 with a non-type template-parameter, the non-type
20636 template-parameter is used in an expression in the function
20637 parameter-list and, if the corresponding template-argument is
20638 deduced, the template-argument type shall match the type of the
20639 template-parameter exactly, except that a template-argument
20640 deduced from an array bound may be of any integral type.
20641 The non-type parameter might use already deduced type parameters. */
20642 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
20643 if (tree a = type_uses_auto (tparm))
20645 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
20646 if (tparm == error_mark_node)
20647 return 1;
20650 if (!TREE_TYPE (arg))
20651 /* Template-parameter dependent expression. Just accept it for now.
20652 It will later be processed in convert_template_argument. */
20654 else if (same_type_p (TREE_TYPE (arg), tparm))
20655 /* OK */;
20656 else if ((strict & UNIFY_ALLOW_INTEGER)
20657 && CP_INTEGRAL_TYPE_P (tparm))
20658 /* Convert the ARG to the type of PARM; the deduced non-type
20659 template argument must exactly match the types of the
20660 corresponding parameter. */
20661 arg = fold (build_nop (tparm, arg));
20662 else if (uses_template_parms (tparm))
20663 /* We haven't deduced the type of this parameter yet. Try again
20664 later. */
20665 return unify_success (explain_p);
20666 else
20667 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
20669 /* If ARG is a parameter pack or an expansion, we cannot unify
20670 against it unless PARM is also a parameter pack. */
20671 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20672 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
20673 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20676 bool removed_attr = false;
20677 arg = strip_typedefs_expr (arg, &removed_attr);
20679 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20680 return unify_success (explain_p);
20682 case PTRMEM_CST:
20684 /* A pointer-to-member constant can be unified only with
20685 another constant. */
20686 if (TREE_CODE (arg) != PTRMEM_CST)
20687 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
20689 /* Just unify the class member. It would be useless (and possibly
20690 wrong, depending on the strict flags) to unify also
20691 PTRMEM_CST_CLASS, because we want to be sure that both parm and
20692 arg refer to the same variable, even if through different
20693 classes. For instance:
20695 struct A { int x; };
20696 struct B : A { };
20698 Unification of &A::x and &B::x must succeed. */
20699 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
20700 PTRMEM_CST_MEMBER (arg), strict, explain_p);
20703 case POINTER_TYPE:
20705 if (!TYPE_PTR_P (arg))
20706 return unify_type_mismatch (explain_p, parm, arg);
20708 /* [temp.deduct.call]
20710 A can be another pointer or pointer to member type that can
20711 be converted to the deduced A via a qualification
20712 conversion (_conv.qual_).
20714 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
20715 This will allow for additional cv-qualification of the
20716 pointed-to types if appropriate. */
20718 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
20719 /* The derived-to-base conversion only persists through one
20720 level of pointers. */
20721 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
20723 return unify (tparms, targs, TREE_TYPE (parm),
20724 TREE_TYPE (arg), strict, explain_p);
20727 case REFERENCE_TYPE:
20728 if (TREE_CODE (arg) != REFERENCE_TYPE)
20729 return unify_type_mismatch (explain_p, parm, arg);
20730 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
20731 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
20733 case ARRAY_TYPE:
20734 if (TREE_CODE (arg) != ARRAY_TYPE)
20735 return unify_type_mismatch (explain_p, parm, arg);
20736 if ((TYPE_DOMAIN (parm) == NULL_TREE)
20737 != (TYPE_DOMAIN (arg) == NULL_TREE))
20738 return unify_type_mismatch (explain_p, parm, arg);
20739 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
20740 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
20741 if (TYPE_DOMAIN (parm) != NULL_TREE)
20742 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20743 TYPE_DOMAIN (arg), explain_p);
20744 return unify_success (explain_p);
20746 case REAL_TYPE:
20747 case COMPLEX_TYPE:
20748 case VECTOR_TYPE:
20749 case INTEGER_TYPE:
20750 case BOOLEAN_TYPE:
20751 case ENUMERAL_TYPE:
20752 case VOID_TYPE:
20753 case NULLPTR_TYPE:
20754 if (TREE_CODE (arg) != TREE_CODE (parm))
20755 return unify_type_mismatch (explain_p, parm, arg);
20757 /* We have already checked cv-qualification at the top of the
20758 function. */
20759 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
20760 return unify_type_mismatch (explain_p, parm, arg);
20762 /* As far as unification is concerned, this wins. Later checks
20763 will invalidate it if necessary. */
20764 return unify_success (explain_p);
20766 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
20767 /* Type INTEGER_CST can come from ordinary constant template args. */
20768 case INTEGER_CST:
20769 while (CONVERT_EXPR_P (arg))
20770 arg = TREE_OPERAND (arg, 0);
20772 if (TREE_CODE (arg) != INTEGER_CST)
20773 return unify_template_argument_mismatch (explain_p, parm, arg);
20774 return (tree_int_cst_equal (parm, arg)
20775 ? unify_success (explain_p)
20776 : unify_template_argument_mismatch (explain_p, parm, arg));
20778 case TREE_VEC:
20780 int i, len, argslen;
20781 int parm_variadic_p = 0;
20783 if (TREE_CODE (arg) != TREE_VEC)
20784 return unify_template_argument_mismatch (explain_p, parm, arg);
20786 len = TREE_VEC_LENGTH (parm);
20787 argslen = TREE_VEC_LENGTH (arg);
20789 /* Check for pack expansions in the parameters. */
20790 for (i = 0; i < len; ++i)
20792 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
20794 if (i == len - 1)
20795 /* We can unify against something with a trailing
20796 parameter pack. */
20797 parm_variadic_p = 1;
20798 else
20799 /* [temp.deduct.type]/9: If the template argument list of
20800 P contains a pack expansion that is not the last
20801 template argument, the entire template argument list
20802 is a non-deduced context. */
20803 return unify_success (explain_p);
20807 /* If we don't have enough arguments to satisfy the parameters
20808 (not counting the pack expression at the end), or we have
20809 too many arguments for a parameter list that doesn't end in
20810 a pack expression, we can't unify. */
20811 if (parm_variadic_p
20812 ? argslen < len - parm_variadic_p
20813 : argslen != len)
20814 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
20816 /* Unify all of the parameters that precede the (optional)
20817 pack expression. */
20818 for (i = 0; i < len - parm_variadic_p; ++i)
20820 RECUR_AND_CHECK_FAILURE (tparms, targs,
20821 TREE_VEC_ELT (parm, i),
20822 TREE_VEC_ELT (arg, i),
20823 UNIFY_ALLOW_NONE, explain_p);
20825 if (parm_variadic_p)
20826 return unify_pack_expansion (tparms, targs, parm, arg,
20827 DEDUCE_EXACT,
20828 /*subr=*/true, explain_p);
20829 return unify_success (explain_p);
20832 case RECORD_TYPE:
20833 case UNION_TYPE:
20834 if (TREE_CODE (arg) != TREE_CODE (parm))
20835 return unify_type_mismatch (explain_p, parm, arg);
20837 if (TYPE_PTRMEMFUNC_P (parm))
20839 if (!TYPE_PTRMEMFUNC_P (arg))
20840 return unify_type_mismatch (explain_p, parm, arg);
20842 return unify (tparms, targs,
20843 TYPE_PTRMEMFUNC_FN_TYPE (parm),
20844 TYPE_PTRMEMFUNC_FN_TYPE (arg),
20845 strict, explain_p);
20847 else if (TYPE_PTRMEMFUNC_P (arg))
20848 return unify_type_mismatch (explain_p, parm, arg);
20850 if (CLASSTYPE_TEMPLATE_INFO (parm))
20852 tree t = NULL_TREE;
20854 if (strict_in & UNIFY_ALLOW_DERIVED)
20856 /* First, we try to unify the PARM and ARG directly. */
20857 t = try_class_unification (tparms, targs,
20858 parm, arg, explain_p);
20860 if (!t)
20862 /* Fallback to the special case allowed in
20863 [temp.deduct.call]:
20865 If P is a class, and P has the form
20866 template-id, then A can be a derived class of
20867 the deduced A. Likewise, if P is a pointer to
20868 a class of the form template-id, A can be a
20869 pointer to a derived class pointed to by the
20870 deduced A. */
20871 enum template_base_result r;
20872 r = get_template_base (tparms, targs, parm, arg,
20873 explain_p, &t);
20875 if (!t)
20877 /* Don't give the derived diagnostic if we're
20878 already dealing with the same template. */
20879 bool same_template
20880 = (CLASSTYPE_TEMPLATE_INFO (arg)
20881 && (CLASSTYPE_TI_TEMPLATE (parm)
20882 == CLASSTYPE_TI_TEMPLATE (arg)));
20883 return unify_no_common_base (explain_p && !same_template,
20884 r, parm, arg);
20888 else if (CLASSTYPE_TEMPLATE_INFO (arg)
20889 && (CLASSTYPE_TI_TEMPLATE (parm)
20890 == CLASSTYPE_TI_TEMPLATE (arg)))
20891 /* Perhaps PARM is something like S<U> and ARG is S<int>.
20892 Then, we should unify `int' and `U'. */
20893 t = arg;
20894 else
20895 /* There's no chance of unification succeeding. */
20896 return unify_type_mismatch (explain_p, parm, arg);
20898 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
20899 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
20901 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
20902 return unify_type_mismatch (explain_p, parm, arg);
20903 return unify_success (explain_p);
20905 case METHOD_TYPE:
20906 case FUNCTION_TYPE:
20908 unsigned int nargs;
20909 tree *args;
20910 tree a;
20911 unsigned int i;
20913 if (TREE_CODE (arg) != TREE_CODE (parm))
20914 return unify_type_mismatch (explain_p, parm, arg);
20916 /* CV qualifications for methods can never be deduced, they must
20917 match exactly. We need to check them explicitly here,
20918 because type_unification_real treats them as any other
20919 cv-qualified parameter. */
20920 if (TREE_CODE (parm) == METHOD_TYPE
20921 && (!check_cv_quals_for_unify
20922 (UNIFY_ALLOW_NONE,
20923 class_of_this_parm (arg),
20924 class_of_this_parm (parm))))
20925 return unify_cv_qual_mismatch (explain_p, parm, arg);
20926 if (TREE_CODE (arg) == FUNCTION_TYPE
20927 && type_memfn_quals (parm) != type_memfn_quals (arg))
20928 return unify_cv_qual_mismatch (explain_p, parm, arg);
20929 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
20930 return unify_type_mismatch (explain_p, parm, arg);
20932 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
20933 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
20935 nargs = list_length (TYPE_ARG_TYPES (arg));
20936 args = XALLOCAVEC (tree, nargs);
20937 for (a = TYPE_ARG_TYPES (arg), i = 0;
20938 a != NULL_TREE && a != void_list_node;
20939 a = TREE_CHAIN (a), ++i)
20940 args[i] = TREE_VALUE (a);
20941 nargs = i;
20943 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
20944 args, nargs, 1, DEDUCE_EXACT,
20945 LOOKUP_NORMAL, NULL, explain_p))
20946 return 1;
20948 if (flag_noexcept_type)
20950 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
20951 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
20952 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
20953 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
20954 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
20955 && uses_template_parms (TREE_PURPOSE (pspec)))
20956 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
20957 TREE_PURPOSE (aspec),
20958 UNIFY_ALLOW_NONE, explain_p);
20959 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
20960 return unify_type_mismatch (explain_p, parm, arg);
20963 return 0;
20966 case OFFSET_TYPE:
20967 /* Unify a pointer to member with a pointer to member function, which
20968 deduces the type of the member as a function type. */
20969 if (TYPE_PTRMEMFUNC_P (arg))
20971 /* Check top-level cv qualifiers */
20972 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
20973 return unify_cv_qual_mismatch (explain_p, parm, arg);
20975 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
20976 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
20977 UNIFY_ALLOW_NONE, explain_p);
20979 /* Determine the type of the function we are unifying against. */
20980 tree fntype = static_fn_type (arg);
20982 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
20985 if (TREE_CODE (arg) != OFFSET_TYPE)
20986 return unify_type_mismatch (explain_p, parm, arg);
20987 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
20988 TYPE_OFFSET_BASETYPE (arg),
20989 UNIFY_ALLOW_NONE, explain_p);
20990 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
20991 strict, explain_p);
20993 case CONST_DECL:
20994 if (DECL_TEMPLATE_PARM_P (parm))
20995 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
20996 if (arg != scalar_constant_value (parm))
20997 return unify_template_argument_mismatch (explain_p, parm, arg);
20998 return unify_success (explain_p);
21000 case FIELD_DECL:
21001 case TEMPLATE_DECL:
21002 /* Matched cases are handled by the ARG == PARM test above. */
21003 return unify_template_argument_mismatch (explain_p, parm, arg);
21005 case VAR_DECL:
21006 /* We might get a variable as a non-type template argument in parm if the
21007 corresponding parameter is type-dependent. Make any necessary
21008 adjustments based on whether arg is a reference. */
21009 if (CONSTANT_CLASS_P (arg))
21010 parm = fold_non_dependent_expr (parm);
21011 else if (REFERENCE_REF_P (arg))
21013 tree sub = TREE_OPERAND (arg, 0);
21014 STRIP_NOPS (sub);
21015 if (TREE_CODE (sub) == ADDR_EXPR)
21016 arg = TREE_OPERAND (sub, 0);
21018 /* Now use the normal expression code to check whether they match. */
21019 goto expr;
21021 case TYPE_ARGUMENT_PACK:
21022 case NONTYPE_ARGUMENT_PACK:
21023 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
21024 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
21026 case TYPEOF_TYPE:
21027 case DECLTYPE_TYPE:
21028 case UNDERLYING_TYPE:
21029 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
21030 or UNDERLYING_TYPE nodes. */
21031 return unify_success (explain_p);
21033 case ERROR_MARK:
21034 /* Unification fails if we hit an error node. */
21035 return unify_invalid (explain_p);
21037 case INDIRECT_REF:
21038 if (REFERENCE_REF_P (parm))
21040 bool pexp = PACK_EXPANSION_P (arg);
21041 if (pexp)
21042 arg = PACK_EXPANSION_PATTERN (arg);
21043 if (REFERENCE_REF_P (arg))
21044 arg = TREE_OPERAND (arg, 0);
21045 if (pexp)
21046 arg = make_pack_expansion (arg);
21047 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
21048 strict, explain_p);
21050 /* FALLTHRU */
21052 default:
21053 /* An unresolved overload is a nondeduced context. */
21054 if (is_overloaded_fn (parm) || type_unknown_p (parm))
21055 return unify_success (explain_p);
21056 gcc_assert (EXPR_P (parm) || TREE_CODE (parm) == TRAIT_EXPR);
21057 expr:
21058 /* We must be looking at an expression. This can happen with
21059 something like:
21061 template <int I>
21062 void foo(S<I>, S<I + 2>);
21064 This is a "nondeduced context":
21066 [deduct.type]
21068 The nondeduced contexts are:
21070 --A type that is a template-id in which one or more of
21071 the template-arguments is an expression that references
21072 a template-parameter.
21074 In these cases, we assume deduction succeeded, but don't
21075 actually infer any unifications. */
21077 if (!uses_template_parms (parm)
21078 && !template_args_equal (parm, arg))
21079 return unify_expression_unequal (explain_p, parm, arg);
21080 else
21081 return unify_success (explain_p);
21084 #undef RECUR_AND_CHECK_FAILURE
21086 /* Note that DECL can be defined in this translation unit, if
21087 required. */
21089 static void
21090 mark_definable (tree decl)
21092 tree clone;
21093 DECL_NOT_REALLY_EXTERN (decl) = 1;
21094 FOR_EACH_CLONE (clone, decl)
21095 DECL_NOT_REALLY_EXTERN (clone) = 1;
21098 /* Called if RESULT is explicitly instantiated, or is a member of an
21099 explicitly instantiated class. */
21101 void
21102 mark_decl_instantiated (tree result, int extern_p)
21104 SET_DECL_EXPLICIT_INSTANTIATION (result);
21106 /* If this entity has already been written out, it's too late to
21107 make any modifications. */
21108 if (TREE_ASM_WRITTEN (result))
21109 return;
21111 /* For anonymous namespace we don't need to do anything. */
21112 if (decl_anon_ns_mem_p (result))
21114 gcc_assert (!TREE_PUBLIC (result));
21115 return;
21118 if (TREE_CODE (result) != FUNCTION_DECL)
21119 /* The TREE_PUBLIC flag for function declarations will have been
21120 set correctly by tsubst. */
21121 TREE_PUBLIC (result) = 1;
21123 /* This might have been set by an earlier implicit instantiation. */
21124 DECL_COMDAT (result) = 0;
21126 if (extern_p)
21127 DECL_NOT_REALLY_EXTERN (result) = 0;
21128 else
21130 mark_definable (result);
21131 mark_needed (result);
21132 /* Always make artificials weak. */
21133 if (DECL_ARTIFICIAL (result) && flag_weak)
21134 comdat_linkage (result);
21135 /* For WIN32 we also want to put explicit instantiations in
21136 linkonce sections. */
21137 else if (TREE_PUBLIC (result))
21138 maybe_make_one_only (result);
21141 /* If EXTERN_P, then this function will not be emitted -- unless
21142 followed by an explicit instantiation, at which point its linkage
21143 will be adjusted. If !EXTERN_P, then this function will be
21144 emitted here. In neither circumstance do we want
21145 import_export_decl to adjust the linkage. */
21146 DECL_INTERFACE_KNOWN (result) = 1;
21149 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
21150 important template arguments. If any are missing, we check whether
21151 they're important by using error_mark_node for substituting into any
21152 args that were used for partial ordering (the ones between ARGS and END)
21153 and seeing if it bubbles up. */
21155 static bool
21156 check_undeduced_parms (tree targs, tree args, tree end)
21158 bool found = false;
21159 int i;
21160 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
21161 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
21163 found = true;
21164 TREE_VEC_ELT (targs, i) = error_mark_node;
21166 if (found)
21168 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
21169 if (substed == error_mark_node)
21170 return true;
21172 return false;
21175 /* Given two function templates PAT1 and PAT2, return:
21177 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
21178 -1 if PAT2 is more specialized than PAT1.
21179 0 if neither is more specialized.
21181 LEN indicates the number of parameters we should consider
21182 (defaulted parameters should not be considered).
21184 The 1998 std underspecified function template partial ordering, and
21185 DR214 addresses the issue. We take pairs of arguments, one from
21186 each of the templates, and deduce them against each other. One of
21187 the templates will be more specialized if all the *other*
21188 template's arguments deduce against its arguments and at least one
21189 of its arguments *does* *not* deduce against the other template's
21190 corresponding argument. Deduction is done as for class templates.
21191 The arguments used in deduction have reference and top level cv
21192 qualifiers removed. Iff both arguments were originally reference
21193 types *and* deduction succeeds in both directions, an lvalue reference
21194 wins against an rvalue reference and otherwise the template
21195 with the more cv-qualified argument wins for that pairing (if
21196 neither is more cv-qualified, they both are equal). Unlike regular
21197 deduction, after all the arguments have been deduced in this way,
21198 we do *not* verify the deduced template argument values can be
21199 substituted into non-deduced contexts.
21201 The logic can be a bit confusing here, because we look at deduce1 and
21202 targs1 to see if pat2 is at least as specialized, and vice versa; if we
21203 can find template arguments for pat1 to make arg1 look like arg2, that
21204 means that arg2 is at least as specialized as arg1. */
21207 more_specialized_fn (tree pat1, tree pat2, int len)
21209 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
21210 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
21211 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
21212 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
21213 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
21214 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
21215 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
21216 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
21217 tree origs1, origs2;
21218 bool lose1 = false;
21219 bool lose2 = false;
21221 /* Remove the this parameter from non-static member functions. If
21222 one is a non-static member function and the other is not a static
21223 member function, remove the first parameter from that function
21224 also. This situation occurs for operator functions where we
21225 locate both a member function (with this pointer) and non-member
21226 operator (with explicit first operand). */
21227 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
21229 len--; /* LEN is the number of significant arguments for DECL1 */
21230 args1 = TREE_CHAIN (args1);
21231 if (!DECL_STATIC_FUNCTION_P (decl2))
21232 args2 = TREE_CHAIN (args2);
21234 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
21236 args2 = TREE_CHAIN (args2);
21237 if (!DECL_STATIC_FUNCTION_P (decl1))
21239 len--;
21240 args1 = TREE_CHAIN (args1);
21244 /* If only one is a conversion operator, they are unordered. */
21245 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
21246 return 0;
21248 /* Consider the return type for a conversion function */
21249 if (DECL_CONV_FN_P (decl1))
21251 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
21252 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
21253 len++;
21256 processing_template_decl++;
21258 origs1 = args1;
21259 origs2 = args2;
21261 while (len--
21262 /* Stop when an ellipsis is seen. */
21263 && args1 != NULL_TREE && args2 != NULL_TREE)
21265 tree arg1 = TREE_VALUE (args1);
21266 tree arg2 = TREE_VALUE (args2);
21267 int deduce1, deduce2;
21268 int quals1 = -1;
21269 int quals2 = -1;
21270 int ref1 = 0;
21271 int ref2 = 0;
21273 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21274 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21276 /* When both arguments are pack expansions, we need only
21277 unify the patterns themselves. */
21278 arg1 = PACK_EXPANSION_PATTERN (arg1);
21279 arg2 = PACK_EXPANSION_PATTERN (arg2);
21281 /* This is the last comparison we need to do. */
21282 len = 0;
21285 /* DR 1847: If a particular P contains no template-parameters that
21286 participate in template argument deduction, that P is not used to
21287 determine the ordering. */
21288 if (!uses_deducible_template_parms (arg1)
21289 && !uses_deducible_template_parms (arg2))
21290 goto next;
21292 if (TREE_CODE (arg1) == REFERENCE_TYPE)
21294 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
21295 arg1 = TREE_TYPE (arg1);
21296 quals1 = cp_type_quals (arg1);
21299 if (TREE_CODE (arg2) == REFERENCE_TYPE)
21301 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
21302 arg2 = TREE_TYPE (arg2);
21303 quals2 = cp_type_quals (arg2);
21306 arg1 = TYPE_MAIN_VARIANT (arg1);
21307 arg2 = TYPE_MAIN_VARIANT (arg2);
21309 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
21311 int i, len2 = remaining_arguments (args2);
21312 tree parmvec = make_tree_vec (1);
21313 tree argvec = make_tree_vec (len2);
21314 tree ta = args2;
21316 /* Setup the parameter vector, which contains only ARG1. */
21317 TREE_VEC_ELT (parmvec, 0) = arg1;
21319 /* Setup the argument vector, which contains the remaining
21320 arguments. */
21321 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
21322 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21324 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
21325 argvec, DEDUCE_EXACT,
21326 /*subr=*/true, /*explain_p=*/false)
21327 == 0);
21329 /* We cannot deduce in the other direction, because ARG1 is
21330 a pack expansion but ARG2 is not. */
21331 deduce2 = 0;
21333 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21335 int i, len1 = remaining_arguments (args1);
21336 tree parmvec = make_tree_vec (1);
21337 tree argvec = make_tree_vec (len1);
21338 tree ta = args1;
21340 /* Setup the parameter vector, which contains only ARG1. */
21341 TREE_VEC_ELT (parmvec, 0) = arg2;
21343 /* Setup the argument vector, which contains the remaining
21344 arguments. */
21345 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
21346 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21348 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
21349 argvec, DEDUCE_EXACT,
21350 /*subr=*/true, /*explain_p=*/false)
21351 == 0);
21353 /* We cannot deduce in the other direction, because ARG2 is
21354 a pack expansion but ARG1 is not.*/
21355 deduce1 = 0;
21358 else
21360 /* The normal case, where neither argument is a pack
21361 expansion. */
21362 deduce1 = (unify (tparms1, targs1, arg1, arg2,
21363 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21364 == 0);
21365 deduce2 = (unify (tparms2, targs2, arg2, arg1,
21366 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21367 == 0);
21370 /* If we couldn't deduce arguments for tparms1 to make arg1 match
21371 arg2, then arg2 is not as specialized as arg1. */
21372 if (!deduce1)
21373 lose2 = true;
21374 if (!deduce2)
21375 lose1 = true;
21377 /* "If, for a given type, deduction succeeds in both directions
21378 (i.e., the types are identical after the transformations above)
21379 and both P and A were reference types (before being replaced with
21380 the type referred to above):
21381 - if the type from the argument template was an lvalue reference and
21382 the type from the parameter template was not, the argument type is
21383 considered to be more specialized than the other; otherwise,
21384 - if the type from the argument template is more cv-qualified
21385 than the type from the parameter template (as described above),
21386 the argument type is considered to be more specialized than the other;
21387 otherwise,
21388 - neither type is more specialized than the other." */
21390 if (deduce1 && deduce2)
21392 if (ref1 && ref2 && ref1 != ref2)
21394 if (ref1 > ref2)
21395 lose1 = true;
21396 else
21397 lose2 = true;
21399 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
21401 if ((quals1 & quals2) == quals2)
21402 lose2 = true;
21403 if ((quals1 & quals2) == quals1)
21404 lose1 = true;
21408 if (lose1 && lose2)
21409 /* We've failed to deduce something in either direction.
21410 These must be unordered. */
21411 break;
21413 next:
21415 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21416 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21417 /* We have already processed all of the arguments in our
21418 handing of the pack expansion type. */
21419 len = 0;
21421 args1 = TREE_CHAIN (args1);
21422 args2 = TREE_CHAIN (args2);
21425 /* "In most cases, all template parameters must have values in order for
21426 deduction to succeed, but for partial ordering purposes a template
21427 parameter may remain without a value provided it is not used in the
21428 types being used for partial ordering."
21430 Thus, if we are missing any of the targs1 we need to substitute into
21431 origs1, then pat2 is not as specialized as pat1. This can happen when
21432 there is a nondeduced context. */
21433 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
21434 lose2 = true;
21435 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
21436 lose1 = true;
21438 processing_template_decl--;
21440 /* If both deductions succeed, the partial ordering selects the more
21441 constrained template. */
21442 if (!lose1 && !lose2)
21444 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
21445 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
21446 lose1 = !subsumes_constraints (c1, c2);
21447 lose2 = !subsumes_constraints (c2, c1);
21450 /* All things being equal, if the next argument is a pack expansion
21451 for one function but not for the other, prefer the
21452 non-variadic function. FIXME this is bogus; see c++/41958. */
21453 if (lose1 == lose2
21454 && args1 && TREE_VALUE (args1)
21455 && args2 && TREE_VALUE (args2))
21457 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
21458 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
21461 if (lose1 == lose2)
21462 return 0;
21463 else if (!lose1)
21464 return 1;
21465 else
21466 return -1;
21469 /* Determine which of two partial specializations of TMPL is more
21470 specialized.
21472 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
21473 to the first partial specialization. The TREE_PURPOSE is the
21474 innermost set of template parameters for the partial
21475 specialization. PAT2 is similar, but for the second template.
21477 Return 1 if the first partial specialization is more specialized;
21478 -1 if the second is more specialized; 0 if neither is more
21479 specialized.
21481 See [temp.class.order] for information about determining which of
21482 two templates is more specialized. */
21484 static int
21485 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
21487 tree targs;
21488 int winner = 0;
21489 bool any_deductions = false;
21491 tree tmpl1 = TREE_VALUE (pat1);
21492 tree tmpl2 = TREE_VALUE (pat2);
21493 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
21494 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
21496 /* Just like what happens for functions, if we are ordering between
21497 different template specializations, we may encounter dependent
21498 types in the arguments, and we need our dependency check functions
21499 to behave correctly. */
21500 ++processing_template_decl;
21501 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
21502 if (targs)
21504 --winner;
21505 any_deductions = true;
21508 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
21509 if (targs)
21511 ++winner;
21512 any_deductions = true;
21514 --processing_template_decl;
21516 /* If both deductions succeed, the partial ordering selects the more
21517 constrained template. */
21518 if (!winner && any_deductions)
21519 return more_constrained (tmpl1, tmpl2);
21521 /* In the case of a tie where at least one of the templates
21522 has a parameter pack at the end, the template with the most
21523 non-packed parameters wins. */
21524 if (winner == 0
21525 && any_deductions
21526 && (template_args_variadic_p (TREE_PURPOSE (pat1))
21527 || template_args_variadic_p (TREE_PURPOSE (pat2))))
21529 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
21530 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
21531 int len1 = TREE_VEC_LENGTH (args1);
21532 int len2 = TREE_VEC_LENGTH (args2);
21534 /* We don't count the pack expansion at the end. */
21535 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
21536 --len1;
21537 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
21538 --len2;
21540 if (len1 > len2)
21541 return 1;
21542 else if (len1 < len2)
21543 return -1;
21546 return winner;
21549 /* Return the template arguments that will produce the function signature
21550 DECL from the function template FN, with the explicit template
21551 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
21552 also match. Return NULL_TREE if no satisfactory arguments could be
21553 found. */
21555 static tree
21556 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
21558 int ntparms = DECL_NTPARMS (fn);
21559 tree targs = make_tree_vec (ntparms);
21560 tree decl_type = TREE_TYPE (decl);
21561 tree decl_arg_types;
21562 tree *args;
21563 unsigned int nargs, ix;
21564 tree arg;
21566 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
21568 /* Never do unification on the 'this' parameter. */
21569 decl_arg_types = skip_artificial_parms_for (decl,
21570 TYPE_ARG_TYPES (decl_type));
21572 nargs = list_length (decl_arg_types);
21573 args = XALLOCAVEC (tree, nargs);
21574 for (arg = decl_arg_types, ix = 0;
21575 arg != NULL_TREE && arg != void_list_node;
21576 arg = TREE_CHAIN (arg), ++ix)
21577 args[ix] = TREE_VALUE (arg);
21579 if (fn_type_unification (fn, explicit_args, targs,
21580 args, ix,
21581 (check_rettype || DECL_CONV_FN_P (fn)
21582 ? TREE_TYPE (decl_type) : NULL_TREE),
21583 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
21584 /*decltype*/false)
21585 == error_mark_node)
21586 return NULL_TREE;
21588 return targs;
21591 /* Return the innermost template arguments that, when applied to a partial
21592 specialization SPEC_TMPL of TMPL, yield the ARGS.
21594 For example, suppose we have:
21596 template <class T, class U> struct S {};
21597 template <class T> struct S<T*, int> {};
21599 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
21600 partial specialization and the ARGS will be {double*, int}. The resulting
21601 vector will be {double}, indicating that `T' is bound to `double'. */
21603 static tree
21604 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
21606 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
21607 tree spec_args
21608 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
21609 int i, ntparms = TREE_VEC_LENGTH (tparms);
21610 tree deduced_args;
21611 tree innermost_deduced_args;
21613 innermost_deduced_args = make_tree_vec (ntparms);
21614 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
21616 deduced_args = copy_node (args);
21617 SET_TMPL_ARGS_LEVEL (deduced_args,
21618 TMPL_ARGS_DEPTH (deduced_args),
21619 innermost_deduced_args);
21621 else
21622 deduced_args = innermost_deduced_args;
21624 bool tried_array_deduction = (cxx_dialect < cxx1z);
21625 again:
21626 if (unify (tparms, deduced_args,
21627 INNERMOST_TEMPLATE_ARGS (spec_args),
21628 INNERMOST_TEMPLATE_ARGS (args),
21629 UNIFY_ALLOW_NONE, /*explain_p=*/false))
21630 return NULL_TREE;
21632 for (i = 0; i < ntparms; ++i)
21633 if (! TREE_VEC_ELT (innermost_deduced_args, i))
21635 if (!tried_array_deduction)
21637 try_array_deduction (tparms, innermost_deduced_args,
21638 INNERMOST_TEMPLATE_ARGS (spec_args));
21639 tried_array_deduction = true;
21640 if (TREE_VEC_ELT (innermost_deduced_args, i))
21641 goto again;
21643 return NULL_TREE;
21646 tree tinst = build_tree_list (spec_tmpl, deduced_args);
21647 if (!push_tinst_level (tinst))
21649 excessive_deduction_depth = true;
21650 return NULL_TREE;
21653 /* Verify that nondeduced template arguments agree with the type
21654 obtained from argument deduction.
21656 For example:
21658 struct A { typedef int X; };
21659 template <class T, class U> struct C {};
21660 template <class T> struct C<T, typename T::X> {};
21662 Then with the instantiation `C<A, int>', we can deduce that
21663 `T' is `A' but unify () does not check whether `typename T::X'
21664 is `int'. */
21665 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
21666 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
21667 spec_args, tmpl,
21668 tf_none, false, false);
21670 pop_tinst_level ();
21672 if (spec_args == error_mark_node
21673 /* We only need to check the innermost arguments; the other
21674 arguments will always agree. */
21675 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
21676 INNERMOST_TEMPLATE_ARGS (args)))
21677 return NULL_TREE;
21679 /* Now that we have bindings for all of the template arguments,
21680 ensure that the arguments deduced for the template template
21681 parameters have compatible template parameter lists. See the use
21682 of template_template_parm_bindings_ok_p in fn_type_unification
21683 for more information. */
21684 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
21685 return NULL_TREE;
21687 return deduced_args;
21690 // Compare two function templates T1 and T2 by deducing bindings
21691 // from one against the other. If both deductions succeed, compare
21692 // constraints to see which is more constrained.
21693 static int
21694 more_specialized_inst (tree t1, tree t2)
21696 int fate = 0;
21697 int count = 0;
21699 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
21701 --fate;
21702 ++count;
21705 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
21707 ++fate;
21708 ++count;
21711 // If both deductions succeed, then one may be more constrained.
21712 if (count == 2 && fate == 0)
21713 fate = more_constrained (t1, t2);
21715 return fate;
21718 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
21719 Return the TREE_LIST node with the most specialized template, if
21720 any. If there is no most specialized template, the error_mark_node
21721 is returned.
21723 Note that this function does not look at, or modify, the
21724 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
21725 returned is one of the elements of INSTANTIATIONS, callers may
21726 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
21727 and retrieve it from the value returned. */
21729 tree
21730 most_specialized_instantiation (tree templates)
21732 tree fn, champ;
21734 ++processing_template_decl;
21736 champ = templates;
21737 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
21739 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
21740 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
21741 if (fate == -1)
21742 champ = fn;
21743 else if (!fate)
21745 /* Equally specialized, move to next function. If there
21746 is no next function, nothing's most specialized. */
21747 fn = TREE_CHAIN (fn);
21748 champ = fn;
21749 if (!fn)
21750 break;
21754 if (champ)
21755 /* Now verify that champ is better than everything earlier in the
21756 instantiation list. */
21757 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
21758 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
21760 champ = NULL_TREE;
21761 break;
21765 processing_template_decl--;
21767 if (!champ)
21768 return error_mark_node;
21770 return champ;
21773 /* If DECL is a specialization of some template, return the most
21774 general such template. Otherwise, returns NULL_TREE.
21776 For example, given:
21778 template <class T> struct S { template <class U> void f(U); };
21780 if TMPL is `template <class U> void S<int>::f(U)' this will return
21781 the full template. This function will not trace past partial
21782 specializations, however. For example, given in addition:
21784 template <class T> struct S<T*> { template <class U> void f(U); };
21786 if TMPL is `template <class U> void S<int*>::f(U)' this will return
21787 `template <class T> template <class U> S<T*>::f(U)'. */
21789 tree
21790 most_general_template (tree decl)
21792 if (TREE_CODE (decl) != TEMPLATE_DECL)
21794 if (tree tinfo = get_template_info (decl))
21795 decl = TI_TEMPLATE (tinfo);
21796 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
21797 template friend, or a FIELD_DECL for a capture pack. */
21798 if (TREE_CODE (decl) != TEMPLATE_DECL)
21799 return NULL_TREE;
21802 /* Look for more and more general templates. */
21803 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
21805 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
21806 (See cp-tree.h for details.) */
21807 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
21808 break;
21810 if (CLASS_TYPE_P (TREE_TYPE (decl))
21811 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
21812 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
21813 break;
21815 /* Stop if we run into an explicitly specialized class template. */
21816 if (!DECL_NAMESPACE_SCOPE_P (decl)
21817 && DECL_CONTEXT (decl)
21818 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
21819 break;
21821 decl = DECL_TI_TEMPLATE (decl);
21824 return decl;
21827 /* Return the most specialized of the template partial specializations
21828 which can produce TARGET, a specialization of some class or variable
21829 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
21830 a TEMPLATE_DECL node corresponding to the partial specialization, while
21831 the TREE_PURPOSE is the set of template arguments that must be
21832 substituted into the template pattern in order to generate TARGET.
21834 If the choice of partial specialization is ambiguous, a diagnostic
21835 is issued, and the error_mark_node is returned. If there are no
21836 partial specializations matching TARGET, then NULL_TREE is
21837 returned, indicating that the primary template should be used. */
21839 static tree
21840 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
21842 tree list = NULL_TREE;
21843 tree t;
21844 tree champ;
21845 int fate;
21846 bool ambiguous_p;
21847 tree outer_args = NULL_TREE;
21848 tree tmpl, args;
21850 if (TYPE_P (target))
21852 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
21853 tmpl = TI_TEMPLATE (tinfo);
21854 args = TI_ARGS (tinfo);
21856 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
21858 tmpl = TREE_OPERAND (target, 0);
21859 args = TREE_OPERAND (target, 1);
21861 else if (VAR_P (target))
21863 tree tinfo = DECL_TEMPLATE_INFO (target);
21864 tmpl = TI_TEMPLATE (tinfo);
21865 args = TI_ARGS (tinfo);
21867 else
21868 gcc_unreachable ();
21870 tree main_tmpl = most_general_template (tmpl);
21872 /* For determining which partial specialization to use, only the
21873 innermost args are interesting. */
21874 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
21876 outer_args = strip_innermost_template_args (args, 1);
21877 args = INNERMOST_TEMPLATE_ARGS (args);
21880 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
21882 tree spec_args;
21883 tree spec_tmpl = TREE_VALUE (t);
21885 if (outer_args)
21887 /* Substitute in the template args from the enclosing class. */
21888 ++processing_template_decl;
21889 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
21890 --processing_template_decl;
21893 if (spec_tmpl == error_mark_node)
21894 return error_mark_node;
21896 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
21897 if (spec_args)
21899 if (outer_args)
21900 spec_args = add_to_template_args (outer_args, spec_args);
21902 /* Keep the candidate only if the constraints are satisfied,
21903 or if we're not compiling with concepts. */
21904 if (!flag_concepts
21905 || constraints_satisfied_p (spec_tmpl, spec_args))
21907 list = tree_cons (spec_args, TREE_VALUE (t), list);
21908 TREE_TYPE (list) = TREE_TYPE (t);
21913 if (! list)
21914 return NULL_TREE;
21916 ambiguous_p = false;
21917 t = list;
21918 champ = t;
21919 t = TREE_CHAIN (t);
21920 for (; t; t = TREE_CHAIN (t))
21922 fate = more_specialized_partial_spec (tmpl, champ, t);
21923 if (fate == 1)
21925 else
21927 if (fate == 0)
21929 t = TREE_CHAIN (t);
21930 if (! t)
21932 ambiguous_p = true;
21933 break;
21936 champ = t;
21940 if (!ambiguous_p)
21941 for (t = list; t && t != champ; t = TREE_CHAIN (t))
21943 fate = more_specialized_partial_spec (tmpl, champ, t);
21944 if (fate != 1)
21946 ambiguous_p = true;
21947 break;
21951 if (ambiguous_p)
21953 const char *str;
21954 char *spaces = NULL;
21955 if (!(complain & tf_error))
21956 return error_mark_node;
21957 if (TYPE_P (target))
21958 error ("ambiguous template instantiation for %q#T", target);
21959 else
21960 error ("ambiguous template instantiation for %q#D", target);
21961 str = ngettext ("candidate is:", "candidates are:", list_length (list));
21962 for (t = list; t; t = TREE_CHAIN (t))
21964 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
21965 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
21966 "%s %#qS", spaces ? spaces : str, subst);
21967 spaces = spaces ? spaces : get_spaces (str);
21969 free (spaces);
21970 return error_mark_node;
21973 return champ;
21976 /* Explicitly instantiate DECL. */
21978 void
21979 do_decl_instantiation (tree decl, tree storage)
21981 tree result = NULL_TREE;
21982 int extern_p = 0;
21984 if (!decl || decl == error_mark_node)
21985 /* An error occurred, for which grokdeclarator has already issued
21986 an appropriate message. */
21987 return;
21988 else if (! DECL_LANG_SPECIFIC (decl))
21990 error ("explicit instantiation of non-template %q#D", decl);
21991 return;
21994 bool var_templ = (DECL_TEMPLATE_INFO (decl)
21995 && variable_template_p (DECL_TI_TEMPLATE (decl)));
21997 if (VAR_P (decl) && !var_templ)
21999 /* There is an asymmetry here in the way VAR_DECLs and
22000 FUNCTION_DECLs are handled by grokdeclarator. In the case of
22001 the latter, the DECL we get back will be marked as a
22002 template instantiation, and the appropriate
22003 DECL_TEMPLATE_INFO will be set up. This does not happen for
22004 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
22005 should handle VAR_DECLs as it currently handles
22006 FUNCTION_DECLs. */
22007 if (!DECL_CLASS_SCOPE_P (decl))
22009 error ("%qD is not a static data member of a class template", decl);
22010 return;
22012 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
22013 if (!result || !VAR_P (result))
22015 error ("no matching template for %qD found", decl);
22016 return;
22018 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
22020 error ("type %qT for explicit instantiation %qD does not match "
22021 "declared type %qT", TREE_TYPE (result), decl,
22022 TREE_TYPE (decl));
22023 return;
22026 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
22028 error ("explicit instantiation of %q#D", decl);
22029 return;
22031 else
22032 result = decl;
22034 /* Check for various error cases. Note that if the explicit
22035 instantiation is valid the RESULT will currently be marked as an
22036 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
22037 until we get here. */
22039 if (DECL_TEMPLATE_SPECIALIZATION (result))
22041 /* DR 259 [temp.spec].
22043 Both an explicit instantiation and a declaration of an explicit
22044 specialization shall not appear in a program unless the explicit
22045 instantiation follows a declaration of the explicit specialization.
22047 For a given set of template parameters, if an explicit
22048 instantiation of a template appears after a declaration of an
22049 explicit specialization for that template, the explicit
22050 instantiation has no effect. */
22051 return;
22053 else if (DECL_EXPLICIT_INSTANTIATION (result))
22055 /* [temp.spec]
22057 No program shall explicitly instantiate any template more
22058 than once.
22060 We check DECL_NOT_REALLY_EXTERN so as not to complain when
22061 the first instantiation was `extern' and the second is not,
22062 and EXTERN_P for the opposite case. */
22063 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
22064 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
22065 /* If an "extern" explicit instantiation follows an ordinary
22066 explicit instantiation, the template is instantiated. */
22067 if (extern_p)
22068 return;
22070 else if (!DECL_IMPLICIT_INSTANTIATION (result))
22072 error ("no matching template for %qD found", result);
22073 return;
22075 else if (!DECL_TEMPLATE_INFO (result))
22077 permerror (input_location, "explicit instantiation of non-template %q#D", result);
22078 return;
22081 if (storage == NULL_TREE)
22083 else if (storage == ridpointers[(int) RID_EXTERN])
22085 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
22086 pedwarn (input_location, OPT_Wpedantic,
22087 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
22088 "instantiations");
22089 extern_p = 1;
22091 else
22092 error ("storage class %qD applied to template instantiation", storage);
22094 check_explicit_instantiation_namespace (result);
22095 mark_decl_instantiated (result, extern_p);
22096 if (! extern_p)
22097 instantiate_decl (result, /*defer_ok=*/true,
22098 /*expl_inst_class_mem_p=*/false);
22101 static void
22102 mark_class_instantiated (tree t, int extern_p)
22104 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
22105 SET_CLASSTYPE_INTERFACE_KNOWN (t);
22106 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
22107 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
22108 if (! extern_p)
22110 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
22111 rest_of_type_compilation (t, 1);
22115 /* Called from do_type_instantiation through binding_table_foreach to
22116 do recursive instantiation for the type bound in ENTRY. */
22117 static void
22118 bt_instantiate_type_proc (binding_entry entry, void *data)
22120 tree storage = *(tree *) data;
22122 if (MAYBE_CLASS_TYPE_P (entry->type)
22123 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
22124 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
22127 /* Called from do_type_instantiation to instantiate a member
22128 (a member function or a static member variable) of an
22129 explicitly instantiated class template. */
22130 static void
22131 instantiate_class_member (tree decl, int extern_p)
22133 mark_decl_instantiated (decl, extern_p);
22134 if (! extern_p)
22135 instantiate_decl (decl, /*defer_ok=*/true,
22136 /*expl_inst_class_mem_p=*/true);
22139 /* Perform an explicit instantiation of template class T. STORAGE, if
22140 non-null, is the RID for extern, inline or static. COMPLAIN is
22141 nonzero if this is called from the parser, zero if called recursively,
22142 since the standard is unclear (as detailed below). */
22144 void
22145 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
22147 int extern_p = 0;
22148 int nomem_p = 0;
22149 int static_p = 0;
22150 int previous_instantiation_extern_p = 0;
22152 if (TREE_CODE (t) == TYPE_DECL)
22153 t = TREE_TYPE (t);
22155 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
22157 tree tmpl =
22158 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
22159 if (tmpl)
22160 error ("explicit instantiation of non-class template %qD", tmpl);
22161 else
22162 error ("explicit instantiation of non-template type %qT", t);
22163 return;
22166 complete_type (t);
22168 if (!COMPLETE_TYPE_P (t))
22170 if (complain & tf_error)
22171 error ("explicit instantiation of %q#T before definition of template",
22173 return;
22176 if (storage != NULL_TREE)
22178 if (!in_system_header_at (input_location))
22180 if (storage == ridpointers[(int) RID_EXTERN])
22182 if (cxx_dialect == cxx98)
22183 pedwarn (input_location, OPT_Wpedantic,
22184 "ISO C++ 1998 forbids the use of %<extern%> on "
22185 "explicit instantiations");
22187 else
22188 pedwarn (input_location, OPT_Wpedantic,
22189 "ISO C++ forbids the use of %qE"
22190 " on explicit instantiations", storage);
22193 if (storage == ridpointers[(int) RID_INLINE])
22194 nomem_p = 1;
22195 else if (storage == ridpointers[(int) RID_EXTERN])
22196 extern_p = 1;
22197 else if (storage == ridpointers[(int) RID_STATIC])
22198 static_p = 1;
22199 else
22201 error ("storage class %qD applied to template instantiation",
22202 storage);
22203 extern_p = 0;
22207 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
22209 /* DR 259 [temp.spec].
22211 Both an explicit instantiation and a declaration of an explicit
22212 specialization shall not appear in a program unless the explicit
22213 instantiation follows a declaration of the explicit specialization.
22215 For a given set of template parameters, if an explicit
22216 instantiation of a template appears after a declaration of an
22217 explicit specialization for that template, the explicit
22218 instantiation has no effect. */
22219 return;
22221 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
22223 /* [temp.spec]
22225 No program shall explicitly instantiate any template more
22226 than once.
22228 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
22229 instantiation was `extern'. If EXTERN_P then the second is.
22230 These cases are OK. */
22231 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
22233 if (!previous_instantiation_extern_p && !extern_p
22234 && (complain & tf_error))
22235 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
22237 /* If we've already instantiated the template, just return now. */
22238 if (!CLASSTYPE_INTERFACE_ONLY (t))
22239 return;
22242 check_explicit_instantiation_namespace (TYPE_NAME (t));
22243 mark_class_instantiated (t, extern_p);
22245 if (nomem_p)
22246 return;
22249 tree tmp;
22251 /* In contrast to implicit instantiation, where only the
22252 declarations, and not the definitions, of members are
22253 instantiated, we have here:
22255 [temp.explicit]
22257 The explicit instantiation of a class template specialization
22258 implies the instantiation of all of its members not
22259 previously explicitly specialized in the translation unit
22260 containing the explicit instantiation.
22262 Of course, we can't instantiate member template classes, since
22263 we don't have any arguments for them. Note that the standard
22264 is unclear on whether the instantiation of the members are
22265 *explicit* instantiations or not. However, the most natural
22266 interpretation is that it should be an explicit instantiation. */
22268 if (! static_p)
22269 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
22270 if (TREE_CODE (tmp) == FUNCTION_DECL
22271 && DECL_TEMPLATE_INSTANTIATION (tmp)
22272 && user_provided_p (tmp))
22273 instantiate_class_member (tmp, extern_p);
22275 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
22276 if (VAR_P (tmp) && DECL_TEMPLATE_INSTANTIATION (tmp))
22277 instantiate_class_member (tmp, extern_p);
22279 if (CLASSTYPE_NESTED_UTDS (t))
22280 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
22281 bt_instantiate_type_proc, &storage);
22285 /* Given a function DECL, which is a specialization of TMPL, modify
22286 DECL to be a re-instantiation of TMPL with the same template
22287 arguments. TMPL should be the template into which tsubst'ing
22288 should occur for DECL, not the most general template.
22290 One reason for doing this is a scenario like this:
22292 template <class T>
22293 void f(const T&, int i);
22295 void g() { f(3, 7); }
22297 template <class T>
22298 void f(const T& t, const int i) { }
22300 Note that when the template is first instantiated, with
22301 instantiate_template, the resulting DECL will have no name for the
22302 first parameter, and the wrong type for the second. So, when we go
22303 to instantiate the DECL, we regenerate it. */
22305 static void
22306 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
22308 /* The arguments used to instantiate DECL, from the most general
22309 template. */
22310 tree code_pattern;
22312 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
22314 /* Make sure that we can see identifiers, and compute access
22315 correctly. */
22316 push_access_scope (decl);
22318 if (TREE_CODE (decl) == FUNCTION_DECL)
22320 tree decl_parm;
22321 tree pattern_parm;
22322 tree specs;
22323 int args_depth;
22324 int parms_depth;
22326 args_depth = TMPL_ARGS_DEPTH (args);
22327 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
22328 if (args_depth > parms_depth)
22329 args = get_innermost_template_args (args, parms_depth);
22331 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
22332 args, tf_error, NULL_TREE,
22333 /*defer_ok*/false);
22334 if (specs && specs != error_mark_node)
22335 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
22336 specs);
22338 /* Merge parameter declarations. */
22339 decl_parm = skip_artificial_parms_for (decl,
22340 DECL_ARGUMENTS (decl));
22341 pattern_parm
22342 = skip_artificial_parms_for (code_pattern,
22343 DECL_ARGUMENTS (code_pattern));
22344 while (decl_parm && !DECL_PACK_P (pattern_parm))
22346 tree parm_type;
22347 tree attributes;
22349 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22350 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
22351 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
22352 NULL_TREE);
22353 parm_type = type_decays_to (parm_type);
22354 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22355 TREE_TYPE (decl_parm) = parm_type;
22356 attributes = DECL_ATTRIBUTES (pattern_parm);
22357 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22359 DECL_ATTRIBUTES (decl_parm) = attributes;
22360 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22362 decl_parm = DECL_CHAIN (decl_parm);
22363 pattern_parm = DECL_CHAIN (pattern_parm);
22365 /* Merge any parameters that match with the function parameter
22366 pack. */
22367 if (pattern_parm && DECL_PACK_P (pattern_parm))
22369 int i, len;
22370 tree expanded_types;
22371 /* Expand the TYPE_PACK_EXPANSION that provides the types for
22372 the parameters in this function parameter pack. */
22373 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
22374 args, tf_error, NULL_TREE);
22375 len = TREE_VEC_LENGTH (expanded_types);
22376 for (i = 0; i < len; i++)
22378 tree parm_type;
22379 tree attributes;
22381 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22382 /* Rename the parameter to include the index. */
22383 DECL_NAME (decl_parm) =
22384 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
22385 parm_type = TREE_VEC_ELT (expanded_types, i);
22386 parm_type = type_decays_to (parm_type);
22387 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22388 TREE_TYPE (decl_parm) = parm_type;
22389 attributes = DECL_ATTRIBUTES (pattern_parm);
22390 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22392 DECL_ATTRIBUTES (decl_parm) = attributes;
22393 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22395 decl_parm = DECL_CHAIN (decl_parm);
22398 /* Merge additional specifiers from the CODE_PATTERN. */
22399 if (DECL_DECLARED_INLINE_P (code_pattern)
22400 && !DECL_DECLARED_INLINE_P (decl))
22401 DECL_DECLARED_INLINE_P (decl) = 1;
22403 else if (VAR_P (decl))
22405 DECL_INITIAL (decl) =
22406 tsubst_expr (DECL_INITIAL (code_pattern), args,
22407 tf_error, DECL_TI_TEMPLATE (decl),
22408 /*integral_constant_expression_p=*/false);
22409 if (VAR_HAD_UNKNOWN_BOUND (decl))
22410 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
22411 tf_error, DECL_TI_TEMPLATE (decl));
22413 else
22414 gcc_unreachable ();
22416 pop_access_scope (decl);
22419 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
22420 substituted to get DECL. */
22422 tree
22423 template_for_substitution (tree decl)
22425 tree tmpl = DECL_TI_TEMPLATE (decl);
22427 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
22428 for the instantiation. This is not always the most general
22429 template. Consider, for example:
22431 template <class T>
22432 struct S { template <class U> void f();
22433 template <> void f<int>(); };
22435 and an instantiation of S<double>::f<int>. We want TD to be the
22436 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
22437 while (/* An instantiation cannot have a definition, so we need a
22438 more general template. */
22439 DECL_TEMPLATE_INSTANTIATION (tmpl)
22440 /* We must also deal with friend templates. Given:
22442 template <class T> struct S {
22443 template <class U> friend void f() {};
22446 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
22447 so far as the language is concerned, but that's still
22448 where we get the pattern for the instantiation from. On
22449 other hand, if the definition comes outside the class, say:
22451 template <class T> struct S {
22452 template <class U> friend void f();
22454 template <class U> friend void f() {}
22456 we don't need to look any further. That's what the check for
22457 DECL_INITIAL is for. */
22458 || (TREE_CODE (decl) == FUNCTION_DECL
22459 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
22460 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
22462 /* The present template, TD, should not be a definition. If it
22463 were a definition, we should be using it! Note that we
22464 cannot restructure the loop to just keep going until we find
22465 a template with a definition, since that might go too far if
22466 a specialization was declared, but not defined. */
22468 /* Fetch the more general template. */
22469 tmpl = DECL_TI_TEMPLATE (tmpl);
22472 return tmpl;
22475 /* Returns true if we need to instantiate this template instance even if we
22476 know we aren't going to emit it. */
22478 bool
22479 always_instantiate_p (tree decl)
22481 /* We always instantiate inline functions so that we can inline them. An
22482 explicit instantiation declaration prohibits implicit instantiation of
22483 non-inline functions. With high levels of optimization, we would
22484 normally inline non-inline functions -- but we're not allowed to do
22485 that for "extern template" functions. Therefore, we check
22486 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
22487 return ((TREE_CODE (decl) == FUNCTION_DECL
22488 && (DECL_DECLARED_INLINE_P (decl)
22489 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
22490 /* And we need to instantiate static data members so that
22491 their initializers are available in integral constant
22492 expressions. */
22493 || (VAR_P (decl)
22494 && decl_maybe_constant_var_p (decl)));
22497 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
22498 instantiate it now, modifying TREE_TYPE (fn). */
22500 void
22501 maybe_instantiate_noexcept (tree fn)
22503 tree fntype, spec, noex, clone;
22505 /* Don't instantiate a noexcept-specification from template context. */
22506 if (processing_template_decl)
22507 return;
22509 if (DECL_CLONED_FUNCTION_P (fn))
22510 fn = DECL_CLONED_FUNCTION (fn);
22511 fntype = TREE_TYPE (fn);
22512 spec = TYPE_RAISES_EXCEPTIONS (fntype);
22514 if (!spec || !TREE_PURPOSE (spec))
22515 return;
22517 noex = TREE_PURPOSE (spec);
22519 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
22521 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
22522 spec = get_defaulted_eh_spec (fn);
22523 else if (push_tinst_level (fn))
22525 push_access_scope (fn);
22526 push_deferring_access_checks (dk_no_deferred);
22527 input_location = DECL_SOURCE_LOCATION (fn);
22528 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
22529 DEFERRED_NOEXCEPT_ARGS (noex),
22530 tf_warning_or_error, fn,
22531 /*function_p=*/false,
22532 /*integral_constant_expression_p=*/true);
22533 pop_deferring_access_checks ();
22534 pop_access_scope (fn);
22535 pop_tinst_level ();
22536 spec = build_noexcept_spec (noex, tf_warning_or_error);
22537 if (spec == error_mark_node)
22538 spec = noexcept_false_spec;
22540 else
22541 spec = noexcept_false_spec;
22543 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
22546 FOR_EACH_CLONE (clone, fn)
22548 if (TREE_TYPE (clone) == fntype)
22549 TREE_TYPE (clone) = TREE_TYPE (fn);
22550 else
22551 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
22555 /* Produce the definition of D, a _DECL generated from a template. If
22556 DEFER_OK is true, then we don't have to actually do the
22557 instantiation now; we just have to do it sometime. Normally it is
22558 an error if this is an explicit instantiation but D is undefined.
22559 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
22560 instantiated class template. */
22562 tree
22563 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
22565 tree tmpl = DECL_TI_TEMPLATE (d);
22566 tree gen_args;
22567 tree args;
22568 tree td;
22569 tree code_pattern;
22570 tree spec;
22571 tree gen_tmpl;
22572 bool pattern_defined;
22573 location_t saved_loc = input_location;
22574 int saved_unevaluated_operand = cp_unevaluated_operand;
22575 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
22576 bool external_p;
22577 bool deleted_p;
22579 /* This function should only be used to instantiate templates for
22580 functions and static member variables. */
22581 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
22583 /* A concept is never instantiated. */
22584 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
22586 /* Variables are never deferred; if instantiation is required, they
22587 are instantiated right away. That allows for better code in the
22588 case that an expression refers to the value of the variable --
22589 if the variable has a constant value the referring expression can
22590 take advantage of that fact. */
22591 if (VAR_P (d))
22592 defer_ok = false;
22594 /* Don't instantiate cloned functions. Instead, instantiate the
22595 functions they cloned. */
22596 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
22597 d = DECL_CLONED_FUNCTION (d);
22599 if (DECL_TEMPLATE_INSTANTIATED (d)
22600 || (TREE_CODE (d) == FUNCTION_DECL
22601 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
22602 || DECL_TEMPLATE_SPECIALIZATION (d))
22603 /* D has already been instantiated or explicitly specialized, so
22604 there's nothing for us to do here.
22606 It might seem reasonable to check whether or not D is an explicit
22607 instantiation, and, if so, stop here. But when an explicit
22608 instantiation is deferred until the end of the compilation,
22609 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
22610 the instantiation. */
22611 return d;
22613 /* Check to see whether we know that this template will be
22614 instantiated in some other file, as with "extern template"
22615 extension. */
22616 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
22618 /* In general, we do not instantiate such templates. */
22619 if (external_p && !always_instantiate_p (d))
22620 return d;
22622 gen_tmpl = most_general_template (tmpl);
22623 gen_args = DECL_TI_ARGS (d);
22625 if (tmpl != gen_tmpl)
22626 /* We should already have the extra args. */
22627 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
22628 == TMPL_ARGS_DEPTH (gen_args));
22629 /* And what's in the hash table should match D. */
22630 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
22631 || spec == NULL_TREE);
22633 /* This needs to happen before any tsubsting. */
22634 if (! push_tinst_level (d))
22635 return d;
22637 timevar_push (TV_TEMPLATE_INST);
22639 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
22640 for the instantiation. */
22641 td = template_for_substitution (d);
22642 args = gen_args;
22644 if (VAR_P (d))
22646 /* Look up an explicit specialization, if any. */
22647 tree tid = lookup_template_variable (gen_tmpl, gen_args);
22648 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
22649 if (elt && elt != error_mark_node)
22651 td = TREE_VALUE (elt);
22652 args = TREE_PURPOSE (elt);
22656 code_pattern = DECL_TEMPLATE_RESULT (td);
22658 /* We should never be trying to instantiate a member of a class
22659 template or partial specialization. */
22660 gcc_assert (d != code_pattern);
22662 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
22663 || DECL_TEMPLATE_SPECIALIZATION (td))
22664 /* In the case of a friend template whose definition is provided
22665 outside the class, we may have too many arguments. Drop the
22666 ones we don't need. The same is true for specializations. */
22667 args = get_innermost_template_args
22668 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
22670 if (TREE_CODE (d) == FUNCTION_DECL)
22672 deleted_p = DECL_DELETED_FN (code_pattern);
22673 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
22674 && DECL_INITIAL (code_pattern) != error_mark_node)
22675 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
22676 || deleted_p);
22678 else
22680 deleted_p = false;
22681 if (DECL_CLASS_SCOPE_P (code_pattern))
22682 pattern_defined = (! DECL_IN_AGGR_P (code_pattern)
22683 || DECL_INLINE_VAR_P (code_pattern));
22684 else
22685 pattern_defined = ! DECL_EXTERNAL (code_pattern);
22688 /* We may be in the middle of deferred access check. Disable it now. */
22689 push_deferring_access_checks (dk_no_deferred);
22691 /* Unless an explicit instantiation directive has already determined
22692 the linkage of D, remember that a definition is available for
22693 this entity. */
22694 if (pattern_defined
22695 && !DECL_INTERFACE_KNOWN (d)
22696 && !DECL_NOT_REALLY_EXTERN (d))
22697 mark_definable (d);
22699 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
22700 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
22701 input_location = DECL_SOURCE_LOCATION (d);
22703 /* If D is a member of an explicitly instantiated class template,
22704 and no definition is available, treat it like an implicit
22705 instantiation. */
22706 if (!pattern_defined && expl_inst_class_mem_p
22707 && DECL_EXPLICIT_INSTANTIATION (d))
22709 /* Leave linkage flags alone on instantiations with anonymous
22710 visibility. */
22711 if (TREE_PUBLIC (d))
22713 DECL_NOT_REALLY_EXTERN (d) = 0;
22714 DECL_INTERFACE_KNOWN (d) = 0;
22716 SET_DECL_IMPLICIT_INSTANTIATION (d);
22719 /* Defer all other templates, unless we have been explicitly
22720 forbidden from doing so. */
22721 if (/* If there is no definition, we cannot instantiate the
22722 template. */
22723 ! pattern_defined
22724 /* If it's OK to postpone instantiation, do so. */
22725 || defer_ok
22726 /* If this is a static data member that will be defined
22727 elsewhere, we don't want to instantiate the entire data
22728 member, but we do want to instantiate the initializer so that
22729 we can substitute that elsewhere. */
22730 || (external_p && VAR_P (d))
22731 /* Handle here a deleted function too, avoid generating
22732 its body (c++/61080). */
22733 || deleted_p)
22735 /* The definition of the static data member is now required so
22736 we must substitute the initializer. */
22737 if (VAR_P (d)
22738 && !DECL_INITIAL (d)
22739 && DECL_INITIAL (code_pattern))
22741 tree ns;
22742 tree init;
22743 bool const_init = false;
22744 bool enter_context = DECL_CLASS_SCOPE_P (d);
22746 ns = decl_namespace_context (d);
22747 push_nested_namespace (ns);
22748 if (enter_context)
22749 push_nested_class (DECL_CONTEXT (d));
22750 init = tsubst_expr (DECL_INITIAL (code_pattern),
22751 args,
22752 tf_warning_or_error, NULL_TREE,
22753 /*integral_constant_expression_p=*/false);
22754 /* If instantiating the initializer involved instantiating this
22755 again, don't call cp_finish_decl twice. */
22756 if (!DECL_INITIAL (d))
22758 /* Make sure the initializer is still constant, in case of
22759 circular dependency (template/instantiate6.C). */
22760 const_init
22761 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
22762 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
22763 /*asmspec_tree=*/NULL_TREE,
22764 LOOKUP_ONLYCONVERTING);
22766 if (enter_context)
22767 pop_nested_class ();
22768 pop_nested_namespace (ns);
22771 /* We restore the source position here because it's used by
22772 add_pending_template. */
22773 input_location = saved_loc;
22775 if (at_eof && !pattern_defined
22776 && DECL_EXPLICIT_INSTANTIATION (d)
22777 && DECL_NOT_REALLY_EXTERN (d))
22778 /* [temp.explicit]
22780 The definition of a non-exported function template, a
22781 non-exported member function template, or a non-exported
22782 member function or static data member of a class template
22783 shall be present in every translation unit in which it is
22784 explicitly instantiated. */
22785 permerror (input_location, "explicit instantiation of %qD "
22786 "but no definition available", d);
22788 /* If we're in unevaluated context, we just wanted to get the
22789 constant value; this isn't an odr use, so don't queue
22790 a full instantiation. */
22791 if (cp_unevaluated_operand != 0)
22792 goto out;
22793 /* ??? Historically, we have instantiated inline functions, even
22794 when marked as "extern template". */
22795 if (!(external_p && VAR_P (d)))
22796 add_pending_template (d);
22797 goto out;
22799 /* Tell the repository that D is available in this translation unit
22800 -- and see if it is supposed to be instantiated here. */
22801 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
22803 /* In a PCH file, despite the fact that the repository hasn't
22804 requested instantiation in the PCH it is still possible that
22805 an instantiation will be required in a file that includes the
22806 PCH. */
22807 if (pch_file)
22808 add_pending_template (d);
22809 /* Instantiate inline functions so that the inliner can do its
22810 job, even though we'll not be emitting a copy of this
22811 function. */
22812 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
22813 goto out;
22816 bool push_to_top, nested;
22817 tree fn_context;
22818 fn_context = decl_function_context (d);
22819 nested = current_function_decl != NULL_TREE;
22820 push_to_top = !(nested && fn_context == current_function_decl);
22822 vec<tree> omp_privatization_save;
22823 if (nested)
22824 save_omp_privatization_clauses (omp_privatization_save);
22826 if (push_to_top)
22827 push_to_top_level ();
22828 else
22830 push_function_context ();
22831 cp_unevaluated_operand = 0;
22832 c_inhibit_evaluation_warnings = 0;
22835 /* Mark D as instantiated so that recursive calls to
22836 instantiate_decl do not try to instantiate it again. */
22837 DECL_TEMPLATE_INSTANTIATED (d) = 1;
22839 /* Regenerate the declaration in case the template has been modified
22840 by a subsequent redeclaration. */
22841 regenerate_decl_from_template (d, td, args);
22843 /* We already set the file and line above. Reset them now in case
22844 they changed as a result of calling regenerate_decl_from_template. */
22845 input_location = DECL_SOURCE_LOCATION (d);
22847 if (VAR_P (d))
22849 tree init;
22850 bool const_init = false;
22852 /* Clear out DECL_RTL; whatever was there before may not be right
22853 since we've reset the type of the declaration. */
22854 SET_DECL_RTL (d, NULL);
22855 DECL_IN_AGGR_P (d) = 0;
22857 /* The initializer is placed in DECL_INITIAL by
22858 regenerate_decl_from_template so we don't need to
22859 push/pop_access_scope again here. Pull it out so that
22860 cp_finish_decl can process it. */
22861 init = DECL_INITIAL (d);
22862 DECL_INITIAL (d) = NULL_TREE;
22863 DECL_INITIALIZED_P (d) = 0;
22865 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
22866 initializer. That function will defer actual emission until
22867 we have a chance to determine linkage. */
22868 DECL_EXTERNAL (d) = 0;
22870 /* Enter the scope of D so that access-checking works correctly. */
22871 bool enter_context = DECL_CLASS_SCOPE_P (d);
22872 if (enter_context)
22873 push_nested_class (DECL_CONTEXT (d));
22875 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
22876 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
22878 if (enter_context)
22879 pop_nested_class ();
22881 if (variable_template_p (gen_tmpl))
22882 note_variable_template_instantiation (d);
22884 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
22885 synthesize_method (d);
22886 else if (TREE_CODE (d) == FUNCTION_DECL)
22888 hash_map<tree, tree> *saved_local_specializations;
22889 tree tmpl_parm;
22890 tree spec_parm;
22891 tree block = NULL_TREE;
22892 tree lambda_ctx = NULL_TREE;
22894 /* Save away the current list, in case we are instantiating one
22895 template from within the body of another. */
22896 saved_local_specializations = local_specializations;
22898 /* Set up the list of local specializations. */
22899 local_specializations = new hash_map<tree, tree>;
22901 /* Set up context. */
22902 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
22903 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
22904 block = push_stmt_list ();
22905 else
22907 if (push_to_top && LAMBDA_FUNCTION_P (d))
22909 /* When instantiating a lambda's templated function
22910 operator, we need to push the non-lambda class scope
22911 of the lambda itself so that the nested function
22912 stack is sufficiently correct to deal with this
22913 capture. */
22914 lambda_ctx = DECL_CONTEXT (d);
22916 lambda_ctx = decl_type_context (TYPE_NAME (lambda_ctx));
22917 while (lambda_ctx && LAMBDA_TYPE_P (lambda_ctx));
22918 if (lambda_ctx)
22919 push_nested_class (lambda_ctx);
22921 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
22924 /* Some typedefs referenced from within the template code need to be
22925 access checked at template instantiation time, i.e now. These
22926 types were added to the template at parsing time. Let's get those
22927 and perform the access checks then. */
22928 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
22929 args);
22931 /* Create substitution entries for the parameters. */
22932 tmpl_parm = DECL_ARGUMENTS (code_pattern);
22933 spec_parm = DECL_ARGUMENTS (d);
22934 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
22936 register_local_specialization (spec_parm, tmpl_parm);
22937 spec_parm = skip_artificial_parms_for (d, spec_parm);
22938 tmpl_parm = skip_artificial_parms_for (code_pattern, tmpl_parm);
22940 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
22942 if (!DECL_PACK_P (tmpl_parm))
22944 register_local_specialization (spec_parm, tmpl_parm);
22945 spec_parm = DECL_CHAIN (spec_parm);
22947 else
22949 /* Register the (value) argument pack as a specialization of
22950 TMPL_PARM, then move on. */
22951 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
22952 register_local_specialization (argpack, tmpl_parm);
22955 gcc_assert (!spec_parm);
22957 /* Substitute into the body of the function. */
22958 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
22959 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
22960 tf_warning_or_error, tmpl);
22961 else
22963 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
22964 tf_warning_or_error, tmpl,
22965 /*integral_constant_expression_p=*/false);
22967 /* Set the current input_location to the end of the function
22968 so that finish_function knows where we are. */
22969 input_location
22970 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
22972 /* Remember if we saw an infinite loop in the template. */
22973 current_function_infinite_loop
22974 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
22977 /* We don't need the local specializations any more. */
22978 delete local_specializations;
22979 local_specializations = saved_local_specializations;
22981 /* Finish the function. */
22982 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
22983 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
22984 DECL_SAVED_TREE (d) = pop_stmt_list (block);
22985 else
22987 d = finish_function (0);
22988 expand_or_defer_fn (d);
22990 if (lambda_ctx)
22991 pop_nested_class ();
22993 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
22994 cp_check_omp_declare_reduction (d);
22997 /* We're not deferring instantiation any more. */
22998 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
23000 if (push_to_top)
23001 pop_from_top_level ();
23002 else
23003 pop_function_context ();
23005 if (nested)
23006 restore_omp_privatization_clauses (omp_privatization_save);
23008 out:
23009 pop_deferring_access_checks ();
23010 timevar_pop (TV_TEMPLATE_INST);
23011 pop_tinst_level ();
23012 input_location = saved_loc;
23013 cp_unevaluated_operand = saved_unevaluated_operand;
23014 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23016 return d;
23019 /* Run through the list of templates that we wish we could
23020 instantiate, and instantiate any we can. RETRIES is the
23021 number of times we retry pending template instantiation. */
23023 void
23024 instantiate_pending_templates (int retries)
23026 int reconsider;
23027 location_t saved_loc = input_location;
23029 /* Instantiating templates may trigger vtable generation. This in turn
23030 may require further template instantiations. We place a limit here
23031 to avoid infinite loop. */
23032 if (pending_templates && retries >= max_tinst_depth)
23034 tree decl = pending_templates->tinst->decl;
23036 fatal_error (input_location,
23037 "template instantiation depth exceeds maximum of %d"
23038 " instantiating %q+D, possibly from virtual table generation"
23039 " (use -ftemplate-depth= to increase the maximum)",
23040 max_tinst_depth, decl);
23041 if (TREE_CODE (decl) == FUNCTION_DECL)
23042 /* Pretend that we defined it. */
23043 DECL_INITIAL (decl) = error_mark_node;
23044 return;
23049 struct pending_template **t = &pending_templates;
23050 struct pending_template *last = NULL;
23051 reconsider = 0;
23052 while (*t)
23054 tree instantiation = reopen_tinst_level ((*t)->tinst);
23055 bool complete = false;
23057 if (TYPE_P (instantiation))
23059 tree fn;
23061 if (!COMPLETE_TYPE_P (instantiation))
23063 instantiate_class_template (instantiation);
23064 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
23065 for (fn = TYPE_METHODS (instantiation);
23067 fn = TREE_CHAIN (fn))
23068 if (! DECL_ARTIFICIAL (fn))
23069 instantiate_decl (fn,
23070 /*defer_ok=*/false,
23071 /*expl_inst_class_mem_p=*/false);
23072 if (COMPLETE_TYPE_P (instantiation))
23073 reconsider = 1;
23076 complete = COMPLETE_TYPE_P (instantiation);
23078 else
23080 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
23081 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
23083 instantiation
23084 = instantiate_decl (instantiation,
23085 /*defer_ok=*/false,
23086 /*expl_inst_class_mem_p=*/false);
23087 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
23088 reconsider = 1;
23091 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
23092 || DECL_TEMPLATE_INSTANTIATED (instantiation));
23095 if (complete)
23096 /* If INSTANTIATION has been instantiated, then we don't
23097 need to consider it again in the future. */
23098 *t = (*t)->next;
23099 else
23101 last = *t;
23102 t = &(*t)->next;
23104 tinst_depth = 0;
23105 current_tinst_level = NULL;
23107 last_pending_template = last;
23109 while (reconsider);
23111 input_location = saved_loc;
23114 /* Substitute ARGVEC into T, which is a list of initializers for
23115 either base class or a non-static data member. The TREE_PURPOSEs
23116 are DECLs, and the TREE_VALUEs are the initializer values. Used by
23117 instantiate_decl. */
23119 static tree
23120 tsubst_initializer_list (tree t, tree argvec)
23122 tree inits = NULL_TREE;
23124 for (; t; t = TREE_CHAIN (t))
23126 tree decl;
23127 tree init;
23128 tree expanded_bases = NULL_TREE;
23129 tree expanded_arguments = NULL_TREE;
23130 int i, len = 1;
23132 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
23134 tree expr;
23135 tree arg;
23137 /* Expand the base class expansion type into separate base
23138 classes. */
23139 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
23140 tf_warning_or_error,
23141 NULL_TREE);
23142 if (expanded_bases == error_mark_node)
23143 continue;
23145 /* We'll be building separate TREE_LISTs of arguments for
23146 each base. */
23147 len = TREE_VEC_LENGTH (expanded_bases);
23148 expanded_arguments = make_tree_vec (len);
23149 for (i = 0; i < len; i++)
23150 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
23152 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
23153 expand each argument in the TREE_VALUE of t. */
23154 expr = make_node (EXPR_PACK_EXPANSION);
23155 PACK_EXPANSION_LOCAL_P (expr) = true;
23156 PACK_EXPANSION_PARAMETER_PACKS (expr) =
23157 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
23159 if (TREE_VALUE (t) == void_type_node)
23160 /* VOID_TYPE_NODE is used to indicate
23161 value-initialization. */
23163 for (i = 0; i < len; i++)
23164 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
23166 else
23168 /* Substitute parameter packs into each argument in the
23169 TREE_LIST. */
23170 in_base_initializer = 1;
23171 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
23173 tree expanded_exprs;
23175 /* Expand the argument. */
23176 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
23177 expanded_exprs
23178 = tsubst_pack_expansion (expr, argvec,
23179 tf_warning_or_error,
23180 NULL_TREE);
23181 if (expanded_exprs == error_mark_node)
23182 continue;
23184 /* Prepend each of the expanded expressions to the
23185 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
23186 for (i = 0; i < len; i++)
23188 TREE_VEC_ELT (expanded_arguments, i) =
23189 tree_cons (NULL_TREE,
23190 TREE_VEC_ELT (expanded_exprs, i),
23191 TREE_VEC_ELT (expanded_arguments, i));
23194 in_base_initializer = 0;
23196 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
23197 since we built them backwards. */
23198 for (i = 0; i < len; i++)
23200 TREE_VEC_ELT (expanded_arguments, i) =
23201 nreverse (TREE_VEC_ELT (expanded_arguments, i));
23206 for (i = 0; i < len; ++i)
23208 if (expanded_bases)
23210 decl = TREE_VEC_ELT (expanded_bases, i);
23211 decl = expand_member_init (decl);
23212 init = TREE_VEC_ELT (expanded_arguments, i);
23214 else
23216 tree tmp;
23217 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
23218 tf_warning_or_error, NULL_TREE);
23220 decl = expand_member_init (decl);
23221 if (decl && !DECL_P (decl))
23222 in_base_initializer = 1;
23224 init = TREE_VALUE (t);
23225 tmp = init;
23226 if (init != void_type_node)
23227 init = tsubst_expr (init, argvec,
23228 tf_warning_or_error, NULL_TREE,
23229 /*integral_constant_expression_p=*/false);
23230 if (init == NULL_TREE && tmp != NULL_TREE)
23231 /* If we had an initializer but it instantiated to nothing,
23232 value-initialize the object. This will only occur when
23233 the initializer was a pack expansion where the parameter
23234 packs used in that expansion were of length zero. */
23235 init = void_type_node;
23236 in_base_initializer = 0;
23239 if (decl)
23241 init = build_tree_list (decl, init);
23242 TREE_CHAIN (init) = inits;
23243 inits = init;
23247 return inits;
23250 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
23252 static void
23253 set_current_access_from_decl (tree decl)
23255 if (TREE_PRIVATE (decl))
23256 current_access_specifier = access_private_node;
23257 else if (TREE_PROTECTED (decl))
23258 current_access_specifier = access_protected_node;
23259 else
23260 current_access_specifier = access_public_node;
23263 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
23264 is the instantiation (which should have been created with
23265 start_enum) and ARGS are the template arguments to use. */
23267 static void
23268 tsubst_enum (tree tag, tree newtag, tree args)
23270 tree e;
23272 if (SCOPED_ENUM_P (newtag))
23273 begin_scope (sk_scoped_enum, newtag);
23275 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
23277 tree value;
23278 tree decl;
23280 decl = TREE_VALUE (e);
23281 /* Note that in a template enum, the TREE_VALUE is the
23282 CONST_DECL, not the corresponding INTEGER_CST. */
23283 value = tsubst_expr (DECL_INITIAL (decl),
23284 args, tf_warning_or_error, NULL_TREE,
23285 /*integral_constant_expression_p=*/true);
23287 /* Give this enumeration constant the correct access. */
23288 set_current_access_from_decl (decl);
23290 /* Actually build the enumerator itself. Here we're assuming that
23291 enumerators can't have dependent attributes. */
23292 build_enumerator (DECL_NAME (decl), value, newtag,
23293 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
23296 if (SCOPED_ENUM_P (newtag))
23297 finish_scope ();
23299 finish_enum_value_list (newtag);
23300 finish_enum (newtag);
23302 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
23303 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
23306 /* DECL is a FUNCTION_DECL that is a template specialization. Return
23307 its type -- but without substituting the innermost set of template
23308 arguments. So, innermost set of template parameters will appear in
23309 the type. */
23311 tree
23312 get_mostly_instantiated_function_type (tree decl)
23314 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
23315 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
23318 /* Return truthvalue if we're processing a template different from
23319 the last one involved in diagnostics. */
23320 bool
23321 problematic_instantiation_changed (void)
23323 return current_tinst_level != last_error_tinst_level;
23326 /* Remember current template involved in diagnostics. */
23327 void
23328 record_last_problematic_instantiation (void)
23330 last_error_tinst_level = current_tinst_level;
23333 struct tinst_level *
23334 current_instantiation (void)
23336 return current_tinst_level;
23339 /* Return TRUE if current_function_decl is being instantiated, false
23340 otherwise. */
23342 bool
23343 instantiating_current_function_p (void)
23345 return (current_instantiation ()
23346 && current_instantiation ()->decl == current_function_decl);
23349 /* [temp.param] Check that template non-type parm TYPE is of an allowable
23350 type. Return zero for ok, nonzero for disallowed. Issue error and
23351 warning messages under control of COMPLAIN. */
23353 static int
23354 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
23356 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
23357 return 0;
23358 else if (POINTER_TYPE_P (type))
23359 return 0;
23360 else if (TYPE_PTRMEM_P (type))
23361 return 0;
23362 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
23363 return 0;
23364 else if (TREE_CODE (type) == TYPENAME_TYPE)
23365 return 0;
23366 else if (TREE_CODE (type) == DECLTYPE_TYPE)
23367 return 0;
23368 else if (TREE_CODE (type) == NULLPTR_TYPE)
23369 return 0;
23370 /* A bound template template parm could later be instantiated to have a valid
23371 nontype parm type via an alias template. */
23372 else if (cxx_dialect >= cxx11
23373 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23374 return 0;
23376 if (complain & tf_error)
23378 if (type == error_mark_node)
23379 inform (input_location, "invalid template non-type parameter");
23380 else
23381 error ("%q#T is not a valid type for a template non-type parameter",
23382 type);
23384 return 1;
23387 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
23388 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
23390 static bool
23391 dependent_type_p_r (tree type)
23393 tree scope;
23395 /* [temp.dep.type]
23397 A type is dependent if it is:
23399 -- a template parameter. Template template parameters are types
23400 for us (since TYPE_P holds true for them) so we handle
23401 them here. */
23402 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
23403 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
23404 return true;
23405 /* -- a qualified-id with a nested-name-specifier which contains a
23406 class-name that names a dependent type or whose unqualified-id
23407 names a dependent type. */
23408 if (TREE_CODE (type) == TYPENAME_TYPE)
23409 return true;
23411 /* An alias template specialization can be dependent even if the
23412 resulting type is not. */
23413 if (dependent_alias_template_spec_p (type))
23414 return true;
23416 /* -- a cv-qualified type where the cv-unqualified type is
23417 dependent.
23418 No code is necessary for this bullet; the code below handles
23419 cv-qualified types, and we don't want to strip aliases with
23420 TYPE_MAIN_VARIANT because of DR 1558. */
23421 /* -- a compound type constructed from any dependent type. */
23422 if (TYPE_PTRMEM_P (type))
23423 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
23424 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
23425 (type)));
23426 else if (TYPE_PTR_P (type)
23427 || TREE_CODE (type) == REFERENCE_TYPE)
23428 return dependent_type_p (TREE_TYPE (type));
23429 else if (TREE_CODE (type) == FUNCTION_TYPE
23430 || TREE_CODE (type) == METHOD_TYPE)
23432 tree arg_type;
23434 if (dependent_type_p (TREE_TYPE (type)))
23435 return true;
23436 for (arg_type = TYPE_ARG_TYPES (type);
23437 arg_type;
23438 arg_type = TREE_CHAIN (arg_type))
23439 if (dependent_type_p (TREE_VALUE (arg_type)))
23440 return true;
23441 if (cxx_dialect >= cxx1z)
23443 /* A value-dependent noexcept-specifier makes the type dependent. */
23444 tree spec = TYPE_RAISES_EXCEPTIONS (type);
23445 if (spec && TREE_PURPOSE (spec)
23446 && value_dependent_expression_p (TREE_PURPOSE (spec)))
23447 return true;
23449 return false;
23451 /* -- an array type constructed from any dependent type or whose
23452 size is specified by a constant expression that is
23453 value-dependent.
23455 We checked for type- and value-dependence of the bounds in
23456 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
23457 if (TREE_CODE (type) == ARRAY_TYPE)
23459 if (TYPE_DOMAIN (type)
23460 && dependent_type_p (TYPE_DOMAIN (type)))
23461 return true;
23462 return dependent_type_p (TREE_TYPE (type));
23465 /* -- a template-id in which either the template name is a template
23466 parameter ... */
23467 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23468 return true;
23469 /* ... or any of the template arguments is a dependent type or
23470 an expression that is type-dependent or value-dependent. */
23471 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
23472 && (any_dependent_template_arguments_p
23473 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
23474 return true;
23476 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
23477 dependent; if the argument of the `typeof' expression is not
23478 type-dependent, then it should already been have resolved. */
23479 if (TREE_CODE (type) == TYPEOF_TYPE
23480 || TREE_CODE (type) == DECLTYPE_TYPE
23481 || TREE_CODE (type) == UNDERLYING_TYPE)
23482 return true;
23484 /* A template argument pack is dependent if any of its packed
23485 arguments are. */
23486 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
23488 tree args = ARGUMENT_PACK_ARGS (type);
23489 int i, len = TREE_VEC_LENGTH (args);
23490 for (i = 0; i < len; ++i)
23491 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
23492 return true;
23495 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
23496 be template parameters. */
23497 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
23498 return true;
23500 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
23501 return true;
23503 /* The standard does not specifically mention types that are local
23504 to template functions or local classes, but they should be
23505 considered dependent too. For example:
23507 template <int I> void f() {
23508 enum E { a = I };
23509 S<sizeof (E)> s;
23512 The size of `E' cannot be known until the value of `I' has been
23513 determined. Therefore, `E' must be considered dependent. */
23514 scope = TYPE_CONTEXT (type);
23515 if (scope && TYPE_P (scope))
23516 return dependent_type_p (scope);
23517 /* Don't use type_dependent_expression_p here, as it can lead
23518 to infinite recursion trying to determine whether a lambda
23519 nested in a lambda is dependent (c++/47687). */
23520 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
23521 && DECL_LANG_SPECIFIC (scope)
23522 && DECL_TEMPLATE_INFO (scope)
23523 && (any_dependent_template_arguments_p
23524 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
23525 return true;
23527 /* Other types are non-dependent. */
23528 return false;
23531 /* Returns TRUE if TYPE is dependent, in the sense of
23532 [temp.dep.type]. Note that a NULL type is considered dependent. */
23534 bool
23535 dependent_type_p (tree type)
23537 /* If there are no template parameters in scope, then there can't be
23538 any dependent types. */
23539 if (!processing_template_decl)
23541 /* If we are not processing a template, then nobody should be
23542 providing us with a dependent type. */
23543 gcc_assert (type);
23544 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
23545 return false;
23548 /* If the type is NULL, we have not computed a type for the entity
23549 in question; in that case, the type is dependent. */
23550 if (!type)
23551 return true;
23553 /* Erroneous types can be considered non-dependent. */
23554 if (type == error_mark_node)
23555 return false;
23557 /* Getting here with global_type_node means we improperly called this
23558 function on the TREE_TYPE of an IDENTIFIER_NODE. */
23559 gcc_checking_assert (type != global_type_node);
23561 /* If we have not already computed the appropriate value for TYPE,
23562 do so now. */
23563 if (!TYPE_DEPENDENT_P_VALID (type))
23565 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
23566 TYPE_DEPENDENT_P_VALID (type) = 1;
23569 return TYPE_DEPENDENT_P (type);
23572 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
23573 lookup. In other words, a dependent type that is not the current
23574 instantiation. */
23576 bool
23577 dependent_scope_p (tree scope)
23579 return (scope && TYPE_P (scope) && dependent_type_p (scope)
23580 && !currently_open_class (scope));
23583 /* T is a SCOPE_REF; return whether we need to consider it
23584 instantiation-dependent so that we can check access at instantiation
23585 time even though we know which member it resolves to. */
23587 static bool
23588 instantiation_dependent_scope_ref_p (tree t)
23590 if (DECL_P (TREE_OPERAND (t, 1))
23591 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
23592 && accessible_in_template_p (TREE_OPERAND (t, 0),
23593 TREE_OPERAND (t, 1)))
23594 return false;
23595 else
23596 return true;
23599 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
23600 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
23601 expression. */
23603 /* Note that this predicate is not appropriate for general expressions;
23604 only constant expressions (that satisfy potential_constant_expression)
23605 can be tested for value dependence. */
23607 bool
23608 value_dependent_expression_p (tree expression)
23610 if (!processing_template_decl || expression == NULL_TREE)
23611 return false;
23613 /* A name declared with a dependent type. */
23614 if (DECL_P (expression) && type_dependent_expression_p (expression))
23615 return true;
23617 switch (TREE_CODE (expression))
23619 case BASELINK:
23620 /* A dependent member function of the current instantiation. */
23621 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
23623 case FUNCTION_DECL:
23624 /* A dependent member function of the current instantiation. */
23625 if (DECL_CLASS_SCOPE_P (expression)
23626 && dependent_type_p (DECL_CONTEXT (expression)))
23627 return true;
23628 break;
23630 case IDENTIFIER_NODE:
23631 /* A name that has not been looked up -- must be dependent. */
23632 return true;
23634 case TEMPLATE_PARM_INDEX:
23635 /* A non-type template parm. */
23636 return true;
23638 case CONST_DECL:
23639 /* A non-type template parm. */
23640 if (DECL_TEMPLATE_PARM_P (expression))
23641 return true;
23642 return value_dependent_expression_p (DECL_INITIAL (expression));
23644 case VAR_DECL:
23645 /* A constant with literal type and is initialized
23646 with an expression that is value-dependent.
23648 Note that a non-dependent parenthesized initializer will have
23649 already been replaced with its constant value, so if we see
23650 a TREE_LIST it must be dependent. */
23651 if (DECL_INITIAL (expression)
23652 && decl_constant_var_p (expression)
23653 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
23654 /* cp_finish_decl doesn't fold reference initializers. */
23655 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE
23656 || type_dependent_expression_p (DECL_INITIAL (expression))
23657 || value_dependent_expression_p (DECL_INITIAL (expression))))
23658 return true;
23659 if (DECL_HAS_VALUE_EXPR_P (expression))
23661 tree value_expr = DECL_VALUE_EXPR (expression);
23662 if (type_dependent_expression_p (value_expr))
23663 return true;
23665 return false;
23667 case DYNAMIC_CAST_EXPR:
23668 case STATIC_CAST_EXPR:
23669 case CONST_CAST_EXPR:
23670 case REINTERPRET_CAST_EXPR:
23671 case CAST_EXPR:
23672 /* These expressions are value-dependent if the type to which
23673 the cast occurs is dependent or the expression being casted
23674 is value-dependent. */
23676 tree type = TREE_TYPE (expression);
23678 if (dependent_type_p (type))
23679 return true;
23681 /* A functional cast has a list of operands. */
23682 expression = TREE_OPERAND (expression, 0);
23683 if (!expression)
23685 /* If there are no operands, it must be an expression such
23686 as "int()". This should not happen for aggregate types
23687 because it would form non-constant expressions. */
23688 gcc_assert (cxx_dialect >= cxx11
23689 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
23691 return false;
23694 if (TREE_CODE (expression) == TREE_LIST)
23695 return any_value_dependent_elements_p (expression);
23697 return value_dependent_expression_p (expression);
23700 case SIZEOF_EXPR:
23701 if (SIZEOF_EXPR_TYPE_P (expression))
23702 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
23703 /* FALLTHRU */
23704 case ALIGNOF_EXPR:
23705 case TYPEID_EXPR:
23706 /* A `sizeof' expression is value-dependent if the operand is
23707 type-dependent or is a pack expansion. */
23708 expression = TREE_OPERAND (expression, 0);
23709 if (PACK_EXPANSION_P (expression))
23710 return true;
23711 else if (TYPE_P (expression))
23712 return dependent_type_p (expression);
23713 return instantiation_dependent_uneval_expression_p (expression);
23715 case AT_ENCODE_EXPR:
23716 /* An 'encode' expression is value-dependent if the operand is
23717 type-dependent. */
23718 expression = TREE_OPERAND (expression, 0);
23719 return dependent_type_p (expression);
23721 case NOEXCEPT_EXPR:
23722 expression = TREE_OPERAND (expression, 0);
23723 return instantiation_dependent_uneval_expression_p (expression);
23725 case SCOPE_REF:
23726 /* All instantiation-dependent expressions should also be considered
23727 value-dependent. */
23728 return instantiation_dependent_scope_ref_p (expression);
23730 case COMPONENT_REF:
23731 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
23732 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
23734 case NONTYPE_ARGUMENT_PACK:
23735 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
23736 is value-dependent. */
23738 tree values = ARGUMENT_PACK_ARGS (expression);
23739 int i, len = TREE_VEC_LENGTH (values);
23741 for (i = 0; i < len; ++i)
23742 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
23743 return true;
23745 return false;
23748 case TRAIT_EXPR:
23750 tree type2 = TRAIT_EXPR_TYPE2 (expression);
23751 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
23752 || (type2 ? dependent_type_p (type2) : false));
23755 case MODOP_EXPR:
23756 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
23757 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
23759 case ARRAY_REF:
23760 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
23761 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
23763 case ADDR_EXPR:
23765 tree op = TREE_OPERAND (expression, 0);
23766 return (value_dependent_expression_p (op)
23767 || has_value_dependent_address (op));
23770 case REQUIRES_EXPR:
23771 /* Treat all requires-expressions as value-dependent so
23772 we don't try to fold them. */
23773 return true;
23775 case TYPE_REQ:
23776 return dependent_type_p (TREE_OPERAND (expression, 0));
23778 case CALL_EXPR:
23780 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
23781 return true;
23782 tree fn = get_callee_fndecl (expression);
23783 int i, nargs;
23784 nargs = call_expr_nargs (expression);
23785 for (i = 0; i < nargs; ++i)
23787 tree op = CALL_EXPR_ARG (expression, i);
23788 /* In a call to a constexpr member function, look through the
23789 implicit ADDR_EXPR on the object argument so that it doesn't
23790 cause the call to be considered value-dependent. We also
23791 look through it in potential_constant_expression. */
23792 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
23793 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
23794 && TREE_CODE (op) == ADDR_EXPR)
23795 op = TREE_OPERAND (op, 0);
23796 if (value_dependent_expression_p (op))
23797 return true;
23799 return false;
23802 case TEMPLATE_ID_EXPR:
23803 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
23804 type-dependent. */
23805 return type_dependent_expression_p (expression)
23806 || variable_concept_p (TREE_OPERAND (expression, 0));
23808 case CONSTRUCTOR:
23810 unsigned ix;
23811 tree val;
23812 if (dependent_type_p (TREE_TYPE (expression)))
23813 return true;
23814 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
23815 if (value_dependent_expression_p (val))
23816 return true;
23817 return false;
23820 case STMT_EXPR:
23821 /* Treat a GNU statement expression as dependent to avoid crashing
23822 under instantiate_non_dependent_expr; it can't be constant. */
23823 return true;
23825 default:
23826 /* A constant expression is value-dependent if any subexpression is
23827 value-dependent. */
23828 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
23830 case tcc_reference:
23831 case tcc_unary:
23832 case tcc_comparison:
23833 case tcc_binary:
23834 case tcc_expression:
23835 case tcc_vl_exp:
23837 int i, len = cp_tree_operand_length (expression);
23839 for (i = 0; i < len; i++)
23841 tree t = TREE_OPERAND (expression, i);
23843 /* In some cases, some of the operands may be missing.l
23844 (For example, in the case of PREDECREMENT_EXPR, the
23845 amount to increment by may be missing.) That doesn't
23846 make the expression dependent. */
23847 if (t && value_dependent_expression_p (t))
23848 return true;
23851 break;
23852 default:
23853 break;
23855 break;
23858 /* The expression is not value-dependent. */
23859 return false;
23862 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
23863 [temp.dep.expr]. Note that an expression with no type is
23864 considered dependent. Other parts of the compiler arrange for an
23865 expression with type-dependent subexpressions to have no type, so
23866 this function doesn't have to be fully recursive. */
23868 bool
23869 type_dependent_expression_p (tree expression)
23871 if (!processing_template_decl)
23872 return false;
23874 if (expression == NULL_TREE || expression == error_mark_node)
23875 return false;
23877 /* An unresolved name is always dependent. */
23878 if (identifier_p (expression)
23879 || TREE_CODE (expression) == USING_DECL
23880 || TREE_CODE (expression) == WILDCARD_DECL)
23881 return true;
23883 /* A fold expression is type-dependent. */
23884 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
23885 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
23886 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
23887 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
23888 return true;
23890 /* Some expression forms are never type-dependent. */
23891 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
23892 || TREE_CODE (expression) == SIZEOF_EXPR
23893 || TREE_CODE (expression) == ALIGNOF_EXPR
23894 || TREE_CODE (expression) == AT_ENCODE_EXPR
23895 || TREE_CODE (expression) == NOEXCEPT_EXPR
23896 || TREE_CODE (expression) == TRAIT_EXPR
23897 || TREE_CODE (expression) == TYPEID_EXPR
23898 || TREE_CODE (expression) == DELETE_EXPR
23899 || TREE_CODE (expression) == VEC_DELETE_EXPR
23900 || TREE_CODE (expression) == THROW_EXPR
23901 || TREE_CODE (expression) == REQUIRES_EXPR)
23902 return false;
23904 /* The types of these expressions depends only on the type to which
23905 the cast occurs. */
23906 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
23907 || TREE_CODE (expression) == STATIC_CAST_EXPR
23908 || TREE_CODE (expression) == CONST_CAST_EXPR
23909 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
23910 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
23911 || TREE_CODE (expression) == CAST_EXPR)
23912 return dependent_type_p (TREE_TYPE (expression));
23914 /* The types of these expressions depends only on the type created
23915 by the expression. */
23916 if (TREE_CODE (expression) == NEW_EXPR
23917 || TREE_CODE (expression) == VEC_NEW_EXPR)
23919 /* For NEW_EXPR tree nodes created inside a template, either
23920 the object type itself or a TREE_LIST may appear as the
23921 operand 1. */
23922 tree type = TREE_OPERAND (expression, 1);
23923 if (TREE_CODE (type) == TREE_LIST)
23924 /* This is an array type. We need to check array dimensions
23925 as well. */
23926 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
23927 || value_dependent_expression_p
23928 (TREE_OPERAND (TREE_VALUE (type), 1));
23929 else
23930 return dependent_type_p (type);
23933 if (TREE_CODE (expression) == SCOPE_REF)
23935 tree scope = TREE_OPERAND (expression, 0);
23936 tree name = TREE_OPERAND (expression, 1);
23938 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
23939 contains an identifier associated by name lookup with one or more
23940 declarations declared with a dependent type, or...a
23941 nested-name-specifier or qualified-id that names a member of an
23942 unknown specialization. */
23943 return (type_dependent_expression_p (name)
23944 || dependent_scope_p (scope));
23947 if (TREE_CODE (expression) == TEMPLATE_DECL
23948 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
23949 return uses_outer_template_parms (expression);
23951 if (TREE_CODE (expression) == STMT_EXPR)
23952 expression = stmt_expr_value_expr (expression);
23954 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
23956 tree elt;
23957 unsigned i;
23959 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
23961 if (type_dependent_expression_p (elt))
23962 return true;
23964 return false;
23967 /* A static data member of the current instantiation with incomplete
23968 array type is type-dependent, as the definition and specializations
23969 can have different bounds. */
23970 if (VAR_P (expression)
23971 && DECL_CLASS_SCOPE_P (expression)
23972 && dependent_type_p (DECL_CONTEXT (expression))
23973 && VAR_HAD_UNKNOWN_BOUND (expression))
23974 return true;
23976 /* An array of unknown bound depending on a variadic parameter, eg:
23978 template<typename... Args>
23979 void foo (Args... args)
23981 int arr[] = { args... };
23984 template<int... vals>
23985 void bar ()
23987 int arr[] = { vals... };
23990 If the array has no length and has an initializer, it must be that
23991 we couldn't determine its length in cp_complete_array_type because
23992 it is dependent. */
23993 if (VAR_P (expression)
23994 && TREE_TYPE (expression) != NULL_TREE
23995 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
23996 && !TYPE_DOMAIN (TREE_TYPE (expression))
23997 && DECL_INITIAL (expression))
23998 return true;
24000 /* A function or variable template-id is type-dependent if it has any
24001 dependent template arguments. Note that we only consider the innermost
24002 template arguments here, since those are the ones that come from the
24003 template-id; the template arguments for the enclosing class do not make it
24004 type-dependent, they only make a member function value-dependent. */
24005 if (VAR_OR_FUNCTION_DECL_P (expression)
24006 && DECL_LANG_SPECIFIC (expression)
24007 && DECL_TEMPLATE_INFO (expression)
24008 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression))
24009 && (any_dependent_template_arguments_p
24010 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
24011 return true;
24013 /* Always dependent, on the number of arguments if nothing else. */
24014 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
24015 return true;
24017 if (TREE_TYPE (expression) == unknown_type_node)
24019 if (TREE_CODE (expression) == ADDR_EXPR)
24020 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
24021 if (TREE_CODE (expression) == COMPONENT_REF
24022 || TREE_CODE (expression) == OFFSET_REF)
24024 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
24025 return true;
24026 expression = TREE_OPERAND (expression, 1);
24027 if (identifier_p (expression))
24028 return false;
24030 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
24031 if (TREE_CODE (expression) == SCOPE_REF)
24032 return false;
24034 if (BASELINK_P (expression))
24036 if (BASELINK_OPTYPE (expression)
24037 && dependent_type_p (BASELINK_OPTYPE (expression)))
24038 return true;
24039 expression = BASELINK_FUNCTIONS (expression);
24042 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
24044 if (any_dependent_template_arguments_p
24045 (TREE_OPERAND (expression, 1)))
24046 return true;
24047 expression = TREE_OPERAND (expression, 0);
24048 if (identifier_p (expression))
24049 return true;
24052 gcc_assert (TREE_CODE (expression) == OVERLOAD
24053 || TREE_CODE (expression) == FUNCTION_DECL);
24055 for (lkp_iterator iter (expression); iter; ++iter)
24056 if (type_dependent_expression_p (*iter))
24057 return true;
24059 return false;
24062 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
24064 /* Dependent type attributes might not have made it from the decl to
24065 the type yet. */
24066 if (DECL_P (expression)
24067 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
24068 return true;
24070 return (dependent_type_p (TREE_TYPE (expression)));
24073 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
24074 type-dependent if the expression refers to a member of the current
24075 instantiation and the type of the referenced member is dependent, or the
24076 class member access expression refers to a member of an unknown
24077 specialization.
24079 This function returns true if the OBJECT in such a class member access
24080 expression is of an unknown specialization. */
24082 bool
24083 type_dependent_object_expression_p (tree object)
24085 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
24086 dependent. */
24087 if (TREE_CODE (object) == IDENTIFIER_NODE)
24088 return true;
24089 tree scope = TREE_TYPE (object);
24090 return (!scope || dependent_scope_p (scope));
24093 /* walk_tree callback function for instantiation_dependent_expression_p,
24094 below. Returns non-zero if a dependent subexpression is found. */
24096 static tree
24097 instantiation_dependent_r (tree *tp, int *walk_subtrees,
24098 void * /*data*/)
24100 if (TYPE_P (*tp))
24102 /* We don't have to worry about decltype currently because decltype
24103 of an instantiation-dependent expr is a dependent type. This
24104 might change depending on the resolution of DR 1172. */
24105 *walk_subtrees = false;
24106 return NULL_TREE;
24108 enum tree_code code = TREE_CODE (*tp);
24109 switch (code)
24111 /* Don't treat an argument list as dependent just because it has no
24112 TREE_TYPE. */
24113 case TREE_LIST:
24114 case TREE_VEC:
24115 return NULL_TREE;
24117 case TEMPLATE_PARM_INDEX:
24118 return *tp;
24120 /* Handle expressions with type operands. */
24121 case SIZEOF_EXPR:
24122 case ALIGNOF_EXPR:
24123 case TYPEID_EXPR:
24124 case AT_ENCODE_EXPR:
24126 tree op = TREE_OPERAND (*tp, 0);
24127 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
24128 op = TREE_TYPE (op);
24129 if (TYPE_P (op))
24131 if (dependent_type_p (op))
24132 return *tp;
24133 else
24135 *walk_subtrees = false;
24136 return NULL_TREE;
24139 break;
24142 case COMPONENT_REF:
24143 if (identifier_p (TREE_OPERAND (*tp, 1)))
24144 /* In a template, finish_class_member_access_expr creates a
24145 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
24146 type-dependent, so that we can check access control at
24147 instantiation time (PR 42277). See also Core issue 1273. */
24148 return *tp;
24149 break;
24151 case SCOPE_REF:
24152 if (instantiation_dependent_scope_ref_p (*tp))
24153 return *tp;
24154 else
24155 break;
24157 /* Treat statement-expressions as dependent. */
24158 case BIND_EXPR:
24159 return *tp;
24161 /* Treat requires-expressions as dependent. */
24162 case REQUIRES_EXPR:
24163 return *tp;
24165 case CALL_EXPR:
24166 /* Treat calls to function concepts as dependent. */
24167 if (function_concept_check_p (*tp))
24168 return *tp;
24169 break;
24171 case TEMPLATE_ID_EXPR:
24172 /* And variable concepts. */
24173 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
24174 return *tp;
24175 break;
24177 default:
24178 break;
24181 if (type_dependent_expression_p (*tp))
24182 return *tp;
24183 else
24184 return NULL_TREE;
24187 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
24188 sense defined by the ABI:
24190 "An expression is instantiation-dependent if it is type-dependent
24191 or value-dependent, or it has a subexpression that is type-dependent
24192 or value-dependent."
24194 Except don't actually check value-dependence for unevaluated expressions,
24195 because in sizeof(i) we don't care about the value of i. Checking
24196 type-dependence will in turn check value-dependence of array bounds/template
24197 arguments as needed. */
24199 bool
24200 instantiation_dependent_uneval_expression_p (tree expression)
24202 tree result;
24204 if (!processing_template_decl)
24205 return false;
24207 if (expression == error_mark_node)
24208 return false;
24210 result = cp_walk_tree_without_duplicates (&expression,
24211 instantiation_dependent_r, NULL);
24212 return result != NULL_TREE;
24215 /* As above, but also check value-dependence of the expression as a whole. */
24217 bool
24218 instantiation_dependent_expression_p (tree expression)
24220 return (instantiation_dependent_uneval_expression_p (expression)
24221 || value_dependent_expression_p (expression));
24224 /* Like type_dependent_expression_p, but it also works while not processing
24225 a template definition, i.e. during substitution or mangling. */
24227 bool
24228 type_dependent_expression_p_push (tree expr)
24230 bool b;
24231 ++processing_template_decl;
24232 b = type_dependent_expression_p (expr);
24233 --processing_template_decl;
24234 return b;
24237 /* Returns TRUE if ARGS contains a type-dependent expression. */
24239 bool
24240 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
24242 unsigned int i;
24243 tree arg;
24245 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
24247 if (type_dependent_expression_p (arg))
24248 return true;
24250 return false;
24253 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24254 expressions) contains any type-dependent expressions. */
24256 bool
24257 any_type_dependent_elements_p (const_tree list)
24259 for (; list; list = TREE_CHAIN (list))
24260 if (type_dependent_expression_p (TREE_VALUE (list)))
24261 return true;
24263 return false;
24266 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24267 expressions) contains any value-dependent expressions. */
24269 bool
24270 any_value_dependent_elements_p (const_tree list)
24272 for (; list; list = TREE_CHAIN (list))
24273 if (value_dependent_expression_p (TREE_VALUE (list)))
24274 return true;
24276 return false;
24279 /* Returns TRUE if the ARG (a template argument) is dependent. */
24281 bool
24282 dependent_template_arg_p (tree arg)
24284 if (!processing_template_decl)
24285 return false;
24287 /* Assume a template argument that was wrongly written by the user
24288 is dependent. This is consistent with what
24289 any_dependent_template_arguments_p [that calls this function]
24290 does. */
24291 if (!arg || arg == error_mark_node)
24292 return true;
24294 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
24295 arg = ARGUMENT_PACK_SELECT_ARG (arg);
24297 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
24298 return true;
24299 if (TREE_CODE (arg) == TEMPLATE_DECL)
24301 if (DECL_TEMPLATE_PARM_P (arg))
24302 return true;
24303 /* A member template of a dependent class is not necessarily
24304 type-dependent, but it is a dependent template argument because it
24305 will be a member of an unknown specialization to that template. */
24306 tree scope = CP_DECL_CONTEXT (arg);
24307 return TYPE_P (scope) && dependent_type_p (scope);
24309 else if (ARGUMENT_PACK_P (arg))
24311 tree args = ARGUMENT_PACK_ARGS (arg);
24312 int i, len = TREE_VEC_LENGTH (args);
24313 for (i = 0; i < len; ++i)
24315 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
24316 return true;
24319 return false;
24321 else if (TYPE_P (arg))
24322 return dependent_type_p (arg);
24323 else
24324 return (type_dependent_expression_p (arg)
24325 || value_dependent_expression_p (arg));
24328 /* Returns true if ARGS (a collection of template arguments) contains
24329 any types that require structural equality testing. */
24331 bool
24332 any_template_arguments_need_structural_equality_p (tree args)
24334 int i;
24335 int j;
24337 if (!args)
24338 return false;
24339 if (args == error_mark_node)
24340 return true;
24342 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24344 tree level = TMPL_ARGS_LEVEL (args, i + 1);
24345 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24347 tree arg = TREE_VEC_ELT (level, j);
24348 tree packed_args = NULL_TREE;
24349 int k, len = 1;
24351 if (ARGUMENT_PACK_P (arg))
24353 /* Look inside the argument pack. */
24354 packed_args = ARGUMENT_PACK_ARGS (arg);
24355 len = TREE_VEC_LENGTH (packed_args);
24358 for (k = 0; k < len; ++k)
24360 if (packed_args)
24361 arg = TREE_VEC_ELT (packed_args, k);
24363 if (error_operand_p (arg))
24364 return true;
24365 else if (TREE_CODE (arg) == TEMPLATE_DECL)
24366 continue;
24367 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
24368 return true;
24369 else if (!TYPE_P (arg) && TREE_TYPE (arg)
24370 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
24371 return true;
24376 return false;
24379 /* Returns true if ARGS (a collection of template arguments) contains
24380 any dependent arguments. */
24382 bool
24383 any_dependent_template_arguments_p (const_tree args)
24385 int i;
24386 int j;
24388 if (!args)
24389 return false;
24390 if (args == error_mark_node)
24391 return true;
24393 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24395 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
24396 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24397 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
24398 return true;
24401 return false;
24404 /* Returns TRUE if the template TMPL is type-dependent. */
24406 bool
24407 dependent_template_p (tree tmpl)
24409 if (TREE_CODE (tmpl) == OVERLOAD)
24411 for (lkp_iterator iter (tmpl); iter; ++iter)
24412 if (dependent_template_p (*iter))
24413 return true;
24414 return false;
24417 /* Template template parameters are dependent. */
24418 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
24419 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
24420 return true;
24421 /* So are names that have not been looked up. */
24422 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
24423 return true;
24424 return false;
24427 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
24429 bool
24430 dependent_template_id_p (tree tmpl, tree args)
24432 return (dependent_template_p (tmpl)
24433 || any_dependent_template_arguments_p (args));
24436 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
24437 are dependent. */
24439 bool
24440 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
24442 int i;
24444 if (!processing_template_decl)
24445 return false;
24447 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
24449 tree decl = TREE_VEC_ELT (declv, i);
24450 tree init = TREE_VEC_ELT (initv, i);
24451 tree cond = TREE_VEC_ELT (condv, i);
24452 tree incr = TREE_VEC_ELT (incrv, i);
24454 if (type_dependent_expression_p (decl)
24455 || TREE_CODE (decl) == SCOPE_REF)
24456 return true;
24458 if (init && type_dependent_expression_p (init))
24459 return true;
24461 if (type_dependent_expression_p (cond))
24462 return true;
24464 if (COMPARISON_CLASS_P (cond)
24465 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
24466 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
24467 return true;
24469 if (TREE_CODE (incr) == MODOP_EXPR)
24471 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
24472 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
24473 return true;
24475 else if (type_dependent_expression_p (incr))
24476 return true;
24477 else if (TREE_CODE (incr) == MODIFY_EXPR)
24479 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
24480 return true;
24481 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
24483 tree t = TREE_OPERAND (incr, 1);
24484 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
24485 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
24486 return true;
24491 return false;
24494 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
24495 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
24496 no such TYPE can be found. Note that this function peers inside
24497 uninstantiated templates and therefore should be used only in
24498 extremely limited situations. ONLY_CURRENT_P restricts this
24499 peering to the currently open classes hierarchy (which is required
24500 when comparing types). */
24502 tree
24503 resolve_typename_type (tree type, bool only_current_p)
24505 tree scope;
24506 tree name;
24507 tree decl;
24508 int quals;
24509 tree pushed_scope;
24510 tree result;
24512 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
24514 scope = TYPE_CONTEXT (type);
24515 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
24516 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
24517 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
24518 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
24519 identifier of the TYPENAME_TYPE anymore.
24520 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
24521 TYPENAME_TYPE instead, we avoid messing up with a possible
24522 typedef variant case. */
24523 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
24525 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
24526 it first before we can figure out what NAME refers to. */
24527 if (TREE_CODE (scope) == TYPENAME_TYPE)
24529 if (TYPENAME_IS_RESOLVING_P (scope))
24530 /* Given a class template A with a dependent base with nested type C,
24531 typedef typename A::C::C C will land us here, as trying to resolve
24532 the initial A::C leads to the local C typedef, which leads back to
24533 A::C::C. So we break the recursion now. */
24534 return type;
24535 else
24536 scope = resolve_typename_type (scope, only_current_p);
24538 /* If we don't know what SCOPE refers to, then we cannot resolve the
24539 TYPENAME_TYPE. */
24540 if (!CLASS_TYPE_P (scope))
24541 return type;
24542 /* If this is a typedef, we don't want to look inside (c++/11987). */
24543 if (typedef_variant_p (type))
24544 return type;
24545 /* If SCOPE isn't the template itself, it will not have a valid
24546 TYPE_FIELDS list. */
24547 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
24548 /* scope is either the template itself or a compatible instantiation
24549 like X<T>, so look up the name in the original template. */
24550 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
24551 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
24552 gcc_checking_assert (uses_template_parms (scope));
24553 /* If scope has no fields, it can't be a current instantiation. Check this
24554 before currently_open_class to avoid infinite recursion (71515). */
24555 if (!TYPE_FIELDS (scope))
24556 return type;
24557 /* If the SCOPE is not the current instantiation, there's no reason
24558 to look inside it. */
24559 if (only_current_p && !currently_open_class (scope))
24560 return type;
24561 /* Enter the SCOPE so that name lookup will be resolved as if we
24562 were in the class definition. In particular, SCOPE will no
24563 longer be considered a dependent type. */
24564 pushed_scope = push_scope (scope);
24565 /* Look up the declaration. */
24566 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
24567 tf_warning_or_error);
24569 result = NULL_TREE;
24571 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
24572 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
24573 if (!decl)
24574 /*nop*/;
24575 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type))
24576 && TREE_CODE (decl) == TYPE_DECL)
24578 result = TREE_TYPE (decl);
24579 if (result == error_mark_node)
24580 result = NULL_TREE;
24582 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
24583 && DECL_CLASS_TEMPLATE_P (decl))
24585 tree tmpl;
24586 tree args;
24587 /* Obtain the template and the arguments. */
24588 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
24589 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
24590 /* Instantiate the template. */
24591 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
24592 /*entering_scope=*/0,
24593 tf_error | tf_user);
24594 if (result == error_mark_node)
24595 result = NULL_TREE;
24598 /* Leave the SCOPE. */
24599 if (pushed_scope)
24600 pop_scope (pushed_scope);
24602 /* If we failed to resolve it, return the original typename. */
24603 if (!result)
24604 return type;
24606 /* If lookup found a typename type, resolve that too. */
24607 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
24609 /* Ill-formed programs can cause infinite recursion here, so we
24610 must catch that. */
24611 TYPENAME_IS_RESOLVING_P (result) = 1;
24612 result = resolve_typename_type (result, only_current_p);
24613 TYPENAME_IS_RESOLVING_P (result) = 0;
24616 /* Qualify the resulting type. */
24617 quals = cp_type_quals (type);
24618 if (quals)
24619 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
24621 return result;
24624 /* EXPR is an expression which is not type-dependent. Return a proxy
24625 for EXPR that can be used to compute the types of larger
24626 expressions containing EXPR. */
24628 tree
24629 build_non_dependent_expr (tree expr)
24631 tree inner_expr;
24633 /* When checking, try to get a constant value for all non-dependent
24634 expressions in order to expose bugs in *_dependent_expression_p
24635 and constexpr. This can affect code generation, see PR70704, so
24636 only do this for -fchecking=2. */
24637 if (flag_checking > 1
24638 && cxx_dialect >= cxx11
24639 /* Don't do this during nsdmi parsing as it can lead to
24640 unexpected recursive instantiations. */
24641 && !parsing_nsdmi ()
24642 /* Don't do this during concept expansion either and for
24643 the same reason. */
24644 && !expanding_concept ())
24645 fold_non_dependent_expr (expr);
24647 /* Preserve OVERLOADs; the functions must be available to resolve
24648 types. */
24649 inner_expr = expr;
24650 if (TREE_CODE (inner_expr) == STMT_EXPR)
24651 inner_expr = stmt_expr_value_expr (inner_expr);
24652 if (TREE_CODE (inner_expr) == ADDR_EXPR)
24653 inner_expr = TREE_OPERAND (inner_expr, 0);
24654 if (TREE_CODE (inner_expr) == COMPONENT_REF)
24655 inner_expr = TREE_OPERAND (inner_expr, 1);
24656 if (is_overloaded_fn (inner_expr)
24657 || TREE_CODE (inner_expr) == OFFSET_REF)
24658 return expr;
24659 /* There is no need to return a proxy for a variable. */
24660 if (VAR_P (expr))
24661 return expr;
24662 /* Preserve string constants; conversions from string constants to
24663 "char *" are allowed, even though normally a "const char *"
24664 cannot be used to initialize a "char *". */
24665 if (TREE_CODE (expr) == STRING_CST)
24666 return expr;
24667 /* Preserve void and arithmetic constants, as an optimization -- there is no
24668 reason to create a new node. */
24669 if (TREE_CODE (expr) == VOID_CST
24670 || TREE_CODE (expr) == INTEGER_CST
24671 || TREE_CODE (expr) == REAL_CST)
24672 return expr;
24673 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
24674 There is at least one place where we want to know that a
24675 particular expression is a throw-expression: when checking a ?:
24676 expression, there are special rules if the second or third
24677 argument is a throw-expression. */
24678 if (TREE_CODE (expr) == THROW_EXPR)
24679 return expr;
24681 /* Don't wrap an initializer list, we need to be able to look inside. */
24682 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
24683 return expr;
24685 /* Don't wrap a dummy object, we need to be able to test for it. */
24686 if (is_dummy_object (expr))
24687 return expr;
24689 if (TREE_CODE (expr) == COND_EXPR)
24690 return build3 (COND_EXPR,
24691 TREE_TYPE (expr),
24692 TREE_OPERAND (expr, 0),
24693 (TREE_OPERAND (expr, 1)
24694 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
24695 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
24696 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
24697 if (TREE_CODE (expr) == COMPOUND_EXPR
24698 && !COMPOUND_EXPR_OVERLOADED (expr))
24699 return build2 (COMPOUND_EXPR,
24700 TREE_TYPE (expr),
24701 TREE_OPERAND (expr, 0),
24702 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
24704 /* If the type is unknown, it can't really be non-dependent */
24705 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
24707 /* Otherwise, build a NON_DEPENDENT_EXPR. */
24708 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
24711 /* ARGS is a vector of expressions as arguments to a function call.
24712 Replace the arguments with equivalent non-dependent expressions.
24713 This modifies ARGS in place. */
24715 void
24716 make_args_non_dependent (vec<tree, va_gc> *args)
24718 unsigned int ix;
24719 tree arg;
24721 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
24723 tree newarg = build_non_dependent_expr (arg);
24724 if (newarg != arg)
24725 (*args)[ix] = newarg;
24729 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
24730 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
24731 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
24733 static tree
24734 make_auto_1 (tree name, bool set_canonical)
24736 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
24737 TYPE_NAME (au) = build_decl (input_location,
24738 TYPE_DECL, name, au);
24739 TYPE_STUB_DECL (au) = TYPE_NAME (au);
24740 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
24741 (0, processing_template_decl + 1, processing_template_decl + 1,
24742 TYPE_NAME (au), NULL_TREE);
24743 if (set_canonical)
24744 TYPE_CANONICAL (au) = canonical_type_parameter (au);
24745 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
24746 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
24748 return au;
24751 tree
24752 make_decltype_auto (void)
24754 return make_auto_1 (decltype_auto_identifier, true);
24757 tree
24758 make_auto (void)
24760 return make_auto_1 (auto_identifier, true);
24763 /* Return a C++17 deduction placeholder for class template TMPL. */
24765 tree
24766 make_template_placeholder (tree tmpl)
24768 tree t = make_auto_1 (DECL_NAME (tmpl), true);
24769 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
24770 return t;
24773 /* Make a "constrained auto" type-specifier. This is an
24774 auto type with constraints that must be associated after
24775 deduction. The constraint is formed from the given
24776 CONC and its optional sequence of arguments, which are
24777 non-null if written as partial-concept-id. */
24779 tree
24780 make_constrained_auto (tree con, tree args)
24782 tree type = make_auto_1 (auto_identifier, false);
24784 /* Build the constraint. */
24785 tree tmpl = DECL_TI_TEMPLATE (con);
24786 tree expr = VAR_P (con) ? tmpl : ovl_make (tmpl);
24787 expr = build_concept_check (expr, type, args);
24789 tree constr = normalize_expression (expr);
24790 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
24792 /* Our canonical type depends on the constraint. */
24793 TYPE_CANONICAL (type) = canonical_type_parameter (type);
24795 /* Attach the constraint to the type declaration. */
24796 tree decl = TYPE_NAME (type);
24797 return decl;
24800 /* Given type ARG, return std::initializer_list<ARG>. */
24802 static tree
24803 listify (tree arg)
24805 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
24807 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
24809 error ("deducing from brace-enclosed initializer list requires "
24810 "#include <initializer_list>");
24811 return error_mark_node;
24813 tree argvec = make_tree_vec (1);
24814 TREE_VEC_ELT (argvec, 0) = arg;
24816 return lookup_template_class (std_init_list, argvec, NULL_TREE,
24817 NULL_TREE, 0, tf_warning_or_error);
24820 /* Replace auto in TYPE with std::initializer_list<auto>. */
24822 static tree
24823 listify_autos (tree type, tree auto_node)
24825 tree init_auto = listify (auto_node);
24826 tree argvec = make_tree_vec (1);
24827 TREE_VEC_ELT (argvec, 0) = init_auto;
24828 if (processing_template_decl)
24829 argvec = add_to_template_args (current_template_args (), argvec);
24830 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
24833 /* Hash traits for hashing possibly constrained 'auto'
24834 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
24836 struct auto_hash : default_hash_traits<tree>
24838 static inline hashval_t hash (tree);
24839 static inline bool equal (tree, tree);
24842 /* Hash the 'auto' T. */
24844 inline hashval_t
24845 auto_hash::hash (tree t)
24847 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
24848 /* Matching constrained-type-specifiers denote the same template
24849 parameter, so hash the constraint. */
24850 return hash_placeholder_constraint (c);
24851 else
24852 /* But unconstrained autos are all separate, so just hash the pointer. */
24853 return iterative_hash_object (t, 0);
24856 /* Compare two 'auto's. */
24858 inline bool
24859 auto_hash::equal (tree t1, tree t2)
24861 if (t1 == t2)
24862 return true;
24864 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
24865 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
24867 /* Two unconstrained autos are distinct. */
24868 if (!c1 || !c2)
24869 return false;
24871 return equivalent_placeholder_constraints (c1, c2);
24874 /* for_each_template_parm callback for extract_autos: if t is a (possibly
24875 constrained) auto, add it to the vector. */
24877 static int
24878 extract_autos_r (tree t, void *data)
24880 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
24881 if (is_auto (t))
24883 /* All the autos were built with index 0; fix that up now. */
24884 tree *p = hash.find_slot (t, INSERT);
24885 unsigned idx;
24886 if (*p)
24887 /* If this is a repeated constrained-type-specifier, use the index we
24888 chose before. */
24889 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
24890 else
24892 /* Otherwise this is new, so use the current count. */
24893 *p = t;
24894 idx = hash.elements () - 1;
24896 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
24899 /* Always keep walking. */
24900 return 0;
24903 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
24904 says they can appear anywhere in the type. */
24906 static tree
24907 extract_autos (tree type)
24909 hash_set<tree> visited;
24910 hash_table<auto_hash> hash (2);
24912 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
24914 tree tree_vec = make_tree_vec (hash.elements());
24915 for (hash_table<auto_hash>::iterator iter = hash.begin();
24916 iter != hash.end(); ++iter)
24918 tree elt = *iter;
24919 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
24920 TREE_VEC_ELT (tree_vec, i)
24921 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
24924 return tree_vec;
24927 /* The stem for deduction guide names. */
24928 const char *const dguide_base = "__dguide_";
24930 /* Return the name for a deduction guide for class template TMPL. */
24932 tree
24933 dguide_name (tree tmpl)
24935 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
24936 tree tname = TYPE_IDENTIFIER (type);
24937 char *buf = (char *) alloca (1 + strlen (dguide_base)
24938 + IDENTIFIER_LENGTH (tname));
24939 memcpy (buf, dguide_base, strlen (dguide_base));
24940 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
24941 IDENTIFIER_LENGTH (tname) + 1);
24942 tree dname = get_identifier (buf);
24943 TREE_TYPE (dname) = type;
24944 return dname;
24947 /* True if NAME is the name of a deduction guide. */
24949 bool
24950 dguide_name_p (tree name)
24952 return (TREE_TYPE (name)
24953 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
24954 strlen (dguide_base)));
24957 /* True if FN is a deduction guide. */
24959 bool
24960 deduction_guide_p (const_tree fn)
24962 if (DECL_P (fn))
24963 if (tree name = DECL_NAME (fn))
24964 return dguide_name_p (name);
24965 return false;
24968 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
24970 bool
24971 copy_guide_p (const_tree fn)
24973 gcc_assert (deduction_guide_p (fn));
24974 if (!DECL_ARTIFICIAL (fn))
24975 return false;
24976 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
24977 return (TREE_CHAIN (parms) == void_list_node
24978 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
24981 /* True if FN is a guide generated from a constructor template. */
24983 bool
24984 template_guide_p (const_tree fn)
24986 gcc_assert (deduction_guide_p (fn));
24987 if (!DECL_ARTIFICIAL (fn))
24988 return false;
24989 if (tree ctor = DECL_ABSTRACT_ORIGIN (fn))
24991 tree tmpl = DECL_TI_TEMPLATE (ctor);
24992 return PRIMARY_TEMPLATE_P (tmpl);
24994 return false;
24997 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
24998 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
24999 template parameter types. Note that the handling of template template
25000 parameters relies on current_template_parms being set appropriately for the
25001 new template. */
25003 static tree
25004 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
25005 tree tsubst_args, tsubst_flags_t complain)
25007 tree oldidx = get_template_parm_index (olddecl);
25009 tree newtype;
25010 if (TREE_CODE (olddecl) == TYPE_DECL
25011 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25013 tree oldtype = TREE_TYPE (olddecl);
25014 newtype = cxx_make_type (TREE_CODE (oldtype));
25015 TYPE_MAIN_VARIANT (newtype) = newtype;
25016 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
25017 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
25018 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
25020 else
25021 newtype = tsubst (TREE_TYPE (olddecl), tsubst_args,
25022 complain, NULL_TREE);
25024 tree newdecl
25025 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
25026 DECL_NAME (olddecl), newtype);
25027 SET_DECL_TEMPLATE_PARM_P (newdecl);
25029 tree newidx;
25030 if (TREE_CODE (olddecl) == TYPE_DECL
25031 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25033 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
25034 = build_template_parm_index (index, level, level,
25035 newdecl, newtype);
25036 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25037 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25038 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
25039 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
25041 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
25043 DECL_TEMPLATE_RESULT (newdecl)
25044 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
25045 DECL_NAME (olddecl), newtype);
25046 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
25047 // First create a copy (ttargs) of tsubst_args with an
25048 // additional level for the template template parameter's own
25049 // template parameters (ttparms).
25050 tree ttparms = (INNERMOST_TEMPLATE_PARMS
25051 (DECL_TEMPLATE_PARMS (olddecl)));
25052 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
25053 tree ttargs = make_tree_vec (depth + 1);
25054 for (int i = 0; i < depth; ++i)
25055 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
25056 TREE_VEC_ELT (ttargs, depth)
25057 = template_parms_level_to_args (ttparms);
25058 // Substitute ttargs into ttparms to fix references to
25059 // other template parameters.
25060 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25061 complain);
25062 // Now substitute again with args based on tparms, to reduce
25063 // the level of the ttparms.
25064 ttargs = current_template_args ();
25065 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25066 complain);
25067 // Finally, tack the adjusted parms onto tparms.
25068 ttparms = tree_cons (size_int (depth), ttparms,
25069 current_template_parms);
25070 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
25073 else
25075 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
25076 tree newconst
25077 = build_decl (DECL_SOURCE_LOCATION (oldconst),
25078 TREE_CODE (oldconst),
25079 DECL_NAME (oldconst), newtype);
25080 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
25081 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
25082 SET_DECL_TEMPLATE_PARM_P (newconst);
25083 newidx = build_template_parm_index (index, level, level,
25084 newconst, newtype);
25085 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25086 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25087 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
25090 return newdecl;
25093 /* Returns a C++17 class deduction guide template based on the constructor
25094 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
25095 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
25097 static tree
25098 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
25100 tree type, tparms, targs, fparms, fargs, ci;
25101 bool memtmpl = false;
25102 bool explicit_p;
25103 location_t loc;
25105 if (TYPE_P (ctor))
25107 type = ctor;
25108 bool copy_p = TREE_CODE (type) == REFERENCE_TYPE;
25109 if (copy_p)
25111 type = TREE_TYPE (type);
25112 fparms = tree_cons (NULL_TREE, type, void_list_node);
25114 else
25115 fparms = void_list_node;
25117 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
25118 tparms = DECL_TEMPLATE_PARMS (ctmpl);
25119 targs = CLASSTYPE_TI_ARGS (type);
25120 ci = NULL_TREE;
25121 fargs = NULL_TREE;
25122 loc = DECL_SOURCE_LOCATION (ctmpl);
25123 explicit_p = false;
25125 else
25127 if (outer_args)
25128 ctor = tsubst (ctor, outer_args, complain, ctor);
25129 type = DECL_CONTEXT (ctor);
25130 tree fn_tmpl;
25131 if (TREE_CODE (ctor) == TEMPLATE_DECL)
25133 fn_tmpl = ctor;
25134 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
25136 else
25137 fn_tmpl = DECL_TI_TEMPLATE (ctor);
25139 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
25140 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
25141 fully specialized args for the enclosing class. Strip those off, as
25142 the deduction guide won't have those template parameters. */
25143 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
25144 TMPL_PARMS_DEPTH (tparms));
25145 /* Discard the 'this' parameter. */
25146 fparms = FUNCTION_ARG_CHAIN (ctor);
25147 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
25148 ci = get_constraints (ctor);
25149 loc = DECL_SOURCE_LOCATION (ctor);
25150 explicit_p = DECL_NONCONVERTING_P (ctor);
25152 if (PRIMARY_TEMPLATE_P (fn_tmpl))
25154 memtmpl = true;
25156 /* For a member template constructor, we need to flatten the two
25157 template parameter lists into one, and then adjust the function
25158 signature accordingly. This gets...complicated. */
25159 ++processing_template_decl;
25160 tree save_parms = current_template_parms;
25162 /* For a member template we should have two levels of parms/args, one
25163 for the class and one for the constructor. We stripped
25164 specialized args for further enclosing classes above. */
25165 const int depth = 2;
25166 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
25168 /* Template args for translating references to the two-level template
25169 parameters into references to the one-level template parameters we
25170 are creating. */
25171 tree tsubst_args = copy_node (targs);
25172 TMPL_ARGS_LEVEL (tsubst_args, depth)
25173 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
25175 /* Template parms for the constructor template. */
25176 tree ftparms = TREE_VALUE (tparms);
25177 unsigned flen = TREE_VEC_LENGTH (ftparms);
25178 /* Template parms for the class template. */
25179 tparms = TREE_CHAIN (tparms);
25180 tree ctparms = TREE_VALUE (tparms);
25181 unsigned clen = TREE_VEC_LENGTH (ctparms);
25182 /* Template parms for the deduction guide start as a copy of the
25183 template parms for the class. We set current_template_parms for
25184 lookup_template_class_1. */
25185 current_template_parms = tparms = copy_node (tparms);
25186 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
25187 for (unsigned i = 0; i < clen; ++i)
25188 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
25190 /* Now we need to rewrite the constructor parms to append them to the
25191 class parms. */
25192 for (unsigned i = 0; i < flen; ++i)
25194 unsigned index = i + clen;
25195 unsigned level = 1;
25196 tree oldelt = TREE_VEC_ELT (ftparms, i);
25197 tree olddecl = TREE_VALUE (oldelt);
25198 tree newdecl = rewrite_template_parm (olddecl, index, level,
25199 tsubst_args, complain);
25200 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
25201 tsubst_args, complain, ctor);
25202 tree list = build_tree_list (newdef, newdecl);
25203 TEMPLATE_PARM_CONSTRAINTS (list)
25204 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
25205 tsubst_args, complain, ctor);
25206 TREE_VEC_ELT (new_vec, index) = list;
25207 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
25210 /* Now we have a final set of template parms to substitute into the
25211 function signature. */
25212 targs = template_parms_to_args (tparms);
25213 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
25214 complain, ctor);
25215 fargs = tsubst (fargs, tsubst_args, complain, ctor);
25216 if (ci)
25217 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
25219 current_template_parms = save_parms;
25220 --processing_template_decl;
25224 if (!memtmpl)
25226 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
25227 tparms = copy_node (tparms);
25228 INNERMOST_TEMPLATE_PARMS (tparms)
25229 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
25232 tree fntype = build_function_type (type, fparms);
25233 tree ded_fn = build_lang_decl_loc (loc,
25234 FUNCTION_DECL,
25235 dguide_name (type), fntype);
25236 DECL_ARGUMENTS (ded_fn) = fargs;
25237 DECL_ARTIFICIAL (ded_fn) = true;
25238 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
25239 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
25240 DECL_ARTIFICIAL (ded_tmpl) = true;
25241 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
25242 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
25243 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
25244 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
25245 if (DECL_P (ctor))
25246 DECL_ABSTRACT_ORIGIN (ded_fn) = ctor;
25247 if (ci)
25248 set_constraints (ded_tmpl, ci);
25250 return ded_tmpl;
25253 /* Deduce template arguments for the class template placeholder PTYPE for
25254 template TMPL based on the initializer INIT, and return the resulting
25255 type. */
25257 static tree
25258 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
25259 tsubst_flags_t complain)
25261 if (!DECL_CLASS_TEMPLATE_P (tmpl))
25263 /* We should have handled this in the caller. */
25264 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
25265 return ptype;
25266 if (complain & tf_error)
25267 error ("non-class template %qT used without template arguments", tmpl);
25268 return error_mark_node;
25271 tree type = TREE_TYPE (tmpl);
25273 vec<tree,va_gc> *args;
25274 if (init == NULL_TREE
25275 || TREE_CODE (init) == TREE_LIST)
25276 args = make_tree_vector_from_list (init);
25277 else if (BRACE_ENCLOSED_INITIALIZER_P (init)
25278 && !TYPE_HAS_LIST_CTOR (type)
25279 && !is_std_init_list (type))
25280 args = make_tree_vector_from_ctor (init);
25281 else
25282 args = make_tree_vector_single (init);
25284 tree dname = dguide_name (tmpl);
25285 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
25286 /*type*/false, /*complain*/false,
25287 /*hidden*/false);
25288 bool elided = false;
25289 if (cands == error_mark_node)
25290 cands = NULL_TREE;
25292 /* Prune explicit deduction guides in copy-initialization context. */
25293 if (flags & LOOKUP_ONLYCONVERTING)
25295 for (lkp_iterator iter (cands); !elided && iter; ++iter)
25296 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25297 elided = true;
25299 if (elided)
25301 /* Found a nonconverting guide, prune the candidates. */
25302 tree pruned = NULL_TREE;
25303 for (lkp_iterator iter (cands); iter; ++iter)
25304 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25305 pruned = lookup_add (*iter, pruned);
25307 cands = pruned;
25311 tree outer_args = NULL_TREE;
25312 if (DECL_CLASS_SCOPE_P (tmpl)
25313 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl)))
25315 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
25316 type = TREE_TYPE (most_general_template (tmpl));
25319 bool saw_ctor = false;
25320 if (CLASSTYPE_METHOD_VEC (type))
25321 // FIXME cache artificial deduction guides
25322 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type));
25323 iter; ++iter)
25325 tree guide = build_deduction_guide (*iter, outer_args, complain);
25326 if ((flags & LOOKUP_ONLYCONVERTING)
25327 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
25328 elided = true;
25329 else
25330 cands = lookup_add (guide, cands);
25332 saw_ctor = true;
25335 if (args->length () < 2)
25337 tree gtype = NULL_TREE;
25339 if (args->length () == 1)
25340 gtype = build_reference_type (type);
25341 else if (!saw_ctor)
25342 gtype = type;
25344 if (gtype)
25346 tree guide = build_deduction_guide (gtype, outer_args, complain);
25347 if ((flags & LOOKUP_ONLYCONVERTING)
25348 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
25349 elided = true;
25350 else
25351 cands = lookup_add (guide, cands);
25355 if (elided && !cands)
25357 error ("cannot deduce template arguments for copy-initialization"
25358 " of %qT, as it has no non-explicit deduction guides or "
25359 "user-declared constructors", type);
25360 return error_mark_node;
25363 ++cp_unevaluated_operand;
25364 tree t = build_new_function_call (cands, &args, tf_decltype);
25366 if (t == error_mark_node && (complain & tf_warning_or_error))
25368 error ("class template argument deduction failed:");
25369 t = build_new_function_call (cands, &args, complain | tf_decltype);
25370 if (elided)
25371 inform (input_location, "explicit deduction guides not considered "
25372 "for copy-initialization");
25375 --cp_unevaluated_operand;
25376 release_tree_vector (args);
25378 return cp_build_qualified_type (TREE_TYPE (t), cp_type_quals (ptype));
25381 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25382 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
25384 tree
25385 do_auto_deduction (tree type, tree init, tree auto_node)
25387 return do_auto_deduction (type, init, auto_node,
25388 tf_warning_or_error,
25389 adc_unspecified);
25392 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25393 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
25394 The CONTEXT determines the context in which auto deduction is performed
25395 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
25396 OUTER_TARGS are used during template argument deduction
25397 (context == adc_unify) to properly substitute the result, and is ignored
25398 in other contexts.
25400 For partial-concept-ids, extra args may be appended to the list of deduced
25401 template arguments prior to determining constraint satisfaction. */
25403 tree
25404 do_auto_deduction (tree type, tree init, tree auto_node,
25405 tsubst_flags_t complain, auto_deduction_context context,
25406 tree outer_targs, int flags)
25408 tree targs;
25410 if (init == error_mark_node)
25411 return error_mark_node;
25413 if (init && type_dependent_expression_p (init)
25414 && context != adc_unify)
25415 /* Defining a subset of type-dependent expressions that we can deduce
25416 from ahead of time isn't worth the trouble. */
25417 return type;
25419 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
25420 /* C++17 class template argument deduction. */
25421 return do_class_deduction (type, tmpl, init, flags, complain);
25423 if (TREE_TYPE (init) == NULL_TREE)
25424 /* Nothing we can do with this, even in deduction context. */
25425 return type;
25427 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
25428 with either a new invented type template parameter U or, if the
25429 initializer is a braced-init-list (8.5.4), with
25430 std::initializer_list<U>. */
25431 if (BRACE_ENCLOSED_INITIALIZER_P (init))
25433 if (!DIRECT_LIST_INIT_P (init))
25434 type = listify_autos (type, auto_node);
25435 else if (CONSTRUCTOR_NELTS (init) == 1)
25436 init = CONSTRUCTOR_ELT (init, 0)->value;
25437 else
25439 if (complain & tf_warning_or_error)
25441 if (permerror (input_location, "direct-list-initialization of "
25442 "%<auto%> requires exactly one element"))
25443 inform (input_location,
25444 "for deduction to %<std::initializer_list%>, use copy-"
25445 "list-initialization (i.e. add %<=%> before the %<{%>)");
25447 type = listify_autos (type, auto_node);
25451 if (type == error_mark_node)
25452 return error_mark_node;
25454 init = resolve_nondeduced_context (init, complain);
25456 if (context == adc_decomp_type
25457 && auto_node == type
25458 && init != error_mark_node
25459 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
25460 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
25461 and initializer has array type, deduce cv-qualified array type. */
25462 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
25463 complain);
25464 else if (AUTO_IS_DECLTYPE (auto_node))
25466 bool id = (DECL_P (init)
25467 || ((TREE_CODE (init) == COMPONENT_REF
25468 || TREE_CODE (init) == SCOPE_REF)
25469 && !REF_PARENTHESIZED_P (init)));
25470 targs = make_tree_vec (1);
25471 TREE_VEC_ELT (targs, 0)
25472 = finish_decltype_type (init, id, tf_warning_or_error);
25473 if (type != auto_node)
25475 if (complain & tf_error)
25476 error ("%qT as type rather than plain %<decltype(auto)%>", type);
25477 return error_mark_node;
25480 else
25482 tree parms = build_tree_list (NULL_TREE, type);
25483 tree tparms;
25485 if (flag_concepts)
25486 tparms = extract_autos (type);
25487 else
25489 tparms = make_tree_vec (1);
25490 TREE_VEC_ELT (tparms, 0)
25491 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
25494 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25495 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
25496 DEDUCE_CALL, LOOKUP_NORMAL,
25497 NULL, /*explain_p=*/false);
25498 if (val > 0)
25500 if (processing_template_decl)
25501 /* Try again at instantiation time. */
25502 return type;
25503 if (type && type != error_mark_node
25504 && (complain & tf_error))
25505 /* If type is error_mark_node a diagnostic must have been
25506 emitted by now. Also, having a mention to '<type error>'
25507 in the diagnostic is not really useful to the user. */
25509 if (cfun && auto_node == current_function_auto_return_pattern
25510 && LAMBDA_FUNCTION_P (current_function_decl))
25511 error ("unable to deduce lambda return type from %qE", init);
25512 else
25513 error ("unable to deduce %qT from %qE", type, init);
25514 type_unification_real (tparms, targs, parms, &init, 1, 0,
25515 DEDUCE_CALL, LOOKUP_NORMAL,
25516 NULL, /*explain_p=*/true);
25518 return error_mark_node;
25522 /* Check any placeholder constraints against the deduced type. */
25523 if (flag_concepts && !processing_template_decl)
25524 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (auto_node))
25526 /* Use the deduced type to check the associated constraints. If we
25527 have a partial-concept-id, rebuild the argument list so that
25528 we check using the extra arguments. */
25529 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
25530 tree cargs = CHECK_CONSTR_ARGS (constr);
25531 if (TREE_VEC_LENGTH (cargs) > 1)
25533 cargs = copy_node (cargs);
25534 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
25536 else
25537 cargs = targs;
25538 if (!constraints_satisfied_p (constr, cargs))
25540 if (complain & tf_warning_or_error)
25542 switch (context)
25544 case adc_unspecified:
25545 case adc_unify:
25546 error("placeholder constraints not satisfied");
25547 break;
25548 case adc_variable_type:
25549 case adc_decomp_type:
25550 error ("deduced initializer does not satisfy "
25551 "placeholder constraints");
25552 break;
25553 case adc_return_type:
25554 error ("deduced return type does not satisfy "
25555 "placeholder constraints");
25556 break;
25557 case adc_requirement:
25558 error ("deduced expression type does not satisfy "
25559 "placeholder constraints");
25560 break;
25562 diagnose_constraints (input_location, constr, targs);
25564 return error_mark_node;
25568 if (processing_template_decl && context != adc_unify)
25569 outer_targs = current_template_args ();
25570 targs = add_to_template_args (outer_targs, targs);
25571 return tsubst (type, targs, complain, NULL_TREE);
25574 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
25575 result. */
25577 tree
25578 splice_late_return_type (tree type, tree late_return_type)
25580 if (is_auto (type))
25582 if (late_return_type)
25583 return late_return_type;
25585 tree idx = get_template_parm_index (type);
25586 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
25587 /* In an abbreviated function template we didn't know we were dealing
25588 with a function template when we saw the auto return type, so update
25589 it to have the correct level. */
25590 return make_auto_1 (TYPE_IDENTIFIER (type), true);
25592 return type;
25595 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
25596 'decltype(auto)' or a deduced class template. */
25598 bool
25599 is_auto (const_tree type)
25601 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
25602 && (TYPE_IDENTIFIER (type) == auto_identifier
25603 || TYPE_IDENTIFIER (type) == decltype_auto_identifier
25604 || CLASS_PLACEHOLDER_TEMPLATE (type)))
25605 return true;
25606 else
25607 return false;
25610 /* for_each_template_parm callback for type_uses_auto. */
25613 is_auto_r (tree tp, void */*data*/)
25615 return is_auto (tp);
25618 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
25619 a use of `auto'. Returns NULL_TREE otherwise. */
25621 tree
25622 type_uses_auto (tree type)
25624 if (type == NULL_TREE)
25625 return NULL_TREE;
25626 else if (flag_concepts)
25628 /* The Concepts TS allows multiple autos in one type-specifier; just
25629 return the first one we find, do_auto_deduction will collect all of
25630 them. */
25631 if (uses_template_parms (type))
25632 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
25633 /*visited*/NULL, /*nondeduced*/true);
25634 else
25635 return NULL_TREE;
25637 else
25638 return find_type_usage (type, is_auto);
25641 /* For a given template T, return the vector of typedefs referenced
25642 in T for which access check is needed at T instantiation time.
25643 T is either a FUNCTION_DECL or a RECORD_TYPE.
25644 Those typedefs were added to T by the function
25645 append_type_to_template_for_access_check. */
25647 vec<qualified_typedef_usage_t, va_gc> *
25648 get_types_needing_access_check (tree t)
25650 tree ti;
25651 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
25653 if (!t || t == error_mark_node)
25654 return NULL;
25656 if (!(ti = get_template_info (t)))
25657 return NULL;
25659 if (CLASS_TYPE_P (t)
25660 || TREE_CODE (t) == FUNCTION_DECL)
25662 if (!TI_TEMPLATE (ti))
25663 return NULL;
25665 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
25668 return result;
25671 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
25672 tied to T. That list of typedefs will be access checked at
25673 T instantiation time.
25674 T is either a FUNCTION_DECL or a RECORD_TYPE.
25675 TYPE_DECL is a TYPE_DECL node representing a typedef.
25676 SCOPE is the scope through which TYPE_DECL is accessed.
25677 LOCATION is the location of the usage point of TYPE_DECL.
25679 This function is a subroutine of
25680 append_type_to_template_for_access_check. */
25682 static void
25683 append_type_to_template_for_access_check_1 (tree t,
25684 tree type_decl,
25685 tree scope,
25686 location_t location)
25688 qualified_typedef_usage_t typedef_usage;
25689 tree ti;
25691 if (!t || t == error_mark_node)
25692 return;
25694 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
25695 || CLASS_TYPE_P (t))
25696 && type_decl
25697 && TREE_CODE (type_decl) == TYPE_DECL
25698 && scope);
25700 if (!(ti = get_template_info (t)))
25701 return;
25703 gcc_assert (TI_TEMPLATE (ti));
25705 typedef_usage.typedef_decl = type_decl;
25706 typedef_usage.context = scope;
25707 typedef_usage.locus = location;
25709 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
25712 /* Append TYPE_DECL to the template TEMPL.
25713 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
25714 At TEMPL instanciation time, TYPE_DECL will be checked to see
25715 if it can be accessed through SCOPE.
25716 LOCATION is the location of the usage point of TYPE_DECL.
25718 e.g. consider the following code snippet:
25720 class C
25722 typedef int myint;
25725 template<class U> struct S
25727 C::myint mi; // <-- usage point of the typedef C::myint
25730 S<char> s;
25732 At S<char> instantiation time, we need to check the access of C::myint
25733 In other words, we need to check the access of the myint typedef through
25734 the C scope. For that purpose, this function will add the myint typedef
25735 and the scope C through which its being accessed to a list of typedefs
25736 tied to the template S. That list will be walked at template instantiation
25737 time and access check performed on each typedefs it contains.
25738 Note that this particular code snippet should yield an error because
25739 myint is private to C. */
25741 void
25742 append_type_to_template_for_access_check (tree templ,
25743 tree type_decl,
25744 tree scope,
25745 location_t location)
25747 qualified_typedef_usage_t *iter;
25748 unsigned i;
25750 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
25752 /* Make sure we don't append the type to the template twice. */
25753 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
25754 if (iter->typedef_decl == type_decl && scope == iter->context)
25755 return;
25757 append_type_to_template_for_access_check_1 (templ, type_decl,
25758 scope, location);
25761 /* Convert the generic type parameters in PARM that match the types given in the
25762 range [START_IDX, END_IDX) from the current_template_parms into generic type
25763 packs. */
25765 tree
25766 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
25768 tree current = current_template_parms;
25769 int depth = TMPL_PARMS_DEPTH (current);
25770 current = INNERMOST_TEMPLATE_PARMS (current);
25771 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
25773 for (int i = 0; i < start_idx; ++i)
25774 TREE_VEC_ELT (replacement, i)
25775 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
25777 for (int i = start_idx; i < end_idx; ++i)
25779 /* Create a distinct parameter pack type from the current parm and add it
25780 to the replacement args to tsubst below into the generic function
25781 parameter. */
25783 tree o = TREE_TYPE (TREE_VALUE
25784 (TREE_VEC_ELT (current, i)));
25785 tree t = copy_type (o);
25786 TEMPLATE_TYPE_PARM_INDEX (t)
25787 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
25788 o, 0, 0, tf_none);
25789 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
25790 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
25791 TYPE_MAIN_VARIANT (t) = t;
25792 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
25793 TYPE_CANONICAL (t) = canonical_type_parameter (t);
25794 TREE_VEC_ELT (replacement, i) = t;
25795 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
25798 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
25799 TREE_VEC_ELT (replacement, i)
25800 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
25802 /* If there are more levels then build up the replacement with the outer
25803 template parms. */
25804 if (depth > 1)
25805 replacement = add_to_template_args (template_parms_to_args
25806 (TREE_CHAIN (current_template_parms)),
25807 replacement);
25809 return tsubst (parm, replacement, tf_none, NULL_TREE);
25812 /* Entries in the decl_constraint hash table. */
25813 struct GTY((for_user)) constr_entry
25815 tree decl;
25816 tree ci;
25819 /* Hashing function and equality for constraint entries. */
25820 struct constr_hasher : ggc_ptr_hash<constr_entry>
25822 static hashval_t hash (constr_entry *e)
25824 return (hashval_t)DECL_UID (e->decl);
25827 static bool equal (constr_entry *e1, constr_entry *e2)
25829 return e1->decl == e2->decl;
25833 /* A mapping from declarations to constraint information. Note that
25834 both templates and their underlying declarations are mapped to the
25835 same constraint information.
25837 FIXME: This is defined in pt.c because garbage collection
25838 code is not being generated for constraint.cc. */
25840 static GTY (()) hash_table<constr_hasher> *decl_constraints;
25842 /* Returns the template constraints of declaration T. If T is not
25843 constrained, return NULL_TREE. Note that T must be non-null. */
25845 tree
25846 get_constraints (tree t)
25848 if (!flag_concepts)
25849 return NULL_TREE;
25851 gcc_assert (DECL_P (t));
25852 if (TREE_CODE (t) == TEMPLATE_DECL)
25853 t = DECL_TEMPLATE_RESULT (t);
25854 constr_entry elt = { t, NULL_TREE };
25855 constr_entry* found = decl_constraints->find (&elt);
25856 if (found)
25857 return found->ci;
25858 else
25859 return NULL_TREE;
25862 /* Associate the given constraint information CI with the declaration
25863 T. If T is a template, then the constraints are associated with
25864 its underlying declaration. Don't build associations if CI is
25865 NULL_TREE. */
25867 void
25868 set_constraints (tree t, tree ci)
25870 if (!ci)
25871 return;
25872 gcc_assert (t && flag_concepts);
25873 if (TREE_CODE (t) == TEMPLATE_DECL)
25874 t = DECL_TEMPLATE_RESULT (t);
25875 gcc_assert (!get_constraints (t));
25876 constr_entry elt = {t, ci};
25877 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
25878 constr_entry* entry = ggc_alloc<constr_entry> ();
25879 *entry = elt;
25880 *slot = entry;
25883 /* Remove the associated constraints of the declaration T. */
25885 void
25886 remove_constraints (tree t)
25888 gcc_assert (DECL_P (t));
25889 if (TREE_CODE (t) == TEMPLATE_DECL)
25890 t = DECL_TEMPLATE_RESULT (t);
25892 constr_entry elt = {t, NULL_TREE};
25893 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
25894 if (slot)
25895 decl_constraints->clear_slot (slot);
25898 /* Memoized satisfaction results for declarations. This
25899 maps the pair (constraint_info, arguments) to the result computed
25900 by constraints_satisfied_p. */
25902 struct GTY((for_user)) constraint_sat_entry
25904 tree ci;
25905 tree args;
25906 tree result;
25909 /* Hashing function and equality for constraint entries. */
25911 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
25913 static hashval_t hash (constraint_sat_entry *e)
25915 hashval_t val = iterative_hash_object(e->ci, 0);
25916 return iterative_hash_template_arg (e->args, val);
25919 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
25921 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
25925 /* Memoized satisfaction results for concept checks. */
25927 struct GTY((for_user)) concept_spec_entry
25929 tree tmpl;
25930 tree args;
25931 tree result;
25934 /* Hashing function and equality for constraint entries. */
25936 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
25938 static hashval_t hash (concept_spec_entry *e)
25940 return hash_tmpl_and_args (e->tmpl, e->args);
25943 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
25945 ++comparing_specializations;
25946 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
25947 --comparing_specializations;
25948 return eq;
25952 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
25953 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
25955 /* Search for a memoized satisfaction result. Returns one of the
25956 truth value nodes if previously memoized, or NULL_TREE otherwise. */
25958 tree
25959 lookup_constraint_satisfaction (tree ci, tree args)
25961 constraint_sat_entry elt = { ci, args, NULL_TREE };
25962 constraint_sat_entry* found = constraint_memos->find (&elt);
25963 if (found)
25964 return found->result;
25965 else
25966 return NULL_TREE;
25969 /* Memoize the result of a satisfication test. Returns the saved result. */
25971 tree
25972 memoize_constraint_satisfaction (tree ci, tree args, tree result)
25974 constraint_sat_entry elt = {ci, args, result};
25975 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
25976 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
25977 *entry = elt;
25978 *slot = entry;
25979 return result;
25982 /* Search for a memoized satisfaction result for a concept. */
25984 tree
25985 lookup_concept_satisfaction (tree tmpl, tree args)
25987 concept_spec_entry elt = { tmpl, args, NULL_TREE };
25988 concept_spec_entry* found = concept_memos->find (&elt);
25989 if (found)
25990 return found->result;
25991 else
25992 return NULL_TREE;
25995 /* Memoize the result of a concept check. Returns the saved result. */
25997 tree
25998 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
26000 concept_spec_entry elt = {tmpl, args, result};
26001 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
26002 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26003 *entry = elt;
26004 *slot = entry;
26005 return result;
26008 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
26010 /* Returns a prior concept specialization. This returns the substituted
26011 and normalized constraints defined by the concept. */
26013 tree
26014 get_concept_expansion (tree tmpl, tree args)
26016 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26017 concept_spec_entry* found = concept_expansions->find (&elt);
26018 if (found)
26019 return found->result;
26020 else
26021 return NULL_TREE;
26024 /* Save a concept expansion for later. */
26026 tree
26027 save_concept_expansion (tree tmpl, tree args, tree def)
26029 concept_spec_entry elt = {tmpl, args, def};
26030 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
26031 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26032 *entry = elt;
26033 *slot = entry;
26034 return def;
26037 static hashval_t
26038 hash_subsumption_args (tree t1, tree t2)
26040 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
26041 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
26042 int val = 0;
26043 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
26044 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
26045 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
26046 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
26047 return val;
26050 /* Compare the constraints of two subsumption entries. The LEFT1 and
26051 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
26052 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
26054 static bool
26055 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
26057 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
26058 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
26059 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
26060 CHECK_CONSTR_ARGS (right1)))
26061 return comp_template_args (CHECK_CONSTR_ARGS (left2),
26062 CHECK_CONSTR_ARGS (right2));
26063 return false;
26066 /* Key/value pair for learning and memoizing subsumption results. This
26067 associates a pair of check constraints (including arguments) with
26068 a boolean value indicating the result. */
26070 struct GTY((for_user)) subsumption_entry
26072 tree t1;
26073 tree t2;
26074 bool result;
26077 /* Hashing function and equality for constraint entries. */
26079 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
26081 static hashval_t hash (subsumption_entry *e)
26083 return hash_subsumption_args (e->t1, e->t2);
26086 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
26088 ++comparing_specializations;
26089 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
26090 --comparing_specializations;
26091 return eq;
26095 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
26097 /* Search for a previously cached subsumption result. */
26099 bool*
26100 lookup_subsumption_result (tree t1, tree t2)
26102 subsumption_entry elt = { t1, t2, false };
26103 subsumption_entry* found = subsumption_table->find (&elt);
26104 if (found)
26105 return &found->result;
26106 else
26107 return 0;
26110 /* Save a subsumption result. */
26112 bool
26113 save_subsumption_result (tree t1, tree t2, bool result)
26115 subsumption_entry elt = {t1, t2, result};
26116 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
26117 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
26118 *entry = elt;
26119 *slot = entry;
26120 return result;
26123 /* Set up the hash table for constraint association. */
26125 void
26126 init_constraint_processing (void)
26128 if (!flag_concepts)
26129 return;
26131 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
26132 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
26133 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
26134 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
26135 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
26138 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
26139 0..N-1. */
26141 void
26142 declare_integer_pack (void)
26144 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
26145 build_function_type_list (integer_type_node,
26146 integer_type_node,
26147 NULL_TREE),
26148 NULL_TREE, ECF_CONST);
26149 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
26150 DECL_BUILT_IN_CLASS (ipfn) = BUILT_IN_FRONTEND;
26153 /* Set up the hash tables for template instantiations. */
26155 void
26156 init_template_processing (void)
26158 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
26159 type_specializations = hash_table<spec_hasher>::create_ggc (37);
26161 if (cxx_dialect >= cxx11)
26162 declare_integer_pack ();
26165 /* Print stats about the template hash tables for -fstats. */
26167 void
26168 print_template_statistics (void)
26170 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
26171 "%f collisions\n", (long) decl_specializations->size (),
26172 (long) decl_specializations->elements (),
26173 decl_specializations->collisions ());
26174 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
26175 "%f collisions\n", (long) type_specializations->size (),
26176 (long) type_specializations->elements (),
26177 type_specializations->collisions ());
26180 #include "gt-cp-pt.h"