Overhaul pointer-to-member conversion and template argument handling.
[official-gcc.git] / gcc / cp / pt.c
blobb537cb8a85d7d9111a1f513fdd1e3ea4afbd36b6
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 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6128 tree orig_expr = expr;
6129 STRIP_NOPS (expr);
6130 if (null_ptr_cst_p (expr))
6131 return true;
6132 if (TREE_CODE (expr) == PTRMEM_CST
6133 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6134 PTRMEM_CST_CLASS (expr)))
6135 return true;
6136 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6137 return true;
6138 if (processing_template_decl
6139 && TREE_CODE (expr) == ADDR_EXPR
6140 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6141 return true;
6142 if (complain & tf_error)
6144 error_at (loc, "%qE is not a valid template argument for type %qT",
6145 orig_expr, type);
6146 if (TREE_CODE (expr) != PTRMEM_CST)
6147 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6148 else
6149 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6151 return false;
6154 /* Returns TRUE iff the address of OP is value-dependent.
6156 14.6.2.4 [temp.dep.temp]:
6157 A non-integral non-type template-argument is dependent if its type is
6158 dependent or it has either of the following forms
6159 qualified-id
6160 & qualified-id
6161 and contains a nested-name-specifier which specifies a class-name that
6162 names a dependent type.
6164 We generalize this to just say that the address of a member of a
6165 dependent class is value-dependent; the above doesn't cover the
6166 address of a static data member named with an unqualified-id. */
6168 static bool
6169 has_value_dependent_address (tree op)
6171 /* We could use get_inner_reference here, but there's no need;
6172 this is only relevant for template non-type arguments, which
6173 can only be expressed as &id-expression. */
6174 if (DECL_P (op))
6176 tree ctx = CP_DECL_CONTEXT (op);
6177 if (TYPE_P (ctx) && dependent_type_p (ctx))
6178 return true;
6181 return false;
6184 /* The next set of functions are used for providing helpful explanatory
6185 diagnostics for failed overload resolution. Their messages should be
6186 indented by two spaces for consistency with the messages in
6187 call.c */
6189 static int
6190 unify_success (bool /*explain_p*/)
6192 return 0;
6195 /* Other failure functions should call this one, to provide a single function
6196 for setting a breakpoint on. */
6198 static int
6199 unify_invalid (bool /*explain_p*/)
6201 return 1;
6204 static int
6205 unify_parameter_deduction_failure (bool explain_p, tree parm)
6207 if (explain_p)
6208 inform (input_location,
6209 " couldn't deduce template parameter %qD", parm);
6210 return unify_invalid (explain_p);
6213 static int
6214 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6216 if (explain_p)
6217 inform (input_location,
6218 " types %qT and %qT have incompatible cv-qualifiers",
6219 parm, arg);
6220 return unify_invalid (explain_p);
6223 static int
6224 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6226 if (explain_p)
6227 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6228 return unify_invalid (explain_p);
6231 static int
6232 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6234 if (explain_p)
6235 inform (input_location,
6236 " template parameter %qD is not a parameter pack, but "
6237 "argument %qD is",
6238 parm, arg);
6239 return unify_invalid (explain_p);
6242 static int
6243 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6245 if (explain_p)
6246 inform (input_location,
6247 " template argument %qE does not match "
6248 "pointer-to-member constant %qE",
6249 arg, parm);
6250 return unify_invalid (explain_p);
6253 static int
6254 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6256 if (explain_p)
6257 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6258 return unify_invalid (explain_p);
6261 static int
6262 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6264 if (explain_p)
6265 inform (input_location,
6266 " inconsistent parameter pack deduction with %qT and %qT",
6267 old_arg, new_arg);
6268 return unify_invalid (explain_p);
6271 static int
6272 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6274 if (explain_p)
6276 if (TYPE_P (parm))
6277 inform (input_location,
6278 " deduced conflicting types for parameter %qT (%qT and %qT)",
6279 parm, first, second);
6280 else
6281 inform (input_location,
6282 " deduced conflicting values for non-type parameter "
6283 "%qE (%qE and %qE)", parm, first, second);
6285 return unify_invalid (explain_p);
6288 static int
6289 unify_vla_arg (bool explain_p, tree arg)
6291 if (explain_p)
6292 inform (input_location,
6293 " variable-sized array type %qT is not "
6294 "a valid template argument",
6295 arg);
6296 return unify_invalid (explain_p);
6299 static int
6300 unify_method_type_error (bool explain_p, tree arg)
6302 if (explain_p)
6303 inform (input_location,
6304 " member function type %qT is not a valid template argument",
6305 arg);
6306 return unify_invalid (explain_p);
6309 static int
6310 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6312 if (explain_p)
6314 if (least_p)
6315 inform_n (input_location, wanted,
6316 " candidate expects at least %d argument, %d provided",
6317 " candidate expects at least %d arguments, %d provided",
6318 wanted, have);
6319 else
6320 inform_n (input_location, wanted,
6321 " candidate expects %d argument, %d provided",
6322 " candidate expects %d arguments, %d provided",
6323 wanted, have);
6325 return unify_invalid (explain_p);
6328 static int
6329 unify_too_many_arguments (bool explain_p, int have, int wanted)
6331 return unify_arity (explain_p, have, wanted);
6334 static int
6335 unify_too_few_arguments (bool explain_p, int have, int wanted,
6336 bool least_p = false)
6338 return unify_arity (explain_p, have, wanted, least_p);
6341 static int
6342 unify_arg_conversion (bool explain_p, tree to_type,
6343 tree from_type, tree arg)
6345 if (explain_p)
6346 inform (EXPR_LOC_OR_LOC (arg, input_location),
6347 " cannot convert %qE (type %qT) to type %qT",
6348 arg, from_type, to_type);
6349 return unify_invalid (explain_p);
6352 static int
6353 unify_no_common_base (bool explain_p, enum template_base_result r,
6354 tree parm, tree arg)
6356 if (explain_p)
6357 switch (r)
6359 case tbr_ambiguous_baseclass:
6360 inform (input_location, " %qT is an ambiguous base class of %qT",
6361 parm, arg);
6362 break;
6363 default:
6364 inform (input_location, " %qT is not derived from %qT", arg, parm);
6365 break;
6367 return unify_invalid (explain_p);
6370 static int
6371 unify_inconsistent_template_template_parameters (bool explain_p)
6373 if (explain_p)
6374 inform (input_location,
6375 " template parameters of a template template argument are "
6376 "inconsistent with other deduced template arguments");
6377 return unify_invalid (explain_p);
6380 static int
6381 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6383 if (explain_p)
6384 inform (input_location,
6385 " can't deduce a template for %qT from non-template type %qT",
6386 parm, arg);
6387 return unify_invalid (explain_p);
6390 static int
6391 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6393 if (explain_p)
6394 inform (input_location,
6395 " template argument %qE does not match %qE", arg, parm);
6396 return unify_invalid (explain_p);
6399 static int
6400 unify_overload_resolution_failure (bool explain_p, tree arg)
6402 if (explain_p)
6403 inform (input_location,
6404 " could not resolve address from overloaded function %qE",
6405 arg);
6406 return unify_invalid (explain_p);
6409 /* Attempt to convert the non-type template parameter EXPR to the
6410 indicated TYPE. If the conversion is successful, return the
6411 converted value. If the conversion is unsuccessful, return
6412 NULL_TREE if we issued an error message, or error_mark_node if we
6413 did not. We issue error messages for out-and-out bad template
6414 parameters, but not simply because the conversion failed, since we
6415 might be just trying to do argument deduction. Both TYPE and EXPR
6416 must be non-dependent.
6418 The conversion follows the special rules described in
6419 [temp.arg.nontype], and it is much more strict than an implicit
6420 conversion.
6422 This function is called twice for each template argument (see
6423 lookup_template_class for a more accurate description of this
6424 problem). This means that we need to handle expressions which
6425 are not valid in a C++ source, but can be created from the
6426 first call (for instance, casts to perform conversions). These
6427 hacks can go away after we fix the double coercion problem. */
6429 static tree
6430 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6432 tree expr_type;
6434 /* Detect immediately string literals as invalid non-type argument.
6435 This special-case is not needed for correctness (we would easily
6436 catch this later), but only to provide better diagnostic for this
6437 common user mistake. As suggested by DR 100, we do not mention
6438 linkage issues in the diagnostic as this is not the point. */
6439 /* FIXME we're making this OK. */
6440 if (TREE_CODE (expr) == STRING_CST)
6442 if (complain & tf_error)
6443 error ("%qE is not a valid template argument for type %qT "
6444 "because string literals can never be used in this context",
6445 expr, type);
6446 return NULL_TREE;
6449 /* Add the ADDR_EXPR now for the benefit of
6450 value_dependent_expression_p. */
6451 if (TYPE_PTROBV_P (type)
6452 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6454 expr = decay_conversion (expr, complain);
6455 if (expr == error_mark_node)
6456 return error_mark_node;
6459 /* If we are in a template, EXPR may be non-dependent, but still
6460 have a syntactic, rather than semantic, form. For example, EXPR
6461 might be a SCOPE_REF, rather than the VAR_DECL to which the
6462 SCOPE_REF refers. Preserving the qualifying scope is necessary
6463 so that access checking can be performed when the template is
6464 instantiated -- but here we need the resolved form so that we can
6465 convert the argument. */
6466 bool non_dep = false;
6467 if (TYPE_REF_OBJ_P (type)
6468 && has_value_dependent_address (expr))
6469 /* If we want the address and it's value-dependent, don't fold. */;
6470 else if (processing_template_decl
6471 && potential_nondependent_constant_expression (expr))
6472 non_dep = true;
6473 if (error_operand_p (expr))
6474 return error_mark_node;
6475 expr_type = TREE_TYPE (expr);
6476 if (TREE_CODE (type) == REFERENCE_TYPE)
6477 expr = mark_lvalue_use (expr);
6478 else
6479 expr = mark_rvalue_use (expr);
6481 /* If the argument is non-dependent, perform any conversions in
6482 non-dependent context as well. */
6483 processing_template_decl_sentinel s (non_dep);
6484 if (non_dep)
6485 expr = instantiate_non_dependent_expr_internal (expr, complain);
6487 if (value_dependent_expression_p (expr))
6488 expr = canonicalize_expr_argument (expr, complain);
6490 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6491 to a non-type argument of "nullptr". */
6492 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
6493 expr = fold_simple (convert (type, expr));
6495 /* In C++11, integral or enumeration non-type template arguments can be
6496 arbitrary constant expressions. Pointer and pointer to
6497 member arguments can be general constant expressions that evaluate
6498 to a null value, but otherwise still need to be of a specific form. */
6499 if (cxx_dialect >= cxx11)
6501 if (TREE_CODE (expr) == PTRMEM_CST)
6502 /* A PTRMEM_CST is already constant, and a valid template
6503 argument for a parameter of pointer to member type, we just want
6504 to leave it in that form rather than lower it to a
6505 CONSTRUCTOR. */;
6506 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6507 /* Constant value checking is done later with type conversion. */;
6508 else if (cxx_dialect >= cxx1z)
6510 if (TREE_CODE (type) != REFERENCE_TYPE)
6511 expr = maybe_constant_value (expr);
6512 else if (REFERENCE_REF_P (expr))
6514 expr = TREE_OPERAND (expr, 0);
6515 expr = maybe_constant_value (expr);
6516 expr = convert_from_reference (expr);
6519 else if (TYPE_PTR_OR_PTRMEM_P (type))
6521 tree folded = maybe_constant_value (expr);
6522 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6523 : null_member_pointer_value_p (folded))
6524 expr = folded;
6528 /* HACK: Due to double coercion, we can get a
6529 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6530 which is the tree that we built on the first call (see
6531 below when coercing to reference to object or to reference to
6532 function). We just strip everything and get to the arg.
6533 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6534 for examples. */
6535 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6537 tree probe_type, probe = expr;
6538 if (REFERENCE_REF_P (probe))
6539 probe = TREE_OPERAND (probe, 0);
6540 probe_type = TREE_TYPE (probe);
6541 if (TREE_CODE (probe) == NOP_EXPR)
6543 /* ??? Maybe we could use convert_from_reference here, but we
6544 would need to relax its constraints because the NOP_EXPR
6545 could actually change the type to something more cv-qualified,
6546 and this is not folded by convert_from_reference. */
6547 tree addr = TREE_OPERAND (probe, 0);
6548 if (TREE_CODE (probe_type) == REFERENCE_TYPE
6549 && TREE_CODE (addr) == ADDR_EXPR
6550 && TYPE_PTR_P (TREE_TYPE (addr))
6551 && (same_type_ignoring_top_level_qualifiers_p
6552 (TREE_TYPE (probe_type),
6553 TREE_TYPE (TREE_TYPE (addr)))))
6555 expr = TREE_OPERAND (addr, 0);
6556 expr_type = TREE_TYPE (probe_type);
6561 /* We could also generate a NOP_EXPR(ADDR_EXPR()) when the
6562 parameter is a pointer to object, through decay and
6563 qualification conversion. Let's strip everything. */
6564 else if (TREE_CODE (expr) == NOP_EXPR && TYPE_PTROBV_P (type))
6566 tree probe = expr;
6567 STRIP_NOPS (probe);
6568 if (TREE_CODE (probe) == ADDR_EXPR
6569 && TYPE_PTR_P (TREE_TYPE (probe)))
6571 /* Skip the ADDR_EXPR only if it is part of the decay for
6572 an array. Otherwise, it is part of the original argument
6573 in the source code. */
6574 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (probe, 0))) == ARRAY_TYPE)
6575 probe = TREE_OPERAND (probe, 0);
6576 expr = probe;
6577 expr_type = TREE_TYPE (expr);
6581 /* [temp.arg.nontype]/5, bullet 1
6583 For a non-type template-parameter of integral or enumeration type,
6584 integral promotions (_conv.prom_) and integral conversions
6585 (_conv.integral_) are applied. */
6586 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6588 tree t = build_integral_nontype_arg_conv (type, expr, complain);
6589 t = maybe_constant_value (t);
6590 if (t != error_mark_node)
6591 expr = t;
6593 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6594 return error_mark_node;
6596 /* Notice that there are constant expressions like '4 % 0' which
6597 do not fold into integer constants. */
6598 if (TREE_CODE (expr) != INTEGER_CST
6599 && !value_dependent_expression_p (expr))
6601 if (complain & tf_error)
6603 int errs = errorcount, warns = warningcount + werrorcount;
6604 if (processing_template_decl
6605 && !require_potential_constant_expression (expr))
6606 return NULL_TREE;
6607 expr = cxx_constant_value (expr);
6608 if (errorcount > errs || warningcount + werrorcount > warns)
6609 inform (EXPR_LOC_OR_LOC (expr, input_location),
6610 "in template argument for type %qT ", type);
6611 if (expr == error_mark_node)
6612 return NULL_TREE;
6613 /* else cxx_constant_value complained but gave us
6614 a real constant, so go ahead. */
6615 gcc_assert (TREE_CODE (expr) == INTEGER_CST);
6617 else
6618 return NULL_TREE;
6621 /* Avoid typedef problems. */
6622 if (TREE_TYPE (expr) != type)
6623 expr = fold_convert (type, expr);
6625 /* [temp.arg.nontype]/5, bullet 2
6627 For a non-type template-parameter of type pointer to object,
6628 qualification conversions (_conv.qual_) and the array-to-pointer
6629 conversion (_conv.array_) are applied. */
6630 else if (TYPE_PTROBV_P (type))
6632 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6634 A template-argument for a non-type, non-template template-parameter
6635 shall be one of: [...]
6637 -- the name of a non-type template-parameter;
6638 -- the address of an object or function with external linkage, [...]
6639 expressed as "& id-expression" where the & is optional if the name
6640 refers to a function or array, or if the corresponding
6641 template-parameter is a reference.
6643 Here, we do not care about functions, as they are invalid anyway
6644 for a parameter of type pointer-to-object. */
6646 if (value_dependent_expression_p (expr))
6647 /* Non-type template parameters are OK. */
6649 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6650 /* Null pointer values are OK in C++11. */;
6651 else if (TREE_CODE (expr) != ADDR_EXPR
6652 && TREE_CODE (expr_type) != ARRAY_TYPE)
6654 if (VAR_P (expr))
6656 if (complain & tf_error)
6657 error ("%qD is not a valid template argument "
6658 "because %qD is a variable, not the address of "
6659 "a variable", expr, expr);
6660 return NULL_TREE;
6662 if (POINTER_TYPE_P (expr_type))
6664 if (complain & tf_error)
6665 error ("%qE is not a valid template argument for %qT "
6666 "because it is not the address of a variable",
6667 expr, type);
6668 return NULL_TREE;
6670 /* Other values, like integer constants, might be valid
6671 non-type arguments of some other type. */
6672 return error_mark_node;
6674 else
6676 tree decl;
6678 decl = ((TREE_CODE (expr) == ADDR_EXPR)
6679 ? TREE_OPERAND (expr, 0) : expr);
6680 if (!VAR_P (decl))
6682 if (complain & tf_error)
6683 error ("%qE is not a valid template argument of type %qT "
6684 "because %qE is not a variable", expr, type, decl);
6685 return NULL_TREE;
6687 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6689 if (complain & tf_error)
6690 error ("%qE is not a valid template argument of type %qT "
6691 "because %qD does not have external linkage",
6692 expr, type, decl);
6693 return NULL_TREE;
6695 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx1z)
6696 && decl_linkage (decl) == lk_none)
6698 if (complain & tf_error)
6699 error ("%qE is not a valid template argument of type %qT "
6700 "because %qD has no linkage", expr, type, decl);
6701 return NULL_TREE;
6703 /* C++17: For a non-type template-parameter of reference or pointer
6704 type, the value of the constant expression shall not refer to (or
6705 for a pointer type, shall not be the address of):
6706 * a subobject (4.5),
6707 * a temporary object (15.2),
6708 * a string literal (5.13.5),
6709 * the result of a typeid expression (8.2.8), or
6710 * a predefined __func__ variable (11.4.1). */
6711 else if (DECL_ARTIFICIAL (decl))
6713 if (complain & tf_error)
6714 error ("the address of %qD is not a valid template argument",
6715 decl);
6716 return NULL_TREE;
6718 else if (!same_type_ignoring_top_level_qualifiers_p
6719 (strip_array_types (TREE_TYPE (type)),
6720 strip_array_types (TREE_TYPE (decl))))
6722 if (complain & tf_error)
6723 error ("the address of the %qT subobject of %qD is not a "
6724 "valid template argument", TREE_TYPE (type), decl);
6725 return NULL_TREE;
6727 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
6729 if (complain & tf_error)
6730 error ("the address of %qD is not a valid template argument "
6731 "because it does not have static storage duration",
6732 decl);
6733 return NULL_TREE;
6737 expr = decay_conversion (expr, complain);
6738 if (expr == error_mark_node)
6739 return error_mark_node;
6741 expr = perform_qualification_conversions (type, expr);
6742 if (expr == error_mark_node)
6743 return error_mark_node;
6745 /* [temp.arg.nontype]/5, bullet 3
6747 For a non-type template-parameter of type reference to object, no
6748 conversions apply. The type referred to by the reference may be more
6749 cv-qualified than the (otherwise identical) type of the
6750 template-argument. The template-parameter is bound directly to the
6751 template-argument, which must be an lvalue. */
6752 else if (TYPE_REF_OBJ_P (type))
6754 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6755 expr_type))
6756 return error_mark_node;
6758 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6760 if (complain & tf_error)
6761 error ("%qE is not a valid template argument for type %qT "
6762 "because of conflicts in cv-qualification", expr, type);
6763 return NULL_TREE;
6766 if (!lvalue_p (expr))
6768 if (complain & tf_error)
6769 error ("%qE is not a valid template argument for type %qT "
6770 "because it is not an lvalue", expr, type);
6771 return NULL_TREE;
6774 /* [temp.arg.nontype]/1
6776 A template-argument for a non-type, non-template template-parameter
6777 shall be one of: [...]
6779 -- the address of an object or function with external linkage. */
6780 if (INDIRECT_REF_P (expr)
6781 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6783 expr = TREE_OPERAND (expr, 0);
6784 if (DECL_P (expr))
6786 if (complain & tf_error)
6787 error ("%q#D is not a valid template argument for type %qT "
6788 "because a reference variable does not have a constant "
6789 "address", expr, type);
6790 return NULL_TREE;
6794 if (TYPE_REF_OBJ_P (TREE_TYPE (expr))
6795 && value_dependent_expression_p (expr))
6796 /* OK, dependent reference. We don't want to ask whether a DECL is
6797 itself value-dependent, since what we want here is its address. */;
6798 else
6800 if (!DECL_P (expr))
6802 if (complain & tf_error)
6803 error ("%qE is not a valid template argument for type %qT "
6804 "because it is not an object with linkage",
6805 expr, type);
6806 return NULL_TREE;
6809 /* DR 1155 allows internal linkage in C++11 and up. */
6810 linkage_kind linkage = decl_linkage (expr);
6811 if (linkage < (cxx_dialect >= cxx11 ? lk_internal : lk_external))
6813 if (complain & tf_error)
6814 error ("%qE is not a valid template argument for type %qT "
6815 "because object %qD does not have linkage",
6816 expr, type, expr);
6817 return NULL_TREE;
6820 expr = build_address (expr);
6823 if (!same_type_p (type, TREE_TYPE (expr)))
6824 expr = build_nop (type, expr);
6826 /* [temp.arg.nontype]/5, bullet 4
6828 For a non-type template-parameter of type pointer to function, only
6829 the function-to-pointer conversion (_conv.func_) is applied. If the
6830 template-argument represents a set of overloaded functions (or a
6831 pointer to such), the matching function is selected from the set
6832 (_over.over_). */
6833 else if (TYPE_PTRFN_P (type))
6835 /* If the argument is a template-id, we might not have enough
6836 context information to decay the pointer. */
6837 if (!type_unknown_p (expr_type))
6839 expr = decay_conversion (expr, complain);
6840 if (expr == error_mark_node)
6841 return error_mark_node;
6844 if (cxx_dialect >= cxx11 && integer_zerop (expr))
6845 /* Null pointer values are OK in C++11. */
6846 return perform_qualification_conversions (type, expr);
6848 expr = convert_nontype_argument_function (type, expr, complain);
6849 if (!expr || expr == error_mark_node)
6850 return expr;
6852 /* [temp.arg.nontype]/5, bullet 5
6854 For a non-type template-parameter of type reference to function, no
6855 conversions apply. If the template-argument represents a set of
6856 overloaded functions, the matching function is selected from the set
6857 (_over.over_). */
6858 else if (TYPE_REFFN_P (type))
6860 if (TREE_CODE (expr) == ADDR_EXPR)
6862 if (complain & tf_error)
6864 error ("%qE is not a valid template argument for type %qT "
6865 "because it is a pointer", expr, type);
6866 inform (input_location, "try using %qE instead",
6867 TREE_OPERAND (expr, 0));
6869 return NULL_TREE;
6872 expr = convert_nontype_argument_function (type, expr, complain);
6873 if (!expr || expr == error_mark_node)
6874 return expr;
6876 /* [temp.arg.nontype]/5, bullet 6
6878 For a non-type template-parameter of type pointer to member function,
6879 no conversions apply. If the template-argument represents a set of
6880 overloaded member functions, the matching member function is selected
6881 from the set (_over.over_). */
6882 else if (TYPE_PTRMEMFUNC_P (type))
6884 expr = instantiate_type (type, expr, tf_none);
6885 if (expr == error_mark_node)
6886 return error_mark_node;
6888 /* [temp.arg.nontype] bullet 1 says the pointer to member
6889 expression must be a pointer-to-member constant. */
6890 if (!value_dependent_expression_p (expr)
6891 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6892 return NULL_TREE;
6894 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
6895 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
6896 if (fnptr_conv_p (type, TREE_TYPE (expr)))
6897 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
6899 /* [temp.arg.nontype]/5, bullet 7
6901 For a non-type template-parameter of type pointer to data member,
6902 qualification conversions (_conv.qual_) are applied. */
6903 else if (TYPE_PTRDATAMEM_P (type))
6905 /* [temp.arg.nontype] bullet 1 says the pointer to member
6906 expression must be a pointer-to-member constant. */
6907 if (!value_dependent_expression_p (expr)
6908 && !check_valid_ptrmem_cst_expr (type, expr, complain))
6909 return NULL_TREE;
6911 expr = perform_qualification_conversions (type, expr);
6912 if (expr == error_mark_node)
6913 return expr;
6915 else if (NULLPTR_TYPE_P (type))
6917 if (expr != nullptr_node)
6919 if (complain & tf_error)
6920 error ("%qE is not a valid template argument for type %qT "
6921 "because it is of type %qT", expr, type, TREE_TYPE (expr));
6922 return NULL_TREE;
6924 return expr;
6926 /* A template non-type parameter must be one of the above. */
6927 else
6928 gcc_unreachable ();
6930 /* Sanity check: did we actually convert the argument to the
6931 right type? */
6932 gcc_assert (same_type_ignoring_top_level_qualifiers_p
6933 (type, TREE_TYPE (expr)));
6934 return convert_from_reference (expr);
6937 /* Subroutine of coerce_template_template_parms, which returns 1 if
6938 PARM_PARM and ARG_PARM match using the rule for the template
6939 parameters of template template parameters. Both PARM and ARG are
6940 template parameters; the rest of the arguments are the same as for
6941 coerce_template_template_parms.
6943 static int
6944 coerce_template_template_parm (tree parm,
6945 tree arg,
6946 tsubst_flags_t complain,
6947 tree in_decl,
6948 tree outer_args)
6950 if (arg == NULL_TREE || error_operand_p (arg)
6951 || parm == NULL_TREE || error_operand_p (parm))
6952 return 0;
6954 if (TREE_CODE (arg) != TREE_CODE (parm))
6955 return 0;
6957 switch (TREE_CODE (parm))
6959 case TEMPLATE_DECL:
6960 /* We encounter instantiations of templates like
6961 template <template <template <class> class> class TT>
6962 class C; */
6964 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
6965 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
6967 if (!coerce_template_template_parms
6968 (parmparm, argparm, complain, in_decl, outer_args))
6969 return 0;
6971 /* Fall through. */
6973 case TYPE_DECL:
6974 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
6975 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
6976 /* Argument is a parameter pack but parameter is not. */
6977 return 0;
6978 break;
6980 case PARM_DECL:
6981 /* The tsubst call is used to handle cases such as
6983 template <int> class C {};
6984 template <class T, template <T> class TT> class D {};
6985 D<int, C> d;
6987 i.e. the parameter list of TT depends on earlier parameters. */
6988 if (!uses_template_parms (TREE_TYPE (arg)))
6990 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
6991 if (!uses_template_parms (t)
6992 && !same_type_p (t, TREE_TYPE (arg)))
6993 return 0;
6996 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
6997 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
6998 /* Argument is a parameter pack but parameter is not. */
6999 return 0;
7001 break;
7003 default:
7004 gcc_unreachable ();
7007 return 1;
7010 /* Coerce template argument list ARGLIST for use with template
7011 template-parameter TEMPL. */
7013 static tree
7014 coerce_template_args_for_ttp (tree templ, tree arglist,
7015 tsubst_flags_t complain)
7017 /* Consider an example where a template template parameter declared as
7019 template <class T, class U = std::allocator<T> > class TT
7021 The template parameter level of T and U are one level larger than
7022 of TT. To proper process the default argument of U, say when an
7023 instantiation `TT<int>' is seen, we need to build the full
7024 arguments containing {int} as the innermost level. Outer levels,
7025 available when not appearing as default template argument, can be
7026 obtained from the arguments of the enclosing template.
7028 Suppose that TT is later substituted with std::vector. The above
7029 instantiation is `TT<int, std::allocator<T> >' with TT at
7030 level 1, and T at level 2, while the template arguments at level 1
7031 becomes {std::vector} and the inner level 2 is {int}. */
7033 tree outer = DECL_CONTEXT (templ);
7034 if (outer)
7036 if (DECL_TEMPLATE_SPECIALIZATION (outer))
7037 /* We want arguments for the partial specialization, not arguments for
7038 the primary template. */
7039 outer = template_parms_to_args (DECL_TEMPLATE_PARMS (outer));
7040 else
7041 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7043 else if (current_template_parms)
7045 /* This is an argument of the current template, so we haven't set
7046 DECL_CONTEXT yet. */
7047 tree relevant_template_parms;
7049 /* Parameter levels that are greater than the level of the given
7050 template template parm are irrelevant. */
7051 relevant_template_parms = current_template_parms;
7052 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7053 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7054 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7056 outer = template_parms_to_args (relevant_template_parms);
7059 if (outer)
7060 arglist = add_to_template_args (outer, arglist);
7062 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7063 return coerce_template_parms (parmlist, arglist, templ,
7064 complain,
7065 /*require_all_args=*/true,
7066 /*use_default_args=*/true);
7069 /* A cache of template template parameters with match-all default
7070 arguments. */
7071 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7072 static void
7073 store_defaulted_ttp (tree v, tree t)
7075 if (!defaulted_ttp_cache)
7076 defaulted_ttp_cache = hash_map<tree,tree>::create_ggc (13);
7077 defaulted_ttp_cache->put (v, t);
7079 static tree
7080 lookup_defaulted_ttp (tree v)
7082 if (defaulted_ttp_cache)
7083 if (tree *p = defaulted_ttp_cache->get (v))
7084 return *p;
7085 return NULL_TREE;
7088 /* T is a bound template template-parameter. Copy its arguments into default
7089 arguments of the template template-parameter's template parameters. */
7091 static tree
7092 add_defaults_to_ttp (tree otmpl)
7094 if (tree c = lookup_defaulted_ttp (otmpl))
7095 return c;
7097 tree ntmpl = copy_node (otmpl);
7099 tree ntype = copy_node (TREE_TYPE (otmpl));
7100 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7101 TYPE_MAIN_VARIANT (ntype) = ntype;
7102 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7103 TYPE_NAME (ntype) = ntmpl;
7104 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7106 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7107 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7108 TEMPLATE_PARM_DECL (idx) = ntmpl;
7109 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7111 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7112 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7113 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7114 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7115 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7117 tree o = TREE_VEC_ELT (vec, i);
7118 if (!template_parameter_pack_p (TREE_VALUE (o)))
7120 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7121 TREE_PURPOSE (n) = any_targ_node;
7125 store_defaulted_ttp (otmpl, ntmpl);
7126 return ntmpl;
7129 /* ARG is a bound potential template template-argument, and PARGS is a list
7130 of arguments for the corresponding template template-parameter. Adjust
7131 PARGS as appropriate for application to ARG's template, and if ARG is a
7132 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7133 arguments to the template template parameter. */
7135 static tree
7136 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7138 ++processing_template_decl;
7139 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7140 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7142 /* When comparing two template template-parameters in partial ordering,
7143 rewrite the one currently being used as an argument to have default
7144 arguments for all parameters. */
7145 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7146 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7147 if (pargs != error_mark_node)
7148 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7149 TYPE_TI_ARGS (arg));
7151 else
7153 tree aparms
7154 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7155 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7156 /*require_all*/true,
7157 /*use_default*/true);
7159 --processing_template_decl;
7160 return pargs;
7163 /* Subroutine of unify for the case when PARM is a
7164 BOUND_TEMPLATE_TEMPLATE_PARM. */
7166 static int
7167 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7168 bool explain_p)
7170 tree parmvec = TYPE_TI_ARGS (parm);
7171 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7173 /* The template template parm might be variadic and the argument
7174 not, so flatten both argument lists. */
7175 parmvec = expand_template_argument_pack (parmvec);
7176 argvec = expand_template_argument_pack (argvec);
7178 tree nparmvec = parmvec;
7179 if (flag_new_ttp)
7181 /* In keeping with P0522R0, adjust P's template arguments
7182 to apply to A's template; then flatten it again. */
7183 nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7184 nparmvec = expand_template_argument_pack (nparmvec);
7187 if (unify (tparms, targs, nparmvec, argvec,
7188 UNIFY_ALLOW_NONE, explain_p))
7189 return 1;
7191 /* If the P0522 adjustment eliminated a pack expansion, deduce
7192 empty packs. */
7193 if (flag_new_ttp
7194 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7195 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7196 DEDUCE_EXACT, /*sub*/true, explain_p))
7197 return 1;
7199 return 0;
7202 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7203 template template parameters. Both PARM_PARMS and ARG_PARMS are
7204 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7205 or PARM_DECL.
7207 Consider the example:
7208 template <class T> class A;
7209 template<template <class U> class TT> class B;
7211 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7212 the parameters to A, and OUTER_ARGS contains A. */
7214 static int
7215 coerce_template_template_parms (tree parm_parms,
7216 tree arg_parms,
7217 tsubst_flags_t complain,
7218 tree in_decl,
7219 tree outer_args)
7221 int nparms, nargs, i;
7222 tree parm, arg;
7223 int variadic_p = 0;
7225 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7226 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7228 nparms = TREE_VEC_LENGTH (parm_parms);
7229 nargs = TREE_VEC_LENGTH (arg_parms);
7231 if (flag_new_ttp)
7233 /* P0522R0: A template template-parameter P is at least as specialized as
7234 a template template-argument A if, given the following rewrite to two
7235 function templates, the function template corresponding to P is at
7236 least as specialized as the function template corresponding to A
7237 according to the partial ordering rules for function templates
7238 ([temp.func.order]). Given an invented class template X with the
7239 template parameter list of A (including default arguments):
7241 * Each of the two function templates has the same template parameters,
7242 respectively, as P or A.
7244 * Each function template has a single function parameter whose type is
7245 a specialization of X with template arguments corresponding to the
7246 template parameters from the respective function template where, for
7247 each template parameter PP in the template parameter list of the
7248 function template, a corresponding template argument AA is formed. If
7249 PP declares a parameter pack, then AA is the pack expansion
7250 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7252 If the rewrite produces an invalid type, then P is not at least as
7253 specialized as A. */
7255 /* So coerce P's args to apply to A's parms, and then deduce between A's
7256 args and the converted args. If that succeeds, A is at least as
7257 specialized as P, so they match.*/
7258 tree pargs = template_parms_level_to_args (parm_parms);
7259 ++processing_template_decl;
7260 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7261 /*require_all*/true, /*use_default*/true);
7262 --processing_template_decl;
7263 if (pargs != error_mark_node)
7265 tree targs = make_tree_vec (nargs);
7266 tree aargs = template_parms_level_to_args (arg_parms);
7267 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7268 /*explain*/false))
7269 return 1;
7273 /* Determine whether we have a parameter pack at the end of the
7274 template template parameter's template parameter list. */
7275 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7277 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7279 if (error_operand_p (parm))
7280 return 0;
7282 switch (TREE_CODE (parm))
7284 case TEMPLATE_DECL:
7285 case TYPE_DECL:
7286 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7287 variadic_p = 1;
7288 break;
7290 case PARM_DECL:
7291 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7292 variadic_p = 1;
7293 break;
7295 default:
7296 gcc_unreachable ();
7300 if (nargs != nparms
7301 && !(variadic_p && nargs >= nparms - 1))
7302 return 0;
7304 /* Check all of the template parameters except the parameter pack at
7305 the end (if any). */
7306 for (i = 0; i < nparms - variadic_p; ++i)
7308 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7309 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7310 continue;
7312 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7313 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7315 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7316 outer_args))
7317 return 0;
7321 if (variadic_p)
7323 /* Check each of the template parameters in the template
7324 argument against the template parameter pack at the end of
7325 the template template parameter. */
7326 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7327 return 0;
7329 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7331 for (; i < nargs; ++i)
7333 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7334 continue;
7336 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7338 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7339 outer_args))
7340 return 0;
7344 return 1;
7347 /* Verifies that the deduced template arguments (in TARGS) for the
7348 template template parameters (in TPARMS) represent valid bindings,
7349 by comparing the template parameter list of each template argument
7350 to the template parameter list of its corresponding template
7351 template parameter, in accordance with DR150. This
7352 routine can only be called after all template arguments have been
7353 deduced. It will return TRUE if all of the template template
7354 parameter bindings are okay, FALSE otherwise. */
7355 bool
7356 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7358 int i, ntparms = TREE_VEC_LENGTH (tparms);
7359 bool ret = true;
7361 /* We're dealing with template parms in this process. */
7362 ++processing_template_decl;
7364 targs = INNERMOST_TEMPLATE_ARGS (targs);
7366 for (i = 0; i < ntparms; ++i)
7368 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7369 tree targ = TREE_VEC_ELT (targs, i);
7371 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7373 tree packed_args = NULL_TREE;
7374 int idx, len = 1;
7376 if (ARGUMENT_PACK_P (targ))
7378 /* Look inside the argument pack. */
7379 packed_args = ARGUMENT_PACK_ARGS (targ);
7380 len = TREE_VEC_LENGTH (packed_args);
7383 for (idx = 0; idx < len; ++idx)
7385 tree targ_parms = NULL_TREE;
7387 if (packed_args)
7388 /* Extract the next argument from the argument
7389 pack. */
7390 targ = TREE_VEC_ELT (packed_args, idx);
7392 if (PACK_EXPANSION_P (targ))
7393 /* Look at the pattern of the pack expansion. */
7394 targ = PACK_EXPANSION_PATTERN (targ);
7396 /* Extract the template parameters from the template
7397 argument. */
7398 if (TREE_CODE (targ) == TEMPLATE_DECL)
7399 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7400 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7401 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7403 /* Verify that we can coerce the template template
7404 parameters from the template argument to the template
7405 parameter. This requires an exact match. */
7406 if (targ_parms
7407 && !coerce_template_template_parms
7408 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7409 targ_parms,
7410 tf_none,
7411 tparm,
7412 targs))
7414 ret = false;
7415 goto out;
7421 out:
7423 --processing_template_decl;
7424 return ret;
7427 /* Since type attributes aren't mangled, we need to strip them from
7428 template type arguments. */
7430 static tree
7431 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7433 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7434 return arg;
7435 bool removed_attributes = false;
7436 tree canon = strip_typedefs (arg, &removed_attributes);
7437 if (removed_attributes
7438 && (complain & tf_warning))
7439 warning (OPT_Wignored_attributes,
7440 "ignoring attributes on template argument %qT", arg);
7441 return canon;
7444 /* And from inside dependent non-type arguments like sizeof(Type). */
7446 static tree
7447 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7449 if (!arg || arg == error_mark_node)
7450 return arg;
7451 bool removed_attributes = false;
7452 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7453 if (removed_attributes
7454 && (complain & tf_warning))
7455 warning (OPT_Wignored_attributes,
7456 "ignoring attributes in template argument %qE", arg);
7457 return canon;
7460 // A template declaration can be substituted for a constrained
7461 // template template parameter only when the argument is more
7462 // constrained than the parameter.
7463 static bool
7464 is_compatible_template_arg (tree parm, tree arg)
7466 tree parm_cons = get_constraints (parm);
7468 /* For now, allow constrained template template arguments
7469 and unconstrained template template parameters. */
7470 if (parm_cons == NULL_TREE)
7471 return true;
7473 tree arg_cons = get_constraints (arg);
7475 // If the template parameter is constrained, we need to rewrite its
7476 // constraints in terms of the ARG's template parameters. This ensures
7477 // that all of the template parameter types will have the same depth.
7479 // Note that this is only valid when coerce_template_template_parm is
7480 // true for the innermost template parameters of PARM and ARG. In other
7481 // words, because coercion is successful, this conversion will be valid.
7482 if (parm_cons)
7484 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7485 parm_cons = tsubst_constraint_info (parm_cons,
7486 INNERMOST_TEMPLATE_ARGS (args),
7487 tf_none, NULL_TREE);
7488 if (parm_cons == error_mark_node)
7489 return false;
7492 return subsumes (parm_cons, arg_cons);
7495 // Convert a placeholder argument into a binding to the original
7496 // parameter. The original parameter is saved as the TREE_TYPE of
7497 // ARG.
7498 static inline tree
7499 convert_wildcard_argument (tree parm, tree arg)
7501 TREE_TYPE (arg) = parm;
7502 return arg;
7505 /* Convert the indicated template ARG as necessary to match the
7506 indicated template PARM. Returns the converted ARG, or
7507 error_mark_node if the conversion was unsuccessful. Error and
7508 warning messages are issued under control of COMPLAIN. This
7509 conversion is for the Ith parameter in the parameter list. ARGS is
7510 the full set of template arguments deduced so far. */
7512 static tree
7513 convert_template_argument (tree parm,
7514 tree arg,
7515 tree args,
7516 tsubst_flags_t complain,
7517 int i,
7518 tree in_decl)
7520 tree orig_arg;
7521 tree val;
7522 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7524 if (parm == error_mark_node)
7525 return error_mark_node;
7527 /* Trivially convert placeholders. */
7528 if (TREE_CODE (arg) == WILDCARD_DECL)
7529 return convert_wildcard_argument (parm, arg);
7531 if (arg == any_targ_node)
7532 return arg;
7534 if (TREE_CODE (arg) == TREE_LIST
7535 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7537 /* The template argument was the name of some
7538 member function. That's usually
7539 invalid, but static members are OK. In any
7540 case, grab the underlying fields/functions
7541 and issue an error later if required. */
7542 orig_arg = TREE_VALUE (arg);
7543 TREE_TYPE (arg) = unknown_type_node;
7546 orig_arg = arg;
7548 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7549 requires_type = (TREE_CODE (parm) == TYPE_DECL
7550 || requires_tmpl_type);
7552 /* When determining whether an argument pack expansion is a template,
7553 look at the pattern. */
7554 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7555 arg = PACK_EXPANSION_PATTERN (arg);
7557 /* Deal with an injected-class-name used as a template template arg. */
7558 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7560 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7561 if (TREE_CODE (t) == TEMPLATE_DECL)
7563 if (cxx_dialect >= cxx11)
7564 /* OK under DR 1004. */;
7565 else if (complain & tf_warning_or_error)
7566 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7567 " used as template template argument", TYPE_NAME (arg));
7568 else if (flag_pedantic_errors)
7569 t = arg;
7571 arg = t;
7575 is_tmpl_type =
7576 ((TREE_CODE (arg) == TEMPLATE_DECL
7577 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7578 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7579 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7580 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7582 if (is_tmpl_type
7583 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7584 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7585 arg = TYPE_STUB_DECL (arg);
7587 is_type = TYPE_P (arg) || is_tmpl_type;
7589 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7590 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7592 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7594 if (complain & tf_error)
7595 error ("invalid use of destructor %qE as a type", orig_arg);
7596 return error_mark_node;
7599 permerror (input_location,
7600 "to refer to a type member of a template parameter, "
7601 "use %<typename %E%>", orig_arg);
7603 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7604 TREE_OPERAND (arg, 1),
7605 typename_type,
7606 complain);
7607 arg = orig_arg;
7608 is_type = 1;
7610 if (is_type != requires_type)
7612 if (in_decl)
7614 if (complain & tf_error)
7616 error ("type/value mismatch at argument %d in template "
7617 "parameter list for %qD",
7618 i + 1, in_decl);
7619 if (is_type)
7620 inform (input_location,
7621 " expected a constant of type %qT, got %qT",
7622 TREE_TYPE (parm),
7623 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7624 else if (requires_tmpl_type)
7625 inform (input_location,
7626 " expected a class template, got %qE", orig_arg);
7627 else
7628 inform (input_location,
7629 " expected a type, got %qE", orig_arg);
7632 return error_mark_node;
7634 if (is_tmpl_type ^ requires_tmpl_type)
7636 if (in_decl && (complain & tf_error))
7638 error ("type/value mismatch at argument %d in template "
7639 "parameter list for %qD",
7640 i + 1, in_decl);
7641 if (is_tmpl_type)
7642 inform (input_location,
7643 " expected a type, got %qT", DECL_NAME (arg));
7644 else
7645 inform (input_location,
7646 " expected a class template, got %qT", orig_arg);
7648 return error_mark_node;
7651 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7652 /* We already did the appropriate conversion when packing args. */
7653 val = orig_arg;
7654 else if (is_type)
7656 if (requires_tmpl_type)
7658 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7659 /* The number of argument required is not known yet.
7660 Just accept it for now. */
7661 val = TREE_TYPE (arg);
7662 else
7664 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7665 tree argparm;
7667 /* Strip alias templates that are equivalent to another
7668 template. */
7669 arg = get_underlying_template (arg);
7670 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7672 if (coerce_template_template_parms (parmparm, argparm,
7673 complain, in_decl,
7674 args))
7676 val = arg;
7678 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7679 TEMPLATE_DECL. */
7680 if (val != error_mark_node)
7682 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
7683 val = TREE_TYPE (val);
7684 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
7685 val = make_pack_expansion (val);
7688 else
7690 if (in_decl && (complain & tf_error))
7692 error ("type/value mismatch at argument %d in "
7693 "template parameter list for %qD",
7694 i + 1, in_decl);
7695 inform (input_location,
7696 " expected a template of type %qD, got %qT",
7697 parm, orig_arg);
7700 val = error_mark_node;
7703 // Check that the constraints are compatible before allowing the
7704 // substitution.
7705 if (val != error_mark_node)
7706 if (!is_compatible_template_arg (parm, arg))
7708 if (in_decl && (complain & tf_error))
7710 error ("constraint mismatch at argument %d in "
7711 "template parameter list for %qD",
7712 i + 1, in_decl);
7713 inform (input_location, " expected %qD but got %qD",
7714 parm, arg);
7716 val = error_mark_node;
7720 else
7721 val = orig_arg;
7722 /* We only form one instance of each template specialization.
7723 Therefore, if we use a non-canonical variant (i.e., a
7724 typedef), any future messages referring to the type will use
7725 the typedef, which is confusing if those future uses do not
7726 themselves also use the typedef. */
7727 if (TYPE_P (val))
7728 val = canonicalize_type_argument (val, complain);
7730 else
7732 tree t = TREE_TYPE (parm);
7734 if (tree a = type_uses_auto (t))
7736 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
7737 if (t == error_mark_node)
7738 return error_mark_node;
7740 else
7741 t = tsubst (t, args, complain, in_decl);
7743 if (invalid_nontype_parm_type_p (t, complain))
7744 return error_mark_node;
7746 if (!type_dependent_expression_p (orig_arg)
7747 && !uses_template_parms (t))
7748 /* We used to call digest_init here. However, digest_init
7749 will report errors, which we don't want when complain
7750 is zero. More importantly, digest_init will try too
7751 hard to convert things: for example, `0' should not be
7752 converted to pointer type at this point according to
7753 the standard. Accepting this is not merely an
7754 extension, since deciding whether or not these
7755 conversions can occur is part of determining which
7756 function template to call, or whether a given explicit
7757 argument specification is valid. */
7758 val = convert_nontype_argument (t, orig_arg, complain);
7759 else
7760 val = canonicalize_expr_argument (orig_arg, complain);
7762 if (val == NULL_TREE)
7763 val = error_mark_node;
7764 else if (val == error_mark_node && (complain & tf_error))
7765 error ("could not convert template argument %qE from %qT to %qT",
7766 orig_arg, TREE_TYPE (orig_arg), t);
7768 if (INDIRECT_REF_P (val))
7770 /* Reject template arguments that are references to built-in
7771 functions with no library fallbacks. */
7772 const_tree inner = TREE_OPERAND (val, 0);
7773 const_tree innertype = TREE_TYPE (inner);
7774 if (innertype
7775 && TREE_CODE (innertype) == REFERENCE_TYPE
7776 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
7777 && 0 < TREE_OPERAND_LENGTH (inner)
7778 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
7779 return error_mark_node;
7782 if (TREE_CODE (val) == SCOPE_REF)
7784 /* Strip typedefs from the SCOPE_REF. */
7785 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
7786 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
7787 complain);
7788 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
7789 QUALIFIED_NAME_IS_TEMPLATE (val));
7793 return val;
7796 /* Coerces the remaining template arguments in INNER_ARGS (from
7797 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
7798 Returns the coerced argument pack. PARM_IDX is the position of this
7799 parameter in the template parameter list. ARGS is the original
7800 template argument list. */
7801 static tree
7802 coerce_template_parameter_pack (tree parms,
7803 int parm_idx,
7804 tree args,
7805 tree inner_args,
7806 int arg_idx,
7807 tree new_args,
7808 int* lost,
7809 tree in_decl,
7810 tsubst_flags_t complain)
7812 tree parm = TREE_VEC_ELT (parms, parm_idx);
7813 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
7814 tree packed_args;
7815 tree argument_pack;
7816 tree packed_parms = NULL_TREE;
7818 if (arg_idx > nargs)
7819 arg_idx = nargs;
7821 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
7823 /* When the template parameter is a non-type template parameter pack
7824 or template template parameter pack whose type or template
7825 parameters use parameter packs, we know exactly how many arguments
7826 we are looking for. Build a vector of the instantiated decls for
7827 these template parameters in PACKED_PARMS. */
7828 /* We can't use make_pack_expansion here because it would interpret a
7829 _DECL as a use rather than a declaration. */
7830 tree decl = TREE_VALUE (parm);
7831 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
7832 SET_PACK_EXPANSION_PATTERN (exp, decl);
7833 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
7834 SET_TYPE_STRUCTURAL_EQUALITY (exp);
7836 TREE_VEC_LENGTH (args)--;
7837 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
7838 TREE_VEC_LENGTH (args)++;
7840 if (packed_parms == error_mark_node)
7841 return error_mark_node;
7843 /* If we're doing a partial instantiation of a member template,
7844 verify that all of the types used for the non-type
7845 template parameter pack are, in fact, valid for non-type
7846 template parameters. */
7847 if (arg_idx < nargs
7848 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
7850 int j, len = TREE_VEC_LENGTH (packed_parms);
7851 for (j = 0; j < len; ++j)
7853 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
7854 if (invalid_nontype_parm_type_p (t, complain))
7855 return error_mark_node;
7857 /* We don't know how many args we have yet, just
7858 use the unconverted ones for now. */
7859 return NULL_TREE;
7862 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
7864 /* Check if we have a placeholder pack, which indicates we're
7865 in the context of a introduction list. In that case we want
7866 to match this pack to the single placeholder. */
7867 else if (arg_idx < nargs
7868 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
7869 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
7871 nargs = arg_idx + 1;
7872 packed_args = make_tree_vec (1);
7874 else
7875 packed_args = make_tree_vec (nargs - arg_idx);
7877 /* Convert the remaining arguments, which will be a part of the
7878 parameter pack "parm". */
7879 int first_pack_arg = arg_idx;
7880 for (; arg_idx < nargs; ++arg_idx)
7882 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
7883 tree actual_parm = TREE_VALUE (parm);
7884 int pack_idx = arg_idx - first_pack_arg;
7886 if (packed_parms)
7888 /* Once we've packed as many args as we have types, stop. */
7889 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
7890 break;
7891 else if (PACK_EXPANSION_P (arg))
7892 /* We don't know how many args we have yet, just
7893 use the unconverted ones for now. */
7894 return NULL_TREE;
7895 else
7896 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
7899 if (arg == error_mark_node)
7901 if (complain & tf_error)
7902 error ("template argument %d is invalid", arg_idx + 1);
7904 else
7905 arg = convert_template_argument (actual_parm,
7906 arg, new_args, complain, parm_idx,
7907 in_decl);
7908 if (arg == error_mark_node)
7909 (*lost)++;
7910 TREE_VEC_ELT (packed_args, pack_idx) = arg;
7913 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
7914 && TREE_VEC_LENGTH (packed_args) > 0)
7916 if (complain & tf_error)
7917 error ("wrong number of template arguments (%d, should be %d)",
7918 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
7919 return error_mark_node;
7922 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
7923 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
7924 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
7925 else
7927 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
7928 TREE_CONSTANT (argument_pack) = 1;
7931 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
7932 if (CHECKING_P)
7933 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
7934 TREE_VEC_LENGTH (packed_args));
7935 return argument_pack;
7938 /* Returns the number of pack expansions in the template argument vector
7939 ARGS. */
7941 static int
7942 pack_expansion_args_count (tree args)
7944 int i;
7945 int count = 0;
7946 if (args)
7947 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
7949 tree elt = TREE_VEC_ELT (args, i);
7950 if (elt && PACK_EXPANSION_P (elt))
7951 ++count;
7953 return count;
7956 /* Convert all template arguments to their appropriate types, and
7957 return a vector containing the innermost resulting template
7958 arguments. If any error occurs, return error_mark_node. Error and
7959 warning messages are issued under control of COMPLAIN.
7961 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
7962 for arguments not specified in ARGS. Otherwise, if
7963 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
7964 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
7965 USE_DEFAULT_ARGS is false, then all arguments must be specified in
7966 ARGS. */
7968 static tree
7969 coerce_template_parms (tree parms,
7970 tree args,
7971 tree in_decl,
7972 tsubst_flags_t complain,
7973 bool require_all_args,
7974 bool use_default_args)
7976 int nparms, nargs, parm_idx, arg_idx, lost = 0;
7977 tree orig_inner_args;
7978 tree inner_args;
7979 tree new_args;
7980 tree new_inner_args;
7981 int saved_unevaluated_operand;
7982 int saved_inhibit_evaluation_warnings;
7984 /* When used as a boolean value, indicates whether this is a
7985 variadic template parameter list. Since it's an int, we can also
7986 subtract it from nparms to get the number of non-variadic
7987 parameters. */
7988 int variadic_p = 0;
7989 int variadic_args_p = 0;
7990 int post_variadic_parms = 0;
7992 /* Likewise for parameters with default arguments. */
7993 int default_p = 0;
7995 if (args == error_mark_node)
7996 return error_mark_node;
7998 nparms = TREE_VEC_LENGTH (parms);
8000 /* Determine if there are any parameter packs or default arguments. */
8001 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8003 tree parm = TREE_VEC_ELT (parms, parm_idx);
8004 if (variadic_p)
8005 ++post_variadic_parms;
8006 if (template_parameter_pack_p (TREE_VALUE (parm)))
8007 ++variadic_p;
8008 if (TREE_PURPOSE (parm))
8009 ++default_p;
8012 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8013 /* If there are no parameters that follow a parameter pack, we need to
8014 expand any argument packs so that we can deduce a parameter pack from
8015 some non-packed args followed by an argument pack, as in variadic85.C.
8016 If there are such parameters, we need to leave argument packs intact
8017 so the arguments are assigned properly. This can happen when dealing
8018 with a nested class inside a partial specialization of a class
8019 template, as in variadic92.C, or when deducing a template parameter pack
8020 from a sub-declarator, as in variadic114.C. */
8021 if (!post_variadic_parms)
8022 inner_args = expand_template_argument_pack (inner_args);
8024 /* Count any pack expansion args. */
8025 variadic_args_p = pack_expansion_args_count (inner_args);
8027 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8028 if ((nargs - variadic_args_p > nparms && !variadic_p)
8029 || (nargs < nparms - variadic_p
8030 && require_all_args
8031 && !variadic_args_p
8032 && (!use_default_args
8033 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8034 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8036 if (complain & tf_error)
8038 if (variadic_p || default_p)
8040 nparms -= variadic_p + default_p;
8041 error ("wrong number of template arguments "
8042 "(%d, should be at least %d)", nargs, nparms);
8044 else
8045 error ("wrong number of template arguments "
8046 "(%d, should be %d)", nargs, nparms);
8048 if (in_decl)
8049 inform (DECL_SOURCE_LOCATION (in_decl),
8050 "provided for %qD", in_decl);
8053 return error_mark_node;
8055 /* We can't pass a pack expansion to a non-pack parameter of an alias
8056 template (DR 1430). */
8057 else if (in_decl
8058 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8059 || concept_template_p (in_decl))
8060 && variadic_args_p
8061 && nargs - variadic_args_p < nparms - variadic_p)
8063 if (complain & tf_error)
8065 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8067 tree arg = TREE_VEC_ELT (inner_args, i);
8068 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8070 if (PACK_EXPANSION_P (arg)
8071 && !template_parameter_pack_p (parm))
8073 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8074 error_at (location_of (arg),
8075 "pack expansion argument for non-pack parameter "
8076 "%qD of alias template %qD", parm, in_decl);
8077 else
8078 error_at (location_of (arg),
8079 "pack expansion argument for non-pack parameter "
8080 "%qD of concept %qD", parm, in_decl);
8081 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8082 goto found;
8085 gcc_unreachable ();
8086 found:;
8088 return error_mark_node;
8091 /* We need to evaluate the template arguments, even though this
8092 template-id may be nested within a "sizeof". */
8093 saved_unevaluated_operand = cp_unevaluated_operand;
8094 cp_unevaluated_operand = 0;
8095 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8096 c_inhibit_evaluation_warnings = 0;
8097 new_inner_args = make_tree_vec (nparms);
8098 new_args = add_outermost_template_args (args, new_inner_args);
8099 int pack_adjust = 0;
8100 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8102 tree arg;
8103 tree parm;
8105 /* Get the Ith template parameter. */
8106 parm = TREE_VEC_ELT (parms, parm_idx);
8108 if (parm == error_mark_node)
8110 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8111 continue;
8114 /* Calculate the next argument. */
8115 if (arg_idx < nargs)
8116 arg = TREE_VEC_ELT (inner_args, arg_idx);
8117 else
8118 arg = NULL_TREE;
8120 if (template_parameter_pack_p (TREE_VALUE (parm))
8121 && !(arg && ARGUMENT_PACK_P (arg)))
8123 /* Some arguments will be placed in the
8124 template parameter pack PARM. */
8125 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8126 inner_args, arg_idx,
8127 new_args, &lost,
8128 in_decl, complain);
8130 if (arg == NULL_TREE)
8132 /* We don't know how many args we have yet, just use the
8133 unconverted (and still packed) ones for now. */
8134 new_inner_args = orig_inner_args;
8135 arg_idx = nargs;
8136 break;
8139 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8141 /* Store this argument. */
8142 if (arg == error_mark_node)
8144 lost++;
8145 /* We are done with all of the arguments. */
8146 arg_idx = nargs;
8148 else
8150 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8151 arg_idx += pack_adjust;
8154 continue;
8156 else if (arg)
8158 if (PACK_EXPANSION_P (arg))
8160 /* "If every valid specialization of a variadic template
8161 requires an empty template parameter pack, the template is
8162 ill-formed, no diagnostic required." So check that the
8163 pattern works with this parameter. */
8164 tree pattern = PACK_EXPANSION_PATTERN (arg);
8165 tree conv = convert_template_argument (TREE_VALUE (parm),
8166 pattern, new_args,
8167 complain, parm_idx,
8168 in_decl);
8169 if (conv == error_mark_node)
8171 if (complain & tf_error)
8172 inform (input_location, "so any instantiation with a "
8173 "non-empty parameter pack would be ill-formed");
8174 ++lost;
8176 else if (TYPE_P (conv) && !TYPE_P (pattern))
8177 /* Recover from missing typename. */
8178 TREE_VEC_ELT (inner_args, arg_idx)
8179 = make_pack_expansion (conv);
8181 /* We don't know how many args we have yet, just
8182 use the unconverted ones for now. */
8183 new_inner_args = inner_args;
8184 arg_idx = nargs;
8185 break;
8188 else if (require_all_args)
8190 /* There must be a default arg in this case. */
8191 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8192 complain, in_decl);
8193 /* The position of the first default template argument,
8194 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8195 Record that. */
8196 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8197 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8198 arg_idx - pack_adjust);
8200 else
8201 break;
8203 if (arg == error_mark_node)
8205 if (complain & tf_error)
8206 error ("template argument %d is invalid", arg_idx + 1);
8208 else if (!arg)
8209 /* This only occurs if there was an error in the template
8210 parameter list itself (which we would already have
8211 reported) that we are trying to recover from, e.g., a class
8212 template with a parameter list such as
8213 template<typename..., typename>. */
8214 ++lost;
8215 else
8216 arg = convert_template_argument (TREE_VALUE (parm),
8217 arg, new_args, complain,
8218 parm_idx, in_decl);
8220 if (arg == error_mark_node)
8221 lost++;
8222 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8224 cp_unevaluated_operand = saved_unevaluated_operand;
8225 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8227 if (variadic_p && arg_idx < nargs)
8229 if (complain & tf_error)
8231 error ("wrong number of template arguments "
8232 "(%d, should be %d)", nargs, arg_idx);
8233 if (in_decl)
8234 error ("provided for %q+D", in_decl);
8236 return error_mark_node;
8239 if (lost)
8240 return error_mark_node;
8242 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8243 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8244 TREE_VEC_LENGTH (new_inner_args));
8246 return new_inner_args;
8249 /* Convert all template arguments to their appropriate types, and
8250 return a vector containing the innermost resulting template
8251 arguments. If any error occurs, return error_mark_node. Error and
8252 warning messages are not issued.
8254 Note that no function argument deduction is performed, and default
8255 arguments are used to fill in unspecified arguments. */
8256 tree
8257 coerce_template_parms (tree parms, tree args, tree in_decl)
8259 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8262 /* Convert all template arguments to their appropriate type, and
8263 instantiate default arguments as needed. This returns a vector
8264 containing the innermost resulting template arguments, or
8265 error_mark_node if unsuccessful. */
8266 tree
8267 coerce_template_parms (tree parms, tree args, tree in_decl,
8268 tsubst_flags_t complain)
8270 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8273 /* Like coerce_template_parms. If PARMS represents all template
8274 parameters levels, this function returns a vector of vectors
8275 representing all the resulting argument levels. Note that in this
8276 case, only the innermost arguments are coerced because the
8277 outermost ones are supposed to have been coerced already.
8279 Otherwise, if PARMS represents only (the innermost) vector of
8280 parameters, this function returns a vector containing just the
8281 innermost resulting arguments. */
8283 static tree
8284 coerce_innermost_template_parms (tree parms,
8285 tree args,
8286 tree in_decl,
8287 tsubst_flags_t complain,
8288 bool require_all_args,
8289 bool use_default_args)
8291 int parms_depth = TMPL_PARMS_DEPTH (parms);
8292 int args_depth = TMPL_ARGS_DEPTH (args);
8293 tree coerced_args;
8295 if (parms_depth > 1)
8297 coerced_args = make_tree_vec (parms_depth);
8298 tree level;
8299 int cur_depth;
8301 for (level = parms, cur_depth = parms_depth;
8302 parms_depth > 0 && level != NULL_TREE;
8303 level = TREE_CHAIN (level), --cur_depth)
8305 tree l;
8306 if (cur_depth == args_depth)
8307 l = coerce_template_parms (TREE_VALUE (level),
8308 args, in_decl, complain,
8309 require_all_args,
8310 use_default_args);
8311 else
8312 l = TMPL_ARGS_LEVEL (args, cur_depth);
8314 if (l == error_mark_node)
8315 return error_mark_node;
8317 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8320 else
8321 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8322 args, in_decl, complain,
8323 require_all_args,
8324 use_default_args);
8325 return coerced_args;
8328 /* Returns 1 if template args OT and NT are equivalent. */
8331 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
8333 if (nt == ot)
8334 return 1;
8335 if (nt == NULL_TREE || ot == NULL_TREE)
8336 return false;
8337 if (nt == any_targ_node || ot == any_targ_node)
8338 return true;
8340 if (TREE_CODE (nt) == TREE_VEC)
8341 /* For member templates */
8342 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8343 else if (PACK_EXPANSION_P (ot))
8344 return (PACK_EXPANSION_P (nt)
8345 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8346 PACK_EXPANSION_PATTERN (nt))
8347 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8348 PACK_EXPANSION_EXTRA_ARGS (nt)));
8349 else if (ARGUMENT_PACK_P (ot))
8351 int i, len;
8352 tree opack, npack;
8354 if (!ARGUMENT_PACK_P (nt))
8355 return 0;
8357 opack = ARGUMENT_PACK_ARGS (ot);
8358 npack = ARGUMENT_PACK_ARGS (nt);
8359 len = TREE_VEC_LENGTH (opack);
8360 if (TREE_VEC_LENGTH (npack) != len)
8361 return 0;
8362 for (i = 0; i < len; ++i)
8363 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8364 TREE_VEC_ELT (npack, i)))
8365 return 0;
8366 return 1;
8368 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8369 gcc_unreachable ();
8370 else if (TYPE_P (nt))
8372 if (!TYPE_P (ot))
8373 return false;
8374 /* Don't treat an alias template specialization with dependent
8375 arguments as equivalent to its underlying type when used as a
8376 template argument; we need them to be distinct so that we
8377 substitute into the specialization arguments at instantiation
8378 time. And aliases can't be equivalent without being ==, so
8379 we don't need to look any deeper.
8381 During partial ordering, however, we need to treat them normally so
8382 that we can order uses of the same alias with different
8383 cv-qualification (79960). */
8384 if (!partial_order
8385 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
8386 return false;
8387 else
8388 return same_type_p (ot, nt);
8390 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8391 return 0;
8392 else
8394 /* Try to treat a template non-type argument that has been converted
8395 to the parameter type as equivalent to one that hasn't yet. */
8396 for (enum tree_code code1 = TREE_CODE (ot);
8397 CONVERT_EXPR_CODE_P (code1)
8398 || code1 == NON_LVALUE_EXPR;
8399 code1 = TREE_CODE (ot))
8400 ot = TREE_OPERAND (ot, 0);
8401 for (enum tree_code code2 = TREE_CODE (nt);
8402 CONVERT_EXPR_CODE_P (code2)
8403 || code2 == NON_LVALUE_EXPR;
8404 code2 = TREE_CODE (nt))
8405 nt = TREE_OPERAND (nt, 0);
8407 return cp_tree_equal (ot, nt);
8411 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8412 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8413 NEWARG_PTR with the offending arguments if they are non-NULL. */
8416 comp_template_args (tree oldargs, tree newargs,
8417 tree *oldarg_ptr, tree *newarg_ptr,
8418 bool partial_order)
8420 int i;
8422 if (oldargs == newargs)
8423 return 1;
8425 if (!oldargs || !newargs)
8426 return 0;
8428 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
8429 return 0;
8431 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
8433 tree nt = TREE_VEC_ELT (newargs, i);
8434 tree ot = TREE_VEC_ELT (oldargs, i);
8436 if (! template_args_equal (ot, nt, partial_order))
8438 if (oldarg_ptr != NULL)
8439 *oldarg_ptr = ot;
8440 if (newarg_ptr != NULL)
8441 *newarg_ptr = nt;
8442 return 0;
8445 return 1;
8448 inline bool
8449 comp_template_args_porder (tree oargs, tree nargs)
8451 return comp_template_args (oargs, nargs, NULL, NULL, true);
8454 static void
8455 add_pending_template (tree d)
8457 tree ti = (TYPE_P (d)
8458 ? CLASSTYPE_TEMPLATE_INFO (d)
8459 : DECL_TEMPLATE_INFO (d));
8460 struct pending_template *pt;
8461 int level;
8463 if (TI_PENDING_TEMPLATE_FLAG (ti))
8464 return;
8466 /* We are called both from instantiate_decl, where we've already had a
8467 tinst_level pushed, and instantiate_template, where we haven't.
8468 Compensate. */
8469 level = !current_tinst_level || current_tinst_level->decl != d;
8471 if (level)
8472 push_tinst_level (d);
8474 pt = ggc_alloc<pending_template> ();
8475 pt->next = NULL;
8476 pt->tinst = current_tinst_level;
8477 if (last_pending_template)
8478 last_pending_template->next = pt;
8479 else
8480 pending_templates = pt;
8482 last_pending_template = pt;
8484 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
8486 if (level)
8487 pop_tinst_level ();
8491 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8492 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8493 documentation for TEMPLATE_ID_EXPR. */
8495 tree
8496 lookup_template_function (tree fns, tree arglist)
8498 tree type;
8500 if (fns == error_mark_node || arglist == error_mark_node)
8501 return error_mark_node;
8503 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
8505 if (!is_overloaded_fn (fns) && !identifier_p (fns))
8507 error ("%q#D is not a function template", fns);
8508 return error_mark_node;
8511 if (BASELINK_P (fns))
8513 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
8514 unknown_type_node,
8515 BASELINK_FUNCTIONS (fns),
8516 arglist);
8517 return fns;
8520 type = TREE_TYPE (fns);
8521 if (TREE_CODE (fns) == OVERLOAD || !type)
8522 type = unknown_type_node;
8524 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
8527 /* Within the scope of a template class S<T>, the name S gets bound
8528 (in build_self_reference) to a TYPE_DECL for the class, not a
8529 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8530 or one of its enclosing classes, and that type is a template,
8531 return the associated TEMPLATE_DECL. Otherwise, the original
8532 DECL is returned.
8534 Also handle the case when DECL is a TREE_LIST of ambiguous
8535 injected-class-names from different bases. */
8537 tree
8538 maybe_get_template_decl_from_type_decl (tree decl)
8540 if (decl == NULL_TREE)
8541 return decl;
8543 /* DR 176: A lookup that finds an injected-class-name (10.2
8544 [class.member.lookup]) can result in an ambiguity in certain cases
8545 (for example, if it is found in more than one base class). If all of
8546 the injected-class-names that are found refer to specializations of
8547 the same class template, and if the name is followed by a
8548 template-argument-list, the reference refers to the class template
8549 itself and not a specialization thereof, and is not ambiguous. */
8550 if (TREE_CODE (decl) == TREE_LIST)
8552 tree t, tmpl = NULL_TREE;
8553 for (t = decl; t; t = TREE_CHAIN (t))
8555 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
8556 if (!tmpl)
8557 tmpl = elt;
8558 else if (tmpl != elt)
8559 break;
8561 if (tmpl && t == NULL_TREE)
8562 return tmpl;
8563 else
8564 return decl;
8567 return (decl != NULL_TREE
8568 && DECL_SELF_REFERENCE_P (decl)
8569 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
8570 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
8573 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8574 parameters, find the desired type.
8576 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8578 IN_DECL, if non-NULL, is the template declaration we are trying to
8579 instantiate.
8581 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8582 the class we are looking up.
8584 Issue error and warning messages under control of COMPLAIN.
8586 If the template class is really a local class in a template
8587 function, then the FUNCTION_CONTEXT is the function in which it is
8588 being instantiated.
8590 ??? Note that this function is currently called *twice* for each
8591 template-id: the first time from the parser, while creating the
8592 incomplete type (finish_template_type), and the second type during the
8593 real instantiation (instantiate_template_class). This is surely something
8594 that we want to avoid. It also causes some problems with argument
8595 coercion (see convert_nontype_argument for more information on this). */
8597 static tree
8598 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
8599 int entering_scope, tsubst_flags_t complain)
8601 tree templ = NULL_TREE, parmlist;
8602 tree t;
8603 spec_entry **slot;
8604 spec_entry *entry;
8605 spec_entry elt;
8606 hashval_t hash;
8608 if (identifier_p (d1))
8610 tree value = innermost_non_namespace_value (d1);
8611 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
8612 templ = value;
8613 else
8615 if (context)
8616 push_decl_namespace (context);
8617 templ = lookup_name (d1);
8618 templ = maybe_get_template_decl_from_type_decl (templ);
8619 if (context)
8620 pop_decl_namespace ();
8622 if (templ)
8623 context = DECL_CONTEXT (templ);
8625 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
8627 tree type = TREE_TYPE (d1);
8629 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8630 an implicit typename for the second A. Deal with it. */
8631 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
8632 type = TREE_TYPE (type);
8634 if (CLASSTYPE_TEMPLATE_INFO (type))
8636 templ = CLASSTYPE_TI_TEMPLATE (type);
8637 d1 = DECL_NAME (templ);
8640 else if (TREE_CODE (d1) == ENUMERAL_TYPE
8641 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
8643 templ = TYPE_TI_TEMPLATE (d1);
8644 d1 = DECL_NAME (templ);
8646 else if (DECL_TYPE_TEMPLATE_P (d1))
8648 templ = d1;
8649 d1 = DECL_NAME (templ);
8650 context = DECL_CONTEXT (templ);
8652 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
8654 templ = d1;
8655 d1 = DECL_NAME (templ);
8658 /* Issue an error message if we didn't find a template. */
8659 if (! templ)
8661 if (complain & tf_error)
8662 error ("%qT is not a template", d1);
8663 return error_mark_node;
8666 if (TREE_CODE (templ) != TEMPLATE_DECL
8667 /* Make sure it's a user visible template, if it was named by
8668 the user. */
8669 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
8670 && !PRIMARY_TEMPLATE_P (templ)))
8672 if (complain & tf_error)
8674 error ("non-template type %qT used as a template", d1);
8675 if (in_decl)
8676 error ("for template declaration %q+D", in_decl);
8678 return error_mark_node;
8681 complain &= ~tf_user;
8683 /* An alias that just changes the name of a template is equivalent to the
8684 other template, so if any of the arguments are pack expansions, strip
8685 the alias to avoid problems with a pack expansion passed to a non-pack
8686 alias template parameter (DR 1430). */
8687 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
8688 templ = get_underlying_template (templ);
8690 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
8692 tree parm;
8693 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
8694 if (arglist2 == error_mark_node
8695 || (!uses_template_parms (arglist2)
8696 && check_instantiated_args (templ, arglist2, complain)))
8697 return error_mark_node;
8699 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
8700 return parm;
8702 else
8704 tree template_type = TREE_TYPE (templ);
8705 tree gen_tmpl;
8706 tree type_decl;
8707 tree found = NULL_TREE;
8708 int arg_depth;
8709 int parm_depth;
8710 int is_dependent_type;
8711 int use_partial_inst_tmpl = false;
8713 if (template_type == error_mark_node)
8714 /* An error occurred while building the template TEMPL, and a
8715 diagnostic has most certainly been emitted for that
8716 already. Let's propagate that error. */
8717 return error_mark_node;
8719 gen_tmpl = most_general_template (templ);
8720 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
8721 parm_depth = TMPL_PARMS_DEPTH (parmlist);
8722 arg_depth = TMPL_ARGS_DEPTH (arglist);
8724 if (arg_depth == 1 && parm_depth > 1)
8726 /* We've been given an incomplete set of template arguments.
8727 For example, given:
8729 template <class T> struct S1 {
8730 template <class U> struct S2 {};
8731 template <class U> struct S2<U*> {};
8734 we will be called with an ARGLIST of `U*', but the
8735 TEMPLATE will be `template <class T> template
8736 <class U> struct S1<T>::S2'. We must fill in the missing
8737 arguments. */
8738 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
8739 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
8740 arg_depth = TMPL_ARGS_DEPTH (arglist);
8743 /* Now we should have enough arguments. */
8744 gcc_assert (parm_depth == arg_depth);
8746 /* From here on, we're only interested in the most general
8747 template. */
8749 /* Calculate the BOUND_ARGS. These will be the args that are
8750 actually tsubst'd into the definition to create the
8751 instantiation. */
8752 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
8753 complain,
8754 /*require_all_args=*/true,
8755 /*use_default_args=*/true);
8757 if (arglist == error_mark_node)
8758 /* We were unable to bind the arguments. */
8759 return error_mark_node;
8761 /* In the scope of a template class, explicit references to the
8762 template class refer to the type of the template, not any
8763 instantiation of it. For example, in:
8765 template <class T> class C { void f(C<T>); }
8767 the `C<T>' is just the same as `C'. Outside of the
8768 class, however, such a reference is an instantiation. */
8769 if (entering_scope
8770 || !PRIMARY_TEMPLATE_P (gen_tmpl)
8771 || currently_open_class (template_type))
8773 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
8775 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
8776 return template_type;
8779 /* If we already have this specialization, return it. */
8780 elt.tmpl = gen_tmpl;
8781 elt.args = arglist;
8782 elt.spec = NULL_TREE;
8783 hash = spec_hasher::hash (&elt);
8784 entry = type_specializations->find_with_hash (&elt, hash);
8786 if (entry)
8787 return entry->spec;
8789 /* If the the template's constraints are not satisfied,
8790 then we cannot form a valid type.
8792 Note that the check is deferred until after the hash
8793 lookup. This prevents redundant checks on previously
8794 instantiated specializations. */
8795 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
8797 if (complain & tf_error)
8799 error ("template constraint failure");
8800 diagnose_constraints (input_location, gen_tmpl, arglist);
8802 return error_mark_node;
8805 is_dependent_type = uses_template_parms (arglist);
8807 /* If the deduced arguments are invalid, then the binding
8808 failed. */
8809 if (!is_dependent_type
8810 && check_instantiated_args (gen_tmpl,
8811 INNERMOST_TEMPLATE_ARGS (arglist),
8812 complain))
8813 return error_mark_node;
8815 if (!is_dependent_type
8816 && !PRIMARY_TEMPLATE_P (gen_tmpl)
8817 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
8818 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
8820 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
8821 DECL_NAME (gen_tmpl),
8822 /*tag_scope=*/ts_global);
8823 return found;
8826 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
8827 complain, in_decl);
8828 if (context == error_mark_node)
8829 return error_mark_node;
8831 if (!context)
8832 context = global_namespace;
8834 /* Create the type. */
8835 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8837 /* The user referred to a specialization of an alias
8838 template represented by GEN_TMPL.
8840 [temp.alias]/2 says:
8842 When a template-id refers to the specialization of an
8843 alias template, it is equivalent to the associated
8844 type obtained by substitution of its
8845 template-arguments for the template-parameters in the
8846 type-id of the alias template. */
8848 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
8849 /* Note that the call above (by indirectly calling
8850 register_specialization in tsubst_decl) registers the
8851 TYPE_DECL representing the specialization of the alias
8852 template. So next time someone substitutes ARGLIST for
8853 the template parms into the alias template (GEN_TMPL),
8854 she'll get that TYPE_DECL back. */
8856 if (t == error_mark_node)
8857 return t;
8859 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
8861 if (!is_dependent_type)
8863 set_current_access_from_decl (TYPE_NAME (template_type));
8864 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
8865 tsubst (ENUM_UNDERLYING_TYPE (template_type),
8866 arglist, complain, in_decl),
8867 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
8868 arglist, complain, in_decl),
8869 SCOPED_ENUM_P (template_type), NULL);
8871 if (t == error_mark_node)
8872 return t;
8874 else
8876 /* We don't want to call start_enum for this type, since
8877 the values for the enumeration constants may involve
8878 template parameters. And, no one should be interested
8879 in the enumeration constants for such a type. */
8880 t = cxx_make_type (ENUMERAL_TYPE);
8881 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
8883 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
8884 ENUM_FIXED_UNDERLYING_TYPE_P (t)
8885 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
8887 else if (CLASS_TYPE_P (template_type))
8889 t = make_class_type (TREE_CODE (template_type));
8890 CLASSTYPE_DECLARED_CLASS (t)
8891 = CLASSTYPE_DECLARED_CLASS (template_type);
8892 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
8894 /* A local class. Make sure the decl gets registered properly. */
8895 if (context == current_function_decl)
8896 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
8898 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
8899 /* This instantiation is another name for the primary
8900 template type. Set the TYPE_CANONICAL field
8901 appropriately. */
8902 TYPE_CANONICAL (t) = template_type;
8903 else if (any_template_arguments_need_structural_equality_p (arglist))
8904 /* Some of the template arguments require structural
8905 equality testing, so this template class requires
8906 structural equality testing. */
8907 SET_TYPE_STRUCTURAL_EQUALITY (t);
8909 else
8910 gcc_unreachable ();
8912 /* If we called start_enum or pushtag above, this information
8913 will already be set up. */
8914 if (!TYPE_NAME (t))
8916 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
8918 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
8919 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
8920 DECL_SOURCE_LOCATION (type_decl)
8921 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
8923 else
8924 type_decl = TYPE_NAME (t);
8926 if (CLASS_TYPE_P (template_type))
8928 TREE_PRIVATE (type_decl)
8929 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
8930 TREE_PROTECTED (type_decl)
8931 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
8932 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
8934 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
8935 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
8939 if (OVERLOAD_TYPE_P (t)
8940 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
8942 static const char *tags[] = {"abi_tag", "may_alias"};
8944 for (unsigned ix = 0; ix != 2; ix++)
8946 tree attributes
8947 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
8949 if (attributes)
8950 TYPE_ATTRIBUTES (t)
8951 = tree_cons (TREE_PURPOSE (attributes),
8952 TREE_VALUE (attributes),
8953 TYPE_ATTRIBUTES (t));
8957 /* Let's consider the explicit specialization of a member
8958 of a class template specialization that is implicitly instantiated,
8959 e.g.:
8960 template<class T>
8961 struct S
8963 template<class U> struct M {}; //#0
8966 template<>
8967 template<>
8968 struct S<int>::M<char> //#1
8970 int i;
8972 [temp.expl.spec]/4 says this is valid.
8974 In this case, when we write:
8975 S<int>::M<char> m;
8977 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
8978 the one of #0.
8980 When we encounter #1, we want to store the partial instantiation
8981 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
8983 For all cases other than this "explicit specialization of member of a
8984 class template", we just want to store the most general template into
8985 the CLASSTYPE_TI_TEMPLATE of M.
8987 This case of "explicit specialization of member of a class template"
8988 only happens when:
8989 1/ the enclosing class is an instantiation of, and therefore not
8990 the same as, the context of the most general template, and
8991 2/ we aren't looking at the partial instantiation itself, i.e.
8992 the innermost arguments are not the same as the innermost parms of
8993 the most general template.
8995 So it's only when 1/ and 2/ happens that we want to use the partial
8996 instantiation of the member template in lieu of its most general
8997 template. */
8999 if (PRIMARY_TEMPLATE_P (gen_tmpl)
9000 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
9001 /* the enclosing class must be an instantiation... */
9002 && CLASS_TYPE_P (context)
9003 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
9005 TREE_VEC_LENGTH (arglist)--;
9006 ++processing_template_decl;
9007 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
9008 tree partial_inst_args =
9009 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
9010 arglist, complain, NULL_TREE);
9011 --processing_template_decl;
9012 TREE_VEC_LENGTH (arglist)++;
9013 if (partial_inst_args == error_mark_node)
9014 return error_mark_node;
9015 use_partial_inst_tmpl =
9016 /*...and we must not be looking at the partial instantiation
9017 itself. */
9018 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
9019 partial_inst_args);
9022 if (!use_partial_inst_tmpl)
9023 /* This case is easy; there are no member templates involved. */
9024 found = gen_tmpl;
9025 else
9027 /* This is a full instantiation of a member template. Find
9028 the partial instantiation of which this is an instance. */
9030 /* Temporarily reduce by one the number of levels in the ARGLIST
9031 so as to avoid comparing the last set of arguments. */
9032 TREE_VEC_LENGTH (arglist)--;
9033 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
9034 TREE_VEC_LENGTH (arglist)++;
9035 /* FOUND is either a proper class type, or an alias
9036 template specialization. In the later case, it's a
9037 TYPE_DECL, resulting from the substituting of arguments
9038 for parameters in the TYPE_DECL of the alias template
9039 done earlier. So be careful while getting the template
9040 of FOUND. */
9041 found = (TREE_CODE (found) == TEMPLATE_DECL
9042 ? found
9043 : (TREE_CODE (found) == TYPE_DECL
9044 ? DECL_TI_TEMPLATE (found)
9045 : CLASSTYPE_TI_TEMPLATE (found)));
9048 // Build template info for the new specialization.
9049 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
9051 elt.spec = t;
9052 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
9053 entry = ggc_alloc<spec_entry> ();
9054 *entry = elt;
9055 *slot = entry;
9057 /* Note this use of the partial instantiation so we can check it
9058 later in maybe_process_partial_specialization. */
9059 DECL_TEMPLATE_INSTANTIATIONS (found)
9060 = tree_cons (arglist, t,
9061 DECL_TEMPLATE_INSTANTIATIONS (found));
9063 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
9064 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9065 /* Now that the type has been registered on the instantiations
9066 list, we set up the enumerators. Because the enumeration
9067 constants may involve the enumeration type itself, we make
9068 sure to register the type first, and then create the
9069 constants. That way, doing tsubst_expr for the enumeration
9070 constants won't result in recursive calls here; we'll find
9071 the instantiation and exit above. */
9072 tsubst_enum (template_type, t, arglist);
9074 if (CLASS_TYPE_P (template_type) && is_dependent_type)
9075 /* If the type makes use of template parameters, the
9076 code that generates debugging information will crash. */
9077 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
9079 /* Possibly limit visibility based on template args. */
9080 TREE_PUBLIC (type_decl) = 1;
9081 determine_visibility (type_decl);
9083 inherit_targ_abi_tags (t);
9085 return t;
9089 /* Wrapper for lookup_template_class_1. */
9091 tree
9092 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
9093 int entering_scope, tsubst_flags_t complain)
9095 tree ret;
9096 timevar_push (TV_TEMPLATE_INST);
9097 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
9098 entering_scope, complain);
9099 timevar_pop (TV_TEMPLATE_INST);
9100 return ret;
9103 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9105 tree
9106 lookup_template_variable (tree templ, tree arglist)
9108 /* The type of the expression is NULL_TREE since the template-id could refer
9109 to an explicit or partial specialization. */
9110 tree type = NULL_TREE;
9111 if (flag_concepts && variable_concept_p (templ))
9112 /* Except that concepts are always bool. */
9113 type = boolean_type_node;
9114 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
9117 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9119 tree
9120 finish_template_variable (tree var, tsubst_flags_t complain)
9122 tree templ = TREE_OPERAND (var, 0);
9123 tree arglist = TREE_OPERAND (var, 1);
9125 /* We never want to return a VAR_DECL for a variable concept, since they
9126 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9127 bool concept_p = flag_concepts && variable_concept_p (templ);
9128 if (concept_p && processing_template_decl)
9129 return var;
9131 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
9132 arglist = add_outermost_template_args (tmpl_args, arglist);
9134 templ = most_general_template (templ);
9135 tree parms = DECL_TEMPLATE_PARMS (templ);
9136 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
9137 /*req_all*/true,
9138 /*use_default*/true);
9140 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
9142 if (complain & tf_error)
9144 error ("use of invalid variable template %qE", var);
9145 diagnose_constraints (location_of (var), templ, arglist);
9147 return error_mark_node;
9150 /* If a template-id refers to a specialization of a variable
9151 concept, then the expression is true if and only if the
9152 concept's constraints are satisfied by the given template
9153 arguments.
9155 NOTE: This is an extension of Concepts Lite TS that
9156 allows constraints to be used in expressions. */
9157 if (concept_p)
9159 tree decl = DECL_TEMPLATE_RESULT (templ);
9160 return evaluate_variable_concept (decl, arglist);
9163 return instantiate_template (templ, arglist, complain);
9166 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9167 TARGS template args, and instantiate it if it's not dependent. */
9169 tree
9170 lookup_and_finish_template_variable (tree templ, tree targs,
9171 tsubst_flags_t complain)
9173 templ = lookup_template_variable (templ, targs);
9174 if (!any_dependent_template_arguments_p (targs))
9176 templ = finish_template_variable (templ, complain);
9177 mark_used (templ);
9180 return convert_from_reference (templ);
9184 struct pair_fn_data
9186 tree_fn_t fn;
9187 tree_fn_t any_fn;
9188 void *data;
9189 /* True when we should also visit template parameters that occur in
9190 non-deduced contexts. */
9191 bool include_nondeduced_p;
9192 hash_set<tree> *visited;
9195 /* Called from for_each_template_parm via walk_tree. */
9197 static tree
9198 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
9200 tree t = *tp;
9201 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
9202 tree_fn_t fn = pfd->fn;
9203 void *data = pfd->data;
9204 tree result = NULL_TREE;
9206 #define WALK_SUBTREE(NODE) \
9207 do \
9209 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9210 pfd->include_nondeduced_p, \
9211 pfd->any_fn); \
9212 if (result) goto out; \
9214 while (0)
9216 if (pfd->any_fn && (*pfd->any_fn)(t, data))
9217 return t;
9219 if (TYPE_P (t)
9220 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
9221 WALK_SUBTREE (TYPE_CONTEXT (t));
9223 switch (TREE_CODE (t))
9225 case RECORD_TYPE:
9226 if (TYPE_PTRMEMFUNC_P (t))
9227 break;
9228 /* Fall through. */
9230 case UNION_TYPE:
9231 case ENUMERAL_TYPE:
9232 if (!TYPE_TEMPLATE_INFO (t))
9233 *walk_subtrees = 0;
9234 else
9235 WALK_SUBTREE (TYPE_TI_ARGS (t));
9236 break;
9238 case INTEGER_TYPE:
9239 WALK_SUBTREE (TYPE_MIN_VALUE (t));
9240 WALK_SUBTREE (TYPE_MAX_VALUE (t));
9241 break;
9243 case METHOD_TYPE:
9244 /* Since we're not going to walk subtrees, we have to do this
9245 explicitly here. */
9246 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
9247 /* Fall through. */
9249 case FUNCTION_TYPE:
9250 /* Check the return type. */
9251 WALK_SUBTREE (TREE_TYPE (t));
9253 /* Check the parameter types. Since default arguments are not
9254 instantiated until they are needed, the TYPE_ARG_TYPES may
9255 contain expressions that involve template parameters. But,
9256 no-one should be looking at them yet. And, once they're
9257 instantiated, they don't contain template parameters, so
9258 there's no point in looking at them then, either. */
9260 tree parm;
9262 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
9263 WALK_SUBTREE (TREE_VALUE (parm));
9265 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9266 want walk_tree walking into them itself. */
9267 *walk_subtrees = 0;
9270 if (flag_noexcept_type)
9272 tree spec = TYPE_RAISES_EXCEPTIONS (t);
9273 if (spec)
9274 WALK_SUBTREE (TREE_PURPOSE (spec));
9276 break;
9278 case TYPEOF_TYPE:
9279 case UNDERLYING_TYPE:
9280 if (pfd->include_nondeduced_p
9281 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
9282 pfd->visited,
9283 pfd->include_nondeduced_p,
9284 pfd->any_fn))
9285 return error_mark_node;
9286 break;
9288 case FUNCTION_DECL:
9289 case VAR_DECL:
9290 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9291 WALK_SUBTREE (DECL_TI_ARGS (t));
9292 /* Fall through. */
9294 case PARM_DECL:
9295 case CONST_DECL:
9296 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
9297 WALK_SUBTREE (DECL_INITIAL (t));
9298 if (DECL_CONTEXT (t)
9299 && pfd->include_nondeduced_p)
9300 WALK_SUBTREE (DECL_CONTEXT (t));
9301 break;
9303 case BOUND_TEMPLATE_TEMPLATE_PARM:
9304 /* Record template parameters such as `T' inside `TT<T>'. */
9305 WALK_SUBTREE (TYPE_TI_ARGS (t));
9306 /* Fall through. */
9308 case TEMPLATE_TEMPLATE_PARM:
9309 case TEMPLATE_TYPE_PARM:
9310 case TEMPLATE_PARM_INDEX:
9311 if (fn && (*fn)(t, data))
9312 return t;
9313 else if (!fn)
9314 return t;
9315 break;
9317 case TEMPLATE_DECL:
9318 /* A template template parameter is encountered. */
9319 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9320 WALK_SUBTREE (TREE_TYPE (t));
9322 /* Already substituted template template parameter */
9323 *walk_subtrees = 0;
9324 break;
9326 case TYPENAME_TYPE:
9327 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9328 partial instantiation. */
9329 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
9330 break;
9332 case CONSTRUCTOR:
9333 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
9334 && pfd->include_nondeduced_p)
9335 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
9336 break;
9338 case INDIRECT_REF:
9339 case COMPONENT_REF:
9340 /* If there's no type, then this thing must be some expression
9341 involving template parameters. */
9342 if (!fn && !TREE_TYPE (t))
9343 return error_mark_node;
9344 break;
9346 case MODOP_EXPR:
9347 case CAST_EXPR:
9348 case IMPLICIT_CONV_EXPR:
9349 case REINTERPRET_CAST_EXPR:
9350 case CONST_CAST_EXPR:
9351 case STATIC_CAST_EXPR:
9352 case DYNAMIC_CAST_EXPR:
9353 case ARROW_EXPR:
9354 case DOTSTAR_EXPR:
9355 case TYPEID_EXPR:
9356 case PSEUDO_DTOR_EXPR:
9357 if (!fn)
9358 return error_mark_node;
9359 break;
9361 default:
9362 break;
9365 #undef WALK_SUBTREE
9367 /* We didn't find any template parameters we liked. */
9368 out:
9369 return result;
9372 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9373 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9374 call FN with the parameter and the DATA.
9375 If FN returns nonzero, the iteration is terminated, and
9376 for_each_template_parm returns 1. Otherwise, the iteration
9377 continues. If FN never returns a nonzero value, the value
9378 returned by for_each_template_parm is 0. If FN is NULL, it is
9379 considered to be the function which always returns 1.
9381 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9382 parameters that occur in non-deduced contexts. When false, only
9383 visits those template parameters that can be deduced. */
9385 static tree
9386 for_each_template_parm (tree t, tree_fn_t fn, void* data,
9387 hash_set<tree> *visited,
9388 bool include_nondeduced_p,
9389 tree_fn_t any_fn)
9391 struct pair_fn_data pfd;
9392 tree result;
9394 /* Set up. */
9395 pfd.fn = fn;
9396 pfd.any_fn = any_fn;
9397 pfd.data = data;
9398 pfd.include_nondeduced_p = include_nondeduced_p;
9400 /* Walk the tree. (Conceptually, we would like to walk without
9401 duplicates, but for_each_template_parm_r recursively calls
9402 for_each_template_parm, so we would need to reorganize a fair
9403 bit to use walk_tree_without_duplicates, so we keep our own
9404 visited list.) */
9405 if (visited)
9406 pfd.visited = visited;
9407 else
9408 pfd.visited = new hash_set<tree>;
9409 result = cp_walk_tree (&t,
9410 for_each_template_parm_r,
9411 &pfd,
9412 pfd.visited);
9414 /* Clean up. */
9415 if (!visited)
9417 delete pfd.visited;
9418 pfd.visited = 0;
9421 return result;
9424 /* Returns true if T depends on any template parameter. */
9427 uses_template_parms (tree t)
9429 if (t == NULL_TREE)
9430 return false;
9432 bool dependent_p;
9433 int saved_processing_template_decl;
9435 saved_processing_template_decl = processing_template_decl;
9436 if (!saved_processing_template_decl)
9437 processing_template_decl = 1;
9438 if (TYPE_P (t))
9439 dependent_p = dependent_type_p (t);
9440 else if (TREE_CODE (t) == TREE_VEC)
9441 dependent_p = any_dependent_template_arguments_p (t);
9442 else if (TREE_CODE (t) == TREE_LIST)
9443 dependent_p = (uses_template_parms (TREE_VALUE (t))
9444 || uses_template_parms (TREE_CHAIN (t)));
9445 else if (TREE_CODE (t) == TYPE_DECL)
9446 dependent_p = dependent_type_p (TREE_TYPE (t));
9447 else if (DECL_P (t)
9448 || EXPR_P (t)
9449 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
9450 || TREE_CODE (t) == OVERLOAD
9451 || BASELINK_P (t)
9452 || identifier_p (t)
9453 || TREE_CODE (t) == TRAIT_EXPR
9454 || TREE_CODE (t) == CONSTRUCTOR
9455 || CONSTANT_CLASS_P (t))
9456 dependent_p = (type_dependent_expression_p (t)
9457 || value_dependent_expression_p (t));
9458 else
9460 gcc_assert (t == error_mark_node);
9461 dependent_p = false;
9464 processing_template_decl = saved_processing_template_decl;
9466 return dependent_p;
9469 /* Returns true iff current_function_decl is an incompletely instantiated
9470 template. Useful instead of processing_template_decl because the latter
9471 is set to 0 during instantiate_non_dependent_expr. */
9473 bool
9474 in_template_function (void)
9476 tree fn = current_function_decl;
9477 bool ret;
9478 ++processing_template_decl;
9479 ret = (fn && DECL_LANG_SPECIFIC (fn)
9480 && DECL_TEMPLATE_INFO (fn)
9481 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
9482 --processing_template_decl;
9483 return ret;
9486 /* Returns true if T depends on any template parameter with level LEVEL. */
9488 bool
9489 uses_template_parms_level (tree t, int level)
9491 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
9492 /*include_nondeduced_p=*/true);
9495 /* Returns true if the signature of DECL depends on any template parameter from
9496 its enclosing class. */
9498 bool
9499 uses_outer_template_parms (tree decl)
9501 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
9502 if (depth == 0)
9503 return false;
9504 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
9505 &depth, NULL, /*include_nondeduced_p=*/true))
9506 return true;
9507 if (PRIMARY_TEMPLATE_P (decl)
9508 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9509 (DECL_TEMPLATE_PARMS (decl)),
9510 template_parm_outer_level,
9511 &depth, NULL, /*include_nondeduced_p=*/true))
9512 return true;
9513 tree ci = get_constraints (decl);
9514 if (ci)
9515 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
9516 if (ci && for_each_template_parm (ci, template_parm_outer_level,
9517 &depth, NULL, /*nondeduced*/true))
9518 return true;
9519 return false;
9522 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9523 ill-formed translation unit, i.e. a variable or function that isn't
9524 usable in a constant expression. */
9526 static inline bool
9527 neglectable_inst_p (tree d)
9529 return (DECL_P (d)
9530 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
9531 : decl_maybe_constant_var_p (d)));
9534 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9535 neglectable and instantiated from within an erroneous instantiation. */
9537 static bool
9538 limit_bad_template_recursion (tree decl)
9540 struct tinst_level *lev = current_tinst_level;
9541 int errs = errorcount + sorrycount;
9542 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
9543 return false;
9545 for (; lev; lev = lev->next)
9546 if (neglectable_inst_p (lev->decl))
9547 break;
9549 return (lev && errs > lev->errors);
9552 static int tinst_depth;
9553 extern int max_tinst_depth;
9554 int depth_reached;
9556 static GTY(()) struct tinst_level *last_error_tinst_level;
9558 /* We're starting to instantiate D; record the template instantiation context
9559 for diagnostics and to restore it later. */
9561 bool
9562 push_tinst_level (tree d)
9564 return push_tinst_level_loc (d, input_location);
9567 /* We're starting to instantiate D; record the template instantiation context
9568 at LOC for diagnostics and to restore it later. */
9570 bool
9571 push_tinst_level_loc (tree d, location_t loc)
9573 struct tinst_level *new_level;
9575 if (tinst_depth >= max_tinst_depth)
9577 /* Tell error.c not to try to instantiate any templates. */
9578 at_eof = 2;
9579 fatal_error (input_location,
9580 "template instantiation depth exceeds maximum of %d"
9581 " (use -ftemplate-depth= to increase the maximum)",
9582 max_tinst_depth);
9583 return false;
9586 /* If the current instantiation caused problems, don't let it instantiate
9587 anything else. Do allow deduction substitution and decls usable in
9588 constant expressions. */
9589 if (limit_bad_template_recursion (d))
9590 return false;
9592 /* When not -quiet, dump template instantiations other than functions, since
9593 announce_function will take care of those. */
9594 if (!quiet_flag
9595 && TREE_CODE (d) != TREE_LIST
9596 && TREE_CODE (d) != FUNCTION_DECL)
9597 fprintf (stderr, " %s", decl_as_string (d, TFF_DECL_SPECIFIERS));
9599 new_level = ggc_alloc<tinst_level> ();
9600 new_level->decl = d;
9601 new_level->locus = loc;
9602 new_level->errors = errorcount+sorrycount;
9603 new_level->in_system_header_p = in_system_header_at (input_location);
9604 new_level->next = current_tinst_level;
9605 current_tinst_level = new_level;
9607 ++tinst_depth;
9608 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
9609 depth_reached = tinst_depth;
9611 return true;
9614 /* We're done instantiating this template; return to the instantiation
9615 context. */
9617 void
9618 pop_tinst_level (void)
9620 /* Restore the filename and line number stashed away when we started
9621 this instantiation. */
9622 input_location = current_tinst_level->locus;
9623 current_tinst_level = current_tinst_level->next;
9624 --tinst_depth;
9627 /* We're instantiating a deferred template; restore the template
9628 instantiation context in which the instantiation was requested, which
9629 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9631 static tree
9632 reopen_tinst_level (struct tinst_level *level)
9634 struct tinst_level *t;
9636 tinst_depth = 0;
9637 for (t = level; t; t = t->next)
9638 ++tinst_depth;
9640 current_tinst_level = level;
9641 pop_tinst_level ();
9642 if (current_tinst_level)
9643 current_tinst_level->errors = errorcount+sorrycount;
9644 return level->decl;
9647 /* Returns the TINST_LEVEL which gives the original instantiation
9648 context. */
9650 struct tinst_level *
9651 outermost_tinst_level (void)
9653 struct tinst_level *level = current_tinst_level;
9654 if (level)
9655 while (level->next)
9656 level = level->next;
9657 return level;
9660 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9661 vector of template arguments, as for tsubst.
9663 Returns an appropriate tsubst'd friend declaration. */
9665 static tree
9666 tsubst_friend_function (tree decl, tree args)
9668 tree new_friend;
9670 if (TREE_CODE (decl) == FUNCTION_DECL
9671 && DECL_TEMPLATE_INSTANTIATION (decl)
9672 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
9673 /* This was a friend declared with an explicit template
9674 argument list, e.g.:
9676 friend void f<>(T);
9678 to indicate that f was a template instantiation, not a new
9679 function declaration. Now, we have to figure out what
9680 instantiation of what template. */
9682 tree template_id, arglist, fns;
9683 tree new_args;
9684 tree tmpl;
9685 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
9687 /* Friend functions are looked up in the containing namespace scope.
9688 We must enter that scope, to avoid finding member functions of the
9689 current class with same name. */
9690 push_nested_namespace (ns);
9691 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
9692 tf_warning_or_error, NULL_TREE,
9693 /*integral_constant_expression_p=*/false);
9694 pop_nested_namespace (ns);
9695 arglist = tsubst (DECL_TI_ARGS (decl), args,
9696 tf_warning_or_error, NULL_TREE);
9697 template_id = lookup_template_function (fns, arglist);
9699 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9700 tmpl = determine_specialization (template_id, new_friend,
9701 &new_args,
9702 /*need_member_template=*/0,
9703 TREE_VEC_LENGTH (args),
9704 tsk_none);
9705 return instantiate_template (tmpl, new_args, tf_error);
9708 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9710 /* The NEW_FRIEND will look like an instantiation, to the
9711 compiler, but is not an instantiation from the point of view of
9712 the language. For example, we might have had:
9714 template <class T> struct S {
9715 template <class U> friend void f(T, U);
9718 Then, in S<int>, template <class U> void f(int, U) is not an
9719 instantiation of anything. */
9720 if (new_friend == error_mark_node)
9721 return error_mark_node;
9723 DECL_USE_TEMPLATE (new_friend) = 0;
9724 if (TREE_CODE (decl) == TEMPLATE_DECL)
9726 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
9727 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
9728 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
9731 /* The mangled name for the NEW_FRIEND is incorrect. The function
9732 is not a template instantiation and should not be mangled like
9733 one. Therefore, we forget the mangling here; we'll recompute it
9734 later if we need it. */
9735 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
9737 SET_DECL_RTL (new_friend, NULL);
9738 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
9741 if (DECL_NAMESPACE_SCOPE_P (new_friend))
9743 tree old_decl;
9744 tree new_friend_template_info;
9745 tree new_friend_result_template_info;
9746 tree ns;
9747 int new_friend_is_defn;
9749 /* We must save some information from NEW_FRIEND before calling
9750 duplicate decls since that function will free NEW_FRIEND if
9751 possible. */
9752 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
9753 new_friend_is_defn =
9754 (DECL_INITIAL (DECL_TEMPLATE_RESULT
9755 (template_for_substitution (new_friend)))
9756 != NULL_TREE);
9757 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
9759 /* This declaration is a `primary' template. */
9760 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
9762 new_friend_result_template_info
9763 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
9765 else
9766 new_friend_result_template_info = NULL_TREE;
9768 /* Inside pushdecl_namespace_level, we will push into the
9769 current namespace. However, the friend function should go
9770 into the namespace of the template. */
9771 ns = decl_namespace_context (new_friend);
9772 push_nested_namespace (ns);
9773 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
9774 pop_nested_namespace (ns);
9776 if (old_decl == error_mark_node)
9777 return error_mark_node;
9779 if (old_decl != new_friend)
9781 /* This new friend declaration matched an existing
9782 declaration. For example, given:
9784 template <class T> void f(T);
9785 template <class U> class C {
9786 template <class T> friend void f(T) {}
9789 the friend declaration actually provides the definition
9790 of `f', once C has been instantiated for some type. So,
9791 old_decl will be the out-of-class template declaration,
9792 while new_friend is the in-class definition.
9794 But, if `f' was called before this point, the
9795 instantiation of `f' will have DECL_TI_ARGS corresponding
9796 to `T' but not to `U', references to which might appear
9797 in the definition of `f'. Previously, the most general
9798 template for an instantiation of `f' was the out-of-class
9799 version; now it is the in-class version. Therefore, we
9800 run through all specialization of `f', adding to their
9801 DECL_TI_ARGS appropriately. In particular, they need a
9802 new set of outer arguments, corresponding to the
9803 arguments for this class instantiation.
9805 The same situation can arise with something like this:
9807 friend void f(int);
9808 template <class T> class C {
9809 friend void f(T) {}
9812 when `C<int>' is instantiated. Now, `f(int)' is defined
9813 in the class. */
9815 if (!new_friend_is_defn)
9816 /* On the other hand, if the in-class declaration does
9817 *not* provide a definition, then we don't want to alter
9818 existing definitions. We can just leave everything
9819 alone. */
9821 else
9823 tree new_template = TI_TEMPLATE (new_friend_template_info);
9824 tree new_args = TI_ARGS (new_friend_template_info);
9826 /* Overwrite whatever template info was there before, if
9827 any, with the new template information pertaining to
9828 the declaration. */
9829 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
9831 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
9833 /* We should have called reregister_specialization in
9834 duplicate_decls. */
9835 gcc_assert (retrieve_specialization (new_template,
9836 new_args, 0)
9837 == old_decl);
9839 /* Instantiate it if the global has already been used. */
9840 if (DECL_ODR_USED (old_decl))
9841 instantiate_decl (old_decl, /*defer_ok=*/true,
9842 /*expl_inst_class_mem_p=*/false);
9844 else
9846 tree t;
9848 /* Indicate that the old function template is a partial
9849 instantiation. */
9850 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
9851 = new_friend_result_template_info;
9853 gcc_assert (new_template
9854 == most_general_template (new_template));
9855 gcc_assert (new_template != old_decl);
9857 /* Reassign any specializations already in the hash table
9858 to the new more general template, and add the
9859 additional template args. */
9860 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
9861 t != NULL_TREE;
9862 t = TREE_CHAIN (t))
9864 tree spec = TREE_VALUE (t);
9865 spec_entry elt;
9867 elt.tmpl = old_decl;
9868 elt.args = DECL_TI_ARGS (spec);
9869 elt.spec = NULL_TREE;
9871 decl_specializations->remove_elt (&elt);
9873 DECL_TI_ARGS (spec)
9874 = add_outermost_template_args (new_args,
9875 DECL_TI_ARGS (spec));
9877 register_specialization
9878 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
9881 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
9885 /* The information from NEW_FRIEND has been merged into OLD_DECL
9886 by duplicate_decls. */
9887 new_friend = old_decl;
9890 else
9892 tree context = DECL_CONTEXT (new_friend);
9893 bool dependent_p;
9895 /* In the code
9896 template <class T> class C {
9897 template <class U> friend void C1<U>::f (); // case 1
9898 friend void C2<T>::f (); // case 2
9900 we only need to make sure CONTEXT is a complete type for
9901 case 2. To distinguish between the two cases, we note that
9902 CONTEXT of case 1 remains dependent type after tsubst while
9903 this isn't true for case 2. */
9904 ++processing_template_decl;
9905 dependent_p = dependent_type_p (context);
9906 --processing_template_decl;
9908 if (!dependent_p
9909 && !complete_type_or_else (context, NULL_TREE))
9910 return error_mark_node;
9912 if (COMPLETE_TYPE_P (context))
9914 tree fn = new_friend;
9915 /* do_friend adds the TEMPLATE_DECL for any member friend
9916 template even if it isn't a member template, i.e.
9917 template <class T> friend A<T>::f();
9918 Look through it in that case. */
9919 if (TREE_CODE (fn) == TEMPLATE_DECL
9920 && !PRIMARY_TEMPLATE_P (fn))
9921 fn = DECL_TEMPLATE_RESULT (fn);
9922 /* Check to see that the declaration is really present, and,
9923 possibly obtain an improved declaration. */
9924 fn = check_classfn (context, fn, NULL_TREE);
9926 if (fn)
9927 new_friend = fn;
9931 return new_friend;
9934 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
9935 template arguments, as for tsubst.
9937 Returns an appropriate tsubst'd friend type or error_mark_node on
9938 failure. */
9940 static tree
9941 tsubst_friend_class (tree friend_tmpl, tree args)
9943 tree friend_type;
9944 tree tmpl;
9945 tree context;
9947 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
9949 tree t = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
9950 return TREE_TYPE (t);
9953 context = CP_DECL_CONTEXT (friend_tmpl);
9955 if (context != global_namespace)
9957 if (TREE_CODE (context) == NAMESPACE_DECL)
9958 push_nested_namespace (context);
9959 else
9960 push_nested_class (tsubst (context, args, tf_none, NULL_TREE));
9963 /* Look for a class template declaration. We look for hidden names
9964 because two friend declarations of the same template are the
9965 same. For example, in:
9967 struct A {
9968 template <typename> friend class F;
9970 template <typename> struct B {
9971 template <typename> friend class F;
9974 both F templates are the same. */
9975 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), 0, 0,
9976 /*block_p=*/true, 0, LOOKUP_HIDDEN);
9978 /* But, if we don't find one, it might be because we're in a
9979 situation like this:
9981 template <class T>
9982 struct S {
9983 template <class U>
9984 friend struct S;
9987 Here, in the scope of (say) S<int>, `S' is bound to a TYPE_DECL
9988 for `S<int>', not the TEMPLATE_DECL. */
9989 if (!tmpl || !DECL_CLASS_TEMPLATE_P (tmpl))
9991 tmpl = lookup_name_prefer_type (DECL_NAME (friend_tmpl), 1);
9992 tmpl = maybe_get_template_decl_from_type_decl (tmpl);
9995 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
9997 /* The friend template has already been declared. Just
9998 check to see that the declarations match, and install any new
9999 default parameters. We must tsubst the default parameters,
10000 of course. We only need the innermost template parameters
10001 because that is all that redeclare_class_template will look
10002 at. */
10003 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
10004 > TMPL_ARGS_DEPTH (args))
10006 tree parms;
10007 location_t saved_input_location;
10008 parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
10009 args, tf_warning_or_error);
10011 saved_input_location = input_location;
10012 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
10013 tree cons = get_constraints (tmpl);
10014 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
10015 input_location = saved_input_location;
10019 friend_type = TREE_TYPE (tmpl);
10021 else
10023 /* The friend template has not already been declared. In this
10024 case, the instantiation of the template class will cause the
10025 injection of this template into the global scope. */
10026 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
10027 if (tmpl == error_mark_node)
10028 return error_mark_node;
10030 /* The new TMPL is not an instantiation of anything, so we
10031 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE for
10032 the new type because that is supposed to be the corresponding
10033 template decl, i.e., TMPL. */
10034 DECL_USE_TEMPLATE (tmpl) = 0;
10035 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
10036 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
10037 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
10038 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
10040 /* Inject this template into the global scope. */
10041 friend_type = TREE_TYPE (pushdecl_top_level (tmpl, true));
10044 if (context != global_namespace)
10046 if (TREE_CODE (context) == NAMESPACE_DECL)
10047 pop_nested_namespace (context);
10048 else
10049 pop_nested_class ();
10052 return friend_type;
10055 /* Returns zero if TYPE cannot be completed later due to circularity.
10056 Otherwise returns one. */
10058 static int
10059 can_complete_type_without_circularity (tree type)
10061 if (type == NULL_TREE || type == error_mark_node)
10062 return 0;
10063 else if (COMPLETE_TYPE_P (type))
10064 return 1;
10065 else if (TREE_CODE (type) == ARRAY_TYPE)
10066 return can_complete_type_without_circularity (TREE_TYPE (type));
10067 else if (CLASS_TYPE_P (type)
10068 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
10069 return 0;
10070 else
10071 return 1;
10074 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
10075 tsubst_flags_t, tree);
10077 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10078 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10080 static tree
10081 tsubst_attribute (tree t, tree *decl_p, tree args,
10082 tsubst_flags_t complain, tree in_decl)
10084 gcc_assert (ATTR_IS_DEPENDENT (t));
10086 tree val = TREE_VALUE (t);
10087 if (val == NULL_TREE)
10088 /* Nothing to do. */;
10089 else if ((flag_openmp || flag_openmp_simd || flag_cilkplus)
10090 && is_attribute_p ("omp declare simd",
10091 get_attribute_name (t)))
10093 tree clauses = TREE_VALUE (val);
10094 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
10095 complain, in_decl);
10096 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
10097 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
10098 tree parms = DECL_ARGUMENTS (*decl_p);
10099 clauses
10100 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
10101 if (clauses)
10102 val = build_tree_list (NULL_TREE, clauses);
10103 else
10104 val = NULL_TREE;
10106 /* If the first attribute argument is an identifier, don't
10107 pass it through tsubst. Attributes like mode, format,
10108 cleanup and several target specific attributes expect it
10109 unmodified. */
10110 else if (attribute_takes_identifier_p (get_attribute_name (t)))
10112 tree chain
10113 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
10114 /*integral_constant_expression_p=*/false);
10115 if (chain != TREE_CHAIN (val))
10116 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
10118 else if (PACK_EXPANSION_P (val))
10120 /* An attribute pack expansion. */
10121 tree purp = TREE_PURPOSE (t);
10122 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
10123 if (pack == error_mark_node)
10124 return error_mark_node;
10125 int len = TREE_VEC_LENGTH (pack);
10126 tree list = NULL_TREE;
10127 tree *q = &list;
10128 for (int i = 0; i < len; ++i)
10130 tree elt = TREE_VEC_ELT (pack, i);
10131 *q = build_tree_list (purp, elt);
10132 q = &TREE_CHAIN (*q);
10134 return list;
10136 else
10137 val = tsubst_expr (val, args, complain, in_decl,
10138 /*integral_constant_expression_p=*/false);
10140 if (val != TREE_VALUE (t))
10141 return build_tree_list (TREE_PURPOSE (t), val);
10142 return t;
10145 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10146 unchanged or a new TREE_LIST chain. */
10148 static tree
10149 tsubst_attributes (tree attributes, tree args,
10150 tsubst_flags_t complain, tree in_decl)
10152 tree last_dep = NULL_TREE;
10154 for (tree t = attributes; t; t = TREE_CHAIN (t))
10155 if (ATTR_IS_DEPENDENT (t))
10157 last_dep = t;
10158 attributes = copy_list (attributes);
10159 break;
10162 if (last_dep)
10163 for (tree *p = &attributes; *p; )
10165 tree t = *p;
10166 if (ATTR_IS_DEPENDENT (t))
10168 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
10169 if (subst != t)
10171 *p = subst;
10173 p = &TREE_CHAIN (*p);
10174 while (*p);
10175 *p = TREE_CHAIN (t);
10176 continue;
10179 p = &TREE_CHAIN (*p);
10182 return attributes;
10185 /* Apply any attributes which had to be deferred until instantiation
10186 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10187 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10189 static void
10190 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
10191 tree args, tsubst_flags_t complain, tree in_decl)
10193 tree last_dep = NULL_TREE;
10194 tree t;
10195 tree *p;
10197 if (attributes == NULL_TREE)
10198 return;
10200 if (DECL_P (*decl_p))
10202 if (TREE_TYPE (*decl_p) == error_mark_node)
10203 return;
10204 p = &DECL_ATTRIBUTES (*decl_p);
10205 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10206 to our attributes parameter. */
10207 gcc_assert (*p == attributes);
10209 else
10211 p = &TYPE_ATTRIBUTES (*decl_p);
10212 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10213 lookup_template_class_1, and should be preserved. */
10214 gcc_assert (*p != attributes);
10215 while (*p)
10216 p = &TREE_CHAIN (*p);
10219 for (t = attributes; t; t = TREE_CHAIN (t))
10220 if (ATTR_IS_DEPENDENT (t))
10222 last_dep = t;
10223 attributes = copy_list (attributes);
10224 break;
10227 *p = attributes;
10228 if (last_dep)
10230 tree late_attrs = NULL_TREE;
10231 tree *q = &late_attrs;
10233 for (; *p; )
10235 t = *p;
10236 if (ATTR_IS_DEPENDENT (t))
10238 *p = TREE_CHAIN (t);
10239 TREE_CHAIN (t) = NULL_TREE;
10240 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
10242 q = &TREE_CHAIN (*q);
10243 while (*q);
10245 else
10246 p = &TREE_CHAIN (t);
10249 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
10253 /* Perform (or defer) access check for typedefs that were referenced
10254 from within the template TMPL code.
10255 This is a subroutine of instantiate_decl and instantiate_class_template.
10256 TMPL is the template to consider and TARGS is the list of arguments of
10257 that template. */
10259 static void
10260 perform_typedefs_access_check (tree tmpl, tree targs)
10262 location_t saved_location;
10263 unsigned i;
10264 qualified_typedef_usage_t *iter;
10266 if (!tmpl
10267 || (!CLASS_TYPE_P (tmpl)
10268 && TREE_CODE (tmpl) != FUNCTION_DECL))
10269 return;
10271 saved_location = input_location;
10272 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
10274 tree type_decl = iter->typedef_decl;
10275 tree type_scope = iter->context;
10277 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
10278 continue;
10280 if (uses_template_parms (type_decl))
10281 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
10282 if (uses_template_parms (type_scope))
10283 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
10285 /* Make access check error messages point to the location
10286 of the use of the typedef. */
10287 input_location = iter->locus;
10288 perform_or_defer_access_check (TYPE_BINFO (type_scope),
10289 type_decl, type_decl,
10290 tf_warning_or_error);
10292 input_location = saved_location;
10295 static tree
10296 instantiate_class_template_1 (tree type)
10298 tree templ, args, pattern, t, member;
10299 tree typedecl;
10300 tree pbinfo;
10301 tree base_list;
10302 unsigned int saved_maximum_field_alignment;
10303 tree fn_context;
10305 if (type == error_mark_node)
10306 return error_mark_node;
10308 if (COMPLETE_OR_OPEN_TYPE_P (type)
10309 || uses_template_parms (type))
10310 return type;
10312 /* Figure out which template is being instantiated. */
10313 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
10314 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
10316 /* Determine what specialization of the original template to
10317 instantiate. */
10318 t = most_specialized_partial_spec (type, tf_warning_or_error);
10319 if (t == error_mark_node)
10321 TYPE_BEING_DEFINED (type) = 1;
10322 return error_mark_node;
10324 else if (t)
10326 /* This TYPE is actually an instantiation of a partial
10327 specialization. We replace the innermost set of ARGS with
10328 the arguments appropriate for substitution. For example,
10329 given:
10331 template <class T> struct S {};
10332 template <class T> struct S<T*> {};
10334 and supposing that we are instantiating S<int*>, ARGS will
10335 presently be {int*} -- but we need {int}. */
10336 pattern = TREE_TYPE (t);
10337 args = TREE_PURPOSE (t);
10339 else
10341 pattern = TREE_TYPE (templ);
10342 args = CLASSTYPE_TI_ARGS (type);
10345 /* If the template we're instantiating is incomplete, then clearly
10346 there's nothing we can do. */
10347 if (!COMPLETE_TYPE_P (pattern))
10348 return type;
10350 /* If we've recursively instantiated too many templates, stop. */
10351 if (! push_tinst_level (type))
10352 return type;
10354 /* Now we're really doing the instantiation. Mark the type as in
10355 the process of being defined. */
10356 TYPE_BEING_DEFINED (type) = 1;
10358 /* We may be in the middle of deferred access check. Disable
10359 it now. */
10360 push_deferring_access_checks (dk_no_deferred);
10362 int saved_unevaluated_operand = cp_unevaluated_operand;
10363 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10365 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
10366 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10367 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
10368 fn_context = error_mark_node;
10369 if (!fn_context)
10370 push_to_top_level ();
10371 else
10373 cp_unevaluated_operand = 0;
10374 c_inhibit_evaluation_warnings = 0;
10376 /* Use #pragma pack from the template context. */
10377 saved_maximum_field_alignment = maximum_field_alignment;
10378 maximum_field_alignment = TYPE_PRECISION (pattern);
10380 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
10382 /* Set the input location to the most specialized template definition.
10383 This is needed if tsubsting causes an error. */
10384 typedecl = TYPE_MAIN_DECL (pattern);
10385 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
10386 DECL_SOURCE_LOCATION (typedecl);
10388 TYPE_PACKED (type) = TYPE_PACKED (pattern);
10389 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
10390 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
10391 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
10392 if (ANON_AGGR_TYPE_P (pattern))
10393 SET_ANON_AGGR_TYPE_P (type);
10394 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
10396 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
10397 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
10398 /* Adjust visibility for template arguments. */
10399 determine_visibility (TYPE_MAIN_DECL (type));
10401 if (CLASS_TYPE_P (type))
10402 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
10404 pbinfo = TYPE_BINFO (pattern);
10406 /* We should never instantiate a nested class before its enclosing
10407 class; we need to look up the nested class by name before we can
10408 instantiate it, and that lookup should instantiate the enclosing
10409 class. */
10410 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
10411 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
10413 base_list = NULL_TREE;
10414 if (BINFO_N_BASE_BINFOS (pbinfo))
10416 tree pbase_binfo;
10417 tree pushed_scope;
10418 int i;
10420 /* We must enter the scope containing the type, as that is where
10421 the accessibility of types named in dependent bases are
10422 looked up from. */
10423 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
10425 /* Substitute into each of the bases to determine the actual
10426 basetypes. */
10427 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
10429 tree base;
10430 tree access = BINFO_BASE_ACCESS (pbinfo, i);
10431 tree expanded_bases = NULL_TREE;
10432 int idx, len = 1;
10434 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
10436 expanded_bases =
10437 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
10438 args, tf_error, NULL_TREE);
10439 if (expanded_bases == error_mark_node)
10440 continue;
10442 len = TREE_VEC_LENGTH (expanded_bases);
10445 for (idx = 0; idx < len; idx++)
10447 if (expanded_bases)
10448 /* Extract the already-expanded base class. */
10449 base = TREE_VEC_ELT (expanded_bases, idx);
10450 else
10451 /* Substitute to figure out the base class. */
10452 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
10453 NULL_TREE);
10455 if (base == error_mark_node)
10456 continue;
10458 base_list = tree_cons (access, base, base_list);
10459 if (BINFO_VIRTUAL_P (pbase_binfo))
10460 TREE_TYPE (base_list) = integer_type_node;
10464 /* The list is now in reverse order; correct that. */
10465 base_list = nreverse (base_list);
10467 if (pushed_scope)
10468 pop_scope (pushed_scope);
10470 /* Now call xref_basetypes to set up all the base-class
10471 information. */
10472 xref_basetypes (type, base_list);
10474 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
10475 (int) ATTR_FLAG_TYPE_IN_PLACE,
10476 args, tf_error, NULL_TREE);
10477 fixup_attribute_variants (type);
10479 /* Now that our base classes are set up, enter the scope of the
10480 class, so that name lookups into base classes, etc. will work
10481 correctly. This is precisely analogous to what we do in
10482 begin_class_definition when defining an ordinary non-template
10483 class, except we also need to push the enclosing classes. */
10484 push_nested_class (type);
10486 /* Now members are processed in the order of declaration. */
10487 for (member = CLASSTYPE_DECL_LIST (pattern);
10488 member; member = TREE_CHAIN (member))
10490 tree t = TREE_VALUE (member);
10492 if (TREE_PURPOSE (member))
10494 if (TYPE_P (t))
10496 /* Build new CLASSTYPE_NESTED_UTDS. */
10498 tree newtag;
10499 bool class_template_p;
10501 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
10502 && TYPE_LANG_SPECIFIC (t)
10503 && CLASSTYPE_IS_TEMPLATE (t));
10504 /* If the member is a class template, then -- even after
10505 substitution -- there may be dependent types in the
10506 template argument list for the class. We increment
10507 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10508 that function will assume that no types are dependent
10509 when outside of a template. */
10510 if (class_template_p)
10511 ++processing_template_decl;
10512 newtag = tsubst (t, args, tf_error, NULL_TREE);
10513 if (class_template_p)
10514 --processing_template_decl;
10515 if (newtag == error_mark_node)
10516 continue;
10518 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
10520 tree name = TYPE_IDENTIFIER (t);
10522 if (class_template_p)
10523 /* Unfortunately, lookup_template_class sets
10524 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10525 instantiation (i.e., for the type of a member
10526 template class nested within a template class.)
10527 This behavior is required for
10528 maybe_process_partial_specialization to work
10529 correctly, but is not accurate in this case;
10530 the TAG is not an instantiation of anything.
10531 (The corresponding TEMPLATE_DECL is an
10532 instantiation, but the TYPE is not.) */
10533 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
10535 /* Now, we call pushtag to put this NEWTAG into the scope of
10536 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10537 pushtag calling push_template_decl. We don't have to do
10538 this for enums because it will already have been done in
10539 tsubst_enum. */
10540 if (name)
10541 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
10542 pushtag (name, newtag, /*tag_scope=*/ts_current);
10545 else if (DECL_DECLARES_FUNCTION_P (t))
10547 /* Build new TYPE_METHODS. */
10548 tree r;
10550 if (TREE_CODE (t) == TEMPLATE_DECL)
10551 ++processing_template_decl;
10552 r = tsubst (t, args, tf_error, NULL_TREE);
10553 if (TREE_CODE (t) == TEMPLATE_DECL)
10554 --processing_template_decl;
10555 set_current_access_from_decl (r);
10556 finish_member_declaration (r);
10557 /* Instantiate members marked with attribute used. */
10558 if (r != error_mark_node && DECL_PRESERVE_P (r))
10559 mark_used (r);
10560 if (TREE_CODE (r) == FUNCTION_DECL
10561 && DECL_OMP_DECLARE_REDUCTION_P (r))
10562 cp_check_omp_declare_reduction (r);
10564 else if (DECL_CLASS_TEMPLATE_P (t)
10565 && LAMBDA_TYPE_P (TREE_TYPE (t)))
10566 /* A closure type for a lambda in a default argument for a
10567 member template. Ignore it; it will be instantiated with
10568 the default argument. */;
10569 else
10571 /* Build new TYPE_FIELDS. */
10572 if (TREE_CODE (t) == STATIC_ASSERT)
10574 tree condition;
10576 ++c_inhibit_evaluation_warnings;
10577 condition =
10578 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
10579 tf_warning_or_error, NULL_TREE,
10580 /*integral_constant_expression_p=*/true);
10581 --c_inhibit_evaluation_warnings;
10583 finish_static_assert (condition,
10584 STATIC_ASSERT_MESSAGE (t),
10585 STATIC_ASSERT_SOURCE_LOCATION (t),
10586 /*member_p=*/true);
10588 else if (TREE_CODE (t) != CONST_DECL)
10590 tree r;
10591 tree vec = NULL_TREE;
10592 int len = 1;
10594 /* The file and line for this declaration, to
10595 assist in error message reporting. Since we
10596 called push_tinst_level above, we don't need to
10597 restore these. */
10598 input_location = DECL_SOURCE_LOCATION (t);
10600 if (TREE_CODE (t) == TEMPLATE_DECL)
10601 ++processing_template_decl;
10602 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
10603 if (TREE_CODE (t) == TEMPLATE_DECL)
10604 --processing_template_decl;
10606 if (TREE_CODE (r) == TREE_VEC)
10608 /* A capture pack became multiple fields. */
10609 vec = r;
10610 len = TREE_VEC_LENGTH (vec);
10613 for (int i = 0; i < len; ++i)
10615 if (vec)
10616 r = TREE_VEC_ELT (vec, i);
10617 if (VAR_P (r))
10619 /* In [temp.inst]:
10621 [t]he initialization (and any associated
10622 side-effects) of a static data member does
10623 not occur unless the static data member is
10624 itself used in a way that requires the
10625 definition of the static data member to
10626 exist.
10628 Therefore, we do not substitute into the
10629 initialized for the static data member here. */
10630 finish_static_data_member_decl
10632 /*init=*/NULL_TREE,
10633 /*init_const_expr_p=*/false,
10634 /*asmspec_tree=*/NULL_TREE,
10635 /*flags=*/0);
10636 /* Instantiate members marked with attribute used. */
10637 if (r != error_mark_node && DECL_PRESERVE_P (r))
10638 mark_used (r);
10640 else if (TREE_CODE (r) == FIELD_DECL)
10642 /* Determine whether R has a valid type and can be
10643 completed later. If R is invalid, then its type
10644 is replaced by error_mark_node. */
10645 tree rtype = TREE_TYPE (r);
10646 if (can_complete_type_without_circularity (rtype))
10647 complete_type (rtype);
10649 if (!complete_or_array_type_p (rtype))
10651 /* If R's type couldn't be completed and
10652 it isn't a flexible array member (whose
10653 type is incomplete by definition) give
10654 an error. */
10655 cxx_incomplete_type_error (r, rtype);
10656 TREE_TYPE (r) = error_mark_node;
10660 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10661 such a thing will already have been added to the field
10662 list by tsubst_enum in finish_member_declaration in the
10663 CLASSTYPE_NESTED_UTDS case above. */
10664 if (!(TREE_CODE (r) == TYPE_DECL
10665 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
10666 && DECL_ARTIFICIAL (r)))
10668 set_current_access_from_decl (r);
10669 finish_member_declaration (r);
10675 else
10677 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
10678 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10680 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10682 tree friend_type = t;
10683 bool adjust_processing_template_decl = false;
10685 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10687 /* template <class T> friend class C; */
10688 friend_type = tsubst_friend_class (friend_type, args);
10689 adjust_processing_template_decl = true;
10691 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
10693 /* template <class T> friend class C::D; */
10694 friend_type = tsubst (friend_type, args,
10695 tf_warning_or_error, NULL_TREE);
10696 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10697 friend_type = TREE_TYPE (friend_type);
10698 adjust_processing_template_decl = true;
10700 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
10701 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
10703 /* This could be either
10705 friend class T::C;
10707 when dependent_type_p is false or
10709 template <class U> friend class T::C;
10711 otherwise. */
10712 /* Bump processing_template_decl in case this is something like
10713 template <class T> friend struct A<T>::B. */
10714 ++processing_template_decl;
10715 friend_type = tsubst (friend_type, args,
10716 tf_warning_or_error, NULL_TREE);
10717 if (dependent_type_p (friend_type))
10718 adjust_processing_template_decl = true;
10719 --processing_template_decl;
10721 else if (!CLASSTYPE_USE_TEMPLATE (friend_type)
10722 && TYPE_HIDDEN_P (friend_type))
10724 /* friend class C;
10726 where C hasn't been declared yet. Let's lookup name
10727 from namespace scope directly, bypassing any name that
10728 come from dependent base class. */
10729 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
10731 /* The call to xref_tag_from_type does injection for friend
10732 classes. */
10733 push_nested_namespace (ns);
10734 friend_type =
10735 xref_tag_from_type (friend_type, NULL_TREE,
10736 /*tag_scope=*/ts_current);
10737 pop_nested_namespace (ns);
10739 else if (uses_template_parms (friend_type))
10740 /* friend class C<T>; */
10741 friend_type = tsubst (friend_type, args,
10742 tf_warning_or_error, NULL_TREE);
10743 /* Otherwise it's
10745 friend class C;
10747 where C is already declared or
10749 friend class C<int>;
10751 We don't have to do anything in these cases. */
10753 if (adjust_processing_template_decl)
10754 /* Trick make_friend_class into realizing that the friend
10755 we're adding is a template, not an ordinary class. It's
10756 important that we use make_friend_class since it will
10757 perform some error-checking and output cross-reference
10758 information. */
10759 ++processing_template_decl;
10761 if (friend_type != error_mark_node)
10762 make_friend_class (type, friend_type, /*complain=*/false);
10764 if (adjust_processing_template_decl)
10765 --processing_template_decl;
10767 else
10769 /* Build new DECL_FRIENDLIST. */
10770 tree r;
10772 /* The file and line for this declaration, to
10773 assist in error message reporting. Since we
10774 called push_tinst_level above, we don't need to
10775 restore these. */
10776 input_location = DECL_SOURCE_LOCATION (t);
10778 if (TREE_CODE (t) == TEMPLATE_DECL)
10780 ++processing_template_decl;
10781 push_deferring_access_checks (dk_no_check);
10784 r = tsubst_friend_function (t, args);
10785 add_friend (type, r, /*complain=*/false);
10786 if (TREE_CODE (t) == TEMPLATE_DECL)
10788 pop_deferring_access_checks ();
10789 --processing_template_decl;
10795 if (fn_context)
10797 /* Restore these before substituting into the lambda capture
10798 initializers. */
10799 cp_unevaluated_operand = saved_unevaluated_operand;
10800 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
10803 if (tree expr = CLASSTYPE_LAMBDA_EXPR (type))
10805 tree decl = lambda_function (type);
10806 if (decl)
10808 if (cxx_dialect >= cxx1z)
10809 CLASSTYPE_LITERAL_P (type) = true;
10811 if (!DECL_TEMPLATE_INFO (decl)
10812 || DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (decl)) != decl)
10814 /* Set function_depth to avoid garbage collection. */
10815 ++function_depth;
10816 instantiate_decl (decl, /*defer_ok=*/false, false);
10817 --function_depth;
10820 /* We need to instantiate the capture list from the template
10821 after we've instantiated the closure members, but before we
10822 consider adding the conversion op. Also keep any captures
10823 that may have been added during instantiation of the op(). */
10824 tree tmpl_expr = CLASSTYPE_LAMBDA_EXPR (pattern);
10825 tree tmpl_cap
10826 = tsubst_copy_and_build (LAMBDA_EXPR_CAPTURE_LIST (tmpl_expr),
10827 args, tf_warning_or_error, NULL_TREE,
10828 false, false);
10830 LAMBDA_EXPR_CAPTURE_LIST (expr)
10831 = chainon (tmpl_cap, nreverse (LAMBDA_EXPR_CAPTURE_LIST (expr)));
10833 maybe_add_lambda_conv_op (type);
10835 else
10836 gcc_assert (errorcount);
10839 /* Set the file and line number information to whatever is given for
10840 the class itself. This puts error messages involving generated
10841 implicit functions at a predictable point, and the same point
10842 that would be used for non-template classes. */
10843 input_location = DECL_SOURCE_LOCATION (typedecl);
10845 unreverse_member_declarations (type);
10846 finish_struct_1 (type);
10847 TYPE_BEING_DEFINED (type) = 0;
10849 /* We don't instantiate default arguments for member functions. 14.7.1:
10851 The implicit instantiation of a class template specialization causes
10852 the implicit instantiation of the declarations, but not of the
10853 definitions or default arguments, of the class member functions,
10854 member classes, static data members and member templates.... */
10856 /* Some typedefs referenced from within the template code need to be access
10857 checked at template instantiation time, i.e now. These types were
10858 added to the template at parsing time. Let's get those and perform
10859 the access checks then. */
10860 perform_typedefs_access_check (pattern, args);
10861 perform_deferred_access_checks (tf_warning_or_error);
10862 pop_nested_class ();
10863 maximum_field_alignment = saved_maximum_field_alignment;
10864 if (!fn_context)
10865 pop_from_top_level ();
10866 pop_deferring_access_checks ();
10867 pop_tinst_level ();
10869 /* The vtable for a template class can be emitted in any translation
10870 unit in which the class is instantiated. When there is no key
10871 method, however, finish_struct_1 will already have added TYPE to
10872 the keyed_classes list. */
10873 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
10874 keyed_classes = tree_cons (NULL_TREE, type, keyed_classes);
10876 return type;
10879 /* Wrapper for instantiate_class_template_1. */
10881 tree
10882 instantiate_class_template (tree type)
10884 tree ret;
10885 timevar_push (TV_TEMPLATE_INST);
10886 ret = instantiate_class_template_1 (type);
10887 timevar_pop (TV_TEMPLATE_INST);
10888 return ret;
10891 static tree
10892 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
10894 tree r;
10896 if (!t)
10897 r = t;
10898 else if (TYPE_P (t))
10899 r = tsubst (t, args, complain, in_decl);
10900 else
10902 if (!(complain & tf_warning))
10903 ++c_inhibit_evaluation_warnings;
10904 r = tsubst_expr (t, args, complain, in_decl,
10905 /*integral_constant_expression_p=*/true);
10906 if (!(complain & tf_warning))
10907 --c_inhibit_evaluation_warnings;
10909 return r;
10912 /* Given a function parameter pack TMPL_PARM and some function parameters
10913 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
10914 and set *SPEC_P to point at the next point in the list. */
10916 tree
10917 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
10919 /* Collect all of the extra "packed" parameters into an
10920 argument pack. */
10921 tree parmvec;
10922 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
10923 tree spec_parm = *spec_p;
10924 int i, len;
10926 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
10927 if (tmpl_parm
10928 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
10929 break;
10931 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
10932 parmvec = make_tree_vec (len);
10933 spec_parm = *spec_p;
10934 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
10935 TREE_VEC_ELT (parmvec, i) = spec_parm;
10937 /* Build the argument packs. */
10938 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
10939 *spec_p = spec_parm;
10941 return argpack;
10944 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
10945 NONTYPE_ARGUMENT_PACK. */
10947 static tree
10948 make_fnparm_pack (tree spec_parm)
10950 return extract_fnparm_pack (NULL_TREE, &spec_parm);
10953 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
10954 pack expansion with no extra args, 2 if it has extra args, or 0
10955 if it is not a pack expansion. */
10957 static int
10958 argument_pack_element_is_expansion_p (tree arg_pack, int i)
10960 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
10961 if (i >= TREE_VEC_LENGTH (vec))
10962 return 0;
10963 tree elt = TREE_VEC_ELT (vec, i);
10964 if (DECL_P (elt))
10965 /* A decl pack is itself an expansion. */
10966 elt = TREE_TYPE (elt);
10967 if (!PACK_EXPANSION_P (elt))
10968 return 0;
10969 if (PACK_EXPANSION_EXTRA_ARGS (elt))
10970 return 2;
10971 return 1;
10975 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
10977 static tree
10978 make_argument_pack_select (tree arg_pack, unsigned index)
10980 tree aps = make_node (ARGUMENT_PACK_SELECT);
10982 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
10983 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
10985 return aps;
10988 /* This is a subroutine of tsubst_pack_expansion.
10990 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
10991 mechanism to store the (non complete list of) arguments of the
10992 substitution and return a non substituted pack expansion, in order
10993 to wait for when we have enough arguments to really perform the
10994 substitution. */
10996 static bool
10997 use_pack_expansion_extra_args_p (tree parm_packs,
10998 int arg_pack_len,
10999 bool has_empty_arg)
11001 /* If one pack has an expansion and another pack has a normal
11002 argument or if one pack has an empty argument and an another
11003 one hasn't then tsubst_pack_expansion cannot perform the
11004 substitution and need to fall back on the
11005 PACK_EXPANSION_EXTRA mechanism. */
11006 if (parm_packs == NULL_TREE)
11007 return false;
11008 else if (has_empty_arg)
11009 return true;
11011 bool has_expansion_arg = false;
11012 for (int i = 0 ; i < arg_pack_len; ++i)
11014 bool has_non_expansion_arg = false;
11015 for (tree parm_pack = parm_packs;
11016 parm_pack;
11017 parm_pack = TREE_CHAIN (parm_pack))
11019 tree arg = TREE_VALUE (parm_pack);
11021 int exp = argument_pack_element_is_expansion_p (arg, i);
11022 if (exp == 2)
11023 /* We can't substitute a pack expansion with extra args into
11024 our pattern. */
11025 return true;
11026 else if (exp)
11027 has_expansion_arg = true;
11028 else
11029 has_non_expansion_arg = true;
11032 if (has_expansion_arg && has_non_expansion_arg)
11033 return true;
11035 return false;
11038 /* [temp.variadic]/6 says that:
11040 The instantiation of a pack expansion [...]
11041 produces a list E1,E2, ..., En, where N is the number of elements
11042 in the pack expansion parameters.
11044 This subroutine of tsubst_pack_expansion produces one of these Ei.
11046 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11047 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11048 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11049 INDEX is the index 'i' of the element Ei to produce. ARGS,
11050 COMPLAIN, and IN_DECL are the same parameters as for the
11051 tsubst_pack_expansion function.
11053 The function returns the resulting Ei upon successful completion,
11054 or error_mark_node.
11056 Note that this function possibly modifies the ARGS parameter, so
11057 it's the responsibility of the caller to restore it. */
11059 static tree
11060 gen_elem_of_pack_expansion_instantiation (tree pattern,
11061 tree parm_packs,
11062 unsigned index,
11063 tree args /* This parm gets
11064 modified. */,
11065 tsubst_flags_t complain,
11066 tree in_decl)
11068 tree t;
11069 bool ith_elem_is_expansion = false;
11071 /* For each parameter pack, change the substitution of the parameter
11072 pack to the ith argument in its argument pack, then expand the
11073 pattern. */
11074 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
11076 tree parm = TREE_PURPOSE (pack);
11077 tree arg_pack = TREE_VALUE (pack);
11078 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
11080 ith_elem_is_expansion |=
11081 argument_pack_element_is_expansion_p (arg_pack, index);
11083 /* Select the Ith argument from the pack. */
11084 if (TREE_CODE (parm) == PARM_DECL
11085 || TREE_CODE (parm) == FIELD_DECL)
11087 if (index == 0)
11089 aps = make_argument_pack_select (arg_pack, index);
11090 if (!mark_used (parm, complain) && !(complain & tf_error))
11091 return error_mark_node;
11092 register_local_specialization (aps, parm);
11094 else
11095 aps = retrieve_local_specialization (parm);
11097 else
11099 int idx, level;
11100 template_parm_level_and_index (parm, &level, &idx);
11102 if (index == 0)
11104 aps = make_argument_pack_select (arg_pack, index);
11105 /* Update the corresponding argument. */
11106 TMPL_ARG (args, level, idx) = aps;
11108 else
11109 /* Re-use the ARGUMENT_PACK_SELECT. */
11110 aps = TMPL_ARG (args, level, idx);
11112 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11115 /* Substitute into the PATTERN with the (possibly altered)
11116 arguments. */
11117 if (pattern == in_decl)
11118 /* Expanding a fixed parameter pack from
11119 coerce_template_parameter_pack. */
11120 t = tsubst_decl (pattern, args, complain);
11121 else if (pattern == error_mark_node)
11122 t = error_mark_node;
11123 else if (constraint_p (pattern))
11125 if (processing_template_decl)
11126 t = tsubst_constraint (pattern, args, complain, in_decl);
11127 else
11128 t = (constraints_satisfied_p (pattern, args)
11129 ? boolean_true_node : boolean_false_node);
11131 else if (!TYPE_P (pattern))
11132 t = tsubst_expr (pattern, args, complain, in_decl,
11133 /*integral_constant_expression_p=*/false);
11134 else
11135 t = tsubst (pattern, args, complain, in_decl);
11137 /* If the Ith argument pack element is a pack expansion, then
11138 the Ith element resulting from the substituting is going to
11139 be a pack expansion as well. */
11140 if (ith_elem_is_expansion)
11141 t = make_pack_expansion (t);
11143 return t;
11146 /* When the unexpanded parameter pack in a fold expression expands to an empty
11147 sequence, the value of the expression is as follows; the program is
11148 ill-formed if the operator is not listed in this table.
11150 && true
11151 || false
11152 , void() */
11154 tree
11155 expand_empty_fold (tree t, tsubst_flags_t complain)
11157 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
11158 if (!FOLD_EXPR_MODIFY_P (t))
11159 switch (code)
11161 case TRUTH_ANDIF_EXPR:
11162 return boolean_true_node;
11163 case TRUTH_ORIF_EXPR:
11164 return boolean_false_node;
11165 case COMPOUND_EXPR:
11166 return void_node;
11167 default:
11168 break;
11171 if (complain & tf_error)
11172 error_at (location_of (t),
11173 "fold of empty expansion over %O", code);
11174 return error_mark_node;
11177 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11178 form an expression that combines the two terms using the
11179 operator of T. */
11181 static tree
11182 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
11184 tree op = FOLD_EXPR_OP (t);
11185 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
11187 // Handle compound assignment operators.
11188 if (FOLD_EXPR_MODIFY_P (t))
11189 return build_x_modify_expr (input_location, left, code, right, complain);
11191 switch (code)
11193 case COMPOUND_EXPR:
11194 return build_x_compound_expr (input_location, left, right, complain);
11195 case DOTSTAR_EXPR:
11196 return build_m_component_ref (left, right, complain);
11197 default:
11198 return build_x_binary_op (input_location, code,
11199 left, TREE_CODE (left),
11200 right, TREE_CODE (right),
11201 /*overload=*/NULL,
11202 complain);
11206 /* Substitute ARGS into the pack of a fold expression T. */
11208 static inline tree
11209 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11211 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
11214 /* Substitute ARGS into the pack of a fold expression T. */
11216 static inline tree
11217 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11219 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
11222 /* Expand a PACK of arguments into a grouped as left fold.
11223 Given a pack containing elements A0, A1, ..., An and an
11224 operator @, this builds the expression:
11226 ((A0 @ A1) @ A2) ... @ An
11228 Note that PACK must not be empty.
11230 The operator is defined by the original fold expression T. */
11232 static tree
11233 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
11235 tree left = TREE_VEC_ELT (pack, 0);
11236 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
11238 tree right = TREE_VEC_ELT (pack, i);
11239 left = fold_expression (t, left, right, complain);
11241 return left;
11244 /* Substitute into a unary left fold expression. */
11246 static tree
11247 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
11248 tree in_decl)
11250 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11251 if (pack == error_mark_node)
11252 return error_mark_node;
11253 if (PACK_EXPANSION_P (pack))
11255 tree r = copy_node (t);
11256 FOLD_EXPR_PACK (r) = pack;
11257 return r;
11259 if (TREE_VEC_LENGTH (pack) == 0)
11260 return expand_empty_fold (t, complain);
11261 else
11262 return expand_left_fold (t, pack, complain);
11265 /* Substitute into a binary left fold expression.
11267 Do ths by building a single (non-empty) vector of argumnts and
11268 building the expression from those elements. */
11270 static tree
11271 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
11272 tree in_decl)
11274 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11275 if (pack == error_mark_node)
11276 return error_mark_node;
11277 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11278 if (init == error_mark_node)
11279 return error_mark_node;
11281 if (PACK_EXPANSION_P (pack))
11283 tree r = copy_node (t);
11284 FOLD_EXPR_PACK (r) = pack;
11285 FOLD_EXPR_INIT (r) = init;
11286 return r;
11289 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
11290 TREE_VEC_ELT (vec, 0) = init;
11291 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
11292 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
11294 return expand_left_fold (t, vec, complain);
11297 /* Expand a PACK of arguments into a grouped as right fold.
11298 Given a pack containing elementns A0, A1, ..., and an
11299 operator @, this builds the expression:
11301 A0@ ... (An-2 @ (An-1 @ An))
11303 Note that PACK must not be empty.
11305 The operator is defined by the original fold expression T. */
11307 tree
11308 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
11310 // Build the expression.
11311 int n = TREE_VEC_LENGTH (pack);
11312 tree right = TREE_VEC_ELT (pack, n - 1);
11313 for (--n; n != 0; --n)
11315 tree left = TREE_VEC_ELT (pack, n - 1);
11316 right = fold_expression (t, left, right, complain);
11318 return right;
11321 /* Substitute into a unary right fold expression. */
11323 static tree
11324 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
11325 tree in_decl)
11327 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11328 if (pack == error_mark_node)
11329 return error_mark_node;
11330 if (PACK_EXPANSION_P (pack))
11332 tree r = copy_node (t);
11333 FOLD_EXPR_PACK (r) = pack;
11334 return r;
11336 if (TREE_VEC_LENGTH (pack) == 0)
11337 return expand_empty_fold (t, complain);
11338 else
11339 return expand_right_fold (t, pack, complain);
11342 /* Substitute into a binary right fold expression.
11344 Do ths by building a single (non-empty) vector of arguments and
11345 building the expression from those elements. */
11347 static tree
11348 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
11349 tree in_decl)
11351 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11352 if (pack == error_mark_node)
11353 return error_mark_node;
11354 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11355 if (init == error_mark_node)
11356 return error_mark_node;
11358 if (PACK_EXPANSION_P (pack))
11360 tree r = copy_node (t);
11361 FOLD_EXPR_PACK (r) = pack;
11362 FOLD_EXPR_INIT (r) = init;
11363 return r;
11366 int n = TREE_VEC_LENGTH (pack);
11367 tree vec = make_tree_vec (n + 1);
11368 for (int i = 0; i < n; ++i)
11369 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
11370 TREE_VEC_ELT (vec, n) = init;
11372 return expand_right_fold (t, vec, complain);
11376 /* Substitute ARGS into T, which is an pack expansion
11377 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11378 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11379 (if only a partial substitution could be performed) or
11380 ERROR_MARK_NODE if there was an error. */
11381 tree
11382 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
11383 tree in_decl)
11385 tree pattern;
11386 tree pack, packs = NULL_TREE;
11387 bool unsubstituted_packs = false;
11388 int i, len = -1;
11389 tree result;
11390 hash_map<tree, tree> *saved_local_specializations = NULL;
11391 bool need_local_specializations = false;
11392 int levels;
11394 gcc_assert (PACK_EXPANSION_P (t));
11395 pattern = PACK_EXPANSION_PATTERN (t);
11397 /* Add in any args remembered from an earlier partial instantiation. */
11398 args = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (t), args);
11400 levels = TMPL_ARGS_DEPTH (args);
11402 /* Determine the argument packs that will instantiate the parameter
11403 packs used in the expansion expression. While we're at it,
11404 compute the number of arguments to be expanded and make sure it
11405 is consistent. */
11406 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
11407 pack = TREE_CHAIN (pack))
11409 tree parm_pack = TREE_VALUE (pack);
11410 tree arg_pack = NULL_TREE;
11411 tree orig_arg = NULL_TREE;
11412 int level = 0;
11414 if (TREE_CODE (parm_pack) == BASES)
11416 gcc_assert (parm_pack == pattern);
11417 if (BASES_DIRECT (parm_pack))
11418 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
11419 args, complain, in_decl, false));
11420 else
11421 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
11422 args, complain, in_decl, false));
11424 else if (builtin_pack_call_p (parm_pack))
11426 /* ??? Support use in other patterns. */
11427 gcc_assert (parm_pack == pattern);
11428 return expand_builtin_pack_call (parm_pack, args,
11429 complain, in_decl);
11431 else if (TREE_CODE (parm_pack) == PARM_DECL)
11433 /* We know we have correct local_specializations if this
11434 expansion is at function scope, or if we're dealing with a
11435 local parameter in a requires expression; for the latter,
11436 tsubst_requires_expr set it up appropriately. */
11437 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
11438 arg_pack = retrieve_local_specialization (parm_pack);
11439 else
11440 /* We can't rely on local_specializations for a parameter
11441 name used later in a function declaration (such as in a
11442 late-specified return type). Even if it exists, it might
11443 have the wrong value for a recursive call. */
11444 need_local_specializations = true;
11446 if (!arg_pack)
11448 /* This parameter pack was used in an unevaluated context. Just
11449 make a dummy decl, since it's only used for its type. */
11450 arg_pack = tsubst_decl (parm_pack, args, complain);
11451 if (arg_pack && DECL_PACK_P (arg_pack))
11452 /* Partial instantiation of the parm_pack, we can't build
11453 up an argument pack yet. */
11454 arg_pack = NULL_TREE;
11455 else
11456 arg_pack = make_fnparm_pack (arg_pack);
11459 else if (TREE_CODE (parm_pack) == FIELD_DECL)
11460 arg_pack = tsubst_copy (parm_pack, args, complain, in_decl);
11461 else
11463 int idx;
11464 template_parm_level_and_index (parm_pack, &level, &idx);
11466 if (level <= levels)
11467 arg_pack = TMPL_ARG (args, level, idx);
11470 orig_arg = arg_pack;
11471 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
11472 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
11474 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
11475 /* This can only happen if we forget to expand an argument
11476 pack somewhere else. Just return an error, silently. */
11478 result = make_tree_vec (1);
11479 TREE_VEC_ELT (result, 0) = error_mark_node;
11480 return result;
11483 if (arg_pack)
11485 int my_len =
11486 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
11488 /* Don't bother trying to do a partial substitution with
11489 incomplete packs; we'll try again after deduction. */
11490 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
11491 return t;
11493 if (len < 0)
11494 len = my_len;
11495 else if (len != my_len)
11497 if (!(complain & tf_error))
11498 /* Fail quietly. */;
11499 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
11500 error ("mismatched argument pack lengths while expanding %qT",
11501 pattern);
11502 else
11503 error ("mismatched argument pack lengths while expanding %qE",
11504 pattern);
11505 return error_mark_node;
11508 /* Keep track of the parameter packs and their corresponding
11509 argument packs. */
11510 packs = tree_cons (parm_pack, arg_pack, packs);
11511 TREE_TYPE (packs) = orig_arg;
11513 else
11515 /* We can't substitute for this parameter pack. We use a flag as
11516 well as the missing_level counter because function parameter
11517 packs don't have a level. */
11518 gcc_assert (processing_template_decl);
11519 unsubstituted_packs = true;
11523 /* If the expansion is just T..., return the matching argument pack, unless
11524 we need to call convert_from_reference on all the elements. This is an
11525 important optimization; see c++/68422. */
11526 if (!unsubstituted_packs
11527 && TREE_PURPOSE (packs) == pattern)
11529 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
11530 /* Types need no adjustment, nor does sizeof..., and if we still have
11531 some pack expansion args we won't do anything yet. */
11532 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
11533 || PACK_EXPANSION_SIZEOF_P (t)
11534 || pack_expansion_args_count (args))
11535 return args;
11536 /* Also optimize expression pack expansions if we can tell that the
11537 elements won't have reference type. */
11538 tree type = TREE_TYPE (pattern);
11539 if (type && TREE_CODE (type) != REFERENCE_TYPE
11540 && !PACK_EXPANSION_P (type)
11541 && !WILDCARD_TYPE_P (type))
11542 return args;
11543 /* Otherwise use the normal path so we get convert_from_reference. */
11546 /* We cannot expand this expansion expression, because we don't have
11547 all of the argument packs we need. */
11548 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
11550 /* We got some full packs, but we can't substitute them in until we
11551 have values for all the packs. So remember these until then. */
11553 t = make_pack_expansion (pattern);
11554 PACK_EXPANSION_EXTRA_ARGS (t) = args;
11555 return t;
11557 else if (unsubstituted_packs)
11559 /* There were no real arguments, we're just replacing a parameter
11560 pack with another version of itself. Substitute into the
11561 pattern and return a PACK_EXPANSION_*. The caller will need to
11562 deal with that. */
11563 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
11564 t = tsubst_expr (pattern, args, complain, in_decl,
11565 /*integral_constant_expression_p=*/false);
11566 else
11567 t = tsubst (pattern, args, complain, in_decl);
11568 t = make_pack_expansion (t);
11569 return t;
11572 gcc_assert (len >= 0);
11574 if (need_local_specializations)
11576 /* We're in a late-specified return type, so create our own local
11577 specializations map; the current map is either NULL or (in the
11578 case of recursive unification) might have bindings that we don't
11579 want to use or alter. */
11580 saved_local_specializations = local_specializations;
11581 local_specializations = new hash_map<tree, tree>;
11584 /* For each argument in each argument pack, substitute into the
11585 pattern. */
11586 result = make_tree_vec (len);
11587 tree elem_args = copy_template_args (args);
11588 for (i = 0; i < len; ++i)
11590 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
11592 elem_args, complain,
11593 in_decl);
11594 TREE_VEC_ELT (result, i) = t;
11595 if (t == error_mark_node)
11597 result = error_mark_node;
11598 break;
11602 /* Update ARGS to restore the substitution from parameter packs to
11603 their argument packs. */
11604 for (pack = packs; pack; pack = TREE_CHAIN (pack))
11606 tree parm = TREE_PURPOSE (pack);
11608 if (TREE_CODE (parm) == PARM_DECL
11609 || TREE_CODE (parm) == FIELD_DECL)
11610 register_local_specialization (TREE_TYPE (pack), parm);
11611 else
11613 int idx, level;
11615 if (TREE_VALUE (pack) == NULL_TREE)
11616 continue;
11618 template_parm_level_and_index (parm, &level, &idx);
11620 /* Update the corresponding argument. */
11621 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
11622 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
11623 TREE_TYPE (pack);
11624 else
11625 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
11629 if (need_local_specializations)
11631 delete local_specializations;
11632 local_specializations = saved_local_specializations;
11635 /* If the dependent pack arguments were such that we end up with only a
11636 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11637 if (len == 1 && TREE_CODE (result) == TREE_VEC
11638 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
11639 return TREE_VEC_ELT (result, 0);
11641 return result;
11644 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11645 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11646 parameter packs; all parms generated from a function parameter pack will
11647 have the same DECL_PARM_INDEX. */
11649 tree
11650 get_pattern_parm (tree parm, tree tmpl)
11652 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
11653 tree patparm;
11655 if (DECL_ARTIFICIAL (parm))
11657 for (patparm = DECL_ARGUMENTS (pattern);
11658 patparm; patparm = DECL_CHAIN (patparm))
11659 if (DECL_ARTIFICIAL (patparm)
11660 && DECL_NAME (parm) == DECL_NAME (patparm))
11661 break;
11663 else
11665 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
11666 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
11667 gcc_assert (DECL_PARM_INDEX (patparm)
11668 == DECL_PARM_INDEX (parm));
11671 return patparm;
11674 /* Make an argument pack out of the TREE_VEC VEC. */
11676 static tree
11677 make_argument_pack (tree vec)
11679 tree pack;
11680 tree elt = TREE_VEC_ELT (vec, 0);
11681 if (TYPE_P (elt))
11682 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
11683 else
11685 pack = make_node (NONTYPE_ARGUMENT_PACK);
11686 TREE_CONSTANT (pack) = 1;
11688 SET_ARGUMENT_PACK_ARGS (pack, vec);
11689 return pack;
11692 /* Return an exact copy of template args T that can be modified
11693 independently. */
11695 static tree
11696 copy_template_args (tree t)
11698 if (t == error_mark_node)
11699 return t;
11701 int len = TREE_VEC_LENGTH (t);
11702 tree new_vec = make_tree_vec (len);
11704 for (int i = 0; i < len; ++i)
11706 tree elt = TREE_VEC_ELT (t, i);
11707 if (elt && TREE_CODE (elt) == TREE_VEC)
11708 elt = copy_template_args (elt);
11709 TREE_VEC_ELT (new_vec, i) = elt;
11712 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
11713 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
11715 return new_vec;
11718 /* Substitute ARGS into the vector or list of template arguments T. */
11720 static tree
11721 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11723 tree orig_t = t;
11724 int len, need_new = 0, i, expanded_len_adjust = 0, out;
11725 tree *elts;
11727 if (t == error_mark_node)
11728 return error_mark_node;
11730 len = TREE_VEC_LENGTH (t);
11731 elts = XALLOCAVEC (tree, len);
11733 for (i = 0; i < len; i++)
11735 tree orig_arg = TREE_VEC_ELT (t, i);
11736 tree new_arg;
11738 if (TREE_CODE (orig_arg) == TREE_VEC)
11739 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
11740 else if (PACK_EXPANSION_P (orig_arg))
11742 /* Substitute into an expansion expression. */
11743 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
11745 if (TREE_CODE (new_arg) == TREE_VEC)
11746 /* Add to the expanded length adjustment the number of
11747 expanded arguments. We subtract one from this
11748 measurement, because the argument pack expression
11749 itself is already counted as 1 in
11750 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
11751 the argument pack is empty. */
11752 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
11754 else if (ARGUMENT_PACK_P (orig_arg))
11756 /* Substitute into each of the arguments. */
11757 new_arg = TYPE_P (orig_arg)
11758 ? cxx_make_type (TREE_CODE (orig_arg))
11759 : make_node (TREE_CODE (orig_arg));
11761 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
11762 args, complain, in_decl);
11763 if (pack_args == error_mark_node)
11764 new_arg = error_mark_node;
11765 else
11766 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
11768 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
11769 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
11771 else
11772 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
11774 if (new_arg == error_mark_node)
11775 return error_mark_node;
11777 elts[i] = new_arg;
11778 if (new_arg != orig_arg)
11779 need_new = 1;
11782 if (!need_new)
11783 return t;
11785 /* Make space for the expanded arguments coming from template
11786 argument packs. */
11787 t = make_tree_vec (len + expanded_len_adjust);
11788 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
11789 arguments for a member template.
11790 In that case each TREE_VEC in ORIG_T represents a level of template
11791 arguments, and ORIG_T won't carry any non defaulted argument count.
11792 It will rather be the nested TREE_VECs that will carry one.
11793 In other words, ORIG_T carries a non defaulted argument count only
11794 if it doesn't contain any nested TREE_VEC. */
11795 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
11797 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
11798 count += expanded_len_adjust;
11799 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
11801 for (i = 0, out = 0; i < len; i++)
11803 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
11804 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
11805 && TREE_CODE (elts[i]) == TREE_VEC)
11807 int idx;
11809 /* Now expand the template argument pack "in place". */
11810 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
11811 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
11813 else
11815 TREE_VEC_ELT (t, out) = elts[i];
11816 out++;
11820 return t;
11823 /* Substitute ARGS into one level PARMS of template parameters. */
11825 static tree
11826 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
11828 if (parms == error_mark_node)
11829 return error_mark_node;
11831 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
11833 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
11835 tree tuple = TREE_VEC_ELT (parms, i);
11837 if (tuple == error_mark_node)
11838 continue;
11840 TREE_VEC_ELT (new_vec, i) =
11841 tsubst_template_parm (tuple, args, complain);
11844 return new_vec;
11847 /* Return the result of substituting ARGS into the template parameters
11848 given by PARMS. If there are m levels of ARGS and m + n levels of
11849 PARMS, then the result will contain n levels of PARMS. For
11850 example, if PARMS is `template <class T> template <class U>
11851 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
11852 result will be `template <int*, double, class V>'. */
11854 static tree
11855 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
11857 tree r = NULL_TREE;
11858 tree* new_parms;
11860 /* When substituting into a template, we must set
11861 PROCESSING_TEMPLATE_DECL as the template parameters may be
11862 dependent if they are based on one-another, and the dependency
11863 predicates are short-circuit outside of templates. */
11864 ++processing_template_decl;
11866 for (new_parms = &r;
11867 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
11868 new_parms = &(TREE_CHAIN (*new_parms)),
11869 parms = TREE_CHAIN (parms))
11871 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
11872 args, complain);
11873 *new_parms =
11874 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
11875 - TMPL_ARGS_DEPTH (args)),
11876 new_vec, NULL_TREE);
11879 --processing_template_decl;
11881 return r;
11884 /* Return the result of substituting ARGS into one template parameter
11885 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
11886 parameter and which TREE_PURPOSE is the default argument of the
11887 template parameter. */
11889 static tree
11890 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
11892 tree default_value, parm_decl;
11894 if (args == NULL_TREE
11895 || t == NULL_TREE
11896 || t == error_mark_node)
11897 return t;
11899 gcc_assert (TREE_CODE (t) == TREE_LIST);
11901 default_value = TREE_PURPOSE (t);
11902 parm_decl = TREE_VALUE (t);
11904 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
11905 if (TREE_CODE (parm_decl) == PARM_DECL
11906 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
11907 parm_decl = error_mark_node;
11908 default_value = tsubst_template_arg (default_value, args,
11909 complain, NULL_TREE);
11911 return build_tree_list (default_value, parm_decl);
11914 /* Substitute the ARGS into the indicated aggregate (or enumeration)
11915 type T. If T is not an aggregate or enumeration type, it is
11916 handled as if by tsubst. IN_DECL is as for tsubst. If
11917 ENTERING_SCOPE is nonzero, T is the context for a template which
11918 we are presently tsubst'ing. Return the substituted value. */
11920 static tree
11921 tsubst_aggr_type (tree t,
11922 tree args,
11923 tsubst_flags_t complain,
11924 tree in_decl,
11925 int entering_scope)
11927 if (t == NULL_TREE)
11928 return NULL_TREE;
11930 switch (TREE_CODE (t))
11932 case RECORD_TYPE:
11933 if (TYPE_PTRMEMFUNC_P (t))
11934 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
11936 /* Fall through. */
11937 case ENUMERAL_TYPE:
11938 case UNION_TYPE:
11939 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
11941 tree argvec;
11942 tree context;
11943 tree r;
11944 int saved_unevaluated_operand;
11945 int saved_inhibit_evaluation_warnings;
11947 /* In "sizeof(X<I>)" we need to evaluate "I". */
11948 saved_unevaluated_operand = cp_unevaluated_operand;
11949 cp_unevaluated_operand = 0;
11950 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
11951 c_inhibit_evaluation_warnings = 0;
11953 /* First, determine the context for the type we are looking
11954 up. */
11955 context = TYPE_CONTEXT (t);
11956 if (context && TYPE_P (context))
11958 context = tsubst_aggr_type (context, args, complain,
11959 in_decl, /*entering_scope=*/1);
11960 /* If context is a nested class inside a class template,
11961 it may still need to be instantiated (c++/33959). */
11962 context = complete_type (context);
11965 /* Then, figure out what arguments are appropriate for the
11966 type we are trying to find. For example, given:
11968 template <class T> struct S;
11969 template <class T, class U> void f(T, U) { S<U> su; }
11971 and supposing that we are instantiating f<int, double>,
11972 then our ARGS will be {int, double}, but, when looking up
11973 S we only want {double}. */
11974 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
11975 complain, in_decl);
11976 if (argvec == error_mark_node)
11977 r = error_mark_node;
11978 else
11980 r = lookup_template_class (t, argvec, in_decl, context,
11981 entering_scope, complain);
11982 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
11985 cp_unevaluated_operand = saved_unevaluated_operand;
11986 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
11988 return r;
11990 else
11991 /* This is not a template type, so there's nothing to do. */
11992 return t;
11994 default:
11995 return tsubst (t, args, complain, in_decl);
11999 /* Substitute into the default argument ARG (a default argument for
12000 FN), which has the indicated TYPE. */
12002 tree
12003 tsubst_default_argument (tree fn, tree type, tree arg, tsubst_flags_t complain)
12005 tree saved_class_ptr = NULL_TREE;
12006 tree saved_class_ref = NULL_TREE;
12007 int errs = errorcount + sorrycount;
12009 /* This can happen in invalid code. */
12010 if (TREE_CODE (arg) == DEFAULT_ARG)
12011 return arg;
12013 /* This default argument came from a template. Instantiate the
12014 default argument here, not in tsubst. In the case of
12015 something like:
12017 template <class T>
12018 struct S {
12019 static T t();
12020 void f(T = t());
12023 we must be careful to do name lookup in the scope of S<T>,
12024 rather than in the current class. */
12025 push_access_scope (fn);
12026 /* The "this" pointer is not valid in a default argument. */
12027 if (cfun)
12029 saved_class_ptr = current_class_ptr;
12030 cp_function_chain->x_current_class_ptr = NULL_TREE;
12031 saved_class_ref = current_class_ref;
12032 cp_function_chain->x_current_class_ref = NULL_TREE;
12035 push_deferring_access_checks(dk_no_deferred);
12036 /* The default argument expression may cause implicitly defined
12037 member functions to be synthesized, which will result in garbage
12038 collection. We must treat this situation as if we were within
12039 the body of function so as to avoid collecting live data on the
12040 stack. */
12041 ++function_depth;
12042 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
12043 complain, NULL_TREE,
12044 /*integral_constant_expression_p=*/false);
12045 --function_depth;
12046 pop_deferring_access_checks();
12048 /* Restore the "this" pointer. */
12049 if (cfun)
12051 cp_function_chain->x_current_class_ptr = saved_class_ptr;
12052 cp_function_chain->x_current_class_ref = saved_class_ref;
12055 if (errorcount+sorrycount > errs
12056 && (complain & tf_warning_or_error))
12057 inform (input_location,
12058 " when instantiating default argument for call to %qD", fn);
12060 /* Make sure the default argument is reasonable. */
12061 arg = check_default_argument (type, arg, complain);
12063 pop_access_scope (fn);
12065 return arg;
12068 /* Substitute into all the default arguments for FN. */
12070 static void
12071 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
12073 tree arg;
12074 tree tmpl_args;
12076 tmpl_args = DECL_TI_ARGS (fn);
12078 /* If this function is not yet instantiated, we certainly don't need
12079 its default arguments. */
12080 if (uses_template_parms (tmpl_args))
12081 return;
12082 /* Don't do this again for clones. */
12083 if (DECL_CLONED_FUNCTION_P (fn))
12084 return;
12086 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
12087 arg;
12088 arg = TREE_CHAIN (arg))
12089 if (TREE_PURPOSE (arg))
12090 TREE_PURPOSE (arg) = tsubst_default_argument (fn,
12091 TREE_VALUE (arg),
12092 TREE_PURPOSE (arg),
12093 complain);
12096 /* Substitute the ARGS into the T, which is a _DECL. Return the
12097 result of the substitution. Issue error and warning messages under
12098 control of COMPLAIN. */
12100 static tree
12101 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
12103 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12104 location_t saved_loc;
12105 tree r = NULL_TREE;
12106 tree in_decl = t;
12107 hashval_t hash = 0;
12109 /* Set the filename and linenumber to improve error-reporting. */
12110 saved_loc = input_location;
12111 input_location = DECL_SOURCE_LOCATION (t);
12113 switch (TREE_CODE (t))
12115 case TEMPLATE_DECL:
12117 /* We can get here when processing a member function template,
12118 member class template, or template template parameter. */
12119 tree decl = DECL_TEMPLATE_RESULT (t);
12120 tree spec;
12121 tree tmpl_args;
12122 tree full_args;
12124 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12126 /* Template template parameter is treated here. */
12127 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12128 if (new_type == error_mark_node)
12129 r = error_mark_node;
12130 /* If we get a real template back, return it. This can happen in
12131 the context of most_specialized_partial_spec. */
12132 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
12133 r = new_type;
12134 else
12135 /* The new TEMPLATE_DECL was built in
12136 reduce_template_parm_level. */
12137 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
12138 break;
12141 /* We might already have an instance of this template.
12142 The ARGS are for the surrounding class type, so the
12143 full args contain the tsubst'd args for the context,
12144 plus the innermost args from the template decl. */
12145 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
12146 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
12147 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
12148 /* Because this is a template, the arguments will still be
12149 dependent, even after substitution. If
12150 PROCESSING_TEMPLATE_DECL is not set, the dependency
12151 predicates will short-circuit. */
12152 ++processing_template_decl;
12153 full_args = tsubst_template_args (tmpl_args, args,
12154 complain, in_decl);
12155 --processing_template_decl;
12156 if (full_args == error_mark_node)
12157 RETURN (error_mark_node);
12159 /* If this is a default template template argument,
12160 tsubst might not have changed anything. */
12161 if (full_args == tmpl_args)
12162 RETURN (t);
12164 hash = hash_tmpl_and_args (t, full_args);
12165 spec = retrieve_specialization (t, full_args, hash);
12166 if (spec != NULL_TREE)
12168 r = spec;
12169 break;
12172 /* Make a new template decl. It will be similar to the
12173 original, but will record the current template arguments.
12174 We also create a new function declaration, which is just
12175 like the old one, but points to this new template, rather
12176 than the old one. */
12177 r = copy_decl (t);
12178 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
12179 DECL_CHAIN (r) = NULL_TREE;
12181 // Build new template info linking to the original template decl.
12182 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12184 if (TREE_CODE (decl) == TYPE_DECL
12185 && !TYPE_DECL_ALIAS_P (decl))
12187 tree new_type;
12188 ++processing_template_decl;
12189 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12190 --processing_template_decl;
12191 if (new_type == error_mark_node)
12192 RETURN (error_mark_node);
12194 TREE_TYPE (r) = new_type;
12195 /* For a partial specialization, we need to keep pointing to
12196 the primary template. */
12197 if (!DECL_TEMPLATE_SPECIALIZATION (t))
12198 CLASSTYPE_TI_TEMPLATE (new_type) = r;
12199 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
12200 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
12201 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
12203 else
12205 tree new_decl;
12206 ++processing_template_decl;
12207 new_decl = tsubst (decl, args, complain, in_decl);
12208 --processing_template_decl;
12209 if (new_decl == error_mark_node)
12210 RETURN (error_mark_node);
12212 DECL_TEMPLATE_RESULT (r) = new_decl;
12213 DECL_TI_TEMPLATE (new_decl) = r;
12214 TREE_TYPE (r) = TREE_TYPE (new_decl);
12215 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
12216 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
12219 SET_DECL_IMPLICIT_INSTANTIATION (r);
12220 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
12221 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
12223 /* The template parameters for this new template are all the
12224 template parameters for the old template, except the
12225 outermost level of parameters. */
12226 DECL_TEMPLATE_PARMS (r)
12227 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
12228 complain);
12230 if (PRIMARY_TEMPLATE_P (t))
12231 DECL_PRIMARY_TEMPLATE (r) = r;
12233 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl))
12234 /* Record this non-type partial instantiation. */
12235 register_specialization (r, t,
12236 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
12237 false, hash);
12239 break;
12241 case FUNCTION_DECL:
12243 tree ctx;
12244 tree argvec = NULL_TREE;
12245 tree *friends;
12246 tree gen_tmpl;
12247 tree type;
12248 int member;
12249 int args_depth;
12250 int parms_depth;
12252 /* Nobody should be tsubst'ing into non-template functions. */
12253 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
12255 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
12257 tree spec;
12259 /* If T is not dependent, just return it. */
12260 if (!uses_template_parms (DECL_TI_ARGS (t)))
12261 RETURN (t);
12263 /* Calculate the most general template of which R is a
12264 specialization, and the complete set of arguments used to
12265 specialize R. */
12266 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
12267 argvec = tsubst_template_args (DECL_TI_ARGS
12268 (DECL_TEMPLATE_RESULT
12269 (DECL_TI_TEMPLATE (t))),
12270 args, complain, in_decl);
12271 if (argvec == error_mark_node)
12272 RETURN (error_mark_node);
12274 /* Check to see if we already have this specialization. */
12275 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12276 spec = retrieve_specialization (gen_tmpl, argvec, hash);
12278 if (spec)
12280 r = spec;
12281 break;
12284 /* We can see more levels of arguments than parameters if
12285 there was a specialization of a member template, like
12286 this:
12288 template <class T> struct S { template <class U> void f(); }
12289 template <> template <class U> void S<int>::f(U);
12291 Here, we'll be substituting into the specialization,
12292 because that's where we can find the code we actually
12293 want to generate, but we'll have enough arguments for
12294 the most general template.
12296 We also deal with the peculiar case:
12298 template <class T> struct S {
12299 template <class U> friend void f();
12301 template <class U> void f() {}
12302 template S<int>;
12303 template void f<double>();
12305 Here, the ARGS for the instantiation of will be {int,
12306 double}. But, we only need as many ARGS as there are
12307 levels of template parameters in CODE_PATTERN. We are
12308 careful not to get fooled into reducing the ARGS in
12309 situations like:
12311 template <class T> struct S { template <class U> void f(U); }
12312 template <class T> template <> void S<T>::f(int) {}
12314 which we can spot because the pattern will be a
12315 specialization in this case. */
12316 args_depth = TMPL_ARGS_DEPTH (args);
12317 parms_depth =
12318 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
12319 if (args_depth > parms_depth
12320 && !DECL_TEMPLATE_SPECIALIZATION (t))
12321 args = get_innermost_template_args (args, parms_depth);
12323 else
12325 /* This special case arises when we have something like this:
12327 template <class T> struct S {
12328 friend void f<int>(int, double);
12331 Here, the DECL_TI_TEMPLATE for the friend declaration
12332 will be an IDENTIFIER_NODE. We are being called from
12333 tsubst_friend_function, and we want only to create a
12334 new decl (R) with appropriate types so that we can call
12335 determine_specialization. */
12336 gen_tmpl = NULL_TREE;
12339 if (DECL_CLASS_SCOPE_P (t))
12341 if (DECL_NAME (t) == constructor_name (DECL_CONTEXT (t)))
12342 member = 2;
12343 else
12344 member = 1;
12345 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args,
12346 complain, t, /*entering_scope=*/1);
12348 else
12350 member = 0;
12351 ctx = DECL_CONTEXT (t);
12353 type = tsubst (TREE_TYPE (t), args, complain|tf_fndecl_type, in_decl);
12354 if (type == error_mark_node)
12355 RETURN (error_mark_node);
12357 /* If we hit excessive deduction depth, the type is bogus even if
12358 it isn't error_mark_node, so don't build a decl. */
12359 if (excessive_deduction_depth)
12360 RETURN (error_mark_node);
12362 /* We do NOT check for matching decls pushed separately at this
12363 point, as they may not represent instantiations of this
12364 template, and in any case are considered separate under the
12365 discrete model. */
12366 r = copy_decl (t);
12367 DECL_USE_TEMPLATE (r) = 0;
12368 TREE_TYPE (r) = type;
12369 /* Clear out the mangled name and RTL for the instantiation. */
12370 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12371 SET_DECL_RTL (r, NULL);
12372 /* Leave DECL_INITIAL set on deleted instantiations. */
12373 if (!DECL_DELETED_FN (r))
12374 DECL_INITIAL (r) = NULL_TREE;
12375 DECL_CONTEXT (r) = ctx;
12377 /* OpenMP UDRs have the only argument a reference to the declared
12378 type. We want to diagnose if the declared type is a reference,
12379 which is invalid, but as references to references are usually
12380 quietly merged, diagnose it here. */
12381 if (DECL_OMP_DECLARE_REDUCTION_P (t))
12383 tree argtype
12384 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
12385 argtype = tsubst (argtype, args, complain, in_decl);
12386 if (TREE_CODE (argtype) == REFERENCE_TYPE)
12387 error_at (DECL_SOURCE_LOCATION (t),
12388 "reference type %qT in "
12389 "%<#pragma omp declare reduction%>", argtype);
12390 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
12391 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
12392 argtype);
12395 if (member && DECL_CONV_FN_P (r))
12396 /* Type-conversion operator. Reconstruct the name, in
12397 case it's the name of one of the template's parameters. */
12398 DECL_NAME (r) = mangle_conv_op_name_for_type (TREE_TYPE (type));
12400 DECL_ARGUMENTS (r) = tsubst (DECL_ARGUMENTS (t), args,
12401 complain, t);
12402 DECL_RESULT (r) = NULL_TREE;
12404 TREE_STATIC (r) = 0;
12405 TREE_PUBLIC (r) = TREE_PUBLIC (t);
12406 DECL_EXTERNAL (r) = 1;
12407 /* If this is an instantiation of a function with internal
12408 linkage, we already know what object file linkage will be
12409 assigned to the instantiation. */
12410 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
12411 DECL_DEFER_OUTPUT (r) = 0;
12412 DECL_CHAIN (r) = NULL_TREE;
12413 DECL_PENDING_INLINE_INFO (r) = 0;
12414 DECL_PENDING_INLINE_P (r) = 0;
12415 DECL_SAVED_TREE (r) = NULL_TREE;
12416 DECL_STRUCT_FUNCTION (r) = NULL;
12417 TREE_USED (r) = 0;
12418 /* We'll re-clone as appropriate in instantiate_template. */
12419 DECL_CLONED_FUNCTION (r) = NULL_TREE;
12421 /* If we aren't complaining now, return on error before we register
12422 the specialization so that we'll complain eventually. */
12423 if ((complain & tf_error) == 0
12424 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
12425 && !grok_op_properties (r, /*complain=*/false))
12426 RETURN (error_mark_node);
12428 /* When instantiating a constrained member, substitute
12429 into the constraints to create a new constraint. */
12430 if (tree ci = get_constraints (t))
12431 if (member)
12433 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
12434 set_constraints (r, ci);
12437 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12438 this in the special friend case mentioned above where
12439 GEN_TMPL is NULL. */
12440 if (gen_tmpl)
12442 DECL_TEMPLATE_INFO (r)
12443 = build_template_info (gen_tmpl, argvec);
12444 SET_DECL_IMPLICIT_INSTANTIATION (r);
12446 tree new_r
12447 = register_specialization (r, gen_tmpl, argvec, false, hash);
12448 if (new_r != r)
12449 /* We instantiated this while substituting into
12450 the type earlier (template/friend54.C). */
12451 RETURN (new_r);
12453 /* We're not supposed to instantiate default arguments
12454 until they are called, for a template. But, for a
12455 declaration like:
12457 template <class T> void f ()
12458 { extern void g(int i = T()); }
12460 we should do the substitution when the template is
12461 instantiated. We handle the member function case in
12462 instantiate_class_template since the default arguments
12463 might refer to other members of the class. */
12464 if (!member
12465 && !PRIMARY_TEMPLATE_P (gen_tmpl)
12466 && !uses_template_parms (argvec))
12467 tsubst_default_arguments (r, complain);
12469 else
12470 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12472 /* Copy the list of befriending classes. */
12473 for (friends = &DECL_BEFRIENDING_CLASSES (r);
12474 *friends;
12475 friends = &TREE_CHAIN (*friends))
12477 *friends = copy_node (*friends);
12478 TREE_VALUE (*friends) = tsubst (TREE_VALUE (*friends),
12479 args, complain,
12480 in_decl);
12483 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
12485 maybe_retrofit_in_chrg (r);
12486 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
12487 RETURN (error_mark_node);
12488 /* If this is an instantiation of a member template, clone it.
12489 If it isn't, that'll be handled by
12490 clone_constructors_and_destructors. */
12491 if (PRIMARY_TEMPLATE_P (gen_tmpl))
12492 clone_function_decl (r, /*update_methods=*/false);
12494 else if ((complain & tf_error) != 0
12495 && IDENTIFIER_OPNAME_P (DECL_NAME (r))
12496 && !grok_op_properties (r, /*complain=*/true))
12497 RETURN (error_mark_node);
12499 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
12500 SET_DECL_FRIEND_CONTEXT (r,
12501 tsubst (DECL_FRIEND_CONTEXT (t),
12502 args, complain, in_decl));
12504 /* Possibly limit visibility based on template args. */
12505 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12506 if (DECL_VISIBILITY_SPECIFIED (t))
12508 DECL_VISIBILITY_SPECIFIED (r) = 0;
12509 DECL_ATTRIBUTES (r)
12510 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12512 determine_visibility (r);
12513 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
12514 && !processing_template_decl)
12515 defaulted_late_check (r);
12517 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12518 args, complain, in_decl);
12520 break;
12522 case PARM_DECL:
12524 tree type = NULL_TREE;
12525 int i, len = 1;
12526 tree expanded_types = NULL_TREE;
12527 tree prev_r = NULL_TREE;
12528 tree first_r = NULL_TREE;
12530 if (DECL_PACK_P (t))
12532 /* If there is a local specialization that isn't a
12533 parameter pack, it means that we're doing a "simple"
12534 substitution from inside tsubst_pack_expansion. Just
12535 return the local specialization (which will be a single
12536 parm). */
12537 tree spec = retrieve_local_specialization (t);
12538 if (spec
12539 && TREE_CODE (spec) == PARM_DECL
12540 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
12541 RETURN (spec);
12543 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12544 the parameters in this function parameter pack. */
12545 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12546 complain, in_decl);
12547 if (TREE_CODE (expanded_types) == TREE_VEC)
12549 len = TREE_VEC_LENGTH (expanded_types);
12551 /* Zero-length parameter packs are boring. Just substitute
12552 into the chain. */
12553 if (len == 0)
12554 RETURN (tsubst (TREE_CHAIN (t), args, complain,
12555 TREE_CHAIN (t)));
12557 else
12559 /* All we did was update the type. Make a note of that. */
12560 type = expanded_types;
12561 expanded_types = NULL_TREE;
12565 /* Loop through all of the parameters we'll build. When T is
12566 a function parameter pack, LEN is the number of expanded
12567 types in EXPANDED_TYPES; otherwise, LEN is 1. */
12568 r = NULL_TREE;
12569 for (i = 0; i < len; ++i)
12571 prev_r = r;
12572 r = copy_node (t);
12573 if (DECL_TEMPLATE_PARM_P (t))
12574 SET_DECL_TEMPLATE_PARM_P (r);
12576 if (expanded_types)
12577 /* We're on the Ith parameter of the function parameter
12578 pack. */
12580 /* Get the Ith type. */
12581 type = TREE_VEC_ELT (expanded_types, i);
12583 /* Rename the parameter to include the index. */
12584 DECL_NAME (r)
12585 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12587 else if (!type)
12588 /* We're dealing with a normal parameter. */
12589 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12591 type = type_decays_to (type);
12592 TREE_TYPE (r) = type;
12593 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12595 if (DECL_INITIAL (r))
12597 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
12598 DECL_INITIAL (r) = TREE_TYPE (r);
12599 else
12600 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
12601 complain, in_decl);
12604 DECL_CONTEXT (r) = NULL_TREE;
12606 if (!DECL_TEMPLATE_PARM_P (r))
12607 DECL_ARG_TYPE (r) = type_passed_as (type);
12609 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12610 args, complain, in_decl);
12612 /* Keep track of the first new parameter we
12613 generate. That's what will be returned to the
12614 caller. */
12615 if (!first_r)
12616 first_r = r;
12618 /* Build a proper chain of parameters when substituting
12619 into a function parameter pack. */
12620 if (prev_r)
12621 DECL_CHAIN (prev_r) = r;
12624 /* If cp_unevaluated_operand is set, we're just looking for a
12625 single dummy parameter, so don't keep going. */
12626 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
12627 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
12628 complain, DECL_CHAIN (t));
12630 /* FIRST_R contains the start of the chain we've built. */
12631 r = first_r;
12633 break;
12635 case FIELD_DECL:
12637 tree type = NULL_TREE;
12638 tree vec = NULL_TREE;
12639 tree expanded_types = NULL_TREE;
12640 int len = 1;
12642 if (PACK_EXPANSION_P (TREE_TYPE (t)))
12644 /* This field is a lambda capture pack. Return a TREE_VEC of
12645 the expanded fields to instantiate_class_template_1 and
12646 store them in the specializations hash table as a
12647 NONTYPE_ARGUMENT_PACK so that tsubst_copy can find them. */
12648 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12649 complain, in_decl);
12650 if (TREE_CODE (expanded_types) == TREE_VEC)
12652 len = TREE_VEC_LENGTH (expanded_types);
12653 vec = make_tree_vec (len);
12655 else
12657 /* All we did was update the type. Make a note of that. */
12658 type = expanded_types;
12659 expanded_types = NULL_TREE;
12663 for (int i = 0; i < len; ++i)
12665 r = copy_decl (t);
12666 if (expanded_types)
12668 type = TREE_VEC_ELT (expanded_types, i);
12669 DECL_NAME (r)
12670 = make_ith_pack_parameter_name (DECL_NAME (r), i);
12672 else if (!type)
12673 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12675 if (type == error_mark_node)
12676 RETURN (error_mark_node);
12677 TREE_TYPE (r) = type;
12678 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12680 if (DECL_C_BIT_FIELD (r))
12681 /* For bit-fields, DECL_INITIAL gives the number of bits. For
12682 non-bit-fields DECL_INITIAL is a non-static data member
12683 initializer, which gets deferred instantiation. */
12684 DECL_INITIAL (r)
12685 = tsubst_expr (DECL_INITIAL (t), args,
12686 complain, in_decl,
12687 /*integral_constant_expression_p=*/true);
12688 else if (DECL_INITIAL (t))
12690 /* Set up DECL_TEMPLATE_INFO so that we can get at the
12691 NSDMI in perform_member_init. Still set DECL_INITIAL
12692 so that we know there is one. */
12693 DECL_INITIAL (r) = void_node;
12694 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
12695 retrofit_lang_decl (r);
12696 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12698 /* We don't have to set DECL_CONTEXT here; it is set by
12699 finish_member_declaration. */
12700 DECL_CHAIN (r) = NULL_TREE;
12702 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12703 args, complain, in_decl);
12705 if (vec)
12706 TREE_VEC_ELT (vec, i) = r;
12709 if (vec)
12711 r = vec;
12712 tree pack = make_node (NONTYPE_ARGUMENT_PACK);
12713 SET_ARGUMENT_PACK_ARGS (pack, vec);
12714 register_specialization (pack, t, args, false, 0);
12717 break;
12719 case USING_DECL:
12720 /* We reach here only for member using decls. We also need to check
12721 uses_template_parms because DECL_DEPENDENT_P is not set for a
12722 using-declaration that designates a member of the current
12723 instantiation (c++/53549). */
12724 if (DECL_DEPENDENT_P (t)
12725 || uses_template_parms (USING_DECL_SCOPE (t)))
12727 tree scope = USING_DECL_SCOPE (t);
12728 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
12729 if (PACK_EXPANSION_P (scope))
12731 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
12732 int len = TREE_VEC_LENGTH (vec);
12733 r = make_tree_vec (len);
12734 for (int i = 0; i < len; ++i)
12736 tree escope = TREE_VEC_ELT (vec, i);
12737 tree elt = do_class_using_decl (escope, name);
12738 if (!elt)
12740 r = error_mark_node;
12741 break;
12743 else
12745 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
12746 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
12748 TREE_VEC_ELT (r, i) = elt;
12751 else
12753 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
12754 complain, in_decl);
12755 r = do_class_using_decl (inst_scope, name);
12756 if (!r)
12757 r = error_mark_node;
12758 else
12760 TREE_PROTECTED (r) = TREE_PROTECTED (t);
12761 TREE_PRIVATE (r) = TREE_PRIVATE (t);
12765 else
12767 r = copy_node (t);
12768 DECL_CHAIN (r) = NULL_TREE;
12770 break;
12772 case TYPE_DECL:
12773 case VAR_DECL:
12775 tree argvec = NULL_TREE;
12776 tree gen_tmpl = NULL_TREE;
12777 tree spec;
12778 tree tmpl = NULL_TREE;
12779 tree ctx;
12780 tree type = NULL_TREE;
12781 bool local_p;
12783 if (TREE_TYPE (t) == error_mark_node)
12784 RETURN (error_mark_node);
12786 if (TREE_CODE (t) == TYPE_DECL
12787 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
12789 /* If this is the canonical decl, we don't have to
12790 mess with instantiations, and often we can't (for
12791 typename, template type parms and such). Note that
12792 TYPE_NAME is not correct for the above test if
12793 we've copied the type for a typedef. */
12794 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12795 if (type == error_mark_node)
12796 RETURN (error_mark_node);
12797 r = TYPE_NAME (type);
12798 break;
12801 /* Check to see if we already have the specialization we
12802 need. */
12803 spec = NULL_TREE;
12804 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
12806 /* T is a static data member or namespace-scope entity.
12807 We have to substitute into namespace-scope variables
12808 (not just variable templates) because of cases like:
12810 template <class T> void f() { extern T t; }
12812 where the entity referenced is not known until
12813 instantiation time. */
12814 local_p = false;
12815 ctx = DECL_CONTEXT (t);
12816 if (DECL_CLASS_SCOPE_P (t))
12818 ctx = tsubst_aggr_type (ctx, args,
12819 complain,
12820 in_decl, /*entering_scope=*/1);
12821 /* If CTX is unchanged, then T is in fact the
12822 specialization we want. That situation occurs when
12823 referencing a static data member within in its own
12824 class. We can use pointer equality, rather than
12825 same_type_p, because DECL_CONTEXT is always
12826 canonical... */
12827 if (ctx == DECL_CONTEXT (t)
12828 /* ... unless T is a member template; in which
12829 case our caller can be willing to create a
12830 specialization of that template represented
12831 by T. */
12832 && !(DECL_TI_TEMPLATE (t)
12833 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
12834 spec = t;
12837 if (!spec)
12839 tmpl = DECL_TI_TEMPLATE (t);
12840 gen_tmpl = most_general_template (tmpl);
12841 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
12842 if (argvec != error_mark_node)
12843 argvec = (coerce_innermost_template_parms
12844 (DECL_TEMPLATE_PARMS (gen_tmpl),
12845 argvec, t, complain,
12846 /*all*/true, /*defarg*/true));
12847 if (argvec == error_mark_node)
12848 RETURN (error_mark_node);
12849 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12850 spec = retrieve_specialization (gen_tmpl, argvec, hash);
12853 else
12855 /* A local variable. */
12856 local_p = true;
12857 /* Subsequent calls to pushdecl will fill this in. */
12858 ctx = NULL_TREE;
12859 /* Unless this is a reference to a static variable from an
12860 enclosing function, in which case we need to fill it in now. */
12861 if (TREE_STATIC (t))
12863 tree fn = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
12864 if (fn != current_function_decl)
12865 ctx = fn;
12867 spec = retrieve_local_specialization (t);
12869 /* If we already have the specialization we need, there is
12870 nothing more to do. */
12871 if (spec)
12873 r = spec;
12874 break;
12877 /* Create a new node for the specialization we need. */
12878 r = copy_decl (t);
12879 if (type == NULL_TREE)
12881 if (is_typedef_decl (t))
12882 type = DECL_ORIGINAL_TYPE (t);
12883 else
12884 type = TREE_TYPE (t);
12885 if (VAR_P (t)
12886 && VAR_HAD_UNKNOWN_BOUND (t)
12887 && type != error_mark_node)
12888 type = strip_array_domain (type);
12889 type = tsubst (type, args, complain, in_decl);
12891 if (VAR_P (r))
12893 /* Even if the original location is out of scope, the
12894 newly substituted one is not. */
12895 DECL_DEAD_FOR_LOCAL (r) = 0;
12896 DECL_INITIALIZED_P (r) = 0;
12897 DECL_TEMPLATE_INSTANTIATED (r) = 0;
12898 if (type == error_mark_node)
12899 RETURN (error_mark_node);
12900 if (TREE_CODE (type) == FUNCTION_TYPE)
12902 /* It may seem that this case cannot occur, since:
12904 typedef void f();
12905 void g() { f x; }
12907 declares a function, not a variable. However:
12909 typedef void f();
12910 template <typename T> void g() { T t; }
12911 template void g<f>();
12913 is an attempt to declare a variable with function
12914 type. */
12915 error ("variable %qD has function type",
12916 /* R is not yet sufficiently initialized, so we
12917 just use its name. */
12918 DECL_NAME (r));
12919 RETURN (error_mark_node);
12921 type = complete_type (type);
12922 /* Wait until cp_finish_decl to set this again, to handle
12923 circular dependency (template/instantiate6.C). */
12924 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
12925 type = check_var_type (DECL_NAME (r), type);
12927 if (DECL_HAS_VALUE_EXPR_P (t))
12929 tree ve = DECL_VALUE_EXPR (t);
12930 ve = tsubst_expr (ve, args, complain, in_decl,
12931 /*constant_expression_p=*/false);
12932 if (REFERENCE_REF_P (ve))
12934 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
12935 ve = TREE_OPERAND (ve, 0);
12937 SET_DECL_VALUE_EXPR (r, ve);
12939 if (CP_DECL_THREAD_LOCAL_P (r)
12940 && !processing_template_decl)
12941 set_decl_tls_model (r, decl_default_tls_model (r));
12943 else if (DECL_SELF_REFERENCE_P (t))
12944 SET_DECL_SELF_REFERENCE_P (r);
12945 TREE_TYPE (r) = type;
12946 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
12947 DECL_CONTEXT (r) = ctx;
12948 /* Clear out the mangled name and RTL for the instantiation. */
12949 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12950 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
12951 SET_DECL_RTL (r, NULL);
12952 /* The initializer must not be expanded until it is required;
12953 see [temp.inst]. */
12954 DECL_INITIAL (r) = NULL_TREE;
12955 if (VAR_P (r))
12956 SET_DECL_MODE (r, VOIDmode);
12957 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
12958 SET_DECL_RTL (r, NULL);
12959 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
12960 if (VAR_P (r))
12962 /* Possibly limit visibility based on template args. */
12963 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12964 if (DECL_VISIBILITY_SPECIFIED (t))
12966 DECL_VISIBILITY_SPECIFIED (r) = 0;
12967 DECL_ATTRIBUTES (r)
12968 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12970 determine_visibility (r);
12973 if (!local_p)
12975 /* A static data member declaration is always marked
12976 external when it is declared in-class, even if an
12977 initializer is present. We mimic the non-template
12978 processing here. */
12979 DECL_EXTERNAL (r) = 1;
12980 if (DECL_NAMESPACE_SCOPE_P (t))
12981 DECL_NOT_REALLY_EXTERN (r) = 1;
12983 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
12984 SET_DECL_IMPLICIT_INSTANTIATION (r);
12985 register_specialization (r, gen_tmpl, argvec, false, hash);
12987 else
12989 if (DECL_LANG_SPECIFIC (r))
12990 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12991 if (!cp_unevaluated_operand)
12992 register_local_specialization (r, t);
12995 DECL_CHAIN (r) = NULL_TREE;
12997 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
12998 /*flags=*/0,
12999 args, complain, in_decl);
13001 /* Preserve a typedef that names a type. */
13002 if (is_typedef_decl (r) && type != error_mark_node)
13004 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
13005 set_underlying_type (r);
13006 if (TYPE_DECL_ALIAS_P (r))
13007 /* An alias template specialization can be dependent
13008 even if its underlying type is not. */
13009 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
13012 layout_decl (r, 0);
13014 break;
13016 default:
13017 gcc_unreachable ();
13019 #undef RETURN
13021 out:
13022 /* Restore the file and line information. */
13023 input_location = saved_loc;
13025 return r;
13028 /* Substitute into the ARG_TYPES of a function type.
13029 If END is a TREE_CHAIN, leave it and any following types
13030 un-substituted. */
13032 static tree
13033 tsubst_arg_types (tree arg_types,
13034 tree args,
13035 tree end,
13036 tsubst_flags_t complain,
13037 tree in_decl)
13039 tree remaining_arg_types;
13040 tree type = NULL_TREE;
13041 int i = 1;
13042 tree expanded_args = NULL_TREE;
13043 tree default_arg;
13045 if (!arg_types || arg_types == void_list_node || arg_types == end)
13046 return arg_types;
13048 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
13049 args, end, complain, in_decl);
13050 if (remaining_arg_types == error_mark_node)
13051 return error_mark_node;
13053 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
13055 /* For a pack expansion, perform substitution on the
13056 entire expression. Later on, we'll handle the arguments
13057 one-by-one. */
13058 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
13059 args, complain, in_decl);
13061 if (TREE_CODE (expanded_args) == TREE_VEC)
13062 /* So that we'll spin through the parameters, one by one. */
13063 i = TREE_VEC_LENGTH (expanded_args);
13064 else
13066 /* We only partially substituted into the parameter
13067 pack. Our type is TYPE_PACK_EXPANSION. */
13068 type = expanded_args;
13069 expanded_args = NULL_TREE;
13073 while (i > 0) {
13074 --i;
13076 if (expanded_args)
13077 type = TREE_VEC_ELT (expanded_args, i);
13078 else if (!type)
13079 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
13081 if (type == error_mark_node)
13082 return error_mark_node;
13083 if (VOID_TYPE_P (type))
13085 if (complain & tf_error)
13087 error ("invalid parameter type %qT", type);
13088 if (in_decl)
13089 error ("in declaration %q+D", in_decl);
13091 return error_mark_node;
13093 /* DR 657. */
13094 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
13095 return error_mark_node;
13097 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13098 top-level qualifiers as required. */
13099 type = cv_unqualified (type_decays_to (type));
13101 /* We do not substitute into default arguments here. The standard
13102 mandates that they be instantiated only when needed, which is
13103 done in build_over_call. */
13104 default_arg = TREE_PURPOSE (arg_types);
13106 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
13108 /* We've instantiated a template before its default arguments
13109 have been parsed. This can happen for a nested template
13110 class, and is not an error unless we require the default
13111 argument in a call of this function. */
13112 remaining_arg_types =
13113 tree_cons (default_arg, type, remaining_arg_types);
13114 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
13116 else
13117 remaining_arg_types =
13118 hash_tree_cons (default_arg, type, remaining_arg_types);
13121 return remaining_arg_types;
13124 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13125 *not* handle the exception-specification for FNTYPE, because the
13126 initial substitution of explicitly provided template parameters
13127 during argument deduction forbids substitution into the
13128 exception-specification:
13130 [temp.deduct]
13132 All references in the function type of the function template to the
13133 corresponding template parameters are replaced by the specified tem-
13134 plate argument values. If a substitution in a template parameter or
13135 in the function type of the function template results in an invalid
13136 type, type deduction fails. [Note: The equivalent substitution in
13137 exception specifications is done only when the function is instanti-
13138 ated, at which point a program is ill-formed if the substitution
13139 results in an invalid type.] */
13141 static tree
13142 tsubst_function_type (tree t,
13143 tree args,
13144 tsubst_flags_t complain,
13145 tree in_decl)
13147 tree return_type;
13148 tree arg_types = NULL_TREE;
13149 tree fntype;
13151 /* The TYPE_CONTEXT is not used for function/method types. */
13152 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
13154 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13155 failure. */
13156 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13158 if (late_return_type_p)
13160 /* Substitute the argument types. */
13161 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13162 complain, in_decl);
13163 if (arg_types == error_mark_node)
13164 return error_mark_node;
13166 tree save_ccp = current_class_ptr;
13167 tree save_ccr = current_class_ref;
13168 tree this_type = (TREE_CODE (t) == METHOD_TYPE
13169 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
13170 bool do_inject = this_type && CLASS_TYPE_P (this_type);
13171 if (do_inject)
13173 /* DR 1207: 'this' is in scope in the trailing return type. */
13174 inject_this_parameter (this_type, cp_type_quals (this_type));
13177 /* Substitute the return type. */
13178 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13180 if (do_inject)
13182 current_class_ptr = save_ccp;
13183 current_class_ref = save_ccr;
13186 else
13187 /* Substitute the return type. */
13188 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13190 if (return_type == error_mark_node)
13191 return error_mark_node;
13192 /* DR 486 clarifies that creation of a function type with an
13193 invalid return type is a deduction failure. */
13194 if (TREE_CODE (return_type) == ARRAY_TYPE
13195 || TREE_CODE (return_type) == FUNCTION_TYPE)
13197 if (complain & tf_error)
13199 if (TREE_CODE (return_type) == ARRAY_TYPE)
13200 error ("function returning an array");
13201 else
13202 error ("function returning a function");
13204 return error_mark_node;
13206 /* And DR 657. */
13207 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
13208 return error_mark_node;
13210 if (!late_return_type_p)
13212 /* Substitute the argument types. */
13213 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13214 complain, in_decl);
13215 if (arg_types == error_mark_node)
13216 return error_mark_node;
13219 /* Construct a new type node and return it. */
13220 if (TREE_CODE (t) == FUNCTION_TYPE)
13222 fntype = build_function_type (return_type, arg_types);
13223 fntype = apply_memfn_quals (fntype,
13224 type_memfn_quals (t),
13225 type_memfn_rqual (t));
13227 else
13229 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13230 /* Don't pick up extra function qualifiers from the basetype. */
13231 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13232 if (! MAYBE_CLASS_TYPE_P (r))
13234 /* [temp.deduct]
13236 Type deduction may fail for any of the following
13237 reasons:
13239 -- Attempting to create "pointer to member of T" when T
13240 is not a class type. */
13241 if (complain & tf_error)
13242 error ("creating pointer to member function of non-class type %qT",
13244 return error_mark_node;
13247 fntype = build_method_type_directly (r, return_type,
13248 TREE_CHAIN (arg_types));
13249 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
13251 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
13253 if (late_return_type_p)
13254 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
13256 return fntype;
13259 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
13260 ARGS into that specification, and return the substituted
13261 specification. If there is no specification, return NULL_TREE. */
13263 static tree
13264 tsubst_exception_specification (tree fntype,
13265 tree args,
13266 tsubst_flags_t complain,
13267 tree in_decl,
13268 bool defer_ok)
13270 tree specs;
13271 tree new_specs;
13273 specs = TYPE_RAISES_EXCEPTIONS (fntype);
13274 new_specs = NULL_TREE;
13275 if (specs && TREE_PURPOSE (specs))
13277 /* A noexcept-specifier. */
13278 tree expr = TREE_PURPOSE (specs);
13279 if (TREE_CODE (expr) == INTEGER_CST)
13280 new_specs = expr;
13281 else if (defer_ok)
13283 /* Defer instantiation of noexcept-specifiers to avoid
13284 excessive instantiations (c++/49107). */
13285 new_specs = make_node (DEFERRED_NOEXCEPT);
13286 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
13288 /* We already partially instantiated this member template,
13289 so combine the new args with the old. */
13290 DEFERRED_NOEXCEPT_PATTERN (new_specs)
13291 = DEFERRED_NOEXCEPT_PATTERN (expr);
13292 DEFERRED_NOEXCEPT_ARGS (new_specs)
13293 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
13295 else
13297 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
13298 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
13301 else
13302 new_specs = tsubst_copy_and_build
13303 (expr, args, complain, in_decl, /*function_p=*/false,
13304 /*integral_constant_expression_p=*/true);
13305 new_specs = build_noexcept_spec (new_specs, complain);
13307 else if (specs)
13309 if (! TREE_VALUE (specs))
13310 new_specs = specs;
13311 else
13312 while (specs)
13314 tree spec;
13315 int i, len = 1;
13316 tree expanded_specs = NULL_TREE;
13318 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
13320 /* Expand the pack expansion type. */
13321 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
13322 args, complain,
13323 in_decl);
13325 if (expanded_specs == error_mark_node)
13326 return error_mark_node;
13327 else if (TREE_CODE (expanded_specs) == TREE_VEC)
13328 len = TREE_VEC_LENGTH (expanded_specs);
13329 else
13331 /* We're substituting into a member template, so
13332 we got a TYPE_PACK_EXPANSION back. Add that
13333 expansion and move on. */
13334 gcc_assert (TREE_CODE (expanded_specs)
13335 == TYPE_PACK_EXPANSION);
13336 new_specs = add_exception_specifier (new_specs,
13337 expanded_specs,
13338 complain);
13339 specs = TREE_CHAIN (specs);
13340 continue;
13344 for (i = 0; i < len; ++i)
13346 if (expanded_specs)
13347 spec = TREE_VEC_ELT (expanded_specs, i);
13348 else
13349 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
13350 if (spec == error_mark_node)
13351 return spec;
13352 new_specs = add_exception_specifier (new_specs, spec,
13353 complain);
13356 specs = TREE_CHAIN (specs);
13359 return new_specs;
13362 /* Take the tree structure T and replace template parameters used
13363 therein with the argument vector ARGS. IN_DECL is an associated
13364 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
13365 Issue error and warning messages under control of COMPLAIN. Note
13366 that we must be relatively non-tolerant of extensions here, in
13367 order to preserve conformance; if we allow substitutions that
13368 should not be allowed, we may allow argument deductions that should
13369 not succeed, and therefore report ambiguous overload situations
13370 where there are none. In theory, we could allow the substitution,
13371 but indicate that it should have failed, and allow our caller to
13372 make sure that the right thing happens, but we don't try to do this
13373 yet.
13375 This function is used for dealing with types, decls and the like;
13376 for expressions, use tsubst_expr or tsubst_copy. */
13378 tree
13379 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13381 enum tree_code code;
13382 tree type, r = NULL_TREE;
13384 if (t == NULL_TREE || t == error_mark_node
13385 || t == integer_type_node
13386 || t == void_type_node
13387 || t == char_type_node
13388 || t == unknown_type_node
13389 || TREE_CODE (t) == NAMESPACE_DECL
13390 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
13391 return t;
13393 if (DECL_P (t))
13394 return tsubst_decl (t, args, complain);
13396 if (args == NULL_TREE)
13397 return t;
13399 code = TREE_CODE (t);
13401 if (code == IDENTIFIER_NODE)
13402 type = IDENTIFIER_TYPE_VALUE (t);
13403 else
13404 type = TREE_TYPE (t);
13406 gcc_assert (type != unknown_type_node);
13408 /* Reuse typedefs. We need to do this to handle dependent attributes,
13409 such as attribute aligned. */
13410 if (TYPE_P (t)
13411 && typedef_variant_p (t))
13413 tree decl = TYPE_NAME (t);
13415 if (alias_template_specialization_p (t))
13417 /* DECL represents an alias template and we want to
13418 instantiate it. */
13419 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13420 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13421 r = instantiate_alias_template (tmpl, gen_args, complain);
13423 else if (DECL_CLASS_SCOPE_P (decl)
13424 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
13425 && uses_template_parms (DECL_CONTEXT (decl)))
13427 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13428 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13429 r = retrieve_specialization (tmpl, gen_args, 0);
13431 else if (DECL_FUNCTION_SCOPE_P (decl)
13432 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
13433 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
13434 r = retrieve_local_specialization (decl);
13435 else
13436 /* The typedef is from a non-template context. */
13437 return t;
13439 if (r)
13441 r = TREE_TYPE (r);
13442 r = cp_build_qualified_type_real
13443 (r, cp_type_quals (t) | cp_type_quals (r),
13444 complain | tf_ignore_bad_quals);
13445 return r;
13447 else
13449 /* We don't have an instantiation yet, so drop the typedef. */
13450 int quals = cp_type_quals (t);
13451 t = DECL_ORIGINAL_TYPE (decl);
13452 t = cp_build_qualified_type_real (t, quals,
13453 complain | tf_ignore_bad_quals);
13457 bool fndecl_type = (complain & tf_fndecl_type);
13458 complain &= ~tf_fndecl_type;
13460 if (type
13461 && code != TYPENAME_TYPE
13462 && code != TEMPLATE_TYPE_PARM
13463 && code != TEMPLATE_PARM_INDEX
13464 && code != IDENTIFIER_NODE
13465 && code != FUNCTION_TYPE
13466 && code != METHOD_TYPE)
13467 type = tsubst (type, args, complain, in_decl);
13468 if (type == error_mark_node)
13469 return error_mark_node;
13471 switch (code)
13473 case RECORD_TYPE:
13474 case UNION_TYPE:
13475 case ENUMERAL_TYPE:
13476 return tsubst_aggr_type (t, args, complain, in_decl,
13477 /*entering_scope=*/0);
13479 case ERROR_MARK:
13480 case IDENTIFIER_NODE:
13481 case VOID_TYPE:
13482 case REAL_TYPE:
13483 case COMPLEX_TYPE:
13484 case VECTOR_TYPE:
13485 case BOOLEAN_TYPE:
13486 case NULLPTR_TYPE:
13487 case LANG_TYPE:
13488 return t;
13490 case INTEGER_TYPE:
13491 if (t == integer_type_node)
13492 return t;
13494 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
13495 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
13496 return t;
13499 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
13501 max = tsubst_expr (omax, args, complain, in_decl,
13502 /*integral_constant_expression_p=*/false);
13504 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13505 needed. */
13506 if (TREE_CODE (max) == NOP_EXPR
13507 && TREE_SIDE_EFFECTS (omax)
13508 && !TREE_TYPE (max))
13509 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
13511 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13512 with TREE_SIDE_EFFECTS that indicates this is not an integral
13513 constant expression. */
13514 if (processing_template_decl
13515 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
13517 gcc_assert (TREE_CODE (max) == NOP_EXPR);
13518 TREE_SIDE_EFFECTS (max) = 1;
13521 return compute_array_index_type (NULL_TREE, max, complain);
13524 case TEMPLATE_TYPE_PARM:
13525 case TEMPLATE_TEMPLATE_PARM:
13526 case BOUND_TEMPLATE_TEMPLATE_PARM:
13527 case TEMPLATE_PARM_INDEX:
13529 int idx;
13530 int level;
13531 int levels;
13532 tree arg = NULL_TREE;
13534 /* Early in template argument deduction substitution, we don't
13535 want to reduce the level of 'auto', or it will be confused
13536 with a normal template parm in subsequent deduction. */
13537 if (is_auto (t) && (complain & tf_partial))
13538 return t;
13540 r = NULL_TREE;
13542 gcc_assert (TREE_VEC_LENGTH (args) > 0);
13543 template_parm_level_and_index (t, &level, &idx);
13545 levels = TMPL_ARGS_DEPTH (args);
13546 if (level <= levels
13547 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
13549 arg = TMPL_ARG (args, level, idx);
13551 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
13553 /* See through ARGUMENT_PACK_SELECT arguments. */
13554 arg = ARGUMENT_PACK_SELECT_ARG (arg);
13555 /* If the selected argument is an expansion E, that most
13556 likely means we were called from
13557 gen_elem_of_pack_expansion_instantiation during the
13558 substituting of pack an argument pack (which Ith
13559 element is a pack expansion, where I is
13560 ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
13561 In this case, the Ith element resulting from this
13562 substituting is going to be a pack expansion, which
13563 pattern is the pattern of E. Let's return the
13564 pattern of E, and
13565 gen_elem_of_pack_expansion_instantiation will
13566 build the resulting pack expansion from it. */
13567 if (PACK_EXPANSION_P (arg))
13569 /* Make sure we aren't throwing away arg info. */
13570 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
13571 arg = PACK_EXPANSION_PATTERN (arg);
13576 if (arg == error_mark_node)
13577 return error_mark_node;
13578 else if (arg != NULL_TREE)
13580 if (ARGUMENT_PACK_P (arg))
13581 /* If ARG is an argument pack, we don't actually want to
13582 perform a substitution here, because substitutions
13583 for argument packs are only done
13584 element-by-element. We can get to this point when
13585 substituting the type of a non-type template
13586 parameter pack, when that type actually contains
13587 template parameter packs from an outer template, e.g.,
13589 template<typename... Types> struct A {
13590 template<Types... Values> struct B { };
13591 }; */
13592 return t;
13594 if (code == TEMPLATE_TYPE_PARM)
13596 int quals;
13597 gcc_assert (TYPE_P (arg));
13599 quals = cp_type_quals (arg) | cp_type_quals (t);
13601 return cp_build_qualified_type_real
13602 (arg, quals, complain | tf_ignore_bad_quals);
13604 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13606 /* We are processing a type constructed from a
13607 template template parameter. */
13608 tree argvec = tsubst (TYPE_TI_ARGS (t),
13609 args, complain, in_decl);
13610 if (argvec == error_mark_node)
13611 return error_mark_node;
13613 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
13614 || TREE_CODE (arg) == TEMPLATE_DECL
13615 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
13617 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
13618 /* Consider this code:
13620 template <template <class> class Template>
13621 struct Internal {
13622 template <class Arg> using Bind = Template<Arg>;
13625 template <template <class> class Template, class Arg>
13626 using Instantiate = Template<Arg>; //#0
13628 template <template <class> class Template,
13629 class Argument>
13630 using Bind =
13631 Instantiate<Internal<Template>::template Bind,
13632 Argument>; //#1
13634 When #1 is parsed, the
13635 BOUND_TEMPLATE_TEMPLATE_PARM representing the
13636 parameter `Template' in #0 matches the
13637 UNBOUND_CLASS_TEMPLATE representing the argument
13638 `Internal<Template>::template Bind'; We then want
13639 to assemble the type `Bind<Argument>' that can't
13640 be fully created right now, because
13641 `Internal<Template>' not being complete, the Bind
13642 template cannot be looked up in that context. So
13643 we need to "store" `Bind<Argument>' for later
13644 when the context of Bind becomes complete. Let's
13645 store that in a TYPENAME_TYPE. */
13646 return make_typename_type (TYPE_CONTEXT (arg),
13647 build_nt (TEMPLATE_ID_EXPR,
13648 TYPE_IDENTIFIER (arg),
13649 argvec),
13650 typename_type,
13651 complain);
13653 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
13654 are resolving nested-types in the signature of a
13655 member function templates. Otherwise ARG is a
13656 TEMPLATE_DECL and is the real template to be
13657 instantiated. */
13658 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
13659 arg = TYPE_NAME (arg);
13661 r = lookup_template_class (arg,
13662 argvec, in_decl,
13663 DECL_CONTEXT (arg),
13664 /*entering_scope=*/0,
13665 complain);
13666 return cp_build_qualified_type_real
13667 (r, cp_type_quals (t) | cp_type_quals (r), complain);
13669 else if (code == TEMPLATE_TEMPLATE_PARM)
13670 return arg;
13671 else
13672 /* TEMPLATE_PARM_INDEX. */
13673 return convert_from_reference (unshare_expr (arg));
13676 if (level == 1)
13677 /* This can happen during the attempted tsubst'ing in
13678 unify. This means that we don't yet have any information
13679 about the template parameter in question. */
13680 return t;
13682 /* If we get here, we must have been looking at a parm for a
13683 more deeply nested template. Make a new version of this
13684 template parameter, but with a lower level. */
13685 switch (code)
13687 case TEMPLATE_TYPE_PARM:
13688 case TEMPLATE_TEMPLATE_PARM:
13689 case BOUND_TEMPLATE_TEMPLATE_PARM:
13690 if (cp_type_quals (t))
13692 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
13693 r = cp_build_qualified_type_real
13694 (r, cp_type_quals (t),
13695 complain | (code == TEMPLATE_TYPE_PARM
13696 ? tf_ignore_bad_quals : 0));
13698 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
13699 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
13700 && (r = (TEMPLATE_PARM_DESCENDANTS
13701 (TEMPLATE_TYPE_PARM_INDEX (t))))
13702 && (r = TREE_TYPE (r))
13703 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
13704 /* Break infinite recursion when substituting the constraints
13705 of a constrained placeholder. */;
13706 else
13708 r = copy_type (t);
13709 TEMPLATE_TYPE_PARM_INDEX (r)
13710 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
13711 r, levels, args, complain);
13712 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
13713 TYPE_MAIN_VARIANT (r) = r;
13714 TYPE_POINTER_TO (r) = NULL_TREE;
13715 TYPE_REFERENCE_TO (r) = NULL_TREE;
13717 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
13719 /* Propagate constraints on placeholders. */
13720 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
13721 PLACEHOLDER_TYPE_CONSTRAINTS (r)
13722 = tsubst_constraint (constr, args, complain, in_decl);
13723 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
13725 if (DECL_TEMPLATE_TEMPLATE_PARM_P (pl))
13726 pl = tsubst (pl, args, complain, in_decl);
13727 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
13731 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
13732 /* We have reduced the level of the template
13733 template parameter, but not the levels of its
13734 template parameters, so canonical_type_parameter
13735 will not be able to find the canonical template
13736 template parameter for this level. Thus, we
13737 require structural equality checking to compare
13738 TEMPLATE_TEMPLATE_PARMs. */
13739 SET_TYPE_STRUCTURAL_EQUALITY (r);
13740 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
13741 SET_TYPE_STRUCTURAL_EQUALITY (r);
13742 else
13743 TYPE_CANONICAL (r) = canonical_type_parameter (r);
13745 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
13747 tree tinfo = TYPE_TEMPLATE_INFO (t);
13748 /* We might need to substitute into the types of non-type
13749 template parameters. */
13750 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
13751 complain, in_decl);
13752 if (tmpl == error_mark_node)
13753 return error_mark_node;
13754 tree argvec = tsubst (TI_ARGS (tinfo), args,
13755 complain, in_decl);
13756 if (argvec == error_mark_node)
13757 return error_mark_node;
13759 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
13760 = build_template_info (tmpl, argvec);
13763 break;
13765 case TEMPLATE_PARM_INDEX:
13766 /* OK, now substitute the type of the non-type parameter. We
13767 couldn't do it earlier because it might be an auto parameter,
13768 and we wouldn't need to if we had an argument. */
13769 type = tsubst (type, args, complain, in_decl);
13770 r = reduce_template_parm_level (t, type, levels, args, complain);
13771 break;
13773 default:
13774 gcc_unreachable ();
13777 return r;
13780 case TREE_LIST:
13782 tree purpose, value, chain;
13784 if (t == void_list_node)
13785 return t;
13787 purpose = TREE_PURPOSE (t);
13788 if (purpose)
13790 purpose = tsubst (purpose, args, complain, in_decl);
13791 if (purpose == error_mark_node)
13792 return error_mark_node;
13794 value = TREE_VALUE (t);
13795 if (value)
13797 value = tsubst (value, args, complain, in_decl);
13798 if (value == error_mark_node)
13799 return error_mark_node;
13801 chain = TREE_CHAIN (t);
13802 if (chain && chain != void_type_node)
13804 chain = tsubst (chain, args, complain, in_decl);
13805 if (chain == error_mark_node)
13806 return error_mark_node;
13808 if (purpose == TREE_PURPOSE (t)
13809 && value == TREE_VALUE (t)
13810 && chain == TREE_CHAIN (t))
13811 return t;
13812 return hash_tree_cons (purpose, value, chain);
13815 case TREE_BINFO:
13816 /* We should never be tsubsting a binfo. */
13817 gcc_unreachable ();
13819 case TREE_VEC:
13820 /* A vector of template arguments. */
13821 gcc_assert (!type);
13822 return tsubst_template_args (t, args, complain, in_decl);
13824 case POINTER_TYPE:
13825 case REFERENCE_TYPE:
13827 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
13828 return t;
13830 /* [temp.deduct]
13832 Type deduction may fail for any of the following
13833 reasons:
13835 -- Attempting to create a pointer to reference type.
13836 -- Attempting to create a reference to a reference type or
13837 a reference to void.
13839 Core issue 106 says that creating a reference to a reference
13840 during instantiation is no longer a cause for failure. We
13841 only enforce this check in strict C++98 mode. */
13842 if ((TREE_CODE (type) == REFERENCE_TYPE
13843 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
13844 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
13846 static location_t last_loc;
13848 /* We keep track of the last time we issued this error
13849 message to avoid spewing a ton of messages during a
13850 single bad template instantiation. */
13851 if (complain & tf_error
13852 && last_loc != input_location)
13854 if (VOID_TYPE_P (type))
13855 error ("forming reference to void");
13856 else if (code == POINTER_TYPE)
13857 error ("forming pointer to reference type %qT", type);
13858 else
13859 error ("forming reference to reference type %qT", type);
13860 last_loc = input_location;
13863 return error_mark_node;
13865 else if (TREE_CODE (type) == FUNCTION_TYPE
13866 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
13867 || type_memfn_rqual (type) != REF_QUAL_NONE))
13869 if (complain & tf_error)
13871 if (code == POINTER_TYPE)
13872 error ("forming pointer to qualified function type %qT",
13873 type);
13874 else
13875 error ("forming reference to qualified function type %qT",
13876 type);
13878 return error_mark_node;
13880 else if (code == POINTER_TYPE)
13882 r = build_pointer_type (type);
13883 if (TREE_CODE (type) == METHOD_TYPE)
13884 r = build_ptrmemfunc_type (r);
13886 else if (TREE_CODE (type) == REFERENCE_TYPE)
13887 /* In C++0x, during template argument substitution, when there is an
13888 attempt to create a reference to a reference type, reference
13889 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
13891 "If a template-argument for a template-parameter T names a type
13892 that is a reference to a type A, an attempt to create the type
13893 'lvalue reference to cv T' creates the type 'lvalue reference to
13894 A,' while an attempt to create the type type rvalue reference to
13895 cv T' creates the type T"
13897 r = cp_build_reference_type
13898 (TREE_TYPE (type),
13899 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
13900 else
13901 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
13902 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
13904 if (r != error_mark_node)
13905 /* Will this ever be needed for TYPE_..._TO values? */
13906 layout_type (r);
13908 return r;
13910 case OFFSET_TYPE:
13912 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
13913 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
13915 /* [temp.deduct]
13917 Type deduction may fail for any of the following
13918 reasons:
13920 -- Attempting to create "pointer to member of T" when T
13921 is not a class type. */
13922 if (complain & tf_error)
13923 error ("creating pointer to member of non-class type %qT", r);
13924 return error_mark_node;
13926 if (TREE_CODE (type) == REFERENCE_TYPE)
13928 if (complain & tf_error)
13929 error ("creating pointer to member reference type %qT", type);
13930 return error_mark_node;
13932 if (VOID_TYPE_P (type))
13934 if (complain & tf_error)
13935 error ("creating pointer to member of type void");
13936 return error_mark_node;
13938 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
13939 if (TREE_CODE (type) == FUNCTION_TYPE)
13941 /* The type of the implicit object parameter gets its
13942 cv-qualifiers from the FUNCTION_TYPE. */
13943 tree memptr;
13944 tree method_type
13945 = build_memfn_type (type, r, type_memfn_quals (type),
13946 type_memfn_rqual (type));
13947 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
13948 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
13949 complain);
13951 else
13952 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
13953 cp_type_quals (t),
13954 complain);
13956 case FUNCTION_TYPE:
13957 case METHOD_TYPE:
13959 tree fntype;
13960 tree specs;
13961 fntype = tsubst_function_type (t, args, complain, in_decl);
13962 if (fntype == error_mark_node)
13963 return error_mark_node;
13965 /* Substitute the exception specification. */
13966 specs = tsubst_exception_specification (t, args, complain, in_decl,
13967 /*defer_ok*/fndecl_type);
13968 if (specs == error_mark_node)
13969 return error_mark_node;
13970 if (specs)
13971 fntype = build_exception_variant (fntype, specs);
13972 return fntype;
13974 case ARRAY_TYPE:
13976 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
13977 if (domain == error_mark_node)
13978 return error_mark_node;
13980 /* As an optimization, we avoid regenerating the array type if
13981 it will obviously be the same as T. */
13982 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
13983 return t;
13985 /* These checks should match the ones in create_array_type_for_decl.
13987 [temp.deduct]
13989 The deduction may fail for any of the following reasons:
13991 -- Attempting to create an array with an element type that
13992 is void, a function type, or a reference type, or [DR337]
13993 an abstract class type. */
13994 if (VOID_TYPE_P (type)
13995 || TREE_CODE (type) == FUNCTION_TYPE
13996 || (TREE_CODE (type) == ARRAY_TYPE
13997 && TYPE_DOMAIN (type) == NULL_TREE)
13998 || TREE_CODE (type) == REFERENCE_TYPE)
14000 if (complain & tf_error)
14001 error ("creating array of %qT", type);
14002 return error_mark_node;
14005 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
14006 return error_mark_node;
14008 r = build_cplus_array_type (type, domain);
14010 if (TYPE_USER_ALIGN (t))
14012 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
14013 TYPE_USER_ALIGN (r) = 1;
14016 return r;
14019 case TYPENAME_TYPE:
14021 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14022 in_decl, /*entering_scope=*/1);
14023 if (ctx == error_mark_node)
14024 return error_mark_node;
14026 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
14027 complain, in_decl);
14028 if (f == error_mark_node)
14029 return error_mark_node;
14031 if (!MAYBE_CLASS_TYPE_P (ctx))
14033 if (complain & tf_error)
14034 error ("%qT is not a class, struct, or union type", ctx);
14035 return error_mark_node;
14037 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
14039 /* Normally, make_typename_type does not require that the CTX
14040 have complete type in order to allow things like:
14042 template <class T> struct S { typename S<T>::X Y; };
14044 But, such constructs have already been resolved by this
14045 point, so here CTX really should have complete type, unless
14046 it's a partial instantiation. */
14047 ctx = complete_type (ctx);
14048 if (!COMPLETE_TYPE_P (ctx))
14050 if (complain & tf_error)
14051 cxx_incomplete_type_error (NULL_TREE, ctx);
14052 return error_mark_node;
14056 f = make_typename_type (ctx, f, typename_type,
14057 complain | tf_keep_type_decl);
14058 if (f == error_mark_node)
14059 return f;
14060 if (TREE_CODE (f) == TYPE_DECL)
14062 complain |= tf_ignore_bad_quals;
14063 f = TREE_TYPE (f);
14066 if (TREE_CODE (f) != TYPENAME_TYPE)
14068 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
14070 if (complain & tf_error)
14071 error ("%qT resolves to %qT, which is not an enumeration type",
14072 t, f);
14073 else
14074 return error_mark_node;
14076 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
14078 if (complain & tf_error)
14079 error ("%qT resolves to %qT, which is is not a class type",
14080 t, f);
14081 else
14082 return error_mark_node;
14086 return cp_build_qualified_type_real
14087 (f, cp_type_quals (f) | cp_type_quals (t), complain);
14090 case UNBOUND_CLASS_TEMPLATE:
14092 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14093 in_decl, /*entering_scope=*/1);
14094 tree name = TYPE_IDENTIFIER (t);
14095 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
14097 if (ctx == error_mark_node || name == error_mark_node)
14098 return error_mark_node;
14100 if (parm_list)
14101 parm_list = tsubst_template_parms (parm_list, args, complain);
14102 return make_unbound_class_template (ctx, name, parm_list, complain);
14105 case TYPEOF_TYPE:
14107 tree type;
14109 ++cp_unevaluated_operand;
14110 ++c_inhibit_evaluation_warnings;
14112 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
14113 complain, in_decl,
14114 /*integral_constant_expression_p=*/false);
14116 --cp_unevaluated_operand;
14117 --c_inhibit_evaluation_warnings;
14119 type = finish_typeof (type);
14120 return cp_build_qualified_type_real (type,
14121 cp_type_quals (t)
14122 | cp_type_quals (type),
14123 complain);
14126 case DECLTYPE_TYPE:
14128 tree type;
14130 ++cp_unevaluated_operand;
14131 ++c_inhibit_evaluation_warnings;
14133 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
14134 complain|tf_decltype, in_decl,
14135 /*function_p*/false,
14136 /*integral_constant_expression*/false);
14138 if (DECLTYPE_FOR_INIT_CAPTURE (t))
14140 if (type == NULL_TREE)
14142 if (complain & tf_error)
14143 error ("empty initializer in lambda init-capture");
14144 type = error_mark_node;
14146 else if (TREE_CODE (type) == TREE_LIST)
14147 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
14150 --cp_unevaluated_operand;
14151 --c_inhibit_evaluation_warnings;
14153 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
14154 type = lambda_capture_field_type (type,
14155 DECLTYPE_FOR_INIT_CAPTURE (t),
14156 DECLTYPE_FOR_REF_CAPTURE (t));
14157 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
14158 type = lambda_proxy_type (type);
14159 else
14161 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
14162 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
14163 && EXPR_P (type))
14164 /* In a template ~id could be either a complement expression
14165 or an unqualified-id naming a destructor; if instantiating
14166 it produces an expression, it's not an id-expression or
14167 member access. */
14168 id = false;
14169 type = finish_decltype_type (type, id, complain);
14171 return cp_build_qualified_type_real (type,
14172 cp_type_quals (t)
14173 | cp_type_quals (type),
14174 complain | tf_ignore_bad_quals);
14177 case UNDERLYING_TYPE:
14179 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
14180 complain, in_decl);
14181 return finish_underlying_type (type);
14184 case TYPE_ARGUMENT_PACK:
14185 case NONTYPE_ARGUMENT_PACK:
14187 tree r;
14189 if (code == NONTYPE_ARGUMENT_PACK)
14190 r = make_node (code);
14191 else
14192 r = cxx_make_type (code);
14194 tree pack_args = ARGUMENT_PACK_ARGS (t);
14195 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
14196 SET_ARGUMENT_PACK_ARGS (r, pack_args);
14198 return r;
14201 case VOID_CST:
14202 case INTEGER_CST:
14203 case REAL_CST:
14204 case STRING_CST:
14205 case PLUS_EXPR:
14206 case MINUS_EXPR:
14207 case NEGATE_EXPR:
14208 case NOP_EXPR:
14209 case INDIRECT_REF:
14210 case ADDR_EXPR:
14211 case CALL_EXPR:
14212 case ARRAY_REF:
14213 case SCOPE_REF:
14214 /* We should use one of the expression tsubsts for these codes. */
14215 gcc_unreachable ();
14217 default:
14218 sorry ("use of %qs in template", get_tree_code_name (code));
14219 return error_mark_node;
14223 /* Like tsubst_expr for a BASELINK. OBJECT_TYPE, if non-NULL, is the
14224 type of the expression on the left-hand side of the "." or "->"
14225 operator. */
14227 static tree
14228 tsubst_baselink (tree baselink, tree object_type,
14229 tree args, tsubst_flags_t complain, tree in_decl)
14231 tree name;
14232 tree qualifying_scope;
14233 tree fns;
14234 tree optype;
14235 tree template_args = 0;
14236 bool template_id_p = false;
14237 bool qualified = BASELINK_QUALIFIED_P (baselink);
14239 /* A baselink indicates a function from a base class. Both the
14240 BASELINK_ACCESS_BINFO and the base class referenced may
14241 indicate bases of the template class, rather than the
14242 instantiated class. In addition, lookups that were not
14243 ambiguous before may be ambiguous now. Therefore, we perform
14244 the lookup again. */
14245 qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
14246 qualifying_scope = tsubst (qualifying_scope, args,
14247 complain, in_decl);
14248 fns = BASELINK_FUNCTIONS (baselink);
14249 optype = tsubst (BASELINK_OPTYPE (baselink), args, complain, in_decl);
14250 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
14252 template_id_p = true;
14253 template_args = TREE_OPERAND (fns, 1);
14254 fns = TREE_OPERAND (fns, 0);
14255 if (template_args)
14256 template_args = tsubst_template_args (template_args, args,
14257 complain, in_decl);
14259 name = OVL_NAME (fns);
14260 if (IDENTIFIER_TYPENAME_P (name))
14261 name = mangle_conv_op_name_for_type (optype);
14262 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
14263 if (!baselink)
14265 if (constructor_name_p (name, qualifying_scope))
14267 if (complain & tf_error)
14268 error ("cannot call constructor %<%T::%D%> directly",
14269 qualifying_scope, name);
14271 return error_mark_node;
14274 /* If lookup found a single function, mark it as used at this
14275 point. (If it lookup found multiple functions the one selected
14276 later by overload resolution will be marked as used at that
14277 point.) */
14278 if (BASELINK_P (baselink))
14279 fns = BASELINK_FUNCTIONS (baselink);
14280 if (!template_id_p && !really_overloaded_fn (fns)
14281 && !mark_used (OVL_FIRST (fns), complain) && !(complain & tf_error))
14282 return error_mark_node;
14284 /* Add back the template arguments, if present. */
14285 if (BASELINK_P (baselink) && template_id_p)
14286 BASELINK_FUNCTIONS (baselink)
14287 = build2 (TEMPLATE_ID_EXPR,
14288 unknown_type_node,
14289 BASELINK_FUNCTIONS (baselink),
14290 template_args);
14291 /* Update the conversion operator type. */
14292 if (BASELINK_P (baselink))
14293 BASELINK_OPTYPE (baselink) = optype;
14295 if (!object_type)
14296 object_type = current_class_type;
14298 if (qualified || name == complete_dtor_identifier)
14300 baselink = adjust_result_of_qualified_name_lookup (baselink,
14301 qualifying_scope,
14302 object_type);
14303 if (!qualified)
14304 /* We need to call adjust_result_of_qualified_name_lookup in case the
14305 destructor names a base class, but we unset BASELINK_QUALIFIED_P
14306 so that we still get virtual function binding. */
14307 BASELINK_QUALIFIED_P (baselink) = false;
14309 return baselink;
14312 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
14313 true if the qualified-id will be a postfix-expression in-and-of
14314 itself; false if more of the postfix-expression follows the
14315 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
14316 of "&". */
14318 static tree
14319 tsubst_qualified_id (tree qualified_id, tree args,
14320 tsubst_flags_t complain, tree in_decl,
14321 bool done, bool address_p)
14323 tree expr;
14324 tree scope;
14325 tree name;
14326 bool is_template;
14327 tree template_args;
14328 location_t loc = UNKNOWN_LOCATION;
14330 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
14332 /* Figure out what name to look up. */
14333 name = TREE_OPERAND (qualified_id, 1);
14334 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14336 is_template = true;
14337 loc = EXPR_LOCATION (name);
14338 template_args = TREE_OPERAND (name, 1);
14339 if (template_args)
14340 template_args = tsubst_template_args (template_args, args,
14341 complain, in_decl);
14342 if (template_args == error_mark_node)
14343 return error_mark_node;
14344 name = TREE_OPERAND (name, 0);
14346 else
14348 is_template = false;
14349 template_args = NULL_TREE;
14352 /* Substitute into the qualifying scope. When there are no ARGS, we
14353 are just trying to simplify a non-dependent expression. In that
14354 case the qualifying scope may be dependent, and, in any case,
14355 substituting will not help. */
14356 scope = TREE_OPERAND (qualified_id, 0);
14357 if (args)
14359 scope = tsubst (scope, args, complain, in_decl);
14360 expr = tsubst_copy (name, args, complain, in_decl);
14362 else
14363 expr = name;
14365 if (dependent_scope_p (scope))
14367 if (is_template)
14368 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
14369 tree r = build_qualified_name (NULL_TREE, scope, expr,
14370 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
14371 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
14372 return r;
14375 if (!BASELINK_P (name) && !DECL_P (expr))
14377 if (TREE_CODE (expr) == BIT_NOT_EXPR)
14379 /* A BIT_NOT_EXPR is used to represent a destructor. */
14380 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
14382 error ("qualifying type %qT does not match destructor name ~%qT",
14383 scope, TREE_OPERAND (expr, 0));
14384 expr = error_mark_node;
14386 else
14387 expr = lookup_qualified_name (scope, complete_dtor_identifier,
14388 /*is_type_p=*/0, false);
14390 else
14391 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
14392 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
14393 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
14395 if (complain & tf_error)
14397 error ("dependent-name %qE is parsed as a non-type, but "
14398 "instantiation yields a type", qualified_id);
14399 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
14401 return error_mark_node;
14405 if (DECL_P (expr))
14407 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
14408 scope);
14409 /* Remember that there was a reference to this entity. */
14410 if (!mark_used (expr, complain) && !(complain & tf_error))
14411 return error_mark_node;
14414 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
14416 if (complain & tf_error)
14417 qualified_name_lookup_error (scope,
14418 TREE_OPERAND (qualified_id, 1),
14419 expr, input_location);
14420 return error_mark_node;
14423 if (is_template)
14425 if (variable_template_p (expr))
14426 expr = lookup_and_finish_template_variable (expr, template_args,
14427 complain);
14428 else
14429 expr = lookup_template_function (expr, template_args);
14432 if (expr == error_mark_node && complain & tf_error)
14433 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
14434 expr, input_location);
14435 else if (TYPE_P (scope))
14437 expr = (adjust_result_of_qualified_name_lookup
14438 (expr, scope, current_nonlambda_class_type ()));
14439 expr = (finish_qualified_id_expr
14440 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
14441 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
14442 /*template_arg_p=*/false, complain));
14445 /* Expressions do not generally have reference type. */
14446 if (TREE_CODE (expr) != SCOPE_REF
14447 /* However, if we're about to form a pointer-to-member, we just
14448 want the referenced member referenced. */
14449 && TREE_CODE (expr) != OFFSET_REF)
14450 expr = convert_from_reference (expr);
14452 if (REF_PARENTHESIZED_P (qualified_id))
14453 expr = force_paren_expr (expr);
14455 return expr;
14458 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14459 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14460 for tsubst. */
14462 static tree
14463 tsubst_init (tree init, tree decl, tree args,
14464 tsubst_flags_t complain, tree in_decl)
14466 if (!init)
14467 return NULL_TREE;
14469 init = tsubst_expr (init, args, complain, in_decl, false);
14471 if (!init && TREE_TYPE (decl) != error_mark_node)
14473 /* If we had an initializer but it
14474 instantiated to nothing,
14475 value-initialize the object. This will
14476 only occur when the initializer was a
14477 pack expansion where the parameter packs
14478 used in that expansion were of length
14479 zero. */
14480 init = build_value_init (TREE_TYPE (decl),
14481 complain);
14482 if (TREE_CODE (init) == AGGR_INIT_EXPR)
14483 init = get_target_expr_sfinae (init, complain);
14484 if (TREE_CODE (init) == TARGET_EXPR)
14485 TARGET_EXPR_DIRECT_INIT_P (init) = true;
14488 return init;
14491 /* Like tsubst, but deals with expressions. This function just replaces
14492 template parms; to finish processing the resultant expression, use
14493 tsubst_copy_and_build or tsubst_expr. */
14495 static tree
14496 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14498 enum tree_code code;
14499 tree r;
14501 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
14502 return t;
14504 code = TREE_CODE (t);
14506 switch (code)
14508 case PARM_DECL:
14509 r = retrieve_local_specialization (t);
14511 if (r == NULL_TREE)
14513 /* We get here for a use of 'this' in an NSDMI as part of a
14514 constructor call or as part of an aggregate initialization. */
14515 if (DECL_NAME (t) == this_identifier
14516 && ((current_function_decl
14517 && DECL_CONSTRUCTOR_P (current_function_decl))
14518 || (current_class_ref
14519 && TREE_CODE (current_class_ref) == PLACEHOLDER_EXPR)))
14520 return current_class_ptr;
14522 /* This can happen for a parameter name used later in a function
14523 declaration (such as in a late-specified return type). Just
14524 make a dummy decl, since it's only used for its type. */
14525 gcc_assert (cp_unevaluated_operand != 0);
14526 r = tsubst_decl (t, args, complain);
14527 /* Give it the template pattern as its context; its true context
14528 hasn't been instantiated yet and this is good enough for
14529 mangling. */
14530 DECL_CONTEXT (r) = DECL_CONTEXT (t);
14533 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14534 r = ARGUMENT_PACK_SELECT_ARG (r);
14535 if (!mark_used (r, complain) && !(complain & tf_error))
14536 return error_mark_node;
14537 return r;
14539 case CONST_DECL:
14541 tree enum_type;
14542 tree v;
14544 if (DECL_TEMPLATE_PARM_P (t))
14545 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
14546 /* There is no need to substitute into namespace-scope
14547 enumerators. */
14548 if (DECL_NAMESPACE_SCOPE_P (t))
14549 return t;
14550 /* If ARGS is NULL, then T is known to be non-dependent. */
14551 if (args == NULL_TREE)
14552 return scalar_constant_value (t);
14554 /* Unfortunately, we cannot just call lookup_name here.
14555 Consider:
14557 template <int I> int f() {
14558 enum E { a = I };
14559 struct S { void g() { E e = a; } };
14562 When we instantiate f<7>::S::g(), say, lookup_name is not
14563 clever enough to find f<7>::a. */
14564 enum_type
14565 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14566 /*entering_scope=*/0);
14568 for (v = TYPE_VALUES (enum_type);
14569 v != NULL_TREE;
14570 v = TREE_CHAIN (v))
14571 if (TREE_PURPOSE (v) == DECL_NAME (t))
14572 return TREE_VALUE (v);
14574 /* We didn't find the name. That should never happen; if
14575 name-lookup found it during preliminary parsing, we
14576 should find it again here during instantiation. */
14577 gcc_unreachable ();
14579 return t;
14581 case FIELD_DECL:
14582 if (PACK_EXPANSION_P (TREE_TYPE (t)))
14584 /* Check for a local specialization set up by
14585 tsubst_pack_expansion. */
14586 if (tree r = retrieve_local_specialization (t))
14588 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14589 r = ARGUMENT_PACK_SELECT_ARG (r);
14590 return r;
14593 /* When retrieving a capture pack from a generic lambda, remove the
14594 lambda call op's own template argument list from ARGS. Only the
14595 template arguments active for the closure type should be used to
14596 retrieve the pack specialization. */
14597 if (LAMBDA_FUNCTION_P (current_function_decl)
14598 && (template_class_depth (DECL_CONTEXT (t))
14599 != TMPL_ARGS_DEPTH (args)))
14600 args = strip_innermost_template_args (args, 1);
14602 /* Otherwise return the full NONTYPE_ARGUMENT_PACK that
14603 tsubst_decl put in the hash table. */
14604 return retrieve_specialization (t, args, 0);
14607 if (DECL_CONTEXT (t))
14609 tree ctx;
14611 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
14612 /*entering_scope=*/1);
14613 if (ctx != DECL_CONTEXT (t))
14615 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
14616 if (!r)
14618 if (complain & tf_error)
14619 error ("using invalid field %qD", t);
14620 return error_mark_node;
14622 return r;
14626 return t;
14628 case VAR_DECL:
14629 case FUNCTION_DECL:
14630 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
14631 r = tsubst (t, args, complain, in_decl);
14632 else if (local_variable_p (t)
14633 && uses_template_parms (DECL_CONTEXT (t)))
14635 r = retrieve_local_specialization (t);
14636 if (r == NULL_TREE)
14638 /* First try name lookup to find the instantiation. */
14639 r = lookup_name (DECL_NAME (t));
14640 if (r && !is_capture_proxy (r))
14642 /* Make sure that the one we found is the one we want. */
14643 tree ctx = DECL_CONTEXT (t);
14644 if (DECL_LANG_SPECIFIC (ctx) && DECL_TEMPLATE_INFO (ctx))
14645 ctx = tsubst (ctx, args, complain, in_decl);
14646 if (ctx != DECL_CONTEXT (r))
14647 r = NULL_TREE;
14650 if (r)
14651 /* OK */;
14652 else
14654 /* This can happen for a variable used in a
14655 late-specified return type of a local lambda, or for a
14656 local static or constant. Building a new VAR_DECL
14657 should be OK in all those cases. */
14658 r = tsubst_decl (t, args, complain);
14659 if (local_specializations)
14660 /* Avoid infinite recursion (79640). */
14661 register_local_specialization (r, t);
14662 if (decl_maybe_constant_var_p (r))
14664 /* We can't call cp_finish_decl, so handle the
14665 initializer by hand. */
14666 tree init = tsubst_init (DECL_INITIAL (t), r, args,
14667 complain, in_decl);
14668 if (!processing_template_decl)
14669 init = maybe_constant_init (init);
14670 if (processing_template_decl
14671 ? potential_constant_expression (init)
14672 : reduced_constant_expression_p (init))
14673 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
14674 = TREE_CONSTANT (r) = true;
14675 DECL_INITIAL (r) = init;
14677 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
14678 || decl_constant_var_p (r)
14679 || errorcount || sorrycount);
14680 if (!processing_template_decl
14681 && !TREE_STATIC (r))
14682 r = process_outer_var_ref (r, complain);
14684 /* Remember this for subsequent uses. */
14685 if (local_specializations)
14686 register_local_specialization (r, t);
14689 else
14690 r = t;
14691 if (!mark_used (r, complain))
14692 return error_mark_node;
14693 return r;
14695 case NAMESPACE_DECL:
14696 return t;
14698 case OVERLOAD:
14699 /* An OVERLOAD will always be a non-dependent overload set; an
14700 overload set from function scope will just be represented with an
14701 IDENTIFIER_NODE, and from class scope with a BASELINK. */
14702 gcc_assert (!uses_template_parms (t));
14703 /* We must have marked any lookups as persistent. */
14704 gcc_assert (!OVL_LOOKUP_P (t) || OVL_USED_P (t));
14705 return t;
14707 case BASELINK:
14708 return tsubst_baselink (t, current_nonlambda_class_type (),
14709 args, complain, in_decl);
14711 case TEMPLATE_DECL:
14712 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
14713 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
14714 args, complain, in_decl);
14715 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
14716 return tsubst (t, args, complain, in_decl);
14717 else if (DECL_CLASS_SCOPE_P (t)
14718 && uses_template_parms (DECL_CONTEXT (t)))
14720 /* Template template argument like the following example need
14721 special treatment:
14723 template <template <class> class TT> struct C {};
14724 template <class T> struct D {
14725 template <class U> struct E {};
14726 C<E> c; // #1
14728 D<int> d; // #2
14730 We are processing the template argument `E' in #1 for
14731 the template instantiation #2. Originally, `E' is a
14732 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
14733 have to substitute this with one having context `D<int>'. */
14735 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
14736 if (dependent_scope_p (context))
14738 /* When rewriting a constructor into a deduction guide, a
14739 non-dependent name can become dependent, so memtmpl<args>
14740 becomes context::template memtmpl<args>. */
14741 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14742 return build_qualified_name (type, context, DECL_NAME (t),
14743 /*template*/true);
14745 return lookup_field (context, DECL_NAME(t), 0, false);
14747 else
14748 /* Ordinary template template argument. */
14749 return t;
14751 case CAST_EXPR:
14752 case REINTERPRET_CAST_EXPR:
14753 case CONST_CAST_EXPR:
14754 case STATIC_CAST_EXPR:
14755 case DYNAMIC_CAST_EXPR:
14756 case IMPLICIT_CONV_EXPR:
14757 case CONVERT_EXPR:
14758 case NOP_EXPR:
14760 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14761 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14762 return build1 (code, type, op0);
14765 case SIZEOF_EXPR:
14766 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
14767 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
14769 tree expanded, op = TREE_OPERAND (t, 0);
14770 int len = 0;
14772 if (SIZEOF_EXPR_TYPE_P (t))
14773 op = TREE_TYPE (op);
14775 ++cp_unevaluated_operand;
14776 ++c_inhibit_evaluation_warnings;
14777 /* We only want to compute the number of arguments. */
14778 if (PACK_EXPANSION_P (op))
14779 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
14780 else
14781 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
14782 args, complain, in_decl);
14783 --cp_unevaluated_operand;
14784 --c_inhibit_evaluation_warnings;
14786 if (TREE_CODE (expanded) == TREE_VEC)
14788 len = TREE_VEC_LENGTH (expanded);
14789 /* Set TREE_USED for the benefit of -Wunused. */
14790 for (int i = 0; i < len; i++)
14791 if (DECL_P (TREE_VEC_ELT (expanded, i)))
14792 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
14795 if (expanded == error_mark_node)
14796 return error_mark_node;
14797 else if (PACK_EXPANSION_P (expanded)
14798 || (TREE_CODE (expanded) == TREE_VEC
14799 && pack_expansion_args_count (expanded)))
14802 if (PACK_EXPANSION_P (expanded))
14803 /* OK. */;
14804 else if (TREE_VEC_LENGTH (expanded) == 1)
14805 expanded = TREE_VEC_ELT (expanded, 0);
14806 else
14807 expanded = make_argument_pack (expanded);
14809 if (TYPE_P (expanded))
14810 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
14811 complain & tf_error);
14812 else
14813 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
14814 complain & tf_error);
14816 else
14817 return build_int_cst (size_type_node, len);
14819 if (SIZEOF_EXPR_TYPE_P (t))
14821 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
14822 args, complain, in_decl);
14823 r = build1 (NOP_EXPR, r, error_mark_node);
14824 r = build1 (SIZEOF_EXPR,
14825 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
14826 SIZEOF_EXPR_TYPE_P (r) = 1;
14827 return r;
14829 /* Fall through */
14831 case INDIRECT_REF:
14832 case NEGATE_EXPR:
14833 case TRUTH_NOT_EXPR:
14834 case BIT_NOT_EXPR:
14835 case ADDR_EXPR:
14836 case UNARY_PLUS_EXPR: /* Unary + */
14837 case ALIGNOF_EXPR:
14838 case AT_ENCODE_EXPR:
14839 case ARROW_EXPR:
14840 case THROW_EXPR:
14841 case TYPEID_EXPR:
14842 case REALPART_EXPR:
14843 case IMAGPART_EXPR:
14844 case PAREN_EXPR:
14846 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
14847 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14848 return build1 (code, type, op0);
14851 case COMPONENT_REF:
14853 tree object;
14854 tree name;
14856 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14857 name = TREE_OPERAND (t, 1);
14858 if (TREE_CODE (name) == BIT_NOT_EXPR)
14860 name = tsubst_copy (TREE_OPERAND (name, 0), args,
14861 complain, in_decl);
14862 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
14864 else if (TREE_CODE (name) == SCOPE_REF
14865 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
14867 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
14868 complain, in_decl);
14869 name = TREE_OPERAND (name, 1);
14870 name = tsubst_copy (TREE_OPERAND (name, 0), args,
14871 complain, in_decl);
14872 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
14873 name = build_qualified_name (/*type=*/NULL_TREE,
14874 base, name,
14875 /*template_p=*/false);
14877 else if (BASELINK_P (name))
14878 name = tsubst_baselink (name,
14879 non_reference (TREE_TYPE (object)),
14880 args, complain,
14881 in_decl);
14882 else
14883 name = tsubst_copy (name, args, complain, in_decl);
14884 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
14887 case PLUS_EXPR:
14888 case MINUS_EXPR:
14889 case MULT_EXPR:
14890 case TRUNC_DIV_EXPR:
14891 case CEIL_DIV_EXPR:
14892 case FLOOR_DIV_EXPR:
14893 case ROUND_DIV_EXPR:
14894 case EXACT_DIV_EXPR:
14895 case BIT_AND_EXPR:
14896 case BIT_IOR_EXPR:
14897 case BIT_XOR_EXPR:
14898 case TRUNC_MOD_EXPR:
14899 case FLOOR_MOD_EXPR:
14900 case TRUTH_ANDIF_EXPR:
14901 case TRUTH_ORIF_EXPR:
14902 case TRUTH_AND_EXPR:
14903 case TRUTH_OR_EXPR:
14904 case RSHIFT_EXPR:
14905 case LSHIFT_EXPR:
14906 case RROTATE_EXPR:
14907 case LROTATE_EXPR:
14908 case EQ_EXPR:
14909 case NE_EXPR:
14910 case MAX_EXPR:
14911 case MIN_EXPR:
14912 case LE_EXPR:
14913 case GE_EXPR:
14914 case LT_EXPR:
14915 case GT_EXPR:
14916 case COMPOUND_EXPR:
14917 case DOTSTAR_EXPR:
14918 case MEMBER_REF:
14919 case PREDECREMENT_EXPR:
14920 case PREINCREMENT_EXPR:
14921 case POSTDECREMENT_EXPR:
14922 case POSTINCREMENT_EXPR:
14924 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14925 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14926 return build_nt (code, op0, op1);
14929 case SCOPE_REF:
14931 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14932 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14933 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
14934 QUALIFIED_NAME_IS_TEMPLATE (t));
14937 case ARRAY_REF:
14939 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14940 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14941 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
14944 case CALL_EXPR:
14946 int n = VL_EXP_OPERAND_LENGTH (t);
14947 tree result = build_vl_exp (CALL_EXPR, n);
14948 int i;
14949 for (i = 0; i < n; i++)
14950 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
14951 complain, in_decl);
14952 return result;
14955 case COND_EXPR:
14956 case MODOP_EXPR:
14957 case PSEUDO_DTOR_EXPR:
14958 case VEC_PERM_EXPR:
14960 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14961 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14962 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
14963 r = build_nt (code, op0, op1, op2);
14964 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
14965 return r;
14968 case NEW_EXPR:
14970 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14971 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14972 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
14973 r = build_nt (code, op0, op1, op2);
14974 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
14975 return r;
14978 case DELETE_EXPR:
14980 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
14981 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
14982 r = build_nt (code, op0, op1);
14983 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
14984 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
14985 return r;
14988 case TEMPLATE_ID_EXPR:
14990 /* Substituted template arguments */
14991 tree fn = TREE_OPERAND (t, 0);
14992 tree targs = TREE_OPERAND (t, 1);
14994 fn = tsubst_copy (fn, args, complain, in_decl);
14995 if (targs)
14996 targs = tsubst_template_args (targs, args, complain, in_decl);
14998 return lookup_template_function (fn, targs);
15001 case TREE_LIST:
15003 tree purpose, value, chain;
15005 if (t == void_list_node)
15006 return t;
15008 purpose = TREE_PURPOSE (t);
15009 if (purpose)
15010 purpose = tsubst_copy (purpose, args, complain, in_decl);
15011 value = TREE_VALUE (t);
15012 if (value)
15013 value = tsubst_copy (value, args, complain, in_decl);
15014 chain = TREE_CHAIN (t);
15015 if (chain && chain != void_type_node)
15016 chain = tsubst_copy (chain, args, complain, in_decl);
15017 if (purpose == TREE_PURPOSE (t)
15018 && value == TREE_VALUE (t)
15019 && chain == TREE_CHAIN (t))
15020 return t;
15021 return tree_cons (purpose, value, chain);
15024 case RECORD_TYPE:
15025 case UNION_TYPE:
15026 case ENUMERAL_TYPE:
15027 case INTEGER_TYPE:
15028 case TEMPLATE_TYPE_PARM:
15029 case TEMPLATE_TEMPLATE_PARM:
15030 case BOUND_TEMPLATE_TEMPLATE_PARM:
15031 case TEMPLATE_PARM_INDEX:
15032 case POINTER_TYPE:
15033 case REFERENCE_TYPE:
15034 case OFFSET_TYPE:
15035 case FUNCTION_TYPE:
15036 case METHOD_TYPE:
15037 case ARRAY_TYPE:
15038 case TYPENAME_TYPE:
15039 case UNBOUND_CLASS_TEMPLATE:
15040 case TYPEOF_TYPE:
15041 case DECLTYPE_TYPE:
15042 case TYPE_DECL:
15043 return tsubst (t, args, complain, in_decl);
15045 case USING_DECL:
15046 t = DECL_NAME (t);
15047 /* Fall through. */
15048 case IDENTIFIER_NODE:
15049 if (IDENTIFIER_TYPENAME_P (t))
15051 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15052 return mangle_conv_op_name_for_type (new_type);
15054 else
15055 return t;
15057 case CONSTRUCTOR:
15058 /* This is handled by tsubst_copy_and_build. */
15059 gcc_unreachable ();
15061 case VA_ARG_EXPR:
15063 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15064 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15065 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
15068 case CLEANUP_POINT_EXPR:
15069 /* We shouldn't have built any of these during initial template
15070 generation. Instead, they should be built during instantiation
15071 in response to the saved STMT_IS_FULL_EXPR_P setting. */
15072 gcc_unreachable ();
15074 case OFFSET_REF:
15076 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15077 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15078 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15079 r = build2 (code, type, op0, op1);
15080 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
15081 if (!mark_used (TREE_OPERAND (r, 1), complain)
15082 && !(complain & tf_error))
15083 return error_mark_node;
15084 return r;
15087 case EXPR_PACK_EXPANSION:
15088 error ("invalid use of pack expansion expression");
15089 return error_mark_node;
15091 case NONTYPE_ARGUMENT_PACK:
15092 error ("use %<...%> to expand argument pack");
15093 return error_mark_node;
15095 case VOID_CST:
15096 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
15097 return t;
15099 case INTEGER_CST:
15100 case REAL_CST:
15101 case STRING_CST:
15102 case COMPLEX_CST:
15104 /* Instantiate any typedefs in the type. */
15105 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15106 r = fold_convert (type, t);
15107 gcc_assert (TREE_CODE (r) == code);
15108 return r;
15111 case PTRMEM_CST:
15112 /* These can sometimes show up in a partial instantiation, but never
15113 involve template parms. */
15114 gcc_assert (!uses_template_parms (t));
15115 return t;
15117 case UNARY_LEFT_FOLD_EXPR:
15118 return tsubst_unary_left_fold (t, args, complain, in_decl);
15119 case UNARY_RIGHT_FOLD_EXPR:
15120 return tsubst_unary_right_fold (t, args, complain, in_decl);
15121 case BINARY_LEFT_FOLD_EXPR:
15122 return tsubst_binary_left_fold (t, args, complain, in_decl);
15123 case BINARY_RIGHT_FOLD_EXPR:
15124 return tsubst_binary_right_fold (t, args, complain, in_decl);
15126 default:
15127 /* We shouldn't get here, but keep going if !flag_checking. */
15128 if (flag_checking)
15129 gcc_unreachable ();
15130 return t;
15134 /* Helper function for tsubst_omp_clauses, used for instantiation of
15135 OMP_CLAUSE_DECL of clauses. */
15137 static tree
15138 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
15139 tree in_decl)
15141 if (decl == NULL_TREE)
15142 return NULL_TREE;
15144 /* Handle an OpenMP array section represented as a TREE_LIST (or
15145 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15146 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15147 TREE_LIST. We can handle it exactly the same as an array section
15148 (purpose, value, and a chain), even though the nomenclature
15149 (low_bound, length, etc) is different. */
15150 if (TREE_CODE (decl) == TREE_LIST)
15152 tree low_bound
15153 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
15154 /*integral_constant_expression_p=*/false);
15155 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
15156 /*integral_constant_expression_p=*/false);
15157 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
15158 in_decl);
15159 if (TREE_PURPOSE (decl) == low_bound
15160 && TREE_VALUE (decl) == length
15161 && TREE_CHAIN (decl) == chain)
15162 return decl;
15163 tree ret = tree_cons (low_bound, length, chain);
15164 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
15165 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
15166 return ret;
15168 tree ret = tsubst_expr (decl, args, complain, in_decl,
15169 /*integral_constant_expression_p=*/false);
15170 /* Undo convert_from_reference tsubst_expr could have called. */
15171 if (decl
15172 && REFERENCE_REF_P (ret)
15173 && !REFERENCE_REF_P (decl))
15174 ret = TREE_OPERAND (ret, 0);
15175 return ret;
15178 /* Like tsubst_copy, but specifically for OpenMP clauses. */
15180 static tree
15181 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
15182 tree args, tsubst_flags_t complain, tree in_decl)
15184 tree new_clauses = NULL_TREE, nc, oc;
15185 tree linear_no_step = NULL_TREE;
15187 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
15189 nc = copy_node (oc);
15190 OMP_CLAUSE_CHAIN (nc) = new_clauses;
15191 new_clauses = nc;
15193 switch (OMP_CLAUSE_CODE (nc))
15195 case OMP_CLAUSE_LASTPRIVATE:
15196 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
15198 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
15199 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
15200 in_decl, /*integral_constant_expression_p=*/false);
15201 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
15202 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
15204 /* FALLTHRU */
15205 case OMP_CLAUSE_PRIVATE:
15206 case OMP_CLAUSE_SHARED:
15207 case OMP_CLAUSE_FIRSTPRIVATE:
15208 case OMP_CLAUSE_COPYIN:
15209 case OMP_CLAUSE_COPYPRIVATE:
15210 case OMP_CLAUSE_UNIFORM:
15211 case OMP_CLAUSE_DEPEND:
15212 case OMP_CLAUSE_FROM:
15213 case OMP_CLAUSE_TO:
15214 case OMP_CLAUSE_MAP:
15215 case OMP_CLAUSE_USE_DEVICE_PTR:
15216 case OMP_CLAUSE_IS_DEVICE_PTR:
15217 OMP_CLAUSE_DECL (nc)
15218 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15219 in_decl);
15220 break;
15221 case OMP_CLAUSE_TILE:
15222 case OMP_CLAUSE_IF:
15223 case OMP_CLAUSE_NUM_THREADS:
15224 case OMP_CLAUSE_SCHEDULE:
15225 case OMP_CLAUSE_COLLAPSE:
15226 case OMP_CLAUSE_FINAL:
15227 case OMP_CLAUSE_DEVICE:
15228 case OMP_CLAUSE_DIST_SCHEDULE:
15229 case OMP_CLAUSE_NUM_TEAMS:
15230 case OMP_CLAUSE_THREAD_LIMIT:
15231 case OMP_CLAUSE_SAFELEN:
15232 case OMP_CLAUSE_SIMDLEN:
15233 case OMP_CLAUSE_NUM_TASKS:
15234 case OMP_CLAUSE_GRAINSIZE:
15235 case OMP_CLAUSE_PRIORITY:
15236 case OMP_CLAUSE_ORDERED:
15237 case OMP_CLAUSE_HINT:
15238 case OMP_CLAUSE_NUM_GANGS:
15239 case OMP_CLAUSE_NUM_WORKERS:
15240 case OMP_CLAUSE_VECTOR_LENGTH:
15241 case OMP_CLAUSE_WORKER:
15242 case OMP_CLAUSE_VECTOR:
15243 case OMP_CLAUSE_ASYNC:
15244 case OMP_CLAUSE_WAIT:
15245 OMP_CLAUSE_OPERAND (nc, 0)
15246 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
15247 in_decl, /*integral_constant_expression_p=*/false);
15248 break;
15249 case OMP_CLAUSE_REDUCTION:
15250 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
15252 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
15253 if (TREE_CODE (placeholder) == SCOPE_REF)
15255 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
15256 complain, in_decl);
15257 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
15258 = build_qualified_name (NULL_TREE, scope,
15259 TREE_OPERAND (placeholder, 1),
15260 false);
15262 else
15263 gcc_assert (identifier_p (placeholder));
15265 OMP_CLAUSE_DECL (nc)
15266 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15267 in_decl);
15268 break;
15269 case OMP_CLAUSE_GANG:
15270 case OMP_CLAUSE_ALIGNED:
15271 OMP_CLAUSE_DECL (nc)
15272 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15273 in_decl);
15274 OMP_CLAUSE_OPERAND (nc, 1)
15275 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
15276 in_decl, /*integral_constant_expression_p=*/false);
15277 break;
15278 case OMP_CLAUSE_LINEAR:
15279 OMP_CLAUSE_DECL (nc)
15280 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15281 in_decl);
15282 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
15284 gcc_assert (!linear_no_step);
15285 linear_no_step = nc;
15287 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
15288 OMP_CLAUSE_LINEAR_STEP (nc)
15289 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
15290 complain, in_decl);
15291 else
15292 OMP_CLAUSE_LINEAR_STEP (nc)
15293 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
15294 in_decl,
15295 /*integral_constant_expression_p=*/false);
15296 break;
15297 case OMP_CLAUSE_NOWAIT:
15298 case OMP_CLAUSE_DEFAULT:
15299 case OMP_CLAUSE_UNTIED:
15300 case OMP_CLAUSE_MERGEABLE:
15301 case OMP_CLAUSE_INBRANCH:
15302 case OMP_CLAUSE_NOTINBRANCH:
15303 case OMP_CLAUSE_PROC_BIND:
15304 case OMP_CLAUSE_FOR:
15305 case OMP_CLAUSE_PARALLEL:
15306 case OMP_CLAUSE_SECTIONS:
15307 case OMP_CLAUSE_TASKGROUP:
15308 case OMP_CLAUSE_NOGROUP:
15309 case OMP_CLAUSE_THREADS:
15310 case OMP_CLAUSE_SIMD:
15311 case OMP_CLAUSE_DEFAULTMAP:
15312 case OMP_CLAUSE_INDEPENDENT:
15313 case OMP_CLAUSE_AUTO:
15314 case OMP_CLAUSE_SEQ:
15315 break;
15316 default:
15317 gcc_unreachable ();
15319 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
15320 switch (OMP_CLAUSE_CODE (nc))
15322 case OMP_CLAUSE_SHARED:
15323 case OMP_CLAUSE_PRIVATE:
15324 case OMP_CLAUSE_FIRSTPRIVATE:
15325 case OMP_CLAUSE_LASTPRIVATE:
15326 case OMP_CLAUSE_COPYPRIVATE:
15327 case OMP_CLAUSE_LINEAR:
15328 case OMP_CLAUSE_REDUCTION:
15329 case OMP_CLAUSE_USE_DEVICE_PTR:
15330 case OMP_CLAUSE_IS_DEVICE_PTR:
15331 /* tsubst_expr on SCOPE_REF results in returning
15332 finish_non_static_data_member result. Undo that here. */
15333 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
15334 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
15335 == IDENTIFIER_NODE))
15337 tree t = OMP_CLAUSE_DECL (nc);
15338 tree v = t;
15339 while (v)
15340 switch (TREE_CODE (v))
15342 case COMPONENT_REF:
15343 case MEM_REF:
15344 case INDIRECT_REF:
15345 CASE_CONVERT:
15346 case POINTER_PLUS_EXPR:
15347 v = TREE_OPERAND (v, 0);
15348 continue;
15349 case PARM_DECL:
15350 if (DECL_CONTEXT (v) == current_function_decl
15351 && DECL_ARTIFICIAL (v)
15352 && DECL_NAME (v) == this_identifier)
15353 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
15354 /* FALLTHRU */
15355 default:
15356 v = NULL_TREE;
15357 break;
15360 else if (VAR_P (OMP_CLAUSE_DECL (oc))
15361 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
15362 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
15363 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
15364 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
15366 tree decl = OMP_CLAUSE_DECL (nc);
15367 if (VAR_P (decl))
15369 retrofit_lang_decl (decl);
15370 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
15373 break;
15374 default:
15375 break;
15379 new_clauses = nreverse (new_clauses);
15380 if (ort != C_ORT_OMP_DECLARE_SIMD)
15382 new_clauses = finish_omp_clauses (new_clauses, ort);
15383 if (linear_no_step)
15384 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
15385 if (nc == linear_no_step)
15387 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
15388 break;
15391 return new_clauses;
15394 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
15396 static tree
15397 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
15398 tree in_decl)
15400 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
15402 tree purpose, value, chain;
15404 if (t == NULL)
15405 return t;
15407 if (TREE_CODE (t) != TREE_LIST)
15408 return tsubst_copy_and_build (t, args, complain, in_decl,
15409 /*function_p=*/false,
15410 /*integral_constant_expression_p=*/false);
15412 if (t == void_list_node)
15413 return t;
15415 purpose = TREE_PURPOSE (t);
15416 if (purpose)
15417 purpose = RECUR (purpose);
15418 value = TREE_VALUE (t);
15419 if (value)
15421 if (TREE_CODE (value) != LABEL_DECL)
15422 value = RECUR (value);
15423 else
15425 value = lookup_label (DECL_NAME (value));
15426 gcc_assert (TREE_CODE (value) == LABEL_DECL);
15427 TREE_USED (value) = 1;
15430 chain = TREE_CHAIN (t);
15431 if (chain && chain != void_type_node)
15432 chain = RECUR (chain);
15433 return tree_cons (purpose, value, chain);
15434 #undef RECUR
15437 /* Used to temporarily communicate the list of #pragma omp parallel
15438 clauses to #pragma omp for instantiation if they are combined
15439 together. */
15441 static tree *omp_parallel_combined_clauses;
15443 /* Substitute one OMP_FOR iterator. */
15445 static void
15446 tsubst_omp_for_iterator (tree t, int i, tree declv, tree orig_declv,
15447 tree initv, tree condv, tree incrv, tree *clauses,
15448 tree args, tsubst_flags_t complain, tree in_decl,
15449 bool integral_constant_expression_p)
15451 #define RECUR(NODE) \
15452 tsubst_expr ((NODE), args, complain, in_decl, \
15453 integral_constant_expression_p)
15454 tree decl, init, cond, incr;
15456 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
15457 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
15459 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
15461 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
15462 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
15465 decl = TREE_OPERAND (init, 0);
15466 init = TREE_OPERAND (init, 1);
15467 tree decl_expr = NULL_TREE;
15468 if (init && TREE_CODE (init) == DECL_EXPR)
15470 /* We need to jump through some hoops to handle declarations in the
15471 init-statement, since we might need to handle auto deduction,
15472 but we need to keep control of initialization. */
15473 decl_expr = init;
15474 init = DECL_INITIAL (DECL_EXPR_DECL (init));
15475 decl = tsubst_decl (decl, args, complain);
15477 else
15479 if (TREE_CODE (decl) == SCOPE_REF)
15481 decl = RECUR (decl);
15482 if (TREE_CODE (decl) == COMPONENT_REF)
15484 tree v = decl;
15485 while (v)
15486 switch (TREE_CODE (v))
15488 case COMPONENT_REF:
15489 case MEM_REF:
15490 case INDIRECT_REF:
15491 CASE_CONVERT:
15492 case POINTER_PLUS_EXPR:
15493 v = TREE_OPERAND (v, 0);
15494 continue;
15495 case PARM_DECL:
15496 if (DECL_CONTEXT (v) == current_function_decl
15497 && DECL_ARTIFICIAL (v)
15498 && DECL_NAME (v) == this_identifier)
15500 decl = TREE_OPERAND (decl, 1);
15501 decl = omp_privatize_field (decl, false);
15503 /* FALLTHRU */
15504 default:
15505 v = NULL_TREE;
15506 break;
15510 else
15511 decl = RECUR (decl);
15513 init = RECUR (init);
15515 tree auto_node = type_uses_auto (TREE_TYPE (decl));
15516 if (auto_node && init)
15517 TREE_TYPE (decl)
15518 = do_auto_deduction (TREE_TYPE (decl), init, auto_node);
15520 gcc_assert (!type_dependent_expression_p (decl));
15522 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15524 if (decl_expr)
15526 /* Declare the variable, but don't let that initialize it. */
15527 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
15528 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
15529 RECUR (decl_expr);
15530 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
15533 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
15534 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15535 if (TREE_CODE (incr) == MODIFY_EXPR)
15537 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15538 tree rhs = RECUR (TREE_OPERAND (incr, 1));
15539 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
15540 NOP_EXPR, rhs, complain);
15542 else
15543 incr = RECUR (incr);
15544 TREE_VEC_ELT (declv, i) = decl;
15545 TREE_VEC_ELT (initv, i) = init;
15546 TREE_VEC_ELT (condv, i) = cond;
15547 TREE_VEC_ELT (incrv, i) = incr;
15548 return;
15551 if (decl_expr)
15553 /* Declare and initialize the variable. */
15554 RECUR (decl_expr);
15555 init = NULL_TREE;
15557 else if (init)
15559 tree *pc;
15560 int j;
15561 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
15563 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
15565 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
15566 && OMP_CLAUSE_DECL (*pc) == decl)
15567 break;
15568 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
15569 && OMP_CLAUSE_DECL (*pc) == decl)
15571 if (j)
15572 break;
15573 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
15574 tree c = *pc;
15575 *pc = OMP_CLAUSE_CHAIN (c);
15576 OMP_CLAUSE_CHAIN (c) = *clauses;
15577 *clauses = c;
15579 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
15580 && OMP_CLAUSE_DECL (*pc) == decl)
15582 error ("iteration variable %qD should not be firstprivate",
15583 decl);
15584 *pc = OMP_CLAUSE_CHAIN (*pc);
15586 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
15587 && OMP_CLAUSE_DECL (*pc) == decl)
15589 error ("iteration variable %qD should not be reduction",
15590 decl);
15591 *pc = OMP_CLAUSE_CHAIN (*pc);
15593 else
15594 pc = &OMP_CLAUSE_CHAIN (*pc);
15596 if (*pc)
15597 break;
15599 if (*pc == NULL_TREE)
15601 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
15602 OMP_CLAUSE_DECL (c) = decl;
15603 c = finish_omp_clauses (c, C_ORT_OMP);
15604 if (c)
15606 OMP_CLAUSE_CHAIN (c) = *clauses;
15607 *clauses = c;
15611 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
15612 if (COMPARISON_CLASS_P (cond))
15614 tree op0 = RECUR (TREE_OPERAND (cond, 0));
15615 tree op1 = RECUR (TREE_OPERAND (cond, 1));
15616 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
15618 else
15619 cond = RECUR (cond);
15620 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15621 switch (TREE_CODE (incr))
15623 case PREINCREMENT_EXPR:
15624 case PREDECREMENT_EXPR:
15625 case POSTINCREMENT_EXPR:
15626 case POSTDECREMENT_EXPR:
15627 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
15628 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
15629 break;
15630 case MODIFY_EXPR:
15631 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15632 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15634 tree rhs = TREE_OPERAND (incr, 1);
15635 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15636 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15637 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15638 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15639 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15640 rhs0, rhs1));
15642 else
15643 incr = RECUR (incr);
15644 break;
15645 case MODOP_EXPR:
15646 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
15647 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
15649 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15650 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15651 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
15652 TREE_TYPE (decl), lhs,
15653 RECUR (TREE_OPERAND (incr, 2))));
15655 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
15656 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
15657 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
15659 tree rhs = TREE_OPERAND (incr, 2);
15660 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15661 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
15662 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
15663 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
15664 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
15665 rhs0, rhs1));
15667 else
15668 incr = RECUR (incr);
15669 break;
15670 default:
15671 incr = RECUR (incr);
15672 break;
15675 TREE_VEC_ELT (declv, i) = decl;
15676 TREE_VEC_ELT (initv, i) = init;
15677 TREE_VEC_ELT (condv, i) = cond;
15678 TREE_VEC_ELT (incrv, i) = incr;
15679 #undef RECUR
15682 /* Helper function of tsubst_expr, find OMP_TEAMS inside
15683 of OMP_TARGET's body. */
15685 static tree
15686 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
15688 *walk_subtrees = 0;
15689 switch (TREE_CODE (*tp))
15691 case OMP_TEAMS:
15692 return *tp;
15693 case BIND_EXPR:
15694 case STATEMENT_LIST:
15695 *walk_subtrees = 1;
15696 break;
15697 default:
15698 break;
15700 return NULL_TREE;
15703 /* Helper function for tsubst_expr. For decomposition declaration
15704 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
15705 also the corresponding decls representing the identifiers
15706 of the decomposition declaration. Return DECL if successful
15707 or error_mark_node otherwise, set *FIRST to the first decl
15708 in the list chained through DECL_CHAIN and *CNT to the number
15709 of such decls. */
15711 static tree
15712 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
15713 tsubst_flags_t complain, tree in_decl, tree *first,
15714 unsigned int *cnt)
15716 tree decl2, decl3, prev = decl;
15717 *cnt = 0;
15718 gcc_assert (DECL_NAME (decl) == NULL_TREE);
15719 for (decl2 = DECL_CHAIN (pattern_decl);
15720 decl2
15721 && VAR_P (decl2)
15722 && DECL_DECOMPOSITION_P (decl2)
15723 && DECL_NAME (decl2);
15724 decl2 = DECL_CHAIN (decl2))
15726 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
15728 gcc_assert (errorcount);
15729 return error_mark_node;
15731 (*cnt)++;
15732 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
15733 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
15734 tree v = DECL_VALUE_EXPR (decl2);
15735 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
15736 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
15737 decl3 = tsubst (decl2, args, complain, in_decl);
15738 SET_DECL_VALUE_EXPR (decl2, v);
15739 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
15740 if (VAR_P (decl3))
15741 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
15742 maybe_push_decl (decl3);
15743 if (error_operand_p (decl3))
15744 decl = error_mark_node;
15745 else if (decl != error_mark_node
15746 && DECL_CHAIN (decl3) != prev)
15748 gcc_assert (errorcount);
15749 decl = error_mark_node;
15751 else
15752 prev = decl3;
15754 *first = prev;
15755 return decl;
15758 /* Like tsubst_copy for expressions, etc. but also does semantic
15759 processing. */
15761 tree
15762 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
15763 bool integral_constant_expression_p)
15765 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
15766 #define RECUR(NODE) \
15767 tsubst_expr ((NODE), args, complain, in_decl, \
15768 integral_constant_expression_p)
15770 tree stmt, tmp;
15771 tree r;
15772 location_t loc;
15774 if (t == NULL_TREE || t == error_mark_node)
15775 return t;
15777 loc = input_location;
15778 if (EXPR_HAS_LOCATION (t))
15779 input_location = EXPR_LOCATION (t);
15780 if (STATEMENT_CODE_P (TREE_CODE (t)))
15781 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
15783 switch (TREE_CODE (t))
15785 case STATEMENT_LIST:
15787 tree_stmt_iterator i;
15788 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
15789 RECUR (tsi_stmt (i));
15790 break;
15793 case CTOR_INITIALIZER:
15794 finish_mem_initializers (tsubst_initializer_list
15795 (TREE_OPERAND (t, 0), args));
15796 break;
15798 case RETURN_EXPR:
15799 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
15800 break;
15802 case EXPR_STMT:
15803 tmp = RECUR (EXPR_STMT_EXPR (t));
15804 if (EXPR_STMT_STMT_EXPR_RESULT (t))
15805 finish_stmt_expr_expr (tmp, cur_stmt_expr);
15806 else
15807 finish_expr_stmt (tmp);
15808 break;
15810 case USING_STMT:
15811 finish_local_using_directive (USING_STMT_NAMESPACE (t),
15812 /*attribs=*/NULL_TREE);
15813 break;
15815 case DECL_EXPR:
15817 tree decl, pattern_decl;
15818 tree init;
15820 pattern_decl = decl = DECL_EXPR_DECL (t);
15821 if (TREE_CODE (decl) == LABEL_DECL)
15822 finish_label_decl (DECL_NAME (decl));
15823 else if (TREE_CODE (decl) == USING_DECL)
15825 tree scope = USING_DECL_SCOPE (decl);
15826 tree name = DECL_NAME (decl);
15828 scope = tsubst (scope, args, complain, in_decl);
15829 decl = lookup_qualified_name (scope, name,
15830 /*is_type_p=*/false,
15831 /*complain=*/false);
15832 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
15833 qualified_name_lookup_error (scope, name, decl, input_location);
15834 else
15835 finish_local_using_decl (decl, scope, name);
15837 else if (DECL_PACK_P (decl))
15839 /* Don't build up decls for a variadic capture proxy, we'll
15840 instantiate the elements directly as needed. */
15841 break;
15843 else
15845 init = DECL_INITIAL (decl);
15846 decl = tsubst (decl, args, complain, in_decl);
15847 if (decl != error_mark_node)
15849 /* By marking the declaration as instantiated, we avoid
15850 trying to instantiate it. Since instantiate_decl can't
15851 handle local variables, and since we've already done
15852 all that needs to be done, that's the right thing to
15853 do. */
15854 if (VAR_P (decl))
15855 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
15856 if (VAR_P (decl)
15857 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
15858 /* Anonymous aggregates are a special case. */
15859 finish_anon_union (decl);
15860 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
15862 DECL_CONTEXT (decl) = current_function_decl;
15863 if (DECL_NAME (decl) == this_identifier)
15865 tree lam = DECL_CONTEXT (current_function_decl);
15866 lam = CLASSTYPE_LAMBDA_EXPR (lam);
15867 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
15869 insert_capture_proxy (decl);
15871 else if (DECL_IMPLICIT_TYPEDEF_P (t))
15872 /* We already did a pushtag. */;
15873 else if (TREE_CODE (decl) == FUNCTION_DECL
15874 && DECL_OMP_DECLARE_REDUCTION_P (decl)
15875 && DECL_FUNCTION_SCOPE_P (pattern_decl))
15877 DECL_CONTEXT (decl) = NULL_TREE;
15878 pushdecl (decl);
15879 DECL_CONTEXT (decl) = current_function_decl;
15880 cp_check_omp_declare_reduction (decl);
15882 else
15884 int const_init = false;
15885 maybe_push_decl (decl);
15886 if (VAR_P (decl)
15887 && DECL_PRETTY_FUNCTION_P (decl))
15889 /* For __PRETTY_FUNCTION__ we have to adjust the
15890 initializer. */
15891 const char *const name
15892 = cxx_printable_name (current_function_decl, 2);
15893 init = cp_fname_init (name, &TREE_TYPE (decl));
15895 else
15896 init = tsubst_init (init, decl, args, complain, in_decl);
15898 if (VAR_P (decl))
15899 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
15900 (pattern_decl));
15901 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
15902 if (VAR_P (decl)
15903 && DECL_DECOMPOSITION_P (decl)
15904 && TREE_TYPE (pattern_decl) != error_mark_node)
15906 unsigned int cnt;
15907 tree first;
15908 decl = tsubst_decomp_names (decl, pattern_decl, args,
15909 complain, in_decl, &first,
15910 &cnt);
15911 if (decl != error_mark_node)
15912 cp_finish_decomp (decl, first, cnt);
15918 break;
15921 case FOR_STMT:
15922 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
15923 RECUR (FOR_INIT_STMT (t));
15924 finish_init_stmt (stmt);
15925 tmp = RECUR (FOR_COND (t));
15926 finish_for_cond (tmp, stmt, false);
15927 tmp = RECUR (FOR_EXPR (t));
15928 finish_for_expr (tmp, stmt);
15929 RECUR (FOR_BODY (t));
15930 finish_for_stmt (stmt);
15931 break;
15933 case RANGE_FOR_STMT:
15935 tree decl, expr;
15936 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
15937 decl = RANGE_FOR_DECL (t);
15938 decl = tsubst (decl, args, complain, in_decl);
15939 maybe_push_decl (decl);
15940 expr = RECUR (RANGE_FOR_EXPR (t));
15941 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
15943 unsigned int cnt;
15944 tree first;
15945 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
15946 complain, in_decl, &first, &cnt);
15947 stmt = cp_convert_range_for (stmt, decl, expr, first, cnt,
15948 RANGE_FOR_IVDEP (t));
15950 else
15951 stmt = cp_convert_range_for (stmt, decl, expr, NULL_TREE, 0,
15952 RANGE_FOR_IVDEP (t));
15953 RECUR (RANGE_FOR_BODY (t));
15954 finish_for_stmt (stmt);
15956 break;
15958 case WHILE_STMT:
15959 stmt = begin_while_stmt ();
15960 tmp = RECUR (WHILE_COND (t));
15961 finish_while_stmt_cond (tmp, stmt, false);
15962 RECUR (WHILE_BODY (t));
15963 finish_while_stmt (stmt);
15964 break;
15966 case DO_STMT:
15967 stmt = begin_do_stmt ();
15968 RECUR (DO_BODY (t));
15969 finish_do_body (stmt);
15970 tmp = RECUR (DO_COND (t));
15971 finish_do_stmt (tmp, stmt, false);
15972 break;
15974 case IF_STMT:
15975 stmt = begin_if_stmt ();
15976 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
15977 tmp = RECUR (IF_COND (t));
15978 tmp = finish_if_stmt_cond (tmp, stmt);
15979 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
15980 /* Don't instantiate the THEN_CLAUSE. */;
15981 else
15983 bool inhibit = integer_zerop (fold_non_dependent_expr (tmp));
15984 if (inhibit)
15985 ++c_inhibit_evaluation_warnings;
15986 RECUR (THEN_CLAUSE (t));
15987 if (inhibit)
15988 --c_inhibit_evaluation_warnings;
15990 finish_then_clause (stmt);
15992 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
15993 /* Don't instantiate the ELSE_CLAUSE. */;
15994 else if (ELSE_CLAUSE (t))
15996 bool inhibit = integer_nonzerop (fold_non_dependent_expr (tmp));
15997 begin_else_clause (stmt);
15998 if (inhibit)
15999 ++c_inhibit_evaluation_warnings;
16000 RECUR (ELSE_CLAUSE (t));
16001 if (inhibit)
16002 --c_inhibit_evaluation_warnings;
16003 finish_else_clause (stmt);
16006 finish_if_stmt (stmt);
16007 break;
16009 case BIND_EXPR:
16010 if (BIND_EXPR_BODY_BLOCK (t))
16011 stmt = begin_function_body ();
16012 else
16013 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
16014 ? BCS_TRY_BLOCK : 0);
16016 RECUR (BIND_EXPR_BODY (t));
16018 if (BIND_EXPR_BODY_BLOCK (t))
16019 finish_function_body (stmt);
16020 else
16021 finish_compound_stmt (stmt);
16022 break;
16024 case BREAK_STMT:
16025 finish_break_stmt ();
16026 break;
16028 case CONTINUE_STMT:
16029 finish_continue_stmt ();
16030 break;
16032 case SWITCH_STMT:
16033 stmt = begin_switch_stmt ();
16034 tmp = RECUR (SWITCH_STMT_COND (t));
16035 finish_switch_cond (tmp, stmt);
16036 RECUR (SWITCH_STMT_BODY (t));
16037 finish_switch_stmt (stmt);
16038 break;
16040 case CASE_LABEL_EXPR:
16042 tree low = RECUR (CASE_LOW (t));
16043 tree high = RECUR (CASE_HIGH (t));
16044 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
16045 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
16046 FALLTHROUGH_LABEL_P (CASE_LABEL (l))
16047 = FALLTHROUGH_LABEL_P (CASE_LABEL (t));
16049 break;
16051 case LABEL_EXPR:
16053 tree decl = LABEL_EXPR_LABEL (t);
16054 tree label;
16056 label = finish_label_stmt (DECL_NAME (decl));
16057 if (TREE_CODE (label) == LABEL_DECL)
16058 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
16059 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
16060 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
16062 break;
16064 case GOTO_EXPR:
16065 tmp = GOTO_DESTINATION (t);
16066 if (TREE_CODE (tmp) != LABEL_DECL)
16067 /* Computed goto's must be tsubst'd into. On the other hand,
16068 non-computed gotos must not be; the identifier in question
16069 will have no binding. */
16070 tmp = RECUR (tmp);
16071 else
16072 tmp = DECL_NAME (tmp);
16073 finish_goto_stmt (tmp);
16074 break;
16076 case ASM_EXPR:
16078 tree string = RECUR (ASM_STRING (t));
16079 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
16080 complain, in_decl);
16081 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
16082 complain, in_decl);
16083 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
16084 complain, in_decl);
16085 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
16086 complain, in_decl);
16087 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
16088 clobbers, labels);
16089 tree asm_expr = tmp;
16090 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
16091 asm_expr = TREE_OPERAND (asm_expr, 0);
16092 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
16094 break;
16096 case TRY_BLOCK:
16097 if (CLEANUP_P (t))
16099 stmt = begin_try_block ();
16100 RECUR (TRY_STMTS (t));
16101 finish_cleanup_try_block (stmt);
16102 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
16104 else
16106 tree compound_stmt = NULL_TREE;
16108 if (FN_TRY_BLOCK_P (t))
16109 stmt = begin_function_try_block (&compound_stmt);
16110 else
16111 stmt = begin_try_block ();
16113 RECUR (TRY_STMTS (t));
16115 if (FN_TRY_BLOCK_P (t))
16116 finish_function_try_block (stmt);
16117 else
16118 finish_try_block (stmt);
16120 RECUR (TRY_HANDLERS (t));
16121 if (FN_TRY_BLOCK_P (t))
16122 finish_function_handler_sequence (stmt, compound_stmt);
16123 else
16124 finish_handler_sequence (stmt);
16126 break;
16128 case HANDLER:
16130 tree decl = HANDLER_PARMS (t);
16132 if (decl)
16134 decl = tsubst (decl, args, complain, in_decl);
16135 /* Prevent instantiate_decl from trying to instantiate
16136 this variable. We've already done all that needs to be
16137 done. */
16138 if (decl != error_mark_node)
16139 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16141 stmt = begin_handler ();
16142 finish_handler_parms (decl, stmt);
16143 RECUR (HANDLER_BODY (t));
16144 finish_handler (stmt);
16146 break;
16148 case TAG_DEFN:
16149 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
16150 if (CLASS_TYPE_P (tmp))
16152 /* Local classes are not independent templates; they are
16153 instantiated along with their containing function. And this
16154 way we don't have to deal with pushing out of one local class
16155 to instantiate a member of another local class. */
16156 tree fn;
16157 /* Closures are handled by the LAMBDA_EXPR. */
16158 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
16159 complete_type (tmp);
16160 for (fn = TYPE_METHODS (tmp); fn; fn = DECL_CHAIN (fn))
16161 if (!DECL_ARTIFICIAL (fn))
16162 instantiate_decl (fn, /*defer_ok=*/false,
16163 /*expl_inst_class=*/false);
16165 break;
16167 case STATIC_ASSERT:
16169 tree condition;
16171 ++c_inhibit_evaluation_warnings;
16172 condition =
16173 tsubst_expr (STATIC_ASSERT_CONDITION (t),
16174 args,
16175 complain, in_decl,
16176 /*integral_constant_expression_p=*/true);
16177 --c_inhibit_evaluation_warnings;
16179 finish_static_assert (condition,
16180 STATIC_ASSERT_MESSAGE (t),
16181 STATIC_ASSERT_SOURCE_LOCATION (t),
16182 /*member_p=*/false);
16184 break;
16186 case OACC_KERNELS:
16187 case OACC_PARALLEL:
16188 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
16189 in_decl);
16190 stmt = begin_omp_parallel ();
16191 RECUR (OMP_BODY (t));
16192 finish_omp_construct (TREE_CODE (t), stmt, tmp);
16193 break;
16195 case OMP_PARALLEL:
16196 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
16197 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
16198 complain, in_decl);
16199 if (OMP_PARALLEL_COMBINED (t))
16200 omp_parallel_combined_clauses = &tmp;
16201 stmt = begin_omp_parallel ();
16202 RECUR (OMP_PARALLEL_BODY (t));
16203 gcc_assert (omp_parallel_combined_clauses == NULL);
16204 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
16205 = OMP_PARALLEL_COMBINED (t);
16206 pop_omp_privatization_clauses (r);
16207 break;
16209 case OMP_TASK:
16210 r = push_omp_privatization_clauses (false);
16211 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
16212 complain, in_decl);
16213 stmt = begin_omp_task ();
16214 RECUR (OMP_TASK_BODY (t));
16215 finish_omp_task (tmp, stmt);
16216 pop_omp_privatization_clauses (r);
16217 break;
16219 case OMP_FOR:
16220 case OMP_SIMD:
16221 case CILK_SIMD:
16222 case CILK_FOR:
16223 case OMP_DISTRIBUTE:
16224 case OMP_TASKLOOP:
16225 case OACC_LOOP:
16227 tree clauses, body, pre_body;
16228 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
16229 tree orig_declv = NULL_TREE;
16230 tree incrv = NULL_TREE;
16231 enum c_omp_region_type ort = C_ORT_OMP;
16232 int i;
16234 if (TREE_CODE (t) == CILK_SIMD || TREE_CODE (t) == CILK_FOR)
16235 ort = C_ORT_CILK;
16236 else if (TREE_CODE (t) == OACC_LOOP)
16237 ort = C_ORT_ACC;
16239 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
16240 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
16241 in_decl);
16242 if (OMP_FOR_INIT (t) != NULL_TREE)
16244 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16245 if (OMP_FOR_ORIG_DECLS (t))
16246 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16247 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16248 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16249 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16252 stmt = begin_omp_structured_block ();
16254 pre_body = push_stmt_list ();
16255 RECUR (OMP_FOR_PRE_BODY (t));
16256 pre_body = pop_stmt_list (pre_body);
16258 if (OMP_FOR_INIT (t) != NULL_TREE)
16259 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
16260 tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, condv,
16261 incrv, &clauses, args, complain, in_decl,
16262 integral_constant_expression_p);
16263 omp_parallel_combined_clauses = NULL;
16265 body = push_stmt_list ();
16266 RECUR (OMP_FOR_BODY (t));
16267 body = pop_stmt_list (body);
16269 if (OMP_FOR_INIT (t) != NULL_TREE)
16270 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
16271 orig_declv, initv, condv, incrv, body, pre_body,
16272 NULL, clauses);
16273 else
16275 t = make_node (TREE_CODE (t));
16276 TREE_TYPE (t) = void_type_node;
16277 OMP_FOR_BODY (t) = body;
16278 OMP_FOR_PRE_BODY (t) = pre_body;
16279 OMP_FOR_CLAUSES (t) = clauses;
16280 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
16281 add_stmt (t);
16284 add_stmt (finish_omp_structured_block (stmt));
16285 pop_omp_privatization_clauses (r);
16287 break;
16289 case OMP_SECTIONS:
16290 omp_parallel_combined_clauses = NULL;
16291 /* FALLTHRU */
16292 case OMP_SINGLE:
16293 case OMP_TEAMS:
16294 case OMP_CRITICAL:
16295 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
16296 && OMP_TEAMS_COMBINED (t));
16297 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
16298 in_decl);
16299 stmt = push_stmt_list ();
16300 RECUR (OMP_BODY (t));
16301 stmt = pop_stmt_list (stmt);
16303 t = copy_node (t);
16304 OMP_BODY (t) = stmt;
16305 OMP_CLAUSES (t) = tmp;
16306 add_stmt (t);
16307 pop_omp_privatization_clauses (r);
16308 break;
16310 case OACC_DATA:
16311 case OMP_TARGET_DATA:
16312 case OMP_TARGET:
16313 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
16314 ? C_ORT_ACC : C_ORT_OMP, args, complain,
16315 in_decl);
16316 keep_next_level (true);
16317 stmt = begin_omp_structured_block ();
16319 RECUR (OMP_BODY (t));
16320 stmt = finish_omp_structured_block (stmt);
16322 t = copy_node (t);
16323 OMP_BODY (t) = stmt;
16324 OMP_CLAUSES (t) = tmp;
16325 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
16327 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
16328 if (teams)
16330 /* For combined target teams, ensure the num_teams and
16331 thread_limit clause expressions are evaluated on the host,
16332 before entering the target construct. */
16333 tree c;
16334 for (c = OMP_TEAMS_CLAUSES (teams);
16335 c; c = OMP_CLAUSE_CHAIN (c))
16336 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
16337 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
16338 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
16340 tree expr = OMP_CLAUSE_OPERAND (c, 0);
16341 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
16342 if (expr == error_mark_node)
16343 continue;
16344 tmp = TARGET_EXPR_SLOT (expr);
16345 add_stmt (expr);
16346 OMP_CLAUSE_OPERAND (c, 0) = expr;
16347 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16348 OMP_CLAUSE_FIRSTPRIVATE);
16349 OMP_CLAUSE_DECL (tc) = tmp;
16350 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
16351 OMP_TARGET_CLAUSES (t) = tc;
16355 add_stmt (t);
16356 break;
16358 case OACC_DECLARE:
16359 t = copy_node (t);
16360 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
16361 complain, in_decl);
16362 OACC_DECLARE_CLAUSES (t) = tmp;
16363 add_stmt (t);
16364 break;
16366 case OMP_TARGET_UPDATE:
16367 case OMP_TARGET_ENTER_DATA:
16368 case OMP_TARGET_EXIT_DATA:
16369 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
16370 complain, in_decl);
16371 t = copy_node (t);
16372 OMP_STANDALONE_CLAUSES (t) = tmp;
16373 add_stmt (t);
16374 break;
16376 case OACC_ENTER_DATA:
16377 case OACC_EXIT_DATA:
16378 case OACC_UPDATE:
16379 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
16380 complain, in_decl);
16381 t = copy_node (t);
16382 OMP_STANDALONE_CLAUSES (t) = tmp;
16383 add_stmt (t);
16384 break;
16386 case OMP_ORDERED:
16387 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
16388 complain, in_decl);
16389 stmt = push_stmt_list ();
16390 RECUR (OMP_BODY (t));
16391 stmt = pop_stmt_list (stmt);
16393 t = copy_node (t);
16394 OMP_BODY (t) = stmt;
16395 OMP_ORDERED_CLAUSES (t) = tmp;
16396 add_stmt (t);
16397 break;
16399 case OMP_SECTION:
16400 case OMP_MASTER:
16401 case OMP_TASKGROUP:
16402 stmt = push_stmt_list ();
16403 RECUR (OMP_BODY (t));
16404 stmt = pop_stmt_list (stmt);
16406 t = copy_node (t);
16407 OMP_BODY (t) = stmt;
16408 add_stmt (t);
16409 break;
16411 case OMP_ATOMIC:
16412 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
16413 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
16415 tree op1 = TREE_OPERAND (t, 1);
16416 tree rhs1 = NULL_TREE;
16417 tree lhs, rhs;
16418 if (TREE_CODE (op1) == COMPOUND_EXPR)
16420 rhs1 = RECUR (TREE_OPERAND (op1, 0));
16421 op1 = TREE_OPERAND (op1, 1);
16423 lhs = RECUR (TREE_OPERAND (op1, 0));
16424 rhs = RECUR (TREE_OPERAND (op1, 1));
16425 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
16426 NULL_TREE, NULL_TREE, rhs1,
16427 OMP_ATOMIC_SEQ_CST (t));
16429 else
16431 tree op1 = TREE_OPERAND (t, 1);
16432 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
16433 tree rhs1 = NULL_TREE;
16434 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
16435 enum tree_code opcode = NOP_EXPR;
16436 if (code == OMP_ATOMIC_READ)
16438 v = RECUR (TREE_OPERAND (op1, 0));
16439 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16441 else if (code == OMP_ATOMIC_CAPTURE_OLD
16442 || code == OMP_ATOMIC_CAPTURE_NEW)
16444 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
16445 v = RECUR (TREE_OPERAND (op1, 0));
16446 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16447 if (TREE_CODE (op11) == COMPOUND_EXPR)
16449 rhs1 = RECUR (TREE_OPERAND (op11, 0));
16450 op11 = TREE_OPERAND (op11, 1);
16452 lhs = RECUR (TREE_OPERAND (op11, 0));
16453 rhs = RECUR (TREE_OPERAND (op11, 1));
16454 opcode = TREE_CODE (op11);
16455 if (opcode == MODIFY_EXPR)
16456 opcode = NOP_EXPR;
16458 else
16460 code = OMP_ATOMIC;
16461 lhs = RECUR (TREE_OPERAND (op1, 0));
16462 rhs = RECUR (TREE_OPERAND (op1, 1));
16464 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
16465 OMP_ATOMIC_SEQ_CST (t));
16467 break;
16469 case TRANSACTION_EXPR:
16471 int flags = 0;
16472 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
16473 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
16475 if (TRANSACTION_EXPR_IS_STMT (t))
16477 tree body = TRANSACTION_EXPR_BODY (t);
16478 tree noex = NULL_TREE;
16479 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
16481 noex = MUST_NOT_THROW_COND (body);
16482 if (noex == NULL_TREE)
16483 noex = boolean_true_node;
16484 body = TREE_OPERAND (body, 0);
16486 stmt = begin_transaction_stmt (input_location, NULL, flags);
16487 RECUR (body);
16488 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
16490 else
16492 stmt = build_transaction_expr (EXPR_LOCATION (t),
16493 RECUR (TRANSACTION_EXPR_BODY (t)),
16494 flags, NULL_TREE);
16495 RETURN (stmt);
16498 break;
16500 case MUST_NOT_THROW_EXPR:
16502 tree op0 = RECUR (TREE_OPERAND (t, 0));
16503 tree cond = RECUR (MUST_NOT_THROW_COND (t));
16504 RETURN (build_must_not_throw_expr (op0, cond));
16507 case EXPR_PACK_EXPANSION:
16508 error ("invalid use of pack expansion expression");
16509 RETURN (error_mark_node);
16511 case NONTYPE_ARGUMENT_PACK:
16512 error ("use %<...%> to expand argument pack");
16513 RETURN (error_mark_node);
16515 case CILK_SPAWN_STMT:
16516 cfun->calls_cilk_spawn = 1;
16517 RETURN (build_cilk_spawn (EXPR_LOCATION (t), RECUR (CILK_SPAWN_FN (t))));
16519 case CILK_SYNC_STMT:
16520 RETURN (build_cilk_sync ());
16522 case COMPOUND_EXPR:
16523 tmp = RECUR (TREE_OPERAND (t, 0));
16524 if (tmp == NULL_TREE)
16525 /* If the first operand was a statement, we're done with it. */
16526 RETURN (RECUR (TREE_OPERAND (t, 1)));
16527 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
16528 RECUR (TREE_OPERAND (t, 1)),
16529 complain));
16531 case ANNOTATE_EXPR:
16532 tmp = RECUR (TREE_OPERAND (t, 0));
16533 RETURN (build2_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
16534 TREE_TYPE (tmp), tmp, RECUR (TREE_OPERAND (t, 1))));
16536 default:
16537 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
16539 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
16540 /*function_p=*/false,
16541 integral_constant_expression_p));
16544 RETURN (NULL_TREE);
16545 out:
16546 input_location = loc;
16547 return r;
16548 #undef RECUR
16549 #undef RETURN
16552 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
16553 function. For description of the body see comment above
16554 cp_parser_omp_declare_reduction_exprs. */
16556 static void
16557 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
16559 if (t == NULL_TREE || t == error_mark_node)
16560 return;
16562 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
16564 tree_stmt_iterator tsi;
16565 int i;
16566 tree stmts[7];
16567 memset (stmts, 0, sizeof stmts);
16568 for (i = 0, tsi = tsi_start (t);
16569 i < 7 && !tsi_end_p (tsi);
16570 i++, tsi_next (&tsi))
16571 stmts[i] = tsi_stmt (tsi);
16572 gcc_assert (tsi_end_p (tsi));
16574 if (i >= 3)
16576 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
16577 && TREE_CODE (stmts[1]) == DECL_EXPR);
16578 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
16579 args, complain, in_decl);
16580 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
16581 args, complain, in_decl);
16582 DECL_CONTEXT (omp_out) = current_function_decl;
16583 DECL_CONTEXT (omp_in) = current_function_decl;
16584 keep_next_level (true);
16585 tree block = begin_omp_structured_block ();
16586 tsubst_expr (stmts[2], args, complain, in_decl, false);
16587 block = finish_omp_structured_block (block);
16588 block = maybe_cleanup_point_expr_void (block);
16589 add_decl_expr (omp_out);
16590 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
16591 TREE_NO_WARNING (omp_out) = 1;
16592 add_decl_expr (omp_in);
16593 finish_expr_stmt (block);
16595 if (i >= 6)
16597 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
16598 && TREE_CODE (stmts[4]) == DECL_EXPR);
16599 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
16600 args, complain, in_decl);
16601 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
16602 args, complain, in_decl);
16603 DECL_CONTEXT (omp_priv) = current_function_decl;
16604 DECL_CONTEXT (omp_orig) = current_function_decl;
16605 keep_next_level (true);
16606 tree block = begin_omp_structured_block ();
16607 tsubst_expr (stmts[5], args, complain, in_decl, false);
16608 block = finish_omp_structured_block (block);
16609 block = maybe_cleanup_point_expr_void (block);
16610 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
16611 add_decl_expr (omp_priv);
16612 add_decl_expr (omp_orig);
16613 finish_expr_stmt (block);
16614 if (i == 7)
16615 add_decl_expr (omp_orig);
16619 /* T is a postfix-expression that is not being used in a function
16620 call. Return the substituted version of T. */
16622 static tree
16623 tsubst_non_call_postfix_expression (tree t, tree args,
16624 tsubst_flags_t complain,
16625 tree in_decl)
16627 if (TREE_CODE (t) == SCOPE_REF)
16628 t = tsubst_qualified_id (t, args, complain, in_decl,
16629 /*done=*/false, /*address_p=*/false);
16630 else
16631 t = tsubst_copy_and_build (t, args, complain, in_decl,
16632 /*function_p=*/false,
16633 /*integral_constant_expression_p=*/false);
16635 return t;
16638 /* Like tsubst but deals with expressions and performs semantic
16639 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
16641 tree
16642 tsubst_copy_and_build (tree t,
16643 tree args,
16644 tsubst_flags_t complain,
16645 tree in_decl,
16646 bool function_p,
16647 bool integral_constant_expression_p)
16649 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
16650 #define RECUR(NODE) \
16651 tsubst_copy_and_build (NODE, args, complain, in_decl, \
16652 /*function_p=*/false, \
16653 integral_constant_expression_p)
16655 tree retval, op1;
16656 location_t loc;
16658 if (t == NULL_TREE || t == error_mark_node)
16659 return t;
16661 loc = input_location;
16662 if (EXPR_HAS_LOCATION (t))
16663 input_location = EXPR_LOCATION (t);
16665 /* N3276 decltype magic only applies to calls at the top level or on the
16666 right side of a comma. */
16667 tsubst_flags_t decltype_flag = (complain & tf_decltype);
16668 complain &= ~tf_decltype;
16670 switch (TREE_CODE (t))
16672 case USING_DECL:
16673 t = DECL_NAME (t);
16674 /* Fall through. */
16675 case IDENTIFIER_NODE:
16677 tree decl;
16678 cp_id_kind idk;
16679 bool non_integral_constant_expression_p;
16680 const char *error_msg;
16682 if (IDENTIFIER_TYPENAME_P (t))
16684 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16685 t = mangle_conv_op_name_for_type (new_type);
16688 /* Look up the name. */
16689 decl = lookup_name (t);
16691 /* By convention, expressions use ERROR_MARK_NODE to indicate
16692 failure, not NULL_TREE. */
16693 if (decl == NULL_TREE)
16694 decl = error_mark_node;
16696 decl = finish_id_expression (t, decl, NULL_TREE,
16697 &idk,
16698 integral_constant_expression_p,
16699 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
16700 &non_integral_constant_expression_p,
16701 /*template_p=*/false,
16702 /*done=*/true,
16703 /*address_p=*/false,
16704 /*template_arg_p=*/false,
16705 &error_msg,
16706 input_location);
16707 if (error_msg)
16708 error (error_msg);
16709 if (!function_p && identifier_p (decl))
16711 if (complain & tf_error)
16712 unqualified_name_lookup_error (decl);
16713 decl = error_mark_node;
16715 RETURN (decl);
16718 case TEMPLATE_ID_EXPR:
16720 tree object;
16721 tree templ = RECUR (TREE_OPERAND (t, 0));
16722 tree targs = TREE_OPERAND (t, 1);
16724 if (targs)
16725 targs = tsubst_template_args (targs, args, complain, in_decl);
16726 if (targs == error_mark_node)
16727 return error_mark_node;
16729 if (TREE_CODE (templ) == SCOPE_REF)
16731 tree name = TREE_OPERAND (templ, 1);
16732 tree tid = lookup_template_function (name, targs);
16733 TREE_OPERAND (templ, 1) = tid;
16734 return templ;
16737 if (variable_template_p (templ))
16738 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
16740 if (TREE_CODE (templ) == COMPONENT_REF)
16742 object = TREE_OPERAND (templ, 0);
16743 templ = TREE_OPERAND (templ, 1);
16745 else
16746 object = NULL_TREE;
16747 templ = lookup_template_function (templ, targs);
16749 if (object)
16750 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
16751 object, templ, NULL_TREE));
16752 else
16753 RETURN (baselink_for_fns (templ));
16756 case INDIRECT_REF:
16758 tree r = RECUR (TREE_OPERAND (t, 0));
16760 if (REFERENCE_REF_P (t))
16762 /* A type conversion to reference type will be enclosed in
16763 such an indirect ref, but the substitution of the cast
16764 will have also added such an indirect ref. */
16765 if (TREE_CODE (TREE_TYPE (r)) == REFERENCE_TYPE)
16766 r = convert_from_reference (r);
16768 else
16769 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
16770 complain|decltype_flag);
16772 if (TREE_CODE (r) == INDIRECT_REF)
16773 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
16775 RETURN (r);
16778 case NOP_EXPR:
16780 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16781 tree op0 = RECUR (TREE_OPERAND (t, 0));
16782 RETURN (build_nop (type, op0));
16785 case IMPLICIT_CONV_EXPR:
16787 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16788 tree expr = RECUR (TREE_OPERAND (t, 0));
16789 int flags = LOOKUP_IMPLICIT;
16790 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
16791 flags = LOOKUP_NORMAL;
16792 RETURN (perform_implicit_conversion_flags (type, expr, complain,
16793 flags));
16796 case CONVERT_EXPR:
16798 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16799 tree op0 = RECUR (TREE_OPERAND (t, 0));
16800 RETURN (build1 (CONVERT_EXPR, type, op0));
16803 case CAST_EXPR:
16804 case REINTERPRET_CAST_EXPR:
16805 case CONST_CAST_EXPR:
16806 case DYNAMIC_CAST_EXPR:
16807 case STATIC_CAST_EXPR:
16809 tree type;
16810 tree op, r = NULL_TREE;
16812 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
16813 if (integral_constant_expression_p
16814 && !cast_valid_in_integral_constant_expression_p (type))
16816 if (complain & tf_error)
16817 error ("a cast to a type other than an integral or "
16818 "enumeration type cannot appear in a constant-expression");
16819 RETURN (error_mark_node);
16822 op = RECUR (TREE_OPERAND (t, 0));
16824 warning_sentinel s(warn_useless_cast);
16825 switch (TREE_CODE (t))
16827 case CAST_EXPR:
16828 r = build_functional_cast (type, op, complain);
16829 break;
16830 case REINTERPRET_CAST_EXPR:
16831 r = build_reinterpret_cast (type, op, complain);
16832 break;
16833 case CONST_CAST_EXPR:
16834 r = build_const_cast (type, op, complain);
16835 break;
16836 case DYNAMIC_CAST_EXPR:
16837 r = build_dynamic_cast (type, op, complain);
16838 break;
16839 case STATIC_CAST_EXPR:
16840 r = build_static_cast (type, op, complain);
16841 break;
16842 default:
16843 gcc_unreachable ();
16846 RETURN (r);
16849 case POSTDECREMENT_EXPR:
16850 case POSTINCREMENT_EXPR:
16851 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16852 args, complain, in_decl);
16853 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
16854 complain|decltype_flag));
16856 case PREDECREMENT_EXPR:
16857 case PREINCREMENT_EXPR:
16858 case NEGATE_EXPR:
16859 case BIT_NOT_EXPR:
16860 case ABS_EXPR:
16861 case TRUTH_NOT_EXPR:
16862 case UNARY_PLUS_EXPR: /* Unary + */
16863 case REALPART_EXPR:
16864 case IMAGPART_EXPR:
16865 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
16866 RECUR (TREE_OPERAND (t, 0)),
16867 complain|decltype_flag));
16869 case FIX_TRUNC_EXPR:
16870 RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
16871 false, complain));
16873 case ADDR_EXPR:
16874 op1 = TREE_OPERAND (t, 0);
16875 if (TREE_CODE (op1) == LABEL_DECL)
16876 RETURN (finish_label_address_expr (DECL_NAME (op1),
16877 EXPR_LOCATION (op1)));
16878 if (TREE_CODE (op1) == SCOPE_REF)
16879 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
16880 /*done=*/true, /*address_p=*/true);
16881 else
16882 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
16883 in_decl);
16884 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
16885 complain|decltype_flag));
16887 case PLUS_EXPR:
16888 case MINUS_EXPR:
16889 case MULT_EXPR:
16890 case TRUNC_DIV_EXPR:
16891 case CEIL_DIV_EXPR:
16892 case FLOOR_DIV_EXPR:
16893 case ROUND_DIV_EXPR:
16894 case EXACT_DIV_EXPR:
16895 case BIT_AND_EXPR:
16896 case BIT_IOR_EXPR:
16897 case BIT_XOR_EXPR:
16898 case TRUNC_MOD_EXPR:
16899 case FLOOR_MOD_EXPR:
16900 case TRUTH_ANDIF_EXPR:
16901 case TRUTH_ORIF_EXPR:
16902 case TRUTH_AND_EXPR:
16903 case TRUTH_OR_EXPR:
16904 case RSHIFT_EXPR:
16905 case LSHIFT_EXPR:
16906 case RROTATE_EXPR:
16907 case LROTATE_EXPR:
16908 case EQ_EXPR:
16909 case NE_EXPR:
16910 case MAX_EXPR:
16911 case MIN_EXPR:
16912 case LE_EXPR:
16913 case GE_EXPR:
16914 case LT_EXPR:
16915 case GT_EXPR:
16916 case MEMBER_REF:
16917 case DOTSTAR_EXPR:
16919 warning_sentinel s1(warn_type_limits);
16920 warning_sentinel s2(warn_div_by_zero);
16921 warning_sentinel s3(warn_logical_op);
16922 warning_sentinel s4(warn_tautological_compare);
16923 tree op0 = RECUR (TREE_OPERAND (t, 0));
16924 tree op1 = RECUR (TREE_OPERAND (t, 1));
16925 tree r = build_x_binary_op
16926 (input_location, TREE_CODE (t),
16927 op0,
16928 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
16929 ? ERROR_MARK
16930 : TREE_CODE (TREE_OPERAND (t, 0))),
16931 op1,
16932 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
16933 ? ERROR_MARK
16934 : TREE_CODE (TREE_OPERAND (t, 1))),
16935 /*overload=*/NULL,
16936 complain|decltype_flag);
16937 if (EXPR_P (r) && TREE_NO_WARNING (t))
16938 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
16940 RETURN (r);
16943 case POINTER_PLUS_EXPR:
16945 tree op0 = RECUR (TREE_OPERAND (t, 0));
16946 tree op1 = RECUR (TREE_OPERAND (t, 1));
16947 return fold_build_pointer_plus (op0, op1);
16950 case SCOPE_REF:
16951 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
16952 /*address_p=*/false));
16953 case ARRAY_REF:
16954 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
16955 args, complain, in_decl);
16956 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
16957 RECUR (TREE_OPERAND (t, 1)),
16958 complain|decltype_flag));
16960 case ARRAY_NOTATION_REF:
16962 tree start_index, length, stride;
16963 op1 = tsubst_non_call_postfix_expression (ARRAY_NOTATION_ARRAY (t),
16964 args, complain, in_decl);
16965 start_index = RECUR (ARRAY_NOTATION_START (t));
16966 length = RECUR (ARRAY_NOTATION_LENGTH (t));
16967 stride = RECUR (ARRAY_NOTATION_STRIDE (t));
16968 RETURN (build_array_notation_ref (EXPR_LOCATION (t), op1, start_index,
16969 length, stride, TREE_TYPE (op1)));
16971 case SIZEOF_EXPR:
16972 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
16973 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
16974 RETURN (tsubst_copy (t, args, complain, in_decl));
16975 /* Fall through */
16977 case ALIGNOF_EXPR:
16979 tree r;
16981 op1 = TREE_OPERAND (t, 0);
16982 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
16983 op1 = TREE_TYPE (op1);
16984 if (!args)
16986 /* When there are no ARGS, we are trying to evaluate a
16987 non-dependent expression from the parser. Trying to do
16988 the substitutions may not work. */
16989 if (!TYPE_P (op1))
16990 op1 = TREE_TYPE (op1);
16992 else
16994 ++cp_unevaluated_operand;
16995 ++c_inhibit_evaluation_warnings;
16996 if (TYPE_P (op1))
16997 op1 = tsubst (op1, args, complain, in_decl);
16998 else
16999 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17000 /*function_p=*/false,
17001 /*integral_constant_expression_p=*/
17002 false);
17003 --cp_unevaluated_operand;
17004 --c_inhibit_evaluation_warnings;
17006 if (TYPE_P (op1))
17007 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
17008 complain & tf_error);
17009 else
17010 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
17011 complain & tf_error);
17012 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
17014 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
17016 if (!processing_template_decl && TYPE_P (op1))
17018 r = build_min (SIZEOF_EXPR, size_type_node,
17019 build1 (NOP_EXPR, op1, error_mark_node));
17020 SIZEOF_EXPR_TYPE_P (r) = 1;
17022 else
17023 r = build_min (SIZEOF_EXPR, size_type_node, op1);
17024 TREE_SIDE_EFFECTS (r) = 0;
17025 TREE_READONLY (r) = 1;
17027 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
17029 RETURN (r);
17032 case AT_ENCODE_EXPR:
17034 op1 = TREE_OPERAND (t, 0);
17035 ++cp_unevaluated_operand;
17036 ++c_inhibit_evaluation_warnings;
17037 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17038 /*function_p=*/false,
17039 /*integral_constant_expression_p=*/false);
17040 --cp_unevaluated_operand;
17041 --c_inhibit_evaluation_warnings;
17042 RETURN (objc_build_encode_expr (op1));
17045 case NOEXCEPT_EXPR:
17046 op1 = TREE_OPERAND (t, 0);
17047 ++cp_unevaluated_operand;
17048 ++c_inhibit_evaluation_warnings;
17049 ++cp_noexcept_operand;
17050 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17051 /*function_p=*/false,
17052 /*integral_constant_expression_p=*/false);
17053 --cp_unevaluated_operand;
17054 --c_inhibit_evaluation_warnings;
17055 --cp_noexcept_operand;
17056 RETURN (finish_noexcept_expr (op1, complain));
17058 case MODOP_EXPR:
17060 warning_sentinel s(warn_div_by_zero);
17061 tree lhs = RECUR (TREE_OPERAND (t, 0));
17062 tree rhs = RECUR (TREE_OPERAND (t, 2));
17063 tree r = build_x_modify_expr
17064 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
17065 complain|decltype_flag);
17066 /* TREE_NO_WARNING must be set if either the expression was
17067 parenthesized or it uses an operator such as >>= rather
17068 than plain assignment. In the former case, it was already
17069 set and must be copied. In the latter case,
17070 build_x_modify_expr sets it and it must not be reset
17071 here. */
17072 if (TREE_NO_WARNING (t))
17073 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17075 RETURN (r);
17078 case ARROW_EXPR:
17079 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17080 args, complain, in_decl);
17081 /* Remember that there was a reference to this entity. */
17082 if (DECL_P (op1)
17083 && !mark_used (op1, complain) && !(complain & tf_error))
17084 RETURN (error_mark_node);
17085 RETURN (build_x_arrow (input_location, op1, complain));
17087 case NEW_EXPR:
17089 tree placement = RECUR (TREE_OPERAND (t, 0));
17090 tree init = RECUR (TREE_OPERAND (t, 3));
17091 vec<tree, va_gc> *placement_vec;
17092 vec<tree, va_gc> *init_vec;
17093 tree ret;
17095 if (placement == NULL_TREE)
17096 placement_vec = NULL;
17097 else
17099 placement_vec = make_tree_vector ();
17100 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
17101 vec_safe_push (placement_vec, TREE_VALUE (placement));
17104 /* If there was an initializer in the original tree, but it
17105 instantiated to an empty list, then we should pass a
17106 non-NULL empty vector to tell build_new that it was an
17107 empty initializer() rather than no initializer. This can
17108 only happen when the initializer is a pack expansion whose
17109 parameter packs are of length zero. */
17110 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
17111 init_vec = NULL;
17112 else
17114 init_vec = make_tree_vector ();
17115 if (init == void_node)
17116 gcc_assert (init_vec != NULL);
17117 else
17119 for (; init != NULL_TREE; init = TREE_CHAIN (init))
17120 vec_safe_push (init_vec, TREE_VALUE (init));
17124 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
17125 tree op2 = RECUR (TREE_OPERAND (t, 2));
17126 ret = build_new (&placement_vec, op1, op2, &init_vec,
17127 NEW_EXPR_USE_GLOBAL (t),
17128 complain);
17130 if (placement_vec != NULL)
17131 release_tree_vector (placement_vec);
17132 if (init_vec != NULL)
17133 release_tree_vector (init_vec);
17135 RETURN (ret);
17138 case DELETE_EXPR:
17140 tree op0 = RECUR (TREE_OPERAND (t, 0));
17141 tree op1 = RECUR (TREE_OPERAND (t, 1));
17142 RETURN (delete_sanity (op0, op1,
17143 DELETE_EXPR_USE_VEC (t),
17144 DELETE_EXPR_USE_GLOBAL (t),
17145 complain));
17148 case COMPOUND_EXPR:
17150 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
17151 complain & ~tf_decltype, in_decl,
17152 /*function_p=*/false,
17153 integral_constant_expression_p);
17154 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
17155 op0,
17156 RECUR (TREE_OPERAND (t, 1)),
17157 complain|decltype_flag));
17160 case CALL_EXPR:
17162 tree function;
17163 vec<tree, va_gc> *call_args;
17164 unsigned int nargs, i;
17165 bool qualified_p;
17166 bool koenig_p;
17167 tree ret;
17169 function = CALL_EXPR_FN (t);
17170 /* Internal function with no arguments. */
17171 if (function == NULL_TREE && call_expr_nargs (t) == 0)
17172 RETURN (t);
17174 /* When we parsed the expression, we determined whether or
17175 not Koenig lookup should be performed. */
17176 koenig_p = KOENIG_LOOKUP_P (t);
17177 if (function == NULL_TREE)
17179 koenig_p = false;
17180 qualified_p = false;
17182 else if (TREE_CODE (function) == SCOPE_REF)
17184 qualified_p = true;
17185 function = tsubst_qualified_id (function, args, complain, in_decl,
17186 /*done=*/false,
17187 /*address_p=*/false);
17189 else if (koenig_p && identifier_p (function))
17191 /* Do nothing; calling tsubst_copy_and_build on an identifier
17192 would incorrectly perform unqualified lookup again.
17194 Note that we can also have an IDENTIFIER_NODE if the earlier
17195 unqualified lookup found a member function; in that case
17196 koenig_p will be false and we do want to do the lookup
17197 again to find the instantiated member function.
17199 FIXME but doing that causes c++/15272, so we need to stop
17200 using IDENTIFIER_NODE in that situation. */
17201 qualified_p = false;
17203 else
17205 if (TREE_CODE (function) == COMPONENT_REF)
17207 tree op = TREE_OPERAND (function, 1);
17209 qualified_p = (TREE_CODE (op) == SCOPE_REF
17210 || (BASELINK_P (op)
17211 && BASELINK_QUALIFIED_P (op)));
17213 else
17214 qualified_p = false;
17216 if (TREE_CODE (function) == ADDR_EXPR
17217 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
17218 /* Avoid error about taking the address of a constructor. */
17219 function = TREE_OPERAND (function, 0);
17221 function = tsubst_copy_and_build (function, args, complain,
17222 in_decl,
17223 !qualified_p,
17224 integral_constant_expression_p);
17226 if (BASELINK_P (function))
17227 qualified_p = true;
17230 nargs = call_expr_nargs (t);
17231 call_args = make_tree_vector ();
17232 for (i = 0; i < nargs; ++i)
17234 tree arg = CALL_EXPR_ARG (t, i);
17236 if (!PACK_EXPANSION_P (arg))
17237 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
17238 else
17240 /* Expand the pack expansion and push each entry onto
17241 CALL_ARGS. */
17242 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
17243 if (TREE_CODE (arg) == TREE_VEC)
17245 unsigned int len, j;
17247 len = TREE_VEC_LENGTH (arg);
17248 for (j = 0; j < len; ++j)
17250 tree value = TREE_VEC_ELT (arg, j);
17251 if (value != NULL_TREE)
17252 value = convert_from_reference (value);
17253 vec_safe_push (call_args, value);
17256 else
17258 /* A partial substitution. Add one entry. */
17259 vec_safe_push (call_args, arg);
17264 /* We do not perform argument-dependent lookup if normal
17265 lookup finds a non-function, in accordance with the
17266 expected resolution of DR 218. */
17267 if (koenig_p
17268 && ((is_overloaded_fn (function)
17269 /* If lookup found a member function, the Koenig lookup is
17270 not appropriate, even if an unqualified-name was used
17271 to denote the function. */
17272 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
17273 || identifier_p (function))
17274 /* Only do this when substitution turns a dependent call
17275 into a non-dependent call. */
17276 && type_dependent_expression_p_push (t)
17277 && !any_type_dependent_arguments_p (call_args))
17278 function = perform_koenig_lookup (function, call_args, tf_none);
17280 if (function != NULL_TREE
17281 && identifier_p (function)
17282 && !any_type_dependent_arguments_p (call_args))
17284 if (koenig_p && (complain & tf_warning_or_error))
17286 /* For backwards compatibility and good diagnostics, try
17287 the unqualified lookup again if we aren't in SFINAE
17288 context. */
17289 tree unq = (tsubst_copy_and_build
17290 (function, args, complain, in_decl, true,
17291 integral_constant_expression_p));
17292 if (unq == error_mark_node)
17294 release_tree_vector (call_args);
17295 RETURN (error_mark_node);
17298 if (unq != function)
17300 /* In a lambda fn, we have to be careful to not
17301 introduce new this captures. Legacy code can't
17302 be using lambdas anyway, so it's ok to be
17303 stricter. */
17304 bool in_lambda = (current_class_type
17305 && LAMBDA_TYPE_P (current_class_type));
17306 char const *const msg
17307 = G_("%qD was not declared in this scope, "
17308 "and no declarations were found by "
17309 "argument-dependent lookup at the point "
17310 "of instantiation");
17312 bool diag = true;
17313 if (in_lambda)
17314 error_at (EXPR_LOC_OR_LOC (t, input_location),
17315 msg, function);
17316 else
17317 diag = permerror (EXPR_LOC_OR_LOC (t, input_location),
17318 msg, function);
17319 if (diag)
17321 tree fn = unq;
17323 if (INDIRECT_REF_P (fn))
17324 fn = TREE_OPERAND (fn, 0);
17325 if (is_overloaded_fn (fn))
17326 fn = get_first_fn (fn);
17328 if (!DECL_P (fn))
17329 /* Can't say anything more. */;
17330 else if (DECL_CLASS_SCOPE_P (fn))
17332 location_t loc = EXPR_LOC_OR_LOC (t,
17333 input_location);
17334 inform (loc,
17335 "declarations in dependent base %qT are "
17336 "not found by unqualified lookup",
17337 DECL_CLASS_CONTEXT (fn));
17338 if (current_class_ptr)
17339 inform (loc,
17340 "use %<this->%D%> instead", function);
17341 else
17342 inform (loc,
17343 "use %<%T::%D%> instead",
17344 current_class_name, function);
17346 else
17347 inform (DECL_SOURCE_LOCATION (fn),
17348 "%qD declared here, later in the "
17349 "translation unit", fn);
17350 if (in_lambda)
17352 release_tree_vector (call_args);
17353 RETURN (error_mark_node);
17357 function = unq;
17360 if (identifier_p (function))
17362 if (complain & tf_error)
17363 unqualified_name_lookup_error (function);
17364 release_tree_vector (call_args);
17365 RETURN (error_mark_node);
17369 /* Remember that there was a reference to this entity. */
17370 if (function != NULL_TREE
17371 && DECL_P (function)
17372 && !mark_used (function, complain) && !(complain & tf_error))
17374 release_tree_vector (call_args);
17375 RETURN (error_mark_node);
17378 /* Put back tf_decltype for the actual call. */
17379 complain |= decltype_flag;
17381 if (function == NULL_TREE)
17382 switch (CALL_EXPR_IFN (t))
17384 case IFN_LAUNDER:
17385 gcc_assert (nargs == 1);
17386 if (vec_safe_length (call_args) != 1)
17388 error_at (EXPR_LOC_OR_LOC (t, input_location),
17389 "wrong number of arguments to "
17390 "%<__builtin_launder%>");
17391 ret = error_mark_node;
17393 else
17394 ret = finish_builtin_launder (EXPR_LOC_OR_LOC (t,
17395 input_location),
17396 (*call_args)[0], complain);
17397 break;
17399 default:
17400 /* Unsupported internal function with arguments. */
17401 gcc_unreachable ();
17403 else if (TREE_CODE (function) == OFFSET_REF)
17404 ret = build_offset_ref_call_from_tree (function, &call_args,
17405 complain);
17406 else if (TREE_CODE (function) == COMPONENT_REF)
17408 tree instance = TREE_OPERAND (function, 0);
17409 tree fn = TREE_OPERAND (function, 1);
17411 if (processing_template_decl
17412 && (type_dependent_expression_p (instance)
17413 || (!BASELINK_P (fn)
17414 && TREE_CODE (fn) != FIELD_DECL)
17415 || type_dependent_expression_p (fn)
17416 || any_type_dependent_arguments_p (call_args)))
17417 ret = build_min_nt_call_vec (function, call_args);
17418 else if (!BASELINK_P (fn))
17419 ret = finish_call_expr (function, &call_args,
17420 /*disallow_virtual=*/false,
17421 /*koenig_p=*/false,
17422 complain);
17423 else
17424 ret = (build_new_method_call
17425 (instance, fn,
17426 &call_args, NULL_TREE,
17427 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
17428 /*fn_p=*/NULL,
17429 complain));
17431 else
17432 ret = finish_call_expr (function, &call_args,
17433 /*disallow_virtual=*/qualified_p,
17434 koenig_p,
17435 complain);
17437 release_tree_vector (call_args);
17439 if (ret != error_mark_node)
17441 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
17442 bool ord = CALL_EXPR_ORDERED_ARGS (t);
17443 bool rev = CALL_EXPR_REVERSE_ARGS (t);
17444 bool thk = CALL_FROM_THUNK_P (t);
17445 if (op || ord || rev || thk)
17447 function = extract_call_expr (ret);
17448 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
17449 CALL_EXPR_ORDERED_ARGS (function) = ord;
17450 CALL_EXPR_REVERSE_ARGS (function) = rev;
17451 if (thk)
17453 CALL_FROM_THUNK_P (function) = true;
17454 /* The thunk location is not interesting. */
17455 SET_EXPR_LOCATION (function, UNKNOWN_LOCATION);
17460 RETURN (ret);
17463 case COND_EXPR:
17465 tree cond = RECUR (TREE_OPERAND (t, 0));
17466 tree folded_cond = fold_non_dependent_expr (cond);
17467 tree exp1, exp2;
17469 if (TREE_CODE (folded_cond) == INTEGER_CST)
17471 if (integer_zerop (folded_cond))
17473 ++c_inhibit_evaluation_warnings;
17474 exp1 = RECUR (TREE_OPERAND (t, 1));
17475 --c_inhibit_evaluation_warnings;
17476 exp2 = RECUR (TREE_OPERAND (t, 2));
17478 else
17480 exp1 = RECUR (TREE_OPERAND (t, 1));
17481 ++c_inhibit_evaluation_warnings;
17482 exp2 = RECUR (TREE_OPERAND (t, 2));
17483 --c_inhibit_evaluation_warnings;
17485 cond = folded_cond;
17487 else
17489 exp1 = RECUR (TREE_OPERAND (t, 1));
17490 exp2 = RECUR (TREE_OPERAND (t, 2));
17493 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
17494 cond, exp1, exp2, complain));
17497 case PSEUDO_DTOR_EXPR:
17499 tree op0 = RECUR (TREE_OPERAND (t, 0));
17500 tree op1 = RECUR (TREE_OPERAND (t, 1));
17501 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
17502 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
17503 input_location));
17506 case TREE_LIST:
17508 tree purpose, value, chain;
17510 if (t == void_list_node)
17511 RETURN (t);
17513 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
17514 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
17516 /* We have pack expansions, so expand those and
17517 create a new list out of it. */
17518 tree purposevec = NULL_TREE;
17519 tree valuevec = NULL_TREE;
17520 tree chain;
17521 int i, len = -1;
17523 /* Expand the argument expressions. */
17524 if (TREE_PURPOSE (t))
17525 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
17526 complain, in_decl);
17527 if (TREE_VALUE (t))
17528 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
17529 complain, in_decl);
17531 /* Build the rest of the list. */
17532 chain = TREE_CHAIN (t);
17533 if (chain && chain != void_type_node)
17534 chain = RECUR (chain);
17536 /* Determine the number of arguments. */
17537 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
17539 len = TREE_VEC_LENGTH (purposevec);
17540 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
17542 else if (TREE_CODE (valuevec) == TREE_VEC)
17543 len = TREE_VEC_LENGTH (valuevec);
17544 else
17546 /* Since we only performed a partial substitution into
17547 the argument pack, we only RETURN (a single list
17548 node. */
17549 if (purposevec == TREE_PURPOSE (t)
17550 && valuevec == TREE_VALUE (t)
17551 && chain == TREE_CHAIN (t))
17552 RETURN (t);
17554 RETURN (tree_cons (purposevec, valuevec, chain));
17557 /* Convert the argument vectors into a TREE_LIST */
17558 i = len;
17559 while (i > 0)
17561 /* Grab the Ith values. */
17562 i--;
17563 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
17564 : NULL_TREE;
17565 value
17566 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
17567 : NULL_TREE;
17569 /* Build the list (backwards). */
17570 chain = tree_cons (purpose, value, chain);
17573 RETURN (chain);
17576 purpose = TREE_PURPOSE (t);
17577 if (purpose)
17578 purpose = RECUR (purpose);
17579 value = TREE_VALUE (t);
17580 if (value)
17581 value = RECUR (value);
17582 chain = TREE_CHAIN (t);
17583 if (chain && chain != void_type_node)
17584 chain = RECUR (chain);
17585 if (purpose == TREE_PURPOSE (t)
17586 && value == TREE_VALUE (t)
17587 && chain == TREE_CHAIN (t))
17588 RETURN (t);
17589 RETURN (tree_cons (purpose, value, chain));
17592 case COMPONENT_REF:
17594 tree object;
17595 tree object_type;
17596 tree member;
17597 tree r;
17599 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17600 args, complain, in_decl);
17601 /* Remember that there was a reference to this entity. */
17602 if (DECL_P (object)
17603 && !mark_used (object, complain) && !(complain & tf_error))
17604 RETURN (error_mark_node);
17605 object_type = TREE_TYPE (object);
17607 member = TREE_OPERAND (t, 1);
17608 if (BASELINK_P (member))
17609 member = tsubst_baselink (member,
17610 non_reference (TREE_TYPE (object)),
17611 args, complain, in_decl);
17612 else
17613 member = tsubst_copy (member, args, complain, in_decl);
17614 if (member == error_mark_node)
17615 RETURN (error_mark_node);
17617 if (TREE_CODE (member) == FIELD_DECL)
17619 r = finish_non_static_data_member (member, object, NULL_TREE);
17620 if (TREE_CODE (r) == COMPONENT_REF)
17621 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17622 RETURN (r);
17624 else if (type_dependent_expression_p (object))
17625 /* We can't do much here. */;
17626 else if (!CLASS_TYPE_P (object_type))
17628 if (scalarish_type_p (object_type))
17630 tree s = NULL_TREE;
17631 tree dtor = member;
17633 if (TREE_CODE (dtor) == SCOPE_REF)
17635 s = TREE_OPERAND (dtor, 0);
17636 dtor = TREE_OPERAND (dtor, 1);
17638 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
17640 dtor = TREE_OPERAND (dtor, 0);
17641 if (TYPE_P (dtor))
17642 RETURN (finish_pseudo_destructor_expr
17643 (object, s, dtor, input_location));
17647 else if (TREE_CODE (member) == SCOPE_REF
17648 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
17650 /* Lookup the template functions now that we know what the
17651 scope is. */
17652 tree scope = TREE_OPERAND (member, 0);
17653 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
17654 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
17655 member = lookup_qualified_name (scope, tmpl,
17656 /*is_type_p=*/false,
17657 /*complain=*/false);
17658 if (BASELINK_P (member))
17660 BASELINK_FUNCTIONS (member)
17661 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
17662 args);
17663 member = (adjust_result_of_qualified_name_lookup
17664 (member, BINFO_TYPE (BASELINK_BINFO (member)),
17665 object_type));
17667 else
17669 qualified_name_lookup_error (scope, tmpl, member,
17670 input_location);
17671 RETURN (error_mark_node);
17674 else if (TREE_CODE (member) == SCOPE_REF
17675 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
17676 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
17678 if (complain & tf_error)
17680 if (TYPE_P (TREE_OPERAND (member, 0)))
17681 error ("%qT is not a class or namespace",
17682 TREE_OPERAND (member, 0));
17683 else
17684 error ("%qD is not a class or namespace",
17685 TREE_OPERAND (member, 0));
17687 RETURN (error_mark_node);
17690 r = finish_class_member_access_expr (object, member,
17691 /*template_p=*/false,
17692 complain);
17693 if (TREE_CODE (r) == COMPONENT_REF)
17694 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
17695 RETURN (r);
17698 case THROW_EXPR:
17699 RETURN (build_throw
17700 (RECUR (TREE_OPERAND (t, 0))));
17702 case CONSTRUCTOR:
17704 vec<constructor_elt, va_gc> *n;
17705 constructor_elt *ce;
17706 unsigned HOST_WIDE_INT idx;
17707 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17708 bool process_index_p;
17709 int newlen;
17710 bool need_copy_p = false;
17711 tree r;
17713 if (type == error_mark_node)
17714 RETURN (error_mark_node);
17716 /* digest_init will do the wrong thing if we let it. */
17717 if (type && TYPE_PTRMEMFUNC_P (type))
17718 RETURN (t);
17720 /* We do not want to process the index of aggregate
17721 initializers as they are identifier nodes which will be
17722 looked up by digest_init. */
17723 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
17725 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
17726 newlen = vec_safe_length (n);
17727 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
17729 if (ce->index && process_index_p
17730 /* An identifier index is looked up in the type
17731 being initialized, not the current scope. */
17732 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
17733 ce->index = RECUR (ce->index);
17735 if (PACK_EXPANSION_P (ce->value))
17737 /* Substitute into the pack expansion. */
17738 ce->value = tsubst_pack_expansion (ce->value, args, complain,
17739 in_decl);
17741 if (ce->value == error_mark_node
17742 || PACK_EXPANSION_P (ce->value))
17744 else if (TREE_VEC_LENGTH (ce->value) == 1)
17745 /* Just move the argument into place. */
17746 ce->value = TREE_VEC_ELT (ce->value, 0);
17747 else
17749 /* Update the length of the final CONSTRUCTOR
17750 arguments vector, and note that we will need to
17751 copy.*/
17752 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
17753 need_copy_p = true;
17756 else
17757 ce->value = RECUR (ce->value);
17760 if (need_copy_p)
17762 vec<constructor_elt, va_gc> *old_n = n;
17764 vec_alloc (n, newlen);
17765 FOR_EACH_VEC_ELT (*old_n, idx, ce)
17767 if (TREE_CODE (ce->value) == TREE_VEC)
17769 int i, len = TREE_VEC_LENGTH (ce->value);
17770 for (i = 0; i < len; ++i)
17771 CONSTRUCTOR_APPEND_ELT (n, 0,
17772 TREE_VEC_ELT (ce->value, i));
17774 else
17775 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
17779 r = build_constructor (init_list_type_node, n);
17780 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
17782 if (TREE_HAS_CONSTRUCTOR (t))
17784 fcl_t cl = fcl_functional;
17785 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
17786 cl = fcl_c99;
17787 RETURN (finish_compound_literal (type, r, complain, cl));
17790 TREE_TYPE (r) = type;
17791 RETURN (r);
17794 case TYPEID_EXPR:
17796 tree operand_0 = TREE_OPERAND (t, 0);
17797 if (TYPE_P (operand_0))
17799 operand_0 = tsubst (operand_0, args, complain, in_decl);
17800 RETURN (get_typeid (operand_0, complain));
17802 else
17804 operand_0 = RECUR (operand_0);
17805 RETURN (build_typeid (operand_0, complain));
17809 case VAR_DECL:
17810 if (!args)
17811 RETURN (t);
17812 else if (DECL_PACK_P (t))
17814 /* We don't build decls for an instantiation of a
17815 variadic capture proxy, we instantiate the elements
17816 when needed. */
17817 gcc_assert (DECL_HAS_VALUE_EXPR_P (t));
17818 return RECUR (DECL_VALUE_EXPR (t));
17820 /* Fall through */
17822 case PARM_DECL:
17824 tree r = tsubst_copy (t, args, complain, in_decl);
17825 /* ??? We're doing a subset of finish_id_expression here. */
17826 if (VAR_P (r)
17827 && !processing_template_decl
17828 && !cp_unevaluated_operand
17829 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
17830 && CP_DECL_THREAD_LOCAL_P (r))
17832 if (tree wrap = get_tls_wrapper_fn (r))
17833 /* Replace an evaluated use of the thread_local variable with
17834 a call to its wrapper. */
17835 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
17837 else if (outer_automatic_var_p (r))
17839 r = process_outer_var_ref (r, complain);
17840 if (is_capture_proxy (r))
17841 register_local_specialization (r, t);
17844 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
17845 /* If the original type was a reference, we'll be wrapped in
17846 the appropriate INDIRECT_REF. */
17847 r = convert_from_reference (r);
17848 RETURN (r);
17851 case VA_ARG_EXPR:
17853 tree op0 = RECUR (TREE_OPERAND (t, 0));
17854 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17855 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
17858 case OFFSETOF_EXPR:
17860 tree object_ptr
17861 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
17862 in_decl, /*function_p=*/false,
17863 /*integral_constant_expression_p=*/false);
17864 RETURN (finish_offsetof (object_ptr,
17865 RECUR (TREE_OPERAND (t, 0)),
17866 EXPR_LOCATION (t)));
17869 case ADDRESSOF_EXPR:
17870 RETURN (cp_build_addressof (EXPR_LOCATION (t),
17871 RECUR (TREE_OPERAND (t, 0)), complain));
17873 case TRAIT_EXPR:
17875 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
17876 complain, in_decl);
17878 tree type2 = TRAIT_EXPR_TYPE2 (t);
17879 if (type2 && TREE_CODE (type2) == TREE_LIST)
17880 type2 = RECUR (type2);
17881 else if (type2)
17882 type2 = tsubst (type2, args, complain, in_decl);
17884 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
17887 case STMT_EXPR:
17889 tree old_stmt_expr = cur_stmt_expr;
17890 tree stmt_expr = begin_stmt_expr ();
17892 cur_stmt_expr = stmt_expr;
17893 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
17894 integral_constant_expression_p);
17895 stmt_expr = finish_stmt_expr (stmt_expr, false);
17896 cur_stmt_expr = old_stmt_expr;
17898 /* If the resulting list of expression statement is empty,
17899 fold it further into void_node. */
17900 if (empty_expr_stmt_p (stmt_expr))
17901 stmt_expr = void_node;
17903 RETURN (stmt_expr);
17906 case LAMBDA_EXPR:
17908 tree r = build_lambda_expr ();
17910 tree type = tsubst (LAMBDA_EXPR_CLOSURE (t), args, complain, NULL_TREE);
17911 LAMBDA_EXPR_CLOSURE (r) = type;
17912 CLASSTYPE_LAMBDA_EXPR (type) = r;
17914 LAMBDA_EXPR_LOCATION (r)
17915 = LAMBDA_EXPR_LOCATION (t);
17916 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
17917 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
17918 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
17919 LAMBDA_EXPR_DISCRIMINATOR (r)
17920 = (LAMBDA_EXPR_DISCRIMINATOR (t));
17921 tree scope = LAMBDA_EXPR_EXTRA_SCOPE (t);
17922 if (!scope)
17923 /* No substitution needed. */;
17924 else if (VAR_OR_FUNCTION_DECL_P (scope))
17925 /* For a function or variable scope, we want to use tsubst so that we
17926 don't complain about referring to an auto before deduction. */
17927 scope = tsubst (scope, args, complain, in_decl);
17928 else if (TREE_CODE (scope) == PARM_DECL)
17930 /* Look up the parameter we want directly, as tsubst_copy
17931 doesn't do what we need. */
17932 tree fn = tsubst (DECL_CONTEXT (scope), args, complain, in_decl);
17933 tree parm = FUNCTION_FIRST_USER_PARM (fn);
17934 while (DECL_PARM_INDEX (parm) != DECL_PARM_INDEX (scope))
17935 parm = DECL_CHAIN (parm);
17936 scope = parm;
17937 /* FIXME Work around the parm not having DECL_CONTEXT set. */
17938 if (DECL_CONTEXT (scope) == NULL_TREE)
17939 DECL_CONTEXT (scope) = fn;
17941 else if (TREE_CODE (scope) == FIELD_DECL)
17942 /* For a field, use tsubst_copy so that we look up the existing field
17943 rather than build a new one. */
17944 scope = RECUR (scope);
17945 else
17946 gcc_unreachable ();
17947 LAMBDA_EXPR_EXTRA_SCOPE (r) = scope;
17949 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
17950 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
17952 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
17953 determine_visibility (TYPE_NAME (type));
17954 /* Now that we know visibility, instantiate the type so we have a
17955 declaration of the op() for later calls to lambda_function. */
17956 complete_type (type);
17958 if (tree fn = lambda_function (type))
17959 LAMBDA_EXPR_RETURN_TYPE (r) = TREE_TYPE (TREE_TYPE (fn));
17961 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
17963 insert_pending_capture_proxies ();
17965 RETURN (build_lambda_object (r));
17968 case TARGET_EXPR:
17969 /* We can get here for a constant initializer of non-dependent type.
17970 FIXME stop folding in cp_parser_initializer_clause. */
17972 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
17973 complain);
17974 RETURN (r);
17977 case TRANSACTION_EXPR:
17978 RETURN (tsubst_expr(t, args, complain, in_decl,
17979 integral_constant_expression_p));
17981 case PAREN_EXPR:
17982 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
17984 case VEC_PERM_EXPR:
17986 tree op0 = RECUR (TREE_OPERAND (t, 0));
17987 tree op1 = RECUR (TREE_OPERAND (t, 1));
17988 tree op2 = RECUR (TREE_OPERAND (t, 2));
17989 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
17990 complain));
17993 case REQUIRES_EXPR:
17994 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
17996 default:
17997 /* Handle Objective-C++ constructs, if appropriate. */
17999 tree subst
18000 = objcp_tsubst_copy_and_build (t, args, complain,
18001 in_decl, /*function_p=*/false);
18002 if (subst)
18003 RETURN (subst);
18005 RETURN (tsubst_copy (t, args, complain, in_decl));
18008 #undef RECUR
18009 #undef RETURN
18010 out:
18011 input_location = loc;
18012 return retval;
18015 /* Verify that the instantiated ARGS are valid. For type arguments,
18016 make sure that the type's linkage is ok. For non-type arguments,
18017 make sure they are constants if they are integral or enumerations.
18018 Emit an error under control of COMPLAIN, and return TRUE on error. */
18020 static bool
18021 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
18023 if (dependent_template_arg_p (t))
18024 return false;
18025 if (ARGUMENT_PACK_P (t))
18027 tree vec = ARGUMENT_PACK_ARGS (t);
18028 int len = TREE_VEC_LENGTH (vec);
18029 bool result = false;
18030 int i;
18032 for (i = 0; i < len; ++i)
18033 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
18034 result = true;
18035 return result;
18037 else if (TYPE_P (t))
18039 /* [basic.link]: A name with no linkage (notably, the name
18040 of a class or enumeration declared in a local scope)
18041 shall not be used to declare an entity with linkage.
18042 This implies that names with no linkage cannot be used as
18043 template arguments
18045 DR 757 relaxes this restriction for C++0x. */
18046 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
18047 : no_linkage_check (t, /*relaxed_p=*/false));
18049 if (nt)
18051 /* DR 488 makes use of a type with no linkage cause
18052 type deduction to fail. */
18053 if (complain & tf_error)
18055 if (TYPE_UNNAMED_P (nt))
18056 error ("%qT is/uses unnamed type", t);
18057 else
18058 error ("template argument for %qD uses local type %qT",
18059 tmpl, t);
18061 return true;
18063 /* In order to avoid all sorts of complications, we do not
18064 allow variably-modified types as template arguments. */
18065 else if (variably_modified_type_p (t, NULL_TREE))
18067 if (complain & tf_error)
18068 error ("%qT is a variably modified type", t);
18069 return true;
18072 /* Class template and alias template arguments should be OK. */
18073 else if (DECL_TYPE_TEMPLATE_P (t))
18075 /* A non-type argument of integral or enumerated type must be a
18076 constant. */
18077 else if (TREE_TYPE (t)
18078 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
18079 && !REFERENCE_REF_P (t)
18080 && !TREE_CONSTANT (t))
18082 if (complain & tf_error)
18083 error ("integral expression %qE is not constant", t);
18084 return true;
18086 return false;
18089 static bool
18090 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
18092 int ix, len = DECL_NTPARMS (tmpl);
18093 bool result = false;
18095 for (ix = 0; ix != len; ix++)
18097 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
18098 result = true;
18100 if (result && (complain & tf_error))
18101 error (" trying to instantiate %qD", tmpl);
18102 return result;
18105 /* We're out of SFINAE context now, so generate diagnostics for the access
18106 errors we saw earlier when instantiating D from TMPL and ARGS. */
18108 static void
18109 recheck_decl_substitution (tree d, tree tmpl, tree args)
18111 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
18112 tree type = TREE_TYPE (pattern);
18113 location_t loc = input_location;
18115 push_access_scope (d);
18116 push_deferring_access_checks (dk_no_deferred);
18117 input_location = DECL_SOURCE_LOCATION (pattern);
18118 tsubst (type, args, tf_warning_or_error, d);
18119 input_location = loc;
18120 pop_deferring_access_checks ();
18121 pop_access_scope (d);
18124 /* Instantiate the indicated variable, function, or alias template TMPL with
18125 the template arguments in TARG_PTR. */
18127 static tree
18128 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
18130 tree targ_ptr = orig_args;
18131 tree fndecl;
18132 tree gen_tmpl;
18133 tree spec;
18134 bool access_ok = true;
18136 if (tmpl == error_mark_node)
18137 return error_mark_node;
18139 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
18141 /* If this function is a clone, handle it specially. */
18142 if (DECL_CLONED_FUNCTION_P (tmpl))
18144 tree spec;
18145 tree clone;
18147 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
18148 DECL_CLONED_FUNCTION. */
18149 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
18150 targ_ptr, complain);
18151 if (spec == error_mark_node)
18152 return error_mark_node;
18154 /* Look for the clone. */
18155 FOR_EACH_CLONE (clone, spec)
18156 if (DECL_NAME (clone) == DECL_NAME (tmpl))
18157 return clone;
18158 /* We should always have found the clone by now. */
18159 gcc_unreachable ();
18160 return NULL_TREE;
18163 if (targ_ptr == error_mark_node)
18164 return error_mark_node;
18166 /* Check to see if we already have this specialization. */
18167 gen_tmpl = most_general_template (tmpl);
18168 if (TMPL_ARGS_DEPTH (targ_ptr)
18169 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
18170 /* targ_ptr only has the innermost template args, so add the outer ones
18171 from tmpl, which could be either a partial instantiation or gen_tmpl (in
18172 the case of a non-dependent call within a template definition). */
18173 targ_ptr = (add_outermost_template_args
18174 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
18175 targ_ptr));
18177 /* It would be nice to avoid hashing here and then again in tsubst_decl,
18178 but it doesn't seem to be on the hot path. */
18179 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
18181 gcc_assert (tmpl == gen_tmpl
18182 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
18183 == spec)
18184 || fndecl == NULL_TREE);
18186 if (spec != NULL_TREE)
18188 if (FNDECL_HAS_ACCESS_ERRORS (spec))
18190 if (complain & tf_error)
18191 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
18192 return error_mark_node;
18194 return spec;
18197 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
18198 complain))
18199 return error_mark_node;
18201 /* We are building a FUNCTION_DECL, during which the access of its
18202 parameters and return types have to be checked. However this
18203 FUNCTION_DECL which is the desired context for access checking
18204 is not built yet. We solve this chicken-and-egg problem by
18205 deferring all checks until we have the FUNCTION_DECL. */
18206 push_deferring_access_checks (dk_deferred);
18208 /* Instantiation of the function happens in the context of the function
18209 template, not the context of the overload resolution we're doing. */
18210 push_to_top_level ();
18211 /* If there are dependent arguments, e.g. because we're doing partial
18212 ordering, make sure processing_template_decl stays set. */
18213 if (uses_template_parms (targ_ptr))
18214 ++processing_template_decl;
18215 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18217 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
18218 complain, gen_tmpl, true);
18219 push_nested_class (ctx);
18222 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
18224 fndecl = NULL_TREE;
18225 if (VAR_P (pattern))
18227 /* We need to determine if we're using a partial or explicit
18228 specialization now, because the type of the variable could be
18229 different. */
18230 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
18231 tree elt = most_specialized_partial_spec (tid, complain);
18232 if (elt == error_mark_node)
18233 pattern = error_mark_node;
18234 else if (elt)
18236 tree partial_tmpl = TREE_VALUE (elt);
18237 tree partial_args = TREE_PURPOSE (elt);
18238 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
18239 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
18243 /* Substitute template parameters to obtain the specialization. */
18244 if (fndecl == NULL_TREE)
18245 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
18246 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18247 pop_nested_class ();
18248 pop_from_top_level ();
18250 if (fndecl == error_mark_node)
18252 pop_deferring_access_checks ();
18253 return error_mark_node;
18256 /* The DECL_TI_TEMPLATE should always be the immediate parent
18257 template, not the most general template. */
18258 DECL_TI_TEMPLATE (fndecl) = tmpl;
18259 DECL_TI_ARGS (fndecl) = targ_ptr;
18261 /* Now we know the specialization, compute access previously
18262 deferred. Do no access control for inheriting constructors,
18263 as we already checked access for the inherited constructor. */
18264 if (!(flag_new_inheriting_ctors
18265 && DECL_INHERITED_CTOR (fndecl)))
18267 push_access_scope (fndecl);
18268 if (!perform_deferred_access_checks (complain))
18269 access_ok = false;
18270 pop_access_scope (fndecl);
18272 pop_deferring_access_checks ();
18274 /* If we've just instantiated the main entry point for a function,
18275 instantiate all the alternate entry points as well. We do this
18276 by cloning the instantiation of the main entry point, not by
18277 instantiating the template clones. */
18278 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
18279 clone_function_decl (fndecl, /*update_methods=*/false);
18281 if (!access_ok)
18283 if (!(complain & tf_error))
18285 /* Remember to reinstantiate when we're out of SFINAE so the user
18286 can see the errors. */
18287 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
18289 return error_mark_node;
18291 return fndecl;
18294 /* Wrapper for instantiate_template_1. */
18296 tree
18297 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
18299 tree ret;
18300 timevar_push (TV_TEMPLATE_INST);
18301 ret = instantiate_template_1 (tmpl, orig_args, complain);
18302 timevar_pop (TV_TEMPLATE_INST);
18303 return ret;
18306 /* Instantiate the alias template TMPL with ARGS. Also push a template
18307 instantiation level, which instantiate_template doesn't do because
18308 functions and variables have sufficient context established by the
18309 callers. */
18311 static tree
18312 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
18314 struct pending_template *old_last_pend = last_pending_template;
18315 struct tinst_level *old_error_tinst = last_error_tinst_level;
18316 if (tmpl == error_mark_node || args == error_mark_node)
18317 return error_mark_node;
18318 tree tinst = build_tree_list (tmpl, args);
18319 if (!push_tinst_level (tinst))
18321 ggc_free (tinst);
18322 return error_mark_node;
18325 args =
18326 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
18327 args, tmpl, complain,
18328 /*require_all_args=*/true,
18329 /*use_default_args=*/true);
18331 tree r = instantiate_template (tmpl, args, complain);
18332 pop_tinst_level ();
18333 /* We can't free this if a pending_template entry or last_error_tinst_level
18334 is pointing at it. */
18335 if (last_pending_template == old_last_pend
18336 && last_error_tinst_level == old_error_tinst)
18337 ggc_free (tinst);
18339 return r;
18342 /* PARM is a template parameter pack for FN. Returns true iff
18343 PARM is used in a deducible way in the argument list of FN. */
18345 static bool
18346 pack_deducible_p (tree parm, tree fn)
18348 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
18349 for (; t; t = TREE_CHAIN (t))
18351 tree type = TREE_VALUE (t);
18352 tree packs;
18353 if (!PACK_EXPANSION_P (type))
18354 continue;
18355 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
18356 packs; packs = TREE_CHAIN (packs))
18357 if (template_args_equal (TREE_VALUE (packs), parm))
18359 /* The template parameter pack is used in a function parameter
18360 pack. If this is the end of the parameter list, the
18361 template parameter pack is deducible. */
18362 if (TREE_CHAIN (t) == void_list_node)
18363 return true;
18364 else
18365 /* Otherwise, not. Well, it could be deduced from
18366 a non-pack parameter, but doing so would end up with
18367 a deduction mismatch, so don't bother. */
18368 return false;
18371 /* The template parameter pack isn't used in any function parameter
18372 packs, but it might be used deeper, e.g. tuple<Args...>. */
18373 return true;
18376 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
18377 NARGS elements of the arguments that are being used when calling
18378 it. TARGS is a vector into which the deduced template arguments
18379 are placed.
18381 Returns either a FUNCTION_DECL for the matching specialization of FN or
18382 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
18383 true, diagnostics will be printed to explain why it failed.
18385 If FN is a conversion operator, or we are trying to produce a specific
18386 specialization, RETURN_TYPE is the return type desired.
18388 The EXPLICIT_TARGS are explicit template arguments provided via a
18389 template-id.
18391 The parameter STRICT is one of:
18393 DEDUCE_CALL:
18394 We are deducing arguments for a function call, as in
18395 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
18396 deducing arguments for a call to the result of a conversion
18397 function template, as in [over.call.object].
18399 DEDUCE_CONV:
18400 We are deducing arguments for a conversion function, as in
18401 [temp.deduct.conv].
18403 DEDUCE_EXACT:
18404 We are deducing arguments when doing an explicit instantiation
18405 as in [temp.explicit], when determining an explicit specialization
18406 as in [temp.expl.spec], or when taking the address of a function
18407 template, as in [temp.deduct.funcaddr]. */
18409 tree
18410 fn_type_unification (tree fn,
18411 tree explicit_targs,
18412 tree targs,
18413 const tree *args,
18414 unsigned int nargs,
18415 tree return_type,
18416 unification_kind_t strict,
18417 int flags,
18418 bool explain_p,
18419 bool decltype_p)
18421 tree parms;
18422 tree fntype;
18423 tree decl = NULL_TREE;
18424 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
18425 bool ok;
18426 static int deduction_depth;
18427 struct pending_template *old_last_pend = last_pending_template;
18428 struct tinst_level *old_error_tinst = last_error_tinst_level;
18430 tree orig_fn = fn;
18431 if (flag_new_inheriting_ctors)
18432 fn = strip_inheriting_ctors (fn);
18434 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
18435 tree tinst;
18436 tree r = error_mark_node;
18438 tree full_targs = targs;
18439 if (TMPL_ARGS_DEPTH (targs)
18440 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
18441 full_targs = (add_outermost_template_args
18442 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
18443 targs));
18445 if (decltype_p)
18446 complain |= tf_decltype;
18448 /* In C++0x, it's possible to have a function template whose type depends
18449 on itself recursively. This is most obvious with decltype, but can also
18450 occur with enumeration scope (c++/48969). So we need to catch infinite
18451 recursion and reject the substitution at deduction time; this function
18452 will return error_mark_node for any repeated substitution.
18454 This also catches excessive recursion such as when f<N> depends on
18455 f<N-1> across all integers, and returns error_mark_node for all the
18456 substitutions back up to the initial one.
18458 This is, of course, not reentrant. */
18459 if (excessive_deduction_depth)
18460 return error_mark_node;
18461 tinst = build_tree_list (fn, NULL_TREE);
18462 ++deduction_depth;
18464 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
18466 fntype = TREE_TYPE (fn);
18467 if (explicit_targs)
18469 /* [temp.deduct]
18471 The specified template arguments must match the template
18472 parameters in kind (i.e., type, nontype, template), and there
18473 must not be more arguments than there are parameters;
18474 otherwise type deduction fails.
18476 Nontype arguments must match the types of the corresponding
18477 nontype template parameters, or must be convertible to the
18478 types of the corresponding nontype parameters as specified in
18479 _temp.arg.nontype_, otherwise type deduction fails.
18481 All references in the function type of the function template
18482 to the corresponding template parameters are replaced by the
18483 specified template argument values. If a substitution in a
18484 template parameter or in the function type of the function
18485 template results in an invalid type, type deduction fails. */
18486 int i, len = TREE_VEC_LENGTH (tparms);
18487 location_t loc = input_location;
18488 bool incomplete = false;
18490 if (explicit_targs == error_mark_node)
18491 goto fail;
18493 if (TMPL_ARGS_DEPTH (explicit_targs)
18494 < TMPL_ARGS_DEPTH (full_targs))
18495 explicit_targs = add_outermost_template_args (full_targs,
18496 explicit_targs);
18498 /* Adjust any explicit template arguments before entering the
18499 substitution context. */
18500 explicit_targs
18501 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
18502 complain,
18503 /*require_all_args=*/false,
18504 /*use_default_args=*/false));
18505 if (explicit_targs == error_mark_node)
18506 goto fail;
18508 /* Substitute the explicit args into the function type. This is
18509 necessary so that, for instance, explicitly declared function
18510 arguments can match null pointed constants. If we were given
18511 an incomplete set of explicit args, we must not do semantic
18512 processing during substitution as we could create partial
18513 instantiations. */
18514 for (i = 0; i < len; i++)
18516 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
18517 bool parameter_pack = false;
18518 tree targ = TREE_VEC_ELT (explicit_targs, i);
18520 /* Dig out the actual parm. */
18521 if (TREE_CODE (parm) == TYPE_DECL
18522 || TREE_CODE (parm) == TEMPLATE_DECL)
18524 parm = TREE_TYPE (parm);
18525 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
18527 else if (TREE_CODE (parm) == PARM_DECL)
18529 parm = DECL_INITIAL (parm);
18530 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
18533 if (!parameter_pack && targ == NULL_TREE)
18534 /* No explicit argument for this template parameter. */
18535 incomplete = true;
18537 if (parameter_pack && pack_deducible_p (parm, fn))
18539 /* Mark the argument pack as "incomplete". We could
18540 still deduce more arguments during unification.
18541 We remove this mark in type_unification_real. */
18542 if (targ)
18544 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
18545 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
18546 = ARGUMENT_PACK_ARGS (targ);
18549 /* We have some incomplete argument packs. */
18550 incomplete = true;
18554 TREE_VALUE (tinst) = explicit_targs;
18555 if (!push_tinst_level (tinst))
18557 excessive_deduction_depth = true;
18558 goto fail;
18560 processing_template_decl += incomplete;
18561 input_location = DECL_SOURCE_LOCATION (fn);
18562 /* Ignore any access checks; we'll see them again in
18563 instantiate_template and they might have the wrong
18564 access path at this point. */
18565 push_deferring_access_checks (dk_deferred);
18566 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
18567 complain | tf_partial | tf_fndecl_type, NULL_TREE);
18568 pop_deferring_access_checks ();
18569 input_location = loc;
18570 processing_template_decl -= incomplete;
18571 pop_tinst_level ();
18573 if (fntype == error_mark_node)
18574 goto fail;
18576 /* Place the explicitly specified arguments in TARGS. */
18577 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
18578 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
18579 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
18582 /* Never do unification on the 'this' parameter. */
18583 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
18585 if (return_type && strict == DEDUCE_CALL)
18587 /* We're deducing for a call to the result of a template conversion
18588 function. The parms we really want are in return_type. */
18589 if (POINTER_TYPE_P (return_type))
18590 return_type = TREE_TYPE (return_type);
18591 parms = TYPE_ARG_TYPES (return_type);
18593 else if (return_type)
18595 tree *new_args;
18597 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
18598 new_args = XALLOCAVEC (tree, nargs + 1);
18599 new_args[0] = return_type;
18600 memcpy (new_args + 1, args, nargs * sizeof (tree));
18601 args = new_args;
18602 ++nargs;
18605 /* We allow incomplete unification without an error message here
18606 because the standard doesn't seem to explicitly prohibit it. Our
18607 callers must be ready to deal with unification failures in any
18608 event. */
18610 TREE_VALUE (tinst) = targs;
18611 /* If we aren't explaining yet, push tinst context so we can see where
18612 any errors (e.g. from class instantiations triggered by instantiation
18613 of default template arguments) come from. If we are explaining, this
18614 context is redundant. */
18615 if (!explain_p && !push_tinst_level (tinst))
18617 excessive_deduction_depth = true;
18618 goto fail;
18621 /* type_unification_real will pass back any access checks from default
18622 template argument substitution. */
18623 vec<deferred_access_check, va_gc> *checks;
18624 checks = NULL;
18626 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
18627 full_targs, parms, args, nargs, /*subr=*/0,
18628 strict, flags, &checks, explain_p);
18629 if (!explain_p)
18630 pop_tinst_level ();
18631 if (!ok)
18632 goto fail;
18634 /* Now that we have bindings for all of the template arguments,
18635 ensure that the arguments deduced for the template template
18636 parameters have compatible template parameter lists. We cannot
18637 check this property before we have deduced all template
18638 arguments, because the template parameter types of a template
18639 template parameter might depend on prior template parameters
18640 deduced after the template template parameter. The following
18641 ill-formed example illustrates this issue:
18643 template<typename T, template<T> class C> void f(C<5>, T);
18645 template<int N> struct X {};
18647 void g() {
18648 f(X<5>(), 5l); // error: template argument deduction fails
18651 The template parameter list of 'C' depends on the template type
18652 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
18653 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
18654 time that we deduce 'C'. */
18655 if (!template_template_parm_bindings_ok_p
18656 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
18658 unify_inconsistent_template_template_parameters (explain_p);
18659 goto fail;
18662 /* All is well so far. Now, check:
18664 [temp.deduct]
18666 When all template arguments have been deduced, all uses of
18667 template parameters in nondeduced contexts are replaced with
18668 the corresponding deduced argument values. If the
18669 substitution results in an invalid type, as described above,
18670 type deduction fails. */
18671 TREE_VALUE (tinst) = targs;
18672 if (!push_tinst_level (tinst))
18674 excessive_deduction_depth = true;
18675 goto fail;
18678 /* Also collect access checks from the instantiation. */
18679 reopen_deferring_access_checks (checks);
18681 decl = instantiate_template (fn, targs, complain);
18683 checks = get_deferred_access_checks ();
18684 pop_deferring_access_checks ();
18686 pop_tinst_level ();
18688 if (decl == error_mark_node)
18689 goto fail;
18691 /* Now perform any access checks encountered during substitution. */
18692 push_access_scope (decl);
18693 ok = perform_access_checks (checks, complain);
18694 pop_access_scope (decl);
18695 if (!ok)
18696 goto fail;
18698 /* If we're looking for an exact match, check that what we got
18699 is indeed an exact match. It might not be if some template
18700 parameters are used in non-deduced contexts. But don't check
18701 for an exact match if we have dependent template arguments;
18702 in that case we're doing partial ordering, and we already know
18703 that we have two candidates that will provide the actual type. */
18704 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
18706 tree substed = TREE_TYPE (decl);
18707 unsigned int i;
18709 tree sarg
18710 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
18711 if (return_type)
18712 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
18713 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
18714 if (!same_type_p (args[i], TREE_VALUE (sarg)))
18716 unify_type_mismatch (explain_p, args[i],
18717 TREE_VALUE (sarg));
18718 goto fail;
18722 /* After doing deduction with the inherited constructor, actually return an
18723 instantiation of the inheriting constructor. */
18724 if (orig_fn != fn)
18725 decl = instantiate_template (orig_fn, targs, complain);
18727 r = decl;
18729 fail:
18730 --deduction_depth;
18731 if (excessive_deduction_depth)
18733 if (deduction_depth == 0)
18734 /* Reset once we're all the way out. */
18735 excessive_deduction_depth = false;
18738 /* We can't free this if a pending_template entry or last_error_tinst_level
18739 is pointing at it. */
18740 if (last_pending_template == old_last_pend
18741 && last_error_tinst_level == old_error_tinst)
18742 ggc_free (tinst);
18744 return r;
18747 /* Adjust types before performing type deduction, as described in
18748 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
18749 sections are symmetric. PARM is the type of a function parameter
18750 or the return type of the conversion function. ARG is the type of
18751 the argument passed to the call, or the type of the value
18752 initialized with the result of the conversion function.
18753 ARG_EXPR is the original argument expression, which may be null. */
18755 static int
18756 maybe_adjust_types_for_deduction (unification_kind_t strict,
18757 tree* parm,
18758 tree* arg,
18759 tree arg_expr)
18761 int result = 0;
18763 switch (strict)
18765 case DEDUCE_CALL:
18766 break;
18768 case DEDUCE_CONV:
18769 /* Swap PARM and ARG throughout the remainder of this
18770 function; the handling is precisely symmetric since PARM
18771 will initialize ARG rather than vice versa. */
18772 std::swap (parm, arg);
18773 break;
18775 case DEDUCE_EXACT:
18776 /* Core issue #873: Do the DR606 thing (see below) for these cases,
18777 too, but here handle it by stripping the reference from PARM
18778 rather than by adding it to ARG. */
18779 if (TREE_CODE (*parm) == REFERENCE_TYPE
18780 && TYPE_REF_IS_RVALUE (*parm)
18781 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
18782 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
18783 && TREE_CODE (*arg) == REFERENCE_TYPE
18784 && !TYPE_REF_IS_RVALUE (*arg))
18785 *parm = TREE_TYPE (*parm);
18786 /* Nothing else to do in this case. */
18787 return 0;
18789 default:
18790 gcc_unreachable ();
18793 if (TREE_CODE (*parm) != REFERENCE_TYPE)
18795 /* [temp.deduct.call]
18797 If P is not a reference type:
18799 --If A is an array type, the pointer type produced by the
18800 array-to-pointer standard conversion (_conv.array_) is
18801 used in place of A for type deduction; otherwise,
18803 --If A is a function type, the pointer type produced by
18804 the function-to-pointer standard conversion
18805 (_conv.func_) is used in place of A for type deduction;
18806 otherwise,
18808 --If A is a cv-qualified type, the top level
18809 cv-qualifiers of A's type are ignored for type
18810 deduction. */
18811 if (TREE_CODE (*arg) == ARRAY_TYPE)
18812 *arg = build_pointer_type (TREE_TYPE (*arg));
18813 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
18814 *arg = build_pointer_type (*arg);
18815 else
18816 *arg = TYPE_MAIN_VARIANT (*arg);
18819 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
18820 reference to a cv-unqualified template parameter that does not represent a
18821 template parameter of a class template (during class template argument
18822 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
18823 an lvalue, the type "lvalue reference to A" is used in place of A for type
18824 deduction. */
18825 if (TREE_CODE (*parm) == REFERENCE_TYPE
18826 && TYPE_REF_IS_RVALUE (*parm)
18827 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
18828 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
18829 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
18830 && (arg_expr ? lvalue_p (arg_expr)
18831 /* try_one_overload doesn't provide an arg_expr, but
18832 functions are always lvalues. */
18833 : TREE_CODE (*arg) == FUNCTION_TYPE))
18834 *arg = build_reference_type (*arg);
18836 /* [temp.deduct.call]
18838 If P is a cv-qualified type, the top level cv-qualifiers
18839 of P's type are ignored for type deduction. If P is a
18840 reference type, the type referred to by P is used for
18841 type deduction. */
18842 *parm = TYPE_MAIN_VARIANT (*parm);
18843 if (TREE_CODE (*parm) == REFERENCE_TYPE)
18845 *parm = TREE_TYPE (*parm);
18846 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
18849 /* DR 322. For conversion deduction, remove a reference type on parm
18850 too (which has been swapped into ARG). */
18851 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
18852 *arg = TREE_TYPE (*arg);
18854 return result;
18857 /* Subroutine of unify_one_argument. PARM is a function parameter of a
18858 template which does contain any deducible template parameters; check if
18859 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
18860 unify_one_argument. */
18862 static int
18863 check_non_deducible_conversion (tree parm, tree arg, int strict,
18864 int flags, bool explain_p)
18866 tree type;
18868 if (!TYPE_P (arg))
18869 type = TREE_TYPE (arg);
18870 else
18871 type = arg;
18873 if (same_type_p (parm, type))
18874 return unify_success (explain_p);
18876 if (strict == DEDUCE_CONV)
18878 if (can_convert_arg (type, parm, NULL_TREE, flags,
18879 explain_p ? tf_warning_or_error : tf_none))
18880 return unify_success (explain_p);
18882 else if (strict != DEDUCE_EXACT)
18884 if (can_convert_arg (parm, type,
18885 TYPE_P (arg) ? NULL_TREE : arg,
18886 flags, explain_p ? tf_warning_or_error : tf_none))
18887 return unify_success (explain_p);
18890 if (strict == DEDUCE_EXACT)
18891 return unify_type_mismatch (explain_p, parm, arg);
18892 else
18893 return unify_arg_conversion (explain_p, parm, type, arg);
18896 static bool uses_deducible_template_parms (tree type);
18898 /* Returns true iff the expression EXPR is one from which a template
18899 argument can be deduced. In other words, if it's an undecorated
18900 use of a template non-type parameter. */
18902 static bool
18903 deducible_expression (tree expr)
18905 /* Strip implicit conversions. */
18906 while (CONVERT_EXPR_P (expr))
18907 expr = TREE_OPERAND (expr, 0);
18908 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
18911 /* Returns true iff the array domain DOMAIN uses a template parameter in a
18912 deducible way; that is, if it has a max value of <PARM> - 1. */
18914 static bool
18915 deducible_array_bound (tree domain)
18917 if (domain == NULL_TREE)
18918 return false;
18920 tree max = TYPE_MAX_VALUE (domain);
18921 if (TREE_CODE (max) != MINUS_EXPR)
18922 return false;
18924 return deducible_expression (TREE_OPERAND (max, 0));
18927 /* Returns true iff the template arguments ARGS use a template parameter
18928 in a deducible way. */
18930 static bool
18931 deducible_template_args (tree args)
18933 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
18935 bool deducible;
18936 tree elt = TREE_VEC_ELT (args, i);
18937 if (ARGUMENT_PACK_P (elt))
18938 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
18939 else
18941 if (PACK_EXPANSION_P (elt))
18942 elt = PACK_EXPANSION_PATTERN (elt);
18943 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
18944 deducible = true;
18945 else if (TYPE_P (elt))
18946 deducible = uses_deducible_template_parms (elt);
18947 else
18948 deducible = deducible_expression (elt);
18950 if (deducible)
18951 return true;
18953 return false;
18956 /* Returns true iff TYPE contains any deducible references to template
18957 parameters, as per 14.8.2.5. */
18959 static bool
18960 uses_deducible_template_parms (tree type)
18962 if (PACK_EXPANSION_P (type))
18963 type = PACK_EXPANSION_PATTERN (type);
18965 /* T
18966 cv-list T
18967 TT<T>
18968 TT<i>
18969 TT<> */
18970 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
18971 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
18972 return true;
18974 /* T*
18976 T&& */
18977 if (POINTER_TYPE_P (type))
18978 return uses_deducible_template_parms (TREE_TYPE (type));
18980 /* T[integer-constant ]
18981 type [i] */
18982 if (TREE_CODE (type) == ARRAY_TYPE)
18983 return (uses_deducible_template_parms (TREE_TYPE (type))
18984 || deducible_array_bound (TYPE_DOMAIN (type)));
18986 /* T type ::*
18987 type T::*
18988 T T::*
18989 T (type ::*)()
18990 type (T::*)()
18991 type (type ::*)(T)
18992 type (T::*)(T)
18993 T (type ::*)(T)
18994 T (T::*)()
18995 T (T::*)(T) */
18996 if (TYPE_PTRMEM_P (type))
18997 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
18998 || (uses_deducible_template_parms
18999 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
19001 /* template-name <T> (where template-name refers to a class template)
19002 template-name <i> (where template-name refers to a class template) */
19003 if (CLASS_TYPE_P (type)
19004 && CLASSTYPE_TEMPLATE_INFO (type)
19005 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
19006 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
19007 (CLASSTYPE_TI_ARGS (type)));
19009 /* type (T)
19011 T(T) */
19012 if (TREE_CODE (type) == FUNCTION_TYPE
19013 || TREE_CODE (type) == METHOD_TYPE)
19015 if (uses_deducible_template_parms (TREE_TYPE (type)))
19016 return true;
19017 tree parm = TYPE_ARG_TYPES (type);
19018 if (TREE_CODE (type) == METHOD_TYPE)
19019 parm = TREE_CHAIN (parm);
19020 for (; parm; parm = TREE_CHAIN (parm))
19021 if (uses_deducible_template_parms (TREE_VALUE (parm)))
19022 return true;
19025 return false;
19028 /* Subroutine of type_unification_real and unify_pack_expansion to
19029 handle unification of a single P/A pair. Parameters are as
19030 for those functions. */
19032 static int
19033 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
19034 int subr, unification_kind_t strict,
19035 bool explain_p)
19037 tree arg_expr = NULL_TREE;
19038 int arg_strict;
19040 if (arg == error_mark_node || parm == error_mark_node)
19041 return unify_invalid (explain_p);
19042 if (arg == unknown_type_node)
19043 /* We can't deduce anything from this, but we might get all the
19044 template args from other function args. */
19045 return unify_success (explain_p);
19047 /* Implicit conversions (Clause 4) will be performed on a function
19048 argument to convert it to the type of the corresponding function
19049 parameter if the parameter type contains no template-parameters that
19050 participate in template argument deduction. */
19051 if (strict != DEDUCE_EXACT
19052 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
19053 /* For function parameters with no deducible template parameters,
19054 just return. We'll check non-dependent conversions later. */
19055 return unify_success (explain_p);
19057 switch (strict)
19059 case DEDUCE_CALL:
19060 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
19061 | UNIFY_ALLOW_MORE_CV_QUAL
19062 | UNIFY_ALLOW_DERIVED);
19063 break;
19065 case DEDUCE_CONV:
19066 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
19067 break;
19069 case DEDUCE_EXACT:
19070 arg_strict = UNIFY_ALLOW_NONE;
19071 break;
19073 default:
19074 gcc_unreachable ();
19077 /* We only do these transformations if this is the top-level
19078 parameter_type_list in a call or declaration matching; in other
19079 situations (nested function declarators, template argument lists) we
19080 won't be comparing a type to an expression, and we don't do any type
19081 adjustments. */
19082 if (!subr)
19084 if (!TYPE_P (arg))
19086 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
19087 if (type_unknown_p (arg))
19089 /* [temp.deduct.type] A template-argument can be
19090 deduced from a pointer to function or pointer
19091 to member function argument if the set of
19092 overloaded functions does not contain function
19093 templates and at most one of a set of
19094 overloaded functions provides a unique
19095 match. */
19097 if (resolve_overloaded_unification
19098 (tparms, targs, parm, arg, strict,
19099 arg_strict, explain_p))
19100 return unify_success (explain_p);
19101 return unify_overload_resolution_failure (explain_p, arg);
19104 arg_expr = arg;
19105 arg = unlowered_expr_type (arg);
19106 if (arg == error_mark_node)
19107 return unify_invalid (explain_p);
19110 arg_strict |=
19111 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
19113 else
19114 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
19115 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
19116 return unify_template_argument_mismatch (explain_p, parm, arg);
19118 /* For deduction from an init-list we need the actual list. */
19119 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
19120 arg = arg_expr;
19121 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
19124 /* for_each_template_parm callback that always returns 0. */
19126 static int
19127 zero_r (tree, void *)
19129 return 0;
19132 /* for_each_template_parm any_fn callback to handle deduction of a template
19133 type argument from the type of an array bound. */
19135 static int
19136 array_deduction_r (tree t, void *data)
19138 tree_pair_p d = (tree_pair_p)data;
19139 tree &tparms = d->purpose;
19140 tree &targs = d->value;
19142 if (TREE_CODE (t) == ARRAY_TYPE)
19143 if (tree dom = TYPE_DOMAIN (t))
19144 if (tree max = TYPE_MAX_VALUE (dom))
19146 if (TREE_CODE (max) == MINUS_EXPR)
19147 max = TREE_OPERAND (max, 0);
19148 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
19149 unify (tparms, targs, TREE_TYPE (max), size_type_node,
19150 UNIFY_ALLOW_NONE, /*explain*/false);
19153 /* Keep walking. */
19154 return 0;
19157 /* Try to deduce any not-yet-deduced template type arguments from the type of
19158 an array bound. This is handled separately from unify because 14.8.2.5 says
19159 "The type of a type parameter is only deduced from an array bound if it is
19160 not otherwise deduced." */
19162 static void
19163 try_array_deduction (tree tparms, tree targs, tree parm)
19165 tree_pair_s data = { tparms, targs };
19166 hash_set<tree> visited;
19167 for_each_template_parm (parm, zero_r, &data, &visited,
19168 /*nondeduced*/false, array_deduction_r);
19171 /* Most parms like fn_type_unification.
19173 If SUBR is 1, we're being called recursively (to unify the
19174 arguments of a function or method parameter of a function
19175 template).
19177 CHECKS is a pointer to a vector of access checks encountered while
19178 substituting default template arguments. */
19180 static int
19181 type_unification_real (tree tparms,
19182 tree full_targs,
19183 tree xparms,
19184 const tree *xargs,
19185 unsigned int xnargs,
19186 int subr,
19187 unification_kind_t strict,
19188 int flags,
19189 vec<deferred_access_check, va_gc> **checks,
19190 bool explain_p)
19192 tree parm, arg;
19193 int i;
19194 int ntparms = TREE_VEC_LENGTH (tparms);
19195 int saw_undeduced = 0;
19196 tree parms;
19197 const tree *args;
19198 unsigned int nargs;
19199 unsigned int ia;
19201 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
19202 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
19203 gcc_assert (ntparms > 0);
19205 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
19207 /* Reset the number of non-defaulted template arguments contained
19208 in TARGS. */
19209 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
19211 again:
19212 parms = xparms;
19213 args = xargs;
19214 nargs = xnargs;
19216 ia = 0;
19217 while (parms && parms != void_list_node
19218 && ia < nargs)
19220 parm = TREE_VALUE (parms);
19222 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19223 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
19224 /* For a function parameter pack that occurs at the end of the
19225 parameter-declaration-list, the type A of each remaining
19226 argument of the call is compared with the type P of the
19227 declarator-id of the function parameter pack. */
19228 break;
19230 parms = TREE_CHAIN (parms);
19232 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19233 /* For a function parameter pack that does not occur at the
19234 end of the parameter-declaration-list, the type of the
19235 parameter pack is a non-deduced context. */
19236 continue;
19238 arg = args[ia];
19239 ++ia;
19241 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
19242 explain_p))
19243 return 1;
19246 if (parms
19247 && parms != void_list_node
19248 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
19250 /* Unify the remaining arguments with the pack expansion type. */
19251 tree argvec;
19252 tree parmvec = make_tree_vec (1);
19254 /* Allocate a TREE_VEC and copy in all of the arguments */
19255 argvec = make_tree_vec (nargs - ia);
19256 for (i = 0; ia < nargs; ++ia, ++i)
19257 TREE_VEC_ELT (argvec, i) = args[ia];
19259 /* Copy the parameter into parmvec. */
19260 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
19261 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
19262 /*subr=*/subr, explain_p))
19263 return 1;
19265 /* Advance to the end of the list of parameters. */
19266 parms = TREE_CHAIN (parms);
19269 /* Fail if we've reached the end of the parm list, and more args
19270 are present, and the parm list isn't variadic. */
19271 if (ia < nargs && parms == void_list_node)
19272 return unify_too_many_arguments (explain_p, nargs, ia);
19273 /* Fail if parms are left and they don't have default values and
19274 they aren't all deduced as empty packs (c++/57397). This is
19275 consistent with sufficient_parms_p. */
19276 if (parms && parms != void_list_node
19277 && TREE_PURPOSE (parms) == NULL_TREE)
19279 unsigned int count = nargs;
19280 tree p = parms;
19281 bool type_pack_p;
19284 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
19285 if (!type_pack_p)
19286 count++;
19287 p = TREE_CHAIN (p);
19289 while (p && p != void_list_node);
19290 if (count != nargs)
19291 return unify_too_few_arguments (explain_p, ia, count,
19292 type_pack_p);
19295 if (!subr)
19297 tsubst_flags_t complain = (explain_p
19298 ? tf_warning_or_error
19299 : tf_none);
19300 bool tried_array_deduction = (cxx_dialect < cxx1z);
19302 for (i = 0; i < ntparms; i++)
19304 tree targ = TREE_VEC_ELT (targs, i);
19305 tree tparm = TREE_VEC_ELT (tparms, i);
19307 /* Clear the "incomplete" flags on all argument packs now so that
19308 substituting them into later default arguments works. */
19309 if (targ && ARGUMENT_PACK_P (targ))
19311 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
19312 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
19315 if (targ || tparm == error_mark_node)
19316 continue;
19317 tparm = TREE_VALUE (tparm);
19319 if (TREE_CODE (tparm) == TYPE_DECL
19320 && !tried_array_deduction)
19322 try_array_deduction (tparms, targs, xparms);
19323 tried_array_deduction = true;
19324 if (TREE_VEC_ELT (targs, i))
19325 continue;
19328 /* If this is an undeduced nontype parameter that depends on
19329 a type parameter, try another pass; its type may have been
19330 deduced from a later argument than the one from which
19331 this parameter can be deduced. */
19332 if (TREE_CODE (tparm) == PARM_DECL
19333 && uses_template_parms (TREE_TYPE (tparm))
19334 && saw_undeduced < 2)
19336 saw_undeduced = 1;
19337 continue;
19340 /* Core issue #226 (C++0x) [temp.deduct]:
19342 If a template argument has not been deduced, its
19343 default template argument, if any, is used.
19345 When we are in C++98 mode, TREE_PURPOSE will either
19346 be NULL_TREE or ERROR_MARK_NODE, so we do not need
19347 to explicitly check cxx_dialect here. */
19348 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
19349 /* OK, there is a default argument. Wait until after the
19350 conversion check to do substitution. */
19351 continue;
19353 /* If the type parameter is a parameter pack, then it will
19354 be deduced to an empty parameter pack. */
19355 if (template_parameter_pack_p (tparm))
19357 tree arg;
19359 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
19361 arg = make_node (NONTYPE_ARGUMENT_PACK);
19362 TREE_CONSTANT (arg) = 1;
19364 else
19365 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
19367 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
19369 TREE_VEC_ELT (targs, i) = arg;
19370 continue;
19373 return unify_parameter_deduction_failure (explain_p, tparm);
19376 /* DR 1391: All parameters have args, now check non-dependent parms for
19377 convertibility. */
19378 if (saw_undeduced < 2)
19379 for (ia = 0, parms = xparms, args = xargs, nargs = xnargs;
19380 parms && parms != void_list_node && ia < nargs; )
19382 parm = TREE_VALUE (parms);
19384 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19385 && (!TREE_CHAIN (parms)
19386 || TREE_CHAIN (parms) == void_list_node))
19387 /* For a function parameter pack that occurs at the end of the
19388 parameter-declaration-list, the type A of each remaining
19389 argument of the call is compared with the type P of the
19390 declarator-id of the function parameter pack. */
19391 break;
19393 parms = TREE_CHAIN (parms);
19395 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19396 /* For a function parameter pack that does not occur at the
19397 end of the parameter-declaration-list, the type of the
19398 parameter pack is a non-deduced context. */
19399 continue;
19401 arg = args[ia];
19402 ++ia;
19404 if (uses_template_parms (parm))
19405 continue;
19406 if (check_non_deducible_conversion (parm, arg, strict, flags,
19407 explain_p))
19408 return 1;
19411 /* Now substitute into the default template arguments. */
19412 for (i = 0; i < ntparms; i++)
19414 tree targ = TREE_VEC_ELT (targs, i);
19415 tree tparm = TREE_VEC_ELT (tparms, i);
19417 if (targ || tparm == error_mark_node)
19418 continue;
19419 tree parm = TREE_VALUE (tparm);
19421 if (TREE_CODE (parm) == PARM_DECL
19422 && uses_template_parms (TREE_TYPE (parm))
19423 && saw_undeduced < 2)
19424 continue;
19426 tree arg = TREE_PURPOSE (tparm);
19427 reopen_deferring_access_checks (*checks);
19428 location_t save_loc = input_location;
19429 if (DECL_P (parm))
19430 input_location = DECL_SOURCE_LOCATION (parm);
19431 arg = tsubst_template_arg (arg, full_targs, complain, NULL_TREE);
19432 if (!uses_template_parms (arg))
19433 arg = convert_template_argument (parm, arg, full_targs, complain,
19434 i, NULL_TREE);
19435 else if (saw_undeduced < 2)
19436 arg = NULL_TREE;
19437 else
19438 arg = error_mark_node;
19439 input_location = save_loc;
19440 *checks = get_deferred_access_checks ();
19441 pop_deferring_access_checks ();
19442 if (arg == error_mark_node)
19443 return 1;
19444 else if (arg)
19446 TREE_VEC_ELT (targs, i) = arg;
19447 /* The position of the first default template argument,
19448 is also the number of non-defaulted arguments in TARGS.
19449 Record that. */
19450 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19451 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
19455 if (saw_undeduced++ == 1)
19456 goto again;
19459 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
19460 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
19462 return unify_success (explain_p);
19465 /* Subroutine of type_unification_real. Args are like the variables
19466 at the call site. ARG is an overloaded function (or template-id);
19467 we try deducing template args from each of the overloads, and if
19468 only one succeeds, we go with that. Modifies TARGS and returns
19469 true on success. */
19471 static bool
19472 resolve_overloaded_unification (tree tparms,
19473 tree targs,
19474 tree parm,
19475 tree arg,
19476 unification_kind_t strict,
19477 int sub_strict,
19478 bool explain_p)
19480 tree tempargs = copy_node (targs);
19481 int good = 0;
19482 tree goodfn = NULL_TREE;
19483 bool addr_p;
19485 if (TREE_CODE (arg) == ADDR_EXPR)
19487 arg = TREE_OPERAND (arg, 0);
19488 addr_p = true;
19490 else
19491 addr_p = false;
19493 if (TREE_CODE (arg) == COMPONENT_REF)
19494 /* Handle `&x' where `x' is some static or non-static member
19495 function name. */
19496 arg = TREE_OPERAND (arg, 1);
19498 if (TREE_CODE (arg) == OFFSET_REF)
19499 arg = TREE_OPERAND (arg, 1);
19501 /* Strip baselink information. */
19502 if (BASELINK_P (arg))
19503 arg = BASELINK_FUNCTIONS (arg);
19505 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
19507 /* If we got some explicit template args, we need to plug them into
19508 the affected templates before we try to unify, in case the
19509 explicit args will completely resolve the templates in question. */
19511 int ok = 0;
19512 tree expl_subargs = TREE_OPERAND (arg, 1);
19513 arg = TREE_OPERAND (arg, 0);
19515 for (lkp_iterator iter (arg); iter; ++iter)
19517 tree fn = *iter;
19518 tree subargs, elem;
19520 if (TREE_CODE (fn) != TEMPLATE_DECL)
19521 continue;
19523 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19524 expl_subargs, NULL_TREE, tf_none,
19525 /*require_all_args=*/true,
19526 /*use_default_args=*/true);
19527 if (subargs != error_mark_node
19528 && !any_dependent_template_arguments_p (subargs))
19530 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
19531 if (try_one_overload (tparms, targs, tempargs, parm,
19532 elem, strict, sub_strict, addr_p, explain_p)
19533 && (!goodfn || !same_type_p (goodfn, elem)))
19535 goodfn = elem;
19536 ++good;
19539 else if (subargs)
19540 ++ok;
19542 /* If no templates (or more than one) are fully resolved by the
19543 explicit arguments, this template-id is a non-deduced context; it
19544 could still be OK if we deduce all template arguments for the
19545 enclosing call through other arguments. */
19546 if (good != 1)
19547 good = ok;
19549 else if (TREE_CODE (arg) != OVERLOAD
19550 && TREE_CODE (arg) != FUNCTION_DECL)
19551 /* If ARG is, for example, "(0, &f)" then its type will be unknown
19552 -- but the deduction does not succeed because the expression is
19553 not just the function on its own. */
19554 return false;
19555 else
19556 for (lkp_iterator iter (arg); iter; ++iter)
19558 tree fn = *iter;
19559 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
19560 strict, sub_strict, addr_p, explain_p)
19561 && (!goodfn || !decls_match (goodfn, fn)))
19563 goodfn = fn;
19564 ++good;
19568 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19569 to function or pointer to member function argument if the set of
19570 overloaded functions does not contain function templates and at most
19571 one of a set of overloaded functions provides a unique match.
19573 So if we found multiple possibilities, we return success but don't
19574 deduce anything. */
19576 if (good == 1)
19578 int i = TREE_VEC_LENGTH (targs);
19579 for (; i--; )
19580 if (TREE_VEC_ELT (tempargs, i))
19582 tree old = TREE_VEC_ELT (targs, i);
19583 tree new_ = TREE_VEC_ELT (tempargs, i);
19584 if (new_ && old && ARGUMENT_PACK_P (old)
19585 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
19586 /* Don't forget explicit template arguments in a pack. */
19587 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
19588 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
19589 TREE_VEC_ELT (targs, i) = new_;
19592 if (good)
19593 return true;
19595 return false;
19598 /* Core DR 115: In contexts where deduction is done and fails, or in
19599 contexts where deduction is not done, if a template argument list is
19600 specified and it, along with any default template arguments, identifies
19601 a single function template specialization, then the template-id is an
19602 lvalue for the function template specialization. */
19604 tree
19605 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
19607 tree expr, offset, baselink;
19608 bool addr;
19610 if (!type_unknown_p (orig_expr))
19611 return orig_expr;
19613 expr = orig_expr;
19614 addr = false;
19615 offset = NULL_TREE;
19616 baselink = NULL_TREE;
19618 if (TREE_CODE (expr) == ADDR_EXPR)
19620 expr = TREE_OPERAND (expr, 0);
19621 addr = true;
19623 if (TREE_CODE (expr) == OFFSET_REF)
19625 offset = expr;
19626 expr = TREE_OPERAND (expr, 1);
19628 if (BASELINK_P (expr))
19630 baselink = expr;
19631 expr = BASELINK_FUNCTIONS (expr);
19634 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
19636 int good = 0;
19637 tree goodfn = NULL_TREE;
19639 /* If we got some explicit template args, we need to plug them into
19640 the affected templates before we try to unify, in case the
19641 explicit args will completely resolve the templates in question. */
19643 tree expl_subargs = TREE_OPERAND (expr, 1);
19644 tree arg = TREE_OPERAND (expr, 0);
19645 tree badfn = NULL_TREE;
19646 tree badargs = NULL_TREE;
19648 for (lkp_iterator iter (arg); iter; ++iter)
19650 tree fn = *iter;
19651 tree subargs, elem;
19653 if (TREE_CODE (fn) != TEMPLATE_DECL)
19654 continue;
19656 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19657 expl_subargs, NULL_TREE, tf_none,
19658 /*require_all_args=*/true,
19659 /*use_default_args=*/true);
19660 if (subargs != error_mark_node
19661 && !any_dependent_template_arguments_p (subargs))
19663 elem = instantiate_template (fn, subargs, tf_none);
19664 if (elem == error_mark_node)
19666 badfn = fn;
19667 badargs = subargs;
19669 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
19671 goodfn = elem;
19672 ++good;
19676 if (good == 1)
19678 mark_used (goodfn);
19679 expr = goodfn;
19680 if (baselink)
19681 expr = build_baselink (BASELINK_BINFO (baselink),
19682 BASELINK_ACCESS_BINFO (baselink),
19683 expr, BASELINK_OPTYPE (baselink));
19684 if (offset)
19686 tree base
19687 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
19688 expr = build_offset_ref (base, expr, addr, complain);
19690 if (addr)
19691 expr = cp_build_addr_expr (expr, complain);
19692 return expr;
19694 else if (good == 0 && badargs && (complain & tf_error))
19695 /* There were no good options and at least one bad one, so let the
19696 user know what the problem is. */
19697 instantiate_template (badfn, badargs, complain);
19699 return orig_expr;
19702 /* Subroutine of resolve_overloaded_unification; does deduction for a single
19703 overload. Fills TARGS with any deduced arguments, or error_mark_node if
19704 different overloads deduce different arguments for a given parm.
19705 ADDR_P is true if the expression for which deduction is being
19706 performed was of the form "& fn" rather than simply "fn".
19708 Returns 1 on success. */
19710 static int
19711 try_one_overload (tree tparms,
19712 tree orig_targs,
19713 tree targs,
19714 tree parm,
19715 tree arg,
19716 unification_kind_t strict,
19717 int sub_strict,
19718 bool addr_p,
19719 bool explain_p)
19721 int nargs;
19722 tree tempargs;
19723 int i;
19725 if (arg == error_mark_node)
19726 return 0;
19728 /* [temp.deduct.type] A template-argument can be deduced from a pointer
19729 to function or pointer to member function argument if the set of
19730 overloaded functions does not contain function templates and at most
19731 one of a set of overloaded functions provides a unique match.
19733 So if this is a template, just return success. */
19735 if (uses_template_parms (arg))
19736 return 1;
19738 if (TREE_CODE (arg) == METHOD_TYPE)
19739 arg = build_ptrmemfunc_type (build_pointer_type (arg));
19740 else if (addr_p)
19741 arg = build_pointer_type (arg);
19743 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
19745 /* We don't copy orig_targs for this because if we have already deduced
19746 some template args from previous args, unify would complain when we
19747 try to deduce a template parameter for the same argument, even though
19748 there isn't really a conflict. */
19749 nargs = TREE_VEC_LENGTH (targs);
19750 tempargs = make_tree_vec (nargs);
19752 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
19753 return 0;
19755 /* First make sure we didn't deduce anything that conflicts with
19756 explicitly specified args. */
19757 for (i = nargs; i--; )
19759 tree elt = TREE_VEC_ELT (tempargs, i);
19760 tree oldelt = TREE_VEC_ELT (orig_targs, i);
19762 if (!elt)
19763 /*NOP*/;
19764 else if (uses_template_parms (elt))
19765 /* Since we're unifying against ourselves, we will fill in
19766 template args used in the function parm list with our own
19767 template parms. Discard them. */
19768 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
19769 else if (oldelt && ARGUMENT_PACK_P (oldelt))
19771 /* Check that the argument at each index of the deduced argument pack
19772 is equivalent to the corresponding explicitly specified argument.
19773 We may have deduced more arguments than were explicitly specified,
19774 and that's OK. */
19776 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
19777 that's wrong if we deduce the same argument pack from multiple
19778 function arguments: it's only incomplete the first time. */
19780 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
19781 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
19783 if (TREE_VEC_LENGTH (deduced_pack)
19784 < TREE_VEC_LENGTH (explicit_pack))
19785 return 0;
19787 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
19788 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
19789 TREE_VEC_ELT (deduced_pack, j)))
19790 return 0;
19792 else if (oldelt && !template_args_equal (oldelt, elt))
19793 return 0;
19796 for (i = nargs; i--; )
19798 tree elt = TREE_VEC_ELT (tempargs, i);
19800 if (elt)
19801 TREE_VEC_ELT (targs, i) = elt;
19804 return 1;
19807 /* PARM is a template class (perhaps with unbound template
19808 parameters). ARG is a fully instantiated type. If ARG can be
19809 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
19810 TARGS are as for unify. */
19812 static tree
19813 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
19814 bool explain_p)
19816 tree copy_of_targs;
19818 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
19819 return NULL_TREE;
19820 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19821 /* Matches anything. */;
19822 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
19823 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
19824 return NULL_TREE;
19826 /* We need to make a new template argument vector for the call to
19827 unify. If we used TARGS, we'd clutter it up with the result of
19828 the attempted unification, even if this class didn't work out.
19829 We also don't want to commit ourselves to all the unifications
19830 we've already done, since unification is supposed to be done on
19831 an argument-by-argument basis. In other words, consider the
19832 following pathological case:
19834 template <int I, int J, int K>
19835 struct S {};
19837 template <int I, int J>
19838 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
19840 template <int I, int J, int K>
19841 void f(S<I, J, K>, S<I, I, I>);
19843 void g() {
19844 S<0, 0, 0> s0;
19845 S<0, 1, 2> s2;
19847 f(s0, s2);
19850 Now, by the time we consider the unification involving `s2', we
19851 already know that we must have `f<0, 0, 0>'. But, even though
19852 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
19853 because there are two ways to unify base classes of S<0, 1, 2>
19854 with S<I, I, I>. If we kept the already deduced knowledge, we
19855 would reject the possibility I=1. */
19856 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
19858 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19860 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
19861 return NULL_TREE;
19862 return arg;
19865 /* If unification failed, we're done. */
19866 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
19867 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
19868 return NULL_TREE;
19870 return arg;
19873 /* Given a template type PARM and a class type ARG, find the unique
19874 base type in ARG that is an instance of PARM. We do not examine
19875 ARG itself; only its base-classes. If there is not exactly one
19876 appropriate base class, return NULL_TREE. PARM may be the type of
19877 a partial specialization, as well as a plain template type. Used
19878 by unify. */
19880 static enum template_base_result
19881 get_template_base (tree tparms, tree targs, tree parm, tree arg,
19882 bool explain_p, tree *result)
19884 tree rval = NULL_TREE;
19885 tree binfo;
19887 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
19889 binfo = TYPE_BINFO (complete_type (arg));
19890 if (!binfo)
19892 /* The type could not be completed. */
19893 *result = NULL_TREE;
19894 return tbr_incomplete_type;
19897 /* Walk in inheritance graph order. The search order is not
19898 important, and this avoids multiple walks of virtual bases. */
19899 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
19901 tree r = try_class_unification (tparms, targs, parm,
19902 BINFO_TYPE (binfo), explain_p);
19904 if (r)
19906 /* If there is more than one satisfactory baseclass, then:
19908 [temp.deduct.call]
19910 If they yield more than one possible deduced A, the type
19911 deduction fails.
19913 applies. */
19914 if (rval && !same_type_p (r, rval))
19916 *result = NULL_TREE;
19917 return tbr_ambiguous_baseclass;
19920 rval = r;
19924 *result = rval;
19925 return tbr_success;
19928 /* Returns the level of DECL, which declares a template parameter. */
19930 static int
19931 template_decl_level (tree decl)
19933 switch (TREE_CODE (decl))
19935 case TYPE_DECL:
19936 case TEMPLATE_DECL:
19937 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
19939 case PARM_DECL:
19940 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
19942 default:
19943 gcc_unreachable ();
19945 return 0;
19948 /* Decide whether ARG can be unified with PARM, considering only the
19949 cv-qualifiers of each type, given STRICT as documented for unify.
19950 Returns nonzero iff the unification is OK on that basis. */
19952 static int
19953 check_cv_quals_for_unify (int strict, tree arg, tree parm)
19955 int arg_quals = cp_type_quals (arg);
19956 int parm_quals = cp_type_quals (parm);
19958 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
19959 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
19961 /* Although a CVR qualifier is ignored when being applied to a
19962 substituted template parameter ([8.3.2]/1 for example), that
19963 does not allow us to unify "const T" with "int&" because both
19964 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
19965 It is ok when we're allowing additional CV qualifiers
19966 at the outer level [14.8.2.1]/3,1st bullet. */
19967 if ((TREE_CODE (arg) == REFERENCE_TYPE
19968 || TREE_CODE (arg) == FUNCTION_TYPE
19969 || TREE_CODE (arg) == METHOD_TYPE)
19970 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
19971 return 0;
19973 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
19974 && (parm_quals & TYPE_QUAL_RESTRICT))
19975 return 0;
19978 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
19979 && (arg_quals & parm_quals) != parm_quals)
19980 return 0;
19982 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
19983 && (parm_quals & arg_quals) != arg_quals)
19984 return 0;
19986 return 1;
19989 /* Determines the LEVEL and INDEX for the template parameter PARM. */
19990 void
19991 template_parm_level_and_index (tree parm, int* level, int* index)
19993 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
19994 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
19995 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
19997 *index = TEMPLATE_TYPE_IDX (parm);
19998 *level = TEMPLATE_TYPE_LEVEL (parm);
20000 else
20002 *index = TEMPLATE_PARM_IDX (parm);
20003 *level = TEMPLATE_PARM_LEVEL (parm);
20007 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
20008 do { \
20009 if (unify (TP, TA, P, A, S, EP)) \
20010 return 1; \
20011 } while (0)
20013 /* Unifies the remaining arguments in PACKED_ARGS with the pack
20014 expansion at the end of PACKED_PARMS. Returns 0 if the type
20015 deduction succeeds, 1 otherwise. STRICT is the same as in
20016 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
20017 function call argument list. We'll need to adjust the arguments to make them
20018 types. SUBR tells us if this is from a recursive call to
20019 type_unification_real, or for comparing two template argument
20020 lists. */
20022 static int
20023 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
20024 tree packed_args, unification_kind_t strict,
20025 bool subr, bool explain_p)
20027 tree parm
20028 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
20029 tree pattern = PACK_EXPANSION_PATTERN (parm);
20030 tree pack, packs = NULL_TREE;
20031 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
20033 packed_args = expand_template_argument_pack (packed_args);
20035 int len = TREE_VEC_LENGTH (packed_args);
20037 /* Determine the parameter packs we will be deducing from the
20038 pattern, and record their current deductions. */
20039 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
20040 pack; pack = TREE_CHAIN (pack))
20042 tree parm_pack = TREE_VALUE (pack);
20043 int idx, level;
20045 /* Determine the index and level of this parameter pack. */
20046 template_parm_level_and_index (parm_pack, &level, &idx);
20048 /* Keep track of the parameter packs and their corresponding
20049 argument packs. */
20050 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
20051 TREE_TYPE (packs) = make_tree_vec (len - start);
20054 /* Loop through all of the arguments that have not yet been
20055 unified and unify each with the pattern. */
20056 for (i = start; i < len; i++)
20058 tree parm;
20059 bool any_explicit = false;
20060 tree arg = TREE_VEC_ELT (packed_args, i);
20062 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
20063 or the element of its argument pack at the current index if
20064 this argument was explicitly specified. */
20065 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20067 int idx, level;
20068 tree arg, pargs;
20069 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20071 arg = NULL_TREE;
20072 if (TREE_VALUE (pack)
20073 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
20074 && (i - start < TREE_VEC_LENGTH (pargs)))
20076 any_explicit = true;
20077 arg = TREE_VEC_ELT (pargs, i - start);
20079 TMPL_ARG (targs, level, idx) = arg;
20082 /* If we had explicit template arguments, substitute them into the
20083 pattern before deduction. */
20084 if (any_explicit)
20086 /* Some arguments might still be unspecified or dependent. */
20087 bool dependent;
20088 ++processing_template_decl;
20089 dependent = any_dependent_template_arguments_p (targs);
20090 if (!dependent)
20091 --processing_template_decl;
20092 parm = tsubst (pattern, targs,
20093 explain_p ? tf_warning_or_error : tf_none,
20094 NULL_TREE);
20095 if (dependent)
20096 --processing_template_decl;
20097 if (parm == error_mark_node)
20098 return 1;
20100 else
20101 parm = pattern;
20103 /* Unify the pattern with the current argument. */
20104 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
20105 explain_p))
20106 return 1;
20108 /* For each parameter pack, collect the deduced value. */
20109 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20111 int idx, level;
20112 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20114 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
20115 TMPL_ARG (targs, level, idx);
20119 /* Verify that the results of unification with the parameter packs
20120 produce results consistent with what we've seen before, and make
20121 the deduced argument packs available. */
20122 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20124 tree old_pack = TREE_VALUE (pack);
20125 tree new_args = TREE_TYPE (pack);
20126 int i, len = TREE_VEC_LENGTH (new_args);
20127 int idx, level;
20128 bool nondeduced_p = false;
20130 /* By default keep the original deduced argument pack.
20131 If necessary, more specific code is going to update the
20132 resulting deduced argument later down in this function. */
20133 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20134 TMPL_ARG (targs, level, idx) = old_pack;
20136 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
20137 actually deduce anything. */
20138 for (i = 0; i < len && !nondeduced_p; ++i)
20139 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
20140 nondeduced_p = true;
20141 if (nondeduced_p)
20142 continue;
20144 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
20146 /* If we had fewer function args than explicit template args,
20147 just use the explicits. */
20148 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20149 int explicit_len = TREE_VEC_LENGTH (explicit_args);
20150 if (len < explicit_len)
20151 new_args = explicit_args;
20154 if (!old_pack)
20156 tree result;
20157 /* Build the deduced *_ARGUMENT_PACK. */
20158 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
20160 result = make_node (NONTYPE_ARGUMENT_PACK);
20161 TREE_CONSTANT (result) = 1;
20163 else
20164 result = cxx_make_type (TYPE_ARGUMENT_PACK);
20166 SET_ARGUMENT_PACK_ARGS (result, new_args);
20168 /* Note the deduced argument packs for this parameter
20169 pack. */
20170 TMPL_ARG (targs, level, idx) = result;
20172 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
20173 && (ARGUMENT_PACK_ARGS (old_pack)
20174 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
20176 /* We only had the explicitly-provided arguments before, but
20177 now we have a complete set of arguments. */
20178 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20180 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
20181 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
20182 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
20184 else
20186 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
20187 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
20189 if (!comp_template_args (old_args, new_args,
20190 &bad_old_arg, &bad_new_arg))
20191 /* Inconsistent unification of this parameter pack. */
20192 return unify_parameter_pack_inconsistent (explain_p,
20193 bad_old_arg,
20194 bad_new_arg);
20198 return unify_success (explain_p);
20201 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
20202 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
20203 parameters and return value are as for unify. */
20205 static int
20206 unify_array_domain (tree tparms, tree targs,
20207 tree parm_dom, tree arg_dom,
20208 bool explain_p)
20210 tree parm_max;
20211 tree arg_max;
20212 bool parm_cst;
20213 bool arg_cst;
20215 /* Our representation of array types uses "N - 1" as the
20216 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
20217 not an integer constant. We cannot unify arbitrarily
20218 complex expressions, so we eliminate the MINUS_EXPRs
20219 here. */
20220 parm_max = TYPE_MAX_VALUE (parm_dom);
20221 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
20222 if (!parm_cst)
20224 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
20225 parm_max = TREE_OPERAND (parm_max, 0);
20227 arg_max = TYPE_MAX_VALUE (arg_dom);
20228 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
20229 if (!arg_cst)
20231 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
20232 trying to unify the type of a variable with the type
20233 of a template parameter. For example:
20235 template <unsigned int N>
20236 void f (char (&) [N]);
20237 int g();
20238 void h(int i) {
20239 char a[g(i)];
20240 f(a);
20243 Here, the type of the ARG will be "int [g(i)]", and
20244 may be a SAVE_EXPR, etc. */
20245 if (TREE_CODE (arg_max) != MINUS_EXPR)
20246 return unify_vla_arg (explain_p, arg_dom);
20247 arg_max = TREE_OPERAND (arg_max, 0);
20250 /* If only one of the bounds used a MINUS_EXPR, compensate
20251 by adding one to the other bound. */
20252 if (parm_cst && !arg_cst)
20253 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
20254 integer_type_node,
20255 parm_max,
20256 integer_one_node);
20257 else if (arg_cst && !parm_cst)
20258 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
20259 integer_type_node,
20260 arg_max,
20261 integer_one_node);
20263 return unify (tparms, targs, parm_max, arg_max,
20264 UNIFY_ALLOW_INTEGER, explain_p);
20267 /* Returns whether T, a P or A in unify, is a type, template or expression. */
20269 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
20271 static pa_kind_t
20272 pa_kind (tree t)
20274 if (PACK_EXPANSION_P (t))
20275 t = PACK_EXPANSION_PATTERN (t);
20276 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
20277 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
20278 || DECL_TYPE_TEMPLATE_P (t))
20279 return pa_tmpl;
20280 else if (TYPE_P (t))
20281 return pa_type;
20282 else
20283 return pa_expr;
20286 /* Deduce the value of template parameters. TPARMS is the (innermost)
20287 set of template parameters to a template. TARGS is the bindings
20288 for those template parameters, as determined thus far; TARGS may
20289 include template arguments for outer levels of template parameters
20290 as well. PARM is a parameter to a template function, or a
20291 subcomponent of that parameter; ARG is the corresponding argument.
20292 This function attempts to match PARM with ARG in a manner
20293 consistent with the existing assignments in TARGS. If more values
20294 are deduced, then TARGS is updated.
20296 Returns 0 if the type deduction succeeds, 1 otherwise. The
20297 parameter STRICT is a bitwise or of the following flags:
20299 UNIFY_ALLOW_NONE:
20300 Require an exact match between PARM and ARG.
20301 UNIFY_ALLOW_MORE_CV_QUAL:
20302 Allow the deduced ARG to be more cv-qualified (by qualification
20303 conversion) than ARG.
20304 UNIFY_ALLOW_LESS_CV_QUAL:
20305 Allow the deduced ARG to be less cv-qualified than ARG.
20306 UNIFY_ALLOW_DERIVED:
20307 Allow the deduced ARG to be a template base class of ARG,
20308 or a pointer to a template base class of the type pointed to by
20309 ARG.
20310 UNIFY_ALLOW_INTEGER:
20311 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
20312 case for more information.
20313 UNIFY_ALLOW_OUTER_LEVEL:
20314 This is the outermost level of a deduction. Used to determine validity
20315 of qualification conversions. A valid qualification conversion must
20316 have const qualified pointers leading up to the inner type which
20317 requires additional CV quals, except at the outer level, where const
20318 is not required [conv.qual]. It would be normal to set this flag in
20319 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
20320 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
20321 This is the outermost level of a deduction, and PARM can be more CV
20322 qualified at this point.
20323 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
20324 This is the outermost level of a deduction, and PARM can be less CV
20325 qualified at this point. */
20327 static int
20328 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
20329 bool explain_p)
20331 int idx;
20332 tree targ;
20333 tree tparm;
20334 int strict_in = strict;
20335 tsubst_flags_t complain = (explain_p
20336 ? tf_warning_or_error
20337 : tf_none);
20339 /* I don't think this will do the right thing with respect to types.
20340 But the only case I've seen it in so far has been array bounds, where
20341 signedness is the only information lost, and I think that will be
20342 okay. */
20343 while (CONVERT_EXPR_P (parm))
20344 parm = TREE_OPERAND (parm, 0);
20346 if (arg == error_mark_node)
20347 return unify_invalid (explain_p);
20348 if (arg == unknown_type_node
20349 || arg == init_list_type_node)
20350 /* We can't deduce anything from this, but we might get all the
20351 template args from other function args. */
20352 return unify_success (explain_p);
20354 if (parm == any_targ_node || arg == any_targ_node)
20355 return unify_success (explain_p);
20357 /* If PARM uses template parameters, then we can't bail out here,
20358 even if ARG == PARM, since we won't record unifications for the
20359 template parameters. We might need them if we're trying to
20360 figure out which of two things is more specialized. */
20361 if (arg == parm && !uses_template_parms (parm))
20362 return unify_success (explain_p);
20364 /* Handle init lists early, so the rest of the function can assume
20365 we're dealing with a type. */
20366 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
20368 tree elt, elttype;
20369 unsigned i;
20370 tree orig_parm = parm;
20372 /* Replace T with std::initializer_list<T> for deduction. */
20373 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20374 && flag_deduce_init_list)
20375 parm = listify (parm);
20377 if (!is_std_init_list (parm)
20378 && TREE_CODE (parm) != ARRAY_TYPE)
20379 /* We can only deduce from an initializer list argument if the
20380 parameter is std::initializer_list or an array; otherwise this
20381 is a non-deduced context. */
20382 return unify_success (explain_p);
20384 if (TREE_CODE (parm) == ARRAY_TYPE)
20385 elttype = TREE_TYPE (parm);
20386 else
20388 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
20389 /* Deduction is defined in terms of a single type, so just punt
20390 on the (bizarre) std::initializer_list<T...>. */
20391 if (PACK_EXPANSION_P (elttype))
20392 return unify_success (explain_p);
20395 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
20397 int elt_strict = strict;
20399 if (elt == error_mark_node)
20400 return unify_invalid (explain_p);
20402 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
20404 tree type = TREE_TYPE (elt);
20405 if (type == error_mark_node)
20406 return unify_invalid (explain_p);
20407 /* It should only be possible to get here for a call. */
20408 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
20409 elt_strict |= maybe_adjust_types_for_deduction
20410 (DEDUCE_CALL, &elttype, &type, elt);
20411 elt = type;
20414 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
20415 explain_p);
20418 if (TREE_CODE (parm) == ARRAY_TYPE
20419 && deducible_array_bound (TYPE_DOMAIN (parm)))
20421 /* Also deduce from the length of the initializer list. */
20422 tree max = size_int (CONSTRUCTOR_NELTS (arg));
20423 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
20424 if (idx == error_mark_node)
20425 return unify_invalid (explain_p);
20426 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20427 idx, explain_p);
20430 /* If the std::initializer_list<T> deduction worked, replace the
20431 deduced A with std::initializer_list<A>. */
20432 if (orig_parm != parm)
20434 idx = TEMPLATE_TYPE_IDX (orig_parm);
20435 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20436 targ = listify (targ);
20437 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
20439 return unify_success (explain_p);
20442 /* If parm and arg aren't the same kind of thing (template, type, or
20443 expression), fail early. */
20444 if (pa_kind (parm) != pa_kind (arg))
20445 return unify_invalid (explain_p);
20447 /* Immediately reject some pairs that won't unify because of
20448 cv-qualification mismatches. */
20449 if (TREE_CODE (arg) == TREE_CODE (parm)
20450 && TYPE_P (arg)
20451 /* It is the elements of the array which hold the cv quals of an array
20452 type, and the elements might be template type parms. We'll check
20453 when we recurse. */
20454 && TREE_CODE (arg) != ARRAY_TYPE
20455 /* We check the cv-qualifiers when unifying with template type
20456 parameters below. We want to allow ARG `const T' to unify with
20457 PARM `T' for example, when computing which of two templates
20458 is more specialized, for example. */
20459 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
20460 && !check_cv_quals_for_unify (strict_in, arg, parm))
20461 return unify_cv_qual_mismatch (explain_p, parm, arg);
20463 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
20464 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
20465 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
20466 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
20467 strict &= ~UNIFY_ALLOW_DERIVED;
20468 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
20469 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
20471 switch (TREE_CODE (parm))
20473 case TYPENAME_TYPE:
20474 case SCOPE_REF:
20475 case UNBOUND_CLASS_TEMPLATE:
20476 /* In a type which contains a nested-name-specifier, template
20477 argument values cannot be deduced for template parameters used
20478 within the nested-name-specifier. */
20479 return unify_success (explain_p);
20481 case TEMPLATE_TYPE_PARM:
20482 case TEMPLATE_TEMPLATE_PARM:
20483 case BOUND_TEMPLATE_TEMPLATE_PARM:
20484 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20485 if (error_operand_p (tparm))
20486 return unify_invalid (explain_p);
20488 if (TEMPLATE_TYPE_LEVEL (parm)
20489 != template_decl_level (tparm))
20490 /* The PARM is not one we're trying to unify. Just check
20491 to see if it matches ARG. */
20493 if (TREE_CODE (arg) == TREE_CODE (parm)
20494 && (is_auto (parm) ? is_auto (arg)
20495 : same_type_p (parm, arg)))
20496 return unify_success (explain_p);
20497 else
20498 return unify_type_mismatch (explain_p, parm, arg);
20500 idx = TEMPLATE_TYPE_IDX (parm);
20501 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20502 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
20503 if (error_operand_p (tparm))
20504 return unify_invalid (explain_p);
20506 /* Check for mixed types and values. */
20507 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20508 && TREE_CODE (tparm) != TYPE_DECL)
20509 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20510 && TREE_CODE (tparm) != TEMPLATE_DECL))
20511 gcc_unreachable ();
20513 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20515 if ((strict_in & UNIFY_ALLOW_DERIVED)
20516 && CLASS_TYPE_P (arg))
20518 /* First try to match ARG directly. */
20519 tree t = try_class_unification (tparms, targs, parm, arg,
20520 explain_p);
20521 if (!t)
20523 /* Otherwise, look for a suitable base of ARG, as below. */
20524 enum template_base_result r;
20525 r = get_template_base (tparms, targs, parm, arg,
20526 explain_p, &t);
20527 if (!t)
20528 return unify_no_common_base (explain_p, r, parm, arg);
20529 arg = t;
20532 /* ARG must be constructed from a template class or a template
20533 template parameter. */
20534 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
20535 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20536 return unify_template_deduction_failure (explain_p, parm, arg);
20538 /* Deduce arguments T, i from TT<T> or TT<i>. */
20539 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
20540 return 1;
20542 arg = TYPE_TI_TEMPLATE (arg);
20544 /* Fall through to deduce template name. */
20547 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20548 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20550 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
20552 /* Simple cases: Value already set, does match or doesn't. */
20553 if (targ != NULL_TREE && template_args_equal (targ, arg))
20554 return unify_success (explain_p);
20555 else if (targ)
20556 return unify_inconsistency (explain_p, parm, targ, arg);
20558 else
20560 /* If PARM is `const T' and ARG is only `int', we don't have
20561 a match unless we are allowing additional qualification.
20562 If ARG is `const int' and PARM is just `T' that's OK;
20563 that binds `const int' to `T'. */
20564 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
20565 arg, parm))
20566 return unify_cv_qual_mismatch (explain_p, parm, arg);
20568 /* Consider the case where ARG is `const volatile int' and
20569 PARM is `const T'. Then, T should be `volatile int'. */
20570 arg = cp_build_qualified_type_real
20571 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
20572 if (arg == error_mark_node)
20573 return unify_invalid (explain_p);
20575 /* Simple cases: Value already set, does match or doesn't. */
20576 if (targ != NULL_TREE && same_type_p (targ, arg))
20577 return unify_success (explain_p);
20578 else if (targ)
20579 return unify_inconsistency (explain_p, parm, targ, arg);
20581 /* Make sure that ARG is not a variable-sized array. (Note
20582 that were talking about variable-sized arrays (like
20583 `int[n]'), rather than arrays of unknown size (like
20584 `int[]').) We'll get very confused by such a type since
20585 the bound of the array is not constant, and therefore
20586 not mangleable. Besides, such types are not allowed in
20587 ISO C++, so we can do as we please here. We do allow
20588 them for 'auto' deduction, since that isn't ABI-exposed. */
20589 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
20590 return unify_vla_arg (explain_p, arg);
20592 /* Strip typedefs as in convert_template_argument. */
20593 arg = canonicalize_type_argument (arg, tf_none);
20596 /* If ARG is a parameter pack or an expansion, we cannot unify
20597 against it unless PARM is also a parameter pack. */
20598 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20599 && !template_parameter_pack_p (parm))
20600 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20602 /* If the argument deduction results is a METHOD_TYPE,
20603 then there is a problem.
20604 METHOD_TYPE doesn't map to any real C++ type the result of
20605 the deduction can not be of that type. */
20606 if (TREE_CODE (arg) == METHOD_TYPE)
20607 return unify_method_type_error (explain_p, arg);
20609 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20610 return unify_success (explain_p);
20612 case TEMPLATE_PARM_INDEX:
20613 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
20614 if (error_operand_p (tparm))
20615 return unify_invalid (explain_p);
20617 if (TEMPLATE_PARM_LEVEL (parm)
20618 != template_decl_level (tparm))
20620 /* The PARM is not one we're trying to unify. Just check
20621 to see if it matches ARG. */
20622 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
20623 && cp_tree_equal (parm, arg));
20624 if (result)
20625 unify_expression_unequal (explain_p, parm, arg);
20626 return result;
20629 idx = TEMPLATE_PARM_IDX (parm);
20630 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
20632 if (targ)
20634 int x = !cp_tree_equal (targ, arg);
20635 if (x)
20636 unify_inconsistency (explain_p, parm, targ, arg);
20637 return x;
20640 if (cxx_dialect >= cxx1z
20641 /* We deduce from array bounds in try_array_deduction. */
20642 && !(strict & UNIFY_ALLOW_INTEGER)
20643 && uses_template_parms (TREE_TYPE (parm))
20644 && !type_uses_auto (TREE_TYPE (parm)))
20646 tree atype = TREE_TYPE (arg);
20647 RECUR_AND_CHECK_FAILURE (tparms, targs,
20648 TREE_TYPE (parm), atype,
20649 UNIFY_ALLOW_NONE, explain_p);
20652 /* [temp.deduct.type] If, in the declaration of a function template
20653 with a non-type template-parameter, the non-type
20654 template-parameter is used in an expression in the function
20655 parameter-list and, if the corresponding template-argument is
20656 deduced, the template-argument type shall match the type of the
20657 template-parameter exactly, except that a template-argument
20658 deduced from an array bound may be of any integral type.
20659 The non-type parameter might use already deduced type parameters. */
20660 tparm = tsubst (TREE_TYPE (parm), targs, 0, NULL_TREE);
20661 if (tree a = type_uses_auto (tparm))
20663 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
20664 if (tparm == error_mark_node)
20665 return 1;
20668 if (!TREE_TYPE (arg))
20669 /* Template-parameter dependent expression. Just accept it for now.
20670 It will later be processed in convert_template_argument. */
20672 else if (same_type_p (TREE_TYPE (arg), tparm))
20673 /* OK */;
20674 else if ((strict & UNIFY_ALLOW_INTEGER)
20675 && CP_INTEGRAL_TYPE_P (tparm))
20676 /* Convert the ARG to the type of PARM; the deduced non-type
20677 template argument must exactly match the types of the
20678 corresponding parameter. */
20679 arg = fold (build_nop (tparm, arg));
20680 else if (uses_template_parms (tparm))
20681 /* We haven't deduced the type of this parameter yet. Try again
20682 later. */
20683 return unify_success (explain_p);
20684 else
20685 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
20687 /* If ARG is a parameter pack or an expansion, we cannot unify
20688 against it unless PARM is also a parameter pack. */
20689 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
20690 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
20691 return unify_parameter_pack_mismatch (explain_p, parm, arg);
20694 bool removed_attr = false;
20695 arg = strip_typedefs_expr (arg, &removed_attr);
20697 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
20698 return unify_success (explain_p);
20700 case PTRMEM_CST:
20702 /* A pointer-to-member constant can be unified only with
20703 another constant. */
20704 if (TREE_CODE (arg) != PTRMEM_CST)
20705 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
20707 /* Just unify the class member. It would be useless (and possibly
20708 wrong, depending on the strict flags) to unify also
20709 PTRMEM_CST_CLASS, because we want to be sure that both parm and
20710 arg refer to the same variable, even if through different
20711 classes. For instance:
20713 struct A { int x; };
20714 struct B : A { };
20716 Unification of &A::x and &B::x must succeed. */
20717 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
20718 PTRMEM_CST_MEMBER (arg), strict, explain_p);
20721 case POINTER_TYPE:
20723 if (!TYPE_PTR_P (arg))
20724 return unify_type_mismatch (explain_p, parm, arg);
20726 /* [temp.deduct.call]
20728 A can be another pointer or pointer to member type that can
20729 be converted to the deduced A via a qualification
20730 conversion (_conv.qual_).
20732 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
20733 This will allow for additional cv-qualification of the
20734 pointed-to types if appropriate. */
20736 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
20737 /* The derived-to-base conversion only persists through one
20738 level of pointers. */
20739 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
20741 return unify (tparms, targs, TREE_TYPE (parm),
20742 TREE_TYPE (arg), strict, explain_p);
20745 case REFERENCE_TYPE:
20746 if (TREE_CODE (arg) != REFERENCE_TYPE)
20747 return unify_type_mismatch (explain_p, parm, arg);
20748 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
20749 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
20751 case ARRAY_TYPE:
20752 if (TREE_CODE (arg) != ARRAY_TYPE)
20753 return unify_type_mismatch (explain_p, parm, arg);
20754 if ((TYPE_DOMAIN (parm) == NULL_TREE)
20755 != (TYPE_DOMAIN (arg) == NULL_TREE))
20756 return unify_type_mismatch (explain_p, parm, arg);
20757 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
20758 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
20759 if (TYPE_DOMAIN (parm) != NULL_TREE)
20760 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
20761 TYPE_DOMAIN (arg), explain_p);
20762 return unify_success (explain_p);
20764 case REAL_TYPE:
20765 case COMPLEX_TYPE:
20766 case VECTOR_TYPE:
20767 case INTEGER_TYPE:
20768 case BOOLEAN_TYPE:
20769 case ENUMERAL_TYPE:
20770 case VOID_TYPE:
20771 case NULLPTR_TYPE:
20772 if (TREE_CODE (arg) != TREE_CODE (parm))
20773 return unify_type_mismatch (explain_p, parm, arg);
20775 /* We have already checked cv-qualification at the top of the
20776 function. */
20777 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
20778 return unify_type_mismatch (explain_p, parm, arg);
20780 /* As far as unification is concerned, this wins. Later checks
20781 will invalidate it if necessary. */
20782 return unify_success (explain_p);
20784 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
20785 /* Type INTEGER_CST can come from ordinary constant template args. */
20786 case INTEGER_CST:
20787 while (CONVERT_EXPR_P (arg))
20788 arg = TREE_OPERAND (arg, 0);
20790 if (TREE_CODE (arg) != INTEGER_CST)
20791 return unify_template_argument_mismatch (explain_p, parm, arg);
20792 return (tree_int_cst_equal (parm, arg)
20793 ? unify_success (explain_p)
20794 : unify_template_argument_mismatch (explain_p, parm, arg));
20796 case TREE_VEC:
20798 int i, len, argslen;
20799 int parm_variadic_p = 0;
20801 if (TREE_CODE (arg) != TREE_VEC)
20802 return unify_template_argument_mismatch (explain_p, parm, arg);
20804 len = TREE_VEC_LENGTH (parm);
20805 argslen = TREE_VEC_LENGTH (arg);
20807 /* Check for pack expansions in the parameters. */
20808 for (i = 0; i < len; ++i)
20810 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
20812 if (i == len - 1)
20813 /* We can unify against something with a trailing
20814 parameter pack. */
20815 parm_variadic_p = 1;
20816 else
20817 /* [temp.deduct.type]/9: If the template argument list of
20818 P contains a pack expansion that is not the last
20819 template argument, the entire template argument list
20820 is a non-deduced context. */
20821 return unify_success (explain_p);
20825 /* If we don't have enough arguments to satisfy the parameters
20826 (not counting the pack expression at the end), or we have
20827 too many arguments for a parameter list that doesn't end in
20828 a pack expression, we can't unify. */
20829 if (parm_variadic_p
20830 ? argslen < len - parm_variadic_p
20831 : argslen != len)
20832 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
20834 /* Unify all of the parameters that precede the (optional)
20835 pack expression. */
20836 for (i = 0; i < len - parm_variadic_p; ++i)
20838 RECUR_AND_CHECK_FAILURE (tparms, targs,
20839 TREE_VEC_ELT (parm, i),
20840 TREE_VEC_ELT (arg, i),
20841 UNIFY_ALLOW_NONE, explain_p);
20843 if (parm_variadic_p)
20844 return unify_pack_expansion (tparms, targs, parm, arg,
20845 DEDUCE_EXACT,
20846 /*subr=*/true, explain_p);
20847 return unify_success (explain_p);
20850 case RECORD_TYPE:
20851 case UNION_TYPE:
20852 if (TREE_CODE (arg) != TREE_CODE (parm))
20853 return unify_type_mismatch (explain_p, parm, arg);
20855 if (TYPE_PTRMEMFUNC_P (parm))
20857 if (!TYPE_PTRMEMFUNC_P (arg))
20858 return unify_type_mismatch (explain_p, parm, arg);
20860 return unify (tparms, targs,
20861 TYPE_PTRMEMFUNC_FN_TYPE (parm),
20862 TYPE_PTRMEMFUNC_FN_TYPE (arg),
20863 strict, explain_p);
20865 else if (TYPE_PTRMEMFUNC_P (arg))
20866 return unify_type_mismatch (explain_p, parm, arg);
20868 if (CLASSTYPE_TEMPLATE_INFO (parm))
20870 tree t = NULL_TREE;
20872 if (strict_in & UNIFY_ALLOW_DERIVED)
20874 /* First, we try to unify the PARM and ARG directly. */
20875 t = try_class_unification (tparms, targs,
20876 parm, arg, explain_p);
20878 if (!t)
20880 /* Fallback to the special case allowed in
20881 [temp.deduct.call]:
20883 If P is a class, and P has the form
20884 template-id, then A can be a derived class of
20885 the deduced A. Likewise, if P is a pointer to
20886 a class of the form template-id, A can be a
20887 pointer to a derived class pointed to by the
20888 deduced A. */
20889 enum template_base_result r;
20890 r = get_template_base (tparms, targs, parm, arg,
20891 explain_p, &t);
20893 if (!t)
20895 /* Don't give the derived diagnostic if we're
20896 already dealing with the same template. */
20897 bool same_template
20898 = (CLASSTYPE_TEMPLATE_INFO (arg)
20899 && (CLASSTYPE_TI_TEMPLATE (parm)
20900 == CLASSTYPE_TI_TEMPLATE (arg)));
20901 return unify_no_common_base (explain_p && !same_template,
20902 r, parm, arg);
20906 else if (CLASSTYPE_TEMPLATE_INFO (arg)
20907 && (CLASSTYPE_TI_TEMPLATE (parm)
20908 == CLASSTYPE_TI_TEMPLATE (arg)))
20909 /* Perhaps PARM is something like S<U> and ARG is S<int>.
20910 Then, we should unify `int' and `U'. */
20911 t = arg;
20912 else
20913 /* There's no chance of unification succeeding. */
20914 return unify_type_mismatch (explain_p, parm, arg);
20916 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
20917 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
20919 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
20920 return unify_type_mismatch (explain_p, parm, arg);
20921 return unify_success (explain_p);
20923 case METHOD_TYPE:
20924 case FUNCTION_TYPE:
20926 unsigned int nargs;
20927 tree *args;
20928 tree a;
20929 unsigned int i;
20931 if (TREE_CODE (arg) != TREE_CODE (parm))
20932 return unify_type_mismatch (explain_p, parm, arg);
20934 /* CV qualifications for methods can never be deduced, they must
20935 match exactly. We need to check them explicitly here,
20936 because type_unification_real treats them as any other
20937 cv-qualified parameter. */
20938 if (TREE_CODE (parm) == METHOD_TYPE
20939 && (!check_cv_quals_for_unify
20940 (UNIFY_ALLOW_NONE,
20941 class_of_this_parm (arg),
20942 class_of_this_parm (parm))))
20943 return unify_cv_qual_mismatch (explain_p, parm, arg);
20944 if (TREE_CODE (arg) == FUNCTION_TYPE
20945 && type_memfn_quals (parm) != type_memfn_quals (arg))
20946 return unify_cv_qual_mismatch (explain_p, parm, arg);
20947 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
20948 return unify_type_mismatch (explain_p, parm, arg);
20950 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
20951 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
20953 nargs = list_length (TYPE_ARG_TYPES (arg));
20954 args = XALLOCAVEC (tree, nargs);
20955 for (a = TYPE_ARG_TYPES (arg), i = 0;
20956 a != NULL_TREE && a != void_list_node;
20957 a = TREE_CHAIN (a), ++i)
20958 args[i] = TREE_VALUE (a);
20959 nargs = i;
20961 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
20962 args, nargs, 1, DEDUCE_EXACT,
20963 LOOKUP_NORMAL, NULL, explain_p))
20964 return 1;
20966 if (flag_noexcept_type)
20968 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
20969 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
20970 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
20971 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
20972 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
20973 && uses_template_parms (TREE_PURPOSE (pspec)))
20974 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
20975 TREE_PURPOSE (aspec),
20976 UNIFY_ALLOW_NONE, explain_p);
20977 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
20978 return unify_type_mismatch (explain_p, parm, arg);
20981 return 0;
20984 case OFFSET_TYPE:
20985 /* Unify a pointer to member with a pointer to member function, which
20986 deduces the type of the member as a function type. */
20987 if (TYPE_PTRMEMFUNC_P (arg))
20989 /* Check top-level cv qualifiers */
20990 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
20991 return unify_cv_qual_mismatch (explain_p, parm, arg);
20993 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
20994 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
20995 UNIFY_ALLOW_NONE, explain_p);
20997 /* Determine the type of the function we are unifying against. */
20998 tree fntype = static_fn_type (arg);
21000 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
21003 if (TREE_CODE (arg) != OFFSET_TYPE)
21004 return unify_type_mismatch (explain_p, parm, arg);
21005 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21006 TYPE_OFFSET_BASETYPE (arg),
21007 UNIFY_ALLOW_NONE, explain_p);
21008 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21009 strict, explain_p);
21011 case CONST_DECL:
21012 if (DECL_TEMPLATE_PARM_P (parm))
21013 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
21014 if (arg != scalar_constant_value (parm))
21015 return unify_template_argument_mismatch (explain_p, parm, arg);
21016 return unify_success (explain_p);
21018 case FIELD_DECL:
21019 case TEMPLATE_DECL:
21020 /* Matched cases are handled by the ARG == PARM test above. */
21021 return unify_template_argument_mismatch (explain_p, parm, arg);
21023 case VAR_DECL:
21024 /* We might get a variable as a non-type template argument in parm if the
21025 corresponding parameter is type-dependent. Make any necessary
21026 adjustments based on whether arg is a reference. */
21027 if (CONSTANT_CLASS_P (arg))
21028 parm = fold_non_dependent_expr (parm);
21029 else if (REFERENCE_REF_P (arg))
21031 tree sub = TREE_OPERAND (arg, 0);
21032 STRIP_NOPS (sub);
21033 if (TREE_CODE (sub) == ADDR_EXPR)
21034 arg = TREE_OPERAND (sub, 0);
21036 /* Now use the normal expression code to check whether they match. */
21037 goto expr;
21039 case TYPE_ARGUMENT_PACK:
21040 case NONTYPE_ARGUMENT_PACK:
21041 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
21042 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
21044 case TYPEOF_TYPE:
21045 case DECLTYPE_TYPE:
21046 case UNDERLYING_TYPE:
21047 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
21048 or UNDERLYING_TYPE nodes. */
21049 return unify_success (explain_p);
21051 case ERROR_MARK:
21052 /* Unification fails if we hit an error node. */
21053 return unify_invalid (explain_p);
21055 case INDIRECT_REF:
21056 if (REFERENCE_REF_P (parm))
21058 bool pexp = PACK_EXPANSION_P (arg);
21059 if (pexp)
21060 arg = PACK_EXPANSION_PATTERN (arg);
21061 if (REFERENCE_REF_P (arg))
21062 arg = TREE_OPERAND (arg, 0);
21063 if (pexp)
21064 arg = make_pack_expansion (arg);
21065 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
21066 strict, explain_p);
21068 /* FALLTHRU */
21070 default:
21071 /* An unresolved overload is a nondeduced context. */
21072 if (is_overloaded_fn (parm) || type_unknown_p (parm))
21073 return unify_success (explain_p);
21074 gcc_assert (EXPR_P (parm) || TREE_CODE (parm) == TRAIT_EXPR);
21075 expr:
21076 /* We must be looking at an expression. This can happen with
21077 something like:
21079 template <int I>
21080 void foo(S<I>, S<I + 2>);
21082 This is a "nondeduced context":
21084 [deduct.type]
21086 The nondeduced contexts are:
21088 --A type that is a template-id in which one or more of
21089 the template-arguments is an expression that references
21090 a template-parameter.
21092 In these cases, we assume deduction succeeded, but don't
21093 actually infer any unifications. */
21095 if (!uses_template_parms (parm)
21096 && !template_args_equal (parm, arg))
21097 return unify_expression_unequal (explain_p, parm, arg);
21098 else
21099 return unify_success (explain_p);
21102 #undef RECUR_AND_CHECK_FAILURE
21104 /* Note that DECL can be defined in this translation unit, if
21105 required. */
21107 static void
21108 mark_definable (tree decl)
21110 tree clone;
21111 DECL_NOT_REALLY_EXTERN (decl) = 1;
21112 FOR_EACH_CLONE (clone, decl)
21113 DECL_NOT_REALLY_EXTERN (clone) = 1;
21116 /* Called if RESULT is explicitly instantiated, or is a member of an
21117 explicitly instantiated class. */
21119 void
21120 mark_decl_instantiated (tree result, int extern_p)
21122 SET_DECL_EXPLICIT_INSTANTIATION (result);
21124 /* If this entity has already been written out, it's too late to
21125 make any modifications. */
21126 if (TREE_ASM_WRITTEN (result))
21127 return;
21129 /* For anonymous namespace we don't need to do anything. */
21130 if (decl_anon_ns_mem_p (result))
21132 gcc_assert (!TREE_PUBLIC (result));
21133 return;
21136 if (TREE_CODE (result) != FUNCTION_DECL)
21137 /* The TREE_PUBLIC flag for function declarations will have been
21138 set correctly by tsubst. */
21139 TREE_PUBLIC (result) = 1;
21141 /* This might have been set by an earlier implicit instantiation. */
21142 DECL_COMDAT (result) = 0;
21144 if (extern_p)
21145 DECL_NOT_REALLY_EXTERN (result) = 0;
21146 else
21148 mark_definable (result);
21149 mark_needed (result);
21150 /* Always make artificials weak. */
21151 if (DECL_ARTIFICIAL (result) && flag_weak)
21152 comdat_linkage (result);
21153 /* For WIN32 we also want to put explicit instantiations in
21154 linkonce sections. */
21155 else if (TREE_PUBLIC (result))
21156 maybe_make_one_only (result);
21159 /* If EXTERN_P, then this function will not be emitted -- unless
21160 followed by an explicit instantiation, at which point its linkage
21161 will be adjusted. If !EXTERN_P, then this function will be
21162 emitted here. In neither circumstance do we want
21163 import_export_decl to adjust the linkage. */
21164 DECL_INTERFACE_KNOWN (result) = 1;
21167 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
21168 important template arguments. If any are missing, we check whether
21169 they're important by using error_mark_node for substituting into any
21170 args that were used for partial ordering (the ones between ARGS and END)
21171 and seeing if it bubbles up. */
21173 static bool
21174 check_undeduced_parms (tree targs, tree args, tree end)
21176 bool found = false;
21177 int i;
21178 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
21179 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
21181 found = true;
21182 TREE_VEC_ELT (targs, i) = error_mark_node;
21184 if (found)
21186 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
21187 if (substed == error_mark_node)
21188 return true;
21190 return false;
21193 /* Given two function templates PAT1 and PAT2, return:
21195 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
21196 -1 if PAT2 is more specialized than PAT1.
21197 0 if neither is more specialized.
21199 LEN indicates the number of parameters we should consider
21200 (defaulted parameters should not be considered).
21202 The 1998 std underspecified function template partial ordering, and
21203 DR214 addresses the issue. We take pairs of arguments, one from
21204 each of the templates, and deduce them against each other. One of
21205 the templates will be more specialized if all the *other*
21206 template's arguments deduce against its arguments and at least one
21207 of its arguments *does* *not* deduce against the other template's
21208 corresponding argument. Deduction is done as for class templates.
21209 The arguments used in deduction have reference and top level cv
21210 qualifiers removed. Iff both arguments were originally reference
21211 types *and* deduction succeeds in both directions, an lvalue reference
21212 wins against an rvalue reference and otherwise the template
21213 with the more cv-qualified argument wins for that pairing (if
21214 neither is more cv-qualified, they both are equal). Unlike regular
21215 deduction, after all the arguments have been deduced in this way,
21216 we do *not* verify the deduced template argument values can be
21217 substituted into non-deduced contexts.
21219 The logic can be a bit confusing here, because we look at deduce1 and
21220 targs1 to see if pat2 is at least as specialized, and vice versa; if we
21221 can find template arguments for pat1 to make arg1 look like arg2, that
21222 means that arg2 is at least as specialized as arg1. */
21225 more_specialized_fn (tree pat1, tree pat2, int len)
21227 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
21228 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
21229 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
21230 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
21231 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
21232 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
21233 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
21234 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
21235 tree origs1, origs2;
21236 bool lose1 = false;
21237 bool lose2 = false;
21239 /* Remove the this parameter from non-static member functions. If
21240 one is a non-static member function and the other is not a static
21241 member function, remove the first parameter from that function
21242 also. This situation occurs for operator functions where we
21243 locate both a member function (with this pointer) and non-member
21244 operator (with explicit first operand). */
21245 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
21247 len--; /* LEN is the number of significant arguments for DECL1 */
21248 args1 = TREE_CHAIN (args1);
21249 if (!DECL_STATIC_FUNCTION_P (decl2))
21250 args2 = TREE_CHAIN (args2);
21252 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
21254 args2 = TREE_CHAIN (args2);
21255 if (!DECL_STATIC_FUNCTION_P (decl1))
21257 len--;
21258 args1 = TREE_CHAIN (args1);
21262 /* If only one is a conversion operator, they are unordered. */
21263 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
21264 return 0;
21266 /* Consider the return type for a conversion function */
21267 if (DECL_CONV_FN_P (decl1))
21269 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
21270 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
21271 len++;
21274 processing_template_decl++;
21276 origs1 = args1;
21277 origs2 = args2;
21279 while (len--
21280 /* Stop when an ellipsis is seen. */
21281 && args1 != NULL_TREE && args2 != NULL_TREE)
21283 tree arg1 = TREE_VALUE (args1);
21284 tree arg2 = TREE_VALUE (args2);
21285 int deduce1, deduce2;
21286 int quals1 = -1;
21287 int quals2 = -1;
21288 int ref1 = 0;
21289 int ref2 = 0;
21291 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21292 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21294 /* When both arguments are pack expansions, we need only
21295 unify the patterns themselves. */
21296 arg1 = PACK_EXPANSION_PATTERN (arg1);
21297 arg2 = PACK_EXPANSION_PATTERN (arg2);
21299 /* This is the last comparison we need to do. */
21300 len = 0;
21303 /* DR 1847: If a particular P contains no template-parameters that
21304 participate in template argument deduction, that P is not used to
21305 determine the ordering. */
21306 if (!uses_deducible_template_parms (arg1)
21307 && !uses_deducible_template_parms (arg2))
21308 goto next;
21310 if (TREE_CODE (arg1) == REFERENCE_TYPE)
21312 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
21313 arg1 = TREE_TYPE (arg1);
21314 quals1 = cp_type_quals (arg1);
21317 if (TREE_CODE (arg2) == REFERENCE_TYPE)
21319 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
21320 arg2 = TREE_TYPE (arg2);
21321 quals2 = cp_type_quals (arg2);
21324 arg1 = TYPE_MAIN_VARIANT (arg1);
21325 arg2 = TYPE_MAIN_VARIANT (arg2);
21327 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
21329 int i, len2 = remaining_arguments (args2);
21330 tree parmvec = make_tree_vec (1);
21331 tree argvec = make_tree_vec (len2);
21332 tree ta = args2;
21334 /* Setup the parameter vector, which contains only ARG1. */
21335 TREE_VEC_ELT (parmvec, 0) = arg1;
21337 /* Setup the argument vector, which contains the remaining
21338 arguments. */
21339 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
21340 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21342 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
21343 argvec, DEDUCE_EXACT,
21344 /*subr=*/true, /*explain_p=*/false)
21345 == 0);
21347 /* We cannot deduce in the other direction, because ARG1 is
21348 a pack expansion but ARG2 is not. */
21349 deduce2 = 0;
21351 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21353 int i, len1 = remaining_arguments (args1);
21354 tree parmvec = make_tree_vec (1);
21355 tree argvec = make_tree_vec (len1);
21356 tree ta = args1;
21358 /* Setup the parameter vector, which contains only ARG1. */
21359 TREE_VEC_ELT (parmvec, 0) = arg2;
21361 /* Setup the argument vector, which contains the remaining
21362 arguments. */
21363 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
21364 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21366 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
21367 argvec, DEDUCE_EXACT,
21368 /*subr=*/true, /*explain_p=*/false)
21369 == 0);
21371 /* We cannot deduce in the other direction, because ARG2 is
21372 a pack expansion but ARG1 is not.*/
21373 deduce1 = 0;
21376 else
21378 /* The normal case, where neither argument is a pack
21379 expansion. */
21380 deduce1 = (unify (tparms1, targs1, arg1, arg2,
21381 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21382 == 0);
21383 deduce2 = (unify (tparms2, targs2, arg2, arg1,
21384 UNIFY_ALLOW_NONE, /*explain_p=*/false)
21385 == 0);
21388 /* If we couldn't deduce arguments for tparms1 to make arg1 match
21389 arg2, then arg2 is not as specialized as arg1. */
21390 if (!deduce1)
21391 lose2 = true;
21392 if (!deduce2)
21393 lose1 = true;
21395 /* "If, for a given type, deduction succeeds in both directions
21396 (i.e., the types are identical after the transformations above)
21397 and both P and A were reference types (before being replaced with
21398 the type referred to above):
21399 - if the type from the argument template was an lvalue reference and
21400 the type from the parameter template was not, the argument type is
21401 considered to be more specialized than the other; otherwise,
21402 - if the type from the argument template is more cv-qualified
21403 than the type from the parameter template (as described above),
21404 the argument type is considered to be more specialized than the other;
21405 otherwise,
21406 - neither type is more specialized than the other." */
21408 if (deduce1 && deduce2)
21410 if (ref1 && ref2 && ref1 != ref2)
21412 if (ref1 > ref2)
21413 lose1 = true;
21414 else
21415 lose2 = true;
21417 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
21419 if ((quals1 & quals2) == quals2)
21420 lose2 = true;
21421 if ((quals1 & quals2) == quals1)
21422 lose1 = true;
21426 if (lose1 && lose2)
21427 /* We've failed to deduce something in either direction.
21428 These must be unordered. */
21429 break;
21431 next:
21433 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21434 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21435 /* We have already processed all of the arguments in our
21436 handing of the pack expansion type. */
21437 len = 0;
21439 args1 = TREE_CHAIN (args1);
21440 args2 = TREE_CHAIN (args2);
21443 /* "In most cases, all template parameters must have values in order for
21444 deduction to succeed, but for partial ordering purposes a template
21445 parameter may remain without a value provided it is not used in the
21446 types being used for partial ordering."
21448 Thus, if we are missing any of the targs1 we need to substitute into
21449 origs1, then pat2 is not as specialized as pat1. This can happen when
21450 there is a nondeduced context. */
21451 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
21452 lose2 = true;
21453 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
21454 lose1 = true;
21456 processing_template_decl--;
21458 /* If both deductions succeed, the partial ordering selects the more
21459 constrained template. */
21460 if (!lose1 && !lose2)
21462 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
21463 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
21464 lose1 = !subsumes_constraints (c1, c2);
21465 lose2 = !subsumes_constraints (c2, c1);
21468 /* All things being equal, if the next argument is a pack expansion
21469 for one function but not for the other, prefer the
21470 non-variadic function. FIXME this is bogus; see c++/41958. */
21471 if (lose1 == lose2
21472 && args1 && TREE_VALUE (args1)
21473 && args2 && TREE_VALUE (args2))
21475 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
21476 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
21479 if (lose1 == lose2)
21480 return 0;
21481 else if (!lose1)
21482 return 1;
21483 else
21484 return -1;
21487 /* Determine which of two partial specializations of TMPL is more
21488 specialized.
21490 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
21491 to the first partial specialization. The TREE_PURPOSE is the
21492 innermost set of template parameters for the partial
21493 specialization. PAT2 is similar, but for the second template.
21495 Return 1 if the first partial specialization is more specialized;
21496 -1 if the second is more specialized; 0 if neither is more
21497 specialized.
21499 See [temp.class.order] for information about determining which of
21500 two templates is more specialized. */
21502 static int
21503 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
21505 tree targs;
21506 int winner = 0;
21507 bool any_deductions = false;
21509 tree tmpl1 = TREE_VALUE (pat1);
21510 tree tmpl2 = TREE_VALUE (pat2);
21511 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
21512 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
21514 /* Just like what happens for functions, if we are ordering between
21515 different template specializations, we may encounter dependent
21516 types in the arguments, and we need our dependency check functions
21517 to behave correctly. */
21518 ++processing_template_decl;
21519 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
21520 if (targs)
21522 --winner;
21523 any_deductions = true;
21526 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
21527 if (targs)
21529 ++winner;
21530 any_deductions = true;
21532 --processing_template_decl;
21534 /* If both deductions succeed, the partial ordering selects the more
21535 constrained template. */
21536 if (!winner && any_deductions)
21537 return more_constrained (tmpl1, tmpl2);
21539 /* In the case of a tie where at least one of the templates
21540 has a parameter pack at the end, the template with the most
21541 non-packed parameters wins. */
21542 if (winner == 0
21543 && any_deductions
21544 && (template_args_variadic_p (TREE_PURPOSE (pat1))
21545 || template_args_variadic_p (TREE_PURPOSE (pat2))))
21547 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
21548 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
21549 int len1 = TREE_VEC_LENGTH (args1);
21550 int len2 = TREE_VEC_LENGTH (args2);
21552 /* We don't count the pack expansion at the end. */
21553 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
21554 --len1;
21555 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
21556 --len2;
21558 if (len1 > len2)
21559 return 1;
21560 else if (len1 < len2)
21561 return -1;
21564 return winner;
21567 /* Return the template arguments that will produce the function signature
21568 DECL from the function template FN, with the explicit template
21569 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
21570 also match. Return NULL_TREE if no satisfactory arguments could be
21571 found. */
21573 static tree
21574 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
21576 int ntparms = DECL_NTPARMS (fn);
21577 tree targs = make_tree_vec (ntparms);
21578 tree decl_type = TREE_TYPE (decl);
21579 tree decl_arg_types;
21580 tree *args;
21581 unsigned int nargs, ix;
21582 tree arg;
21584 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
21586 /* Never do unification on the 'this' parameter. */
21587 decl_arg_types = skip_artificial_parms_for (decl,
21588 TYPE_ARG_TYPES (decl_type));
21590 nargs = list_length (decl_arg_types);
21591 args = XALLOCAVEC (tree, nargs);
21592 for (arg = decl_arg_types, ix = 0;
21593 arg != NULL_TREE && arg != void_list_node;
21594 arg = TREE_CHAIN (arg), ++ix)
21595 args[ix] = TREE_VALUE (arg);
21597 if (fn_type_unification (fn, explicit_args, targs,
21598 args, ix,
21599 (check_rettype || DECL_CONV_FN_P (fn)
21600 ? TREE_TYPE (decl_type) : NULL_TREE),
21601 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
21602 /*decltype*/false)
21603 == error_mark_node)
21604 return NULL_TREE;
21606 return targs;
21609 /* Return the innermost template arguments that, when applied to a partial
21610 specialization SPEC_TMPL of TMPL, yield the ARGS.
21612 For example, suppose we have:
21614 template <class T, class U> struct S {};
21615 template <class T> struct S<T*, int> {};
21617 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
21618 partial specialization and the ARGS will be {double*, int}. The resulting
21619 vector will be {double}, indicating that `T' is bound to `double'. */
21621 static tree
21622 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
21624 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
21625 tree spec_args
21626 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
21627 int i, ntparms = TREE_VEC_LENGTH (tparms);
21628 tree deduced_args;
21629 tree innermost_deduced_args;
21631 innermost_deduced_args = make_tree_vec (ntparms);
21632 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
21634 deduced_args = copy_node (args);
21635 SET_TMPL_ARGS_LEVEL (deduced_args,
21636 TMPL_ARGS_DEPTH (deduced_args),
21637 innermost_deduced_args);
21639 else
21640 deduced_args = innermost_deduced_args;
21642 bool tried_array_deduction = (cxx_dialect < cxx1z);
21643 again:
21644 if (unify (tparms, deduced_args,
21645 INNERMOST_TEMPLATE_ARGS (spec_args),
21646 INNERMOST_TEMPLATE_ARGS (args),
21647 UNIFY_ALLOW_NONE, /*explain_p=*/false))
21648 return NULL_TREE;
21650 for (i = 0; i < ntparms; ++i)
21651 if (! TREE_VEC_ELT (innermost_deduced_args, i))
21653 if (!tried_array_deduction)
21655 try_array_deduction (tparms, innermost_deduced_args,
21656 INNERMOST_TEMPLATE_ARGS (spec_args));
21657 tried_array_deduction = true;
21658 if (TREE_VEC_ELT (innermost_deduced_args, i))
21659 goto again;
21661 return NULL_TREE;
21664 tree tinst = build_tree_list (spec_tmpl, deduced_args);
21665 if (!push_tinst_level (tinst))
21667 excessive_deduction_depth = true;
21668 return NULL_TREE;
21671 /* Verify that nondeduced template arguments agree with the type
21672 obtained from argument deduction.
21674 For example:
21676 struct A { typedef int X; };
21677 template <class T, class U> struct C {};
21678 template <class T> struct C<T, typename T::X> {};
21680 Then with the instantiation `C<A, int>', we can deduce that
21681 `T' is `A' but unify () does not check whether `typename T::X'
21682 is `int'. */
21683 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
21684 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
21685 spec_args, tmpl,
21686 tf_none, false, false);
21688 pop_tinst_level ();
21690 if (spec_args == error_mark_node
21691 /* We only need to check the innermost arguments; the other
21692 arguments will always agree. */
21693 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
21694 INNERMOST_TEMPLATE_ARGS (args)))
21695 return NULL_TREE;
21697 /* Now that we have bindings for all of the template arguments,
21698 ensure that the arguments deduced for the template template
21699 parameters have compatible template parameter lists. See the use
21700 of template_template_parm_bindings_ok_p in fn_type_unification
21701 for more information. */
21702 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
21703 return NULL_TREE;
21705 return deduced_args;
21708 // Compare two function templates T1 and T2 by deducing bindings
21709 // from one against the other. If both deductions succeed, compare
21710 // constraints to see which is more constrained.
21711 static int
21712 more_specialized_inst (tree t1, tree t2)
21714 int fate = 0;
21715 int count = 0;
21717 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
21719 --fate;
21720 ++count;
21723 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
21725 ++fate;
21726 ++count;
21729 // If both deductions succeed, then one may be more constrained.
21730 if (count == 2 && fate == 0)
21731 fate = more_constrained (t1, t2);
21733 return fate;
21736 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
21737 Return the TREE_LIST node with the most specialized template, if
21738 any. If there is no most specialized template, the error_mark_node
21739 is returned.
21741 Note that this function does not look at, or modify, the
21742 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
21743 returned is one of the elements of INSTANTIATIONS, callers may
21744 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
21745 and retrieve it from the value returned. */
21747 tree
21748 most_specialized_instantiation (tree templates)
21750 tree fn, champ;
21752 ++processing_template_decl;
21754 champ = templates;
21755 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
21757 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
21758 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
21759 if (fate == -1)
21760 champ = fn;
21761 else if (!fate)
21763 /* Equally specialized, move to next function. If there
21764 is no next function, nothing's most specialized. */
21765 fn = TREE_CHAIN (fn);
21766 champ = fn;
21767 if (!fn)
21768 break;
21772 if (champ)
21773 /* Now verify that champ is better than everything earlier in the
21774 instantiation list. */
21775 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
21776 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
21778 champ = NULL_TREE;
21779 break;
21783 processing_template_decl--;
21785 if (!champ)
21786 return error_mark_node;
21788 return champ;
21791 /* If DECL is a specialization of some template, return the most
21792 general such template. Otherwise, returns NULL_TREE.
21794 For example, given:
21796 template <class T> struct S { template <class U> void f(U); };
21798 if TMPL is `template <class U> void S<int>::f(U)' this will return
21799 the full template. This function will not trace past partial
21800 specializations, however. For example, given in addition:
21802 template <class T> struct S<T*> { template <class U> void f(U); };
21804 if TMPL is `template <class U> void S<int*>::f(U)' this will return
21805 `template <class T> template <class U> S<T*>::f(U)'. */
21807 tree
21808 most_general_template (tree decl)
21810 if (TREE_CODE (decl) != TEMPLATE_DECL)
21812 if (tree tinfo = get_template_info (decl))
21813 decl = TI_TEMPLATE (tinfo);
21814 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
21815 template friend, or a FIELD_DECL for a capture pack. */
21816 if (TREE_CODE (decl) != TEMPLATE_DECL)
21817 return NULL_TREE;
21820 /* Look for more and more general templates. */
21821 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
21823 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
21824 (See cp-tree.h for details.) */
21825 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
21826 break;
21828 if (CLASS_TYPE_P (TREE_TYPE (decl))
21829 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
21830 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
21831 break;
21833 /* Stop if we run into an explicitly specialized class template. */
21834 if (!DECL_NAMESPACE_SCOPE_P (decl)
21835 && DECL_CONTEXT (decl)
21836 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
21837 break;
21839 decl = DECL_TI_TEMPLATE (decl);
21842 return decl;
21845 /* Return the most specialized of the template partial specializations
21846 which can produce TARGET, a specialization of some class or variable
21847 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
21848 a TEMPLATE_DECL node corresponding to the partial specialization, while
21849 the TREE_PURPOSE is the set of template arguments that must be
21850 substituted into the template pattern in order to generate TARGET.
21852 If the choice of partial specialization is ambiguous, a diagnostic
21853 is issued, and the error_mark_node is returned. If there are no
21854 partial specializations matching TARGET, then NULL_TREE is
21855 returned, indicating that the primary template should be used. */
21857 static tree
21858 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
21860 tree list = NULL_TREE;
21861 tree t;
21862 tree champ;
21863 int fate;
21864 bool ambiguous_p;
21865 tree outer_args = NULL_TREE;
21866 tree tmpl, args;
21868 if (TYPE_P (target))
21870 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
21871 tmpl = TI_TEMPLATE (tinfo);
21872 args = TI_ARGS (tinfo);
21874 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
21876 tmpl = TREE_OPERAND (target, 0);
21877 args = TREE_OPERAND (target, 1);
21879 else if (VAR_P (target))
21881 tree tinfo = DECL_TEMPLATE_INFO (target);
21882 tmpl = TI_TEMPLATE (tinfo);
21883 args = TI_ARGS (tinfo);
21885 else
21886 gcc_unreachable ();
21888 tree main_tmpl = most_general_template (tmpl);
21890 /* For determining which partial specialization to use, only the
21891 innermost args are interesting. */
21892 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
21894 outer_args = strip_innermost_template_args (args, 1);
21895 args = INNERMOST_TEMPLATE_ARGS (args);
21898 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
21900 tree spec_args;
21901 tree spec_tmpl = TREE_VALUE (t);
21903 if (outer_args)
21905 /* Substitute in the template args from the enclosing class. */
21906 ++processing_template_decl;
21907 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
21908 --processing_template_decl;
21911 if (spec_tmpl == error_mark_node)
21912 return error_mark_node;
21914 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
21915 if (spec_args)
21917 if (outer_args)
21918 spec_args = add_to_template_args (outer_args, spec_args);
21920 /* Keep the candidate only if the constraints are satisfied,
21921 or if we're not compiling with concepts. */
21922 if (!flag_concepts
21923 || constraints_satisfied_p (spec_tmpl, spec_args))
21925 list = tree_cons (spec_args, TREE_VALUE (t), list);
21926 TREE_TYPE (list) = TREE_TYPE (t);
21931 if (! list)
21932 return NULL_TREE;
21934 ambiguous_p = false;
21935 t = list;
21936 champ = t;
21937 t = TREE_CHAIN (t);
21938 for (; t; t = TREE_CHAIN (t))
21940 fate = more_specialized_partial_spec (tmpl, champ, t);
21941 if (fate == 1)
21943 else
21945 if (fate == 0)
21947 t = TREE_CHAIN (t);
21948 if (! t)
21950 ambiguous_p = true;
21951 break;
21954 champ = t;
21958 if (!ambiguous_p)
21959 for (t = list; t && t != champ; t = TREE_CHAIN (t))
21961 fate = more_specialized_partial_spec (tmpl, champ, t);
21962 if (fate != 1)
21964 ambiguous_p = true;
21965 break;
21969 if (ambiguous_p)
21971 const char *str;
21972 char *spaces = NULL;
21973 if (!(complain & tf_error))
21974 return error_mark_node;
21975 if (TYPE_P (target))
21976 error ("ambiguous template instantiation for %q#T", target);
21977 else
21978 error ("ambiguous template instantiation for %q#D", target);
21979 str = ngettext ("candidate is:", "candidates are:", list_length (list));
21980 for (t = list; t; t = TREE_CHAIN (t))
21982 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
21983 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
21984 "%s %#qS", spaces ? spaces : str, subst);
21985 spaces = spaces ? spaces : get_spaces (str);
21987 free (spaces);
21988 return error_mark_node;
21991 return champ;
21994 /* Explicitly instantiate DECL. */
21996 void
21997 do_decl_instantiation (tree decl, tree storage)
21999 tree result = NULL_TREE;
22000 int extern_p = 0;
22002 if (!decl || decl == error_mark_node)
22003 /* An error occurred, for which grokdeclarator has already issued
22004 an appropriate message. */
22005 return;
22006 else if (! DECL_LANG_SPECIFIC (decl))
22008 error ("explicit instantiation of non-template %q#D", decl);
22009 return;
22012 bool var_templ = (DECL_TEMPLATE_INFO (decl)
22013 && variable_template_p (DECL_TI_TEMPLATE (decl)));
22015 if (VAR_P (decl) && !var_templ)
22017 /* There is an asymmetry here in the way VAR_DECLs and
22018 FUNCTION_DECLs are handled by grokdeclarator. In the case of
22019 the latter, the DECL we get back will be marked as a
22020 template instantiation, and the appropriate
22021 DECL_TEMPLATE_INFO will be set up. This does not happen for
22022 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
22023 should handle VAR_DECLs as it currently handles
22024 FUNCTION_DECLs. */
22025 if (!DECL_CLASS_SCOPE_P (decl))
22027 error ("%qD is not a static data member of a class template", decl);
22028 return;
22030 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
22031 if (!result || !VAR_P (result))
22033 error ("no matching template for %qD found", decl);
22034 return;
22036 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
22038 error ("type %qT for explicit instantiation %qD does not match "
22039 "declared type %qT", TREE_TYPE (result), decl,
22040 TREE_TYPE (decl));
22041 return;
22044 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
22046 error ("explicit instantiation of %q#D", decl);
22047 return;
22049 else
22050 result = decl;
22052 /* Check for various error cases. Note that if the explicit
22053 instantiation is valid the RESULT will currently be marked as an
22054 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
22055 until we get here. */
22057 if (DECL_TEMPLATE_SPECIALIZATION (result))
22059 /* DR 259 [temp.spec].
22061 Both an explicit instantiation and a declaration of an explicit
22062 specialization shall not appear in a program unless the explicit
22063 instantiation follows a declaration of the explicit specialization.
22065 For a given set of template parameters, if an explicit
22066 instantiation of a template appears after a declaration of an
22067 explicit specialization for that template, the explicit
22068 instantiation has no effect. */
22069 return;
22071 else if (DECL_EXPLICIT_INSTANTIATION (result))
22073 /* [temp.spec]
22075 No program shall explicitly instantiate any template more
22076 than once.
22078 We check DECL_NOT_REALLY_EXTERN so as not to complain when
22079 the first instantiation was `extern' and the second is not,
22080 and EXTERN_P for the opposite case. */
22081 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
22082 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
22083 /* If an "extern" explicit instantiation follows an ordinary
22084 explicit instantiation, the template is instantiated. */
22085 if (extern_p)
22086 return;
22088 else if (!DECL_IMPLICIT_INSTANTIATION (result))
22090 error ("no matching template for %qD found", result);
22091 return;
22093 else if (!DECL_TEMPLATE_INFO (result))
22095 permerror (input_location, "explicit instantiation of non-template %q#D", result);
22096 return;
22099 if (storage == NULL_TREE)
22101 else if (storage == ridpointers[(int) RID_EXTERN])
22103 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
22104 pedwarn (input_location, OPT_Wpedantic,
22105 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
22106 "instantiations");
22107 extern_p = 1;
22109 else
22110 error ("storage class %qD applied to template instantiation", storage);
22112 check_explicit_instantiation_namespace (result);
22113 mark_decl_instantiated (result, extern_p);
22114 if (! extern_p)
22115 instantiate_decl (result, /*defer_ok=*/true,
22116 /*expl_inst_class_mem_p=*/false);
22119 static void
22120 mark_class_instantiated (tree t, int extern_p)
22122 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
22123 SET_CLASSTYPE_INTERFACE_KNOWN (t);
22124 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
22125 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
22126 if (! extern_p)
22128 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
22129 rest_of_type_compilation (t, 1);
22133 /* Called from do_type_instantiation through binding_table_foreach to
22134 do recursive instantiation for the type bound in ENTRY. */
22135 static void
22136 bt_instantiate_type_proc (binding_entry entry, void *data)
22138 tree storage = *(tree *) data;
22140 if (MAYBE_CLASS_TYPE_P (entry->type)
22141 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
22142 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
22145 /* Called from do_type_instantiation to instantiate a member
22146 (a member function or a static member variable) of an
22147 explicitly instantiated class template. */
22148 static void
22149 instantiate_class_member (tree decl, int extern_p)
22151 mark_decl_instantiated (decl, extern_p);
22152 if (! extern_p)
22153 instantiate_decl (decl, /*defer_ok=*/true,
22154 /*expl_inst_class_mem_p=*/true);
22157 /* Perform an explicit instantiation of template class T. STORAGE, if
22158 non-null, is the RID for extern, inline or static. COMPLAIN is
22159 nonzero if this is called from the parser, zero if called recursively,
22160 since the standard is unclear (as detailed below). */
22162 void
22163 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
22165 int extern_p = 0;
22166 int nomem_p = 0;
22167 int static_p = 0;
22168 int previous_instantiation_extern_p = 0;
22170 if (TREE_CODE (t) == TYPE_DECL)
22171 t = TREE_TYPE (t);
22173 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
22175 tree tmpl =
22176 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
22177 if (tmpl)
22178 error ("explicit instantiation of non-class template %qD", tmpl);
22179 else
22180 error ("explicit instantiation of non-template type %qT", t);
22181 return;
22184 complete_type (t);
22186 if (!COMPLETE_TYPE_P (t))
22188 if (complain & tf_error)
22189 error ("explicit instantiation of %q#T before definition of template",
22191 return;
22194 if (storage != NULL_TREE)
22196 if (!in_system_header_at (input_location))
22198 if (storage == ridpointers[(int) RID_EXTERN])
22200 if (cxx_dialect == cxx98)
22201 pedwarn (input_location, OPT_Wpedantic,
22202 "ISO C++ 1998 forbids the use of %<extern%> on "
22203 "explicit instantiations");
22205 else
22206 pedwarn (input_location, OPT_Wpedantic,
22207 "ISO C++ forbids the use of %qE"
22208 " on explicit instantiations", storage);
22211 if (storage == ridpointers[(int) RID_INLINE])
22212 nomem_p = 1;
22213 else if (storage == ridpointers[(int) RID_EXTERN])
22214 extern_p = 1;
22215 else if (storage == ridpointers[(int) RID_STATIC])
22216 static_p = 1;
22217 else
22219 error ("storage class %qD applied to template instantiation",
22220 storage);
22221 extern_p = 0;
22225 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
22227 /* DR 259 [temp.spec].
22229 Both an explicit instantiation and a declaration of an explicit
22230 specialization shall not appear in a program unless the explicit
22231 instantiation follows a declaration of the explicit specialization.
22233 For a given set of template parameters, if an explicit
22234 instantiation of a template appears after a declaration of an
22235 explicit specialization for that template, the explicit
22236 instantiation has no effect. */
22237 return;
22239 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
22241 /* [temp.spec]
22243 No program shall explicitly instantiate any template more
22244 than once.
22246 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
22247 instantiation was `extern'. If EXTERN_P then the second is.
22248 These cases are OK. */
22249 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
22251 if (!previous_instantiation_extern_p && !extern_p
22252 && (complain & tf_error))
22253 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
22255 /* If we've already instantiated the template, just return now. */
22256 if (!CLASSTYPE_INTERFACE_ONLY (t))
22257 return;
22260 check_explicit_instantiation_namespace (TYPE_NAME (t));
22261 mark_class_instantiated (t, extern_p);
22263 if (nomem_p)
22264 return;
22267 tree tmp;
22269 /* In contrast to implicit instantiation, where only the
22270 declarations, and not the definitions, of members are
22271 instantiated, we have here:
22273 [temp.explicit]
22275 The explicit instantiation of a class template specialization
22276 implies the instantiation of all of its members not
22277 previously explicitly specialized in the translation unit
22278 containing the explicit instantiation.
22280 Of course, we can't instantiate member template classes, since
22281 we don't have any arguments for them. Note that the standard
22282 is unclear on whether the instantiation of the members are
22283 *explicit* instantiations or not. However, the most natural
22284 interpretation is that it should be an explicit instantiation. */
22286 if (! static_p)
22287 for (tmp = TYPE_METHODS (t); tmp; tmp = DECL_CHAIN (tmp))
22288 if (TREE_CODE (tmp) == FUNCTION_DECL
22289 && DECL_TEMPLATE_INSTANTIATION (tmp)
22290 && user_provided_p (tmp))
22291 instantiate_class_member (tmp, extern_p);
22293 for (tmp = TYPE_FIELDS (t); tmp; tmp = DECL_CHAIN (tmp))
22294 if (VAR_P (tmp) && DECL_TEMPLATE_INSTANTIATION (tmp))
22295 instantiate_class_member (tmp, extern_p);
22297 if (CLASSTYPE_NESTED_UTDS (t))
22298 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
22299 bt_instantiate_type_proc, &storage);
22303 /* Given a function DECL, which is a specialization of TMPL, modify
22304 DECL to be a re-instantiation of TMPL with the same template
22305 arguments. TMPL should be the template into which tsubst'ing
22306 should occur for DECL, not the most general template.
22308 One reason for doing this is a scenario like this:
22310 template <class T>
22311 void f(const T&, int i);
22313 void g() { f(3, 7); }
22315 template <class T>
22316 void f(const T& t, const int i) { }
22318 Note that when the template is first instantiated, with
22319 instantiate_template, the resulting DECL will have no name for the
22320 first parameter, and the wrong type for the second. So, when we go
22321 to instantiate the DECL, we regenerate it. */
22323 static void
22324 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
22326 /* The arguments used to instantiate DECL, from the most general
22327 template. */
22328 tree code_pattern;
22330 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
22332 /* Make sure that we can see identifiers, and compute access
22333 correctly. */
22334 push_access_scope (decl);
22336 if (TREE_CODE (decl) == FUNCTION_DECL)
22338 tree decl_parm;
22339 tree pattern_parm;
22340 tree specs;
22341 int args_depth;
22342 int parms_depth;
22344 args_depth = TMPL_ARGS_DEPTH (args);
22345 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
22346 if (args_depth > parms_depth)
22347 args = get_innermost_template_args (args, parms_depth);
22349 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
22350 args, tf_error, NULL_TREE,
22351 /*defer_ok*/false);
22352 if (specs && specs != error_mark_node)
22353 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
22354 specs);
22356 /* Merge parameter declarations. */
22357 decl_parm = skip_artificial_parms_for (decl,
22358 DECL_ARGUMENTS (decl));
22359 pattern_parm
22360 = skip_artificial_parms_for (code_pattern,
22361 DECL_ARGUMENTS (code_pattern));
22362 while (decl_parm && !DECL_PACK_P (pattern_parm))
22364 tree parm_type;
22365 tree attributes;
22367 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22368 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
22369 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
22370 NULL_TREE);
22371 parm_type = type_decays_to (parm_type);
22372 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22373 TREE_TYPE (decl_parm) = parm_type;
22374 attributes = DECL_ATTRIBUTES (pattern_parm);
22375 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22377 DECL_ATTRIBUTES (decl_parm) = attributes;
22378 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22380 decl_parm = DECL_CHAIN (decl_parm);
22381 pattern_parm = DECL_CHAIN (pattern_parm);
22383 /* Merge any parameters that match with the function parameter
22384 pack. */
22385 if (pattern_parm && DECL_PACK_P (pattern_parm))
22387 int i, len;
22388 tree expanded_types;
22389 /* Expand the TYPE_PACK_EXPANSION that provides the types for
22390 the parameters in this function parameter pack. */
22391 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
22392 args, tf_error, NULL_TREE);
22393 len = TREE_VEC_LENGTH (expanded_types);
22394 for (i = 0; i < len; i++)
22396 tree parm_type;
22397 tree attributes;
22399 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22400 /* Rename the parameter to include the index. */
22401 DECL_NAME (decl_parm) =
22402 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
22403 parm_type = TREE_VEC_ELT (expanded_types, i);
22404 parm_type = type_decays_to (parm_type);
22405 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22406 TREE_TYPE (decl_parm) = parm_type;
22407 attributes = DECL_ATTRIBUTES (pattern_parm);
22408 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22410 DECL_ATTRIBUTES (decl_parm) = attributes;
22411 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22413 decl_parm = DECL_CHAIN (decl_parm);
22416 /* Merge additional specifiers from the CODE_PATTERN. */
22417 if (DECL_DECLARED_INLINE_P (code_pattern)
22418 && !DECL_DECLARED_INLINE_P (decl))
22419 DECL_DECLARED_INLINE_P (decl) = 1;
22421 else if (VAR_P (decl))
22423 DECL_INITIAL (decl) =
22424 tsubst_expr (DECL_INITIAL (code_pattern), args,
22425 tf_error, DECL_TI_TEMPLATE (decl),
22426 /*integral_constant_expression_p=*/false);
22427 if (VAR_HAD_UNKNOWN_BOUND (decl))
22428 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
22429 tf_error, DECL_TI_TEMPLATE (decl));
22431 else
22432 gcc_unreachable ();
22434 pop_access_scope (decl);
22437 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
22438 substituted to get DECL. */
22440 tree
22441 template_for_substitution (tree decl)
22443 tree tmpl = DECL_TI_TEMPLATE (decl);
22445 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
22446 for the instantiation. This is not always the most general
22447 template. Consider, for example:
22449 template <class T>
22450 struct S { template <class U> void f();
22451 template <> void f<int>(); };
22453 and an instantiation of S<double>::f<int>. We want TD to be the
22454 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
22455 while (/* An instantiation cannot have a definition, so we need a
22456 more general template. */
22457 DECL_TEMPLATE_INSTANTIATION (tmpl)
22458 /* We must also deal with friend templates. Given:
22460 template <class T> struct S {
22461 template <class U> friend void f() {};
22464 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
22465 so far as the language is concerned, but that's still
22466 where we get the pattern for the instantiation from. On
22467 other hand, if the definition comes outside the class, say:
22469 template <class T> struct S {
22470 template <class U> friend void f();
22472 template <class U> friend void f() {}
22474 we don't need to look any further. That's what the check for
22475 DECL_INITIAL is for. */
22476 || (TREE_CODE (decl) == FUNCTION_DECL
22477 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
22478 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
22480 /* The present template, TD, should not be a definition. If it
22481 were a definition, we should be using it! Note that we
22482 cannot restructure the loop to just keep going until we find
22483 a template with a definition, since that might go too far if
22484 a specialization was declared, but not defined. */
22486 /* Fetch the more general template. */
22487 tmpl = DECL_TI_TEMPLATE (tmpl);
22490 return tmpl;
22493 /* Returns true if we need to instantiate this template instance even if we
22494 know we aren't going to emit it. */
22496 bool
22497 always_instantiate_p (tree decl)
22499 /* We always instantiate inline functions so that we can inline them. An
22500 explicit instantiation declaration prohibits implicit instantiation of
22501 non-inline functions. With high levels of optimization, we would
22502 normally inline non-inline functions -- but we're not allowed to do
22503 that for "extern template" functions. Therefore, we check
22504 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
22505 return ((TREE_CODE (decl) == FUNCTION_DECL
22506 && (DECL_DECLARED_INLINE_P (decl)
22507 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
22508 /* And we need to instantiate static data members so that
22509 their initializers are available in integral constant
22510 expressions. */
22511 || (VAR_P (decl)
22512 && decl_maybe_constant_var_p (decl)));
22515 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
22516 instantiate it now, modifying TREE_TYPE (fn). */
22518 void
22519 maybe_instantiate_noexcept (tree fn)
22521 tree fntype, spec, noex, clone;
22523 /* Don't instantiate a noexcept-specification from template context. */
22524 if (processing_template_decl)
22525 return;
22527 if (DECL_CLONED_FUNCTION_P (fn))
22528 fn = DECL_CLONED_FUNCTION (fn);
22529 fntype = TREE_TYPE (fn);
22530 spec = TYPE_RAISES_EXCEPTIONS (fntype);
22532 if (!spec || !TREE_PURPOSE (spec))
22533 return;
22535 noex = TREE_PURPOSE (spec);
22537 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
22539 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
22540 spec = get_defaulted_eh_spec (fn);
22541 else if (push_tinst_level (fn))
22543 push_access_scope (fn);
22544 push_deferring_access_checks (dk_no_deferred);
22545 input_location = DECL_SOURCE_LOCATION (fn);
22546 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
22547 DEFERRED_NOEXCEPT_ARGS (noex),
22548 tf_warning_or_error, fn,
22549 /*function_p=*/false,
22550 /*integral_constant_expression_p=*/true);
22551 pop_deferring_access_checks ();
22552 pop_access_scope (fn);
22553 pop_tinst_level ();
22554 spec = build_noexcept_spec (noex, tf_warning_or_error);
22555 if (spec == error_mark_node)
22556 spec = noexcept_false_spec;
22558 else
22559 spec = noexcept_false_spec;
22561 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
22564 FOR_EACH_CLONE (clone, fn)
22566 if (TREE_TYPE (clone) == fntype)
22567 TREE_TYPE (clone) = TREE_TYPE (fn);
22568 else
22569 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
22573 /* Produce the definition of D, a _DECL generated from a template. If
22574 DEFER_OK is true, then we don't have to actually do the
22575 instantiation now; we just have to do it sometime. Normally it is
22576 an error if this is an explicit instantiation but D is undefined.
22577 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
22578 instantiated class template. */
22580 tree
22581 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
22583 tree tmpl = DECL_TI_TEMPLATE (d);
22584 tree gen_args;
22585 tree args;
22586 tree td;
22587 tree code_pattern;
22588 tree spec;
22589 tree gen_tmpl;
22590 bool pattern_defined;
22591 location_t saved_loc = input_location;
22592 int saved_unevaluated_operand = cp_unevaluated_operand;
22593 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
22594 bool external_p;
22595 bool deleted_p;
22597 /* This function should only be used to instantiate templates for
22598 functions and static member variables. */
22599 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
22601 /* A concept is never instantiated. */
22602 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
22604 /* Variables are never deferred; if instantiation is required, they
22605 are instantiated right away. That allows for better code in the
22606 case that an expression refers to the value of the variable --
22607 if the variable has a constant value the referring expression can
22608 take advantage of that fact. */
22609 if (VAR_P (d))
22610 defer_ok = false;
22612 /* Don't instantiate cloned functions. Instead, instantiate the
22613 functions they cloned. */
22614 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
22615 d = DECL_CLONED_FUNCTION (d);
22617 if (DECL_TEMPLATE_INSTANTIATED (d)
22618 || (TREE_CODE (d) == FUNCTION_DECL
22619 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
22620 || DECL_TEMPLATE_SPECIALIZATION (d))
22621 /* D has already been instantiated or explicitly specialized, so
22622 there's nothing for us to do here.
22624 It might seem reasonable to check whether or not D is an explicit
22625 instantiation, and, if so, stop here. But when an explicit
22626 instantiation is deferred until the end of the compilation,
22627 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
22628 the instantiation. */
22629 return d;
22631 /* Check to see whether we know that this template will be
22632 instantiated in some other file, as with "extern template"
22633 extension. */
22634 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
22636 /* In general, we do not instantiate such templates. */
22637 if (external_p && !always_instantiate_p (d))
22638 return d;
22640 gen_tmpl = most_general_template (tmpl);
22641 gen_args = DECL_TI_ARGS (d);
22643 if (tmpl != gen_tmpl)
22644 /* We should already have the extra args. */
22645 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
22646 == TMPL_ARGS_DEPTH (gen_args));
22647 /* And what's in the hash table should match D. */
22648 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
22649 || spec == NULL_TREE);
22651 /* This needs to happen before any tsubsting. */
22652 if (! push_tinst_level (d))
22653 return d;
22655 timevar_push (TV_TEMPLATE_INST);
22657 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
22658 for the instantiation. */
22659 td = template_for_substitution (d);
22660 args = gen_args;
22662 if (VAR_P (d))
22664 /* Look up an explicit specialization, if any. */
22665 tree tid = lookup_template_variable (gen_tmpl, gen_args);
22666 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
22667 if (elt && elt != error_mark_node)
22669 td = TREE_VALUE (elt);
22670 args = TREE_PURPOSE (elt);
22674 code_pattern = DECL_TEMPLATE_RESULT (td);
22676 /* We should never be trying to instantiate a member of a class
22677 template or partial specialization. */
22678 gcc_assert (d != code_pattern);
22680 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
22681 || DECL_TEMPLATE_SPECIALIZATION (td))
22682 /* In the case of a friend template whose definition is provided
22683 outside the class, we may have too many arguments. Drop the
22684 ones we don't need. The same is true for specializations. */
22685 args = get_innermost_template_args
22686 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
22688 if (TREE_CODE (d) == FUNCTION_DECL)
22690 deleted_p = DECL_DELETED_FN (code_pattern);
22691 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
22692 && DECL_INITIAL (code_pattern) != error_mark_node)
22693 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
22694 || deleted_p);
22696 else
22698 deleted_p = false;
22699 if (DECL_CLASS_SCOPE_P (code_pattern))
22700 pattern_defined = (! DECL_IN_AGGR_P (code_pattern)
22701 || DECL_INLINE_VAR_P (code_pattern));
22702 else
22703 pattern_defined = ! DECL_EXTERNAL (code_pattern);
22706 /* We may be in the middle of deferred access check. Disable it now. */
22707 push_deferring_access_checks (dk_no_deferred);
22709 /* Unless an explicit instantiation directive has already determined
22710 the linkage of D, remember that a definition is available for
22711 this entity. */
22712 if (pattern_defined
22713 && !DECL_INTERFACE_KNOWN (d)
22714 && !DECL_NOT_REALLY_EXTERN (d))
22715 mark_definable (d);
22717 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
22718 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
22719 input_location = DECL_SOURCE_LOCATION (d);
22721 /* If D is a member of an explicitly instantiated class template,
22722 and no definition is available, treat it like an implicit
22723 instantiation. */
22724 if (!pattern_defined && expl_inst_class_mem_p
22725 && DECL_EXPLICIT_INSTANTIATION (d))
22727 /* Leave linkage flags alone on instantiations with anonymous
22728 visibility. */
22729 if (TREE_PUBLIC (d))
22731 DECL_NOT_REALLY_EXTERN (d) = 0;
22732 DECL_INTERFACE_KNOWN (d) = 0;
22734 SET_DECL_IMPLICIT_INSTANTIATION (d);
22737 /* Defer all other templates, unless we have been explicitly
22738 forbidden from doing so. */
22739 if (/* If there is no definition, we cannot instantiate the
22740 template. */
22741 ! pattern_defined
22742 /* If it's OK to postpone instantiation, do so. */
22743 || defer_ok
22744 /* If this is a static data member that will be defined
22745 elsewhere, we don't want to instantiate the entire data
22746 member, but we do want to instantiate the initializer so that
22747 we can substitute that elsewhere. */
22748 || (external_p && VAR_P (d))
22749 /* Handle here a deleted function too, avoid generating
22750 its body (c++/61080). */
22751 || deleted_p)
22753 /* The definition of the static data member is now required so
22754 we must substitute the initializer. */
22755 if (VAR_P (d)
22756 && !DECL_INITIAL (d)
22757 && DECL_INITIAL (code_pattern))
22759 tree ns;
22760 tree init;
22761 bool const_init = false;
22762 bool enter_context = DECL_CLASS_SCOPE_P (d);
22764 ns = decl_namespace_context (d);
22765 push_nested_namespace (ns);
22766 if (enter_context)
22767 push_nested_class (DECL_CONTEXT (d));
22768 init = tsubst_expr (DECL_INITIAL (code_pattern),
22769 args,
22770 tf_warning_or_error, NULL_TREE,
22771 /*integral_constant_expression_p=*/false);
22772 /* If instantiating the initializer involved instantiating this
22773 again, don't call cp_finish_decl twice. */
22774 if (!DECL_INITIAL (d))
22776 /* Make sure the initializer is still constant, in case of
22777 circular dependency (template/instantiate6.C). */
22778 const_init
22779 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
22780 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
22781 /*asmspec_tree=*/NULL_TREE,
22782 LOOKUP_ONLYCONVERTING);
22784 if (enter_context)
22785 pop_nested_class ();
22786 pop_nested_namespace (ns);
22789 /* We restore the source position here because it's used by
22790 add_pending_template. */
22791 input_location = saved_loc;
22793 if (at_eof && !pattern_defined
22794 && DECL_EXPLICIT_INSTANTIATION (d)
22795 && DECL_NOT_REALLY_EXTERN (d))
22796 /* [temp.explicit]
22798 The definition of a non-exported function template, a
22799 non-exported member function template, or a non-exported
22800 member function or static data member of a class template
22801 shall be present in every translation unit in which it is
22802 explicitly instantiated. */
22803 permerror (input_location, "explicit instantiation of %qD "
22804 "but no definition available", d);
22806 /* If we're in unevaluated context, we just wanted to get the
22807 constant value; this isn't an odr use, so don't queue
22808 a full instantiation. */
22809 if (cp_unevaluated_operand != 0)
22810 goto out;
22811 /* ??? Historically, we have instantiated inline functions, even
22812 when marked as "extern template". */
22813 if (!(external_p && VAR_P (d)))
22814 add_pending_template (d);
22815 goto out;
22817 /* Tell the repository that D is available in this translation unit
22818 -- and see if it is supposed to be instantiated here. */
22819 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
22821 /* In a PCH file, despite the fact that the repository hasn't
22822 requested instantiation in the PCH it is still possible that
22823 an instantiation will be required in a file that includes the
22824 PCH. */
22825 if (pch_file)
22826 add_pending_template (d);
22827 /* Instantiate inline functions so that the inliner can do its
22828 job, even though we'll not be emitting a copy of this
22829 function. */
22830 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
22831 goto out;
22834 bool push_to_top, nested;
22835 tree fn_context;
22836 fn_context = decl_function_context (d);
22837 nested = current_function_decl != NULL_TREE;
22838 push_to_top = !(nested && fn_context == current_function_decl);
22840 vec<tree> omp_privatization_save;
22841 if (nested)
22842 save_omp_privatization_clauses (omp_privatization_save);
22844 if (push_to_top)
22845 push_to_top_level ();
22846 else
22848 push_function_context ();
22849 cp_unevaluated_operand = 0;
22850 c_inhibit_evaluation_warnings = 0;
22853 /* Mark D as instantiated so that recursive calls to
22854 instantiate_decl do not try to instantiate it again. */
22855 DECL_TEMPLATE_INSTANTIATED (d) = 1;
22857 /* Regenerate the declaration in case the template has been modified
22858 by a subsequent redeclaration. */
22859 regenerate_decl_from_template (d, td, args);
22861 /* We already set the file and line above. Reset them now in case
22862 they changed as a result of calling regenerate_decl_from_template. */
22863 input_location = DECL_SOURCE_LOCATION (d);
22865 if (VAR_P (d))
22867 tree init;
22868 bool const_init = false;
22870 /* Clear out DECL_RTL; whatever was there before may not be right
22871 since we've reset the type of the declaration. */
22872 SET_DECL_RTL (d, NULL);
22873 DECL_IN_AGGR_P (d) = 0;
22875 /* The initializer is placed in DECL_INITIAL by
22876 regenerate_decl_from_template so we don't need to
22877 push/pop_access_scope again here. Pull it out so that
22878 cp_finish_decl can process it. */
22879 init = DECL_INITIAL (d);
22880 DECL_INITIAL (d) = NULL_TREE;
22881 DECL_INITIALIZED_P (d) = 0;
22883 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
22884 initializer. That function will defer actual emission until
22885 we have a chance to determine linkage. */
22886 DECL_EXTERNAL (d) = 0;
22888 /* Enter the scope of D so that access-checking works correctly. */
22889 bool enter_context = DECL_CLASS_SCOPE_P (d);
22890 if (enter_context)
22891 push_nested_class (DECL_CONTEXT (d));
22893 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
22894 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
22896 if (enter_context)
22897 pop_nested_class ();
22899 if (variable_template_p (gen_tmpl))
22900 note_variable_template_instantiation (d);
22902 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
22903 synthesize_method (d);
22904 else if (TREE_CODE (d) == FUNCTION_DECL)
22906 hash_map<tree, tree> *saved_local_specializations;
22907 tree tmpl_parm;
22908 tree spec_parm;
22909 tree block = NULL_TREE;
22910 tree lambda_ctx = NULL_TREE;
22912 /* Save away the current list, in case we are instantiating one
22913 template from within the body of another. */
22914 saved_local_specializations = local_specializations;
22916 /* Set up the list of local specializations. */
22917 local_specializations = new hash_map<tree, tree>;
22919 /* Set up context. */
22920 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
22921 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
22922 block = push_stmt_list ();
22923 else
22925 if (push_to_top && LAMBDA_FUNCTION_P (d))
22927 /* When instantiating a lambda's templated function
22928 operator, we need to push the non-lambda class scope
22929 of the lambda itself so that the nested function
22930 stack is sufficiently correct to deal with this
22931 capture. */
22932 lambda_ctx = DECL_CONTEXT (d);
22934 lambda_ctx = decl_type_context (TYPE_NAME (lambda_ctx));
22935 while (lambda_ctx && LAMBDA_TYPE_P (lambda_ctx));
22936 if (lambda_ctx)
22937 push_nested_class (lambda_ctx);
22939 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
22942 /* Some typedefs referenced from within the template code need to be
22943 access checked at template instantiation time, i.e now. These
22944 types were added to the template at parsing time. Let's get those
22945 and perform the access checks then. */
22946 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
22947 args);
22949 /* Create substitution entries for the parameters. */
22950 tmpl_parm = DECL_ARGUMENTS (code_pattern);
22951 spec_parm = DECL_ARGUMENTS (d);
22952 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (d))
22954 register_local_specialization (spec_parm, tmpl_parm);
22955 spec_parm = skip_artificial_parms_for (d, spec_parm);
22956 tmpl_parm = skip_artificial_parms_for (code_pattern, tmpl_parm);
22958 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
22960 if (!DECL_PACK_P (tmpl_parm))
22962 register_local_specialization (spec_parm, tmpl_parm);
22963 spec_parm = DECL_CHAIN (spec_parm);
22965 else
22967 /* Register the (value) argument pack as a specialization of
22968 TMPL_PARM, then move on. */
22969 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
22970 register_local_specialization (argpack, tmpl_parm);
22973 gcc_assert (!spec_parm);
22975 /* Substitute into the body of the function. */
22976 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
22977 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
22978 tf_warning_or_error, tmpl);
22979 else
22981 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
22982 tf_warning_or_error, tmpl,
22983 /*integral_constant_expression_p=*/false);
22985 /* Set the current input_location to the end of the function
22986 so that finish_function knows where we are. */
22987 input_location
22988 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
22990 /* Remember if we saw an infinite loop in the template. */
22991 current_function_infinite_loop
22992 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
22995 /* We don't need the local specializations any more. */
22996 delete local_specializations;
22997 local_specializations = saved_local_specializations;
22999 /* Finish the function. */
23000 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23001 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23002 DECL_SAVED_TREE (d) = pop_stmt_list (block);
23003 else
23005 d = finish_function (0);
23006 expand_or_defer_fn (d);
23008 if (lambda_ctx)
23009 pop_nested_class ();
23011 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23012 cp_check_omp_declare_reduction (d);
23015 /* We're not deferring instantiation any more. */
23016 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
23018 if (push_to_top)
23019 pop_from_top_level ();
23020 else
23021 pop_function_context ();
23023 if (nested)
23024 restore_omp_privatization_clauses (omp_privatization_save);
23026 out:
23027 pop_deferring_access_checks ();
23028 timevar_pop (TV_TEMPLATE_INST);
23029 pop_tinst_level ();
23030 input_location = saved_loc;
23031 cp_unevaluated_operand = saved_unevaluated_operand;
23032 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23034 return d;
23037 /* Run through the list of templates that we wish we could
23038 instantiate, and instantiate any we can. RETRIES is the
23039 number of times we retry pending template instantiation. */
23041 void
23042 instantiate_pending_templates (int retries)
23044 int reconsider;
23045 location_t saved_loc = input_location;
23047 /* Instantiating templates may trigger vtable generation. This in turn
23048 may require further template instantiations. We place a limit here
23049 to avoid infinite loop. */
23050 if (pending_templates && retries >= max_tinst_depth)
23052 tree decl = pending_templates->tinst->decl;
23054 fatal_error (input_location,
23055 "template instantiation depth exceeds maximum of %d"
23056 " instantiating %q+D, possibly from virtual table generation"
23057 " (use -ftemplate-depth= to increase the maximum)",
23058 max_tinst_depth, decl);
23059 if (TREE_CODE (decl) == FUNCTION_DECL)
23060 /* Pretend that we defined it. */
23061 DECL_INITIAL (decl) = error_mark_node;
23062 return;
23067 struct pending_template **t = &pending_templates;
23068 struct pending_template *last = NULL;
23069 reconsider = 0;
23070 while (*t)
23072 tree instantiation = reopen_tinst_level ((*t)->tinst);
23073 bool complete = false;
23075 if (TYPE_P (instantiation))
23077 tree fn;
23079 if (!COMPLETE_TYPE_P (instantiation))
23081 instantiate_class_template (instantiation);
23082 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
23083 for (fn = TYPE_METHODS (instantiation);
23085 fn = TREE_CHAIN (fn))
23086 if (! DECL_ARTIFICIAL (fn))
23087 instantiate_decl (fn,
23088 /*defer_ok=*/false,
23089 /*expl_inst_class_mem_p=*/false);
23090 if (COMPLETE_TYPE_P (instantiation))
23091 reconsider = 1;
23094 complete = COMPLETE_TYPE_P (instantiation);
23096 else
23098 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
23099 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
23101 instantiation
23102 = instantiate_decl (instantiation,
23103 /*defer_ok=*/false,
23104 /*expl_inst_class_mem_p=*/false);
23105 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
23106 reconsider = 1;
23109 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
23110 || DECL_TEMPLATE_INSTANTIATED (instantiation));
23113 if (complete)
23114 /* If INSTANTIATION has been instantiated, then we don't
23115 need to consider it again in the future. */
23116 *t = (*t)->next;
23117 else
23119 last = *t;
23120 t = &(*t)->next;
23122 tinst_depth = 0;
23123 current_tinst_level = NULL;
23125 last_pending_template = last;
23127 while (reconsider);
23129 input_location = saved_loc;
23132 /* Substitute ARGVEC into T, which is a list of initializers for
23133 either base class or a non-static data member. The TREE_PURPOSEs
23134 are DECLs, and the TREE_VALUEs are the initializer values. Used by
23135 instantiate_decl. */
23137 static tree
23138 tsubst_initializer_list (tree t, tree argvec)
23140 tree inits = NULL_TREE;
23142 for (; t; t = TREE_CHAIN (t))
23144 tree decl;
23145 tree init;
23146 tree expanded_bases = NULL_TREE;
23147 tree expanded_arguments = NULL_TREE;
23148 int i, len = 1;
23150 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
23152 tree expr;
23153 tree arg;
23155 /* Expand the base class expansion type into separate base
23156 classes. */
23157 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
23158 tf_warning_or_error,
23159 NULL_TREE);
23160 if (expanded_bases == error_mark_node)
23161 continue;
23163 /* We'll be building separate TREE_LISTs of arguments for
23164 each base. */
23165 len = TREE_VEC_LENGTH (expanded_bases);
23166 expanded_arguments = make_tree_vec (len);
23167 for (i = 0; i < len; i++)
23168 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
23170 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
23171 expand each argument in the TREE_VALUE of t. */
23172 expr = make_node (EXPR_PACK_EXPANSION);
23173 PACK_EXPANSION_LOCAL_P (expr) = true;
23174 PACK_EXPANSION_PARAMETER_PACKS (expr) =
23175 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
23177 if (TREE_VALUE (t) == void_type_node)
23178 /* VOID_TYPE_NODE is used to indicate
23179 value-initialization. */
23181 for (i = 0; i < len; i++)
23182 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
23184 else
23186 /* Substitute parameter packs into each argument in the
23187 TREE_LIST. */
23188 in_base_initializer = 1;
23189 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
23191 tree expanded_exprs;
23193 /* Expand the argument. */
23194 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
23195 expanded_exprs
23196 = tsubst_pack_expansion (expr, argvec,
23197 tf_warning_or_error,
23198 NULL_TREE);
23199 if (expanded_exprs == error_mark_node)
23200 continue;
23202 /* Prepend each of the expanded expressions to the
23203 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
23204 for (i = 0; i < len; i++)
23206 TREE_VEC_ELT (expanded_arguments, i) =
23207 tree_cons (NULL_TREE,
23208 TREE_VEC_ELT (expanded_exprs, i),
23209 TREE_VEC_ELT (expanded_arguments, i));
23212 in_base_initializer = 0;
23214 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
23215 since we built them backwards. */
23216 for (i = 0; i < len; i++)
23218 TREE_VEC_ELT (expanded_arguments, i) =
23219 nreverse (TREE_VEC_ELT (expanded_arguments, i));
23224 for (i = 0; i < len; ++i)
23226 if (expanded_bases)
23228 decl = TREE_VEC_ELT (expanded_bases, i);
23229 decl = expand_member_init (decl);
23230 init = TREE_VEC_ELT (expanded_arguments, i);
23232 else
23234 tree tmp;
23235 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
23236 tf_warning_or_error, NULL_TREE);
23238 decl = expand_member_init (decl);
23239 if (decl && !DECL_P (decl))
23240 in_base_initializer = 1;
23242 init = TREE_VALUE (t);
23243 tmp = init;
23244 if (init != void_type_node)
23245 init = tsubst_expr (init, argvec,
23246 tf_warning_or_error, NULL_TREE,
23247 /*integral_constant_expression_p=*/false);
23248 if (init == NULL_TREE && tmp != NULL_TREE)
23249 /* If we had an initializer but it instantiated to nothing,
23250 value-initialize the object. This will only occur when
23251 the initializer was a pack expansion where the parameter
23252 packs used in that expansion were of length zero. */
23253 init = void_type_node;
23254 in_base_initializer = 0;
23257 if (decl)
23259 init = build_tree_list (decl, init);
23260 TREE_CHAIN (init) = inits;
23261 inits = init;
23265 return inits;
23268 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
23270 static void
23271 set_current_access_from_decl (tree decl)
23273 if (TREE_PRIVATE (decl))
23274 current_access_specifier = access_private_node;
23275 else if (TREE_PROTECTED (decl))
23276 current_access_specifier = access_protected_node;
23277 else
23278 current_access_specifier = access_public_node;
23281 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
23282 is the instantiation (which should have been created with
23283 start_enum) and ARGS are the template arguments to use. */
23285 static void
23286 tsubst_enum (tree tag, tree newtag, tree args)
23288 tree e;
23290 if (SCOPED_ENUM_P (newtag))
23291 begin_scope (sk_scoped_enum, newtag);
23293 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
23295 tree value;
23296 tree decl;
23298 decl = TREE_VALUE (e);
23299 /* Note that in a template enum, the TREE_VALUE is the
23300 CONST_DECL, not the corresponding INTEGER_CST. */
23301 value = tsubst_expr (DECL_INITIAL (decl),
23302 args, tf_warning_or_error, NULL_TREE,
23303 /*integral_constant_expression_p=*/true);
23305 /* Give this enumeration constant the correct access. */
23306 set_current_access_from_decl (decl);
23308 /* Actually build the enumerator itself. Here we're assuming that
23309 enumerators can't have dependent attributes. */
23310 build_enumerator (DECL_NAME (decl), value, newtag,
23311 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
23314 if (SCOPED_ENUM_P (newtag))
23315 finish_scope ();
23317 finish_enum_value_list (newtag);
23318 finish_enum (newtag);
23320 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
23321 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
23324 /* DECL is a FUNCTION_DECL that is a template specialization. Return
23325 its type -- but without substituting the innermost set of template
23326 arguments. So, innermost set of template parameters will appear in
23327 the type. */
23329 tree
23330 get_mostly_instantiated_function_type (tree decl)
23332 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
23333 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
23336 /* Return truthvalue if we're processing a template different from
23337 the last one involved in diagnostics. */
23338 bool
23339 problematic_instantiation_changed (void)
23341 return current_tinst_level != last_error_tinst_level;
23344 /* Remember current template involved in diagnostics. */
23345 void
23346 record_last_problematic_instantiation (void)
23348 last_error_tinst_level = current_tinst_level;
23351 struct tinst_level *
23352 current_instantiation (void)
23354 return current_tinst_level;
23357 /* Return TRUE if current_function_decl is being instantiated, false
23358 otherwise. */
23360 bool
23361 instantiating_current_function_p (void)
23363 return (current_instantiation ()
23364 && current_instantiation ()->decl == current_function_decl);
23367 /* [temp.param] Check that template non-type parm TYPE is of an allowable
23368 type. Return zero for ok, nonzero for disallowed. Issue error and
23369 warning messages under control of COMPLAIN. */
23371 static int
23372 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
23374 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
23375 return 0;
23376 else if (POINTER_TYPE_P (type))
23377 return 0;
23378 else if (TYPE_PTRMEM_P (type))
23379 return 0;
23380 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
23381 return 0;
23382 else if (TREE_CODE (type) == TYPENAME_TYPE)
23383 return 0;
23384 else if (TREE_CODE (type) == DECLTYPE_TYPE)
23385 return 0;
23386 else if (TREE_CODE (type) == NULLPTR_TYPE)
23387 return 0;
23388 /* A bound template template parm could later be instantiated to have a valid
23389 nontype parm type via an alias template. */
23390 else if (cxx_dialect >= cxx11
23391 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23392 return 0;
23394 if (complain & tf_error)
23396 if (type == error_mark_node)
23397 inform (input_location, "invalid template non-type parameter");
23398 else
23399 error ("%q#T is not a valid type for a template non-type parameter",
23400 type);
23402 return 1;
23405 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
23406 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
23408 static bool
23409 dependent_type_p_r (tree type)
23411 tree scope;
23413 /* [temp.dep.type]
23415 A type is dependent if it is:
23417 -- a template parameter. Template template parameters are types
23418 for us (since TYPE_P holds true for them) so we handle
23419 them here. */
23420 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
23421 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
23422 return true;
23423 /* -- a qualified-id with a nested-name-specifier which contains a
23424 class-name that names a dependent type or whose unqualified-id
23425 names a dependent type. */
23426 if (TREE_CODE (type) == TYPENAME_TYPE)
23427 return true;
23429 /* An alias template specialization can be dependent even if the
23430 resulting type is not. */
23431 if (dependent_alias_template_spec_p (type))
23432 return true;
23434 /* -- a cv-qualified type where the cv-unqualified type is
23435 dependent.
23436 No code is necessary for this bullet; the code below handles
23437 cv-qualified types, and we don't want to strip aliases with
23438 TYPE_MAIN_VARIANT because of DR 1558. */
23439 /* -- a compound type constructed from any dependent type. */
23440 if (TYPE_PTRMEM_P (type))
23441 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
23442 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
23443 (type)));
23444 else if (TYPE_PTR_P (type)
23445 || TREE_CODE (type) == REFERENCE_TYPE)
23446 return dependent_type_p (TREE_TYPE (type));
23447 else if (TREE_CODE (type) == FUNCTION_TYPE
23448 || TREE_CODE (type) == METHOD_TYPE)
23450 tree arg_type;
23452 if (dependent_type_p (TREE_TYPE (type)))
23453 return true;
23454 for (arg_type = TYPE_ARG_TYPES (type);
23455 arg_type;
23456 arg_type = TREE_CHAIN (arg_type))
23457 if (dependent_type_p (TREE_VALUE (arg_type)))
23458 return true;
23459 if (cxx_dialect >= cxx1z)
23461 /* A value-dependent noexcept-specifier makes the type dependent. */
23462 tree spec = TYPE_RAISES_EXCEPTIONS (type);
23463 if (spec && TREE_PURPOSE (spec)
23464 && value_dependent_expression_p (TREE_PURPOSE (spec)))
23465 return true;
23467 return false;
23469 /* -- an array type constructed from any dependent type or whose
23470 size is specified by a constant expression that is
23471 value-dependent.
23473 We checked for type- and value-dependence of the bounds in
23474 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
23475 if (TREE_CODE (type) == ARRAY_TYPE)
23477 if (TYPE_DOMAIN (type)
23478 && dependent_type_p (TYPE_DOMAIN (type)))
23479 return true;
23480 return dependent_type_p (TREE_TYPE (type));
23483 /* -- a template-id in which either the template name is a template
23484 parameter ... */
23485 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
23486 return true;
23487 /* ... or any of the template arguments is a dependent type or
23488 an expression that is type-dependent or value-dependent. */
23489 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
23490 && (any_dependent_template_arguments_p
23491 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
23492 return true;
23494 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
23495 dependent; if the argument of the `typeof' expression is not
23496 type-dependent, then it should already been have resolved. */
23497 if (TREE_CODE (type) == TYPEOF_TYPE
23498 || TREE_CODE (type) == DECLTYPE_TYPE
23499 || TREE_CODE (type) == UNDERLYING_TYPE)
23500 return true;
23502 /* A template argument pack is dependent if any of its packed
23503 arguments are. */
23504 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
23506 tree args = ARGUMENT_PACK_ARGS (type);
23507 int i, len = TREE_VEC_LENGTH (args);
23508 for (i = 0; i < len; ++i)
23509 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
23510 return true;
23513 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
23514 be template parameters. */
23515 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
23516 return true;
23518 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
23519 return true;
23521 /* The standard does not specifically mention types that are local
23522 to template functions or local classes, but they should be
23523 considered dependent too. For example:
23525 template <int I> void f() {
23526 enum E { a = I };
23527 S<sizeof (E)> s;
23530 The size of `E' cannot be known until the value of `I' has been
23531 determined. Therefore, `E' must be considered dependent. */
23532 scope = TYPE_CONTEXT (type);
23533 if (scope && TYPE_P (scope))
23534 return dependent_type_p (scope);
23535 /* Don't use type_dependent_expression_p here, as it can lead
23536 to infinite recursion trying to determine whether a lambda
23537 nested in a lambda is dependent (c++/47687). */
23538 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
23539 && DECL_LANG_SPECIFIC (scope)
23540 && DECL_TEMPLATE_INFO (scope)
23541 && (any_dependent_template_arguments_p
23542 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
23543 return true;
23545 /* Other types are non-dependent. */
23546 return false;
23549 /* Returns TRUE if TYPE is dependent, in the sense of
23550 [temp.dep.type]. Note that a NULL type is considered dependent. */
23552 bool
23553 dependent_type_p (tree type)
23555 /* If there are no template parameters in scope, then there can't be
23556 any dependent types. */
23557 if (!processing_template_decl)
23559 /* If we are not processing a template, then nobody should be
23560 providing us with a dependent type. */
23561 gcc_assert (type);
23562 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
23563 return false;
23566 /* If the type is NULL, we have not computed a type for the entity
23567 in question; in that case, the type is dependent. */
23568 if (!type)
23569 return true;
23571 /* Erroneous types can be considered non-dependent. */
23572 if (type == error_mark_node)
23573 return false;
23575 /* Getting here with global_type_node means we improperly called this
23576 function on the TREE_TYPE of an IDENTIFIER_NODE. */
23577 gcc_checking_assert (type != global_type_node);
23579 /* If we have not already computed the appropriate value for TYPE,
23580 do so now. */
23581 if (!TYPE_DEPENDENT_P_VALID (type))
23583 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
23584 TYPE_DEPENDENT_P_VALID (type) = 1;
23587 return TYPE_DEPENDENT_P (type);
23590 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
23591 lookup. In other words, a dependent type that is not the current
23592 instantiation. */
23594 bool
23595 dependent_scope_p (tree scope)
23597 return (scope && TYPE_P (scope) && dependent_type_p (scope)
23598 && !currently_open_class (scope));
23601 /* T is a SCOPE_REF; return whether we need to consider it
23602 instantiation-dependent so that we can check access at instantiation
23603 time even though we know which member it resolves to. */
23605 static bool
23606 instantiation_dependent_scope_ref_p (tree t)
23608 if (DECL_P (TREE_OPERAND (t, 1))
23609 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
23610 && accessible_in_template_p (TREE_OPERAND (t, 0),
23611 TREE_OPERAND (t, 1)))
23612 return false;
23613 else
23614 return true;
23617 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
23618 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
23619 expression. */
23621 /* Note that this predicate is not appropriate for general expressions;
23622 only constant expressions (that satisfy potential_constant_expression)
23623 can be tested for value dependence. */
23625 bool
23626 value_dependent_expression_p (tree expression)
23628 if (!processing_template_decl || expression == NULL_TREE)
23629 return false;
23631 /* A name declared with a dependent type. */
23632 if (DECL_P (expression) && type_dependent_expression_p (expression))
23633 return true;
23635 switch (TREE_CODE (expression))
23637 case BASELINK:
23638 /* A dependent member function of the current instantiation. */
23639 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
23641 case FUNCTION_DECL:
23642 /* A dependent member function of the current instantiation. */
23643 if (DECL_CLASS_SCOPE_P (expression)
23644 && dependent_type_p (DECL_CONTEXT (expression)))
23645 return true;
23646 break;
23648 case IDENTIFIER_NODE:
23649 /* A name that has not been looked up -- must be dependent. */
23650 return true;
23652 case TEMPLATE_PARM_INDEX:
23653 /* A non-type template parm. */
23654 return true;
23656 case CONST_DECL:
23657 /* A non-type template parm. */
23658 if (DECL_TEMPLATE_PARM_P (expression))
23659 return true;
23660 return value_dependent_expression_p (DECL_INITIAL (expression));
23662 case VAR_DECL:
23663 /* A constant with literal type and is initialized
23664 with an expression that is value-dependent.
23666 Note that a non-dependent parenthesized initializer will have
23667 already been replaced with its constant value, so if we see
23668 a TREE_LIST it must be dependent. */
23669 if (DECL_INITIAL (expression)
23670 && decl_constant_var_p (expression)
23671 && (TREE_CODE (DECL_INITIAL (expression)) == TREE_LIST
23672 /* cp_finish_decl doesn't fold reference initializers. */
23673 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE
23674 || type_dependent_expression_p (DECL_INITIAL (expression))
23675 || value_dependent_expression_p (DECL_INITIAL (expression))))
23676 return true;
23677 if (DECL_HAS_VALUE_EXPR_P (expression))
23679 tree value_expr = DECL_VALUE_EXPR (expression);
23680 if (type_dependent_expression_p (value_expr))
23681 return true;
23683 return false;
23685 case DYNAMIC_CAST_EXPR:
23686 case STATIC_CAST_EXPR:
23687 case CONST_CAST_EXPR:
23688 case REINTERPRET_CAST_EXPR:
23689 case CAST_EXPR:
23690 /* These expressions are value-dependent if the type to which
23691 the cast occurs is dependent or the expression being casted
23692 is value-dependent. */
23694 tree type = TREE_TYPE (expression);
23696 if (dependent_type_p (type))
23697 return true;
23699 /* A functional cast has a list of operands. */
23700 expression = TREE_OPERAND (expression, 0);
23701 if (!expression)
23703 /* If there are no operands, it must be an expression such
23704 as "int()". This should not happen for aggregate types
23705 because it would form non-constant expressions. */
23706 gcc_assert (cxx_dialect >= cxx11
23707 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
23709 return false;
23712 if (TREE_CODE (expression) == TREE_LIST)
23713 return any_value_dependent_elements_p (expression);
23715 return value_dependent_expression_p (expression);
23718 case SIZEOF_EXPR:
23719 if (SIZEOF_EXPR_TYPE_P (expression))
23720 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
23721 /* FALLTHRU */
23722 case ALIGNOF_EXPR:
23723 case TYPEID_EXPR:
23724 /* A `sizeof' expression is value-dependent if the operand is
23725 type-dependent or is a pack expansion. */
23726 expression = TREE_OPERAND (expression, 0);
23727 if (PACK_EXPANSION_P (expression))
23728 return true;
23729 else if (TYPE_P (expression))
23730 return dependent_type_p (expression);
23731 return instantiation_dependent_uneval_expression_p (expression);
23733 case AT_ENCODE_EXPR:
23734 /* An 'encode' expression is value-dependent if the operand is
23735 type-dependent. */
23736 expression = TREE_OPERAND (expression, 0);
23737 return dependent_type_p (expression);
23739 case NOEXCEPT_EXPR:
23740 expression = TREE_OPERAND (expression, 0);
23741 return instantiation_dependent_uneval_expression_p (expression);
23743 case SCOPE_REF:
23744 /* All instantiation-dependent expressions should also be considered
23745 value-dependent. */
23746 return instantiation_dependent_scope_ref_p (expression);
23748 case COMPONENT_REF:
23749 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
23750 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
23752 case NONTYPE_ARGUMENT_PACK:
23753 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
23754 is value-dependent. */
23756 tree values = ARGUMENT_PACK_ARGS (expression);
23757 int i, len = TREE_VEC_LENGTH (values);
23759 for (i = 0; i < len; ++i)
23760 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
23761 return true;
23763 return false;
23766 case TRAIT_EXPR:
23768 tree type2 = TRAIT_EXPR_TYPE2 (expression);
23769 return (dependent_type_p (TRAIT_EXPR_TYPE1 (expression))
23770 || (type2 ? dependent_type_p (type2) : false));
23773 case MODOP_EXPR:
23774 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
23775 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
23777 case ARRAY_REF:
23778 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
23779 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
23781 case ADDR_EXPR:
23783 tree op = TREE_OPERAND (expression, 0);
23784 return (value_dependent_expression_p (op)
23785 || has_value_dependent_address (op));
23788 case REQUIRES_EXPR:
23789 /* Treat all requires-expressions as value-dependent so
23790 we don't try to fold them. */
23791 return true;
23793 case TYPE_REQ:
23794 return dependent_type_p (TREE_OPERAND (expression, 0));
23796 case CALL_EXPR:
23798 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
23799 return true;
23800 tree fn = get_callee_fndecl (expression);
23801 int i, nargs;
23802 nargs = call_expr_nargs (expression);
23803 for (i = 0; i < nargs; ++i)
23805 tree op = CALL_EXPR_ARG (expression, i);
23806 /* In a call to a constexpr member function, look through the
23807 implicit ADDR_EXPR on the object argument so that it doesn't
23808 cause the call to be considered value-dependent. We also
23809 look through it in potential_constant_expression. */
23810 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
23811 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
23812 && TREE_CODE (op) == ADDR_EXPR)
23813 op = TREE_OPERAND (op, 0);
23814 if (value_dependent_expression_p (op))
23815 return true;
23817 return false;
23820 case TEMPLATE_ID_EXPR:
23821 /* If a TEMPLATE_ID_EXPR involves a dependent name, it will be
23822 type-dependent. */
23823 return type_dependent_expression_p (expression)
23824 || variable_concept_p (TREE_OPERAND (expression, 0));
23826 case CONSTRUCTOR:
23828 unsigned ix;
23829 tree val;
23830 if (dependent_type_p (TREE_TYPE (expression)))
23831 return true;
23832 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
23833 if (value_dependent_expression_p (val))
23834 return true;
23835 return false;
23838 case STMT_EXPR:
23839 /* Treat a GNU statement expression as dependent to avoid crashing
23840 under instantiate_non_dependent_expr; it can't be constant. */
23841 return true;
23843 default:
23844 /* A constant expression is value-dependent if any subexpression is
23845 value-dependent. */
23846 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
23848 case tcc_reference:
23849 case tcc_unary:
23850 case tcc_comparison:
23851 case tcc_binary:
23852 case tcc_expression:
23853 case tcc_vl_exp:
23855 int i, len = cp_tree_operand_length (expression);
23857 for (i = 0; i < len; i++)
23859 tree t = TREE_OPERAND (expression, i);
23861 /* In some cases, some of the operands may be missing.l
23862 (For example, in the case of PREDECREMENT_EXPR, the
23863 amount to increment by may be missing.) That doesn't
23864 make the expression dependent. */
23865 if (t && value_dependent_expression_p (t))
23866 return true;
23869 break;
23870 default:
23871 break;
23873 break;
23876 /* The expression is not value-dependent. */
23877 return false;
23880 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
23881 [temp.dep.expr]. Note that an expression with no type is
23882 considered dependent. Other parts of the compiler arrange for an
23883 expression with type-dependent subexpressions to have no type, so
23884 this function doesn't have to be fully recursive. */
23886 bool
23887 type_dependent_expression_p (tree expression)
23889 if (!processing_template_decl)
23890 return false;
23892 if (expression == NULL_TREE || expression == error_mark_node)
23893 return false;
23895 /* An unresolved name is always dependent. */
23896 if (identifier_p (expression)
23897 || TREE_CODE (expression) == USING_DECL
23898 || TREE_CODE (expression) == WILDCARD_DECL)
23899 return true;
23901 /* A fold expression is type-dependent. */
23902 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
23903 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
23904 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
23905 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
23906 return true;
23908 /* Some expression forms are never type-dependent. */
23909 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
23910 || TREE_CODE (expression) == SIZEOF_EXPR
23911 || TREE_CODE (expression) == ALIGNOF_EXPR
23912 || TREE_CODE (expression) == AT_ENCODE_EXPR
23913 || TREE_CODE (expression) == NOEXCEPT_EXPR
23914 || TREE_CODE (expression) == TRAIT_EXPR
23915 || TREE_CODE (expression) == TYPEID_EXPR
23916 || TREE_CODE (expression) == DELETE_EXPR
23917 || TREE_CODE (expression) == VEC_DELETE_EXPR
23918 || TREE_CODE (expression) == THROW_EXPR
23919 || TREE_CODE (expression) == REQUIRES_EXPR)
23920 return false;
23922 /* The types of these expressions depends only on the type to which
23923 the cast occurs. */
23924 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
23925 || TREE_CODE (expression) == STATIC_CAST_EXPR
23926 || TREE_CODE (expression) == CONST_CAST_EXPR
23927 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
23928 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
23929 || TREE_CODE (expression) == CAST_EXPR)
23930 return dependent_type_p (TREE_TYPE (expression));
23932 /* The types of these expressions depends only on the type created
23933 by the expression. */
23934 if (TREE_CODE (expression) == NEW_EXPR
23935 || TREE_CODE (expression) == VEC_NEW_EXPR)
23937 /* For NEW_EXPR tree nodes created inside a template, either
23938 the object type itself or a TREE_LIST may appear as the
23939 operand 1. */
23940 tree type = TREE_OPERAND (expression, 1);
23941 if (TREE_CODE (type) == TREE_LIST)
23942 /* This is an array type. We need to check array dimensions
23943 as well. */
23944 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
23945 || value_dependent_expression_p
23946 (TREE_OPERAND (TREE_VALUE (type), 1));
23947 else
23948 return dependent_type_p (type);
23951 if (TREE_CODE (expression) == SCOPE_REF)
23953 tree scope = TREE_OPERAND (expression, 0);
23954 tree name = TREE_OPERAND (expression, 1);
23956 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
23957 contains an identifier associated by name lookup with one or more
23958 declarations declared with a dependent type, or...a
23959 nested-name-specifier or qualified-id that names a member of an
23960 unknown specialization. */
23961 return (type_dependent_expression_p (name)
23962 || dependent_scope_p (scope));
23965 if (TREE_CODE (expression) == TEMPLATE_DECL
23966 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
23967 return uses_outer_template_parms (expression);
23969 if (TREE_CODE (expression) == STMT_EXPR)
23970 expression = stmt_expr_value_expr (expression);
23972 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
23974 tree elt;
23975 unsigned i;
23977 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
23979 if (type_dependent_expression_p (elt))
23980 return true;
23982 return false;
23985 /* A static data member of the current instantiation with incomplete
23986 array type is type-dependent, as the definition and specializations
23987 can have different bounds. */
23988 if (VAR_P (expression)
23989 && DECL_CLASS_SCOPE_P (expression)
23990 && dependent_type_p (DECL_CONTEXT (expression))
23991 && VAR_HAD_UNKNOWN_BOUND (expression))
23992 return true;
23994 /* An array of unknown bound depending on a variadic parameter, eg:
23996 template<typename... Args>
23997 void foo (Args... args)
23999 int arr[] = { args... };
24002 template<int... vals>
24003 void bar ()
24005 int arr[] = { vals... };
24008 If the array has no length and has an initializer, it must be that
24009 we couldn't determine its length in cp_complete_array_type because
24010 it is dependent. */
24011 if (VAR_P (expression)
24012 && TREE_TYPE (expression) != NULL_TREE
24013 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
24014 && !TYPE_DOMAIN (TREE_TYPE (expression))
24015 && DECL_INITIAL (expression))
24016 return true;
24018 /* A function or variable template-id is type-dependent if it has any
24019 dependent template arguments. Note that we only consider the innermost
24020 template arguments here, since those are the ones that come from the
24021 template-id; the template arguments for the enclosing class do not make it
24022 type-dependent, they only make a member function value-dependent. */
24023 if (VAR_OR_FUNCTION_DECL_P (expression)
24024 && DECL_LANG_SPECIFIC (expression)
24025 && DECL_TEMPLATE_INFO (expression)
24026 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression))
24027 && (any_dependent_template_arguments_p
24028 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
24029 return true;
24031 /* Always dependent, on the number of arguments if nothing else. */
24032 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
24033 return true;
24035 if (TREE_TYPE (expression) == unknown_type_node)
24037 if (TREE_CODE (expression) == ADDR_EXPR)
24038 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
24039 if (TREE_CODE (expression) == COMPONENT_REF
24040 || TREE_CODE (expression) == OFFSET_REF)
24042 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
24043 return true;
24044 expression = TREE_OPERAND (expression, 1);
24045 if (identifier_p (expression))
24046 return false;
24048 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
24049 if (TREE_CODE (expression) == SCOPE_REF)
24050 return false;
24052 if (BASELINK_P (expression))
24054 if (BASELINK_OPTYPE (expression)
24055 && dependent_type_p (BASELINK_OPTYPE (expression)))
24056 return true;
24057 expression = BASELINK_FUNCTIONS (expression);
24060 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
24062 if (any_dependent_template_arguments_p
24063 (TREE_OPERAND (expression, 1)))
24064 return true;
24065 expression = TREE_OPERAND (expression, 0);
24066 if (identifier_p (expression))
24067 return true;
24070 gcc_assert (TREE_CODE (expression) == OVERLOAD
24071 || TREE_CODE (expression) == FUNCTION_DECL);
24073 for (lkp_iterator iter (expression); iter; ++iter)
24074 if (type_dependent_expression_p (*iter))
24075 return true;
24077 return false;
24080 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
24082 /* Dependent type attributes might not have made it from the decl to
24083 the type yet. */
24084 if (DECL_P (expression)
24085 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
24086 return true;
24088 return (dependent_type_p (TREE_TYPE (expression)));
24091 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
24092 type-dependent if the expression refers to a member of the current
24093 instantiation and the type of the referenced member is dependent, or the
24094 class member access expression refers to a member of an unknown
24095 specialization.
24097 This function returns true if the OBJECT in such a class member access
24098 expression is of an unknown specialization. */
24100 bool
24101 type_dependent_object_expression_p (tree object)
24103 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
24104 dependent. */
24105 if (TREE_CODE (object) == IDENTIFIER_NODE)
24106 return true;
24107 tree scope = TREE_TYPE (object);
24108 return (!scope || dependent_scope_p (scope));
24111 /* walk_tree callback function for instantiation_dependent_expression_p,
24112 below. Returns non-zero if a dependent subexpression is found. */
24114 static tree
24115 instantiation_dependent_r (tree *tp, int *walk_subtrees,
24116 void * /*data*/)
24118 if (TYPE_P (*tp))
24120 /* We don't have to worry about decltype currently because decltype
24121 of an instantiation-dependent expr is a dependent type. This
24122 might change depending on the resolution of DR 1172. */
24123 *walk_subtrees = false;
24124 return NULL_TREE;
24126 enum tree_code code = TREE_CODE (*tp);
24127 switch (code)
24129 /* Don't treat an argument list as dependent just because it has no
24130 TREE_TYPE. */
24131 case TREE_LIST:
24132 case TREE_VEC:
24133 return NULL_TREE;
24135 case TEMPLATE_PARM_INDEX:
24136 return *tp;
24138 /* Handle expressions with type operands. */
24139 case SIZEOF_EXPR:
24140 case ALIGNOF_EXPR:
24141 case TYPEID_EXPR:
24142 case AT_ENCODE_EXPR:
24144 tree op = TREE_OPERAND (*tp, 0);
24145 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
24146 op = TREE_TYPE (op);
24147 if (TYPE_P (op))
24149 if (dependent_type_p (op))
24150 return *tp;
24151 else
24153 *walk_subtrees = false;
24154 return NULL_TREE;
24157 break;
24160 case COMPONENT_REF:
24161 if (identifier_p (TREE_OPERAND (*tp, 1)))
24162 /* In a template, finish_class_member_access_expr creates a
24163 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
24164 type-dependent, so that we can check access control at
24165 instantiation time (PR 42277). See also Core issue 1273. */
24166 return *tp;
24167 break;
24169 case SCOPE_REF:
24170 if (instantiation_dependent_scope_ref_p (*tp))
24171 return *tp;
24172 else
24173 break;
24175 /* Treat statement-expressions as dependent. */
24176 case BIND_EXPR:
24177 return *tp;
24179 /* Treat requires-expressions as dependent. */
24180 case REQUIRES_EXPR:
24181 return *tp;
24183 case CALL_EXPR:
24184 /* Treat calls to function concepts as dependent. */
24185 if (function_concept_check_p (*tp))
24186 return *tp;
24187 break;
24189 case TEMPLATE_ID_EXPR:
24190 /* And variable concepts. */
24191 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
24192 return *tp;
24193 break;
24195 default:
24196 break;
24199 if (type_dependent_expression_p (*tp))
24200 return *tp;
24201 else
24202 return NULL_TREE;
24205 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
24206 sense defined by the ABI:
24208 "An expression is instantiation-dependent if it is type-dependent
24209 or value-dependent, or it has a subexpression that is type-dependent
24210 or value-dependent."
24212 Except don't actually check value-dependence for unevaluated expressions,
24213 because in sizeof(i) we don't care about the value of i. Checking
24214 type-dependence will in turn check value-dependence of array bounds/template
24215 arguments as needed. */
24217 bool
24218 instantiation_dependent_uneval_expression_p (tree expression)
24220 tree result;
24222 if (!processing_template_decl)
24223 return false;
24225 if (expression == error_mark_node)
24226 return false;
24228 result = cp_walk_tree_without_duplicates (&expression,
24229 instantiation_dependent_r, NULL);
24230 return result != NULL_TREE;
24233 /* As above, but also check value-dependence of the expression as a whole. */
24235 bool
24236 instantiation_dependent_expression_p (tree expression)
24238 return (instantiation_dependent_uneval_expression_p (expression)
24239 || value_dependent_expression_p (expression));
24242 /* Like type_dependent_expression_p, but it also works while not processing
24243 a template definition, i.e. during substitution or mangling. */
24245 bool
24246 type_dependent_expression_p_push (tree expr)
24248 bool b;
24249 ++processing_template_decl;
24250 b = type_dependent_expression_p (expr);
24251 --processing_template_decl;
24252 return b;
24255 /* Returns TRUE if ARGS contains a type-dependent expression. */
24257 bool
24258 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
24260 unsigned int i;
24261 tree arg;
24263 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
24265 if (type_dependent_expression_p (arg))
24266 return true;
24268 return false;
24271 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24272 expressions) contains any type-dependent expressions. */
24274 bool
24275 any_type_dependent_elements_p (const_tree list)
24277 for (; list; list = TREE_CHAIN (list))
24278 if (type_dependent_expression_p (TREE_VALUE (list)))
24279 return true;
24281 return false;
24284 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24285 expressions) contains any value-dependent expressions. */
24287 bool
24288 any_value_dependent_elements_p (const_tree list)
24290 for (; list; list = TREE_CHAIN (list))
24291 if (value_dependent_expression_p (TREE_VALUE (list)))
24292 return true;
24294 return false;
24297 /* Returns TRUE if the ARG (a template argument) is dependent. */
24299 bool
24300 dependent_template_arg_p (tree arg)
24302 if (!processing_template_decl)
24303 return false;
24305 /* Assume a template argument that was wrongly written by the user
24306 is dependent. This is consistent with what
24307 any_dependent_template_arguments_p [that calls this function]
24308 does. */
24309 if (!arg || arg == error_mark_node)
24310 return true;
24312 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
24313 arg = ARGUMENT_PACK_SELECT_ARG (arg);
24315 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
24316 return true;
24317 if (TREE_CODE (arg) == TEMPLATE_DECL)
24319 if (DECL_TEMPLATE_PARM_P (arg))
24320 return true;
24321 /* A member template of a dependent class is not necessarily
24322 type-dependent, but it is a dependent template argument because it
24323 will be a member of an unknown specialization to that template. */
24324 tree scope = CP_DECL_CONTEXT (arg);
24325 return TYPE_P (scope) && dependent_type_p (scope);
24327 else if (ARGUMENT_PACK_P (arg))
24329 tree args = ARGUMENT_PACK_ARGS (arg);
24330 int i, len = TREE_VEC_LENGTH (args);
24331 for (i = 0; i < len; ++i)
24333 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
24334 return true;
24337 return false;
24339 else if (TYPE_P (arg))
24340 return dependent_type_p (arg);
24341 else
24342 return (type_dependent_expression_p (arg)
24343 || value_dependent_expression_p (arg));
24346 /* Returns true if ARGS (a collection of template arguments) contains
24347 any types that require structural equality testing. */
24349 bool
24350 any_template_arguments_need_structural_equality_p (tree args)
24352 int i;
24353 int j;
24355 if (!args)
24356 return false;
24357 if (args == error_mark_node)
24358 return true;
24360 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24362 tree level = TMPL_ARGS_LEVEL (args, i + 1);
24363 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24365 tree arg = TREE_VEC_ELT (level, j);
24366 tree packed_args = NULL_TREE;
24367 int k, len = 1;
24369 if (ARGUMENT_PACK_P (arg))
24371 /* Look inside the argument pack. */
24372 packed_args = ARGUMENT_PACK_ARGS (arg);
24373 len = TREE_VEC_LENGTH (packed_args);
24376 for (k = 0; k < len; ++k)
24378 if (packed_args)
24379 arg = TREE_VEC_ELT (packed_args, k);
24381 if (error_operand_p (arg))
24382 return true;
24383 else if (TREE_CODE (arg) == TEMPLATE_DECL)
24384 continue;
24385 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
24386 return true;
24387 else if (!TYPE_P (arg) && TREE_TYPE (arg)
24388 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
24389 return true;
24394 return false;
24397 /* Returns true if ARGS (a collection of template arguments) contains
24398 any dependent arguments. */
24400 bool
24401 any_dependent_template_arguments_p (const_tree args)
24403 int i;
24404 int j;
24406 if (!args)
24407 return false;
24408 if (args == error_mark_node)
24409 return true;
24411 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
24413 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
24414 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
24415 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
24416 return true;
24419 return false;
24422 /* Returns TRUE if the template TMPL is type-dependent. */
24424 bool
24425 dependent_template_p (tree tmpl)
24427 if (TREE_CODE (tmpl) == OVERLOAD)
24429 for (lkp_iterator iter (tmpl); iter; ++iter)
24430 if (dependent_template_p (*iter))
24431 return true;
24432 return false;
24435 /* Template template parameters are dependent. */
24436 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
24437 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
24438 return true;
24439 /* So are names that have not been looked up. */
24440 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
24441 return true;
24442 return false;
24445 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
24447 bool
24448 dependent_template_id_p (tree tmpl, tree args)
24450 return (dependent_template_p (tmpl)
24451 || any_dependent_template_arguments_p (args));
24454 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
24455 are dependent. */
24457 bool
24458 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
24460 int i;
24462 if (!processing_template_decl)
24463 return false;
24465 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
24467 tree decl = TREE_VEC_ELT (declv, i);
24468 tree init = TREE_VEC_ELT (initv, i);
24469 tree cond = TREE_VEC_ELT (condv, i);
24470 tree incr = TREE_VEC_ELT (incrv, i);
24472 if (type_dependent_expression_p (decl)
24473 || TREE_CODE (decl) == SCOPE_REF)
24474 return true;
24476 if (init && type_dependent_expression_p (init))
24477 return true;
24479 if (type_dependent_expression_p (cond))
24480 return true;
24482 if (COMPARISON_CLASS_P (cond)
24483 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
24484 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
24485 return true;
24487 if (TREE_CODE (incr) == MODOP_EXPR)
24489 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
24490 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
24491 return true;
24493 else if (type_dependent_expression_p (incr))
24494 return true;
24495 else if (TREE_CODE (incr) == MODIFY_EXPR)
24497 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
24498 return true;
24499 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
24501 tree t = TREE_OPERAND (incr, 1);
24502 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
24503 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
24504 return true;
24509 return false;
24512 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
24513 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
24514 no such TYPE can be found. Note that this function peers inside
24515 uninstantiated templates and therefore should be used only in
24516 extremely limited situations. ONLY_CURRENT_P restricts this
24517 peering to the currently open classes hierarchy (which is required
24518 when comparing types). */
24520 tree
24521 resolve_typename_type (tree type, bool only_current_p)
24523 tree scope;
24524 tree name;
24525 tree decl;
24526 int quals;
24527 tree pushed_scope;
24528 tree result;
24530 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
24532 scope = TYPE_CONTEXT (type);
24533 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
24534 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
24535 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
24536 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
24537 identifier of the TYPENAME_TYPE anymore.
24538 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
24539 TYPENAME_TYPE instead, we avoid messing up with a possible
24540 typedef variant case. */
24541 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
24543 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
24544 it first before we can figure out what NAME refers to. */
24545 if (TREE_CODE (scope) == TYPENAME_TYPE)
24547 if (TYPENAME_IS_RESOLVING_P (scope))
24548 /* Given a class template A with a dependent base with nested type C,
24549 typedef typename A::C::C C will land us here, as trying to resolve
24550 the initial A::C leads to the local C typedef, which leads back to
24551 A::C::C. So we break the recursion now. */
24552 return type;
24553 else
24554 scope = resolve_typename_type (scope, only_current_p);
24556 /* If we don't know what SCOPE refers to, then we cannot resolve the
24557 TYPENAME_TYPE. */
24558 if (!CLASS_TYPE_P (scope))
24559 return type;
24560 /* If this is a typedef, we don't want to look inside (c++/11987). */
24561 if (typedef_variant_p (type))
24562 return type;
24563 /* If SCOPE isn't the template itself, it will not have a valid
24564 TYPE_FIELDS list. */
24565 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
24566 /* scope is either the template itself or a compatible instantiation
24567 like X<T>, so look up the name in the original template. */
24568 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
24569 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
24570 gcc_checking_assert (uses_template_parms (scope));
24571 /* If scope has no fields, it can't be a current instantiation. Check this
24572 before currently_open_class to avoid infinite recursion (71515). */
24573 if (!TYPE_FIELDS (scope))
24574 return type;
24575 /* If the SCOPE is not the current instantiation, there's no reason
24576 to look inside it. */
24577 if (only_current_p && !currently_open_class (scope))
24578 return type;
24579 /* Enter the SCOPE so that name lookup will be resolved as if we
24580 were in the class definition. In particular, SCOPE will no
24581 longer be considered a dependent type. */
24582 pushed_scope = push_scope (scope);
24583 /* Look up the declaration. */
24584 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
24585 tf_warning_or_error);
24587 result = NULL_TREE;
24589 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
24590 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
24591 if (!decl)
24592 /*nop*/;
24593 else if (identifier_p (TYPENAME_TYPE_FULLNAME (type))
24594 && TREE_CODE (decl) == TYPE_DECL)
24596 result = TREE_TYPE (decl);
24597 if (result == error_mark_node)
24598 result = NULL_TREE;
24600 else if (TREE_CODE (TYPENAME_TYPE_FULLNAME (type)) == TEMPLATE_ID_EXPR
24601 && DECL_CLASS_TEMPLATE_P (decl))
24603 tree tmpl;
24604 tree args;
24605 /* Obtain the template and the arguments. */
24606 tmpl = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 0);
24607 args = TREE_OPERAND (TYPENAME_TYPE_FULLNAME (type), 1);
24608 /* Instantiate the template. */
24609 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
24610 /*entering_scope=*/0,
24611 tf_error | tf_user);
24612 if (result == error_mark_node)
24613 result = NULL_TREE;
24616 /* Leave the SCOPE. */
24617 if (pushed_scope)
24618 pop_scope (pushed_scope);
24620 /* If we failed to resolve it, return the original typename. */
24621 if (!result)
24622 return type;
24624 /* If lookup found a typename type, resolve that too. */
24625 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
24627 /* Ill-formed programs can cause infinite recursion here, so we
24628 must catch that. */
24629 TYPENAME_IS_RESOLVING_P (result) = 1;
24630 result = resolve_typename_type (result, only_current_p);
24631 TYPENAME_IS_RESOLVING_P (result) = 0;
24634 /* Qualify the resulting type. */
24635 quals = cp_type_quals (type);
24636 if (quals)
24637 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
24639 return result;
24642 /* EXPR is an expression which is not type-dependent. Return a proxy
24643 for EXPR that can be used to compute the types of larger
24644 expressions containing EXPR. */
24646 tree
24647 build_non_dependent_expr (tree expr)
24649 tree inner_expr;
24651 /* When checking, try to get a constant value for all non-dependent
24652 expressions in order to expose bugs in *_dependent_expression_p
24653 and constexpr. This can affect code generation, see PR70704, so
24654 only do this for -fchecking=2. */
24655 if (flag_checking > 1
24656 && cxx_dialect >= cxx11
24657 /* Don't do this during nsdmi parsing as it can lead to
24658 unexpected recursive instantiations. */
24659 && !parsing_nsdmi ()
24660 /* Don't do this during concept expansion either and for
24661 the same reason. */
24662 && !expanding_concept ())
24663 fold_non_dependent_expr (expr);
24665 /* Preserve OVERLOADs; the functions must be available to resolve
24666 types. */
24667 inner_expr = expr;
24668 if (TREE_CODE (inner_expr) == STMT_EXPR)
24669 inner_expr = stmt_expr_value_expr (inner_expr);
24670 if (TREE_CODE (inner_expr) == ADDR_EXPR)
24671 inner_expr = TREE_OPERAND (inner_expr, 0);
24672 if (TREE_CODE (inner_expr) == COMPONENT_REF)
24673 inner_expr = TREE_OPERAND (inner_expr, 1);
24674 if (is_overloaded_fn (inner_expr)
24675 || TREE_CODE (inner_expr) == OFFSET_REF)
24676 return expr;
24677 /* There is no need to return a proxy for a variable. */
24678 if (VAR_P (expr))
24679 return expr;
24680 /* Preserve string constants; conversions from string constants to
24681 "char *" are allowed, even though normally a "const char *"
24682 cannot be used to initialize a "char *". */
24683 if (TREE_CODE (expr) == STRING_CST)
24684 return expr;
24685 /* Preserve void and arithmetic constants, as an optimization -- there is no
24686 reason to create a new node. */
24687 if (TREE_CODE (expr) == VOID_CST
24688 || TREE_CODE (expr) == INTEGER_CST
24689 || TREE_CODE (expr) == REAL_CST)
24690 return expr;
24691 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
24692 There is at least one place where we want to know that a
24693 particular expression is a throw-expression: when checking a ?:
24694 expression, there are special rules if the second or third
24695 argument is a throw-expression. */
24696 if (TREE_CODE (expr) == THROW_EXPR)
24697 return expr;
24699 /* Don't wrap an initializer list, we need to be able to look inside. */
24700 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
24701 return expr;
24703 /* Don't wrap a dummy object, we need to be able to test for it. */
24704 if (is_dummy_object (expr))
24705 return expr;
24707 if (TREE_CODE (expr) == COND_EXPR)
24708 return build3 (COND_EXPR,
24709 TREE_TYPE (expr),
24710 TREE_OPERAND (expr, 0),
24711 (TREE_OPERAND (expr, 1)
24712 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
24713 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
24714 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
24715 if (TREE_CODE (expr) == COMPOUND_EXPR
24716 && !COMPOUND_EXPR_OVERLOADED (expr))
24717 return build2 (COMPOUND_EXPR,
24718 TREE_TYPE (expr),
24719 TREE_OPERAND (expr, 0),
24720 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
24722 /* If the type is unknown, it can't really be non-dependent */
24723 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
24725 /* Otherwise, build a NON_DEPENDENT_EXPR. */
24726 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
24729 /* ARGS is a vector of expressions as arguments to a function call.
24730 Replace the arguments with equivalent non-dependent expressions.
24731 This modifies ARGS in place. */
24733 void
24734 make_args_non_dependent (vec<tree, va_gc> *args)
24736 unsigned int ix;
24737 tree arg;
24739 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
24741 tree newarg = build_non_dependent_expr (arg);
24742 if (newarg != arg)
24743 (*args)[ix] = newarg;
24747 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
24748 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
24749 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
24751 static tree
24752 make_auto_1 (tree name, bool set_canonical)
24754 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
24755 TYPE_NAME (au) = build_decl (input_location,
24756 TYPE_DECL, name, au);
24757 TYPE_STUB_DECL (au) = TYPE_NAME (au);
24758 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
24759 (0, processing_template_decl + 1, processing_template_decl + 1,
24760 TYPE_NAME (au), NULL_TREE);
24761 if (set_canonical)
24762 TYPE_CANONICAL (au) = canonical_type_parameter (au);
24763 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
24764 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
24766 return au;
24769 tree
24770 make_decltype_auto (void)
24772 return make_auto_1 (decltype_auto_identifier, true);
24775 tree
24776 make_auto (void)
24778 return make_auto_1 (auto_identifier, true);
24781 /* Return a C++17 deduction placeholder for class template TMPL. */
24783 tree
24784 make_template_placeholder (tree tmpl)
24786 tree t = make_auto_1 (DECL_NAME (tmpl), true);
24787 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
24788 return t;
24791 /* Make a "constrained auto" type-specifier. This is an
24792 auto type with constraints that must be associated after
24793 deduction. The constraint is formed from the given
24794 CONC and its optional sequence of arguments, which are
24795 non-null if written as partial-concept-id. */
24797 tree
24798 make_constrained_auto (tree con, tree args)
24800 tree type = make_auto_1 (auto_identifier, false);
24802 /* Build the constraint. */
24803 tree tmpl = DECL_TI_TEMPLATE (con);
24804 tree expr = VAR_P (con) ? tmpl : ovl_make (tmpl);
24805 expr = build_concept_check (expr, type, args);
24807 tree constr = normalize_expression (expr);
24808 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
24810 /* Our canonical type depends on the constraint. */
24811 TYPE_CANONICAL (type) = canonical_type_parameter (type);
24813 /* Attach the constraint to the type declaration. */
24814 tree decl = TYPE_NAME (type);
24815 return decl;
24818 /* Given type ARG, return std::initializer_list<ARG>. */
24820 static tree
24821 listify (tree arg)
24823 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
24825 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
24827 error ("deducing from brace-enclosed initializer list requires "
24828 "#include <initializer_list>");
24829 return error_mark_node;
24831 tree argvec = make_tree_vec (1);
24832 TREE_VEC_ELT (argvec, 0) = arg;
24834 return lookup_template_class (std_init_list, argvec, NULL_TREE,
24835 NULL_TREE, 0, tf_warning_or_error);
24838 /* Replace auto in TYPE with std::initializer_list<auto>. */
24840 static tree
24841 listify_autos (tree type, tree auto_node)
24843 tree init_auto = listify (auto_node);
24844 tree argvec = make_tree_vec (1);
24845 TREE_VEC_ELT (argvec, 0) = init_auto;
24846 if (processing_template_decl)
24847 argvec = add_to_template_args (current_template_args (), argvec);
24848 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
24851 /* Hash traits for hashing possibly constrained 'auto'
24852 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
24854 struct auto_hash : default_hash_traits<tree>
24856 static inline hashval_t hash (tree);
24857 static inline bool equal (tree, tree);
24860 /* Hash the 'auto' T. */
24862 inline hashval_t
24863 auto_hash::hash (tree t)
24865 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
24866 /* Matching constrained-type-specifiers denote the same template
24867 parameter, so hash the constraint. */
24868 return hash_placeholder_constraint (c);
24869 else
24870 /* But unconstrained autos are all separate, so just hash the pointer. */
24871 return iterative_hash_object (t, 0);
24874 /* Compare two 'auto's. */
24876 inline bool
24877 auto_hash::equal (tree t1, tree t2)
24879 if (t1 == t2)
24880 return true;
24882 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
24883 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
24885 /* Two unconstrained autos are distinct. */
24886 if (!c1 || !c2)
24887 return false;
24889 return equivalent_placeholder_constraints (c1, c2);
24892 /* for_each_template_parm callback for extract_autos: if t is a (possibly
24893 constrained) auto, add it to the vector. */
24895 static int
24896 extract_autos_r (tree t, void *data)
24898 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
24899 if (is_auto (t))
24901 /* All the autos were built with index 0; fix that up now. */
24902 tree *p = hash.find_slot (t, INSERT);
24903 unsigned idx;
24904 if (*p)
24905 /* If this is a repeated constrained-type-specifier, use the index we
24906 chose before. */
24907 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
24908 else
24910 /* Otherwise this is new, so use the current count. */
24911 *p = t;
24912 idx = hash.elements () - 1;
24914 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
24917 /* Always keep walking. */
24918 return 0;
24921 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
24922 says they can appear anywhere in the type. */
24924 static tree
24925 extract_autos (tree type)
24927 hash_set<tree> visited;
24928 hash_table<auto_hash> hash (2);
24930 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
24932 tree tree_vec = make_tree_vec (hash.elements());
24933 for (hash_table<auto_hash>::iterator iter = hash.begin();
24934 iter != hash.end(); ++iter)
24936 tree elt = *iter;
24937 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
24938 TREE_VEC_ELT (tree_vec, i)
24939 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
24942 return tree_vec;
24945 /* The stem for deduction guide names. */
24946 const char *const dguide_base = "__dguide_";
24948 /* Return the name for a deduction guide for class template TMPL. */
24950 tree
24951 dguide_name (tree tmpl)
24953 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
24954 tree tname = TYPE_IDENTIFIER (type);
24955 char *buf = (char *) alloca (1 + strlen (dguide_base)
24956 + IDENTIFIER_LENGTH (tname));
24957 memcpy (buf, dguide_base, strlen (dguide_base));
24958 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
24959 IDENTIFIER_LENGTH (tname) + 1);
24960 tree dname = get_identifier (buf);
24961 TREE_TYPE (dname) = type;
24962 return dname;
24965 /* True if NAME is the name of a deduction guide. */
24967 bool
24968 dguide_name_p (tree name)
24970 return (TREE_TYPE (name)
24971 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
24972 strlen (dguide_base)));
24975 /* True if FN is a deduction guide. */
24977 bool
24978 deduction_guide_p (const_tree fn)
24980 if (DECL_P (fn))
24981 if (tree name = DECL_NAME (fn))
24982 return dguide_name_p (name);
24983 return false;
24986 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
24988 bool
24989 copy_guide_p (const_tree fn)
24991 gcc_assert (deduction_guide_p (fn));
24992 if (!DECL_ARTIFICIAL (fn))
24993 return false;
24994 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
24995 return (TREE_CHAIN (parms) == void_list_node
24996 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
24999 /* True if FN is a guide generated from a constructor template. */
25001 bool
25002 template_guide_p (const_tree fn)
25004 gcc_assert (deduction_guide_p (fn));
25005 if (!DECL_ARTIFICIAL (fn))
25006 return false;
25007 if (tree ctor = DECL_ABSTRACT_ORIGIN (fn))
25009 tree tmpl = DECL_TI_TEMPLATE (ctor);
25010 return PRIMARY_TEMPLATE_P (tmpl);
25012 return false;
25015 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
25016 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
25017 template parameter types. Note that the handling of template template
25018 parameters relies on current_template_parms being set appropriately for the
25019 new template. */
25021 static tree
25022 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
25023 tree tsubst_args, tsubst_flags_t complain)
25025 tree oldidx = get_template_parm_index (olddecl);
25027 tree newtype;
25028 if (TREE_CODE (olddecl) == TYPE_DECL
25029 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25031 tree oldtype = TREE_TYPE (olddecl);
25032 newtype = cxx_make_type (TREE_CODE (oldtype));
25033 TYPE_MAIN_VARIANT (newtype) = newtype;
25034 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
25035 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
25036 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
25038 else
25039 newtype = tsubst (TREE_TYPE (olddecl), tsubst_args,
25040 complain, NULL_TREE);
25042 tree newdecl
25043 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
25044 DECL_NAME (olddecl), newtype);
25045 SET_DECL_TEMPLATE_PARM_P (newdecl);
25047 tree newidx;
25048 if (TREE_CODE (olddecl) == TYPE_DECL
25049 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25051 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
25052 = build_template_parm_index (index, level, level,
25053 newdecl, newtype);
25054 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25055 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25056 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
25057 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
25059 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
25061 DECL_TEMPLATE_RESULT (newdecl)
25062 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
25063 DECL_NAME (olddecl), newtype);
25064 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
25065 // First create a copy (ttargs) of tsubst_args with an
25066 // additional level for the template template parameter's own
25067 // template parameters (ttparms).
25068 tree ttparms = (INNERMOST_TEMPLATE_PARMS
25069 (DECL_TEMPLATE_PARMS (olddecl)));
25070 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
25071 tree ttargs = make_tree_vec (depth + 1);
25072 for (int i = 0; i < depth; ++i)
25073 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
25074 TREE_VEC_ELT (ttargs, depth)
25075 = template_parms_level_to_args (ttparms);
25076 // Substitute ttargs into ttparms to fix references to
25077 // other template parameters.
25078 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25079 complain);
25080 // Now substitute again with args based on tparms, to reduce
25081 // the level of the ttparms.
25082 ttargs = current_template_args ();
25083 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25084 complain);
25085 // Finally, tack the adjusted parms onto tparms.
25086 ttparms = tree_cons (size_int (depth), ttparms,
25087 current_template_parms);
25088 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
25091 else
25093 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
25094 tree newconst
25095 = build_decl (DECL_SOURCE_LOCATION (oldconst),
25096 TREE_CODE (oldconst),
25097 DECL_NAME (oldconst), newtype);
25098 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
25099 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
25100 SET_DECL_TEMPLATE_PARM_P (newconst);
25101 newidx = build_template_parm_index (index, level, level,
25102 newconst, newtype);
25103 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25104 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25105 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
25108 return newdecl;
25111 /* Returns a C++17 class deduction guide template based on the constructor
25112 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
25113 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
25115 static tree
25116 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
25118 tree type, tparms, targs, fparms, fargs, ci;
25119 bool memtmpl = false;
25120 bool explicit_p;
25121 location_t loc;
25123 if (TYPE_P (ctor))
25125 type = ctor;
25126 bool copy_p = TREE_CODE (type) == REFERENCE_TYPE;
25127 if (copy_p)
25129 type = TREE_TYPE (type);
25130 fparms = tree_cons (NULL_TREE, type, void_list_node);
25132 else
25133 fparms = void_list_node;
25135 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
25136 tparms = DECL_TEMPLATE_PARMS (ctmpl);
25137 targs = CLASSTYPE_TI_ARGS (type);
25138 ci = NULL_TREE;
25139 fargs = NULL_TREE;
25140 loc = DECL_SOURCE_LOCATION (ctmpl);
25141 explicit_p = false;
25143 else
25145 if (outer_args)
25146 ctor = tsubst (ctor, outer_args, complain, ctor);
25147 type = DECL_CONTEXT (ctor);
25148 tree fn_tmpl;
25149 if (TREE_CODE (ctor) == TEMPLATE_DECL)
25151 fn_tmpl = ctor;
25152 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
25154 else
25155 fn_tmpl = DECL_TI_TEMPLATE (ctor);
25157 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
25158 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
25159 fully specialized args for the enclosing class. Strip those off, as
25160 the deduction guide won't have those template parameters. */
25161 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
25162 TMPL_PARMS_DEPTH (tparms));
25163 /* Discard the 'this' parameter. */
25164 fparms = FUNCTION_ARG_CHAIN (ctor);
25165 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
25166 ci = get_constraints (ctor);
25167 loc = DECL_SOURCE_LOCATION (ctor);
25168 explicit_p = DECL_NONCONVERTING_P (ctor);
25170 if (PRIMARY_TEMPLATE_P (fn_tmpl))
25172 memtmpl = true;
25174 /* For a member template constructor, we need to flatten the two
25175 template parameter lists into one, and then adjust the function
25176 signature accordingly. This gets...complicated. */
25177 ++processing_template_decl;
25178 tree save_parms = current_template_parms;
25180 /* For a member template we should have two levels of parms/args, one
25181 for the class and one for the constructor. We stripped
25182 specialized args for further enclosing classes above. */
25183 const int depth = 2;
25184 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
25186 /* Template args for translating references to the two-level template
25187 parameters into references to the one-level template parameters we
25188 are creating. */
25189 tree tsubst_args = copy_node (targs);
25190 TMPL_ARGS_LEVEL (tsubst_args, depth)
25191 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
25193 /* Template parms for the constructor template. */
25194 tree ftparms = TREE_VALUE (tparms);
25195 unsigned flen = TREE_VEC_LENGTH (ftparms);
25196 /* Template parms for the class template. */
25197 tparms = TREE_CHAIN (tparms);
25198 tree ctparms = TREE_VALUE (tparms);
25199 unsigned clen = TREE_VEC_LENGTH (ctparms);
25200 /* Template parms for the deduction guide start as a copy of the
25201 template parms for the class. We set current_template_parms for
25202 lookup_template_class_1. */
25203 current_template_parms = tparms = copy_node (tparms);
25204 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
25205 for (unsigned i = 0; i < clen; ++i)
25206 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
25208 /* Now we need to rewrite the constructor parms to append them to the
25209 class parms. */
25210 for (unsigned i = 0; i < flen; ++i)
25212 unsigned index = i + clen;
25213 unsigned level = 1;
25214 tree oldelt = TREE_VEC_ELT (ftparms, i);
25215 tree olddecl = TREE_VALUE (oldelt);
25216 tree newdecl = rewrite_template_parm (olddecl, index, level,
25217 tsubst_args, complain);
25218 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
25219 tsubst_args, complain, ctor);
25220 tree list = build_tree_list (newdef, newdecl);
25221 TEMPLATE_PARM_CONSTRAINTS (list)
25222 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
25223 tsubst_args, complain, ctor);
25224 TREE_VEC_ELT (new_vec, index) = list;
25225 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
25228 /* Now we have a final set of template parms to substitute into the
25229 function signature. */
25230 targs = template_parms_to_args (tparms);
25231 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
25232 complain, ctor);
25233 fargs = tsubst (fargs, tsubst_args, complain, ctor);
25234 if (ci)
25235 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
25237 current_template_parms = save_parms;
25238 --processing_template_decl;
25242 if (!memtmpl)
25244 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
25245 tparms = copy_node (tparms);
25246 INNERMOST_TEMPLATE_PARMS (tparms)
25247 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
25250 tree fntype = build_function_type (type, fparms);
25251 tree ded_fn = build_lang_decl_loc (loc,
25252 FUNCTION_DECL,
25253 dguide_name (type), fntype);
25254 DECL_ARGUMENTS (ded_fn) = fargs;
25255 DECL_ARTIFICIAL (ded_fn) = true;
25256 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
25257 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
25258 DECL_ARTIFICIAL (ded_tmpl) = true;
25259 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
25260 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
25261 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
25262 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
25263 if (DECL_P (ctor))
25264 DECL_ABSTRACT_ORIGIN (ded_fn) = ctor;
25265 if (ci)
25266 set_constraints (ded_tmpl, ci);
25268 return ded_tmpl;
25271 /* Deduce template arguments for the class template placeholder PTYPE for
25272 template TMPL based on the initializer INIT, and return the resulting
25273 type. */
25275 static tree
25276 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
25277 tsubst_flags_t complain)
25279 if (!DECL_CLASS_TEMPLATE_P (tmpl))
25281 /* We should have handled this in the caller. */
25282 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
25283 return ptype;
25284 if (complain & tf_error)
25285 error ("non-class template %qT used without template arguments", tmpl);
25286 return error_mark_node;
25289 tree type = TREE_TYPE (tmpl);
25291 vec<tree,va_gc> *args;
25292 if (init == NULL_TREE
25293 || TREE_CODE (init) == TREE_LIST)
25294 args = make_tree_vector_from_list (init);
25295 else if (BRACE_ENCLOSED_INITIALIZER_P (init)
25296 && !TYPE_HAS_LIST_CTOR (type)
25297 && !is_std_init_list (type))
25298 args = make_tree_vector_from_ctor (init);
25299 else
25300 args = make_tree_vector_single (init);
25302 tree dname = dguide_name (tmpl);
25303 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
25304 /*type*/false, /*complain*/false,
25305 /*hidden*/false);
25306 bool elided = false;
25307 if (cands == error_mark_node)
25308 cands = NULL_TREE;
25310 /* Prune explicit deduction guides in copy-initialization context. */
25311 if (flags & LOOKUP_ONLYCONVERTING)
25313 for (lkp_iterator iter (cands); !elided && iter; ++iter)
25314 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25315 elided = true;
25317 if (elided)
25319 /* Found a nonconverting guide, prune the candidates. */
25320 tree pruned = NULL_TREE;
25321 for (lkp_iterator iter (cands); iter; ++iter)
25322 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
25323 pruned = lookup_add (*iter, pruned);
25325 cands = pruned;
25329 tree outer_args = NULL_TREE;
25330 if (DECL_CLASS_SCOPE_P (tmpl)
25331 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl)))
25333 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
25334 type = TREE_TYPE (most_general_template (tmpl));
25337 bool saw_ctor = false;
25338 if (CLASSTYPE_METHOD_VEC (type))
25339 // FIXME cache artificial deduction guides
25340 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type));
25341 iter; ++iter)
25343 tree guide = build_deduction_guide (*iter, outer_args, complain);
25344 if ((flags & LOOKUP_ONLYCONVERTING)
25345 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
25346 elided = true;
25347 else
25348 cands = lookup_add (guide, cands);
25350 saw_ctor = true;
25353 if (args->length () < 2)
25355 tree gtype = NULL_TREE;
25357 if (args->length () == 1)
25358 gtype = build_reference_type (type);
25359 else if (!saw_ctor)
25360 gtype = type;
25362 if (gtype)
25364 tree guide = build_deduction_guide (gtype, outer_args, complain);
25365 if ((flags & LOOKUP_ONLYCONVERTING)
25366 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
25367 elided = true;
25368 else
25369 cands = lookup_add (guide, cands);
25373 if (elided && !cands)
25375 error ("cannot deduce template arguments for copy-initialization"
25376 " of %qT, as it has no non-explicit deduction guides or "
25377 "user-declared constructors", type);
25378 return error_mark_node;
25381 ++cp_unevaluated_operand;
25382 tree t = build_new_function_call (cands, &args, tf_decltype);
25384 if (t == error_mark_node && (complain & tf_warning_or_error))
25386 error ("class template argument deduction failed:");
25387 t = build_new_function_call (cands, &args, complain | tf_decltype);
25388 if (elided)
25389 inform (input_location, "explicit deduction guides not considered "
25390 "for copy-initialization");
25393 --cp_unevaluated_operand;
25394 release_tree_vector (args);
25396 return cp_build_qualified_type (TREE_TYPE (t), cp_type_quals (ptype));
25399 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25400 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. */
25402 tree
25403 do_auto_deduction (tree type, tree init, tree auto_node)
25405 return do_auto_deduction (type, init, auto_node,
25406 tf_warning_or_error,
25407 adc_unspecified);
25410 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
25411 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
25412 The CONTEXT determines the context in which auto deduction is performed
25413 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
25414 OUTER_TARGS are used during template argument deduction
25415 (context == adc_unify) to properly substitute the result, and is ignored
25416 in other contexts.
25418 For partial-concept-ids, extra args may be appended to the list of deduced
25419 template arguments prior to determining constraint satisfaction. */
25421 tree
25422 do_auto_deduction (tree type, tree init, tree auto_node,
25423 tsubst_flags_t complain, auto_deduction_context context,
25424 tree outer_targs, int flags)
25426 tree targs;
25428 if (init == error_mark_node)
25429 return error_mark_node;
25431 if (init && type_dependent_expression_p (init)
25432 && context != adc_unify)
25433 /* Defining a subset of type-dependent expressions that we can deduce
25434 from ahead of time isn't worth the trouble. */
25435 return type;
25437 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
25438 /* C++17 class template argument deduction. */
25439 return do_class_deduction (type, tmpl, init, flags, complain);
25441 if (TREE_TYPE (init) == NULL_TREE)
25442 /* Nothing we can do with this, even in deduction context. */
25443 return type;
25445 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
25446 with either a new invented type template parameter U or, if the
25447 initializer is a braced-init-list (8.5.4), with
25448 std::initializer_list<U>. */
25449 if (BRACE_ENCLOSED_INITIALIZER_P (init))
25451 if (!DIRECT_LIST_INIT_P (init))
25452 type = listify_autos (type, auto_node);
25453 else if (CONSTRUCTOR_NELTS (init) == 1)
25454 init = CONSTRUCTOR_ELT (init, 0)->value;
25455 else
25457 if (complain & tf_warning_or_error)
25459 if (permerror (input_location, "direct-list-initialization of "
25460 "%<auto%> requires exactly one element"))
25461 inform (input_location,
25462 "for deduction to %<std::initializer_list%>, use copy-"
25463 "list-initialization (i.e. add %<=%> before the %<{%>)");
25465 type = listify_autos (type, auto_node);
25469 if (type == error_mark_node)
25470 return error_mark_node;
25472 init = resolve_nondeduced_context (init, complain);
25474 if (context == adc_decomp_type
25475 && auto_node == type
25476 && init != error_mark_node
25477 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
25478 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
25479 and initializer has array type, deduce cv-qualified array type. */
25480 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
25481 complain);
25482 else if (AUTO_IS_DECLTYPE (auto_node))
25484 bool id = (DECL_P (init)
25485 || ((TREE_CODE (init) == COMPONENT_REF
25486 || TREE_CODE (init) == SCOPE_REF)
25487 && !REF_PARENTHESIZED_P (init)));
25488 targs = make_tree_vec (1);
25489 TREE_VEC_ELT (targs, 0)
25490 = finish_decltype_type (init, id, tf_warning_or_error);
25491 if (type != auto_node)
25493 if (complain & tf_error)
25494 error ("%qT as type rather than plain %<decltype(auto)%>", type);
25495 return error_mark_node;
25498 else
25500 tree parms = build_tree_list (NULL_TREE, type);
25501 tree tparms;
25503 if (flag_concepts)
25504 tparms = extract_autos (type);
25505 else
25507 tparms = make_tree_vec (1);
25508 TREE_VEC_ELT (tparms, 0)
25509 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
25512 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
25513 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
25514 DEDUCE_CALL, LOOKUP_NORMAL,
25515 NULL, /*explain_p=*/false);
25516 if (val > 0)
25518 if (processing_template_decl)
25519 /* Try again at instantiation time. */
25520 return type;
25521 if (type && type != error_mark_node
25522 && (complain & tf_error))
25523 /* If type is error_mark_node a diagnostic must have been
25524 emitted by now. Also, having a mention to '<type error>'
25525 in the diagnostic is not really useful to the user. */
25527 if (cfun && auto_node == current_function_auto_return_pattern
25528 && LAMBDA_FUNCTION_P (current_function_decl))
25529 error ("unable to deduce lambda return type from %qE", init);
25530 else
25531 error ("unable to deduce %qT from %qE", type, init);
25532 type_unification_real (tparms, targs, parms, &init, 1, 0,
25533 DEDUCE_CALL, LOOKUP_NORMAL,
25534 NULL, /*explain_p=*/true);
25536 return error_mark_node;
25540 /* Check any placeholder constraints against the deduced type. */
25541 if (flag_concepts && !processing_template_decl)
25542 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (auto_node))
25544 /* Use the deduced type to check the associated constraints. If we
25545 have a partial-concept-id, rebuild the argument list so that
25546 we check using the extra arguments. */
25547 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
25548 tree cargs = CHECK_CONSTR_ARGS (constr);
25549 if (TREE_VEC_LENGTH (cargs) > 1)
25551 cargs = copy_node (cargs);
25552 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
25554 else
25555 cargs = targs;
25556 if (!constraints_satisfied_p (constr, cargs))
25558 if (complain & tf_warning_or_error)
25560 switch (context)
25562 case adc_unspecified:
25563 case adc_unify:
25564 error("placeholder constraints not satisfied");
25565 break;
25566 case adc_variable_type:
25567 case adc_decomp_type:
25568 error ("deduced initializer does not satisfy "
25569 "placeholder constraints");
25570 break;
25571 case adc_return_type:
25572 error ("deduced return type does not satisfy "
25573 "placeholder constraints");
25574 break;
25575 case adc_requirement:
25576 error ("deduced expression type does not satisfy "
25577 "placeholder constraints");
25578 break;
25580 diagnose_constraints (input_location, constr, targs);
25582 return error_mark_node;
25586 if (processing_template_decl && context != adc_unify)
25587 outer_targs = current_template_args ();
25588 targs = add_to_template_args (outer_targs, targs);
25589 return tsubst (type, targs, complain, NULL_TREE);
25592 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
25593 result. */
25595 tree
25596 splice_late_return_type (tree type, tree late_return_type)
25598 if (is_auto (type))
25600 if (late_return_type)
25601 return late_return_type;
25603 tree idx = get_template_parm_index (type);
25604 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
25605 /* In an abbreviated function template we didn't know we were dealing
25606 with a function template when we saw the auto return type, so update
25607 it to have the correct level. */
25608 return make_auto_1 (TYPE_IDENTIFIER (type), true);
25610 return type;
25613 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
25614 'decltype(auto)' or a deduced class template. */
25616 bool
25617 is_auto (const_tree type)
25619 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
25620 && (TYPE_IDENTIFIER (type) == auto_identifier
25621 || TYPE_IDENTIFIER (type) == decltype_auto_identifier
25622 || CLASS_PLACEHOLDER_TEMPLATE (type)))
25623 return true;
25624 else
25625 return false;
25628 /* for_each_template_parm callback for type_uses_auto. */
25631 is_auto_r (tree tp, void */*data*/)
25633 return is_auto (tp);
25636 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
25637 a use of `auto'. Returns NULL_TREE otherwise. */
25639 tree
25640 type_uses_auto (tree type)
25642 if (type == NULL_TREE)
25643 return NULL_TREE;
25644 else if (flag_concepts)
25646 /* The Concepts TS allows multiple autos in one type-specifier; just
25647 return the first one we find, do_auto_deduction will collect all of
25648 them. */
25649 if (uses_template_parms (type))
25650 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
25651 /*visited*/NULL, /*nondeduced*/true);
25652 else
25653 return NULL_TREE;
25655 else
25656 return find_type_usage (type, is_auto);
25659 /* For a given template T, return the vector of typedefs referenced
25660 in T for which access check is needed at T instantiation time.
25661 T is either a FUNCTION_DECL or a RECORD_TYPE.
25662 Those typedefs were added to T by the function
25663 append_type_to_template_for_access_check. */
25665 vec<qualified_typedef_usage_t, va_gc> *
25666 get_types_needing_access_check (tree t)
25668 tree ti;
25669 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
25671 if (!t || t == error_mark_node)
25672 return NULL;
25674 if (!(ti = get_template_info (t)))
25675 return NULL;
25677 if (CLASS_TYPE_P (t)
25678 || TREE_CODE (t) == FUNCTION_DECL)
25680 if (!TI_TEMPLATE (ti))
25681 return NULL;
25683 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
25686 return result;
25689 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
25690 tied to T. That list of typedefs will be access checked at
25691 T instantiation time.
25692 T is either a FUNCTION_DECL or a RECORD_TYPE.
25693 TYPE_DECL is a TYPE_DECL node representing a typedef.
25694 SCOPE is the scope through which TYPE_DECL is accessed.
25695 LOCATION is the location of the usage point of TYPE_DECL.
25697 This function is a subroutine of
25698 append_type_to_template_for_access_check. */
25700 static void
25701 append_type_to_template_for_access_check_1 (tree t,
25702 tree type_decl,
25703 tree scope,
25704 location_t location)
25706 qualified_typedef_usage_t typedef_usage;
25707 tree ti;
25709 if (!t || t == error_mark_node)
25710 return;
25712 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
25713 || CLASS_TYPE_P (t))
25714 && type_decl
25715 && TREE_CODE (type_decl) == TYPE_DECL
25716 && scope);
25718 if (!(ti = get_template_info (t)))
25719 return;
25721 gcc_assert (TI_TEMPLATE (ti));
25723 typedef_usage.typedef_decl = type_decl;
25724 typedef_usage.context = scope;
25725 typedef_usage.locus = location;
25727 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
25730 /* Append TYPE_DECL to the template TEMPL.
25731 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
25732 At TEMPL instanciation time, TYPE_DECL will be checked to see
25733 if it can be accessed through SCOPE.
25734 LOCATION is the location of the usage point of TYPE_DECL.
25736 e.g. consider the following code snippet:
25738 class C
25740 typedef int myint;
25743 template<class U> struct S
25745 C::myint mi; // <-- usage point of the typedef C::myint
25748 S<char> s;
25750 At S<char> instantiation time, we need to check the access of C::myint
25751 In other words, we need to check the access of the myint typedef through
25752 the C scope. For that purpose, this function will add the myint typedef
25753 and the scope C through which its being accessed to a list of typedefs
25754 tied to the template S. That list will be walked at template instantiation
25755 time and access check performed on each typedefs it contains.
25756 Note that this particular code snippet should yield an error because
25757 myint is private to C. */
25759 void
25760 append_type_to_template_for_access_check (tree templ,
25761 tree type_decl,
25762 tree scope,
25763 location_t location)
25765 qualified_typedef_usage_t *iter;
25766 unsigned i;
25768 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
25770 /* Make sure we don't append the type to the template twice. */
25771 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
25772 if (iter->typedef_decl == type_decl && scope == iter->context)
25773 return;
25775 append_type_to_template_for_access_check_1 (templ, type_decl,
25776 scope, location);
25779 /* Convert the generic type parameters in PARM that match the types given in the
25780 range [START_IDX, END_IDX) from the current_template_parms into generic type
25781 packs. */
25783 tree
25784 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
25786 tree current = current_template_parms;
25787 int depth = TMPL_PARMS_DEPTH (current);
25788 current = INNERMOST_TEMPLATE_PARMS (current);
25789 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
25791 for (int i = 0; i < start_idx; ++i)
25792 TREE_VEC_ELT (replacement, i)
25793 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
25795 for (int i = start_idx; i < end_idx; ++i)
25797 /* Create a distinct parameter pack type from the current parm and add it
25798 to the replacement args to tsubst below into the generic function
25799 parameter. */
25801 tree o = TREE_TYPE (TREE_VALUE
25802 (TREE_VEC_ELT (current, i)));
25803 tree t = copy_type (o);
25804 TEMPLATE_TYPE_PARM_INDEX (t)
25805 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
25806 o, 0, 0, tf_none);
25807 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
25808 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
25809 TYPE_MAIN_VARIANT (t) = t;
25810 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
25811 TYPE_CANONICAL (t) = canonical_type_parameter (t);
25812 TREE_VEC_ELT (replacement, i) = t;
25813 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
25816 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
25817 TREE_VEC_ELT (replacement, i)
25818 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
25820 /* If there are more levels then build up the replacement with the outer
25821 template parms. */
25822 if (depth > 1)
25823 replacement = add_to_template_args (template_parms_to_args
25824 (TREE_CHAIN (current_template_parms)),
25825 replacement);
25827 return tsubst (parm, replacement, tf_none, NULL_TREE);
25830 /* Entries in the decl_constraint hash table. */
25831 struct GTY((for_user)) constr_entry
25833 tree decl;
25834 tree ci;
25837 /* Hashing function and equality for constraint entries. */
25838 struct constr_hasher : ggc_ptr_hash<constr_entry>
25840 static hashval_t hash (constr_entry *e)
25842 return (hashval_t)DECL_UID (e->decl);
25845 static bool equal (constr_entry *e1, constr_entry *e2)
25847 return e1->decl == e2->decl;
25851 /* A mapping from declarations to constraint information. Note that
25852 both templates and their underlying declarations are mapped to the
25853 same constraint information.
25855 FIXME: This is defined in pt.c because garbage collection
25856 code is not being generated for constraint.cc. */
25858 static GTY (()) hash_table<constr_hasher> *decl_constraints;
25860 /* Returns the template constraints of declaration T. If T is not
25861 constrained, return NULL_TREE. Note that T must be non-null. */
25863 tree
25864 get_constraints (tree t)
25866 if (!flag_concepts)
25867 return NULL_TREE;
25869 gcc_assert (DECL_P (t));
25870 if (TREE_CODE (t) == TEMPLATE_DECL)
25871 t = DECL_TEMPLATE_RESULT (t);
25872 constr_entry elt = { t, NULL_TREE };
25873 constr_entry* found = decl_constraints->find (&elt);
25874 if (found)
25875 return found->ci;
25876 else
25877 return NULL_TREE;
25880 /* Associate the given constraint information CI with the declaration
25881 T. If T is a template, then the constraints are associated with
25882 its underlying declaration. Don't build associations if CI is
25883 NULL_TREE. */
25885 void
25886 set_constraints (tree t, tree ci)
25888 if (!ci)
25889 return;
25890 gcc_assert (t && flag_concepts);
25891 if (TREE_CODE (t) == TEMPLATE_DECL)
25892 t = DECL_TEMPLATE_RESULT (t);
25893 gcc_assert (!get_constraints (t));
25894 constr_entry elt = {t, ci};
25895 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
25896 constr_entry* entry = ggc_alloc<constr_entry> ();
25897 *entry = elt;
25898 *slot = entry;
25901 /* Remove the associated constraints of the declaration T. */
25903 void
25904 remove_constraints (tree t)
25906 gcc_assert (DECL_P (t));
25907 if (TREE_CODE (t) == TEMPLATE_DECL)
25908 t = DECL_TEMPLATE_RESULT (t);
25910 constr_entry elt = {t, NULL_TREE};
25911 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
25912 if (slot)
25913 decl_constraints->clear_slot (slot);
25916 /* Memoized satisfaction results for declarations. This
25917 maps the pair (constraint_info, arguments) to the result computed
25918 by constraints_satisfied_p. */
25920 struct GTY((for_user)) constraint_sat_entry
25922 tree ci;
25923 tree args;
25924 tree result;
25927 /* Hashing function and equality for constraint entries. */
25929 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
25931 static hashval_t hash (constraint_sat_entry *e)
25933 hashval_t val = iterative_hash_object(e->ci, 0);
25934 return iterative_hash_template_arg (e->args, val);
25937 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
25939 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
25943 /* Memoized satisfaction results for concept checks. */
25945 struct GTY((for_user)) concept_spec_entry
25947 tree tmpl;
25948 tree args;
25949 tree result;
25952 /* Hashing function and equality for constraint entries. */
25954 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
25956 static hashval_t hash (concept_spec_entry *e)
25958 return hash_tmpl_and_args (e->tmpl, e->args);
25961 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
25963 ++comparing_specializations;
25964 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
25965 --comparing_specializations;
25966 return eq;
25970 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
25971 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
25973 /* Search for a memoized satisfaction result. Returns one of the
25974 truth value nodes if previously memoized, or NULL_TREE otherwise. */
25976 tree
25977 lookup_constraint_satisfaction (tree ci, tree args)
25979 constraint_sat_entry elt = { ci, args, NULL_TREE };
25980 constraint_sat_entry* found = constraint_memos->find (&elt);
25981 if (found)
25982 return found->result;
25983 else
25984 return NULL_TREE;
25987 /* Memoize the result of a satisfication test. Returns the saved result. */
25989 tree
25990 memoize_constraint_satisfaction (tree ci, tree args, tree result)
25992 constraint_sat_entry elt = {ci, args, result};
25993 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
25994 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
25995 *entry = elt;
25996 *slot = entry;
25997 return result;
26000 /* Search for a memoized satisfaction result for a concept. */
26002 tree
26003 lookup_concept_satisfaction (tree tmpl, tree args)
26005 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26006 concept_spec_entry* found = concept_memos->find (&elt);
26007 if (found)
26008 return found->result;
26009 else
26010 return NULL_TREE;
26013 /* Memoize the result of a concept check. Returns the saved result. */
26015 tree
26016 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
26018 concept_spec_entry elt = {tmpl, args, result};
26019 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
26020 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26021 *entry = elt;
26022 *slot = entry;
26023 return result;
26026 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
26028 /* Returns a prior concept specialization. This returns the substituted
26029 and normalized constraints defined by the concept. */
26031 tree
26032 get_concept_expansion (tree tmpl, tree args)
26034 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26035 concept_spec_entry* found = concept_expansions->find (&elt);
26036 if (found)
26037 return found->result;
26038 else
26039 return NULL_TREE;
26042 /* Save a concept expansion for later. */
26044 tree
26045 save_concept_expansion (tree tmpl, tree args, tree def)
26047 concept_spec_entry elt = {tmpl, args, def};
26048 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
26049 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26050 *entry = elt;
26051 *slot = entry;
26052 return def;
26055 static hashval_t
26056 hash_subsumption_args (tree t1, tree t2)
26058 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
26059 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
26060 int val = 0;
26061 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
26062 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
26063 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
26064 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
26065 return val;
26068 /* Compare the constraints of two subsumption entries. The LEFT1 and
26069 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
26070 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
26072 static bool
26073 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
26075 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
26076 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
26077 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
26078 CHECK_CONSTR_ARGS (right1)))
26079 return comp_template_args (CHECK_CONSTR_ARGS (left2),
26080 CHECK_CONSTR_ARGS (right2));
26081 return false;
26084 /* Key/value pair for learning and memoizing subsumption results. This
26085 associates a pair of check constraints (including arguments) with
26086 a boolean value indicating the result. */
26088 struct GTY((for_user)) subsumption_entry
26090 tree t1;
26091 tree t2;
26092 bool result;
26095 /* Hashing function and equality for constraint entries. */
26097 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
26099 static hashval_t hash (subsumption_entry *e)
26101 return hash_subsumption_args (e->t1, e->t2);
26104 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
26106 ++comparing_specializations;
26107 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
26108 --comparing_specializations;
26109 return eq;
26113 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
26115 /* Search for a previously cached subsumption result. */
26117 bool*
26118 lookup_subsumption_result (tree t1, tree t2)
26120 subsumption_entry elt = { t1, t2, false };
26121 subsumption_entry* found = subsumption_table->find (&elt);
26122 if (found)
26123 return &found->result;
26124 else
26125 return 0;
26128 /* Save a subsumption result. */
26130 bool
26131 save_subsumption_result (tree t1, tree t2, bool result)
26133 subsumption_entry elt = {t1, t2, result};
26134 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
26135 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
26136 *entry = elt;
26137 *slot = entry;
26138 return result;
26141 /* Set up the hash table for constraint association. */
26143 void
26144 init_constraint_processing (void)
26146 if (!flag_concepts)
26147 return;
26149 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
26150 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
26151 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
26152 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
26153 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
26156 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
26157 0..N-1. */
26159 void
26160 declare_integer_pack (void)
26162 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
26163 build_function_type_list (integer_type_node,
26164 integer_type_node,
26165 NULL_TREE),
26166 NULL_TREE, ECF_CONST);
26167 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
26168 DECL_BUILT_IN_CLASS (ipfn) = BUILT_IN_FRONTEND;
26171 /* Set up the hash tables for template instantiations. */
26173 void
26174 init_template_processing (void)
26176 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
26177 type_specializations = hash_table<spec_hasher>::create_ggc (37);
26179 if (cxx_dialect >= cxx11)
26180 declare_integer_pack ();
26183 /* Print stats about the template hash tables for -fstats. */
26185 void
26186 print_template_statistics (void)
26188 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
26189 "%f collisions\n", (long) decl_specializations->size (),
26190 (long) decl_specializations->elements (),
26191 decl_specializations->collisions ());
26192 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
26193 "%f collisions\n", (long) type_specializations->size (),
26194 (long) type_specializations->elements (),
26195 type_specializations->collisions ());
26198 #include "gt-cp-pt.h"