Fix typo in last ChangeLog entry
[official-gcc.git] / gcc / cp / pt.c
blob07dad99650462cd774739ecfb5d143316a53f0c6
1 /* Handle parameterized types (templates) for GNU -*- C++ -*-.
2 Copyright (C) 1992-2018 Free Software Foundation, Inc.
3 Written by Ken Raeburn (raeburn@cygnus.com) while at Watchmaker Computing.
4 Rewritten by Jason Merrill (jason@cygnus.com).
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* Known bugs or deficiencies include:
24 all methods must be provided in header files; can't use a source
25 file that contains only the method templates and "just win". */
27 #include "config.h"
28 #include "system.h"
29 #include "coretypes.h"
30 #include "cp-tree.h"
31 #include "timevar.h"
32 #include "stringpool.h"
33 #include "varasm.h"
34 #include "attribs.h"
35 #include "stor-layout.h"
36 #include "intl.h"
37 #include "c-family/c-objc.h"
38 #include "cp-objcp-common.h"
39 #include "toplev.h"
40 #include "tree-iterator.h"
41 #include "type-utils.h"
42 #include "gimplify.h"
43 #include "gcc-rich-location.h"
44 #include "selftest.h"
46 /* The type of functions taking a tree, and some additional data, and
47 returning an int. */
48 typedef int (*tree_fn_t) (tree, void*);
50 /* The PENDING_TEMPLATES is a TREE_LIST of templates whose
51 instantiations have been deferred, either because their definitions
52 were not yet available, or because we were putting off doing the work. */
53 struct GTY ((chain_next ("%h.next"))) pending_template {
54 struct pending_template *next;
55 struct tinst_level *tinst;
58 static GTY(()) struct pending_template *pending_templates;
59 static GTY(()) struct pending_template *last_pending_template;
61 int processing_template_parmlist;
62 static int template_header_count;
64 static GTY(()) tree saved_trees;
65 static vec<int> inline_parm_levels;
67 static GTY(()) struct tinst_level *current_tinst_level;
69 static GTY(()) tree saved_access_scope;
71 /* Live only within one (recursive) call to tsubst_expr. We use
72 this to pass the statement expression node from the STMT_EXPR
73 to the EXPR_STMT that is its result. */
74 static tree cur_stmt_expr;
76 // -------------------------------------------------------------------------- //
77 // Local Specialization Stack
79 // Implementation of the RAII helper for creating new local
80 // specializations.
81 local_specialization_stack::local_specialization_stack (lss_policy policy)
82 : saved (local_specializations)
84 if (policy == lss_blank || !saved)
85 local_specializations = new hash_map<tree, tree>;
86 else
87 local_specializations = new hash_map<tree, tree>(*saved);
90 local_specialization_stack::~local_specialization_stack ()
92 delete local_specializations;
93 local_specializations = saved;
96 /* True if we've recursed into fn_type_unification too many times. */
97 static bool excessive_deduction_depth;
99 struct GTY((for_user)) spec_entry
101 tree tmpl;
102 tree args;
103 tree spec;
106 struct spec_hasher : ggc_ptr_hash<spec_entry>
108 static hashval_t hash (spec_entry *);
109 static bool equal (spec_entry *, spec_entry *);
112 static GTY (()) hash_table<spec_hasher> *decl_specializations;
114 static GTY (()) hash_table<spec_hasher> *type_specializations;
116 /* Contains canonical template parameter types. The vector is indexed by
117 the TEMPLATE_TYPE_IDX of the template parameter. Each element is a
118 TREE_LIST, whose TREE_VALUEs contain the canonical template
119 parameters of various types and levels. */
120 static GTY(()) vec<tree, va_gc> *canonical_template_parms;
122 #define UNIFY_ALLOW_NONE 0
123 #define UNIFY_ALLOW_MORE_CV_QUAL 1
124 #define UNIFY_ALLOW_LESS_CV_QUAL 2
125 #define UNIFY_ALLOW_DERIVED 4
126 #define UNIFY_ALLOW_INTEGER 8
127 #define UNIFY_ALLOW_OUTER_LEVEL 16
128 #define UNIFY_ALLOW_OUTER_MORE_CV_QUAL 32
129 #define UNIFY_ALLOW_OUTER_LESS_CV_QUAL 64
131 enum template_base_result {
132 tbr_incomplete_type,
133 tbr_ambiguous_baseclass,
134 tbr_success
137 static void push_access_scope (tree);
138 static void pop_access_scope (tree);
139 static bool resolve_overloaded_unification (tree, tree, tree, tree,
140 unification_kind_t, int,
141 bool);
142 static int try_one_overload (tree, tree, tree, tree, tree,
143 unification_kind_t, int, bool, bool);
144 static int unify (tree, tree, tree, tree, int, bool);
145 static void add_pending_template (tree);
146 static tree reopen_tinst_level (struct tinst_level *);
147 static tree tsubst_initializer_list (tree, tree);
148 static tree get_partial_spec_bindings (tree, tree, tree);
149 static tree coerce_template_parms (tree, tree, tree, tsubst_flags_t,
150 bool, bool);
151 static tree coerce_innermost_template_parms (tree, tree, tree, tsubst_flags_t,
152 bool, bool);
153 static void tsubst_enum (tree, tree, tree);
154 static tree add_to_template_args (tree, tree);
155 static tree add_outermost_template_args (tree, tree);
156 static bool check_instantiated_args (tree, tree, tsubst_flags_t);
157 static int maybe_adjust_types_for_deduction (unification_kind_t, tree*, tree*,
158 tree);
159 static int type_unification_real (tree, tree, tree, const tree *,
160 unsigned int, int, unification_kind_t, int,
161 vec<deferred_access_check, va_gc> **,
162 bool);
163 static void note_template_header (int);
164 static tree convert_nontype_argument_function (tree, tree, tsubst_flags_t);
165 static tree convert_nontype_argument (tree, tree, tsubst_flags_t);
166 static tree convert_template_argument (tree, tree, tree,
167 tsubst_flags_t, int, tree);
168 static tree for_each_template_parm (tree, tree_fn_t, void*,
169 hash_set<tree> *, bool, tree_fn_t = NULL);
170 static tree expand_template_argument_pack (tree);
171 static tree build_template_parm_index (int, int, int, tree, tree);
172 static bool inline_needs_template_parms (tree, bool);
173 static void push_inline_template_parms_recursive (tree, int);
174 static tree reduce_template_parm_level (tree, tree, int, tree, tsubst_flags_t);
175 static int mark_template_parm (tree, void *);
176 static int template_parm_this_level_p (tree, void *);
177 static tree tsubst_friend_function (tree, tree);
178 static tree tsubst_friend_class (tree, tree);
179 static int can_complete_type_without_circularity (tree);
180 static tree get_bindings (tree, tree, tree, bool);
181 static int template_decl_level (tree);
182 static int check_cv_quals_for_unify (int, tree, tree);
183 static void template_parm_level_and_index (tree, int*, int*);
184 static int unify_pack_expansion (tree, tree, tree,
185 tree, unification_kind_t, bool, bool);
186 static tree copy_template_args (tree);
187 static tree tsubst_template_arg (tree, tree, tsubst_flags_t, tree);
188 static tree tsubst_template_args (tree, tree, tsubst_flags_t, tree);
189 static tree tsubst_template_parms (tree, tree, tsubst_flags_t);
190 static tree most_specialized_partial_spec (tree, tsubst_flags_t);
191 static tree tsubst_aggr_type (tree, tree, tsubst_flags_t, tree, int);
192 static tree tsubst_arg_types (tree, tree, tree, tsubst_flags_t, tree);
193 static tree tsubst_function_type (tree, tree, tsubst_flags_t, tree);
194 static bool check_specialization_scope (void);
195 static tree process_partial_specialization (tree);
196 static void set_current_access_from_decl (tree);
197 static enum template_base_result get_template_base (tree, tree, tree, tree,
198 bool , tree *);
199 static tree try_class_unification (tree, tree, tree, tree, bool);
200 static int coerce_template_template_parms (tree, tree, tsubst_flags_t,
201 tree, tree);
202 static bool template_template_parm_bindings_ok_p (tree, tree);
203 static void tsubst_default_arguments (tree, tsubst_flags_t);
204 static tree for_each_template_parm_r (tree *, int *, void *);
205 static tree copy_default_args_to_explicit_spec_1 (tree, tree);
206 static void copy_default_args_to_explicit_spec (tree);
207 static bool invalid_nontype_parm_type_p (tree, tsubst_flags_t);
208 static bool dependent_template_arg_p (tree);
209 static bool any_template_arguments_need_structural_equality_p (tree);
210 static bool dependent_type_p_r (tree);
211 static tree tsubst_copy (tree, tree, tsubst_flags_t, tree);
212 static tree tsubst_decl (tree, tree, tsubst_flags_t);
213 static void perform_typedefs_access_check (tree tmpl, tree targs);
214 static void append_type_to_template_for_access_check_1 (tree, tree, tree,
215 location_t);
216 static tree listify (tree);
217 static tree listify_autos (tree, tree);
218 static tree tsubst_template_parm (tree, tree, tsubst_flags_t);
219 static tree instantiate_alias_template (tree, tree, tsubst_flags_t);
220 static bool complex_alias_template_p (const_tree tmpl);
221 static tree tsubst_attributes (tree, tree, tsubst_flags_t, tree);
222 static tree canonicalize_expr_argument (tree, tsubst_flags_t);
223 static tree make_argument_pack (tree);
224 static void register_parameter_specializations (tree, tree);
225 static tree enclosing_instantiation_of (tree tctx);
227 /* Make the current scope suitable for access checking when we are
228 processing T. T can be FUNCTION_DECL for instantiated function
229 template, VAR_DECL for static member variable, or TYPE_DECL for
230 alias template (needed by instantiate_decl). */
232 static void
233 push_access_scope (tree t)
235 gcc_assert (VAR_OR_FUNCTION_DECL_P (t)
236 || TREE_CODE (t) == TYPE_DECL);
238 if (DECL_FRIEND_CONTEXT (t))
239 push_nested_class (DECL_FRIEND_CONTEXT (t));
240 else if (DECL_CLASS_SCOPE_P (t))
241 push_nested_class (DECL_CONTEXT (t));
242 else
243 push_to_top_level ();
245 if (TREE_CODE (t) == FUNCTION_DECL)
247 saved_access_scope = tree_cons
248 (NULL_TREE, current_function_decl, saved_access_scope);
249 current_function_decl = t;
253 /* Restore the scope set up by push_access_scope. T is the node we
254 are processing. */
256 static void
257 pop_access_scope (tree t)
259 if (TREE_CODE (t) == FUNCTION_DECL)
261 current_function_decl = TREE_VALUE (saved_access_scope);
262 saved_access_scope = TREE_CHAIN (saved_access_scope);
265 if (DECL_FRIEND_CONTEXT (t) || DECL_CLASS_SCOPE_P (t))
266 pop_nested_class ();
267 else
268 pop_from_top_level ();
271 /* Do any processing required when DECL (a member template
272 declaration) is finished. Returns the TEMPLATE_DECL corresponding
273 to DECL, unless it is a specialization, in which case the DECL
274 itself is returned. */
276 tree
277 finish_member_template_decl (tree decl)
279 if (decl == error_mark_node)
280 return error_mark_node;
282 gcc_assert (DECL_P (decl));
284 if (TREE_CODE (decl) == TYPE_DECL)
286 tree type;
288 type = TREE_TYPE (decl);
289 if (type == error_mark_node)
290 return error_mark_node;
291 if (MAYBE_CLASS_TYPE_P (type)
292 && CLASSTYPE_TEMPLATE_INFO (type)
293 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
295 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
296 check_member_template (tmpl);
297 return tmpl;
299 return NULL_TREE;
301 else if (TREE_CODE (decl) == FIELD_DECL)
302 error ("data member %qD cannot be a member template", decl);
303 else if (DECL_TEMPLATE_INFO (decl))
305 if (!DECL_TEMPLATE_SPECIALIZATION (decl))
307 check_member_template (DECL_TI_TEMPLATE (decl));
308 return DECL_TI_TEMPLATE (decl);
310 else
311 return decl;
313 else
314 error ("invalid member template declaration %qD", decl);
316 return error_mark_node;
319 /* Create a template info node. */
321 tree
322 build_template_info (tree template_decl, tree template_args)
324 tree result = make_node (TEMPLATE_INFO);
325 TI_TEMPLATE (result) = template_decl;
326 TI_ARGS (result) = template_args;
327 return result;
330 /* Return the template info node corresponding to T, whatever T is. */
332 tree
333 get_template_info (const_tree t)
335 tree tinfo = NULL_TREE;
337 if (!t || t == error_mark_node)
338 return NULL;
340 if (TREE_CODE (t) == NAMESPACE_DECL
341 || TREE_CODE (t) == PARM_DECL)
342 return NULL;
344 if (DECL_P (t) && DECL_LANG_SPECIFIC (t))
345 tinfo = DECL_TEMPLATE_INFO (t);
347 if (!tinfo && DECL_IMPLICIT_TYPEDEF_P (t))
348 t = TREE_TYPE (t);
350 if (OVERLOAD_TYPE_P (t))
351 tinfo = TYPE_TEMPLATE_INFO (t);
352 else if (TREE_CODE (t) == BOUND_TEMPLATE_TEMPLATE_PARM)
353 tinfo = TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (t);
355 return tinfo;
358 /* Returns the template nesting level of the indicated class TYPE.
360 For example, in:
361 template <class T>
362 struct A
364 template <class U>
365 struct B {};
368 A<T>::B<U> has depth two, while A<T> has depth one.
369 Both A<T>::B<int> and A<int>::B<U> have depth one, if
370 they are instantiations, not specializations.
372 This function is guaranteed to return 0 if passed NULL_TREE so
373 that, for example, `template_class_depth (current_class_type)' is
374 always safe. */
377 template_class_depth (tree type)
379 int depth;
381 for (depth = 0; type && TREE_CODE (type) != NAMESPACE_DECL; )
383 tree tinfo = get_template_info (type);
385 if (tinfo && PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo))
386 && uses_template_parms (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo))))
387 ++depth;
389 if (DECL_P (type))
390 type = CP_DECL_CONTEXT (type);
391 else if (LAMBDA_TYPE_P (type))
392 type = LAMBDA_TYPE_EXTRA_SCOPE (type);
393 else
394 type = CP_TYPE_CONTEXT (type);
397 return depth;
400 /* Subroutine of maybe_begin_member_template_processing.
401 Returns true if processing DECL needs us to push template parms. */
403 static bool
404 inline_needs_template_parms (tree decl, bool nsdmi)
406 if (!decl || (!nsdmi && ! DECL_TEMPLATE_INFO (decl)))
407 return false;
409 return (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (most_general_template (decl)))
410 > (processing_template_decl + DECL_TEMPLATE_SPECIALIZATION (decl)));
413 /* Subroutine of maybe_begin_member_template_processing.
414 Push the template parms in PARMS, starting from LEVELS steps into the
415 chain, and ending at the beginning, since template parms are listed
416 innermost first. */
418 static void
419 push_inline_template_parms_recursive (tree parmlist, int levels)
421 tree parms = TREE_VALUE (parmlist);
422 int i;
424 if (levels > 1)
425 push_inline_template_parms_recursive (TREE_CHAIN (parmlist), levels - 1);
427 ++processing_template_decl;
428 current_template_parms
429 = tree_cons (size_int (processing_template_decl),
430 parms, current_template_parms);
431 TEMPLATE_PARMS_FOR_INLINE (current_template_parms) = 1;
433 begin_scope (TREE_VEC_LENGTH (parms) ? sk_template_parms : sk_template_spec,
434 NULL);
435 for (i = 0; i < TREE_VEC_LENGTH (parms); ++i)
437 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
439 if (error_operand_p (parm))
440 continue;
442 gcc_assert (DECL_P (parm));
444 switch (TREE_CODE (parm))
446 case TYPE_DECL:
447 case TEMPLATE_DECL:
448 pushdecl (parm);
449 break;
451 case PARM_DECL:
452 /* Push the CONST_DECL. */
453 pushdecl (TEMPLATE_PARM_DECL (DECL_INITIAL (parm)));
454 break;
456 default:
457 gcc_unreachable ();
462 /* Restore the template parameter context for a member template, a
463 friend template defined in a class definition, or a non-template
464 member of template class. */
466 void
467 maybe_begin_member_template_processing (tree decl)
469 tree parms;
470 int levels = 0;
471 bool nsdmi = TREE_CODE (decl) == FIELD_DECL;
473 if (nsdmi)
475 tree ctx = DECL_CONTEXT (decl);
476 decl = (CLASSTYPE_TEMPLATE_INFO (ctx)
477 /* Disregard full specializations (c++/60999). */
478 && uses_template_parms (ctx)
479 ? CLASSTYPE_TI_TEMPLATE (ctx) : NULL_TREE);
482 if (inline_needs_template_parms (decl, nsdmi))
484 parms = DECL_TEMPLATE_PARMS (most_general_template (decl));
485 levels = TMPL_PARMS_DEPTH (parms) - processing_template_decl;
487 if (DECL_TEMPLATE_SPECIALIZATION (decl))
489 --levels;
490 parms = TREE_CHAIN (parms);
493 push_inline_template_parms_recursive (parms, levels);
496 /* Remember how many levels of template parameters we pushed so that
497 we can pop them later. */
498 inline_parm_levels.safe_push (levels);
501 /* Undo the effects of maybe_begin_member_template_processing. */
503 void
504 maybe_end_member_template_processing (void)
506 int i;
507 int last;
509 if (inline_parm_levels.length () == 0)
510 return;
512 last = inline_parm_levels.pop ();
513 for (i = 0; i < last; ++i)
515 --processing_template_decl;
516 current_template_parms = TREE_CHAIN (current_template_parms);
517 poplevel (0, 0, 0);
521 /* Return a new template argument vector which contains all of ARGS,
522 but has as its innermost set of arguments the EXTRA_ARGS. */
524 static tree
525 add_to_template_args (tree args, tree extra_args)
527 tree new_args;
528 int extra_depth;
529 int i;
530 int j;
532 if (args == NULL_TREE || extra_args == error_mark_node)
533 return extra_args;
535 extra_depth = TMPL_ARGS_DEPTH (extra_args);
536 new_args = make_tree_vec (TMPL_ARGS_DEPTH (args) + extra_depth);
538 for (i = 1; i <= TMPL_ARGS_DEPTH (args); ++i)
539 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (args, i));
541 for (j = 1; j <= extra_depth; ++j, ++i)
542 SET_TMPL_ARGS_LEVEL (new_args, i, TMPL_ARGS_LEVEL (extra_args, j));
544 return new_args;
547 /* Like add_to_template_args, but only the outermost ARGS are added to
548 the EXTRA_ARGS. In particular, all but TMPL_ARGS_DEPTH
549 (EXTRA_ARGS) levels are added. This function is used to combine
550 the template arguments from a partial instantiation with the
551 template arguments used to attain the full instantiation from the
552 partial instantiation. */
554 static tree
555 add_outermost_template_args (tree args, tree extra_args)
557 tree new_args;
559 /* If there are more levels of EXTRA_ARGS than there are ARGS,
560 something very fishy is going on. */
561 gcc_assert (TMPL_ARGS_DEPTH (args) >= TMPL_ARGS_DEPTH (extra_args));
563 /* If *all* the new arguments will be the EXTRA_ARGS, just return
564 them. */
565 if (TMPL_ARGS_DEPTH (args) == TMPL_ARGS_DEPTH (extra_args))
566 return extra_args;
568 /* For the moment, we make ARGS look like it contains fewer levels. */
569 TREE_VEC_LENGTH (args) -= TMPL_ARGS_DEPTH (extra_args);
571 new_args = add_to_template_args (args, extra_args);
573 /* Now, we restore ARGS to its full dimensions. */
574 TREE_VEC_LENGTH (args) += TMPL_ARGS_DEPTH (extra_args);
576 return new_args;
579 /* Return the N levels of innermost template arguments from the ARGS. */
581 tree
582 get_innermost_template_args (tree args, int n)
584 tree new_args;
585 int extra_levels;
586 int i;
588 gcc_assert (n >= 0);
590 /* If N is 1, just return the innermost set of template arguments. */
591 if (n == 1)
592 return TMPL_ARGS_LEVEL (args, TMPL_ARGS_DEPTH (args));
594 /* If we're not removing anything, just return the arguments we were
595 given. */
596 extra_levels = TMPL_ARGS_DEPTH (args) - n;
597 gcc_assert (extra_levels >= 0);
598 if (extra_levels == 0)
599 return args;
601 /* Make a new set of arguments, not containing the outer arguments. */
602 new_args = make_tree_vec (n);
603 for (i = 1; i <= n; ++i)
604 SET_TMPL_ARGS_LEVEL (new_args, i,
605 TMPL_ARGS_LEVEL (args, i + extra_levels));
607 return new_args;
610 /* The inverse of get_innermost_template_args: Return all but the innermost
611 EXTRA_LEVELS levels of template arguments from the ARGS. */
613 static tree
614 strip_innermost_template_args (tree args, int extra_levels)
616 tree new_args;
617 int n = TMPL_ARGS_DEPTH (args) - extra_levels;
618 int i;
620 gcc_assert (n >= 0);
622 /* If N is 1, just return the outermost set of template arguments. */
623 if (n == 1)
624 return TMPL_ARGS_LEVEL (args, 1);
626 /* If we're not removing anything, just return the arguments we were
627 given. */
628 gcc_assert (extra_levels >= 0);
629 if (extra_levels == 0)
630 return args;
632 /* Make a new set of arguments, not containing the inner arguments. */
633 new_args = make_tree_vec (n);
634 for (i = 1; i <= n; ++i)
635 SET_TMPL_ARGS_LEVEL (new_args, i,
636 TMPL_ARGS_LEVEL (args, i));
638 return new_args;
641 /* We've got a template header coming up; push to a new level for storing
642 the parms. */
644 void
645 begin_template_parm_list (void)
647 /* We use a non-tag-transparent scope here, which causes pushtag to
648 put tags in this scope, rather than in the enclosing class or
649 namespace scope. This is the right thing, since we want
650 TEMPLATE_DECLS, and not TYPE_DECLS for template classes. For a
651 global template class, push_template_decl handles putting the
652 TEMPLATE_DECL into top-level scope. For a nested template class,
653 e.g.:
655 template <class T> struct S1 {
656 template <class T> struct S2 {};
659 pushtag contains special code to insert the TEMPLATE_DECL for S2
660 at the right scope. */
661 begin_scope (sk_template_parms, NULL);
662 ++processing_template_decl;
663 ++processing_template_parmlist;
664 note_template_header (0);
666 /* Add a dummy parameter level while we process the parameter list. */
667 current_template_parms
668 = tree_cons (size_int (processing_template_decl),
669 make_tree_vec (0),
670 current_template_parms);
673 /* This routine is called when a specialization is declared. If it is
674 invalid to declare a specialization here, an error is reported and
675 false is returned, otherwise this routine will return true. */
677 static bool
678 check_specialization_scope (void)
680 tree scope = current_scope ();
682 /* [temp.expl.spec]
684 An explicit specialization shall be declared in the namespace of
685 which the template is a member, or, for member templates, in the
686 namespace of which the enclosing class or enclosing class
687 template is a member. An explicit specialization of a member
688 function, member class or static data member of a class template
689 shall be declared in the namespace of which the class template
690 is a member. */
691 if (scope && TREE_CODE (scope) != NAMESPACE_DECL)
693 error ("explicit specialization in non-namespace scope %qD", scope);
694 return false;
697 /* [temp.expl.spec]
699 In an explicit specialization declaration for a member of a class
700 template or a member template that appears in namespace scope,
701 the member template and some of its enclosing class templates may
702 remain unspecialized, except that the declaration shall not
703 explicitly specialize a class member template if its enclosing
704 class templates are not explicitly specialized as well. */
705 if (current_template_parms)
707 error ("enclosing class templates are not explicitly specialized");
708 return false;
711 return true;
714 /* We've just seen template <>. */
716 bool
717 begin_specialization (void)
719 begin_scope (sk_template_spec, NULL);
720 note_template_header (1);
721 return check_specialization_scope ();
724 /* Called at then end of processing a declaration preceded by
725 template<>. */
727 void
728 end_specialization (void)
730 finish_scope ();
731 reset_specialization ();
734 /* Any template <>'s that we have seen thus far are not referring to a
735 function specialization. */
737 void
738 reset_specialization (void)
740 processing_specialization = 0;
741 template_header_count = 0;
744 /* We've just seen a template header. If SPECIALIZATION is nonzero,
745 it was of the form template <>. */
747 static void
748 note_template_header (int specialization)
750 processing_specialization = specialization;
751 template_header_count++;
754 /* We're beginning an explicit instantiation. */
756 void
757 begin_explicit_instantiation (void)
759 gcc_assert (!processing_explicit_instantiation);
760 processing_explicit_instantiation = true;
764 void
765 end_explicit_instantiation (void)
767 gcc_assert (processing_explicit_instantiation);
768 processing_explicit_instantiation = false;
771 /* An explicit specialization or partial specialization of TMPL is being
772 declared. Check that the namespace in which the specialization is
773 occurring is permissible. Returns false iff it is invalid to
774 specialize TMPL in the current namespace. */
776 static bool
777 check_specialization_namespace (tree tmpl)
779 tree tpl_ns = decl_namespace_context (tmpl);
781 /* [tmpl.expl.spec]
783 An explicit specialization shall be declared in a namespace enclosing the
784 specialized template. An explicit specialization whose declarator-id is
785 not qualified shall be declared in the nearest enclosing namespace of the
786 template, or, if the namespace is inline (7.3.1), any namespace from its
787 enclosing namespace set. */
788 if (current_scope() != DECL_CONTEXT (tmpl)
789 && !at_namespace_scope_p ())
791 error ("specialization of %qD must appear at namespace scope", tmpl);
792 return false;
795 if (is_nested_namespace (current_namespace, tpl_ns, cxx_dialect < cxx11))
796 /* Same or enclosing namespace. */
797 return true;
798 else
800 permerror (input_location,
801 "specialization of %qD in different namespace", tmpl);
802 inform (DECL_SOURCE_LOCATION (tmpl),
803 " from definition of %q#D", tmpl);
804 return false;
808 /* SPEC is an explicit instantiation. Check that it is valid to
809 perform this explicit instantiation in the current namespace. */
811 static void
812 check_explicit_instantiation_namespace (tree spec)
814 tree ns;
816 /* DR 275: An explicit instantiation shall appear in an enclosing
817 namespace of its template. */
818 ns = decl_namespace_context (spec);
819 if (!is_nested_namespace (current_namespace, ns))
820 permerror (input_location, "explicit instantiation of %qD in namespace %qD "
821 "(which does not enclose namespace %qD)",
822 spec, current_namespace, ns);
825 // Returns the type of a template specialization only if that
826 // specialization needs to be defined. Otherwise (e.g., if the type has
827 // already been defined), the function returns NULL_TREE.
828 static tree
829 maybe_new_partial_specialization (tree type)
831 // An implicit instantiation of an incomplete type implies
832 // the definition of a new class template.
834 // template<typename T>
835 // struct S;
837 // template<typename T>
838 // struct S<T*>;
840 // Here, S<T*> is an implicit instantiation of S whose type
841 // is incomplete.
842 if (CLASSTYPE_IMPLICIT_INSTANTIATION (type) && !COMPLETE_TYPE_P (type))
843 return type;
845 // It can also be the case that TYPE is a completed specialization.
846 // Continuing the previous example, suppose we also declare:
848 // template<typename T>
849 // requires Integral<T>
850 // struct S<T*>;
852 // Here, S<T*> refers to the specialization S<T*> defined
853 // above. However, we need to differentiate definitions because
854 // we intend to define a new partial specialization. In this case,
855 // we rely on the fact that the constraints are different for
856 // this declaration than that above.
858 // Note that we also get here for injected class names and
859 // late-parsed template definitions. We must ensure that we
860 // do not create new type declarations for those cases.
861 if (flag_concepts && CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
863 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
864 tree args = CLASSTYPE_TI_ARGS (type);
866 // If there are no template parameters, this cannot be a new
867 // partial template specializtion?
868 if (!current_template_parms)
869 return NULL_TREE;
871 // The injected-class-name is not a new partial specialization.
872 if (DECL_SELF_REFERENCE_P (TYPE_NAME (type)))
873 return NULL_TREE;
875 // If the constraints are not the same as those of the primary
876 // then, we can probably create a new specialization.
877 tree type_constr = current_template_constraints ();
879 if (type == TREE_TYPE (tmpl))
881 tree main_constr = get_constraints (tmpl);
882 if (equivalent_constraints (type_constr, main_constr))
883 return NULL_TREE;
886 // Also, if there's a pre-existing specialization with matching
887 // constraints, then this also isn't new.
888 tree specs = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
889 while (specs)
891 tree spec_tmpl = TREE_VALUE (specs);
892 tree spec_args = TREE_PURPOSE (specs);
893 tree spec_constr = get_constraints (spec_tmpl);
894 if (comp_template_args (args, spec_args)
895 && equivalent_constraints (type_constr, spec_constr))
896 return NULL_TREE;
897 specs = TREE_CHAIN (specs);
900 // Create a new type node (and corresponding type decl)
901 // for the newly declared specialization.
902 tree t = make_class_type (TREE_CODE (type));
903 CLASSTYPE_DECLARED_CLASS (t) = CLASSTYPE_DECLARED_CLASS (type);
904 SET_TYPE_TEMPLATE_INFO (t, build_template_info (tmpl, args));
906 /* We only need a separate type node for storing the definition of this
907 partial specialization; uses of S<T*> are unconstrained, so all are
908 equivalent. So keep TYPE_CANONICAL the same. */
909 TYPE_CANONICAL (t) = TYPE_CANONICAL (type);
911 // Build the corresponding type decl.
912 tree d = create_implicit_typedef (DECL_NAME (tmpl), t);
913 DECL_CONTEXT (d) = TYPE_CONTEXT (t);
914 DECL_SOURCE_LOCATION (d) = input_location;
916 return t;
919 return NULL_TREE;
922 /* The TYPE is being declared. If it is a template type, that means it
923 is a partial specialization. Do appropriate error-checking. */
925 tree
926 maybe_process_partial_specialization (tree type)
928 tree context;
930 if (type == error_mark_node)
931 return error_mark_node;
933 /* A lambda that appears in specialization context is not itself a
934 specialization. */
935 if (CLASS_TYPE_P (type) && CLASSTYPE_LAMBDA_EXPR (type))
936 return type;
938 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
940 error ("name of class shadows template template parameter %qD",
941 TYPE_NAME (type));
942 return error_mark_node;
945 context = TYPE_CONTEXT (type);
947 if (TYPE_ALIAS_P (type))
949 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (type);
951 if (tinfo && DECL_ALIAS_TEMPLATE_P (TI_TEMPLATE (tinfo)))
952 error ("specialization of alias template %qD",
953 TI_TEMPLATE (tinfo));
954 else
955 error ("explicit specialization of non-template %qT", type);
956 return error_mark_node;
958 else if (CLASS_TYPE_P (type) && CLASSTYPE_USE_TEMPLATE (type))
960 /* This is for ordinary explicit specialization and partial
961 specialization of a template class such as:
963 template <> class C<int>;
967 template <class T> class C<T*>;
969 Make sure that `C<int>' and `C<T*>' are implicit instantiations. */
971 if (tree t = maybe_new_partial_specialization (type))
973 if (!check_specialization_namespace (CLASSTYPE_TI_TEMPLATE (t))
974 && !at_namespace_scope_p ())
975 return error_mark_node;
976 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (t);
977 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (t)) = input_location;
978 if (processing_template_decl)
980 tree decl = push_template_decl (TYPE_MAIN_DECL (t));
981 if (decl == error_mark_node)
982 return error_mark_node;
983 return TREE_TYPE (decl);
986 else if (CLASSTYPE_TEMPLATE_INSTANTIATION (type))
987 error ("specialization of %qT after instantiation", type);
988 else if (errorcount && !processing_specialization
989 && CLASSTYPE_TEMPLATE_SPECIALIZATION (type)
990 && !uses_template_parms (CLASSTYPE_TI_ARGS (type)))
991 /* Trying to define a specialization either without a template<> header
992 or in an inappropriate place. We've already given an error, so just
993 bail now so we don't actually define the specialization. */
994 return error_mark_node;
996 else if (CLASS_TYPE_P (type)
997 && !CLASSTYPE_USE_TEMPLATE (type)
998 && CLASSTYPE_TEMPLATE_INFO (type)
999 && context && CLASS_TYPE_P (context)
1000 && CLASSTYPE_TEMPLATE_INFO (context))
1002 /* This is for an explicit specialization of member class
1003 template according to [temp.expl.spec/18]:
1005 template <> template <class U> class C<int>::D;
1007 The context `C<int>' must be an implicit instantiation.
1008 Otherwise this is just a member class template declared
1009 earlier like:
1011 template <> class C<int> { template <class U> class D; };
1012 template <> template <class U> class C<int>::D;
1014 In the first case, `C<int>::D' is a specialization of `C<T>::D'
1015 while in the second case, `C<int>::D' is a primary template
1016 and `C<T>::D' may not exist. */
1018 if (CLASSTYPE_IMPLICIT_INSTANTIATION (context)
1019 && !COMPLETE_TYPE_P (type))
1021 tree t;
1022 tree tmpl = CLASSTYPE_TI_TEMPLATE (type);
1024 if (current_namespace
1025 != decl_namespace_context (tmpl))
1027 permerror (input_location,
1028 "specializing %q#T in different namespace", type);
1029 permerror (DECL_SOURCE_LOCATION (tmpl),
1030 " from definition of %q#D", tmpl);
1033 /* Check for invalid specialization after instantiation:
1035 template <> template <> class C<int>::D<int>;
1036 template <> template <class U> class C<int>::D; */
1038 for (t = DECL_TEMPLATE_INSTANTIATIONS (tmpl);
1039 t; t = TREE_CHAIN (t))
1041 tree inst = TREE_VALUE (t);
1042 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (inst)
1043 || !COMPLETE_OR_OPEN_TYPE_P (inst))
1045 /* We already have a full specialization of this partial
1046 instantiation, or a full specialization has been
1047 looked up but not instantiated. Reassign it to the
1048 new member specialization template. */
1049 spec_entry elt;
1050 spec_entry *entry;
1052 elt.tmpl = most_general_template (tmpl);
1053 elt.args = CLASSTYPE_TI_ARGS (inst);
1054 elt.spec = inst;
1056 type_specializations->remove_elt (&elt);
1058 elt.tmpl = tmpl;
1059 elt.args = INNERMOST_TEMPLATE_ARGS (elt.args);
1061 spec_entry **slot
1062 = type_specializations->find_slot (&elt, INSERT);
1063 entry = ggc_alloc<spec_entry> ();
1064 *entry = elt;
1065 *slot = entry;
1067 else
1068 /* But if we've had an implicit instantiation, that's a
1069 problem ([temp.expl.spec]/6). */
1070 error ("specialization %qT after instantiation %qT",
1071 type, inst);
1074 /* Mark TYPE as a specialization. And as a result, we only
1075 have one level of template argument for the innermost
1076 class template. */
1077 SET_CLASSTYPE_TEMPLATE_SPECIALIZATION (type);
1078 DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)) = input_location;
1079 CLASSTYPE_TI_ARGS (type)
1080 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type));
1083 else if (processing_specialization)
1085 /* Someday C++0x may allow for enum template specialization. */
1086 if (cxx_dialect > cxx98 && TREE_CODE (type) == ENUMERAL_TYPE
1087 && CLASS_TYPE_P (context) && CLASSTYPE_USE_TEMPLATE (context))
1088 pedwarn (input_location, OPT_Wpedantic, "template specialization "
1089 "of %qD not allowed by ISO C++", type);
1090 else
1092 error ("explicit specialization of non-template %qT", type);
1093 return error_mark_node;
1097 return type;
1100 /* Returns nonzero if we can optimize the retrieval of specializations
1101 for TMPL, a TEMPLATE_DECL. In particular, for such a template, we
1102 do not use DECL_TEMPLATE_SPECIALIZATIONS at all. */
1104 static inline bool
1105 optimize_specialization_lookup_p (tree tmpl)
1107 return (DECL_FUNCTION_TEMPLATE_P (tmpl)
1108 && DECL_CLASS_SCOPE_P (tmpl)
1109 /* DECL_CLASS_SCOPE_P holds of T::f even if T is a template
1110 parameter. */
1111 && CLASS_TYPE_P (DECL_CONTEXT (tmpl))
1112 /* The optimized lookup depends on the fact that the
1113 template arguments for the member function template apply
1114 purely to the containing class, which is not true if the
1115 containing class is an explicit or partial
1116 specialization. */
1117 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (tmpl))
1118 && !DECL_MEMBER_TEMPLATE_P (tmpl)
1119 && !DECL_CONV_FN_P (tmpl)
1120 /* It is possible to have a template that is not a member
1121 template and is not a member of a template class:
1123 template <typename T>
1124 struct S { friend A::f(); };
1126 Here, the friend function is a template, but the context does
1127 not have template information. The optimized lookup relies
1128 on having ARGS be the template arguments for both the class
1129 and the function template. */
1130 && !DECL_FRIEND_P (DECL_TEMPLATE_RESULT (tmpl)));
1133 /* Make sure ARGS doesn't use any inappropriate typedefs; we should have
1134 gone through coerce_template_parms by now. */
1136 static void
1137 verify_unstripped_args_1 (tree inner)
1139 for (int i = 0; i < TREE_VEC_LENGTH (inner); ++i)
1141 tree arg = TREE_VEC_ELT (inner, i);
1142 if (TREE_CODE (arg) == TEMPLATE_DECL)
1143 /* OK */;
1144 else if (TYPE_P (arg))
1145 gcc_assert (strip_typedefs (arg, NULL) == arg);
1146 else if (ARGUMENT_PACK_P (arg))
1147 verify_unstripped_args_1 (ARGUMENT_PACK_ARGS (arg));
1148 else if (strip_typedefs (TREE_TYPE (arg), NULL) != TREE_TYPE (arg))
1149 /* Allow typedefs on the type of a non-type argument, since a
1150 parameter can have them. */;
1151 else
1152 gcc_assert (strip_typedefs_expr (arg, NULL) == arg);
1156 static void
1157 verify_unstripped_args (tree args)
1159 ++processing_template_decl;
1160 if (!any_dependent_template_arguments_p (args))
1161 verify_unstripped_args_1 (INNERMOST_TEMPLATE_ARGS (args));
1162 --processing_template_decl;
1165 /* Retrieve the specialization (in the sense of [temp.spec] - a
1166 specialization is either an instantiation or an explicit
1167 specialization) of TMPL for the given template ARGS. If there is
1168 no such specialization, return NULL_TREE. The ARGS are a vector of
1169 arguments, or a vector of vectors of arguments, in the case of
1170 templates with more than one level of parameters.
1172 If TMPL is a type template and CLASS_SPECIALIZATIONS_P is true,
1173 then we search for a partial specialization matching ARGS. This
1174 parameter is ignored if TMPL is not a class template.
1176 We can also look up a FIELD_DECL, if it is a lambda capture pack; the
1177 result is a NONTYPE_ARGUMENT_PACK. */
1179 static tree
1180 retrieve_specialization (tree tmpl, tree args, hashval_t hash)
1182 if (tmpl == NULL_TREE)
1183 return NULL_TREE;
1185 if (args == error_mark_node)
1186 return NULL_TREE;
1188 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL
1189 || TREE_CODE (tmpl) == FIELD_DECL);
1191 /* There should be as many levels of arguments as there are
1192 levels of parameters. */
1193 gcc_assert (TMPL_ARGS_DEPTH (args)
1194 == (TREE_CODE (tmpl) == TEMPLATE_DECL
1195 ? TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl))
1196 : template_class_depth (DECL_CONTEXT (tmpl))));
1198 if (flag_checking)
1199 verify_unstripped_args (args);
1201 /* Lambda functions in templates aren't instantiated normally, but through
1202 tsubst_lambda_expr. */
1203 if (lambda_fn_in_template_p (tmpl))
1204 return NULL_TREE;
1206 if (optimize_specialization_lookup_p (tmpl))
1208 /* The template arguments actually apply to the containing
1209 class. Find the class specialization with those
1210 arguments. */
1211 tree class_template = CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (tmpl));
1212 tree class_specialization
1213 = retrieve_specialization (class_template, args, 0);
1214 if (!class_specialization)
1215 return NULL_TREE;
1217 /* Find the instance of TMPL. */
1218 tree fns = get_class_binding (class_specialization, DECL_NAME (tmpl));
1219 for (ovl_iterator iter (fns); iter; ++iter)
1221 tree fn = *iter;
1222 if (DECL_TEMPLATE_INFO (fn) && DECL_TI_TEMPLATE (fn) == tmpl
1223 /* using-declarations can add base methods to the method vec,
1224 and we don't want those here. */
1225 && DECL_CONTEXT (fn) == class_specialization)
1226 return fn;
1228 return NULL_TREE;
1230 else
1232 spec_entry *found;
1233 spec_entry elt;
1234 hash_table<spec_hasher> *specializations;
1236 elt.tmpl = tmpl;
1237 elt.args = args;
1238 elt.spec = NULL_TREE;
1240 if (DECL_CLASS_TEMPLATE_P (tmpl))
1241 specializations = type_specializations;
1242 else
1243 specializations = decl_specializations;
1245 if (hash == 0)
1246 hash = spec_hasher::hash (&elt);
1247 found = specializations->find_with_hash (&elt, hash);
1248 if (found)
1249 return found->spec;
1252 return NULL_TREE;
1255 /* Like retrieve_specialization, but for local declarations. */
1257 tree
1258 retrieve_local_specialization (tree tmpl)
1260 if (local_specializations == NULL)
1261 return NULL_TREE;
1263 tree *slot = local_specializations->get (tmpl);
1264 return slot ? *slot : NULL_TREE;
1267 /* Returns nonzero iff DECL is a specialization of TMPL. */
1270 is_specialization_of (tree decl, tree tmpl)
1272 tree t;
1274 if (TREE_CODE (decl) == FUNCTION_DECL)
1276 for (t = decl;
1277 t != NULL_TREE;
1278 t = DECL_TEMPLATE_INFO (t) ? DECL_TI_TEMPLATE (t) : NULL_TREE)
1279 if (t == tmpl)
1280 return 1;
1282 else
1284 gcc_assert (TREE_CODE (decl) == TYPE_DECL);
1286 for (t = TREE_TYPE (decl);
1287 t != NULL_TREE;
1288 t = CLASSTYPE_USE_TEMPLATE (t)
1289 ? TREE_TYPE (CLASSTYPE_TI_TEMPLATE (t)) : NULL_TREE)
1290 if (same_type_ignoring_top_level_qualifiers_p (t, TREE_TYPE (tmpl)))
1291 return 1;
1294 return 0;
1297 /* Returns nonzero iff DECL is a specialization of friend declaration
1298 FRIEND_DECL according to [temp.friend]. */
1300 bool
1301 is_specialization_of_friend (tree decl, tree friend_decl)
1303 bool need_template = true;
1304 int template_depth;
1306 gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
1307 || TREE_CODE (decl) == TYPE_DECL);
1309 /* For [temp.friend/6] when FRIEND_DECL is an ordinary member function
1310 of a template class, we want to check if DECL is a specialization
1311 if this. */
1312 if (TREE_CODE (friend_decl) == FUNCTION_DECL
1313 && DECL_TEMPLATE_INFO (friend_decl)
1314 && !DECL_USE_TEMPLATE (friend_decl))
1316 /* We want a TEMPLATE_DECL for `is_specialization_of'. */
1317 friend_decl = DECL_TI_TEMPLATE (friend_decl);
1318 need_template = false;
1320 else if (TREE_CODE (friend_decl) == TEMPLATE_DECL
1321 && !PRIMARY_TEMPLATE_P (friend_decl))
1322 need_template = false;
1324 /* There is nothing to do if this is not a template friend. */
1325 if (TREE_CODE (friend_decl) != TEMPLATE_DECL)
1326 return false;
1328 if (is_specialization_of (decl, friend_decl))
1329 return true;
1331 /* [temp.friend/6]
1332 A member of a class template may be declared to be a friend of a
1333 non-template class. In this case, the corresponding member of
1334 every specialization of the class template is a friend of the
1335 class granting friendship.
1337 For example, given a template friend declaration
1339 template <class T> friend void A<T>::f();
1341 the member function below is considered a friend
1343 template <> struct A<int> {
1344 void f();
1347 For this type of template friend, TEMPLATE_DEPTH below will be
1348 nonzero. To determine if DECL is a friend of FRIEND, we first
1349 check if the enclosing class is a specialization of another. */
1351 template_depth = template_class_depth (CP_DECL_CONTEXT (friend_decl));
1352 if (template_depth
1353 && DECL_CLASS_SCOPE_P (decl)
1354 && is_specialization_of (TYPE_NAME (DECL_CONTEXT (decl)),
1355 CLASSTYPE_TI_TEMPLATE (DECL_CONTEXT (friend_decl))))
1357 /* Next, we check the members themselves. In order to handle
1358 a few tricky cases, such as when FRIEND_DECL's are
1360 template <class T> friend void A<T>::g(T t);
1361 template <class T> template <T t> friend void A<T>::h();
1363 and DECL's are
1365 void A<int>::g(int);
1366 template <int> void A<int>::h();
1368 we need to figure out ARGS, the template arguments from
1369 the context of DECL. This is required for template substitution
1370 of `T' in the function parameter of `g' and template parameter
1371 of `h' in the above examples. Here ARGS corresponds to `int'. */
1373 tree context = DECL_CONTEXT (decl);
1374 tree args = NULL_TREE;
1375 int current_depth = 0;
1377 while (current_depth < template_depth)
1379 if (CLASSTYPE_TEMPLATE_INFO (context))
1381 if (current_depth == 0)
1382 args = TYPE_TI_ARGS (context);
1383 else
1384 args = add_to_template_args (TYPE_TI_ARGS (context), args);
1385 current_depth++;
1387 context = TYPE_CONTEXT (context);
1390 if (TREE_CODE (decl) == FUNCTION_DECL)
1392 bool is_template;
1393 tree friend_type;
1394 tree decl_type;
1395 tree friend_args_type;
1396 tree decl_args_type;
1398 /* Make sure that both DECL and FRIEND_DECL are templates or
1399 non-templates. */
1400 is_template = DECL_TEMPLATE_INFO (decl)
1401 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl));
1402 if (need_template ^ is_template)
1403 return false;
1404 else if (is_template)
1406 /* If both are templates, check template parameter list. */
1407 tree friend_parms
1408 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1409 args, tf_none);
1410 if (!comp_template_parms
1411 (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (decl)),
1412 friend_parms))
1413 return false;
1415 decl_type = TREE_TYPE (DECL_TI_TEMPLATE (decl));
1417 else
1418 decl_type = TREE_TYPE (decl);
1420 friend_type = tsubst_function_type (TREE_TYPE (friend_decl), args,
1421 tf_none, NULL_TREE);
1422 if (friend_type == error_mark_node)
1423 return false;
1425 /* Check if return types match. */
1426 if (!same_type_p (TREE_TYPE (decl_type), TREE_TYPE (friend_type)))
1427 return false;
1429 /* Check if function parameter types match, ignoring the
1430 `this' parameter. */
1431 friend_args_type = TYPE_ARG_TYPES (friend_type);
1432 decl_args_type = TYPE_ARG_TYPES (decl_type);
1433 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (friend_decl))
1434 friend_args_type = TREE_CHAIN (friend_args_type);
1435 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
1436 decl_args_type = TREE_CHAIN (decl_args_type);
1438 return compparms (decl_args_type, friend_args_type);
1440 else
1442 /* DECL is a TYPE_DECL */
1443 bool is_template;
1444 tree decl_type = TREE_TYPE (decl);
1446 /* Make sure that both DECL and FRIEND_DECL are templates or
1447 non-templates. */
1448 is_template
1449 = CLASSTYPE_TEMPLATE_INFO (decl_type)
1450 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (decl_type));
1452 if (need_template ^ is_template)
1453 return false;
1454 else if (is_template)
1456 tree friend_parms;
1457 /* If both are templates, check the name of the two
1458 TEMPLATE_DECL's first because is_friend didn't. */
1459 if (DECL_NAME (CLASSTYPE_TI_TEMPLATE (decl_type))
1460 != DECL_NAME (friend_decl))
1461 return false;
1463 /* Now check template parameter list. */
1464 friend_parms
1465 = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_decl),
1466 args, tf_none);
1467 return comp_template_parms
1468 (DECL_TEMPLATE_PARMS (CLASSTYPE_TI_TEMPLATE (decl_type)),
1469 friend_parms);
1471 else
1472 return (DECL_NAME (decl)
1473 == DECL_NAME (friend_decl));
1476 return false;
1479 /* Register the specialization SPEC as a specialization of TMPL with
1480 the indicated ARGS. IS_FRIEND indicates whether the specialization
1481 is actually just a friend declaration. ATTRLIST is the list of
1482 attributes that the specialization is declared with or NULL when
1483 it isn't. Returns SPEC, or an equivalent prior declaration, if
1484 available.
1486 We also store instantiations of field packs in the hash table, even
1487 though they are not themselves templates, to make lookup easier. */
1489 static tree
1490 register_specialization (tree spec, tree tmpl, tree args, bool is_friend,
1491 hashval_t hash)
1493 tree fn;
1494 spec_entry **slot = NULL;
1495 spec_entry elt;
1497 gcc_assert ((TREE_CODE (tmpl) == TEMPLATE_DECL && DECL_P (spec))
1498 || (TREE_CODE (tmpl) == FIELD_DECL
1499 && TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK));
1501 if (TREE_CODE (spec) == FUNCTION_DECL
1502 && uses_template_parms (DECL_TI_ARGS (spec)))
1503 /* This is the FUNCTION_DECL for a partial instantiation. Don't
1504 register it; we want the corresponding TEMPLATE_DECL instead.
1505 We use `uses_template_parms (DECL_TI_ARGS (spec))' rather than
1506 the more obvious `uses_template_parms (spec)' to avoid problems
1507 with default function arguments. In particular, given
1508 something like this:
1510 template <class T> void f(T t1, T t = T())
1512 the default argument expression is not substituted for in an
1513 instantiation unless and until it is actually needed. */
1514 return spec;
1516 if (optimize_specialization_lookup_p (tmpl))
1517 /* We don't put these specializations in the hash table, but we might
1518 want to give an error about a mismatch. */
1519 fn = retrieve_specialization (tmpl, args, 0);
1520 else
1522 elt.tmpl = tmpl;
1523 elt.args = args;
1524 elt.spec = spec;
1526 if (hash == 0)
1527 hash = spec_hasher::hash (&elt);
1529 slot =
1530 decl_specializations->find_slot_with_hash (&elt, hash, INSERT);
1531 if (*slot)
1532 fn = ((spec_entry *) *slot)->spec;
1533 else
1534 fn = NULL_TREE;
1537 /* We can sometimes try to re-register a specialization that we've
1538 already got. In particular, regenerate_decl_from_template calls
1539 duplicate_decls which will update the specialization list. But,
1540 we'll still get called again here anyhow. It's more convenient
1541 to simply allow this than to try to prevent it. */
1542 if (fn == spec)
1543 return spec;
1544 else if (fn && DECL_TEMPLATE_SPECIALIZATION (spec))
1546 if (DECL_TEMPLATE_INSTANTIATION (fn))
1548 if (DECL_ODR_USED (fn)
1549 || DECL_EXPLICIT_INSTANTIATION (fn))
1551 error ("specialization of %qD after instantiation",
1552 fn);
1553 return error_mark_node;
1555 else
1557 tree clone;
1558 /* This situation should occur only if the first
1559 specialization is an implicit instantiation, the
1560 second is an explicit specialization, and the
1561 implicit instantiation has not yet been used. That
1562 situation can occur if we have implicitly
1563 instantiated a member function and then specialized
1564 it later.
1566 We can also wind up here if a friend declaration that
1567 looked like an instantiation turns out to be a
1568 specialization:
1570 template <class T> void foo(T);
1571 class S { friend void foo<>(int) };
1572 template <> void foo(int);
1574 We transform the existing DECL in place so that any
1575 pointers to it become pointers to the updated
1576 declaration.
1578 If there was a definition for the template, but not
1579 for the specialization, we want this to look as if
1580 there were no definition, and vice versa. */
1581 DECL_INITIAL (fn) = NULL_TREE;
1582 duplicate_decls (spec, fn, is_friend);
1583 /* The call to duplicate_decls will have applied
1584 [temp.expl.spec]:
1586 An explicit specialization of a function template
1587 is inline only if it is explicitly declared to be,
1588 and independently of whether its function template
1591 to the primary function; now copy the inline bits to
1592 the various clones. */
1593 FOR_EACH_CLONE (clone, fn)
1595 DECL_DECLARED_INLINE_P (clone)
1596 = DECL_DECLARED_INLINE_P (fn);
1597 DECL_SOURCE_LOCATION (clone)
1598 = DECL_SOURCE_LOCATION (fn);
1599 DECL_DELETED_FN (clone)
1600 = DECL_DELETED_FN (fn);
1602 check_specialization_namespace (tmpl);
1604 return fn;
1607 else if (DECL_TEMPLATE_SPECIALIZATION (fn))
1609 tree dd = duplicate_decls (spec, fn, is_friend);
1610 if (dd == error_mark_node)
1611 /* We've already complained in duplicate_decls. */
1612 return error_mark_node;
1614 if (dd == NULL_TREE && DECL_INITIAL (spec))
1615 /* Dup decl failed, but this is a new definition. Set the
1616 line number so any errors match this new
1617 definition. */
1618 DECL_SOURCE_LOCATION (fn) = DECL_SOURCE_LOCATION (spec);
1620 return fn;
1623 else if (fn)
1624 return duplicate_decls (spec, fn, is_friend);
1626 /* A specialization must be declared in the same namespace as the
1627 template it is specializing. */
1628 if (DECL_P (spec) && DECL_TEMPLATE_SPECIALIZATION (spec)
1629 && !check_specialization_namespace (tmpl))
1630 DECL_CONTEXT (spec) = DECL_CONTEXT (tmpl);
1632 if (slot != NULL /* !optimize_specialization_lookup_p (tmpl) */)
1634 spec_entry *entry = ggc_alloc<spec_entry> ();
1635 gcc_assert (tmpl && args && spec);
1636 *entry = elt;
1637 *slot = entry;
1638 if ((TREE_CODE (spec) == FUNCTION_DECL && DECL_NAMESPACE_SCOPE_P (spec)
1639 && PRIMARY_TEMPLATE_P (tmpl)
1640 && DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (tmpl)) == NULL_TREE)
1641 || variable_template_p (tmpl))
1642 /* If TMPL is a forward declaration of a template function, keep a list
1643 of all specializations in case we need to reassign them to a friend
1644 template later in tsubst_friend_function.
1646 Also keep a list of all variable template instantiations so that
1647 process_partial_specialization can check whether a later partial
1648 specialization would have used it. */
1649 DECL_TEMPLATE_INSTANTIATIONS (tmpl)
1650 = tree_cons (args, spec, DECL_TEMPLATE_INSTANTIATIONS (tmpl));
1653 return spec;
1656 /* Returns true iff two spec_entry nodes are equivalent. */
1658 int comparing_specializations;
1660 bool
1661 spec_hasher::equal (spec_entry *e1, spec_entry *e2)
1663 int equal;
1665 ++comparing_specializations;
1666 equal = (e1->tmpl == e2->tmpl
1667 && comp_template_args (e1->args, e2->args));
1668 if (equal && flag_concepts
1669 /* tmpl could be a FIELD_DECL for a capture pack. */
1670 && TREE_CODE (e1->tmpl) == TEMPLATE_DECL
1671 && VAR_P (DECL_TEMPLATE_RESULT (e1->tmpl))
1672 && uses_template_parms (e1->args))
1674 /* Partial specializations of a variable template can be distinguished by
1675 constraints. */
1676 tree c1 = e1->spec ? get_constraints (e1->spec) : NULL_TREE;
1677 tree c2 = e2->spec ? get_constraints (e2->spec) : NULL_TREE;
1678 equal = equivalent_constraints (c1, c2);
1680 --comparing_specializations;
1682 return equal;
1685 /* Returns a hash for a template TMPL and template arguments ARGS. */
1687 static hashval_t
1688 hash_tmpl_and_args (tree tmpl, tree args)
1690 hashval_t val = iterative_hash_object (DECL_UID (tmpl), 0);
1691 return iterative_hash_template_arg (args, val);
1694 /* Returns a hash for a spec_entry node based on the TMPL and ARGS members,
1695 ignoring SPEC. */
1697 hashval_t
1698 spec_hasher::hash (spec_entry *e)
1700 return hash_tmpl_and_args (e->tmpl, e->args);
1703 /* Recursively calculate a hash value for a template argument ARG, for use
1704 in the hash tables of template specializations. */
1706 hashval_t
1707 iterative_hash_template_arg (tree arg, hashval_t val)
1709 unsigned HOST_WIDE_INT i;
1710 enum tree_code code;
1711 char tclass;
1713 if (arg == NULL_TREE)
1714 return iterative_hash_object (arg, val);
1716 if (!TYPE_P (arg))
1717 STRIP_NOPS (arg);
1719 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
1720 gcc_unreachable ();
1722 code = TREE_CODE (arg);
1723 tclass = TREE_CODE_CLASS (code);
1725 val = iterative_hash_object (code, val);
1727 switch (code)
1729 case ERROR_MARK:
1730 return val;
1732 case IDENTIFIER_NODE:
1733 return iterative_hash_object (IDENTIFIER_HASH_VALUE (arg), val);
1735 case TREE_VEC:
1737 int i, len = TREE_VEC_LENGTH (arg);
1738 for (i = 0; i < len; ++i)
1739 val = iterative_hash_template_arg (TREE_VEC_ELT (arg, i), val);
1740 return val;
1743 case TYPE_PACK_EXPANSION:
1744 case EXPR_PACK_EXPANSION:
1745 val = iterative_hash_template_arg (PACK_EXPANSION_PATTERN (arg), val);
1746 return iterative_hash_template_arg (PACK_EXPANSION_EXTRA_ARGS (arg), val);
1748 case TYPE_ARGUMENT_PACK:
1749 case NONTYPE_ARGUMENT_PACK:
1750 return iterative_hash_template_arg (ARGUMENT_PACK_ARGS (arg), val);
1752 case TREE_LIST:
1753 for (; arg; arg = TREE_CHAIN (arg))
1754 val = iterative_hash_template_arg (TREE_VALUE (arg), val);
1755 return val;
1757 case OVERLOAD:
1758 for (lkp_iterator iter (arg); iter; ++iter)
1759 val = iterative_hash_template_arg (*iter, val);
1760 return val;
1762 case CONSTRUCTOR:
1764 tree field, value;
1765 iterative_hash_template_arg (TREE_TYPE (arg), val);
1766 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg), i, field, value)
1768 val = iterative_hash_template_arg (field, val);
1769 val = iterative_hash_template_arg (value, val);
1771 return val;
1774 case PARM_DECL:
1775 if (!DECL_ARTIFICIAL (arg))
1777 val = iterative_hash_object (DECL_PARM_INDEX (arg), val);
1778 val = iterative_hash_object (DECL_PARM_LEVEL (arg), val);
1780 return iterative_hash_template_arg (TREE_TYPE (arg), val);
1782 case TARGET_EXPR:
1783 return iterative_hash_template_arg (TARGET_EXPR_INITIAL (arg), val);
1785 case PTRMEM_CST:
1786 val = iterative_hash_template_arg (PTRMEM_CST_CLASS (arg), val);
1787 return iterative_hash_template_arg (PTRMEM_CST_MEMBER (arg), val);
1789 case TEMPLATE_PARM_INDEX:
1790 val = iterative_hash_template_arg
1791 (TREE_TYPE (TEMPLATE_PARM_DECL (arg)), val);
1792 val = iterative_hash_object (TEMPLATE_PARM_LEVEL (arg), val);
1793 return iterative_hash_object (TEMPLATE_PARM_IDX (arg), val);
1795 case TRAIT_EXPR:
1796 val = iterative_hash_object (TRAIT_EXPR_KIND (arg), val);
1797 val = iterative_hash_template_arg (TRAIT_EXPR_TYPE1 (arg), val);
1798 return iterative_hash_template_arg (TRAIT_EXPR_TYPE2 (arg), val);
1800 case BASELINK:
1801 val = iterative_hash_template_arg (BINFO_TYPE (BASELINK_BINFO (arg)),
1802 val);
1803 return iterative_hash_template_arg (DECL_NAME (get_first_fn (arg)),
1804 val);
1806 case MODOP_EXPR:
1807 val = iterative_hash_template_arg (TREE_OPERAND (arg, 0), val);
1808 code = TREE_CODE (TREE_OPERAND (arg, 1));
1809 val = iterative_hash_object (code, val);
1810 return iterative_hash_template_arg (TREE_OPERAND (arg, 2), val);
1812 case LAMBDA_EXPR:
1813 /* A lambda can't appear in a template arg, but don't crash on
1814 erroneous input. */
1815 gcc_assert (seen_error ());
1816 return val;
1818 case CAST_EXPR:
1819 case IMPLICIT_CONV_EXPR:
1820 case STATIC_CAST_EXPR:
1821 case REINTERPRET_CAST_EXPR:
1822 case CONST_CAST_EXPR:
1823 case DYNAMIC_CAST_EXPR:
1824 case NEW_EXPR:
1825 val = iterative_hash_template_arg (TREE_TYPE (arg), val);
1826 /* Now hash operands as usual. */
1827 break;
1829 default:
1830 break;
1833 switch (tclass)
1835 case tcc_type:
1836 if (alias_template_specialization_p (arg))
1838 // We want an alias specialization that survived strip_typedefs
1839 // to hash differently from its TYPE_CANONICAL, to avoid hash
1840 // collisions that compare as different in template_args_equal.
1841 // These could be dependent specializations that strip_typedefs
1842 // left alone, or untouched specializations because
1843 // coerce_template_parms returns the unconverted template
1844 // arguments if it sees incomplete argument packs.
1845 tree ti = TYPE_ALIAS_TEMPLATE_INFO (arg);
1846 return hash_tmpl_and_args (TI_TEMPLATE (ti), TI_ARGS (ti));
1848 if (TYPE_CANONICAL (arg))
1849 return iterative_hash_object (TYPE_HASH (TYPE_CANONICAL (arg)),
1850 val);
1851 else if (TREE_CODE (arg) == DECLTYPE_TYPE)
1852 return iterative_hash_template_arg (DECLTYPE_TYPE_EXPR (arg), val);
1853 /* Otherwise just compare the types during lookup. */
1854 return val;
1856 case tcc_declaration:
1857 case tcc_constant:
1858 return iterative_hash_expr (arg, val);
1860 default:
1861 gcc_assert (IS_EXPR_CODE_CLASS (tclass));
1863 unsigned n = cp_tree_operand_length (arg);
1864 for (i = 0; i < n; ++i)
1865 val = iterative_hash_template_arg (TREE_OPERAND (arg, i), val);
1866 return val;
1869 gcc_unreachable ();
1870 return 0;
1873 /* Unregister the specialization SPEC as a specialization of TMPL.
1874 Replace it with NEW_SPEC, if NEW_SPEC is non-NULL. Returns true
1875 if the SPEC was listed as a specialization of TMPL.
1877 Note that SPEC has been ggc_freed, so we can't look inside it. */
1879 bool
1880 reregister_specialization (tree spec, tree tinfo, tree new_spec)
1882 spec_entry *entry;
1883 spec_entry elt;
1885 elt.tmpl = most_general_template (TI_TEMPLATE (tinfo));
1886 elt.args = TI_ARGS (tinfo);
1887 elt.spec = NULL_TREE;
1889 entry = decl_specializations->find (&elt);
1890 if (entry != NULL)
1892 gcc_assert (entry->spec == spec || entry->spec == new_spec);
1893 gcc_assert (new_spec != NULL_TREE);
1894 entry->spec = new_spec;
1895 return 1;
1898 return 0;
1901 /* Like register_specialization, but for local declarations. We are
1902 registering SPEC, an instantiation of TMPL. */
1904 void
1905 register_local_specialization (tree spec, tree tmpl)
1907 gcc_assert (tmpl != spec);
1908 local_specializations->put (tmpl, spec);
1911 /* TYPE is a class type. Returns true if TYPE is an explicitly
1912 specialized class. */
1914 bool
1915 explicit_class_specialization_p (tree type)
1917 if (!CLASSTYPE_TEMPLATE_SPECIALIZATION (type))
1918 return false;
1919 return !uses_template_parms (CLASSTYPE_TI_ARGS (type));
1922 /* Print the list of functions at FNS, going through all the overloads
1923 for each element of the list. Alternatively, FNS can not be a
1924 TREE_LIST, in which case it will be printed together with all the
1925 overloads.
1927 MORE and *STR should respectively be FALSE and NULL when the function
1928 is called from the outside. They are used internally on recursive
1929 calls. print_candidates manages the two parameters and leaves NULL
1930 in *STR when it ends. */
1932 static void
1933 print_candidates_1 (tree fns, char **str, bool more = false)
1935 if (TREE_CODE (fns) == TREE_LIST)
1936 for (; fns; fns = TREE_CHAIN (fns))
1937 print_candidates_1 (TREE_VALUE (fns), str, more || TREE_CHAIN (fns));
1938 else
1939 for (lkp_iterator iter (fns); iter;)
1941 tree cand = *iter;
1942 ++iter;
1944 const char *pfx = *str;
1945 if (!pfx)
1947 if (more || iter)
1948 pfx = _("candidates are:");
1949 else
1950 pfx = _("candidate is:");
1951 *str = get_spaces (pfx);
1953 inform (DECL_SOURCE_LOCATION (cand), "%s %#qD", pfx, cand);
1957 /* Print the list of candidate FNS in an error message. FNS can also
1958 be a TREE_LIST of non-functions in the case of an ambiguous lookup. */
1960 void
1961 print_candidates (tree fns)
1963 char *str = NULL;
1964 print_candidates_1 (fns, &str);
1965 free (str);
1968 /* Get a (possibly) constrained template declaration for the
1969 purpose of ordering candidates. */
1970 static tree
1971 get_template_for_ordering (tree list)
1973 gcc_assert (TREE_CODE (list) == TREE_LIST);
1974 tree f = TREE_VALUE (list);
1975 if (tree ti = DECL_TEMPLATE_INFO (f))
1976 return TI_TEMPLATE (ti);
1977 return f;
1980 /* Among candidates having the same signature, return the
1981 most constrained or NULL_TREE if there is no best candidate.
1982 If the signatures of candidates vary (e.g., template
1983 specialization vs. member function), then there can be no
1984 most constrained.
1986 Note that we don't compare constraints on the functions
1987 themselves, but rather those of their templates. */
1988 static tree
1989 most_constrained_function (tree candidates)
1991 // Try to find the best candidate in a first pass.
1992 tree champ = candidates;
1993 for (tree c = TREE_CHAIN (champ); c; c = TREE_CHAIN (c))
1995 int winner = more_constrained (get_template_for_ordering (champ),
1996 get_template_for_ordering (c));
1997 if (winner == -1)
1998 champ = c; // The candidate is more constrained
1999 else if (winner == 0)
2000 return NULL_TREE; // Neither is more constrained
2003 // Verify that the champ is better than previous candidates.
2004 for (tree c = candidates; c != champ; c = TREE_CHAIN (c)) {
2005 if (!more_constrained (get_template_for_ordering (champ),
2006 get_template_for_ordering (c)))
2007 return NULL_TREE;
2010 return champ;
2014 /* Returns the template (one of the functions given by TEMPLATE_ID)
2015 which can be specialized to match the indicated DECL with the
2016 explicit template args given in TEMPLATE_ID. The DECL may be
2017 NULL_TREE if none is available. In that case, the functions in
2018 TEMPLATE_ID are non-members.
2020 If NEED_MEMBER_TEMPLATE is nonzero the function is known to be a
2021 specialization of a member template.
2023 The TEMPLATE_COUNT is the number of references to qualifying
2024 template classes that appeared in the name of the function. See
2025 check_explicit_specialization for a more accurate description.
2027 TSK indicates what kind of template declaration (if any) is being
2028 declared. TSK_TEMPLATE indicates that the declaration given by
2029 DECL, though a FUNCTION_DECL, has template parameters, and is
2030 therefore a template function.
2032 The template args (those explicitly specified and those deduced)
2033 are output in a newly created vector *TARGS_OUT.
2035 If it is impossible to determine the result, an error message is
2036 issued. The error_mark_node is returned to indicate failure. */
2038 static tree
2039 determine_specialization (tree template_id,
2040 tree decl,
2041 tree* targs_out,
2042 int need_member_template,
2043 int template_count,
2044 tmpl_spec_kind tsk)
2046 tree fns;
2047 tree targs;
2048 tree explicit_targs;
2049 tree candidates = NULL_TREE;
2051 /* A TREE_LIST of templates of which DECL may be a specialization.
2052 The TREE_VALUE of each node is a TEMPLATE_DECL. The
2053 corresponding TREE_PURPOSE is the set of template arguments that,
2054 when used to instantiate the template, would produce a function
2055 with the signature of DECL. */
2056 tree templates = NULL_TREE;
2057 int header_count;
2058 cp_binding_level *b;
2060 *targs_out = NULL_TREE;
2062 if (template_id == error_mark_node || decl == error_mark_node)
2063 return error_mark_node;
2065 /* We shouldn't be specializing a member template of an
2066 unspecialized class template; we already gave an error in
2067 check_specialization_scope, now avoid crashing. */
2068 if (!VAR_P (decl)
2069 && template_count && DECL_CLASS_SCOPE_P (decl)
2070 && template_class_depth (DECL_CONTEXT (decl)) > 0)
2072 gcc_assert (errorcount);
2073 return error_mark_node;
2076 fns = TREE_OPERAND (template_id, 0);
2077 explicit_targs = TREE_OPERAND (template_id, 1);
2079 if (fns == error_mark_node)
2080 return error_mark_node;
2082 /* Check for baselinks. */
2083 if (BASELINK_P (fns))
2084 fns = BASELINK_FUNCTIONS (fns);
2086 if (TREE_CODE (decl) == FUNCTION_DECL && !is_overloaded_fn (fns))
2088 error ("%qD is not a function template", fns);
2089 return error_mark_node;
2091 else if (VAR_P (decl) && !variable_template_p (fns))
2093 error ("%qD is not a variable template", fns);
2094 return error_mark_node;
2097 /* Count the number of template headers specified for this
2098 specialization. */
2099 header_count = 0;
2100 for (b = current_binding_level;
2101 b->kind == sk_template_parms;
2102 b = b->level_chain)
2103 ++header_count;
2105 tree orig_fns = fns;
2107 if (variable_template_p (fns))
2109 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (fns));
2110 targs = coerce_template_parms (parms, explicit_targs, fns,
2111 tf_warning_or_error,
2112 /*req_all*/true, /*use_defarg*/true);
2113 if (targs != error_mark_node)
2114 templates = tree_cons (targs, fns, templates);
2116 else for (lkp_iterator iter (fns); iter; ++iter)
2118 tree fn = *iter;
2120 if (TREE_CODE (fn) == TEMPLATE_DECL)
2122 tree decl_arg_types;
2123 tree fn_arg_types;
2124 tree insttype;
2126 /* In case of explicit specialization, we need to check if
2127 the number of template headers appearing in the specialization
2128 is correct. This is usually done in check_explicit_specialization,
2129 but the check done there cannot be exhaustive when specializing
2130 member functions. Consider the following code:
2132 template <> void A<int>::f(int);
2133 template <> template <> void A<int>::f(int);
2135 Assuming that A<int> is not itself an explicit specialization
2136 already, the first line specializes "f" which is a non-template
2137 member function, whilst the second line specializes "f" which
2138 is a template member function. So both lines are syntactically
2139 correct, and check_explicit_specialization does not reject
2140 them.
2142 Here, we can do better, as we are matching the specialization
2143 against the declarations. We count the number of template
2144 headers, and we check if they match TEMPLATE_COUNT + 1
2145 (TEMPLATE_COUNT is the number of qualifying template classes,
2146 plus there must be another header for the member template
2147 itself).
2149 Notice that if header_count is zero, this is not a
2150 specialization but rather a template instantiation, so there
2151 is no check we can perform here. */
2152 if (header_count && header_count != template_count + 1)
2153 continue;
2155 /* Check that the number of template arguments at the
2156 innermost level for DECL is the same as for FN. */
2157 if (current_binding_level->kind == sk_template_parms
2158 && !current_binding_level->explicit_spec_p
2159 && (TREE_VEC_LENGTH (DECL_INNERMOST_TEMPLATE_PARMS (fn))
2160 != TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
2161 (current_template_parms))))
2162 continue;
2164 /* DECL might be a specialization of FN. */
2165 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2166 fn_arg_types = TYPE_ARG_TYPES (TREE_TYPE (fn));
2168 /* For a non-static member function, we need to make sure
2169 that the const qualification is the same. Since
2170 get_bindings does not try to merge the "this" parameter,
2171 we must do the comparison explicitly. */
2172 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn))
2174 if (!same_type_p (TREE_VALUE (fn_arg_types),
2175 TREE_VALUE (decl_arg_types)))
2176 continue;
2178 /* And the ref-qualification. */
2179 if (type_memfn_rqual (TREE_TYPE (decl))
2180 != type_memfn_rqual (TREE_TYPE (fn)))
2181 continue;
2184 /* Skip the "this" parameter and, for constructors of
2185 classes with virtual bases, the VTT parameter. A
2186 full specialization of a constructor will have a VTT
2187 parameter, but a template never will. */
2188 decl_arg_types
2189 = skip_artificial_parms_for (decl, decl_arg_types);
2190 fn_arg_types
2191 = skip_artificial_parms_for (fn, fn_arg_types);
2193 /* Function templates cannot be specializations; there are
2194 no partial specializations of functions. Therefore, if
2195 the type of DECL does not match FN, there is no
2196 match.
2198 Note that it should never be the case that we have both
2199 candidates added here, and for regular member functions
2200 below. */
2201 if (tsk == tsk_template)
2203 if (compparms (fn_arg_types, decl_arg_types))
2204 candidates = tree_cons (NULL_TREE, fn, candidates);
2205 continue;
2208 /* See whether this function might be a specialization of this
2209 template. Suppress access control because we might be trying
2210 to make this specialization a friend, and we have already done
2211 access control for the declaration of the specialization. */
2212 push_deferring_access_checks (dk_no_check);
2213 targs = get_bindings (fn, decl, explicit_targs, /*check_ret=*/true);
2214 pop_deferring_access_checks ();
2216 if (!targs)
2217 /* We cannot deduce template arguments that when used to
2218 specialize TMPL will produce DECL. */
2219 continue;
2221 if (uses_template_parms (targs))
2222 /* We deduced something involving 'auto', which isn't a valid
2223 template argument. */
2224 continue;
2226 /* Remove, from the set of candidates, all those functions
2227 whose constraints are not satisfied. */
2228 if (flag_concepts && !constraints_satisfied_p (fn, targs))
2229 continue;
2231 // Then, try to form the new function type.
2232 insttype = tsubst (TREE_TYPE (fn), targs, tf_fndecl_type, NULL_TREE);
2233 if (insttype == error_mark_node)
2234 continue;
2235 fn_arg_types
2236 = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (insttype));
2237 if (!compparms (fn_arg_types, decl_arg_types))
2238 continue;
2240 /* Save this template, and the arguments deduced. */
2241 templates = tree_cons (targs, fn, templates);
2243 else if (need_member_template)
2244 /* FN is an ordinary member function, and we need a
2245 specialization of a member template. */
2247 else if (TREE_CODE (fn) != FUNCTION_DECL)
2248 /* We can get IDENTIFIER_NODEs here in certain erroneous
2249 cases. */
2251 else if (!DECL_FUNCTION_MEMBER_P (fn))
2252 /* This is just an ordinary non-member function. Nothing can
2253 be a specialization of that. */
2255 else if (DECL_ARTIFICIAL (fn))
2256 /* Cannot specialize functions that are created implicitly. */
2258 else
2260 tree decl_arg_types;
2262 /* This is an ordinary member function. However, since
2263 we're here, we can assume its enclosing class is a
2264 template class. For example,
2266 template <typename T> struct S { void f(); };
2267 template <> void S<int>::f() {}
2269 Here, S<int>::f is a non-template, but S<int> is a
2270 template class. If FN has the same type as DECL, we
2271 might be in business. */
2273 if (!DECL_TEMPLATE_INFO (fn))
2274 /* Its enclosing class is an explicit specialization
2275 of a template class. This is not a candidate. */
2276 continue;
2278 if (!same_type_p (TREE_TYPE (TREE_TYPE (decl)),
2279 TREE_TYPE (TREE_TYPE (fn))))
2280 /* The return types differ. */
2281 continue;
2283 /* Adjust the type of DECL in case FN is a static member. */
2284 decl_arg_types = TYPE_ARG_TYPES (TREE_TYPE (decl));
2285 if (DECL_STATIC_FUNCTION_P (fn)
2286 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2287 decl_arg_types = TREE_CHAIN (decl_arg_types);
2289 if (!compparms (TYPE_ARG_TYPES (TREE_TYPE (fn)),
2290 decl_arg_types))
2291 continue;
2293 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
2294 && (type_memfn_rqual (TREE_TYPE (decl))
2295 != type_memfn_rqual (TREE_TYPE (fn))))
2296 continue;
2298 // If the deduced arguments do not satisfy the constraints,
2299 // this is not a candidate.
2300 if (flag_concepts && !constraints_satisfied_p (fn))
2301 continue;
2303 // Add the candidate.
2304 candidates = tree_cons (NULL_TREE, fn, candidates);
2308 if (templates && TREE_CHAIN (templates))
2310 /* We have:
2312 [temp.expl.spec]
2314 It is possible for a specialization with a given function
2315 signature to be instantiated from more than one function
2316 template. In such cases, explicit specification of the
2317 template arguments must be used to uniquely identify the
2318 function template specialization being specialized.
2320 Note that here, there's no suggestion that we're supposed to
2321 determine which of the candidate templates is most
2322 specialized. However, we, also have:
2324 [temp.func.order]
2326 Partial ordering of overloaded function template
2327 declarations is used in the following contexts to select
2328 the function template to which a function template
2329 specialization refers:
2331 -- when an explicit specialization refers to a function
2332 template.
2334 So, we do use the partial ordering rules, at least for now.
2335 This extension can only serve to make invalid programs valid,
2336 so it's safe. And, there is strong anecdotal evidence that
2337 the committee intended the partial ordering rules to apply;
2338 the EDG front end has that behavior, and John Spicer claims
2339 that the committee simply forgot to delete the wording in
2340 [temp.expl.spec]. */
2341 tree tmpl = most_specialized_instantiation (templates);
2342 if (tmpl != error_mark_node)
2344 templates = tmpl;
2345 TREE_CHAIN (templates) = NULL_TREE;
2349 // Concepts allows multiple declarations of member functions
2350 // with the same signature. Like above, we need to rely on
2351 // on the partial ordering of those candidates to determine which
2352 // is the best.
2353 if (flag_concepts && candidates && TREE_CHAIN (candidates))
2355 if (tree cand = most_constrained_function (candidates))
2357 candidates = cand;
2358 TREE_CHAIN (cand) = NULL_TREE;
2362 if (templates == NULL_TREE && candidates == NULL_TREE)
2364 error ("template-id %qD for %q+D does not match any template "
2365 "declaration", template_id, decl);
2366 if (header_count && header_count != template_count + 1)
2367 inform (input_location, "saw %d %<template<>%>, need %d for "
2368 "specializing a member function template",
2369 header_count, template_count + 1);
2370 else
2371 print_candidates (orig_fns);
2372 return error_mark_node;
2374 else if ((templates && TREE_CHAIN (templates))
2375 || (candidates && TREE_CHAIN (candidates))
2376 || (templates && candidates))
2378 error ("ambiguous template specialization %qD for %q+D",
2379 template_id, decl);
2380 candidates = chainon (candidates, templates);
2381 print_candidates (candidates);
2382 return error_mark_node;
2385 /* We have one, and exactly one, match. */
2386 if (candidates)
2388 tree fn = TREE_VALUE (candidates);
2389 *targs_out = copy_node (DECL_TI_ARGS (fn));
2391 // Propagate the candidate's constraints to the declaration.
2392 set_constraints (decl, get_constraints (fn));
2394 /* DECL is a re-declaration or partial instantiation of a template
2395 function. */
2396 if (TREE_CODE (fn) == TEMPLATE_DECL)
2397 return fn;
2398 /* It was a specialization of an ordinary member function in a
2399 template class. */
2400 return DECL_TI_TEMPLATE (fn);
2403 /* It was a specialization of a template. */
2404 targs = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (TREE_VALUE (templates)));
2405 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
2407 *targs_out = copy_node (targs);
2408 SET_TMPL_ARGS_LEVEL (*targs_out,
2409 TMPL_ARGS_DEPTH (*targs_out),
2410 TREE_PURPOSE (templates));
2412 else
2413 *targs_out = TREE_PURPOSE (templates);
2414 return TREE_VALUE (templates);
2417 /* Returns a chain of parameter types, exactly like the SPEC_TYPES,
2418 but with the default argument values filled in from those in the
2419 TMPL_TYPES. */
2421 static tree
2422 copy_default_args_to_explicit_spec_1 (tree spec_types,
2423 tree tmpl_types)
2425 tree new_spec_types;
2427 if (!spec_types)
2428 return NULL_TREE;
2430 if (spec_types == void_list_node)
2431 return void_list_node;
2433 /* Substitute into the rest of the list. */
2434 new_spec_types =
2435 copy_default_args_to_explicit_spec_1 (TREE_CHAIN (spec_types),
2436 TREE_CHAIN (tmpl_types));
2438 /* Add the default argument for this parameter. */
2439 return hash_tree_cons (TREE_PURPOSE (tmpl_types),
2440 TREE_VALUE (spec_types),
2441 new_spec_types);
2444 /* DECL is an explicit specialization. Replicate default arguments
2445 from the template it specializes. (That way, code like:
2447 template <class T> void f(T = 3);
2448 template <> void f(double);
2449 void g () { f (); }
2451 works, as required.) An alternative approach would be to look up
2452 the correct default arguments at the call-site, but this approach
2453 is consistent with how implicit instantiations are handled. */
2455 static void
2456 copy_default_args_to_explicit_spec (tree decl)
2458 tree tmpl;
2459 tree spec_types;
2460 tree tmpl_types;
2461 tree new_spec_types;
2462 tree old_type;
2463 tree new_type;
2464 tree t;
2465 tree object_type = NULL_TREE;
2466 tree in_charge = NULL_TREE;
2467 tree vtt = NULL_TREE;
2469 /* See if there's anything we need to do. */
2470 tmpl = DECL_TI_TEMPLATE (decl);
2471 tmpl_types = TYPE_ARG_TYPES (TREE_TYPE (DECL_TEMPLATE_RESULT (tmpl)));
2472 for (t = tmpl_types; t; t = TREE_CHAIN (t))
2473 if (TREE_PURPOSE (t))
2474 break;
2475 if (!t)
2476 return;
2478 old_type = TREE_TYPE (decl);
2479 spec_types = TYPE_ARG_TYPES (old_type);
2481 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
2483 /* Remove the this pointer, but remember the object's type for
2484 CV quals. */
2485 object_type = TREE_TYPE (TREE_VALUE (spec_types));
2486 spec_types = TREE_CHAIN (spec_types);
2487 tmpl_types = TREE_CHAIN (tmpl_types);
2489 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
2491 /* DECL may contain more parameters than TMPL due to the extra
2492 in-charge parameter in constructors and destructors. */
2493 in_charge = spec_types;
2494 spec_types = TREE_CHAIN (spec_types);
2496 if (DECL_HAS_VTT_PARM_P (decl))
2498 vtt = spec_types;
2499 spec_types = TREE_CHAIN (spec_types);
2503 /* Compute the merged default arguments. */
2504 new_spec_types =
2505 copy_default_args_to_explicit_spec_1 (spec_types, tmpl_types);
2507 /* Compute the new FUNCTION_TYPE. */
2508 if (object_type)
2510 if (vtt)
2511 new_spec_types = hash_tree_cons (TREE_PURPOSE (vtt),
2512 TREE_VALUE (vtt),
2513 new_spec_types);
2515 if (in_charge)
2516 /* Put the in-charge parameter back. */
2517 new_spec_types = hash_tree_cons (TREE_PURPOSE (in_charge),
2518 TREE_VALUE (in_charge),
2519 new_spec_types);
2521 new_type = build_method_type_directly (object_type,
2522 TREE_TYPE (old_type),
2523 new_spec_types);
2525 else
2526 new_type = build_function_type (TREE_TYPE (old_type),
2527 new_spec_types);
2528 new_type = cp_build_type_attribute_variant (new_type,
2529 TYPE_ATTRIBUTES (old_type));
2530 new_type = build_exception_variant (new_type,
2531 TYPE_RAISES_EXCEPTIONS (old_type));
2533 if (TYPE_HAS_LATE_RETURN_TYPE (old_type))
2534 TYPE_HAS_LATE_RETURN_TYPE (new_type) = 1;
2536 TREE_TYPE (decl) = new_type;
2539 /* Return the number of template headers we expect to see for a definition
2540 or specialization of CTYPE or one of its non-template members. */
2543 num_template_headers_for_class (tree ctype)
2545 int num_templates = 0;
2547 while (ctype && CLASS_TYPE_P (ctype))
2549 /* You're supposed to have one `template <...>' for every
2550 template class, but you don't need one for a full
2551 specialization. For example:
2553 template <class T> struct S{};
2554 template <> struct S<int> { void f(); };
2555 void S<int>::f () {}
2557 is correct; there shouldn't be a `template <>' for the
2558 definition of `S<int>::f'. */
2559 if (!CLASSTYPE_TEMPLATE_INFO (ctype))
2560 /* If CTYPE does not have template information of any
2561 kind, then it is not a template, nor is it nested
2562 within a template. */
2563 break;
2564 if (explicit_class_specialization_p (ctype))
2565 break;
2566 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (ctype)))
2567 ++num_templates;
2569 ctype = TYPE_CONTEXT (ctype);
2572 return num_templates;
2575 /* Do a simple sanity check on the template headers that precede the
2576 variable declaration DECL. */
2578 void
2579 check_template_variable (tree decl)
2581 tree ctx = CP_DECL_CONTEXT (decl);
2582 int wanted = num_template_headers_for_class (ctx);
2583 if (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl)
2584 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
2586 if (cxx_dialect < cxx14)
2587 pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2588 "variable templates only available with "
2589 "-std=c++14 or -std=gnu++14");
2591 // Namespace-scope variable templates should have a template header.
2592 ++wanted;
2594 if (template_header_count > wanted)
2596 bool warned = pedwarn (DECL_SOURCE_LOCATION (decl), 0,
2597 "too many template headers for %qD "
2598 "(should be %d)",
2599 decl, wanted);
2600 if (warned && CLASS_TYPE_P (ctx)
2601 && CLASSTYPE_TEMPLATE_SPECIALIZATION (ctx))
2602 inform (DECL_SOURCE_LOCATION (decl),
2603 "members of an explicitly specialized class are defined "
2604 "without a template header");
2608 /* An explicit specialization whose declarator-id or class-head-name is not
2609 qualified shall be declared in the nearest enclosing namespace of the
2610 template, or, if the namespace is inline (7.3.1), any namespace from its
2611 enclosing namespace set.
2613 If the name declared in the explicit instantiation is an unqualified name,
2614 the explicit instantiation shall appear in the namespace where its template
2615 is declared or, if that namespace is inline (7.3.1), any namespace from its
2616 enclosing namespace set. */
2618 void
2619 check_unqualified_spec_or_inst (tree t, location_t loc)
2621 tree tmpl = most_general_template (t);
2622 if (DECL_NAMESPACE_SCOPE_P (tmpl)
2623 && !is_nested_namespace (current_namespace,
2624 CP_DECL_CONTEXT (tmpl), true))
2626 if (processing_specialization)
2627 permerror (loc, "explicit specialization of %qD outside its "
2628 "namespace must use a nested-name-specifier", tmpl);
2629 else if (processing_explicit_instantiation
2630 && cxx_dialect >= cxx11)
2631 /* This was allowed in C++98, so only pedwarn. */
2632 pedwarn (loc, OPT_Wpedantic, "explicit instantiation of %qD "
2633 "outside its namespace must use a nested-name-"
2634 "specifier", tmpl);
2638 /* Warn for a template specialization SPEC that is missing some of a set
2639 of function or type attributes that the template TEMPL is declared with.
2640 ATTRLIST is a list of additional attributes that SPEC should be taken
2641 to ultimately be declared with. */
2643 static void
2644 warn_spec_missing_attributes (tree tmpl, tree spec, tree attrlist)
2646 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
2647 tmpl = DECL_TEMPLATE_RESULT (tmpl);
2649 if (TREE_CODE (tmpl) != FUNCTION_DECL)
2650 return;
2652 /* Avoid warning if either declaration or its type is deprecated. */
2653 if (TREE_DEPRECATED (tmpl)
2654 || TREE_DEPRECATED (spec))
2655 return;
2657 tree tmpl_type = TREE_TYPE (tmpl);
2658 tree spec_type = TREE_TYPE (spec);
2660 if (TREE_DEPRECATED (tmpl_type)
2661 || TREE_DEPRECATED (spec_type)
2662 || TREE_DEPRECATED (TREE_TYPE (tmpl_type))
2663 || TREE_DEPRECATED (TREE_TYPE (spec_type)))
2664 return;
2666 tree tmpl_attrs[] = { DECL_ATTRIBUTES (tmpl), TYPE_ATTRIBUTES (tmpl_type) };
2667 tree spec_attrs[] = { DECL_ATTRIBUTES (spec), TYPE_ATTRIBUTES (spec_type) };
2669 if (!spec_attrs[0])
2670 spec_attrs[0] = attrlist;
2671 else if (!spec_attrs[1])
2672 spec_attrs[1] = attrlist;
2674 /* Avoid warning if the primary has no attributes. */
2675 if (!tmpl_attrs[0] && !tmpl_attrs[1])
2676 return;
2678 /* Avoid warning if either declaration contains an attribute on
2679 the white list below. */
2680 const char* const whitelist[] = {
2681 "error", "warning"
2684 for (unsigned i = 0; i != 2; ++i)
2685 for (unsigned j = 0; j != sizeof whitelist / sizeof *whitelist; ++j)
2686 if (lookup_attribute (whitelist[j], tmpl_attrs[i])
2687 || lookup_attribute (whitelist[j], spec_attrs[i]))
2688 return;
2690 /* Avoid warning if the difference between the primary and
2691 the specialization is not in one of the attributes below. */
2692 const char* const blacklist[] = {
2693 "alloc_align", "alloc_size", "assume_aligned", "format",
2694 "format_arg", "malloc", "nonnull"
2697 /* Put together a list of the black listed attributes that the primary
2698 template is declared with that the specialization is not, in case
2699 it's not apparent from the most recent declaration of the primary. */
2700 unsigned nattrs = 0;
2701 pretty_printer str;
2703 for (unsigned i = 0; i != sizeof blacklist / sizeof *blacklist; ++i)
2705 for (unsigned j = 0; j != 2; ++j)
2707 if (!lookup_attribute (blacklist[i], tmpl_attrs[j]))
2708 continue;
2710 for (unsigned k = 0; k != 1 + !!spec_attrs[1]; ++k)
2712 if (lookup_attribute (blacklist[i], spec_attrs[k]))
2713 break;
2715 if (nattrs)
2716 pp_string (&str, ", ");
2717 pp_begin_quote (&str, pp_show_color (global_dc->printer));
2718 pp_string (&str, blacklist[i]);
2719 pp_end_quote (&str, pp_show_color (global_dc->printer));
2720 ++nattrs;
2725 if (!nattrs)
2726 return;
2728 if (warning_at (DECL_SOURCE_LOCATION (spec), OPT_Wmissing_attributes,
2729 "explicit specialization %q#D may be missing attributes",
2730 spec))
2731 inform (DECL_SOURCE_LOCATION (tmpl),
2732 nattrs > 1
2733 ? G_("missing primary template attributes %s")
2734 : G_("missing primary template attribute %s"),
2735 pp_formatted_text (&str));
2738 /* Check to see if the function just declared, as indicated in
2739 DECLARATOR, and in DECL, is a specialization of a function
2740 template. We may also discover that the declaration is an explicit
2741 instantiation at this point.
2743 Returns DECL, or an equivalent declaration that should be used
2744 instead if all goes well. Issues an error message if something is
2745 amiss. Returns error_mark_node if the error is not easily
2746 recoverable.
2748 FLAGS is a bitmask consisting of the following flags:
2750 2: The function has a definition.
2751 4: The function is a friend.
2753 The TEMPLATE_COUNT is the number of references to qualifying
2754 template classes that appeared in the name of the function. For
2755 example, in
2757 template <class T> struct S { void f(); };
2758 void S<int>::f();
2760 the TEMPLATE_COUNT would be 1. However, explicitly specialized
2761 classes are not counted in the TEMPLATE_COUNT, so that in
2763 template <class T> struct S {};
2764 template <> struct S<int> { void f(); }
2765 template <> void S<int>::f();
2767 the TEMPLATE_COUNT would be 0. (Note that this declaration is
2768 invalid; there should be no template <>.)
2770 If the function is a specialization, it is marked as such via
2771 DECL_TEMPLATE_SPECIALIZATION. Furthermore, its DECL_TEMPLATE_INFO
2772 is set up correctly, and it is added to the list of specializations
2773 for that template. */
2775 tree
2776 check_explicit_specialization (tree declarator,
2777 tree decl,
2778 int template_count,
2779 int flags,
2780 tree attrlist)
2782 int have_def = flags & 2;
2783 int is_friend = flags & 4;
2784 bool is_concept = flags & 8;
2785 int specialization = 0;
2786 int explicit_instantiation = 0;
2787 int member_specialization = 0;
2788 tree ctype = DECL_CLASS_CONTEXT (decl);
2789 tree dname = DECL_NAME (decl);
2790 tmpl_spec_kind tsk;
2792 if (is_friend)
2794 if (!processing_specialization)
2795 tsk = tsk_none;
2796 else
2797 tsk = tsk_excessive_parms;
2799 else
2800 tsk = current_tmpl_spec_kind (template_count);
2802 switch (tsk)
2804 case tsk_none:
2805 if (processing_specialization && !VAR_P (decl))
2807 specialization = 1;
2808 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2810 else if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2812 if (is_friend)
2813 /* This could be something like:
2815 template <class T> void f(T);
2816 class S { friend void f<>(int); } */
2817 specialization = 1;
2818 else
2820 /* This case handles bogus declarations like template <>
2821 template <class T> void f<int>(); */
2823 error ("template-id %qD in declaration of primary template",
2824 declarator);
2825 return decl;
2828 break;
2830 case tsk_invalid_member_spec:
2831 /* The error has already been reported in
2832 check_specialization_scope. */
2833 return error_mark_node;
2835 case tsk_invalid_expl_inst:
2836 error ("template parameter list used in explicit instantiation");
2838 /* Fall through. */
2840 case tsk_expl_inst:
2841 if (have_def)
2842 error ("definition provided for explicit instantiation");
2844 explicit_instantiation = 1;
2845 break;
2847 case tsk_excessive_parms:
2848 case tsk_insufficient_parms:
2849 if (tsk == tsk_excessive_parms)
2850 error ("too many template parameter lists in declaration of %qD",
2851 decl);
2852 else if (template_header_count)
2853 error("too few template parameter lists in declaration of %qD", decl);
2854 else
2855 error("explicit specialization of %qD must be introduced by "
2856 "%<template <>%>", decl);
2858 /* Fall through. */
2859 case tsk_expl_spec:
2860 if (is_concept)
2861 error ("explicit specialization declared %<concept%>");
2863 if (VAR_P (decl) && TREE_CODE (declarator) != TEMPLATE_ID_EXPR)
2864 /* In cases like template<> constexpr bool v = true;
2865 We'll give an error in check_template_variable. */
2866 break;
2868 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2869 if (ctype)
2870 member_specialization = 1;
2871 else
2872 specialization = 1;
2873 break;
2875 case tsk_template:
2876 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
2878 /* This case handles bogus declarations like template <>
2879 template <class T> void f<int>(); */
2881 if (!uses_template_parms (declarator))
2882 error ("template-id %qD in declaration of primary template",
2883 declarator);
2884 else if (variable_template_p (TREE_OPERAND (declarator, 0)))
2886 /* Partial specialization of variable template. */
2887 SET_DECL_TEMPLATE_SPECIALIZATION (decl);
2888 specialization = 1;
2889 goto ok;
2891 else if (cxx_dialect < cxx14)
2892 error ("non-type partial specialization %qD "
2893 "is not allowed", declarator);
2894 else
2895 error ("non-class, non-variable partial specialization %qD "
2896 "is not allowed", declarator);
2897 return decl;
2898 ok:;
2901 if (ctype && CLASSTYPE_TEMPLATE_INSTANTIATION (ctype))
2902 /* This is a specialization of a member template, without
2903 specialization the containing class. Something like:
2905 template <class T> struct S {
2906 template <class U> void f (U);
2908 template <> template <class U> void S<int>::f(U) {}
2910 That's a specialization -- but of the entire template. */
2911 specialization = 1;
2912 break;
2914 default:
2915 gcc_unreachable ();
2918 if ((specialization || member_specialization)
2919 /* This doesn't apply to variable templates. */
2920 && (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE
2921 || TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE))
2923 tree t = TYPE_ARG_TYPES (TREE_TYPE (decl));
2924 for (; t; t = TREE_CHAIN (t))
2925 if (TREE_PURPOSE (t))
2927 permerror (input_location,
2928 "default argument specified in explicit specialization");
2929 break;
2933 if (specialization || member_specialization || explicit_instantiation)
2935 tree tmpl = NULL_TREE;
2936 tree targs = NULL_TREE;
2937 bool was_template_id = (TREE_CODE (declarator) == TEMPLATE_ID_EXPR);
2939 /* Make sure that the declarator is a TEMPLATE_ID_EXPR. */
2940 if (!was_template_id)
2942 tree fns;
2944 gcc_assert (identifier_p (declarator));
2945 if (ctype)
2946 fns = dname;
2947 else
2949 /* If there is no class context, the explicit instantiation
2950 must be at namespace scope. */
2951 gcc_assert (DECL_NAMESPACE_SCOPE_P (decl));
2953 /* Find the namespace binding, using the declaration
2954 context. */
2955 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2956 false, true);
2957 if (fns == error_mark_node)
2958 /* If lookup fails, look for a friend declaration so we can
2959 give a better diagnostic. */
2960 fns = lookup_qualified_name (CP_DECL_CONTEXT (decl), dname,
2961 /*type*/false, /*complain*/true,
2962 /*hidden*/true);
2964 if (fns == error_mark_node || !is_overloaded_fn (fns))
2966 error ("%qD is not a template function", dname);
2967 fns = error_mark_node;
2971 declarator = lookup_template_function (fns, NULL_TREE);
2974 if (declarator == error_mark_node)
2975 return error_mark_node;
2977 if (ctype != NULL_TREE && TYPE_BEING_DEFINED (ctype))
2979 if (!explicit_instantiation)
2980 /* A specialization in class scope. This is invalid,
2981 but the error will already have been flagged by
2982 check_specialization_scope. */
2983 return error_mark_node;
2984 else
2986 /* It's not valid to write an explicit instantiation in
2987 class scope, e.g.:
2989 class C { template void f(); }
2991 This case is caught by the parser. However, on
2992 something like:
2994 template class C { void f(); };
2996 (which is invalid) we can get here. The error will be
2997 issued later. */
3001 return decl;
3003 else if (ctype != NULL_TREE
3004 && (identifier_p (TREE_OPERAND (declarator, 0))))
3006 // We'll match variable templates in start_decl.
3007 if (VAR_P (decl))
3008 return decl;
3010 /* Find the list of functions in ctype that have the same
3011 name as the declared function. */
3012 tree name = TREE_OPERAND (declarator, 0);
3014 if (constructor_name_p (name, ctype))
3016 if (DECL_CONSTRUCTOR_P (decl)
3017 ? !TYPE_HAS_USER_CONSTRUCTOR (ctype)
3018 : !CLASSTYPE_DESTRUCTOR (ctype))
3020 /* From [temp.expl.spec]:
3022 If such an explicit specialization for the member
3023 of a class template names an implicitly-declared
3024 special member function (clause _special_), the
3025 program is ill-formed.
3027 Similar language is found in [temp.explicit]. */
3028 error ("specialization of implicitly-declared special member function");
3029 return error_mark_node;
3032 name = DECL_NAME (decl);
3035 /* For a type-conversion operator, We might be looking for
3036 `operator int' which will be a specialization of
3037 `operator T'. Grab all the conversion operators, and
3038 then select from them. */
3039 tree fns = get_class_binding (ctype, IDENTIFIER_CONV_OP_P (name)
3040 ? conv_op_identifier : name);
3042 if (fns == NULL_TREE)
3044 error ("no member function %qD declared in %qT", name, ctype);
3045 return error_mark_node;
3047 else
3048 TREE_OPERAND (declarator, 0) = fns;
3051 /* Figure out what exactly is being specialized at this point.
3052 Note that for an explicit instantiation, even one for a
3053 member function, we cannot tell a priori whether the
3054 instantiation is for a member template, or just a member
3055 function of a template class. Even if a member template is
3056 being instantiated, the member template arguments may be
3057 elided if they can be deduced from the rest of the
3058 declaration. */
3059 tmpl = determine_specialization (declarator, decl,
3060 &targs,
3061 member_specialization,
3062 template_count,
3063 tsk);
3065 if (!tmpl || tmpl == error_mark_node)
3066 /* We couldn't figure out what this declaration was
3067 specializing. */
3068 return error_mark_node;
3069 else
3071 if (TREE_CODE (decl) == FUNCTION_DECL
3072 && DECL_HIDDEN_FRIEND_P (tmpl))
3074 if (pedwarn (DECL_SOURCE_LOCATION (decl), 0,
3075 "friend declaration %qD is not visible to "
3076 "explicit specialization", tmpl))
3077 inform (DECL_SOURCE_LOCATION (tmpl),
3078 "friend declaration here");
3080 else if (!ctype && !is_friend
3081 && CP_DECL_CONTEXT (decl) == current_namespace)
3082 check_unqualified_spec_or_inst (tmpl, DECL_SOURCE_LOCATION (decl));
3084 tree gen_tmpl = most_general_template (tmpl);
3086 if (explicit_instantiation)
3088 /* We don't set DECL_EXPLICIT_INSTANTIATION here; that
3089 is done by do_decl_instantiation later. */
3091 int arg_depth = TMPL_ARGS_DEPTH (targs);
3092 int parm_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
3094 if (arg_depth > parm_depth)
3096 /* If TMPL is not the most general template (for
3097 example, if TMPL is a friend template that is
3098 injected into namespace scope), then there will
3099 be too many levels of TARGS. Remove some of them
3100 here. */
3101 int i;
3102 tree new_targs;
3104 new_targs = make_tree_vec (parm_depth);
3105 for (i = arg_depth - parm_depth; i < arg_depth; ++i)
3106 TREE_VEC_ELT (new_targs, i - (arg_depth - parm_depth))
3107 = TREE_VEC_ELT (targs, i);
3108 targs = new_targs;
3111 return instantiate_template (tmpl, targs, tf_error);
3114 /* If we thought that the DECL was a member function, but it
3115 turns out to be specializing a static member function,
3116 make DECL a static member function as well. */
3117 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
3118 && DECL_STATIC_FUNCTION_P (tmpl)
3119 && DECL_NONSTATIC_MEMBER_FUNCTION_P (decl))
3120 revert_static_member_fn (decl);
3122 /* If this is a specialization of a member template of a
3123 template class, we want to return the TEMPLATE_DECL, not
3124 the specialization of it. */
3125 if (tsk == tsk_template && !was_template_id)
3127 tree result = DECL_TEMPLATE_RESULT (tmpl);
3128 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
3129 DECL_INITIAL (result) = NULL_TREE;
3130 if (have_def)
3132 tree parm;
3133 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
3134 DECL_SOURCE_LOCATION (result)
3135 = DECL_SOURCE_LOCATION (decl);
3136 /* We want to use the argument list specified in the
3137 definition, not in the original declaration. */
3138 DECL_ARGUMENTS (result) = DECL_ARGUMENTS (decl);
3139 for (parm = DECL_ARGUMENTS (result); parm;
3140 parm = DECL_CHAIN (parm))
3141 DECL_CONTEXT (parm) = result;
3143 return register_specialization (tmpl, gen_tmpl, targs,
3144 is_friend, 0);
3147 /* Set up the DECL_TEMPLATE_INFO for DECL. */
3148 DECL_TEMPLATE_INFO (decl) = build_template_info (tmpl, targs);
3150 if (was_template_id)
3151 TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl)) = true;
3153 /* Inherit default function arguments from the template
3154 DECL is specializing. */
3155 if (DECL_FUNCTION_TEMPLATE_P (tmpl))
3156 copy_default_args_to_explicit_spec (decl);
3158 /* This specialization has the same protection as the
3159 template it specializes. */
3160 TREE_PRIVATE (decl) = TREE_PRIVATE (gen_tmpl);
3161 TREE_PROTECTED (decl) = TREE_PROTECTED (gen_tmpl);
3163 /* 7.1.1-1 [dcl.stc]
3165 A storage-class-specifier shall not be specified in an
3166 explicit specialization...
3168 The parser rejects these, so unless action is taken here,
3169 explicit function specializations will always appear with
3170 global linkage.
3172 The action recommended by the C++ CWG in response to C++
3173 defect report 605 is to make the storage class and linkage
3174 of the explicit specialization match the templated function:
3176 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#605
3178 if (tsk == tsk_expl_spec && DECL_FUNCTION_TEMPLATE_P (gen_tmpl))
3180 tree tmpl_func = DECL_TEMPLATE_RESULT (gen_tmpl);
3181 gcc_assert (TREE_CODE (tmpl_func) == FUNCTION_DECL);
3183 /* A concept cannot be specialized. */
3184 if (DECL_DECLARED_CONCEPT_P (tmpl_func))
3186 error ("explicit specialization of function concept %qD",
3187 gen_tmpl);
3188 return error_mark_node;
3191 /* This specialization has the same linkage and visibility as
3192 the function template it specializes. */
3193 TREE_PUBLIC (decl) = TREE_PUBLIC (tmpl_func);
3194 if (! TREE_PUBLIC (decl))
3196 DECL_INTERFACE_KNOWN (decl) = 1;
3197 DECL_NOT_REALLY_EXTERN (decl) = 1;
3199 DECL_THIS_STATIC (decl) = DECL_THIS_STATIC (tmpl_func);
3200 if (DECL_VISIBILITY_SPECIFIED (tmpl_func))
3202 DECL_VISIBILITY_SPECIFIED (decl) = 1;
3203 DECL_VISIBILITY (decl) = DECL_VISIBILITY (tmpl_func);
3207 /* If DECL is a friend declaration, declared using an
3208 unqualified name, the namespace associated with DECL may
3209 have been set incorrectly. For example, in:
3211 template <typename T> void f(T);
3212 namespace N {
3213 struct S { friend void f<int>(int); }
3216 we will have set the DECL_CONTEXT for the friend
3217 declaration to N, rather than to the global namespace. */
3218 if (DECL_NAMESPACE_SCOPE_P (decl))
3219 DECL_CONTEXT (decl) = DECL_CONTEXT (tmpl);
3221 if (is_friend && !have_def)
3222 /* This is not really a declaration of a specialization.
3223 It's just the name of an instantiation. But, it's not
3224 a request for an instantiation, either. */
3225 SET_DECL_IMPLICIT_INSTANTIATION (decl);
3226 else if (TREE_CODE (decl) == FUNCTION_DECL)
3227 /* A specialization is not necessarily COMDAT. */
3228 DECL_COMDAT (decl) = (TREE_PUBLIC (decl)
3229 && DECL_DECLARED_INLINE_P (decl));
3230 else if (VAR_P (decl))
3231 DECL_COMDAT (decl) = false;
3233 /* If this is a full specialization, register it so that we can find
3234 it again. Partial specializations will be registered in
3235 process_partial_specialization. */
3236 if (!processing_template_decl)
3238 warn_spec_missing_attributes (gen_tmpl, decl, attrlist);
3240 decl = register_specialization (decl, gen_tmpl, targs,
3241 is_friend, 0);
3245 /* A 'structor should already have clones. */
3246 gcc_assert (decl == error_mark_node
3247 || variable_template_p (tmpl)
3248 || !(DECL_CONSTRUCTOR_P (decl)
3249 || DECL_DESTRUCTOR_P (decl))
3250 || DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
3254 return decl;
3257 /* Returns 1 iff PARMS1 and PARMS2 are identical sets of template
3258 parameters. These are represented in the same format used for
3259 DECL_TEMPLATE_PARMS. */
3262 comp_template_parms (const_tree parms1, const_tree parms2)
3264 const_tree p1;
3265 const_tree p2;
3267 if (parms1 == parms2)
3268 return 1;
3270 for (p1 = parms1, p2 = parms2;
3271 p1 != NULL_TREE && p2 != NULL_TREE;
3272 p1 = TREE_CHAIN (p1), p2 = TREE_CHAIN (p2))
3274 tree t1 = TREE_VALUE (p1);
3275 tree t2 = TREE_VALUE (p2);
3276 int i;
3278 gcc_assert (TREE_CODE (t1) == TREE_VEC);
3279 gcc_assert (TREE_CODE (t2) == TREE_VEC);
3281 if (TREE_VEC_LENGTH (t1) != TREE_VEC_LENGTH (t2))
3282 return 0;
3284 for (i = 0; i < TREE_VEC_LENGTH (t2); ++i)
3286 tree parm1 = TREE_VALUE (TREE_VEC_ELT (t1, i));
3287 tree parm2 = TREE_VALUE (TREE_VEC_ELT (t2, i));
3289 /* If either of the template parameters are invalid, assume
3290 they match for the sake of error recovery. */
3291 if (error_operand_p (parm1) || error_operand_p (parm2))
3292 return 1;
3294 if (TREE_CODE (parm1) != TREE_CODE (parm2))
3295 return 0;
3297 if (TREE_CODE (parm1) == TEMPLATE_TYPE_PARM
3298 && (TEMPLATE_TYPE_PARAMETER_PACK (parm1)
3299 == TEMPLATE_TYPE_PARAMETER_PACK (parm2)))
3300 continue;
3301 else if (!same_type_p (TREE_TYPE (parm1), TREE_TYPE (parm2)))
3302 return 0;
3306 if ((p1 != NULL_TREE) != (p2 != NULL_TREE))
3307 /* One set of parameters has more parameters lists than the
3308 other. */
3309 return 0;
3311 return 1;
3314 /* Determine whether PARM is a parameter pack. */
3316 bool
3317 template_parameter_pack_p (const_tree parm)
3319 /* Determine if we have a non-type template parameter pack. */
3320 if (TREE_CODE (parm) == PARM_DECL)
3321 return (DECL_TEMPLATE_PARM_P (parm)
3322 && TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)));
3323 if (TREE_CODE (parm) == TEMPLATE_PARM_INDEX)
3324 return TEMPLATE_PARM_PARAMETER_PACK (parm);
3326 /* If this is a list of template parameters, we could get a
3327 TYPE_DECL or a TEMPLATE_DECL. */
3328 if (TREE_CODE (parm) == TYPE_DECL || TREE_CODE (parm) == TEMPLATE_DECL)
3329 parm = TREE_TYPE (parm);
3331 /* Otherwise it must be a type template parameter. */
3332 return ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
3333 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
3334 && TEMPLATE_TYPE_PARAMETER_PACK (parm));
3337 /* Determine if T is a function parameter pack. */
3339 bool
3340 function_parameter_pack_p (const_tree t)
3342 if (t && TREE_CODE (t) == PARM_DECL)
3343 return DECL_PACK_P (t);
3344 return false;
3347 /* Return the function template declaration of PRIMARY_FUNC_TMPL_INST.
3348 PRIMARY_FUNC_TMPL_INST is a primary function template instantiation. */
3350 tree
3351 get_function_template_decl (const_tree primary_func_tmpl_inst)
3353 if (! primary_func_tmpl_inst
3354 || TREE_CODE (primary_func_tmpl_inst) != FUNCTION_DECL
3355 || ! primary_template_specialization_p (primary_func_tmpl_inst))
3356 return NULL;
3358 return DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (primary_func_tmpl_inst));
3361 /* Return true iff the function parameter PARAM_DECL was expanded
3362 from the function parameter pack PACK. */
3364 bool
3365 function_parameter_expanded_from_pack_p (tree param_decl, tree pack)
3367 if (DECL_ARTIFICIAL (param_decl)
3368 || !function_parameter_pack_p (pack))
3369 return false;
3371 /* The parameter pack and its pack arguments have the same
3372 DECL_PARM_INDEX. */
3373 return DECL_PARM_INDEX (pack) == DECL_PARM_INDEX (param_decl);
3376 /* Determine whether ARGS describes a variadic template args list,
3377 i.e., one that is terminated by a template argument pack. */
3379 static bool
3380 template_args_variadic_p (tree args)
3382 int nargs;
3383 tree last_parm;
3385 if (args == NULL_TREE)
3386 return false;
3388 args = INNERMOST_TEMPLATE_ARGS (args);
3389 nargs = TREE_VEC_LENGTH (args);
3391 if (nargs == 0)
3392 return false;
3394 last_parm = TREE_VEC_ELT (args, nargs - 1);
3396 return ARGUMENT_PACK_P (last_parm);
3399 /* Generate a new name for the parameter pack name NAME (an
3400 IDENTIFIER_NODE) that incorporates its */
3402 static tree
3403 make_ith_pack_parameter_name (tree name, int i)
3405 /* Munge the name to include the parameter index. */
3406 #define NUMBUF_LEN 128
3407 char numbuf[NUMBUF_LEN];
3408 char* newname;
3409 int newname_len;
3411 if (name == NULL_TREE)
3412 return name;
3413 snprintf (numbuf, NUMBUF_LEN, "%i", i);
3414 newname_len = IDENTIFIER_LENGTH (name)
3415 + strlen (numbuf) + 2;
3416 newname = (char*)alloca (newname_len);
3417 snprintf (newname, newname_len,
3418 "%s#%i", IDENTIFIER_POINTER (name), i);
3419 return get_identifier (newname);
3422 /* Return true if T is a primary function, class or alias template
3423 specialization, not including the template pattern. */
3425 bool
3426 primary_template_specialization_p (const_tree t)
3428 if (!t)
3429 return false;
3431 if (TREE_CODE (t) == FUNCTION_DECL || VAR_P (t))
3432 return (DECL_LANG_SPECIFIC (t)
3433 && DECL_USE_TEMPLATE (t)
3434 && DECL_TEMPLATE_INFO (t)
3435 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (t)));
3436 else if (CLASS_TYPE_P (t) && !TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
3437 return (CLASSTYPE_TEMPLATE_INFO (t)
3438 && CLASSTYPE_USE_TEMPLATE (t)
3439 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (t)));
3440 else if (alias_template_specialization_p (t))
3441 return true;
3442 return false;
3445 /* Return true if PARM is a template template parameter. */
3447 bool
3448 template_template_parameter_p (const_tree parm)
3450 return DECL_TEMPLATE_TEMPLATE_PARM_P (parm);
3453 /* Return true iff PARM is a DECL representing a type template
3454 parameter. */
3456 bool
3457 template_type_parameter_p (const_tree parm)
3459 return (parm
3460 && (TREE_CODE (parm) == TYPE_DECL
3461 || TREE_CODE (parm) == TEMPLATE_DECL)
3462 && DECL_TEMPLATE_PARM_P (parm));
3465 /* Return the template parameters of T if T is a
3466 primary template instantiation, NULL otherwise. */
3468 tree
3469 get_primary_template_innermost_parameters (const_tree t)
3471 tree parms = NULL, template_info = NULL;
3473 if ((template_info = get_template_info (t))
3474 && primary_template_specialization_p (t))
3475 parms = INNERMOST_TEMPLATE_PARMS
3476 (DECL_TEMPLATE_PARMS (TI_TEMPLATE (template_info)));
3478 return parms;
3481 /* Return the template parameters of the LEVELth level from the full list
3482 of template parameters PARMS. */
3484 tree
3485 get_template_parms_at_level (tree parms, int level)
3487 tree p;
3488 if (!parms
3489 || TREE_CODE (parms) != TREE_LIST
3490 || level > TMPL_PARMS_DEPTH (parms))
3491 return NULL_TREE;
3493 for (p = parms; p; p = TREE_CHAIN (p))
3494 if (TMPL_PARMS_DEPTH (p) == level)
3495 return p;
3497 return NULL_TREE;
3500 /* Returns the template arguments of T if T is a template instantiation,
3501 NULL otherwise. */
3503 tree
3504 get_template_innermost_arguments (const_tree t)
3506 tree args = NULL, template_info = NULL;
3508 if ((template_info = get_template_info (t))
3509 && TI_ARGS (template_info))
3510 args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (template_info));
3512 return args;
3515 /* Return the argument pack elements of T if T is a template argument pack,
3516 NULL otherwise. */
3518 tree
3519 get_template_argument_pack_elems (const_tree t)
3521 if (TREE_CODE (t) != TYPE_ARGUMENT_PACK
3522 && TREE_CODE (t) != NONTYPE_ARGUMENT_PACK)
3523 return NULL;
3525 return ARGUMENT_PACK_ARGS (t);
3528 /* In an ARGUMENT_PACK_SELECT, the actual underlying argument that the
3529 ARGUMENT_PACK_SELECT represents. */
3531 static tree
3532 argument_pack_select_arg (tree t)
3534 tree args = ARGUMENT_PACK_ARGS (ARGUMENT_PACK_SELECT_FROM_PACK (t));
3535 tree arg = TREE_VEC_ELT (args, ARGUMENT_PACK_SELECT_INDEX (t));
3537 /* If the selected argument is an expansion E, that most likely means we were
3538 called from gen_elem_of_pack_expansion_instantiation during the
3539 substituting of an argument pack (of which the Ith element is a pack
3540 expansion, where I is ARGUMENT_PACK_SELECT_INDEX) into a pack expansion.
3541 In this case, the Ith element resulting from this substituting is going to
3542 be a pack expansion, which pattern is the pattern of E. Let's return the
3543 pattern of E, and gen_elem_of_pack_expansion_instantiation will build the
3544 resulting pack expansion from it. */
3545 if (PACK_EXPANSION_P (arg))
3547 /* Make sure we aren't throwing away arg info. */
3548 gcc_assert (!PACK_EXPANSION_EXTRA_ARGS (arg));
3549 arg = PACK_EXPANSION_PATTERN (arg);
3552 return arg;
3556 /* True iff FN is a function representing a built-in variadic parameter
3557 pack. */
3559 bool
3560 builtin_pack_fn_p (tree fn)
3562 if (!fn
3563 || TREE_CODE (fn) != FUNCTION_DECL
3564 || !DECL_IS_BUILTIN (fn))
3565 return false;
3567 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3568 return true;
3570 return false;
3573 /* True iff CALL is a call to a function representing a built-in variadic
3574 parameter pack. */
3576 static bool
3577 builtin_pack_call_p (tree call)
3579 if (TREE_CODE (call) != CALL_EXPR)
3580 return false;
3581 return builtin_pack_fn_p (CALL_EXPR_FN (call));
3584 /* Return a TREE_VEC for the expansion of __integer_pack(HI). */
3586 static tree
3587 expand_integer_pack (tree call, tree args, tsubst_flags_t complain,
3588 tree in_decl)
3590 tree ohi = CALL_EXPR_ARG (call, 0);
3591 tree hi = tsubst_copy_and_build (ohi, args, complain, in_decl,
3592 false/*fn*/, true/*int_cst*/);
3594 if (value_dependent_expression_p (hi))
3596 if (hi != ohi)
3598 call = copy_node (call);
3599 CALL_EXPR_ARG (call, 0) = hi;
3601 tree ex = make_pack_expansion (call, complain);
3602 tree vec = make_tree_vec (1);
3603 TREE_VEC_ELT (vec, 0) = ex;
3604 return vec;
3606 else
3608 hi = cxx_constant_value (hi);
3609 int len = valid_constant_size_p (hi) ? tree_to_shwi (hi) : -1;
3611 /* Calculate the largest value of len that won't make the size of the vec
3612 overflow an int. The compiler will exceed resource limits long before
3613 this, but it seems a decent place to diagnose. */
3614 int max = ((INT_MAX - sizeof (tree_vec)) / sizeof (tree)) + 1;
3616 if (len < 0 || len > max)
3618 if ((complain & tf_error)
3619 && hi != error_mark_node)
3620 error ("argument to __integer_pack must be between 0 and %d", max);
3621 return error_mark_node;
3624 tree vec = make_tree_vec (len);
3626 for (int i = 0; i < len; ++i)
3627 TREE_VEC_ELT (vec, i) = size_int (i);
3629 return vec;
3633 /* Return a TREE_VEC for the expansion of built-in template parameter pack
3634 CALL. */
3636 static tree
3637 expand_builtin_pack_call (tree call, tree args, tsubst_flags_t complain,
3638 tree in_decl)
3640 if (!builtin_pack_call_p (call))
3641 return NULL_TREE;
3643 tree fn = CALL_EXPR_FN (call);
3645 if (id_equal (DECL_NAME (fn), "__integer_pack"))
3646 return expand_integer_pack (call, args, complain, in_decl);
3648 return NULL_TREE;
3651 /* Structure used to track the progress of find_parameter_packs_r. */
3652 struct find_parameter_pack_data
3654 /* TREE_LIST that will contain all of the parameter packs found by
3655 the traversal. */
3656 tree* parameter_packs;
3658 /* Set of AST nodes that have been visited by the traversal. */
3659 hash_set<tree> *visited;
3661 /* True iff we're making a type pack expansion. */
3662 bool type_pack_expansion_p;
3665 /* Identifies all of the argument packs that occur in a template
3666 argument and appends them to the TREE_LIST inside DATA, which is a
3667 find_parameter_pack_data structure. This is a subroutine of
3668 make_pack_expansion and uses_parameter_packs. */
3669 static tree
3670 find_parameter_packs_r (tree *tp, int *walk_subtrees, void* data)
3672 tree t = *tp;
3673 struct find_parameter_pack_data* ppd =
3674 (struct find_parameter_pack_data*)data;
3675 bool parameter_pack_p = false;
3677 /* Handle type aliases/typedefs. */
3678 if (TYPE_ALIAS_P (t))
3680 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
3681 cp_walk_tree (&TI_ARGS (tinfo),
3682 &find_parameter_packs_r,
3683 ppd, ppd->visited);
3684 *walk_subtrees = 0;
3685 return NULL_TREE;
3688 /* Identify whether this is a parameter pack or not. */
3689 switch (TREE_CODE (t))
3691 case TEMPLATE_PARM_INDEX:
3692 if (TEMPLATE_PARM_PARAMETER_PACK (t))
3693 parameter_pack_p = true;
3694 break;
3696 case TEMPLATE_TYPE_PARM:
3697 t = TYPE_MAIN_VARIANT (t);
3698 /* FALLTHRU */
3699 case TEMPLATE_TEMPLATE_PARM:
3700 /* If the placeholder appears in the decl-specifier-seq of a function
3701 parameter pack (14.6.3), or the type-specifier-seq of a type-id that
3702 is a pack expansion, the invented template parameter is a template
3703 parameter pack. */
3704 if (ppd->type_pack_expansion_p && is_auto (t))
3705 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
3706 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
3707 parameter_pack_p = true;
3708 break;
3710 case FIELD_DECL:
3711 case PARM_DECL:
3712 if (DECL_PACK_P (t))
3714 /* We don't want to walk into the type of a PARM_DECL,
3715 because we don't want to see the type parameter pack. */
3716 *walk_subtrees = 0;
3717 parameter_pack_p = true;
3719 break;
3721 case VAR_DECL:
3722 if (DECL_PACK_P (t))
3724 /* We don't want to walk into the type of a variadic capture proxy,
3725 because we don't want to see the type parameter pack. */
3726 *walk_subtrees = 0;
3727 parameter_pack_p = true;
3729 else if (variable_template_specialization_p (t))
3731 cp_walk_tree (&DECL_TI_ARGS (t),
3732 find_parameter_packs_r,
3733 ppd, ppd->visited);
3734 *walk_subtrees = 0;
3736 break;
3738 case CALL_EXPR:
3739 if (builtin_pack_call_p (t))
3740 parameter_pack_p = true;
3741 break;
3743 case BASES:
3744 parameter_pack_p = true;
3745 break;
3746 default:
3747 /* Not a parameter pack. */
3748 break;
3751 if (parameter_pack_p)
3753 /* Add this parameter pack to the list. */
3754 *ppd->parameter_packs = tree_cons (NULL_TREE, t, *ppd->parameter_packs);
3757 if (TYPE_P (t))
3758 cp_walk_tree (&TYPE_CONTEXT (t),
3759 &find_parameter_packs_r, ppd, ppd->visited);
3761 /* This switch statement will return immediately if we don't find a
3762 parameter pack. */
3763 switch (TREE_CODE (t))
3765 case TEMPLATE_PARM_INDEX:
3766 return NULL_TREE;
3768 case BOUND_TEMPLATE_TEMPLATE_PARM:
3769 /* Check the template itself. */
3770 cp_walk_tree (&TREE_TYPE (TYPE_TI_TEMPLATE (t)),
3771 &find_parameter_packs_r, ppd, ppd->visited);
3772 /* Check the template arguments. */
3773 cp_walk_tree (&TYPE_TI_ARGS (t), &find_parameter_packs_r, ppd,
3774 ppd->visited);
3775 *walk_subtrees = 0;
3776 return NULL_TREE;
3778 case TEMPLATE_TYPE_PARM:
3779 case TEMPLATE_TEMPLATE_PARM:
3780 return NULL_TREE;
3782 case PARM_DECL:
3783 return NULL_TREE;
3785 case DECL_EXPR:
3786 /* Ignore the declaration of a capture proxy for a parameter pack. */
3787 if (is_capture_proxy (DECL_EXPR_DECL (t)))
3788 *walk_subtrees = 0;
3789 return NULL_TREE;
3791 case RECORD_TYPE:
3792 if (TYPE_PTRMEMFUNC_P (t))
3793 return NULL_TREE;
3794 /* Fall through. */
3796 case UNION_TYPE:
3797 case ENUMERAL_TYPE:
3798 if (TYPE_TEMPLATE_INFO (t))
3799 cp_walk_tree (&TYPE_TI_ARGS (t),
3800 &find_parameter_packs_r, ppd, ppd->visited);
3802 *walk_subtrees = 0;
3803 return NULL_TREE;
3805 case TEMPLATE_DECL:
3806 if (!DECL_TEMPLATE_TEMPLATE_PARM_P (t))
3807 return NULL_TREE;
3808 gcc_fallthrough();
3810 case CONSTRUCTOR:
3811 cp_walk_tree (&TREE_TYPE (t),
3812 &find_parameter_packs_r, ppd, ppd->visited);
3813 return NULL_TREE;
3815 case TYPENAME_TYPE:
3816 cp_walk_tree (&TYPENAME_TYPE_FULLNAME (t), &find_parameter_packs_r,
3817 ppd, ppd->visited);
3818 *walk_subtrees = 0;
3819 return NULL_TREE;
3821 case TYPE_PACK_EXPANSION:
3822 case EXPR_PACK_EXPANSION:
3823 *walk_subtrees = 0;
3824 return NULL_TREE;
3826 case INTEGER_TYPE:
3827 cp_walk_tree (&TYPE_MAX_VALUE (t), &find_parameter_packs_r,
3828 ppd, ppd->visited);
3829 *walk_subtrees = 0;
3830 return NULL_TREE;
3832 case IDENTIFIER_NODE:
3833 cp_walk_tree (&TREE_TYPE (t), &find_parameter_packs_r, ppd,
3834 ppd->visited);
3835 *walk_subtrees = 0;
3836 return NULL_TREE;
3838 case LAMBDA_EXPR:
3840 /* Look at explicit captures. */
3841 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t);
3842 cap; cap = TREE_CHAIN (cap))
3843 cp_walk_tree (&TREE_VALUE (cap), &find_parameter_packs_r, ppd,
3844 ppd->visited);
3845 /* Since we defer implicit capture, look in the body as well. */
3846 tree fn = lambda_function (t);
3847 cp_walk_tree (&DECL_SAVED_TREE (fn), &find_parameter_packs_r, ppd,
3848 ppd->visited);
3849 *walk_subtrees = 0;
3850 return NULL_TREE;
3853 case DECLTYPE_TYPE:
3855 /* When traversing a DECLTYPE_TYPE_EXPR, we need to set
3856 type_pack_expansion_p to false so that any placeholders
3857 within the expression don't get marked as parameter packs. */
3858 bool type_pack_expansion_p = ppd->type_pack_expansion_p;
3859 ppd->type_pack_expansion_p = false;
3860 cp_walk_tree (&DECLTYPE_TYPE_EXPR (t), &find_parameter_packs_r,
3861 ppd, ppd->visited);
3862 ppd->type_pack_expansion_p = type_pack_expansion_p;
3863 *walk_subtrees = 0;
3864 return NULL_TREE;
3867 default:
3868 return NULL_TREE;
3871 return NULL_TREE;
3874 /* Determines if the expression or type T uses any parameter packs. */
3875 bool
3876 uses_parameter_packs (tree t)
3878 tree parameter_packs = NULL_TREE;
3879 struct find_parameter_pack_data ppd;
3880 ppd.parameter_packs = &parameter_packs;
3881 ppd.visited = new hash_set<tree>;
3882 ppd.type_pack_expansion_p = false;
3883 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
3884 delete ppd.visited;
3885 return parameter_packs != NULL_TREE;
3888 /* Turn ARG, which may be an expression, type, or a TREE_LIST
3889 representation a base-class initializer into a parameter pack
3890 expansion. If all goes well, the resulting node will be an
3891 EXPR_PACK_EXPANSION, TYPE_PACK_EXPANSION, or TREE_LIST,
3892 respectively. */
3893 tree
3894 make_pack_expansion (tree arg, tsubst_flags_t complain)
3896 tree result;
3897 tree parameter_packs = NULL_TREE;
3898 bool for_types = false;
3899 struct find_parameter_pack_data ppd;
3901 if (!arg || arg == error_mark_node)
3902 return arg;
3904 if (TREE_CODE (arg) == TREE_LIST && TREE_PURPOSE (arg))
3906 /* A TREE_LIST with a non-null TREE_PURPOSE is for a base
3907 class initializer. In this case, the TREE_PURPOSE will be a
3908 _TYPE node (representing the base class expansion we're
3909 initializing) and the TREE_VALUE will be a TREE_LIST
3910 containing the initialization arguments.
3912 The resulting expansion looks somewhat different from most
3913 expansions. Rather than returning just one _EXPANSION, we
3914 return a TREE_LIST whose TREE_PURPOSE is a
3915 TYPE_PACK_EXPANSION containing the bases that will be
3916 initialized. The TREE_VALUE will be identical to the
3917 original TREE_VALUE, which is a list of arguments that will
3918 be passed to each base. We do not introduce any new pack
3919 expansion nodes into the TREE_VALUE (although it is possible
3920 that some already exist), because the TREE_PURPOSE and
3921 TREE_VALUE all need to be expanded together with the same
3922 _EXPANSION node. Note that the TYPE_PACK_EXPANSION in the
3923 resulting TREE_PURPOSE will mention the parameter packs in
3924 both the bases and the arguments to the bases. */
3925 tree purpose;
3926 tree value;
3927 tree parameter_packs = NULL_TREE;
3929 /* Determine which parameter packs will be used by the base
3930 class expansion. */
3931 ppd.visited = new hash_set<tree>;
3932 ppd.parameter_packs = &parameter_packs;
3933 ppd.type_pack_expansion_p = true;
3934 gcc_assert (TYPE_P (TREE_PURPOSE (arg)));
3935 cp_walk_tree (&TREE_PURPOSE (arg), &find_parameter_packs_r,
3936 &ppd, ppd.visited);
3938 if (parameter_packs == NULL_TREE)
3940 if (complain & tf_error)
3941 error ("base initializer expansion %qT contains no parameter packs",
3942 arg);
3943 delete ppd.visited;
3944 return error_mark_node;
3947 if (TREE_VALUE (arg) != void_type_node)
3949 /* Collect the sets of parameter packs used in each of the
3950 initialization arguments. */
3951 for (value = TREE_VALUE (arg); value; value = TREE_CHAIN (value))
3953 /* Determine which parameter packs will be expanded in this
3954 argument. */
3955 cp_walk_tree (&TREE_VALUE (value), &find_parameter_packs_r,
3956 &ppd, ppd.visited);
3960 delete ppd.visited;
3962 /* Create the pack expansion type for the base type. */
3963 purpose = cxx_make_type (TYPE_PACK_EXPANSION);
3964 SET_PACK_EXPANSION_PATTERN (purpose, TREE_PURPOSE (arg));
3965 PACK_EXPANSION_PARAMETER_PACKS (purpose) = parameter_packs;
3966 PACK_EXPANSION_LOCAL_P (purpose) = at_function_scope_p ();
3968 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3969 they will rarely be compared to anything. */
3970 SET_TYPE_STRUCTURAL_EQUALITY (purpose);
3972 return tree_cons (purpose, TREE_VALUE (arg), NULL_TREE);
3975 if (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL)
3976 for_types = true;
3978 /* Build the PACK_EXPANSION_* node. */
3979 result = for_types
3980 ? cxx_make_type (TYPE_PACK_EXPANSION)
3981 : make_node (EXPR_PACK_EXPANSION);
3982 SET_PACK_EXPANSION_PATTERN (result, arg);
3983 if (TREE_CODE (result) == EXPR_PACK_EXPANSION)
3985 /* Propagate type and const-expression information. */
3986 TREE_TYPE (result) = TREE_TYPE (arg);
3987 TREE_CONSTANT (result) = TREE_CONSTANT (arg);
3988 /* Mark this read now, since the expansion might be length 0. */
3989 mark_exp_read (arg);
3991 else
3992 /* Just use structural equality for these TYPE_PACK_EXPANSIONS;
3993 they will rarely be compared to anything. */
3994 SET_TYPE_STRUCTURAL_EQUALITY (result);
3996 /* Determine which parameter packs will be expanded. */
3997 ppd.parameter_packs = &parameter_packs;
3998 ppd.visited = new hash_set<tree>;
3999 ppd.type_pack_expansion_p = TYPE_P (arg);
4000 cp_walk_tree (&arg, &find_parameter_packs_r, &ppd, ppd.visited);
4001 delete ppd.visited;
4003 /* Make sure we found some parameter packs. */
4004 if (parameter_packs == NULL_TREE)
4006 if (complain & tf_error)
4008 if (TYPE_P (arg))
4009 error ("expansion pattern %qT contains no argument packs", arg);
4010 else
4011 error ("expansion pattern %qE contains no argument packs", arg);
4013 return error_mark_node;
4015 PACK_EXPANSION_PARAMETER_PACKS (result) = parameter_packs;
4017 PACK_EXPANSION_LOCAL_P (result) = at_function_scope_p ();
4019 return result;
4022 /* Checks T for any "bare" parameter packs, which have not yet been
4023 expanded, and issues an error if any are found. This operation can
4024 only be done on full expressions or types (e.g., an expression
4025 statement, "if" condition, etc.), because we could have expressions like:
4027 foo(f(g(h(args)))...)
4029 where "args" is a parameter pack. check_for_bare_parameter_packs
4030 should not be called for the subexpressions args, h(args),
4031 g(h(args)), or f(g(h(args))), because we would produce erroneous
4032 error messages.
4034 Returns TRUE and emits an error if there were bare parameter packs,
4035 returns FALSE otherwise. */
4036 bool
4037 check_for_bare_parameter_packs (tree t)
4039 tree parameter_packs = NULL_TREE;
4040 struct find_parameter_pack_data ppd;
4042 if (!processing_template_decl || !t || t == error_mark_node)
4043 return false;
4045 /* A lambda might use a parameter pack from the containing context. */
4046 if (current_class_type && LAMBDA_TYPE_P (current_class_type)
4047 && CLASSTYPE_TEMPLATE_INFO (current_class_type))
4048 return false;
4050 if (TREE_CODE (t) == TYPE_DECL)
4051 t = TREE_TYPE (t);
4053 ppd.parameter_packs = &parameter_packs;
4054 ppd.visited = new hash_set<tree>;
4055 ppd.type_pack_expansion_p = false;
4056 cp_walk_tree (&t, &find_parameter_packs_r, &ppd, ppd.visited);
4057 delete ppd.visited;
4059 if (parameter_packs)
4061 location_t loc = EXPR_LOC_OR_LOC (t, input_location);
4062 error_at (loc, "parameter packs not expanded with %<...%>:");
4063 while (parameter_packs)
4065 tree pack = TREE_VALUE (parameter_packs);
4066 tree name = NULL_TREE;
4068 if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM
4069 || TREE_CODE (pack) == TEMPLATE_TEMPLATE_PARM)
4070 name = TYPE_NAME (pack);
4071 else if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
4072 name = DECL_NAME (TEMPLATE_PARM_DECL (pack));
4073 else if (TREE_CODE (pack) == CALL_EXPR)
4074 name = DECL_NAME (CALL_EXPR_FN (pack));
4075 else
4076 name = DECL_NAME (pack);
4078 if (name)
4079 inform (loc, " %qD", name);
4080 else
4081 inform (loc, " <anonymous>");
4083 parameter_packs = TREE_CHAIN (parameter_packs);
4086 return true;
4089 return false;
4092 /* Expand any parameter packs that occur in the template arguments in
4093 ARGS. */
4094 tree
4095 expand_template_argument_pack (tree args)
4097 if (args == error_mark_node)
4098 return error_mark_node;
4100 tree result_args = NULL_TREE;
4101 int in_arg, out_arg = 0, nargs = args ? TREE_VEC_LENGTH (args) : 0;
4102 int num_result_args = -1;
4103 int non_default_args_count = -1;
4105 /* First, determine if we need to expand anything, and the number of
4106 slots we'll need. */
4107 for (in_arg = 0; in_arg < nargs; ++in_arg)
4109 tree arg = TREE_VEC_ELT (args, in_arg);
4110 if (arg == NULL_TREE)
4111 return args;
4112 if (ARGUMENT_PACK_P (arg))
4114 int num_packed = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg));
4115 if (num_result_args < 0)
4116 num_result_args = in_arg + num_packed;
4117 else
4118 num_result_args += num_packed;
4120 else
4122 if (num_result_args >= 0)
4123 num_result_args++;
4127 /* If no expansion is necessary, we're done. */
4128 if (num_result_args < 0)
4129 return args;
4131 /* Expand arguments. */
4132 result_args = make_tree_vec (num_result_args);
4133 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (args))
4134 non_default_args_count =
4135 GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (args);
4136 for (in_arg = 0; in_arg < nargs; ++in_arg)
4138 tree arg = TREE_VEC_ELT (args, in_arg);
4139 if (ARGUMENT_PACK_P (arg))
4141 tree packed = ARGUMENT_PACK_ARGS (arg);
4142 int i, num_packed = TREE_VEC_LENGTH (packed);
4143 for (i = 0; i < num_packed; ++i, ++out_arg)
4144 TREE_VEC_ELT (result_args, out_arg) = TREE_VEC_ELT(packed, i);
4145 if (non_default_args_count > 0)
4146 non_default_args_count += num_packed - 1;
4148 else
4150 TREE_VEC_ELT (result_args, out_arg) = arg;
4151 ++out_arg;
4154 if (non_default_args_count >= 0)
4155 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (result_args, non_default_args_count);
4156 return result_args;
4159 /* Checks if DECL shadows a template parameter.
4161 [temp.local]: A template-parameter shall not be redeclared within its
4162 scope (including nested scopes).
4164 Emits an error and returns TRUE if the DECL shadows a parameter,
4165 returns FALSE otherwise. */
4167 bool
4168 check_template_shadow (tree decl)
4170 tree olddecl;
4172 /* If we're not in a template, we can't possibly shadow a template
4173 parameter. */
4174 if (!current_template_parms)
4175 return true;
4177 /* Figure out what we're shadowing. */
4178 decl = OVL_FIRST (decl);
4179 olddecl = innermost_non_namespace_value (DECL_NAME (decl));
4181 /* If there's no previous binding for this name, we're not shadowing
4182 anything, let alone a template parameter. */
4183 if (!olddecl)
4184 return true;
4186 /* If we're not shadowing a template parameter, we're done. Note
4187 that OLDDECL might be an OVERLOAD (or perhaps even an
4188 ERROR_MARK), so we can't just blithely assume it to be a _DECL
4189 node. */
4190 if (!DECL_P (olddecl) || !DECL_TEMPLATE_PARM_P (olddecl))
4191 return true;
4193 /* We check for decl != olddecl to avoid bogus errors for using a
4194 name inside a class. We check TPFI to avoid duplicate errors for
4195 inline member templates. */
4196 if (decl == olddecl
4197 || (DECL_TEMPLATE_PARM_P (decl)
4198 && TEMPLATE_PARMS_FOR_INLINE (current_template_parms)))
4199 return true;
4201 /* Don't complain about the injected class name, as we've already
4202 complained about the class itself. */
4203 if (DECL_SELF_REFERENCE_P (decl))
4204 return false;
4206 if (DECL_TEMPLATE_PARM_P (decl))
4207 error ("declaration of template parameter %q+D shadows "
4208 "template parameter", decl);
4209 else
4210 error ("declaration of %q+#D shadows template parameter", decl);
4211 inform (DECL_SOURCE_LOCATION (olddecl),
4212 "template parameter %qD declared here", olddecl);
4213 return false;
4216 /* Return a new TEMPLATE_PARM_INDEX with the indicated INDEX, LEVEL,
4217 ORIG_LEVEL, DECL, and TYPE. */
4219 static tree
4220 build_template_parm_index (int index,
4221 int level,
4222 int orig_level,
4223 tree decl,
4224 tree type)
4226 tree t = make_node (TEMPLATE_PARM_INDEX);
4227 TEMPLATE_PARM_IDX (t) = index;
4228 TEMPLATE_PARM_LEVEL (t) = level;
4229 TEMPLATE_PARM_ORIG_LEVEL (t) = orig_level;
4230 TEMPLATE_PARM_DECL (t) = decl;
4231 TREE_TYPE (t) = type;
4232 TREE_CONSTANT (t) = TREE_CONSTANT (decl);
4233 TREE_READONLY (t) = TREE_READONLY (decl);
4235 return t;
4238 /* Find the canonical type parameter for the given template type
4239 parameter. Returns the canonical type parameter, which may be TYPE
4240 if no such parameter existed. */
4242 static tree
4243 canonical_type_parameter (tree type)
4245 tree list;
4246 int idx = TEMPLATE_TYPE_IDX (type);
4247 if (!canonical_template_parms)
4248 vec_alloc (canonical_template_parms, idx + 1);
4250 if (canonical_template_parms->length () <= (unsigned) idx)
4251 vec_safe_grow_cleared (canonical_template_parms, idx + 1);
4253 list = (*canonical_template_parms)[idx];
4254 while (list && !comptypes (type, TREE_VALUE (list), COMPARE_STRUCTURAL))
4255 list = TREE_CHAIN (list);
4257 if (list)
4258 return TREE_VALUE (list);
4259 else
4261 (*canonical_template_parms)[idx]
4262 = tree_cons (NULL_TREE, type, (*canonical_template_parms)[idx]);
4263 return type;
4267 /* Return a TEMPLATE_PARM_INDEX, similar to INDEX, but whose
4268 TEMPLATE_PARM_LEVEL has been decreased by LEVELS. If such a
4269 TEMPLATE_PARM_INDEX already exists, it is returned; otherwise, a
4270 new one is created. */
4272 static tree
4273 reduce_template_parm_level (tree index, tree type, int levels, tree args,
4274 tsubst_flags_t complain)
4276 if (TEMPLATE_PARM_DESCENDANTS (index) == NULL_TREE
4277 || (TEMPLATE_PARM_LEVEL (TEMPLATE_PARM_DESCENDANTS (index))
4278 != TEMPLATE_PARM_LEVEL (index) - levels)
4279 || !same_type_p (type, TREE_TYPE (TEMPLATE_PARM_DESCENDANTS (index))))
4281 tree orig_decl = TEMPLATE_PARM_DECL (index);
4282 tree decl, t;
4284 decl = build_decl (DECL_SOURCE_LOCATION (orig_decl),
4285 TREE_CODE (orig_decl), DECL_NAME (orig_decl), type);
4286 TREE_CONSTANT (decl) = TREE_CONSTANT (orig_decl);
4287 TREE_READONLY (decl) = TREE_READONLY (orig_decl);
4288 DECL_ARTIFICIAL (decl) = 1;
4289 SET_DECL_TEMPLATE_PARM_P (decl);
4291 t = build_template_parm_index (TEMPLATE_PARM_IDX (index),
4292 TEMPLATE_PARM_LEVEL (index) - levels,
4293 TEMPLATE_PARM_ORIG_LEVEL (index),
4294 decl, type);
4295 TEMPLATE_PARM_DESCENDANTS (index) = t;
4296 TEMPLATE_PARM_PARAMETER_PACK (t)
4297 = TEMPLATE_PARM_PARAMETER_PACK (index);
4299 /* Template template parameters need this. */
4300 if (TREE_CODE (decl) == TEMPLATE_DECL)
4302 DECL_TEMPLATE_RESULT (decl)
4303 = build_decl (DECL_SOURCE_LOCATION (decl),
4304 TYPE_DECL, DECL_NAME (decl), type);
4305 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (decl)) = true;
4306 DECL_TEMPLATE_PARMS (decl) = tsubst_template_parms
4307 (DECL_TEMPLATE_PARMS (orig_decl), args, complain);
4311 return TEMPLATE_PARM_DESCENDANTS (index);
4314 /* Process information from new template parameter PARM and append it
4315 to the LIST being built. This new parameter is a non-type
4316 parameter iff IS_NON_TYPE is true. This new parameter is a
4317 parameter pack iff IS_PARAMETER_PACK is true. The location of PARM
4318 is in PARM_LOC. */
4320 tree
4321 process_template_parm (tree list, location_t parm_loc, tree parm,
4322 bool is_non_type, bool is_parameter_pack)
4324 tree decl = 0;
4325 int idx = 0;
4327 gcc_assert (TREE_CODE (parm) == TREE_LIST);
4328 tree defval = TREE_PURPOSE (parm);
4329 tree constr = TREE_TYPE (parm);
4331 if (list)
4333 tree p = tree_last (list);
4335 if (p && TREE_VALUE (p) != error_mark_node)
4337 p = TREE_VALUE (p);
4338 if (TREE_CODE (p) == TYPE_DECL || TREE_CODE (p) == TEMPLATE_DECL)
4339 idx = TEMPLATE_TYPE_IDX (TREE_TYPE (p));
4340 else
4341 idx = TEMPLATE_PARM_IDX (DECL_INITIAL (p));
4344 ++idx;
4347 if (is_non_type)
4349 parm = TREE_VALUE (parm);
4351 SET_DECL_TEMPLATE_PARM_P (parm);
4353 if (TREE_TYPE (parm) != error_mark_node)
4355 /* [temp.param]
4357 The top-level cv-qualifiers on the template-parameter are
4358 ignored when determining its type. */
4359 TREE_TYPE (parm) = TYPE_MAIN_VARIANT (TREE_TYPE (parm));
4360 if (invalid_nontype_parm_type_p (TREE_TYPE (parm), 1))
4361 TREE_TYPE (parm) = error_mark_node;
4362 else if (uses_parameter_packs (TREE_TYPE (parm))
4363 && !is_parameter_pack
4364 /* If we're in a nested template parameter list, the template
4365 template parameter could be a parameter pack. */
4366 && processing_template_parmlist == 1)
4368 /* This template parameter is not a parameter pack, but it
4369 should be. Complain about "bare" parameter packs. */
4370 check_for_bare_parameter_packs (TREE_TYPE (parm));
4372 /* Recover by calling this a parameter pack. */
4373 is_parameter_pack = true;
4377 /* A template parameter is not modifiable. */
4378 TREE_CONSTANT (parm) = 1;
4379 TREE_READONLY (parm) = 1;
4380 decl = build_decl (parm_loc,
4381 CONST_DECL, DECL_NAME (parm), TREE_TYPE (parm));
4382 TREE_CONSTANT (decl) = 1;
4383 TREE_READONLY (decl) = 1;
4384 DECL_INITIAL (parm) = DECL_INITIAL (decl)
4385 = build_template_parm_index (idx, processing_template_decl,
4386 processing_template_decl,
4387 decl, TREE_TYPE (parm));
4389 TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm))
4390 = is_parameter_pack;
4392 else
4394 tree t;
4395 parm = TREE_VALUE (TREE_VALUE (parm));
4397 if (parm && TREE_CODE (parm) == TEMPLATE_DECL)
4399 t = cxx_make_type (TEMPLATE_TEMPLATE_PARM);
4400 /* This is for distinguishing between real templates and template
4401 template parameters */
4402 TREE_TYPE (parm) = t;
4403 TREE_TYPE (DECL_TEMPLATE_RESULT (parm)) = t;
4404 decl = parm;
4406 else
4408 t = cxx_make_type (TEMPLATE_TYPE_PARM);
4409 /* parm is either IDENTIFIER_NODE or NULL_TREE. */
4410 decl = build_decl (parm_loc,
4411 TYPE_DECL, parm, t);
4414 TYPE_NAME (t) = decl;
4415 TYPE_STUB_DECL (t) = decl;
4416 parm = decl;
4417 TEMPLATE_TYPE_PARM_INDEX (t)
4418 = build_template_parm_index (idx, processing_template_decl,
4419 processing_template_decl,
4420 decl, TREE_TYPE (parm));
4421 TEMPLATE_TYPE_PARAMETER_PACK (t) = is_parameter_pack;
4422 TYPE_CANONICAL (t) = canonical_type_parameter (t);
4424 DECL_ARTIFICIAL (decl) = 1;
4425 SET_DECL_TEMPLATE_PARM_P (decl);
4427 /* Build requirements for the type/template parameter.
4428 This must be done after SET_DECL_TEMPLATE_PARM_P or
4429 process_template_parm could fail. */
4430 tree reqs = finish_shorthand_constraint (parm, constr);
4432 pushdecl (decl);
4434 if (defval && TREE_CODE (defval) == OVERLOAD)
4435 lookup_keep (defval, true);
4437 /* Build the parameter node linking the parameter declaration,
4438 its default argument (if any), and its constraints (if any). */
4439 parm = build_tree_list (defval, parm);
4440 TEMPLATE_PARM_CONSTRAINTS (parm) = reqs;
4442 return chainon (list, parm);
4445 /* The end of a template parameter list has been reached. Process the
4446 tree list into a parameter vector, converting each parameter into a more
4447 useful form. Type parameters are saved as IDENTIFIER_NODEs, and others
4448 as PARM_DECLs. */
4450 tree
4451 end_template_parm_list (tree parms)
4453 int nparms;
4454 tree parm, next;
4455 tree saved_parmlist = make_tree_vec (list_length (parms));
4457 /* Pop the dummy parameter level and add the real one. */
4458 current_template_parms = TREE_CHAIN (current_template_parms);
4460 current_template_parms
4461 = tree_cons (size_int (processing_template_decl),
4462 saved_parmlist, current_template_parms);
4464 for (parm = parms, nparms = 0; parm; parm = next, nparms++)
4466 next = TREE_CHAIN (parm);
4467 TREE_VEC_ELT (saved_parmlist, nparms) = parm;
4468 TREE_CHAIN (parm) = NULL_TREE;
4471 --processing_template_parmlist;
4473 return saved_parmlist;
4476 // Explicitly indicate the end of the template parameter list. We assume
4477 // that the current template parameters have been constructed and/or
4478 // managed explicitly, as when creating new template template parameters
4479 // from a shorthand constraint.
4480 void
4481 end_template_parm_list ()
4483 --processing_template_parmlist;
4486 /* end_template_decl is called after a template declaration is seen. */
4488 void
4489 end_template_decl (void)
4491 reset_specialization ();
4493 if (! processing_template_decl)
4494 return;
4496 /* This matches the pushlevel in begin_template_parm_list. */
4497 finish_scope ();
4499 --processing_template_decl;
4500 current_template_parms = TREE_CHAIN (current_template_parms);
4503 /* Takes a TREE_LIST representing a template parameter and convert it
4504 into an argument suitable to be passed to the type substitution
4505 functions. Note that If the TREE_LIST contains an error_mark
4506 node, the returned argument is error_mark_node. */
4508 tree
4509 template_parm_to_arg (tree t)
4512 if (t == NULL_TREE
4513 || TREE_CODE (t) != TREE_LIST)
4514 return t;
4516 if (error_operand_p (TREE_VALUE (t)))
4517 return error_mark_node;
4519 t = TREE_VALUE (t);
4521 if (TREE_CODE (t) == TYPE_DECL
4522 || TREE_CODE (t) == TEMPLATE_DECL)
4524 t = TREE_TYPE (t);
4526 if (TEMPLATE_TYPE_PARAMETER_PACK (t))
4528 /* Turn this argument into a TYPE_ARGUMENT_PACK
4529 with a single element, which expands T. */
4530 tree vec = make_tree_vec (1);
4531 if (CHECKING_P)
4532 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4534 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4536 t = cxx_make_type (TYPE_ARGUMENT_PACK);
4537 SET_ARGUMENT_PACK_ARGS (t, vec);
4540 else
4542 t = DECL_INITIAL (t);
4544 if (TEMPLATE_PARM_PARAMETER_PACK (t))
4546 /* Turn this argument into a NONTYPE_ARGUMENT_PACK
4547 with a single element, which expands T. */
4548 tree vec = make_tree_vec (1);
4549 if (CHECKING_P)
4550 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
4552 t = convert_from_reference (t);
4553 TREE_VEC_ELT (vec, 0) = make_pack_expansion (t);
4555 t = make_node (NONTYPE_ARGUMENT_PACK);
4556 SET_ARGUMENT_PACK_ARGS (t, vec);
4558 else
4559 t = convert_from_reference (t);
4561 return t;
4564 /* Given a single level of template parameters (a TREE_VEC), return it
4565 as a set of template arguments. */
4567 static tree
4568 template_parms_level_to_args (tree parms)
4570 tree a = copy_node (parms);
4571 TREE_TYPE (a) = NULL_TREE;
4572 for (int i = TREE_VEC_LENGTH (a) - 1; i >= 0; --i)
4573 TREE_VEC_ELT (a, i) = template_parm_to_arg (TREE_VEC_ELT (a, i));
4575 if (CHECKING_P)
4576 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (a, TREE_VEC_LENGTH (a));
4578 return a;
4581 /* Given a set of template parameters, return them as a set of template
4582 arguments. The template parameters are represented as a TREE_VEC, in
4583 the form documented in cp-tree.h for template arguments. */
4585 static tree
4586 template_parms_to_args (tree parms)
4588 tree header;
4589 tree args = NULL_TREE;
4590 int length = TMPL_PARMS_DEPTH (parms);
4591 int l = length;
4593 /* If there is only one level of template parameters, we do not
4594 create a TREE_VEC of TREE_VECs. Instead, we return a single
4595 TREE_VEC containing the arguments. */
4596 if (length > 1)
4597 args = make_tree_vec (length);
4599 for (header = parms; header; header = TREE_CHAIN (header))
4601 tree a = template_parms_level_to_args (TREE_VALUE (header));
4603 if (length > 1)
4604 TREE_VEC_ELT (args, --l) = a;
4605 else
4606 args = a;
4609 return args;
4612 /* Within the declaration of a template, return the currently active
4613 template parameters as an argument TREE_VEC. */
4615 static tree
4616 current_template_args (void)
4618 return template_parms_to_args (current_template_parms);
4621 /* Update the declared TYPE by doing any lookups which were thought to be
4622 dependent, but are not now that we know the SCOPE of the declarator. */
4624 tree
4625 maybe_update_decl_type (tree orig_type, tree scope)
4627 tree type = orig_type;
4629 if (type == NULL_TREE)
4630 return type;
4632 if (TREE_CODE (orig_type) == TYPE_DECL)
4633 type = TREE_TYPE (type);
4635 if (scope && TYPE_P (scope) && dependent_type_p (scope)
4636 && dependent_type_p (type)
4637 /* Don't bother building up the args in this case. */
4638 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
4640 /* tsubst in the args corresponding to the template parameters,
4641 including auto if present. Most things will be unchanged, but
4642 make_typename_type and tsubst_qualified_id will resolve
4643 TYPENAME_TYPEs and SCOPE_REFs that were previously dependent. */
4644 tree args = current_template_args ();
4645 tree auto_node = type_uses_auto (type);
4646 tree pushed;
4647 if (auto_node)
4649 tree auto_vec = make_tree_vec (1);
4650 TREE_VEC_ELT (auto_vec, 0) = auto_node;
4651 args = add_to_template_args (args, auto_vec);
4653 pushed = push_scope (scope);
4654 type = tsubst (type, args, tf_warning_or_error, NULL_TREE);
4655 if (pushed)
4656 pop_scope (scope);
4659 if (type == error_mark_node)
4660 return orig_type;
4662 if (TREE_CODE (orig_type) == TYPE_DECL)
4664 if (same_type_p (type, TREE_TYPE (orig_type)))
4665 type = orig_type;
4666 else
4667 type = TYPE_NAME (type);
4669 return type;
4672 /* Return a TEMPLATE_DECL corresponding to DECL, using the indicated
4673 template PARMS and constraints, CONSTR. If MEMBER_TEMPLATE_P is true,
4674 the new template is a member template. */
4676 tree
4677 build_template_decl (tree decl, tree parms, bool member_template_p)
4679 tree tmpl = build_lang_decl (TEMPLATE_DECL, DECL_NAME (decl), NULL_TREE);
4680 SET_DECL_LANGUAGE (tmpl, DECL_LANGUAGE (decl));
4681 DECL_TEMPLATE_PARMS (tmpl) = parms;
4682 DECL_CONTEXT (tmpl) = DECL_CONTEXT (decl);
4683 DECL_SOURCE_LOCATION (tmpl) = DECL_SOURCE_LOCATION (decl);
4684 DECL_MEMBER_TEMPLATE_P (tmpl) = member_template_p;
4686 return tmpl;
4689 struct template_parm_data
4691 /* The level of the template parameters we are currently
4692 processing. */
4693 int level;
4695 /* The index of the specialization argument we are currently
4696 processing. */
4697 int current_arg;
4699 /* An array whose size is the number of template parameters. The
4700 elements are nonzero if the parameter has been used in any one
4701 of the arguments processed so far. */
4702 int* parms;
4704 /* An array whose size is the number of template arguments. The
4705 elements are nonzero if the argument makes use of template
4706 parameters of this level. */
4707 int* arg_uses_template_parms;
4710 /* Subroutine of push_template_decl used to see if each template
4711 parameter in a partial specialization is used in the explicit
4712 argument list. If T is of the LEVEL given in DATA (which is
4713 treated as a template_parm_data*), then DATA->PARMS is marked
4714 appropriately. */
4716 static int
4717 mark_template_parm (tree t, void* data)
4719 int level;
4720 int idx;
4721 struct template_parm_data* tpd = (struct template_parm_data*) data;
4723 template_parm_level_and_index (t, &level, &idx);
4725 if (level == tpd->level)
4727 tpd->parms[idx] = 1;
4728 tpd->arg_uses_template_parms[tpd->current_arg] = 1;
4731 /* In C++17 the type of a non-type argument is a deduced context. */
4732 if (cxx_dialect >= cxx17
4733 && TREE_CODE (t) == TEMPLATE_PARM_INDEX)
4734 for_each_template_parm (TREE_TYPE (t),
4735 &mark_template_parm,
4736 data,
4737 NULL,
4738 /*include_nondeduced_p=*/false);
4740 /* Return zero so that for_each_template_parm will continue the
4741 traversal of the tree; we want to mark *every* template parm. */
4742 return 0;
4745 /* Process the partial specialization DECL. */
4747 static tree
4748 process_partial_specialization (tree decl)
4750 tree type = TREE_TYPE (decl);
4751 tree tinfo = get_template_info (decl);
4752 tree maintmpl = TI_TEMPLATE (tinfo);
4753 tree specargs = TI_ARGS (tinfo);
4754 tree inner_args = INNERMOST_TEMPLATE_ARGS (specargs);
4755 tree main_inner_parms = DECL_INNERMOST_TEMPLATE_PARMS (maintmpl);
4756 tree inner_parms;
4757 tree inst;
4758 int nargs = TREE_VEC_LENGTH (inner_args);
4759 int ntparms;
4760 int i;
4761 bool did_error_intro = false;
4762 struct template_parm_data tpd;
4763 struct template_parm_data tpd2;
4765 gcc_assert (current_template_parms);
4767 /* A concept cannot be specialized. */
4768 if (flag_concepts && variable_concept_p (maintmpl))
4770 error ("specialization of variable concept %q#D", maintmpl);
4771 return error_mark_node;
4774 inner_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
4775 ntparms = TREE_VEC_LENGTH (inner_parms);
4777 /* We check that each of the template parameters given in the
4778 partial specialization is used in the argument list to the
4779 specialization. For example:
4781 template <class T> struct S;
4782 template <class T> struct S<T*>;
4784 The second declaration is OK because `T*' uses the template
4785 parameter T, whereas
4787 template <class T> struct S<int>;
4789 is no good. Even trickier is:
4791 template <class T>
4792 struct S1
4794 template <class U>
4795 struct S2;
4796 template <class U>
4797 struct S2<T>;
4800 The S2<T> declaration is actually invalid; it is a
4801 full-specialization. Of course,
4803 template <class U>
4804 struct S2<T (*)(U)>;
4806 or some such would have been OK. */
4807 tpd.level = TMPL_PARMS_DEPTH (current_template_parms);
4808 tpd.parms = XALLOCAVEC (int, ntparms);
4809 memset (tpd.parms, 0, sizeof (int) * ntparms);
4811 tpd.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4812 memset (tpd.arg_uses_template_parms, 0, sizeof (int) * nargs);
4813 for (i = 0; i < nargs; ++i)
4815 tpd.current_arg = i;
4816 for_each_template_parm (TREE_VEC_ELT (inner_args, i),
4817 &mark_template_parm,
4818 &tpd,
4819 NULL,
4820 /*include_nondeduced_p=*/false);
4822 for (i = 0; i < ntparms; ++i)
4823 if (tpd.parms[i] == 0)
4825 /* One of the template parms was not used in a deduced context in the
4826 specialization. */
4827 if (!did_error_intro)
4829 error ("template parameters not deducible in "
4830 "partial specialization:");
4831 did_error_intro = true;
4834 inform (input_location, " %qD",
4835 TREE_VALUE (TREE_VEC_ELT (inner_parms, i)));
4838 if (did_error_intro)
4839 return error_mark_node;
4841 /* [temp.class.spec]
4843 The argument list of the specialization shall not be identical to
4844 the implicit argument list of the primary template. */
4845 tree main_args
4846 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (maintmpl)));
4847 if (comp_template_args (inner_args, INNERMOST_TEMPLATE_ARGS (main_args))
4848 && (!flag_concepts
4849 || !strictly_subsumes (current_template_constraints (),
4850 get_constraints (maintmpl))))
4852 if (!flag_concepts)
4853 error ("partial specialization %q+D does not specialize "
4854 "any template arguments; to define the primary template, "
4855 "remove the template argument list", decl);
4856 else
4857 error ("partial specialization %q+D does not specialize any "
4858 "template arguments and is not more constrained than "
4859 "the primary template; to define the primary template, "
4860 "remove the template argument list", decl);
4861 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4864 /* A partial specialization that replaces multiple parameters of the
4865 primary template with a pack expansion is less specialized for those
4866 parameters. */
4867 if (nargs < DECL_NTPARMS (maintmpl))
4869 error ("partial specialization is not more specialized than the "
4870 "primary template because it replaces multiple parameters "
4871 "with a pack expansion");
4872 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template here");
4873 /* Avoid crash in process_partial_specialization. */
4874 return decl;
4877 /* If we aren't in a dependent class, we can actually try deduction. */
4878 else if (tpd.level == 1
4879 /* FIXME we should be able to handle a partial specialization of a
4880 partial instantiation, but currently we can't (c++/41727). */
4881 && TMPL_ARGS_DEPTH (specargs) == 1
4882 && !get_partial_spec_bindings (maintmpl, maintmpl, specargs))
4884 if (permerror (input_location, "partial specialization %qD is not "
4885 "more specialized than", decl))
4886 inform (DECL_SOURCE_LOCATION (maintmpl), "primary template %qD",
4887 maintmpl);
4890 /* [temp.class.spec]
4892 A partially specialized non-type argument expression shall not
4893 involve template parameters of the partial specialization except
4894 when the argument expression is a simple identifier.
4896 The type of a template parameter corresponding to a specialized
4897 non-type argument shall not be dependent on a parameter of the
4898 specialization.
4900 Also, we verify that pack expansions only occur at the
4901 end of the argument list. */
4902 gcc_assert (nargs == DECL_NTPARMS (maintmpl));
4903 tpd2.parms = 0;
4904 for (i = 0; i < nargs; ++i)
4906 tree parm = TREE_VALUE (TREE_VEC_ELT (main_inner_parms, i));
4907 tree arg = TREE_VEC_ELT (inner_args, i);
4908 tree packed_args = NULL_TREE;
4909 int j, len = 1;
4911 if (ARGUMENT_PACK_P (arg))
4913 /* Extract the arguments from the argument pack. We'll be
4914 iterating over these in the following loop. */
4915 packed_args = ARGUMENT_PACK_ARGS (arg);
4916 len = TREE_VEC_LENGTH (packed_args);
4919 for (j = 0; j < len; j++)
4921 if (packed_args)
4922 /* Get the Jth argument in the parameter pack. */
4923 arg = TREE_VEC_ELT (packed_args, j);
4925 if (PACK_EXPANSION_P (arg))
4927 /* Pack expansions must come at the end of the
4928 argument list. */
4929 if ((packed_args && j < len - 1)
4930 || (!packed_args && i < nargs - 1))
4932 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4933 error ("parameter pack argument %qE must be at the "
4934 "end of the template argument list", arg);
4935 else
4936 error ("parameter pack argument %qT must be at the "
4937 "end of the template argument list", arg);
4941 if (TREE_CODE (arg) == EXPR_PACK_EXPANSION)
4942 /* We only care about the pattern. */
4943 arg = PACK_EXPANSION_PATTERN (arg);
4945 if (/* These first two lines are the `non-type' bit. */
4946 !TYPE_P (arg)
4947 && TREE_CODE (arg) != TEMPLATE_DECL
4948 /* This next two lines are the `argument expression is not just a
4949 simple identifier' condition and also the `specialized
4950 non-type argument' bit. */
4951 && TREE_CODE (arg) != TEMPLATE_PARM_INDEX
4952 && !(REFERENCE_REF_P (arg)
4953 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_PARM_INDEX))
4955 if ((!packed_args && tpd.arg_uses_template_parms[i])
4956 || (packed_args && uses_template_parms (arg)))
4957 error ("template argument %qE involves template parameter(s)",
4958 arg);
4959 else
4961 /* Look at the corresponding template parameter,
4962 marking which template parameters its type depends
4963 upon. */
4964 tree type = TREE_TYPE (parm);
4966 if (!tpd2.parms)
4968 /* We haven't yet initialized TPD2. Do so now. */
4969 tpd2.arg_uses_template_parms = XALLOCAVEC (int, nargs);
4970 /* The number of parameters here is the number in the
4971 main template, which, as checked in the assertion
4972 above, is NARGS. */
4973 tpd2.parms = XALLOCAVEC (int, nargs);
4974 tpd2.level =
4975 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (maintmpl));
4978 /* Mark the template parameters. But this time, we're
4979 looking for the template parameters of the main
4980 template, not in the specialization. */
4981 tpd2.current_arg = i;
4982 tpd2.arg_uses_template_parms[i] = 0;
4983 memset (tpd2.parms, 0, sizeof (int) * nargs);
4984 for_each_template_parm (type,
4985 &mark_template_parm,
4986 &tpd2,
4987 NULL,
4988 /*include_nondeduced_p=*/false);
4990 if (tpd2.arg_uses_template_parms [i])
4992 /* The type depended on some template parameters.
4993 If they are fully specialized in the
4994 specialization, that's OK. */
4995 int j;
4996 int count = 0;
4997 for (j = 0; j < nargs; ++j)
4998 if (tpd2.parms[j] != 0
4999 && tpd.arg_uses_template_parms [j])
5000 ++count;
5001 if (count != 0)
5002 error_n (input_location, count,
5003 "type %qT of template argument %qE depends "
5004 "on a template parameter",
5005 "type %qT of template argument %qE depends "
5006 "on template parameters",
5007 type,
5008 arg);
5015 /* We should only get here once. */
5016 if (TREE_CODE (decl) == TYPE_DECL)
5017 gcc_assert (!COMPLETE_TYPE_P (type));
5019 // Build the template decl.
5020 tree tmpl = build_template_decl (decl, current_template_parms,
5021 DECL_MEMBER_TEMPLATE_P (maintmpl));
5022 TREE_TYPE (tmpl) = type;
5023 DECL_TEMPLATE_RESULT (tmpl) = decl;
5024 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5025 DECL_TEMPLATE_INFO (tmpl) = build_template_info (maintmpl, specargs);
5026 DECL_PRIMARY_TEMPLATE (tmpl) = maintmpl;
5028 /* Give template template parms a DECL_CONTEXT of the template
5029 for which they are a parameter. */
5030 for (i = 0; i < ntparms; ++i)
5032 tree parm = TREE_VALUE (TREE_VEC_ELT (inner_parms, i));
5033 if (TREE_CODE (parm) == TEMPLATE_DECL)
5034 DECL_CONTEXT (parm) = tmpl;
5037 if (VAR_P (decl))
5038 /* We didn't register this in check_explicit_specialization so we could
5039 wait until the constraints were set. */
5040 decl = register_specialization (decl, maintmpl, specargs, false, 0);
5041 else
5042 associate_classtype_constraints (type);
5044 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)
5045 = tree_cons (specargs, tmpl,
5046 DECL_TEMPLATE_SPECIALIZATIONS (maintmpl));
5047 TREE_TYPE (DECL_TEMPLATE_SPECIALIZATIONS (maintmpl)) = type;
5049 for (inst = DECL_TEMPLATE_INSTANTIATIONS (maintmpl); inst;
5050 inst = TREE_CHAIN (inst))
5052 tree instance = TREE_VALUE (inst);
5053 if (TYPE_P (instance)
5054 ? (COMPLETE_TYPE_P (instance)
5055 && CLASSTYPE_IMPLICIT_INSTANTIATION (instance))
5056 : DECL_TEMPLATE_INSTANTIATION (instance))
5058 tree spec = most_specialized_partial_spec (instance, tf_none);
5059 tree inst_decl = (DECL_P (instance)
5060 ? instance : TYPE_NAME (instance));
5061 if (!spec)
5062 /* OK */;
5063 else if (spec == error_mark_node)
5064 permerror (input_location,
5065 "declaration of %qD ambiguates earlier template "
5066 "instantiation for %qD", decl, inst_decl);
5067 else if (TREE_VALUE (spec) == tmpl)
5068 permerror (input_location,
5069 "partial specialization of %qD after instantiation "
5070 "of %qD", decl, inst_decl);
5074 return decl;
5077 /* PARM is a template parameter of some form; return the corresponding
5078 TEMPLATE_PARM_INDEX. */
5080 static tree
5081 get_template_parm_index (tree parm)
5083 if (TREE_CODE (parm) == PARM_DECL
5084 || TREE_CODE (parm) == CONST_DECL)
5085 parm = DECL_INITIAL (parm);
5086 else if (TREE_CODE (parm) == TYPE_DECL
5087 || TREE_CODE (parm) == TEMPLATE_DECL)
5088 parm = TREE_TYPE (parm);
5089 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
5090 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM
5091 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM)
5092 parm = TEMPLATE_TYPE_PARM_INDEX (parm);
5093 gcc_assert (TREE_CODE (parm) == TEMPLATE_PARM_INDEX);
5094 return parm;
5097 /* Subroutine of fixed_parameter_pack_p below. Look for any template
5098 parameter packs used by the template parameter PARM. */
5100 static void
5101 fixed_parameter_pack_p_1 (tree parm, struct find_parameter_pack_data *ppd)
5103 /* A type parm can't refer to another parm. */
5104 if (TREE_CODE (parm) == TYPE_DECL)
5105 return;
5106 else if (TREE_CODE (parm) == PARM_DECL)
5108 cp_walk_tree (&TREE_TYPE (parm), &find_parameter_packs_r,
5109 ppd, ppd->visited);
5110 return;
5113 gcc_assert (TREE_CODE (parm) == TEMPLATE_DECL);
5115 tree vec = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (parm));
5116 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
5117 fixed_parameter_pack_p_1 (TREE_VALUE (TREE_VEC_ELT (vec, i)), ppd);
5120 /* PARM is a template parameter pack. Return any parameter packs used in
5121 its type or the type of any of its template parameters. If there are
5122 any such packs, it will be instantiated into a fixed template parameter
5123 list by partial instantiation rather than be fully deduced. */
5125 tree
5126 fixed_parameter_pack_p (tree parm)
5128 /* This can only be true in a member template. */
5129 if (TEMPLATE_PARM_ORIG_LEVEL (get_template_parm_index (parm)) < 2)
5130 return NULL_TREE;
5131 /* This can only be true for a parameter pack. */
5132 if (!template_parameter_pack_p (parm))
5133 return NULL_TREE;
5134 /* A type parm can't refer to another parm. */
5135 if (TREE_CODE (parm) == TYPE_DECL)
5136 return NULL_TREE;
5138 tree parameter_packs = NULL_TREE;
5139 struct find_parameter_pack_data ppd;
5140 ppd.parameter_packs = &parameter_packs;
5141 ppd.visited = new hash_set<tree>;
5142 ppd.type_pack_expansion_p = false;
5144 fixed_parameter_pack_p_1 (parm, &ppd);
5146 delete ppd.visited;
5147 return parameter_packs;
5150 /* Check that a template declaration's use of default arguments and
5151 parameter packs is not invalid. Here, PARMS are the template
5152 parameters. IS_PRIMARY is true if DECL is the thing declared by
5153 a primary template. IS_PARTIAL is true if DECL is a partial
5154 specialization.
5156 IS_FRIEND_DECL is nonzero if DECL is either a non-defining friend
5157 function template declaration or a friend class template
5158 declaration. In the function case, 1 indicates a declaration, 2
5159 indicates a redeclaration. When IS_FRIEND_DECL=2, no errors are
5160 emitted for extraneous default arguments.
5162 Returns TRUE if there were no errors found, FALSE otherwise. */
5164 bool
5165 check_default_tmpl_args (tree decl, tree parms, bool is_primary,
5166 bool is_partial, int is_friend_decl)
5168 const char *msg;
5169 int last_level_to_check;
5170 tree parm_level;
5171 bool no_errors = true;
5173 /* [temp.param]
5175 A default template-argument shall not be specified in a
5176 function template declaration or a function template definition, nor
5177 in the template-parameter-list of the definition of a member of a
5178 class template. */
5180 if (TREE_CODE (CP_DECL_CONTEXT (decl)) == FUNCTION_DECL
5181 || (TREE_CODE (decl) == FUNCTION_DECL && DECL_LOCAL_FUNCTION_P (decl)))
5182 /* You can't have a function template declaration in a local
5183 scope, nor you can you define a member of a class template in a
5184 local scope. */
5185 return true;
5187 if ((TREE_CODE (decl) == TYPE_DECL
5188 && TREE_TYPE (decl)
5189 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5190 || (TREE_CODE (decl) == FUNCTION_DECL
5191 && LAMBDA_FUNCTION_P (decl)))
5192 /* A lambda doesn't have an explicit declaration; don't complain
5193 about the parms of the enclosing class. */
5194 return true;
5196 if (current_class_type
5197 && !TYPE_BEING_DEFINED (current_class_type)
5198 && DECL_LANG_SPECIFIC (decl)
5199 && DECL_DECLARES_FUNCTION_P (decl)
5200 /* If this is either a friend defined in the scope of the class
5201 or a member function. */
5202 && (DECL_FUNCTION_MEMBER_P (decl)
5203 ? same_type_p (DECL_CONTEXT (decl), current_class_type)
5204 : DECL_FRIEND_CONTEXT (decl)
5205 ? same_type_p (DECL_FRIEND_CONTEXT (decl), current_class_type)
5206 : false)
5207 /* And, if it was a member function, it really was defined in
5208 the scope of the class. */
5209 && (!DECL_FUNCTION_MEMBER_P (decl)
5210 || DECL_INITIALIZED_IN_CLASS_P (decl)))
5211 /* We already checked these parameters when the template was
5212 declared, so there's no need to do it again now. This function
5213 was defined in class scope, but we're processing its body now
5214 that the class is complete. */
5215 return true;
5217 /* Core issue 226 (C++0x only): the following only applies to class
5218 templates. */
5219 if (is_primary
5220 && ((cxx_dialect == cxx98) || TREE_CODE (decl) != FUNCTION_DECL))
5222 /* [temp.param]
5224 If a template-parameter has a default template-argument, all
5225 subsequent template-parameters shall have a default
5226 template-argument supplied. */
5227 for (parm_level = parms; parm_level; parm_level = TREE_CHAIN (parm_level))
5229 tree inner_parms = TREE_VALUE (parm_level);
5230 int ntparms = TREE_VEC_LENGTH (inner_parms);
5231 int seen_def_arg_p = 0;
5232 int i;
5234 for (i = 0; i < ntparms; ++i)
5236 tree parm = TREE_VEC_ELT (inner_parms, i);
5238 if (parm == error_mark_node)
5239 continue;
5241 if (TREE_PURPOSE (parm))
5242 seen_def_arg_p = 1;
5243 else if (seen_def_arg_p
5244 && !template_parameter_pack_p (TREE_VALUE (parm)))
5246 error ("no default argument for %qD", TREE_VALUE (parm));
5247 /* For better subsequent error-recovery, we indicate that
5248 there should have been a default argument. */
5249 TREE_PURPOSE (parm) = error_mark_node;
5250 no_errors = false;
5252 else if (!is_partial
5253 && !is_friend_decl
5254 /* Don't complain about an enclosing partial
5255 specialization. */
5256 && parm_level == parms
5257 && TREE_CODE (decl) == TYPE_DECL
5258 && i < ntparms - 1
5259 && template_parameter_pack_p (TREE_VALUE (parm))
5260 /* A fixed parameter pack will be partially
5261 instantiated into a fixed length list. */
5262 && !fixed_parameter_pack_p (TREE_VALUE (parm)))
5264 /* A primary class template can only have one
5265 parameter pack, at the end of the template
5266 parameter list. */
5268 error ("parameter pack %q+D must be at the end of the"
5269 " template parameter list", TREE_VALUE (parm));
5271 TREE_VALUE (TREE_VEC_ELT (inner_parms, i))
5272 = error_mark_node;
5273 no_errors = false;
5279 if (((cxx_dialect == cxx98) && TREE_CODE (decl) != TYPE_DECL)
5280 || is_partial
5281 || !is_primary
5282 || is_friend_decl)
5283 /* For an ordinary class template, default template arguments are
5284 allowed at the innermost level, e.g.:
5285 template <class T = int>
5286 struct S {};
5287 but, in a partial specialization, they're not allowed even
5288 there, as we have in [temp.class.spec]:
5290 The template parameter list of a specialization shall not
5291 contain default template argument values.
5293 So, for a partial specialization, or for a function template
5294 (in C++98/C++03), we look at all of them. */
5296 else
5297 /* But, for a primary class template that is not a partial
5298 specialization we look at all template parameters except the
5299 innermost ones. */
5300 parms = TREE_CHAIN (parms);
5302 /* Figure out what error message to issue. */
5303 if (is_friend_decl == 2)
5304 msg = G_("default template arguments may not be used in function template "
5305 "friend re-declaration");
5306 else if (is_friend_decl)
5307 msg = G_("default template arguments may not be used in template "
5308 "friend declarations");
5309 else if (TREE_CODE (decl) == FUNCTION_DECL && (cxx_dialect == cxx98))
5310 msg = G_("default template arguments may not be used in function templates "
5311 "without -std=c++11 or -std=gnu++11");
5312 else if (is_partial)
5313 msg = G_("default template arguments may not be used in "
5314 "partial specializations");
5315 else if (current_class_type && CLASSTYPE_IS_TEMPLATE (current_class_type))
5316 msg = G_("default argument for template parameter for class enclosing %qD");
5317 else
5318 /* Per [temp.param]/9, "A default template-argument shall not be
5319 specified in the template-parameter-lists of the definition of
5320 a member of a class template that appears outside of the member's
5321 class.", thus if we aren't handling a member of a class template
5322 there is no need to examine the parameters. */
5323 return true;
5325 if (current_class_type && TYPE_BEING_DEFINED (current_class_type))
5326 /* If we're inside a class definition, there's no need to
5327 examine the parameters to the class itself. On the one
5328 hand, they will be checked when the class is defined, and,
5329 on the other, default arguments are valid in things like:
5330 template <class T = double>
5331 struct S { template <class U> void f(U); };
5332 Here the default argument for `S' has no bearing on the
5333 declaration of `f'. */
5334 last_level_to_check = template_class_depth (current_class_type) + 1;
5335 else
5336 /* Check everything. */
5337 last_level_to_check = 0;
5339 for (parm_level = parms;
5340 parm_level && TMPL_PARMS_DEPTH (parm_level) >= last_level_to_check;
5341 parm_level = TREE_CHAIN (parm_level))
5343 tree inner_parms = TREE_VALUE (parm_level);
5344 int i;
5345 int ntparms;
5347 ntparms = TREE_VEC_LENGTH (inner_parms);
5348 for (i = 0; i < ntparms; ++i)
5350 if (TREE_VEC_ELT (inner_parms, i) == error_mark_node)
5351 continue;
5353 if (TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)))
5355 if (msg)
5357 no_errors = false;
5358 if (is_friend_decl == 2)
5359 return no_errors;
5361 error (msg, decl);
5362 msg = 0;
5365 /* Clear out the default argument so that we are not
5366 confused later. */
5367 TREE_PURPOSE (TREE_VEC_ELT (inner_parms, i)) = NULL_TREE;
5371 /* At this point, if we're still interested in issuing messages,
5372 they must apply to classes surrounding the object declared. */
5373 if (msg)
5374 msg = G_("default argument for template parameter for class "
5375 "enclosing %qD");
5378 return no_errors;
5381 /* Worker for push_template_decl_real, called via
5382 for_each_template_parm. DATA is really an int, indicating the
5383 level of the parameters we are interested in. If T is a template
5384 parameter of that level, return nonzero. */
5386 static int
5387 template_parm_this_level_p (tree t, void* data)
5389 int this_level = *(int *)data;
5390 int level;
5392 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5393 level = TEMPLATE_PARM_LEVEL (t);
5394 else
5395 level = TEMPLATE_TYPE_LEVEL (t);
5396 return level == this_level;
5399 /* Worker for uses_outer_template_parms, called via for_each_template_parm.
5400 DATA is really an int, indicating the innermost outer level of parameters.
5401 If T is a template parameter of that level or further out, return
5402 nonzero. */
5404 static int
5405 template_parm_outer_level (tree t, void *data)
5407 int this_level = *(int *)data;
5408 int level;
5410 if (TREE_CODE (t) == TEMPLATE_PARM_INDEX)
5411 level = TEMPLATE_PARM_LEVEL (t);
5412 else
5413 level = TEMPLATE_TYPE_LEVEL (t);
5414 return level <= this_level;
5417 /* Creates a TEMPLATE_DECL for the indicated DECL using the template
5418 parameters given by current_template_args, or reuses a
5419 previously existing one, if appropriate. Returns the DECL, or an
5420 equivalent one, if it is replaced via a call to duplicate_decls.
5422 If IS_FRIEND is true, DECL is a friend declaration. */
5424 tree
5425 push_template_decl_real (tree decl, bool is_friend)
5427 tree tmpl;
5428 tree args;
5429 tree info;
5430 tree ctx;
5431 bool is_primary;
5432 bool is_partial;
5433 int new_template_p = 0;
5434 /* True if the template is a member template, in the sense of
5435 [temp.mem]. */
5436 bool member_template_p = false;
5438 if (decl == error_mark_node || !current_template_parms)
5439 return error_mark_node;
5441 /* See if this is a partial specialization. */
5442 is_partial = ((DECL_IMPLICIT_TYPEDEF_P (decl)
5443 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
5444 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
5445 || (VAR_P (decl)
5446 && DECL_LANG_SPECIFIC (decl)
5447 && DECL_TEMPLATE_SPECIALIZATION (decl)
5448 && TINFO_USED_TEMPLATE_ID (DECL_TEMPLATE_INFO (decl))));
5450 if (TREE_CODE (decl) == FUNCTION_DECL && DECL_FRIEND_P (decl))
5451 is_friend = true;
5453 if (is_friend)
5454 /* For a friend, we want the context of the friend, not
5455 the type of which it is a friend. */
5456 ctx = CP_DECL_CONTEXT (decl);
5457 else if (CP_DECL_CONTEXT (decl)
5458 && TREE_CODE (CP_DECL_CONTEXT (decl)) != NAMESPACE_DECL)
5459 /* In the case of a virtual function, we want the class in which
5460 it is defined. */
5461 ctx = CP_DECL_CONTEXT (decl);
5462 else
5463 /* Otherwise, if we're currently defining some class, the DECL
5464 is assumed to be a member of the class. */
5465 ctx = current_scope ();
5467 if (ctx && TREE_CODE (ctx) == NAMESPACE_DECL)
5468 ctx = NULL_TREE;
5470 if (!DECL_CONTEXT (decl))
5471 DECL_CONTEXT (decl) = FROB_CONTEXT (current_namespace);
5473 /* See if this is a primary template. */
5474 if (is_friend && ctx
5475 && uses_template_parms_level (ctx, processing_template_decl))
5476 /* A friend template that specifies a class context, i.e.
5477 template <typename T> friend void A<T>::f();
5478 is not primary. */
5479 is_primary = false;
5480 else if (TREE_CODE (decl) == TYPE_DECL
5481 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5482 is_primary = false;
5483 else
5484 is_primary = template_parm_scope_p ();
5486 if (is_primary)
5488 warning (OPT_Wtemplates, "template %qD declared", decl);
5490 if (DECL_CLASS_SCOPE_P (decl))
5491 member_template_p = true;
5492 if (TREE_CODE (decl) == TYPE_DECL
5493 && anon_aggrname_p (DECL_NAME (decl)))
5495 error ("template class without a name");
5496 return error_mark_node;
5498 else if (TREE_CODE (decl) == FUNCTION_DECL)
5500 if (member_template_p)
5502 if (DECL_OVERRIDE_P (decl) || DECL_FINAL_P (decl))
5503 error ("member template %qD may not have virt-specifiers", decl);
5505 if (DECL_DESTRUCTOR_P (decl))
5507 /* [temp.mem]
5509 A destructor shall not be a member template. */
5510 error ("destructor %qD declared as member template", decl);
5511 return error_mark_node;
5513 if (IDENTIFIER_NEWDEL_OP_P (DECL_NAME (decl))
5514 && (!prototype_p (TREE_TYPE (decl))
5515 || TYPE_ARG_TYPES (TREE_TYPE (decl)) == void_list_node
5516 || !TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5517 || (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (decl)))
5518 == void_list_node)))
5520 /* [basic.stc.dynamic.allocation]
5522 An allocation function can be a function
5523 template. ... Template allocation functions shall
5524 have two or more parameters. */
5525 error ("invalid template declaration of %qD", decl);
5526 return error_mark_node;
5529 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5530 && CLASS_TYPE_P (TREE_TYPE (decl)))
5532 /* Class template, set TEMPLATE_TYPE_PARM_FOR_CLASS. */
5533 tree parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
5534 for (int i = 0; i < TREE_VEC_LENGTH (parms); ++i)
5536 tree t = TREE_VALUE (TREE_VEC_ELT (parms, i));
5537 if (TREE_CODE (t) == TYPE_DECL)
5538 t = TREE_TYPE (t);
5539 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
5540 TEMPLATE_TYPE_PARM_FOR_CLASS (t) = true;
5543 else if (TREE_CODE (decl) == TYPE_DECL
5544 && TYPE_DECL_ALIAS_P (decl))
5545 /* alias-declaration */
5546 gcc_assert (!DECL_ARTIFICIAL (decl));
5547 else if (VAR_P (decl))
5548 /* C++14 variable template. */;
5549 else
5551 error ("template declaration of %q#D", decl);
5552 return error_mark_node;
5556 /* Check to see that the rules regarding the use of default
5557 arguments are not being violated. We check args for a friend
5558 functions when we know whether it's a definition, introducing
5559 declaration or re-declaration. */
5560 if (!is_friend || TREE_CODE (decl) != FUNCTION_DECL)
5561 check_default_tmpl_args (decl, current_template_parms,
5562 is_primary, is_partial, is_friend);
5564 /* Ensure that there are no parameter packs in the type of this
5565 declaration that have not been expanded. */
5566 if (TREE_CODE (decl) == FUNCTION_DECL)
5568 /* Check each of the arguments individually to see if there are
5569 any bare parameter packs. */
5570 tree type = TREE_TYPE (decl);
5571 tree arg = DECL_ARGUMENTS (decl);
5572 tree argtype = TYPE_ARG_TYPES (type);
5574 while (arg && argtype)
5576 if (!DECL_PACK_P (arg)
5577 && check_for_bare_parameter_packs (TREE_TYPE (arg)))
5579 /* This is a PARM_DECL that contains unexpanded parameter
5580 packs. We have already complained about this in the
5581 check_for_bare_parameter_packs call, so just replace
5582 these types with ERROR_MARK_NODE. */
5583 TREE_TYPE (arg) = error_mark_node;
5584 TREE_VALUE (argtype) = error_mark_node;
5587 arg = DECL_CHAIN (arg);
5588 argtype = TREE_CHAIN (argtype);
5591 /* Check for bare parameter packs in the return type and the
5592 exception specifiers. */
5593 if (check_for_bare_parameter_packs (TREE_TYPE (type)))
5594 /* Errors were already issued, set return type to int
5595 as the frontend doesn't expect error_mark_node as
5596 the return type. */
5597 TREE_TYPE (type) = integer_type_node;
5598 if (check_for_bare_parameter_packs (TYPE_RAISES_EXCEPTIONS (type)))
5599 TYPE_RAISES_EXCEPTIONS (type) = NULL_TREE;
5601 else if (check_for_bare_parameter_packs ((TREE_CODE (decl) == TYPE_DECL
5602 && TYPE_DECL_ALIAS_P (decl))
5603 ? DECL_ORIGINAL_TYPE (decl)
5604 : TREE_TYPE (decl)))
5606 TREE_TYPE (decl) = error_mark_node;
5607 return error_mark_node;
5610 if (is_partial)
5611 return process_partial_specialization (decl);
5613 args = current_template_args ();
5615 if (!ctx
5616 || TREE_CODE (ctx) == FUNCTION_DECL
5617 || (CLASS_TYPE_P (ctx) && TYPE_BEING_DEFINED (ctx))
5618 || (TREE_CODE (decl) == TYPE_DECL
5619 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
5620 || (is_friend && !DECL_TEMPLATE_INFO (decl)))
5622 if (DECL_LANG_SPECIFIC (decl)
5623 && DECL_TEMPLATE_INFO (decl)
5624 && DECL_TI_TEMPLATE (decl))
5625 tmpl = DECL_TI_TEMPLATE (decl);
5626 /* If DECL is a TYPE_DECL for a class-template, then there won't
5627 be DECL_LANG_SPECIFIC. The information equivalent to
5628 DECL_TEMPLATE_INFO is found in TYPE_TEMPLATE_INFO instead. */
5629 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
5630 && TYPE_TEMPLATE_INFO (TREE_TYPE (decl))
5631 && TYPE_TI_TEMPLATE (TREE_TYPE (decl)))
5633 /* Since a template declaration already existed for this
5634 class-type, we must be redeclaring it here. Make sure
5635 that the redeclaration is valid. */
5636 redeclare_class_template (TREE_TYPE (decl),
5637 current_template_parms,
5638 current_template_constraints ());
5639 /* We don't need to create a new TEMPLATE_DECL; just use the
5640 one we already had. */
5641 tmpl = TYPE_TI_TEMPLATE (TREE_TYPE (decl));
5643 else
5645 tmpl = build_template_decl (decl, current_template_parms,
5646 member_template_p);
5647 new_template_p = 1;
5649 if (DECL_LANG_SPECIFIC (decl)
5650 && DECL_TEMPLATE_SPECIALIZATION (decl))
5652 /* A specialization of a member template of a template
5653 class. */
5654 SET_DECL_TEMPLATE_SPECIALIZATION (tmpl);
5655 DECL_TEMPLATE_INFO (tmpl) = DECL_TEMPLATE_INFO (decl);
5656 DECL_TEMPLATE_INFO (decl) = NULL_TREE;
5660 else
5662 tree a, t, current, parms;
5663 int i;
5664 tree tinfo = get_template_info (decl);
5666 if (!tinfo)
5668 error ("template definition of non-template %q#D", decl);
5669 return error_mark_node;
5672 tmpl = TI_TEMPLATE (tinfo);
5674 if (DECL_FUNCTION_TEMPLATE_P (tmpl)
5675 && DECL_TEMPLATE_INFO (decl) && DECL_TI_ARGS (decl)
5676 && DECL_TEMPLATE_SPECIALIZATION (decl)
5677 && DECL_MEMBER_TEMPLATE_P (tmpl))
5679 tree new_tmpl;
5681 /* The declaration is a specialization of a member
5682 template, declared outside the class. Therefore, the
5683 innermost template arguments will be NULL, so we
5684 replace them with the arguments determined by the
5685 earlier call to check_explicit_specialization. */
5686 args = DECL_TI_ARGS (decl);
5688 new_tmpl
5689 = build_template_decl (decl, current_template_parms,
5690 member_template_p);
5691 DECL_TEMPLATE_RESULT (new_tmpl) = decl;
5692 TREE_TYPE (new_tmpl) = TREE_TYPE (decl);
5693 DECL_TI_TEMPLATE (decl) = new_tmpl;
5694 SET_DECL_TEMPLATE_SPECIALIZATION (new_tmpl);
5695 DECL_TEMPLATE_INFO (new_tmpl)
5696 = build_template_info (tmpl, args);
5698 register_specialization (new_tmpl,
5699 most_general_template (tmpl),
5700 args,
5701 is_friend, 0);
5702 return decl;
5705 /* Make sure the template headers we got make sense. */
5707 parms = DECL_TEMPLATE_PARMS (tmpl);
5708 i = TMPL_PARMS_DEPTH (parms);
5709 if (TMPL_ARGS_DEPTH (args) != i)
5711 error ("expected %d levels of template parms for %q#D, got %d",
5712 i, decl, TMPL_ARGS_DEPTH (args));
5713 DECL_INTERFACE_KNOWN (decl) = 1;
5714 return error_mark_node;
5716 else
5717 for (current = decl; i > 0; --i, parms = TREE_CHAIN (parms))
5719 a = TMPL_ARGS_LEVEL (args, i);
5720 t = INNERMOST_TEMPLATE_PARMS (parms);
5722 if (TREE_VEC_LENGTH (t) != TREE_VEC_LENGTH (a))
5724 if (current == decl)
5725 error ("got %d template parameters for %q#D",
5726 TREE_VEC_LENGTH (a), decl);
5727 else
5728 error ("got %d template parameters for %q#T",
5729 TREE_VEC_LENGTH (a), current);
5730 error (" but %d required", TREE_VEC_LENGTH (t));
5731 /* Avoid crash in import_export_decl. */
5732 DECL_INTERFACE_KNOWN (decl) = 1;
5733 return error_mark_node;
5736 if (current == decl)
5737 current = ctx;
5738 else if (current == NULL_TREE)
5739 /* Can happen in erroneous input. */
5740 break;
5741 else
5742 current = get_containing_scope (current);
5745 /* Check that the parms are used in the appropriate qualifying scopes
5746 in the declarator. */
5747 if (!comp_template_args
5748 (TI_ARGS (tinfo),
5749 TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl)))))
5751 error ("template arguments to %qD do not match original "
5752 "template %qD", decl, DECL_TEMPLATE_RESULT (tmpl));
5753 if (!uses_template_parms (TI_ARGS (tinfo)))
5754 inform (input_location, "use %<template<>%> for"
5755 " an explicit specialization");
5756 /* Avoid crash in import_export_decl. */
5757 DECL_INTERFACE_KNOWN (decl) = 1;
5758 return error_mark_node;
5762 DECL_TEMPLATE_RESULT (tmpl) = decl;
5763 TREE_TYPE (tmpl) = TREE_TYPE (decl);
5765 /* Push template declarations for global functions and types. Note
5766 that we do not try to push a global template friend declared in a
5767 template class; such a thing may well depend on the template
5768 parameters of the class. */
5769 if (new_template_p && !ctx
5770 && !(is_friend && template_class_depth (current_class_type) > 0))
5772 tmpl = pushdecl_namespace_level (tmpl, is_friend);
5773 if (tmpl == error_mark_node)
5774 return error_mark_node;
5776 /* Hide template friend classes that haven't been declared yet. */
5777 if (is_friend && TREE_CODE (decl) == TYPE_DECL)
5779 DECL_ANTICIPATED (tmpl) = 1;
5780 DECL_FRIEND_P (tmpl) = 1;
5784 if (is_primary)
5786 tree parms = DECL_TEMPLATE_PARMS (tmpl);
5788 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5790 /* Give template template parms a DECL_CONTEXT of the template
5791 for which they are a parameter. */
5792 parms = INNERMOST_TEMPLATE_PARMS (parms);
5793 for (int i = TREE_VEC_LENGTH (parms) - 1; i >= 0; --i)
5795 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5796 if (TREE_CODE (parm) == TEMPLATE_DECL)
5797 DECL_CONTEXT (parm) = tmpl;
5800 if (TREE_CODE (decl) == TYPE_DECL
5801 && TYPE_DECL_ALIAS_P (decl)
5802 && complex_alias_template_p (tmpl))
5803 TEMPLATE_DECL_COMPLEX_ALIAS_P (tmpl) = true;
5806 /* The DECL_TI_ARGS of DECL contains full set of arguments referring
5807 back to its most general template. If TMPL is a specialization,
5808 ARGS may only have the innermost set of arguments. Add the missing
5809 argument levels if necessary. */
5810 if (DECL_TEMPLATE_INFO (tmpl))
5811 args = add_outermost_template_args (DECL_TI_ARGS (tmpl), args);
5813 info = build_template_info (tmpl, args);
5815 if (DECL_IMPLICIT_TYPEDEF_P (decl))
5816 SET_TYPE_TEMPLATE_INFO (TREE_TYPE (tmpl), info);
5817 else
5819 if (is_primary)
5820 retrofit_lang_decl (decl);
5821 if (DECL_LANG_SPECIFIC (decl))
5822 DECL_TEMPLATE_INFO (decl) = info;
5825 if (flag_implicit_templates
5826 && !is_friend
5827 && TREE_PUBLIC (decl)
5828 && VAR_OR_FUNCTION_DECL_P (decl))
5829 /* Set DECL_COMDAT on template instantiations; if we force
5830 them to be emitted by explicit instantiation or -frepo,
5831 mark_needed will tell cgraph to do the right thing. */
5832 DECL_COMDAT (decl) = true;
5834 return DECL_TEMPLATE_RESULT (tmpl);
5837 tree
5838 push_template_decl (tree decl)
5840 return push_template_decl_real (decl, false);
5843 /* FN is an inheriting constructor that inherits from the constructor
5844 template INHERITED; turn FN into a constructor template with a matching
5845 template header. */
5847 tree
5848 add_inherited_template_parms (tree fn, tree inherited)
5850 tree inner_parms
5851 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (inherited));
5852 inner_parms = copy_node (inner_parms);
5853 tree parms
5854 = tree_cons (size_int (processing_template_decl + 1),
5855 inner_parms, current_template_parms);
5856 tree tmpl = build_template_decl (fn, parms, /*member*/true);
5857 tree args = template_parms_to_args (parms);
5858 DECL_TEMPLATE_INFO (fn) = build_template_info (tmpl, args);
5859 TREE_TYPE (tmpl) = TREE_TYPE (fn);
5860 DECL_TEMPLATE_RESULT (tmpl) = fn;
5861 DECL_ARTIFICIAL (tmpl) = true;
5862 DECL_PRIMARY_TEMPLATE (tmpl) = tmpl;
5863 return tmpl;
5866 /* Called when a class template TYPE is redeclared with the indicated
5867 template PARMS, e.g.:
5869 template <class T> struct S;
5870 template <class T> struct S {}; */
5872 bool
5873 redeclare_class_template (tree type, tree parms, tree cons)
5875 tree tmpl;
5876 tree tmpl_parms;
5877 int i;
5879 if (!TYPE_TEMPLATE_INFO (type))
5881 error ("%qT is not a template type", type);
5882 return false;
5885 tmpl = TYPE_TI_TEMPLATE (type);
5886 if (!PRIMARY_TEMPLATE_P (tmpl))
5887 /* The type is nested in some template class. Nothing to worry
5888 about here; there are no new template parameters for the nested
5889 type. */
5890 return true;
5892 if (!parms)
5894 error ("template specifiers not specified in declaration of %qD",
5895 tmpl);
5896 return false;
5899 parms = INNERMOST_TEMPLATE_PARMS (parms);
5900 tmpl_parms = DECL_INNERMOST_TEMPLATE_PARMS (tmpl);
5902 if (TREE_VEC_LENGTH (parms) != TREE_VEC_LENGTH (tmpl_parms))
5904 error_n (input_location, TREE_VEC_LENGTH (parms),
5905 "redeclared with %d template parameter",
5906 "redeclared with %d template parameters",
5907 TREE_VEC_LENGTH (parms));
5908 inform_n (DECL_SOURCE_LOCATION (tmpl), TREE_VEC_LENGTH (tmpl_parms),
5909 "previous declaration %qD used %d template parameter",
5910 "previous declaration %qD used %d template parameters",
5911 tmpl, TREE_VEC_LENGTH (tmpl_parms));
5912 return false;
5915 for (i = 0; i < TREE_VEC_LENGTH (tmpl_parms); ++i)
5917 tree tmpl_parm;
5918 tree parm;
5919 tree tmpl_default;
5920 tree parm_default;
5922 if (TREE_VEC_ELT (tmpl_parms, i) == error_mark_node
5923 || TREE_VEC_ELT (parms, i) == error_mark_node)
5924 continue;
5926 tmpl_parm = TREE_VALUE (TREE_VEC_ELT (tmpl_parms, i));
5927 if (error_operand_p (tmpl_parm))
5928 return false;
5930 parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
5931 tmpl_default = TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i));
5932 parm_default = TREE_PURPOSE (TREE_VEC_ELT (parms, i));
5934 /* TMPL_PARM and PARM can be either TYPE_DECL, PARM_DECL, or
5935 TEMPLATE_DECL. */
5936 if (TREE_CODE (tmpl_parm) != TREE_CODE (parm)
5937 || (TREE_CODE (tmpl_parm) != TYPE_DECL
5938 && !same_type_p (TREE_TYPE (tmpl_parm), TREE_TYPE (parm)))
5939 || (TREE_CODE (tmpl_parm) != PARM_DECL
5940 && (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (tmpl_parm))
5941 != TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm))))
5942 || (TREE_CODE (tmpl_parm) == PARM_DECL
5943 && (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (tmpl_parm))
5944 != TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))))
5946 error ("template parameter %q+#D", tmpl_parm);
5947 error ("redeclared here as %q#D", parm);
5948 return false;
5951 if (tmpl_default != NULL_TREE && parm_default != NULL_TREE)
5953 /* We have in [temp.param]:
5955 A template-parameter may not be given default arguments
5956 by two different declarations in the same scope. */
5957 error_at (input_location, "redefinition of default argument for %q#D", parm);
5958 inform (DECL_SOURCE_LOCATION (tmpl_parm),
5959 "original definition appeared here");
5960 return false;
5963 if (parm_default != NULL_TREE)
5964 /* Update the previous template parameters (which are the ones
5965 that will really count) with the new default value. */
5966 TREE_PURPOSE (TREE_VEC_ELT (tmpl_parms, i)) = parm_default;
5967 else if (tmpl_default != NULL_TREE)
5968 /* Update the new parameters, too; they'll be used as the
5969 parameters for any members. */
5970 TREE_PURPOSE (TREE_VEC_ELT (parms, i)) = tmpl_default;
5972 /* Give each template template parm in this redeclaration a
5973 DECL_CONTEXT of the template for which they are a parameter. */
5974 if (TREE_CODE (parm) == TEMPLATE_DECL)
5976 gcc_assert (DECL_CONTEXT (parm) == NULL_TREE);
5977 DECL_CONTEXT (parm) = tmpl;
5980 if (TREE_CODE (parm) == TYPE_DECL)
5981 TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (parm)) = true;
5984 // Cannot redeclare a class template with a different set of constraints.
5985 if (!equivalent_constraints (get_constraints (tmpl), cons))
5987 error_at (input_location, "redeclaration %q#D with different "
5988 "constraints", tmpl);
5989 inform (DECL_SOURCE_LOCATION (tmpl),
5990 "original declaration appeared here");
5993 return true;
5996 /* The actual substitution part of instantiate_non_dependent_expr_sfinae,
5997 to be used when the caller has already checked
5998 (processing_template_decl
5999 && !instantiation_dependent_expression_p (expr)
6000 && potential_constant_expression (expr))
6001 and cleared processing_template_decl. */
6003 tree
6004 instantiate_non_dependent_expr_internal (tree expr, tsubst_flags_t complain)
6006 return tsubst_copy_and_build (expr,
6007 /*args=*/NULL_TREE,
6008 complain,
6009 /*in_decl=*/NULL_TREE,
6010 /*function_p=*/false,
6011 /*integral_constant_expression_p=*/true);
6014 /* Simplify EXPR if it is a non-dependent expression. Returns the
6015 (possibly simplified) expression. */
6017 tree
6018 instantiate_non_dependent_expr_sfinae (tree expr, tsubst_flags_t complain)
6020 if (expr == NULL_TREE)
6021 return NULL_TREE;
6023 /* If we're in a template, but EXPR isn't value dependent, simplify
6024 it. We're supposed to treat:
6026 template <typename T> void f(T[1 + 1]);
6027 template <typename T> void f(T[2]);
6029 as two declarations of the same function, for example. */
6030 if (processing_template_decl
6031 && is_nondependent_constant_expression (expr))
6033 processing_template_decl_sentinel s;
6034 expr = instantiate_non_dependent_expr_internal (expr, complain);
6036 return expr;
6039 tree
6040 instantiate_non_dependent_expr (tree expr)
6042 return instantiate_non_dependent_expr_sfinae (expr, tf_error);
6045 /* Like instantiate_non_dependent_expr, but return NULL_TREE rather than
6046 an uninstantiated expression. */
6048 tree
6049 instantiate_non_dependent_or_null (tree expr)
6051 if (expr == NULL_TREE)
6052 return NULL_TREE;
6053 if (processing_template_decl)
6055 if (!is_nondependent_constant_expression (expr))
6056 expr = NULL_TREE;
6057 else
6059 processing_template_decl_sentinel s;
6060 expr = instantiate_non_dependent_expr_internal (expr, tf_error);
6063 return expr;
6066 /* True iff T is a specialization of a variable template. */
6068 bool
6069 variable_template_specialization_p (tree t)
6071 if (!VAR_P (t) || !DECL_LANG_SPECIFIC (t) || !DECL_TEMPLATE_INFO (t))
6072 return false;
6073 tree tmpl = DECL_TI_TEMPLATE (t);
6074 return variable_template_p (tmpl);
6077 /* Return TRUE iff T is a type alias, a TEMPLATE_DECL for an alias
6078 template declaration, or a TYPE_DECL for an alias declaration. */
6080 bool
6081 alias_type_or_template_p (tree t)
6083 if (t == NULL_TREE)
6084 return false;
6085 return ((TREE_CODE (t) == TYPE_DECL && TYPE_DECL_ALIAS_P (t))
6086 || (TYPE_P (t)
6087 && TYPE_NAME (t)
6088 && TYPE_DECL_ALIAS_P (TYPE_NAME (t)))
6089 || DECL_ALIAS_TEMPLATE_P (t));
6092 /* Return TRUE iff T is a specialization of an alias template. */
6094 bool
6095 alias_template_specialization_p (const_tree t)
6097 /* It's an alias template specialization if it's an alias and its
6098 TYPE_NAME is a specialization of a primary template. */
6099 if (TYPE_ALIAS_P (t))
6100 if (tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t))
6101 return PRIMARY_TEMPLATE_P (TI_TEMPLATE (tinfo));
6103 return false;
6106 /* An alias template is complex from a SFINAE perspective if a template-id
6107 using that alias can be ill-formed when the expansion is not, as with
6108 the void_t template. We determine this by checking whether the
6109 expansion for the alias template uses all its template parameters. */
6111 struct uses_all_template_parms_data
6113 int level;
6114 bool *seen;
6117 static int
6118 uses_all_template_parms_r (tree t, void *data_)
6120 struct uses_all_template_parms_data &data
6121 = *(struct uses_all_template_parms_data*)data_;
6122 tree idx = get_template_parm_index (t);
6124 if (TEMPLATE_PARM_LEVEL (idx) == data.level)
6125 data.seen[TEMPLATE_PARM_IDX (idx)] = true;
6126 return 0;
6129 static bool
6130 complex_alias_template_p (const_tree tmpl)
6132 struct uses_all_template_parms_data data;
6133 tree pat = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6134 tree parms = DECL_TEMPLATE_PARMS (tmpl);
6135 data.level = TMPL_PARMS_DEPTH (parms);
6136 int len = TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS (parms));
6137 data.seen = XALLOCAVEC (bool, len);
6138 for (int i = 0; i < len; ++i)
6139 data.seen[i] = false;
6141 for_each_template_parm (pat, uses_all_template_parms_r, &data, NULL, true);
6142 for (int i = 0; i < len; ++i)
6143 if (!data.seen[i])
6144 return true;
6145 return false;
6148 /* Return TRUE iff T is a specialization of a complex alias template with
6149 dependent template-arguments. */
6151 bool
6152 dependent_alias_template_spec_p (const_tree t)
6154 if (!alias_template_specialization_p (t))
6155 return false;
6157 tree tinfo = TYPE_ALIAS_TEMPLATE_INFO (t);
6158 if (!TEMPLATE_DECL_COMPLEX_ALIAS_P (TI_TEMPLATE (tinfo)))
6159 return false;
6161 tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo));
6162 if (!any_dependent_template_arguments_p (args))
6163 return false;
6165 return true;
6168 /* Return the number of innermost template parameters in TMPL. */
6170 static int
6171 num_innermost_template_parms (tree tmpl)
6173 tree parms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
6174 return TREE_VEC_LENGTH (parms);
6177 /* Return either TMPL or another template that it is equivalent to under DR
6178 1286: An alias that just changes the name of a template is equivalent to
6179 the other template. */
6181 static tree
6182 get_underlying_template (tree tmpl)
6184 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
6185 while (DECL_ALIAS_TEMPLATE_P (tmpl))
6187 /* Determine if the alias is equivalent to an underlying template. */
6188 tree orig_type = DECL_ORIGINAL_TYPE (DECL_TEMPLATE_RESULT (tmpl));
6189 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (orig_type);
6190 if (!tinfo)
6191 break;
6193 tree underlying = TI_TEMPLATE (tinfo);
6194 if (!PRIMARY_TEMPLATE_P (underlying)
6195 || (num_innermost_template_parms (tmpl)
6196 != num_innermost_template_parms (underlying)))
6197 break;
6199 tree alias_args = INNERMOST_TEMPLATE_ARGS
6200 (template_parms_to_args (DECL_TEMPLATE_PARMS (tmpl)));
6201 if (!comp_template_args (TI_ARGS (tinfo), alias_args))
6202 break;
6204 /* Alias is equivalent. Strip it and repeat. */
6205 tmpl = underlying;
6208 return tmpl;
6211 /* Subroutine of convert_nontype_argument. Converts EXPR to TYPE, which
6212 must be a reference-to-function or a pointer-to-function type, as specified
6213 in [temp.arg.nontype]: disambiguate EXPR if it is an overload set,
6214 and check that the resulting function has external linkage. */
6216 static tree
6217 convert_nontype_argument_function (tree type, tree expr,
6218 tsubst_flags_t complain)
6220 tree fns = expr;
6221 tree fn, fn_no_ptr;
6222 linkage_kind linkage;
6224 fn = instantiate_type (type, fns, tf_none);
6225 if (fn == error_mark_node)
6226 return error_mark_node;
6228 if (value_dependent_expression_p (fn))
6229 goto accept;
6231 fn_no_ptr = strip_fnptr_conv (fn);
6232 if (TREE_CODE (fn_no_ptr) == ADDR_EXPR)
6233 fn_no_ptr = TREE_OPERAND (fn_no_ptr, 0);
6234 if (BASELINK_P (fn_no_ptr))
6235 fn_no_ptr = BASELINK_FUNCTIONS (fn_no_ptr);
6237 /* [temp.arg.nontype]/1
6239 A template-argument for a non-type, non-template template-parameter
6240 shall be one of:
6241 [...]
6242 -- the address of an object or function with external [C++11: or
6243 internal] linkage. */
6245 if (TREE_CODE (fn_no_ptr) != FUNCTION_DECL)
6247 if (complain & tf_error)
6249 error ("%qE is not a valid template argument for type %qT",
6250 expr, type);
6251 if (TYPE_PTR_P (type))
6252 inform (input_location, "it must be the address of a function "
6253 "with external linkage");
6254 else
6255 inform (input_location, "it must be the name of a function with "
6256 "external linkage");
6258 return NULL_TREE;
6261 linkage = decl_linkage (fn_no_ptr);
6262 if (cxx_dialect >= cxx11 ? linkage == lk_none : linkage != lk_external)
6264 if (complain & tf_error)
6266 if (cxx_dialect >= cxx11)
6267 error ("%qE is not a valid template argument for type %qT "
6268 "because %qD has no linkage",
6269 expr, type, fn_no_ptr);
6270 else
6271 error ("%qE is not a valid template argument for type %qT "
6272 "because %qD does not have external linkage",
6273 expr, type, fn_no_ptr);
6275 return NULL_TREE;
6278 accept:
6279 if (TREE_CODE (type) == REFERENCE_TYPE)
6281 if (REFERENCE_REF_P (fn))
6282 fn = TREE_OPERAND (fn, 0);
6283 else
6284 fn = build_address (fn);
6286 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (fn)))
6287 fn = build_nop (type, fn);
6289 return fn;
6292 /* Subroutine of convert_nontype_argument.
6293 Check if EXPR of type TYPE is a valid pointer-to-member constant.
6294 Emit an error otherwise. */
6296 static bool
6297 check_valid_ptrmem_cst_expr (tree type, tree expr,
6298 tsubst_flags_t complain)
6300 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6301 tree orig_expr = expr;
6302 STRIP_NOPS (expr);
6303 if (null_ptr_cst_p (expr))
6304 return true;
6305 if (TREE_CODE (expr) == PTRMEM_CST
6306 && same_type_p (TYPE_PTRMEM_CLASS_TYPE (type),
6307 PTRMEM_CST_CLASS (expr)))
6308 return true;
6309 if (cxx_dialect >= cxx11 && null_member_pointer_value_p (expr))
6310 return true;
6311 if (processing_template_decl
6312 && TREE_CODE (expr) == ADDR_EXPR
6313 && TREE_CODE (TREE_OPERAND (expr, 0)) == OFFSET_REF)
6314 return true;
6315 if (complain & tf_error)
6317 error_at (loc, "%qE is not a valid template argument for type %qT",
6318 orig_expr, type);
6319 if (TREE_CODE (expr) != PTRMEM_CST)
6320 inform (loc, "it must be a pointer-to-member of the form %<&X::Y%>");
6321 else
6322 inform (loc, "because it is a member of %qT", PTRMEM_CST_CLASS (expr));
6324 return false;
6327 /* Returns TRUE iff the address of OP is value-dependent.
6329 14.6.2.4 [temp.dep.temp]:
6330 A non-integral non-type template-argument is dependent if its type is
6331 dependent or it has either of the following forms
6332 qualified-id
6333 & qualified-id
6334 and contains a nested-name-specifier which specifies a class-name that
6335 names a dependent type.
6337 We generalize this to just say that the address of a member of a
6338 dependent class is value-dependent; the above doesn't cover the
6339 address of a static data member named with an unqualified-id. */
6341 static bool
6342 has_value_dependent_address (tree op)
6344 /* We could use get_inner_reference here, but there's no need;
6345 this is only relevant for template non-type arguments, which
6346 can only be expressed as &id-expression. */
6347 if (DECL_P (op))
6349 tree ctx = CP_DECL_CONTEXT (op);
6350 if (TYPE_P (ctx) && dependent_type_p (ctx))
6351 return true;
6354 return false;
6357 /* The next set of functions are used for providing helpful explanatory
6358 diagnostics for failed overload resolution. Their messages should be
6359 indented by two spaces for consistency with the messages in
6360 call.c */
6362 static int
6363 unify_success (bool /*explain_p*/)
6365 return 0;
6368 /* Other failure functions should call this one, to provide a single function
6369 for setting a breakpoint on. */
6371 static int
6372 unify_invalid (bool /*explain_p*/)
6374 return 1;
6377 static int
6378 unify_parameter_deduction_failure (bool explain_p, tree parm)
6380 if (explain_p)
6381 inform (input_location,
6382 " couldn't deduce template parameter %qD", parm);
6383 return unify_invalid (explain_p);
6386 static int
6387 unify_cv_qual_mismatch (bool explain_p, tree parm, tree arg)
6389 if (explain_p)
6390 inform (input_location,
6391 " types %qT and %qT have incompatible cv-qualifiers",
6392 parm, arg);
6393 return unify_invalid (explain_p);
6396 static int
6397 unify_type_mismatch (bool explain_p, tree parm, tree arg)
6399 if (explain_p)
6400 inform (input_location, " mismatched types %qT and %qT", parm, arg);
6401 return unify_invalid (explain_p);
6404 static int
6405 unify_parameter_pack_mismatch (bool explain_p, tree parm, tree arg)
6407 if (explain_p)
6408 inform (input_location,
6409 " template parameter %qD is not a parameter pack, but "
6410 "argument %qD is",
6411 parm, arg);
6412 return unify_invalid (explain_p);
6415 static int
6416 unify_ptrmem_cst_mismatch (bool explain_p, tree parm, tree arg)
6418 if (explain_p)
6419 inform (input_location,
6420 " template argument %qE does not match "
6421 "pointer-to-member constant %qE",
6422 arg, parm);
6423 return unify_invalid (explain_p);
6426 static int
6427 unify_expression_unequal (bool explain_p, tree parm, tree arg)
6429 if (explain_p)
6430 inform (input_location, " %qE is not equivalent to %qE", parm, arg);
6431 return unify_invalid (explain_p);
6434 static int
6435 unify_parameter_pack_inconsistent (bool explain_p, tree old_arg, tree new_arg)
6437 if (explain_p)
6438 inform (input_location,
6439 " inconsistent parameter pack deduction with %qT and %qT",
6440 old_arg, new_arg);
6441 return unify_invalid (explain_p);
6444 static int
6445 unify_inconsistency (bool explain_p, tree parm, tree first, tree second)
6447 if (explain_p)
6449 if (TYPE_P (parm))
6450 inform (input_location,
6451 " deduced conflicting types for parameter %qT (%qT and %qT)",
6452 parm, first, second);
6453 else
6454 inform (input_location,
6455 " deduced conflicting values for non-type parameter "
6456 "%qE (%qE and %qE)", parm, first, second);
6458 return unify_invalid (explain_p);
6461 static int
6462 unify_vla_arg (bool explain_p, tree arg)
6464 if (explain_p)
6465 inform (input_location,
6466 " variable-sized array type %qT is not "
6467 "a valid template argument",
6468 arg);
6469 return unify_invalid (explain_p);
6472 static int
6473 unify_method_type_error (bool explain_p, tree arg)
6475 if (explain_p)
6476 inform (input_location,
6477 " member function type %qT is not a valid template argument",
6478 arg);
6479 return unify_invalid (explain_p);
6482 static int
6483 unify_arity (bool explain_p, int have, int wanted, bool least_p = false)
6485 if (explain_p)
6487 if (least_p)
6488 inform_n (input_location, wanted,
6489 " candidate expects at least %d argument, %d provided",
6490 " candidate expects at least %d arguments, %d provided",
6491 wanted, have);
6492 else
6493 inform_n (input_location, wanted,
6494 " candidate expects %d argument, %d provided",
6495 " candidate expects %d arguments, %d provided",
6496 wanted, have);
6498 return unify_invalid (explain_p);
6501 static int
6502 unify_too_many_arguments (bool explain_p, int have, int wanted)
6504 return unify_arity (explain_p, have, wanted);
6507 static int
6508 unify_too_few_arguments (bool explain_p, int have, int wanted,
6509 bool least_p = false)
6511 return unify_arity (explain_p, have, wanted, least_p);
6514 static int
6515 unify_arg_conversion (bool explain_p, tree to_type,
6516 tree from_type, tree arg)
6518 if (explain_p)
6519 inform (EXPR_LOC_OR_LOC (arg, input_location),
6520 " cannot convert %qE (type %qT) to type %qT",
6521 arg, from_type, to_type);
6522 return unify_invalid (explain_p);
6525 static int
6526 unify_no_common_base (bool explain_p, enum template_base_result r,
6527 tree parm, tree arg)
6529 if (explain_p)
6530 switch (r)
6532 case tbr_ambiguous_baseclass:
6533 inform (input_location, " %qT is an ambiguous base class of %qT",
6534 parm, arg);
6535 break;
6536 default:
6537 inform (input_location, " %qT is not derived from %qT", arg, parm);
6538 break;
6540 return unify_invalid (explain_p);
6543 static int
6544 unify_inconsistent_template_template_parameters (bool explain_p)
6546 if (explain_p)
6547 inform (input_location,
6548 " template parameters of a template template argument are "
6549 "inconsistent with other deduced template arguments");
6550 return unify_invalid (explain_p);
6553 static int
6554 unify_template_deduction_failure (bool explain_p, tree parm, tree arg)
6556 if (explain_p)
6557 inform (input_location,
6558 " can't deduce a template for %qT from non-template type %qT",
6559 parm, arg);
6560 return unify_invalid (explain_p);
6563 static int
6564 unify_template_argument_mismatch (bool explain_p, tree parm, tree arg)
6566 if (explain_p)
6567 inform (input_location,
6568 " template argument %qE does not match %qE", arg, parm);
6569 return unify_invalid (explain_p);
6572 /* Attempt to convert the non-type template parameter EXPR to the
6573 indicated TYPE. If the conversion is successful, return the
6574 converted value. If the conversion is unsuccessful, return
6575 NULL_TREE if we issued an error message, or error_mark_node if we
6576 did not. We issue error messages for out-and-out bad template
6577 parameters, but not simply because the conversion failed, since we
6578 might be just trying to do argument deduction. Both TYPE and EXPR
6579 must be non-dependent.
6581 The conversion follows the special rules described in
6582 [temp.arg.nontype], and it is much more strict than an implicit
6583 conversion.
6585 This function is called twice for each template argument (see
6586 lookup_template_class for a more accurate description of this
6587 problem). This means that we need to handle expressions which
6588 are not valid in a C++ source, but can be created from the
6589 first call (for instance, casts to perform conversions). These
6590 hacks can go away after we fix the double coercion problem. */
6592 static tree
6593 convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain)
6595 tree expr_type;
6596 location_t loc = EXPR_LOC_OR_LOC (expr, input_location);
6597 tree orig_expr = expr;
6599 /* Detect immediately string literals as invalid non-type argument.
6600 This special-case is not needed for correctness (we would easily
6601 catch this later), but only to provide better diagnostic for this
6602 common user mistake. As suggested by DR 100, we do not mention
6603 linkage issues in the diagnostic as this is not the point. */
6604 /* FIXME we're making this OK. */
6605 if (TREE_CODE (expr) == STRING_CST)
6607 if (complain & tf_error)
6608 error ("%qE is not a valid template argument for type %qT "
6609 "because string literals can never be used in this context",
6610 expr, type);
6611 return NULL_TREE;
6614 /* Add the ADDR_EXPR now for the benefit of
6615 value_dependent_expression_p. */
6616 if (TYPE_PTROBV_P (type)
6617 && TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE)
6619 expr = decay_conversion (expr, complain);
6620 if (expr == error_mark_node)
6621 return error_mark_node;
6624 /* If we are in a template, EXPR may be non-dependent, but still
6625 have a syntactic, rather than semantic, form. For example, EXPR
6626 might be a SCOPE_REF, rather than the VAR_DECL to which the
6627 SCOPE_REF refers. Preserving the qualifying scope is necessary
6628 so that access checking can be performed when the template is
6629 instantiated -- but here we need the resolved form so that we can
6630 convert the argument. */
6631 bool non_dep = false;
6632 if (TYPE_REF_OBJ_P (type)
6633 && has_value_dependent_address (expr))
6634 /* If we want the address and it's value-dependent, don't fold. */;
6635 else if (processing_template_decl
6636 && is_nondependent_constant_expression (expr))
6637 non_dep = true;
6638 if (error_operand_p (expr))
6639 return error_mark_node;
6640 expr_type = TREE_TYPE (expr);
6642 /* If the argument is non-dependent, perform any conversions in
6643 non-dependent context as well. */
6644 processing_template_decl_sentinel s (non_dep);
6645 if (non_dep)
6646 expr = instantiate_non_dependent_expr_internal (expr, complain);
6648 if (value_dependent_expression_p (expr))
6649 expr = canonicalize_expr_argument (expr, complain);
6651 /* 14.3.2/5: The null pointer{,-to-member} conversion is applied
6652 to a non-type argument of "nullptr". */
6653 if (NULLPTR_TYPE_P (expr_type) && TYPE_PTR_OR_PTRMEM_P (type))
6654 expr = fold_simple (convert (type, expr));
6656 /* In C++11, integral or enumeration non-type template arguments can be
6657 arbitrary constant expressions. Pointer and pointer to
6658 member arguments can be general constant expressions that evaluate
6659 to a null value, but otherwise still need to be of a specific form. */
6660 if (cxx_dialect >= cxx11)
6662 if (TREE_CODE (expr) == PTRMEM_CST)
6663 /* A PTRMEM_CST is already constant, and a valid template
6664 argument for a parameter of pointer to member type, we just want
6665 to leave it in that form rather than lower it to a
6666 CONSTRUCTOR. */;
6667 else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type)
6668 || cxx_dialect >= cxx17)
6670 /* C++17: A template-argument for a non-type template-parameter shall
6671 be a converted constant expression (8.20) of the type of the
6672 template-parameter. */
6673 expr = build_converted_constant_expr (type, expr, complain);
6674 if (expr == error_mark_node)
6675 return error_mark_node;
6676 expr = maybe_constant_value (expr);
6677 expr = convert_from_reference (expr);
6679 else if (TYPE_PTR_OR_PTRMEM_P (type))
6681 tree folded = maybe_constant_value (expr);
6682 if (TYPE_PTR_P (type) ? integer_zerop (folded)
6683 : null_member_pointer_value_p (folded))
6684 expr = folded;
6688 if (TREE_CODE (type) == REFERENCE_TYPE)
6689 expr = mark_lvalue_use (expr);
6690 else
6691 expr = mark_rvalue_use (expr);
6693 /* HACK: Due to double coercion, we can get a
6694 NOP_EXPR<REFERENCE_TYPE>(ADDR_EXPR<POINTER_TYPE> (arg)) here,
6695 which is the tree that we built on the first call (see
6696 below when coercing to reference to object or to reference to
6697 function). We just strip everything and get to the arg.
6698 See g++.old-deja/g++.oliva/template4.C and g++.dg/template/nontype9.C
6699 for examples. */
6700 if (TYPE_REF_OBJ_P (type) || TYPE_REFFN_P (type))
6702 tree probe_type, probe = expr;
6703 if (REFERENCE_REF_P (probe))
6704 probe = TREE_OPERAND (probe, 0);
6705 probe_type = TREE_TYPE (probe);
6706 if (TREE_CODE (probe) == NOP_EXPR)
6708 /* ??? Maybe we could use convert_from_reference here, but we
6709 would need to relax its constraints because the NOP_EXPR
6710 could actually change the type to something more cv-qualified,
6711 and this is not folded by convert_from_reference. */
6712 tree addr = TREE_OPERAND (probe, 0);
6713 if (TREE_CODE (probe_type) == REFERENCE_TYPE
6714 && TREE_CODE (addr) == ADDR_EXPR
6715 && TYPE_PTR_P (TREE_TYPE (addr))
6716 && (same_type_ignoring_top_level_qualifiers_p
6717 (TREE_TYPE (probe_type),
6718 TREE_TYPE (TREE_TYPE (addr)))))
6720 expr = TREE_OPERAND (addr, 0);
6721 expr_type = TREE_TYPE (probe_type);
6726 /* [temp.arg.nontype]/5, bullet 1
6728 For a non-type template-parameter of integral or enumeration type,
6729 integral promotions (_conv.prom_) and integral conversions
6730 (_conv.integral_) are applied. */
6731 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
6733 if (cxx_dialect < cxx11)
6735 tree t = build_converted_constant_expr (type, expr, complain);
6736 t = maybe_constant_value (t);
6737 if (t != error_mark_node)
6738 expr = t;
6741 if (!same_type_ignoring_top_level_qualifiers_p (type, TREE_TYPE (expr)))
6742 return error_mark_node;
6744 /* Notice that there are constant expressions like '4 % 0' which
6745 do not fold into integer constants. */
6746 if (TREE_CODE (expr) != INTEGER_CST
6747 && !value_dependent_expression_p (expr))
6749 if (complain & tf_error)
6751 int errs = errorcount, warns = warningcount + werrorcount;
6752 if (!require_potential_constant_expression (expr))
6753 expr = error_mark_node;
6754 else
6755 expr = cxx_constant_value (expr);
6756 if (errorcount > errs || warningcount + werrorcount > warns)
6757 inform (loc, "in template argument for type %qT", type);
6758 if (expr == error_mark_node)
6759 return NULL_TREE;
6760 /* else cxx_constant_value complained but gave us
6761 a real constant, so go ahead. */
6762 if (TREE_CODE (expr) != INTEGER_CST)
6764 /* Some assemble time constant expressions like
6765 (intptr_t)&&lab1 - (intptr_t)&&lab2 or
6766 4 + (intptr_t)&&var satisfy reduced_constant_expression_p
6767 as we can emit them into .rodata initializers of
6768 variables, yet they can't fold into an INTEGER_CST at
6769 compile time. Refuse them here. */
6770 gcc_checking_assert (reduced_constant_expression_p (expr));
6771 error_at (loc, "template argument %qE for type %qT not "
6772 "a constant integer", expr, type);
6773 return NULL_TREE;
6776 else
6777 return NULL_TREE;
6780 /* Avoid typedef problems. */
6781 if (TREE_TYPE (expr) != type)
6782 expr = fold_convert (type, expr);
6784 /* [temp.arg.nontype]/5, bullet 2
6786 For a non-type template-parameter of type pointer to object,
6787 qualification conversions (_conv.qual_) and the array-to-pointer
6788 conversion (_conv.array_) are applied. */
6789 else if (TYPE_PTROBV_P (type))
6791 tree decayed = expr;
6793 /* Look through any NOP_EXPRs around an ADDR_EXPR, whether they come from
6794 decay_conversion or an explicit cast. If it's a problematic cast,
6795 we'll complain about it below. */
6796 if (TREE_CODE (expr) == NOP_EXPR)
6798 tree probe = expr;
6799 STRIP_NOPS (probe);
6800 if (TREE_CODE (probe) == ADDR_EXPR
6801 && TYPE_PTR_P (TREE_TYPE (probe)))
6803 expr = probe;
6804 expr_type = TREE_TYPE (expr);
6808 /* [temp.arg.nontype]/1 (TC1 version, DR 49):
6810 A template-argument for a non-type, non-template template-parameter
6811 shall be one of: [...]
6813 -- the name of a non-type template-parameter;
6814 -- the address of an object or function with external linkage, [...]
6815 expressed as "& id-expression" where the & is optional if the name
6816 refers to a function or array, or if the corresponding
6817 template-parameter is a reference.
6819 Here, we do not care about functions, as they are invalid anyway
6820 for a parameter of type pointer-to-object. */
6822 if (value_dependent_expression_p (expr))
6823 /* Non-type template parameters are OK. */
6825 else if (cxx_dialect >= cxx11 && integer_zerop (expr))
6826 /* Null pointer values are OK in C++11. */;
6827 else if (TREE_CODE (expr) != ADDR_EXPR)
6829 if (VAR_P (expr))
6831 if (complain & tf_error)
6832 error ("%qD is not a valid template argument "
6833 "because %qD is a variable, not the address of "
6834 "a variable", orig_expr, expr);
6835 return NULL_TREE;
6837 if (POINTER_TYPE_P (expr_type))
6839 if (complain & tf_error)
6840 error ("%qE is not a valid template argument for %qT "
6841 "because it is not the address of a variable",
6842 orig_expr, type);
6843 return NULL_TREE;
6845 /* Other values, like integer constants, might be valid
6846 non-type arguments of some other type. */
6847 return error_mark_node;
6849 else
6851 tree decl = TREE_OPERAND (expr, 0);
6853 if (!VAR_P (decl))
6855 if (complain & tf_error)
6856 error ("%qE is not a valid template argument of type %qT "
6857 "because %qE is not a variable", orig_expr, type, decl);
6858 return NULL_TREE;
6860 else if (cxx_dialect < cxx11 && !DECL_EXTERNAL_LINKAGE_P (decl))
6862 if (complain & tf_error)
6863 error ("%qE is not a valid template argument of type %qT "
6864 "because %qD does not have external linkage",
6865 orig_expr, type, decl);
6866 return NULL_TREE;
6868 else if ((cxx_dialect >= cxx11 && cxx_dialect < cxx17)
6869 && decl_linkage (decl) == lk_none)
6871 if (complain & tf_error)
6872 error ("%qE is not a valid template argument of type %qT "
6873 "because %qD has no linkage", orig_expr, type, decl);
6874 return NULL_TREE;
6876 /* C++17: For a non-type template-parameter of reference or pointer
6877 type, the value of the constant expression shall not refer to (or
6878 for a pointer type, shall not be the address of):
6879 * a subobject (4.5),
6880 * a temporary object (15.2),
6881 * a string literal (5.13.5),
6882 * the result of a typeid expression (8.2.8), or
6883 * a predefined __func__ variable (11.4.1). */
6884 else if (DECL_ARTIFICIAL (decl))
6886 if (complain & tf_error)
6887 error ("the address of %qD is not a valid template argument",
6888 decl);
6889 return NULL_TREE;
6891 else if (!same_type_ignoring_top_level_qualifiers_p
6892 (strip_array_types (TREE_TYPE (type)),
6893 strip_array_types (TREE_TYPE (decl))))
6895 if (complain & tf_error)
6896 error ("the address of the %qT subobject of %qD is not a "
6897 "valid template argument", TREE_TYPE (type), decl);
6898 return NULL_TREE;
6900 else if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
6902 if (complain & tf_error)
6903 error ("the address of %qD is not a valid template argument "
6904 "because it does not have static storage duration",
6905 decl);
6906 return NULL_TREE;
6910 expr = decayed;
6912 expr = perform_qualification_conversions (type, expr);
6913 if (expr == error_mark_node)
6914 return error_mark_node;
6916 /* [temp.arg.nontype]/5, bullet 3
6918 For a non-type template-parameter of type reference to object, no
6919 conversions apply. The type referred to by the reference may be more
6920 cv-qualified than the (otherwise identical) type of the
6921 template-argument. The template-parameter is bound directly to the
6922 template-argument, which must be an lvalue. */
6923 else if (TYPE_REF_OBJ_P (type))
6925 if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (type),
6926 expr_type))
6927 return error_mark_node;
6929 if (!at_least_as_qualified_p (TREE_TYPE (type), expr_type))
6931 if (complain & tf_error)
6932 error ("%qE is not a valid template argument for type %qT "
6933 "because of conflicts in cv-qualification", expr, type);
6934 return NULL_TREE;
6937 if (!lvalue_p (expr))
6939 if (complain & tf_error)
6940 error ("%qE is not a valid template argument for type %qT "
6941 "because it is not an lvalue", expr, type);
6942 return NULL_TREE;
6945 /* [temp.arg.nontype]/1
6947 A template-argument for a non-type, non-template template-parameter
6948 shall be one of: [...]
6950 -- the address of an object or function with external linkage. */
6951 if (INDIRECT_REF_P (expr)
6952 && TYPE_REF_OBJ_P (TREE_TYPE (TREE_OPERAND (expr, 0))))
6954 expr = TREE_OPERAND (expr, 0);
6955 if (DECL_P (expr))
6957 if (complain & tf_error)
6958 error ("%q#D is not a valid template argument for type %qT "
6959 "because a reference variable does not have a constant "
6960 "address", expr, type);
6961 return NULL_TREE;
6965 if (TYPE_REF_OBJ_P (TREE_TYPE (expr))
6966 && value_dependent_expression_p (expr))
6967 /* OK, dependent reference. We don't want to ask whether a DECL is
6968 itself value-dependent, since what we want here is its address. */;
6969 else
6971 if (!DECL_P (expr))
6973 if (complain & tf_error)
6974 error ("%qE is not a valid template argument for type %qT "
6975 "because it is not an object with linkage",
6976 expr, type);
6977 return NULL_TREE;
6980 /* DR 1155 allows internal linkage in C++11 and up. */
6981 linkage_kind linkage = decl_linkage (expr);
6982 if (linkage < (cxx_dialect >= cxx11 ? lk_internal : lk_external))
6984 if (complain & tf_error)
6985 error ("%qE is not a valid template argument for type %qT "
6986 "because object %qD does not have linkage",
6987 expr, type, expr);
6988 return NULL_TREE;
6991 expr = build_address (expr);
6994 if (!same_type_p (type, TREE_TYPE (expr)))
6995 expr = build_nop (type, expr);
6997 /* [temp.arg.nontype]/5, bullet 4
6999 For a non-type template-parameter of type pointer to function, only
7000 the function-to-pointer conversion (_conv.func_) is applied. If the
7001 template-argument represents a set of overloaded functions (or a
7002 pointer to such), the matching function is selected from the set
7003 (_over.over_). */
7004 else if (TYPE_PTRFN_P (type))
7006 /* If the argument is a template-id, we might not have enough
7007 context information to decay the pointer. */
7008 if (!type_unknown_p (expr_type))
7010 expr = decay_conversion (expr, complain);
7011 if (expr == error_mark_node)
7012 return error_mark_node;
7015 if (cxx_dialect >= cxx11 && integer_zerop (expr))
7016 /* Null pointer values are OK in C++11. */
7017 return perform_qualification_conversions (type, expr);
7019 expr = convert_nontype_argument_function (type, expr, complain);
7020 if (!expr || expr == error_mark_node)
7021 return expr;
7023 /* [temp.arg.nontype]/5, bullet 5
7025 For a non-type template-parameter of type reference to function, no
7026 conversions apply. If the template-argument represents a set of
7027 overloaded functions, the matching function is selected from the set
7028 (_over.over_). */
7029 else if (TYPE_REFFN_P (type))
7031 if (TREE_CODE (expr) == ADDR_EXPR)
7033 if (complain & tf_error)
7035 error ("%qE is not a valid template argument for type %qT "
7036 "because it is a pointer", expr, type);
7037 inform (input_location, "try using %qE instead",
7038 TREE_OPERAND (expr, 0));
7040 return NULL_TREE;
7043 expr = convert_nontype_argument_function (type, expr, complain);
7044 if (!expr || expr == error_mark_node)
7045 return expr;
7047 /* [temp.arg.nontype]/5, bullet 6
7049 For a non-type template-parameter of type pointer to member function,
7050 no conversions apply. If the template-argument represents a set of
7051 overloaded member functions, the matching member function is selected
7052 from the set (_over.over_). */
7053 else if (TYPE_PTRMEMFUNC_P (type))
7055 expr = instantiate_type (type, expr, tf_none);
7056 if (expr == error_mark_node)
7057 return error_mark_node;
7059 /* [temp.arg.nontype] bullet 1 says the pointer to member
7060 expression must be a pointer-to-member constant. */
7061 if (!value_dependent_expression_p (expr)
7062 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7063 return NULL_TREE;
7065 /* Repeated conversion can't deal with a conversion that turns PTRMEM_CST
7066 into a CONSTRUCTOR, so build up a new PTRMEM_CST instead. */
7067 if (fnptr_conv_p (type, TREE_TYPE (expr)))
7068 expr = make_ptrmem_cst (type, PTRMEM_CST_MEMBER (expr));
7070 /* [temp.arg.nontype]/5, bullet 7
7072 For a non-type template-parameter of type pointer to data member,
7073 qualification conversions (_conv.qual_) are applied. */
7074 else if (TYPE_PTRDATAMEM_P (type))
7076 /* [temp.arg.nontype] bullet 1 says the pointer to member
7077 expression must be a pointer-to-member constant. */
7078 if (!value_dependent_expression_p (expr)
7079 && !check_valid_ptrmem_cst_expr (type, expr, complain))
7080 return NULL_TREE;
7082 expr = perform_qualification_conversions (type, expr);
7083 if (expr == error_mark_node)
7084 return expr;
7086 else if (NULLPTR_TYPE_P (type))
7088 if (!NULLPTR_TYPE_P (TREE_TYPE (expr)))
7090 if (complain & tf_error)
7091 error ("%qE is not a valid template argument for type %qT "
7092 "because it is of type %qT", expr, type, TREE_TYPE (expr));
7093 return NULL_TREE;
7095 return expr;
7097 /* A template non-type parameter must be one of the above. */
7098 else
7099 gcc_unreachable ();
7101 /* Sanity check: did we actually convert the argument to the
7102 right type? */
7103 gcc_assert (same_type_ignoring_top_level_qualifiers_p
7104 (type, TREE_TYPE (expr)));
7105 return convert_from_reference (expr);
7108 /* Subroutine of coerce_template_template_parms, which returns 1 if
7109 PARM_PARM and ARG_PARM match using the rule for the template
7110 parameters of template template parameters. Both PARM and ARG are
7111 template parameters; the rest of the arguments are the same as for
7112 coerce_template_template_parms.
7114 static int
7115 coerce_template_template_parm (tree parm,
7116 tree arg,
7117 tsubst_flags_t complain,
7118 tree in_decl,
7119 tree outer_args)
7121 if (arg == NULL_TREE || error_operand_p (arg)
7122 || parm == NULL_TREE || error_operand_p (parm))
7123 return 0;
7125 if (TREE_CODE (arg) != TREE_CODE (parm))
7126 return 0;
7128 switch (TREE_CODE (parm))
7130 case TEMPLATE_DECL:
7131 /* We encounter instantiations of templates like
7132 template <template <template <class> class> class TT>
7133 class C; */
7135 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7136 tree argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7138 if (!coerce_template_template_parms
7139 (parmparm, argparm, complain, in_decl, outer_args))
7140 return 0;
7142 /* Fall through. */
7144 case TYPE_DECL:
7145 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (arg))
7146 && !TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7147 /* Argument is a parameter pack but parameter is not. */
7148 return 0;
7149 break;
7151 case PARM_DECL:
7152 /* The tsubst call is used to handle cases such as
7154 template <int> class C {};
7155 template <class T, template <T> class TT> class D {};
7156 D<int, C> d;
7158 i.e. the parameter list of TT depends on earlier parameters. */
7159 if (!uses_template_parms (TREE_TYPE (arg)))
7161 tree t = tsubst (TREE_TYPE (parm), outer_args, complain, in_decl);
7162 if (!uses_template_parms (t)
7163 && !same_type_p (t, TREE_TYPE (arg)))
7164 return 0;
7167 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (arg))
7168 && !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7169 /* Argument is a parameter pack but parameter is not. */
7170 return 0;
7172 break;
7174 default:
7175 gcc_unreachable ();
7178 return 1;
7181 /* Coerce template argument list ARGLIST for use with template
7182 template-parameter TEMPL. */
7184 static tree
7185 coerce_template_args_for_ttp (tree templ, tree arglist,
7186 tsubst_flags_t complain)
7188 /* Consider an example where a template template parameter declared as
7190 template <class T, class U = std::allocator<T> > class TT
7192 The template parameter level of T and U are one level larger than
7193 of TT. To proper process the default argument of U, say when an
7194 instantiation `TT<int>' is seen, we need to build the full
7195 arguments containing {int} as the innermost level. Outer levels,
7196 available when not appearing as default template argument, can be
7197 obtained from the arguments of the enclosing template.
7199 Suppose that TT is later substituted with std::vector. The above
7200 instantiation is `TT<int, std::allocator<T> >' with TT at
7201 level 1, and T at level 2, while the template arguments at level 1
7202 becomes {std::vector} and the inner level 2 is {int}. */
7204 tree outer = DECL_CONTEXT (templ);
7205 if (outer)
7207 if (DECL_TEMPLATE_SPECIALIZATION (outer))
7208 /* We want arguments for the partial specialization, not arguments for
7209 the primary template. */
7210 outer = template_parms_to_args (DECL_TEMPLATE_PARMS (outer));
7211 else
7212 outer = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (outer)));
7214 else if (current_template_parms)
7216 /* This is an argument of the current template, so we haven't set
7217 DECL_CONTEXT yet. */
7218 tree relevant_template_parms;
7220 /* Parameter levels that are greater than the level of the given
7221 template template parm are irrelevant. */
7222 relevant_template_parms = current_template_parms;
7223 while (TMPL_PARMS_DEPTH (relevant_template_parms)
7224 != TEMPLATE_TYPE_LEVEL (TREE_TYPE (templ)))
7225 relevant_template_parms = TREE_CHAIN (relevant_template_parms);
7227 outer = template_parms_to_args (relevant_template_parms);
7230 if (outer)
7231 arglist = add_to_template_args (outer, arglist);
7233 tree parmlist = DECL_INNERMOST_TEMPLATE_PARMS (templ);
7234 return coerce_template_parms (parmlist, arglist, templ,
7235 complain,
7236 /*require_all_args=*/true,
7237 /*use_default_args=*/true);
7240 /* A cache of template template parameters with match-all default
7241 arguments. */
7242 static GTY((deletable)) hash_map<tree,tree> *defaulted_ttp_cache;
7243 static void
7244 store_defaulted_ttp (tree v, tree t)
7246 if (!defaulted_ttp_cache)
7247 defaulted_ttp_cache = hash_map<tree,tree>::create_ggc (13);
7248 defaulted_ttp_cache->put (v, t);
7250 static tree
7251 lookup_defaulted_ttp (tree v)
7253 if (defaulted_ttp_cache)
7254 if (tree *p = defaulted_ttp_cache->get (v))
7255 return *p;
7256 return NULL_TREE;
7259 /* T is a bound template template-parameter. Copy its arguments into default
7260 arguments of the template template-parameter's template parameters. */
7262 static tree
7263 add_defaults_to_ttp (tree otmpl)
7265 if (tree c = lookup_defaulted_ttp (otmpl))
7266 return c;
7268 tree ntmpl = copy_node (otmpl);
7270 tree ntype = copy_node (TREE_TYPE (otmpl));
7271 TYPE_STUB_DECL (ntype) = TYPE_NAME (ntype) = ntmpl;
7272 TYPE_MAIN_VARIANT (ntype) = ntype;
7273 TYPE_POINTER_TO (ntype) = TYPE_REFERENCE_TO (ntype) = NULL_TREE;
7274 TYPE_NAME (ntype) = ntmpl;
7275 SET_TYPE_STRUCTURAL_EQUALITY (ntype);
7277 tree idx = TEMPLATE_TYPE_PARM_INDEX (ntype)
7278 = copy_node (TEMPLATE_TYPE_PARM_INDEX (ntype));
7279 TEMPLATE_PARM_DECL (idx) = ntmpl;
7280 TREE_TYPE (ntmpl) = TREE_TYPE (idx) = ntype;
7282 tree oparms = DECL_TEMPLATE_PARMS (otmpl);
7283 tree parms = DECL_TEMPLATE_PARMS (ntmpl) = copy_node (oparms);
7284 TREE_CHAIN (parms) = TREE_CHAIN (oparms);
7285 tree vec = TREE_VALUE (parms) = copy_node (TREE_VALUE (parms));
7286 for (int i = 0; i < TREE_VEC_LENGTH (vec); ++i)
7288 tree o = TREE_VEC_ELT (vec, i);
7289 if (!template_parameter_pack_p (TREE_VALUE (o)))
7291 tree n = TREE_VEC_ELT (vec, i) = copy_node (o);
7292 TREE_PURPOSE (n) = any_targ_node;
7296 store_defaulted_ttp (otmpl, ntmpl);
7297 return ntmpl;
7300 /* ARG is a bound potential template template-argument, and PARGS is a list
7301 of arguments for the corresponding template template-parameter. Adjust
7302 PARGS as appropriate for application to ARG's template, and if ARG is a
7303 BOUND_TEMPLATE_TEMPLATE_PARM, possibly adjust it to add default template
7304 arguments to the template template parameter. */
7306 static tree
7307 coerce_ttp_args_for_tta (tree& arg, tree pargs, tsubst_flags_t complain)
7309 ++processing_template_decl;
7310 tree arg_tmpl = TYPE_TI_TEMPLATE (arg);
7311 if (DECL_TEMPLATE_TEMPLATE_PARM_P (arg_tmpl))
7313 /* When comparing two template template-parameters in partial ordering,
7314 rewrite the one currently being used as an argument to have default
7315 arguments for all parameters. */
7316 arg_tmpl = add_defaults_to_ttp (arg_tmpl);
7317 pargs = coerce_template_args_for_ttp (arg_tmpl, pargs, complain);
7318 if (pargs != error_mark_node)
7319 arg = bind_template_template_parm (TREE_TYPE (arg_tmpl),
7320 TYPE_TI_ARGS (arg));
7322 else
7324 tree aparms
7325 = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (arg_tmpl));
7326 pargs = coerce_template_parms (aparms, pargs, arg_tmpl, complain,
7327 /*require_all*/true,
7328 /*use_default*/true);
7330 --processing_template_decl;
7331 return pargs;
7334 /* Subroutine of unify for the case when PARM is a
7335 BOUND_TEMPLATE_TEMPLATE_PARM. */
7337 static int
7338 unify_bound_ttp_args (tree tparms, tree targs, tree parm, tree& arg,
7339 bool explain_p)
7341 tree parmvec = TYPE_TI_ARGS (parm);
7342 tree argvec = INNERMOST_TEMPLATE_ARGS (TYPE_TI_ARGS (arg));
7344 /* The template template parm might be variadic and the argument
7345 not, so flatten both argument lists. */
7346 parmvec = expand_template_argument_pack (parmvec);
7347 argvec = expand_template_argument_pack (argvec);
7349 if (flag_new_ttp)
7351 /* In keeping with P0522R0, adjust P's template arguments
7352 to apply to A's template; then flatten it again. */
7353 tree nparmvec = parmvec;
7354 nparmvec = coerce_ttp_args_for_tta (arg, parmvec, tf_none);
7355 nparmvec = expand_template_argument_pack (nparmvec);
7357 if (unify (tparms, targs, nparmvec, argvec,
7358 UNIFY_ALLOW_NONE, explain_p))
7359 return 1;
7361 /* If the P0522 adjustment eliminated a pack expansion, deduce
7362 empty packs. */
7363 if (flag_new_ttp
7364 && TREE_VEC_LENGTH (nparmvec) < TREE_VEC_LENGTH (parmvec)
7365 && unify_pack_expansion (tparms, targs, parmvec, argvec,
7366 DEDUCE_EXACT, /*sub*/true, explain_p))
7367 return 1;
7369 else
7371 /* Deduce arguments T, i from TT<T> or TT<i>.
7372 We check each element of PARMVEC and ARGVEC individually
7373 rather than the whole TREE_VEC since they can have
7374 different number of elements, which is allowed under N2555. */
7376 int len = TREE_VEC_LENGTH (parmvec);
7378 /* Check if the parameters end in a pack, making them
7379 variadic. */
7380 int parm_variadic_p = 0;
7381 if (len > 0
7382 && PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, len - 1)))
7383 parm_variadic_p = 1;
7385 for (int i = 0; i < len - parm_variadic_p; ++i)
7386 /* If the template argument list of P contains a pack
7387 expansion that is not the last template argument, the
7388 entire template argument list is a non-deduced
7389 context. */
7390 if (PACK_EXPANSION_P (TREE_VEC_ELT (parmvec, i)))
7391 return unify_success (explain_p);
7393 if (TREE_VEC_LENGTH (argvec) < len - parm_variadic_p)
7394 return unify_too_few_arguments (explain_p,
7395 TREE_VEC_LENGTH (argvec), len);
7397 for (int i = 0; i < len - parm_variadic_p; ++i)
7398 if (unify (tparms, targs,
7399 TREE_VEC_ELT (parmvec, i),
7400 TREE_VEC_ELT (argvec, i),
7401 UNIFY_ALLOW_NONE, explain_p))
7402 return 1;
7404 if (parm_variadic_p
7405 && unify_pack_expansion (tparms, targs,
7406 parmvec, argvec,
7407 DEDUCE_EXACT,
7408 /*subr=*/true, explain_p))
7409 return 1;
7412 return 0;
7415 /* Return 1 if PARM_PARMS and ARG_PARMS matches using rule for
7416 template template parameters. Both PARM_PARMS and ARG_PARMS are
7417 vectors of TREE_LIST nodes containing TYPE_DECL, TEMPLATE_DECL
7418 or PARM_DECL.
7420 Consider the example:
7421 template <class T> class A;
7422 template<template <class U> class TT> class B;
7424 For B<A>, PARM_PARMS are the parameters to TT, while ARG_PARMS are
7425 the parameters to A, and OUTER_ARGS contains A. */
7427 static int
7428 coerce_template_template_parms (tree parm_parms,
7429 tree arg_parms,
7430 tsubst_flags_t complain,
7431 tree in_decl,
7432 tree outer_args)
7434 int nparms, nargs, i;
7435 tree parm, arg;
7436 int variadic_p = 0;
7438 gcc_assert (TREE_CODE (parm_parms) == TREE_VEC);
7439 gcc_assert (TREE_CODE (arg_parms) == TREE_VEC);
7441 nparms = TREE_VEC_LENGTH (parm_parms);
7442 nargs = TREE_VEC_LENGTH (arg_parms);
7444 if (flag_new_ttp)
7446 /* P0522R0: A template template-parameter P is at least as specialized as
7447 a template template-argument A if, given the following rewrite to two
7448 function templates, the function template corresponding to P is at
7449 least as specialized as the function template corresponding to A
7450 according to the partial ordering rules for function templates
7451 ([temp.func.order]). Given an invented class template X with the
7452 template parameter list of A (including default arguments):
7454 * Each of the two function templates has the same template parameters,
7455 respectively, as P or A.
7457 * Each function template has a single function parameter whose type is
7458 a specialization of X with template arguments corresponding to the
7459 template parameters from the respective function template where, for
7460 each template parameter PP in the template parameter list of the
7461 function template, a corresponding template argument AA is formed. If
7462 PP declares a parameter pack, then AA is the pack expansion
7463 PP... ([temp.variadic]); otherwise, AA is the id-expression PP.
7465 If the rewrite produces an invalid type, then P is not at least as
7466 specialized as A. */
7468 /* So coerce P's args to apply to A's parms, and then deduce between A's
7469 args and the converted args. If that succeeds, A is at least as
7470 specialized as P, so they match.*/
7471 tree pargs = template_parms_level_to_args (parm_parms);
7472 ++processing_template_decl;
7473 pargs = coerce_template_parms (arg_parms, pargs, NULL_TREE, tf_none,
7474 /*require_all*/true, /*use_default*/true);
7475 --processing_template_decl;
7476 if (pargs != error_mark_node)
7478 tree targs = make_tree_vec (nargs);
7479 tree aargs = template_parms_level_to_args (arg_parms);
7480 if (!unify (arg_parms, targs, aargs, pargs, UNIFY_ALLOW_NONE,
7481 /*explain*/false))
7482 return 1;
7486 /* Determine whether we have a parameter pack at the end of the
7487 template template parameter's template parameter list. */
7488 if (TREE_VEC_ELT (parm_parms, nparms - 1) != error_mark_node)
7490 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, nparms - 1));
7492 if (error_operand_p (parm))
7493 return 0;
7495 switch (TREE_CODE (parm))
7497 case TEMPLATE_DECL:
7498 case TYPE_DECL:
7499 if (TEMPLATE_TYPE_PARAMETER_PACK (TREE_TYPE (parm)))
7500 variadic_p = 1;
7501 break;
7503 case PARM_DECL:
7504 if (TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
7505 variadic_p = 1;
7506 break;
7508 default:
7509 gcc_unreachable ();
7513 if (nargs != nparms
7514 && !(variadic_p && nargs >= nparms - 1))
7515 return 0;
7517 /* Check all of the template parameters except the parameter pack at
7518 the end (if any). */
7519 for (i = 0; i < nparms - variadic_p; ++i)
7521 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node
7522 || TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7523 continue;
7525 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7526 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7528 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7529 outer_args))
7530 return 0;
7534 if (variadic_p)
7536 /* Check each of the template parameters in the template
7537 argument against the template parameter pack at the end of
7538 the template template parameter. */
7539 if (TREE_VEC_ELT (parm_parms, i) == error_mark_node)
7540 return 0;
7542 parm = TREE_VALUE (TREE_VEC_ELT (parm_parms, i));
7544 for (; i < nargs; ++i)
7546 if (TREE_VEC_ELT (arg_parms, i) == error_mark_node)
7547 continue;
7549 arg = TREE_VALUE (TREE_VEC_ELT (arg_parms, i));
7551 if (!coerce_template_template_parm (parm, arg, complain, in_decl,
7552 outer_args))
7553 return 0;
7557 return 1;
7560 /* Verifies that the deduced template arguments (in TARGS) for the
7561 template template parameters (in TPARMS) represent valid bindings,
7562 by comparing the template parameter list of each template argument
7563 to the template parameter list of its corresponding template
7564 template parameter, in accordance with DR150. This
7565 routine can only be called after all template arguments have been
7566 deduced. It will return TRUE if all of the template template
7567 parameter bindings are okay, FALSE otherwise. */
7568 bool
7569 template_template_parm_bindings_ok_p (tree tparms, tree targs)
7571 int i, ntparms = TREE_VEC_LENGTH (tparms);
7572 bool ret = true;
7574 /* We're dealing with template parms in this process. */
7575 ++processing_template_decl;
7577 targs = INNERMOST_TEMPLATE_ARGS (targs);
7579 for (i = 0; i < ntparms; ++i)
7581 tree tparm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
7582 tree targ = TREE_VEC_ELT (targs, i);
7584 if (TREE_CODE (tparm) == TEMPLATE_DECL && targ)
7586 tree packed_args = NULL_TREE;
7587 int idx, len = 1;
7589 if (ARGUMENT_PACK_P (targ))
7591 /* Look inside the argument pack. */
7592 packed_args = ARGUMENT_PACK_ARGS (targ);
7593 len = TREE_VEC_LENGTH (packed_args);
7596 for (idx = 0; idx < len; ++idx)
7598 tree targ_parms = NULL_TREE;
7600 if (packed_args)
7601 /* Extract the next argument from the argument
7602 pack. */
7603 targ = TREE_VEC_ELT (packed_args, idx);
7605 if (PACK_EXPANSION_P (targ))
7606 /* Look at the pattern of the pack expansion. */
7607 targ = PACK_EXPANSION_PATTERN (targ);
7609 /* Extract the template parameters from the template
7610 argument. */
7611 if (TREE_CODE (targ) == TEMPLATE_DECL)
7612 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (targ);
7613 else if (TREE_CODE (targ) == TEMPLATE_TEMPLATE_PARM)
7614 targ_parms = DECL_INNERMOST_TEMPLATE_PARMS (TYPE_NAME (targ));
7616 /* Verify that we can coerce the template template
7617 parameters from the template argument to the template
7618 parameter. This requires an exact match. */
7619 if (targ_parms
7620 && !coerce_template_template_parms
7621 (DECL_INNERMOST_TEMPLATE_PARMS (tparm),
7622 targ_parms,
7623 tf_none,
7624 tparm,
7625 targs))
7627 ret = false;
7628 goto out;
7634 out:
7636 --processing_template_decl;
7637 return ret;
7640 /* Since type attributes aren't mangled, we need to strip them from
7641 template type arguments. */
7643 static tree
7644 canonicalize_type_argument (tree arg, tsubst_flags_t complain)
7646 if (!arg || arg == error_mark_node || arg == TYPE_CANONICAL (arg))
7647 return arg;
7648 bool removed_attributes = false;
7649 tree canon = strip_typedefs (arg, &removed_attributes);
7650 if (removed_attributes
7651 && (complain & tf_warning))
7652 warning (OPT_Wignored_attributes,
7653 "ignoring attributes on template argument %qT", arg);
7654 return canon;
7657 /* And from inside dependent non-type arguments like sizeof(Type). */
7659 static tree
7660 canonicalize_expr_argument (tree arg, tsubst_flags_t complain)
7662 if (!arg || arg == error_mark_node)
7663 return arg;
7664 bool removed_attributes = false;
7665 tree canon = strip_typedefs_expr (arg, &removed_attributes);
7666 if (removed_attributes
7667 && (complain & tf_warning))
7668 warning (OPT_Wignored_attributes,
7669 "ignoring attributes in template argument %qE", arg);
7670 return canon;
7673 // A template declaration can be substituted for a constrained
7674 // template template parameter only when the argument is more
7675 // constrained than the parameter.
7676 static bool
7677 is_compatible_template_arg (tree parm, tree arg)
7679 tree parm_cons = get_constraints (parm);
7681 /* For now, allow constrained template template arguments
7682 and unconstrained template template parameters. */
7683 if (parm_cons == NULL_TREE)
7684 return true;
7686 tree arg_cons = get_constraints (arg);
7688 // If the template parameter is constrained, we need to rewrite its
7689 // constraints in terms of the ARG's template parameters. This ensures
7690 // that all of the template parameter types will have the same depth.
7692 // Note that this is only valid when coerce_template_template_parm is
7693 // true for the innermost template parameters of PARM and ARG. In other
7694 // words, because coercion is successful, this conversion will be valid.
7695 if (parm_cons)
7697 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (arg));
7698 parm_cons = tsubst_constraint_info (parm_cons,
7699 INNERMOST_TEMPLATE_ARGS (args),
7700 tf_none, NULL_TREE);
7701 if (parm_cons == error_mark_node)
7702 return false;
7705 return subsumes (parm_cons, arg_cons);
7708 // Convert a placeholder argument into a binding to the original
7709 // parameter. The original parameter is saved as the TREE_TYPE of
7710 // ARG.
7711 static inline tree
7712 convert_wildcard_argument (tree parm, tree arg)
7714 TREE_TYPE (arg) = parm;
7715 return arg;
7718 /* We can't fully resolve ARG given as a non-type template argument to TYPE,
7719 because one of them is dependent. But we need to represent the
7720 conversion for the benefit of cp_tree_equal. */
7722 static tree
7723 maybe_convert_nontype_argument (tree type, tree arg)
7725 /* Auto parms get no conversion. */
7726 if (type_uses_auto (type))
7727 return arg;
7728 /* We don't need or want to add this conversion now if we're going to use the
7729 argument for deduction. */
7730 if (value_dependent_expression_p (arg))
7731 return arg;
7733 type = cv_unqualified (type);
7734 tree argtype = TREE_TYPE (arg);
7735 if (same_type_p (type, argtype))
7736 return arg;
7738 arg = build1 (IMPLICIT_CONV_EXPR, type, arg);
7739 IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true;
7740 return arg;
7743 /* Convert the indicated template ARG as necessary to match the
7744 indicated template PARM. Returns the converted ARG, or
7745 error_mark_node if the conversion was unsuccessful. Error and
7746 warning messages are issued under control of COMPLAIN. This
7747 conversion is for the Ith parameter in the parameter list. ARGS is
7748 the full set of template arguments deduced so far. */
7750 static tree
7751 convert_template_argument (tree parm,
7752 tree arg,
7753 tree args,
7754 tsubst_flags_t complain,
7755 int i,
7756 tree in_decl)
7758 tree orig_arg;
7759 tree val;
7760 int is_type, requires_type, is_tmpl_type, requires_tmpl_type;
7762 if (parm == error_mark_node)
7763 return error_mark_node;
7765 /* Trivially convert placeholders. */
7766 if (TREE_CODE (arg) == WILDCARD_DECL)
7767 return convert_wildcard_argument (parm, arg);
7769 if (arg == any_targ_node)
7770 return arg;
7772 if (TREE_CODE (arg) == TREE_LIST
7773 && TREE_CODE (TREE_VALUE (arg)) == OFFSET_REF)
7775 /* The template argument was the name of some
7776 member function. That's usually
7777 invalid, but static members are OK. In any
7778 case, grab the underlying fields/functions
7779 and issue an error later if required. */
7780 orig_arg = TREE_VALUE (arg);
7781 TREE_TYPE (arg) = unknown_type_node;
7784 orig_arg = arg;
7786 requires_tmpl_type = TREE_CODE (parm) == TEMPLATE_DECL;
7787 requires_type = (TREE_CODE (parm) == TYPE_DECL
7788 || requires_tmpl_type);
7790 /* When determining whether an argument pack expansion is a template,
7791 look at the pattern. */
7792 if (TREE_CODE (arg) == TYPE_PACK_EXPANSION)
7793 arg = PACK_EXPANSION_PATTERN (arg);
7795 /* Deal with an injected-class-name used as a template template arg. */
7796 if (requires_tmpl_type && CLASS_TYPE_P (arg))
7798 tree t = maybe_get_template_decl_from_type_decl (TYPE_NAME (arg));
7799 if (TREE_CODE (t) == TEMPLATE_DECL)
7801 if (cxx_dialect >= cxx11)
7802 /* OK under DR 1004. */;
7803 else if (complain & tf_warning_or_error)
7804 pedwarn (input_location, OPT_Wpedantic, "injected-class-name %qD"
7805 " used as template template argument", TYPE_NAME (arg));
7806 else if (flag_pedantic_errors)
7807 t = arg;
7809 arg = t;
7813 is_tmpl_type =
7814 ((TREE_CODE (arg) == TEMPLATE_DECL
7815 && TREE_CODE (DECL_TEMPLATE_RESULT (arg)) == TYPE_DECL)
7816 || (requires_tmpl_type && TREE_CODE (arg) == TYPE_ARGUMENT_PACK)
7817 || TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7818 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
7820 if (is_tmpl_type
7821 && (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
7822 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE))
7823 arg = TYPE_STUB_DECL (arg);
7825 is_type = TYPE_P (arg) || is_tmpl_type;
7827 if (requires_type && ! is_type && TREE_CODE (arg) == SCOPE_REF
7828 && TREE_CODE (TREE_OPERAND (arg, 0)) == TEMPLATE_TYPE_PARM)
7830 if (TREE_CODE (TREE_OPERAND (arg, 1)) == BIT_NOT_EXPR)
7832 if (complain & tf_error)
7833 error ("invalid use of destructor %qE as a type", orig_arg);
7834 return error_mark_node;
7837 permerror (input_location,
7838 "to refer to a type member of a template parameter, "
7839 "use %<typename %E%>", orig_arg);
7841 orig_arg = make_typename_type (TREE_OPERAND (arg, 0),
7842 TREE_OPERAND (arg, 1),
7843 typename_type,
7844 complain);
7845 arg = orig_arg;
7846 is_type = 1;
7848 if (is_type != requires_type)
7850 if (in_decl)
7852 if (complain & tf_error)
7854 error ("type/value mismatch at argument %d in template "
7855 "parameter list for %qD",
7856 i + 1, in_decl);
7857 if (is_type)
7858 inform (input_location,
7859 " expected a constant of type %qT, got %qT",
7860 TREE_TYPE (parm),
7861 (DECL_P (arg) ? DECL_NAME (arg) : orig_arg));
7862 else if (requires_tmpl_type)
7863 inform (input_location,
7864 " expected a class template, got %qE", orig_arg);
7865 else
7866 inform (input_location,
7867 " expected a type, got %qE", orig_arg);
7870 return error_mark_node;
7872 if (is_tmpl_type ^ requires_tmpl_type)
7874 if (in_decl && (complain & tf_error))
7876 error ("type/value mismatch at argument %d in template "
7877 "parameter list for %qD",
7878 i + 1, in_decl);
7879 if (is_tmpl_type)
7880 inform (input_location,
7881 " expected a type, got %qT", DECL_NAME (arg));
7882 else
7883 inform (input_location,
7884 " expected a class template, got %qT", orig_arg);
7886 return error_mark_node;
7889 if (template_parameter_pack_p (parm) && ARGUMENT_PACK_P (orig_arg))
7890 /* We already did the appropriate conversion when packing args. */
7891 val = orig_arg;
7892 else if (is_type)
7894 if (requires_tmpl_type)
7896 if (TREE_CODE (TREE_TYPE (arg)) == UNBOUND_CLASS_TEMPLATE)
7897 /* The number of argument required is not known yet.
7898 Just accept it for now. */
7899 val = orig_arg;
7900 else
7902 tree parmparm = DECL_INNERMOST_TEMPLATE_PARMS (parm);
7903 tree argparm;
7905 /* Strip alias templates that are equivalent to another
7906 template. */
7907 arg = get_underlying_template (arg);
7908 argparm = DECL_INNERMOST_TEMPLATE_PARMS (arg);
7910 if (coerce_template_template_parms (parmparm, argparm,
7911 complain, in_decl,
7912 args))
7914 val = arg;
7916 /* TEMPLATE_TEMPLATE_PARM node is preferred over
7917 TEMPLATE_DECL. */
7918 if (val != error_mark_node)
7920 if (DECL_TEMPLATE_TEMPLATE_PARM_P (val))
7921 val = TREE_TYPE (val);
7922 if (TREE_CODE (orig_arg) == TYPE_PACK_EXPANSION)
7923 val = make_pack_expansion (val, complain);
7926 else
7928 if (in_decl && (complain & tf_error))
7930 error ("type/value mismatch at argument %d in "
7931 "template parameter list for %qD",
7932 i + 1, in_decl);
7933 inform (input_location,
7934 " expected a template of type %qD, got %qT",
7935 parm, orig_arg);
7938 val = error_mark_node;
7941 // Check that the constraints are compatible before allowing the
7942 // substitution.
7943 if (val != error_mark_node)
7944 if (!is_compatible_template_arg (parm, arg))
7946 if (in_decl && (complain & tf_error))
7948 error ("constraint mismatch at argument %d in "
7949 "template parameter list for %qD",
7950 i + 1, in_decl);
7951 inform (input_location, " expected %qD but got %qD",
7952 parm, arg);
7954 val = error_mark_node;
7958 else
7959 val = orig_arg;
7960 /* We only form one instance of each template specialization.
7961 Therefore, if we use a non-canonical variant (i.e., a
7962 typedef), any future messages referring to the type will use
7963 the typedef, which is confusing if those future uses do not
7964 themselves also use the typedef. */
7965 if (TYPE_P (val))
7966 val = canonicalize_type_argument (val, complain);
7968 else
7970 tree t = TREE_TYPE (parm);
7972 if (TEMPLATE_PARM_LEVEL (get_template_parm_index (parm))
7973 > TMPL_ARGS_DEPTH (args))
7974 /* We don't have enough levels of args to do any substitution. This
7975 can happen in the context of -fnew-ttp-matching. */;
7976 else if (tree a = type_uses_auto (t))
7978 t = do_auto_deduction (t, arg, a, complain, adc_unify, args);
7979 if (t == error_mark_node)
7980 return error_mark_node;
7982 else
7983 t = tsubst (t, args, complain, in_decl);
7985 if (invalid_nontype_parm_type_p (t, complain))
7986 return error_mark_node;
7988 if (!type_dependent_expression_p (orig_arg)
7989 && !uses_template_parms (t))
7990 /* We used to call digest_init here. However, digest_init
7991 will report errors, which we don't want when complain
7992 is zero. More importantly, digest_init will try too
7993 hard to convert things: for example, `0' should not be
7994 converted to pointer type at this point according to
7995 the standard. Accepting this is not merely an
7996 extension, since deciding whether or not these
7997 conversions can occur is part of determining which
7998 function template to call, or whether a given explicit
7999 argument specification is valid. */
8000 val = convert_nontype_argument (t, orig_arg, complain);
8001 else
8003 val = canonicalize_expr_argument (orig_arg, complain);
8004 val = maybe_convert_nontype_argument (t, val);
8008 if (val == NULL_TREE)
8009 val = error_mark_node;
8010 else if (val == error_mark_node && (complain & tf_error))
8011 error ("could not convert template argument %qE from %qT to %qT",
8012 orig_arg, TREE_TYPE (orig_arg), t);
8014 if (INDIRECT_REF_P (val))
8016 /* Reject template arguments that are references to built-in
8017 functions with no library fallbacks. */
8018 const_tree inner = TREE_OPERAND (val, 0);
8019 const_tree innertype = TREE_TYPE (inner);
8020 if (innertype
8021 && TREE_CODE (innertype) == REFERENCE_TYPE
8022 && TREE_CODE (TREE_TYPE (innertype)) == FUNCTION_TYPE
8023 && TREE_OPERAND_LENGTH (inner) > 0
8024 && reject_gcc_builtin (TREE_OPERAND (inner, 0)))
8025 return error_mark_node;
8028 if (TREE_CODE (val) == SCOPE_REF)
8030 /* Strip typedefs from the SCOPE_REF. */
8031 tree type = canonicalize_type_argument (TREE_TYPE (val), complain);
8032 tree scope = canonicalize_type_argument (TREE_OPERAND (val, 0),
8033 complain);
8034 val = build_qualified_name (type, scope, TREE_OPERAND (val, 1),
8035 QUALIFIED_NAME_IS_TEMPLATE (val));
8039 return val;
8042 /* Coerces the remaining template arguments in INNER_ARGS (from
8043 ARG_IDX to the end) into the parameter pack at PARM_IDX in PARMS.
8044 Returns the coerced argument pack. PARM_IDX is the position of this
8045 parameter in the template parameter list. ARGS is the original
8046 template argument list. */
8047 static tree
8048 coerce_template_parameter_pack (tree parms,
8049 int parm_idx,
8050 tree args,
8051 tree inner_args,
8052 int arg_idx,
8053 tree new_args,
8054 int* lost,
8055 tree in_decl,
8056 tsubst_flags_t complain)
8058 tree parm = TREE_VEC_ELT (parms, parm_idx);
8059 int nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8060 tree packed_args;
8061 tree argument_pack;
8062 tree packed_parms = NULL_TREE;
8064 if (arg_idx > nargs)
8065 arg_idx = nargs;
8067 if (tree packs = fixed_parameter_pack_p (TREE_VALUE (parm)))
8069 /* When the template parameter is a non-type template parameter pack
8070 or template template parameter pack whose type or template
8071 parameters use parameter packs, we know exactly how many arguments
8072 we are looking for. Build a vector of the instantiated decls for
8073 these template parameters in PACKED_PARMS. */
8074 /* We can't use make_pack_expansion here because it would interpret a
8075 _DECL as a use rather than a declaration. */
8076 tree decl = TREE_VALUE (parm);
8077 tree exp = cxx_make_type (TYPE_PACK_EXPANSION);
8078 SET_PACK_EXPANSION_PATTERN (exp, decl);
8079 PACK_EXPANSION_PARAMETER_PACKS (exp) = packs;
8080 SET_TYPE_STRUCTURAL_EQUALITY (exp);
8082 TREE_VEC_LENGTH (args)--;
8083 packed_parms = tsubst_pack_expansion (exp, args, complain, decl);
8084 TREE_VEC_LENGTH (args)++;
8086 if (packed_parms == error_mark_node)
8087 return error_mark_node;
8089 /* If we're doing a partial instantiation of a member template,
8090 verify that all of the types used for the non-type
8091 template parameter pack are, in fact, valid for non-type
8092 template parameters. */
8093 if (arg_idx < nargs
8094 && PACK_EXPANSION_P (TREE_VEC_ELT (inner_args, arg_idx)))
8096 int j, len = TREE_VEC_LENGTH (packed_parms);
8097 for (j = 0; j < len; ++j)
8099 tree t = TREE_TYPE (TREE_VEC_ELT (packed_parms, j));
8100 if (invalid_nontype_parm_type_p (t, complain))
8101 return error_mark_node;
8103 /* We don't know how many args we have yet, just
8104 use the unconverted ones for now. */
8105 return NULL_TREE;
8108 packed_args = make_tree_vec (TREE_VEC_LENGTH (packed_parms));
8110 /* Check if we have a placeholder pack, which indicates we're
8111 in the context of a introduction list. In that case we want
8112 to match this pack to the single placeholder. */
8113 else if (arg_idx < nargs
8114 && TREE_CODE (TREE_VEC_ELT (inner_args, arg_idx)) == WILDCARD_DECL
8115 && WILDCARD_PACK_P (TREE_VEC_ELT (inner_args, arg_idx)))
8117 nargs = arg_idx + 1;
8118 packed_args = make_tree_vec (1);
8120 else
8121 packed_args = make_tree_vec (nargs - arg_idx);
8123 /* Convert the remaining arguments, which will be a part of the
8124 parameter pack "parm". */
8125 int first_pack_arg = arg_idx;
8126 for (; arg_idx < nargs; ++arg_idx)
8128 tree arg = TREE_VEC_ELT (inner_args, arg_idx);
8129 tree actual_parm = TREE_VALUE (parm);
8130 int pack_idx = arg_idx - first_pack_arg;
8132 if (packed_parms)
8134 /* Once we've packed as many args as we have types, stop. */
8135 if (pack_idx >= TREE_VEC_LENGTH (packed_parms))
8136 break;
8137 else if (PACK_EXPANSION_P (arg))
8138 /* We don't know how many args we have yet, just
8139 use the unconverted ones for now. */
8140 return NULL_TREE;
8141 else
8142 actual_parm = TREE_VEC_ELT (packed_parms, pack_idx);
8145 if (arg == error_mark_node)
8147 if (complain & tf_error)
8148 error ("template argument %d is invalid", arg_idx + 1);
8150 else
8151 arg = convert_template_argument (actual_parm,
8152 arg, new_args, complain, parm_idx,
8153 in_decl);
8154 if (arg == error_mark_node)
8155 (*lost)++;
8156 TREE_VEC_ELT (packed_args, pack_idx) = arg;
8159 if (arg_idx - first_pack_arg < TREE_VEC_LENGTH (packed_args)
8160 && TREE_VEC_LENGTH (packed_args) > 0)
8162 if (complain & tf_error)
8163 error ("wrong number of template arguments (%d, should be %d)",
8164 arg_idx - first_pack_arg, TREE_VEC_LENGTH (packed_args));
8165 return error_mark_node;
8168 if (TREE_CODE (TREE_VALUE (parm)) == TYPE_DECL
8169 || TREE_CODE (TREE_VALUE (parm)) == TEMPLATE_DECL)
8170 argument_pack = cxx_make_type (TYPE_ARGUMENT_PACK);
8171 else
8173 argument_pack = make_node (NONTYPE_ARGUMENT_PACK);
8174 TREE_CONSTANT (argument_pack) = 1;
8177 SET_ARGUMENT_PACK_ARGS (argument_pack, packed_args);
8178 if (CHECKING_P)
8179 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (packed_args,
8180 TREE_VEC_LENGTH (packed_args));
8181 return argument_pack;
8184 /* Returns the number of pack expansions in the template argument vector
8185 ARGS. */
8187 static int
8188 pack_expansion_args_count (tree args)
8190 int i;
8191 int count = 0;
8192 if (args)
8193 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
8195 tree elt = TREE_VEC_ELT (args, i);
8196 if (elt && PACK_EXPANSION_P (elt))
8197 ++count;
8199 return count;
8202 /* Convert all template arguments to their appropriate types, and
8203 return a vector containing the innermost resulting template
8204 arguments. If any error occurs, return error_mark_node. Error and
8205 warning messages are issued under control of COMPLAIN.
8207 If REQUIRE_ALL_ARGS is false, argument deduction will be performed
8208 for arguments not specified in ARGS. Otherwise, if
8209 USE_DEFAULT_ARGS is true, default arguments will be used to fill in
8210 unspecified arguments. If REQUIRE_ALL_ARGS is true, but
8211 USE_DEFAULT_ARGS is false, then all arguments must be specified in
8212 ARGS. */
8214 static tree
8215 coerce_template_parms (tree parms,
8216 tree args,
8217 tree in_decl,
8218 tsubst_flags_t complain,
8219 bool require_all_args,
8220 bool use_default_args)
8222 int nparms, nargs, parm_idx, arg_idx, lost = 0;
8223 tree orig_inner_args;
8224 tree inner_args;
8225 tree new_args;
8226 tree new_inner_args;
8227 int saved_unevaluated_operand;
8228 int saved_inhibit_evaluation_warnings;
8230 /* When used as a boolean value, indicates whether this is a
8231 variadic template parameter list. Since it's an int, we can also
8232 subtract it from nparms to get the number of non-variadic
8233 parameters. */
8234 int variadic_p = 0;
8235 int variadic_args_p = 0;
8236 int post_variadic_parms = 0;
8238 /* Adjustment to nparms for fixed parameter packs. */
8239 int fixed_pack_adjust = 0;
8240 int fixed_packs = 0;
8241 int missing = 0;
8243 /* Likewise for parameters with default arguments. */
8244 int default_p = 0;
8246 if (args == error_mark_node)
8247 return error_mark_node;
8249 nparms = TREE_VEC_LENGTH (parms);
8251 /* Determine if there are any parameter packs or default arguments. */
8252 for (parm_idx = 0; parm_idx < nparms; ++parm_idx)
8254 tree parm = TREE_VEC_ELT (parms, parm_idx);
8255 if (variadic_p)
8256 ++post_variadic_parms;
8257 if (template_parameter_pack_p (TREE_VALUE (parm)))
8258 ++variadic_p;
8259 if (TREE_PURPOSE (parm))
8260 ++default_p;
8263 inner_args = orig_inner_args = INNERMOST_TEMPLATE_ARGS (args);
8264 /* If there are no parameters that follow a parameter pack, we need to
8265 expand any argument packs so that we can deduce a parameter pack from
8266 some non-packed args followed by an argument pack, as in variadic85.C.
8267 If there are such parameters, we need to leave argument packs intact
8268 so the arguments are assigned properly. This can happen when dealing
8269 with a nested class inside a partial specialization of a class
8270 template, as in variadic92.C, or when deducing a template parameter pack
8271 from a sub-declarator, as in variadic114.C. */
8272 if (!post_variadic_parms)
8273 inner_args = expand_template_argument_pack (inner_args);
8275 /* Count any pack expansion args. */
8276 variadic_args_p = pack_expansion_args_count (inner_args);
8278 nargs = inner_args ? NUM_TMPL_ARGS (inner_args) : 0;
8279 if ((nargs - variadic_args_p > nparms && !variadic_p)
8280 || (nargs < nparms - variadic_p
8281 && require_all_args
8282 && !variadic_args_p
8283 && (!use_default_args
8284 || (TREE_VEC_ELT (parms, nargs) != error_mark_node
8285 && !TREE_PURPOSE (TREE_VEC_ELT (parms, nargs))))))
8287 bad_nargs:
8288 if (complain & tf_error)
8290 if (variadic_p || default_p)
8292 nparms -= variadic_p + default_p;
8293 error ("wrong number of template arguments "
8294 "(%d, should be at least %d)", nargs, nparms);
8296 else
8297 error ("wrong number of template arguments "
8298 "(%d, should be %d)", nargs, nparms);
8300 if (in_decl)
8301 inform (DECL_SOURCE_LOCATION (in_decl),
8302 "provided for %qD", in_decl);
8305 return error_mark_node;
8307 /* We can't pass a pack expansion to a non-pack parameter of an alias
8308 template (DR 1430). */
8309 else if (in_decl
8310 && (DECL_ALIAS_TEMPLATE_P (in_decl)
8311 || concept_template_p (in_decl))
8312 && variadic_args_p
8313 && nargs - variadic_args_p < nparms - variadic_p)
8315 if (complain & tf_error)
8317 for (int i = 0; i < TREE_VEC_LENGTH (inner_args); ++i)
8319 tree arg = TREE_VEC_ELT (inner_args, i);
8320 tree parm = TREE_VALUE (TREE_VEC_ELT (parms, i));
8322 if (PACK_EXPANSION_P (arg)
8323 && !template_parameter_pack_p (parm))
8325 if (DECL_ALIAS_TEMPLATE_P (in_decl))
8326 error_at (location_of (arg),
8327 "pack expansion argument for non-pack parameter "
8328 "%qD of alias template %qD", parm, in_decl);
8329 else
8330 error_at (location_of (arg),
8331 "pack expansion argument for non-pack parameter "
8332 "%qD of concept %qD", parm, in_decl);
8333 inform (DECL_SOURCE_LOCATION (parm), "declared here");
8334 goto found;
8337 gcc_unreachable ();
8338 found:;
8340 return error_mark_node;
8343 /* We need to evaluate the template arguments, even though this
8344 template-id may be nested within a "sizeof". */
8345 saved_unevaluated_operand = cp_unevaluated_operand;
8346 cp_unevaluated_operand = 0;
8347 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
8348 c_inhibit_evaluation_warnings = 0;
8349 new_inner_args = make_tree_vec (nparms);
8350 new_args = add_outermost_template_args (args, new_inner_args);
8351 int pack_adjust = 0;
8352 for (parm_idx = 0, arg_idx = 0; parm_idx < nparms; parm_idx++, arg_idx++)
8354 tree arg;
8355 tree parm;
8357 /* Get the Ith template parameter. */
8358 parm = TREE_VEC_ELT (parms, parm_idx);
8360 if (parm == error_mark_node)
8362 TREE_VEC_ELT (new_inner_args, arg_idx) = error_mark_node;
8363 continue;
8366 /* Calculate the next argument. */
8367 if (arg_idx < nargs)
8368 arg = TREE_VEC_ELT (inner_args, arg_idx);
8369 else
8370 arg = NULL_TREE;
8372 if (template_parameter_pack_p (TREE_VALUE (parm))
8373 && !(arg && ARGUMENT_PACK_P (arg)))
8375 /* Some arguments will be placed in the
8376 template parameter pack PARM. */
8377 arg = coerce_template_parameter_pack (parms, parm_idx, args,
8378 inner_args, arg_idx,
8379 new_args, &lost,
8380 in_decl, complain);
8382 if (arg == NULL_TREE)
8384 /* We don't know how many args we have yet, just use the
8385 unconverted (and still packed) ones for now. */
8386 new_inner_args = orig_inner_args;
8387 arg_idx = nargs;
8388 break;
8391 TREE_VEC_ELT (new_inner_args, parm_idx) = arg;
8393 /* Store this argument. */
8394 if (arg == error_mark_node)
8396 lost++;
8397 /* We are done with all of the arguments. */
8398 arg_idx = nargs;
8399 break;
8401 else
8403 pack_adjust = TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg)) - 1;
8404 arg_idx += pack_adjust;
8405 if (fixed_parameter_pack_p (TREE_VALUE (parm)))
8407 ++fixed_packs;
8408 fixed_pack_adjust += pack_adjust;
8412 continue;
8414 else if (arg)
8416 if (PACK_EXPANSION_P (arg))
8418 /* "If every valid specialization of a variadic template
8419 requires an empty template parameter pack, the template is
8420 ill-formed, no diagnostic required." So check that the
8421 pattern works with this parameter. */
8422 tree pattern = PACK_EXPANSION_PATTERN (arg);
8423 tree conv = convert_template_argument (TREE_VALUE (parm),
8424 pattern, new_args,
8425 complain, parm_idx,
8426 in_decl);
8427 if (conv == error_mark_node)
8429 if (complain & tf_error)
8430 inform (input_location, "so any instantiation with a "
8431 "non-empty parameter pack would be ill-formed");
8432 ++lost;
8434 else if (TYPE_P (conv) && !TYPE_P (pattern))
8435 /* Recover from missing typename. */
8436 TREE_VEC_ELT (inner_args, arg_idx)
8437 = make_pack_expansion (conv, complain);
8439 /* We don't know how many args we have yet, just
8440 use the unconverted ones for now. */
8441 new_inner_args = inner_args;
8442 arg_idx = nargs;
8443 break;
8446 else if (require_all_args)
8448 /* There must be a default arg in this case. */
8449 arg = tsubst_template_arg (TREE_PURPOSE (parm), new_args,
8450 complain, in_decl);
8451 /* The position of the first default template argument,
8452 is also the number of non-defaulted arguments in NEW_INNER_ARGS.
8453 Record that. */
8454 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8455 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8456 arg_idx - pack_adjust);
8458 else
8459 break;
8461 if (arg == error_mark_node)
8463 if (complain & tf_error)
8464 error ("template argument %d is invalid", arg_idx + 1);
8466 else if (!arg)
8468 /* This can occur if there was an error in the template
8469 parameter list itself (which we would already have
8470 reported) that we are trying to recover from, e.g., a class
8471 template with a parameter list such as
8472 template<typename..., typename> (cpp0x/variadic150.C). */
8473 ++lost;
8475 /* This can also happen with a fixed parameter pack (71834). */
8476 if (arg_idx >= nargs)
8477 ++missing;
8479 else
8480 arg = convert_template_argument (TREE_VALUE (parm),
8481 arg, new_args, complain,
8482 parm_idx, in_decl);
8484 if (arg == error_mark_node)
8485 lost++;
8486 TREE_VEC_ELT (new_inner_args, arg_idx - pack_adjust) = arg;
8488 cp_unevaluated_operand = saved_unevaluated_operand;
8489 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
8491 if (missing || arg_idx < nargs - variadic_args_p)
8493 /* If we had fixed parameter packs, we didn't know how many arguments we
8494 actually needed earlier; now we do. */
8495 nparms += fixed_pack_adjust;
8496 variadic_p -= fixed_packs;
8497 goto bad_nargs;
8500 if (lost)
8502 gcc_assert (!(complain & tf_error) || seen_error ());
8503 return error_mark_node;
8506 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args))
8507 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
8508 TREE_VEC_LENGTH (new_inner_args));
8510 return new_inner_args;
8513 /* Convert all template arguments to their appropriate types, and
8514 return a vector containing the innermost resulting template
8515 arguments. If any error occurs, return error_mark_node. Error and
8516 warning messages are not issued.
8518 Note that no function argument deduction is performed, and default
8519 arguments are used to fill in unspecified arguments. */
8520 tree
8521 coerce_template_parms (tree parms, tree args, tree in_decl)
8523 return coerce_template_parms (parms, args, in_decl, tf_none, true, true);
8526 /* Convert all template arguments to their appropriate type, and
8527 instantiate default arguments as needed. This returns a vector
8528 containing the innermost resulting template arguments, or
8529 error_mark_node if unsuccessful. */
8530 tree
8531 coerce_template_parms (tree parms, tree args, tree in_decl,
8532 tsubst_flags_t complain)
8534 return coerce_template_parms (parms, args, in_decl, complain, true, true);
8537 /* Like coerce_template_parms. If PARMS represents all template
8538 parameters levels, this function returns a vector of vectors
8539 representing all the resulting argument levels. Note that in this
8540 case, only the innermost arguments are coerced because the
8541 outermost ones are supposed to have been coerced already.
8543 Otherwise, if PARMS represents only (the innermost) vector of
8544 parameters, this function returns a vector containing just the
8545 innermost resulting arguments. */
8547 static tree
8548 coerce_innermost_template_parms (tree parms,
8549 tree args,
8550 tree in_decl,
8551 tsubst_flags_t complain,
8552 bool require_all_args,
8553 bool use_default_args)
8555 int parms_depth = TMPL_PARMS_DEPTH (parms);
8556 int args_depth = TMPL_ARGS_DEPTH (args);
8557 tree coerced_args;
8559 if (parms_depth > 1)
8561 coerced_args = make_tree_vec (parms_depth);
8562 tree level;
8563 int cur_depth;
8565 for (level = parms, cur_depth = parms_depth;
8566 parms_depth > 0 && level != NULL_TREE;
8567 level = TREE_CHAIN (level), --cur_depth)
8569 tree l;
8570 if (cur_depth == args_depth)
8571 l = coerce_template_parms (TREE_VALUE (level),
8572 args, in_decl, complain,
8573 require_all_args,
8574 use_default_args);
8575 else
8576 l = TMPL_ARGS_LEVEL (args, cur_depth);
8578 if (l == error_mark_node)
8579 return error_mark_node;
8581 SET_TMPL_ARGS_LEVEL (coerced_args, cur_depth, l);
8584 else
8585 coerced_args = coerce_template_parms (INNERMOST_TEMPLATE_PARMS (parms),
8586 args, in_decl, complain,
8587 require_all_args,
8588 use_default_args);
8589 return coerced_args;
8592 /* Returns 1 if template args OT and NT are equivalent. */
8595 template_args_equal (tree ot, tree nt, bool partial_order /* = false */)
8597 if (nt == ot)
8598 return 1;
8599 if (nt == NULL_TREE || ot == NULL_TREE)
8600 return false;
8601 if (nt == any_targ_node || ot == any_targ_node)
8602 return true;
8604 if (TREE_CODE (nt) == TREE_VEC)
8605 /* For member templates */
8606 return TREE_CODE (ot) == TREE_VEC && comp_template_args (ot, nt);
8607 else if (PACK_EXPANSION_P (ot))
8608 return (PACK_EXPANSION_P (nt)
8609 && template_args_equal (PACK_EXPANSION_PATTERN (ot),
8610 PACK_EXPANSION_PATTERN (nt))
8611 && template_args_equal (PACK_EXPANSION_EXTRA_ARGS (ot),
8612 PACK_EXPANSION_EXTRA_ARGS (nt)));
8613 else if (ARGUMENT_PACK_P (ot))
8615 int i, len;
8616 tree opack, npack;
8618 if (!ARGUMENT_PACK_P (nt))
8619 return 0;
8621 opack = ARGUMENT_PACK_ARGS (ot);
8622 npack = ARGUMENT_PACK_ARGS (nt);
8623 len = TREE_VEC_LENGTH (opack);
8624 if (TREE_VEC_LENGTH (npack) != len)
8625 return 0;
8626 for (i = 0; i < len; ++i)
8627 if (!template_args_equal (TREE_VEC_ELT (opack, i),
8628 TREE_VEC_ELT (npack, i)))
8629 return 0;
8630 return 1;
8632 else if (ot && TREE_CODE (ot) == ARGUMENT_PACK_SELECT)
8633 gcc_unreachable ();
8634 else if (TYPE_P (nt))
8636 if (!TYPE_P (ot))
8637 return false;
8638 /* Don't treat an alias template specialization with dependent
8639 arguments as equivalent to its underlying type when used as a
8640 template argument; we need them to be distinct so that we
8641 substitute into the specialization arguments at instantiation
8642 time. And aliases can't be equivalent without being ==, so
8643 we don't need to look any deeper.
8645 During partial ordering, however, we need to treat them normally so
8646 that we can order uses of the same alias with different
8647 cv-qualification (79960). */
8648 if (!partial_order
8649 && (TYPE_ALIAS_P (nt) || TYPE_ALIAS_P (ot)))
8650 return false;
8651 else
8652 return same_type_p (ot, nt);
8654 else if (TREE_CODE (ot) == TREE_VEC || TYPE_P (ot))
8655 return 0;
8656 else
8658 /* Try to treat a template non-type argument that has been converted
8659 to the parameter type as equivalent to one that hasn't yet. */
8660 for (enum tree_code code1 = TREE_CODE (ot);
8661 CONVERT_EXPR_CODE_P (code1)
8662 || code1 == NON_LVALUE_EXPR;
8663 code1 = TREE_CODE (ot))
8664 ot = TREE_OPERAND (ot, 0);
8665 for (enum tree_code code2 = TREE_CODE (nt);
8666 CONVERT_EXPR_CODE_P (code2)
8667 || code2 == NON_LVALUE_EXPR;
8668 code2 = TREE_CODE (nt))
8669 nt = TREE_OPERAND (nt, 0);
8671 return cp_tree_equal (ot, nt);
8675 /* Returns 1 iff the OLDARGS and NEWARGS are in fact identical sets of
8676 template arguments. Returns 0 otherwise, and updates OLDARG_PTR and
8677 NEWARG_PTR with the offending arguments if they are non-NULL. */
8680 comp_template_args (tree oldargs, tree newargs,
8681 tree *oldarg_ptr, tree *newarg_ptr,
8682 bool partial_order)
8684 int i;
8686 if (oldargs == newargs)
8687 return 1;
8689 if (!oldargs || !newargs)
8690 return 0;
8692 if (TREE_VEC_LENGTH (oldargs) != TREE_VEC_LENGTH (newargs))
8693 return 0;
8695 for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
8697 tree nt = TREE_VEC_ELT (newargs, i);
8698 tree ot = TREE_VEC_ELT (oldargs, i);
8700 if (! template_args_equal (ot, nt, partial_order))
8702 if (oldarg_ptr != NULL)
8703 *oldarg_ptr = ot;
8704 if (newarg_ptr != NULL)
8705 *newarg_ptr = nt;
8706 return 0;
8709 return 1;
8712 inline bool
8713 comp_template_args_porder (tree oargs, tree nargs)
8715 return comp_template_args (oargs, nargs, NULL, NULL, true);
8718 static void
8719 add_pending_template (tree d)
8721 tree ti = (TYPE_P (d)
8722 ? CLASSTYPE_TEMPLATE_INFO (d)
8723 : DECL_TEMPLATE_INFO (d));
8724 struct pending_template *pt;
8725 int level;
8727 if (TI_PENDING_TEMPLATE_FLAG (ti))
8728 return;
8730 /* We are called both from instantiate_decl, where we've already had a
8731 tinst_level pushed, and instantiate_template, where we haven't.
8732 Compensate. */
8733 level = !current_tinst_level || current_tinst_level->decl != d;
8735 if (level)
8736 push_tinst_level (d);
8738 pt = ggc_alloc<pending_template> ();
8739 pt->next = NULL;
8740 pt->tinst = current_tinst_level;
8741 if (last_pending_template)
8742 last_pending_template->next = pt;
8743 else
8744 pending_templates = pt;
8746 last_pending_template = pt;
8748 TI_PENDING_TEMPLATE_FLAG (ti) = 1;
8750 if (level)
8751 pop_tinst_level ();
8755 /* Return a TEMPLATE_ID_EXPR corresponding to the indicated FNS and
8756 ARGLIST. Valid choices for FNS are given in the cp-tree.def
8757 documentation for TEMPLATE_ID_EXPR. */
8759 tree
8760 lookup_template_function (tree fns, tree arglist)
8762 tree type;
8764 if (fns == error_mark_node || arglist == error_mark_node)
8765 return error_mark_node;
8767 gcc_assert (!arglist || TREE_CODE (arglist) == TREE_VEC);
8769 if (!is_overloaded_fn (fns) && !identifier_p (fns))
8771 error ("%q#D is not a function template", fns);
8772 return error_mark_node;
8775 if (BASELINK_P (fns))
8777 BASELINK_FUNCTIONS (fns) = build2 (TEMPLATE_ID_EXPR,
8778 unknown_type_node,
8779 BASELINK_FUNCTIONS (fns),
8780 arglist);
8781 return fns;
8784 type = TREE_TYPE (fns);
8785 if (TREE_CODE (fns) == OVERLOAD || !type)
8786 type = unknown_type_node;
8788 return build2 (TEMPLATE_ID_EXPR, type, fns, arglist);
8791 /* Within the scope of a template class S<T>, the name S gets bound
8792 (in build_self_reference) to a TYPE_DECL for the class, not a
8793 TEMPLATE_DECL. If DECL is a TYPE_DECL for current_class_type,
8794 or one of its enclosing classes, and that type is a template,
8795 return the associated TEMPLATE_DECL. Otherwise, the original
8796 DECL is returned.
8798 Also handle the case when DECL is a TREE_LIST of ambiguous
8799 injected-class-names from different bases. */
8801 tree
8802 maybe_get_template_decl_from_type_decl (tree decl)
8804 if (decl == NULL_TREE)
8805 return decl;
8807 /* DR 176: A lookup that finds an injected-class-name (10.2
8808 [class.member.lookup]) can result in an ambiguity in certain cases
8809 (for example, if it is found in more than one base class). If all of
8810 the injected-class-names that are found refer to specializations of
8811 the same class template, and if the name is followed by a
8812 template-argument-list, the reference refers to the class template
8813 itself and not a specialization thereof, and is not ambiguous. */
8814 if (TREE_CODE (decl) == TREE_LIST)
8816 tree t, tmpl = NULL_TREE;
8817 for (t = decl; t; t = TREE_CHAIN (t))
8819 tree elt = maybe_get_template_decl_from_type_decl (TREE_VALUE (t));
8820 if (!tmpl)
8821 tmpl = elt;
8822 else if (tmpl != elt)
8823 break;
8825 if (tmpl && t == NULL_TREE)
8826 return tmpl;
8827 else
8828 return decl;
8831 return (decl != NULL_TREE
8832 && DECL_SELF_REFERENCE_P (decl)
8833 && CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (decl)))
8834 ? CLASSTYPE_TI_TEMPLATE (TREE_TYPE (decl)) : decl;
8837 /* Given an IDENTIFIER_NODE (or type TEMPLATE_DECL) and a chain of
8838 parameters, find the desired type.
8840 D1 is the PTYPENAME terminal, and ARGLIST is the list of arguments.
8842 IN_DECL, if non-NULL, is the template declaration we are trying to
8843 instantiate.
8845 If ENTERING_SCOPE is nonzero, we are about to enter the scope of
8846 the class we are looking up.
8848 Issue error and warning messages under control of COMPLAIN.
8850 If the template class is really a local class in a template
8851 function, then the FUNCTION_CONTEXT is the function in which it is
8852 being instantiated.
8854 ??? Note that this function is currently called *twice* for each
8855 template-id: the first time from the parser, while creating the
8856 incomplete type (finish_template_type), and the second type during the
8857 real instantiation (instantiate_template_class). This is surely something
8858 that we want to avoid. It also causes some problems with argument
8859 coercion (see convert_nontype_argument for more information on this). */
8861 static tree
8862 lookup_template_class_1 (tree d1, tree arglist, tree in_decl, tree context,
8863 int entering_scope, tsubst_flags_t complain)
8865 tree templ = NULL_TREE, parmlist;
8866 tree t;
8867 spec_entry **slot;
8868 spec_entry *entry;
8869 spec_entry elt;
8870 hashval_t hash;
8872 if (identifier_p (d1))
8874 tree value = innermost_non_namespace_value (d1);
8875 if (value && DECL_TEMPLATE_TEMPLATE_PARM_P (value))
8876 templ = value;
8877 else
8879 if (context)
8880 push_decl_namespace (context);
8881 templ = lookup_name (d1);
8882 templ = maybe_get_template_decl_from_type_decl (templ);
8883 if (context)
8884 pop_decl_namespace ();
8886 if (templ)
8887 context = DECL_CONTEXT (templ);
8889 else if (TREE_CODE (d1) == TYPE_DECL && MAYBE_CLASS_TYPE_P (TREE_TYPE (d1)))
8891 tree type = TREE_TYPE (d1);
8893 /* If we are declaring a constructor, say A<T>::A<T>, we will get
8894 an implicit typename for the second A. Deal with it. */
8895 if (TREE_CODE (type) == TYPENAME_TYPE && TREE_TYPE (type))
8896 type = TREE_TYPE (type);
8898 if (CLASSTYPE_TEMPLATE_INFO (type))
8900 templ = CLASSTYPE_TI_TEMPLATE (type);
8901 d1 = DECL_NAME (templ);
8904 else if (TREE_CODE (d1) == ENUMERAL_TYPE
8905 || (TYPE_P (d1) && MAYBE_CLASS_TYPE_P (d1)))
8907 templ = TYPE_TI_TEMPLATE (d1);
8908 d1 = DECL_NAME (templ);
8910 else if (DECL_TYPE_TEMPLATE_P (d1))
8912 templ = d1;
8913 d1 = DECL_NAME (templ);
8914 context = DECL_CONTEXT (templ);
8916 else if (DECL_TEMPLATE_TEMPLATE_PARM_P (d1))
8918 templ = d1;
8919 d1 = DECL_NAME (templ);
8922 /* Issue an error message if we didn't find a template. */
8923 if (! templ)
8925 if (complain & tf_error)
8926 error ("%qT is not a template", d1);
8927 return error_mark_node;
8930 if (TREE_CODE (templ) != TEMPLATE_DECL
8931 /* Make sure it's a user visible template, if it was named by
8932 the user. */
8933 || ((complain & tf_user) && !DECL_TEMPLATE_PARM_P (templ)
8934 && !PRIMARY_TEMPLATE_P (templ)))
8936 if (complain & tf_error)
8938 error ("non-template type %qT used as a template", d1);
8939 if (in_decl)
8940 error ("for template declaration %q+D", in_decl);
8942 return error_mark_node;
8945 complain &= ~tf_user;
8947 /* An alias that just changes the name of a template is equivalent to the
8948 other template, so if any of the arguments are pack expansions, strip
8949 the alias to avoid problems with a pack expansion passed to a non-pack
8950 alias template parameter (DR 1430). */
8951 if (pack_expansion_args_count (INNERMOST_TEMPLATE_ARGS (arglist)))
8952 templ = get_underlying_template (templ);
8954 if (DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
8956 tree parm;
8957 tree arglist2 = coerce_template_args_for_ttp (templ, arglist, complain);
8958 if (arglist2 == error_mark_node
8959 || (!uses_template_parms (arglist2)
8960 && check_instantiated_args (templ, arglist2, complain)))
8961 return error_mark_node;
8963 parm = bind_template_template_parm (TREE_TYPE (templ), arglist2);
8964 return parm;
8966 else
8968 tree template_type = TREE_TYPE (templ);
8969 tree gen_tmpl;
8970 tree type_decl;
8971 tree found = NULL_TREE;
8972 int arg_depth;
8973 int parm_depth;
8974 int is_dependent_type;
8975 int use_partial_inst_tmpl = false;
8977 if (template_type == error_mark_node)
8978 /* An error occurred while building the template TEMPL, and a
8979 diagnostic has most certainly been emitted for that
8980 already. Let's propagate that error. */
8981 return error_mark_node;
8983 gen_tmpl = most_general_template (templ);
8984 parmlist = DECL_TEMPLATE_PARMS (gen_tmpl);
8985 parm_depth = TMPL_PARMS_DEPTH (parmlist);
8986 arg_depth = TMPL_ARGS_DEPTH (arglist);
8988 if (arg_depth == 1 && parm_depth > 1)
8990 /* We've been given an incomplete set of template arguments.
8991 For example, given:
8993 template <class T> struct S1 {
8994 template <class U> struct S2 {};
8995 template <class U> struct S2<U*> {};
8998 we will be called with an ARGLIST of `U*', but the
8999 TEMPLATE will be `template <class T> template
9000 <class U> struct S1<T>::S2'. We must fill in the missing
9001 arguments. */
9002 tree ti = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (templ));
9003 arglist = add_outermost_template_args (TI_ARGS (ti), arglist);
9004 arg_depth = TMPL_ARGS_DEPTH (arglist);
9007 /* Now we should have enough arguments. */
9008 gcc_assert (parm_depth == arg_depth);
9010 /* From here on, we're only interested in the most general
9011 template. */
9013 /* Calculate the BOUND_ARGS. These will be the args that are
9014 actually tsubst'd into the definition to create the
9015 instantiation. */
9016 arglist = coerce_innermost_template_parms (parmlist, arglist, gen_tmpl,
9017 complain,
9018 /*require_all_args=*/true,
9019 /*use_default_args=*/true);
9021 if (arglist == error_mark_node)
9022 /* We were unable to bind the arguments. */
9023 return error_mark_node;
9025 /* In the scope of a template class, explicit references to the
9026 template class refer to the type of the template, not any
9027 instantiation of it. For example, in:
9029 template <class T> class C { void f(C<T>); }
9031 the `C<T>' is just the same as `C'. Outside of the
9032 class, however, such a reference is an instantiation. */
9033 if (entering_scope
9034 || !PRIMARY_TEMPLATE_P (gen_tmpl)
9035 || currently_open_class (template_type))
9037 tree tinfo = TYPE_TEMPLATE_INFO (template_type);
9039 if (tinfo && comp_template_args (TI_ARGS (tinfo), arglist))
9040 return template_type;
9043 /* If we already have this specialization, return it. */
9044 elt.tmpl = gen_tmpl;
9045 elt.args = arglist;
9046 elt.spec = NULL_TREE;
9047 hash = spec_hasher::hash (&elt);
9048 entry = type_specializations->find_with_hash (&elt, hash);
9050 if (entry)
9051 return entry->spec;
9053 /* If the the template's constraints are not satisfied,
9054 then we cannot form a valid type.
9056 Note that the check is deferred until after the hash
9057 lookup. This prevents redundant checks on previously
9058 instantiated specializations. */
9059 if (flag_concepts && !constraints_satisfied_p (gen_tmpl, arglist))
9061 if (complain & tf_error)
9063 error ("template constraint failure");
9064 diagnose_constraints (input_location, gen_tmpl, arglist);
9066 return error_mark_node;
9069 is_dependent_type = uses_template_parms (arglist);
9071 /* If the deduced arguments are invalid, then the binding
9072 failed. */
9073 if (!is_dependent_type
9074 && check_instantiated_args (gen_tmpl,
9075 INNERMOST_TEMPLATE_ARGS (arglist),
9076 complain))
9077 return error_mark_node;
9079 if (!is_dependent_type
9080 && !PRIMARY_TEMPLATE_P (gen_tmpl)
9081 && !LAMBDA_TYPE_P (TREE_TYPE (gen_tmpl))
9082 && TREE_CODE (CP_DECL_CONTEXT (gen_tmpl)) == NAMESPACE_DECL)
9084 found = xref_tag_from_type (TREE_TYPE (gen_tmpl),
9085 DECL_NAME (gen_tmpl),
9086 /*tag_scope=*/ts_global);
9087 return found;
9090 context = tsubst (DECL_CONTEXT (gen_tmpl), arglist,
9091 complain, in_decl);
9092 if (context == error_mark_node)
9093 return error_mark_node;
9095 if (!context)
9096 context = global_namespace;
9098 /* Create the type. */
9099 if (DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9101 /* The user referred to a specialization of an alias
9102 template represented by GEN_TMPL.
9104 [temp.alias]/2 says:
9106 When a template-id refers to the specialization of an
9107 alias template, it is equivalent to the associated
9108 type obtained by substitution of its
9109 template-arguments for the template-parameters in the
9110 type-id of the alias template. */
9112 t = tsubst (TREE_TYPE (gen_tmpl), arglist, complain, in_decl);
9113 /* Note that the call above (by indirectly calling
9114 register_specialization in tsubst_decl) registers the
9115 TYPE_DECL representing the specialization of the alias
9116 template. So next time someone substitutes ARGLIST for
9117 the template parms into the alias template (GEN_TMPL),
9118 she'll get that TYPE_DECL back. */
9120 if (t == error_mark_node)
9121 return t;
9123 else if (TREE_CODE (template_type) == ENUMERAL_TYPE)
9125 if (!is_dependent_type)
9127 set_current_access_from_decl (TYPE_NAME (template_type));
9128 t = start_enum (TYPE_IDENTIFIER (template_type), NULL_TREE,
9129 tsubst (ENUM_UNDERLYING_TYPE (template_type),
9130 arglist, complain, in_decl),
9131 tsubst_attributes (TYPE_ATTRIBUTES (template_type),
9132 arglist, complain, in_decl),
9133 SCOPED_ENUM_P (template_type), NULL);
9135 if (t == error_mark_node)
9136 return t;
9138 else
9140 /* We don't want to call start_enum for this type, since
9141 the values for the enumeration constants may involve
9142 template parameters. And, no one should be interested
9143 in the enumeration constants for such a type. */
9144 t = cxx_make_type (ENUMERAL_TYPE);
9145 SET_SCOPED_ENUM_P (t, SCOPED_ENUM_P (template_type));
9147 SET_OPAQUE_ENUM_P (t, OPAQUE_ENUM_P (template_type));
9148 ENUM_FIXED_UNDERLYING_TYPE_P (t)
9149 = ENUM_FIXED_UNDERLYING_TYPE_P (template_type);
9151 else if (CLASS_TYPE_P (template_type))
9153 /* Lambda closures are regenerated in tsubst_lambda_expr, not
9154 instantiated here. */
9155 gcc_assert (!LAMBDA_TYPE_P (template_type));
9157 t = make_class_type (TREE_CODE (template_type));
9158 CLASSTYPE_DECLARED_CLASS (t)
9159 = CLASSTYPE_DECLARED_CLASS (template_type);
9160 SET_CLASSTYPE_IMPLICIT_INSTANTIATION (t);
9162 /* A local class. Make sure the decl gets registered properly. */
9163 if (context == current_function_decl)
9164 pushtag (DECL_NAME (gen_tmpl), t, /*tag_scope=*/ts_current);
9166 if (comp_template_args (CLASSTYPE_TI_ARGS (template_type), arglist))
9167 /* This instantiation is another name for the primary
9168 template type. Set the TYPE_CANONICAL field
9169 appropriately. */
9170 TYPE_CANONICAL (t) = template_type;
9171 else if (any_template_arguments_need_structural_equality_p (arglist))
9172 /* Some of the template arguments require structural
9173 equality testing, so this template class requires
9174 structural equality testing. */
9175 SET_TYPE_STRUCTURAL_EQUALITY (t);
9177 else
9178 gcc_unreachable ();
9180 /* If we called start_enum or pushtag above, this information
9181 will already be set up. */
9182 if (!TYPE_NAME (t))
9184 TYPE_CONTEXT (t) = FROB_CONTEXT (context);
9186 type_decl = create_implicit_typedef (DECL_NAME (gen_tmpl), t);
9187 DECL_CONTEXT (type_decl) = TYPE_CONTEXT (t);
9188 DECL_SOURCE_LOCATION (type_decl)
9189 = DECL_SOURCE_LOCATION (TYPE_STUB_DECL (template_type));
9191 else
9192 type_decl = TYPE_NAME (t);
9194 if (CLASS_TYPE_P (template_type))
9196 TREE_PRIVATE (type_decl)
9197 = TREE_PRIVATE (TYPE_MAIN_DECL (template_type));
9198 TREE_PROTECTED (type_decl)
9199 = TREE_PROTECTED (TYPE_MAIN_DECL (template_type));
9200 if (CLASSTYPE_VISIBILITY_SPECIFIED (template_type))
9202 DECL_VISIBILITY_SPECIFIED (type_decl) = 1;
9203 DECL_VISIBILITY (type_decl) = CLASSTYPE_VISIBILITY (template_type);
9207 if (OVERLOAD_TYPE_P (t)
9208 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9210 static const char *tags[] = {"abi_tag", "may_alias"};
9212 for (unsigned ix = 0; ix != 2; ix++)
9214 tree attributes
9215 = lookup_attribute (tags[ix], TYPE_ATTRIBUTES (template_type));
9217 if (attributes)
9218 TYPE_ATTRIBUTES (t)
9219 = tree_cons (TREE_PURPOSE (attributes),
9220 TREE_VALUE (attributes),
9221 TYPE_ATTRIBUTES (t));
9225 /* Let's consider the explicit specialization of a member
9226 of a class template specialization that is implicitly instantiated,
9227 e.g.:
9228 template<class T>
9229 struct S
9231 template<class U> struct M {}; //#0
9234 template<>
9235 template<>
9236 struct S<int>::M<char> //#1
9238 int i;
9240 [temp.expl.spec]/4 says this is valid.
9242 In this case, when we write:
9243 S<int>::M<char> m;
9245 M is instantiated from the CLASSTYPE_TI_TEMPLATE of #1, not from
9246 the one of #0.
9248 When we encounter #1, we want to store the partial instantiation
9249 of M (template<class T> S<int>::M<T>) in its CLASSTYPE_TI_TEMPLATE.
9251 For all cases other than this "explicit specialization of member of a
9252 class template", we just want to store the most general template into
9253 the CLASSTYPE_TI_TEMPLATE of M.
9255 This case of "explicit specialization of member of a class template"
9256 only happens when:
9257 1/ the enclosing class is an instantiation of, and therefore not
9258 the same as, the context of the most general template, and
9259 2/ we aren't looking at the partial instantiation itself, i.e.
9260 the innermost arguments are not the same as the innermost parms of
9261 the most general template.
9263 So it's only when 1/ and 2/ happens that we want to use the partial
9264 instantiation of the member template in lieu of its most general
9265 template. */
9267 if (PRIMARY_TEMPLATE_P (gen_tmpl)
9268 && TMPL_ARGS_HAVE_MULTIPLE_LEVELS (arglist)
9269 /* the enclosing class must be an instantiation... */
9270 && CLASS_TYPE_P (context)
9271 && !same_type_p (context, DECL_CONTEXT (gen_tmpl)))
9273 TREE_VEC_LENGTH (arglist)--;
9274 ++processing_template_decl;
9275 tree tinfo = TYPE_TEMPLATE_INFO_MAYBE_ALIAS (TREE_TYPE (gen_tmpl));
9276 tree partial_inst_args =
9277 tsubst (INNERMOST_TEMPLATE_ARGS (TI_ARGS (tinfo)),
9278 arglist, complain, NULL_TREE);
9279 --processing_template_decl;
9280 TREE_VEC_LENGTH (arglist)++;
9281 if (partial_inst_args == error_mark_node)
9282 return error_mark_node;
9283 use_partial_inst_tmpl =
9284 /*...and we must not be looking at the partial instantiation
9285 itself. */
9286 !comp_template_args (INNERMOST_TEMPLATE_ARGS (arglist),
9287 partial_inst_args);
9290 if (!use_partial_inst_tmpl)
9291 /* This case is easy; there are no member templates involved. */
9292 found = gen_tmpl;
9293 else
9295 /* This is a full instantiation of a member template. Find
9296 the partial instantiation of which this is an instance. */
9298 /* Temporarily reduce by one the number of levels in the ARGLIST
9299 so as to avoid comparing the last set of arguments. */
9300 TREE_VEC_LENGTH (arglist)--;
9301 found = tsubst (gen_tmpl, arglist, complain, NULL_TREE);
9302 TREE_VEC_LENGTH (arglist)++;
9303 /* FOUND is either a proper class type, or an alias
9304 template specialization. In the later case, it's a
9305 TYPE_DECL, resulting from the substituting of arguments
9306 for parameters in the TYPE_DECL of the alias template
9307 done earlier. So be careful while getting the template
9308 of FOUND. */
9309 found = (TREE_CODE (found) == TEMPLATE_DECL
9310 ? found
9311 : (TREE_CODE (found) == TYPE_DECL
9312 ? DECL_TI_TEMPLATE (found)
9313 : CLASSTYPE_TI_TEMPLATE (found)));
9316 // Build template info for the new specialization.
9317 SET_TYPE_TEMPLATE_INFO (t, build_template_info (found, arglist));
9319 elt.spec = t;
9320 slot = type_specializations->find_slot_with_hash (&elt, hash, INSERT);
9321 entry = ggc_alloc<spec_entry> ();
9322 *entry = elt;
9323 *slot = entry;
9325 /* Note this use of the partial instantiation so we can check it
9326 later in maybe_process_partial_specialization. */
9327 DECL_TEMPLATE_INSTANTIATIONS (found)
9328 = tree_cons (arglist, t,
9329 DECL_TEMPLATE_INSTANTIATIONS (found));
9331 if (TREE_CODE (template_type) == ENUMERAL_TYPE && !is_dependent_type
9332 && !DECL_ALIAS_TEMPLATE_P (gen_tmpl))
9333 /* Now that the type has been registered on the instantiations
9334 list, we set up the enumerators. Because the enumeration
9335 constants may involve the enumeration type itself, we make
9336 sure to register the type first, and then create the
9337 constants. That way, doing tsubst_expr for the enumeration
9338 constants won't result in recursive calls here; we'll find
9339 the instantiation and exit above. */
9340 tsubst_enum (template_type, t, arglist);
9342 if (CLASS_TYPE_P (template_type) && is_dependent_type)
9343 /* If the type makes use of template parameters, the
9344 code that generates debugging information will crash. */
9345 DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = 1;
9347 /* Possibly limit visibility based on template args. */
9348 TREE_PUBLIC (type_decl) = 1;
9349 determine_visibility (type_decl);
9351 inherit_targ_abi_tags (t);
9353 return t;
9357 /* Wrapper for lookup_template_class_1. */
9359 tree
9360 lookup_template_class (tree d1, tree arglist, tree in_decl, tree context,
9361 int entering_scope, tsubst_flags_t complain)
9363 tree ret;
9364 timevar_push (TV_TEMPLATE_INST);
9365 ret = lookup_template_class_1 (d1, arglist, in_decl, context,
9366 entering_scope, complain);
9367 timevar_pop (TV_TEMPLATE_INST);
9368 return ret;
9371 /* Return a TEMPLATE_ID_EXPR for the given variable template and ARGLIST. */
9373 tree
9374 lookup_template_variable (tree templ, tree arglist)
9376 /* The type of the expression is NULL_TREE since the template-id could refer
9377 to an explicit or partial specialization. */
9378 tree type = NULL_TREE;
9379 if (flag_concepts && variable_concept_p (templ))
9380 /* Except that concepts are always bool. */
9381 type = boolean_type_node;
9382 return build2 (TEMPLATE_ID_EXPR, type, templ, arglist);
9385 /* Instantiate a variable declaration from a TEMPLATE_ID_EXPR for use. */
9387 tree
9388 finish_template_variable (tree var, tsubst_flags_t complain)
9390 tree templ = TREE_OPERAND (var, 0);
9391 tree arglist = TREE_OPERAND (var, 1);
9393 /* We never want to return a VAR_DECL for a variable concept, since they
9394 aren't instantiated. In a template, leave the TEMPLATE_ID_EXPR alone. */
9395 bool concept_p = flag_concepts && variable_concept_p (templ);
9396 if (concept_p && processing_template_decl)
9397 return var;
9399 tree tmpl_args = DECL_TI_ARGS (DECL_TEMPLATE_RESULT (templ));
9400 arglist = add_outermost_template_args (tmpl_args, arglist);
9402 templ = most_general_template (templ);
9403 tree parms = DECL_TEMPLATE_PARMS (templ);
9404 arglist = coerce_innermost_template_parms (parms, arglist, templ, complain,
9405 /*req_all*/true,
9406 /*use_default*/true);
9408 if (flag_concepts && !constraints_satisfied_p (templ, arglist))
9410 if (complain & tf_error)
9412 error ("use of invalid variable template %qE", var);
9413 diagnose_constraints (location_of (var), templ, arglist);
9415 return error_mark_node;
9418 /* If a template-id refers to a specialization of a variable
9419 concept, then the expression is true if and only if the
9420 concept's constraints are satisfied by the given template
9421 arguments.
9423 NOTE: This is an extension of Concepts Lite TS that
9424 allows constraints to be used in expressions. */
9425 if (concept_p)
9427 tree decl = DECL_TEMPLATE_RESULT (templ);
9428 return evaluate_variable_concept (decl, arglist);
9431 return instantiate_template (templ, arglist, complain);
9434 /* Construct a TEMPLATE_ID_EXPR for the given variable template TEMPL having
9435 TARGS template args, and instantiate it if it's not dependent. */
9437 tree
9438 lookup_and_finish_template_variable (tree templ, tree targs,
9439 tsubst_flags_t complain)
9441 templ = lookup_template_variable (templ, targs);
9442 if (!any_dependent_template_arguments_p (targs))
9444 templ = finish_template_variable (templ, complain);
9445 mark_used (templ);
9448 return convert_from_reference (templ);
9452 struct pair_fn_data
9454 tree_fn_t fn;
9455 tree_fn_t any_fn;
9456 void *data;
9457 /* True when we should also visit template parameters that occur in
9458 non-deduced contexts. */
9459 bool include_nondeduced_p;
9460 hash_set<tree> *visited;
9463 /* Called from for_each_template_parm via walk_tree. */
9465 static tree
9466 for_each_template_parm_r (tree *tp, int *walk_subtrees, void *d)
9468 tree t = *tp;
9469 struct pair_fn_data *pfd = (struct pair_fn_data *) d;
9470 tree_fn_t fn = pfd->fn;
9471 void *data = pfd->data;
9472 tree result = NULL_TREE;
9474 #define WALK_SUBTREE(NODE) \
9475 do \
9477 result = for_each_template_parm (NODE, fn, data, pfd->visited, \
9478 pfd->include_nondeduced_p, \
9479 pfd->any_fn); \
9480 if (result) goto out; \
9482 while (0)
9484 if (pfd->any_fn && (*pfd->any_fn)(t, data))
9485 return t;
9487 if (TYPE_P (t)
9488 && (pfd->include_nondeduced_p || TREE_CODE (t) != TYPENAME_TYPE))
9489 WALK_SUBTREE (TYPE_CONTEXT (t));
9491 switch (TREE_CODE (t))
9493 case RECORD_TYPE:
9494 if (TYPE_PTRMEMFUNC_P (t))
9495 break;
9496 /* Fall through. */
9498 case UNION_TYPE:
9499 case ENUMERAL_TYPE:
9500 if (!TYPE_TEMPLATE_INFO (t))
9501 *walk_subtrees = 0;
9502 else
9503 WALK_SUBTREE (TYPE_TI_ARGS (t));
9504 break;
9506 case INTEGER_TYPE:
9507 WALK_SUBTREE (TYPE_MIN_VALUE (t));
9508 WALK_SUBTREE (TYPE_MAX_VALUE (t));
9509 break;
9511 case METHOD_TYPE:
9512 /* Since we're not going to walk subtrees, we have to do this
9513 explicitly here. */
9514 WALK_SUBTREE (TYPE_METHOD_BASETYPE (t));
9515 /* Fall through. */
9517 case FUNCTION_TYPE:
9518 /* Check the return type. */
9519 WALK_SUBTREE (TREE_TYPE (t));
9521 /* Check the parameter types. Since default arguments are not
9522 instantiated until they are needed, the TYPE_ARG_TYPES may
9523 contain expressions that involve template parameters. But,
9524 no-one should be looking at them yet. And, once they're
9525 instantiated, they don't contain template parameters, so
9526 there's no point in looking at them then, either. */
9528 tree parm;
9530 for (parm = TYPE_ARG_TYPES (t); parm; parm = TREE_CHAIN (parm))
9531 WALK_SUBTREE (TREE_VALUE (parm));
9533 /* Since we've already handled the TYPE_ARG_TYPES, we don't
9534 want walk_tree walking into them itself. */
9535 *walk_subtrees = 0;
9538 if (flag_noexcept_type)
9540 tree spec = TYPE_RAISES_EXCEPTIONS (t);
9541 if (spec)
9542 WALK_SUBTREE (TREE_PURPOSE (spec));
9544 break;
9546 case TYPEOF_TYPE:
9547 case UNDERLYING_TYPE:
9548 if (pfd->include_nondeduced_p
9549 && for_each_template_parm (TYPE_VALUES_RAW (t), fn, data,
9550 pfd->visited,
9551 pfd->include_nondeduced_p,
9552 pfd->any_fn))
9553 return error_mark_node;
9554 break;
9556 case FUNCTION_DECL:
9557 case VAR_DECL:
9558 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
9559 WALK_SUBTREE (DECL_TI_ARGS (t));
9560 /* Fall through. */
9562 case PARM_DECL:
9563 case CONST_DECL:
9564 if (TREE_CODE (t) == CONST_DECL && DECL_TEMPLATE_PARM_P (t))
9565 WALK_SUBTREE (DECL_INITIAL (t));
9566 if (DECL_CONTEXT (t)
9567 && pfd->include_nondeduced_p)
9568 WALK_SUBTREE (DECL_CONTEXT (t));
9569 break;
9571 case BOUND_TEMPLATE_TEMPLATE_PARM:
9572 /* Record template parameters such as `T' inside `TT<T>'. */
9573 WALK_SUBTREE (TYPE_TI_ARGS (t));
9574 /* Fall through. */
9576 case TEMPLATE_TEMPLATE_PARM:
9577 case TEMPLATE_TYPE_PARM:
9578 case TEMPLATE_PARM_INDEX:
9579 if (fn && (*fn)(t, data))
9580 return t;
9581 else if (!fn)
9582 return t;
9583 break;
9585 case TEMPLATE_DECL:
9586 /* A template template parameter is encountered. */
9587 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
9588 WALK_SUBTREE (TREE_TYPE (t));
9590 /* Already substituted template template parameter */
9591 *walk_subtrees = 0;
9592 break;
9594 case TYPENAME_TYPE:
9595 /* A template-id in a TYPENAME_TYPE might be a deduced context after
9596 partial instantiation. */
9597 WALK_SUBTREE (TYPENAME_TYPE_FULLNAME (t));
9598 break;
9600 case CONSTRUCTOR:
9601 if (TREE_TYPE (t) && TYPE_PTRMEMFUNC_P (TREE_TYPE (t))
9602 && pfd->include_nondeduced_p)
9603 WALK_SUBTREE (TYPE_PTRMEMFUNC_FN_TYPE (TREE_TYPE (t)));
9604 break;
9606 case INDIRECT_REF:
9607 case COMPONENT_REF:
9608 /* If there's no type, then this thing must be some expression
9609 involving template parameters. */
9610 if (!fn && !TREE_TYPE (t))
9611 return error_mark_node;
9612 break;
9614 case MODOP_EXPR:
9615 case CAST_EXPR:
9616 case IMPLICIT_CONV_EXPR:
9617 case REINTERPRET_CAST_EXPR:
9618 case CONST_CAST_EXPR:
9619 case STATIC_CAST_EXPR:
9620 case DYNAMIC_CAST_EXPR:
9621 case ARROW_EXPR:
9622 case DOTSTAR_EXPR:
9623 case TYPEID_EXPR:
9624 case PSEUDO_DTOR_EXPR:
9625 if (!fn)
9626 return error_mark_node;
9627 break;
9629 default:
9630 break;
9633 #undef WALK_SUBTREE
9635 /* We didn't find any template parameters we liked. */
9636 out:
9637 return result;
9640 /* For each TEMPLATE_TYPE_PARM, TEMPLATE_TEMPLATE_PARM,
9641 BOUND_TEMPLATE_TEMPLATE_PARM or TEMPLATE_PARM_INDEX in T,
9642 call FN with the parameter and the DATA.
9643 If FN returns nonzero, the iteration is terminated, and
9644 for_each_template_parm returns 1. Otherwise, the iteration
9645 continues. If FN never returns a nonzero value, the value
9646 returned by for_each_template_parm is 0. If FN is NULL, it is
9647 considered to be the function which always returns 1.
9649 If INCLUDE_NONDEDUCED_P, then this routine will also visit template
9650 parameters that occur in non-deduced contexts. When false, only
9651 visits those template parameters that can be deduced. */
9653 static tree
9654 for_each_template_parm (tree t, tree_fn_t fn, void* data,
9655 hash_set<tree> *visited,
9656 bool include_nondeduced_p,
9657 tree_fn_t any_fn)
9659 struct pair_fn_data pfd;
9660 tree result;
9662 /* Set up. */
9663 pfd.fn = fn;
9664 pfd.any_fn = any_fn;
9665 pfd.data = data;
9666 pfd.include_nondeduced_p = include_nondeduced_p;
9668 /* Walk the tree. (Conceptually, we would like to walk without
9669 duplicates, but for_each_template_parm_r recursively calls
9670 for_each_template_parm, so we would need to reorganize a fair
9671 bit to use walk_tree_without_duplicates, so we keep our own
9672 visited list.) */
9673 if (visited)
9674 pfd.visited = visited;
9675 else
9676 pfd.visited = new hash_set<tree>;
9677 result = cp_walk_tree (&t,
9678 for_each_template_parm_r,
9679 &pfd,
9680 pfd.visited);
9682 /* Clean up. */
9683 if (!visited)
9685 delete pfd.visited;
9686 pfd.visited = 0;
9689 return result;
9692 /* Returns true if T depends on any template parameter. */
9695 uses_template_parms (tree t)
9697 if (t == NULL_TREE)
9698 return false;
9700 bool dependent_p;
9701 int saved_processing_template_decl;
9703 saved_processing_template_decl = processing_template_decl;
9704 if (!saved_processing_template_decl)
9705 processing_template_decl = 1;
9706 if (TYPE_P (t))
9707 dependent_p = dependent_type_p (t);
9708 else if (TREE_CODE (t) == TREE_VEC)
9709 dependent_p = any_dependent_template_arguments_p (t);
9710 else if (TREE_CODE (t) == TREE_LIST)
9711 dependent_p = (uses_template_parms (TREE_VALUE (t))
9712 || uses_template_parms (TREE_CHAIN (t)));
9713 else if (TREE_CODE (t) == TYPE_DECL)
9714 dependent_p = dependent_type_p (TREE_TYPE (t));
9715 else if (DECL_P (t)
9716 || EXPR_P (t)
9717 || TREE_CODE (t) == TEMPLATE_PARM_INDEX
9718 || TREE_CODE (t) == OVERLOAD
9719 || BASELINK_P (t)
9720 || identifier_p (t)
9721 || TREE_CODE (t) == TRAIT_EXPR
9722 || TREE_CODE (t) == CONSTRUCTOR
9723 || CONSTANT_CLASS_P (t))
9724 dependent_p = (type_dependent_expression_p (t)
9725 || value_dependent_expression_p (t));
9726 else
9728 gcc_assert (t == error_mark_node);
9729 dependent_p = false;
9732 processing_template_decl = saved_processing_template_decl;
9734 return dependent_p;
9737 /* Returns true iff current_function_decl is an incompletely instantiated
9738 template. Useful instead of processing_template_decl because the latter
9739 is set to 0 during instantiate_non_dependent_expr. */
9741 bool
9742 in_template_function (void)
9744 tree fn = current_function_decl;
9745 bool ret;
9746 ++processing_template_decl;
9747 ret = (fn && DECL_LANG_SPECIFIC (fn)
9748 && DECL_TEMPLATE_INFO (fn)
9749 && any_dependent_template_arguments_p (DECL_TI_ARGS (fn)));
9750 --processing_template_decl;
9751 return ret;
9754 /* Returns true if T depends on any template parameter with level LEVEL. */
9756 bool
9757 uses_template_parms_level (tree t, int level)
9759 return for_each_template_parm (t, template_parm_this_level_p, &level, NULL,
9760 /*include_nondeduced_p=*/true);
9763 /* Returns true if the signature of DECL depends on any template parameter from
9764 its enclosing class. */
9766 bool
9767 uses_outer_template_parms (tree decl)
9769 int depth = template_class_depth (CP_DECL_CONTEXT (decl));
9770 if (depth == 0)
9771 return false;
9772 if (for_each_template_parm (TREE_TYPE (decl), template_parm_outer_level,
9773 &depth, NULL, /*include_nondeduced_p=*/true))
9774 return true;
9775 if (PRIMARY_TEMPLATE_P (decl)
9776 && for_each_template_parm (INNERMOST_TEMPLATE_PARMS
9777 (DECL_TEMPLATE_PARMS (decl)),
9778 template_parm_outer_level,
9779 &depth, NULL, /*include_nondeduced_p=*/true))
9780 return true;
9781 tree ci = get_constraints (decl);
9782 if (ci)
9783 ci = CI_ASSOCIATED_CONSTRAINTS (ci);
9784 if (ci && for_each_template_parm (ci, template_parm_outer_level,
9785 &depth, NULL, /*nondeduced*/true))
9786 return true;
9787 return false;
9790 /* Returns TRUE iff INST is an instantiation we don't need to do in an
9791 ill-formed translation unit, i.e. a variable or function that isn't
9792 usable in a constant expression. */
9794 static inline bool
9795 neglectable_inst_p (tree d)
9797 return (DECL_P (d)
9798 && !undeduced_auto_decl (d)
9799 && !(TREE_CODE (d) == FUNCTION_DECL ? DECL_DECLARED_CONSTEXPR_P (d)
9800 : decl_maybe_constant_var_p (d)));
9803 /* Returns TRUE iff we should refuse to instantiate DECL because it's
9804 neglectable and instantiated from within an erroneous instantiation. */
9806 static bool
9807 limit_bad_template_recursion (tree decl)
9809 struct tinst_level *lev = current_tinst_level;
9810 int errs = errorcount + sorrycount;
9811 if (lev == NULL || errs == 0 || !neglectable_inst_p (decl))
9812 return false;
9814 for (; lev; lev = lev->next)
9815 if (neglectable_inst_p (lev->decl))
9816 break;
9818 return (lev && errs > lev->errors);
9821 static int tinst_depth;
9822 extern int max_tinst_depth;
9823 int depth_reached;
9825 static GTY(()) struct tinst_level *last_error_tinst_level;
9827 /* We're starting to instantiate D; record the template instantiation context
9828 for diagnostics and to restore it later. */
9830 bool
9831 push_tinst_level (tree d)
9833 return push_tinst_level_loc (d, input_location);
9836 /* We're starting to instantiate D; record the template instantiation context
9837 at LOC for diagnostics and to restore it later. */
9839 bool
9840 push_tinst_level_loc (tree d, location_t loc)
9842 struct tinst_level *new_level;
9844 if (tinst_depth >= max_tinst_depth)
9846 /* Tell error.c not to try to instantiate any templates. */
9847 at_eof = 2;
9848 fatal_error (input_location,
9849 "template instantiation depth exceeds maximum of %d"
9850 " (use -ftemplate-depth= to increase the maximum)",
9851 max_tinst_depth);
9852 return false;
9855 /* If the current instantiation caused problems, don't let it instantiate
9856 anything else. Do allow deduction substitution and decls usable in
9857 constant expressions. */
9858 if (limit_bad_template_recursion (d))
9859 return false;
9861 /* When not -quiet, dump template instantiations other than functions, since
9862 announce_function will take care of those. */
9863 if (!quiet_flag
9864 && TREE_CODE (d) != TREE_LIST
9865 && TREE_CODE (d) != FUNCTION_DECL)
9866 fprintf (stderr, " %s", decl_as_string (d, TFF_DECL_SPECIFIERS));
9868 new_level = ggc_alloc<tinst_level> ();
9869 new_level->decl = d;
9870 new_level->locus = loc;
9871 new_level->errors = errorcount+sorrycount;
9872 new_level->in_system_header_p = in_system_header_at (input_location);
9873 new_level->next = current_tinst_level;
9874 current_tinst_level = new_level;
9876 ++tinst_depth;
9877 if (GATHER_STATISTICS && (tinst_depth > depth_reached))
9878 depth_reached = tinst_depth;
9880 return true;
9883 /* We're done instantiating this template; return to the instantiation
9884 context. */
9886 void
9887 pop_tinst_level (void)
9889 /* Restore the filename and line number stashed away when we started
9890 this instantiation. */
9891 input_location = current_tinst_level->locus;
9892 current_tinst_level = current_tinst_level->next;
9893 --tinst_depth;
9896 /* We're instantiating a deferred template; restore the template
9897 instantiation context in which the instantiation was requested, which
9898 is one step out from LEVEL. Return the corresponding DECL or TYPE. */
9900 static tree
9901 reopen_tinst_level (struct tinst_level *level)
9903 struct tinst_level *t;
9905 tinst_depth = 0;
9906 for (t = level; t; t = t->next)
9907 ++tinst_depth;
9909 current_tinst_level = level;
9910 pop_tinst_level ();
9911 if (current_tinst_level)
9912 current_tinst_level->errors = errorcount+sorrycount;
9913 return level->decl;
9916 /* Returns the TINST_LEVEL which gives the original instantiation
9917 context. */
9919 struct tinst_level *
9920 outermost_tinst_level (void)
9922 struct tinst_level *level = current_tinst_level;
9923 if (level)
9924 while (level->next)
9925 level = level->next;
9926 return level;
9929 /* DECL is a friend FUNCTION_DECL or TEMPLATE_DECL. ARGS is the
9930 vector of template arguments, as for tsubst.
9932 Returns an appropriate tsubst'd friend declaration. */
9934 static tree
9935 tsubst_friend_function (tree decl, tree args)
9937 tree new_friend;
9939 if (TREE_CODE (decl) == FUNCTION_DECL
9940 && DECL_TEMPLATE_INSTANTIATION (decl)
9941 && TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
9942 /* This was a friend declared with an explicit template
9943 argument list, e.g.:
9945 friend void f<>(T);
9947 to indicate that f was a template instantiation, not a new
9948 function declaration. Now, we have to figure out what
9949 instantiation of what template. */
9951 tree template_id, arglist, fns;
9952 tree new_args;
9953 tree tmpl;
9954 tree ns = decl_namespace_context (TYPE_MAIN_DECL (current_class_type));
9956 /* Friend functions are looked up in the containing namespace scope.
9957 We must enter that scope, to avoid finding member functions of the
9958 current class with same name. */
9959 push_nested_namespace (ns);
9960 fns = tsubst_expr (DECL_TI_TEMPLATE (decl), args,
9961 tf_warning_or_error, NULL_TREE,
9962 /*integral_constant_expression_p=*/false);
9963 pop_nested_namespace (ns);
9964 arglist = tsubst (DECL_TI_ARGS (decl), args,
9965 tf_warning_or_error, NULL_TREE);
9966 template_id = lookup_template_function (fns, arglist);
9968 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9969 tmpl = determine_specialization (template_id, new_friend,
9970 &new_args,
9971 /*need_member_template=*/0,
9972 TREE_VEC_LENGTH (args),
9973 tsk_none);
9974 return instantiate_template (tmpl, new_args, tf_error);
9977 new_friend = tsubst (decl, args, tf_warning_or_error, NULL_TREE);
9979 /* The NEW_FRIEND will look like an instantiation, to the
9980 compiler, but is not an instantiation from the point of view of
9981 the language. For example, we might have had:
9983 template <class T> struct S {
9984 template <class U> friend void f(T, U);
9987 Then, in S<int>, template <class U> void f(int, U) is not an
9988 instantiation of anything. */
9989 if (new_friend == error_mark_node)
9990 return error_mark_node;
9992 DECL_USE_TEMPLATE (new_friend) = 0;
9993 if (TREE_CODE (decl) == TEMPLATE_DECL)
9995 DECL_USE_TEMPLATE (DECL_TEMPLATE_RESULT (new_friend)) = 0;
9996 DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (new_friend))
9997 = DECL_SAVED_TREE (DECL_TEMPLATE_RESULT (decl));
10000 /* The mangled name for the NEW_FRIEND is incorrect. The function
10001 is not a template instantiation and should not be mangled like
10002 one. Therefore, we forget the mangling here; we'll recompute it
10003 later if we need it. */
10004 if (TREE_CODE (new_friend) != TEMPLATE_DECL)
10006 SET_DECL_RTL (new_friend, NULL);
10007 SET_DECL_ASSEMBLER_NAME (new_friend, NULL_TREE);
10010 if (DECL_NAMESPACE_SCOPE_P (new_friend))
10012 tree old_decl;
10013 tree new_friend_template_info;
10014 tree new_friend_result_template_info;
10015 tree ns;
10016 int new_friend_is_defn;
10018 /* We must save some information from NEW_FRIEND before calling
10019 duplicate decls since that function will free NEW_FRIEND if
10020 possible. */
10021 new_friend_template_info = DECL_TEMPLATE_INFO (new_friend);
10022 new_friend_is_defn =
10023 (DECL_INITIAL (DECL_TEMPLATE_RESULT
10024 (template_for_substitution (new_friend)))
10025 != NULL_TREE);
10026 if (TREE_CODE (new_friend) == TEMPLATE_DECL)
10028 /* This declaration is a `primary' template. */
10029 DECL_PRIMARY_TEMPLATE (new_friend) = new_friend;
10031 new_friend_result_template_info
10032 = DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (new_friend));
10034 else
10035 new_friend_result_template_info = NULL_TREE;
10037 /* Inside pushdecl_namespace_level, we will push into the
10038 current namespace. However, the friend function should go
10039 into the namespace of the template. */
10040 ns = decl_namespace_context (new_friend);
10041 push_nested_namespace (ns);
10042 old_decl = pushdecl_namespace_level (new_friend, /*is_friend=*/true);
10043 pop_nested_namespace (ns);
10045 if (old_decl == error_mark_node)
10046 return error_mark_node;
10048 if (old_decl != new_friend)
10050 /* This new friend declaration matched an existing
10051 declaration. For example, given:
10053 template <class T> void f(T);
10054 template <class U> class C {
10055 template <class T> friend void f(T) {}
10058 the friend declaration actually provides the definition
10059 of `f', once C has been instantiated for some type. So,
10060 old_decl will be the out-of-class template declaration,
10061 while new_friend is the in-class definition.
10063 But, if `f' was called before this point, the
10064 instantiation of `f' will have DECL_TI_ARGS corresponding
10065 to `T' but not to `U', references to which might appear
10066 in the definition of `f'. Previously, the most general
10067 template for an instantiation of `f' was the out-of-class
10068 version; now it is the in-class version. Therefore, we
10069 run through all specialization of `f', adding to their
10070 DECL_TI_ARGS appropriately. In particular, they need a
10071 new set of outer arguments, corresponding to the
10072 arguments for this class instantiation.
10074 The same situation can arise with something like this:
10076 friend void f(int);
10077 template <class T> class C {
10078 friend void f(T) {}
10081 when `C<int>' is instantiated. Now, `f(int)' is defined
10082 in the class. */
10084 if (!new_friend_is_defn)
10085 /* On the other hand, if the in-class declaration does
10086 *not* provide a definition, then we don't want to alter
10087 existing definitions. We can just leave everything
10088 alone. */
10090 else
10092 tree new_template = TI_TEMPLATE (new_friend_template_info);
10093 tree new_args = TI_ARGS (new_friend_template_info);
10095 /* Overwrite whatever template info was there before, if
10096 any, with the new template information pertaining to
10097 the declaration. */
10098 DECL_TEMPLATE_INFO (old_decl) = new_friend_template_info;
10100 if (TREE_CODE (old_decl) != TEMPLATE_DECL)
10102 /* We should have called reregister_specialization in
10103 duplicate_decls. */
10104 gcc_assert (retrieve_specialization (new_template,
10105 new_args, 0)
10106 == old_decl);
10108 /* Instantiate it if the global has already been used. */
10109 if (DECL_ODR_USED (old_decl))
10110 instantiate_decl (old_decl, /*defer_ok=*/true,
10111 /*expl_inst_class_mem_p=*/false);
10113 else
10115 tree t;
10117 /* Indicate that the old function template is a partial
10118 instantiation. */
10119 DECL_TEMPLATE_INFO (DECL_TEMPLATE_RESULT (old_decl))
10120 = new_friend_result_template_info;
10122 gcc_assert (new_template
10123 == most_general_template (new_template));
10124 gcc_assert (new_template != old_decl);
10126 /* Reassign any specializations already in the hash table
10127 to the new more general template, and add the
10128 additional template args. */
10129 for (t = DECL_TEMPLATE_INSTANTIATIONS (old_decl);
10130 t != NULL_TREE;
10131 t = TREE_CHAIN (t))
10133 tree spec = TREE_VALUE (t);
10134 spec_entry elt;
10136 elt.tmpl = old_decl;
10137 elt.args = DECL_TI_ARGS (spec);
10138 elt.spec = NULL_TREE;
10140 decl_specializations->remove_elt (&elt);
10142 DECL_TI_ARGS (spec)
10143 = add_outermost_template_args (new_args,
10144 DECL_TI_ARGS (spec));
10146 register_specialization
10147 (spec, new_template, DECL_TI_ARGS (spec), true, 0);
10150 DECL_TEMPLATE_INSTANTIATIONS (old_decl) = NULL_TREE;
10154 /* The information from NEW_FRIEND has been merged into OLD_DECL
10155 by duplicate_decls. */
10156 new_friend = old_decl;
10159 else
10161 tree context = DECL_CONTEXT (new_friend);
10162 bool dependent_p;
10164 /* In the code
10165 template <class T> class C {
10166 template <class U> friend void C1<U>::f (); // case 1
10167 friend void C2<T>::f (); // case 2
10169 we only need to make sure CONTEXT is a complete type for
10170 case 2. To distinguish between the two cases, we note that
10171 CONTEXT of case 1 remains dependent type after tsubst while
10172 this isn't true for case 2. */
10173 ++processing_template_decl;
10174 dependent_p = dependent_type_p (context);
10175 --processing_template_decl;
10177 if (!dependent_p
10178 && !complete_type_or_else (context, NULL_TREE))
10179 return error_mark_node;
10181 if (COMPLETE_TYPE_P (context))
10183 tree fn = new_friend;
10184 /* do_friend adds the TEMPLATE_DECL for any member friend
10185 template even if it isn't a member template, i.e.
10186 template <class T> friend A<T>::f();
10187 Look through it in that case. */
10188 if (TREE_CODE (fn) == TEMPLATE_DECL
10189 && !PRIMARY_TEMPLATE_P (fn))
10190 fn = DECL_TEMPLATE_RESULT (fn);
10191 /* Check to see that the declaration is really present, and,
10192 possibly obtain an improved declaration. */
10193 fn = check_classfn (context, fn, NULL_TREE);
10195 if (fn)
10196 new_friend = fn;
10200 return new_friend;
10203 /* FRIEND_TMPL is a friend TEMPLATE_DECL. ARGS is the vector of
10204 template arguments, as for tsubst.
10206 Returns an appropriate tsubst'd friend type or error_mark_node on
10207 failure. */
10209 static tree
10210 tsubst_friend_class (tree friend_tmpl, tree args)
10212 tree tmpl;
10214 if (DECL_TEMPLATE_TEMPLATE_PARM_P (friend_tmpl))
10216 tmpl = tsubst (TREE_TYPE (friend_tmpl), args, tf_none, NULL_TREE);
10217 return TREE_TYPE (tmpl);
10220 tree context = CP_DECL_CONTEXT (friend_tmpl);
10221 if (TREE_CODE (context) == NAMESPACE_DECL)
10222 push_nested_namespace (context);
10223 else
10224 push_nested_class (context);
10226 tmpl = lookup_name_real (DECL_NAME (friend_tmpl), /*prefer_type=*/false,
10227 /*non_class=*/false, /*block_p=*/false,
10228 /*namespaces_only=*/false, LOOKUP_HIDDEN);
10230 if (tmpl && DECL_CLASS_TEMPLATE_P (tmpl))
10232 /* The friend template has already been declared. Just
10233 check to see that the declarations match, and install any new
10234 default parameters. We must tsubst the default parameters,
10235 of course. We only need the innermost template parameters
10236 because that is all that redeclare_class_template will look
10237 at. */
10238 if (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (friend_tmpl))
10239 > TMPL_ARGS_DEPTH (args))
10241 tree parms = tsubst_template_parms (DECL_TEMPLATE_PARMS (friend_tmpl),
10242 args, tf_warning_or_error);
10243 location_t saved_input_location = input_location;
10244 input_location = DECL_SOURCE_LOCATION (friend_tmpl);
10245 tree cons = get_constraints (tmpl);
10246 redeclare_class_template (TREE_TYPE (tmpl), parms, cons);
10247 input_location = saved_input_location;
10250 else
10252 /* The friend template has not already been declared. In this
10253 case, the instantiation of the template class will cause the
10254 injection of this template into the namespace scope. */
10255 tmpl = tsubst (friend_tmpl, args, tf_warning_or_error, NULL_TREE);
10257 if (tmpl != error_mark_node)
10259 /* The new TMPL is not an instantiation of anything, so we
10260 forget its origins. We don't reset CLASSTYPE_TI_TEMPLATE
10261 for the new type because that is supposed to be the
10262 corresponding template decl, i.e., TMPL. */
10263 DECL_USE_TEMPLATE (tmpl) = 0;
10264 DECL_TEMPLATE_INFO (tmpl) = NULL_TREE;
10265 CLASSTYPE_USE_TEMPLATE (TREE_TYPE (tmpl)) = 0;
10266 CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
10267 = INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));
10269 /* It is hidden. */
10270 retrofit_lang_decl (DECL_TEMPLATE_RESULT (tmpl));
10271 DECL_ANTICIPATED (tmpl)
10272 = DECL_ANTICIPATED (DECL_TEMPLATE_RESULT (tmpl)) = true;
10274 /* Inject this template into the enclosing namspace scope. */
10275 tmpl = pushdecl_namespace_level (tmpl, true);
10279 if (TREE_CODE (context) == NAMESPACE_DECL)
10280 pop_nested_namespace (context);
10281 else
10282 pop_nested_class ();
10284 return TREE_TYPE (tmpl);
10287 /* Returns zero if TYPE cannot be completed later due to circularity.
10288 Otherwise returns one. */
10290 static int
10291 can_complete_type_without_circularity (tree type)
10293 if (type == NULL_TREE || type == error_mark_node)
10294 return 0;
10295 else if (COMPLETE_TYPE_P (type))
10296 return 1;
10297 else if (TREE_CODE (type) == ARRAY_TYPE)
10298 return can_complete_type_without_circularity (TREE_TYPE (type));
10299 else if (CLASS_TYPE_P (type)
10300 && TYPE_BEING_DEFINED (TYPE_MAIN_VARIANT (type)))
10301 return 0;
10302 else
10303 return 1;
10306 static tree tsubst_omp_clauses (tree, enum c_omp_region_type, tree,
10307 tsubst_flags_t, tree);
10309 /* Instantiate a single dependent attribute T (a TREE_LIST), and return either
10310 T or a new TREE_LIST, possibly a chain in the case of a pack expansion. */
10312 static tree
10313 tsubst_attribute (tree t, tree *decl_p, tree args,
10314 tsubst_flags_t complain, tree in_decl)
10316 gcc_assert (ATTR_IS_DEPENDENT (t));
10318 tree val = TREE_VALUE (t);
10319 if (val == NULL_TREE)
10320 /* Nothing to do. */;
10321 else if ((flag_openmp || flag_openmp_simd)
10322 && is_attribute_p ("omp declare simd",
10323 get_attribute_name (t)))
10325 tree clauses = TREE_VALUE (val);
10326 clauses = tsubst_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD, args,
10327 complain, in_decl);
10328 c_omp_declare_simd_clauses_to_decls (*decl_p, clauses);
10329 clauses = finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
10330 tree parms = DECL_ARGUMENTS (*decl_p);
10331 clauses
10332 = c_omp_declare_simd_clauses_to_numbers (parms, clauses);
10333 if (clauses)
10334 val = build_tree_list (NULL_TREE, clauses);
10335 else
10336 val = NULL_TREE;
10338 /* If the first attribute argument is an identifier, don't
10339 pass it through tsubst. Attributes like mode, format,
10340 cleanup and several target specific attributes expect it
10341 unmodified. */
10342 else if (attribute_takes_identifier_p (get_attribute_name (t)))
10344 tree chain
10345 = tsubst_expr (TREE_CHAIN (val), args, complain, in_decl,
10346 /*integral_constant_expression_p=*/false);
10347 if (chain != TREE_CHAIN (val))
10348 val = tree_cons (NULL_TREE, TREE_VALUE (val), chain);
10350 else if (PACK_EXPANSION_P (val))
10352 /* An attribute pack expansion. */
10353 tree purp = TREE_PURPOSE (t);
10354 tree pack = tsubst_pack_expansion (val, args, complain, in_decl);
10355 if (pack == error_mark_node)
10356 return error_mark_node;
10357 int len = TREE_VEC_LENGTH (pack);
10358 tree list = NULL_TREE;
10359 tree *q = &list;
10360 for (int i = 0; i < len; ++i)
10362 tree elt = TREE_VEC_ELT (pack, i);
10363 *q = build_tree_list (purp, elt);
10364 q = &TREE_CHAIN (*q);
10366 return list;
10368 else
10369 val = tsubst_expr (val, args, complain, in_decl,
10370 /*integral_constant_expression_p=*/false);
10372 if (val != TREE_VALUE (t))
10373 return build_tree_list (TREE_PURPOSE (t), val);
10374 return t;
10377 /* Instantiate any dependent attributes in ATTRIBUTES, returning either it
10378 unchanged or a new TREE_LIST chain. */
10380 static tree
10381 tsubst_attributes (tree attributes, tree args,
10382 tsubst_flags_t complain, tree in_decl)
10384 tree last_dep = NULL_TREE;
10386 for (tree t = attributes; t; t = TREE_CHAIN (t))
10387 if (ATTR_IS_DEPENDENT (t))
10389 last_dep = t;
10390 attributes = copy_list (attributes);
10391 break;
10394 if (last_dep)
10395 for (tree *p = &attributes; *p; )
10397 tree t = *p;
10398 if (ATTR_IS_DEPENDENT (t))
10400 tree subst = tsubst_attribute (t, NULL, args, complain, in_decl);
10401 if (subst != t)
10403 *p = subst;
10404 while (*p)
10405 p = &TREE_CHAIN (*p);
10406 *p = TREE_CHAIN (t);
10407 continue;
10410 p = &TREE_CHAIN (*p);
10413 return attributes;
10416 /* Apply any attributes which had to be deferred until instantiation
10417 time. DECL_P, ATTRIBUTES and ATTR_FLAGS are as cplus_decl_attributes;
10418 ARGS, COMPLAIN, IN_DECL are as tsubst. */
10420 static void
10421 apply_late_template_attributes (tree *decl_p, tree attributes, int attr_flags,
10422 tree args, tsubst_flags_t complain, tree in_decl)
10424 tree last_dep = NULL_TREE;
10425 tree t;
10426 tree *p;
10428 if (attributes == NULL_TREE)
10429 return;
10431 if (DECL_P (*decl_p))
10433 if (TREE_TYPE (*decl_p) == error_mark_node)
10434 return;
10435 p = &DECL_ATTRIBUTES (*decl_p);
10436 /* DECL_ATTRIBUTES comes from copy_node in tsubst_decl, and is identical
10437 to our attributes parameter. */
10438 gcc_assert (*p == attributes);
10440 else
10442 p = &TYPE_ATTRIBUTES (*decl_p);
10443 /* TYPE_ATTRIBUTES was set up (with abi_tag and may_alias) in
10444 lookup_template_class_1, and should be preserved. */
10445 gcc_assert (*p != attributes);
10446 while (*p)
10447 p = &TREE_CHAIN (*p);
10450 for (t = attributes; t; t = TREE_CHAIN (t))
10451 if (ATTR_IS_DEPENDENT (t))
10453 last_dep = t;
10454 attributes = copy_list (attributes);
10455 break;
10458 *p = attributes;
10459 if (last_dep)
10461 tree late_attrs = NULL_TREE;
10462 tree *q = &late_attrs;
10464 for (; *p; )
10466 t = *p;
10467 if (ATTR_IS_DEPENDENT (t))
10469 *p = TREE_CHAIN (t);
10470 TREE_CHAIN (t) = NULL_TREE;
10471 *q = tsubst_attribute (t, decl_p, args, complain, in_decl);
10472 while (*q)
10473 q = &TREE_CHAIN (*q);
10475 else
10476 p = &TREE_CHAIN (t);
10479 cplus_decl_attributes (decl_p, late_attrs, attr_flags);
10483 /* Perform (or defer) access check for typedefs that were referenced
10484 from within the template TMPL code.
10485 This is a subroutine of instantiate_decl and instantiate_class_template.
10486 TMPL is the template to consider and TARGS is the list of arguments of
10487 that template. */
10489 static void
10490 perform_typedefs_access_check (tree tmpl, tree targs)
10492 location_t saved_location;
10493 unsigned i;
10494 qualified_typedef_usage_t *iter;
10496 if (!tmpl
10497 || (!CLASS_TYPE_P (tmpl)
10498 && TREE_CODE (tmpl) != FUNCTION_DECL))
10499 return;
10501 saved_location = input_location;
10502 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (tmpl), i, iter)
10504 tree type_decl = iter->typedef_decl;
10505 tree type_scope = iter->context;
10507 if (!type_decl || !type_scope || !CLASS_TYPE_P (type_scope))
10508 continue;
10510 if (uses_template_parms (type_decl))
10511 type_decl = tsubst (type_decl, targs, tf_error, NULL_TREE);
10512 if (uses_template_parms (type_scope))
10513 type_scope = tsubst (type_scope, targs, tf_error, NULL_TREE);
10515 /* Make access check error messages point to the location
10516 of the use of the typedef. */
10517 input_location = iter->locus;
10518 perform_or_defer_access_check (TYPE_BINFO (type_scope),
10519 type_decl, type_decl,
10520 tf_warning_or_error);
10522 input_location = saved_location;
10525 static tree
10526 instantiate_class_template_1 (tree type)
10528 tree templ, args, pattern, t, member;
10529 tree typedecl;
10530 tree pbinfo;
10531 tree base_list;
10532 unsigned int saved_maximum_field_alignment;
10533 tree fn_context;
10535 if (type == error_mark_node)
10536 return error_mark_node;
10538 if (COMPLETE_OR_OPEN_TYPE_P (type)
10539 || uses_template_parms (type))
10540 return type;
10542 /* Figure out which template is being instantiated. */
10543 templ = most_general_template (CLASSTYPE_TI_TEMPLATE (type));
10544 gcc_assert (TREE_CODE (templ) == TEMPLATE_DECL);
10546 /* Mark the type as in the process of being defined. */
10547 TYPE_BEING_DEFINED (type) = 1;
10549 /* Determine what specialization of the original template to
10550 instantiate. */
10551 t = most_specialized_partial_spec (type, tf_warning_or_error);
10552 if (t == error_mark_node)
10553 return error_mark_node;
10554 else if (t)
10556 /* This TYPE is actually an instantiation of a partial
10557 specialization. We replace the innermost set of ARGS with
10558 the arguments appropriate for substitution. For example,
10559 given:
10561 template <class T> struct S {};
10562 template <class T> struct S<T*> {};
10564 and supposing that we are instantiating S<int*>, ARGS will
10565 presently be {int*} -- but we need {int}. */
10566 pattern = TREE_TYPE (t);
10567 args = TREE_PURPOSE (t);
10569 else
10571 pattern = TREE_TYPE (templ);
10572 args = CLASSTYPE_TI_ARGS (type);
10575 /* If the template we're instantiating is incomplete, then clearly
10576 there's nothing we can do. */
10577 if (!COMPLETE_TYPE_P (pattern))
10579 /* We can try again later. */
10580 TYPE_BEING_DEFINED (type) = 0;
10581 return type;
10584 /* If we've recursively instantiated too many templates, stop. */
10585 if (! push_tinst_level (type))
10586 return type;
10588 /* We may be in the middle of deferred access check. Disable
10589 it now. */
10590 push_deferring_access_checks (dk_no_deferred);
10592 int saved_unevaluated_operand = cp_unevaluated_operand;
10593 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
10595 fn_context = decl_function_context (TYPE_MAIN_DECL (type));
10596 /* Also avoid push_to_top_level for a lambda in an NSDMI. */
10597 if (!fn_context && LAMBDA_TYPE_P (type) && TYPE_CLASS_SCOPE_P (type))
10598 fn_context = error_mark_node;
10599 if (!fn_context)
10600 push_to_top_level ();
10601 else
10603 cp_unevaluated_operand = 0;
10604 c_inhibit_evaluation_warnings = 0;
10606 /* Use #pragma pack from the template context. */
10607 saved_maximum_field_alignment = maximum_field_alignment;
10608 maximum_field_alignment = TYPE_PRECISION (pattern);
10610 SET_CLASSTYPE_INTERFACE_UNKNOWN (type);
10612 /* Set the input location to the most specialized template definition.
10613 This is needed if tsubsting causes an error. */
10614 typedecl = TYPE_MAIN_DECL (pattern);
10615 input_location = DECL_SOURCE_LOCATION (TYPE_NAME (type)) =
10616 DECL_SOURCE_LOCATION (typedecl);
10618 TYPE_PACKED (type) = TYPE_PACKED (pattern);
10619 SET_TYPE_ALIGN (type, TYPE_ALIGN (pattern));
10620 TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (pattern);
10621 CLASSTYPE_NON_AGGREGATE (type) = CLASSTYPE_NON_AGGREGATE (pattern);
10622 if (ANON_AGGR_TYPE_P (pattern))
10623 SET_ANON_AGGR_TYPE_P (type);
10624 if (CLASSTYPE_VISIBILITY_SPECIFIED (pattern))
10626 CLASSTYPE_VISIBILITY_SPECIFIED (type) = 1;
10627 CLASSTYPE_VISIBILITY (type) = CLASSTYPE_VISIBILITY (pattern);
10628 /* Adjust visibility for template arguments. */
10629 determine_visibility (TYPE_MAIN_DECL (type));
10631 if (CLASS_TYPE_P (type))
10632 CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (pattern);
10634 pbinfo = TYPE_BINFO (pattern);
10636 /* We should never instantiate a nested class before its enclosing
10637 class; we need to look up the nested class by name before we can
10638 instantiate it, and that lookup should instantiate the enclosing
10639 class. */
10640 gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
10641 || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
10643 base_list = NULL_TREE;
10644 if (BINFO_N_BASE_BINFOS (pbinfo))
10646 tree pbase_binfo;
10647 tree pushed_scope;
10648 int i;
10650 /* We must enter the scope containing the type, as that is where
10651 the accessibility of types named in dependent bases are
10652 looked up from. */
10653 pushed_scope = push_scope (CP_TYPE_CONTEXT (type));
10655 /* Substitute into each of the bases to determine the actual
10656 basetypes. */
10657 for (i = 0; BINFO_BASE_ITERATE (pbinfo, i, pbase_binfo); i++)
10659 tree base;
10660 tree access = BINFO_BASE_ACCESS (pbinfo, i);
10661 tree expanded_bases = NULL_TREE;
10662 int idx, len = 1;
10664 if (PACK_EXPANSION_P (BINFO_TYPE (pbase_binfo)))
10666 expanded_bases =
10667 tsubst_pack_expansion (BINFO_TYPE (pbase_binfo),
10668 args, tf_error, NULL_TREE);
10669 if (expanded_bases == error_mark_node)
10670 continue;
10672 len = TREE_VEC_LENGTH (expanded_bases);
10675 for (idx = 0; idx < len; idx++)
10677 if (expanded_bases)
10678 /* Extract the already-expanded base class. */
10679 base = TREE_VEC_ELT (expanded_bases, idx);
10680 else
10681 /* Substitute to figure out the base class. */
10682 base = tsubst (BINFO_TYPE (pbase_binfo), args, tf_error,
10683 NULL_TREE);
10685 if (base == error_mark_node)
10686 continue;
10688 base_list = tree_cons (access, base, base_list);
10689 if (BINFO_VIRTUAL_P (pbase_binfo))
10690 TREE_TYPE (base_list) = integer_type_node;
10694 /* The list is now in reverse order; correct that. */
10695 base_list = nreverse (base_list);
10697 if (pushed_scope)
10698 pop_scope (pushed_scope);
10700 /* Now call xref_basetypes to set up all the base-class
10701 information. */
10702 xref_basetypes (type, base_list);
10704 apply_late_template_attributes (&type, TYPE_ATTRIBUTES (pattern),
10705 (int) ATTR_FLAG_TYPE_IN_PLACE,
10706 args, tf_error, NULL_TREE);
10707 fixup_attribute_variants (type);
10709 /* Now that our base classes are set up, enter the scope of the
10710 class, so that name lookups into base classes, etc. will work
10711 correctly. This is precisely analogous to what we do in
10712 begin_class_definition when defining an ordinary non-template
10713 class, except we also need to push the enclosing classes. */
10714 push_nested_class (type);
10716 /* Now members are processed in the order of declaration. */
10717 for (member = CLASSTYPE_DECL_LIST (pattern);
10718 member; member = TREE_CHAIN (member))
10720 tree t = TREE_VALUE (member);
10722 if (TREE_PURPOSE (member))
10724 if (TYPE_P (t))
10726 if (LAMBDA_TYPE_P (t))
10727 /* A closure type for a lambda in an NSDMI or default argument.
10728 Ignore it; it will be regenerated when needed. */
10729 continue;
10731 /* Build new CLASSTYPE_NESTED_UTDS. */
10733 tree newtag;
10734 bool class_template_p;
10736 class_template_p = (TREE_CODE (t) != ENUMERAL_TYPE
10737 && TYPE_LANG_SPECIFIC (t)
10738 && CLASSTYPE_IS_TEMPLATE (t));
10739 /* If the member is a class template, then -- even after
10740 substitution -- there may be dependent types in the
10741 template argument list for the class. We increment
10742 PROCESSING_TEMPLATE_DECL so that dependent_type_p, as
10743 that function will assume that no types are dependent
10744 when outside of a template. */
10745 if (class_template_p)
10746 ++processing_template_decl;
10747 newtag = tsubst (t, args, tf_error, NULL_TREE);
10748 if (class_template_p)
10749 --processing_template_decl;
10750 if (newtag == error_mark_node)
10751 continue;
10753 if (TREE_CODE (newtag) != ENUMERAL_TYPE)
10755 tree name = TYPE_IDENTIFIER (t);
10757 if (class_template_p)
10758 /* Unfortunately, lookup_template_class sets
10759 CLASSTYPE_IMPLICIT_INSTANTIATION for a partial
10760 instantiation (i.e., for the type of a member
10761 template class nested within a template class.)
10762 This behavior is required for
10763 maybe_process_partial_specialization to work
10764 correctly, but is not accurate in this case;
10765 the TAG is not an instantiation of anything.
10766 (The corresponding TEMPLATE_DECL is an
10767 instantiation, but the TYPE is not.) */
10768 CLASSTYPE_USE_TEMPLATE (newtag) = 0;
10770 /* Now, we call pushtag to put this NEWTAG into the scope of
10771 TYPE. We first set up the IDENTIFIER_TYPE_VALUE to avoid
10772 pushtag calling push_template_decl. We don't have to do
10773 this for enums because it will already have been done in
10774 tsubst_enum. */
10775 if (name)
10776 SET_IDENTIFIER_TYPE_VALUE (name, newtag);
10777 pushtag (name, newtag, /*tag_scope=*/ts_current);
10780 else if (DECL_DECLARES_FUNCTION_P (t))
10782 tree r;
10784 if (TREE_CODE (t) == TEMPLATE_DECL)
10785 ++processing_template_decl;
10786 r = tsubst (t, args, tf_error, NULL_TREE);
10787 if (TREE_CODE (t) == TEMPLATE_DECL)
10788 --processing_template_decl;
10789 set_current_access_from_decl (r);
10790 finish_member_declaration (r);
10791 /* Instantiate members marked with attribute used. */
10792 if (r != error_mark_node && DECL_PRESERVE_P (r))
10793 mark_used (r);
10794 if (TREE_CODE (r) == FUNCTION_DECL
10795 && DECL_OMP_DECLARE_REDUCTION_P (r))
10796 cp_check_omp_declare_reduction (r);
10798 else if ((DECL_CLASS_TEMPLATE_P (t) || DECL_IMPLICIT_TYPEDEF_P (t))
10799 && LAMBDA_TYPE_P (TREE_TYPE (t)))
10800 /* A closure type for a lambda in an NSDMI or default argument.
10801 Ignore it; it will be regenerated when needed. */;
10802 else
10804 /* Build new TYPE_FIELDS. */
10805 if (TREE_CODE (t) == STATIC_ASSERT)
10807 tree condition;
10809 ++c_inhibit_evaluation_warnings;
10810 condition =
10811 tsubst_expr (STATIC_ASSERT_CONDITION (t), args,
10812 tf_warning_or_error, NULL_TREE,
10813 /*integral_constant_expression_p=*/true);
10814 --c_inhibit_evaluation_warnings;
10816 finish_static_assert (condition,
10817 STATIC_ASSERT_MESSAGE (t),
10818 STATIC_ASSERT_SOURCE_LOCATION (t),
10819 /*member_p=*/true);
10821 else if (TREE_CODE (t) != CONST_DECL)
10823 tree r;
10824 tree vec = NULL_TREE;
10825 int len = 1;
10827 /* The file and line for this declaration, to
10828 assist in error message reporting. Since we
10829 called push_tinst_level above, we don't need to
10830 restore these. */
10831 input_location = DECL_SOURCE_LOCATION (t);
10833 if (TREE_CODE (t) == TEMPLATE_DECL)
10834 ++processing_template_decl;
10835 r = tsubst (t, args, tf_warning_or_error, NULL_TREE);
10836 if (TREE_CODE (t) == TEMPLATE_DECL)
10837 --processing_template_decl;
10839 if (TREE_CODE (r) == TREE_VEC)
10841 /* A capture pack became multiple fields. */
10842 vec = r;
10843 len = TREE_VEC_LENGTH (vec);
10846 for (int i = 0; i < len; ++i)
10848 if (vec)
10849 r = TREE_VEC_ELT (vec, i);
10850 if (VAR_P (r))
10852 /* In [temp.inst]:
10854 [t]he initialization (and any associated
10855 side-effects) of a static data member does
10856 not occur unless the static data member is
10857 itself used in a way that requires the
10858 definition of the static data member to
10859 exist.
10861 Therefore, we do not substitute into the
10862 initialized for the static data member here. */
10863 finish_static_data_member_decl
10865 /*init=*/NULL_TREE,
10866 /*init_const_expr_p=*/false,
10867 /*asmspec_tree=*/NULL_TREE,
10868 /*flags=*/0);
10869 /* Instantiate members marked with attribute used. */
10870 if (r != error_mark_node && DECL_PRESERVE_P (r))
10871 mark_used (r);
10873 else if (TREE_CODE (r) == FIELD_DECL)
10875 /* Determine whether R has a valid type and can be
10876 completed later. If R is invalid, then its type
10877 is replaced by error_mark_node. */
10878 tree rtype = TREE_TYPE (r);
10879 if (can_complete_type_without_circularity (rtype))
10880 complete_type (rtype);
10882 if (!complete_or_array_type_p (rtype))
10884 /* If R's type couldn't be completed and
10885 it isn't a flexible array member (whose
10886 type is incomplete by definition) give
10887 an error. */
10888 cxx_incomplete_type_error (r, rtype);
10889 TREE_TYPE (r) = error_mark_node;
10893 /* If it is a TYPE_DECL for a class-scoped ENUMERAL_TYPE,
10894 such a thing will already have been added to the field
10895 list by tsubst_enum in finish_member_declaration in the
10896 CLASSTYPE_NESTED_UTDS case above. */
10897 if (!(TREE_CODE (r) == TYPE_DECL
10898 && TREE_CODE (TREE_TYPE (r)) == ENUMERAL_TYPE
10899 && DECL_ARTIFICIAL (r)))
10901 set_current_access_from_decl (r);
10902 finish_member_declaration (r);
10908 else
10910 if (TYPE_P (t) || DECL_CLASS_TEMPLATE_P (t)
10911 || DECL_TEMPLATE_TEMPLATE_PARM_P (t))
10913 /* Build new CLASSTYPE_FRIEND_CLASSES. */
10915 tree friend_type = t;
10916 bool adjust_processing_template_decl = false;
10918 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10920 /* template <class T> friend class C; */
10921 friend_type = tsubst_friend_class (friend_type, args);
10922 adjust_processing_template_decl = true;
10924 else if (TREE_CODE (friend_type) == UNBOUND_CLASS_TEMPLATE)
10926 /* template <class T> friend class C::D; */
10927 friend_type = tsubst (friend_type, args,
10928 tf_warning_or_error, NULL_TREE);
10929 if (TREE_CODE (friend_type) == TEMPLATE_DECL)
10930 friend_type = TREE_TYPE (friend_type);
10931 adjust_processing_template_decl = true;
10933 else if (TREE_CODE (friend_type) == TYPENAME_TYPE
10934 || TREE_CODE (friend_type) == TEMPLATE_TYPE_PARM)
10936 /* This could be either
10938 friend class T::C;
10940 when dependent_type_p is false or
10942 template <class U> friend class T::C;
10944 otherwise. */
10945 /* Bump processing_template_decl in case this is something like
10946 template <class T> friend struct A<T>::B. */
10947 ++processing_template_decl;
10948 friend_type = tsubst (friend_type, args,
10949 tf_warning_or_error, NULL_TREE);
10950 if (dependent_type_p (friend_type))
10951 adjust_processing_template_decl = true;
10952 --processing_template_decl;
10954 else if (TREE_CODE (friend_type) != BOUND_TEMPLATE_TEMPLATE_PARM
10955 && !CLASSTYPE_USE_TEMPLATE (friend_type)
10956 && TYPE_HIDDEN_P (friend_type))
10958 /* friend class C;
10960 where C hasn't been declared yet. Let's lookup name
10961 from namespace scope directly, bypassing any name that
10962 come from dependent base class. */
10963 tree ns = decl_namespace_context (TYPE_MAIN_DECL (friend_type));
10965 /* The call to xref_tag_from_type does injection for friend
10966 classes. */
10967 push_nested_namespace (ns);
10968 friend_type =
10969 xref_tag_from_type (friend_type, NULL_TREE,
10970 /*tag_scope=*/ts_current);
10971 pop_nested_namespace (ns);
10973 else if (uses_template_parms (friend_type))
10974 /* friend class C<T>; */
10975 friend_type = tsubst (friend_type, args,
10976 tf_warning_or_error, NULL_TREE);
10977 /* Otherwise it's
10979 friend class C;
10981 where C is already declared or
10983 friend class C<int>;
10985 We don't have to do anything in these cases. */
10987 if (adjust_processing_template_decl)
10988 /* Trick make_friend_class into realizing that the friend
10989 we're adding is a template, not an ordinary class. It's
10990 important that we use make_friend_class since it will
10991 perform some error-checking and output cross-reference
10992 information. */
10993 ++processing_template_decl;
10995 if (friend_type != error_mark_node)
10996 make_friend_class (type, friend_type, /*complain=*/false);
10998 if (adjust_processing_template_decl)
10999 --processing_template_decl;
11001 else
11003 /* Build new DECL_FRIENDLIST. */
11004 tree r;
11006 /* The file and line for this declaration, to
11007 assist in error message reporting. Since we
11008 called push_tinst_level above, we don't need to
11009 restore these. */
11010 input_location = DECL_SOURCE_LOCATION (t);
11012 if (TREE_CODE (t) == TEMPLATE_DECL)
11014 ++processing_template_decl;
11015 push_deferring_access_checks (dk_no_check);
11018 r = tsubst_friend_function (t, args);
11019 add_friend (type, r, /*complain=*/false);
11020 if (TREE_CODE (t) == TEMPLATE_DECL)
11022 pop_deferring_access_checks ();
11023 --processing_template_decl;
11029 if (fn_context)
11031 /* Restore these before substituting into the lambda capture
11032 initializers. */
11033 cp_unevaluated_operand = saved_unevaluated_operand;
11034 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
11037 /* Set the file and line number information to whatever is given for
11038 the class itself. This puts error messages involving generated
11039 implicit functions at a predictable point, and the same point
11040 that would be used for non-template classes. */
11041 input_location = DECL_SOURCE_LOCATION (typedecl);
11043 unreverse_member_declarations (type);
11044 finish_struct_1 (type);
11045 TYPE_BEING_DEFINED (type) = 0;
11047 /* We don't instantiate default arguments for member functions. 14.7.1:
11049 The implicit instantiation of a class template specialization causes
11050 the implicit instantiation of the declarations, but not of the
11051 definitions or default arguments, of the class member functions,
11052 member classes, static data members and member templates.... */
11054 /* Some typedefs referenced from within the template code need to be access
11055 checked at template instantiation time, i.e now. These types were
11056 added to the template at parsing time. Let's get those and perform
11057 the access checks then. */
11058 perform_typedefs_access_check (pattern, args);
11059 perform_deferred_access_checks (tf_warning_or_error);
11060 pop_nested_class ();
11061 maximum_field_alignment = saved_maximum_field_alignment;
11062 if (!fn_context)
11063 pop_from_top_level ();
11064 pop_deferring_access_checks ();
11065 pop_tinst_level ();
11067 /* The vtable for a template class can be emitted in any translation
11068 unit in which the class is instantiated. When there is no key
11069 method, however, finish_struct_1 will already have added TYPE to
11070 the keyed_classes. */
11071 if (TYPE_CONTAINS_VPTR_P (type) && CLASSTYPE_KEY_METHOD (type))
11072 vec_safe_push (keyed_classes, type);
11074 return type;
11077 /* Wrapper for instantiate_class_template_1. */
11079 tree
11080 instantiate_class_template (tree type)
11082 tree ret;
11083 timevar_push (TV_TEMPLATE_INST);
11084 ret = instantiate_class_template_1 (type);
11085 timevar_pop (TV_TEMPLATE_INST);
11086 return ret;
11089 static tree
11090 tsubst_template_arg (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11092 tree r;
11094 if (!t)
11095 r = t;
11096 else if (TYPE_P (t))
11097 r = tsubst (t, args, complain, in_decl);
11098 else
11100 if (!(complain & tf_warning))
11101 ++c_inhibit_evaluation_warnings;
11102 r = tsubst_expr (t, args, complain, in_decl,
11103 /*integral_constant_expression_p=*/true);
11104 if (!(complain & tf_warning))
11105 --c_inhibit_evaluation_warnings;
11107 return r;
11110 /* Given a function parameter pack TMPL_PARM and some function parameters
11111 instantiated from it at *SPEC_P, return a NONTYPE_ARGUMENT_PACK of them
11112 and set *SPEC_P to point at the next point in the list. */
11114 tree
11115 extract_fnparm_pack (tree tmpl_parm, tree *spec_p)
11117 /* Collect all of the extra "packed" parameters into an
11118 argument pack. */
11119 tree parmvec;
11120 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
11121 tree spec_parm = *spec_p;
11122 int i, len;
11124 for (len = 0; spec_parm; ++len, spec_parm = TREE_CHAIN (spec_parm))
11125 if (tmpl_parm
11126 && !function_parameter_expanded_from_pack_p (spec_parm, tmpl_parm))
11127 break;
11129 /* Fill in PARMVEC and PARMTYPEVEC with all of the parameters. */
11130 parmvec = make_tree_vec (len);
11131 spec_parm = *spec_p;
11132 for (i = 0; i < len; i++, spec_parm = DECL_CHAIN (spec_parm))
11134 tree elt = spec_parm;
11135 if (DECL_PACK_P (elt))
11136 elt = make_pack_expansion (elt);
11137 TREE_VEC_ELT (parmvec, i) = elt;
11140 /* Build the argument packs. */
11141 SET_ARGUMENT_PACK_ARGS (argpack, parmvec);
11142 *spec_p = spec_parm;
11144 return argpack;
11147 /* Give a chain SPEC_PARM of PARM_DECLs, pack them into a
11148 NONTYPE_ARGUMENT_PACK. */
11150 static tree
11151 make_fnparm_pack (tree spec_parm)
11153 return extract_fnparm_pack (NULL_TREE, &spec_parm);
11156 /* Return 1 if the Ith element of the argument pack ARG_PACK is a
11157 pack expansion with no extra args, 2 if it has extra args, or 0
11158 if it is not a pack expansion. */
11160 static int
11161 argument_pack_element_is_expansion_p (tree arg_pack, int i)
11163 tree vec = ARGUMENT_PACK_ARGS (arg_pack);
11164 if (i >= TREE_VEC_LENGTH (vec))
11165 return 0;
11166 tree elt = TREE_VEC_ELT (vec, i);
11167 if (DECL_P (elt))
11168 /* A decl pack is itself an expansion. */
11169 elt = TREE_TYPE (elt);
11170 if (!PACK_EXPANSION_P (elt))
11171 return 0;
11172 if (PACK_EXPANSION_EXTRA_ARGS (elt))
11173 return 2;
11174 return 1;
11178 /* Creates and return an ARGUMENT_PACK_SELECT tree node. */
11180 static tree
11181 make_argument_pack_select (tree arg_pack, unsigned index)
11183 tree aps = make_node (ARGUMENT_PACK_SELECT);
11185 ARGUMENT_PACK_SELECT_FROM_PACK (aps) = arg_pack;
11186 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11188 return aps;
11191 /* This is a subroutine of tsubst_pack_expansion.
11193 It returns TRUE if we need to use the PACK_EXPANSION_EXTRA_ARGS
11194 mechanism to store the (non complete list of) arguments of the
11195 substitution and return a non substituted pack expansion, in order
11196 to wait for when we have enough arguments to really perform the
11197 substitution. */
11199 static bool
11200 use_pack_expansion_extra_args_p (tree parm_packs,
11201 int arg_pack_len,
11202 bool has_empty_arg)
11204 /* If one pack has an expansion and another pack has a normal
11205 argument or if one pack has an empty argument and an another
11206 one hasn't then tsubst_pack_expansion cannot perform the
11207 substitution and need to fall back on the
11208 PACK_EXPANSION_EXTRA mechanism. */
11209 if (parm_packs == NULL_TREE)
11210 return false;
11211 else if (has_empty_arg)
11212 return true;
11214 bool has_expansion_arg = false;
11215 for (int i = 0 ; i < arg_pack_len; ++i)
11217 bool has_non_expansion_arg = false;
11218 for (tree parm_pack = parm_packs;
11219 parm_pack;
11220 parm_pack = TREE_CHAIN (parm_pack))
11222 tree arg = TREE_VALUE (parm_pack);
11224 int exp = argument_pack_element_is_expansion_p (arg, i);
11225 if (exp == 2)
11226 /* We can't substitute a pack expansion with extra args into
11227 our pattern. */
11228 return true;
11229 else if (exp)
11230 has_expansion_arg = true;
11231 else
11232 has_non_expansion_arg = true;
11235 if (has_expansion_arg && has_non_expansion_arg)
11236 return true;
11238 return false;
11241 /* [temp.variadic]/6 says that:
11243 The instantiation of a pack expansion [...]
11244 produces a list E1,E2, ..., En, where N is the number of elements
11245 in the pack expansion parameters.
11247 This subroutine of tsubst_pack_expansion produces one of these Ei.
11249 PATTERN is the pattern of the pack expansion. PARM_PACKS is a
11250 TREE_LIST in which each TREE_PURPOSE is a parameter pack of
11251 PATTERN, and each TREE_VALUE is its corresponding argument pack.
11252 INDEX is the index 'i' of the element Ei to produce. ARGS,
11253 COMPLAIN, and IN_DECL are the same parameters as for the
11254 tsubst_pack_expansion function.
11256 The function returns the resulting Ei upon successful completion,
11257 or error_mark_node.
11259 Note that this function possibly modifies the ARGS parameter, so
11260 it's the responsibility of the caller to restore it. */
11262 static tree
11263 gen_elem_of_pack_expansion_instantiation (tree pattern,
11264 tree parm_packs,
11265 unsigned index,
11266 tree args /* This parm gets
11267 modified. */,
11268 tsubst_flags_t complain,
11269 tree in_decl)
11271 tree t;
11272 bool ith_elem_is_expansion = false;
11274 /* For each parameter pack, change the substitution of the parameter
11275 pack to the ith argument in its argument pack, then expand the
11276 pattern. */
11277 for (tree pack = parm_packs; pack; pack = TREE_CHAIN (pack))
11279 tree parm = TREE_PURPOSE (pack);
11280 tree arg_pack = TREE_VALUE (pack);
11281 tree aps; /* instance of ARGUMENT_PACK_SELECT. */
11283 ith_elem_is_expansion |=
11284 argument_pack_element_is_expansion_p (arg_pack, index);
11286 /* Select the Ith argument from the pack. */
11287 if (TREE_CODE (parm) == PARM_DECL
11288 || VAR_P (parm)
11289 || TREE_CODE (parm) == FIELD_DECL)
11291 if (index == 0)
11293 aps = make_argument_pack_select (arg_pack, index);
11294 if (!mark_used (parm, complain) && !(complain & tf_error))
11295 return error_mark_node;
11296 register_local_specialization (aps, parm);
11298 else
11299 aps = retrieve_local_specialization (parm);
11301 else
11303 int idx, level;
11304 template_parm_level_and_index (parm, &level, &idx);
11306 if (index == 0)
11308 aps = make_argument_pack_select (arg_pack, index);
11309 /* Update the corresponding argument. */
11310 TMPL_ARG (args, level, idx) = aps;
11312 else
11313 /* Re-use the ARGUMENT_PACK_SELECT. */
11314 aps = TMPL_ARG (args, level, idx);
11316 ARGUMENT_PACK_SELECT_INDEX (aps) = index;
11319 /* Substitute into the PATTERN with the (possibly altered)
11320 arguments. */
11321 if (pattern == in_decl)
11322 /* Expanding a fixed parameter pack from
11323 coerce_template_parameter_pack. */
11324 t = tsubst_decl (pattern, args, complain);
11325 else if (pattern == error_mark_node)
11326 t = error_mark_node;
11327 else if (constraint_p (pattern))
11329 if (processing_template_decl)
11330 t = tsubst_constraint (pattern, args, complain, in_decl);
11331 else
11332 t = (constraints_satisfied_p (pattern, args)
11333 ? boolean_true_node : boolean_false_node);
11335 else if (!TYPE_P (pattern))
11336 t = tsubst_expr (pattern, args, complain, in_decl,
11337 /*integral_constant_expression_p=*/false);
11338 else
11339 t = tsubst (pattern, args, complain, in_decl);
11341 /* If the Ith argument pack element is a pack expansion, then
11342 the Ith element resulting from the substituting is going to
11343 be a pack expansion as well. */
11344 if (ith_elem_is_expansion)
11345 t = make_pack_expansion (t, complain);
11347 return t;
11350 /* When the unexpanded parameter pack in a fold expression expands to an empty
11351 sequence, the value of the expression is as follows; the program is
11352 ill-formed if the operator is not listed in this table.
11354 && true
11355 || false
11356 , void() */
11358 tree
11359 expand_empty_fold (tree t, tsubst_flags_t complain)
11361 tree_code code = (tree_code)TREE_INT_CST_LOW (TREE_OPERAND (t, 0));
11362 if (!FOLD_EXPR_MODIFY_P (t))
11363 switch (code)
11365 case TRUTH_ANDIF_EXPR:
11366 return boolean_true_node;
11367 case TRUTH_ORIF_EXPR:
11368 return boolean_false_node;
11369 case COMPOUND_EXPR:
11370 return void_node;
11371 default:
11372 break;
11375 if (complain & tf_error)
11376 error_at (location_of (t),
11377 "fold of empty expansion over %O", code);
11378 return error_mark_node;
11381 /* Given a fold-expression T and a current LEFT and RIGHT operand,
11382 form an expression that combines the two terms using the
11383 operator of T. */
11385 static tree
11386 fold_expression (tree t, tree left, tree right, tsubst_flags_t complain)
11388 tree op = FOLD_EXPR_OP (t);
11389 tree_code code = (tree_code)TREE_INT_CST_LOW (op);
11391 // Handle compound assignment operators.
11392 if (FOLD_EXPR_MODIFY_P (t))
11393 return build_x_modify_expr (input_location, left, code, right, complain);
11395 switch (code)
11397 case COMPOUND_EXPR:
11398 return build_x_compound_expr (input_location, left, right, complain);
11399 case DOTSTAR_EXPR:
11400 return build_m_component_ref (left, right, complain);
11401 default:
11402 return build_x_binary_op (input_location, code,
11403 left, TREE_CODE (left),
11404 right, TREE_CODE (right),
11405 /*overload=*/NULL,
11406 complain);
11410 /* Substitute ARGS into the pack of a fold expression T. */
11412 static inline tree
11413 tsubst_fold_expr_pack (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11415 return tsubst_pack_expansion (FOLD_EXPR_PACK (t), args, complain, in_decl);
11418 /* Substitute ARGS into the pack of a fold expression T. */
11420 static inline tree
11421 tsubst_fold_expr_init (tree t, tree args, tsubst_flags_t complain, tree in_decl)
11423 return tsubst_expr (FOLD_EXPR_INIT (t), args, complain, in_decl, false);
11426 /* Expand a PACK of arguments into a grouped as left fold.
11427 Given a pack containing elements A0, A1, ..., An and an
11428 operator @, this builds the expression:
11430 ((A0 @ A1) @ A2) ... @ An
11432 Note that PACK must not be empty.
11434 The operator is defined by the original fold expression T. */
11436 static tree
11437 expand_left_fold (tree t, tree pack, tsubst_flags_t complain)
11439 tree left = TREE_VEC_ELT (pack, 0);
11440 for (int i = 1; i < TREE_VEC_LENGTH (pack); ++i)
11442 tree right = TREE_VEC_ELT (pack, i);
11443 left = fold_expression (t, left, right, complain);
11445 return left;
11448 /* Substitute into a unary left fold expression. */
11450 static tree
11451 tsubst_unary_left_fold (tree t, tree args, tsubst_flags_t complain,
11452 tree in_decl)
11454 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11455 if (pack == error_mark_node)
11456 return error_mark_node;
11457 if (PACK_EXPANSION_P (pack))
11459 tree r = copy_node (t);
11460 FOLD_EXPR_PACK (r) = pack;
11461 return r;
11463 if (TREE_VEC_LENGTH (pack) == 0)
11464 return expand_empty_fold (t, complain);
11465 else
11466 return expand_left_fold (t, pack, complain);
11469 /* Substitute into a binary left fold expression.
11471 Do ths by building a single (non-empty) vector of argumnts and
11472 building the expression from those elements. */
11474 static tree
11475 tsubst_binary_left_fold (tree t, tree args, tsubst_flags_t complain,
11476 tree in_decl)
11478 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11479 if (pack == error_mark_node)
11480 return error_mark_node;
11481 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11482 if (init == error_mark_node)
11483 return error_mark_node;
11485 if (PACK_EXPANSION_P (pack))
11487 tree r = copy_node (t);
11488 FOLD_EXPR_PACK (r) = pack;
11489 FOLD_EXPR_INIT (r) = init;
11490 return r;
11493 tree vec = make_tree_vec (TREE_VEC_LENGTH (pack) + 1);
11494 TREE_VEC_ELT (vec, 0) = init;
11495 for (int i = 0; i < TREE_VEC_LENGTH (pack); ++i)
11496 TREE_VEC_ELT (vec, i + 1) = TREE_VEC_ELT (pack, i);
11498 return expand_left_fold (t, vec, complain);
11501 /* Expand a PACK of arguments into a grouped as right fold.
11502 Given a pack containing elementns A0, A1, ..., and an
11503 operator @, this builds the expression:
11505 A0@ ... (An-2 @ (An-1 @ An))
11507 Note that PACK must not be empty.
11509 The operator is defined by the original fold expression T. */
11511 tree
11512 expand_right_fold (tree t, tree pack, tsubst_flags_t complain)
11514 // Build the expression.
11515 int n = TREE_VEC_LENGTH (pack);
11516 tree right = TREE_VEC_ELT (pack, n - 1);
11517 for (--n; n != 0; --n)
11519 tree left = TREE_VEC_ELT (pack, n - 1);
11520 right = fold_expression (t, left, right, complain);
11522 return right;
11525 /* Substitute into a unary right fold expression. */
11527 static tree
11528 tsubst_unary_right_fold (tree t, tree args, tsubst_flags_t complain,
11529 tree in_decl)
11531 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11532 if (pack == error_mark_node)
11533 return error_mark_node;
11534 if (PACK_EXPANSION_P (pack))
11536 tree r = copy_node (t);
11537 FOLD_EXPR_PACK (r) = pack;
11538 return r;
11540 if (TREE_VEC_LENGTH (pack) == 0)
11541 return expand_empty_fold (t, complain);
11542 else
11543 return expand_right_fold (t, pack, complain);
11546 /* Substitute into a binary right fold expression.
11548 Do ths by building a single (non-empty) vector of arguments and
11549 building the expression from those elements. */
11551 static tree
11552 tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
11553 tree in_decl)
11555 tree pack = tsubst_fold_expr_pack (t, args, complain, in_decl);
11556 if (pack == error_mark_node)
11557 return error_mark_node;
11558 tree init = tsubst_fold_expr_init (t, args, complain, in_decl);
11559 if (init == error_mark_node)
11560 return error_mark_node;
11562 if (PACK_EXPANSION_P (pack))
11564 tree r = copy_node (t);
11565 FOLD_EXPR_PACK (r) = pack;
11566 FOLD_EXPR_INIT (r) = init;
11567 return r;
11570 int n = TREE_VEC_LENGTH (pack);
11571 tree vec = make_tree_vec (n + 1);
11572 for (int i = 0; i < n; ++i)
11573 TREE_VEC_ELT (vec, i) = TREE_VEC_ELT (pack, i);
11574 TREE_VEC_ELT (vec, n) = init;
11576 return expand_right_fold (t, vec, complain);
11579 /* Walk through the pattern of a pack expansion, adding everything in
11580 local_specializations to a list. */
11582 struct el_data
11584 tree extra;
11585 tsubst_flags_t complain;
11587 static tree
11588 extract_locals_r (tree *tp, int */*walk_subtrees*/, void *data_)
11590 el_data &data = *reinterpret_cast<el_data*>(data_);
11591 tree *extra = &data.extra;
11592 tsubst_flags_t complain = data.complain;
11593 if (tree spec = retrieve_local_specialization (*tp))
11595 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
11597 /* Maybe pull out the PARM_DECL for a partial instantiation. */
11598 tree args = ARGUMENT_PACK_ARGS (spec);
11599 if (TREE_VEC_LENGTH (args) == 1)
11601 tree elt = TREE_VEC_ELT (args, 0);
11602 if (PACK_EXPANSION_P (elt))
11603 elt = PACK_EXPANSION_PATTERN (elt);
11604 if (DECL_PACK_P (elt))
11605 spec = elt;
11607 if (TREE_CODE (spec) == NONTYPE_ARGUMENT_PACK)
11609 /* Handle lambda capture here, since we aren't doing any
11610 substitution now, and so tsubst_copy won't call
11611 process_outer_var_ref. */
11612 tree args = ARGUMENT_PACK_ARGS (spec);
11613 int len = TREE_VEC_LENGTH (args);
11614 for (int i = 0; i < len; ++i)
11616 tree arg = TREE_VEC_ELT (args, i);
11617 tree carg = arg;
11618 if (outer_automatic_var_p (arg))
11619 carg = process_outer_var_ref (arg, complain);
11620 if (carg != arg)
11622 /* Make a new NONTYPE_ARGUMENT_PACK of the capture
11623 proxies. */
11624 if (i == 0)
11626 spec = copy_node (spec);
11627 args = copy_node (args);
11628 SET_ARGUMENT_PACK_ARGS (spec, args);
11629 register_local_specialization (spec, *tp);
11631 TREE_VEC_ELT (args, i) = carg;
11636 if (outer_automatic_var_p (spec))
11637 spec = process_outer_var_ref (spec, complain);
11638 *extra = tree_cons (*tp, spec, *extra);
11640 return NULL_TREE;
11642 static tree
11643 extract_local_specs (tree pattern, tsubst_flags_t complain)
11645 el_data data = { NULL_TREE, complain };
11646 cp_walk_tree_without_duplicates (&pattern, extract_locals_r, &data);
11647 return data.extra;
11650 /* Substitute ARGS into T, which is an pack expansion
11651 (i.e. TYPE_PACK_EXPANSION or EXPR_PACK_EXPANSION). Returns a
11652 TREE_VEC with the substituted arguments, a PACK_EXPANSION_* node
11653 (if only a partial substitution could be performed) or
11654 ERROR_MARK_NODE if there was an error. */
11655 tree
11656 tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
11657 tree in_decl)
11659 tree pattern;
11660 tree pack, packs = NULL_TREE;
11661 bool unsubstituted_packs = false;
11662 bool unsubstituted_fn_pack = false;
11663 int i, len = -1;
11664 tree result;
11665 hash_map<tree, tree> *saved_local_specializations = NULL;
11666 bool need_local_specializations = false;
11667 int levels;
11669 gcc_assert (PACK_EXPANSION_P (t));
11670 pattern = PACK_EXPANSION_PATTERN (t);
11672 /* Add in any args remembered from an earlier partial instantiation. */
11673 tree extra = PACK_EXPANSION_EXTRA_ARGS (t);
11674 if (extra && TREE_CODE (extra) == TREE_LIST)
11676 for (tree elt = TREE_CHAIN (extra); elt; elt = TREE_CHAIN (elt))
11678 /* The partial instantiation involved local declarations collected in
11679 extract_local_specs; map from the general template to our local
11680 context. */
11681 tree gen = TREE_PURPOSE (elt);
11682 tree inst = TREE_VALUE (elt);
11683 if (DECL_P (inst))
11684 if (tree local = retrieve_local_specialization (inst))
11685 inst = local;
11686 /* else inst is already a full instantiation of the pack. */
11687 register_local_specialization (inst, gen);
11689 gcc_assert (!TREE_PURPOSE (extra));
11690 extra = TREE_VALUE (extra);
11692 args = add_to_template_args (extra, args);
11694 levels = TMPL_ARGS_DEPTH (args);
11696 /* Determine the argument packs that will instantiate the parameter
11697 packs used in the expansion expression. While we're at it,
11698 compute the number of arguments to be expanded and make sure it
11699 is consistent. */
11700 for (pack = PACK_EXPANSION_PARAMETER_PACKS (t); pack;
11701 pack = TREE_CHAIN (pack))
11703 tree parm_pack = TREE_VALUE (pack);
11704 tree arg_pack = NULL_TREE;
11705 tree orig_arg = NULL_TREE;
11706 int level = 0;
11708 if (TREE_CODE (parm_pack) == BASES)
11710 gcc_assert (parm_pack == pattern);
11711 if (BASES_DIRECT (parm_pack))
11712 return calculate_direct_bases (tsubst_expr (BASES_TYPE (parm_pack),
11713 args, complain, in_decl, false));
11714 else
11715 return calculate_bases (tsubst_expr (BASES_TYPE (parm_pack),
11716 args, complain, in_decl, false));
11718 else if (builtin_pack_call_p (parm_pack))
11720 /* ??? Support use in other patterns. */
11721 gcc_assert (parm_pack == pattern);
11722 return expand_builtin_pack_call (parm_pack, args,
11723 complain, in_decl);
11725 else if (TREE_CODE (parm_pack) == PARM_DECL)
11727 /* We know we have correct local_specializations if this
11728 expansion is at function scope, or if we're dealing with a
11729 local parameter in a requires expression; for the latter,
11730 tsubst_requires_expr set it up appropriately. */
11731 if (PACK_EXPANSION_LOCAL_P (t) || CONSTRAINT_VAR_P (parm_pack))
11732 arg_pack = retrieve_local_specialization (parm_pack);
11733 else
11734 /* We can't rely on local_specializations for a parameter
11735 name used later in a function declaration (such as in a
11736 late-specified return type). Even if it exists, it might
11737 have the wrong value for a recursive call. */
11738 need_local_specializations = true;
11740 if (!arg_pack)
11742 /* This parameter pack was used in an unevaluated context. Just
11743 make a dummy decl, since it's only used for its type. */
11744 ++cp_unevaluated_operand;
11745 arg_pack = tsubst_decl (parm_pack, args, complain);
11746 --cp_unevaluated_operand;
11747 if (arg_pack && DECL_PACK_P (arg_pack))
11748 /* Partial instantiation of the parm_pack, we can't build
11749 up an argument pack yet. */
11750 arg_pack = NULL_TREE;
11751 else
11752 arg_pack = make_fnparm_pack (arg_pack);
11754 else if (argument_pack_element_is_expansion_p (arg_pack, 0))
11755 /* This argument pack isn't fully instantiated yet. We set this
11756 flag rather than clear arg_pack because we do want to do the
11757 optimization below, and we don't want to substitute directly
11758 into the pattern (as that would expose a NONTYPE_ARGUMENT_PACK
11759 where it isn't expected). */
11760 unsubstituted_fn_pack = true;
11762 else if (is_normal_capture_proxy (parm_pack))
11764 arg_pack = retrieve_local_specialization (parm_pack);
11765 if (argument_pack_element_is_expansion_p (arg_pack, 0))
11766 unsubstituted_fn_pack = true;
11768 else
11770 int idx;
11771 template_parm_level_and_index (parm_pack, &level, &idx);
11773 if (level <= levels)
11774 arg_pack = TMPL_ARG (args, level, idx);
11777 orig_arg = arg_pack;
11778 if (arg_pack && TREE_CODE (arg_pack) == ARGUMENT_PACK_SELECT)
11779 arg_pack = ARGUMENT_PACK_SELECT_FROM_PACK (arg_pack);
11781 if (arg_pack && !ARGUMENT_PACK_P (arg_pack))
11782 /* This can only happen if we forget to expand an argument
11783 pack somewhere else. Just return an error, silently. */
11785 result = make_tree_vec (1);
11786 TREE_VEC_ELT (result, 0) = error_mark_node;
11787 return result;
11790 if (arg_pack)
11792 int my_len =
11793 TREE_VEC_LENGTH (ARGUMENT_PACK_ARGS (arg_pack));
11795 /* Don't bother trying to do a partial substitution with
11796 incomplete packs; we'll try again after deduction. */
11797 if (ARGUMENT_PACK_INCOMPLETE_P (arg_pack))
11798 return t;
11800 if (len < 0)
11801 len = my_len;
11802 else if (len != my_len
11803 && !unsubstituted_fn_pack)
11805 if (!(complain & tf_error))
11806 /* Fail quietly. */;
11807 else if (TREE_CODE (t) == TYPE_PACK_EXPANSION)
11808 error ("mismatched argument pack lengths while expanding %qT",
11809 pattern);
11810 else
11811 error ("mismatched argument pack lengths while expanding %qE",
11812 pattern);
11813 return error_mark_node;
11816 /* Keep track of the parameter packs and their corresponding
11817 argument packs. */
11818 packs = tree_cons (parm_pack, arg_pack, packs);
11819 TREE_TYPE (packs) = orig_arg;
11821 else
11823 /* We can't substitute for this parameter pack. We use a flag as
11824 well as the missing_level counter because function parameter
11825 packs don't have a level. */
11826 gcc_assert (processing_template_decl);
11827 unsubstituted_packs = true;
11831 /* If the expansion is just T..., return the matching argument pack, unless
11832 we need to call convert_from_reference on all the elements. This is an
11833 important optimization; see c++/68422. */
11834 if (!unsubstituted_packs
11835 && TREE_PURPOSE (packs) == pattern)
11837 tree args = ARGUMENT_PACK_ARGS (TREE_VALUE (packs));
11839 /* If the argument pack is a single pack expansion, pull it out. */
11840 if (TREE_VEC_LENGTH (args) == 1
11841 && pack_expansion_args_count (args))
11842 return TREE_VEC_ELT (args, 0);
11844 /* Types need no adjustment, nor does sizeof..., and if we still have
11845 some pack expansion args we won't do anything yet. */
11846 if (TREE_CODE (t) == TYPE_PACK_EXPANSION
11847 || PACK_EXPANSION_SIZEOF_P (t)
11848 || pack_expansion_args_count (args))
11849 return args;
11850 /* Also optimize expression pack expansions if we can tell that the
11851 elements won't have reference type. */
11852 tree type = TREE_TYPE (pattern);
11853 if (type && TREE_CODE (type) != REFERENCE_TYPE
11854 && !PACK_EXPANSION_P (type)
11855 && !WILDCARD_TYPE_P (type))
11856 return args;
11857 /* Otherwise use the normal path so we get convert_from_reference. */
11860 /* We cannot expand this expansion expression, because we don't have
11861 all of the argument packs we need. */
11862 if (use_pack_expansion_extra_args_p (packs, len, unsubstituted_packs))
11864 /* We got some full packs, but we can't substitute them in until we
11865 have values for all the packs. So remember these until then. */
11867 t = make_pack_expansion (pattern, complain);
11868 tree extra = args;
11869 if (local_specializations)
11870 if (tree locals = extract_local_specs (pattern, complain))
11871 extra = tree_cons (NULL_TREE, extra, locals);
11872 PACK_EXPANSION_EXTRA_ARGS (t) = extra;
11873 return t;
11875 else if (unsubstituted_packs)
11877 /* There were no real arguments, we're just replacing a parameter
11878 pack with another version of itself. Substitute into the
11879 pattern and return a PACK_EXPANSION_*. The caller will need to
11880 deal with that. */
11881 if (TREE_CODE (t) == EXPR_PACK_EXPANSION)
11882 t = tsubst_expr (pattern, args, complain, in_decl,
11883 /*integral_constant_expression_p=*/false);
11884 else
11885 t = tsubst (pattern, args, complain, in_decl);
11886 t = make_pack_expansion (t, complain);
11887 return t;
11890 gcc_assert (len >= 0);
11892 if (need_local_specializations)
11894 /* We're in a late-specified return type, so create our own local
11895 specializations map; the current map is either NULL or (in the
11896 case of recursive unification) might have bindings that we don't
11897 want to use or alter. */
11898 saved_local_specializations = local_specializations;
11899 local_specializations = new hash_map<tree, tree>;
11902 /* For each argument in each argument pack, substitute into the
11903 pattern. */
11904 result = make_tree_vec (len);
11905 tree elem_args = copy_template_args (args);
11906 for (i = 0; i < len; ++i)
11908 t = gen_elem_of_pack_expansion_instantiation (pattern, packs,
11910 elem_args, complain,
11911 in_decl);
11912 TREE_VEC_ELT (result, i) = t;
11913 if (t == error_mark_node)
11915 result = error_mark_node;
11916 break;
11920 /* Update ARGS to restore the substitution from parameter packs to
11921 their argument packs. */
11922 for (pack = packs; pack; pack = TREE_CHAIN (pack))
11924 tree parm = TREE_PURPOSE (pack);
11926 if (TREE_CODE (parm) == PARM_DECL
11927 || VAR_P (parm)
11928 || TREE_CODE (parm) == FIELD_DECL)
11929 register_local_specialization (TREE_TYPE (pack), parm);
11930 else
11932 int idx, level;
11934 if (TREE_VALUE (pack) == NULL_TREE)
11935 continue;
11937 template_parm_level_and_index (parm, &level, &idx);
11939 /* Update the corresponding argument. */
11940 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
11941 TREE_VEC_ELT (TREE_VEC_ELT (args, level -1 ), idx) =
11942 TREE_TYPE (pack);
11943 else
11944 TREE_VEC_ELT (args, idx) = TREE_TYPE (pack);
11948 if (need_local_specializations)
11950 delete local_specializations;
11951 local_specializations = saved_local_specializations;
11954 /* If the dependent pack arguments were such that we end up with only a
11955 single pack expansion again, there's no need to keep it in a TREE_VEC. */
11956 if (len == 1 && TREE_CODE (result) == TREE_VEC
11957 && PACK_EXPANSION_P (TREE_VEC_ELT (result, 0)))
11958 return TREE_VEC_ELT (result, 0);
11960 return result;
11963 /* Given PARM_DECL PARM, find the corresponding PARM_DECL in the template
11964 TMPL. We do this using DECL_PARM_INDEX, which should work even with
11965 parameter packs; all parms generated from a function parameter pack will
11966 have the same DECL_PARM_INDEX. */
11968 tree
11969 get_pattern_parm (tree parm, tree tmpl)
11971 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
11972 tree patparm;
11974 if (DECL_ARTIFICIAL (parm))
11976 for (patparm = DECL_ARGUMENTS (pattern);
11977 patparm; patparm = DECL_CHAIN (patparm))
11978 if (DECL_ARTIFICIAL (patparm)
11979 && DECL_NAME (parm) == DECL_NAME (patparm))
11980 break;
11982 else
11984 patparm = FUNCTION_FIRST_USER_PARM (DECL_TEMPLATE_RESULT (tmpl));
11985 patparm = chain_index (DECL_PARM_INDEX (parm)-1, patparm);
11986 gcc_assert (DECL_PARM_INDEX (patparm)
11987 == DECL_PARM_INDEX (parm));
11990 return patparm;
11993 /* Make an argument pack out of the TREE_VEC VEC. */
11995 static tree
11996 make_argument_pack (tree vec)
11998 tree pack;
11999 tree elt = TREE_VEC_ELT (vec, 0);
12000 if (TYPE_P (elt))
12001 pack = cxx_make_type (TYPE_ARGUMENT_PACK);
12002 else
12004 pack = make_node (NONTYPE_ARGUMENT_PACK);
12005 TREE_CONSTANT (pack) = 1;
12007 SET_ARGUMENT_PACK_ARGS (pack, vec);
12008 return pack;
12011 /* Return an exact copy of template args T that can be modified
12012 independently. */
12014 static tree
12015 copy_template_args (tree t)
12017 if (t == error_mark_node)
12018 return t;
12020 int len = TREE_VEC_LENGTH (t);
12021 tree new_vec = make_tree_vec (len);
12023 for (int i = 0; i < len; ++i)
12025 tree elt = TREE_VEC_ELT (t, i);
12026 if (elt && TREE_CODE (elt) == TREE_VEC)
12027 elt = copy_template_args (elt);
12028 TREE_VEC_ELT (new_vec, i) = elt;
12031 NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_vec)
12032 = NON_DEFAULT_TEMPLATE_ARGS_COUNT (t);
12034 return new_vec;
12037 /* Substitute ARGS into the vector or list of template arguments T. */
12039 static tree
12040 tsubst_template_args (tree t, tree args, tsubst_flags_t complain, tree in_decl)
12042 tree orig_t = t;
12043 int len, need_new = 0, i, expanded_len_adjust = 0, out;
12044 tree *elts;
12046 if (t == error_mark_node)
12047 return error_mark_node;
12049 len = TREE_VEC_LENGTH (t);
12050 elts = XALLOCAVEC (tree, len);
12052 for (i = 0; i < len; i++)
12054 tree orig_arg = TREE_VEC_ELT (t, i);
12055 tree new_arg;
12057 if (TREE_CODE (orig_arg) == TREE_VEC)
12058 new_arg = tsubst_template_args (orig_arg, args, complain, in_decl);
12059 else if (PACK_EXPANSION_P (orig_arg))
12061 /* Substitute into an expansion expression. */
12062 new_arg = tsubst_pack_expansion (orig_arg, args, complain, in_decl);
12064 if (TREE_CODE (new_arg) == TREE_VEC)
12065 /* Add to the expanded length adjustment the number of
12066 expanded arguments. We subtract one from this
12067 measurement, because the argument pack expression
12068 itself is already counted as 1 in
12069 LEN. EXPANDED_LEN_ADJUST can actually be negative, if
12070 the argument pack is empty. */
12071 expanded_len_adjust += TREE_VEC_LENGTH (new_arg) - 1;
12073 else if (ARGUMENT_PACK_P (orig_arg))
12075 /* Substitute into each of the arguments. */
12076 new_arg = TYPE_P (orig_arg)
12077 ? cxx_make_type (TREE_CODE (orig_arg))
12078 : make_node (TREE_CODE (orig_arg));
12080 tree pack_args = tsubst_template_args (ARGUMENT_PACK_ARGS (orig_arg),
12081 args, complain, in_decl);
12082 if (pack_args == error_mark_node)
12083 new_arg = error_mark_node;
12084 else
12085 SET_ARGUMENT_PACK_ARGS (new_arg, pack_args);
12087 if (TREE_CODE (new_arg) == NONTYPE_ARGUMENT_PACK)
12088 TREE_CONSTANT (new_arg) = TREE_CONSTANT (orig_arg);
12090 else
12091 new_arg = tsubst_template_arg (orig_arg, args, complain, in_decl);
12093 if (new_arg == error_mark_node)
12094 return error_mark_node;
12096 elts[i] = new_arg;
12097 if (new_arg != orig_arg)
12098 need_new = 1;
12101 if (!need_new)
12102 return t;
12104 /* Make space for the expanded arguments coming from template
12105 argument packs. */
12106 t = make_tree_vec (len + expanded_len_adjust);
12107 /* ORIG_T can contain TREE_VECs. That happens if ORIG_T contains the
12108 arguments for a member template.
12109 In that case each TREE_VEC in ORIG_T represents a level of template
12110 arguments, and ORIG_T won't carry any non defaulted argument count.
12111 It will rather be the nested TREE_VECs that will carry one.
12112 In other words, ORIG_T carries a non defaulted argument count only
12113 if it doesn't contain any nested TREE_VEC. */
12114 if (NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t))
12116 int count = GET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (orig_t);
12117 count += expanded_len_adjust;
12118 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (t, count);
12120 for (i = 0, out = 0; i < len; i++)
12122 if ((PACK_EXPANSION_P (TREE_VEC_ELT (orig_t, i))
12123 || ARGUMENT_PACK_P (TREE_VEC_ELT (orig_t, i)))
12124 && TREE_CODE (elts[i]) == TREE_VEC)
12126 int idx;
12128 /* Now expand the template argument pack "in place". */
12129 for (idx = 0; idx < TREE_VEC_LENGTH (elts[i]); idx++, out++)
12130 TREE_VEC_ELT (t, out) = TREE_VEC_ELT (elts[i], idx);
12132 else
12134 TREE_VEC_ELT (t, out) = elts[i];
12135 out++;
12139 return t;
12142 /* Substitute ARGS into one level PARMS of template parameters. */
12144 static tree
12145 tsubst_template_parms_level (tree parms, tree args, tsubst_flags_t complain)
12147 if (parms == error_mark_node)
12148 return error_mark_node;
12150 tree new_vec = make_tree_vec (TREE_VEC_LENGTH (parms));
12152 for (int i = 0; i < TREE_VEC_LENGTH (new_vec); ++i)
12154 tree tuple = TREE_VEC_ELT (parms, i);
12156 if (tuple == error_mark_node)
12157 continue;
12159 TREE_VEC_ELT (new_vec, i) =
12160 tsubst_template_parm (tuple, args, complain);
12163 return new_vec;
12166 /* Return the result of substituting ARGS into the template parameters
12167 given by PARMS. If there are m levels of ARGS and m + n levels of
12168 PARMS, then the result will contain n levels of PARMS. For
12169 example, if PARMS is `template <class T> template <class U>
12170 template <T*, U, class V>' and ARGS is {{int}, {double}} then the
12171 result will be `template <int*, double, class V>'. */
12173 static tree
12174 tsubst_template_parms (tree parms, tree args, tsubst_flags_t complain)
12176 tree r = NULL_TREE;
12177 tree* new_parms;
12179 /* When substituting into a template, we must set
12180 PROCESSING_TEMPLATE_DECL as the template parameters may be
12181 dependent if they are based on one-another, and the dependency
12182 predicates are short-circuit outside of templates. */
12183 ++processing_template_decl;
12185 for (new_parms = &r;
12186 parms && TMPL_PARMS_DEPTH (parms) > TMPL_ARGS_DEPTH (args);
12187 new_parms = &(TREE_CHAIN (*new_parms)),
12188 parms = TREE_CHAIN (parms))
12190 tree new_vec = tsubst_template_parms_level (TREE_VALUE (parms),
12191 args, complain);
12192 *new_parms =
12193 tree_cons (size_int (TMPL_PARMS_DEPTH (parms)
12194 - TMPL_ARGS_DEPTH (args)),
12195 new_vec, NULL_TREE);
12198 --processing_template_decl;
12200 return r;
12203 /* Return the result of substituting ARGS into one template parameter
12204 given by T. T Must be a TREE_LIST which TREE_VALUE is the template
12205 parameter and which TREE_PURPOSE is the default argument of the
12206 template parameter. */
12208 static tree
12209 tsubst_template_parm (tree t, tree args, tsubst_flags_t complain)
12211 tree default_value, parm_decl;
12213 if (args == NULL_TREE
12214 || t == NULL_TREE
12215 || t == error_mark_node)
12216 return t;
12218 gcc_assert (TREE_CODE (t) == TREE_LIST);
12220 default_value = TREE_PURPOSE (t);
12221 parm_decl = TREE_VALUE (t);
12223 parm_decl = tsubst (parm_decl, args, complain, NULL_TREE);
12224 if (TREE_CODE (parm_decl) == PARM_DECL
12225 && invalid_nontype_parm_type_p (TREE_TYPE (parm_decl), complain))
12226 parm_decl = error_mark_node;
12227 default_value = tsubst_template_arg (default_value, args,
12228 complain, NULL_TREE);
12230 return build_tree_list (default_value, parm_decl);
12233 /* Substitute the ARGS into the indicated aggregate (or enumeration)
12234 type T. If T is not an aggregate or enumeration type, it is
12235 handled as if by tsubst. IN_DECL is as for tsubst. If
12236 ENTERING_SCOPE is nonzero, T is the context for a template which
12237 we are presently tsubst'ing. Return the substituted value. */
12239 static tree
12240 tsubst_aggr_type (tree t,
12241 tree args,
12242 tsubst_flags_t complain,
12243 tree in_decl,
12244 int entering_scope)
12246 if (t == NULL_TREE)
12247 return NULL_TREE;
12249 switch (TREE_CODE (t))
12251 case RECORD_TYPE:
12252 if (TYPE_PTRMEMFUNC_P (t))
12253 return tsubst (TYPE_PTRMEMFUNC_FN_TYPE (t), args, complain, in_decl);
12255 /* Fall through. */
12256 case ENUMERAL_TYPE:
12257 case UNION_TYPE:
12258 if (TYPE_TEMPLATE_INFO (t) && uses_template_parms (t))
12260 tree argvec;
12261 tree context;
12262 tree r;
12263 int saved_unevaluated_operand;
12264 int saved_inhibit_evaluation_warnings;
12266 /* In "sizeof(X<I>)" we need to evaluate "I". */
12267 saved_unevaluated_operand = cp_unevaluated_operand;
12268 cp_unevaluated_operand = 0;
12269 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
12270 c_inhibit_evaluation_warnings = 0;
12272 /* First, determine the context for the type we are looking
12273 up. */
12274 context = TYPE_CONTEXT (t);
12275 if (context && TYPE_P (context))
12277 context = tsubst_aggr_type (context, args, complain,
12278 in_decl, /*entering_scope=*/1);
12279 /* If context is a nested class inside a class template,
12280 it may still need to be instantiated (c++/33959). */
12281 context = complete_type (context);
12284 /* Then, figure out what arguments are appropriate for the
12285 type we are trying to find. For example, given:
12287 template <class T> struct S;
12288 template <class T, class U> void f(T, U) { S<U> su; }
12290 and supposing that we are instantiating f<int, double>,
12291 then our ARGS will be {int, double}, but, when looking up
12292 S we only want {double}. */
12293 argvec = tsubst_template_args (TYPE_TI_ARGS (t), args,
12294 complain, in_decl);
12295 if (argvec == error_mark_node)
12296 r = error_mark_node;
12297 else
12299 r = lookup_template_class (t, argvec, in_decl, context,
12300 entering_scope, complain);
12301 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
12304 cp_unevaluated_operand = saved_unevaluated_operand;
12305 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
12307 return r;
12309 else
12310 /* This is not a template type, so there's nothing to do. */
12311 return t;
12313 default:
12314 return tsubst (t, args, complain, in_decl);
12318 static GTY((cache)) tree_cache_map *defarg_inst;
12320 /* Substitute into the default argument ARG (a default argument for
12321 FN), which has the indicated TYPE. */
12323 tree
12324 tsubst_default_argument (tree fn, int parmnum, tree type, tree arg,
12325 tsubst_flags_t complain)
12327 tree saved_class_ptr = NULL_TREE;
12328 tree saved_class_ref = NULL_TREE;
12329 int errs = errorcount + sorrycount;
12331 /* This can happen in invalid code. */
12332 if (TREE_CODE (arg) == DEFAULT_ARG)
12333 return arg;
12335 tree parm = FUNCTION_FIRST_USER_PARM (fn);
12336 parm = chain_index (parmnum, parm);
12337 tree parmtype = TREE_TYPE (parm);
12338 if (DECL_BY_REFERENCE (parm))
12339 parmtype = TREE_TYPE (parmtype);
12340 if (parmtype == error_mark_node)
12341 return error_mark_node;
12343 gcc_assert (same_type_ignoring_top_level_qualifiers_p (type, parmtype));
12345 tree *slot;
12346 if (defarg_inst && (slot = defarg_inst->get (parm)))
12347 return *slot;
12349 /* This default argument came from a template. Instantiate the
12350 default argument here, not in tsubst. In the case of
12351 something like:
12353 template <class T>
12354 struct S {
12355 static T t();
12356 void f(T = t());
12359 we must be careful to do name lookup in the scope of S<T>,
12360 rather than in the current class. */
12361 push_access_scope (fn);
12362 /* The "this" pointer is not valid in a default argument. */
12363 if (cfun)
12365 saved_class_ptr = current_class_ptr;
12366 cp_function_chain->x_current_class_ptr = NULL_TREE;
12367 saved_class_ref = current_class_ref;
12368 cp_function_chain->x_current_class_ref = NULL_TREE;
12371 start_lambda_scope (parm);
12373 push_deferring_access_checks(dk_no_deferred);
12374 /* The default argument expression may cause implicitly defined
12375 member functions to be synthesized, which will result in garbage
12376 collection. We must treat this situation as if we were within
12377 the body of function so as to avoid collecting live data on the
12378 stack. */
12379 ++function_depth;
12380 arg = tsubst_expr (arg, DECL_TI_ARGS (fn),
12381 complain, NULL_TREE,
12382 /*integral_constant_expression_p=*/false);
12383 --function_depth;
12384 pop_deferring_access_checks();
12386 finish_lambda_scope ();
12388 /* Restore the "this" pointer. */
12389 if (cfun)
12391 cp_function_chain->x_current_class_ptr = saved_class_ptr;
12392 cp_function_chain->x_current_class_ref = saved_class_ref;
12395 if (errorcount+sorrycount > errs
12396 && (complain & tf_warning_or_error))
12397 inform (input_location,
12398 " when instantiating default argument for call to %qD", fn);
12400 /* Make sure the default argument is reasonable. */
12401 arg = check_default_argument (type, arg, complain);
12403 pop_access_scope (fn);
12405 if (arg != error_mark_node && !cp_unevaluated_operand)
12407 if (!defarg_inst)
12408 defarg_inst = tree_cache_map::create_ggc (37);
12409 defarg_inst->put (parm, arg);
12412 return arg;
12415 /* Substitute into all the default arguments for FN. */
12417 static void
12418 tsubst_default_arguments (tree fn, tsubst_flags_t complain)
12420 tree arg;
12421 tree tmpl_args;
12423 tmpl_args = DECL_TI_ARGS (fn);
12425 /* If this function is not yet instantiated, we certainly don't need
12426 its default arguments. */
12427 if (uses_template_parms (tmpl_args))
12428 return;
12429 /* Don't do this again for clones. */
12430 if (DECL_CLONED_FUNCTION_P (fn))
12431 return;
12433 int i = 0;
12434 for (arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
12435 arg;
12436 arg = TREE_CHAIN (arg), ++i)
12437 if (TREE_PURPOSE (arg))
12438 TREE_PURPOSE (arg) = tsubst_default_argument (fn, i,
12439 TREE_VALUE (arg),
12440 TREE_PURPOSE (arg),
12441 complain);
12444 /* Subroutine of tsubst_decl for the case when T is a FUNCTION_DECL. */
12446 static tree
12447 tsubst_function_decl (tree t, tree args, tsubst_flags_t complain,
12448 tree lambda_fntype)
12450 tree gen_tmpl, argvec;
12451 hashval_t hash = 0;
12452 tree in_decl = t;
12454 /* Nobody should be tsubst'ing into non-template functions. */
12455 gcc_assert (DECL_TEMPLATE_INFO (t) != NULL_TREE);
12457 if (TREE_CODE (DECL_TI_TEMPLATE (t)) == TEMPLATE_DECL)
12459 /* If T is not dependent, just return it. */
12460 if (!uses_template_parms (DECL_TI_ARGS (t)))
12461 return t;
12463 /* Calculate the most general template of which R is a
12464 specialization. */
12465 gen_tmpl = most_general_template (DECL_TI_TEMPLATE (t));
12467 /* We're substituting a lambda function under tsubst_lambda_expr but not
12468 directly from it; find the matching function we're already inside.
12469 But don't do this if T is a generic lambda with a single level of
12470 template parms, as in that case we're doing a normal instantiation. */
12471 if (LAMBDA_FUNCTION_P (t) && !lambda_fntype
12472 && (!generic_lambda_fn_p (t)
12473 || TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)) > 1))
12474 return enclosing_instantiation_of (t);
12476 /* Calculate the complete set of arguments used to
12477 specialize R. */
12478 argvec = tsubst_template_args (DECL_TI_ARGS
12479 (DECL_TEMPLATE_RESULT
12480 (DECL_TI_TEMPLATE (t))),
12481 args, complain, in_decl);
12482 if (argvec == error_mark_node)
12483 return error_mark_node;
12485 /* Check to see if we already have this specialization. */
12486 if (!lambda_fntype)
12488 hash = hash_tmpl_and_args (gen_tmpl, argvec);
12489 if (tree spec = retrieve_specialization (gen_tmpl, argvec, hash))
12490 return spec;
12493 /* We can see more levels of arguments than parameters if
12494 there was a specialization of a member template, like
12495 this:
12497 template <class T> struct S { template <class U> void f(); }
12498 template <> template <class U> void S<int>::f(U);
12500 Here, we'll be substituting into the specialization,
12501 because that's where we can find the code we actually
12502 want to generate, but we'll have enough arguments for
12503 the most general template.
12505 We also deal with the peculiar case:
12507 template <class T> struct S {
12508 template <class U> friend void f();
12510 template <class U> void f() {}
12511 template S<int>;
12512 template void f<double>();
12514 Here, the ARGS for the instantiation of will be {int,
12515 double}. But, we only need as many ARGS as there are
12516 levels of template parameters in CODE_PATTERN. We are
12517 careful not to get fooled into reducing the ARGS in
12518 situations like:
12520 template <class T> struct S { template <class U> void f(U); }
12521 template <class T> template <> void S<T>::f(int) {}
12523 which we can spot because the pattern will be a
12524 specialization in this case. */
12525 int args_depth = TMPL_ARGS_DEPTH (args);
12526 int parms_depth =
12527 TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (DECL_TI_TEMPLATE (t)));
12529 if (args_depth > parms_depth && !DECL_TEMPLATE_SPECIALIZATION (t))
12530 args = get_innermost_template_args (args, parms_depth);
12532 else
12534 /* This special case arises when we have something like this:
12536 template <class T> struct S {
12537 friend void f<int>(int, double);
12540 Here, the DECL_TI_TEMPLATE for the friend declaration
12541 will be an IDENTIFIER_NODE. We are being called from
12542 tsubst_friend_function, and we want only to create a
12543 new decl (R) with appropriate types so that we can call
12544 determine_specialization. */
12545 gen_tmpl = NULL_TREE;
12546 argvec = NULL_TREE;
12549 tree closure = (lambda_fntype ? TYPE_METHOD_BASETYPE (lambda_fntype)
12550 : NULL_TREE);
12551 tree ctx = closure ? closure : DECL_CONTEXT (t);
12552 bool member = ctx && TYPE_P (ctx);
12554 if (member && !closure)
12555 ctx = tsubst_aggr_type (ctx, args,
12556 complain, t, /*entering_scope=*/1);
12558 tree type = (lambda_fntype ? lambda_fntype
12559 : tsubst (TREE_TYPE (t), args,
12560 complain | tf_fndecl_type, in_decl));
12561 if (type == error_mark_node)
12562 return error_mark_node;
12564 /* If we hit excessive deduction depth, the type is bogus even if
12565 it isn't error_mark_node, so don't build a decl. */
12566 if (excessive_deduction_depth)
12567 return error_mark_node;
12569 /* We do NOT check for matching decls pushed separately at this
12570 point, as they may not represent instantiations of this
12571 template, and in any case are considered separate under the
12572 discrete model. */
12573 tree r = copy_decl (t);
12574 DECL_USE_TEMPLATE (r) = 0;
12575 TREE_TYPE (r) = type;
12576 /* Clear out the mangled name and RTL for the instantiation. */
12577 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
12578 SET_DECL_RTL (r, NULL);
12579 /* Leave DECL_INITIAL set on deleted instantiations. */
12580 if (!DECL_DELETED_FN (r))
12581 DECL_INITIAL (r) = NULL_TREE;
12582 DECL_CONTEXT (r) = ctx;
12584 /* OpenMP UDRs have the only argument a reference to the declared
12585 type. We want to diagnose if the declared type is a reference,
12586 which is invalid, but as references to references are usually
12587 quietly merged, diagnose it here. */
12588 if (DECL_OMP_DECLARE_REDUCTION_P (t))
12590 tree argtype
12591 = TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (t))));
12592 argtype = tsubst (argtype, args, complain, in_decl);
12593 if (TREE_CODE (argtype) == REFERENCE_TYPE)
12594 error_at (DECL_SOURCE_LOCATION (t),
12595 "reference type %qT in "
12596 "%<#pragma omp declare reduction%>", argtype);
12597 if (strchr (IDENTIFIER_POINTER (DECL_NAME (t)), '~') == NULL)
12598 DECL_NAME (r) = omp_reduction_id (ERROR_MARK, DECL_NAME (t),
12599 argtype);
12602 if (member && DECL_CONV_FN_P (r))
12603 /* Type-conversion operator. Reconstruct the name, in
12604 case it's the name of one of the template's parameters. */
12605 DECL_NAME (r) = make_conv_op_name (TREE_TYPE (type));
12607 tree parms = DECL_ARGUMENTS (t);
12608 if (closure)
12609 parms = DECL_CHAIN (parms);
12610 parms = tsubst (parms, args, complain, t);
12611 for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
12612 DECL_CONTEXT (parm) = r;
12613 if (closure)
12615 tree tparm = build_this_parm (r, closure, type_memfn_quals (type));
12616 DECL_CHAIN (tparm) = parms;
12617 parms = tparm;
12619 DECL_ARGUMENTS (r) = parms;
12620 DECL_RESULT (r) = NULL_TREE;
12622 TREE_STATIC (r) = 0;
12623 TREE_PUBLIC (r) = TREE_PUBLIC (t);
12624 DECL_EXTERNAL (r) = 1;
12625 /* If this is an instantiation of a function with internal
12626 linkage, we already know what object file linkage will be
12627 assigned to the instantiation. */
12628 DECL_INTERFACE_KNOWN (r) = !TREE_PUBLIC (r);
12629 DECL_DEFER_OUTPUT (r) = 0;
12630 DECL_CHAIN (r) = NULL_TREE;
12631 DECL_PENDING_INLINE_INFO (r) = 0;
12632 DECL_PENDING_INLINE_P (r) = 0;
12633 DECL_SAVED_TREE (r) = NULL_TREE;
12634 DECL_STRUCT_FUNCTION (r) = NULL;
12635 TREE_USED (r) = 0;
12636 /* We'll re-clone as appropriate in instantiate_template. */
12637 DECL_CLONED_FUNCTION (r) = NULL_TREE;
12639 /* If we aren't complaining now, return on error before we register
12640 the specialization so that we'll complain eventually. */
12641 if ((complain & tf_error) == 0
12642 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12643 && !grok_op_properties (r, /*complain=*/false))
12644 return error_mark_node;
12646 /* When instantiating a constrained member, substitute
12647 into the constraints to create a new constraint. */
12648 if (tree ci = get_constraints (t))
12649 if (member)
12651 ci = tsubst_constraint_info (ci, argvec, complain, NULL_TREE);
12652 set_constraints (r, ci);
12655 /* Set up the DECL_TEMPLATE_INFO for R. There's no need to do
12656 this in the special friend case mentioned above where
12657 GEN_TMPL is NULL. */
12658 if (gen_tmpl && !closure)
12660 DECL_TEMPLATE_INFO (r)
12661 = build_template_info (gen_tmpl, argvec);
12662 SET_DECL_IMPLICIT_INSTANTIATION (r);
12664 tree new_r
12665 = register_specialization (r, gen_tmpl, argvec, false, hash);
12666 if (new_r != r)
12667 /* We instantiated this while substituting into
12668 the type earlier (template/friend54.C). */
12669 return new_r;
12671 /* We're not supposed to instantiate default arguments
12672 until they are called, for a template. But, for a
12673 declaration like:
12675 template <class T> void f ()
12676 { extern void g(int i = T()); }
12678 we should do the substitution when the template is
12679 instantiated. We handle the member function case in
12680 instantiate_class_template since the default arguments
12681 might refer to other members of the class. */
12682 if (!member
12683 && !PRIMARY_TEMPLATE_P (gen_tmpl)
12684 && !uses_template_parms (argvec))
12685 tsubst_default_arguments (r, complain);
12687 else
12688 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12690 /* Copy the list of befriending classes. */
12691 for (tree *friends = &DECL_BEFRIENDING_CLASSES (r);
12692 *friends;
12693 friends = &TREE_CHAIN (*friends))
12695 *friends = copy_node (*friends);
12696 TREE_VALUE (*friends)
12697 = tsubst (TREE_VALUE (*friends), args, complain, in_decl);
12700 if (DECL_CONSTRUCTOR_P (r) || DECL_DESTRUCTOR_P (r))
12702 maybe_retrofit_in_chrg (r);
12703 if (DECL_CONSTRUCTOR_P (r) && !grok_ctor_properties (ctx, r))
12704 return error_mark_node;
12705 /* If this is an instantiation of a member template, clone it.
12706 If it isn't, that'll be handled by
12707 clone_constructors_and_destructors. */
12708 if (PRIMARY_TEMPLATE_P (gen_tmpl))
12709 clone_function_decl (r, /*update_methods=*/false);
12711 else if ((complain & tf_error) != 0
12712 && IDENTIFIER_ANY_OP_P (DECL_NAME (r))
12713 && !grok_op_properties (r, /*complain=*/true))
12714 return error_mark_node;
12716 if (DECL_FRIEND_P (t) && DECL_FRIEND_CONTEXT (t))
12717 SET_DECL_FRIEND_CONTEXT (r,
12718 tsubst (DECL_FRIEND_CONTEXT (t),
12719 args, complain, in_decl));
12721 /* Possibly limit visibility based on template args. */
12722 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
12723 if (DECL_VISIBILITY_SPECIFIED (t))
12725 DECL_VISIBILITY_SPECIFIED (r) = 0;
12726 DECL_ATTRIBUTES (r)
12727 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
12729 determine_visibility (r);
12730 if (DECL_DEFAULTED_OUTSIDE_CLASS_P (r)
12731 && !processing_template_decl)
12732 defaulted_late_check (r);
12734 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
12735 args, complain, in_decl);
12736 return r;
12739 /* Subroutine of tsubst_decl for the case when T is a TEMPLATE_DECL. */
12741 static tree
12742 tsubst_template_decl (tree t, tree args, tsubst_flags_t complain,
12743 tree lambda_fntype)
12745 /* We can get here when processing a member function template,
12746 member class template, or template template parameter. */
12747 tree decl = DECL_TEMPLATE_RESULT (t);
12748 tree in_decl = t;
12749 tree spec;
12750 tree tmpl_args;
12751 tree full_args;
12752 tree r;
12753 hashval_t hash = 0;
12755 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
12757 /* Template template parameter is treated here. */
12758 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12759 if (new_type == error_mark_node)
12760 r = error_mark_node;
12761 /* If we get a real template back, return it. This can happen in
12762 the context of most_specialized_partial_spec. */
12763 else if (TREE_CODE (new_type) == TEMPLATE_DECL)
12764 r = new_type;
12765 else
12766 /* The new TEMPLATE_DECL was built in
12767 reduce_template_parm_level. */
12768 r = TEMPLATE_TEMPLATE_PARM_TEMPLATE_DECL (new_type);
12769 return r;
12772 if (!lambda_fntype)
12774 /* We might already have an instance of this template.
12775 The ARGS are for the surrounding class type, so the
12776 full args contain the tsubst'd args for the context,
12777 plus the innermost args from the template decl. */
12778 tmpl_args = DECL_CLASS_TEMPLATE_P (t)
12779 ? CLASSTYPE_TI_ARGS (TREE_TYPE (t))
12780 : DECL_TI_ARGS (DECL_TEMPLATE_RESULT (t));
12781 /* Because this is a template, the arguments will still be
12782 dependent, even after substitution. If
12783 PROCESSING_TEMPLATE_DECL is not set, the dependency
12784 predicates will short-circuit. */
12785 ++processing_template_decl;
12786 full_args = tsubst_template_args (tmpl_args, args,
12787 complain, in_decl);
12788 --processing_template_decl;
12789 if (full_args == error_mark_node)
12790 return error_mark_node;
12792 /* If this is a default template template argument,
12793 tsubst might not have changed anything. */
12794 if (full_args == tmpl_args)
12795 return t;
12797 hash = hash_tmpl_and_args (t, full_args);
12798 spec = retrieve_specialization (t, full_args, hash);
12799 if (spec != NULL_TREE)
12800 return spec;
12803 /* Make a new template decl. It will be similar to the
12804 original, but will record the current template arguments.
12805 We also create a new function declaration, which is just
12806 like the old one, but points to this new template, rather
12807 than the old one. */
12808 r = copy_decl (t);
12809 gcc_assert (DECL_LANG_SPECIFIC (r) != 0);
12810 DECL_CHAIN (r) = NULL_TREE;
12812 // Build new template info linking to the original template decl.
12813 if (!lambda_fntype)
12815 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
12816 SET_DECL_IMPLICIT_INSTANTIATION (r);
12818 else
12819 DECL_TEMPLATE_INFO (r) = NULL_TREE;
12821 /* The template parameters for this new template are all the
12822 template parameters for the old template, except the
12823 outermost level of parameters. */
12824 DECL_TEMPLATE_PARMS (r)
12825 = tsubst_template_parms (DECL_TEMPLATE_PARMS (t), args,
12826 complain);
12828 if (TREE_CODE (decl) == TYPE_DECL
12829 && !TYPE_DECL_ALIAS_P (decl))
12831 tree new_type;
12832 ++processing_template_decl;
12833 new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
12834 --processing_template_decl;
12835 if (new_type == error_mark_node)
12836 return error_mark_node;
12838 TREE_TYPE (r) = new_type;
12839 /* For a partial specialization, we need to keep pointing to
12840 the primary template. */
12841 if (!DECL_TEMPLATE_SPECIALIZATION (t))
12842 CLASSTYPE_TI_TEMPLATE (new_type) = r;
12843 DECL_TEMPLATE_RESULT (r) = TYPE_MAIN_DECL (new_type);
12844 DECL_TI_ARGS (r) = CLASSTYPE_TI_ARGS (new_type);
12845 DECL_CONTEXT (r) = TYPE_CONTEXT (new_type);
12847 else
12849 tree new_decl;
12850 ++processing_template_decl;
12851 if (TREE_CODE (decl) == FUNCTION_DECL)
12852 new_decl = tsubst_function_decl (decl, args, complain, lambda_fntype);
12853 else
12854 new_decl = tsubst (decl, args, complain, in_decl);
12855 --processing_template_decl;
12856 if (new_decl == error_mark_node)
12857 return error_mark_node;
12859 DECL_TEMPLATE_RESULT (r) = new_decl;
12860 TREE_TYPE (r) = TREE_TYPE (new_decl);
12861 DECL_CONTEXT (r) = DECL_CONTEXT (new_decl);
12862 if (lambda_fntype)
12864 tree args = template_parms_to_args (DECL_TEMPLATE_PARMS (r));
12865 DECL_TEMPLATE_INFO (new_decl) = build_template_info (r, args);
12867 else
12869 DECL_TI_TEMPLATE (new_decl) = r;
12870 DECL_TI_ARGS (r) = DECL_TI_ARGS (new_decl);
12874 DECL_TEMPLATE_INSTANTIATIONS (r) = NULL_TREE;
12875 DECL_TEMPLATE_SPECIALIZATIONS (r) = NULL_TREE;
12877 if (PRIMARY_TEMPLATE_P (t))
12878 DECL_PRIMARY_TEMPLATE (r) = r;
12880 if (TREE_CODE (decl) != TYPE_DECL && !VAR_P (decl)
12881 && !lambda_fntype)
12882 /* Record this non-type partial instantiation. */
12883 register_specialization (r, t,
12884 DECL_TI_ARGS (DECL_TEMPLATE_RESULT (r)),
12885 false, hash);
12887 return r;
12890 /* True if FN is the op() for a lambda in an uninstantiated template. */
12892 bool
12893 lambda_fn_in_template_p (tree fn)
12895 if (!fn || !LAMBDA_FUNCTION_P (fn))
12896 return false;
12897 tree closure = DECL_CONTEXT (fn);
12898 return CLASSTYPE_TEMPLATE_INFO (closure) != NULL_TREE;
12901 /* We're instantiating a variable from template function TCTX. Return the
12902 corresponding current enclosing scope. This gets complicated because lambda
12903 functions in templates are regenerated rather than instantiated, but generic
12904 lambda functions are subsequently instantiated. */
12906 static tree
12907 enclosing_instantiation_of (tree otctx)
12909 tree tctx = otctx;
12910 tree fn = current_function_decl;
12911 int lambda_count = 0;
12913 for (; tctx && lambda_fn_in_template_p (tctx);
12914 tctx = decl_function_context (tctx))
12915 ++lambda_count;
12916 for (; fn; fn = decl_function_context (fn))
12918 tree ofn = fn;
12919 int flambda_count = 0;
12920 for (; flambda_count < lambda_count && fn && LAMBDA_FUNCTION_P (fn);
12921 fn = decl_function_context (fn))
12922 ++flambda_count;
12923 if ((fn && DECL_TEMPLATE_INFO (fn))
12924 ? most_general_template (fn) != most_general_template (tctx)
12925 : fn != tctx)
12926 continue;
12927 gcc_assert (DECL_NAME (ofn) == DECL_NAME (otctx)
12928 || DECL_CONV_FN_P (ofn));
12929 return ofn;
12931 gcc_unreachable ();
12934 /* Substitute the ARGS into the T, which is a _DECL. Return the
12935 result of the substitution. Issue error and warning messages under
12936 control of COMPLAIN. */
12938 static tree
12939 tsubst_decl (tree t, tree args, tsubst_flags_t complain)
12941 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
12942 location_t saved_loc;
12943 tree r = NULL_TREE;
12944 tree in_decl = t;
12945 hashval_t hash = 0;
12947 /* Set the filename and linenumber to improve error-reporting. */
12948 saved_loc = input_location;
12949 input_location = DECL_SOURCE_LOCATION (t);
12951 switch (TREE_CODE (t))
12953 case TEMPLATE_DECL:
12954 r = tsubst_template_decl (t, args, complain, /*lambda*/NULL_TREE);
12955 break;
12957 case FUNCTION_DECL:
12958 r = tsubst_function_decl (t, args, complain, /*lambda*/NULL_TREE);
12959 break;
12961 case PARM_DECL:
12963 tree type = NULL_TREE;
12964 int i, len = 1;
12965 tree expanded_types = NULL_TREE;
12966 tree prev_r = NULL_TREE;
12967 tree first_r = NULL_TREE;
12969 if (DECL_PACK_P (t))
12971 /* If there is a local specialization that isn't a
12972 parameter pack, it means that we're doing a "simple"
12973 substitution from inside tsubst_pack_expansion. Just
12974 return the local specialization (which will be a single
12975 parm). */
12976 tree spec = retrieve_local_specialization (t);
12977 if (spec
12978 && TREE_CODE (spec) == PARM_DECL
12979 && TREE_CODE (TREE_TYPE (spec)) != TYPE_PACK_EXPANSION)
12980 RETURN (spec);
12982 /* Expand the TYPE_PACK_EXPANSION that provides the types for
12983 the parameters in this function parameter pack. */
12984 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
12985 complain, in_decl);
12986 if (TREE_CODE (expanded_types) == TREE_VEC)
12988 len = TREE_VEC_LENGTH (expanded_types);
12990 /* Zero-length parameter packs are boring. Just substitute
12991 into the chain. */
12992 if (len == 0)
12993 RETURN (tsubst (TREE_CHAIN (t), args, complain,
12994 TREE_CHAIN (t)));
12996 else
12998 /* All we did was update the type. Make a note of that. */
12999 type = expanded_types;
13000 expanded_types = NULL_TREE;
13004 /* Loop through all of the parameters we'll build. When T is
13005 a function parameter pack, LEN is the number of expanded
13006 types in EXPANDED_TYPES; otherwise, LEN is 1. */
13007 r = NULL_TREE;
13008 for (i = 0; i < len; ++i)
13010 prev_r = r;
13011 r = copy_node (t);
13012 if (DECL_TEMPLATE_PARM_P (t))
13013 SET_DECL_TEMPLATE_PARM_P (r);
13015 if (expanded_types)
13016 /* We're on the Ith parameter of the function parameter
13017 pack. */
13019 /* Get the Ith type. */
13020 type = TREE_VEC_ELT (expanded_types, i);
13022 /* Rename the parameter to include the index. */
13023 DECL_NAME (r)
13024 = make_ith_pack_parameter_name (DECL_NAME (r), i);
13026 else if (!type)
13027 /* We're dealing with a normal parameter. */
13028 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13030 type = type_decays_to (type);
13031 TREE_TYPE (r) = type;
13032 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13034 if (DECL_INITIAL (r))
13036 if (TREE_CODE (DECL_INITIAL (r)) != TEMPLATE_PARM_INDEX)
13037 DECL_INITIAL (r) = TREE_TYPE (r);
13038 else
13039 DECL_INITIAL (r) = tsubst (DECL_INITIAL (r), args,
13040 complain, in_decl);
13043 DECL_CONTEXT (r) = NULL_TREE;
13045 if (!DECL_TEMPLATE_PARM_P (r))
13046 DECL_ARG_TYPE (r) = type_passed_as (type);
13048 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13049 args, complain, in_decl);
13051 /* Keep track of the first new parameter we
13052 generate. That's what will be returned to the
13053 caller. */
13054 if (!first_r)
13055 first_r = r;
13057 /* Build a proper chain of parameters when substituting
13058 into a function parameter pack. */
13059 if (prev_r)
13060 DECL_CHAIN (prev_r) = r;
13063 /* If cp_unevaluated_operand is set, we're just looking for a
13064 single dummy parameter, so don't keep going. */
13065 if (DECL_CHAIN (t) && !cp_unevaluated_operand)
13066 DECL_CHAIN (r) = tsubst (DECL_CHAIN (t), args,
13067 complain, DECL_CHAIN (t));
13069 /* FIRST_R contains the start of the chain we've built. */
13070 r = first_r;
13072 break;
13074 case FIELD_DECL:
13076 tree type = NULL_TREE;
13077 tree vec = NULL_TREE;
13078 tree expanded_types = NULL_TREE;
13079 int len = 1;
13081 if (PACK_EXPANSION_P (TREE_TYPE (t)))
13083 /* This field is a lambda capture pack. Return a TREE_VEC of
13084 the expanded fields to instantiate_class_template_1. */
13085 expanded_types = tsubst_pack_expansion (TREE_TYPE (t), args,
13086 complain, in_decl);
13087 if (TREE_CODE (expanded_types) == TREE_VEC)
13089 len = TREE_VEC_LENGTH (expanded_types);
13090 vec = make_tree_vec (len);
13092 else
13094 /* All we did was update the type. Make a note of that. */
13095 type = expanded_types;
13096 expanded_types = NULL_TREE;
13100 for (int i = 0; i < len; ++i)
13102 r = copy_decl (t);
13103 if (expanded_types)
13105 type = TREE_VEC_ELT (expanded_types, i);
13106 DECL_NAME (r)
13107 = make_ith_pack_parameter_name (DECL_NAME (r), i);
13109 else if (!type)
13110 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13112 if (type == error_mark_node)
13113 RETURN (error_mark_node);
13114 TREE_TYPE (r) = type;
13115 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13117 if (DECL_C_BIT_FIELD (r))
13118 /* For bit-fields, DECL_BIT_FIELD_REPRESENTATIVE gives the
13119 number of bits. */
13120 DECL_BIT_FIELD_REPRESENTATIVE (r)
13121 = tsubst_expr (DECL_BIT_FIELD_REPRESENTATIVE (t), args,
13122 complain, in_decl,
13123 /*integral_constant_expression_p=*/true);
13124 if (DECL_INITIAL (t))
13126 /* Set up DECL_TEMPLATE_INFO so that we can get at the
13127 NSDMI in perform_member_init. Still set DECL_INITIAL
13128 so that we know there is one. */
13129 DECL_INITIAL (r) = void_node;
13130 gcc_assert (DECL_LANG_SPECIFIC (r) == NULL);
13131 retrofit_lang_decl (r);
13132 DECL_TEMPLATE_INFO (r) = build_template_info (t, args);
13134 /* We don't have to set DECL_CONTEXT here; it is set by
13135 finish_member_declaration. */
13136 DECL_CHAIN (r) = NULL_TREE;
13138 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r), 0,
13139 args, complain, in_decl);
13141 if (vec)
13142 TREE_VEC_ELT (vec, i) = r;
13145 if (vec)
13146 r = vec;
13148 break;
13150 case USING_DECL:
13151 /* We reach here only for member using decls. We also need to check
13152 uses_template_parms because DECL_DEPENDENT_P is not set for a
13153 using-declaration that designates a member of the current
13154 instantiation (c++/53549). */
13155 if (DECL_DEPENDENT_P (t)
13156 || uses_template_parms (USING_DECL_SCOPE (t)))
13158 tree scope = USING_DECL_SCOPE (t);
13159 tree name = tsubst_copy (DECL_NAME (t), args, complain, in_decl);
13160 if (PACK_EXPANSION_P (scope))
13162 tree vec = tsubst_pack_expansion (scope, args, complain, in_decl);
13163 int len = TREE_VEC_LENGTH (vec);
13164 r = make_tree_vec (len);
13165 for (int i = 0; i < len; ++i)
13167 tree escope = TREE_VEC_ELT (vec, i);
13168 tree elt = do_class_using_decl (escope, name);
13169 if (!elt)
13171 r = error_mark_node;
13172 break;
13174 else
13176 TREE_PROTECTED (elt) = TREE_PROTECTED (t);
13177 TREE_PRIVATE (elt) = TREE_PRIVATE (t);
13179 TREE_VEC_ELT (r, i) = elt;
13182 else
13184 tree inst_scope = tsubst_copy (USING_DECL_SCOPE (t), args,
13185 complain, in_decl);
13186 r = do_class_using_decl (inst_scope, name);
13187 if (!r)
13188 r = error_mark_node;
13189 else
13191 TREE_PROTECTED (r) = TREE_PROTECTED (t);
13192 TREE_PRIVATE (r) = TREE_PRIVATE (t);
13196 else
13198 r = copy_node (t);
13199 DECL_CHAIN (r) = NULL_TREE;
13201 break;
13203 case TYPE_DECL:
13204 case VAR_DECL:
13206 tree argvec = NULL_TREE;
13207 tree gen_tmpl = NULL_TREE;
13208 tree spec;
13209 tree tmpl = NULL_TREE;
13210 tree ctx;
13211 tree type = NULL_TREE;
13212 bool local_p;
13214 if (TREE_TYPE (t) == error_mark_node)
13215 RETURN (error_mark_node);
13217 if (TREE_CODE (t) == TYPE_DECL
13218 && t == TYPE_MAIN_DECL (TREE_TYPE (t)))
13220 /* If this is the canonical decl, we don't have to
13221 mess with instantiations, and often we can't (for
13222 typename, template type parms and such). Note that
13223 TYPE_NAME is not correct for the above test if
13224 we've copied the type for a typedef. */
13225 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13226 if (type == error_mark_node)
13227 RETURN (error_mark_node);
13228 r = TYPE_NAME (type);
13229 break;
13232 /* Check to see if we already have the specialization we
13233 need. */
13234 spec = NULL_TREE;
13235 if (DECL_CLASS_SCOPE_P (t) || DECL_NAMESPACE_SCOPE_P (t))
13237 /* T is a static data member or namespace-scope entity.
13238 We have to substitute into namespace-scope variables
13239 (not just variable templates) because of cases like:
13241 template <class T> void f() { extern T t; }
13243 where the entity referenced is not known until
13244 instantiation time. */
13245 local_p = false;
13246 ctx = DECL_CONTEXT (t);
13247 if (DECL_CLASS_SCOPE_P (t))
13249 ctx = tsubst_aggr_type (ctx, args,
13250 complain,
13251 in_decl, /*entering_scope=*/1);
13252 /* If CTX is unchanged, then T is in fact the
13253 specialization we want. That situation occurs when
13254 referencing a static data member within in its own
13255 class. We can use pointer equality, rather than
13256 same_type_p, because DECL_CONTEXT is always
13257 canonical... */
13258 if (ctx == DECL_CONTEXT (t)
13259 /* ... unless T is a member template; in which
13260 case our caller can be willing to create a
13261 specialization of that template represented
13262 by T. */
13263 && !(DECL_TI_TEMPLATE (t)
13264 && DECL_MEMBER_TEMPLATE_P (DECL_TI_TEMPLATE (t))))
13265 spec = t;
13268 if (!spec)
13270 tmpl = DECL_TI_TEMPLATE (t);
13271 gen_tmpl = most_general_template (tmpl);
13272 argvec = tsubst (DECL_TI_ARGS (t), args, complain, in_decl);
13273 if (argvec != error_mark_node)
13274 argvec = (coerce_innermost_template_parms
13275 (DECL_TEMPLATE_PARMS (gen_tmpl),
13276 argvec, t, complain,
13277 /*all*/true, /*defarg*/true));
13278 if (argvec == error_mark_node)
13279 RETURN (error_mark_node);
13280 hash = hash_tmpl_and_args (gen_tmpl, argvec);
13281 spec = retrieve_specialization (gen_tmpl, argvec, hash);
13284 else
13286 /* A local variable. */
13287 local_p = true;
13288 /* Subsequent calls to pushdecl will fill this in. */
13289 ctx = NULL_TREE;
13290 /* Unless this is a reference to a static variable from an
13291 enclosing function, in which case we need to fill it in now. */
13292 if (TREE_STATIC (t))
13294 tree fn = enclosing_instantiation_of (DECL_CONTEXT (t));
13295 if (fn != current_function_decl)
13296 ctx = fn;
13298 spec = retrieve_local_specialization (t);
13300 /* If we already have the specialization we need, there is
13301 nothing more to do. */
13302 if (spec)
13304 r = spec;
13305 break;
13308 /* Create a new node for the specialization we need. */
13309 r = copy_decl (t);
13310 if (type == NULL_TREE)
13312 if (is_typedef_decl (t))
13313 type = DECL_ORIGINAL_TYPE (t);
13314 else
13315 type = TREE_TYPE (t);
13316 if (VAR_P (t)
13317 && VAR_HAD_UNKNOWN_BOUND (t)
13318 && type != error_mark_node)
13319 type = strip_array_domain (type);
13320 tree sub_args = args;
13321 if (tree auto_node = type_uses_auto (type))
13323 /* Mask off any template args past the variable's context so we
13324 don't replace the auto with an unrelated argument. */
13325 int nouter = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
13326 int extra = TMPL_ARGS_DEPTH (args) - nouter;
13327 if (extra > 0)
13328 /* This should never happen with the new lambda instantiation
13329 model, but keep the handling just in case. */
13330 gcc_assert (!CHECKING_P),
13331 sub_args = strip_innermost_template_args (args, extra);
13333 type = tsubst (type, sub_args, complain, in_decl);
13335 if (VAR_P (r))
13337 /* Even if the original location is out of scope, the
13338 newly substituted one is not. */
13339 DECL_DEAD_FOR_LOCAL (r) = 0;
13340 DECL_INITIALIZED_P (r) = 0;
13341 DECL_TEMPLATE_INSTANTIATED (r) = 0;
13342 if (type == error_mark_node)
13343 RETURN (error_mark_node);
13344 if (TREE_CODE (type) == FUNCTION_TYPE)
13346 /* It may seem that this case cannot occur, since:
13348 typedef void f();
13349 void g() { f x; }
13351 declares a function, not a variable. However:
13353 typedef void f();
13354 template <typename T> void g() { T t; }
13355 template void g<f>();
13357 is an attempt to declare a variable with function
13358 type. */
13359 error ("variable %qD has function type",
13360 /* R is not yet sufficiently initialized, so we
13361 just use its name. */
13362 DECL_NAME (r));
13363 RETURN (error_mark_node);
13365 type = complete_type (type);
13366 /* Wait until cp_finish_decl to set this again, to handle
13367 circular dependency (template/instantiate6.C). */
13368 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r) = 0;
13369 type = check_var_type (DECL_NAME (r), type);
13371 if (DECL_HAS_VALUE_EXPR_P (t))
13373 tree ve = DECL_VALUE_EXPR (t);
13374 ve = tsubst_expr (ve, args, complain, in_decl,
13375 /*constant_expression_p=*/false);
13376 if (REFERENCE_REF_P (ve))
13378 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
13379 ve = TREE_OPERAND (ve, 0);
13381 SET_DECL_VALUE_EXPR (r, ve);
13383 if (CP_DECL_THREAD_LOCAL_P (r)
13384 && !processing_template_decl)
13385 set_decl_tls_model (r, decl_default_tls_model (r));
13387 else if (DECL_SELF_REFERENCE_P (t))
13388 SET_DECL_SELF_REFERENCE_P (r);
13389 TREE_TYPE (r) = type;
13390 cp_apply_type_quals_to_decl (cp_type_quals (type), r);
13391 DECL_CONTEXT (r) = ctx;
13392 /* Clear out the mangled name and RTL for the instantiation. */
13393 SET_DECL_ASSEMBLER_NAME (r, NULL_TREE);
13394 if (CODE_CONTAINS_STRUCT (TREE_CODE (t), TS_DECL_WRTL))
13395 SET_DECL_RTL (r, NULL);
13396 /* The initializer must not be expanded until it is required;
13397 see [temp.inst]. */
13398 DECL_INITIAL (r) = NULL_TREE;
13399 DECL_SIZE (r) = DECL_SIZE_UNIT (r) = 0;
13400 if (VAR_P (r))
13402 if (DECL_LANG_SPECIFIC (r))
13403 SET_DECL_DEPENDENT_INIT_P (r, false);
13405 SET_DECL_MODE (r, VOIDmode);
13407 /* Possibly limit visibility based on template args. */
13408 DECL_VISIBILITY (r) = VISIBILITY_DEFAULT;
13409 if (DECL_VISIBILITY_SPECIFIED (t))
13411 DECL_VISIBILITY_SPECIFIED (r) = 0;
13412 DECL_ATTRIBUTES (r)
13413 = remove_attribute ("visibility", DECL_ATTRIBUTES (r));
13415 determine_visibility (r);
13418 if (!local_p)
13420 /* A static data member declaration is always marked
13421 external when it is declared in-class, even if an
13422 initializer is present. We mimic the non-template
13423 processing here. */
13424 DECL_EXTERNAL (r) = 1;
13425 if (DECL_NAMESPACE_SCOPE_P (t))
13426 DECL_NOT_REALLY_EXTERN (r) = 1;
13428 DECL_TEMPLATE_INFO (r) = build_template_info (tmpl, argvec);
13429 SET_DECL_IMPLICIT_INSTANTIATION (r);
13430 register_specialization (r, gen_tmpl, argvec, false, hash);
13432 else
13434 if (DECL_LANG_SPECIFIC (r))
13435 DECL_TEMPLATE_INFO (r) = NULL_TREE;
13436 if (!cp_unevaluated_operand)
13437 register_local_specialization (r, t);
13440 DECL_CHAIN (r) = NULL_TREE;
13442 apply_late_template_attributes (&r, DECL_ATTRIBUTES (r),
13443 /*flags=*/0,
13444 args, complain, in_decl);
13446 /* Preserve a typedef that names a type. */
13447 if (is_typedef_decl (r) && type != error_mark_node)
13449 DECL_ORIGINAL_TYPE (r) = NULL_TREE;
13450 set_underlying_type (r);
13451 if (TYPE_DECL_ALIAS_P (r))
13452 /* An alias template specialization can be dependent
13453 even if its underlying type is not. */
13454 TYPE_DEPENDENT_P_VALID (TREE_TYPE (r)) = false;
13457 layout_decl (r, 0);
13459 break;
13461 default:
13462 gcc_unreachable ();
13464 #undef RETURN
13466 out:
13467 /* Restore the file and line information. */
13468 input_location = saved_loc;
13470 return r;
13473 /* Substitute into the ARG_TYPES of a function type.
13474 If END is a TREE_CHAIN, leave it and any following types
13475 un-substituted. */
13477 static tree
13478 tsubst_arg_types (tree arg_types,
13479 tree args,
13480 tree end,
13481 tsubst_flags_t complain,
13482 tree in_decl)
13484 tree remaining_arg_types;
13485 tree type = NULL_TREE;
13486 int i = 1;
13487 tree expanded_args = NULL_TREE;
13488 tree default_arg;
13490 if (!arg_types || arg_types == void_list_node || arg_types == end)
13491 return arg_types;
13493 remaining_arg_types = tsubst_arg_types (TREE_CHAIN (arg_types),
13494 args, end, complain, in_decl);
13495 if (remaining_arg_types == error_mark_node)
13496 return error_mark_node;
13498 if (PACK_EXPANSION_P (TREE_VALUE (arg_types)))
13500 /* For a pack expansion, perform substitution on the
13501 entire expression. Later on, we'll handle the arguments
13502 one-by-one. */
13503 expanded_args = tsubst_pack_expansion (TREE_VALUE (arg_types),
13504 args, complain, in_decl);
13506 if (TREE_CODE (expanded_args) == TREE_VEC)
13507 /* So that we'll spin through the parameters, one by one. */
13508 i = TREE_VEC_LENGTH (expanded_args);
13509 else
13511 /* We only partially substituted into the parameter
13512 pack. Our type is TYPE_PACK_EXPANSION. */
13513 type = expanded_args;
13514 expanded_args = NULL_TREE;
13518 while (i > 0) {
13519 --i;
13521 if (expanded_args)
13522 type = TREE_VEC_ELT (expanded_args, i);
13523 else if (!type)
13524 type = tsubst (TREE_VALUE (arg_types), args, complain, in_decl);
13526 if (type == error_mark_node)
13527 return error_mark_node;
13528 if (VOID_TYPE_P (type))
13530 if (complain & tf_error)
13532 error ("invalid parameter type %qT", type);
13533 if (in_decl)
13534 error ("in declaration %q+D", in_decl);
13536 return error_mark_node;
13538 /* DR 657. */
13539 if (abstract_virtuals_error_sfinae (ACU_PARM, type, complain))
13540 return error_mark_node;
13542 /* Do array-to-pointer, function-to-pointer conversion, and ignore
13543 top-level qualifiers as required. */
13544 type = cv_unqualified (type_decays_to (type));
13546 /* We do not substitute into default arguments here. The standard
13547 mandates that they be instantiated only when needed, which is
13548 done in build_over_call. */
13549 default_arg = TREE_PURPOSE (arg_types);
13551 /* Except that we do substitute default arguments under tsubst_lambda_expr,
13552 since the new op() won't have any associated template arguments for us
13553 to refer to later. */
13554 if (lambda_fn_in_template_p (in_decl))
13555 default_arg = tsubst_copy_and_build (default_arg, args, complain, in_decl,
13556 false/*fn*/, false/*constexpr*/);
13558 if (default_arg && TREE_CODE (default_arg) == DEFAULT_ARG)
13560 /* We've instantiated a template before its default arguments
13561 have been parsed. This can happen for a nested template
13562 class, and is not an error unless we require the default
13563 argument in a call of this function. */
13564 remaining_arg_types =
13565 tree_cons (default_arg, type, remaining_arg_types);
13566 vec_safe_push (DEFARG_INSTANTIATIONS(default_arg), remaining_arg_types);
13568 else
13569 remaining_arg_types =
13570 hash_tree_cons (default_arg, type, remaining_arg_types);
13573 return remaining_arg_types;
13576 /* Substitute into a FUNCTION_TYPE or METHOD_TYPE. This routine does
13577 *not* handle the exception-specification for FNTYPE, because the
13578 initial substitution of explicitly provided template parameters
13579 during argument deduction forbids substitution into the
13580 exception-specification:
13582 [temp.deduct]
13584 All references in the function type of the function template to the
13585 corresponding template parameters are replaced by the specified tem-
13586 plate argument values. If a substitution in a template parameter or
13587 in the function type of the function template results in an invalid
13588 type, type deduction fails. [Note: The equivalent substitution in
13589 exception specifications is done only when the function is instanti-
13590 ated, at which point a program is ill-formed if the substitution
13591 results in an invalid type.] */
13593 static tree
13594 tsubst_function_type (tree t,
13595 tree args,
13596 tsubst_flags_t complain,
13597 tree in_decl)
13599 tree return_type;
13600 tree arg_types = NULL_TREE;
13601 tree fntype;
13603 /* The TYPE_CONTEXT is not used for function/method types. */
13604 gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
13606 /* DR 1227: Mixing immediate and non-immediate contexts in deduction
13607 failure. */
13608 bool late_return_type_p = TYPE_HAS_LATE_RETURN_TYPE (t);
13610 if (late_return_type_p)
13612 /* Substitute the argument types. */
13613 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13614 complain, in_decl);
13615 if (arg_types == error_mark_node)
13616 return error_mark_node;
13618 tree save_ccp = current_class_ptr;
13619 tree save_ccr = current_class_ref;
13620 tree this_type = (TREE_CODE (t) == METHOD_TYPE
13621 ? TREE_TYPE (TREE_VALUE (arg_types)) : NULL_TREE);
13622 bool do_inject = this_type && CLASS_TYPE_P (this_type);
13623 if (do_inject)
13625 /* DR 1207: 'this' is in scope in the trailing return type. */
13626 inject_this_parameter (this_type, cp_type_quals (this_type));
13629 /* Substitute the return type. */
13630 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13632 if (do_inject)
13634 current_class_ptr = save_ccp;
13635 current_class_ref = save_ccr;
13638 else
13639 /* Substitute the return type. */
13640 return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
13642 if (return_type == error_mark_node)
13643 return error_mark_node;
13644 /* DR 486 clarifies that creation of a function type with an
13645 invalid return type is a deduction failure. */
13646 if (TREE_CODE (return_type) == ARRAY_TYPE
13647 || TREE_CODE (return_type) == FUNCTION_TYPE)
13649 if (complain & tf_error)
13651 if (TREE_CODE (return_type) == ARRAY_TYPE)
13652 error ("function returning an array");
13653 else
13654 error ("function returning a function");
13656 return error_mark_node;
13658 /* And DR 657. */
13659 if (abstract_virtuals_error_sfinae (ACU_RETURN, return_type, complain))
13660 return error_mark_node;
13662 if (!late_return_type_p)
13664 /* Substitute the argument types. */
13665 arg_types = tsubst_arg_types (TYPE_ARG_TYPES (t), args, NULL_TREE,
13666 complain, in_decl);
13667 if (arg_types == error_mark_node)
13668 return error_mark_node;
13671 /* Construct a new type node and return it. */
13672 if (TREE_CODE (t) == FUNCTION_TYPE)
13674 fntype = build_function_type (return_type, arg_types);
13675 fntype = apply_memfn_quals (fntype,
13676 type_memfn_quals (t),
13677 type_memfn_rqual (t));
13679 else
13681 tree r = TREE_TYPE (TREE_VALUE (arg_types));
13682 /* Don't pick up extra function qualifiers from the basetype. */
13683 r = cp_build_qualified_type_real (r, type_memfn_quals (t), complain);
13684 if (! MAYBE_CLASS_TYPE_P (r))
13686 /* [temp.deduct]
13688 Type deduction may fail for any of the following
13689 reasons:
13691 -- Attempting to create "pointer to member of T" when T
13692 is not a class type. */
13693 if (complain & tf_error)
13694 error ("creating pointer to member function of non-class type %qT",
13696 return error_mark_node;
13699 fntype = build_method_type_directly (r, return_type,
13700 TREE_CHAIN (arg_types));
13701 fntype = build_ref_qualified_type (fntype, type_memfn_rqual (t));
13703 fntype = cp_build_type_attribute_variant (fntype, TYPE_ATTRIBUTES (t));
13705 if (late_return_type_p)
13706 TYPE_HAS_LATE_RETURN_TYPE (fntype) = 1;
13708 return fntype;
13711 /* FNTYPE is a FUNCTION_TYPE or METHOD_TYPE. Substitute the template
13712 ARGS into that specification, and return the substituted
13713 specification. If there is no specification, return NULL_TREE. */
13715 static tree
13716 tsubst_exception_specification (tree fntype,
13717 tree args,
13718 tsubst_flags_t complain,
13719 tree in_decl,
13720 bool defer_ok)
13722 tree specs;
13723 tree new_specs;
13725 specs = TYPE_RAISES_EXCEPTIONS (fntype);
13726 new_specs = NULL_TREE;
13727 if (specs && TREE_PURPOSE (specs))
13729 /* A noexcept-specifier. */
13730 tree expr = TREE_PURPOSE (specs);
13731 if (TREE_CODE (expr) == INTEGER_CST)
13732 new_specs = expr;
13733 else if (defer_ok)
13735 /* Defer instantiation of noexcept-specifiers to avoid
13736 excessive instantiations (c++/49107). */
13737 new_specs = make_node (DEFERRED_NOEXCEPT);
13738 if (DEFERRED_NOEXCEPT_SPEC_P (specs))
13740 /* We already partially instantiated this member template,
13741 so combine the new args with the old. */
13742 DEFERRED_NOEXCEPT_PATTERN (new_specs)
13743 = DEFERRED_NOEXCEPT_PATTERN (expr);
13744 DEFERRED_NOEXCEPT_ARGS (new_specs)
13745 = add_to_template_args (DEFERRED_NOEXCEPT_ARGS (expr), args);
13747 else
13749 DEFERRED_NOEXCEPT_PATTERN (new_specs) = expr;
13750 DEFERRED_NOEXCEPT_ARGS (new_specs) = args;
13753 else
13754 new_specs = tsubst_copy_and_build
13755 (expr, args, complain, in_decl, /*function_p=*/false,
13756 /*integral_constant_expression_p=*/true);
13757 new_specs = build_noexcept_spec (new_specs, complain);
13759 else if (specs)
13761 if (! TREE_VALUE (specs))
13762 new_specs = specs;
13763 else
13764 while (specs)
13766 tree spec;
13767 int i, len = 1;
13768 tree expanded_specs = NULL_TREE;
13770 if (PACK_EXPANSION_P (TREE_VALUE (specs)))
13772 /* Expand the pack expansion type. */
13773 expanded_specs = tsubst_pack_expansion (TREE_VALUE (specs),
13774 args, complain,
13775 in_decl);
13777 if (expanded_specs == error_mark_node)
13778 return error_mark_node;
13779 else if (TREE_CODE (expanded_specs) == TREE_VEC)
13780 len = TREE_VEC_LENGTH (expanded_specs);
13781 else
13783 /* We're substituting into a member template, so
13784 we got a TYPE_PACK_EXPANSION back. Add that
13785 expansion and move on. */
13786 gcc_assert (TREE_CODE (expanded_specs)
13787 == TYPE_PACK_EXPANSION);
13788 new_specs = add_exception_specifier (new_specs,
13789 expanded_specs,
13790 complain);
13791 specs = TREE_CHAIN (specs);
13792 continue;
13796 for (i = 0; i < len; ++i)
13798 if (expanded_specs)
13799 spec = TREE_VEC_ELT (expanded_specs, i);
13800 else
13801 spec = tsubst (TREE_VALUE (specs), args, complain, in_decl);
13802 if (spec == error_mark_node)
13803 return spec;
13804 new_specs = add_exception_specifier (new_specs, spec,
13805 complain);
13808 specs = TREE_CHAIN (specs);
13811 return new_specs;
13814 /* Take the tree structure T and replace template parameters used
13815 therein with the argument vector ARGS. IN_DECL is an associated
13816 decl for diagnostics. If an error occurs, returns ERROR_MARK_NODE.
13817 Issue error and warning messages under control of COMPLAIN. Note
13818 that we must be relatively non-tolerant of extensions here, in
13819 order to preserve conformance; if we allow substitutions that
13820 should not be allowed, we may allow argument deductions that should
13821 not succeed, and therefore report ambiguous overload situations
13822 where there are none. In theory, we could allow the substitution,
13823 but indicate that it should have failed, and allow our caller to
13824 make sure that the right thing happens, but we don't try to do this
13825 yet.
13827 This function is used for dealing with types, decls and the like;
13828 for expressions, use tsubst_expr or tsubst_copy. */
13830 tree
13831 tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
13833 enum tree_code code;
13834 tree type, r = NULL_TREE;
13836 if (t == NULL_TREE || t == error_mark_node
13837 || t == integer_type_node
13838 || t == void_type_node
13839 || t == char_type_node
13840 || t == unknown_type_node
13841 || TREE_CODE (t) == NAMESPACE_DECL
13842 || TREE_CODE (t) == TRANSLATION_UNIT_DECL)
13843 return t;
13845 if (DECL_P (t))
13846 return tsubst_decl (t, args, complain);
13848 if (args == NULL_TREE)
13849 return t;
13851 code = TREE_CODE (t);
13853 if (code == IDENTIFIER_NODE)
13854 type = IDENTIFIER_TYPE_VALUE (t);
13855 else
13856 type = TREE_TYPE (t);
13858 gcc_assert (type != unknown_type_node);
13860 /* Reuse typedefs. We need to do this to handle dependent attributes,
13861 such as attribute aligned. */
13862 if (TYPE_P (t)
13863 && typedef_variant_p (t))
13865 tree decl = TYPE_NAME (t);
13867 if (alias_template_specialization_p (t))
13869 /* DECL represents an alias template and we want to
13870 instantiate it. */
13871 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13872 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13873 r = instantiate_alias_template (tmpl, gen_args, complain);
13875 else if (DECL_CLASS_SCOPE_P (decl)
13876 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (decl))
13877 && uses_template_parms (DECL_CONTEXT (decl)))
13879 tree tmpl = most_general_template (DECL_TI_TEMPLATE (decl));
13880 tree gen_args = tsubst (DECL_TI_ARGS (decl), args, complain, in_decl);
13881 r = retrieve_specialization (tmpl, gen_args, 0);
13883 else if (DECL_FUNCTION_SCOPE_P (decl)
13884 && DECL_TEMPLATE_INFO (DECL_CONTEXT (decl))
13885 && uses_template_parms (DECL_TI_ARGS (DECL_CONTEXT (decl))))
13886 r = retrieve_local_specialization (decl);
13887 else
13888 /* The typedef is from a non-template context. */
13889 return t;
13891 if (r)
13893 r = TREE_TYPE (r);
13894 r = cp_build_qualified_type_real
13895 (r, cp_type_quals (t) | cp_type_quals (r),
13896 complain | tf_ignore_bad_quals);
13897 return r;
13899 else
13901 /* We don't have an instantiation yet, so drop the typedef. */
13902 int quals = cp_type_quals (t);
13903 t = DECL_ORIGINAL_TYPE (decl);
13904 t = cp_build_qualified_type_real (t, quals,
13905 complain | tf_ignore_bad_quals);
13909 bool fndecl_type = (complain & tf_fndecl_type);
13910 complain &= ~tf_fndecl_type;
13912 if (type
13913 && code != TYPENAME_TYPE
13914 && code != TEMPLATE_TYPE_PARM
13915 && code != TEMPLATE_PARM_INDEX
13916 && code != IDENTIFIER_NODE
13917 && code != FUNCTION_TYPE
13918 && code != METHOD_TYPE)
13919 type = tsubst (type, args, complain, in_decl);
13920 if (type == error_mark_node)
13921 return error_mark_node;
13923 switch (code)
13925 case RECORD_TYPE:
13926 case UNION_TYPE:
13927 case ENUMERAL_TYPE:
13928 return tsubst_aggr_type (t, args, complain, in_decl,
13929 /*entering_scope=*/0);
13931 case ERROR_MARK:
13932 case IDENTIFIER_NODE:
13933 case VOID_TYPE:
13934 case REAL_TYPE:
13935 case COMPLEX_TYPE:
13936 case VECTOR_TYPE:
13937 case BOOLEAN_TYPE:
13938 case NULLPTR_TYPE:
13939 case LANG_TYPE:
13940 return t;
13942 case INTEGER_TYPE:
13943 if (t == integer_type_node)
13944 return t;
13946 if (TREE_CODE (TYPE_MIN_VALUE (t)) == INTEGER_CST
13947 && TREE_CODE (TYPE_MAX_VALUE (t)) == INTEGER_CST)
13948 return t;
13951 tree max, omax = TREE_OPERAND (TYPE_MAX_VALUE (t), 0);
13953 max = tsubst_expr (omax, args, complain, in_decl,
13954 /*integral_constant_expression_p=*/false);
13956 /* Fix up type of the magic NOP_EXPR with TREE_SIDE_EFFECTS if
13957 needed. */
13958 if (TREE_CODE (max) == NOP_EXPR
13959 && TREE_SIDE_EFFECTS (omax)
13960 && !TREE_TYPE (max))
13961 TREE_TYPE (max) = TREE_TYPE (TREE_OPERAND (max, 0));
13963 /* If we're in a partial instantiation, preserve the magic NOP_EXPR
13964 with TREE_SIDE_EFFECTS that indicates this is not an integral
13965 constant expression. */
13966 if (processing_template_decl
13967 && TREE_SIDE_EFFECTS (omax) && TREE_CODE (omax) == NOP_EXPR)
13969 gcc_assert (TREE_CODE (max) == NOP_EXPR);
13970 TREE_SIDE_EFFECTS (max) = 1;
13973 return compute_array_index_type (NULL_TREE, max, complain);
13976 case TEMPLATE_TYPE_PARM:
13977 case TEMPLATE_TEMPLATE_PARM:
13978 case BOUND_TEMPLATE_TEMPLATE_PARM:
13979 case TEMPLATE_PARM_INDEX:
13981 int idx;
13982 int level;
13983 int levels;
13984 tree arg = NULL_TREE;
13986 /* Early in template argument deduction substitution, we don't
13987 want to reduce the level of 'auto', or it will be confused
13988 with a normal template parm in subsequent deduction. */
13989 if (is_auto (t) && (complain & tf_partial))
13990 return t;
13992 r = NULL_TREE;
13994 gcc_assert (TREE_VEC_LENGTH (args) > 0);
13995 template_parm_level_and_index (t, &level, &idx);
13997 levels = TMPL_ARGS_DEPTH (args);
13998 if (level <= levels
13999 && TREE_VEC_LENGTH (TMPL_ARGS_LEVEL (args, level)) > 0)
14001 arg = TMPL_ARG (args, level, idx);
14003 /* See through ARGUMENT_PACK_SELECT arguments. */
14004 if (arg && TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
14005 arg = argument_pack_select_arg (arg);
14008 if (arg == error_mark_node)
14009 return error_mark_node;
14010 else if (arg != NULL_TREE)
14012 if (ARGUMENT_PACK_P (arg))
14013 /* If ARG is an argument pack, we don't actually want to
14014 perform a substitution here, because substitutions
14015 for argument packs are only done
14016 element-by-element. We can get to this point when
14017 substituting the type of a non-type template
14018 parameter pack, when that type actually contains
14019 template parameter packs from an outer template, e.g.,
14021 template<typename... Types> struct A {
14022 template<Types... Values> struct B { };
14023 }; */
14024 return t;
14026 if (code == TEMPLATE_TYPE_PARM)
14028 int quals;
14029 gcc_assert (TYPE_P (arg));
14031 quals = cp_type_quals (arg) | cp_type_quals (t);
14033 return cp_build_qualified_type_real
14034 (arg, quals, complain | tf_ignore_bad_quals);
14036 else if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
14038 /* We are processing a type constructed from a
14039 template template parameter. */
14040 tree argvec = tsubst (TYPE_TI_ARGS (t),
14041 args, complain, in_decl);
14042 if (argvec == error_mark_node)
14043 return error_mark_node;
14045 gcc_assert (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM
14046 || TREE_CODE (arg) == TEMPLATE_DECL
14047 || TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE);
14049 if (TREE_CODE (arg) == UNBOUND_CLASS_TEMPLATE)
14050 /* Consider this code:
14052 template <template <class> class Template>
14053 struct Internal {
14054 template <class Arg> using Bind = Template<Arg>;
14057 template <template <class> class Template, class Arg>
14058 using Instantiate = Template<Arg>; //#0
14060 template <template <class> class Template,
14061 class Argument>
14062 using Bind =
14063 Instantiate<Internal<Template>::template Bind,
14064 Argument>; //#1
14066 When #1 is parsed, the
14067 BOUND_TEMPLATE_TEMPLATE_PARM representing the
14068 parameter `Template' in #0 matches the
14069 UNBOUND_CLASS_TEMPLATE representing the argument
14070 `Internal<Template>::template Bind'; We then want
14071 to assemble the type `Bind<Argument>' that can't
14072 be fully created right now, because
14073 `Internal<Template>' not being complete, the Bind
14074 template cannot be looked up in that context. So
14075 we need to "store" `Bind<Argument>' for later
14076 when the context of Bind becomes complete. Let's
14077 store that in a TYPENAME_TYPE. */
14078 return make_typename_type (TYPE_CONTEXT (arg),
14079 build_nt (TEMPLATE_ID_EXPR,
14080 TYPE_IDENTIFIER (arg),
14081 argvec),
14082 typename_type,
14083 complain);
14085 /* We can get a TEMPLATE_TEMPLATE_PARM here when we
14086 are resolving nested-types in the signature of a
14087 member function templates. Otherwise ARG is a
14088 TEMPLATE_DECL and is the real template to be
14089 instantiated. */
14090 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
14091 arg = TYPE_NAME (arg);
14093 r = lookup_template_class (arg,
14094 argvec, in_decl,
14095 DECL_CONTEXT (arg),
14096 /*entering_scope=*/0,
14097 complain);
14098 return cp_build_qualified_type_real
14099 (r, cp_type_quals (t) | cp_type_quals (r), complain);
14101 else if (code == TEMPLATE_TEMPLATE_PARM)
14102 return arg;
14103 else
14104 /* TEMPLATE_PARM_INDEX. */
14105 return convert_from_reference (unshare_expr (arg));
14108 if (level == 1)
14109 /* This can happen during the attempted tsubst'ing in
14110 unify. This means that we don't yet have any information
14111 about the template parameter in question. */
14112 return t;
14114 /* If we get here, we must have been looking at a parm for a
14115 more deeply nested template. Make a new version of this
14116 template parameter, but with a lower level. */
14117 switch (code)
14119 case TEMPLATE_TYPE_PARM:
14120 case TEMPLATE_TEMPLATE_PARM:
14121 case BOUND_TEMPLATE_TEMPLATE_PARM:
14122 if (cp_type_quals (t))
14124 r = tsubst (TYPE_MAIN_VARIANT (t), args, complain, in_decl);
14125 r = cp_build_qualified_type_real
14126 (r, cp_type_quals (t),
14127 complain | (code == TEMPLATE_TYPE_PARM
14128 ? tf_ignore_bad_quals : 0));
14130 else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
14131 && PLACEHOLDER_TYPE_CONSTRAINTS (t)
14132 && (r = (TEMPLATE_PARM_DESCENDANTS
14133 (TEMPLATE_TYPE_PARM_INDEX (t))))
14134 && (r = TREE_TYPE (r))
14135 && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
14136 /* Break infinite recursion when substituting the constraints
14137 of a constrained placeholder. */;
14138 else
14140 r = copy_type (t);
14141 TEMPLATE_TYPE_PARM_INDEX (r)
14142 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (t),
14143 r, levels, args, complain);
14144 TYPE_STUB_DECL (r) = TYPE_NAME (r) = TEMPLATE_TYPE_DECL (r);
14145 TYPE_MAIN_VARIANT (r) = r;
14146 TYPE_POINTER_TO (r) = NULL_TREE;
14147 TYPE_REFERENCE_TO (r) = NULL_TREE;
14149 if (TREE_CODE (t) == TEMPLATE_TYPE_PARM)
14151 /* Propagate constraints on placeholders. */
14152 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (t))
14153 PLACEHOLDER_TYPE_CONSTRAINTS (r)
14154 = tsubst_constraint (constr, args, complain, in_decl);
14155 else if (tree pl = CLASS_PLACEHOLDER_TEMPLATE (t))
14157 pl = tsubst_copy (pl, args, complain, in_decl);
14158 CLASS_PLACEHOLDER_TEMPLATE (r) = pl;
14162 if (TREE_CODE (r) == TEMPLATE_TEMPLATE_PARM)
14163 /* We have reduced the level of the template
14164 template parameter, but not the levels of its
14165 template parameters, so canonical_type_parameter
14166 will not be able to find the canonical template
14167 template parameter for this level. Thus, we
14168 require structural equality checking to compare
14169 TEMPLATE_TEMPLATE_PARMs. */
14170 SET_TYPE_STRUCTURAL_EQUALITY (r);
14171 else if (TYPE_STRUCTURAL_EQUALITY_P (t))
14172 SET_TYPE_STRUCTURAL_EQUALITY (r);
14173 else
14174 TYPE_CANONICAL (r) = canonical_type_parameter (r);
14176 if (code == BOUND_TEMPLATE_TEMPLATE_PARM)
14178 tree tinfo = TYPE_TEMPLATE_INFO (t);
14179 /* We might need to substitute into the types of non-type
14180 template parameters. */
14181 tree tmpl = tsubst (TI_TEMPLATE (tinfo), args,
14182 complain, in_decl);
14183 if (tmpl == error_mark_node)
14184 return error_mark_node;
14185 tree argvec = tsubst (TI_ARGS (tinfo), args,
14186 complain, in_decl);
14187 if (argvec == error_mark_node)
14188 return error_mark_node;
14190 TEMPLATE_TEMPLATE_PARM_TEMPLATE_INFO (r)
14191 = build_template_info (tmpl, argvec);
14194 break;
14196 case TEMPLATE_PARM_INDEX:
14197 /* OK, now substitute the type of the non-type parameter. We
14198 couldn't do it earlier because it might be an auto parameter,
14199 and we wouldn't need to if we had an argument. */
14200 type = tsubst (type, args, complain, in_decl);
14201 if (type == error_mark_node)
14202 return error_mark_node;
14203 r = reduce_template_parm_level (t, type, levels, args, complain);
14204 break;
14206 default:
14207 gcc_unreachable ();
14210 return r;
14213 case TREE_LIST:
14215 tree purpose, value, chain;
14217 if (t == void_list_node)
14218 return t;
14220 purpose = TREE_PURPOSE (t);
14221 if (purpose)
14223 purpose = tsubst (purpose, args, complain, in_decl);
14224 if (purpose == error_mark_node)
14225 return error_mark_node;
14227 value = TREE_VALUE (t);
14228 if (value)
14230 value = tsubst (value, args, complain, in_decl);
14231 if (value == error_mark_node)
14232 return error_mark_node;
14234 chain = TREE_CHAIN (t);
14235 if (chain && chain != void_type_node)
14237 chain = tsubst (chain, args, complain, in_decl);
14238 if (chain == error_mark_node)
14239 return error_mark_node;
14241 if (purpose == TREE_PURPOSE (t)
14242 && value == TREE_VALUE (t)
14243 && chain == TREE_CHAIN (t))
14244 return t;
14245 return hash_tree_cons (purpose, value, chain);
14248 case TREE_BINFO:
14249 /* We should never be tsubsting a binfo. */
14250 gcc_unreachable ();
14252 case TREE_VEC:
14253 /* A vector of template arguments. */
14254 gcc_assert (!type);
14255 return tsubst_template_args (t, args, complain, in_decl);
14257 case POINTER_TYPE:
14258 case REFERENCE_TYPE:
14260 if (type == TREE_TYPE (t) && TREE_CODE (type) != METHOD_TYPE)
14261 return t;
14263 /* [temp.deduct]
14265 Type deduction may fail for any of the following
14266 reasons:
14268 -- Attempting to create a pointer to reference type.
14269 -- Attempting to create a reference to a reference type or
14270 a reference to void.
14272 Core issue 106 says that creating a reference to a reference
14273 during instantiation is no longer a cause for failure. We
14274 only enforce this check in strict C++98 mode. */
14275 if ((TREE_CODE (type) == REFERENCE_TYPE
14276 && (((cxx_dialect == cxx98) && flag_iso) || code != REFERENCE_TYPE))
14277 || (code == REFERENCE_TYPE && VOID_TYPE_P (type)))
14279 static location_t last_loc;
14281 /* We keep track of the last time we issued this error
14282 message to avoid spewing a ton of messages during a
14283 single bad template instantiation. */
14284 if (complain & tf_error
14285 && last_loc != input_location)
14287 if (VOID_TYPE_P (type))
14288 error ("forming reference to void");
14289 else if (code == POINTER_TYPE)
14290 error ("forming pointer to reference type %qT", type);
14291 else
14292 error ("forming reference to reference type %qT", type);
14293 last_loc = input_location;
14296 return error_mark_node;
14298 else if (TREE_CODE (type) == FUNCTION_TYPE
14299 && (type_memfn_quals (type) != TYPE_UNQUALIFIED
14300 || type_memfn_rqual (type) != REF_QUAL_NONE))
14302 if (complain & tf_error)
14304 if (code == POINTER_TYPE)
14305 error ("forming pointer to qualified function type %qT",
14306 type);
14307 else
14308 error ("forming reference to qualified function type %qT",
14309 type);
14311 return error_mark_node;
14313 else if (code == POINTER_TYPE)
14315 r = build_pointer_type (type);
14316 if (TREE_CODE (type) == METHOD_TYPE)
14317 r = build_ptrmemfunc_type (r);
14319 else if (TREE_CODE (type) == REFERENCE_TYPE)
14320 /* In C++0x, during template argument substitution, when there is an
14321 attempt to create a reference to a reference type, reference
14322 collapsing is applied as described in [14.3.1/4 temp.arg.type]:
14324 "If a template-argument for a template-parameter T names a type
14325 that is a reference to a type A, an attempt to create the type
14326 'lvalue reference to cv T' creates the type 'lvalue reference to
14327 A,' while an attempt to create the type type rvalue reference to
14328 cv T' creates the type T"
14330 r = cp_build_reference_type
14331 (TREE_TYPE (type),
14332 TYPE_REF_IS_RVALUE (t) && TYPE_REF_IS_RVALUE (type));
14333 else
14334 r = cp_build_reference_type (type, TYPE_REF_IS_RVALUE (t));
14335 r = cp_build_qualified_type_real (r, cp_type_quals (t), complain);
14337 if (r != error_mark_node)
14338 /* Will this ever be needed for TYPE_..._TO values? */
14339 layout_type (r);
14341 return r;
14343 case OFFSET_TYPE:
14345 r = tsubst (TYPE_OFFSET_BASETYPE (t), args, complain, in_decl);
14346 if (r == error_mark_node || !MAYBE_CLASS_TYPE_P (r))
14348 /* [temp.deduct]
14350 Type deduction may fail for any of the following
14351 reasons:
14353 -- Attempting to create "pointer to member of T" when T
14354 is not a class type. */
14355 if (complain & tf_error)
14356 error ("creating pointer to member of non-class type %qT", r);
14357 return error_mark_node;
14359 if (TREE_CODE (type) == REFERENCE_TYPE)
14361 if (complain & tf_error)
14362 error ("creating pointer to member reference type %qT", type);
14363 return error_mark_node;
14365 if (VOID_TYPE_P (type))
14367 if (complain & tf_error)
14368 error ("creating pointer to member of type void");
14369 return error_mark_node;
14371 gcc_assert (TREE_CODE (type) != METHOD_TYPE);
14372 if (TREE_CODE (type) == FUNCTION_TYPE)
14374 /* The type of the implicit object parameter gets its
14375 cv-qualifiers from the FUNCTION_TYPE. */
14376 tree memptr;
14377 tree method_type
14378 = build_memfn_type (type, r, type_memfn_quals (type),
14379 type_memfn_rqual (type));
14380 memptr = build_ptrmemfunc_type (build_pointer_type (method_type));
14381 return cp_build_qualified_type_real (memptr, cp_type_quals (t),
14382 complain);
14384 else
14385 return cp_build_qualified_type_real (build_ptrmem_type (r, type),
14386 cp_type_quals (t),
14387 complain);
14389 case FUNCTION_TYPE:
14390 case METHOD_TYPE:
14392 tree fntype;
14393 tree specs;
14394 fntype = tsubst_function_type (t, args, complain, in_decl);
14395 if (fntype == error_mark_node)
14396 return error_mark_node;
14398 /* Substitute the exception specification. */
14399 specs = tsubst_exception_specification (t, args, complain, in_decl,
14400 /*defer_ok*/fndecl_type);
14401 if (specs == error_mark_node)
14402 return error_mark_node;
14403 if (specs)
14404 fntype = build_exception_variant (fntype, specs);
14405 return fntype;
14407 case ARRAY_TYPE:
14409 tree domain = tsubst (TYPE_DOMAIN (t), args, complain, in_decl);
14410 if (domain == error_mark_node)
14411 return error_mark_node;
14413 /* As an optimization, we avoid regenerating the array type if
14414 it will obviously be the same as T. */
14415 if (type == TREE_TYPE (t) && domain == TYPE_DOMAIN (t))
14416 return t;
14418 /* These checks should match the ones in create_array_type_for_decl.
14420 [temp.deduct]
14422 The deduction may fail for any of the following reasons:
14424 -- Attempting to create an array with an element type that
14425 is void, a function type, or a reference type, or [DR337]
14426 an abstract class type. */
14427 if (VOID_TYPE_P (type)
14428 || TREE_CODE (type) == FUNCTION_TYPE
14429 || (TREE_CODE (type) == ARRAY_TYPE
14430 && TYPE_DOMAIN (type) == NULL_TREE)
14431 || TREE_CODE (type) == REFERENCE_TYPE)
14433 if (complain & tf_error)
14434 error ("creating array of %qT", type);
14435 return error_mark_node;
14438 if (abstract_virtuals_error_sfinae (ACU_ARRAY, type, complain))
14439 return error_mark_node;
14441 r = build_cplus_array_type (type, domain);
14443 if (TYPE_USER_ALIGN (t))
14445 SET_TYPE_ALIGN (r, TYPE_ALIGN (t));
14446 TYPE_USER_ALIGN (r) = 1;
14449 return r;
14452 case TYPENAME_TYPE:
14454 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14455 in_decl, /*entering_scope=*/1);
14456 if (ctx == error_mark_node)
14457 return error_mark_node;
14459 tree f = tsubst_copy (TYPENAME_TYPE_FULLNAME (t), args,
14460 complain, in_decl);
14461 if (f == error_mark_node)
14462 return error_mark_node;
14464 if (!MAYBE_CLASS_TYPE_P (ctx))
14466 if (complain & tf_error)
14467 error ("%qT is not a class, struct, or union type", ctx);
14468 return error_mark_node;
14470 else if (!uses_template_parms (ctx) && !TYPE_BEING_DEFINED (ctx))
14472 /* Normally, make_typename_type does not require that the CTX
14473 have complete type in order to allow things like:
14475 template <class T> struct S { typename S<T>::X Y; };
14477 But, such constructs have already been resolved by this
14478 point, so here CTX really should have complete type, unless
14479 it's a partial instantiation. */
14480 ctx = complete_type (ctx);
14481 if (!COMPLETE_TYPE_P (ctx))
14483 if (complain & tf_error)
14484 cxx_incomplete_type_error (NULL_TREE, ctx);
14485 return error_mark_node;
14489 f = make_typename_type (ctx, f, typename_type,
14490 complain | tf_keep_type_decl);
14491 if (f == error_mark_node)
14492 return f;
14493 if (TREE_CODE (f) == TYPE_DECL)
14495 complain |= tf_ignore_bad_quals;
14496 f = TREE_TYPE (f);
14499 if (TREE_CODE (f) != TYPENAME_TYPE)
14501 if (TYPENAME_IS_ENUM_P (t) && TREE_CODE (f) != ENUMERAL_TYPE)
14503 if (complain & tf_error)
14504 error ("%qT resolves to %qT, which is not an enumeration type",
14505 t, f);
14506 else
14507 return error_mark_node;
14509 else if (TYPENAME_IS_CLASS_P (t) && !CLASS_TYPE_P (f))
14511 if (complain & tf_error)
14512 error ("%qT resolves to %qT, which is is not a class type",
14513 t, f);
14514 else
14515 return error_mark_node;
14519 return cp_build_qualified_type_real
14520 (f, cp_type_quals (f) | cp_type_quals (t), complain);
14523 case UNBOUND_CLASS_TEMPLATE:
14525 tree ctx = tsubst_aggr_type (TYPE_CONTEXT (t), args, complain,
14526 in_decl, /*entering_scope=*/1);
14527 tree name = TYPE_IDENTIFIER (t);
14528 tree parm_list = DECL_TEMPLATE_PARMS (TYPE_NAME (t));
14530 if (ctx == error_mark_node || name == error_mark_node)
14531 return error_mark_node;
14533 if (parm_list)
14534 parm_list = tsubst_template_parms (parm_list, args, complain);
14535 return make_unbound_class_template (ctx, name, parm_list, complain);
14538 case TYPEOF_TYPE:
14540 tree type;
14542 ++cp_unevaluated_operand;
14543 ++c_inhibit_evaluation_warnings;
14545 type = tsubst_expr (TYPEOF_TYPE_EXPR (t), args,
14546 complain, in_decl,
14547 /*integral_constant_expression_p=*/false);
14549 --cp_unevaluated_operand;
14550 --c_inhibit_evaluation_warnings;
14552 type = finish_typeof (type);
14553 return cp_build_qualified_type_real (type,
14554 cp_type_quals (t)
14555 | cp_type_quals (type),
14556 complain);
14559 case DECLTYPE_TYPE:
14561 tree type;
14563 ++cp_unevaluated_operand;
14564 ++c_inhibit_evaluation_warnings;
14566 type = tsubst_copy_and_build (DECLTYPE_TYPE_EXPR (t), args,
14567 complain|tf_decltype, in_decl,
14568 /*function_p*/false,
14569 /*integral_constant_expression*/false);
14571 if (DECLTYPE_FOR_INIT_CAPTURE (t))
14573 if (type == NULL_TREE)
14575 if (complain & tf_error)
14576 error ("empty initializer in lambda init-capture");
14577 type = error_mark_node;
14579 else if (TREE_CODE (type) == TREE_LIST)
14580 type = build_x_compound_expr_from_list (type, ELK_INIT, complain);
14583 --cp_unevaluated_operand;
14584 --c_inhibit_evaluation_warnings;
14586 if (DECLTYPE_FOR_LAMBDA_CAPTURE (t))
14587 type = lambda_capture_field_type (type,
14588 DECLTYPE_FOR_INIT_CAPTURE (t),
14589 DECLTYPE_FOR_REF_CAPTURE (t));
14590 else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
14591 type = lambda_proxy_type (type);
14592 else
14594 bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
14595 if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
14596 && EXPR_P (type))
14597 /* In a template ~id could be either a complement expression
14598 or an unqualified-id naming a destructor; if instantiating
14599 it produces an expression, it's not an id-expression or
14600 member access. */
14601 id = false;
14602 type = finish_decltype_type (type, id, complain);
14604 return cp_build_qualified_type_real (type,
14605 cp_type_quals (t)
14606 | cp_type_quals (type),
14607 complain | tf_ignore_bad_quals);
14610 case UNDERLYING_TYPE:
14612 tree type = tsubst (UNDERLYING_TYPE_TYPE (t), args,
14613 complain, in_decl);
14614 return finish_underlying_type (type);
14617 case TYPE_ARGUMENT_PACK:
14618 case NONTYPE_ARGUMENT_PACK:
14620 tree r;
14622 if (code == NONTYPE_ARGUMENT_PACK)
14623 r = make_node (code);
14624 else
14625 r = cxx_make_type (code);
14627 tree pack_args = ARGUMENT_PACK_ARGS (t);
14628 pack_args = tsubst_template_args (pack_args, args, complain, in_decl);
14629 SET_ARGUMENT_PACK_ARGS (r, pack_args);
14631 return r;
14634 case VOID_CST:
14635 case INTEGER_CST:
14636 case REAL_CST:
14637 case STRING_CST:
14638 case PLUS_EXPR:
14639 case MINUS_EXPR:
14640 case NEGATE_EXPR:
14641 case NOP_EXPR:
14642 case INDIRECT_REF:
14643 case ADDR_EXPR:
14644 case CALL_EXPR:
14645 case ARRAY_REF:
14646 case SCOPE_REF:
14647 /* We should use one of the expression tsubsts for these codes. */
14648 gcc_unreachable ();
14650 default:
14651 sorry ("use of %qs in template", get_tree_code_name (code));
14652 return error_mark_node;
14656 /* tsubst a BASELINK. OBJECT_TYPE, if non-NULL, is the type of the
14657 expression on the left-hand side of the "." or "->" operator. We
14658 only do the lookup if we had a dependent BASELINK. Otherwise we
14659 adjust it onto the instantiated heirarchy. */
14661 static tree
14662 tsubst_baselink (tree baselink, tree object_type,
14663 tree args, tsubst_flags_t complain, tree in_decl)
14665 bool qualified_p = BASELINK_QUALIFIED_P (baselink);
14666 tree qualifying_scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (baselink));
14667 qualifying_scope = tsubst (qualifying_scope, args, complain, in_decl);
14669 tree optype = BASELINK_OPTYPE (baselink);
14670 optype = tsubst (optype, args, complain, in_decl);
14672 tree template_args = NULL_TREE;
14673 bool template_id_p = false;
14674 tree fns = BASELINK_FUNCTIONS (baselink);
14675 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
14677 template_id_p = true;
14678 template_args = TREE_OPERAND (fns, 1);
14679 fns = TREE_OPERAND (fns, 0);
14680 if (template_args)
14681 template_args = tsubst_template_args (template_args, args,
14682 complain, in_decl);
14685 tree binfo_type = BINFO_TYPE (BASELINK_BINFO (baselink));
14686 binfo_type = tsubst (binfo_type, args, complain, in_decl);
14687 bool dependent_p = binfo_type != BINFO_TYPE (BASELINK_BINFO (baselink));
14689 if (dependent_p)
14691 tree name = OVL_NAME (fns);
14692 if (IDENTIFIER_CONV_OP_P (name))
14693 name = make_conv_op_name (optype);
14695 if (name == complete_dtor_identifier)
14696 /* Treat as-if non-dependent below. */
14697 dependent_p = false;
14699 baselink = lookup_fnfields (qualifying_scope, name, /*protect=*/1);
14700 if (!baselink)
14702 if ((complain & tf_error)
14703 && constructor_name_p (name, qualifying_scope))
14704 error ("cannot call constructor %<%T::%D%> directly",
14705 qualifying_scope, name);
14706 return error_mark_node;
14709 if (BASELINK_P (baselink))
14710 fns = BASELINK_FUNCTIONS (baselink);
14712 else
14713 /* We're going to overwrite pieces below, make a duplicate. */
14714 baselink = copy_node (baselink);
14716 /* If lookup found a single function, mark it as used at this point.
14717 (If lookup found multiple functions the one selected later by
14718 overload resolution will be marked as used at that point.) */
14719 if (!template_id_p && !really_overloaded_fn (fns))
14721 tree fn = OVL_FIRST (fns);
14722 bool ok = mark_used (fn, complain);
14723 if (!ok && !(complain & tf_error))
14724 return error_mark_node;
14725 if (ok && BASELINK_P (baselink))
14726 /* We might have instantiated an auto function. */
14727 TREE_TYPE (baselink) = TREE_TYPE (fn);
14730 if (BASELINK_P (baselink))
14732 /* Add back the template arguments, if present. */
14733 if (template_id_p)
14734 BASELINK_FUNCTIONS (baselink)
14735 = build2 (TEMPLATE_ID_EXPR, unknown_type_node, fns, template_args);
14737 /* Update the conversion operator type. */
14738 BASELINK_OPTYPE (baselink) = optype;
14741 if (!object_type)
14742 object_type = current_class_type;
14744 if (qualified_p || !dependent_p)
14746 baselink = adjust_result_of_qualified_name_lookup (baselink,
14747 qualifying_scope,
14748 object_type);
14749 if (!qualified_p)
14750 /* We need to call adjust_result_of_qualified_name_lookup in case the
14751 destructor names a base class, but we unset BASELINK_QUALIFIED_P
14752 so that we still get virtual function binding. */
14753 BASELINK_QUALIFIED_P (baselink) = false;
14756 return baselink;
14759 /* Like tsubst_expr for a SCOPE_REF, given by QUALIFIED_ID. DONE is
14760 true if the qualified-id will be a postfix-expression in-and-of
14761 itself; false if more of the postfix-expression follows the
14762 QUALIFIED_ID. ADDRESS_P is true if the qualified-id is the operand
14763 of "&". */
14765 static tree
14766 tsubst_qualified_id (tree qualified_id, tree args,
14767 tsubst_flags_t complain, tree in_decl,
14768 bool done, bool address_p)
14770 tree expr;
14771 tree scope;
14772 tree name;
14773 bool is_template;
14774 tree template_args;
14775 location_t loc = UNKNOWN_LOCATION;
14777 gcc_assert (TREE_CODE (qualified_id) == SCOPE_REF);
14779 /* Figure out what name to look up. */
14780 name = TREE_OPERAND (qualified_id, 1);
14781 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14783 is_template = true;
14784 loc = EXPR_LOCATION (name);
14785 template_args = TREE_OPERAND (name, 1);
14786 if (template_args)
14787 template_args = tsubst_template_args (template_args, args,
14788 complain, in_decl);
14789 if (template_args == error_mark_node)
14790 return error_mark_node;
14791 name = TREE_OPERAND (name, 0);
14793 else
14795 is_template = false;
14796 template_args = NULL_TREE;
14799 /* Substitute into the qualifying scope. When there are no ARGS, we
14800 are just trying to simplify a non-dependent expression. In that
14801 case the qualifying scope may be dependent, and, in any case,
14802 substituting will not help. */
14803 scope = TREE_OPERAND (qualified_id, 0);
14804 if (args)
14806 scope = tsubst (scope, args, complain, in_decl);
14807 expr = tsubst_copy (name, args, complain, in_decl);
14809 else
14810 expr = name;
14812 if (dependent_scope_p (scope))
14814 if (is_template)
14815 expr = build_min_nt_loc (loc, TEMPLATE_ID_EXPR, expr, template_args);
14816 tree r = build_qualified_name (NULL_TREE, scope, expr,
14817 QUALIFIED_NAME_IS_TEMPLATE (qualified_id));
14818 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (qualified_id);
14819 return r;
14822 if (!BASELINK_P (name) && !DECL_P (expr))
14824 if (TREE_CODE (expr) == BIT_NOT_EXPR)
14826 /* A BIT_NOT_EXPR is used to represent a destructor. */
14827 if (!check_dtor_name (scope, TREE_OPERAND (expr, 0)))
14829 error ("qualifying type %qT does not match destructor name ~%qT",
14830 scope, TREE_OPERAND (expr, 0));
14831 expr = error_mark_node;
14833 else
14834 expr = lookup_qualified_name (scope, complete_dtor_identifier,
14835 /*is_type_p=*/0, false);
14837 else
14838 expr = lookup_qualified_name (scope, expr, /*is_type_p=*/0, false);
14839 if (TREE_CODE (TREE_CODE (expr) == TEMPLATE_DECL
14840 ? DECL_TEMPLATE_RESULT (expr) : expr) == TYPE_DECL)
14842 if (complain & tf_error)
14844 error ("dependent-name %qE is parsed as a non-type, but "
14845 "instantiation yields a type", qualified_id);
14846 inform (input_location, "say %<typename %E%> if a type is meant", qualified_id);
14848 return error_mark_node;
14852 if (DECL_P (expr))
14854 check_accessibility_of_qualified_id (expr, /*object_type=*/NULL_TREE,
14855 scope);
14856 /* Remember that there was a reference to this entity. */
14857 if (!mark_used (expr, complain) && !(complain & tf_error))
14858 return error_mark_node;
14861 if (expr == error_mark_node || TREE_CODE (expr) == TREE_LIST)
14863 if (complain & tf_error)
14864 qualified_name_lookup_error (scope,
14865 TREE_OPERAND (qualified_id, 1),
14866 expr, input_location);
14867 return error_mark_node;
14870 if (is_template)
14872 if (variable_template_p (expr))
14873 expr = lookup_and_finish_template_variable (expr, template_args,
14874 complain);
14875 else
14876 expr = lookup_template_function (expr, template_args);
14879 if (expr == error_mark_node && complain & tf_error)
14880 qualified_name_lookup_error (scope, TREE_OPERAND (qualified_id, 1),
14881 expr, input_location);
14882 else if (TYPE_P (scope))
14884 expr = (adjust_result_of_qualified_name_lookup
14885 (expr, scope, current_nonlambda_class_type ()));
14886 expr = (finish_qualified_id_expr
14887 (scope, expr, done, address_p && PTRMEM_OK_P (qualified_id),
14888 QUALIFIED_NAME_IS_TEMPLATE (qualified_id),
14889 /*template_arg_p=*/false, complain));
14892 /* Expressions do not generally have reference type. */
14893 if (TREE_CODE (expr) != SCOPE_REF
14894 /* However, if we're about to form a pointer-to-member, we just
14895 want the referenced member referenced. */
14896 && TREE_CODE (expr) != OFFSET_REF)
14897 expr = convert_from_reference (expr);
14899 if (REF_PARENTHESIZED_P (qualified_id))
14900 expr = force_paren_expr (expr);
14902 return expr;
14905 /* tsubst the initializer for a VAR_DECL. INIT is the unsubstituted
14906 initializer, DECL is the substituted VAR_DECL. Other arguments are as
14907 for tsubst. */
14909 static tree
14910 tsubst_init (tree init, tree decl, tree args,
14911 tsubst_flags_t complain, tree in_decl)
14913 if (!init)
14914 return NULL_TREE;
14916 init = tsubst_expr (init, args, complain, in_decl, false);
14918 if (!init && TREE_TYPE (decl) != error_mark_node)
14920 /* If we had an initializer but it
14921 instantiated to nothing,
14922 value-initialize the object. This will
14923 only occur when the initializer was a
14924 pack expansion where the parameter packs
14925 used in that expansion were of length
14926 zero. */
14927 init = build_value_init (TREE_TYPE (decl),
14928 complain);
14929 if (TREE_CODE (init) == AGGR_INIT_EXPR)
14930 init = get_target_expr_sfinae (init, complain);
14931 if (TREE_CODE (init) == TARGET_EXPR)
14932 TARGET_EXPR_DIRECT_INIT_P (init) = true;
14935 return init;
14938 /* Like tsubst, but deals with expressions. This function just replaces
14939 template parms; to finish processing the resultant expression, use
14940 tsubst_copy_and_build or tsubst_expr. */
14942 static tree
14943 tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
14945 enum tree_code code;
14946 tree r;
14948 if (t == NULL_TREE || t == error_mark_node || args == NULL_TREE)
14949 return t;
14951 code = TREE_CODE (t);
14953 switch (code)
14955 case PARM_DECL:
14956 r = retrieve_local_specialization (t);
14958 if (r == NULL_TREE)
14960 /* We get here for a use of 'this' in an NSDMI. */
14961 if (DECL_NAME (t) == this_identifier && current_class_ptr)
14962 return current_class_ptr;
14964 /* This can happen for a parameter name used later in a function
14965 declaration (such as in a late-specified return type). Just
14966 make a dummy decl, since it's only used for its type. */
14967 gcc_assert (cp_unevaluated_operand != 0);
14968 r = tsubst_decl (t, args, complain);
14969 /* Give it the template pattern as its context; its true context
14970 hasn't been instantiated yet and this is good enough for
14971 mangling. */
14972 DECL_CONTEXT (r) = DECL_CONTEXT (t);
14975 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
14976 r = argument_pack_select_arg (r);
14977 if (!mark_used (r, complain) && !(complain & tf_error))
14978 return error_mark_node;
14979 return r;
14981 case CONST_DECL:
14983 tree enum_type;
14984 tree v;
14986 if (DECL_TEMPLATE_PARM_P (t))
14987 return tsubst_copy (DECL_INITIAL (t), args, complain, in_decl);
14988 /* There is no need to substitute into namespace-scope
14989 enumerators. */
14990 if (DECL_NAMESPACE_SCOPE_P (t))
14991 return t;
14992 /* If ARGS is NULL, then T is known to be non-dependent. */
14993 if (args == NULL_TREE)
14994 return scalar_constant_value (t);
14996 /* Unfortunately, we cannot just call lookup_name here.
14997 Consider:
14999 template <int I> int f() {
15000 enum E { a = I };
15001 struct S { void g() { E e = a; } };
15004 When we instantiate f<7>::S::g(), say, lookup_name is not
15005 clever enough to find f<7>::a. */
15006 enum_type
15007 = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
15008 /*entering_scope=*/0);
15010 for (v = TYPE_VALUES (enum_type);
15011 v != NULL_TREE;
15012 v = TREE_CHAIN (v))
15013 if (TREE_PURPOSE (v) == DECL_NAME (t))
15014 return TREE_VALUE (v);
15016 /* We didn't find the name. That should never happen; if
15017 name-lookup found it during preliminary parsing, we
15018 should find it again here during instantiation. */
15019 gcc_unreachable ();
15021 return t;
15023 case FIELD_DECL:
15024 if (DECL_CONTEXT (t))
15026 tree ctx;
15028 ctx = tsubst_aggr_type (DECL_CONTEXT (t), args, complain, in_decl,
15029 /*entering_scope=*/1);
15030 if (ctx != DECL_CONTEXT (t))
15032 tree r = lookup_field (ctx, DECL_NAME (t), 0, false);
15033 if (!r)
15035 if (complain & tf_error)
15036 error ("using invalid field %qD", t);
15037 return error_mark_node;
15039 return r;
15043 return t;
15045 case VAR_DECL:
15046 case FUNCTION_DECL:
15047 if (DECL_LANG_SPECIFIC (t) && DECL_TEMPLATE_INFO (t))
15048 r = tsubst (t, args, complain, in_decl);
15049 else if (local_variable_p (t)
15050 && uses_template_parms (DECL_CONTEXT (t)))
15052 r = retrieve_local_specialization (t);
15053 if (r == NULL_TREE)
15055 /* First try name lookup to find the instantiation. */
15056 r = lookup_name (DECL_NAME (t));
15057 if (r && !is_capture_proxy (r))
15059 /* Make sure that the one we found is the one we want. */
15060 tree ctx = enclosing_instantiation_of (DECL_CONTEXT (t));
15061 if (ctx != DECL_CONTEXT (r))
15062 r = NULL_TREE;
15065 if (r)
15066 /* OK */;
15067 else
15069 /* This can happen for a variable used in a
15070 late-specified return type of a local lambda, or for a
15071 local static or constant. Building a new VAR_DECL
15072 should be OK in all those cases. */
15073 r = tsubst_decl (t, args, complain);
15074 if (local_specializations)
15075 /* Avoid infinite recursion (79640). */
15076 register_local_specialization (r, t);
15077 if (decl_maybe_constant_var_p (r))
15079 /* We can't call cp_finish_decl, so handle the
15080 initializer by hand. */
15081 tree init = tsubst_init (DECL_INITIAL (t), r, args,
15082 complain, in_decl);
15083 if (!processing_template_decl)
15084 init = maybe_constant_init (init);
15085 if (processing_template_decl
15086 ? potential_constant_expression (init)
15087 : reduced_constant_expression_p (init))
15088 DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
15089 = TREE_CONSTANT (r) = true;
15090 DECL_INITIAL (r) = init;
15091 if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
15092 TREE_TYPE (r)
15093 = do_auto_deduction (TREE_TYPE (r), init, auto_node,
15094 complain, adc_variable_type);
15096 gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
15097 || decl_constant_var_p (r)
15098 || errorcount || sorrycount);
15099 if (!processing_template_decl
15100 && !TREE_STATIC (r))
15101 r = process_outer_var_ref (r, complain);
15103 /* Remember this for subsequent uses. */
15104 if (local_specializations)
15105 register_local_specialization (r, t);
15107 if (TREE_CODE (r) == ARGUMENT_PACK_SELECT)
15108 r = argument_pack_select_arg (r);
15110 else
15111 r = t;
15112 if (!mark_used (r, complain))
15113 return error_mark_node;
15114 return r;
15116 case NAMESPACE_DECL:
15117 return t;
15119 case OVERLOAD:
15120 /* An OVERLOAD will always be a non-dependent overload set; an
15121 overload set from function scope will just be represented with an
15122 IDENTIFIER_NODE, and from class scope with a BASELINK. */
15123 gcc_assert (!uses_template_parms (t));
15124 /* We must have marked any lookups as persistent. */
15125 gcc_assert (!OVL_LOOKUP_P (t) || OVL_USED_P (t));
15126 return t;
15128 case BASELINK:
15129 return tsubst_baselink (t, current_nonlambda_class_type (),
15130 args, complain, in_decl);
15132 case TEMPLATE_DECL:
15133 if (DECL_TEMPLATE_TEMPLATE_PARM_P (t))
15134 return tsubst (TREE_TYPE (DECL_TEMPLATE_RESULT (t)),
15135 args, complain, in_decl);
15136 else if (DECL_FUNCTION_TEMPLATE_P (t) && DECL_MEMBER_TEMPLATE_P (t))
15137 return tsubst (t, args, complain, in_decl);
15138 else if (DECL_CLASS_SCOPE_P (t)
15139 && uses_template_parms (DECL_CONTEXT (t)))
15141 /* Template template argument like the following example need
15142 special treatment:
15144 template <template <class> class TT> struct C {};
15145 template <class T> struct D {
15146 template <class U> struct E {};
15147 C<E> c; // #1
15149 D<int> d; // #2
15151 We are processing the template argument `E' in #1 for
15152 the template instantiation #2. Originally, `E' is a
15153 TEMPLATE_DECL with `D<T>' as its DECL_CONTEXT. Now we
15154 have to substitute this with one having context `D<int>'. */
15156 tree context = tsubst (DECL_CONTEXT (t), args, complain, in_decl);
15157 if (dependent_scope_p (context))
15159 /* When rewriting a constructor into a deduction guide, a
15160 non-dependent name can become dependent, so memtmpl<args>
15161 becomes context::template memtmpl<args>. */
15162 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15163 return build_qualified_name (type, context, DECL_NAME (t),
15164 /*template*/true);
15166 return lookup_field (context, DECL_NAME(t), 0, false);
15168 else
15169 /* Ordinary template template argument. */
15170 return t;
15172 case NON_LVALUE_EXPR:
15173 case VIEW_CONVERT_EXPR:
15175 /* Handle location wrappers by substituting the wrapped node
15176 first, *then* reusing the resulting type. Doing the type
15177 first ensures that we handle template parameters and
15178 parameter pack expansions. */
15179 gcc_assert (location_wrapper_p (t));
15180 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15181 return maybe_wrap_with_location (op0, EXPR_LOCATION (t));
15184 case CAST_EXPR:
15185 case REINTERPRET_CAST_EXPR:
15186 case CONST_CAST_EXPR:
15187 case STATIC_CAST_EXPR:
15188 case DYNAMIC_CAST_EXPR:
15189 case IMPLICIT_CONV_EXPR:
15190 case CONVERT_EXPR:
15191 case NOP_EXPR:
15193 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15194 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15195 return build1 (code, type, op0);
15198 case SIZEOF_EXPR:
15199 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
15200 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
15202 tree expanded, op = TREE_OPERAND (t, 0);
15203 int len = 0;
15205 if (SIZEOF_EXPR_TYPE_P (t))
15206 op = TREE_TYPE (op);
15208 ++cp_unevaluated_operand;
15209 ++c_inhibit_evaluation_warnings;
15210 /* We only want to compute the number of arguments. */
15211 if (PACK_EXPANSION_P (op))
15212 expanded = tsubst_pack_expansion (op, args, complain, in_decl);
15213 else
15214 expanded = tsubst_template_args (ARGUMENT_PACK_ARGS (op),
15215 args, complain, in_decl);
15216 --cp_unevaluated_operand;
15217 --c_inhibit_evaluation_warnings;
15219 if (TREE_CODE (expanded) == TREE_VEC)
15221 len = TREE_VEC_LENGTH (expanded);
15222 /* Set TREE_USED for the benefit of -Wunused. */
15223 for (int i = 0; i < len; i++)
15224 if (DECL_P (TREE_VEC_ELT (expanded, i)))
15225 TREE_USED (TREE_VEC_ELT (expanded, i)) = true;
15228 if (expanded == error_mark_node)
15229 return error_mark_node;
15230 else if (PACK_EXPANSION_P (expanded)
15231 || (TREE_CODE (expanded) == TREE_VEC
15232 && pack_expansion_args_count (expanded)))
15235 if (PACK_EXPANSION_P (expanded))
15236 /* OK. */;
15237 else if (TREE_VEC_LENGTH (expanded) == 1)
15238 expanded = TREE_VEC_ELT (expanded, 0);
15239 else
15240 expanded = make_argument_pack (expanded);
15242 if (TYPE_P (expanded))
15243 return cxx_sizeof_or_alignof_type (expanded, SIZEOF_EXPR,
15244 complain & tf_error);
15245 else
15246 return cxx_sizeof_or_alignof_expr (expanded, SIZEOF_EXPR,
15247 complain & tf_error);
15249 else
15250 return build_int_cst (size_type_node, len);
15252 if (SIZEOF_EXPR_TYPE_P (t))
15254 r = tsubst (TREE_TYPE (TREE_OPERAND (t, 0)),
15255 args, complain, in_decl);
15256 r = build1 (NOP_EXPR, r, error_mark_node);
15257 r = build1 (SIZEOF_EXPR,
15258 tsubst (TREE_TYPE (t), args, complain, in_decl), r);
15259 SIZEOF_EXPR_TYPE_P (r) = 1;
15260 return r;
15262 /* Fall through */
15264 case INDIRECT_REF:
15265 case NEGATE_EXPR:
15266 case TRUTH_NOT_EXPR:
15267 case BIT_NOT_EXPR:
15268 case ADDR_EXPR:
15269 case UNARY_PLUS_EXPR: /* Unary + */
15270 case ALIGNOF_EXPR:
15271 case AT_ENCODE_EXPR:
15272 case ARROW_EXPR:
15273 case THROW_EXPR:
15274 case TYPEID_EXPR:
15275 case REALPART_EXPR:
15276 case IMAGPART_EXPR:
15277 case PAREN_EXPR:
15279 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15280 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15281 return build1 (code, type, op0);
15284 case COMPONENT_REF:
15286 tree object;
15287 tree name;
15289 object = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15290 name = TREE_OPERAND (t, 1);
15291 if (TREE_CODE (name) == BIT_NOT_EXPR)
15293 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15294 complain, in_decl);
15295 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15297 else if (TREE_CODE (name) == SCOPE_REF
15298 && TREE_CODE (TREE_OPERAND (name, 1)) == BIT_NOT_EXPR)
15300 tree base = tsubst_copy (TREE_OPERAND (name, 0), args,
15301 complain, in_decl);
15302 name = TREE_OPERAND (name, 1);
15303 name = tsubst_copy (TREE_OPERAND (name, 0), args,
15304 complain, in_decl);
15305 name = build1 (BIT_NOT_EXPR, NULL_TREE, name);
15306 name = build_qualified_name (/*type=*/NULL_TREE,
15307 base, name,
15308 /*template_p=*/false);
15310 else if (BASELINK_P (name))
15311 name = tsubst_baselink (name,
15312 non_reference (TREE_TYPE (object)),
15313 args, complain,
15314 in_decl);
15315 else
15316 name = tsubst_copy (name, args, complain, in_decl);
15317 return build_nt (COMPONENT_REF, object, name, NULL_TREE);
15320 case PLUS_EXPR:
15321 case MINUS_EXPR:
15322 case MULT_EXPR:
15323 case TRUNC_DIV_EXPR:
15324 case CEIL_DIV_EXPR:
15325 case FLOOR_DIV_EXPR:
15326 case ROUND_DIV_EXPR:
15327 case EXACT_DIV_EXPR:
15328 case BIT_AND_EXPR:
15329 case BIT_IOR_EXPR:
15330 case BIT_XOR_EXPR:
15331 case TRUNC_MOD_EXPR:
15332 case FLOOR_MOD_EXPR:
15333 case TRUTH_ANDIF_EXPR:
15334 case TRUTH_ORIF_EXPR:
15335 case TRUTH_AND_EXPR:
15336 case TRUTH_OR_EXPR:
15337 case RSHIFT_EXPR:
15338 case LSHIFT_EXPR:
15339 case RROTATE_EXPR:
15340 case LROTATE_EXPR:
15341 case EQ_EXPR:
15342 case NE_EXPR:
15343 case MAX_EXPR:
15344 case MIN_EXPR:
15345 case LE_EXPR:
15346 case GE_EXPR:
15347 case LT_EXPR:
15348 case GT_EXPR:
15349 case COMPOUND_EXPR:
15350 case DOTSTAR_EXPR:
15351 case MEMBER_REF:
15352 case PREDECREMENT_EXPR:
15353 case PREINCREMENT_EXPR:
15354 case POSTDECREMENT_EXPR:
15355 case POSTINCREMENT_EXPR:
15357 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15358 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15359 return build_nt (code, op0, op1);
15362 case SCOPE_REF:
15364 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15365 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15366 return build_qualified_name (/*type=*/NULL_TREE, op0, op1,
15367 QUALIFIED_NAME_IS_TEMPLATE (t));
15370 case ARRAY_REF:
15372 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15373 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15374 return build_nt (ARRAY_REF, op0, op1, NULL_TREE, NULL_TREE);
15377 case CALL_EXPR:
15379 int n = VL_EXP_OPERAND_LENGTH (t);
15380 tree result = build_vl_exp (CALL_EXPR, n);
15381 int i;
15382 for (i = 0; i < n; i++)
15383 TREE_OPERAND (t, i) = tsubst_copy (TREE_OPERAND (t, i), args,
15384 complain, in_decl);
15385 return result;
15388 case COND_EXPR:
15389 case MODOP_EXPR:
15390 case PSEUDO_DTOR_EXPR:
15391 case VEC_PERM_EXPR:
15393 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15394 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15395 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15396 r = build_nt (code, op0, op1, op2);
15397 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
15398 return r;
15401 case NEW_EXPR:
15403 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15404 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15405 tree op2 = tsubst_copy (TREE_OPERAND (t, 2), args, complain, in_decl);
15406 r = build_nt (code, op0, op1, op2);
15407 NEW_EXPR_USE_GLOBAL (r) = NEW_EXPR_USE_GLOBAL (t);
15408 return r;
15411 case DELETE_EXPR:
15413 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15414 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15415 r = build_nt (code, op0, op1);
15416 DELETE_EXPR_USE_GLOBAL (r) = DELETE_EXPR_USE_GLOBAL (t);
15417 DELETE_EXPR_USE_VEC (r) = DELETE_EXPR_USE_VEC (t);
15418 return r;
15421 case TEMPLATE_ID_EXPR:
15423 /* Substituted template arguments */
15424 tree fn = TREE_OPERAND (t, 0);
15425 tree targs = TREE_OPERAND (t, 1);
15427 fn = tsubst_copy (fn, args, complain, in_decl);
15428 if (targs)
15429 targs = tsubst_template_args (targs, args, complain, in_decl);
15431 return lookup_template_function (fn, targs);
15434 case TREE_LIST:
15436 tree purpose, value, chain;
15438 if (t == void_list_node)
15439 return t;
15441 purpose = TREE_PURPOSE (t);
15442 if (purpose)
15443 purpose = tsubst_copy (purpose, args, complain, in_decl);
15444 value = TREE_VALUE (t);
15445 if (value)
15446 value = tsubst_copy (value, args, complain, in_decl);
15447 chain = TREE_CHAIN (t);
15448 if (chain && chain != void_type_node)
15449 chain = tsubst_copy (chain, args, complain, in_decl);
15450 if (purpose == TREE_PURPOSE (t)
15451 && value == TREE_VALUE (t)
15452 && chain == TREE_CHAIN (t))
15453 return t;
15454 return tree_cons (purpose, value, chain);
15457 case RECORD_TYPE:
15458 case UNION_TYPE:
15459 case ENUMERAL_TYPE:
15460 case INTEGER_TYPE:
15461 case TEMPLATE_TYPE_PARM:
15462 case TEMPLATE_TEMPLATE_PARM:
15463 case BOUND_TEMPLATE_TEMPLATE_PARM:
15464 case TEMPLATE_PARM_INDEX:
15465 case POINTER_TYPE:
15466 case REFERENCE_TYPE:
15467 case OFFSET_TYPE:
15468 case FUNCTION_TYPE:
15469 case METHOD_TYPE:
15470 case ARRAY_TYPE:
15471 case TYPENAME_TYPE:
15472 case UNBOUND_CLASS_TEMPLATE:
15473 case TYPEOF_TYPE:
15474 case DECLTYPE_TYPE:
15475 case TYPE_DECL:
15476 return tsubst (t, args, complain, in_decl);
15478 case USING_DECL:
15479 t = DECL_NAME (t);
15480 /* Fall through. */
15481 case IDENTIFIER_NODE:
15482 if (IDENTIFIER_CONV_OP_P (t))
15484 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15485 return make_conv_op_name (new_type);
15487 else
15488 return t;
15490 case CONSTRUCTOR:
15491 /* This is handled by tsubst_copy_and_build. */
15492 gcc_unreachable ();
15494 case VA_ARG_EXPR:
15496 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15497 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15498 return build_x_va_arg (EXPR_LOCATION (t), op0, type);
15501 case CLEANUP_POINT_EXPR:
15502 /* We shouldn't have built any of these during initial template
15503 generation. Instead, they should be built during instantiation
15504 in response to the saved STMT_IS_FULL_EXPR_P setting. */
15505 gcc_unreachable ();
15507 case OFFSET_REF:
15509 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15510 tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, complain, in_decl);
15511 tree op1 = tsubst_copy (TREE_OPERAND (t, 1), args, complain, in_decl);
15512 r = build2 (code, type, op0, op1);
15513 PTRMEM_OK_P (r) = PTRMEM_OK_P (t);
15514 if (!mark_used (TREE_OPERAND (r, 1), complain)
15515 && !(complain & tf_error))
15516 return error_mark_node;
15517 return r;
15520 case EXPR_PACK_EXPANSION:
15521 error ("invalid use of pack expansion expression");
15522 return error_mark_node;
15524 case NONTYPE_ARGUMENT_PACK:
15525 error ("use %<...%> to expand argument pack");
15526 return error_mark_node;
15528 case VOID_CST:
15529 gcc_checking_assert (t == void_node && VOID_TYPE_P (TREE_TYPE (t)));
15530 return t;
15532 case INTEGER_CST:
15533 case REAL_CST:
15534 case STRING_CST:
15535 case COMPLEX_CST:
15537 /* Instantiate any typedefs in the type. */
15538 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
15539 r = fold_convert (type, t);
15540 gcc_assert (TREE_CODE (r) == code);
15541 return r;
15544 case PTRMEM_CST:
15545 /* These can sometimes show up in a partial instantiation, but never
15546 involve template parms. */
15547 gcc_assert (!uses_template_parms (t));
15548 return t;
15550 case UNARY_LEFT_FOLD_EXPR:
15551 return tsubst_unary_left_fold (t, args, complain, in_decl);
15552 case UNARY_RIGHT_FOLD_EXPR:
15553 return tsubst_unary_right_fold (t, args, complain, in_decl);
15554 case BINARY_LEFT_FOLD_EXPR:
15555 return tsubst_binary_left_fold (t, args, complain, in_decl);
15556 case BINARY_RIGHT_FOLD_EXPR:
15557 return tsubst_binary_right_fold (t, args, complain, in_decl);
15558 case PREDICT_EXPR:
15559 return t;
15561 case DEBUG_BEGIN_STMT:
15562 /* ??? There's no point in copying it for now, but maybe some
15563 day it will contain more information, such as a pointer back
15564 to the containing function, inlined copy or so. */
15565 return t;
15567 default:
15568 /* We shouldn't get here, but keep going if !flag_checking. */
15569 if (flag_checking)
15570 gcc_unreachable ();
15571 return t;
15575 /* Helper function for tsubst_omp_clauses, used for instantiation of
15576 OMP_CLAUSE_DECL of clauses. */
15578 static tree
15579 tsubst_omp_clause_decl (tree decl, tree args, tsubst_flags_t complain,
15580 tree in_decl)
15582 if (decl == NULL_TREE)
15583 return NULL_TREE;
15585 /* Handle an OpenMP array section represented as a TREE_LIST (or
15586 OMP_CLAUSE_DEPEND_KIND). An OMP_CLAUSE_DEPEND (with a depend
15587 kind of OMP_CLAUSE_DEPEND_SINK) can also be represented as a
15588 TREE_LIST. We can handle it exactly the same as an array section
15589 (purpose, value, and a chain), even though the nomenclature
15590 (low_bound, length, etc) is different. */
15591 if (TREE_CODE (decl) == TREE_LIST)
15593 tree low_bound
15594 = tsubst_expr (TREE_PURPOSE (decl), args, complain, in_decl,
15595 /*integral_constant_expression_p=*/false);
15596 tree length = tsubst_expr (TREE_VALUE (decl), args, complain, in_decl,
15597 /*integral_constant_expression_p=*/false);
15598 tree chain = tsubst_omp_clause_decl (TREE_CHAIN (decl), args, complain,
15599 in_decl);
15600 if (TREE_PURPOSE (decl) == low_bound
15601 && TREE_VALUE (decl) == length
15602 && TREE_CHAIN (decl) == chain)
15603 return decl;
15604 tree ret = tree_cons (low_bound, length, chain);
15605 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (ret)
15606 = OMP_CLAUSE_DEPEND_SINK_NEGATIVE (decl);
15607 return ret;
15609 tree ret = tsubst_expr (decl, args, complain, in_decl,
15610 /*integral_constant_expression_p=*/false);
15611 /* Undo convert_from_reference tsubst_expr could have called. */
15612 if (decl
15613 && REFERENCE_REF_P (ret)
15614 && !REFERENCE_REF_P (decl))
15615 ret = TREE_OPERAND (ret, 0);
15616 return ret;
15619 /* Like tsubst_copy, but specifically for OpenMP clauses. */
15621 static tree
15622 tsubst_omp_clauses (tree clauses, enum c_omp_region_type ort,
15623 tree args, tsubst_flags_t complain, tree in_decl)
15625 tree new_clauses = NULL_TREE, nc, oc;
15626 tree linear_no_step = NULL_TREE;
15628 for (oc = clauses; oc ; oc = OMP_CLAUSE_CHAIN (oc))
15630 nc = copy_node (oc);
15631 OMP_CLAUSE_CHAIN (nc) = new_clauses;
15632 new_clauses = nc;
15634 switch (OMP_CLAUSE_CODE (nc))
15636 case OMP_CLAUSE_LASTPRIVATE:
15637 if (OMP_CLAUSE_LASTPRIVATE_STMT (oc))
15639 OMP_CLAUSE_LASTPRIVATE_STMT (nc) = push_stmt_list ();
15640 tsubst_expr (OMP_CLAUSE_LASTPRIVATE_STMT (oc), args, complain,
15641 in_decl, /*integral_constant_expression_p=*/false);
15642 OMP_CLAUSE_LASTPRIVATE_STMT (nc)
15643 = pop_stmt_list (OMP_CLAUSE_LASTPRIVATE_STMT (nc));
15645 /* FALLTHRU */
15646 case OMP_CLAUSE_PRIVATE:
15647 case OMP_CLAUSE_SHARED:
15648 case OMP_CLAUSE_FIRSTPRIVATE:
15649 case OMP_CLAUSE_COPYIN:
15650 case OMP_CLAUSE_COPYPRIVATE:
15651 case OMP_CLAUSE_UNIFORM:
15652 case OMP_CLAUSE_DEPEND:
15653 case OMP_CLAUSE_FROM:
15654 case OMP_CLAUSE_TO:
15655 case OMP_CLAUSE_MAP:
15656 case OMP_CLAUSE_USE_DEVICE_PTR:
15657 case OMP_CLAUSE_IS_DEVICE_PTR:
15658 OMP_CLAUSE_DECL (nc)
15659 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15660 in_decl);
15661 break;
15662 case OMP_CLAUSE_TILE:
15663 case OMP_CLAUSE_IF:
15664 case OMP_CLAUSE_NUM_THREADS:
15665 case OMP_CLAUSE_SCHEDULE:
15666 case OMP_CLAUSE_COLLAPSE:
15667 case OMP_CLAUSE_FINAL:
15668 case OMP_CLAUSE_DEVICE:
15669 case OMP_CLAUSE_DIST_SCHEDULE:
15670 case OMP_CLAUSE_NUM_TEAMS:
15671 case OMP_CLAUSE_THREAD_LIMIT:
15672 case OMP_CLAUSE_SAFELEN:
15673 case OMP_CLAUSE_SIMDLEN:
15674 case OMP_CLAUSE_NUM_TASKS:
15675 case OMP_CLAUSE_GRAINSIZE:
15676 case OMP_CLAUSE_PRIORITY:
15677 case OMP_CLAUSE_ORDERED:
15678 case OMP_CLAUSE_HINT:
15679 case OMP_CLAUSE_NUM_GANGS:
15680 case OMP_CLAUSE_NUM_WORKERS:
15681 case OMP_CLAUSE_VECTOR_LENGTH:
15682 case OMP_CLAUSE_WORKER:
15683 case OMP_CLAUSE_VECTOR:
15684 case OMP_CLAUSE_ASYNC:
15685 case OMP_CLAUSE_WAIT:
15686 OMP_CLAUSE_OPERAND (nc, 0)
15687 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 0), args, complain,
15688 in_decl, /*integral_constant_expression_p=*/false);
15689 break;
15690 case OMP_CLAUSE_REDUCTION:
15691 if (OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc))
15693 tree placeholder = OMP_CLAUSE_REDUCTION_PLACEHOLDER (oc);
15694 if (TREE_CODE (placeholder) == SCOPE_REF)
15696 tree scope = tsubst (TREE_OPERAND (placeholder, 0), args,
15697 complain, in_decl);
15698 OMP_CLAUSE_REDUCTION_PLACEHOLDER (nc)
15699 = build_qualified_name (NULL_TREE, scope,
15700 TREE_OPERAND (placeholder, 1),
15701 false);
15703 else
15704 gcc_assert (identifier_p (placeholder));
15706 OMP_CLAUSE_DECL (nc)
15707 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15708 in_decl);
15709 break;
15710 case OMP_CLAUSE_GANG:
15711 case OMP_CLAUSE_ALIGNED:
15712 OMP_CLAUSE_DECL (nc)
15713 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15714 in_decl);
15715 OMP_CLAUSE_OPERAND (nc, 1)
15716 = tsubst_expr (OMP_CLAUSE_OPERAND (oc, 1), args, complain,
15717 in_decl, /*integral_constant_expression_p=*/false);
15718 break;
15719 case OMP_CLAUSE_LINEAR:
15720 OMP_CLAUSE_DECL (nc)
15721 = tsubst_omp_clause_decl (OMP_CLAUSE_DECL (oc), args, complain,
15722 in_decl);
15723 if (OMP_CLAUSE_LINEAR_STEP (oc) == NULL_TREE)
15725 gcc_assert (!linear_no_step);
15726 linear_no_step = nc;
15728 else if (OMP_CLAUSE_LINEAR_VARIABLE_STRIDE (oc))
15729 OMP_CLAUSE_LINEAR_STEP (nc)
15730 = tsubst_omp_clause_decl (OMP_CLAUSE_LINEAR_STEP (oc), args,
15731 complain, in_decl);
15732 else
15733 OMP_CLAUSE_LINEAR_STEP (nc)
15734 = tsubst_expr (OMP_CLAUSE_LINEAR_STEP (oc), args, complain,
15735 in_decl,
15736 /*integral_constant_expression_p=*/false);
15737 break;
15738 case OMP_CLAUSE_NOWAIT:
15739 case OMP_CLAUSE_DEFAULT:
15740 case OMP_CLAUSE_UNTIED:
15741 case OMP_CLAUSE_MERGEABLE:
15742 case OMP_CLAUSE_INBRANCH:
15743 case OMP_CLAUSE_NOTINBRANCH:
15744 case OMP_CLAUSE_PROC_BIND:
15745 case OMP_CLAUSE_FOR:
15746 case OMP_CLAUSE_PARALLEL:
15747 case OMP_CLAUSE_SECTIONS:
15748 case OMP_CLAUSE_TASKGROUP:
15749 case OMP_CLAUSE_NOGROUP:
15750 case OMP_CLAUSE_THREADS:
15751 case OMP_CLAUSE_SIMD:
15752 case OMP_CLAUSE_DEFAULTMAP:
15753 case OMP_CLAUSE_INDEPENDENT:
15754 case OMP_CLAUSE_AUTO:
15755 case OMP_CLAUSE_SEQ:
15756 break;
15757 default:
15758 gcc_unreachable ();
15760 if ((ort & C_ORT_OMP_DECLARE_SIMD) == C_ORT_OMP)
15761 switch (OMP_CLAUSE_CODE (nc))
15763 case OMP_CLAUSE_SHARED:
15764 case OMP_CLAUSE_PRIVATE:
15765 case OMP_CLAUSE_FIRSTPRIVATE:
15766 case OMP_CLAUSE_LASTPRIVATE:
15767 case OMP_CLAUSE_COPYPRIVATE:
15768 case OMP_CLAUSE_LINEAR:
15769 case OMP_CLAUSE_REDUCTION:
15770 case OMP_CLAUSE_USE_DEVICE_PTR:
15771 case OMP_CLAUSE_IS_DEVICE_PTR:
15772 /* tsubst_expr on SCOPE_REF results in returning
15773 finish_non_static_data_member result. Undo that here. */
15774 if (TREE_CODE (OMP_CLAUSE_DECL (oc)) == SCOPE_REF
15775 && (TREE_CODE (TREE_OPERAND (OMP_CLAUSE_DECL (oc), 1))
15776 == IDENTIFIER_NODE))
15778 tree t = OMP_CLAUSE_DECL (nc);
15779 tree v = t;
15780 while (v)
15781 switch (TREE_CODE (v))
15783 case COMPONENT_REF:
15784 case MEM_REF:
15785 case INDIRECT_REF:
15786 CASE_CONVERT:
15787 case POINTER_PLUS_EXPR:
15788 v = TREE_OPERAND (v, 0);
15789 continue;
15790 case PARM_DECL:
15791 if (DECL_CONTEXT (v) == current_function_decl
15792 && DECL_ARTIFICIAL (v)
15793 && DECL_NAME (v) == this_identifier)
15794 OMP_CLAUSE_DECL (nc) = TREE_OPERAND (t, 1);
15795 /* FALLTHRU */
15796 default:
15797 v = NULL_TREE;
15798 break;
15801 else if (VAR_P (OMP_CLAUSE_DECL (oc))
15802 && DECL_HAS_VALUE_EXPR_P (OMP_CLAUSE_DECL (oc))
15803 && DECL_ARTIFICIAL (OMP_CLAUSE_DECL (oc))
15804 && DECL_LANG_SPECIFIC (OMP_CLAUSE_DECL (oc))
15805 && DECL_OMP_PRIVATIZED_MEMBER (OMP_CLAUSE_DECL (oc)))
15807 tree decl = OMP_CLAUSE_DECL (nc);
15808 if (VAR_P (decl))
15810 retrofit_lang_decl (decl);
15811 DECL_OMP_PRIVATIZED_MEMBER (decl) = 1;
15814 break;
15815 default:
15816 break;
15820 new_clauses = nreverse (new_clauses);
15821 if (ort != C_ORT_OMP_DECLARE_SIMD)
15823 new_clauses = finish_omp_clauses (new_clauses, ort);
15824 if (linear_no_step)
15825 for (nc = new_clauses; nc; nc = OMP_CLAUSE_CHAIN (nc))
15826 if (nc == linear_no_step)
15828 OMP_CLAUSE_LINEAR_STEP (nc) = NULL_TREE;
15829 break;
15832 return new_clauses;
15835 /* Like tsubst_copy_and_build, but unshare TREE_LIST nodes. */
15837 static tree
15838 tsubst_copy_asm_operands (tree t, tree args, tsubst_flags_t complain,
15839 tree in_decl)
15841 #define RECUR(t) tsubst_copy_asm_operands (t, args, complain, in_decl)
15843 tree purpose, value, chain;
15845 if (t == NULL)
15846 return t;
15848 if (TREE_CODE (t) != TREE_LIST)
15849 return tsubst_copy_and_build (t, args, complain, in_decl,
15850 /*function_p=*/false,
15851 /*integral_constant_expression_p=*/false);
15853 if (t == void_list_node)
15854 return t;
15856 purpose = TREE_PURPOSE (t);
15857 if (purpose)
15858 purpose = RECUR (purpose);
15859 value = TREE_VALUE (t);
15860 if (value)
15862 if (TREE_CODE (value) != LABEL_DECL)
15863 value = RECUR (value);
15864 else
15866 value = lookup_label (DECL_NAME (value));
15867 gcc_assert (TREE_CODE (value) == LABEL_DECL);
15868 TREE_USED (value) = 1;
15871 chain = TREE_CHAIN (t);
15872 if (chain && chain != void_type_node)
15873 chain = RECUR (chain);
15874 return tree_cons (purpose, value, chain);
15875 #undef RECUR
15878 /* Used to temporarily communicate the list of #pragma omp parallel
15879 clauses to #pragma omp for instantiation if they are combined
15880 together. */
15882 static tree *omp_parallel_combined_clauses;
15884 /* Substitute one OMP_FOR iterator. */
15886 static void
15887 tsubst_omp_for_iterator (tree t, int i, tree declv, tree orig_declv,
15888 tree initv, tree condv, tree incrv, tree *clauses,
15889 tree args, tsubst_flags_t complain, tree in_decl,
15890 bool integral_constant_expression_p)
15892 #define RECUR(NODE) \
15893 tsubst_expr ((NODE), args, complain, in_decl, \
15894 integral_constant_expression_p)
15895 tree decl, init, cond, incr;
15897 init = TREE_VEC_ELT (OMP_FOR_INIT (t), i);
15898 gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
15900 if (orig_declv && OMP_FOR_ORIG_DECLS (t))
15902 tree o = TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (t), i);
15903 TREE_VEC_ELT (orig_declv, i) = RECUR (o);
15906 decl = TREE_OPERAND (init, 0);
15907 init = TREE_OPERAND (init, 1);
15908 tree decl_expr = NULL_TREE;
15909 if (init && TREE_CODE (init) == DECL_EXPR)
15911 /* We need to jump through some hoops to handle declarations in the
15912 init-statement, since we might need to handle auto deduction,
15913 but we need to keep control of initialization. */
15914 decl_expr = init;
15915 init = DECL_INITIAL (DECL_EXPR_DECL (init));
15916 decl = tsubst_decl (decl, args, complain);
15918 else
15920 if (TREE_CODE (decl) == SCOPE_REF)
15922 decl = RECUR (decl);
15923 if (TREE_CODE (decl) == COMPONENT_REF)
15925 tree v = decl;
15926 while (v)
15927 switch (TREE_CODE (v))
15929 case COMPONENT_REF:
15930 case MEM_REF:
15931 case INDIRECT_REF:
15932 CASE_CONVERT:
15933 case POINTER_PLUS_EXPR:
15934 v = TREE_OPERAND (v, 0);
15935 continue;
15936 case PARM_DECL:
15937 if (DECL_CONTEXT (v) == current_function_decl
15938 && DECL_ARTIFICIAL (v)
15939 && DECL_NAME (v) == this_identifier)
15941 decl = TREE_OPERAND (decl, 1);
15942 decl = omp_privatize_field (decl, false);
15944 /* FALLTHRU */
15945 default:
15946 v = NULL_TREE;
15947 break;
15951 else
15952 decl = RECUR (decl);
15954 init = RECUR (init);
15956 tree auto_node = type_uses_auto (TREE_TYPE (decl));
15957 if (auto_node && init)
15958 TREE_TYPE (decl)
15959 = do_auto_deduction (TREE_TYPE (decl), init, auto_node, complain);
15961 gcc_assert (!type_dependent_expression_p (decl));
15963 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15965 if (decl_expr)
15967 /* Declare the variable, but don't let that initialize it. */
15968 tree init_sav = DECL_INITIAL (DECL_EXPR_DECL (decl_expr));
15969 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = NULL_TREE;
15970 RECUR (decl_expr);
15971 DECL_INITIAL (DECL_EXPR_DECL (decl_expr)) = init_sav;
15974 cond = RECUR (TREE_VEC_ELT (OMP_FOR_COND (t), i));
15975 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
15976 if (TREE_CODE (incr) == MODIFY_EXPR)
15978 tree lhs = RECUR (TREE_OPERAND (incr, 0));
15979 tree rhs = RECUR (TREE_OPERAND (incr, 1));
15980 incr = build_x_modify_expr (EXPR_LOCATION (incr), lhs,
15981 NOP_EXPR, rhs, complain);
15983 else
15984 incr = RECUR (incr);
15985 TREE_VEC_ELT (declv, i) = decl;
15986 TREE_VEC_ELT (initv, i) = init;
15987 TREE_VEC_ELT (condv, i) = cond;
15988 TREE_VEC_ELT (incrv, i) = incr;
15989 return;
15992 if (decl_expr)
15994 /* Declare and initialize the variable. */
15995 RECUR (decl_expr);
15996 init = NULL_TREE;
15998 else if (init)
16000 tree *pc;
16001 int j;
16002 for (j = (omp_parallel_combined_clauses == NULL ? 1 : 0); j < 2; j++)
16004 for (pc = j ? clauses : omp_parallel_combined_clauses; *pc; )
16006 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_PRIVATE
16007 && OMP_CLAUSE_DECL (*pc) == decl)
16008 break;
16009 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LASTPRIVATE
16010 && OMP_CLAUSE_DECL (*pc) == decl)
16012 if (j)
16013 break;
16014 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
16015 tree c = *pc;
16016 *pc = OMP_CLAUSE_CHAIN (c);
16017 OMP_CLAUSE_CHAIN (c) = *clauses;
16018 *clauses = c;
16020 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_FIRSTPRIVATE
16021 && OMP_CLAUSE_DECL (*pc) == decl)
16023 error ("iteration variable %qD should not be firstprivate",
16024 decl);
16025 *pc = OMP_CLAUSE_CHAIN (*pc);
16027 else if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_REDUCTION
16028 && OMP_CLAUSE_DECL (*pc) == decl)
16030 error ("iteration variable %qD should not be reduction",
16031 decl);
16032 *pc = OMP_CLAUSE_CHAIN (*pc);
16034 else
16035 pc = &OMP_CLAUSE_CHAIN (*pc);
16037 if (*pc)
16038 break;
16040 if (*pc == NULL_TREE)
16042 tree c = build_omp_clause (input_location, OMP_CLAUSE_PRIVATE);
16043 OMP_CLAUSE_DECL (c) = decl;
16044 c = finish_omp_clauses (c, C_ORT_OMP);
16045 if (c)
16047 OMP_CLAUSE_CHAIN (c) = *clauses;
16048 *clauses = c;
16052 cond = TREE_VEC_ELT (OMP_FOR_COND (t), i);
16053 if (COMPARISON_CLASS_P (cond))
16055 tree op0 = RECUR (TREE_OPERAND (cond, 0));
16056 tree op1 = RECUR (TREE_OPERAND (cond, 1));
16057 cond = build2 (TREE_CODE (cond), boolean_type_node, op0, op1);
16059 else
16060 cond = RECUR (cond);
16061 incr = TREE_VEC_ELT (OMP_FOR_INCR (t), i);
16062 switch (TREE_CODE (incr))
16064 case PREINCREMENT_EXPR:
16065 case PREDECREMENT_EXPR:
16066 case POSTINCREMENT_EXPR:
16067 case POSTDECREMENT_EXPR:
16068 incr = build2 (TREE_CODE (incr), TREE_TYPE (decl),
16069 RECUR (TREE_OPERAND (incr, 0)), NULL_TREE);
16070 break;
16071 case MODIFY_EXPR:
16072 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
16073 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
16075 tree rhs = TREE_OPERAND (incr, 1);
16076 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16077 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
16078 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
16079 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16080 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
16081 rhs0, rhs1));
16083 else
16084 incr = RECUR (incr);
16085 break;
16086 case MODOP_EXPR:
16087 if (TREE_CODE (TREE_OPERAND (incr, 1)) == PLUS_EXPR
16088 || TREE_CODE (TREE_OPERAND (incr, 1)) == MINUS_EXPR)
16090 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16091 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16092 build2 (TREE_CODE (TREE_OPERAND (incr, 1)),
16093 TREE_TYPE (decl), lhs,
16094 RECUR (TREE_OPERAND (incr, 2))));
16096 else if (TREE_CODE (TREE_OPERAND (incr, 1)) == NOP_EXPR
16097 && (TREE_CODE (TREE_OPERAND (incr, 2)) == PLUS_EXPR
16098 || (TREE_CODE (TREE_OPERAND (incr, 2)) == MINUS_EXPR)))
16100 tree rhs = TREE_OPERAND (incr, 2);
16101 tree lhs = RECUR (TREE_OPERAND (incr, 0));
16102 tree rhs0 = RECUR (TREE_OPERAND (rhs, 0));
16103 tree rhs1 = RECUR (TREE_OPERAND (rhs, 1));
16104 incr = build2 (MODIFY_EXPR, TREE_TYPE (decl), lhs,
16105 build2 (TREE_CODE (rhs), TREE_TYPE (decl),
16106 rhs0, rhs1));
16108 else
16109 incr = RECUR (incr);
16110 break;
16111 default:
16112 incr = RECUR (incr);
16113 break;
16116 TREE_VEC_ELT (declv, i) = decl;
16117 TREE_VEC_ELT (initv, i) = init;
16118 TREE_VEC_ELT (condv, i) = cond;
16119 TREE_VEC_ELT (incrv, i) = incr;
16120 #undef RECUR
16123 /* Helper function of tsubst_expr, find OMP_TEAMS inside
16124 of OMP_TARGET's body. */
16126 static tree
16127 tsubst_find_omp_teams (tree *tp, int *walk_subtrees, void *)
16129 *walk_subtrees = 0;
16130 switch (TREE_CODE (*tp))
16132 case OMP_TEAMS:
16133 return *tp;
16134 case BIND_EXPR:
16135 case STATEMENT_LIST:
16136 *walk_subtrees = 1;
16137 break;
16138 default:
16139 break;
16141 return NULL_TREE;
16144 /* Helper function for tsubst_expr. For decomposition declaration
16145 artificial base DECL, which is tsubsted PATTERN_DECL, tsubst
16146 also the corresponding decls representing the identifiers
16147 of the decomposition declaration. Return DECL if successful
16148 or error_mark_node otherwise, set *FIRST to the first decl
16149 in the list chained through DECL_CHAIN and *CNT to the number
16150 of such decls. */
16152 static tree
16153 tsubst_decomp_names (tree decl, tree pattern_decl, tree args,
16154 tsubst_flags_t complain, tree in_decl, tree *first,
16155 unsigned int *cnt)
16157 tree decl2, decl3, prev = decl;
16158 *cnt = 0;
16159 gcc_assert (DECL_NAME (decl) == NULL_TREE);
16160 for (decl2 = DECL_CHAIN (pattern_decl);
16161 decl2
16162 && VAR_P (decl2)
16163 && DECL_DECOMPOSITION_P (decl2)
16164 && DECL_NAME (decl2);
16165 decl2 = DECL_CHAIN (decl2))
16167 if (TREE_TYPE (decl2) == error_mark_node && *cnt == 0)
16169 gcc_assert (errorcount);
16170 return error_mark_node;
16172 (*cnt)++;
16173 gcc_assert (DECL_DECOMP_BASE (decl2) == pattern_decl);
16174 gcc_assert (DECL_HAS_VALUE_EXPR_P (decl2));
16175 tree v = DECL_VALUE_EXPR (decl2);
16176 DECL_HAS_VALUE_EXPR_P (decl2) = 0;
16177 SET_DECL_VALUE_EXPR (decl2, NULL_TREE);
16178 decl3 = tsubst (decl2, args, complain, in_decl);
16179 SET_DECL_VALUE_EXPR (decl2, v);
16180 DECL_HAS_VALUE_EXPR_P (decl2) = 1;
16181 if (VAR_P (decl3))
16182 DECL_TEMPLATE_INSTANTIATED (decl3) = 1;
16183 maybe_push_decl (decl3);
16184 if (error_operand_p (decl3))
16185 decl = error_mark_node;
16186 else if (decl != error_mark_node
16187 && DECL_CHAIN (decl3) != prev)
16189 gcc_assert (errorcount);
16190 decl = error_mark_node;
16192 else
16193 prev = decl3;
16195 *first = prev;
16196 return decl;
16199 /* Like tsubst_copy for expressions, etc. but also does semantic
16200 processing. */
16202 tree
16203 tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
16204 bool integral_constant_expression_p)
16206 #define RETURN(EXP) do { r = (EXP); goto out; } while(0)
16207 #define RECUR(NODE) \
16208 tsubst_expr ((NODE), args, complain, in_decl, \
16209 integral_constant_expression_p)
16211 tree stmt, tmp;
16212 tree r;
16213 location_t loc;
16215 if (t == NULL_TREE || t == error_mark_node)
16216 return t;
16218 loc = input_location;
16219 if (EXPR_HAS_LOCATION (t))
16220 input_location = EXPR_LOCATION (t);
16221 if (STATEMENT_CODE_P (TREE_CODE (t)))
16222 current_stmt_tree ()->stmts_are_full_exprs_p = STMT_IS_FULL_EXPR_P (t);
16224 switch (TREE_CODE (t))
16226 case STATEMENT_LIST:
16228 tree_stmt_iterator i;
16229 for (i = tsi_start (t); !tsi_end_p (i); tsi_next (&i))
16230 RECUR (tsi_stmt (i));
16231 break;
16234 case CTOR_INITIALIZER:
16235 finish_mem_initializers (tsubst_initializer_list
16236 (TREE_OPERAND (t, 0), args));
16237 break;
16239 case RETURN_EXPR:
16240 finish_return_stmt (RECUR (TREE_OPERAND (t, 0)));
16241 break;
16243 case EXPR_STMT:
16244 tmp = RECUR (EXPR_STMT_EXPR (t));
16245 if (EXPR_STMT_STMT_EXPR_RESULT (t))
16246 finish_stmt_expr_expr (tmp, cur_stmt_expr);
16247 else
16248 finish_expr_stmt (tmp);
16249 break;
16251 case USING_STMT:
16252 finish_local_using_directive (USING_STMT_NAMESPACE (t),
16253 /*attribs=*/NULL_TREE);
16254 break;
16256 case DECL_EXPR:
16258 tree decl, pattern_decl;
16259 tree init;
16261 pattern_decl = decl = DECL_EXPR_DECL (t);
16262 if (TREE_CODE (decl) == LABEL_DECL)
16263 finish_label_decl (DECL_NAME (decl));
16264 else if (TREE_CODE (decl) == USING_DECL)
16266 tree scope = USING_DECL_SCOPE (decl);
16267 tree name = DECL_NAME (decl);
16269 scope = tsubst (scope, args, complain, in_decl);
16270 decl = lookup_qualified_name (scope, name,
16271 /*is_type_p=*/false,
16272 /*complain=*/false);
16273 if (decl == error_mark_node || TREE_CODE (decl) == TREE_LIST)
16274 qualified_name_lookup_error (scope, name, decl, input_location);
16275 else
16276 finish_local_using_decl (decl, scope, name);
16278 else if (is_capture_proxy (decl)
16279 && !DECL_TEMPLATE_INSTANTIATION (current_function_decl))
16281 /* We're in tsubst_lambda_expr, we've already inserted a new
16282 capture proxy, so look it up and register it. */
16283 tree inst;
16284 if (DECL_PACK_P (decl))
16286 inst = (retrieve_local_specialization
16287 (DECL_CAPTURED_VARIABLE (decl)));
16288 gcc_assert (TREE_CODE (inst) == NONTYPE_ARGUMENT_PACK);
16290 else
16292 inst = lookup_name_real (DECL_NAME (decl), 0, 0,
16293 /*block_p=*/true, 0, LOOKUP_HIDDEN);
16294 gcc_assert (inst != decl && is_capture_proxy (inst));
16296 register_local_specialization (inst, decl);
16297 break;
16299 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
16300 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
16301 /* Don't copy the old closure; we'll create a new one in
16302 tsubst_lambda_expr. */
16303 break;
16304 else
16306 init = DECL_INITIAL (decl);
16307 decl = tsubst (decl, args, complain, in_decl);
16308 if (decl != error_mark_node)
16310 /* By marking the declaration as instantiated, we avoid
16311 trying to instantiate it. Since instantiate_decl can't
16312 handle local variables, and since we've already done
16313 all that needs to be done, that's the right thing to
16314 do. */
16315 if (VAR_P (decl))
16316 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16317 if (VAR_P (decl)
16318 && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
16319 /* Anonymous aggregates are a special case. */
16320 finish_anon_union (decl);
16321 else if (is_capture_proxy (DECL_EXPR_DECL (t)))
16323 DECL_CONTEXT (decl) = current_function_decl;
16324 if (DECL_NAME (decl) == this_identifier)
16326 tree lam = DECL_CONTEXT (current_function_decl);
16327 lam = CLASSTYPE_LAMBDA_EXPR (lam);
16328 LAMBDA_EXPR_THIS_CAPTURE (lam) = decl;
16330 insert_capture_proxy (decl);
16332 else if (DECL_IMPLICIT_TYPEDEF_P (t))
16333 /* We already did a pushtag. */;
16334 else if (TREE_CODE (decl) == FUNCTION_DECL
16335 && DECL_OMP_DECLARE_REDUCTION_P (decl)
16336 && DECL_FUNCTION_SCOPE_P (pattern_decl))
16338 DECL_CONTEXT (decl) = NULL_TREE;
16339 pushdecl (decl);
16340 DECL_CONTEXT (decl) = current_function_decl;
16341 cp_check_omp_declare_reduction (decl);
16343 else
16345 int const_init = false;
16346 maybe_push_decl (decl);
16347 if (VAR_P (decl)
16348 && DECL_PRETTY_FUNCTION_P (decl))
16350 /* For __PRETTY_FUNCTION__ we have to adjust the
16351 initializer. */
16352 const char *const name
16353 = cxx_printable_name (current_function_decl, 2);
16354 init = cp_fname_init (name, &TREE_TYPE (decl));
16356 else
16357 init = tsubst_init (init, decl, args, complain, in_decl);
16359 if (VAR_P (decl))
16360 const_init = (DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P
16361 (pattern_decl));
16362 if (VAR_P (decl)
16363 && DECL_DECOMPOSITION_P (decl)
16364 && TREE_TYPE (pattern_decl) != error_mark_node)
16366 unsigned int cnt;
16367 tree first;
16368 tree ndecl
16369 = tsubst_decomp_names (decl, pattern_decl, args,
16370 complain, in_decl, &first, &cnt);
16371 if (ndecl != error_mark_node)
16372 cp_maybe_mangle_decomp (ndecl, first, cnt);
16373 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
16374 if (ndecl != error_mark_node)
16375 cp_finish_decomp (ndecl, first, cnt);
16377 else
16378 cp_finish_decl (decl, init, const_init, NULL_TREE, 0);
16383 break;
16386 case FOR_STMT:
16387 stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
16388 RECUR (FOR_INIT_STMT (t));
16389 finish_init_stmt (stmt);
16390 tmp = RECUR (FOR_COND (t));
16391 finish_for_cond (tmp, stmt, false, 0);
16392 tmp = RECUR (FOR_EXPR (t));
16393 finish_for_expr (tmp, stmt);
16395 bool prev = note_iteration_stmt_body_start ();
16396 RECUR (FOR_BODY (t));
16397 note_iteration_stmt_body_end (prev);
16399 finish_for_stmt (stmt);
16400 break;
16402 case RANGE_FOR_STMT:
16404 /* Construct another range_for, if this is not a final
16405 substitution (for inside inside a generic lambda of a
16406 template). Otherwise convert to a regular for. */
16407 tree decl, expr;
16408 stmt = (processing_template_decl
16409 ? begin_range_for_stmt (NULL_TREE, NULL_TREE)
16410 : begin_for_stmt (NULL_TREE, NULL_TREE));
16411 decl = RANGE_FOR_DECL (t);
16412 decl = tsubst (decl, args, complain, in_decl);
16413 maybe_push_decl (decl);
16414 expr = RECUR (RANGE_FOR_EXPR (t));
16416 tree decomp_first = NULL_TREE;
16417 unsigned decomp_cnt = 0;
16418 if (VAR_P (decl) && DECL_DECOMPOSITION_P (decl))
16419 decl = tsubst_decomp_names (decl, RANGE_FOR_DECL (t), args,
16420 complain, in_decl,
16421 &decomp_first, &decomp_cnt);
16423 if (processing_template_decl)
16425 RANGE_FOR_IVDEP (stmt) = RANGE_FOR_IVDEP (t);
16426 RANGE_FOR_UNROLL (stmt) = RANGE_FOR_UNROLL (t);
16427 finish_range_for_decl (stmt, decl, expr);
16429 else
16431 unsigned short unroll = (RANGE_FOR_UNROLL (t)
16432 ? tree_to_uhwi (RANGE_FOR_UNROLL (t)) : 0);
16433 stmt = cp_convert_range_for (stmt, decl, expr,
16434 decomp_first, decomp_cnt,
16435 RANGE_FOR_IVDEP (t), unroll);
16438 bool prev = note_iteration_stmt_body_start ();
16439 RECUR (RANGE_FOR_BODY (t));
16440 note_iteration_stmt_body_end (prev);
16441 finish_for_stmt (stmt);
16443 break;
16445 case WHILE_STMT:
16446 stmt = begin_while_stmt ();
16447 tmp = RECUR (WHILE_COND (t));
16448 finish_while_stmt_cond (tmp, stmt, false, 0);
16450 bool prev = note_iteration_stmt_body_start ();
16451 RECUR (WHILE_BODY (t));
16452 note_iteration_stmt_body_end (prev);
16454 finish_while_stmt (stmt);
16455 break;
16457 case DO_STMT:
16458 stmt = begin_do_stmt ();
16460 bool prev = note_iteration_stmt_body_start ();
16461 RECUR (DO_BODY (t));
16462 note_iteration_stmt_body_end (prev);
16464 finish_do_body (stmt);
16465 tmp = RECUR (DO_COND (t));
16466 finish_do_stmt (tmp, stmt, false, 0);
16467 break;
16469 case IF_STMT:
16470 stmt = begin_if_stmt ();
16471 IF_STMT_CONSTEXPR_P (stmt) = IF_STMT_CONSTEXPR_P (t);
16472 tmp = RECUR (IF_COND (t));
16473 tmp = finish_if_stmt_cond (tmp, stmt);
16474 if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
16475 /* Don't instantiate the THEN_CLAUSE. */;
16476 else
16478 bool inhibit = integer_zerop (fold_non_dependent_expr (tmp));
16479 if (inhibit)
16480 ++c_inhibit_evaluation_warnings;
16481 RECUR (THEN_CLAUSE (t));
16482 if (inhibit)
16483 --c_inhibit_evaluation_warnings;
16485 finish_then_clause (stmt);
16487 if (IF_STMT_CONSTEXPR_P (t) && integer_nonzerop (tmp))
16488 /* Don't instantiate the ELSE_CLAUSE. */;
16489 else if (ELSE_CLAUSE (t))
16491 bool inhibit = integer_nonzerop (fold_non_dependent_expr (tmp));
16492 begin_else_clause (stmt);
16493 if (inhibit)
16494 ++c_inhibit_evaluation_warnings;
16495 RECUR (ELSE_CLAUSE (t));
16496 if (inhibit)
16497 --c_inhibit_evaluation_warnings;
16498 finish_else_clause (stmt);
16501 finish_if_stmt (stmt);
16502 break;
16504 case BIND_EXPR:
16505 if (BIND_EXPR_BODY_BLOCK (t))
16506 stmt = begin_function_body ();
16507 else
16508 stmt = begin_compound_stmt (BIND_EXPR_TRY_BLOCK (t)
16509 ? BCS_TRY_BLOCK : 0);
16511 RECUR (BIND_EXPR_BODY (t));
16513 if (BIND_EXPR_BODY_BLOCK (t))
16514 finish_function_body (stmt);
16515 else
16516 finish_compound_stmt (stmt);
16517 break;
16519 case BREAK_STMT:
16520 finish_break_stmt ();
16521 break;
16523 case CONTINUE_STMT:
16524 finish_continue_stmt ();
16525 break;
16527 case SWITCH_STMT:
16528 stmt = begin_switch_stmt ();
16529 tmp = RECUR (SWITCH_STMT_COND (t));
16530 finish_switch_cond (tmp, stmt);
16531 RECUR (SWITCH_STMT_BODY (t));
16532 finish_switch_stmt (stmt);
16533 break;
16535 case CASE_LABEL_EXPR:
16537 tree low = RECUR (CASE_LOW (t));
16538 tree high = RECUR (CASE_HIGH (t));
16539 tree l = finish_case_label (EXPR_LOCATION (t), low, high);
16540 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
16541 FALLTHROUGH_LABEL_P (CASE_LABEL (l))
16542 = FALLTHROUGH_LABEL_P (CASE_LABEL (t));
16544 break;
16546 case LABEL_EXPR:
16548 tree decl = LABEL_EXPR_LABEL (t);
16549 tree label;
16551 label = finish_label_stmt (DECL_NAME (decl));
16552 if (TREE_CODE (label) == LABEL_DECL)
16553 FALLTHROUGH_LABEL_P (label) = FALLTHROUGH_LABEL_P (decl);
16554 if (DECL_ATTRIBUTES (decl) != NULL_TREE)
16555 cplus_decl_attributes (&label, DECL_ATTRIBUTES (decl), 0);
16557 break;
16559 case GOTO_EXPR:
16560 tmp = GOTO_DESTINATION (t);
16561 if (TREE_CODE (tmp) != LABEL_DECL)
16562 /* Computed goto's must be tsubst'd into. On the other hand,
16563 non-computed gotos must not be; the identifier in question
16564 will have no binding. */
16565 tmp = RECUR (tmp);
16566 else
16567 tmp = DECL_NAME (tmp);
16568 finish_goto_stmt (tmp);
16569 break;
16571 case ASM_EXPR:
16573 tree string = RECUR (ASM_STRING (t));
16574 tree outputs = tsubst_copy_asm_operands (ASM_OUTPUTS (t), args,
16575 complain, in_decl);
16576 tree inputs = tsubst_copy_asm_operands (ASM_INPUTS (t), args,
16577 complain, in_decl);
16578 tree clobbers = tsubst_copy_asm_operands (ASM_CLOBBERS (t), args,
16579 complain, in_decl);
16580 tree labels = tsubst_copy_asm_operands (ASM_LABELS (t), args,
16581 complain, in_decl);
16582 tmp = finish_asm_stmt (ASM_VOLATILE_P (t), string, outputs, inputs,
16583 clobbers, labels);
16584 tree asm_expr = tmp;
16585 if (TREE_CODE (asm_expr) == CLEANUP_POINT_EXPR)
16586 asm_expr = TREE_OPERAND (asm_expr, 0);
16587 ASM_INPUT_P (asm_expr) = ASM_INPUT_P (t);
16589 break;
16591 case TRY_BLOCK:
16592 if (CLEANUP_P (t))
16594 stmt = begin_try_block ();
16595 RECUR (TRY_STMTS (t));
16596 finish_cleanup_try_block (stmt);
16597 finish_cleanup (RECUR (TRY_HANDLERS (t)), stmt);
16599 else
16601 tree compound_stmt = NULL_TREE;
16603 if (FN_TRY_BLOCK_P (t))
16604 stmt = begin_function_try_block (&compound_stmt);
16605 else
16606 stmt = begin_try_block ();
16608 RECUR (TRY_STMTS (t));
16610 if (FN_TRY_BLOCK_P (t))
16611 finish_function_try_block (stmt);
16612 else
16613 finish_try_block (stmt);
16615 RECUR (TRY_HANDLERS (t));
16616 if (FN_TRY_BLOCK_P (t))
16617 finish_function_handler_sequence (stmt, compound_stmt);
16618 else
16619 finish_handler_sequence (stmt);
16621 break;
16623 case HANDLER:
16625 tree decl = HANDLER_PARMS (t);
16627 if (decl)
16629 decl = tsubst (decl, args, complain, in_decl);
16630 /* Prevent instantiate_decl from trying to instantiate
16631 this variable. We've already done all that needs to be
16632 done. */
16633 if (decl != error_mark_node)
16634 DECL_TEMPLATE_INSTANTIATED (decl) = 1;
16636 stmt = begin_handler ();
16637 finish_handler_parms (decl, stmt);
16638 RECUR (HANDLER_BODY (t));
16639 finish_handler (stmt);
16641 break;
16643 case TAG_DEFN:
16644 tmp = tsubst (TREE_TYPE (t), args, complain, NULL_TREE);
16645 if (CLASS_TYPE_P (tmp))
16647 /* Local classes are not independent templates; they are
16648 instantiated along with their containing function. And this
16649 way we don't have to deal with pushing out of one local class
16650 to instantiate a member of another local class. */
16651 /* Closures are handled by the LAMBDA_EXPR. */
16652 gcc_assert (!LAMBDA_TYPE_P (TREE_TYPE (t)));
16653 complete_type (tmp);
16654 for (tree fld = TYPE_FIELDS (tmp); fld; fld = DECL_CHAIN (fld))
16655 if ((VAR_P (fld)
16656 || (TREE_CODE (fld) == FUNCTION_DECL
16657 && !DECL_ARTIFICIAL (fld)))
16658 && DECL_TEMPLATE_INSTANTIATION (fld))
16659 instantiate_decl (fld, /*defer_ok=*/false,
16660 /*expl_inst_class=*/false);
16662 break;
16664 case STATIC_ASSERT:
16666 tree condition;
16668 ++c_inhibit_evaluation_warnings;
16669 condition =
16670 tsubst_expr (STATIC_ASSERT_CONDITION (t),
16671 args,
16672 complain, in_decl,
16673 /*integral_constant_expression_p=*/true);
16674 --c_inhibit_evaluation_warnings;
16676 finish_static_assert (condition,
16677 STATIC_ASSERT_MESSAGE (t),
16678 STATIC_ASSERT_SOURCE_LOCATION (t),
16679 /*member_p=*/false);
16681 break;
16683 case OACC_KERNELS:
16684 case OACC_PARALLEL:
16685 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_ACC, args, complain,
16686 in_decl);
16687 stmt = begin_omp_parallel ();
16688 RECUR (OMP_BODY (t));
16689 finish_omp_construct (TREE_CODE (t), stmt, tmp);
16690 break;
16692 case OMP_PARALLEL:
16693 r = push_omp_privatization_clauses (OMP_PARALLEL_COMBINED (t));
16694 tmp = tsubst_omp_clauses (OMP_PARALLEL_CLAUSES (t), C_ORT_OMP, args,
16695 complain, in_decl);
16696 if (OMP_PARALLEL_COMBINED (t))
16697 omp_parallel_combined_clauses = &tmp;
16698 stmt = begin_omp_parallel ();
16699 RECUR (OMP_PARALLEL_BODY (t));
16700 gcc_assert (omp_parallel_combined_clauses == NULL);
16701 OMP_PARALLEL_COMBINED (finish_omp_parallel (tmp, stmt))
16702 = OMP_PARALLEL_COMBINED (t);
16703 pop_omp_privatization_clauses (r);
16704 break;
16706 case OMP_TASK:
16707 r = push_omp_privatization_clauses (false);
16708 tmp = tsubst_omp_clauses (OMP_TASK_CLAUSES (t), C_ORT_OMP, args,
16709 complain, in_decl);
16710 stmt = begin_omp_task ();
16711 RECUR (OMP_TASK_BODY (t));
16712 finish_omp_task (tmp, stmt);
16713 pop_omp_privatization_clauses (r);
16714 break;
16716 case OMP_FOR:
16717 case OMP_SIMD:
16718 case OMP_DISTRIBUTE:
16719 case OMP_TASKLOOP:
16720 case OACC_LOOP:
16722 tree clauses, body, pre_body;
16723 tree declv = NULL_TREE, initv = NULL_TREE, condv = NULL_TREE;
16724 tree orig_declv = NULL_TREE;
16725 tree incrv = NULL_TREE;
16726 enum c_omp_region_type ort = C_ORT_OMP;
16727 int i;
16729 if (TREE_CODE (t) == OACC_LOOP)
16730 ort = C_ORT_ACC;
16732 r = push_omp_privatization_clauses (OMP_FOR_INIT (t) == NULL_TREE);
16733 clauses = tsubst_omp_clauses (OMP_FOR_CLAUSES (t), ort, args, complain,
16734 in_decl);
16735 if (OMP_FOR_INIT (t) != NULL_TREE)
16737 declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16738 if (OMP_FOR_ORIG_DECLS (t))
16739 orig_declv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16740 initv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16741 condv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16742 incrv = make_tree_vec (TREE_VEC_LENGTH (OMP_FOR_INIT (t)));
16745 stmt = begin_omp_structured_block ();
16747 pre_body = push_stmt_list ();
16748 RECUR (OMP_FOR_PRE_BODY (t));
16749 pre_body = pop_stmt_list (pre_body);
16751 if (OMP_FOR_INIT (t) != NULL_TREE)
16752 for (i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (t)); i++)
16753 tsubst_omp_for_iterator (t, i, declv, orig_declv, initv, condv,
16754 incrv, &clauses, args, complain, in_decl,
16755 integral_constant_expression_p);
16756 omp_parallel_combined_clauses = NULL;
16758 body = push_stmt_list ();
16759 RECUR (OMP_FOR_BODY (t));
16760 body = pop_stmt_list (body);
16762 if (OMP_FOR_INIT (t) != NULL_TREE)
16763 t = finish_omp_for (EXPR_LOCATION (t), TREE_CODE (t), declv,
16764 orig_declv, initv, condv, incrv, body, pre_body,
16765 NULL, clauses);
16766 else
16768 t = make_node (TREE_CODE (t));
16769 TREE_TYPE (t) = void_type_node;
16770 OMP_FOR_BODY (t) = body;
16771 OMP_FOR_PRE_BODY (t) = pre_body;
16772 OMP_FOR_CLAUSES (t) = clauses;
16773 SET_EXPR_LOCATION (t, EXPR_LOCATION (t));
16774 add_stmt (t);
16777 add_stmt (finish_omp_structured_block (stmt));
16778 pop_omp_privatization_clauses (r);
16780 break;
16782 case OMP_SECTIONS:
16783 omp_parallel_combined_clauses = NULL;
16784 /* FALLTHRU */
16785 case OMP_SINGLE:
16786 case OMP_TEAMS:
16787 case OMP_CRITICAL:
16788 r = push_omp_privatization_clauses (TREE_CODE (t) == OMP_TEAMS
16789 && OMP_TEAMS_COMBINED (t));
16790 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), C_ORT_OMP, args, complain,
16791 in_decl);
16792 stmt = push_stmt_list ();
16793 RECUR (OMP_BODY (t));
16794 stmt = pop_stmt_list (stmt);
16796 t = copy_node (t);
16797 OMP_BODY (t) = stmt;
16798 OMP_CLAUSES (t) = tmp;
16799 add_stmt (t);
16800 pop_omp_privatization_clauses (r);
16801 break;
16803 case OACC_DATA:
16804 case OMP_TARGET_DATA:
16805 case OMP_TARGET:
16806 tmp = tsubst_omp_clauses (OMP_CLAUSES (t), (TREE_CODE (t) == OACC_DATA)
16807 ? C_ORT_ACC : C_ORT_OMP, args, complain,
16808 in_decl);
16809 keep_next_level (true);
16810 stmt = begin_omp_structured_block ();
16812 RECUR (OMP_BODY (t));
16813 stmt = finish_omp_structured_block (stmt);
16815 t = copy_node (t);
16816 OMP_BODY (t) = stmt;
16817 OMP_CLAUSES (t) = tmp;
16818 if (TREE_CODE (t) == OMP_TARGET && OMP_TARGET_COMBINED (t))
16820 tree teams = cp_walk_tree (&stmt, tsubst_find_omp_teams, NULL, NULL);
16821 if (teams)
16823 /* For combined target teams, ensure the num_teams and
16824 thread_limit clause expressions are evaluated on the host,
16825 before entering the target construct. */
16826 tree c;
16827 for (c = OMP_TEAMS_CLAUSES (teams);
16828 c; c = OMP_CLAUSE_CHAIN (c))
16829 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
16830 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
16831 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
16833 tree expr = OMP_CLAUSE_OPERAND (c, 0);
16834 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
16835 if (expr == error_mark_node)
16836 continue;
16837 tmp = TARGET_EXPR_SLOT (expr);
16838 add_stmt (expr);
16839 OMP_CLAUSE_OPERAND (c, 0) = expr;
16840 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
16841 OMP_CLAUSE_FIRSTPRIVATE);
16842 OMP_CLAUSE_DECL (tc) = tmp;
16843 OMP_CLAUSE_CHAIN (tc) = OMP_TARGET_CLAUSES (t);
16844 OMP_TARGET_CLAUSES (t) = tc;
16848 add_stmt (t);
16849 break;
16851 case OACC_DECLARE:
16852 t = copy_node (t);
16853 tmp = tsubst_omp_clauses (OACC_DECLARE_CLAUSES (t), C_ORT_ACC, args,
16854 complain, in_decl);
16855 OACC_DECLARE_CLAUSES (t) = tmp;
16856 add_stmt (t);
16857 break;
16859 case OMP_TARGET_UPDATE:
16860 case OMP_TARGET_ENTER_DATA:
16861 case OMP_TARGET_EXIT_DATA:
16862 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_OMP, args,
16863 complain, in_decl);
16864 t = copy_node (t);
16865 OMP_STANDALONE_CLAUSES (t) = tmp;
16866 add_stmt (t);
16867 break;
16869 case OACC_ENTER_DATA:
16870 case OACC_EXIT_DATA:
16871 case OACC_UPDATE:
16872 tmp = tsubst_omp_clauses (OMP_STANDALONE_CLAUSES (t), C_ORT_ACC, args,
16873 complain, in_decl);
16874 t = copy_node (t);
16875 OMP_STANDALONE_CLAUSES (t) = tmp;
16876 add_stmt (t);
16877 break;
16879 case OMP_ORDERED:
16880 tmp = tsubst_omp_clauses (OMP_ORDERED_CLAUSES (t), C_ORT_OMP, args,
16881 complain, in_decl);
16882 stmt = push_stmt_list ();
16883 RECUR (OMP_BODY (t));
16884 stmt = pop_stmt_list (stmt);
16886 t = copy_node (t);
16887 OMP_BODY (t) = stmt;
16888 OMP_ORDERED_CLAUSES (t) = tmp;
16889 add_stmt (t);
16890 break;
16892 case OMP_SECTION:
16893 case OMP_MASTER:
16894 case OMP_TASKGROUP:
16895 stmt = push_stmt_list ();
16896 RECUR (OMP_BODY (t));
16897 stmt = pop_stmt_list (stmt);
16899 t = copy_node (t);
16900 OMP_BODY (t) = stmt;
16901 add_stmt (t);
16902 break;
16904 case OMP_ATOMIC:
16905 gcc_assert (OMP_ATOMIC_DEPENDENT_P (t));
16906 if (TREE_CODE (TREE_OPERAND (t, 1)) != MODIFY_EXPR)
16908 tree op1 = TREE_OPERAND (t, 1);
16909 tree rhs1 = NULL_TREE;
16910 tree lhs, rhs;
16911 if (TREE_CODE (op1) == COMPOUND_EXPR)
16913 rhs1 = RECUR (TREE_OPERAND (op1, 0));
16914 op1 = TREE_OPERAND (op1, 1);
16916 lhs = RECUR (TREE_OPERAND (op1, 0));
16917 rhs = RECUR (TREE_OPERAND (op1, 1));
16918 finish_omp_atomic (OMP_ATOMIC, TREE_CODE (op1), lhs, rhs,
16919 NULL_TREE, NULL_TREE, rhs1,
16920 OMP_ATOMIC_SEQ_CST (t));
16922 else
16924 tree op1 = TREE_OPERAND (t, 1);
16925 tree v = NULL_TREE, lhs, rhs = NULL_TREE, lhs1 = NULL_TREE;
16926 tree rhs1 = NULL_TREE;
16927 enum tree_code code = TREE_CODE (TREE_OPERAND (op1, 1));
16928 enum tree_code opcode = NOP_EXPR;
16929 if (code == OMP_ATOMIC_READ)
16931 v = RECUR (TREE_OPERAND (op1, 0));
16932 lhs = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16934 else if (code == OMP_ATOMIC_CAPTURE_OLD
16935 || code == OMP_ATOMIC_CAPTURE_NEW)
16937 tree op11 = TREE_OPERAND (TREE_OPERAND (op1, 1), 1);
16938 v = RECUR (TREE_OPERAND (op1, 0));
16939 lhs1 = RECUR (TREE_OPERAND (TREE_OPERAND (op1, 1), 0));
16940 if (TREE_CODE (op11) == COMPOUND_EXPR)
16942 rhs1 = RECUR (TREE_OPERAND (op11, 0));
16943 op11 = TREE_OPERAND (op11, 1);
16945 lhs = RECUR (TREE_OPERAND (op11, 0));
16946 rhs = RECUR (TREE_OPERAND (op11, 1));
16947 opcode = TREE_CODE (op11);
16948 if (opcode == MODIFY_EXPR)
16949 opcode = NOP_EXPR;
16951 else
16953 code = OMP_ATOMIC;
16954 lhs = RECUR (TREE_OPERAND (op1, 0));
16955 rhs = RECUR (TREE_OPERAND (op1, 1));
16957 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1,
16958 OMP_ATOMIC_SEQ_CST (t));
16960 break;
16962 case TRANSACTION_EXPR:
16964 int flags = 0;
16965 flags |= (TRANSACTION_EXPR_OUTER (t) ? TM_STMT_ATTR_OUTER : 0);
16966 flags |= (TRANSACTION_EXPR_RELAXED (t) ? TM_STMT_ATTR_RELAXED : 0);
16968 if (TRANSACTION_EXPR_IS_STMT (t))
16970 tree body = TRANSACTION_EXPR_BODY (t);
16971 tree noex = NULL_TREE;
16972 if (TREE_CODE (body) == MUST_NOT_THROW_EXPR)
16974 noex = MUST_NOT_THROW_COND (body);
16975 if (noex == NULL_TREE)
16976 noex = boolean_true_node;
16977 body = TREE_OPERAND (body, 0);
16979 stmt = begin_transaction_stmt (input_location, NULL, flags);
16980 RECUR (body);
16981 finish_transaction_stmt (stmt, NULL, flags, RECUR (noex));
16983 else
16985 stmt = build_transaction_expr (EXPR_LOCATION (t),
16986 RECUR (TRANSACTION_EXPR_BODY (t)),
16987 flags, NULL_TREE);
16988 RETURN (stmt);
16991 break;
16993 case MUST_NOT_THROW_EXPR:
16995 tree op0 = RECUR (TREE_OPERAND (t, 0));
16996 tree cond = RECUR (MUST_NOT_THROW_COND (t));
16997 RETURN (build_must_not_throw_expr (op0, cond));
17000 case EXPR_PACK_EXPANSION:
17001 error ("invalid use of pack expansion expression");
17002 RETURN (error_mark_node);
17004 case NONTYPE_ARGUMENT_PACK:
17005 error ("use %<...%> to expand argument pack");
17006 RETURN (error_mark_node);
17008 case COMPOUND_EXPR:
17009 tmp = RECUR (TREE_OPERAND (t, 0));
17010 if (tmp == NULL_TREE)
17011 /* If the first operand was a statement, we're done with it. */
17012 RETURN (RECUR (TREE_OPERAND (t, 1)));
17013 RETURN (build_x_compound_expr (EXPR_LOCATION (t), tmp,
17014 RECUR (TREE_OPERAND (t, 1)),
17015 complain));
17017 case ANNOTATE_EXPR:
17018 tmp = RECUR (TREE_OPERAND (t, 0));
17019 RETURN (build3_loc (EXPR_LOCATION (t), ANNOTATE_EXPR,
17020 TREE_TYPE (tmp), tmp,
17021 RECUR (TREE_OPERAND (t, 1)),
17022 RECUR (TREE_OPERAND (t, 2))));
17024 default:
17025 gcc_assert (!STATEMENT_CODE_P (TREE_CODE (t)));
17027 RETURN (tsubst_copy_and_build (t, args, complain, in_decl,
17028 /*function_p=*/false,
17029 integral_constant_expression_p));
17032 RETURN (NULL_TREE);
17033 out:
17034 input_location = loc;
17035 return r;
17036 #undef RECUR
17037 #undef RETURN
17040 /* Instantiate the special body of the artificial DECL_OMP_DECLARE_REDUCTION
17041 function. For description of the body see comment above
17042 cp_parser_omp_declare_reduction_exprs. */
17044 static void
17045 tsubst_omp_udr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
17047 if (t == NULL_TREE || t == error_mark_node)
17048 return;
17050 gcc_assert (TREE_CODE (t) == STATEMENT_LIST);
17052 tree_stmt_iterator tsi;
17053 int i;
17054 tree stmts[7];
17055 memset (stmts, 0, sizeof stmts);
17056 for (i = 0, tsi = tsi_start (t);
17057 i < 7 && !tsi_end_p (tsi);
17058 i++, tsi_next (&tsi))
17059 stmts[i] = tsi_stmt (tsi);
17060 gcc_assert (tsi_end_p (tsi));
17062 if (i >= 3)
17064 gcc_assert (TREE_CODE (stmts[0]) == DECL_EXPR
17065 && TREE_CODE (stmts[1]) == DECL_EXPR);
17066 tree omp_out = tsubst (DECL_EXPR_DECL (stmts[0]),
17067 args, complain, in_decl);
17068 tree omp_in = tsubst (DECL_EXPR_DECL (stmts[1]),
17069 args, complain, in_decl);
17070 DECL_CONTEXT (omp_out) = current_function_decl;
17071 DECL_CONTEXT (omp_in) = current_function_decl;
17072 keep_next_level (true);
17073 tree block = begin_omp_structured_block ();
17074 tsubst_expr (stmts[2], args, complain, in_decl, false);
17075 block = finish_omp_structured_block (block);
17076 block = maybe_cleanup_point_expr_void (block);
17077 add_decl_expr (omp_out);
17078 if (TREE_NO_WARNING (DECL_EXPR_DECL (stmts[0])))
17079 TREE_NO_WARNING (omp_out) = 1;
17080 add_decl_expr (omp_in);
17081 finish_expr_stmt (block);
17083 if (i >= 6)
17085 gcc_assert (TREE_CODE (stmts[3]) == DECL_EXPR
17086 && TREE_CODE (stmts[4]) == DECL_EXPR);
17087 tree omp_priv = tsubst (DECL_EXPR_DECL (stmts[3]),
17088 args, complain, in_decl);
17089 tree omp_orig = tsubst (DECL_EXPR_DECL (stmts[4]),
17090 args, complain, in_decl);
17091 DECL_CONTEXT (omp_priv) = current_function_decl;
17092 DECL_CONTEXT (omp_orig) = current_function_decl;
17093 keep_next_level (true);
17094 tree block = begin_omp_structured_block ();
17095 tsubst_expr (stmts[5], args, complain, in_decl, false);
17096 block = finish_omp_structured_block (block);
17097 block = maybe_cleanup_point_expr_void (block);
17098 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
17099 add_decl_expr (omp_priv);
17100 add_decl_expr (omp_orig);
17101 finish_expr_stmt (block);
17102 if (i == 7)
17103 add_decl_expr (omp_orig);
17107 /* T is a postfix-expression that is not being used in a function
17108 call. Return the substituted version of T. */
17110 static tree
17111 tsubst_non_call_postfix_expression (tree t, tree args,
17112 tsubst_flags_t complain,
17113 tree in_decl)
17115 if (TREE_CODE (t) == SCOPE_REF)
17116 t = tsubst_qualified_id (t, args, complain, in_decl,
17117 /*done=*/false, /*address_p=*/false);
17118 else
17119 t = tsubst_copy_and_build (t, args, complain, in_decl,
17120 /*function_p=*/false,
17121 /*integral_constant_expression_p=*/false);
17123 return t;
17126 /* T is a LAMBDA_EXPR. Generate a new LAMBDA_EXPR for the current
17127 instantiation context. Instantiating a pack expansion containing a lambda
17128 might result in multiple lambdas all based on the same lambda in the
17129 template. */
17131 tree
17132 tsubst_lambda_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
17134 tree oldfn = lambda_function (t);
17135 in_decl = oldfn;
17137 tree r = build_lambda_expr ();
17139 LAMBDA_EXPR_LOCATION (r)
17140 = LAMBDA_EXPR_LOCATION (t);
17141 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (r)
17142 = LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (t);
17143 LAMBDA_EXPR_MUTABLE_P (r) = LAMBDA_EXPR_MUTABLE_P (t);
17145 if (LAMBDA_EXPR_EXTRA_SCOPE (t) == NULL_TREE)
17146 LAMBDA_EXPR_EXTRA_SCOPE (r) = NULL_TREE;
17147 else
17148 record_lambda_scope (r);
17150 gcc_assert (LAMBDA_EXPR_THIS_CAPTURE (t) == NULL_TREE
17151 && LAMBDA_EXPR_PENDING_PROXIES (t) == NULL);
17153 for (tree cap = LAMBDA_EXPR_CAPTURE_LIST (t); cap;
17154 cap = TREE_CHAIN (cap))
17156 tree field = TREE_PURPOSE (cap);
17157 if (PACK_EXPANSION_P (field))
17158 field = PACK_EXPANSION_PATTERN (field);
17159 field = tsubst_decl (field, args, complain);
17161 if (field == error_mark_node)
17162 return error_mark_node;
17164 tree init = TREE_VALUE (cap);
17165 if (PACK_EXPANSION_P (init))
17166 init = tsubst_pack_expansion (init, args, complain, in_decl);
17167 else
17168 init = tsubst_copy_and_build (init, args, complain, in_decl,
17169 /*fn*/false, /*constexpr*/false);
17171 if (TREE_CODE (field) == TREE_VEC)
17173 int len = TREE_VEC_LENGTH (field);
17174 gcc_assert (TREE_CODE (init) == TREE_VEC
17175 && TREE_VEC_LENGTH (init) == len);
17176 for (int i = 0; i < len; ++i)
17177 LAMBDA_EXPR_CAPTURE_LIST (r)
17178 = tree_cons (TREE_VEC_ELT (field, i),
17179 TREE_VEC_ELT (init, i),
17180 LAMBDA_EXPR_CAPTURE_LIST (r));
17182 else
17184 LAMBDA_EXPR_CAPTURE_LIST (r)
17185 = tree_cons (field, init, LAMBDA_EXPR_CAPTURE_LIST (r));
17187 if (id_equal (DECL_NAME (field), "__this"))
17188 LAMBDA_EXPR_THIS_CAPTURE (r) = field;
17192 tree type = begin_lambda_type (r);
17194 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
17195 determine_visibility (TYPE_NAME (type));
17197 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (r));
17199 tree oldtmpl = (generic_lambda_fn_p (oldfn)
17200 ? DECL_TI_TEMPLATE (oldfn)
17201 : NULL_TREE);
17203 tree fntype = static_fn_type (oldfn);
17204 if (oldtmpl)
17205 ++processing_template_decl;
17206 fntype = tsubst (fntype, args, complain, in_decl);
17207 if (oldtmpl)
17208 --processing_template_decl;
17210 if (fntype == error_mark_node)
17211 r = error_mark_node;
17212 else
17214 /* Fix the type of 'this'. */
17215 fntype = build_memfn_type (fntype, type,
17216 type_memfn_quals (fntype),
17217 type_memfn_rqual (fntype));
17218 tree fn, tmpl;
17219 if (oldtmpl)
17221 tmpl = tsubst_template_decl (oldtmpl, args, complain, fntype);
17222 fn = DECL_TEMPLATE_RESULT (tmpl);
17223 finish_member_declaration (tmpl);
17225 else
17227 tmpl = NULL_TREE;
17228 fn = tsubst_function_decl (oldfn, args, complain, fntype);
17229 finish_member_declaration (fn);
17232 /* Let finish_function set this. */
17233 DECL_DECLARED_CONSTEXPR_P (fn) = false;
17235 bool nested = cfun;
17236 if (nested)
17237 push_function_context ();
17238 else
17239 /* Still increment function_depth so that we don't GC in the
17240 middle of an expression. */
17241 ++function_depth;
17243 local_specialization_stack s (lss_copy);
17245 tree body = start_lambda_function (fn, r);
17247 register_parameter_specializations (oldfn, fn);
17249 tsubst_expr (DECL_SAVED_TREE (oldfn), args, complain, r,
17250 /*constexpr*/false);
17252 finish_lambda_function (body);
17254 if (nested)
17255 pop_function_context ();
17256 else
17257 --function_depth;
17259 /* The capture list was built up in reverse order; fix that now. */
17260 LAMBDA_EXPR_CAPTURE_LIST (r)
17261 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (r));
17263 LAMBDA_EXPR_THIS_CAPTURE (r) = NULL_TREE;
17265 maybe_add_lambda_conv_op (type);
17268 finish_struct (type, /*attr*/NULL_TREE);
17270 insert_pending_capture_proxies ();
17272 return r;
17275 /* Like tsubst but deals with expressions and performs semantic
17276 analysis. FUNCTION_P is true if T is the "F" in "F (ARGS)". */
17278 tree
17279 tsubst_copy_and_build (tree t,
17280 tree args,
17281 tsubst_flags_t complain,
17282 tree in_decl,
17283 bool function_p,
17284 bool integral_constant_expression_p)
17286 #define RETURN(EXP) do { retval = (EXP); goto out; } while(0)
17287 #define RECUR(NODE) \
17288 tsubst_copy_and_build (NODE, args, complain, in_decl, \
17289 /*function_p=*/false, \
17290 integral_constant_expression_p)
17292 tree retval, op1;
17293 location_t loc;
17295 if (t == NULL_TREE || t == error_mark_node)
17296 return t;
17298 loc = input_location;
17299 if (EXPR_HAS_LOCATION (t))
17300 input_location = EXPR_LOCATION (t);
17302 /* N3276 decltype magic only applies to calls at the top level or on the
17303 right side of a comma. */
17304 tsubst_flags_t decltype_flag = (complain & tf_decltype);
17305 complain &= ~tf_decltype;
17307 switch (TREE_CODE (t))
17309 case USING_DECL:
17310 t = DECL_NAME (t);
17311 /* Fall through. */
17312 case IDENTIFIER_NODE:
17314 tree decl;
17315 cp_id_kind idk;
17316 bool non_integral_constant_expression_p;
17317 const char *error_msg;
17319 if (IDENTIFIER_CONV_OP_P (t))
17321 tree new_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17322 t = make_conv_op_name (new_type);
17325 /* Look up the name. */
17326 decl = lookup_name (t);
17328 /* By convention, expressions use ERROR_MARK_NODE to indicate
17329 failure, not NULL_TREE. */
17330 if (decl == NULL_TREE)
17331 decl = error_mark_node;
17333 decl = finish_id_expression (t, decl, NULL_TREE,
17334 &idk,
17335 integral_constant_expression_p,
17336 /*allow_non_integral_constant_expression_p=*/(cxx_dialect >= cxx11),
17337 &non_integral_constant_expression_p,
17338 /*template_p=*/false,
17339 /*done=*/true,
17340 /*address_p=*/false,
17341 /*template_arg_p=*/false,
17342 &error_msg,
17343 input_location);
17344 if (error_msg)
17345 error (error_msg);
17346 if (!function_p && identifier_p (decl))
17348 if (complain & tf_error)
17349 unqualified_name_lookup_error (decl);
17350 decl = error_mark_node;
17352 RETURN (decl);
17355 case TEMPLATE_ID_EXPR:
17357 tree object;
17358 tree templ = RECUR (TREE_OPERAND (t, 0));
17359 tree targs = TREE_OPERAND (t, 1);
17361 if (targs)
17362 targs = tsubst_template_args (targs, args, complain, in_decl);
17363 if (targs == error_mark_node)
17364 RETURN (error_mark_node);
17366 if (TREE_CODE (templ) == SCOPE_REF)
17368 tree name = TREE_OPERAND (templ, 1);
17369 tree tid = lookup_template_function (name, targs);
17370 TREE_OPERAND (templ, 1) = tid;
17371 RETURN (templ);
17374 if (variable_template_p (templ))
17375 RETURN (lookup_and_finish_template_variable (templ, targs, complain));
17377 if (TREE_CODE (templ) == COMPONENT_REF)
17379 object = TREE_OPERAND (templ, 0);
17380 templ = TREE_OPERAND (templ, 1);
17382 else
17383 object = NULL_TREE;
17384 templ = lookup_template_function (templ, targs);
17386 if (object)
17387 RETURN (build3 (COMPONENT_REF, TREE_TYPE (templ),
17388 object, templ, NULL_TREE));
17389 else
17390 RETURN (baselink_for_fns (templ));
17393 case INDIRECT_REF:
17395 tree r = RECUR (TREE_OPERAND (t, 0));
17397 if (REFERENCE_REF_P (t))
17399 /* A type conversion to reference type will be enclosed in
17400 such an indirect ref, but the substitution of the cast
17401 will have also added such an indirect ref. */
17402 r = convert_from_reference (r);
17404 else
17405 r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
17406 complain|decltype_flag);
17408 if (REF_PARENTHESIZED_P (t))
17409 r = force_paren_expr (r);
17411 RETURN (r);
17414 case NOP_EXPR:
17416 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17417 tree op0 = RECUR (TREE_OPERAND (t, 0));
17418 RETURN (build_nop (type, op0));
17421 case IMPLICIT_CONV_EXPR:
17423 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17424 tree expr = RECUR (TREE_OPERAND (t, 0));
17425 if (dependent_type_p (type) || type_dependent_expression_p (expr))
17427 retval = copy_node (t);
17428 TREE_TYPE (retval) = type;
17429 TREE_OPERAND (retval, 0) = expr;
17430 RETURN (retval);
17432 if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t))
17433 /* We'll pass this to convert_nontype_argument again, we don't need
17434 to actually perform any conversion here. */
17435 RETURN (expr);
17436 int flags = LOOKUP_IMPLICIT;
17437 if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t))
17438 flags = LOOKUP_NORMAL;
17439 RETURN (perform_implicit_conversion_flags (type, expr, complain,
17440 flags));
17443 case CONVERT_EXPR:
17445 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17446 tree op0 = RECUR (TREE_OPERAND (t, 0));
17447 if (op0 == error_mark_node)
17448 RETURN (error_mark_node);
17449 RETURN (build1 (CONVERT_EXPR, type, op0));
17452 case CAST_EXPR:
17453 case REINTERPRET_CAST_EXPR:
17454 case CONST_CAST_EXPR:
17455 case DYNAMIC_CAST_EXPR:
17456 case STATIC_CAST_EXPR:
17458 tree type;
17459 tree op, r = NULL_TREE;
17461 type = tsubst (TREE_TYPE (t), args, complain, in_decl);
17462 if (integral_constant_expression_p
17463 && !cast_valid_in_integral_constant_expression_p (type))
17465 if (complain & tf_error)
17466 error ("a cast to a type other than an integral or "
17467 "enumeration type cannot appear in a constant-expression");
17468 RETURN (error_mark_node);
17471 op = RECUR (TREE_OPERAND (t, 0));
17473 warning_sentinel s(warn_useless_cast);
17474 warning_sentinel s2(warn_ignored_qualifiers);
17475 switch (TREE_CODE (t))
17477 case CAST_EXPR:
17478 r = build_functional_cast (type, op, complain);
17479 break;
17480 case REINTERPRET_CAST_EXPR:
17481 r = build_reinterpret_cast (type, op, complain);
17482 break;
17483 case CONST_CAST_EXPR:
17484 r = build_const_cast (type, op, complain);
17485 break;
17486 case DYNAMIC_CAST_EXPR:
17487 r = build_dynamic_cast (type, op, complain);
17488 break;
17489 case STATIC_CAST_EXPR:
17490 r = build_static_cast (type, op, complain);
17491 break;
17492 default:
17493 gcc_unreachable ();
17496 RETURN (r);
17499 case POSTDECREMENT_EXPR:
17500 case POSTINCREMENT_EXPR:
17501 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17502 args, complain, in_decl);
17503 RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
17504 complain|decltype_flag));
17506 case PREDECREMENT_EXPR:
17507 case PREINCREMENT_EXPR:
17508 case NEGATE_EXPR:
17509 case BIT_NOT_EXPR:
17510 case ABS_EXPR:
17511 case TRUTH_NOT_EXPR:
17512 case UNARY_PLUS_EXPR: /* Unary + */
17513 case REALPART_EXPR:
17514 case IMAGPART_EXPR:
17515 RETURN (build_x_unary_op (input_location, TREE_CODE (t),
17516 RECUR (TREE_OPERAND (t, 0)),
17517 complain|decltype_flag));
17519 case FIX_TRUNC_EXPR:
17520 gcc_unreachable ();
17522 case ADDR_EXPR:
17523 op1 = TREE_OPERAND (t, 0);
17524 if (TREE_CODE (op1) == LABEL_DECL)
17525 RETURN (finish_label_address_expr (DECL_NAME (op1),
17526 EXPR_LOCATION (op1)));
17527 if (TREE_CODE (op1) == SCOPE_REF)
17528 op1 = tsubst_qualified_id (op1, args, complain, in_decl,
17529 /*done=*/true, /*address_p=*/true);
17530 else
17531 op1 = tsubst_non_call_postfix_expression (op1, args, complain,
17532 in_decl);
17533 RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
17534 complain|decltype_flag));
17536 case PLUS_EXPR:
17537 case MINUS_EXPR:
17538 case MULT_EXPR:
17539 case TRUNC_DIV_EXPR:
17540 case CEIL_DIV_EXPR:
17541 case FLOOR_DIV_EXPR:
17542 case ROUND_DIV_EXPR:
17543 case EXACT_DIV_EXPR:
17544 case BIT_AND_EXPR:
17545 case BIT_IOR_EXPR:
17546 case BIT_XOR_EXPR:
17547 case TRUNC_MOD_EXPR:
17548 case FLOOR_MOD_EXPR:
17549 case TRUTH_ANDIF_EXPR:
17550 case TRUTH_ORIF_EXPR:
17551 case TRUTH_AND_EXPR:
17552 case TRUTH_OR_EXPR:
17553 case RSHIFT_EXPR:
17554 case LSHIFT_EXPR:
17555 case RROTATE_EXPR:
17556 case LROTATE_EXPR:
17557 case EQ_EXPR:
17558 case NE_EXPR:
17559 case MAX_EXPR:
17560 case MIN_EXPR:
17561 case LE_EXPR:
17562 case GE_EXPR:
17563 case LT_EXPR:
17564 case GT_EXPR:
17565 case MEMBER_REF:
17566 case DOTSTAR_EXPR:
17568 warning_sentinel s1(warn_type_limits);
17569 warning_sentinel s2(warn_div_by_zero);
17570 warning_sentinel s3(warn_logical_op);
17571 warning_sentinel s4(warn_tautological_compare);
17572 tree op0 = RECUR (TREE_OPERAND (t, 0));
17573 tree op1 = RECUR (TREE_OPERAND (t, 1));
17574 tree r = build_x_binary_op
17575 (input_location, TREE_CODE (t),
17576 op0,
17577 (TREE_NO_WARNING (TREE_OPERAND (t, 0))
17578 ? ERROR_MARK
17579 : TREE_CODE (TREE_OPERAND (t, 0))),
17580 op1,
17581 (TREE_NO_WARNING (TREE_OPERAND (t, 1))
17582 ? ERROR_MARK
17583 : TREE_CODE (TREE_OPERAND (t, 1))),
17584 /*overload=*/NULL,
17585 complain|decltype_flag);
17586 if (EXPR_P (r) && TREE_NO_WARNING (t))
17587 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17589 RETURN (r);
17592 case POINTER_PLUS_EXPR:
17594 tree op0 = RECUR (TREE_OPERAND (t, 0));
17595 tree op1 = RECUR (TREE_OPERAND (t, 1));
17596 RETURN (fold_build_pointer_plus (op0, op1));
17599 case SCOPE_REF:
17600 RETURN (tsubst_qualified_id (t, args, complain, in_decl, /*done=*/true,
17601 /*address_p=*/false));
17602 case ARRAY_REF:
17603 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17604 args, complain, in_decl);
17605 RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
17606 RECUR (TREE_OPERAND (t, 1)),
17607 complain|decltype_flag));
17609 case SIZEOF_EXPR:
17610 if (PACK_EXPANSION_P (TREE_OPERAND (t, 0))
17611 || ARGUMENT_PACK_P (TREE_OPERAND (t, 0)))
17612 RETURN (tsubst_copy (t, args, complain, in_decl));
17613 /* Fall through */
17615 case ALIGNOF_EXPR:
17617 tree r;
17619 op1 = TREE_OPERAND (t, 0);
17620 if (TREE_CODE (t) == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (t))
17621 op1 = TREE_TYPE (op1);
17622 if (!args)
17624 /* When there are no ARGS, we are trying to evaluate a
17625 non-dependent expression from the parser. Trying to do
17626 the substitutions may not work. */
17627 if (!TYPE_P (op1))
17628 op1 = TREE_TYPE (op1);
17630 else
17632 ++cp_unevaluated_operand;
17633 ++c_inhibit_evaluation_warnings;
17634 if (TYPE_P (op1))
17635 op1 = tsubst (op1, args, complain, in_decl);
17636 else
17637 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17638 /*function_p=*/false,
17639 /*integral_constant_expression_p=*/
17640 false);
17641 --cp_unevaluated_operand;
17642 --c_inhibit_evaluation_warnings;
17644 if (TYPE_P (op1))
17645 r = cxx_sizeof_or_alignof_type (op1, TREE_CODE (t),
17646 complain & tf_error);
17647 else
17648 r = cxx_sizeof_or_alignof_expr (op1, TREE_CODE (t),
17649 complain & tf_error);
17650 if (TREE_CODE (t) == SIZEOF_EXPR && r != error_mark_node)
17652 if (TREE_CODE (r) != SIZEOF_EXPR || TYPE_P (op1))
17654 if (!processing_template_decl && TYPE_P (op1))
17656 r = build_min (SIZEOF_EXPR, size_type_node,
17657 build1 (NOP_EXPR, op1, error_mark_node));
17658 SIZEOF_EXPR_TYPE_P (r) = 1;
17660 else
17661 r = build_min (SIZEOF_EXPR, size_type_node, op1);
17662 TREE_SIDE_EFFECTS (r) = 0;
17663 TREE_READONLY (r) = 1;
17665 SET_EXPR_LOCATION (r, EXPR_LOCATION (t));
17667 RETURN (r);
17670 case AT_ENCODE_EXPR:
17672 op1 = TREE_OPERAND (t, 0);
17673 ++cp_unevaluated_operand;
17674 ++c_inhibit_evaluation_warnings;
17675 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17676 /*function_p=*/false,
17677 /*integral_constant_expression_p=*/false);
17678 --cp_unevaluated_operand;
17679 --c_inhibit_evaluation_warnings;
17680 RETURN (objc_build_encode_expr (op1));
17683 case NOEXCEPT_EXPR:
17684 op1 = TREE_OPERAND (t, 0);
17685 ++cp_unevaluated_operand;
17686 ++c_inhibit_evaluation_warnings;
17687 ++cp_noexcept_operand;
17688 op1 = tsubst_copy_and_build (op1, args, complain, in_decl,
17689 /*function_p=*/false,
17690 /*integral_constant_expression_p=*/false);
17691 --cp_unevaluated_operand;
17692 --c_inhibit_evaluation_warnings;
17693 --cp_noexcept_operand;
17694 RETURN (finish_noexcept_expr (op1, complain));
17696 case MODOP_EXPR:
17698 warning_sentinel s(warn_div_by_zero);
17699 tree lhs = RECUR (TREE_OPERAND (t, 0));
17700 tree rhs = RECUR (TREE_OPERAND (t, 2));
17701 tree r = build_x_modify_expr
17702 (EXPR_LOCATION (t), lhs, TREE_CODE (TREE_OPERAND (t, 1)), rhs,
17703 complain|decltype_flag);
17704 /* TREE_NO_WARNING must be set if either the expression was
17705 parenthesized or it uses an operator such as >>= rather
17706 than plain assignment. In the former case, it was already
17707 set and must be copied. In the latter case,
17708 build_x_modify_expr sets it and it must not be reset
17709 here. */
17710 if (TREE_NO_WARNING (t))
17711 TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
17713 RETURN (r);
17716 case ARROW_EXPR:
17717 op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
17718 args, complain, in_decl);
17719 /* Remember that there was a reference to this entity. */
17720 if (DECL_P (op1)
17721 && !mark_used (op1, complain) && !(complain & tf_error))
17722 RETURN (error_mark_node);
17723 RETURN (build_x_arrow (input_location, op1, complain));
17725 case NEW_EXPR:
17727 tree placement = RECUR (TREE_OPERAND (t, 0));
17728 tree init = RECUR (TREE_OPERAND (t, 3));
17729 vec<tree, va_gc> *placement_vec;
17730 vec<tree, va_gc> *init_vec;
17731 tree ret;
17733 if (placement == NULL_TREE)
17734 placement_vec = NULL;
17735 else
17737 placement_vec = make_tree_vector ();
17738 for (; placement != NULL_TREE; placement = TREE_CHAIN (placement))
17739 vec_safe_push (placement_vec, TREE_VALUE (placement));
17742 /* If there was an initializer in the original tree, but it
17743 instantiated to an empty list, then we should pass a
17744 non-NULL empty vector to tell build_new that it was an
17745 empty initializer() rather than no initializer. This can
17746 only happen when the initializer is a pack expansion whose
17747 parameter packs are of length zero. */
17748 if (init == NULL_TREE && TREE_OPERAND (t, 3) == NULL_TREE)
17749 init_vec = NULL;
17750 else
17752 init_vec = make_tree_vector ();
17753 if (init == void_node)
17754 gcc_assert (init_vec != NULL);
17755 else
17757 for (; init != NULL_TREE; init = TREE_CHAIN (init))
17758 vec_safe_push (init_vec, TREE_VALUE (init));
17762 tree op1 = tsubst (TREE_OPERAND (t, 1), args, complain, in_decl);
17763 tree op2 = RECUR (TREE_OPERAND (t, 2));
17764 ret = build_new (&placement_vec, op1, op2, &init_vec,
17765 NEW_EXPR_USE_GLOBAL (t),
17766 complain);
17768 if (placement_vec != NULL)
17769 release_tree_vector (placement_vec);
17770 if (init_vec != NULL)
17771 release_tree_vector (init_vec);
17773 RETURN (ret);
17776 case DELETE_EXPR:
17778 tree op0 = RECUR (TREE_OPERAND (t, 0));
17779 tree op1 = RECUR (TREE_OPERAND (t, 1));
17780 RETURN (delete_sanity (op0, op1,
17781 DELETE_EXPR_USE_VEC (t),
17782 DELETE_EXPR_USE_GLOBAL (t),
17783 complain));
17786 case COMPOUND_EXPR:
17788 tree op0 = tsubst_copy_and_build (TREE_OPERAND (t, 0), args,
17789 complain & ~tf_decltype, in_decl,
17790 /*function_p=*/false,
17791 integral_constant_expression_p);
17792 RETURN (build_x_compound_expr (EXPR_LOCATION (t),
17793 op0,
17794 RECUR (TREE_OPERAND (t, 1)),
17795 complain|decltype_flag));
17798 case CALL_EXPR:
17800 tree function;
17801 vec<tree, va_gc> *call_args;
17802 unsigned int nargs, i;
17803 bool qualified_p;
17804 bool koenig_p;
17805 tree ret;
17807 function = CALL_EXPR_FN (t);
17808 /* Internal function with no arguments. */
17809 if (function == NULL_TREE && call_expr_nargs (t) == 0)
17810 RETURN (t);
17812 /* When we parsed the expression, we determined whether or
17813 not Koenig lookup should be performed. */
17814 koenig_p = KOENIG_LOOKUP_P (t);
17815 if (function == NULL_TREE)
17817 koenig_p = false;
17818 qualified_p = false;
17820 else if (TREE_CODE (function) == SCOPE_REF)
17822 qualified_p = true;
17823 function = tsubst_qualified_id (function, args, complain, in_decl,
17824 /*done=*/false,
17825 /*address_p=*/false);
17827 else if (koenig_p && identifier_p (function))
17829 /* Do nothing; calling tsubst_copy_and_build on an identifier
17830 would incorrectly perform unqualified lookup again.
17832 Note that we can also have an IDENTIFIER_NODE if the earlier
17833 unqualified lookup found a member function; in that case
17834 koenig_p will be false and we do want to do the lookup
17835 again to find the instantiated member function.
17837 FIXME but doing that causes c++/15272, so we need to stop
17838 using IDENTIFIER_NODE in that situation. */
17839 qualified_p = false;
17841 else
17843 if (TREE_CODE (function) == COMPONENT_REF)
17845 tree op = TREE_OPERAND (function, 1);
17847 qualified_p = (TREE_CODE (op) == SCOPE_REF
17848 || (BASELINK_P (op)
17849 && BASELINK_QUALIFIED_P (op)));
17851 else
17852 qualified_p = false;
17854 if (TREE_CODE (function) == ADDR_EXPR
17855 && TREE_CODE (TREE_OPERAND (function, 0)) == FUNCTION_DECL)
17856 /* Avoid error about taking the address of a constructor. */
17857 function = TREE_OPERAND (function, 0);
17859 function = tsubst_copy_and_build (function, args, complain,
17860 in_decl,
17861 !qualified_p,
17862 integral_constant_expression_p);
17864 if (BASELINK_P (function))
17865 qualified_p = true;
17868 nargs = call_expr_nargs (t);
17869 call_args = make_tree_vector ();
17870 for (i = 0; i < nargs; ++i)
17872 tree arg = CALL_EXPR_ARG (t, i);
17874 if (!PACK_EXPANSION_P (arg))
17875 vec_safe_push (call_args, RECUR (CALL_EXPR_ARG (t, i)));
17876 else
17878 /* Expand the pack expansion and push each entry onto
17879 CALL_ARGS. */
17880 arg = tsubst_pack_expansion (arg, args, complain, in_decl);
17881 if (TREE_CODE (arg) == TREE_VEC)
17883 unsigned int len, j;
17885 len = TREE_VEC_LENGTH (arg);
17886 for (j = 0; j < len; ++j)
17888 tree value = TREE_VEC_ELT (arg, j);
17889 if (value != NULL_TREE)
17890 value = convert_from_reference (value);
17891 vec_safe_push (call_args, value);
17894 else
17896 /* A partial substitution. Add one entry. */
17897 vec_safe_push (call_args, arg);
17902 /* We do not perform argument-dependent lookup if normal
17903 lookup finds a non-function, in accordance with the
17904 expected resolution of DR 218. */
17905 if (koenig_p
17906 && ((is_overloaded_fn (function)
17907 /* If lookup found a member function, the Koenig lookup is
17908 not appropriate, even if an unqualified-name was used
17909 to denote the function. */
17910 && !DECL_FUNCTION_MEMBER_P (get_first_fn (function)))
17911 || identifier_p (function))
17912 /* Only do this when substitution turns a dependent call
17913 into a non-dependent call. */
17914 && type_dependent_expression_p_push (t)
17915 && !any_type_dependent_arguments_p (call_args))
17916 function = perform_koenig_lookup (function, call_args, tf_none);
17918 if (function != NULL_TREE
17919 && identifier_p (function)
17920 && !any_type_dependent_arguments_p (call_args))
17922 if (koenig_p && (complain & tf_warning_or_error))
17924 /* For backwards compatibility and good diagnostics, try
17925 the unqualified lookup again if we aren't in SFINAE
17926 context. */
17927 tree unq = (tsubst_copy_and_build
17928 (function, args, complain, in_decl, true,
17929 integral_constant_expression_p));
17930 if (unq == error_mark_node)
17932 release_tree_vector (call_args);
17933 RETURN (error_mark_node);
17936 if (unq != function)
17938 /* In a lambda fn, we have to be careful to not
17939 introduce new this captures. Legacy code can't
17940 be using lambdas anyway, so it's ok to be
17941 stricter. */
17942 bool in_lambda = (current_class_type
17943 && LAMBDA_TYPE_P (current_class_type));
17944 char const *const msg
17945 = G_("%qD was not declared in this scope, "
17946 "and no declarations were found by "
17947 "argument-dependent lookup at the point "
17948 "of instantiation");
17950 bool diag = true;
17951 if (in_lambda)
17952 error_at (EXPR_LOC_OR_LOC (t, input_location),
17953 msg, function);
17954 else
17955 diag = permerror (EXPR_LOC_OR_LOC (t, input_location),
17956 msg, function);
17957 if (diag)
17959 tree fn = unq;
17961 if (INDIRECT_REF_P (fn))
17962 fn = TREE_OPERAND (fn, 0);
17963 if (is_overloaded_fn (fn))
17964 fn = get_first_fn (fn);
17966 if (!DECL_P (fn))
17967 /* Can't say anything more. */;
17968 else if (DECL_CLASS_SCOPE_P (fn))
17970 location_t loc = EXPR_LOC_OR_LOC (t,
17971 input_location);
17972 inform (loc,
17973 "declarations in dependent base %qT are "
17974 "not found by unqualified lookup",
17975 DECL_CLASS_CONTEXT (fn));
17976 if (current_class_ptr)
17977 inform (loc,
17978 "use %<this->%D%> instead", function);
17979 else
17980 inform (loc,
17981 "use %<%T::%D%> instead",
17982 current_class_name, function);
17984 else
17985 inform (DECL_SOURCE_LOCATION (fn),
17986 "%qD declared here, later in the "
17987 "translation unit", fn);
17988 if (in_lambda)
17990 release_tree_vector (call_args);
17991 RETURN (error_mark_node);
17995 function = unq;
17998 if (identifier_p (function))
18000 if (complain & tf_error)
18001 unqualified_name_lookup_error (function);
18002 release_tree_vector (call_args);
18003 RETURN (error_mark_node);
18007 /* Remember that there was a reference to this entity. */
18008 if (function != NULL_TREE
18009 && DECL_P (function)
18010 && !mark_used (function, complain) && !(complain & tf_error))
18012 release_tree_vector (call_args);
18013 RETURN (error_mark_node);
18016 /* Put back tf_decltype for the actual call. */
18017 complain |= decltype_flag;
18019 if (function == NULL_TREE)
18020 switch (CALL_EXPR_IFN (t))
18022 case IFN_LAUNDER:
18023 gcc_assert (nargs == 1);
18024 if (vec_safe_length (call_args) != 1)
18026 error_at (EXPR_LOC_OR_LOC (t, input_location),
18027 "wrong number of arguments to "
18028 "%<__builtin_launder%>");
18029 ret = error_mark_node;
18031 else
18032 ret = finish_builtin_launder (EXPR_LOC_OR_LOC (t,
18033 input_location),
18034 (*call_args)[0], complain);
18035 break;
18037 default:
18038 /* Unsupported internal function with arguments. */
18039 gcc_unreachable ();
18041 else if (TREE_CODE (function) == OFFSET_REF)
18042 ret = build_offset_ref_call_from_tree (function, &call_args,
18043 complain);
18044 else if (TREE_CODE (function) == COMPONENT_REF)
18046 tree instance = TREE_OPERAND (function, 0);
18047 tree fn = TREE_OPERAND (function, 1);
18049 if (processing_template_decl
18050 && (type_dependent_expression_p (instance)
18051 || (!BASELINK_P (fn)
18052 && TREE_CODE (fn) != FIELD_DECL)
18053 || type_dependent_expression_p (fn)
18054 || any_type_dependent_arguments_p (call_args)))
18055 ret = build_min_nt_call_vec (function, call_args);
18056 else if (!BASELINK_P (fn))
18057 ret = finish_call_expr (function, &call_args,
18058 /*disallow_virtual=*/false,
18059 /*koenig_p=*/false,
18060 complain);
18061 else
18062 ret = (build_new_method_call
18063 (instance, fn,
18064 &call_args, NULL_TREE,
18065 qualified_p ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL,
18066 /*fn_p=*/NULL,
18067 complain));
18069 else
18070 ret = finish_call_expr (function, &call_args,
18071 /*disallow_virtual=*/qualified_p,
18072 koenig_p,
18073 complain);
18075 release_tree_vector (call_args);
18077 if (ret != error_mark_node)
18079 bool op = CALL_EXPR_OPERATOR_SYNTAX (t);
18080 bool ord = CALL_EXPR_ORDERED_ARGS (t);
18081 bool rev = CALL_EXPR_REVERSE_ARGS (t);
18082 bool thk = CALL_FROM_THUNK_P (t);
18083 if (op || ord || rev || thk)
18085 function = extract_call_expr (ret);
18086 CALL_EXPR_OPERATOR_SYNTAX (function) = op;
18087 CALL_EXPR_ORDERED_ARGS (function) = ord;
18088 CALL_EXPR_REVERSE_ARGS (function) = rev;
18089 if (thk)
18091 if (TREE_CODE (function) == CALL_EXPR)
18092 CALL_FROM_THUNK_P (function) = true;
18093 else
18094 AGGR_INIT_FROM_THUNK_P (function) = true;
18095 /* The thunk location is not interesting. */
18096 SET_EXPR_LOCATION (function, UNKNOWN_LOCATION);
18101 RETURN (ret);
18104 case COND_EXPR:
18106 tree cond = RECUR (TREE_OPERAND (t, 0));
18107 tree folded_cond = fold_non_dependent_expr (cond);
18108 tree exp1, exp2;
18110 if (TREE_CODE (folded_cond) == INTEGER_CST)
18112 if (integer_zerop (folded_cond))
18114 ++c_inhibit_evaluation_warnings;
18115 exp1 = RECUR (TREE_OPERAND (t, 1));
18116 --c_inhibit_evaluation_warnings;
18117 exp2 = RECUR (TREE_OPERAND (t, 2));
18119 else
18121 exp1 = RECUR (TREE_OPERAND (t, 1));
18122 ++c_inhibit_evaluation_warnings;
18123 exp2 = RECUR (TREE_OPERAND (t, 2));
18124 --c_inhibit_evaluation_warnings;
18126 cond = folded_cond;
18128 else
18130 exp1 = RECUR (TREE_OPERAND (t, 1));
18131 exp2 = RECUR (TREE_OPERAND (t, 2));
18134 warning_sentinel s(warn_duplicated_branches);
18135 RETURN (build_x_conditional_expr (EXPR_LOCATION (t),
18136 cond, exp1, exp2, complain));
18139 case PSEUDO_DTOR_EXPR:
18141 tree op0 = RECUR (TREE_OPERAND (t, 0));
18142 tree op1 = RECUR (TREE_OPERAND (t, 1));
18143 tree op2 = tsubst (TREE_OPERAND (t, 2), args, complain, in_decl);
18144 RETURN (finish_pseudo_destructor_expr (op0, op1, op2,
18145 input_location));
18148 case TREE_LIST:
18150 tree purpose, value, chain;
18152 if (t == void_list_node)
18153 RETURN (t);
18155 if ((TREE_PURPOSE (t) && PACK_EXPANSION_P (TREE_PURPOSE (t)))
18156 || (TREE_VALUE (t) && PACK_EXPANSION_P (TREE_VALUE (t))))
18158 /* We have pack expansions, so expand those and
18159 create a new list out of it. */
18160 tree purposevec = NULL_TREE;
18161 tree valuevec = NULL_TREE;
18162 tree chain;
18163 int i, len = -1;
18165 /* Expand the argument expressions. */
18166 if (TREE_PURPOSE (t))
18167 purposevec = tsubst_pack_expansion (TREE_PURPOSE (t), args,
18168 complain, in_decl);
18169 if (TREE_VALUE (t))
18170 valuevec = tsubst_pack_expansion (TREE_VALUE (t), args,
18171 complain, in_decl);
18173 /* Build the rest of the list. */
18174 chain = TREE_CHAIN (t);
18175 if (chain && chain != void_type_node)
18176 chain = RECUR (chain);
18178 /* Determine the number of arguments. */
18179 if (purposevec && TREE_CODE (purposevec) == TREE_VEC)
18181 len = TREE_VEC_LENGTH (purposevec);
18182 gcc_assert (!valuevec || len == TREE_VEC_LENGTH (valuevec));
18184 else if (TREE_CODE (valuevec) == TREE_VEC)
18185 len = TREE_VEC_LENGTH (valuevec);
18186 else
18188 /* Since we only performed a partial substitution into
18189 the argument pack, we only RETURN (a single list
18190 node. */
18191 if (purposevec == TREE_PURPOSE (t)
18192 && valuevec == TREE_VALUE (t)
18193 && chain == TREE_CHAIN (t))
18194 RETURN (t);
18196 RETURN (tree_cons (purposevec, valuevec, chain));
18199 /* Convert the argument vectors into a TREE_LIST */
18200 i = len;
18201 while (i > 0)
18203 /* Grab the Ith values. */
18204 i--;
18205 purpose = purposevec ? TREE_VEC_ELT (purposevec, i)
18206 : NULL_TREE;
18207 value
18208 = valuevec ? convert_from_reference (TREE_VEC_ELT (valuevec, i))
18209 : NULL_TREE;
18211 /* Build the list (backwards). */
18212 chain = tree_cons (purpose, value, chain);
18215 RETURN (chain);
18218 purpose = TREE_PURPOSE (t);
18219 if (purpose)
18220 purpose = RECUR (purpose);
18221 value = TREE_VALUE (t);
18222 if (value)
18223 value = RECUR (value);
18224 chain = TREE_CHAIN (t);
18225 if (chain && chain != void_type_node)
18226 chain = RECUR (chain);
18227 if (purpose == TREE_PURPOSE (t)
18228 && value == TREE_VALUE (t)
18229 && chain == TREE_CHAIN (t))
18230 RETURN (t);
18231 RETURN (tree_cons (purpose, value, chain));
18234 case COMPONENT_REF:
18236 tree object;
18237 tree object_type;
18238 tree member;
18239 tree r;
18241 object = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
18242 args, complain, in_decl);
18243 /* Remember that there was a reference to this entity. */
18244 if (DECL_P (object)
18245 && !mark_used (object, complain) && !(complain & tf_error))
18246 RETURN (error_mark_node);
18247 object_type = TREE_TYPE (object);
18249 member = TREE_OPERAND (t, 1);
18250 if (BASELINK_P (member))
18251 member = tsubst_baselink (member,
18252 non_reference (TREE_TYPE (object)),
18253 args, complain, in_decl);
18254 else
18255 member = tsubst_copy (member, args, complain, in_decl);
18256 if (member == error_mark_node)
18257 RETURN (error_mark_node);
18259 if (TREE_CODE (member) == FIELD_DECL)
18261 r = finish_non_static_data_member (member, object, NULL_TREE);
18262 if (TREE_CODE (r) == COMPONENT_REF)
18263 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
18264 RETURN (r);
18266 else if (type_dependent_expression_p (object))
18267 /* We can't do much here. */;
18268 else if (!CLASS_TYPE_P (object_type))
18270 if (scalarish_type_p (object_type))
18272 tree s = NULL_TREE;
18273 tree dtor = member;
18275 if (TREE_CODE (dtor) == SCOPE_REF)
18277 s = TREE_OPERAND (dtor, 0);
18278 dtor = TREE_OPERAND (dtor, 1);
18280 if (TREE_CODE (dtor) == BIT_NOT_EXPR)
18282 dtor = TREE_OPERAND (dtor, 0);
18283 if (TYPE_P (dtor))
18284 RETURN (finish_pseudo_destructor_expr
18285 (object, s, dtor, input_location));
18289 else if (TREE_CODE (member) == SCOPE_REF
18290 && TREE_CODE (TREE_OPERAND (member, 1)) == TEMPLATE_ID_EXPR)
18292 /* Lookup the template functions now that we know what the
18293 scope is. */
18294 tree scope = TREE_OPERAND (member, 0);
18295 tree tmpl = TREE_OPERAND (TREE_OPERAND (member, 1), 0);
18296 tree args = TREE_OPERAND (TREE_OPERAND (member, 1), 1);
18297 member = lookup_qualified_name (scope, tmpl,
18298 /*is_type_p=*/false,
18299 /*complain=*/false);
18300 if (BASELINK_P (member))
18302 BASELINK_FUNCTIONS (member)
18303 = build_nt (TEMPLATE_ID_EXPR, BASELINK_FUNCTIONS (member),
18304 args);
18305 member = (adjust_result_of_qualified_name_lookup
18306 (member, BINFO_TYPE (BASELINK_BINFO (member)),
18307 object_type));
18309 else
18311 qualified_name_lookup_error (scope, tmpl, member,
18312 input_location);
18313 RETURN (error_mark_node);
18316 else if (TREE_CODE (member) == SCOPE_REF
18317 && !CLASS_TYPE_P (TREE_OPERAND (member, 0))
18318 && TREE_CODE (TREE_OPERAND (member, 0)) != NAMESPACE_DECL)
18320 if (complain & tf_error)
18322 if (TYPE_P (TREE_OPERAND (member, 0)))
18323 error ("%qT is not a class or namespace",
18324 TREE_OPERAND (member, 0));
18325 else
18326 error ("%qD is not a class or namespace",
18327 TREE_OPERAND (member, 0));
18329 RETURN (error_mark_node);
18332 r = finish_class_member_access_expr (object, member,
18333 /*template_p=*/false,
18334 complain);
18335 if (TREE_CODE (r) == COMPONENT_REF)
18336 REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t);
18337 RETURN (r);
18340 case THROW_EXPR:
18341 RETURN (build_throw
18342 (RECUR (TREE_OPERAND (t, 0))));
18344 case CONSTRUCTOR:
18346 vec<constructor_elt, va_gc> *n;
18347 constructor_elt *ce;
18348 unsigned HOST_WIDE_INT idx;
18349 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18350 bool process_index_p;
18351 int newlen;
18352 bool need_copy_p = false;
18353 tree r;
18355 if (type == error_mark_node)
18356 RETURN (error_mark_node);
18358 /* We do not want to process the index of aggregate
18359 initializers as they are identifier nodes which will be
18360 looked up by digest_init. */
18361 process_index_p = !(type && MAYBE_CLASS_TYPE_P (type));
18363 n = vec_safe_copy (CONSTRUCTOR_ELTS (t));
18364 newlen = vec_safe_length (n);
18365 FOR_EACH_VEC_SAFE_ELT (n, idx, ce)
18367 if (ce->index && process_index_p
18368 /* An identifier index is looked up in the type
18369 being initialized, not the current scope. */
18370 && TREE_CODE (ce->index) != IDENTIFIER_NODE)
18371 ce->index = RECUR (ce->index);
18373 if (PACK_EXPANSION_P (ce->value))
18375 /* Substitute into the pack expansion. */
18376 ce->value = tsubst_pack_expansion (ce->value, args, complain,
18377 in_decl);
18379 if (ce->value == error_mark_node
18380 || PACK_EXPANSION_P (ce->value))
18382 else if (TREE_VEC_LENGTH (ce->value) == 1)
18383 /* Just move the argument into place. */
18384 ce->value = TREE_VEC_ELT (ce->value, 0);
18385 else
18387 /* Update the length of the final CONSTRUCTOR
18388 arguments vector, and note that we will need to
18389 copy.*/
18390 newlen = newlen + TREE_VEC_LENGTH (ce->value) - 1;
18391 need_copy_p = true;
18394 else
18395 ce->value = RECUR (ce->value);
18398 if (need_copy_p)
18400 vec<constructor_elt, va_gc> *old_n = n;
18402 vec_alloc (n, newlen);
18403 FOR_EACH_VEC_ELT (*old_n, idx, ce)
18405 if (TREE_CODE (ce->value) == TREE_VEC)
18407 int i, len = TREE_VEC_LENGTH (ce->value);
18408 for (i = 0; i < len; ++i)
18409 CONSTRUCTOR_APPEND_ELT (n, 0,
18410 TREE_VEC_ELT (ce->value, i));
18412 else
18413 CONSTRUCTOR_APPEND_ELT (n, 0, ce->value);
18417 r = build_constructor (init_list_type_node, n);
18418 CONSTRUCTOR_IS_DIRECT_INIT (r) = CONSTRUCTOR_IS_DIRECT_INIT (t);
18420 if (TREE_HAS_CONSTRUCTOR (t))
18422 fcl_t cl = fcl_functional;
18423 if (CONSTRUCTOR_C99_COMPOUND_LITERAL (t))
18424 cl = fcl_c99;
18425 RETURN (finish_compound_literal (type, r, complain, cl));
18428 TREE_TYPE (r) = type;
18429 RETURN (r);
18432 case TYPEID_EXPR:
18434 tree operand_0 = TREE_OPERAND (t, 0);
18435 if (TYPE_P (operand_0))
18437 operand_0 = tsubst (operand_0, args, complain, in_decl);
18438 RETURN (get_typeid (operand_0, complain));
18440 else
18442 operand_0 = RECUR (operand_0);
18443 RETURN (build_typeid (operand_0, complain));
18447 case VAR_DECL:
18448 if (!args)
18449 RETURN (t);
18450 /* Fall through */
18452 case PARM_DECL:
18454 tree r = tsubst_copy (t, args, complain, in_decl);
18455 /* ??? We're doing a subset of finish_id_expression here. */
18456 if (VAR_P (r)
18457 && !processing_template_decl
18458 && !cp_unevaluated_operand
18459 && (TREE_STATIC (r) || DECL_EXTERNAL (r))
18460 && CP_DECL_THREAD_LOCAL_P (r))
18462 if (tree wrap = get_tls_wrapper_fn (r))
18463 /* Replace an evaluated use of the thread_local variable with
18464 a call to its wrapper. */
18465 r = build_cxx_call (wrap, 0, NULL, tf_warning_or_error);
18467 else if (outer_automatic_var_p (r))
18468 r = process_outer_var_ref (r, complain);
18470 if (TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)
18471 /* If the original type was a reference, we'll be wrapped in
18472 the appropriate INDIRECT_REF. */
18473 r = convert_from_reference (r);
18474 RETURN (r);
18477 case VA_ARG_EXPR:
18479 tree op0 = RECUR (TREE_OPERAND (t, 0));
18480 tree type = tsubst (TREE_TYPE (t), args, complain, in_decl);
18481 RETURN (build_x_va_arg (EXPR_LOCATION (t), op0, type));
18484 case OFFSETOF_EXPR:
18486 tree object_ptr
18487 = tsubst_copy_and_build (TREE_OPERAND (t, 1), args, complain,
18488 in_decl, /*function_p=*/false,
18489 /*integral_constant_expression_p=*/false);
18490 RETURN (finish_offsetof (object_ptr,
18491 RECUR (TREE_OPERAND (t, 0)),
18492 EXPR_LOCATION (t)));
18495 case ADDRESSOF_EXPR:
18496 RETURN (cp_build_addressof (EXPR_LOCATION (t),
18497 RECUR (TREE_OPERAND (t, 0)), complain));
18499 case TRAIT_EXPR:
18501 tree type1 = tsubst (TRAIT_EXPR_TYPE1 (t), args,
18502 complain, in_decl);
18504 tree type2 = TRAIT_EXPR_TYPE2 (t);
18505 if (type2 && TREE_CODE (type2) == TREE_LIST)
18506 type2 = RECUR (type2);
18507 else if (type2)
18508 type2 = tsubst (type2, args, complain, in_decl);
18510 RETURN (finish_trait_expr (TRAIT_EXPR_KIND (t), type1, type2));
18513 case STMT_EXPR:
18515 tree old_stmt_expr = cur_stmt_expr;
18516 tree stmt_expr = begin_stmt_expr ();
18518 cur_stmt_expr = stmt_expr;
18519 tsubst_expr (STMT_EXPR_STMT (t), args, complain, in_decl,
18520 integral_constant_expression_p);
18521 stmt_expr = finish_stmt_expr (stmt_expr, false);
18522 cur_stmt_expr = old_stmt_expr;
18524 /* If the resulting list of expression statement is empty,
18525 fold it further into void_node. */
18526 if (empty_expr_stmt_p (stmt_expr))
18527 stmt_expr = void_node;
18529 RETURN (stmt_expr);
18532 case LAMBDA_EXPR:
18534 tree r = tsubst_lambda_expr (t, args, complain, in_decl);
18536 RETURN (build_lambda_object (r));
18539 case TARGET_EXPR:
18540 /* We can get here for a constant initializer of non-dependent type.
18541 FIXME stop folding in cp_parser_initializer_clause. */
18543 tree r = get_target_expr_sfinae (RECUR (TARGET_EXPR_INITIAL (t)),
18544 complain);
18545 RETURN (r);
18548 case TRANSACTION_EXPR:
18549 RETURN (tsubst_expr(t, args, complain, in_decl,
18550 integral_constant_expression_p));
18552 case PAREN_EXPR:
18553 RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
18555 case VEC_PERM_EXPR:
18557 tree op0 = RECUR (TREE_OPERAND (t, 0));
18558 tree op1 = RECUR (TREE_OPERAND (t, 1));
18559 tree op2 = RECUR (TREE_OPERAND (t, 2));
18560 RETURN (build_x_vec_perm_expr (input_location, op0, op1, op2,
18561 complain));
18564 case REQUIRES_EXPR:
18565 RETURN (tsubst_requires_expr (t, args, complain, in_decl));
18567 case NON_LVALUE_EXPR:
18568 case VIEW_CONVERT_EXPR:
18569 /* We should only see these for location wrapper nodes, or within
18570 instantiate_non_dependent_expr (when args is NULL_TREE). */
18571 gcc_assert (location_wrapper_p (t) || args == NULL_TREE);
18572 if (location_wrapper_p (t))
18573 RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)),
18574 EXPR_LOCATION (t)));
18575 /* fallthrough. */
18577 default:
18578 /* Handle Objective-C++ constructs, if appropriate. */
18580 tree subst
18581 = objcp_tsubst_copy_and_build (t, args, complain,
18582 in_decl, /*function_p=*/false);
18583 if (subst)
18584 RETURN (subst);
18586 RETURN (tsubst_copy (t, args, complain, in_decl));
18589 #undef RECUR
18590 #undef RETURN
18591 out:
18592 input_location = loc;
18593 return retval;
18596 /* Verify that the instantiated ARGS are valid. For type arguments,
18597 make sure that the type's linkage is ok. For non-type arguments,
18598 make sure they are constants if they are integral or enumerations.
18599 Emit an error under control of COMPLAIN, and return TRUE on error. */
18601 static bool
18602 check_instantiated_arg (tree tmpl, tree t, tsubst_flags_t complain)
18604 if (dependent_template_arg_p (t))
18605 return false;
18606 if (ARGUMENT_PACK_P (t))
18608 tree vec = ARGUMENT_PACK_ARGS (t);
18609 int len = TREE_VEC_LENGTH (vec);
18610 bool result = false;
18611 int i;
18613 for (i = 0; i < len; ++i)
18614 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (vec, i), complain))
18615 result = true;
18616 return result;
18618 else if (TYPE_P (t))
18620 /* [basic.link]: A name with no linkage (notably, the name
18621 of a class or enumeration declared in a local scope)
18622 shall not be used to declare an entity with linkage.
18623 This implies that names with no linkage cannot be used as
18624 template arguments
18626 DR 757 relaxes this restriction for C++0x. */
18627 tree nt = (cxx_dialect > cxx98 ? NULL_TREE
18628 : no_linkage_check (t, /*relaxed_p=*/false));
18630 if (nt)
18632 /* DR 488 makes use of a type with no linkage cause
18633 type deduction to fail. */
18634 if (complain & tf_error)
18636 if (TYPE_UNNAMED_P (nt))
18637 error ("%qT is/uses unnamed type", t);
18638 else
18639 error ("template argument for %qD uses local type %qT",
18640 tmpl, t);
18642 return true;
18644 /* In order to avoid all sorts of complications, we do not
18645 allow variably-modified types as template arguments. */
18646 else if (variably_modified_type_p (t, NULL_TREE))
18648 if (complain & tf_error)
18649 error ("%qT is a variably modified type", t);
18650 return true;
18653 /* Class template and alias template arguments should be OK. */
18654 else if (DECL_TYPE_TEMPLATE_P (t))
18656 /* A non-type argument of integral or enumerated type must be a
18657 constant. */
18658 else if (TREE_TYPE (t)
18659 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (t))
18660 && !REFERENCE_REF_P (t)
18661 && !TREE_CONSTANT (t))
18663 if (complain & tf_error)
18664 error ("integral expression %qE is not constant", t);
18665 return true;
18667 return false;
18670 static bool
18671 check_instantiated_args (tree tmpl, tree args, tsubst_flags_t complain)
18673 int ix, len = DECL_NTPARMS (tmpl);
18674 bool result = false;
18676 for (ix = 0; ix != len; ix++)
18678 if (check_instantiated_arg (tmpl, TREE_VEC_ELT (args, ix), complain))
18679 result = true;
18681 if (result && (complain & tf_error))
18682 error (" trying to instantiate %qD", tmpl);
18683 return result;
18686 /* We're out of SFINAE context now, so generate diagnostics for the access
18687 errors we saw earlier when instantiating D from TMPL and ARGS. */
18689 static void
18690 recheck_decl_substitution (tree d, tree tmpl, tree args)
18692 tree pattern = DECL_TEMPLATE_RESULT (tmpl);
18693 tree type = TREE_TYPE (pattern);
18694 location_t loc = input_location;
18696 push_access_scope (d);
18697 push_deferring_access_checks (dk_no_deferred);
18698 input_location = DECL_SOURCE_LOCATION (pattern);
18699 tsubst (type, args, tf_warning_or_error, d);
18700 input_location = loc;
18701 pop_deferring_access_checks ();
18702 pop_access_scope (d);
18705 /* Instantiate the indicated variable, function, or alias template TMPL with
18706 the template arguments in TARG_PTR. */
18708 static tree
18709 instantiate_template_1 (tree tmpl, tree orig_args, tsubst_flags_t complain)
18711 tree targ_ptr = orig_args;
18712 tree fndecl;
18713 tree gen_tmpl;
18714 tree spec;
18715 bool access_ok = true;
18717 if (tmpl == error_mark_node)
18718 return error_mark_node;
18720 gcc_assert (TREE_CODE (tmpl) == TEMPLATE_DECL);
18722 /* If this function is a clone, handle it specially. */
18723 if (DECL_CLONED_FUNCTION_P (tmpl))
18725 tree spec;
18726 tree clone;
18728 /* Use DECL_ABSTRACT_ORIGIN because only FUNCTION_DECLs have
18729 DECL_CLONED_FUNCTION. */
18730 spec = instantiate_template (DECL_ABSTRACT_ORIGIN (tmpl),
18731 targ_ptr, complain);
18732 if (spec == error_mark_node)
18733 return error_mark_node;
18735 /* Look for the clone. */
18736 FOR_EACH_CLONE (clone, spec)
18737 if (DECL_NAME (clone) == DECL_NAME (tmpl))
18738 return clone;
18739 /* We should always have found the clone by now. */
18740 gcc_unreachable ();
18741 return NULL_TREE;
18744 if (targ_ptr == error_mark_node)
18745 return error_mark_node;
18747 /* Check to see if we already have this specialization. */
18748 gen_tmpl = most_general_template (tmpl);
18749 if (TMPL_ARGS_DEPTH (targ_ptr)
18750 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl)))
18751 /* targ_ptr only has the innermost template args, so add the outer ones
18752 from tmpl, which could be either a partial instantiation or gen_tmpl (in
18753 the case of a non-dependent call within a template definition). */
18754 targ_ptr = (add_outermost_template_args
18755 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (tmpl)),
18756 targ_ptr));
18758 /* It would be nice to avoid hashing here and then again in tsubst_decl,
18759 but it doesn't seem to be on the hot path. */
18760 spec = retrieve_specialization (gen_tmpl, targ_ptr, 0);
18762 gcc_assert (tmpl == gen_tmpl
18763 || ((fndecl = retrieve_specialization (tmpl, orig_args, 0))
18764 == spec)
18765 || fndecl == NULL_TREE);
18767 if (spec != NULL_TREE)
18769 if (FNDECL_HAS_ACCESS_ERRORS (spec))
18771 if (complain & tf_error)
18772 recheck_decl_substitution (spec, gen_tmpl, targ_ptr);
18773 return error_mark_node;
18775 return spec;
18778 if (check_instantiated_args (gen_tmpl, INNERMOST_TEMPLATE_ARGS (targ_ptr),
18779 complain))
18780 return error_mark_node;
18782 /* We are building a FUNCTION_DECL, during which the access of its
18783 parameters and return types have to be checked. However this
18784 FUNCTION_DECL which is the desired context for access checking
18785 is not built yet. We solve this chicken-and-egg problem by
18786 deferring all checks until we have the FUNCTION_DECL. */
18787 push_deferring_access_checks (dk_deferred);
18789 /* Instantiation of the function happens in the context of the function
18790 template, not the context of the overload resolution we're doing. */
18791 push_to_top_level ();
18792 /* If there are dependent arguments, e.g. because we're doing partial
18793 ordering, make sure processing_template_decl stays set. */
18794 if (uses_template_parms (targ_ptr))
18795 ++processing_template_decl;
18796 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18798 tree ctx = tsubst_aggr_type (DECL_CONTEXT (gen_tmpl), targ_ptr,
18799 complain, gen_tmpl, true);
18800 push_nested_class (ctx);
18803 tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl);
18805 fndecl = NULL_TREE;
18806 if (VAR_P (pattern))
18808 /* We need to determine if we're using a partial or explicit
18809 specialization now, because the type of the variable could be
18810 different. */
18811 tree tid = lookup_template_variable (gen_tmpl, targ_ptr);
18812 tree elt = most_specialized_partial_spec (tid, complain);
18813 if (elt == error_mark_node)
18814 pattern = error_mark_node;
18815 else if (elt)
18817 tree partial_tmpl = TREE_VALUE (elt);
18818 tree partial_args = TREE_PURPOSE (elt);
18819 tree partial_pat = DECL_TEMPLATE_RESULT (partial_tmpl);
18820 fndecl = tsubst (partial_pat, partial_args, complain, gen_tmpl);
18824 /* Substitute template parameters to obtain the specialization. */
18825 if (fndecl == NULL_TREE)
18826 fndecl = tsubst (pattern, targ_ptr, complain, gen_tmpl);
18827 if (DECL_CLASS_SCOPE_P (gen_tmpl))
18828 pop_nested_class ();
18829 pop_from_top_level ();
18831 if (fndecl == error_mark_node)
18833 pop_deferring_access_checks ();
18834 return error_mark_node;
18837 /* The DECL_TI_TEMPLATE should always be the immediate parent
18838 template, not the most general template. */
18839 DECL_TI_TEMPLATE (fndecl) = tmpl;
18840 DECL_TI_ARGS (fndecl) = targ_ptr;
18842 /* Now we know the specialization, compute access previously
18843 deferred. Do no access control for inheriting constructors,
18844 as we already checked access for the inherited constructor. */
18845 if (!(flag_new_inheriting_ctors
18846 && DECL_INHERITED_CTOR (fndecl)))
18848 push_access_scope (fndecl);
18849 if (!perform_deferred_access_checks (complain))
18850 access_ok = false;
18851 pop_access_scope (fndecl);
18853 pop_deferring_access_checks ();
18855 /* If we've just instantiated the main entry point for a function,
18856 instantiate all the alternate entry points as well. We do this
18857 by cloning the instantiation of the main entry point, not by
18858 instantiating the template clones. */
18859 if (DECL_CHAIN (gen_tmpl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (gen_tmpl)))
18860 clone_function_decl (fndecl, /*update_methods=*/false);
18862 if (!access_ok)
18864 if (!(complain & tf_error))
18866 /* Remember to reinstantiate when we're out of SFINAE so the user
18867 can see the errors. */
18868 FNDECL_HAS_ACCESS_ERRORS (fndecl) = true;
18870 return error_mark_node;
18872 return fndecl;
18875 /* Wrapper for instantiate_template_1. */
18877 tree
18878 instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain)
18880 tree ret;
18881 timevar_push (TV_TEMPLATE_INST);
18882 ret = instantiate_template_1 (tmpl, orig_args, complain);
18883 timevar_pop (TV_TEMPLATE_INST);
18884 return ret;
18887 /* Instantiate the alias template TMPL with ARGS. Also push a template
18888 instantiation level, which instantiate_template doesn't do because
18889 functions and variables have sufficient context established by the
18890 callers. */
18892 static tree
18893 instantiate_alias_template (tree tmpl, tree args, tsubst_flags_t complain)
18895 struct pending_template *old_last_pend = last_pending_template;
18896 struct tinst_level *old_error_tinst = last_error_tinst_level;
18897 if (tmpl == error_mark_node || args == error_mark_node)
18898 return error_mark_node;
18899 tree tinst = build_tree_list (tmpl, args);
18900 if (!push_tinst_level (tinst))
18902 ggc_free (tinst);
18903 return error_mark_node;
18906 args =
18907 coerce_innermost_template_parms (DECL_TEMPLATE_PARMS (tmpl),
18908 args, tmpl, complain,
18909 /*require_all_args=*/true,
18910 /*use_default_args=*/true);
18912 tree r = instantiate_template (tmpl, args, complain);
18913 pop_tinst_level ();
18914 /* We can't free this if a pending_template entry or last_error_tinst_level
18915 is pointing at it. */
18916 if (last_pending_template == old_last_pend
18917 && last_error_tinst_level == old_error_tinst)
18918 ggc_free (tinst);
18920 return r;
18923 /* PARM is a template parameter pack for FN. Returns true iff
18924 PARM is used in a deducible way in the argument list of FN. */
18926 static bool
18927 pack_deducible_p (tree parm, tree fn)
18929 tree t = FUNCTION_FIRST_USER_PARMTYPE (fn);
18930 for (; t; t = TREE_CHAIN (t))
18932 tree type = TREE_VALUE (t);
18933 tree packs;
18934 if (!PACK_EXPANSION_P (type))
18935 continue;
18936 for (packs = PACK_EXPANSION_PARAMETER_PACKS (type);
18937 packs; packs = TREE_CHAIN (packs))
18938 if (template_args_equal (TREE_VALUE (packs), parm))
18940 /* The template parameter pack is used in a function parameter
18941 pack. If this is the end of the parameter list, the
18942 template parameter pack is deducible. */
18943 if (TREE_CHAIN (t) == void_list_node)
18944 return true;
18945 else
18946 /* Otherwise, not. Well, it could be deduced from
18947 a non-pack parameter, but doing so would end up with
18948 a deduction mismatch, so don't bother. */
18949 return false;
18952 /* The template parameter pack isn't used in any function parameter
18953 packs, but it might be used deeper, e.g. tuple<Args...>. */
18954 return true;
18957 /* The FN is a TEMPLATE_DECL for a function. ARGS is an array with
18958 NARGS elements of the arguments that are being used when calling
18959 it. TARGS is a vector into which the deduced template arguments
18960 are placed.
18962 Returns either a FUNCTION_DECL for the matching specialization of FN or
18963 NULL_TREE if no suitable specialization can be found. If EXPLAIN_P is
18964 true, diagnostics will be printed to explain why it failed.
18966 If FN is a conversion operator, or we are trying to produce a specific
18967 specialization, RETURN_TYPE is the return type desired.
18969 The EXPLICIT_TARGS are explicit template arguments provided via a
18970 template-id.
18972 The parameter STRICT is one of:
18974 DEDUCE_CALL:
18975 We are deducing arguments for a function call, as in
18976 [temp.deduct.call]. If RETURN_TYPE is non-null, we are
18977 deducing arguments for a call to the result of a conversion
18978 function template, as in [over.call.object].
18980 DEDUCE_CONV:
18981 We are deducing arguments for a conversion function, as in
18982 [temp.deduct.conv].
18984 DEDUCE_EXACT:
18985 We are deducing arguments when doing an explicit instantiation
18986 as in [temp.explicit], when determining an explicit specialization
18987 as in [temp.expl.spec], or when taking the address of a function
18988 template, as in [temp.deduct.funcaddr]. */
18990 tree
18991 fn_type_unification (tree fn,
18992 tree explicit_targs,
18993 tree targs,
18994 const tree *args,
18995 unsigned int nargs,
18996 tree return_type,
18997 unification_kind_t strict,
18998 int flags,
18999 bool explain_p,
19000 bool decltype_p)
19002 tree parms;
19003 tree fntype;
19004 tree decl = NULL_TREE;
19005 tsubst_flags_t complain = (explain_p ? tf_warning_or_error : tf_none);
19006 bool ok;
19007 static int deduction_depth;
19008 struct pending_template *old_last_pend = last_pending_template;
19009 struct tinst_level *old_error_tinst = last_error_tinst_level;
19011 tree orig_fn = fn;
19012 if (flag_new_inheriting_ctors)
19013 fn = strip_inheriting_ctors (fn);
19015 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (fn);
19016 tree tinst;
19017 tree r = error_mark_node;
19019 tree full_targs = targs;
19020 if (TMPL_ARGS_DEPTH (targs)
19021 < TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (fn)))
19022 full_targs = (add_outermost_template_args
19023 (DECL_TI_ARGS (DECL_TEMPLATE_RESULT (fn)),
19024 targs));
19026 if (decltype_p)
19027 complain |= tf_decltype;
19029 /* In C++0x, it's possible to have a function template whose type depends
19030 on itself recursively. This is most obvious with decltype, but can also
19031 occur with enumeration scope (c++/48969). So we need to catch infinite
19032 recursion and reject the substitution at deduction time; this function
19033 will return error_mark_node for any repeated substitution.
19035 This also catches excessive recursion such as when f<N> depends on
19036 f<N-1> across all integers, and returns error_mark_node for all the
19037 substitutions back up to the initial one.
19039 This is, of course, not reentrant. */
19040 if (excessive_deduction_depth)
19041 return error_mark_node;
19042 tinst = build_tree_list (fn, NULL_TREE);
19043 ++deduction_depth;
19045 gcc_assert (TREE_CODE (fn) == TEMPLATE_DECL);
19047 fntype = TREE_TYPE (fn);
19048 if (explicit_targs)
19050 /* [temp.deduct]
19052 The specified template arguments must match the template
19053 parameters in kind (i.e., type, nontype, template), and there
19054 must not be more arguments than there are parameters;
19055 otherwise type deduction fails.
19057 Nontype arguments must match the types of the corresponding
19058 nontype template parameters, or must be convertible to the
19059 types of the corresponding nontype parameters as specified in
19060 _temp.arg.nontype_, otherwise type deduction fails.
19062 All references in the function type of the function template
19063 to the corresponding template parameters are replaced by the
19064 specified template argument values. If a substitution in a
19065 template parameter or in the function type of the function
19066 template results in an invalid type, type deduction fails. */
19067 int i, len = TREE_VEC_LENGTH (tparms);
19068 location_t loc = input_location;
19069 bool incomplete = false;
19071 if (explicit_targs == error_mark_node)
19072 goto fail;
19074 if (TMPL_ARGS_DEPTH (explicit_targs)
19075 < TMPL_ARGS_DEPTH (full_targs))
19076 explicit_targs = add_outermost_template_args (full_targs,
19077 explicit_targs);
19079 /* Adjust any explicit template arguments before entering the
19080 substitution context. */
19081 explicit_targs
19082 = (coerce_template_parms (tparms, explicit_targs, NULL_TREE,
19083 complain,
19084 /*require_all_args=*/false,
19085 /*use_default_args=*/false));
19086 if (explicit_targs == error_mark_node)
19087 goto fail;
19089 /* Substitute the explicit args into the function type. This is
19090 necessary so that, for instance, explicitly declared function
19091 arguments can match null pointed constants. If we were given
19092 an incomplete set of explicit args, we must not do semantic
19093 processing during substitution as we could create partial
19094 instantiations. */
19095 for (i = 0; i < len; i++)
19097 tree parm = TREE_VALUE (TREE_VEC_ELT (tparms, i));
19098 bool parameter_pack = false;
19099 tree targ = TREE_VEC_ELT (explicit_targs, i);
19101 /* Dig out the actual parm. */
19102 if (TREE_CODE (parm) == TYPE_DECL
19103 || TREE_CODE (parm) == TEMPLATE_DECL)
19105 parm = TREE_TYPE (parm);
19106 parameter_pack = TEMPLATE_TYPE_PARAMETER_PACK (parm);
19108 else if (TREE_CODE (parm) == PARM_DECL)
19110 parm = DECL_INITIAL (parm);
19111 parameter_pack = TEMPLATE_PARM_PARAMETER_PACK (parm);
19114 if (!parameter_pack && targ == NULL_TREE)
19115 /* No explicit argument for this template parameter. */
19116 incomplete = true;
19118 if (parameter_pack && pack_deducible_p (parm, fn))
19120 /* Mark the argument pack as "incomplete". We could
19121 still deduce more arguments during unification.
19122 We remove this mark in type_unification_real. */
19123 if (targ)
19125 ARGUMENT_PACK_INCOMPLETE_P(targ) = 1;
19126 ARGUMENT_PACK_EXPLICIT_ARGS (targ)
19127 = ARGUMENT_PACK_ARGS (targ);
19130 /* We have some incomplete argument packs. */
19131 incomplete = true;
19135 TREE_VALUE (tinst) = explicit_targs;
19136 if (!push_tinst_level (tinst))
19138 excessive_deduction_depth = true;
19139 goto fail;
19141 processing_template_decl += incomplete;
19142 input_location = DECL_SOURCE_LOCATION (fn);
19143 /* Ignore any access checks; we'll see them again in
19144 instantiate_template and they might have the wrong
19145 access path at this point. */
19146 push_deferring_access_checks (dk_deferred);
19147 fntype = tsubst (TREE_TYPE (fn), explicit_targs,
19148 complain | tf_partial | tf_fndecl_type, NULL_TREE);
19149 pop_deferring_access_checks ();
19150 input_location = loc;
19151 processing_template_decl -= incomplete;
19152 pop_tinst_level ();
19154 if (fntype == error_mark_node)
19155 goto fail;
19157 /* Place the explicitly specified arguments in TARGS. */
19158 explicit_targs = INNERMOST_TEMPLATE_ARGS (explicit_targs);
19159 for (i = NUM_TMPL_ARGS (explicit_targs); i--;)
19160 TREE_VEC_ELT (targs, i) = TREE_VEC_ELT (explicit_targs, i);
19163 /* Never do unification on the 'this' parameter. */
19164 parms = skip_artificial_parms_for (fn, TYPE_ARG_TYPES (fntype));
19166 if (return_type && strict == DEDUCE_CALL)
19168 /* We're deducing for a call to the result of a template conversion
19169 function. The parms we really want are in return_type. */
19170 if (POINTER_TYPE_P (return_type))
19171 return_type = TREE_TYPE (return_type);
19172 parms = TYPE_ARG_TYPES (return_type);
19174 else if (return_type)
19176 tree *new_args;
19178 parms = tree_cons (NULL_TREE, TREE_TYPE (fntype), parms);
19179 new_args = XALLOCAVEC (tree, nargs + 1);
19180 new_args[0] = return_type;
19181 memcpy (new_args + 1, args, nargs * sizeof (tree));
19182 args = new_args;
19183 ++nargs;
19186 /* We allow incomplete unification without an error message here
19187 because the standard doesn't seem to explicitly prohibit it. Our
19188 callers must be ready to deal with unification failures in any
19189 event. */
19191 TREE_VALUE (tinst) = targs;
19192 /* If we aren't explaining yet, push tinst context so we can see where
19193 any errors (e.g. from class instantiations triggered by instantiation
19194 of default template arguments) come from. If we are explaining, this
19195 context is redundant. */
19196 if (!explain_p && !push_tinst_level (tinst))
19198 excessive_deduction_depth = true;
19199 goto fail;
19202 /* type_unification_real will pass back any access checks from default
19203 template argument substitution. */
19204 vec<deferred_access_check, va_gc> *checks;
19205 checks = NULL;
19207 ok = !type_unification_real (DECL_INNERMOST_TEMPLATE_PARMS (fn),
19208 full_targs, parms, args, nargs, /*subr=*/0,
19209 strict, flags, &checks, explain_p);
19210 if (!explain_p)
19211 pop_tinst_level ();
19212 if (!ok)
19213 goto fail;
19215 /* Now that we have bindings for all of the template arguments,
19216 ensure that the arguments deduced for the template template
19217 parameters have compatible template parameter lists. We cannot
19218 check this property before we have deduced all template
19219 arguments, because the template parameter types of a template
19220 template parameter might depend on prior template parameters
19221 deduced after the template template parameter. The following
19222 ill-formed example illustrates this issue:
19224 template<typename T, template<T> class C> void f(C<5>, T);
19226 template<int N> struct X {};
19228 void g() {
19229 f(X<5>(), 5l); // error: template argument deduction fails
19232 The template parameter list of 'C' depends on the template type
19233 parameter 'T', but 'C' is deduced to 'X' before 'T' is deduced to
19234 'long'. Thus, we can't check that 'C' cannot bind to 'X' at the
19235 time that we deduce 'C'. */
19236 if (!template_template_parm_bindings_ok_p
19237 (DECL_INNERMOST_TEMPLATE_PARMS (fn), targs))
19239 unify_inconsistent_template_template_parameters (explain_p);
19240 goto fail;
19243 /* All is well so far. Now, check:
19245 [temp.deduct]
19247 When all template arguments have been deduced, all uses of
19248 template parameters in nondeduced contexts are replaced with
19249 the corresponding deduced argument values. If the
19250 substitution results in an invalid type, as described above,
19251 type deduction fails. */
19252 TREE_VALUE (tinst) = targs;
19253 if (!push_tinst_level (tinst))
19255 excessive_deduction_depth = true;
19256 goto fail;
19259 /* Also collect access checks from the instantiation. */
19260 reopen_deferring_access_checks (checks);
19262 decl = instantiate_template (fn, targs, complain);
19264 checks = get_deferred_access_checks ();
19265 pop_deferring_access_checks ();
19267 pop_tinst_level ();
19269 if (decl == error_mark_node)
19270 goto fail;
19272 /* Now perform any access checks encountered during substitution. */
19273 push_access_scope (decl);
19274 ok = perform_access_checks (checks, complain);
19275 pop_access_scope (decl);
19276 if (!ok)
19277 goto fail;
19279 /* If we're looking for an exact match, check that what we got
19280 is indeed an exact match. It might not be if some template
19281 parameters are used in non-deduced contexts. But don't check
19282 for an exact match if we have dependent template arguments;
19283 in that case we're doing partial ordering, and we already know
19284 that we have two candidates that will provide the actual type. */
19285 if (strict == DEDUCE_EXACT && !any_dependent_template_arguments_p (targs))
19287 tree substed = TREE_TYPE (decl);
19288 unsigned int i;
19290 tree sarg
19291 = skip_artificial_parms_for (decl, TYPE_ARG_TYPES (substed));
19292 if (return_type)
19293 sarg = tree_cons (NULL_TREE, TREE_TYPE (substed), sarg);
19294 for (i = 0; i < nargs && sarg; ++i, sarg = TREE_CHAIN (sarg))
19295 if (!same_type_p (args[i], TREE_VALUE (sarg)))
19297 unify_type_mismatch (explain_p, args[i],
19298 TREE_VALUE (sarg));
19299 goto fail;
19303 /* After doing deduction with the inherited constructor, actually return an
19304 instantiation of the inheriting constructor. */
19305 if (orig_fn != fn)
19306 decl = instantiate_template (orig_fn, targs, complain);
19308 r = decl;
19310 fail:
19311 --deduction_depth;
19312 if (excessive_deduction_depth)
19314 if (deduction_depth == 0)
19315 /* Reset once we're all the way out. */
19316 excessive_deduction_depth = false;
19319 /* We can't free this if a pending_template entry or last_error_tinst_level
19320 is pointing at it. */
19321 if (last_pending_template == old_last_pend
19322 && last_error_tinst_level == old_error_tinst)
19323 ggc_free (tinst);
19325 return r;
19328 /* Adjust types before performing type deduction, as described in
19329 [temp.deduct.call] and [temp.deduct.conv]. The rules in these two
19330 sections are symmetric. PARM is the type of a function parameter
19331 or the return type of the conversion function. ARG is the type of
19332 the argument passed to the call, or the type of the value
19333 initialized with the result of the conversion function.
19334 ARG_EXPR is the original argument expression, which may be null. */
19336 static int
19337 maybe_adjust_types_for_deduction (unification_kind_t strict,
19338 tree* parm,
19339 tree* arg,
19340 tree arg_expr)
19342 int result = 0;
19344 switch (strict)
19346 case DEDUCE_CALL:
19347 break;
19349 case DEDUCE_CONV:
19350 /* Swap PARM and ARG throughout the remainder of this
19351 function; the handling is precisely symmetric since PARM
19352 will initialize ARG rather than vice versa. */
19353 std::swap (parm, arg);
19354 break;
19356 case DEDUCE_EXACT:
19357 /* Core issue #873: Do the DR606 thing (see below) for these cases,
19358 too, but here handle it by stripping the reference from PARM
19359 rather than by adding it to ARG. */
19360 if (TREE_CODE (*parm) == REFERENCE_TYPE
19361 && TYPE_REF_IS_RVALUE (*parm)
19362 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19363 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19364 && TREE_CODE (*arg) == REFERENCE_TYPE
19365 && !TYPE_REF_IS_RVALUE (*arg))
19366 *parm = TREE_TYPE (*parm);
19367 /* Nothing else to do in this case. */
19368 return 0;
19370 default:
19371 gcc_unreachable ();
19374 if (TREE_CODE (*parm) != REFERENCE_TYPE)
19376 /* [temp.deduct.call]
19378 If P is not a reference type:
19380 --If A is an array type, the pointer type produced by the
19381 array-to-pointer standard conversion (_conv.array_) is
19382 used in place of A for type deduction; otherwise,
19384 --If A is a function type, the pointer type produced by
19385 the function-to-pointer standard conversion
19386 (_conv.func_) is used in place of A for type deduction;
19387 otherwise,
19389 --If A is a cv-qualified type, the top level
19390 cv-qualifiers of A's type are ignored for type
19391 deduction. */
19392 if (TREE_CODE (*arg) == ARRAY_TYPE)
19393 *arg = build_pointer_type (TREE_TYPE (*arg));
19394 else if (TREE_CODE (*arg) == FUNCTION_TYPE)
19395 *arg = build_pointer_type (*arg);
19396 else
19397 *arg = TYPE_MAIN_VARIANT (*arg);
19400 /* [14.8.2.1/3 temp.deduct.call], "A forwarding reference is an rvalue
19401 reference to a cv-unqualified template parameter that does not represent a
19402 template parameter of a class template (during class template argument
19403 deduction (13.3.1.8)). If P is a forwarding reference and the argument is
19404 an lvalue, the type "lvalue reference to A" is used in place of A for type
19405 deduction. */
19406 if (TREE_CODE (*parm) == REFERENCE_TYPE
19407 && TYPE_REF_IS_RVALUE (*parm)
19408 && TREE_CODE (TREE_TYPE (*parm)) == TEMPLATE_TYPE_PARM
19409 && !TEMPLATE_TYPE_PARM_FOR_CLASS (TREE_TYPE (*parm))
19410 && cp_type_quals (TREE_TYPE (*parm)) == TYPE_UNQUALIFIED
19411 && (arg_expr ? lvalue_p (arg_expr)
19412 /* try_one_overload doesn't provide an arg_expr, but
19413 functions are always lvalues. */
19414 : TREE_CODE (*arg) == FUNCTION_TYPE))
19415 *arg = build_reference_type (*arg);
19417 /* [temp.deduct.call]
19419 If P is a cv-qualified type, the top level cv-qualifiers
19420 of P's type are ignored for type deduction. If P is a
19421 reference type, the type referred to by P is used for
19422 type deduction. */
19423 *parm = TYPE_MAIN_VARIANT (*parm);
19424 if (TREE_CODE (*parm) == REFERENCE_TYPE)
19426 *parm = TREE_TYPE (*parm);
19427 result |= UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
19430 /* DR 322. For conversion deduction, remove a reference type on parm
19431 too (which has been swapped into ARG). */
19432 if (strict == DEDUCE_CONV && TREE_CODE (*arg) == REFERENCE_TYPE)
19433 *arg = TREE_TYPE (*arg);
19435 return result;
19438 /* Subroutine of unify_one_argument. PARM is a function parameter of a
19439 template which does contain any deducible template parameters; check if
19440 ARG is a suitable match for it. STRICT, FLAGS and EXPLAIN_P are as in
19441 unify_one_argument. */
19443 static int
19444 check_non_deducible_conversion (tree parm, tree arg, int strict,
19445 int flags, bool explain_p)
19447 tree type;
19449 if (!TYPE_P (arg))
19450 type = TREE_TYPE (arg);
19451 else
19452 type = arg;
19454 if (same_type_p (parm, type))
19455 return unify_success (explain_p);
19457 if (strict == DEDUCE_CONV)
19459 if (can_convert_arg (type, parm, NULL_TREE, flags,
19460 explain_p ? tf_warning_or_error : tf_none))
19461 return unify_success (explain_p);
19463 else if (strict != DEDUCE_EXACT)
19465 if (can_convert_arg (parm, type,
19466 TYPE_P (arg) ? NULL_TREE : arg,
19467 flags, explain_p ? tf_warning_or_error : tf_none))
19468 return unify_success (explain_p);
19471 if (strict == DEDUCE_EXACT)
19472 return unify_type_mismatch (explain_p, parm, arg);
19473 else
19474 return unify_arg_conversion (explain_p, parm, type, arg);
19477 static bool uses_deducible_template_parms (tree type);
19479 /* Returns true iff the expression EXPR is one from which a template
19480 argument can be deduced. In other words, if it's an undecorated
19481 use of a template non-type parameter. */
19483 static bool
19484 deducible_expression (tree expr)
19486 /* Strip implicit conversions. */
19487 while (CONVERT_EXPR_P (expr))
19488 expr = TREE_OPERAND (expr, 0);
19489 return (TREE_CODE (expr) == TEMPLATE_PARM_INDEX);
19492 /* Returns true iff the array domain DOMAIN uses a template parameter in a
19493 deducible way; that is, if it has a max value of <PARM> - 1. */
19495 static bool
19496 deducible_array_bound (tree domain)
19498 if (domain == NULL_TREE)
19499 return false;
19501 tree max = TYPE_MAX_VALUE (domain);
19502 if (TREE_CODE (max) != MINUS_EXPR)
19503 return false;
19505 return deducible_expression (TREE_OPERAND (max, 0));
19508 /* Returns true iff the template arguments ARGS use a template parameter
19509 in a deducible way. */
19511 static bool
19512 deducible_template_args (tree args)
19514 for (int i = 0; i < TREE_VEC_LENGTH (args); ++i)
19516 bool deducible;
19517 tree elt = TREE_VEC_ELT (args, i);
19518 if (ARGUMENT_PACK_P (elt))
19519 deducible = deducible_template_args (ARGUMENT_PACK_ARGS (elt));
19520 else
19522 if (PACK_EXPANSION_P (elt))
19523 elt = PACK_EXPANSION_PATTERN (elt);
19524 if (TREE_CODE (elt) == TEMPLATE_TEMPLATE_PARM)
19525 deducible = true;
19526 else if (TYPE_P (elt))
19527 deducible = uses_deducible_template_parms (elt);
19528 else
19529 deducible = deducible_expression (elt);
19531 if (deducible)
19532 return true;
19534 return false;
19537 /* Returns true iff TYPE contains any deducible references to template
19538 parameters, as per 14.8.2.5. */
19540 static bool
19541 uses_deducible_template_parms (tree type)
19543 if (PACK_EXPANSION_P (type))
19544 type = PACK_EXPANSION_PATTERN (type);
19546 /* T
19547 cv-list T
19548 TT<T>
19549 TT<i>
19550 TT<> */
19551 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
19552 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
19553 return true;
19555 /* T*
19557 T&& */
19558 if (POINTER_TYPE_P (type))
19559 return uses_deducible_template_parms (TREE_TYPE (type));
19561 /* T[integer-constant ]
19562 type [i] */
19563 if (TREE_CODE (type) == ARRAY_TYPE)
19564 return (uses_deducible_template_parms (TREE_TYPE (type))
19565 || deducible_array_bound (TYPE_DOMAIN (type)));
19567 /* T type ::*
19568 type T::*
19569 T T::*
19570 T (type ::*)()
19571 type (T::*)()
19572 type (type ::*)(T)
19573 type (T::*)(T)
19574 T (type ::*)(T)
19575 T (T::*)()
19576 T (T::*)(T) */
19577 if (TYPE_PTRMEM_P (type))
19578 return (uses_deducible_template_parms (TYPE_PTRMEM_CLASS_TYPE (type))
19579 || (uses_deducible_template_parms
19580 (TYPE_PTRMEM_POINTED_TO_TYPE (type))));
19582 /* template-name <T> (where template-name refers to a class template)
19583 template-name <i> (where template-name refers to a class template) */
19584 if (CLASS_TYPE_P (type)
19585 && CLASSTYPE_TEMPLATE_INFO (type)
19586 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type)))
19587 return deducible_template_args (INNERMOST_TEMPLATE_ARGS
19588 (CLASSTYPE_TI_ARGS (type)));
19590 /* type (T)
19592 T(T) */
19593 if (TREE_CODE (type) == FUNCTION_TYPE
19594 || TREE_CODE (type) == METHOD_TYPE)
19596 if (uses_deducible_template_parms (TREE_TYPE (type)))
19597 return true;
19598 tree parm = TYPE_ARG_TYPES (type);
19599 if (TREE_CODE (type) == METHOD_TYPE)
19600 parm = TREE_CHAIN (parm);
19601 for (; parm; parm = TREE_CHAIN (parm))
19602 if (uses_deducible_template_parms (TREE_VALUE (parm)))
19603 return true;
19606 return false;
19609 /* Subroutine of type_unification_real and unify_pack_expansion to
19610 handle unification of a single P/A pair. Parameters are as
19611 for those functions. */
19613 static int
19614 unify_one_argument (tree tparms, tree targs, tree parm, tree arg,
19615 int subr, unification_kind_t strict,
19616 bool explain_p)
19618 tree arg_expr = NULL_TREE;
19619 int arg_strict;
19621 if (arg == error_mark_node || parm == error_mark_node)
19622 return unify_invalid (explain_p);
19623 if (arg == unknown_type_node)
19624 /* We can't deduce anything from this, but we might get all the
19625 template args from other function args. */
19626 return unify_success (explain_p);
19628 /* Implicit conversions (Clause 4) will be performed on a function
19629 argument to convert it to the type of the corresponding function
19630 parameter if the parameter type contains no template-parameters that
19631 participate in template argument deduction. */
19632 if (strict != DEDUCE_EXACT
19633 && TYPE_P (parm) && !uses_deducible_template_parms (parm))
19634 /* For function parameters with no deducible template parameters,
19635 just return. We'll check non-dependent conversions later. */
19636 return unify_success (explain_p);
19638 switch (strict)
19640 case DEDUCE_CALL:
19641 arg_strict = (UNIFY_ALLOW_OUTER_LEVEL
19642 | UNIFY_ALLOW_MORE_CV_QUAL
19643 | UNIFY_ALLOW_DERIVED);
19644 break;
19646 case DEDUCE_CONV:
19647 arg_strict = UNIFY_ALLOW_LESS_CV_QUAL;
19648 break;
19650 case DEDUCE_EXACT:
19651 arg_strict = UNIFY_ALLOW_NONE;
19652 break;
19654 default:
19655 gcc_unreachable ();
19658 /* We only do these transformations if this is the top-level
19659 parameter_type_list in a call or declaration matching; in other
19660 situations (nested function declarators, template argument lists) we
19661 won't be comparing a type to an expression, and we don't do any type
19662 adjustments. */
19663 if (!subr)
19665 if (!TYPE_P (arg))
19667 gcc_assert (TREE_TYPE (arg) != NULL_TREE);
19668 if (type_unknown_p (arg))
19670 /* [temp.deduct.type] A template-argument can be
19671 deduced from a pointer to function or pointer
19672 to member function argument if the set of
19673 overloaded functions does not contain function
19674 templates and at most one of a set of
19675 overloaded functions provides a unique
19676 match. */
19677 resolve_overloaded_unification (tparms, targs, parm,
19678 arg, strict,
19679 arg_strict, explain_p);
19680 /* If a unique match was not found, this is a
19681 non-deduced context, so we still succeed. */
19682 return unify_success (explain_p);
19685 arg_expr = arg;
19686 arg = unlowered_expr_type (arg);
19687 if (arg == error_mark_node)
19688 return unify_invalid (explain_p);
19691 arg_strict |=
19692 maybe_adjust_types_for_deduction (strict, &parm, &arg, arg_expr);
19694 else
19695 if ((TYPE_P (parm) || TREE_CODE (parm) == TEMPLATE_DECL)
19696 != (TYPE_P (arg) || TREE_CODE (arg) == TEMPLATE_DECL))
19697 return unify_template_argument_mismatch (explain_p, parm, arg);
19699 /* For deduction from an init-list we need the actual list. */
19700 if (arg_expr && BRACE_ENCLOSED_INITIALIZER_P (arg_expr))
19701 arg = arg_expr;
19702 return unify (tparms, targs, parm, arg, arg_strict, explain_p);
19705 /* for_each_template_parm callback that always returns 0. */
19707 static int
19708 zero_r (tree, void *)
19710 return 0;
19713 /* for_each_template_parm any_fn callback to handle deduction of a template
19714 type argument from the type of an array bound. */
19716 static int
19717 array_deduction_r (tree t, void *data)
19719 tree_pair_p d = (tree_pair_p)data;
19720 tree &tparms = d->purpose;
19721 tree &targs = d->value;
19723 if (TREE_CODE (t) == ARRAY_TYPE)
19724 if (tree dom = TYPE_DOMAIN (t))
19725 if (tree max = TYPE_MAX_VALUE (dom))
19727 if (TREE_CODE (max) == MINUS_EXPR)
19728 max = TREE_OPERAND (max, 0);
19729 if (TREE_CODE (max) == TEMPLATE_PARM_INDEX)
19730 unify (tparms, targs, TREE_TYPE (max), size_type_node,
19731 UNIFY_ALLOW_NONE, /*explain*/false);
19734 /* Keep walking. */
19735 return 0;
19738 /* Try to deduce any not-yet-deduced template type arguments from the type of
19739 an array bound. This is handled separately from unify because 14.8.2.5 says
19740 "The type of a type parameter is only deduced from an array bound if it is
19741 not otherwise deduced." */
19743 static void
19744 try_array_deduction (tree tparms, tree targs, tree parm)
19746 tree_pair_s data = { tparms, targs };
19747 hash_set<tree> visited;
19748 for_each_template_parm (parm, zero_r, &data, &visited,
19749 /*nondeduced*/false, array_deduction_r);
19752 /* Most parms like fn_type_unification.
19754 If SUBR is 1, we're being called recursively (to unify the
19755 arguments of a function or method parameter of a function
19756 template).
19758 CHECKS is a pointer to a vector of access checks encountered while
19759 substituting default template arguments. */
19761 static int
19762 type_unification_real (tree tparms,
19763 tree full_targs,
19764 tree xparms,
19765 const tree *xargs,
19766 unsigned int xnargs,
19767 int subr,
19768 unification_kind_t strict,
19769 int flags,
19770 vec<deferred_access_check, va_gc> **checks,
19771 bool explain_p)
19773 tree parm, arg;
19774 int i;
19775 int ntparms = TREE_VEC_LENGTH (tparms);
19776 int saw_undeduced = 0;
19777 tree parms;
19778 const tree *args;
19779 unsigned int nargs;
19780 unsigned int ia;
19782 gcc_assert (TREE_CODE (tparms) == TREE_VEC);
19783 gcc_assert (xparms == NULL_TREE || TREE_CODE (xparms) == TREE_LIST);
19784 gcc_assert (ntparms > 0);
19786 tree targs = INNERMOST_TEMPLATE_ARGS (full_targs);
19788 /* Reset the number of non-defaulted template arguments contained
19789 in TARGS. */
19790 NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs) = NULL_TREE;
19792 again:
19793 parms = xparms;
19794 args = xargs;
19795 nargs = xnargs;
19797 ia = 0;
19798 while (parms && parms != void_list_node
19799 && ia < nargs)
19801 parm = TREE_VALUE (parms);
19803 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19804 && (!TREE_CHAIN (parms) || TREE_CHAIN (parms) == void_list_node))
19805 /* For a function parameter pack that occurs at the end of the
19806 parameter-declaration-list, the type A of each remaining
19807 argument of the call is compared with the type P of the
19808 declarator-id of the function parameter pack. */
19809 break;
19811 parms = TREE_CHAIN (parms);
19813 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19814 /* For a function parameter pack that does not occur at the
19815 end of the parameter-declaration-list, the type of the
19816 parameter pack is a non-deduced context. */
19817 continue;
19819 arg = args[ia];
19820 ++ia;
19822 if (unify_one_argument (tparms, full_targs, parm, arg, subr, strict,
19823 explain_p))
19824 return 1;
19827 if (parms
19828 && parms != void_list_node
19829 && TREE_CODE (TREE_VALUE (parms)) == TYPE_PACK_EXPANSION)
19831 /* Unify the remaining arguments with the pack expansion type. */
19832 tree argvec;
19833 tree parmvec = make_tree_vec (1);
19835 /* Allocate a TREE_VEC and copy in all of the arguments */
19836 argvec = make_tree_vec (nargs - ia);
19837 for (i = 0; ia < nargs; ++ia, ++i)
19838 TREE_VEC_ELT (argvec, i) = args[ia];
19840 /* Copy the parameter into parmvec. */
19841 TREE_VEC_ELT (parmvec, 0) = TREE_VALUE (parms);
19842 if (unify_pack_expansion (tparms, full_targs, parmvec, argvec, strict,
19843 /*subr=*/subr, explain_p))
19844 return 1;
19846 /* Advance to the end of the list of parameters. */
19847 parms = TREE_CHAIN (parms);
19850 /* Fail if we've reached the end of the parm list, and more args
19851 are present, and the parm list isn't variadic. */
19852 if (ia < nargs && parms == void_list_node)
19853 return unify_too_many_arguments (explain_p, nargs, ia);
19854 /* Fail if parms are left and they don't have default values and
19855 they aren't all deduced as empty packs (c++/57397). This is
19856 consistent with sufficient_parms_p. */
19857 if (parms && parms != void_list_node
19858 && TREE_PURPOSE (parms) == NULL_TREE)
19860 unsigned int count = nargs;
19861 tree p = parms;
19862 bool type_pack_p;
19865 type_pack_p = TREE_CODE (TREE_VALUE (p)) == TYPE_PACK_EXPANSION;
19866 if (!type_pack_p)
19867 count++;
19868 p = TREE_CHAIN (p);
19870 while (p && p != void_list_node);
19871 if (count != nargs)
19872 return unify_too_few_arguments (explain_p, ia, count,
19873 type_pack_p);
19876 if (!subr)
19878 tsubst_flags_t complain = (explain_p
19879 ? tf_warning_or_error
19880 : tf_none);
19881 bool tried_array_deduction = (cxx_dialect < cxx17);
19883 for (i = 0; i < ntparms; i++)
19885 tree targ = TREE_VEC_ELT (targs, i);
19886 tree tparm = TREE_VEC_ELT (tparms, i);
19888 /* Clear the "incomplete" flags on all argument packs now so that
19889 substituting them into later default arguments works. */
19890 if (targ && ARGUMENT_PACK_P (targ))
19892 ARGUMENT_PACK_INCOMPLETE_P (targ) = 0;
19893 ARGUMENT_PACK_EXPLICIT_ARGS (targ) = NULL_TREE;
19896 if (targ || tparm == error_mark_node)
19897 continue;
19898 tparm = TREE_VALUE (tparm);
19900 if (TREE_CODE (tparm) == TYPE_DECL
19901 && !tried_array_deduction)
19903 try_array_deduction (tparms, targs, xparms);
19904 tried_array_deduction = true;
19905 if (TREE_VEC_ELT (targs, i))
19906 continue;
19909 /* If this is an undeduced nontype parameter that depends on
19910 a type parameter, try another pass; its type may have been
19911 deduced from a later argument than the one from which
19912 this parameter can be deduced. */
19913 if (TREE_CODE (tparm) == PARM_DECL
19914 && uses_template_parms (TREE_TYPE (tparm))
19915 && saw_undeduced < 2)
19917 saw_undeduced = 1;
19918 continue;
19921 /* Core issue #226 (C++0x) [temp.deduct]:
19923 If a template argument has not been deduced, its
19924 default template argument, if any, is used.
19926 When we are in C++98 mode, TREE_PURPOSE will either
19927 be NULL_TREE or ERROR_MARK_NODE, so we do not need
19928 to explicitly check cxx_dialect here. */
19929 if (TREE_PURPOSE (TREE_VEC_ELT (tparms, i)))
19930 /* OK, there is a default argument. Wait until after the
19931 conversion check to do substitution. */
19932 continue;
19934 /* If the type parameter is a parameter pack, then it will
19935 be deduced to an empty parameter pack. */
19936 if (template_parameter_pack_p (tparm))
19938 tree arg;
19940 if (TREE_CODE (tparm) == TEMPLATE_PARM_INDEX)
19942 arg = make_node (NONTYPE_ARGUMENT_PACK);
19943 TREE_CONSTANT (arg) = 1;
19945 else
19946 arg = cxx_make_type (TYPE_ARGUMENT_PACK);
19948 SET_ARGUMENT_PACK_ARGS (arg, make_tree_vec (0));
19950 TREE_VEC_ELT (targs, i) = arg;
19951 continue;
19954 return unify_parameter_deduction_failure (explain_p, tparm);
19957 /* DR 1391: All parameters have args, now check non-dependent parms for
19958 convertibility. */
19959 if (saw_undeduced < 2)
19960 for (ia = 0, parms = xparms, args = xargs, nargs = xnargs;
19961 parms && parms != void_list_node && ia < nargs; )
19963 parm = TREE_VALUE (parms);
19965 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION
19966 && (!TREE_CHAIN (parms)
19967 || TREE_CHAIN (parms) == void_list_node))
19968 /* For a function parameter pack that occurs at the end of the
19969 parameter-declaration-list, the type A of each remaining
19970 argument of the call is compared with the type P of the
19971 declarator-id of the function parameter pack. */
19972 break;
19974 parms = TREE_CHAIN (parms);
19976 if (TREE_CODE (parm) == TYPE_PACK_EXPANSION)
19977 /* For a function parameter pack that does not occur at the
19978 end of the parameter-declaration-list, the type of the
19979 parameter pack is a non-deduced context. */
19980 continue;
19982 arg = args[ia];
19983 ++ia;
19985 if (uses_template_parms (parm))
19986 continue;
19987 if (check_non_deducible_conversion (parm, arg, strict, flags,
19988 explain_p))
19989 return 1;
19992 /* Now substitute into the default template arguments. */
19993 for (i = 0; i < ntparms; i++)
19995 tree targ = TREE_VEC_ELT (targs, i);
19996 tree tparm = TREE_VEC_ELT (tparms, i);
19998 if (targ || tparm == error_mark_node)
19999 continue;
20000 tree parm = TREE_VALUE (tparm);
20001 tree arg = TREE_PURPOSE (tparm);
20002 reopen_deferring_access_checks (*checks);
20003 location_t save_loc = input_location;
20004 if (DECL_P (parm))
20005 input_location = DECL_SOURCE_LOCATION (parm);
20006 if (saw_undeduced == 1)
20007 ++processing_template_decl;
20009 if (saw_undeduced == 1
20010 && TREE_CODE (parm) == PARM_DECL
20011 && uses_template_parms (TREE_TYPE (parm)))
20013 /* The type of this non-type parameter depends on undeduced
20014 parameters. Don't try to use its default argument yet,
20015 but do check whether the arguments we already have cause
20016 substitution failure, so that that happens before we try
20017 later default arguments (78489). */
20018 tree type = tsubst (TREE_TYPE (parm), full_targs, complain,
20019 NULL_TREE);
20020 if (type == error_mark_node)
20021 arg = error_mark_node;
20022 else
20023 arg = NULL_TREE;
20025 else
20027 arg = tsubst_template_arg (arg, full_targs, complain, NULL_TREE);
20029 if (!uses_template_parms (arg))
20030 arg = convert_template_argument (parm, arg, full_targs,
20031 complain, i, NULL_TREE);
20032 else if (saw_undeduced == 1)
20033 arg = NULL_TREE;
20034 else
20035 arg = error_mark_node;
20038 if (saw_undeduced == 1)
20039 --processing_template_decl;
20040 input_location = save_loc;
20041 *checks = get_deferred_access_checks ();
20042 pop_deferring_access_checks ();
20044 if (arg == error_mark_node)
20045 return 1;
20046 else if (arg)
20048 TREE_VEC_ELT (targs, i) = arg;
20049 /* The position of the first default template argument,
20050 is also the number of non-defaulted arguments in TARGS.
20051 Record that. */
20052 if (!NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
20053 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, i);
20057 if (saw_undeduced++ == 1)
20058 goto again;
20061 if (CHECKING_P && !NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs))
20062 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (targs, TREE_VEC_LENGTH (targs));
20064 return unify_success (explain_p);
20067 /* Subroutine of type_unification_real. Args are like the variables
20068 at the call site. ARG is an overloaded function (or template-id);
20069 we try deducing template args from each of the overloads, and if
20070 only one succeeds, we go with that. Modifies TARGS and returns
20071 true on success. */
20073 static bool
20074 resolve_overloaded_unification (tree tparms,
20075 tree targs,
20076 tree parm,
20077 tree arg,
20078 unification_kind_t strict,
20079 int sub_strict,
20080 bool explain_p)
20082 tree tempargs = copy_node (targs);
20083 int good = 0;
20084 tree goodfn = NULL_TREE;
20085 bool addr_p;
20087 if (TREE_CODE (arg) == ADDR_EXPR)
20089 arg = TREE_OPERAND (arg, 0);
20090 addr_p = true;
20092 else
20093 addr_p = false;
20095 if (TREE_CODE (arg) == COMPONENT_REF)
20096 /* Handle `&x' where `x' is some static or non-static member
20097 function name. */
20098 arg = TREE_OPERAND (arg, 1);
20100 if (TREE_CODE (arg) == OFFSET_REF)
20101 arg = TREE_OPERAND (arg, 1);
20103 /* Strip baselink information. */
20104 if (BASELINK_P (arg))
20105 arg = BASELINK_FUNCTIONS (arg);
20107 if (TREE_CODE (arg) == TEMPLATE_ID_EXPR)
20109 /* If we got some explicit template args, we need to plug them into
20110 the affected templates before we try to unify, in case the
20111 explicit args will completely resolve the templates in question. */
20113 int ok = 0;
20114 tree expl_subargs = TREE_OPERAND (arg, 1);
20115 arg = TREE_OPERAND (arg, 0);
20117 for (lkp_iterator iter (arg); iter; ++iter)
20119 tree fn = *iter;
20120 tree subargs, elem;
20122 if (TREE_CODE (fn) != TEMPLATE_DECL)
20123 continue;
20125 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
20126 expl_subargs, NULL_TREE, tf_none,
20127 /*require_all_args=*/true,
20128 /*use_default_args=*/true);
20129 if (subargs != error_mark_node
20130 && !any_dependent_template_arguments_p (subargs))
20132 elem = TREE_TYPE (instantiate_template (fn, subargs, tf_none));
20133 if (try_one_overload (tparms, targs, tempargs, parm,
20134 elem, strict, sub_strict, addr_p, explain_p)
20135 && (!goodfn || !same_type_p (goodfn, elem)))
20137 goodfn = elem;
20138 ++good;
20141 else if (subargs)
20142 ++ok;
20144 /* If no templates (or more than one) are fully resolved by the
20145 explicit arguments, this template-id is a non-deduced context; it
20146 could still be OK if we deduce all template arguments for the
20147 enclosing call through other arguments. */
20148 if (good != 1)
20149 good = ok;
20151 else if (TREE_CODE (arg) != OVERLOAD
20152 && TREE_CODE (arg) != FUNCTION_DECL)
20153 /* If ARG is, for example, "(0, &f)" then its type will be unknown
20154 -- but the deduction does not succeed because the expression is
20155 not just the function on its own. */
20156 return false;
20157 else
20158 for (lkp_iterator iter (arg); iter; ++iter)
20160 tree fn = *iter;
20161 if (try_one_overload (tparms, targs, tempargs, parm, TREE_TYPE (fn),
20162 strict, sub_strict, addr_p, explain_p)
20163 && (!goodfn || !decls_match (goodfn, fn)))
20165 goodfn = fn;
20166 ++good;
20170 /* [temp.deduct.type] A template-argument can be deduced from a pointer
20171 to function or pointer to member function argument if the set of
20172 overloaded functions does not contain function templates and at most
20173 one of a set of overloaded functions provides a unique match.
20175 So if we found multiple possibilities, we return success but don't
20176 deduce anything. */
20178 if (good == 1)
20180 int i = TREE_VEC_LENGTH (targs);
20181 for (; i--; )
20182 if (TREE_VEC_ELT (tempargs, i))
20184 tree old = TREE_VEC_ELT (targs, i);
20185 tree new_ = TREE_VEC_ELT (tempargs, i);
20186 if (new_ && old && ARGUMENT_PACK_P (old)
20187 && ARGUMENT_PACK_EXPLICIT_ARGS (old))
20188 /* Don't forget explicit template arguments in a pack. */
20189 ARGUMENT_PACK_EXPLICIT_ARGS (new_)
20190 = ARGUMENT_PACK_EXPLICIT_ARGS (old);
20191 TREE_VEC_ELT (targs, i) = new_;
20194 if (good)
20195 return true;
20197 return false;
20200 /* Core DR 115: In contexts where deduction is done and fails, or in
20201 contexts where deduction is not done, if a template argument list is
20202 specified and it, along with any default template arguments, identifies
20203 a single function template specialization, then the template-id is an
20204 lvalue for the function template specialization. */
20206 tree
20207 resolve_nondeduced_context (tree orig_expr, tsubst_flags_t complain)
20209 tree expr, offset, baselink;
20210 bool addr;
20212 if (!type_unknown_p (orig_expr))
20213 return orig_expr;
20215 expr = orig_expr;
20216 addr = false;
20217 offset = NULL_TREE;
20218 baselink = NULL_TREE;
20220 if (TREE_CODE (expr) == ADDR_EXPR)
20222 expr = TREE_OPERAND (expr, 0);
20223 addr = true;
20225 if (TREE_CODE (expr) == OFFSET_REF)
20227 offset = expr;
20228 expr = TREE_OPERAND (expr, 1);
20230 if (BASELINK_P (expr))
20232 baselink = expr;
20233 expr = BASELINK_FUNCTIONS (expr);
20236 if (TREE_CODE (expr) == TEMPLATE_ID_EXPR)
20238 int good = 0;
20239 tree goodfn = NULL_TREE;
20241 /* If we got some explicit template args, we need to plug them into
20242 the affected templates before we try to unify, in case the
20243 explicit args will completely resolve the templates in question. */
20245 tree expl_subargs = TREE_OPERAND (expr, 1);
20246 tree arg = TREE_OPERAND (expr, 0);
20247 tree badfn = NULL_TREE;
20248 tree badargs = NULL_TREE;
20250 for (lkp_iterator iter (arg); iter; ++iter)
20252 tree fn = *iter;
20253 tree subargs, elem;
20255 if (TREE_CODE (fn) != TEMPLATE_DECL)
20256 continue;
20258 subargs = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (fn),
20259 expl_subargs, NULL_TREE, tf_none,
20260 /*require_all_args=*/true,
20261 /*use_default_args=*/true);
20262 if (subargs != error_mark_node
20263 && !any_dependent_template_arguments_p (subargs))
20265 elem = instantiate_template (fn, subargs, tf_none);
20266 if (elem == error_mark_node)
20268 badfn = fn;
20269 badargs = subargs;
20271 else if (elem && (!goodfn || !decls_match (goodfn, elem)))
20273 goodfn = elem;
20274 ++good;
20278 if (good == 1)
20280 mark_used (goodfn);
20281 expr = goodfn;
20282 if (baselink)
20283 expr = build_baselink (BASELINK_BINFO (baselink),
20284 BASELINK_ACCESS_BINFO (baselink),
20285 expr, BASELINK_OPTYPE (baselink));
20286 if (offset)
20288 tree base
20289 = TYPE_MAIN_VARIANT (TREE_TYPE (TREE_OPERAND (offset, 0)));
20290 expr = build_offset_ref (base, expr, addr, complain);
20292 if (addr)
20293 expr = cp_build_addr_expr (expr, complain);
20294 return expr;
20296 else if (good == 0 && badargs && (complain & tf_error))
20297 /* There were no good options and at least one bad one, so let the
20298 user know what the problem is. */
20299 instantiate_template (badfn, badargs, complain);
20301 return orig_expr;
20304 /* Subroutine of resolve_overloaded_unification; does deduction for a single
20305 overload. Fills TARGS with any deduced arguments, or error_mark_node if
20306 different overloads deduce different arguments for a given parm.
20307 ADDR_P is true if the expression for which deduction is being
20308 performed was of the form "& fn" rather than simply "fn".
20310 Returns 1 on success. */
20312 static int
20313 try_one_overload (tree tparms,
20314 tree orig_targs,
20315 tree targs,
20316 tree parm,
20317 tree arg,
20318 unification_kind_t strict,
20319 int sub_strict,
20320 bool addr_p,
20321 bool explain_p)
20323 int nargs;
20324 tree tempargs;
20325 int i;
20327 if (arg == error_mark_node)
20328 return 0;
20330 /* [temp.deduct.type] A template-argument can be deduced from a pointer
20331 to function or pointer to member function argument if the set of
20332 overloaded functions does not contain function templates and at most
20333 one of a set of overloaded functions provides a unique match.
20335 So if this is a template, just return success. */
20337 if (uses_template_parms (arg))
20338 return 1;
20340 if (TREE_CODE (arg) == METHOD_TYPE)
20341 arg = build_ptrmemfunc_type (build_pointer_type (arg));
20342 else if (addr_p)
20343 arg = build_pointer_type (arg);
20345 sub_strict |= maybe_adjust_types_for_deduction (strict, &parm, &arg, NULL);
20347 /* We don't copy orig_targs for this because if we have already deduced
20348 some template args from previous args, unify would complain when we
20349 try to deduce a template parameter for the same argument, even though
20350 there isn't really a conflict. */
20351 nargs = TREE_VEC_LENGTH (targs);
20352 tempargs = make_tree_vec (nargs);
20354 if (unify (tparms, tempargs, parm, arg, sub_strict, explain_p))
20355 return 0;
20357 /* First make sure we didn't deduce anything that conflicts with
20358 explicitly specified args. */
20359 for (i = nargs; i--; )
20361 tree elt = TREE_VEC_ELT (tempargs, i);
20362 tree oldelt = TREE_VEC_ELT (orig_targs, i);
20364 if (!elt)
20365 /*NOP*/;
20366 else if (uses_template_parms (elt))
20367 /* Since we're unifying against ourselves, we will fill in
20368 template args used in the function parm list with our own
20369 template parms. Discard them. */
20370 TREE_VEC_ELT (tempargs, i) = NULL_TREE;
20371 else if (oldelt && ARGUMENT_PACK_P (oldelt))
20373 /* Check that the argument at each index of the deduced argument pack
20374 is equivalent to the corresponding explicitly specified argument.
20375 We may have deduced more arguments than were explicitly specified,
20376 and that's OK. */
20378 /* We used to assert ARGUMENT_PACK_INCOMPLETE_P (oldelt) here, but
20379 that's wrong if we deduce the same argument pack from multiple
20380 function arguments: it's only incomplete the first time. */
20382 tree explicit_pack = ARGUMENT_PACK_ARGS (oldelt);
20383 tree deduced_pack = ARGUMENT_PACK_ARGS (elt);
20385 if (TREE_VEC_LENGTH (deduced_pack)
20386 < TREE_VEC_LENGTH (explicit_pack))
20387 return 0;
20389 for (int j = 0; j < TREE_VEC_LENGTH (explicit_pack); j++)
20390 if (!template_args_equal (TREE_VEC_ELT (explicit_pack, j),
20391 TREE_VEC_ELT (deduced_pack, j)))
20392 return 0;
20394 else if (oldelt && !template_args_equal (oldelt, elt))
20395 return 0;
20398 for (i = nargs; i--; )
20400 tree elt = TREE_VEC_ELT (tempargs, i);
20402 if (elt)
20403 TREE_VEC_ELT (targs, i) = elt;
20406 return 1;
20409 /* PARM is a template class (perhaps with unbound template
20410 parameters). ARG is a fully instantiated type. If ARG can be
20411 bound to PARM, return ARG, otherwise return NULL_TREE. TPARMS and
20412 TARGS are as for unify. */
20414 static tree
20415 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
20416 bool explain_p)
20418 tree copy_of_targs;
20420 if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
20421 return NULL_TREE;
20422 else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20423 /* Matches anything. */;
20424 else if (most_general_template (CLASSTYPE_TI_TEMPLATE (arg))
20425 != most_general_template (CLASSTYPE_TI_TEMPLATE (parm)))
20426 return NULL_TREE;
20428 /* We need to make a new template argument vector for the call to
20429 unify. If we used TARGS, we'd clutter it up with the result of
20430 the attempted unification, even if this class didn't work out.
20431 We also don't want to commit ourselves to all the unifications
20432 we've already done, since unification is supposed to be done on
20433 an argument-by-argument basis. In other words, consider the
20434 following pathological case:
20436 template <int I, int J, int K>
20437 struct S {};
20439 template <int I, int J>
20440 struct S<I, J, 2> : public S<I, I, I>, S<J, J, J> {};
20442 template <int I, int J, int K>
20443 void f(S<I, J, K>, S<I, I, I>);
20445 void g() {
20446 S<0, 0, 0> s0;
20447 S<0, 1, 2> s2;
20449 f(s0, s2);
20452 Now, by the time we consider the unification involving `s2', we
20453 already know that we must have `f<0, 0, 0>'. But, even though
20454 `S<0, 1, 2>' is derived from `S<0, 0, 0>', the code is invalid
20455 because there are two ways to unify base classes of S<0, 1, 2>
20456 with S<I, I, I>. If we kept the already deduced knowledge, we
20457 would reject the possibility I=1. */
20458 copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
20460 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20462 if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
20463 return NULL_TREE;
20464 return arg;
20467 /* If unification failed, we're done. */
20468 if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
20469 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
20470 return NULL_TREE;
20472 return arg;
20475 /* Given a template type PARM and a class type ARG, find the unique
20476 base type in ARG that is an instance of PARM. We do not examine
20477 ARG itself; only its base-classes. If there is not exactly one
20478 appropriate base class, return NULL_TREE. PARM may be the type of
20479 a partial specialization, as well as a plain template type. Used
20480 by unify. */
20482 static enum template_base_result
20483 get_template_base (tree tparms, tree targs, tree parm, tree arg,
20484 bool explain_p, tree *result)
20486 tree rval = NULL_TREE;
20487 tree binfo;
20489 gcc_assert (RECORD_OR_UNION_CODE_P (TREE_CODE (arg)));
20491 binfo = TYPE_BINFO (complete_type (arg));
20492 if (!binfo)
20494 /* The type could not be completed. */
20495 *result = NULL_TREE;
20496 return tbr_incomplete_type;
20499 /* Walk in inheritance graph order. The search order is not
20500 important, and this avoids multiple walks of virtual bases. */
20501 for (binfo = TREE_CHAIN (binfo); binfo; binfo = TREE_CHAIN (binfo))
20503 tree r = try_class_unification (tparms, targs, parm,
20504 BINFO_TYPE (binfo), explain_p);
20506 if (r)
20508 /* If there is more than one satisfactory baseclass, then:
20510 [temp.deduct.call]
20512 If they yield more than one possible deduced A, the type
20513 deduction fails.
20515 applies. */
20516 if (rval && !same_type_p (r, rval))
20518 *result = NULL_TREE;
20519 return tbr_ambiguous_baseclass;
20522 rval = r;
20526 *result = rval;
20527 return tbr_success;
20530 /* Returns the level of DECL, which declares a template parameter. */
20532 static int
20533 template_decl_level (tree decl)
20535 switch (TREE_CODE (decl))
20537 case TYPE_DECL:
20538 case TEMPLATE_DECL:
20539 return TEMPLATE_TYPE_LEVEL (TREE_TYPE (decl));
20541 case PARM_DECL:
20542 return TEMPLATE_PARM_LEVEL (DECL_INITIAL (decl));
20544 default:
20545 gcc_unreachable ();
20547 return 0;
20550 /* Decide whether ARG can be unified with PARM, considering only the
20551 cv-qualifiers of each type, given STRICT as documented for unify.
20552 Returns nonzero iff the unification is OK on that basis. */
20554 static int
20555 check_cv_quals_for_unify (int strict, tree arg, tree parm)
20557 int arg_quals = cp_type_quals (arg);
20558 int parm_quals = cp_type_quals (parm);
20560 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20561 && !(strict & UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20563 /* Although a CVR qualifier is ignored when being applied to a
20564 substituted template parameter ([8.3.2]/1 for example), that
20565 does not allow us to unify "const T" with "int&" because both
20566 types are not of the form "cv-list T" [14.8.2.5 temp.deduct.type].
20567 It is ok when we're allowing additional CV qualifiers
20568 at the outer level [14.8.2.1]/3,1st bullet. */
20569 if ((TREE_CODE (arg) == REFERENCE_TYPE
20570 || TREE_CODE (arg) == FUNCTION_TYPE
20571 || TREE_CODE (arg) == METHOD_TYPE)
20572 && (parm_quals & (TYPE_QUAL_CONST | TYPE_QUAL_VOLATILE)))
20573 return 0;
20575 if ((!POINTER_TYPE_P (arg) && TREE_CODE (arg) != TEMPLATE_TYPE_PARM)
20576 && (parm_quals & TYPE_QUAL_RESTRICT))
20577 return 0;
20580 if (!(strict & (UNIFY_ALLOW_MORE_CV_QUAL | UNIFY_ALLOW_OUTER_MORE_CV_QUAL))
20581 && (arg_quals & parm_quals) != parm_quals)
20582 return 0;
20584 if (!(strict & (UNIFY_ALLOW_LESS_CV_QUAL | UNIFY_ALLOW_OUTER_LESS_CV_QUAL))
20585 && (parm_quals & arg_quals) != arg_quals)
20586 return 0;
20588 return 1;
20591 /* Determines the LEVEL and INDEX for the template parameter PARM. */
20592 void
20593 template_parm_level_and_index (tree parm, int* level, int* index)
20595 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20596 || TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
20597 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
20599 *index = TEMPLATE_TYPE_IDX (parm);
20600 *level = TEMPLATE_TYPE_LEVEL (parm);
20602 else
20604 *index = TEMPLATE_PARM_IDX (parm);
20605 *level = TEMPLATE_PARM_LEVEL (parm);
20609 #define RECUR_AND_CHECK_FAILURE(TP, TA, P, A, S, EP) \
20610 do { \
20611 if (unify (TP, TA, P, A, S, EP)) \
20612 return 1; \
20613 } while (0)
20615 /* Unifies the remaining arguments in PACKED_ARGS with the pack
20616 expansion at the end of PACKED_PARMS. Returns 0 if the type
20617 deduction succeeds, 1 otherwise. STRICT is the same as in
20618 fn_type_unification. CALL_ARGS_P is true iff PACKED_ARGS is actually a
20619 function call argument list. We'll need to adjust the arguments to make them
20620 types. SUBR tells us if this is from a recursive call to
20621 type_unification_real, or for comparing two template argument
20622 lists. */
20624 static int
20625 unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
20626 tree packed_args, unification_kind_t strict,
20627 bool subr, bool explain_p)
20629 tree parm
20630 = TREE_VEC_ELT (packed_parms, TREE_VEC_LENGTH (packed_parms) - 1);
20631 tree pattern = PACK_EXPANSION_PATTERN (parm);
20632 tree pack, packs = NULL_TREE;
20633 int i, start = TREE_VEC_LENGTH (packed_parms) - 1;
20635 /* Add in any args remembered from an earlier partial instantiation. */
20636 targs = add_to_template_args (PACK_EXPANSION_EXTRA_ARGS (parm), targs);
20637 int levels = TMPL_ARGS_DEPTH (targs);
20639 packed_args = expand_template_argument_pack (packed_args);
20641 int len = TREE_VEC_LENGTH (packed_args);
20643 /* Determine the parameter packs we will be deducing from the
20644 pattern, and record their current deductions. */
20645 for (pack = PACK_EXPANSION_PARAMETER_PACKS (parm);
20646 pack; pack = TREE_CHAIN (pack))
20648 tree parm_pack = TREE_VALUE (pack);
20649 int idx, level;
20651 /* Only template parameter packs can be deduced, not e.g. function
20652 parameter packs or __bases or __integer_pack. */
20653 if (!TEMPLATE_PARM_P (parm_pack))
20654 continue;
20656 /* Determine the index and level of this parameter pack. */
20657 template_parm_level_and_index (parm_pack, &level, &idx);
20658 if (level < levels)
20659 continue;
20661 /* Keep track of the parameter packs and their corresponding
20662 argument packs. */
20663 packs = tree_cons (parm_pack, TMPL_ARG (targs, level, idx), packs);
20664 TREE_TYPE (packs) = make_tree_vec (len - start);
20667 /* Loop through all of the arguments that have not yet been
20668 unified and unify each with the pattern. */
20669 for (i = start; i < len; i++)
20671 tree parm;
20672 bool any_explicit = false;
20673 tree arg = TREE_VEC_ELT (packed_args, i);
20675 /* For each parameter pack, set its TMPL_ARG to either NULL_TREE
20676 or the element of its argument pack at the current index if
20677 this argument was explicitly specified. */
20678 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20680 int idx, level;
20681 tree arg, pargs;
20682 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20684 arg = NULL_TREE;
20685 if (TREE_VALUE (pack)
20686 && (pargs = ARGUMENT_PACK_EXPLICIT_ARGS (TREE_VALUE (pack)))
20687 && (i - start < TREE_VEC_LENGTH (pargs)))
20689 any_explicit = true;
20690 arg = TREE_VEC_ELT (pargs, i - start);
20692 TMPL_ARG (targs, level, idx) = arg;
20695 /* If we had explicit template arguments, substitute them into the
20696 pattern before deduction. */
20697 if (any_explicit)
20699 /* Some arguments might still be unspecified or dependent. */
20700 bool dependent;
20701 ++processing_template_decl;
20702 dependent = any_dependent_template_arguments_p (targs);
20703 if (!dependent)
20704 --processing_template_decl;
20705 parm = tsubst (pattern, targs,
20706 explain_p ? tf_warning_or_error : tf_none,
20707 NULL_TREE);
20708 if (dependent)
20709 --processing_template_decl;
20710 if (parm == error_mark_node)
20711 return 1;
20713 else
20714 parm = pattern;
20716 /* Unify the pattern with the current argument. */
20717 if (unify_one_argument (tparms, targs, parm, arg, subr, strict,
20718 explain_p))
20719 return 1;
20721 /* For each parameter pack, collect the deduced value. */
20722 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20724 int idx, level;
20725 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20727 TREE_VEC_ELT (TREE_TYPE (pack), i - start) =
20728 TMPL_ARG (targs, level, idx);
20732 /* Verify that the results of unification with the parameter packs
20733 produce results consistent with what we've seen before, and make
20734 the deduced argument packs available. */
20735 for (pack = packs; pack; pack = TREE_CHAIN (pack))
20737 tree old_pack = TREE_VALUE (pack);
20738 tree new_args = TREE_TYPE (pack);
20739 int i, len = TREE_VEC_LENGTH (new_args);
20740 int idx, level;
20741 bool nondeduced_p = false;
20743 /* By default keep the original deduced argument pack.
20744 If necessary, more specific code is going to update the
20745 resulting deduced argument later down in this function. */
20746 template_parm_level_and_index (TREE_PURPOSE (pack), &level, &idx);
20747 TMPL_ARG (targs, level, idx) = old_pack;
20749 /* If NEW_ARGS contains any NULL_TREE entries, we didn't
20750 actually deduce anything. */
20751 for (i = 0; i < len && !nondeduced_p; ++i)
20752 if (TREE_VEC_ELT (new_args, i) == NULL_TREE)
20753 nondeduced_p = true;
20754 if (nondeduced_p)
20755 continue;
20757 if (old_pack && ARGUMENT_PACK_INCOMPLETE_P (old_pack))
20759 /* If we had fewer function args than explicit template args,
20760 just use the explicits. */
20761 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20762 int explicit_len = TREE_VEC_LENGTH (explicit_args);
20763 if (len < explicit_len)
20764 new_args = explicit_args;
20767 if (!old_pack)
20769 tree result;
20770 /* Build the deduced *_ARGUMENT_PACK. */
20771 if (TREE_CODE (TREE_PURPOSE (pack)) == TEMPLATE_PARM_INDEX)
20773 result = make_node (NONTYPE_ARGUMENT_PACK);
20774 TREE_CONSTANT (result) = 1;
20776 else
20777 result = cxx_make_type (TYPE_ARGUMENT_PACK);
20779 SET_ARGUMENT_PACK_ARGS (result, new_args);
20781 /* Note the deduced argument packs for this parameter
20782 pack. */
20783 TMPL_ARG (targs, level, idx) = result;
20785 else if (ARGUMENT_PACK_INCOMPLETE_P (old_pack)
20786 && (ARGUMENT_PACK_ARGS (old_pack)
20787 == ARGUMENT_PACK_EXPLICIT_ARGS (old_pack)))
20789 /* We only had the explicitly-provided arguments before, but
20790 now we have a complete set of arguments. */
20791 tree explicit_args = ARGUMENT_PACK_EXPLICIT_ARGS (old_pack);
20793 SET_ARGUMENT_PACK_ARGS (old_pack, new_args);
20794 ARGUMENT_PACK_INCOMPLETE_P (old_pack) = 1;
20795 ARGUMENT_PACK_EXPLICIT_ARGS (old_pack) = explicit_args;
20797 else
20799 tree bad_old_arg = NULL_TREE, bad_new_arg = NULL_TREE;
20800 tree old_args = ARGUMENT_PACK_ARGS (old_pack);
20802 if (!comp_template_args (old_args, new_args,
20803 &bad_old_arg, &bad_new_arg))
20804 /* Inconsistent unification of this parameter pack. */
20805 return unify_parameter_pack_inconsistent (explain_p,
20806 bad_old_arg,
20807 bad_new_arg);
20811 return unify_success (explain_p);
20814 /* Handle unification of the domain of an array. PARM_DOM and ARG_DOM are
20815 INTEGER_TYPEs representing the TYPE_DOMAIN of ARRAY_TYPEs. The other
20816 parameters and return value are as for unify. */
20818 static int
20819 unify_array_domain (tree tparms, tree targs,
20820 tree parm_dom, tree arg_dom,
20821 bool explain_p)
20823 tree parm_max;
20824 tree arg_max;
20825 bool parm_cst;
20826 bool arg_cst;
20828 /* Our representation of array types uses "N - 1" as the
20829 TYPE_MAX_VALUE for an array with "N" elements, if "N" is
20830 not an integer constant. We cannot unify arbitrarily
20831 complex expressions, so we eliminate the MINUS_EXPRs
20832 here. */
20833 parm_max = TYPE_MAX_VALUE (parm_dom);
20834 parm_cst = TREE_CODE (parm_max) == INTEGER_CST;
20835 if (!parm_cst)
20837 gcc_assert (TREE_CODE (parm_max) == MINUS_EXPR);
20838 parm_max = TREE_OPERAND (parm_max, 0);
20840 arg_max = TYPE_MAX_VALUE (arg_dom);
20841 arg_cst = TREE_CODE (arg_max) == INTEGER_CST;
20842 if (!arg_cst)
20844 /* The ARG_MAX may not be a simple MINUS_EXPR, if we are
20845 trying to unify the type of a variable with the type
20846 of a template parameter. For example:
20848 template <unsigned int N>
20849 void f (char (&) [N]);
20850 int g();
20851 void h(int i) {
20852 char a[g(i)];
20853 f(a);
20856 Here, the type of the ARG will be "int [g(i)]", and
20857 may be a SAVE_EXPR, etc. */
20858 if (TREE_CODE (arg_max) != MINUS_EXPR)
20859 return unify_vla_arg (explain_p, arg_dom);
20860 arg_max = TREE_OPERAND (arg_max, 0);
20863 /* If only one of the bounds used a MINUS_EXPR, compensate
20864 by adding one to the other bound. */
20865 if (parm_cst && !arg_cst)
20866 parm_max = fold_build2_loc (input_location, PLUS_EXPR,
20867 integer_type_node,
20868 parm_max,
20869 integer_one_node);
20870 else if (arg_cst && !parm_cst)
20871 arg_max = fold_build2_loc (input_location, PLUS_EXPR,
20872 integer_type_node,
20873 arg_max,
20874 integer_one_node);
20876 return unify (tparms, targs, parm_max, arg_max,
20877 UNIFY_ALLOW_INTEGER, explain_p);
20880 /* Returns whether T, a P or A in unify, is a type, template or expression. */
20882 enum pa_kind_t { pa_type, pa_tmpl, pa_expr };
20884 static pa_kind_t
20885 pa_kind (tree t)
20887 if (PACK_EXPANSION_P (t))
20888 t = PACK_EXPANSION_PATTERN (t);
20889 if (TREE_CODE (t) == TEMPLATE_TEMPLATE_PARM
20890 || TREE_CODE (t) == UNBOUND_CLASS_TEMPLATE
20891 || DECL_TYPE_TEMPLATE_P (t))
20892 return pa_tmpl;
20893 else if (TYPE_P (t))
20894 return pa_type;
20895 else
20896 return pa_expr;
20899 /* Deduce the value of template parameters. TPARMS is the (innermost)
20900 set of template parameters to a template. TARGS is the bindings
20901 for those template parameters, as determined thus far; TARGS may
20902 include template arguments for outer levels of template parameters
20903 as well. PARM is a parameter to a template function, or a
20904 subcomponent of that parameter; ARG is the corresponding argument.
20905 This function attempts to match PARM with ARG in a manner
20906 consistent with the existing assignments in TARGS. If more values
20907 are deduced, then TARGS is updated.
20909 Returns 0 if the type deduction succeeds, 1 otherwise. The
20910 parameter STRICT is a bitwise or of the following flags:
20912 UNIFY_ALLOW_NONE:
20913 Require an exact match between PARM and ARG.
20914 UNIFY_ALLOW_MORE_CV_QUAL:
20915 Allow the deduced ARG to be more cv-qualified (by qualification
20916 conversion) than ARG.
20917 UNIFY_ALLOW_LESS_CV_QUAL:
20918 Allow the deduced ARG to be less cv-qualified than ARG.
20919 UNIFY_ALLOW_DERIVED:
20920 Allow the deduced ARG to be a template base class of ARG,
20921 or a pointer to a template base class of the type pointed to by
20922 ARG.
20923 UNIFY_ALLOW_INTEGER:
20924 Allow any integral type to be deduced. See the TEMPLATE_PARM_INDEX
20925 case for more information.
20926 UNIFY_ALLOW_OUTER_LEVEL:
20927 This is the outermost level of a deduction. Used to determine validity
20928 of qualification conversions. A valid qualification conversion must
20929 have const qualified pointers leading up to the inner type which
20930 requires additional CV quals, except at the outer level, where const
20931 is not required [conv.qual]. It would be normal to set this flag in
20932 addition to setting UNIFY_ALLOW_MORE_CV_QUAL.
20933 UNIFY_ALLOW_OUTER_MORE_CV_QUAL:
20934 This is the outermost level of a deduction, and PARM can be more CV
20935 qualified at this point.
20936 UNIFY_ALLOW_OUTER_LESS_CV_QUAL:
20937 This is the outermost level of a deduction, and PARM can be less CV
20938 qualified at this point. */
20940 static int
20941 unify (tree tparms, tree targs, tree parm, tree arg, int strict,
20942 bool explain_p)
20944 int idx;
20945 tree targ;
20946 tree tparm;
20947 int strict_in = strict;
20948 tsubst_flags_t complain = (explain_p
20949 ? tf_warning_or_error
20950 : tf_none);
20952 /* I don't think this will do the right thing with respect to types.
20953 But the only case I've seen it in so far has been array bounds, where
20954 signedness is the only information lost, and I think that will be
20955 okay. */
20956 while (CONVERT_EXPR_P (parm))
20957 parm = TREE_OPERAND (parm, 0);
20959 if (arg == error_mark_node)
20960 return unify_invalid (explain_p);
20961 if (arg == unknown_type_node
20962 || arg == init_list_type_node)
20963 /* We can't deduce anything from this, but we might get all the
20964 template args from other function args. */
20965 return unify_success (explain_p);
20967 if (parm == any_targ_node || arg == any_targ_node)
20968 return unify_success (explain_p);
20970 /* If PARM uses template parameters, then we can't bail out here,
20971 even if ARG == PARM, since we won't record unifications for the
20972 template parameters. We might need them if we're trying to
20973 figure out which of two things is more specialized. */
20974 if (arg == parm && !uses_template_parms (parm))
20975 return unify_success (explain_p);
20977 /* Handle init lists early, so the rest of the function can assume
20978 we're dealing with a type. */
20979 if (BRACE_ENCLOSED_INITIALIZER_P (arg))
20981 tree elt, elttype;
20982 unsigned i;
20983 tree orig_parm = parm;
20985 /* Replace T with std::initializer_list<T> for deduction. */
20986 if (TREE_CODE (parm) == TEMPLATE_TYPE_PARM
20987 && flag_deduce_init_list)
20988 parm = listify (parm);
20990 if (!is_std_init_list (parm)
20991 && TREE_CODE (parm) != ARRAY_TYPE)
20992 /* We can only deduce from an initializer list argument if the
20993 parameter is std::initializer_list or an array; otherwise this
20994 is a non-deduced context. */
20995 return unify_success (explain_p);
20997 if (TREE_CODE (parm) == ARRAY_TYPE)
20998 elttype = TREE_TYPE (parm);
20999 else
21001 elttype = TREE_VEC_ELT (CLASSTYPE_TI_ARGS (parm), 0);
21002 /* Deduction is defined in terms of a single type, so just punt
21003 on the (bizarre) std::initializer_list<T...>. */
21004 if (PACK_EXPANSION_P (elttype))
21005 return unify_success (explain_p);
21008 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg), i, elt)
21010 int elt_strict = strict;
21012 if (elt == error_mark_node)
21013 return unify_invalid (explain_p);
21015 if (!BRACE_ENCLOSED_INITIALIZER_P (elt))
21017 tree type = TREE_TYPE (elt);
21018 if (type == error_mark_node)
21019 return unify_invalid (explain_p);
21020 /* It should only be possible to get here for a call. */
21021 gcc_assert (elt_strict & UNIFY_ALLOW_OUTER_LEVEL);
21022 elt_strict |= maybe_adjust_types_for_deduction
21023 (DEDUCE_CALL, &elttype, &type, elt);
21024 elt = type;
21027 RECUR_AND_CHECK_FAILURE (tparms, targs, elttype, elt, elt_strict,
21028 explain_p);
21031 if (TREE_CODE (parm) == ARRAY_TYPE
21032 && deducible_array_bound (TYPE_DOMAIN (parm)))
21034 /* Also deduce from the length of the initializer list. */
21035 tree max = size_int (CONSTRUCTOR_NELTS (arg));
21036 tree idx = compute_array_index_type (NULL_TREE, max, tf_none);
21037 if (idx == error_mark_node)
21038 return unify_invalid (explain_p);
21039 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
21040 idx, explain_p);
21043 /* If the std::initializer_list<T> deduction worked, replace the
21044 deduced A with std::initializer_list<A>. */
21045 if (orig_parm != parm)
21047 idx = TEMPLATE_TYPE_IDX (orig_parm);
21048 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
21049 targ = listify (targ);
21050 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = targ;
21052 return unify_success (explain_p);
21055 /* If parm and arg aren't the same kind of thing (template, type, or
21056 expression), fail early. */
21057 if (pa_kind (parm) != pa_kind (arg))
21058 return unify_invalid (explain_p);
21060 /* Immediately reject some pairs that won't unify because of
21061 cv-qualification mismatches. */
21062 if (TREE_CODE (arg) == TREE_CODE (parm)
21063 && TYPE_P (arg)
21064 /* It is the elements of the array which hold the cv quals of an array
21065 type, and the elements might be template type parms. We'll check
21066 when we recurse. */
21067 && TREE_CODE (arg) != ARRAY_TYPE
21068 /* We check the cv-qualifiers when unifying with template type
21069 parameters below. We want to allow ARG `const T' to unify with
21070 PARM `T' for example, when computing which of two templates
21071 is more specialized, for example. */
21072 && TREE_CODE (arg) != TEMPLATE_TYPE_PARM
21073 && !check_cv_quals_for_unify (strict_in, arg, parm))
21074 return unify_cv_qual_mismatch (explain_p, parm, arg);
21076 if (!(strict & UNIFY_ALLOW_OUTER_LEVEL)
21077 && TYPE_P (parm) && !CP_TYPE_CONST_P (parm))
21078 strict &= ~UNIFY_ALLOW_MORE_CV_QUAL;
21079 strict &= ~UNIFY_ALLOW_OUTER_LEVEL;
21080 strict &= ~UNIFY_ALLOW_DERIVED;
21081 strict &= ~UNIFY_ALLOW_OUTER_MORE_CV_QUAL;
21082 strict &= ~UNIFY_ALLOW_OUTER_LESS_CV_QUAL;
21084 switch (TREE_CODE (parm))
21086 case TYPENAME_TYPE:
21087 case SCOPE_REF:
21088 case UNBOUND_CLASS_TEMPLATE:
21089 /* In a type which contains a nested-name-specifier, template
21090 argument values cannot be deduced for template parameters used
21091 within the nested-name-specifier. */
21092 return unify_success (explain_p);
21094 case TEMPLATE_TYPE_PARM:
21095 case TEMPLATE_TEMPLATE_PARM:
21096 case BOUND_TEMPLATE_TEMPLATE_PARM:
21097 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
21098 if (error_operand_p (tparm))
21099 return unify_invalid (explain_p);
21101 if (TEMPLATE_TYPE_LEVEL (parm)
21102 != template_decl_level (tparm))
21103 /* The PARM is not one we're trying to unify. Just check
21104 to see if it matches ARG. */
21106 if (TREE_CODE (arg) == TREE_CODE (parm)
21107 && (is_auto (parm) ? is_auto (arg)
21108 : same_type_p (parm, arg)))
21109 return unify_success (explain_p);
21110 else
21111 return unify_type_mismatch (explain_p, parm, arg);
21113 idx = TEMPLATE_TYPE_IDX (parm);
21114 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
21115 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, idx));
21116 if (error_operand_p (tparm))
21117 return unify_invalid (explain_p);
21119 /* Check for mixed types and values. */
21120 if ((TREE_CODE (parm) == TEMPLATE_TYPE_PARM
21121 && TREE_CODE (tparm) != TYPE_DECL)
21122 || (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
21123 && TREE_CODE (tparm) != TEMPLATE_DECL))
21124 gcc_unreachable ();
21126 if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21128 if ((strict_in & UNIFY_ALLOW_DERIVED)
21129 && CLASS_TYPE_P (arg))
21131 /* First try to match ARG directly. */
21132 tree t = try_class_unification (tparms, targs, parm, arg,
21133 explain_p);
21134 if (!t)
21136 /* Otherwise, look for a suitable base of ARG, as below. */
21137 enum template_base_result r;
21138 r = get_template_base (tparms, targs, parm, arg,
21139 explain_p, &t);
21140 if (!t)
21141 return unify_no_common_base (explain_p, r, parm, arg);
21142 arg = t;
21145 /* ARG must be constructed from a template class or a template
21146 template parameter. */
21147 else if (TREE_CODE (arg) != BOUND_TEMPLATE_TEMPLATE_PARM
21148 && !CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
21149 return unify_template_deduction_failure (explain_p, parm, arg);
21151 /* Deduce arguments T, i from TT<T> or TT<i>. */
21152 if (unify_bound_ttp_args (tparms, targs, parm, arg, explain_p))
21153 return 1;
21155 arg = TYPE_TI_TEMPLATE (arg);
21157 /* Fall through to deduce template name. */
21160 if (TREE_CODE (parm) == TEMPLATE_TEMPLATE_PARM
21161 || TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
21163 /* Deduce template name TT from TT, TT<>, TT<T> and TT<i>. */
21165 /* Simple cases: Value already set, does match or doesn't. */
21166 if (targ != NULL_TREE && template_args_equal (targ, arg))
21167 return unify_success (explain_p);
21168 else if (targ)
21169 return unify_inconsistency (explain_p, parm, targ, arg);
21171 else
21173 /* If PARM is `const T' and ARG is only `int', we don't have
21174 a match unless we are allowing additional qualification.
21175 If ARG is `const int' and PARM is just `T' that's OK;
21176 that binds `const int' to `T'. */
21177 if (!check_cv_quals_for_unify (strict_in | UNIFY_ALLOW_LESS_CV_QUAL,
21178 arg, parm))
21179 return unify_cv_qual_mismatch (explain_p, parm, arg);
21181 /* Consider the case where ARG is `const volatile int' and
21182 PARM is `const T'. Then, T should be `volatile int'. */
21183 arg = cp_build_qualified_type_real
21184 (arg, cp_type_quals (arg) & ~cp_type_quals (parm), tf_none);
21185 if (arg == error_mark_node)
21186 return unify_invalid (explain_p);
21188 /* Simple cases: Value already set, does match or doesn't. */
21189 if (targ != NULL_TREE && same_type_p (targ, arg))
21190 return unify_success (explain_p);
21191 else if (targ)
21192 return unify_inconsistency (explain_p, parm, targ, arg);
21194 /* Make sure that ARG is not a variable-sized array. (Note
21195 that were talking about variable-sized arrays (like
21196 `int[n]'), rather than arrays of unknown size (like
21197 `int[]').) We'll get very confused by such a type since
21198 the bound of the array is not constant, and therefore
21199 not mangleable. Besides, such types are not allowed in
21200 ISO C++, so we can do as we please here. We do allow
21201 them for 'auto' deduction, since that isn't ABI-exposed. */
21202 if (!is_auto (parm) && variably_modified_type_p (arg, NULL_TREE))
21203 return unify_vla_arg (explain_p, arg);
21205 /* Strip typedefs as in convert_template_argument. */
21206 arg = canonicalize_type_argument (arg, tf_none);
21209 /* If ARG is a parameter pack or an expansion, we cannot unify
21210 against it unless PARM is also a parameter pack. */
21211 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
21212 && !template_parameter_pack_p (parm))
21213 return unify_parameter_pack_mismatch (explain_p, parm, arg);
21215 /* If the argument deduction results is a METHOD_TYPE,
21216 then there is a problem.
21217 METHOD_TYPE doesn't map to any real C++ type the result of
21218 the deduction can not be of that type. */
21219 if (TREE_CODE (arg) == METHOD_TYPE)
21220 return unify_method_type_error (explain_p, arg);
21222 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
21223 return unify_success (explain_p);
21225 case TEMPLATE_PARM_INDEX:
21226 tparm = TREE_VALUE (TREE_VEC_ELT (tparms, 0));
21227 if (error_operand_p (tparm))
21228 return unify_invalid (explain_p);
21230 if (TEMPLATE_PARM_LEVEL (parm)
21231 != template_decl_level (tparm))
21233 /* The PARM is not one we're trying to unify. Just check
21234 to see if it matches ARG. */
21235 int result = !(TREE_CODE (arg) == TREE_CODE (parm)
21236 && cp_tree_equal (parm, arg));
21237 if (result)
21238 unify_expression_unequal (explain_p, parm, arg);
21239 return result;
21242 idx = TEMPLATE_PARM_IDX (parm);
21243 targ = TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx);
21245 if (targ)
21247 if ((strict & UNIFY_ALLOW_INTEGER)
21248 && TREE_TYPE (targ) && TREE_TYPE (arg)
21249 && CP_INTEGRAL_TYPE_P (TREE_TYPE (targ)))
21250 /* We're deducing from an array bound, the type doesn't matter. */
21251 arg = fold_convert (TREE_TYPE (targ), arg);
21252 int x = !cp_tree_equal (targ, arg);
21253 if (x)
21254 unify_inconsistency (explain_p, parm, targ, arg);
21255 return x;
21258 /* [temp.deduct.type] If, in the declaration of a function template
21259 with a non-type template-parameter, the non-type
21260 template-parameter is used in an expression in the function
21261 parameter-list and, if the corresponding template-argument is
21262 deduced, the template-argument type shall match the type of the
21263 template-parameter exactly, except that a template-argument
21264 deduced from an array bound may be of any integral type.
21265 The non-type parameter might use already deduced type parameters. */
21266 tparm = TREE_TYPE (parm);
21267 if (TEMPLATE_PARM_LEVEL (parm) > TMPL_ARGS_DEPTH (targs))
21268 /* We don't have enough levels of args to do any substitution. This
21269 can happen in the context of -fnew-ttp-matching. */;
21270 else
21272 ++processing_template_decl;
21273 tparm = tsubst (tparm, targs, tf_none, NULL_TREE);
21274 --processing_template_decl;
21276 if (tree a = type_uses_auto (tparm))
21278 tparm = do_auto_deduction (tparm, arg, a, complain, adc_unify);
21279 if (tparm == error_mark_node)
21280 return 1;
21284 if (!TREE_TYPE (arg))
21285 /* Template-parameter dependent expression. Just accept it for now.
21286 It will later be processed in convert_template_argument. */
21288 else if (same_type_p (non_reference (TREE_TYPE (arg)),
21289 non_reference (tparm)))
21290 /* OK */;
21291 else if ((strict & UNIFY_ALLOW_INTEGER)
21292 && CP_INTEGRAL_TYPE_P (tparm))
21293 /* Convert the ARG to the type of PARM; the deduced non-type
21294 template argument must exactly match the types of the
21295 corresponding parameter. */
21296 arg = fold (build_nop (tparm, arg));
21297 else if (uses_template_parms (tparm))
21299 /* We haven't deduced the type of this parameter yet. */
21300 if (cxx_dialect >= cxx17
21301 /* We deduce from array bounds in try_array_deduction. */
21302 && !(strict & UNIFY_ALLOW_INTEGER))
21304 /* Deduce it from the non-type argument. */
21305 tree atype = TREE_TYPE (arg);
21306 RECUR_AND_CHECK_FAILURE (tparms, targs,
21307 tparm, atype,
21308 UNIFY_ALLOW_NONE, explain_p);
21310 else
21311 /* Try again later. */
21312 return unify_success (explain_p);
21314 else
21315 return unify_type_mismatch (explain_p, tparm, TREE_TYPE (arg));
21317 /* If ARG is a parameter pack or an expansion, we cannot unify
21318 against it unless PARM is also a parameter pack. */
21319 if ((template_parameter_pack_p (arg) || PACK_EXPANSION_P (arg))
21320 && !TEMPLATE_PARM_PARAMETER_PACK (parm))
21321 return unify_parameter_pack_mismatch (explain_p, parm, arg);
21324 bool removed_attr = false;
21325 arg = strip_typedefs_expr (arg, &removed_attr);
21327 TREE_VEC_ELT (INNERMOST_TEMPLATE_ARGS (targs), idx) = arg;
21328 return unify_success (explain_p);
21330 case PTRMEM_CST:
21332 /* A pointer-to-member constant can be unified only with
21333 another constant. */
21334 if (TREE_CODE (arg) != PTRMEM_CST)
21335 return unify_ptrmem_cst_mismatch (explain_p, parm, arg);
21337 /* Just unify the class member. It would be useless (and possibly
21338 wrong, depending on the strict flags) to unify also
21339 PTRMEM_CST_CLASS, because we want to be sure that both parm and
21340 arg refer to the same variable, even if through different
21341 classes. For instance:
21343 struct A { int x; };
21344 struct B : A { };
21346 Unification of &A::x and &B::x must succeed. */
21347 return unify (tparms, targs, PTRMEM_CST_MEMBER (parm),
21348 PTRMEM_CST_MEMBER (arg), strict, explain_p);
21351 case POINTER_TYPE:
21353 if (!TYPE_PTR_P (arg))
21354 return unify_type_mismatch (explain_p, parm, arg);
21356 /* [temp.deduct.call]
21358 A can be another pointer or pointer to member type that can
21359 be converted to the deduced A via a qualification
21360 conversion (_conv.qual_).
21362 We pass down STRICT here rather than UNIFY_ALLOW_NONE.
21363 This will allow for additional cv-qualification of the
21364 pointed-to types if appropriate. */
21366 if (TREE_CODE (TREE_TYPE (arg)) == RECORD_TYPE)
21367 /* The derived-to-base conversion only persists through one
21368 level of pointers. */
21369 strict |= (strict_in & UNIFY_ALLOW_DERIVED);
21371 return unify (tparms, targs, TREE_TYPE (parm),
21372 TREE_TYPE (arg), strict, explain_p);
21375 case REFERENCE_TYPE:
21376 if (TREE_CODE (arg) != REFERENCE_TYPE)
21377 return unify_type_mismatch (explain_p, parm, arg);
21378 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21379 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21381 case ARRAY_TYPE:
21382 if (TREE_CODE (arg) != ARRAY_TYPE)
21383 return unify_type_mismatch (explain_p, parm, arg);
21384 if ((TYPE_DOMAIN (parm) == NULL_TREE)
21385 != (TYPE_DOMAIN (arg) == NULL_TREE))
21386 return unify_type_mismatch (explain_p, parm, arg);
21387 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21388 strict & UNIFY_ALLOW_MORE_CV_QUAL, explain_p);
21389 if (TYPE_DOMAIN (parm) != NULL_TREE)
21390 return unify_array_domain (tparms, targs, TYPE_DOMAIN (parm),
21391 TYPE_DOMAIN (arg), explain_p);
21392 return unify_success (explain_p);
21394 case REAL_TYPE:
21395 case COMPLEX_TYPE:
21396 case VECTOR_TYPE:
21397 case INTEGER_TYPE:
21398 case BOOLEAN_TYPE:
21399 case ENUMERAL_TYPE:
21400 case VOID_TYPE:
21401 case NULLPTR_TYPE:
21402 if (TREE_CODE (arg) != TREE_CODE (parm))
21403 return unify_type_mismatch (explain_p, parm, arg);
21405 /* We have already checked cv-qualification at the top of the
21406 function. */
21407 if (!same_type_ignoring_top_level_qualifiers_p (arg, parm))
21408 return unify_type_mismatch (explain_p, parm, arg);
21410 /* As far as unification is concerned, this wins. Later checks
21411 will invalidate it if necessary. */
21412 return unify_success (explain_p);
21414 /* Types INTEGER_CST and MINUS_EXPR can come from array bounds. */
21415 /* Type INTEGER_CST can come from ordinary constant template args. */
21416 case INTEGER_CST:
21417 while (CONVERT_EXPR_P (arg))
21418 arg = TREE_OPERAND (arg, 0);
21420 if (TREE_CODE (arg) != INTEGER_CST)
21421 return unify_template_argument_mismatch (explain_p, parm, arg);
21422 return (tree_int_cst_equal (parm, arg)
21423 ? unify_success (explain_p)
21424 : unify_template_argument_mismatch (explain_p, parm, arg));
21426 case TREE_VEC:
21428 int i, len, argslen;
21429 int parm_variadic_p = 0;
21431 if (TREE_CODE (arg) != TREE_VEC)
21432 return unify_template_argument_mismatch (explain_p, parm, arg);
21434 len = TREE_VEC_LENGTH (parm);
21435 argslen = TREE_VEC_LENGTH (arg);
21437 /* Check for pack expansions in the parameters. */
21438 for (i = 0; i < len; ++i)
21440 if (PACK_EXPANSION_P (TREE_VEC_ELT (parm, i)))
21442 if (i == len - 1)
21443 /* We can unify against something with a trailing
21444 parameter pack. */
21445 parm_variadic_p = 1;
21446 else
21447 /* [temp.deduct.type]/9: If the template argument list of
21448 P contains a pack expansion that is not the last
21449 template argument, the entire template argument list
21450 is a non-deduced context. */
21451 return unify_success (explain_p);
21455 /* If we don't have enough arguments to satisfy the parameters
21456 (not counting the pack expression at the end), or we have
21457 too many arguments for a parameter list that doesn't end in
21458 a pack expression, we can't unify. */
21459 if (parm_variadic_p
21460 ? argslen < len - parm_variadic_p
21461 : argslen != len)
21462 return unify_arity (explain_p, TREE_VEC_LENGTH (arg), len);
21464 /* Unify all of the parameters that precede the (optional)
21465 pack expression. */
21466 for (i = 0; i < len - parm_variadic_p; ++i)
21468 RECUR_AND_CHECK_FAILURE (tparms, targs,
21469 TREE_VEC_ELT (parm, i),
21470 TREE_VEC_ELT (arg, i),
21471 UNIFY_ALLOW_NONE, explain_p);
21473 if (parm_variadic_p)
21474 return unify_pack_expansion (tparms, targs, parm, arg,
21475 DEDUCE_EXACT,
21476 /*subr=*/true, explain_p);
21477 return unify_success (explain_p);
21480 case RECORD_TYPE:
21481 case UNION_TYPE:
21482 if (TREE_CODE (arg) != TREE_CODE (parm))
21483 return unify_type_mismatch (explain_p, parm, arg);
21485 if (TYPE_PTRMEMFUNC_P (parm))
21487 if (!TYPE_PTRMEMFUNC_P (arg))
21488 return unify_type_mismatch (explain_p, parm, arg);
21490 return unify (tparms, targs,
21491 TYPE_PTRMEMFUNC_FN_TYPE (parm),
21492 TYPE_PTRMEMFUNC_FN_TYPE (arg),
21493 strict, explain_p);
21495 else if (TYPE_PTRMEMFUNC_P (arg))
21496 return unify_type_mismatch (explain_p, parm, arg);
21498 if (CLASSTYPE_TEMPLATE_INFO (parm))
21500 tree t = NULL_TREE;
21502 if (strict_in & UNIFY_ALLOW_DERIVED)
21504 /* First, we try to unify the PARM and ARG directly. */
21505 t = try_class_unification (tparms, targs,
21506 parm, arg, explain_p);
21508 if (!t)
21510 /* Fallback to the special case allowed in
21511 [temp.deduct.call]:
21513 If P is a class, and P has the form
21514 template-id, then A can be a derived class of
21515 the deduced A. Likewise, if P is a pointer to
21516 a class of the form template-id, A can be a
21517 pointer to a derived class pointed to by the
21518 deduced A. */
21519 enum template_base_result r;
21520 r = get_template_base (tparms, targs, parm, arg,
21521 explain_p, &t);
21523 if (!t)
21525 /* Don't give the derived diagnostic if we're
21526 already dealing with the same template. */
21527 bool same_template
21528 = (CLASSTYPE_TEMPLATE_INFO (arg)
21529 && (CLASSTYPE_TI_TEMPLATE (parm)
21530 == CLASSTYPE_TI_TEMPLATE (arg)));
21531 return unify_no_common_base (explain_p && !same_template,
21532 r, parm, arg);
21536 else if (CLASSTYPE_TEMPLATE_INFO (arg)
21537 && (CLASSTYPE_TI_TEMPLATE (parm)
21538 == CLASSTYPE_TI_TEMPLATE (arg)))
21539 /* Perhaps PARM is something like S<U> and ARG is S<int>.
21540 Then, we should unify `int' and `U'. */
21541 t = arg;
21542 else
21543 /* There's no chance of unification succeeding. */
21544 return unify_type_mismatch (explain_p, parm, arg);
21546 return unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
21547 CLASSTYPE_TI_ARGS (t), UNIFY_ALLOW_NONE, explain_p);
21549 else if (!same_type_ignoring_top_level_qualifiers_p (parm, arg))
21550 return unify_type_mismatch (explain_p, parm, arg);
21551 return unify_success (explain_p);
21553 case METHOD_TYPE:
21554 case FUNCTION_TYPE:
21556 unsigned int nargs;
21557 tree *args;
21558 tree a;
21559 unsigned int i;
21561 if (TREE_CODE (arg) != TREE_CODE (parm))
21562 return unify_type_mismatch (explain_p, parm, arg);
21564 /* CV qualifications for methods can never be deduced, they must
21565 match exactly. We need to check them explicitly here,
21566 because type_unification_real treats them as any other
21567 cv-qualified parameter. */
21568 if (TREE_CODE (parm) == METHOD_TYPE
21569 && (!check_cv_quals_for_unify
21570 (UNIFY_ALLOW_NONE,
21571 class_of_this_parm (arg),
21572 class_of_this_parm (parm))))
21573 return unify_cv_qual_mismatch (explain_p, parm, arg);
21574 if (TREE_CODE (arg) == FUNCTION_TYPE
21575 && type_memfn_quals (parm) != type_memfn_quals (arg))
21576 return unify_cv_qual_mismatch (explain_p, parm, arg);
21577 if (type_memfn_rqual (parm) != type_memfn_rqual (arg))
21578 return unify_type_mismatch (explain_p, parm, arg);
21580 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_TYPE (parm),
21581 TREE_TYPE (arg), UNIFY_ALLOW_NONE, explain_p);
21583 nargs = list_length (TYPE_ARG_TYPES (arg));
21584 args = XALLOCAVEC (tree, nargs);
21585 for (a = TYPE_ARG_TYPES (arg), i = 0;
21586 a != NULL_TREE && a != void_list_node;
21587 a = TREE_CHAIN (a), ++i)
21588 args[i] = TREE_VALUE (a);
21589 nargs = i;
21591 if (type_unification_real (tparms, targs, TYPE_ARG_TYPES (parm),
21592 args, nargs, 1, DEDUCE_EXACT,
21593 LOOKUP_NORMAL, NULL, explain_p))
21594 return 1;
21596 if (flag_noexcept_type)
21598 tree pspec = TYPE_RAISES_EXCEPTIONS (parm);
21599 tree aspec = canonical_eh_spec (TYPE_RAISES_EXCEPTIONS (arg));
21600 if (pspec == NULL_TREE) pspec = noexcept_false_spec;
21601 if (aspec == NULL_TREE) aspec = noexcept_false_spec;
21602 if (TREE_PURPOSE (pspec) && TREE_PURPOSE (aspec)
21603 && uses_template_parms (TREE_PURPOSE (pspec)))
21604 RECUR_AND_CHECK_FAILURE (tparms, targs, TREE_PURPOSE (pspec),
21605 TREE_PURPOSE (aspec),
21606 UNIFY_ALLOW_NONE, explain_p);
21607 else if (nothrow_spec_p (pspec) && !nothrow_spec_p (aspec))
21608 return unify_type_mismatch (explain_p, parm, arg);
21611 return 0;
21614 case OFFSET_TYPE:
21615 /* Unify a pointer to member with a pointer to member function, which
21616 deduces the type of the member as a function type. */
21617 if (TYPE_PTRMEMFUNC_P (arg))
21619 /* Check top-level cv qualifiers */
21620 if (!check_cv_quals_for_unify (UNIFY_ALLOW_NONE, arg, parm))
21621 return unify_cv_qual_mismatch (explain_p, parm, arg);
21623 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21624 TYPE_PTRMEMFUNC_OBJECT_TYPE (arg),
21625 UNIFY_ALLOW_NONE, explain_p);
21627 /* Determine the type of the function we are unifying against. */
21628 tree fntype = static_fn_type (arg);
21630 return unify (tparms, targs, TREE_TYPE (parm), fntype, strict, explain_p);
21633 if (TREE_CODE (arg) != OFFSET_TYPE)
21634 return unify_type_mismatch (explain_p, parm, arg);
21635 RECUR_AND_CHECK_FAILURE (tparms, targs, TYPE_OFFSET_BASETYPE (parm),
21636 TYPE_OFFSET_BASETYPE (arg),
21637 UNIFY_ALLOW_NONE, explain_p);
21638 return unify (tparms, targs, TREE_TYPE (parm), TREE_TYPE (arg),
21639 strict, explain_p);
21641 case CONST_DECL:
21642 if (DECL_TEMPLATE_PARM_P (parm))
21643 return unify (tparms, targs, DECL_INITIAL (parm), arg, strict, explain_p);
21644 if (arg != scalar_constant_value (parm))
21645 return unify_template_argument_mismatch (explain_p, parm, arg);
21646 return unify_success (explain_p);
21648 case FIELD_DECL:
21649 case TEMPLATE_DECL:
21650 /* Matched cases are handled by the ARG == PARM test above. */
21651 return unify_template_argument_mismatch (explain_p, parm, arg);
21653 case VAR_DECL:
21654 /* We might get a variable as a non-type template argument in parm if the
21655 corresponding parameter is type-dependent. Make any necessary
21656 adjustments based on whether arg is a reference. */
21657 if (CONSTANT_CLASS_P (arg))
21658 parm = fold_non_dependent_expr (parm);
21659 else if (REFERENCE_REF_P (arg))
21661 tree sub = TREE_OPERAND (arg, 0);
21662 STRIP_NOPS (sub);
21663 if (TREE_CODE (sub) == ADDR_EXPR)
21664 arg = TREE_OPERAND (sub, 0);
21666 /* Now use the normal expression code to check whether they match. */
21667 goto expr;
21669 case TYPE_ARGUMENT_PACK:
21670 case NONTYPE_ARGUMENT_PACK:
21671 return unify (tparms, targs, ARGUMENT_PACK_ARGS (parm),
21672 ARGUMENT_PACK_ARGS (arg), strict, explain_p);
21674 case TYPEOF_TYPE:
21675 case DECLTYPE_TYPE:
21676 case UNDERLYING_TYPE:
21677 /* Cannot deduce anything from TYPEOF_TYPE, DECLTYPE_TYPE,
21678 or UNDERLYING_TYPE nodes. */
21679 return unify_success (explain_p);
21681 case ERROR_MARK:
21682 /* Unification fails if we hit an error node. */
21683 return unify_invalid (explain_p);
21685 case INDIRECT_REF:
21686 if (REFERENCE_REF_P (parm))
21688 bool pexp = PACK_EXPANSION_P (arg);
21689 if (pexp)
21690 arg = PACK_EXPANSION_PATTERN (arg);
21691 if (REFERENCE_REF_P (arg))
21692 arg = TREE_OPERAND (arg, 0);
21693 if (pexp)
21694 arg = make_pack_expansion (arg, complain);
21695 return unify (tparms, targs, TREE_OPERAND (parm, 0), arg,
21696 strict, explain_p);
21698 /* FALLTHRU */
21700 default:
21701 /* An unresolved overload is a nondeduced context. */
21702 if (is_overloaded_fn (parm) || type_unknown_p (parm))
21703 return unify_success (explain_p);
21704 gcc_assert (EXPR_P (parm) || TREE_CODE (parm) == TRAIT_EXPR);
21705 expr:
21706 /* We must be looking at an expression. This can happen with
21707 something like:
21709 template <int I>
21710 void foo(S<I>, S<I + 2>);
21712 This is a "nondeduced context":
21714 [deduct.type]
21716 The nondeduced contexts are:
21718 --A type that is a template-id in which one or more of
21719 the template-arguments is an expression that references
21720 a template-parameter.
21722 In these cases, we assume deduction succeeded, but don't
21723 actually infer any unifications. */
21725 if (!uses_template_parms (parm)
21726 && !template_args_equal (parm, arg))
21727 return unify_expression_unequal (explain_p, parm, arg);
21728 else
21729 return unify_success (explain_p);
21732 #undef RECUR_AND_CHECK_FAILURE
21734 /* Note that DECL can be defined in this translation unit, if
21735 required. */
21737 static void
21738 mark_definable (tree decl)
21740 tree clone;
21741 DECL_NOT_REALLY_EXTERN (decl) = 1;
21742 FOR_EACH_CLONE (clone, decl)
21743 DECL_NOT_REALLY_EXTERN (clone) = 1;
21746 /* Called if RESULT is explicitly instantiated, or is a member of an
21747 explicitly instantiated class. */
21749 void
21750 mark_decl_instantiated (tree result, int extern_p)
21752 SET_DECL_EXPLICIT_INSTANTIATION (result);
21754 /* If this entity has already been written out, it's too late to
21755 make any modifications. */
21756 if (TREE_ASM_WRITTEN (result))
21757 return;
21759 /* For anonymous namespace we don't need to do anything. */
21760 if (decl_anon_ns_mem_p (result))
21762 gcc_assert (!TREE_PUBLIC (result));
21763 return;
21766 if (TREE_CODE (result) != FUNCTION_DECL)
21767 /* The TREE_PUBLIC flag for function declarations will have been
21768 set correctly by tsubst. */
21769 TREE_PUBLIC (result) = 1;
21771 /* This might have been set by an earlier implicit instantiation. */
21772 DECL_COMDAT (result) = 0;
21774 if (extern_p)
21775 DECL_NOT_REALLY_EXTERN (result) = 0;
21776 else
21778 mark_definable (result);
21779 mark_needed (result);
21780 /* Always make artificials weak. */
21781 if (DECL_ARTIFICIAL (result) && flag_weak)
21782 comdat_linkage (result);
21783 /* For WIN32 we also want to put explicit instantiations in
21784 linkonce sections. */
21785 else if (TREE_PUBLIC (result))
21786 maybe_make_one_only (result);
21789 /* If EXTERN_P, then this function will not be emitted -- unless
21790 followed by an explicit instantiation, at which point its linkage
21791 will be adjusted. If !EXTERN_P, then this function will be
21792 emitted here. In neither circumstance do we want
21793 import_export_decl to adjust the linkage. */
21794 DECL_INTERFACE_KNOWN (result) = 1;
21797 /* Subroutine of more_specialized_fn: check whether TARGS is missing any
21798 important template arguments. If any are missing, we check whether
21799 they're important by using error_mark_node for substituting into any
21800 args that were used for partial ordering (the ones between ARGS and END)
21801 and seeing if it bubbles up. */
21803 static bool
21804 check_undeduced_parms (tree targs, tree args, tree end)
21806 bool found = false;
21807 int i;
21808 for (i = TREE_VEC_LENGTH (targs) - 1; i >= 0; --i)
21809 if (TREE_VEC_ELT (targs, i) == NULL_TREE)
21811 found = true;
21812 TREE_VEC_ELT (targs, i) = error_mark_node;
21814 if (found)
21816 tree substed = tsubst_arg_types (args, targs, end, tf_none, NULL_TREE);
21817 if (substed == error_mark_node)
21818 return true;
21820 return false;
21823 /* Given two function templates PAT1 and PAT2, return:
21825 1 if PAT1 is more specialized than PAT2 as described in [temp.func.order].
21826 -1 if PAT2 is more specialized than PAT1.
21827 0 if neither is more specialized.
21829 LEN indicates the number of parameters we should consider
21830 (defaulted parameters should not be considered).
21832 The 1998 std underspecified function template partial ordering, and
21833 DR214 addresses the issue. We take pairs of arguments, one from
21834 each of the templates, and deduce them against each other. One of
21835 the templates will be more specialized if all the *other*
21836 template's arguments deduce against its arguments and at least one
21837 of its arguments *does* *not* deduce against the other template's
21838 corresponding argument. Deduction is done as for class templates.
21839 The arguments used in deduction have reference and top level cv
21840 qualifiers removed. Iff both arguments were originally reference
21841 types *and* deduction succeeds in both directions, an lvalue reference
21842 wins against an rvalue reference and otherwise the template
21843 with the more cv-qualified argument wins for that pairing (if
21844 neither is more cv-qualified, they both are equal). Unlike regular
21845 deduction, after all the arguments have been deduced in this way,
21846 we do *not* verify the deduced template argument values can be
21847 substituted into non-deduced contexts.
21849 The logic can be a bit confusing here, because we look at deduce1 and
21850 targs1 to see if pat2 is at least as specialized, and vice versa; if we
21851 can find template arguments for pat1 to make arg1 look like arg2, that
21852 means that arg2 is at least as specialized as arg1. */
21855 more_specialized_fn (tree pat1, tree pat2, int len)
21857 tree decl1 = DECL_TEMPLATE_RESULT (pat1);
21858 tree decl2 = DECL_TEMPLATE_RESULT (pat2);
21859 tree targs1 = make_tree_vec (DECL_NTPARMS (pat1));
21860 tree targs2 = make_tree_vec (DECL_NTPARMS (pat2));
21861 tree tparms1 = DECL_INNERMOST_TEMPLATE_PARMS (pat1);
21862 tree tparms2 = DECL_INNERMOST_TEMPLATE_PARMS (pat2);
21863 tree args1 = TYPE_ARG_TYPES (TREE_TYPE (decl1));
21864 tree args2 = TYPE_ARG_TYPES (TREE_TYPE (decl2));
21865 tree origs1, origs2;
21866 bool lose1 = false;
21867 bool lose2 = false;
21869 /* Remove the this parameter from non-static member functions. If
21870 one is a non-static member function and the other is not a static
21871 member function, remove the first parameter from that function
21872 also. This situation occurs for operator functions where we
21873 locate both a member function (with this pointer) and non-member
21874 operator (with explicit first operand). */
21875 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl1))
21877 len--; /* LEN is the number of significant arguments for DECL1 */
21878 args1 = TREE_CHAIN (args1);
21879 if (!DECL_STATIC_FUNCTION_P (decl2))
21880 args2 = TREE_CHAIN (args2);
21882 else if (DECL_NONSTATIC_MEMBER_FUNCTION_P (decl2))
21884 args2 = TREE_CHAIN (args2);
21885 if (!DECL_STATIC_FUNCTION_P (decl1))
21887 len--;
21888 args1 = TREE_CHAIN (args1);
21892 /* If only one is a conversion operator, they are unordered. */
21893 if (DECL_CONV_FN_P (decl1) != DECL_CONV_FN_P (decl2))
21894 return 0;
21896 /* Consider the return type for a conversion function */
21897 if (DECL_CONV_FN_P (decl1))
21899 args1 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl1)), args1);
21900 args2 = tree_cons (NULL_TREE, TREE_TYPE (TREE_TYPE (decl2)), args2);
21901 len++;
21904 processing_template_decl++;
21906 origs1 = args1;
21907 origs2 = args2;
21909 while (len--
21910 /* Stop when an ellipsis is seen. */
21911 && args1 != NULL_TREE && args2 != NULL_TREE)
21913 tree arg1 = TREE_VALUE (args1);
21914 tree arg2 = TREE_VALUE (args2);
21915 int deduce1, deduce2;
21916 int quals1 = -1;
21917 int quals2 = -1;
21918 int ref1 = 0;
21919 int ref2 = 0;
21921 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
21922 && TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21924 /* When both arguments are pack expansions, we need only
21925 unify the patterns themselves. */
21926 arg1 = PACK_EXPANSION_PATTERN (arg1);
21927 arg2 = PACK_EXPANSION_PATTERN (arg2);
21929 /* This is the last comparison we need to do. */
21930 len = 0;
21933 /* DR 1847: If a particular P contains no template-parameters that
21934 participate in template argument deduction, that P is not used to
21935 determine the ordering. */
21936 if (!uses_deducible_template_parms (arg1)
21937 && !uses_deducible_template_parms (arg2))
21938 goto next;
21940 if (TREE_CODE (arg1) == REFERENCE_TYPE)
21942 ref1 = TYPE_REF_IS_RVALUE (arg1) + 1;
21943 arg1 = TREE_TYPE (arg1);
21944 quals1 = cp_type_quals (arg1);
21947 if (TREE_CODE (arg2) == REFERENCE_TYPE)
21949 ref2 = TYPE_REF_IS_RVALUE (arg2) + 1;
21950 arg2 = TREE_TYPE (arg2);
21951 quals2 = cp_type_quals (arg2);
21954 arg1 = TYPE_MAIN_VARIANT (arg1);
21955 arg2 = TYPE_MAIN_VARIANT (arg2);
21957 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION)
21959 int i, len2 = remaining_arguments (args2);
21960 tree parmvec = make_tree_vec (1);
21961 tree argvec = make_tree_vec (len2);
21962 tree ta = args2;
21964 /* Setup the parameter vector, which contains only ARG1. */
21965 TREE_VEC_ELT (parmvec, 0) = arg1;
21967 /* Setup the argument vector, which contains the remaining
21968 arguments. */
21969 for (i = 0; i < len2; i++, ta = TREE_CHAIN (ta))
21970 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21972 deduce1 = (unify_pack_expansion (tparms1, targs1, parmvec,
21973 argvec, DEDUCE_EXACT,
21974 /*subr=*/true, /*explain_p=*/false)
21975 == 0);
21977 /* We cannot deduce in the other direction, because ARG1 is
21978 a pack expansion but ARG2 is not. */
21979 deduce2 = 0;
21981 else if (TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
21983 int i, len1 = remaining_arguments (args1);
21984 tree parmvec = make_tree_vec (1);
21985 tree argvec = make_tree_vec (len1);
21986 tree ta = args1;
21988 /* Setup the parameter vector, which contains only ARG1. */
21989 TREE_VEC_ELT (parmvec, 0) = arg2;
21991 /* Setup the argument vector, which contains the remaining
21992 arguments. */
21993 for (i = 0; i < len1; i++, ta = TREE_CHAIN (ta))
21994 TREE_VEC_ELT (argvec, i) = TREE_VALUE (ta);
21996 deduce2 = (unify_pack_expansion (tparms2, targs2, parmvec,
21997 argvec, DEDUCE_EXACT,
21998 /*subr=*/true, /*explain_p=*/false)
21999 == 0);
22001 /* We cannot deduce in the other direction, because ARG2 is
22002 a pack expansion but ARG1 is not.*/
22003 deduce1 = 0;
22006 else
22008 /* The normal case, where neither argument is a pack
22009 expansion. */
22010 deduce1 = (unify (tparms1, targs1, arg1, arg2,
22011 UNIFY_ALLOW_NONE, /*explain_p=*/false)
22012 == 0);
22013 deduce2 = (unify (tparms2, targs2, arg2, arg1,
22014 UNIFY_ALLOW_NONE, /*explain_p=*/false)
22015 == 0);
22018 /* If we couldn't deduce arguments for tparms1 to make arg1 match
22019 arg2, then arg2 is not as specialized as arg1. */
22020 if (!deduce1)
22021 lose2 = true;
22022 if (!deduce2)
22023 lose1 = true;
22025 /* "If, for a given type, deduction succeeds in both directions
22026 (i.e., the types are identical after the transformations above)
22027 and both P and A were reference types (before being replaced with
22028 the type referred to above):
22029 - if the type from the argument template was an lvalue reference and
22030 the type from the parameter template was not, the argument type is
22031 considered to be more specialized than the other; otherwise,
22032 - if the type from the argument template is more cv-qualified
22033 than the type from the parameter template (as described above),
22034 the argument type is considered to be more specialized than the other;
22035 otherwise,
22036 - neither type is more specialized than the other." */
22038 if (deduce1 && deduce2)
22040 if (ref1 && ref2 && ref1 != ref2)
22042 if (ref1 > ref2)
22043 lose1 = true;
22044 else
22045 lose2 = true;
22047 else if (quals1 != quals2 && quals1 >= 0 && quals2 >= 0)
22049 if ((quals1 & quals2) == quals2)
22050 lose2 = true;
22051 if ((quals1 & quals2) == quals1)
22052 lose1 = true;
22056 if (lose1 && lose2)
22057 /* We've failed to deduce something in either direction.
22058 These must be unordered. */
22059 break;
22061 next:
22063 if (TREE_CODE (arg1) == TYPE_PACK_EXPANSION
22064 || TREE_CODE (arg2) == TYPE_PACK_EXPANSION)
22065 /* We have already processed all of the arguments in our
22066 handing of the pack expansion type. */
22067 len = 0;
22069 args1 = TREE_CHAIN (args1);
22070 args2 = TREE_CHAIN (args2);
22073 /* "In most cases, all template parameters must have values in order for
22074 deduction to succeed, but for partial ordering purposes a template
22075 parameter may remain without a value provided it is not used in the
22076 types being used for partial ordering."
22078 Thus, if we are missing any of the targs1 we need to substitute into
22079 origs1, then pat2 is not as specialized as pat1. This can happen when
22080 there is a nondeduced context. */
22081 if (!lose2 && check_undeduced_parms (targs1, origs1, args1))
22082 lose2 = true;
22083 if (!lose1 && check_undeduced_parms (targs2, origs2, args2))
22084 lose1 = true;
22086 processing_template_decl--;
22088 /* If both deductions succeed, the partial ordering selects the more
22089 constrained template. */
22090 if (!lose1 && !lose2)
22092 tree c1 = get_constraints (DECL_TEMPLATE_RESULT (pat1));
22093 tree c2 = get_constraints (DECL_TEMPLATE_RESULT (pat2));
22094 lose1 = !subsumes_constraints (c1, c2);
22095 lose2 = !subsumes_constraints (c2, c1);
22098 /* All things being equal, if the next argument is a pack expansion
22099 for one function but not for the other, prefer the
22100 non-variadic function. FIXME this is bogus; see c++/41958. */
22101 if (lose1 == lose2
22102 && args1 && TREE_VALUE (args1)
22103 && args2 && TREE_VALUE (args2))
22105 lose1 = TREE_CODE (TREE_VALUE (args1)) == TYPE_PACK_EXPANSION;
22106 lose2 = TREE_CODE (TREE_VALUE (args2)) == TYPE_PACK_EXPANSION;
22109 if (lose1 == lose2)
22110 return 0;
22111 else if (!lose1)
22112 return 1;
22113 else
22114 return -1;
22117 /* Determine which of two partial specializations of TMPL is more
22118 specialized.
22120 PAT1 is a TREE_LIST whose TREE_VALUE is the TEMPLATE_DECL corresponding
22121 to the first partial specialization. The TREE_PURPOSE is the
22122 innermost set of template parameters for the partial
22123 specialization. PAT2 is similar, but for the second template.
22125 Return 1 if the first partial specialization is more specialized;
22126 -1 if the second is more specialized; 0 if neither is more
22127 specialized.
22129 See [temp.class.order] for information about determining which of
22130 two templates is more specialized. */
22132 static int
22133 more_specialized_partial_spec (tree tmpl, tree pat1, tree pat2)
22135 tree targs;
22136 int winner = 0;
22137 bool any_deductions = false;
22139 tree tmpl1 = TREE_VALUE (pat1);
22140 tree tmpl2 = TREE_VALUE (pat2);
22141 tree specargs1 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl1)));
22142 tree specargs2 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (tmpl2)));
22144 /* Just like what happens for functions, if we are ordering between
22145 different template specializations, we may encounter dependent
22146 types in the arguments, and we need our dependency check functions
22147 to behave correctly. */
22148 ++processing_template_decl;
22149 targs = get_partial_spec_bindings (tmpl, tmpl1, specargs2);
22150 if (targs)
22152 --winner;
22153 any_deductions = true;
22156 targs = get_partial_spec_bindings (tmpl, tmpl2, specargs1);
22157 if (targs)
22159 ++winner;
22160 any_deductions = true;
22162 --processing_template_decl;
22164 /* If both deductions succeed, the partial ordering selects the more
22165 constrained template. */
22166 if (!winner && any_deductions)
22167 return more_constrained (tmpl1, tmpl2);
22169 /* In the case of a tie where at least one of the templates
22170 has a parameter pack at the end, the template with the most
22171 non-packed parameters wins. */
22172 if (winner == 0
22173 && any_deductions
22174 && (template_args_variadic_p (TREE_PURPOSE (pat1))
22175 || template_args_variadic_p (TREE_PURPOSE (pat2))))
22177 tree args1 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat1));
22178 tree args2 = INNERMOST_TEMPLATE_ARGS (TREE_PURPOSE (pat2));
22179 int len1 = TREE_VEC_LENGTH (args1);
22180 int len2 = TREE_VEC_LENGTH (args2);
22182 /* We don't count the pack expansion at the end. */
22183 if (template_args_variadic_p (TREE_PURPOSE (pat1)))
22184 --len1;
22185 if (template_args_variadic_p (TREE_PURPOSE (pat2)))
22186 --len2;
22188 if (len1 > len2)
22189 return 1;
22190 else if (len1 < len2)
22191 return -1;
22194 return winner;
22197 /* Return the template arguments that will produce the function signature
22198 DECL from the function template FN, with the explicit template
22199 arguments EXPLICIT_ARGS. If CHECK_RETTYPE is true, the return type must
22200 also match. Return NULL_TREE if no satisfactory arguments could be
22201 found. */
22203 static tree
22204 get_bindings (tree fn, tree decl, tree explicit_args, bool check_rettype)
22206 int ntparms = DECL_NTPARMS (fn);
22207 tree targs = make_tree_vec (ntparms);
22208 tree decl_type = TREE_TYPE (decl);
22209 tree decl_arg_types;
22210 tree *args;
22211 unsigned int nargs, ix;
22212 tree arg;
22214 gcc_assert (decl != DECL_TEMPLATE_RESULT (fn));
22216 /* Never do unification on the 'this' parameter. */
22217 decl_arg_types = skip_artificial_parms_for (decl,
22218 TYPE_ARG_TYPES (decl_type));
22220 nargs = list_length (decl_arg_types);
22221 args = XALLOCAVEC (tree, nargs);
22222 for (arg = decl_arg_types, ix = 0;
22223 arg != NULL_TREE && arg != void_list_node;
22224 arg = TREE_CHAIN (arg), ++ix)
22225 args[ix] = TREE_VALUE (arg);
22227 if (fn_type_unification (fn, explicit_args, targs,
22228 args, ix,
22229 (check_rettype || DECL_CONV_FN_P (fn)
22230 ? TREE_TYPE (decl_type) : NULL_TREE),
22231 DEDUCE_EXACT, LOOKUP_NORMAL, /*explain_p=*/false,
22232 /*decltype*/false)
22233 == error_mark_node)
22234 return NULL_TREE;
22236 return targs;
22239 /* Return the innermost template arguments that, when applied to a partial
22240 specialization SPEC_TMPL of TMPL, yield the ARGS.
22242 For example, suppose we have:
22244 template <class T, class U> struct S {};
22245 template <class T> struct S<T*, int> {};
22247 Then, suppose we want to get `S<double*, int>'. SPEC_TMPL will be the
22248 partial specialization and the ARGS will be {double*, int}. The resulting
22249 vector will be {double}, indicating that `T' is bound to `double'. */
22251 static tree
22252 get_partial_spec_bindings (tree tmpl, tree spec_tmpl, tree args)
22254 tree tparms = DECL_INNERMOST_TEMPLATE_PARMS (spec_tmpl);
22255 tree spec_args
22256 = TI_ARGS (get_template_info (DECL_TEMPLATE_RESULT (spec_tmpl)));
22257 int i, ntparms = TREE_VEC_LENGTH (tparms);
22258 tree deduced_args;
22259 tree innermost_deduced_args;
22261 innermost_deduced_args = make_tree_vec (ntparms);
22262 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
22264 deduced_args = copy_node (args);
22265 SET_TMPL_ARGS_LEVEL (deduced_args,
22266 TMPL_ARGS_DEPTH (deduced_args),
22267 innermost_deduced_args);
22269 else
22270 deduced_args = innermost_deduced_args;
22272 bool tried_array_deduction = (cxx_dialect < cxx17);
22273 again:
22274 if (unify (tparms, deduced_args,
22275 INNERMOST_TEMPLATE_ARGS (spec_args),
22276 INNERMOST_TEMPLATE_ARGS (args),
22277 UNIFY_ALLOW_NONE, /*explain_p=*/false))
22278 return NULL_TREE;
22280 for (i = 0; i < ntparms; ++i)
22281 if (! TREE_VEC_ELT (innermost_deduced_args, i))
22283 if (!tried_array_deduction)
22285 try_array_deduction (tparms, innermost_deduced_args,
22286 INNERMOST_TEMPLATE_ARGS (spec_args));
22287 tried_array_deduction = true;
22288 if (TREE_VEC_ELT (innermost_deduced_args, i))
22289 goto again;
22291 return NULL_TREE;
22294 tree tinst = build_tree_list (spec_tmpl, deduced_args);
22295 if (!push_tinst_level (tinst))
22297 excessive_deduction_depth = true;
22298 return NULL_TREE;
22301 /* Verify that nondeduced template arguments agree with the type
22302 obtained from argument deduction.
22304 For example:
22306 struct A { typedef int X; };
22307 template <class T, class U> struct C {};
22308 template <class T> struct C<T, typename T::X> {};
22310 Then with the instantiation `C<A, int>', we can deduce that
22311 `T' is `A' but unify () does not check whether `typename T::X'
22312 is `int'. */
22313 spec_args = tsubst (spec_args, deduced_args, tf_none, NULL_TREE);
22315 if (spec_args != error_mark_node)
22316 spec_args = coerce_template_parms (DECL_INNERMOST_TEMPLATE_PARMS (tmpl),
22317 INNERMOST_TEMPLATE_ARGS (spec_args),
22318 tmpl, tf_none, false, false);
22320 pop_tinst_level ();
22322 if (spec_args == error_mark_node
22323 /* We only need to check the innermost arguments; the other
22324 arguments will always agree. */
22325 || !comp_template_args_porder (INNERMOST_TEMPLATE_ARGS (spec_args),
22326 INNERMOST_TEMPLATE_ARGS (args)))
22327 return NULL_TREE;
22329 /* Now that we have bindings for all of the template arguments,
22330 ensure that the arguments deduced for the template template
22331 parameters have compatible template parameter lists. See the use
22332 of template_template_parm_bindings_ok_p in fn_type_unification
22333 for more information. */
22334 if (!template_template_parm_bindings_ok_p (tparms, deduced_args))
22335 return NULL_TREE;
22337 return deduced_args;
22340 // Compare two function templates T1 and T2 by deducing bindings
22341 // from one against the other. If both deductions succeed, compare
22342 // constraints to see which is more constrained.
22343 static int
22344 more_specialized_inst (tree t1, tree t2)
22346 int fate = 0;
22347 int count = 0;
22349 if (get_bindings (t1, DECL_TEMPLATE_RESULT (t2), NULL_TREE, true))
22351 --fate;
22352 ++count;
22355 if (get_bindings (t2, DECL_TEMPLATE_RESULT (t1), NULL_TREE, true))
22357 ++fate;
22358 ++count;
22361 // If both deductions succeed, then one may be more constrained.
22362 if (count == 2 && fate == 0)
22363 fate = more_constrained (t1, t2);
22365 return fate;
22368 /* TEMPLATES is a TREE_LIST. Each TREE_VALUE is a TEMPLATE_DECL.
22369 Return the TREE_LIST node with the most specialized template, if
22370 any. If there is no most specialized template, the error_mark_node
22371 is returned.
22373 Note that this function does not look at, or modify, the
22374 TREE_PURPOSE or TREE_TYPE of any of the nodes. Since the node
22375 returned is one of the elements of INSTANTIATIONS, callers may
22376 store information in the TREE_PURPOSE or TREE_TYPE of the nodes,
22377 and retrieve it from the value returned. */
22379 tree
22380 most_specialized_instantiation (tree templates)
22382 tree fn, champ;
22384 ++processing_template_decl;
22386 champ = templates;
22387 for (fn = TREE_CHAIN (templates); fn; fn = TREE_CHAIN (fn))
22389 gcc_assert (TREE_VALUE (champ) != TREE_VALUE (fn));
22390 int fate = more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn));
22391 if (fate == -1)
22392 champ = fn;
22393 else if (!fate)
22395 /* Equally specialized, move to next function. If there
22396 is no next function, nothing's most specialized. */
22397 fn = TREE_CHAIN (fn);
22398 champ = fn;
22399 if (!fn)
22400 break;
22404 if (champ)
22405 /* Now verify that champ is better than everything earlier in the
22406 instantiation list. */
22407 for (fn = templates; fn != champ; fn = TREE_CHAIN (fn)) {
22408 if (more_specialized_inst (TREE_VALUE (champ), TREE_VALUE (fn)) != 1)
22410 champ = NULL_TREE;
22411 break;
22415 processing_template_decl--;
22417 if (!champ)
22418 return error_mark_node;
22420 return champ;
22423 /* If DECL is a specialization of some template, return the most
22424 general such template. Otherwise, returns NULL_TREE.
22426 For example, given:
22428 template <class T> struct S { template <class U> void f(U); };
22430 if TMPL is `template <class U> void S<int>::f(U)' this will return
22431 the full template. This function will not trace past partial
22432 specializations, however. For example, given in addition:
22434 template <class T> struct S<T*> { template <class U> void f(U); };
22436 if TMPL is `template <class U> void S<int*>::f(U)' this will return
22437 `template <class T> template <class U> S<T*>::f(U)'. */
22439 tree
22440 most_general_template (tree decl)
22442 if (TREE_CODE (decl) != TEMPLATE_DECL)
22444 if (tree tinfo = get_template_info (decl))
22445 decl = TI_TEMPLATE (tinfo);
22446 /* The TI_TEMPLATE can be an IDENTIFIER_NODE for a
22447 template friend, or a FIELD_DECL for a capture pack. */
22448 if (TREE_CODE (decl) != TEMPLATE_DECL)
22449 return NULL_TREE;
22452 /* Look for more and more general templates. */
22453 while (DECL_LANG_SPECIFIC (decl) && DECL_TEMPLATE_INFO (decl))
22455 /* The DECL_TI_TEMPLATE can be an IDENTIFIER_NODE in some cases.
22456 (See cp-tree.h for details.) */
22457 if (TREE_CODE (DECL_TI_TEMPLATE (decl)) != TEMPLATE_DECL)
22458 break;
22460 if (CLASS_TYPE_P (TREE_TYPE (decl))
22461 && !TYPE_DECL_ALIAS_P (TYPE_NAME (TREE_TYPE (decl)))
22462 && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
22463 break;
22465 /* Stop if we run into an explicitly specialized class template. */
22466 if (!DECL_NAMESPACE_SCOPE_P (decl)
22467 && DECL_CONTEXT (decl)
22468 && CLASSTYPE_TEMPLATE_SPECIALIZATION (DECL_CONTEXT (decl)))
22469 break;
22471 decl = DECL_TI_TEMPLATE (decl);
22474 return decl;
22477 /* Return the most specialized of the template partial specializations
22478 which can produce TARGET, a specialization of some class or variable
22479 template. The value returned is actually a TREE_LIST; the TREE_VALUE is
22480 a TEMPLATE_DECL node corresponding to the partial specialization, while
22481 the TREE_PURPOSE is the set of template arguments that must be
22482 substituted into the template pattern in order to generate TARGET.
22484 If the choice of partial specialization is ambiguous, a diagnostic
22485 is issued, and the error_mark_node is returned. If there are no
22486 partial specializations matching TARGET, then NULL_TREE is
22487 returned, indicating that the primary template should be used. */
22489 static tree
22490 most_specialized_partial_spec (tree target, tsubst_flags_t complain)
22492 tree list = NULL_TREE;
22493 tree t;
22494 tree champ;
22495 int fate;
22496 bool ambiguous_p;
22497 tree outer_args = NULL_TREE;
22498 tree tmpl, args;
22500 if (TYPE_P (target))
22502 tree tinfo = CLASSTYPE_TEMPLATE_INFO (target);
22503 tmpl = TI_TEMPLATE (tinfo);
22504 args = TI_ARGS (tinfo);
22506 else if (TREE_CODE (target) == TEMPLATE_ID_EXPR)
22508 tmpl = TREE_OPERAND (target, 0);
22509 args = TREE_OPERAND (target, 1);
22511 else if (VAR_P (target))
22513 tree tinfo = DECL_TEMPLATE_INFO (target);
22514 tmpl = TI_TEMPLATE (tinfo);
22515 args = TI_ARGS (tinfo);
22517 else
22518 gcc_unreachable ();
22520 tree main_tmpl = most_general_template (tmpl);
22522 /* For determining which partial specialization to use, only the
22523 innermost args are interesting. */
22524 if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (args))
22526 outer_args = strip_innermost_template_args (args, 1);
22527 args = INNERMOST_TEMPLATE_ARGS (args);
22530 for (t = DECL_TEMPLATE_SPECIALIZATIONS (main_tmpl); t; t = TREE_CHAIN (t))
22532 tree spec_args;
22533 tree spec_tmpl = TREE_VALUE (t);
22535 if (outer_args)
22537 /* Substitute in the template args from the enclosing class. */
22538 ++processing_template_decl;
22539 spec_tmpl = tsubst (spec_tmpl, outer_args, tf_none, NULL_TREE);
22540 --processing_template_decl;
22543 if (spec_tmpl == error_mark_node)
22544 return error_mark_node;
22546 spec_args = get_partial_spec_bindings (tmpl, spec_tmpl, args);
22547 if (spec_args)
22549 if (outer_args)
22550 spec_args = add_to_template_args (outer_args, spec_args);
22552 /* Keep the candidate only if the constraints are satisfied,
22553 or if we're not compiling with concepts. */
22554 if (!flag_concepts
22555 || constraints_satisfied_p (spec_tmpl, spec_args))
22557 list = tree_cons (spec_args, TREE_VALUE (t), list);
22558 TREE_TYPE (list) = TREE_TYPE (t);
22563 if (! list)
22564 return NULL_TREE;
22566 ambiguous_p = false;
22567 t = list;
22568 champ = t;
22569 t = TREE_CHAIN (t);
22570 for (; t; t = TREE_CHAIN (t))
22572 fate = more_specialized_partial_spec (tmpl, champ, t);
22573 if (fate == 1)
22575 else
22577 if (fate == 0)
22579 t = TREE_CHAIN (t);
22580 if (! t)
22582 ambiguous_p = true;
22583 break;
22586 champ = t;
22590 if (!ambiguous_p)
22591 for (t = list; t && t != champ; t = TREE_CHAIN (t))
22593 fate = more_specialized_partial_spec (tmpl, champ, t);
22594 if (fate != 1)
22596 ambiguous_p = true;
22597 break;
22601 if (ambiguous_p)
22603 const char *str;
22604 char *spaces = NULL;
22605 if (!(complain & tf_error))
22606 return error_mark_node;
22607 if (TYPE_P (target))
22608 error ("ambiguous template instantiation for %q#T", target);
22609 else
22610 error ("ambiguous template instantiation for %q#D", target);
22611 str = ngettext ("candidate is:", "candidates are:", list_length (list));
22612 for (t = list; t; t = TREE_CHAIN (t))
22614 tree subst = build_tree_list (TREE_VALUE (t), TREE_PURPOSE (t));
22615 inform (DECL_SOURCE_LOCATION (TREE_VALUE (t)),
22616 "%s %#qS", spaces ? spaces : str, subst);
22617 spaces = spaces ? spaces : get_spaces (str);
22619 free (spaces);
22620 return error_mark_node;
22623 return champ;
22626 /* Explicitly instantiate DECL. */
22628 void
22629 do_decl_instantiation (tree decl, tree storage)
22631 tree result = NULL_TREE;
22632 int extern_p = 0;
22634 if (!decl || decl == error_mark_node)
22635 /* An error occurred, for which grokdeclarator has already issued
22636 an appropriate message. */
22637 return;
22638 else if (! DECL_LANG_SPECIFIC (decl))
22640 error ("explicit instantiation of non-template %q#D", decl);
22641 return;
22644 bool var_templ = (DECL_TEMPLATE_INFO (decl)
22645 && variable_template_p (DECL_TI_TEMPLATE (decl)));
22647 if (VAR_P (decl) && !var_templ)
22649 /* There is an asymmetry here in the way VAR_DECLs and
22650 FUNCTION_DECLs are handled by grokdeclarator. In the case of
22651 the latter, the DECL we get back will be marked as a
22652 template instantiation, and the appropriate
22653 DECL_TEMPLATE_INFO will be set up. This does not happen for
22654 VAR_DECLs so we do the lookup here. Probably, grokdeclarator
22655 should handle VAR_DECLs as it currently handles
22656 FUNCTION_DECLs. */
22657 if (!DECL_CLASS_SCOPE_P (decl))
22659 error ("%qD is not a static data member of a class template", decl);
22660 return;
22662 result = lookup_field (DECL_CONTEXT (decl), DECL_NAME (decl), 0, false);
22663 if (!result || !VAR_P (result))
22665 error ("no matching template for %qD found", decl);
22666 return;
22668 if (!same_type_p (TREE_TYPE (result), TREE_TYPE (decl)))
22670 error ("type %qT for explicit instantiation %qD does not match "
22671 "declared type %qT", TREE_TYPE (result), decl,
22672 TREE_TYPE (decl));
22673 return;
22676 else if (TREE_CODE (decl) != FUNCTION_DECL && !var_templ)
22678 error ("explicit instantiation of %q#D", decl);
22679 return;
22681 else
22682 result = decl;
22684 /* Check for various error cases. Note that if the explicit
22685 instantiation is valid the RESULT will currently be marked as an
22686 *implicit* instantiation; DECL_EXPLICIT_INSTANTIATION is not set
22687 until we get here. */
22689 if (DECL_TEMPLATE_SPECIALIZATION (result))
22691 /* DR 259 [temp.spec].
22693 Both an explicit instantiation and a declaration of an explicit
22694 specialization shall not appear in a program unless the explicit
22695 instantiation follows a declaration of the explicit specialization.
22697 For a given set of template parameters, if an explicit
22698 instantiation of a template appears after a declaration of an
22699 explicit specialization for that template, the explicit
22700 instantiation has no effect. */
22701 return;
22703 else if (DECL_EXPLICIT_INSTANTIATION (result))
22705 /* [temp.spec]
22707 No program shall explicitly instantiate any template more
22708 than once.
22710 We check DECL_NOT_REALLY_EXTERN so as not to complain when
22711 the first instantiation was `extern' and the second is not,
22712 and EXTERN_P for the opposite case. */
22713 if (DECL_NOT_REALLY_EXTERN (result) && !extern_p)
22714 permerror (input_location, "duplicate explicit instantiation of %q#D", result);
22715 /* If an "extern" explicit instantiation follows an ordinary
22716 explicit instantiation, the template is instantiated. */
22717 if (extern_p)
22718 return;
22720 else if (!DECL_IMPLICIT_INSTANTIATION (result))
22722 error ("no matching template for %qD found", result);
22723 return;
22725 else if (!DECL_TEMPLATE_INFO (result))
22727 permerror (input_location, "explicit instantiation of non-template %q#D", result);
22728 return;
22731 if (storage == NULL_TREE)
22733 else if (storage == ridpointers[(int) RID_EXTERN])
22735 if (!in_system_header_at (input_location) && (cxx_dialect == cxx98))
22736 pedwarn (input_location, OPT_Wpedantic,
22737 "ISO C++ 1998 forbids the use of %<extern%> on explicit "
22738 "instantiations");
22739 extern_p = 1;
22741 else
22742 error ("storage class %qD applied to template instantiation", storage);
22744 check_explicit_instantiation_namespace (result);
22745 mark_decl_instantiated (result, extern_p);
22746 if (! extern_p)
22747 instantiate_decl (result, /*defer_ok=*/true,
22748 /*expl_inst_class_mem_p=*/false);
22751 static void
22752 mark_class_instantiated (tree t, int extern_p)
22754 SET_CLASSTYPE_EXPLICIT_INSTANTIATION (t);
22755 SET_CLASSTYPE_INTERFACE_KNOWN (t);
22756 CLASSTYPE_INTERFACE_ONLY (t) = extern_p;
22757 TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (t)) = extern_p;
22758 if (! extern_p)
22760 CLASSTYPE_DEBUG_REQUESTED (t) = 1;
22761 rest_of_type_compilation (t, 1);
22765 /* Called from do_type_instantiation through binding_table_foreach to
22766 do recursive instantiation for the type bound in ENTRY. */
22767 static void
22768 bt_instantiate_type_proc (binding_entry entry, void *data)
22770 tree storage = *(tree *) data;
22772 if (MAYBE_CLASS_TYPE_P (entry->type)
22773 && !uses_template_parms (CLASSTYPE_TI_ARGS (entry->type)))
22774 do_type_instantiation (TYPE_MAIN_DECL (entry->type), storage, 0);
22777 /* Perform an explicit instantiation of template class T. STORAGE, if
22778 non-null, is the RID for extern, inline or static. COMPLAIN is
22779 nonzero if this is called from the parser, zero if called recursively,
22780 since the standard is unclear (as detailed below). */
22782 void
22783 do_type_instantiation (tree t, tree storage, tsubst_flags_t complain)
22785 int extern_p = 0;
22786 int nomem_p = 0;
22787 int static_p = 0;
22788 int previous_instantiation_extern_p = 0;
22790 if (TREE_CODE (t) == TYPE_DECL)
22791 t = TREE_TYPE (t);
22793 if (! CLASS_TYPE_P (t) || ! CLASSTYPE_TEMPLATE_INFO (t))
22795 tree tmpl =
22796 (TYPE_TEMPLATE_INFO (t)) ? TYPE_TI_TEMPLATE (t) : NULL;
22797 if (tmpl)
22798 error ("explicit instantiation of non-class template %qD", tmpl);
22799 else
22800 error ("explicit instantiation of non-template type %qT", t);
22801 return;
22804 complete_type (t);
22806 if (!COMPLETE_TYPE_P (t))
22808 if (complain & tf_error)
22809 error ("explicit instantiation of %q#T before definition of template",
22811 return;
22814 if (storage != NULL_TREE)
22816 if (!in_system_header_at (input_location))
22818 if (storage == ridpointers[(int) RID_EXTERN])
22820 if (cxx_dialect == cxx98)
22821 pedwarn (input_location, OPT_Wpedantic,
22822 "ISO C++ 1998 forbids the use of %<extern%> on "
22823 "explicit instantiations");
22825 else
22826 pedwarn (input_location, OPT_Wpedantic,
22827 "ISO C++ forbids the use of %qE"
22828 " on explicit instantiations", storage);
22831 if (storage == ridpointers[(int) RID_INLINE])
22832 nomem_p = 1;
22833 else if (storage == ridpointers[(int) RID_EXTERN])
22834 extern_p = 1;
22835 else if (storage == ridpointers[(int) RID_STATIC])
22836 static_p = 1;
22837 else
22839 error ("storage class %qD applied to template instantiation",
22840 storage);
22841 extern_p = 0;
22845 if (CLASSTYPE_TEMPLATE_SPECIALIZATION (t))
22847 /* DR 259 [temp.spec].
22849 Both an explicit instantiation and a declaration of an explicit
22850 specialization shall not appear in a program unless the explicit
22851 instantiation follows a declaration of the explicit specialization.
22853 For a given set of template parameters, if an explicit
22854 instantiation of a template appears after a declaration of an
22855 explicit specialization for that template, the explicit
22856 instantiation has no effect. */
22857 return;
22859 else if (CLASSTYPE_EXPLICIT_INSTANTIATION (t))
22861 /* [temp.spec]
22863 No program shall explicitly instantiate any template more
22864 than once.
22866 If PREVIOUS_INSTANTIATION_EXTERN_P, then the first explicit
22867 instantiation was `extern'. If EXTERN_P then the second is.
22868 These cases are OK. */
22869 previous_instantiation_extern_p = CLASSTYPE_INTERFACE_ONLY (t);
22871 if (!previous_instantiation_extern_p && !extern_p
22872 && (complain & tf_error))
22873 permerror (input_location, "duplicate explicit instantiation of %q#T", t);
22875 /* If we've already instantiated the template, just return now. */
22876 if (!CLASSTYPE_INTERFACE_ONLY (t))
22877 return;
22880 check_explicit_instantiation_namespace (TYPE_NAME (t));
22881 mark_class_instantiated (t, extern_p);
22883 if (nomem_p)
22884 return;
22886 /* In contrast to implicit instantiation, where only the
22887 declarations, and not the definitions, of members are
22888 instantiated, we have here:
22890 [temp.explicit]
22892 The explicit instantiation of a class template specialization
22893 implies the instantiation of all of its members not
22894 previously explicitly specialized in the translation unit
22895 containing the explicit instantiation.
22897 Of course, we can't instantiate member template classes, since we
22898 don't have any arguments for them. Note that the standard is
22899 unclear on whether the instantiation of the members are
22900 *explicit* instantiations or not. However, the most natural
22901 interpretation is that it should be an explicit
22902 instantiation. */
22903 for (tree fld = TYPE_FIELDS (t); fld; fld = DECL_CHAIN (fld))
22904 if ((VAR_P (fld)
22905 || (TREE_CODE (fld) == FUNCTION_DECL
22906 && !static_p
22907 && user_provided_p (fld)))
22908 && DECL_TEMPLATE_INSTANTIATION (fld))
22910 mark_decl_instantiated (fld, extern_p);
22911 if (! extern_p)
22912 instantiate_decl (fld, /*defer_ok=*/true,
22913 /*expl_inst_class_mem_p=*/true);
22916 if (CLASSTYPE_NESTED_UTDS (t))
22917 binding_table_foreach (CLASSTYPE_NESTED_UTDS (t),
22918 bt_instantiate_type_proc, &storage);
22921 /* Given a function DECL, which is a specialization of TMPL, modify
22922 DECL to be a re-instantiation of TMPL with the same template
22923 arguments. TMPL should be the template into which tsubst'ing
22924 should occur for DECL, not the most general template.
22926 One reason for doing this is a scenario like this:
22928 template <class T>
22929 void f(const T&, int i);
22931 void g() { f(3, 7); }
22933 template <class T>
22934 void f(const T& t, const int i) { }
22936 Note that when the template is first instantiated, with
22937 instantiate_template, the resulting DECL will have no name for the
22938 first parameter, and the wrong type for the second. So, when we go
22939 to instantiate the DECL, we regenerate it. */
22941 static void
22942 regenerate_decl_from_template (tree decl, tree tmpl, tree args)
22944 /* The arguments used to instantiate DECL, from the most general
22945 template. */
22946 tree code_pattern;
22948 code_pattern = DECL_TEMPLATE_RESULT (tmpl);
22950 /* Make sure that we can see identifiers, and compute access
22951 correctly. */
22952 push_access_scope (decl);
22954 if (TREE_CODE (decl) == FUNCTION_DECL)
22956 tree decl_parm;
22957 tree pattern_parm;
22958 tree specs;
22959 int args_depth;
22960 int parms_depth;
22962 args_depth = TMPL_ARGS_DEPTH (args);
22963 parms_depth = TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (tmpl));
22964 if (args_depth > parms_depth)
22965 args = get_innermost_template_args (args, parms_depth);
22967 specs = tsubst_exception_specification (TREE_TYPE (code_pattern),
22968 args, tf_error, NULL_TREE,
22969 /*defer_ok*/false);
22970 if (specs && specs != error_mark_node)
22971 TREE_TYPE (decl) = build_exception_variant (TREE_TYPE (decl),
22972 specs);
22974 /* Merge parameter declarations. */
22975 decl_parm = skip_artificial_parms_for (decl,
22976 DECL_ARGUMENTS (decl));
22977 pattern_parm
22978 = skip_artificial_parms_for (code_pattern,
22979 DECL_ARGUMENTS (code_pattern));
22980 while (decl_parm && !DECL_PACK_P (pattern_parm))
22982 tree parm_type;
22983 tree attributes;
22985 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
22986 DECL_NAME (decl_parm) = DECL_NAME (pattern_parm);
22987 parm_type = tsubst (TREE_TYPE (pattern_parm), args, tf_error,
22988 NULL_TREE);
22989 parm_type = type_decays_to (parm_type);
22990 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
22991 TREE_TYPE (decl_parm) = parm_type;
22992 attributes = DECL_ATTRIBUTES (pattern_parm);
22993 if (DECL_ATTRIBUTES (decl_parm) != attributes)
22995 DECL_ATTRIBUTES (decl_parm) = attributes;
22996 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
22998 decl_parm = DECL_CHAIN (decl_parm);
22999 pattern_parm = DECL_CHAIN (pattern_parm);
23001 /* Merge any parameters that match with the function parameter
23002 pack. */
23003 if (pattern_parm && DECL_PACK_P (pattern_parm))
23005 int i, len;
23006 tree expanded_types;
23007 /* Expand the TYPE_PACK_EXPANSION that provides the types for
23008 the parameters in this function parameter pack. */
23009 expanded_types = tsubst_pack_expansion (TREE_TYPE (pattern_parm),
23010 args, tf_error, NULL_TREE);
23011 len = TREE_VEC_LENGTH (expanded_types);
23012 for (i = 0; i < len; i++)
23014 tree parm_type;
23015 tree attributes;
23017 if (DECL_NAME (decl_parm) != DECL_NAME (pattern_parm))
23018 /* Rename the parameter to include the index. */
23019 DECL_NAME (decl_parm) =
23020 make_ith_pack_parameter_name (DECL_NAME (pattern_parm), i);
23021 parm_type = TREE_VEC_ELT (expanded_types, i);
23022 parm_type = type_decays_to (parm_type);
23023 if (!same_type_p (TREE_TYPE (decl_parm), parm_type))
23024 TREE_TYPE (decl_parm) = parm_type;
23025 attributes = DECL_ATTRIBUTES (pattern_parm);
23026 if (DECL_ATTRIBUTES (decl_parm) != attributes)
23028 DECL_ATTRIBUTES (decl_parm) = attributes;
23029 cplus_decl_attributes (&decl_parm, attributes, /*flags=*/0);
23031 decl_parm = DECL_CHAIN (decl_parm);
23034 /* Merge additional specifiers from the CODE_PATTERN. */
23035 if (DECL_DECLARED_INLINE_P (code_pattern)
23036 && !DECL_DECLARED_INLINE_P (decl))
23037 DECL_DECLARED_INLINE_P (decl) = 1;
23039 else if (VAR_P (decl))
23041 start_lambda_scope (decl);
23042 DECL_INITIAL (decl) =
23043 tsubst_expr (DECL_INITIAL (code_pattern), args,
23044 tf_error, DECL_TI_TEMPLATE (decl),
23045 /*integral_constant_expression_p=*/false);
23046 finish_lambda_scope ();
23047 if (VAR_HAD_UNKNOWN_BOUND (decl))
23048 TREE_TYPE (decl) = tsubst (TREE_TYPE (code_pattern), args,
23049 tf_error, DECL_TI_TEMPLATE (decl));
23051 else
23052 gcc_unreachable ();
23054 pop_access_scope (decl);
23057 /* Return the TEMPLATE_DECL into which DECL_TI_ARGS(DECL) should be
23058 substituted to get DECL. */
23060 tree
23061 template_for_substitution (tree decl)
23063 tree tmpl = DECL_TI_TEMPLATE (decl);
23065 /* Set TMPL to the template whose DECL_TEMPLATE_RESULT is the pattern
23066 for the instantiation. This is not always the most general
23067 template. Consider, for example:
23069 template <class T>
23070 struct S { template <class U> void f();
23071 template <> void f<int>(); };
23073 and an instantiation of S<double>::f<int>. We want TD to be the
23074 specialization S<T>::f<int>, not the more general S<T>::f<U>. */
23075 while (/* An instantiation cannot have a definition, so we need a
23076 more general template. */
23077 DECL_TEMPLATE_INSTANTIATION (tmpl)
23078 /* We must also deal with friend templates. Given:
23080 template <class T> struct S {
23081 template <class U> friend void f() {};
23084 S<int>::f<U> say, is not an instantiation of S<T>::f<U>,
23085 so far as the language is concerned, but that's still
23086 where we get the pattern for the instantiation from. On
23087 other hand, if the definition comes outside the class, say:
23089 template <class T> struct S {
23090 template <class U> friend void f();
23092 template <class U> friend void f() {}
23094 we don't need to look any further. That's what the check for
23095 DECL_INITIAL is for. */
23096 || (TREE_CODE (decl) == FUNCTION_DECL
23097 && DECL_FRIEND_PSEUDO_TEMPLATE_INSTANTIATION (tmpl)
23098 && !DECL_INITIAL (DECL_TEMPLATE_RESULT (tmpl))))
23100 /* The present template, TD, should not be a definition. If it
23101 were a definition, we should be using it! Note that we
23102 cannot restructure the loop to just keep going until we find
23103 a template with a definition, since that might go too far if
23104 a specialization was declared, but not defined. */
23106 /* Fetch the more general template. */
23107 tmpl = DECL_TI_TEMPLATE (tmpl);
23110 return tmpl;
23113 /* Returns true if we need to instantiate this template instance even if we
23114 know we aren't going to emit it. */
23116 bool
23117 always_instantiate_p (tree decl)
23119 /* We always instantiate inline functions so that we can inline them. An
23120 explicit instantiation declaration prohibits implicit instantiation of
23121 non-inline functions. With high levels of optimization, we would
23122 normally inline non-inline functions -- but we're not allowed to do
23123 that for "extern template" functions. Therefore, we check
23124 DECL_DECLARED_INLINE_P, rather than possibly_inlined_p. */
23125 return ((TREE_CODE (decl) == FUNCTION_DECL
23126 && (DECL_DECLARED_INLINE_P (decl)
23127 || type_uses_auto (TREE_TYPE (TREE_TYPE (decl)))))
23128 /* And we need to instantiate static data members so that
23129 their initializers are available in integral constant
23130 expressions. */
23131 || (VAR_P (decl)
23132 && decl_maybe_constant_var_p (decl)));
23135 /* If FN has a noexcept-specifier that hasn't been instantiated yet,
23136 instantiate it now, modifying TREE_TYPE (fn). Returns false on
23137 error, true otherwise. */
23139 bool
23140 maybe_instantiate_noexcept (tree fn, tsubst_flags_t complain)
23142 tree fntype, spec, noex, clone;
23144 /* Don't instantiate a noexcept-specification from template context. */
23145 if (processing_template_decl)
23146 return true;
23148 if (DECL_CLONED_FUNCTION_P (fn))
23149 fn = DECL_CLONED_FUNCTION (fn);
23150 fntype = TREE_TYPE (fn);
23151 spec = TYPE_RAISES_EXCEPTIONS (fntype);
23153 if (!spec || !TREE_PURPOSE (spec))
23154 return true;
23156 noex = TREE_PURPOSE (spec);
23158 if (TREE_CODE (noex) == DEFERRED_NOEXCEPT)
23160 static hash_set<tree>* fns = new hash_set<tree>;
23161 bool added = false;
23162 if (DEFERRED_NOEXCEPT_PATTERN (noex) == NULL_TREE)
23163 spec = get_defaulted_eh_spec (fn, complain);
23164 else if (!(added = !fns->add (fn)))
23166 /* If hash_set::add returns true, the element was already there. */
23167 location_t loc = EXPR_LOC_OR_LOC (DEFERRED_NOEXCEPT_PATTERN (noex),
23168 DECL_SOURCE_LOCATION (fn));
23169 error_at (loc,
23170 "exception specification of %qD depends on itself",
23171 fn);
23172 spec = noexcept_false_spec;
23174 else if (push_tinst_level (fn))
23176 push_access_scope (fn);
23177 push_deferring_access_checks (dk_no_deferred);
23178 input_location = DECL_SOURCE_LOCATION (fn);
23179 noex = tsubst_copy_and_build (DEFERRED_NOEXCEPT_PATTERN (noex),
23180 DEFERRED_NOEXCEPT_ARGS (noex),
23181 tf_warning_or_error, fn,
23182 /*function_p=*/false,
23183 /*integral_constant_expression_p=*/true);
23184 pop_deferring_access_checks ();
23185 pop_access_scope (fn);
23186 pop_tinst_level ();
23187 spec = build_noexcept_spec (noex, tf_warning_or_error);
23188 if (spec == error_mark_node)
23189 spec = noexcept_false_spec;
23191 else
23192 spec = noexcept_false_spec;
23194 if (added)
23195 fns->remove (fn);
23197 if (spec == error_mark_node)
23198 return false;
23200 TREE_TYPE (fn) = build_exception_variant (fntype, spec);
23203 FOR_EACH_CLONE (clone, fn)
23205 if (TREE_TYPE (clone) == fntype)
23206 TREE_TYPE (clone) = TREE_TYPE (fn);
23207 else
23208 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone), spec);
23211 return true;
23214 /* We're starting to process the function INST, an instantiation of PATTERN;
23215 add their parameters to local_specializations. */
23217 static void
23218 register_parameter_specializations (tree pattern, tree inst)
23220 tree tmpl_parm = DECL_ARGUMENTS (pattern);
23221 tree spec_parm = DECL_ARGUMENTS (inst);
23222 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (inst))
23224 register_local_specialization (spec_parm, tmpl_parm);
23225 spec_parm = skip_artificial_parms_for (inst, spec_parm);
23226 tmpl_parm = skip_artificial_parms_for (pattern, tmpl_parm);
23228 for (; tmpl_parm; tmpl_parm = DECL_CHAIN (tmpl_parm))
23230 if (!DECL_PACK_P (tmpl_parm))
23232 register_local_specialization (spec_parm, tmpl_parm);
23233 spec_parm = DECL_CHAIN (spec_parm);
23235 else
23237 /* Register the (value) argument pack as a specialization of
23238 TMPL_PARM, then move on. */
23239 tree argpack = extract_fnparm_pack (tmpl_parm, &spec_parm);
23240 register_local_specialization (argpack, tmpl_parm);
23243 gcc_assert (!spec_parm);
23246 /* Produce the definition of D, a _DECL generated from a template. If
23247 DEFER_OK is true, then we don't have to actually do the
23248 instantiation now; we just have to do it sometime. Normally it is
23249 an error if this is an explicit instantiation but D is undefined.
23250 EXPL_INST_CLASS_MEM_P is true iff D is a member of an explicitly
23251 instantiated class template. */
23253 tree
23254 instantiate_decl (tree d, bool defer_ok, bool expl_inst_class_mem_p)
23256 tree tmpl = DECL_TI_TEMPLATE (d);
23257 tree gen_args;
23258 tree args;
23259 tree td;
23260 tree code_pattern;
23261 tree spec;
23262 tree gen_tmpl;
23263 bool pattern_defined;
23264 location_t saved_loc = input_location;
23265 int saved_unevaluated_operand = cp_unevaluated_operand;
23266 int saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
23267 bool external_p;
23268 bool deleted_p;
23270 /* This function should only be used to instantiate templates for
23271 functions and static member variables. */
23272 gcc_assert (VAR_OR_FUNCTION_DECL_P (d));
23274 /* A concept is never instantiated. */
23275 gcc_assert (!DECL_DECLARED_CONCEPT_P (d));
23277 /* Variables are never deferred; if instantiation is required, they
23278 are instantiated right away. That allows for better code in the
23279 case that an expression refers to the value of the variable --
23280 if the variable has a constant value the referring expression can
23281 take advantage of that fact. */
23282 if (VAR_P (d))
23283 defer_ok = false;
23285 /* Don't instantiate cloned functions. Instead, instantiate the
23286 functions they cloned. */
23287 if (TREE_CODE (d) == FUNCTION_DECL && DECL_CLONED_FUNCTION_P (d))
23288 d = DECL_CLONED_FUNCTION (d);
23290 if (DECL_TEMPLATE_INSTANTIATED (d)
23291 || (TREE_CODE (d) == FUNCTION_DECL
23292 && DECL_DEFAULTED_FN (d) && DECL_INITIAL (d))
23293 || DECL_TEMPLATE_SPECIALIZATION (d))
23294 /* D has already been instantiated or explicitly specialized, so
23295 there's nothing for us to do here.
23297 It might seem reasonable to check whether or not D is an explicit
23298 instantiation, and, if so, stop here. But when an explicit
23299 instantiation is deferred until the end of the compilation,
23300 DECL_EXPLICIT_INSTANTIATION is set, even though we still need to do
23301 the instantiation. */
23302 return d;
23304 /* Check to see whether we know that this template will be
23305 instantiated in some other file, as with "extern template"
23306 extension. */
23307 external_p = (DECL_INTERFACE_KNOWN (d) && DECL_REALLY_EXTERN (d));
23309 /* In general, we do not instantiate such templates. */
23310 if (external_p && !always_instantiate_p (d))
23311 return d;
23313 gen_tmpl = most_general_template (tmpl);
23314 gen_args = DECL_TI_ARGS (d);
23316 if (tmpl != gen_tmpl)
23317 /* We should already have the extra args. */
23318 gcc_assert (TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (gen_tmpl))
23319 == TMPL_ARGS_DEPTH (gen_args));
23320 /* And what's in the hash table should match D. */
23321 gcc_assert ((spec = retrieve_specialization (gen_tmpl, gen_args, 0)) == d
23322 || spec == NULL_TREE);
23324 /* This needs to happen before any tsubsting. */
23325 if (! push_tinst_level (d))
23326 return d;
23328 timevar_push (TV_TEMPLATE_INST);
23330 /* Set TD to the template whose DECL_TEMPLATE_RESULT is the pattern
23331 for the instantiation. */
23332 td = template_for_substitution (d);
23333 args = gen_args;
23335 if (VAR_P (d))
23337 /* Look up an explicit specialization, if any. */
23338 tree tid = lookup_template_variable (gen_tmpl, gen_args);
23339 tree elt = most_specialized_partial_spec (tid, tf_warning_or_error);
23340 if (elt && elt != error_mark_node)
23342 td = TREE_VALUE (elt);
23343 args = TREE_PURPOSE (elt);
23347 code_pattern = DECL_TEMPLATE_RESULT (td);
23349 /* We should never be trying to instantiate a member of a class
23350 template or partial specialization. */
23351 gcc_assert (d != code_pattern);
23353 if ((DECL_NAMESPACE_SCOPE_P (d) && !DECL_INITIALIZED_IN_CLASS_P (d))
23354 || DECL_TEMPLATE_SPECIALIZATION (td))
23355 /* In the case of a friend template whose definition is provided
23356 outside the class, we may have too many arguments. Drop the
23357 ones we don't need. The same is true for specializations. */
23358 args = get_innermost_template_args
23359 (args, TMPL_PARMS_DEPTH (DECL_TEMPLATE_PARMS (td)));
23361 if (TREE_CODE (d) == FUNCTION_DECL)
23363 deleted_p = DECL_DELETED_FN (code_pattern);
23364 pattern_defined = ((DECL_SAVED_TREE (code_pattern) != NULL_TREE
23365 && DECL_INITIAL (code_pattern) != error_mark_node)
23366 || DECL_DEFAULTED_OUTSIDE_CLASS_P (code_pattern)
23367 || deleted_p);
23369 else
23371 deleted_p = false;
23372 if (DECL_CLASS_SCOPE_P (code_pattern))
23373 pattern_defined = (! DECL_IN_AGGR_P (code_pattern)
23374 || DECL_INLINE_VAR_P (code_pattern));
23375 else
23376 pattern_defined = ! DECL_EXTERNAL (code_pattern);
23379 /* We may be in the middle of deferred access check. Disable it now. */
23380 push_deferring_access_checks (dk_no_deferred);
23382 /* Unless an explicit instantiation directive has already determined
23383 the linkage of D, remember that a definition is available for
23384 this entity. */
23385 if (pattern_defined
23386 && !DECL_INTERFACE_KNOWN (d)
23387 && !DECL_NOT_REALLY_EXTERN (d))
23388 mark_definable (d);
23390 DECL_SOURCE_LOCATION (td) = DECL_SOURCE_LOCATION (code_pattern);
23391 DECL_SOURCE_LOCATION (d) = DECL_SOURCE_LOCATION (code_pattern);
23392 input_location = DECL_SOURCE_LOCATION (d);
23394 /* If D is a member of an explicitly instantiated class template,
23395 and no definition is available, treat it like an implicit
23396 instantiation. */
23397 if (!pattern_defined && expl_inst_class_mem_p
23398 && DECL_EXPLICIT_INSTANTIATION (d))
23400 /* Leave linkage flags alone on instantiations with anonymous
23401 visibility. */
23402 if (TREE_PUBLIC (d))
23404 DECL_NOT_REALLY_EXTERN (d) = 0;
23405 DECL_INTERFACE_KNOWN (d) = 0;
23407 SET_DECL_IMPLICIT_INSTANTIATION (d);
23410 /* Defer all other templates, unless we have been explicitly
23411 forbidden from doing so. */
23412 if (/* If there is no definition, we cannot instantiate the
23413 template. */
23414 ! pattern_defined
23415 /* If it's OK to postpone instantiation, do so. */
23416 || defer_ok
23417 /* If this is a static data member that will be defined
23418 elsewhere, we don't want to instantiate the entire data
23419 member, but we do want to instantiate the initializer so that
23420 we can substitute that elsewhere. */
23421 || (external_p && VAR_P (d))
23422 /* Handle here a deleted function too, avoid generating
23423 its body (c++/61080). */
23424 || deleted_p)
23426 /* The definition of the static data member is now required so
23427 we must substitute the initializer. */
23428 if (VAR_P (d)
23429 && !DECL_INITIAL (d)
23430 && DECL_INITIAL (code_pattern))
23432 tree ns;
23433 tree init;
23434 bool const_init = false;
23435 bool enter_context = DECL_CLASS_SCOPE_P (d);
23437 ns = decl_namespace_context (d);
23438 push_nested_namespace (ns);
23439 if (enter_context)
23440 push_nested_class (DECL_CONTEXT (d));
23441 init = tsubst_expr (DECL_INITIAL (code_pattern),
23442 args,
23443 tf_warning_or_error, NULL_TREE,
23444 /*integral_constant_expression_p=*/false);
23445 /* If instantiating the initializer involved instantiating this
23446 again, don't call cp_finish_decl twice. */
23447 if (!DECL_INITIAL (d))
23449 /* Make sure the initializer is still constant, in case of
23450 circular dependency (template/instantiate6.C). */
23451 const_init
23452 = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
23453 cp_finish_decl (d, init, /*init_const_expr_p=*/const_init,
23454 /*asmspec_tree=*/NULL_TREE,
23455 LOOKUP_ONLYCONVERTING);
23457 if (enter_context)
23458 pop_nested_class ();
23459 pop_nested_namespace (ns);
23462 /* We restore the source position here because it's used by
23463 add_pending_template. */
23464 input_location = saved_loc;
23466 if (at_eof && !pattern_defined
23467 && DECL_EXPLICIT_INSTANTIATION (d)
23468 && DECL_NOT_REALLY_EXTERN (d))
23469 /* [temp.explicit]
23471 The definition of a non-exported function template, a
23472 non-exported member function template, or a non-exported
23473 member function or static data member of a class template
23474 shall be present in every translation unit in which it is
23475 explicitly instantiated. */
23476 permerror (input_location, "explicit instantiation of %qD "
23477 "but no definition available", d);
23479 /* If we're in unevaluated context, we just wanted to get the
23480 constant value; this isn't an odr use, so don't queue
23481 a full instantiation. */
23482 if (cp_unevaluated_operand != 0)
23483 goto out;
23484 /* ??? Historically, we have instantiated inline functions, even
23485 when marked as "extern template". */
23486 if (!(external_p && VAR_P (d)))
23487 add_pending_template (d);
23488 goto out;
23490 /* Tell the repository that D is available in this translation unit
23491 -- and see if it is supposed to be instantiated here. */
23492 if (TREE_PUBLIC (d) && !DECL_REALLY_EXTERN (d) && !repo_emit_p (d))
23494 /* In a PCH file, despite the fact that the repository hasn't
23495 requested instantiation in the PCH it is still possible that
23496 an instantiation will be required in a file that includes the
23497 PCH. */
23498 if (pch_file)
23499 add_pending_template (d);
23500 /* Instantiate inline functions so that the inliner can do its
23501 job, even though we'll not be emitting a copy of this
23502 function. */
23503 if (!(TREE_CODE (d) == FUNCTION_DECL && possibly_inlined_p (d)))
23504 goto out;
23507 bool push_to_top, nested;
23508 tree fn_context;
23509 fn_context = decl_function_context (d);
23510 if (LAMBDA_FUNCTION_P (d))
23511 /* tsubst_lambda_expr resolved any references to enclosing functions. */
23512 fn_context = NULL_TREE;
23513 nested = current_function_decl != NULL_TREE;
23514 push_to_top = !(nested && fn_context == current_function_decl);
23516 vec<tree> omp_privatization_save;
23517 if (nested)
23518 save_omp_privatization_clauses (omp_privatization_save);
23520 if (push_to_top)
23521 push_to_top_level ();
23522 else
23524 push_function_context ();
23525 cp_unevaluated_operand = 0;
23526 c_inhibit_evaluation_warnings = 0;
23529 /* Mark D as instantiated so that recursive calls to
23530 instantiate_decl do not try to instantiate it again. */
23531 DECL_TEMPLATE_INSTANTIATED (d) = 1;
23533 /* Regenerate the declaration in case the template has been modified
23534 by a subsequent redeclaration. */
23535 regenerate_decl_from_template (d, td, args);
23537 /* We already set the file and line above. Reset them now in case
23538 they changed as a result of calling regenerate_decl_from_template. */
23539 input_location = DECL_SOURCE_LOCATION (d);
23541 if (VAR_P (d))
23543 tree init;
23544 bool const_init = false;
23546 /* Clear out DECL_RTL; whatever was there before may not be right
23547 since we've reset the type of the declaration. */
23548 SET_DECL_RTL (d, NULL);
23549 DECL_IN_AGGR_P (d) = 0;
23551 /* The initializer is placed in DECL_INITIAL by
23552 regenerate_decl_from_template so we don't need to
23553 push/pop_access_scope again here. Pull it out so that
23554 cp_finish_decl can process it. */
23555 init = DECL_INITIAL (d);
23556 DECL_INITIAL (d) = NULL_TREE;
23557 DECL_INITIALIZED_P (d) = 0;
23559 /* Clear DECL_EXTERNAL so that cp_finish_decl will process the
23560 initializer. That function will defer actual emission until
23561 we have a chance to determine linkage. */
23562 DECL_EXTERNAL (d) = 0;
23564 /* Enter the scope of D so that access-checking works correctly. */
23565 bool enter_context = DECL_CLASS_SCOPE_P (d);
23566 if (enter_context)
23567 push_nested_class (DECL_CONTEXT (d));
23569 const_init = DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (code_pattern);
23570 cp_finish_decl (d, init, const_init, NULL_TREE, 0);
23572 if (enter_context)
23573 pop_nested_class ();
23575 if (variable_template_p (gen_tmpl))
23576 note_variable_template_instantiation (d);
23578 else if (TREE_CODE (d) == FUNCTION_DECL && DECL_DEFAULTED_FN (code_pattern))
23579 synthesize_method (d);
23580 else if (TREE_CODE (d) == FUNCTION_DECL)
23582 /* Set up the list of local specializations. */
23583 local_specialization_stack lss (push_to_top ? lss_blank : lss_copy);
23584 tree block = NULL_TREE;
23586 /* Set up context. */
23587 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23588 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23589 block = push_stmt_list ();
23590 else
23591 start_preparsed_function (d, NULL_TREE, SF_PRE_PARSED);
23593 /* Some typedefs referenced from within the template code need to be
23594 access checked at template instantiation time, i.e now. These
23595 types were added to the template at parsing time. Let's get those
23596 and perform the access checks then. */
23597 perform_typedefs_access_check (DECL_TEMPLATE_RESULT (td),
23598 args);
23600 /* Create substitution entries for the parameters. */
23601 register_parameter_specializations (code_pattern, d);
23603 /* Substitute into the body of the function. */
23604 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23605 tsubst_omp_udr (DECL_SAVED_TREE (code_pattern), args,
23606 tf_warning_or_error, tmpl);
23607 else
23609 tsubst_expr (DECL_SAVED_TREE (code_pattern), args,
23610 tf_warning_or_error, tmpl,
23611 /*integral_constant_expression_p=*/false);
23613 /* Set the current input_location to the end of the function
23614 so that finish_function knows where we are. */
23615 input_location
23616 = DECL_STRUCT_FUNCTION (code_pattern)->function_end_locus;
23618 /* Remember if we saw an infinite loop in the template. */
23619 current_function_infinite_loop
23620 = DECL_STRUCT_FUNCTION (code_pattern)->language->infinite_loop;
23623 /* Finish the function. */
23624 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern)
23625 && TREE_CODE (DECL_CONTEXT (code_pattern)) == FUNCTION_DECL)
23626 DECL_SAVED_TREE (d) = pop_stmt_list (block);
23627 else
23629 d = finish_function (/*inline_p=*/false);
23630 expand_or_defer_fn (d);
23633 if (DECL_OMP_DECLARE_REDUCTION_P (code_pattern))
23634 cp_check_omp_declare_reduction (d);
23637 /* We're not deferring instantiation any more. */
23638 TI_PENDING_TEMPLATE_FLAG (DECL_TEMPLATE_INFO (d)) = 0;
23640 if (push_to_top)
23641 pop_from_top_level ();
23642 else
23643 pop_function_context ();
23645 if (nested)
23646 restore_omp_privatization_clauses (omp_privatization_save);
23648 out:
23649 pop_deferring_access_checks ();
23650 timevar_pop (TV_TEMPLATE_INST);
23651 pop_tinst_level ();
23652 input_location = saved_loc;
23653 cp_unevaluated_operand = saved_unevaluated_operand;
23654 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
23656 return d;
23659 /* Run through the list of templates that we wish we could
23660 instantiate, and instantiate any we can. RETRIES is the
23661 number of times we retry pending template instantiation. */
23663 void
23664 instantiate_pending_templates (int retries)
23666 int reconsider;
23667 location_t saved_loc = input_location;
23669 /* Instantiating templates may trigger vtable generation. This in turn
23670 may require further template instantiations. We place a limit here
23671 to avoid infinite loop. */
23672 if (pending_templates && retries >= max_tinst_depth)
23674 tree decl = pending_templates->tinst->decl;
23676 fatal_error (input_location,
23677 "template instantiation depth exceeds maximum of %d"
23678 " instantiating %q+D, possibly from virtual table generation"
23679 " (use -ftemplate-depth= to increase the maximum)",
23680 max_tinst_depth, decl);
23681 if (TREE_CODE (decl) == FUNCTION_DECL)
23682 /* Pretend that we defined it. */
23683 DECL_INITIAL (decl) = error_mark_node;
23684 return;
23689 struct pending_template **t = &pending_templates;
23690 struct pending_template *last = NULL;
23691 reconsider = 0;
23692 while (*t)
23694 tree instantiation = reopen_tinst_level ((*t)->tinst);
23695 bool complete = false;
23697 if (TYPE_P (instantiation))
23699 if (!COMPLETE_TYPE_P (instantiation))
23701 instantiate_class_template (instantiation);
23702 if (CLASSTYPE_TEMPLATE_INSTANTIATION (instantiation))
23703 for (tree fld = TYPE_FIELDS (instantiation);
23704 fld; fld = TREE_CHAIN (fld))
23705 if ((VAR_P (fld)
23706 || (TREE_CODE (fld) == FUNCTION_DECL
23707 && !DECL_ARTIFICIAL (fld)))
23708 && DECL_TEMPLATE_INSTANTIATION (fld))
23709 instantiate_decl (fld,
23710 /*defer_ok=*/false,
23711 /*expl_inst_class_mem_p=*/false);
23713 if (COMPLETE_TYPE_P (instantiation))
23714 reconsider = 1;
23717 complete = COMPLETE_TYPE_P (instantiation);
23719 else
23721 if (!DECL_TEMPLATE_SPECIALIZATION (instantiation)
23722 && !DECL_TEMPLATE_INSTANTIATED (instantiation))
23724 instantiation
23725 = instantiate_decl (instantiation,
23726 /*defer_ok=*/false,
23727 /*expl_inst_class_mem_p=*/false);
23728 if (DECL_TEMPLATE_INSTANTIATED (instantiation))
23729 reconsider = 1;
23732 complete = (DECL_TEMPLATE_SPECIALIZATION (instantiation)
23733 || DECL_TEMPLATE_INSTANTIATED (instantiation));
23736 if (complete)
23737 /* If INSTANTIATION has been instantiated, then we don't
23738 need to consider it again in the future. */
23739 *t = (*t)->next;
23740 else
23742 last = *t;
23743 t = &(*t)->next;
23745 tinst_depth = 0;
23746 current_tinst_level = NULL;
23748 last_pending_template = last;
23750 while (reconsider);
23752 input_location = saved_loc;
23755 /* Substitute ARGVEC into T, which is a list of initializers for
23756 either base class or a non-static data member. The TREE_PURPOSEs
23757 are DECLs, and the TREE_VALUEs are the initializer values. Used by
23758 instantiate_decl. */
23760 static tree
23761 tsubst_initializer_list (tree t, tree argvec)
23763 tree inits = NULL_TREE;
23764 tree target_ctor = error_mark_node;
23766 for (; t; t = TREE_CHAIN (t))
23768 tree decl;
23769 tree init;
23770 tree expanded_bases = NULL_TREE;
23771 tree expanded_arguments = NULL_TREE;
23772 int i, len = 1;
23774 if (TREE_CODE (TREE_PURPOSE (t)) == TYPE_PACK_EXPANSION)
23776 tree expr;
23777 tree arg;
23779 /* Expand the base class expansion type into separate base
23780 classes. */
23781 expanded_bases = tsubst_pack_expansion (TREE_PURPOSE (t), argvec,
23782 tf_warning_or_error,
23783 NULL_TREE);
23784 if (expanded_bases == error_mark_node)
23785 continue;
23787 /* We'll be building separate TREE_LISTs of arguments for
23788 each base. */
23789 len = TREE_VEC_LENGTH (expanded_bases);
23790 expanded_arguments = make_tree_vec (len);
23791 for (i = 0; i < len; i++)
23792 TREE_VEC_ELT (expanded_arguments, i) = NULL_TREE;
23794 /* Build a dummy EXPR_PACK_EXPANSION that will be used to
23795 expand each argument in the TREE_VALUE of t. */
23796 expr = make_node (EXPR_PACK_EXPANSION);
23797 PACK_EXPANSION_LOCAL_P (expr) = true;
23798 PACK_EXPANSION_PARAMETER_PACKS (expr) =
23799 PACK_EXPANSION_PARAMETER_PACKS (TREE_PURPOSE (t));
23801 if (TREE_VALUE (t) == void_type_node)
23802 /* VOID_TYPE_NODE is used to indicate
23803 value-initialization. */
23805 for (i = 0; i < len; i++)
23806 TREE_VEC_ELT (expanded_arguments, i) = void_type_node;
23808 else
23810 /* Substitute parameter packs into each argument in the
23811 TREE_LIST. */
23812 in_base_initializer = 1;
23813 for (arg = TREE_VALUE (t); arg; arg = TREE_CHAIN (arg))
23815 tree expanded_exprs;
23817 /* Expand the argument. */
23818 SET_PACK_EXPANSION_PATTERN (expr, TREE_VALUE (arg));
23819 expanded_exprs
23820 = tsubst_pack_expansion (expr, argvec,
23821 tf_warning_or_error,
23822 NULL_TREE);
23823 if (expanded_exprs == error_mark_node)
23824 continue;
23826 /* Prepend each of the expanded expressions to the
23827 corresponding TREE_LIST in EXPANDED_ARGUMENTS. */
23828 for (i = 0; i < len; i++)
23830 TREE_VEC_ELT (expanded_arguments, i) =
23831 tree_cons (NULL_TREE,
23832 TREE_VEC_ELT (expanded_exprs, i),
23833 TREE_VEC_ELT (expanded_arguments, i));
23836 in_base_initializer = 0;
23838 /* Reverse all of the TREE_LISTs in EXPANDED_ARGUMENTS,
23839 since we built them backwards. */
23840 for (i = 0; i < len; i++)
23842 TREE_VEC_ELT (expanded_arguments, i) =
23843 nreverse (TREE_VEC_ELT (expanded_arguments, i));
23848 for (i = 0; i < len; ++i)
23850 if (expanded_bases)
23852 decl = TREE_VEC_ELT (expanded_bases, i);
23853 decl = expand_member_init (decl);
23854 init = TREE_VEC_ELT (expanded_arguments, i);
23856 else
23858 tree tmp;
23859 decl = tsubst_copy (TREE_PURPOSE (t), argvec,
23860 tf_warning_or_error, NULL_TREE);
23862 decl = expand_member_init (decl);
23863 if (decl && !DECL_P (decl))
23864 in_base_initializer = 1;
23866 init = TREE_VALUE (t);
23867 tmp = init;
23868 if (init != void_type_node)
23869 init = tsubst_expr (init, argvec,
23870 tf_warning_or_error, NULL_TREE,
23871 /*integral_constant_expression_p=*/false);
23872 if (init == NULL_TREE && tmp != NULL_TREE)
23873 /* If we had an initializer but it instantiated to nothing,
23874 value-initialize the object. This will only occur when
23875 the initializer was a pack expansion where the parameter
23876 packs used in that expansion were of length zero. */
23877 init = void_type_node;
23878 in_base_initializer = 0;
23881 if (target_ctor != error_mark_node
23882 && init != error_mark_node)
23884 error ("mem-initializer for %qD follows constructor delegation",
23885 decl);
23886 return inits;
23888 /* Look for a target constructor. */
23889 if (init != error_mark_node
23890 && decl && CLASS_TYPE_P (decl)
23891 && same_type_p (decl, current_class_type))
23893 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
23894 if (inits)
23896 error ("constructor delegation follows mem-initializer for %qD",
23897 TREE_PURPOSE (inits));
23898 continue;
23900 target_ctor = init;
23903 if (decl)
23905 init = build_tree_list (decl, init);
23906 TREE_CHAIN (init) = inits;
23907 inits = init;
23911 return inits;
23914 /* Set CURRENT_ACCESS_SPECIFIER based on the protection of DECL. */
23916 static void
23917 set_current_access_from_decl (tree decl)
23919 if (TREE_PRIVATE (decl))
23920 current_access_specifier = access_private_node;
23921 else if (TREE_PROTECTED (decl))
23922 current_access_specifier = access_protected_node;
23923 else
23924 current_access_specifier = access_public_node;
23927 /* Instantiate an enumerated type. TAG is the template type, NEWTAG
23928 is the instantiation (which should have been created with
23929 start_enum) and ARGS are the template arguments to use. */
23931 static void
23932 tsubst_enum (tree tag, tree newtag, tree args)
23934 tree e;
23936 if (SCOPED_ENUM_P (newtag))
23937 begin_scope (sk_scoped_enum, newtag);
23939 for (e = TYPE_VALUES (tag); e; e = TREE_CHAIN (e))
23941 tree value;
23942 tree decl;
23944 decl = TREE_VALUE (e);
23945 /* Note that in a template enum, the TREE_VALUE is the
23946 CONST_DECL, not the corresponding INTEGER_CST. */
23947 value = tsubst_expr (DECL_INITIAL (decl),
23948 args, tf_warning_or_error, NULL_TREE,
23949 /*integral_constant_expression_p=*/true);
23951 /* Give this enumeration constant the correct access. */
23952 set_current_access_from_decl (decl);
23954 /* Actually build the enumerator itself. Here we're assuming that
23955 enumerators can't have dependent attributes. */
23956 build_enumerator (DECL_NAME (decl), value, newtag,
23957 DECL_ATTRIBUTES (decl), DECL_SOURCE_LOCATION (decl));
23960 if (SCOPED_ENUM_P (newtag))
23961 finish_scope ();
23963 finish_enum_value_list (newtag);
23964 finish_enum (newtag);
23966 DECL_SOURCE_LOCATION (TYPE_NAME (newtag))
23967 = DECL_SOURCE_LOCATION (TYPE_NAME (tag));
23970 /* DECL is a FUNCTION_DECL that is a template specialization. Return
23971 its type -- but without substituting the innermost set of template
23972 arguments. So, innermost set of template parameters will appear in
23973 the type. */
23975 tree
23976 get_mostly_instantiated_function_type (tree decl)
23978 /* For a function, DECL_TI_TEMPLATE is partially instantiated. */
23979 return TREE_TYPE (DECL_TI_TEMPLATE (decl));
23982 /* Return truthvalue if we're processing a template different from
23983 the last one involved in diagnostics. */
23984 bool
23985 problematic_instantiation_changed (void)
23987 return current_tinst_level != last_error_tinst_level;
23990 /* Remember current template involved in diagnostics. */
23991 void
23992 record_last_problematic_instantiation (void)
23994 last_error_tinst_level = current_tinst_level;
23997 struct tinst_level *
23998 current_instantiation (void)
24000 return current_tinst_level;
24003 /* Return TRUE if current_function_decl is being instantiated, false
24004 otherwise. */
24006 bool
24007 instantiating_current_function_p (void)
24009 return (current_instantiation ()
24010 && current_instantiation ()->decl == current_function_decl);
24013 /* [temp.param] Check that template non-type parm TYPE is of an allowable
24014 type. Return false for ok, true for disallowed. Issue error and
24015 inform messages under control of COMPLAIN. */
24017 static bool
24018 invalid_nontype_parm_type_p (tree type, tsubst_flags_t complain)
24020 if (INTEGRAL_OR_ENUMERATION_TYPE_P (type))
24021 return false;
24022 else if (TYPE_PTR_P (type))
24023 return false;
24024 else if (TREE_CODE (type) == REFERENCE_TYPE
24025 && !TYPE_REF_IS_RVALUE (type))
24026 return false;
24027 else if (TYPE_PTRMEM_P (type))
24028 return false;
24029 else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM)
24030 return false;
24031 else if (TREE_CODE (type) == TYPENAME_TYPE)
24032 return false;
24033 else if (TREE_CODE (type) == DECLTYPE_TYPE)
24034 return false;
24035 else if (TREE_CODE (type) == NULLPTR_TYPE)
24036 return false;
24037 /* A bound template template parm could later be instantiated to have a valid
24038 nontype parm type via an alias template. */
24039 else if (cxx_dialect >= cxx11
24040 && TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
24041 return false;
24043 if (complain & tf_error)
24045 if (type == error_mark_node)
24046 inform (input_location, "invalid template non-type parameter");
24047 else
24048 error ("%q#T is not a valid type for a template non-type parameter",
24049 type);
24051 return true;
24054 /* Returns TRUE if TYPE is dependent, in the sense of [temp.dep.type].
24055 Assumes that TYPE really is a type, and not the ERROR_MARK_NODE.*/
24057 static bool
24058 dependent_type_p_r (tree type)
24060 tree scope;
24062 /* [temp.dep.type]
24064 A type is dependent if it is:
24066 -- a template parameter. Template template parameters are types
24067 for us (since TYPE_P holds true for them) so we handle
24068 them here. */
24069 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
24070 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
24071 return true;
24072 /* -- a qualified-id with a nested-name-specifier which contains a
24073 class-name that names a dependent type or whose unqualified-id
24074 names a dependent type. */
24075 if (TREE_CODE (type) == TYPENAME_TYPE)
24076 return true;
24078 /* An alias template specialization can be dependent even if the
24079 resulting type is not. */
24080 if (dependent_alias_template_spec_p (type))
24081 return true;
24083 /* -- a cv-qualified type where the cv-unqualified type is
24084 dependent.
24085 No code is necessary for this bullet; the code below handles
24086 cv-qualified types, and we don't want to strip aliases with
24087 TYPE_MAIN_VARIANT because of DR 1558. */
24088 /* -- a compound type constructed from any dependent type. */
24089 if (TYPE_PTRMEM_P (type))
24090 return (dependent_type_p (TYPE_PTRMEM_CLASS_TYPE (type))
24091 || dependent_type_p (TYPE_PTRMEM_POINTED_TO_TYPE
24092 (type)));
24093 else if (TYPE_PTR_P (type)
24094 || TREE_CODE (type) == REFERENCE_TYPE)
24095 return dependent_type_p (TREE_TYPE (type));
24096 else if (TREE_CODE (type) == FUNCTION_TYPE
24097 || TREE_CODE (type) == METHOD_TYPE)
24099 tree arg_type;
24101 if (dependent_type_p (TREE_TYPE (type)))
24102 return true;
24103 for (arg_type = TYPE_ARG_TYPES (type);
24104 arg_type;
24105 arg_type = TREE_CHAIN (arg_type))
24106 if (dependent_type_p (TREE_VALUE (arg_type)))
24107 return true;
24108 if (cxx_dialect >= cxx17)
24109 /* A value-dependent noexcept-specifier makes the type dependent. */
24110 if (tree spec = TYPE_RAISES_EXCEPTIONS (type))
24111 if (tree noex = TREE_PURPOSE (spec))
24112 /* Treat DEFERRED_NOEXCEPT as non-dependent, since it doesn't
24113 affect overload resolution and treating it as dependent breaks
24114 things. */
24115 if (TREE_CODE (noex) != DEFERRED_NOEXCEPT
24116 && value_dependent_expression_p (noex))
24117 return true;
24118 return false;
24120 /* -- an array type constructed from any dependent type or whose
24121 size is specified by a constant expression that is
24122 value-dependent.
24124 We checked for type- and value-dependence of the bounds in
24125 compute_array_index_type, so TYPE_DEPENDENT_P is already set. */
24126 if (TREE_CODE (type) == ARRAY_TYPE)
24128 if (TYPE_DOMAIN (type)
24129 && dependent_type_p (TYPE_DOMAIN (type)))
24130 return true;
24131 return dependent_type_p (TREE_TYPE (type));
24134 /* -- a template-id in which either the template name is a template
24135 parameter ... */
24136 if (TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
24137 return true;
24138 /* ... or any of the template arguments is a dependent type or
24139 an expression that is type-dependent or value-dependent. */
24140 else if (CLASS_TYPE_P (type) && CLASSTYPE_TEMPLATE_INFO (type)
24141 && (any_dependent_template_arguments_p
24142 (INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (type)))))
24143 return true;
24145 /* All TYPEOF_TYPEs, DECLTYPE_TYPEs, and UNDERLYING_TYPEs are
24146 dependent; if the argument of the `typeof' expression is not
24147 type-dependent, then it should already been have resolved. */
24148 if (TREE_CODE (type) == TYPEOF_TYPE
24149 || TREE_CODE (type) == DECLTYPE_TYPE
24150 || TREE_CODE (type) == UNDERLYING_TYPE)
24151 return true;
24153 /* A template argument pack is dependent if any of its packed
24154 arguments are. */
24155 if (TREE_CODE (type) == TYPE_ARGUMENT_PACK)
24157 tree args = ARGUMENT_PACK_ARGS (type);
24158 int i, len = TREE_VEC_LENGTH (args);
24159 for (i = 0; i < len; ++i)
24160 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
24161 return true;
24164 /* All TYPE_PACK_EXPANSIONs are dependent, because parameter packs must
24165 be template parameters. */
24166 if (TREE_CODE (type) == TYPE_PACK_EXPANSION)
24167 return true;
24169 if (any_dependent_type_attributes_p (TYPE_ATTRIBUTES (type)))
24170 return true;
24172 /* The standard does not specifically mention types that are local
24173 to template functions or local classes, but they should be
24174 considered dependent too. For example:
24176 template <int I> void f() {
24177 enum E { a = I };
24178 S<sizeof (E)> s;
24181 The size of `E' cannot be known until the value of `I' has been
24182 determined. Therefore, `E' must be considered dependent. */
24183 scope = TYPE_CONTEXT (type);
24184 if (scope && TYPE_P (scope))
24185 return dependent_type_p (scope);
24186 /* Don't use type_dependent_expression_p here, as it can lead
24187 to infinite recursion trying to determine whether a lambda
24188 nested in a lambda is dependent (c++/47687). */
24189 else if (scope && TREE_CODE (scope) == FUNCTION_DECL
24190 && DECL_LANG_SPECIFIC (scope)
24191 && DECL_TEMPLATE_INFO (scope)
24192 && (any_dependent_template_arguments_p
24193 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (scope)))))
24194 return true;
24196 /* Other types are non-dependent. */
24197 return false;
24200 /* Returns TRUE if TYPE is dependent, in the sense of
24201 [temp.dep.type]. Note that a NULL type is considered dependent. */
24203 bool
24204 dependent_type_p (tree type)
24206 /* If there are no template parameters in scope, then there can't be
24207 any dependent types. */
24208 if (!processing_template_decl)
24210 /* If we are not processing a template, then nobody should be
24211 providing us with a dependent type. */
24212 gcc_assert (type);
24213 gcc_assert (TREE_CODE (type) != TEMPLATE_TYPE_PARM || is_auto (type));
24214 return false;
24217 /* If the type is NULL, we have not computed a type for the entity
24218 in question; in that case, the type is dependent. */
24219 if (!type)
24220 return true;
24222 /* Erroneous types can be considered non-dependent. */
24223 if (type == error_mark_node)
24224 return false;
24226 /* Getting here with global_type_node means we improperly called this
24227 function on the TREE_TYPE of an IDENTIFIER_NODE. */
24228 gcc_checking_assert (type != global_type_node);
24230 /* If we have not already computed the appropriate value for TYPE,
24231 do so now. */
24232 if (!TYPE_DEPENDENT_P_VALID (type))
24234 TYPE_DEPENDENT_P (type) = dependent_type_p_r (type);
24235 TYPE_DEPENDENT_P_VALID (type) = 1;
24238 return TYPE_DEPENDENT_P (type);
24241 /* Returns TRUE if SCOPE is a dependent scope, in which we can't do any
24242 lookup. In other words, a dependent type that is not the current
24243 instantiation. */
24245 bool
24246 dependent_scope_p (tree scope)
24248 return (scope && TYPE_P (scope) && dependent_type_p (scope)
24249 && !currently_open_class (scope));
24252 /* T is a SCOPE_REF. Return whether it represents a non-static member of
24253 an unknown base of 'this' (and is therefore instantiation-dependent). */
24255 static bool
24256 unknown_base_ref_p (tree t)
24258 if (!current_class_ptr)
24259 return false;
24261 tree mem = TREE_OPERAND (t, 1);
24262 if (shared_member_p (mem))
24263 return false;
24265 tree cur = current_nonlambda_class_type ();
24266 if (!any_dependent_bases_p (cur))
24267 return false;
24269 tree ctx = TREE_OPERAND (t, 0);
24270 if (DERIVED_FROM_P (ctx, cur))
24271 return false;
24273 return true;
24276 /* T is a SCOPE_REF; return whether we need to consider it
24277 instantiation-dependent so that we can check access at instantiation
24278 time even though we know which member it resolves to. */
24280 static bool
24281 instantiation_dependent_scope_ref_p (tree t)
24283 if (DECL_P (TREE_OPERAND (t, 1))
24284 && CLASS_TYPE_P (TREE_OPERAND (t, 0))
24285 && !unknown_base_ref_p (t)
24286 && accessible_in_template_p (TREE_OPERAND (t, 0),
24287 TREE_OPERAND (t, 1)))
24288 return false;
24289 else
24290 return true;
24293 /* Returns TRUE if the EXPRESSION is value-dependent, in the sense of
24294 [temp.dep.constexpr]. EXPRESSION is already known to be a constant
24295 expression. */
24297 /* Note that this predicate is not appropriate for general expressions;
24298 only constant expressions (that satisfy potential_constant_expression)
24299 can be tested for value dependence. */
24301 bool
24302 value_dependent_expression_p (tree expression)
24304 if (!processing_template_decl || expression == NULL_TREE)
24305 return false;
24307 /* A type-dependent expression is also value-dependent. */
24308 if (type_dependent_expression_p (expression))
24309 return true;
24311 switch (TREE_CODE (expression))
24313 case BASELINK:
24314 /* A dependent member function of the current instantiation. */
24315 return dependent_type_p (BINFO_TYPE (BASELINK_BINFO (expression)));
24317 case FUNCTION_DECL:
24318 /* A dependent member function of the current instantiation. */
24319 if (DECL_CLASS_SCOPE_P (expression)
24320 && dependent_type_p (DECL_CONTEXT (expression)))
24321 return true;
24322 break;
24324 case IDENTIFIER_NODE:
24325 /* A name that has not been looked up -- must be dependent. */
24326 return true;
24328 case TEMPLATE_PARM_INDEX:
24329 /* A non-type template parm. */
24330 return true;
24332 case CONST_DECL:
24333 /* A non-type template parm. */
24334 if (DECL_TEMPLATE_PARM_P (expression))
24335 return true;
24336 return value_dependent_expression_p (DECL_INITIAL (expression));
24338 case VAR_DECL:
24339 /* A constant with literal type and is initialized
24340 with an expression that is value-dependent. */
24341 if (DECL_DEPENDENT_INIT_P (expression)
24342 /* FIXME cp_finish_decl doesn't fold reference initializers. */
24343 || TREE_CODE (TREE_TYPE (expression)) == REFERENCE_TYPE)
24344 return true;
24345 if (DECL_HAS_VALUE_EXPR_P (expression))
24347 tree value_expr = DECL_VALUE_EXPR (expression);
24348 if (value_dependent_expression_p (value_expr))
24349 return true;
24351 return false;
24353 case DYNAMIC_CAST_EXPR:
24354 case STATIC_CAST_EXPR:
24355 case CONST_CAST_EXPR:
24356 case REINTERPRET_CAST_EXPR:
24357 case CAST_EXPR:
24358 case IMPLICIT_CONV_EXPR:
24359 /* These expressions are value-dependent if the type to which
24360 the cast occurs is dependent or the expression being casted
24361 is value-dependent. */
24363 tree type = TREE_TYPE (expression);
24365 if (dependent_type_p (type))
24366 return true;
24368 /* A functional cast has a list of operands. */
24369 expression = TREE_OPERAND (expression, 0);
24370 if (!expression)
24372 /* If there are no operands, it must be an expression such
24373 as "int()". This should not happen for aggregate types
24374 because it would form non-constant expressions. */
24375 gcc_assert (cxx_dialect >= cxx11
24376 || INTEGRAL_OR_ENUMERATION_TYPE_P (type));
24378 return false;
24381 if (TREE_CODE (expression) == TREE_LIST)
24382 return any_value_dependent_elements_p (expression);
24384 return value_dependent_expression_p (expression);
24387 case SIZEOF_EXPR:
24388 if (SIZEOF_EXPR_TYPE_P (expression))
24389 return dependent_type_p (TREE_TYPE (TREE_OPERAND (expression, 0)));
24390 /* FALLTHRU */
24391 case ALIGNOF_EXPR:
24392 case TYPEID_EXPR:
24393 /* A `sizeof' expression is value-dependent if the operand is
24394 type-dependent or is a pack expansion. */
24395 expression = TREE_OPERAND (expression, 0);
24396 if (PACK_EXPANSION_P (expression))
24397 return true;
24398 else if (TYPE_P (expression))
24399 return dependent_type_p (expression);
24400 return instantiation_dependent_uneval_expression_p (expression);
24402 case AT_ENCODE_EXPR:
24403 /* An 'encode' expression is value-dependent if the operand is
24404 type-dependent. */
24405 expression = TREE_OPERAND (expression, 0);
24406 return dependent_type_p (expression);
24408 case NOEXCEPT_EXPR:
24409 expression = TREE_OPERAND (expression, 0);
24410 return instantiation_dependent_uneval_expression_p (expression);
24412 case SCOPE_REF:
24413 /* All instantiation-dependent expressions should also be considered
24414 value-dependent. */
24415 return instantiation_dependent_scope_ref_p (expression);
24417 case COMPONENT_REF:
24418 return (value_dependent_expression_p (TREE_OPERAND (expression, 0))
24419 || value_dependent_expression_p (TREE_OPERAND (expression, 1)));
24421 case NONTYPE_ARGUMENT_PACK:
24422 /* A NONTYPE_ARGUMENT_PACK is value-dependent if any packed argument
24423 is value-dependent. */
24425 tree values = ARGUMENT_PACK_ARGS (expression);
24426 int i, len = TREE_VEC_LENGTH (values);
24428 for (i = 0; i < len; ++i)
24429 if (value_dependent_expression_p (TREE_VEC_ELT (values, i)))
24430 return true;
24432 return false;
24435 case TRAIT_EXPR:
24437 tree type2 = TRAIT_EXPR_TYPE2 (expression);
24439 if (dependent_type_p (TRAIT_EXPR_TYPE1 (expression)))
24440 return true;
24442 if (!type2)
24443 return false;
24445 if (TREE_CODE (type2) != TREE_LIST)
24446 return dependent_type_p (type2);
24448 for (; type2; type2 = TREE_CHAIN (type2))
24449 if (dependent_type_p (TREE_VALUE (type2)))
24450 return true;
24452 return false;
24455 case MODOP_EXPR:
24456 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24457 || (value_dependent_expression_p (TREE_OPERAND (expression, 2))));
24459 case ARRAY_REF:
24460 return ((value_dependent_expression_p (TREE_OPERAND (expression, 0)))
24461 || (value_dependent_expression_p (TREE_OPERAND (expression, 1))));
24463 case ADDR_EXPR:
24465 tree op = TREE_OPERAND (expression, 0);
24466 return (value_dependent_expression_p (op)
24467 || has_value_dependent_address (op));
24470 case REQUIRES_EXPR:
24471 /* Treat all requires-expressions as value-dependent so
24472 we don't try to fold them. */
24473 return true;
24475 case TYPE_REQ:
24476 return dependent_type_p (TREE_OPERAND (expression, 0));
24478 case CALL_EXPR:
24480 if (value_dependent_expression_p (CALL_EXPR_FN (expression)))
24481 return true;
24482 tree fn = get_callee_fndecl (expression);
24483 int i, nargs;
24484 nargs = call_expr_nargs (expression);
24485 for (i = 0; i < nargs; ++i)
24487 tree op = CALL_EXPR_ARG (expression, i);
24488 /* In a call to a constexpr member function, look through the
24489 implicit ADDR_EXPR on the object argument so that it doesn't
24490 cause the call to be considered value-dependent. We also
24491 look through it in potential_constant_expression. */
24492 if (i == 0 && fn && DECL_DECLARED_CONSTEXPR_P (fn)
24493 && DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
24494 && TREE_CODE (op) == ADDR_EXPR)
24495 op = TREE_OPERAND (op, 0);
24496 if (value_dependent_expression_p (op))
24497 return true;
24499 return false;
24502 case TEMPLATE_ID_EXPR:
24503 return variable_concept_p (TREE_OPERAND (expression, 0));
24505 case CONSTRUCTOR:
24507 unsigned ix;
24508 tree val;
24509 if (dependent_type_p (TREE_TYPE (expression)))
24510 return true;
24511 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), ix, val)
24512 if (value_dependent_expression_p (val))
24513 return true;
24514 return false;
24517 case STMT_EXPR:
24518 /* Treat a GNU statement expression as dependent to avoid crashing
24519 under instantiate_non_dependent_expr; it can't be constant. */
24520 return true;
24522 default:
24523 /* A constant expression is value-dependent if any subexpression is
24524 value-dependent. */
24525 switch (TREE_CODE_CLASS (TREE_CODE (expression)))
24527 case tcc_reference:
24528 case tcc_unary:
24529 case tcc_comparison:
24530 case tcc_binary:
24531 case tcc_expression:
24532 case tcc_vl_exp:
24534 int i, len = cp_tree_operand_length (expression);
24536 for (i = 0; i < len; i++)
24538 tree t = TREE_OPERAND (expression, i);
24540 /* In some cases, some of the operands may be missing.
24541 (For example, in the case of PREDECREMENT_EXPR, the
24542 amount to increment by may be missing.) That doesn't
24543 make the expression dependent. */
24544 if (t && value_dependent_expression_p (t))
24545 return true;
24548 break;
24549 default:
24550 break;
24552 break;
24555 /* The expression is not value-dependent. */
24556 return false;
24559 /* Returns TRUE if the EXPRESSION is type-dependent, in the sense of
24560 [temp.dep.expr]. Note that an expression with no type is
24561 considered dependent. Other parts of the compiler arrange for an
24562 expression with type-dependent subexpressions to have no type, so
24563 this function doesn't have to be fully recursive. */
24565 bool
24566 type_dependent_expression_p (tree expression)
24568 if (!processing_template_decl)
24569 return false;
24571 if (expression == NULL_TREE || expression == error_mark_node)
24572 return false;
24574 STRIP_ANY_LOCATION_WRAPPER (expression);
24576 /* An unresolved name is always dependent. */
24577 if (identifier_p (expression)
24578 || TREE_CODE (expression) == USING_DECL
24579 || TREE_CODE (expression) == WILDCARD_DECL)
24580 return true;
24582 /* A fold expression is type-dependent. */
24583 if (TREE_CODE (expression) == UNARY_LEFT_FOLD_EXPR
24584 || TREE_CODE (expression) == UNARY_RIGHT_FOLD_EXPR
24585 || TREE_CODE (expression) == BINARY_LEFT_FOLD_EXPR
24586 || TREE_CODE (expression) == BINARY_RIGHT_FOLD_EXPR)
24587 return true;
24589 /* Some expression forms are never type-dependent. */
24590 if (TREE_CODE (expression) == PSEUDO_DTOR_EXPR
24591 || TREE_CODE (expression) == SIZEOF_EXPR
24592 || TREE_CODE (expression) == ALIGNOF_EXPR
24593 || TREE_CODE (expression) == AT_ENCODE_EXPR
24594 || TREE_CODE (expression) == NOEXCEPT_EXPR
24595 || TREE_CODE (expression) == TRAIT_EXPR
24596 || TREE_CODE (expression) == TYPEID_EXPR
24597 || TREE_CODE (expression) == DELETE_EXPR
24598 || TREE_CODE (expression) == VEC_DELETE_EXPR
24599 || TREE_CODE (expression) == THROW_EXPR
24600 || TREE_CODE (expression) == REQUIRES_EXPR)
24601 return false;
24603 /* The types of these expressions depends only on the type to which
24604 the cast occurs. */
24605 if (TREE_CODE (expression) == DYNAMIC_CAST_EXPR
24606 || TREE_CODE (expression) == STATIC_CAST_EXPR
24607 || TREE_CODE (expression) == CONST_CAST_EXPR
24608 || TREE_CODE (expression) == REINTERPRET_CAST_EXPR
24609 || TREE_CODE (expression) == IMPLICIT_CONV_EXPR
24610 || TREE_CODE (expression) == CAST_EXPR)
24611 return dependent_type_p (TREE_TYPE (expression));
24613 /* The types of these expressions depends only on the type created
24614 by the expression. */
24615 if (TREE_CODE (expression) == NEW_EXPR
24616 || TREE_CODE (expression) == VEC_NEW_EXPR)
24618 /* For NEW_EXPR tree nodes created inside a template, either
24619 the object type itself or a TREE_LIST may appear as the
24620 operand 1. */
24621 tree type = TREE_OPERAND (expression, 1);
24622 if (TREE_CODE (type) == TREE_LIST)
24623 /* This is an array type. We need to check array dimensions
24624 as well. */
24625 return dependent_type_p (TREE_VALUE (TREE_PURPOSE (type)))
24626 || value_dependent_expression_p
24627 (TREE_OPERAND (TREE_VALUE (type), 1));
24628 else
24629 return dependent_type_p (type);
24632 if (TREE_CODE (expression) == SCOPE_REF)
24634 tree scope = TREE_OPERAND (expression, 0);
24635 tree name = TREE_OPERAND (expression, 1);
24637 /* 14.6.2.2 [temp.dep.expr]: An id-expression is type-dependent if it
24638 contains an identifier associated by name lookup with one or more
24639 declarations declared with a dependent type, or...a
24640 nested-name-specifier or qualified-id that names a member of an
24641 unknown specialization. */
24642 return (type_dependent_expression_p (name)
24643 || dependent_scope_p (scope));
24646 if (TREE_CODE (expression) == TEMPLATE_DECL
24647 && !DECL_TEMPLATE_TEMPLATE_PARM_P (expression))
24648 return uses_outer_template_parms (expression);
24650 if (TREE_CODE (expression) == STMT_EXPR)
24651 expression = stmt_expr_value_expr (expression);
24653 if (BRACE_ENCLOSED_INITIALIZER_P (expression))
24655 tree elt;
24656 unsigned i;
24658 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (expression), i, elt)
24660 if (type_dependent_expression_p (elt))
24661 return true;
24663 return false;
24666 /* A static data member of the current instantiation with incomplete
24667 array type is type-dependent, as the definition and specializations
24668 can have different bounds. */
24669 if (VAR_P (expression)
24670 && DECL_CLASS_SCOPE_P (expression)
24671 && dependent_type_p (DECL_CONTEXT (expression))
24672 && VAR_HAD_UNKNOWN_BOUND (expression))
24673 return true;
24675 /* An array of unknown bound depending on a variadic parameter, eg:
24677 template<typename... Args>
24678 void foo (Args... args)
24680 int arr[] = { args... };
24683 template<int... vals>
24684 void bar ()
24686 int arr[] = { vals... };
24689 If the array has no length and has an initializer, it must be that
24690 we couldn't determine its length in cp_complete_array_type because
24691 it is dependent. */
24692 if (VAR_P (expression)
24693 && TREE_TYPE (expression) != NULL_TREE
24694 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE
24695 && !TYPE_DOMAIN (TREE_TYPE (expression))
24696 && DECL_INITIAL (expression))
24697 return true;
24699 /* A function or variable template-id is type-dependent if it has any
24700 dependent template arguments. */
24701 if (VAR_OR_FUNCTION_DECL_P (expression)
24702 && DECL_LANG_SPECIFIC (expression)
24703 && DECL_TEMPLATE_INFO (expression))
24705 /* Consider the innermost template arguments, since those are the ones
24706 that come from the template-id; the template arguments for the
24707 enclosing class do not make it type-dependent unless they are used in
24708 the type of the decl. */
24709 if (PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (expression))
24710 && (any_dependent_template_arguments_p
24711 (INNERMOST_TEMPLATE_ARGS (DECL_TI_ARGS (expression)))))
24712 return true;
24715 /* Otherwise, if the function decl isn't from a dependent scope, it can't be
24716 type-dependent. Checking this is important for functions with auto return
24717 type, which looks like a dependent type. */
24718 if (TREE_CODE (expression) == FUNCTION_DECL
24719 && !(DECL_CLASS_SCOPE_P (expression)
24720 && dependent_type_p (DECL_CONTEXT (expression)))
24721 && !(DECL_LANG_SPECIFIC (expression)
24722 && DECL_FRIEND_P (expression)
24723 && (!DECL_FRIEND_CONTEXT (expression)
24724 || dependent_type_p (DECL_FRIEND_CONTEXT (expression))))
24725 && !DECL_LOCAL_FUNCTION_P (expression))
24727 gcc_assert (!dependent_type_p (TREE_TYPE (expression))
24728 || undeduced_auto_decl (expression));
24729 return false;
24732 /* Always dependent, on the number of arguments if nothing else. */
24733 if (TREE_CODE (expression) == EXPR_PACK_EXPANSION)
24734 return true;
24736 if (TREE_TYPE (expression) == unknown_type_node)
24738 if (TREE_CODE (expression) == ADDR_EXPR)
24739 return type_dependent_expression_p (TREE_OPERAND (expression, 0));
24740 if (TREE_CODE (expression) == COMPONENT_REF
24741 || TREE_CODE (expression) == OFFSET_REF)
24743 if (type_dependent_expression_p (TREE_OPERAND (expression, 0)))
24744 return true;
24745 expression = TREE_OPERAND (expression, 1);
24746 if (identifier_p (expression))
24747 return false;
24749 /* SCOPE_REF with non-null TREE_TYPE is always non-dependent. */
24750 if (TREE_CODE (expression) == SCOPE_REF)
24751 return false;
24753 if (BASELINK_P (expression))
24755 if (BASELINK_OPTYPE (expression)
24756 && dependent_type_p (BASELINK_OPTYPE (expression)))
24757 return true;
24758 expression = BASELINK_FUNCTIONS (expression);
24761 if (TREE_CODE (expression) == TEMPLATE_ID_EXPR)
24763 if (any_dependent_template_arguments_p
24764 (TREE_OPERAND (expression, 1)))
24765 return true;
24766 expression = TREE_OPERAND (expression, 0);
24767 if (identifier_p (expression))
24768 return true;
24771 gcc_assert (TREE_CODE (expression) == OVERLOAD
24772 || TREE_CODE (expression) == FUNCTION_DECL);
24774 for (lkp_iterator iter (expression); iter; ++iter)
24775 if (type_dependent_expression_p (*iter))
24776 return true;
24778 return false;
24781 gcc_assert (TREE_CODE (expression) != TYPE_DECL);
24783 /* Dependent type attributes might not have made it from the decl to
24784 the type yet. */
24785 if (DECL_P (expression)
24786 && any_dependent_type_attributes_p (DECL_ATTRIBUTES (expression)))
24787 return true;
24789 return (dependent_type_p (TREE_TYPE (expression)));
24792 /* [temp.dep.expr]/5: A class member access expression (5.2.5) is
24793 type-dependent if the expression refers to a member of the current
24794 instantiation and the type of the referenced member is dependent, or the
24795 class member access expression refers to a member of an unknown
24796 specialization.
24798 This function returns true if the OBJECT in such a class member access
24799 expression is of an unknown specialization. */
24801 bool
24802 type_dependent_object_expression_p (tree object)
24804 /* An IDENTIFIER_NODE can sometimes have a TREE_TYPE, but it's still
24805 dependent. */
24806 if (TREE_CODE (object) == IDENTIFIER_NODE)
24807 return true;
24808 tree scope = TREE_TYPE (object);
24809 return (!scope || dependent_scope_p (scope));
24812 /* walk_tree callback function for instantiation_dependent_expression_p,
24813 below. Returns non-zero if a dependent subexpression is found. */
24815 static tree
24816 instantiation_dependent_r (tree *tp, int *walk_subtrees,
24817 void * /*data*/)
24819 if (TYPE_P (*tp))
24821 /* We don't have to worry about decltype currently because decltype
24822 of an instantiation-dependent expr is a dependent type. This
24823 might change depending on the resolution of DR 1172. */
24824 *walk_subtrees = false;
24825 return NULL_TREE;
24827 enum tree_code code = TREE_CODE (*tp);
24828 switch (code)
24830 /* Don't treat an argument list as dependent just because it has no
24831 TREE_TYPE. */
24832 case TREE_LIST:
24833 case TREE_VEC:
24834 return NULL_TREE;
24836 case TEMPLATE_PARM_INDEX:
24837 return *tp;
24839 /* Handle expressions with type operands. */
24840 case SIZEOF_EXPR:
24841 case ALIGNOF_EXPR:
24842 case TYPEID_EXPR:
24843 case AT_ENCODE_EXPR:
24845 tree op = TREE_OPERAND (*tp, 0);
24846 if (code == SIZEOF_EXPR && SIZEOF_EXPR_TYPE_P (*tp))
24847 op = TREE_TYPE (op);
24848 if (TYPE_P (op))
24850 if (dependent_type_p (op))
24851 return *tp;
24852 else
24854 *walk_subtrees = false;
24855 return NULL_TREE;
24858 break;
24861 case COMPONENT_REF:
24862 if (identifier_p (TREE_OPERAND (*tp, 1)))
24863 /* In a template, finish_class_member_access_expr creates a
24864 COMPONENT_REF with an IDENTIFIER_NODE for op1 even if it isn't
24865 type-dependent, so that we can check access control at
24866 instantiation time (PR 42277). See also Core issue 1273. */
24867 return *tp;
24868 break;
24870 case SCOPE_REF:
24871 if (instantiation_dependent_scope_ref_p (*tp))
24872 return *tp;
24873 else
24874 break;
24876 /* Treat statement-expressions as dependent. */
24877 case BIND_EXPR:
24878 return *tp;
24880 /* Treat requires-expressions as dependent. */
24881 case REQUIRES_EXPR:
24882 return *tp;
24884 case CALL_EXPR:
24885 /* Treat calls to function concepts as dependent. */
24886 if (function_concept_check_p (*tp))
24887 return *tp;
24888 break;
24890 case TEMPLATE_ID_EXPR:
24891 /* And variable concepts. */
24892 if (variable_concept_p (TREE_OPERAND (*tp, 0)))
24893 return *tp;
24894 break;
24896 default:
24897 break;
24900 if (type_dependent_expression_p (*tp))
24901 return *tp;
24902 else
24903 return NULL_TREE;
24906 /* Returns TRUE if the EXPRESSION is instantiation-dependent, in the
24907 sense defined by the ABI:
24909 "An expression is instantiation-dependent if it is type-dependent
24910 or value-dependent, or it has a subexpression that is type-dependent
24911 or value-dependent."
24913 Except don't actually check value-dependence for unevaluated expressions,
24914 because in sizeof(i) we don't care about the value of i. Checking
24915 type-dependence will in turn check value-dependence of array bounds/template
24916 arguments as needed. */
24918 bool
24919 instantiation_dependent_uneval_expression_p (tree expression)
24921 tree result;
24923 if (!processing_template_decl)
24924 return false;
24926 if (expression == error_mark_node)
24927 return false;
24929 result = cp_walk_tree_without_duplicates (&expression,
24930 instantiation_dependent_r, NULL);
24931 return result != NULL_TREE;
24934 /* As above, but also check value-dependence of the expression as a whole. */
24936 bool
24937 instantiation_dependent_expression_p (tree expression)
24939 return (instantiation_dependent_uneval_expression_p (expression)
24940 || value_dependent_expression_p (expression));
24943 /* Like type_dependent_expression_p, but it also works while not processing
24944 a template definition, i.e. during substitution or mangling. */
24946 bool
24947 type_dependent_expression_p_push (tree expr)
24949 bool b;
24950 ++processing_template_decl;
24951 b = type_dependent_expression_p (expr);
24952 --processing_template_decl;
24953 return b;
24956 /* Returns TRUE if ARGS contains a type-dependent expression. */
24958 bool
24959 any_type_dependent_arguments_p (const vec<tree, va_gc> *args)
24961 unsigned int i;
24962 tree arg;
24964 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
24966 if (type_dependent_expression_p (arg))
24967 return true;
24969 return false;
24972 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24973 expressions) contains any type-dependent expressions. */
24975 bool
24976 any_type_dependent_elements_p (const_tree list)
24978 for (; list; list = TREE_CHAIN (list))
24979 if (type_dependent_expression_p (TREE_VALUE (list)))
24980 return true;
24982 return false;
24985 /* Returns TRUE if LIST (a TREE_LIST whose TREE_VALUEs are
24986 expressions) contains any value-dependent expressions. */
24988 bool
24989 any_value_dependent_elements_p (const_tree list)
24991 for (; list; list = TREE_CHAIN (list))
24992 if (value_dependent_expression_p (TREE_VALUE (list)))
24993 return true;
24995 return false;
24998 /* Returns TRUE if the ARG (a template argument) is dependent. */
25000 bool
25001 dependent_template_arg_p (tree arg)
25003 if (!processing_template_decl)
25004 return false;
25006 /* Assume a template argument that was wrongly written by the user
25007 is dependent. This is consistent with what
25008 any_dependent_template_arguments_p [that calls this function]
25009 does. */
25010 if (!arg || arg == error_mark_node)
25011 return true;
25013 if (TREE_CODE (arg) == ARGUMENT_PACK_SELECT)
25014 arg = argument_pack_select_arg (arg);
25016 if (TREE_CODE (arg) == TEMPLATE_TEMPLATE_PARM)
25017 return true;
25018 if (TREE_CODE (arg) == TEMPLATE_DECL)
25020 if (DECL_TEMPLATE_PARM_P (arg))
25021 return true;
25022 /* A member template of a dependent class is not necessarily
25023 type-dependent, but it is a dependent template argument because it
25024 will be a member of an unknown specialization to that template. */
25025 tree scope = CP_DECL_CONTEXT (arg);
25026 return TYPE_P (scope) && dependent_type_p (scope);
25028 else if (ARGUMENT_PACK_P (arg))
25030 tree args = ARGUMENT_PACK_ARGS (arg);
25031 int i, len = TREE_VEC_LENGTH (args);
25032 for (i = 0; i < len; ++i)
25034 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
25035 return true;
25038 return false;
25040 else if (TYPE_P (arg))
25041 return dependent_type_p (arg);
25042 else
25043 return (type_dependent_expression_p (arg)
25044 || value_dependent_expression_p (arg));
25047 /* Returns true if ARGS (a collection of template arguments) contains
25048 any types that require structural equality testing. */
25050 bool
25051 any_template_arguments_need_structural_equality_p (tree args)
25053 int i;
25054 int j;
25056 if (!args)
25057 return false;
25058 if (args == error_mark_node)
25059 return true;
25061 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
25063 tree level = TMPL_ARGS_LEVEL (args, i + 1);
25064 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
25066 tree arg = TREE_VEC_ELT (level, j);
25067 tree packed_args = NULL_TREE;
25068 int k, len = 1;
25070 if (ARGUMENT_PACK_P (arg))
25072 /* Look inside the argument pack. */
25073 packed_args = ARGUMENT_PACK_ARGS (arg);
25074 len = TREE_VEC_LENGTH (packed_args);
25077 for (k = 0; k < len; ++k)
25079 if (packed_args)
25080 arg = TREE_VEC_ELT (packed_args, k);
25082 if (error_operand_p (arg))
25083 return true;
25084 else if (TREE_CODE (arg) == TEMPLATE_DECL)
25085 continue;
25086 else if (TYPE_P (arg) && TYPE_STRUCTURAL_EQUALITY_P (arg))
25087 return true;
25088 else if (!TYPE_P (arg) && TREE_TYPE (arg)
25089 && TYPE_STRUCTURAL_EQUALITY_P (TREE_TYPE (arg)))
25090 return true;
25095 return false;
25098 /* Returns true if ARGS (a collection of template arguments) contains
25099 any dependent arguments. */
25101 bool
25102 any_dependent_template_arguments_p (const_tree args)
25104 int i;
25105 int j;
25107 if (!args)
25108 return false;
25109 if (args == error_mark_node)
25110 return true;
25112 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
25114 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
25115 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
25116 if (dependent_template_arg_p (TREE_VEC_ELT (level, j)))
25117 return true;
25120 return false;
25123 /* Returns true if ARGS contains any errors. */
25125 bool
25126 any_erroneous_template_args_p (const_tree args)
25128 int i;
25129 int j;
25131 if (args == error_mark_node)
25132 return true;
25134 if (args && TREE_CODE (args) != TREE_VEC)
25136 if (tree ti = get_template_info (args))
25137 args = TI_ARGS (ti);
25138 else
25139 args = NULL_TREE;
25142 if (!args)
25143 return false;
25145 for (i = 0; i < TMPL_ARGS_DEPTH (args); ++i)
25147 const_tree level = TMPL_ARGS_LEVEL (args, i + 1);
25148 for (j = 0; j < TREE_VEC_LENGTH (level); ++j)
25149 if (error_operand_p (TREE_VEC_ELT (level, j)))
25150 return true;
25153 return false;
25156 /* Returns TRUE if the template TMPL is type-dependent. */
25158 bool
25159 dependent_template_p (tree tmpl)
25161 if (TREE_CODE (tmpl) == OVERLOAD)
25163 for (lkp_iterator iter (tmpl); iter; ++iter)
25164 if (dependent_template_p (*iter))
25165 return true;
25166 return false;
25169 /* Template template parameters are dependent. */
25170 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)
25171 || TREE_CODE (tmpl) == TEMPLATE_TEMPLATE_PARM)
25172 return true;
25173 /* So are names that have not been looked up. */
25174 if (TREE_CODE (tmpl) == SCOPE_REF || identifier_p (tmpl))
25175 return true;
25176 return false;
25179 /* Returns TRUE if the specialization TMPL<ARGS> is dependent. */
25181 bool
25182 dependent_template_id_p (tree tmpl, tree args)
25184 return (dependent_template_p (tmpl)
25185 || any_dependent_template_arguments_p (args));
25188 /* Returns TRUE if OMP_FOR with DECLV, INITV, CONDV and INCRV vectors
25189 are dependent. */
25191 bool
25192 dependent_omp_for_p (tree declv, tree initv, tree condv, tree incrv)
25194 int i;
25196 if (!processing_template_decl)
25197 return false;
25199 for (i = 0; i < TREE_VEC_LENGTH (declv); i++)
25201 tree decl = TREE_VEC_ELT (declv, i);
25202 tree init = TREE_VEC_ELT (initv, i);
25203 tree cond = TREE_VEC_ELT (condv, i);
25204 tree incr = TREE_VEC_ELT (incrv, i);
25206 if (type_dependent_expression_p (decl)
25207 || TREE_CODE (decl) == SCOPE_REF)
25208 return true;
25210 if (init && type_dependent_expression_p (init))
25211 return true;
25213 if (type_dependent_expression_p (cond))
25214 return true;
25216 if (COMPARISON_CLASS_P (cond)
25217 && (type_dependent_expression_p (TREE_OPERAND (cond, 0))
25218 || type_dependent_expression_p (TREE_OPERAND (cond, 1))))
25219 return true;
25221 if (TREE_CODE (incr) == MODOP_EXPR)
25223 if (type_dependent_expression_p (TREE_OPERAND (incr, 0))
25224 || type_dependent_expression_p (TREE_OPERAND (incr, 2)))
25225 return true;
25227 else if (type_dependent_expression_p (incr))
25228 return true;
25229 else if (TREE_CODE (incr) == MODIFY_EXPR)
25231 if (type_dependent_expression_p (TREE_OPERAND (incr, 0)))
25232 return true;
25233 else if (BINARY_CLASS_P (TREE_OPERAND (incr, 1)))
25235 tree t = TREE_OPERAND (incr, 1);
25236 if (type_dependent_expression_p (TREE_OPERAND (t, 0))
25237 || type_dependent_expression_p (TREE_OPERAND (t, 1)))
25238 return true;
25243 return false;
25246 /* TYPE is a TYPENAME_TYPE. Returns the ordinary TYPE to which the
25247 TYPENAME_TYPE corresponds. Returns the original TYPENAME_TYPE if
25248 no such TYPE can be found. Note that this function peers inside
25249 uninstantiated templates and therefore should be used only in
25250 extremely limited situations. ONLY_CURRENT_P restricts this
25251 peering to the currently open classes hierarchy (which is required
25252 when comparing types). */
25254 tree
25255 resolve_typename_type (tree type, bool only_current_p)
25257 tree scope;
25258 tree name;
25259 tree decl;
25260 int quals;
25261 tree pushed_scope;
25262 tree result;
25264 gcc_assert (TREE_CODE (type) == TYPENAME_TYPE);
25266 scope = TYPE_CONTEXT (type);
25267 /* We shouldn't have built a TYPENAME_TYPE with a non-dependent scope. */
25268 gcc_checking_assert (uses_template_parms (scope));
25270 /* Usually the non-qualified identifier of a TYPENAME_TYPE is
25271 TYPE_IDENTIFIER (type). But when 'type' is a typedef variant of
25272 a TYPENAME_TYPE node, then TYPE_NAME (type) is set to the TYPE_DECL representing
25273 the typedef. In that case TYPE_IDENTIFIER (type) is not the non-qualified
25274 identifier of the TYPENAME_TYPE anymore.
25275 So by getting the TYPE_IDENTIFIER of the _main declaration_ of the
25276 TYPENAME_TYPE instead, we avoid messing up with a possible
25277 typedef variant case. */
25278 name = TYPE_IDENTIFIER (TYPE_MAIN_VARIANT (type));
25280 /* If the SCOPE is itself a TYPENAME_TYPE, then we need to resolve
25281 it first before we can figure out what NAME refers to. */
25282 if (TREE_CODE (scope) == TYPENAME_TYPE)
25284 if (TYPENAME_IS_RESOLVING_P (scope))
25285 /* Given a class template A with a dependent base with nested type C,
25286 typedef typename A::C::C C will land us here, as trying to resolve
25287 the initial A::C leads to the local C typedef, which leads back to
25288 A::C::C. So we break the recursion now. */
25289 return type;
25290 else
25291 scope = resolve_typename_type (scope, only_current_p);
25293 /* If we don't know what SCOPE refers to, then we cannot resolve the
25294 TYPENAME_TYPE. */
25295 if (!CLASS_TYPE_P (scope))
25296 return type;
25297 /* If this is a typedef, we don't want to look inside (c++/11987). */
25298 if (typedef_variant_p (type))
25299 return type;
25300 /* If SCOPE isn't the template itself, it will not have a valid
25301 TYPE_FIELDS list. */
25302 if (same_type_p (scope, CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope)))
25303 /* scope is either the template itself or a compatible instantiation
25304 like X<T>, so look up the name in the original template. */
25305 scope = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (scope);
25306 /* If scope has no fields, it can't be a current instantiation. Check this
25307 before currently_open_class to avoid infinite recursion (71515). */
25308 if (!TYPE_FIELDS (scope))
25309 return type;
25310 /* If the SCOPE is not the current instantiation, there's no reason
25311 to look inside it. */
25312 if (only_current_p && !currently_open_class (scope))
25313 return type;
25314 /* Enter the SCOPE so that name lookup will be resolved as if we
25315 were in the class definition. In particular, SCOPE will no
25316 longer be considered a dependent type. */
25317 pushed_scope = push_scope (scope);
25318 /* Look up the declaration. */
25319 decl = lookup_member (scope, name, /*protect=*/0, /*want_type=*/true,
25320 tf_warning_or_error);
25322 result = NULL_TREE;
25324 /* For a TYPENAME_TYPE like "typename X::template Y<T>", we want to
25325 find a TEMPLATE_DECL. Otherwise, we want to find a TYPE_DECL. */
25326 tree fullname = TYPENAME_TYPE_FULLNAME (type);
25327 if (!decl)
25328 /*nop*/;
25329 else if (identifier_p (fullname)
25330 && TREE_CODE (decl) == TYPE_DECL)
25332 result = TREE_TYPE (decl);
25333 if (result == error_mark_node)
25334 result = NULL_TREE;
25336 else if (TREE_CODE (fullname) == TEMPLATE_ID_EXPR
25337 && DECL_CLASS_TEMPLATE_P (decl))
25339 /* Obtain the template and the arguments. */
25340 tree tmpl = TREE_OPERAND (fullname, 0);
25341 if (TREE_CODE (tmpl) == IDENTIFIER_NODE)
25343 /* We get here with a plain identifier because a previous tentative
25344 parse of the nested-name-specifier as part of a ptr-operator saw
25345 ::template X<A>. The use of ::template is necessary in a
25346 ptr-operator, but wrong in a declarator-id.
25348 [temp.names]: In a qualified-id of a declarator-id, the keyword
25349 template shall not appear at the top level. */
25350 pedwarn (EXPR_LOC_OR_LOC (fullname, input_location), OPT_Wpedantic,
25351 "keyword %<template%> not allowed in declarator-id");
25352 tmpl = decl;
25354 tree args = TREE_OPERAND (fullname, 1);
25355 /* Instantiate the template. */
25356 result = lookup_template_class (tmpl, args, NULL_TREE, NULL_TREE,
25357 /*entering_scope=*/true,
25358 tf_error | tf_user);
25359 if (result == error_mark_node)
25360 result = NULL_TREE;
25363 /* Leave the SCOPE. */
25364 if (pushed_scope)
25365 pop_scope (pushed_scope);
25367 /* If we failed to resolve it, return the original typename. */
25368 if (!result)
25369 return type;
25371 /* If lookup found a typename type, resolve that too. */
25372 if (TREE_CODE (result) == TYPENAME_TYPE && !TYPENAME_IS_RESOLVING_P (result))
25374 /* Ill-formed programs can cause infinite recursion here, so we
25375 must catch that. */
25376 TYPENAME_IS_RESOLVING_P (result) = 1;
25377 result = resolve_typename_type (result, only_current_p);
25378 TYPENAME_IS_RESOLVING_P (result) = 0;
25381 /* Qualify the resulting type. */
25382 quals = cp_type_quals (type);
25383 if (quals)
25384 result = cp_build_qualified_type (result, cp_type_quals (result) | quals);
25386 return result;
25389 /* EXPR is an expression which is not type-dependent. Return a proxy
25390 for EXPR that can be used to compute the types of larger
25391 expressions containing EXPR. */
25393 tree
25394 build_non_dependent_expr (tree expr)
25396 tree orig_expr = expr;
25397 tree inner_expr;
25399 /* When checking, try to get a constant value for all non-dependent
25400 expressions in order to expose bugs in *_dependent_expression_p
25401 and constexpr. This can affect code generation, see PR70704, so
25402 only do this for -fchecking=2. */
25403 if (flag_checking > 1
25404 && cxx_dialect >= cxx11
25405 /* Don't do this during nsdmi parsing as it can lead to
25406 unexpected recursive instantiations. */
25407 && !parsing_nsdmi ()
25408 /* Don't do this during concept expansion either and for
25409 the same reason. */
25410 && !expanding_concept ())
25411 fold_non_dependent_expr (expr);
25413 STRIP_ANY_LOCATION_WRAPPER (expr);
25415 /* Preserve OVERLOADs; the functions must be available to resolve
25416 types. */
25417 inner_expr = expr;
25418 if (TREE_CODE (inner_expr) == STMT_EXPR)
25419 inner_expr = stmt_expr_value_expr (inner_expr);
25420 if (TREE_CODE (inner_expr) == ADDR_EXPR)
25421 inner_expr = TREE_OPERAND (inner_expr, 0);
25422 if (TREE_CODE (inner_expr) == COMPONENT_REF)
25423 inner_expr = TREE_OPERAND (inner_expr, 1);
25424 if (is_overloaded_fn (inner_expr)
25425 || TREE_CODE (inner_expr) == OFFSET_REF)
25426 return orig_expr;
25427 /* There is no need to return a proxy for a variable. */
25428 if (VAR_P (expr))
25429 return orig_expr;
25430 /* Preserve string constants; conversions from string constants to
25431 "char *" are allowed, even though normally a "const char *"
25432 cannot be used to initialize a "char *". */
25433 if (TREE_CODE (expr) == STRING_CST)
25434 return orig_expr;
25435 /* Preserve void and arithmetic constants, as an optimization -- there is no
25436 reason to create a new node. */
25437 if (TREE_CODE (expr) == VOID_CST
25438 || TREE_CODE (expr) == INTEGER_CST
25439 || TREE_CODE (expr) == REAL_CST)
25440 return orig_expr;
25441 /* Preserve THROW_EXPRs -- all throw-expressions have type "void".
25442 There is at least one place where we want to know that a
25443 particular expression is a throw-expression: when checking a ?:
25444 expression, there are special rules if the second or third
25445 argument is a throw-expression. */
25446 if (TREE_CODE (expr) == THROW_EXPR)
25447 return orig_expr;
25449 /* Don't wrap an initializer list, we need to be able to look inside. */
25450 if (BRACE_ENCLOSED_INITIALIZER_P (expr))
25451 return orig_expr;
25453 /* Don't wrap a dummy object, we need to be able to test for it. */
25454 if (is_dummy_object (expr))
25455 return orig_expr;
25457 if (TREE_CODE (expr) == COND_EXPR)
25458 return build3 (COND_EXPR,
25459 TREE_TYPE (expr),
25460 TREE_OPERAND (expr, 0),
25461 (TREE_OPERAND (expr, 1)
25462 ? build_non_dependent_expr (TREE_OPERAND (expr, 1))
25463 : build_non_dependent_expr (TREE_OPERAND (expr, 0))),
25464 build_non_dependent_expr (TREE_OPERAND (expr, 2)));
25465 if (TREE_CODE (expr) == COMPOUND_EXPR
25466 && !COMPOUND_EXPR_OVERLOADED (expr))
25467 return build2 (COMPOUND_EXPR,
25468 TREE_TYPE (expr),
25469 TREE_OPERAND (expr, 0),
25470 build_non_dependent_expr (TREE_OPERAND (expr, 1)));
25472 /* If the type is unknown, it can't really be non-dependent */
25473 gcc_assert (TREE_TYPE (expr) != unknown_type_node);
25475 /* Otherwise, build a NON_DEPENDENT_EXPR. */
25476 return build1 (NON_DEPENDENT_EXPR, TREE_TYPE (expr), expr);
25479 /* ARGS is a vector of expressions as arguments to a function call.
25480 Replace the arguments with equivalent non-dependent expressions.
25481 This modifies ARGS in place. */
25483 void
25484 make_args_non_dependent (vec<tree, va_gc> *args)
25486 unsigned int ix;
25487 tree arg;
25489 FOR_EACH_VEC_SAFE_ELT (args, ix, arg)
25491 tree newarg = build_non_dependent_expr (arg);
25492 if (newarg != arg)
25493 (*args)[ix] = newarg;
25497 /* Returns a type which represents 'auto' or 'decltype(auto)'. We use a
25498 TEMPLATE_TYPE_PARM with a level one deeper than the actual template
25499 parms. If set_canonical is true, we set TYPE_CANONICAL on it. */
25501 static tree
25502 make_auto_1 (tree name, bool set_canonical)
25504 tree au = cxx_make_type (TEMPLATE_TYPE_PARM);
25505 TYPE_NAME (au) = build_decl (input_location,
25506 TYPE_DECL, name, au);
25507 TYPE_STUB_DECL (au) = TYPE_NAME (au);
25508 TEMPLATE_TYPE_PARM_INDEX (au) = build_template_parm_index
25509 (0, processing_template_decl + 1, processing_template_decl + 1,
25510 TYPE_NAME (au), NULL_TREE);
25511 if (set_canonical)
25512 TYPE_CANONICAL (au) = canonical_type_parameter (au);
25513 DECL_ARTIFICIAL (TYPE_NAME (au)) = 1;
25514 SET_DECL_TEMPLATE_PARM_P (TYPE_NAME (au));
25516 return au;
25519 tree
25520 make_decltype_auto (void)
25522 return make_auto_1 (decltype_auto_identifier, true);
25525 tree
25526 make_auto (void)
25528 return make_auto_1 (auto_identifier, true);
25531 /* Return a C++17 deduction placeholder for class template TMPL. */
25533 tree
25534 make_template_placeholder (tree tmpl)
25536 tree t = make_auto_1 (DECL_NAME (tmpl), true);
25537 CLASS_PLACEHOLDER_TEMPLATE (t) = tmpl;
25538 return t;
25541 /* True iff T is a C++17 class template deduction placeholder. */
25543 bool
25544 template_placeholder_p (tree t)
25546 return is_auto (t) && CLASS_PLACEHOLDER_TEMPLATE (t);
25549 /* Make a "constrained auto" type-specifier. This is an
25550 auto type with constraints that must be associated after
25551 deduction. The constraint is formed from the given
25552 CONC and its optional sequence of arguments, which are
25553 non-null if written as partial-concept-id. */
25555 tree
25556 make_constrained_auto (tree con, tree args)
25558 tree type = make_auto_1 (auto_identifier, false);
25560 /* Build the constraint. */
25561 tree tmpl = DECL_TI_TEMPLATE (con);
25562 tree expr = VAR_P (con) ? tmpl : ovl_make (tmpl);
25563 expr = build_concept_check (expr, type, args);
25565 tree constr = normalize_expression (expr);
25566 PLACEHOLDER_TYPE_CONSTRAINTS (type) = constr;
25568 /* Our canonical type depends on the constraint. */
25569 TYPE_CANONICAL (type) = canonical_type_parameter (type);
25571 /* Attach the constraint to the type declaration. */
25572 tree decl = TYPE_NAME (type);
25573 return decl;
25576 /* Given type ARG, return std::initializer_list<ARG>. */
25578 static tree
25579 listify (tree arg)
25581 tree std_init_list = get_namespace_binding (std_node, init_list_identifier);
25583 if (!std_init_list || !DECL_CLASS_TEMPLATE_P (std_init_list))
25585 gcc_rich_location richloc (input_location);
25586 maybe_add_include_fixit (&richloc, "<initializer_list>");
25587 error_at (&richloc,
25588 "deducing from brace-enclosed initializer list"
25589 " requires %<#include <initializer_list>%>");
25591 return error_mark_node;
25593 tree argvec = make_tree_vec (1);
25594 TREE_VEC_ELT (argvec, 0) = arg;
25596 return lookup_template_class (std_init_list, argvec, NULL_TREE,
25597 NULL_TREE, 0, tf_warning_or_error);
25600 /* Replace auto in TYPE with std::initializer_list<auto>. */
25602 static tree
25603 listify_autos (tree type, tree auto_node)
25605 tree init_auto = listify (auto_node);
25606 tree argvec = make_tree_vec (1);
25607 TREE_VEC_ELT (argvec, 0) = init_auto;
25608 if (processing_template_decl)
25609 argvec = add_to_template_args (current_template_args (), argvec);
25610 return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
25613 /* Hash traits for hashing possibly constrained 'auto'
25614 TEMPLATE_TYPE_PARMs for use by do_auto_deduction. */
25616 struct auto_hash : default_hash_traits<tree>
25618 static inline hashval_t hash (tree);
25619 static inline bool equal (tree, tree);
25622 /* Hash the 'auto' T. */
25624 inline hashval_t
25625 auto_hash::hash (tree t)
25627 if (tree c = PLACEHOLDER_TYPE_CONSTRAINTS (t))
25628 /* Matching constrained-type-specifiers denote the same template
25629 parameter, so hash the constraint. */
25630 return hash_placeholder_constraint (c);
25631 else
25632 /* But unconstrained autos are all separate, so just hash the pointer. */
25633 return iterative_hash_object (t, 0);
25636 /* Compare two 'auto's. */
25638 inline bool
25639 auto_hash::equal (tree t1, tree t2)
25641 if (t1 == t2)
25642 return true;
25644 tree c1 = PLACEHOLDER_TYPE_CONSTRAINTS (t1);
25645 tree c2 = PLACEHOLDER_TYPE_CONSTRAINTS (t2);
25647 /* Two unconstrained autos are distinct. */
25648 if (!c1 || !c2)
25649 return false;
25651 return equivalent_placeholder_constraints (c1, c2);
25654 /* for_each_template_parm callback for extract_autos: if t is a (possibly
25655 constrained) auto, add it to the vector. */
25657 static int
25658 extract_autos_r (tree t, void *data)
25660 hash_table<auto_hash> &hash = *(hash_table<auto_hash>*)data;
25661 if (is_auto (t))
25663 /* All the autos were built with index 0; fix that up now. */
25664 tree *p = hash.find_slot (t, INSERT);
25665 unsigned idx;
25666 if (*p)
25667 /* If this is a repeated constrained-type-specifier, use the index we
25668 chose before. */
25669 idx = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (*p));
25670 else
25672 /* Otherwise this is new, so use the current count. */
25673 *p = t;
25674 idx = hash.elements () - 1;
25676 TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (t)) = idx;
25679 /* Always keep walking. */
25680 return 0;
25683 /* Return a TREE_VEC of the 'auto's used in type under the Concepts TS, which
25684 says they can appear anywhere in the type. */
25686 static tree
25687 extract_autos (tree type)
25689 hash_set<tree> visited;
25690 hash_table<auto_hash> hash (2);
25692 for_each_template_parm (type, extract_autos_r, &hash, &visited, true);
25694 tree tree_vec = make_tree_vec (hash.elements());
25695 for (hash_table<auto_hash>::iterator iter = hash.begin();
25696 iter != hash.end(); ++iter)
25698 tree elt = *iter;
25699 unsigned i = TEMPLATE_PARM_IDX (TEMPLATE_TYPE_PARM_INDEX (elt));
25700 TREE_VEC_ELT (tree_vec, i)
25701 = build_tree_list (NULL_TREE, TYPE_NAME (elt));
25704 return tree_vec;
25707 /* The stem for deduction guide names. */
25708 const char *const dguide_base = "__dguide_";
25710 /* Return the name for a deduction guide for class template TMPL. */
25712 tree
25713 dguide_name (tree tmpl)
25715 tree type = (TYPE_P (tmpl) ? tmpl : TREE_TYPE (tmpl));
25716 tree tname = TYPE_IDENTIFIER (type);
25717 char *buf = (char *) alloca (1 + strlen (dguide_base)
25718 + IDENTIFIER_LENGTH (tname));
25719 memcpy (buf, dguide_base, strlen (dguide_base));
25720 memcpy (buf + strlen (dguide_base), IDENTIFIER_POINTER (tname),
25721 IDENTIFIER_LENGTH (tname) + 1);
25722 tree dname = get_identifier (buf);
25723 TREE_TYPE (dname) = type;
25724 return dname;
25727 /* True if NAME is the name of a deduction guide. */
25729 bool
25730 dguide_name_p (tree name)
25732 return (TREE_CODE (name) == IDENTIFIER_NODE
25733 && TREE_TYPE (name)
25734 && !strncmp (IDENTIFIER_POINTER (name), dguide_base,
25735 strlen (dguide_base)));
25738 /* True if FN is a deduction guide. */
25740 bool
25741 deduction_guide_p (const_tree fn)
25743 if (DECL_P (fn))
25744 if (tree name = DECL_NAME (fn))
25745 return dguide_name_p (name);
25746 return false;
25749 /* True if FN is the copy deduction guide, i.e. A(A)->A. */
25751 bool
25752 copy_guide_p (const_tree fn)
25754 gcc_assert (deduction_guide_p (fn));
25755 if (!DECL_ARTIFICIAL (fn))
25756 return false;
25757 tree parms = FUNCTION_FIRST_USER_PARMTYPE (DECL_TI_TEMPLATE (fn));
25758 return (TREE_CHAIN (parms) == void_list_node
25759 && same_type_p (TREE_VALUE (parms), TREE_TYPE (DECL_NAME (fn))));
25762 /* True if FN is a guide generated from a constructor template. */
25764 bool
25765 template_guide_p (const_tree fn)
25767 gcc_assert (deduction_guide_p (fn));
25768 if (!DECL_ARTIFICIAL (fn))
25769 return false;
25770 tree tmpl = DECL_TI_TEMPLATE (fn);
25771 if (tree org = DECL_ABSTRACT_ORIGIN (tmpl))
25772 return PRIMARY_TEMPLATE_P (org);
25773 return false;
25776 /* OLDDECL is a _DECL for a template parameter. Return a similar parameter at
25777 LEVEL:INDEX, using tsubst_args and complain for substitution into non-type
25778 template parameter types. Note that the handling of template template
25779 parameters relies on current_template_parms being set appropriately for the
25780 new template. */
25782 static tree
25783 rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
25784 tree tsubst_args, tsubst_flags_t complain)
25786 tree oldidx = get_template_parm_index (olddecl);
25788 tree newtype;
25789 if (TREE_CODE (olddecl) == TYPE_DECL
25790 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25792 tree oldtype = TREE_TYPE (olddecl);
25793 newtype = cxx_make_type (TREE_CODE (oldtype));
25794 TYPE_MAIN_VARIANT (newtype) = newtype;
25795 if (TREE_CODE (oldtype) == TEMPLATE_TYPE_PARM)
25796 TEMPLATE_TYPE_PARM_FOR_CLASS (newtype)
25797 = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
25799 else
25801 newtype = TREE_TYPE (olddecl);
25802 if (type_uses_auto (newtype))
25804 // Substitute once to fix references to other template parameters.
25805 newtype = tsubst (newtype, tsubst_args,
25806 complain|tf_partial, NULL_TREE);
25807 // Now substitute again to reduce the level of the auto.
25808 newtype = tsubst (newtype, current_template_args (),
25809 complain, NULL_TREE);
25811 else
25812 newtype = tsubst (newtype, tsubst_args,
25813 complain, NULL_TREE);
25816 tree newdecl
25817 = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
25818 DECL_NAME (olddecl), newtype);
25819 SET_DECL_TEMPLATE_PARM_P (newdecl);
25821 tree newidx;
25822 if (TREE_CODE (olddecl) == TYPE_DECL
25823 || TREE_CODE (olddecl) == TEMPLATE_DECL)
25825 newidx = TEMPLATE_TYPE_PARM_INDEX (newtype)
25826 = build_template_parm_index (index, level, level,
25827 newdecl, newtype);
25828 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25829 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25830 TYPE_STUB_DECL (newtype) = TYPE_NAME (newtype) = newdecl;
25831 TYPE_CANONICAL (newtype) = canonical_type_parameter (newtype);
25833 if (TREE_CODE (olddecl) == TEMPLATE_DECL)
25835 DECL_TEMPLATE_RESULT (newdecl)
25836 = build_decl (DECL_SOURCE_LOCATION (olddecl), TYPE_DECL,
25837 DECL_NAME (olddecl), newtype);
25838 DECL_ARTIFICIAL (DECL_TEMPLATE_RESULT (newdecl)) = true;
25839 // First create a copy (ttargs) of tsubst_args with an
25840 // additional level for the template template parameter's own
25841 // template parameters (ttparms).
25842 tree ttparms = (INNERMOST_TEMPLATE_PARMS
25843 (DECL_TEMPLATE_PARMS (olddecl)));
25844 const int depth = TMPL_ARGS_DEPTH (tsubst_args);
25845 tree ttargs = make_tree_vec (depth + 1);
25846 for (int i = 0; i < depth; ++i)
25847 TREE_VEC_ELT (ttargs, i) = TREE_VEC_ELT (tsubst_args, i);
25848 TREE_VEC_ELT (ttargs, depth)
25849 = template_parms_level_to_args (ttparms);
25850 // Substitute ttargs into ttparms to fix references to
25851 // other template parameters.
25852 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25853 complain|tf_partial);
25854 // Now substitute again with args based on tparms, to reduce
25855 // the level of the ttparms.
25856 ttargs = current_template_args ();
25857 ttparms = tsubst_template_parms_level (ttparms, ttargs,
25858 complain);
25859 // Finally, tack the adjusted parms onto tparms.
25860 ttparms = tree_cons (size_int (depth), ttparms,
25861 current_template_parms);
25862 DECL_TEMPLATE_PARMS (newdecl) = ttparms;
25865 else
25867 tree oldconst = TEMPLATE_PARM_DECL (oldidx);
25868 tree newconst
25869 = build_decl (DECL_SOURCE_LOCATION (oldconst),
25870 TREE_CODE (oldconst),
25871 DECL_NAME (oldconst), newtype);
25872 TREE_CONSTANT (newconst) = TREE_CONSTANT (newdecl)
25873 = TREE_READONLY (newconst) = TREE_READONLY (newdecl) = true;
25874 SET_DECL_TEMPLATE_PARM_P (newconst);
25875 newidx = build_template_parm_index (index, level, level,
25876 newconst, newtype);
25877 TEMPLATE_PARM_PARAMETER_PACK (newidx)
25878 = TEMPLATE_PARM_PARAMETER_PACK (oldidx);
25879 DECL_INITIAL (newdecl) = DECL_INITIAL (newconst) = newidx;
25882 return newdecl;
25885 /* Returns a C++17 class deduction guide template based on the constructor
25886 CTOR. As a special case, CTOR can be a RECORD_TYPE for an implicit default
25887 guide, or REFERENCE_TYPE for an implicit copy/move guide. */
25889 static tree
25890 build_deduction_guide (tree ctor, tree outer_args, tsubst_flags_t complain)
25892 tree type, tparms, targs, fparms, fargs, ci;
25893 bool memtmpl = false;
25894 bool explicit_p;
25895 location_t loc;
25896 tree fn_tmpl = NULL_TREE;
25898 if (TYPE_P (ctor))
25900 type = ctor;
25901 bool copy_p = TREE_CODE (type) == REFERENCE_TYPE;
25902 if (copy_p)
25904 type = TREE_TYPE (type);
25905 fparms = tree_cons (NULL_TREE, type, void_list_node);
25907 else
25908 fparms = void_list_node;
25910 tree ctmpl = CLASSTYPE_TI_TEMPLATE (type);
25911 tparms = DECL_TEMPLATE_PARMS (ctmpl);
25912 targs = CLASSTYPE_TI_ARGS (type);
25913 ci = NULL_TREE;
25914 fargs = NULL_TREE;
25915 loc = DECL_SOURCE_LOCATION (ctmpl);
25916 explicit_p = false;
25918 else
25920 ++processing_template_decl;
25922 fn_tmpl
25923 = (TREE_CODE (ctor) == TEMPLATE_DECL ? ctor
25924 : DECL_TI_TEMPLATE (ctor));
25925 if (outer_args)
25926 fn_tmpl = tsubst (fn_tmpl, outer_args, complain, ctor);
25927 ctor = DECL_TEMPLATE_RESULT (fn_tmpl);
25929 type = DECL_CONTEXT (ctor);
25931 tparms = DECL_TEMPLATE_PARMS (fn_tmpl);
25932 /* If type is a member class template, DECL_TI_ARGS (ctor) will have
25933 fully specialized args for the enclosing class. Strip those off, as
25934 the deduction guide won't have those template parameters. */
25935 targs = get_innermost_template_args (DECL_TI_ARGS (ctor),
25936 TMPL_PARMS_DEPTH (tparms));
25937 /* Discard the 'this' parameter. */
25938 fparms = FUNCTION_ARG_CHAIN (ctor);
25939 fargs = TREE_CHAIN (DECL_ARGUMENTS (ctor));
25940 ci = get_constraints (ctor);
25941 loc = DECL_SOURCE_LOCATION (ctor);
25942 explicit_p = DECL_NONCONVERTING_P (ctor);
25944 if (PRIMARY_TEMPLATE_P (fn_tmpl))
25946 memtmpl = true;
25948 /* For a member template constructor, we need to flatten the two
25949 template parameter lists into one, and then adjust the function
25950 signature accordingly. This gets...complicated. */
25951 tree save_parms = current_template_parms;
25953 /* For a member template we should have two levels of parms/args, one
25954 for the class and one for the constructor. We stripped
25955 specialized args for further enclosing classes above. */
25956 const int depth = 2;
25957 gcc_assert (TMPL_ARGS_DEPTH (targs) == depth);
25959 /* Template args for translating references to the two-level template
25960 parameters into references to the one-level template parameters we
25961 are creating. */
25962 tree tsubst_args = copy_node (targs);
25963 TMPL_ARGS_LEVEL (tsubst_args, depth)
25964 = copy_node (TMPL_ARGS_LEVEL (tsubst_args, depth));
25966 /* Template parms for the constructor template. */
25967 tree ftparms = TREE_VALUE (tparms);
25968 unsigned flen = TREE_VEC_LENGTH (ftparms);
25969 /* Template parms for the class template. */
25970 tparms = TREE_CHAIN (tparms);
25971 tree ctparms = TREE_VALUE (tparms);
25972 unsigned clen = TREE_VEC_LENGTH (ctparms);
25973 /* Template parms for the deduction guide start as a copy of the
25974 template parms for the class. We set current_template_parms for
25975 lookup_template_class_1. */
25976 current_template_parms = tparms = copy_node (tparms);
25977 tree new_vec = TREE_VALUE (tparms) = make_tree_vec (flen + clen);
25978 for (unsigned i = 0; i < clen; ++i)
25979 TREE_VEC_ELT (new_vec, i) = TREE_VEC_ELT (ctparms, i);
25981 /* Now we need to rewrite the constructor parms to append them to the
25982 class parms. */
25983 for (unsigned i = 0; i < flen; ++i)
25985 unsigned index = i + clen;
25986 unsigned level = 1;
25987 tree oldelt = TREE_VEC_ELT (ftparms, i);
25988 tree olddecl = TREE_VALUE (oldelt);
25989 tree newdecl = rewrite_template_parm (olddecl, index, level,
25990 tsubst_args, complain);
25991 tree newdef = tsubst_template_arg (TREE_PURPOSE (oldelt),
25992 tsubst_args, complain, ctor);
25993 tree list = build_tree_list (newdef, newdecl);
25994 TEMPLATE_PARM_CONSTRAINTS (list)
25995 = tsubst_constraint_info (TEMPLATE_PARM_CONSTRAINTS (oldelt),
25996 tsubst_args, complain, ctor);
25997 TREE_VEC_ELT (new_vec, index) = list;
25998 TMPL_ARG (tsubst_args, depth, i) = template_parm_to_arg (list);
26001 /* Now we have a final set of template parms to substitute into the
26002 function signature. */
26003 targs = template_parms_to_args (tparms);
26004 fparms = tsubst_arg_types (fparms, tsubst_args, NULL_TREE,
26005 complain, ctor);
26006 fargs = tsubst (fargs, tsubst_args, complain, ctor);
26007 if (ci)
26008 ci = tsubst_constraint_info (ci, tsubst_args, complain, ctor);
26010 current_template_parms = save_parms;
26012 --processing_template_decl;
26015 if (!memtmpl)
26017 /* Copy the parms so we can set DECL_PRIMARY_TEMPLATE. */
26018 tparms = copy_node (tparms);
26019 INNERMOST_TEMPLATE_PARMS (tparms)
26020 = copy_node (INNERMOST_TEMPLATE_PARMS (tparms));
26023 tree fntype = build_function_type (type, fparms);
26024 tree ded_fn = build_lang_decl_loc (loc,
26025 FUNCTION_DECL,
26026 dguide_name (type), fntype);
26027 DECL_ARGUMENTS (ded_fn) = fargs;
26028 DECL_ARTIFICIAL (ded_fn) = true;
26029 DECL_NONCONVERTING_P (ded_fn) = explicit_p;
26030 tree ded_tmpl = build_template_decl (ded_fn, tparms, /*member*/false);
26031 DECL_ARTIFICIAL (ded_tmpl) = true;
26032 DECL_TEMPLATE_RESULT (ded_tmpl) = ded_fn;
26033 TREE_TYPE (ded_tmpl) = TREE_TYPE (ded_fn);
26034 DECL_TEMPLATE_INFO (ded_fn) = build_template_info (ded_tmpl, targs);
26035 DECL_PRIMARY_TEMPLATE (ded_tmpl) = ded_tmpl;
26036 if (DECL_P (ctor))
26037 DECL_ABSTRACT_ORIGIN (ded_tmpl) = fn_tmpl;
26038 if (ci)
26039 set_constraints (ded_tmpl, ci);
26041 return ded_tmpl;
26044 /* Deduce template arguments for the class template placeholder PTYPE for
26045 template TMPL based on the initializer INIT, and return the resulting
26046 type. */
26048 static tree
26049 do_class_deduction (tree ptype, tree tmpl, tree init, int flags,
26050 tsubst_flags_t complain)
26052 if (!DECL_CLASS_TEMPLATE_P (tmpl))
26054 /* We should have handled this in the caller. */
26055 if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26056 return ptype;
26057 if (complain & tf_error)
26058 error ("non-class template %qT used without template arguments", tmpl);
26059 return error_mark_node;
26062 tree type = TREE_TYPE (tmpl);
26064 bool try_list_ctor = false;
26066 vec<tree,va_gc> *args;
26067 if (init == NULL_TREE
26068 || TREE_CODE (init) == TREE_LIST)
26069 args = make_tree_vector_from_list (init);
26070 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
26072 try_list_ctor = TYPE_HAS_LIST_CTOR (type);
26073 if (try_list_ctor && CONSTRUCTOR_NELTS (init) == 1)
26075 /* As an exception, the first phase in 16.3.1.7 (considering the
26076 initializer list as a single argument) is omitted if the
26077 initializer list consists of a single expression of type cv U,
26078 where U is a specialization of C or a class derived from a
26079 specialization of C. */
26080 tree elt = CONSTRUCTOR_ELT (init, 0)->value;
26081 tree etype = TREE_TYPE (elt);
26083 tree tparms = INNERMOST_TEMPLATE_PARMS (DECL_TEMPLATE_PARMS (tmpl));
26084 tree targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
26085 int err = unify (tparms, targs, type, etype,
26086 UNIFY_ALLOW_DERIVED, /*explain*/false);
26087 if (err == 0)
26088 try_list_ctor = false;
26089 ggc_free (targs);
26091 if (try_list_ctor || is_std_init_list (type))
26092 args = make_tree_vector_single (init);
26093 else
26094 args = make_tree_vector_from_ctor (init);
26096 else
26097 args = make_tree_vector_single (init);
26099 tree dname = dguide_name (tmpl);
26100 tree cands = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dname,
26101 /*type*/false, /*complain*/false,
26102 /*hidden*/false);
26103 bool elided = false;
26104 if (cands == error_mark_node)
26105 cands = NULL_TREE;
26107 /* Prune explicit deduction guides in copy-initialization context. */
26108 if (flags & LOOKUP_ONLYCONVERTING)
26110 for (lkp_iterator iter (cands); !elided && iter; ++iter)
26111 if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
26112 elided = true;
26114 if (elided)
26116 /* Found a nonconverting guide, prune the candidates. */
26117 tree pruned = NULL_TREE;
26118 for (lkp_iterator iter (cands); iter; ++iter)
26119 if (!DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter)))
26120 pruned = lookup_add (*iter, pruned);
26122 cands = pruned;
26126 tree outer_args = NULL_TREE;
26127 if (DECL_CLASS_SCOPE_P (tmpl)
26128 && CLASSTYPE_TEMPLATE_INFO (DECL_CONTEXT (tmpl)))
26130 outer_args = CLASSTYPE_TI_ARGS (DECL_CONTEXT (tmpl));
26131 type = TREE_TYPE (most_general_template (tmpl));
26134 bool saw_ctor = false;
26135 // FIXME cache artificial deduction guides
26136 for (ovl_iterator iter (CLASSTYPE_CONSTRUCTORS (type)); iter; ++iter)
26138 tree guide = build_deduction_guide (*iter, outer_args, complain);
26139 if ((flags & LOOKUP_ONLYCONVERTING)
26140 && DECL_NONCONVERTING_P (STRIP_TEMPLATE (guide)))
26141 elided = true;
26142 else
26143 cands = lookup_add (guide, cands);
26145 saw_ctor = true;
26148 tree call = error_mark_node;
26150 /* If this is list-initialization and the class has a list constructor, first
26151 try deducing from the list as a single argument, as [over.match.list]. */
26152 tree list_cands = NULL_TREE;
26153 if (try_list_ctor && cands)
26154 for (lkp_iterator iter (cands); iter; ++iter)
26156 tree dg = *iter;
26157 if (is_list_ctor (dg))
26158 list_cands = lookup_add (dg, list_cands);
26160 if (list_cands)
26162 ++cp_unevaluated_operand;
26163 call = build_new_function_call (list_cands, &args, tf_decltype);
26164 --cp_unevaluated_operand;
26166 if (call == error_mark_node)
26168 /* That didn't work, now try treating the list as a sequence of
26169 arguments. */
26170 release_tree_vector (args);
26171 args = make_tree_vector_from_ctor (init);
26175 /* Maybe generate an implicit deduction guide. */
26176 if (call == error_mark_node && args->length () < 2)
26178 tree gtype = NULL_TREE;
26180 if (args->length () == 1)
26181 /* Generate a copy guide. */
26182 gtype = build_reference_type (type);
26183 else if (!saw_ctor)
26184 /* Generate a default guide. */
26185 gtype = type;
26187 if (gtype)
26189 tree guide = build_deduction_guide (gtype, outer_args, complain);
26190 cands = lookup_add (guide, cands);
26194 if (elided && !cands)
26196 error ("cannot deduce template arguments for copy-initialization"
26197 " of %qT, as it has no non-explicit deduction guides or "
26198 "user-declared constructors", type);
26199 return error_mark_node;
26201 else if (!cands && call == error_mark_node)
26203 error ("cannot deduce template arguments of %qT, as it has no viable "
26204 "deduction guides", type);
26205 return error_mark_node;
26208 if (call == error_mark_node)
26210 ++cp_unevaluated_operand;
26211 call = build_new_function_call (cands, &args, tf_decltype);
26212 --cp_unevaluated_operand;
26215 if (call == error_mark_node && (complain & tf_warning_or_error))
26217 error ("class template argument deduction failed:");
26219 ++cp_unevaluated_operand;
26220 call = build_new_function_call (cands, &args, complain | tf_decltype);
26221 --cp_unevaluated_operand;
26223 if (elided)
26224 inform (input_location, "explicit deduction guides not considered "
26225 "for copy-initialization");
26228 release_tree_vector (args);
26230 return cp_build_qualified_type (TREE_TYPE (call), cp_type_quals (ptype));
26233 /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced
26234 from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE.
26235 The CONTEXT determines the context in which auto deduction is performed
26236 and is used to control error diagnostics. FLAGS are the LOOKUP_* flags.
26237 OUTER_TARGS are used during template argument deduction
26238 (context == adc_unify) to properly substitute the result, and is ignored
26239 in other contexts.
26241 For partial-concept-ids, extra args may be appended to the list of deduced
26242 template arguments prior to determining constraint satisfaction. */
26244 tree
26245 do_auto_deduction (tree type, tree init, tree auto_node,
26246 tsubst_flags_t complain, auto_deduction_context context,
26247 tree outer_targs, int flags)
26249 tree targs;
26251 if (init == error_mark_node)
26252 return error_mark_node;
26254 if (init && type_dependent_expression_p (init)
26255 && context != adc_unify)
26256 /* Defining a subset of type-dependent expressions that we can deduce
26257 from ahead of time isn't worth the trouble. */
26258 return type;
26260 /* Similarly, we can't deduce from another undeduced decl. */
26261 if (init && undeduced_auto_decl (init))
26262 return type;
26264 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
26265 /* C++17 class template argument deduction. */
26266 return do_class_deduction (type, tmpl, init, flags, complain);
26268 if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE)
26269 /* Nothing we can do with this, even in deduction context. */
26270 return type;
26272 /* [dcl.spec.auto]: Obtain P from T by replacing the occurrences of auto
26273 with either a new invented type template parameter U or, if the
26274 initializer is a braced-init-list (8.5.4), with
26275 std::initializer_list<U>. */
26276 if (BRACE_ENCLOSED_INITIALIZER_P (init))
26278 if (!DIRECT_LIST_INIT_P (init))
26279 type = listify_autos (type, auto_node);
26280 else if (CONSTRUCTOR_NELTS (init) == 1)
26281 init = CONSTRUCTOR_ELT (init, 0)->value;
26282 else
26284 if (complain & tf_warning_or_error)
26286 if (permerror (input_location, "direct-list-initialization of "
26287 "%<auto%> requires exactly one element"))
26288 inform (input_location,
26289 "for deduction to %<std::initializer_list%>, use copy-"
26290 "list-initialization (i.e. add %<=%> before the %<{%>)");
26292 type = listify_autos (type, auto_node);
26296 if (type == error_mark_node)
26297 return error_mark_node;
26299 init = resolve_nondeduced_context (init, complain);
26301 if (context == adc_decomp_type
26302 && auto_node == type
26303 && init != error_mark_node
26304 && TREE_CODE (TREE_TYPE (init)) == ARRAY_TYPE)
26305 /* [dcl.decomp]/1 - if decomposition declaration has no ref-qualifiers
26306 and initializer has array type, deduce cv-qualified array type. */
26307 return cp_build_qualified_type_real (TREE_TYPE (init), TYPE_QUALS (type),
26308 complain);
26309 else if (AUTO_IS_DECLTYPE (auto_node))
26311 bool id = (DECL_P (init)
26312 || ((TREE_CODE (init) == COMPONENT_REF
26313 || TREE_CODE (init) == SCOPE_REF)
26314 && !REF_PARENTHESIZED_P (init)));
26315 targs = make_tree_vec (1);
26316 TREE_VEC_ELT (targs, 0)
26317 = finish_decltype_type (init, id, tf_warning_or_error);
26318 if (type != auto_node)
26320 if (complain & tf_error)
26321 error ("%qT as type rather than plain %<decltype(auto)%>", type);
26322 return error_mark_node;
26325 else
26327 tree parms = build_tree_list (NULL_TREE, type);
26328 tree tparms;
26330 if (flag_concepts)
26331 tparms = extract_autos (type);
26332 else
26334 tparms = make_tree_vec (1);
26335 TREE_VEC_ELT (tparms, 0)
26336 = build_tree_list (NULL_TREE, TYPE_NAME (auto_node));
26339 targs = make_tree_vec (TREE_VEC_LENGTH (tparms));
26340 int val = type_unification_real (tparms, targs, parms, &init, 1, 0,
26341 DEDUCE_CALL, LOOKUP_NORMAL,
26342 NULL, /*explain_p=*/false);
26343 if (val > 0)
26345 if (processing_template_decl)
26346 /* Try again at instantiation time. */
26347 return type;
26348 if (type && type != error_mark_node
26349 && (complain & tf_error))
26350 /* If type is error_mark_node a diagnostic must have been
26351 emitted by now. Also, having a mention to '<type error>'
26352 in the diagnostic is not really useful to the user. */
26354 if (cfun && auto_node == current_function_auto_return_pattern
26355 && LAMBDA_FUNCTION_P (current_function_decl))
26356 error ("unable to deduce lambda return type from %qE", init);
26357 else
26358 error ("unable to deduce %qT from %qE", type, init);
26359 type_unification_real (tparms, targs, parms, &init, 1, 0,
26360 DEDUCE_CALL, LOOKUP_NORMAL,
26361 NULL, /*explain_p=*/true);
26363 return error_mark_node;
26367 /* Check any placeholder constraints against the deduced type. */
26368 if (flag_concepts && !processing_template_decl)
26369 if (tree constr = PLACEHOLDER_TYPE_CONSTRAINTS (auto_node))
26371 /* Use the deduced type to check the associated constraints. If we
26372 have a partial-concept-id, rebuild the argument list so that
26373 we check using the extra arguments. */
26374 gcc_assert (TREE_CODE (constr) == CHECK_CONSTR);
26375 tree cargs = CHECK_CONSTR_ARGS (constr);
26376 if (TREE_VEC_LENGTH (cargs) > 1)
26378 cargs = copy_node (cargs);
26379 TREE_VEC_ELT (cargs, 0) = TREE_VEC_ELT (targs, 0);
26381 else
26382 cargs = targs;
26383 if (!constraints_satisfied_p (constr, cargs))
26385 if (complain & tf_warning_or_error)
26387 switch (context)
26389 case adc_unspecified:
26390 case adc_unify:
26391 error("placeholder constraints not satisfied");
26392 break;
26393 case adc_variable_type:
26394 case adc_decomp_type:
26395 error ("deduced initializer does not satisfy "
26396 "placeholder constraints");
26397 break;
26398 case adc_return_type:
26399 error ("deduced return type does not satisfy "
26400 "placeholder constraints");
26401 break;
26402 case adc_requirement:
26403 error ("deduced expression type does not satisfy "
26404 "placeholder constraints");
26405 break;
26407 diagnose_constraints (input_location, constr, targs);
26409 return error_mark_node;
26413 if (processing_template_decl && context != adc_unify)
26414 outer_targs = current_template_args ();
26415 targs = add_to_template_args (outer_targs, targs);
26416 return tsubst (type, targs, complain, NULL_TREE);
26419 /* Substitutes LATE_RETURN_TYPE for 'auto' in TYPE and returns the
26420 result. */
26422 tree
26423 splice_late_return_type (tree type, tree late_return_type)
26425 if (is_auto (type))
26427 if (late_return_type)
26428 return late_return_type;
26430 tree idx = get_template_parm_index (type);
26431 if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
26432 /* In an abbreviated function template we didn't know we were dealing
26433 with a function template when we saw the auto return type, so update
26434 it to have the correct level. */
26435 return make_auto_1 (TYPE_IDENTIFIER (type), true);
26437 return type;
26440 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
26441 'decltype(auto)' or a deduced class template. */
26443 bool
26444 is_auto (const_tree type)
26446 if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
26447 && (TYPE_IDENTIFIER (type) == auto_identifier
26448 || TYPE_IDENTIFIER (type) == decltype_auto_identifier
26449 || CLASS_PLACEHOLDER_TEMPLATE (type)))
26450 return true;
26451 else
26452 return false;
26455 /* for_each_template_parm callback for type_uses_auto. */
26458 is_auto_r (tree tp, void */*data*/)
26460 return is_auto (tp);
26463 /* Returns the TEMPLATE_TYPE_PARM in TYPE representing `auto' iff TYPE contains
26464 a use of `auto'. Returns NULL_TREE otherwise. */
26466 tree
26467 type_uses_auto (tree type)
26469 if (type == NULL_TREE)
26470 return NULL_TREE;
26471 else if (flag_concepts)
26473 /* The Concepts TS allows multiple autos in one type-specifier; just
26474 return the first one we find, do_auto_deduction will collect all of
26475 them. */
26476 if (uses_template_parms (type))
26477 return for_each_template_parm (type, is_auto_r, /*data*/NULL,
26478 /*visited*/NULL, /*nondeduced*/true);
26479 else
26480 return NULL_TREE;
26482 else
26483 return find_type_usage (type, is_auto);
26486 /* For a given template T, return the vector of typedefs referenced
26487 in T for which access check is needed at T instantiation time.
26488 T is either a FUNCTION_DECL or a RECORD_TYPE.
26489 Those typedefs were added to T by the function
26490 append_type_to_template_for_access_check. */
26492 vec<qualified_typedef_usage_t, va_gc> *
26493 get_types_needing_access_check (tree t)
26495 tree ti;
26496 vec<qualified_typedef_usage_t, va_gc> *result = NULL;
26498 if (!t || t == error_mark_node)
26499 return NULL;
26501 if (!(ti = get_template_info (t)))
26502 return NULL;
26504 if (CLASS_TYPE_P (t)
26505 || TREE_CODE (t) == FUNCTION_DECL)
26507 if (!TI_TEMPLATE (ti))
26508 return NULL;
26510 result = TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti);
26513 return result;
26516 /* Append the typedef TYPE_DECL used in template T to a list of typedefs
26517 tied to T. That list of typedefs will be access checked at
26518 T instantiation time.
26519 T is either a FUNCTION_DECL or a RECORD_TYPE.
26520 TYPE_DECL is a TYPE_DECL node representing a typedef.
26521 SCOPE is the scope through which TYPE_DECL is accessed.
26522 LOCATION is the location of the usage point of TYPE_DECL.
26524 This function is a subroutine of
26525 append_type_to_template_for_access_check. */
26527 static void
26528 append_type_to_template_for_access_check_1 (tree t,
26529 tree type_decl,
26530 tree scope,
26531 location_t location)
26533 qualified_typedef_usage_t typedef_usage;
26534 tree ti;
26536 if (!t || t == error_mark_node)
26537 return;
26539 gcc_assert ((TREE_CODE (t) == FUNCTION_DECL
26540 || CLASS_TYPE_P (t))
26541 && type_decl
26542 && TREE_CODE (type_decl) == TYPE_DECL
26543 && scope);
26545 if (!(ti = get_template_info (t)))
26546 return;
26548 gcc_assert (TI_TEMPLATE (ti));
26550 typedef_usage.typedef_decl = type_decl;
26551 typedef_usage.context = scope;
26552 typedef_usage.locus = location;
26554 vec_safe_push (TI_TYPEDEFS_NEEDING_ACCESS_CHECKING (ti), typedef_usage);
26557 /* Append TYPE_DECL to the template TEMPL.
26558 TEMPL is either a class type, a FUNCTION_DECL or a a TEMPLATE_DECL.
26559 At TEMPL instanciation time, TYPE_DECL will be checked to see
26560 if it can be accessed through SCOPE.
26561 LOCATION is the location of the usage point of TYPE_DECL.
26563 e.g. consider the following code snippet:
26565 class C
26567 typedef int myint;
26570 template<class U> struct S
26572 C::myint mi; // <-- usage point of the typedef C::myint
26575 S<char> s;
26577 At S<char> instantiation time, we need to check the access of C::myint
26578 In other words, we need to check the access of the myint typedef through
26579 the C scope. For that purpose, this function will add the myint typedef
26580 and the scope C through which its being accessed to a list of typedefs
26581 tied to the template S. That list will be walked at template instantiation
26582 time and access check performed on each typedefs it contains.
26583 Note that this particular code snippet should yield an error because
26584 myint is private to C. */
26586 void
26587 append_type_to_template_for_access_check (tree templ,
26588 tree type_decl,
26589 tree scope,
26590 location_t location)
26592 qualified_typedef_usage_t *iter;
26593 unsigned i;
26595 gcc_assert (type_decl && (TREE_CODE (type_decl) == TYPE_DECL));
26597 /* Make sure we don't append the type to the template twice. */
26598 FOR_EACH_VEC_SAFE_ELT (get_types_needing_access_check (templ), i, iter)
26599 if (iter->typedef_decl == type_decl && scope == iter->context)
26600 return;
26602 append_type_to_template_for_access_check_1 (templ, type_decl,
26603 scope, location);
26606 /* Convert the generic type parameters in PARM that match the types given in the
26607 range [START_IDX, END_IDX) from the current_template_parms into generic type
26608 packs. */
26610 tree
26611 convert_generic_types_to_packs (tree parm, int start_idx, int end_idx)
26613 tree current = current_template_parms;
26614 int depth = TMPL_PARMS_DEPTH (current);
26615 current = INNERMOST_TEMPLATE_PARMS (current);
26616 tree replacement = make_tree_vec (TREE_VEC_LENGTH (current));
26618 for (int i = 0; i < start_idx; ++i)
26619 TREE_VEC_ELT (replacement, i)
26620 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
26622 for (int i = start_idx; i < end_idx; ++i)
26624 /* Create a distinct parameter pack type from the current parm and add it
26625 to the replacement args to tsubst below into the generic function
26626 parameter. */
26628 tree o = TREE_TYPE (TREE_VALUE
26629 (TREE_VEC_ELT (current, i)));
26630 tree t = copy_type (o);
26631 TEMPLATE_TYPE_PARM_INDEX (t)
26632 = reduce_template_parm_level (TEMPLATE_TYPE_PARM_INDEX (o),
26633 o, 0, 0, tf_none);
26634 TREE_TYPE (TEMPLATE_TYPE_DECL (t)) = t;
26635 TYPE_STUB_DECL (t) = TYPE_NAME (t) = TEMPLATE_TYPE_DECL (t);
26636 TYPE_MAIN_VARIANT (t) = t;
26637 TEMPLATE_TYPE_PARAMETER_PACK (t) = true;
26638 TYPE_CANONICAL (t) = canonical_type_parameter (t);
26639 TREE_VEC_ELT (replacement, i) = t;
26640 TREE_VALUE (TREE_VEC_ELT (current, i)) = TREE_CHAIN (t);
26643 for (int i = end_idx, e = TREE_VEC_LENGTH (current); i < e; ++i)
26644 TREE_VEC_ELT (replacement, i)
26645 = TREE_TYPE (TREE_VALUE (TREE_VEC_ELT (current, i)));
26647 /* If there are more levels then build up the replacement with the outer
26648 template parms. */
26649 if (depth > 1)
26650 replacement = add_to_template_args (template_parms_to_args
26651 (TREE_CHAIN (current_template_parms)),
26652 replacement);
26654 return tsubst (parm, replacement, tf_none, NULL_TREE);
26657 /* Entries in the decl_constraint hash table. */
26658 struct GTY((for_user)) constr_entry
26660 tree decl;
26661 tree ci;
26664 /* Hashing function and equality for constraint entries. */
26665 struct constr_hasher : ggc_ptr_hash<constr_entry>
26667 static hashval_t hash (constr_entry *e)
26669 return (hashval_t)DECL_UID (e->decl);
26672 static bool equal (constr_entry *e1, constr_entry *e2)
26674 return e1->decl == e2->decl;
26678 /* A mapping from declarations to constraint information. Note that
26679 both templates and their underlying declarations are mapped to the
26680 same constraint information.
26682 FIXME: This is defined in pt.c because garbage collection
26683 code is not being generated for constraint.cc. */
26685 static GTY (()) hash_table<constr_hasher> *decl_constraints;
26687 /* Returns the template constraints of declaration T. If T is not
26688 constrained, return NULL_TREE. Note that T must be non-null. */
26690 tree
26691 get_constraints (tree t)
26693 if (!flag_concepts)
26694 return NULL_TREE;
26696 gcc_assert (DECL_P (t));
26697 if (TREE_CODE (t) == TEMPLATE_DECL)
26698 t = DECL_TEMPLATE_RESULT (t);
26699 constr_entry elt = { t, NULL_TREE };
26700 constr_entry* found = decl_constraints->find (&elt);
26701 if (found)
26702 return found->ci;
26703 else
26704 return NULL_TREE;
26707 /* Associate the given constraint information CI with the declaration
26708 T. If T is a template, then the constraints are associated with
26709 its underlying declaration. Don't build associations if CI is
26710 NULL_TREE. */
26712 void
26713 set_constraints (tree t, tree ci)
26715 if (!ci)
26716 return;
26717 gcc_assert (t && flag_concepts);
26718 if (TREE_CODE (t) == TEMPLATE_DECL)
26719 t = DECL_TEMPLATE_RESULT (t);
26720 gcc_assert (!get_constraints (t));
26721 constr_entry elt = {t, ci};
26722 constr_entry** slot = decl_constraints->find_slot (&elt, INSERT);
26723 constr_entry* entry = ggc_alloc<constr_entry> ();
26724 *entry = elt;
26725 *slot = entry;
26728 /* Remove the associated constraints of the declaration T. */
26730 void
26731 remove_constraints (tree t)
26733 gcc_assert (DECL_P (t));
26734 if (TREE_CODE (t) == TEMPLATE_DECL)
26735 t = DECL_TEMPLATE_RESULT (t);
26737 constr_entry elt = {t, NULL_TREE};
26738 constr_entry** slot = decl_constraints->find_slot (&elt, NO_INSERT);
26739 if (slot)
26740 decl_constraints->clear_slot (slot);
26743 /* Memoized satisfaction results for declarations. This
26744 maps the pair (constraint_info, arguments) to the result computed
26745 by constraints_satisfied_p. */
26747 struct GTY((for_user)) constraint_sat_entry
26749 tree ci;
26750 tree args;
26751 tree result;
26754 /* Hashing function and equality for constraint entries. */
26756 struct constraint_sat_hasher : ggc_ptr_hash<constraint_sat_entry>
26758 static hashval_t hash (constraint_sat_entry *e)
26760 hashval_t val = iterative_hash_object(e->ci, 0);
26761 return iterative_hash_template_arg (e->args, val);
26764 static bool equal (constraint_sat_entry *e1, constraint_sat_entry *e2)
26766 return e1->ci == e2->ci && comp_template_args (e1->args, e2->args);
26770 /* Memoized satisfaction results for concept checks. */
26772 struct GTY((for_user)) concept_spec_entry
26774 tree tmpl;
26775 tree args;
26776 tree result;
26779 /* Hashing function and equality for constraint entries. */
26781 struct concept_spec_hasher : ggc_ptr_hash<concept_spec_entry>
26783 static hashval_t hash (concept_spec_entry *e)
26785 return hash_tmpl_and_args (e->tmpl, e->args);
26788 static bool equal (concept_spec_entry *e1, concept_spec_entry *e2)
26790 ++comparing_specializations;
26791 bool eq = e1->tmpl == e2->tmpl && comp_template_args (e1->args, e2->args);
26792 --comparing_specializations;
26793 return eq;
26797 static GTY (()) hash_table<constraint_sat_hasher> *constraint_memos;
26798 static GTY (()) hash_table<concept_spec_hasher> *concept_memos;
26800 /* Search for a memoized satisfaction result. Returns one of the
26801 truth value nodes if previously memoized, or NULL_TREE otherwise. */
26803 tree
26804 lookup_constraint_satisfaction (tree ci, tree args)
26806 constraint_sat_entry elt = { ci, args, NULL_TREE };
26807 constraint_sat_entry* found = constraint_memos->find (&elt);
26808 if (found)
26809 return found->result;
26810 else
26811 return NULL_TREE;
26814 /* Memoize the result of a satisfication test. Returns the saved result. */
26816 tree
26817 memoize_constraint_satisfaction (tree ci, tree args, tree result)
26819 constraint_sat_entry elt = {ci, args, result};
26820 constraint_sat_entry** slot = constraint_memos->find_slot (&elt, INSERT);
26821 constraint_sat_entry* entry = ggc_alloc<constraint_sat_entry> ();
26822 *entry = elt;
26823 *slot = entry;
26824 return result;
26827 /* Search for a memoized satisfaction result for a concept. */
26829 tree
26830 lookup_concept_satisfaction (tree tmpl, tree args)
26832 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26833 concept_spec_entry* found = concept_memos->find (&elt);
26834 if (found)
26835 return found->result;
26836 else
26837 return NULL_TREE;
26840 /* Memoize the result of a concept check. Returns the saved result. */
26842 tree
26843 memoize_concept_satisfaction (tree tmpl, tree args, tree result)
26845 concept_spec_entry elt = {tmpl, args, result};
26846 concept_spec_entry** slot = concept_memos->find_slot (&elt, INSERT);
26847 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26848 *entry = elt;
26849 *slot = entry;
26850 return result;
26853 static GTY (()) hash_table<concept_spec_hasher> *concept_expansions;
26855 /* Returns a prior concept specialization. This returns the substituted
26856 and normalized constraints defined by the concept. */
26858 tree
26859 get_concept_expansion (tree tmpl, tree args)
26861 concept_spec_entry elt = { tmpl, args, NULL_TREE };
26862 concept_spec_entry* found = concept_expansions->find (&elt);
26863 if (found)
26864 return found->result;
26865 else
26866 return NULL_TREE;
26869 /* Save a concept expansion for later. */
26871 tree
26872 save_concept_expansion (tree tmpl, tree args, tree def)
26874 concept_spec_entry elt = {tmpl, args, def};
26875 concept_spec_entry** slot = concept_expansions->find_slot (&elt, INSERT);
26876 concept_spec_entry* entry = ggc_alloc<concept_spec_entry> ();
26877 *entry = elt;
26878 *slot = entry;
26879 return def;
26882 static hashval_t
26883 hash_subsumption_args (tree t1, tree t2)
26885 gcc_assert (TREE_CODE (t1) == CHECK_CONSTR);
26886 gcc_assert (TREE_CODE (t2) == CHECK_CONSTR);
26887 int val = 0;
26888 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t1), val);
26889 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t1), val);
26890 val = iterative_hash_object (CHECK_CONSTR_CONCEPT (t2), val);
26891 val = iterative_hash_template_arg (CHECK_CONSTR_ARGS (t2), val);
26892 return val;
26895 /* Compare the constraints of two subsumption entries. The LEFT1 and
26896 LEFT2 arguments comprise the first subsumption pair and the RIGHT1
26897 and RIGHT2 arguments comprise the second. These are all CHECK_CONSTRs. */
26899 static bool
26900 comp_subsumption_args (tree left1, tree left2, tree right1, tree right2)
26902 if (CHECK_CONSTR_CONCEPT (left1) == CHECK_CONSTR_CONCEPT (right1))
26903 if (CHECK_CONSTR_CONCEPT (left2) == CHECK_CONSTR_CONCEPT (right2))
26904 if (comp_template_args (CHECK_CONSTR_ARGS (left1),
26905 CHECK_CONSTR_ARGS (right1)))
26906 return comp_template_args (CHECK_CONSTR_ARGS (left2),
26907 CHECK_CONSTR_ARGS (right2));
26908 return false;
26911 /* Key/value pair for learning and memoizing subsumption results. This
26912 associates a pair of check constraints (including arguments) with
26913 a boolean value indicating the result. */
26915 struct GTY((for_user)) subsumption_entry
26917 tree t1;
26918 tree t2;
26919 bool result;
26922 /* Hashing function and equality for constraint entries. */
26924 struct subsumption_hasher : ggc_ptr_hash<subsumption_entry>
26926 static hashval_t hash (subsumption_entry *e)
26928 return hash_subsumption_args (e->t1, e->t2);
26931 static bool equal (subsumption_entry *e1, subsumption_entry *e2)
26933 ++comparing_specializations;
26934 bool eq = comp_subsumption_args(e1->t1, e1->t2, e2->t1, e2->t2);
26935 --comparing_specializations;
26936 return eq;
26940 static GTY (()) hash_table<subsumption_hasher> *subsumption_table;
26942 /* Search for a previously cached subsumption result. */
26944 bool*
26945 lookup_subsumption_result (tree t1, tree t2)
26947 subsumption_entry elt = { t1, t2, false };
26948 subsumption_entry* found = subsumption_table->find (&elt);
26949 if (found)
26950 return &found->result;
26951 else
26952 return 0;
26955 /* Save a subsumption result. */
26957 bool
26958 save_subsumption_result (tree t1, tree t2, bool result)
26960 subsumption_entry elt = {t1, t2, result};
26961 subsumption_entry** slot = subsumption_table->find_slot (&elt, INSERT);
26962 subsumption_entry* entry = ggc_alloc<subsumption_entry> ();
26963 *entry = elt;
26964 *slot = entry;
26965 return result;
26968 /* Set up the hash table for constraint association. */
26970 void
26971 init_constraint_processing (void)
26973 if (!flag_concepts)
26974 return;
26976 decl_constraints = hash_table<constr_hasher>::create_ggc(37);
26977 constraint_memos = hash_table<constraint_sat_hasher>::create_ggc(37);
26978 concept_memos = hash_table<concept_spec_hasher>::create_ggc(37);
26979 concept_expansions = hash_table<concept_spec_hasher>::create_ggc(37);
26980 subsumption_table = hash_table<subsumption_hasher>::create_ggc(37);
26983 /* __integer_pack(N) in a pack expansion expands to a sequence of numbers from
26984 0..N-1. */
26986 void
26987 declare_integer_pack (void)
26989 tree ipfn = push_library_fn (get_identifier ("__integer_pack"),
26990 build_function_type_list (integer_type_node,
26991 integer_type_node,
26992 NULL_TREE),
26993 NULL_TREE, ECF_CONST);
26994 DECL_DECLARED_CONSTEXPR_P (ipfn) = true;
26995 DECL_BUILT_IN_CLASS (ipfn) = BUILT_IN_FRONTEND;
26998 /* Set up the hash tables for template instantiations. */
27000 void
27001 init_template_processing (void)
27003 decl_specializations = hash_table<spec_hasher>::create_ggc (37);
27004 type_specializations = hash_table<spec_hasher>::create_ggc (37);
27006 if (cxx_dialect >= cxx11)
27007 declare_integer_pack ();
27010 /* Print stats about the template hash tables for -fstats. */
27012 void
27013 print_template_statistics (void)
27015 fprintf (stderr, "decl_specializations: size %ld, %ld elements, "
27016 "%f collisions\n", (long) decl_specializations->size (),
27017 (long) decl_specializations->elements (),
27018 decl_specializations->collisions ());
27019 fprintf (stderr, "type_specializations: size %ld, %ld elements, "
27020 "%f collisions\n", (long) type_specializations->size (),
27021 (long) type_specializations->elements (),
27022 type_specializations->collisions ());
27025 #if CHECKING_P
27027 namespace selftest {
27029 /* Verify that build_non_dependent_expr () works, for various expressions,
27030 and that location wrappers don't affect the results. */
27032 static void
27033 test_build_non_dependent_expr ()
27035 location_t loc = BUILTINS_LOCATION;
27037 /* Verify constants, without and with location wrappers. */
27038 tree int_cst = build_int_cst (integer_type_node, 42);
27039 ASSERT_EQ (int_cst, build_non_dependent_expr (int_cst));
27041 tree wrapped_int_cst = maybe_wrap_with_location (int_cst, loc);
27042 ASSERT_TRUE (location_wrapper_p (wrapped_int_cst));
27043 ASSERT_EQ (wrapped_int_cst, build_non_dependent_expr (wrapped_int_cst));
27045 tree string_lit = build_string (4, "foo");
27046 TREE_TYPE (string_lit) = char_array_type_node;
27047 string_lit = fix_string_type (string_lit);
27048 ASSERT_EQ (string_lit, build_non_dependent_expr (string_lit));
27050 tree wrapped_string_lit = maybe_wrap_with_location (string_lit, loc);
27051 ASSERT_TRUE (location_wrapper_p (wrapped_string_lit));
27052 ASSERT_EQ (wrapped_string_lit,
27053 build_non_dependent_expr (wrapped_string_lit));
27056 /* Verify that type_dependent_expression_p () works correctly, even
27057 in the presence of location wrapper nodes. */
27059 static void
27060 test_type_dependent_expression_p ()
27062 location_t loc = BUILTINS_LOCATION;
27064 tree name = get_identifier ("foo");
27066 /* If no templates are involved, nothing is type-dependent. */
27067 gcc_assert (!processing_template_decl);
27068 ASSERT_FALSE (type_dependent_expression_p (name));
27070 ++processing_template_decl;
27072 /* Within a template, an unresolved name is always type-dependent. */
27073 ASSERT_TRUE (type_dependent_expression_p (name));
27075 /* Ensure it copes with NULL_TREE and errors. */
27076 ASSERT_FALSE (type_dependent_expression_p (NULL_TREE));
27077 ASSERT_FALSE (type_dependent_expression_p (error_mark_node));
27079 /* A USING_DECL in a template should be type-dependent, even if wrapped
27080 with a location wrapper (PR c++/83799). */
27081 tree using_decl = build_lang_decl (USING_DECL, name, NULL_TREE);
27082 TREE_TYPE (using_decl) = integer_type_node;
27083 ASSERT_TRUE (type_dependent_expression_p (using_decl));
27084 tree wrapped_using_decl = maybe_wrap_with_location (using_decl, loc);
27085 ASSERT_TRUE (location_wrapper_p (wrapped_using_decl));
27086 ASSERT_TRUE (type_dependent_expression_p (wrapped_using_decl));
27088 --processing_template_decl;
27091 /* Run all of the selftests within this file. */
27093 void
27094 cp_pt_c_tests ()
27096 test_build_non_dependent_expr ();
27097 test_type_dependent_expression_p ();
27100 } // namespace selftest
27102 #endif /* #if CHECKING_P */
27104 #include "gt-cp-pt.h"